Move DWARF code to dwarf2/ subdirectory
[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/index-cache.h"
36 #include "dwarf2/index-common.h"
37 #include "dwarf2/leb.h"
38 #include "bfd.h"
39 #include "elf-bfd.h"
40 #include "symtab.h"
41 #include "gdbtypes.h"
42 #include "objfiles.h"
43 #include "dwarf2.h"
44 #include "buildsym.h"
45 #include "demangle.h"
46 #include "gdb-demangle.h"
47 #include "filenames.h" /* for DOSish file names */
48 #include "macrotab.h"
49 #include "language.h"
50 #include "complaints.h"
51 #include "dwarf2/expr.h"
52 #include "dwarf2/loc.h"
53 #include "cp-support.h"
54 #include "hashtab.h"
55 #include "command.h"
56 #include "gdbcmd.h"
57 #include "block.h"
58 #include "addrmap.h"
59 #include "typeprint.h"
60 #include "psympriv.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "build-id.h"
70 #include "namespace.h"
71 #include "gdbsupport/function-view.h"
72 #include "gdbsupport/gdb_optional.h"
73 #include "gdbsupport/underlying.h"
74 #include "gdbsupport/hash_enum.h"
75 #include "filename-seen-cache.h"
76 #include "producer.h"
77 #include <fcntl.h>
78 #include <algorithm>
79 #include <unordered_map>
80 #include "gdbsupport/selftest.h"
81 #include "rust-lang.h"
82 #include "gdbsupport/pathstuff.h"
83
84 /* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87 static unsigned int dwarf_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static unsigned int dwarf_die_debug = 0;
91
92 /* When non-zero, dump line number entries as they are read in. */
93 static unsigned int dwarf_line_debug = 0;
94
95 /* When true, cross-check physname against demangler. */
96 static bool check_physname = false;
97
98 /* When true, do not reject deprecated .gdb_index sections. */
99 static bool use_deprecated_index_sections = false;
100
101 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
102
103 /* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105 static int dwarf2_locexpr_index;
106 static int dwarf2_loclist_index;
107 static int dwarf2_locexpr_block_index;
108 static int dwarf2_loclist_block_index;
109
110 /* An index into a (C++) symbol name component in a symbol name as
111 recorded in the mapped_index's symbol table. For each C++ symbol
112 in the symbol table, we record one entry for the start of each
113 component in the symbol in a table of name components, and then
114 sort the table, in order to be able to binary search symbol names,
115 ignoring leading namespaces, both completion and regular look up.
116 For example, for symbol "A::B::C", we'll have an entry that points
117 to "A::B::C", another that points to "B::C", and another for "C".
118 Note that function symbols in GDB index have no parameter
119 information, just the function/method names. You can convert a
120 name_component to a "const char *" using the
121 'mapped_index::symbol_name_at(offset_type)' method. */
122
123 struct name_component
124 {
125 /* Offset in the symbol name where the component starts. Stored as
126 a (32-bit) offset instead of a pointer to save memory and improve
127 locality on 64-bit architectures. */
128 offset_type name_offset;
129
130 /* The symbol's index in the symbol and constant pool tables of a
131 mapped_index. */
132 offset_type idx;
133 };
134
135 /* Base class containing bits shared by both .gdb_index and
136 .debug_name indexes. */
137
138 struct mapped_index_base
139 {
140 mapped_index_base () = default;
141 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
142
143 /* The name_component table (a sorted vector). See name_component's
144 description above. */
145 std::vector<name_component> name_components;
146
147 /* How NAME_COMPONENTS is sorted. */
148 enum case_sensitivity name_components_casing;
149
150 /* Return the number of names in the symbol table. */
151 virtual size_t symbol_name_count () const = 0;
152
153 /* Get the name of the symbol at IDX in the symbol table. */
154 virtual const char *symbol_name_at (offset_type idx) const = 0;
155
156 /* Return whether the name at IDX in the symbol table should be
157 ignored. */
158 virtual bool symbol_name_slot_invalid (offset_type idx) const
159 {
160 return false;
161 }
162
163 /* Build the symbol name component sorted vector, if we haven't
164 yet. */
165 void build_name_components ();
166
167 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
168 possible matches for LN_NO_PARAMS in the name component
169 vector. */
170 std::pair<std::vector<name_component>::const_iterator,
171 std::vector<name_component>::const_iterator>
172 find_name_components_bounds (const lookup_name_info &ln_no_params,
173 enum language lang) const;
174
175 /* Prevent deleting/destroying via a base class pointer. */
176 protected:
177 ~mapped_index_base() = default;
178 };
179
180 /* A description of the mapped index. The file format is described in
181 a comment by the code that writes the index. */
182 struct mapped_index final : public mapped_index_base
183 {
184 /* A slot/bucket in the symbol table hash. */
185 struct symbol_table_slot
186 {
187 const offset_type name;
188 const offset_type vec;
189 };
190
191 /* Index data format version. */
192 int version = 0;
193
194 /* The address table data. */
195 gdb::array_view<const gdb_byte> address_table;
196
197 /* The symbol table, implemented as a hash table. */
198 gdb::array_view<symbol_table_slot> symbol_table;
199
200 /* A pointer to the constant pool. */
201 const char *constant_pool = nullptr;
202
203 bool symbol_name_slot_invalid (offset_type idx) const override
204 {
205 const auto &bucket = this->symbol_table[idx];
206 return bucket.name == 0 && bucket.vec == 0;
207 }
208
209 /* Convenience method to get at the name of the symbol at IDX in the
210 symbol table. */
211 const char *symbol_name_at (offset_type idx) const override
212 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
213
214 size_t symbol_name_count () const override
215 { return this->symbol_table.size (); }
216 };
217
218 /* A description of the mapped .debug_names.
219 Uninitialized map has CU_COUNT 0. */
220 struct mapped_debug_names final : public mapped_index_base
221 {
222 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
223 : dwarf2_per_objfile (dwarf2_per_objfile_)
224 {}
225
226 struct dwarf2_per_objfile *dwarf2_per_objfile;
227 bfd_endian dwarf5_byte_order;
228 bool dwarf5_is_dwarf64;
229 bool augmentation_is_gdb;
230 uint8_t offset_size;
231 uint32_t cu_count = 0;
232 uint32_t tu_count, bucket_count, name_count;
233 const gdb_byte *cu_table_reordered, *tu_table_reordered;
234 const uint32_t *bucket_table_reordered, *hash_table_reordered;
235 const gdb_byte *name_table_string_offs_reordered;
236 const gdb_byte *name_table_entry_offs_reordered;
237 const gdb_byte *entry_pool;
238
239 struct index_val
240 {
241 ULONGEST dwarf_tag;
242 struct attr
243 {
244 /* Attribute name DW_IDX_*. */
245 ULONGEST dw_idx;
246
247 /* Attribute form DW_FORM_*. */
248 ULONGEST form;
249
250 /* Value if FORM is DW_FORM_implicit_const. */
251 LONGEST implicit_const;
252 };
253 std::vector<attr> attr_vec;
254 };
255
256 std::unordered_map<ULONGEST, index_val> abbrev_map;
257
258 const char *namei_to_name (uint32_t namei) const;
259
260 /* Implementation of the mapped_index_base virtual interface, for
261 the name_components cache. */
262
263 const char *symbol_name_at (offset_type idx) const override
264 { return namei_to_name (idx); }
265
266 size_t symbol_name_count () const override
267 { return this->name_count; }
268 };
269
270 /* See dwarf2read.h. */
271
272 dwarf2_per_objfile *
273 get_dwarf2_per_objfile (struct objfile *objfile)
274 {
275 return dwarf2_objfile_data_key.get (objfile);
276 }
277
278 /* Default names of the debugging sections. */
279
280 /* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
283 static const struct dwarf2_debug_sections dwarf2_elf_names =
284 {
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_loclists", ".zdebug_loclists" },
290 { ".debug_macinfo", ".zdebug_macinfo" },
291 { ".debug_macro", ".zdebug_macro" },
292 { ".debug_str", ".zdebug_str" },
293 { ".debug_str_offsets", ".zdebug_str_offsets" },
294 { ".debug_line_str", ".zdebug_line_str" },
295 { ".debug_ranges", ".zdebug_ranges" },
296 { ".debug_rnglists", ".zdebug_rnglists" },
297 { ".debug_types", ".zdebug_types" },
298 { ".debug_addr", ".zdebug_addr" },
299 { ".debug_frame", ".zdebug_frame" },
300 { ".eh_frame", NULL },
301 { ".gdb_index", ".zgdb_index" },
302 { ".debug_names", ".zdebug_names" },
303 { ".debug_aranges", ".zdebug_aranges" },
304 23
305 };
306
307 /* List of DWO/DWP sections. */
308
309 static const struct dwop_section_names
310 {
311 struct dwarf2_section_names abbrev_dwo;
312 struct dwarf2_section_names info_dwo;
313 struct dwarf2_section_names line_dwo;
314 struct dwarf2_section_names loc_dwo;
315 struct dwarf2_section_names loclists_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
338 };
339
340 /* local data types */
341
342 /* The data in a compilation unit header, after target2host
343 translation, looks like this. */
344 struct comp_unit_head
345 {
346 unsigned int length;
347 short version;
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
350 sect_offset abbrev_sect_off;
351
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
354
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
357
358 enum dwarf_unit_type unit_type;
359
360 /* Offset to the first byte of this compilation unit header in the
361 .debug_info section, for resolving relative reference dies. */
362 sect_offset sect_off;
363
364 /* Offset to first die in this cu from the start of the cu.
365 This will be the first byte following the compilation unit header. */
366 cu_offset first_die_cu_offset;
367
368
369 /* 64-bit signature of this unit. For type units, it denotes the signature of
370 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
371 Also used in DWARF 5, to denote the dwo id when the unit type is
372 DW_UT_skeleton or DW_UT_split_compile. */
373 ULONGEST signature;
374
375 /* For types, offset in the type's DIE of the type defined by this TU. */
376 cu_offset type_cu_offset_in_tu;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
428
429 /* Non-zero if base_address has been set. */
430 int base_known = 0;
431
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
435
436 const char *producer = nullptr;
437
438 private:
439 /* The symtab builder for this CU. This is only non-NULL when full
440 symbols are being read. */
441 std::unique_ptr<buildsym_compunit> m_builder;
442
443 public:
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
453 struct pending **list_in_scope = nullptr;
454
455 /* Hash table holding all the loaded partial DIEs
456 with partial_die->offset.SECT_OFF as hash. */
457 htab_t partial_dies = nullptr;
458
459 /* Storage for things with the same lifetime as this read-in compilation
460 unit, including partial DIEs. */
461 auto_obstack comp_unit_obstack;
462
463 /* When multiple dwarf2_cu structures are living in memory, this field
464 chains them all together, so that they can be released efficiently.
465 We will probably also want a generation counter so that most-recently-used
466 compilation units are cached... */
467 struct dwarf2_per_cu_data *read_in_chain = nullptr;
468
469 /* Backlink to our per_cu entry. */
470 struct dwarf2_per_cu_data *per_cu;
471
472 /* How many compilation units ago was this CU last referenced? */
473 int last_used = 0;
474
475 /* A hash table of DIE cu_offset for following references with
476 die_info->offset.sect_off as hash. */
477 htab_t die_hash = nullptr;
478
479 /* Full DIEs if read in. */
480 struct die_info *dies = nullptr;
481
482 /* A set of pointers to dwarf2_per_cu_data objects for compilation
483 units referenced by this one. Only set during full symbol processing;
484 partial symbol tables do not have dependencies. */
485 htab_t dependencies = nullptr;
486
487 /* Header data from the line table, during full symbol processing. */
488 struct line_header *line_header = nullptr;
489 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
490 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
491 this is the DW_TAG_compile_unit die for this CU. We'll hold on
492 to the line header as long as this DIE is being processed. See
493 process_die_scope. */
494 die_info *line_header_die_owner = nullptr;
495
496 /* A list of methods which need to have physnames computed
497 after all type information has been read. */
498 std::vector<delayed_method_info> method_list;
499
500 /* To be copied to symtab->call_site_htab. */
501 htab_t call_site_htab = nullptr;
502
503 /* Non-NULL if this CU came from a DWO file.
504 There is an invariant here that is important to remember:
505 Except for attributes copied from the top level DIE in the "main"
506 (or "stub") file in preparation for reading the DWO file
507 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
508 Either there isn't a DWO file (in which case this is NULL and the point
509 is moot), or there is and either we're not going to read it (in which
510 case this is NULL) or there is and we are reading it (in which case this
511 is non-NULL). */
512 struct dwo_unit *dwo_unit = nullptr;
513
514 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
515 Note this value comes from the Fission stub CU/TU's DIE. */
516 gdb::optional<ULONGEST> addr_base;
517
518 /* The DW_AT_rnglists_base attribute if present.
519 Note this value comes from the Fission stub CU/TU's DIE.
520 Also note that the value is zero in the non-DWO case so this value can
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
527 ULONGEST ranges_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab.
611 This PER_CU is recognized by having no section. */
612 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
613 struct dwarf2_per_cu_data per_cu;
614
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
618 std::vector<signatured_type *> *tus;
619
620 /* The compunit symtab.
621 Type units in a group needn't all be defined in the same source file,
622 so we create an essentially anonymous symtab as the compunit symtab. */
623 struct compunit_symtab *compunit_symtab;
624
625 /* The data used to construct the hash key. */
626 struct stmt_list_hash hash;
627
628 /* The number of symtabs from the line header.
629 The value here must match line_header.num_file_names. */
630 unsigned int num_symtabs;
631
632 /* The symbol tables for this TU (obtained from the files listed in
633 DW_AT_stmt_list).
634 WARNING: The order of entries here must match the order of entries
635 in the line header. After the first TU using this type_unit_group, the
636 line header for the subsequent TUs is recreated from this. This is done
637 because we need to use the same symtabs for each TU using the same
638 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
639 there's no guarantee the line header doesn't have duplicate entries. */
640 struct symtab **symtabs;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_t cus {};
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_t tus {};
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_t loaded_cus {};
872 htab_t loaded_tus {};
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (struct dwarf2_per_cu_data *this_cu,
918 struct abbrev_table *abbrev_table,
919 int use_existing_cu, int keep,
920 bool skip_partial);
921
922 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 ~cutu_reader ();
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 int has_children = 0;
933 bool dummy_p = false;
934
935 private:
936 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
937 int use_existing_cu, int keep);
938
939 struct dwarf2_per_cu_data *m_this_cu;
940 int m_keep = 0;
941 std::unique_ptr<dwarf2_cu> m_new_cu;
942
943 /* The ordinary abbreviation table. */
944 abbrev_table_up m_abbrev_table_holder;
945
946 /* The DWO abbreviation table. */
947 abbrev_table_up m_dwo_abbrev_table;
948 };
949
950 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
951 later. */
952 typedef int dir_index;
953
954 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
955 and later. */
956 typedef int file_name_index;
957
958 struct file_entry
959 {
960 file_entry () = default;
961
962 file_entry (const char *name_, dir_index d_index_,
963 unsigned int mod_time_, unsigned int length_)
964 : name (name_),
965 d_index (d_index_),
966 mod_time (mod_time_),
967 length (length_)
968 {}
969
970 /* Return the include directory at D_INDEX stored in LH. Returns
971 NULL if D_INDEX is out of bounds. */
972 const char *include_dir (const line_header *lh) const;
973
974 /* The file name. Note this is an observing pointer. The memory is
975 owned by debug_line_buffer. */
976 const char *name {};
977
978 /* The directory index (1-based). */
979 dir_index d_index {};
980
981 unsigned int mod_time {};
982
983 unsigned int length {};
984
985 /* True if referenced by the Line Number Program. */
986 bool included_p {};
987
988 /* The associated symbol table, if any. */
989 struct symtab *symtab {};
990 };
991
992 /* The line number information for a compilation unit (found in the
993 .debug_line section) begins with a "statement program header",
994 which contains the following information. */
995 struct line_header
996 {
997 line_header ()
998 : offset_in_dwz {}
999 {}
1000
1001 /* Add an entry to the include directory table. */
1002 void add_include_dir (const char *include_dir);
1003
1004 /* Add an entry to the file name table. */
1005 void add_file_name (const char *name, dir_index d_index,
1006 unsigned int mod_time, unsigned int length);
1007
1008 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1009 Returns NULL if INDEX is out of bounds. */
1010 const char *include_dir_at (dir_index index) const
1011 {
1012 int vec_index;
1013 if (version >= 5)
1014 vec_index = index;
1015 else
1016 vec_index = index - 1;
1017 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1018 return NULL;
1019 return m_include_dirs[vec_index];
1020 }
1021
1022 bool is_valid_file_index (int file_index)
1023 {
1024 if (version >= 5)
1025 return 0 <= file_index && file_index < file_names_size ();
1026 return 1 <= file_index && file_index <= file_names_size ();
1027 }
1028
1029 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1030 Returns NULL if INDEX is out of bounds. */
1031 file_entry *file_name_at (file_name_index index)
1032 {
1033 int vec_index;
1034 if (version >= 5)
1035 vec_index = index;
1036 else
1037 vec_index = index - 1;
1038 if (vec_index < 0 || vec_index >= m_file_names.size ())
1039 return NULL;
1040 return &m_file_names[vec_index];
1041 }
1042
1043 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1044 this method should only be used to iterate through all file entries in an
1045 index-agnostic manner. */
1046 std::vector<file_entry> &file_names ()
1047 { return m_file_names; }
1048
1049 /* Offset of line number information in .debug_line section. */
1050 sect_offset sect_off {};
1051
1052 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1053 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1054
1055 unsigned int total_length {};
1056 unsigned short version {};
1057 unsigned int header_length {};
1058 unsigned char minimum_instruction_length {};
1059 unsigned char maximum_ops_per_instruction {};
1060 unsigned char default_is_stmt {};
1061 int line_base {};
1062 unsigned char line_range {};
1063 unsigned char opcode_base {};
1064
1065 /* standard_opcode_lengths[i] is the number of operands for the
1066 standard opcode whose value is i. This means that
1067 standard_opcode_lengths[0] is unused, and the last meaningful
1068 element is standard_opcode_lengths[opcode_base - 1]. */
1069 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1070
1071 int file_names_size ()
1072 { return m_file_names.size(); }
1073
1074 /* The start and end of the statement program following this
1075 header. These point into dwarf2_per_objfile->line_buffer. */
1076 const gdb_byte *statement_program_start {}, *statement_program_end {};
1077
1078 private:
1079 /* The include_directories table. Note these are observing
1080 pointers. The memory is owned by debug_line_buffer. */
1081 std::vector<const char *> m_include_dirs;
1082
1083 /* The file_names table. This is private because the meaning of indexes
1084 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1085 before, and is 0 in DWARF 5 and later). So the client should use
1086 file_name_at method for access. */
1087 std::vector<file_entry> m_file_names;
1088 };
1089
1090 typedef std::unique_ptr<line_header> line_header_up;
1091
1092 const char *
1093 file_entry::include_dir (const line_header *lh) const
1094 {
1095 return lh->include_dir_at (d_index);
1096 }
1097
1098 /* When we construct a partial symbol table entry we only
1099 need this much information. */
1100 struct partial_die_info : public allocate_on_obstack
1101 {
1102 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1103
1104 /* Disable assign but still keep copy ctor, which is needed
1105 load_partial_dies. */
1106 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1107
1108 /* Adjust the partial die before generating a symbol for it. This
1109 function may set the is_external flag or change the DIE's
1110 name. */
1111 void fixup (struct dwarf2_cu *cu);
1112
1113 /* Read a minimal amount of information into the minimal die
1114 structure. */
1115 const gdb_byte *read (const struct die_reader_specs *reader,
1116 const struct abbrev_info &abbrev,
1117 const gdb_byte *info_ptr);
1118
1119 /* Offset of this DIE. */
1120 const sect_offset sect_off;
1121
1122 /* DWARF-2 tag for this DIE. */
1123 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1124
1125 /* Assorted flags describing the data found in this DIE. */
1126 const unsigned int has_children : 1;
1127
1128 unsigned int is_external : 1;
1129 unsigned int is_declaration : 1;
1130 unsigned int has_type : 1;
1131 unsigned int has_specification : 1;
1132 unsigned int has_pc_info : 1;
1133 unsigned int may_be_inlined : 1;
1134
1135 /* This DIE has been marked DW_AT_main_subprogram. */
1136 unsigned int main_subprogram : 1;
1137
1138 /* Flag set if the SCOPE field of this structure has been
1139 computed. */
1140 unsigned int scope_set : 1;
1141
1142 /* Flag set if the DIE has a byte_size attribute. */
1143 unsigned int has_byte_size : 1;
1144
1145 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1146 unsigned int has_const_value : 1;
1147
1148 /* Flag set if any of the DIE's children are template arguments. */
1149 unsigned int has_template_arguments : 1;
1150
1151 /* Flag set if fixup has been called on this die. */
1152 unsigned int fixup_called : 1;
1153
1154 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1155 unsigned int is_dwz : 1;
1156
1157 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1158 unsigned int spec_is_dwz : 1;
1159
1160 /* The name of this DIE. Normally the value of DW_AT_name, but
1161 sometimes a default name for unnamed DIEs. */
1162 const char *name = nullptr;
1163
1164 /* The linkage name, if present. */
1165 const char *linkage_name = nullptr;
1166
1167 /* The scope to prepend to our children. This is generally
1168 allocated on the comp_unit_obstack, so will disappear
1169 when this compilation unit leaves the cache. */
1170 const char *scope = nullptr;
1171
1172 /* Some data associated with the partial DIE. The tag determines
1173 which field is live. */
1174 union
1175 {
1176 /* The location description associated with this DIE, if any. */
1177 struct dwarf_block *locdesc;
1178 /* The offset of an import, for DW_TAG_imported_unit. */
1179 sect_offset sect_off;
1180 } d {};
1181
1182 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1183 CORE_ADDR lowpc = 0;
1184 CORE_ADDR highpc = 0;
1185
1186 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1187 DW_AT_sibling, if any. */
1188 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1189 could return DW_AT_sibling values to its caller load_partial_dies. */
1190 const gdb_byte *sibling = nullptr;
1191
1192 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1193 DW_AT_specification (or DW_AT_abstract_origin or
1194 DW_AT_extension). */
1195 sect_offset spec_offset {};
1196
1197 /* Pointers to this DIE's parent, first child, and next sibling,
1198 if any. */
1199 struct partial_die_info *die_parent = nullptr;
1200 struct partial_die_info *die_child = nullptr;
1201 struct partial_die_info *die_sibling = nullptr;
1202
1203 friend struct partial_die_info *
1204 dwarf2_cu::find_partial_die (sect_offset sect_off);
1205
1206 private:
1207 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1208 partial_die_info (sect_offset sect_off)
1209 : partial_die_info (sect_off, DW_TAG_padding, 0)
1210 {
1211 }
1212
1213 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1214 int has_children_)
1215 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1216 {
1217 is_external = 0;
1218 is_declaration = 0;
1219 has_type = 0;
1220 has_specification = 0;
1221 has_pc_info = 0;
1222 may_be_inlined = 0;
1223 main_subprogram = 0;
1224 scope_set = 0;
1225 has_byte_size = 0;
1226 has_const_value = 0;
1227 has_template_arguments = 0;
1228 fixup_called = 0;
1229 is_dwz = 0;
1230 spec_is_dwz = 0;
1231 }
1232 };
1233
1234 /* This data structure holds a complete die structure. */
1235 struct die_info
1236 {
1237 /* DWARF-2 tag for this DIE. */
1238 ENUM_BITFIELD(dwarf_tag) tag : 16;
1239
1240 /* Number of attributes */
1241 unsigned char num_attrs;
1242
1243 /* True if we're presently building the full type name for the
1244 type derived from this DIE. */
1245 unsigned char building_fullname : 1;
1246
1247 /* True if this die is in process. PR 16581. */
1248 unsigned char in_process : 1;
1249
1250 /* Abbrev number */
1251 unsigned int abbrev;
1252
1253 /* Offset in .debug_info or .debug_types section. */
1254 sect_offset sect_off;
1255
1256 /* The dies in a compilation unit form an n-ary tree. PARENT
1257 points to this die's parent; CHILD points to the first child of
1258 this node; and all the children of a given node are chained
1259 together via their SIBLING fields. */
1260 struct die_info *child; /* Its first child, if any. */
1261 struct die_info *sibling; /* Its next sibling, if any. */
1262 struct die_info *parent; /* Its parent, if any. */
1263
1264 /* An array of attributes, with NUM_ATTRS elements. There may be
1265 zero, but it's not common and zero-sized arrays are not
1266 sufficiently portable C. */
1267 struct attribute attrs[1];
1268 };
1269
1270 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1271 but this would require a corresponding change in unpack_field_as_long
1272 and friends. */
1273 static int bits_per_byte = 8;
1274
1275 /* When reading a variant or variant part, we track a bit more
1276 information about the field, and store it in an object of this
1277 type. */
1278
1279 struct variant_field
1280 {
1281 /* If we see a DW_TAG_variant, then this will be the discriminant
1282 value. */
1283 ULONGEST discriminant_value;
1284 /* If we see a DW_TAG_variant, then this will be set if this is the
1285 default branch. */
1286 bool default_branch;
1287 /* While reading a DW_TAG_variant_part, this will be set if this
1288 field is the discriminant. */
1289 bool is_discriminant;
1290 };
1291
1292 struct nextfield
1293 {
1294 int accessibility = 0;
1295 int virtuality = 0;
1296 /* Extra information to describe a variant or variant part. */
1297 struct variant_field variant {};
1298 struct field field {};
1299 };
1300
1301 struct fnfieldlist
1302 {
1303 const char *name = nullptr;
1304 std::vector<struct fn_field> fnfields;
1305 };
1306
1307 /* The routines that read and process dies for a C struct or C++ class
1308 pass lists of data member fields and lists of member function fields
1309 in an instance of a field_info structure, as defined below. */
1310 struct field_info
1311 {
1312 /* List of data member and baseclasses fields. */
1313 std::vector<struct nextfield> fields;
1314 std::vector<struct nextfield> baseclasses;
1315
1316 /* Number of fields (including baseclasses). */
1317 int nfields = 0;
1318
1319 /* Set if the accessibility of one of the fields is not public. */
1320 int non_public_fields = 0;
1321
1322 /* Member function fieldlist array, contains name of possibly overloaded
1323 member function, number of overloaded member functions and a pointer
1324 to the head of the member function field chain. */
1325 std::vector<struct fnfieldlist> fnfieldlists;
1326
1327 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1328 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1329 std::vector<struct decl_field> typedef_field_list;
1330
1331 /* Nested types defined by this class and the number of elements in this
1332 list. */
1333 std::vector<struct decl_field> nested_types_list;
1334 };
1335
1336 /* One item on the queue of compilation units to read in full symbols
1337 for. */
1338 struct dwarf2_queue_item
1339 {
1340 struct dwarf2_per_cu_data *per_cu;
1341 enum language pretend_language;
1342 struct dwarf2_queue_item *next;
1343 };
1344
1345 /* The current queue. */
1346 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1347
1348 /* Loaded secondary compilation units are kept in memory until they
1349 have not been referenced for the processing of this many
1350 compilation units. Set this to zero to disable caching. Cache
1351 sizes of up to at least twenty will improve startup time for
1352 typical inter-CU-reference binaries, at an obvious memory cost. */
1353 static int dwarf_max_cache_age = 5;
1354 static void
1355 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1356 struct cmd_list_element *c, const char *value)
1357 {
1358 fprintf_filtered (file, _("The upper bound on the age of cached "
1359 "DWARF compilation units is %s.\n"),
1360 value);
1361 }
1362 \f
1363 /* local function prototypes */
1364
1365 static void dwarf2_find_base_address (struct die_info *die,
1366 struct dwarf2_cu *cu);
1367
1368 static dwarf2_psymtab *create_partial_symtab
1369 (struct dwarf2_per_cu_data *per_cu, const char *name);
1370
1371 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1372 const gdb_byte *info_ptr,
1373 struct die_info *type_unit_die,
1374 int has_children);
1375
1376 static void dwarf2_build_psymtabs_hard
1377 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1378
1379 static void scan_partial_symbols (struct partial_die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 int, struct dwarf2_cu *);
1382
1383 static void add_partial_symbol (struct partial_die_info *,
1384 struct dwarf2_cu *);
1385
1386 static void add_partial_namespace (struct partial_die_info *pdi,
1387 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1388 int set_addrmap, struct dwarf2_cu *cu);
1389
1390 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1391 CORE_ADDR *highpc, int set_addrmap,
1392 struct dwarf2_cu *cu);
1393
1394 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1395 struct dwarf2_cu *cu);
1396
1397 static void add_partial_subprogram (struct partial_die_info *pdi,
1398 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1399 int need_pc, struct dwarf2_cu *cu);
1400
1401 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1402
1403 static struct partial_die_info *load_partial_dies
1404 (const struct die_reader_specs *, const gdb_byte *, int);
1405
1406 /* A pair of partial_die_info and compilation unit. */
1407 struct cu_partial_die_info
1408 {
1409 /* The compilation unit of the partial_die_info. */
1410 struct dwarf2_cu *cu;
1411 /* A partial_die_info. */
1412 struct partial_die_info *pdi;
1413
1414 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1415 : cu (cu),
1416 pdi (pdi)
1417 { /* Nothing. */ }
1418
1419 private:
1420 cu_partial_die_info () = delete;
1421 };
1422
1423 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1424 struct dwarf2_cu *);
1425
1426 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1427 struct attribute *, struct attr_abbrev *,
1428 const gdb_byte *, bool *need_reprocess);
1429
1430 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1431 struct attribute *attr);
1432
1433 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1434
1435 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1436 unsigned int *);
1437
1438 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1439
1440 static LONGEST read_checked_initial_length_and_offset
1441 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1442 unsigned int *, unsigned int *);
1443
1444 static LONGEST read_offset (bfd *, const gdb_byte *,
1445 const struct comp_unit_head *,
1446 unsigned int *);
1447
1448 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1449
1450 static sect_offset read_abbrev_offset
1451 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1452 struct dwarf2_section_info *, sect_offset);
1453
1454 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1455
1456 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1457
1458 static const char *read_indirect_string
1459 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1460 const struct comp_unit_head *, unsigned int *);
1461
1462 static const char *read_indirect_line_string
1463 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1464 const struct comp_unit_head *, unsigned int *);
1465
1466 static const char *read_indirect_string_at_offset
1467 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1468 LONGEST str_offset);
1469
1470 static const char *read_indirect_string_from_dwz
1471 (struct objfile *objfile, struct dwz_file *, LONGEST);
1472
1473 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1474 const gdb_byte *,
1475 unsigned int *);
1476
1477 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1478 ULONGEST str_index);
1479
1480 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1481 ULONGEST str_index);
1482
1483 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1484
1485 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1486 struct dwarf2_cu *);
1487
1488 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1489 unsigned int);
1490
1491 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1492 struct dwarf2_cu *cu);
1493
1494 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1495
1496 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1497 struct dwarf2_cu *cu);
1498
1499 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1500
1501 static struct die_info *die_specification (struct die_info *die,
1502 struct dwarf2_cu **);
1503
1504 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1505 struct dwarf2_cu *cu);
1506
1507 static void dwarf_decode_lines (struct line_header *, const char *,
1508 struct dwarf2_cu *, dwarf2_psymtab *,
1509 CORE_ADDR, int decode_mapping);
1510
1511 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1512 const char *);
1513
1514 static struct symbol *new_symbol (struct die_info *, struct type *,
1515 struct dwarf2_cu *, struct symbol * = NULL);
1516
1517 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1518 struct dwarf2_cu *);
1519
1520 static void dwarf2_const_value_attr (const struct attribute *attr,
1521 struct type *type,
1522 const char *name,
1523 struct obstack *obstack,
1524 struct dwarf2_cu *cu, LONGEST *value,
1525 const gdb_byte **bytes,
1526 struct dwarf2_locexpr_baton **baton);
1527
1528 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1529
1530 static int need_gnat_info (struct dwarf2_cu *);
1531
1532 static struct type *die_descriptive_type (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static void set_descriptive_type (struct type *, struct die_info *,
1536 struct dwarf2_cu *);
1537
1538 static struct type *die_containing_type (struct die_info *,
1539 struct dwarf2_cu *);
1540
1541 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1542 struct dwarf2_cu *);
1543
1544 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1545
1546 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1547
1548 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1549
1550 static char *typename_concat (struct obstack *obs, const char *prefix,
1551 const char *suffix, int physname,
1552 struct dwarf2_cu *cu);
1553
1554 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1555
1556 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1557
1558 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1559
1560 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1561
1562 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1563
1564 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1565
1566 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1567 struct dwarf2_cu *, dwarf2_psymtab *);
1568
1569 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1570 values. Keep the items ordered with increasing constraints compliance. */
1571 enum pc_bounds_kind
1572 {
1573 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1574 PC_BOUNDS_NOT_PRESENT,
1575
1576 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1577 were present but they do not form a valid range of PC addresses. */
1578 PC_BOUNDS_INVALID,
1579
1580 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1581 PC_BOUNDS_RANGES,
1582
1583 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1584 PC_BOUNDS_HIGH_LOW,
1585 };
1586
1587 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1588 CORE_ADDR *, CORE_ADDR *,
1589 struct dwarf2_cu *,
1590 dwarf2_psymtab *);
1591
1592 static void get_scope_pc_bounds (struct die_info *,
1593 CORE_ADDR *, CORE_ADDR *,
1594 struct dwarf2_cu *);
1595
1596 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1597 CORE_ADDR, struct dwarf2_cu *);
1598
1599 static void dwarf2_add_field (struct field_info *, struct die_info *,
1600 struct dwarf2_cu *);
1601
1602 static void dwarf2_attach_fields_to_type (struct field_info *,
1603 struct type *, struct dwarf2_cu *);
1604
1605 static void dwarf2_add_member_fn (struct field_info *,
1606 struct die_info *, struct type *,
1607 struct dwarf2_cu *);
1608
1609 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1610 struct type *,
1611 struct dwarf2_cu *);
1612
1613 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1614
1615 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1616
1617 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1618
1619 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1620
1621 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1622
1623 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1624
1625 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1626
1627 static struct type *read_module_type (struct die_info *die,
1628 struct dwarf2_cu *cu);
1629
1630 static const char *namespace_name (struct die_info *die,
1631 int *is_anonymous, struct dwarf2_cu *);
1632
1633 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1634
1635 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1636
1637 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1638 struct dwarf2_cu *);
1639
1640 static struct die_info *read_die_and_siblings_1
1641 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1642 struct die_info *);
1643
1644 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1645 const gdb_byte *info_ptr,
1646 const gdb_byte **new_info_ptr,
1647 struct die_info *parent);
1648
1649 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1650 struct die_info **, const gdb_byte *,
1651 int *, int);
1652
1653 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1654 struct die_info **, const gdb_byte *,
1655 int *);
1656
1657 static void process_die (struct die_info *, struct dwarf2_cu *);
1658
1659 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1660 struct obstack *);
1661
1662 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1663
1664 static const char *dwarf2_full_name (const char *name,
1665 struct die_info *die,
1666 struct dwarf2_cu *cu);
1667
1668 static const char *dwarf2_physname (const char *name, struct die_info *die,
1669 struct dwarf2_cu *cu);
1670
1671 static struct die_info *dwarf2_extension (struct die_info *die,
1672 struct dwarf2_cu **);
1673
1674 static const char *dwarf_tag_name (unsigned int);
1675
1676 static const char *dwarf_attr_name (unsigned int);
1677
1678 static const char *dwarf_unit_type_name (int unit_type);
1679
1680 static const char *dwarf_form_name (unsigned int);
1681
1682 static const char *dwarf_bool_name (unsigned int);
1683
1684 static const char *dwarf_type_encoding_name (unsigned int);
1685
1686 static struct die_info *sibling_die (struct die_info *);
1687
1688 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1689
1690 static void dump_die_for_error (struct die_info *);
1691
1692 static void dump_die_1 (struct ui_file *, int level, int max_level,
1693 struct die_info *);
1694
1695 /*static*/ void dump_die (struct die_info *, int max_level);
1696
1697 static void store_in_ref_table (struct die_info *,
1698 struct dwarf2_cu *);
1699
1700 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1701
1702 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1703
1704 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1705 const struct attribute *,
1706 struct dwarf2_cu **);
1707
1708 static struct die_info *follow_die_ref (struct die_info *,
1709 const struct attribute *,
1710 struct dwarf2_cu **);
1711
1712 static struct die_info *follow_die_sig (struct die_info *,
1713 const struct attribute *,
1714 struct dwarf2_cu **);
1715
1716 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1717 struct dwarf2_cu *);
1718
1719 static struct type *get_DW_AT_signature_type (struct die_info *,
1720 const struct attribute *,
1721 struct dwarf2_cu *);
1722
1723 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1724
1725 static void read_signatured_type (struct signatured_type *);
1726
1727 static int attr_to_dynamic_prop (const struct attribute *attr,
1728 struct die_info *die, struct dwarf2_cu *cu,
1729 struct dynamic_prop *prop, struct type *type);
1730
1731 /* memory allocation interface */
1732
1733 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1734
1735 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1736
1737 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1738
1739 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1740 struct dwarf2_loclist_baton *baton,
1741 const struct attribute *attr);
1742
1743 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1744 struct symbol *sym,
1745 struct dwarf2_cu *cu,
1746 int is_block);
1747
1748 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1749 const gdb_byte *info_ptr,
1750 struct abbrev_info *abbrev);
1751
1752 static hashval_t partial_die_hash (const void *item);
1753
1754 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1755
1756 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1757 (sect_offset sect_off, unsigned int offset_in_dwz,
1758 struct dwarf2_per_objfile *dwarf2_per_objfile);
1759
1760 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1761 struct die_info *comp_unit_die,
1762 enum language pretend_language);
1763
1764 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1765
1766 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1767
1768 static struct type *set_die_type (struct die_info *, struct type *,
1769 struct dwarf2_cu *);
1770
1771 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1772
1773 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1774
1775 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1776 enum language);
1777
1778 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1779 enum language);
1780
1781 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1782 enum language);
1783
1784 static void dwarf2_add_dependence (struct dwarf2_cu *,
1785 struct dwarf2_per_cu_data *);
1786
1787 static void dwarf2_mark (struct dwarf2_cu *);
1788
1789 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1790
1791 static struct type *get_die_type_at_offset (sect_offset,
1792 struct dwarf2_per_cu_data *);
1793
1794 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1795
1796 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1797 enum language pretend_language);
1798
1799 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1800
1801 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1802 static struct type *dwarf2_per_cu_addr_sized_int_type
1803 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1804 static struct type *dwarf2_per_cu_int_type
1805 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1806 bool unsigned_p);
1807
1808 /* Class, the destructor of which frees all allocated queue entries. This
1809 will only have work to do if an error was thrown while processing the
1810 dwarf. If no error was thrown then the queue entries should have all
1811 been processed, and freed, as we went along. */
1812
1813 class dwarf2_queue_guard
1814 {
1815 public:
1816 dwarf2_queue_guard () = default;
1817
1818 /* Free any entries remaining on the queue. There should only be
1819 entries left if we hit an error while processing the dwarf. */
1820 ~dwarf2_queue_guard ()
1821 {
1822 struct dwarf2_queue_item *item, *last;
1823
1824 item = dwarf2_queue;
1825 while (item)
1826 {
1827 /* Anything still marked queued is likely to be in an
1828 inconsistent state, so discard it. */
1829 if (item->per_cu->queued)
1830 {
1831 if (item->per_cu->cu != NULL)
1832 free_one_cached_comp_unit (item->per_cu);
1833 item->per_cu->queued = 0;
1834 }
1835
1836 last = item;
1837 item = item->next;
1838 xfree (last);
1839 }
1840
1841 dwarf2_queue = dwarf2_queue_tail = NULL;
1842 }
1843 };
1844
1845 /* The return type of find_file_and_directory. Note, the enclosed
1846 string pointers are only valid while this object is valid. */
1847
1848 struct file_and_directory
1849 {
1850 /* The filename. This is never NULL. */
1851 const char *name;
1852
1853 /* The compilation directory. NULL if not known. If we needed to
1854 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1855 points directly to the DW_AT_comp_dir string attribute owned by
1856 the obstack that owns the DIE. */
1857 const char *comp_dir;
1858
1859 /* If we needed to build a new string for comp_dir, this is what
1860 owns the storage. */
1861 std::string comp_dir_storage;
1862 };
1863
1864 static file_and_directory find_file_and_directory (struct die_info *die,
1865 struct dwarf2_cu *cu);
1866
1867 static char *file_full_name (int file, struct line_header *lh,
1868 const char *comp_dir);
1869
1870 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1871 enum class rcuh_kind { COMPILE, TYPE };
1872
1873 static const gdb_byte *read_and_check_comp_unit_head
1874 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1875 struct comp_unit_head *header,
1876 struct dwarf2_section_info *section,
1877 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1878 rcuh_kind section_kind);
1879
1880 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1881
1882 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1883
1884 static struct dwo_unit *lookup_dwo_unit_in_dwp
1885 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1886 struct dwp_file *dwp_file, const char *comp_dir,
1887 ULONGEST signature, int is_debug_types);
1888
1889 static struct dwp_file *get_dwp_file
1890 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1891
1892 static struct dwo_unit *lookup_dwo_comp_unit
1893 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1894
1895 static struct dwo_unit *lookup_dwo_type_unit
1896 (struct signatured_type *, const char *, const char *);
1897
1898 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1899
1900 /* A unique pointer to a dwo_file. */
1901
1902 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1903
1904 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1905
1906 static void check_producer (struct dwarf2_cu *cu);
1907
1908 static void free_line_header_voidp (void *arg);
1909 \f
1910 /* Various complaints about symbol reading that don't abort the process. */
1911
1912 static void
1913 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1914 {
1915 complaint (_("statement list doesn't fit in .debug_line section"));
1916 }
1917
1918 static void
1919 dwarf2_debug_line_missing_file_complaint (void)
1920 {
1921 complaint (_(".debug_line section has line data without a file"));
1922 }
1923
1924 static void
1925 dwarf2_debug_line_missing_end_sequence_complaint (void)
1926 {
1927 complaint (_(".debug_line section has line "
1928 "program sequence without an end"));
1929 }
1930
1931 static void
1932 dwarf2_complex_location_expr_complaint (void)
1933 {
1934 complaint (_("location expression too complex"));
1935 }
1936
1937 static void
1938 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1939 int arg3)
1940 {
1941 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1942 arg1, arg2, arg3);
1943 }
1944
1945 static void
1946 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1947 {
1948 complaint (_("debug info runs off end of %s section"
1949 " [in module %s]"),
1950 section->get_name (),
1951 section->get_file_name ());
1952 }
1953
1954 static void
1955 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1956 {
1957 complaint (_("macro debug info contains a "
1958 "malformed macro definition:\n`%s'"),
1959 arg1);
1960 }
1961
1962 static void
1963 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1964 {
1965 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1966 arg1, arg2);
1967 }
1968
1969 /* Hash function for line_header_hash. */
1970
1971 static hashval_t
1972 line_header_hash (const struct line_header *ofs)
1973 {
1974 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1975 }
1976
1977 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1978
1979 static hashval_t
1980 line_header_hash_voidp (const void *item)
1981 {
1982 const struct line_header *ofs = (const struct line_header *) item;
1983
1984 return line_header_hash (ofs);
1985 }
1986
1987 /* Equality function for line_header_hash. */
1988
1989 static int
1990 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1991 {
1992 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1993 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1994
1995 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1996 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1997 }
1998
1999 \f
2000
2001 /* See declaration. */
2002
2003 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2004 const dwarf2_debug_sections *names,
2005 bool can_copy_)
2006 : objfile (objfile_),
2007 can_copy (can_copy_)
2008 {
2009 if (names == NULL)
2010 names = &dwarf2_elf_names;
2011
2012 bfd *obfd = objfile->obfd;
2013
2014 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2015 locate_sections (obfd, sec, *names);
2016 }
2017
2018 dwarf2_per_objfile::~dwarf2_per_objfile ()
2019 {
2020 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2021 free_cached_comp_units ();
2022
2023 if (quick_file_names_table)
2024 htab_delete (quick_file_names_table);
2025
2026 if (line_header_hash)
2027 htab_delete (line_header_hash);
2028
2029 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2030 per_cu->imported_symtabs_free ();
2031
2032 for (signatured_type *sig_type : all_type_units)
2033 sig_type->per_cu.imported_symtabs_free ();
2034
2035 /* Everything else should be on the objfile obstack. */
2036 }
2037
2038 /* See declaration. */
2039
2040 void
2041 dwarf2_per_objfile::free_cached_comp_units ()
2042 {
2043 dwarf2_per_cu_data *per_cu = read_in_chain;
2044 dwarf2_per_cu_data **last_chain = &read_in_chain;
2045 while (per_cu != NULL)
2046 {
2047 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2048
2049 delete per_cu->cu;
2050 *last_chain = next_cu;
2051 per_cu = next_cu;
2052 }
2053 }
2054
2055 /* A helper class that calls free_cached_comp_units on
2056 destruction. */
2057
2058 class free_cached_comp_units
2059 {
2060 public:
2061
2062 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2063 : m_per_objfile (per_objfile)
2064 {
2065 }
2066
2067 ~free_cached_comp_units ()
2068 {
2069 m_per_objfile->free_cached_comp_units ();
2070 }
2071
2072 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2073
2074 private:
2075
2076 dwarf2_per_objfile *m_per_objfile;
2077 };
2078
2079 /* Try to locate the sections we need for DWARF 2 debugging
2080 information and return true if we have enough to do something.
2081 NAMES points to the dwarf2 section names, or is NULL if the standard
2082 ELF names are used. CAN_COPY is true for formats where symbol
2083 interposition is possible and so symbol values must follow copy
2084 relocation rules. */
2085
2086 int
2087 dwarf2_has_info (struct objfile *objfile,
2088 const struct dwarf2_debug_sections *names,
2089 bool can_copy)
2090 {
2091 if (objfile->flags & OBJF_READNEVER)
2092 return 0;
2093
2094 struct dwarf2_per_objfile *dwarf2_per_objfile
2095 = get_dwarf2_per_objfile (objfile);
2096
2097 if (dwarf2_per_objfile == NULL)
2098 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2099 names,
2100 can_copy);
2101
2102 return (!dwarf2_per_objfile->info.is_virtual
2103 && dwarf2_per_objfile->info.s.section != NULL
2104 && !dwarf2_per_objfile->abbrev.is_virtual
2105 && dwarf2_per_objfile->abbrev.s.section != NULL);
2106 }
2107
2108 /* When loading sections, we look either for uncompressed section or for
2109 compressed section names. */
2110
2111 static int
2112 section_is_p (const char *section_name,
2113 const struct dwarf2_section_names *names)
2114 {
2115 if (names->normal != NULL
2116 && strcmp (section_name, names->normal) == 0)
2117 return 1;
2118 if (names->compressed != NULL
2119 && strcmp (section_name, names->compressed) == 0)
2120 return 1;
2121 return 0;
2122 }
2123
2124 /* See declaration. */
2125
2126 void
2127 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2128 const dwarf2_debug_sections &names)
2129 {
2130 flagword aflag = bfd_section_flags (sectp);
2131
2132 if ((aflag & SEC_HAS_CONTENTS) == 0)
2133 {
2134 }
2135 else if (elf_section_data (sectp)->this_hdr.sh_size
2136 > bfd_get_file_size (abfd))
2137 {
2138 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2139 warning (_("Discarding section %s which has a section size (%s"
2140 ") larger than the file size [in module %s]"),
2141 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2142 bfd_get_filename (abfd));
2143 }
2144 else if (section_is_p (sectp->name, &names.info))
2145 {
2146 this->info.s.section = sectp;
2147 this->info.size = bfd_section_size (sectp);
2148 }
2149 else if (section_is_p (sectp->name, &names.abbrev))
2150 {
2151 this->abbrev.s.section = sectp;
2152 this->abbrev.size = bfd_section_size (sectp);
2153 }
2154 else if (section_is_p (sectp->name, &names.line))
2155 {
2156 this->line.s.section = sectp;
2157 this->line.size = bfd_section_size (sectp);
2158 }
2159 else if (section_is_p (sectp->name, &names.loc))
2160 {
2161 this->loc.s.section = sectp;
2162 this->loc.size = bfd_section_size (sectp);
2163 }
2164 else if (section_is_p (sectp->name, &names.loclists))
2165 {
2166 this->loclists.s.section = sectp;
2167 this->loclists.size = bfd_section_size (sectp);
2168 }
2169 else if (section_is_p (sectp->name, &names.macinfo))
2170 {
2171 this->macinfo.s.section = sectp;
2172 this->macinfo.size = bfd_section_size (sectp);
2173 }
2174 else if (section_is_p (sectp->name, &names.macro))
2175 {
2176 this->macro.s.section = sectp;
2177 this->macro.size = bfd_section_size (sectp);
2178 }
2179 else if (section_is_p (sectp->name, &names.str))
2180 {
2181 this->str.s.section = sectp;
2182 this->str.size = bfd_section_size (sectp);
2183 }
2184 else if (section_is_p (sectp->name, &names.str_offsets))
2185 {
2186 this->str_offsets.s.section = sectp;
2187 this->str_offsets.size = bfd_section_size (sectp);
2188 }
2189 else if (section_is_p (sectp->name, &names.line_str))
2190 {
2191 this->line_str.s.section = sectp;
2192 this->line_str.size = bfd_section_size (sectp);
2193 }
2194 else if (section_is_p (sectp->name, &names.addr))
2195 {
2196 this->addr.s.section = sectp;
2197 this->addr.size = bfd_section_size (sectp);
2198 }
2199 else if (section_is_p (sectp->name, &names.frame))
2200 {
2201 this->frame.s.section = sectp;
2202 this->frame.size = bfd_section_size (sectp);
2203 }
2204 else if (section_is_p (sectp->name, &names.eh_frame))
2205 {
2206 this->eh_frame.s.section = sectp;
2207 this->eh_frame.size = bfd_section_size (sectp);
2208 }
2209 else if (section_is_p (sectp->name, &names.ranges))
2210 {
2211 this->ranges.s.section = sectp;
2212 this->ranges.size = bfd_section_size (sectp);
2213 }
2214 else if (section_is_p (sectp->name, &names.rnglists))
2215 {
2216 this->rnglists.s.section = sectp;
2217 this->rnglists.size = bfd_section_size (sectp);
2218 }
2219 else if (section_is_p (sectp->name, &names.types))
2220 {
2221 struct dwarf2_section_info type_section;
2222
2223 memset (&type_section, 0, sizeof (type_section));
2224 type_section.s.section = sectp;
2225 type_section.size = bfd_section_size (sectp);
2226
2227 this->types.push_back (type_section);
2228 }
2229 else if (section_is_p (sectp->name, &names.gdb_index))
2230 {
2231 this->gdb_index.s.section = sectp;
2232 this->gdb_index.size = bfd_section_size (sectp);
2233 }
2234 else if (section_is_p (sectp->name, &names.debug_names))
2235 {
2236 this->debug_names.s.section = sectp;
2237 this->debug_names.size = bfd_section_size (sectp);
2238 }
2239 else if (section_is_p (sectp->name, &names.debug_aranges))
2240 {
2241 this->debug_aranges.s.section = sectp;
2242 this->debug_aranges.size = bfd_section_size (sectp);
2243 }
2244
2245 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2246 && bfd_section_vma (sectp) == 0)
2247 this->has_section_at_zero = true;
2248 }
2249
2250 /* A helper function that returns the size of a section in a safe way.
2251 If you are positive that the section has been read before using the
2252 size, then it is safe to refer to the dwarf2_section_info object's
2253 "size" field directly. In other cases, you must call this
2254 function, because for compressed sections the size field is not set
2255 correctly until the section has been read. */
2256
2257 static bfd_size_type
2258 dwarf2_section_size (struct objfile *objfile,
2259 struct dwarf2_section_info *info)
2260 {
2261 if (!info->readin)
2262 info->read (objfile);
2263 return info->size;
2264 }
2265
2266 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2267 SECTION_NAME. */
2268
2269 void
2270 dwarf2_get_section_info (struct objfile *objfile,
2271 enum dwarf2_section_enum sect,
2272 asection **sectp, const gdb_byte **bufp,
2273 bfd_size_type *sizep)
2274 {
2275 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2276 struct dwarf2_section_info *info;
2277
2278 /* We may see an objfile without any DWARF, in which case we just
2279 return nothing. */
2280 if (data == NULL)
2281 {
2282 *sectp = NULL;
2283 *bufp = NULL;
2284 *sizep = 0;
2285 return;
2286 }
2287 switch (sect)
2288 {
2289 case DWARF2_DEBUG_FRAME:
2290 info = &data->frame;
2291 break;
2292 case DWARF2_EH_FRAME:
2293 info = &data->eh_frame;
2294 break;
2295 default:
2296 gdb_assert_not_reached ("unexpected section");
2297 }
2298
2299 info->read (objfile);
2300
2301 *sectp = info->get_bfd_section ();
2302 *bufp = info->buffer;
2303 *sizep = info->size;
2304 }
2305
2306 /* A helper function to find the sections for a .dwz file. */
2307
2308 static void
2309 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2310 {
2311 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2312
2313 /* Note that we only support the standard ELF names, because .dwz
2314 is ELF-only (at the time of writing). */
2315 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2316 {
2317 dwz_file->abbrev.s.section = sectp;
2318 dwz_file->abbrev.size = bfd_section_size (sectp);
2319 }
2320 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2321 {
2322 dwz_file->info.s.section = sectp;
2323 dwz_file->info.size = bfd_section_size (sectp);
2324 }
2325 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2326 {
2327 dwz_file->str.s.section = sectp;
2328 dwz_file->str.size = bfd_section_size (sectp);
2329 }
2330 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2331 {
2332 dwz_file->line.s.section = sectp;
2333 dwz_file->line.size = bfd_section_size (sectp);
2334 }
2335 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2336 {
2337 dwz_file->macro.s.section = sectp;
2338 dwz_file->macro.size = bfd_section_size (sectp);
2339 }
2340 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2341 {
2342 dwz_file->gdb_index.s.section = sectp;
2343 dwz_file->gdb_index.size = bfd_section_size (sectp);
2344 }
2345 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2346 {
2347 dwz_file->debug_names.s.section = sectp;
2348 dwz_file->debug_names.size = bfd_section_size (sectp);
2349 }
2350 }
2351
2352 /* See dwarf2read.h. */
2353
2354 struct dwz_file *
2355 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2356 {
2357 const char *filename;
2358 bfd_size_type buildid_len_arg;
2359 size_t buildid_len;
2360 bfd_byte *buildid;
2361
2362 if (dwarf2_per_objfile->dwz_file != NULL)
2363 return dwarf2_per_objfile->dwz_file.get ();
2364
2365 bfd_set_error (bfd_error_no_error);
2366 gdb::unique_xmalloc_ptr<char> data
2367 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2368 &buildid_len_arg, &buildid));
2369 if (data == NULL)
2370 {
2371 if (bfd_get_error () == bfd_error_no_error)
2372 return NULL;
2373 error (_("could not read '.gnu_debugaltlink' section: %s"),
2374 bfd_errmsg (bfd_get_error ()));
2375 }
2376
2377 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2378
2379 buildid_len = (size_t) buildid_len_arg;
2380
2381 filename = data.get ();
2382
2383 std::string abs_storage;
2384 if (!IS_ABSOLUTE_PATH (filename))
2385 {
2386 gdb::unique_xmalloc_ptr<char> abs
2387 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2388
2389 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2390 filename = abs_storage.c_str ();
2391 }
2392
2393 /* First try the file name given in the section. If that doesn't
2394 work, try to use the build-id instead. */
2395 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2396 if (dwz_bfd != NULL)
2397 {
2398 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2399 dwz_bfd.reset (nullptr);
2400 }
2401
2402 if (dwz_bfd == NULL)
2403 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2404
2405 if (dwz_bfd == NULL)
2406 error (_("could not find '.gnu_debugaltlink' file for %s"),
2407 objfile_name (dwarf2_per_objfile->objfile));
2408
2409 std::unique_ptr<struct dwz_file> result
2410 (new struct dwz_file (std::move (dwz_bfd)));
2411
2412 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2413 result.get ());
2414
2415 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2416 result->dwz_bfd.get ());
2417 dwarf2_per_objfile->dwz_file = std::move (result);
2418 return dwarf2_per_objfile->dwz_file.get ();
2419 }
2420 \f
2421 /* DWARF quick_symbols_functions support. */
2422
2423 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2424 unique line tables, so we maintain a separate table of all .debug_line
2425 derived entries to support the sharing.
2426 All the quick functions need is the list of file names. We discard the
2427 line_header when we're done and don't need to record it here. */
2428 struct quick_file_names
2429 {
2430 /* The data used to construct the hash key. */
2431 struct stmt_list_hash hash;
2432
2433 /* The number of entries in file_names, real_names. */
2434 unsigned int num_file_names;
2435
2436 /* The file names from the line table, after being run through
2437 file_full_name. */
2438 const char **file_names;
2439
2440 /* The file names from the line table after being run through
2441 gdb_realpath. These are computed lazily. */
2442 const char **real_names;
2443 };
2444
2445 /* When using the index (and thus not using psymtabs), each CU has an
2446 object of this type. This is used to hold information needed by
2447 the various "quick" methods. */
2448 struct dwarf2_per_cu_quick_data
2449 {
2450 /* The file table. This can be NULL if there was no file table
2451 or it's currently not read in.
2452 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2453 struct quick_file_names *file_names;
2454
2455 /* The corresponding symbol table. This is NULL if symbols for this
2456 CU have not yet been read. */
2457 struct compunit_symtab *compunit_symtab;
2458
2459 /* A temporary mark bit used when iterating over all CUs in
2460 expand_symtabs_matching. */
2461 unsigned int mark : 1;
2462
2463 /* True if we've tried to read the file table and found there isn't one.
2464 There will be no point in trying to read it again next time. */
2465 unsigned int no_file_data : 1;
2466 };
2467
2468 /* Utility hash function for a stmt_list_hash. */
2469
2470 static hashval_t
2471 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2472 {
2473 hashval_t v = 0;
2474
2475 if (stmt_list_hash->dwo_unit != NULL)
2476 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2477 v += to_underlying (stmt_list_hash->line_sect_off);
2478 return v;
2479 }
2480
2481 /* Utility equality function for a stmt_list_hash. */
2482
2483 static int
2484 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2485 const struct stmt_list_hash *rhs)
2486 {
2487 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2488 return 0;
2489 if (lhs->dwo_unit != NULL
2490 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2491 return 0;
2492
2493 return lhs->line_sect_off == rhs->line_sect_off;
2494 }
2495
2496 /* Hash function for a quick_file_names. */
2497
2498 static hashval_t
2499 hash_file_name_entry (const void *e)
2500 {
2501 const struct quick_file_names *file_data
2502 = (const struct quick_file_names *) e;
2503
2504 return hash_stmt_list_entry (&file_data->hash);
2505 }
2506
2507 /* Equality function for a quick_file_names. */
2508
2509 static int
2510 eq_file_name_entry (const void *a, const void *b)
2511 {
2512 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2513 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2514
2515 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2516 }
2517
2518 /* Delete function for a quick_file_names. */
2519
2520 static void
2521 delete_file_name_entry (void *e)
2522 {
2523 struct quick_file_names *file_data = (struct quick_file_names *) e;
2524 int i;
2525
2526 for (i = 0; i < file_data->num_file_names; ++i)
2527 {
2528 xfree ((void*) file_data->file_names[i]);
2529 if (file_data->real_names)
2530 xfree ((void*) file_data->real_names[i]);
2531 }
2532
2533 /* The space for the struct itself lives on objfile_obstack,
2534 so we don't free it here. */
2535 }
2536
2537 /* Create a quick_file_names hash table. */
2538
2539 static htab_t
2540 create_quick_file_names_table (unsigned int nr_initial_entries)
2541 {
2542 return htab_create_alloc (nr_initial_entries,
2543 hash_file_name_entry, eq_file_name_entry,
2544 delete_file_name_entry, xcalloc, xfree);
2545 }
2546
2547 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2548 have to be created afterwards. You should call age_cached_comp_units after
2549 processing PER_CU->CU. dw2_setup must have been already called. */
2550
2551 static void
2552 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2553 {
2554 if (per_cu->is_debug_types)
2555 load_full_type_unit (per_cu);
2556 else
2557 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2558
2559 if (per_cu->cu == NULL)
2560 return; /* Dummy CU. */
2561
2562 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2563 }
2564
2565 /* Read in the symbols for PER_CU. */
2566
2567 static void
2568 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2569 {
2570 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2571
2572 /* Skip type_unit_groups, reading the type units they contain
2573 is handled elsewhere. */
2574 if (IS_TYPE_UNIT_GROUP (per_cu))
2575 return;
2576
2577 /* The destructor of dwarf2_queue_guard frees any entries left on
2578 the queue. After this point we're guaranteed to leave this function
2579 with the dwarf queue empty. */
2580 dwarf2_queue_guard q_guard;
2581
2582 if (dwarf2_per_objfile->using_index
2583 ? per_cu->v.quick->compunit_symtab == NULL
2584 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2585 {
2586 queue_comp_unit (per_cu, language_minimal);
2587 load_cu (per_cu, skip_partial);
2588
2589 /* If we just loaded a CU from a DWO, and we're working with an index
2590 that may badly handle TUs, load all the TUs in that DWO as well.
2591 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2592 if (!per_cu->is_debug_types
2593 && per_cu->cu != NULL
2594 && per_cu->cu->dwo_unit != NULL
2595 && dwarf2_per_objfile->index_table != NULL
2596 && dwarf2_per_objfile->index_table->version <= 7
2597 /* DWP files aren't supported yet. */
2598 && get_dwp_file (dwarf2_per_objfile) == NULL)
2599 queue_and_load_all_dwo_tus (per_cu);
2600 }
2601
2602 process_queue (dwarf2_per_objfile);
2603
2604 /* Age the cache, releasing compilation units that have not
2605 been used recently. */
2606 age_cached_comp_units (dwarf2_per_objfile);
2607 }
2608
2609 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2610 the objfile from which this CU came. Returns the resulting symbol
2611 table. */
2612
2613 static struct compunit_symtab *
2614 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2615 {
2616 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2617
2618 gdb_assert (dwarf2_per_objfile->using_index);
2619 if (!per_cu->v.quick->compunit_symtab)
2620 {
2621 free_cached_comp_units freer (dwarf2_per_objfile);
2622 scoped_restore decrementer = increment_reading_symtab ();
2623 dw2_do_instantiate_symtab (per_cu, skip_partial);
2624 process_cu_includes (dwarf2_per_objfile);
2625 }
2626
2627 return per_cu->v.quick->compunit_symtab;
2628 }
2629
2630 /* See declaration. */
2631
2632 dwarf2_per_cu_data *
2633 dwarf2_per_objfile::get_cutu (int index)
2634 {
2635 if (index >= this->all_comp_units.size ())
2636 {
2637 index -= this->all_comp_units.size ();
2638 gdb_assert (index < this->all_type_units.size ());
2639 return &this->all_type_units[index]->per_cu;
2640 }
2641
2642 return this->all_comp_units[index];
2643 }
2644
2645 /* See declaration. */
2646
2647 dwarf2_per_cu_data *
2648 dwarf2_per_objfile::get_cu (int index)
2649 {
2650 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2651
2652 return this->all_comp_units[index];
2653 }
2654
2655 /* See declaration. */
2656
2657 signatured_type *
2658 dwarf2_per_objfile::get_tu (int index)
2659 {
2660 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2661
2662 return this->all_type_units[index];
2663 }
2664
2665 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2666 objfile_obstack, and constructed with the specified field
2667 values. */
2668
2669 static dwarf2_per_cu_data *
2670 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2671 struct dwarf2_section_info *section,
2672 int is_dwz,
2673 sect_offset sect_off, ULONGEST length)
2674 {
2675 struct objfile *objfile = dwarf2_per_objfile->objfile;
2676 dwarf2_per_cu_data *the_cu
2677 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2678 struct dwarf2_per_cu_data);
2679 the_cu->sect_off = sect_off;
2680 the_cu->length = length;
2681 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2682 the_cu->section = section;
2683 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2684 struct dwarf2_per_cu_quick_data);
2685 the_cu->is_dwz = is_dwz;
2686 return the_cu;
2687 }
2688
2689 /* A helper for create_cus_from_index that handles a given list of
2690 CUs. */
2691
2692 static void
2693 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2694 const gdb_byte *cu_list, offset_type n_elements,
2695 struct dwarf2_section_info *section,
2696 int is_dwz)
2697 {
2698 for (offset_type i = 0; i < n_elements; i += 2)
2699 {
2700 gdb_static_assert (sizeof (ULONGEST) >= 8);
2701
2702 sect_offset sect_off
2703 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2704 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2705 cu_list += 2 * 8;
2706
2707 dwarf2_per_cu_data *per_cu
2708 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2709 sect_off, length);
2710 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2711 }
2712 }
2713
2714 /* Read the CU list from the mapped index, and use it to create all
2715 the CU objects for this objfile. */
2716
2717 static void
2718 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2719 const gdb_byte *cu_list, offset_type cu_list_elements,
2720 const gdb_byte *dwz_list, offset_type dwz_elements)
2721 {
2722 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2723 dwarf2_per_objfile->all_comp_units.reserve
2724 ((cu_list_elements + dwz_elements) / 2);
2725
2726 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2727 &dwarf2_per_objfile->info, 0);
2728
2729 if (dwz_elements == 0)
2730 return;
2731
2732 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2733 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2734 &dwz->info, 1);
2735 }
2736
2737 /* Create the signatured type hash table from the index. */
2738
2739 static void
2740 create_signatured_type_table_from_index
2741 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2742 struct dwarf2_section_info *section,
2743 const gdb_byte *bytes,
2744 offset_type elements)
2745 {
2746 struct objfile *objfile = dwarf2_per_objfile->objfile;
2747
2748 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2749 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2750
2751 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2752
2753 for (offset_type i = 0; i < elements; i += 3)
2754 {
2755 struct signatured_type *sig_type;
2756 ULONGEST signature;
2757 void **slot;
2758 cu_offset type_offset_in_tu;
2759
2760 gdb_static_assert (sizeof (ULONGEST) >= 8);
2761 sect_offset sect_off
2762 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2763 type_offset_in_tu
2764 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2765 BFD_ENDIAN_LITTLE);
2766 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2767 bytes += 3 * 8;
2768
2769 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2770 struct signatured_type);
2771 sig_type->signature = signature;
2772 sig_type->type_offset_in_tu = type_offset_in_tu;
2773 sig_type->per_cu.is_debug_types = 1;
2774 sig_type->per_cu.section = section;
2775 sig_type->per_cu.sect_off = sect_off;
2776 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2777 sig_type->per_cu.v.quick
2778 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2779 struct dwarf2_per_cu_quick_data);
2780
2781 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2782 *slot = sig_type;
2783
2784 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2785 }
2786
2787 dwarf2_per_objfile->signatured_types = sig_types_hash;
2788 }
2789
2790 /* Create the signatured type hash table from .debug_names. */
2791
2792 static void
2793 create_signatured_type_table_from_debug_names
2794 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2795 const mapped_debug_names &map,
2796 struct dwarf2_section_info *section,
2797 struct dwarf2_section_info *abbrev_section)
2798 {
2799 struct objfile *objfile = dwarf2_per_objfile->objfile;
2800
2801 section->read (objfile);
2802 abbrev_section->read (objfile);
2803
2804 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2805 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2806
2807 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2808
2809 for (uint32_t i = 0; i < map.tu_count; ++i)
2810 {
2811 struct signatured_type *sig_type;
2812 void **slot;
2813
2814 sect_offset sect_off
2815 = (sect_offset) (extract_unsigned_integer
2816 (map.tu_table_reordered + i * map.offset_size,
2817 map.offset_size,
2818 map.dwarf5_byte_order));
2819
2820 comp_unit_head cu_header;
2821 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2822 abbrev_section,
2823 section->buffer + to_underlying (sect_off),
2824 rcuh_kind::TYPE);
2825
2826 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2827 struct signatured_type);
2828 sig_type->signature = cu_header.signature;
2829 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2830 sig_type->per_cu.is_debug_types = 1;
2831 sig_type->per_cu.section = section;
2832 sig_type->per_cu.sect_off = sect_off;
2833 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2834 sig_type->per_cu.v.quick
2835 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2836 struct dwarf2_per_cu_quick_data);
2837
2838 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2839 *slot = sig_type;
2840
2841 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2842 }
2843
2844 dwarf2_per_objfile->signatured_types = sig_types_hash;
2845 }
2846
2847 /* Read the address map data from the mapped index, and use it to
2848 populate the objfile's psymtabs_addrmap. */
2849
2850 static void
2851 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2852 struct mapped_index *index)
2853 {
2854 struct objfile *objfile = dwarf2_per_objfile->objfile;
2855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2856 const gdb_byte *iter, *end;
2857 struct addrmap *mutable_map;
2858 CORE_ADDR baseaddr;
2859
2860 auto_obstack temp_obstack;
2861
2862 mutable_map = addrmap_create_mutable (&temp_obstack);
2863
2864 iter = index->address_table.data ();
2865 end = iter + index->address_table.size ();
2866
2867 baseaddr = objfile->text_section_offset ();
2868
2869 while (iter < end)
2870 {
2871 ULONGEST hi, lo, cu_index;
2872 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2873 iter += 8;
2874 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2875 iter += 8;
2876 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2877 iter += 4;
2878
2879 if (lo > hi)
2880 {
2881 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2882 hex_string (lo), hex_string (hi));
2883 continue;
2884 }
2885
2886 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2887 {
2888 complaint (_(".gdb_index address table has invalid CU number %u"),
2889 (unsigned) cu_index);
2890 continue;
2891 }
2892
2893 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2894 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2895 addrmap_set_empty (mutable_map, lo, hi - 1,
2896 dwarf2_per_objfile->get_cu (cu_index));
2897 }
2898
2899 objfile->partial_symtabs->psymtabs_addrmap
2900 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2901 }
2902
2903 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2904 populate the objfile's psymtabs_addrmap. */
2905
2906 static void
2907 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2908 struct dwarf2_section_info *section)
2909 {
2910 struct objfile *objfile = dwarf2_per_objfile->objfile;
2911 bfd *abfd = objfile->obfd;
2912 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2913 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2914
2915 auto_obstack temp_obstack;
2916 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2917
2918 std::unordered_map<sect_offset,
2919 dwarf2_per_cu_data *,
2920 gdb::hash_enum<sect_offset>>
2921 debug_info_offset_to_per_cu;
2922 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2923 {
2924 const auto insertpair
2925 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2926 if (!insertpair.second)
2927 {
2928 warning (_("Section .debug_aranges in %s has duplicate "
2929 "debug_info_offset %s, ignoring .debug_aranges."),
2930 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2931 return;
2932 }
2933 }
2934
2935 section->read (objfile);
2936
2937 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2938
2939 const gdb_byte *addr = section->buffer;
2940
2941 while (addr < section->buffer + section->size)
2942 {
2943 const gdb_byte *const entry_addr = addr;
2944 unsigned int bytes_read;
2945
2946 const LONGEST entry_length = read_initial_length (abfd, addr,
2947 &bytes_read);
2948 addr += bytes_read;
2949
2950 const gdb_byte *const entry_end = addr + entry_length;
2951 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2952 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2953 if (addr + entry_length > section->buffer + section->size)
2954 {
2955 warning (_("Section .debug_aranges in %s entry at offset %s "
2956 "length %s exceeds section length %s, "
2957 "ignoring .debug_aranges."),
2958 objfile_name (objfile),
2959 plongest (entry_addr - section->buffer),
2960 plongest (bytes_read + entry_length),
2961 pulongest (section->size));
2962 return;
2963 }
2964
2965 /* The version number. */
2966 const uint16_t version = read_2_bytes (abfd, addr);
2967 addr += 2;
2968 if (version != 2)
2969 {
2970 warning (_("Section .debug_aranges in %s entry at offset %s "
2971 "has unsupported version %d, ignoring .debug_aranges."),
2972 objfile_name (objfile),
2973 plongest (entry_addr - section->buffer), version);
2974 return;
2975 }
2976
2977 const uint64_t debug_info_offset
2978 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2979 addr += offset_size;
2980 const auto per_cu_it
2981 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2982 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2983 {
2984 warning (_("Section .debug_aranges in %s entry at offset %s "
2985 "debug_info_offset %s does not exists, "
2986 "ignoring .debug_aranges."),
2987 objfile_name (objfile),
2988 plongest (entry_addr - section->buffer),
2989 pulongest (debug_info_offset));
2990 return;
2991 }
2992 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2993
2994 const uint8_t address_size = *addr++;
2995 if (address_size < 1 || address_size > 8)
2996 {
2997 warning (_("Section .debug_aranges in %s entry at offset %s "
2998 "address_size %u is invalid, ignoring .debug_aranges."),
2999 objfile_name (objfile),
3000 plongest (entry_addr - section->buffer), address_size);
3001 return;
3002 }
3003
3004 const uint8_t segment_selector_size = *addr++;
3005 if (segment_selector_size != 0)
3006 {
3007 warning (_("Section .debug_aranges in %s entry at offset %s "
3008 "segment_selector_size %u is not supported, "
3009 "ignoring .debug_aranges."),
3010 objfile_name (objfile),
3011 plongest (entry_addr - section->buffer),
3012 segment_selector_size);
3013 return;
3014 }
3015
3016 /* Must pad to an alignment boundary that is twice the address
3017 size. It is undocumented by the DWARF standard but GCC does
3018 use it. */
3019 for (size_t padding = ((-(addr - section->buffer))
3020 & (2 * address_size - 1));
3021 padding > 0; padding--)
3022 if (*addr++ != 0)
3023 {
3024 warning (_("Section .debug_aranges in %s entry at offset %s "
3025 "padding is not zero, ignoring .debug_aranges."),
3026 objfile_name (objfile),
3027 plongest (entry_addr - section->buffer));
3028 return;
3029 }
3030
3031 for (;;)
3032 {
3033 if (addr + 2 * address_size > entry_end)
3034 {
3035 warning (_("Section .debug_aranges in %s entry at offset %s "
3036 "address list is not properly terminated, "
3037 "ignoring .debug_aranges."),
3038 objfile_name (objfile),
3039 plongest (entry_addr - section->buffer));
3040 return;
3041 }
3042 ULONGEST start = extract_unsigned_integer (addr, address_size,
3043 dwarf5_byte_order);
3044 addr += address_size;
3045 ULONGEST length = extract_unsigned_integer (addr, address_size,
3046 dwarf5_byte_order);
3047 addr += address_size;
3048 if (start == 0 && length == 0)
3049 break;
3050 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3051 {
3052 /* Symbol was eliminated due to a COMDAT group. */
3053 continue;
3054 }
3055 ULONGEST end = start + length;
3056 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3057 - baseaddr);
3058 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3059 - baseaddr);
3060 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3061 }
3062 }
3063
3064 objfile->partial_symtabs->psymtabs_addrmap
3065 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3066 }
3067
3068 /* Find a slot in the mapped index INDEX for the object named NAME.
3069 If NAME is found, set *VEC_OUT to point to the CU vector in the
3070 constant pool and return true. If NAME cannot be found, return
3071 false. */
3072
3073 static bool
3074 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3075 offset_type **vec_out)
3076 {
3077 offset_type hash;
3078 offset_type slot, step;
3079 int (*cmp) (const char *, const char *);
3080
3081 gdb::unique_xmalloc_ptr<char> without_params;
3082 if (current_language->la_language == language_cplus
3083 || current_language->la_language == language_fortran
3084 || current_language->la_language == language_d)
3085 {
3086 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3087 not contain any. */
3088
3089 if (strchr (name, '(') != NULL)
3090 {
3091 without_params = cp_remove_params (name);
3092
3093 if (without_params != NULL)
3094 name = without_params.get ();
3095 }
3096 }
3097
3098 /* Index version 4 did not support case insensitive searches. But the
3099 indices for case insensitive languages are built in lowercase, therefore
3100 simulate our NAME being searched is also lowercased. */
3101 hash = mapped_index_string_hash ((index->version == 4
3102 && case_sensitivity == case_sensitive_off
3103 ? 5 : index->version),
3104 name);
3105
3106 slot = hash & (index->symbol_table.size () - 1);
3107 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3108 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3109
3110 for (;;)
3111 {
3112 const char *str;
3113
3114 const auto &bucket = index->symbol_table[slot];
3115 if (bucket.name == 0 && bucket.vec == 0)
3116 return false;
3117
3118 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3119 if (!cmp (name, str))
3120 {
3121 *vec_out = (offset_type *) (index->constant_pool
3122 + MAYBE_SWAP (bucket.vec));
3123 return true;
3124 }
3125
3126 slot = (slot + step) & (index->symbol_table.size () - 1);
3127 }
3128 }
3129
3130 /* A helper function that reads the .gdb_index from BUFFER and fills
3131 in MAP. FILENAME is the name of the file containing the data;
3132 it is used for error reporting. DEPRECATED_OK is true if it is
3133 ok to use deprecated sections.
3134
3135 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3136 out parameters that are filled in with information about the CU and
3137 TU lists in the section.
3138
3139 Returns true if all went well, false otherwise. */
3140
3141 static bool
3142 read_gdb_index_from_buffer (struct objfile *objfile,
3143 const char *filename,
3144 bool deprecated_ok,
3145 gdb::array_view<const gdb_byte> buffer,
3146 struct mapped_index *map,
3147 const gdb_byte **cu_list,
3148 offset_type *cu_list_elements,
3149 const gdb_byte **types_list,
3150 offset_type *types_list_elements)
3151 {
3152 const gdb_byte *addr = &buffer[0];
3153
3154 /* Version check. */
3155 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3156 /* Versions earlier than 3 emitted every copy of a psymbol. This
3157 causes the index to behave very poorly for certain requests. Version 3
3158 contained incomplete addrmap. So, it seems better to just ignore such
3159 indices. */
3160 if (version < 4)
3161 {
3162 static int warning_printed = 0;
3163 if (!warning_printed)
3164 {
3165 warning (_("Skipping obsolete .gdb_index section in %s."),
3166 filename);
3167 warning_printed = 1;
3168 }
3169 return 0;
3170 }
3171 /* Index version 4 uses a different hash function than index version
3172 5 and later.
3173
3174 Versions earlier than 6 did not emit psymbols for inlined
3175 functions. Using these files will cause GDB not to be able to
3176 set breakpoints on inlined functions by name, so we ignore these
3177 indices unless the user has done
3178 "set use-deprecated-index-sections on". */
3179 if (version < 6 && !deprecated_ok)
3180 {
3181 static int warning_printed = 0;
3182 if (!warning_printed)
3183 {
3184 warning (_("\
3185 Skipping deprecated .gdb_index section in %s.\n\
3186 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3187 to use the section anyway."),
3188 filename);
3189 warning_printed = 1;
3190 }
3191 return 0;
3192 }
3193 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3194 of the TU (for symbols coming from TUs),
3195 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3196 Plus gold-generated indices can have duplicate entries for global symbols,
3197 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3198 These are just performance bugs, and we can't distinguish gdb-generated
3199 indices from gold-generated ones, so issue no warning here. */
3200
3201 /* Indexes with higher version than the one supported by GDB may be no
3202 longer backward compatible. */
3203 if (version > 8)
3204 return 0;
3205
3206 map->version = version;
3207
3208 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3209
3210 int i = 0;
3211 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3212 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3213 / 8);
3214 ++i;
3215
3216 *types_list = addr + MAYBE_SWAP (metadata[i]);
3217 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3218 - MAYBE_SWAP (metadata[i]))
3219 / 8);
3220 ++i;
3221
3222 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3223 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3224 map->address_table
3225 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3226 ++i;
3227
3228 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3229 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3230 map->symbol_table
3231 = gdb::array_view<mapped_index::symbol_table_slot>
3232 ((mapped_index::symbol_table_slot *) symbol_table,
3233 (mapped_index::symbol_table_slot *) symbol_table_end);
3234
3235 ++i;
3236 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3237
3238 return 1;
3239 }
3240
3241 /* Callback types for dwarf2_read_gdb_index. */
3242
3243 typedef gdb::function_view
3244 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3245 get_gdb_index_contents_ftype;
3246 typedef gdb::function_view
3247 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3248 get_gdb_index_contents_dwz_ftype;
3249
3250 /* Read .gdb_index. If everything went ok, initialize the "quick"
3251 elements of all the CUs and return 1. Otherwise, return 0. */
3252
3253 static int
3254 dwarf2_read_gdb_index
3255 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3256 get_gdb_index_contents_ftype get_gdb_index_contents,
3257 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3258 {
3259 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3260 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3261 struct dwz_file *dwz;
3262 struct objfile *objfile = dwarf2_per_objfile->objfile;
3263
3264 gdb::array_view<const gdb_byte> main_index_contents
3265 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3266
3267 if (main_index_contents.empty ())
3268 return 0;
3269
3270 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3271 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3272 use_deprecated_index_sections,
3273 main_index_contents, map.get (), &cu_list,
3274 &cu_list_elements, &types_list,
3275 &types_list_elements))
3276 return 0;
3277
3278 /* Don't use the index if it's empty. */
3279 if (map->symbol_table.empty ())
3280 return 0;
3281
3282 /* If there is a .dwz file, read it so we can get its CU list as
3283 well. */
3284 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3285 if (dwz != NULL)
3286 {
3287 struct mapped_index dwz_map;
3288 const gdb_byte *dwz_types_ignore;
3289 offset_type dwz_types_elements_ignore;
3290
3291 gdb::array_view<const gdb_byte> dwz_index_content
3292 = get_gdb_index_contents_dwz (objfile, dwz);
3293
3294 if (dwz_index_content.empty ())
3295 return 0;
3296
3297 if (!read_gdb_index_from_buffer (objfile,
3298 bfd_get_filename (dwz->dwz_bfd.get ()),
3299 1, dwz_index_content, &dwz_map,
3300 &dwz_list, &dwz_list_elements,
3301 &dwz_types_ignore,
3302 &dwz_types_elements_ignore))
3303 {
3304 warning (_("could not read '.gdb_index' section from %s; skipping"),
3305 bfd_get_filename (dwz->dwz_bfd.get ()));
3306 return 0;
3307 }
3308 }
3309
3310 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3311 dwz_list, dwz_list_elements);
3312
3313 if (types_list_elements)
3314 {
3315 /* We can only handle a single .debug_types when we have an
3316 index. */
3317 if (dwarf2_per_objfile->types.size () != 1)
3318 return 0;
3319
3320 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3321
3322 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3323 types_list, types_list_elements);
3324 }
3325
3326 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3327
3328 dwarf2_per_objfile->index_table = std::move (map);
3329 dwarf2_per_objfile->using_index = 1;
3330 dwarf2_per_objfile->quick_file_names_table =
3331 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3332
3333 return 1;
3334 }
3335
3336 /* die_reader_func for dw2_get_file_names. */
3337
3338 static void
3339 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3340 const gdb_byte *info_ptr,
3341 struct die_info *comp_unit_die,
3342 int has_children)
3343 {
3344 struct dwarf2_cu *cu = reader->cu;
3345 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3346 struct dwarf2_per_objfile *dwarf2_per_objfile
3347 = cu->per_cu->dwarf2_per_objfile;
3348 struct objfile *objfile = dwarf2_per_objfile->objfile;
3349 struct dwarf2_per_cu_data *lh_cu;
3350 struct attribute *attr;
3351 void **slot;
3352 struct quick_file_names *qfn;
3353
3354 gdb_assert (! this_cu->is_debug_types);
3355
3356 /* Our callers never want to match partial units -- instead they
3357 will match the enclosing full CU. */
3358 if (comp_unit_die->tag == DW_TAG_partial_unit)
3359 {
3360 this_cu->v.quick->no_file_data = 1;
3361 return;
3362 }
3363
3364 lh_cu = this_cu;
3365 slot = NULL;
3366
3367 line_header_up lh;
3368 sect_offset line_offset {};
3369
3370 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3371 if (attr != nullptr)
3372 {
3373 struct quick_file_names find_entry;
3374
3375 line_offset = (sect_offset) DW_UNSND (attr);
3376
3377 /* We may have already read in this line header (TU line header sharing).
3378 If we have we're done. */
3379 find_entry.hash.dwo_unit = cu->dwo_unit;
3380 find_entry.hash.line_sect_off = line_offset;
3381 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3382 &find_entry, INSERT);
3383 if (*slot != NULL)
3384 {
3385 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3386 return;
3387 }
3388
3389 lh = dwarf_decode_line_header (line_offset, cu);
3390 }
3391 if (lh == NULL)
3392 {
3393 lh_cu->v.quick->no_file_data = 1;
3394 return;
3395 }
3396
3397 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3398 qfn->hash.dwo_unit = cu->dwo_unit;
3399 qfn->hash.line_sect_off = line_offset;
3400 gdb_assert (slot != NULL);
3401 *slot = qfn;
3402
3403 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3404
3405 int offset = 0;
3406 if (strcmp (fnd.name, "<unknown>") != 0)
3407 ++offset;
3408
3409 qfn->num_file_names = offset + lh->file_names_size ();
3410 qfn->file_names =
3411 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3412 if (offset != 0)
3413 qfn->file_names[0] = xstrdup (fnd.name);
3414 for (int i = 0; i < lh->file_names_size (); ++i)
3415 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3416 qfn->real_names = NULL;
3417
3418 lh_cu->v.quick->file_names = qfn;
3419 }
3420
3421 /* A helper for the "quick" functions which attempts to read the line
3422 table for THIS_CU. */
3423
3424 static struct quick_file_names *
3425 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3426 {
3427 /* This should never be called for TUs. */
3428 gdb_assert (! this_cu->is_debug_types);
3429 /* Nor type unit groups. */
3430 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3431
3432 if (this_cu->v.quick->file_names != NULL)
3433 return this_cu->v.quick->file_names;
3434 /* If we know there is no line data, no point in looking again. */
3435 if (this_cu->v.quick->no_file_data)
3436 return NULL;
3437
3438 cutu_reader reader (this_cu);
3439 if (!reader.dummy_p)
3440 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3441 reader.has_children);
3442
3443 if (this_cu->v.quick->no_file_data)
3444 return NULL;
3445 return this_cu->v.quick->file_names;
3446 }
3447
3448 /* A helper for the "quick" functions which computes and caches the
3449 real path for a given file name from the line table. */
3450
3451 static const char *
3452 dw2_get_real_path (struct objfile *objfile,
3453 struct quick_file_names *qfn, int index)
3454 {
3455 if (qfn->real_names == NULL)
3456 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3457 qfn->num_file_names, const char *);
3458
3459 if (qfn->real_names[index] == NULL)
3460 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3461
3462 return qfn->real_names[index];
3463 }
3464
3465 static struct symtab *
3466 dw2_find_last_source_symtab (struct objfile *objfile)
3467 {
3468 struct dwarf2_per_objfile *dwarf2_per_objfile
3469 = get_dwarf2_per_objfile (objfile);
3470 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3471 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3472
3473 if (cust == NULL)
3474 return NULL;
3475
3476 return compunit_primary_filetab (cust);
3477 }
3478
3479 /* Traversal function for dw2_forget_cached_source_info. */
3480
3481 static int
3482 dw2_free_cached_file_names (void **slot, void *info)
3483 {
3484 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3485
3486 if (file_data->real_names)
3487 {
3488 int i;
3489
3490 for (i = 0; i < file_data->num_file_names; ++i)
3491 {
3492 xfree ((void*) file_data->real_names[i]);
3493 file_data->real_names[i] = NULL;
3494 }
3495 }
3496
3497 return 1;
3498 }
3499
3500 static void
3501 dw2_forget_cached_source_info (struct objfile *objfile)
3502 {
3503 struct dwarf2_per_objfile *dwarf2_per_objfile
3504 = get_dwarf2_per_objfile (objfile);
3505
3506 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3507 dw2_free_cached_file_names, NULL);
3508 }
3509
3510 /* Helper function for dw2_map_symtabs_matching_filename that expands
3511 the symtabs and calls the iterator. */
3512
3513 static int
3514 dw2_map_expand_apply (struct objfile *objfile,
3515 struct dwarf2_per_cu_data *per_cu,
3516 const char *name, const char *real_path,
3517 gdb::function_view<bool (symtab *)> callback)
3518 {
3519 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3520
3521 /* Don't visit already-expanded CUs. */
3522 if (per_cu->v.quick->compunit_symtab)
3523 return 0;
3524
3525 /* This may expand more than one symtab, and we want to iterate over
3526 all of them. */
3527 dw2_instantiate_symtab (per_cu, false);
3528
3529 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3530 last_made, callback);
3531 }
3532
3533 /* Implementation of the map_symtabs_matching_filename method. */
3534
3535 static bool
3536 dw2_map_symtabs_matching_filename
3537 (struct objfile *objfile, const char *name, const char *real_path,
3538 gdb::function_view<bool (symtab *)> callback)
3539 {
3540 const char *name_basename = lbasename (name);
3541 struct dwarf2_per_objfile *dwarf2_per_objfile
3542 = get_dwarf2_per_objfile (objfile);
3543
3544 /* The rule is CUs specify all the files, including those used by
3545 any TU, so there's no need to scan TUs here. */
3546
3547 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3548 {
3549 /* We only need to look at symtabs not already expanded. */
3550 if (per_cu->v.quick->compunit_symtab)
3551 continue;
3552
3553 quick_file_names *file_data = dw2_get_file_names (per_cu);
3554 if (file_data == NULL)
3555 continue;
3556
3557 for (int j = 0; j < file_data->num_file_names; ++j)
3558 {
3559 const char *this_name = file_data->file_names[j];
3560 const char *this_real_name;
3561
3562 if (compare_filenames_for_search (this_name, name))
3563 {
3564 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3565 callback))
3566 return true;
3567 continue;
3568 }
3569
3570 /* Before we invoke realpath, which can get expensive when many
3571 files are involved, do a quick comparison of the basenames. */
3572 if (! basenames_may_differ
3573 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3574 continue;
3575
3576 this_real_name = dw2_get_real_path (objfile, file_data, j);
3577 if (compare_filenames_for_search (this_real_name, name))
3578 {
3579 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3580 callback))
3581 return true;
3582 continue;
3583 }
3584
3585 if (real_path != NULL)
3586 {
3587 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3588 gdb_assert (IS_ABSOLUTE_PATH (name));
3589 if (this_real_name != NULL
3590 && FILENAME_CMP (real_path, this_real_name) == 0)
3591 {
3592 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3593 callback))
3594 return true;
3595 continue;
3596 }
3597 }
3598 }
3599 }
3600
3601 return false;
3602 }
3603
3604 /* Struct used to manage iterating over all CUs looking for a symbol. */
3605
3606 struct dw2_symtab_iterator
3607 {
3608 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3609 struct dwarf2_per_objfile *dwarf2_per_objfile;
3610 /* If set, only look for symbols that match that block. Valid values are
3611 GLOBAL_BLOCK and STATIC_BLOCK. */
3612 gdb::optional<block_enum> block_index;
3613 /* The kind of symbol we're looking for. */
3614 domain_enum domain;
3615 /* The list of CUs from the index entry of the symbol,
3616 or NULL if not found. */
3617 offset_type *vec;
3618 /* The next element in VEC to look at. */
3619 int next;
3620 /* The number of elements in VEC, or zero if there is no match. */
3621 int length;
3622 /* Have we seen a global version of the symbol?
3623 If so we can ignore all further global instances.
3624 This is to work around gold/15646, inefficient gold-generated
3625 indices. */
3626 int global_seen;
3627 };
3628
3629 /* Initialize the index symtab iterator ITER. */
3630
3631 static void
3632 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3633 struct dwarf2_per_objfile *dwarf2_per_objfile,
3634 gdb::optional<block_enum> block_index,
3635 domain_enum domain,
3636 const char *name)
3637 {
3638 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3639 iter->block_index = block_index;
3640 iter->domain = domain;
3641 iter->next = 0;
3642 iter->global_seen = 0;
3643
3644 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3645
3646 /* index is NULL if OBJF_READNOW. */
3647 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3648 iter->length = MAYBE_SWAP (*iter->vec);
3649 else
3650 {
3651 iter->vec = NULL;
3652 iter->length = 0;
3653 }
3654 }
3655
3656 /* Return the next matching CU or NULL if there are no more. */
3657
3658 static struct dwarf2_per_cu_data *
3659 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3660 {
3661 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3662
3663 for ( ; iter->next < iter->length; ++iter->next)
3664 {
3665 offset_type cu_index_and_attrs =
3666 MAYBE_SWAP (iter->vec[iter->next + 1]);
3667 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3668 gdb_index_symbol_kind symbol_kind =
3669 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3670 /* Only check the symbol attributes if they're present.
3671 Indices prior to version 7 don't record them,
3672 and indices >= 7 may elide them for certain symbols
3673 (gold does this). */
3674 int attrs_valid =
3675 (dwarf2_per_objfile->index_table->version >= 7
3676 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3677
3678 /* Don't crash on bad data. */
3679 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3680 + dwarf2_per_objfile->all_type_units.size ()))
3681 {
3682 complaint (_(".gdb_index entry has bad CU index"
3683 " [in module %s]"),
3684 objfile_name (dwarf2_per_objfile->objfile));
3685 continue;
3686 }
3687
3688 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3689
3690 /* Skip if already read in. */
3691 if (per_cu->v.quick->compunit_symtab)
3692 continue;
3693
3694 /* Check static vs global. */
3695 if (attrs_valid)
3696 {
3697 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3698
3699 if (iter->block_index.has_value ())
3700 {
3701 bool want_static = *iter->block_index == STATIC_BLOCK;
3702
3703 if (is_static != want_static)
3704 continue;
3705 }
3706
3707 /* Work around gold/15646. */
3708 if (!is_static && iter->global_seen)
3709 continue;
3710 if (!is_static)
3711 iter->global_seen = 1;
3712 }
3713
3714 /* Only check the symbol's kind if it has one. */
3715 if (attrs_valid)
3716 {
3717 switch (iter->domain)
3718 {
3719 case VAR_DOMAIN:
3720 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3721 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3722 /* Some types are also in VAR_DOMAIN. */
3723 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3724 continue;
3725 break;
3726 case STRUCT_DOMAIN:
3727 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3728 continue;
3729 break;
3730 case LABEL_DOMAIN:
3731 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3732 continue;
3733 break;
3734 case MODULE_DOMAIN:
3735 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3736 continue;
3737 break;
3738 default:
3739 break;
3740 }
3741 }
3742
3743 ++iter->next;
3744 return per_cu;
3745 }
3746
3747 return NULL;
3748 }
3749
3750 static struct compunit_symtab *
3751 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3752 const char *name, domain_enum domain)
3753 {
3754 struct compunit_symtab *stab_best = NULL;
3755 struct dwarf2_per_objfile *dwarf2_per_objfile
3756 = get_dwarf2_per_objfile (objfile);
3757
3758 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3759
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3764
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3766 {
3767 struct symbol *sym, *with_opaque = NULL;
3768 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3769 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3770 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3771
3772 sym = block_find_symbol (block, name, domain,
3773 block_find_non_opaque_type_preferred,
3774 &with_opaque);
3775
3776 /* Some caution must be observed with overloaded functions
3777 and methods, since the index will not contain any overload
3778 information (but NAME might contain it). */
3779
3780 if (sym != NULL
3781 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3782 return stab;
3783 if (with_opaque != NULL
3784 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3785 stab_best = stab;
3786
3787 /* Keep looking through other CUs. */
3788 }
3789
3790 return stab_best;
3791 }
3792
3793 static void
3794 dw2_print_stats (struct objfile *objfile)
3795 {
3796 struct dwarf2_per_objfile *dwarf2_per_objfile
3797 = get_dwarf2_per_objfile (objfile);
3798 int total = (dwarf2_per_objfile->all_comp_units.size ()
3799 + dwarf2_per_objfile->all_type_units.size ());
3800 int count = 0;
3801
3802 for (int i = 0; i < total; ++i)
3803 {
3804 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3805
3806 if (!per_cu->v.quick->compunit_symtab)
3807 ++count;
3808 }
3809 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3810 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3811 }
3812
3813 /* This dumps minimal information about the index.
3814 It is called via "mt print objfiles".
3815 One use is to verify .gdb_index has been loaded by the
3816 gdb.dwarf2/gdb-index.exp testcase. */
3817
3818 static void
3819 dw2_dump (struct objfile *objfile)
3820 {
3821 struct dwarf2_per_objfile *dwarf2_per_objfile
3822 = get_dwarf2_per_objfile (objfile);
3823
3824 gdb_assert (dwarf2_per_objfile->using_index);
3825 printf_filtered (".gdb_index:");
3826 if (dwarf2_per_objfile->index_table != NULL)
3827 {
3828 printf_filtered (" version %d\n",
3829 dwarf2_per_objfile->index_table->version);
3830 }
3831 else
3832 printf_filtered (" faked for \"readnow\"\n");
3833 printf_filtered ("\n");
3834 }
3835
3836 static void
3837 dw2_expand_symtabs_for_function (struct objfile *objfile,
3838 const char *func_name)
3839 {
3840 struct dwarf2_per_objfile *dwarf2_per_objfile
3841 = get_dwarf2_per_objfile (objfile);
3842
3843 struct dw2_symtab_iterator iter;
3844 struct dwarf2_per_cu_data *per_cu;
3845
3846 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3847
3848 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3849 dw2_instantiate_symtab (per_cu, false);
3850
3851 }
3852
3853 static void
3854 dw2_expand_all_symtabs (struct objfile *objfile)
3855 {
3856 struct dwarf2_per_objfile *dwarf2_per_objfile
3857 = get_dwarf2_per_objfile (objfile);
3858 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3859 + dwarf2_per_objfile->all_type_units.size ());
3860
3861 for (int i = 0; i < total_units; ++i)
3862 {
3863 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3864
3865 /* We don't want to directly expand a partial CU, because if we
3866 read it with the wrong language, then assertion failures can
3867 be triggered later on. See PR symtab/23010. So, tell
3868 dw2_instantiate_symtab to skip partial CUs -- any important
3869 partial CU will be read via DW_TAG_imported_unit anyway. */
3870 dw2_instantiate_symtab (per_cu, true);
3871 }
3872 }
3873
3874 static void
3875 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3876 const char *fullname)
3877 {
3878 struct dwarf2_per_objfile *dwarf2_per_objfile
3879 = get_dwarf2_per_objfile (objfile);
3880
3881 /* We don't need to consider type units here.
3882 This is only called for examining code, e.g. expand_line_sal.
3883 There can be an order of magnitude (or more) more type units
3884 than comp units, and we avoid them if we can. */
3885
3886 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3887 {
3888 /* We only need to look at symtabs not already expanded. */
3889 if (per_cu->v.quick->compunit_symtab)
3890 continue;
3891
3892 quick_file_names *file_data = dw2_get_file_names (per_cu);
3893 if (file_data == NULL)
3894 continue;
3895
3896 for (int j = 0; j < file_data->num_file_names; ++j)
3897 {
3898 const char *this_fullname = file_data->file_names[j];
3899
3900 if (filename_cmp (this_fullname, fullname) == 0)
3901 {
3902 dw2_instantiate_symtab (per_cu, false);
3903 break;
3904 }
3905 }
3906 }
3907 }
3908
3909 static void
3910 dw2_map_matching_symbols
3911 (struct objfile *objfile,
3912 const lookup_name_info &name, domain_enum domain,
3913 int global,
3914 gdb::function_view<symbol_found_callback_ftype> callback,
3915 symbol_compare_ftype *ordered_compare)
3916 {
3917 /* Currently unimplemented; used for Ada. The function can be called if the
3918 current language is Ada for a non-Ada objfile using GNU index. As Ada
3919 does not look for non-Ada symbols this function should just return. */
3920 }
3921
3922 /* Starting from a search name, return the string that finds the upper
3923 bound of all strings that start with SEARCH_NAME in a sorted name
3924 list. Returns the empty string to indicate that the upper bound is
3925 the end of the list. */
3926
3927 static std::string
3928 make_sort_after_prefix_name (const char *search_name)
3929 {
3930 /* When looking to complete "func", we find the upper bound of all
3931 symbols that start with "func" by looking for where we'd insert
3932 the closest string that would follow "func" in lexicographical
3933 order. Usually, that's "func"-with-last-character-incremented,
3934 i.e. "fund". Mind non-ASCII characters, though. Usually those
3935 will be UTF-8 multi-byte sequences, but we can't be certain.
3936 Especially mind the 0xff character, which is a valid character in
3937 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3938 rule out compilers allowing it in identifiers. Note that
3939 conveniently, strcmp/strcasecmp are specified to compare
3940 characters interpreted as unsigned char. So what we do is treat
3941 the whole string as a base 256 number composed of a sequence of
3942 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3943 to 0, and carries 1 to the following more-significant position.
3944 If the very first character in SEARCH_NAME ends up incremented
3945 and carries/overflows, then the upper bound is the end of the
3946 list. The string after the empty string is also the empty
3947 string.
3948
3949 Some examples of this operation:
3950
3951 SEARCH_NAME => "+1" RESULT
3952
3953 "abc" => "abd"
3954 "ab\xff" => "ac"
3955 "\xff" "a" "\xff" => "\xff" "b"
3956 "\xff" => ""
3957 "\xff\xff" => ""
3958 "" => ""
3959
3960 Then, with these symbols for example:
3961
3962 func
3963 func1
3964 fund
3965
3966 completing "func" looks for symbols between "func" and
3967 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3968 which finds "func" and "func1", but not "fund".
3969
3970 And with:
3971
3972 funcÿ (Latin1 'ÿ' [0xff])
3973 funcÿ1
3974 fund
3975
3976 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3977 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3978
3979 And with:
3980
3981 ÿÿ (Latin1 'ÿ' [0xff])
3982 ÿÿ1
3983
3984 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3985 the end of the list.
3986 */
3987 std::string after = search_name;
3988 while (!after.empty () && (unsigned char) after.back () == 0xff)
3989 after.pop_back ();
3990 if (!after.empty ())
3991 after.back () = (unsigned char) after.back () + 1;
3992 return after;
3993 }
3994
3995 /* See declaration. */
3996
3997 std::pair<std::vector<name_component>::const_iterator,
3998 std::vector<name_component>::const_iterator>
3999 mapped_index_base::find_name_components_bounds
4000 (const lookup_name_info &lookup_name_without_params, language lang) const
4001 {
4002 auto *name_cmp
4003 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4004
4005 const char *lang_name
4006 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4007
4008 /* Comparison function object for lower_bound that matches against a
4009 given symbol name. */
4010 auto lookup_compare_lower = [&] (const name_component &elem,
4011 const char *name)
4012 {
4013 const char *elem_qualified = this->symbol_name_at (elem.idx);
4014 const char *elem_name = elem_qualified + elem.name_offset;
4015 return name_cmp (elem_name, name) < 0;
4016 };
4017
4018 /* Comparison function object for upper_bound that matches against a
4019 given symbol name. */
4020 auto lookup_compare_upper = [&] (const char *name,
4021 const name_component &elem)
4022 {
4023 const char *elem_qualified = this->symbol_name_at (elem.idx);
4024 const char *elem_name = elem_qualified + elem.name_offset;
4025 return name_cmp (name, elem_name) < 0;
4026 };
4027
4028 auto begin = this->name_components.begin ();
4029 auto end = this->name_components.end ();
4030
4031 /* Find the lower bound. */
4032 auto lower = [&] ()
4033 {
4034 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4035 return begin;
4036 else
4037 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4038 } ();
4039
4040 /* Find the upper bound. */
4041 auto upper = [&] ()
4042 {
4043 if (lookup_name_without_params.completion_mode ())
4044 {
4045 /* In completion mode, we want UPPER to point past all
4046 symbols names that have the same prefix. I.e., with
4047 these symbols, and completing "func":
4048
4049 function << lower bound
4050 function1
4051 other_function << upper bound
4052
4053 We find the upper bound by looking for the insertion
4054 point of "func"-with-last-character-incremented,
4055 i.e. "fund". */
4056 std::string after = make_sort_after_prefix_name (lang_name);
4057 if (after.empty ())
4058 return end;
4059 return std::lower_bound (lower, end, after.c_str (),
4060 lookup_compare_lower);
4061 }
4062 else
4063 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4064 } ();
4065
4066 return {lower, upper};
4067 }
4068
4069 /* See declaration. */
4070
4071 void
4072 mapped_index_base::build_name_components ()
4073 {
4074 if (!this->name_components.empty ())
4075 return;
4076
4077 this->name_components_casing = case_sensitivity;
4078 auto *name_cmp
4079 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4080
4081 /* The code below only knows how to break apart components of C++
4082 symbol names (and other languages that use '::' as
4083 namespace/module separator) and Ada symbol names. */
4084 auto count = this->symbol_name_count ();
4085 for (offset_type idx = 0; idx < count; idx++)
4086 {
4087 if (this->symbol_name_slot_invalid (idx))
4088 continue;
4089
4090 const char *name = this->symbol_name_at (idx);
4091
4092 /* Add each name component to the name component table. */
4093 unsigned int previous_len = 0;
4094
4095 if (strstr (name, "::") != nullptr)
4096 {
4097 for (unsigned int current_len = cp_find_first_component (name);
4098 name[current_len] != '\0';
4099 current_len += cp_find_first_component (name + current_len))
4100 {
4101 gdb_assert (name[current_len] == ':');
4102 this->name_components.push_back ({previous_len, idx});
4103 /* Skip the '::'. */
4104 current_len += 2;
4105 previous_len = current_len;
4106 }
4107 }
4108 else
4109 {
4110 /* Handle the Ada encoded (aka mangled) form here. */
4111 for (const char *iter = strstr (name, "__");
4112 iter != nullptr;
4113 iter = strstr (iter, "__"))
4114 {
4115 this->name_components.push_back ({previous_len, idx});
4116 iter += 2;
4117 previous_len = iter - name;
4118 }
4119 }
4120
4121 this->name_components.push_back ({previous_len, idx});
4122 }
4123
4124 /* Sort name_components elements by name. */
4125 auto name_comp_compare = [&] (const name_component &left,
4126 const name_component &right)
4127 {
4128 const char *left_qualified = this->symbol_name_at (left.idx);
4129 const char *right_qualified = this->symbol_name_at (right.idx);
4130
4131 const char *left_name = left_qualified + left.name_offset;
4132 const char *right_name = right_qualified + right.name_offset;
4133
4134 return name_cmp (left_name, right_name) < 0;
4135 };
4136
4137 std::sort (this->name_components.begin (),
4138 this->name_components.end (),
4139 name_comp_compare);
4140 }
4141
4142 /* Helper for dw2_expand_symtabs_matching that works with a
4143 mapped_index_base instead of the containing objfile. This is split
4144 to a separate function in order to be able to unit test the
4145 name_components matching using a mock mapped_index_base. For each
4146 symbol name that matches, calls MATCH_CALLBACK, passing it the
4147 symbol's index in the mapped_index_base symbol table. */
4148
4149 static void
4150 dw2_expand_symtabs_matching_symbol
4151 (mapped_index_base &index,
4152 const lookup_name_info &lookup_name_in,
4153 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4154 enum search_domain kind,
4155 gdb::function_view<bool (offset_type)> match_callback)
4156 {
4157 lookup_name_info lookup_name_without_params
4158 = lookup_name_in.make_ignore_params ();
4159
4160 /* Build the symbol name component sorted vector, if we haven't
4161 yet. */
4162 index.build_name_components ();
4163
4164 /* The same symbol may appear more than once in the range though.
4165 E.g., if we're looking for symbols that complete "w", and we have
4166 a symbol named "w1::w2", we'll find the two name components for
4167 that same symbol in the range. To be sure we only call the
4168 callback once per symbol, we first collect the symbol name
4169 indexes that matched in a temporary vector and ignore
4170 duplicates. */
4171 std::vector<offset_type> matches;
4172
4173 struct name_and_matcher
4174 {
4175 symbol_name_matcher_ftype *matcher;
4176 const std::string &name;
4177
4178 bool operator== (const name_and_matcher &other) const
4179 {
4180 return matcher == other.matcher && name == other.name;
4181 }
4182 };
4183
4184 /* A vector holding all the different symbol name matchers, for all
4185 languages. */
4186 std::vector<name_and_matcher> matchers;
4187
4188 for (int i = 0; i < nr_languages; i++)
4189 {
4190 enum language lang_e = (enum language) i;
4191
4192 const language_defn *lang = language_def (lang_e);
4193 symbol_name_matcher_ftype *name_matcher
4194 = get_symbol_name_matcher (lang, lookup_name_without_params);
4195
4196 name_and_matcher key {
4197 name_matcher,
4198 lookup_name_without_params.language_lookup_name (lang_e)
4199 };
4200
4201 /* Don't insert the same comparison routine more than once.
4202 Note that we do this linear walk. This is not a problem in
4203 practice because the number of supported languages is
4204 low. */
4205 if (std::find (matchers.begin (), matchers.end (), key)
4206 != matchers.end ())
4207 continue;
4208 matchers.push_back (std::move (key));
4209
4210 auto bounds
4211 = index.find_name_components_bounds (lookup_name_without_params,
4212 lang_e);
4213
4214 /* Now for each symbol name in range, check to see if we have a name
4215 match, and if so, call the MATCH_CALLBACK callback. */
4216
4217 for (; bounds.first != bounds.second; ++bounds.first)
4218 {
4219 const char *qualified = index.symbol_name_at (bounds.first->idx);
4220
4221 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4222 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4223 continue;
4224
4225 matches.push_back (bounds.first->idx);
4226 }
4227 }
4228
4229 std::sort (matches.begin (), matches.end ());
4230
4231 /* Finally call the callback, once per match. */
4232 ULONGEST prev = -1;
4233 for (offset_type idx : matches)
4234 {
4235 if (prev != idx)
4236 {
4237 if (!match_callback (idx))
4238 break;
4239 prev = idx;
4240 }
4241 }
4242
4243 /* Above we use a type wider than idx's for 'prev', since 0 and
4244 (offset_type)-1 are both possible values. */
4245 static_assert (sizeof (prev) > sizeof (offset_type), "");
4246 }
4247
4248 #if GDB_SELF_TEST
4249
4250 namespace selftests { namespace dw2_expand_symtabs_matching {
4251
4252 /* A mock .gdb_index/.debug_names-like name index table, enough to
4253 exercise dw2_expand_symtabs_matching_symbol, which works with the
4254 mapped_index_base interface. Builds an index from the symbol list
4255 passed as parameter to the constructor. */
4256 class mock_mapped_index : public mapped_index_base
4257 {
4258 public:
4259 mock_mapped_index (gdb::array_view<const char *> symbols)
4260 : m_symbol_table (symbols)
4261 {}
4262
4263 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4264
4265 /* Return the number of names in the symbol table. */
4266 size_t symbol_name_count () const override
4267 {
4268 return m_symbol_table.size ();
4269 }
4270
4271 /* Get the name of the symbol at IDX in the symbol table. */
4272 const char *symbol_name_at (offset_type idx) const override
4273 {
4274 return m_symbol_table[idx];
4275 }
4276
4277 private:
4278 gdb::array_view<const char *> m_symbol_table;
4279 };
4280
4281 /* Convenience function that converts a NULL pointer to a "<null>"
4282 string, to pass to print routines. */
4283
4284 static const char *
4285 string_or_null (const char *str)
4286 {
4287 return str != NULL ? str : "<null>";
4288 }
4289
4290 /* Check if a lookup_name_info built from
4291 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4292 index. EXPECTED_LIST is the list of expected matches, in expected
4293 matching order. If no match expected, then an empty list is
4294 specified. Returns true on success. On failure prints a warning
4295 indicating the file:line that failed, and returns false. */
4296
4297 static bool
4298 check_match (const char *file, int line,
4299 mock_mapped_index &mock_index,
4300 const char *name, symbol_name_match_type match_type,
4301 bool completion_mode,
4302 std::initializer_list<const char *> expected_list)
4303 {
4304 lookup_name_info lookup_name (name, match_type, completion_mode);
4305
4306 bool matched = true;
4307
4308 auto mismatch = [&] (const char *expected_str,
4309 const char *got)
4310 {
4311 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4312 "expected=\"%s\", got=\"%s\"\n"),
4313 file, line,
4314 (match_type == symbol_name_match_type::FULL
4315 ? "FULL" : "WILD"),
4316 name, string_or_null (expected_str), string_or_null (got));
4317 matched = false;
4318 };
4319
4320 auto expected_it = expected_list.begin ();
4321 auto expected_end = expected_list.end ();
4322
4323 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4324 NULL, ALL_DOMAIN,
4325 [&] (offset_type idx)
4326 {
4327 const char *matched_name = mock_index.symbol_name_at (idx);
4328 const char *expected_str
4329 = expected_it == expected_end ? NULL : *expected_it++;
4330
4331 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4332 mismatch (expected_str, matched_name);
4333 return true;
4334 });
4335
4336 const char *expected_str
4337 = expected_it == expected_end ? NULL : *expected_it++;
4338 if (expected_str != NULL)
4339 mismatch (expected_str, NULL);
4340
4341 return matched;
4342 }
4343
4344 /* The symbols added to the mock mapped_index for testing (in
4345 canonical form). */
4346 static const char *test_symbols[] = {
4347 "function",
4348 "std::bar",
4349 "std::zfunction",
4350 "std::zfunction2",
4351 "w1::w2",
4352 "ns::foo<char*>",
4353 "ns::foo<int>",
4354 "ns::foo<long>",
4355 "ns2::tmpl<int>::foo2",
4356 "(anonymous namespace)::A::B::C",
4357
4358 /* These are used to check that the increment-last-char in the
4359 matching algorithm for completion doesn't match "t1_fund" when
4360 completing "t1_func". */
4361 "t1_func",
4362 "t1_func1",
4363 "t1_fund",
4364 "t1_fund1",
4365
4366 /* A UTF-8 name with multi-byte sequences to make sure that
4367 cp-name-parser understands this as a single identifier ("função"
4368 is "function" in PT). */
4369 u8"u8função",
4370
4371 /* \377 (0xff) is Latin1 'ÿ'. */
4372 "yfunc\377",
4373
4374 /* \377 (0xff) is Latin1 'ÿ'. */
4375 "\377",
4376 "\377\377123",
4377
4378 /* A name with all sorts of complications. Starts with "z" to make
4379 it easier for the completion tests below. */
4380 #define Z_SYM_NAME \
4381 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4382 "::tuple<(anonymous namespace)::ui*, " \
4383 "std::default_delete<(anonymous namespace)::ui>, void>"
4384
4385 Z_SYM_NAME
4386 };
4387
4388 /* Returns true if the mapped_index_base::find_name_component_bounds
4389 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4390 in completion mode. */
4391
4392 static bool
4393 check_find_bounds_finds (mapped_index_base &index,
4394 const char *search_name,
4395 gdb::array_view<const char *> expected_syms)
4396 {
4397 lookup_name_info lookup_name (search_name,
4398 symbol_name_match_type::FULL, true);
4399
4400 auto bounds = index.find_name_components_bounds (lookup_name,
4401 language_cplus);
4402
4403 size_t distance = std::distance (bounds.first, bounds.second);
4404 if (distance != expected_syms.size ())
4405 return false;
4406
4407 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4408 {
4409 auto nc_elem = bounds.first + exp_elem;
4410 const char *qualified = index.symbol_name_at (nc_elem->idx);
4411 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4412 return false;
4413 }
4414
4415 return true;
4416 }
4417
4418 /* Test the lower-level mapped_index::find_name_component_bounds
4419 method. */
4420
4421 static void
4422 test_mapped_index_find_name_component_bounds ()
4423 {
4424 mock_mapped_index mock_index (test_symbols);
4425
4426 mock_index.build_name_components ();
4427
4428 /* Test the lower-level mapped_index::find_name_component_bounds
4429 method in completion mode. */
4430 {
4431 static const char *expected_syms[] = {
4432 "t1_func",
4433 "t1_func1",
4434 };
4435
4436 SELF_CHECK (check_find_bounds_finds (mock_index,
4437 "t1_func", expected_syms));
4438 }
4439
4440 /* Check that the increment-last-char in the name matching algorithm
4441 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4442 {
4443 static const char *expected_syms1[] = {
4444 "\377",
4445 "\377\377123",
4446 };
4447 SELF_CHECK (check_find_bounds_finds (mock_index,
4448 "\377", expected_syms1));
4449
4450 static const char *expected_syms2[] = {
4451 "\377\377123",
4452 };
4453 SELF_CHECK (check_find_bounds_finds (mock_index,
4454 "\377\377", expected_syms2));
4455 }
4456 }
4457
4458 /* Test dw2_expand_symtabs_matching_symbol. */
4459
4460 static void
4461 test_dw2_expand_symtabs_matching_symbol ()
4462 {
4463 mock_mapped_index mock_index (test_symbols);
4464
4465 /* We let all tests run until the end even if some fails, for debug
4466 convenience. */
4467 bool any_mismatch = false;
4468
4469 /* Create the expected symbols list (an initializer_list). Needed
4470 because lists have commas, and we need to pass them to CHECK,
4471 which is a macro. */
4472 #define EXPECT(...) { __VA_ARGS__ }
4473
4474 /* Wrapper for check_match that passes down the current
4475 __FILE__/__LINE__. */
4476 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4477 any_mismatch |= !check_match (__FILE__, __LINE__, \
4478 mock_index, \
4479 NAME, MATCH_TYPE, COMPLETION_MODE, \
4480 EXPECTED_LIST)
4481
4482 /* Identity checks. */
4483 for (const char *sym : test_symbols)
4484 {
4485 /* Should be able to match all existing symbols. */
4486 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4487 EXPECT (sym));
4488
4489 /* Should be able to match all existing symbols with
4490 parameters. */
4491 std::string with_params = std::string (sym) + "(int)";
4492 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4493 EXPECT (sym));
4494
4495 /* Should be able to match all existing symbols with
4496 parameters and qualifiers. */
4497 with_params = std::string (sym) + " ( int ) const";
4498 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4499 EXPECT (sym));
4500
4501 /* This should really find sym, but cp-name-parser.y doesn't
4502 know about lvalue/rvalue qualifiers yet. */
4503 with_params = std::string (sym) + " ( int ) &&";
4504 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4505 {});
4506 }
4507
4508 /* Check that the name matching algorithm for completion doesn't get
4509 confused with Latin1 'ÿ' / 0xff. */
4510 {
4511 static const char str[] = "\377";
4512 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4513 EXPECT ("\377", "\377\377123"));
4514 }
4515
4516 /* Check that the increment-last-char in the matching algorithm for
4517 completion doesn't match "t1_fund" when completing "t1_func". */
4518 {
4519 static const char str[] = "t1_func";
4520 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4521 EXPECT ("t1_func", "t1_func1"));
4522 }
4523
4524 /* Check that completion mode works at each prefix of the expected
4525 symbol name. */
4526 {
4527 static const char str[] = "function(int)";
4528 size_t len = strlen (str);
4529 std::string lookup;
4530
4531 for (size_t i = 1; i < len; i++)
4532 {
4533 lookup.assign (str, i);
4534 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4535 EXPECT ("function"));
4536 }
4537 }
4538
4539 /* While "w" is a prefix of both components, the match function
4540 should still only be called once. */
4541 {
4542 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4543 EXPECT ("w1::w2"));
4544 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4545 EXPECT ("w1::w2"));
4546 }
4547
4548 /* Same, with a "complicated" symbol. */
4549 {
4550 static const char str[] = Z_SYM_NAME;
4551 size_t len = strlen (str);
4552 std::string lookup;
4553
4554 for (size_t i = 1; i < len; i++)
4555 {
4556 lookup.assign (str, i);
4557 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4558 EXPECT (Z_SYM_NAME));
4559 }
4560 }
4561
4562 /* In FULL mode, an incomplete symbol doesn't match. */
4563 {
4564 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4565 {});
4566 }
4567
4568 /* A complete symbol with parameters matches any overload, since the
4569 index has no overload info. */
4570 {
4571 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4572 EXPECT ("std::zfunction", "std::zfunction2"));
4573 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4574 EXPECT ("std::zfunction", "std::zfunction2"));
4575 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4576 EXPECT ("std::zfunction", "std::zfunction2"));
4577 }
4578
4579 /* Check that whitespace is ignored appropriately. A symbol with a
4580 template argument list. */
4581 {
4582 static const char expected[] = "ns::foo<int>";
4583 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4584 EXPECT (expected));
4585 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4586 EXPECT (expected));
4587 }
4588
4589 /* Check that whitespace is ignored appropriately. A symbol with a
4590 template argument list that includes a pointer. */
4591 {
4592 static const char expected[] = "ns::foo<char*>";
4593 /* Try both completion and non-completion modes. */
4594 static const bool completion_mode[2] = {false, true};
4595 for (size_t i = 0; i < 2; i++)
4596 {
4597 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4598 completion_mode[i], EXPECT (expected));
4599 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4600 completion_mode[i], EXPECT (expected));
4601
4602 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4603 completion_mode[i], EXPECT (expected));
4604 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4605 completion_mode[i], EXPECT (expected));
4606 }
4607 }
4608
4609 {
4610 /* Check method qualifiers are ignored. */
4611 static const char expected[] = "ns::foo<char*>";
4612 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4613 symbol_name_match_type::FULL, true, EXPECT (expected));
4614 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4615 symbol_name_match_type::FULL, true, EXPECT (expected));
4616 CHECK_MATCH ("foo < char * > ( int ) const",
4617 symbol_name_match_type::WILD, true, EXPECT (expected));
4618 CHECK_MATCH ("foo < char * > ( int ) &&",
4619 symbol_name_match_type::WILD, true, EXPECT (expected));
4620 }
4621
4622 /* Test lookup names that don't match anything. */
4623 {
4624 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4625 {});
4626
4627 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4628 {});
4629 }
4630
4631 /* Some wild matching tests, exercising "(anonymous namespace)",
4632 which should not be confused with a parameter list. */
4633 {
4634 static const char *syms[] = {
4635 "A::B::C",
4636 "B::C",
4637 "C",
4638 "A :: B :: C ( int )",
4639 "B :: C ( int )",
4640 "C ( int )",
4641 };
4642
4643 for (const char *s : syms)
4644 {
4645 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4646 EXPECT ("(anonymous namespace)::A::B::C"));
4647 }
4648 }
4649
4650 {
4651 static const char expected[] = "ns2::tmpl<int>::foo2";
4652 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4653 EXPECT (expected));
4654 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4655 EXPECT (expected));
4656 }
4657
4658 SELF_CHECK (!any_mismatch);
4659
4660 #undef EXPECT
4661 #undef CHECK_MATCH
4662 }
4663
4664 static void
4665 run_test ()
4666 {
4667 test_mapped_index_find_name_component_bounds ();
4668 test_dw2_expand_symtabs_matching_symbol ();
4669 }
4670
4671 }} // namespace selftests::dw2_expand_symtabs_matching
4672
4673 #endif /* GDB_SELF_TEST */
4674
4675 /* If FILE_MATCHER is NULL or if PER_CU has
4676 dwarf2_per_cu_quick_data::MARK set (see
4677 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4678 EXPANSION_NOTIFY on it. */
4679
4680 static void
4681 dw2_expand_symtabs_matching_one
4682 (struct dwarf2_per_cu_data *per_cu,
4683 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4684 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4685 {
4686 if (file_matcher == NULL || per_cu->v.quick->mark)
4687 {
4688 bool symtab_was_null
4689 = (per_cu->v.quick->compunit_symtab == NULL);
4690
4691 dw2_instantiate_symtab (per_cu, false);
4692
4693 if (expansion_notify != NULL
4694 && symtab_was_null
4695 && per_cu->v.quick->compunit_symtab != NULL)
4696 expansion_notify (per_cu->v.quick->compunit_symtab);
4697 }
4698 }
4699
4700 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4701 matched, to expand corresponding CUs that were marked. IDX is the
4702 index of the symbol name that matched. */
4703
4704 static void
4705 dw2_expand_marked_cus
4706 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4707 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4708 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4709 search_domain kind)
4710 {
4711 offset_type *vec, vec_len, vec_idx;
4712 bool global_seen = false;
4713 mapped_index &index = *dwarf2_per_objfile->index_table;
4714
4715 vec = (offset_type *) (index.constant_pool
4716 + MAYBE_SWAP (index.symbol_table[idx].vec));
4717 vec_len = MAYBE_SWAP (vec[0]);
4718 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4719 {
4720 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4721 /* This value is only valid for index versions >= 7. */
4722 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4723 gdb_index_symbol_kind symbol_kind =
4724 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4725 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4726 /* Only check the symbol attributes if they're present.
4727 Indices prior to version 7 don't record them,
4728 and indices >= 7 may elide them for certain symbols
4729 (gold does this). */
4730 int attrs_valid =
4731 (index.version >= 7
4732 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4733
4734 /* Work around gold/15646. */
4735 if (attrs_valid)
4736 {
4737 if (!is_static && global_seen)
4738 continue;
4739 if (!is_static)
4740 global_seen = true;
4741 }
4742
4743 /* Only check the symbol's kind if it has one. */
4744 if (attrs_valid)
4745 {
4746 switch (kind)
4747 {
4748 case VARIABLES_DOMAIN:
4749 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4750 continue;
4751 break;
4752 case FUNCTIONS_DOMAIN:
4753 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4754 continue;
4755 break;
4756 case TYPES_DOMAIN:
4757 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4758 continue;
4759 break;
4760 case MODULES_DOMAIN:
4761 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4762 continue;
4763 break;
4764 default:
4765 break;
4766 }
4767 }
4768
4769 /* Don't crash on bad data. */
4770 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4771 + dwarf2_per_objfile->all_type_units.size ()))
4772 {
4773 complaint (_(".gdb_index entry has bad CU index"
4774 " [in module %s]"),
4775 objfile_name (dwarf2_per_objfile->objfile));
4776 continue;
4777 }
4778
4779 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4780 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4781 expansion_notify);
4782 }
4783 }
4784
4785 /* If FILE_MATCHER is non-NULL, set all the
4786 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4787 that match FILE_MATCHER. */
4788
4789 static void
4790 dw_expand_symtabs_matching_file_matcher
4791 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4792 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4793 {
4794 if (file_matcher == NULL)
4795 return;
4796
4797 objfile *const objfile = dwarf2_per_objfile->objfile;
4798
4799 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4800 htab_eq_pointer,
4801 NULL, xcalloc, xfree));
4802 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4803 htab_eq_pointer,
4804 NULL, xcalloc, xfree));
4805
4806 /* The rule is CUs specify all the files, including those used by
4807 any TU, so there's no need to scan TUs here. */
4808
4809 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4810 {
4811 QUIT;
4812
4813 per_cu->v.quick->mark = 0;
4814
4815 /* We only need to look at symtabs not already expanded. */
4816 if (per_cu->v.quick->compunit_symtab)
4817 continue;
4818
4819 quick_file_names *file_data = dw2_get_file_names (per_cu);
4820 if (file_data == NULL)
4821 continue;
4822
4823 if (htab_find (visited_not_found.get (), file_data) != NULL)
4824 continue;
4825 else if (htab_find (visited_found.get (), file_data) != NULL)
4826 {
4827 per_cu->v.quick->mark = 1;
4828 continue;
4829 }
4830
4831 for (int j = 0; j < file_data->num_file_names; ++j)
4832 {
4833 const char *this_real_name;
4834
4835 if (file_matcher (file_data->file_names[j], false))
4836 {
4837 per_cu->v.quick->mark = 1;
4838 break;
4839 }
4840
4841 /* Before we invoke realpath, which can get expensive when many
4842 files are involved, do a quick comparison of the basenames. */
4843 if (!basenames_may_differ
4844 && !file_matcher (lbasename (file_data->file_names[j]),
4845 true))
4846 continue;
4847
4848 this_real_name = dw2_get_real_path (objfile, file_data, j);
4849 if (file_matcher (this_real_name, false))
4850 {
4851 per_cu->v.quick->mark = 1;
4852 break;
4853 }
4854 }
4855
4856 void **slot = htab_find_slot (per_cu->v.quick->mark
4857 ? visited_found.get ()
4858 : visited_not_found.get (),
4859 file_data, INSERT);
4860 *slot = file_data;
4861 }
4862 }
4863
4864 static void
4865 dw2_expand_symtabs_matching
4866 (struct objfile *objfile,
4867 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4868 const lookup_name_info &lookup_name,
4869 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4870 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4871 enum search_domain kind)
4872 {
4873 struct dwarf2_per_objfile *dwarf2_per_objfile
4874 = get_dwarf2_per_objfile (objfile);
4875
4876 /* index_table is NULL if OBJF_READNOW. */
4877 if (!dwarf2_per_objfile->index_table)
4878 return;
4879
4880 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4881
4882 mapped_index &index = *dwarf2_per_objfile->index_table;
4883
4884 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4885 symbol_matcher,
4886 kind, [&] (offset_type idx)
4887 {
4888 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4889 expansion_notify, kind);
4890 return true;
4891 });
4892 }
4893
4894 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4895 symtab. */
4896
4897 static struct compunit_symtab *
4898 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4899 CORE_ADDR pc)
4900 {
4901 int i;
4902
4903 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4904 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4905 return cust;
4906
4907 if (cust->includes == NULL)
4908 return NULL;
4909
4910 for (i = 0; cust->includes[i]; ++i)
4911 {
4912 struct compunit_symtab *s = cust->includes[i];
4913
4914 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4915 if (s != NULL)
4916 return s;
4917 }
4918
4919 return NULL;
4920 }
4921
4922 static struct compunit_symtab *
4923 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4924 struct bound_minimal_symbol msymbol,
4925 CORE_ADDR pc,
4926 struct obj_section *section,
4927 int warn_if_readin)
4928 {
4929 struct dwarf2_per_cu_data *data;
4930 struct compunit_symtab *result;
4931
4932 if (!objfile->partial_symtabs->psymtabs_addrmap)
4933 return NULL;
4934
4935 CORE_ADDR baseaddr = objfile->text_section_offset ();
4936 data = (struct dwarf2_per_cu_data *) addrmap_find
4937 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4938 if (!data)
4939 return NULL;
4940
4941 if (warn_if_readin && data->v.quick->compunit_symtab)
4942 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4943 paddress (get_objfile_arch (objfile), pc));
4944
4945 result
4946 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4947 false),
4948 pc);
4949 gdb_assert (result != NULL);
4950 return result;
4951 }
4952
4953 static void
4954 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4955 void *data, int need_fullname)
4956 {
4957 struct dwarf2_per_objfile *dwarf2_per_objfile
4958 = get_dwarf2_per_objfile (objfile);
4959
4960 if (!dwarf2_per_objfile->filenames_cache)
4961 {
4962 dwarf2_per_objfile->filenames_cache.emplace ();
4963
4964 htab_up visited (htab_create_alloc (10,
4965 htab_hash_pointer, htab_eq_pointer,
4966 NULL, xcalloc, xfree));
4967
4968 /* The rule is CUs specify all the files, including those used
4969 by any TU, so there's no need to scan TUs here. We can
4970 ignore file names coming from already-expanded CUs. */
4971
4972 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4973 {
4974 if (per_cu->v.quick->compunit_symtab)
4975 {
4976 void **slot = htab_find_slot (visited.get (),
4977 per_cu->v.quick->file_names,
4978 INSERT);
4979
4980 *slot = per_cu->v.quick->file_names;
4981 }
4982 }
4983
4984 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4985 {
4986 /* We only need to look at symtabs not already expanded. */
4987 if (per_cu->v.quick->compunit_symtab)
4988 continue;
4989
4990 quick_file_names *file_data = dw2_get_file_names (per_cu);
4991 if (file_data == NULL)
4992 continue;
4993
4994 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4995 if (*slot)
4996 {
4997 /* Already visited. */
4998 continue;
4999 }
5000 *slot = file_data;
5001
5002 for (int j = 0; j < file_data->num_file_names; ++j)
5003 {
5004 const char *filename = file_data->file_names[j];
5005 dwarf2_per_objfile->filenames_cache->seen (filename);
5006 }
5007 }
5008 }
5009
5010 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5011 {
5012 gdb::unique_xmalloc_ptr<char> this_real_name;
5013
5014 if (need_fullname)
5015 this_real_name = gdb_realpath (filename);
5016 (*fun) (filename, this_real_name.get (), data);
5017 });
5018 }
5019
5020 static int
5021 dw2_has_symbols (struct objfile *objfile)
5022 {
5023 return 1;
5024 }
5025
5026 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5027 {
5028 dw2_has_symbols,
5029 dw2_find_last_source_symtab,
5030 dw2_forget_cached_source_info,
5031 dw2_map_symtabs_matching_filename,
5032 dw2_lookup_symbol,
5033 dw2_print_stats,
5034 dw2_dump,
5035 dw2_expand_symtabs_for_function,
5036 dw2_expand_all_symtabs,
5037 dw2_expand_symtabs_with_fullname,
5038 dw2_map_matching_symbols,
5039 dw2_expand_symtabs_matching,
5040 dw2_find_pc_sect_compunit_symtab,
5041 NULL,
5042 dw2_map_symbol_filenames
5043 };
5044
5045 /* DWARF-5 debug_names reader. */
5046
5047 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5048 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5049
5050 /* A helper function that reads the .debug_names section in SECTION
5051 and fills in MAP. FILENAME is the name of the file containing the
5052 section; it is used for error reporting.
5053
5054 Returns true if all went well, false otherwise. */
5055
5056 static bool
5057 read_debug_names_from_section (struct objfile *objfile,
5058 const char *filename,
5059 struct dwarf2_section_info *section,
5060 mapped_debug_names &map)
5061 {
5062 if (section->empty ())
5063 return false;
5064
5065 /* Older elfutils strip versions could keep the section in the main
5066 executable while splitting it for the separate debug info file. */
5067 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5068 return false;
5069
5070 section->read (objfile);
5071
5072 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5073
5074 const gdb_byte *addr = section->buffer;
5075
5076 bfd *const abfd = section->get_bfd_owner ();
5077
5078 unsigned int bytes_read;
5079 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5080 addr += bytes_read;
5081
5082 map.dwarf5_is_dwarf64 = bytes_read != 4;
5083 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5084 if (bytes_read + length != section->size)
5085 {
5086 /* There may be multiple per-CU indices. */
5087 warning (_("Section .debug_names in %s length %s does not match "
5088 "section length %s, ignoring .debug_names."),
5089 filename, plongest (bytes_read + length),
5090 pulongest (section->size));
5091 return false;
5092 }
5093
5094 /* The version number. */
5095 uint16_t version = read_2_bytes (abfd, addr);
5096 addr += 2;
5097 if (version != 5)
5098 {
5099 warning (_("Section .debug_names in %s has unsupported version %d, "
5100 "ignoring .debug_names."),
5101 filename, version);
5102 return false;
5103 }
5104
5105 /* Padding. */
5106 uint16_t padding = read_2_bytes (abfd, addr);
5107 addr += 2;
5108 if (padding != 0)
5109 {
5110 warning (_("Section .debug_names in %s has unsupported padding %d, "
5111 "ignoring .debug_names."),
5112 filename, padding);
5113 return false;
5114 }
5115
5116 /* comp_unit_count - The number of CUs in the CU list. */
5117 map.cu_count = read_4_bytes (abfd, addr);
5118 addr += 4;
5119
5120 /* local_type_unit_count - The number of TUs in the local TU
5121 list. */
5122 map.tu_count = read_4_bytes (abfd, addr);
5123 addr += 4;
5124
5125 /* foreign_type_unit_count - The number of TUs in the foreign TU
5126 list. */
5127 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5128 addr += 4;
5129 if (foreign_tu_count != 0)
5130 {
5131 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5132 "ignoring .debug_names."),
5133 filename, static_cast<unsigned long> (foreign_tu_count));
5134 return false;
5135 }
5136
5137 /* bucket_count - The number of hash buckets in the hash lookup
5138 table. */
5139 map.bucket_count = read_4_bytes (abfd, addr);
5140 addr += 4;
5141
5142 /* name_count - The number of unique names in the index. */
5143 map.name_count = read_4_bytes (abfd, addr);
5144 addr += 4;
5145
5146 /* abbrev_table_size - The size in bytes of the abbreviations
5147 table. */
5148 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5149 addr += 4;
5150
5151 /* augmentation_string_size - The size in bytes of the augmentation
5152 string. This value is rounded up to a multiple of 4. */
5153 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5154 addr += 4;
5155 map.augmentation_is_gdb = ((augmentation_string_size
5156 == sizeof (dwarf5_augmentation))
5157 && memcmp (addr, dwarf5_augmentation,
5158 sizeof (dwarf5_augmentation)) == 0);
5159 augmentation_string_size += (-augmentation_string_size) & 3;
5160 addr += augmentation_string_size;
5161
5162 /* List of CUs */
5163 map.cu_table_reordered = addr;
5164 addr += map.cu_count * map.offset_size;
5165
5166 /* List of Local TUs */
5167 map.tu_table_reordered = addr;
5168 addr += map.tu_count * map.offset_size;
5169
5170 /* Hash Lookup Table */
5171 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5172 addr += map.bucket_count * 4;
5173 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5174 addr += map.name_count * 4;
5175
5176 /* Name Table */
5177 map.name_table_string_offs_reordered = addr;
5178 addr += map.name_count * map.offset_size;
5179 map.name_table_entry_offs_reordered = addr;
5180 addr += map.name_count * map.offset_size;
5181
5182 const gdb_byte *abbrev_table_start = addr;
5183 for (;;)
5184 {
5185 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5186 addr += bytes_read;
5187 if (index_num == 0)
5188 break;
5189
5190 const auto insertpair
5191 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5192 if (!insertpair.second)
5193 {
5194 warning (_("Section .debug_names in %s has duplicate index %s, "
5195 "ignoring .debug_names."),
5196 filename, pulongest (index_num));
5197 return false;
5198 }
5199 mapped_debug_names::index_val &indexval = insertpair.first->second;
5200 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5201 addr += bytes_read;
5202
5203 for (;;)
5204 {
5205 mapped_debug_names::index_val::attr attr;
5206 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5207 addr += bytes_read;
5208 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5209 addr += bytes_read;
5210 if (attr.form == DW_FORM_implicit_const)
5211 {
5212 attr.implicit_const = read_signed_leb128 (abfd, addr,
5213 &bytes_read);
5214 addr += bytes_read;
5215 }
5216 if (attr.dw_idx == 0 && attr.form == 0)
5217 break;
5218 indexval.attr_vec.push_back (std::move (attr));
5219 }
5220 }
5221 if (addr != abbrev_table_start + abbrev_table_size)
5222 {
5223 warning (_("Section .debug_names in %s has abbreviation_table "
5224 "of size %s vs. written as %u, ignoring .debug_names."),
5225 filename, plongest (addr - abbrev_table_start),
5226 abbrev_table_size);
5227 return false;
5228 }
5229 map.entry_pool = addr;
5230
5231 return true;
5232 }
5233
5234 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5235 list. */
5236
5237 static void
5238 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5239 const mapped_debug_names &map,
5240 dwarf2_section_info &section,
5241 bool is_dwz)
5242 {
5243 sect_offset sect_off_prev;
5244 for (uint32_t i = 0; i <= map.cu_count; ++i)
5245 {
5246 sect_offset sect_off_next;
5247 if (i < map.cu_count)
5248 {
5249 sect_off_next
5250 = (sect_offset) (extract_unsigned_integer
5251 (map.cu_table_reordered + i * map.offset_size,
5252 map.offset_size,
5253 map.dwarf5_byte_order));
5254 }
5255 else
5256 sect_off_next = (sect_offset) section.size;
5257 if (i >= 1)
5258 {
5259 const ULONGEST length = sect_off_next - sect_off_prev;
5260 dwarf2_per_cu_data *per_cu
5261 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5262 sect_off_prev, length);
5263 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5264 }
5265 sect_off_prev = sect_off_next;
5266 }
5267 }
5268
5269 /* Read the CU list from the mapped index, and use it to create all
5270 the CU objects for this dwarf2_per_objfile. */
5271
5272 static void
5273 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5274 const mapped_debug_names &map,
5275 const mapped_debug_names &dwz_map)
5276 {
5277 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5278 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5279
5280 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5281 dwarf2_per_objfile->info,
5282 false /* is_dwz */);
5283
5284 if (dwz_map.cu_count == 0)
5285 return;
5286
5287 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5288 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5289 true /* is_dwz */);
5290 }
5291
5292 /* Read .debug_names. If everything went ok, initialize the "quick"
5293 elements of all the CUs and return true. Otherwise, return false. */
5294
5295 static bool
5296 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5297 {
5298 std::unique_ptr<mapped_debug_names> map
5299 (new mapped_debug_names (dwarf2_per_objfile));
5300 mapped_debug_names dwz_map (dwarf2_per_objfile);
5301 struct objfile *objfile = dwarf2_per_objfile->objfile;
5302
5303 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5304 &dwarf2_per_objfile->debug_names,
5305 *map))
5306 return false;
5307
5308 /* Don't use the index if it's empty. */
5309 if (map->name_count == 0)
5310 return false;
5311
5312 /* If there is a .dwz file, read it so we can get its CU list as
5313 well. */
5314 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5315 if (dwz != NULL)
5316 {
5317 if (!read_debug_names_from_section (objfile,
5318 bfd_get_filename (dwz->dwz_bfd.get ()),
5319 &dwz->debug_names, dwz_map))
5320 {
5321 warning (_("could not read '.debug_names' section from %s; skipping"),
5322 bfd_get_filename (dwz->dwz_bfd.get ()));
5323 return false;
5324 }
5325 }
5326
5327 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5328
5329 if (map->tu_count != 0)
5330 {
5331 /* We can only handle a single .debug_types when we have an
5332 index. */
5333 if (dwarf2_per_objfile->types.size () != 1)
5334 return false;
5335
5336 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5337
5338 create_signatured_type_table_from_debug_names
5339 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5340 }
5341
5342 create_addrmap_from_aranges (dwarf2_per_objfile,
5343 &dwarf2_per_objfile->debug_aranges);
5344
5345 dwarf2_per_objfile->debug_names_table = std::move (map);
5346 dwarf2_per_objfile->using_index = 1;
5347 dwarf2_per_objfile->quick_file_names_table =
5348 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5349
5350 return true;
5351 }
5352
5353 /* Type used to manage iterating over all CUs looking for a symbol for
5354 .debug_names. */
5355
5356 class dw2_debug_names_iterator
5357 {
5358 public:
5359 dw2_debug_names_iterator (const mapped_debug_names &map,
5360 gdb::optional<block_enum> block_index,
5361 domain_enum domain,
5362 const char *name)
5363 : m_map (map), m_block_index (block_index), m_domain (domain),
5364 m_addr (find_vec_in_debug_names (map, name))
5365 {}
5366
5367 dw2_debug_names_iterator (const mapped_debug_names &map,
5368 search_domain search, uint32_t namei)
5369 : m_map (map),
5370 m_search (search),
5371 m_addr (find_vec_in_debug_names (map, namei))
5372 {}
5373
5374 dw2_debug_names_iterator (const mapped_debug_names &map,
5375 block_enum block_index, domain_enum domain,
5376 uint32_t namei)
5377 : m_map (map), m_block_index (block_index), m_domain (domain),
5378 m_addr (find_vec_in_debug_names (map, namei))
5379 {}
5380
5381 /* Return the next matching CU or NULL if there are no more. */
5382 dwarf2_per_cu_data *next ();
5383
5384 private:
5385 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5386 const char *name);
5387 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5388 uint32_t namei);
5389
5390 /* The internalized form of .debug_names. */
5391 const mapped_debug_names &m_map;
5392
5393 /* If set, only look for symbols that match that block. Valid values are
5394 GLOBAL_BLOCK and STATIC_BLOCK. */
5395 const gdb::optional<block_enum> m_block_index;
5396
5397 /* The kind of symbol we're looking for. */
5398 const domain_enum m_domain = UNDEF_DOMAIN;
5399 const search_domain m_search = ALL_DOMAIN;
5400
5401 /* The list of CUs from the index entry of the symbol, or NULL if
5402 not found. */
5403 const gdb_byte *m_addr;
5404 };
5405
5406 const char *
5407 mapped_debug_names::namei_to_name (uint32_t namei) const
5408 {
5409 const ULONGEST namei_string_offs
5410 = extract_unsigned_integer ((name_table_string_offs_reordered
5411 + namei * offset_size),
5412 offset_size,
5413 dwarf5_byte_order);
5414 return read_indirect_string_at_offset
5415 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5416 }
5417
5418 /* Find a slot in .debug_names for the object named NAME. If NAME is
5419 found, return pointer to its pool data. If NAME cannot be found,
5420 return NULL. */
5421
5422 const gdb_byte *
5423 dw2_debug_names_iterator::find_vec_in_debug_names
5424 (const mapped_debug_names &map, const char *name)
5425 {
5426 int (*cmp) (const char *, const char *);
5427
5428 gdb::unique_xmalloc_ptr<char> without_params;
5429 if (current_language->la_language == language_cplus
5430 || current_language->la_language == language_fortran
5431 || current_language->la_language == language_d)
5432 {
5433 /* NAME is already canonical. Drop any qualifiers as
5434 .debug_names does not contain any. */
5435
5436 if (strchr (name, '(') != NULL)
5437 {
5438 without_params = cp_remove_params (name);
5439 if (without_params != NULL)
5440 name = without_params.get ();
5441 }
5442 }
5443
5444 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5445
5446 const uint32_t full_hash = dwarf5_djb_hash (name);
5447 uint32_t namei
5448 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5449 (map.bucket_table_reordered
5450 + (full_hash % map.bucket_count)), 4,
5451 map.dwarf5_byte_order);
5452 if (namei == 0)
5453 return NULL;
5454 --namei;
5455 if (namei >= map.name_count)
5456 {
5457 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5458 "[in module %s]"),
5459 namei, map.name_count,
5460 objfile_name (map.dwarf2_per_objfile->objfile));
5461 return NULL;
5462 }
5463
5464 for (;;)
5465 {
5466 const uint32_t namei_full_hash
5467 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5468 (map.hash_table_reordered + namei), 4,
5469 map.dwarf5_byte_order);
5470 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5471 return NULL;
5472
5473 if (full_hash == namei_full_hash)
5474 {
5475 const char *const namei_string = map.namei_to_name (namei);
5476
5477 #if 0 /* An expensive sanity check. */
5478 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5479 {
5480 complaint (_("Wrong .debug_names hash for string at index %u "
5481 "[in module %s]"),
5482 namei, objfile_name (dwarf2_per_objfile->objfile));
5483 return NULL;
5484 }
5485 #endif
5486
5487 if (cmp (namei_string, name) == 0)
5488 {
5489 const ULONGEST namei_entry_offs
5490 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5491 + namei * map.offset_size),
5492 map.offset_size, map.dwarf5_byte_order);
5493 return map.entry_pool + namei_entry_offs;
5494 }
5495 }
5496
5497 ++namei;
5498 if (namei >= map.name_count)
5499 return NULL;
5500 }
5501 }
5502
5503 const gdb_byte *
5504 dw2_debug_names_iterator::find_vec_in_debug_names
5505 (const mapped_debug_names &map, uint32_t namei)
5506 {
5507 if (namei >= map.name_count)
5508 {
5509 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5510 "[in module %s]"),
5511 namei, map.name_count,
5512 objfile_name (map.dwarf2_per_objfile->objfile));
5513 return NULL;
5514 }
5515
5516 const ULONGEST namei_entry_offs
5517 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5518 + namei * map.offset_size),
5519 map.offset_size, map.dwarf5_byte_order);
5520 return map.entry_pool + namei_entry_offs;
5521 }
5522
5523 /* See dw2_debug_names_iterator. */
5524
5525 dwarf2_per_cu_data *
5526 dw2_debug_names_iterator::next ()
5527 {
5528 if (m_addr == NULL)
5529 return NULL;
5530
5531 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5532 struct objfile *objfile = dwarf2_per_objfile->objfile;
5533 bfd *const abfd = objfile->obfd;
5534
5535 again:
5536
5537 unsigned int bytes_read;
5538 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5539 m_addr += bytes_read;
5540 if (abbrev == 0)
5541 return NULL;
5542
5543 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5544 if (indexval_it == m_map.abbrev_map.cend ())
5545 {
5546 complaint (_("Wrong .debug_names undefined abbrev code %s "
5547 "[in module %s]"),
5548 pulongest (abbrev), objfile_name (objfile));
5549 return NULL;
5550 }
5551 const mapped_debug_names::index_val &indexval = indexval_it->second;
5552 enum class symbol_linkage {
5553 unknown,
5554 static_,
5555 extern_,
5556 } symbol_linkage_ = symbol_linkage::unknown;
5557 dwarf2_per_cu_data *per_cu = NULL;
5558 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5559 {
5560 ULONGEST ull;
5561 switch (attr.form)
5562 {
5563 case DW_FORM_implicit_const:
5564 ull = attr.implicit_const;
5565 break;
5566 case DW_FORM_flag_present:
5567 ull = 1;
5568 break;
5569 case DW_FORM_udata:
5570 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5571 m_addr += bytes_read;
5572 break;
5573 default:
5574 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5575 dwarf_form_name (attr.form),
5576 objfile_name (objfile));
5577 return NULL;
5578 }
5579 switch (attr.dw_idx)
5580 {
5581 case DW_IDX_compile_unit:
5582 /* Don't crash on bad data. */
5583 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5584 {
5585 complaint (_(".debug_names entry has bad CU index %s"
5586 " [in module %s]"),
5587 pulongest (ull),
5588 objfile_name (dwarf2_per_objfile->objfile));
5589 continue;
5590 }
5591 per_cu = dwarf2_per_objfile->get_cutu (ull);
5592 break;
5593 case DW_IDX_type_unit:
5594 /* Don't crash on bad data. */
5595 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5596 {
5597 complaint (_(".debug_names entry has bad TU index %s"
5598 " [in module %s]"),
5599 pulongest (ull),
5600 objfile_name (dwarf2_per_objfile->objfile));
5601 continue;
5602 }
5603 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5604 break;
5605 case DW_IDX_GNU_internal:
5606 if (!m_map.augmentation_is_gdb)
5607 break;
5608 symbol_linkage_ = symbol_linkage::static_;
5609 break;
5610 case DW_IDX_GNU_external:
5611 if (!m_map.augmentation_is_gdb)
5612 break;
5613 symbol_linkage_ = symbol_linkage::extern_;
5614 break;
5615 }
5616 }
5617
5618 /* Skip if already read in. */
5619 if (per_cu->v.quick->compunit_symtab)
5620 goto again;
5621
5622 /* Check static vs global. */
5623 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5624 {
5625 const bool want_static = *m_block_index == STATIC_BLOCK;
5626 const bool symbol_is_static =
5627 symbol_linkage_ == symbol_linkage::static_;
5628 if (want_static != symbol_is_static)
5629 goto again;
5630 }
5631
5632 /* Match dw2_symtab_iter_next, symbol_kind
5633 and debug_names::psymbol_tag. */
5634 switch (m_domain)
5635 {
5636 case VAR_DOMAIN:
5637 switch (indexval.dwarf_tag)
5638 {
5639 case DW_TAG_variable:
5640 case DW_TAG_subprogram:
5641 /* Some types are also in VAR_DOMAIN. */
5642 case DW_TAG_typedef:
5643 case DW_TAG_structure_type:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
5649 case STRUCT_DOMAIN:
5650 switch (indexval.dwarf_tag)
5651 {
5652 case DW_TAG_typedef:
5653 case DW_TAG_structure_type:
5654 break;
5655 default:
5656 goto again;
5657 }
5658 break;
5659 case LABEL_DOMAIN:
5660 switch (indexval.dwarf_tag)
5661 {
5662 case 0:
5663 case DW_TAG_variable:
5664 break;
5665 default:
5666 goto again;
5667 }
5668 break;
5669 case MODULE_DOMAIN:
5670 switch (indexval.dwarf_tag)
5671 {
5672 case DW_TAG_module:
5673 break;
5674 default:
5675 goto again;
5676 }
5677 break;
5678 default:
5679 break;
5680 }
5681
5682 /* Match dw2_expand_symtabs_matching, symbol_kind and
5683 debug_names::psymbol_tag. */
5684 switch (m_search)
5685 {
5686 case VARIABLES_DOMAIN:
5687 switch (indexval.dwarf_tag)
5688 {
5689 case DW_TAG_variable:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
5695 case FUNCTIONS_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_subprogram:
5699 break;
5700 default:
5701 goto again;
5702 }
5703 break;
5704 case TYPES_DOMAIN:
5705 switch (indexval.dwarf_tag)
5706 {
5707 case DW_TAG_typedef:
5708 case DW_TAG_structure_type:
5709 break;
5710 default:
5711 goto again;
5712 }
5713 break;
5714 case MODULES_DOMAIN:
5715 switch (indexval.dwarf_tag)
5716 {
5717 case DW_TAG_module:
5718 break;
5719 default:
5720 goto again;
5721 }
5722 default:
5723 break;
5724 }
5725
5726 return per_cu;
5727 }
5728
5729 static struct compunit_symtab *
5730 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5731 const char *name, domain_enum domain)
5732 {
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
5735
5736 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5737 if (!mapp)
5738 {
5739 /* index is NULL if OBJF_READNOW. */
5740 return NULL;
5741 }
5742 const auto &map = *mapp;
5743
5744 dw2_debug_names_iterator iter (map, block_index, domain, name);
5745
5746 struct compunit_symtab *stab_best = NULL;
5747 struct dwarf2_per_cu_data *per_cu;
5748 while ((per_cu = iter.next ()) != NULL)
5749 {
5750 struct symbol *sym, *with_opaque = NULL;
5751 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5752 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5753 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5754
5755 sym = block_find_symbol (block, name, domain,
5756 block_find_non_opaque_type_preferred,
5757 &with_opaque);
5758
5759 /* Some caution must be observed with overloaded functions and
5760 methods, since the index will not contain any overload
5761 information (but NAME might contain it). */
5762
5763 if (sym != NULL
5764 && strcmp_iw (sym->search_name (), name) == 0)
5765 return stab;
5766 if (with_opaque != NULL
5767 && strcmp_iw (with_opaque->search_name (), name) == 0)
5768 stab_best = stab;
5769
5770 /* Keep looking through other CUs. */
5771 }
5772
5773 return stab_best;
5774 }
5775
5776 /* This dumps minimal information about .debug_names. It is called
5777 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5778 uses this to verify that .debug_names has been loaded. */
5779
5780 static void
5781 dw2_debug_names_dump (struct objfile *objfile)
5782 {
5783 struct dwarf2_per_objfile *dwarf2_per_objfile
5784 = get_dwarf2_per_objfile (objfile);
5785
5786 gdb_assert (dwarf2_per_objfile->using_index);
5787 printf_filtered (".debug_names:");
5788 if (dwarf2_per_objfile->debug_names_table)
5789 printf_filtered (" exists\n");
5790 else
5791 printf_filtered (" faked for \"readnow\"\n");
5792 printf_filtered ("\n");
5793 }
5794
5795 static void
5796 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5797 const char *func_name)
5798 {
5799 struct dwarf2_per_objfile *dwarf2_per_objfile
5800 = get_dwarf2_per_objfile (objfile);
5801
5802 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5803 if (dwarf2_per_objfile->debug_names_table)
5804 {
5805 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5806
5807 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5808
5809 struct dwarf2_per_cu_data *per_cu;
5810 while ((per_cu = iter.next ()) != NULL)
5811 dw2_instantiate_symtab (per_cu, false);
5812 }
5813 }
5814
5815 static void
5816 dw2_debug_names_map_matching_symbols
5817 (struct objfile *objfile,
5818 const lookup_name_info &name, domain_enum domain,
5819 int global,
5820 gdb::function_view<symbol_found_callback_ftype> callback,
5821 symbol_compare_ftype *ordered_compare)
5822 {
5823 struct dwarf2_per_objfile *dwarf2_per_objfile
5824 = get_dwarf2_per_objfile (objfile);
5825
5826 /* debug_names_table is NULL if OBJF_READNOW. */
5827 if (!dwarf2_per_objfile->debug_names_table)
5828 return;
5829
5830 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5831 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5832
5833 const char *match_name = name.ada ().lookup_name ().c_str ();
5834 auto matcher = [&] (const char *symname)
5835 {
5836 if (ordered_compare == nullptr)
5837 return true;
5838 return ordered_compare (symname, match_name) == 0;
5839 };
5840
5841 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5842 [&] (offset_type namei)
5843 {
5844 /* The name was matched, now expand corresponding CUs that were
5845 marked. */
5846 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5847
5848 struct dwarf2_per_cu_data *per_cu;
5849 while ((per_cu = iter.next ()) != NULL)
5850 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5851 return true;
5852 });
5853
5854 /* It's a shame we couldn't do this inside the
5855 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5856 that have already been expanded. Instead, this loop matches what
5857 the psymtab code does. */
5858 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5859 {
5860 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5861 if (cust != nullptr)
5862 {
5863 const struct block *block
5864 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5865 if (!iterate_over_symbols_terminated (block, name,
5866 domain, callback))
5867 break;
5868 }
5869 }
5870 }
5871
5872 static void
5873 dw2_debug_names_expand_symtabs_matching
5874 (struct objfile *objfile,
5875 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5876 const lookup_name_info &lookup_name,
5877 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5878 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5879 enum search_domain kind)
5880 {
5881 struct dwarf2_per_objfile *dwarf2_per_objfile
5882 = get_dwarf2_per_objfile (objfile);
5883
5884 /* debug_names_table is NULL if OBJF_READNOW. */
5885 if (!dwarf2_per_objfile->debug_names_table)
5886 return;
5887
5888 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5889
5890 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5891
5892 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5893 symbol_matcher,
5894 kind, [&] (offset_type namei)
5895 {
5896 /* The name was matched, now expand corresponding CUs that were
5897 marked. */
5898 dw2_debug_names_iterator iter (map, kind, namei);
5899
5900 struct dwarf2_per_cu_data *per_cu;
5901 while ((per_cu = iter.next ()) != NULL)
5902 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5903 expansion_notify);
5904 return true;
5905 });
5906 }
5907
5908 const struct quick_symbol_functions dwarf2_debug_names_functions =
5909 {
5910 dw2_has_symbols,
5911 dw2_find_last_source_symtab,
5912 dw2_forget_cached_source_info,
5913 dw2_map_symtabs_matching_filename,
5914 dw2_debug_names_lookup_symbol,
5915 dw2_print_stats,
5916 dw2_debug_names_dump,
5917 dw2_debug_names_expand_symtabs_for_function,
5918 dw2_expand_all_symtabs,
5919 dw2_expand_symtabs_with_fullname,
5920 dw2_debug_names_map_matching_symbols,
5921 dw2_debug_names_expand_symtabs_matching,
5922 dw2_find_pc_sect_compunit_symtab,
5923 NULL,
5924 dw2_map_symbol_filenames
5925 };
5926
5927 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5928 to either a dwarf2_per_objfile or dwz_file object. */
5929
5930 template <typename T>
5931 static gdb::array_view<const gdb_byte>
5932 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5933 {
5934 dwarf2_section_info *section = &section_owner->gdb_index;
5935
5936 if (section->empty ())
5937 return {};
5938
5939 /* Older elfutils strip versions could keep the section in the main
5940 executable while splitting it for the separate debug info file. */
5941 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5942 return {};
5943
5944 section->read (obj);
5945
5946 /* dwarf2_section_info::size is a bfd_size_type, while
5947 gdb::array_view works with size_t. On 32-bit hosts, with
5948 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5949 is 32-bit. So we need an explicit narrowing conversion here.
5950 This is fine, because it's impossible to allocate or mmap an
5951 array/buffer larger than what size_t can represent. */
5952 return gdb::make_array_view (section->buffer, section->size);
5953 }
5954
5955 /* Lookup the index cache for the contents of the index associated to
5956 DWARF2_OBJ. */
5957
5958 static gdb::array_view<const gdb_byte>
5959 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5960 {
5961 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5962 if (build_id == nullptr)
5963 return {};
5964
5965 return global_index_cache.lookup_gdb_index (build_id,
5966 &dwarf2_obj->index_cache_res);
5967 }
5968
5969 /* Same as the above, but for DWZ. */
5970
5971 static gdb::array_view<const gdb_byte>
5972 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5973 {
5974 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5975 if (build_id == nullptr)
5976 return {};
5977
5978 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5979 }
5980
5981 /* See symfile.h. */
5982
5983 bool
5984 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5985 {
5986 struct dwarf2_per_objfile *dwarf2_per_objfile
5987 = get_dwarf2_per_objfile (objfile);
5988
5989 /* If we're about to read full symbols, don't bother with the
5990 indices. In this case we also don't care if some other debug
5991 format is making psymtabs, because they are all about to be
5992 expanded anyway. */
5993 if ((objfile->flags & OBJF_READNOW))
5994 {
5995 dwarf2_per_objfile->using_index = 1;
5996 create_all_comp_units (dwarf2_per_objfile);
5997 create_all_type_units (dwarf2_per_objfile);
5998 dwarf2_per_objfile->quick_file_names_table
5999 = create_quick_file_names_table
6000 (dwarf2_per_objfile->all_comp_units.size ());
6001
6002 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6003 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6004 {
6005 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6006
6007 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6008 struct dwarf2_per_cu_quick_data);
6009 }
6010
6011 /* Return 1 so that gdb sees the "quick" functions. However,
6012 these functions will be no-ops because we will have expanded
6013 all symtabs. */
6014 *index_kind = dw_index_kind::GDB_INDEX;
6015 return true;
6016 }
6017
6018 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6019 {
6020 *index_kind = dw_index_kind::DEBUG_NAMES;
6021 return true;
6022 }
6023
6024 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6025 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6026 get_gdb_index_contents_from_section<dwz_file>))
6027 {
6028 *index_kind = dw_index_kind::GDB_INDEX;
6029 return true;
6030 }
6031
6032 /* ... otherwise, try to find the index in the index cache. */
6033 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6034 get_gdb_index_contents_from_cache,
6035 get_gdb_index_contents_from_cache_dwz))
6036 {
6037 global_index_cache.hit ();
6038 *index_kind = dw_index_kind::GDB_INDEX;
6039 return true;
6040 }
6041
6042 global_index_cache.miss ();
6043 return false;
6044 }
6045
6046 \f
6047
6048 /* Build a partial symbol table. */
6049
6050 void
6051 dwarf2_build_psymtabs (struct objfile *objfile)
6052 {
6053 struct dwarf2_per_objfile *dwarf2_per_objfile
6054 = get_dwarf2_per_objfile (objfile);
6055
6056 init_psymbol_list (objfile, 1024);
6057
6058 try
6059 {
6060 /* This isn't really ideal: all the data we allocate on the
6061 objfile's obstack is still uselessly kept around. However,
6062 freeing it seems unsafe. */
6063 psymtab_discarder psymtabs (objfile);
6064 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6065 psymtabs.keep ();
6066
6067 /* (maybe) store an index in the cache. */
6068 global_index_cache.store (dwarf2_per_objfile);
6069 }
6070 catch (const gdb_exception_error &except)
6071 {
6072 exception_print (gdb_stderr, except);
6073 }
6074 }
6075
6076 /* Return the total length of the CU described by HEADER. */
6077
6078 static unsigned int
6079 get_cu_length (const struct comp_unit_head *header)
6080 {
6081 return header->initial_length_size + header->length;
6082 }
6083
6084 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6085
6086 static inline bool
6087 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6088 {
6089 sect_offset bottom = cu_header->sect_off;
6090 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6091
6092 return sect_off >= bottom && sect_off < top;
6093 }
6094
6095 /* Find the base address of the compilation unit for range lists and
6096 location lists. It will normally be specified by DW_AT_low_pc.
6097 In DWARF-3 draft 4, the base address could be overridden by
6098 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6099 compilation units with discontinuous ranges. */
6100
6101 static void
6102 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6103 {
6104 struct attribute *attr;
6105
6106 cu->base_known = 0;
6107 cu->base_address = 0;
6108
6109 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6110 if (attr != nullptr)
6111 {
6112 cu->base_address = attr->value_as_address ();
6113 cu->base_known = 1;
6114 }
6115 else
6116 {
6117 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6118 if (attr != nullptr)
6119 {
6120 cu->base_address = attr->value_as_address ();
6121 cu->base_known = 1;
6122 }
6123 }
6124 }
6125
6126 /* Read in the comp unit header information from the debug_info at info_ptr.
6127 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6128 NOTE: This leaves members offset, first_die_offset to be filled in
6129 by the caller. */
6130
6131 static const gdb_byte *
6132 read_comp_unit_head (struct comp_unit_head *cu_header,
6133 const gdb_byte *info_ptr,
6134 struct dwarf2_section_info *section,
6135 rcuh_kind section_kind)
6136 {
6137 int signed_addr;
6138 unsigned int bytes_read;
6139 const char *filename = section->get_file_name ();
6140 bfd *abfd = section->get_bfd_owner ();
6141
6142 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6143 cu_header->initial_length_size = bytes_read;
6144 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6145 info_ptr += bytes_read;
6146 cu_header->version = read_2_bytes (abfd, info_ptr);
6147 if (cu_header->version < 2 || cu_header->version > 5)
6148 error (_("Dwarf Error: wrong version in compilation unit header "
6149 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6150 cu_header->version, filename);
6151 info_ptr += 2;
6152 if (cu_header->version < 5)
6153 switch (section_kind)
6154 {
6155 case rcuh_kind::COMPILE:
6156 cu_header->unit_type = DW_UT_compile;
6157 break;
6158 case rcuh_kind::TYPE:
6159 cu_header->unit_type = DW_UT_type;
6160 break;
6161 default:
6162 internal_error (__FILE__, __LINE__,
6163 _("read_comp_unit_head: invalid section_kind"));
6164 }
6165 else
6166 {
6167 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6168 (read_1_byte (abfd, info_ptr));
6169 info_ptr += 1;
6170 switch (cu_header->unit_type)
6171 {
6172 case DW_UT_compile:
6173 case DW_UT_partial:
6174 case DW_UT_skeleton:
6175 case DW_UT_split_compile:
6176 if (section_kind != rcuh_kind::COMPILE)
6177 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6178 "(is %s, should be %s) [in module %s]"),
6179 dwarf_unit_type_name (cu_header->unit_type),
6180 dwarf_unit_type_name (DW_UT_type), filename);
6181 break;
6182 case DW_UT_type:
6183 case DW_UT_split_type:
6184 section_kind = rcuh_kind::TYPE;
6185 break;
6186 default:
6187 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6188 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6189 "[in module %s]"), cu_header->unit_type,
6190 dwarf_unit_type_name (DW_UT_compile),
6191 dwarf_unit_type_name (DW_UT_skeleton),
6192 dwarf_unit_type_name (DW_UT_split_compile),
6193 dwarf_unit_type_name (DW_UT_type),
6194 dwarf_unit_type_name (DW_UT_split_type), filename);
6195 }
6196
6197 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6198 info_ptr += 1;
6199 }
6200 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6201 cu_header,
6202 &bytes_read);
6203 info_ptr += bytes_read;
6204 if (cu_header->version < 5)
6205 {
6206 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6207 info_ptr += 1;
6208 }
6209 signed_addr = bfd_get_sign_extend_vma (abfd);
6210 if (signed_addr < 0)
6211 internal_error (__FILE__, __LINE__,
6212 _("read_comp_unit_head: dwarf from non elf file"));
6213 cu_header->signed_addr_p = signed_addr;
6214
6215 bool header_has_signature = section_kind == rcuh_kind::TYPE
6216 || cu_header->unit_type == DW_UT_skeleton
6217 || cu_header->unit_type == DW_UT_split_compile;
6218
6219 if (header_has_signature)
6220 {
6221 cu_header->signature = read_8_bytes (abfd, info_ptr);
6222 info_ptr += 8;
6223 }
6224
6225 if (section_kind == rcuh_kind::TYPE)
6226 {
6227 LONGEST type_offset;
6228 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6229 info_ptr += bytes_read;
6230 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6231 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6232 error (_("Dwarf Error: Too big type_offset in compilation unit "
6233 "header (is %s) [in module %s]"), plongest (type_offset),
6234 filename);
6235 }
6236
6237 return info_ptr;
6238 }
6239
6240 /* Helper function that returns the proper abbrev section for
6241 THIS_CU. */
6242
6243 static struct dwarf2_section_info *
6244 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6245 {
6246 struct dwarf2_section_info *abbrev;
6247 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6248
6249 if (this_cu->is_dwz)
6250 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6251 else
6252 abbrev = &dwarf2_per_objfile->abbrev;
6253
6254 return abbrev;
6255 }
6256
6257 /* Subroutine of read_and_check_comp_unit_head and
6258 read_and_check_type_unit_head to simplify them.
6259 Perform various error checking on the header. */
6260
6261 static void
6262 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6263 struct comp_unit_head *header,
6264 struct dwarf2_section_info *section,
6265 struct dwarf2_section_info *abbrev_section)
6266 {
6267 const char *filename = section->get_file_name ();
6268
6269 if (to_underlying (header->abbrev_sect_off)
6270 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6271 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6272 "(offset %s + 6) [in module %s]"),
6273 sect_offset_str (header->abbrev_sect_off),
6274 sect_offset_str (header->sect_off),
6275 filename);
6276
6277 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6278 avoid potential 32-bit overflow. */
6279 if (((ULONGEST) header->sect_off + get_cu_length (header))
6280 > section->size)
6281 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6282 "(offset %s + 0) [in module %s]"),
6283 header->length, sect_offset_str (header->sect_off),
6284 filename);
6285 }
6286
6287 /* Read in a CU/TU header and perform some basic error checking.
6288 The contents of the header are stored in HEADER.
6289 The result is a pointer to the start of the first DIE. */
6290
6291 static const gdb_byte *
6292 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6293 struct comp_unit_head *header,
6294 struct dwarf2_section_info *section,
6295 struct dwarf2_section_info *abbrev_section,
6296 const gdb_byte *info_ptr,
6297 rcuh_kind section_kind)
6298 {
6299 const gdb_byte *beg_of_comp_unit = info_ptr;
6300
6301 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6302
6303 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6304
6305 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6306
6307 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6308 abbrev_section);
6309
6310 return info_ptr;
6311 }
6312
6313 /* Fetch the abbreviation table offset from a comp or type unit header. */
6314
6315 static sect_offset
6316 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6317 struct dwarf2_section_info *section,
6318 sect_offset sect_off)
6319 {
6320 bfd *abfd = section->get_bfd_owner ();
6321 const gdb_byte *info_ptr;
6322 unsigned int initial_length_size, offset_size;
6323 uint16_t version;
6324
6325 section->read (dwarf2_per_objfile->objfile);
6326 info_ptr = section->buffer + to_underlying (sect_off);
6327 read_initial_length (abfd, info_ptr, &initial_length_size);
6328 offset_size = initial_length_size == 4 ? 4 : 8;
6329 info_ptr += initial_length_size;
6330
6331 version = read_2_bytes (abfd, info_ptr);
6332 info_ptr += 2;
6333 if (version >= 5)
6334 {
6335 /* Skip unit type and address size. */
6336 info_ptr += 2;
6337 }
6338
6339 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6340 }
6341
6342 /* Allocate a new partial symtab for file named NAME and mark this new
6343 partial symtab as being an include of PST. */
6344
6345 static void
6346 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6347 struct objfile *objfile)
6348 {
6349 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6350
6351 if (!IS_ABSOLUTE_PATH (subpst->filename))
6352 {
6353 /* It shares objfile->objfile_obstack. */
6354 subpst->dirname = pst->dirname;
6355 }
6356
6357 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6358 subpst->dependencies[0] = pst;
6359 subpst->number_of_dependencies = 1;
6360
6361 /* No private part is necessary for include psymtabs. This property
6362 can be used to differentiate between such include psymtabs and
6363 the regular ones. */
6364 subpst->per_cu_data = nullptr;
6365 }
6366
6367 /* Read the Line Number Program data and extract the list of files
6368 included by the source file represented by PST. Build an include
6369 partial symtab for each of these included files. */
6370
6371 static void
6372 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6373 struct die_info *die,
6374 dwarf2_psymtab *pst)
6375 {
6376 line_header_up lh;
6377 struct attribute *attr;
6378
6379 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6380 if (attr != nullptr)
6381 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6382 if (lh == NULL)
6383 return; /* No linetable, so no includes. */
6384
6385 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6386 that we pass in the raw text_low here; that is ok because we're
6387 only decoding the line table to make include partial symtabs, and
6388 so the addresses aren't really used. */
6389 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6390 pst->raw_text_low (), 1);
6391 }
6392
6393 static hashval_t
6394 hash_signatured_type (const void *item)
6395 {
6396 const struct signatured_type *sig_type
6397 = (const struct signatured_type *) item;
6398
6399 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6400 return sig_type->signature;
6401 }
6402
6403 static int
6404 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6405 {
6406 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6407 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6408
6409 return lhs->signature == rhs->signature;
6410 }
6411
6412 /* Allocate a hash table for signatured types. */
6413
6414 static htab_t
6415 allocate_signatured_type_table (struct objfile *objfile)
6416 {
6417 return htab_create_alloc_ex (41,
6418 hash_signatured_type,
6419 eq_signatured_type,
6420 NULL,
6421 &objfile->objfile_obstack,
6422 hashtab_obstack_allocate,
6423 dummy_obstack_deallocate);
6424 }
6425
6426 /* A helper function to add a signatured type CU to a table. */
6427
6428 static int
6429 add_signatured_type_cu_to_table (void **slot, void *datum)
6430 {
6431 struct signatured_type *sigt = (struct signatured_type *) *slot;
6432 std::vector<signatured_type *> *all_type_units
6433 = (std::vector<signatured_type *> *) datum;
6434
6435 all_type_units->push_back (sigt);
6436
6437 return 1;
6438 }
6439
6440 /* A helper for create_debug_types_hash_table. Read types from SECTION
6441 and fill them into TYPES_HTAB. It will process only type units,
6442 therefore DW_UT_type. */
6443
6444 static void
6445 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6446 struct dwo_file *dwo_file,
6447 dwarf2_section_info *section, htab_t &types_htab,
6448 rcuh_kind section_kind)
6449 {
6450 struct objfile *objfile = dwarf2_per_objfile->objfile;
6451 struct dwarf2_section_info *abbrev_section;
6452 bfd *abfd;
6453 const gdb_byte *info_ptr, *end_ptr;
6454
6455 abbrev_section = (dwo_file != NULL
6456 ? &dwo_file->sections.abbrev
6457 : &dwarf2_per_objfile->abbrev);
6458
6459 if (dwarf_read_debug)
6460 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6461 section->get_name (),
6462 abbrev_section->get_file_name ());
6463
6464 section->read (objfile);
6465 info_ptr = section->buffer;
6466
6467 if (info_ptr == NULL)
6468 return;
6469
6470 /* We can't set abfd until now because the section may be empty or
6471 not present, in which case the bfd is unknown. */
6472 abfd = section->get_bfd_owner ();
6473
6474 /* We don't use cutu_reader here because we don't need to read
6475 any dies: the signature is in the header. */
6476
6477 end_ptr = info_ptr + section->size;
6478 while (info_ptr < end_ptr)
6479 {
6480 struct signatured_type *sig_type;
6481 struct dwo_unit *dwo_tu;
6482 void **slot;
6483 const gdb_byte *ptr = info_ptr;
6484 struct comp_unit_head header;
6485 unsigned int length;
6486
6487 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6488
6489 /* Initialize it due to a false compiler warning. */
6490 header.signature = -1;
6491 header.type_cu_offset_in_tu = (cu_offset) -1;
6492
6493 /* We need to read the type's signature in order to build the hash
6494 table, but we don't need anything else just yet. */
6495
6496 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6497 abbrev_section, ptr, section_kind);
6498
6499 length = get_cu_length (&header);
6500
6501 /* Skip dummy type units. */
6502 if (ptr >= info_ptr + length
6503 || peek_abbrev_code (abfd, ptr) == 0
6504 || header.unit_type != DW_UT_type)
6505 {
6506 info_ptr += length;
6507 continue;
6508 }
6509
6510 if (types_htab == NULL)
6511 {
6512 if (dwo_file)
6513 types_htab = allocate_dwo_unit_table (objfile);
6514 else
6515 types_htab = allocate_signatured_type_table (objfile);
6516 }
6517
6518 if (dwo_file)
6519 {
6520 sig_type = NULL;
6521 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6522 struct dwo_unit);
6523 dwo_tu->dwo_file = dwo_file;
6524 dwo_tu->signature = header.signature;
6525 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6526 dwo_tu->section = section;
6527 dwo_tu->sect_off = sect_off;
6528 dwo_tu->length = length;
6529 }
6530 else
6531 {
6532 /* N.B.: type_offset is not usable if this type uses a DWO file.
6533 The real type_offset is in the DWO file. */
6534 dwo_tu = NULL;
6535 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6536 struct signatured_type);
6537 sig_type->signature = header.signature;
6538 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6539 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6540 sig_type->per_cu.is_debug_types = 1;
6541 sig_type->per_cu.section = section;
6542 sig_type->per_cu.sect_off = sect_off;
6543 sig_type->per_cu.length = length;
6544 }
6545
6546 slot = htab_find_slot (types_htab,
6547 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6548 INSERT);
6549 gdb_assert (slot != NULL);
6550 if (*slot != NULL)
6551 {
6552 sect_offset dup_sect_off;
6553
6554 if (dwo_file)
6555 {
6556 const struct dwo_unit *dup_tu
6557 = (const struct dwo_unit *) *slot;
6558
6559 dup_sect_off = dup_tu->sect_off;
6560 }
6561 else
6562 {
6563 const struct signatured_type *dup_tu
6564 = (const struct signatured_type *) *slot;
6565
6566 dup_sect_off = dup_tu->per_cu.sect_off;
6567 }
6568
6569 complaint (_("debug type entry at offset %s is duplicate to"
6570 " the entry at offset %s, signature %s"),
6571 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6572 hex_string (header.signature));
6573 }
6574 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6575
6576 if (dwarf_read_debug > 1)
6577 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6578 sect_offset_str (sect_off),
6579 hex_string (header.signature));
6580
6581 info_ptr += length;
6582 }
6583 }
6584
6585 /* Create the hash table of all entries in the .debug_types
6586 (or .debug_types.dwo) section(s).
6587 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6588 otherwise it is NULL.
6589
6590 The result is a pointer to the hash table or NULL if there are no types.
6591
6592 Note: This function processes DWO files only, not DWP files. */
6593
6594 static void
6595 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6596 struct dwo_file *dwo_file,
6597 gdb::array_view<dwarf2_section_info> type_sections,
6598 htab_t &types_htab)
6599 {
6600 for (dwarf2_section_info &section : type_sections)
6601 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6602 types_htab, rcuh_kind::TYPE);
6603 }
6604
6605 /* Create the hash table of all entries in the .debug_types section,
6606 and initialize all_type_units.
6607 The result is zero if there is an error (e.g. missing .debug_types section),
6608 otherwise non-zero. */
6609
6610 static int
6611 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6612 {
6613 htab_t types_htab = NULL;
6614
6615 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6616 &dwarf2_per_objfile->info, types_htab,
6617 rcuh_kind::COMPILE);
6618 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6619 dwarf2_per_objfile->types, types_htab);
6620 if (types_htab == NULL)
6621 {
6622 dwarf2_per_objfile->signatured_types = NULL;
6623 return 0;
6624 }
6625
6626 dwarf2_per_objfile->signatured_types = types_htab;
6627
6628 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6629 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6630
6631 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6632 &dwarf2_per_objfile->all_type_units);
6633
6634 return 1;
6635 }
6636
6637 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6638 If SLOT is non-NULL, it is the entry to use in the hash table.
6639 Otherwise we find one. */
6640
6641 static struct signatured_type *
6642 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6643 void **slot)
6644 {
6645 struct objfile *objfile = dwarf2_per_objfile->objfile;
6646
6647 if (dwarf2_per_objfile->all_type_units.size ()
6648 == dwarf2_per_objfile->all_type_units.capacity ())
6649 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6650
6651 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6652 struct signatured_type);
6653
6654 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6655 sig_type->signature = sig;
6656 sig_type->per_cu.is_debug_types = 1;
6657 if (dwarf2_per_objfile->using_index)
6658 {
6659 sig_type->per_cu.v.quick =
6660 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6661 struct dwarf2_per_cu_quick_data);
6662 }
6663
6664 if (slot == NULL)
6665 {
6666 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6667 sig_type, INSERT);
6668 }
6669 gdb_assert (*slot == NULL);
6670 *slot = sig_type;
6671 /* The rest of sig_type must be filled in by the caller. */
6672 return sig_type;
6673 }
6674
6675 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6676 Fill in SIG_ENTRY with DWO_ENTRY. */
6677
6678 static void
6679 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6680 struct signatured_type *sig_entry,
6681 struct dwo_unit *dwo_entry)
6682 {
6683 /* Make sure we're not clobbering something we don't expect to. */
6684 gdb_assert (! sig_entry->per_cu.queued);
6685 gdb_assert (sig_entry->per_cu.cu == NULL);
6686 if (dwarf2_per_objfile->using_index)
6687 {
6688 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6689 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6690 }
6691 else
6692 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6693 gdb_assert (sig_entry->signature == dwo_entry->signature);
6694 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6695 gdb_assert (sig_entry->type_unit_group == NULL);
6696 gdb_assert (sig_entry->dwo_unit == NULL);
6697
6698 sig_entry->per_cu.section = dwo_entry->section;
6699 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6700 sig_entry->per_cu.length = dwo_entry->length;
6701 sig_entry->per_cu.reading_dwo_directly = 1;
6702 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6703 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6704 sig_entry->dwo_unit = dwo_entry;
6705 }
6706
6707 /* Subroutine of lookup_signatured_type.
6708 If we haven't read the TU yet, create the signatured_type data structure
6709 for a TU to be read in directly from a DWO file, bypassing the stub.
6710 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6711 using .gdb_index, then when reading a CU we want to stay in the DWO file
6712 containing that CU. Otherwise we could end up reading several other DWO
6713 files (due to comdat folding) to process the transitive closure of all the
6714 mentioned TUs, and that can be slow. The current DWO file will have every
6715 type signature that it needs.
6716 We only do this for .gdb_index because in the psymtab case we already have
6717 to read all the DWOs to build the type unit groups. */
6718
6719 static struct signatured_type *
6720 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6721 {
6722 struct dwarf2_per_objfile *dwarf2_per_objfile
6723 = cu->per_cu->dwarf2_per_objfile;
6724 struct objfile *objfile = dwarf2_per_objfile->objfile;
6725 struct dwo_file *dwo_file;
6726 struct dwo_unit find_dwo_entry, *dwo_entry;
6727 struct signatured_type find_sig_entry, *sig_entry;
6728 void **slot;
6729
6730 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6731
6732 /* If TU skeletons have been removed then we may not have read in any
6733 TUs yet. */
6734 if (dwarf2_per_objfile->signatured_types == NULL)
6735 {
6736 dwarf2_per_objfile->signatured_types
6737 = allocate_signatured_type_table (objfile);
6738 }
6739
6740 /* We only ever need to read in one copy of a signatured type.
6741 Use the global signatured_types array to do our own comdat-folding
6742 of types. If this is the first time we're reading this TU, and
6743 the TU has an entry in .gdb_index, replace the recorded data from
6744 .gdb_index with this TU. */
6745
6746 find_sig_entry.signature = sig;
6747 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6748 &find_sig_entry, INSERT);
6749 sig_entry = (struct signatured_type *) *slot;
6750
6751 /* We can get here with the TU already read, *or* in the process of being
6752 read. Don't reassign the global entry to point to this DWO if that's
6753 the case. Also note that if the TU is already being read, it may not
6754 have come from a DWO, the program may be a mix of Fission-compiled
6755 code and non-Fission-compiled code. */
6756
6757 /* Have we already tried to read this TU?
6758 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6759 needn't exist in the global table yet). */
6760 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6761 return sig_entry;
6762
6763 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6764 dwo_unit of the TU itself. */
6765 dwo_file = cu->dwo_unit->dwo_file;
6766
6767 /* Ok, this is the first time we're reading this TU. */
6768 if (dwo_file->tus == NULL)
6769 return NULL;
6770 find_dwo_entry.signature = sig;
6771 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6772 if (dwo_entry == NULL)
6773 return NULL;
6774
6775 /* If the global table doesn't have an entry for this TU, add one. */
6776 if (sig_entry == NULL)
6777 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6778
6779 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6780 sig_entry->per_cu.tu_read = 1;
6781 return sig_entry;
6782 }
6783
6784 /* Subroutine of lookup_signatured_type.
6785 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6786 then try the DWP file. If the TU stub (skeleton) has been removed then
6787 it won't be in .gdb_index. */
6788
6789 static struct signatured_type *
6790 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6791 {
6792 struct dwarf2_per_objfile *dwarf2_per_objfile
6793 = cu->per_cu->dwarf2_per_objfile;
6794 struct objfile *objfile = dwarf2_per_objfile->objfile;
6795 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6796 struct dwo_unit *dwo_entry;
6797 struct signatured_type find_sig_entry, *sig_entry;
6798 void **slot;
6799
6800 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6801 gdb_assert (dwp_file != NULL);
6802
6803 /* If TU skeletons have been removed then we may not have read in any
6804 TUs yet. */
6805 if (dwarf2_per_objfile->signatured_types == NULL)
6806 {
6807 dwarf2_per_objfile->signatured_types
6808 = allocate_signatured_type_table (objfile);
6809 }
6810
6811 find_sig_entry.signature = sig;
6812 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6813 &find_sig_entry, INSERT);
6814 sig_entry = (struct signatured_type *) *slot;
6815
6816 /* Have we already tried to read this TU?
6817 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6818 needn't exist in the global table yet). */
6819 if (sig_entry != NULL)
6820 return sig_entry;
6821
6822 if (dwp_file->tus == NULL)
6823 return NULL;
6824 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6825 sig, 1 /* is_debug_types */);
6826 if (dwo_entry == NULL)
6827 return NULL;
6828
6829 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6830 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6831
6832 return sig_entry;
6833 }
6834
6835 /* Lookup a signature based type for DW_FORM_ref_sig8.
6836 Returns NULL if signature SIG is not present in the table.
6837 It is up to the caller to complain about this. */
6838
6839 static struct signatured_type *
6840 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6841 {
6842 struct dwarf2_per_objfile *dwarf2_per_objfile
6843 = cu->per_cu->dwarf2_per_objfile;
6844
6845 if (cu->dwo_unit
6846 && dwarf2_per_objfile->using_index)
6847 {
6848 /* We're in a DWO/DWP file, and we're using .gdb_index.
6849 These cases require special processing. */
6850 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6851 return lookup_dwo_signatured_type (cu, sig);
6852 else
6853 return lookup_dwp_signatured_type (cu, sig);
6854 }
6855 else
6856 {
6857 struct signatured_type find_entry, *entry;
6858
6859 if (dwarf2_per_objfile->signatured_types == NULL)
6860 return NULL;
6861 find_entry.signature = sig;
6862 entry = ((struct signatured_type *)
6863 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6864 return entry;
6865 }
6866 }
6867
6868 /* Return the address base of the compile unit, which, if exists, is stored
6869 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6870 static gdb::optional<ULONGEST>
6871 lookup_addr_base (struct die_info *comp_unit_die)
6872 {
6873 struct attribute *attr;
6874 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6875 if (attr == nullptr)
6876 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6877 if (attr == nullptr)
6878 return gdb::optional<ULONGEST> ();
6879 return DW_UNSND (attr);
6880 }
6881
6882 /* Return range lists base of the compile unit, which, if exists, is stored
6883 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6884 static ULONGEST
6885 lookup_ranges_base (struct die_info *comp_unit_die)
6886 {
6887 struct attribute *attr;
6888 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6889 if (attr == nullptr)
6890 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6891 if (attr == nullptr)
6892 return 0;
6893 return DW_UNSND (attr);
6894 }
6895
6896 /* Low level DIE reading support. */
6897
6898 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6899
6900 static void
6901 init_cu_die_reader (struct die_reader_specs *reader,
6902 struct dwarf2_cu *cu,
6903 struct dwarf2_section_info *section,
6904 struct dwo_file *dwo_file,
6905 struct abbrev_table *abbrev_table)
6906 {
6907 gdb_assert (section->readin && section->buffer != NULL);
6908 reader->abfd = section->get_bfd_owner ();
6909 reader->cu = cu;
6910 reader->dwo_file = dwo_file;
6911 reader->die_section = section;
6912 reader->buffer = section->buffer;
6913 reader->buffer_end = section->buffer + section->size;
6914 reader->abbrev_table = abbrev_table;
6915 }
6916
6917 /* Subroutine of cutu_reader to simplify it.
6918 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6919 There's just a lot of work to do, and cutu_reader is big enough
6920 already.
6921
6922 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6923 from it to the DIE in the DWO. If NULL we are skipping the stub.
6924 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6925 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6926 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6927 STUB_COMP_DIR may be non-NULL.
6928 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6929 are filled in with the info of the DIE from the DWO file.
6930 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6931 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6932 kept around for at least as long as *RESULT_READER.
6933
6934 The result is non-zero if a valid (non-dummy) DIE was found. */
6935
6936 static int
6937 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6938 struct dwo_unit *dwo_unit,
6939 struct die_info *stub_comp_unit_die,
6940 const char *stub_comp_dir,
6941 struct die_reader_specs *result_reader,
6942 const gdb_byte **result_info_ptr,
6943 struct die_info **result_comp_unit_die,
6944 int *result_has_children,
6945 abbrev_table_up *result_dwo_abbrev_table)
6946 {
6947 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6948 struct objfile *objfile = dwarf2_per_objfile->objfile;
6949 struct dwarf2_cu *cu = this_cu->cu;
6950 bfd *abfd;
6951 const gdb_byte *begin_info_ptr, *info_ptr;
6952 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6953 int i,num_extra_attrs;
6954 struct dwarf2_section_info *dwo_abbrev_section;
6955 struct die_info *comp_unit_die;
6956
6957 /* At most one of these may be provided. */
6958 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6959
6960 /* These attributes aren't processed until later:
6961 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6962 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6963 referenced later. However, these attributes are found in the stub
6964 which we won't have later. In order to not impose this complication
6965 on the rest of the code, we read them here and copy them to the
6966 DWO CU/TU die. */
6967
6968 stmt_list = NULL;
6969 low_pc = NULL;
6970 high_pc = NULL;
6971 ranges = NULL;
6972 comp_dir = NULL;
6973
6974 if (stub_comp_unit_die != NULL)
6975 {
6976 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6977 DWO file. */
6978 if (! this_cu->is_debug_types)
6979 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6980 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6981 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6982 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6983 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6984
6985 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6986
6987 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6988 here (if needed). We need the value before we can process
6989 DW_AT_ranges. */
6990 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6991 }
6992 else if (stub_comp_dir != NULL)
6993 {
6994 /* Reconstruct the comp_dir attribute to simplify the code below. */
6995 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6996 comp_dir->name = DW_AT_comp_dir;
6997 comp_dir->form = DW_FORM_string;
6998 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6999 DW_STRING (comp_dir) = stub_comp_dir;
7000 }
7001
7002 /* Set up for reading the DWO CU/TU. */
7003 cu->dwo_unit = dwo_unit;
7004 dwarf2_section_info *section = dwo_unit->section;
7005 section->read (objfile);
7006 abfd = section->get_bfd_owner ();
7007 begin_info_ptr = info_ptr = (section->buffer
7008 + to_underlying (dwo_unit->sect_off));
7009 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7010
7011 if (this_cu->is_debug_types)
7012 {
7013 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7014
7015 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7016 &cu->header, section,
7017 dwo_abbrev_section,
7018 info_ptr, rcuh_kind::TYPE);
7019 /* This is not an assert because it can be caused by bad debug info. */
7020 if (sig_type->signature != cu->header.signature)
7021 {
7022 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7023 " TU at offset %s [in module %s]"),
7024 hex_string (sig_type->signature),
7025 hex_string (cu->header.signature),
7026 sect_offset_str (dwo_unit->sect_off),
7027 bfd_get_filename (abfd));
7028 }
7029 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7030 /* For DWOs coming from DWP files, we don't know the CU length
7031 nor the type's offset in the TU until now. */
7032 dwo_unit->length = get_cu_length (&cu->header);
7033 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7034
7035 /* Establish the type offset that can be used to lookup the type.
7036 For DWO files, we don't know it until now. */
7037 sig_type->type_offset_in_section
7038 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7039 }
7040 else
7041 {
7042 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7043 &cu->header, section,
7044 dwo_abbrev_section,
7045 info_ptr, rcuh_kind::COMPILE);
7046 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7047 /* For DWOs coming from DWP files, we don't know the CU length
7048 until now. */
7049 dwo_unit->length = get_cu_length (&cu->header);
7050 }
7051
7052 *result_dwo_abbrev_table
7053 = abbrev_table_read_table (objfile, dwo_abbrev_section,
7054 cu->header.abbrev_sect_off);
7055 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7056 result_dwo_abbrev_table->get ());
7057
7058 /* Read in the die, but leave space to copy over the attributes
7059 from the stub. This has the benefit of simplifying the rest of
7060 the code - all the work to maintain the illusion of a single
7061 DW_TAG_{compile,type}_unit DIE is done here. */
7062 num_extra_attrs = ((stmt_list != NULL)
7063 + (low_pc != NULL)
7064 + (high_pc != NULL)
7065 + (ranges != NULL)
7066 + (comp_dir != NULL));
7067 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7068 result_has_children, num_extra_attrs);
7069
7070 /* Copy over the attributes from the stub to the DIE we just read in. */
7071 comp_unit_die = *result_comp_unit_die;
7072 i = comp_unit_die->num_attrs;
7073 if (stmt_list != NULL)
7074 comp_unit_die->attrs[i++] = *stmt_list;
7075 if (low_pc != NULL)
7076 comp_unit_die->attrs[i++] = *low_pc;
7077 if (high_pc != NULL)
7078 comp_unit_die->attrs[i++] = *high_pc;
7079 if (ranges != NULL)
7080 comp_unit_die->attrs[i++] = *ranges;
7081 if (comp_dir != NULL)
7082 comp_unit_die->attrs[i++] = *comp_dir;
7083 comp_unit_die->num_attrs += num_extra_attrs;
7084
7085 if (dwarf_die_debug)
7086 {
7087 fprintf_unfiltered (gdb_stdlog,
7088 "Read die from %s@0x%x of %s:\n",
7089 section->get_name (),
7090 (unsigned) (begin_info_ptr - section->buffer),
7091 bfd_get_filename (abfd));
7092 dump_die (comp_unit_die, dwarf_die_debug);
7093 }
7094
7095 /* Skip dummy compilation units. */
7096 if (info_ptr >= begin_info_ptr + dwo_unit->length
7097 || peek_abbrev_code (abfd, info_ptr) == 0)
7098 return 0;
7099
7100 *result_info_ptr = info_ptr;
7101 return 1;
7102 }
7103
7104 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7105 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7106 signature is part of the header. */
7107 static gdb::optional<ULONGEST>
7108 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7109 {
7110 if (cu->header.version >= 5)
7111 return cu->header.signature;
7112 struct attribute *attr;
7113 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7114 if (attr == nullptr)
7115 return gdb::optional<ULONGEST> ();
7116 return DW_UNSND (attr);
7117 }
7118
7119 /* Subroutine of cutu_reader to simplify it.
7120 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7121 Returns NULL if the specified DWO unit cannot be found. */
7122
7123 static struct dwo_unit *
7124 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7125 struct die_info *comp_unit_die,
7126 const char *dwo_name)
7127 {
7128 struct dwarf2_cu *cu = this_cu->cu;
7129 struct dwo_unit *dwo_unit;
7130 const char *comp_dir;
7131
7132 gdb_assert (cu != NULL);
7133
7134 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7135 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7136 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7137
7138 if (this_cu->is_debug_types)
7139 {
7140 struct signatured_type *sig_type;
7141
7142 /* Since this_cu is the first member of struct signatured_type,
7143 we can go from a pointer to one to a pointer to the other. */
7144 sig_type = (struct signatured_type *) this_cu;
7145 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7146 }
7147 else
7148 {
7149 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7150 if (!signature.has_value ())
7151 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7152 " [in module %s]"),
7153 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7154 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7155 *signature);
7156 }
7157
7158 return dwo_unit;
7159 }
7160
7161 /* Subroutine of cutu_reader to simplify it.
7162 See it for a description of the parameters.
7163 Read a TU directly from a DWO file, bypassing the stub. */
7164
7165 void
7166 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7167 int use_existing_cu, int keep)
7168 {
7169 struct signatured_type *sig_type;
7170 struct die_reader_specs reader;
7171
7172 /* Verify we can do the following downcast, and that we have the
7173 data we need. */
7174 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7175 sig_type = (struct signatured_type *) this_cu;
7176 gdb_assert (sig_type->dwo_unit != NULL);
7177
7178 if (use_existing_cu && this_cu->cu != NULL)
7179 {
7180 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7181 /* There's no need to do the rereading_dwo_cu handling that
7182 cutu_reader does since we don't read the stub. */
7183 }
7184 else
7185 {
7186 /* If !use_existing_cu, this_cu->cu must be NULL. */
7187 gdb_assert (this_cu->cu == NULL);
7188 m_new_cu.reset (new dwarf2_cu (this_cu));
7189 }
7190
7191 /* A future optimization, if needed, would be to use an existing
7192 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7193 could share abbrev tables. */
7194
7195 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7196 NULL /* stub_comp_unit_die */,
7197 sig_type->dwo_unit->dwo_file->comp_dir,
7198 &reader, &info_ptr,
7199 &comp_unit_die, &has_children,
7200 &m_dwo_abbrev_table) == 0)
7201 {
7202 /* Dummy die. */
7203 dummy_p = true;
7204 }
7205 }
7206
7207 /* Initialize a CU (or TU) and read its DIEs.
7208 If the CU defers to a DWO file, read the DWO file as well.
7209
7210 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7211 Otherwise the table specified in the comp unit header is read in and used.
7212 This is an optimization for when we already have the abbrev table.
7213
7214 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7215 Otherwise, a new CU is allocated with xmalloc.
7216
7217 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7218 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7219 end. */
7220
7221 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7222 struct abbrev_table *abbrev_table,
7223 int use_existing_cu, int keep,
7224 bool skip_partial)
7225 : die_reader_specs {},
7226 m_this_cu (this_cu),
7227 m_keep (keep)
7228 {
7229 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7230 struct objfile *objfile = dwarf2_per_objfile->objfile;
7231 struct dwarf2_section_info *section = this_cu->section;
7232 bfd *abfd = section->get_bfd_owner ();
7233 struct dwarf2_cu *cu;
7234 const gdb_byte *begin_info_ptr;
7235 struct signatured_type *sig_type = NULL;
7236 struct dwarf2_section_info *abbrev_section;
7237 /* Non-zero if CU currently points to a DWO file and we need to
7238 reread it. When this happens we need to reread the skeleton die
7239 before we can reread the DWO file (this only applies to CUs, not TUs). */
7240 int rereading_dwo_cu = 0;
7241
7242 if (dwarf_die_debug)
7243 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7244 this_cu->is_debug_types ? "type" : "comp",
7245 sect_offset_str (this_cu->sect_off));
7246
7247 if (use_existing_cu)
7248 gdb_assert (keep);
7249
7250 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7251 file (instead of going through the stub), short-circuit all of this. */
7252 if (this_cu->reading_dwo_directly)
7253 {
7254 /* Narrow down the scope of possibilities to have to understand. */
7255 gdb_assert (this_cu->is_debug_types);
7256 gdb_assert (abbrev_table == NULL);
7257 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7258 return;
7259 }
7260
7261 /* This is cheap if the section is already read in. */
7262 section->read (objfile);
7263
7264 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7265
7266 abbrev_section = get_abbrev_section_for_cu (this_cu);
7267
7268 if (use_existing_cu && this_cu->cu != NULL)
7269 {
7270 cu = this_cu->cu;
7271 /* If this CU is from a DWO file we need to start over, we need to
7272 refetch the attributes from the skeleton CU.
7273 This could be optimized by retrieving those attributes from when we
7274 were here the first time: the previous comp_unit_die was stored in
7275 comp_unit_obstack. But there's no data yet that we need this
7276 optimization. */
7277 if (cu->dwo_unit != NULL)
7278 rereading_dwo_cu = 1;
7279 }
7280 else
7281 {
7282 /* If !use_existing_cu, this_cu->cu must be NULL. */
7283 gdb_assert (this_cu->cu == NULL);
7284 m_new_cu.reset (new dwarf2_cu (this_cu));
7285 cu = m_new_cu.get ();
7286 }
7287
7288 /* Get the header. */
7289 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7290 {
7291 /* We already have the header, there's no need to read it in again. */
7292 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7293 }
7294 else
7295 {
7296 if (this_cu->is_debug_types)
7297 {
7298 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7299 &cu->header, section,
7300 abbrev_section, info_ptr,
7301 rcuh_kind::TYPE);
7302
7303 /* Since per_cu is the first member of struct signatured_type,
7304 we can go from a pointer to one to a pointer to the other. */
7305 sig_type = (struct signatured_type *) this_cu;
7306 gdb_assert (sig_type->signature == cu->header.signature);
7307 gdb_assert (sig_type->type_offset_in_tu
7308 == cu->header.type_cu_offset_in_tu);
7309 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7310
7311 /* LENGTH has not been set yet for type units if we're
7312 using .gdb_index. */
7313 this_cu->length = get_cu_length (&cu->header);
7314
7315 /* Establish the type offset that can be used to lookup the type. */
7316 sig_type->type_offset_in_section =
7317 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7318
7319 this_cu->dwarf_version = cu->header.version;
7320 }
7321 else
7322 {
7323 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7324 &cu->header, section,
7325 abbrev_section,
7326 info_ptr,
7327 rcuh_kind::COMPILE);
7328
7329 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7330 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7331 this_cu->dwarf_version = cu->header.version;
7332 }
7333 }
7334
7335 /* Skip dummy compilation units. */
7336 if (info_ptr >= begin_info_ptr + this_cu->length
7337 || peek_abbrev_code (abfd, info_ptr) == 0)
7338 {
7339 dummy_p = true;
7340 return;
7341 }
7342
7343 /* If we don't have them yet, read the abbrevs for this compilation unit.
7344 And if we need to read them now, make sure they're freed when we're
7345 done. */
7346 if (abbrev_table != NULL)
7347 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7348 else
7349 {
7350 m_abbrev_table_holder
7351 = abbrev_table_read_table (objfile, abbrev_section,
7352 cu->header.abbrev_sect_off);
7353 abbrev_table = m_abbrev_table_holder.get ();
7354 }
7355
7356 /* Read the top level CU/TU die. */
7357 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7358 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7359
7360 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7361 {
7362 dummy_p = true;
7363 return;
7364 }
7365
7366 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7367 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7368 table from the DWO file and pass the ownership over to us. It will be
7369 referenced from READER, so we must make sure to free it after we're done
7370 with READER.
7371
7372 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7373 DWO CU, that this test will fail (the attribute will not be present). */
7374 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7375 if (dwo_name != nullptr)
7376 {
7377 struct dwo_unit *dwo_unit;
7378 struct die_info *dwo_comp_unit_die;
7379
7380 if (has_children)
7381 {
7382 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7383 " has children (offset %s) [in module %s]"),
7384 sect_offset_str (this_cu->sect_off),
7385 bfd_get_filename (abfd));
7386 }
7387 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7388 if (dwo_unit != NULL)
7389 {
7390 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7391 comp_unit_die, NULL,
7392 this, &info_ptr,
7393 &dwo_comp_unit_die, &has_children,
7394 &m_dwo_abbrev_table) == 0)
7395 {
7396 /* Dummy die. */
7397 dummy_p = true;
7398 return;
7399 }
7400 comp_unit_die = dwo_comp_unit_die;
7401 }
7402 else
7403 {
7404 /* Yikes, we couldn't find the rest of the DIE, we only have
7405 the stub. A complaint has already been logged. There's
7406 not much more we can do except pass on the stub DIE to
7407 die_reader_func. We don't want to throw an error on bad
7408 debug info. */
7409 }
7410 }
7411 }
7412
7413 cutu_reader::~cutu_reader ()
7414 {
7415 /* Done, clean up. */
7416 if (m_new_cu != NULL && m_keep && !dummy_p)
7417 {
7418 struct dwarf2_per_objfile *dwarf2_per_objfile
7419 = m_this_cu->dwarf2_per_objfile;
7420 /* Link this CU into read_in_chain. */
7421 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7422 dwarf2_per_objfile->read_in_chain = m_this_cu;
7423 /* The chain owns it now. */
7424 m_new_cu.release ();
7425 }
7426 }
7427
7428 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7429 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7430 assumed to have already done the lookup to find the DWO file).
7431
7432 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7433 THIS_CU->is_debug_types, but nothing else.
7434
7435 We fill in THIS_CU->length.
7436
7437 THIS_CU->cu is always freed when done.
7438 This is done in order to not leave THIS_CU->cu in a state where we have
7439 to care whether it refers to the "main" CU or the DWO CU.
7440
7441 When parent_cu is passed, it is used to provide a default value for
7442 str_offsets_base and addr_base from the parent. */
7443
7444 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7445 struct dwarf2_cu *parent_cu,
7446 struct dwo_file *dwo_file)
7447 : die_reader_specs {},
7448 m_this_cu (this_cu)
7449 {
7450 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7451 struct objfile *objfile = dwarf2_per_objfile->objfile;
7452 struct dwarf2_section_info *section = this_cu->section;
7453 bfd *abfd = section->get_bfd_owner ();
7454 struct dwarf2_section_info *abbrev_section;
7455 const gdb_byte *begin_info_ptr, *info_ptr;
7456 int has_children;
7457
7458 if (dwarf_die_debug)
7459 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7460 this_cu->is_debug_types ? "type" : "comp",
7461 sect_offset_str (this_cu->sect_off));
7462
7463 gdb_assert (this_cu->cu == NULL);
7464
7465 abbrev_section = (dwo_file != NULL
7466 ? &dwo_file->sections.abbrev
7467 : get_abbrev_section_for_cu (this_cu));
7468
7469 /* This is cheap if the section is already read in. */
7470 section->read (objfile);
7471
7472 m_new_cu.reset (new dwarf2_cu (this_cu));
7473
7474 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7475 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7476 &m_new_cu->header, section,
7477 abbrev_section, info_ptr,
7478 (this_cu->is_debug_types
7479 ? rcuh_kind::TYPE
7480 : rcuh_kind::COMPILE));
7481
7482 if (parent_cu != nullptr)
7483 {
7484 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7485 m_new_cu->addr_base = parent_cu->addr_base;
7486 }
7487 this_cu->length = get_cu_length (&m_new_cu->header);
7488
7489 /* Skip dummy compilation units. */
7490 if (info_ptr >= begin_info_ptr + this_cu->length
7491 || peek_abbrev_code (abfd, info_ptr) == 0)
7492 {
7493 dummy_p = true;
7494 return;
7495 }
7496
7497 m_abbrev_table_holder
7498 = abbrev_table_read_table (objfile, abbrev_section,
7499 m_new_cu->header.abbrev_sect_off);
7500
7501 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7502 m_abbrev_table_holder.get ());
7503 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7504 }
7505
7506 \f
7507 /* Type Unit Groups.
7508
7509 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7510 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7511 so that all types coming from the same compilation (.o file) are grouped
7512 together. A future step could be to put the types in the same symtab as
7513 the CU the types ultimately came from. */
7514
7515 static hashval_t
7516 hash_type_unit_group (const void *item)
7517 {
7518 const struct type_unit_group *tu_group
7519 = (const struct type_unit_group *) item;
7520
7521 return hash_stmt_list_entry (&tu_group->hash);
7522 }
7523
7524 static int
7525 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7526 {
7527 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7528 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7529
7530 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7531 }
7532
7533 /* Allocate a hash table for type unit groups. */
7534
7535 static htab_t
7536 allocate_type_unit_groups_table (struct objfile *objfile)
7537 {
7538 return htab_create_alloc_ex (3,
7539 hash_type_unit_group,
7540 eq_type_unit_group,
7541 NULL,
7542 &objfile->objfile_obstack,
7543 hashtab_obstack_allocate,
7544 dummy_obstack_deallocate);
7545 }
7546
7547 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7548 partial symtabs. We combine several TUs per psymtab to not let the size
7549 of any one psymtab grow too big. */
7550 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7551 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7552
7553 /* Helper routine for get_type_unit_group.
7554 Create the type_unit_group object used to hold one or more TUs. */
7555
7556 static struct type_unit_group *
7557 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7558 {
7559 struct dwarf2_per_objfile *dwarf2_per_objfile
7560 = cu->per_cu->dwarf2_per_objfile;
7561 struct objfile *objfile = dwarf2_per_objfile->objfile;
7562 struct dwarf2_per_cu_data *per_cu;
7563 struct type_unit_group *tu_group;
7564
7565 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7566 struct type_unit_group);
7567 per_cu = &tu_group->per_cu;
7568 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7569
7570 if (dwarf2_per_objfile->using_index)
7571 {
7572 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7573 struct dwarf2_per_cu_quick_data);
7574 }
7575 else
7576 {
7577 unsigned int line_offset = to_underlying (line_offset_struct);
7578 dwarf2_psymtab *pst;
7579 std::string name;
7580
7581 /* Give the symtab a useful name for debug purposes. */
7582 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7583 name = string_printf ("<type_units_%d>",
7584 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7585 else
7586 name = string_printf ("<type_units_at_0x%x>", line_offset);
7587
7588 pst = create_partial_symtab (per_cu, name.c_str ());
7589 pst->anonymous = true;
7590 }
7591
7592 tu_group->hash.dwo_unit = cu->dwo_unit;
7593 tu_group->hash.line_sect_off = line_offset_struct;
7594
7595 return tu_group;
7596 }
7597
7598 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7599 STMT_LIST is a DW_AT_stmt_list attribute. */
7600
7601 static struct type_unit_group *
7602 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7603 {
7604 struct dwarf2_per_objfile *dwarf2_per_objfile
7605 = cu->per_cu->dwarf2_per_objfile;
7606 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7607 struct type_unit_group *tu_group;
7608 void **slot;
7609 unsigned int line_offset;
7610 struct type_unit_group type_unit_group_for_lookup;
7611
7612 if (dwarf2_per_objfile->type_unit_groups == NULL)
7613 {
7614 dwarf2_per_objfile->type_unit_groups =
7615 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7616 }
7617
7618 /* Do we need to create a new group, or can we use an existing one? */
7619
7620 if (stmt_list)
7621 {
7622 line_offset = DW_UNSND (stmt_list);
7623 ++tu_stats->nr_symtab_sharers;
7624 }
7625 else
7626 {
7627 /* Ugh, no stmt_list. Rare, but we have to handle it.
7628 We can do various things here like create one group per TU or
7629 spread them over multiple groups to split up the expansion work.
7630 To avoid worst case scenarios (too many groups or too large groups)
7631 we, umm, group them in bunches. */
7632 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7633 | (tu_stats->nr_stmt_less_type_units
7634 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7635 ++tu_stats->nr_stmt_less_type_units;
7636 }
7637
7638 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7639 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7640 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7641 &type_unit_group_for_lookup, INSERT);
7642 if (*slot != NULL)
7643 {
7644 tu_group = (struct type_unit_group *) *slot;
7645 gdb_assert (tu_group != NULL);
7646 }
7647 else
7648 {
7649 sect_offset line_offset_struct = (sect_offset) line_offset;
7650 tu_group = create_type_unit_group (cu, line_offset_struct);
7651 *slot = tu_group;
7652 ++tu_stats->nr_symtabs;
7653 }
7654
7655 return tu_group;
7656 }
7657 \f
7658 /* Partial symbol tables. */
7659
7660 /* Create a psymtab named NAME and assign it to PER_CU.
7661
7662 The caller must fill in the following details:
7663 dirname, textlow, texthigh. */
7664
7665 static dwarf2_psymtab *
7666 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7667 {
7668 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7669 dwarf2_psymtab *pst;
7670
7671 pst = new dwarf2_psymtab (name, objfile, 0);
7672
7673 pst->psymtabs_addrmap_supported = true;
7674
7675 /* This is the glue that links PST into GDB's symbol API. */
7676 pst->per_cu_data = per_cu;
7677 per_cu->v.psymtab = pst;
7678
7679 return pst;
7680 }
7681
7682 /* DIE reader function for process_psymtab_comp_unit. */
7683
7684 static void
7685 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7686 const gdb_byte *info_ptr,
7687 struct die_info *comp_unit_die,
7688 int has_children,
7689 int want_partial_unit,
7690 enum language pretend_language)
7691 {
7692 struct dwarf2_cu *cu = reader->cu;
7693 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7694 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7695 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7696 CORE_ADDR baseaddr;
7697 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7698 dwarf2_psymtab *pst;
7699 enum pc_bounds_kind cu_bounds_kind;
7700 const char *filename;
7701
7702 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7703 return;
7704
7705 gdb_assert (! per_cu->is_debug_types);
7706
7707 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7708
7709 /* Allocate a new partial symbol table structure. */
7710 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7711 if (filename == NULL)
7712 filename = "";
7713
7714 pst = create_partial_symtab (per_cu, filename);
7715
7716 /* This must be done before calling dwarf2_build_include_psymtabs. */
7717 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7718
7719 baseaddr = objfile->text_section_offset ();
7720
7721 dwarf2_find_base_address (comp_unit_die, cu);
7722
7723 /* Possibly set the default values of LOWPC and HIGHPC from
7724 `DW_AT_ranges'. */
7725 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7726 &best_highpc, cu, pst);
7727 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7728 {
7729 CORE_ADDR low
7730 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7731 - baseaddr);
7732 CORE_ADDR high
7733 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7734 - baseaddr - 1);
7735 /* Store the contiguous range if it is not empty; it can be
7736 empty for CUs with no code. */
7737 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7738 low, high, pst);
7739 }
7740
7741 /* Check if comp unit has_children.
7742 If so, read the rest of the partial symbols from this comp unit.
7743 If not, there's no more debug_info for this comp unit. */
7744 if (has_children)
7745 {
7746 struct partial_die_info *first_die;
7747 CORE_ADDR lowpc, highpc;
7748
7749 lowpc = ((CORE_ADDR) -1);
7750 highpc = ((CORE_ADDR) 0);
7751
7752 first_die = load_partial_dies (reader, info_ptr, 1);
7753
7754 scan_partial_symbols (first_die, &lowpc, &highpc,
7755 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7756
7757 /* If we didn't find a lowpc, set it to highpc to avoid
7758 complaints from `maint check'. */
7759 if (lowpc == ((CORE_ADDR) -1))
7760 lowpc = highpc;
7761
7762 /* If the compilation unit didn't have an explicit address range,
7763 then use the information extracted from its child dies. */
7764 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7765 {
7766 best_lowpc = lowpc;
7767 best_highpc = highpc;
7768 }
7769 }
7770 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7771 best_lowpc + baseaddr)
7772 - baseaddr);
7773 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7774 best_highpc + baseaddr)
7775 - baseaddr);
7776
7777 end_psymtab_common (objfile, pst);
7778
7779 if (!cu->per_cu->imported_symtabs_empty ())
7780 {
7781 int i;
7782 int len = cu->per_cu->imported_symtabs_size ();
7783
7784 /* Fill in 'dependencies' here; we fill in 'users' in a
7785 post-pass. */
7786 pst->number_of_dependencies = len;
7787 pst->dependencies
7788 = objfile->partial_symtabs->allocate_dependencies (len);
7789 for (i = 0; i < len; ++i)
7790 {
7791 pst->dependencies[i]
7792 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7793 }
7794
7795 cu->per_cu->imported_symtabs_free ();
7796 }
7797
7798 /* Get the list of files included in the current compilation unit,
7799 and build a psymtab for each of them. */
7800 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7801
7802 if (dwarf_read_debug)
7803 fprintf_unfiltered (gdb_stdlog,
7804 "Psymtab for %s unit @%s: %s - %s"
7805 ", %d global, %d static syms\n",
7806 per_cu->is_debug_types ? "type" : "comp",
7807 sect_offset_str (per_cu->sect_off),
7808 paddress (gdbarch, pst->text_low (objfile)),
7809 paddress (gdbarch, pst->text_high (objfile)),
7810 pst->n_global_syms, pst->n_static_syms);
7811 }
7812
7813 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7814 Process compilation unit THIS_CU for a psymtab. */
7815
7816 static void
7817 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7818 int want_partial_unit,
7819 enum language pretend_language)
7820 {
7821 /* If this compilation unit was already read in, free the
7822 cached copy in order to read it in again. This is
7823 necessary because we skipped some symbols when we first
7824 read in the compilation unit (see load_partial_dies).
7825 This problem could be avoided, but the benefit is unclear. */
7826 if (this_cu->cu != NULL)
7827 free_one_cached_comp_unit (this_cu);
7828
7829 cutu_reader reader (this_cu, NULL, 0, 0, false);
7830
7831 if (reader.dummy_p)
7832 {
7833 /* Nothing. */
7834 }
7835 else if (this_cu->is_debug_types)
7836 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7837 reader.has_children);
7838 else
7839 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7840 reader.comp_unit_die,
7841 reader.has_children,
7842 want_partial_unit,
7843 pretend_language);
7844
7845 /* Age out any secondary CUs. */
7846 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7847 }
7848
7849 /* Reader function for build_type_psymtabs. */
7850
7851 static void
7852 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7853 const gdb_byte *info_ptr,
7854 struct die_info *type_unit_die,
7855 int has_children)
7856 {
7857 struct dwarf2_per_objfile *dwarf2_per_objfile
7858 = reader->cu->per_cu->dwarf2_per_objfile;
7859 struct objfile *objfile = dwarf2_per_objfile->objfile;
7860 struct dwarf2_cu *cu = reader->cu;
7861 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7862 struct signatured_type *sig_type;
7863 struct type_unit_group *tu_group;
7864 struct attribute *attr;
7865 struct partial_die_info *first_die;
7866 CORE_ADDR lowpc, highpc;
7867 dwarf2_psymtab *pst;
7868
7869 gdb_assert (per_cu->is_debug_types);
7870 sig_type = (struct signatured_type *) per_cu;
7871
7872 if (! has_children)
7873 return;
7874
7875 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7876 tu_group = get_type_unit_group (cu, attr);
7877
7878 if (tu_group->tus == nullptr)
7879 tu_group->tus = new std::vector<signatured_type *>;
7880 tu_group->tus->push_back (sig_type);
7881
7882 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7883 pst = create_partial_symtab (per_cu, "");
7884 pst->anonymous = true;
7885
7886 first_die = load_partial_dies (reader, info_ptr, 1);
7887
7888 lowpc = (CORE_ADDR) -1;
7889 highpc = (CORE_ADDR) 0;
7890 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7891
7892 end_psymtab_common (objfile, pst);
7893 }
7894
7895 /* Struct used to sort TUs by their abbreviation table offset. */
7896
7897 struct tu_abbrev_offset
7898 {
7899 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7900 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7901 {}
7902
7903 signatured_type *sig_type;
7904 sect_offset abbrev_offset;
7905 };
7906
7907 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7908
7909 static bool
7910 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7911 const struct tu_abbrev_offset &b)
7912 {
7913 return a.abbrev_offset < b.abbrev_offset;
7914 }
7915
7916 /* Efficiently read all the type units.
7917 This does the bulk of the work for build_type_psymtabs.
7918
7919 The efficiency is because we sort TUs by the abbrev table they use and
7920 only read each abbrev table once. In one program there are 200K TUs
7921 sharing 8K abbrev tables.
7922
7923 The main purpose of this function is to support building the
7924 dwarf2_per_objfile->type_unit_groups table.
7925 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7926 can collapse the search space by grouping them by stmt_list.
7927 The savings can be significant, in the same program from above the 200K TUs
7928 share 8K stmt_list tables.
7929
7930 FUNC is expected to call get_type_unit_group, which will create the
7931 struct type_unit_group if necessary and add it to
7932 dwarf2_per_objfile->type_unit_groups. */
7933
7934 static void
7935 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7936 {
7937 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7938 abbrev_table_up abbrev_table;
7939 sect_offset abbrev_offset;
7940
7941 /* It's up to the caller to not call us multiple times. */
7942 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7943
7944 if (dwarf2_per_objfile->all_type_units.empty ())
7945 return;
7946
7947 /* TUs typically share abbrev tables, and there can be way more TUs than
7948 abbrev tables. Sort by abbrev table to reduce the number of times we
7949 read each abbrev table in.
7950 Alternatives are to punt or to maintain a cache of abbrev tables.
7951 This is simpler and efficient enough for now.
7952
7953 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7954 symtab to use). Typically TUs with the same abbrev offset have the same
7955 stmt_list value too so in practice this should work well.
7956
7957 The basic algorithm here is:
7958
7959 sort TUs by abbrev table
7960 for each TU with same abbrev table:
7961 read abbrev table if first user
7962 read TU top level DIE
7963 [IWBN if DWO skeletons had DW_AT_stmt_list]
7964 call FUNC */
7965
7966 if (dwarf_read_debug)
7967 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7968
7969 /* Sort in a separate table to maintain the order of all_type_units
7970 for .gdb_index: TU indices directly index all_type_units. */
7971 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7972 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7973
7974 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7975 sorted_by_abbrev.emplace_back
7976 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7977 sig_type->per_cu.section,
7978 sig_type->per_cu.sect_off));
7979
7980 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7981 sort_tu_by_abbrev_offset);
7982
7983 abbrev_offset = (sect_offset) ~(unsigned) 0;
7984
7985 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7986 {
7987 /* Switch to the next abbrev table if necessary. */
7988 if (abbrev_table == NULL
7989 || tu.abbrev_offset != abbrev_offset)
7990 {
7991 abbrev_offset = tu.abbrev_offset;
7992 abbrev_table =
7993 abbrev_table_read_table (dwarf2_per_objfile->objfile,
7994 &dwarf2_per_objfile->abbrev,
7995 abbrev_offset);
7996 ++tu_stats->nr_uniq_abbrev_tables;
7997 }
7998
7999 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8000 0, 0, false);
8001 if (!reader.dummy_p)
8002 build_type_psymtabs_reader (&reader, reader.info_ptr,
8003 reader.comp_unit_die,
8004 reader.has_children);
8005 }
8006 }
8007
8008 /* Print collected type unit statistics. */
8009
8010 static void
8011 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8012 {
8013 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8014
8015 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8016 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8017 dwarf2_per_objfile->all_type_units.size ());
8018 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8019 tu_stats->nr_uniq_abbrev_tables);
8020 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8021 tu_stats->nr_symtabs);
8022 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8023 tu_stats->nr_symtab_sharers);
8024 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8025 tu_stats->nr_stmt_less_type_units);
8026 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8027 tu_stats->nr_all_type_units_reallocs);
8028 }
8029
8030 /* Traversal function for build_type_psymtabs. */
8031
8032 static int
8033 build_type_psymtab_dependencies (void **slot, void *info)
8034 {
8035 struct dwarf2_per_objfile *dwarf2_per_objfile
8036 = (struct dwarf2_per_objfile *) info;
8037 struct objfile *objfile = dwarf2_per_objfile->objfile;
8038 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8039 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8040 dwarf2_psymtab *pst = per_cu->v.psymtab;
8041 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8042 int i;
8043
8044 gdb_assert (len > 0);
8045 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8046
8047 pst->number_of_dependencies = len;
8048 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8049 for (i = 0; i < len; ++i)
8050 {
8051 struct signatured_type *iter = tu_group->tus->at (i);
8052 gdb_assert (iter->per_cu.is_debug_types);
8053 pst->dependencies[i] = iter->per_cu.v.psymtab;
8054 iter->type_unit_group = tu_group;
8055 }
8056
8057 delete tu_group->tus;
8058 tu_group->tus = nullptr;
8059
8060 return 1;
8061 }
8062
8063 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8064 Build partial symbol tables for the .debug_types comp-units. */
8065
8066 static void
8067 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8068 {
8069 if (! create_all_type_units (dwarf2_per_objfile))
8070 return;
8071
8072 build_type_psymtabs_1 (dwarf2_per_objfile);
8073 }
8074
8075 /* Traversal function for process_skeletonless_type_unit.
8076 Read a TU in a DWO file and build partial symbols for it. */
8077
8078 static int
8079 process_skeletonless_type_unit (void **slot, void *info)
8080 {
8081 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8082 struct dwarf2_per_objfile *dwarf2_per_objfile
8083 = (struct dwarf2_per_objfile *) info;
8084 struct signatured_type find_entry, *entry;
8085
8086 /* If this TU doesn't exist in the global table, add it and read it in. */
8087
8088 if (dwarf2_per_objfile->signatured_types == NULL)
8089 {
8090 dwarf2_per_objfile->signatured_types
8091 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8092 }
8093
8094 find_entry.signature = dwo_unit->signature;
8095 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8096 INSERT);
8097 /* If we've already seen this type there's nothing to do. What's happening
8098 is we're doing our own version of comdat-folding here. */
8099 if (*slot != NULL)
8100 return 1;
8101
8102 /* This does the job that create_all_type_units would have done for
8103 this TU. */
8104 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8105 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8106 *slot = entry;
8107
8108 /* This does the job that build_type_psymtabs_1 would have done. */
8109 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8110 if (!reader.dummy_p)
8111 build_type_psymtabs_reader (&reader, reader.info_ptr,
8112 reader.comp_unit_die, reader.has_children);
8113
8114 return 1;
8115 }
8116
8117 /* Traversal function for process_skeletonless_type_units. */
8118
8119 static int
8120 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8121 {
8122 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8123
8124 if (dwo_file->tus != NULL)
8125 {
8126 htab_traverse_noresize (dwo_file->tus,
8127 process_skeletonless_type_unit, info);
8128 }
8129
8130 return 1;
8131 }
8132
8133 /* Scan all TUs of DWO files, verifying we've processed them.
8134 This is needed in case a TU was emitted without its skeleton.
8135 Note: This can't be done until we know what all the DWO files are. */
8136
8137 static void
8138 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8139 {
8140 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8141 if (get_dwp_file (dwarf2_per_objfile) == NULL
8142 && dwarf2_per_objfile->dwo_files != NULL)
8143 {
8144 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8145 process_dwo_file_for_skeletonless_type_units,
8146 dwarf2_per_objfile);
8147 }
8148 }
8149
8150 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8151
8152 static void
8153 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8154 {
8155 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8156 {
8157 dwarf2_psymtab *pst = per_cu->v.psymtab;
8158
8159 if (pst == NULL)
8160 continue;
8161
8162 for (int j = 0; j < pst->number_of_dependencies; ++j)
8163 {
8164 /* Set the 'user' field only if it is not already set. */
8165 if (pst->dependencies[j]->user == NULL)
8166 pst->dependencies[j]->user = pst;
8167 }
8168 }
8169 }
8170
8171 /* Build the partial symbol table by doing a quick pass through the
8172 .debug_info and .debug_abbrev sections. */
8173
8174 static void
8175 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8176 {
8177 struct objfile *objfile = dwarf2_per_objfile->objfile;
8178
8179 if (dwarf_read_debug)
8180 {
8181 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8182 objfile_name (objfile));
8183 }
8184
8185 dwarf2_per_objfile->reading_partial_symbols = 1;
8186
8187 dwarf2_per_objfile->info.read (objfile);
8188
8189 /* Any cached compilation units will be linked by the per-objfile
8190 read_in_chain. Make sure to free them when we're done. */
8191 free_cached_comp_units freer (dwarf2_per_objfile);
8192
8193 build_type_psymtabs (dwarf2_per_objfile);
8194
8195 create_all_comp_units (dwarf2_per_objfile);
8196
8197 /* Create a temporary address map on a temporary obstack. We later
8198 copy this to the final obstack. */
8199 auto_obstack temp_obstack;
8200
8201 scoped_restore save_psymtabs_addrmap
8202 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8203 addrmap_create_mutable (&temp_obstack));
8204
8205 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8206 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8207
8208 /* This has to wait until we read the CUs, we need the list of DWOs. */
8209 process_skeletonless_type_units (dwarf2_per_objfile);
8210
8211 /* Now that all TUs have been processed we can fill in the dependencies. */
8212 if (dwarf2_per_objfile->type_unit_groups != NULL)
8213 {
8214 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8215 build_type_psymtab_dependencies, dwarf2_per_objfile);
8216 }
8217
8218 if (dwarf_read_debug)
8219 print_tu_stats (dwarf2_per_objfile);
8220
8221 set_partial_user (dwarf2_per_objfile);
8222
8223 objfile->partial_symtabs->psymtabs_addrmap
8224 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8225 objfile->partial_symtabs->obstack ());
8226 /* At this point we want to keep the address map. */
8227 save_psymtabs_addrmap.release ();
8228
8229 if (dwarf_read_debug)
8230 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8231 objfile_name (objfile));
8232 }
8233
8234 /* Load the partial DIEs for a secondary CU into memory.
8235 This is also used when rereading a primary CU with load_all_dies. */
8236
8237 static void
8238 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8239 {
8240 cutu_reader reader (this_cu, NULL, 1, 1, false);
8241
8242 if (!reader.dummy_p)
8243 {
8244 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8245 language_minimal);
8246
8247 /* Check if comp unit has_children.
8248 If so, read the rest of the partial symbols from this comp unit.
8249 If not, there's no more debug_info for this comp unit. */
8250 if (reader.has_children)
8251 load_partial_dies (&reader, reader.info_ptr, 0);
8252 }
8253 }
8254
8255 static void
8256 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8257 struct dwarf2_section_info *section,
8258 struct dwarf2_section_info *abbrev_section,
8259 unsigned int is_dwz)
8260 {
8261 const gdb_byte *info_ptr;
8262 struct objfile *objfile = dwarf2_per_objfile->objfile;
8263
8264 if (dwarf_read_debug)
8265 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8266 section->get_name (),
8267 section->get_file_name ());
8268
8269 section->read (objfile);
8270
8271 info_ptr = section->buffer;
8272
8273 while (info_ptr < section->buffer + section->size)
8274 {
8275 struct dwarf2_per_cu_data *this_cu;
8276
8277 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8278
8279 comp_unit_head cu_header;
8280 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8281 abbrev_section, info_ptr,
8282 rcuh_kind::COMPILE);
8283
8284 /* Save the compilation unit for later lookup. */
8285 if (cu_header.unit_type != DW_UT_type)
8286 {
8287 this_cu = XOBNEW (&objfile->objfile_obstack,
8288 struct dwarf2_per_cu_data);
8289 memset (this_cu, 0, sizeof (*this_cu));
8290 }
8291 else
8292 {
8293 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8294 struct signatured_type);
8295 memset (sig_type, 0, sizeof (*sig_type));
8296 sig_type->signature = cu_header.signature;
8297 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8298 this_cu = &sig_type->per_cu;
8299 }
8300 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8301 this_cu->sect_off = sect_off;
8302 this_cu->length = cu_header.length + cu_header.initial_length_size;
8303 this_cu->is_dwz = is_dwz;
8304 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8305 this_cu->section = section;
8306
8307 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8308
8309 info_ptr = info_ptr + this_cu->length;
8310 }
8311 }
8312
8313 /* Create a list of all compilation units in OBJFILE.
8314 This is only done for -readnow and building partial symtabs. */
8315
8316 static void
8317 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8318 {
8319 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8320 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8321 &dwarf2_per_objfile->abbrev, 0);
8322
8323 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8324 if (dwz != NULL)
8325 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8326 1);
8327 }
8328
8329 /* Process all loaded DIEs for compilation unit CU, starting at
8330 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8331 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8332 DW_AT_ranges). See the comments of add_partial_subprogram on how
8333 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8334
8335 static void
8336 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8337 CORE_ADDR *highpc, int set_addrmap,
8338 struct dwarf2_cu *cu)
8339 {
8340 struct partial_die_info *pdi;
8341
8342 /* Now, march along the PDI's, descending into ones which have
8343 interesting children but skipping the children of the other ones,
8344 until we reach the end of the compilation unit. */
8345
8346 pdi = first_die;
8347
8348 while (pdi != NULL)
8349 {
8350 pdi->fixup (cu);
8351
8352 /* Anonymous namespaces or modules have no name but have interesting
8353 children, so we need to look at them. Ditto for anonymous
8354 enums. */
8355
8356 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8357 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8358 || pdi->tag == DW_TAG_imported_unit
8359 || pdi->tag == DW_TAG_inlined_subroutine)
8360 {
8361 switch (pdi->tag)
8362 {
8363 case DW_TAG_subprogram:
8364 case DW_TAG_inlined_subroutine:
8365 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8366 break;
8367 case DW_TAG_constant:
8368 case DW_TAG_variable:
8369 case DW_TAG_typedef:
8370 case DW_TAG_union_type:
8371 if (!pdi->is_declaration)
8372 {
8373 add_partial_symbol (pdi, cu);
8374 }
8375 break;
8376 case DW_TAG_class_type:
8377 case DW_TAG_interface_type:
8378 case DW_TAG_structure_type:
8379 if (!pdi->is_declaration)
8380 {
8381 add_partial_symbol (pdi, cu);
8382 }
8383 if ((cu->language == language_rust
8384 || cu->language == language_cplus) && pdi->has_children)
8385 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8386 set_addrmap, cu);
8387 break;
8388 case DW_TAG_enumeration_type:
8389 if (!pdi->is_declaration)
8390 add_partial_enumeration (pdi, cu);
8391 break;
8392 case DW_TAG_base_type:
8393 case DW_TAG_subrange_type:
8394 /* File scope base type definitions are added to the partial
8395 symbol table. */
8396 add_partial_symbol (pdi, cu);
8397 break;
8398 case DW_TAG_namespace:
8399 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8400 break;
8401 case DW_TAG_module:
8402 if (!pdi->is_declaration)
8403 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8404 break;
8405 case DW_TAG_imported_unit:
8406 {
8407 struct dwarf2_per_cu_data *per_cu;
8408
8409 /* For now we don't handle imported units in type units. */
8410 if (cu->per_cu->is_debug_types)
8411 {
8412 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8413 " supported in type units [in module %s]"),
8414 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8415 }
8416
8417 per_cu = dwarf2_find_containing_comp_unit
8418 (pdi->d.sect_off, pdi->is_dwz,
8419 cu->per_cu->dwarf2_per_objfile);
8420
8421 /* Go read the partial unit, if needed. */
8422 if (per_cu->v.psymtab == NULL)
8423 process_psymtab_comp_unit (per_cu, 1, cu->language);
8424
8425 cu->per_cu->imported_symtabs_push (per_cu);
8426 }
8427 break;
8428 case DW_TAG_imported_declaration:
8429 add_partial_symbol (pdi, cu);
8430 break;
8431 default:
8432 break;
8433 }
8434 }
8435
8436 /* If the die has a sibling, skip to the sibling. */
8437
8438 pdi = pdi->die_sibling;
8439 }
8440 }
8441
8442 /* Functions used to compute the fully scoped name of a partial DIE.
8443
8444 Normally, this is simple. For C++, the parent DIE's fully scoped
8445 name is concatenated with "::" and the partial DIE's name.
8446 Enumerators are an exception; they use the scope of their parent
8447 enumeration type, i.e. the name of the enumeration type is not
8448 prepended to the enumerator.
8449
8450 There are two complexities. One is DW_AT_specification; in this
8451 case "parent" means the parent of the target of the specification,
8452 instead of the direct parent of the DIE. The other is compilers
8453 which do not emit DW_TAG_namespace; in this case we try to guess
8454 the fully qualified name of structure types from their members'
8455 linkage names. This must be done using the DIE's children rather
8456 than the children of any DW_AT_specification target. We only need
8457 to do this for structures at the top level, i.e. if the target of
8458 any DW_AT_specification (if any; otherwise the DIE itself) does not
8459 have a parent. */
8460
8461 /* Compute the scope prefix associated with PDI's parent, in
8462 compilation unit CU. The result will be allocated on CU's
8463 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8464 field. NULL is returned if no prefix is necessary. */
8465 static const char *
8466 partial_die_parent_scope (struct partial_die_info *pdi,
8467 struct dwarf2_cu *cu)
8468 {
8469 const char *grandparent_scope;
8470 struct partial_die_info *parent, *real_pdi;
8471
8472 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8473 then this means the parent of the specification DIE. */
8474
8475 real_pdi = pdi;
8476 while (real_pdi->has_specification)
8477 {
8478 auto res = find_partial_die (real_pdi->spec_offset,
8479 real_pdi->spec_is_dwz, cu);
8480 real_pdi = res.pdi;
8481 cu = res.cu;
8482 }
8483
8484 parent = real_pdi->die_parent;
8485 if (parent == NULL)
8486 return NULL;
8487
8488 if (parent->scope_set)
8489 return parent->scope;
8490
8491 parent->fixup (cu);
8492
8493 grandparent_scope = partial_die_parent_scope (parent, cu);
8494
8495 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8496 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8497 Work around this problem here. */
8498 if (cu->language == language_cplus
8499 && parent->tag == DW_TAG_namespace
8500 && strcmp (parent->name, "::") == 0
8501 && grandparent_scope == NULL)
8502 {
8503 parent->scope = NULL;
8504 parent->scope_set = 1;
8505 return NULL;
8506 }
8507
8508 /* Nested subroutines in Fortran get a prefix. */
8509 if (pdi->tag == DW_TAG_enumerator)
8510 /* Enumerators should not get the name of the enumeration as a prefix. */
8511 parent->scope = grandparent_scope;
8512 else if (parent->tag == DW_TAG_namespace
8513 || parent->tag == DW_TAG_module
8514 || parent->tag == DW_TAG_structure_type
8515 || parent->tag == DW_TAG_class_type
8516 || parent->tag == DW_TAG_interface_type
8517 || parent->tag == DW_TAG_union_type
8518 || parent->tag == DW_TAG_enumeration_type
8519 || (cu->language == language_fortran
8520 && parent->tag == DW_TAG_subprogram
8521 && pdi->tag == DW_TAG_subprogram))
8522 {
8523 if (grandparent_scope == NULL)
8524 parent->scope = parent->name;
8525 else
8526 parent->scope = typename_concat (&cu->comp_unit_obstack,
8527 grandparent_scope,
8528 parent->name, 0, cu);
8529 }
8530 else
8531 {
8532 /* FIXME drow/2004-04-01: What should we be doing with
8533 function-local names? For partial symbols, we should probably be
8534 ignoring them. */
8535 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8536 dwarf_tag_name (parent->tag),
8537 sect_offset_str (pdi->sect_off));
8538 parent->scope = grandparent_scope;
8539 }
8540
8541 parent->scope_set = 1;
8542 return parent->scope;
8543 }
8544
8545 /* Return the fully scoped name associated with PDI, from compilation unit
8546 CU. The result will be allocated with malloc. */
8547
8548 static gdb::unique_xmalloc_ptr<char>
8549 partial_die_full_name (struct partial_die_info *pdi,
8550 struct dwarf2_cu *cu)
8551 {
8552 const char *parent_scope;
8553
8554 /* If this is a template instantiation, we can not work out the
8555 template arguments from partial DIEs. So, unfortunately, we have
8556 to go through the full DIEs. At least any work we do building
8557 types here will be reused if full symbols are loaded later. */
8558 if (pdi->has_template_arguments)
8559 {
8560 pdi->fixup (cu);
8561
8562 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8563 {
8564 struct die_info *die;
8565 struct attribute attr;
8566 struct dwarf2_cu *ref_cu = cu;
8567
8568 /* DW_FORM_ref_addr is using section offset. */
8569 attr.name = (enum dwarf_attribute) 0;
8570 attr.form = DW_FORM_ref_addr;
8571 attr.u.unsnd = to_underlying (pdi->sect_off);
8572 die = follow_die_ref (NULL, &attr, &ref_cu);
8573
8574 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8575 }
8576 }
8577
8578 parent_scope = partial_die_parent_scope (pdi, cu);
8579 if (parent_scope == NULL)
8580 return NULL;
8581 else
8582 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8583 pdi->name, 0, cu));
8584 }
8585
8586 static void
8587 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8588 {
8589 struct dwarf2_per_objfile *dwarf2_per_objfile
8590 = cu->per_cu->dwarf2_per_objfile;
8591 struct objfile *objfile = dwarf2_per_objfile->objfile;
8592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8593 CORE_ADDR addr = 0;
8594 const char *actual_name = NULL;
8595 CORE_ADDR baseaddr;
8596
8597 baseaddr = objfile->text_section_offset ();
8598
8599 gdb::unique_xmalloc_ptr<char> built_actual_name
8600 = partial_die_full_name (pdi, cu);
8601 if (built_actual_name != NULL)
8602 actual_name = built_actual_name.get ();
8603
8604 if (actual_name == NULL)
8605 actual_name = pdi->name;
8606
8607 switch (pdi->tag)
8608 {
8609 case DW_TAG_inlined_subroutine:
8610 case DW_TAG_subprogram:
8611 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8612 - baseaddr);
8613 if (pdi->is_external
8614 || cu->language == language_ada
8615 || (cu->language == language_fortran
8616 && pdi->die_parent != NULL
8617 && pdi->die_parent->tag == DW_TAG_subprogram))
8618 {
8619 /* Normally, only "external" DIEs are part of the global scope.
8620 But in Ada and Fortran, we want to be able to access nested
8621 procedures globally. So all Ada and Fortran subprograms are
8622 stored in the global scope. */
8623 add_psymbol_to_list (actual_name,
8624 built_actual_name != NULL,
8625 VAR_DOMAIN, LOC_BLOCK,
8626 SECT_OFF_TEXT (objfile),
8627 psymbol_placement::GLOBAL,
8628 addr,
8629 cu->language, objfile);
8630 }
8631 else
8632 {
8633 add_psymbol_to_list (actual_name,
8634 built_actual_name != NULL,
8635 VAR_DOMAIN, LOC_BLOCK,
8636 SECT_OFF_TEXT (objfile),
8637 psymbol_placement::STATIC,
8638 addr, cu->language, objfile);
8639 }
8640
8641 if (pdi->main_subprogram && actual_name != NULL)
8642 set_objfile_main_name (objfile, actual_name, cu->language);
8643 break;
8644 case DW_TAG_constant:
8645 add_psymbol_to_list (actual_name,
8646 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8647 -1, (pdi->is_external
8648 ? psymbol_placement::GLOBAL
8649 : psymbol_placement::STATIC),
8650 0, cu->language, objfile);
8651 break;
8652 case DW_TAG_variable:
8653 if (pdi->d.locdesc)
8654 addr = decode_locdesc (pdi->d.locdesc, cu);
8655
8656 if (pdi->d.locdesc
8657 && addr == 0
8658 && !dwarf2_per_objfile->has_section_at_zero)
8659 {
8660 /* A global or static variable may also have been stripped
8661 out by the linker if unused, in which case its address
8662 will be nullified; do not add such variables into partial
8663 symbol table then. */
8664 }
8665 else if (pdi->is_external)
8666 {
8667 /* Global Variable.
8668 Don't enter into the minimal symbol tables as there is
8669 a minimal symbol table entry from the ELF symbols already.
8670 Enter into partial symbol table if it has a location
8671 descriptor or a type.
8672 If the location descriptor is missing, new_symbol will create
8673 a LOC_UNRESOLVED symbol, the address of the variable will then
8674 be determined from the minimal symbol table whenever the variable
8675 is referenced.
8676 The address for the partial symbol table entry is not
8677 used by GDB, but it comes in handy for debugging partial symbol
8678 table building. */
8679
8680 if (pdi->d.locdesc || pdi->has_type)
8681 add_psymbol_to_list (actual_name,
8682 built_actual_name != NULL,
8683 VAR_DOMAIN, LOC_STATIC,
8684 SECT_OFF_TEXT (objfile),
8685 psymbol_placement::GLOBAL,
8686 addr, cu->language, objfile);
8687 }
8688 else
8689 {
8690 int has_loc = pdi->d.locdesc != NULL;
8691
8692 /* Static Variable. Skip symbols whose value we cannot know (those
8693 without location descriptors or constant values). */
8694 if (!has_loc && !pdi->has_const_value)
8695 return;
8696
8697 add_psymbol_to_list (actual_name,
8698 built_actual_name != NULL,
8699 VAR_DOMAIN, LOC_STATIC,
8700 SECT_OFF_TEXT (objfile),
8701 psymbol_placement::STATIC,
8702 has_loc ? addr : 0,
8703 cu->language, objfile);
8704 }
8705 break;
8706 case DW_TAG_typedef:
8707 case DW_TAG_base_type:
8708 case DW_TAG_subrange_type:
8709 add_psymbol_to_list (actual_name,
8710 built_actual_name != NULL,
8711 VAR_DOMAIN, LOC_TYPEDEF, -1,
8712 psymbol_placement::STATIC,
8713 0, cu->language, objfile);
8714 break;
8715 case DW_TAG_imported_declaration:
8716 case DW_TAG_namespace:
8717 add_psymbol_to_list (actual_name,
8718 built_actual_name != NULL,
8719 VAR_DOMAIN, LOC_TYPEDEF, -1,
8720 psymbol_placement::GLOBAL,
8721 0, cu->language, objfile);
8722 break;
8723 case DW_TAG_module:
8724 /* With Fortran 77 there might be a "BLOCK DATA" module
8725 available without any name. If so, we skip the module as it
8726 doesn't bring any value. */
8727 if (actual_name != nullptr)
8728 add_psymbol_to_list (actual_name,
8729 built_actual_name != NULL,
8730 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8731 psymbol_placement::GLOBAL,
8732 0, cu->language, objfile);
8733 break;
8734 case DW_TAG_class_type:
8735 case DW_TAG_interface_type:
8736 case DW_TAG_structure_type:
8737 case DW_TAG_union_type:
8738 case DW_TAG_enumeration_type:
8739 /* Skip external references. The DWARF standard says in the section
8740 about "Structure, Union, and Class Type Entries": "An incomplete
8741 structure, union or class type is represented by a structure,
8742 union or class entry that does not have a byte size attribute
8743 and that has a DW_AT_declaration attribute." */
8744 if (!pdi->has_byte_size && pdi->is_declaration)
8745 return;
8746
8747 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8748 static vs. global. */
8749 add_psymbol_to_list (actual_name,
8750 built_actual_name != NULL,
8751 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8752 cu->language == language_cplus
8753 ? psymbol_placement::GLOBAL
8754 : psymbol_placement::STATIC,
8755 0, cu->language, objfile);
8756
8757 break;
8758 case DW_TAG_enumerator:
8759 add_psymbol_to_list (actual_name,
8760 built_actual_name != NULL,
8761 VAR_DOMAIN, LOC_CONST, -1,
8762 cu->language == language_cplus
8763 ? psymbol_placement::GLOBAL
8764 : psymbol_placement::STATIC,
8765 0, cu->language, objfile);
8766 break;
8767 default:
8768 break;
8769 }
8770 }
8771
8772 /* Read a partial die corresponding to a namespace; also, add a symbol
8773 corresponding to that namespace to the symbol table. NAMESPACE is
8774 the name of the enclosing namespace. */
8775
8776 static void
8777 add_partial_namespace (struct partial_die_info *pdi,
8778 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8779 int set_addrmap, struct dwarf2_cu *cu)
8780 {
8781 /* Add a symbol for the namespace. */
8782
8783 add_partial_symbol (pdi, cu);
8784
8785 /* Now scan partial symbols in that namespace. */
8786
8787 if (pdi->has_children)
8788 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8789 }
8790
8791 /* Read a partial die corresponding to a Fortran module. */
8792
8793 static void
8794 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8795 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8796 {
8797 /* Add a symbol for the namespace. */
8798
8799 add_partial_symbol (pdi, cu);
8800
8801 /* Now scan partial symbols in that module. */
8802
8803 if (pdi->has_children)
8804 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8805 }
8806
8807 /* Read a partial die corresponding to a subprogram or an inlined
8808 subprogram and create a partial symbol for that subprogram.
8809 When the CU language allows it, this routine also defines a partial
8810 symbol for each nested subprogram that this subprogram contains.
8811 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8812 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8813
8814 PDI may also be a lexical block, in which case we simply search
8815 recursively for subprograms defined inside that lexical block.
8816 Again, this is only performed when the CU language allows this
8817 type of definitions. */
8818
8819 static void
8820 add_partial_subprogram (struct partial_die_info *pdi,
8821 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8822 int set_addrmap, struct dwarf2_cu *cu)
8823 {
8824 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8825 {
8826 if (pdi->has_pc_info)
8827 {
8828 if (pdi->lowpc < *lowpc)
8829 *lowpc = pdi->lowpc;
8830 if (pdi->highpc > *highpc)
8831 *highpc = pdi->highpc;
8832 if (set_addrmap)
8833 {
8834 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8835 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8836 CORE_ADDR baseaddr;
8837 CORE_ADDR this_highpc;
8838 CORE_ADDR this_lowpc;
8839
8840 baseaddr = objfile->text_section_offset ();
8841 this_lowpc
8842 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8843 pdi->lowpc + baseaddr)
8844 - baseaddr);
8845 this_highpc
8846 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8847 pdi->highpc + baseaddr)
8848 - baseaddr);
8849 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8850 this_lowpc, this_highpc - 1,
8851 cu->per_cu->v.psymtab);
8852 }
8853 }
8854
8855 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8856 {
8857 if (!pdi->is_declaration)
8858 /* Ignore subprogram DIEs that do not have a name, they are
8859 illegal. Do not emit a complaint at this point, we will
8860 do so when we convert this psymtab into a symtab. */
8861 if (pdi->name)
8862 add_partial_symbol (pdi, cu);
8863 }
8864 }
8865
8866 if (! pdi->has_children)
8867 return;
8868
8869 if (cu->language == language_ada || cu->language == language_fortran)
8870 {
8871 pdi = pdi->die_child;
8872 while (pdi != NULL)
8873 {
8874 pdi->fixup (cu);
8875 if (pdi->tag == DW_TAG_subprogram
8876 || pdi->tag == DW_TAG_inlined_subroutine
8877 || pdi->tag == DW_TAG_lexical_block)
8878 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8879 pdi = pdi->die_sibling;
8880 }
8881 }
8882 }
8883
8884 /* Read a partial die corresponding to an enumeration type. */
8885
8886 static void
8887 add_partial_enumeration (struct partial_die_info *enum_pdi,
8888 struct dwarf2_cu *cu)
8889 {
8890 struct partial_die_info *pdi;
8891
8892 if (enum_pdi->name != NULL)
8893 add_partial_symbol (enum_pdi, cu);
8894
8895 pdi = enum_pdi->die_child;
8896 while (pdi)
8897 {
8898 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8899 complaint (_("malformed enumerator DIE ignored"));
8900 else
8901 add_partial_symbol (pdi, cu);
8902 pdi = pdi->die_sibling;
8903 }
8904 }
8905
8906 /* Return the initial uleb128 in the die at INFO_PTR. */
8907
8908 static unsigned int
8909 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8910 {
8911 unsigned int bytes_read;
8912
8913 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8914 }
8915
8916 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8917 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8918
8919 Return the corresponding abbrev, or NULL if the number is zero (indicating
8920 an empty DIE). In either case *BYTES_READ will be set to the length of
8921 the initial number. */
8922
8923 static struct abbrev_info *
8924 peek_die_abbrev (const die_reader_specs &reader,
8925 const gdb_byte *info_ptr, unsigned int *bytes_read)
8926 {
8927 dwarf2_cu *cu = reader.cu;
8928 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8929 unsigned int abbrev_number
8930 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8931
8932 if (abbrev_number == 0)
8933 return NULL;
8934
8935 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8936 if (!abbrev)
8937 {
8938 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8939 " at offset %s [in module %s]"),
8940 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8941 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8942 }
8943
8944 return abbrev;
8945 }
8946
8947 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8948 Returns a pointer to the end of a series of DIEs, terminated by an empty
8949 DIE. Any children of the skipped DIEs will also be skipped. */
8950
8951 static const gdb_byte *
8952 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8953 {
8954 while (1)
8955 {
8956 unsigned int bytes_read;
8957 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8958
8959 if (abbrev == NULL)
8960 return info_ptr + bytes_read;
8961 else
8962 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8963 }
8964 }
8965
8966 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8967 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8968 abbrev corresponding to that skipped uleb128 should be passed in
8969 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8970 children. */
8971
8972 static const gdb_byte *
8973 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8974 struct abbrev_info *abbrev)
8975 {
8976 unsigned int bytes_read;
8977 struct attribute attr;
8978 bfd *abfd = reader->abfd;
8979 struct dwarf2_cu *cu = reader->cu;
8980 const gdb_byte *buffer = reader->buffer;
8981 const gdb_byte *buffer_end = reader->buffer_end;
8982 unsigned int form, i;
8983
8984 for (i = 0; i < abbrev->num_attrs; i++)
8985 {
8986 /* The only abbrev we care about is DW_AT_sibling. */
8987 if (abbrev->attrs[i].name == DW_AT_sibling)
8988 {
8989 bool ignored;
8990 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8991 &ignored);
8992 if (attr.form == DW_FORM_ref_addr)
8993 complaint (_("ignoring absolute DW_AT_sibling"));
8994 else
8995 {
8996 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8997 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8998
8999 if (sibling_ptr < info_ptr)
9000 complaint (_("DW_AT_sibling points backwards"));
9001 else if (sibling_ptr > reader->buffer_end)
9002 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9003 else
9004 return sibling_ptr;
9005 }
9006 }
9007
9008 /* If it isn't DW_AT_sibling, skip this attribute. */
9009 form = abbrev->attrs[i].form;
9010 skip_attribute:
9011 switch (form)
9012 {
9013 case DW_FORM_ref_addr:
9014 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9015 and later it is offset sized. */
9016 if (cu->header.version == 2)
9017 info_ptr += cu->header.addr_size;
9018 else
9019 info_ptr += cu->header.offset_size;
9020 break;
9021 case DW_FORM_GNU_ref_alt:
9022 info_ptr += cu->header.offset_size;
9023 break;
9024 case DW_FORM_addr:
9025 info_ptr += cu->header.addr_size;
9026 break;
9027 case DW_FORM_data1:
9028 case DW_FORM_ref1:
9029 case DW_FORM_flag:
9030 case DW_FORM_strx1:
9031 info_ptr += 1;
9032 break;
9033 case DW_FORM_flag_present:
9034 case DW_FORM_implicit_const:
9035 break;
9036 case DW_FORM_data2:
9037 case DW_FORM_ref2:
9038 case DW_FORM_strx2:
9039 info_ptr += 2;
9040 break;
9041 case DW_FORM_strx3:
9042 info_ptr += 3;
9043 break;
9044 case DW_FORM_data4:
9045 case DW_FORM_ref4:
9046 case DW_FORM_strx4:
9047 info_ptr += 4;
9048 break;
9049 case DW_FORM_data8:
9050 case DW_FORM_ref8:
9051 case DW_FORM_ref_sig8:
9052 info_ptr += 8;
9053 break;
9054 case DW_FORM_data16:
9055 info_ptr += 16;
9056 break;
9057 case DW_FORM_string:
9058 read_direct_string (abfd, info_ptr, &bytes_read);
9059 info_ptr += bytes_read;
9060 break;
9061 case DW_FORM_sec_offset:
9062 case DW_FORM_strp:
9063 case DW_FORM_GNU_strp_alt:
9064 info_ptr += cu->header.offset_size;
9065 break;
9066 case DW_FORM_exprloc:
9067 case DW_FORM_block:
9068 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9069 info_ptr += bytes_read;
9070 break;
9071 case DW_FORM_block1:
9072 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9073 break;
9074 case DW_FORM_block2:
9075 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9076 break;
9077 case DW_FORM_block4:
9078 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9079 break;
9080 case DW_FORM_addrx:
9081 case DW_FORM_strx:
9082 case DW_FORM_sdata:
9083 case DW_FORM_udata:
9084 case DW_FORM_ref_udata:
9085 case DW_FORM_GNU_addr_index:
9086 case DW_FORM_GNU_str_index:
9087 case DW_FORM_rnglistx:
9088 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9089 break;
9090 case DW_FORM_indirect:
9091 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9092 info_ptr += bytes_read;
9093 /* We need to continue parsing from here, so just go back to
9094 the top. */
9095 goto skip_attribute;
9096
9097 default:
9098 error (_("Dwarf Error: Cannot handle %s "
9099 "in DWARF reader [in module %s]"),
9100 dwarf_form_name (form),
9101 bfd_get_filename (abfd));
9102 }
9103 }
9104
9105 if (abbrev->has_children)
9106 return skip_children (reader, info_ptr);
9107 else
9108 return info_ptr;
9109 }
9110
9111 /* Locate ORIG_PDI's sibling.
9112 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9113
9114 static const gdb_byte *
9115 locate_pdi_sibling (const struct die_reader_specs *reader,
9116 struct partial_die_info *orig_pdi,
9117 const gdb_byte *info_ptr)
9118 {
9119 /* Do we know the sibling already? */
9120
9121 if (orig_pdi->sibling)
9122 return orig_pdi->sibling;
9123
9124 /* Are there any children to deal with? */
9125
9126 if (!orig_pdi->has_children)
9127 return info_ptr;
9128
9129 /* Skip the children the long way. */
9130
9131 return skip_children (reader, info_ptr);
9132 }
9133
9134 /* Expand this partial symbol table into a full symbol table. SELF is
9135 not NULL. */
9136
9137 void
9138 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9139 {
9140 struct dwarf2_per_objfile *dwarf2_per_objfile
9141 = get_dwarf2_per_objfile (objfile);
9142
9143 gdb_assert (!readin);
9144 /* If this psymtab is constructed from a debug-only objfile, the
9145 has_section_at_zero flag will not necessarily be correct. We
9146 can get the correct value for this flag by looking at the data
9147 associated with the (presumably stripped) associated objfile. */
9148 if (objfile->separate_debug_objfile_backlink)
9149 {
9150 struct dwarf2_per_objfile *dpo_backlink
9151 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9152
9153 dwarf2_per_objfile->has_section_at_zero
9154 = dpo_backlink->has_section_at_zero;
9155 }
9156
9157 dwarf2_per_objfile->reading_partial_symbols = 0;
9158
9159 expand_psymtab (objfile);
9160
9161 process_cu_includes (dwarf2_per_objfile);
9162 }
9163 \f
9164 /* Reading in full CUs. */
9165
9166 /* Add PER_CU to the queue. */
9167
9168 static void
9169 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9170 enum language pretend_language)
9171 {
9172 struct dwarf2_queue_item *item;
9173
9174 per_cu->queued = 1;
9175 item = XNEW (struct dwarf2_queue_item);
9176 item->per_cu = per_cu;
9177 item->pretend_language = pretend_language;
9178 item->next = NULL;
9179
9180 if (dwarf2_queue == NULL)
9181 dwarf2_queue = item;
9182 else
9183 dwarf2_queue_tail->next = item;
9184
9185 dwarf2_queue_tail = item;
9186 }
9187
9188 /* If PER_CU is not yet queued, add it to the queue.
9189 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9190 dependency.
9191 The result is non-zero if PER_CU was queued, otherwise the result is zero
9192 meaning either PER_CU is already queued or it is already loaded.
9193
9194 N.B. There is an invariant here that if a CU is queued then it is loaded.
9195 The caller is required to load PER_CU if we return non-zero. */
9196
9197 static int
9198 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9199 struct dwarf2_per_cu_data *per_cu,
9200 enum language pretend_language)
9201 {
9202 /* We may arrive here during partial symbol reading, if we need full
9203 DIEs to process an unusual case (e.g. template arguments). Do
9204 not queue PER_CU, just tell our caller to load its DIEs. */
9205 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9206 {
9207 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9208 return 1;
9209 return 0;
9210 }
9211
9212 /* Mark the dependence relation so that we don't flush PER_CU
9213 too early. */
9214 if (dependent_cu != NULL)
9215 dwarf2_add_dependence (dependent_cu, per_cu);
9216
9217 /* If it's already on the queue, we have nothing to do. */
9218 if (per_cu->queued)
9219 return 0;
9220
9221 /* If the compilation unit is already loaded, just mark it as
9222 used. */
9223 if (per_cu->cu != NULL)
9224 {
9225 per_cu->cu->last_used = 0;
9226 return 0;
9227 }
9228
9229 /* Add it to the queue. */
9230 queue_comp_unit (per_cu, pretend_language);
9231
9232 return 1;
9233 }
9234
9235 /* Process the queue. */
9236
9237 static void
9238 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9239 {
9240 struct dwarf2_queue_item *item, *next_item;
9241
9242 if (dwarf_read_debug)
9243 {
9244 fprintf_unfiltered (gdb_stdlog,
9245 "Expanding one or more symtabs of objfile %s ...\n",
9246 objfile_name (dwarf2_per_objfile->objfile));
9247 }
9248
9249 /* The queue starts out with one item, but following a DIE reference
9250 may load a new CU, adding it to the end of the queue. */
9251 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9252 {
9253 if ((dwarf2_per_objfile->using_index
9254 ? !item->per_cu->v.quick->compunit_symtab
9255 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9256 /* Skip dummy CUs. */
9257 && item->per_cu->cu != NULL)
9258 {
9259 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9260 unsigned int debug_print_threshold;
9261 char buf[100];
9262
9263 if (per_cu->is_debug_types)
9264 {
9265 struct signatured_type *sig_type =
9266 (struct signatured_type *) per_cu;
9267
9268 sprintf (buf, "TU %s at offset %s",
9269 hex_string (sig_type->signature),
9270 sect_offset_str (per_cu->sect_off));
9271 /* There can be 100s of TUs.
9272 Only print them in verbose mode. */
9273 debug_print_threshold = 2;
9274 }
9275 else
9276 {
9277 sprintf (buf, "CU at offset %s",
9278 sect_offset_str (per_cu->sect_off));
9279 debug_print_threshold = 1;
9280 }
9281
9282 if (dwarf_read_debug >= debug_print_threshold)
9283 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9284
9285 if (per_cu->is_debug_types)
9286 process_full_type_unit (per_cu, item->pretend_language);
9287 else
9288 process_full_comp_unit (per_cu, item->pretend_language);
9289
9290 if (dwarf_read_debug >= debug_print_threshold)
9291 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9292 }
9293
9294 item->per_cu->queued = 0;
9295 next_item = item->next;
9296 xfree (item);
9297 }
9298
9299 dwarf2_queue_tail = NULL;
9300
9301 if (dwarf_read_debug)
9302 {
9303 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9304 objfile_name (dwarf2_per_objfile->objfile));
9305 }
9306 }
9307
9308 /* Read in full symbols for PST, and anything it depends on. */
9309
9310 void
9311 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9312 {
9313 struct dwarf2_per_cu_data *per_cu;
9314
9315 if (readin)
9316 return;
9317
9318 read_dependencies (objfile);
9319
9320 per_cu = per_cu_data;
9321
9322 if (per_cu == NULL)
9323 {
9324 /* It's an include file, no symbols to read for it.
9325 Everything is in the parent symtab. */
9326 readin = true;
9327 return;
9328 }
9329
9330 dw2_do_instantiate_symtab (per_cu, false);
9331 }
9332
9333 /* Trivial hash function for die_info: the hash value of a DIE
9334 is its offset in .debug_info for this objfile. */
9335
9336 static hashval_t
9337 die_hash (const void *item)
9338 {
9339 const struct die_info *die = (const struct die_info *) item;
9340
9341 return to_underlying (die->sect_off);
9342 }
9343
9344 /* Trivial comparison function for die_info structures: two DIEs
9345 are equal if they have the same offset. */
9346
9347 static int
9348 die_eq (const void *item_lhs, const void *item_rhs)
9349 {
9350 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9351 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9352
9353 return die_lhs->sect_off == die_rhs->sect_off;
9354 }
9355
9356 /* Load the DIEs associated with PER_CU into memory. */
9357
9358 static void
9359 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9360 bool skip_partial,
9361 enum language pretend_language)
9362 {
9363 gdb_assert (! this_cu->is_debug_types);
9364
9365 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9366 if (reader.dummy_p)
9367 return;
9368
9369 struct dwarf2_cu *cu = reader.cu;
9370 const gdb_byte *info_ptr = reader.info_ptr;
9371
9372 gdb_assert (cu->die_hash == NULL);
9373 cu->die_hash =
9374 htab_create_alloc_ex (cu->header.length / 12,
9375 die_hash,
9376 die_eq,
9377 NULL,
9378 &cu->comp_unit_obstack,
9379 hashtab_obstack_allocate,
9380 dummy_obstack_deallocate);
9381
9382 if (reader.has_children)
9383 reader.comp_unit_die->child
9384 = read_die_and_siblings (&reader, reader.info_ptr,
9385 &info_ptr, reader.comp_unit_die);
9386 cu->dies = reader.comp_unit_die;
9387 /* comp_unit_die is not stored in die_hash, no need. */
9388
9389 /* We try not to read any attributes in this function, because not
9390 all CUs needed for references have been loaded yet, and symbol
9391 table processing isn't initialized. But we have to set the CU language,
9392 or we won't be able to build types correctly.
9393 Similarly, if we do not read the producer, we can not apply
9394 producer-specific interpretation. */
9395 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9396 }
9397
9398 /* Add a DIE to the delayed physname list. */
9399
9400 static void
9401 add_to_method_list (struct type *type, int fnfield_index, int index,
9402 const char *name, struct die_info *die,
9403 struct dwarf2_cu *cu)
9404 {
9405 struct delayed_method_info mi;
9406 mi.type = type;
9407 mi.fnfield_index = fnfield_index;
9408 mi.index = index;
9409 mi.name = name;
9410 mi.die = die;
9411 cu->method_list.push_back (mi);
9412 }
9413
9414 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9415 "const" / "volatile". If so, decrements LEN by the length of the
9416 modifier and return true. Otherwise return false. */
9417
9418 template<size_t N>
9419 static bool
9420 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9421 {
9422 size_t mod_len = sizeof (mod) - 1;
9423 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9424 {
9425 len -= mod_len;
9426 return true;
9427 }
9428 return false;
9429 }
9430
9431 /* Compute the physnames of any methods on the CU's method list.
9432
9433 The computation of method physnames is delayed in order to avoid the
9434 (bad) condition that one of the method's formal parameters is of an as yet
9435 incomplete type. */
9436
9437 static void
9438 compute_delayed_physnames (struct dwarf2_cu *cu)
9439 {
9440 /* Only C++ delays computing physnames. */
9441 if (cu->method_list.empty ())
9442 return;
9443 gdb_assert (cu->language == language_cplus);
9444
9445 for (const delayed_method_info &mi : cu->method_list)
9446 {
9447 const char *physname;
9448 struct fn_fieldlist *fn_flp
9449 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9450 physname = dwarf2_physname (mi.name, mi.die, cu);
9451 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9452 = physname ? physname : "";
9453
9454 /* Since there's no tag to indicate whether a method is a
9455 const/volatile overload, extract that information out of the
9456 demangled name. */
9457 if (physname != NULL)
9458 {
9459 size_t len = strlen (physname);
9460
9461 while (1)
9462 {
9463 if (physname[len] == ')') /* shortcut */
9464 break;
9465 else if (check_modifier (physname, len, " const"))
9466 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9467 else if (check_modifier (physname, len, " volatile"))
9468 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9469 else
9470 break;
9471 }
9472 }
9473 }
9474
9475 /* The list is no longer needed. */
9476 cu->method_list.clear ();
9477 }
9478
9479 /* Go objects should be embedded in a DW_TAG_module DIE,
9480 and it's not clear if/how imported objects will appear.
9481 To keep Go support simple until that's worked out,
9482 go back through what we've read and create something usable.
9483 We could do this while processing each DIE, and feels kinda cleaner,
9484 but that way is more invasive.
9485 This is to, for example, allow the user to type "p var" or "b main"
9486 without having to specify the package name, and allow lookups
9487 of module.object to work in contexts that use the expression
9488 parser. */
9489
9490 static void
9491 fixup_go_packaging (struct dwarf2_cu *cu)
9492 {
9493 gdb::unique_xmalloc_ptr<char> package_name;
9494 struct pending *list;
9495 int i;
9496
9497 for (list = *cu->get_builder ()->get_global_symbols ();
9498 list != NULL;
9499 list = list->next)
9500 {
9501 for (i = 0; i < list->nsyms; ++i)
9502 {
9503 struct symbol *sym = list->symbol[i];
9504
9505 if (sym->language () == language_go
9506 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9507 {
9508 gdb::unique_xmalloc_ptr<char> this_package_name
9509 (go_symbol_package_name (sym));
9510
9511 if (this_package_name == NULL)
9512 continue;
9513 if (package_name == NULL)
9514 package_name = std::move (this_package_name);
9515 else
9516 {
9517 struct objfile *objfile
9518 = cu->per_cu->dwarf2_per_objfile->objfile;
9519 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9520 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9521 (symbol_symtab (sym) != NULL
9522 ? symtab_to_filename_for_display
9523 (symbol_symtab (sym))
9524 : objfile_name (objfile)),
9525 this_package_name.get (), package_name.get ());
9526 }
9527 }
9528 }
9529 }
9530
9531 if (package_name != NULL)
9532 {
9533 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9534 const char *saved_package_name
9535 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9536 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9537 saved_package_name);
9538 struct symbol *sym;
9539
9540 sym = allocate_symbol (objfile);
9541 sym->set_language (language_go, &objfile->objfile_obstack);
9542 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9543 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9544 e.g., "main" finds the "main" module and not C's main(). */
9545 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9546 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9547 SYMBOL_TYPE (sym) = type;
9548
9549 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9550 }
9551 }
9552
9553 /* Allocate a fully-qualified name consisting of the two parts on the
9554 obstack. */
9555
9556 static const char *
9557 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9558 {
9559 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9560 }
9561
9562 /* A helper that allocates a struct discriminant_info to attach to a
9563 union type. */
9564
9565 static struct discriminant_info *
9566 alloc_discriminant_info (struct type *type, int discriminant_index,
9567 int default_index)
9568 {
9569 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9570 gdb_assert (discriminant_index == -1
9571 || (discriminant_index >= 0
9572 && discriminant_index < TYPE_NFIELDS (type)));
9573 gdb_assert (default_index == -1
9574 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9575
9576 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9577
9578 struct discriminant_info *disc
9579 = ((struct discriminant_info *)
9580 TYPE_ZALLOC (type,
9581 offsetof (struct discriminant_info, discriminants)
9582 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9583 disc->default_index = default_index;
9584 disc->discriminant_index = discriminant_index;
9585
9586 struct dynamic_prop prop;
9587 prop.kind = PROP_UNDEFINED;
9588 prop.data.baton = disc;
9589
9590 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9591
9592 return disc;
9593 }
9594
9595 /* Some versions of rustc emitted enums in an unusual way.
9596
9597 Ordinary enums were emitted as unions. The first element of each
9598 structure in the union was named "RUST$ENUM$DISR". This element
9599 held the discriminant.
9600
9601 These versions of Rust also implemented the "non-zero"
9602 optimization. When the enum had two values, and one is empty and
9603 the other holds a pointer that cannot be zero, the pointer is used
9604 as the discriminant, with a zero value meaning the empty variant.
9605 Here, the union's first member is of the form
9606 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9607 where the fieldnos are the indices of the fields that should be
9608 traversed in order to find the field (which may be several fields deep)
9609 and the variantname is the name of the variant of the case when the
9610 field is zero.
9611
9612 This function recognizes whether TYPE is of one of these forms,
9613 and, if so, smashes it to be a variant type. */
9614
9615 static void
9616 quirk_rust_enum (struct type *type, struct objfile *objfile)
9617 {
9618 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9619
9620 /* We don't need to deal with empty enums. */
9621 if (TYPE_NFIELDS (type) == 0)
9622 return;
9623
9624 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9625 if (TYPE_NFIELDS (type) == 1
9626 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9627 {
9628 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9629
9630 /* Decode the field name to find the offset of the
9631 discriminant. */
9632 ULONGEST bit_offset = 0;
9633 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9634 while (name[0] >= '0' && name[0] <= '9')
9635 {
9636 char *tail;
9637 unsigned long index = strtoul (name, &tail, 10);
9638 name = tail;
9639 if (*name != '$'
9640 || index >= TYPE_NFIELDS (field_type)
9641 || (TYPE_FIELD_LOC_KIND (field_type, index)
9642 != FIELD_LOC_KIND_BITPOS))
9643 {
9644 complaint (_("Could not parse Rust enum encoding string \"%s\""
9645 "[in module %s]"),
9646 TYPE_FIELD_NAME (type, 0),
9647 objfile_name (objfile));
9648 return;
9649 }
9650 ++name;
9651
9652 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9653 field_type = TYPE_FIELD_TYPE (field_type, index);
9654 }
9655
9656 /* Make a union to hold the variants. */
9657 struct type *union_type = alloc_type (objfile);
9658 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9659 TYPE_NFIELDS (union_type) = 3;
9660 TYPE_FIELDS (union_type)
9661 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9662 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9663 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9664
9665 /* Put the discriminant must at index 0. */
9666 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9667 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9668 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9669 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9670
9671 /* The order of fields doesn't really matter, so put the real
9672 field at index 1 and the data-less field at index 2. */
9673 struct discriminant_info *disc
9674 = alloc_discriminant_info (union_type, 0, 1);
9675 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9676 TYPE_FIELD_NAME (union_type, 1)
9677 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9678 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9679 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9680 TYPE_FIELD_NAME (union_type, 1));
9681
9682 const char *dataless_name
9683 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9684 name);
9685 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9686 dataless_name);
9687 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9688 /* NAME points into the original discriminant name, which
9689 already has the correct lifetime. */
9690 TYPE_FIELD_NAME (union_type, 2) = name;
9691 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9692 disc->discriminants[2] = 0;
9693
9694 /* Smash this type to be a structure type. We have to do this
9695 because the type has already been recorded. */
9696 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9697 TYPE_NFIELDS (type) = 1;
9698 TYPE_FIELDS (type)
9699 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9700
9701 /* Install the variant part. */
9702 TYPE_FIELD_TYPE (type, 0) = union_type;
9703 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9704 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9705 }
9706 /* A union with a single anonymous field is probably an old-style
9707 univariant enum. */
9708 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9709 {
9710 /* Smash this type to be a structure type. We have to do this
9711 because the type has already been recorded. */
9712 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9713
9714 /* Make a union to hold the variants. */
9715 struct type *union_type = alloc_type (objfile);
9716 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9717 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9718 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9719 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9720 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9721
9722 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9723 const char *variant_name
9724 = rust_last_path_segment (TYPE_NAME (field_type));
9725 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9726 TYPE_NAME (field_type)
9727 = rust_fully_qualify (&objfile->objfile_obstack,
9728 TYPE_NAME (type), variant_name);
9729
9730 /* Install the union in the outer struct type. */
9731 TYPE_NFIELDS (type) = 1;
9732 TYPE_FIELDS (type)
9733 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9734 TYPE_FIELD_TYPE (type, 0) = union_type;
9735 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9736 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9737
9738 alloc_discriminant_info (union_type, -1, 0);
9739 }
9740 else
9741 {
9742 struct type *disr_type = nullptr;
9743 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9744 {
9745 disr_type = TYPE_FIELD_TYPE (type, i);
9746
9747 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9748 {
9749 /* All fields of a true enum will be structs. */
9750 return;
9751 }
9752 else if (TYPE_NFIELDS (disr_type) == 0)
9753 {
9754 /* Could be data-less variant, so keep going. */
9755 disr_type = nullptr;
9756 }
9757 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9758 "RUST$ENUM$DISR") != 0)
9759 {
9760 /* Not a Rust enum. */
9761 return;
9762 }
9763 else
9764 {
9765 /* Found one. */
9766 break;
9767 }
9768 }
9769
9770 /* If we got here without a discriminant, then it's probably
9771 just a union. */
9772 if (disr_type == nullptr)
9773 return;
9774
9775 /* Smash this type to be a structure type. We have to do this
9776 because the type has already been recorded. */
9777 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9778
9779 /* Make a union to hold the variants. */
9780 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9781 struct type *union_type = alloc_type (objfile);
9782 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9783 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9784 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9785 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9786 TYPE_FIELDS (union_type)
9787 = (struct field *) TYPE_ZALLOC (union_type,
9788 (TYPE_NFIELDS (union_type)
9789 * sizeof (struct field)));
9790
9791 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9792 TYPE_NFIELDS (type) * sizeof (struct field));
9793
9794 /* Install the discriminant at index 0 in the union. */
9795 TYPE_FIELD (union_type, 0) = *disr_field;
9796 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9797 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9798
9799 /* Install the union in the outer struct type. */
9800 TYPE_FIELD_TYPE (type, 0) = union_type;
9801 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9802 TYPE_NFIELDS (type) = 1;
9803
9804 /* Set the size and offset of the union type. */
9805 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9806
9807 /* We need a way to find the correct discriminant given a
9808 variant name. For convenience we build a map here. */
9809 struct type *enum_type = FIELD_TYPE (*disr_field);
9810 std::unordered_map<std::string, ULONGEST> discriminant_map;
9811 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9812 {
9813 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9814 {
9815 const char *name
9816 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9817 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9818 }
9819 }
9820
9821 int n_fields = TYPE_NFIELDS (union_type);
9822 struct discriminant_info *disc
9823 = alloc_discriminant_info (union_type, 0, -1);
9824 /* Skip the discriminant here. */
9825 for (int i = 1; i < n_fields; ++i)
9826 {
9827 /* Find the final word in the name of this variant's type.
9828 That name can be used to look up the correct
9829 discriminant. */
9830 const char *variant_name
9831 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9832 i)));
9833
9834 auto iter = discriminant_map.find (variant_name);
9835 if (iter != discriminant_map.end ())
9836 disc->discriminants[i] = iter->second;
9837
9838 /* Remove the discriminant field, if it exists. */
9839 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9840 if (TYPE_NFIELDS (sub_type) > 0)
9841 {
9842 --TYPE_NFIELDS (sub_type);
9843 ++TYPE_FIELDS (sub_type);
9844 }
9845 TYPE_FIELD_NAME (union_type, i) = variant_name;
9846 TYPE_NAME (sub_type)
9847 = rust_fully_qualify (&objfile->objfile_obstack,
9848 TYPE_NAME (type), variant_name);
9849 }
9850 }
9851 }
9852
9853 /* Rewrite some Rust unions to be structures with variants parts. */
9854
9855 static void
9856 rust_union_quirks (struct dwarf2_cu *cu)
9857 {
9858 gdb_assert (cu->language == language_rust);
9859 for (type *type_ : cu->rust_unions)
9860 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9861 /* We don't need this any more. */
9862 cu->rust_unions.clear ();
9863 }
9864
9865 /* Return the symtab for PER_CU. This works properly regardless of
9866 whether we're using the index or psymtabs. */
9867
9868 static struct compunit_symtab *
9869 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9870 {
9871 return (per_cu->dwarf2_per_objfile->using_index
9872 ? per_cu->v.quick->compunit_symtab
9873 : per_cu->v.psymtab->compunit_symtab);
9874 }
9875
9876 /* A helper function for computing the list of all symbol tables
9877 included by PER_CU. */
9878
9879 static void
9880 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9881 htab_t all_children, htab_t all_type_symtabs,
9882 struct dwarf2_per_cu_data *per_cu,
9883 struct compunit_symtab *immediate_parent)
9884 {
9885 void **slot;
9886 struct compunit_symtab *cust;
9887
9888 slot = htab_find_slot (all_children, per_cu, INSERT);
9889 if (*slot != NULL)
9890 {
9891 /* This inclusion and its children have been processed. */
9892 return;
9893 }
9894
9895 *slot = per_cu;
9896 /* Only add a CU if it has a symbol table. */
9897 cust = get_compunit_symtab (per_cu);
9898 if (cust != NULL)
9899 {
9900 /* If this is a type unit only add its symbol table if we haven't
9901 seen it yet (type unit per_cu's can share symtabs). */
9902 if (per_cu->is_debug_types)
9903 {
9904 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9905 if (*slot == NULL)
9906 {
9907 *slot = cust;
9908 result->push_back (cust);
9909 if (cust->user == NULL)
9910 cust->user = immediate_parent;
9911 }
9912 }
9913 else
9914 {
9915 result->push_back (cust);
9916 if (cust->user == NULL)
9917 cust->user = immediate_parent;
9918 }
9919 }
9920
9921 if (!per_cu->imported_symtabs_empty ())
9922 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9923 {
9924 recursively_compute_inclusions (result, all_children,
9925 all_type_symtabs, ptr, cust);
9926 }
9927 }
9928
9929 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9930 PER_CU. */
9931
9932 static void
9933 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9934 {
9935 gdb_assert (! per_cu->is_debug_types);
9936
9937 if (!per_cu->imported_symtabs_empty ())
9938 {
9939 int len;
9940 std::vector<compunit_symtab *> result_symtabs;
9941 htab_t all_children, all_type_symtabs;
9942 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9943
9944 /* If we don't have a symtab, we can just skip this case. */
9945 if (cust == NULL)
9946 return;
9947
9948 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9949 NULL, xcalloc, xfree);
9950 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9951 NULL, xcalloc, xfree);
9952
9953 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9954 {
9955 recursively_compute_inclusions (&result_symtabs, all_children,
9956 all_type_symtabs, ptr, cust);
9957 }
9958
9959 /* Now we have a transitive closure of all the included symtabs. */
9960 len = result_symtabs.size ();
9961 cust->includes
9962 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9963 struct compunit_symtab *, len + 1);
9964 memcpy (cust->includes, result_symtabs.data (),
9965 len * sizeof (compunit_symtab *));
9966 cust->includes[len] = NULL;
9967
9968 htab_delete (all_children);
9969 htab_delete (all_type_symtabs);
9970 }
9971 }
9972
9973 /* Compute the 'includes' field for the symtabs of all the CUs we just
9974 read. */
9975
9976 static void
9977 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9978 {
9979 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9980 {
9981 if (! iter->is_debug_types)
9982 compute_compunit_symtab_includes (iter);
9983 }
9984
9985 dwarf2_per_objfile->just_read_cus.clear ();
9986 }
9987
9988 /* Generate full symbol information for PER_CU, whose DIEs have
9989 already been loaded into memory. */
9990
9991 static void
9992 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9993 enum language pretend_language)
9994 {
9995 struct dwarf2_cu *cu = per_cu->cu;
9996 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9997 struct objfile *objfile = dwarf2_per_objfile->objfile;
9998 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9999 CORE_ADDR lowpc, highpc;
10000 struct compunit_symtab *cust;
10001 CORE_ADDR baseaddr;
10002 struct block *static_block;
10003 CORE_ADDR addr;
10004
10005 baseaddr = objfile->text_section_offset ();
10006
10007 /* Clear the list here in case something was left over. */
10008 cu->method_list.clear ();
10009
10010 cu->language = pretend_language;
10011 cu->language_defn = language_def (cu->language);
10012
10013 /* Do line number decoding in read_file_scope () */
10014 process_die (cu->dies, cu);
10015
10016 /* For now fudge the Go package. */
10017 if (cu->language == language_go)
10018 fixup_go_packaging (cu);
10019
10020 /* Now that we have processed all the DIEs in the CU, all the types
10021 should be complete, and it should now be safe to compute all of the
10022 physnames. */
10023 compute_delayed_physnames (cu);
10024
10025 if (cu->language == language_rust)
10026 rust_union_quirks (cu);
10027
10028 /* Some compilers don't define a DW_AT_high_pc attribute for the
10029 compilation unit. If the DW_AT_high_pc is missing, synthesize
10030 it, by scanning the DIE's below the compilation unit. */
10031 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10032
10033 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10034 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10035
10036 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10037 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10038 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10039 addrmap to help ensure it has an accurate map of pc values belonging to
10040 this comp unit. */
10041 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10042
10043 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10044 SECT_OFF_TEXT (objfile),
10045 0);
10046
10047 if (cust != NULL)
10048 {
10049 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10050
10051 /* Set symtab language to language from DW_AT_language. If the
10052 compilation is from a C file generated by language preprocessors, do
10053 not set the language if it was already deduced by start_subfile. */
10054 if (!(cu->language == language_c
10055 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10056 COMPUNIT_FILETABS (cust)->language = cu->language;
10057
10058 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10059 produce DW_AT_location with location lists but it can be possibly
10060 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10061 there were bugs in prologue debug info, fixed later in GCC-4.5
10062 by "unwind info for epilogues" patch (which is not directly related).
10063
10064 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10065 needed, it would be wrong due to missing DW_AT_producer there.
10066
10067 Still one can confuse GDB by using non-standard GCC compilation
10068 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10069 */
10070 if (cu->has_loclist && gcc_4_minor >= 5)
10071 cust->locations_valid = 1;
10072
10073 if (gcc_4_minor >= 5)
10074 cust->epilogue_unwind_valid = 1;
10075
10076 cust->call_site_htab = cu->call_site_htab;
10077 }
10078
10079 if (dwarf2_per_objfile->using_index)
10080 per_cu->v.quick->compunit_symtab = cust;
10081 else
10082 {
10083 dwarf2_psymtab *pst = per_cu->v.psymtab;
10084 pst->compunit_symtab = cust;
10085 pst->readin = true;
10086 }
10087
10088 /* Push it for inclusion processing later. */
10089 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10090
10091 /* Not needed any more. */
10092 cu->reset_builder ();
10093 }
10094
10095 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10096 already been loaded into memory. */
10097
10098 static void
10099 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10100 enum language pretend_language)
10101 {
10102 struct dwarf2_cu *cu = per_cu->cu;
10103 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10104 struct objfile *objfile = dwarf2_per_objfile->objfile;
10105 struct compunit_symtab *cust;
10106 struct signatured_type *sig_type;
10107
10108 gdb_assert (per_cu->is_debug_types);
10109 sig_type = (struct signatured_type *) per_cu;
10110
10111 /* Clear the list here in case something was left over. */
10112 cu->method_list.clear ();
10113
10114 cu->language = pretend_language;
10115 cu->language_defn = language_def (cu->language);
10116
10117 /* The symbol tables are set up in read_type_unit_scope. */
10118 process_die (cu->dies, cu);
10119
10120 /* For now fudge the Go package. */
10121 if (cu->language == language_go)
10122 fixup_go_packaging (cu);
10123
10124 /* Now that we have processed all the DIEs in the CU, all the types
10125 should be complete, and it should now be safe to compute all of the
10126 physnames. */
10127 compute_delayed_physnames (cu);
10128
10129 if (cu->language == language_rust)
10130 rust_union_quirks (cu);
10131
10132 /* TUs share symbol tables.
10133 If this is the first TU to use this symtab, complete the construction
10134 of it with end_expandable_symtab. Otherwise, complete the addition of
10135 this TU's symbols to the existing symtab. */
10136 if (sig_type->type_unit_group->compunit_symtab == NULL)
10137 {
10138 buildsym_compunit *builder = cu->get_builder ();
10139 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10140 sig_type->type_unit_group->compunit_symtab = cust;
10141
10142 if (cust != NULL)
10143 {
10144 /* Set symtab language to language from DW_AT_language. If the
10145 compilation is from a C file generated by language preprocessors,
10146 do not set the language if it was already deduced by
10147 start_subfile. */
10148 if (!(cu->language == language_c
10149 && COMPUNIT_FILETABS (cust)->language != language_c))
10150 COMPUNIT_FILETABS (cust)->language = cu->language;
10151 }
10152 }
10153 else
10154 {
10155 cu->get_builder ()->augment_type_symtab ();
10156 cust = sig_type->type_unit_group->compunit_symtab;
10157 }
10158
10159 if (dwarf2_per_objfile->using_index)
10160 per_cu->v.quick->compunit_symtab = cust;
10161 else
10162 {
10163 dwarf2_psymtab *pst = per_cu->v.psymtab;
10164 pst->compunit_symtab = cust;
10165 pst->readin = true;
10166 }
10167
10168 /* Not needed any more. */
10169 cu->reset_builder ();
10170 }
10171
10172 /* Process an imported unit DIE. */
10173
10174 static void
10175 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10176 {
10177 struct attribute *attr;
10178
10179 /* For now we don't handle imported units in type units. */
10180 if (cu->per_cu->is_debug_types)
10181 {
10182 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10183 " supported in type units [in module %s]"),
10184 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10185 }
10186
10187 attr = dwarf2_attr (die, DW_AT_import, cu);
10188 if (attr != NULL)
10189 {
10190 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10191 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10192 dwarf2_per_cu_data *per_cu
10193 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10194 cu->per_cu->dwarf2_per_objfile);
10195
10196 /* If necessary, add it to the queue and load its DIEs. */
10197 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10198 load_full_comp_unit (per_cu, false, cu->language);
10199
10200 cu->per_cu->imported_symtabs_push (per_cu);
10201 }
10202 }
10203
10204 /* RAII object that represents a process_die scope: i.e.,
10205 starts/finishes processing a DIE. */
10206 class process_die_scope
10207 {
10208 public:
10209 process_die_scope (die_info *die, dwarf2_cu *cu)
10210 : m_die (die), m_cu (cu)
10211 {
10212 /* We should only be processing DIEs not already in process. */
10213 gdb_assert (!m_die->in_process);
10214 m_die->in_process = true;
10215 }
10216
10217 ~process_die_scope ()
10218 {
10219 m_die->in_process = false;
10220
10221 /* If we're done processing the DIE for the CU that owns the line
10222 header, we don't need the line header anymore. */
10223 if (m_cu->line_header_die_owner == m_die)
10224 {
10225 delete m_cu->line_header;
10226 m_cu->line_header = NULL;
10227 m_cu->line_header_die_owner = NULL;
10228 }
10229 }
10230
10231 private:
10232 die_info *m_die;
10233 dwarf2_cu *m_cu;
10234 };
10235
10236 /* Process a die and its children. */
10237
10238 static void
10239 process_die (struct die_info *die, struct dwarf2_cu *cu)
10240 {
10241 process_die_scope scope (die, cu);
10242
10243 switch (die->tag)
10244 {
10245 case DW_TAG_padding:
10246 break;
10247 case DW_TAG_compile_unit:
10248 case DW_TAG_partial_unit:
10249 read_file_scope (die, cu);
10250 break;
10251 case DW_TAG_type_unit:
10252 read_type_unit_scope (die, cu);
10253 break;
10254 case DW_TAG_subprogram:
10255 /* Nested subprograms in Fortran get a prefix. */
10256 if (cu->language == language_fortran
10257 && die->parent != NULL
10258 && die->parent->tag == DW_TAG_subprogram)
10259 cu->processing_has_namespace_info = true;
10260 /* Fall through. */
10261 case DW_TAG_inlined_subroutine:
10262 read_func_scope (die, cu);
10263 break;
10264 case DW_TAG_lexical_block:
10265 case DW_TAG_try_block:
10266 case DW_TAG_catch_block:
10267 read_lexical_block_scope (die, cu);
10268 break;
10269 case DW_TAG_call_site:
10270 case DW_TAG_GNU_call_site:
10271 read_call_site_scope (die, cu);
10272 break;
10273 case DW_TAG_class_type:
10274 case DW_TAG_interface_type:
10275 case DW_TAG_structure_type:
10276 case DW_TAG_union_type:
10277 process_structure_scope (die, cu);
10278 break;
10279 case DW_TAG_enumeration_type:
10280 process_enumeration_scope (die, cu);
10281 break;
10282
10283 /* These dies have a type, but processing them does not create
10284 a symbol or recurse to process the children. Therefore we can
10285 read them on-demand through read_type_die. */
10286 case DW_TAG_subroutine_type:
10287 case DW_TAG_set_type:
10288 case DW_TAG_array_type:
10289 case DW_TAG_pointer_type:
10290 case DW_TAG_ptr_to_member_type:
10291 case DW_TAG_reference_type:
10292 case DW_TAG_rvalue_reference_type:
10293 case DW_TAG_string_type:
10294 break;
10295
10296 case DW_TAG_base_type:
10297 case DW_TAG_subrange_type:
10298 case DW_TAG_typedef:
10299 /* Add a typedef symbol for the type definition, if it has a
10300 DW_AT_name. */
10301 new_symbol (die, read_type_die (die, cu), cu);
10302 break;
10303 case DW_TAG_common_block:
10304 read_common_block (die, cu);
10305 break;
10306 case DW_TAG_common_inclusion:
10307 break;
10308 case DW_TAG_namespace:
10309 cu->processing_has_namespace_info = true;
10310 read_namespace (die, cu);
10311 break;
10312 case DW_TAG_module:
10313 cu->processing_has_namespace_info = true;
10314 read_module (die, cu);
10315 break;
10316 case DW_TAG_imported_declaration:
10317 cu->processing_has_namespace_info = true;
10318 if (read_namespace_alias (die, cu))
10319 break;
10320 /* The declaration is not a global namespace alias. */
10321 /* Fall through. */
10322 case DW_TAG_imported_module:
10323 cu->processing_has_namespace_info = true;
10324 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10325 || cu->language != language_fortran))
10326 complaint (_("Tag '%s' has unexpected children"),
10327 dwarf_tag_name (die->tag));
10328 read_import_statement (die, cu);
10329 break;
10330
10331 case DW_TAG_imported_unit:
10332 process_imported_unit_die (die, cu);
10333 break;
10334
10335 case DW_TAG_variable:
10336 read_variable (die, cu);
10337 break;
10338
10339 default:
10340 new_symbol (die, NULL, cu);
10341 break;
10342 }
10343 }
10344 \f
10345 /* DWARF name computation. */
10346
10347 /* A helper function for dwarf2_compute_name which determines whether DIE
10348 needs to have the name of the scope prepended to the name listed in the
10349 die. */
10350
10351 static int
10352 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10353 {
10354 struct attribute *attr;
10355
10356 switch (die->tag)
10357 {
10358 case DW_TAG_namespace:
10359 case DW_TAG_typedef:
10360 case DW_TAG_class_type:
10361 case DW_TAG_interface_type:
10362 case DW_TAG_structure_type:
10363 case DW_TAG_union_type:
10364 case DW_TAG_enumeration_type:
10365 case DW_TAG_enumerator:
10366 case DW_TAG_subprogram:
10367 case DW_TAG_inlined_subroutine:
10368 case DW_TAG_member:
10369 case DW_TAG_imported_declaration:
10370 return 1;
10371
10372 case DW_TAG_variable:
10373 case DW_TAG_constant:
10374 /* We only need to prefix "globally" visible variables. These include
10375 any variable marked with DW_AT_external or any variable that
10376 lives in a namespace. [Variables in anonymous namespaces
10377 require prefixing, but they are not DW_AT_external.] */
10378
10379 if (dwarf2_attr (die, DW_AT_specification, cu))
10380 {
10381 struct dwarf2_cu *spec_cu = cu;
10382
10383 return die_needs_namespace (die_specification (die, &spec_cu),
10384 spec_cu);
10385 }
10386
10387 attr = dwarf2_attr (die, DW_AT_external, cu);
10388 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10389 && die->parent->tag != DW_TAG_module)
10390 return 0;
10391 /* A variable in a lexical block of some kind does not need a
10392 namespace, even though in C++ such variables may be external
10393 and have a mangled name. */
10394 if (die->parent->tag == DW_TAG_lexical_block
10395 || die->parent->tag == DW_TAG_try_block
10396 || die->parent->tag == DW_TAG_catch_block
10397 || die->parent->tag == DW_TAG_subprogram)
10398 return 0;
10399 return 1;
10400
10401 default:
10402 return 0;
10403 }
10404 }
10405
10406 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10407 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10408 defined for the given DIE. */
10409
10410 static struct attribute *
10411 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10412 {
10413 struct attribute *attr;
10414
10415 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10416 if (attr == NULL)
10417 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10418
10419 return attr;
10420 }
10421
10422 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10423 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10424 defined for the given DIE. */
10425
10426 static const char *
10427 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10428 {
10429 const char *linkage_name;
10430
10431 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10432 if (linkage_name == NULL)
10433 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10434
10435 return linkage_name;
10436 }
10437
10438 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10439 compute the physname for the object, which include a method's:
10440 - formal parameters (C++),
10441 - receiver type (Go),
10442
10443 The term "physname" is a bit confusing.
10444 For C++, for example, it is the demangled name.
10445 For Go, for example, it's the mangled name.
10446
10447 For Ada, return the DIE's linkage name rather than the fully qualified
10448 name. PHYSNAME is ignored..
10449
10450 The result is allocated on the objfile_obstack and canonicalized. */
10451
10452 static const char *
10453 dwarf2_compute_name (const char *name,
10454 struct die_info *die, struct dwarf2_cu *cu,
10455 int physname)
10456 {
10457 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10458
10459 if (name == NULL)
10460 name = dwarf2_name (die, cu);
10461
10462 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10463 but otherwise compute it by typename_concat inside GDB.
10464 FIXME: Actually this is not really true, or at least not always true.
10465 It's all very confusing. compute_and_set_names doesn't try to demangle
10466 Fortran names because there is no mangling standard. So new_symbol
10467 will set the demangled name to the result of dwarf2_full_name, and it is
10468 the demangled name that GDB uses if it exists. */
10469 if (cu->language == language_ada
10470 || (cu->language == language_fortran && physname))
10471 {
10472 /* For Ada unit, we prefer the linkage name over the name, as
10473 the former contains the exported name, which the user expects
10474 to be able to reference. Ideally, we want the user to be able
10475 to reference this entity using either natural or linkage name,
10476 but we haven't started looking at this enhancement yet. */
10477 const char *linkage_name = dw2_linkage_name (die, cu);
10478
10479 if (linkage_name != NULL)
10480 return linkage_name;
10481 }
10482
10483 /* These are the only languages we know how to qualify names in. */
10484 if (name != NULL
10485 && (cu->language == language_cplus
10486 || cu->language == language_fortran || cu->language == language_d
10487 || cu->language == language_rust))
10488 {
10489 if (die_needs_namespace (die, cu))
10490 {
10491 const char *prefix;
10492 const char *canonical_name = NULL;
10493
10494 string_file buf;
10495
10496 prefix = determine_prefix (die, cu);
10497 if (*prefix != '\0')
10498 {
10499 gdb::unique_xmalloc_ptr<char> prefixed_name
10500 (typename_concat (NULL, prefix, name, physname, cu));
10501
10502 buf.puts (prefixed_name.get ());
10503 }
10504 else
10505 buf.puts (name);
10506
10507 /* Template parameters may be specified in the DIE's DW_AT_name, or
10508 as children with DW_TAG_template_type_param or
10509 DW_TAG_value_type_param. If the latter, add them to the name
10510 here. If the name already has template parameters, then
10511 skip this step; some versions of GCC emit both, and
10512 it is more efficient to use the pre-computed name.
10513
10514 Something to keep in mind about this process: it is very
10515 unlikely, or in some cases downright impossible, to produce
10516 something that will match the mangled name of a function.
10517 If the definition of the function has the same debug info,
10518 we should be able to match up with it anyway. But fallbacks
10519 using the minimal symbol, for instance to find a method
10520 implemented in a stripped copy of libstdc++, will not work.
10521 If we do not have debug info for the definition, we will have to
10522 match them up some other way.
10523
10524 When we do name matching there is a related problem with function
10525 templates; two instantiated function templates are allowed to
10526 differ only by their return types, which we do not add here. */
10527
10528 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10529 {
10530 struct attribute *attr;
10531 struct die_info *child;
10532 int first = 1;
10533
10534 die->building_fullname = 1;
10535
10536 for (child = die->child; child != NULL; child = child->sibling)
10537 {
10538 struct type *type;
10539 LONGEST value;
10540 const gdb_byte *bytes;
10541 struct dwarf2_locexpr_baton *baton;
10542 struct value *v;
10543
10544 if (child->tag != DW_TAG_template_type_param
10545 && child->tag != DW_TAG_template_value_param)
10546 continue;
10547
10548 if (first)
10549 {
10550 buf.puts ("<");
10551 first = 0;
10552 }
10553 else
10554 buf.puts (", ");
10555
10556 attr = dwarf2_attr (child, DW_AT_type, cu);
10557 if (attr == NULL)
10558 {
10559 complaint (_("template parameter missing DW_AT_type"));
10560 buf.puts ("UNKNOWN_TYPE");
10561 continue;
10562 }
10563 type = die_type (child, cu);
10564
10565 if (child->tag == DW_TAG_template_type_param)
10566 {
10567 c_print_type (type, "", &buf, -1, 0, cu->language,
10568 &type_print_raw_options);
10569 continue;
10570 }
10571
10572 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10573 if (attr == NULL)
10574 {
10575 complaint (_("template parameter missing "
10576 "DW_AT_const_value"));
10577 buf.puts ("UNKNOWN_VALUE");
10578 continue;
10579 }
10580
10581 dwarf2_const_value_attr (attr, type, name,
10582 &cu->comp_unit_obstack, cu,
10583 &value, &bytes, &baton);
10584
10585 if (TYPE_NOSIGN (type))
10586 /* GDB prints characters as NUMBER 'CHAR'. If that's
10587 changed, this can use value_print instead. */
10588 c_printchar (value, type, &buf);
10589 else
10590 {
10591 struct value_print_options opts;
10592
10593 if (baton != NULL)
10594 v = dwarf2_evaluate_loc_desc (type, NULL,
10595 baton->data,
10596 baton->size,
10597 baton->per_cu);
10598 else if (bytes != NULL)
10599 {
10600 v = allocate_value (type);
10601 memcpy (value_contents_writeable (v), bytes,
10602 TYPE_LENGTH (type));
10603 }
10604 else
10605 v = value_from_longest (type, value);
10606
10607 /* Specify decimal so that we do not depend on
10608 the radix. */
10609 get_formatted_print_options (&opts, 'd');
10610 opts.raw = 1;
10611 value_print (v, &buf, &opts);
10612 release_value (v);
10613 }
10614 }
10615
10616 die->building_fullname = 0;
10617
10618 if (!first)
10619 {
10620 /* Close the argument list, with a space if necessary
10621 (nested templates). */
10622 if (!buf.empty () && buf.string ().back () == '>')
10623 buf.puts (" >");
10624 else
10625 buf.puts (">");
10626 }
10627 }
10628
10629 /* For C++ methods, append formal parameter type
10630 information, if PHYSNAME. */
10631
10632 if (physname && die->tag == DW_TAG_subprogram
10633 && cu->language == language_cplus)
10634 {
10635 struct type *type = read_type_die (die, cu);
10636
10637 c_type_print_args (type, &buf, 1, cu->language,
10638 &type_print_raw_options);
10639
10640 if (cu->language == language_cplus)
10641 {
10642 /* Assume that an artificial first parameter is
10643 "this", but do not crash if it is not. RealView
10644 marks unnamed (and thus unused) parameters as
10645 artificial; there is no way to differentiate
10646 the two cases. */
10647 if (TYPE_NFIELDS (type) > 0
10648 && TYPE_FIELD_ARTIFICIAL (type, 0)
10649 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10650 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10651 0))))
10652 buf.puts (" const");
10653 }
10654 }
10655
10656 const std::string &intermediate_name = buf.string ();
10657
10658 if (cu->language == language_cplus)
10659 canonical_name
10660 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10661 &objfile->per_bfd->storage_obstack);
10662
10663 /* If we only computed INTERMEDIATE_NAME, or if
10664 INTERMEDIATE_NAME is already canonical, then we need to
10665 copy it to the appropriate obstack. */
10666 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10667 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10668 intermediate_name);
10669 else
10670 name = canonical_name;
10671 }
10672 }
10673
10674 return name;
10675 }
10676
10677 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10678 If scope qualifiers are appropriate they will be added. The result
10679 will be allocated on the storage_obstack, or NULL if the DIE does
10680 not have a name. NAME may either be from a previous call to
10681 dwarf2_name or NULL.
10682
10683 The output string will be canonicalized (if C++). */
10684
10685 static const char *
10686 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10687 {
10688 return dwarf2_compute_name (name, die, cu, 0);
10689 }
10690
10691 /* Construct a physname for the given DIE in CU. NAME may either be
10692 from a previous call to dwarf2_name or NULL. The result will be
10693 allocated on the objfile_objstack or NULL if the DIE does not have a
10694 name.
10695
10696 The output string will be canonicalized (if C++). */
10697
10698 static const char *
10699 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10700 {
10701 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10702 const char *retval, *mangled = NULL, *canon = NULL;
10703 int need_copy = 1;
10704
10705 /* In this case dwarf2_compute_name is just a shortcut not building anything
10706 on its own. */
10707 if (!die_needs_namespace (die, cu))
10708 return dwarf2_compute_name (name, die, cu, 1);
10709
10710 mangled = dw2_linkage_name (die, cu);
10711
10712 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10713 See https://github.com/rust-lang/rust/issues/32925. */
10714 if (cu->language == language_rust && mangled != NULL
10715 && strchr (mangled, '{') != NULL)
10716 mangled = NULL;
10717
10718 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10719 has computed. */
10720 gdb::unique_xmalloc_ptr<char> demangled;
10721 if (mangled != NULL)
10722 {
10723
10724 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10725 {
10726 /* Do nothing (do not demangle the symbol name). */
10727 }
10728 else if (cu->language == language_go)
10729 {
10730 /* This is a lie, but we already lie to the caller new_symbol.
10731 new_symbol assumes we return the mangled name.
10732 This just undoes that lie until things are cleaned up. */
10733 }
10734 else
10735 {
10736 /* Use DMGL_RET_DROP for C++ template functions to suppress
10737 their return type. It is easier for GDB users to search
10738 for such functions as `name(params)' than `long name(params)'.
10739 In such case the minimal symbol names do not match the full
10740 symbol names but for template functions there is never a need
10741 to look up their definition from their declaration so
10742 the only disadvantage remains the minimal symbol variant
10743 `long name(params)' does not have the proper inferior type. */
10744 demangled.reset (gdb_demangle (mangled,
10745 (DMGL_PARAMS | DMGL_ANSI
10746 | DMGL_RET_DROP)));
10747 }
10748 if (demangled)
10749 canon = demangled.get ();
10750 else
10751 {
10752 canon = mangled;
10753 need_copy = 0;
10754 }
10755 }
10756
10757 if (canon == NULL || check_physname)
10758 {
10759 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10760
10761 if (canon != NULL && strcmp (physname, canon) != 0)
10762 {
10763 /* It may not mean a bug in GDB. The compiler could also
10764 compute DW_AT_linkage_name incorrectly. But in such case
10765 GDB would need to be bug-to-bug compatible. */
10766
10767 complaint (_("Computed physname <%s> does not match demangled <%s> "
10768 "(from linkage <%s>) - DIE at %s [in module %s]"),
10769 physname, canon, mangled, sect_offset_str (die->sect_off),
10770 objfile_name (objfile));
10771
10772 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10773 is available here - over computed PHYSNAME. It is safer
10774 against both buggy GDB and buggy compilers. */
10775
10776 retval = canon;
10777 }
10778 else
10779 {
10780 retval = physname;
10781 need_copy = 0;
10782 }
10783 }
10784 else
10785 retval = canon;
10786
10787 if (need_copy)
10788 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10789
10790 return retval;
10791 }
10792
10793 /* Inspect DIE in CU for a namespace alias. If one exists, record
10794 a new symbol for it.
10795
10796 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10797
10798 static int
10799 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10800 {
10801 struct attribute *attr;
10802
10803 /* If the die does not have a name, this is not a namespace
10804 alias. */
10805 attr = dwarf2_attr (die, DW_AT_name, cu);
10806 if (attr != NULL)
10807 {
10808 int num;
10809 struct die_info *d = die;
10810 struct dwarf2_cu *imported_cu = cu;
10811
10812 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10813 keep inspecting DIEs until we hit the underlying import. */
10814 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10815 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10816 {
10817 attr = dwarf2_attr (d, DW_AT_import, cu);
10818 if (attr == NULL)
10819 break;
10820
10821 d = follow_die_ref (d, attr, &imported_cu);
10822 if (d->tag != DW_TAG_imported_declaration)
10823 break;
10824 }
10825
10826 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10827 {
10828 complaint (_("DIE at %s has too many recursively imported "
10829 "declarations"), sect_offset_str (d->sect_off));
10830 return 0;
10831 }
10832
10833 if (attr != NULL)
10834 {
10835 struct type *type;
10836 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10837
10838 type = get_die_type_at_offset (sect_off, cu->per_cu);
10839 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10840 {
10841 /* This declaration is a global namespace alias. Add
10842 a symbol for it whose type is the aliased namespace. */
10843 new_symbol (die, type, cu);
10844 return 1;
10845 }
10846 }
10847 }
10848
10849 return 0;
10850 }
10851
10852 /* Return the using directives repository (global or local?) to use in the
10853 current context for CU.
10854
10855 For Ada, imported declarations can materialize renamings, which *may* be
10856 global. However it is impossible (for now?) in DWARF to distinguish
10857 "external" imported declarations and "static" ones. As all imported
10858 declarations seem to be static in all other languages, make them all CU-wide
10859 global only in Ada. */
10860
10861 static struct using_direct **
10862 using_directives (struct dwarf2_cu *cu)
10863 {
10864 if (cu->language == language_ada
10865 && cu->get_builder ()->outermost_context_p ())
10866 return cu->get_builder ()->get_global_using_directives ();
10867 else
10868 return cu->get_builder ()->get_local_using_directives ();
10869 }
10870
10871 /* Read the import statement specified by the given die and record it. */
10872
10873 static void
10874 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10875 {
10876 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10877 struct attribute *import_attr;
10878 struct die_info *imported_die, *child_die;
10879 struct dwarf2_cu *imported_cu;
10880 const char *imported_name;
10881 const char *imported_name_prefix;
10882 const char *canonical_name;
10883 const char *import_alias;
10884 const char *imported_declaration = NULL;
10885 const char *import_prefix;
10886 std::vector<const char *> excludes;
10887
10888 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10889 if (import_attr == NULL)
10890 {
10891 complaint (_("Tag '%s' has no DW_AT_import"),
10892 dwarf_tag_name (die->tag));
10893 return;
10894 }
10895
10896 imported_cu = cu;
10897 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10898 imported_name = dwarf2_name (imported_die, imported_cu);
10899 if (imported_name == NULL)
10900 {
10901 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10902
10903 The import in the following code:
10904 namespace A
10905 {
10906 typedef int B;
10907 }
10908
10909 int main ()
10910 {
10911 using A::B;
10912 B b;
10913 return b;
10914 }
10915
10916 ...
10917 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10918 <52> DW_AT_decl_file : 1
10919 <53> DW_AT_decl_line : 6
10920 <54> DW_AT_import : <0x75>
10921 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10922 <59> DW_AT_name : B
10923 <5b> DW_AT_decl_file : 1
10924 <5c> DW_AT_decl_line : 2
10925 <5d> DW_AT_type : <0x6e>
10926 ...
10927 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10928 <76> DW_AT_byte_size : 4
10929 <77> DW_AT_encoding : 5 (signed)
10930
10931 imports the wrong die ( 0x75 instead of 0x58 ).
10932 This case will be ignored until the gcc bug is fixed. */
10933 return;
10934 }
10935
10936 /* Figure out the local name after import. */
10937 import_alias = dwarf2_name (die, cu);
10938
10939 /* Figure out where the statement is being imported to. */
10940 import_prefix = determine_prefix (die, cu);
10941
10942 /* Figure out what the scope of the imported die is and prepend it
10943 to the name of the imported die. */
10944 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10945
10946 if (imported_die->tag != DW_TAG_namespace
10947 && imported_die->tag != DW_TAG_module)
10948 {
10949 imported_declaration = imported_name;
10950 canonical_name = imported_name_prefix;
10951 }
10952 else if (strlen (imported_name_prefix) > 0)
10953 canonical_name = obconcat (&objfile->objfile_obstack,
10954 imported_name_prefix,
10955 (cu->language == language_d ? "." : "::"),
10956 imported_name, (char *) NULL);
10957 else
10958 canonical_name = imported_name;
10959
10960 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10961 for (child_die = die->child; child_die && child_die->tag;
10962 child_die = sibling_die (child_die))
10963 {
10964 /* DWARF-4: A Fortran use statement with a “rename list” may be
10965 represented by an imported module entry with an import attribute
10966 referring to the module and owned entries corresponding to those
10967 entities that are renamed as part of being imported. */
10968
10969 if (child_die->tag != DW_TAG_imported_declaration)
10970 {
10971 complaint (_("child DW_TAG_imported_declaration expected "
10972 "- DIE at %s [in module %s]"),
10973 sect_offset_str (child_die->sect_off),
10974 objfile_name (objfile));
10975 continue;
10976 }
10977
10978 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10979 if (import_attr == NULL)
10980 {
10981 complaint (_("Tag '%s' has no DW_AT_import"),
10982 dwarf_tag_name (child_die->tag));
10983 continue;
10984 }
10985
10986 imported_cu = cu;
10987 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10988 &imported_cu);
10989 imported_name = dwarf2_name (imported_die, imported_cu);
10990 if (imported_name == NULL)
10991 {
10992 complaint (_("child DW_TAG_imported_declaration has unknown "
10993 "imported name - DIE at %s [in module %s]"),
10994 sect_offset_str (child_die->sect_off),
10995 objfile_name (objfile));
10996 continue;
10997 }
10998
10999 excludes.push_back (imported_name);
11000
11001 process_die (child_die, cu);
11002 }
11003
11004 add_using_directive (using_directives (cu),
11005 import_prefix,
11006 canonical_name,
11007 import_alias,
11008 imported_declaration,
11009 excludes,
11010 0,
11011 &objfile->objfile_obstack);
11012 }
11013
11014 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11015 types, but gives them a size of zero. Starting with version 14,
11016 ICC is compatible with GCC. */
11017
11018 static bool
11019 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11020 {
11021 if (!cu->checked_producer)
11022 check_producer (cu);
11023
11024 return cu->producer_is_icc_lt_14;
11025 }
11026
11027 /* ICC generates a DW_AT_type for C void functions. This was observed on
11028 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11029 which says that void functions should not have a DW_AT_type. */
11030
11031 static bool
11032 producer_is_icc (struct dwarf2_cu *cu)
11033 {
11034 if (!cu->checked_producer)
11035 check_producer (cu);
11036
11037 return cu->producer_is_icc;
11038 }
11039
11040 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11041 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11042 this, it was first present in GCC release 4.3.0. */
11043
11044 static bool
11045 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11046 {
11047 if (!cu->checked_producer)
11048 check_producer (cu);
11049
11050 return cu->producer_is_gcc_lt_4_3;
11051 }
11052
11053 static file_and_directory
11054 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11055 {
11056 file_and_directory res;
11057
11058 /* Find the filename. Do not use dwarf2_name here, since the filename
11059 is not a source language identifier. */
11060 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11061 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11062
11063 if (res.comp_dir == NULL
11064 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11065 && IS_ABSOLUTE_PATH (res.name))
11066 {
11067 res.comp_dir_storage = ldirname (res.name);
11068 if (!res.comp_dir_storage.empty ())
11069 res.comp_dir = res.comp_dir_storage.c_str ();
11070 }
11071 if (res.comp_dir != NULL)
11072 {
11073 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11074 directory, get rid of it. */
11075 const char *cp = strchr (res.comp_dir, ':');
11076
11077 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11078 res.comp_dir = cp + 1;
11079 }
11080
11081 if (res.name == NULL)
11082 res.name = "<unknown>";
11083
11084 return res;
11085 }
11086
11087 /* Handle DW_AT_stmt_list for a compilation unit.
11088 DIE is the DW_TAG_compile_unit die for CU.
11089 COMP_DIR is the compilation directory. LOWPC is passed to
11090 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11091
11092 static void
11093 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11094 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11095 {
11096 struct dwarf2_per_objfile *dwarf2_per_objfile
11097 = cu->per_cu->dwarf2_per_objfile;
11098 struct objfile *objfile = dwarf2_per_objfile->objfile;
11099 struct attribute *attr;
11100 struct line_header line_header_local;
11101 hashval_t line_header_local_hash;
11102 void **slot;
11103 int decode_mapping;
11104
11105 gdb_assert (! cu->per_cu->is_debug_types);
11106
11107 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11108 if (attr == NULL)
11109 return;
11110
11111 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11112
11113 /* The line header hash table is only created if needed (it exists to
11114 prevent redundant reading of the line table for partial_units).
11115 If we're given a partial_unit, we'll need it. If we're given a
11116 compile_unit, then use the line header hash table if it's already
11117 created, but don't create one just yet. */
11118
11119 if (dwarf2_per_objfile->line_header_hash == NULL
11120 && die->tag == DW_TAG_partial_unit)
11121 {
11122 dwarf2_per_objfile->line_header_hash
11123 = htab_create_alloc_ex (127, line_header_hash_voidp,
11124 line_header_eq_voidp,
11125 free_line_header_voidp,
11126 &objfile->objfile_obstack,
11127 hashtab_obstack_allocate,
11128 dummy_obstack_deallocate);
11129 }
11130
11131 line_header_local.sect_off = line_offset;
11132 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11133 line_header_local_hash = line_header_hash (&line_header_local);
11134 if (dwarf2_per_objfile->line_header_hash != NULL)
11135 {
11136 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11137 &line_header_local,
11138 line_header_local_hash, NO_INSERT);
11139
11140 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11141 is not present in *SLOT (since if there is something in *SLOT then
11142 it will be for a partial_unit). */
11143 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11144 {
11145 gdb_assert (*slot != NULL);
11146 cu->line_header = (struct line_header *) *slot;
11147 return;
11148 }
11149 }
11150
11151 /* dwarf_decode_line_header does not yet provide sufficient information.
11152 We always have to call also dwarf_decode_lines for it. */
11153 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11154 if (lh == NULL)
11155 return;
11156
11157 cu->line_header = lh.release ();
11158 cu->line_header_die_owner = die;
11159
11160 if (dwarf2_per_objfile->line_header_hash == NULL)
11161 slot = NULL;
11162 else
11163 {
11164 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11165 &line_header_local,
11166 line_header_local_hash, INSERT);
11167 gdb_assert (slot != NULL);
11168 }
11169 if (slot != NULL && *slot == NULL)
11170 {
11171 /* This newly decoded line number information unit will be owned
11172 by line_header_hash hash table. */
11173 *slot = cu->line_header;
11174 cu->line_header_die_owner = NULL;
11175 }
11176 else
11177 {
11178 /* We cannot free any current entry in (*slot) as that struct line_header
11179 may be already used by multiple CUs. Create only temporary decoded
11180 line_header for this CU - it may happen at most once for each line
11181 number information unit. And if we're not using line_header_hash
11182 then this is what we want as well. */
11183 gdb_assert (die->tag != DW_TAG_partial_unit);
11184 }
11185 decode_mapping = (die->tag != DW_TAG_partial_unit);
11186 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11187 decode_mapping);
11188
11189 }
11190
11191 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11192
11193 static void
11194 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11195 {
11196 struct dwarf2_per_objfile *dwarf2_per_objfile
11197 = cu->per_cu->dwarf2_per_objfile;
11198 struct objfile *objfile = dwarf2_per_objfile->objfile;
11199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11200 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11201 CORE_ADDR highpc = ((CORE_ADDR) 0);
11202 struct attribute *attr;
11203 struct die_info *child_die;
11204 CORE_ADDR baseaddr;
11205
11206 prepare_one_comp_unit (cu, die, cu->language);
11207 baseaddr = objfile->text_section_offset ();
11208
11209 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11210
11211 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11212 from finish_block. */
11213 if (lowpc == ((CORE_ADDR) -1))
11214 lowpc = highpc;
11215 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11216
11217 file_and_directory fnd = find_file_and_directory (die, cu);
11218
11219 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11220 standardised yet. As a workaround for the language detection we fall
11221 back to the DW_AT_producer string. */
11222 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11223 cu->language = language_opencl;
11224
11225 /* Similar hack for Go. */
11226 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11227 set_cu_language (DW_LANG_Go, cu);
11228
11229 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11230
11231 /* Decode line number information if present. We do this before
11232 processing child DIEs, so that the line header table is available
11233 for DW_AT_decl_file. */
11234 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11235
11236 /* Process all dies in compilation unit. */
11237 if (die->child != NULL)
11238 {
11239 child_die = die->child;
11240 while (child_die && child_die->tag)
11241 {
11242 process_die (child_die, cu);
11243 child_die = sibling_die (child_die);
11244 }
11245 }
11246
11247 /* Decode macro information, if present. Dwarf 2 macro information
11248 refers to information in the line number info statement program
11249 header, so we can only read it if we've read the header
11250 successfully. */
11251 attr = dwarf2_attr (die, DW_AT_macros, cu);
11252 if (attr == NULL)
11253 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11254 if (attr && cu->line_header)
11255 {
11256 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11257 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11258
11259 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11260 }
11261 else
11262 {
11263 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11264 if (attr && cu->line_header)
11265 {
11266 unsigned int macro_offset = DW_UNSND (attr);
11267
11268 dwarf_decode_macros (cu, macro_offset, 0);
11269 }
11270 }
11271 }
11272
11273 void
11274 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11275 {
11276 struct type_unit_group *tu_group;
11277 int first_time;
11278 struct attribute *attr;
11279 unsigned int i;
11280 struct signatured_type *sig_type;
11281
11282 gdb_assert (per_cu->is_debug_types);
11283 sig_type = (struct signatured_type *) per_cu;
11284
11285 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11286
11287 /* If we're using .gdb_index (includes -readnow) then
11288 per_cu->type_unit_group may not have been set up yet. */
11289 if (sig_type->type_unit_group == NULL)
11290 sig_type->type_unit_group = get_type_unit_group (this, attr);
11291 tu_group = sig_type->type_unit_group;
11292
11293 /* If we've already processed this stmt_list there's no real need to
11294 do it again, we could fake it and just recreate the part we need
11295 (file name,index -> symtab mapping). If data shows this optimization
11296 is useful we can do it then. */
11297 first_time = tu_group->compunit_symtab == NULL;
11298
11299 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11300 debug info. */
11301 line_header_up lh;
11302 if (attr != NULL)
11303 {
11304 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11305 lh = dwarf_decode_line_header (line_offset, this);
11306 }
11307 if (lh == NULL)
11308 {
11309 if (first_time)
11310 start_symtab ("", NULL, 0);
11311 else
11312 {
11313 gdb_assert (tu_group->symtabs == NULL);
11314 gdb_assert (m_builder == nullptr);
11315 struct compunit_symtab *cust = tu_group->compunit_symtab;
11316 m_builder.reset (new struct buildsym_compunit
11317 (COMPUNIT_OBJFILE (cust), "",
11318 COMPUNIT_DIRNAME (cust),
11319 compunit_language (cust),
11320 0, cust));
11321 }
11322 return;
11323 }
11324
11325 line_header = lh.release ();
11326 line_header_die_owner = die;
11327
11328 if (first_time)
11329 {
11330 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11331
11332 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11333 still initializing it, and our caller (a few levels up)
11334 process_full_type_unit still needs to know if this is the first
11335 time. */
11336
11337 tu_group->num_symtabs = line_header->file_names_size ();
11338 tu_group->symtabs = XNEWVEC (struct symtab *,
11339 line_header->file_names_size ());
11340
11341 auto &file_names = line_header->file_names ();
11342 for (i = 0; i < file_names.size (); ++i)
11343 {
11344 file_entry &fe = file_names[i];
11345 dwarf2_start_subfile (this, fe.name,
11346 fe.include_dir (line_header));
11347 buildsym_compunit *b = get_builder ();
11348 if (b->get_current_subfile ()->symtab == NULL)
11349 {
11350 /* NOTE: start_subfile will recognize when it's been
11351 passed a file it has already seen. So we can't
11352 assume there's a simple mapping from
11353 cu->line_header->file_names to subfiles, plus
11354 cu->line_header->file_names may contain dups. */
11355 b->get_current_subfile ()->symtab
11356 = allocate_symtab (cust, b->get_current_subfile ()->name);
11357 }
11358
11359 fe.symtab = b->get_current_subfile ()->symtab;
11360 tu_group->symtabs[i] = fe.symtab;
11361 }
11362 }
11363 else
11364 {
11365 gdb_assert (m_builder == nullptr);
11366 struct compunit_symtab *cust = tu_group->compunit_symtab;
11367 m_builder.reset (new struct buildsym_compunit
11368 (COMPUNIT_OBJFILE (cust), "",
11369 COMPUNIT_DIRNAME (cust),
11370 compunit_language (cust),
11371 0, cust));
11372
11373 auto &file_names = line_header->file_names ();
11374 for (i = 0; i < file_names.size (); ++i)
11375 {
11376 file_entry &fe = file_names[i];
11377 fe.symtab = tu_group->symtabs[i];
11378 }
11379 }
11380
11381 /* The main symtab is allocated last. Type units don't have DW_AT_name
11382 so they don't have a "real" (so to speak) symtab anyway.
11383 There is later code that will assign the main symtab to all symbols
11384 that don't have one. We need to handle the case of a symbol with a
11385 missing symtab (DW_AT_decl_file) anyway. */
11386 }
11387
11388 /* Process DW_TAG_type_unit.
11389 For TUs we want to skip the first top level sibling if it's not the
11390 actual type being defined by this TU. In this case the first top
11391 level sibling is there to provide context only. */
11392
11393 static void
11394 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11395 {
11396 struct die_info *child_die;
11397
11398 prepare_one_comp_unit (cu, die, language_minimal);
11399
11400 /* Initialize (or reinitialize) the machinery for building symtabs.
11401 We do this before processing child DIEs, so that the line header table
11402 is available for DW_AT_decl_file. */
11403 cu->setup_type_unit_groups (die);
11404
11405 if (die->child != NULL)
11406 {
11407 child_die = die->child;
11408 while (child_die && child_die->tag)
11409 {
11410 process_die (child_die, cu);
11411 child_die = sibling_die (child_die);
11412 }
11413 }
11414 }
11415 \f
11416 /* DWO/DWP files.
11417
11418 http://gcc.gnu.org/wiki/DebugFission
11419 http://gcc.gnu.org/wiki/DebugFissionDWP
11420
11421 To simplify handling of both DWO files ("object" files with the DWARF info)
11422 and DWP files (a file with the DWOs packaged up into one file), we treat
11423 DWP files as having a collection of virtual DWO files. */
11424
11425 static hashval_t
11426 hash_dwo_file (const void *item)
11427 {
11428 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11429 hashval_t hash;
11430
11431 hash = htab_hash_string (dwo_file->dwo_name);
11432 if (dwo_file->comp_dir != NULL)
11433 hash += htab_hash_string (dwo_file->comp_dir);
11434 return hash;
11435 }
11436
11437 static int
11438 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11439 {
11440 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11441 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11442
11443 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11444 return 0;
11445 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11446 return lhs->comp_dir == rhs->comp_dir;
11447 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11448 }
11449
11450 /* Allocate a hash table for DWO files. */
11451
11452 static htab_up
11453 allocate_dwo_file_hash_table (struct objfile *objfile)
11454 {
11455 auto delete_dwo_file = [] (void *item)
11456 {
11457 struct dwo_file *dwo_file = (struct dwo_file *) item;
11458
11459 delete dwo_file;
11460 };
11461
11462 return htab_up (htab_create_alloc_ex (41,
11463 hash_dwo_file,
11464 eq_dwo_file,
11465 delete_dwo_file,
11466 &objfile->objfile_obstack,
11467 hashtab_obstack_allocate,
11468 dummy_obstack_deallocate));
11469 }
11470
11471 /* Lookup DWO file DWO_NAME. */
11472
11473 static void **
11474 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11475 const char *dwo_name,
11476 const char *comp_dir)
11477 {
11478 struct dwo_file find_entry;
11479 void **slot;
11480
11481 if (dwarf2_per_objfile->dwo_files == NULL)
11482 dwarf2_per_objfile->dwo_files
11483 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11484
11485 find_entry.dwo_name = dwo_name;
11486 find_entry.comp_dir = comp_dir;
11487 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11488 INSERT);
11489
11490 return slot;
11491 }
11492
11493 static hashval_t
11494 hash_dwo_unit (const void *item)
11495 {
11496 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11497
11498 /* This drops the top 32 bits of the id, but is ok for a hash. */
11499 return dwo_unit->signature;
11500 }
11501
11502 static int
11503 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11504 {
11505 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11506 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11507
11508 /* The signature is assumed to be unique within the DWO file.
11509 So while object file CU dwo_id's always have the value zero,
11510 that's OK, assuming each object file DWO file has only one CU,
11511 and that's the rule for now. */
11512 return lhs->signature == rhs->signature;
11513 }
11514
11515 /* Allocate a hash table for DWO CUs,TUs.
11516 There is one of these tables for each of CUs,TUs for each DWO file. */
11517
11518 static htab_t
11519 allocate_dwo_unit_table (struct objfile *objfile)
11520 {
11521 /* Start out with a pretty small number.
11522 Generally DWO files contain only one CU and maybe some TUs. */
11523 return htab_create_alloc_ex (3,
11524 hash_dwo_unit,
11525 eq_dwo_unit,
11526 NULL,
11527 &objfile->objfile_obstack,
11528 hashtab_obstack_allocate,
11529 dummy_obstack_deallocate);
11530 }
11531
11532 /* die_reader_func for create_dwo_cu. */
11533
11534 static void
11535 create_dwo_cu_reader (const struct die_reader_specs *reader,
11536 const gdb_byte *info_ptr,
11537 struct die_info *comp_unit_die,
11538 int has_children,
11539 struct dwo_file *dwo_file,
11540 struct dwo_unit *dwo_unit)
11541 {
11542 struct dwarf2_cu *cu = reader->cu;
11543 sect_offset sect_off = cu->per_cu->sect_off;
11544 struct dwarf2_section_info *section = cu->per_cu->section;
11545
11546 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11547 if (!signature.has_value ())
11548 {
11549 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11550 " its dwo_id [in module %s]"),
11551 sect_offset_str (sect_off), dwo_file->dwo_name);
11552 return;
11553 }
11554
11555 dwo_unit->dwo_file = dwo_file;
11556 dwo_unit->signature = *signature;
11557 dwo_unit->section = section;
11558 dwo_unit->sect_off = sect_off;
11559 dwo_unit->length = cu->per_cu->length;
11560
11561 if (dwarf_read_debug)
11562 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11563 sect_offset_str (sect_off),
11564 hex_string (dwo_unit->signature));
11565 }
11566
11567 /* Create the dwo_units for the CUs in a DWO_FILE.
11568 Note: This function processes DWO files only, not DWP files. */
11569
11570 static void
11571 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11572 dwarf2_cu *cu, struct dwo_file &dwo_file,
11573 dwarf2_section_info &section, htab_t &cus_htab)
11574 {
11575 struct objfile *objfile = dwarf2_per_objfile->objfile;
11576 const gdb_byte *info_ptr, *end_ptr;
11577
11578 section.read (objfile);
11579 info_ptr = section.buffer;
11580
11581 if (info_ptr == NULL)
11582 return;
11583
11584 if (dwarf_read_debug)
11585 {
11586 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11587 section.get_name (),
11588 section.get_file_name ());
11589 }
11590
11591 end_ptr = info_ptr + section.size;
11592 while (info_ptr < end_ptr)
11593 {
11594 struct dwarf2_per_cu_data per_cu;
11595 struct dwo_unit read_unit {};
11596 struct dwo_unit *dwo_unit;
11597 void **slot;
11598 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11599
11600 memset (&per_cu, 0, sizeof (per_cu));
11601 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11602 per_cu.is_debug_types = 0;
11603 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11604 per_cu.section = &section;
11605
11606 cutu_reader reader (&per_cu, cu, &dwo_file);
11607 if (!reader.dummy_p)
11608 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11609 reader.has_children, &dwo_file, &read_unit);
11610 info_ptr += per_cu.length;
11611
11612 // If the unit could not be parsed, skip it.
11613 if (read_unit.dwo_file == NULL)
11614 continue;
11615
11616 if (cus_htab == NULL)
11617 cus_htab = allocate_dwo_unit_table (objfile);
11618
11619 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11620 *dwo_unit = read_unit;
11621 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11622 gdb_assert (slot != NULL);
11623 if (*slot != NULL)
11624 {
11625 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11626 sect_offset dup_sect_off = dup_cu->sect_off;
11627
11628 complaint (_("debug cu entry at offset %s is duplicate to"
11629 " the entry at offset %s, signature %s"),
11630 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11631 hex_string (dwo_unit->signature));
11632 }
11633 *slot = (void *)dwo_unit;
11634 }
11635 }
11636
11637 /* DWP file .debug_{cu,tu}_index section format:
11638 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11639
11640 DWP Version 1:
11641
11642 Both index sections have the same format, and serve to map a 64-bit
11643 signature to a set of section numbers. Each section begins with a header,
11644 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11645 indexes, and a pool of 32-bit section numbers. The index sections will be
11646 aligned at 8-byte boundaries in the file.
11647
11648 The index section header consists of:
11649
11650 V, 32 bit version number
11651 -, 32 bits unused
11652 N, 32 bit number of compilation units or type units in the index
11653 M, 32 bit number of slots in the hash table
11654
11655 Numbers are recorded using the byte order of the application binary.
11656
11657 The hash table begins at offset 16 in the section, and consists of an array
11658 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11659 order of the application binary). Unused slots in the hash table are 0.
11660 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11661
11662 The parallel table begins immediately after the hash table
11663 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11664 array of 32-bit indexes (using the byte order of the application binary),
11665 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11666 table contains a 32-bit index into the pool of section numbers. For unused
11667 hash table slots, the corresponding entry in the parallel table will be 0.
11668
11669 The pool of section numbers begins immediately following the hash table
11670 (at offset 16 + 12 * M from the beginning of the section). The pool of
11671 section numbers consists of an array of 32-bit words (using the byte order
11672 of the application binary). Each item in the array is indexed starting
11673 from 0. The hash table entry provides the index of the first section
11674 number in the set. Additional section numbers in the set follow, and the
11675 set is terminated by a 0 entry (section number 0 is not used in ELF).
11676
11677 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11678 section must be the first entry in the set, and the .debug_abbrev.dwo must
11679 be the second entry. Other members of the set may follow in any order.
11680
11681 ---
11682
11683 DWP Version 2:
11684
11685 DWP Version 2 combines all the .debug_info, etc. sections into one,
11686 and the entries in the index tables are now offsets into these sections.
11687 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11688 section.
11689
11690 Index Section Contents:
11691 Header
11692 Hash Table of Signatures dwp_hash_table.hash_table
11693 Parallel Table of Indices dwp_hash_table.unit_table
11694 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11695 Table of Section Sizes dwp_hash_table.v2.sizes
11696
11697 The index section header consists of:
11698
11699 V, 32 bit version number
11700 L, 32 bit number of columns in the table of section offsets
11701 N, 32 bit number of compilation units or type units in the index
11702 M, 32 bit number of slots in the hash table
11703
11704 Numbers are recorded using the byte order of the application binary.
11705
11706 The hash table has the same format as version 1.
11707 The parallel table of indices has the same format as version 1,
11708 except that the entries are origin-1 indices into the table of sections
11709 offsets and the table of section sizes.
11710
11711 The table of offsets begins immediately following the parallel table
11712 (at offset 16 + 12 * M from the beginning of the section). The table is
11713 a two-dimensional array of 32-bit words (using the byte order of the
11714 application binary), with L columns and N+1 rows, in row-major order.
11715 Each row in the array is indexed starting from 0. The first row provides
11716 a key to the remaining rows: each column in this row provides an identifier
11717 for a debug section, and the offsets in the same column of subsequent rows
11718 refer to that section. The section identifiers are:
11719
11720 DW_SECT_INFO 1 .debug_info.dwo
11721 DW_SECT_TYPES 2 .debug_types.dwo
11722 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11723 DW_SECT_LINE 4 .debug_line.dwo
11724 DW_SECT_LOC 5 .debug_loc.dwo
11725 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11726 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11727 DW_SECT_MACRO 8 .debug_macro.dwo
11728
11729 The offsets provided by the CU and TU index sections are the base offsets
11730 for the contributions made by each CU or TU to the corresponding section
11731 in the package file. Each CU and TU header contains an abbrev_offset
11732 field, used to find the abbreviations table for that CU or TU within the
11733 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11734 be interpreted as relative to the base offset given in the index section.
11735 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11736 should be interpreted as relative to the base offset for .debug_line.dwo,
11737 and offsets into other debug sections obtained from DWARF attributes should
11738 also be interpreted as relative to the corresponding base offset.
11739
11740 The table of sizes begins immediately following the table of offsets.
11741 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11742 with L columns and N rows, in row-major order. Each row in the array is
11743 indexed starting from 1 (row 0 is shared by the two tables).
11744
11745 ---
11746
11747 Hash table lookup is handled the same in version 1 and 2:
11748
11749 We assume that N and M will not exceed 2^32 - 1.
11750 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11751
11752 Given a 64-bit compilation unit signature or a type signature S, an entry
11753 in the hash table is located as follows:
11754
11755 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11756 the low-order k bits all set to 1.
11757
11758 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11759
11760 3) If the hash table entry at index H matches the signature, use that
11761 entry. If the hash table entry at index H is unused (all zeroes),
11762 terminate the search: the signature is not present in the table.
11763
11764 4) Let H = (H + H') modulo M. Repeat at Step 3.
11765
11766 Because M > N and H' and M are relatively prime, the search is guaranteed
11767 to stop at an unused slot or find the match. */
11768
11769 /* Create a hash table to map DWO IDs to their CU/TU entry in
11770 .debug_{info,types}.dwo in DWP_FILE.
11771 Returns NULL if there isn't one.
11772 Note: This function processes DWP files only, not DWO files. */
11773
11774 static struct dwp_hash_table *
11775 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11776 struct dwp_file *dwp_file, int is_debug_types)
11777 {
11778 struct objfile *objfile = dwarf2_per_objfile->objfile;
11779 bfd *dbfd = dwp_file->dbfd.get ();
11780 const gdb_byte *index_ptr, *index_end;
11781 struct dwarf2_section_info *index;
11782 uint32_t version, nr_columns, nr_units, nr_slots;
11783 struct dwp_hash_table *htab;
11784
11785 if (is_debug_types)
11786 index = &dwp_file->sections.tu_index;
11787 else
11788 index = &dwp_file->sections.cu_index;
11789
11790 if (index->empty ())
11791 return NULL;
11792 index->read (objfile);
11793
11794 index_ptr = index->buffer;
11795 index_end = index_ptr + index->size;
11796
11797 version = read_4_bytes (dbfd, index_ptr);
11798 index_ptr += 4;
11799 if (version == 2)
11800 nr_columns = read_4_bytes (dbfd, index_ptr);
11801 else
11802 nr_columns = 0;
11803 index_ptr += 4;
11804 nr_units = read_4_bytes (dbfd, index_ptr);
11805 index_ptr += 4;
11806 nr_slots = read_4_bytes (dbfd, index_ptr);
11807 index_ptr += 4;
11808
11809 if (version != 1 && version != 2)
11810 {
11811 error (_("Dwarf Error: unsupported DWP file version (%s)"
11812 " [in module %s]"),
11813 pulongest (version), dwp_file->name);
11814 }
11815 if (nr_slots != (nr_slots & -nr_slots))
11816 {
11817 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11818 " is not power of 2 [in module %s]"),
11819 pulongest (nr_slots), dwp_file->name);
11820 }
11821
11822 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11823 htab->version = version;
11824 htab->nr_columns = nr_columns;
11825 htab->nr_units = nr_units;
11826 htab->nr_slots = nr_slots;
11827 htab->hash_table = index_ptr;
11828 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11829
11830 /* Exit early if the table is empty. */
11831 if (nr_slots == 0 || nr_units == 0
11832 || (version == 2 && nr_columns == 0))
11833 {
11834 /* All must be zero. */
11835 if (nr_slots != 0 || nr_units != 0
11836 || (version == 2 && nr_columns != 0))
11837 {
11838 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11839 " all zero [in modules %s]"),
11840 dwp_file->name);
11841 }
11842 return htab;
11843 }
11844
11845 if (version == 1)
11846 {
11847 htab->section_pool.v1.indices =
11848 htab->unit_table + sizeof (uint32_t) * nr_slots;
11849 /* It's harder to decide whether the section is too small in v1.
11850 V1 is deprecated anyway so we punt. */
11851 }
11852 else
11853 {
11854 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11855 int *ids = htab->section_pool.v2.section_ids;
11856 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11857 /* Reverse map for error checking. */
11858 int ids_seen[DW_SECT_MAX + 1];
11859 int i;
11860
11861 if (nr_columns < 2)
11862 {
11863 error (_("Dwarf Error: bad DWP hash table, too few columns"
11864 " in section table [in module %s]"),
11865 dwp_file->name);
11866 }
11867 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11868 {
11869 error (_("Dwarf Error: bad DWP hash table, too many columns"
11870 " in section table [in module %s]"),
11871 dwp_file->name);
11872 }
11873 memset (ids, 255, sizeof_ids);
11874 memset (ids_seen, 255, sizeof (ids_seen));
11875 for (i = 0; i < nr_columns; ++i)
11876 {
11877 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11878
11879 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11880 {
11881 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11882 " in section table [in module %s]"),
11883 id, dwp_file->name);
11884 }
11885 if (ids_seen[id] != -1)
11886 {
11887 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11888 " id %d in section table [in module %s]"),
11889 id, dwp_file->name);
11890 }
11891 ids_seen[id] = i;
11892 ids[i] = id;
11893 }
11894 /* Must have exactly one info or types section. */
11895 if (((ids_seen[DW_SECT_INFO] != -1)
11896 + (ids_seen[DW_SECT_TYPES] != -1))
11897 != 1)
11898 {
11899 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11900 " DWO info/types section [in module %s]"),
11901 dwp_file->name);
11902 }
11903 /* Must have an abbrev section. */
11904 if (ids_seen[DW_SECT_ABBREV] == -1)
11905 {
11906 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11907 " section [in module %s]"),
11908 dwp_file->name);
11909 }
11910 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11911 htab->section_pool.v2.sizes =
11912 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11913 * nr_units * nr_columns);
11914 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11915 * nr_units * nr_columns))
11916 > index_end)
11917 {
11918 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11919 " [in module %s]"),
11920 dwp_file->name);
11921 }
11922 }
11923
11924 return htab;
11925 }
11926
11927 /* Update SECTIONS with the data from SECTP.
11928
11929 This function is like the other "locate" section routines that are
11930 passed to bfd_map_over_sections, but in this context the sections to
11931 read comes from the DWP V1 hash table, not the full ELF section table.
11932
11933 The result is non-zero for success, or zero if an error was found. */
11934
11935 static int
11936 locate_v1_virtual_dwo_sections (asection *sectp,
11937 struct virtual_v1_dwo_sections *sections)
11938 {
11939 const struct dwop_section_names *names = &dwop_section_names;
11940
11941 if (section_is_p (sectp->name, &names->abbrev_dwo))
11942 {
11943 /* There can be only one. */
11944 if (sections->abbrev.s.section != NULL)
11945 return 0;
11946 sections->abbrev.s.section = sectp;
11947 sections->abbrev.size = bfd_section_size (sectp);
11948 }
11949 else if (section_is_p (sectp->name, &names->info_dwo)
11950 || section_is_p (sectp->name, &names->types_dwo))
11951 {
11952 /* There can be only one. */
11953 if (sections->info_or_types.s.section != NULL)
11954 return 0;
11955 sections->info_or_types.s.section = sectp;
11956 sections->info_or_types.size = bfd_section_size (sectp);
11957 }
11958 else if (section_is_p (sectp->name, &names->line_dwo))
11959 {
11960 /* There can be only one. */
11961 if (sections->line.s.section != NULL)
11962 return 0;
11963 sections->line.s.section = sectp;
11964 sections->line.size = bfd_section_size (sectp);
11965 }
11966 else if (section_is_p (sectp->name, &names->loc_dwo))
11967 {
11968 /* There can be only one. */
11969 if (sections->loc.s.section != NULL)
11970 return 0;
11971 sections->loc.s.section = sectp;
11972 sections->loc.size = bfd_section_size (sectp);
11973 }
11974 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11975 {
11976 /* There can be only one. */
11977 if (sections->macinfo.s.section != NULL)
11978 return 0;
11979 sections->macinfo.s.section = sectp;
11980 sections->macinfo.size = bfd_section_size (sectp);
11981 }
11982 else if (section_is_p (sectp->name, &names->macro_dwo))
11983 {
11984 /* There can be only one. */
11985 if (sections->macro.s.section != NULL)
11986 return 0;
11987 sections->macro.s.section = sectp;
11988 sections->macro.size = bfd_section_size (sectp);
11989 }
11990 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11991 {
11992 /* There can be only one. */
11993 if (sections->str_offsets.s.section != NULL)
11994 return 0;
11995 sections->str_offsets.s.section = sectp;
11996 sections->str_offsets.size = bfd_section_size (sectp);
11997 }
11998 else
11999 {
12000 /* No other kind of section is valid. */
12001 return 0;
12002 }
12003
12004 return 1;
12005 }
12006
12007 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12008 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12009 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12010 This is for DWP version 1 files. */
12011
12012 static struct dwo_unit *
12013 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12014 struct dwp_file *dwp_file,
12015 uint32_t unit_index,
12016 const char *comp_dir,
12017 ULONGEST signature, int is_debug_types)
12018 {
12019 struct objfile *objfile = dwarf2_per_objfile->objfile;
12020 const struct dwp_hash_table *dwp_htab =
12021 is_debug_types ? dwp_file->tus : dwp_file->cus;
12022 bfd *dbfd = dwp_file->dbfd.get ();
12023 const char *kind = is_debug_types ? "TU" : "CU";
12024 struct dwo_file *dwo_file;
12025 struct dwo_unit *dwo_unit;
12026 struct virtual_v1_dwo_sections sections;
12027 void **dwo_file_slot;
12028 int i;
12029
12030 gdb_assert (dwp_file->version == 1);
12031
12032 if (dwarf_read_debug)
12033 {
12034 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12035 kind,
12036 pulongest (unit_index), hex_string (signature),
12037 dwp_file->name);
12038 }
12039
12040 /* Fetch the sections of this DWO unit.
12041 Put a limit on the number of sections we look for so that bad data
12042 doesn't cause us to loop forever. */
12043
12044 #define MAX_NR_V1_DWO_SECTIONS \
12045 (1 /* .debug_info or .debug_types */ \
12046 + 1 /* .debug_abbrev */ \
12047 + 1 /* .debug_line */ \
12048 + 1 /* .debug_loc */ \
12049 + 1 /* .debug_str_offsets */ \
12050 + 1 /* .debug_macro or .debug_macinfo */ \
12051 + 1 /* trailing zero */)
12052
12053 memset (&sections, 0, sizeof (sections));
12054
12055 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12056 {
12057 asection *sectp;
12058 uint32_t section_nr =
12059 read_4_bytes (dbfd,
12060 dwp_htab->section_pool.v1.indices
12061 + (unit_index + i) * sizeof (uint32_t));
12062
12063 if (section_nr == 0)
12064 break;
12065 if (section_nr >= dwp_file->num_sections)
12066 {
12067 error (_("Dwarf Error: bad DWP hash table, section number too large"
12068 " [in module %s]"),
12069 dwp_file->name);
12070 }
12071
12072 sectp = dwp_file->elf_sections[section_nr];
12073 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12074 {
12075 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12076 " [in module %s]"),
12077 dwp_file->name);
12078 }
12079 }
12080
12081 if (i < 2
12082 || sections.info_or_types.empty ()
12083 || sections.abbrev.empty ())
12084 {
12085 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12086 " [in module %s]"),
12087 dwp_file->name);
12088 }
12089 if (i == MAX_NR_V1_DWO_SECTIONS)
12090 {
12091 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12092 " [in module %s]"),
12093 dwp_file->name);
12094 }
12095
12096 /* It's easier for the rest of the code if we fake a struct dwo_file and
12097 have dwo_unit "live" in that. At least for now.
12098
12099 The DWP file can be made up of a random collection of CUs and TUs.
12100 However, for each CU + set of TUs that came from the same original DWO
12101 file, we can combine them back into a virtual DWO file to save space
12102 (fewer struct dwo_file objects to allocate). Remember that for really
12103 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12104
12105 std::string virtual_dwo_name =
12106 string_printf ("virtual-dwo/%d-%d-%d-%d",
12107 sections.abbrev.get_id (),
12108 sections.line.get_id (),
12109 sections.loc.get_id (),
12110 sections.str_offsets.get_id ());
12111 /* Can we use an existing virtual DWO file? */
12112 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12113 virtual_dwo_name.c_str (),
12114 comp_dir);
12115 /* Create one if necessary. */
12116 if (*dwo_file_slot == NULL)
12117 {
12118 if (dwarf_read_debug)
12119 {
12120 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12121 virtual_dwo_name.c_str ());
12122 }
12123 dwo_file = new struct dwo_file;
12124 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12125 virtual_dwo_name);
12126 dwo_file->comp_dir = comp_dir;
12127 dwo_file->sections.abbrev = sections.abbrev;
12128 dwo_file->sections.line = sections.line;
12129 dwo_file->sections.loc = sections.loc;
12130 dwo_file->sections.macinfo = sections.macinfo;
12131 dwo_file->sections.macro = sections.macro;
12132 dwo_file->sections.str_offsets = sections.str_offsets;
12133 /* The "str" section is global to the entire DWP file. */
12134 dwo_file->sections.str = dwp_file->sections.str;
12135 /* The info or types section is assigned below to dwo_unit,
12136 there's no need to record it in dwo_file.
12137 Also, we can't simply record type sections in dwo_file because
12138 we record a pointer into the vector in dwo_unit. As we collect more
12139 types we'll grow the vector and eventually have to reallocate space
12140 for it, invalidating all copies of pointers into the previous
12141 contents. */
12142 *dwo_file_slot = dwo_file;
12143 }
12144 else
12145 {
12146 if (dwarf_read_debug)
12147 {
12148 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12149 virtual_dwo_name.c_str ());
12150 }
12151 dwo_file = (struct dwo_file *) *dwo_file_slot;
12152 }
12153
12154 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12155 dwo_unit->dwo_file = dwo_file;
12156 dwo_unit->signature = signature;
12157 dwo_unit->section =
12158 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12159 *dwo_unit->section = sections.info_or_types;
12160 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12161
12162 return dwo_unit;
12163 }
12164
12165 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12166 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12167 piece within that section used by a TU/CU, return a virtual section
12168 of just that piece. */
12169
12170 static struct dwarf2_section_info
12171 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12172 struct dwarf2_section_info *section,
12173 bfd_size_type offset, bfd_size_type size)
12174 {
12175 struct dwarf2_section_info result;
12176 asection *sectp;
12177
12178 gdb_assert (section != NULL);
12179 gdb_assert (!section->is_virtual);
12180
12181 memset (&result, 0, sizeof (result));
12182 result.s.containing_section = section;
12183 result.is_virtual = true;
12184
12185 if (size == 0)
12186 return result;
12187
12188 sectp = section->get_bfd_section ();
12189
12190 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12191 bounds of the real section. This is a pretty-rare event, so just
12192 flag an error (easier) instead of a warning and trying to cope. */
12193 if (sectp == NULL
12194 || offset + size > bfd_section_size (sectp))
12195 {
12196 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12197 " in section %s [in module %s]"),
12198 sectp ? bfd_section_name (sectp) : "<unknown>",
12199 objfile_name (dwarf2_per_objfile->objfile));
12200 }
12201
12202 result.virtual_offset = offset;
12203 result.size = size;
12204 return result;
12205 }
12206
12207 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12208 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12209 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12210 This is for DWP version 2 files. */
12211
12212 static struct dwo_unit *
12213 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12214 struct dwp_file *dwp_file,
12215 uint32_t unit_index,
12216 const char *comp_dir,
12217 ULONGEST signature, int is_debug_types)
12218 {
12219 struct objfile *objfile = dwarf2_per_objfile->objfile;
12220 const struct dwp_hash_table *dwp_htab =
12221 is_debug_types ? dwp_file->tus : dwp_file->cus;
12222 bfd *dbfd = dwp_file->dbfd.get ();
12223 const char *kind = is_debug_types ? "TU" : "CU";
12224 struct dwo_file *dwo_file;
12225 struct dwo_unit *dwo_unit;
12226 struct virtual_v2_dwo_sections sections;
12227 void **dwo_file_slot;
12228 int i;
12229
12230 gdb_assert (dwp_file->version == 2);
12231
12232 if (dwarf_read_debug)
12233 {
12234 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12235 kind,
12236 pulongest (unit_index), hex_string (signature),
12237 dwp_file->name);
12238 }
12239
12240 /* Fetch the section offsets of this DWO unit. */
12241
12242 memset (&sections, 0, sizeof (sections));
12243
12244 for (i = 0; i < dwp_htab->nr_columns; ++i)
12245 {
12246 uint32_t offset = read_4_bytes (dbfd,
12247 dwp_htab->section_pool.v2.offsets
12248 + (((unit_index - 1) * dwp_htab->nr_columns
12249 + i)
12250 * sizeof (uint32_t)));
12251 uint32_t size = read_4_bytes (dbfd,
12252 dwp_htab->section_pool.v2.sizes
12253 + (((unit_index - 1) * dwp_htab->nr_columns
12254 + i)
12255 * sizeof (uint32_t)));
12256
12257 switch (dwp_htab->section_pool.v2.section_ids[i])
12258 {
12259 case DW_SECT_INFO:
12260 case DW_SECT_TYPES:
12261 sections.info_or_types_offset = offset;
12262 sections.info_or_types_size = size;
12263 break;
12264 case DW_SECT_ABBREV:
12265 sections.abbrev_offset = offset;
12266 sections.abbrev_size = size;
12267 break;
12268 case DW_SECT_LINE:
12269 sections.line_offset = offset;
12270 sections.line_size = size;
12271 break;
12272 case DW_SECT_LOC:
12273 sections.loc_offset = offset;
12274 sections.loc_size = size;
12275 break;
12276 case DW_SECT_STR_OFFSETS:
12277 sections.str_offsets_offset = offset;
12278 sections.str_offsets_size = size;
12279 break;
12280 case DW_SECT_MACINFO:
12281 sections.macinfo_offset = offset;
12282 sections.macinfo_size = size;
12283 break;
12284 case DW_SECT_MACRO:
12285 sections.macro_offset = offset;
12286 sections.macro_size = size;
12287 break;
12288 }
12289 }
12290
12291 /* It's easier for the rest of the code if we fake a struct dwo_file and
12292 have dwo_unit "live" in that. At least for now.
12293
12294 The DWP file can be made up of a random collection of CUs and TUs.
12295 However, for each CU + set of TUs that came from the same original DWO
12296 file, we can combine them back into a virtual DWO file to save space
12297 (fewer struct dwo_file objects to allocate). Remember that for really
12298 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12299
12300 std::string virtual_dwo_name =
12301 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12302 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12303 (long) (sections.line_size ? sections.line_offset : 0),
12304 (long) (sections.loc_size ? sections.loc_offset : 0),
12305 (long) (sections.str_offsets_size
12306 ? sections.str_offsets_offset : 0));
12307 /* Can we use an existing virtual DWO file? */
12308 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12309 virtual_dwo_name.c_str (),
12310 comp_dir);
12311 /* Create one if necessary. */
12312 if (*dwo_file_slot == NULL)
12313 {
12314 if (dwarf_read_debug)
12315 {
12316 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12317 virtual_dwo_name.c_str ());
12318 }
12319 dwo_file = new struct dwo_file;
12320 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12321 virtual_dwo_name);
12322 dwo_file->comp_dir = comp_dir;
12323 dwo_file->sections.abbrev =
12324 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12325 sections.abbrev_offset, sections.abbrev_size);
12326 dwo_file->sections.line =
12327 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12328 sections.line_offset, sections.line_size);
12329 dwo_file->sections.loc =
12330 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12331 sections.loc_offset, sections.loc_size);
12332 dwo_file->sections.macinfo =
12333 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12334 sections.macinfo_offset, sections.macinfo_size);
12335 dwo_file->sections.macro =
12336 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12337 sections.macro_offset, sections.macro_size);
12338 dwo_file->sections.str_offsets =
12339 create_dwp_v2_section (dwarf2_per_objfile,
12340 &dwp_file->sections.str_offsets,
12341 sections.str_offsets_offset,
12342 sections.str_offsets_size);
12343 /* The "str" section is global to the entire DWP file. */
12344 dwo_file->sections.str = dwp_file->sections.str;
12345 /* The info or types section is assigned below to dwo_unit,
12346 there's no need to record it in dwo_file.
12347 Also, we can't simply record type sections in dwo_file because
12348 we record a pointer into the vector in dwo_unit. As we collect more
12349 types we'll grow the vector and eventually have to reallocate space
12350 for it, invalidating all copies of pointers into the previous
12351 contents. */
12352 *dwo_file_slot = dwo_file;
12353 }
12354 else
12355 {
12356 if (dwarf_read_debug)
12357 {
12358 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12359 virtual_dwo_name.c_str ());
12360 }
12361 dwo_file = (struct dwo_file *) *dwo_file_slot;
12362 }
12363
12364 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12365 dwo_unit->dwo_file = dwo_file;
12366 dwo_unit->signature = signature;
12367 dwo_unit->section =
12368 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12369 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12370 is_debug_types
12371 ? &dwp_file->sections.types
12372 : &dwp_file->sections.info,
12373 sections.info_or_types_offset,
12374 sections.info_or_types_size);
12375 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12376
12377 return dwo_unit;
12378 }
12379
12380 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12381 Returns NULL if the signature isn't found. */
12382
12383 static struct dwo_unit *
12384 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12385 struct dwp_file *dwp_file, const char *comp_dir,
12386 ULONGEST signature, int is_debug_types)
12387 {
12388 const struct dwp_hash_table *dwp_htab =
12389 is_debug_types ? dwp_file->tus : dwp_file->cus;
12390 bfd *dbfd = dwp_file->dbfd.get ();
12391 uint32_t mask = dwp_htab->nr_slots - 1;
12392 uint32_t hash = signature & mask;
12393 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12394 unsigned int i;
12395 void **slot;
12396 struct dwo_unit find_dwo_cu;
12397
12398 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12399 find_dwo_cu.signature = signature;
12400 slot = htab_find_slot (is_debug_types
12401 ? dwp_file->loaded_tus
12402 : dwp_file->loaded_cus,
12403 &find_dwo_cu, INSERT);
12404
12405 if (*slot != NULL)
12406 return (struct dwo_unit *) *slot;
12407
12408 /* Use a for loop so that we don't loop forever on bad debug info. */
12409 for (i = 0; i < dwp_htab->nr_slots; ++i)
12410 {
12411 ULONGEST signature_in_table;
12412
12413 signature_in_table =
12414 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12415 if (signature_in_table == signature)
12416 {
12417 uint32_t unit_index =
12418 read_4_bytes (dbfd,
12419 dwp_htab->unit_table + hash * sizeof (uint32_t));
12420
12421 if (dwp_file->version == 1)
12422 {
12423 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12424 dwp_file, unit_index,
12425 comp_dir, signature,
12426 is_debug_types);
12427 }
12428 else
12429 {
12430 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12431 dwp_file, unit_index,
12432 comp_dir, signature,
12433 is_debug_types);
12434 }
12435 return (struct dwo_unit *) *slot;
12436 }
12437 if (signature_in_table == 0)
12438 return NULL;
12439 hash = (hash + hash2) & mask;
12440 }
12441
12442 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12443 " [in module %s]"),
12444 dwp_file->name);
12445 }
12446
12447 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12448 Open the file specified by FILE_NAME and hand it off to BFD for
12449 preliminary analysis. Return a newly initialized bfd *, which
12450 includes a canonicalized copy of FILE_NAME.
12451 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12452 SEARCH_CWD is true if the current directory is to be searched.
12453 It will be searched before debug-file-directory.
12454 If successful, the file is added to the bfd include table of the
12455 objfile's bfd (see gdb_bfd_record_inclusion).
12456 If unable to find/open the file, return NULL.
12457 NOTE: This function is derived from symfile_bfd_open. */
12458
12459 static gdb_bfd_ref_ptr
12460 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12461 const char *file_name, int is_dwp, int search_cwd)
12462 {
12463 int desc;
12464 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12465 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12466 to debug_file_directory. */
12467 const char *search_path;
12468 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12469
12470 gdb::unique_xmalloc_ptr<char> search_path_holder;
12471 if (search_cwd)
12472 {
12473 if (*debug_file_directory != '\0')
12474 {
12475 search_path_holder.reset (concat (".", dirname_separator_string,
12476 debug_file_directory,
12477 (char *) NULL));
12478 search_path = search_path_holder.get ();
12479 }
12480 else
12481 search_path = ".";
12482 }
12483 else
12484 search_path = debug_file_directory;
12485
12486 openp_flags flags = OPF_RETURN_REALPATH;
12487 if (is_dwp)
12488 flags |= OPF_SEARCH_IN_PATH;
12489
12490 gdb::unique_xmalloc_ptr<char> absolute_name;
12491 desc = openp (search_path, flags, file_name,
12492 O_RDONLY | O_BINARY, &absolute_name);
12493 if (desc < 0)
12494 return NULL;
12495
12496 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12497 gnutarget, desc));
12498 if (sym_bfd == NULL)
12499 return NULL;
12500 bfd_set_cacheable (sym_bfd.get (), 1);
12501
12502 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12503 return NULL;
12504
12505 /* Success. Record the bfd as having been included by the objfile's bfd.
12506 This is important because things like demangled_names_hash lives in the
12507 objfile's per_bfd space and may have references to things like symbol
12508 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12509 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12510
12511 return sym_bfd;
12512 }
12513
12514 /* Try to open DWO file FILE_NAME.
12515 COMP_DIR is the DW_AT_comp_dir attribute.
12516 The result is the bfd handle of the file.
12517 If there is a problem finding or opening the file, return NULL.
12518 Upon success, the canonicalized path of the file is stored in the bfd,
12519 same as symfile_bfd_open. */
12520
12521 static gdb_bfd_ref_ptr
12522 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12523 const char *file_name, const char *comp_dir)
12524 {
12525 if (IS_ABSOLUTE_PATH (file_name))
12526 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12527 0 /*is_dwp*/, 0 /*search_cwd*/);
12528
12529 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12530
12531 if (comp_dir != NULL)
12532 {
12533 gdb::unique_xmalloc_ptr<char> path_to_try
12534 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12535
12536 /* NOTE: If comp_dir is a relative path, this will also try the
12537 search path, which seems useful. */
12538 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12539 path_to_try.get (),
12540 0 /*is_dwp*/,
12541 1 /*search_cwd*/));
12542 if (abfd != NULL)
12543 return abfd;
12544 }
12545
12546 /* That didn't work, try debug-file-directory, which, despite its name,
12547 is a list of paths. */
12548
12549 if (*debug_file_directory == '\0')
12550 return NULL;
12551
12552 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12553 0 /*is_dwp*/, 1 /*search_cwd*/);
12554 }
12555
12556 /* This function is mapped across the sections and remembers the offset and
12557 size of each of the DWO debugging sections we are interested in. */
12558
12559 static void
12560 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12561 {
12562 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12563 const struct dwop_section_names *names = &dwop_section_names;
12564
12565 if (section_is_p (sectp->name, &names->abbrev_dwo))
12566 {
12567 dwo_sections->abbrev.s.section = sectp;
12568 dwo_sections->abbrev.size = bfd_section_size (sectp);
12569 }
12570 else if (section_is_p (sectp->name, &names->info_dwo))
12571 {
12572 dwo_sections->info.s.section = sectp;
12573 dwo_sections->info.size = bfd_section_size (sectp);
12574 }
12575 else if (section_is_p (sectp->name, &names->line_dwo))
12576 {
12577 dwo_sections->line.s.section = sectp;
12578 dwo_sections->line.size = bfd_section_size (sectp);
12579 }
12580 else if (section_is_p (sectp->name, &names->loc_dwo))
12581 {
12582 dwo_sections->loc.s.section = sectp;
12583 dwo_sections->loc.size = bfd_section_size (sectp);
12584 }
12585 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12586 {
12587 dwo_sections->macinfo.s.section = sectp;
12588 dwo_sections->macinfo.size = bfd_section_size (sectp);
12589 }
12590 else if (section_is_p (sectp->name, &names->macro_dwo))
12591 {
12592 dwo_sections->macro.s.section = sectp;
12593 dwo_sections->macro.size = bfd_section_size (sectp);
12594 }
12595 else if (section_is_p (sectp->name, &names->str_dwo))
12596 {
12597 dwo_sections->str.s.section = sectp;
12598 dwo_sections->str.size = bfd_section_size (sectp);
12599 }
12600 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12601 {
12602 dwo_sections->str_offsets.s.section = sectp;
12603 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12604 }
12605 else if (section_is_p (sectp->name, &names->types_dwo))
12606 {
12607 struct dwarf2_section_info type_section;
12608
12609 memset (&type_section, 0, sizeof (type_section));
12610 type_section.s.section = sectp;
12611 type_section.size = bfd_section_size (sectp);
12612 dwo_sections->types.push_back (type_section);
12613 }
12614 }
12615
12616 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12617 by PER_CU. This is for the non-DWP case.
12618 The result is NULL if DWO_NAME can't be found. */
12619
12620 static struct dwo_file *
12621 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12622 const char *dwo_name, const char *comp_dir)
12623 {
12624 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12625
12626 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12627 if (dbfd == NULL)
12628 {
12629 if (dwarf_read_debug)
12630 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12631 return NULL;
12632 }
12633
12634 dwo_file_up dwo_file (new struct dwo_file);
12635 dwo_file->dwo_name = dwo_name;
12636 dwo_file->comp_dir = comp_dir;
12637 dwo_file->dbfd = std::move (dbfd);
12638
12639 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12640 &dwo_file->sections);
12641
12642 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12643 dwo_file->sections.info, dwo_file->cus);
12644
12645 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12646 dwo_file->sections.types, dwo_file->tus);
12647
12648 if (dwarf_read_debug)
12649 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12650
12651 return dwo_file.release ();
12652 }
12653
12654 /* This function is mapped across the sections and remembers the offset and
12655 size of each of the DWP debugging sections common to version 1 and 2 that
12656 we are interested in. */
12657
12658 static void
12659 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12660 void *dwp_file_ptr)
12661 {
12662 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12663 const struct dwop_section_names *names = &dwop_section_names;
12664 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12665
12666 /* Record the ELF section number for later lookup: this is what the
12667 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12668 gdb_assert (elf_section_nr < dwp_file->num_sections);
12669 dwp_file->elf_sections[elf_section_nr] = sectp;
12670
12671 /* Look for specific sections that we need. */
12672 if (section_is_p (sectp->name, &names->str_dwo))
12673 {
12674 dwp_file->sections.str.s.section = sectp;
12675 dwp_file->sections.str.size = bfd_section_size (sectp);
12676 }
12677 else if (section_is_p (sectp->name, &names->cu_index))
12678 {
12679 dwp_file->sections.cu_index.s.section = sectp;
12680 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12681 }
12682 else if (section_is_p (sectp->name, &names->tu_index))
12683 {
12684 dwp_file->sections.tu_index.s.section = sectp;
12685 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12686 }
12687 }
12688
12689 /* This function is mapped across the sections and remembers the offset and
12690 size of each of the DWP version 2 debugging sections that we are interested
12691 in. This is split into a separate function because we don't know if we
12692 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12693
12694 static void
12695 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12696 {
12697 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12698 const struct dwop_section_names *names = &dwop_section_names;
12699 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12700
12701 /* Record the ELF section number for later lookup: this is what the
12702 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12703 gdb_assert (elf_section_nr < dwp_file->num_sections);
12704 dwp_file->elf_sections[elf_section_nr] = sectp;
12705
12706 /* Look for specific sections that we need. */
12707 if (section_is_p (sectp->name, &names->abbrev_dwo))
12708 {
12709 dwp_file->sections.abbrev.s.section = sectp;
12710 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12711 }
12712 else if (section_is_p (sectp->name, &names->info_dwo))
12713 {
12714 dwp_file->sections.info.s.section = sectp;
12715 dwp_file->sections.info.size = bfd_section_size (sectp);
12716 }
12717 else if (section_is_p (sectp->name, &names->line_dwo))
12718 {
12719 dwp_file->sections.line.s.section = sectp;
12720 dwp_file->sections.line.size = bfd_section_size (sectp);
12721 }
12722 else if (section_is_p (sectp->name, &names->loc_dwo))
12723 {
12724 dwp_file->sections.loc.s.section = sectp;
12725 dwp_file->sections.loc.size = bfd_section_size (sectp);
12726 }
12727 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12728 {
12729 dwp_file->sections.macinfo.s.section = sectp;
12730 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12731 }
12732 else if (section_is_p (sectp->name, &names->macro_dwo))
12733 {
12734 dwp_file->sections.macro.s.section = sectp;
12735 dwp_file->sections.macro.size = bfd_section_size (sectp);
12736 }
12737 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12738 {
12739 dwp_file->sections.str_offsets.s.section = sectp;
12740 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12741 }
12742 else if (section_is_p (sectp->name, &names->types_dwo))
12743 {
12744 dwp_file->sections.types.s.section = sectp;
12745 dwp_file->sections.types.size = bfd_section_size (sectp);
12746 }
12747 }
12748
12749 /* Hash function for dwp_file loaded CUs/TUs. */
12750
12751 static hashval_t
12752 hash_dwp_loaded_cutus (const void *item)
12753 {
12754 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12755
12756 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12757 return dwo_unit->signature;
12758 }
12759
12760 /* Equality function for dwp_file loaded CUs/TUs. */
12761
12762 static int
12763 eq_dwp_loaded_cutus (const void *a, const void *b)
12764 {
12765 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12766 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12767
12768 return dua->signature == dub->signature;
12769 }
12770
12771 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12772
12773 static htab_t
12774 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12775 {
12776 return htab_create_alloc_ex (3,
12777 hash_dwp_loaded_cutus,
12778 eq_dwp_loaded_cutus,
12779 NULL,
12780 &objfile->objfile_obstack,
12781 hashtab_obstack_allocate,
12782 dummy_obstack_deallocate);
12783 }
12784
12785 /* Try to open DWP file FILE_NAME.
12786 The result is the bfd handle of the file.
12787 If there is a problem finding or opening the file, return NULL.
12788 Upon success, the canonicalized path of the file is stored in the bfd,
12789 same as symfile_bfd_open. */
12790
12791 static gdb_bfd_ref_ptr
12792 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12793 const char *file_name)
12794 {
12795 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12796 1 /*is_dwp*/,
12797 1 /*search_cwd*/));
12798 if (abfd != NULL)
12799 return abfd;
12800
12801 /* Work around upstream bug 15652.
12802 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12803 [Whether that's a "bug" is debatable, but it is getting in our way.]
12804 We have no real idea where the dwp file is, because gdb's realpath-ing
12805 of the executable's path may have discarded the needed info.
12806 [IWBN if the dwp file name was recorded in the executable, akin to
12807 .gnu_debuglink, but that doesn't exist yet.]
12808 Strip the directory from FILE_NAME and search again. */
12809 if (*debug_file_directory != '\0')
12810 {
12811 /* Don't implicitly search the current directory here.
12812 If the user wants to search "." to handle this case,
12813 it must be added to debug-file-directory. */
12814 return try_open_dwop_file (dwarf2_per_objfile,
12815 lbasename (file_name), 1 /*is_dwp*/,
12816 0 /*search_cwd*/);
12817 }
12818
12819 return NULL;
12820 }
12821
12822 /* Initialize the use of the DWP file for the current objfile.
12823 By convention the name of the DWP file is ${objfile}.dwp.
12824 The result is NULL if it can't be found. */
12825
12826 static std::unique_ptr<struct dwp_file>
12827 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12828 {
12829 struct objfile *objfile = dwarf2_per_objfile->objfile;
12830
12831 /* Try to find first .dwp for the binary file before any symbolic links
12832 resolving. */
12833
12834 /* If the objfile is a debug file, find the name of the real binary
12835 file and get the name of dwp file from there. */
12836 std::string dwp_name;
12837 if (objfile->separate_debug_objfile_backlink != NULL)
12838 {
12839 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12840 const char *backlink_basename = lbasename (backlink->original_name);
12841
12842 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12843 }
12844 else
12845 dwp_name = objfile->original_name;
12846
12847 dwp_name += ".dwp";
12848
12849 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12850 if (dbfd == NULL
12851 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12852 {
12853 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12854 dwp_name = objfile_name (objfile);
12855 dwp_name += ".dwp";
12856 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12857 }
12858
12859 if (dbfd == NULL)
12860 {
12861 if (dwarf_read_debug)
12862 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12863 return std::unique_ptr<dwp_file> ();
12864 }
12865
12866 const char *name = bfd_get_filename (dbfd.get ());
12867 std::unique_ptr<struct dwp_file> dwp_file
12868 (new struct dwp_file (name, std::move (dbfd)));
12869
12870 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12871 dwp_file->elf_sections =
12872 OBSTACK_CALLOC (&objfile->objfile_obstack,
12873 dwp_file->num_sections, asection *);
12874
12875 bfd_map_over_sections (dwp_file->dbfd.get (),
12876 dwarf2_locate_common_dwp_sections,
12877 dwp_file.get ());
12878
12879 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12880 0);
12881
12882 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12883 1);
12884
12885 /* The DWP file version is stored in the hash table. Oh well. */
12886 if (dwp_file->cus && dwp_file->tus
12887 && dwp_file->cus->version != dwp_file->tus->version)
12888 {
12889 /* Technically speaking, we should try to limp along, but this is
12890 pretty bizarre. We use pulongest here because that's the established
12891 portability solution (e.g, we cannot use %u for uint32_t). */
12892 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12893 " TU version %s [in DWP file %s]"),
12894 pulongest (dwp_file->cus->version),
12895 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12896 }
12897
12898 if (dwp_file->cus)
12899 dwp_file->version = dwp_file->cus->version;
12900 else if (dwp_file->tus)
12901 dwp_file->version = dwp_file->tus->version;
12902 else
12903 dwp_file->version = 2;
12904
12905 if (dwp_file->version == 2)
12906 bfd_map_over_sections (dwp_file->dbfd.get (),
12907 dwarf2_locate_v2_dwp_sections,
12908 dwp_file.get ());
12909
12910 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12911 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12912
12913 if (dwarf_read_debug)
12914 {
12915 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12916 fprintf_unfiltered (gdb_stdlog,
12917 " %s CUs, %s TUs\n",
12918 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12919 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12920 }
12921
12922 return dwp_file;
12923 }
12924
12925 /* Wrapper around open_and_init_dwp_file, only open it once. */
12926
12927 static struct dwp_file *
12928 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12929 {
12930 if (! dwarf2_per_objfile->dwp_checked)
12931 {
12932 dwarf2_per_objfile->dwp_file
12933 = open_and_init_dwp_file (dwarf2_per_objfile);
12934 dwarf2_per_objfile->dwp_checked = 1;
12935 }
12936 return dwarf2_per_objfile->dwp_file.get ();
12937 }
12938
12939 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12940 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12941 or in the DWP file for the objfile, referenced by THIS_UNIT.
12942 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12943 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12944
12945 This is called, for example, when wanting to read a variable with a
12946 complex location. Therefore we don't want to do file i/o for every call.
12947 Therefore we don't want to look for a DWO file on every call.
12948 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12949 then we check if we've already seen DWO_NAME, and only THEN do we check
12950 for a DWO file.
12951
12952 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12953 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12954
12955 static struct dwo_unit *
12956 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12957 const char *dwo_name, const char *comp_dir,
12958 ULONGEST signature, int is_debug_types)
12959 {
12960 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12961 struct objfile *objfile = dwarf2_per_objfile->objfile;
12962 const char *kind = is_debug_types ? "TU" : "CU";
12963 void **dwo_file_slot;
12964 struct dwo_file *dwo_file;
12965 struct dwp_file *dwp_file;
12966
12967 /* First see if there's a DWP file.
12968 If we have a DWP file but didn't find the DWO inside it, don't
12969 look for the original DWO file. It makes gdb behave differently
12970 depending on whether one is debugging in the build tree. */
12971
12972 dwp_file = get_dwp_file (dwarf2_per_objfile);
12973 if (dwp_file != NULL)
12974 {
12975 const struct dwp_hash_table *dwp_htab =
12976 is_debug_types ? dwp_file->tus : dwp_file->cus;
12977
12978 if (dwp_htab != NULL)
12979 {
12980 struct dwo_unit *dwo_cutu =
12981 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12982 signature, is_debug_types);
12983
12984 if (dwo_cutu != NULL)
12985 {
12986 if (dwarf_read_debug)
12987 {
12988 fprintf_unfiltered (gdb_stdlog,
12989 "Virtual DWO %s %s found: @%s\n",
12990 kind, hex_string (signature),
12991 host_address_to_string (dwo_cutu));
12992 }
12993 return dwo_cutu;
12994 }
12995 }
12996 }
12997 else
12998 {
12999 /* No DWP file, look for the DWO file. */
13000
13001 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13002 dwo_name, comp_dir);
13003 if (*dwo_file_slot == NULL)
13004 {
13005 /* Read in the file and build a table of the CUs/TUs it contains. */
13006 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13007 }
13008 /* NOTE: This will be NULL if unable to open the file. */
13009 dwo_file = (struct dwo_file *) *dwo_file_slot;
13010
13011 if (dwo_file != NULL)
13012 {
13013 struct dwo_unit *dwo_cutu = NULL;
13014
13015 if (is_debug_types && dwo_file->tus)
13016 {
13017 struct dwo_unit find_dwo_cutu;
13018
13019 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13020 find_dwo_cutu.signature = signature;
13021 dwo_cutu
13022 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13023 }
13024 else if (!is_debug_types && dwo_file->cus)
13025 {
13026 struct dwo_unit find_dwo_cutu;
13027
13028 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13029 find_dwo_cutu.signature = signature;
13030 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13031 &find_dwo_cutu);
13032 }
13033
13034 if (dwo_cutu != NULL)
13035 {
13036 if (dwarf_read_debug)
13037 {
13038 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13039 kind, dwo_name, hex_string (signature),
13040 host_address_to_string (dwo_cutu));
13041 }
13042 return dwo_cutu;
13043 }
13044 }
13045 }
13046
13047 /* We didn't find it. This could mean a dwo_id mismatch, or
13048 someone deleted the DWO/DWP file, or the search path isn't set up
13049 correctly to find the file. */
13050
13051 if (dwarf_read_debug)
13052 {
13053 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13054 kind, dwo_name, hex_string (signature));
13055 }
13056
13057 /* This is a warning and not a complaint because it can be caused by
13058 pilot error (e.g., user accidentally deleting the DWO). */
13059 {
13060 /* Print the name of the DWP file if we looked there, helps the user
13061 better diagnose the problem. */
13062 std::string dwp_text;
13063
13064 if (dwp_file != NULL)
13065 dwp_text = string_printf (" [in DWP file %s]",
13066 lbasename (dwp_file->name));
13067
13068 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13069 " [in module %s]"),
13070 kind, dwo_name, hex_string (signature),
13071 dwp_text.c_str (),
13072 this_unit->is_debug_types ? "TU" : "CU",
13073 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13074 }
13075 return NULL;
13076 }
13077
13078 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13079 See lookup_dwo_cutu_unit for details. */
13080
13081 static struct dwo_unit *
13082 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13083 const char *dwo_name, const char *comp_dir,
13084 ULONGEST signature)
13085 {
13086 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13087 }
13088
13089 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13090 See lookup_dwo_cutu_unit for details. */
13091
13092 static struct dwo_unit *
13093 lookup_dwo_type_unit (struct signatured_type *this_tu,
13094 const char *dwo_name, const char *comp_dir)
13095 {
13096 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13097 }
13098
13099 /* Traversal function for queue_and_load_all_dwo_tus. */
13100
13101 static int
13102 queue_and_load_dwo_tu (void **slot, void *info)
13103 {
13104 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13105 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13106 ULONGEST signature = dwo_unit->signature;
13107 struct signatured_type *sig_type =
13108 lookup_dwo_signatured_type (per_cu->cu, signature);
13109
13110 if (sig_type != NULL)
13111 {
13112 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13113
13114 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13115 a real dependency of PER_CU on SIG_TYPE. That is detected later
13116 while processing PER_CU. */
13117 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13118 load_full_type_unit (sig_cu);
13119 per_cu->imported_symtabs_push (sig_cu);
13120 }
13121
13122 return 1;
13123 }
13124
13125 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13126 The DWO may have the only definition of the type, though it may not be
13127 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13128 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13129
13130 static void
13131 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13132 {
13133 struct dwo_unit *dwo_unit;
13134 struct dwo_file *dwo_file;
13135
13136 gdb_assert (!per_cu->is_debug_types);
13137 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13138 gdb_assert (per_cu->cu != NULL);
13139
13140 dwo_unit = per_cu->cu->dwo_unit;
13141 gdb_assert (dwo_unit != NULL);
13142
13143 dwo_file = dwo_unit->dwo_file;
13144 if (dwo_file->tus != NULL)
13145 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13146 }
13147
13148 /* Read in various DIEs. */
13149
13150 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13151 Inherit only the children of the DW_AT_abstract_origin DIE not being
13152 already referenced by DW_AT_abstract_origin from the children of the
13153 current DIE. */
13154
13155 static void
13156 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13157 {
13158 struct die_info *child_die;
13159 sect_offset *offsetp;
13160 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13161 struct die_info *origin_die;
13162 /* Iterator of the ORIGIN_DIE children. */
13163 struct die_info *origin_child_die;
13164 struct attribute *attr;
13165 struct dwarf2_cu *origin_cu;
13166 struct pending **origin_previous_list_in_scope;
13167
13168 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13169 if (!attr)
13170 return;
13171
13172 /* Note that following die references may follow to a die in a
13173 different cu. */
13174
13175 origin_cu = cu;
13176 origin_die = follow_die_ref (die, attr, &origin_cu);
13177
13178 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13179 symbols in. */
13180 origin_previous_list_in_scope = origin_cu->list_in_scope;
13181 origin_cu->list_in_scope = cu->list_in_scope;
13182
13183 if (die->tag != origin_die->tag
13184 && !(die->tag == DW_TAG_inlined_subroutine
13185 && origin_die->tag == DW_TAG_subprogram))
13186 complaint (_("DIE %s and its abstract origin %s have different tags"),
13187 sect_offset_str (die->sect_off),
13188 sect_offset_str (origin_die->sect_off));
13189
13190 std::vector<sect_offset> offsets;
13191
13192 for (child_die = die->child;
13193 child_die && child_die->tag;
13194 child_die = sibling_die (child_die))
13195 {
13196 struct die_info *child_origin_die;
13197 struct dwarf2_cu *child_origin_cu;
13198
13199 /* We are trying to process concrete instance entries:
13200 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13201 it's not relevant to our analysis here. i.e. detecting DIEs that are
13202 present in the abstract instance but not referenced in the concrete
13203 one. */
13204 if (child_die->tag == DW_TAG_call_site
13205 || child_die->tag == DW_TAG_GNU_call_site)
13206 continue;
13207
13208 /* For each CHILD_DIE, find the corresponding child of
13209 ORIGIN_DIE. If there is more than one layer of
13210 DW_AT_abstract_origin, follow them all; there shouldn't be,
13211 but GCC versions at least through 4.4 generate this (GCC PR
13212 40573). */
13213 child_origin_die = child_die;
13214 child_origin_cu = cu;
13215 while (1)
13216 {
13217 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13218 child_origin_cu);
13219 if (attr == NULL)
13220 break;
13221 child_origin_die = follow_die_ref (child_origin_die, attr,
13222 &child_origin_cu);
13223 }
13224
13225 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13226 counterpart may exist. */
13227 if (child_origin_die != child_die)
13228 {
13229 if (child_die->tag != child_origin_die->tag
13230 && !(child_die->tag == DW_TAG_inlined_subroutine
13231 && child_origin_die->tag == DW_TAG_subprogram))
13232 complaint (_("Child DIE %s and its abstract origin %s have "
13233 "different tags"),
13234 sect_offset_str (child_die->sect_off),
13235 sect_offset_str (child_origin_die->sect_off));
13236 if (child_origin_die->parent != origin_die)
13237 complaint (_("Child DIE %s and its abstract origin %s have "
13238 "different parents"),
13239 sect_offset_str (child_die->sect_off),
13240 sect_offset_str (child_origin_die->sect_off));
13241 else
13242 offsets.push_back (child_origin_die->sect_off);
13243 }
13244 }
13245 std::sort (offsets.begin (), offsets.end ());
13246 sect_offset *offsets_end = offsets.data () + offsets.size ();
13247 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13248 if (offsetp[-1] == *offsetp)
13249 complaint (_("Multiple children of DIE %s refer "
13250 "to DIE %s as their abstract origin"),
13251 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13252
13253 offsetp = offsets.data ();
13254 origin_child_die = origin_die->child;
13255 while (origin_child_die && origin_child_die->tag)
13256 {
13257 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13258 while (offsetp < offsets_end
13259 && *offsetp < origin_child_die->sect_off)
13260 offsetp++;
13261 if (offsetp >= offsets_end
13262 || *offsetp > origin_child_die->sect_off)
13263 {
13264 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13265 Check whether we're already processing ORIGIN_CHILD_DIE.
13266 This can happen with mutually referenced abstract_origins.
13267 PR 16581. */
13268 if (!origin_child_die->in_process)
13269 process_die (origin_child_die, origin_cu);
13270 }
13271 origin_child_die = sibling_die (origin_child_die);
13272 }
13273 origin_cu->list_in_scope = origin_previous_list_in_scope;
13274
13275 if (cu != origin_cu)
13276 compute_delayed_physnames (origin_cu);
13277 }
13278
13279 static void
13280 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13281 {
13282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13284 struct context_stack *newobj;
13285 CORE_ADDR lowpc;
13286 CORE_ADDR highpc;
13287 struct die_info *child_die;
13288 struct attribute *attr, *call_line, *call_file;
13289 const char *name;
13290 CORE_ADDR baseaddr;
13291 struct block *block;
13292 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13293 std::vector<struct symbol *> template_args;
13294 struct template_symbol *templ_func = NULL;
13295
13296 if (inlined_func)
13297 {
13298 /* If we do not have call site information, we can't show the
13299 caller of this inlined function. That's too confusing, so
13300 only use the scope for local variables. */
13301 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13302 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13303 if (call_line == NULL || call_file == NULL)
13304 {
13305 read_lexical_block_scope (die, cu);
13306 return;
13307 }
13308 }
13309
13310 baseaddr = objfile->text_section_offset ();
13311
13312 name = dwarf2_name (die, cu);
13313
13314 /* Ignore functions with missing or empty names. These are actually
13315 illegal according to the DWARF standard. */
13316 if (name == NULL)
13317 {
13318 complaint (_("missing name for subprogram DIE at %s"),
13319 sect_offset_str (die->sect_off));
13320 return;
13321 }
13322
13323 /* Ignore functions with missing or invalid low and high pc attributes. */
13324 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13325 <= PC_BOUNDS_INVALID)
13326 {
13327 attr = dwarf2_attr (die, DW_AT_external, cu);
13328 if (!attr || !DW_UNSND (attr))
13329 complaint (_("cannot get low and high bounds "
13330 "for subprogram DIE at %s"),
13331 sect_offset_str (die->sect_off));
13332 return;
13333 }
13334
13335 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13336 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13337
13338 /* If we have any template arguments, then we must allocate a
13339 different sort of symbol. */
13340 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13341 {
13342 if (child_die->tag == DW_TAG_template_type_param
13343 || child_die->tag == DW_TAG_template_value_param)
13344 {
13345 templ_func = allocate_template_symbol (objfile);
13346 templ_func->subclass = SYMBOL_TEMPLATE;
13347 break;
13348 }
13349 }
13350
13351 newobj = cu->get_builder ()->push_context (0, lowpc);
13352 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13353 (struct symbol *) templ_func);
13354
13355 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13356 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13357 cu->language);
13358
13359 /* If there is a location expression for DW_AT_frame_base, record
13360 it. */
13361 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13362 if (attr != nullptr)
13363 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13364
13365 /* If there is a location for the static link, record it. */
13366 newobj->static_link = NULL;
13367 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13368 if (attr != nullptr)
13369 {
13370 newobj->static_link
13371 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13372 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13373 dwarf2_per_cu_addr_type (cu->per_cu));
13374 }
13375
13376 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13377
13378 if (die->child != NULL)
13379 {
13380 child_die = die->child;
13381 while (child_die && child_die->tag)
13382 {
13383 if (child_die->tag == DW_TAG_template_type_param
13384 || child_die->tag == DW_TAG_template_value_param)
13385 {
13386 struct symbol *arg = new_symbol (child_die, NULL, cu);
13387
13388 if (arg != NULL)
13389 template_args.push_back (arg);
13390 }
13391 else
13392 process_die (child_die, cu);
13393 child_die = sibling_die (child_die);
13394 }
13395 }
13396
13397 inherit_abstract_dies (die, cu);
13398
13399 /* If we have a DW_AT_specification, we might need to import using
13400 directives from the context of the specification DIE. See the
13401 comment in determine_prefix. */
13402 if (cu->language == language_cplus
13403 && dwarf2_attr (die, DW_AT_specification, cu))
13404 {
13405 struct dwarf2_cu *spec_cu = cu;
13406 struct die_info *spec_die = die_specification (die, &spec_cu);
13407
13408 while (spec_die)
13409 {
13410 child_die = spec_die->child;
13411 while (child_die && child_die->tag)
13412 {
13413 if (child_die->tag == DW_TAG_imported_module)
13414 process_die (child_die, spec_cu);
13415 child_die = sibling_die (child_die);
13416 }
13417
13418 /* In some cases, GCC generates specification DIEs that
13419 themselves contain DW_AT_specification attributes. */
13420 spec_die = die_specification (spec_die, &spec_cu);
13421 }
13422 }
13423
13424 struct context_stack cstk = cu->get_builder ()->pop_context ();
13425 /* Make a block for the local symbols within. */
13426 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13427 cstk.static_link, lowpc, highpc);
13428
13429 /* For C++, set the block's scope. */
13430 if ((cu->language == language_cplus
13431 || cu->language == language_fortran
13432 || cu->language == language_d
13433 || cu->language == language_rust)
13434 && cu->processing_has_namespace_info)
13435 block_set_scope (block, determine_prefix (die, cu),
13436 &objfile->objfile_obstack);
13437
13438 /* If we have address ranges, record them. */
13439 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13440
13441 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13442
13443 /* Attach template arguments to function. */
13444 if (!template_args.empty ())
13445 {
13446 gdb_assert (templ_func != NULL);
13447
13448 templ_func->n_template_arguments = template_args.size ();
13449 templ_func->template_arguments
13450 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13451 templ_func->n_template_arguments);
13452 memcpy (templ_func->template_arguments,
13453 template_args.data (),
13454 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13455
13456 /* Make sure that the symtab is set on the new symbols. Even
13457 though they don't appear in this symtab directly, other parts
13458 of gdb assume that symbols do, and this is reasonably
13459 true. */
13460 for (symbol *sym : template_args)
13461 symbol_set_symtab (sym, symbol_symtab (templ_func));
13462 }
13463
13464 /* In C++, we can have functions nested inside functions (e.g., when
13465 a function declares a class that has methods). This means that
13466 when we finish processing a function scope, we may need to go
13467 back to building a containing block's symbol lists. */
13468 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13469 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13470
13471 /* If we've finished processing a top-level function, subsequent
13472 symbols go in the file symbol list. */
13473 if (cu->get_builder ()->outermost_context_p ())
13474 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13475 }
13476
13477 /* Process all the DIES contained within a lexical block scope. Start
13478 a new scope, process the dies, and then close the scope. */
13479
13480 static void
13481 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13482 {
13483 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13484 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13485 CORE_ADDR lowpc, highpc;
13486 struct die_info *child_die;
13487 CORE_ADDR baseaddr;
13488
13489 baseaddr = objfile->text_section_offset ();
13490
13491 /* Ignore blocks with missing or invalid low and high pc attributes. */
13492 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13493 as multiple lexical blocks? Handling children in a sane way would
13494 be nasty. Might be easier to properly extend generic blocks to
13495 describe ranges. */
13496 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13497 {
13498 case PC_BOUNDS_NOT_PRESENT:
13499 /* DW_TAG_lexical_block has no attributes, process its children as if
13500 there was no wrapping by that DW_TAG_lexical_block.
13501 GCC does no longer produces such DWARF since GCC r224161. */
13502 for (child_die = die->child;
13503 child_die != NULL && child_die->tag;
13504 child_die = sibling_die (child_die))
13505 process_die (child_die, cu);
13506 return;
13507 case PC_BOUNDS_INVALID:
13508 return;
13509 }
13510 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13511 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13512
13513 cu->get_builder ()->push_context (0, lowpc);
13514 if (die->child != NULL)
13515 {
13516 child_die = die->child;
13517 while (child_die && child_die->tag)
13518 {
13519 process_die (child_die, cu);
13520 child_die = sibling_die (child_die);
13521 }
13522 }
13523 inherit_abstract_dies (die, cu);
13524 struct context_stack cstk = cu->get_builder ()->pop_context ();
13525
13526 if (*cu->get_builder ()->get_local_symbols () != NULL
13527 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13528 {
13529 struct block *block
13530 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13531 cstk.start_addr, highpc);
13532
13533 /* Note that recording ranges after traversing children, as we
13534 do here, means that recording a parent's ranges entails
13535 walking across all its children's ranges as they appear in
13536 the address map, which is quadratic behavior.
13537
13538 It would be nicer to record the parent's ranges before
13539 traversing its children, simply overriding whatever you find
13540 there. But since we don't even decide whether to create a
13541 block until after we've traversed its children, that's hard
13542 to do. */
13543 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13544 }
13545 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13546 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13547 }
13548
13549 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13550
13551 static void
13552 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13553 {
13554 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13556 CORE_ADDR pc, baseaddr;
13557 struct attribute *attr;
13558 struct call_site *call_site, call_site_local;
13559 void **slot;
13560 int nparams;
13561 struct die_info *child_die;
13562
13563 baseaddr = objfile->text_section_offset ();
13564
13565 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13566 if (attr == NULL)
13567 {
13568 /* This was a pre-DWARF-5 GNU extension alias
13569 for DW_AT_call_return_pc. */
13570 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13571 }
13572 if (!attr)
13573 {
13574 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13575 "DIE %s [in module %s]"),
13576 sect_offset_str (die->sect_off), objfile_name (objfile));
13577 return;
13578 }
13579 pc = attr->value_as_address () + baseaddr;
13580 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13581
13582 if (cu->call_site_htab == NULL)
13583 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13584 NULL, &objfile->objfile_obstack,
13585 hashtab_obstack_allocate, NULL);
13586 call_site_local.pc = pc;
13587 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13588 if (*slot != NULL)
13589 {
13590 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13591 "DIE %s [in module %s]"),
13592 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13593 objfile_name (objfile));
13594 return;
13595 }
13596
13597 /* Count parameters at the caller. */
13598
13599 nparams = 0;
13600 for (child_die = die->child; child_die && child_die->tag;
13601 child_die = sibling_die (child_die))
13602 {
13603 if (child_die->tag != DW_TAG_call_site_parameter
13604 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13605 {
13606 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13607 "DW_TAG_call_site child DIE %s [in module %s]"),
13608 child_die->tag, sect_offset_str (child_die->sect_off),
13609 objfile_name (objfile));
13610 continue;
13611 }
13612
13613 nparams++;
13614 }
13615
13616 call_site
13617 = ((struct call_site *)
13618 obstack_alloc (&objfile->objfile_obstack,
13619 sizeof (*call_site)
13620 + (sizeof (*call_site->parameter) * (nparams - 1))));
13621 *slot = call_site;
13622 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13623 call_site->pc = pc;
13624
13625 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13626 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13627 {
13628 struct die_info *func_die;
13629
13630 /* Skip also over DW_TAG_inlined_subroutine. */
13631 for (func_die = die->parent;
13632 func_die && func_die->tag != DW_TAG_subprogram
13633 && func_die->tag != DW_TAG_subroutine_type;
13634 func_die = func_die->parent);
13635
13636 /* DW_AT_call_all_calls is a superset
13637 of DW_AT_call_all_tail_calls. */
13638 if (func_die
13639 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13640 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13641 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13642 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13643 {
13644 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13645 not complete. But keep CALL_SITE for look ups via call_site_htab,
13646 both the initial caller containing the real return address PC and
13647 the final callee containing the current PC of a chain of tail
13648 calls do not need to have the tail call list complete. But any
13649 function candidate for a virtual tail call frame searched via
13650 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13651 determined unambiguously. */
13652 }
13653 else
13654 {
13655 struct type *func_type = NULL;
13656
13657 if (func_die)
13658 func_type = get_die_type (func_die, cu);
13659 if (func_type != NULL)
13660 {
13661 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13662
13663 /* Enlist this call site to the function. */
13664 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13665 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13666 }
13667 else
13668 complaint (_("Cannot find function owning DW_TAG_call_site "
13669 "DIE %s [in module %s]"),
13670 sect_offset_str (die->sect_off), objfile_name (objfile));
13671 }
13672 }
13673
13674 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13675 if (attr == NULL)
13676 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13677 if (attr == NULL)
13678 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13679 if (attr == NULL)
13680 {
13681 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13682 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13683 }
13684 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13685 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13686 /* Keep NULL DWARF_BLOCK. */;
13687 else if (attr->form_is_block ())
13688 {
13689 struct dwarf2_locexpr_baton *dlbaton;
13690
13691 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13692 dlbaton->data = DW_BLOCK (attr)->data;
13693 dlbaton->size = DW_BLOCK (attr)->size;
13694 dlbaton->per_cu = cu->per_cu;
13695
13696 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13697 }
13698 else if (attr->form_is_ref ())
13699 {
13700 struct dwarf2_cu *target_cu = cu;
13701 struct die_info *target_die;
13702
13703 target_die = follow_die_ref (die, attr, &target_cu);
13704 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13705 if (die_is_declaration (target_die, target_cu))
13706 {
13707 const char *target_physname;
13708
13709 /* Prefer the mangled name; otherwise compute the demangled one. */
13710 target_physname = dw2_linkage_name (target_die, target_cu);
13711 if (target_physname == NULL)
13712 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13713 if (target_physname == NULL)
13714 complaint (_("DW_AT_call_target target DIE has invalid "
13715 "physname, for referencing DIE %s [in module %s]"),
13716 sect_offset_str (die->sect_off), objfile_name (objfile));
13717 else
13718 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13719 }
13720 else
13721 {
13722 CORE_ADDR lowpc;
13723
13724 /* DW_AT_entry_pc should be preferred. */
13725 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13726 <= PC_BOUNDS_INVALID)
13727 complaint (_("DW_AT_call_target target DIE has invalid "
13728 "low pc, for referencing DIE %s [in module %s]"),
13729 sect_offset_str (die->sect_off), objfile_name (objfile));
13730 else
13731 {
13732 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13733 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13734 }
13735 }
13736 }
13737 else
13738 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13739 "block nor reference, for DIE %s [in module %s]"),
13740 sect_offset_str (die->sect_off), objfile_name (objfile));
13741
13742 call_site->per_cu = cu->per_cu;
13743
13744 for (child_die = die->child;
13745 child_die && child_die->tag;
13746 child_die = sibling_die (child_die))
13747 {
13748 struct call_site_parameter *parameter;
13749 struct attribute *loc, *origin;
13750
13751 if (child_die->tag != DW_TAG_call_site_parameter
13752 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13753 {
13754 /* Already printed the complaint above. */
13755 continue;
13756 }
13757
13758 gdb_assert (call_site->parameter_count < nparams);
13759 parameter = &call_site->parameter[call_site->parameter_count];
13760
13761 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13762 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13763 register is contained in DW_AT_call_value. */
13764
13765 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13766 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13767 if (origin == NULL)
13768 {
13769 /* This was a pre-DWARF-5 GNU extension alias
13770 for DW_AT_call_parameter. */
13771 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13772 }
13773 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13774 {
13775 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13776
13777 sect_offset sect_off
13778 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13779 if (!offset_in_cu_p (&cu->header, sect_off))
13780 {
13781 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13782 binding can be done only inside one CU. Such referenced DIE
13783 therefore cannot be even moved to DW_TAG_partial_unit. */
13784 complaint (_("DW_AT_call_parameter offset is not in CU for "
13785 "DW_TAG_call_site child DIE %s [in module %s]"),
13786 sect_offset_str (child_die->sect_off),
13787 objfile_name (objfile));
13788 continue;
13789 }
13790 parameter->u.param_cu_off
13791 = (cu_offset) (sect_off - cu->header.sect_off);
13792 }
13793 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13794 {
13795 complaint (_("No DW_FORM_block* DW_AT_location for "
13796 "DW_TAG_call_site child DIE %s [in module %s]"),
13797 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13798 continue;
13799 }
13800 else
13801 {
13802 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13803 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13804 if (parameter->u.dwarf_reg != -1)
13805 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13806 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13807 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13808 &parameter->u.fb_offset))
13809 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13810 else
13811 {
13812 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13813 "for DW_FORM_block* DW_AT_location is supported for "
13814 "DW_TAG_call_site child DIE %s "
13815 "[in module %s]"),
13816 sect_offset_str (child_die->sect_off),
13817 objfile_name (objfile));
13818 continue;
13819 }
13820 }
13821
13822 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13823 if (attr == NULL)
13824 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13825 if (attr == NULL || !attr->form_is_block ())
13826 {
13827 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13828 "DW_TAG_call_site child DIE %s [in module %s]"),
13829 sect_offset_str (child_die->sect_off),
13830 objfile_name (objfile));
13831 continue;
13832 }
13833 parameter->value = DW_BLOCK (attr)->data;
13834 parameter->value_size = DW_BLOCK (attr)->size;
13835
13836 /* Parameters are not pre-cleared by memset above. */
13837 parameter->data_value = NULL;
13838 parameter->data_value_size = 0;
13839 call_site->parameter_count++;
13840
13841 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13842 if (attr == NULL)
13843 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13844 if (attr != nullptr)
13845 {
13846 if (!attr->form_is_block ())
13847 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13848 "DW_TAG_call_site child DIE %s [in module %s]"),
13849 sect_offset_str (child_die->sect_off),
13850 objfile_name (objfile));
13851 else
13852 {
13853 parameter->data_value = DW_BLOCK (attr)->data;
13854 parameter->data_value_size = DW_BLOCK (attr)->size;
13855 }
13856 }
13857 }
13858 }
13859
13860 /* Helper function for read_variable. If DIE represents a virtual
13861 table, then return the type of the concrete object that is
13862 associated with the virtual table. Otherwise, return NULL. */
13863
13864 static struct type *
13865 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13866 {
13867 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13868 if (attr == NULL)
13869 return NULL;
13870
13871 /* Find the type DIE. */
13872 struct die_info *type_die = NULL;
13873 struct dwarf2_cu *type_cu = cu;
13874
13875 if (attr->form_is_ref ())
13876 type_die = follow_die_ref (die, attr, &type_cu);
13877 if (type_die == NULL)
13878 return NULL;
13879
13880 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13881 return NULL;
13882 return die_containing_type (type_die, type_cu);
13883 }
13884
13885 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13886
13887 static void
13888 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13889 {
13890 struct rust_vtable_symbol *storage = NULL;
13891
13892 if (cu->language == language_rust)
13893 {
13894 struct type *containing_type = rust_containing_type (die, cu);
13895
13896 if (containing_type != NULL)
13897 {
13898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13899
13900 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13901 initialize_objfile_symbol (storage);
13902 storage->concrete_type = containing_type;
13903 storage->subclass = SYMBOL_RUST_VTABLE;
13904 }
13905 }
13906
13907 struct symbol *res = new_symbol (die, NULL, cu, storage);
13908 struct attribute *abstract_origin
13909 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13910 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13911 if (res == NULL && loc && abstract_origin)
13912 {
13913 /* We have a variable without a name, but with a location and an abstract
13914 origin. This may be a concrete instance of an abstract variable
13915 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13916 later. */
13917 struct dwarf2_cu *origin_cu = cu;
13918 struct die_info *origin_die
13919 = follow_die_ref (die, abstract_origin, &origin_cu);
13920 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13921 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13922 }
13923 }
13924
13925 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13926 reading .debug_rnglists.
13927 Callback's type should be:
13928 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13929 Return true if the attributes are present and valid, otherwise,
13930 return false. */
13931
13932 template <typename Callback>
13933 static bool
13934 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13935 Callback &&callback)
13936 {
13937 struct dwarf2_per_objfile *dwarf2_per_objfile
13938 = cu->per_cu->dwarf2_per_objfile;
13939 struct objfile *objfile = dwarf2_per_objfile->objfile;
13940 bfd *obfd = objfile->obfd;
13941 /* Base address selection entry. */
13942 CORE_ADDR base;
13943 int found_base;
13944 const gdb_byte *buffer;
13945 CORE_ADDR baseaddr;
13946 bool overflow = false;
13947
13948 found_base = cu->base_known;
13949 base = cu->base_address;
13950
13951 dwarf2_per_objfile->rnglists.read (objfile);
13952 if (offset >= dwarf2_per_objfile->rnglists.size)
13953 {
13954 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13955 offset);
13956 return false;
13957 }
13958 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13959
13960 baseaddr = objfile->text_section_offset ();
13961
13962 while (1)
13963 {
13964 /* Initialize it due to a false compiler warning. */
13965 CORE_ADDR range_beginning = 0, range_end = 0;
13966 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13967 + dwarf2_per_objfile->rnglists.size);
13968 unsigned int bytes_read;
13969
13970 if (buffer == buf_end)
13971 {
13972 overflow = true;
13973 break;
13974 }
13975 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13976 switch (rlet)
13977 {
13978 case DW_RLE_end_of_list:
13979 break;
13980 case DW_RLE_base_address:
13981 if (buffer + cu->header.addr_size > buf_end)
13982 {
13983 overflow = true;
13984 break;
13985 }
13986 base = read_address (obfd, buffer, cu, &bytes_read);
13987 found_base = 1;
13988 buffer += bytes_read;
13989 break;
13990 case DW_RLE_start_length:
13991 if (buffer + cu->header.addr_size > buf_end)
13992 {
13993 overflow = true;
13994 break;
13995 }
13996 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13997 buffer += bytes_read;
13998 range_end = (range_beginning
13999 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14000 buffer += bytes_read;
14001 if (buffer > buf_end)
14002 {
14003 overflow = true;
14004 break;
14005 }
14006 break;
14007 case DW_RLE_offset_pair:
14008 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14009 buffer += bytes_read;
14010 if (buffer > buf_end)
14011 {
14012 overflow = true;
14013 break;
14014 }
14015 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14016 buffer += bytes_read;
14017 if (buffer > buf_end)
14018 {
14019 overflow = true;
14020 break;
14021 }
14022 break;
14023 case DW_RLE_start_end:
14024 if (buffer + 2 * cu->header.addr_size > buf_end)
14025 {
14026 overflow = true;
14027 break;
14028 }
14029 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14030 buffer += bytes_read;
14031 range_end = read_address (obfd, buffer, cu, &bytes_read);
14032 buffer += bytes_read;
14033 break;
14034 default:
14035 complaint (_("Invalid .debug_rnglists data (no base address)"));
14036 return false;
14037 }
14038 if (rlet == DW_RLE_end_of_list || overflow)
14039 break;
14040 if (rlet == DW_RLE_base_address)
14041 continue;
14042
14043 if (!found_base)
14044 {
14045 /* We have no valid base address for the ranges
14046 data. */
14047 complaint (_("Invalid .debug_rnglists data (no base address)"));
14048 return false;
14049 }
14050
14051 if (range_beginning > range_end)
14052 {
14053 /* Inverted range entries are invalid. */
14054 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14055 return false;
14056 }
14057
14058 /* Empty range entries have no effect. */
14059 if (range_beginning == range_end)
14060 continue;
14061
14062 range_beginning += base;
14063 range_end += base;
14064
14065 /* A not-uncommon case of bad debug info.
14066 Don't pollute the addrmap with bad data. */
14067 if (range_beginning + baseaddr == 0
14068 && !dwarf2_per_objfile->has_section_at_zero)
14069 {
14070 complaint (_(".debug_rnglists entry has start address of zero"
14071 " [in module %s]"), objfile_name (objfile));
14072 continue;
14073 }
14074
14075 callback (range_beginning, range_end);
14076 }
14077
14078 if (overflow)
14079 {
14080 complaint (_("Offset %d is not terminated "
14081 "for DW_AT_ranges attribute"),
14082 offset);
14083 return false;
14084 }
14085
14086 return true;
14087 }
14088
14089 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14090 Callback's type should be:
14091 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14092 Return 1 if the attributes are present and valid, otherwise, return 0. */
14093
14094 template <typename Callback>
14095 static int
14096 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14097 Callback &&callback)
14098 {
14099 struct dwarf2_per_objfile *dwarf2_per_objfile
14100 = cu->per_cu->dwarf2_per_objfile;
14101 struct objfile *objfile = dwarf2_per_objfile->objfile;
14102 struct comp_unit_head *cu_header = &cu->header;
14103 bfd *obfd = objfile->obfd;
14104 unsigned int addr_size = cu_header->addr_size;
14105 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14106 /* Base address selection entry. */
14107 CORE_ADDR base;
14108 int found_base;
14109 unsigned int dummy;
14110 const gdb_byte *buffer;
14111 CORE_ADDR baseaddr;
14112
14113 if (cu_header->version >= 5)
14114 return dwarf2_rnglists_process (offset, cu, callback);
14115
14116 found_base = cu->base_known;
14117 base = cu->base_address;
14118
14119 dwarf2_per_objfile->ranges.read (objfile);
14120 if (offset >= dwarf2_per_objfile->ranges.size)
14121 {
14122 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14123 offset);
14124 return 0;
14125 }
14126 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14127
14128 baseaddr = objfile->text_section_offset ();
14129
14130 while (1)
14131 {
14132 CORE_ADDR range_beginning, range_end;
14133
14134 range_beginning = read_address (obfd, buffer, cu, &dummy);
14135 buffer += addr_size;
14136 range_end = read_address (obfd, buffer, cu, &dummy);
14137 buffer += addr_size;
14138 offset += 2 * addr_size;
14139
14140 /* An end of list marker is a pair of zero addresses. */
14141 if (range_beginning == 0 && range_end == 0)
14142 /* Found the end of list entry. */
14143 break;
14144
14145 /* Each base address selection entry is a pair of 2 values.
14146 The first is the largest possible address, the second is
14147 the base address. Check for a base address here. */
14148 if ((range_beginning & mask) == mask)
14149 {
14150 /* If we found the largest possible address, then we already
14151 have the base address in range_end. */
14152 base = range_end;
14153 found_base = 1;
14154 continue;
14155 }
14156
14157 if (!found_base)
14158 {
14159 /* We have no valid base address for the ranges
14160 data. */
14161 complaint (_("Invalid .debug_ranges data (no base address)"));
14162 return 0;
14163 }
14164
14165 if (range_beginning > range_end)
14166 {
14167 /* Inverted range entries are invalid. */
14168 complaint (_("Invalid .debug_ranges data (inverted range)"));
14169 return 0;
14170 }
14171
14172 /* Empty range entries have no effect. */
14173 if (range_beginning == range_end)
14174 continue;
14175
14176 range_beginning += base;
14177 range_end += base;
14178
14179 /* A not-uncommon case of bad debug info.
14180 Don't pollute the addrmap with bad data. */
14181 if (range_beginning + baseaddr == 0
14182 && !dwarf2_per_objfile->has_section_at_zero)
14183 {
14184 complaint (_(".debug_ranges entry has start address of zero"
14185 " [in module %s]"), objfile_name (objfile));
14186 continue;
14187 }
14188
14189 callback (range_beginning, range_end);
14190 }
14191
14192 return 1;
14193 }
14194
14195 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14196 Return 1 if the attributes are present and valid, otherwise, return 0.
14197 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14198
14199 static int
14200 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14201 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14202 dwarf2_psymtab *ranges_pst)
14203 {
14204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14206 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14207 int low_set = 0;
14208 CORE_ADDR low = 0;
14209 CORE_ADDR high = 0;
14210 int retval;
14211
14212 retval = dwarf2_ranges_process (offset, cu,
14213 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14214 {
14215 if (ranges_pst != NULL)
14216 {
14217 CORE_ADDR lowpc;
14218 CORE_ADDR highpc;
14219
14220 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14221 range_beginning + baseaddr)
14222 - baseaddr);
14223 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14224 range_end + baseaddr)
14225 - baseaddr);
14226 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14227 lowpc, highpc - 1, ranges_pst);
14228 }
14229
14230 /* FIXME: This is recording everything as a low-high
14231 segment of consecutive addresses. We should have a
14232 data structure for discontiguous block ranges
14233 instead. */
14234 if (! low_set)
14235 {
14236 low = range_beginning;
14237 high = range_end;
14238 low_set = 1;
14239 }
14240 else
14241 {
14242 if (range_beginning < low)
14243 low = range_beginning;
14244 if (range_end > high)
14245 high = range_end;
14246 }
14247 });
14248 if (!retval)
14249 return 0;
14250
14251 if (! low_set)
14252 /* If the first entry is an end-of-list marker, the range
14253 describes an empty scope, i.e. no instructions. */
14254 return 0;
14255
14256 if (low_return)
14257 *low_return = low;
14258 if (high_return)
14259 *high_return = high;
14260 return 1;
14261 }
14262
14263 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14264 definition for the return value. *LOWPC and *HIGHPC are set iff
14265 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14266
14267 static enum pc_bounds_kind
14268 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14269 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14270 dwarf2_psymtab *pst)
14271 {
14272 struct dwarf2_per_objfile *dwarf2_per_objfile
14273 = cu->per_cu->dwarf2_per_objfile;
14274 struct attribute *attr;
14275 struct attribute *attr_high;
14276 CORE_ADDR low = 0;
14277 CORE_ADDR high = 0;
14278 enum pc_bounds_kind ret;
14279
14280 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14281 if (attr_high)
14282 {
14283 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14284 if (attr != nullptr)
14285 {
14286 low = attr->value_as_address ();
14287 high = attr_high->value_as_address ();
14288 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14289 high += low;
14290 }
14291 else
14292 /* Found high w/o low attribute. */
14293 return PC_BOUNDS_INVALID;
14294
14295 /* Found consecutive range of addresses. */
14296 ret = PC_BOUNDS_HIGH_LOW;
14297 }
14298 else
14299 {
14300 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14301 if (attr != NULL)
14302 {
14303 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14304 We take advantage of the fact that DW_AT_ranges does not appear
14305 in DW_TAG_compile_unit of DWO files. */
14306 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14307 unsigned int ranges_offset = (DW_UNSND (attr)
14308 + (need_ranges_base
14309 ? cu->ranges_base
14310 : 0));
14311
14312 /* Value of the DW_AT_ranges attribute is the offset in the
14313 .debug_ranges section. */
14314 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14315 return PC_BOUNDS_INVALID;
14316 /* Found discontinuous range of addresses. */
14317 ret = PC_BOUNDS_RANGES;
14318 }
14319 else
14320 return PC_BOUNDS_NOT_PRESENT;
14321 }
14322
14323 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14324 if (high <= low)
14325 return PC_BOUNDS_INVALID;
14326
14327 /* When using the GNU linker, .gnu.linkonce. sections are used to
14328 eliminate duplicate copies of functions and vtables and such.
14329 The linker will arbitrarily choose one and discard the others.
14330 The AT_*_pc values for such functions refer to local labels in
14331 these sections. If the section from that file was discarded, the
14332 labels are not in the output, so the relocs get a value of 0.
14333 If this is a discarded function, mark the pc bounds as invalid,
14334 so that GDB will ignore it. */
14335 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14336 return PC_BOUNDS_INVALID;
14337
14338 *lowpc = low;
14339 if (highpc)
14340 *highpc = high;
14341 return ret;
14342 }
14343
14344 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14345 its low and high PC addresses. Do nothing if these addresses could not
14346 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14347 and HIGHPC to the high address if greater than HIGHPC. */
14348
14349 static void
14350 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14351 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14352 struct dwarf2_cu *cu)
14353 {
14354 CORE_ADDR low, high;
14355 struct die_info *child = die->child;
14356
14357 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14358 {
14359 *lowpc = std::min (*lowpc, low);
14360 *highpc = std::max (*highpc, high);
14361 }
14362
14363 /* If the language does not allow nested subprograms (either inside
14364 subprograms or lexical blocks), we're done. */
14365 if (cu->language != language_ada)
14366 return;
14367
14368 /* Check all the children of the given DIE. If it contains nested
14369 subprograms, then check their pc bounds. Likewise, we need to
14370 check lexical blocks as well, as they may also contain subprogram
14371 definitions. */
14372 while (child && child->tag)
14373 {
14374 if (child->tag == DW_TAG_subprogram
14375 || child->tag == DW_TAG_lexical_block)
14376 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14377 child = sibling_die (child);
14378 }
14379 }
14380
14381 /* Get the low and high pc's represented by the scope DIE, and store
14382 them in *LOWPC and *HIGHPC. If the correct values can't be
14383 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14384
14385 static void
14386 get_scope_pc_bounds (struct die_info *die,
14387 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14388 struct dwarf2_cu *cu)
14389 {
14390 CORE_ADDR best_low = (CORE_ADDR) -1;
14391 CORE_ADDR best_high = (CORE_ADDR) 0;
14392 CORE_ADDR current_low, current_high;
14393
14394 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14395 >= PC_BOUNDS_RANGES)
14396 {
14397 best_low = current_low;
14398 best_high = current_high;
14399 }
14400 else
14401 {
14402 struct die_info *child = die->child;
14403
14404 while (child && child->tag)
14405 {
14406 switch (child->tag) {
14407 case DW_TAG_subprogram:
14408 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14409 break;
14410 case DW_TAG_namespace:
14411 case DW_TAG_module:
14412 /* FIXME: carlton/2004-01-16: Should we do this for
14413 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14414 that current GCC's always emit the DIEs corresponding
14415 to definitions of methods of classes as children of a
14416 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14417 the DIEs giving the declarations, which could be
14418 anywhere). But I don't see any reason why the
14419 standards says that they have to be there. */
14420 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14421
14422 if (current_low != ((CORE_ADDR) -1))
14423 {
14424 best_low = std::min (best_low, current_low);
14425 best_high = std::max (best_high, current_high);
14426 }
14427 break;
14428 default:
14429 /* Ignore. */
14430 break;
14431 }
14432
14433 child = sibling_die (child);
14434 }
14435 }
14436
14437 *lowpc = best_low;
14438 *highpc = best_high;
14439 }
14440
14441 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14442 in DIE. */
14443
14444 static void
14445 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14446 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14447 {
14448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14449 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14450 struct attribute *attr;
14451 struct attribute *attr_high;
14452
14453 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14454 if (attr_high)
14455 {
14456 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14457 if (attr != nullptr)
14458 {
14459 CORE_ADDR low = attr->value_as_address ();
14460 CORE_ADDR high = attr_high->value_as_address ();
14461
14462 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14463 high += low;
14464
14465 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14466 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14467 cu->get_builder ()->record_block_range (block, low, high - 1);
14468 }
14469 }
14470
14471 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14472 if (attr != nullptr)
14473 {
14474 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14475 We take advantage of the fact that DW_AT_ranges does not appear
14476 in DW_TAG_compile_unit of DWO files. */
14477 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14478
14479 /* The value of the DW_AT_ranges attribute is the offset of the
14480 address range list in the .debug_ranges section. */
14481 unsigned long offset = (DW_UNSND (attr)
14482 + (need_ranges_base ? cu->ranges_base : 0));
14483
14484 std::vector<blockrange> blockvec;
14485 dwarf2_ranges_process (offset, cu,
14486 [&] (CORE_ADDR start, CORE_ADDR end)
14487 {
14488 start += baseaddr;
14489 end += baseaddr;
14490 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14491 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14492 cu->get_builder ()->record_block_range (block, start, end - 1);
14493 blockvec.emplace_back (start, end);
14494 });
14495
14496 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14497 }
14498 }
14499
14500 /* Check whether the producer field indicates either of GCC < 4.6, or the
14501 Intel C/C++ compiler, and cache the result in CU. */
14502
14503 static void
14504 check_producer (struct dwarf2_cu *cu)
14505 {
14506 int major, minor;
14507
14508 if (cu->producer == NULL)
14509 {
14510 /* For unknown compilers expect their behavior is DWARF version
14511 compliant.
14512
14513 GCC started to support .debug_types sections by -gdwarf-4 since
14514 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14515 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14516 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14517 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14518 }
14519 else if (producer_is_gcc (cu->producer, &major, &minor))
14520 {
14521 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14522 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14523 }
14524 else if (producer_is_icc (cu->producer, &major, &minor))
14525 {
14526 cu->producer_is_icc = true;
14527 cu->producer_is_icc_lt_14 = major < 14;
14528 }
14529 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14530 cu->producer_is_codewarrior = true;
14531 else
14532 {
14533 /* For other non-GCC compilers, expect their behavior is DWARF version
14534 compliant. */
14535 }
14536
14537 cu->checked_producer = true;
14538 }
14539
14540 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14541 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14542 during 4.6.0 experimental. */
14543
14544 static bool
14545 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14546 {
14547 if (!cu->checked_producer)
14548 check_producer (cu);
14549
14550 return cu->producer_is_gxx_lt_4_6;
14551 }
14552
14553
14554 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14555 with incorrect is_stmt attributes. */
14556
14557 static bool
14558 producer_is_codewarrior (struct dwarf2_cu *cu)
14559 {
14560 if (!cu->checked_producer)
14561 check_producer (cu);
14562
14563 return cu->producer_is_codewarrior;
14564 }
14565
14566 /* Return the default accessibility type if it is not overridden by
14567 DW_AT_accessibility. */
14568
14569 static enum dwarf_access_attribute
14570 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14571 {
14572 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14573 {
14574 /* The default DWARF 2 accessibility for members is public, the default
14575 accessibility for inheritance is private. */
14576
14577 if (die->tag != DW_TAG_inheritance)
14578 return DW_ACCESS_public;
14579 else
14580 return DW_ACCESS_private;
14581 }
14582 else
14583 {
14584 /* DWARF 3+ defines the default accessibility a different way. The same
14585 rules apply now for DW_TAG_inheritance as for the members and it only
14586 depends on the container kind. */
14587
14588 if (die->parent->tag == DW_TAG_class_type)
14589 return DW_ACCESS_private;
14590 else
14591 return DW_ACCESS_public;
14592 }
14593 }
14594
14595 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14596 offset. If the attribute was not found return 0, otherwise return
14597 1. If it was found but could not properly be handled, set *OFFSET
14598 to 0. */
14599
14600 static int
14601 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14602 LONGEST *offset)
14603 {
14604 struct attribute *attr;
14605
14606 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14607 if (attr != NULL)
14608 {
14609 *offset = 0;
14610
14611 /* Note that we do not check for a section offset first here.
14612 This is because DW_AT_data_member_location is new in DWARF 4,
14613 so if we see it, we can assume that a constant form is really
14614 a constant and not a section offset. */
14615 if (attr->form_is_constant ())
14616 *offset = dwarf2_get_attr_constant_value (attr, 0);
14617 else if (attr->form_is_section_offset ())
14618 dwarf2_complex_location_expr_complaint ();
14619 else if (attr->form_is_block ())
14620 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14621 else
14622 dwarf2_complex_location_expr_complaint ();
14623
14624 return 1;
14625 }
14626
14627 return 0;
14628 }
14629
14630 /* Add an aggregate field to the field list. */
14631
14632 static void
14633 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14634 struct dwarf2_cu *cu)
14635 {
14636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14637 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14638 struct nextfield *new_field;
14639 struct attribute *attr;
14640 struct field *fp;
14641 const char *fieldname = "";
14642
14643 if (die->tag == DW_TAG_inheritance)
14644 {
14645 fip->baseclasses.emplace_back ();
14646 new_field = &fip->baseclasses.back ();
14647 }
14648 else
14649 {
14650 fip->fields.emplace_back ();
14651 new_field = &fip->fields.back ();
14652 }
14653
14654 fip->nfields++;
14655
14656 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14657 if (attr != nullptr)
14658 new_field->accessibility = DW_UNSND (attr);
14659 else
14660 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14661 if (new_field->accessibility != DW_ACCESS_public)
14662 fip->non_public_fields = 1;
14663
14664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14665 if (attr != nullptr)
14666 new_field->virtuality = DW_UNSND (attr);
14667 else
14668 new_field->virtuality = DW_VIRTUALITY_none;
14669
14670 fp = &new_field->field;
14671
14672 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14673 {
14674 LONGEST offset;
14675
14676 /* Data member other than a C++ static data member. */
14677
14678 /* Get type of field. */
14679 fp->type = die_type (die, cu);
14680
14681 SET_FIELD_BITPOS (*fp, 0);
14682
14683 /* Get bit size of field (zero if none). */
14684 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14685 if (attr != nullptr)
14686 {
14687 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14688 }
14689 else
14690 {
14691 FIELD_BITSIZE (*fp) = 0;
14692 }
14693
14694 /* Get bit offset of field. */
14695 if (handle_data_member_location (die, cu, &offset))
14696 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14697 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14698 if (attr != nullptr)
14699 {
14700 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14701 {
14702 /* For big endian bits, the DW_AT_bit_offset gives the
14703 additional bit offset from the MSB of the containing
14704 anonymous object to the MSB of the field. We don't
14705 have to do anything special since we don't need to
14706 know the size of the anonymous object. */
14707 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14708 }
14709 else
14710 {
14711 /* For little endian bits, compute the bit offset to the
14712 MSB of the anonymous object, subtract off the number of
14713 bits from the MSB of the field to the MSB of the
14714 object, and then subtract off the number of bits of
14715 the field itself. The result is the bit offset of
14716 the LSB of the field. */
14717 int anonymous_size;
14718 int bit_offset = DW_UNSND (attr);
14719
14720 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14721 if (attr != nullptr)
14722 {
14723 /* The size of the anonymous object containing
14724 the bit field is explicit, so use the
14725 indicated size (in bytes). */
14726 anonymous_size = DW_UNSND (attr);
14727 }
14728 else
14729 {
14730 /* The size of the anonymous object containing
14731 the bit field must be inferred from the type
14732 attribute of the data member containing the
14733 bit field. */
14734 anonymous_size = TYPE_LENGTH (fp->type);
14735 }
14736 SET_FIELD_BITPOS (*fp,
14737 (FIELD_BITPOS (*fp)
14738 + anonymous_size * bits_per_byte
14739 - bit_offset - FIELD_BITSIZE (*fp)));
14740 }
14741 }
14742 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14743 if (attr != NULL)
14744 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14745 + dwarf2_get_attr_constant_value (attr, 0)));
14746
14747 /* Get name of field. */
14748 fieldname = dwarf2_name (die, cu);
14749 if (fieldname == NULL)
14750 fieldname = "";
14751
14752 /* The name is already allocated along with this objfile, so we don't
14753 need to duplicate it for the type. */
14754 fp->name = fieldname;
14755
14756 /* Change accessibility for artificial fields (e.g. virtual table
14757 pointer or virtual base class pointer) to private. */
14758 if (dwarf2_attr (die, DW_AT_artificial, cu))
14759 {
14760 FIELD_ARTIFICIAL (*fp) = 1;
14761 new_field->accessibility = DW_ACCESS_private;
14762 fip->non_public_fields = 1;
14763 }
14764 }
14765 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14766 {
14767 /* C++ static member. */
14768
14769 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14770 is a declaration, but all versions of G++ as of this writing
14771 (so through at least 3.2.1) incorrectly generate
14772 DW_TAG_variable tags. */
14773
14774 const char *physname;
14775
14776 /* Get name of field. */
14777 fieldname = dwarf2_name (die, cu);
14778 if (fieldname == NULL)
14779 return;
14780
14781 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14782 if (attr
14783 /* Only create a symbol if this is an external value.
14784 new_symbol checks this and puts the value in the global symbol
14785 table, which we want. If it is not external, new_symbol
14786 will try to put the value in cu->list_in_scope which is wrong. */
14787 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14788 {
14789 /* A static const member, not much different than an enum as far as
14790 we're concerned, except that we can support more types. */
14791 new_symbol (die, NULL, cu);
14792 }
14793
14794 /* Get physical name. */
14795 physname = dwarf2_physname (fieldname, die, cu);
14796
14797 /* The name is already allocated along with this objfile, so we don't
14798 need to duplicate it for the type. */
14799 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14800 FIELD_TYPE (*fp) = die_type (die, cu);
14801 FIELD_NAME (*fp) = fieldname;
14802 }
14803 else if (die->tag == DW_TAG_inheritance)
14804 {
14805 LONGEST offset;
14806
14807 /* C++ base class field. */
14808 if (handle_data_member_location (die, cu, &offset))
14809 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14810 FIELD_BITSIZE (*fp) = 0;
14811 FIELD_TYPE (*fp) = die_type (die, cu);
14812 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14813 }
14814 else if (die->tag == DW_TAG_variant_part)
14815 {
14816 /* process_structure_scope will treat this DIE as a union. */
14817 process_structure_scope (die, cu);
14818
14819 /* The variant part is relative to the start of the enclosing
14820 structure. */
14821 SET_FIELD_BITPOS (*fp, 0);
14822 fp->type = get_die_type (die, cu);
14823 fp->artificial = 1;
14824 fp->name = "<<variant>>";
14825
14826 /* Normally a DW_TAG_variant_part won't have a size, but our
14827 representation requires one, so set it to the maximum of the
14828 child sizes, being sure to account for the offset at which
14829 each child is seen. */
14830 if (TYPE_LENGTH (fp->type) == 0)
14831 {
14832 unsigned max = 0;
14833 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14834 {
14835 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14836 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14837 if (len > max)
14838 max = len;
14839 }
14840 TYPE_LENGTH (fp->type) = max;
14841 }
14842 }
14843 else
14844 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14845 }
14846
14847 /* Can the type given by DIE define another type? */
14848
14849 static bool
14850 type_can_define_types (const struct die_info *die)
14851 {
14852 switch (die->tag)
14853 {
14854 case DW_TAG_typedef:
14855 case DW_TAG_class_type:
14856 case DW_TAG_structure_type:
14857 case DW_TAG_union_type:
14858 case DW_TAG_enumeration_type:
14859 return true;
14860
14861 default:
14862 return false;
14863 }
14864 }
14865
14866 /* Add a type definition defined in the scope of the FIP's class. */
14867
14868 static void
14869 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14870 struct dwarf2_cu *cu)
14871 {
14872 struct decl_field fp;
14873 memset (&fp, 0, sizeof (fp));
14874
14875 gdb_assert (type_can_define_types (die));
14876
14877 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14878 fp.name = dwarf2_name (die, cu);
14879 fp.type = read_type_die (die, cu);
14880
14881 /* Save accessibility. */
14882 enum dwarf_access_attribute accessibility;
14883 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14884 if (attr != NULL)
14885 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14886 else
14887 accessibility = dwarf2_default_access_attribute (die, cu);
14888 switch (accessibility)
14889 {
14890 case DW_ACCESS_public:
14891 /* The assumed value if neither private nor protected. */
14892 break;
14893 case DW_ACCESS_private:
14894 fp.is_private = 1;
14895 break;
14896 case DW_ACCESS_protected:
14897 fp.is_protected = 1;
14898 break;
14899 default:
14900 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14901 }
14902
14903 if (die->tag == DW_TAG_typedef)
14904 fip->typedef_field_list.push_back (fp);
14905 else
14906 fip->nested_types_list.push_back (fp);
14907 }
14908
14909 /* Create the vector of fields, and attach it to the type. */
14910
14911 static void
14912 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14913 struct dwarf2_cu *cu)
14914 {
14915 int nfields = fip->nfields;
14916
14917 /* Record the field count, allocate space for the array of fields,
14918 and create blank accessibility bitfields if necessary. */
14919 TYPE_NFIELDS (type) = nfields;
14920 TYPE_FIELDS (type) = (struct field *)
14921 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14922
14923 if (fip->non_public_fields && cu->language != language_ada)
14924 {
14925 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14926
14927 TYPE_FIELD_PRIVATE_BITS (type) =
14928 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14929 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14930
14931 TYPE_FIELD_PROTECTED_BITS (type) =
14932 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14933 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14934
14935 TYPE_FIELD_IGNORE_BITS (type) =
14936 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14937 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14938 }
14939
14940 /* If the type has baseclasses, allocate and clear a bit vector for
14941 TYPE_FIELD_VIRTUAL_BITS. */
14942 if (!fip->baseclasses.empty () && cu->language != language_ada)
14943 {
14944 int num_bytes = B_BYTES (fip->baseclasses.size ());
14945 unsigned char *pointer;
14946
14947 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14948 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14949 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14950 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14951 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14952 }
14953
14954 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14955 {
14956 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14957
14958 for (int index = 0; index < nfields; ++index)
14959 {
14960 struct nextfield &field = fip->fields[index];
14961
14962 if (field.variant.is_discriminant)
14963 di->discriminant_index = index;
14964 else if (field.variant.default_branch)
14965 di->default_index = index;
14966 else
14967 di->discriminants[index] = field.variant.discriminant_value;
14968 }
14969 }
14970
14971 /* Copy the saved-up fields into the field vector. */
14972 for (int i = 0; i < nfields; ++i)
14973 {
14974 struct nextfield &field
14975 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14976 : fip->fields[i - fip->baseclasses.size ()]);
14977
14978 TYPE_FIELD (type, i) = field.field;
14979 switch (field.accessibility)
14980 {
14981 case DW_ACCESS_private:
14982 if (cu->language != language_ada)
14983 SET_TYPE_FIELD_PRIVATE (type, i);
14984 break;
14985
14986 case DW_ACCESS_protected:
14987 if (cu->language != language_ada)
14988 SET_TYPE_FIELD_PROTECTED (type, i);
14989 break;
14990
14991 case DW_ACCESS_public:
14992 break;
14993
14994 default:
14995 /* Unknown accessibility. Complain and treat it as public. */
14996 {
14997 complaint (_("unsupported accessibility %d"),
14998 field.accessibility);
14999 }
15000 break;
15001 }
15002 if (i < fip->baseclasses.size ())
15003 {
15004 switch (field.virtuality)
15005 {
15006 case DW_VIRTUALITY_virtual:
15007 case DW_VIRTUALITY_pure_virtual:
15008 if (cu->language == language_ada)
15009 error (_("unexpected virtuality in component of Ada type"));
15010 SET_TYPE_FIELD_VIRTUAL (type, i);
15011 break;
15012 }
15013 }
15014 }
15015 }
15016
15017 /* Return true if this member function is a constructor, false
15018 otherwise. */
15019
15020 static int
15021 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15022 {
15023 const char *fieldname;
15024 const char *type_name;
15025 int len;
15026
15027 if (die->parent == NULL)
15028 return 0;
15029
15030 if (die->parent->tag != DW_TAG_structure_type
15031 && die->parent->tag != DW_TAG_union_type
15032 && die->parent->tag != DW_TAG_class_type)
15033 return 0;
15034
15035 fieldname = dwarf2_name (die, cu);
15036 type_name = dwarf2_name (die->parent, cu);
15037 if (fieldname == NULL || type_name == NULL)
15038 return 0;
15039
15040 len = strlen (fieldname);
15041 return (strncmp (fieldname, type_name, len) == 0
15042 && (type_name[len] == '\0' || type_name[len] == '<'));
15043 }
15044
15045 /* Check if the given VALUE is a recognized enum
15046 dwarf_defaulted_attribute constant according to DWARF5 spec,
15047 Table 7.24. */
15048
15049 static bool
15050 is_valid_DW_AT_defaulted (ULONGEST value)
15051 {
15052 switch (value)
15053 {
15054 case DW_DEFAULTED_no:
15055 case DW_DEFAULTED_in_class:
15056 case DW_DEFAULTED_out_of_class:
15057 return true;
15058 }
15059
15060 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15061 return false;
15062 }
15063
15064 /* Add a member function to the proper fieldlist. */
15065
15066 static void
15067 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15068 struct type *type, struct dwarf2_cu *cu)
15069 {
15070 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15071 struct attribute *attr;
15072 int i;
15073 struct fnfieldlist *flp = nullptr;
15074 struct fn_field *fnp;
15075 const char *fieldname;
15076 struct type *this_type;
15077 enum dwarf_access_attribute accessibility;
15078
15079 if (cu->language == language_ada)
15080 error (_("unexpected member function in Ada type"));
15081
15082 /* Get name of member function. */
15083 fieldname = dwarf2_name (die, cu);
15084 if (fieldname == NULL)
15085 return;
15086
15087 /* Look up member function name in fieldlist. */
15088 for (i = 0; i < fip->fnfieldlists.size (); i++)
15089 {
15090 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15091 {
15092 flp = &fip->fnfieldlists[i];
15093 break;
15094 }
15095 }
15096
15097 /* Create a new fnfieldlist if necessary. */
15098 if (flp == nullptr)
15099 {
15100 fip->fnfieldlists.emplace_back ();
15101 flp = &fip->fnfieldlists.back ();
15102 flp->name = fieldname;
15103 i = fip->fnfieldlists.size () - 1;
15104 }
15105
15106 /* Create a new member function field and add it to the vector of
15107 fnfieldlists. */
15108 flp->fnfields.emplace_back ();
15109 fnp = &flp->fnfields.back ();
15110
15111 /* Delay processing of the physname until later. */
15112 if (cu->language == language_cplus)
15113 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15114 die, cu);
15115 else
15116 {
15117 const char *physname = dwarf2_physname (fieldname, die, cu);
15118 fnp->physname = physname ? physname : "";
15119 }
15120
15121 fnp->type = alloc_type (objfile);
15122 this_type = read_type_die (die, cu);
15123 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15124 {
15125 int nparams = TYPE_NFIELDS (this_type);
15126
15127 /* TYPE is the domain of this method, and THIS_TYPE is the type
15128 of the method itself (TYPE_CODE_METHOD). */
15129 smash_to_method_type (fnp->type, type,
15130 TYPE_TARGET_TYPE (this_type),
15131 TYPE_FIELDS (this_type),
15132 TYPE_NFIELDS (this_type),
15133 TYPE_VARARGS (this_type));
15134
15135 /* Handle static member functions.
15136 Dwarf2 has no clean way to discern C++ static and non-static
15137 member functions. G++ helps GDB by marking the first
15138 parameter for non-static member functions (which is the this
15139 pointer) as artificial. We obtain this information from
15140 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15141 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15142 fnp->voffset = VOFFSET_STATIC;
15143 }
15144 else
15145 complaint (_("member function type missing for '%s'"),
15146 dwarf2_full_name (fieldname, die, cu));
15147
15148 /* Get fcontext from DW_AT_containing_type if present. */
15149 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15150 fnp->fcontext = die_containing_type (die, cu);
15151
15152 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15153 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15154
15155 /* Get accessibility. */
15156 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15157 if (attr != nullptr)
15158 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15159 else
15160 accessibility = dwarf2_default_access_attribute (die, cu);
15161 switch (accessibility)
15162 {
15163 case DW_ACCESS_private:
15164 fnp->is_private = 1;
15165 break;
15166 case DW_ACCESS_protected:
15167 fnp->is_protected = 1;
15168 break;
15169 }
15170
15171 /* Check for artificial methods. */
15172 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15173 if (attr && DW_UNSND (attr) != 0)
15174 fnp->is_artificial = 1;
15175
15176 /* Check for defaulted methods. */
15177 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15178 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15179 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15180
15181 /* Check for deleted methods. */
15182 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15183 if (attr != nullptr && DW_UNSND (attr) != 0)
15184 fnp->is_deleted = 1;
15185
15186 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15187
15188 /* Get index in virtual function table if it is a virtual member
15189 function. For older versions of GCC, this is an offset in the
15190 appropriate virtual table, as specified by DW_AT_containing_type.
15191 For everyone else, it is an expression to be evaluated relative
15192 to the object address. */
15193
15194 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15195 if (attr != nullptr)
15196 {
15197 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15198 {
15199 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15200 {
15201 /* Old-style GCC. */
15202 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15203 }
15204 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15205 || (DW_BLOCK (attr)->size > 1
15206 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15207 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15208 {
15209 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15210 if ((fnp->voffset % cu->header.addr_size) != 0)
15211 dwarf2_complex_location_expr_complaint ();
15212 else
15213 fnp->voffset /= cu->header.addr_size;
15214 fnp->voffset += 2;
15215 }
15216 else
15217 dwarf2_complex_location_expr_complaint ();
15218
15219 if (!fnp->fcontext)
15220 {
15221 /* If there is no `this' field and no DW_AT_containing_type,
15222 we cannot actually find a base class context for the
15223 vtable! */
15224 if (TYPE_NFIELDS (this_type) == 0
15225 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15226 {
15227 complaint (_("cannot determine context for virtual member "
15228 "function \"%s\" (offset %s)"),
15229 fieldname, sect_offset_str (die->sect_off));
15230 }
15231 else
15232 {
15233 fnp->fcontext
15234 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15235 }
15236 }
15237 }
15238 else if (attr->form_is_section_offset ())
15239 {
15240 dwarf2_complex_location_expr_complaint ();
15241 }
15242 else
15243 {
15244 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15245 fieldname);
15246 }
15247 }
15248 else
15249 {
15250 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15251 if (attr && DW_UNSND (attr))
15252 {
15253 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15254 complaint (_("Member function \"%s\" (offset %s) is virtual "
15255 "but the vtable offset is not specified"),
15256 fieldname, sect_offset_str (die->sect_off));
15257 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15258 TYPE_CPLUS_DYNAMIC (type) = 1;
15259 }
15260 }
15261 }
15262
15263 /* Create the vector of member function fields, and attach it to the type. */
15264
15265 static void
15266 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15267 struct dwarf2_cu *cu)
15268 {
15269 if (cu->language == language_ada)
15270 error (_("unexpected member functions in Ada type"));
15271
15272 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15273 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15274 TYPE_ALLOC (type,
15275 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15276
15277 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15278 {
15279 struct fnfieldlist &nf = fip->fnfieldlists[i];
15280 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15281
15282 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15283 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15284 fn_flp->fn_fields = (struct fn_field *)
15285 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15286
15287 for (int k = 0; k < nf.fnfields.size (); ++k)
15288 fn_flp->fn_fields[k] = nf.fnfields[k];
15289 }
15290
15291 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15292 }
15293
15294 /* Returns non-zero if NAME is the name of a vtable member in CU's
15295 language, zero otherwise. */
15296 static int
15297 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15298 {
15299 static const char vptr[] = "_vptr";
15300
15301 /* Look for the C++ form of the vtable. */
15302 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15303 return 1;
15304
15305 return 0;
15306 }
15307
15308 /* GCC outputs unnamed structures that are really pointers to member
15309 functions, with the ABI-specified layout. If TYPE describes
15310 such a structure, smash it into a member function type.
15311
15312 GCC shouldn't do this; it should just output pointer to member DIEs.
15313 This is GCC PR debug/28767. */
15314
15315 static void
15316 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15317 {
15318 struct type *pfn_type, *self_type, *new_type;
15319
15320 /* Check for a structure with no name and two children. */
15321 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15322 return;
15323
15324 /* Check for __pfn and __delta members. */
15325 if (TYPE_FIELD_NAME (type, 0) == NULL
15326 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15327 || TYPE_FIELD_NAME (type, 1) == NULL
15328 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15329 return;
15330
15331 /* Find the type of the method. */
15332 pfn_type = TYPE_FIELD_TYPE (type, 0);
15333 if (pfn_type == NULL
15334 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15335 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15336 return;
15337
15338 /* Look for the "this" argument. */
15339 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15340 if (TYPE_NFIELDS (pfn_type) == 0
15341 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15342 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15343 return;
15344
15345 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15346 new_type = alloc_type (objfile);
15347 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15348 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15349 TYPE_VARARGS (pfn_type));
15350 smash_to_methodptr_type (type, new_type);
15351 }
15352
15353 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15354 appropriate error checking and issuing complaints if there is a
15355 problem. */
15356
15357 static ULONGEST
15358 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15359 {
15360 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15361
15362 if (attr == nullptr)
15363 return 0;
15364
15365 if (!attr->form_is_constant ())
15366 {
15367 complaint (_("DW_AT_alignment must have constant form"
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
15370 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15371 return 0;
15372 }
15373
15374 ULONGEST align;
15375 if (attr->form == DW_FORM_sdata)
15376 {
15377 LONGEST val = DW_SND (attr);
15378 if (val < 0)
15379 {
15380 complaint (_("DW_AT_alignment value must not be negative"
15381 " - DIE at %s [in module %s]"),
15382 sect_offset_str (die->sect_off),
15383 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15384 return 0;
15385 }
15386 align = val;
15387 }
15388 else
15389 align = DW_UNSND (attr);
15390
15391 if (align == 0)
15392 {
15393 complaint (_("DW_AT_alignment value must not be zero"
15394 " - DIE at %s [in module %s]"),
15395 sect_offset_str (die->sect_off),
15396 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15397 return 0;
15398 }
15399 if ((align & (align - 1)) != 0)
15400 {
15401 complaint (_("DW_AT_alignment value must be a power of 2"
15402 " - DIE at %s [in module %s]"),
15403 sect_offset_str (die->sect_off),
15404 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15405 return 0;
15406 }
15407
15408 return align;
15409 }
15410
15411 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15412 the alignment for TYPE. */
15413
15414 static void
15415 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15416 struct type *type)
15417 {
15418 if (!set_type_align (type, get_alignment (cu, die)))
15419 complaint (_("DW_AT_alignment value too large"
15420 " - DIE at %s [in module %s]"),
15421 sect_offset_str (die->sect_off),
15422 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15423 }
15424
15425 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15426 constant for a type, according to DWARF5 spec, Table 5.5. */
15427
15428 static bool
15429 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15430 {
15431 switch (value)
15432 {
15433 case DW_CC_normal:
15434 case DW_CC_pass_by_reference:
15435 case DW_CC_pass_by_value:
15436 return true;
15437
15438 default:
15439 complaint (_("unrecognized DW_AT_calling_convention value "
15440 "(%s) for a type"), pulongest (value));
15441 return false;
15442 }
15443 }
15444
15445 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15446 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15447 also according to GNU-specific values (see include/dwarf2.h). */
15448
15449 static bool
15450 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15451 {
15452 switch (value)
15453 {
15454 case DW_CC_normal:
15455 case DW_CC_program:
15456 case DW_CC_nocall:
15457 return true;
15458
15459 case DW_CC_GNU_renesas_sh:
15460 case DW_CC_GNU_borland_fastcall_i386:
15461 case DW_CC_GDB_IBM_OpenCL:
15462 return true;
15463
15464 default:
15465 complaint (_("unrecognized DW_AT_calling_convention value "
15466 "(%s) for a subroutine"), pulongest (value));
15467 return false;
15468 }
15469 }
15470
15471 /* Called when we find the DIE that starts a structure or union scope
15472 (definition) to create a type for the structure or union. Fill in
15473 the type's name and general properties; the members will not be
15474 processed until process_structure_scope. A symbol table entry for
15475 the type will also not be done until process_structure_scope (assuming
15476 the type has a name).
15477
15478 NOTE: we need to call these functions regardless of whether or not the
15479 DIE has a DW_AT_name attribute, since it might be an anonymous
15480 structure or union. This gets the type entered into our set of
15481 user defined types. */
15482
15483 static struct type *
15484 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15485 {
15486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15487 struct type *type;
15488 struct attribute *attr;
15489 const char *name;
15490
15491 /* If the definition of this type lives in .debug_types, read that type.
15492 Don't follow DW_AT_specification though, that will take us back up
15493 the chain and we want to go down. */
15494 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15495 if (attr != nullptr)
15496 {
15497 type = get_DW_AT_signature_type (die, attr, cu);
15498
15499 /* The type's CU may not be the same as CU.
15500 Ensure TYPE is recorded with CU in die_type_hash. */
15501 return set_die_type (die, type, cu);
15502 }
15503
15504 type = alloc_type (objfile);
15505 INIT_CPLUS_SPECIFIC (type);
15506
15507 name = dwarf2_name (die, cu);
15508 if (name != NULL)
15509 {
15510 if (cu->language == language_cplus
15511 || cu->language == language_d
15512 || cu->language == language_rust)
15513 {
15514 const char *full_name = dwarf2_full_name (name, die, cu);
15515
15516 /* dwarf2_full_name might have already finished building the DIE's
15517 type. If so, there is no need to continue. */
15518 if (get_die_type (die, cu) != NULL)
15519 return get_die_type (die, cu);
15520
15521 TYPE_NAME (type) = full_name;
15522 }
15523 else
15524 {
15525 /* The name is already allocated along with this objfile, so
15526 we don't need to duplicate it for the type. */
15527 TYPE_NAME (type) = name;
15528 }
15529 }
15530
15531 if (die->tag == DW_TAG_structure_type)
15532 {
15533 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15534 }
15535 else if (die->tag == DW_TAG_union_type)
15536 {
15537 TYPE_CODE (type) = TYPE_CODE_UNION;
15538 }
15539 else if (die->tag == DW_TAG_variant_part)
15540 {
15541 TYPE_CODE (type) = TYPE_CODE_UNION;
15542 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15543 }
15544 else
15545 {
15546 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15547 }
15548
15549 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15550 TYPE_DECLARED_CLASS (type) = 1;
15551
15552 /* Store the calling convention in the type if it's available in
15553 the die. Otherwise the calling convention remains set to
15554 the default value DW_CC_normal. */
15555 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15556 if (attr != nullptr
15557 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15558 {
15559 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15560 TYPE_CPLUS_CALLING_CONVENTION (type)
15561 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15562 }
15563
15564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15565 if (attr != nullptr)
15566 {
15567 if (attr->form_is_constant ())
15568 TYPE_LENGTH (type) = DW_UNSND (attr);
15569 else
15570 {
15571 /* For the moment, dynamic type sizes are not supported
15572 by GDB's struct type. The actual size is determined
15573 on-demand when resolving the type of a given object,
15574 so set the type's length to zero for now. Otherwise,
15575 we record an expression as the length, and that expression
15576 could lead to a very large value, which could eventually
15577 lead to us trying to allocate that much memory when creating
15578 a value of that type. */
15579 TYPE_LENGTH (type) = 0;
15580 }
15581 }
15582 else
15583 {
15584 TYPE_LENGTH (type) = 0;
15585 }
15586
15587 maybe_set_alignment (cu, die, type);
15588
15589 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15590 {
15591 /* ICC<14 does not output the required DW_AT_declaration on
15592 incomplete types, but gives them a size of zero. */
15593 TYPE_STUB (type) = 1;
15594 }
15595 else
15596 TYPE_STUB_SUPPORTED (type) = 1;
15597
15598 if (die_is_declaration (die, cu))
15599 TYPE_STUB (type) = 1;
15600 else if (attr == NULL && die->child == NULL
15601 && producer_is_realview (cu->producer))
15602 /* RealView does not output the required DW_AT_declaration
15603 on incomplete types. */
15604 TYPE_STUB (type) = 1;
15605
15606 /* We need to add the type field to the die immediately so we don't
15607 infinitely recurse when dealing with pointers to the structure
15608 type within the structure itself. */
15609 set_die_type (die, type, cu);
15610
15611 /* set_die_type should be already done. */
15612 set_descriptive_type (type, die, cu);
15613
15614 return type;
15615 }
15616
15617 /* A helper for process_structure_scope that handles a single member
15618 DIE. */
15619
15620 static void
15621 handle_struct_member_die (struct die_info *child_die, struct type *type,
15622 struct field_info *fi,
15623 std::vector<struct symbol *> *template_args,
15624 struct dwarf2_cu *cu)
15625 {
15626 if (child_die->tag == DW_TAG_member
15627 || child_die->tag == DW_TAG_variable
15628 || child_die->tag == DW_TAG_variant_part)
15629 {
15630 /* NOTE: carlton/2002-11-05: A C++ static data member
15631 should be a DW_TAG_member that is a declaration, but
15632 all versions of G++ as of this writing (so through at
15633 least 3.2.1) incorrectly generate DW_TAG_variable
15634 tags for them instead. */
15635 dwarf2_add_field (fi, child_die, cu);
15636 }
15637 else if (child_die->tag == DW_TAG_subprogram)
15638 {
15639 /* Rust doesn't have member functions in the C++ sense.
15640 However, it does emit ordinary functions as children
15641 of a struct DIE. */
15642 if (cu->language == language_rust)
15643 read_func_scope (child_die, cu);
15644 else
15645 {
15646 /* C++ member function. */
15647 dwarf2_add_member_fn (fi, child_die, type, cu);
15648 }
15649 }
15650 else if (child_die->tag == DW_TAG_inheritance)
15651 {
15652 /* C++ base class field. */
15653 dwarf2_add_field (fi, child_die, cu);
15654 }
15655 else if (type_can_define_types (child_die))
15656 dwarf2_add_type_defn (fi, child_die, cu);
15657 else if (child_die->tag == DW_TAG_template_type_param
15658 || child_die->tag == DW_TAG_template_value_param)
15659 {
15660 struct symbol *arg = new_symbol (child_die, NULL, cu);
15661
15662 if (arg != NULL)
15663 template_args->push_back (arg);
15664 }
15665 else if (child_die->tag == DW_TAG_variant)
15666 {
15667 /* In a variant we want to get the discriminant and also add a
15668 field for our sole member child. */
15669 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15670
15671 for (die_info *variant_child = child_die->child;
15672 variant_child != NULL;
15673 variant_child = sibling_die (variant_child))
15674 {
15675 if (variant_child->tag == DW_TAG_member)
15676 {
15677 handle_struct_member_die (variant_child, type, fi,
15678 template_args, cu);
15679 /* Only handle the one. */
15680 break;
15681 }
15682 }
15683
15684 /* We don't handle this but we might as well report it if we see
15685 it. */
15686 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15687 complaint (_("DW_AT_discr_list is not supported yet"
15688 " - DIE at %s [in module %s]"),
15689 sect_offset_str (child_die->sect_off),
15690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15691
15692 /* The first field was just added, so we can stash the
15693 discriminant there. */
15694 gdb_assert (!fi->fields.empty ());
15695 if (discr == NULL)
15696 fi->fields.back ().variant.default_branch = true;
15697 else
15698 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15699 }
15700 }
15701
15702 /* Finish creating a structure or union type, including filling in
15703 its members and creating a symbol for it. */
15704
15705 static void
15706 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15707 {
15708 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15709 struct die_info *child_die;
15710 struct type *type;
15711
15712 type = get_die_type (die, cu);
15713 if (type == NULL)
15714 type = read_structure_type (die, cu);
15715
15716 /* When reading a DW_TAG_variant_part, we need to notice when we
15717 read the discriminant member, so we can record it later in the
15718 discriminant_info. */
15719 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15720 sect_offset discr_offset {};
15721 bool has_template_parameters = false;
15722
15723 if (is_variant_part)
15724 {
15725 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15726 if (discr == NULL)
15727 {
15728 /* Maybe it's a univariant form, an extension we support.
15729 In this case arrange not to check the offset. */
15730 is_variant_part = false;
15731 }
15732 else if (discr->form_is_ref ())
15733 {
15734 struct dwarf2_cu *target_cu = cu;
15735 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15736
15737 discr_offset = target_die->sect_off;
15738 }
15739 else
15740 {
15741 complaint (_("DW_AT_discr does not have DIE reference form"
15742 " - DIE at %s [in module %s]"),
15743 sect_offset_str (die->sect_off),
15744 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15745 is_variant_part = false;
15746 }
15747 }
15748
15749 if (die->child != NULL && ! die_is_declaration (die, cu))
15750 {
15751 struct field_info fi;
15752 std::vector<struct symbol *> template_args;
15753
15754 child_die = die->child;
15755
15756 while (child_die && child_die->tag)
15757 {
15758 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15759
15760 if (is_variant_part && discr_offset == child_die->sect_off)
15761 fi.fields.back ().variant.is_discriminant = true;
15762
15763 child_die = sibling_die (child_die);
15764 }
15765
15766 /* Attach template arguments to type. */
15767 if (!template_args.empty ())
15768 {
15769 has_template_parameters = true;
15770 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15771 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15772 TYPE_TEMPLATE_ARGUMENTS (type)
15773 = XOBNEWVEC (&objfile->objfile_obstack,
15774 struct symbol *,
15775 TYPE_N_TEMPLATE_ARGUMENTS (type));
15776 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15777 template_args.data (),
15778 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15779 * sizeof (struct symbol *)));
15780 }
15781
15782 /* Attach fields and member functions to the type. */
15783 if (fi.nfields)
15784 dwarf2_attach_fields_to_type (&fi, type, cu);
15785 if (!fi.fnfieldlists.empty ())
15786 {
15787 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15788
15789 /* Get the type which refers to the base class (possibly this
15790 class itself) which contains the vtable pointer for the current
15791 class from the DW_AT_containing_type attribute. This use of
15792 DW_AT_containing_type is a GNU extension. */
15793
15794 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15795 {
15796 struct type *t = die_containing_type (die, cu);
15797
15798 set_type_vptr_basetype (type, t);
15799 if (type == t)
15800 {
15801 int i;
15802
15803 /* Our own class provides vtbl ptr. */
15804 for (i = TYPE_NFIELDS (t) - 1;
15805 i >= TYPE_N_BASECLASSES (t);
15806 --i)
15807 {
15808 const char *fieldname = TYPE_FIELD_NAME (t, i);
15809
15810 if (is_vtable_name (fieldname, cu))
15811 {
15812 set_type_vptr_fieldno (type, i);
15813 break;
15814 }
15815 }
15816
15817 /* Complain if virtual function table field not found. */
15818 if (i < TYPE_N_BASECLASSES (t))
15819 complaint (_("virtual function table pointer "
15820 "not found when defining class '%s'"),
15821 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15822 }
15823 else
15824 {
15825 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15826 }
15827 }
15828 else if (cu->producer
15829 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15830 {
15831 /* The IBM XLC compiler does not provide direct indication
15832 of the containing type, but the vtable pointer is
15833 always named __vfp. */
15834
15835 int i;
15836
15837 for (i = TYPE_NFIELDS (type) - 1;
15838 i >= TYPE_N_BASECLASSES (type);
15839 --i)
15840 {
15841 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15842 {
15843 set_type_vptr_fieldno (type, i);
15844 set_type_vptr_basetype (type, type);
15845 break;
15846 }
15847 }
15848 }
15849 }
15850
15851 /* Copy fi.typedef_field_list linked list elements content into the
15852 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15853 if (!fi.typedef_field_list.empty ())
15854 {
15855 int count = fi.typedef_field_list.size ();
15856
15857 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15858 TYPE_TYPEDEF_FIELD_ARRAY (type)
15859 = ((struct decl_field *)
15860 TYPE_ALLOC (type,
15861 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15862 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15863
15864 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15865 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15866 }
15867
15868 /* Copy fi.nested_types_list linked list elements content into the
15869 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15870 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15871 {
15872 int count = fi.nested_types_list.size ();
15873
15874 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15875 TYPE_NESTED_TYPES_ARRAY (type)
15876 = ((struct decl_field *)
15877 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15878 TYPE_NESTED_TYPES_COUNT (type) = count;
15879
15880 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15881 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15882 }
15883 }
15884
15885 quirk_gcc_member_function_pointer (type, objfile);
15886 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15887 cu->rust_unions.push_back (type);
15888
15889 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15890 snapshots) has been known to create a die giving a declaration
15891 for a class that has, as a child, a die giving a definition for a
15892 nested class. So we have to process our children even if the
15893 current die is a declaration. Normally, of course, a declaration
15894 won't have any children at all. */
15895
15896 child_die = die->child;
15897
15898 while (child_die != NULL && child_die->tag)
15899 {
15900 if (child_die->tag == DW_TAG_member
15901 || child_die->tag == DW_TAG_variable
15902 || child_die->tag == DW_TAG_inheritance
15903 || child_die->tag == DW_TAG_template_value_param
15904 || child_die->tag == DW_TAG_template_type_param)
15905 {
15906 /* Do nothing. */
15907 }
15908 else
15909 process_die (child_die, cu);
15910
15911 child_die = sibling_die (child_die);
15912 }
15913
15914 /* Do not consider external references. According to the DWARF standard,
15915 these DIEs are identified by the fact that they have no byte_size
15916 attribute, and a declaration attribute. */
15917 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15918 || !die_is_declaration (die, cu))
15919 {
15920 struct symbol *sym = new_symbol (die, type, cu);
15921
15922 if (has_template_parameters)
15923 {
15924 struct symtab *symtab;
15925 if (sym != nullptr)
15926 symtab = symbol_symtab (sym);
15927 else if (cu->line_header != nullptr)
15928 {
15929 /* Any related symtab will do. */
15930 symtab
15931 = cu->line_header->file_names ()[0].symtab;
15932 }
15933 else
15934 {
15935 symtab = nullptr;
15936 complaint (_("could not find suitable "
15937 "symtab for template parameter"
15938 " - DIE at %s [in module %s]"),
15939 sect_offset_str (die->sect_off),
15940 objfile_name (objfile));
15941 }
15942
15943 if (symtab != nullptr)
15944 {
15945 /* Make sure that the symtab is set on the new symbols.
15946 Even though they don't appear in this symtab directly,
15947 other parts of gdb assume that symbols do, and this is
15948 reasonably true. */
15949 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15950 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15951 }
15952 }
15953 }
15954 }
15955
15956 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15957 update TYPE using some information only available in DIE's children. */
15958
15959 static void
15960 update_enumeration_type_from_children (struct die_info *die,
15961 struct type *type,
15962 struct dwarf2_cu *cu)
15963 {
15964 struct die_info *child_die;
15965 int unsigned_enum = 1;
15966 int flag_enum = 1;
15967 ULONGEST mask = 0;
15968
15969 auto_obstack obstack;
15970
15971 for (child_die = die->child;
15972 child_die != NULL && child_die->tag;
15973 child_die = sibling_die (child_die))
15974 {
15975 struct attribute *attr;
15976 LONGEST value;
15977 const gdb_byte *bytes;
15978 struct dwarf2_locexpr_baton *baton;
15979 const char *name;
15980
15981 if (child_die->tag != DW_TAG_enumerator)
15982 continue;
15983
15984 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15985 if (attr == NULL)
15986 continue;
15987
15988 name = dwarf2_name (child_die, cu);
15989 if (name == NULL)
15990 name = "<anonymous enumerator>";
15991
15992 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15993 &value, &bytes, &baton);
15994 if (value < 0)
15995 {
15996 unsigned_enum = 0;
15997 flag_enum = 0;
15998 }
15999 else if ((mask & value) != 0)
16000 flag_enum = 0;
16001 else
16002 mask |= value;
16003
16004 /* If we already know that the enum type is neither unsigned, nor
16005 a flag type, no need to look at the rest of the enumerates. */
16006 if (!unsigned_enum && !flag_enum)
16007 break;
16008 }
16009
16010 if (unsigned_enum)
16011 TYPE_UNSIGNED (type) = 1;
16012 if (flag_enum)
16013 TYPE_FLAG_ENUM (type) = 1;
16014 }
16015
16016 /* Given a DW_AT_enumeration_type die, set its type. We do not
16017 complete the type's fields yet, or create any symbols. */
16018
16019 static struct type *
16020 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16021 {
16022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16023 struct type *type;
16024 struct attribute *attr;
16025 const char *name;
16026
16027 /* If the definition of this type lives in .debug_types, read that type.
16028 Don't follow DW_AT_specification though, that will take us back up
16029 the chain and we want to go down. */
16030 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16031 if (attr != nullptr)
16032 {
16033 type = get_DW_AT_signature_type (die, attr, cu);
16034
16035 /* The type's CU may not be the same as CU.
16036 Ensure TYPE is recorded with CU in die_type_hash. */
16037 return set_die_type (die, type, cu);
16038 }
16039
16040 type = alloc_type (objfile);
16041
16042 TYPE_CODE (type) = TYPE_CODE_ENUM;
16043 name = dwarf2_full_name (NULL, die, cu);
16044 if (name != NULL)
16045 TYPE_NAME (type) = name;
16046
16047 attr = dwarf2_attr (die, DW_AT_type, cu);
16048 if (attr != NULL)
16049 {
16050 struct type *underlying_type = die_type (die, cu);
16051
16052 TYPE_TARGET_TYPE (type) = underlying_type;
16053 }
16054
16055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16056 if (attr != nullptr)
16057 {
16058 TYPE_LENGTH (type) = DW_UNSND (attr);
16059 }
16060 else
16061 {
16062 TYPE_LENGTH (type) = 0;
16063 }
16064
16065 maybe_set_alignment (cu, die, type);
16066
16067 /* The enumeration DIE can be incomplete. In Ada, any type can be
16068 declared as private in the package spec, and then defined only
16069 inside the package body. Such types are known as Taft Amendment
16070 Types. When another package uses such a type, an incomplete DIE
16071 may be generated by the compiler. */
16072 if (die_is_declaration (die, cu))
16073 TYPE_STUB (type) = 1;
16074
16075 /* Finish the creation of this type by using the enum's children.
16076 We must call this even when the underlying type has been provided
16077 so that we can determine if we're looking at a "flag" enum. */
16078 update_enumeration_type_from_children (die, type, cu);
16079
16080 /* If this type has an underlying type that is not a stub, then we
16081 may use its attributes. We always use the "unsigned" attribute
16082 in this situation, because ordinarily we guess whether the type
16083 is unsigned -- but the guess can be wrong and the underlying type
16084 can tell us the reality. However, we defer to a local size
16085 attribute if one exists, because this lets the compiler override
16086 the underlying type if needed. */
16087 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16088 {
16089 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16090 if (TYPE_LENGTH (type) == 0)
16091 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16092 if (TYPE_RAW_ALIGN (type) == 0
16093 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16094 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16095 }
16096
16097 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16098
16099 return set_die_type (die, type, cu);
16100 }
16101
16102 /* Given a pointer to a die which begins an enumeration, process all
16103 the dies that define the members of the enumeration, and create the
16104 symbol for the enumeration type.
16105
16106 NOTE: We reverse the order of the element list. */
16107
16108 static void
16109 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16110 {
16111 struct type *this_type;
16112
16113 this_type = get_die_type (die, cu);
16114 if (this_type == NULL)
16115 this_type = read_enumeration_type (die, cu);
16116
16117 if (die->child != NULL)
16118 {
16119 struct die_info *child_die;
16120 struct symbol *sym;
16121 std::vector<struct field> fields;
16122 const char *name;
16123
16124 child_die = die->child;
16125 while (child_die && child_die->tag)
16126 {
16127 if (child_die->tag != DW_TAG_enumerator)
16128 {
16129 process_die (child_die, cu);
16130 }
16131 else
16132 {
16133 name = dwarf2_name (child_die, cu);
16134 if (name)
16135 {
16136 sym = new_symbol (child_die, this_type, cu);
16137
16138 fields.emplace_back ();
16139 struct field &field = fields.back ();
16140
16141 FIELD_NAME (field) = sym->linkage_name ();
16142 FIELD_TYPE (field) = NULL;
16143 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16144 FIELD_BITSIZE (field) = 0;
16145 }
16146 }
16147
16148 child_die = sibling_die (child_die);
16149 }
16150
16151 if (!fields.empty ())
16152 {
16153 TYPE_NFIELDS (this_type) = fields.size ();
16154 TYPE_FIELDS (this_type) = (struct field *)
16155 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16156 memcpy (TYPE_FIELDS (this_type), fields.data (),
16157 sizeof (struct field) * fields.size ());
16158 }
16159 }
16160
16161 /* If we are reading an enum from a .debug_types unit, and the enum
16162 is a declaration, and the enum is not the signatured type in the
16163 unit, then we do not want to add a symbol for it. Adding a
16164 symbol would in some cases obscure the true definition of the
16165 enum, giving users an incomplete type when the definition is
16166 actually available. Note that we do not want to do this for all
16167 enums which are just declarations, because C++0x allows forward
16168 enum declarations. */
16169 if (cu->per_cu->is_debug_types
16170 && die_is_declaration (die, cu))
16171 {
16172 struct signatured_type *sig_type;
16173
16174 sig_type = (struct signatured_type *) cu->per_cu;
16175 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16176 if (sig_type->type_offset_in_section != die->sect_off)
16177 return;
16178 }
16179
16180 new_symbol (die, this_type, cu);
16181 }
16182
16183 /* Extract all information from a DW_TAG_array_type DIE and put it in
16184 the DIE's type field. For now, this only handles one dimensional
16185 arrays. */
16186
16187 static struct type *
16188 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16189 {
16190 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16191 struct die_info *child_die;
16192 struct type *type;
16193 struct type *element_type, *range_type, *index_type;
16194 struct attribute *attr;
16195 const char *name;
16196 struct dynamic_prop *byte_stride_prop = NULL;
16197 unsigned int bit_stride = 0;
16198
16199 element_type = die_type (die, cu);
16200
16201 /* The die_type call above may have already set the type for this DIE. */
16202 type = get_die_type (die, cu);
16203 if (type)
16204 return type;
16205
16206 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16207 if (attr != NULL)
16208 {
16209 int stride_ok;
16210 struct type *prop_type
16211 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16212
16213 byte_stride_prop
16214 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16215 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16216 prop_type);
16217 if (!stride_ok)
16218 {
16219 complaint (_("unable to read array DW_AT_byte_stride "
16220 " - DIE at %s [in module %s]"),
16221 sect_offset_str (die->sect_off),
16222 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16223 /* Ignore this attribute. We will likely not be able to print
16224 arrays of this type correctly, but there is little we can do
16225 to help if we cannot read the attribute's value. */
16226 byte_stride_prop = NULL;
16227 }
16228 }
16229
16230 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16231 if (attr != NULL)
16232 bit_stride = DW_UNSND (attr);
16233
16234 /* Irix 6.2 native cc creates array types without children for
16235 arrays with unspecified length. */
16236 if (die->child == NULL)
16237 {
16238 index_type = objfile_type (objfile)->builtin_int;
16239 range_type = create_static_range_type (NULL, index_type, 0, -1);
16240 type = create_array_type_with_stride (NULL, element_type, range_type,
16241 byte_stride_prop, bit_stride);
16242 return set_die_type (die, type, cu);
16243 }
16244
16245 std::vector<struct type *> range_types;
16246 child_die = die->child;
16247 while (child_die && child_die->tag)
16248 {
16249 if (child_die->tag == DW_TAG_subrange_type)
16250 {
16251 struct type *child_type = read_type_die (child_die, cu);
16252
16253 if (child_type != NULL)
16254 {
16255 /* The range type was succesfully read. Save it for the
16256 array type creation. */
16257 range_types.push_back (child_type);
16258 }
16259 }
16260 child_die = sibling_die (child_die);
16261 }
16262
16263 /* Dwarf2 dimensions are output from left to right, create the
16264 necessary array types in backwards order. */
16265
16266 type = element_type;
16267
16268 if (read_array_order (die, cu) == DW_ORD_col_major)
16269 {
16270 int i = 0;
16271
16272 while (i < range_types.size ())
16273 type = create_array_type_with_stride (NULL, type, range_types[i++],
16274 byte_stride_prop, bit_stride);
16275 }
16276 else
16277 {
16278 size_t ndim = range_types.size ();
16279 while (ndim-- > 0)
16280 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16281 byte_stride_prop, bit_stride);
16282 }
16283
16284 /* Understand Dwarf2 support for vector types (like they occur on
16285 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16286 array type. This is not part of the Dwarf2/3 standard yet, but a
16287 custom vendor extension. The main difference between a regular
16288 array and the vector variant is that vectors are passed by value
16289 to functions. */
16290 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16291 if (attr != nullptr)
16292 make_vector_type (type);
16293
16294 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16295 implementation may choose to implement triple vectors using this
16296 attribute. */
16297 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16298 if (attr != nullptr)
16299 {
16300 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16301 TYPE_LENGTH (type) = DW_UNSND (attr);
16302 else
16303 complaint (_("DW_AT_byte_size for array type smaller "
16304 "than the total size of elements"));
16305 }
16306
16307 name = dwarf2_name (die, cu);
16308 if (name)
16309 TYPE_NAME (type) = name;
16310
16311 maybe_set_alignment (cu, die, type);
16312
16313 /* Install the type in the die. */
16314 set_die_type (die, type, cu);
16315
16316 /* set_die_type should be already done. */
16317 set_descriptive_type (type, die, cu);
16318
16319 return type;
16320 }
16321
16322 static enum dwarf_array_dim_ordering
16323 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16324 {
16325 struct attribute *attr;
16326
16327 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16328
16329 if (attr != nullptr)
16330 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16331
16332 /* GNU F77 is a special case, as at 08/2004 array type info is the
16333 opposite order to the dwarf2 specification, but data is still
16334 laid out as per normal fortran.
16335
16336 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16337 version checking. */
16338
16339 if (cu->language == language_fortran
16340 && cu->producer && strstr (cu->producer, "GNU F77"))
16341 {
16342 return DW_ORD_row_major;
16343 }
16344
16345 switch (cu->language_defn->la_array_ordering)
16346 {
16347 case array_column_major:
16348 return DW_ORD_col_major;
16349 case array_row_major:
16350 default:
16351 return DW_ORD_row_major;
16352 };
16353 }
16354
16355 /* Extract all information from a DW_TAG_set_type DIE and put it in
16356 the DIE's type field. */
16357
16358 static struct type *
16359 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16360 {
16361 struct type *domain_type, *set_type;
16362 struct attribute *attr;
16363
16364 domain_type = die_type (die, cu);
16365
16366 /* The die_type call above may have already set the type for this DIE. */
16367 set_type = get_die_type (die, cu);
16368 if (set_type)
16369 return set_type;
16370
16371 set_type = create_set_type (NULL, domain_type);
16372
16373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16374 if (attr != nullptr)
16375 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16376
16377 maybe_set_alignment (cu, die, set_type);
16378
16379 return set_die_type (die, set_type, cu);
16380 }
16381
16382 /* A helper for read_common_block that creates a locexpr baton.
16383 SYM is the symbol which we are marking as computed.
16384 COMMON_DIE is the DIE for the common block.
16385 COMMON_LOC is the location expression attribute for the common
16386 block itself.
16387 MEMBER_LOC is the location expression attribute for the particular
16388 member of the common block that we are processing.
16389 CU is the CU from which the above come. */
16390
16391 static void
16392 mark_common_block_symbol_computed (struct symbol *sym,
16393 struct die_info *common_die,
16394 struct attribute *common_loc,
16395 struct attribute *member_loc,
16396 struct dwarf2_cu *cu)
16397 {
16398 struct dwarf2_per_objfile *dwarf2_per_objfile
16399 = cu->per_cu->dwarf2_per_objfile;
16400 struct objfile *objfile = dwarf2_per_objfile->objfile;
16401 struct dwarf2_locexpr_baton *baton;
16402 gdb_byte *ptr;
16403 unsigned int cu_off;
16404 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16405 LONGEST offset = 0;
16406
16407 gdb_assert (common_loc && member_loc);
16408 gdb_assert (common_loc->form_is_block ());
16409 gdb_assert (member_loc->form_is_block ()
16410 || member_loc->form_is_constant ());
16411
16412 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16413 baton->per_cu = cu->per_cu;
16414 gdb_assert (baton->per_cu);
16415
16416 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16417
16418 if (member_loc->form_is_constant ())
16419 {
16420 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16421 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16422 }
16423 else
16424 baton->size += DW_BLOCK (member_loc)->size;
16425
16426 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16427 baton->data = ptr;
16428
16429 *ptr++ = DW_OP_call4;
16430 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16431 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16432 ptr += 4;
16433
16434 if (member_loc->form_is_constant ())
16435 {
16436 *ptr++ = DW_OP_addr;
16437 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16438 ptr += cu->header.addr_size;
16439 }
16440 else
16441 {
16442 /* We have to copy the data here, because DW_OP_call4 will only
16443 use a DW_AT_location attribute. */
16444 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16445 ptr += DW_BLOCK (member_loc)->size;
16446 }
16447
16448 *ptr++ = DW_OP_plus;
16449 gdb_assert (ptr - baton->data == baton->size);
16450
16451 SYMBOL_LOCATION_BATON (sym) = baton;
16452 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16453 }
16454
16455 /* Create appropriate locally-scoped variables for all the
16456 DW_TAG_common_block entries. Also create a struct common_block
16457 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16458 is used to separate the common blocks name namespace from regular
16459 variable names. */
16460
16461 static void
16462 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16463 {
16464 struct attribute *attr;
16465
16466 attr = dwarf2_attr (die, DW_AT_location, cu);
16467 if (attr != nullptr)
16468 {
16469 /* Support the .debug_loc offsets. */
16470 if (attr->form_is_block ())
16471 {
16472 /* Ok. */
16473 }
16474 else if (attr->form_is_section_offset ())
16475 {
16476 dwarf2_complex_location_expr_complaint ();
16477 attr = NULL;
16478 }
16479 else
16480 {
16481 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16482 "common block member");
16483 attr = NULL;
16484 }
16485 }
16486
16487 if (die->child != NULL)
16488 {
16489 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16490 struct die_info *child_die;
16491 size_t n_entries = 0, size;
16492 struct common_block *common_block;
16493 struct symbol *sym;
16494
16495 for (child_die = die->child;
16496 child_die && child_die->tag;
16497 child_die = sibling_die (child_die))
16498 ++n_entries;
16499
16500 size = (sizeof (struct common_block)
16501 + (n_entries - 1) * sizeof (struct symbol *));
16502 common_block
16503 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16504 size);
16505 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16506 common_block->n_entries = 0;
16507
16508 for (child_die = die->child;
16509 child_die && child_die->tag;
16510 child_die = sibling_die (child_die))
16511 {
16512 /* Create the symbol in the DW_TAG_common_block block in the current
16513 symbol scope. */
16514 sym = new_symbol (child_die, NULL, cu);
16515 if (sym != NULL)
16516 {
16517 struct attribute *member_loc;
16518
16519 common_block->contents[common_block->n_entries++] = sym;
16520
16521 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16522 cu);
16523 if (member_loc)
16524 {
16525 /* GDB has handled this for a long time, but it is
16526 not specified by DWARF. It seems to have been
16527 emitted by gfortran at least as recently as:
16528 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16529 complaint (_("Variable in common block has "
16530 "DW_AT_data_member_location "
16531 "- DIE at %s [in module %s]"),
16532 sect_offset_str (child_die->sect_off),
16533 objfile_name (objfile));
16534
16535 if (member_loc->form_is_section_offset ())
16536 dwarf2_complex_location_expr_complaint ();
16537 else if (member_loc->form_is_constant ()
16538 || member_loc->form_is_block ())
16539 {
16540 if (attr != nullptr)
16541 mark_common_block_symbol_computed (sym, die, attr,
16542 member_loc, cu);
16543 }
16544 else
16545 dwarf2_complex_location_expr_complaint ();
16546 }
16547 }
16548 }
16549
16550 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16551 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16552 }
16553 }
16554
16555 /* Create a type for a C++ namespace. */
16556
16557 static struct type *
16558 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16559 {
16560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16561 const char *previous_prefix, *name;
16562 int is_anonymous;
16563 struct type *type;
16564
16565 /* For extensions, reuse the type of the original namespace. */
16566 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16567 {
16568 struct die_info *ext_die;
16569 struct dwarf2_cu *ext_cu = cu;
16570
16571 ext_die = dwarf2_extension (die, &ext_cu);
16572 type = read_type_die (ext_die, ext_cu);
16573
16574 /* EXT_CU may not be the same as CU.
16575 Ensure TYPE is recorded with CU in die_type_hash. */
16576 return set_die_type (die, type, cu);
16577 }
16578
16579 name = namespace_name (die, &is_anonymous, cu);
16580
16581 /* Now build the name of the current namespace. */
16582
16583 previous_prefix = determine_prefix (die, cu);
16584 if (previous_prefix[0] != '\0')
16585 name = typename_concat (&objfile->objfile_obstack,
16586 previous_prefix, name, 0, cu);
16587
16588 /* Create the type. */
16589 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16590
16591 return set_die_type (die, type, cu);
16592 }
16593
16594 /* Read a namespace scope. */
16595
16596 static void
16597 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16598 {
16599 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16600 int is_anonymous;
16601
16602 /* Add a symbol associated to this if we haven't seen the namespace
16603 before. Also, add a using directive if it's an anonymous
16604 namespace. */
16605
16606 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16607 {
16608 struct type *type;
16609
16610 type = read_type_die (die, cu);
16611 new_symbol (die, type, cu);
16612
16613 namespace_name (die, &is_anonymous, cu);
16614 if (is_anonymous)
16615 {
16616 const char *previous_prefix = determine_prefix (die, cu);
16617
16618 std::vector<const char *> excludes;
16619 add_using_directive (using_directives (cu),
16620 previous_prefix, TYPE_NAME (type), NULL,
16621 NULL, excludes, 0, &objfile->objfile_obstack);
16622 }
16623 }
16624
16625 if (die->child != NULL)
16626 {
16627 struct die_info *child_die = die->child;
16628
16629 while (child_die && child_die->tag)
16630 {
16631 process_die (child_die, cu);
16632 child_die = sibling_die (child_die);
16633 }
16634 }
16635 }
16636
16637 /* Read a Fortran module as type. This DIE can be only a declaration used for
16638 imported module. Still we need that type as local Fortran "use ... only"
16639 declaration imports depend on the created type in determine_prefix. */
16640
16641 static struct type *
16642 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16643 {
16644 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16645 const char *module_name;
16646 struct type *type;
16647
16648 module_name = dwarf2_name (die, cu);
16649 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16650
16651 return set_die_type (die, type, cu);
16652 }
16653
16654 /* Read a Fortran module. */
16655
16656 static void
16657 read_module (struct die_info *die, struct dwarf2_cu *cu)
16658 {
16659 struct die_info *child_die = die->child;
16660 struct type *type;
16661
16662 type = read_type_die (die, cu);
16663 new_symbol (die, type, cu);
16664
16665 while (child_die && child_die->tag)
16666 {
16667 process_die (child_die, cu);
16668 child_die = sibling_die (child_die);
16669 }
16670 }
16671
16672 /* Return the name of the namespace represented by DIE. Set
16673 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16674 namespace. */
16675
16676 static const char *
16677 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16678 {
16679 struct die_info *current_die;
16680 const char *name = NULL;
16681
16682 /* Loop through the extensions until we find a name. */
16683
16684 for (current_die = die;
16685 current_die != NULL;
16686 current_die = dwarf2_extension (die, &cu))
16687 {
16688 /* We don't use dwarf2_name here so that we can detect the absence
16689 of a name -> anonymous namespace. */
16690 name = dwarf2_string_attr (die, DW_AT_name, cu);
16691
16692 if (name != NULL)
16693 break;
16694 }
16695
16696 /* Is it an anonymous namespace? */
16697
16698 *is_anonymous = (name == NULL);
16699 if (*is_anonymous)
16700 name = CP_ANONYMOUS_NAMESPACE_STR;
16701
16702 return name;
16703 }
16704
16705 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16706 the user defined type vector. */
16707
16708 static struct type *
16709 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16710 {
16711 struct gdbarch *gdbarch
16712 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16713 struct comp_unit_head *cu_header = &cu->header;
16714 struct type *type;
16715 struct attribute *attr_byte_size;
16716 struct attribute *attr_address_class;
16717 int byte_size, addr_class;
16718 struct type *target_type;
16719
16720 target_type = die_type (die, cu);
16721
16722 /* The die_type call above may have already set the type for this DIE. */
16723 type = get_die_type (die, cu);
16724 if (type)
16725 return type;
16726
16727 type = lookup_pointer_type (target_type);
16728
16729 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16730 if (attr_byte_size)
16731 byte_size = DW_UNSND (attr_byte_size);
16732 else
16733 byte_size = cu_header->addr_size;
16734
16735 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16736 if (attr_address_class)
16737 addr_class = DW_UNSND (attr_address_class);
16738 else
16739 addr_class = DW_ADDR_none;
16740
16741 ULONGEST alignment = get_alignment (cu, die);
16742
16743 /* If the pointer size, alignment, or address class is different
16744 than the default, create a type variant marked as such and set
16745 the length accordingly. */
16746 if (TYPE_LENGTH (type) != byte_size
16747 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16748 && alignment != TYPE_RAW_ALIGN (type))
16749 || addr_class != DW_ADDR_none)
16750 {
16751 if (gdbarch_address_class_type_flags_p (gdbarch))
16752 {
16753 int type_flags;
16754
16755 type_flags = gdbarch_address_class_type_flags
16756 (gdbarch, byte_size, addr_class);
16757 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16758 == 0);
16759 type = make_type_with_address_space (type, type_flags);
16760 }
16761 else if (TYPE_LENGTH (type) != byte_size)
16762 {
16763 complaint (_("invalid pointer size %d"), byte_size);
16764 }
16765 else if (TYPE_RAW_ALIGN (type) != alignment)
16766 {
16767 complaint (_("Invalid DW_AT_alignment"
16768 " - DIE at %s [in module %s]"),
16769 sect_offset_str (die->sect_off),
16770 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16771 }
16772 else
16773 {
16774 /* Should we also complain about unhandled address classes? */
16775 }
16776 }
16777
16778 TYPE_LENGTH (type) = byte_size;
16779 set_type_align (type, alignment);
16780 return set_die_type (die, type, cu);
16781 }
16782
16783 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16784 the user defined type vector. */
16785
16786 static struct type *
16787 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16788 {
16789 struct type *type;
16790 struct type *to_type;
16791 struct type *domain;
16792
16793 to_type = die_type (die, cu);
16794 domain = die_containing_type (die, cu);
16795
16796 /* The calls above may have already set the type for this DIE. */
16797 type = get_die_type (die, cu);
16798 if (type)
16799 return type;
16800
16801 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16802 type = lookup_methodptr_type (to_type);
16803 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16804 {
16805 struct type *new_type
16806 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16807
16808 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16809 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16810 TYPE_VARARGS (to_type));
16811 type = lookup_methodptr_type (new_type);
16812 }
16813 else
16814 type = lookup_memberptr_type (to_type, domain);
16815
16816 return set_die_type (die, type, cu);
16817 }
16818
16819 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16820 the user defined type vector. */
16821
16822 static struct type *
16823 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16824 enum type_code refcode)
16825 {
16826 struct comp_unit_head *cu_header = &cu->header;
16827 struct type *type, *target_type;
16828 struct attribute *attr;
16829
16830 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16831
16832 target_type = die_type (die, cu);
16833
16834 /* The die_type call above may have already set the type for this DIE. */
16835 type = get_die_type (die, cu);
16836 if (type)
16837 return type;
16838
16839 type = lookup_reference_type (target_type, refcode);
16840 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16841 if (attr != nullptr)
16842 {
16843 TYPE_LENGTH (type) = DW_UNSND (attr);
16844 }
16845 else
16846 {
16847 TYPE_LENGTH (type) = cu_header->addr_size;
16848 }
16849 maybe_set_alignment (cu, die, type);
16850 return set_die_type (die, type, cu);
16851 }
16852
16853 /* Add the given cv-qualifiers to the element type of the array. GCC
16854 outputs DWARF type qualifiers that apply to an array, not the
16855 element type. But GDB relies on the array element type to carry
16856 the cv-qualifiers. This mimics section 6.7.3 of the C99
16857 specification. */
16858
16859 static struct type *
16860 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16861 struct type *base_type, int cnst, int voltl)
16862 {
16863 struct type *el_type, *inner_array;
16864
16865 base_type = copy_type (base_type);
16866 inner_array = base_type;
16867
16868 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16869 {
16870 TYPE_TARGET_TYPE (inner_array) =
16871 copy_type (TYPE_TARGET_TYPE (inner_array));
16872 inner_array = TYPE_TARGET_TYPE (inner_array);
16873 }
16874
16875 el_type = TYPE_TARGET_TYPE (inner_array);
16876 cnst |= TYPE_CONST (el_type);
16877 voltl |= TYPE_VOLATILE (el_type);
16878 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16879
16880 return set_die_type (die, base_type, cu);
16881 }
16882
16883 static struct type *
16884 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16885 {
16886 struct type *base_type, *cv_type;
16887
16888 base_type = die_type (die, cu);
16889
16890 /* The die_type call above may have already set the type for this DIE. */
16891 cv_type = get_die_type (die, cu);
16892 if (cv_type)
16893 return cv_type;
16894
16895 /* In case the const qualifier is applied to an array type, the element type
16896 is so qualified, not the array type (section 6.7.3 of C99). */
16897 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16898 return add_array_cv_type (die, cu, base_type, 1, 0);
16899
16900 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16901 return set_die_type (die, cv_type, cu);
16902 }
16903
16904 static struct type *
16905 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16906 {
16907 struct type *base_type, *cv_type;
16908
16909 base_type = die_type (die, cu);
16910
16911 /* The die_type call above may have already set the type for this DIE. */
16912 cv_type = get_die_type (die, cu);
16913 if (cv_type)
16914 return cv_type;
16915
16916 /* In case the volatile qualifier is applied to an array type, the
16917 element type is so qualified, not the array type (section 6.7.3
16918 of C99). */
16919 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16920 return add_array_cv_type (die, cu, base_type, 0, 1);
16921
16922 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16923 return set_die_type (die, cv_type, cu);
16924 }
16925
16926 /* Handle DW_TAG_restrict_type. */
16927
16928 static struct type *
16929 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16930 {
16931 struct type *base_type, *cv_type;
16932
16933 base_type = die_type (die, cu);
16934
16935 /* The die_type call above may have already set the type for this DIE. */
16936 cv_type = get_die_type (die, cu);
16937 if (cv_type)
16938 return cv_type;
16939
16940 cv_type = make_restrict_type (base_type);
16941 return set_die_type (die, cv_type, cu);
16942 }
16943
16944 /* Handle DW_TAG_atomic_type. */
16945
16946 static struct type *
16947 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16948 {
16949 struct type *base_type, *cv_type;
16950
16951 base_type = die_type (die, cu);
16952
16953 /* The die_type call above may have already set the type for this DIE. */
16954 cv_type = get_die_type (die, cu);
16955 if (cv_type)
16956 return cv_type;
16957
16958 cv_type = make_atomic_type (base_type);
16959 return set_die_type (die, cv_type, cu);
16960 }
16961
16962 /* Extract all information from a DW_TAG_string_type DIE and add to
16963 the user defined type vector. It isn't really a user defined type,
16964 but it behaves like one, with other DIE's using an AT_user_def_type
16965 attribute to reference it. */
16966
16967 static struct type *
16968 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16969 {
16970 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16972 struct type *type, *range_type, *index_type, *char_type;
16973 struct attribute *attr;
16974 struct dynamic_prop prop;
16975 bool length_is_constant = true;
16976 LONGEST length;
16977
16978 /* There are a couple of places where bit sizes might be made use of
16979 when parsing a DW_TAG_string_type, however, no producer that we know
16980 of make use of these. Handling bit sizes that are a multiple of the
16981 byte size is easy enough, but what about other bit sizes? Lets deal
16982 with that problem when we have to. Warn about these attributes being
16983 unsupported, then parse the type and ignore them like we always
16984 have. */
16985 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16986 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16987 {
16988 static bool warning_printed = false;
16989 if (!warning_printed)
16990 {
16991 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16992 "currently supported on DW_TAG_string_type."));
16993 warning_printed = true;
16994 }
16995 }
16996
16997 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16998 if (attr != nullptr && !attr->form_is_constant ())
16999 {
17000 /* The string length describes the location at which the length of
17001 the string can be found. The size of the length field can be
17002 specified with one of the attributes below. */
17003 struct type *prop_type;
17004 struct attribute *len
17005 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17006 if (len == nullptr)
17007 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17008 if (len != nullptr && len->form_is_constant ())
17009 {
17010 /* Pass 0 as the default as we know this attribute is constant
17011 and the default value will not be returned. */
17012 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17013 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17014 }
17015 else
17016 {
17017 /* If the size is not specified then we assume it is the size of
17018 an address on this target. */
17019 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17020 }
17021
17022 /* Convert the attribute into a dynamic property. */
17023 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17024 length = 1;
17025 else
17026 length_is_constant = false;
17027 }
17028 else if (attr != nullptr)
17029 {
17030 /* This DW_AT_string_length just contains the length with no
17031 indirection. There's no need to create a dynamic property in this
17032 case. Pass 0 for the default value as we know it will not be
17033 returned in this case. */
17034 length = dwarf2_get_attr_constant_value (attr, 0);
17035 }
17036 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17037 {
17038 /* We don't currently support non-constant byte sizes for strings. */
17039 length = dwarf2_get_attr_constant_value (attr, 1);
17040 }
17041 else
17042 {
17043 /* Use 1 as a fallback length if we have nothing else. */
17044 length = 1;
17045 }
17046
17047 index_type = objfile_type (objfile)->builtin_int;
17048 if (length_is_constant)
17049 range_type = create_static_range_type (NULL, index_type, 1, length);
17050 else
17051 {
17052 struct dynamic_prop low_bound;
17053
17054 low_bound.kind = PROP_CONST;
17055 low_bound.data.const_val = 1;
17056 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17057 }
17058 char_type = language_string_char_type (cu->language_defn, gdbarch);
17059 type = create_string_type (NULL, char_type, range_type);
17060
17061 return set_die_type (die, type, cu);
17062 }
17063
17064 /* Assuming that DIE corresponds to a function, returns nonzero
17065 if the function is prototyped. */
17066
17067 static int
17068 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17069 {
17070 struct attribute *attr;
17071
17072 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17073 if (attr && (DW_UNSND (attr) != 0))
17074 return 1;
17075
17076 /* The DWARF standard implies that the DW_AT_prototyped attribute
17077 is only meaningful for C, but the concept also extends to other
17078 languages that allow unprototyped functions (Eg: Objective C).
17079 For all other languages, assume that functions are always
17080 prototyped. */
17081 if (cu->language != language_c
17082 && cu->language != language_objc
17083 && cu->language != language_opencl)
17084 return 1;
17085
17086 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17087 prototyped and unprototyped functions; default to prototyped,
17088 since that is more common in modern code (and RealView warns
17089 about unprototyped functions). */
17090 if (producer_is_realview (cu->producer))
17091 return 1;
17092
17093 return 0;
17094 }
17095
17096 /* Handle DIES due to C code like:
17097
17098 struct foo
17099 {
17100 int (*funcp)(int a, long l);
17101 int b;
17102 };
17103
17104 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17105
17106 static struct type *
17107 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17108 {
17109 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17110 struct type *type; /* Type that this function returns. */
17111 struct type *ftype; /* Function that returns above type. */
17112 struct attribute *attr;
17113
17114 type = die_type (die, cu);
17115
17116 /* The die_type call above may have already set the type for this DIE. */
17117 ftype = get_die_type (die, cu);
17118 if (ftype)
17119 return ftype;
17120
17121 ftype = lookup_function_type (type);
17122
17123 if (prototyped_function_p (die, cu))
17124 TYPE_PROTOTYPED (ftype) = 1;
17125
17126 /* Store the calling convention in the type if it's available in
17127 the subroutine die. Otherwise set the calling convention to
17128 the default value DW_CC_normal. */
17129 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17130 if (attr != nullptr
17131 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17132 TYPE_CALLING_CONVENTION (ftype)
17133 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17134 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17135 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17136 else
17137 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17138
17139 /* Record whether the function returns normally to its caller or not
17140 if the DWARF producer set that information. */
17141 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17142 if (attr && (DW_UNSND (attr) != 0))
17143 TYPE_NO_RETURN (ftype) = 1;
17144
17145 /* We need to add the subroutine type to the die immediately so
17146 we don't infinitely recurse when dealing with parameters
17147 declared as the same subroutine type. */
17148 set_die_type (die, ftype, cu);
17149
17150 if (die->child != NULL)
17151 {
17152 struct type *void_type = objfile_type (objfile)->builtin_void;
17153 struct die_info *child_die;
17154 int nparams, iparams;
17155
17156 /* Count the number of parameters.
17157 FIXME: GDB currently ignores vararg functions, but knows about
17158 vararg member functions. */
17159 nparams = 0;
17160 child_die = die->child;
17161 while (child_die && child_die->tag)
17162 {
17163 if (child_die->tag == DW_TAG_formal_parameter)
17164 nparams++;
17165 else if (child_die->tag == DW_TAG_unspecified_parameters)
17166 TYPE_VARARGS (ftype) = 1;
17167 child_die = sibling_die (child_die);
17168 }
17169
17170 /* Allocate storage for parameters and fill them in. */
17171 TYPE_NFIELDS (ftype) = nparams;
17172 TYPE_FIELDS (ftype) = (struct field *)
17173 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17174
17175 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17176 even if we error out during the parameters reading below. */
17177 for (iparams = 0; iparams < nparams; iparams++)
17178 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17179
17180 iparams = 0;
17181 child_die = die->child;
17182 while (child_die && child_die->tag)
17183 {
17184 if (child_die->tag == DW_TAG_formal_parameter)
17185 {
17186 struct type *arg_type;
17187
17188 /* DWARF version 2 has no clean way to discern C++
17189 static and non-static member functions. G++ helps
17190 GDB by marking the first parameter for non-static
17191 member functions (which is the this pointer) as
17192 artificial. We pass this information to
17193 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17194
17195 DWARF version 3 added DW_AT_object_pointer, which GCC
17196 4.5 does not yet generate. */
17197 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17198 if (attr != nullptr)
17199 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17200 else
17201 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17202 arg_type = die_type (child_die, cu);
17203
17204 /* RealView does not mark THIS as const, which the testsuite
17205 expects. GCC marks THIS as const in method definitions,
17206 but not in the class specifications (GCC PR 43053). */
17207 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17208 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17209 {
17210 int is_this = 0;
17211 struct dwarf2_cu *arg_cu = cu;
17212 const char *name = dwarf2_name (child_die, cu);
17213
17214 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17215 if (attr != nullptr)
17216 {
17217 /* If the compiler emits this, use it. */
17218 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17219 is_this = 1;
17220 }
17221 else if (name && strcmp (name, "this") == 0)
17222 /* Function definitions will have the argument names. */
17223 is_this = 1;
17224 else if (name == NULL && iparams == 0)
17225 /* Declarations may not have the names, so like
17226 elsewhere in GDB, assume an artificial first
17227 argument is "this". */
17228 is_this = 1;
17229
17230 if (is_this)
17231 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17232 arg_type, 0);
17233 }
17234
17235 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17236 iparams++;
17237 }
17238 child_die = sibling_die (child_die);
17239 }
17240 }
17241
17242 return ftype;
17243 }
17244
17245 static struct type *
17246 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17247 {
17248 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17249 const char *name = NULL;
17250 struct type *this_type, *target_type;
17251
17252 name = dwarf2_full_name (NULL, die, cu);
17253 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17254 TYPE_TARGET_STUB (this_type) = 1;
17255 set_die_type (die, this_type, cu);
17256 target_type = die_type (die, cu);
17257 if (target_type != this_type)
17258 TYPE_TARGET_TYPE (this_type) = target_type;
17259 else
17260 {
17261 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17262 spec and cause infinite loops in GDB. */
17263 complaint (_("Self-referential DW_TAG_typedef "
17264 "- DIE at %s [in module %s]"),
17265 sect_offset_str (die->sect_off), objfile_name (objfile));
17266 TYPE_TARGET_TYPE (this_type) = NULL;
17267 }
17268 return this_type;
17269 }
17270
17271 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17272 (which may be different from NAME) to the architecture back-end to allow
17273 it to guess the correct format if necessary. */
17274
17275 static struct type *
17276 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17277 const char *name_hint, enum bfd_endian byte_order)
17278 {
17279 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17280 const struct floatformat **format;
17281 struct type *type;
17282
17283 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17284 if (format)
17285 type = init_float_type (objfile, bits, name, format, byte_order);
17286 else
17287 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17288
17289 return type;
17290 }
17291
17292 /* Allocate an integer type of size BITS and name NAME. */
17293
17294 static struct type *
17295 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17296 int bits, int unsigned_p, const char *name)
17297 {
17298 struct type *type;
17299
17300 /* Versions of Intel's C Compiler generate an integer type called "void"
17301 instead of using DW_TAG_unspecified_type. This has been seen on
17302 at least versions 14, 17, and 18. */
17303 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17304 && strcmp (name, "void") == 0)
17305 type = objfile_type (objfile)->builtin_void;
17306 else
17307 type = init_integer_type (objfile, bits, unsigned_p, name);
17308
17309 return type;
17310 }
17311
17312 /* Initialise and return a floating point type of size BITS suitable for
17313 use as a component of a complex number. The NAME_HINT is passed through
17314 when initialising the floating point type and is the name of the complex
17315 type.
17316
17317 As DWARF doesn't currently provide an explicit name for the components
17318 of a complex number, but it can be helpful to have these components
17319 named, we try to select a suitable name based on the size of the
17320 component. */
17321 static struct type *
17322 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17323 struct objfile *objfile,
17324 int bits, const char *name_hint,
17325 enum bfd_endian byte_order)
17326 {
17327 gdbarch *gdbarch = get_objfile_arch (objfile);
17328 struct type *tt = nullptr;
17329
17330 /* Try to find a suitable floating point builtin type of size BITS.
17331 We're going to use the name of this type as the name for the complex
17332 target type that we are about to create. */
17333 switch (cu->language)
17334 {
17335 case language_fortran:
17336 switch (bits)
17337 {
17338 case 32:
17339 tt = builtin_f_type (gdbarch)->builtin_real;
17340 break;
17341 case 64:
17342 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17343 break;
17344 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17345 case 128:
17346 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17347 break;
17348 }
17349 break;
17350 default:
17351 switch (bits)
17352 {
17353 case 32:
17354 tt = builtin_type (gdbarch)->builtin_float;
17355 break;
17356 case 64:
17357 tt = builtin_type (gdbarch)->builtin_double;
17358 break;
17359 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17360 case 128:
17361 tt = builtin_type (gdbarch)->builtin_long_double;
17362 break;
17363 }
17364 break;
17365 }
17366
17367 /* If the type we found doesn't match the size we were looking for, then
17368 pretend we didn't find a type at all, the complex target type we
17369 create will then be nameless. */
17370 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17371 tt = nullptr;
17372
17373 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17374 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17375 }
17376
17377 /* Find a representation of a given base type and install
17378 it in the TYPE field of the die. */
17379
17380 static struct type *
17381 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17382 {
17383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17384 struct type *type;
17385 struct attribute *attr;
17386 int encoding = 0, bits = 0;
17387 const char *name;
17388 gdbarch *arch;
17389
17390 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17391 if (attr != nullptr)
17392 encoding = DW_UNSND (attr);
17393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17394 if (attr != nullptr)
17395 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17396 name = dwarf2_name (die, cu);
17397 if (!name)
17398 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17399
17400 arch = get_objfile_arch (objfile);
17401 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17402
17403 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17404 if (attr)
17405 {
17406 int endianity = DW_UNSND (attr);
17407
17408 switch (endianity)
17409 {
17410 case DW_END_big:
17411 byte_order = BFD_ENDIAN_BIG;
17412 break;
17413 case DW_END_little:
17414 byte_order = BFD_ENDIAN_LITTLE;
17415 break;
17416 default:
17417 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17418 break;
17419 }
17420 }
17421
17422 switch (encoding)
17423 {
17424 case DW_ATE_address:
17425 /* Turn DW_ATE_address into a void * pointer. */
17426 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17427 type = init_pointer_type (objfile, bits, name, type);
17428 break;
17429 case DW_ATE_boolean:
17430 type = init_boolean_type (objfile, bits, 1, name);
17431 break;
17432 case DW_ATE_complex_float:
17433 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17434 byte_order);
17435 type = init_complex_type (objfile, name, type);
17436 break;
17437 case DW_ATE_decimal_float:
17438 type = init_decfloat_type (objfile, bits, name);
17439 break;
17440 case DW_ATE_float:
17441 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17442 break;
17443 case DW_ATE_signed:
17444 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17445 break;
17446 case DW_ATE_unsigned:
17447 if (cu->language == language_fortran
17448 && name
17449 && startswith (name, "character("))
17450 type = init_character_type (objfile, bits, 1, name);
17451 else
17452 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17453 break;
17454 case DW_ATE_signed_char:
17455 if (cu->language == language_ada || cu->language == language_m2
17456 || cu->language == language_pascal
17457 || cu->language == language_fortran)
17458 type = init_character_type (objfile, bits, 0, name);
17459 else
17460 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17461 break;
17462 case DW_ATE_unsigned_char:
17463 if (cu->language == language_ada || cu->language == language_m2
17464 || cu->language == language_pascal
17465 || cu->language == language_fortran
17466 || cu->language == language_rust)
17467 type = init_character_type (objfile, bits, 1, name);
17468 else
17469 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17470 break;
17471 case DW_ATE_UTF:
17472 {
17473 if (bits == 16)
17474 type = builtin_type (arch)->builtin_char16;
17475 else if (bits == 32)
17476 type = builtin_type (arch)->builtin_char32;
17477 else
17478 {
17479 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17480 bits);
17481 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17482 }
17483 return set_die_type (die, type, cu);
17484 }
17485 break;
17486
17487 default:
17488 complaint (_("unsupported DW_AT_encoding: '%s'"),
17489 dwarf_type_encoding_name (encoding));
17490 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17491 break;
17492 }
17493
17494 if (name && strcmp (name, "char") == 0)
17495 TYPE_NOSIGN (type) = 1;
17496
17497 maybe_set_alignment (cu, die, type);
17498
17499 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17500
17501 return set_die_type (die, type, cu);
17502 }
17503
17504 /* Parse dwarf attribute if it's a block, reference or constant and put the
17505 resulting value of the attribute into struct bound_prop.
17506 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17507
17508 static int
17509 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17510 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17511 struct type *default_type)
17512 {
17513 struct dwarf2_property_baton *baton;
17514 struct obstack *obstack
17515 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17516
17517 gdb_assert (default_type != NULL);
17518
17519 if (attr == NULL || prop == NULL)
17520 return 0;
17521
17522 if (attr->form_is_block ())
17523 {
17524 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17525 baton->property_type = default_type;
17526 baton->locexpr.per_cu = cu->per_cu;
17527 baton->locexpr.size = DW_BLOCK (attr)->size;
17528 baton->locexpr.data = DW_BLOCK (attr)->data;
17529 switch (attr->name)
17530 {
17531 case DW_AT_string_length:
17532 baton->locexpr.is_reference = true;
17533 break;
17534 default:
17535 baton->locexpr.is_reference = false;
17536 break;
17537 }
17538 prop->data.baton = baton;
17539 prop->kind = PROP_LOCEXPR;
17540 gdb_assert (prop->data.baton != NULL);
17541 }
17542 else if (attr->form_is_ref ())
17543 {
17544 struct dwarf2_cu *target_cu = cu;
17545 struct die_info *target_die;
17546 struct attribute *target_attr;
17547
17548 target_die = follow_die_ref (die, attr, &target_cu);
17549 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17550 if (target_attr == NULL)
17551 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17552 target_cu);
17553 if (target_attr == NULL)
17554 return 0;
17555
17556 switch (target_attr->name)
17557 {
17558 case DW_AT_location:
17559 if (target_attr->form_is_section_offset ())
17560 {
17561 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17562 baton->property_type = die_type (target_die, target_cu);
17563 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17564 prop->data.baton = baton;
17565 prop->kind = PROP_LOCLIST;
17566 gdb_assert (prop->data.baton != NULL);
17567 }
17568 else if (target_attr->form_is_block ())
17569 {
17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17571 baton->property_type = die_type (target_die, target_cu);
17572 baton->locexpr.per_cu = cu->per_cu;
17573 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17574 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17575 baton->locexpr.is_reference = true;
17576 prop->data.baton = baton;
17577 prop->kind = PROP_LOCEXPR;
17578 gdb_assert (prop->data.baton != NULL);
17579 }
17580 else
17581 {
17582 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17583 "dynamic property");
17584 return 0;
17585 }
17586 break;
17587 case DW_AT_data_member_location:
17588 {
17589 LONGEST offset;
17590
17591 if (!handle_data_member_location (target_die, target_cu,
17592 &offset))
17593 return 0;
17594
17595 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17596 baton->property_type = read_type_die (target_die->parent,
17597 target_cu);
17598 baton->offset_info.offset = offset;
17599 baton->offset_info.type = die_type (target_die, target_cu);
17600 prop->data.baton = baton;
17601 prop->kind = PROP_ADDR_OFFSET;
17602 break;
17603 }
17604 }
17605 }
17606 else if (attr->form_is_constant ())
17607 {
17608 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17609 prop->kind = PROP_CONST;
17610 }
17611 else
17612 {
17613 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17614 dwarf2_name (die, cu));
17615 return 0;
17616 }
17617
17618 return 1;
17619 }
17620
17621 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17622 UNSIGNED_P controls if the integer is unsigned or not. */
17623
17624 static struct type *
17625 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17626 int size_in_bytes, bool unsigned_p)
17627 {
17628 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17629 struct type *int_type;
17630
17631 /* Helper macro to examine the various builtin types. */
17632 #define TRY_TYPE(F) \
17633 int_type = (unsigned_p \
17634 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17635 : objfile_type (objfile)->builtin_ ## F); \
17636 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17637 return int_type
17638
17639 TRY_TYPE (char);
17640 TRY_TYPE (short);
17641 TRY_TYPE (int);
17642 TRY_TYPE (long);
17643 TRY_TYPE (long_long);
17644
17645 #undef TRY_TYPE
17646
17647 gdb_assert_not_reached ("unable to find suitable integer type");
17648 }
17649
17650 /* Find an integer type the same size as the address size given in the
17651 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17652 is unsigned or not. */
17653
17654 static struct type *
17655 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17656 bool unsigned_p)
17657 {
17658 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17659 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17660 }
17661
17662 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17663 present (which is valid) then compute the default type based on the
17664 compilation units address size. */
17665
17666 static struct type *
17667 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17668 {
17669 struct type *index_type = die_type (die, cu);
17670
17671 /* Dwarf-2 specifications explicitly allows to create subrange types
17672 without specifying a base type.
17673 In that case, the base type must be set to the type of
17674 the lower bound, upper bound or count, in that order, if any of these
17675 three attributes references an object that has a type.
17676 If no base type is found, the Dwarf-2 specifications say that
17677 a signed integer type of size equal to the size of an address should
17678 be used.
17679 For the following C code: `extern char gdb_int [];'
17680 GCC produces an empty range DIE.
17681 FIXME: muller/2010-05-28: Possible references to object for low bound,
17682 high bound or count are not yet handled by this code. */
17683 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17684 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17685
17686 return index_type;
17687 }
17688
17689 /* Read the given DW_AT_subrange DIE. */
17690
17691 static struct type *
17692 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17693 {
17694 struct type *base_type, *orig_base_type;
17695 struct type *range_type;
17696 struct attribute *attr;
17697 struct dynamic_prop low, high;
17698 int low_default_is_valid;
17699 int high_bound_is_count = 0;
17700 const char *name;
17701 ULONGEST negative_mask;
17702
17703 orig_base_type = read_subrange_index_type (die, cu);
17704
17705 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17706 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17707 creating the range type, but we use the result of check_typedef
17708 when examining properties of the type. */
17709 base_type = check_typedef (orig_base_type);
17710
17711 /* The die_type call above may have already set the type for this DIE. */
17712 range_type = get_die_type (die, cu);
17713 if (range_type)
17714 return range_type;
17715
17716 low.kind = PROP_CONST;
17717 high.kind = PROP_CONST;
17718 high.data.const_val = 0;
17719
17720 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17721 omitting DW_AT_lower_bound. */
17722 switch (cu->language)
17723 {
17724 case language_c:
17725 case language_cplus:
17726 low.data.const_val = 0;
17727 low_default_is_valid = 1;
17728 break;
17729 case language_fortran:
17730 low.data.const_val = 1;
17731 low_default_is_valid = 1;
17732 break;
17733 case language_d:
17734 case language_objc:
17735 case language_rust:
17736 low.data.const_val = 0;
17737 low_default_is_valid = (cu->header.version >= 4);
17738 break;
17739 case language_ada:
17740 case language_m2:
17741 case language_pascal:
17742 low.data.const_val = 1;
17743 low_default_is_valid = (cu->header.version >= 4);
17744 break;
17745 default:
17746 low.data.const_val = 0;
17747 low_default_is_valid = 0;
17748 break;
17749 }
17750
17751 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17752 if (attr != nullptr)
17753 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17754 else if (!low_default_is_valid)
17755 complaint (_("Missing DW_AT_lower_bound "
17756 "- DIE at %s [in module %s]"),
17757 sect_offset_str (die->sect_off),
17758 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17759
17760 struct attribute *attr_ub, *attr_count;
17761 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17762 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17763 {
17764 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17765 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17766 {
17767 /* If bounds are constant do the final calculation here. */
17768 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17769 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17770 else
17771 high_bound_is_count = 1;
17772 }
17773 else
17774 {
17775 if (attr_ub != NULL)
17776 complaint (_("Unresolved DW_AT_upper_bound "
17777 "- DIE at %s [in module %s]"),
17778 sect_offset_str (die->sect_off),
17779 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17780 if (attr_count != NULL)
17781 complaint (_("Unresolved DW_AT_count "
17782 "- DIE at %s [in module %s]"),
17783 sect_offset_str (die->sect_off),
17784 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17785 }
17786 }
17787
17788 LONGEST bias = 0;
17789 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17790 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17791 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17792
17793 /* Normally, the DWARF producers are expected to use a signed
17794 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17795 But this is unfortunately not always the case, as witnessed
17796 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17797 is used instead. To work around that ambiguity, we treat
17798 the bounds as signed, and thus sign-extend their values, when
17799 the base type is signed. */
17800 negative_mask =
17801 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17802 if (low.kind == PROP_CONST
17803 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17804 low.data.const_val |= negative_mask;
17805 if (high.kind == PROP_CONST
17806 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17807 high.data.const_val |= negative_mask;
17808
17809 /* Check for bit and byte strides. */
17810 struct dynamic_prop byte_stride_prop;
17811 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17812 if (attr_byte_stride != nullptr)
17813 {
17814 struct type *prop_type
17815 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17816 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17817 prop_type);
17818 }
17819
17820 struct dynamic_prop bit_stride_prop;
17821 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17822 if (attr_bit_stride != nullptr)
17823 {
17824 /* It only makes sense to have either a bit or byte stride. */
17825 if (attr_byte_stride != nullptr)
17826 {
17827 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17828 "- DIE at %s [in module %s]"),
17829 sect_offset_str (die->sect_off),
17830 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17831 attr_bit_stride = nullptr;
17832 }
17833 else
17834 {
17835 struct type *prop_type
17836 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17837 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17838 prop_type);
17839 }
17840 }
17841
17842 if (attr_byte_stride != nullptr
17843 || attr_bit_stride != nullptr)
17844 {
17845 bool byte_stride_p = (attr_byte_stride != nullptr);
17846 struct dynamic_prop *stride
17847 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17848
17849 range_type
17850 = create_range_type_with_stride (NULL, orig_base_type, &low,
17851 &high, bias, stride, byte_stride_p);
17852 }
17853 else
17854 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17855
17856 if (high_bound_is_count)
17857 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17858
17859 /* Ada expects an empty array on no boundary attributes. */
17860 if (attr == NULL && cu->language != language_ada)
17861 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17862
17863 name = dwarf2_name (die, cu);
17864 if (name)
17865 TYPE_NAME (range_type) = name;
17866
17867 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17868 if (attr != nullptr)
17869 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17870
17871 maybe_set_alignment (cu, die, range_type);
17872
17873 set_die_type (die, range_type, cu);
17874
17875 /* set_die_type should be already done. */
17876 set_descriptive_type (range_type, die, cu);
17877
17878 return range_type;
17879 }
17880
17881 static struct type *
17882 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17883 {
17884 struct type *type;
17885
17886 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17887 NULL);
17888 TYPE_NAME (type) = dwarf2_name (die, cu);
17889
17890 /* In Ada, an unspecified type is typically used when the description
17891 of the type is deferred to a different unit. When encountering
17892 such a type, we treat it as a stub, and try to resolve it later on,
17893 when needed. */
17894 if (cu->language == language_ada)
17895 TYPE_STUB (type) = 1;
17896
17897 return set_die_type (die, type, cu);
17898 }
17899
17900 /* Read a single die and all its descendents. Set the die's sibling
17901 field to NULL; set other fields in the die correctly, and set all
17902 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17903 location of the info_ptr after reading all of those dies. PARENT
17904 is the parent of the die in question. */
17905
17906 static struct die_info *
17907 read_die_and_children (const struct die_reader_specs *reader,
17908 const gdb_byte *info_ptr,
17909 const gdb_byte **new_info_ptr,
17910 struct die_info *parent)
17911 {
17912 struct die_info *die;
17913 const gdb_byte *cur_ptr;
17914 int has_children;
17915
17916 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17917 if (die == NULL)
17918 {
17919 *new_info_ptr = cur_ptr;
17920 return NULL;
17921 }
17922 store_in_ref_table (die, reader->cu);
17923
17924 if (has_children)
17925 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17926 else
17927 {
17928 die->child = NULL;
17929 *new_info_ptr = cur_ptr;
17930 }
17931
17932 die->sibling = NULL;
17933 die->parent = parent;
17934 return die;
17935 }
17936
17937 /* Read a die, all of its descendents, and all of its siblings; set
17938 all of the fields of all of the dies correctly. Arguments are as
17939 in read_die_and_children. */
17940
17941 static struct die_info *
17942 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17943 const gdb_byte *info_ptr,
17944 const gdb_byte **new_info_ptr,
17945 struct die_info *parent)
17946 {
17947 struct die_info *first_die, *last_sibling;
17948 const gdb_byte *cur_ptr;
17949
17950 cur_ptr = info_ptr;
17951 first_die = last_sibling = NULL;
17952
17953 while (1)
17954 {
17955 struct die_info *die
17956 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17957
17958 if (die == NULL)
17959 {
17960 *new_info_ptr = cur_ptr;
17961 return first_die;
17962 }
17963
17964 if (!first_die)
17965 first_die = die;
17966 else
17967 last_sibling->sibling = die;
17968
17969 last_sibling = die;
17970 }
17971 }
17972
17973 /* Read a die, all of its descendents, and all of its siblings; set
17974 all of the fields of all of the dies correctly. Arguments are as
17975 in read_die_and_children.
17976 This the main entry point for reading a DIE and all its children. */
17977
17978 static struct die_info *
17979 read_die_and_siblings (const struct die_reader_specs *reader,
17980 const gdb_byte *info_ptr,
17981 const gdb_byte **new_info_ptr,
17982 struct die_info *parent)
17983 {
17984 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17985 new_info_ptr, parent);
17986
17987 if (dwarf_die_debug)
17988 {
17989 fprintf_unfiltered (gdb_stdlog,
17990 "Read die from %s@0x%x of %s:\n",
17991 reader->die_section->get_name (),
17992 (unsigned) (info_ptr - reader->die_section->buffer),
17993 bfd_get_filename (reader->abfd));
17994 dump_die (die, dwarf_die_debug);
17995 }
17996
17997 return die;
17998 }
17999
18000 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18001 attributes.
18002 The caller is responsible for filling in the extra attributes
18003 and updating (*DIEP)->num_attrs.
18004 Set DIEP to point to a newly allocated die with its information,
18005 except for its child, sibling, and parent fields.
18006 Set HAS_CHILDREN to tell whether the die has children or not. */
18007
18008 static const gdb_byte *
18009 read_full_die_1 (const struct die_reader_specs *reader,
18010 struct die_info **diep, const gdb_byte *info_ptr,
18011 int *has_children, int num_extra_attrs)
18012 {
18013 unsigned int abbrev_number, bytes_read, i;
18014 struct abbrev_info *abbrev;
18015 struct die_info *die;
18016 struct dwarf2_cu *cu = reader->cu;
18017 bfd *abfd = reader->abfd;
18018
18019 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18020 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18021 info_ptr += bytes_read;
18022 if (!abbrev_number)
18023 {
18024 *diep = NULL;
18025 *has_children = 0;
18026 return info_ptr;
18027 }
18028
18029 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18030 if (!abbrev)
18031 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18032 abbrev_number,
18033 bfd_get_filename (abfd));
18034
18035 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18036 die->sect_off = sect_off;
18037 die->tag = abbrev->tag;
18038 die->abbrev = abbrev_number;
18039
18040 /* Make the result usable.
18041 The caller needs to update num_attrs after adding the extra
18042 attributes. */
18043 die->num_attrs = abbrev->num_attrs;
18044
18045 std::vector<int> indexes_that_need_reprocess;
18046 for (i = 0; i < abbrev->num_attrs; ++i)
18047 {
18048 bool need_reprocess;
18049 info_ptr =
18050 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18051 info_ptr, &need_reprocess);
18052 if (need_reprocess)
18053 indexes_that_need_reprocess.push_back (i);
18054 }
18055
18056 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18057 if (attr != nullptr)
18058 cu->str_offsets_base = DW_UNSND (attr);
18059
18060 auto maybe_addr_base = lookup_addr_base(die);
18061 if (maybe_addr_base.has_value ())
18062 cu->addr_base = *maybe_addr_base;
18063 for (int index : indexes_that_need_reprocess)
18064 read_attribute_reprocess (reader, &die->attrs[index]);
18065 *diep = die;
18066 *has_children = abbrev->has_children;
18067 return info_ptr;
18068 }
18069
18070 /* Read a die and all its attributes.
18071 Set DIEP to point to a newly allocated die with its information,
18072 except for its child, sibling, and parent fields.
18073 Set HAS_CHILDREN to tell whether the die has children or not. */
18074
18075 static const gdb_byte *
18076 read_full_die (const struct die_reader_specs *reader,
18077 struct die_info **diep, const gdb_byte *info_ptr,
18078 int *has_children)
18079 {
18080 const gdb_byte *result;
18081
18082 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18083
18084 if (dwarf_die_debug)
18085 {
18086 fprintf_unfiltered (gdb_stdlog,
18087 "Read die from %s@0x%x of %s:\n",
18088 reader->die_section->get_name (),
18089 (unsigned) (info_ptr - reader->die_section->buffer),
18090 bfd_get_filename (reader->abfd));
18091 dump_die (*diep, dwarf_die_debug);
18092 }
18093
18094 return result;
18095 }
18096 \f
18097
18098 /* Returns nonzero if TAG represents a type that we might generate a partial
18099 symbol for. */
18100
18101 static int
18102 is_type_tag_for_partial (int tag)
18103 {
18104 switch (tag)
18105 {
18106 #if 0
18107 /* Some types that would be reasonable to generate partial symbols for,
18108 that we don't at present. */
18109 case DW_TAG_array_type:
18110 case DW_TAG_file_type:
18111 case DW_TAG_ptr_to_member_type:
18112 case DW_TAG_set_type:
18113 case DW_TAG_string_type:
18114 case DW_TAG_subroutine_type:
18115 #endif
18116 case DW_TAG_base_type:
18117 case DW_TAG_class_type:
18118 case DW_TAG_interface_type:
18119 case DW_TAG_enumeration_type:
18120 case DW_TAG_structure_type:
18121 case DW_TAG_subrange_type:
18122 case DW_TAG_typedef:
18123 case DW_TAG_union_type:
18124 return 1;
18125 default:
18126 return 0;
18127 }
18128 }
18129
18130 /* Load all DIEs that are interesting for partial symbols into memory. */
18131
18132 static struct partial_die_info *
18133 load_partial_dies (const struct die_reader_specs *reader,
18134 const gdb_byte *info_ptr, int building_psymtab)
18135 {
18136 struct dwarf2_cu *cu = reader->cu;
18137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18138 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18139 unsigned int bytes_read;
18140 unsigned int load_all = 0;
18141 int nesting_level = 1;
18142
18143 parent_die = NULL;
18144 last_die = NULL;
18145
18146 gdb_assert (cu->per_cu != NULL);
18147 if (cu->per_cu->load_all_dies)
18148 load_all = 1;
18149
18150 cu->partial_dies
18151 = htab_create_alloc_ex (cu->header.length / 12,
18152 partial_die_hash,
18153 partial_die_eq,
18154 NULL,
18155 &cu->comp_unit_obstack,
18156 hashtab_obstack_allocate,
18157 dummy_obstack_deallocate);
18158
18159 while (1)
18160 {
18161 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18162
18163 /* A NULL abbrev means the end of a series of children. */
18164 if (abbrev == NULL)
18165 {
18166 if (--nesting_level == 0)
18167 return first_die;
18168
18169 info_ptr += bytes_read;
18170 last_die = parent_die;
18171 parent_die = parent_die->die_parent;
18172 continue;
18173 }
18174
18175 /* Check for template arguments. We never save these; if
18176 they're seen, we just mark the parent, and go on our way. */
18177 if (parent_die != NULL
18178 && cu->language == language_cplus
18179 && (abbrev->tag == DW_TAG_template_type_param
18180 || abbrev->tag == DW_TAG_template_value_param))
18181 {
18182 parent_die->has_template_arguments = 1;
18183
18184 if (!load_all)
18185 {
18186 /* We don't need a partial DIE for the template argument. */
18187 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18188 continue;
18189 }
18190 }
18191
18192 /* We only recurse into c++ subprograms looking for template arguments.
18193 Skip their other children. */
18194 if (!load_all
18195 && cu->language == language_cplus
18196 && parent_die != NULL
18197 && parent_die->tag == DW_TAG_subprogram)
18198 {
18199 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18200 continue;
18201 }
18202
18203 /* Check whether this DIE is interesting enough to save. Normally
18204 we would not be interested in members here, but there may be
18205 later variables referencing them via DW_AT_specification (for
18206 static members). */
18207 if (!load_all
18208 && !is_type_tag_for_partial (abbrev->tag)
18209 && abbrev->tag != DW_TAG_constant
18210 && abbrev->tag != DW_TAG_enumerator
18211 && abbrev->tag != DW_TAG_subprogram
18212 && abbrev->tag != DW_TAG_inlined_subroutine
18213 && abbrev->tag != DW_TAG_lexical_block
18214 && abbrev->tag != DW_TAG_variable
18215 && abbrev->tag != DW_TAG_namespace
18216 && abbrev->tag != DW_TAG_module
18217 && abbrev->tag != DW_TAG_member
18218 && abbrev->tag != DW_TAG_imported_unit
18219 && abbrev->tag != DW_TAG_imported_declaration)
18220 {
18221 /* Otherwise we skip to the next sibling, if any. */
18222 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18223 continue;
18224 }
18225
18226 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18227 abbrev);
18228
18229 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18230
18231 /* This two-pass algorithm for processing partial symbols has a
18232 high cost in cache pressure. Thus, handle some simple cases
18233 here which cover the majority of C partial symbols. DIEs
18234 which neither have specification tags in them, nor could have
18235 specification tags elsewhere pointing at them, can simply be
18236 processed and discarded.
18237
18238 This segment is also optional; scan_partial_symbols and
18239 add_partial_symbol will handle these DIEs if we chain
18240 them in normally. When compilers which do not emit large
18241 quantities of duplicate debug information are more common,
18242 this code can probably be removed. */
18243
18244 /* Any complete simple types at the top level (pretty much all
18245 of them, for a language without namespaces), can be processed
18246 directly. */
18247 if (parent_die == NULL
18248 && pdi.has_specification == 0
18249 && pdi.is_declaration == 0
18250 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18251 || pdi.tag == DW_TAG_base_type
18252 || pdi.tag == DW_TAG_subrange_type))
18253 {
18254 if (building_psymtab && pdi.name != NULL)
18255 add_psymbol_to_list (pdi.name, false,
18256 VAR_DOMAIN, LOC_TYPEDEF, -1,
18257 psymbol_placement::STATIC,
18258 0, cu->language, objfile);
18259 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18260 continue;
18261 }
18262
18263 /* The exception for DW_TAG_typedef with has_children above is
18264 a workaround of GCC PR debug/47510. In the case of this complaint
18265 type_name_or_error will error on such types later.
18266
18267 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18268 it could not find the child DIEs referenced later, this is checked
18269 above. In correct DWARF DW_TAG_typedef should have no children. */
18270
18271 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18272 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18273 "- DIE at %s [in module %s]"),
18274 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18275
18276 /* If we're at the second level, and we're an enumerator, and
18277 our parent has no specification (meaning possibly lives in a
18278 namespace elsewhere), then we can add the partial symbol now
18279 instead of queueing it. */
18280 if (pdi.tag == DW_TAG_enumerator
18281 && parent_die != NULL
18282 && parent_die->die_parent == NULL
18283 && parent_die->tag == DW_TAG_enumeration_type
18284 && parent_die->has_specification == 0)
18285 {
18286 if (pdi.name == NULL)
18287 complaint (_("malformed enumerator DIE ignored"));
18288 else if (building_psymtab)
18289 add_psymbol_to_list (pdi.name, false,
18290 VAR_DOMAIN, LOC_CONST, -1,
18291 cu->language == language_cplus
18292 ? psymbol_placement::GLOBAL
18293 : psymbol_placement::STATIC,
18294 0, cu->language, objfile);
18295
18296 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18297 continue;
18298 }
18299
18300 struct partial_die_info *part_die
18301 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18302
18303 /* We'll save this DIE so link it in. */
18304 part_die->die_parent = parent_die;
18305 part_die->die_sibling = NULL;
18306 part_die->die_child = NULL;
18307
18308 if (last_die && last_die == parent_die)
18309 last_die->die_child = part_die;
18310 else if (last_die)
18311 last_die->die_sibling = part_die;
18312
18313 last_die = part_die;
18314
18315 if (first_die == NULL)
18316 first_die = part_die;
18317
18318 /* Maybe add the DIE to the hash table. Not all DIEs that we
18319 find interesting need to be in the hash table, because we
18320 also have the parent/sibling/child chains; only those that we
18321 might refer to by offset later during partial symbol reading.
18322
18323 For now this means things that might have be the target of a
18324 DW_AT_specification, DW_AT_abstract_origin, or
18325 DW_AT_extension. DW_AT_extension will refer only to
18326 namespaces; DW_AT_abstract_origin refers to functions (and
18327 many things under the function DIE, but we do not recurse
18328 into function DIEs during partial symbol reading) and
18329 possibly variables as well; DW_AT_specification refers to
18330 declarations. Declarations ought to have the DW_AT_declaration
18331 flag. It happens that GCC forgets to put it in sometimes, but
18332 only for functions, not for types.
18333
18334 Adding more things than necessary to the hash table is harmless
18335 except for the performance cost. Adding too few will result in
18336 wasted time in find_partial_die, when we reread the compilation
18337 unit with load_all_dies set. */
18338
18339 if (load_all
18340 || abbrev->tag == DW_TAG_constant
18341 || abbrev->tag == DW_TAG_subprogram
18342 || abbrev->tag == DW_TAG_variable
18343 || abbrev->tag == DW_TAG_namespace
18344 || part_die->is_declaration)
18345 {
18346 void **slot;
18347
18348 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18349 to_underlying (part_die->sect_off),
18350 INSERT);
18351 *slot = part_die;
18352 }
18353
18354 /* For some DIEs we want to follow their children (if any). For C
18355 we have no reason to follow the children of structures; for other
18356 languages we have to, so that we can get at method physnames
18357 to infer fully qualified class names, for DW_AT_specification,
18358 and for C++ template arguments. For C++, we also look one level
18359 inside functions to find template arguments (if the name of the
18360 function does not already contain the template arguments).
18361
18362 For Ada and Fortran, we need to scan the children of subprograms
18363 and lexical blocks as well because these languages allow the
18364 definition of nested entities that could be interesting for the
18365 debugger, such as nested subprograms for instance. */
18366 if (last_die->has_children
18367 && (load_all
18368 || last_die->tag == DW_TAG_namespace
18369 || last_die->tag == DW_TAG_module
18370 || last_die->tag == DW_TAG_enumeration_type
18371 || (cu->language == language_cplus
18372 && last_die->tag == DW_TAG_subprogram
18373 && (last_die->name == NULL
18374 || strchr (last_die->name, '<') == NULL))
18375 || (cu->language != language_c
18376 && (last_die->tag == DW_TAG_class_type
18377 || last_die->tag == DW_TAG_interface_type
18378 || last_die->tag == DW_TAG_structure_type
18379 || last_die->tag == DW_TAG_union_type))
18380 || ((cu->language == language_ada
18381 || cu->language == language_fortran)
18382 && (last_die->tag == DW_TAG_subprogram
18383 || last_die->tag == DW_TAG_lexical_block))))
18384 {
18385 nesting_level++;
18386 parent_die = last_die;
18387 continue;
18388 }
18389
18390 /* Otherwise we skip to the next sibling, if any. */
18391 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18392
18393 /* Back to the top, do it again. */
18394 }
18395 }
18396
18397 partial_die_info::partial_die_info (sect_offset sect_off_,
18398 struct abbrev_info *abbrev)
18399 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18400 {
18401 }
18402
18403 /* Read a minimal amount of information into the minimal die structure.
18404 INFO_PTR should point just after the initial uleb128 of a DIE. */
18405
18406 const gdb_byte *
18407 partial_die_info::read (const struct die_reader_specs *reader,
18408 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18409 {
18410 struct dwarf2_cu *cu = reader->cu;
18411 struct dwarf2_per_objfile *dwarf2_per_objfile
18412 = cu->per_cu->dwarf2_per_objfile;
18413 unsigned int i;
18414 int has_low_pc_attr = 0;
18415 int has_high_pc_attr = 0;
18416 int high_pc_relative = 0;
18417
18418 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18419 for (i = 0; i < abbrev.num_attrs; ++i)
18420 {
18421 bool need_reprocess;
18422 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18423 info_ptr, &need_reprocess);
18424 /* String and address offsets that need to do the reprocessing have
18425 already been read at this point, so there is no need to wait until
18426 the loop terminates to do the reprocessing. */
18427 if (need_reprocess)
18428 read_attribute_reprocess (reader, &attr_vec[i]);
18429 attribute &attr = attr_vec[i];
18430 /* Store the data if it is of an attribute we want to keep in a
18431 partial symbol table. */
18432 switch (attr.name)
18433 {
18434 case DW_AT_name:
18435 switch (tag)
18436 {
18437 case DW_TAG_compile_unit:
18438 case DW_TAG_partial_unit:
18439 case DW_TAG_type_unit:
18440 /* Compilation units have a DW_AT_name that is a filename, not
18441 a source language identifier. */
18442 case DW_TAG_enumeration_type:
18443 case DW_TAG_enumerator:
18444 /* These tags always have simple identifiers already; no need
18445 to canonicalize them. */
18446 name = DW_STRING (&attr);
18447 break;
18448 default:
18449 {
18450 struct objfile *objfile = dwarf2_per_objfile->objfile;
18451
18452 name
18453 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18454 &objfile->per_bfd->storage_obstack);
18455 }
18456 break;
18457 }
18458 break;
18459 case DW_AT_linkage_name:
18460 case DW_AT_MIPS_linkage_name:
18461 /* Note that both forms of linkage name might appear. We
18462 assume they will be the same, and we only store the last
18463 one we see. */
18464 linkage_name = DW_STRING (&attr);
18465 break;
18466 case DW_AT_low_pc:
18467 has_low_pc_attr = 1;
18468 lowpc = attr.value_as_address ();
18469 break;
18470 case DW_AT_high_pc:
18471 has_high_pc_attr = 1;
18472 highpc = attr.value_as_address ();
18473 if (cu->header.version >= 4 && attr.form_is_constant ())
18474 high_pc_relative = 1;
18475 break;
18476 case DW_AT_location:
18477 /* Support the .debug_loc offsets. */
18478 if (attr.form_is_block ())
18479 {
18480 d.locdesc = DW_BLOCK (&attr);
18481 }
18482 else if (attr.form_is_section_offset ())
18483 {
18484 dwarf2_complex_location_expr_complaint ();
18485 }
18486 else
18487 {
18488 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18489 "partial symbol information");
18490 }
18491 break;
18492 case DW_AT_external:
18493 is_external = DW_UNSND (&attr);
18494 break;
18495 case DW_AT_declaration:
18496 is_declaration = DW_UNSND (&attr);
18497 break;
18498 case DW_AT_type:
18499 has_type = 1;
18500 break;
18501 case DW_AT_abstract_origin:
18502 case DW_AT_specification:
18503 case DW_AT_extension:
18504 has_specification = 1;
18505 spec_offset = dwarf2_get_ref_die_offset (&attr);
18506 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18507 || cu->per_cu->is_dwz);
18508 break;
18509 case DW_AT_sibling:
18510 /* Ignore absolute siblings, they might point outside of
18511 the current compile unit. */
18512 if (attr.form == DW_FORM_ref_addr)
18513 complaint (_("ignoring absolute DW_AT_sibling"));
18514 else
18515 {
18516 const gdb_byte *buffer = reader->buffer;
18517 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18518 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18519
18520 if (sibling_ptr < info_ptr)
18521 complaint (_("DW_AT_sibling points backwards"));
18522 else if (sibling_ptr > reader->buffer_end)
18523 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18524 else
18525 sibling = sibling_ptr;
18526 }
18527 break;
18528 case DW_AT_byte_size:
18529 has_byte_size = 1;
18530 break;
18531 case DW_AT_const_value:
18532 has_const_value = 1;
18533 break;
18534 case DW_AT_calling_convention:
18535 /* DWARF doesn't provide a way to identify a program's source-level
18536 entry point. DW_AT_calling_convention attributes are only meant
18537 to describe functions' calling conventions.
18538
18539 However, because it's a necessary piece of information in
18540 Fortran, and before DWARF 4 DW_CC_program was the only
18541 piece of debugging information whose definition refers to
18542 a 'main program' at all, several compilers marked Fortran
18543 main programs with DW_CC_program --- even when those
18544 functions use the standard calling conventions.
18545
18546 Although DWARF now specifies a way to provide this
18547 information, we support this practice for backward
18548 compatibility. */
18549 if (DW_UNSND (&attr) == DW_CC_program
18550 && cu->language == language_fortran)
18551 main_subprogram = 1;
18552 break;
18553 case DW_AT_inline:
18554 if (DW_UNSND (&attr) == DW_INL_inlined
18555 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18556 may_be_inlined = 1;
18557 break;
18558
18559 case DW_AT_import:
18560 if (tag == DW_TAG_imported_unit)
18561 {
18562 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18563 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18564 || cu->per_cu->is_dwz);
18565 }
18566 break;
18567
18568 case DW_AT_main_subprogram:
18569 main_subprogram = DW_UNSND (&attr);
18570 break;
18571
18572 case DW_AT_ranges:
18573 {
18574 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18575 but that requires a full DIE, so instead we just
18576 reimplement it. */
18577 int need_ranges_base = tag != DW_TAG_compile_unit;
18578 unsigned int ranges_offset = (DW_UNSND (&attr)
18579 + (need_ranges_base
18580 ? cu->ranges_base
18581 : 0));
18582
18583 /* Value of the DW_AT_ranges attribute is the offset in the
18584 .debug_ranges section. */
18585 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18586 nullptr))
18587 has_pc_info = 1;
18588 }
18589 break;
18590
18591 default:
18592 break;
18593 }
18594 }
18595
18596 /* For Ada, if both the name and the linkage name appear, we prefer
18597 the latter. This lets "catch exception" work better, regardless
18598 of the order in which the name and linkage name were emitted.
18599 Really, though, this is just a workaround for the fact that gdb
18600 doesn't store both the name and the linkage name. */
18601 if (cu->language == language_ada && linkage_name != nullptr)
18602 name = linkage_name;
18603
18604 if (high_pc_relative)
18605 highpc += lowpc;
18606
18607 if (has_low_pc_attr && has_high_pc_attr)
18608 {
18609 /* When using the GNU linker, .gnu.linkonce. sections are used to
18610 eliminate duplicate copies of functions and vtables and such.
18611 The linker will arbitrarily choose one and discard the others.
18612 The AT_*_pc values for such functions refer to local labels in
18613 these sections. If the section from that file was discarded, the
18614 labels are not in the output, so the relocs get a value of 0.
18615 If this is a discarded function, mark the pc bounds as invalid,
18616 so that GDB will ignore it. */
18617 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18618 {
18619 struct objfile *objfile = dwarf2_per_objfile->objfile;
18620 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18621
18622 complaint (_("DW_AT_low_pc %s is zero "
18623 "for DIE at %s [in module %s]"),
18624 paddress (gdbarch, lowpc),
18625 sect_offset_str (sect_off),
18626 objfile_name (objfile));
18627 }
18628 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18629 else if (lowpc >= highpc)
18630 {
18631 struct objfile *objfile = dwarf2_per_objfile->objfile;
18632 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18633
18634 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18635 "for DIE at %s [in module %s]"),
18636 paddress (gdbarch, lowpc),
18637 paddress (gdbarch, highpc),
18638 sect_offset_str (sect_off),
18639 objfile_name (objfile));
18640 }
18641 else
18642 has_pc_info = 1;
18643 }
18644
18645 return info_ptr;
18646 }
18647
18648 /* Find a cached partial DIE at OFFSET in CU. */
18649
18650 struct partial_die_info *
18651 dwarf2_cu::find_partial_die (sect_offset sect_off)
18652 {
18653 struct partial_die_info *lookup_die = NULL;
18654 struct partial_die_info part_die (sect_off);
18655
18656 lookup_die = ((struct partial_die_info *)
18657 htab_find_with_hash (partial_dies, &part_die,
18658 to_underlying (sect_off)));
18659
18660 return lookup_die;
18661 }
18662
18663 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18664 except in the case of .debug_types DIEs which do not reference
18665 outside their CU (they do however referencing other types via
18666 DW_FORM_ref_sig8). */
18667
18668 static const struct cu_partial_die_info
18669 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18670 {
18671 struct dwarf2_per_objfile *dwarf2_per_objfile
18672 = cu->per_cu->dwarf2_per_objfile;
18673 struct objfile *objfile = dwarf2_per_objfile->objfile;
18674 struct dwarf2_per_cu_data *per_cu = NULL;
18675 struct partial_die_info *pd = NULL;
18676
18677 if (offset_in_dwz == cu->per_cu->is_dwz
18678 && offset_in_cu_p (&cu->header, sect_off))
18679 {
18680 pd = cu->find_partial_die (sect_off);
18681 if (pd != NULL)
18682 return { cu, pd };
18683 /* We missed recording what we needed.
18684 Load all dies and try again. */
18685 per_cu = cu->per_cu;
18686 }
18687 else
18688 {
18689 /* TUs don't reference other CUs/TUs (except via type signatures). */
18690 if (cu->per_cu->is_debug_types)
18691 {
18692 error (_("Dwarf Error: Type Unit at offset %s contains"
18693 " external reference to offset %s [in module %s].\n"),
18694 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18695 bfd_get_filename (objfile->obfd));
18696 }
18697 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18698 dwarf2_per_objfile);
18699
18700 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18701 load_partial_comp_unit (per_cu);
18702
18703 per_cu->cu->last_used = 0;
18704 pd = per_cu->cu->find_partial_die (sect_off);
18705 }
18706
18707 /* If we didn't find it, and not all dies have been loaded,
18708 load them all and try again. */
18709
18710 if (pd == NULL && per_cu->load_all_dies == 0)
18711 {
18712 per_cu->load_all_dies = 1;
18713
18714 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18715 THIS_CU->cu may already be in use. So we can't just free it and
18716 replace its DIEs with the ones we read in. Instead, we leave those
18717 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18718 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18719 set. */
18720 load_partial_comp_unit (per_cu);
18721
18722 pd = per_cu->cu->find_partial_die (sect_off);
18723 }
18724
18725 if (pd == NULL)
18726 internal_error (__FILE__, __LINE__,
18727 _("could not find partial DIE %s "
18728 "in cache [from module %s]\n"),
18729 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18730 return { per_cu->cu, pd };
18731 }
18732
18733 /* See if we can figure out if the class lives in a namespace. We do
18734 this by looking for a member function; its demangled name will
18735 contain namespace info, if there is any. */
18736
18737 static void
18738 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18739 struct dwarf2_cu *cu)
18740 {
18741 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18742 what template types look like, because the demangler
18743 frequently doesn't give the same name as the debug info. We
18744 could fix this by only using the demangled name to get the
18745 prefix (but see comment in read_structure_type). */
18746
18747 struct partial_die_info *real_pdi;
18748 struct partial_die_info *child_pdi;
18749
18750 /* If this DIE (this DIE's specification, if any) has a parent, then
18751 we should not do this. We'll prepend the parent's fully qualified
18752 name when we create the partial symbol. */
18753
18754 real_pdi = struct_pdi;
18755 while (real_pdi->has_specification)
18756 {
18757 auto res = find_partial_die (real_pdi->spec_offset,
18758 real_pdi->spec_is_dwz, cu);
18759 real_pdi = res.pdi;
18760 cu = res.cu;
18761 }
18762
18763 if (real_pdi->die_parent != NULL)
18764 return;
18765
18766 for (child_pdi = struct_pdi->die_child;
18767 child_pdi != NULL;
18768 child_pdi = child_pdi->die_sibling)
18769 {
18770 if (child_pdi->tag == DW_TAG_subprogram
18771 && child_pdi->linkage_name != NULL)
18772 {
18773 gdb::unique_xmalloc_ptr<char> actual_class_name
18774 (language_class_name_from_physname (cu->language_defn,
18775 child_pdi->linkage_name));
18776 if (actual_class_name != NULL)
18777 {
18778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18779 struct_pdi->name
18780 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18781 actual_class_name.get ());
18782 }
18783 break;
18784 }
18785 }
18786 }
18787
18788 void
18789 partial_die_info::fixup (struct dwarf2_cu *cu)
18790 {
18791 /* Once we've fixed up a die, there's no point in doing so again.
18792 This also avoids a memory leak if we were to call
18793 guess_partial_die_structure_name multiple times. */
18794 if (fixup_called)
18795 return;
18796
18797 /* If we found a reference attribute and the DIE has no name, try
18798 to find a name in the referred to DIE. */
18799
18800 if (name == NULL && has_specification)
18801 {
18802 struct partial_die_info *spec_die;
18803
18804 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18805 spec_die = res.pdi;
18806 cu = res.cu;
18807
18808 spec_die->fixup (cu);
18809
18810 if (spec_die->name)
18811 {
18812 name = spec_die->name;
18813
18814 /* Copy DW_AT_external attribute if it is set. */
18815 if (spec_die->is_external)
18816 is_external = spec_die->is_external;
18817 }
18818 }
18819
18820 /* Set default names for some unnamed DIEs. */
18821
18822 if (name == NULL && tag == DW_TAG_namespace)
18823 name = CP_ANONYMOUS_NAMESPACE_STR;
18824
18825 /* If there is no parent die to provide a namespace, and there are
18826 children, see if we can determine the namespace from their linkage
18827 name. */
18828 if (cu->language == language_cplus
18829 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18830 && die_parent == NULL
18831 && has_children
18832 && (tag == DW_TAG_class_type
18833 || tag == DW_TAG_structure_type
18834 || tag == DW_TAG_union_type))
18835 guess_partial_die_structure_name (this, cu);
18836
18837 /* GCC might emit a nameless struct or union that has a linkage
18838 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18839 if (name == NULL
18840 && (tag == DW_TAG_class_type
18841 || tag == DW_TAG_interface_type
18842 || tag == DW_TAG_structure_type
18843 || tag == DW_TAG_union_type)
18844 && linkage_name != NULL)
18845 {
18846 gdb::unique_xmalloc_ptr<char> demangled
18847 (gdb_demangle (linkage_name, DMGL_TYPES));
18848 if (demangled != nullptr)
18849 {
18850 const char *base;
18851
18852 /* Strip any leading namespaces/classes, keep only the base name.
18853 DW_AT_name for named DIEs does not contain the prefixes. */
18854 base = strrchr (demangled.get (), ':');
18855 if (base && base > demangled.get () && base[-1] == ':')
18856 base++;
18857 else
18858 base = demangled.get ();
18859
18860 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18861 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18862 }
18863 }
18864
18865 fixup_called = 1;
18866 }
18867
18868 /* Process the attributes that had to be skipped in the first round. These
18869 attributes are the ones that need str_offsets_base or addr_base attributes.
18870 They could not have been processed in the first round, because at the time
18871 the values of str_offsets_base or addr_base may not have been known. */
18872 void read_attribute_reprocess (const struct die_reader_specs *reader,
18873 struct attribute *attr)
18874 {
18875 struct dwarf2_cu *cu = reader->cu;
18876 switch (attr->form)
18877 {
18878 case DW_FORM_addrx:
18879 case DW_FORM_GNU_addr_index:
18880 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18881 break;
18882 case DW_FORM_strx:
18883 case DW_FORM_strx1:
18884 case DW_FORM_strx2:
18885 case DW_FORM_strx3:
18886 case DW_FORM_strx4:
18887 case DW_FORM_GNU_str_index:
18888 {
18889 unsigned int str_index = DW_UNSND (attr);
18890 if (reader->dwo_file != NULL)
18891 {
18892 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18893 DW_STRING_IS_CANONICAL (attr) = 0;
18894 }
18895 else
18896 {
18897 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18898 DW_STRING_IS_CANONICAL (attr) = 0;
18899 }
18900 break;
18901 }
18902 default:
18903 gdb_assert_not_reached (_("Unexpected DWARF form."));
18904 }
18905 }
18906
18907 /* Read an attribute value described by an attribute form. */
18908
18909 static const gdb_byte *
18910 read_attribute_value (const struct die_reader_specs *reader,
18911 struct attribute *attr, unsigned form,
18912 LONGEST implicit_const, const gdb_byte *info_ptr,
18913 bool *need_reprocess)
18914 {
18915 struct dwarf2_cu *cu = reader->cu;
18916 struct dwarf2_per_objfile *dwarf2_per_objfile
18917 = cu->per_cu->dwarf2_per_objfile;
18918 struct objfile *objfile = dwarf2_per_objfile->objfile;
18919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18920 bfd *abfd = reader->abfd;
18921 struct comp_unit_head *cu_header = &cu->header;
18922 unsigned int bytes_read;
18923 struct dwarf_block *blk;
18924 *need_reprocess = false;
18925
18926 attr->form = (enum dwarf_form) form;
18927 switch (form)
18928 {
18929 case DW_FORM_ref_addr:
18930 if (cu->header.version == 2)
18931 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18932 else
18933 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18934 &cu->header, &bytes_read);
18935 info_ptr += bytes_read;
18936 break;
18937 case DW_FORM_GNU_ref_alt:
18938 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18939 info_ptr += bytes_read;
18940 break;
18941 case DW_FORM_addr:
18942 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18943 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18944 info_ptr += bytes_read;
18945 break;
18946 case DW_FORM_block2:
18947 blk = dwarf_alloc_block (cu);
18948 blk->size = read_2_bytes (abfd, info_ptr);
18949 info_ptr += 2;
18950 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18951 info_ptr += blk->size;
18952 DW_BLOCK (attr) = blk;
18953 break;
18954 case DW_FORM_block4:
18955 blk = dwarf_alloc_block (cu);
18956 blk->size = read_4_bytes (abfd, info_ptr);
18957 info_ptr += 4;
18958 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18959 info_ptr += blk->size;
18960 DW_BLOCK (attr) = blk;
18961 break;
18962 case DW_FORM_data2:
18963 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18964 info_ptr += 2;
18965 break;
18966 case DW_FORM_data4:
18967 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18968 info_ptr += 4;
18969 break;
18970 case DW_FORM_data8:
18971 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18972 info_ptr += 8;
18973 break;
18974 case DW_FORM_data16:
18975 blk = dwarf_alloc_block (cu);
18976 blk->size = 16;
18977 blk->data = read_n_bytes (abfd, info_ptr, 16);
18978 info_ptr += 16;
18979 DW_BLOCK (attr) = blk;
18980 break;
18981 case DW_FORM_sec_offset:
18982 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18983 info_ptr += bytes_read;
18984 break;
18985 case DW_FORM_string:
18986 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18987 DW_STRING_IS_CANONICAL (attr) = 0;
18988 info_ptr += bytes_read;
18989 break;
18990 case DW_FORM_strp:
18991 if (!cu->per_cu->is_dwz)
18992 {
18993 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18994 abfd, info_ptr, cu_header,
18995 &bytes_read);
18996 DW_STRING_IS_CANONICAL (attr) = 0;
18997 info_ptr += bytes_read;
18998 break;
18999 }
19000 /* FALLTHROUGH */
19001 case DW_FORM_line_strp:
19002 if (!cu->per_cu->is_dwz)
19003 {
19004 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19005 abfd, info_ptr,
19006 cu_header, &bytes_read);
19007 DW_STRING_IS_CANONICAL (attr) = 0;
19008 info_ptr += bytes_read;
19009 break;
19010 }
19011 /* FALLTHROUGH */
19012 case DW_FORM_GNU_strp_alt:
19013 {
19014 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19015 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19016 &bytes_read);
19017
19018 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19019 dwz, str_offset);
19020 DW_STRING_IS_CANONICAL (attr) = 0;
19021 info_ptr += bytes_read;
19022 }
19023 break;
19024 case DW_FORM_exprloc:
19025 case DW_FORM_block:
19026 blk = dwarf_alloc_block (cu);
19027 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19028 info_ptr += bytes_read;
19029 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19030 info_ptr += blk->size;
19031 DW_BLOCK (attr) = blk;
19032 break;
19033 case DW_FORM_block1:
19034 blk = dwarf_alloc_block (cu);
19035 blk->size = read_1_byte (abfd, info_ptr);
19036 info_ptr += 1;
19037 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19038 info_ptr += blk->size;
19039 DW_BLOCK (attr) = blk;
19040 break;
19041 case DW_FORM_data1:
19042 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19043 info_ptr += 1;
19044 break;
19045 case DW_FORM_flag:
19046 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19047 info_ptr += 1;
19048 break;
19049 case DW_FORM_flag_present:
19050 DW_UNSND (attr) = 1;
19051 break;
19052 case DW_FORM_sdata:
19053 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19054 info_ptr += bytes_read;
19055 break;
19056 case DW_FORM_udata:
19057 case DW_FORM_rnglistx:
19058 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19059 info_ptr += bytes_read;
19060 break;
19061 case DW_FORM_ref1:
19062 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19063 + read_1_byte (abfd, info_ptr));
19064 info_ptr += 1;
19065 break;
19066 case DW_FORM_ref2:
19067 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19068 + read_2_bytes (abfd, info_ptr));
19069 info_ptr += 2;
19070 break;
19071 case DW_FORM_ref4:
19072 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19073 + read_4_bytes (abfd, info_ptr));
19074 info_ptr += 4;
19075 break;
19076 case DW_FORM_ref8:
19077 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19078 + read_8_bytes (abfd, info_ptr));
19079 info_ptr += 8;
19080 break;
19081 case DW_FORM_ref_sig8:
19082 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19083 info_ptr += 8;
19084 break;
19085 case DW_FORM_ref_udata:
19086 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19087 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19088 info_ptr += bytes_read;
19089 break;
19090 case DW_FORM_indirect:
19091 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 if (form == DW_FORM_implicit_const)
19094 {
19095 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19096 info_ptr += bytes_read;
19097 }
19098 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19099 info_ptr, need_reprocess);
19100 break;
19101 case DW_FORM_implicit_const:
19102 DW_SND (attr) = implicit_const;
19103 break;
19104 case DW_FORM_addrx:
19105 case DW_FORM_GNU_addr_index:
19106 *need_reprocess = true;
19107 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19108 info_ptr += bytes_read;
19109 break;
19110 case DW_FORM_strx:
19111 case DW_FORM_strx1:
19112 case DW_FORM_strx2:
19113 case DW_FORM_strx3:
19114 case DW_FORM_strx4:
19115 case DW_FORM_GNU_str_index:
19116 {
19117 ULONGEST str_index;
19118 if (form == DW_FORM_strx1)
19119 {
19120 str_index = read_1_byte (abfd, info_ptr);
19121 info_ptr += 1;
19122 }
19123 else if (form == DW_FORM_strx2)
19124 {
19125 str_index = read_2_bytes (abfd, info_ptr);
19126 info_ptr += 2;
19127 }
19128 else if (form == DW_FORM_strx3)
19129 {
19130 str_index = read_3_bytes (abfd, info_ptr);
19131 info_ptr += 3;
19132 }
19133 else if (form == DW_FORM_strx4)
19134 {
19135 str_index = read_4_bytes (abfd, info_ptr);
19136 info_ptr += 4;
19137 }
19138 else
19139 {
19140 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19141 info_ptr += bytes_read;
19142 }
19143 *need_reprocess = true;
19144 DW_UNSND (attr) = str_index;
19145 }
19146 break;
19147 default:
19148 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19149 dwarf_form_name (form),
19150 bfd_get_filename (abfd));
19151 }
19152
19153 /* Super hack. */
19154 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19155 attr->form = DW_FORM_GNU_ref_alt;
19156
19157 /* We have seen instances where the compiler tried to emit a byte
19158 size attribute of -1 which ended up being encoded as an unsigned
19159 0xffffffff. Although 0xffffffff is technically a valid size value,
19160 an object of this size seems pretty unlikely so we can relatively
19161 safely treat these cases as if the size attribute was invalid and
19162 treat them as zero by default. */
19163 if (attr->name == DW_AT_byte_size
19164 && form == DW_FORM_data4
19165 && DW_UNSND (attr) >= 0xffffffff)
19166 {
19167 complaint
19168 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19169 hex_string (DW_UNSND (attr)));
19170 DW_UNSND (attr) = 0;
19171 }
19172
19173 return info_ptr;
19174 }
19175
19176 /* Read an attribute described by an abbreviated attribute. */
19177
19178 static const gdb_byte *
19179 read_attribute (const struct die_reader_specs *reader,
19180 struct attribute *attr, struct attr_abbrev *abbrev,
19181 const gdb_byte *info_ptr, bool *need_reprocess)
19182 {
19183 attr->name = abbrev->name;
19184 return read_attribute_value (reader, attr, abbrev->form,
19185 abbrev->implicit_const, info_ptr,
19186 need_reprocess);
19187 }
19188
19189 static CORE_ADDR
19190 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19191 unsigned int *bytes_read)
19192 {
19193 struct comp_unit_head *cu_header = &cu->header;
19194 CORE_ADDR retval = 0;
19195
19196 if (cu_header->signed_addr_p)
19197 {
19198 switch (cu_header->addr_size)
19199 {
19200 case 2:
19201 retval = bfd_get_signed_16 (abfd, buf);
19202 break;
19203 case 4:
19204 retval = bfd_get_signed_32 (abfd, buf);
19205 break;
19206 case 8:
19207 retval = bfd_get_signed_64 (abfd, buf);
19208 break;
19209 default:
19210 internal_error (__FILE__, __LINE__,
19211 _("read_address: bad switch, signed [in module %s]"),
19212 bfd_get_filename (abfd));
19213 }
19214 }
19215 else
19216 {
19217 switch (cu_header->addr_size)
19218 {
19219 case 2:
19220 retval = bfd_get_16 (abfd, buf);
19221 break;
19222 case 4:
19223 retval = bfd_get_32 (abfd, buf);
19224 break;
19225 case 8:
19226 retval = bfd_get_64 (abfd, buf);
19227 break;
19228 default:
19229 internal_error (__FILE__, __LINE__,
19230 _("read_address: bad switch, "
19231 "unsigned [in module %s]"),
19232 bfd_get_filename (abfd));
19233 }
19234 }
19235
19236 *bytes_read = cu_header->addr_size;
19237 return retval;
19238 }
19239
19240 /* Read the initial length from a section. The (draft) DWARF 3
19241 specification allows the initial length to take up either 4 bytes
19242 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19243 bytes describe the length and all offsets will be 8 bytes in length
19244 instead of 4.
19245
19246 An older, non-standard 64-bit format is also handled by this
19247 function. The older format in question stores the initial length
19248 as an 8-byte quantity without an escape value. Lengths greater
19249 than 2^32 aren't very common which means that the initial 4 bytes
19250 is almost always zero. Since a length value of zero doesn't make
19251 sense for the 32-bit format, this initial zero can be considered to
19252 be an escape value which indicates the presence of the older 64-bit
19253 format. As written, the code can't detect (old format) lengths
19254 greater than 4GB. If it becomes necessary to handle lengths
19255 somewhat larger than 4GB, we could allow other small values (such
19256 as the non-sensical values of 1, 2, and 3) to also be used as
19257 escape values indicating the presence of the old format.
19258
19259 The value returned via bytes_read should be used to increment the
19260 relevant pointer after calling read_initial_length().
19261
19262 [ Note: read_initial_length() and read_offset() are based on the
19263 document entitled "DWARF Debugging Information Format", revision
19264 3, draft 8, dated November 19, 2001. This document was obtained
19265 from:
19266
19267 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19268
19269 This document is only a draft and is subject to change. (So beware.)
19270
19271 Details regarding the older, non-standard 64-bit format were
19272 determined empirically by examining 64-bit ELF files produced by
19273 the SGI toolchain on an IRIX 6.5 machine.
19274
19275 - Kevin, July 16, 2002
19276 ] */
19277
19278 static LONGEST
19279 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19280 {
19281 LONGEST length = bfd_get_32 (abfd, buf);
19282
19283 if (length == 0xffffffff)
19284 {
19285 length = bfd_get_64 (abfd, buf + 4);
19286 *bytes_read = 12;
19287 }
19288 else if (length == 0)
19289 {
19290 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19291 length = bfd_get_64 (abfd, buf);
19292 *bytes_read = 8;
19293 }
19294 else
19295 {
19296 *bytes_read = 4;
19297 }
19298
19299 return length;
19300 }
19301
19302 /* Cover function for read_initial_length.
19303 Returns the length of the object at BUF, and stores the size of the
19304 initial length in *BYTES_READ and stores the size that offsets will be in
19305 *OFFSET_SIZE.
19306 If the initial length size is not equivalent to that specified in
19307 CU_HEADER then issue a complaint.
19308 This is useful when reading non-comp-unit headers. */
19309
19310 static LONGEST
19311 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19312 const struct comp_unit_head *cu_header,
19313 unsigned int *bytes_read,
19314 unsigned int *offset_size)
19315 {
19316 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19317
19318 gdb_assert (cu_header->initial_length_size == 4
19319 || cu_header->initial_length_size == 8
19320 || cu_header->initial_length_size == 12);
19321
19322 if (cu_header->initial_length_size != *bytes_read)
19323 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19324
19325 *offset_size = (*bytes_read == 4) ? 4 : 8;
19326 return length;
19327 }
19328
19329 /* Read an offset from the data stream. The size of the offset is
19330 given by cu_header->offset_size. */
19331
19332 static LONGEST
19333 read_offset (bfd *abfd, const gdb_byte *buf,
19334 const struct comp_unit_head *cu_header,
19335 unsigned int *bytes_read)
19336 {
19337 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19338
19339 *bytes_read = cu_header->offset_size;
19340 return offset;
19341 }
19342
19343 /* Read an offset from the data stream. */
19344
19345 static LONGEST
19346 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19347 {
19348 LONGEST retval = 0;
19349
19350 switch (offset_size)
19351 {
19352 case 4:
19353 retval = bfd_get_32 (abfd, buf);
19354 break;
19355 case 8:
19356 retval = bfd_get_64 (abfd, buf);
19357 break;
19358 default:
19359 internal_error (__FILE__, __LINE__,
19360 _("read_offset_1: bad switch [in module %s]"),
19361 bfd_get_filename (abfd));
19362 }
19363
19364 return retval;
19365 }
19366
19367 static const gdb_byte *
19368 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19369 {
19370 /* If the size of a host char is 8 bits, we can return a pointer
19371 to the buffer, otherwise we have to copy the data to a buffer
19372 allocated on the temporary obstack. */
19373 gdb_assert (HOST_CHAR_BIT == 8);
19374 return buf;
19375 }
19376
19377 static const char *
19378 read_direct_string (bfd *abfd, const gdb_byte *buf,
19379 unsigned int *bytes_read_ptr)
19380 {
19381 /* If the size of a host char is 8 bits, we can return a pointer
19382 to the string, otherwise we have to copy the string to a buffer
19383 allocated on the temporary obstack. */
19384 gdb_assert (HOST_CHAR_BIT == 8);
19385 if (*buf == '\0')
19386 {
19387 *bytes_read_ptr = 1;
19388 return NULL;
19389 }
19390 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19391 return (const char *) buf;
19392 }
19393
19394 /* Return pointer to string at section SECT offset STR_OFFSET with error
19395 reporting strings FORM_NAME and SECT_NAME. */
19396
19397 static const char *
19398 read_indirect_string_at_offset_from (struct objfile *objfile,
19399 bfd *abfd, LONGEST str_offset,
19400 struct dwarf2_section_info *sect,
19401 const char *form_name,
19402 const char *sect_name)
19403 {
19404 sect->read (objfile);
19405 if (sect->buffer == NULL)
19406 error (_("%s used without %s section [in module %s]"),
19407 form_name, sect_name, bfd_get_filename (abfd));
19408 if (str_offset >= sect->size)
19409 error (_("%s pointing outside of %s section [in module %s]"),
19410 form_name, sect_name, bfd_get_filename (abfd));
19411 gdb_assert (HOST_CHAR_BIT == 8);
19412 if (sect->buffer[str_offset] == '\0')
19413 return NULL;
19414 return (const char *) (sect->buffer + str_offset);
19415 }
19416
19417 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19418
19419 static const char *
19420 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19421 bfd *abfd, LONGEST str_offset)
19422 {
19423 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19424 abfd, str_offset,
19425 &dwarf2_per_objfile->str,
19426 "DW_FORM_strp", ".debug_str");
19427 }
19428
19429 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19430
19431 static const char *
19432 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19433 bfd *abfd, LONGEST str_offset)
19434 {
19435 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19436 abfd, str_offset,
19437 &dwarf2_per_objfile->line_str,
19438 "DW_FORM_line_strp",
19439 ".debug_line_str");
19440 }
19441
19442 /* Read a string at offset STR_OFFSET in the .debug_str section from
19443 the .dwz file DWZ. Throw an error if the offset is too large. If
19444 the string consists of a single NUL byte, return NULL; otherwise
19445 return a pointer to the string. */
19446
19447 static const char *
19448 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19449 LONGEST str_offset)
19450 {
19451 dwz->str.read (objfile);
19452
19453 if (dwz->str.buffer == NULL)
19454 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19455 "section [in module %s]"),
19456 bfd_get_filename (dwz->dwz_bfd.get ()));
19457 if (str_offset >= dwz->str.size)
19458 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19459 ".debug_str section [in module %s]"),
19460 bfd_get_filename (dwz->dwz_bfd.get ()));
19461 gdb_assert (HOST_CHAR_BIT == 8);
19462 if (dwz->str.buffer[str_offset] == '\0')
19463 return NULL;
19464 return (const char *) (dwz->str.buffer + str_offset);
19465 }
19466
19467 /* Return pointer to string at .debug_str offset as read from BUF.
19468 BUF is assumed to be in a compilation unit described by CU_HEADER.
19469 Return *BYTES_READ_PTR count of bytes read from BUF. */
19470
19471 static const char *
19472 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19473 const gdb_byte *buf,
19474 const struct comp_unit_head *cu_header,
19475 unsigned int *bytes_read_ptr)
19476 {
19477 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19478
19479 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19480 }
19481
19482 /* Return pointer to string at .debug_line_str offset as read from BUF.
19483 BUF is assumed to be in a compilation unit described by CU_HEADER.
19484 Return *BYTES_READ_PTR count of bytes read from BUF. */
19485
19486 static const char *
19487 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19488 bfd *abfd, const gdb_byte *buf,
19489 const struct comp_unit_head *cu_header,
19490 unsigned int *bytes_read_ptr)
19491 {
19492 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19493
19494 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19495 str_offset);
19496 }
19497
19498 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19499 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19500 ADDR_SIZE is the size of addresses from the CU header. */
19501
19502 static CORE_ADDR
19503 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19504 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19505 int addr_size)
19506 {
19507 struct objfile *objfile = dwarf2_per_objfile->objfile;
19508 bfd *abfd = objfile->obfd;
19509 const gdb_byte *info_ptr;
19510 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19511
19512 dwarf2_per_objfile->addr.read (objfile);
19513 if (dwarf2_per_objfile->addr.buffer == NULL)
19514 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19515 objfile_name (objfile));
19516 if (addr_base_or_zero + addr_index * addr_size
19517 >= dwarf2_per_objfile->addr.size)
19518 error (_("DW_FORM_addr_index pointing outside of "
19519 ".debug_addr section [in module %s]"),
19520 objfile_name (objfile));
19521 info_ptr = (dwarf2_per_objfile->addr.buffer
19522 + addr_base_or_zero + addr_index * addr_size);
19523 if (addr_size == 4)
19524 return bfd_get_32 (abfd, info_ptr);
19525 else
19526 return bfd_get_64 (abfd, info_ptr);
19527 }
19528
19529 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19530
19531 static CORE_ADDR
19532 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19533 {
19534 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19535 cu->addr_base, cu->header.addr_size);
19536 }
19537
19538 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19539
19540 static CORE_ADDR
19541 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19542 unsigned int *bytes_read)
19543 {
19544 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19545 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19546
19547 return read_addr_index (cu, addr_index);
19548 }
19549
19550 /* Given an index in .debug_addr, fetch the value.
19551 NOTE: This can be called during dwarf expression evaluation,
19552 long after the debug information has been read, and thus per_cu->cu
19553 may no longer exist. */
19554
19555 CORE_ADDR
19556 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19557 unsigned int addr_index)
19558 {
19559 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19560 struct dwarf2_cu *cu = per_cu->cu;
19561 gdb::optional<ULONGEST> addr_base;
19562 int addr_size;
19563
19564 /* We need addr_base and addr_size.
19565 If we don't have PER_CU->cu, we have to get it.
19566 Nasty, but the alternative is storing the needed info in PER_CU,
19567 which at this point doesn't seem justified: it's not clear how frequently
19568 it would get used and it would increase the size of every PER_CU.
19569 Entry points like dwarf2_per_cu_addr_size do a similar thing
19570 so we're not in uncharted territory here.
19571 Alas we need to be a bit more complicated as addr_base is contained
19572 in the DIE.
19573
19574 We don't need to read the entire CU(/TU).
19575 We just need the header and top level die.
19576
19577 IWBN to use the aging mechanism to let us lazily later discard the CU.
19578 For now we skip this optimization. */
19579
19580 if (cu != NULL)
19581 {
19582 addr_base = cu->addr_base;
19583 addr_size = cu->header.addr_size;
19584 }
19585 else
19586 {
19587 cutu_reader reader (per_cu, NULL, 0, 0, false);
19588 addr_base = reader.cu->addr_base;
19589 addr_size = reader.cu->header.addr_size;
19590 }
19591
19592 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19593 addr_size);
19594 }
19595
19596 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19597 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19598 DWO file. */
19599
19600 static const char *
19601 read_str_index (struct dwarf2_cu *cu,
19602 struct dwarf2_section_info *str_section,
19603 struct dwarf2_section_info *str_offsets_section,
19604 ULONGEST str_offsets_base, ULONGEST str_index)
19605 {
19606 struct dwarf2_per_objfile *dwarf2_per_objfile
19607 = cu->per_cu->dwarf2_per_objfile;
19608 struct objfile *objfile = dwarf2_per_objfile->objfile;
19609 const char *objf_name = objfile_name (objfile);
19610 bfd *abfd = objfile->obfd;
19611 const gdb_byte *info_ptr;
19612 ULONGEST str_offset;
19613 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19614
19615 str_section->read (objfile);
19616 str_offsets_section->read (objfile);
19617 if (str_section->buffer == NULL)
19618 error (_("%s used without %s section"
19619 " in CU at offset %s [in module %s]"),
19620 form_name, str_section->get_name (),
19621 sect_offset_str (cu->header.sect_off), objf_name);
19622 if (str_offsets_section->buffer == NULL)
19623 error (_("%s used without %s section"
19624 " in CU at offset %s [in module %s]"),
19625 form_name, str_section->get_name (),
19626 sect_offset_str (cu->header.sect_off), objf_name);
19627 info_ptr = (str_offsets_section->buffer
19628 + str_offsets_base
19629 + str_index * cu->header.offset_size);
19630 if (cu->header.offset_size == 4)
19631 str_offset = bfd_get_32 (abfd, info_ptr);
19632 else
19633 str_offset = bfd_get_64 (abfd, info_ptr);
19634 if (str_offset >= str_section->size)
19635 error (_("Offset from %s pointing outside of"
19636 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19637 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19638 return (const char *) (str_section->buffer + str_offset);
19639 }
19640
19641 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19642
19643 static const char *
19644 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19645 {
19646 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19647 ? reader->cu->header.addr_size : 0;
19648 return read_str_index (reader->cu,
19649 &reader->dwo_file->sections.str,
19650 &reader->dwo_file->sections.str_offsets,
19651 str_offsets_base, str_index);
19652 }
19653
19654 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19655
19656 static const char *
19657 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19658 {
19659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19660 const char *objf_name = objfile_name (objfile);
19661 static const char form_name[] = "DW_FORM_GNU_str_index";
19662 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19663
19664 if (!cu->str_offsets_base.has_value ())
19665 error (_("%s used in Fission stub without %s"
19666 " in CU at offset 0x%lx [in module %s]"),
19667 form_name, str_offsets_attr_name,
19668 (long) cu->header.offset_size, objf_name);
19669
19670 return read_str_index (cu,
19671 &cu->per_cu->dwarf2_per_objfile->str,
19672 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19673 *cu->str_offsets_base, str_index);
19674 }
19675
19676 /* Return the length of an LEB128 number in BUF. */
19677
19678 static int
19679 leb128_size (const gdb_byte *buf)
19680 {
19681 const gdb_byte *begin = buf;
19682 gdb_byte byte;
19683
19684 while (1)
19685 {
19686 byte = *buf++;
19687 if ((byte & 128) == 0)
19688 return buf - begin;
19689 }
19690 }
19691
19692 static void
19693 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19694 {
19695 switch (lang)
19696 {
19697 case DW_LANG_C89:
19698 case DW_LANG_C99:
19699 case DW_LANG_C11:
19700 case DW_LANG_C:
19701 case DW_LANG_UPC:
19702 cu->language = language_c;
19703 break;
19704 case DW_LANG_Java:
19705 case DW_LANG_C_plus_plus:
19706 case DW_LANG_C_plus_plus_11:
19707 case DW_LANG_C_plus_plus_14:
19708 cu->language = language_cplus;
19709 break;
19710 case DW_LANG_D:
19711 cu->language = language_d;
19712 break;
19713 case DW_LANG_Fortran77:
19714 case DW_LANG_Fortran90:
19715 case DW_LANG_Fortran95:
19716 case DW_LANG_Fortran03:
19717 case DW_LANG_Fortran08:
19718 cu->language = language_fortran;
19719 break;
19720 case DW_LANG_Go:
19721 cu->language = language_go;
19722 break;
19723 case DW_LANG_Mips_Assembler:
19724 cu->language = language_asm;
19725 break;
19726 case DW_LANG_Ada83:
19727 case DW_LANG_Ada95:
19728 cu->language = language_ada;
19729 break;
19730 case DW_LANG_Modula2:
19731 cu->language = language_m2;
19732 break;
19733 case DW_LANG_Pascal83:
19734 cu->language = language_pascal;
19735 break;
19736 case DW_LANG_ObjC:
19737 cu->language = language_objc;
19738 break;
19739 case DW_LANG_Rust:
19740 case DW_LANG_Rust_old:
19741 cu->language = language_rust;
19742 break;
19743 case DW_LANG_Cobol74:
19744 case DW_LANG_Cobol85:
19745 default:
19746 cu->language = language_minimal;
19747 break;
19748 }
19749 cu->language_defn = language_def (cu->language);
19750 }
19751
19752 /* Return the named attribute or NULL if not there. */
19753
19754 static struct attribute *
19755 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19756 {
19757 for (;;)
19758 {
19759 unsigned int i;
19760 struct attribute *spec = NULL;
19761
19762 for (i = 0; i < die->num_attrs; ++i)
19763 {
19764 if (die->attrs[i].name == name)
19765 return &die->attrs[i];
19766 if (die->attrs[i].name == DW_AT_specification
19767 || die->attrs[i].name == DW_AT_abstract_origin)
19768 spec = &die->attrs[i];
19769 }
19770
19771 if (!spec)
19772 break;
19773
19774 die = follow_die_ref (die, spec, &cu);
19775 }
19776
19777 return NULL;
19778 }
19779
19780 /* Return the named attribute or NULL if not there,
19781 but do not follow DW_AT_specification, etc.
19782 This is for use in contexts where we're reading .debug_types dies.
19783 Following DW_AT_specification, DW_AT_abstract_origin will take us
19784 back up the chain, and we want to go down. */
19785
19786 static struct attribute *
19787 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19788 {
19789 unsigned int i;
19790
19791 for (i = 0; i < die->num_attrs; ++i)
19792 if (die->attrs[i].name == name)
19793 return &die->attrs[i];
19794
19795 return NULL;
19796 }
19797
19798 /* Return the string associated with a string-typed attribute, or NULL if it
19799 is either not found or is of an incorrect type. */
19800
19801 static const char *
19802 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19803 {
19804 struct attribute *attr;
19805 const char *str = NULL;
19806
19807 attr = dwarf2_attr (die, name, cu);
19808
19809 if (attr != NULL)
19810 {
19811 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19812 || attr->form == DW_FORM_string
19813 || attr->form == DW_FORM_strx
19814 || attr->form == DW_FORM_strx1
19815 || attr->form == DW_FORM_strx2
19816 || attr->form == DW_FORM_strx3
19817 || attr->form == DW_FORM_strx4
19818 || attr->form == DW_FORM_GNU_str_index
19819 || attr->form == DW_FORM_GNU_strp_alt)
19820 str = DW_STRING (attr);
19821 else
19822 complaint (_("string type expected for attribute %s for "
19823 "DIE at %s in module %s"),
19824 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19825 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19826 }
19827
19828 return str;
19829 }
19830
19831 /* Return the dwo name or NULL if not present. If present, it is in either
19832 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19833 static const char *
19834 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19835 {
19836 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19837 if (dwo_name == nullptr)
19838 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19839 return dwo_name;
19840 }
19841
19842 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19843 and holds a non-zero value. This function should only be used for
19844 DW_FORM_flag or DW_FORM_flag_present attributes. */
19845
19846 static int
19847 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19848 {
19849 struct attribute *attr = dwarf2_attr (die, name, cu);
19850
19851 return (attr && DW_UNSND (attr));
19852 }
19853
19854 static int
19855 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19856 {
19857 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19858 which value is non-zero. However, we have to be careful with
19859 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19860 (via dwarf2_flag_true_p) follows this attribute. So we may
19861 end up accidently finding a declaration attribute that belongs
19862 to a different DIE referenced by the specification attribute,
19863 even though the given DIE does not have a declaration attribute. */
19864 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19865 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19866 }
19867
19868 /* Return the die giving the specification for DIE, if there is
19869 one. *SPEC_CU is the CU containing DIE on input, and the CU
19870 containing the return value on output. If there is no
19871 specification, but there is an abstract origin, that is
19872 returned. */
19873
19874 static struct die_info *
19875 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19876 {
19877 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19878 *spec_cu);
19879
19880 if (spec_attr == NULL)
19881 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19882
19883 if (spec_attr == NULL)
19884 return NULL;
19885 else
19886 return follow_die_ref (die, spec_attr, spec_cu);
19887 }
19888
19889 /* Stub for free_line_header to match void * callback types. */
19890
19891 static void
19892 free_line_header_voidp (void *arg)
19893 {
19894 struct line_header *lh = (struct line_header *) arg;
19895
19896 delete lh;
19897 }
19898
19899 void
19900 line_header::add_include_dir (const char *include_dir)
19901 {
19902 if (dwarf_line_debug >= 2)
19903 {
19904 size_t new_size;
19905 if (version >= 5)
19906 new_size = m_include_dirs.size ();
19907 else
19908 new_size = m_include_dirs.size () + 1;
19909 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19910 new_size, include_dir);
19911 }
19912 m_include_dirs.push_back (include_dir);
19913 }
19914
19915 void
19916 line_header::add_file_name (const char *name,
19917 dir_index d_index,
19918 unsigned int mod_time,
19919 unsigned int length)
19920 {
19921 if (dwarf_line_debug >= 2)
19922 {
19923 size_t new_size;
19924 if (version >= 5)
19925 new_size = file_names_size ();
19926 else
19927 new_size = file_names_size () + 1;
19928 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19929 new_size, name);
19930 }
19931 m_file_names.emplace_back (name, d_index, mod_time, length);
19932 }
19933
19934 /* A convenience function to find the proper .debug_line section for a CU. */
19935
19936 static struct dwarf2_section_info *
19937 get_debug_line_section (struct dwarf2_cu *cu)
19938 {
19939 struct dwarf2_section_info *section;
19940 struct dwarf2_per_objfile *dwarf2_per_objfile
19941 = cu->per_cu->dwarf2_per_objfile;
19942
19943 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19944 DWO file. */
19945 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19946 section = &cu->dwo_unit->dwo_file->sections.line;
19947 else if (cu->per_cu->is_dwz)
19948 {
19949 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19950
19951 section = &dwz->line;
19952 }
19953 else
19954 section = &dwarf2_per_objfile->line;
19955
19956 return section;
19957 }
19958
19959 /* Read directory or file name entry format, starting with byte of
19960 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19961 entries count and the entries themselves in the described entry
19962 format. */
19963
19964 static void
19965 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19966 bfd *abfd, const gdb_byte **bufp,
19967 struct line_header *lh,
19968 const struct comp_unit_head *cu_header,
19969 void (*callback) (struct line_header *lh,
19970 const char *name,
19971 dir_index d_index,
19972 unsigned int mod_time,
19973 unsigned int length))
19974 {
19975 gdb_byte format_count, formati;
19976 ULONGEST data_count, datai;
19977 const gdb_byte *buf = *bufp;
19978 const gdb_byte *format_header_data;
19979 unsigned int bytes_read;
19980
19981 format_count = read_1_byte (abfd, buf);
19982 buf += 1;
19983 format_header_data = buf;
19984 for (formati = 0; formati < format_count; formati++)
19985 {
19986 read_unsigned_leb128 (abfd, buf, &bytes_read);
19987 buf += bytes_read;
19988 read_unsigned_leb128 (abfd, buf, &bytes_read);
19989 buf += bytes_read;
19990 }
19991
19992 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19993 buf += bytes_read;
19994 for (datai = 0; datai < data_count; datai++)
19995 {
19996 const gdb_byte *format = format_header_data;
19997 struct file_entry fe;
19998
19999 for (formati = 0; formati < format_count; formati++)
20000 {
20001 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20002 format += bytes_read;
20003
20004 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20005 format += bytes_read;
20006
20007 gdb::optional<const char *> string;
20008 gdb::optional<unsigned int> uint;
20009
20010 switch (form)
20011 {
20012 case DW_FORM_string:
20013 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20014 buf += bytes_read;
20015 break;
20016
20017 case DW_FORM_line_strp:
20018 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20019 abfd, buf,
20020 cu_header,
20021 &bytes_read));
20022 buf += bytes_read;
20023 break;
20024
20025 case DW_FORM_data1:
20026 uint.emplace (read_1_byte (abfd, buf));
20027 buf += 1;
20028 break;
20029
20030 case DW_FORM_data2:
20031 uint.emplace (read_2_bytes (abfd, buf));
20032 buf += 2;
20033 break;
20034
20035 case DW_FORM_data4:
20036 uint.emplace (read_4_bytes (abfd, buf));
20037 buf += 4;
20038 break;
20039
20040 case DW_FORM_data8:
20041 uint.emplace (read_8_bytes (abfd, buf));
20042 buf += 8;
20043 break;
20044
20045 case DW_FORM_data16:
20046 /* This is used for MD5, but file_entry does not record MD5s. */
20047 buf += 16;
20048 break;
20049
20050 case DW_FORM_udata:
20051 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20052 buf += bytes_read;
20053 break;
20054
20055 case DW_FORM_block:
20056 /* It is valid only for DW_LNCT_timestamp which is ignored by
20057 current GDB. */
20058 break;
20059 }
20060
20061 switch (content_type)
20062 {
20063 case DW_LNCT_path:
20064 if (string.has_value ())
20065 fe.name = *string;
20066 break;
20067 case DW_LNCT_directory_index:
20068 if (uint.has_value ())
20069 fe.d_index = (dir_index) *uint;
20070 break;
20071 case DW_LNCT_timestamp:
20072 if (uint.has_value ())
20073 fe.mod_time = *uint;
20074 break;
20075 case DW_LNCT_size:
20076 if (uint.has_value ())
20077 fe.length = *uint;
20078 break;
20079 case DW_LNCT_MD5:
20080 break;
20081 default:
20082 complaint (_("Unknown format content type %s"),
20083 pulongest (content_type));
20084 }
20085 }
20086
20087 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20088 }
20089
20090 *bufp = buf;
20091 }
20092
20093 /* Read the statement program header starting at OFFSET in
20094 .debug_line, or .debug_line.dwo. Return a pointer
20095 to a struct line_header, allocated using xmalloc.
20096 Returns NULL if there is a problem reading the header, e.g., if it
20097 has a version we don't understand.
20098
20099 NOTE: the strings in the include directory and file name tables of
20100 the returned object point into the dwarf line section buffer,
20101 and must not be freed. */
20102
20103 static line_header_up
20104 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20105 {
20106 const gdb_byte *line_ptr;
20107 unsigned int bytes_read, offset_size;
20108 int i;
20109 const char *cur_dir, *cur_file;
20110 struct dwarf2_section_info *section;
20111 bfd *abfd;
20112 struct dwarf2_per_objfile *dwarf2_per_objfile
20113 = cu->per_cu->dwarf2_per_objfile;
20114
20115 section = get_debug_line_section (cu);
20116 section->read (dwarf2_per_objfile->objfile);
20117 if (section->buffer == NULL)
20118 {
20119 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20120 complaint (_("missing .debug_line.dwo section"));
20121 else
20122 complaint (_("missing .debug_line section"));
20123 return 0;
20124 }
20125
20126 /* We can't do this until we know the section is non-empty.
20127 Only then do we know we have such a section. */
20128 abfd = section->get_bfd_owner ();
20129
20130 /* Make sure that at least there's room for the total_length field.
20131 That could be 12 bytes long, but we're just going to fudge that. */
20132 if (to_underlying (sect_off) + 4 >= section->size)
20133 {
20134 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20135 return 0;
20136 }
20137
20138 line_header_up lh (new line_header ());
20139
20140 lh->sect_off = sect_off;
20141 lh->offset_in_dwz = cu->per_cu->is_dwz;
20142
20143 line_ptr = section->buffer + to_underlying (sect_off);
20144
20145 /* Read in the header. */
20146 lh->total_length =
20147 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20148 &bytes_read, &offset_size);
20149 line_ptr += bytes_read;
20150
20151 const gdb_byte *start_here = line_ptr;
20152
20153 if (line_ptr + lh->total_length > (section->buffer + section->size))
20154 {
20155 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20156 return 0;
20157 }
20158 lh->statement_program_end = start_here + lh->total_length;
20159 lh->version = read_2_bytes (abfd, line_ptr);
20160 line_ptr += 2;
20161 if (lh->version > 5)
20162 {
20163 /* This is a version we don't understand. The format could have
20164 changed in ways we don't handle properly so just punt. */
20165 complaint (_("unsupported version in .debug_line section"));
20166 return NULL;
20167 }
20168 if (lh->version >= 5)
20169 {
20170 gdb_byte segment_selector_size;
20171
20172 /* Skip address size. */
20173 read_1_byte (abfd, line_ptr);
20174 line_ptr += 1;
20175
20176 segment_selector_size = read_1_byte (abfd, line_ptr);
20177 line_ptr += 1;
20178 if (segment_selector_size != 0)
20179 {
20180 complaint (_("unsupported segment selector size %u "
20181 "in .debug_line section"),
20182 segment_selector_size);
20183 return NULL;
20184 }
20185 }
20186 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20187 line_ptr += offset_size;
20188 lh->statement_program_start = line_ptr + lh->header_length;
20189 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20190 line_ptr += 1;
20191 if (lh->version >= 4)
20192 {
20193 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20194 line_ptr += 1;
20195 }
20196 else
20197 lh->maximum_ops_per_instruction = 1;
20198
20199 if (lh->maximum_ops_per_instruction == 0)
20200 {
20201 lh->maximum_ops_per_instruction = 1;
20202 complaint (_("invalid maximum_ops_per_instruction "
20203 "in `.debug_line' section"));
20204 }
20205
20206 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20207 line_ptr += 1;
20208 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20209 line_ptr += 1;
20210 lh->line_range = read_1_byte (abfd, line_ptr);
20211 line_ptr += 1;
20212 lh->opcode_base = read_1_byte (abfd, line_ptr);
20213 line_ptr += 1;
20214 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20215
20216 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20217 for (i = 1; i < lh->opcode_base; ++i)
20218 {
20219 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20220 line_ptr += 1;
20221 }
20222
20223 if (lh->version >= 5)
20224 {
20225 /* Read directory table. */
20226 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20227 &cu->header,
20228 [] (struct line_header *header, const char *name,
20229 dir_index d_index, unsigned int mod_time,
20230 unsigned int length)
20231 {
20232 header->add_include_dir (name);
20233 });
20234
20235 /* Read file name table. */
20236 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20237 &cu->header,
20238 [] (struct line_header *header, const char *name,
20239 dir_index d_index, unsigned int mod_time,
20240 unsigned int length)
20241 {
20242 header->add_file_name (name, d_index, mod_time, length);
20243 });
20244 }
20245 else
20246 {
20247 /* Read directory table. */
20248 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20249 {
20250 line_ptr += bytes_read;
20251 lh->add_include_dir (cur_dir);
20252 }
20253 line_ptr += bytes_read;
20254
20255 /* Read file name table. */
20256 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20257 {
20258 unsigned int mod_time, length;
20259 dir_index d_index;
20260
20261 line_ptr += bytes_read;
20262 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20263 line_ptr += bytes_read;
20264 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20265 line_ptr += bytes_read;
20266 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20267 line_ptr += bytes_read;
20268
20269 lh->add_file_name (cur_file, d_index, mod_time, length);
20270 }
20271 line_ptr += bytes_read;
20272 }
20273
20274 if (line_ptr > (section->buffer + section->size))
20275 complaint (_("line number info header doesn't "
20276 "fit in `.debug_line' section"));
20277
20278 return lh;
20279 }
20280
20281 /* Subroutine of dwarf_decode_lines to simplify it.
20282 Return the file name of the psymtab for the given file_entry.
20283 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20284 If space for the result is malloc'd, *NAME_HOLDER will be set.
20285 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20286
20287 static const char *
20288 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20289 const dwarf2_psymtab *pst,
20290 const char *comp_dir,
20291 gdb::unique_xmalloc_ptr<char> *name_holder)
20292 {
20293 const char *include_name = fe.name;
20294 const char *include_name_to_compare = include_name;
20295 const char *pst_filename;
20296 int file_is_pst;
20297
20298 const char *dir_name = fe.include_dir (lh);
20299
20300 gdb::unique_xmalloc_ptr<char> hold_compare;
20301 if (!IS_ABSOLUTE_PATH (include_name)
20302 && (dir_name != NULL || comp_dir != NULL))
20303 {
20304 /* Avoid creating a duplicate psymtab for PST.
20305 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20306 Before we do the comparison, however, we need to account
20307 for DIR_NAME and COMP_DIR.
20308 First prepend dir_name (if non-NULL). If we still don't
20309 have an absolute path prepend comp_dir (if non-NULL).
20310 However, the directory we record in the include-file's
20311 psymtab does not contain COMP_DIR (to match the
20312 corresponding symtab(s)).
20313
20314 Example:
20315
20316 bash$ cd /tmp
20317 bash$ gcc -g ./hello.c
20318 include_name = "hello.c"
20319 dir_name = "."
20320 DW_AT_comp_dir = comp_dir = "/tmp"
20321 DW_AT_name = "./hello.c"
20322
20323 */
20324
20325 if (dir_name != NULL)
20326 {
20327 name_holder->reset (concat (dir_name, SLASH_STRING,
20328 include_name, (char *) NULL));
20329 include_name = name_holder->get ();
20330 include_name_to_compare = include_name;
20331 }
20332 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20333 {
20334 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20335 include_name, (char *) NULL));
20336 include_name_to_compare = hold_compare.get ();
20337 }
20338 }
20339
20340 pst_filename = pst->filename;
20341 gdb::unique_xmalloc_ptr<char> copied_name;
20342 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20343 {
20344 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20345 pst_filename, (char *) NULL));
20346 pst_filename = copied_name.get ();
20347 }
20348
20349 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20350
20351 if (file_is_pst)
20352 return NULL;
20353 return include_name;
20354 }
20355
20356 /* State machine to track the state of the line number program. */
20357
20358 class lnp_state_machine
20359 {
20360 public:
20361 /* Initialize a machine state for the start of a line number
20362 program. */
20363 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20364 bool record_lines_p);
20365
20366 file_entry *current_file ()
20367 {
20368 /* lh->file_names is 0-based, but the file name numbers in the
20369 statement program are 1-based. */
20370 return m_line_header->file_name_at (m_file);
20371 }
20372
20373 /* Record the line in the state machine. END_SEQUENCE is true if
20374 we're processing the end of a sequence. */
20375 void record_line (bool end_sequence);
20376
20377 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20378 nop-out rest of the lines in this sequence. */
20379 void check_line_address (struct dwarf2_cu *cu,
20380 const gdb_byte *line_ptr,
20381 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20382
20383 void handle_set_discriminator (unsigned int discriminator)
20384 {
20385 m_discriminator = discriminator;
20386 m_line_has_non_zero_discriminator |= discriminator != 0;
20387 }
20388
20389 /* Handle DW_LNE_set_address. */
20390 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20391 {
20392 m_op_index = 0;
20393 address += baseaddr;
20394 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20395 }
20396
20397 /* Handle DW_LNS_advance_pc. */
20398 void handle_advance_pc (CORE_ADDR adjust);
20399
20400 /* Handle a special opcode. */
20401 void handle_special_opcode (unsigned char op_code);
20402
20403 /* Handle DW_LNS_advance_line. */
20404 void handle_advance_line (int line_delta)
20405 {
20406 advance_line (line_delta);
20407 }
20408
20409 /* Handle DW_LNS_set_file. */
20410 void handle_set_file (file_name_index file);
20411
20412 /* Handle DW_LNS_negate_stmt. */
20413 void handle_negate_stmt ()
20414 {
20415 m_is_stmt = !m_is_stmt;
20416 }
20417
20418 /* Handle DW_LNS_const_add_pc. */
20419 void handle_const_add_pc ();
20420
20421 /* Handle DW_LNS_fixed_advance_pc. */
20422 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20423 {
20424 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20425 m_op_index = 0;
20426 }
20427
20428 /* Handle DW_LNS_copy. */
20429 void handle_copy ()
20430 {
20431 record_line (false);
20432 m_discriminator = 0;
20433 }
20434
20435 /* Handle DW_LNE_end_sequence. */
20436 void handle_end_sequence ()
20437 {
20438 m_currently_recording_lines = true;
20439 }
20440
20441 private:
20442 /* Advance the line by LINE_DELTA. */
20443 void advance_line (int line_delta)
20444 {
20445 m_line += line_delta;
20446
20447 if (line_delta != 0)
20448 m_line_has_non_zero_discriminator = m_discriminator != 0;
20449 }
20450
20451 struct dwarf2_cu *m_cu;
20452
20453 gdbarch *m_gdbarch;
20454
20455 /* True if we're recording lines.
20456 Otherwise we're building partial symtabs and are just interested in
20457 finding include files mentioned by the line number program. */
20458 bool m_record_lines_p;
20459
20460 /* The line number header. */
20461 line_header *m_line_header;
20462
20463 /* These are part of the standard DWARF line number state machine,
20464 and initialized according to the DWARF spec. */
20465
20466 unsigned char m_op_index = 0;
20467 /* The line table index of the current file. */
20468 file_name_index m_file = 1;
20469 unsigned int m_line = 1;
20470
20471 /* These are initialized in the constructor. */
20472
20473 CORE_ADDR m_address;
20474 bool m_is_stmt;
20475 unsigned int m_discriminator;
20476
20477 /* Additional bits of state we need to track. */
20478
20479 /* The last file that we called dwarf2_start_subfile for.
20480 This is only used for TLLs. */
20481 unsigned int m_last_file = 0;
20482 /* The last file a line number was recorded for. */
20483 struct subfile *m_last_subfile = NULL;
20484
20485 /* When true, record the lines we decode. */
20486 bool m_currently_recording_lines = false;
20487
20488 /* The last line number that was recorded, used to coalesce
20489 consecutive entries for the same line. This can happen, for
20490 example, when discriminators are present. PR 17276. */
20491 unsigned int m_last_line = 0;
20492 bool m_line_has_non_zero_discriminator = false;
20493 };
20494
20495 void
20496 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20497 {
20498 CORE_ADDR addr_adj = (((m_op_index + adjust)
20499 / m_line_header->maximum_ops_per_instruction)
20500 * m_line_header->minimum_instruction_length);
20501 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20502 m_op_index = ((m_op_index + adjust)
20503 % m_line_header->maximum_ops_per_instruction);
20504 }
20505
20506 void
20507 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20508 {
20509 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20510 CORE_ADDR addr_adj = (((m_op_index
20511 + (adj_opcode / m_line_header->line_range))
20512 / m_line_header->maximum_ops_per_instruction)
20513 * m_line_header->minimum_instruction_length);
20514 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20515 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20516 % m_line_header->maximum_ops_per_instruction);
20517
20518 int line_delta = (m_line_header->line_base
20519 + (adj_opcode % m_line_header->line_range));
20520 advance_line (line_delta);
20521 record_line (false);
20522 m_discriminator = 0;
20523 }
20524
20525 void
20526 lnp_state_machine::handle_set_file (file_name_index file)
20527 {
20528 m_file = file;
20529
20530 const file_entry *fe = current_file ();
20531 if (fe == NULL)
20532 dwarf2_debug_line_missing_file_complaint ();
20533 else if (m_record_lines_p)
20534 {
20535 const char *dir = fe->include_dir (m_line_header);
20536
20537 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20538 m_line_has_non_zero_discriminator = m_discriminator != 0;
20539 dwarf2_start_subfile (m_cu, fe->name, dir);
20540 }
20541 }
20542
20543 void
20544 lnp_state_machine::handle_const_add_pc ()
20545 {
20546 CORE_ADDR adjust
20547 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20548
20549 CORE_ADDR addr_adj
20550 = (((m_op_index + adjust)
20551 / m_line_header->maximum_ops_per_instruction)
20552 * m_line_header->minimum_instruction_length);
20553
20554 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20555 m_op_index = ((m_op_index + adjust)
20556 % m_line_header->maximum_ops_per_instruction);
20557 }
20558
20559 /* Return non-zero if we should add LINE to the line number table.
20560 LINE is the line to add, LAST_LINE is the last line that was added,
20561 LAST_SUBFILE is the subfile for LAST_LINE.
20562 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20563 had a non-zero discriminator.
20564
20565 We have to be careful in the presence of discriminators.
20566 E.g., for this line:
20567
20568 for (i = 0; i < 100000; i++);
20569
20570 clang can emit four line number entries for that one line,
20571 each with a different discriminator.
20572 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20573
20574 However, we want gdb to coalesce all four entries into one.
20575 Otherwise the user could stepi into the middle of the line and
20576 gdb would get confused about whether the pc really was in the
20577 middle of the line.
20578
20579 Things are further complicated by the fact that two consecutive
20580 line number entries for the same line is a heuristic used by gcc
20581 to denote the end of the prologue. So we can't just discard duplicate
20582 entries, we have to be selective about it. The heuristic we use is
20583 that we only collapse consecutive entries for the same line if at least
20584 one of those entries has a non-zero discriminator. PR 17276.
20585
20586 Note: Addresses in the line number state machine can never go backwards
20587 within one sequence, thus this coalescing is ok. */
20588
20589 static int
20590 dwarf_record_line_p (struct dwarf2_cu *cu,
20591 unsigned int line, unsigned int last_line,
20592 int line_has_non_zero_discriminator,
20593 struct subfile *last_subfile)
20594 {
20595 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20596 return 1;
20597 if (line != last_line)
20598 return 1;
20599 /* Same line for the same file that we've seen already.
20600 As a last check, for pr 17276, only record the line if the line
20601 has never had a non-zero discriminator. */
20602 if (!line_has_non_zero_discriminator)
20603 return 1;
20604 return 0;
20605 }
20606
20607 /* Use the CU's builder to record line number LINE beginning at
20608 address ADDRESS in the line table of subfile SUBFILE. */
20609
20610 static void
20611 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20612 unsigned int line, CORE_ADDR address,
20613 struct dwarf2_cu *cu)
20614 {
20615 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20616
20617 if (dwarf_line_debug)
20618 {
20619 fprintf_unfiltered (gdb_stdlog,
20620 "Recording line %u, file %s, address %s\n",
20621 line, lbasename (subfile->name),
20622 paddress (gdbarch, address));
20623 }
20624
20625 if (cu != nullptr)
20626 cu->get_builder ()->record_line (subfile, line, addr);
20627 }
20628
20629 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20630 Mark the end of a set of line number records.
20631 The arguments are the same as for dwarf_record_line_1.
20632 If SUBFILE is NULL the request is ignored. */
20633
20634 static void
20635 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20636 CORE_ADDR address, struct dwarf2_cu *cu)
20637 {
20638 if (subfile == NULL)
20639 return;
20640
20641 if (dwarf_line_debug)
20642 {
20643 fprintf_unfiltered (gdb_stdlog,
20644 "Finishing current line, file %s, address %s\n",
20645 lbasename (subfile->name),
20646 paddress (gdbarch, address));
20647 }
20648
20649 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20650 }
20651
20652 void
20653 lnp_state_machine::record_line (bool end_sequence)
20654 {
20655 if (dwarf_line_debug)
20656 {
20657 fprintf_unfiltered (gdb_stdlog,
20658 "Processing actual line %u: file %u,"
20659 " address %s, is_stmt %u, discrim %u%s\n",
20660 m_line, m_file,
20661 paddress (m_gdbarch, m_address),
20662 m_is_stmt, m_discriminator,
20663 (end_sequence ? "\t(end sequence)" : ""));
20664 }
20665
20666 file_entry *fe = current_file ();
20667
20668 if (fe == NULL)
20669 dwarf2_debug_line_missing_file_complaint ();
20670 /* For now we ignore lines not starting on an instruction boundary.
20671 But not when processing end_sequence for compatibility with the
20672 previous version of the code. */
20673 else if (m_op_index == 0 || end_sequence)
20674 {
20675 fe->included_p = 1;
20676 if (m_record_lines_p
20677 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20678 {
20679 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20680 || end_sequence)
20681 {
20682 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20683 m_currently_recording_lines ? m_cu : nullptr);
20684 }
20685
20686 if (!end_sequence)
20687 {
20688 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20689 m_line_has_non_zero_discriminator,
20690 m_last_subfile))
20691 {
20692 buildsym_compunit *builder = m_cu->get_builder ();
20693 dwarf_record_line_1 (m_gdbarch,
20694 builder->get_current_subfile (),
20695 m_line, m_address,
20696 m_currently_recording_lines ? m_cu : nullptr);
20697 }
20698 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20699 m_last_line = m_line;
20700 }
20701 }
20702 }
20703 }
20704
20705 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20706 line_header *lh, bool record_lines_p)
20707 {
20708 m_cu = cu;
20709 m_gdbarch = arch;
20710 m_record_lines_p = record_lines_p;
20711 m_line_header = lh;
20712
20713 m_currently_recording_lines = true;
20714
20715 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20716 was a line entry for it so that the backend has a chance to adjust it
20717 and also record it in case it needs it. This is currently used by MIPS
20718 code, cf. `mips_adjust_dwarf2_line'. */
20719 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20720 m_is_stmt = lh->default_is_stmt;
20721 m_discriminator = 0;
20722 }
20723
20724 void
20725 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20726 const gdb_byte *line_ptr,
20727 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20728 {
20729 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20730 the pc range of the CU. However, we restrict the test to only ADDRESS
20731 values of zero to preserve GDB's previous behaviour which is to handle
20732 the specific case of a function being GC'd by the linker. */
20733
20734 if (address == 0 && address < unrelocated_lowpc)
20735 {
20736 /* This line table is for a function which has been
20737 GCd by the linker. Ignore it. PR gdb/12528 */
20738
20739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20740 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20741
20742 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20743 line_offset, objfile_name (objfile));
20744 m_currently_recording_lines = false;
20745 /* Note: m_currently_recording_lines is left as false until we see
20746 DW_LNE_end_sequence. */
20747 }
20748 }
20749
20750 /* Subroutine of dwarf_decode_lines to simplify it.
20751 Process the line number information in LH.
20752 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20753 program in order to set included_p for every referenced header. */
20754
20755 static void
20756 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20757 const int decode_for_pst_p, CORE_ADDR lowpc)
20758 {
20759 const gdb_byte *line_ptr, *extended_end;
20760 const gdb_byte *line_end;
20761 unsigned int bytes_read, extended_len;
20762 unsigned char op_code, extended_op;
20763 CORE_ADDR baseaddr;
20764 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20765 bfd *abfd = objfile->obfd;
20766 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20767 /* True if we're recording line info (as opposed to building partial
20768 symtabs and just interested in finding include files mentioned by
20769 the line number program). */
20770 bool record_lines_p = !decode_for_pst_p;
20771
20772 baseaddr = objfile->text_section_offset ();
20773
20774 line_ptr = lh->statement_program_start;
20775 line_end = lh->statement_program_end;
20776
20777 /* Read the statement sequences until there's nothing left. */
20778 while (line_ptr < line_end)
20779 {
20780 /* The DWARF line number program state machine. Reset the state
20781 machine at the start of each sequence. */
20782 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20783 bool end_sequence = false;
20784
20785 if (record_lines_p)
20786 {
20787 /* Start a subfile for the current file of the state
20788 machine. */
20789 const file_entry *fe = state_machine.current_file ();
20790
20791 if (fe != NULL)
20792 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20793 }
20794
20795 /* Decode the table. */
20796 while (line_ptr < line_end && !end_sequence)
20797 {
20798 op_code = read_1_byte (abfd, line_ptr);
20799 line_ptr += 1;
20800
20801 if (op_code >= lh->opcode_base)
20802 {
20803 /* Special opcode. */
20804 state_machine.handle_special_opcode (op_code);
20805 }
20806 else switch (op_code)
20807 {
20808 case DW_LNS_extended_op:
20809 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20810 &bytes_read);
20811 line_ptr += bytes_read;
20812 extended_end = line_ptr + extended_len;
20813 extended_op = read_1_byte (abfd, line_ptr);
20814 line_ptr += 1;
20815 switch (extended_op)
20816 {
20817 case DW_LNE_end_sequence:
20818 state_machine.handle_end_sequence ();
20819 end_sequence = true;
20820 break;
20821 case DW_LNE_set_address:
20822 {
20823 CORE_ADDR address
20824 = read_address (abfd, line_ptr, cu, &bytes_read);
20825 line_ptr += bytes_read;
20826
20827 state_machine.check_line_address (cu, line_ptr,
20828 lowpc - baseaddr, address);
20829 state_machine.handle_set_address (baseaddr, address);
20830 }
20831 break;
20832 case DW_LNE_define_file:
20833 {
20834 const char *cur_file;
20835 unsigned int mod_time, length;
20836 dir_index dindex;
20837
20838 cur_file = read_direct_string (abfd, line_ptr,
20839 &bytes_read);
20840 line_ptr += bytes_read;
20841 dindex = (dir_index)
20842 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20843 line_ptr += bytes_read;
20844 mod_time =
20845 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20846 line_ptr += bytes_read;
20847 length =
20848 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20849 line_ptr += bytes_read;
20850 lh->add_file_name (cur_file, dindex, mod_time, length);
20851 }
20852 break;
20853 case DW_LNE_set_discriminator:
20854 {
20855 /* The discriminator is not interesting to the
20856 debugger; just ignore it. We still need to
20857 check its value though:
20858 if there are consecutive entries for the same
20859 (non-prologue) line we want to coalesce them.
20860 PR 17276. */
20861 unsigned int discr
20862 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20863 line_ptr += bytes_read;
20864
20865 state_machine.handle_set_discriminator (discr);
20866 }
20867 break;
20868 default:
20869 complaint (_("mangled .debug_line section"));
20870 return;
20871 }
20872 /* Make sure that we parsed the extended op correctly. If e.g.
20873 we expected a different address size than the producer used,
20874 we may have read the wrong number of bytes. */
20875 if (line_ptr != extended_end)
20876 {
20877 complaint (_("mangled .debug_line section"));
20878 return;
20879 }
20880 break;
20881 case DW_LNS_copy:
20882 state_machine.handle_copy ();
20883 break;
20884 case DW_LNS_advance_pc:
20885 {
20886 CORE_ADDR adjust
20887 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20888 line_ptr += bytes_read;
20889
20890 state_machine.handle_advance_pc (adjust);
20891 }
20892 break;
20893 case DW_LNS_advance_line:
20894 {
20895 int line_delta
20896 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20897 line_ptr += bytes_read;
20898
20899 state_machine.handle_advance_line (line_delta);
20900 }
20901 break;
20902 case DW_LNS_set_file:
20903 {
20904 file_name_index file
20905 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20906 &bytes_read);
20907 line_ptr += bytes_read;
20908
20909 state_machine.handle_set_file (file);
20910 }
20911 break;
20912 case DW_LNS_set_column:
20913 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20914 line_ptr += bytes_read;
20915 break;
20916 case DW_LNS_negate_stmt:
20917 state_machine.handle_negate_stmt ();
20918 break;
20919 case DW_LNS_set_basic_block:
20920 break;
20921 /* Add to the address register of the state machine the
20922 address increment value corresponding to special opcode
20923 255. I.e., this value is scaled by the minimum
20924 instruction length since special opcode 255 would have
20925 scaled the increment. */
20926 case DW_LNS_const_add_pc:
20927 state_machine.handle_const_add_pc ();
20928 break;
20929 case DW_LNS_fixed_advance_pc:
20930 {
20931 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20932 line_ptr += 2;
20933
20934 state_machine.handle_fixed_advance_pc (addr_adj);
20935 }
20936 break;
20937 default:
20938 {
20939 /* Unknown standard opcode, ignore it. */
20940 int i;
20941
20942 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20943 {
20944 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20945 line_ptr += bytes_read;
20946 }
20947 }
20948 }
20949 }
20950
20951 if (!end_sequence)
20952 dwarf2_debug_line_missing_end_sequence_complaint ();
20953
20954 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20955 in which case we still finish recording the last line). */
20956 state_machine.record_line (true);
20957 }
20958 }
20959
20960 /* Decode the Line Number Program (LNP) for the given line_header
20961 structure and CU. The actual information extracted and the type
20962 of structures created from the LNP depends on the value of PST.
20963
20964 1. If PST is NULL, then this procedure uses the data from the program
20965 to create all necessary symbol tables, and their linetables.
20966
20967 2. If PST is not NULL, this procedure reads the program to determine
20968 the list of files included by the unit represented by PST, and
20969 builds all the associated partial symbol tables.
20970
20971 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20972 It is used for relative paths in the line table.
20973 NOTE: When processing partial symtabs (pst != NULL),
20974 comp_dir == pst->dirname.
20975
20976 NOTE: It is important that psymtabs have the same file name (via strcmp)
20977 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20978 symtab we don't use it in the name of the psymtabs we create.
20979 E.g. expand_line_sal requires this when finding psymtabs to expand.
20980 A good testcase for this is mb-inline.exp.
20981
20982 LOWPC is the lowest address in CU (or 0 if not known).
20983
20984 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20985 for its PC<->lines mapping information. Otherwise only the filename
20986 table is read in. */
20987
20988 static void
20989 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20990 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20991 CORE_ADDR lowpc, int decode_mapping)
20992 {
20993 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20994 const int decode_for_pst_p = (pst != NULL);
20995
20996 if (decode_mapping)
20997 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20998
20999 if (decode_for_pst_p)
21000 {
21001 /* Now that we're done scanning the Line Header Program, we can
21002 create the psymtab of each included file. */
21003 for (auto &file_entry : lh->file_names ())
21004 if (file_entry.included_p == 1)
21005 {
21006 gdb::unique_xmalloc_ptr<char> name_holder;
21007 const char *include_name =
21008 psymtab_include_file_name (lh, file_entry, pst,
21009 comp_dir, &name_holder);
21010 if (include_name != NULL)
21011 dwarf2_create_include_psymtab (include_name, pst, objfile);
21012 }
21013 }
21014 else
21015 {
21016 /* Make sure a symtab is created for every file, even files
21017 which contain only variables (i.e. no code with associated
21018 line numbers). */
21019 buildsym_compunit *builder = cu->get_builder ();
21020 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21021
21022 for (auto &fe : lh->file_names ())
21023 {
21024 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21025 if (builder->get_current_subfile ()->symtab == NULL)
21026 {
21027 builder->get_current_subfile ()->symtab
21028 = allocate_symtab (cust,
21029 builder->get_current_subfile ()->name);
21030 }
21031 fe.symtab = builder->get_current_subfile ()->symtab;
21032 }
21033 }
21034 }
21035
21036 /* Start a subfile for DWARF. FILENAME is the name of the file and
21037 DIRNAME the name of the source directory which contains FILENAME
21038 or NULL if not known.
21039 This routine tries to keep line numbers from identical absolute and
21040 relative file names in a common subfile.
21041
21042 Using the `list' example from the GDB testsuite, which resides in
21043 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21044 of /srcdir/list0.c yields the following debugging information for list0.c:
21045
21046 DW_AT_name: /srcdir/list0.c
21047 DW_AT_comp_dir: /compdir
21048 files.files[0].name: list0.h
21049 files.files[0].dir: /srcdir
21050 files.files[1].name: list0.c
21051 files.files[1].dir: /srcdir
21052
21053 The line number information for list0.c has to end up in a single
21054 subfile, so that `break /srcdir/list0.c:1' works as expected.
21055 start_subfile will ensure that this happens provided that we pass the
21056 concatenation of files.files[1].dir and files.files[1].name as the
21057 subfile's name. */
21058
21059 static void
21060 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21061 const char *dirname)
21062 {
21063 gdb::unique_xmalloc_ptr<char> copy;
21064
21065 /* In order not to lose the line information directory,
21066 we concatenate it to the filename when it makes sense.
21067 Note that the Dwarf3 standard says (speaking of filenames in line
21068 information): ``The directory index is ignored for file names
21069 that represent full path names''. Thus ignoring dirname in the
21070 `else' branch below isn't an issue. */
21071
21072 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21073 {
21074 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21075 filename = copy.get ();
21076 }
21077
21078 cu->get_builder ()->start_subfile (filename);
21079 }
21080
21081 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21082 buildsym_compunit constructor. */
21083
21084 struct compunit_symtab *
21085 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21086 CORE_ADDR low_pc)
21087 {
21088 gdb_assert (m_builder == nullptr);
21089
21090 m_builder.reset (new struct buildsym_compunit
21091 (per_cu->dwarf2_per_objfile->objfile,
21092 name, comp_dir, language, low_pc));
21093
21094 list_in_scope = get_builder ()->get_file_symbols ();
21095
21096 get_builder ()->record_debugformat ("DWARF 2");
21097 get_builder ()->record_producer (producer);
21098
21099 processing_has_namespace_info = false;
21100
21101 return get_builder ()->get_compunit_symtab ();
21102 }
21103
21104 static void
21105 var_decode_location (struct attribute *attr, struct symbol *sym,
21106 struct dwarf2_cu *cu)
21107 {
21108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21109 struct comp_unit_head *cu_header = &cu->header;
21110
21111 /* NOTE drow/2003-01-30: There used to be a comment and some special
21112 code here to turn a symbol with DW_AT_external and a
21113 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21114 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21115 with some versions of binutils) where shared libraries could have
21116 relocations against symbols in their debug information - the
21117 minimal symbol would have the right address, but the debug info
21118 would not. It's no longer necessary, because we will explicitly
21119 apply relocations when we read in the debug information now. */
21120
21121 /* A DW_AT_location attribute with no contents indicates that a
21122 variable has been optimized away. */
21123 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21124 {
21125 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21126 return;
21127 }
21128
21129 /* Handle one degenerate form of location expression specially, to
21130 preserve GDB's previous behavior when section offsets are
21131 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21132 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21133
21134 if (attr->form_is_block ()
21135 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21136 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21137 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21138 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21139 && (DW_BLOCK (attr)->size
21140 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21141 {
21142 unsigned int dummy;
21143
21144 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21145 SET_SYMBOL_VALUE_ADDRESS (sym,
21146 read_address (objfile->obfd,
21147 DW_BLOCK (attr)->data + 1,
21148 cu, &dummy));
21149 else
21150 SET_SYMBOL_VALUE_ADDRESS
21151 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21152 &dummy));
21153 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21154 fixup_symbol_section (sym, objfile);
21155 SET_SYMBOL_VALUE_ADDRESS
21156 (sym,
21157 SYMBOL_VALUE_ADDRESS (sym)
21158 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21159 return;
21160 }
21161
21162 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21163 expression evaluator, and use LOC_COMPUTED only when necessary
21164 (i.e. when the value of a register or memory location is
21165 referenced, or a thread-local block, etc.). Then again, it might
21166 not be worthwhile. I'm assuming that it isn't unless performance
21167 or memory numbers show me otherwise. */
21168
21169 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21170
21171 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21172 cu->has_loclist = true;
21173 }
21174
21175 /* Given a pointer to a DWARF information entry, figure out if we need
21176 to make a symbol table entry for it, and if so, create a new entry
21177 and return a pointer to it.
21178 If TYPE is NULL, determine symbol type from the die, otherwise
21179 used the passed type.
21180 If SPACE is not NULL, use it to hold the new symbol. If it is
21181 NULL, allocate a new symbol on the objfile's obstack. */
21182
21183 static struct symbol *
21184 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21185 struct symbol *space)
21186 {
21187 struct dwarf2_per_objfile *dwarf2_per_objfile
21188 = cu->per_cu->dwarf2_per_objfile;
21189 struct objfile *objfile = dwarf2_per_objfile->objfile;
21190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21191 struct symbol *sym = NULL;
21192 const char *name;
21193 struct attribute *attr = NULL;
21194 struct attribute *attr2 = NULL;
21195 CORE_ADDR baseaddr;
21196 struct pending **list_to_add = NULL;
21197
21198 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21199
21200 baseaddr = objfile->text_section_offset ();
21201
21202 name = dwarf2_name (die, cu);
21203 if (name)
21204 {
21205 const char *linkagename;
21206 int suppress_add = 0;
21207
21208 if (space)
21209 sym = space;
21210 else
21211 sym = allocate_symbol (objfile);
21212 OBJSTAT (objfile, n_syms++);
21213
21214 /* Cache this symbol's name and the name's demangled form (if any). */
21215 sym->set_language (cu->language, &objfile->objfile_obstack);
21216 linkagename = dwarf2_physname (name, die, cu);
21217 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21218
21219 /* Fortran does not have mangling standard and the mangling does differ
21220 between gfortran, iFort etc. */
21221 if (cu->language == language_fortran
21222 && symbol_get_demangled_name (sym) == NULL)
21223 symbol_set_demangled_name (sym,
21224 dwarf2_full_name (name, die, cu),
21225 NULL);
21226
21227 /* Default assumptions.
21228 Use the passed type or decode it from the die. */
21229 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21230 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21231 if (type != NULL)
21232 SYMBOL_TYPE (sym) = type;
21233 else
21234 SYMBOL_TYPE (sym) = die_type (die, cu);
21235 attr = dwarf2_attr (die,
21236 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21237 cu);
21238 if (attr != nullptr)
21239 {
21240 SYMBOL_LINE (sym) = DW_UNSND (attr);
21241 }
21242
21243 attr = dwarf2_attr (die,
21244 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21245 cu);
21246 if (attr != nullptr)
21247 {
21248 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21249 struct file_entry *fe;
21250
21251 if (cu->line_header != NULL)
21252 fe = cu->line_header->file_name_at (file_index);
21253 else
21254 fe = NULL;
21255
21256 if (fe == NULL)
21257 complaint (_("file index out of range"));
21258 else
21259 symbol_set_symtab (sym, fe->symtab);
21260 }
21261
21262 switch (die->tag)
21263 {
21264 case DW_TAG_label:
21265 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21266 if (attr != nullptr)
21267 {
21268 CORE_ADDR addr;
21269
21270 addr = attr->value_as_address ();
21271 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21272 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21273 }
21274 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21275 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21276 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21277 add_symbol_to_list (sym, cu->list_in_scope);
21278 break;
21279 case DW_TAG_subprogram:
21280 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21281 finish_block. */
21282 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21283 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21284 if ((attr2 && (DW_UNSND (attr2) != 0))
21285 || cu->language == language_ada
21286 || cu->language == language_fortran)
21287 {
21288 /* Subprograms marked external are stored as a global symbol.
21289 Ada and Fortran subprograms, whether marked external or
21290 not, are always stored as a global symbol, because we want
21291 to be able to access them globally. For instance, we want
21292 to be able to break on a nested subprogram without having
21293 to specify the context. */
21294 list_to_add = cu->get_builder ()->get_global_symbols ();
21295 }
21296 else
21297 {
21298 list_to_add = cu->list_in_scope;
21299 }
21300 break;
21301 case DW_TAG_inlined_subroutine:
21302 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21303 finish_block. */
21304 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21305 SYMBOL_INLINED (sym) = 1;
21306 list_to_add = cu->list_in_scope;
21307 break;
21308 case DW_TAG_template_value_param:
21309 suppress_add = 1;
21310 /* Fall through. */
21311 case DW_TAG_constant:
21312 case DW_TAG_variable:
21313 case DW_TAG_member:
21314 /* Compilation with minimal debug info may result in
21315 variables with missing type entries. Change the
21316 misleading `void' type to something sensible. */
21317 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21318 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21319
21320 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21321 /* In the case of DW_TAG_member, we should only be called for
21322 static const members. */
21323 if (die->tag == DW_TAG_member)
21324 {
21325 /* dwarf2_add_field uses die_is_declaration,
21326 so we do the same. */
21327 gdb_assert (die_is_declaration (die, cu));
21328 gdb_assert (attr);
21329 }
21330 if (attr != nullptr)
21331 {
21332 dwarf2_const_value (attr, sym, cu);
21333 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21334 if (!suppress_add)
21335 {
21336 if (attr2 && (DW_UNSND (attr2) != 0))
21337 list_to_add = cu->get_builder ()->get_global_symbols ();
21338 else
21339 list_to_add = cu->list_in_scope;
21340 }
21341 break;
21342 }
21343 attr = dwarf2_attr (die, DW_AT_location, cu);
21344 if (attr != nullptr)
21345 {
21346 var_decode_location (attr, sym, cu);
21347 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21348
21349 /* Fortran explicitly imports any global symbols to the local
21350 scope by DW_TAG_common_block. */
21351 if (cu->language == language_fortran && die->parent
21352 && die->parent->tag == DW_TAG_common_block)
21353 attr2 = NULL;
21354
21355 if (SYMBOL_CLASS (sym) == LOC_STATIC
21356 && SYMBOL_VALUE_ADDRESS (sym) == 0
21357 && !dwarf2_per_objfile->has_section_at_zero)
21358 {
21359 /* When a static variable is eliminated by the linker,
21360 the corresponding debug information is not stripped
21361 out, but the variable address is set to null;
21362 do not add such variables into symbol table. */
21363 }
21364 else if (attr2 && (DW_UNSND (attr2) != 0))
21365 {
21366 if (SYMBOL_CLASS (sym) == LOC_STATIC
21367 && (objfile->flags & OBJF_MAINLINE) == 0
21368 && dwarf2_per_objfile->can_copy)
21369 {
21370 /* A global static variable might be subject to
21371 copy relocation. We first check for a local
21372 minsym, though, because maybe the symbol was
21373 marked hidden, in which case this would not
21374 apply. */
21375 bound_minimal_symbol found
21376 = (lookup_minimal_symbol_linkage
21377 (sym->linkage_name (), objfile));
21378 if (found.minsym != nullptr)
21379 sym->maybe_copied = 1;
21380 }
21381
21382 /* A variable with DW_AT_external is never static,
21383 but it may be block-scoped. */
21384 list_to_add
21385 = ((cu->list_in_scope
21386 == cu->get_builder ()->get_file_symbols ())
21387 ? cu->get_builder ()->get_global_symbols ()
21388 : cu->list_in_scope);
21389 }
21390 else
21391 list_to_add = cu->list_in_scope;
21392 }
21393 else
21394 {
21395 /* We do not know the address of this symbol.
21396 If it is an external symbol and we have type information
21397 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21398 The address of the variable will then be determined from
21399 the minimal symbol table whenever the variable is
21400 referenced. */
21401 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21402
21403 /* Fortran explicitly imports any global symbols to the local
21404 scope by DW_TAG_common_block. */
21405 if (cu->language == language_fortran && die->parent
21406 && die->parent->tag == DW_TAG_common_block)
21407 {
21408 /* SYMBOL_CLASS doesn't matter here because
21409 read_common_block is going to reset it. */
21410 if (!suppress_add)
21411 list_to_add = cu->list_in_scope;
21412 }
21413 else if (attr2 && (DW_UNSND (attr2) != 0)
21414 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21415 {
21416 /* A variable with DW_AT_external is never static, but it
21417 may be block-scoped. */
21418 list_to_add
21419 = ((cu->list_in_scope
21420 == cu->get_builder ()->get_file_symbols ())
21421 ? cu->get_builder ()->get_global_symbols ()
21422 : cu->list_in_scope);
21423
21424 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21425 }
21426 else if (!die_is_declaration (die, cu))
21427 {
21428 /* Use the default LOC_OPTIMIZED_OUT class. */
21429 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21430 if (!suppress_add)
21431 list_to_add = cu->list_in_scope;
21432 }
21433 }
21434 break;
21435 case DW_TAG_formal_parameter:
21436 {
21437 /* If we are inside a function, mark this as an argument. If
21438 not, we might be looking at an argument to an inlined function
21439 when we do not have enough information to show inlined frames;
21440 pretend it's a local variable in that case so that the user can
21441 still see it. */
21442 struct context_stack *curr
21443 = cu->get_builder ()->get_current_context_stack ();
21444 if (curr != nullptr && curr->name != nullptr)
21445 SYMBOL_IS_ARGUMENT (sym) = 1;
21446 attr = dwarf2_attr (die, DW_AT_location, cu);
21447 if (attr != nullptr)
21448 {
21449 var_decode_location (attr, sym, cu);
21450 }
21451 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21452 if (attr != nullptr)
21453 {
21454 dwarf2_const_value (attr, sym, cu);
21455 }
21456
21457 list_to_add = cu->list_in_scope;
21458 }
21459 break;
21460 case DW_TAG_unspecified_parameters:
21461 /* From varargs functions; gdb doesn't seem to have any
21462 interest in this information, so just ignore it for now.
21463 (FIXME?) */
21464 break;
21465 case DW_TAG_template_type_param:
21466 suppress_add = 1;
21467 /* Fall through. */
21468 case DW_TAG_class_type:
21469 case DW_TAG_interface_type:
21470 case DW_TAG_structure_type:
21471 case DW_TAG_union_type:
21472 case DW_TAG_set_type:
21473 case DW_TAG_enumeration_type:
21474 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21475 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21476
21477 {
21478 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21479 really ever be static objects: otherwise, if you try
21480 to, say, break of a class's method and you're in a file
21481 which doesn't mention that class, it won't work unless
21482 the check for all static symbols in lookup_symbol_aux
21483 saves you. See the OtherFileClass tests in
21484 gdb.c++/namespace.exp. */
21485
21486 if (!suppress_add)
21487 {
21488 buildsym_compunit *builder = cu->get_builder ();
21489 list_to_add
21490 = (cu->list_in_scope == builder->get_file_symbols ()
21491 && cu->language == language_cplus
21492 ? builder->get_global_symbols ()
21493 : cu->list_in_scope);
21494
21495 /* The semantics of C++ state that "struct foo {
21496 ... }" also defines a typedef for "foo". */
21497 if (cu->language == language_cplus
21498 || cu->language == language_ada
21499 || cu->language == language_d
21500 || cu->language == language_rust)
21501 {
21502 /* The symbol's name is already allocated along
21503 with this objfile, so we don't need to
21504 duplicate it for the type. */
21505 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21506 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21507 }
21508 }
21509 }
21510 break;
21511 case DW_TAG_typedef:
21512 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21513 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21514 list_to_add = cu->list_in_scope;
21515 break;
21516 case DW_TAG_base_type:
21517 case DW_TAG_subrange_type:
21518 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21519 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21520 list_to_add = cu->list_in_scope;
21521 break;
21522 case DW_TAG_enumerator:
21523 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21524 if (attr != nullptr)
21525 {
21526 dwarf2_const_value (attr, sym, cu);
21527 }
21528 {
21529 /* NOTE: carlton/2003-11-10: See comment above in the
21530 DW_TAG_class_type, etc. block. */
21531
21532 list_to_add
21533 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21534 && cu->language == language_cplus
21535 ? cu->get_builder ()->get_global_symbols ()
21536 : cu->list_in_scope);
21537 }
21538 break;
21539 case DW_TAG_imported_declaration:
21540 case DW_TAG_namespace:
21541 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21542 list_to_add = cu->get_builder ()->get_global_symbols ();
21543 break;
21544 case DW_TAG_module:
21545 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21546 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21547 list_to_add = cu->get_builder ()->get_global_symbols ();
21548 break;
21549 case DW_TAG_common_block:
21550 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21551 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21552 add_symbol_to_list (sym, cu->list_in_scope);
21553 break;
21554 default:
21555 /* Not a tag we recognize. Hopefully we aren't processing
21556 trash data, but since we must specifically ignore things
21557 we don't recognize, there is nothing else we should do at
21558 this point. */
21559 complaint (_("unsupported tag: '%s'"),
21560 dwarf_tag_name (die->tag));
21561 break;
21562 }
21563
21564 if (suppress_add)
21565 {
21566 sym->hash_next = objfile->template_symbols;
21567 objfile->template_symbols = sym;
21568 list_to_add = NULL;
21569 }
21570
21571 if (list_to_add != NULL)
21572 add_symbol_to_list (sym, list_to_add);
21573
21574 /* For the benefit of old versions of GCC, check for anonymous
21575 namespaces based on the demangled name. */
21576 if (!cu->processing_has_namespace_info
21577 && cu->language == language_cplus)
21578 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21579 }
21580 return (sym);
21581 }
21582
21583 /* Given an attr with a DW_FORM_dataN value in host byte order,
21584 zero-extend it as appropriate for the symbol's type. The DWARF
21585 standard (v4) is not entirely clear about the meaning of using
21586 DW_FORM_dataN for a constant with a signed type, where the type is
21587 wider than the data. The conclusion of a discussion on the DWARF
21588 list was that this is unspecified. We choose to always zero-extend
21589 because that is the interpretation long in use by GCC. */
21590
21591 static gdb_byte *
21592 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21593 struct dwarf2_cu *cu, LONGEST *value, int bits)
21594 {
21595 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21596 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21597 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21598 LONGEST l = DW_UNSND (attr);
21599
21600 if (bits < sizeof (*value) * 8)
21601 {
21602 l &= ((LONGEST) 1 << bits) - 1;
21603 *value = l;
21604 }
21605 else if (bits == sizeof (*value) * 8)
21606 *value = l;
21607 else
21608 {
21609 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21610 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21611 return bytes;
21612 }
21613
21614 return NULL;
21615 }
21616
21617 /* Read a constant value from an attribute. Either set *VALUE, or if
21618 the value does not fit in *VALUE, set *BYTES - either already
21619 allocated on the objfile obstack, or newly allocated on OBSTACK,
21620 or, set *BATON, if we translated the constant to a location
21621 expression. */
21622
21623 static void
21624 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21625 const char *name, struct obstack *obstack,
21626 struct dwarf2_cu *cu,
21627 LONGEST *value, const gdb_byte **bytes,
21628 struct dwarf2_locexpr_baton **baton)
21629 {
21630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21631 struct comp_unit_head *cu_header = &cu->header;
21632 struct dwarf_block *blk;
21633 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21634 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21635
21636 *value = 0;
21637 *bytes = NULL;
21638 *baton = NULL;
21639
21640 switch (attr->form)
21641 {
21642 case DW_FORM_addr:
21643 case DW_FORM_addrx:
21644 case DW_FORM_GNU_addr_index:
21645 {
21646 gdb_byte *data;
21647
21648 if (TYPE_LENGTH (type) != cu_header->addr_size)
21649 dwarf2_const_value_length_mismatch_complaint (name,
21650 cu_header->addr_size,
21651 TYPE_LENGTH (type));
21652 /* Symbols of this form are reasonably rare, so we just
21653 piggyback on the existing location code rather than writing
21654 a new implementation of symbol_computed_ops. */
21655 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21656 (*baton)->per_cu = cu->per_cu;
21657 gdb_assert ((*baton)->per_cu);
21658
21659 (*baton)->size = 2 + cu_header->addr_size;
21660 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21661 (*baton)->data = data;
21662
21663 data[0] = DW_OP_addr;
21664 store_unsigned_integer (&data[1], cu_header->addr_size,
21665 byte_order, DW_ADDR (attr));
21666 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21667 }
21668 break;
21669 case DW_FORM_string:
21670 case DW_FORM_strp:
21671 case DW_FORM_strx:
21672 case DW_FORM_GNU_str_index:
21673 case DW_FORM_GNU_strp_alt:
21674 /* DW_STRING is already allocated on the objfile obstack, point
21675 directly to it. */
21676 *bytes = (const gdb_byte *) DW_STRING (attr);
21677 break;
21678 case DW_FORM_block1:
21679 case DW_FORM_block2:
21680 case DW_FORM_block4:
21681 case DW_FORM_block:
21682 case DW_FORM_exprloc:
21683 case DW_FORM_data16:
21684 blk = DW_BLOCK (attr);
21685 if (TYPE_LENGTH (type) != blk->size)
21686 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21687 TYPE_LENGTH (type));
21688 *bytes = blk->data;
21689 break;
21690
21691 /* The DW_AT_const_value attributes are supposed to carry the
21692 symbol's value "represented as it would be on the target
21693 architecture." By the time we get here, it's already been
21694 converted to host endianness, so we just need to sign- or
21695 zero-extend it as appropriate. */
21696 case DW_FORM_data1:
21697 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21698 break;
21699 case DW_FORM_data2:
21700 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21701 break;
21702 case DW_FORM_data4:
21703 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21704 break;
21705 case DW_FORM_data8:
21706 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21707 break;
21708
21709 case DW_FORM_sdata:
21710 case DW_FORM_implicit_const:
21711 *value = DW_SND (attr);
21712 break;
21713
21714 case DW_FORM_udata:
21715 *value = DW_UNSND (attr);
21716 break;
21717
21718 default:
21719 complaint (_("unsupported const value attribute form: '%s'"),
21720 dwarf_form_name (attr->form));
21721 *value = 0;
21722 break;
21723 }
21724 }
21725
21726
21727 /* Copy constant value from an attribute to a symbol. */
21728
21729 static void
21730 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21731 struct dwarf2_cu *cu)
21732 {
21733 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21734 LONGEST value;
21735 const gdb_byte *bytes;
21736 struct dwarf2_locexpr_baton *baton;
21737
21738 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21739 sym->print_name (),
21740 &objfile->objfile_obstack, cu,
21741 &value, &bytes, &baton);
21742
21743 if (baton != NULL)
21744 {
21745 SYMBOL_LOCATION_BATON (sym) = baton;
21746 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21747 }
21748 else if (bytes != NULL)
21749 {
21750 SYMBOL_VALUE_BYTES (sym) = bytes;
21751 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21752 }
21753 else
21754 {
21755 SYMBOL_VALUE (sym) = value;
21756 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21757 }
21758 }
21759
21760 /* Return the type of the die in question using its DW_AT_type attribute. */
21761
21762 static struct type *
21763 die_type (struct die_info *die, struct dwarf2_cu *cu)
21764 {
21765 struct attribute *type_attr;
21766
21767 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21768 if (!type_attr)
21769 {
21770 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21771 /* A missing DW_AT_type represents a void type. */
21772 return objfile_type (objfile)->builtin_void;
21773 }
21774
21775 return lookup_die_type (die, type_attr, cu);
21776 }
21777
21778 /* True iff CU's producer generates GNAT Ada auxiliary information
21779 that allows to find parallel types through that information instead
21780 of having to do expensive parallel lookups by type name. */
21781
21782 static int
21783 need_gnat_info (struct dwarf2_cu *cu)
21784 {
21785 /* Assume that the Ada compiler was GNAT, which always produces
21786 the auxiliary information. */
21787 return (cu->language == language_ada);
21788 }
21789
21790 /* Return the auxiliary type of the die in question using its
21791 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21792 attribute is not present. */
21793
21794 static struct type *
21795 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21796 {
21797 struct attribute *type_attr;
21798
21799 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21800 if (!type_attr)
21801 return NULL;
21802
21803 return lookup_die_type (die, type_attr, cu);
21804 }
21805
21806 /* If DIE has a descriptive_type attribute, then set the TYPE's
21807 descriptive type accordingly. */
21808
21809 static void
21810 set_descriptive_type (struct type *type, struct die_info *die,
21811 struct dwarf2_cu *cu)
21812 {
21813 struct type *descriptive_type = die_descriptive_type (die, cu);
21814
21815 if (descriptive_type)
21816 {
21817 ALLOCATE_GNAT_AUX_TYPE (type);
21818 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21819 }
21820 }
21821
21822 /* Return the containing type of the die in question using its
21823 DW_AT_containing_type attribute. */
21824
21825 static struct type *
21826 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21827 {
21828 struct attribute *type_attr;
21829 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21830
21831 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21832 if (!type_attr)
21833 error (_("Dwarf Error: Problem turning containing type into gdb type "
21834 "[in module %s]"), objfile_name (objfile));
21835
21836 return lookup_die_type (die, type_attr, cu);
21837 }
21838
21839 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21840
21841 static struct type *
21842 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21843 {
21844 struct dwarf2_per_objfile *dwarf2_per_objfile
21845 = cu->per_cu->dwarf2_per_objfile;
21846 struct objfile *objfile = dwarf2_per_objfile->objfile;
21847 char *saved;
21848
21849 std::string message
21850 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21851 objfile_name (objfile),
21852 sect_offset_str (cu->header.sect_off),
21853 sect_offset_str (die->sect_off));
21854 saved = obstack_strdup (&objfile->objfile_obstack, message);
21855
21856 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21857 }
21858
21859 /* Look up the type of DIE in CU using its type attribute ATTR.
21860 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21861 DW_AT_containing_type.
21862 If there is no type substitute an error marker. */
21863
21864 static struct type *
21865 lookup_die_type (struct die_info *die, const struct attribute *attr,
21866 struct dwarf2_cu *cu)
21867 {
21868 struct dwarf2_per_objfile *dwarf2_per_objfile
21869 = cu->per_cu->dwarf2_per_objfile;
21870 struct objfile *objfile = dwarf2_per_objfile->objfile;
21871 struct type *this_type;
21872
21873 gdb_assert (attr->name == DW_AT_type
21874 || attr->name == DW_AT_GNAT_descriptive_type
21875 || attr->name == DW_AT_containing_type);
21876
21877 /* First see if we have it cached. */
21878
21879 if (attr->form == DW_FORM_GNU_ref_alt)
21880 {
21881 struct dwarf2_per_cu_data *per_cu;
21882 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21883
21884 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21885 dwarf2_per_objfile);
21886 this_type = get_die_type_at_offset (sect_off, per_cu);
21887 }
21888 else if (attr->form_is_ref ())
21889 {
21890 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21891
21892 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21893 }
21894 else if (attr->form == DW_FORM_ref_sig8)
21895 {
21896 ULONGEST signature = DW_SIGNATURE (attr);
21897
21898 return get_signatured_type (die, signature, cu);
21899 }
21900 else
21901 {
21902 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21903 " at %s [in module %s]"),
21904 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21905 objfile_name (objfile));
21906 return build_error_marker_type (cu, die);
21907 }
21908
21909 /* If not cached we need to read it in. */
21910
21911 if (this_type == NULL)
21912 {
21913 struct die_info *type_die = NULL;
21914 struct dwarf2_cu *type_cu = cu;
21915
21916 if (attr->form_is_ref ())
21917 type_die = follow_die_ref (die, attr, &type_cu);
21918 if (type_die == NULL)
21919 return build_error_marker_type (cu, die);
21920 /* If we find the type now, it's probably because the type came
21921 from an inter-CU reference and the type's CU got expanded before
21922 ours. */
21923 this_type = read_type_die (type_die, type_cu);
21924 }
21925
21926 /* If we still don't have a type use an error marker. */
21927
21928 if (this_type == NULL)
21929 return build_error_marker_type (cu, die);
21930
21931 return this_type;
21932 }
21933
21934 /* Return the type in DIE, CU.
21935 Returns NULL for invalid types.
21936
21937 This first does a lookup in die_type_hash,
21938 and only reads the die in if necessary.
21939
21940 NOTE: This can be called when reading in partial or full symbols. */
21941
21942 static struct type *
21943 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21944 {
21945 struct type *this_type;
21946
21947 this_type = get_die_type (die, cu);
21948 if (this_type)
21949 return this_type;
21950
21951 return read_type_die_1 (die, cu);
21952 }
21953
21954 /* Read the type in DIE, CU.
21955 Returns NULL for invalid types. */
21956
21957 static struct type *
21958 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21959 {
21960 struct type *this_type = NULL;
21961
21962 switch (die->tag)
21963 {
21964 case DW_TAG_class_type:
21965 case DW_TAG_interface_type:
21966 case DW_TAG_structure_type:
21967 case DW_TAG_union_type:
21968 this_type = read_structure_type (die, cu);
21969 break;
21970 case DW_TAG_enumeration_type:
21971 this_type = read_enumeration_type (die, cu);
21972 break;
21973 case DW_TAG_subprogram:
21974 case DW_TAG_subroutine_type:
21975 case DW_TAG_inlined_subroutine:
21976 this_type = read_subroutine_type (die, cu);
21977 break;
21978 case DW_TAG_array_type:
21979 this_type = read_array_type (die, cu);
21980 break;
21981 case DW_TAG_set_type:
21982 this_type = read_set_type (die, cu);
21983 break;
21984 case DW_TAG_pointer_type:
21985 this_type = read_tag_pointer_type (die, cu);
21986 break;
21987 case DW_TAG_ptr_to_member_type:
21988 this_type = read_tag_ptr_to_member_type (die, cu);
21989 break;
21990 case DW_TAG_reference_type:
21991 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21992 break;
21993 case DW_TAG_rvalue_reference_type:
21994 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21995 break;
21996 case DW_TAG_const_type:
21997 this_type = read_tag_const_type (die, cu);
21998 break;
21999 case DW_TAG_volatile_type:
22000 this_type = read_tag_volatile_type (die, cu);
22001 break;
22002 case DW_TAG_restrict_type:
22003 this_type = read_tag_restrict_type (die, cu);
22004 break;
22005 case DW_TAG_string_type:
22006 this_type = read_tag_string_type (die, cu);
22007 break;
22008 case DW_TAG_typedef:
22009 this_type = read_typedef (die, cu);
22010 break;
22011 case DW_TAG_subrange_type:
22012 this_type = read_subrange_type (die, cu);
22013 break;
22014 case DW_TAG_base_type:
22015 this_type = read_base_type (die, cu);
22016 break;
22017 case DW_TAG_unspecified_type:
22018 this_type = read_unspecified_type (die, cu);
22019 break;
22020 case DW_TAG_namespace:
22021 this_type = read_namespace_type (die, cu);
22022 break;
22023 case DW_TAG_module:
22024 this_type = read_module_type (die, cu);
22025 break;
22026 case DW_TAG_atomic_type:
22027 this_type = read_tag_atomic_type (die, cu);
22028 break;
22029 default:
22030 complaint (_("unexpected tag in read_type_die: '%s'"),
22031 dwarf_tag_name (die->tag));
22032 break;
22033 }
22034
22035 return this_type;
22036 }
22037
22038 /* See if we can figure out if the class lives in a namespace. We do
22039 this by looking for a member function; its demangled name will
22040 contain namespace info, if there is any.
22041 Return the computed name or NULL.
22042 Space for the result is allocated on the objfile's obstack.
22043 This is the full-die version of guess_partial_die_structure_name.
22044 In this case we know DIE has no useful parent. */
22045
22046 static const char *
22047 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22048 {
22049 struct die_info *spec_die;
22050 struct dwarf2_cu *spec_cu;
22051 struct die_info *child;
22052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22053
22054 spec_cu = cu;
22055 spec_die = die_specification (die, &spec_cu);
22056 if (spec_die != NULL)
22057 {
22058 die = spec_die;
22059 cu = spec_cu;
22060 }
22061
22062 for (child = die->child;
22063 child != NULL;
22064 child = child->sibling)
22065 {
22066 if (child->tag == DW_TAG_subprogram)
22067 {
22068 const char *linkage_name = dw2_linkage_name (child, cu);
22069
22070 if (linkage_name != NULL)
22071 {
22072 gdb::unique_xmalloc_ptr<char> actual_name
22073 (language_class_name_from_physname (cu->language_defn,
22074 linkage_name));
22075 const char *name = NULL;
22076
22077 if (actual_name != NULL)
22078 {
22079 const char *die_name = dwarf2_name (die, cu);
22080
22081 if (die_name != NULL
22082 && strcmp (die_name, actual_name.get ()) != 0)
22083 {
22084 /* Strip off the class name from the full name.
22085 We want the prefix. */
22086 int die_name_len = strlen (die_name);
22087 int actual_name_len = strlen (actual_name.get ());
22088 const char *ptr = actual_name.get ();
22089
22090 /* Test for '::' as a sanity check. */
22091 if (actual_name_len > die_name_len + 2
22092 && ptr[actual_name_len - die_name_len - 1] == ':')
22093 name = obstack_strndup (
22094 &objfile->per_bfd->storage_obstack,
22095 ptr, actual_name_len - die_name_len - 2);
22096 }
22097 }
22098 return name;
22099 }
22100 }
22101 }
22102
22103 return NULL;
22104 }
22105
22106 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22107 prefix part in such case. See
22108 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22109
22110 static const char *
22111 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22112 {
22113 struct attribute *attr;
22114 const char *base;
22115
22116 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22117 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22118 return NULL;
22119
22120 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22121 return NULL;
22122
22123 attr = dw2_linkage_name_attr (die, cu);
22124 if (attr == NULL || DW_STRING (attr) == NULL)
22125 return NULL;
22126
22127 /* dwarf2_name had to be already called. */
22128 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22129
22130 /* Strip the base name, keep any leading namespaces/classes. */
22131 base = strrchr (DW_STRING (attr), ':');
22132 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22133 return "";
22134
22135 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22136 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22137 DW_STRING (attr),
22138 &base[-1] - DW_STRING (attr));
22139 }
22140
22141 /* Return the name of the namespace/class that DIE is defined within,
22142 or "" if we can't tell. The caller should not xfree the result.
22143
22144 For example, if we're within the method foo() in the following
22145 code:
22146
22147 namespace N {
22148 class C {
22149 void foo () {
22150 }
22151 };
22152 }
22153
22154 then determine_prefix on foo's die will return "N::C". */
22155
22156 static const char *
22157 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22158 {
22159 struct dwarf2_per_objfile *dwarf2_per_objfile
22160 = cu->per_cu->dwarf2_per_objfile;
22161 struct die_info *parent, *spec_die;
22162 struct dwarf2_cu *spec_cu;
22163 struct type *parent_type;
22164 const char *retval;
22165
22166 if (cu->language != language_cplus
22167 && cu->language != language_fortran && cu->language != language_d
22168 && cu->language != language_rust)
22169 return "";
22170
22171 retval = anonymous_struct_prefix (die, cu);
22172 if (retval)
22173 return retval;
22174
22175 /* We have to be careful in the presence of DW_AT_specification.
22176 For example, with GCC 3.4, given the code
22177
22178 namespace N {
22179 void foo() {
22180 // Definition of N::foo.
22181 }
22182 }
22183
22184 then we'll have a tree of DIEs like this:
22185
22186 1: DW_TAG_compile_unit
22187 2: DW_TAG_namespace // N
22188 3: DW_TAG_subprogram // declaration of N::foo
22189 4: DW_TAG_subprogram // definition of N::foo
22190 DW_AT_specification // refers to die #3
22191
22192 Thus, when processing die #4, we have to pretend that we're in
22193 the context of its DW_AT_specification, namely the contex of die
22194 #3. */
22195 spec_cu = cu;
22196 spec_die = die_specification (die, &spec_cu);
22197 if (spec_die == NULL)
22198 parent = die->parent;
22199 else
22200 {
22201 parent = spec_die->parent;
22202 cu = spec_cu;
22203 }
22204
22205 if (parent == NULL)
22206 return "";
22207 else if (parent->building_fullname)
22208 {
22209 const char *name;
22210 const char *parent_name;
22211
22212 /* It has been seen on RealView 2.2 built binaries,
22213 DW_TAG_template_type_param types actually _defined_ as
22214 children of the parent class:
22215
22216 enum E {};
22217 template class <class Enum> Class{};
22218 Class<enum E> class_e;
22219
22220 1: DW_TAG_class_type (Class)
22221 2: DW_TAG_enumeration_type (E)
22222 3: DW_TAG_enumerator (enum1:0)
22223 3: DW_TAG_enumerator (enum2:1)
22224 ...
22225 2: DW_TAG_template_type_param
22226 DW_AT_type DW_FORM_ref_udata (E)
22227
22228 Besides being broken debug info, it can put GDB into an
22229 infinite loop. Consider:
22230
22231 When we're building the full name for Class<E>, we'll start
22232 at Class, and go look over its template type parameters,
22233 finding E. We'll then try to build the full name of E, and
22234 reach here. We're now trying to build the full name of E,
22235 and look over the parent DIE for containing scope. In the
22236 broken case, if we followed the parent DIE of E, we'd again
22237 find Class, and once again go look at its template type
22238 arguments, etc., etc. Simply don't consider such parent die
22239 as source-level parent of this die (it can't be, the language
22240 doesn't allow it), and break the loop here. */
22241 name = dwarf2_name (die, cu);
22242 parent_name = dwarf2_name (parent, cu);
22243 complaint (_("template param type '%s' defined within parent '%s'"),
22244 name ? name : "<unknown>",
22245 parent_name ? parent_name : "<unknown>");
22246 return "";
22247 }
22248 else
22249 switch (parent->tag)
22250 {
22251 case DW_TAG_namespace:
22252 parent_type = read_type_die (parent, cu);
22253 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22254 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22255 Work around this problem here. */
22256 if (cu->language == language_cplus
22257 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22258 return "";
22259 /* We give a name to even anonymous namespaces. */
22260 return TYPE_NAME (parent_type);
22261 case DW_TAG_class_type:
22262 case DW_TAG_interface_type:
22263 case DW_TAG_structure_type:
22264 case DW_TAG_union_type:
22265 case DW_TAG_module:
22266 parent_type = read_type_die (parent, cu);
22267 if (TYPE_NAME (parent_type) != NULL)
22268 return TYPE_NAME (parent_type);
22269 else
22270 /* An anonymous structure is only allowed non-static data
22271 members; no typedefs, no member functions, et cetera.
22272 So it does not need a prefix. */
22273 return "";
22274 case DW_TAG_compile_unit:
22275 case DW_TAG_partial_unit:
22276 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22277 if (cu->language == language_cplus
22278 && !dwarf2_per_objfile->types.empty ()
22279 && die->child != NULL
22280 && (die->tag == DW_TAG_class_type
22281 || die->tag == DW_TAG_structure_type
22282 || die->tag == DW_TAG_union_type))
22283 {
22284 const char *name = guess_full_die_structure_name (die, cu);
22285 if (name != NULL)
22286 return name;
22287 }
22288 return "";
22289 case DW_TAG_subprogram:
22290 /* Nested subroutines in Fortran get a prefix with the name
22291 of the parent's subroutine. */
22292 if (cu->language == language_fortran)
22293 {
22294 if ((die->tag == DW_TAG_subprogram)
22295 && (dwarf2_name (parent, cu) != NULL))
22296 return dwarf2_name (parent, cu);
22297 }
22298 return determine_prefix (parent, cu);
22299 case DW_TAG_enumeration_type:
22300 parent_type = read_type_die (parent, cu);
22301 if (TYPE_DECLARED_CLASS (parent_type))
22302 {
22303 if (TYPE_NAME (parent_type) != NULL)
22304 return TYPE_NAME (parent_type);
22305 return "";
22306 }
22307 /* Fall through. */
22308 default:
22309 return determine_prefix (parent, cu);
22310 }
22311 }
22312
22313 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22314 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22315 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22316 an obconcat, otherwise allocate storage for the result. The CU argument is
22317 used to determine the language and hence, the appropriate separator. */
22318
22319 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22320
22321 static char *
22322 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22323 int physname, struct dwarf2_cu *cu)
22324 {
22325 const char *lead = "";
22326 const char *sep;
22327
22328 if (suffix == NULL || suffix[0] == '\0'
22329 || prefix == NULL || prefix[0] == '\0')
22330 sep = "";
22331 else if (cu->language == language_d)
22332 {
22333 /* For D, the 'main' function could be defined in any module, but it
22334 should never be prefixed. */
22335 if (strcmp (suffix, "D main") == 0)
22336 {
22337 prefix = "";
22338 sep = "";
22339 }
22340 else
22341 sep = ".";
22342 }
22343 else if (cu->language == language_fortran && physname)
22344 {
22345 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22346 DW_AT_MIPS_linkage_name is preferred and used instead. */
22347
22348 lead = "__";
22349 sep = "_MOD_";
22350 }
22351 else
22352 sep = "::";
22353
22354 if (prefix == NULL)
22355 prefix = "";
22356 if (suffix == NULL)
22357 suffix = "";
22358
22359 if (obs == NULL)
22360 {
22361 char *retval
22362 = ((char *)
22363 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22364
22365 strcpy (retval, lead);
22366 strcat (retval, prefix);
22367 strcat (retval, sep);
22368 strcat (retval, suffix);
22369 return retval;
22370 }
22371 else
22372 {
22373 /* We have an obstack. */
22374 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22375 }
22376 }
22377
22378 /* Return sibling of die, NULL if no sibling. */
22379
22380 static struct die_info *
22381 sibling_die (struct die_info *die)
22382 {
22383 return die->sibling;
22384 }
22385
22386 /* Get name of a die, return NULL if not found. */
22387
22388 static const char *
22389 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22390 struct obstack *obstack)
22391 {
22392 if (name && cu->language == language_cplus)
22393 {
22394 std::string canon_name = cp_canonicalize_string (name);
22395
22396 if (!canon_name.empty ())
22397 {
22398 if (canon_name != name)
22399 name = obstack_strdup (obstack, canon_name);
22400 }
22401 }
22402
22403 return name;
22404 }
22405
22406 /* Get name of a die, return NULL if not found.
22407 Anonymous namespaces are converted to their magic string. */
22408
22409 static const char *
22410 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22411 {
22412 struct attribute *attr;
22413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22414
22415 attr = dwarf2_attr (die, DW_AT_name, cu);
22416 if ((!attr || !DW_STRING (attr))
22417 && die->tag != DW_TAG_namespace
22418 && die->tag != DW_TAG_class_type
22419 && die->tag != DW_TAG_interface_type
22420 && die->tag != DW_TAG_structure_type
22421 && die->tag != DW_TAG_union_type)
22422 return NULL;
22423
22424 switch (die->tag)
22425 {
22426 case DW_TAG_compile_unit:
22427 case DW_TAG_partial_unit:
22428 /* Compilation units have a DW_AT_name that is a filename, not
22429 a source language identifier. */
22430 case DW_TAG_enumeration_type:
22431 case DW_TAG_enumerator:
22432 /* These tags always have simple identifiers already; no need
22433 to canonicalize them. */
22434 return DW_STRING (attr);
22435
22436 case DW_TAG_namespace:
22437 if (attr != NULL && DW_STRING (attr) != NULL)
22438 return DW_STRING (attr);
22439 return CP_ANONYMOUS_NAMESPACE_STR;
22440
22441 case DW_TAG_class_type:
22442 case DW_TAG_interface_type:
22443 case DW_TAG_structure_type:
22444 case DW_TAG_union_type:
22445 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22446 structures or unions. These were of the form "._%d" in GCC 4.1,
22447 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22448 and GCC 4.4. We work around this problem by ignoring these. */
22449 if (attr && DW_STRING (attr)
22450 && (startswith (DW_STRING (attr), "._")
22451 || startswith (DW_STRING (attr), "<anonymous")))
22452 return NULL;
22453
22454 /* GCC might emit a nameless typedef that has a linkage name. See
22455 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22456 if (!attr || DW_STRING (attr) == NULL)
22457 {
22458 attr = dw2_linkage_name_attr (die, cu);
22459 if (attr == NULL || DW_STRING (attr) == NULL)
22460 return NULL;
22461
22462 /* Avoid demangling DW_STRING (attr) the second time on a second
22463 call for the same DIE. */
22464 if (!DW_STRING_IS_CANONICAL (attr))
22465 {
22466 gdb::unique_xmalloc_ptr<char> demangled
22467 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22468
22469 const char *base;
22470
22471 /* FIXME: we already did this for the partial symbol... */
22472 DW_STRING (attr)
22473 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22474 demangled.get ());
22475 DW_STRING_IS_CANONICAL (attr) = 1;
22476
22477 /* Strip any leading namespaces/classes, keep only the base name.
22478 DW_AT_name for named DIEs does not contain the prefixes. */
22479 base = strrchr (DW_STRING (attr), ':');
22480 if (base && base > DW_STRING (attr) && base[-1] == ':')
22481 return &base[1];
22482 else
22483 return DW_STRING (attr);
22484 }
22485 }
22486 break;
22487
22488 default:
22489 break;
22490 }
22491
22492 if (!DW_STRING_IS_CANONICAL (attr))
22493 {
22494 DW_STRING (attr)
22495 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22496 &objfile->per_bfd->storage_obstack);
22497 DW_STRING_IS_CANONICAL (attr) = 1;
22498 }
22499 return DW_STRING (attr);
22500 }
22501
22502 /* Return the die that this die in an extension of, or NULL if there
22503 is none. *EXT_CU is the CU containing DIE on input, and the CU
22504 containing the return value on output. */
22505
22506 static struct die_info *
22507 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22508 {
22509 struct attribute *attr;
22510
22511 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22512 if (attr == NULL)
22513 return NULL;
22514
22515 return follow_die_ref (die, attr, ext_cu);
22516 }
22517
22518 /* A convenience function that returns an "unknown" DWARF name,
22519 including the value of V. STR is the name of the entity being
22520 printed, e.g., "TAG". */
22521
22522 static const char *
22523 dwarf_unknown (const char *str, unsigned v)
22524 {
22525 char *cell = get_print_cell ();
22526 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22527 return cell;
22528 }
22529
22530 /* Convert a DIE tag into its string name. */
22531
22532 static const char *
22533 dwarf_tag_name (unsigned tag)
22534 {
22535 const char *name = get_DW_TAG_name (tag);
22536
22537 if (name == NULL)
22538 return dwarf_unknown ("TAG", tag);
22539
22540 return name;
22541 }
22542
22543 /* Convert a DWARF attribute code into its string name. */
22544
22545 static const char *
22546 dwarf_attr_name (unsigned attr)
22547 {
22548 const char *name;
22549
22550 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22551 if (attr == DW_AT_MIPS_fde)
22552 return "DW_AT_MIPS_fde";
22553 #else
22554 if (attr == DW_AT_HP_block_index)
22555 return "DW_AT_HP_block_index";
22556 #endif
22557
22558 name = get_DW_AT_name (attr);
22559
22560 if (name == NULL)
22561 return dwarf_unknown ("AT", attr);
22562
22563 return name;
22564 }
22565
22566 /* Convert a unit type to corresponding DW_UT name. */
22567
22568 static const char *
22569 dwarf_unit_type_name (int unit_type) {
22570 switch (unit_type)
22571 {
22572 case 0x01:
22573 return "DW_UT_compile (0x01)";
22574 case 0x02:
22575 return "DW_UT_type (0x02)";
22576 case 0x03:
22577 return "DW_UT_partial (0x03)";
22578 case 0x04:
22579 return "DW_UT_skeleton (0x04)";
22580 case 0x05:
22581 return "DW_UT_split_compile (0x05)";
22582 case 0x06:
22583 return "DW_UT_split_type (0x06)";
22584 case 0x80:
22585 return "DW_UT_lo_user (0x80)";
22586 case 0xff:
22587 return "DW_UT_hi_user (0xff)";
22588 default:
22589 return nullptr;
22590 }
22591 }
22592
22593 /* Convert a DWARF value form code into its string name. */
22594
22595 static const char *
22596 dwarf_form_name (unsigned form)
22597 {
22598 const char *name = get_DW_FORM_name (form);
22599
22600 if (name == NULL)
22601 return dwarf_unknown ("FORM", form);
22602
22603 return name;
22604 }
22605
22606 static const char *
22607 dwarf_bool_name (unsigned mybool)
22608 {
22609 if (mybool)
22610 return "TRUE";
22611 else
22612 return "FALSE";
22613 }
22614
22615 /* Convert a DWARF type code into its string name. */
22616
22617 static const char *
22618 dwarf_type_encoding_name (unsigned enc)
22619 {
22620 const char *name = get_DW_ATE_name (enc);
22621
22622 if (name == NULL)
22623 return dwarf_unknown ("ATE", enc);
22624
22625 return name;
22626 }
22627
22628 static void
22629 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22630 {
22631 unsigned int i;
22632
22633 print_spaces (indent, f);
22634 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22635 dwarf_tag_name (die->tag), die->abbrev,
22636 sect_offset_str (die->sect_off));
22637
22638 if (die->parent != NULL)
22639 {
22640 print_spaces (indent, f);
22641 fprintf_unfiltered (f, " parent at offset: %s\n",
22642 sect_offset_str (die->parent->sect_off));
22643 }
22644
22645 print_spaces (indent, f);
22646 fprintf_unfiltered (f, " has children: %s\n",
22647 dwarf_bool_name (die->child != NULL));
22648
22649 print_spaces (indent, f);
22650 fprintf_unfiltered (f, " attributes:\n");
22651
22652 for (i = 0; i < die->num_attrs; ++i)
22653 {
22654 print_spaces (indent, f);
22655 fprintf_unfiltered (f, " %s (%s) ",
22656 dwarf_attr_name (die->attrs[i].name),
22657 dwarf_form_name (die->attrs[i].form));
22658
22659 switch (die->attrs[i].form)
22660 {
22661 case DW_FORM_addr:
22662 case DW_FORM_addrx:
22663 case DW_FORM_GNU_addr_index:
22664 fprintf_unfiltered (f, "address: ");
22665 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22666 break;
22667 case DW_FORM_block2:
22668 case DW_FORM_block4:
22669 case DW_FORM_block:
22670 case DW_FORM_block1:
22671 fprintf_unfiltered (f, "block: size %s",
22672 pulongest (DW_BLOCK (&die->attrs[i])->size));
22673 break;
22674 case DW_FORM_exprloc:
22675 fprintf_unfiltered (f, "expression: size %s",
22676 pulongest (DW_BLOCK (&die->attrs[i])->size));
22677 break;
22678 case DW_FORM_data16:
22679 fprintf_unfiltered (f, "constant of 16 bytes");
22680 break;
22681 case DW_FORM_ref_addr:
22682 fprintf_unfiltered (f, "ref address: ");
22683 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22684 break;
22685 case DW_FORM_GNU_ref_alt:
22686 fprintf_unfiltered (f, "alt ref address: ");
22687 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22688 break;
22689 case DW_FORM_ref1:
22690 case DW_FORM_ref2:
22691 case DW_FORM_ref4:
22692 case DW_FORM_ref8:
22693 case DW_FORM_ref_udata:
22694 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22695 (long) (DW_UNSND (&die->attrs[i])));
22696 break;
22697 case DW_FORM_data1:
22698 case DW_FORM_data2:
22699 case DW_FORM_data4:
22700 case DW_FORM_data8:
22701 case DW_FORM_udata:
22702 case DW_FORM_sdata:
22703 fprintf_unfiltered (f, "constant: %s",
22704 pulongest (DW_UNSND (&die->attrs[i])));
22705 break;
22706 case DW_FORM_sec_offset:
22707 fprintf_unfiltered (f, "section offset: %s",
22708 pulongest (DW_UNSND (&die->attrs[i])));
22709 break;
22710 case DW_FORM_ref_sig8:
22711 fprintf_unfiltered (f, "signature: %s",
22712 hex_string (DW_SIGNATURE (&die->attrs[i])));
22713 break;
22714 case DW_FORM_string:
22715 case DW_FORM_strp:
22716 case DW_FORM_line_strp:
22717 case DW_FORM_strx:
22718 case DW_FORM_GNU_str_index:
22719 case DW_FORM_GNU_strp_alt:
22720 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22721 DW_STRING (&die->attrs[i])
22722 ? DW_STRING (&die->attrs[i]) : "",
22723 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22724 break;
22725 case DW_FORM_flag:
22726 if (DW_UNSND (&die->attrs[i]))
22727 fprintf_unfiltered (f, "flag: TRUE");
22728 else
22729 fprintf_unfiltered (f, "flag: FALSE");
22730 break;
22731 case DW_FORM_flag_present:
22732 fprintf_unfiltered (f, "flag: TRUE");
22733 break;
22734 case DW_FORM_indirect:
22735 /* The reader will have reduced the indirect form to
22736 the "base form" so this form should not occur. */
22737 fprintf_unfiltered (f,
22738 "unexpected attribute form: DW_FORM_indirect");
22739 break;
22740 case DW_FORM_implicit_const:
22741 fprintf_unfiltered (f, "constant: %s",
22742 plongest (DW_SND (&die->attrs[i])));
22743 break;
22744 default:
22745 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22746 die->attrs[i].form);
22747 break;
22748 }
22749 fprintf_unfiltered (f, "\n");
22750 }
22751 }
22752
22753 static void
22754 dump_die_for_error (struct die_info *die)
22755 {
22756 dump_die_shallow (gdb_stderr, 0, die);
22757 }
22758
22759 static void
22760 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22761 {
22762 int indent = level * 4;
22763
22764 gdb_assert (die != NULL);
22765
22766 if (level >= max_level)
22767 return;
22768
22769 dump_die_shallow (f, indent, die);
22770
22771 if (die->child != NULL)
22772 {
22773 print_spaces (indent, f);
22774 fprintf_unfiltered (f, " Children:");
22775 if (level + 1 < max_level)
22776 {
22777 fprintf_unfiltered (f, "\n");
22778 dump_die_1 (f, level + 1, max_level, die->child);
22779 }
22780 else
22781 {
22782 fprintf_unfiltered (f,
22783 " [not printed, max nesting level reached]\n");
22784 }
22785 }
22786
22787 if (die->sibling != NULL && level > 0)
22788 {
22789 dump_die_1 (f, level, max_level, die->sibling);
22790 }
22791 }
22792
22793 /* This is called from the pdie macro in gdbinit.in.
22794 It's not static so gcc will keep a copy callable from gdb. */
22795
22796 void
22797 dump_die (struct die_info *die, int max_level)
22798 {
22799 dump_die_1 (gdb_stdlog, 0, max_level, die);
22800 }
22801
22802 static void
22803 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22804 {
22805 void **slot;
22806
22807 slot = htab_find_slot_with_hash (cu->die_hash, die,
22808 to_underlying (die->sect_off),
22809 INSERT);
22810
22811 *slot = die;
22812 }
22813
22814 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22815 required kind. */
22816
22817 static sect_offset
22818 dwarf2_get_ref_die_offset (const struct attribute *attr)
22819 {
22820 if (attr->form_is_ref ())
22821 return (sect_offset) DW_UNSND (attr);
22822
22823 complaint (_("unsupported die ref attribute form: '%s'"),
22824 dwarf_form_name (attr->form));
22825 return {};
22826 }
22827
22828 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22829 * the value held by the attribute is not constant. */
22830
22831 static LONGEST
22832 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22833 {
22834 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22835 return DW_SND (attr);
22836 else if (attr->form == DW_FORM_udata
22837 || attr->form == DW_FORM_data1
22838 || attr->form == DW_FORM_data2
22839 || attr->form == DW_FORM_data4
22840 || attr->form == DW_FORM_data8)
22841 return DW_UNSND (attr);
22842 else
22843 {
22844 /* For DW_FORM_data16 see attribute::form_is_constant. */
22845 complaint (_("Attribute value is not a constant (%s)"),
22846 dwarf_form_name (attr->form));
22847 return default_value;
22848 }
22849 }
22850
22851 /* Follow reference or signature attribute ATTR of SRC_DIE.
22852 On entry *REF_CU is the CU of SRC_DIE.
22853 On exit *REF_CU is the CU of the result. */
22854
22855 static struct die_info *
22856 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22857 struct dwarf2_cu **ref_cu)
22858 {
22859 struct die_info *die;
22860
22861 if (attr->form_is_ref ())
22862 die = follow_die_ref (src_die, attr, ref_cu);
22863 else if (attr->form == DW_FORM_ref_sig8)
22864 die = follow_die_sig (src_die, attr, ref_cu);
22865 else
22866 {
22867 dump_die_for_error (src_die);
22868 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22869 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22870 }
22871
22872 return die;
22873 }
22874
22875 /* Follow reference OFFSET.
22876 On entry *REF_CU is the CU of the source die referencing OFFSET.
22877 On exit *REF_CU is the CU of the result.
22878 Returns NULL if OFFSET is invalid. */
22879
22880 static struct die_info *
22881 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22882 struct dwarf2_cu **ref_cu)
22883 {
22884 struct die_info temp_die;
22885 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22886 struct dwarf2_per_objfile *dwarf2_per_objfile
22887 = cu->per_cu->dwarf2_per_objfile;
22888
22889 gdb_assert (cu->per_cu != NULL);
22890
22891 target_cu = cu;
22892
22893 if (cu->per_cu->is_debug_types)
22894 {
22895 /* .debug_types CUs cannot reference anything outside their CU.
22896 If they need to, they have to reference a signatured type via
22897 DW_FORM_ref_sig8. */
22898 if (!offset_in_cu_p (&cu->header, sect_off))
22899 return NULL;
22900 }
22901 else if (offset_in_dwz != cu->per_cu->is_dwz
22902 || !offset_in_cu_p (&cu->header, sect_off))
22903 {
22904 struct dwarf2_per_cu_data *per_cu;
22905
22906 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22907 dwarf2_per_objfile);
22908
22909 /* If necessary, add it to the queue and load its DIEs. */
22910 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22911 load_full_comp_unit (per_cu, false, cu->language);
22912
22913 target_cu = per_cu->cu;
22914 }
22915 else if (cu->dies == NULL)
22916 {
22917 /* We're loading full DIEs during partial symbol reading. */
22918 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22919 load_full_comp_unit (cu->per_cu, false, language_minimal);
22920 }
22921
22922 *ref_cu = target_cu;
22923 temp_die.sect_off = sect_off;
22924
22925 if (target_cu != cu)
22926 target_cu->ancestor = cu;
22927
22928 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22929 &temp_die,
22930 to_underlying (sect_off));
22931 }
22932
22933 /* Follow reference attribute ATTR of SRC_DIE.
22934 On entry *REF_CU is the CU of SRC_DIE.
22935 On exit *REF_CU is the CU of the result. */
22936
22937 static struct die_info *
22938 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22939 struct dwarf2_cu **ref_cu)
22940 {
22941 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22942 struct dwarf2_cu *cu = *ref_cu;
22943 struct die_info *die;
22944
22945 die = follow_die_offset (sect_off,
22946 (attr->form == DW_FORM_GNU_ref_alt
22947 || cu->per_cu->is_dwz),
22948 ref_cu);
22949 if (!die)
22950 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22951 "at %s [in module %s]"),
22952 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22954
22955 return die;
22956 }
22957
22958 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22959 Returned value is intended for DW_OP_call*. Returned
22960 dwarf2_locexpr_baton->data has lifetime of
22961 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22962
22963 struct dwarf2_locexpr_baton
22964 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22965 struct dwarf2_per_cu_data *per_cu,
22966 CORE_ADDR (*get_frame_pc) (void *baton),
22967 void *baton, bool resolve_abstract_p)
22968 {
22969 struct dwarf2_cu *cu;
22970 struct die_info *die;
22971 struct attribute *attr;
22972 struct dwarf2_locexpr_baton retval;
22973 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22974 struct objfile *objfile = dwarf2_per_objfile->objfile;
22975
22976 if (per_cu->cu == NULL)
22977 load_cu (per_cu, false);
22978 cu = per_cu->cu;
22979 if (cu == NULL)
22980 {
22981 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22982 Instead just throw an error, not much else we can do. */
22983 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22984 sect_offset_str (sect_off), objfile_name (objfile));
22985 }
22986
22987 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22988 if (!die)
22989 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22990 sect_offset_str (sect_off), objfile_name (objfile));
22991
22992 attr = dwarf2_attr (die, DW_AT_location, cu);
22993 if (!attr && resolve_abstract_p
22994 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22995 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22996 {
22997 CORE_ADDR pc = (*get_frame_pc) (baton);
22998 CORE_ADDR baseaddr = objfile->text_section_offset ();
22999 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23000
23001 for (const auto &cand_off
23002 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23003 {
23004 struct dwarf2_cu *cand_cu = cu;
23005 struct die_info *cand
23006 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23007 if (!cand
23008 || !cand->parent
23009 || cand->parent->tag != DW_TAG_subprogram)
23010 continue;
23011
23012 CORE_ADDR pc_low, pc_high;
23013 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23014 if (pc_low == ((CORE_ADDR) -1))
23015 continue;
23016 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23017 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23018 if (!(pc_low <= pc && pc < pc_high))
23019 continue;
23020
23021 die = cand;
23022 attr = dwarf2_attr (die, DW_AT_location, cu);
23023 break;
23024 }
23025 }
23026
23027 if (!attr)
23028 {
23029 /* DWARF: "If there is no such attribute, then there is no effect.".
23030 DATA is ignored if SIZE is 0. */
23031
23032 retval.data = NULL;
23033 retval.size = 0;
23034 }
23035 else if (attr->form_is_section_offset ())
23036 {
23037 struct dwarf2_loclist_baton loclist_baton;
23038 CORE_ADDR pc = (*get_frame_pc) (baton);
23039 size_t size;
23040
23041 fill_in_loclist_baton (cu, &loclist_baton, attr);
23042
23043 retval.data = dwarf2_find_location_expression (&loclist_baton,
23044 &size, pc);
23045 retval.size = size;
23046 }
23047 else
23048 {
23049 if (!attr->form_is_block ())
23050 error (_("Dwarf Error: DIE at %s referenced in module %s "
23051 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23052 sect_offset_str (sect_off), objfile_name (objfile));
23053
23054 retval.data = DW_BLOCK (attr)->data;
23055 retval.size = DW_BLOCK (attr)->size;
23056 }
23057 retval.per_cu = cu->per_cu;
23058
23059 age_cached_comp_units (dwarf2_per_objfile);
23060
23061 return retval;
23062 }
23063
23064 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23065 offset. */
23066
23067 struct dwarf2_locexpr_baton
23068 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23069 struct dwarf2_per_cu_data *per_cu,
23070 CORE_ADDR (*get_frame_pc) (void *baton),
23071 void *baton)
23072 {
23073 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23074
23075 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23076 }
23077
23078 /* Write a constant of a given type as target-ordered bytes into
23079 OBSTACK. */
23080
23081 static const gdb_byte *
23082 write_constant_as_bytes (struct obstack *obstack,
23083 enum bfd_endian byte_order,
23084 struct type *type,
23085 ULONGEST value,
23086 LONGEST *len)
23087 {
23088 gdb_byte *result;
23089
23090 *len = TYPE_LENGTH (type);
23091 result = (gdb_byte *) obstack_alloc (obstack, *len);
23092 store_unsigned_integer (result, *len, byte_order, value);
23093
23094 return result;
23095 }
23096
23097 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23098 pointer to the constant bytes and set LEN to the length of the
23099 data. If memory is needed, allocate it on OBSTACK. If the DIE
23100 does not have a DW_AT_const_value, return NULL. */
23101
23102 const gdb_byte *
23103 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23104 struct dwarf2_per_cu_data *per_cu,
23105 struct obstack *obstack,
23106 LONGEST *len)
23107 {
23108 struct dwarf2_cu *cu;
23109 struct die_info *die;
23110 struct attribute *attr;
23111 const gdb_byte *result = NULL;
23112 struct type *type;
23113 LONGEST value;
23114 enum bfd_endian byte_order;
23115 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23116
23117 if (per_cu->cu == NULL)
23118 load_cu (per_cu, false);
23119 cu = per_cu->cu;
23120 if (cu == NULL)
23121 {
23122 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23123 Instead just throw an error, not much else we can do. */
23124 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23125 sect_offset_str (sect_off), objfile_name (objfile));
23126 }
23127
23128 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23129 if (!die)
23130 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23131 sect_offset_str (sect_off), objfile_name (objfile));
23132
23133 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23134 if (attr == NULL)
23135 return NULL;
23136
23137 byte_order = (bfd_big_endian (objfile->obfd)
23138 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23139
23140 switch (attr->form)
23141 {
23142 case DW_FORM_addr:
23143 case DW_FORM_addrx:
23144 case DW_FORM_GNU_addr_index:
23145 {
23146 gdb_byte *tem;
23147
23148 *len = cu->header.addr_size;
23149 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23150 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23151 result = tem;
23152 }
23153 break;
23154 case DW_FORM_string:
23155 case DW_FORM_strp:
23156 case DW_FORM_strx:
23157 case DW_FORM_GNU_str_index:
23158 case DW_FORM_GNU_strp_alt:
23159 /* DW_STRING is already allocated on the objfile obstack, point
23160 directly to it. */
23161 result = (const gdb_byte *) DW_STRING (attr);
23162 *len = strlen (DW_STRING (attr));
23163 break;
23164 case DW_FORM_block1:
23165 case DW_FORM_block2:
23166 case DW_FORM_block4:
23167 case DW_FORM_block:
23168 case DW_FORM_exprloc:
23169 case DW_FORM_data16:
23170 result = DW_BLOCK (attr)->data;
23171 *len = DW_BLOCK (attr)->size;
23172 break;
23173
23174 /* The DW_AT_const_value attributes are supposed to carry the
23175 symbol's value "represented as it would be on the target
23176 architecture." By the time we get here, it's already been
23177 converted to host endianness, so we just need to sign- or
23178 zero-extend it as appropriate. */
23179 case DW_FORM_data1:
23180 type = die_type (die, cu);
23181 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23182 if (result == NULL)
23183 result = write_constant_as_bytes (obstack, byte_order,
23184 type, value, len);
23185 break;
23186 case DW_FORM_data2:
23187 type = die_type (die, cu);
23188 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23189 if (result == NULL)
23190 result = write_constant_as_bytes (obstack, byte_order,
23191 type, value, len);
23192 break;
23193 case DW_FORM_data4:
23194 type = die_type (die, cu);
23195 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23196 if (result == NULL)
23197 result = write_constant_as_bytes (obstack, byte_order,
23198 type, value, len);
23199 break;
23200 case DW_FORM_data8:
23201 type = die_type (die, cu);
23202 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23203 if (result == NULL)
23204 result = write_constant_as_bytes (obstack, byte_order,
23205 type, value, len);
23206 break;
23207
23208 case DW_FORM_sdata:
23209 case DW_FORM_implicit_const:
23210 type = die_type (die, cu);
23211 result = write_constant_as_bytes (obstack, byte_order,
23212 type, DW_SND (attr), len);
23213 break;
23214
23215 case DW_FORM_udata:
23216 type = die_type (die, cu);
23217 result = write_constant_as_bytes (obstack, byte_order,
23218 type, DW_UNSND (attr), len);
23219 break;
23220
23221 default:
23222 complaint (_("unsupported const value attribute form: '%s'"),
23223 dwarf_form_name (attr->form));
23224 break;
23225 }
23226
23227 return result;
23228 }
23229
23230 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23231 valid type for this die is found. */
23232
23233 struct type *
23234 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23235 struct dwarf2_per_cu_data *per_cu)
23236 {
23237 struct dwarf2_cu *cu;
23238 struct die_info *die;
23239
23240 if (per_cu->cu == NULL)
23241 load_cu (per_cu, false);
23242 cu = per_cu->cu;
23243 if (!cu)
23244 return NULL;
23245
23246 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23247 if (!die)
23248 return NULL;
23249
23250 return die_type (die, cu);
23251 }
23252
23253 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23254 PER_CU. */
23255
23256 struct type *
23257 dwarf2_get_die_type (cu_offset die_offset,
23258 struct dwarf2_per_cu_data *per_cu)
23259 {
23260 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23261 return get_die_type_at_offset (die_offset_sect, per_cu);
23262 }
23263
23264 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23265 On entry *REF_CU is the CU of SRC_DIE.
23266 On exit *REF_CU is the CU of the result.
23267 Returns NULL if the referenced DIE isn't found. */
23268
23269 static struct die_info *
23270 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23271 struct dwarf2_cu **ref_cu)
23272 {
23273 struct die_info temp_die;
23274 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23275 struct die_info *die;
23276
23277 /* While it might be nice to assert sig_type->type == NULL here,
23278 we can get here for DW_AT_imported_declaration where we need
23279 the DIE not the type. */
23280
23281 /* If necessary, add it to the queue and load its DIEs. */
23282
23283 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23284 read_signatured_type (sig_type);
23285
23286 sig_cu = sig_type->per_cu.cu;
23287 gdb_assert (sig_cu != NULL);
23288 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23289 temp_die.sect_off = sig_type->type_offset_in_section;
23290 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23291 to_underlying (temp_die.sect_off));
23292 if (die)
23293 {
23294 struct dwarf2_per_objfile *dwarf2_per_objfile
23295 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23296
23297 /* For .gdb_index version 7 keep track of included TUs.
23298 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23299 if (dwarf2_per_objfile->index_table != NULL
23300 && dwarf2_per_objfile->index_table->version <= 7)
23301 {
23302 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23303 }
23304
23305 *ref_cu = sig_cu;
23306 if (sig_cu != cu)
23307 sig_cu->ancestor = cu;
23308
23309 return die;
23310 }
23311
23312 return NULL;
23313 }
23314
23315 /* Follow signatured type referenced by ATTR in SRC_DIE.
23316 On entry *REF_CU is the CU of SRC_DIE.
23317 On exit *REF_CU is the CU of the result.
23318 The result is the DIE of the type.
23319 If the referenced type cannot be found an error is thrown. */
23320
23321 static struct die_info *
23322 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23323 struct dwarf2_cu **ref_cu)
23324 {
23325 ULONGEST signature = DW_SIGNATURE (attr);
23326 struct signatured_type *sig_type;
23327 struct die_info *die;
23328
23329 gdb_assert (attr->form == DW_FORM_ref_sig8);
23330
23331 sig_type = lookup_signatured_type (*ref_cu, signature);
23332 /* sig_type will be NULL if the signatured type is missing from
23333 the debug info. */
23334 if (sig_type == NULL)
23335 {
23336 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23337 " from DIE at %s [in module %s]"),
23338 hex_string (signature), sect_offset_str (src_die->sect_off),
23339 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23340 }
23341
23342 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23343 if (die == NULL)
23344 {
23345 dump_die_for_error (src_die);
23346 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23347 " from DIE at %s [in module %s]"),
23348 hex_string (signature), sect_offset_str (src_die->sect_off),
23349 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23350 }
23351
23352 return die;
23353 }
23354
23355 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23356 reading in and processing the type unit if necessary. */
23357
23358 static struct type *
23359 get_signatured_type (struct die_info *die, ULONGEST signature,
23360 struct dwarf2_cu *cu)
23361 {
23362 struct dwarf2_per_objfile *dwarf2_per_objfile
23363 = cu->per_cu->dwarf2_per_objfile;
23364 struct signatured_type *sig_type;
23365 struct dwarf2_cu *type_cu;
23366 struct die_info *type_die;
23367 struct type *type;
23368
23369 sig_type = lookup_signatured_type (cu, signature);
23370 /* sig_type will be NULL if the signatured type is missing from
23371 the debug info. */
23372 if (sig_type == NULL)
23373 {
23374 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23375 " from DIE at %s [in module %s]"),
23376 hex_string (signature), sect_offset_str (die->sect_off),
23377 objfile_name (dwarf2_per_objfile->objfile));
23378 return build_error_marker_type (cu, die);
23379 }
23380
23381 /* If we already know the type we're done. */
23382 if (sig_type->type != NULL)
23383 return sig_type->type;
23384
23385 type_cu = cu;
23386 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23387 if (type_die != NULL)
23388 {
23389 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23390 is created. This is important, for example, because for c++ classes
23391 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23392 type = read_type_die (type_die, type_cu);
23393 if (type == NULL)
23394 {
23395 complaint (_("Dwarf Error: Cannot build signatured type %s"
23396 " referenced from DIE at %s [in module %s]"),
23397 hex_string (signature), sect_offset_str (die->sect_off),
23398 objfile_name (dwarf2_per_objfile->objfile));
23399 type = build_error_marker_type (cu, die);
23400 }
23401 }
23402 else
23403 {
23404 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23405 " from DIE at %s [in module %s]"),
23406 hex_string (signature), sect_offset_str (die->sect_off),
23407 objfile_name (dwarf2_per_objfile->objfile));
23408 type = build_error_marker_type (cu, die);
23409 }
23410 sig_type->type = type;
23411
23412 return type;
23413 }
23414
23415 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23416 reading in and processing the type unit if necessary. */
23417
23418 static struct type *
23419 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23420 struct dwarf2_cu *cu) /* ARI: editCase function */
23421 {
23422 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23423 if (attr->form_is_ref ())
23424 {
23425 struct dwarf2_cu *type_cu = cu;
23426 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23427
23428 return read_type_die (type_die, type_cu);
23429 }
23430 else if (attr->form == DW_FORM_ref_sig8)
23431 {
23432 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23433 }
23434 else
23435 {
23436 struct dwarf2_per_objfile *dwarf2_per_objfile
23437 = cu->per_cu->dwarf2_per_objfile;
23438
23439 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23440 " at %s [in module %s]"),
23441 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23442 objfile_name (dwarf2_per_objfile->objfile));
23443 return build_error_marker_type (cu, die);
23444 }
23445 }
23446
23447 /* Load the DIEs associated with type unit PER_CU into memory. */
23448
23449 static void
23450 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23451 {
23452 struct signatured_type *sig_type;
23453
23454 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23455 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23456
23457 /* We have the per_cu, but we need the signatured_type.
23458 Fortunately this is an easy translation. */
23459 gdb_assert (per_cu->is_debug_types);
23460 sig_type = (struct signatured_type *) per_cu;
23461
23462 gdb_assert (per_cu->cu == NULL);
23463
23464 read_signatured_type (sig_type);
23465
23466 gdb_assert (per_cu->cu != NULL);
23467 }
23468
23469 /* Read in a signatured type and build its CU and DIEs.
23470 If the type is a stub for the real type in a DWO file,
23471 read in the real type from the DWO file as well. */
23472
23473 static void
23474 read_signatured_type (struct signatured_type *sig_type)
23475 {
23476 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23477
23478 gdb_assert (per_cu->is_debug_types);
23479 gdb_assert (per_cu->cu == NULL);
23480
23481 cutu_reader reader (per_cu, NULL, 0, 1, false);
23482
23483 if (!reader.dummy_p)
23484 {
23485 struct dwarf2_cu *cu = reader.cu;
23486 const gdb_byte *info_ptr = reader.info_ptr;
23487
23488 gdb_assert (cu->die_hash == NULL);
23489 cu->die_hash =
23490 htab_create_alloc_ex (cu->header.length / 12,
23491 die_hash,
23492 die_eq,
23493 NULL,
23494 &cu->comp_unit_obstack,
23495 hashtab_obstack_allocate,
23496 dummy_obstack_deallocate);
23497
23498 if (reader.has_children)
23499 reader.comp_unit_die->child
23500 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23501 reader.comp_unit_die);
23502 cu->dies = reader.comp_unit_die;
23503 /* comp_unit_die is not stored in die_hash, no need. */
23504
23505 /* We try not to read any attributes in this function, because
23506 not all CUs needed for references have been loaded yet, and
23507 symbol table processing isn't initialized. But we have to
23508 set the CU language, or we won't be able to build types
23509 correctly. Similarly, if we do not read the producer, we can
23510 not apply producer-specific interpretation. */
23511 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23512 }
23513
23514 sig_type->per_cu.tu_read = 1;
23515 }
23516
23517 /* Decode simple location descriptions.
23518 Given a pointer to a dwarf block that defines a location, compute
23519 the location and return the value.
23520
23521 NOTE drow/2003-11-18: This function is called in two situations
23522 now: for the address of static or global variables (partial symbols
23523 only) and for offsets into structures which are expected to be
23524 (more or less) constant. The partial symbol case should go away,
23525 and only the constant case should remain. That will let this
23526 function complain more accurately. A few special modes are allowed
23527 without complaint for global variables (for instance, global
23528 register values and thread-local values).
23529
23530 A location description containing no operations indicates that the
23531 object is optimized out. The return value is 0 for that case.
23532 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23533 callers will only want a very basic result and this can become a
23534 complaint.
23535
23536 Note that stack[0] is unused except as a default error return. */
23537
23538 static CORE_ADDR
23539 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23540 {
23541 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23542 size_t i;
23543 size_t size = blk->size;
23544 const gdb_byte *data = blk->data;
23545 CORE_ADDR stack[64];
23546 int stacki;
23547 unsigned int bytes_read, unsnd;
23548 gdb_byte op;
23549
23550 i = 0;
23551 stacki = 0;
23552 stack[stacki] = 0;
23553 stack[++stacki] = 0;
23554
23555 while (i < size)
23556 {
23557 op = data[i++];
23558 switch (op)
23559 {
23560 case DW_OP_lit0:
23561 case DW_OP_lit1:
23562 case DW_OP_lit2:
23563 case DW_OP_lit3:
23564 case DW_OP_lit4:
23565 case DW_OP_lit5:
23566 case DW_OP_lit6:
23567 case DW_OP_lit7:
23568 case DW_OP_lit8:
23569 case DW_OP_lit9:
23570 case DW_OP_lit10:
23571 case DW_OP_lit11:
23572 case DW_OP_lit12:
23573 case DW_OP_lit13:
23574 case DW_OP_lit14:
23575 case DW_OP_lit15:
23576 case DW_OP_lit16:
23577 case DW_OP_lit17:
23578 case DW_OP_lit18:
23579 case DW_OP_lit19:
23580 case DW_OP_lit20:
23581 case DW_OP_lit21:
23582 case DW_OP_lit22:
23583 case DW_OP_lit23:
23584 case DW_OP_lit24:
23585 case DW_OP_lit25:
23586 case DW_OP_lit26:
23587 case DW_OP_lit27:
23588 case DW_OP_lit28:
23589 case DW_OP_lit29:
23590 case DW_OP_lit30:
23591 case DW_OP_lit31:
23592 stack[++stacki] = op - DW_OP_lit0;
23593 break;
23594
23595 case DW_OP_reg0:
23596 case DW_OP_reg1:
23597 case DW_OP_reg2:
23598 case DW_OP_reg3:
23599 case DW_OP_reg4:
23600 case DW_OP_reg5:
23601 case DW_OP_reg6:
23602 case DW_OP_reg7:
23603 case DW_OP_reg8:
23604 case DW_OP_reg9:
23605 case DW_OP_reg10:
23606 case DW_OP_reg11:
23607 case DW_OP_reg12:
23608 case DW_OP_reg13:
23609 case DW_OP_reg14:
23610 case DW_OP_reg15:
23611 case DW_OP_reg16:
23612 case DW_OP_reg17:
23613 case DW_OP_reg18:
23614 case DW_OP_reg19:
23615 case DW_OP_reg20:
23616 case DW_OP_reg21:
23617 case DW_OP_reg22:
23618 case DW_OP_reg23:
23619 case DW_OP_reg24:
23620 case DW_OP_reg25:
23621 case DW_OP_reg26:
23622 case DW_OP_reg27:
23623 case DW_OP_reg28:
23624 case DW_OP_reg29:
23625 case DW_OP_reg30:
23626 case DW_OP_reg31:
23627 stack[++stacki] = op - DW_OP_reg0;
23628 if (i < size)
23629 dwarf2_complex_location_expr_complaint ();
23630 break;
23631
23632 case DW_OP_regx:
23633 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23634 i += bytes_read;
23635 stack[++stacki] = unsnd;
23636 if (i < size)
23637 dwarf2_complex_location_expr_complaint ();
23638 break;
23639
23640 case DW_OP_addr:
23641 stack[++stacki] = read_address (objfile->obfd, &data[i],
23642 cu, &bytes_read);
23643 i += bytes_read;
23644 break;
23645
23646 case DW_OP_const1u:
23647 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23648 i += 1;
23649 break;
23650
23651 case DW_OP_const1s:
23652 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23653 i += 1;
23654 break;
23655
23656 case DW_OP_const2u:
23657 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23658 i += 2;
23659 break;
23660
23661 case DW_OP_const2s:
23662 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23663 i += 2;
23664 break;
23665
23666 case DW_OP_const4u:
23667 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23668 i += 4;
23669 break;
23670
23671 case DW_OP_const4s:
23672 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23673 i += 4;
23674 break;
23675
23676 case DW_OP_const8u:
23677 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23678 i += 8;
23679 break;
23680
23681 case DW_OP_constu:
23682 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23683 &bytes_read);
23684 i += bytes_read;
23685 break;
23686
23687 case DW_OP_consts:
23688 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23689 i += bytes_read;
23690 break;
23691
23692 case DW_OP_dup:
23693 stack[stacki + 1] = stack[stacki];
23694 stacki++;
23695 break;
23696
23697 case DW_OP_plus:
23698 stack[stacki - 1] += stack[stacki];
23699 stacki--;
23700 break;
23701
23702 case DW_OP_plus_uconst:
23703 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23704 &bytes_read);
23705 i += bytes_read;
23706 break;
23707
23708 case DW_OP_minus:
23709 stack[stacki - 1] -= stack[stacki];
23710 stacki--;
23711 break;
23712
23713 case DW_OP_deref:
23714 /* If we're not the last op, then we definitely can't encode
23715 this using GDB's address_class enum. This is valid for partial
23716 global symbols, although the variable's address will be bogus
23717 in the psymtab. */
23718 if (i < size)
23719 dwarf2_complex_location_expr_complaint ();
23720 break;
23721
23722 case DW_OP_GNU_push_tls_address:
23723 case DW_OP_form_tls_address:
23724 /* The top of the stack has the offset from the beginning
23725 of the thread control block at which the variable is located. */
23726 /* Nothing should follow this operator, so the top of stack would
23727 be returned. */
23728 /* This is valid for partial global symbols, but the variable's
23729 address will be bogus in the psymtab. Make it always at least
23730 non-zero to not look as a variable garbage collected by linker
23731 which have DW_OP_addr 0. */
23732 if (i < size)
23733 dwarf2_complex_location_expr_complaint ();
23734 stack[stacki]++;
23735 break;
23736
23737 case DW_OP_GNU_uninit:
23738 break;
23739
23740 case DW_OP_addrx:
23741 case DW_OP_GNU_addr_index:
23742 case DW_OP_GNU_const_index:
23743 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23744 &bytes_read);
23745 i += bytes_read;
23746 break;
23747
23748 default:
23749 {
23750 const char *name = get_DW_OP_name (op);
23751
23752 if (name)
23753 complaint (_("unsupported stack op: '%s'"),
23754 name);
23755 else
23756 complaint (_("unsupported stack op: '%02x'"),
23757 op);
23758 }
23759
23760 return (stack[stacki]);
23761 }
23762
23763 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23764 outside of the allocated space. Also enforce minimum>0. */
23765 if (stacki >= ARRAY_SIZE (stack) - 1)
23766 {
23767 complaint (_("location description stack overflow"));
23768 return 0;
23769 }
23770
23771 if (stacki <= 0)
23772 {
23773 complaint (_("location description stack underflow"));
23774 return 0;
23775 }
23776 }
23777 return (stack[stacki]);
23778 }
23779
23780 /* memory allocation interface */
23781
23782 static struct dwarf_block *
23783 dwarf_alloc_block (struct dwarf2_cu *cu)
23784 {
23785 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23786 }
23787
23788 static struct die_info *
23789 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23790 {
23791 struct die_info *die;
23792 size_t size = sizeof (struct die_info);
23793
23794 if (num_attrs > 1)
23795 size += (num_attrs - 1) * sizeof (struct attribute);
23796
23797 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23798 memset (die, 0, sizeof (struct die_info));
23799 return (die);
23800 }
23801
23802 \f
23803 /* Macro support. */
23804
23805 /* Return file name relative to the compilation directory of file number I in
23806 *LH's file name table. The result is allocated using xmalloc; the caller is
23807 responsible for freeing it. */
23808
23809 static char *
23810 file_file_name (int file, struct line_header *lh)
23811 {
23812 /* Is the file number a valid index into the line header's file name
23813 table? Remember that file numbers start with one, not zero. */
23814 if (lh->is_valid_file_index (file))
23815 {
23816 const file_entry *fe = lh->file_name_at (file);
23817
23818 if (!IS_ABSOLUTE_PATH (fe->name))
23819 {
23820 const char *dir = fe->include_dir (lh);
23821 if (dir != NULL)
23822 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
23823 }
23824 return xstrdup (fe->name);
23825 }
23826 else
23827 {
23828 /* The compiler produced a bogus file number. We can at least
23829 record the macro definitions made in the file, even if we
23830 won't be able to find the file by name. */
23831 char fake_name[80];
23832
23833 xsnprintf (fake_name, sizeof (fake_name),
23834 "<bad macro file number %d>", file);
23835
23836 complaint (_("bad file number in macro information (%d)"),
23837 file);
23838
23839 return xstrdup (fake_name);
23840 }
23841 }
23842
23843 /* Return the full name of file number I in *LH's file name table.
23844 Use COMP_DIR as the name of the current directory of the
23845 compilation. The result is allocated using xmalloc; the caller is
23846 responsible for freeing it. */
23847 static char *
23848 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23849 {
23850 /* Is the file number a valid index into the line header's file name
23851 table? Remember that file numbers start with one, not zero. */
23852 if (lh->is_valid_file_index (file))
23853 {
23854 char *relative = file_file_name (file, lh);
23855
23856 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23857 return relative;
23858 return reconcat (relative, comp_dir, SLASH_STRING,
23859 relative, (char *) NULL);
23860 }
23861 else
23862 return file_file_name (file, lh);
23863 }
23864
23865
23866 static struct macro_source_file *
23867 macro_start_file (struct dwarf2_cu *cu,
23868 int file, int line,
23869 struct macro_source_file *current_file,
23870 struct line_header *lh)
23871 {
23872 /* File name relative to the compilation directory of this source file. */
23873 char *file_name = file_file_name (file, lh);
23874
23875 if (! current_file)
23876 {
23877 /* Note: We don't create a macro table for this compilation unit
23878 at all until we actually get a filename. */
23879 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23880
23881 /* If we have no current file, then this must be the start_file
23882 directive for the compilation unit's main source file. */
23883 current_file = macro_set_main (macro_table, file_name);
23884 macro_define_special (macro_table);
23885 }
23886 else
23887 current_file = macro_include (current_file, line, file_name);
23888
23889 xfree (file_name);
23890
23891 return current_file;
23892 }
23893
23894 static const char *
23895 consume_improper_spaces (const char *p, const char *body)
23896 {
23897 if (*p == ' ')
23898 {
23899 complaint (_("macro definition contains spaces "
23900 "in formal argument list:\n`%s'"),
23901 body);
23902
23903 while (*p == ' ')
23904 p++;
23905 }
23906
23907 return p;
23908 }
23909
23910
23911 static void
23912 parse_macro_definition (struct macro_source_file *file, int line,
23913 const char *body)
23914 {
23915 const char *p;
23916
23917 /* The body string takes one of two forms. For object-like macro
23918 definitions, it should be:
23919
23920 <macro name> " " <definition>
23921
23922 For function-like macro definitions, it should be:
23923
23924 <macro name> "() " <definition>
23925 or
23926 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23927
23928 Spaces may appear only where explicitly indicated, and in the
23929 <definition>.
23930
23931 The Dwarf 2 spec says that an object-like macro's name is always
23932 followed by a space, but versions of GCC around March 2002 omit
23933 the space when the macro's definition is the empty string.
23934
23935 The Dwarf 2 spec says that there should be no spaces between the
23936 formal arguments in a function-like macro's formal argument list,
23937 but versions of GCC around March 2002 include spaces after the
23938 commas. */
23939
23940
23941 /* Find the extent of the macro name. The macro name is terminated
23942 by either a space or null character (for an object-like macro) or
23943 an opening paren (for a function-like macro). */
23944 for (p = body; *p; p++)
23945 if (*p == ' ' || *p == '(')
23946 break;
23947
23948 if (*p == ' ' || *p == '\0')
23949 {
23950 /* It's an object-like macro. */
23951 int name_len = p - body;
23952 std::string name (body, name_len);
23953 const char *replacement;
23954
23955 if (*p == ' ')
23956 replacement = body + name_len + 1;
23957 else
23958 {
23959 dwarf2_macro_malformed_definition_complaint (body);
23960 replacement = body + name_len;
23961 }
23962
23963 macro_define_object (file, line, name.c_str (), replacement);
23964 }
23965 else if (*p == '(')
23966 {
23967 /* It's a function-like macro. */
23968 std::string name (body, p - body);
23969 int argc = 0;
23970 int argv_size = 1;
23971 char **argv = XNEWVEC (char *, argv_size);
23972
23973 p++;
23974
23975 p = consume_improper_spaces (p, body);
23976
23977 /* Parse the formal argument list. */
23978 while (*p && *p != ')')
23979 {
23980 /* Find the extent of the current argument name. */
23981 const char *arg_start = p;
23982
23983 while (*p && *p != ',' && *p != ')' && *p != ' ')
23984 p++;
23985
23986 if (! *p || p == arg_start)
23987 dwarf2_macro_malformed_definition_complaint (body);
23988 else
23989 {
23990 /* Make sure argv has room for the new argument. */
23991 if (argc >= argv_size)
23992 {
23993 argv_size *= 2;
23994 argv = XRESIZEVEC (char *, argv, argv_size);
23995 }
23996
23997 argv[argc++] = savestring (arg_start, p - arg_start);
23998 }
23999
24000 p = consume_improper_spaces (p, body);
24001
24002 /* Consume the comma, if present. */
24003 if (*p == ',')
24004 {
24005 p++;
24006
24007 p = consume_improper_spaces (p, body);
24008 }
24009 }
24010
24011 if (*p == ')')
24012 {
24013 p++;
24014
24015 if (*p == ' ')
24016 /* Perfectly formed definition, no complaints. */
24017 macro_define_function (file, line, name.c_str (),
24018 argc, (const char **) argv,
24019 p + 1);
24020 else if (*p == '\0')
24021 {
24022 /* Complain, but do define it. */
24023 dwarf2_macro_malformed_definition_complaint (body);
24024 macro_define_function (file, line, name.c_str (),
24025 argc, (const char **) argv,
24026 p);
24027 }
24028 else
24029 /* Just complain. */
24030 dwarf2_macro_malformed_definition_complaint (body);
24031 }
24032 else
24033 /* Just complain. */
24034 dwarf2_macro_malformed_definition_complaint (body);
24035
24036 {
24037 int i;
24038
24039 for (i = 0; i < argc; i++)
24040 xfree (argv[i]);
24041 }
24042 xfree (argv);
24043 }
24044 else
24045 dwarf2_macro_malformed_definition_complaint (body);
24046 }
24047
24048 /* Skip some bytes from BYTES according to the form given in FORM.
24049 Returns the new pointer. */
24050
24051 static const gdb_byte *
24052 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24053 enum dwarf_form form,
24054 unsigned int offset_size,
24055 struct dwarf2_section_info *section)
24056 {
24057 unsigned int bytes_read;
24058
24059 switch (form)
24060 {
24061 case DW_FORM_data1:
24062 case DW_FORM_flag:
24063 ++bytes;
24064 break;
24065
24066 case DW_FORM_data2:
24067 bytes += 2;
24068 break;
24069
24070 case DW_FORM_data4:
24071 bytes += 4;
24072 break;
24073
24074 case DW_FORM_data8:
24075 bytes += 8;
24076 break;
24077
24078 case DW_FORM_data16:
24079 bytes += 16;
24080 break;
24081
24082 case DW_FORM_string:
24083 read_direct_string (abfd, bytes, &bytes_read);
24084 bytes += bytes_read;
24085 break;
24086
24087 case DW_FORM_sec_offset:
24088 case DW_FORM_strp:
24089 case DW_FORM_GNU_strp_alt:
24090 bytes += offset_size;
24091 break;
24092
24093 case DW_FORM_block:
24094 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24095 bytes += bytes_read;
24096 break;
24097
24098 case DW_FORM_block1:
24099 bytes += 1 + read_1_byte (abfd, bytes);
24100 break;
24101 case DW_FORM_block2:
24102 bytes += 2 + read_2_bytes (abfd, bytes);
24103 break;
24104 case DW_FORM_block4:
24105 bytes += 4 + read_4_bytes (abfd, bytes);
24106 break;
24107
24108 case DW_FORM_addrx:
24109 case DW_FORM_sdata:
24110 case DW_FORM_strx:
24111 case DW_FORM_udata:
24112 case DW_FORM_GNU_addr_index:
24113 case DW_FORM_GNU_str_index:
24114 bytes = gdb_skip_leb128 (bytes, buffer_end);
24115 if (bytes == NULL)
24116 {
24117 dwarf2_section_buffer_overflow_complaint (section);
24118 return NULL;
24119 }
24120 break;
24121
24122 case DW_FORM_implicit_const:
24123 break;
24124
24125 default:
24126 {
24127 complaint (_("invalid form 0x%x in `%s'"),
24128 form, section->get_name ());
24129 return NULL;
24130 }
24131 }
24132
24133 return bytes;
24134 }
24135
24136 /* A helper for dwarf_decode_macros that handles skipping an unknown
24137 opcode. Returns an updated pointer to the macro data buffer; or,
24138 on error, issues a complaint and returns NULL. */
24139
24140 static const gdb_byte *
24141 skip_unknown_opcode (unsigned int opcode,
24142 const gdb_byte **opcode_definitions,
24143 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24144 bfd *abfd,
24145 unsigned int offset_size,
24146 struct dwarf2_section_info *section)
24147 {
24148 unsigned int bytes_read, i;
24149 unsigned long arg;
24150 const gdb_byte *defn;
24151
24152 if (opcode_definitions[opcode] == NULL)
24153 {
24154 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24155 opcode);
24156 return NULL;
24157 }
24158
24159 defn = opcode_definitions[opcode];
24160 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24161 defn += bytes_read;
24162
24163 for (i = 0; i < arg; ++i)
24164 {
24165 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24166 (enum dwarf_form) defn[i], offset_size,
24167 section);
24168 if (mac_ptr == NULL)
24169 {
24170 /* skip_form_bytes already issued the complaint. */
24171 return NULL;
24172 }
24173 }
24174
24175 return mac_ptr;
24176 }
24177
24178 /* A helper function which parses the header of a macro section.
24179 If the macro section is the extended (for now called "GNU") type,
24180 then this updates *OFFSET_SIZE. Returns a pointer to just after
24181 the header, or issues a complaint and returns NULL on error. */
24182
24183 static const gdb_byte *
24184 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24185 bfd *abfd,
24186 const gdb_byte *mac_ptr,
24187 unsigned int *offset_size,
24188 int section_is_gnu)
24189 {
24190 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24191
24192 if (section_is_gnu)
24193 {
24194 unsigned int version, flags;
24195
24196 version = read_2_bytes (abfd, mac_ptr);
24197 if (version != 4 && version != 5)
24198 {
24199 complaint (_("unrecognized version `%d' in .debug_macro section"),
24200 version);
24201 return NULL;
24202 }
24203 mac_ptr += 2;
24204
24205 flags = read_1_byte (abfd, mac_ptr);
24206 ++mac_ptr;
24207 *offset_size = (flags & 1) ? 8 : 4;
24208
24209 if ((flags & 2) != 0)
24210 /* We don't need the line table offset. */
24211 mac_ptr += *offset_size;
24212
24213 /* Vendor opcode descriptions. */
24214 if ((flags & 4) != 0)
24215 {
24216 unsigned int i, count;
24217
24218 count = read_1_byte (abfd, mac_ptr);
24219 ++mac_ptr;
24220 for (i = 0; i < count; ++i)
24221 {
24222 unsigned int opcode, bytes_read;
24223 unsigned long arg;
24224
24225 opcode = read_1_byte (abfd, mac_ptr);
24226 ++mac_ptr;
24227 opcode_definitions[opcode] = mac_ptr;
24228 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24229 mac_ptr += bytes_read;
24230 mac_ptr += arg;
24231 }
24232 }
24233 }
24234
24235 return mac_ptr;
24236 }
24237
24238 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24239 including DW_MACRO_import. */
24240
24241 static void
24242 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24243 bfd *abfd,
24244 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24245 struct macro_source_file *current_file,
24246 struct line_header *lh,
24247 struct dwarf2_section_info *section,
24248 int section_is_gnu, int section_is_dwz,
24249 unsigned int offset_size,
24250 htab_t include_hash)
24251 {
24252 struct dwarf2_per_objfile *dwarf2_per_objfile
24253 = cu->per_cu->dwarf2_per_objfile;
24254 struct objfile *objfile = dwarf2_per_objfile->objfile;
24255 enum dwarf_macro_record_type macinfo_type;
24256 int at_commandline;
24257 const gdb_byte *opcode_definitions[256];
24258
24259 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24260 &offset_size, section_is_gnu);
24261 if (mac_ptr == NULL)
24262 {
24263 /* We already issued a complaint. */
24264 return;
24265 }
24266
24267 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24268 GDB is still reading the definitions from command line. First
24269 DW_MACINFO_start_file will need to be ignored as it was already executed
24270 to create CURRENT_FILE for the main source holding also the command line
24271 definitions. On first met DW_MACINFO_start_file this flag is reset to
24272 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24273
24274 at_commandline = 1;
24275
24276 do
24277 {
24278 /* Do we at least have room for a macinfo type byte? */
24279 if (mac_ptr >= mac_end)
24280 {
24281 dwarf2_section_buffer_overflow_complaint (section);
24282 break;
24283 }
24284
24285 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24286 mac_ptr++;
24287
24288 /* Note that we rely on the fact that the corresponding GNU and
24289 DWARF constants are the same. */
24290 DIAGNOSTIC_PUSH
24291 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24292 switch (macinfo_type)
24293 {
24294 /* A zero macinfo type indicates the end of the macro
24295 information. */
24296 case 0:
24297 break;
24298
24299 case DW_MACRO_define:
24300 case DW_MACRO_undef:
24301 case DW_MACRO_define_strp:
24302 case DW_MACRO_undef_strp:
24303 case DW_MACRO_define_sup:
24304 case DW_MACRO_undef_sup:
24305 {
24306 unsigned int bytes_read;
24307 int line;
24308 const char *body;
24309 int is_define;
24310
24311 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24312 mac_ptr += bytes_read;
24313
24314 if (macinfo_type == DW_MACRO_define
24315 || macinfo_type == DW_MACRO_undef)
24316 {
24317 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24318 mac_ptr += bytes_read;
24319 }
24320 else
24321 {
24322 LONGEST str_offset;
24323
24324 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24325 mac_ptr += offset_size;
24326
24327 if (macinfo_type == DW_MACRO_define_sup
24328 || macinfo_type == DW_MACRO_undef_sup
24329 || section_is_dwz)
24330 {
24331 struct dwz_file *dwz
24332 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24333
24334 body = read_indirect_string_from_dwz (objfile,
24335 dwz, str_offset);
24336 }
24337 else
24338 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24339 abfd, str_offset);
24340 }
24341
24342 is_define = (macinfo_type == DW_MACRO_define
24343 || macinfo_type == DW_MACRO_define_strp
24344 || macinfo_type == DW_MACRO_define_sup);
24345 if (! current_file)
24346 {
24347 /* DWARF violation as no main source is present. */
24348 complaint (_("debug info with no main source gives macro %s "
24349 "on line %d: %s"),
24350 is_define ? _("definition") : _("undefinition"),
24351 line, body);
24352 break;
24353 }
24354 if ((line == 0 && !at_commandline)
24355 || (line != 0 && at_commandline))
24356 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24357 at_commandline ? _("command-line") : _("in-file"),
24358 is_define ? _("definition") : _("undefinition"),
24359 line == 0 ? _("zero") : _("non-zero"), line, body);
24360
24361 if (body == NULL)
24362 {
24363 /* Fedora's rpm-build's "debugedit" binary
24364 corrupted .debug_macro sections.
24365
24366 For more info, see
24367 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24368 complaint (_("debug info gives %s invalid macro %s "
24369 "without body (corrupted?) at line %d "
24370 "on file %s"),
24371 at_commandline ? _("command-line") : _("in-file"),
24372 is_define ? _("definition") : _("undefinition"),
24373 line, current_file->filename);
24374 }
24375 else if (is_define)
24376 parse_macro_definition (current_file, line, body);
24377 else
24378 {
24379 gdb_assert (macinfo_type == DW_MACRO_undef
24380 || macinfo_type == DW_MACRO_undef_strp
24381 || macinfo_type == DW_MACRO_undef_sup);
24382 macro_undef (current_file, line, body);
24383 }
24384 }
24385 break;
24386
24387 case DW_MACRO_start_file:
24388 {
24389 unsigned int bytes_read;
24390 int line, file;
24391
24392 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24393 mac_ptr += bytes_read;
24394 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24395 mac_ptr += bytes_read;
24396
24397 if ((line == 0 && !at_commandline)
24398 || (line != 0 && at_commandline))
24399 complaint (_("debug info gives source %d included "
24400 "from %s at %s line %d"),
24401 file, at_commandline ? _("command-line") : _("file"),
24402 line == 0 ? _("zero") : _("non-zero"), line);
24403
24404 if (at_commandline)
24405 {
24406 /* This DW_MACRO_start_file was executed in the
24407 pass one. */
24408 at_commandline = 0;
24409 }
24410 else
24411 current_file = macro_start_file (cu, file, line, current_file,
24412 lh);
24413 }
24414 break;
24415
24416 case DW_MACRO_end_file:
24417 if (! current_file)
24418 complaint (_("macro debug info has an unmatched "
24419 "`close_file' directive"));
24420 else
24421 {
24422 current_file = current_file->included_by;
24423 if (! current_file)
24424 {
24425 enum dwarf_macro_record_type next_type;
24426
24427 /* GCC circa March 2002 doesn't produce the zero
24428 type byte marking the end of the compilation
24429 unit. Complain if it's not there, but exit no
24430 matter what. */
24431
24432 /* Do we at least have room for a macinfo type byte? */
24433 if (mac_ptr >= mac_end)
24434 {
24435 dwarf2_section_buffer_overflow_complaint (section);
24436 return;
24437 }
24438
24439 /* We don't increment mac_ptr here, so this is just
24440 a look-ahead. */
24441 next_type
24442 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24443 mac_ptr);
24444 if (next_type != 0)
24445 complaint (_("no terminating 0-type entry for "
24446 "macros in `.debug_macinfo' section"));
24447
24448 return;
24449 }
24450 }
24451 break;
24452
24453 case DW_MACRO_import:
24454 case DW_MACRO_import_sup:
24455 {
24456 LONGEST offset;
24457 void **slot;
24458 bfd *include_bfd = abfd;
24459 struct dwarf2_section_info *include_section = section;
24460 const gdb_byte *include_mac_end = mac_end;
24461 int is_dwz = section_is_dwz;
24462 const gdb_byte *new_mac_ptr;
24463
24464 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24465 mac_ptr += offset_size;
24466
24467 if (macinfo_type == DW_MACRO_import_sup)
24468 {
24469 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24470
24471 dwz->macro.read (objfile);
24472
24473 include_section = &dwz->macro;
24474 include_bfd = include_section->get_bfd_owner ();
24475 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24476 is_dwz = 1;
24477 }
24478
24479 new_mac_ptr = include_section->buffer + offset;
24480 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24481
24482 if (*slot != NULL)
24483 {
24484 /* This has actually happened; see
24485 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24486 complaint (_("recursive DW_MACRO_import in "
24487 ".debug_macro section"));
24488 }
24489 else
24490 {
24491 *slot = (void *) new_mac_ptr;
24492
24493 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24494 include_mac_end, current_file, lh,
24495 section, section_is_gnu, is_dwz,
24496 offset_size, include_hash);
24497
24498 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24499 }
24500 }
24501 break;
24502
24503 case DW_MACINFO_vendor_ext:
24504 if (!section_is_gnu)
24505 {
24506 unsigned int bytes_read;
24507
24508 /* This reads the constant, but since we don't recognize
24509 any vendor extensions, we ignore it. */
24510 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24511 mac_ptr += bytes_read;
24512 read_direct_string (abfd, mac_ptr, &bytes_read);
24513 mac_ptr += bytes_read;
24514
24515 /* We don't recognize any vendor extensions. */
24516 break;
24517 }
24518 /* FALLTHROUGH */
24519
24520 default:
24521 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24522 mac_ptr, mac_end, abfd, offset_size,
24523 section);
24524 if (mac_ptr == NULL)
24525 return;
24526 break;
24527 }
24528 DIAGNOSTIC_POP
24529 } while (macinfo_type != 0);
24530 }
24531
24532 static void
24533 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24534 int section_is_gnu)
24535 {
24536 struct dwarf2_per_objfile *dwarf2_per_objfile
24537 = cu->per_cu->dwarf2_per_objfile;
24538 struct objfile *objfile = dwarf2_per_objfile->objfile;
24539 struct line_header *lh = cu->line_header;
24540 bfd *abfd;
24541 const gdb_byte *mac_ptr, *mac_end;
24542 struct macro_source_file *current_file = 0;
24543 enum dwarf_macro_record_type macinfo_type;
24544 unsigned int offset_size = cu->header.offset_size;
24545 const gdb_byte *opcode_definitions[256];
24546 void **slot;
24547 struct dwarf2_section_info *section;
24548 const char *section_name;
24549
24550 if (cu->dwo_unit != NULL)
24551 {
24552 if (section_is_gnu)
24553 {
24554 section = &cu->dwo_unit->dwo_file->sections.macro;
24555 section_name = ".debug_macro.dwo";
24556 }
24557 else
24558 {
24559 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24560 section_name = ".debug_macinfo.dwo";
24561 }
24562 }
24563 else
24564 {
24565 if (section_is_gnu)
24566 {
24567 section = &dwarf2_per_objfile->macro;
24568 section_name = ".debug_macro";
24569 }
24570 else
24571 {
24572 section = &dwarf2_per_objfile->macinfo;
24573 section_name = ".debug_macinfo";
24574 }
24575 }
24576
24577 section->read (objfile);
24578 if (section->buffer == NULL)
24579 {
24580 complaint (_("missing %s section"), section_name);
24581 return;
24582 }
24583 abfd = section->get_bfd_owner ();
24584
24585 /* First pass: Find the name of the base filename.
24586 This filename is needed in order to process all macros whose definition
24587 (or undefinition) comes from the command line. These macros are defined
24588 before the first DW_MACINFO_start_file entry, and yet still need to be
24589 associated to the base file.
24590
24591 To determine the base file name, we scan the macro definitions until we
24592 reach the first DW_MACINFO_start_file entry. We then initialize
24593 CURRENT_FILE accordingly so that any macro definition found before the
24594 first DW_MACINFO_start_file can still be associated to the base file. */
24595
24596 mac_ptr = section->buffer + offset;
24597 mac_end = section->buffer + section->size;
24598
24599 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24600 &offset_size, section_is_gnu);
24601 if (mac_ptr == NULL)
24602 {
24603 /* We already issued a complaint. */
24604 return;
24605 }
24606
24607 do
24608 {
24609 /* Do we at least have room for a macinfo type byte? */
24610 if (mac_ptr >= mac_end)
24611 {
24612 /* Complaint is printed during the second pass as GDB will probably
24613 stop the first pass earlier upon finding
24614 DW_MACINFO_start_file. */
24615 break;
24616 }
24617
24618 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24619 mac_ptr++;
24620
24621 /* Note that we rely on the fact that the corresponding GNU and
24622 DWARF constants are the same. */
24623 DIAGNOSTIC_PUSH
24624 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24625 switch (macinfo_type)
24626 {
24627 /* A zero macinfo type indicates the end of the macro
24628 information. */
24629 case 0:
24630 break;
24631
24632 case DW_MACRO_define:
24633 case DW_MACRO_undef:
24634 /* Only skip the data by MAC_PTR. */
24635 {
24636 unsigned int bytes_read;
24637
24638 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24639 mac_ptr += bytes_read;
24640 read_direct_string (abfd, mac_ptr, &bytes_read);
24641 mac_ptr += bytes_read;
24642 }
24643 break;
24644
24645 case DW_MACRO_start_file:
24646 {
24647 unsigned int bytes_read;
24648 int line, file;
24649
24650 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24651 mac_ptr += bytes_read;
24652 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24653 mac_ptr += bytes_read;
24654
24655 current_file = macro_start_file (cu, file, line, current_file, lh);
24656 }
24657 break;
24658
24659 case DW_MACRO_end_file:
24660 /* No data to skip by MAC_PTR. */
24661 break;
24662
24663 case DW_MACRO_define_strp:
24664 case DW_MACRO_undef_strp:
24665 case DW_MACRO_define_sup:
24666 case DW_MACRO_undef_sup:
24667 {
24668 unsigned int bytes_read;
24669
24670 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24671 mac_ptr += bytes_read;
24672 mac_ptr += offset_size;
24673 }
24674 break;
24675
24676 case DW_MACRO_import:
24677 case DW_MACRO_import_sup:
24678 /* Note that, according to the spec, a transparent include
24679 chain cannot call DW_MACRO_start_file. So, we can just
24680 skip this opcode. */
24681 mac_ptr += offset_size;
24682 break;
24683
24684 case DW_MACINFO_vendor_ext:
24685 /* Only skip the data by MAC_PTR. */
24686 if (!section_is_gnu)
24687 {
24688 unsigned int bytes_read;
24689
24690 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24691 mac_ptr += bytes_read;
24692 read_direct_string (abfd, mac_ptr, &bytes_read);
24693 mac_ptr += bytes_read;
24694 }
24695 /* FALLTHROUGH */
24696
24697 default:
24698 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24699 mac_ptr, mac_end, abfd, offset_size,
24700 section);
24701 if (mac_ptr == NULL)
24702 return;
24703 break;
24704 }
24705 DIAGNOSTIC_POP
24706 } while (macinfo_type != 0 && current_file == NULL);
24707
24708 /* Second pass: Process all entries.
24709
24710 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24711 command-line macro definitions/undefinitions. This flag is unset when we
24712 reach the first DW_MACINFO_start_file entry. */
24713
24714 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24715 htab_eq_pointer,
24716 NULL, xcalloc, xfree));
24717 mac_ptr = section->buffer + offset;
24718 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24719 *slot = (void *) mac_ptr;
24720 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24721 current_file, lh, section,
24722 section_is_gnu, 0, offset_size,
24723 include_hash.get ());
24724 }
24725
24726 /* Return the .debug_loc section to use for CU.
24727 For DWO files use .debug_loc.dwo. */
24728
24729 static struct dwarf2_section_info *
24730 cu_debug_loc_section (struct dwarf2_cu *cu)
24731 {
24732 struct dwarf2_per_objfile *dwarf2_per_objfile
24733 = cu->per_cu->dwarf2_per_objfile;
24734
24735 if (cu->dwo_unit)
24736 {
24737 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24738
24739 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24740 }
24741 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24742 : &dwarf2_per_objfile->loc);
24743 }
24744
24745 /* A helper function that fills in a dwarf2_loclist_baton. */
24746
24747 static void
24748 fill_in_loclist_baton (struct dwarf2_cu *cu,
24749 struct dwarf2_loclist_baton *baton,
24750 const struct attribute *attr)
24751 {
24752 struct dwarf2_per_objfile *dwarf2_per_objfile
24753 = cu->per_cu->dwarf2_per_objfile;
24754 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24755
24756 section->read (dwarf2_per_objfile->objfile);
24757
24758 baton->per_cu = cu->per_cu;
24759 gdb_assert (baton->per_cu);
24760 /* We don't know how long the location list is, but make sure we
24761 don't run off the edge of the section. */
24762 baton->size = section->size - DW_UNSND (attr);
24763 baton->data = section->buffer + DW_UNSND (attr);
24764 baton->base_address = cu->base_address;
24765 baton->from_dwo = cu->dwo_unit != NULL;
24766 }
24767
24768 static void
24769 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24770 struct dwarf2_cu *cu, int is_block)
24771 {
24772 struct dwarf2_per_objfile *dwarf2_per_objfile
24773 = cu->per_cu->dwarf2_per_objfile;
24774 struct objfile *objfile = dwarf2_per_objfile->objfile;
24775 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24776
24777 if (attr->form_is_section_offset ()
24778 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24779 the section. If so, fall through to the complaint in the
24780 other branch. */
24781 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24782 {
24783 struct dwarf2_loclist_baton *baton;
24784
24785 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24786
24787 fill_in_loclist_baton (cu, baton, attr);
24788
24789 if (cu->base_known == 0)
24790 complaint (_("Location list used without "
24791 "specifying the CU base address."));
24792
24793 SYMBOL_ACLASS_INDEX (sym) = (is_block
24794 ? dwarf2_loclist_block_index
24795 : dwarf2_loclist_index);
24796 SYMBOL_LOCATION_BATON (sym) = baton;
24797 }
24798 else
24799 {
24800 struct dwarf2_locexpr_baton *baton;
24801
24802 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24803 baton->per_cu = cu->per_cu;
24804 gdb_assert (baton->per_cu);
24805
24806 if (attr->form_is_block ())
24807 {
24808 /* Note that we're just copying the block's data pointer
24809 here, not the actual data. We're still pointing into the
24810 info_buffer for SYM's objfile; right now we never release
24811 that buffer, but when we do clean up properly this may
24812 need to change. */
24813 baton->size = DW_BLOCK (attr)->size;
24814 baton->data = DW_BLOCK (attr)->data;
24815 }
24816 else
24817 {
24818 dwarf2_invalid_attrib_class_complaint ("location description",
24819 sym->natural_name ());
24820 baton->size = 0;
24821 }
24822
24823 SYMBOL_ACLASS_INDEX (sym) = (is_block
24824 ? dwarf2_locexpr_block_index
24825 : dwarf2_locexpr_index);
24826 SYMBOL_LOCATION_BATON (sym) = baton;
24827 }
24828 }
24829
24830 /* Return the OBJFILE associated with the compilation unit CU. If CU
24831 came from a separate debuginfo file, then the master objfile is
24832 returned. */
24833
24834 struct objfile *
24835 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24836 {
24837 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24838
24839 /* Return the master objfile, so that we can report and look up the
24840 correct file containing this variable. */
24841 if (objfile->separate_debug_objfile_backlink)
24842 objfile = objfile->separate_debug_objfile_backlink;
24843
24844 return objfile;
24845 }
24846
24847 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24848 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24849 CU_HEADERP first. */
24850
24851 static const struct comp_unit_head *
24852 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24853 struct dwarf2_per_cu_data *per_cu)
24854 {
24855 const gdb_byte *info_ptr;
24856
24857 if (per_cu->cu)
24858 return &per_cu->cu->header;
24859
24860 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24861
24862 memset (cu_headerp, 0, sizeof (*cu_headerp));
24863 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24864 rcuh_kind::COMPILE);
24865
24866 return cu_headerp;
24867 }
24868
24869 /* Return the address size given in the compilation unit header for CU. */
24870
24871 int
24872 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24873 {
24874 struct comp_unit_head cu_header_local;
24875 const struct comp_unit_head *cu_headerp;
24876
24877 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24878
24879 return cu_headerp->addr_size;
24880 }
24881
24882 /* Return the offset size given in the compilation unit header for CU. */
24883
24884 int
24885 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24886 {
24887 struct comp_unit_head cu_header_local;
24888 const struct comp_unit_head *cu_headerp;
24889
24890 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24891
24892 return cu_headerp->offset_size;
24893 }
24894
24895 /* See its dwarf2loc.h declaration. */
24896
24897 int
24898 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24899 {
24900 struct comp_unit_head cu_header_local;
24901 const struct comp_unit_head *cu_headerp;
24902
24903 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24904
24905 if (cu_headerp->version == 2)
24906 return cu_headerp->addr_size;
24907 else
24908 return cu_headerp->offset_size;
24909 }
24910
24911 /* Return the text offset of the CU. The returned offset comes from
24912 this CU's objfile. If this objfile came from a separate debuginfo
24913 file, then the offset may be different from the corresponding
24914 offset in the parent objfile. */
24915
24916 CORE_ADDR
24917 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24918 {
24919 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
24920 }
24921
24922 /* Return a type that is a generic pointer type, the size of which matches
24923 the address size given in the compilation unit header for PER_CU. */
24924 static struct type *
24925 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24926 {
24927 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24928 struct type *void_type = objfile_type (objfile)->builtin_void;
24929 struct type *addr_type = lookup_pointer_type (void_type);
24930 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24931
24932 if (TYPE_LENGTH (addr_type) == addr_size)
24933 return addr_type;
24934
24935 addr_type
24936 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24937 return addr_type;
24938 }
24939
24940 /* Return DWARF version number of PER_CU. */
24941
24942 short
24943 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24944 {
24945 return per_cu->dwarf_version;
24946 }
24947
24948 /* Locate the .debug_info compilation unit from CU's objfile which contains
24949 the DIE at OFFSET. Raises an error on failure. */
24950
24951 static struct dwarf2_per_cu_data *
24952 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24953 unsigned int offset_in_dwz,
24954 struct dwarf2_per_objfile *dwarf2_per_objfile)
24955 {
24956 struct dwarf2_per_cu_data *this_cu;
24957 int low, high;
24958
24959 low = 0;
24960 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24961 while (high > low)
24962 {
24963 struct dwarf2_per_cu_data *mid_cu;
24964 int mid = low + (high - low) / 2;
24965
24966 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24967 if (mid_cu->is_dwz > offset_in_dwz
24968 || (mid_cu->is_dwz == offset_in_dwz
24969 && mid_cu->sect_off + mid_cu->length >= sect_off))
24970 high = mid;
24971 else
24972 low = mid + 1;
24973 }
24974 gdb_assert (low == high);
24975 this_cu = dwarf2_per_objfile->all_comp_units[low];
24976 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24977 {
24978 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24979 error (_("Dwarf Error: could not find partial DIE containing "
24980 "offset %s [in module %s]"),
24981 sect_offset_str (sect_off),
24982 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24983
24984 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24985 <= sect_off);
24986 return dwarf2_per_objfile->all_comp_units[low-1];
24987 }
24988 else
24989 {
24990 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24991 && sect_off >= this_cu->sect_off + this_cu->length)
24992 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24993 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24994 return this_cu;
24995 }
24996 }
24997
24998 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24999
25000 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25001 : per_cu (per_cu_),
25002 mark (false),
25003 has_loclist (false),
25004 checked_producer (false),
25005 producer_is_gxx_lt_4_6 (false),
25006 producer_is_gcc_lt_4_3 (false),
25007 producer_is_icc (false),
25008 producer_is_icc_lt_14 (false),
25009 producer_is_codewarrior (false),
25010 processing_has_namespace_info (false)
25011 {
25012 per_cu->cu = this;
25013 }
25014
25015 /* Destroy a dwarf2_cu. */
25016
25017 dwarf2_cu::~dwarf2_cu ()
25018 {
25019 per_cu->cu = NULL;
25020 }
25021
25022 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25023
25024 static void
25025 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25026 enum language pretend_language)
25027 {
25028 struct attribute *attr;
25029
25030 /* Set the language we're debugging. */
25031 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25032 if (attr != nullptr)
25033 set_cu_language (DW_UNSND (attr), cu);
25034 else
25035 {
25036 cu->language = pretend_language;
25037 cu->language_defn = language_def (cu->language);
25038 }
25039
25040 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25041 }
25042
25043 /* Increase the age counter on each cached compilation unit, and free
25044 any that are too old. */
25045
25046 static void
25047 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25048 {
25049 struct dwarf2_per_cu_data *per_cu, **last_chain;
25050
25051 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25052 per_cu = dwarf2_per_objfile->read_in_chain;
25053 while (per_cu != NULL)
25054 {
25055 per_cu->cu->last_used ++;
25056 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25057 dwarf2_mark (per_cu->cu);
25058 per_cu = per_cu->cu->read_in_chain;
25059 }
25060
25061 per_cu = dwarf2_per_objfile->read_in_chain;
25062 last_chain = &dwarf2_per_objfile->read_in_chain;
25063 while (per_cu != NULL)
25064 {
25065 struct dwarf2_per_cu_data *next_cu;
25066
25067 next_cu = per_cu->cu->read_in_chain;
25068
25069 if (!per_cu->cu->mark)
25070 {
25071 delete per_cu->cu;
25072 *last_chain = next_cu;
25073 }
25074 else
25075 last_chain = &per_cu->cu->read_in_chain;
25076
25077 per_cu = next_cu;
25078 }
25079 }
25080
25081 /* Remove a single compilation unit from the cache. */
25082
25083 static void
25084 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25085 {
25086 struct dwarf2_per_cu_data *per_cu, **last_chain;
25087 struct dwarf2_per_objfile *dwarf2_per_objfile
25088 = target_per_cu->dwarf2_per_objfile;
25089
25090 per_cu = dwarf2_per_objfile->read_in_chain;
25091 last_chain = &dwarf2_per_objfile->read_in_chain;
25092 while (per_cu != NULL)
25093 {
25094 struct dwarf2_per_cu_data *next_cu;
25095
25096 next_cu = per_cu->cu->read_in_chain;
25097
25098 if (per_cu == target_per_cu)
25099 {
25100 delete per_cu->cu;
25101 per_cu->cu = NULL;
25102 *last_chain = next_cu;
25103 break;
25104 }
25105 else
25106 last_chain = &per_cu->cu->read_in_chain;
25107
25108 per_cu = next_cu;
25109 }
25110 }
25111
25112 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25113 We store these in a hash table separate from the DIEs, and preserve them
25114 when the DIEs are flushed out of cache.
25115
25116 The CU "per_cu" pointer is needed because offset alone is not enough to
25117 uniquely identify the type. A file may have multiple .debug_types sections,
25118 or the type may come from a DWO file. Furthermore, while it's more logical
25119 to use per_cu->section+offset, with Fission the section with the data is in
25120 the DWO file but we don't know that section at the point we need it.
25121 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25122 because we can enter the lookup routine, get_die_type_at_offset, from
25123 outside this file, and thus won't necessarily have PER_CU->cu.
25124 Fortunately, PER_CU is stable for the life of the objfile. */
25125
25126 struct dwarf2_per_cu_offset_and_type
25127 {
25128 const struct dwarf2_per_cu_data *per_cu;
25129 sect_offset sect_off;
25130 struct type *type;
25131 };
25132
25133 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25134
25135 static hashval_t
25136 per_cu_offset_and_type_hash (const void *item)
25137 {
25138 const struct dwarf2_per_cu_offset_and_type *ofs
25139 = (const struct dwarf2_per_cu_offset_and_type *) item;
25140
25141 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25142 }
25143
25144 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25145
25146 static int
25147 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25148 {
25149 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25150 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25151 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25152 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25153
25154 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25155 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25156 }
25157
25158 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25159 table if necessary. For convenience, return TYPE.
25160
25161 The DIEs reading must have careful ordering to:
25162 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25163 reading current DIE.
25164 * Not trying to dereference contents of still incompletely read in types
25165 while reading in other DIEs.
25166 * Enable referencing still incompletely read in types just by a pointer to
25167 the type without accessing its fields.
25168
25169 Therefore caller should follow these rules:
25170 * Try to fetch any prerequisite types we may need to build this DIE type
25171 before building the type and calling set_die_type.
25172 * After building type call set_die_type for current DIE as soon as
25173 possible before fetching more types to complete the current type.
25174 * Make the type as complete as possible before fetching more types. */
25175
25176 static struct type *
25177 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25178 {
25179 struct dwarf2_per_objfile *dwarf2_per_objfile
25180 = cu->per_cu->dwarf2_per_objfile;
25181 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25182 struct objfile *objfile = dwarf2_per_objfile->objfile;
25183 struct attribute *attr;
25184 struct dynamic_prop prop;
25185
25186 /* For Ada types, make sure that the gnat-specific data is always
25187 initialized (if not already set). There are a few types where
25188 we should not be doing so, because the type-specific area is
25189 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25190 where the type-specific area is used to store the floatformat).
25191 But this is not a problem, because the gnat-specific information
25192 is actually not needed for these types. */
25193 if (need_gnat_info (cu)
25194 && TYPE_CODE (type) != TYPE_CODE_FUNC
25195 && TYPE_CODE (type) != TYPE_CODE_FLT
25196 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25197 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25198 && TYPE_CODE (type) != TYPE_CODE_METHOD
25199 && !HAVE_GNAT_AUX_INFO (type))
25200 INIT_GNAT_SPECIFIC (type);
25201
25202 /* Read DW_AT_allocated and set in type. */
25203 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25204 if (attr != NULL && attr->form_is_block ())
25205 {
25206 struct type *prop_type
25207 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25208 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25209 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25210 }
25211 else if (attr != NULL)
25212 {
25213 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25214 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25215 sect_offset_str (die->sect_off));
25216 }
25217
25218 /* Read DW_AT_associated and set in type. */
25219 attr = dwarf2_attr (die, DW_AT_associated, cu);
25220 if (attr != NULL && attr->form_is_block ())
25221 {
25222 struct type *prop_type
25223 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25224 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25225 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25226 }
25227 else if (attr != NULL)
25228 {
25229 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25230 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25231 sect_offset_str (die->sect_off));
25232 }
25233
25234 /* Read DW_AT_data_location and set in type. */
25235 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25236 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25237 dwarf2_per_cu_addr_type (cu->per_cu)))
25238 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25239
25240 if (dwarf2_per_objfile->die_type_hash == NULL)
25241 {
25242 dwarf2_per_objfile->die_type_hash =
25243 htab_create_alloc_ex (127,
25244 per_cu_offset_and_type_hash,
25245 per_cu_offset_and_type_eq,
25246 NULL,
25247 &objfile->objfile_obstack,
25248 hashtab_obstack_allocate,
25249 dummy_obstack_deallocate);
25250 }
25251
25252 ofs.per_cu = cu->per_cu;
25253 ofs.sect_off = die->sect_off;
25254 ofs.type = type;
25255 slot = (struct dwarf2_per_cu_offset_and_type **)
25256 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25257 if (*slot)
25258 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25259 sect_offset_str (die->sect_off));
25260 *slot = XOBNEW (&objfile->objfile_obstack,
25261 struct dwarf2_per_cu_offset_and_type);
25262 **slot = ofs;
25263 return type;
25264 }
25265
25266 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25267 or return NULL if the die does not have a saved type. */
25268
25269 static struct type *
25270 get_die_type_at_offset (sect_offset sect_off,
25271 struct dwarf2_per_cu_data *per_cu)
25272 {
25273 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25274 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25275
25276 if (dwarf2_per_objfile->die_type_hash == NULL)
25277 return NULL;
25278
25279 ofs.per_cu = per_cu;
25280 ofs.sect_off = sect_off;
25281 slot = ((struct dwarf2_per_cu_offset_and_type *)
25282 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25283 if (slot)
25284 return slot->type;
25285 else
25286 return NULL;
25287 }
25288
25289 /* Look up the type for DIE in CU in die_type_hash,
25290 or return NULL if DIE does not have a saved type. */
25291
25292 static struct type *
25293 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25294 {
25295 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25296 }
25297
25298 /* Add a dependence relationship from CU to REF_PER_CU. */
25299
25300 static void
25301 dwarf2_add_dependence (struct dwarf2_cu *cu,
25302 struct dwarf2_per_cu_data *ref_per_cu)
25303 {
25304 void **slot;
25305
25306 if (cu->dependencies == NULL)
25307 cu->dependencies
25308 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25309 NULL, &cu->comp_unit_obstack,
25310 hashtab_obstack_allocate,
25311 dummy_obstack_deallocate);
25312
25313 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25314 if (*slot == NULL)
25315 *slot = ref_per_cu;
25316 }
25317
25318 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25319 Set the mark field in every compilation unit in the
25320 cache that we must keep because we are keeping CU. */
25321
25322 static int
25323 dwarf2_mark_helper (void **slot, void *data)
25324 {
25325 struct dwarf2_per_cu_data *per_cu;
25326
25327 per_cu = (struct dwarf2_per_cu_data *) *slot;
25328
25329 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25330 reading of the chain. As such dependencies remain valid it is not much
25331 useful to track and undo them during QUIT cleanups. */
25332 if (per_cu->cu == NULL)
25333 return 1;
25334
25335 if (per_cu->cu->mark)
25336 return 1;
25337 per_cu->cu->mark = true;
25338
25339 if (per_cu->cu->dependencies != NULL)
25340 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25341
25342 return 1;
25343 }
25344
25345 /* Set the mark field in CU and in every other compilation unit in the
25346 cache that we must keep because we are keeping CU. */
25347
25348 static void
25349 dwarf2_mark (struct dwarf2_cu *cu)
25350 {
25351 if (cu->mark)
25352 return;
25353 cu->mark = true;
25354 if (cu->dependencies != NULL)
25355 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25356 }
25357
25358 static void
25359 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25360 {
25361 while (per_cu)
25362 {
25363 per_cu->cu->mark = false;
25364 per_cu = per_cu->cu->read_in_chain;
25365 }
25366 }
25367
25368 /* Trivial hash function for partial_die_info: the hash value of a DIE
25369 is its offset in .debug_info for this objfile. */
25370
25371 static hashval_t
25372 partial_die_hash (const void *item)
25373 {
25374 const struct partial_die_info *part_die
25375 = (const struct partial_die_info *) item;
25376
25377 return to_underlying (part_die->sect_off);
25378 }
25379
25380 /* Trivial comparison function for partial_die_info structures: two DIEs
25381 are equal if they have the same offset. */
25382
25383 static int
25384 partial_die_eq (const void *item_lhs, const void *item_rhs)
25385 {
25386 const struct partial_die_info *part_die_lhs
25387 = (const struct partial_die_info *) item_lhs;
25388 const struct partial_die_info *part_die_rhs
25389 = (const struct partial_die_info *) item_rhs;
25390
25391 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25392 }
25393
25394 struct cmd_list_element *set_dwarf_cmdlist;
25395 struct cmd_list_element *show_dwarf_cmdlist;
25396
25397 static void
25398 set_dwarf_cmd (const char *args, int from_tty)
25399 {
25400 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25401 gdb_stdout);
25402 }
25403
25404 static void
25405 show_dwarf_cmd (const char *args, int from_tty)
25406 {
25407 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25408 }
25409
25410 bool dwarf_always_disassemble;
25411
25412 static void
25413 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25414 struct cmd_list_element *c, const char *value)
25415 {
25416 fprintf_filtered (file,
25417 _("Whether to always disassemble "
25418 "DWARF expressions is %s.\n"),
25419 value);
25420 }
25421
25422 static void
25423 show_check_physname (struct ui_file *file, int from_tty,
25424 struct cmd_list_element *c, const char *value)
25425 {
25426 fprintf_filtered (file,
25427 _("Whether to check \"physname\" is %s.\n"),
25428 value);
25429 }
25430
25431 void _initialize_dwarf2_read ();
25432 void
25433 _initialize_dwarf2_read ()
25434 {
25435 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25436 Set DWARF specific variables.\n\
25437 Configure DWARF variables such as the cache size."),
25438 &set_dwarf_cmdlist, "maintenance set dwarf ",
25439 0/*allow-unknown*/, &maintenance_set_cmdlist);
25440
25441 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25442 Show DWARF specific variables.\n\
25443 Show DWARF variables such as the cache size."),
25444 &show_dwarf_cmdlist, "maintenance show dwarf ",
25445 0/*allow-unknown*/, &maintenance_show_cmdlist);
25446
25447 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25448 &dwarf_max_cache_age, _("\
25449 Set the upper bound on the age of cached DWARF compilation units."), _("\
25450 Show the upper bound on the age of cached DWARF compilation units."), _("\
25451 A higher limit means that cached compilation units will be stored\n\
25452 in memory longer, and more total memory will be used. Zero disables\n\
25453 caching, which can slow down startup."),
25454 NULL,
25455 show_dwarf_max_cache_age,
25456 &set_dwarf_cmdlist,
25457 &show_dwarf_cmdlist);
25458
25459 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25460 &dwarf_always_disassemble, _("\
25461 Set whether `info address' always disassembles DWARF expressions."), _("\
25462 Show whether `info address' always disassembles DWARF expressions."), _("\
25463 When enabled, DWARF expressions are always printed in an assembly-like\n\
25464 syntax. When disabled, expressions will be printed in a more\n\
25465 conversational style, when possible."),
25466 NULL,
25467 show_dwarf_always_disassemble,
25468 &set_dwarf_cmdlist,
25469 &show_dwarf_cmdlist);
25470
25471 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25472 Set debugging of the DWARF reader."), _("\
25473 Show debugging of the DWARF reader."), _("\
25474 When enabled (non-zero), debugging messages are printed during DWARF\n\
25475 reading and symtab expansion. A value of 1 (one) provides basic\n\
25476 information. A value greater than 1 provides more verbose information."),
25477 NULL,
25478 NULL,
25479 &setdebuglist, &showdebuglist);
25480
25481 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25482 Set debugging of the DWARF DIE reader."), _("\
25483 Show debugging of the DWARF DIE reader."), _("\
25484 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25485 The value is the maximum depth to print."),
25486 NULL,
25487 NULL,
25488 &setdebuglist, &showdebuglist);
25489
25490 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25491 Set debugging of the dwarf line reader."), _("\
25492 Show debugging of the dwarf line reader."), _("\
25493 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25494 A value of 1 (one) provides basic information.\n\
25495 A value greater than 1 provides more verbose information."),
25496 NULL,
25497 NULL,
25498 &setdebuglist, &showdebuglist);
25499
25500 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25501 Set cross-checking of \"physname\" code against demangler."), _("\
25502 Show cross-checking of \"physname\" code against demangler."), _("\
25503 When enabled, GDB's internal \"physname\" code is checked against\n\
25504 the demangler."),
25505 NULL, show_check_physname,
25506 &setdebuglist, &showdebuglist);
25507
25508 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25509 no_class, &use_deprecated_index_sections, _("\
25510 Set whether to use deprecated gdb_index sections."), _("\
25511 Show whether to use deprecated gdb_index sections."), _("\
25512 When enabled, deprecated .gdb_index sections are used anyway.\n\
25513 Normally they are ignored either because of a missing feature or\n\
25514 performance issue.\n\
25515 Warning: This option must be enabled before gdb reads the file."),
25516 NULL,
25517 NULL,
25518 &setlist, &showlist);
25519
25520 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25521 &dwarf2_locexpr_funcs);
25522 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25523 &dwarf2_loclist_funcs);
25524
25525 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25526 &dwarf2_block_frame_base_locexpr_funcs);
25527 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25528 &dwarf2_block_frame_base_loclist_funcs);
25529
25530 #if GDB_SELF_TEST
25531 selftests::register_test ("dw2_expand_symtabs_matching",
25532 selftests::dw2_expand_symtabs_matching::run_test);
25533 #endif
25534 }
This page took 0.567133 seconds and 4 git commands to generate.