Change dwarf2_per_objfile::signatured_types to be htab_up
[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_up cus;
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_up 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 bool dummy_p = false;
933
934 private:
935 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
936 int use_existing_cu, int keep);
937
938 struct dwarf2_per_cu_data *m_this_cu;
939 int m_keep = 0;
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947 };
948
949 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
950 later. */
951 typedef int dir_index;
952
953 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
954 and later. */
955 typedef int file_name_index;
956
957 struct file_entry
958 {
959 file_entry () = default;
960
961 file_entry (const char *name_, dir_index d_index_,
962 unsigned int mod_time_, unsigned int length_)
963 : name (name_),
964 d_index (d_index_),
965 mod_time (mod_time_),
966 length (length_)
967 {}
968
969 /* Return the include directory at D_INDEX stored in LH. Returns
970 NULL if D_INDEX is out of bounds. */
971 const char *include_dir (const line_header *lh) const;
972
973 /* The file name. Note this is an observing pointer. The memory is
974 owned by debug_line_buffer. */
975 const char *name {};
976
977 /* The directory index (1-based). */
978 dir_index d_index {};
979
980 unsigned int mod_time {};
981
982 unsigned int length {};
983
984 /* True if referenced by the Line Number Program. */
985 bool included_p {};
986
987 /* The associated symbol table, if any. */
988 struct symtab *symtab {};
989 };
990
991 /* The line number information for a compilation unit (found in the
992 .debug_line section) begins with a "statement program header",
993 which contains the following information. */
994 struct line_header
995 {
996 line_header ()
997 : offset_in_dwz {}
998 {}
999
1000 /* Add an entry to the include directory table. */
1001 void add_include_dir (const char *include_dir);
1002
1003 /* Add an entry to the file name table. */
1004 void add_file_name (const char *name, dir_index d_index,
1005 unsigned int mod_time, unsigned int length);
1006
1007 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1008 Returns NULL if INDEX is out of bounds. */
1009 const char *include_dir_at (dir_index index) const
1010 {
1011 int vec_index;
1012 if (version >= 5)
1013 vec_index = index;
1014 else
1015 vec_index = index - 1;
1016 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1017 return NULL;
1018 return m_include_dirs[vec_index];
1019 }
1020
1021 bool is_valid_file_index (int file_index)
1022 {
1023 if (version >= 5)
1024 return 0 <= file_index && file_index < file_names_size ();
1025 return 1 <= file_index && file_index <= file_names_size ();
1026 }
1027
1028 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1029 Returns NULL if INDEX is out of bounds. */
1030 file_entry *file_name_at (file_name_index index)
1031 {
1032 int vec_index;
1033 if (version >= 5)
1034 vec_index = index;
1035 else
1036 vec_index = index - 1;
1037 if (vec_index < 0 || vec_index >= m_file_names.size ())
1038 return NULL;
1039 return &m_file_names[vec_index];
1040 }
1041
1042 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1043 this method should only be used to iterate through all file entries in an
1044 index-agnostic manner. */
1045 std::vector<file_entry> &file_names ()
1046 { return m_file_names; }
1047
1048 /* Offset of line number information in .debug_line section. */
1049 sect_offset sect_off {};
1050
1051 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1052 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1053
1054 unsigned int total_length {};
1055 unsigned short version {};
1056 unsigned int header_length {};
1057 unsigned char minimum_instruction_length {};
1058 unsigned char maximum_ops_per_instruction {};
1059 unsigned char default_is_stmt {};
1060 int line_base {};
1061 unsigned char line_range {};
1062 unsigned char opcode_base {};
1063
1064 /* standard_opcode_lengths[i] is the number of operands for the
1065 standard opcode whose value is i. This means that
1066 standard_opcode_lengths[0] is unused, and the last meaningful
1067 element is standard_opcode_lengths[opcode_base - 1]. */
1068 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1069
1070 int file_names_size ()
1071 { return m_file_names.size(); }
1072
1073 /* The start and end of the statement program following this
1074 header. These point into dwarf2_per_objfile->line_buffer. */
1075 const gdb_byte *statement_program_start {}, *statement_program_end {};
1076
1077 private:
1078 /* The include_directories table. Note these are observing
1079 pointers. The memory is owned by debug_line_buffer. */
1080 std::vector<const char *> m_include_dirs;
1081
1082 /* The file_names table. This is private because the meaning of indexes
1083 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1084 before, and is 0 in DWARF 5 and later). So the client should use
1085 file_name_at method for access. */
1086 std::vector<file_entry> m_file_names;
1087 };
1088
1089 typedef std::unique_ptr<line_header> line_header_up;
1090
1091 const char *
1092 file_entry::include_dir (const line_header *lh) const
1093 {
1094 return lh->include_dir_at (d_index);
1095 }
1096
1097 /* When we construct a partial symbol table entry we only
1098 need this much information. */
1099 struct partial_die_info : public allocate_on_obstack
1100 {
1101 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1102
1103 /* Disable assign but still keep copy ctor, which is needed
1104 load_partial_dies. */
1105 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1106
1107 /* Adjust the partial die before generating a symbol for it. This
1108 function may set the is_external flag or change the DIE's
1109 name. */
1110 void fixup (struct dwarf2_cu *cu);
1111
1112 /* Read a minimal amount of information into the minimal die
1113 structure. */
1114 const gdb_byte *read (const struct die_reader_specs *reader,
1115 const struct abbrev_info &abbrev,
1116 const gdb_byte *info_ptr);
1117
1118 /* Offset of this DIE. */
1119 const sect_offset sect_off;
1120
1121 /* DWARF-2 tag for this DIE. */
1122 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1123
1124 /* Assorted flags describing the data found in this DIE. */
1125 const unsigned int has_children : 1;
1126
1127 unsigned int is_external : 1;
1128 unsigned int is_declaration : 1;
1129 unsigned int has_type : 1;
1130 unsigned int has_specification : 1;
1131 unsigned int has_pc_info : 1;
1132 unsigned int may_be_inlined : 1;
1133
1134 /* This DIE has been marked DW_AT_main_subprogram. */
1135 unsigned int main_subprogram : 1;
1136
1137 /* Flag set if the SCOPE field of this structure has been
1138 computed. */
1139 unsigned int scope_set : 1;
1140
1141 /* Flag set if the DIE has a byte_size attribute. */
1142 unsigned int has_byte_size : 1;
1143
1144 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1145 unsigned int has_const_value : 1;
1146
1147 /* Flag set if any of the DIE's children are template arguments. */
1148 unsigned int has_template_arguments : 1;
1149
1150 /* Flag set if fixup has been called on this die. */
1151 unsigned int fixup_called : 1;
1152
1153 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1154 unsigned int is_dwz : 1;
1155
1156 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1157 unsigned int spec_is_dwz : 1;
1158
1159 /* The name of this DIE. Normally the value of DW_AT_name, but
1160 sometimes a default name for unnamed DIEs. */
1161 const char *name = nullptr;
1162
1163 /* The linkage name, if present. */
1164 const char *linkage_name = nullptr;
1165
1166 /* The scope to prepend to our children. This is generally
1167 allocated on the comp_unit_obstack, so will disappear
1168 when this compilation unit leaves the cache. */
1169 const char *scope = nullptr;
1170
1171 /* Some data associated with the partial DIE. The tag determines
1172 which field is live. */
1173 union
1174 {
1175 /* The location description associated with this DIE, if any. */
1176 struct dwarf_block *locdesc;
1177 /* The offset of an import, for DW_TAG_imported_unit. */
1178 sect_offset sect_off;
1179 } d {};
1180
1181 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1182 CORE_ADDR lowpc = 0;
1183 CORE_ADDR highpc = 0;
1184
1185 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1186 DW_AT_sibling, if any. */
1187 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1188 could return DW_AT_sibling values to its caller load_partial_dies. */
1189 const gdb_byte *sibling = nullptr;
1190
1191 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1192 DW_AT_specification (or DW_AT_abstract_origin or
1193 DW_AT_extension). */
1194 sect_offset spec_offset {};
1195
1196 /* Pointers to this DIE's parent, first child, and next sibling,
1197 if any. */
1198 struct partial_die_info *die_parent = nullptr;
1199 struct partial_die_info *die_child = nullptr;
1200 struct partial_die_info *die_sibling = nullptr;
1201
1202 friend struct partial_die_info *
1203 dwarf2_cu::find_partial_die (sect_offset sect_off);
1204
1205 private:
1206 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1207 partial_die_info (sect_offset sect_off)
1208 : partial_die_info (sect_off, DW_TAG_padding, 0)
1209 {
1210 }
1211
1212 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1213 int has_children_)
1214 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1215 {
1216 is_external = 0;
1217 is_declaration = 0;
1218 has_type = 0;
1219 has_specification = 0;
1220 has_pc_info = 0;
1221 may_be_inlined = 0;
1222 main_subprogram = 0;
1223 scope_set = 0;
1224 has_byte_size = 0;
1225 has_const_value = 0;
1226 has_template_arguments = 0;
1227 fixup_called = 0;
1228 is_dwz = 0;
1229 spec_is_dwz = 0;
1230 }
1231 };
1232
1233 /* This data structure holds a complete die structure. */
1234 struct die_info
1235 {
1236 /* DWARF-2 tag for this DIE. */
1237 ENUM_BITFIELD(dwarf_tag) tag : 16;
1238
1239 /* Number of attributes */
1240 unsigned char num_attrs;
1241
1242 /* True if we're presently building the full type name for the
1243 type derived from this DIE. */
1244 unsigned char building_fullname : 1;
1245
1246 /* True if this die is in process. PR 16581. */
1247 unsigned char in_process : 1;
1248
1249 /* True if this DIE has children. */
1250 unsigned char has_children : 1;
1251
1252 /* Abbrev number */
1253 unsigned int abbrev;
1254
1255 /* Offset in .debug_info or .debug_types section. */
1256 sect_offset sect_off;
1257
1258 /* The dies in a compilation unit form an n-ary tree. PARENT
1259 points to this die's parent; CHILD points to the first child of
1260 this node; and all the children of a given node are chained
1261 together via their SIBLING fields. */
1262 struct die_info *child; /* Its first child, if any. */
1263 struct die_info *sibling; /* Its next sibling, if any. */
1264 struct die_info *parent; /* Its parent, if any. */
1265
1266 /* An array of attributes, with NUM_ATTRS elements. There may be
1267 zero, but it's not common and zero-sized arrays are not
1268 sufficiently portable C. */
1269 struct attribute attrs[1];
1270 };
1271
1272 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1273 but this would require a corresponding change in unpack_field_as_long
1274 and friends. */
1275 static int bits_per_byte = 8;
1276
1277 /* When reading a variant or variant part, we track a bit more
1278 information about the field, and store it in an object of this
1279 type. */
1280
1281 struct variant_field
1282 {
1283 /* If we see a DW_TAG_variant, then this will be the discriminant
1284 value. */
1285 ULONGEST discriminant_value;
1286 /* If we see a DW_TAG_variant, then this will be set if this is the
1287 default branch. */
1288 bool default_branch;
1289 /* While reading a DW_TAG_variant_part, this will be set if this
1290 field is the discriminant. */
1291 bool is_discriminant;
1292 };
1293
1294 struct nextfield
1295 {
1296 int accessibility = 0;
1297 int virtuality = 0;
1298 /* Extra information to describe a variant or variant part. */
1299 struct variant_field variant {};
1300 struct field field {};
1301 };
1302
1303 struct fnfieldlist
1304 {
1305 const char *name = nullptr;
1306 std::vector<struct fn_field> fnfields;
1307 };
1308
1309 /* The routines that read and process dies for a C struct or C++ class
1310 pass lists of data member fields and lists of member function fields
1311 in an instance of a field_info structure, as defined below. */
1312 struct field_info
1313 {
1314 /* List of data member and baseclasses fields. */
1315 std::vector<struct nextfield> fields;
1316 std::vector<struct nextfield> baseclasses;
1317
1318 /* Number of fields (including baseclasses). */
1319 int nfields = 0;
1320
1321 /* Set if the accessibility of one of the fields is not public. */
1322 int non_public_fields = 0;
1323
1324 /* Member function fieldlist array, contains name of possibly overloaded
1325 member function, number of overloaded member functions and a pointer
1326 to the head of the member function field chain. */
1327 std::vector<struct fnfieldlist> fnfieldlists;
1328
1329 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1330 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1331 std::vector<struct decl_field> typedef_field_list;
1332
1333 /* Nested types defined by this class and the number of elements in this
1334 list. */
1335 std::vector<struct decl_field> nested_types_list;
1336 };
1337
1338 /* Loaded secondary compilation units are kept in memory until they
1339 have not been referenced for the processing of this many
1340 compilation units. Set this to zero to disable caching. Cache
1341 sizes of up to at least twenty will improve startup time for
1342 typical inter-CU-reference binaries, at an obvious memory cost. */
1343 static int dwarf_max_cache_age = 5;
1344 static void
1345 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1346 struct cmd_list_element *c, const char *value)
1347 {
1348 fprintf_filtered (file, _("The upper bound on the age of cached "
1349 "DWARF compilation units is %s.\n"),
1350 value);
1351 }
1352 \f
1353 /* local function prototypes */
1354
1355 static void dwarf2_find_base_address (struct die_info *die,
1356 struct dwarf2_cu *cu);
1357
1358 static dwarf2_psymtab *create_partial_symtab
1359 (struct dwarf2_per_cu_data *per_cu, const char *name);
1360
1361 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1362 const gdb_byte *info_ptr,
1363 struct die_info *type_unit_die);
1364
1365 static void dwarf2_build_psymtabs_hard
1366 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1367
1368 static void scan_partial_symbols (struct partial_die_info *,
1369 CORE_ADDR *, CORE_ADDR *,
1370 int, struct dwarf2_cu *);
1371
1372 static void add_partial_symbol (struct partial_die_info *,
1373 struct dwarf2_cu *);
1374
1375 static void add_partial_namespace (struct partial_die_info *pdi,
1376 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1377 int set_addrmap, struct dwarf2_cu *cu);
1378
1379 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1380 CORE_ADDR *highpc, int set_addrmap,
1381 struct dwarf2_cu *cu);
1382
1383 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1384 struct dwarf2_cu *cu);
1385
1386 static void add_partial_subprogram (struct partial_die_info *pdi,
1387 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1388 int need_pc, struct dwarf2_cu *cu);
1389
1390 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1391
1392 static struct partial_die_info *load_partial_dies
1393 (const struct die_reader_specs *, const gdb_byte *, int);
1394
1395 /* A pair of partial_die_info and compilation unit. */
1396 struct cu_partial_die_info
1397 {
1398 /* The compilation unit of the partial_die_info. */
1399 struct dwarf2_cu *cu;
1400 /* A partial_die_info. */
1401 struct partial_die_info *pdi;
1402
1403 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1404 : cu (cu),
1405 pdi (pdi)
1406 { /* Nothing. */ }
1407
1408 private:
1409 cu_partial_die_info () = delete;
1410 };
1411
1412 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1413 struct dwarf2_cu *);
1414
1415 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1416 struct attribute *, struct attr_abbrev *,
1417 const gdb_byte *, bool *need_reprocess);
1418
1419 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1420 struct attribute *attr);
1421
1422 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1423
1424 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1425 unsigned int *);
1426
1427 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1428
1429 static LONGEST read_checked_initial_length_and_offset
1430 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1431 unsigned int *, unsigned int *);
1432
1433 static LONGEST read_offset (bfd *, const gdb_byte *,
1434 const struct comp_unit_head *,
1435 unsigned int *);
1436
1437 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1438
1439 static sect_offset read_abbrev_offset
1440 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1441 struct dwarf2_section_info *, sect_offset);
1442
1443 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1444
1445 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1446
1447 static const char *read_indirect_string
1448 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1449 const struct comp_unit_head *, unsigned int *);
1450
1451 static const char *read_indirect_line_string
1452 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1453 const struct comp_unit_head *, unsigned int *);
1454
1455 static const char *read_indirect_string_at_offset
1456 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1457 LONGEST str_offset);
1458
1459 static const char *read_indirect_string_from_dwz
1460 (struct objfile *objfile, struct dwz_file *, LONGEST);
1461
1462 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1463 const gdb_byte *,
1464 unsigned int *);
1465
1466 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1467 ULONGEST str_index);
1468
1469 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1470 ULONGEST str_index);
1471
1472 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1473
1474 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1475 struct dwarf2_cu *);
1476
1477 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1478 unsigned int);
1479
1480 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1481 struct dwarf2_cu *cu);
1482
1483 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1484
1485 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1486 struct dwarf2_cu *cu);
1487
1488 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1489
1490 static struct die_info *die_specification (struct die_info *die,
1491 struct dwarf2_cu **);
1492
1493 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1494 struct dwarf2_cu *cu);
1495
1496 static void dwarf_decode_lines (struct line_header *, const char *,
1497 struct dwarf2_cu *, dwarf2_psymtab *,
1498 CORE_ADDR, int decode_mapping);
1499
1500 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1501 const char *);
1502
1503 static struct symbol *new_symbol (struct die_info *, struct type *,
1504 struct dwarf2_cu *, struct symbol * = NULL);
1505
1506 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1507 struct dwarf2_cu *);
1508
1509 static void dwarf2_const_value_attr (const struct attribute *attr,
1510 struct type *type,
1511 const char *name,
1512 struct obstack *obstack,
1513 struct dwarf2_cu *cu, LONGEST *value,
1514 const gdb_byte **bytes,
1515 struct dwarf2_locexpr_baton **baton);
1516
1517 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1518
1519 static int need_gnat_info (struct dwarf2_cu *);
1520
1521 static struct type *die_descriptive_type (struct die_info *,
1522 struct dwarf2_cu *);
1523
1524 static void set_descriptive_type (struct type *, struct die_info *,
1525 struct dwarf2_cu *);
1526
1527 static struct type *die_containing_type (struct die_info *,
1528 struct dwarf2_cu *);
1529
1530 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1531 struct dwarf2_cu *);
1532
1533 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1534
1535 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1536
1537 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1538
1539 static char *typename_concat (struct obstack *obs, const char *prefix,
1540 const char *suffix, int physname,
1541 struct dwarf2_cu *cu);
1542
1543 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1544
1545 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1546
1547 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1548
1549 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1550
1551 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1552
1553 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1554
1555 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1556 struct dwarf2_cu *, dwarf2_psymtab *);
1557
1558 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1559 values. Keep the items ordered with increasing constraints compliance. */
1560 enum pc_bounds_kind
1561 {
1562 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1563 PC_BOUNDS_NOT_PRESENT,
1564
1565 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1566 were present but they do not form a valid range of PC addresses. */
1567 PC_BOUNDS_INVALID,
1568
1569 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1570 PC_BOUNDS_RANGES,
1571
1572 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1573 PC_BOUNDS_HIGH_LOW,
1574 };
1575
1576 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1577 CORE_ADDR *, CORE_ADDR *,
1578 struct dwarf2_cu *,
1579 dwarf2_psymtab *);
1580
1581 static void get_scope_pc_bounds (struct die_info *,
1582 CORE_ADDR *, CORE_ADDR *,
1583 struct dwarf2_cu *);
1584
1585 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1586 CORE_ADDR, struct dwarf2_cu *);
1587
1588 static void dwarf2_add_field (struct field_info *, struct die_info *,
1589 struct dwarf2_cu *);
1590
1591 static void dwarf2_attach_fields_to_type (struct field_info *,
1592 struct type *, struct dwarf2_cu *);
1593
1594 static void dwarf2_add_member_fn (struct field_info *,
1595 struct die_info *, struct type *,
1596 struct dwarf2_cu *);
1597
1598 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1599 struct type *,
1600 struct dwarf2_cu *);
1601
1602 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1603
1604 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1605
1606 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1607
1608 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1609
1610 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1611
1612 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1613
1614 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1615
1616 static struct type *read_module_type (struct die_info *die,
1617 struct dwarf2_cu *cu);
1618
1619 static const char *namespace_name (struct die_info *die,
1620 int *is_anonymous, struct dwarf2_cu *);
1621
1622 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1623
1624 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1625
1626 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1627 struct dwarf2_cu *);
1628
1629 static struct die_info *read_die_and_siblings_1
1630 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1631 struct die_info *);
1632
1633 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1634 const gdb_byte *info_ptr,
1635 const gdb_byte **new_info_ptr,
1636 struct die_info *parent);
1637
1638 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1639 struct die_info **, const gdb_byte *,
1640 int);
1641
1642 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1643 struct die_info **, const gdb_byte *);
1644
1645 static void process_die (struct die_info *, struct dwarf2_cu *);
1646
1647 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1648 struct obstack *);
1649
1650 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1651
1652 static const char *dwarf2_full_name (const char *name,
1653 struct die_info *die,
1654 struct dwarf2_cu *cu);
1655
1656 static const char *dwarf2_physname (const char *name, struct die_info *die,
1657 struct dwarf2_cu *cu);
1658
1659 static struct die_info *dwarf2_extension (struct die_info *die,
1660 struct dwarf2_cu **);
1661
1662 static const char *dwarf_tag_name (unsigned int);
1663
1664 static const char *dwarf_attr_name (unsigned int);
1665
1666 static const char *dwarf_unit_type_name (int unit_type);
1667
1668 static const char *dwarf_form_name (unsigned int);
1669
1670 static const char *dwarf_bool_name (unsigned int);
1671
1672 static const char *dwarf_type_encoding_name (unsigned int);
1673
1674 static struct die_info *sibling_die (struct die_info *);
1675
1676 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1677
1678 static void dump_die_for_error (struct die_info *);
1679
1680 static void dump_die_1 (struct ui_file *, int level, int max_level,
1681 struct die_info *);
1682
1683 /*static*/ void dump_die (struct die_info *, int max_level);
1684
1685 static void store_in_ref_table (struct die_info *,
1686 struct dwarf2_cu *);
1687
1688 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1689
1690 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1691
1692 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1693 const struct attribute *,
1694 struct dwarf2_cu **);
1695
1696 static struct die_info *follow_die_ref (struct die_info *,
1697 const struct attribute *,
1698 struct dwarf2_cu **);
1699
1700 static struct die_info *follow_die_sig (struct die_info *,
1701 const struct attribute *,
1702 struct dwarf2_cu **);
1703
1704 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1705 struct dwarf2_cu *);
1706
1707 static struct type *get_DW_AT_signature_type (struct die_info *,
1708 const struct attribute *,
1709 struct dwarf2_cu *);
1710
1711 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1712
1713 static void read_signatured_type (struct signatured_type *);
1714
1715 static int attr_to_dynamic_prop (const struct attribute *attr,
1716 struct die_info *die, struct dwarf2_cu *cu,
1717 struct dynamic_prop *prop, struct type *type);
1718
1719 /* memory allocation interface */
1720
1721 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1722
1723 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1724
1725 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1726
1727 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1728 struct dwarf2_loclist_baton *baton,
1729 const struct attribute *attr);
1730
1731 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1732 struct symbol *sym,
1733 struct dwarf2_cu *cu,
1734 int is_block);
1735
1736 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1737 const gdb_byte *info_ptr,
1738 struct abbrev_info *abbrev);
1739
1740 static hashval_t partial_die_hash (const void *item);
1741
1742 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1743
1744 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1745 (sect_offset sect_off, unsigned int offset_in_dwz,
1746 struct dwarf2_per_objfile *dwarf2_per_objfile);
1747
1748 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1749 struct die_info *comp_unit_die,
1750 enum language pretend_language);
1751
1752 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1753
1754 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1755
1756 static struct type *set_die_type (struct die_info *, struct type *,
1757 struct dwarf2_cu *);
1758
1759 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1760
1761 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1762
1763 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1764 enum language);
1765
1766 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1767 enum language);
1768
1769 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1770 enum language);
1771
1772 static void dwarf2_add_dependence (struct dwarf2_cu *,
1773 struct dwarf2_per_cu_data *);
1774
1775 static void dwarf2_mark (struct dwarf2_cu *);
1776
1777 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1778
1779 static struct type *get_die_type_at_offset (sect_offset,
1780 struct dwarf2_per_cu_data *);
1781
1782 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1783
1784 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1785 enum language pretend_language);
1786
1787 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1788
1789 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1790 static struct type *dwarf2_per_cu_addr_sized_int_type
1791 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1792 static struct type *dwarf2_per_cu_int_type
1793 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1794 bool unsigned_p);
1795
1796 /* Class, the destructor of which frees all allocated queue entries. This
1797 will only have work to do if an error was thrown while processing the
1798 dwarf. If no error was thrown then the queue entries should have all
1799 been processed, and freed, as we went along. */
1800
1801 class dwarf2_queue_guard
1802 {
1803 public:
1804 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1805 : m_per_objfile (per_objfile)
1806 {
1807 }
1808
1809 /* Free any entries remaining on the queue. There should only be
1810 entries left if we hit an error while processing the dwarf. */
1811 ~dwarf2_queue_guard ()
1812 {
1813 /* Ensure that no memory is allocated by the queue. */
1814 std::queue<dwarf2_queue_item> empty;
1815 std::swap (m_per_objfile->queue, empty);
1816 }
1817
1818 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1819
1820 private:
1821 dwarf2_per_objfile *m_per_objfile;
1822 };
1823
1824 dwarf2_queue_item::~dwarf2_queue_item ()
1825 {
1826 /* Anything still marked queued is likely to be in an
1827 inconsistent state, so discard it. */
1828 if (per_cu->queued)
1829 {
1830 if (per_cu->cu != NULL)
1831 free_one_cached_comp_unit (per_cu);
1832 per_cu->queued = 0;
1833 }
1834 }
1835
1836 /* The return type of find_file_and_directory. Note, the enclosed
1837 string pointers are only valid while this object is valid. */
1838
1839 struct file_and_directory
1840 {
1841 /* The filename. This is never NULL. */
1842 const char *name;
1843
1844 /* The compilation directory. NULL if not known. If we needed to
1845 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1846 points directly to the DW_AT_comp_dir string attribute owned by
1847 the obstack that owns the DIE. */
1848 const char *comp_dir;
1849
1850 /* If we needed to build a new string for comp_dir, this is what
1851 owns the storage. */
1852 std::string comp_dir_storage;
1853 };
1854
1855 static file_and_directory find_file_and_directory (struct die_info *die,
1856 struct dwarf2_cu *cu);
1857
1858 static char *file_full_name (int file, struct line_header *lh,
1859 const char *comp_dir);
1860
1861 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1862 enum class rcuh_kind { COMPILE, TYPE };
1863
1864 static const gdb_byte *read_and_check_comp_unit_head
1865 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1866 struct comp_unit_head *header,
1867 struct dwarf2_section_info *section,
1868 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1869 rcuh_kind section_kind);
1870
1871 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1872
1873 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1874
1875 static struct dwo_unit *lookup_dwo_unit_in_dwp
1876 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1877 struct dwp_file *dwp_file, const char *comp_dir,
1878 ULONGEST signature, int is_debug_types);
1879
1880 static struct dwp_file *get_dwp_file
1881 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1882
1883 static struct dwo_unit *lookup_dwo_comp_unit
1884 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1885
1886 static struct dwo_unit *lookup_dwo_type_unit
1887 (struct signatured_type *, const char *, const char *);
1888
1889 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1890
1891 /* A unique pointer to a dwo_file. */
1892
1893 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1894
1895 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1896
1897 static void check_producer (struct dwarf2_cu *cu);
1898
1899 static void free_line_header_voidp (void *arg);
1900 \f
1901 /* Various complaints about symbol reading that don't abort the process. */
1902
1903 static void
1904 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1905 {
1906 complaint (_("statement list doesn't fit in .debug_line section"));
1907 }
1908
1909 static void
1910 dwarf2_debug_line_missing_file_complaint (void)
1911 {
1912 complaint (_(".debug_line section has line data without a file"));
1913 }
1914
1915 static void
1916 dwarf2_debug_line_missing_end_sequence_complaint (void)
1917 {
1918 complaint (_(".debug_line section has line "
1919 "program sequence without an end"));
1920 }
1921
1922 static void
1923 dwarf2_complex_location_expr_complaint (void)
1924 {
1925 complaint (_("location expression too complex"));
1926 }
1927
1928 static void
1929 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1930 int arg3)
1931 {
1932 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1933 arg1, arg2, arg3);
1934 }
1935
1936 static void
1937 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1938 {
1939 complaint (_("debug info runs off end of %s section"
1940 " [in module %s]"),
1941 section->get_name (),
1942 section->get_file_name ());
1943 }
1944
1945 static void
1946 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1947 {
1948 complaint (_("macro debug info contains a "
1949 "malformed macro definition:\n`%s'"),
1950 arg1);
1951 }
1952
1953 static void
1954 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1955 {
1956 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1957 arg1, arg2);
1958 }
1959
1960 /* Hash function for line_header_hash. */
1961
1962 static hashval_t
1963 line_header_hash (const struct line_header *ofs)
1964 {
1965 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1966 }
1967
1968 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1969
1970 static hashval_t
1971 line_header_hash_voidp (const void *item)
1972 {
1973 const struct line_header *ofs = (const struct line_header *) item;
1974
1975 return line_header_hash (ofs);
1976 }
1977
1978 /* Equality function for line_header_hash. */
1979
1980 static int
1981 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1982 {
1983 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1984 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1985
1986 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1987 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1988 }
1989
1990 \f
1991
1992 /* See declaration. */
1993
1994 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1995 const dwarf2_debug_sections *names,
1996 bool can_copy_)
1997 : objfile (objfile_),
1998 can_copy (can_copy_)
1999 {
2000 if (names == NULL)
2001 names = &dwarf2_elf_names;
2002
2003 bfd *obfd = objfile->obfd;
2004
2005 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2006 locate_sections (obfd, sec, *names);
2007 }
2008
2009 dwarf2_per_objfile::~dwarf2_per_objfile ()
2010 {
2011 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2012 free_cached_comp_units ();
2013
2014 if (quick_file_names_table)
2015 htab_delete (quick_file_names_table);
2016
2017 if (line_header_hash)
2018 htab_delete (line_header_hash);
2019
2020 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2021 per_cu->imported_symtabs_free ();
2022
2023 for (signatured_type *sig_type : all_type_units)
2024 sig_type->per_cu.imported_symtabs_free ();
2025
2026 /* Everything else should be on the objfile obstack. */
2027 }
2028
2029 /* See declaration. */
2030
2031 void
2032 dwarf2_per_objfile::free_cached_comp_units ()
2033 {
2034 dwarf2_per_cu_data *per_cu = read_in_chain;
2035 dwarf2_per_cu_data **last_chain = &read_in_chain;
2036 while (per_cu != NULL)
2037 {
2038 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2039
2040 delete per_cu->cu;
2041 *last_chain = next_cu;
2042 per_cu = next_cu;
2043 }
2044 }
2045
2046 /* A helper class that calls free_cached_comp_units on
2047 destruction. */
2048
2049 class free_cached_comp_units
2050 {
2051 public:
2052
2053 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2054 : m_per_objfile (per_objfile)
2055 {
2056 }
2057
2058 ~free_cached_comp_units ()
2059 {
2060 m_per_objfile->free_cached_comp_units ();
2061 }
2062
2063 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2064
2065 private:
2066
2067 dwarf2_per_objfile *m_per_objfile;
2068 };
2069
2070 /* Try to locate the sections we need for DWARF 2 debugging
2071 information and return true if we have enough to do something.
2072 NAMES points to the dwarf2 section names, or is NULL if the standard
2073 ELF names are used. CAN_COPY is true for formats where symbol
2074 interposition is possible and so symbol values must follow copy
2075 relocation rules. */
2076
2077 int
2078 dwarf2_has_info (struct objfile *objfile,
2079 const struct dwarf2_debug_sections *names,
2080 bool can_copy)
2081 {
2082 if (objfile->flags & OBJF_READNEVER)
2083 return 0;
2084
2085 struct dwarf2_per_objfile *dwarf2_per_objfile
2086 = get_dwarf2_per_objfile (objfile);
2087
2088 if (dwarf2_per_objfile == NULL)
2089 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2090 names,
2091 can_copy);
2092
2093 return (!dwarf2_per_objfile->info.is_virtual
2094 && dwarf2_per_objfile->info.s.section != NULL
2095 && !dwarf2_per_objfile->abbrev.is_virtual
2096 && dwarf2_per_objfile->abbrev.s.section != NULL);
2097 }
2098
2099 /* When loading sections, we look either for uncompressed section or for
2100 compressed section names. */
2101
2102 static int
2103 section_is_p (const char *section_name,
2104 const struct dwarf2_section_names *names)
2105 {
2106 if (names->normal != NULL
2107 && strcmp (section_name, names->normal) == 0)
2108 return 1;
2109 if (names->compressed != NULL
2110 && strcmp (section_name, names->compressed) == 0)
2111 return 1;
2112 return 0;
2113 }
2114
2115 /* See declaration. */
2116
2117 void
2118 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2119 const dwarf2_debug_sections &names)
2120 {
2121 flagword aflag = bfd_section_flags (sectp);
2122
2123 if ((aflag & SEC_HAS_CONTENTS) == 0)
2124 {
2125 }
2126 else if (elf_section_data (sectp)->this_hdr.sh_size
2127 > bfd_get_file_size (abfd))
2128 {
2129 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2130 warning (_("Discarding section %s which has a section size (%s"
2131 ") larger than the file size [in module %s]"),
2132 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2133 bfd_get_filename (abfd));
2134 }
2135 else if (section_is_p (sectp->name, &names.info))
2136 {
2137 this->info.s.section = sectp;
2138 this->info.size = bfd_section_size (sectp);
2139 }
2140 else if (section_is_p (sectp->name, &names.abbrev))
2141 {
2142 this->abbrev.s.section = sectp;
2143 this->abbrev.size = bfd_section_size (sectp);
2144 }
2145 else if (section_is_p (sectp->name, &names.line))
2146 {
2147 this->line.s.section = sectp;
2148 this->line.size = bfd_section_size (sectp);
2149 }
2150 else if (section_is_p (sectp->name, &names.loc))
2151 {
2152 this->loc.s.section = sectp;
2153 this->loc.size = bfd_section_size (sectp);
2154 }
2155 else if (section_is_p (sectp->name, &names.loclists))
2156 {
2157 this->loclists.s.section = sectp;
2158 this->loclists.size = bfd_section_size (sectp);
2159 }
2160 else if (section_is_p (sectp->name, &names.macinfo))
2161 {
2162 this->macinfo.s.section = sectp;
2163 this->macinfo.size = bfd_section_size (sectp);
2164 }
2165 else if (section_is_p (sectp->name, &names.macro))
2166 {
2167 this->macro.s.section = sectp;
2168 this->macro.size = bfd_section_size (sectp);
2169 }
2170 else if (section_is_p (sectp->name, &names.str))
2171 {
2172 this->str.s.section = sectp;
2173 this->str.size = bfd_section_size (sectp);
2174 }
2175 else if (section_is_p (sectp->name, &names.str_offsets))
2176 {
2177 this->str_offsets.s.section = sectp;
2178 this->str_offsets.size = bfd_section_size (sectp);
2179 }
2180 else if (section_is_p (sectp->name, &names.line_str))
2181 {
2182 this->line_str.s.section = sectp;
2183 this->line_str.size = bfd_section_size (sectp);
2184 }
2185 else if (section_is_p (sectp->name, &names.addr))
2186 {
2187 this->addr.s.section = sectp;
2188 this->addr.size = bfd_section_size (sectp);
2189 }
2190 else if (section_is_p (sectp->name, &names.frame))
2191 {
2192 this->frame.s.section = sectp;
2193 this->frame.size = bfd_section_size (sectp);
2194 }
2195 else if (section_is_p (sectp->name, &names.eh_frame))
2196 {
2197 this->eh_frame.s.section = sectp;
2198 this->eh_frame.size = bfd_section_size (sectp);
2199 }
2200 else if (section_is_p (sectp->name, &names.ranges))
2201 {
2202 this->ranges.s.section = sectp;
2203 this->ranges.size = bfd_section_size (sectp);
2204 }
2205 else if (section_is_p (sectp->name, &names.rnglists))
2206 {
2207 this->rnglists.s.section = sectp;
2208 this->rnglists.size = bfd_section_size (sectp);
2209 }
2210 else if (section_is_p (sectp->name, &names.types))
2211 {
2212 struct dwarf2_section_info type_section;
2213
2214 memset (&type_section, 0, sizeof (type_section));
2215 type_section.s.section = sectp;
2216 type_section.size = bfd_section_size (sectp);
2217
2218 this->types.push_back (type_section);
2219 }
2220 else if (section_is_p (sectp->name, &names.gdb_index))
2221 {
2222 this->gdb_index.s.section = sectp;
2223 this->gdb_index.size = bfd_section_size (sectp);
2224 }
2225 else if (section_is_p (sectp->name, &names.debug_names))
2226 {
2227 this->debug_names.s.section = sectp;
2228 this->debug_names.size = bfd_section_size (sectp);
2229 }
2230 else if (section_is_p (sectp->name, &names.debug_aranges))
2231 {
2232 this->debug_aranges.s.section = sectp;
2233 this->debug_aranges.size = bfd_section_size (sectp);
2234 }
2235
2236 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2237 && bfd_section_vma (sectp) == 0)
2238 this->has_section_at_zero = true;
2239 }
2240
2241 /* A helper function that returns the size of a section in a safe way.
2242 If you are positive that the section has been read before using the
2243 size, then it is safe to refer to the dwarf2_section_info object's
2244 "size" field directly. In other cases, you must call this
2245 function, because for compressed sections the size field is not set
2246 correctly until the section has been read. */
2247
2248 static bfd_size_type
2249 dwarf2_section_size (struct objfile *objfile,
2250 struct dwarf2_section_info *info)
2251 {
2252 if (!info->readin)
2253 info->read (objfile);
2254 return info->size;
2255 }
2256
2257 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2258 SECTION_NAME. */
2259
2260 void
2261 dwarf2_get_section_info (struct objfile *objfile,
2262 enum dwarf2_section_enum sect,
2263 asection **sectp, const gdb_byte **bufp,
2264 bfd_size_type *sizep)
2265 {
2266 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2267 struct dwarf2_section_info *info;
2268
2269 /* We may see an objfile without any DWARF, in which case we just
2270 return nothing. */
2271 if (data == NULL)
2272 {
2273 *sectp = NULL;
2274 *bufp = NULL;
2275 *sizep = 0;
2276 return;
2277 }
2278 switch (sect)
2279 {
2280 case DWARF2_DEBUG_FRAME:
2281 info = &data->frame;
2282 break;
2283 case DWARF2_EH_FRAME:
2284 info = &data->eh_frame;
2285 break;
2286 default:
2287 gdb_assert_not_reached ("unexpected section");
2288 }
2289
2290 info->read (objfile);
2291
2292 *sectp = info->get_bfd_section ();
2293 *bufp = info->buffer;
2294 *sizep = info->size;
2295 }
2296
2297 /* A helper function to find the sections for a .dwz file. */
2298
2299 static void
2300 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2301 {
2302 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2303
2304 /* Note that we only support the standard ELF names, because .dwz
2305 is ELF-only (at the time of writing). */
2306 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2307 {
2308 dwz_file->abbrev.s.section = sectp;
2309 dwz_file->abbrev.size = bfd_section_size (sectp);
2310 }
2311 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2312 {
2313 dwz_file->info.s.section = sectp;
2314 dwz_file->info.size = bfd_section_size (sectp);
2315 }
2316 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2317 {
2318 dwz_file->str.s.section = sectp;
2319 dwz_file->str.size = bfd_section_size (sectp);
2320 }
2321 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2322 {
2323 dwz_file->line.s.section = sectp;
2324 dwz_file->line.size = bfd_section_size (sectp);
2325 }
2326 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2327 {
2328 dwz_file->macro.s.section = sectp;
2329 dwz_file->macro.size = bfd_section_size (sectp);
2330 }
2331 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2332 {
2333 dwz_file->gdb_index.s.section = sectp;
2334 dwz_file->gdb_index.size = bfd_section_size (sectp);
2335 }
2336 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2337 {
2338 dwz_file->debug_names.s.section = sectp;
2339 dwz_file->debug_names.size = bfd_section_size (sectp);
2340 }
2341 }
2342
2343 /* See dwarf2read.h. */
2344
2345 struct dwz_file *
2346 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2347 {
2348 const char *filename;
2349 bfd_size_type buildid_len_arg;
2350 size_t buildid_len;
2351 bfd_byte *buildid;
2352
2353 if (dwarf2_per_objfile->dwz_file != NULL)
2354 return dwarf2_per_objfile->dwz_file.get ();
2355
2356 bfd_set_error (bfd_error_no_error);
2357 gdb::unique_xmalloc_ptr<char> data
2358 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2359 &buildid_len_arg, &buildid));
2360 if (data == NULL)
2361 {
2362 if (bfd_get_error () == bfd_error_no_error)
2363 return NULL;
2364 error (_("could not read '.gnu_debugaltlink' section: %s"),
2365 bfd_errmsg (bfd_get_error ()));
2366 }
2367
2368 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2369
2370 buildid_len = (size_t) buildid_len_arg;
2371
2372 filename = data.get ();
2373
2374 std::string abs_storage;
2375 if (!IS_ABSOLUTE_PATH (filename))
2376 {
2377 gdb::unique_xmalloc_ptr<char> abs
2378 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2379
2380 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2381 filename = abs_storage.c_str ();
2382 }
2383
2384 /* First try the file name given in the section. If that doesn't
2385 work, try to use the build-id instead. */
2386 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2387 if (dwz_bfd != NULL)
2388 {
2389 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2390 dwz_bfd.reset (nullptr);
2391 }
2392
2393 if (dwz_bfd == NULL)
2394 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2395
2396 if (dwz_bfd == NULL)
2397 error (_("could not find '.gnu_debugaltlink' file for %s"),
2398 objfile_name (dwarf2_per_objfile->objfile));
2399
2400 std::unique_ptr<struct dwz_file> result
2401 (new struct dwz_file (std::move (dwz_bfd)));
2402
2403 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2404 result.get ());
2405
2406 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2407 result->dwz_bfd.get ());
2408 dwarf2_per_objfile->dwz_file = std::move (result);
2409 return dwarf2_per_objfile->dwz_file.get ();
2410 }
2411 \f
2412 /* DWARF quick_symbols_functions support. */
2413
2414 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2415 unique line tables, so we maintain a separate table of all .debug_line
2416 derived entries to support the sharing.
2417 All the quick functions need is the list of file names. We discard the
2418 line_header when we're done and don't need to record it here. */
2419 struct quick_file_names
2420 {
2421 /* The data used to construct the hash key. */
2422 struct stmt_list_hash hash;
2423
2424 /* The number of entries in file_names, real_names. */
2425 unsigned int num_file_names;
2426
2427 /* The file names from the line table, after being run through
2428 file_full_name. */
2429 const char **file_names;
2430
2431 /* The file names from the line table after being run through
2432 gdb_realpath. These are computed lazily. */
2433 const char **real_names;
2434 };
2435
2436 /* When using the index (and thus not using psymtabs), each CU has an
2437 object of this type. This is used to hold information needed by
2438 the various "quick" methods. */
2439 struct dwarf2_per_cu_quick_data
2440 {
2441 /* The file table. This can be NULL if there was no file table
2442 or it's currently not read in.
2443 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2444 struct quick_file_names *file_names;
2445
2446 /* The corresponding symbol table. This is NULL if symbols for this
2447 CU have not yet been read. */
2448 struct compunit_symtab *compunit_symtab;
2449
2450 /* A temporary mark bit used when iterating over all CUs in
2451 expand_symtabs_matching. */
2452 unsigned int mark : 1;
2453
2454 /* True if we've tried to read the file table and found there isn't one.
2455 There will be no point in trying to read it again next time. */
2456 unsigned int no_file_data : 1;
2457 };
2458
2459 /* Utility hash function for a stmt_list_hash. */
2460
2461 static hashval_t
2462 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2463 {
2464 hashval_t v = 0;
2465
2466 if (stmt_list_hash->dwo_unit != NULL)
2467 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2468 v += to_underlying (stmt_list_hash->line_sect_off);
2469 return v;
2470 }
2471
2472 /* Utility equality function for a stmt_list_hash. */
2473
2474 static int
2475 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2476 const struct stmt_list_hash *rhs)
2477 {
2478 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2479 return 0;
2480 if (lhs->dwo_unit != NULL
2481 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2482 return 0;
2483
2484 return lhs->line_sect_off == rhs->line_sect_off;
2485 }
2486
2487 /* Hash function for a quick_file_names. */
2488
2489 static hashval_t
2490 hash_file_name_entry (const void *e)
2491 {
2492 const struct quick_file_names *file_data
2493 = (const struct quick_file_names *) e;
2494
2495 return hash_stmt_list_entry (&file_data->hash);
2496 }
2497
2498 /* Equality function for a quick_file_names. */
2499
2500 static int
2501 eq_file_name_entry (const void *a, const void *b)
2502 {
2503 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2504 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2505
2506 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2507 }
2508
2509 /* Delete function for a quick_file_names. */
2510
2511 static void
2512 delete_file_name_entry (void *e)
2513 {
2514 struct quick_file_names *file_data = (struct quick_file_names *) e;
2515 int i;
2516
2517 for (i = 0; i < file_data->num_file_names; ++i)
2518 {
2519 xfree ((void*) file_data->file_names[i]);
2520 if (file_data->real_names)
2521 xfree ((void*) file_data->real_names[i]);
2522 }
2523
2524 /* The space for the struct itself lives on objfile_obstack,
2525 so we don't free it here. */
2526 }
2527
2528 /* Create a quick_file_names hash table. */
2529
2530 static htab_t
2531 create_quick_file_names_table (unsigned int nr_initial_entries)
2532 {
2533 return htab_create_alloc (nr_initial_entries,
2534 hash_file_name_entry, eq_file_name_entry,
2535 delete_file_name_entry, xcalloc, xfree);
2536 }
2537
2538 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2539 have to be created afterwards. You should call age_cached_comp_units after
2540 processing PER_CU->CU. dw2_setup must have been already called. */
2541
2542 static void
2543 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2544 {
2545 if (per_cu->is_debug_types)
2546 load_full_type_unit (per_cu);
2547 else
2548 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2549
2550 if (per_cu->cu == NULL)
2551 return; /* Dummy CU. */
2552
2553 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2554 }
2555
2556 /* Read in the symbols for PER_CU. */
2557
2558 static void
2559 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2560 {
2561 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2562
2563 /* Skip type_unit_groups, reading the type units they contain
2564 is handled elsewhere. */
2565 if (IS_TYPE_UNIT_GROUP (per_cu))
2566 return;
2567
2568 /* The destructor of dwarf2_queue_guard frees any entries left on
2569 the queue. After this point we're guaranteed to leave this function
2570 with the dwarf queue empty. */
2571 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2572
2573 if (dwarf2_per_objfile->using_index
2574 ? per_cu->v.quick->compunit_symtab == NULL
2575 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2576 {
2577 queue_comp_unit (per_cu, language_minimal);
2578 load_cu (per_cu, skip_partial);
2579
2580 /* If we just loaded a CU from a DWO, and we're working with an index
2581 that may badly handle TUs, load all the TUs in that DWO as well.
2582 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2583 if (!per_cu->is_debug_types
2584 && per_cu->cu != NULL
2585 && per_cu->cu->dwo_unit != NULL
2586 && dwarf2_per_objfile->index_table != NULL
2587 && dwarf2_per_objfile->index_table->version <= 7
2588 /* DWP files aren't supported yet. */
2589 && get_dwp_file (dwarf2_per_objfile) == NULL)
2590 queue_and_load_all_dwo_tus (per_cu);
2591 }
2592
2593 process_queue (dwarf2_per_objfile);
2594
2595 /* Age the cache, releasing compilation units that have not
2596 been used recently. */
2597 age_cached_comp_units (dwarf2_per_objfile);
2598 }
2599
2600 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2601 the objfile from which this CU came. Returns the resulting symbol
2602 table. */
2603
2604 static struct compunit_symtab *
2605 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2606 {
2607 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2608
2609 gdb_assert (dwarf2_per_objfile->using_index);
2610 if (!per_cu->v.quick->compunit_symtab)
2611 {
2612 free_cached_comp_units freer (dwarf2_per_objfile);
2613 scoped_restore decrementer = increment_reading_symtab ();
2614 dw2_do_instantiate_symtab (per_cu, skip_partial);
2615 process_cu_includes (dwarf2_per_objfile);
2616 }
2617
2618 return per_cu->v.quick->compunit_symtab;
2619 }
2620
2621 /* See declaration. */
2622
2623 dwarf2_per_cu_data *
2624 dwarf2_per_objfile::get_cutu (int index)
2625 {
2626 if (index >= this->all_comp_units.size ())
2627 {
2628 index -= this->all_comp_units.size ();
2629 gdb_assert (index < this->all_type_units.size ());
2630 return &this->all_type_units[index]->per_cu;
2631 }
2632
2633 return this->all_comp_units[index];
2634 }
2635
2636 /* See declaration. */
2637
2638 dwarf2_per_cu_data *
2639 dwarf2_per_objfile::get_cu (int index)
2640 {
2641 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2642
2643 return this->all_comp_units[index];
2644 }
2645
2646 /* See declaration. */
2647
2648 signatured_type *
2649 dwarf2_per_objfile::get_tu (int index)
2650 {
2651 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2652
2653 return this->all_type_units[index];
2654 }
2655
2656 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2657 objfile_obstack, and constructed with the specified field
2658 values. */
2659
2660 static dwarf2_per_cu_data *
2661 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2662 struct dwarf2_section_info *section,
2663 int is_dwz,
2664 sect_offset sect_off, ULONGEST length)
2665 {
2666 struct objfile *objfile = dwarf2_per_objfile->objfile;
2667 dwarf2_per_cu_data *the_cu
2668 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2669 struct dwarf2_per_cu_data);
2670 the_cu->sect_off = sect_off;
2671 the_cu->length = length;
2672 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2673 the_cu->section = section;
2674 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2675 struct dwarf2_per_cu_quick_data);
2676 the_cu->is_dwz = is_dwz;
2677 return the_cu;
2678 }
2679
2680 /* A helper for create_cus_from_index that handles a given list of
2681 CUs. */
2682
2683 static void
2684 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2685 const gdb_byte *cu_list, offset_type n_elements,
2686 struct dwarf2_section_info *section,
2687 int is_dwz)
2688 {
2689 for (offset_type i = 0; i < n_elements; i += 2)
2690 {
2691 gdb_static_assert (sizeof (ULONGEST) >= 8);
2692
2693 sect_offset sect_off
2694 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2695 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2696 cu_list += 2 * 8;
2697
2698 dwarf2_per_cu_data *per_cu
2699 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2700 sect_off, length);
2701 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2702 }
2703 }
2704
2705 /* Read the CU list from the mapped index, and use it to create all
2706 the CU objects for this objfile. */
2707
2708 static void
2709 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2710 const gdb_byte *cu_list, offset_type cu_list_elements,
2711 const gdb_byte *dwz_list, offset_type dwz_elements)
2712 {
2713 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2714 dwarf2_per_objfile->all_comp_units.reserve
2715 ((cu_list_elements + dwz_elements) / 2);
2716
2717 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2718 &dwarf2_per_objfile->info, 0);
2719
2720 if (dwz_elements == 0)
2721 return;
2722
2723 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2724 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2725 &dwz->info, 1);
2726 }
2727
2728 /* Create the signatured type hash table from the index. */
2729
2730 static void
2731 create_signatured_type_table_from_index
2732 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2733 struct dwarf2_section_info *section,
2734 const gdb_byte *bytes,
2735 offset_type elements)
2736 {
2737 struct objfile *objfile = dwarf2_per_objfile->objfile;
2738
2739 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2740 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2741
2742 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2743
2744 for (offset_type i = 0; i < elements; i += 3)
2745 {
2746 struct signatured_type *sig_type;
2747 ULONGEST signature;
2748 void **slot;
2749 cu_offset type_offset_in_tu;
2750
2751 gdb_static_assert (sizeof (ULONGEST) >= 8);
2752 sect_offset sect_off
2753 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2754 type_offset_in_tu
2755 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2756 BFD_ENDIAN_LITTLE);
2757 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2758 bytes += 3 * 8;
2759
2760 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2761 struct signatured_type);
2762 sig_type->signature = signature;
2763 sig_type->type_offset_in_tu = type_offset_in_tu;
2764 sig_type->per_cu.is_debug_types = 1;
2765 sig_type->per_cu.section = section;
2766 sig_type->per_cu.sect_off = sect_off;
2767 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2768 sig_type->per_cu.v.quick
2769 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2770 struct dwarf2_per_cu_quick_data);
2771
2772 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2773 *slot = sig_type;
2774
2775 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2776 }
2777
2778 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2779 }
2780
2781 /* Create the signatured type hash table from .debug_names. */
2782
2783 static void
2784 create_signatured_type_table_from_debug_names
2785 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2786 const mapped_debug_names &map,
2787 struct dwarf2_section_info *section,
2788 struct dwarf2_section_info *abbrev_section)
2789 {
2790 struct objfile *objfile = dwarf2_per_objfile->objfile;
2791
2792 section->read (objfile);
2793 abbrev_section->read (objfile);
2794
2795 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2796 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2797
2798 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2799
2800 for (uint32_t i = 0; i < map.tu_count; ++i)
2801 {
2802 struct signatured_type *sig_type;
2803 void **slot;
2804
2805 sect_offset sect_off
2806 = (sect_offset) (extract_unsigned_integer
2807 (map.tu_table_reordered + i * map.offset_size,
2808 map.offset_size,
2809 map.dwarf5_byte_order));
2810
2811 comp_unit_head cu_header;
2812 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2813 abbrev_section,
2814 section->buffer + to_underlying (sect_off),
2815 rcuh_kind::TYPE);
2816
2817 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2818 struct signatured_type);
2819 sig_type->signature = cu_header.signature;
2820 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2821 sig_type->per_cu.is_debug_types = 1;
2822 sig_type->per_cu.section = section;
2823 sig_type->per_cu.sect_off = sect_off;
2824 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2825 sig_type->per_cu.v.quick
2826 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2827 struct dwarf2_per_cu_quick_data);
2828
2829 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2830 *slot = sig_type;
2831
2832 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2833 }
2834
2835 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2836 }
2837
2838 /* Read the address map data from the mapped index, and use it to
2839 populate the objfile's psymtabs_addrmap. */
2840
2841 static void
2842 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2843 struct mapped_index *index)
2844 {
2845 struct objfile *objfile = dwarf2_per_objfile->objfile;
2846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2847 const gdb_byte *iter, *end;
2848 struct addrmap *mutable_map;
2849 CORE_ADDR baseaddr;
2850
2851 auto_obstack temp_obstack;
2852
2853 mutable_map = addrmap_create_mutable (&temp_obstack);
2854
2855 iter = index->address_table.data ();
2856 end = iter + index->address_table.size ();
2857
2858 baseaddr = objfile->text_section_offset ();
2859
2860 while (iter < end)
2861 {
2862 ULONGEST hi, lo, cu_index;
2863 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2864 iter += 8;
2865 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2866 iter += 8;
2867 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2868 iter += 4;
2869
2870 if (lo > hi)
2871 {
2872 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2873 hex_string (lo), hex_string (hi));
2874 continue;
2875 }
2876
2877 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2878 {
2879 complaint (_(".gdb_index address table has invalid CU number %u"),
2880 (unsigned) cu_index);
2881 continue;
2882 }
2883
2884 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2885 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2886 addrmap_set_empty (mutable_map, lo, hi - 1,
2887 dwarf2_per_objfile->get_cu (cu_index));
2888 }
2889
2890 objfile->partial_symtabs->psymtabs_addrmap
2891 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2892 }
2893
2894 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2895 populate the objfile's psymtabs_addrmap. */
2896
2897 static void
2898 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2899 struct dwarf2_section_info *section)
2900 {
2901 struct objfile *objfile = dwarf2_per_objfile->objfile;
2902 bfd *abfd = objfile->obfd;
2903 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2904 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2905
2906 auto_obstack temp_obstack;
2907 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2908
2909 std::unordered_map<sect_offset,
2910 dwarf2_per_cu_data *,
2911 gdb::hash_enum<sect_offset>>
2912 debug_info_offset_to_per_cu;
2913 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2914 {
2915 const auto insertpair
2916 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2917 if (!insertpair.second)
2918 {
2919 warning (_("Section .debug_aranges in %s has duplicate "
2920 "debug_info_offset %s, ignoring .debug_aranges."),
2921 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2922 return;
2923 }
2924 }
2925
2926 section->read (objfile);
2927
2928 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2929
2930 const gdb_byte *addr = section->buffer;
2931
2932 while (addr < section->buffer + section->size)
2933 {
2934 const gdb_byte *const entry_addr = addr;
2935 unsigned int bytes_read;
2936
2937 const LONGEST entry_length = read_initial_length (abfd, addr,
2938 &bytes_read);
2939 addr += bytes_read;
2940
2941 const gdb_byte *const entry_end = addr + entry_length;
2942 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2943 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2944 if (addr + entry_length > section->buffer + section->size)
2945 {
2946 warning (_("Section .debug_aranges in %s entry at offset %s "
2947 "length %s exceeds section length %s, "
2948 "ignoring .debug_aranges."),
2949 objfile_name (objfile),
2950 plongest (entry_addr - section->buffer),
2951 plongest (bytes_read + entry_length),
2952 pulongest (section->size));
2953 return;
2954 }
2955
2956 /* The version number. */
2957 const uint16_t version = read_2_bytes (abfd, addr);
2958 addr += 2;
2959 if (version != 2)
2960 {
2961 warning (_("Section .debug_aranges in %s entry at offset %s "
2962 "has unsupported version %d, ignoring .debug_aranges."),
2963 objfile_name (objfile),
2964 plongest (entry_addr - section->buffer), version);
2965 return;
2966 }
2967
2968 const uint64_t debug_info_offset
2969 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2970 addr += offset_size;
2971 const auto per_cu_it
2972 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2973 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2974 {
2975 warning (_("Section .debug_aranges in %s entry at offset %s "
2976 "debug_info_offset %s does not exists, "
2977 "ignoring .debug_aranges."),
2978 objfile_name (objfile),
2979 plongest (entry_addr - section->buffer),
2980 pulongest (debug_info_offset));
2981 return;
2982 }
2983 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2984
2985 const uint8_t address_size = *addr++;
2986 if (address_size < 1 || address_size > 8)
2987 {
2988 warning (_("Section .debug_aranges in %s entry at offset %s "
2989 "address_size %u is invalid, ignoring .debug_aranges."),
2990 objfile_name (objfile),
2991 plongest (entry_addr - section->buffer), address_size);
2992 return;
2993 }
2994
2995 const uint8_t segment_selector_size = *addr++;
2996 if (segment_selector_size != 0)
2997 {
2998 warning (_("Section .debug_aranges in %s entry at offset %s "
2999 "segment_selector_size %u is not supported, "
3000 "ignoring .debug_aranges."),
3001 objfile_name (objfile),
3002 plongest (entry_addr - section->buffer),
3003 segment_selector_size);
3004 return;
3005 }
3006
3007 /* Must pad to an alignment boundary that is twice the address
3008 size. It is undocumented by the DWARF standard but GCC does
3009 use it. */
3010 for (size_t padding = ((-(addr - section->buffer))
3011 & (2 * address_size - 1));
3012 padding > 0; padding--)
3013 if (*addr++ != 0)
3014 {
3015 warning (_("Section .debug_aranges in %s entry at offset %s "
3016 "padding is not zero, ignoring .debug_aranges."),
3017 objfile_name (objfile),
3018 plongest (entry_addr - section->buffer));
3019 return;
3020 }
3021
3022 for (;;)
3023 {
3024 if (addr + 2 * address_size > entry_end)
3025 {
3026 warning (_("Section .debug_aranges in %s entry at offset %s "
3027 "address list is not properly terminated, "
3028 "ignoring .debug_aranges."),
3029 objfile_name (objfile),
3030 plongest (entry_addr - section->buffer));
3031 return;
3032 }
3033 ULONGEST start = extract_unsigned_integer (addr, address_size,
3034 dwarf5_byte_order);
3035 addr += address_size;
3036 ULONGEST length = extract_unsigned_integer (addr, address_size,
3037 dwarf5_byte_order);
3038 addr += address_size;
3039 if (start == 0 && length == 0)
3040 break;
3041 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3042 {
3043 /* Symbol was eliminated due to a COMDAT group. */
3044 continue;
3045 }
3046 ULONGEST end = start + length;
3047 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3048 - baseaddr);
3049 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3050 - baseaddr);
3051 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3052 }
3053 }
3054
3055 objfile->partial_symtabs->psymtabs_addrmap
3056 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3057 }
3058
3059 /* Find a slot in the mapped index INDEX for the object named NAME.
3060 If NAME is found, set *VEC_OUT to point to the CU vector in the
3061 constant pool and return true. If NAME cannot be found, return
3062 false. */
3063
3064 static bool
3065 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3066 offset_type **vec_out)
3067 {
3068 offset_type hash;
3069 offset_type slot, step;
3070 int (*cmp) (const char *, const char *);
3071
3072 gdb::unique_xmalloc_ptr<char> without_params;
3073 if (current_language->la_language == language_cplus
3074 || current_language->la_language == language_fortran
3075 || current_language->la_language == language_d)
3076 {
3077 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3078 not contain any. */
3079
3080 if (strchr (name, '(') != NULL)
3081 {
3082 without_params = cp_remove_params (name);
3083
3084 if (without_params != NULL)
3085 name = without_params.get ();
3086 }
3087 }
3088
3089 /* Index version 4 did not support case insensitive searches. But the
3090 indices for case insensitive languages are built in lowercase, therefore
3091 simulate our NAME being searched is also lowercased. */
3092 hash = mapped_index_string_hash ((index->version == 4
3093 && case_sensitivity == case_sensitive_off
3094 ? 5 : index->version),
3095 name);
3096
3097 slot = hash & (index->symbol_table.size () - 1);
3098 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3099 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3100
3101 for (;;)
3102 {
3103 const char *str;
3104
3105 const auto &bucket = index->symbol_table[slot];
3106 if (bucket.name == 0 && bucket.vec == 0)
3107 return false;
3108
3109 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3110 if (!cmp (name, str))
3111 {
3112 *vec_out = (offset_type *) (index->constant_pool
3113 + MAYBE_SWAP (bucket.vec));
3114 return true;
3115 }
3116
3117 slot = (slot + step) & (index->symbol_table.size () - 1);
3118 }
3119 }
3120
3121 /* A helper function that reads the .gdb_index from BUFFER and fills
3122 in MAP. FILENAME is the name of the file containing the data;
3123 it is used for error reporting. DEPRECATED_OK is true if it is
3124 ok to use deprecated sections.
3125
3126 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3127 out parameters that are filled in with information about the CU and
3128 TU lists in the section.
3129
3130 Returns true if all went well, false otherwise. */
3131
3132 static bool
3133 read_gdb_index_from_buffer (struct objfile *objfile,
3134 const char *filename,
3135 bool deprecated_ok,
3136 gdb::array_view<const gdb_byte> buffer,
3137 struct mapped_index *map,
3138 const gdb_byte **cu_list,
3139 offset_type *cu_list_elements,
3140 const gdb_byte **types_list,
3141 offset_type *types_list_elements)
3142 {
3143 const gdb_byte *addr = &buffer[0];
3144
3145 /* Version check. */
3146 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3147 /* Versions earlier than 3 emitted every copy of a psymbol. This
3148 causes the index to behave very poorly for certain requests. Version 3
3149 contained incomplete addrmap. So, it seems better to just ignore such
3150 indices. */
3151 if (version < 4)
3152 {
3153 static int warning_printed = 0;
3154 if (!warning_printed)
3155 {
3156 warning (_("Skipping obsolete .gdb_index section in %s."),
3157 filename);
3158 warning_printed = 1;
3159 }
3160 return 0;
3161 }
3162 /* Index version 4 uses a different hash function than index version
3163 5 and later.
3164
3165 Versions earlier than 6 did not emit psymbols for inlined
3166 functions. Using these files will cause GDB not to be able to
3167 set breakpoints on inlined functions by name, so we ignore these
3168 indices unless the user has done
3169 "set use-deprecated-index-sections on". */
3170 if (version < 6 && !deprecated_ok)
3171 {
3172 static int warning_printed = 0;
3173 if (!warning_printed)
3174 {
3175 warning (_("\
3176 Skipping deprecated .gdb_index section in %s.\n\
3177 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3178 to use the section anyway."),
3179 filename);
3180 warning_printed = 1;
3181 }
3182 return 0;
3183 }
3184 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3185 of the TU (for symbols coming from TUs),
3186 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3187 Plus gold-generated indices can have duplicate entries for global symbols,
3188 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3189 These are just performance bugs, and we can't distinguish gdb-generated
3190 indices from gold-generated ones, so issue no warning here. */
3191
3192 /* Indexes with higher version than the one supported by GDB may be no
3193 longer backward compatible. */
3194 if (version > 8)
3195 return 0;
3196
3197 map->version = version;
3198
3199 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3200
3201 int i = 0;
3202 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3203 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3204 / 8);
3205 ++i;
3206
3207 *types_list = addr + MAYBE_SWAP (metadata[i]);
3208 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3209 - MAYBE_SWAP (metadata[i]))
3210 / 8);
3211 ++i;
3212
3213 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3214 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3215 map->address_table
3216 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3217 ++i;
3218
3219 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3220 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3221 map->symbol_table
3222 = gdb::array_view<mapped_index::symbol_table_slot>
3223 ((mapped_index::symbol_table_slot *) symbol_table,
3224 (mapped_index::symbol_table_slot *) symbol_table_end);
3225
3226 ++i;
3227 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3228
3229 return 1;
3230 }
3231
3232 /* Callback types for dwarf2_read_gdb_index. */
3233
3234 typedef gdb::function_view
3235 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3236 get_gdb_index_contents_ftype;
3237 typedef gdb::function_view
3238 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3239 get_gdb_index_contents_dwz_ftype;
3240
3241 /* Read .gdb_index. If everything went ok, initialize the "quick"
3242 elements of all the CUs and return 1. Otherwise, return 0. */
3243
3244 static int
3245 dwarf2_read_gdb_index
3246 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3247 get_gdb_index_contents_ftype get_gdb_index_contents,
3248 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3249 {
3250 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3251 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3252 struct dwz_file *dwz;
3253 struct objfile *objfile = dwarf2_per_objfile->objfile;
3254
3255 gdb::array_view<const gdb_byte> main_index_contents
3256 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3257
3258 if (main_index_contents.empty ())
3259 return 0;
3260
3261 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3262 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3263 use_deprecated_index_sections,
3264 main_index_contents, map.get (), &cu_list,
3265 &cu_list_elements, &types_list,
3266 &types_list_elements))
3267 return 0;
3268
3269 /* Don't use the index if it's empty. */
3270 if (map->symbol_table.empty ())
3271 return 0;
3272
3273 /* If there is a .dwz file, read it so we can get its CU list as
3274 well. */
3275 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3276 if (dwz != NULL)
3277 {
3278 struct mapped_index dwz_map;
3279 const gdb_byte *dwz_types_ignore;
3280 offset_type dwz_types_elements_ignore;
3281
3282 gdb::array_view<const gdb_byte> dwz_index_content
3283 = get_gdb_index_contents_dwz (objfile, dwz);
3284
3285 if (dwz_index_content.empty ())
3286 return 0;
3287
3288 if (!read_gdb_index_from_buffer (objfile,
3289 bfd_get_filename (dwz->dwz_bfd.get ()),
3290 1, dwz_index_content, &dwz_map,
3291 &dwz_list, &dwz_list_elements,
3292 &dwz_types_ignore,
3293 &dwz_types_elements_ignore))
3294 {
3295 warning (_("could not read '.gdb_index' section from %s; skipping"),
3296 bfd_get_filename (dwz->dwz_bfd.get ()));
3297 return 0;
3298 }
3299 }
3300
3301 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3302 dwz_list, dwz_list_elements);
3303
3304 if (types_list_elements)
3305 {
3306 /* We can only handle a single .debug_types when we have an
3307 index. */
3308 if (dwarf2_per_objfile->types.size () != 1)
3309 return 0;
3310
3311 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3312
3313 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3314 types_list, types_list_elements);
3315 }
3316
3317 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3318
3319 dwarf2_per_objfile->index_table = std::move (map);
3320 dwarf2_per_objfile->using_index = 1;
3321 dwarf2_per_objfile->quick_file_names_table =
3322 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3323
3324 return 1;
3325 }
3326
3327 /* die_reader_func for dw2_get_file_names. */
3328
3329 static void
3330 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3331 const gdb_byte *info_ptr,
3332 struct die_info *comp_unit_die)
3333 {
3334 struct dwarf2_cu *cu = reader->cu;
3335 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3336 struct dwarf2_per_objfile *dwarf2_per_objfile
3337 = cu->per_cu->dwarf2_per_objfile;
3338 struct objfile *objfile = dwarf2_per_objfile->objfile;
3339 struct dwarf2_per_cu_data *lh_cu;
3340 struct attribute *attr;
3341 void **slot;
3342 struct quick_file_names *qfn;
3343
3344 gdb_assert (! this_cu->is_debug_types);
3345
3346 /* Our callers never want to match partial units -- instead they
3347 will match the enclosing full CU. */
3348 if (comp_unit_die->tag == DW_TAG_partial_unit)
3349 {
3350 this_cu->v.quick->no_file_data = 1;
3351 return;
3352 }
3353
3354 lh_cu = this_cu;
3355 slot = NULL;
3356
3357 line_header_up lh;
3358 sect_offset line_offset {};
3359
3360 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3361 if (attr != nullptr)
3362 {
3363 struct quick_file_names find_entry;
3364
3365 line_offset = (sect_offset) DW_UNSND (attr);
3366
3367 /* We may have already read in this line header (TU line header sharing).
3368 If we have we're done. */
3369 find_entry.hash.dwo_unit = cu->dwo_unit;
3370 find_entry.hash.line_sect_off = line_offset;
3371 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3372 &find_entry, INSERT);
3373 if (*slot != NULL)
3374 {
3375 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3376 return;
3377 }
3378
3379 lh = dwarf_decode_line_header (line_offset, cu);
3380 }
3381 if (lh == NULL)
3382 {
3383 lh_cu->v.quick->no_file_data = 1;
3384 return;
3385 }
3386
3387 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3388 qfn->hash.dwo_unit = cu->dwo_unit;
3389 qfn->hash.line_sect_off = line_offset;
3390 gdb_assert (slot != NULL);
3391 *slot = qfn;
3392
3393 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3394
3395 int offset = 0;
3396 if (strcmp (fnd.name, "<unknown>") != 0)
3397 ++offset;
3398
3399 qfn->num_file_names = offset + lh->file_names_size ();
3400 qfn->file_names =
3401 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3402 if (offset != 0)
3403 qfn->file_names[0] = xstrdup (fnd.name);
3404 for (int i = 0; i < lh->file_names_size (); ++i)
3405 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3406 qfn->real_names = NULL;
3407
3408 lh_cu->v.quick->file_names = qfn;
3409 }
3410
3411 /* A helper for the "quick" functions which attempts to read the line
3412 table for THIS_CU. */
3413
3414 static struct quick_file_names *
3415 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3416 {
3417 /* This should never be called for TUs. */
3418 gdb_assert (! this_cu->is_debug_types);
3419 /* Nor type unit groups. */
3420 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3421
3422 if (this_cu->v.quick->file_names != NULL)
3423 return this_cu->v.quick->file_names;
3424 /* If we know there is no line data, no point in looking again. */
3425 if (this_cu->v.quick->no_file_data)
3426 return NULL;
3427
3428 cutu_reader reader (this_cu);
3429 if (!reader.dummy_p)
3430 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3431
3432 if (this_cu->v.quick->no_file_data)
3433 return NULL;
3434 return this_cu->v.quick->file_names;
3435 }
3436
3437 /* A helper for the "quick" functions which computes and caches the
3438 real path for a given file name from the line table. */
3439
3440 static const char *
3441 dw2_get_real_path (struct objfile *objfile,
3442 struct quick_file_names *qfn, int index)
3443 {
3444 if (qfn->real_names == NULL)
3445 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3446 qfn->num_file_names, const char *);
3447
3448 if (qfn->real_names[index] == NULL)
3449 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3450
3451 return qfn->real_names[index];
3452 }
3453
3454 static struct symtab *
3455 dw2_find_last_source_symtab (struct objfile *objfile)
3456 {
3457 struct dwarf2_per_objfile *dwarf2_per_objfile
3458 = get_dwarf2_per_objfile (objfile);
3459 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3460 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3461
3462 if (cust == NULL)
3463 return NULL;
3464
3465 return compunit_primary_filetab (cust);
3466 }
3467
3468 /* Traversal function for dw2_forget_cached_source_info. */
3469
3470 static int
3471 dw2_free_cached_file_names (void **slot, void *info)
3472 {
3473 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3474
3475 if (file_data->real_names)
3476 {
3477 int i;
3478
3479 for (i = 0; i < file_data->num_file_names; ++i)
3480 {
3481 xfree ((void*) file_data->real_names[i]);
3482 file_data->real_names[i] = NULL;
3483 }
3484 }
3485
3486 return 1;
3487 }
3488
3489 static void
3490 dw2_forget_cached_source_info (struct objfile *objfile)
3491 {
3492 struct dwarf2_per_objfile *dwarf2_per_objfile
3493 = get_dwarf2_per_objfile (objfile);
3494
3495 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3496 dw2_free_cached_file_names, NULL);
3497 }
3498
3499 /* Helper function for dw2_map_symtabs_matching_filename that expands
3500 the symtabs and calls the iterator. */
3501
3502 static int
3503 dw2_map_expand_apply (struct objfile *objfile,
3504 struct dwarf2_per_cu_data *per_cu,
3505 const char *name, const char *real_path,
3506 gdb::function_view<bool (symtab *)> callback)
3507 {
3508 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3509
3510 /* Don't visit already-expanded CUs. */
3511 if (per_cu->v.quick->compunit_symtab)
3512 return 0;
3513
3514 /* This may expand more than one symtab, and we want to iterate over
3515 all of them. */
3516 dw2_instantiate_symtab (per_cu, false);
3517
3518 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3519 last_made, callback);
3520 }
3521
3522 /* Implementation of the map_symtabs_matching_filename method. */
3523
3524 static bool
3525 dw2_map_symtabs_matching_filename
3526 (struct objfile *objfile, const char *name, const char *real_path,
3527 gdb::function_view<bool (symtab *)> callback)
3528 {
3529 const char *name_basename = lbasename (name);
3530 struct dwarf2_per_objfile *dwarf2_per_objfile
3531 = get_dwarf2_per_objfile (objfile);
3532
3533 /* The rule is CUs specify all the files, including those used by
3534 any TU, so there's no need to scan TUs here. */
3535
3536 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3537 {
3538 /* We only need to look at symtabs not already expanded. */
3539 if (per_cu->v.quick->compunit_symtab)
3540 continue;
3541
3542 quick_file_names *file_data = dw2_get_file_names (per_cu);
3543 if (file_data == NULL)
3544 continue;
3545
3546 for (int j = 0; j < file_data->num_file_names; ++j)
3547 {
3548 const char *this_name = file_data->file_names[j];
3549 const char *this_real_name;
3550
3551 if (compare_filenames_for_search (this_name, name))
3552 {
3553 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3554 callback))
3555 return true;
3556 continue;
3557 }
3558
3559 /* Before we invoke realpath, which can get expensive when many
3560 files are involved, do a quick comparison of the basenames. */
3561 if (! basenames_may_differ
3562 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3563 continue;
3564
3565 this_real_name = dw2_get_real_path (objfile, file_data, j);
3566 if (compare_filenames_for_search (this_real_name, name))
3567 {
3568 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3569 callback))
3570 return true;
3571 continue;
3572 }
3573
3574 if (real_path != NULL)
3575 {
3576 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3577 gdb_assert (IS_ABSOLUTE_PATH (name));
3578 if (this_real_name != NULL
3579 && FILENAME_CMP (real_path, this_real_name) == 0)
3580 {
3581 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3582 callback))
3583 return true;
3584 continue;
3585 }
3586 }
3587 }
3588 }
3589
3590 return false;
3591 }
3592
3593 /* Struct used to manage iterating over all CUs looking for a symbol. */
3594
3595 struct dw2_symtab_iterator
3596 {
3597 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3598 struct dwarf2_per_objfile *dwarf2_per_objfile;
3599 /* If set, only look for symbols that match that block. Valid values are
3600 GLOBAL_BLOCK and STATIC_BLOCK. */
3601 gdb::optional<block_enum> block_index;
3602 /* The kind of symbol we're looking for. */
3603 domain_enum domain;
3604 /* The list of CUs from the index entry of the symbol,
3605 or NULL if not found. */
3606 offset_type *vec;
3607 /* The next element in VEC to look at. */
3608 int next;
3609 /* The number of elements in VEC, or zero if there is no match. */
3610 int length;
3611 /* Have we seen a global version of the symbol?
3612 If so we can ignore all further global instances.
3613 This is to work around gold/15646, inefficient gold-generated
3614 indices. */
3615 int global_seen;
3616 };
3617
3618 /* Initialize the index symtab iterator ITER. */
3619
3620 static void
3621 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3622 struct dwarf2_per_objfile *dwarf2_per_objfile,
3623 gdb::optional<block_enum> block_index,
3624 domain_enum domain,
3625 const char *name)
3626 {
3627 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3628 iter->block_index = block_index;
3629 iter->domain = domain;
3630 iter->next = 0;
3631 iter->global_seen = 0;
3632
3633 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3634
3635 /* index is NULL if OBJF_READNOW. */
3636 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3637 iter->length = MAYBE_SWAP (*iter->vec);
3638 else
3639 {
3640 iter->vec = NULL;
3641 iter->length = 0;
3642 }
3643 }
3644
3645 /* Return the next matching CU or NULL if there are no more. */
3646
3647 static struct dwarf2_per_cu_data *
3648 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3649 {
3650 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3651
3652 for ( ; iter->next < iter->length; ++iter->next)
3653 {
3654 offset_type cu_index_and_attrs =
3655 MAYBE_SWAP (iter->vec[iter->next + 1]);
3656 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3657 gdb_index_symbol_kind symbol_kind =
3658 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3659 /* Only check the symbol attributes if they're present.
3660 Indices prior to version 7 don't record them,
3661 and indices >= 7 may elide them for certain symbols
3662 (gold does this). */
3663 int attrs_valid =
3664 (dwarf2_per_objfile->index_table->version >= 7
3665 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3666
3667 /* Don't crash on bad data. */
3668 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3669 + dwarf2_per_objfile->all_type_units.size ()))
3670 {
3671 complaint (_(".gdb_index entry has bad CU index"
3672 " [in module %s]"),
3673 objfile_name (dwarf2_per_objfile->objfile));
3674 continue;
3675 }
3676
3677 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3678
3679 /* Skip if already read in. */
3680 if (per_cu->v.quick->compunit_symtab)
3681 continue;
3682
3683 /* Check static vs global. */
3684 if (attrs_valid)
3685 {
3686 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3687
3688 if (iter->block_index.has_value ())
3689 {
3690 bool want_static = *iter->block_index == STATIC_BLOCK;
3691
3692 if (is_static != want_static)
3693 continue;
3694 }
3695
3696 /* Work around gold/15646. */
3697 if (!is_static && iter->global_seen)
3698 continue;
3699 if (!is_static)
3700 iter->global_seen = 1;
3701 }
3702
3703 /* Only check the symbol's kind if it has one. */
3704 if (attrs_valid)
3705 {
3706 switch (iter->domain)
3707 {
3708 case VAR_DOMAIN:
3709 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3710 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3711 /* Some types are also in VAR_DOMAIN. */
3712 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3713 continue;
3714 break;
3715 case STRUCT_DOMAIN:
3716 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3717 continue;
3718 break;
3719 case LABEL_DOMAIN:
3720 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3721 continue;
3722 break;
3723 case MODULE_DOMAIN:
3724 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3725 continue;
3726 break;
3727 default:
3728 break;
3729 }
3730 }
3731
3732 ++iter->next;
3733 return per_cu;
3734 }
3735
3736 return NULL;
3737 }
3738
3739 static struct compunit_symtab *
3740 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3741 const char *name, domain_enum domain)
3742 {
3743 struct compunit_symtab *stab_best = NULL;
3744 struct dwarf2_per_objfile *dwarf2_per_objfile
3745 = get_dwarf2_per_objfile (objfile);
3746
3747 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3748
3749 struct dw2_symtab_iterator iter;
3750 struct dwarf2_per_cu_data *per_cu;
3751
3752 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3753
3754 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3755 {
3756 struct symbol *sym, *with_opaque = NULL;
3757 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3758 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3759 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3760
3761 sym = block_find_symbol (block, name, domain,
3762 block_find_non_opaque_type_preferred,
3763 &with_opaque);
3764
3765 /* Some caution must be observed with overloaded functions
3766 and methods, since the index will not contain any overload
3767 information (but NAME might contain it). */
3768
3769 if (sym != NULL
3770 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3771 return stab;
3772 if (with_opaque != NULL
3773 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3774 stab_best = stab;
3775
3776 /* Keep looking through other CUs. */
3777 }
3778
3779 return stab_best;
3780 }
3781
3782 static void
3783 dw2_print_stats (struct objfile *objfile)
3784 {
3785 struct dwarf2_per_objfile *dwarf2_per_objfile
3786 = get_dwarf2_per_objfile (objfile);
3787 int total = (dwarf2_per_objfile->all_comp_units.size ()
3788 + dwarf2_per_objfile->all_type_units.size ());
3789 int count = 0;
3790
3791 for (int i = 0; i < total; ++i)
3792 {
3793 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3794
3795 if (!per_cu->v.quick->compunit_symtab)
3796 ++count;
3797 }
3798 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3799 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3800 }
3801
3802 /* This dumps minimal information about the index.
3803 It is called via "mt print objfiles".
3804 One use is to verify .gdb_index has been loaded by the
3805 gdb.dwarf2/gdb-index.exp testcase. */
3806
3807 static void
3808 dw2_dump (struct objfile *objfile)
3809 {
3810 struct dwarf2_per_objfile *dwarf2_per_objfile
3811 = get_dwarf2_per_objfile (objfile);
3812
3813 gdb_assert (dwarf2_per_objfile->using_index);
3814 printf_filtered (".gdb_index:");
3815 if (dwarf2_per_objfile->index_table != NULL)
3816 {
3817 printf_filtered (" version %d\n",
3818 dwarf2_per_objfile->index_table->version);
3819 }
3820 else
3821 printf_filtered (" faked for \"readnow\"\n");
3822 printf_filtered ("\n");
3823 }
3824
3825 static void
3826 dw2_expand_symtabs_for_function (struct objfile *objfile,
3827 const char *func_name)
3828 {
3829 struct dwarf2_per_objfile *dwarf2_per_objfile
3830 = get_dwarf2_per_objfile (objfile);
3831
3832 struct dw2_symtab_iterator iter;
3833 struct dwarf2_per_cu_data *per_cu;
3834
3835 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3836
3837 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3838 dw2_instantiate_symtab (per_cu, false);
3839
3840 }
3841
3842 static void
3843 dw2_expand_all_symtabs (struct objfile *objfile)
3844 {
3845 struct dwarf2_per_objfile *dwarf2_per_objfile
3846 = get_dwarf2_per_objfile (objfile);
3847 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3848 + dwarf2_per_objfile->all_type_units.size ());
3849
3850 for (int i = 0; i < total_units; ++i)
3851 {
3852 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3853
3854 /* We don't want to directly expand a partial CU, because if we
3855 read it with the wrong language, then assertion failures can
3856 be triggered later on. See PR symtab/23010. So, tell
3857 dw2_instantiate_symtab to skip partial CUs -- any important
3858 partial CU will be read via DW_TAG_imported_unit anyway. */
3859 dw2_instantiate_symtab (per_cu, true);
3860 }
3861 }
3862
3863 static void
3864 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3865 const char *fullname)
3866 {
3867 struct dwarf2_per_objfile *dwarf2_per_objfile
3868 = get_dwarf2_per_objfile (objfile);
3869
3870 /* We don't need to consider type units here.
3871 This is only called for examining code, e.g. expand_line_sal.
3872 There can be an order of magnitude (or more) more type units
3873 than comp units, and we avoid them if we can. */
3874
3875 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3876 {
3877 /* We only need to look at symtabs not already expanded. */
3878 if (per_cu->v.quick->compunit_symtab)
3879 continue;
3880
3881 quick_file_names *file_data = dw2_get_file_names (per_cu);
3882 if (file_data == NULL)
3883 continue;
3884
3885 for (int j = 0; j < file_data->num_file_names; ++j)
3886 {
3887 const char *this_fullname = file_data->file_names[j];
3888
3889 if (filename_cmp (this_fullname, fullname) == 0)
3890 {
3891 dw2_instantiate_symtab (per_cu, false);
3892 break;
3893 }
3894 }
3895 }
3896 }
3897
3898 static void
3899 dw2_map_matching_symbols
3900 (struct objfile *objfile,
3901 const lookup_name_info &name, domain_enum domain,
3902 int global,
3903 gdb::function_view<symbol_found_callback_ftype> callback,
3904 symbol_compare_ftype *ordered_compare)
3905 {
3906 /* Currently unimplemented; used for Ada. The function can be called if the
3907 current language is Ada for a non-Ada objfile using GNU index. As Ada
3908 does not look for non-Ada symbols this function should just return. */
3909 }
3910
3911 /* Starting from a search name, return the string that finds the upper
3912 bound of all strings that start with SEARCH_NAME in a sorted name
3913 list. Returns the empty string to indicate that the upper bound is
3914 the end of the list. */
3915
3916 static std::string
3917 make_sort_after_prefix_name (const char *search_name)
3918 {
3919 /* When looking to complete "func", we find the upper bound of all
3920 symbols that start with "func" by looking for where we'd insert
3921 the closest string that would follow "func" in lexicographical
3922 order. Usually, that's "func"-with-last-character-incremented,
3923 i.e. "fund". Mind non-ASCII characters, though. Usually those
3924 will be UTF-8 multi-byte sequences, but we can't be certain.
3925 Especially mind the 0xff character, which is a valid character in
3926 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3927 rule out compilers allowing it in identifiers. Note that
3928 conveniently, strcmp/strcasecmp are specified to compare
3929 characters interpreted as unsigned char. So what we do is treat
3930 the whole string as a base 256 number composed of a sequence of
3931 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3932 to 0, and carries 1 to the following more-significant position.
3933 If the very first character in SEARCH_NAME ends up incremented
3934 and carries/overflows, then the upper bound is the end of the
3935 list. The string after the empty string is also the empty
3936 string.
3937
3938 Some examples of this operation:
3939
3940 SEARCH_NAME => "+1" RESULT
3941
3942 "abc" => "abd"
3943 "ab\xff" => "ac"
3944 "\xff" "a" "\xff" => "\xff" "b"
3945 "\xff" => ""
3946 "\xff\xff" => ""
3947 "" => ""
3948
3949 Then, with these symbols for example:
3950
3951 func
3952 func1
3953 fund
3954
3955 completing "func" looks for symbols between "func" and
3956 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3957 which finds "func" and "func1", but not "fund".
3958
3959 And with:
3960
3961 funcÿ (Latin1 'ÿ' [0xff])
3962 funcÿ1
3963 fund
3964
3965 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3966 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3967
3968 And with:
3969
3970 ÿÿ (Latin1 'ÿ' [0xff])
3971 ÿÿ1
3972
3973 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3974 the end of the list.
3975 */
3976 std::string after = search_name;
3977 while (!after.empty () && (unsigned char) after.back () == 0xff)
3978 after.pop_back ();
3979 if (!after.empty ())
3980 after.back () = (unsigned char) after.back () + 1;
3981 return after;
3982 }
3983
3984 /* See declaration. */
3985
3986 std::pair<std::vector<name_component>::const_iterator,
3987 std::vector<name_component>::const_iterator>
3988 mapped_index_base::find_name_components_bounds
3989 (const lookup_name_info &lookup_name_without_params, language lang) const
3990 {
3991 auto *name_cmp
3992 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3993
3994 const char *lang_name
3995 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3996
3997 /* Comparison function object for lower_bound that matches against a
3998 given symbol name. */
3999 auto lookup_compare_lower = [&] (const name_component &elem,
4000 const char *name)
4001 {
4002 const char *elem_qualified = this->symbol_name_at (elem.idx);
4003 const char *elem_name = elem_qualified + elem.name_offset;
4004 return name_cmp (elem_name, name) < 0;
4005 };
4006
4007 /* Comparison function object for upper_bound that matches against a
4008 given symbol name. */
4009 auto lookup_compare_upper = [&] (const char *name,
4010 const name_component &elem)
4011 {
4012 const char *elem_qualified = this->symbol_name_at (elem.idx);
4013 const char *elem_name = elem_qualified + elem.name_offset;
4014 return name_cmp (name, elem_name) < 0;
4015 };
4016
4017 auto begin = this->name_components.begin ();
4018 auto end = this->name_components.end ();
4019
4020 /* Find the lower bound. */
4021 auto lower = [&] ()
4022 {
4023 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4024 return begin;
4025 else
4026 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4027 } ();
4028
4029 /* Find the upper bound. */
4030 auto upper = [&] ()
4031 {
4032 if (lookup_name_without_params.completion_mode ())
4033 {
4034 /* In completion mode, we want UPPER to point past all
4035 symbols names that have the same prefix. I.e., with
4036 these symbols, and completing "func":
4037
4038 function << lower bound
4039 function1
4040 other_function << upper bound
4041
4042 We find the upper bound by looking for the insertion
4043 point of "func"-with-last-character-incremented,
4044 i.e. "fund". */
4045 std::string after = make_sort_after_prefix_name (lang_name);
4046 if (after.empty ())
4047 return end;
4048 return std::lower_bound (lower, end, after.c_str (),
4049 lookup_compare_lower);
4050 }
4051 else
4052 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4053 } ();
4054
4055 return {lower, upper};
4056 }
4057
4058 /* See declaration. */
4059
4060 void
4061 mapped_index_base::build_name_components ()
4062 {
4063 if (!this->name_components.empty ())
4064 return;
4065
4066 this->name_components_casing = case_sensitivity;
4067 auto *name_cmp
4068 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4069
4070 /* The code below only knows how to break apart components of C++
4071 symbol names (and other languages that use '::' as
4072 namespace/module separator) and Ada symbol names. */
4073 auto count = this->symbol_name_count ();
4074 for (offset_type idx = 0; idx < count; idx++)
4075 {
4076 if (this->symbol_name_slot_invalid (idx))
4077 continue;
4078
4079 const char *name = this->symbol_name_at (idx);
4080
4081 /* Add each name component to the name component table. */
4082 unsigned int previous_len = 0;
4083
4084 if (strstr (name, "::") != nullptr)
4085 {
4086 for (unsigned int current_len = cp_find_first_component (name);
4087 name[current_len] != '\0';
4088 current_len += cp_find_first_component (name + current_len))
4089 {
4090 gdb_assert (name[current_len] == ':');
4091 this->name_components.push_back ({previous_len, idx});
4092 /* Skip the '::'. */
4093 current_len += 2;
4094 previous_len = current_len;
4095 }
4096 }
4097 else
4098 {
4099 /* Handle the Ada encoded (aka mangled) form here. */
4100 for (const char *iter = strstr (name, "__");
4101 iter != nullptr;
4102 iter = strstr (iter, "__"))
4103 {
4104 this->name_components.push_back ({previous_len, idx});
4105 iter += 2;
4106 previous_len = iter - name;
4107 }
4108 }
4109
4110 this->name_components.push_back ({previous_len, idx});
4111 }
4112
4113 /* Sort name_components elements by name. */
4114 auto name_comp_compare = [&] (const name_component &left,
4115 const name_component &right)
4116 {
4117 const char *left_qualified = this->symbol_name_at (left.idx);
4118 const char *right_qualified = this->symbol_name_at (right.idx);
4119
4120 const char *left_name = left_qualified + left.name_offset;
4121 const char *right_name = right_qualified + right.name_offset;
4122
4123 return name_cmp (left_name, right_name) < 0;
4124 };
4125
4126 std::sort (this->name_components.begin (),
4127 this->name_components.end (),
4128 name_comp_compare);
4129 }
4130
4131 /* Helper for dw2_expand_symtabs_matching that works with a
4132 mapped_index_base instead of the containing objfile. This is split
4133 to a separate function in order to be able to unit test the
4134 name_components matching using a mock mapped_index_base. For each
4135 symbol name that matches, calls MATCH_CALLBACK, passing it the
4136 symbol's index in the mapped_index_base symbol table. */
4137
4138 static void
4139 dw2_expand_symtabs_matching_symbol
4140 (mapped_index_base &index,
4141 const lookup_name_info &lookup_name_in,
4142 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4143 enum search_domain kind,
4144 gdb::function_view<bool (offset_type)> match_callback)
4145 {
4146 lookup_name_info lookup_name_without_params
4147 = lookup_name_in.make_ignore_params ();
4148
4149 /* Build the symbol name component sorted vector, if we haven't
4150 yet. */
4151 index.build_name_components ();
4152
4153 /* The same symbol may appear more than once in the range though.
4154 E.g., if we're looking for symbols that complete "w", and we have
4155 a symbol named "w1::w2", we'll find the two name components for
4156 that same symbol in the range. To be sure we only call the
4157 callback once per symbol, we first collect the symbol name
4158 indexes that matched in a temporary vector and ignore
4159 duplicates. */
4160 std::vector<offset_type> matches;
4161
4162 struct name_and_matcher
4163 {
4164 symbol_name_matcher_ftype *matcher;
4165 const std::string &name;
4166
4167 bool operator== (const name_and_matcher &other) const
4168 {
4169 return matcher == other.matcher && name == other.name;
4170 }
4171 };
4172
4173 /* A vector holding all the different symbol name matchers, for all
4174 languages. */
4175 std::vector<name_and_matcher> matchers;
4176
4177 for (int i = 0; i < nr_languages; i++)
4178 {
4179 enum language lang_e = (enum language) i;
4180
4181 const language_defn *lang = language_def (lang_e);
4182 symbol_name_matcher_ftype *name_matcher
4183 = get_symbol_name_matcher (lang, lookup_name_without_params);
4184
4185 name_and_matcher key {
4186 name_matcher,
4187 lookup_name_without_params.language_lookup_name (lang_e)
4188 };
4189
4190 /* Don't insert the same comparison routine more than once.
4191 Note that we do this linear walk. This is not a problem in
4192 practice because the number of supported languages is
4193 low. */
4194 if (std::find (matchers.begin (), matchers.end (), key)
4195 != matchers.end ())
4196 continue;
4197 matchers.push_back (std::move (key));
4198
4199 auto bounds
4200 = index.find_name_components_bounds (lookup_name_without_params,
4201 lang_e);
4202
4203 /* Now for each symbol name in range, check to see if we have a name
4204 match, and if so, call the MATCH_CALLBACK callback. */
4205
4206 for (; bounds.first != bounds.second; ++bounds.first)
4207 {
4208 const char *qualified = index.symbol_name_at (bounds.first->idx);
4209
4210 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4211 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4212 continue;
4213
4214 matches.push_back (bounds.first->idx);
4215 }
4216 }
4217
4218 std::sort (matches.begin (), matches.end ());
4219
4220 /* Finally call the callback, once per match. */
4221 ULONGEST prev = -1;
4222 for (offset_type idx : matches)
4223 {
4224 if (prev != idx)
4225 {
4226 if (!match_callback (idx))
4227 break;
4228 prev = idx;
4229 }
4230 }
4231
4232 /* Above we use a type wider than idx's for 'prev', since 0 and
4233 (offset_type)-1 are both possible values. */
4234 static_assert (sizeof (prev) > sizeof (offset_type), "");
4235 }
4236
4237 #if GDB_SELF_TEST
4238
4239 namespace selftests { namespace dw2_expand_symtabs_matching {
4240
4241 /* A mock .gdb_index/.debug_names-like name index table, enough to
4242 exercise dw2_expand_symtabs_matching_symbol, which works with the
4243 mapped_index_base interface. Builds an index from the symbol list
4244 passed as parameter to the constructor. */
4245 class mock_mapped_index : public mapped_index_base
4246 {
4247 public:
4248 mock_mapped_index (gdb::array_view<const char *> symbols)
4249 : m_symbol_table (symbols)
4250 {}
4251
4252 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4253
4254 /* Return the number of names in the symbol table. */
4255 size_t symbol_name_count () const override
4256 {
4257 return m_symbol_table.size ();
4258 }
4259
4260 /* Get the name of the symbol at IDX in the symbol table. */
4261 const char *symbol_name_at (offset_type idx) const override
4262 {
4263 return m_symbol_table[idx];
4264 }
4265
4266 private:
4267 gdb::array_view<const char *> m_symbol_table;
4268 };
4269
4270 /* Convenience function that converts a NULL pointer to a "<null>"
4271 string, to pass to print routines. */
4272
4273 static const char *
4274 string_or_null (const char *str)
4275 {
4276 return str != NULL ? str : "<null>";
4277 }
4278
4279 /* Check if a lookup_name_info built from
4280 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4281 index. EXPECTED_LIST is the list of expected matches, in expected
4282 matching order. If no match expected, then an empty list is
4283 specified. Returns true on success. On failure prints a warning
4284 indicating the file:line that failed, and returns false. */
4285
4286 static bool
4287 check_match (const char *file, int line,
4288 mock_mapped_index &mock_index,
4289 const char *name, symbol_name_match_type match_type,
4290 bool completion_mode,
4291 std::initializer_list<const char *> expected_list)
4292 {
4293 lookup_name_info lookup_name (name, match_type, completion_mode);
4294
4295 bool matched = true;
4296
4297 auto mismatch = [&] (const char *expected_str,
4298 const char *got)
4299 {
4300 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4301 "expected=\"%s\", got=\"%s\"\n"),
4302 file, line,
4303 (match_type == symbol_name_match_type::FULL
4304 ? "FULL" : "WILD"),
4305 name, string_or_null (expected_str), string_or_null (got));
4306 matched = false;
4307 };
4308
4309 auto expected_it = expected_list.begin ();
4310 auto expected_end = expected_list.end ();
4311
4312 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4313 NULL, ALL_DOMAIN,
4314 [&] (offset_type idx)
4315 {
4316 const char *matched_name = mock_index.symbol_name_at (idx);
4317 const char *expected_str
4318 = expected_it == expected_end ? NULL : *expected_it++;
4319
4320 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4321 mismatch (expected_str, matched_name);
4322 return true;
4323 });
4324
4325 const char *expected_str
4326 = expected_it == expected_end ? NULL : *expected_it++;
4327 if (expected_str != NULL)
4328 mismatch (expected_str, NULL);
4329
4330 return matched;
4331 }
4332
4333 /* The symbols added to the mock mapped_index for testing (in
4334 canonical form). */
4335 static const char *test_symbols[] = {
4336 "function",
4337 "std::bar",
4338 "std::zfunction",
4339 "std::zfunction2",
4340 "w1::w2",
4341 "ns::foo<char*>",
4342 "ns::foo<int>",
4343 "ns::foo<long>",
4344 "ns2::tmpl<int>::foo2",
4345 "(anonymous namespace)::A::B::C",
4346
4347 /* These are used to check that the increment-last-char in the
4348 matching algorithm for completion doesn't match "t1_fund" when
4349 completing "t1_func". */
4350 "t1_func",
4351 "t1_func1",
4352 "t1_fund",
4353 "t1_fund1",
4354
4355 /* A UTF-8 name with multi-byte sequences to make sure that
4356 cp-name-parser understands this as a single identifier ("função"
4357 is "function" in PT). */
4358 u8"u8função",
4359
4360 /* \377 (0xff) is Latin1 'ÿ'. */
4361 "yfunc\377",
4362
4363 /* \377 (0xff) is Latin1 'ÿ'. */
4364 "\377",
4365 "\377\377123",
4366
4367 /* A name with all sorts of complications. Starts with "z" to make
4368 it easier for the completion tests below. */
4369 #define Z_SYM_NAME \
4370 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4371 "::tuple<(anonymous namespace)::ui*, " \
4372 "std::default_delete<(anonymous namespace)::ui>, void>"
4373
4374 Z_SYM_NAME
4375 };
4376
4377 /* Returns true if the mapped_index_base::find_name_component_bounds
4378 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4379 in completion mode. */
4380
4381 static bool
4382 check_find_bounds_finds (mapped_index_base &index,
4383 const char *search_name,
4384 gdb::array_view<const char *> expected_syms)
4385 {
4386 lookup_name_info lookup_name (search_name,
4387 symbol_name_match_type::FULL, true);
4388
4389 auto bounds = index.find_name_components_bounds (lookup_name,
4390 language_cplus);
4391
4392 size_t distance = std::distance (bounds.first, bounds.second);
4393 if (distance != expected_syms.size ())
4394 return false;
4395
4396 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4397 {
4398 auto nc_elem = bounds.first + exp_elem;
4399 const char *qualified = index.symbol_name_at (nc_elem->idx);
4400 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4401 return false;
4402 }
4403
4404 return true;
4405 }
4406
4407 /* Test the lower-level mapped_index::find_name_component_bounds
4408 method. */
4409
4410 static void
4411 test_mapped_index_find_name_component_bounds ()
4412 {
4413 mock_mapped_index mock_index (test_symbols);
4414
4415 mock_index.build_name_components ();
4416
4417 /* Test the lower-level mapped_index::find_name_component_bounds
4418 method in completion mode. */
4419 {
4420 static const char *expected_syms[] = {
4421 "t1_func",
4422 "t1_func1",
4423 };
4424
4425 SELF_CHECK (check_find_bounds_finds (mock_index,
4426 "t1_func", expected_syms));
4427 }
4428
4429 /* Check that the increment-last-char in the name matching algorithm
4430 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4431 {
4432 static const char *expected_syms1[] = {
4433 "\377",
4434 "\377\377123",
4435 };
4436 SELF_CHECK (check_find_bounds_finds (mock_index,
4437 "\377", expected_syms1));
4438
4439 static const char *expected_syms2[] = {
4440 "\377\377123",
4441 };
4442 SELF_CHECK (check_find_bounds_finds (mock_index,
4443 "\377\377", expected_syms2));
4444 }
4445 }
4446
4447 /* Test dw2_expand_symtabs_matching_symbol. */
4448
4449 static void
4450 test_dw2_expand_symtabs_matching_symbol ()
4451 {
4452 mock_mapped_index mock_index (test_symbols);
4453
4454 /* We let all tests run until the end even if some fails, for debug
4455 convenience. */
4456 bool any_mismatch = false;
4457
4458 /* Create the expected symbols list (an initializer_list). Needed
4459 because lists have commas, and we need to pass them to CHECK,
4460 which is a macro. */
4461 #define EXPECT(...) { __VA_ARGS__ }
4462
4463 /* Wrapper for check_match that passes down the current
4464 __FILE__/__LINE__. */
4465 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4466 any_mismatch |= !check_match (__FILE__, __LINE__, \
4467 mock_index, \
4468 NAME, MATCH_TYPE, COMPLETION_MODE, \
4469 EXPECTED_LIST)
4470
4471 /* Identity checks. */
4472 for (const char *sym : test_symbols)
4473 {
4474 /* Should be able to match all existing symbols. */
4475 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4476 EXPECT (sym));
4477
4478 /* Should be able to match all existing symbols with
4479 parameters. */
4480 std::string with_params = std::string (sym) + "(int)";
4481 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4482 EXPECT (sym));
4483
4484 /* Should be able to match all existing symbols with
4485 parameters and qualifiers. */
4486 with_params = std::string (sym) + " ( int ) const";
4487 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4488 EXPECT (sym));
4489
4490 /* This should really find sym, but cp-name-parser.y doesn't
4491 know about lvalue/rvalue qualifiers yet. */
4492 with_params = std::string (sym) + " ( int ) &&";
4493 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4494 {});
4495 }
4496
4497 /* Check that the name matching algorithm for completion doesn't get
4498 confused with Latin1 'ÿ' / 0xff. */
4499 {
4500 static const char str[] = "\377";
4501 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4502 EXPECT ("\377", "\377\377123"));
4503 }
4504
4505 /* Check that the increment-last-char in the matching algorithm for
4506 completion doesn't match "t1_fund" when completing "t1_func". */
4507 {
4508 static const char str[] = "t1_func";
4509 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4510 EXPECT ("t1_func", "t1_func1"));
4511 }
4512
4513 /* Check that completion mode works at each prefix of the expected
4514 symbol name. */
4515 {
4516 static const char str[] = "function(int)";
4517 size_t len = strlen (str);
4518 std::string lookup;
4519
4520 for (size_t i = 1; i < len; i++)
4521 {
4522 lookup.assign (str, i);
4523 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4524 EXPECT ("function"));
4525 }
4526 }
4527
4528 /* While "w" is a prefix of both components, the match function
4529 should still only be called once. */
4530 {
4531 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4532 EXPECT ("w1::w2"));
4533 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4534 EXPECT ("w1::w2"));
4535 }
4536
4537 /* Same, with a "complicated" symbol. */
4538 {
4539 static const char str[] = Z_SYM_NAME;
4540 size_t len = strlen (str);
4541 std::string lookup;
4542
4543 for (size_t i = 1; i < len; i++)
4544 {
4545 lookup.assign (str, i);
4546 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4547 EXPECT (Z_SYM_NAME));
4548 }
4549 }
4550
4551 /* In FULL mode, an incomplete symbol doesn't match. */
4552 {
4553 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4554 {});
4555 }
4556
4557 /* A complete symbol with parameters matches any overload, since the
4558 index has no overload info. */
4559 {
4560 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4561 EXPECT ("std::zfunction", "std::zfunction2"));
4562 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4563 EXPECT ("std::zfunction", "std::zfunction2"));
4564 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4565 EXPECT ("std::zfunction", "std::zfunction2"));
4566 }
4567
4568 /* Check that whitespace is ignored appropriately. A symbol with a
4569 template argument list. */
4570 {
4571 static const char expected[] = "ns::foo<int>";
4572 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4573 EXPECT (expected));
4574 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4575 EXPECT (expected));
4576 }
4577
4578 /* Check that whitespace is ignored appropriately. A symbol with a
4579 template argument list that includes a pointer. */
4580 {
4581 static const char expected[] = "ns::foo<char*>";
4582 /* Try both completion and non-completion modes. */
4583 static const bool completion_mode[2] = {false, true};
4584 for (size_t i = 0; i < 2; i++)
4585 {
4586 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4587 completion_mode[i], EXPECT (expected));
4588 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4589 completion_mode[i], EXPECT (expected));
4590
4591 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4592 completion_mode[i], EXPECT (expected));
4593 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4594 completion_mode[i], EXPECT (expected));
4595 }
4596 }
4597
4598 {
4599 /* Check method qualifiers are ignored. */
4600 static const char expected[] = "ns::foo<char*>";
4601 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4602 symbol_name_match_type::FULL, true, EXPECT (expected));
4603 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4604 symbol_name_match_type::FULL, true, EXPECT (expected));
4605 CHECK_MATCH ("foo < char * > ( int ) const",
4606 symbol_name_match_type::WILD, true, EXPECT (expected));
4607 CHECK_MATCH ("foo < char * > ( int ) &&",
4608 symbol_name_match_type::WILD, true, EXPECT (expected));
4609 }
4610
4611 /* Test lookup names that don't match anything. */
4612 {
4613 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4614 {});
4615
4616 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4617 {});
4618 }
4619
4620 /* Some wild matching tests, exercising "(anonymous namespace)",
4621 which should not be confused with a parameter list. */
4622 {
4623 static const char *syms[] = {
4624 "A::B::C",
4625 "B::C",
4626 "C",
4627 "A :: B :: C ( int )",
4628 "B :: C ( int )",
4629 "C ( int )",
4630 };
4631
4632 for (const char *s : syms)
4633 {
4634 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4635 EXPECT ("(anonymous namespace)::A::B::C"));
4636 }
4637 }
4638
4639 {
4640 static const char expected[] = "ns2::tmpl<int>::foo2";
4641 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4642 EXPECT (expected));
4643 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4644 EXPECT (expected));
4645 }
4646
4647 SELF_CHECK (!any_mismatch);
4648
4649 #undef EXPECT
4650 #undef CHECK_MATCH
4651 }
4652
4653 static void
4654 run_test ()
4655 {
4656 test_mapped_index_find_name_component_bounds ();
4657 test_dw2_expand_symtabs_matching_symbol ();
4658 }
4659
4660 }} // namespace selftests::dw2_expand_symtabs_matching
4661
4662 #endif /* GDB_SELF_TEST */
4663
4664 /* If FILE_MATCHER is NULL or if PER_CU has
4665 dwarf2_per_cu_quick_data::MARK set (see
4666 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4667 EXPANSION_NOTIFY on it. */
4668
4669 static void
4670 dw2_expand_symtabs_matching_one
4671 (struct dwarf2_per_cu_data *per_cu,
4672 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4673 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4674 {
4675 if (file_matcher == NULL || per_cu->v.quick->mark)
4676 {
4677 bool symtab_was_null
4678 = (per_cu->v.quick->compunit_symtab == NULL);
4679
4680 dw2_instantiate_symtab (per_cu, false);
4681
4682 if (expansion_notify != NULL
4683 && symtab_was_null
4684 && per_cu->v.quick->compunit_symtab != NULL)
4685 expansion_notify (per_cu->v.quick->compunit_symtab);
4686 }
4687 }
4688
4689 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4690 matched, to expand corresponding CUs that were marked. IDX is the
4691 index of the symbol name that matched. */
4692
4693 static void
4694 dw2_expand_marked_cus
4695 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4696 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4697 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4698 search_domain kind)
4699 {
4700 offset_type *vec, vec_len, vec_idx;
4701 bool global_seen = false;
4702 mapped_index &index = *dwarf2_per_objfile->index_table;
4703
4704 vec = (offset_type *) (index.constant_pool
4705 + MAYBE_SWAP (index.symbol_table[idx].vec));
4706 vec_len = MAYBE_SWAP (vec[0]);
4707 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4708 {
4709 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4710 /* This value is only valid for index versions >= 7. */
4711 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4712 gdb_index_symbol_kind symbol_kind =
4713 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4714 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4715 /* Only check the symbol attributes if they're present.
4716 Indices prior to version 7 don't record them,
4717 and indices >= 7 may elide them for certain symbols
4718 (gold does this). */
4719 int attrs_valid =
4720 (index.version >= 7
4721 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4722
4723 /* Work around gold/15646. */
4724 if (attrs_valid)
4725 {
4726 if (!is_static && global_seen)
4727 continue;
4728 if (!is_static)
4729 global_seen = true;
4730 }
4731
4732 /* Only check the symbol's kind if it has one. */
4733 if (attrs_valid)
4734 {
4735 switch (kind)
4736 {
4737 case VARIABLES_DOMAIN:
4738 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4739 continue;
4740 break;
4741 case FUNCTIONS_DOMAIN:
4742 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4743 continue;
4744 break;
4745 case TYPES_DOMAIN:
4746 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4747 continue;
4748 break;
4749 case MODULES_DOMAIN:
4750 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4751 continue;
4752 break;
4753 default:
4754 break;
4755 }
4756 }
4757
4758 /* Don't crash on bad data. */
4759 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4760 + dwarf2_per_objfile->all_type_units.size ()))
4761 {
4762 complaint (_(".gdb_index entry has bad CU index"
4763 " [in module %s]"),
4764 objfile_name (dwarf2_per_objfile->objfile));
4765 continue;
4766 }
4767
4768 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4769 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4770 expansion_notify);
4771 }
4772 }
4773
4774 /* If FILE_MATCHER is non-NULL, set all the
4775 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4776 that match FILE_MATCHER. */
4777
4778 static void
4779 dw_expand_symtabs_matching_file_matcher
4780 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4781 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4782 {
4783 if (file_matcher == NULL)
4784 return;
4785
4786 objfile *const objfile = dwarf2_per_objfile->objfile;
4787
4788 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4789 htab_eq_pointer,
4790 NULL, xcalloc, xfree));
4791 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4792 htab_eq_pointer,
4793 NULL, xcalloc, xfree));
4794
4795 /* The rule is CUs specify all the files, including those used by
4796 any TU, so there's no need to scan TUs here. */
4797
4798 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4799 {
4800 QUIT;
4801
4802 per_cu->v.quick->mark = 0;
4803
4804 /* We only need to look at symtabs not already expanded. */
4805 if (per_cu->v.quick->compunit_symtab)
4806 continue;
4807
4808 quick_file_names *file_data = dw2_get_file_names (per_cu);
4809 if (file_data == NULL)
4810 continue;
4811
4812 if (htab_find (visited_not_found.get (), file_data) != NULL)
4813 continue;
4814 else if (htab_find (visited_found.get (), file_data) != NULL)
4815 {
4816 per_cu->v.quick->mark = 1;
4817 continue;
4818 }
4819
4820 for (int j = 0; j < file_data->num_file_names; ++j)
4821 {
4822 const char *this_real_name;
4823
4824 if (file_matcher (file_data->file_names[j], false))
4825 {
4826 per_cu->v.quick->mark = 1;
4827 break;
4828 }
4829
4830 /* Before we invoke realpath, which can get expensive when many
4831 files are involved, do a quick comparison of the basenames. */
4832 if (!basenames_may_differ
4833 && !file_matcher (lbasename (file_data->file_names[j]),
4834 true))
4835 continue;
4836
4837 this_real_name = dw2_get_real_path (objfile, file_data, j);
4838 if (file_matcher (this_real_name, false))
4839 {
4840 per_cu->v.quick->mark = 1;
4841 break;
4842 }
4843 }
4844
4845 void **slot = htab_find_slot (per_cu->v.quick->mark
4846 ? visited_found.get ()
4847 : visited_not_found.get (),
4848 file_data, INSERT);
4849 *slot = file_data;
4850 }
4851 }
4852
4853 static void
4854 dw2_expand_symtabs_matching
4855 (struct objfile *objfile,
4856 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4857 const lookup_name_info &lookup_name,
4858 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4859 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4860 enum search_domain kind)
4861 {
4862 struct dwarf2_per_objfile *dwarf2_per_objfile
4863 = get_dwarf2_per_objfile (objfile);
4864
4865 /* index_table is NULL if OBJF_READNOW. */
4866 if (!dwarf2_per_objfile->index_table)
4867 return;
4868
4869 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4870
4871 mapped_index &index = *dwarf2_per_objfile->index_table;
4872
4873 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4874 symbol_matcher,
4875 kind, [&] (offset_type idx)
4876 {
4877 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4878 expansion_notify, kind);
4879 return true;
4880 });
4881 }
4882
4883 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4884 symtab. */
4885
4886 static struct compunit_symtab *
4887 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4888 CORE_ADDR pc)
4889 {
4890 int i;
4891
4892 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4893 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4894 return cust;
4895
4896 if (cust->includes == NULL)
4897 return NULL;
4898
4899 for (i = 0; cust->includes[i]; ++i)
4900 {
4901 struct compunit_symtab *s = cust->includes[i];
4902
4903 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4904 if (s != NULL)
4905 return s;
4906 }
4907
4908 return NULL;
4909 }
4910
4911 static struct compunit_symtab *
4912 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4913 struct bound_minimal_symbol msymbol,
4914 CORE_ADDR pc,
4915 struct obj_section *section,
4916 int warn_if_readin)
4917 {
4918 struct dwarf2_per_cu_data *data;
4919 struct compunit_symtab *result;
4920
4921 if (!objfile->partial_symtabs->psymtabs_addrmap)
4922 return NULL;
4923
4924 CORE_ADDR baseaddr = objfile->text_section_offset ();
4925 data = (struct dwarf2_per_cu_data *) addrmap_find
4926 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4927 if (!data)
4928 return NULL;
4929
4930 if (warn_if_readin && data->v.quick->compunit_symtab)
4931 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4932 paddress (get_objfile_arch (objfile), pc));
4933
4934 result
4935 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4936 false),
4937 pc);
4938 gdb_assert (result != NULL);
4939 return result;
4940 }
4941
4942 static void
4943 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4944 void *data, int need_fullname)
4945 {
4946 struct dwarf2_per_objfile *dwarf2_per_objfile
4947 = get_dwarf2_per_objfile (objfile);
4948
4949 if (!dwarf2_per_objfile->filenames_cache)
4950 {
4951 dwarf2_per_objfile->filenames_cache.emplace ();
4952
4953 htab_up visited (htab_create_alloc (10,
4954 htab_hash_pointer, htab_eq_pointer,
4955 NULL, xcalloc, xfree));
4956
4957 /* The rule is CUs specify all the files, including those used
4958 by any TU, so there's no need to scan TUs here. We can
4959 ignore file names coming from already-expanded CUs. */
4960
4961 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4962 {
4963 if (per_cu->v.quick->compunit_symtab)
4964 {
4965 void **slot = htab_find_slot (visited.get (),
4966 per_cu->v.quick->file_names,
4967 INSERT);
4968
4969 *slot = per_cu->v.quick->file_names;
4970 }
4971 }
4972
4973 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4974 {
4975 /* We only need to look at symtabs not already expanded. */
4976 if (per_cu->v.quick->compunit_symtab)
4977 continue;
4978
4979 quick_file_names *file_data = dw2_get_file_names (per_cu);
4980 if (file_data == NULL)
4981 continue;
4982
4983 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4984 if (*slot)
4985 {
4986 /* Already visited. */
4987 continue;
4988 }
4989 *slot = file_data;
4990
4991 for (int j = 0; j < file_data->num_file_names; ++j)
4992 {
4993 const char *filename = file_data->file_names[j];
4994 dwarf2_per_objfile->filenames_cache->seen (filename);
4995 }
4996 }
4997 }
4998
4999 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5000 {
5001 gdb::unique_xmalloc_ptr<char> this_real_name;
5002
5003 if (need_fullname)
5004 this_real_name = gdb_realpath (filename);
5005 (*fun) (filename, this_real_name.get (), data);
5006 });
5007 }
5008
5009 static int
5010 dw2_has_symbols (struct objfile *objfile)
5011 {
5012 return 1;
5013 }
5014
5015 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5016 {
5017 dw2_has_symbols,
5018 dw2_find_last_source_symtab,
5019 dw2_forget_cached_source_info,
5020 dw2_map_symtabs_matching_filename,
5021 dw2_lookup_symbol,
5022 dw2_print_stats,
5023 dw2_dump,
5024 dw2_expand_symtabs_for_function,
5025 dw2_expand_all_symtabs,
5026 dw2_expand_symtabs_with_fullname,
5027 dw2_map_matching_symbols,
5028 dw2_expand_symtabs_matching,
5029 dw2_find_pc_sect_compunit_symtab,
5030 NULL,
5031 dw2_map_symbol_filenames
5032 };
5033
5034 /* DWARF-5 debug_names reader. */
5035
5036 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5037 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5038
5039 /* A helper function that reads the .debug_names section in SECTION
5040 and fills in MAP. FILENAME is the name of the file containing the
5041 section; it is used for error reporting.
5042
5043 Returns true if all went well, false otherwise. */
5044
5045 static bool
5046 read_debug_names_from_section (struct objfile *objfile,
5047 const char *filename,
5048 struct dwarf2_section_info *section,
5049 mapped_debug_names &map)
5050 {
5051 if (section->empty ())
5052 return false;
5053
5054 /* Older elfutils strip versions could keep the section in the main
5055 executable while splitting it for the separate debug info file. */
5056 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5057 return false;
5058
5059 section->read (objfile);
5060
5061 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5062
5063 const gdb_byte *addr = section->buffer;
5064
5065 bfd *const abfd = section->get_bfd_owner ();
5066
5067 unsigned int bytes_read;
5068 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5069 addr += bytes_read;
5070
5071 map.dwarf5_is_dwarf64 = bytes_read != 4;
5072 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5073 if (bytes_read + length != section->size)
5074 {
5075 /* There may be multiple per-CU indices. */
5076 warning (_("Section .debug_names in %s length %s does not match "
5077 "section length %s, ignoring .debug_names."),
5078 filename, plongest (bytes_read + length),
5079 pulongest (section->size));
5080 return false;
5081 }
5082
5083 /* The version number. */
5084 uint16_t version = read_2_bytes (abfd, addr);
5085 addr += 2;
5086 if (version != 5)
5087 {
5088 warning (_("Section .debug_names in %s has unsupported version %d, "
5089 "ignoring .debug_names."),
5090 filename, version);
5091 return false;
5092 }
5093
5094 /* Padding. */
5095 uint16_t padding = read_2_bytes (abfd, addr);
5096 addr += 2;
5097 if (padding != 0)
5098 {
5099 warning (_("Section .debug_names in %s has unsupported padding %d, "
5100 "ignoring .debug_names."),
5101 filename, padding);
5102 return false;
5103 }
5104
5105 /* comp_unit_count - The number of CUs in the CU list. */
5106 map.cu_count = read_4_bytes (abfd, addr);
5107 addr += 4;
5108
5109 /* local_type_unit_count - The number of TUs in the local TU
5110 list. */
5111 map.tu_count = read_4_bytes (abfd, addr);
5112 addr += 4;
5113
5114 /* foreign_type_unit_count - The number of TUs in the foreign TU
5115 list. */
5116 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5117 addr += 4;
5118 if (foreign_tu_count != 0)
5119 {
5120 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5121 "ignoring .debug_names."),
5122 filename, static_cast<unsigned long> (foreign_tu_count));
5123 return false;
5124 }
5125
5126 /* bucket_count - The number of hash buckets in the hash lookup
5127 table. */
5128 map.bucket_count = read_4_bytes (abfd, addr);
5129 addr += 4;
5130
5131 /* name_count - The number of unique names in the index. */
5132 map.name_count = read_4_bytes (abfd, addr);
5133 addr += 4;
5134
5135 /* abbrev_table_size - The size in bytes of the abbreviations
5136 table. */
5137 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5138 addr += 4;
5139
5140 /* augmentation_string_size - The size in bytes of the augmentation
5141 string. This value is rounded up to a multiple of 4. */
5142 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5143 addr += 4;
5144 map.augmentation_is_gdb = ((augmentation_string_size
5145 == sizeof (dwarf5_augmentation))
5146 && memcmp (addr, dwarf5_augmentation,
5147 sizeof (dwarf5_augmentation)) == 0);
5148 augmentation_string_size += (-augmentation_string_size) & 3;
5149 addr += augmentation_string_size;
5150
5151 /* List of CUs */
5152 map.cu_table_reordered = addr;
5153 addr += map.cu_count * map.offset_size;
5154
5155 /* List of Local TUs */
5156 map.tu_table_reordered = addr;
5157 addr += map.tu_count * map.offset_size;
5158
5159 /* Hash Lookup Table */
5160 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5161 addr += map.bucket_count * 4;
5162 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5163 addr += map.name_count * 4;
5164
5165 /* Name Table */
5166 map.name_table_string_offs_reordered = addr;
5167 addr += map.name_count * map.offset_size;
5168 map.name_table_entry_offs_reordered = addr;
5169 addr += map.name_count * map.offset_size;
5170
5171 const gdb_byte *abbrev_table_start = addr;
5172 for (;;)
5173 {
5174 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5175 addr += bytes_read;
5176 if (index_num == 0)
5177 break;
5178
5179 const auto insertpair
5180 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5181 if (!insertpair.second)
5182 {
5183 warning (_("Section .debug_names in %s has duplicate index %s, "
5184 "ignoring .debug_names."),
5185 filename, pulongest (index_num));
5186 return false;
5187 }
5188 mapped_debug_names::index_val &indexval = insertpair.first->second;
5189 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191
5192 for (;;)
5193 {
5194 mapped_debug_names::index_val::attr attr;
5195 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5196 addr += bytes_read;
5197 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5198 addr += bytes_read;
5199 if (attr.form == DW_FORM_implicit_const)
5200 {
5201 attr.implicit_const = read_signed_leb128 (abfd, addr,
5202 &bytes_read);
5203 addr += bytes_read;
5204 }
5205 if (attr.dw_idx == 0 && attr.form == 0)
5206 break;
5207 indexval.attr_vec.push_back (std::move (attr));
5208 }
5209 }
5210 if (addr != abbrev_table_start + abbrev_table_size)
5211 {
5212 warning (_("Section .debug_names in %s has abbreviation_table "
5213 "of size %s vs. written as %u, ignoring .debug_names."),
5214 filename, plongest (addr - abbrev_table_start),
5215 abbrev_table_size);
5216 return false;
5217 }
5218 map.entry_pool = addr;
5219
5220 return true;
5221 }
5222
5223 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5224 list. */
5225
5226 static void
5227 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5228 const mapped_debug_names &map,
5229 dwarf2_section_info &section,
5230 bool is_dwz)
5231 {
5232 sect_offset sect_off_prev;
5233 for (uint32_t i = 0; i <= map.cu_count; ++i)
5234 {
5235 sect_offset sect_off_next;
5236 if (i < map.cu_count)
5237 {
5238 sect_off_next
5239 = (sect_offset) (extract_unsigned_integer
5240 (map.cu_table_reordered + i * map.offset_size,
5241 map.offset_size,
5242 map.dwarf5_byte_order));
5243 }
5244 else
5245 sect_off_next = (sect_offset) section.size;
5246 if (i >= 1)
5247 {
5248 const ULONGEST length = sect_off_next - sect_off_prev;
5249 dwarf2_per_cu_data *per_cu
5250 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5251 sect_off_prev, length);
5252 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5253 }
5254 sect_off_prev = sect_off_next;
5255 }
5256 }
5257
5258 /* Read the CU list from the mapped index, and use it to create all
5259 the CU objects for this dwarf2_per_objfile. */
5260
5261 static void
5262 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5263 const mapped_debug_names &map,
5264 const mapped_debug_names &dwz_map)
5265 {
5266 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5267 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5268
5269 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5270 dwarf2_per_objfile->info,
5271 false /* is_dwz */);
5272
5273 if (dwz_map.cu_count == 0)
5274 return;
5275
5276 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5277 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5278 true /* is_dwz */);
5279 }
5280
5281 /* Read .debug_names. If everything went ok, initialize the "quick"
5282 elements of all the CUs and return true. Otherwise, return false. */
5283
5284 static bool
5285 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5286 {
5287 std::unique_ptr<mapped_debug_names> map
5288 (new mapped_debug_names (dwarf2_per_objfile));
5289 mapped_debug_names dwz_map (dwarf2_per_objfile);
5290 struct objfile *objfile = dwarf2_per_objfile->objfile;
5291
5292 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5293 &dwarf2_per_objfile->debug_names,
5294 *map))
5295 return false;
5296
5297 /* Don't use the index if it's empty. */
5298 if (map->name_count == 0)
5299 return false;
5300
5301 /* If there is a .dwz file, read it so we can get its CU list as
5302 well. */
5303 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5304 if (dwz != NULL)
5305 {
5306 if (!read_debug_names_from_section (objfile,
5307 bfd_get_filename (dwz->dwz_bfd.get ()),
5308 &dwz->debug_names, dwz_map))
5309 {
5310 warning (_("could not read '.debug_names' section from %s; skipping"),
5311 bfd_get_filename (dwz->dwz_bfd.get ()));
5312 return false;
5313 }
5314 }
5315
5316 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5317
5318 if (map->tu_count != 0)
5319 {
5320 /* We can only handle a single .debug_types when we have an
5321 index. */
5322 if (dwarf2_per_objfile->types.size () != 1)
5323 return false;
5324
5325 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5326
5327 create_signatured_type_table_from_debug_names
5328 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5329 }
5330
5331 create_addrmap_from_aranges (dwarf2_per_objfile,
5332 &dwarf2_per_objfile->debug_aranges);
5333
5334 dwarf2_per_objfile->debug_names_table = std::move (map);
5335 dwarf2_per_objfile->using_index = 1;
5336 dwarf2_per_objfile->quick_file_names_table =
5337 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5338
5339 return true;
5340 }
5341
5342 /* Type used to manage iterating over all CUs looking for a symbol for
5343 .debug_names. */
5344
5345 class dw2_debug_names_iterator
5346 {
5347 public:
5348 dw2_debug_names_iterator (const mapped_debug_names &map,
5349 gdb::optional<block_enum> block_index,
5350 domain_enum domain,
5351 const char *name)
5352 : m_map (map), m_block_index (block_index), m_domain (domain),
5353 m_addr (find_vec_in_debug_names (map, name))
5354 {}
5355
5356 dw2_debug_names_iterator (const mapped_debug_names &map,
5357 search_domain search, uint32_t namei)
5358 : m_map (map),
5359 m_search (search),
5360 m_addr (find_vec_in_debug_names (map, namei))
5361 {}
5362
5363 dw2_debug_names_iterator (const mapped_debug_names &map,
5364 block_enum block_index, domain_enum domain,
5365 uint32_t namei)
5366 : m_map (map), m_block_index (block_index), m_domain (domain),
5367 m_addr (find_vec_in_debug_names (map, namei))
5368 {}
5369
5370 /* Return the next matching CU or NULL if there are no more. */
5371 dwarf2_per_cu_data *next ();
5372
5373 private:
5374 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5375 const char *name);
5376 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5377 uint32_t namei);
5378
5379 /* The internalized form of .debug_names. */
5380 const mapped_debug_names &m_map;
5381
5382 /* If set, only look for symbols that match that block. Valid values are
5383 GLOBAL_BLOCK and STATIC_BLOCK. */
5384 const gdb::optional<block_enum> m_block_index;
5385
5386 /* The kind of symbol we're looking for. */
5387 const domain_enum m_domain = UNDEF_DOMAIN;
5388 const search_domain m_search = ALL_DOMAIN;
5389
5390 /* The list of CUs from the index entry of the symbol, or NULL if
5391 not found. */
5392 const gdb_byte *m_addr;
5393 };
5394
5395 const char *
5396 mapped_debug_names::namei_to_name (uint32_t namei) const
5397 {
5398 const ULONGEST namei_string_offs
5399 = extract_unsigned_integer ((name_table_string_offs_reordered
5400 + namei * offset_size),
5401 offset_size,
5402 dwarf5_byte_order);
5403 return read_indirect_string_at_offset
5404 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5405 }
5406
5407 /* Find a slot in .debug_names for the object named NAME. If NAME is
5408 found, return pointer to its pool data. If NAME cannot be found,
5409 return NULL. */
5410
5411 const gdb_byte *
5412 dw2_debug_names_iterator::find_vec_in_debug_names
5413 (const mapped_debug_names &map, const char *name)
5414 {
5415 int (*cmp) (const char *, const char *);
5416
5417 gdb::unique_xmalloc_ptr<char> without_params;
5418 if (current_language->la_language == language_cplus
5419 || current_language->la_language == language_fortran
5420 || current_language->la_language == language_d)
5421 {
5422 /* NAME is already canonical. Drop any qualifiers as
5423 .debug_names does not contain any. */
5424
5425 if (strchr (name, '(') != NULL)
5426 {
5427 without_params = cp_remove_params (name);
5428 if (without_params != NULL)
5429 name = without_params.get ();
5430 }
5431 }
5432
5433 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5434
5435 const uint32_t full_hash = dwarf5_djb_hash (name);
5436 uint32_t namei
5437 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5438 (map.bucket_table_reordered
5439 + (full_hash % map.bucket_count)), 4,
5440 map.dwarf5_byte_order);
5441 if (namei == 0)
5442 return NULL;
5443 --namei;
5444 if (namei >= map.name_count)
5445 {
5446 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5447 "[in module %s]"),
5448 namei, map.name_count,
5449 objfile_name (map.dwarf2_per_objfile->objfile));
5450 return NULL;
5451 }
5452
5453 for (;;)
5454 {
5455 const uint32_t namei_full_hash
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.hash_table_reordered + namei), 4,
5458 map.dwarf5_byte_order);
5459 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5460 return NULL;
5461
5462 if (full_hash == namei_full_hash)
5463 {
5464 const char *const namei_string = map.namei_to_name (namei);
5465
5466 #if 0 /* An expensive sanity check. */
5467 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5468 {
5469 complaint (_("Wrong .debug_names hash for string at index %u "
5470 "[in module %s]"),
5471 namei, objfile_name (dwarf2_per_objfile->objfile));
5472 return NULL;
5473 }
5474 #endif
5475
5476 if (cmp (namei_string, name) == 0)
5477 {
5478 const ULONGEST namei_entry_offs
5479 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5480 + namei * map.offset_size),
5481 map.offset_size, map.dwarf5_byte_order);
5482 return map.entry_pool + namei_entry_offs;
5483 }
5484 }
5485
5486 ++namei;
5487 if (namei >= map.name_count)
5488 return NULL;
5489 }
5490 }
5491
5492 const gdb_byte *
5493 dw2_debug_names_iterator::find_vec_in_debug_names
5494 (const mapped_debug_names &map, uint32_t namei)
5495 {
5496 if (namei >= map.name_count)
5497 {
5498 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5499 "[in module %s]"),
5500 namei, map.name_count,
5501 objfile_name (map.dwarf2_per_objfile->objfile));
5502 return NULL;
5503 }
5504
5505 const ULONGEST namei_entry_offs
5506 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5507 + namei * map.offset_size),
5508 map.offset_size, map.dwarf5_byte_order);
5509 return map.entry_pool + namei_entry_offs;
5510 }
5511
5512 /* See dw2_debug_names_iterator. */
5513
5514 dwarf2_per_cu_data *
5515 dw2_debug_names_iterator::next ()
5516 {
5517 if (m_addr == NULL)
5518 return NULL;
5519
5520 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5521 struct objfile *objfile = dwarf2_per_objfile->objfile;
5522 bfd *const abfd = objfile->obfd;
5523
5524 again:
5525
5526 unsigned int bytes_read;
5527 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5528 m_addr += bytes_read;
5529 if (abbrev == 0)
5530 return NULL;
5531
5532 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5533 if (indexval_it == m_map.abbrev_map.cend ())
5534 {
5535 complaint (_("Wrong .debug_names undefined abbrev code %s "
5536 "[in module %s]"),
5537 pulongest (abbrev), objfile_name (objfile));
5538 return NULL;
5539 }
5540 const mapped_debug_names::index_val &indexval = indexval_it->second;
5541 enum class symbol_linkage {
5542 unknown,
5543 static_,
5544 extern_,
5545 } symbol_linkage_ = symbol_linkage::unknown;
5546 dwarf2_per_cu_data *per_cu = NULL;
5547 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5548 {
5549 ULONGEST ull;
5550 switch (attr.form)
5551 {
5552 case DW_FORM_implicit_const:
5553 ull = attr.implicit_const;
5554 break;
5555 case DW_FORM_flag_present:
5556 ull = 1;
5557 break;
5558 case DW_FORM_udata:
5559 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5560 m_addr += bytes_read;
5561 break;
5562 default:
5563 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5564 dwarf_form_name (attr.form),
5565 objfile_name (objfile));
5566 return NULL;
5567 }
5568 switch (attr.dw_idx)
5569 {
5570 case DW_IDX_compile_unit:
5571 /* Don't crash on bad data. */
5572 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5573 {
5574 complaint (_(".debug_names entry has bad CU index %s"
5575 " [in module %s]"),
5576 pulongest (ull),
5577 objfile_name (dwarf2_per_objfile->objfile));
5578 continue;
5579 }
5580 per_cu = dwarf2_per_objfile->get_cutu (ull);
5581 break;
5582 case DW_IDX_type_unit:
5583 /* Don't crash on bad data. */
5584 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5585 {
5586 complaint (_(".debug_names entry has bad TU index %s"
5587 " [in module %s]"),
5588 pulongest (ull),
5589 objfile_name (dwarf2_per_objfile->objfile));
5590 continue;
5591 }
5592 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5593 break;
5594 case DW_IDX_GNU_internal:
5595 if (!m_map.augmentation_is_gdb)
5596 break;
5597 symbol_linkage_ = symbol_linkage::static_;
5598 break;
5599 case DW_IDX_GNU_external:
5600 if (!m_map.augmentation_is_gdb)
5601 break;
5602 symbol_linkage_ = symbol_linkage::extern_;
5603 break;
5604 }
5605 }
5606
5607 /* Skip if already read in. */
5608 if (per_cu->v.quick->compunit_symtab)
5609 goto again;
5610
5611 /* Check static vs global. */
5612 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5613 {
5614 const bool want_static = *m_block_index == STATIC_BLOCK;
5615 const bool symbol_is_static =
5616 symbol_linkage_ == symbol_linkage::static_;
5617 if (want_static != symbol_is_static)
5618 goto again;
5619 }
5620
5621 /* Match dw2_symtab_iter_next, symbol_kind
5622 and debug_names::psymbol_tag. */
5623 switch (m_domain)
5624 {
5625 case VAR_DOMAIN:
5626 switch (indexval.dwarf_tag)
5627 {
5628 case DW_TAG_variable:
5629 case DW_TAG_subprogram:
5630 /* Some types are also in VAR_DOMAIN. */
5631 case DW_TAG_typedef:
5632 case DW_TAG_structure_type:
5633 break;
5634 default:
5635 goto again;
5636 }
5637 break;
5638 case STRUCT_DOMAIN:
5639 switch (indexval.dwarf_tag)
5640 {
5641 case DW_TAG_typedef:
5642 case DW_TAG_structure_type:
5643 break;
5644 default:
5645 goto again;
5646 }
5647 break;
5648 case LABEL_DOMAIN:
5649 switch (indexval.dwarf_tag)
5650 {
5651 case 0:
5652 case DW_TAG_variable:
5653 break;
5654 default:
5655 goto again;
5656 }
5657 break;
5658 case MODULE_DOMAIN:
5659 switch (indexval.dwarf_tag)
5660 {
5661 case DW_TAG_module:
5662 break;
5663 default:
5664 goto again;
5665 }
5666 break;
5667 default:
5668 break;
5669 }
5670
5671 /* Match dw2_expand_symtabs_matching, symbol_kind and
5672 debug_names::psymbol_tag. */
5673 switch (m_search)
5674 {
5675 case VARIABLES_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_variable:
5679 break;
5680 default:
5681 goto again;
5682 }
5683 break;
5684 case FUNCTIONS_DOMAIN:
5685 switch (indexval.dwarf_tag)
5686 {
5687 case DW_TAG_subprogram:
5688 break;
5689 default:
5690 goto again;
5691 }
5692 break;
5693 case TYPES_DOMAIN:
5694 switch (indexval.dwarf_tag)
5695 {
5696 case DW_TAG_typedef:
5697 case DW_TAG_structure_type:
5698 break;
5699 default:
5700 goto again;
5701 }
5702 break;
5703 case MODULES_DOMAIN:
5704 switch (indexval.dwarf_tag)
5705 {
5706 case DW_TAG_module:
5707 break;
5708 default:
5709 goto again;
5710 }
5711 default:
5712 break;
5713 }
5714
5715 return per_cu;
5716 }
5717
5718 static struct compunit_symtab *
5719 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5720 const char *name, domain_enum domain)
5721 {
5722 struct dwarf2_per_objfile *dwarf2_per_objfile
5723 = get_dwarf2_per_objfile (objfile);
5724
5725 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5726 if (!mapp)
5727 {
5728 /* index is NULL if OBJF_READNOW. */
5729 return NULL;
5730 }
5731 const auto &map = *mapp;
5732
5733 dw2_debug_names_iterator iter (map, block_index, domain, name);
5734
5735 struct compunit_symtab *stab_best = NULL;
5736 struct dwarf2_per_cu_data *per_cu;
5737 while ((per_cu = iter.next ()) != NULL)
5738 {
5739 struct symbol *sym, *with_opaque = NULL;
5740 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5741 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5742 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5743
5744 sym = block_find_symbol (block, name, domain,
5745 block_find_non_opaque_type_preferred,
5746 &with_opaque);
5747
5748 /* Some caution must be observed with overloaded functions and
5749 methods, since the index will not contain any overload
5750 information (but NAME might contain it). */
5751
5752 if (sym != NULL
5753 && strcmp_iw (sym->search_name (), name) == 0)
5754 return stab;
5755 if (with_opaque != NULL
5756 && strcmp_iw (with_opaque->search_name (), name) == 0)
5757 stab_best = stab;
5758
5759 /* Keep looking through other CUs. */
5760 }
5761
5762 return stab_best;
5763 }
5764
5765 /* This dumps minimal information about .debug_names. It is called
5766 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5767 uses this to verify that .debug_names has been loaded. */
5768
5769 static void
5770 dw2_debug_names_dump (struct objfile *objfile)
5771 {
5772 struct dwarf2_per_objfile *dwarf2_per_objfile
5773 = get_dwarf2_per_objfile (objfile);
5774
5775 gdb_assert (dwarf2_per_objfile->using_index);
5776 printf_filtered (".debug_names:");
5777 if (dwarf2_per_objfile->debug_names_table)
5778 printf_filtered (" exists\n");
5779 else
5780 printf_filtered (" faked for \"readnow\"\n");
5781 printf_filtered ("\n");
5782 }
5783
5784 static void
5785 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5786 const char *func_name)
5787 {
5788 struct dwarf2_per_objfile *dwarf2_per_objfile
5789 = get_dwarf2_per_objfile (objfile);
5790
5791 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5792 if (dwarf2_per_objfile->debug_names_table)
5793 {
5794 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5795
5796 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5797
5798 struct dwarf2_per_cu_data *per_cu;
5799 while ((per_cu = iter.next ()) != NULL)
5800 dw2_instantiate_symtab (per_cu, false);
5801 }
5802 }
5803
5804 static void
5805 dw2_debug_names_map_matching_symbols
5806 (struct objfile *objfile,
5807 const lookup_name_info &name, domain_enum domain,
5808 int global,
5809 gdb::function_view<symbol_found_callback_ftype> callback,
5810 symbol_compare_ftype *ordered_compare)
5811 {
5812 struct dwarf2_per_objfile *dwarf2_per_objfile
5813 = get_dwarf2_per_objfile (objfile);
5814
5815 /* debug_names_table is NULL if OBJF_READNOW. */
5816 if (!dwarf2_per_objfile->debug_names_table)
5817 return;
5818
5819 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5820 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5821
5822 const char *match_name = name.ada ().lookup_name ().c_str ();
5823 auto matcher = [&] (const char *symname)
5824 {
5825 if (ordered_compare == nullptr)
5826 return true;
5827 return ordered_compare (symname, match_name) == 0;
5828 };
5829
5830 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5831 [&] (offset_type namei)
5832 {
5833 /* The name was matched, now expand corresponding CUs that were
5834 marked. */
5835 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5836
5837 struct dwarf2_per_cu_data *per_cu;
5838 while ((per_cu = iter.next ()) != NULL)
5839 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5840 return true;
5841 });
5842
5843 /* It's a shame we couldn't do this inside the
5844 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5845 that have already been expanded. Instead, this loop matches what
5846 the psymtab code does. */
5847 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5848 {
5849 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5850 if (cust != nullptr)
5851 {
5852 const struct block *block
5853 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5854 if (!iterate_over_symbols_terminated (block, name,
5855 domain, callback))
5856 break;
5857 }
5858 }
5859 }
5860
5861 static void
5862 dw2_debug_names_expand_symtabs_matching
5863 (struct objfile *objfile,
5864 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5865 const lookup_name_info &lookup_name,
5866 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5867 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5868 enum search_domain kind)
5869 {
5870 struct dwarf2_per_objfile *dwarf2_per_objfile
5871 = get_dwarf2_per_objfile (objfile);
5872
5873 /* debug_names_table is NULL if OBJF_READNOW. */
5874 if (!dwarf2_per_objfile->debug_names_table)
5875 return;
5876
5877 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5878
5879 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5880
5881 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5882 symbol_matcher,
5883 kind, [&] (offset_type namei)
5884 {
5885 /* The name was matched, now expand corresponding CUs that were
5886 marked. */
5887 dw2_debug_names_iterator iter (map, kind, namei);
5888
5889 struct dwarf2_per_cu_data *per_cu;
5890 while ((per_cu = iter.next ()) != NULL)
5891 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5892 expansion_notify);
5893 return true;
5894 });
5895 }
5896
5897 const struct quick_symbol_functions dwarf2_debug_names_functions =
5898 {
5899 dw2_has_symbols,
5900 dw2_find_last_source_symtab,
5901 dw2_forget_cached_source_info,
5902 dw2_map_symtabs_matching_filename,
5903 dw2_debug_names_lookup_symbol,
5904 dw2_print_stats,
5905 dw2_debug_names_dump,
5906 dw2_debug_names_expand_symtabs_for_function,
5907 dw2_expand_all_symtabs,
5908 dw2_expand_symtabs_with_fullname,
5909 dw2_debug_names_map_matching_symbols,
5910 dw2_debug_names_expand_symtabs_matching,
5911 dw2_find_pc_sect_compunit_symtab,
5912 NULL,
5913 dw2_map_symbol_filenames
5914 };
5915
5916 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5917 to either a dwarf2_per_objfile or dwz_file object. */
5918
5919 template <typename T>
5920 static gdb::array_view<const gdb_byte>
5921 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5922 {
5923 dwarf2_section_info *section = &section_owner->gdb_index;
5924
5925 if (section->empty ())
5926 return {};
5927
5928 /* Older elfutils strip versions could keep the section in the main
5929 executable while splitting it for the separate debug info file. */
5930 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5931 return {};
5932
5933 section->read (obj);
5934
5935 /* dwarf2_section_info::size is a bfd_size_type, while
5936 gdb::array_view works with size_t. On 32-bit hosts, with
5937 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5938 is 32-bit. So we need an explicit narrowing conversion here.
5939 This is fine, because it's impossible to allocate or mmap an
5940 array/buffer larger than what size_t can represent. */
5941 return gdb::make_array_view (section->buffer, section->size);
5942 }
5943
5944 /* Lookup the index cache for the contents of the index associated to
5945 DWARF2_OBJ. */
5946
5947 static gdb::array_view<const gdb_byte>
5948 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5949 {
5950 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5951 if (build_id == nullptr)
5952 return {};
5953
5954 return global_index_cache.lookup_gdb_index (build_id,
5955 &dwarf2_obj->index_cache_res);
5956 }
5957
5958 /* Same as the above, but for DWZ. */
5959
5960 static gdb::array_view<const gdb_byte>
5961 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5962 {
5963 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5964 if (build_id == nullptr)
5965 return {};
5966
5967 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5968 }
5969
5970 /* See symfile.h. */
5971
5972 bool
5973 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5974 {
5975 struct dwarf2_per_objfile *dwarf2_per_objfile
5976 = get_dwarf2_per_objfile (objfile);
5977
5978 /* If we're about to read full symbols, don't bother with the
5979 indices. In this case we also don't care if some other debug
5980 format is making psymtabs, because they are all about to be
5981 expanded anyway. */
5982 if ((objfile->flags & OBJF_READNOW))
5983 {
5984 dwarf2_per_objfile->using_index = 1;
5985 create_all_comp_units (dwarf2_per_objfile);
5986 create_all_type_units (dwarf2_per_objfile);
5987 dwarf2_per_objfile->quick_file_names_table
5988 = create_quick_file_names_table
5989 (dwarf2_per_objfile->all_comp_units.size ());
5990
5991 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5992 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5993 {
5994 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5995
5996 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5997 struct dwarf2_per_cu_quick_data);
5998 }
5999
6000 /* Return 1 so that gdb sees the "quick" functions. However,
6001 these functions will be no-ops because we will have expanded
6002 all symtabs. */
6003 *index_kind = dw_index_kind::GDB_INDEX;
6004 return true;
6005 }
6006
6007 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6008 {
6009 *index_kind = dw_index_kind::DEBUG_NAMES;
6010 return true;
6011 }
6012
6013 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6014 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6015 get_gdb_index_contents_from_section<dwz_file>))
6016 {
6017 *index_kind = dw_index_kind::GDB_INDEX;
6018 return true;
6019 }
6020
6021 /* ... otherwise, try to find the index in the index cache. */
6022 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6023 get_gdb_index_contents_from_cache,
6024 get_gdb_index_contents_from_cache_dwz))
6025 {
6026 global_index_cache.hit ();
6027 *index_kind = dw_index_kind::GDB_INDEX;
6028 return true;
6029 }
6030
6031 global_index_cache.miss ();
6032 return false;
6033 }
6034
6035 \f
6036
6037 /* Build a partial symbol table. */
6038
6039 void
6040 dwarf2_build_psymtabs (struct objfile *objfile)
6041 {
6042 struct dwarf2_per_objfile *dwarf2_per_objfile
6043 = get_dwarf2_per_objfile (objfile);
6044
6045 init_psymbol_list (objfile, 1024);
6046
6047 try
6048 {
6049 /* This isn't really ideal: all the data we allocate on the
6050 objfile's obstack is still uselessly kept around. However,
6051 freeing it seems unsafe. */
6052 psymtab_discarder psymtabs (objfile);
6053 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6054 psymtabs.keep ();
6055
6056 /* (maybe) store an index in the cache. */
6057 global_index_cache.store (dwarf2_per_objfile);
6058 }
6059 catch (const gdb_exception_error &except)
6060 {
6061 exception_print (gdb_stderr, except);
6062 }
6063 }
6064
6065 /* Return the total length of the CU described by HEADER. */
6066
6067 static unsigned int
6068 get_cu_length (const struct comp_unit_head *header)
6069 {
6070 return header->initial_length_size + header->length;
6071 }
6072
6073 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6074
6075 static inline bool
6076 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6077 {
6078 sect_offset bottom = cu_header->sect_off;
6079 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6080
6081 return sect_off >= bottom && sect_off < top;
6082 }
6083
6084 /* Find the base address of the compilation unit for range lists and
6085 location lists. It will normally be specified by DW_AT_low_pc.
6086 In DWARF-3 draft 4, the base address could be overridden by
6087 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6088 compilation units with discontinuous ranges. */
6089
6090 static void
6091 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6092 {
6093 struct attribute *attr;
6094
6095 cu->base_known = 0;
6096 cu->base_address = 0;
6097
6098 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6099 if (attr != nullptr)
6100 {
6101 cu->base_address = attr->value_as_address ();
6102 cu->base_known = 1;
6103 }
6104 else
6105 {
6106 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6107 if (attr != nullptr)
6108 {
6109 cu->base_address = attr->value_as_address ();
6110 cu->base_known = 1;
6111 }
6112 }
6113 }
6114
6115 /* Read in the comp unit header information from the debug_info at info_ptr.
6116 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6117 NOTE: This leaves members offset, first_die_offset to be filled in
6118 by the caller. */
6119
6120 static const gdb_byte *
6121 read_comp_unit_head (struct comp_unit_head *cu_header,
6122 const gdb_byte *info_ptr,
6123 struct dwarf2_section_info *section,
6124 rcuh_kind section_kind)
6125 {
6126 int signed_addr;
6127 unsigned int bytes_read;
6128 const char *filename = section->get_file_name ();
6129 bfd *abfd = section->get_bfd_owner ();
6130
6131 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6132 cu_header->initial_length_size = bytes_read;
6133 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6134 info_ptr += bytes_read;
6135 cu_header->version = read_2_bytes (abfd, info_ptr);
6136 if (cu_header->version < 2 || cu_header->version > 5)
6137 error (_("Dwarf Error: wrong version in compilation unit header "
6138 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6139 cu_header->version, filename);
6140 info_ptr += 2;
6141 if (cu_header->version < 5)
6142 switch (section_kind)
6143 {
6144 case rcuh_kind::COMPILE:
6145 cu_header->unit_type = DW_UT_compile;
6146 break;
6147 case rcuh_kind::TYPE:
6148 cu_header->unit_type = DW_UT_type;
6149 break;
6150 default:
6151 internal_error (__FILE__, __LINE__,
6152 _("read_comp_unit_head: invalid section_kind"));
6153 }
6154 else
6155 {
6156 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6157 (read_1_byte (abfd, info_ptr));
6158 info_ptr += 1;
6159 switch (cu_header->unit_type)
6160 {
6161 case DW_UT_compile:
6162 case DW_UT_partial:
6163 case DW_UT_skeleton:
6164 case DW_UT_split_compile:
6165 if (section_kind != rcuh_kind::COMPILE)
6166 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6167 "(is %s, should be %s) [in module %s]"),
6168 dwarf_unit_type_name (cu_header->unit_type),
6169 dwarf_unit_type_name (DW_UT_type), filename);
6170 break;
6171 case DW_UT_type:
6172 case DW_UT_split_type:
6173 section_kind = rcuh_kind::TYPE;
6174 break;
6175 default:
6176 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6177 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6178 "[in module %s]"), cu_header->unit_type,
6179 dwarf_unit_type_name (DW_UT_compile),
6180 dwarf_unit_type_name (DW_UT_skeleton),
6181 dwarf_unit_type_name (DW_UT_split_compile),
6182 dwarf_unit_type_name (DW_UT_type),
6183 dwarf_unit_type_name (DW_UT_split_type), filename);
6184 }
6185
6186 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6187 info_ptr += 1;
6188 }
6189 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6190 cu_header,
6191 &bytes_read);
6192 info_ptr += bytes_read;
6193 if (cu_header->version < 5)
6194 {
6195 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6196 info_ptr += 1;
6197 }
6198 signed_addr = bfd_get_sign_extend_vma (abfd);
6199 if (signed_addr < 0)
6200 internal_error (__FILE__, __LINE__,
6201 _("read_comp_unit_head: dwarf from non elf file"));
6202 cu_header->signed_addr_p = signed_addr;
6203
6204 bool header_has_signature = section_kind == rcuh_kind::TYPE
6205 || cu_header->unit_type == DW_UT_skeleton
6206 || cu_header->unit_type == DW_UT_split_compile;
6207
6208 if (header_has_signature)
6209 {
6210 cu_header->signature = read_8_bytes (abfd, info_ptr);
6211 info_ptr += 8;
6212 }
6213
6214 if (section_kind == rcuh_kind::TYPE)
6215 {
6216 LONGEST type_offset;
6217 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6218 info_ptr += bytes_read;
6219 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6220 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6221 error (_("Dwarf Error: Too big type_offset in compilation unit "
6222 "header (is %s) [in module %s]"), plongest (type_offset),
6223 filename);
6224 }
6225
6226 return info_ptr;
6227 }
6228
6229 /* Helper function that returns the proper abbrev section for
6230 THIS_CU. */
6231
6232 static struct dwarf2_section_info *
6233 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6234 {
6235 struct dwarf2_section_info *abbrev;
6236 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6237
6238 if (this_cu->is_dwz)
6239 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6240 else
6241 abbrev = &dwarf2_per_objfile->abbrev;
6242
6243 return abbrev;
6244 }
6245
6246 /* Subroutine of read_and_check_comp_unit_head and
6247 read_and_check_type_unit_head to simplify them.
6248 Perform various error checking on the header. */
6249
6250 static void
6251 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6252 struct comp_unit_head *header,
6253 struct dwarf2_section_info *section,
6254 struct dwarf2_section_info *abbrev_section)
6255 {
6256 const char *filename = section->get_file_name ();
6257
6258 if (to_underlying (header->abbrev_sect_off)
6259 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6260 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6261 "(offset %s + 6) [in module %s]"),
6262 sect_offset_str (header->abbrev_sect_off),
6263 sect_offset_str (header->sect_off),
6264 filename);
6265
6266 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6267 avoid potential 32-bit overflow. */
6268 if (((ULONGEST) header->sect_off + get_cu_length (header))
6269 > section->size)
6270 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6271 "(offset %s + 0) [in module %s]"),
6272 header->length, sect_offset_str (header->sect_off),
6273 filename);
6274 }
6275
6276 /* Read in a CU/TU header and perform some basic error checking.
6277 The contents of the header are stored in HEADER.
6278 The result is a pointer to the start of the first DIE. */
6279
6280 static const gdb_byte *
6281 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6282 struct comp_unit_head *header,
6283 struct dwarf2_section_info *section,
6284 struct dwarf2_section_info *abbrev_section,
6285 const gdb_byte *info_ptr,
6286 rcuh_kind section_kind)
6287 {
6288 const gdb_byte *beg_of_comp_unit = info_ptr;
6289
6290 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6291
6292 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6293
6294 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6295
6296 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6297 abbrev_section);
6298
6299 return info_ptr;
6300 }
6301
6302 /* Fetch the abbreviation table offset from a comp or type unit header. */
6303
6304 static sect_offset
6305 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6306 struct dwarf2_section_info *section,
6307 sect_offset sect_off)
6308 {
6309 bfd *abfd = section->get_bfd_owner ();
6310 const gdb_byte *info_ptr;
6311 unsigned int initial_length_size, offset_size;
6312 uint16_t version;
6313
6314 section->read (dwarf2_per_objfile->objfile);
6315 info_ptr = section->buffer + to_underlying (sect_off);
6316 read_initial_length (abfd, info_ptr, &initial_length_size);
6317 offset_size = initial_length_size == 4 ? 4 : 8;
6318 info_ptr += initial_length_size;
6319
6320 version = read_2_bytes (abfd, info_ptr);
6321 info_ptr += 2;
6322 if (version >= 5)
6323 {
6324 /* Skip unit type and address size. */
6325 info_ptr += 2;
6326 }
6327
6328 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6329 }
6330
6331 /* Allocate a new partial symtab for file named NAME and mark this new
6332 partial symtab as being an include of PST. */
6333
6334 static void
6335 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6336 struct objfile *objfile)
6337 {
6338 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6339
6340 if (!IS_ABSOLUTE_PATH (subpst->filename))
6341 {
6342 /* It shares objfile->objfile_obstack. */
6343 subpst->dirname = pst->dirname;
6344 }
6345
6346 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6347 subpst->dependencies[0] = pst;
6348 subpst->number_of_dependencies = 1;
6349
6350 /* No private part is necessary for include psymtabs. This property
6351 can be used to differentiate between such include psymtabs and
6352 the regular ones. */
6353 subpst->per_cu_data = nullptr;
6354 }
6355
6356 /* Read the Line Number Program data and extract the list of files
6357 included by the source file represented by PST. Build an include
6358 partial symtab for each of these included files. */
6359
6360 static void
6361 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6362 struct die_info *die,
6363 dwarf2_psymtab *pst)
6364 {
6365 line_header_up lh;
6366 struct attribute *attr;
6367
6368 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6369 if (attr != nullptr)
6370 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6371 if (lh == NULL)
6372 return; /* No linetable, so no includes. */
6373
6374 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6375 that we pass in the raw text_low here; that is ok because we're
6376 only decoding the line table to make include partial symtabs, and
6377 so the addresses aren't really used. */
6378 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6379 pst->raw_text_low (), 1);
6380 }
6381
6382 static hashval_t
6383 hash_signatured_type (const void *item)
6384 {
6385 const struct signatured_type *sig_type
6386 = (const struct signatured_type *) item;
6387
6388 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6389 return sig_type->signature;
6390 }
6391
6392 static int
6393 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6394 {
6395 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6396 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6397
6398 return lhs->signature == rhs->signature;
6399 }
6400
6401 /* Allocate a hash table for signatured types. */
6402
6403 static htab_up
6404 allocate_signatured_type_table (struct objfile *objfile)
6405 {
6406 return htab_up (htab_create_alloc (41,
6407 hash_signatured_type,
6408 eq_signatured_type,
6409 NULL, xcalloc, xfree));
6410 }
6411
6412 /* A helper function to add a signatured type CU to a table. */
6413
6414 static int
6415 add_signatured_type_cu_to_table (void **slot, void *datum)
6416 {
6417 struct signatured_type *sigt = (struct signatured_type *) *slot;
6418 std::vector<signatured_type *> *all_type_units
6419 = (std::vector<signatured_type *> *) datum;
6420
6421 all_type_units->push_back (sigt);
6422
6423 return 1;
6424 }
6425
6426 /* A helper for create_debug_types_hash_table. Read types from SECTION
6427 and fill them into TYPES_HTAB. It will process only type units,
6428 therefore DW_UT_type. */
6429
6430 static void
6431 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6432 struct dwo_file *dwo_file,
6433 dwarf2_section_info *section, htab_up &types_htab,
6434 rcuh_kind section_kind)
6435 {
6436 struct objfile *objfile = dwarf2_per_objfile->objfile;
6437 struct dwarf2_section_info *abbrev_section;
6438 bfd *abfd;
6439 const gdb_byte *info_ptr, *end_ptr;
6440
6441 abbrev_section = (dwo_file != NULL
6442 ? &dwo_file->sections.abbrev
6443 : &dwarf2_per_objfile->abbrev);
6444
6445 if (dwarf_read_debug)
6446 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6447 section->get_name (),
6448 abbrev_section->get_file_name ());
6449
6450 section->read (objfile);
6451 info_ptr = section->buffer;
6452
6453 if (info_ptr == NULL)
6454 return;
6455
6456 /* We can't set abfd until now because the section may be empty or
6457 not present, in which case the bfd is unknown. */
6458 abfd = section->get_bfd_owner ();
6459
6460 /* We don't use cutu_reader here because we don't need to read
6461 any dies: the signature is in the header. */
6462
6463 end_ptr = info_ptr + section->size;
6464 while (info_ptr < end_ptr)
6465 {
6466 struct signatured_type *sig_type;
6467 struct dwo_unit *dwo_tu;
6468 void **slot;
6469 const gdb_byte *ptr = info_ptr;
6470 struct comp_unit_head header;
6471 unsigned int length;
6472
6473 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6474
6475 /* Initialize it due to a false compiler warning. */
6476 header.signature = -1;
6477 header.type_cu_offset_in_tu = (cu_offset) -1;
6478
6479 /* We need to read the type's signature in order to build the hash
6480 table, but we don't need anything else just yet. */
6481
6482 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6483 abbrev_section, ptr, section_kind);
6484
6485 length = get_cu_length (&header);
6486
6487 /* Skip dummy type units. */
6488 if (ptr >= info_ptr + length
6489 || peek_abbrev_code (abfd, ptr) == 0
6490 || header.unit_type != DW_UT_type)
6491 {
6492 info_ptr += length;
6493 continue;
6494 }
6495
6496 if (types_htab == NULL)
6497 {
6498 if (dwo_file)
6499 types_htab = allocate_dwo_unit_table (objfile);
6500 else
6501 types_htab = allocate_signatured_type_table (objfile);
6502 }
6503
6504 if (dwo_file)
6505 {
6506 sig_type = NULL;
6507 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6508 struct dwo_unit);
6509 dwo_tu->dwo_file = dwo_file;
6510 dwo_tu->signature = header.signature;
6511 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6512 dwo_tu->section = section;
6513 dwo_tu->sect_off = sect_off;
6514 dwo_tu->length = length;
6515 }
6516 else
6517 {
6518 /* N.B.: type_offset is not usable if this type uses a DWO file.
6519 The real type_offset is in the DWO file. */
6520 dwo_tu = NULL;
6521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6522 struct signatured_type);
6523 sig_type->signature = header.signature;
6524 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6525 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6526 sig_type->per_cu.is_debug_types = 1;
6527 sig_type->per_cu.section = section;
6528 sig_type->per_cu.sect_off = sect_off;
6529 sig_type->per_cu.length = length;
6530 }
6531
6532 slot = htab_find_slot (types_htab.get (),
6533 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6534 INSERT);
6535 gdb_assert (slot != NULL);
6536 if (*slot != NULL)
6537 {
6538 sect_offset dup_sect_off;
6539
6540 if (dwo_file)
6541 {
6542 const struct dwo_unit *dup_tu
6543 = (const struct dwo_unit *) *slot;
6544
6545 dup_sect_off = dup_tu->sect_off;
6546 }
6547 else
6548 {
6549 const struct signatured_type *dup_tu
6550 = (const struct signatured_type *) *slot;
6551
6552 dup_sect_off = dup_tu->per_cu.sect_off;
6553 }
6554
6555 complaint (_("debug type entry at offset %s is duplicate to"
6556 " the entry at offset %s, signature %s"),
6557 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6558 hex_string (header.signature));
6559 }
6560 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6561
6562 if (dwarf_read_debug > 1)
6563 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6564 sect_offset_str (sect_off),
6565 hex_string (header.signature));
6566
6567 info_ptr += length;
6568 }
6569 }
6570
6571 /* Create the hash table of all entries in the .debug_types
6572 (or .debug_types.dwo) section(s).
6573 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6574 otherwise it is NULL.
6575
6576 The result is a pointer to the hash table or NULL if there are no types.
6577
6578 Note: This function processes DWO files only, not DWP files. */
6579
6580 static void
6581 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6582 struct dwo_file *dwo_file,
6583 gdb::array_view<dwarf2_section_info> type_sections,
6584 htab_up &types_htab)
6585 {
6586 for (dwarf2_section_info &section : type_sections)
6587 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6588 types_htab, rcuh_kind::TYPE);
6589 }
6590
6591 /* Create the hash table of all entries in the .debug_types section,
6592 and initialize all_type_units.
6593 The result is zero if there is an error (e.g. missing .debug_types section),
6594 otherwise non-zero. */
6595
6596 static int
6597 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6598 {
6599 htab_up types_htab;
6600
6601 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6602 &dwarf2_per_objfile->info, types_htab,
6603 rcuh_kind::COMPILE);
6604 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6605 dwarf2_per_objfile->types, types_htab);
6606 if (types_htab == NULL)
6607 {
6608 dwarf2_per_objfile->signatured_types = NULL;
6609 return 0;
6610 }
6611
6612 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6613
6614 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6615 dwarf2_per_objfile->all_type_units.reserve
6616 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6617
6618 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6619 add_signatured_type_cu_to_table,
6620 &dwarf2_per_objfile->all_type_units);
6621
6622 return 1;
6623 }
6624
6625 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6626 If SLOT is non-NULL, it is the entry to use in the hash table.
6627 Otherwise we find one. */
6628
6629 static struct signatured_type *
6630 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6631 void **slot)
6632 {
6633 struct objfile *objfile = dwarf2_per_objfile->objfile;
6634
6635 if (dwarf2_per_objfile->all_type_units.size ()
6636 == dwarf2_per_objfile->all_type_units.capacity ())
6637 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6638
6639 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6640 struct signatured_type);
6641
6642 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6643 sig_type->signature = sig;
6644 sig_type->per_cu.is_debug_types = 1;
6645 if (dwarf2_per_objfile->using_index)
6646 {
6647 sig_type->per_cu.v.quick =
6648 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6649 struct dwarf2_per_cu_quick_data);
6650 }
6651
6652 if (slot == NULL)
6653 {
6654 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6655 sig_type, INSERT);
6656 }
6657 gdb_assert (*slot == NULL);
6658 *slot = sig_type;
6659 /* The rest of sig_type must be filled in by the caller. */
6660 return sig_type;
6661 }
6662
6663 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6664 Fill in SIG_ENTRY with DWO_ENTRY. */
6665
6666 static void
6667 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6668 struct signatured_type *sig_entry,
6669 struct dwo_unit *dwo_entry)
6670 {
6671 /* Make sure we're not clobbering something we don't expect to. */
6672 gdb_assert (! sig_entry->per_cu.queued);
6673 gdb_assert (sig_entry->per_cu.cu == NULL);
6674 if (dwarf2_per_objfile->using_index)
6675 {
6676 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6677 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6678 }
6679 else
6680 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6681 gdb_assert (sig_entry->signature == dwo_entry->signature);
6682 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6683 gdb_assert (sig_entry->type_unit_group == NULL);
6684 gdb_assert (sig_entry->dwo_unit == NULL);
6685
6686 sig_entry->per_cu.section = dwo_entry->section;
6687 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6688 sig_entry->per_cu.length = dwo_entry->length;
6689 sig_entry->per_cu.reading_dwo_directly = 1;
6690 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6691 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6692 sig_entry->dwo_unit = dwo_entry;
6693 }
6694
6695 /* Subroutine of lookup_signatured_type.
6696 If we haven't read the TU yet, create the signatured_type data structure
6697 for a TU to be read in directly from a DWO file, bypassing the stub.
6698 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6699 using .gdb_index, then when reading a CU we want to stay in the DWO file
6700 containing that CU. Otherwise we could end up reading several other DWO
6701 files (due to comdat folding) to process the transitive closure of all the
6702 mentioned TUs, and that can be slow. The current DWO file will have every
6703 type signature that it needs.
6704 We only do this for .gdb_index because in the psymtab case we already have
6705 to read all the DWOs to build the type unit groups. */
6706
6707 static struct signatured_type *
6708 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6709 {
6710 struct dwarf2_per_objfile *dwarf2_per_objfile
6711 = cu->per_cu->dwarf2_per_objfile;
6712 struct objfile *objfile = dwarf2_per_objfile->objfile;
6713 struct dwo_file *dwo_file;
6714 struct dwo_unit find_dwo_entry, *dwo_entry;
6715 struct signatured_type find_sig_entry, *sig_entry;
6716 void **slot;
6717
6718 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6719
6720 /* If TU skeletons have been removed then we may not have read in any
6721 TUs yet. */
6722 if (dwarf2_per_objfile->signatured_types == NULL)
6723 {
6724 dwarf2_per_objfile->signatured_types
6725 = allocate_signatured_type_table (objfile);
6726 }
6727
6728 /* We only ever need to read in one copy of a signatured type.
6729 Use the global signatured_types array to do our own comdat-folding
6730 of types. If this is the first time we're reading this TU, and
6731 the TU has an entry in .gdb_index, replace the recorded data from
6732 .gdb_index with this TU. */
6733
6734 find_sig_entry.signature = sig;
6735 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6736 &find_sig_entry, INSERT);
6737 sig_entry = (struct signatured_type *) *slot;
6738
6739 /* We can get here with the TU already read, *or* in the process of being
6740 read. Don't reassign the global entry to point to this DWO if that's
6741 the case. Also note that if the TU is already being read, it may not
6742 have come from a DWO, the program may be a mix of Fission-compiled
6743 code and non-Fission-compiled code. */
6744
6745 /* Have we already tried to read this TU?
6746 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6747 needn't exist in the global table yet). */
6748 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6749 return sig_entry;
6750
6751 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6752 dwo_unit of the TU itself. */
6753 dwo_file = cu->dwo_unit->dwo_file;
6754
6755 /* Ok, this is the first time we're reading this TU. */
6756 if (dwo_file->tus == NULL)
6757 return NULL;
6758 find_dwo_entry.signature = sig;
6759 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6760 &find_dwo_entry);
6761 if (dwo_entry == NULL)
6762 return NULL;
6763
6764 /* If the global table doesn't have an entry for this TU, add one. */
6765 if (sig_entry == NULL)
6766 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6767
6768 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6769 sig_entry->per_cu.tu_read = 1;
6770 return sig_entry;
6771 }
6772
6773 /* Subroutine of lookup_signatured_type.
6774 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6775 then try the DWP file. If the TU stub (skeleton) has been removed then
6776 it won't be in .gdb_index. */
6777
6778 static struct signatured_type *
6779 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6780 {
6781 struct dwarf2_per_objfile *dwarf2_per_objfile
6782 = cu->per_cu->dwarf2_per_objfile;
6783 struct objfile *objfile = dwarf2_per_objfile->objfile;
6784 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6785 struct dwo_unit *dwo_entry;
6786 struct signatured_type find_sig_entry, *sig_entry;
6787 void **slot;
6788
6789 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6790 gdb_assert (dwp_file != NULL);
6791
6792 /* If TU skeletons have been removed then we may not have read in any
6793 TUs yet. */
6794 if (dwarf2_per_objfile->signatured_types == NULL)
6795 {
6796 dwarf2_per_objfile->signatured_types
6797 = allocate_signatured_type_table (objfile);
6798 }
6799
6800 find_sig_entry.signature = sig;
6801 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6802 &find_sig_entry, INSERT);
6803 sig_entry = (struct signatured_type *) *slot;
6804
6805 /* Have we already tried to read this TU?
6806 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6807 needn't exist in the global table yet). */
6808 if (sig_entry != NULL)
6809 return sig_entry;
6810
6811 if (dwp_file->tus == NULL)
6812 return NULL;
6813 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6814 sig, 1 /* is_debug_types */);
6815 if (dwo_entry == NULL)
6816 return NULL;
6817
6818 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6819 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6820
6821 return sig_entry;
6822 }
6823
6824 /* Lookup a signature based type for DW_FORM_ref_sig8.
6825 Returns NULL if signature SIG is not present in the table.
6826 It is up to the caller to complain about this. */
6827
6828 static struct signatured_type *
6829 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6830 {
6831 struct dwarf2_per_objfile *dwarf2_per_objfile
6832 = cu->per_cu->dwarf2_per_objfile;
6833
6834 if (cu->dwo_unit
6835 && dwarf2_per_objfile->using_index)
6836 {
6837 /* We're in a DWO/DWP file, and we're using .gdb_index.
6838 These cases require special processing. */
6839 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6840 return lookup_dwo_signatured_type (cu, sig);
6841 else
6842 return lookup_dwp_signatured_type (cu, sig);
6843 }
6844 else
6845 {
6846 struct signatured_type find_entry, *entry;
6847
6848 if (dwarf2_per_objfile->signatured_types == NULL)
6849 return NULL;
6850 find_entry.signature = sig;
6851 entry = ((struct signatured_type *)
6852 htab_find (dwarf2_per_objfile->signatured_types.get (),
6853 &find_entry));
6854 return entry;
6855 }
6856 }
6857
6858 /* Return the address base of the compile unit, which, if exists, is stored
6859 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6860 static gdb::optional<ULONGEST>
6861 lookup_addr_base (struct die_info *comp_unit_die)
6862 {
6863 struct attribute *attr;
6864 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6865 if (attr == nullptr)
6866 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6867 if (attr == nullptr)
6868 return gdb::optional<ULONGEST> ();
6869 return DW_UNSND (attr);
6870 }
6871
6872 /* Return range lists base of the compile unit, which, if exists, is stored
6873 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6874 static ULONGEST
6875 lookup_ranges_base (struct die_info *comp_unit_die)
6876 {
6877 struct attribute *attr;
6878 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6879 if (attr == nullptr)
6880 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6881 if (attr == nullptr)
6882 return 0;
6883 return DW_UNSND (attr);
6884 }
6885
6886 /* Low level DIE reading support. */
6887
6888 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6889
6890 static void
6891 init_cu_die_reader (struct die_reader_specs *reader,
6892 struct dwarf2_cu *cu,
6893 struct dwarf2_section_info *section,
6894 struct dwo_file *dwo_file,
6895 struct abbrev_table *abbrev_table)
6896 {
6897 gdb_assert (section->readin && section->buffer != NULL);
6898 reader->abfd = section->get_bfd_owner ();
6899 reader->cu = cu;
6900 reader->dwo_file = dwo_file;
6901 reader->die_section = section;
6902 reader->buffer = section->buffer;
6903 reader->buffer_end = section->buffer + section->size;
6904 reader->abbrev_table = abbrev_table;
6905 }
6906
6907 /* Subroutine of cutu_reader to simplify it.
6908 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6909 There's just a lot of work to do, and cutu_reader is big enough
6910 already.
6911
6912 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6913 from it to the DIE in the DWO. If NULL we are skipping the stub.
6914 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6915 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6916 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6917 STUB_COMP_DIR may be non-NULL.
6918 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6919 are filled in with the info of the DIE from the DWO file.
6920 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6921 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6922 kept around for at least as long as *RESULT_READER.
6923
6924 The result is non-zero if a valid (non-dummy) DIE was found. */
6925
6926 static int
6927 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6928 struct dwo_unit *dwo_unit,
6929 struct die_info *stub_comp_unit_die,
6930 const char *stub_comp_dir,
6931 struct die_reader_specs *result_reader,
6932 const gdb_byte **result_info_ptr,
6933 struct die_info **result_comp_unit_die,
6934 abbrev_table_up *result_dwo_abbrev_table)
6935 {
6936 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6937 struct objfile *objfile = dwarf2_per_objfile->objfile;
6938 struct dwarf2_cu *cu = this_cu->cu;
6939 bfd *abfd;
6940 const gdb_byte *begin_info_ptr, *info_ptr;
6941 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6942 int i,num_extra_attrs;
6943 struct dwarf2_section_info *dwo_abbrev_section;
6944 struct die_info *comp_unit_die;
6945
6946 /* At most one of these may be provided. */
6947 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6948
6949 /* These attributes aren't processed until later:
6950 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6951 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6952 referenced later. However, these attributes are found in the stub
6953 which we won't have later. In order to not impose this complication
6954 on the rest of the code, we read them here and copy them to the
6955 DWO CU/TU die. */
6956
6957 stmt_list = NULL;
6958 low_pc = NULL;
6959 high_pc = NULL;
6960 ranges = NULL;
6961 comp_dir = NULL;
6962
6963 if (stub_comp_unit_die != NULL)
6964 {
6965 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6966 DWO file. */
6967 if (! this_cu->is_debug_types)
6968 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6969 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6970 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6971 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6972 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6973
6974 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6975
6976 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6977 here (if needed). We need the value before we can process
6978 DW_AT_ranges. */
6979 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6980 }
6981 else if (stub_comp_dir != NULL)
6982 {
6983 /* Reconstruct the comp_dir attribute to simplify the code below. */
6984 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6985 comp_dir->name = DW_AT_comp_dir;
6986 comp_dir->form = DW_FORM_string;
6987 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6988 DW_STRING (comp_dir) = stub_comp_dir;
6989 }
6990
6991 /* Set up for reading the DWO CU/TU. */
6992 cu->dwo_unit = dwo_unit;
6993 dwarf2_section_info *section = dwo_unit->section;
6994 section->read (objfile);
6995 abfd = section->get_bfd_owner ();
6996 begin_info_ptr = info_ptr = (section->buffer
6997 + to_underlying (dwo_unit->sect_off));
6998 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6999
7000 if (this_cu->is_debug_types)
7001 {
7002 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7003
7004 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7005 &cu->header, section,
7006 dwo_abbrev_section,
7007 info_ptr, rcuh_kind::TYPE);
7008 /* This is not an assert because it can be caused by bad debug info. */
7009 if (sig_type->signature != cu->header.signature)
7010 {
7011 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7012 " TU at offset %s [in module %s]"),
7013 hex_string (sig_type->signature),
7014 hex_string (cu->header.signature),
7015 sect_offset_str (dwo_unit->sect_off),
7016 bfd_get_filename (abfd));
7017 }
7018 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7019 /* For DWOs coming from DWP files, we don't know the CU length
7020 nor the type's offset in the TU until now. */
7021 dwo_unit->length = get_cu_length (&cu->header);
7022 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7023
7024 /* Establish the type offset that can be used to lookup the type.
7025 For DWO files, we don't know it until now. */
7026 sig_type->type_offset_in_section
7027 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7028 }
7029 else
7030 {
7031 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7032 &cu->header, section,
7033 dwo_abbrev_section,
7034 info_ptr, rcuh_kind::COMPILE);
7035 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7036 /* For DWOs coming from DWP files, we don't know the CU length
7037 until now. */
7038 dwo_unit->length = get_cu_length (&cu->header);
7039 }
7040
7041 *result_dwo_abbrev_table
7042 = abbrev_table_read_table (objfile, dwo_abbrev_section,
7043 cu->header.abbrev_sect_off);
7044 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7045 result_dwo_abbrev_table->get ());
7046
7047 /* Read in the die, but leave space to copy over the attributes
7048 from the stub. This has the benefit of simplifying the rest of
7049 the code - all the work to maintain the illusion of a single
7050 DW_TAG_{compile,type}_unit DIE is done here. */
7051 num_extra_attrs = ((stmt_list != NULL)
7052 + (low_pc != NULL)
7053 + (high_pc != NULL)
7054 + (ranges != NULL)
7055 + (comp_dir != NULL));
7056 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7057 num_extra_attrs);
7058
7059 /* Copy over the attributes from the stub to the DIE we just read in. */
7060 comp_unit_die = *result_comp_unit_die;
7061 i = comp_unit_die->num_attrs;
7062 if (stmt_list != NULL)
7063 comp_unit_die->attrs[i++] = *stmt_list;
7064 if (low_pc != NULL)
7065 comp_unit_die->attrs[i++] = *low_pc;
7066 if (high_pc != NULL)
7067 comp_unit_die->attrs[i++] = *high_pc;
7068 if (ranges != NULL)
7069 comp_unit_die->attrs[i++] = *ranges;
7070 if (comp_dir != NULL)
7071 comp_unit_die->attrs[i++] = *comp_dir;
7072 comp_unit_die->num_attrs += num_extra_attrs;
7073
7074 if (dwarf_die_debug)
7075 {
7076 fprintf_unfiltered (gdb_stdlog,
7077 "Read die from %s@0x%x of %s:\n",
7078 section->get_name (),
7079 (unsigned) (begin_info_ptr - section->buffer),
7080 bfd_get_filename (abfd));
7081 dump_die (comp_unit_die, dwarf_die_debug);
7082 }
7083
7084 /* Skip dummy compilation units. */
7085 if (info_ptr >= begin_info_ptr + dwo_unit->length
7086 || peek_abbrev_code (abfd, info_ptr) == 0)
7087 return 0;
7088
7089 *result_info_ptr = info_ptr;
7090 return 1;
7091 }
7092
7093 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7094 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7095 signature is part of the header. */
7096 static gdb::optional<ULONGEST>
7097 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7098 {
7099 if (cu->header.version >= 5)
7100 return cu->header.signature;
7101 struct attribute *attr;
7102 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7103 if (attr == nullptr)
7104 return gdb::optional<ULONGEST> ();
7105 return DW_UNSND (attr);
7106 }
7107
7108 /* Subroutine of cutu_reader to simplify it.
7109 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7110 Returns NULL if the specified DWO unit cannot be found. */
7111
7112 static struct dwo_unit *
7113 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7114 struct die_info *comp_unit_die,
7115 const char *dwo_name)
7116 {
7117 struct dwarf2_cu *cu = this_cu->cu;
7118 struct dwo_unit *dwo_unit;
7119 const char *comp_dir;
7120
7121 gdb_assert (cu != NULL);
7122
7123 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7124 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7125 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7126
7127 if (this_cu->is_debug_types)
7128 {
7129 struct signatured_type *sig_type;
7130
7131 /* Since this_cu is the first member of struct signatured_type,
7132 we can go from a pointer to one to a pointer to the other. */
7133 sig_type = (struct signatured_type *) this_cu;
7134 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7135 }
7136 else
7137 {
7138 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7139 if (!signature.has_value ())
7140 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7141 " [in module %s]"),
7142 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7143 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7144 *signature);
7145 }
7146
7147 return dwo_unit;
7148 }
7149
7150 /* Subroutine of cutu_reader to simplify it.
7151 See it for a description of the parameters.
7152 Read a TU directly from a DWO file, bypassing the stub. */
7153
7154 void
7155 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7156 int use_existing_cu, int keep)
7157 {
7158 struct signatured_type *sig_type;
7159 struct die_reader_specs reader;
7160
7161 /* Verify we can do the following downcast, and that we have the
7162 data we need. */
7163 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7164 sig_type = (struct signatured_type *) this_cu;
7165 gdb_assert (sig_type->dwo_unit != NULL);
7166
7167 if (use_existing_cu && this_cu->cu != NULL)
7168 {
7169 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7170 /* There's no need to do the rereading_dwo_cu handling that
7171 cutu_reader does since we don't read the stub. */
7172 }
7173 else
7174 {
7175 /* If !use_existing_cu, this_cu->cu must be NULL. */
7176 gdb_assert (this_cu->cu == NULL);
7177 m_new_cu.reset (new dwarf2_cu (this_cu));
7178 }
7179
7180 /* A future optimization, if needed, would be to use an existing
7181 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7182 could share abbrev tables. */
7183
7184 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7185 NULL /* stub_comp_unit_die */,
7186 sig_type->dwo_unit->dwo_file->comp_dir,
7187 &reader, &info_ptr,
7188 &comp_unit_die,
7189 &m_dwo_abbrev_table) == 0)
7190 {
7191 /* Dummy die. */
7192 dummy_p = true;
7193 }
7194 }
7195
7196 /* Initialize a CU (or TU) and read its DIEs.
7197 If the CU defers to a DWO file, read the DWO file as well.
7198
7199 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7200 Otherwise the table specified in the comp unit header is read in and used.
7201 This is an optimization for when we already have the abbrev table.
7202
7203 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7204 Otherwise, a new CU is allocated with xmalloc.
7205
7206 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7207 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7208 end. */
7209
7210 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7211 struct abbrev_table *abbrev_table,
7212 int use_existing_cu, int keep,
7213 bool skip_partial)
7214 : die_reader_specs {},
7215 m_this_cu (this_cu),
7216 m_keep (keep)
7217 {
7218 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7219 struct objfile *objfile = dwarf2_per_objfile->objfile;
7220 struct dwarf2_section_info *section = this_cu->section;
7221 bfd *abfd = section->get_bfd_owner ();
7222 struct dwarf2_cu *cu;
7223 const gdb_byte *begin_info_ptr;
7224 struct signatured_type *sig_type = NULL;
7225 struct dwarf2_section_info *abbrev_section;
7226 /* Non-zero if CU currently points to a DWO file and we need to
7227 reread it. When this happens we need to reread the skeleton die
7228 before we can reread the DWO file (this only applies to CUs, not TUs). */
7229 int rereading_dwo_cu = 0;
7230
7231 if (dwarf_die_debug)
7232 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7233 this_cu->is_debug_types ? "type" : "comp",
7234 sect_offset_str (this_cu->sect_off));
7235
7236 if (use_existing_cu)
7237 gdb_assert (keep);
7238
7239 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7240 file (instead of going through the stub), short-circuit all of this. */
7241 if (this_cu->reading_dwo_directly)
7242 {
7243 /* Narrow down the scope of possibilities to have to understand. */
7244 gdb_assert (this_cu->is_debug_types);
7245 gdb_assert (abbrev_table == NULL);
7246 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7247 return;
7248 }
7249
7250 /* This is cheap if the section is already read in. */
7251 section->read (objfile);
7252
7253 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7254
7255 abbrev_section = get_abbrev_section_for_cu (this_cu);
7256
7257 if (use_existing_cu && this_cu->cu != NULL)
7258 {
7259 cu = this_cu->cu;
7260 /* If this CU is from a DWO file we need to start over, we need to
7261 refetch the attributes from the skeleton CU.
7262 This could be optimized by retrieving those attributes from when we
7263 were here the first time: the previous comp_unit_die was stored in
7264 comp_unit_obstack. But there's no data yet that we need this
7265 optimization. */
7266 if (cu->dwo_unit != NULL)
7267 rereading_dwo_cu = 1;
7268 }
7269 else
7270 {
7271 /* If !use_existing_cu, this_cu->cu must be NULL. */
7272 gdb_assert (this_cu->cu == NULL);
7273 m_new_cu.reset (new dwarf2_cu (this_cu));
7274 cu = m_new_cu.get ();
7275 }
7276
7277 /* Get the header. */
7278 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7279 {
7280 /* We already have the header, there's no need to read it in again. */
7281 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7282 }
7283 else
7284 {
7285 if (this_cu->is_debug_types)
7286 {
7287 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7288 &cu->header, section,
7289 abbrev_section, info_ptr,
7290 rcuh_kind::TYPE);
7291
7292 /* Since per_cu is the first member of struct signatured_type,
7293 we can go from a pointer to one to a pointer to the other. */
7294 sig_type = (struct signatured_type *) this_cu;
7295 gdb_assert (sig_type->signature == cu->header.signature);
7296 gdb_assert (sig_type->type_offset_in_tu
7297 == cu->header.type_cu_offset_in_tu);
7298 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7299
7300 /* LENGTH has not been set yet for type units if we're
7301 using .gdb_index. */
7302 this_cu->length = get_cu_length (&cu->header);
7303
7304 /* Establish the type offset that can be used to lookup the type. */
7305 sig_type->type_offset_in_section =
7306 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7307
7308 this_cu->dwarf_version = cu->header.version;
7309 }
7310 else
7311 {
7312 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7313 &cu->header, section,
7314 abbrev_section,
7315 info_ptr,
7316 rcuh_kind::COMPILE);
7317
7318 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7319 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7320 this_cu->dwarf_version = cu->header.version;
7321 }
7322 }
7323
7324 /* Skip dummy compilation units. */
7325 if (info_ptr >= begin_info_ptr + this_cu->length
7326 || peek_abbrev_code (abfd, info_ptr) == 0)
7327 {
7328 dummy_p = true;
7329 return;
7330 }
7331
7332 /* If we don't have them yet, read the abbrevs for this compilation unit.
7333 And if we need to read them now, make sure they're freed when we're
7334 done. */
7335 if (abbrev_table != NULL)
7336 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7337 else
7338 {
7339 m_abbrev_table_holder
7340 = abbrev_table_read_table (objfile, abbrev_section,
7341 cu->header.abbrev_sect_off);
7342 abbrev_table = m_abbrev_table_holder.get ();
7343 }
7344
7345 /* Read the top level CU/TU die. */
7346 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7347 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7348
7349 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7350 {
7351 dummy_p = true;
7352 return;
7353 }
7354
7355 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7356 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7357 table from the DWO file and pass the ownership over to us. It will be
7358 referenced from READER, so we must make sure to free it after we're done
7359 with READER.
7360
7361 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7362 DWO CU, that this test will fail (the attribute will not be present). */
7363 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7364 if (dwo_name != nullptr)
7365 {
7366 struct dwo_unit *dwo_unit;
7367 struct die_info *dwo_comp_unit_die;
7368
7369 if (comp_unit_die->has_children)
7370 {
7371 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7372 " has children (offset %s) [in module %s]"),
7373 sect_offset_str (this_cu->sect_off),
7374 bfd_get_filename (abfd));
7375 }
7376 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7377 if (dwo_unit != NULL)
7378 {
7379 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7380 comp_unit_die, NULL,
7381 this, &info_ptr,
7382 &dwo_comp_unit_die,
7383 &m_dwo_abbrev_table) == 0)
7384 {
7385 /* Dummy die. */
7386 dummy_p = true;
7387 return;
7388 }
7389 comp_unit_die = dwo_comp_unit_die;
7390 }
7391 else
7392 {
7393 /* Yikes, we couldn't find the rest of the DIE, we only have
7394 the stub. A complaint has already been logged. There's
7395 not much more we can do except pass on the stub DIE to
7396 die_reader_func. We don't want to throw an error on bad
7397 debug info. */
7398 }
7399 }
7400 }
7401
7402 cutu_reader::~cutu_reader ()
7403 {
7404 /* Done, clean up. */
7405 if (m_new_cu != NULL && m_keep && !dummy_p)
7406 {
7407 struct dwarf2_per_objfile *dwarf2_per_objfile
7408 = m_this_cu->dwarf2_per_objfile;
7409 /* Link this CU into read_in_chain. */
7410 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7411 dwarf2_per_objfile->read_in_chain = m_this_cu;
7412 /* The chain owns it now. */
7413 m_new_cu.release ();
7414 }
7415 }
7416
7417 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7418 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7419 assumed to have already done the lookup to find the DWO file).
7420
7421 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7422 THIS_CU->is_debug_types, but nothing else.
7423
7424 We fill in THIS_CU->length.
7425
7426 THIS_CU->cu is always freed when done.
7427 This is done in order to not leave THIS_CU->cu in a state where we have
7428 to care whether it refers to the "main" CU or the DWO CU.
7429
7430 When parent_cu is passed, it is used to provide a default value for
7431 str_offsets_base and addr_base from the parent. */
7432
7433 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7434 struct dwarf2_cu *parent_cu,
7435 struct dwo_file *dwo_file)
7436 : die_reader_specs {},
7437 m_this_cu (this_cu)
7438 {
7439 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7440 struct objfile *objfile = dwarf2_per_objfile->objfile;
7441 struct dwarf2_section_info *section = this_cu->section;
7442 bfd *abfd = section->get_bfd_owner ();
7443 struct dwarf2_section_info *abbrev_section;
7444 const gdb_byte *begin_info_ptr, *info_ptr;
7445
7446 if (dwarf_die_debug)
7447 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7448 this_cu->is_debug_types ? "type" : "comp",
7449 sect_offset_str (this_cu->sect_off));
7450
7451 gdb_assert (this_cu->cu == NULL);
7452
7453 abbrev_section = (dwo_file != NULL
7454 ? &dwo_file->sections.abbrev
7455 : get_abbrev_section_for_cu (this_cu));
7456
7457 /* This is cheap if the section is already read in. */
7458 section->read (objfile);
7459
7460 m_new_cu.reset (new dwarf2_cu (this_cu));
7461
7462 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7463 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7464 &m_new_cu->header, section,
7465 abbrev_section, info_ptr,
7466 (this_cu->is_debug_types
7467 ? rcuh_kind::TYPE
7468 : rcuh_kind::COMPILE));
7469
7470 if (parent_cu != nullptr)
7471 {
7472 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7473 m_new_cu->addr_base = parent_cu->addr_base;
7474 }
7475 this_cu->length = get_cu_length (&m_new_cu->header);
7476
7477 /* Skip dummy compilation units. */
7478 if (info_ptr >= begin_info_ptr + this_cu->length
7479 || peek_abbrev_code (abfd, info_ptr) == 0)
7480 {
7481 dummy_p = true;
7482 return;
7483 }
7484
7485 m_abbrev_table_holder
7486 = abbrev_table_read_table (objfile, abbrev_section,
7487 m_new_cu->header.abbrev_sect_off);
7488
7489 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7490 m_abbrev_table_holder.get ());
7491 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7492 }
7493
7494 \f
7495 /* Type Unit Groups.
7496
7497 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7498 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7499 so that all types coming from the same compilation (.o file) are grouped
7500 together. A future step could be to put the types in the same symtab as
7501 the CU the types ultimately came from. */
7502
7503 static hashval_t
7504 hash_type_unit_group (const void *item)
7505 {
7506 const struct type_unit_group *tu_group
7507 = (const struct type_unit_group *) item;
7508
7509 return hash_stmt_list_entry (&tu_group->hash);
7510 }
7511
7512 static int
7513 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7514 {
7515 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7516 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7517
7518 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7519 }
7520
7521 /* Allocate a hash table for type unit groups. */
7522
7523 static htab_t
7524 allocate_type_unit_groups_table (struct objfile *objfile)
7525 {
7526 return htab_create_alloc_ex (3,
7527 hash_type_unit_group,
7528 eq_type_unit_group,
7529 NULL,
7530 &objfile->objfile_obstack,
7531 hashtab_obstack_allocate,
7532 dummy_obstack_deallocate);
7533 }
7534
7535 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7536 partial symtabs. We combine several TUs per psymtab to not let the size
7537 of any one psymtab grow too big. */
7538 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7539 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7540
7541 /* Helper routine for get_type_unit_group.
7542 Create the type_unit_group object used to hold one or more TUs. */
7543
7544 static struct type_unit_group *
7545 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7546 {
7547 struct dwarf2_per_objfile *dwarf2_per_objfile
7548 = cu->per_cu->dwarf2_per_objfile;
7549 struct objfile *objfile = dwarf2_per_objfile->objfile;
7550 struct dwarf2_per_cu_data *per_cu;
7551 struct type_unit_group *tu_group;
7552
7553 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7554 struct type_unit_group);
7555 per_cu = &tu_group->per_cu;
7556 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7557
7558 if (dwarf2_per_objfile->using_index)
7559 {
7560 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7561 struct dwarf2_per_cu_quick_data);
7562 }
7563 else
7564 {
7565 unsigned int line_offset = to_underlying (line_offset_struct);
7566 dwarf2_psymtab *pst;
7567 std::string name;
7568
7569 /* Give the symtab a useful name for debug purposes. */
7570 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7571 name = string_printf ("<type_units_%d>",
7572 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7573 else
7574 name = string_printf ("<type_units_at_0x%x>", line_offset);
7575
7576 pst = create_partial_symtab (per_cu, name.c_str ());
7577 pst->anonymous = true;
7578 }
7579
7580 tu_group->hash.dwo_unit = cu->dwo_unit;
7581 tu_group->hash.line_sect_off = line_offset_struct;
7582
7583 return tu_group;
7584 }
7585
7586 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7587 STMT_LIST is a DW_AT_stmt_list attribute. */
7588
7589 static struct type_unit_group *
7590 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7591 {
7592 struct dwarf2_per_objfile *dwarf2_per_objfile
7593 = cu->per_cu->dwarf2_per_objfile;
7594 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7595 struct type_unit_group *tu_group;
7596 void **slot;
7597 unsigned int line_offset;
7598 struct type_unit_group type_unit_group_for_lookup;
7599
7600 if (dwarf2_per_objfile->type_unit_groups == NULL)
7601 {
7602 dwarf2_per_objfile->type_unit_groups =
7603 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7604 }
7605
7606 /* Do we need to create a new group, or can we use an existing one? */
7607
7608 if (stmt_list)
7609 {
7610 line_offset = DW_UNSND (stmt_list);
7611 ++tu_stats->nr_symtab_sharers;
7612 }
7613 else
7614 {
7615 /* Ugh, no stmt_list. Rare, but we have to handle it.
7616 We can do various things here like create one group per TU or
7617 spread them over multiple groups to split up the expansion work.
7618 To avoid worst case scenarios (too many groups or too large groups)
7619 we, umm, group them in bunches. */
7620 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7621 | (tu_stats->nr_stmt_less_type_units
7622 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7623 ++tu_stats->nr_stmt_less_type_units;
7624 }
7625
7626 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7627 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7628 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7629 &type_unit_group_for_lookup, INSERT);
7630 if (*slot != NULL)
7631 {
7632 tu_group = (struct type_unit_group *) *slot;
7633 gdb_assert (tu_group != NULL);
7634 }
7635 else
7636 {
7637 sect_offset line_offset_struct = (sect_offset) line_offset;
7638 tu_group = create_type_unit_group (cu, line_offset_struct);
7639 *slot = tu_group;
7640 ++tu_stats->nr_symtabs;
7641 }
7642
7643 return tu_group;
7644 }
7645 \f
7646 /* Partial symbol tables. */
7647
7648 /* Create a psymtab named NAME and assign it to PER_CU.
7649
7650 The caller must fill in the following details:
7651 dirname, textlow, texthigh. */
7652
7653 static dwarf2_psymtab *
7654 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7655 {
7656 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7657 dwarf2_psymtab *pst;
7658
7659 pst = new dwarf2_psymtab (name, objfile, 0);
7660
7661 pst->psymtabs_addrmap_supported = true;
7662
7663 /* This is the glue that links PST into GDB's symbol API. */
7664 pst->per_cu_data = per_cu;
7665 per_cu->v.psymtab = pst;
7666
7667 return pst;
7668 }
7669
7670 /* DIE reader function for process_psymtab_comp_unit. */
7671
7672 static void
7673 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7674 const gdb_byte *info_ptr,
7675 struct die_info *comp_unit_die,
7676 int want_partial_unit,
7677 enum language pretend_language)
7678 {
7679 struct dwarf2_cu *cu = reader->cu;
7680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7682 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7683 CORE_ADDR baseaddr;
7684 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7685 dwarf2_psymtab *pst;
7686 enum pc_bounds_kind cu_bounds_kind;
7687 const char *filename;
7688
7689 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7690 return;
7691
7692 gdb_assert (! per_cu->is_debug_types);
7693
7694 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7695
7696 /* Allocate a new partial symbol table structure. */
7697 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7698 if (filename == NULL)
7699 filename = "";
7700
7701 pst = create_partial_symtab (per_cu, filename);
7702
7703 /* This must be done before calling dwarf2_build_include_psymtabs. */
7704 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7705
7706 baseaddr = objfile->text_section_offset ();
7707
7708 dwarf2_find_base_address (comp_unit_die, cu);
7709
7710 /* Possibly set the default values of LOWPC and HIGHPC from
7711 `DW_AT_ranges'. */
7712 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7713 &best_highpc, cu, pst);
7714 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7715 {
7716 CORE_ADDR low
7717 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7718 - baseaddr);
7719 CORE_ADDR high
7720 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7721 - baseaddr - 1);
7722 /* Store the contiguous range if it is not empty; it can be
7723 empty for CUs with no code. */
7724 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7725 low, high, pst);
7726 }
7727
7728 /* Check if comp unit has_children.
7729 If so, read the rest of the partial symbols from this comp unit.
7730 If not, there's no more debug_info for this comp unit. */
7731 if (comp_unit_die->has_children)
7732 {
7733 struct partial_die_info *first_die;
7734 CORE_ADDR lowpc, highpc;
7735
7736 lowpc = ((CORE_ADDR) -1);
7737 highpc = ((CORE_ADDR) 0);
7738
7739 first_die = load_partial_dies (reader, info_ptr, 1);
7740
7741 scan_partial_symbols (first_die, &lowpc, &highpc,
7742 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7743
7744 /* If we didn't find a lowpc, set it to highpc to avoid
7745 complaints from `maint check'. */
7746 if (lowpc == ((CORE_ADDR) -1))
7747 lowpc = highpc;
7748
7749 /* If the compilation unit didn't have an explicit address range,
7750 then use the information extracted from its child dies. */
7751 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7752 {
7753 best_lowpc = lowpc;
7754 best_highpc = highpc;
7755 }
7756 }
7757 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7758 best_lowpc + baseaddr)
7759 - baseaddr);
7760 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7761 best_highpc + baseaddr)
7762 - baseaddr);
7763
7764 end_psymtab_common (objfile, pst);
7765
7766 if (!cu->per_cu->imported_symtabs_empty ())
7767 {
7768 int i;
7769 int len = cu->per_cu->imported_symtabs_size ();
7770
7771 /* Fill in 'dependencies' here; we fill in 'users' in a
7772 post-pass. */
7773 pst->number_of_dependencies = len;
7774 pst->dependencies
7775 = objfile->partial_symtabs->allocate_dependencies (len);
7776 for (i = 0; i < len; ++i)
7777 {
7778 pst->dependencies[i]
7779 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7780 }
7781
7782 cu->per_cu->imported_symtabs_free ();
7783 }
7784
7785 /* Get the list of files included in the current compilation unit,
7786 and build a psymtab for each of them. */
7787 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7788
7789 if (dwarf_read_debug)
7790 fprintf_unfiltered (gdb_stdlog,
7791 "Psymtab for %s unit @%s: %s - %s"
7792 ", %d global, %d static syms\n",
7793 per_cu->is_debug_types ? "type" : "comp",
7794 sect_offset_str (per_cu->sect_off),
7795 paddress (gdbarch, pst->text_low (objfile)),
7796 paddress (gdbarch, pst->text_high (objfile)),
7797 pst->n_global_syms, pst->n_static_syms);
7798 }
7799
7800 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7801 Process compilation unit THIS_CU for a psymtab. */
7802
7803 static void
7804 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7805 int want_partial_unit,
7806 enum language pretend_language)
7807 {
7808 /* If this compilation unit was already read in, free the
7809 cached copy in order to read it in again. This is
7810 necessary because we skipped some symbols when we first
7811 read in the compilation unit (see load_partial_dies).
7812 This problem could be avoided, but the benefit is unclear. */
7813 if (this_cu->cu != NULL)
7814 free_one_cached_comp_unit (this_cu);
7815
7816 cutu_reader reader (this_cu, NULL, 0, 0, false);
7817
7818 if (reader.dummy_p)
7819 {
7820 /* Nothing. */
7821 }
7822 else if (this_cu->is_debug_types)
7823 build_type_psymtabs_reader (&reader, reader.info_ptr,
7824 reader.comp_unit_die);
7825 else
7826 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7827 reader.comp_unit_die,
7828 want_partial_unit,
7829 pretend_language);
7830
7831 /* Age out any secondary CUs. */
7832 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7833 }
7834
7835 /* Reader function for build_type_psymtabs. */
7836
7837 static void
7838 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7839 const gdb_byte *info_ptr,
7840 struct die_info *type_unit_die)
7841 {
7842 struct dwarf2_per_objfile *dwarf2_per_objfile
7843 = reader->cu->per_cu->dwarf2_per_objfile;
7844 struct objfile *objfile = dwarf2_per_objfile->objfile;
7845 struct dwarf2_cu *cu = reader->cu;
7846 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7847 struct signatured_type *sig_type;
7848 struct type_unit_group *tu_group;
7849 struct attribute *attr;
7850 struct partial_die_info *first_die;
7851 CORE_ADDR lowpc, highpc;
7852 dwarf2_psymtab *pst;
7853
7854 gdb_assert (per_cu->is_debug_types);
7855 sig_type = (struct signatured_type *) per_cu;
7856
7857 if (! type_unit_die->has_children)
7858 return;
7859
7860 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7861 tu_group = get_type_unit_group (cu, attr);
7862
7863 if (tu_group->tus == nullptr)
7864 tu_group->tus = new std::vector<signatured_type *>;
7865 tu_group->tus->push_back (sig_type);
7866
7867 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7868 pst = create_partial_symtab (per_cu, "");
7869 pst->anonymous = true;
7870
7871 first_die = load_partial_dies (reader, info_ptr, 1);
7872
7873 lowpc = (CORE_ADDR) -1;
7874 highpc = (CORE_ADDR) 0;
7875 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7876
7877 end_psymtab_common (objfile, pst);
7878 }
7879
7880 /* Struct used to sort TUs by their abbreviation table offset. */
7881
7882 struct tu_abbrev_offset
7883 {
7884 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7885 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7886 {}
7887
7888 signatured_type *sig_type;
7889 sect_offset abbrev_offset;
7890 };
7891
7892 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7893
7894 static bool
7895 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7896 const struct tu_abbrev_offset &b)
7897 {
7898 return a.abbrev_offset < b.abbrev_offset;
7899 }
7900
7901 /* Efficiently read all the type units.
7902 This does the bulk of the work for build_type_psymtabs.
7903
7904 The efficiency is because we sort TUs by the abbrev table they use and
7905 only read each abbrev table once. In one program there are 200K TUs
7906 sharing 8K abbrev tables.
7907
7908 The main purpose of this function is to support building the
7909 dwarf2_per_objfile->type_unit_groups table.
7910 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7911 can collapse the search space by grouping them by stmt_list.
7912 The savings can be significant, in the same program from above the 200K TUs
7913 share 8K stmt_list tables.
7914
7915 FUNC is expected to call get_type_unit_group, which will create the
7916 struct type_unit_group if necessary and add it to
7917 dwarf2_per_objfile->type_unit_groups. */
7918
7919 static void
7920 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7921 {
7922 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7923 abbrev_table_up abbrev_table;
7924 sect_offset abbrev_offset;
7925
7926 /* It's up to the caller to not call us multiple times. */
7927 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7928
7929 if (dwarf2_per_objfile->all_type_units.empty ())
7930 return;
7931
7932 /* TUs typically share abbrev tables, and there can be way more TUs than
7933 abbrev tables. Sort by abbrev table to reduce the number of times we
7934 read each abbrev table in.
7935 Alternatives are to punt or to maintain a cache of abbrev tables.
7936 This is simpler and efficient enough for now.
7937
7938 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7939 symtab to use). Typically TUs with the same abbrev offset have the same
7940 stmt_list value too so in practice this should work well.
7941
7942 The basic algorithm here is:
7943
7944 sort TUs by abbrev table
7945 for each TU with same abbrev table:
7946 read abbrev table if first user
7947 read TU top level DIE
7948 [IWBN if DWO skeletons had DW_AT_stmt_list]
7949 call FUNC */
7950
7951 if (dwarf_read_debug)
7952 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7953
7954 /* Sort in a separate table to maintain the order of all_type_units
7955 for .gdb_index: TU indices directly index all_type_units. */
7956 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7957 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7958
7959 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7960 sorted_by_abbrev.emplace_back
7961 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7962 sig_type->per_cu.section,
7963 sig_type->per_cu.sect_off));
7964
7965 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7966 sort_tu_by_abbrev_offset);
7967
7968 abbrev_offset = (sect_offset) ~(unsigned) 0;
7969
7970 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7971 {
7972 /* Switch to the next abbrev table if necessary. */
7973 if (abbrev_table == NULL
7974 || tu.abbrev_offset != abbrev_offset)
7975 {
7976 abbrev_offset = tu.abbrev_offset;
7977 abbrev_table =
7978 abbrev_table_read_table (dwarf2_per_objfile->objfile,
7979 &dwarf2_per_objfile->abbrev,
7980 abbrev_offset);
7981 ++tu_stats->nr_uniq_abbrev_tables;
7982 }
7983
7984 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7985 0, 0, false);
7986 if (!reader.dummy_p)
7987 build_type_psymtabs_reader (&reader, reader.info_ptr,
7988 reader.comp_unit_die);
7989 }
7990 }
7991
7992 /* Print collected type unit statistics. */
7993
7994 static void
7995 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7996 {
7997 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7998
7999 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8000 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8001 dwarf2_per_objfile->all_type_units.size ());
8002 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8003 tu_stats->nr_uniq_abbrev_tables);
8004 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8005 tu_stats->nr_symtabs);
8006 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8007 tu_stats->nr_symtab_sharers);
8008 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8009 tu_stats->nr_stmt_less_type_units);
8010 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8011 tu_stats->nr_all_type_units_reallocs);
8012 }
8013
8014 /* Traversal function for build_type_psymtabs. */
8015
8016 static int
8017 build_type_psymtab_dependencies (void **slot, void *info)
8018 {
8019 struct dwarf2_per_objfile *dwarf2_per_objfile
8020 = (struct dwarf2_per_objfile *) info;
8021 struct objfile *objfile = dwarf2_per_objfile->objfile;
8022 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8023 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8024 dwarf2_psymtab *pst = per_cu->v.psymtab;
8025 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8026 int i;
8027
8028 gdb_assert (len > 0);
8029 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8030
8031 pst->number_of_dependencies = len;
8032 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8033 for (i = 0; i < len; ++i)
8034 {
8035 struct signatured_type *iter = tu_group->tus->at (i);
8036 gdb_assert (iter->per_cu.is_debug_types);
8037 pst->dependencies[i] = iter->per_cu.v.psymtab;
8038 iter->type_unit_group = tu_group;
8039 }
8040
8041 delete tu_group->tus;
8042 tu_group->tus = nullptr;
8043
8044 return 1;
8045 }
8046
8047 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8048 Build partial symbol tables for the .debug_types comp-units. */
8049
8050 static void
8051 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8052 {
8053 if (! create_all_type_units (dwarf2_per_objfile))
8054 return;
8055
8056 build_type_psymtabs_1 (dwarf2_per_objfile);
8057 }
8058
8059 /* Traversal function for process_skeletonless_type_unit.
8060 Read a TU in a DWO file and build partial symbols for it. */
8061
8062 static int
8063 process_skeletonless_type_unit (void **slot, void *info)
8064 {
8065 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8066 struct dwarf2_per_objfile *dwarf2_per_objfile
8067 = (struct dwarf2_per_objfile *) info;
8068 struct signatured_type find_entry, *entry;
8069
8070 /* If this TU doesn't exist in the global table, add it and read it in. */
8071
8072 if (dwarf2_per_objfile->signatured_types == NULL)
8073 {
8074 dwarf2_per_objfile->signatured_types
8075 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8076 }
8077
8078 find_entry.signature = dwo_unit->signature;
8079 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
8080 &find_entry, INSERT);
8081 /* If we've already seen this type there's nothing to do. What's happening
8082 is we're doing our own version of comdat-folding here. */
8083 if (*slot != NULL)
8084 return 1;
8085
8086 /* This does the job that create_all_type_units would have done for
8087 this TU. */
8088 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8089 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8090 *slot = entry;
8091
8092 /* This does the job that build_type_psymtabs_1 would have done. */
8093 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8094 if (!reader.dummy_p)
8095 build_type_psymtabs_reader (&reader, reader.info_ptr,
8096 reader.comp_unit_die);
8097
8098 return 1;
8099 }
8100
8101 /* Traversal function for process_skeletonless_type_units. */
8102
8103 static int
8104 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8105 {
8106 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8107
8108 if (dwo_file->tus != NULL)
8109 htab_traverse_noresize (dwo_file->tus.get (),
8110 process_skeletonless_type_unit, info);
8111
8112 return 1;
8113 }
8114
8115 /* Scan all TUs of DWO files, verifying we've processed them.
8116 This is needed in case a TU was emitted without its skeleton.
8117 Note: This can't be done until we know what all the DWO files are. */
8118
8119 static void
8120 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8121 {
8122 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8123 if (get_dwp_file (dwarf2_per_objfile) == NULL
8124 && dwarf2_per_objfile->dwo_files != NULL)
8125 {
8126 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8127 process_dwo_file_for_skeletonless_type_units,
8128 dwarf2_per_objfile);
8129 }
8130 }
8131
8132 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8133
8134 static void
8135 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8136 {
8137 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8138 {
8139 dwarf2_psymtab *pst = per_cu->v.psymtab;
8140
8141 if (pst == NULL)
8142 continue;
8143
8144 for (int j = 0; j < pst->number_of_dependencies; ++j)
8145 {
8146 /* Set the 'user' field only if it is not already set. */
8147 if (pst->dependencies[j]->user == NULL)
8148 pst->dependencies[j]->user = pst;
8149 }
8150 }
8151 }
8152
8153 /* Build the partial symbol table by doing a quick pass through the
8154 .debug_info and .debug_abbrev sections. */
8155
8156 static void
8157 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8158 {
8159 struct objfile *objfile = dwarf2_per_objfile->objfile;
8160
8161 if (dwarf_read_debug)
8162 {
8163 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8164 objfile_name (objfile));
8165 }
8166
8167 dwarf2_per_objfile->reading_partial_symbols = 1;
8168
8169 dwarf2_per_objfile->info.read (objfile);
8170
8171 /* Any cached compilation units will be linked by the per-objfile
8172 read_in_chain. Make sure to free them when we're done. */
8173 free_cached_comp_units freer (dwarf2_per_objfile);
8174
8175 build_type_psymtabs (dwarf2_per_objfile);
8176
8177 create_all_comp_units (dwarf2_per_objfile);
8178
8179 /* Create a temporary address map on a temporary obstack. We later
8180 copy this to the final obstack. */
8181 auto_obstack temp_obstack;
8182
8183 scoped_restore save_psymtabs_addrmap
8184 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8185 addrmap_create_mutable (&temp_obstack));
8186
8187 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8188 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8189
8190 /* This has to wait until we read the CUs, we need the list of DWOs. */
8191 process_skeletonless_type_units (dwarf2_per_objfile);
8192
8193 /* Now that all TUs have been processed we can fill in the dependencies. */
8194 if (dwarf2_per_objfile->type_unit_groups != NULL)
8195 {
8196 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8197 build_type_psymtab_dependencies, dwarf2_per_objfile);
8198 }
8199
8200 if (dwarf_read_debug)
8201 print_tu_stats (dwarf2_per_objfile);
8202
8203 set_partial_user (dwarf2_per_objfile);
8204
8205 objfile->partial_symtabs->psymtabs_addrmap
8206 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8207 objfile->partial_symtabs->obstack ());
8208 /* At this point we want to keep the address map. */
8209 save_psymtabs_addrmap.release ();
8210
8211 if (dwarf_read_debug)
8212 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8213 objfile_name (objfile));
8214 }
8215
8216 /* Load the partial DIEs for a secondary CU into memory.
8217 This is also used when rereading a primary CU with load_all_dies. */
8218
8219 static void
8220 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8221 {
8222 cutu_reader reader (this_cu, NULL, 1, 1, false);
8223
8224 if (!reader.dummy_p)
8225 {
8226 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8227 language_minimal);
8228
8229 /* Check if comp unit has_children.
8230 If so, read the rest of the partial symbols from this comp unit.
8231 If not, there's no more debug_info for this comp unit. */
8232 if (reader.comp_unit_die->has_children)
8233 load_partial_dies (&reader, reader.info_ptr, 0);
8234 }
8235 }
8236
8237 static void
8238 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8239 struct dwarf2_section_info *section,
8240 struct dwarf2_section_info *abbrev_section,
8241 unsigned int is_dwz)
8242 {
8243 const gdb_byte *info_ptr;
8244 struct objfile *objfile = dwarf2_per_objfile->objfile;
8245
8246 if (dwarf_read_debug)
8247 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8248 section->get_name (),
8249 section->get_file_name ());
8250
8251 section->read (objfile);
8252
8253 info_ptr = section->buffer;
8254
8255 while (info_ptr < section->buffer + section->size)
8256 {
8257 struct dwarf2_per_cu_data *this_cu;
8258
8259 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8260
8261 comp_unit_head cu_header;
8262 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8263 abbrev_section, info_ptr,
8264 rcuh_kind::COMPILE);
8265
8266 /* Save the compilation unit for later lookup. */
8267 if (cu_header.unit_type != DW_UT_type)
8268 {
8269 this_cu = XOBNEW (&objfile->objfile_obstack,
8270 struct dwarf2_per_cu_data);
8271 memset (this_cu, 0, sizeof (*this_cu));
8272 }
8273 else
8274 {
8275 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8276 struct signatured_type);
8277 memset (sig_type, 0, sizeof (*sig_type));
8278 sig_type->signature = cu_header.signature;
8279 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8280 this_cu = &sig_type->per_cu;
8281 }
8282 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8283 this_cu->sect_off = sect_off;
8284 this_cu->length = cu_header.length + cu_header.initial_length_size;
8285 this_cu->is_dwz = is_dwz;
8286 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8287 this_cu->section = section;
8288
8289 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8290
8291 info_ptr = info_ptr + this_cu->length;
8292 }
8293 }
8294
8295 /* Create a list of all compilation units in OBJFILE.
8296 This is only done for -readnow and building partial symtabs. */
8297
8298 static void
8299 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8300 {
8301 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8302 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8303 &dwarf2_per_objfile->abbrev, 0);
8304
8305 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8306 if (dwz != NULL)
8307 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8308 1);
8309 }
8310
8311 /* Process all loaded DIEs for compilation unit CU, starting at
8312 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8313 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8314 DW_AT_ranges). See the comments of add_partial_subprogram on how
8315 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8316
8317 static void
8318 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8319 CORE_ADDR *highpc, int set_addrmap,
8320 struct dwarf2_cu *cu)
8321 {
8322 struct partial_die_info *pdi;
8323
8324 /* Now, march along the PDI's, descending into ones which have
8325 interesting children but skipping the children of the other ones,
8326 until we reach the end of the compilation unit. */
8327
8328 pdi = first_die;
8329
8330 while (pdi != NULL)
8331 {
8332 pdi->fixup (cu);
8333
8334 /* Anonymous namespaces or modules have no name but have interesting
8335 children, so we need to look at them. Ditto for anonymous
8336 enums. */
8337
8338 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8339 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8340 || pdi->tag == DW_TAG_imported_unit
8341 || pdi->tag == DW_TAG_inlined_subroutine)
8342 {
8343 switch (pdi->tag)
8344 {
8345 case DW_TAG_subprogram:
8346 case DW_TAG_inlined_subroutine:
8347 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8348 break;
8349 case DW_TAG_constant:
8350 case DW_TAG_variable:
8351 case DW_TAG_typedef:
8352 case DW_TAG_union_type:
8353 if (!pdi->is_declaration)
8354 {
8355 add_partial_symbol (pdi, cu);
8356 }
8357 break;
8358 case DW_TAG_class_type:
8359 case DW_TAG_interface_type:
8360 case DW_TAG_structure_type:
8361 if (!pdi->is_declaration)
8362 {
8363 add_partial_symbol (pdi, cu);
8364 }
8365 if ((cu->language == language_rust
8366 || cu->language == language_cplus) && pdi->has_children)
8367 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8368 set_addrmap, cu);
8369 break;
8370 case DW_TAG_enumeration_type:
8371 if (!pdi->is_declaration)
8372 add_partial_enumeration (pdi, cu);
8373 break;
8374 case DW_TAG_base_type:
8375 case DW_TAG_subrange_type:
8376 /* File scope base type definitions are added to the partial
8377 symbol table. */
8378 add_partial_symbol (pdi, cu);
8379 break;
8380 case DW_TAG_namespace:
8381 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8382 break;
8383 case DW_TAG_module:
8384 if (!pdi->is_declaration)
8385 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8386 break;
8387 case DW_TAG_imported_unit:
8388 {
8389 struct dwarf2_per_cu_data *per_cu;
8390
8391 /* For now we don't handle imported units in type units. */
8392 if (cu->per_cu->is_debug_types)
8393 {
8394 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8395 " supported in type units [in module %s]"),
8396 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8397 }
8398
8399 per_cu = dwarf2_find_containing_comp_unit
8400 (pdi->d.sect_off, pdi->is_dwz,
8401 cu->per_cu->dwarf2_per_objfile);
8402
8403 /* Go read the partial unit, if needed. */
8404 if (per_cu->v.psymtab == NULL)
8405 process_psymtab_comp_unit (per_cu, 1, cu->language);
8406
8407 cu->per_cu->imported_symtabs_push (per_cu);
8408 }
8409 break;
8410 case DW_TAG_imported_declaration:
8411 add_partial_symbol (pdi, cu);
8412 break;
8413 default:
8414 break;
8415 }
8416 }
8417
8418 /* If the die has a sibling, skip to the sibling. */
8419
8420 pdi = pdi->die_sibling;
8421 }
8422 }
8423
8424 /* Functions used to compute the fully scoped name of a partial DIE.
8425
8426 Normally, this is simple. For C++, the parent DIE's fully scoped
8427 name is concatenated with "::" and the partial DIE's name.
8428 Enumerators are an exception; they use the scope of their parent
8429 enumeration type, i.e. the name of the enumeration type is not
8430 prepended to the enumerator.
8431
8432 There are two complexities. One is DW_AT_specification; in this
8433 case "parent" means the parent of the target of the specification,
8434 instead of the direct parent of the DIE. The other is compilers
8435 which do not emit DW_TAG_namespace; in this case we try to guess
8436 the fully qualified name of structure types from their members'
8437 linkage names. This must be done using the DIE's children rather
8438 than the children of any DW_AT_specification target. We only need
8439 to do this for structures at the top level, i.e. if the target of
8440 any DW_AT_specification (if any; otherwise the DIE itself) does not
8441 have a parent. */
8442
8443 /* Compute the scope prefix associated with PDI's parent, in
8444 compilation unit CU. The result will be allocated on CU's
8445 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8446 field. NULL is returned if no prefix is necessary. */
8447 static const char *
8448 partial_die_parent_scope (struct partial_die_info *pdi,
8449 struct dwarf2_cu *cu)
8450 {
8451 const char *grandparent_scope;
8452 struct partial_die_info *parent, *real_pdi;
8453
8454 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8455 then this means the parent of the specification DIE. */
8456
8457 real_pdi = pdi;
8458 while (real_pdi->has_specification)
8459 {
8460 auto res = find_partial_die (real_pdi->spec_offset,
8461 real_pdi->spec_is_dwz, cu);
8462 real_pdi = res.pdi;
8463 cu = res.cu;
8464 }
8465
8466 parent = real_pdi->die_parent;
8467 if (parent == NULL)
8468 return NULL;
8469
8470 if (parent->scope_set)
8471 return parent->scope;
8472
8473 parent->fixup (cu);
8474
8475 grandparent_scope = partial_die_parent_scope (parent, cu);
8476
8477 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8478 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8479 Work around this problem here. */
8480 if (cu->language == language_cplus
8481 && parent->tag == DW_TAG_namespace
8482 && strcmp (parent->name, "::") == 0
8483 && grandparent_scope == NULL)
8484 {
8485 parent->scope = NULL;
8486 parent->scope_set = 1;
8487 return NULL;
8488 }
8489
8490 /* Nested subroutines in Fortran get a prefix. */
8491 if (pdi->tag == DW_TAG_enumerator)
8492 /* Enumerators should not get the name of the enumeration as a prefix. */
8493 parent->scope = grandparent_scope;
8494 else if (parent->tag == DW_TAG_namespace
8495 || parent->tag == DW_TAG_module
8496 || parent->tag == DW_TAG_structure_type
8497 || parent->tag == DW_TAG_class_type
8498 || parent->tag == DW_TAG_interface_type
8499 || parent->tag == DW_TAG_union_type
8500 || parent->tag == DW_TAG_enumeration_type
8501 || (cu->language == language_fortran
8502 && parent->tag == DW_TAG_subprogram
8503 && pdi->tag == DW_TAG_subprogram))
8504 {
8505 if (grandparent_scope == NULL)
8506 parent->scope = parent->name;
8507 else
8508 parent->scope = typename_concat (&cu->comp_unit_obstack,
8509 grandparent_scope,
8510 parent->name, 0, cu);
8511 }
8512 else
8513 {
8514 /* FIXME drow/2004-04-01: What should we be doing with
8515 function-local names? For partial symbols, we should probably be
8516 ignoring them. */
8517 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8518 dwarf_tag_name (parent->tag),
8519 sect_offset_str (pdi->sect_off));
8520 parent->scope = grandparent_scope;
8521 }
8522
8523 parent->scope_set = 1;
8524 return parent->scope;
8525 }
8526
8527 /* Return the fully scoped name associated with PDI, from compilation unit
8528 CU. The result will be allocated with malloc. */
8529
8530 static gdb::unique_xmalloc_ptr<char>
8531 partial_die_full_name (struct partial_die_info *pdi,
8532 struct dwarf2_cu *cu)
8533 {
8534 const char *parent_scope;
8535
8536 /* If this is a template instantiation, we can not work out the
8537 template arguments from partial DIEs. So, unfortunately, we have
8538 to go through the full DIEs. At least any work we do building
8539 types here will be reused if full symbols are loaded later. */
8540 if (pdi->has_template_arguments)
8541 {
8542 pdi->fixup (cu);
8543
8544 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8545 {
8546 struct die_info *die;
8547 struct attribute attr;
8548 struct dwarf2_cu *ref_cu = cu;
8549
8550 /* DW_FORM_ref_addr is using section offset. */
8551 attr.name = (enum dwarf_attribute) 0;
8552 attr.form = DW_FORM_ref_addr;
8553 attr.u.unsnd = to_underlying (pdi->sect_off);
8554 die = follow_die_ref (NULL, &attr, &ref_cu);
8555
8556 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8557 }
8558 }
8559
8560 parent_scope = partial_die_parent_scope (pdi, cu);
8561 if (parent_scope == NULL)
8562 return NULL;
8563 else
8564 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8565 pdi->name, 0, cu));
8566 }
8567
8568 static void
8569 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8570 {
8571 struct dwarf2_per_objfile *dwarf2_per_objfile
8572 = cu->per_cu->dwarf2_per_objfile;
8573 struct objfile *objfile = dwarf2_per_objfile->objfile;
8574 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8575 CORE_ADDR addr = 0;
8576 const char *actual_name = NULL;
8577 CORE_ADDR baseaddr;
8578
8579 baseaddr = objfile->text_section_offset ();
8580
8581 gdb::unique_xmalloc_ptr<char> built_actual_name
8582 = partial_die_full_name (pdi, cu);
8583 if (built_actual_name != NULL)
8584 actual_name = built_actual_name.get ();
8585
8586 if (actual_name == NULL)
8587 actual_name = pdi->name;
8588
8589 switch (pdi->tag)
8590 {
8591 case DW_TAG_inlined_subroutine:
8592 case DW_TAG_subprogram:
8593 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8594 - baseaddr);
8595 if (pdi->is_external
8596 || cu->language == language_ada
8597 || (cu->language == language_fortran
8598 && pdi->die_parent != NULL
8599 && pdi->die_parent->tag == DW_TAG_subprogram))
8600 {
8601 /* Normally, only "external" DIEs are part of the global scope.
8602 But in Ada and Fortran, we want to be able to access nested
8603 procedures globally. So all Ada and Fortran subprograms are
8604 stored in the global scope. */
8605 add_psymbol_to_list (actual_name,
8606 built_actual_name != NULL,
8607 VAR_DOMAIN, LOC_BLOCK,
8608 SECT_OFF_TEXT (objfile),
8609 psymbol_placement::GLOBAL,
8610 addr,
8611 cu->language, objfile);
8612 }
8613 else
8614 {
8615 add_psymbol_to_list (actual_name,
8616 built_actual_name != NULL,
8617 VAR_DOMAIN, LOC_BLOCK,
8618 SECT_OFF_TEXT (objfile),
8619 psymbol_placement::STATIC,
8620 addr, cu->language, objfile);
8621 }
8622
8623 if (pdi->main_subprogram && actual_name != NULL)
8624 set_objfile_main_name (objfile, actual_name, cu->language);
8625 break;
8626 case DW_TAG_constant:
8627 add_psymbol_to_list (actual_name,
8628 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8629 -1, (pdi->is_external
8630 ? psymbol_placement::GLOBAL
8631 : psymbol_placement::STATIC),
8632 0, cu->language, objfile);
8633 break;
8634 case DW_TAG_variable:
8635 if (pdi->d.locdesc)
8636 addr = decode_locdesc (pdi->d.locdesc, cu);
8637
8638 if (pdi->d.locdesc
8639 && addr == 0
8640 && !dwarf2_per_objfile->has_section_at_zero)
8641 {
8642 /* A global or static variable may also have been stripped
8643 out by the linker if unused, in which case its address
8644 will be nullified; do not add such variables into partial
8645 symbol table then. */
8646 }
8647 else if (pdi->is_external)
8648 {
8649 /* Global Variable.
8650 Don't enter into the minimal symbol tables as there is
8651 a minimal symbol table entry from the ELF symbols already.
8652 Enter into partial symbol table if it has a location
8653 descriptor or a type.
8654 If the location descriptor is missing, new_symbol will create
8655 a LOC_UNRESOLVED symbol, the address of the variable will then
8656 be determined from the minimal symbol table whenever the variable
8657 is referenced.
8658 The address for the partial symbol table entry is not
8659 used by GDB, but it comes in handy for debugging partial symbol
8660 table building. */
8661
8662 if (pdi->d.locdesc || pdi->has_type)
8663 add_psymbol_to_list (actual_name,
8664 built_actual_name != NULL,
8665 VAR_DOMAIN, LOC_STATIC,
8666 SECT_OFF_TEXT (objfile),
8667 psymbol_placement::GLOBAL,
8668 addr, cu->language, objfile);
8669 }
8670 else
8671 {
8672 int has_loc = pdi->d.locdesc != NULL;
8673
8674 /* Static Variable. Skip symbols whose value we cannot know (those
8675 without location descriptors or constant values). */
8676 if (!has_loc && !pdi->has_const_value)
8677 return;
8678
8679 add_psymbol_to_list (actual_name,
8680 built_actual_name != NULL,
8681 VAR_DOMAIN, LOC_STATIC,
8682 SECT_OFF_TEXT (objfile),
8683 psymbol_placement::STATIC,
8684 has_loc ? addr : 0,
8685 cu->language, objfile);
8686 }
8687 break;
8688 case DW_TAG_typedef:
8689 case DW_TAG_base_type:
8690 case DW_TAG_subrange_type:
8691 add_psymbol_to_list (actual_name,
8692 built_actual_name != NULL,
8693 VAR_DOMAIN, LOC_TYPEDEF, -1,
8694 psymbol_placement::STATIC,
8695 0, cu->language, objfile);
8696 break;
8697 case DW_TAG_imported_declaration:
8698 case DW_TAG_namespace:
8699 add_psymbol_to_list (actual_name,
8700 built_actual_name != NULL,
8701 VAR_DOMAIN, LOC_TYPEDEF, -1,
8702 psymbol_placement::GLOBAL,
8703 0, cu->language, objfile);
8704 break;
8705 case DW_TAG_module:
8706 /* With Fortran 77 there might be a "BLOCK DATA" module
8707 available without any name. If so, we skip the module as it
8708 doesn't bring any value. */
8709 if (actual_name != nullptr)
8710 add_psymbol_to_list (actual_name,
8711 built_actual_name != NULL,
8712 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8713 psymbol_placement::GLOBAL,
8714 0, cu->language, objfile);
8715 break;
8716 case DW_TAG_class_type:
8717 case DW_TAG_interface_type:
8718 case DW_TAG_structure_type:
8719 case DW_TAG_union_type:
8720 case DW_TAG_enumeration_type:
8721 /* Skip external references. The DWARF standard says in the section
8722 about "Structure, Union, and Class Type Entries": "An incomplete
8723 structure, union or class type is represented by a structure,
8724 union or class entry that does not have a byte size attribute
8725 and that has a DW_AT_declaration attribute." */
8726 if (!pdi->has_byte_size && pdi->is_declaration)
8727 return;
8728
8729 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8730 static vs. global. */
8731 add_psymbol_to_list (actual_name,
8732 built_actual_name != NULL,
8733 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8734 cu->language == language_cplus
8735 ? psymbol_placement::GLOBAL
8736 : psymbol_placement::STATIC,
8737 0, cu->language, objfile);
8738
8739 break;
8740 case DW_TAG_enumerator:
8741 add_psymbol_to_list (actual_name,
8742 built_actual_name != NULL,
8743 VAR_DOMAIN, LOC_CONST, -1,
8744 cu->language == language_cplus
8745 ? psymbol_placement::GLOBAL
8746 : psymbol_placement::STATIC,
8747 0, cu->language, objfile);
8748 break;
8749 default:
8750 break;
8751 }
8752 }
8753
8754 /* Read a partial die corresponding to a namespace; also, add a symbol
8755 corresponding to that namespace to the symbol table. NAMESPACE is
8756 the name of the enclosing namespace. */
8757
8758 static void
8759 add_partial_namespace (struct partial_die_info *pdi,
8760 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8761 int set_addrmap, struct dwarf2_cu *cu)
8762 {
8763 /* Add a symbol for the namespace. */
8764
8765 add_partial_symbol (pdi, cu);
8766
8767 /* Now scan partial symbols in that namespace. */
8768
8769 if (pdi->has_children)
8770 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8771 }
8772
8773 /* Read a partial die corresponding to a Fortran module. */
8774
8775 static void
8776 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8777 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8778 {
8779 /* Add a symbol for the namespace. */
8780
8781 add_partial_symbol (pdi, cu);
8782
8783 /* Now scan partial symbols in that module. */
8784
8785 if (pdi->has_children)
8786 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8787 }
8788
8789 /* Read a partial die corresponding to a subprogram or an inlined
8790 subprogram and create a partial symbol for that subprogram.
8791 When the CU language allows it, this routine also defines a partial
8792 symbol for each nested subprogram that this subprogram contains.
8793 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8794 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8795
8796 PDI may also be a lexical block, in which case we simply search
8797 recursively for subprograms defined inside that lexical block.
8798 Again, this is only performed when the CU language allows this
8799 type of definitions. */
8800
8801 static void
8802 add_partial_subprogram (struct partial_die_info *pdi,
8803 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8804 int set_addrmap, struct dwarf2_cu *cu)
8805 {
8806 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8807 {
8808 if (pdi->has_pc_info)
8809 {
8810 if (pdi->lowpc < *lowpc)
8811 *lowpc = pdi->lowpc;
8812 if (pdi->highpc > *highpc)
8813 *highpc = pdi->highpc;
8814 if (set_addrmap)
8815 {
8816 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8817 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8818 CORE_ADDR baseaddr;
8819 CORE_ADDR this_highpc;
8820 CORE_ADDR this_lowpc;
8821
8822 baseaddr = objfile->text_section_offset ();
8823 this_lowpc
8824 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8825 pdi->lowpc + baseaddr)
8826 - baseaddr);
8827 this_highpc
8828 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8829 pdi->highpc + baseaddr)
8830 - baseaddr);
8831 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8832 this_lowpc, this_highpc - 1,
8833 cu->per_cu->v.psymtab);
8834 }
8835 }
8836
8837 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8838 {
8839 if (!pdi->is_declaration)
8840 /* Ignore subprogram DIEs that do not have a name, they are
8841 illegal. Do not emit a complaint at this point, we will
8842 do so when we convert this psymtab into a symtab. */
8843 if (pdi->name)
8844 add_partial_symbol (pdi, cu);
8845 }
8846 }
8847
8848 if (! pdi->has_children)
8849 return;
8850
8851 if (cu->language == language_ada || cu->language == language_fortran)
8852 {
8853 pdi = pdi->die_child;
8854 while (pdi != NULL)
8855 {
8856 pdi->fixup (cu);
8857 if (pdi->tag == DW_TAG_subprogram
8858 || pdi->tag == DW_TAG_inlined_subroutine
8859 || pdi->tag == DW_TAG_lexical_block)
8860 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8861 pdi = pdi->die_sibling;
8862 }
8863 }
8864 }
8865
8866 /* Read a partial die corresponding to an enumeration type. */
8867
8868 static void
8869 add_partial_enumeration (struct partial_die_info *enum_pdi,
8870 struct dwarf2_cu *cu)
8871 {
8872 struct partial_die_info *pdi;
8873
8874 if (enum_pdi->name != NULL)
8875 add_partial_symbol (enum_pdi, cu);
8876
8877 pdi = enum_pdi->die_child;
8878 while (pdi)
8879 {
8880 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8881 complaint (_("malformed enumerator DIE ignored"));
8882 else
8883 add_partial_symbol (pdi, cu);
8884 pdi = pdi->die_sibling;
8885 }
8886 }
8887
8888 /* Return the initial uleb128 in the die at INFO_PTR. */
8889
8890 static unsigned int
8891 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8892 {
8893 unsigned int bytes_read;
8894
8895 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8896 }
8897
8898 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8899 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8900
8901 Return the corresponding abbrev, or NULL if the number is zero (indicating
8902 an empty DIE). In either case *BYTES_READ will be set to the length of
8903 the initial number. */
8904
8905 static struct abbrev_info *
8906 peek_die_abbrev (const die_reader_specs &reader,
8907 const gdb_byte *info_ptr, unsigned int *bytes_read)
8908 {
8909 dwarf2_cu *cu = reader.cu;
8910 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8911 unsigned int abbrev_number
8912 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8913
8914 if (abbrev_number == 0)
8915 return NULL;
8916
8917 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8918 if (!abbrev)
8919 {
8920 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8921 " at offset %s [in module %s]"),
8922 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8923 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8924 }
8925
8926 return abbrev;
8927 }
8928
8929 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8930 Returns a pointer to the end of a series of DIEs, terminated by an empty
8931 DIE. Any children of the skipped DIEs will also be skipped. */
8932
8933 static const gdb_byte *
8934 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8935 {
8936 while (1)
8937 {
8938 unsigned int bytes_read;
8939 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8940
8941 if (abbrev == NULL)
8942 return info_ptr + bytes_read;
8943 else
8944 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8945 }
8946 }
8947
8948 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8949 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8950 abbrev corresponding to that skipped uleb128 should be passed in
8951 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8952 children. */
8953
8954 static const gdb_byte *
8955 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8956 struct abbrev_info *abbrev)
8957 {
8958 unsigned int bytes_read;
8959 struct attribute attr;
8960 bfd *abfd = reader->abfd;
8961 struct dwarf2_cu *cu = reader->cu;
8962 const gdb_byte *buffer = reader->buffer;
8963 const gdb_byte *buffer_end = reader->buffer_end;
8964 unsigned int form, i;
8965
8966 for (i = 0; i < abbrev->num_attrs; i++)
8967 {
8968 /* The only abbrev we care about is DW_AT_sibling. */
8969 if (abbrev->attrs[i].name == DW_AT_sibling)
8970 {
8971 bool ignored;
8972 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8973 &ignored);
8974 if (attr.form == DW_FORM_ref_addr)
8975 complaint (_("ignoring absolute DW_AT_sibling"));
8976 else
8977 {
8978 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8979 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8980
8981 if (sibling_ptr < info_ptr)
8982 complaint (_("DW_AT_sibling points backwards"));
8983 else if (sibling_ptr > reader->buffer_end)
8984 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8985 else
8986 return sibling_ptr;
8987 }
8988 }
8989
8990 /* If it isn't DW_AT_sibling, skip this attribute. */
8991 form = abbrev->attrs[i].form;
8992 skip_attribute:
8993 switch (form)
8994 {
8995 case DW_FORM_ref_addr:
8996 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8997 and later it is offset sized. */
8998 if (cu->header.version == 2)
8999 info_ptr += cu->header.addr_size;
9000 else
9001 info_ptr += cu->header.offset_size;
9002 break;
9003 case DW_FORM_GNU_ref_alt:
9004 info_ptr += cu->header.offset_size;
9005 break;
9006 case DW_FORM_addr:
9007 info_ptr += cu->header.addr_size;
9008 break;
9009 case DW_FORM_data1:
9010 case DW_FORM_ref1:
9011 case DW_FORM_flag:
9012 case DW_FORM_strx1:
9013 info_ptr += 1;
9014 break;
9015 case DW_FORM_flag_present:
9016 case DW_FORM_implicit_const:
9017 break;
9018 case DW_FORM_data2:
9019 case DW_FORM_ref2:
9020 case DW_FORM_strx2:
9021 info_ptr += 2;
9022 break;
9023 case DW_FORM_strx3:
9024 info_ptr += 3;
9025 break;
9026 case DW_FORM_data4:
9027 case DW_FORM_ref4:
9028 case DW_FORM_strx4:
9029 info_ptr += 4;
9030 break;
9031 case DW_FORM_data8:
9032 case DW_FORM_ref8:
9033 case DW_FORM_ref_sig8:
9034 info_ptr += 8;
9035 break;
9036 case DW_FORM_data16:
9037 info_ptr += 16;
9038 break;
9039 case DW_FORM_string:
9040 read_direct_string (abfd, info_ptr, &bytes_read);
9041 info_ptr += bytes_read;
9042 break;
9043 case DW_FORM_sec_offset:
9044 case DW_FORM_strp:
9045 case DW_FORM_GNU_strp_alt:
9046 info_ptr += cu->header.offset_size;
9047 break;
9048 case DW_FORM_exprloc:
9049 case DW_FORM_block:
9050 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9051 info_ptr += bytes_read;
9052 break;
9053 case DW_FORM_block1:
9054 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9055 break;
9056 case DW_FORM_block2:
9057 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9058 break;
9059 case DW_FORM_block4:
9060 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9061 break;
9062 case DW_FORM_addrx:
9063 case DW_FORM_strx:
9064 case DW_FORM_sdata:
9065 case DW_FORM_udata:
9066 case DW_FORM_ref_udata:
9067 case DW_FORM_GNU_addr_index:
9068 case DW_FORM_GNU_str_index:
9069 case DW_FORM_rnglistx:
9070 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9071 break;
9072 case DW_FORM_indirect:
9073 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9074 info_ptr += bytes_read;
9075 /* We need to continue parsing from here, so just go back to
9076 the top. */
9077 goto skip_attribute;
9078
9079 default:
9080 error (_("Dwarf Error: Cannot handle %s "
9081 "in DWARF reader [in module %s]"),
9082 dwarf_form_name (form),
9083 bfd_get_filename (abfd));
9084 }
9085 }
9086
9087 if (abbrev->has_children)
9088 return skip_children (reader, info_ptr);
9089 else
9090 return info_ptr;
9091 }
9092
9093 /* Locate ORIG_PDI's sibling.
9094 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9095
9096 static const gdb_byte *
9097 locate_pdi_sibling (const struct die_reader_specs *reader,
9098 struct partial_die_info *orig_pdi,
9099 const gdb_byte *info_ptr)
9100 {
9101 /* Do we know the sibling already? */
9102
9103 if (orig_pdi->sibling)
9104 return orig_pdi->sibling;
9105
9106 /* Are there any children to deal with? */
9107
9108 if (!orig_pdi->has_children)
9109 return info_ptr;
9110
9111 /* Skip the children the long way. */
9112
9113 return skip_children (reader, info_ptr);
9114 }
9115
9116 /* Expand this partial symbol table into a full symbol table. SELF is
9117 not NULL. */
9118
9119 void
9120 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9121 {
9122 struct dwarf2_per_objfile *dwarf2_per_objfile
9123 = get_dwarf2_per_objfile (objfile);
9124
9125 gdb_assert (!readin);
9126 /* If this psymtab is constructed from a debug-only objfile, the
9127 has_section_at_zero flag will not necessarily be correct. We
9128 can get the correct value for this flag by looking at the data
9129 associated with the (presumably stripped) associated objfile. */
9130 if (objfile->separate_debug_objfile_backlink)
9131 {
9132 struct dwarf2_per_objfile *dpo_backlink
9133 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9134
9135 dwarf2_per_objfile->has_section_at_zero
9136 = dpo_backlink->has_section_at_zero;
9137 }
9138
9139 dwarf2_per_objfile->reading_partial_symbols = 0;
9140
9141 expand_psymtab (objfile);
9142
9143 process_cu_includes (dwarf2_per_objfile);
9144 }
9145 \f
9146 /* Reading in full CUs. */
9147
9148 /* Add PER_CU to the queue. */
9149
9150 static void
9151 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9152 enum language pretend_language)
9153 {
9154 per_cu->queued = 1;
9155 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
9156 }
9157
9158 /* If PER_CU is not yet queued, add it to the queue.
9159 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9160 dependency.
9161 The result is non-zero if PER_CU was queued, otherwise the result is zero
9162 meaning either PER_CU is already queued or it is already loaded.
9163
9164 N.B. There is an invariant here that if a CU is queued then it is loaded.
9165 The caller is required to load PER_CU if we return non-zero. */
9166
9167 static int
9168 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9169 struct dwarf2_per_cu_data *per_cu,
9170 enum language pretend_language)
9171 {
9172 /* We may arrive here during partial symbol reading, if we need full
9173 DIEs to process an unusual case (e.g. template arguments). Do
9174 not queue PER_CU, just tell our caller to load its DIEs. */
9175 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9176 {
9177 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9178 return 1;
9179 return 0;
9180 }
9181
9182 /* Mark the dependence relation so that we don't flush PER_CU
9183 too early. */
9184 if (dependent_cu != NULL)
9185 dwarf2_add_dependence (dependent_cu, per_cu);
9186
9187 /* If it's already on the queue, we have nothing to do. */
9188 if (per_cu->queued)
9189 return 0;
9190
9191 /* If the compilation unit is already loaded, just mark it as
9192 used. */
9193 if (per_cu->cu != NULL)
9194 {
9195 per_cu->cu->last_used = 0;
9196 return 0;
9197 }
9198
9199 /* Add it to the queue. */
9200 queue_comp_unit (per_cu, pretend_language);
9201
9202 return 1;
9203 }
9204
9205 /* Process the queue. */
9206
9207 static void
9208 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9209 {
9210 if (dwarf_read_debug)
9211 {
9212 fprintf_unfiltered (gdb_stdlog,
9213 "Expanding one or more symtabs of objfile %s ...\n",
9214 objfile_name (dwarf2_per_objfile->objfile));
9215 }
9216
9217 /* The queue starts out with one item, but following a DIE reference
9218 may load a new CU, adding it to the end of the queue. */
9219 while (!dwarf2_per_objfile->queue.empty ())
9220 {
9221 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
9222
9223 if ((dwarf2_per_objfile->using_index
9224 ? !item.per_cu->v.quick->compunit_symtab
9225 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
9226 /* Skip dummy CUs. */
9227 && item.per_cu->cu != NULL)
9228 {
9229 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9230 unsigned int debug_print_threshold;
9231 char buf[100];
9232
9233 if (per_cu->is_debug_types)
9234 {
9235 struct signatured_type *sig_type =
9236 (struct signatured_type *) per_cu;
9237
9238 sprintf (buf, "TU %s at offset %s",
9239 hex_string (sig_type->signature),
9240 sect_offset_str (per_cu->sect_off));
9241 /* There can be 100s of TUs.
9242 Only print them in verbose mode. */
9243 debug_print_threshold = 2;
9244 }
9245 else
9246 {
9247 sprintf (buf, "CU at offset %s",
9248 sect_offset_str (per_cu->sect_off));
9249 debug_print_threshold = 1;
9250 }
9251
9252 if (dwarf_read_debug >= debug_print_threshold)
9253 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9254
9255 if (per_cu->is_debug_types)
9256 process_full_type_unit (per_cu, item.pretend_language);
9257 else
9258 process_full_comp_unit (per_cu, item.pretend_language);
9259
9260 if (dwarf_read_debug >= debug_print_threshold)
9261 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9262 }
9263
9264 item.per_cu->queued = 0;
9265 dwarf2_per_objfile->queue.pop ();
9266 }
9267
9268 if (dwarf_read_debug)
9269 {
9270 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9271 objfile_name (dwarf2_per_objfile->objfile));
9272 }
9273 }
9274
9275 /* Read in full symbols for PST, and anything it depends on. */
9276
9277 void
9278 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9279 {
9280 struct dwarf2_per_cu_data *per_cu;
9281
9282 if (readin)
9283 return;
9284
9285 read_dependencies (objfile);
9286
9287 per_cu = per_cu_data;
9288
9289 if (per_cu == NULL)
9290 {
9291 /* It's an include file, no symbols to read for it.
9292 Everything is in the parent symtab. */
9293 readin = true;
9294 return;
9295 }
9296
9297 dw2_do_instantiate_symtab (per_cu, false);
9298 }
9299
9300 /* Trivial hash function for die_info: the hash value of a DIE
9301 is its offset in .debug_info for this objfile. */
9302
9303 static hashval_t
9304 die_hash (const void *item)
9305 {
9306 const struct die_info *die = (const struct die_info *) item;
9307
9308 return to_underlying (die->sect_off);
9309 }
9310
9311 /* Trivial comparison function for die_info structures: two DIEs
9312 are equal if they have the same offset. */
9313
9314 static int
9315 die_eq (const void *item_lhs, const void *item_rhs)
9316 {
9317 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9318 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9319
9320 return die_lhs->sect_off == die_rhs->sect_off;
9321 }
9322
9323 /* Load the DIEs associated with PER_CU into memory. */
9324
9325 static void
9326 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9327 bool skip_partial,
9328 enum language pretend_language)
9329 {
9330 gdb_assert (! this_cu->is_debug_types);
9331
9332 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9333 if (reader.dummy_p)
9334 return;
9335
9336 struct dwarf2_cu *cu = reader.cu;
9337 const gdb_byte *info_ptr = reader.info_ptr;
9338
9339 gdb_assert (cu->die_hash == NULL);
9340 cu->die_hash =
9341 htab_create_alloc_ex (cu->header.length / 12,
9342 die_hash,
9343 die_eq,
9344 NULL,
9345 &cu->comp_unit_obstack,
9346 hashtab_obstack_allocate,
9347 dummy_obstack_deallocate);
9348
9349 if (reader.comp_unit_die->has_children)
9350 reader.comp_unit_die->child
9351 = read_die_and_siblings (&reader, reader.info_ptr,
9352 &info_ptr, reader.comp_unit_die);
9353 cu->dies = reader.comp_unit_die;
9354 /* comp_unit_die is not stored in die_hash, no need. */
9355
9356 /* We try not to read any attributes in this function, because not
9357 all CUs needed for references have been loaded yet, and symbol
9358 table processing isn't initialized. But we have to set the CU language,
9359 or we won't be able to build types correctly.
9360 Similarly, if we do not read the producer, we can not apply
9361 producer-specific interpretation. */
9362 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9363 }
9364
9365 /* Add a DIE to the delayed physname list. */
9366
9367 static void
9368 add_to_method_list (struct type *type, int fnfield_index, int index,
9369 const char *name, struct die_info *die,
9370 struct dwarf2_cu *cu)
9371 {
9372 struct delayed_method_info mi;
9373 mi.type = type;
9374 mi.fnfield_index = fnfield_index;
9375 mi.index = index;
9376 mi.name = name;
9377 mi.die = die;
9378 cu->method_list.push_back (mi);
9379 }
9380
9381 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9382 "const" / "volatile". If so, decrements LEN by the length of the
9383 modifier and return true. Otherwise return false. */
9384
9385 template<size_t N>
9386 static bool
9387 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9388 {
9389 size_t mod_len = sizeof (mod) - 1;
9390 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9391 {
9392 len -= mod_len;
9393 return true;
9394 }
9395 return false;
9396 }
9397
9398 /* Compute the physnames of any methods on the CU's method list.
9399
9400 The computation of method physnames is delayed in order to avoid the
9401 (bad) condition that one of the method's formal parameters is of an as yet
9402 incomplete type. */
9403
9404 static void
9405 compute_delayed_physnames (struct dwarf2_cu *cu)
9406 {
9407 /* Only C++ delays computing physnames. */
9408 if (cu->method_list.empty ())
9409 return;
9410 gdb_assert (cu->language == language_cplus);
9411
9412 for (const delayed_method_info &mi : cu->method_list)
9413 {
9414 const char *physname;
9415 struct fn_fieldlist *fn_flp
9416 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9417 physname = dwarf2_physname (mi.name, mi.die, cu);
9418 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9419 = physname ? physname : "";
9420
9421 /* Since there's no tag to indicate whether a method is a
9422 const/volatile overload, extract that information out of the
9423 demangled name. */
9424 if (physname != NULL)
9425 {
9426 size_t len = strlen (physname);
9427
9428 while (1)
9429 {
9430 if (physname[len] == ')') /* shortcut */
9431 break;
9432 else if (check_modifier (physname, len, " const"))
9433 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9434 else if (check_modifier (physname, len, " volatile"))
9435 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9436 else
9437 break;
9438 }
9439 }
9440 }
9441
9442 /* The list is no longer needed. */
9443 cu->method_list.clear ();
9444 }
9445
9446 /* Go objects should be embedded in a DW_TAG_module DIE,
9447 and it's not clear if/how imported objects will appear.
9448 To keep Go support simple until that's worked out,
9449 go back through what we've read and create something usable.
9450 We could do this while processing each DIE, and feels kinda cleaner,
9451 but that way is more invasive.
9452 This is to, for example, allow the user to type "p var" or "b main"
9453 without having to specify the package name, and allow lookups
9454 of module.object to work in contexts that use the expression
9455 parser. */
9456
9457 static void
9458 fixup_go_packaging (struct dwarf2_cu *cu)
9459 {
9460 gdb::unique_xmalloc_ptr<char> package_name;
9461 struct pending *list;
9462 int i;
9463
9464 for (list = *cu->get_builder ()->get_global_symbols ();
9465 list != NULL;
9466 list = list->next)
9467 {
9468 for (i = 0; i < list->nsyms; ++i)
9469 {
9470 struct symbol *sym = list->symbol[i];
9471
9472 if (sym->language () == language_go
9473 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9474 {
9475 gdb::unique_xmalloc_ptr<char> this_package_name
9476 (go_symbol_package_name (sym));
9477
9478 if (this_package_name == NULL)
9479 continue;
9480 if (package_name == NULL)
9481 package_name = std::move (this_package_name);
9482 else
9483 {
9484 struct objfile *objfile
9485 = cu->per_cu->dwarf2_per_objfile->objfile;
9486 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9487 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9488 (symbol_symtab (sym) != NULL
9489 ? symtab_to_filename_for_display
9490 (symbol_symtab (sym))
9491 : objfile_name (objfile)),
9492 this_package_name.get (), package_name.get ());
9493 }
9494 }
9495 }
9496 }
9497
9498 if (package_name != NULL)
9499 {
9500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9501 const char *saved_package_name
9502 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9503 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9504 saved_package_name);
9505 struct symbol *sym;
9506
9507 sym = allocate_symbol (objfile);
9508 sym->set_language (language_go, &objfile->objfile_obstack);
9509 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9510 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9511 e.g., "main" finds the "main" module and not C's main(). */
9512 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9513 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9514 SYMBOL_TYPE (sym) = type;
9515
9516 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9517 }
9518 }
9519
9520 /* Allocate a fully-qualified name consisting of the two parts on the
9521 obstack. */
9522
9523 static const char *
9524 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9525 {
9526 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9527 }
9528
9529 /* A helper that allocates a struct discriminant_info to attach to a
9530 union type. */
9531
9532 static struct discriminant_info *
9533 alloc_discriminant_info (struct type *type, int discriminant_index,
9534 int default_index)
9535 {
9536 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9537 gdb_assert (discriminant_index == -1
9538 || (discriminant_index >= 0
9539 && discriminant_index < TYPE_NFIELDS (type)));
9540 gdb_assert (default_index == -1
9541 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9542
9543 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9544
9545 struct discriminant_info *disc
9546 = ((struct discriminant_info *)
9547 TYPE_ZALLOC (type,
9548 offsetof (struct discriminant_info, discriminants)
9549 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9550 disc->default_index = default_index;
9551 disc->discriminant_index = discriminant_index;
9552
9553 struct dynamic_prop prop;
9554 prop.kind = PROP_UNDEFINED;
9555 prop.data.baton = disc;
9556
9557 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9558
9559 return disc;
9560 }
9561
9562 /* Some versions of rustc emitted enums in an unusual way.
9563
9564 Ordinary enums were emitted as unions. The first element of each
9565 structure in the union was named "RUST$ENUM$DISR". This element
9566 held the discriminant.
9567
9568 These versions of Rust also implemented the "non-zero"
9569 optimization. When the enum had two values, and one is empty and
9570 the other holds a pointer that cannot be zero, the pointer is used
9571 as the discriminant, with a zero value meaning the empty variant.
9572 Here, the union's first member is of the form
9573 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9574 where the fieldnos are the indices of the fields that should be
9575 traversed in order to find the field (which may be several fields deep)
9576 and the variantname is the name of the variant of the case when the
9577 field is zero.
9578
9579 This function recognizes whether TYPE is of one of these forms,
9580 and, if so, smashes it to be a variant type. */
9581
9582 static void
9583 quirk_rust_enum (struct type *type, struct objfile *objfile)
9584 {
9585 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9586
9587 /* We don't need to deal with empty enums. */
9588 if (TYPE_NFIELDS (type) == 0)
9589 return;
9590
9591 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9592 if (TYPE_NFIELDS (type) == 1
9593 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9594 {
9595 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9596
9597 /* Decode the field name to find the offset of the
9598 discriminant. */
9599 ULONGEST bit_offset = 0;
9600 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9601 while (name[0] >= '0' && name[0] <= '9')
9602 {
9603 char *tail;
9604 unsigned long index = strtoul (name, &tail, 10);
9605 name = tail;
9606 if (*name != '$'
9607 || index >= TYPE_NFIELDS (field_type)
9608 || (TYPE_FIELD_LOC_KIND (field_type, index)
9609 != FIELD_LOC_KIND_BITPOS))
9610 {
9611 complaint (_("Could not parse Rust enum encoding string \"%s\""
9612 "[in module %s]"),
9613 TYPE_FIELD_NAME (type, 0),
9614 objfile_name (objfile));
9615 return;
9616 }
9617 ++name;
9618
9619 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9620 field_type = TYPE_FIELD_TYPE (field_type, index);
9621 }
9622
9623 /* Make a union to hold the variants. */
9624 struct type *union_type = alloc_type (objfile);
9625 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9626 TYPE_NFIELDS (union_type) = 3;
9627 TYPE_FIELDS (union_type)
9628 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9629 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9630 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9631
9632 /* Put the discriminant must at index 0. */
9633 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9634 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9635 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9636 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9637
9638 /* The order of fields doesn't really matter, so put the real
9639 field at index 1 and the data-less field at index 2. */
9640 struct discriminant_info *disc
9641 = alloc_discriminant_info (union_type, 0, 1);
9642 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9643 TYPE_FIELD_NAME (union_type, 1)
9644 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9645 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9646 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9647 TYPE_FIELD_NAME (union_type, 1));
9648
9649 const char *dataless_name
9650 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9651 name);
9652 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9653 dataless_name);
9654 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9655 /* NAME points into the original discriminant name, which
9656 already has the correct lifetime. */
9657 TYPE_FIELD_NAME (union_type, 2) = name;
9658 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9659 disc->discriminants[2] = 0;
9660
9661 /* Smash this type to be a structure type. We have to do this
9662 because the type has already been recorded. */
9663 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9664 TYPE_NFIELDS (type) = 1;
9665 TYPE_FIELDS (type)
9666 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9667
9668 /* Install the variant part. */
9669 TYPE_FIELD_TYPE (type, 0) = union_type;
9670 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9671 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9672 }
9673 /* A union with a single anonymous field is probably an old-style
9674 univariant enum. */
9675 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9676 {
9677 /* Smash this type to be a structure type. We have to do this
9678 because the type has already been recorded. */
9679 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9680
9681 /* Make a union to hold the variants. */
9682 struct type *union_type = alloc_type (objfile);
9683 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9684 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9685 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9686 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9687 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9688
9689 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9690 const char *variant_name
9691 = rust_last_path_segment (TYPE_NAME (field_type));
9692 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9693 TYPE_NAME (field_type)
9694 = rust_fully_qualify (&objfile->objfile_obstack,
9695 TYPE_NAME (type), variant_name);
9696
9697 /* Install the union in the outer struct type. */
9698 TYPE_NFIELDS (type) = 1;
9699 TYPE_FIELDS (type)
9700 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9701 TYPE_FIELD_TYPE (type, 0) = union_type;
9702 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9703 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9704
9705 alloc_discriminant_info (union_type, -1, 0);
9706 }
9707 else
9708 {
9709 struct type *disr_type = nullptr;
9710 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9711 {
9712 disr_type = TYPE_FIELD_TYPE (type, i);
9713
9714 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9715 {
9716 /* All fields of a true enum will be structs. */
9717 return;
9718 }
9719 else if (TYPE_NFIELDS (disr_type) == 0)
9720 {
9721 /* Could be data-less variant, so keep going. */
9722 disr_type = nullptr;
9723 }
9724 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9725 "RUST$ENUM$DISR") != 0)
9726 {
9727 /* Not a Rust enum. */
9728 return;
9729 }
9730 else
9731 {
9732 /* Found one. */
9733 break;
9734 }
9735 }
9736
9737 /* If we got here without a discriminant, then it's probably
9738 just a union. */
9739 if (disr_type == nullptr)
9740 return;
9741
9742 /* Smash this type to be a structure type. We have to do this
9743 because the type has already been recorded. */
9744 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9745
9746 /* Make a union to hold the variants. */
9747 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9748 struct type *union_type = alloc_type (objfile);
9749 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9750 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9751 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9752 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9753 TYPE_FIELDS (union_type)
9754 = (struct field *) TYPE_ZALLOC (union_type,
9755 (TYPE_NFIELDS (union_type)
9756 * sizeof (struct field)));
9757
9758 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9759 TYPE_NFIELDS (type) * sizeof (struct field));
9760
9761 /* Install the discriminant at index 0 in the union. */
9762 TYPE_FIELD (union_type, 0) = *disr_field;
9763 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9764 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9765
9766 /* Install the union in the outer struct type. */
9767 TYPE_FIELD_TYPE (type, 0) = union_type;
9768 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9769 TYPE_NFIELDS (type) = 1;
9770
9771 /* Set the size and offset of the union type. */
9772 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9773
9774 /* We need a way to find the correct discriminant given a
9775 variant name. For convenience we build a map here. */
9776 struct type *enum_type = FIELD_TYPE (*disr_field);
9777 std::unordered_map<std::string, ULONGEST> discriminant_map;
9778 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9779 {
9780 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9781 {
9782 const char *name
9783 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9784 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9785 }
9786 }
9787
9788 int n_fields = TYPE_NFIELDS (union_type);
9789 struct discriminant_info *disc
9790 = alloc_discriminant_info (union_type, 0, -1);
9791 /* Skip the discriminant here. */
9792 for (int i = 1; i < n_fields; ++i)
9793 {
9794 /* Find the final word in the name of this variant's type.
9795 That name can be used to look up the correct
9796 discriminant. */
9797 const char *variant_name
9798 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9799 i)));
9800
9801 auto iter = discriminant_map.find (variant_name);
9802 if (iter != discriminant_map.end ())
9803 disc->discriminants[i] = iter->second;
9804
9805 /* Remove the discriminant field, if it exists. */
9806 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9807 if (TYPE_NFIELDS (sub_type) > 0)
9808 {
9809 --TYPE_NFIELDS (sub_type);
9810 ++TYPE_FIELDS (sub_type);
9811 }
9812 TYPE_FIELD_NAME (union_type, i) = variant_name;
9813 TYPE_NAME (sub_type)
9814 = rust_fully_qualify (&objfile->objfile_obstack,
9815 TYPE_NAME (type), variant_name);
9816 }
9817 }
9818 }
9819
9820 /* Rewrite some Rust unions to be structures with variants parts. */
9821
9822 static void
9823 rust_union_quirks (struct dwarf2_cu *cu)
9824 {
9825 gdb_assert (cu->language == language_rust);
9826 for (type *type_ : cu->rust_unions)
9827 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9828 /* We don't need this any more. */
9829 cu->rust_unions.clear ();
9830 }
9831
9832 /* Return the symtab for PER_CU. This works properly regardless of
9833 whether we're using the index or psymtabs. */
9834
9835 static struct compunit_symtab *
9836 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9837 {
9838 return (per_cu->dwarf2_per_objfile->using_index
9839 ? per_cu->v.quick->compunit_symtab
9840 : per_cu->v.psymtab->compunit_symtab);
9841 }
9842
9843 /* A helper function for computing the list of all symbol tables
9844 included by PER_CU. */
9845
9846 static void
9847 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9848 htab_t all_children, htab_t all_type_symtabs,
9849 struct dwarf2_per_cu_data *per_cu,
9850 struct compunit_symtab *immediate_parent)
9851 {
9852 void **slot;
9853 struct compunit_symtab *cust;
9854
9855 slot = htab_find_slot (all_children, per_cu, INSERT);
9856 if (*slot != NULL)
9857 {
9858 /* This inclusion and its children have been processed. */
9859 return;
9860 }
9861
9862 *slot = per_cu;
9863 /* Only add a CU if it has a symbol table. */
9864 cust = get_compunit_symtab (per_cu);
9865 if (cust != NULL)
9866 {
9867 /* If this is a type unit only add its symbol table if we haven't
9868 seen it yet (type unit per_cu's can share symtabs). */
9869 if (per_cu->is_debug_types)
9870 {
9871 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9872 if (*slot == NULL)
9873 {
9874 *slot = cust;
9875 result->push_back (cust);
9876 if (cust->user == NULL)
9877 cust->user = immediate_parent;
9878 }
9879 }
9880 else
9881 {
9882 result->push_back (cust);
9883 if (cust->user == NULL)
9884 cust->user = immediate_parent;
9885 }
9886 }
9887
9888 if (!per_cu->imported_symtabs_empty ())
9889 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9890 {
9891 recursively_compute_inclusions (result, all_children,
9892 all_type_symtabs, ptr, cust);
9893 }
9894 }
9895
9896 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9897 PER_CU. */
9898
9899 static void
9900 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9901 {
9902 gdb_assert (! per_cu->is_debug_types);
9903
9904 if (!per_cu->imported_symtabs_empty ())
9905 {
9906 int len;
9907 std::vector<compunit_symtab *> result_symtabs;
9908 htab_t all_children, all_type_symtabs;
9909 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9910
9911 /* If we don't have a symtab, we can just skip this case. */
9912 if (cust == NULL)
9913 return;
9914
9915 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9916 NULL, xcalloc, xfree);
9917 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9918 NULL, xcalloc, xfree);
9919
9920 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9921 {
9922 recursively_compute_inclusions (&result_symtabs, all_children,
9923 all_type_symtabs, ptr, cust);
9924 }
9925
9926 /* Now we have a transitive closure of all the included symtabs. */
9927 len = result_symtabs.size ();
9928 cust->includes
9929 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9930 struct compunit_symtab *, len + 1);
9931 memcpy (cust->includes, result_symtabs.data (),
9932 len * sizeof (compunit_symtab *));
9933 cust->includes[len] = NULL;
9934
9935 htab_delete (all_children);
9936 htab_delete (all_type_symtabs);
9937 }
9938 }
9939
9940 /* Compute the 'includes' field for the symtabs of all the CUs we just
9941 read. */
9942
9943 static void
9944 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9945 {
9946 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9947 {
9948 if (! iter->is_debug_types)
9949 compute_compunit_symtab_includes (iter);
9950 }
9951
9952 dwarf2_per_objfile->just_read_cus.clear ();
9953 }
9954
9955 /* Generate full symbol information for PER_CU, whose DIEs have
9956 already been loaded into memory. */
9957
9958 static void
9959 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9960 enum language pretend_language)
9961 {
9962 struct dwarf2_cu *cu = per_cu->cu;
9963 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9964 struct objfile *objfile = dwarf2_per_objfile->objfile;
9965 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9966 CORE_ADDR lowpc, highpc;
9967 struct compunit_symtab *cust;
9968 CORE_ADDR baseaddr;
9969 struct block *static_block;
9970 CORE_ADDR addr;
9971
9972 baseaddr = objfile->text_section_offset ();
9973
9974 /* Clear the list here in case something was left over. */
9975 cu->method_list.clear ();
9976
9977 cu->language = pretend_language;
9978 cu->language_defn = language_def (cu->language);
9979
9980 /* Do line number decoding in read_file_scope () */
9981 process_die (cu->dies, cu);
9982
9983 /* For now fudge the Go package. */
9984 if (cu->language == language_go)
9985 fixup_go_packaging (cu);
9986
9987 /* Now that we have processed all the DIEs in the CU, all the types
9988 should be complete, and it should now be safe to compute all of the
9989 physnames. */
9990 compute_delayed_physnames (cu);
9991
9992 if (cu->language == language_rust)
9993 rust_union_quirks (cu);
9994
9995 /* Some compilers don't define a DW_AT_high_pc attribute for the
9996 compilation unit. If the DW_AT_high_pc is missing, synthesize
9997 it, by scanning the DIE's below the compilation unit. */
9998 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9999
10000 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10001 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10002
10003 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10004 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10005 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10006 addrmap to help ensure it has an accurate map of pc values belonging to
10007 this comp unit. */
10008 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10009
10010 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10011 SECT_OFF_TEXT (objfile),
10012 0);
10013
10014 if (cust != NULL)
10015 {
10016 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10017
10018 /* Set symtab language to language from DW_AT_language. If the
10019 compilation is from a C file generated by language preprocessors, do
10020 not set the language if it was already deduced by start_subfile. */
10021 if (!(cu->language == language_c
10022 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10023 COMPUNIT_FILETABS (cust)->language = cu->language;
10024
10025 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10026 produce DW_AT_location with location lists but it can be possibly
10027 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10028 there were bugs in prologue debug info, fixed later in GCC-4.5
10029 by "unwind info for epilogues" patch (which is not directly related).
10030
10031 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10032 needed, it would be wrong due to missing DW_AT_producer there.
10033
10034 Still one can confuse GDB by using non-standard GCC compilation
10035 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10036 */
10037 if (cu->has_loclist && gcc_4_minor >= 5)
10038 cust->locations_valid = 1;
10039
10040 if (gcc_4_minor >= 5)
10041 cust->epilogue_unwind_valid = 1;
10042
10043 cust->call_site_htab = cu->call_site_htab;
10044 }
10045
10046 if (dwarf2_per_objfile->using_index)
10047 per_cu->v.quick->compunit_symtab = cust;
10048 else
10049 {
10050 dwarf2_psymtab *pst = per_cu->v.psymtab;
10051 pst->compunit_symtab = cust;
10052 pst->readin = true;
10053 }
10054
10055 /* Push it for inclusion processing later. */
10056 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10057
10058 /* Not needed any more. */
10059 cu->reset_builder ();
10060 }
10061
10062 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10063 already been loaded into memory. */
10064
10065 static void
10066 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10067 enum language pretend_language)
10068 {
10069 struct dwarf2_cu *cu = per_cu->cu;
10070 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10071 struct objfile *objfile = dwarf2_per_objfile->objfile;
10072 struct compunit_symtab *cust;
10073 struct signatured_type *sig_type;
10074
10075 gdb_assert (per_cu->is_debug_types);
10076 sig_type = (struct signatured_type *) per_cu;
10077
10078 /* Clear the list here in case something was left over. */
10079 cu->method_list.clear ();
10080
10081 cu->language = pretend_language;
10082 cu->language_defn = language_def (cu->language);
10083
10084 /* The symbol tables are set up in read_type_unit_scope. */
10085 process_die (cu->dies, cu);
10086
10087 /* For now fudge the Go package. */
10088 if (cu->language == language_go)
10089 fixup_go_packaging (cu);
10090
10091 /* Now that we have processed all the DIEs in the CU, all the types
10092 should be complete, and it should now be safe to compute all of the
10093 physnames. */
10094 compute_delayed_physnames (cu);
10095
10096 if (cu->language == language_rust)
10097 rust_union_quirks (cu);
10098
10099 /* TUs share symbol tables.
10100 If this is the first TU to use this symtab, complete the construction
10101 of it with end_expandable_symtab. Otherwise, complete the addition of
10102 this TU's symbols to the existing symtab. */
10103 if (sig_type->type_unit_group->compunit_symtab == NULL)
10104 {
10105 buildsym_compunit *builder = cu->get_builder ();
10106 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10107 sig_type->type_unit_group->compunit_symtab = cust;
10108
10109 if (cust != NULL)
10110 {
10111 /* Set symtab language to language from DW_AT_language. If the
10112 compilation is from a C file generated by language preprocessors,
10113 do not set the language if it was already deduced by
10114 start_subfile. */
10115 if (!(cu->language == language_c
10116 && COMPUNIT_FILETABS (cust)->language != language_c))
10117 COMPUNIT_FILETABS (cust)->language = cu->language;
10118 }
10119 }
10120 else
10121 {
10122 cu->get_builder ()->augment_type_symtab ();
10123 cust = sig_type->type_unit_group->compunit_symtab;
10124 }
10125
10126 if (dwarf2_per_objfile->using_index)
10127 per_cu->v.quick->compunit_symtab = cust;
10128 else
10129 {
10130 dwarf2_psymtab *pst = per_cu->v.psymtab;
10131 pst->compunit_symtab = cust;
10132 pst->readin = true;
10133 }
10134
10135 /* Not needed any more. */
10136 cu->reset_builder ();
10137 }
10138
10139 /* Process an imported unit DIE. */
10140
10141 static void
10142 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10143 {
10144 struct attribute *attr;
10145
10146 /* For now we don't handle imported units in type units. */
10147 if (cu->per_cu->is_debug_types)
10148 {
10149 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10150 " supported in type units [in module %s]"),
10151 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10152 }
10153
10154 attr = dwarf2_attr (die, DW_AT_import, cu);
10155 if (attr != NULL)
10156 {
10157 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10158 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10159 dwarf2_per_cu_data *per_cu
10160 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10161 cu->per_cu->dwarf2_per_objfile);
10162
10163 /* If necessary, add it to the queue and load its DIEs. */
10164 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10165 load_full_comp_unit (per_cu, false, cu->language);
10166
10167 cu->per_cu->imported_symtabs_push (per_cu);
10168 }
10169 }
10170
10171 /* RAII object that represents a process_die scope: i.e.,
10172 starts/finishes processing a DIE. */
10173 class process_die_scope
10174 {
10175 public:
10176 process_die_scope (die_info *die, dwarf2_cu *cu)
10177 : m_die (die), m_cu (cu)
10178 {
10179 /* We should only be processing DIEs not already in process. */
10180 gdb_assert (!m_die->in_process);
10181 m_die->in_process = true;
10182 }
10183
10184 ~process_die_scope ()
10185 {
10186 m_die->in_process = false;
10187
10188 /* If we're done processing the DIE for the CU that owns the line
10189 header, we don't need the line header anymore. */
10190 if (m_cu->line_header_die_owner == m_die)
10191 {
10192 delete m_cu->line_header;
10193 m_cu->line_header = NULL;
10194 m_cu->line_header_die_owner = NULL;
10195 }
10196 }
10197
10198 private:
10199 die_info *m_die;
10200 dwarf2_cu *m_cu;
10201 };
10202
10203 /* Process a die and its children. */
10204
10205 static void
10206 process_die (struct die_info *die, struct dwarf2_cu *cu)
10207 {
10208 process_die_scope scope (die, cu);
10209
10210 switch (die->tag)
10211 {
10212 case DW_TAG_padding:
10213 break;
10214 case DW_TAG_compile_unit:
10215 case DW_TAG_partial_unit:
10216 read_file_scope (die, cu);
10217 break;
10218 case DW_TAG_type_unit:
10219 read_type_unit_scope (die, cu);
10220 break;
10221 case DW_TAG_subprogram:
10222 /* Nested subprograms in Fortran get a prefix. */
10223 if (cu->language == language_fortran
10224 && die->parent != NULL
10225 && die->parent->tag == DW_TAG_subprogram)
10226 cu->processing_has_namespace_info = true;
10227 /* Fall through. */
10228 case DW_TAG_inlined_subroutine:
10229 read_func_scope (die, cu);
10230 break;
10231 case DW_TAG_lexical_block:
10232 case DW_TAG_try_block:
10233 case DW_TAG_catch_block:
10234 read_lexical_block_scope (die, cu);
10235 break;
10236 case DW_TAG_call_site:
10237 case DW_TAG_GNU_call_site:
10238 read_call_site_scope (die, cu);
10239 break;
10240 case DW_TAG_class_type:
10241 case DW_TAG_interface_type:
10242 case DW_TAG_structure_type:
10243 case DW_TAG_union_type:
10244 process_structure_scope (die, cu);
10245 break;
10246 case DW_TAG_enumeration_type:
10247 process_enumeration_scope (die, cu);
10248 break;
10249
10250 /* These dies have a type, but processing them does not create
10251 a symbol or recurse to process the children. Therefore we can
10252 read them on-demand through read_type_die. */
10253 case DW_TAG_subroutine_type:
10254 case DW_TAG_set_type:
10255 case DW_TAG_array_type:
10256 case DW_TAG_pointer_type:
10257 case DW_TAG_ptr_to_member_type:
10258 case DW_TAG_reference_type:
10259 case DW_TAG_rvalue_reference_type:
10260 case DW_TAG_string_type:
10261 break;
10262
10263 case DW_TAG_base_type:
10264 case DW_TAG_subrange_type:
10265 case DW_TAG_typedef:
10266 /* Add a typedef symbol for the type definition, if it has a
10267 DW_AT_name. */
10268 new_symbol (die, read_type_die (die, cu), cu);
10269 break;
10270 case DW_TAG_common_block:
10271 read_common_block (die, cu);
10272 break;
10273 case DW_TAG_common_inclusion:
10274 break;
10275 case DW_TAG_namespace:
10276 cu->processing_has_namespace_info = true;
10277 read_namespace (die, cu);
10278 break;
10279 case DW_TAG_module:
10280 cu->processing_has_namespace_info = true;
10281 read_module (die, cu);
10282 break;
10283 case DW_TAG_imported_declaration:
10284 cu->processing_has_namespace_info = true;
10285 if (read_namespace_alias (die, cu))
10286 break;
10287 /* The declaration is not a global namespace alias. */
10288 /* Fall through. */
10289 case DW_TAG_imported_module:
10290 cu->processing_has_namespace_info = true;
10291 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10292 || cu->language != language_fortran))
10293 complaint (_("Tag '%s' has unexpected children"),
10294 dwarf_tag_name (die->tag));
10295 read_import_statement (die, cu);
10296 break;
10297
10298 case DW_TAG_imported_unit:
10299 process_imported_unit_die (die, cu);
10300 break;
10301
10302 case DW_TAG_variable:
10303 read_variable (die, cu);
10304 break;
10305
10306 default:
10307 new_symbol (die, NULL, cu);
10308 break;
10309 }
10310 }
10311 \f
10312 /* DWARF name computation. */
10313
10314 /* A helper function for dwarf2_compute_name which determines whether DIE
10315 needs to have the name of the scope prepended to the name listed in the
10316 die. */
10317
10318 static int
10319 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10320 {
10321 struct attribute *attr;
10322
10323 switch (die->tag)
10324 {
10325 case DW_TAG_namespace:
10326 case DW_TAG_typedef:
10327 case DW_TAG_class_type:
10328 case DW_TAG_interface_type:
10329 case DW_TAG_structure_type:
10330 case DW_TAG_union_type:
10331 case DW_TAG_enumeration_type:
10332 case DW_TAG_enumerator:
10333 case DW_TAG_subprogram:
10334 case DW_TAG_inlined_subroutine:
10335 case DW_TAG_member:
10336 case DW_TAG_imported_declaration:
10337 return 1;
10338
10339 case DW_TAG_variable:
10340 case DW_TAG_constant:
10341 /* We only need to prefix "globally" visible variables. These include
10342 any variable marked with DW_AT_external or any variable that
10343 lives in a namespace. [Variables in anonymous namespaces
10344 require prefixing, but they are not DW_AT_external.] */
10345
10346 if (dwarf2_attr (die, DW_AT_specification, cu))
10347 {
10348 struct dwarf2_cu *spec_cu = cu;
10349
10350 return die_needs_namespace (die_specification (die, &spec_cu),
10351 spec_cu);
10352 }
10353
10354 attr = dwarf2_attr (die, DW_AT_external, cu);
10355 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10356 && die->parent->tag != DW_TAG_module)
10357 return 0;
10358 /* A variable in a lexical block of some kind does not need a
10359 namespace, even though in C++ such variables may be external
10360 and have a mangled name. */
10361 if (die->parent->tag == DW_TAG_lexical_block
10362 || die->parent->tag == DW_TAG_try_block
10363 || die->parent->tag == DW_TAG_catch_block
10364 || die->parent->tag == DW_TAG_subprogram)
10365 return 0;
10366 return 1;
10367
10368 default:
10369 return 0;
10370 }
10371 }
10372
10373 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10374 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10375 defined for the given DIE. */
10376
10377 static struct attribute *
10378 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10379 {
10380 struct attribute *attr;
10381
10382 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10383 if (attr == NULL)
10384 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10385
10386 return attr;
10387 }
10388
10389 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10390 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10391 defined for the given DIE. */
10392
10393 static const char *
10394 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10395 {
10396 const char *linkage_name;
10397
10398 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10399 if (linkage_name == NULL)
10400 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10401
10402 return linkage_name;
10403 }
10404
10405 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10406 compute the physname for the object, which include a method's:
10407 - formal parameters (C++),
10408 - receiver type (Go),
10409
10410 The term "physname" is a bit confusing.
10411 For C++, for example, it is the demangled name.
10412 For Go, for example, it's the mangled name.
10413
10414 For Ada, return the DIE's linkage name rather than the fully qualified
10415 name. PHYSNAME is ignored..
10416
10417 The result is allocated on the objfile_obstack and canonicalized. */
10418
10419 static const char *
10420 dwarf2_compute_name (const char *name,
10421 struct die_info *die, struct dwarf2_cu *cu,
10422 int physname)
10423 {
10424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10425
10426 if (name == NULL)
10427 name = dwarf2_name (die, cu);
10428
10429 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10430 but otherwise compute it by typename_concat inside GDB.
10431 FIXME: Actually this is not really true, or at least not always true.
10432 It's all very confusing. compute_and_set_names doesn't try to demangle
10433 Fortran names because there is no mangling standard. So new_symbol
10434 will set the demangled name to the result of dwarf2_full_name, and it is
10435 the demangled name that GDB uses if it exists. */
10436 if (cu->language == language_ada
10437 || (cu->language == language_fortran && physname))
10438 {
10439 /* For Ada unit, we prefer the linkage name over the name, as
10440 the former contains the exported name, which the user expects
10441 to be able to reference. Ideally, we want the user to be able
10442 to reference this entity using either natural or linkage name,
10443 but we haven't started looking at this enhancement yet. */
10444 const char *linkage_name = dw2_linkage_name (die, cu);
10445
10446 if (linkage_name != NULL)
10447 return linkage_name;
10448 }
10449
10450 /* These are the only languages we know how to qualify names in. */
10451 if (name != NULL
10452 && (cu->language == language_cplus
10453 || cu->language == language_fortran || cu->language == language_d
10454 || cu->language == language_rust))
10455 {
10456 if (die_needs_namespace (die, cu))
10457 {
10458 const char *prefix;
10459 const char *canonical_name = NULL;
10460
10461 string_file buf;
10462
10463 prefix = determine_prefix (die, cu);
10464 if (*prefix != '\0')
10465 {
10466 gdb::unique_xmalloc_ptr<char> prefixed_name
10467 (typename_concat (NULL, prefix, name, physname, cu));
10468
10469 buf.puts (prefixed_name.get ());
10470 }
10471 else
10472 buf.puts (name);
10473
10474 /* Template parameters may be specified in the DIE's DW_AT_name, or
10475 as children with DW_TAG_template_type_param or
10476 DW_TAG_value_type_param. If the latter, add them to the name
10477 here. If the name already has template parameters, then
10478 skip this step; some versions of GCC emit both, and
10479 it is more efficient to use the pre-computed name.
10480
10481 Something to keep in mind about this process: it is very
10482 unlikely, or in some cases downright impossible, to produce
10483 something that will match the mangled name of a function.
10484 If the definition of the function has the same debug info,
10485 we should be able to match up with it anyway. But fallbacks
10486 using the minimal symbol, for instance to find a method
10487 implemented in a stripped copy of libstdc++, will not work.
10488 If we do not have debug info for the definition, we will have to
10489 match them up some other way.
10490
10491 When we do name matching there is a related problem with function
10492 templates; two instantiated function templates are allowed to
10493 differ only by their return types, which we do not add here. */
10494
10495 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10496 {
10497 struct attribute *attr;
10498 struct die_info *child;
10499 int first = 1;
10500
10501 die->building_fullname = 1;
10502
10503 for (child = die->child; child != NULL; child = child->sibling)
10504 {
10505 struct type *type;
10506 LONGEST value;
10507 const gdb_byte *bytes;
10508 struct dwarf2_locexpr_baton *baton;
10509 struct value *v;
10510
10511 if (child->tag != DW_TAG_template_type_param
10512 && child->tag != DW_TAG_template_value_param)
10513 continue;
10514
10515 if (first)
10516 {
10517 buf.puts ("<");
10518 first = 0;
10519 }
10520 else
10521 buf.puts (", ");
10522
10523 attr = dwarf2_attr (child, DW_AT_type, cu);
10524 if (attr == NULL)
10525 {
10526 complaint (_("template parameter missing DW_AT_type"));
10527 buf.puts ("UNKNOWN_TYPE");
10528 continue;
10529 }
10530 type = die_type (child, cu);
10531
10532 if (child->tag == DW_TAG_template_type_param)
10533 {
10534 c_print_type (type, "", &buf, -1, 0, cu->language,
10535 &type_print_raw_options);
10536 continue;
10537 }
10538
10539 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10540 if (attr == NULL)
10541 {
10542 complaint (_("template parameter missing "
10543 "DW_AT_const_value"));
10544 buf.puts ("UNKNOWN_VALUE");
10545 continue;
10546 }
10547
10548 dwarf2_const_value_attr (attr, type, name,
10549 &cu->comp_unit_obstack, cu,
10550 &value, &bytes, &baton);
10551
10552 if (TYPE_NOSIGN (type))
10553 /* GDB prints characters as NUMBER 'CHAR'. If that's
10554 changed, this can use value_print instead. */
10555 c_printchar (value, type, &buf);
10556 else
10557 {
10558 struct value_print_options opts;
10559
10560 if (baton != NULL)
10561 v = dwarf2_evaluate_loc_desc (type, NULL,
10562 baton->data,
10563 baton->size,
10564 baton->per_cu);
10565 else if (bytes != NULL)
10566 {
10567 v = allocate_value (type);
10568 memcpy (value_contents_writeable (v), bytes,
10569 TYPE_LENGTH (type));
10570 }
10571 else
10572 v = value_from_longest (type, value);
10573
10574 /* Specify decimal so that we do not depend on
10575 the radix. */
10576 get_formatted_print_options (&opts, 'd');
10577 opts.raw = 1;
10578 value_print (v, &buf, &opts);
10579 release_value (v);
10580 }
10581 }
10582
10583 die->building_fullname = 0;
10584
10585 if (!first)
10586 {
10587 /* Close the argument list, with a space if necessary
10588 (nested templates). */
10589 if (!buf.empty () && buf.string ().back () == '>')
10590 buf.puts (" >");
10591 else
10592 buf.puts (">");
10593 }
10594 }
10595
10596 /* For C++ methods, append formal parameter type
10597 information, if PHYSNAME. */
10598
10599 if (physname && die->tag == DW_TAG_subprogram
10600 && cu->language == language_cplus)
10601 {
10602 struct type *type = read_type_die (die, cu);
10603
10604 c_type_print_args (type, &buf, 1, cu->language,
10605 &type_print_raw_options);
10606
10607 if (cu->language == language_cplus)
10608 {
10609 /* Assume that an artificial first parameter is
10610 "this", but do not crash if it is not. RealView
10611 marks unnamed (and thus unused) parameters as
10612 artificial; there is no way to differentiate
10613 the two cases. */
10614 if (TYPE_NFIELDS (type) > 0
10615 && TYPE_FIELD_ARTIFICIAL (type, 0)
10616 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10617 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10618 0))))
10619 buf.puts (" const");
10620 }
10621 }
10622
10623 const std::string &intermediate_name = buf.string ();
10624
10625 if (cu->language == language_cplus)
10626 canonical_name
10627 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10628 &objfile->per_bfd->storage_obstack);
10629
10630 /* If we only computed INTERMEDIATE_NAME, or if
10631 INTERMEDIATE_NAME is already canonical, then we need to
10632 copy it to the appropriate obstack. */
10633 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10634 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10635 intermediate_name);
10636 else
10637 name = canonical_name;
10638 }
10639 }
10640
10641 return name;
10642 }
10643
10644 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10645 If scope qualifiers are appropriate they will be added. The result
10646 will be allocated on the storage_obstack, or NULL if the DIE does
10647 not have a name. NAME may either be from a previous call to
10648 dwarf2_name or NULL.
10649
10650 The output string will be canonicalized (if C++). */
10651
10652 static const char *
10653 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10654 {
10655 return dwarf2_compute_name (name, die, cu, 0);
10656 }
10657
10658 /* Construct a physname for the given DIE in CU. NAME may either be
10659 from a previous call to dwarf2_name or NULL. The result will be
10660 allocated on the objfile_objstack or NULL if the DIE does not have a
10661 name.
10662
10663 The output string will be canonicalized (if C++). */
10664
10665 static const char *
10666 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10667 {
10668 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10669 const char *retval, *mangled = NULL, *canon = NULL;
10670 int need_copy = 1;
10671
10672 /* In this case dwarf2_compute_name is just a shortcut not building anything
10673 on its own. */
10674 if (!die_needs_namespace (die, cu))
10675 return dwarf2_compute_name (name, die, cu, 1);
10676
10677 mangled = dw2_linkage_name (die, cu);
10678
10679 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10680 See https://github.com/rust-lang/rust/issues/32925. */
10681 if (cu->language == language_rust && mangled != NULL
10682 && strchr (mangled, '{') != NULL)
10683 mangled = NULL;
10684
10685 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10686 has computed. */
10687 gdb::unique_xmalloc_ptr<char> demangled;
10688 if (mangled != NULL)
10689 {
10690
10691 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10692 {
10693 /* Do nothing (do not demangle the symbol name). */
10694 }
10695 else if (cu->language == language_go)
10696 {
10697 /* This is a lie, but we already lie to the caller new_symbol.
10698 new_symbol assumes we return the mangled name.
10699 This just undoes that lie until things are cleaned up. */
10700 }
10701 else
10702 {
10703 /* Use DMGL_RET_DROP for C++ template functions to suppress
10704 their return type. It is easier for GDB users to search
10705 for such functions as `name(params)' than `long name(params)'.
10706 In such case the minimal symbol names do not match the full
10707 symbol names but for template functions there is never a need
10708 to look up their definition from their declaration so
10709 the only disadvantage remains the minimal symbol variant
10710 `long name(params)' does not have the proper inferior type. */
10711 demangled.reset (gdb_demangle (mangled,
10712 (DMGL_PARAMS | DMGL_ANSI
10713 | DMGL_RET_DROP)));
10714 }
10715 if (demangled)
10716 canon = demangled.get ();
10717 else
10718 {
10719 canon = mangled;
10720 need_copy = 0;
10721 }
10722 }
10723
10724 if (canon == NULL || check_physname)
10725 {
10726 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10727
10728 if (canon != NULL && strcmp (physname, canon) != 0)
10729 {
10730 /* It may not mean a bug in GDB. The compiler could also
10731 compute DW_AT_linkage_name incorrectly. But in such case
10732 GDB would need to be bug-to-bug compatible. */
10733
10734 complaint (_("Computed physname <%s> does not match demangled <%s> "
10735 "(from linkage <%s>) - DIE at %s [in module %s]"),
10736 physname, canon, mangled, sect_offset_str (die->sect_off),
10737 objfile_name (objfile));
10738
10739 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10740 is available here - over computed PHYSNAME. It is safer
10741 against both buggy GDB and buggy compilers. */
10742
10743 retval = canon;
10744 }
10745 else
10746 {
10747 retval = physname;
10748 need_copy = 0;
10749 }
10750 }
10751 else
10752 retval = canon;
10753
10754 if (need_copy)
10755 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10756
10757 return retval;
10758 }
10759
10760 /* Inspect DIE in CU for a namespace alias. If one exists, record
10761 a new symbol for it.
10762
10763 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10764
10765 static int
10766 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10767 {
10768 struct attribute *attr;
10769
10770 /* If the die does not have a name, this is not a namespace
10771 alias. */
10772 attr = dwarf2_attr (die, DW_AT_name, cu);
10773 if (attr != NULL)
10774 {
10775 int num;
10776 struct die_info *d = die;
10777 struct dwarf2_cu *imported_cu = cu;
10778
10779 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10780 keep inspecting DIEs until we hit the underlying import. */
10781 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10782 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10783 {
10784 attr = dwarf2_attr (d, DW_AT_import, cu);
10785 if (attr == NULL)
10786 break;
10787
10788 d = follow_die_ref (d, attr, &imported_cu);
10789 if (d->tag != DW_TAG_imported_declaration)
10790 break;
10791 }
10792
10793 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10794 {
10795 complaint (_("DIE at %s has too many recursively imported "
10796 "declarations"), sect_offset_str (d->sect_off));
10797 return 0;
10798 }
10799
10800 if (attr != NULL)
10801 {
10802 struct type *type;
10803 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10804
10805 type = get_die_type_at_offset (sect_off, cu->per_cu);
10806 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10807 {
10808 /* This declaration is a global namespace alias. Add
10809 a symbol for it whose type is the aliased namespace. */
10810 new_symbol (die, type, cu);
10811 return 1;
10812 }
10813 }
10814 }
10815
10816 return 0;
10817 }
10818
10819 /* Return the using directives repository (global or local?) to use in the
10820 current context for CU.
10821
10822 For Ada, imported declarations can materialize renamings, which *may* be
10823 global. However it is impossible (for now?) in DWARF to distinguish
10824 "external" imported declarations and "static" ones. As all imported
10825 declarations seem to be static in all other languages, make them all CU-wide
10826 global only in Ada. */
10827
10828 static struct using_direct **
10829 using_directives (struct dwarf2_cu *cu)
10830 {
10831 if (cu->language == language_ada
10832 && cu->get_builder ()->outermost_context_p ())
10833 return cu->get_builder ()->get_global_using_directives ();
10834 else
10835 return cu->get_builder ()->get_local_using_directives ();
10836 }
10837
10838 /* Read the import statement specified by the given die and record it. */
10839
10840 static void
10841 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10842 {
10843 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10844 struct attribute *import_attr;
10845 struct die_info *imported_die, *child_die;
10846 struct dwarf2_cu *imported_cu;
10847 const char *imported_name;
10848 const char *imported_name_prefix;
10849 const char *canonical_name;
10850 const char *import_alias;
10851 const char *imported_declaration = NULL;
10852 const char *import_prefix;
10853 std::vector<const char *> excludes;
10854
10855 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10856 if (import_attr == NULL)
10857 {
10858 complaint (_("Tag '%s' has no DW_AT_import"),
10859 dwarf_tag_name (die->tag));
10860 return;
10861 }
10862
10863 imported_cu = cu;
10864 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10865 imported_name = dwarf2_name (imported_die, imported_cu);
10866 if (imported_name == NULL)
10867 {
10868 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10869
10870 The import in the following code:
10871 namespace A
10872 {
10873 typedef int B;
10874 }
10875
10876 int main ()
10877 {
10878 using A::B;
10879 B b;
10880 return b;
10881 }
10882
10883 ...
10884 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10885 <52> DW_AT_decl_file : 1
10886 <53> DW_AT_decl_line : 6
10887 <54> DW_AT_import : <0x75>
10888 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10889 <59> DW_AT_name : B
10890 <5b> DW_AT_decl_file : 1
10891 <5c> DW_AT_decl_line : 2
10892 <5d> DW_AT_type : <0x6e>
10893 ...
10894 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10895 <76> DW_AT_byte_size : 4
10896 <77> DW_AT_encoding : 5 (signed)
10897
10898 imports the wrong die ( 0x75 instead of 0x58 ).
10899 This case will be ignored until the gcc bug is fixed. */
10900 return;
10901 }
10902
10903 /* Figure out the local name after import. */
10904 import_alias = dwarf2_name (die, cu);
10905
10906 /* Figure out where the statement is being imported to. */
10907 import_prefix = determine_prefix (die, cu);
10908
10909 /* Figure out what the scope of the imported die is and prepend it
10910 to the name of the imported die. */
10911 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10912
10913 if (imported_die->tag != DW_TAG_namespace
10914 && imported_die->tag != DW_TAG_module)
10915 {
10916 imported_declaration = imported_name;
10917 canonical_name = imported_name_prefix;
10918 }
10919 else if (strlen (imported_name_prefix) > 0)
10920 canonical_name = obconcat (&objfile->objfile_obstack,
10921 imported_name_prefix,
10922 (cu->language == language_d ? "." : "::"),
10923 imported_name, (char *) NULL);
10924 else
10925 canonical_name = imported_name;
10926
10927 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10928 for (child_die = die->child; child_die && child_die->tag;
10929 child_die = sibling_die (child_die))
10930 {
10931 /* DWARF-4: A Fortran use statement with a “rename list” may be
10932 represented by an imported module entry with an import attribute
10933 referring to the module and owned entries corresponding to those
10934 entities that are renamed as part of being imported. */
10935
10936 if (child_die->tag != DW_TAG_imported_declaration)
10937 {
10938 complaint (_("child DW_TAG_imported_declaration expected "
10939 "- DIE at %s [in module %s]"),
10940 sect_offset_str (child_die->sect_off),
10941 objfile_name (objfile));
10942 continue;
10943 }
10944
10945 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10946 if (import_attr == NULL)
10947 {
10948 complaint (_("Tag '%s' has no DW_AT_import"),
10949 dwarf_tag_name (child_die->tag));
10950 continue;
10951 }
10952
10953 imported_cu = cu;
10954 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10955 &imported_cu);
10956 imported_name = dwarf2_name (imported_die, imported_cu);
10957 if (imported_name == NULL)
10958 {
10959 complaint (_("child DW_TAG_imported_declaration has unknown "
10960 "imported name - DIE at %s [in module %s]"),
10961 sect_offset_str (child_die->sect_off),
10962 objfile_name (objfile));
10963 continue;
10964 }
10965
10966 excludes.push_back (imported_name);
10967
10968 process_die (child_die, cu);
10969 }
10970
10971 add_using_directive (using_directives (cu),
10972 import_prefix,
10973 canonical_name,
10974 import_alias,
10975 imported_declaration,
10976 excludes,
10977 0,
10978 &objfile->objfile_obstack);
10979 }
10980
10981 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10982 types, but gives them a size of zero. Starting with version 14,
10983 ICC is compatible with GCC. */
10984
10985 static bool
10986 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10987 {
10988 if (!cu->checked_producer)
10989 check_producer (cu);
10990
10991 return cu->producer_is_icc_lt_14;
10992 }
10993
10994 /* ICC generates a DW_AT_type for C void functions. This was observed on
10995 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10996 which says that void functions should not have a DW_AT_type. */
10997
10998 static bool
10999 producer_is_icc (struct dwarf2_cu *cu)
11000 {
11001 if (!cu->checked_producer)
11002 check_producer (cu);
11003
11004 return cu->producer_is_icc;
11005 }
11006
11007 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11008 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11009 this, it was first present in GCC release 4.3.0. */
11010
11011 static bool
11012 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11013 {
11014 if (!cu->checked_producer)
11015 check_producer (cu);
11016
11017 return cu->producer_is_gcc_lt_4_3;
11018 }
11019
11020 static file_and_directory
11021 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11022 {
11023 file_and_directory res;
11024
11025 /* Find the filename. Do not use dwarf2_name here, since the filename
11026 is not a source language identifier. */
11027 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11028 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11029
11030 if (res.comp_dir == NULL
11031 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11032 && IS_ABSOLUTE_PATH (res.name))
11033 {
11034 res.comp_dir_storage = ldirname (res.name);
11035 if (!res.comp_dir_storage.empty ())
11036 res.comp_dir = res.comp_dir_storage.c_str ();
11037 }
11038 if (res.comp_dir != NULL)
11039 {
11040 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11041 directory, get rid of it. */
11042 const char *cp = strchr (res.comp_dir, ':');
11043
11044 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11045 res.comp_dir = cp + 1;
11046 }
11047
11048 if (res.name == NULL)
11049 res.name = "<unknown>";
11050
11051 return res;
11052 }
11053
11054 /* Handle DW_AT_stmt_list for a compilation unit.
11055 DIE is the DW_TAG_compile_unit die for CU.
11056 COMP_DIR is the compilation directory. LOWPC is passed to
11057 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11058
11059 static void
11060 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11061 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11062 {
11063 struct dwarf2_per_objfile *dwarf2_per_objfile
11064 = cu->per_cu->dwarf2_per_objfile;
11065 struct objfile *objfile = dwarf2_per_objfile->objfile;
11066 struct attribute *attr;
11067 struct line_header line_header_local;
11068 hashval_t line_header_local_hash;
11069 void **slot;
11070 int decode_mapping;
11071
11072 gdb_assert (! cu->per_cu->is_debug_types);
11073
11074 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11075 if (attr == NULL)
11076 return;
11077
11078 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11079
11080 /* The line header hash table is only created if needed (it exists to
11081 prevent redundant reading of the line table for partial_units).
11082 If we're given a partial_unit, we'll need it. If we're given a
11083 compile_unit, then use the line header hash table if it's already
11084 created, but don't create one just yet. */
11085
11086 if (dwarf2_per_objfile->line_header_hash == NULL
11087 && die->tag == DW_TAG_partial_unit)
11088 {
11089 dwarf2_per_objfile->line_header_hash
11090 = htab_create_alloc_ex (127, line_header_hash_voidp,
11091 line_header_eq_voidp,
11092 free_line_header_voidp,
11093 &objfile->objfile_obstack,
11094 hashtab_obstack_allocate,
11095 dummy_obstack_deallocate);
11096 }
11097
11098 line_header_local.sect_off = line_offset;
11099 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11100 line_header_local_hash = line_header_hash (&line_header_local);
11101 if (dwarf2_per_objfile->line_header_hash != NULL)
11102 {
11103 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11104 &line_header_local,
11105 line_header_local_hash, NO_INSERT);
11106
11107 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11108 is not present in *SLOT (since if there is something in *SLOT then
11109 it will be for a partial_unit). */
11110 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11111 {
11112 gdb_assert (*slot != NULL);
11113 cu->line_header = (struct line_header *) *slot;
11114 return;
11115 }
11116 }
11117
11118 /* dwarf_decode_line_header does not yet provide sufficient information.
11119 We always have to call also dwarf_decode_lines for it. */
11120 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11121 if (lh == NULL)
11122 return;
11123
11124 cu->line_header = lh.release ();
11125 cu->line_header_die_owner = die;
11126
11127 if (dwarf2_per_objfile->line_header_hash == NULL)
11128 slot = NULL;
11129 else
11130 {
11131 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11132 &line_header_local,
11133 line_header_local_hash, INSERT);
11134 gdb_assert (slot != NULL);
11135 }
11136 if (slot != NULL && *slot == NULL)
11137 {
11138 /* This newly decoded line number information unit will be owned
11139 by line_header_hash hash table. */
11140 *slot = cu->line_header;
11141 cu->line_header_die_owner = NULL;
11142 }
11143 else
11144 {
11145 /* We cannot free any current entry in (*slot) as that struct line_header
11146 may be already used by multiple CUs. Create only temporary decoded
11147 line_header for this CU - it may happen at most once for each line
11148 number information unit. And if we're not using line_header_hash
11149 then this is what we want as well. */
11150 gdb_assert (die->tag != DW_TAG_partial_unit);
11151 }
11152 decode_mapping = (die->tag != DW_TAG_partial_unit);
11153 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11154 decode_mapping);
11155
11156 }
11157
11158 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11159
11160 static void
11161 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11162 {
11163 struct dwarf2_per_objfile *dwarf2_per_objfile
11164 = cu->per_cu->dwarf2_per_objfile;
11165 struct objfile *objfile = dwarf2_per_objfile->objfile;
11166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11167 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11168 CORE_ADDR highpc = ((CORE_ADDR) 0);
11169 struct attribute *attr;
11170 struct die_info *child_die;
11171 CORE_ADDR baseaddr;
11172
11173 prepare_one_comp_unit (cu, die, cu->language);
11174 baseaddr = objfile->text_section_offset ();
11175
11176 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11177
11178 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11179 from finish_block. */
11180 if (lowpc == ((CORE_ADDR) -1))
11181 lowpc = highpc;
11182 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11183
11184 file_and_directory fnd = find_file_and_directory (die, cu);
11185
11186 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11187 standardised yet. As a workaround for the language detection we fall
11188 back to the DW_AT_producer string. */
11189 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11190 cu->language = language_opencl;
11191
11192 /* Similar hack for Go. */
11193 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11194 set_cu_language (DW_LANG_Go, cu);
11195
11196 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11197
11198 /* Decode line number information if present. We do this before
11199 processing child DIEs, so that the line header table is available
11200 for DW_AT_decl_file. */
11201 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11202
11203 /* Process all dies in compilation unit. */
11204 if (die->child != NULL)
11205 {
11206 child_die = die->child;
11207 while (child_die && child_die->tag)
11208 {
11209 process_die (child_die, cu);
11210 child_die = sibling_die (child_die);
11211 }
11212 }
11213
11214 /* Decode macro information, if present. Dwarf 2 macro information
11215 refers to information in the line number info statement program
11216 header, so we can only read it if we've read the header
11217 successfully. */
11218 attr = dwarf2_attr (die, DW_AT_macros, cu);
11219 if (attr == NULL)
11220 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11221 if (attr && cu->line_header)
11222 {
11223 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11224 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11225
11226 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11227 }
11228 else
11229 {
11230 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11231 if (attr && cu->line_header)
11232 {
11233 unsigned int macro_offset = DW_UNSND (attr);
11234
11235 dwarf_decode_macros (cu, macro_offset, 0);
11236 }
11237 }
11238 }
11239
11240 void
11241 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11242 {
11243 struct type_unit_group *tu_group;
11244 int first_time;
11245 struct attribute *attr;
11246 unsigned int i;
11247 struct signatured_type *sig_type;
11248
11249 gdb_assert (per_cu->is_debug_types);
11250 sig_type = (struct signatured_type *) per_cu;
11251
11252 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11253
11254 /* If we're using .gdb_index (includes -readnow) then
11255 per_cu->type_unit_group may not have been set up yet. */
11256 if (sig_type->type_unit_group == NULL)
11257 sig_type->type_unit_group = get_type_unit_group (this, attr);
11258 tu_group = sig_type->type_unit_group;
11259
11260 /* If we've already processed this stmt_list there's no real need to
11261 do it again, we could fake it and just recreate the part we need
11262 (file name,index -> symtab mapping). If data shows this optimization
11263 is useful we can do it then. */
11264 first_time = tu_group->compunit_symtab == NULL;
11265
11266 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11267 debug info. */
11268 line_header_up lh;
11269 if (attr != NULL)
11270 {
11271 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11272 lh = dwarf_decode_line_header (line_offset, this);
11273 }
11274 if (lh == NULL)
11275 {
11276 if (first_time)
11277 start_symtab ("", NULL, 0);
11278 else
11279 {
11280 gdb_assert (tu_group->symtabs == NULL);
11281 gdb_assert (m_builder == nullptr);
11282 struct compunit_symtab *cust = tu_group->compunit_symtab;
11283 m_builder.reset (new struct buildsym_compunit
11284 (COMPUNIT_OBJFILE (cust), "",
11285 COMPUNIT_DIRNAME (cust),
11286 compunit_language (cust),
11287 0, cust));
11288 }
11289 return;
11290 }
11291
11292 line_header = lh.release ();
11293 line_header_die_owner = die;
11294
11295 if (first_time)
11296 {
11297 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11298
11299 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11300 still initializing it, and our caller (a few levels up)
11301 process_full_type_unit still needs to know if this is the first
11302 time. */
11303
11304 tu_group->num_symtabs = line_header->file_names_size ();
11305 tu_group->symtabs = XNEWVEC (struct symtab *,
11306 line_header->file_names_size ());
11307
11308 auto &file_names = line_header->file_names ();
11309 for (i = 0; i < file_names.size (); ++i)
11310 {
11311 file_entry &fe = file_names[i];
11312 dwarf2_start_subfile (this, fe.name,
11313 fe.include_dir (line_header));
11314 buildsym_compunit *b = get_builder ();
11315 if (b->get_current_subfile ()->symtab == NULL)
11316 {
11317 /* NOTE: start_subfile will recognize when it's been
11318 passed a file it has already seen. So we can't
11319 assume there's a simple mapping from
11320 cu->line_header->file_names to subfiles, plus
11321 cu->line_header->file_names may contain dups. */
11322 b->get_current_subfile ()->symtab
11323 = allocate_symtab (cust, b->get_current_subfile ()->name);
11324 }
11325
11326 fe.symtab = b->get_current_subfile ()->symtab;
11327 tu_group->symtabs[i] = fe.symtab;
11328 }
11329 }
11330 else
11331 {
11332 gdb_assert (m_builder == nullptr);
11333 struct compunit_symtab *cust = tu_group->compunit_symtab;
11334 m_builder.reset (new struct buildsym_compunit
11335 (COMPUNIT_OBJFILE (cust), "",
11336 COMPUNIT_DIRNAME (cust),
11337 compunit_language (cust),
11338 0, cust));
11339
11340 auto &file_names = line_header->file_names ();
11341 for (i = 0; i < file_names.size (); ++i)
11342 {
11343 file_entry &fe = file_names[i];
11344 fe.symtab = tu_group->symtabs[i];
11345 }
11346 }
11347
11348 /* The main symtab is allocated last. Type units don't have DW_AT_name
11349 so they don't have a "real" (so to speak) symtab anyway.
11350 There is later code that will assign the main symtab to all symbols
11351 that don't have one. We need to handle the case of a symbol with a
11352 missing symtab (DW_AT_decl_file) anyway. */
11353 }
11354
11355 /* Process DW_TAG_type_unit.
11356 For TUs we want to skip the first top level sibling if it's not the
11357 actual type being defined by this TU. In this case the first top
11358 level sibling is there to provide context only. */
11359
11360 static void
11361 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11362 {
11363 struct die_info *child_die;
11364
11365 prepare_one_comp_unit (cu, die, language_minimal);
11366
11367 /* Initialize (or reinitialize) the machinery for building symtabs.
11368 We do this before processing child DIEs, so that the line header table
11369 is available for DW_AT_decl_file. */
11370 cu->setup_type_unit_groups (die);
11371
11372 if (die->child != NULL)
11373 {
11374 child_die = die->child;
11375 while (child_die && child_die->tag)
11376 {
11377 process_die (child_die, cu);
11378 child_die = sibling_die (child_die);
11379 }
11380 }
11381 }
11382 \f
11383 /* DWO/DWP files.
11384
11385 http://gcc.gnu.org/wiki/DebugFission
11386 http://gcc.gnu.org/wiki/DebugFissionDWP
11387
11388 To simplify handling of both DWO files ("object" files with the DWARF info)
11389 and DWP files (a file with the DWOs packaged up into one file), we treat
11390 DWP files as having a collection of virtual DWO files. */
11391
11392 static hashval_t
11393 hash_dwo_file (const void *item)
11394 {
11395 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11396 hashval_t hash;
11397
11398 hash = htab_hash_string (dwo_file->dwo_name);
11399 if (dwo_file->comp_dir != NULL)
11400 hash += htab_hash_string (dwo_file->comp_dir);
11401 return hash;
11402 }
11403
11404 static int
11405 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11406 {
11407 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11408 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11409
11410 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11411 return 0;
11412 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11413 return lhs->comp_dir == rhs->comp_dir;
11414 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11415 }
11416
11417 /* Allocate a hash table for DWO files. */
11418
11419 static htab_up
11420 allocate_dwo_file_hash_table (struct objfile *objfile)
11421 {
11422 auto delete_dwo_file = [] (void *item)
11423 {
11424 struct dwo_file *dwo_file = (struct dwo_file *) item;
11425
11426 delete dwo_file;
11427 };
11428
11429 return htab_up (htab_create_alloc_ex (41,
11430 hash_dwo_file,
11431 eq_dwo_file,
11432 delete_dwo_file,
11433 &objfile->objfile_obstack,
11434 hashtab_obstack_allocate,
11435 dummy_obstack_deallocate));
11436 }
11437
11438 /* Lookup DWO file DWO_NAME. */
11439
11440 static void **
11441 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11442 const char *dwo_name,
11443 const char *comp_dir)
11444 {
11445 struct dwo_file find_entry;
11446 void **slot;
11447
11448 if (dwarf2_per_objfile->dwo_files == NULL)
11449 dwarf2_per_objfile->dwo_files
11450 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11451
11452 find_entry.dwo_name = dwo_name;
11453 find_entry.comp_dir = comp_dir;
11454 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11455 INSERT);
11456
11457 return slot;
11458 }
11459
11460 static hashval_t
11461 hash_dwo_unit (const void *item)
11462 {
11463 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11464
11465 /* This drops the top 32 bits of the id, but is ok for a hash. */
11466 return dwo_unit->signature;
11467 }
11468
11469 static int
11470 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11471 {
11472 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11473 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11474
11475 /* The signature is assumed to be unique within the DWO file.
11476 So while object file CU dwo_id's always have the value zero,
11477 that's OK, assuming each object file DWO file has only one CU,
11478 and that's the rule for now. */
11479 return lhs->signature == rhs->signature;
11480 }
11481
11482 /* Allocate a hash table for DWO CUs,TUs.
11483 There is one of these tables for each of CUs,TUs for each DWO file. */
11484
11485 static htab_up
11486 allocate_dwo_unit_table (struct objfile *objfile)
11487 {
11488 /* Start out with a pretty small number.
11489 Generally DWO files contain only one CU and maybe some TUs. */
11490 return htab_up (htab_create_alloc (3,
11491 hash_dwo_unit,
11492 eq_dwo_unit,
11493 NULL, xcalloc, xfree));
11494 }
11495
11496 /* die_reader_func for create_dwo_cu. */
11497
11498 static void
11499 create_dwo_cu_reader (const struct die_reader_specs *reader,
11500 const gdb_byte *info_ptr,
11501 struct die_info *comp_unit_die,
11502 struct dwo_file *dwo_file,
11503 struct dwo_unit *dwo_unit)
11504 {
11505 struct dwarf2_cu *cu = reader->cu;
11506 sect_offset sect_off = cu->per_cu->sect_off;
11507 struct dwarf2_section_info *section = cu->per_cu->section;
11508
11509 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11510 if (!signature.has_value ())
11511 {
11512 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11513 " its dwo_id [in module %s]"),
11514 sect_offset_str (sect_off), dwo_file->dwo_name);
11515 return;
11516 }
11517
11518 dwo_unit->dwo_file = dwo_file;
11519 dwo_unit->signature = *signature;
11520 dwo_unit->section = section;
11521 dwo_unit->sect_off = sect_off;
11522 dwo_unit->length = cu->per_cu->length;
11523
11524 if (dwarf_read_debug)
11525 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11526 sect_offset_str (sect_off),
11527 hex_string (dwo_unit->signature));
11528 }
11529
11530 /* Create the dwo_units for the CUs in a DWO_FILE.
11531 Note: This function processes DWO files only, not DWP files. */
11532
11533 static void
11534 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11535 dwarf2_cu *cu, struct dwo_file &dwo_file,
11536 dwarf2_section_info &section, htab_up &cus_htab)
11537 {
11538 struct objfile *objfile = dwarf2_per_objfile->objfile;
11539 const gdb_byte *info_ptr, *end_ptr;
11540
11541 section.read (objfile);
11542 info_ptr = section.buffer;
11543
11544 if (info_ptr == NULL)
11545 return;
11546
11547 if (dwarf_read_debug)
11548 {
11549 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11550 section.get_name (),
11551 section.get_file_name ());
11552 }
11553
11554 end_ptr = info_ptr + section.size;
11555 while (info_ptr < end_ptr)
11556 {
11557 struct dwarf2_per_cu_data per_cu;
11558 struct dwo_unit read_unit {};
11559 struct dwo_unit *dwo_unit;
11560 void **slot;
11561 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11562
11563 memset (&per_cu, 0, sizeof (per_cu));
11564 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11565 per_cu.is_debug_types = 0;
11566 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11567 per_cu.section = &section;
11568
11569 cutu_reader reader (&per_cu, cu, &dwo_file);
11570 if (!reader.dummy_p)
11571 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11572 &dwo_file, &read_unit);
11573 info_ptr += per_cu.length;
11574
11575 // If the unit could not be parsed, skip it.
11576 if (read_unit.dwo_file == NULL)
11577 continue;
11578
11579 if (cus_htab == NULL)
11580 cus_htab = allocate_dwo_unit_table (objfile);
11581
11582 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11583 *dwo_unit = read_unit;
11584 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11585 gdb_assert (slot != NULL);
11586 if (*slot != NULL)
11587 {
11588 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11589 sect_offset dup_sect_off = dup_cu->sect_off;
11590
11591 complaint (_("debug cu entry at offset %s is duplicate to"
11592 " the entry at offset %s, signature %s"),
11593 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11594 hex_string (dwo_unit->signature));
11595 }
11596 *slot = (void *)dwo_unit;
11597 }
11598 }
11599
11600 /* DWP file .debug_{cu,tu}_index section format:
11601 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11602
11603 DWP Version 1:
11604
11605 Both index sections have the same format, and serve to map a 64-bit
11606 signature to a set of section numbers. Each section begins with a header,
11607 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11608 indexes, and a pool of 32-bit section numbers. The index sections will be
11609 aligned at 8-byte boundaries in the file.
11610
11611 The index section header consists of:
11612
11613 V, 32 bit version number
11614 -, 32 bits unused
11615 N, 32 bit number of compilation units or type units in the index
11616 M, 32 bit number of slots in the hash table
11617
11618 Numbers are recorded using the byte order of the application binary.
11619
11620 The hash table begins at offset 16 in the section, and consists of an array
11621 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11622 order of the application binary). Unused slots in the hash table are 0.
11623 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11624
11625 The parallel table begins immediately after the hash table
11626 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11627 array of 32-bit indexes (using the byte order of the application binary),
11628 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11629 table contains a 32-bit index into the pool of section numbers. For unused
11630 hash table slots, the corresponding entry in the parallel table will be 0.
11631
11632 The pool of section numbers begins immediately following the hash table
11633 (at offset 16 + 12 * M from the beginning of the section). The pool of
11634 section numbers consists of an array of 32-bit words (using the byte order
11635 of the application binary). Each item in the array is indexed starting
11636 from 0. The hash table entry provides the index of the first section
11637 number in the set. Additional section numbers in the set follow, and the
11638 set is terminated by a 0 entry (section number 0 is not used in ELF).
11639
11640 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11641 section must be the first entry in the set, and the .debug_abbrev.dwo must
11642 be the second entry. Other members of the set may follow in any order.
11643
11644 ---
11645
11646 DWP Version 2:
11647
11648 DWP Version 2 combines all the .debug_info, etc. sections into one,
11649 and the entries in the index tables are now offsets into these sections.
11650 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11651 section.
11652
11653 Index Section Contents:
11654 Header
11655 Hash Table of Signatures dwp_hash_table.hash_table
11656 Parallel Table of Indices dwp_hash_table.unit_table
11657 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11658 Table of Section Sizes dwp_hash_table.v2.sizes
11659
11660 The index section header consists of:
11661
11662 V, 32 bit version number
11663 L, 32 bit number of columns in the table of section offsets
11664 N, 32 bit number of compilation units or type units in the index
11665 M, 32 bit number of slots in the hash table
11666
11667 Numbers are recorded using the byte order of the application binary.
11668
11669 The hash table has the same format as version 1.
11670 The parallel table of indices has the same format as version 1,
11671 except that the entries are origin-1 indices into the table of sections
11672 offsets and the table of section sizes.
11673
11674 The table of offsets begins immediately following the parallel table
11675 (at offset 16 + 12 * M from the beginning of the section). The table is
11676 a two-dimensional array of 32-bit words (using the byte order of the
11677 application binary), with L columns and N+1 rows, in row-major order.
11678 Each row in the array is indexed starting from 0. The first row provides
11679 a key to the remaining rows: each column in this row provides an identifier
11680 for a debug section, and the offsets in the same column of subsequent rows
11681 refer to that section. The section identifiers are:
11682
11683 DW_SECT_INFO 1 .debug_info.dwo
11684 DW_SECT_TYPES 2 .debug_types.dwo
11685 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11686 DW_SECT_LINE 4 .debug_line.dwo
11687 DW_SECT_LOC 5 .debug_loc.dwo
11688 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11689 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11690 DW_SECT_MACRO 8 .debug_macro.dwo
11691
11692 The offsets provided by the CU and TU index sections are the base offsets
11693 for the contributions made by each CU or TU to the corresponding section
11694 in the package file. Each CU and TU header contains an abbrev_offset
11695 field, used to find the abbreviations table for that CU or TU within the
11696 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11697 be interpreted as relative to the base offset given in the index section.
11698 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11699 should be interpreted as relative to the base offset for .debug_line.dwo,
11700 and offsets into other debug sections obtained from DWARF attributes should
11701 also be interpreted as relative to the corresponding base offset.
11702
11703 The table of sizes begins immediately following the table of offsets.
11704 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11705 with L columns and N rows, in row-major order. Each row in the array is
11706 indexed starting from 1 (row 0 is shared by the two tables).
11707
11708 ---
11709
11710 Hash table lookup is handled the same in version 1 and 2:
11711
11712 We assume that N and M will not exceed 2^32 - 1.
11713 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11714
11715 Given a 64-bit compilation unit signature or a type signature S, an entry
11716 in the hash table is located as follows:
11717
11718 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11719 the low-order k bits all set to 1.
11720
11721 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11722
11723 3) If the hash table entry at index H matches the signature, use that
11724 entry. If the hash table entry at index H is unused (all zeroes),
11725 terminate the search: the signature is not present in the table.
11726
11727 4) Let H = (H + H') modulo M. Repeat at Step 3.
11728
11729 Because M > N and H' and M are relatively prime, the search is guaranteed
11730 to stop at an unused slot or find the match. */
11731
11732 /* Create a hash table to map DWO IDs to their CU/TU entry in
11733 .debug_{info,types}.dwo in DWP_FILE.
11734 Returns NULL if there isn't one.
11735 Note: This function processes DWP files only, not DWO files. */
11736
11737 static struct dwp_hash_table *
11738 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11739 struct dwp_file *dwp_file, int is_debug_types)
11740 {
11741 struct objfile *objfile = dwarf2_per_objfile->objfile;
11742 bfd *dbfd = dwp_file->dbfd.get ();
11743 const gdb_byte *index_ptr, *index_end;
11744 struct dwarf2_section_info *index;
11745 uint32_t version, nr_columns, nr_units, nr_slots;
11746 struct dwp_hash_table *htab;
11747
11748 if (is_debug_types)
11749 index = &dwp_file->sections.tu_index;
11750 else
11751 index = &dwp_file->sections.cu_index;
11752
11753 if (index->empty ())
11754 return NULL;
11755 index->read (objfile);
11756
11757 index_ptr = index->buffer;
11758 index_end = index_ptr + index->size;
11759
11760 version = read_4_bytes (dbfd, index_ptr);
11761 index_ptr += 4;
11762 if (version == 2)
11763 nr_columns = read_4_bytes (dbfd, index_ptr);
11764 else
11765 nr_columns = 0;
11766 index_ptr += 4;
11767 nr_units = read_4_bytes (dbfd, index_ptr);
11768 index_ptr += 4;
11769 nr_slots = read_4_bytes (dbfd, index_ptr);
11770 index_ptr += 4;
11771
11772 if (version != 1 && version != 2)
11773 {
11774 error (_("Dwarf Error: unsupported DWP file version (%s)"
11775 " [in module %s]"),
11776 pulongest (version), dwp_file->name);
11777 }
11778 if (nr_slots != (nr_slots & -nr_slots))
11779 {
11780 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11781 " is not power of 2 [in module %s]"),
11782 pulongest (nr_slots), dwp_file->name);
11783 }
11784
11785 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11786 htab->version = version;
11787 htab->nr_columns = nr_columns;
11788 htab->nr_units = nr_units;
11789 htab->nr_slots = nr_slots;
11790 htab->hash_table = index_ptr;
11791 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11792
11793 /* Exit early if the table is empty. */
11794 if (nr_slots == 0 || nr_units == 0
11795 || (version == 2 && nr_columns == 0))
11796 {
11797 /* All must be zero. */
11798 if (nr_slots != 0 || nr_units != 0
11799 || (version == 2 && nr_columns != 0))
11800 {
11801 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11802 " all zero [in modules %s]"),
11803 dwp_file->name);
11804 }
11805 return htab;
11806 }
11807
11808 if (version == 1)
11809 {
11810 htab->section_pool.v1.indices =
11811 htab->unit_table + sizeof (uint32_t) * nr_slots;
11812 /* It's harder to decide whether the section is too small in v1.
11813 V1 is deprecated anyway so we punt. */
11814 }
11815 else
11816 {
11817 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11818 int *ids = htab->section_pool.v2.section_ids;
11819 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11820 /* Reverse map for error checking. */
11821 int ids_seen[DW_SECT_MAX + 1];
11822 int i;
11823
11824 if (nr_columns < 2)
11825 {
11826 error (_("Dwarf Error: bad DWP hash table, too few columns"
11827 " in section table [in module %s]"),
11828 dwp_file->name);
11829 }
11830 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, too many columns"
11833 " in section table [in module %s]"),
11834 dwp_file->name);
11835 }
11836 memset (ids, 255, sizeof_ids);
11837 memset (ids_seen, 255, sizeof (ids_seen));
11838 for (i = 0; i < nr_columns; ++i)
11839 {
11840 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11841
11842 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11843 {
11844 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11845 " in section table [in module %s]"),
11846 id, dwp_file->name);
11847 }
11848 if (ids_seen[id] != -1)
11849 {
11850 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11851 " id %d in section table [in module %s]"),
11852 id, dwp_file->name);
11853 }
11854 ids_seen[id] = i;
11855 ids[i] = id;
11856 }
11857 /* Must have exactly one info or types section. */
11858 if (((ids_seen[DW_SECT_INFO] != -1)
11859 + (ids_seen[DW_SECT_TYPES] != -1))
11860 != 1)
11861 {
11862 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11863 " DWO info/types section [in module %s]"),
11864 dwp_file->name);
11865 }
11866 /* Must have an abbrev section. */
11867 if (ids_seen[DW_SECT_ABBREV] == -1)
11868 {
11869 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11870 " section [in module %s]"),
11871 dwp_file->name);
11872 }
11873 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11874 htab->section_pool.v2.sizes =
11875 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11876 * nr_units * nr_columns);
11877 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11878 * nr_units * nr_columns))
11879 > index_end)
11880 {
11881 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11882 " [in module %s]"),
11883 dwp_file->name);
11884 }
11885 }
11886
11887 return htab;
11888 }
11889
11890 /* Update SECTIONS with the data from SECTP.
11891
11892 This function is like the other "locate" section routines that are
11893 passed to bfd_map_over_sections, but in this context the sections to
11894 read comes from the DWP V1 hash table, not the full ELF section table.
11895
11896 The result is non-zero for success, or zero if an error was found. */
11897
11898 static int
11899 locate_v1_virtual_dwo_sections (asection *sectp,
11900 struct virtual_v1_dwo_sections *sections)
11901 {
11902 const struct dwop_section_names *names = &dwop_section_names;
11903
11904 if (section_is_p (sectp->name, &names->abbrev_dwo))
11905 {
11906 /* There can be only one. */
11907 if (sections->abbrev.s.section != NULL)
11908 return 0;
11909 sections->abbrev.s.section = sectp;
11910 sections->abbrev.size = bfd_section_size (sectp);
11911 }
11912 else if (section_is_p (sectp->name, &names->info_dwo)
11913 || section_is_p (sectp->name, &names->types_dwo))
11914 {
11915 /* There can be only one. */
11916 if (sections->info_or_types.s.section != NULL)
11917 return 0;
11918 sections->info_or_types.s.section = sectp;
11919 sections->info_or_types.size = bfd_section_size (sectp);
11920 }
11921 else if (section_is_p (sectp->name, &names->line_dwo))
11922 {
11923 /* There can be only one. */
11924 if (sections->line.s.section != NULL)
11925 return 0;
11926 sections->line.s.section = sectp;
11927 sections->line.size = bfd_section_size (sectp);
11928 }
11929 else if (section_is_p (sectp->name, &names->loc_dwo))
11930 {
11931 /* There can be only one. */
11932 if (sections->loc.s.section != NULL)
11933 return 0;
11934 sections->loc.s.section = sectp;
11935 sections->loc.size = bfd_section_size (sectp);
11936 }
11937 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11938 {
11939 /* There can be only one. */
11940 if (sections->macinfo.s.section != NULL)
11941 return 0;
11942 sections->macinfo.s.section = sectp;
11943 sections->macinfo.size = bfd_section_size (sectp);
11944 }
11945 else if (section_is_p (sectp->name, &names->macro_dwo))
11946 {
11947 /* There can be only one. */
11948 if (sections->macro.s.section != NULL)
11949 return 0;
11950 sections->macro.s.section = sectp;
11951 sections->macro.size = bfd_section_size (sectp);
11952 }
11953 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11954 {
11955 /* There can be only one. */
11956 if (sections->str_offsets.s.section != NULL)
11957 return 0;
11958 sections->str_offsets.s.section = sectp;
11959 sections->str_offsets.size = bfd_section_size (sectp);
11960 }
11961 else
11962 {
11963 /* No other kind of section is valid. */
11964 return 0;
11965 }
11966
11967 return 1;
11968 }
11969
11970 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11971 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11972 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11973 This is for DWP version 1 files. */
11974
11975 static struct dwo_unit *
11976 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11977 struct dwp_file *dwp_file,
11978 uint32_t unit_index,
11979 const char *comp_dir,
11980 ULONGEST signature, int is_debug_types)
11981 {
11982 struct objfile *objfile = dwarf2_per_objfile->objfile;
11983 const struct dwp_hash_table *dwp_htab =
11984 is_debug_types ? dwp_file->tus : dwp_file->cus;
11985 bfd *dbfd = dwp_file->dbfd.get ();
11986 const char *kind = is_debug_types ? "TU" : "CU";
11987 struct dwo_file *dwo_file;
11988 struct dwo_unit *dwo_unit;
11989 struct virtual_v1_dwo_sections sections;
11990 void **dwo_file_slot;
11991 int i;
11992
11993 gdb_assert (dwp_file->version == 1);
11994
11995 if (dwarf_read_debug)
11996 {
11997 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11998 kind,
11999 pulongest (unit_index), hex_string (signature),
12000 dwp_file->name);
12001 }
12002
12003 /* Fetch the sections of this DWO unit.
12004 Put a limit on the number of sections we look for so that bad data
12005 doesn't cause us to loop forever. */
12006
12007 #define MAX_NR_V1_DWO_SECTIONS \
12008 (1 /* .debug_info or .debug_types */ \
12009 + 1 /* .debug_abbrev */ \
12010 + 1 /* .debug_line */ \
12011 + 1 /* .debug_loc */ \
12012 + 1 /* .debug_str_offsets */ \
12013 + 1 /* .debug_macro or .debug_macinfo */ \
12014 + 1 /* trailing zero */)
12015
12016 memset (&sections, 0, sizeof (sections));
12017
12018 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12019 {
12020 asection *sectp;
12021 uint32_t section_nr =
12022 read_4_bytes (dbfd,
12023 dwp_htab->section_pool.v1.indices
12024 + (unit_index + i) * sizeof (uint32_t));
12025
12026 if (section_nr == 0)
12027 break;
12028 if (section_nr >= dwp_file->num_sections)
12029 {
12030 error (_("Dwarf Error: bad DWP hash table, section number too large"
12031 " [in module %s]"),
12032 dwp_file->name);
12033 }
12034
12035 sectp = dwp_file->elf_sections[section_nr];
12036 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12037 {
12038 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12039 " [in module %s]"),
12040 dwp_file->name);
12041 }
12042 }
12043
12044 if (i < 2
12045 || sections.info_or_types.empty ()
12046 || sections.abbrev.empty ())
12047 {
12048 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12049 " [in module %s]"),
12050 dwp_file->name);
12051 }
12052 if (i == MAX_NR_V1_DWO_SECTIONS)
12053 {
12054 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12055 " [in module %s]"),
12056 dwp_file->name);
12057 }
12058
12059 /* It's easier for the rest of the code if we fake a struct dwo_file and
12060 have dwo_unit "live" in that. At least for now.
12061
12062 The DWP file can be made up of a random collection of CUs and TUs.
12063 However, for each CU + set of TUs that came from the same original DWO
12064 file, we can combine them back into a virtual DWO file to save space
12065 (fewer struct dwo_file objects to allocate). Remember that for really
12066 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12067
12068 std::string virtual_dwo_name =
12069 string_printf ("virtual-dwo/%d-%d-%d-%d",
12070 sections.abbrev.get_id (),
12071 sections.line.get_id (),
12072 sections.loc.get_id (),
12073 sections.str_offsets.get_id ());
12074 /* Can we use an existing virtual DWO file? */
12075 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12076 virtual_dwo_name.c_str (),
12077 comp_dir);
12078 /* Create one if necessary. */
12079 if (*dwo_file_slot == NULL)
12080 {
12081 if (dwarf_read_debug)
12082 {
12083 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12084 virtual_dwo_name.c_str ());
12085 }
12086 dwo_file = new struct dwo_file;
12087 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12088 virtual_dwo_name);
12089 dwo_file->comp_dir = comp_dir;
12090 dwo_file->sections.abbrev = sections.abbrev;
12091 dwo_file->sections.line = sections.line;
12092 dwo_file->sections.loc = sections.loc;
12093 dwo_file->sections.macinfo = sections.macinfo;
12094 dwo_file->sections.macro = sections.macro;
12095 dwo_file->sections.str_offsets = sections.str_offsets;
12096 /* The "str" section is global to the entire DWP file. */
12097 dwo_file->sections.str = dwp_file->sections.str;
12098 /* The info or types section is assigned below to dwo_unit,
12099 there's no need to record it in dwo_file.
12100 Also, we can't simply record type sections in dwo_file because
12101 we record a pointer into the vector in dwo_unit. As we collect more
12102 types we'll grow the vector and eventually have to reallocate space
12103 for it, invalidating all copies of pointers into the previous
12104 contents. */
12105 *dwo_file_slot = dwo_file;
12106 }
12107 else
12108 {
12109 if (dwarf_read_debug)
12110 {
12111 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12112 virtual_dwo_name.c_str ());
12113 }
12114 dwo_file = (struct dwo_file *) *dwo_file_slot;
12115 }
12116
12117 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12118 dwo_unit->dwo_file = dwo_file;
12119 dwo_unit->signature = signature;
12120 dwo_unit->section =
12121 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12122 *dwo_unit->section = sections.info_or_types;
12123 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12124
12125 return dwo_unit;
12126 }
12127
12128 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12129 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12130 piece within that section used by a TU/CU, return a virtual section
12131 of just that piece. */
12132
12133 static struct dwarf2_section_info
12134 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12135 struct dwarf2_section_info *section,
12136 bfd_size_type offset, bfd_size_type size)
12137 {
12138 struct dwarf2_section_info result;
12139 asection *sectp;
12140
12141 gdb_assert (section != NULL);
12142 gdb_assert (!section->is_virtual);
12143
12144 memset (&result, 0, sizeof (result));
12145 result.s.containing_section = section;
12146 result.is_virtual = true;
12147
12148 if (size == 0)
12149 return result;
12150
12151 sectp = section->get_bfd_section ();
12152
12153 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12154 bounds of the real section. This is a pretty-rare event, so just
12155 flag an error (easier) instead of a warning and trying to cope. */
12156 if (sectp == NULL
12157 || offset + size > bfd_section_size (sectp))
12158 {
12159 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12160 " in section %s [in module %s]"),
12161 sectp ? bfd_section_name (sectp) : "<unknown>",
12162 objfile_name (dwarf2_per_objfile->objfile));
12163 }
12164
12165 result.virtual_offset = offset;
12166 result.size = size;
12167 return result;
12168 }
12169
12170 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12171 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12172 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12173 This is for DWP version 2 files. */
12174
12175 static struct dwo_unit *
12176 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12177 struct dwp_file *dwp_file,
12178 uint32_t unit_index,
12179 const char *comp_dir,
12180 ULONGEST signature, int is_debug_types)
12181 {
12182 struct objfile *objfile = dwarf2_per_objfile->objfile;
12183 const struct dwp_hash_table *dwp_htab =
12184 is_debug_types ? dwp_file->tus : dwp_file->cus;
12185 bfd *dbfd = dwp_file->dbfd.get ();
12186 const char *kind = is_debug_types ? "TU" : "CU";
12187 struct dwo_file *dwo_file;
12188 struct dwo_unit *dwo_unit;
12189 struct virtual_v2_dwo_sections sections;
12190 void **dwo_file_slot;
12191 int i;
12192
12193 gdb_assert (dwp_file->version == 2);
12194
12195 if (dwarf_read_debug)
12196 {
12197 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12198 kind,
12199 pulongest (unit_index), hex_string (signature),
12200 dwp_file->name);
12201 }
12202
12203 /* Fetch the section offsets of this DWO unit. */
12204
12205 memset (&sections, 0, sizeof (sections));
12206
12207 for (i = 0; i < dwp_htab->nr_columns; ++i)
12208 {
12209 uint32_t offset = read_4_bytes (dbfd,
12210 dwp_htab->section_pool.v2.offsets
12211 + (((unit_index - 1) * dwp_htab->nr_columns
12212 + i)
12213 * sizeof (uint32_t)));
12214 uint32_t size = read_4_bytes (dbfd,
12215 dwp_htab->section_pool.v2.sizes
12216 + (((unit_index - 1) * dwp_htab->nr_columns
12217 + i)
12218 * sizeof (uint32_t)));
12219
12220 switch (dwp_htab->section_pool.v2.section_ids[i])
12221 {
12222 case DW_SECT_INFO:
12223 case DW_SECT_TYPES:
12224 sections.info_or_types_offset = offset;
12225 sections.info_or_types_size = size;
12226 break;
12227 case DW_SECT_ABBREV:
12228 sections.abbrev_offset = offset;
12229 sections.abbrev_size = size;
12230 break;
12231 case DW_SECT_LINE:
12232 sections.line_offset = offset;
12233 sections.line_size = size;
12234 break;
12235 case DW_SECT_LOC:
12236 sections.loc_offset = offset;
12237 sections.loc_size = size;
12238 break;
12239 case DW_SECT_STR_OFFSETS:
12240 sections.str_offsets_offset = offset;
12241 sections.str_offsets_size = size;
12242 break;
12243 case DW_SECT_MACINFO:
12244 sections.macinfo_offset = offset;
12245 sections.macinfo_size = size;
12246 break;
12247 case DW_SECT_MACRO:
12248 sections.macro_offset = offset;
12249 sections.macro_size = size;
12250 break;
12251 }
12252 }
12253
12254 /* It's easier for the rest of the code if we fake a struct dwo_file and
12255 have dwo_unit "live" in that. At least for now.
12256
12257 The DWP file can be made up of a random collection of CUs and TUs.
12258 However, for each CU + set of TUs that came from the same original DWO
12259 file, we can combine them back into a virtual DWO file to save space
12260 (fewer struct dwo_file objects to allocate). Remember that for really
12261 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12262
12263 std::string virtual_dwo_name =
12264 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12265 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12266 (long) (sections.line_size ? sections.line_offset : 0),
12267 (long) (sections.loc_size ? sections.loc_offset : 0),
12268 (long) (sections.str_offsets_size
12269 ? sections.str_offsets_offset : 0));
12270 /* Can we use an existing virtual DWO file? */
12271 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12272 virtual_dwo_name.c_str (),
12273 comp_dir);
12274 /* Create one if necessary. */
12275 if (*dwo_file_slot == NULL)
12276 {
12277 if (dwarf_read_debug)
12278 {
12279 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12280 virtual_dwo_name.c_str ());
12281 }
12282 dwo_file = new struct dwo_file;
12283 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12284 virtual_dwo_name);
12285 dwo_file->comp_dir = comp_dir;
12286 dwo_file->sections.abbrev =
12287 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12288 sections.abbrev_offset, sections.abbrev_size);
12289 dwo_file->sections.line =
12290 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12291 sections.line_offset, sections.line_size);
12292 dwo_file->sections.loc =
12293 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12294 sections.loc_offset, sections.loc_size);
12295 dwo_file->sections.macinfo =
12296 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12297 sections.macinfo_offset, sections.macinfo_size);
12298 dwo_file->sections.macro =
12299 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12300 sections.macro_offset, sections.macro_size);
12301 dwo_file->sections.str_offsets =
12302 create_dwp_v2_section (dwarf2_per_objfile,
12303 &dwp_file->sections.str_offsets,
12304 sections.str_offsets_offset,
12305 sections.str_offsets_size);
12306 /* The "str" section is global to the entire DWP file. */
12307 dwo_file->sections.str = dwp_file->sections.str;
12308 /* The info or types section is assigned below to dwo_unit,
12309 there's no need to record it in dwo_file.
12310 Also, we can't simply record type sections in dwo_file because
12311 we record a pointer into the vector in dwo_unit. As we collect more
12312 types we'll grow the vector and eventually have to reallocate space
12313 for it, invalidating all copies of pointers into the previous
12314 contents. */
12315 *dwo_file_slot = dwo_file;
12316 }
12317 else
12318 {
12319 if (dwarf_read_debug)
12320 {
12321 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12322 virtual_dwo_name.c_str ());
12323 }
12324 dwo_file = (struct dwo_file *) *dwo_file_slot;
12325 }
12326
12327 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12328 dwo_unit->dwo_file = dwo_file;
12329 dwo_unit->signature = signature;
12330 dwo_unit->section =
12331 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12332 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12333 is_debug_types
12334 ? &dwp_file->sections.types
12335 : &dwp_file->sections.info,
12336 sections.info_or_types_offset,
12337 sections.info_or_types_size);
12338 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12339
12340 return dwo_unit;
12341 }
12342
12343 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12344 Returns NULL if the signature isn't found. */
12345
12346 static struct dwo_unit *
12347 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12348 struct dwp_file *dwp_file, const char *comp_dir,
12349 ULONGEST signature, int is_debug_types)
12350 {
12351 const struct dwp_hash_table *dwp_htab =
12352 is_debug_types ? dwp_file->tus : dwp_file->cus;
12353 bfd *dbfd = dwp_file->dbfd.get ();
12354 uint32_t mask = dwp_htab->nr_slots - 1;
12355 uint32_t hash = signature & mask;
12356 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12357 unsigned int i;
12358 void **slot;
12359 struct dwo_unit find_dwo_cu;
12360
12361 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12362 find_dwo_cu.signature = signature;
12363 slot = htab_find_slot (is_debug_types
12364 ? dwp_file->loaded_tus
12365 : dwp_file->loaded_cus,
12366 &find_dwo_cu, INSERT);
12367
12368 if (*slot != NULL)
12369 return (struct dwo_unit *) *slot;
12370
12371 /* Use a for loop so that we don't loop forever on bad debug info. */
12372 for (i = 0; i < dwp_htab->nr_slots; ++i)
12373 {
12374 ULONGEST signature_in_table;
12375
12376 signature_in_table =
12377 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12378 if (signature_in_table == signature)
12379 {
12380 uint32_t unit_index =
12381 read_4_bytes (dbfd,
12382 dwp_htab->unit_table + hash * sizeof (uint32_t));
12383
12384 if (dwp_file->version == 1)
12385 {
12386 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12387 dwp_file, unit_index,
12388 comp_dir, signature,
12389 is_debug_types);
12390 }
12391 else
12392 {
12393 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12394 dwp_file, unit_index,
12395 comp_dir, signature,
12396 is_debug_types);
12397 }
12398 return (struct dwo_unit *) *slot;
12399 }
12400 if (signature_in_table == 0)
12401 return NULL;
12402 hash = (hash + hash2) & mask;
12403 }
12404
12405 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12406 " [in module %s]"),
12407 dwp_file->name);
12408 }
12409
12410 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12411 Open the file specified by FILE_NAME and hand it off to BFD for
12412 preliminary analysis. Return a newly initialized bfd *, which
12413 includes a canonicalized copy of FILE_NAME.
12414 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12415 SEARCH_CWD is true if the current directory is to be searched.
12416 It will be searched before debug-file-directory.
12417 If successful, the file is added to the bfd include table of the
12418 objfile's bfd (see gdb_bfd_record_inclusion).
12419 If unable to find/open the file, return NULL.
12420 NOTE: This function is derived from symfile_bfd_open. */
12421
12422 static gdb_bfd_ref_ptr
12423 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12424 const char *file_name, int is_dwp, int search_cwd)
12425 {
12426 int desc;
12427 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12428 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12429 to debug_file_directory. */
12430 const char *search_path;
12431 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12432
12433 gdb::unique_xmalloc_ptr<char> search_path_holder;
12434 if (search_cwd)
12435 {
12436 if (*debug_file_directory != '\0')
12437 {
12438 search_path_holder.reset (concat (".", dirname_separator_string,
12439 debug_file_directory,
12440 (char *) NULL));
12441 search_path = search_path_holder.get ();
12442 }
12443 else
12444 search_path = ".";
12445 }
12446 else
12447 search_path = debug_file_directory;
12448
12449 openp_flags flags = OPF_RETURN_REALPATH;
12450 if (is_dwp)
12451 flags |= OPF_SEARCH_IN_PATH;
12452
12453 gdb::unique_xmalloc_ptr<char> absolute_name;
12454 desc = openp (search_path, flags, file_name,
12455 O_RDONLY | O_BINARY, &absolute_name);
12456 if (desc < 0)
12457 return NULL;
12458
12459 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12460 gnutarget, desc));
12461 if (sym_bfd == NULL)
12462 return NULL;
12463 bfd_set_cacheable (sym_bfd.get (), 1);
12464
12465 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12466 return NULL;
12467
12468 /* Success. Record the bfd as having been included by the objfile's bfd.
12469 This is important because things like demangled_names_hash lives in the
12470 objfile's per_bfd space and may have references to things like symbol
12471 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12472 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12473
12474 return sym_bfd;
12475 }
12476
12477 /* Try to open DWO file FILE_NAME.
12478 COMP_DIR is the DW_AT_comp_dir attribute.
12479 The result is the bfd handle of the file.
12480 If there is a problem finding or opening the file, return NULL.
12481 Upon success, the canonicalized path of the file is stored in the bfd,
12482 same as symfile_bfd_open. */
12483
12484 static gdb_bfd_ref_ptr
12485 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12486 const char *file_name, const char *comp_dir)
12487 {
12488 if (IS_ABSOLUTE_PATH (file_name))
12489 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12490 0 /*is_dwp*/, 0 /*search_cwd*/);
12491
12492 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12493
12494 if (comp_dir != NULL)
12495 {
12496 gdb::unique_xmalloc_ptr<char> path_to_try
12497 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12498
12499 /* NOTE: If comp_dir is a relative path, this will also try the
12500 search path, which seems useful. */
12501 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12502 path_to_try.get (),
12503 0 /*is_dwp*/,
12504 1 /*search_cwd*/));
12505 if (abfd != NULL)
12506 return abfd;
12507 }
12508
12509 /* That didn't work, try debug-file-directory, which, despite its name,
12510 is a list of paths. */
12511
12512 if (*debug_file_directory == '\0')
12513 return NULL;
12514
12515 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12516 0 /*is_dwp*/, 1 /*search_cwd*/);
12517 }
12518
12519 /* This function is mapped across the sections and remembers the offset and
12520 size of each of the DWO debugging sections we are interested in. */
12521
12522 static void
12523 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12524 {
12525 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12526 const struct dwop_section_names *names = &dwop_section_names;
12527
12528 if (section_is_p (sectp->name, &names->abbrev_dwo))
12529 {
12530 dwo_sections->abbrev.s.section = sectp;
12531 dwo_sections->abbrev.size = bfd_section_size (sectp);
12532 }
12533 else if (section_is_p (sectp->name, &names->info_dwo))
12534 {
12535 dwo_sections->info.s.section = sectp;
12536 dwo_sections->info.size = bfd_section_size (sectp);
12537 }
12538 else if (section_is_p (sectp->name, &names->line_dwo))
12539 {
12540 dwo_sections->line.s.section = sectp;
12541 dwo_sections->line.size = bfd_section_size (sectp);
12542 }
12543 else if (section_is_p (sectp->name, &names->loc_dwo))
12544 {
12545 dwo_sections->loc.s.section = sectp;
12546 dwo_sections->loc.size = bfd_section_size (sectp);
12547 }
12548 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12549 {
12550 dwo_sections->macinfo.s.section = sectp;
12551 dwo_sections->macinfo.size = bfd_section_size (sectp);
12552 }
12553 else if (section_is_p (sectp->name, &names->macro_dwo))
12554 {
12555 dwo_sections->macro.s.section = sectp;
12556 dwo_sections->macro.size = bfd_section_size (sectp);
12557 }
12558 else if (section_is_p (sectp->name, &names->str_dwo))
12559 {
12560 dwo_sections->str.s.section = sectp;
12561 dwo_sections->str.size = bfd_section_size (sectp);
12562 }
12563 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12564 {
12565 dwo_sections->str_offsets.s.section = sectp;
12566 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12567 }
12568 else if (section_is_p (sectp->name, &names->types_dwo))
12569 {
12570 struct dwarf2_section_info type_section;
12571
12572 memset (&type_section, 0, sizeof (type_section));
12573 type_section.s.section = sectp;
12574 type_section.size = bfd_section_size (sectp);
12575 dwo_sections->types.push_back (type_section);
12576 }
12577 }
12578
12579 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12580 by PER_CU. This is for the non-DWP case.
12581 The result is NULL if DWO_NAME can't be found. */
12582
12583 static struct dwo_file *
12584 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12585 const char *dwo_name, const char *comp_dir)
12586 {
12587 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12588
12589 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12590 if (dbfd == NULL)
12591 {
12592 if (dwarf_read_debug)
12593 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12594 return NULL;
12595 }
12596
12597 dwo_file_up dwo_file (new struct dwo_file);
12598 dwo_file->dwo_name = dwo_name;
12599 dwo_file->comp_dir = comp_dir;
12600 dwo_file->dbfd = std::move (dbfd);
12601
12602 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12603 &dwo_file->sections);
12604
12605 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12606 dwo_file->sections.info, dwo_file->cus);
12607
12608 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12609 dwo_file->sections.types, dwo_file->tus);
12610
12611 if (dwarf_read_debug)
12612 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12613
12614 return dwo_file.release ();
12615 }
12616
12617 /* This function is mapped across the sections and remembers the offset and
12618 size of each of the DWP debugging sections common to version 1 and 2 that
12619 we are interested in. */
12620
12621 static void
12622 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12623 void *dwp_file_ptr)
12624 {
12625 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12626 const struct dwop_section_names *names = &dwop_section_names;
12627 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12628
12629 /* Record the ELF section number for later lookup: this is what the
12630 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12631 gdb_assert (elf_section_nr < dwp_file->num_sections);
12632 dwp_file->elf_sections[elf_section_nr] = sectp;
12633
12634 /* Look for specific sections that we need. */
12635 if (section_is_p (sectp->name, &names->str_dwo))
12636 {
12637 dwp_file->sections.str.s.section = sectp;
12638 dwp_file->sections.str.size = bfd_section_size (sectp);
12639 }
12640 else if (section_is_p (sectp->name, &names->cu_index))
12641 {
12642 dwp_file->sections.cu_index.s.section = sectp;
12643 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12644 }
12645 else if (section_is_p (sectp->name, &names->tu_index))
12646 {
12647 dwp_file->sections.tu_index.s.section = sectp;
12648 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12649 }
12650 }
12651
12652 /* This function is mapped across the sections and remembers the offset and
12653 size of each of the DWP version 2 debugging sections that we are interested
12654 in. This is split into a separate function because we don't know if we
12655 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12656
12657 static void
12658 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12659 {
12660 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12661 const struct dwop_section_names *names = &dwop_section_names;
12662 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12663
12664 /* Record the ELF section number for later lookup: this is what the
12665 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12666 gdb_assert (elf_section_nr < dwp_file->num_sections);
12667 dwp_file->elf_sections[elf_section_nr] = sectp;
12668
12669 /* Look for specific sections that we need. */
12670 if (section_is_p (sectp->name, &names->abbrev_dwo))
12671 {
12672 dwp_file->sections.abbrev.s.section = sectp;
12673 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12674 }
12675 else if (section_is_p (sectp->name, &names->info_dwo))
12676 {
12677 dwp_file->sections.info.s.section = sectp;
12678 dwp_file->sections.info.size = bfd_section_size (sectp);
12679 }
12680 else if (section_is_p (sectp->name, &names->line_dwo))
12681 {
12682 dwp_file->sections.line.s.section = sectp;
12683 dwp_file->sections.line.size = bfd_section_size (sectp);
12684 }
12685 else if (section_is_p (sectp->name, &names->loc_dwo))
12686 {
12687 dwp_file->sections.loc.s.section = sectp;
12688 dwp_file->sections.loc.size = bfd_section_size (sectp);
12689 }
12690 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12691 {
12692 dwp_file->sections.macinfo.s.section = sectp;
12693 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12694 }
12695 else if (section_is_p (sectp->name, &names->macro_dwo))
12696 {
12697 dwp_file->sections.macro.s.section = sectp;
12698 dwp_file->sections.macro.size = bfd_section_size (sectp);
12699 }
12700 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12701 {
12702 dwp_file->sections.str_offsets.s.section = sectp;
12703 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12704 }
12705 else if (section_is_p (sectp->name, &names->types_dwo))
12706 {
12707 dwp_file->sections.types.s.section = sectp;
12708 dwp_file->sections.types.size = bfd_section_size (sectp);
12709 }
12710 }
12711
12712 /* Hash function for dwp_file loaded CUs/TUs. */
12713
12714 static hashval_t
12715 hash_dwp_loaded_cutus (const void *item)
12716 {
12717 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12718
12719 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12720 return dwo_unit->signature;
12721 }
12722
12723 /* Equality function for dwp_file loaded CUs/TUs. */
12724
12725 static int
12726 eq_dwp_loaded_cutus (const void *a, const void *b)
12727 {
12728 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12729 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12730
12731 return dua->signature == dub->signature;
12732 }
12733
12734 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12735
12736 static htab_t
12737 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12738 {
12739 return htab_create_alloc_ex (3,
12740 hash_dwp_loaded_cutus,
12741 eq_dwp_loaded_cutus,
12742 NULL,
12743 &objfile->objfile_obstack,
12744 hashtab_obstack_allocate,
12745 dummy_obstack_deallocate);
12746 }
12747
12748 /* Try to open DWP file FILE_NAME.
12749 The result is the bfd handle of the file.
12750 If there is a problem finding or opening the file, return NULL.
12751 Upon success, the canonicalized path of the file is stored in the bfd,
12752 same as symfile_bfd_open. */
12753
12754 static gdb_bfd_ref_ptr
12755 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12756 const char *file_name)
12757 {
12758 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12759 1 /*is_dwp*/,
12760 1 /*search_cwd*/));
12761 if (abfd != NULL)
12762 return abfd;
12763
12764 /* Work around upstream bug 15652.
12765 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12766 [Whether that's a "bug" is debatable, but it is getting in our way.]
12767 We have no real idea where the dwp file is, because gdb's realpath-ing
12768 of the executable's path may have discarded the needed info.
12769 [IWBN if the dwp file name was recorded in the executable, akin to
12770 .gnu_debuglink, but that doesn't exist yet.]
12771 Strip the directory from FILE_NAME and search again. */
12772 if (*debug_file_directory != '\0')
12773 {
12774 /* Don't implicitly search the current directory here.
12775 If the user wants to search "." to handle this case,
12776 it must be added to debug-file-directory. */
12777 return try_open_dwop_file (dwarf2_per_objfile,
12778 lbasename (file_name), 1 /*is_dwp*/,
12779 0 /*search_cwd*/);
12780 }
12781
12782 return NULL;
12783 }
12784
12785 /* Initialize the use of the DWP file for the current objfile.
12786 By convention the name of the DWP file is ${objfile}.dwp.
12787 The result is NULL if it can't be found. */
12788
12789 static std::unique_ptr<struct dwp_file>
12790 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12791 {
12792 struct objfile *objfile = dwarf2_per_objfile->objfile;
12793
12794 /* Try to find first .dwp for the binary file before any symbolic links
12795 resolving. */
12796
12797 /* If the objfile is a debug file, find the name of the real binary
12798 file and get the name of dwp file from there. */
12799 std::string dwp_name;
12800 if (objfile->separate_debug_objfile_backlink != NULL)
12801 {
12802 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12803 const char *backlink_basename = lbasename (backlink->original_name);
12804
12805 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12806 }
12807 else
12808 dwp_name = objfile->original_name;
12809
12810 dwp_name += ".dwp";
12811
12812 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12813 if (dbfd == NULL
12814 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12815 {
12816 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12817 dwp_name = objfile_name (objfile);
12818 dwp_name += ".dwp";
12819 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12820 }
12821
12822 if (dbfd == NULL)
12823 {
12824 if (dwarf_read_debug)
12825 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12826 return std::unique_ptr<dwp_file> ();
12827 }
12828
12829 const char *name = bfd_get_filename (dbfd.get ());
12830 std::unique_ptr<struct dwp_file> dwp_file
12831 (new struct dwp_file (name, std::move (dbfd)));
12832
12833 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12834 dwp_file->elf_sections =
12835 OBSTACK_CALLOC (&objfile->objfile_obstack,
12836 dwp_file->num_sections, asection *);
12837
12838 bfd_map_over_sections (dwp_file->dbfd.get (),
12839 dwarf2_locate_common_dwp_sections,
12840 dwp_file.get ());
12841
12842 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12843 0);
12844
12845 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12846 1);
12847
12848 /* The DWP file version is stored in the hash table. Oh well. */
12849 if (dwp_file->cus && dwp_file->tus
12850 && dwp_file->cus->version != dwp_file->tus->version)
12851 {
12852 /* Technically speaking, we should try to limp along, but this is
12853 pretty bizarre. We use pulongest here because that's the established
12854 portability solution (e.g, we cannot use %u for uint32_t). */
12855 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12856 " TU version %s [in DWP file %s]"),
12857 pulongest (dwp_file->cus->version),
12858 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12859 }
12860
12861 if (dwp_file->cus)
12862 dwp_file->version = dwp_file->cus->version;
12863 else if (dwp_file->tus)
12864 dwp_file->version = dwp_file->tus->version;
12865 else
12866 dwp_file->version = 2;
12867
12868 if (dwp_file->version == 2)
12869 bfd_map_over_sections (dwp_file->dbfd.get (),
12870 dwarf2_locate_v2_dwp_sections,
12871 dwp_file.get ());
12872
12873 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12874 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12875
12876 if (dwarf_read_debug)
12877 {
12878 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12879 fprintf_unfiltered (gdb_stdlog,
12880 " %s CUs, %s TUs\n",
12881 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12882 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12883 }
12884
12885 return dwp_file;
12886 }
12887
12888 /* Wrapper around open_and_init_dwp_file, only open it once. */
12889
12890 static struct dwp_file *
12891 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12892 {
12893 if (! dwarf2_per_objfile->dwp_checked)
12894 {
12895 dwarf2_per_objfile->dwp_file
12896 = open_and_init_dwp_file (dwarf2_per_objfile);
12897 dwarf2_per_objfile->dwp_checked = 1;
12898 }
12899 return dwarf2_per_objfile->dwp_file.get ();
12900 }
12901
12902 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12903 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12904 or in the DWP file for the objfile, referenced by THIS_UNIT.
12905 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12906 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12907
12908 This is called, for example, when wanting to read a variable with a
12909 complex location. Therefore we don't want to do file i/o for every call.
12910 Therefore we don't want to look for a DWO file on every call.
12911 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12912 then we check if we've already seen DWO_NAME, and only THEN do we check
12913 for a DWO file.
12914
12915 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12916 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12917
12918 static struct dwo_unit *
12919 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12920 const char *dwo_name, const char *comp_dir,
12921 ULONGEST signature, int is_debug_types)
12922 {
12923 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12924 struct objfile *objfile = dwarf2_per_objfile->objfile;
12925 const char *kind = is_debug_types ? "TU" : "CU";
12926 void **dwo_file_slot;
12927 struct dwo_file *dwo_file;
12928 struct dwp_file *dwp_file;
12929
12930 /* First see if there's a DWP file.
12931 If we have a DWP file but didn't find the DWO inside it, don't
12932 look for the original DWO file. It makes gdb behave differently
12933 depending on whether one is debugging in the build tree. */
12934
12935 dwp_file = get_dwp_file (dwarf2_per_objfile);
12936 if (dwp_file != NULL)
12937 {
12938 const struct dwp_hash_table *dwp_htab =
12939 is_debug_types ? dwp_file->tus : dwp_file->cus;
12940
12941 if (dwp_htab != NULL)
12942 {
12943 struct dwo_unit *dwo_cutu =
12944 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12945 signature, is_debug_types);
12946
12947 if (dwo_cutu != NULL)
12948 {
12949 if (dwarf_read_debug)
12950 {
12951 fprintf_unfiltered (gdb_stdlog,
12952 "Virtual DWO %s %s found: @%s\n",
12953 kind, hex_string (signature),
12954 host_address_to_string (dwo_cutu));
12955 }
12956 return dwo_cutu;
12957 }
12958 }
12959 }
12960 else
12961 {
12962 /* No DWP file, look for the DWO file. */
12963
12964 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12965 dwo_name, comp_dir);
12966 if (*dwo_file_slot == NULL)
12967 {
12968 /* Read in the file and build a table of the CUs/TUs it contains. */
12969 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12970 }
12971 /* NOTE: This will be NULL if unable to open the file. */
12972 dwo_file = (struct dwo_file *) *dwo_file_slot;
12973
12974 if (dwo_file != NULL)
12975 {
12976 struct dwo_unit *dwo_cutu = NULL;
12977
12978 if (is_debug_types && dwo_file->tus)
12979 {
12980 struct dwo_unit find_dwo_cutu;
12981
12982 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12983 find_dwo_cutu.signature = signature;
12984 dwo_cutu
12985 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12986 &find_dwo_cutu);
12987 }
12988 else if (!is_debug_types && dwo_file->cus)
12989 {
12990 struct dwo_unit find_dwo_cutu;
12991
12992 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12993 find_dwo_cutu.signature = signature;
12994 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12995 &find_dwo_cutu);
12996 }
12997
12998 if (dwo_cutu != NULL)
12999 {
13000 if (dwarf_read_debug)
13001 {
13002 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13003 kind, dwo_name, hex_string (signature),
13004 host_address_to_string (dwo_cutu));
13005 }
13006 return dwo_cutu;
13007 }
13008 }
13009 }
13010
13011 /* We didn't find it. This could mean a dwo_id mismatch, or
13012 someone deleted the DWO/DWP file, or the search path isn't set up
13013 correctly to find the file. */
13014
13015 if (dwarf_read_debug)
13016 {
13017 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13018 kind, dwo_name, hex_string (signature));
13019 }
13020
13021 /* This is a warning and not a complaint because it can be caused by
13022 pilot error (e.g., user accidentally deleting the DWO). */
13023 {
13024 /* Print the name of the DWP file if we looked there, helps the user
13025 better diagnose the problem. */
13026 std::string dwp_text;
13027
13028 if (dwp_file != NULL)
13029 dwp_text = string_printf (" [in DWP file %s]",
13030 lbasename (dwp_file->name));
13031
13032 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13033 " [in module %s]"),
13034 kind, dwo_name, hex_string (signature),
13035 dwp_text.c_str (),
13036 this_unit->is_debug_types ? "TU" : "CU",
13037 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13038 }
13039 return NULL;
13040 }
13041
13042 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13043 See lookup_dwo_cutu_unit for details. */
13044
13045 static struct dwo_unit *
13046 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13047 const char *dwo_name, const char *comp_dir,
13048 ULONGEST signature)
13049 {
13050 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13051 }
13052
13053 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13054 See lookup_dwo_cutu_unit for details. */
13055
13056 static struct dwo_unit *
13057 lookup_dwo_type_unit (struct signatured_type *this_tu,
13058 const char *dwo_name, const char *comp_dir)
13059 {
13060 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13061 }
13062
13063 /* Traversal function for queue_and_load_all_dwo_tus. */
13064
13065 static int
13066 queue_and_load_dwo_tu (void **slot, void *info)
13067 {
13068 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13069 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13070 ULONGEST signature = dwo_unit->signature;
13071 struct signatured_type *sig_type =
13072 lookup_dwo_signatured_type (per_cu->cu, signature);
13073
13074 if (sig_type != NULL)
13075 {
13076 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13077
13078 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13079 a real dependency of PER_CU on SIG_TYPE. That is detected later
13080 while processing PER_CU. */
13081 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13082 load_full_type_unit (sig_cu);
13083 per_cu->imported_symtabs_push (sig_cu);
13084 }
13085
13086 return 1;
13087 }
13088
13089 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13090 The DWO may have the only definition of the type, though it may not be
13091 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13092 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13093
13094 static void
13095 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13096 {
13097 struct dwo_unit *dwo_unit;
13098 struct dwo_file *dwo_file;
13099
13100 gdb_assert (!per_cu->is_debug_types);
13101 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13102 gdb_assert (per_cu->cu != NULL);
13103
13104 dwo_unit = per_cu->cu->dwo_unit;
13105 gdb_assert (dwo_unit != NULL);
13106
13107 dwo_file = dwo_unit->dwo_file;
13108 if (dwo_file->tus != NULL)
13109 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
13110 per_cu);
13111 }
13112
13113 /* Read in various DIEs. */
13114
13115 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13116 Inherit only the children of the DW_AT_abstract_origin DIE not being
13117 already referenced by DW_AT_abstract_origin from the children of the
13118 current DIE. */
13119
13120 static void
13121 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13122 {
13123 struct die_info *child_die;
13124 sect_offset *offsetp;
13125 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13126 struct die_info *origin_die;
13127 /* Iterator of the ORIGIN_DIE children. */
13128 struct die_info *origin_child_die;
13129 struct attribute *attr;
13130 struct dwarf2_cu *origin_cu;
13131 struct pending **origin_previous_list_in_scope;
13132
13133 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13134 if (!attr)
13135 return;
13136
13137 /* Note that following die references may follow to a die in a
13138 different cu. */
13139
13140 origin_cu = cu;
13141 origin_die = follow_die_ref (die, attr, &origin_cu);
13142
13143 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13144 symbols in. */
13145 origin_previous_list_in_scope = origin_cu->list_in_scope;
13146 origin_cu->list_in_scope = cu->list_in_scope;
13147
13148 if (die->tag != origin_die->tag
13149 && !(die->tag == DW_TAG_inlined_subroutine
13150 && origin_die->tag == DW_TAG_subprogram))
13151 complaint (_("DIE %s and its abstract origin %s have different tags"),
13152 sect_offset_str (die->sect_off),
13153 sect_offset_str (origin_die->sect_off));
13154
13155 std::vector<sect_offset> offsets;
13156
13157 for (child_die = die->child;
13158 child_die && child_die->tag;
13159 child_die = sibling_die (child_die))
13160 {
13161 struct die_info *child_origin_die;
13162 struct dwarf2_cu *child_origin_cu;
13163
13164 /* We are trying to process concrete instance entries:
13165 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13166 it's not relevant to our analysis here. i.e. detecting DIEs that are
13167 present in the abstract instance but not referenced in the concrete
13168 one. */
13169 if (child_die->tag == DW_TAG_call_site
13170 || child_die->tag == DW_TAG_GNU_call_site)
13171 continue;
13172
13173 /* For each CHILD_DIE, find the corresponding child of
13174 ORIGIN_DIE. If there is more than one layer of
13175 DW_AT_abstract_origin, follow them all; there shouldn't be,
13176 but GCC versions at least through 4.4 generate this (GCC PR
13177 40573). */
13178 child_origin_die = child_die;
13179 child_origin_cu = cu;
13180 while (1)
13181 {
13182 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13183 child_origin_cu);
13184 if (attr == NULL)
13185 break;
13186 child_origin_die = follow_die_ref (child_origin_die, attr,
13187 &child_origin_cu);
13188 }
13189
13190 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13191 counterpart may exist. */
13192 if (child_origin_die != child_die)
13193 {
13194 if (child_die->tag != child_origin_die->tag
13195 && !(child_die->tag == DW_TAG_inlined_subroutine
13196 && child_origin_die->tag == DW_TAG_subprogram))
13197 complaint (_("Child DIE %s and its abstract origin %s have "
13198 "different tags"),
13199 sect_offset_str (child_die->sect_off),
13200 sect_offset_str (child_origin_die->sect_off));
13201 if (child_origin_die->parent != origin_die)
13202 complaint (_("Child DIE %s and its abstract origin %s have "
13203 "different parents"),
13204 sect_offset_str (child_die->sect_off),
13205 sect_offset_str (child_origin_die->sect_off));
13206 else
13207 offsets.push_back (child_origin_die->sect_off);
13208 }
13209 }
13210 std::sort (offsets.begin (), offsets.end ());
13211 sect_offset *offsets_end = offsets.data () + offsets.size ();
13212 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13213 if (offsetp[-1] == *offsetp)
13214 complaint (_("Multiple children of DIE %s refer "
13215 "to DIE %s as their abstract origin"),
13216 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13217
13218 offsetp = offsets.data ();
13219 origin_child_die = origin_die->child;
13220 while (origin_child_die && origin_child_die->tag)
13221 {
13222 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13223 while (offsetp < offsets_end
13224 && *offsetp < origin_child_die->sect_off)
13225 offsetp++;
13226 if (offsetp >= offsets_end
13227 || *offsetp > origin_child_die->sect_off)
13228 {
13229 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13230 Check whether we're already processing ORIGIN_CHILD_DIE.
13231 This can happen with mutually referenced abstract_origins.
13232 PR 16581. */
13233 if (!origin_child_die->in_process)
13234 process_die (origin_child_die, origin_cu);
13235 }
13236 origin_child_die = sibling_die (origin_child_die);
13237 }
13238 origin_cu->list_in_scope = origin_previous_list_in_scope;
13239
13240 if (cu != origin_cu)
13241 compute_delayed_physnames (origin_cu);
13242 }
13243
13244 static void
13245 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13246 {
13247 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13248 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13249 struct context_stack *newobj;
13250 CORE_ADDR lowpc;
13251 CORE_ADDR highpc;
13252 struct die_info *child_die;
13253 struct attribute *attr, *call_line, *call_file;
13254 const char *name;
13255 CORE_ADDR baseaddr;
13256 struct block *block;
13257 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13258 std::vector<struct symbol *> template_args;
13259 struct template_symbol *templ_func = NULL;
13260
13261 if (inlined_func)
13262 {
13263 /* If we do not have call site information, we can't show the
13264 caller of this inlined function. That's too confusing, so
13265 only use the scope for local variables. */
13266 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13267 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13268 if (call_line == NULL || call_file == NULL)
13269 {
13270 read_lexical_block_scope (die, cu);
13271 return;
13272 }
13273 }
13274
13275 baseaddr = objfile->text_section_offset ();
13276
13277 name = dwarf2_name (die, cu);
13278
13279 /* Ignore functions with missing or empty names. These are actually
13280 illegal according to the DWARF standard. */
13281 if (name == NULL)
13282 {
13283 complaint (_("missing name for subprogram DIE at %s"),
13284 sect_offset_str (die->sect_off));
13285 return;
13286 }
13287
13288 /* Ignore functions with missing or invalid low and high pc attributes. */
13289 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13290 <= PC_BOUNDS_INVALID)
13291 {
13292 attr = dwarf2_attr (die, DW_AT_external, cu);
13293 if (!attr || !DW_UNSND (attr))
13294 complaint (_("cannot get low and high bounds "
13295 "for subprogram DIE at %s"),
13296 sect_offset_str (die->sect_off));
13297 return;
13298 }
13299
13300 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13301 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13302
13303 /* If we have any template arguments, then we must allocate a
13304 different sort of symbol. */
13305 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13306 {
13307 if (child_die->tag == DW_TAG_template_type_param
13308 || child_die->tag == DW_TAG_template_value_param)
13309 {
13310 templ_func = allocate_template_symbol (objfile);
13311 templ_func->subclass = SYMBOL_TEMPLATE;
13312 break;
13313 }
13314 }
13315
13316 newobj = cu->get_builder ()->push_context (0, lowpc);
13317 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13318 (struct symbol *) templ_func);
13319
13320 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13321 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13322 cu->language);
13323
13324 /* If there is a location expression for DW_AT_frame_base, record
13325 it. */
13326 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13327 if (attr != nullptr)
13328 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13329
13330 /* If there is a location for the static link, record it. */
13331 newobj->static_link = NULL;
13332 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13333 if (attr != nullptr)
13334 {
13335 newobj->static_link
13336 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13337 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13338 dwarf2_per_cu_addr_type (cu->per_cu));
13339 }
13340
13341 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13342
13343 if (die->child != NULL)
13344 {
13345 child_die = die->child;
13346 while (child_die && child_die->tag)
13347 {
13348 if (child_die->tag == DW_TAG_template_type_param
13349 || child_die->tag == DW_TAG_template_value_param)
13350 {
13351 struct symbol *arg = new_symbol (child_die, NULL, cu);
13352
13353 if (arg != NULL)
13354 template_args.push_back (arg);
13355 }
13356 else
13357 process_die (child_die, cu);
13358 child_die = sibling_die (child_die);
13359 }
13360 }
13361
13362 inherit_abstract_dies (die, cu);
13363
13364 /* If we have a DW_AT_specification, we might need to import using
13365 directives from the context of the specification DIE. See the
13366 comment in determine_prefix. */
13367 if (cu->language == language_cplus
13368 && dwarf2_attr (die, DW_AT_specification, cu))
13369 {
13370 struct dwarf2_cu *spec_cu = cu;
13371 struct die_info *spec_die = die_specification (die, &spec_cu);
13372
13373 while (spec_die)
13374 {
13375 child_die = spec_die->child;
13376 while (child_die && child_die->tag)
13377 {
13378 if (child_die->tag == DW_TAG_imported_module)
13379 process_die (child_die, spec_cu);
13380 child_die = sibling_die (child_die);
13381 }
13382
13383 /* In some cases, GCC generates specification DIEs that
13384 themselves contain DW_AT_specification attributes. */
13385 spec_die = die_specification (spec_die, &spec_cu);
13386 }
13387 }
13388
13389 struct context_stack cstk = cu->get_builder ()->pop_context ();
13390 /* Make a block for the local symbols within. */
13391 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13392 cstk.static_link, lowpc, highpc);
13393
13394 /* For C++, set the block's scope. */
13395 if ((cu->language == language_cplus
13396 || cu->language == language_fortran
13397 || cu->language == language_d
13398 || cu->language == language_rust)
13399 && cu->processing_has_namespace_info)
13400 block_set_scope (block, determine_prefix (die, cu),
13401 &objfile->objfile_obstack);
13402
13403 /* If we have address ranges, record them. */
13404 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13405
13406 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13407
13408 /* Attach template arguments to function. */
13409 if (!template_args.empty ())
13410 {
13411 gdb_assert (templ_func != NULL);
13412
13413 templ_func->n_template_arguments = template_args.size ();
13414 templ_func->template_arguments
13415 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13416 templ_func->n_template_arguments);
13417 memcpy (templ_func->template_arguments,
13418 template_args.data (),
13419 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13420
13421 /* Make sure that the symtab is set on the new symbols. Even
13422 though they don't appear in this symtab directly, other parts
13423 of gdb assume that symbols do, and this is reasonably
13424 true. */
13425 for (symbol *sym : template_args)
13426 symbol_set_symtab (sym, symbol_symtab (templ_func));
13427 }
13428
13429 /* In C++, we can have functions nested inside functions (e.g., when
13430 a function declares a class that has methods). This means that
13431 when we finish processing a function scope, we may need to go
13432 back to building a containing block's symbol lists. */
13433 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13434 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13435
13436 /* If we've finished processing a top-level function, subsequent
13437 symbols go in the file symbol list. */
13438 if (cu->get_builder ()->outermost_context_p ())
13439 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13440 }
13441
13442 /* Process all the DIES contained within a lexical block scope. Start
13443 a new scope, process the dies, and then close the scope. */
13444
13445 static void
13446 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13447 {
13448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13449 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13450 CORE_ADDR lowpc, highpc;
13451 struct die_info *child_die;
13452 CORE_ADDR baseaddr;
13453
13454 baseaddr = objfile->text_section_offset ();
13455
13456 /* Ignore blocks with missing or invalid low and high pc attributes. */
13457 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13458 as multiple lexical blocks? Handling children in a sane way would
13459 be nasty. Might be easier to properly extend generic blocks to
13460 describe ranges. */
13461 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13462 {
13463 case PC_BOUNDS_NOT_PRESENT:
13464 /* DW_TAG_lexical_block has no attributes, process its children as if
13465 there was no wrapping by that DW_TAG_lexical_block.
13466 GCC does no longer produces such DWARF since GCC r224161. */
13467 for (child_die = die->child;
13468 child_die != NULL && child_die->tag;
13469 child_die = sibling_die (child_die))
13470 process_die (child_die, cu);
13471 return;
13472 case PC_BOUNDS_INVALID:
13473 return;
13474 }
13475 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13476 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13477
13478 cu->get_builder ()->push_context (0, lowpc);
13479 if (die->child != NULL)
13480 {
13481 child_die = die->child;
13482 while (child_die && child_die->tag)
13483 {
13484 process_die (child_die, cu);
13485 child_die = sibling_die (child_die);
13486 }
13487 }
13488 inherit_abstract_dies (die, cu);
13489 struct context_stack cstk = cu->get_builder ()->pop_context ();
13490
13491 if (*cu->get_builder ()->get_local_symbols () != NULL
13492 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13493 {
13494 struct block *block
13495 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13496 cstk.start_addr, highpc);
13497
13498 /* Note that recording ranges after traversing children, as we
13499 do here, means that recording a parent's ranges entails
13500 walking across all its children's ranges as they appear in
13501 the address map, which is quadratic behavior.
13502
13503 It would be nicer to record the parent's ranges before
13504 traversing its children, simply overriding whatever you find
13505 there. But since we don't even decide whether to create a
13506 block until after we've traversed its children, that's hard
13507 to do. */
13508 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13509 }
13510 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13511 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13512 }
13513
13514 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13515
13516 static void
13517 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13518 {
13519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13521 CORE_ADDR pc, baseaddr;
13522 struct attribute *attr;
13523 struct call_site *call_site, call_site_local;
13524 void **slot;
13525 int nparams;
13526 struct die_info *child_die;
13527
13528 baseaddr = objfile->text_section_offset ();
13529
13530 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13531 if (attr == NULL)
13532 {
13533 /* This was a pre-DWARF-5 GNU extension alias
13534 for DW_AT_call_return_pc. */
13535 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13536 }
13537 if (!attr)
13538 {
13539 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13540 "DIE %s [in module %s]"),
13541 sect_offset_str (die->sect_off), objfile_name (objfile));
13542 return;
13543 }
13544 pc = attr->value_as_address () + baseaddr;
13545 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13546
13547 if (cu->call_site_htab == NULL)
13548 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13549 NULL, &objfile->objfile_obstack,
13550 hashtab_obstack_allocate, NULL);
13551 call_site_local.pc = pc;
13552 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13553 if (*slot != NULL)
13554 {
13555 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13556 "DIE %s [in module %s]"),
13557 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13558 objfile_name (objfile));
13559 return;
13560 }
13561
13562 /* Count parameters at the caller. */
13563
13564 nparams = 0;
13565 for (child_die = die->child; child_die && child_die->tag;
13566 child_die = sibling_die (child_die))
13567 {
13568 if (child_die->tag != DW_TAG_call_site_parameter
13569 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13570 {
13571 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13572 "DW_TAG_call_site child DIE %s [in module %s]"),
13573 child_die->tag, sect_offset_str (child_die->sect_off),
13574 objfile_name (objfile));
13575 continue;
13576 }
13577
13578 nparams++;
13579 }
13580
13581 call_site
13582 = ((struct call_site *)
13583 obstack_alloc (&objfile->objfile_obstack,
13584 sizeof (*call_site)
13585 + (sizeof (*call_site->parameter) * (nparams - 1))));
13586 *slot = call_site;
13587 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13588 call_site->pc = pc;
13589
13590 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13591 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13592 {
13593 struct die_info *func_die;
13594
13595 /* Skip also over DW_TAG_inlined_subroutine. */
13596 for (func_die = die->parent;
13597 func_die && func_die->tag != DW_TAG_subprogram
13598 && func_die->tag != DW_TAG_subroutine_type;
13599 func_die = func_die->parent);
13600
13601 /* DW_AT_call_all_calls is a superset
13602 of DW_AT_call_all_tail_calls. */
13603 if (func_die
13604 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13605 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13606 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13607 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13608 {
13609 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13610 not complete. But keep CALL_SITE for look ups via call_site_htab,
13611 both the initial caller containing the real return address PC and
13612 the final callee containing the current PC of a chain of tail
13613 calls do not need to have the tail call list complete. But any
13614 function candidate for a virtual tail call frame searched via
13615 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13616 determined unambiguously. */
13617 }
13618 else
13619 {
13620 struct type *func_type = NULL;
13621
13622 if (func_die)
13623 func_type = get_die_type (func_die, cu);
13624 if (func_type != NULL)
13625 {
13626 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13627
13628 /* Enlist this call site to the function. */
13629 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13630 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13631 }
13632 else
13633 complaint (_("Cannot find function owning DW_TAG_call_site "
13634 "DIE %s [in module %s]"),
13635 sect_offset_str (die->sect_off), objfile_name (objfile));
13636 }
13637 }
13638
13639 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13640 if (attr == NULL)
13641 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13642 if (attr == NULL)
13643 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13644 if (attr == NULL)
13645 {
13646 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13647 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13648 }
13649 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13650 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13651 /* Keep NULL DWARF_BLOCK. */;
13652 else if (attr->form_is_block ())
13653 {
13654 struct dwarf2_locexpr_baton *dlbaton;
13655
13656 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13657 dlbaton->data = DW_BLOCK (attr)->data;
13658 dlbaton->size = DW_BLOCK (attr)->size;
13659 dlbaton->per_cu = cu->per_cu;
13660
13661 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13662 }
13663 else if (attr->form_is_ref ())
13664 {
13665 struct dwarf2_cu *target_cu = cu;
13666 struct die_info *target_die;
13667
13668 target_die = follow_die_ref (die, attr, &target_cu);
13669 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13670 if (die_is_declaration (target_die, target_cu))
13671 {
13672 const char *target_physname;
13673
13674 /* Prefer the mangled name; otherwise compute the demangled one. */
13675 target_physname = dw2_linkage_name (target_die, target_cu);
13676 if (target_physname == NULL)
13677 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13678 if (target_physname == NULL)
13679 complaint (_("DW_AT_call_target target DIE has invalid "
13680 "physname, for referencing DIE %s [in module %s]"),
13681 sect_offset_str (die->sect_off), objfile_name (objfile));
13682 else
13683 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13684 }
13685 else
13686 {
13687 CORE_ADDR lowpc;
13688
13689 /* DW_AT_entry_pc should be preferred. */
13690 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13691 <= PC_BOUNDS_INVALID)
13692 complaint (_("DW_AT_call_target target DIE has invalid "
13693 "low pc, for referencing DIE %s [in module %s]"),
13694 sect_offset_str (die->sect_off), objfile_name (objfile));
13695 else
13696 {
13697 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13698 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13699 }
13700 }
13701 }
13702 else
13703 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13704 "block nor reference, for DIE %s [in module %s]"),
13705 sect_offset_str (die->sect_off), objfile_name (objfile));
13706
13707 call_site->per_cu = cu->per_cu;
13708
13709 for (child_die = die->child;
13710 child_die && child_die->tag;
13711 child_die = sibling_die (child_die))
13712 {
13713 struct call_site_parameter *parameter;
13714 struct attribute *loc, *origin;
13715
13716 if (child_die->tag != DW_TAG_call_site_parameter
13717 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13718 {
13719 /* Already printed the complaint above. */
13720 continue;
13721 }
13722
13723 gdb_assert (call_site->parameter_count < nparams);
13724 parameter = &call_site->parameter[call_site->parameter_count];
13725
13726 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13727 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13728 register is contained in DW_AT_call_value. */
13729
13730 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13731 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13732 if (origin == NULL)
13733 {
13734 /* This was a pre-DWARF-5 GNU extension alias
13735 for DW_AT_call_parameter. */
13736 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13737 }
13738 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13739 {
13740 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13741
13742 sect_offset sect_off
13743 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13744 if (!offset_in_cu_p (&cu->header, sect_off))
13745 {
13746 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13747 binding can be done only inside one CU. Such referenced DIE
13748 therefore cannot be even moved to DW_TAG_partial_unit. */
13749 complaint (_("DW_AT_call_parameter offset is not in CU for "
13750 "DW_TAG_call_site child DIE %s [in module %s]"),
13751 sect_offset_str (child_die->sect_off),
13752 objfile_name (objfile));
13753 continue;
13754 }
13755 parameter->u.param_cu_off
13756 = (cu_offset) (sect_off - cu->header.sect_off);
13757 }
13758 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13759 {
13760 complaint (_("No DW_FORM_block* DW_AT_location for "
13761 "DW_TAG_call_site child DIE %s [in module %s]"),
13762 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13763 continue;
13764 }
13765 else
13766 {
13767 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13768 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13769 if (parameter->u.dwarf_reg != -1)
13770 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13771 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13772 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13773 &parameter->u.fb_offset))
13774 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13775 else
13776 {
13777 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13778 "for DW_FORM_block* DW_AT_location is supported for "
13779 "DW_TAG_call_site child DIE %s "
13780 "[in module %s]"),
13781 sect_offset_str (child_die->sect_off),
13782 objfile_name (objfile));
13783 continue;
13784 }
13785 }
13786
13787 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13788 if (attr == NULL)
13789 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13790 if (attr == NULL || !attr->form_is_block ())
13791 {
13792 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13793 "DW_TAG_call_site child DIE %s [in module %s]"),
13794 sect_offset_str (child_die->sect_off),
13795 objfile_name (objfile));
13796 continue;
13797 }
13798 parameter->value = DW_BLOCK (attr)->data;
13799 parameter->value_size = DW_BLOCK (attr)->size;
13800
13801 /* Parameters are not pre-cleared by memset above. */
13802 parameter->data_value = NULL;
13803 parameter->data_value_size = 0;
13804 call_site->parameter_count++;
13805
13806 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13807 if (attr == NULL)
13808 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13809 if (attr != nullptr)
13810 {
13811 if (!attr->form_is_block ())
13812 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13813 "DW_TAG_call_site child DIE %s [in module %s]"),
13814 sect_offset_str (child_die->sect_off),
13815 objfile_name (objfile));
13816 else
13817 {
13818 parameter->data_value = DW_BLOCK (attr)->data;
13819 parameter->data_value_size = DW_BLOCK (attr)->size;
13820 }
13821 }
13822 }
13823 }
13824
13825 /* Helper function for read_variable. If DIE represents a virtual
13826 table, then return the type of the concrete object that is
13827 associated with the virtual table. Otherwise, return NULL. */
13828
13829 static struct type *
13830 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13831 {
13832 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13833 if (attr == NULL)
13834 return NULL;
13835
13836 /* Find the type DIE. */
13837 struct die_info *type_die = NULL;
13838 struct dwarf2_cu *type_cu = cu;
13839
13840 if (attr->form_is_ref ())
13841 type_die = follow_die_ref (die, attr, &type_cu);
13842 if (type_die == NULL)
13843 return NULL;
13844
13845 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13846 return NULL;
13847 return die_containing_type (type_die, type_cu);
13848 }
13849
13850 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13851
13852 static void
13853 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13854 {
13855 struct rust_vtable_symbol *storage = NULL;
13856
13857 if (cu->language == language_rust)
13858 {
13859 struct type *containing_type = rust_containing_type (die, cu);
13860
13861 if (containing_type != NULL)
13862 {
13863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13864
13865 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13866 initialize_objfile_symbol (storage);
13867 storage->concrete_type = containing_type;
13868 storage->subclass = SYMBOL_RUST_VTABLE;
13869 }
13870 }
13871
13872 struct symbol *res = new_symbol (die, NULL, cu, storage);
13873 struct attribute *abstract_origin
13874 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13875 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13876 if (res == NULL && loc && abstract_origin)
13877 {
13878 /* We have a variable without a name, but with a location and an abstract
13879 origin. This may be a concrete instance of an abstract variable
13880 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13881 later. */
13882 struct dwarf2_cu *origin_cu = cu;
13883 struct die_info *origin_die
13884 = follow_die_ref (die, abstract_origin, &origin_cu);
13885 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13886 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13887 }
13888 }
13889
13890 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13891 reading .debug_rnglists.
13892 Callback's type should be:
13893 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13894 Return true if the attributes are present and valid, otherwise,
13895 return false. */
13896
13897 template <typename Callback>
13898 static bool
13899 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13900 Callback &&callback)
13901 {
13902 struct dwarf2_per_objfile *dwarf2_per_objfile
13903 = cu->per_cu->dwarf2_per_objfile;
13904 struct objfile *objfile = dwarf2_per_objfile->objfile;
13905 bfd *obfd = objfile->obfd;
13906 /* Base address selection entry. */
13907 CORE_ADDR base;
13908 int found_base;
13909 const gdb_byte *buffer;
13910 CORE_ADDR baseaddr;
13911 bool overflow = false;
13912
13913 found_base = cu->base_known;
13914 base = cu->base_address;
13915
13916 dwarf2_per_objfile->rnglists.read (objfile);
13917 if (offset >= dwarf2_per_objfile->rnglists.size)
13918 {
13919 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13920 offset);
13921 return false;
13922 }
13923 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13924
13925 baseaddr = objfile->text_section_offset ();
13926
13927 while (1)
13928 {
13929 /* Initialize it due to a false compiler warning. */
13930 CORE_ADDR range_beginning = 0, range_end = 0;
13931 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13932 + dwarf2_per_objfile->rnglists.size);
13933 unsigned int bytes_read;
13934
13935 if (buffer == buf_end)
13936 {
13937 overflow = true;
13938 break;
13939 }
13940 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13941 switch (rlet)
13942 {
13943 case DW_RLE_end_of_list:
13944 break;
13945 case DW_RLE_base_address:
13946 if (buffer + cu->header.addr_size > buf_end)
13947 {
13948 overflow = true;
13949 break;
13950 }
13951 base = read_address (obfd, buffer, cu, &bytes_read);
13952 found_base = 1;
13953 buffer += bytes_read;
13954 break;
13955 case DW_RLE_start_length:
13956 if (buffer + cu->header.addr_size > buf_end)
13957 {
13958 overflow = true;
13959 break;
13960 }
13961 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13962 buffer += bytes_read;
13963 range_end = (range_beginning
13964 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13965 buffer += bytes_read;
13966 if (buffer > buf_end)
13967 {
13968 overflow = true;
13969 break;
13970 }
13971 break;
13972 case DW_RLE_offset_pair:
13973 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13974 buffer += bytes_read;
13975 if (buffer > buf_end)
13976 {
13977 overflow = true;
13978 break;
13979 }
13980 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13981 buffer += bytes_read;
13982 if (buffer > buf_end)
13983 {
13984 overflow = true;
13985 break;
13986 }
13987 break;
13988 case DW_RLE_start_end:
13989 if (buffer + 2 * cu->header.addr_size > buf_end)
13990 {
13991 overflow = true;
13992 break;
13993 }
13994 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13995 buffer += bytes_read;
13996 range_end = read_address (obfd, buffer, cu, &bytes_read);
13997 buffer += bytes_read;
13998 break;
13999 default:
14000 complaint (_("Invalid .debug_rnglists data (no base address)"));
14001 return false;
14002 }
14003 if (rlet == DW_RLE_end_of_list || overflow)
14004 break;
14005 if (rlet == DW_RLE_base_address)
14006 continue;
14007
14008 if (!found_base)
14009 {
14010 /* We have no valid base address for the ranges
14011 data. */
14012 complaint (_("Invalid .debug_rnglists data (no base address)"));
14013 return false;
14014 }
14015
14016 if (range_beginning > range_end)
14017 {
14018 /* Inverted range entries are invalid. */
14019 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14020 return false;
14021 }
14022
14023 /* Empty range entries have no effect. */
14024 if (range_beginning == range_end)
14025 continue;
14026
14027 range_beginning += base;
14028 range_end += base;
14029
14030 /* A not-uncommon case of bad debug info.
14031 Don't pollute the addrmap with bad data. */
14032 if (range_beginning + baseaddr == 0
14033 && !dwarf2_per_objfile->has_section_at_zero)
14034 {
14035 complaint (_(".debug_rnglists entry has start address of zero"
14036 " [in module %s]"), objfile_name (objfile));
14037 continue;
14038 }
14039
14040 callback (range_beginning, range_end);
14041 }
14042
14043 if (overflow)
14044 {
14045 complaint (_("Offset %d is not terminated "
14046 "for DW_AT_ranges attribute"),
14047 offset);
14048 return false;
14049 }
14050
14051 return true;
14052 }
14053
14054 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14055 Callback's type should be:
14056 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14057 Return 1 if the attributes are present and valid, otherwise, return 0. */
14058
14059 template <typename Callback>
14060 static int
14061 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14062 Callback &&callback)
14063 {
14064 struct dwarf2_per_objfile *dwarf2_per_objfile
14065 = cu->per_cu->dwarf2_per_objfile;
14066 struct objfile *objfile = dwarf2_per_objfile->objfile;
14067 struct comp_unit_head *cu_header = &cu->header;
14068 bfd *obfd = objfile->obfd;
14069 unsigned int addr_size = cu_header->addr_size;
14070 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14071 /* Base address selection entry. */
14072 CORE_ADDR base;
14073 int found_base;
14074 unsigned int dummy;
14075 const gdb_byte *buffer;
14076 CORE_ADDR baseaddr;
14077
14078 if (cu_header->version >= 5)
14079 return dwarf2_rnglists_process (offset, cu, callback);
14080
14081 found_base = cu->base_known;
14082 base = cu->base_address;
14083
14084 dwarf2_per_objfile->ranges.read (objfile);
14085 if (offset >= dwarf2_per_objfile->ranges.size)
14086 {
14087 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14088 offset);
14089 return 0;
14090 }
14091 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14092
14093 baseaddr = objfile->text_section_offset ();
14094
14095 while (1)
14096 {
14097 CORE_ADDR range_beginning, range_end;
14098
14099 range_beginning = read_address (obfd, buffer, cu, &dummy);
14100 buffer += addr_size;
14101 range_end = read_address (obfd, buffer, cu, &dummy);
14102 buffer += addr_size;
14103 offset += 2 * addr_size;
14104
14105 /* An end of list marker is a pair of zero addresses. */
14106 if (range_beginning == 0 && range_end == 0)
14107 /* Found the end of list entry. */
14108 break;
14109
14110 /* Each base address selection entry is a pair of 2 values.
14111 The first is the largest possible address, the second is
14112 the base address. Check for a base address here. */
14113 if ((range_beginning & mask) == mask)
14114 {
14115 /* If we found the largest possible address, then we already
14116 have the base address in range_end. */
14117 base = range_end;
14118 found_base = 1;
14119 continue;
14120 }
14121
14122 if (!found_base)
14123 {
14124 /* We have no valid base address for the ranges
14125 data. */
14126 complaint (_("Invalid .debug_ranges data (no base address)"));
14127 return 0;
14128 }
14129
14130 if (range_beginning > range_end)
14131 {
14132 /* Inverted range entries are invalid. */
14133 complaint (_("Invalid .debug_ranges data (inverted range)"));
14134 return 0;
14135 }
14136
14137 /* Empty range entries have no effect. */
14138 if (range_beginning == range_end)
14139 continue;
14140
14141 range_beginning += base;
14142 range_end += base;
14143
14144 /* A not-uncommon case of bad debug info.
14145 Don't pollute the addrmap with bad data. */
14146 if (range_beginning + baseaddr == 0
14147 && !dwarf2_per_objfile->has_section_at_zero)
14148 {
14149 complaint (_(".debug_ranges entry has start address of zero"
14150 " [in module %s]"), objfile_name (objfile));
14151 continue;
14152 }
14153
14154 callback (range_beginning, range_end);
14155 }
14156
14157 return 1;
14158 }
14159
14160 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14161 Return 1 if the attributes are present and valid, otherwise, return 0.
14162 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14163
14164 static int
14165 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14166 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14167 dwarf2_psymtab *ranges_pst)
14168 {
14169 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14171 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14172 int low_set = 0;
14173 CORE_ADDR low = 0;
14174 CORE_ADDR high = 0;
14175 int retval;
14176
14177 retval = dwarf2_ranges_process (offset, cu,
14178 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14179 {
14180 if (ranges_pst != NULL)
14181 {
14182 CORE_ADDR lowpc;
14183 CORE_ADDR highpc;
14184
14185 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14186 range_beginning + baseaddr)
14187 - baseaddr);
14188 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14189 range_end + baseaddr)
14190 - baseaddr);
14191 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14192 lowpc, highpc - 1, ranges_pst);
14193 }
14194
14195 /* FIXME: This is recording everything as a low-high
14196 segment of consecutive addresses. We should have a
14197 data structure for discontiguous block ranges
14198 instead. */
14199 if (! low_set)
14200 {
14201 low = range_beginning;
14202 high = range_end;
14203 low_set = 1;
14204 }
14205 else
14206 {
14207 if (range_beginning < low)
14208 low = range_beginning;
14209 if (range_end > high)
14210 high = range_end;
14211 }
14212 });
14213 if (!retval)
14214 return 0;
14215
14216 if (! low_set)
14217 /* If the first entry is an end-of-list marker, the range
14218 describes an empty scope, i.e. no instructions. */
14219 return 0;
14220
14221 if (low_return)
14222 *low_return = low;
14223 if (high_return)
14224 *high_return = high;
14225 return 1;
14226 }
14227
14228 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14229 definition for the return value. *LOWPC and *HIGHPC are set iff
14230 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14231
14232 static enum pc_bounds_kind
14233 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14234 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14235 dwarf2_psymtab *pst)
14236 {
14237 struct dwarf2_per_objfile *dwarf2_per_objfile
14238 = cu->per_cu->dwarf2_per_objfile;
14239 struct attribute *attr;
14240 struct attribute *attr_high;
14241 CORE_ADDR low = 0;
14242 CORE_ADDR high = 0;
14243 enum pc_bounds_kind ret;
14244
14245 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14246 if (attr_high)
14247 {
14248 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14249 if (attr != nullptr)
14250 {
14251 low = attr->value_as_address ();
14252 high = attr_high->value_as_address ();
14253 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14254 high += low;
14255 }
14256 else
14257 /* Found high w/o low attribute. */
14258 return PC_BOUNDS_INVALID;
14259
14260 /* Found consecutive range of addresses. */
14261 ret = PC_BOUNDS_HIGH_LOW;
14262 }
14263 else
14264 {
14265 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14266 if (attr != NULL)
14267 {
14268 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14269 We take advantage of the fact that DW_AT_ranges does not appear
14270 in DW_TAG_compile_unit of DWO files. */
14271 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14272 unsigned int ranges_offset = (DW_UNSND (attr)
14273 + (need_ranges_base
14274 ? cu->ranges_base
14275 : 0));
14276
14277 /* Value of the DW_AT_ranges attribute is the offset in the
14278 .debug_ranges section. */
14279 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14280 return PC_BOUNDS_INVALID;
14281 /* Found discontinuous range of addresses. */
14282 ret = PC_BOUNDS_RANGES;
14283 }
14284 else
14285 return PC_BOUNDS_NOT_PRESENT;
14286 }
14287
14288 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14289 if (high <= low)
14290 return PC_BOUNDS_INVALID;
14291
14292 /* When using the GNU linker, .gnu.linkonce. sections are used to
14293 eliminate duplicate copies of functions and vtables and such.
14294 The linker will arbitrarily choose one and discard the others.
14295 The AT_*_pc values for such functions refer to local labels in
14296 these sections. If the section from that file was discarded, the
14297 labels are not in the output, so the relocs get a value of 0.
14298 If this is a discarded function, mark the pc bounds as invalid,
14299 so that GDB will ignore it. */
14300 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14301 return PC_BOUNDS_INVALID;
14302
14303 *lowpc = low;
14304 if (highpc)
14305 *highpc = high;
14306 return ret;
14307 }
14308
14309 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14310 its low and high PC addresses. Do nothing if these addresses could not
14311 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14312 and HIGHPC to the high address if greater than HIGHPC. */
14313
14314 static void
14315 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14316 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14317 struct dwarf2_cu *cu)
14318 {
14319 CORE_ADDR low, high;
14320 struct die_info *child = die->child;
14321
14322 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14323 {
14324 *lowpc = std::min (*lowpc, low);
14325 *highpc = std::max (*highpc, high);
14326 }
14327
14328 /* If the language does not allow nested subprograms (either inside
14329 subprograms or lexical blocks), we're done. */
14330 if (cu->language != language_ada)
14331 return;
14332
14333 /* Check all the children of the given DIE. If it contains nested
14334 subprograms, then check their pc bounds. Likewise, we need to
14335 check lexical blocks as well, as they may also contain subprogram
14336 definitions. */
14337 while (child && child->tag)
14338 {
14339 if (child->tag == DW_TAG_subprogram
14340 || child->tag == DW_TAG_lexical_block)
14341 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14342 child = sibling_die (child);
14343 }
14344 }
14345
14346 /* Get the low and high pc's represented by the scope DIE, and store
14347 them in *LOWPC and *HIGHPC. If the correct values can't be
14348 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14349
14350 static void
14351 get_scope_pc_bounds (struct die_info *die,
14352 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14353 struct dwarf2_cu *cu)
14354 {
14355 CORE_ADDR best_low = (CORE_ADDR) -1;
14356 CORE_ADDR best_high = (CORE_ADDR) 0;
14357 CORE_ADDR current_low, current_high;
14358
14359 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14360 >= PC_BOUNDS_RANGES)
14361 {
14362 best_low = current_low;
14363 best_high = current_high;
14364 }
14365 else
14366 {
14367 struct die_info *child = die->child;
14368
14369 while (child && child->tag)
14370 {
14371 switch (child->tag) {
14372 case DW_TAG_subprogram:
14373 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14374 break;
14375 case DW_TAG_namespace:
14376 case DW_TAG_module:
14377 /* FIXME: carlton/2004-01-16: Should we do this for
14378 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14379 that current GCC's always emit the DIEs corresponding
14380 to definitions of methods of classes as children of a
14381 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14382 the DIEs giving the declarations, which could be
14383 anywhere). But I don't see any reason why the
14384 standards says that they have to be there. */
14385 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14386
14387 if (current_low != ((CORE_ADDR) -1))
14388 {
14389 best_low = std::min (best_low, current_low);
14390 best_high = std::max (best_high, current_high);
14391 }
14392 break;
14393 default:
14394 /* Ignore. */
14395 break;
14396 }
14397
14398 child = sibling_die (child);
14399 }
14400 }
14401
14402 *lowpc = best_low;
14403 *highpc = best_high;
14404 }
14405
14406 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14407 in DIE. */
14408
14409 static void
14410 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14411 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14412 {
14413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14415 struct attribute *attr;
14416 struct attribute *attr_high;
14417
14418 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14419 if (attr_high)
14420 {
14421 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14422 if (attr != nullptr)
14423 {
14424 CORE_ADDR low = attr->value_as_address ();
14425 CORE_ADDR high = attr_high->value_as_address ();
14426
14427 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14428 high += low;
14429
14430 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14431 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14432 cu->get_builder ()->record_block_range (block, low, high - 1);
14433 }
14434 }
14435
14436 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14437 if (attr != nullptr)
14438 {
14439 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14440 We take advantage of the fact that DW_AT_ranges does not appear
14441 in DW_TAG_compile_unit of DWO files. */
14442 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14443
14444 /* The value of the DW_AT_ranges attribute is the offset of the
14445 address range list in the .debug_ranges section. */
14446 unsigned long offset = (DW_UNSND (attr)
14447 + (need_ranges_base ? cu->ranges_base : 0));
14448
14449 std::vector<blockrange> blockvec;
14450 dwarf2_ranges_process (offset, cu,
14451 [&] (CORE_ADDR start, CORE_ADDR end)
14452 {
14453 start += baseaddr;
14454 end += baseaddr;
14455 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14456 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14457 cu->get_builder ()->record_block_range (block, start, end - 1);
14458 blockvec.emplace_back (start, end);
14459 });
14460
14461 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14462 }
14463 }
14464
14465 /* Check whether the producer field indicates either of GCC < 4.6, or the
14466 Intel C/C++ compiler, and cache the result in CU. */
14467
14468 static void
14469 check_producer (struct dwarf2_cu *cu)
14470 {
14471 int major, minor;
14472
14473 if (cu->producer == NULL)
14474 {
14475 /* For unknown compilers expect their behavior is DWARF version
14476 compliant.
14477
14478 GCC started to support .debug_types sections by -gdwarf-4 since
14479 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14480 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14481 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14482 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14483 }
14484 else if (producer_is_gcc (cu->producer, &major, &minor))
14485 {
14486 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14487 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14488 }
14489 else if (producer_is_icc (cu->producer, &major, &minor))
14490 {
14491 cu->producer_is_icc = true;
14492 cu->producer_is_icc_lt_14 = major < 14;
14493 }
14494 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14495 cu->producer_is_codewarrior = true;
14496 else
14497 {
14498 /* For other non-GCC compilers, expect their behavior is DWARF version
14499 compliant. */
14500 }
14501
14502 cu->checked_producer = true;
14503 }
14504
14505 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14506 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14507 during 4.6.0 experimental. */
14508
14509 static bool
14510 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14511 {
14512 if (!cu->checked_producer)
14513 check_producer (cu);
14514
14515 return cu->producer_is_gxx_lt_4_6;
14516 }
14517
14518
14519 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14520 with incorrect is_stmt attributes. */
14521
14522 static bool
14523 producer_is_codewarrior (struct dwarf2_cu *cu)
14524 {
14525 if (!cu->checked_producer)
14526 check_producer (cu);
14527
14528 return cu->producer_is_codewarrior;
14529 }
14530
14531 /* Return the default accessibility type if it is not overridden by
14532 DW_AT_accessibility. */
14533
14534 static enum dwarf_access_attribute
14535 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14536 {
14537 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14538 {
14539 /* The default DWARF 2 accessibility for members is public, the default
14540 accessibility for inheritance is private. */
14541
14542 if (die->tag != DW_TAG_inheritance)
14543 return DW_ACCESS_public;
14544 else
14545 return DW_ACCESS_private;
14546 }
14547 else
14548 {
14549 /* DWARF 3+ defines the default accessibility a different way. The same
14550 rules apply now for DW_TAG_inheritance as for the members and it only
14551 depends on the container kind. */
14552
14553 if (die->parent->tag == DW_TAG_class_type)
14554 return DW_ACCESS_private;
14555 else
14556 return DW_ACCESS_public;
14557 }
14558 }
14559
14560 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14561 offset. If the attribute was not found return 0, otherwise return
14562 1. If it was found but could not properly be handled, set *OFFSET
14563 to 0. */
14564
14565 static int
14566 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14567 LONGEST *offset)
14568 {
14569 struct attribute *attr;
14570
14571 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14572 if (attr != NULL)
14573 {
14574 *offset = 0;
14575
14576 /* Note that we do not check for a section offset first here.
14577 This is because DW_AT_data_member_location is new in DWARF 4,
14578 so if we see it, we can assume that a constant form is really
14579 a constant and not a section offset. */
14580 if (attr->form_is_constant ())
14581 *offset = dwarf2_get_attr_constant_value (attr, 0);
14582 else if (attr->form_is_section_offset ())
14583 dwarf2_complex_location_expr_complaint ();
14584 else if (attr->form_is_block ())
14585 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14586 else
14587 dwarf2_complex_location_expr_complaint ();
14588
14589 return 1;
14590 }
14591
14592 return 0;
14593 }
14594
14595 /* Add an aggregate field to the field list. */
14596
14597 static void
14598 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14599 struct dwarf2_cu *cu)
14600 {
14601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14603 struct nextfield *new_field;
14604 struct attribute *attr;
14605 struct field *fp;
14606 const char *fieldname = "";
14607
14608 if (die->tag == DW_TAG_inheritance)
14609 {
14610 fip->baseclasses.emplace_back ();
14611 new_field = &fip->baseclasses.back ();
14612 }
14613 else
14614 {
14615 fip->fields.emplace_back ();
14616 new_field = &fip->fields.back ();
14617 }
14618
14619 fip->nfields++;
14620
14621 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14622 if (attr != nullptr)
14623 new_field->accessibility = DW_UNSND (attr);
14624 else
14625 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14626 if (new_field->accessibility != DW_ACCESS_public)
14627 fip->non_public_fields = 1;
14628
14629 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14630 if (attr != nullptr)
14631 new_field->virtuality = DW_UNSND (attr);
14632 else
14633 new_field->virtuality = DW_VIRTUALITY_none;
14634
14635 fp = &new_field->field;
14636
14637 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14638 {
14639 LONGEST offset;
14640
14641 /* Data member other than a C++ static data member. */
14642
14643 /* Get type of field. */
14644 fp->type = die_type (die, cu);
14645
14646 SET_FIELD_BITPOS (*fp, 0);
14647
14648 /* Get bit size of field (zero if none). */
14649 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14650 if (attr != nullptr)
14651 {
14652 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14653 }
14654 else
14655 {
14656 FIELD_BITSIZE (*fp) = 0;
14657 }
14658
14659 /* Get bit offset of field. */
14660 if (handle_data_member_location (die, cu, &offset))
14661 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14662 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14663 if (attr != nullptr)
14664 {
14665 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14666 {
14667 /* For big endian bits, the DW_AT_bit_offset gives the
14668 additional bit offset from the MSB of the containing
14669 anonymous object to the MSB of the field. We don't
14670 have to do anything special since we don't need to
14671 know the size of the anonymous object. */
14672 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14673 }
14674 else
14675 {
14676 /* For little endian bits, compute the bit offset to the
14677 MSB of the anonymous object, subtract off the number of
14678 bits from the MSB of the field to the MSB of the
14679 object, and then subtract off the number of bits of
14680 the field itself. The result is the bit offset of
14681 the LSB of the field. */
14682 int anonymous_size;
14683 int bit_offset = DW_UNSND (attr);
14684
14685 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14686 if (attr != nullptr)
14687 {
14688 /* The size of the anonymous object containing
14689 the bit field is explicit, so use the
14690 indicated size (in bytes). */
14691 anonymous_size = DW_UNSND (attr);
14692 }
14693 else
14694 {
14695 /* The size of the anonymous object containing
14696 the bit field must be inferred from the type
14697 attribute of the data member containing the
14698 bit field. */
14699 anonymous_size = TYPE_LENGTH (fp->type);
14700 }
14701 SET_FIELD_BITPOS (*fp,
14702 (FIELD_BITPOS (*fp)
14703 + anonymous_size * bits_per_byte
14704 - bit_offset - FIELD_BITSIZE (*fp)));
14705 }
14706 }
14707 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14708 if (attr != NULL)
14709 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14710 + dwarf2_get_attr_constant_value (attr, 0)));
14711
14712 /* Get name of field. */
14713 fieldname = dwarf2_name (die, cu);
14714 if (fieldname == NULL)
14715 fieldname = "";
14716
14717 /* The name is already allocated along with this objfile, so we don't
14718 need to duplicate it for the type. */
14719 fp->name = fieldname;
14720
14721 /* Change accessibility for artificial fields (e.g. virtual table
14722 pointer or virtual base class pointer) to private. */
14723 if (dwarf2_attr (die, DW_AT_artificial, cu))
14724 {
14725 FIELD_ARTIFICIAL (*fp) = 1;
14726 new_field->accessibility = DW_ACCESS_private;
14727 fip->non_public_fields = 1;
14728 }
14729 }
14730 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14731 {
14732 /* C++ static member. */
14733
14734 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14735 is a declaration, but all versions of G++ as of this writing
14736 (so through at least 3.2.1) incorrectly generate
14737 DW_TAG_variable tags. */
14738
14739 const char *physname;
14740
14741 /* Get name of field. */
14742 fieldname = dwarf2_name (die, cu);
14743 if (fieldname == NULL)
14744 return;
14745
14746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14747 if (attr
14748 /* Only create a symbol if this is an external value.
14749 new_symbol checks this and puts the value in the global symbol
14750 table, which we want. If it is not external, new_symbol
14751 will try to put the value in cu->list_in_scope which is wrong. */
14752 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14753 {
14754 /* A static const member, not much different than an enum as far as
14755 we're concerned, except that we can support more types. */
14756 new_symbol (die, NULL, cu);
14757 }
14758
14759 /* Get physical name. */
14760 physname = dwarf2_physname (fieldname, die, cu);
14761
14762 /* The name is already allocated along with this objfile, so we don't
14763 need to duplicate it for the type. */
14764 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14765 FIELD_TYPE (*fp) = die_type (die, cu);
14766 FIELD_NAME (*fp) = fieldname;
14767 }
14768 else if (die->tag == DW_TAG_inheritance)
14769 {
14770 LONGEST offset;
14771
14772 /* C++ base class field. */
14773 if (handle_data_member_location (die, cu, &offset))
14774 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14775 FIELD_BITSIZE (*fp) = 0;
14776 FIELD_TYPE (*fp) = die_type (die, cu);
14777 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14778 }
14779 else if (die->tag == DW_TAG_variant_part)
14780 {
14781 /* process_structure_scope will treat this DIE as a union. */
14782 process_structure_scope (die, cu);
14783
14784 /* The variant part is relative to the start of the enclosing
14785 structure. */
14786 SET_FIELD_BITPOS (*fp, 0);
14787 fp->type = get_die_type (die, cu);
14788 fp->artificial = 1;
14789 fp->name = "<<variant>>";
14790
14791 /* Normally a DW_TAG_variant_part won't have a size, but our
14792 representation requires one, so set it to the maximum of the
14793 child sizes, being sure to account for the offset at which
14794 each child is seen. */
14795 if (TYPE_LENGTH (fp->type) == 0)
14796 {
14797 unsigned max = 0;
14798 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14799 {
14800 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14801 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14802 if (len > max)
14803 max = len;
14804 }
14805 TYPE_LENGTH (fp->type) = max;
14806 }
14807 }
14808 else
14809 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14810 }
14811
14812 /* Can the type given by DIE define another type? */
14813
14814 static bool
14815 type_can_define_types (const struct die_info *die)
14816 {
14817 switch (die->tag)
14818 {
14819 case DW_TAG_typedef:
14820 case DW_TAG_class_type:
14821 case DW_TAG_structure_type:
14822 case DW_TAG_union_type:
14823 case DW_TAG_enumeration_type:
14824 return true;
14825
14826 default:
14827 return false;
14828 }
14829 }
14830
14831 /* Add a type definition defined in the scope of the FIP's class. */
14832
14833 static void
14834 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14835 struct dwarf2_cu *cu)
14836 {
14837 struct decl_field fp;
14838 memset (&fp, 0, sizeof (fp));
14839
14840 gdb_assert (type_can_define_types (die));
14841
14842 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14843 fp.name = dwarf2_name (die, cu);
14844 fp.type = read_type_die (die, cu);
14845
14846 /* Save accessibility. */
14847 enum dwarf_access_attribute accessibility;
14848 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14849 if (attr != NULL)
14850 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14851 else
14852 accessibility = dwarf2_default_access_attribute (die, cu);
14853 switch (accessibility)
14854 {
14855 case DW_ACCESS_public:
14856 /* The assumed value if neither private nor protected. */
14857 break;
14858 case DW_ACCESS_private:
14859 fp.is_private = 1;
14860 break;
14861 case DW_ACCESS_protected:
14862 fp.is_protected = 1;
14863 break;
14864 default:
14865 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14866 }
14867
14868 if (die->tag == DW_TAG_typedef)
14869 fip->typedef_field_list.push_back (fp);
14870 else
14871 fip->nested_types_list.push_back (fp);
14872 }
14873
14874 /* Create the vector of fields, and attach it to the type. */
14875
14876 static void
14877 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14878 struct dwarf2_cu *cu)
14879 {
14880 int nfields = fip->nfields;
14881
14882 /* Record the field count, allocate space for the array of fields,
14883 and create blank accessibility bitfields if necessary. */
14884 TYPE_NFIELDS (type) = nfields;
14885 TYPE_FIELDS (type) = (struct field *)
14886 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14887
14888 if (fip->non_public_fields && cu->language != language_ada)
14889 {
14890 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14891
14892 TYPE_FIELD_PRIVATE_BITS (type) =
14893 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14894 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14895
14896 TYPE_FIELD_PROTECTED_BITS (type) =
14897 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14898 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14899
14900 TYPE_FIELD_IGNORE_BITS (type) =
14901 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14902 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14903 }
14904
14905 /* If the type has baseclasses, allocate and clear a bit vector for
14906 TYPE_FIELD_VIRTUAL_BITS. */
14907 if (!fip->baseclasses.empty () && cu->language != language_ada)
14908 {
14909 int num_bytes = B_BYTES (fip->baseclasses.size ());
14910 unsigned char *pointer;
14911
14912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14913 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14914 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14915 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14916 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14917 }
14918
14919 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14920 {
14921 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14922
14923 for (int index = 0; index < nfields; ++index)
14924 {
14925 struct nextfield &field = fip->fields[index];
14926
14927 if (field.variant.is_discriminant)
14928 di->discriminant_index = index;
14929 else if (field.variant.default_branch)
14930 di->default_index = index;
14931 else
14932 di->discriminants[index] = field.variant.discriminant_value;
14933 }
14934 }
14935
14936 /* Copy the saved-up fields into the field vector. */
14937 for (int i = 0; i < nfields; ++i)
14938 {
14939 struct nextfield &field
14940 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14941 : fip->fields[i - fip->baseclasses.size ()]);
14942
14943 TYPE_FIELD (type, i) = field.field;
14944 switch (field.accessibility)
14945 {
14946 case DW_ACCESS_private:
14947 if (cu->language != language_ada)
14948 SET_TYPE_FIELD_PRIVATE (type, i);
14949 break;
14950
14951 case DW_ACCESS_protected:
14952 if (cu->language != language_ada)
14953 SET_TYPE_FIELD_PROTECTED (type, i);
14954 break;
14955
14956 case DW_ACCESS_public:
14957 break;
14958
14959 default:
14960 /* Unknown accessibility. Complain and treat it as public. */
14961 {
14962 complaint (_("unsupported accessibility %d"),
14963 field.accessibility);
14964 }
14965 break;
14966 }
14967 if (i < fip->baseclasses.size ())
14968 {
14969 switch (field.virtuality)
14970 {
14971 case DW_VIRTUALITY_virtual:
14972 case DW_VIRTUALITY_pure_virtual:
14973 if (cu->language == language_ada)
14974 error (_("unexpected virtuality in component of Ada type"));
14975 SET_TYPE_FIELD_VIRTUAL (type, i);
14976 break;
14977 }
14978 }
14979 }
14980 }
14981
14982 /* Return true if this member function is a constructor, false
14983 otherwise. */
14984
14985 static int
14986 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14987 {
14988 const char *fieldname;
14989 const char *type_name;
14990 int len;
14991
14992 if (die->parent == NULL)
14993 return 0;
14994
14995 if (die->parent->tag != DW_TAG_structure_type
14996 && die->parent->tag != DW_TAG_union_type
14997 && die->parent->tag != DW_TAG_class_type)
14998 return 0;
14999
15000 fieldname = dwarf2_name (die, cu);
15001 type_name = dwarf2_name (die->parent, cu);
15002 if (fieldname == NULL || type_name == NULL)
15003 return 0;
15004
15005 len = strlen (fieldname);
15006 return (strncmp (fieldname, type_name, len) == 0
15007 && (type_name[len] == '\0' || type_name[len] == '<'));
15008 }
15009
15010 /* Check if the given VALUE is a recognized enum
15011 dwarf_defaulted_attribute constant according to DWARF5 spec,
15012 Table 7.24. */
15013
15014 static bool
15015 is_valid_DW_AT_defaulted (ULONGEST value)
15016 {
15017 switch (value)
15018 {
15019 case DW_DEFAULTED_no:
15020 case DW_DEFAULTED_in_class:
15021 case DW_DEFAULTED_out_of_class:
15022 return true;
15023 }
15024
15025 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15026 return false;
15027 }
15028
15029 /* Add a member function to the proper fieldlist. */
15030
15031 static void
15032 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15033 struct type *type, struct dwarf2_cu *cu)
15034 {
15035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15036 struct attribute *attr;
15037 int i;
15038 struct fnfieldlist *flp = nullptr;
15039 struct fn_field *fnp;
15040 const char *fieldname;
15041 struct type *this_type;
15042 enum dwarf_access_attribute accessibility;
15043
15044 if (cu->language == language_ada)
15045 error (_("unexpected member function in Ada type"));
15046
15047 /* Get name of member function. */
15048 fieldname = dwarf2_name (die, cu);
15049 if (fieldname == NULL)
15050 return;
15051
15052 /* Look up member function name in fieldlist. */
15053 for (i = 0; i < fip->fnfieldlists.size (); i++)
15054 {
15055 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15056 {
15057 flp = &fip->fnfieldlists[i];
15058 break;
15059 }
15060 }
15061
15062 /* Create a new fnfieldlist if necessary. */
15063 if (flp == nullptr)
15064 {
15065 fip->fnfieldlists.emplace_back ();
15066 flp = &fip->fnfieldlists.back ();
15067 flp->name = fieldname;
15068 i = fip->fnfieldlists.size () - 1;
15069 }
15070
15071 /* Create a new member function field and add it to the vector of
15072 fnfieldlists. */
15073 flp->fnfields.emplace_back ();
15074 fnp = &flp->fnfields.back ();
15075
15076 /* Delay processing of the physname until later. */
15077 if (cu->language == language_cplus)
15078 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15079 die, cu);
15080 else
15081 {
15082 const char *physname = dwarf2_physname (fieldname, die, cu);
15083 fnp->physname = physname ? physname : "";
15084 }
15085
15086 fnp->type = alloc_type (objfile);
15087 this_type = read_type_die (die, cu);
15088 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15089 {
15090 int nparams = TYPE_NFIELDS (this_type);
15091
15092 /* TYPE is the domain of this method, and THIS_TYPE is the type
15093 of the method itself (TYPE_CODE_METHOD). */
15094 smash_to_method_type (fnp->type, type,
15095 TYPE_TARGET_TYPE (this_type),
15096 TYPE_FIELDS (this_type),
15097 TYPE_NFIELDS (this_type),
15098 TYPE_VARARGS (this_type));
15099
15100 /* Handle static member functions.
15101 Dwarf2 has no clean way to discern C++ static and non-static
15102 member functions. G++ helps GDB by marking the first
15103 parameter for non-static member functions (which is the this
15104 pointer) as artificial. We obtain this information from
15105 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15106 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15107 fnp->voffset = VOFFSET_STATIC;
15108 }
15109 else
15110 complaint (_("member function type missing for '%s'"),
15111 dwarf2_full_name (fieldname, die, cu));
15112
15113 /* Get fcontext from DW_AT_containing_type if present. */
15114 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15115 fnp->fcontext = die_containing_type (die, cu);
15116
15117 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15118 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15119
15120 /* Get accessibility. */
15121 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15122 if (attr != nullptr)
15123 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15124 else
15125 accessibility = dwarf2_default_access_attribute (die, cu);
15126 switch (accessibility)
15127 {
15128 case DW_ACCESS_private:
15129 fnp->is_private = 1;
15130 break;
15131 case DW_ACCESS_protected:
15132 fnp->is_protected = 1;
15133 break;
15134 }
15135
15136 /* Check for artificial methods. */
15137 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15138 if (attr && DW_UNSND (attr) != 0)
15139 fnp->is_artificial = 1;
15140
15141 /* Check for defaulted methods. */
15142 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15143 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15144 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15145
15146 /* Check for deleted methods. */
15147 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15148 if (attr != nullptr && DW_UNSND (attr) != 0)
15149 fnp->is_deleted = 1;
15150
15151 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15152
15153 /* Get index in virtual function table if it is a virtual member
15154 function. For older versions of GCC, this is an offset in the
15155 appropriate virtual table, as specified by DW_AT_containing_type.
15156 For everyone else, it is an expression to be evaluated relative
15157 to the object address. */
15158
15159 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15160 if (attr != nullptr)
15161 {
15162 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15163 {
15164 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15165 {
15166 /* Old-style GCC. */
15167 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15168 }
15169 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15170 || (DW_BLOCK (attr)->size > 1
15171 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15172 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15173 {
15174 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15175 if ((fnp->voffset % cu->header.addr_size) != 0)
15176 dwarf2_complex_location_expr_complaint ();
15177 else
15178 fnp->voffset /= cu->header.addr_size;
15179 fnp->voffset += 2;
15180 }
15181 else
15182 dwarf2_complex_location_expr_complaint ();
15183
15184 if (!fnp->fcontext)
15185 {
15186 /* If there is no `this' field and no DW_AT_containing_type,
15187 we cannot actually find a base class context for the
15188 vtable! */
15189 if (TYPE_NFIELDS (this_type) == 0
15190 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15191 {
15192 complaint (_("cannot determine context for virtual member "
15193 "function \"%s\" (offset %s)"),
15194 fieldname, sect_offset_str (die->sect_off));
15195 }
15196 else
15197 {
15198 fnp->fcontext
15199 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15200 }
15201 }
15202 }
15203 else if (attr->form_is_section_offset ())
15204 {
15205 dwarf2_complex_location_expr_complaint ();
15206 }
15207 else
15208 {
15209 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15210 fieldname);
15211 }
15212 }
15213 else
15214 {
15215 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15216 if (attr && DW_UNSND (attr))
15217 {
15218 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15219 complaint (_("Member function \"%s\" (offset %s) is virtual "
15220 "but the vtable offset is not specified"),
15221 fieldname, sect_offset_str (die->sect_off));
15222 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15223 TYPE_CPLUS_DYNAMIC (type) = 1;
15224 }
15225 }
15226 }
15227
15228 /* Create the vector of member function fields, and attach it to the type. */
15229
15230 static void
15231 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15232 struct dwarf2_cu *cu)
15233 {
15234 if (cu->language == language_ada)
15235 error (_("unexpected member functions in Ada type"));
15236
15237 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15238 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15239 TYPE_ALLOC (type,
15240 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15241
15242 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15243 {
15244 struct fnfieldlist &nf = fip->fnfieldlists[i];
15245 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15246
15247 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15248 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15249 fn_flp->fn_fields = (struct fn_field *)
15250 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15251
15252 for (int k = 0; k < nf.fnfields.size (); ++k)
15253 fn_flp->fn_fields[k] = nf.fnfields[k];
15254 }
15255
15256 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15257 }
15258
15259 /* Returns non-zero if NAME is the name of a vtable member in CU's
15260 language, zero otherwise. */
15261 static int
15262 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15263 {
15264 static const char vptr[] = "_vptr";
15265
15266 /* Look for the C++ form of the vtable. */
15267 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15268 return 1;
15269
15270 return 0;
15271 }
15272
15273 /* GCC outputs unnamed structures that are really pointers to member
15274 functions, with the ABI-specified layout. If TYPE describes
15275 such a structure, smash it into a member function type.
15276
15277 GCC shouldn't do this; it should just output pointer to member DIEs.
15278 This is GCC PR debug/28767. */
15279
15280 static void
15281 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15282 {
15283 struct type *pfn_type, *self_type, *new_type;
15284
15285 /* Check for a structure with no name and two children. */
15286 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15287 return;
15288
15289 /* Check for __pfn and __delta members. */
15290 if (TYPE_FIELD_NAME (type, 0) == NULL
15291 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15292 || TYPE_FIELD_NAME (type, 1) == NULL
15293 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15294 return;
15295
15296 /* Find the type of the method. */
15297 pfn_type = TYPE_FIELD_TYPE (type, 0);
15298 if (pfn_type == NULL
15299 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15300 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15301 return;
15302
15303 /* Look for the "this" argument. */
15304 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15305 if (TYPE_NFIELDS (pfn_type) == 0
15306 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15307 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15308 return;
15309
15310 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15311 new_type = alloc_type (objfile);
15312 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15313 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15314 TYPE_VARARGS (pfn_type));
15315 smash_to_methodptr_type (type, new_type);
15316 }
15317
15318 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15319 appropriate error checking and issuing complaints if there is a
15320 problem. */
15321
15322 static ULONGEST
15323 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15324 {
15325 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15326
15327 if (attr == nullptr)
15328 return 0;
15329
15330 if (!attr->form_is_constant ())
15331 {
15332 complaint (_("DW_AT_alignment must have constant form"
15333 " - DIE at %s [in module %s]"),
15334 sect_offset_str (die->sect_off),
15335 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15336 return 0;
15337 }
15338
15339 ULONGEST align;
15340 if (attr->form == DW_FORM_sdata)
15341 {
15342 LONGEST val = DW_SND (attr);
15343 if (val < 0)
15344 {
15345 complaint (_("DW_AT_alignment value must not be negative"
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
15348 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15349 return 0;
15350 }
15351 align = val;
15352 }
15353 else
15354 align = DW_UNSND (attr);
15355
15356 if (align == 0)
15357 {
15358 complaint (_("DW_AT_alignment value must not be zero"
15359 " - DIE at %s [in module %s]"),
15360 sect_offset_str (die->sect_off),
15361 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15362 return 0;
15363 }
15364 if ((align & (align - 1)) != 0)
15365 {
15366 complaint (_("DW_AT_alignment value must be a power of 2"
15367 " - DIE at %s [in module %s]"),
15368 sect_offset_str (die->sect_off),
15369 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15370 return 0;
15371 }
15372
15373 return align;
15374 }
15375
15376 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15377 the alignment for TYPE. */
15378
15379 static void
15380 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15381 struct type *type)
15382 {
15383 if (!set_type_align (type, get_alignment (cu, die)))
15384 complaint (_("DW_AT_alignment value too large"
15385 " - DIE at %s [in module %s]"),
15386 sect_offset_str (die->sect_off),
15387 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15388 }
15389
15390 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15391 constant for a type, according to DWARF5 spec, Table 5.5. */
15392
15393 static bool
15394 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15395 {
15396 switch (value)
15397 {
15398 case DW_CC_normal:
15399 case DW_CC_pass_by_reference:
15400 case DW_CC_pass_by_value:
15401 return true;
15402
15403 default:
15404 complaint (_("unrecognized DW_AT_calling_convention value "
15405 "(%s) for a type"), pulongest (value));
15406 return false;
15407 }
15408 }
15409
15410 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15411 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15412 also according to GNU-specific values (see include/dwarf2.h). */
15413
15414 static bool
15415 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15416 {
15417 switch (value)
15418 {
15419 case DW_CC_normal:
15420 case DW_CC_program:
15421 case DW_CC_nocall:
15422 return true;
15423
15424 case DW_CC_GNU_renesas_sh:
15425 case DW_CC_GNU_borland_fastcall_i386:
15426 case DW_CC_GDB_IBM_OpenCL:
15427 return true;
15428
15429 default:
15430 complaint (_("unrecognized DW_AT_calling_convention value "
15431 "(%s) for a subroutine"), pulongest (value));
15432 return false;
15433 }
15434 }
15435
15436 /* Called when we find the DIE that starts a structure or union scope
15437 (definition) to create a type for the structure or union. Fill in
15438 the type's name and general properties; the members will not be
15439 processed until process_structure_scope. A symbol table entry for
15440 the type will also not be done until process_structure_scope (assuming
15441 the type has a name).
15442
15443 NOTE: we need to call these functions regardless of whether or not the
15444 DIE has a DW_AT_name attribute, since it might be an anonymous
15445 structure or union. This gets the type entered into our set of
15446 user defined types. */
15447
15448 static struct type *
15449 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15450 {
15451 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15452 struct type *type;
15453 struct attribute *attr;
15454 const char *name;
15455
15456 /* If the definition of this type lives in .debug_types, read that type.
15457 Don't follow DW_AT_specification though, that will take us back up
15458 the chain and we want to go down. */
15459 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15460 if (attr != nullptr)
15461 {
15462 type = get_DW_AT_signature_type (die, attr, cu);
15463
15464 /* The type's CU may not be the same as CU.
15465 Ensure TYPE is recorded with CU in die_type_hash. */
15466 return set_die_type (die, type, cu);
15467 }
15468
15469 type = alloc_type (objfile);
15470 INIT_CPLUS_SPECIFIC (type);
15471
15472 name = dwarf2_name (die, cu);
15473 if (name != NULL)
15474 {
15475 if (cu->language == language_cplus
15476 || cu->language == language_d
15477 || cu->language == language_rust)
15478 {
15479 const char *full_name = dwarf2_full_name (name, die, cu);
15480
15481 /* dwarf2_full_name might have already finished building the DIE's
15482 type. If so, there is no need to continue. */
15483 if (get_die_type (die, cu) != NULL)
15484 return get_die_type (die, cu);
15485
15486 TYPE_NAME (type) = full_name;
15487 }
15488 else
15489 {
15490 /* The name is already allocated along with this objfile, so
15491 we don't need to duplicate it for the type. */
15492 TYPE_NAME (type) = name;
15493 }
15494 }
15495
15496 if (die->tag == DW_TAG_structure_type)
15497 {
15498 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15499 }
15500 else if (die->tag == DW_TAG_union_type)
15501 {
15502 TYPE_CODE (type) = TYPE_CODE_UNION;
15503 }
15504 else if (die->tag == DW_TAG_variant_part)
15505 {
15506 TYPE_CODE (type) = TYPE_CODE_UNION;
15507 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15508 }
15509 else
15510 {
15511 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15512 }
15513
15514 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15515 TYPE_DECLARED_CLASS (type) = 1;
15516
15517 /* Store the calling convention in the type if it's available in
15518 the die. Otherwise the calling convention remains set to
15519 the default value DW_CC_normal. */
15520 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15521 if (attr != nullptr
15522 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15523 {
15524 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15525 TYPE_CPLUS_CALLING_CONVENTION (type)
15526 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15527 }
15528
15529 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15530 if (attr != nullptr)
15531 {
15532 if (attr->form_is_constant ())
15533 TYPE_LENGTH (type) = DW_UNSND (attr);
15534 else
15535 {
15536 /* For the moment, dynamic type sizes are not supported
15537 by GDB's struct type. The actual size is determined
15538 on-demand when resolving the type of a given object,
15539 so set the type's length to zero for now. Otherwise,
15540 we record an expression as the length, and that expression
15541 could lead to a very large value, which could eventually
15542 lead to us trying to allocate that much memory when creating
15543 a value of that type. */
15544 TYPE_LENGTH (type) = 0;
15545 }
15546 }
15547 else
15548 {
15549 TYPE_LENGTH (type) = 0;
15550 }
15551
15552 maybe_set_alignment (cu, die, type);
15553
15554 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15555 {
15556 /* ICC<14 does not output the required DW_AT_declaration on
15557 incomplete types, but gives them a size of zero. */
15558 TYPE_STUB (type) = 1;
15559 }
15560 else
15561 TYPE_STUB_SUPPORTED (type) = 1;
15562
15563 if (die_is_declaration (die, cu))
15564 TYPE_STUB (type) = 1;
15565 else if (attr == NULL && die->child == NULL
15566 && producer_is_realview (cu->producer))
15567 /* RealView does not output the required DW_AT_declaration
15568 on incomplete types. */
15569 TYPE_STUB (type) = 1;
15570
15571 /* We need to add the type field to the die immediately so we don't
15572 infinitely recurse when dealing with pointers to the structure
15573 type within the structure itself. */
15574 set_die_type (die, type, cu);
15575
15576 /* set_die_type should be already done. */
15577 set_descriptive_type (type, die, cu);
15578
15579 return type;
15580 }
15581
15582 /* A helper for process_structure_scope that handles a single member
15583 DIE. */
15584
15585 static void
15586 handle_struct_member_die (struct die_info *child_die, struct type *type,
15587 struct field_info *fi,
15588 std::vector<struct symbol *> *template_args,
15589 struct dwarf2_cu *cu)
15590 {
15591 if (child_die->tag == DW_TAG_member
15592 || child_die->tag == DW_TAG_variable
15593 || child_die->tag == DW_TAG_variant_part)
15594 {
15595 /* NOTE: carlton/2002-11-05: A C++ static data member
15596 should be a DW_TAG_member that is a declaration, but
15597 all versions of G++ as of this writing (so through at
15598 least 3.2.1) incorrectly generate DW_TAG_variable
15599 tags for them instead. */
15600 dwarf2_add_field (fi, child_die, cu);
15601 }
15602 else if (child_die->tag == DW_TAG_subprogram)
15603 {
15604 /* Rust doesn't have member functions in the C++ sense.
15605 However, it does emit ordinary functions as children
15606 of a struct DIE. */
15607 if (cu->language == language_rust)
15608 read_func_scope (child_die, cu);
15609 else
15610 {
15611 /* C++ member function. */
15612 dwarf2_add_member_fn (fi, child_die, type, cu);
15613 }
15614 }
15615 else if (child_die->tag == DW_TAG_inheritance)
15616 {
15617 /* C++ base class field. */
15618 dwarf2_add_field (fi, child_die, cu);
15619 }
15620 else if (type_can_define_types (child_die))
15621 dwarf2_add_type_defn (fi, child_die, cu);
15622 else if (child_die->tag == DW_TAG_template_type_param
15623 || child_die->tag == DW_TAG_template_value_param)
15624 {
15625 struct symbol *arg = new_symbol (child_die, NULL, cu);
15626
15627 if (arg != NULL)
15628 template_args->push_back (arg);
15629 }
15630 else if (child_die->tag == DW_TAG_variant)
15631 {
15632 /* In a variant we want to get the discriminant and also add a
15633 field for our sole member child. */
15634 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15635
15636 for (die_info *variant_child = child_die->child;
15637 variant_child != NULL;
15638 variant_child = sibling_die (variant_child))
15639 {
15640 if (variant_child->tag == DW_TAG_member)
15641 {
15642 handle_struct_member_die (variant_child, type, fi,
15643 template_args, cu);
15644 /* Only handle the one. */
15645 break;
15646 }
15647 }
15648
15649 /* We don't handle this but we might as well report it if we see
15650 it. */
15651 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15652 complaint (_("DW_AT_discr_list is not supported yet"
15653 " - DIE at %s [in module %s]"),
15654 sect_offset_str (child_die->sect_off),
15655 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15656
15657 /* The first field was just added, so we can stash the
15658 discriminant there. */
15659 gdb_assert (!fi->fields.empty ());
15660 if (discr == NULL)
15661 fi->fields.back ().variant.default_branch = true;
15662 else
15663 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15664 }
15665 }
15666
15667 /* Finish creating a structure or union type, including filling in
15668 its members and creating a symbol for it. */
15669
15670 static void
15671 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15672 {
15673 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15674 struct die_info *child_die;
15675 struct type *type;
15676
15677 type = get_die_type (die, cu);
15678 if (type == NULL)
15679 type = read_structure_type (die, cu);
15680
15681 /* When reading a DW_TAG_variant_part, we need to notice when we
15682 read the discriminant member, so we can record it later in the
15683 discriminant_info. */
15684 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15685 sect_offset discr_offset {};
15686 bool has_template_parameters = false;
15687
15688 if (is_variant_part)
15689 {
15690 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15691 if (discr == NULL)
15692 {
15693 /* Maybe it's a univariant form, an extension we support.
15694 In this case arrange not to check the offset. */
15695 is_variant_part = false;
15696 }
15697 else if (discr->form_is_ref ())
15698 {
15699 struct dwarf2_cu *target_cu = cu;
15700 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15701
15702 discr_offset = target_die->sect_off;
15703 }
15704 else
15705 {
15706 complaint (_("DW_AT_discr does not have DIE reference form"
15707 " - DIE at %s [in module %s]"),
15708 sect_offset_str (die->sect_off),
15709 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15710 is_variant_part = false;
15711 }
15712 }
15713
15714 if (die->child != NULL && ! die_is_declaration (die, cu))
15715 {
15716 struct field_info fi;
15717 std::vector<struct symbol *> template_args;
15718
15719 child_die = die->child;
15720
15721 while (child_die && child_die->tag)
15722 {
15723 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15724
15725 if (is_variant_part && discr_offset == child_die->sect_off)
15726 fi.fields.back ().variant.is_discriminant = true;
15727
15728 child_die = sibling_die (child_die);
15729 }
15730
15731 /* Attach template arguments to type. */
15732 if (!template_args.empty ())
15733 {
15734 has_template_parameters = true;
15735 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15736 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15737 TYPE_TEMPLATE_ARGUMENTS (type)
15738 = XOBNEWVEC (&objfile->objfile_obstack,
15739 struct symbol *,
15740 TYPE_N_TEMPLATE_ARGUMENTS (type));
15741 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15742 template_args.data (),
15743 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15744 * sizeof (struct symbol *)));
15745 }
15746
15747 /* Attach fields and member functions to the type. */
15748 if (fi.nfields)
15749 dwarf2_attach_fields_to_type (&fi, type, cu);
15750 if (!fi.fnfieldlists.empty ())
15751 {
15752 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15753
15754 /* Get the type which refers to the base class (possibly this
15755 class itself) which contains the vtable pointer for the current
15756 class from the DW_AT_containing_type attribute. This use of
15757 DW_AT_containing_type is a GNU extension. */
15758
15759 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15760 {
15761 struct type *t = die_containing_type (die, cu);
15762
15763 set_type_vptr_basetype (type, t);
15764 if (type == t)
15765 {
15766 int i;
15767
15768 /* Our own class provides vtbl ptr. */
15769 for (i = TYPE_NFIELDS (t) - 1;
15770 i >= TYPE_N_BASECLASSES (t);
15771 --i)
15772 {
15773 const char *fieldname = TYPE_FIELD_NAME (t, i);
15774
15775 if (is_vtable_name (fieldname, cu))
15776 {
15777 set_type_vptr_fieldno (type, i);
15778 break;
15779 }
15780 }
15781
15782 /* Complain if virtual function table field not found. */
15783 if (i < TYPE_N_BASECLASSES (t))
15784 complaint (_("virtual function table pointer "
15785 "not found when defining class '%s'"),
15786 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15787 }
15788 else
15789 {
15790 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15791 }
15792 }
15793 else if (cu->producer
15794 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15795 {
15796 /* The IBM XLC compiler does not provide direct indication
15797 of the containing type, but the vtable pointer is
15798 always named __vfp. */
15799
15800 int i;
15801
15802 for (i = TYPE_NFIELDS (type) - 1;
15803 i >= TYPE_N_BASECLASSES (type);
15804 --i)
15805 {
15806 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15807 {
15808 set_type_vptr_fieldno (type, i);
15809 set_type_vptr_basetype (type, type);
15810 break;
15811 }
15812 }
15813 }
15814 }
15815
15816 /* Copy fi.typedef_field_list linked list elements content into the
15817 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15818 if (!fi.typedef_field_list.empty ())
15819 {
15820 int count = fi.typedef_field_list.size ();
15821
15822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15823 TYPE_TYPEDEF_FIELD_ARRAY (type)
15824 = ((struct decl_field *)
15825 TYPE_ALLOC (type,
15826 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15827 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15828
15829 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15830 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15831 }
15832
15833 /* Copy fi.nested_types_list linked list elements content into the
15834 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15835 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15836 {
15837 int count = fi.nested_types_list.size ();
15838
15839 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15840 TYPE_NESTED_TYPES_ARRAY (type)
15841 = ((struct decl_field *)
15842 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15843 TYPE_NESTED_TYPES_COUNT (type) = count;
15844
15845 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15846 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15847 }
15848 }
15849
15850 quirk_gcc_member_function_pointer (type, objfile);
15851 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15852 cu->rust_unions.push_back (type);
15853
15854 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15855 snapshots) has been known to create a die giving a declaration
15856 for a class that has, as a child, a die giving a definition for a
15857 nested class. So we have to process our children even if the
15858 current die is a declaration. Normally, of course, a declaration
15859 won't have any children at all. */
15860
15861 child_die = die->child;
15862
15863 while (child_die != NULL && child_die->tag)
15864 {
15865 if (child_die->tag == DW_TAG_member
15866 || child_die->tag == DW_TAG_variable
15867 || child_die->tag == DW_TAG_inheritance
15868 || child_die->tag == DW_TAG_template_value_param
15869 || child_die->tag == DW_TAG_template_type_param)
15870 {
15871 /* Do nothing. */
15872 }
15873 else
15874 process_die (child_die, cu);
15875
15876 child_die = sibling_die (child_die);
15877 }
15878
15879 /* Do not consider external references. According to the DWARF standard,
15880 these DIEs are identified by the fact that they have no byte_size
15881 attribute, and a declaration attribute. */
15882 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15883 || !die_is_declaration (die, cu))
15884 {
15885 struct symbol *sym = new_symbol (die, type, cu);
15886
15887 if (has_template_parameters)
15888 {
15889 struct symtab *symtab;
15890 if (sym != nullptr)
15891 symtab = symbol_symtab (sym);
15892 else if (cu->line_header != nullptr)
15893 {
15894 /* Any related symtab will do. */
15895 symtab
15896 = cu->line_header->file_names ()[0].symtab;
15897 }
15898 else
15899 {
15900 symtab = nullptr;
15901 complaint (_("could not find suitable "
15902 "symtab for template parameter"
15903 " - DIE at %s [in module %s]"),
15904 sect_offset_str (die->sect_off),
15905 objfile_name (objfile));
15906 }
15907
15908 if (symtab != nullptr)
15909 {
15910 /* Make sure that the symtab is set on the new symbols.
15911 Even though they don't appear in this symtab directly,
15912 other parts of gdb assume that symbols do, and this is
15913 reasonably true. */
15914 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15915 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15916 }
15917 }
15918 }
15919 }
15920
15921 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15922 update TYPE using some information only available in DIE's children. */
15923
15924 static void
15925 update_enumeration_type_from_children (struct die_info *die,
15926 struct type *type,
15927 struct dwarf2_cu *cu)
15928 {
15929 struct die_info *child_die;
15930 int unsigned_enum = 1;
15931 int flag_enum = 1;
15932 ULONGEST mask = 0;
15933
15934 auto_obstack obstack;
15935
15936 for (child_die = die->child;
15937 child_die != NULL && child_die->tag;
15938 child_die = sibling_die (child_die))
15939 {
15940 struct attribute *attr;
15941 LONGEST value;
15942 const gdb_byte *bytes;
15943 struct dwarf2_locexpr_baton *baton;
15944 const char *name;
15945
15946 if (child_die->tag != DW_TAG_enumerator)
15947 continue;
15948
15949 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15950 if (attr == NULL)
15951 continue;
15952
15953 name = dwarf2_name (child_die, cu);
15954 if (name == NULL)
15955 name = "<anonymous enumerator>";
15956
15957 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15958 &value, &bytes, &baton);
15959 if (value < 0)
15960 {
15961 unsigned_enum = 0;
15962 flag_enum = 0;
15963 }
15964 else if ((mask & value) != 0)
15965 flag_enum = 0;
15966 else
15967 mask |= value;
15968
15969 /* If we already know that the enum type is neither unsigned, nor
15970 a flag type, no need to look at the rest of the enumerates. */
15971 if (!unsigned_enum && !flag_enum)
15972 break;
15973 }
15974
15975 if (unsigned_enum)
15976 TYPE_UNSIGNED (type) = 1;
15977 if (flag_enum)
15978 TYPE_FLAG_ENUM (type) = 1;
15979 }
15980
15981 /* Given a DW_AT_enumeration_type die, set its type. We do not
15982 complete the type's fields yet, or create any symbols. */
15983
15984 static struct type *
15985 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15986 {
15987 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15988 struct type *type;
15989 struct attribute *attr;
15990 const char *name;
15991
15992 /* If the definition of this type lives in .debug_types, read that type.
15993 Don't follow DW_AT_specification though, that will take us back up
15994 the chain and we want to go down. */
15995 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15996 if (attr != nullptr)
15997 {
15998 type = get_DW_AT_signature_type (die, attr, cu);
15999
16000 /* The type's CU may not be the same as CU.
16001 Ensure TYPE is recorded with CU in die_type_hash. */
16002 return set_die_type (die, type, cu);
16003 }
16004
16005 type = alloc_type (objfile);
16006
16007 TYPE_CODE (type) = TYPE_CODE_ENUM;
16008 name = dwarf2_full_name (NULL, die, cu);
16009 if (name != NULL)
16010 TYPE_NAME (type) = name;
16011
16012 attr = dwarf2_attr (die, DW_AT_type, cu);
16013 if (attr != NULL)
16014 {
16015 struct type *underlying_type = die_type (die, cu);
16016
16017 TYPE_TARGET_TYPE (type) = underlying_type;
16018 }
16019
16020 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16021 if (attr != nullptr)
16022 {
16023 TYPE_LENGTH (type) = DW_UNSND (attr);
16024 }
16025 else
16026 {
16027 TYPE_LENGTH (type) = 0;
16028 }
16029
16030 maybe_set_alignment (cu, die, type);
16031
16032 /* The enumeration DIE can be incomplete. In Ada, any type can be
16033 declared as private in the package spec, and then defined only
16034 inside the package body. Such types are known as Taft Amendment
16035 Types. When another package uses such a type, an incomplete DIE
16036 may be generated by the compiler. */
16037 if (die_is_declaration (die, cu))
16038 TYPE_STUB (type) = 1;
16039
16040 /* Finish the creation of this type by using the enum's children.
16041 We must call this even when the underlying type has been provided
16042 so that we can determine if we're looking at a "flag" enum. */
16043 update_enumeration_type_from_children (die, type, cu);
16044
16045 /* If this type has an underlying type that is not a stub, then we
16046 may use its attributes. We always use the "unsigned" attribute
16047 in this situation, because ordinarily we guess whether the type
16048 is unsigned -- but the guess can be wrong and the underlying type
16049 can tell us the reality. However, we defer to a local size
16050 attribute if one exists, because this lets the compiler override
16051 the underlying type if needed. */
16052 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16053 {
16054 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16055 if (TYPE_LENGTH (type) == 0)
16056 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16057 if (TYPE_RAW_ALIGN (type) == 0
16058 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16059 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16060 }
16061
16062 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16063
16064 return set_die_type (die, type, cu);
16065 }
16066
16067 /* Given a pointer to a die which begins an enumeration, process all
16068 the dies that define the members of the enumeration, and create the
16069 symbol for the enumeration type.
16070
16071 NOTE: We reverse the order of the element list. */
16072
16073 static void
16074 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16075 {
16076 struct type *this_type;
16077
16078 this_type = get_die_type (die, cu);
16079 if (this_type == NULL)
16080 this_type = read_enumeration_type (die, cu);
16081
16082 if (die->child != NULL)
16083 {
16084 struct die_info *child_die;
16085 struct symbol *sym;
16086 std::vector<struct field> fields;
16087 const char *name;
16088
16089 child_die = die->child;
16090 while (child_die && child_die->tag)
16091 {
16092 if (child_die->tag != DW_TAG_enumerator)
16093 {
16094 process_die (child_die, cu);
16095 }
16096 else
16097 {
16098 name = dwarf2_name (child_die, cu);
16099 if (name)
16100 {
16101 sym = new_symbol (child_die, this_type, cu);
16102
16103 fields.emplace_back ();
16104 struct field &field = fields.back ();
16105
16106 FIELD_NAME (field) = sym->linkage_name ();
16107 FIELD_TYPE (field) = NULL;
16108 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16109 FIELD_BITSIZE (field) = 0;
16110 }
16111 }
16112
16113 child_die = sibling_die (child_die);
16114 }
16115
16116 if (!fields.empty ())
16117 {
16118 TYPE_NFIELDS (this_type) = fields.size ();
16119 TYPE_FIELDS (this_type) = (struct field *)
16120 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16121 memcpy (TYPE_FIELDS (this_type), fields.data (),
16122 sizeof (struct field) * fields.size ());
16123 }
16124 }
16125
16126 /* If we are reading an enum from a .debug_types unit, and the enum
16127 is a declaration, and the enum is not the signatured type in the
16128 unit, then we do not want to add a symbol for it. Adding a
16129 symbol would in some cases obscure the true definition of the
16130 enum, giving users an incomplete type when the definition is
16131 actually available. Note that we do not want to do this for all
16132 enums which are just declarations, because C++0x allows forward
16133 enum declarations. */
16134 if (cu->per_cu->is_debug_types
16135 && die_is_declaration (die, cu))
16136 {
16137 struct signatured_type *sig_type;
16138
16139 sig_type = (struct signatured_type *) cu->per_cu;
16140 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16141 if (sig_type->type_offset_in_section != die->sect_off)
16142 return;
16143 }
16144
16145 new_symbol (die, this_type, cu);
16146 }
16147
16148 /* Extract all information from a DW_TAG_array_type DIE and put it in
16149 the DIE's type field. For now, this only handles one dimensional
16150 arrays. */
16151
16152 static struct type *
16153 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16154 {
16155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16156 struct die_info *child_die;
16157 struct type *type;
16158 struct type *element_type, *range_type, *index_type;
16159 struct attribute *attr;
16160 const char *name;
16161 struct dynamic_prop *byte_stride_prop = NULL;
16162 unsigned int bit_stride = 0;
16163
16164 element_type = die_type (die, cu);
16165
16166 /* The die_type call above may have already set the type for this DIE. */
16167 type = get_die_type (die, cu);
16168 if (type)
16169 return type;
16170
16171 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16172 if (attr != NULL)
16173 {
16174 int stride_ok;
16175 struct type *prop_type
16176 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16177
16178 byte_stride_prop
16179 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16180 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16181 prop_type);
16182 if (!stride_ok)
16183 {
16184 complaint (_("unable to read array DW_AT_byte_stride "
16185 " - DIE at %s [in module %s]"),
16186 sect_offset_str (die->sect_off),
16187 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16188 /* Ignore this attribute. We will likely not be able to print
16189 arrays of this type correctly, but there is little we can do
16190 to help if we cannot read the attribute's value. */
16191 byte_stride_prop = NULL;
16192 }
16193 }
16194
16195 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16196 if (attr != NULL)
16197 bit_stride = DW_UNSND (attr);
16198
16199 /* Irix 6.2 native cc creates array types without children for
16200 arrays with unspecified length. */
16201 if (die->child == NULL)
16202 {
16203 index_type = objfile_type (objfile)->builtin_int;
16204 range_type = create_static_range_type (NULL, index_type, 0, -1);
16205 type = create_array_type_with_stride (NULL, element_type, range_type,
16206 byte_stride_prop, bit_stride);
16207 return set_die_type (die, type, cu);
16208 }
16209
16210 std::vector<struct type *> range_types;
16211 child_die = die->child;
16212 while (child_die && child_die->tag)
16213 {
16214 if (child_die->tag == DW_TAG_subrange_type)
16215 {
16216 struct type *child_type = read_type_die (child_die, cu);
16217
16218 if (child_type != NULL)
16219 {
16220 /* The range type was succesfully read. Save it for the
16221 array type creation. */
16222 range_types.push_back (child_type);
16223 }
16224 }
16225 child_die = sibling_die (child_die);
16226 }
16227
16228 /* Dwarf2 dimensions are output from left to right, create the
16229 necessary array types in backwards order. */
16230
16231 type = element_type;
16232
16233 if (read_array_order (die, cu) == DW_ORD_col_major)
16234 {
16235 int i = 0;
16236
16237 while (i < range_types.size ())
16238 type = create_array_type_with_stride (NULL, type, range_types[i++],
16239 byte_stride_prop, bit_stride);
16240 }
16241 else
16242 {
16243 size_t ndim = range_types.size ();
16244 while (ndim-- > 0)
16245 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16246 byte_stride_prop, bit_stride);
16247 }
16248
16249 /* Understand Dwarf2 support for vector types (like they occur on
16250 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16251 array type. This is not part of the Dwarf2/3 standard yet, but a
16252 custom vendor extension. The main difference between a regular
16253 array and the vector variant is that vectors are passed by value
16254 to functions. */
16255 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16256 if (attr != nullptr)
16257 make_vector_type (type);
16258
16259 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16260 implementation may choose to implement triple vectors using this
16261 attribute. */
16262 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16263 if (attr != nullptr)
16264 {
16265 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16266 TYPE_LENGTH (type) = DW_UNSND (attr);
16267 else
16268 complaint (_("DW_AT_byte_size for array type smaller "
16269 "than the total size of elements"));
16270 }
16271
16272 name = dwarf2_name (die, cu);
16273 if (name)
16274 TYPE_NAME (type) = name;
16275
16276 maybe_set_alignment (cu, die, type);
16277
16278 /* Install the type in the die. */
16279 set_die_type (die, type, cu);
16280
16281 /* set_die_type should be already done. */
16282 set_descriptive_type (type, die, cu);
16283
16284 return type;
16285 }
16286
16287 static enum dwarf_array_dim_ordering
16288 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16289 {
16290 struct attribute *attr;
16291
16292 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16293
16294 if (attr != nullptr)
16295 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16296
16297 /* GNU F77 is a special case, as at 08/2004 array type info is the
16298 opposite order to the dwarf2 specification, but data is still
16299 laid out as per normal fortran.
16300
16301 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16302 version checking. */
16303
16304 if (cu->language == language_fortran
16305 && cu->producer && strstr (cu->producer, "GNU F77"))
16306 {
16307 return DW_ORD_row_major;
16308 }
16309
16310 switch (cu->language_defn->la_array_ordering)
16311 {
16312 case array_column_major:
16313 return DW_ORD_col_major;
16314 case array_row_major:
16315 default:
16316 return DW_ORD_row_major;
16317 };
16318 }
16319
16320 /* Extract all information from a DW_TAG_set_type DIE and put it in
16321 the DIE's type field. */
16322
16323 static struct type *
16324 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16325 {
16326 struct type *domain_type, *set_type;
16327 struct attribute *attr;
16328
16329 domain_type = die_type (die, cu);
16330
16331 /* The die_type call above may have already set the type for this DIE. */
16332 set_type = get_die_type (die, cu);
16333 if (set_type)
16334 return set_type;
16335
16336 set_type = create_set_type (NULL, domain_type);
16337
16338 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16339 if (attr != nullptr)
16340 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16341
16342 maybe_set_alignment (cu, die, set_type);
16343
16344 return set_die_type (die, set_type, cu);
16345 }
16346
16347 /* A helper for read_common_block that creates a locexpr baton.
16348 SYM is the symbol which we are marking as computed.
16349 COMMON_DIE is the DIE for the common block.
16350 COMMON_LOC is the location expression attribute for the common
16351 block itself.
16352 MEMBER_LOC is the location expression attribute for the particular
16353 member of the common block that we are processing.
16354 CU is the CU from which the above come. */
16355
16356 static void
16357 mark_common_block_symbol_computed (struct symbol *sym,
16358 struct die_info *common_die,
16359 struct attribute *common_loc,
16360 struct attribute *member_loc,
16361 struct dwarf2_cu *cu)
16362 {
16363 struct dwarf2_per_objfile *dwarf2_per_objfile
16364 = cu->per_cu->dwarf2_per_objfile;
16365 struct objfile *objfile = dwarf2_per_objfile->objfile;
16366 struct dwarf2_locexpr_baton *baton;
16367 gdb_byte *ptr;
16368 unsigned int cu_off;
16369 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16370 LONGEST offset = 0;
16371
16372 gdb_assert (common_loc && member_loc);
16373 gdb_assert (common_loc->form_is_block ());
16374 gdb_assert (member_loc->form_is_block ()
16375 || member_loc->form_is_constant ());
16376
16377 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16378 baton->per_cu = cu->per_cu;
16379 gdb_assert (baton->per_cu);
16380
16381 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16382
16383 if (member_loc->form_is_constant ())
16384 {
16385 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16386 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16387 }
16388 else
16389 baton->size += DW_BLOCK (member_loc)->size;
16390
16391 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16392 baton->data = ptr;
16393
16394 *ptr++ = DW_OP_call4;
16395 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16396 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16397 ptr += 4;
16398
16399 if (member_loc->form_is_constant ())
16400 {
16401 *ptr++ = DW_OP_addr;
16402 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16403 ptr += cu->header.addr_size;
16404 }
16405 else
16406 {
16407 /* We have to copy the data here, because DW_OP_call4 will only
16408 use a DW_AT_location attribute. */
16409 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16410 ptr += DW_BLOCK (member_loc)->size;
16411 }
16412
16413 *ptr++ = DW_OP_plus;
16414 gdb_assert (ptr - baton->data == baton->size);
16415
16416 SYMBOL_LOCATION_BATON (sym) = baton;
16417 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16418 }
16419
16420 /* Create appropriate locally-scoped variables for all the
16421 DW_TAG_common_block entries. Also create a struct common_block
16422 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16423 is used to separate the common blocks name namespace from regular
16424 variable names. */
16425
16426 static void
16427 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16428 {
16429 struct attribute *attr;
16430
16431 attr = dwarf2_attr (die, DW_AT_location, cu);
16432 if (attr != nullptr)
16433 {
16434 /* Support the .debug_loc offsets. */
16435 if (attr->form_is_block ())
16436 {
16437 /* Ok. */
16438 }
16439 else if (attr->form_is_section_offset ())
16440 {
16441 dwarf2_complex_location_expr_complaint ();
16442 attr = NULL;
16443 }
16444 else
16445 {
16446 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16447 "common block member");
16448 attr = NULL;
16449 }
16450 }
16451
16452 if (die->child != NULL)
16453 {
16454 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16455 struct die_info *child_die;
16456 size_t n_entries = 0, size;
16457 struct common_block *common_block;
16458 struct symbol *sym;
16459
16460 for (child_die = die->child;
16461 child_die && child_die->tag;
16462 child_die = sibling_die (child_die))
16463 ++n_entries;
16464
16465 size = (sizeof (struct common_block)
16466 + (n_entries - 1) * sizeof (struct symbol *));
16467 common_block
16468 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16469 size);
16470 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16471 common_block->n_entries = 0;
16472
16473 for (child_die = die->child;
16474 child_die && child_die->tag;
16475 child_die = sibling_die (child_die))
16476 {
16477 /* Create the symbol in the DW_TAG_common_block block in the current
16478 symbol scope. */
16479 sym = new_symbol (child_die, NULL, cu);
16480 if (sym != NULL)
16481 {
16482 struct attribute *member_loc;
16483
16484 common_block->contents[common_block->n_entries++] = sym;
16485
16486 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16487 cu);
16488 if (member_loc)
16489 {
16490 /* GDB has handled this for a long time, but it is
16491 not specified by DWARF. It seems to have been
16492 emitted by gfortran at least as recently as:
16493 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16494 complaint (_("Variable in common block has "
16495 "DW_AT_data_member_location "
16496 "- DIE at %s [in module %s]"),
16497 sect_offset_str (child_die->sect_off),
16498 objfile_name (objfile));
16499
16500 if (member_loc->form_is_section_offset ())
16501 dwarf2_complex_location_expr_complaint ();
16502 else if (member_loc->form_is_constant ()
16503 || member_loc->form_is_block ())
16504 {
16505 if (attr != nullptr)
16506 mark_common_block_symbol_computed (sym, die, attr,
16507 member_loc, cu);
16508 }
16509 else
16510 dwarf2_complex_location_expr_complaint ();
16511 }
16512 }
16513 }
16514
16515 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16516 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16517 }
16518 }
16519
16520 /* Create a type for a C++ namespace. */
16521
16522 static struct type *
16523 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16524 {
16525 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16526 const char *previous_prefix, *name;
16527 int is_anonymous;
16528 struct type *type;
16529
16530 /* For extensions, reuse the type of the original namespace. */
16531 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16532 {
16533 struct die_info *ext_die;
16534 struct dwarf2_cu *ext_cu = cu;
16535
16536 ext_die = dwarf2_extension (die, &ext_cu);
16537 type = read_type_die (ext_die, ext_cu);
16538
16539 /* EXT_CU may not be the same as CU.
16540 Ensure TYPE is recorded with CU in die_type_hash. */
16541 return set_die_type (die, type, cu);
16542 }
16543
16544 name = namespace_name (die, &is_anonymous, cu);
16545
16546 /* Now build the name of the current namespace. */
16547
16548 previous_prefix = determine_prefix (die, cu);
16549 if (previous_prefix[0] != '\0')
16550 name = typename_concat (&objfile->objfile_obstack,
16551 previous_prefix, name, 0, cu);
16552
16553 /* Create the type. */
16554 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16555
16556 return set_die_type (die, type, cu);
16557 }
16558
16559 /* Read a namespace scope. */
16560
16561 static void
16562 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16563 {
16564 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16565 int is_anonymous;
16566
16567 /* Add a symbol associated to this if we haven't seen the namespace
16568 before. Also, add a using directive if it's an anonymous
16569 namespace. */
16570
16571 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16572 {
16573 struct type *type;
16574
16575 type = read_type_die (die, cu);
16576 new_symbol (die, type, cu);
16577
16578 namespace_name (die, &is_anonymous, cu);
16579 if (is_anonymous)
16580 {
16581 const char *previous_prefix = determine_prefix (die, cu);
16582
16583 std::vector<const char *> excludes;
16584 add_using_directive (using_directives (cu),
16585 previous_prefix, TYPE_NAME (type), NULL,
16586 NULL, excludes, 0, &objfile->objfile_obstack);
16587 }
16588 }
16589
16590 if (die->child != NULL)
16591 {
16592 struct die_info *child_die = die->child;
16593
16594 while (child_die && child_die->tag)
16595 {
16596 process_die (child_die, cu);
16597 child_die = sibling_die (child_die);
16598 }
16599 }
16600 }
16601
16602 /* Read a Fortran module as type. This DIE can be only a declaration used for
16603 imported module. Still we need that type as local Fortran "use ... only"
16604 declaration imports depend on the created type in determine_prefix. */
16605
16606 static struct type *
16607 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16608 {
16609 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16610 const char *module_name;
16611 struct type *type;
16612
16613 module_name = dwarf2_name (die, cu);
16614 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16615
16616 return set_die_type (die, type, cu);
16617 }
16618
16619 /* Read a Fortran module. */
16620
16621 static void
16622 read_module (struct die_info *die, struct dwarf2_cu *cu)
16623 {
16624 struct die_info *child_die = die->child;
16625 struct type *type;
16626
16627 type = read_type_die (die, cu);
16628 new_symbol (die, type, cu);
16629
16630 while (child_die && child_die->tag)
16631 {
16632 process_die (child_die, cu);
16633 child_die = sibling_die (child_die);
16634 }
16635 }
16636
16637 /* Return the name of the namespace represented by DIE. Set
16638 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16639 namespace. */
16640
16641 static const char *
16642 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16643 {
16644 struct die_info *current_die;
16645 const char *name = NULL;
16646
16647 /* Loop through the extensions until we find a name. */
16648
16649 for (current_die = die;
16650 current_die != NULL;
16651 current_die = dwarf2_extension (die, &cu))
16652 {
16653 /* We don't use dwarf2_name here so that we can detect the absence
16654 of a name -> anonymous namespace. */
16655 name = dwarf2_string_attr (die, DW_AT_name, cu);
16656
16657 if (name != NULL)
16658 break;
16659 }
16660
16661 /* Is it an anonymous namespace? */
16662
16663 *is_anonymous = (name == NULL);
16664 if (*is_anonymous)
16665 name = CP_ANONYMOUS_NAMESPACE_STR;
16666
16667 return name;
16668 }
16669
16670 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16671 the user defined type vector. */
16672
16673 static struct type *
16674 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16675 {
16676 struct gdbarch *gdbarch
16677 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16678 struct comp_unit_head *cu_header = &cu->header;
16679 struct type *type;
16680 struct attribute *attr_byte_size;
16681 struct attribute *attr_address_class;
16682 int byte_size, addr_class;
16683 struct type *target_type;
16684
16685 target_type = die_type (die, cu);
16686
16687 /* The die_type call above may have already set the type for this DIE. */
16688 type = get_die_type (die, cu);
16689 if (type)
16690 return type;
16691
16692 type = lookup_pointer_type (target_type);
16693
16694 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16695 if (attr_byte_size)
16696 byte_size = DW_UNSND (attr_byte_size);
16697 else
16698 byte_size = cu_header->addr_size;
16699
16700 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16701 if (attr_address_class)
16702 addr_class = DW_UNSND (attr_address_class);
16703 else
16704 addr_class = DW_ADDR_none;
16705
16706 ULONGEST alignment = get_alignment (cu, die);
16707
16708 /* If the pointer size, alignment, or address class is different
16709 than the default, create a type variant marked as such and set
16710 the length accordingly. */
16711 if (TYPE_LENGTH (type) != byte_size
16712 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16713 && alignment != TYPE_RAW_ALIGN (type))
16714 || addr_class != DW_ADDR_none)
16715 {
16716 if (gdbarch_address_class_type_flags_p (gdbarch))
16717 {
16718 int type_flags;
16719
16720 type_flags = gdbarch_address_class_type_flags
16721 (gdbarch, byte_size, addr_class);
16722 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16723 == 0);
16724 type = make_type_with_address_space (type, type_flags);
16725 }
16726 else if (TYPE_LENGTH (type) != byte_size)
16727 {
16728 complaint (_("invalid pointer size %d"), byte_size);
16729 }
16730 else if (TYPE_RAW_ALIGN (type) != alignment)
16731 {
16732 complaint (_("Invalid DW_AT_alignment"
16733 " - DIE at %s [in module %s]"),
16734 sect_offset_str (die->sect_off),
16735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16736 }
16737 else
16738 {
16739 /* Should we also complain about unhandled address classes? */
16740 }
16741 }
16742
16743 TYPE_LENGTH (type) = byte_size;
16744 set_type_align (type, alignment);
16745 return set_die_type (die, type, cu);
16746 }
16747
16748 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16749 the user defined type vector. */
16750
16751 static struct type *
16752 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16753 {
16754 struct type *type;
16755 struct type *to_type;
16756 struct type *domain;
16757
16758 to_type = die_type (die, cu);
16759 domain = die_containing_type (die, cu);
16760
16761 /* The calls above may have already set the type for this DIE. */
16762 type = get_die_type (die, cu);
16763 if (type)
16764 return type;
16765
16766 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16767 type = lookup_methodptr_type (to_type);
16768 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16769 {
16770 struct type *new_type
16771 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16772
16773 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16774 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16775 TYPE_VARARGS (to_type));
16776 type = lookup_methodptr_type (new_type);
16777 }
16778 else
16779 type = lookup_memberptr_type (to_type, domain);
16780
16781 return set_die_type (die, type, cu);
16782 }
16783
16784 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16785 the user defined type vector. */
16786
16787 static struct type *
16788 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16789 enum type_code refcode)
16790 {
16791 struct comp_unit_head *cu_header = &cu->header;
16792 struct type *type, *target_type;
16793 struct attribute *attr;
16794
16795 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16796
16797 target_type = die_type (die, cu);
16798
16799 /* The die_type call above may have already set the type for this DIE. */
16800 type = get_die_type (die, cu);
16801 if (type)
16802 return type;
16803
16804 type = lookup_reference_type (target_type, refcode);
16805 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16806 if (attr != nullptr)
16807 {
16808 TYPE_LENGTH (type) = DW_UNSND (attr);
16809 }
16810 else
16811 {
16812 TYPE_LENGTH (type) = cu_header->addr_size;
16813 }
16814 maybe_set_alignment (cu, die, type);
16815 return set_die_type (die, type, cu);
16816 }
16817
16818 /* Add the given cv-qualifiers to the element type of the array. GCC
16819 outputs DWARF type qualifiers that apply to an array, not the
16820 element type. But GDB relies on the array element type to carry
16821 the cv-qualifiers. This mimics section 6.7.3 of the C99
16822 specification. */
16823
16824 static struct type *
16825 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16826 struct type *base_type, int cnst, int voltl)
16827 {
16828 struct type *el_type, *inner_array;
16829
16830 base_type = copy_type (base_type);
16831 inner_array = base_type;
16832
16833 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16834 {
16835 TYPE_TARGET_TYPE (inner_array) =
16836 copy_type (TYPE_TARGET_TYPE (inner_array));
16837 inner_array = TYPE_TARGET_TYPE (inner_array);
16838 }
16839
16840 el_type = TYPE_TARGET_TYPE (inner_array);
16841 cnst |= TYPE_CONST (el_type);
16842 voltl |= TYPE_VOLATILE (el_type);
16843 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16844
16845 return set_die_type (die, base_type, cu);
16846 }
16847
16848 static struct type *
16849 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16850 {
16851 struct type *base_type, *cv_type;
16852
16853 base_type = die_type (die, cu);
16854
16855 /* The die_type call above may have already set the type for this DIE. */
16856 cv_type = get_die_type (die, cu);
16857 if (cv_type)
16858 return cv_type;
16859
16860 /* In case the const qualifier is applied to an array type, the element type
16861 is so qualified, not the array type (section 6.7.3 of C99). */
16862 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16863 return add_array_cv_type (die, cu, base_type, 1, 0);
16864
16865 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16866 return set_die_type (die, cv_type, cu);
16867 }
16868
16869 static struct type *
16870 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16871 {
16872 struct type *base_type, *cv_type;
16873
16874 base_type = die_type (die, cu);
16875
16876 /* The die_type call above may have already set the type for this DIE. */
16877 cv_type = get_die_type (die, cu);
16878 if (cv_type)
16879 return cv_type;
16880
16881 /* In case the volatile qualifier is applied to an array type, the
16882 element type is so qualified, not the array type (section 6.7.3
16883 of C99). */
16884 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16885 return add_array_cv_type (die, cu, base_type, 0, 1);
16886
16887 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16888 return set_die_type (die, cv_type, cu);
16889 }
16890
16891 /* Handle DW_TAG_restrict_type. */
16892
16893 static struct type *
16894 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16895 {
16896 struct type *base_type, *cv_type;
16897
16898 base_type = die_type (die, cu);
16899
16900 /* The die_type call above may have already set the type for this DIE. */
16901 cv_type = get_die_type (die, cu);
16902 if (cv_type)
16903 return cv_type;
16904
16905 cv_type = make_restrict_type (base_type);
16906 return set_die_type (die, cv_type, cu);
16907 }
16908
16909 /* Handle DW_TAG_atomic_type. */
16910
16911 static struct type *
16912 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16913 {
16914 struct type *base_type, *cv_type;
16915
16916 base_type = die_type (die, cu);
16917
16918 /* The die_type call above may have already set the type for this DIE. */
16919 cv_type = get_die_type (die, cu);
16920 if (cv_type)
16921 return cv_type;
16922
16923 cv_type = make_atomic_type (base_type);
16924 return set_die_type (die, cv_type, cu);
16925 }
16926
16927 /* Extract all information from a DW_TAG_string_type DIE and add to
16928 the user defined type vector. It isn't really a user defined type,
16929 but it behaves like one, with other DIE's using an AT_user_def_type
16930 attribute to reference it. */
16931
16932 static struct type *
16933 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16934 {
16935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16936 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16937 struct type *type, *range_type, *index_type, *char_type;
16938 struct attribute *attr;
16939 struct dynamic_prop prop;
16940 bool length_is_constant = true;
16941 LONGEST length;
16942
16943 /* There are a couple of places where bit sizes might be made use of
16944 when parsing a DW_TAG_string_type, however, no producer that we know
16945 of make use of these. Handling bit sizes that are a multiple of the
16946 byte size is easy enough, but what about other bit sizes? Lets deal
16947 with that problem when we have to. Warn about these attributes being
16948 unsupported, then parse the type and ignore them like we always
16949 have. */
16950 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16951 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16952 {
16953 static bool warning_printed = false;
16954 if (!warning_printed)
16955 {
16956 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16957 "currently supported on DW_TAG_string_type."));
16958 warning_printed = true;
16959 }
16960 }
16961
16962 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16963 if (attr != nullptr && !attr->form_is_constant ())
16964 {
16965 /* The string length describes the location at which the length of
16966 the string can be found. The size of the length field can be
16967 specified with one of the attributes below. */
16968 struct type *prop_type;
16969 struct attribute *len
16970 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16971 if (len == nullptr)
16972 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16973 if (len != nullptr && len->form_is_constant ())
16974 {
16975 /* Pass 0 as the default as we know this attribute is constant
16976 and the default value will not be returned. */
16977 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16978 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
16979 }
16980 else
16981 {
16982 /* If the size is not specified then we assume it is the size of
16983 an address on this target. */
16984 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
16985 }
16986
16987 /* Convert the attribute into a dynamic property. */
16988 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16989 length = 1;
16990 else
16991 length_is_constant = false;
16992 }
16993 else if (attr != nullptr)
16994 {
16995 /* This DW_AT_string_length just contains the length with no
16996 indirection. There's no need to create a dynamic property in this
16997 case. Pass 0 for the default value as we know it will not be
16998 returned in this case. */
16999 length = dwarf2_get_attr_constant_value (attr, 0);
17000 }
17001 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17002 {
17003 /* We don't currently support non-constant byte sizes for strings. */
17004 length = dwarf2_get_attr_constant_value (attr, 1);
17005 }
17006 else
17007 {
17008 /* Use 1 as a fallback length if we have nothing else. */
17009 length = 1;
17010 }
17011
17012 index_type = objfile_type (objfile)->builtin_int;
17013 if (length_is_constant)
17014 range_type = create_static_range_type (NULL, index_type, 1, length);
17015 else
17016 {
17017 struct dynamic_prop low_bound;
17018
17019 low_bound.kind = PROP_CONST;
17020 low_bound.data.const_val = 1;
17021 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17022 }
17023 char_type = language_string_char_type (cu->language_defn, gdbarch);
17024 type = create_string_type (NULL, char_type, range_type);
17025
17026 return set_die_type (die, type, cu);
17027 }
17028
17029 /* Assuming that DIE corresponds to a function, returns nonzero
17030 if the function is prototyped. */
17031
17032 static int
17033 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17034 {
17035 struct attribute *attr;
17036
17037 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17038 if (attr && (DW_UNSND (attr) != 0))
17039 return 1;
17040
17041 /* The DWARF standard implies that the DW_AT_prototyped attribute
17042 is only meaningful for C, but the concept also extends to other
17043 languages that allow unprototyped functions (Eg: Objective C).
17044 For all other languages, assume that functions are always
17045 prototyped. */
17046 if (cu->language != language_c
17047 && cu->language != language_objc
17048 && cu->language != language_opencl)
17049 return 1;
17050
17051 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17052 prototyped and unprototyped functions; default to prototyped,
17053 since that is more common in modern code (and RealView warns
17054 about unprototyped functions). */
17055 if (producer_is_realview (cu->producer))
17056 return 1;
17057
17058 return 0;
17059 }
17060
17061 /* Handle DIES due to C code like:
17062
17063 struct foo
17064 {
17065 int (*funcp)(int a, long l);
17066 int b;
17067 };
17068
17069 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17070
17071 static struct type *
17072 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17073 {
17074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17075 struct type *type; /* Type that this function returns. */
17076 struct type *ftype; /* Function that returns above type. */
17077 struct attribute *attr;
17078
17079 type = die_type (die, cu);
17080
17081 /* The die_type call above may have already set the type for this DIE. */
17082 ftype = get_die_type (die, cu);
17083 if (ftype)
17084 return ftype;
17085
17086 ftype = lookup_function_type (type);
17087
17088 if (prototyped_function_p (die, cu))
17089 TYPE_PROTOTYPED (ftype) = 1;
17090
17091 /* Store the calling convention in the type if it's available in
17092 the subroutine die. Otherwise set the calling convention to
17093 the default value DW_CC_normal. */
17094 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17095 if (attr != nullptr
17096 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17097 TYPE_CALLING_CONVENTION (ftype)
17098 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17099 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17100 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17101 else
17102 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17103
17104 /* Record whether the function returns normally to its caller or not
17105 if the DWARF producer set that information. */
17106 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17107 if (attr && (DW_UNSND (attr) != 0))
17108 TYPE_NO_RETURN (ftype) = 1;
17109
17110 /* We need to add the subroutine type to the die immediately so
17111 we don't infinitely recurse when dealing with parameters
17112 declared as the same subroutine type. */
17113 set_die_type (die, ftype, cu);
17114
17115 if (die->child != NULL)
17116 {
17117 struct type *void_type = objfile_type (objfile)->builtin_void;
17118 struct die_info *child_die;
17119 int nparams, iparams;
17120
17121 /* Count the number of parameters.
17122 FIXME: GDB currently ignores vararg functions, but knows about
17123 vararg member functions. */
17124 nparams = 0;
17125 child_die = die->child;
17126 while (child_die && child_die->tag)
17127 {
17128 if (child_die->tag == DW_TAG_formal_parameter)
17129 nparams++;
17130 else if (child_die->tag == DW_TAG_unspecified_parameters)
17131 TYPE_VARARGS (ftype) = 1;
17132 child_die = sibling_die (child_die);
17133 }
17134
17135 /* Allocate storage for parameters and fill them in. */
17136 TYPE_NFIELDS (ftype) = nparams;
17137 TYPE_FIELDS (ftype) = (struct field *)
17138 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17139
17140 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17141 even if we error out during the parameters reading below. */
17142 for (iparams = 0; iparams < nparams; iparams++)
17143 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17144
17145 iparams = 0;
17146 child_die = die->child;
17147 while (child_die && child_die->tag)
17148 {
17149 if (child_die->tag == DW_TAG_formal_parameter)
17150 {
17151 struct type *arg_type;
17152
17153 /* DWARF version 2 has no clean way to discern C++
17154 static and non-static member functions. G++ helps
17155 GDB by marking the first parameter for non-static
17156 member functions (which is the this pointer) as
17157 artificial. We pass this information to
17158 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17159
17160 DWARF version 3 added DW_AT_object_pointer, which GCC
17161 4.5 does not yet generate. */
17162 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17163 if (attr != nullptr)
17164 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17165 else
17166 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17167 arg_type = die_type (child_die, cu);
17168
17169 /* RealView does not mark THIS as const, which the testsuite
17170 expects. GCC marks THIS as const in method definitions,
17171 but not in the class specifications (GCC PR 43053). */
17172 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17173 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17174 {
17175 int is_this = 0;
17176 struct dwarf2_cu *arg_cu = cu;
17177 const char *name = dwarf2_name (child_die, cu);
17178
17179 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17180 if (attr != nullptr)
17181 {
17182 /* If the compiler emits this, use it. */
17183 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17184 is_this = 1;
17185 }
17186 else if (name && strcmp (name, "this") == 0)
17187 /* Function definitions will have the argument names. */
17188 is_this = 1;
17189 else if (name == NULL && iparams == 0)
17190 /* Declarations may not have the names, so like
17191 elsewhere in GDB, assume an artificial first
17192 argument is "this". */
17193 is_this = 1;
17194
17195 if (is_this)
17196 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17197 arg_type, 0);
17198 }
17199
17200 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17201 iparams++;
17202 }
17203 child_die = sibling_die (child_die);
17204 }
17205 }
17206
17207 return ftype;
17208 }
17209
17210 static struct type *
17211 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17212 {
17213 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17214 const char *name = NULL;
17215 struct type *this_type, *target_type;
17216
17217 name = dwarf2_full_name (NULL, die, cu);
17218 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17219 TYPE_TARGET_STUB (this_type) = 1;
17220 set_die_type (die, this_type, cu);
17221 target_type = die_type (die, cu);
17222 if (target_type != this_type)
17223 TYPE_TARGET_TYPE (this_type) = target_type;
17224 else
17225 {
17226 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17227 spec and cause infinite loops in GDB. */
17228 complaint (_("Self-referential DW_TAG_typedef "
17229 "- DIE at %s [in module %s]"),
17230 sect_offset_str (die->sect_off), objfile_name (objfile));
17231 TYPE_TARGET_TYPE (this_type) = NULL;
17232 }
17233 return this_type;
17234 }
17235
17236 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17237 (which may be different from NAME) to the architecture back-end to allow
17238 it to guess the correct format if necessary. */
17239
17240 static struct type *
17241 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17242 const char *name_hint, enum bfd_endian byte_order)
17243 {
17244 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17245 const struct floatformat **format;
17246 struct type *type;
17247
17248 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17249 if (format)
17250 type = init_float_type (objfile, bits, name, format, byte_order);
17251 else
17252 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17253
17254 return type;
17255 }
17256
17257 /* Allocate an integer type of size BITS and name NAME. */
17258
17259 static struct type *
17260 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17261 int bits, int unsigned_p, const char *name)
17262 {
17263 struct type *type;
17264
17265 /* Versions of Intel's C Compiler generate an integer type called "void"
17266 instead of using DW_TAG_unspecified_type. This has been seen on
17267 at least versions 14, 17, and 18. */
17268 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17269 && strcmp (name, "void") == 0)
17270 type = objfile_type (objfile)->builtin_void;
17271 else
17272 type = init_integer_type (objfile, bits, unsigned_p, name);
17273
17274 return type;
17275 }
17276
17277 /* Initialise and return a floating point type of size BITS suitable for
17278 use as a component of a complex number. The NAME_HINT is passed through
17279 when initialising the floating point type and is the name of the complex
17280 type.
17281
17282 As DWARF doesn't currently provide an explicit name for the components
17283 of a complex number, but it can be helpful to have these components
17284 named, we try to select a suitable name based on the size of the
17285 component. */
17286 static struct type *
17287 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17288 struct objfile *objfile,
17289 int bits, const char *name_hint,
17290 enum bfd_endian byte_order)
17291 {
17292 gdbarch *gdbarch = get_objfile_arch (objfile);
17293 struct type *tt = nullptr;
17294
17295 /* Try to find a suitable floating point builtin type of size BITS.
17296 We're going to use the name of this type as the name for the complex
17297 target type that we are about to create. */
17298 switch (cu->language)
17299 {
17300 case language_fortran:
17301 switch (bits)
17302 {
17303 case 32:
17304 tt = builtin_f_type (gdbarch)->builtin_real;
17305 break;
17306 case 64:
17307 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17308 break;
17309 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17310 case 128:
17311 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17312 break;
17313 }
17314 break;
17315 default:
17316 switch (bits)
17317 {
17318 case 32:
17319 tt = builtin_type (gdbarch)->builtin_float;
17320 break;
17321 case 64:
17322 tt = builtin_type (gdbarch)->builtin_double;
17323 break;
17324 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17325 case 128:
17326 tt = builtin_type (gdbarch)->builtin_long_double;
17327 break;
17328 }
17329 break;
17330 }
17331
17332 /* If the type we found doesn't match the size we were looking for, then
17333 pretend we didn't find a type at all, the complex target type we
17334 create will then be nameless. */
17335 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17336 tt = nullptr;
17337
17338 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17339 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17340 }
17341
17342 /* Find a representation of a given base type and install
17343 it in the TYPE field of the die. */
17344
17345 static struct type *
17346 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17347 {
17348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17349 struct type *type;
17350 struct attribute *attr;
17351 int encoding = 0, bits = 0;
17352 const char *name;
17353 gdbarch *arch;
17354
17355 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17356 if (attr != nullptr)
17357 encoding = DW_UNSND (attr);
17358 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17359 if (attr != nullptr)
17360 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17361 name = dwarf2_name (die, cu);
17362 if (!name)
17363 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17364
17365 arch = get_objfile_arch (objfile);
17366 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17367
17368 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17369 if (attr)
17370 {
17371 int endianity = DW_UNSND (attr);
17372
17373 switch (endianity)
17374 {
17375 case DW_END_big:
17376 byte_order = BFD_ENDIAN_BIG;
17377 break;
17378 case DW_END_little:
17379 byte_order = BFD_ENDIAN_LITTLE;
17380 break;
17381 default:
17382 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17383 break;
17384 }
17385 }
17386
17387 switch (encoding)
17388 {
17389 case DW_ATE_address:
17390 /* Turn DW_ATE_address into a void * pointer. */
17391 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17392 type = init_pointer_type (objfile, bits, name, type);
17393 break;
17394 case DW_ATE_boolean:
17395 type = init_boolean_type (objfile, bits, 1, name);
17396 break;
17397 case DW_ATE_complex_float:
17398 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17399 byte_order);
17400 type = init_complex_type (objfile, name, type);
17401 break;
17402 case DW_ATE_decimal_float:
17403 type = init_decfloat_type (objfile, bits, name);
17404 break;
17405 case DW_ATE_float:
17406 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17407 break;
17408 case DW_ATE_signed:
17409 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17410 break;
17411 case DW_ATE_unsigned:
17412 if (cu->language == language_fortran
17413 && name
17414 && startswith (name, "character("))
17415 type = init_character_type (objfile, bits, 1, name);
17416 else
17417 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17418 break;
17419 case DW_ATE_signed_char:
17420 if (cu->language == language_ada || cu->language == language_m2
17421 || cu->language == language_pascal
17422 || cu->language == language_fortran)
17423 type = init_character_type (objfile, bits, 0, name);
17424 else
17425 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17426 break;
17427 case DW_ATE_unsigned_char:
17428 if (cu->language == language_ada || cu->language == language_m2
17429 || cu->language == language_pascal
17430 || cu->language == language_fortran
17431 || cu->language == language_rust)
17432 type = init_character_type (objfile, bits, 1, name);
17433 else
17434 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17435 break;
17436 case DW_ATE_UTF:
17437 {
17438 if (bits == 16)
17439 type = builtin_type (arch)->builtin_char16;
17440 else if (bits == 32)
17441 type = builtin_type (arch)->builtin_char32;
17442 else
17443 {
17444 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17445 bits);
17446 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17447 }
17448 return set_die_type (die, type, cu);
17449 }
17450 break;
17451
17452 default:
17453 complaint (_("unsupported DW_AT_encoding: '%s'"),
17454 dwarf_type_encoding_name (encoding));
17455 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17456 break;
17457 }
17458
17459 if (name && strcmp (name, "char") == 0)
17460 TYPE_NOSIGN (type) = 1;
17461
17462 maybe_set_alignment (cu, die, type);
17463
17464 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17465
17466 return set_die_type (die, type, cu);
17467 }
17468
17469 /* Parse dwarf attribute if it's a block, reference or constant and put the
17470 resulting value of the attribute into struct bound_prop.
17471 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17472
17473 static int
17474 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17475 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17476 struct type *default_type)
17477 {
17478 struct dwarf2_property_baton *baton;
17479 struct obstack *obstack
17480 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17481
17482 gdb_assert (default_type != NULL);
17483
17484 if (attr == NULL || prop == NULL)
17485 return 0;
17486
17487 if (attr->form_is_block ())
17488 {
17489 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17490 baton->property_type = default_type;
17491 baton->locexpr.per_cu = cu->per_cu;
17492 baton->locexpr.size = DW_BLOCK (attr)->size;
17493 baton->locexpr.data = DW_BLOCK (attr)->data;
17494 switch (attr->name)
17495 {
17496 case DW_AT_string_length:
17497 baton->locexpr.is_reference = true;
17498 break;
17499 default:
17500 baton->locexpr.is_reference = false;
17501 break;
17502 }
17503 prop->data.baton = baton;
17504 prop->kind = PROP_LOCEXPR;
17505 gdb_assert (prop->data.baton != NULL);
17506 }
17507 else if (attr->form_is_ref ())
17508 {
17509 struct dwarf2_cu *target_cu = cu;
17510 struct die_info *target_die;
17511 struct attribute *target_attr;
17512
17513 target_die = follow_die_ref (die, attr, &target_cu);
17514 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17515 if (target_attr == NULL)
17516 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17517 target_cu);
17518 if (target_attr == NULL)
17519 return 0;
17520
17521 switch (target_attr->name)
17522 {
17523 case DW_AT_location:
17524 if (target_attr->form_is_section_offset ())
17525 {
17526 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17527 baton->property_type = die_type (target_die, target_cu);
17528 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17529 prop->data.baton = baton;
17530 prop->kind = PROP_LOCLIST;
17531 gdb_assert (prop->data.baton != NULL);
17532 }
17533 else if (target_attr->form_is_block ())
17534 {
17535 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17536 baton->property_type = die_type (target_die, target_cu);
17537 baton->locexpr.per_cu = cu->per_cu;
17538 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17539 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17540 baton->locexpr.is_reference = true;
17541 prop->data.baton = baton;
17542 prop->kind = PROP_LOCEXPR;
17543 gdb_assert (prop->data.baton != NULL);
17544 }
17545 else
17546 {
17547 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17548 "dynamic property");
17549 return 0;
17550 }
17551 break;
17552 case DW_AT_data_member_location:
17553 {
17554 LONGEST offset;
17555
17556 if (!handle_data_member_location (target_die, target_cu,
17557 &offset))
17558 return 0;
17559
17560 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17561 baton->property_type = read_type_die (target_die->parent,
17562 target_cu);
17563 baton->offset_info.offset = offset;
17564 baton->offset_info.type = die_type (target_die, target_cu);
17565 prop->data.baton = baton;
17566 prop->kind = PROP_ADDR_OFFSET;
17567 break;
17568 }
17569 }
17570 }
17571 else if (attr->form_is_constant ())
17572 {
17573 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17574 prop->kind = PROP_CONST;
17575 }
17576 else
17577 {
17578 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17579 dwarf2_name (die, cu));
17580 return 0;
17581 }
17582
17583 return 1;
17584 }
17585
17586 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17587 UNSIGNED_P controls if the integer is unsigned or not. */
17588
17589 static struct type *
17590 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17591 int size_in_bytes, bool unsigned_p)
17592 {
17593 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17594 struct type *int_type;
17595
17596 /* Helper macro to examine the various builtin types. */
17597 #define TRY_TYPE(F) \
17598 int_type = (unsigned_p \
17599 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17600 : objfile_type (objfile)->builtin_ ## F); \
17601 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17602 return int_type
17603
17604 TRY_TYPE (char);
17605 TRY_TYPE (short);
17606 TRY_TYPE (int);
17607 TRY_TYPE (long);
17608 TRY_TYPE (long_long);
17609
17610 #undef TRY_TYPE
17611
17612 gdb_assert_not_reached ("unable to find suitable integer type");
17613 }
17614
17615 /* Find an integer type the same size as the address size given in the
17616 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17617 is unsigned or not. */
17618
17619 static struct type *
17620 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17621 bool unsigned_p)
17622 {
17623 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17624 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17625 }
17626
17627 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17628 present (which is valid) then compute the default type based on the
17629 compilation units address size. */
17630
17631 static struct type *
17632 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17633 {
17634 struct type *index_type = die_type (die, cu);
17635
17636 /* Dwarf-2 specifications explicitly allows to create subrange types
17637 without specifying a base type.
17638 In that case, the base type must be set to the type of
17639 the lower bound, upper bound or count, in that order, if any of these
17640 three attributes references an object that has a type.
17641 If no base type is found, the Dwarf-2 specifications say that
17642 a signed integer type of size equal to the size of an address should
17643 be used.
17644 For the following C code: `extern char gdb_int [];'
17645 GCC produces an empty range DIE.
17646 FIXME: muller/2010-05-28: Possible references to object for low bound,
17647 high bound or count are not yet handled by this code. */
17648 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17649 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17650
17651 return index_type;
17652 }
17653
17654 /* Read the given DW_AT_subrange DIE. */
17655
17656 static struct type *
17657 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17658 {
17659 struct type *base_type, *orig_base_type;
17660 struct type *range_type;
17661 struct attribute *attr;
17662 struct dynamic_prop low, high;
17663 int low_default_is_valid;
17664 int high_bound_is_count = 0;
17665 const char *name;
17666 ULONGEST negative_mask;
17667
17668 orig_base_type = read_subrange_index_type (die, cu);
17669
17670 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17671 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17672 creating the range type, but we use the result of check_typedef
17673 when examining properties of the type. */
17674 base_type = check_typedef (orig_base_type);
17675
17676 /* The die_type call above may have already set the type for this DIE. */
17677 range_type = get_die_type (die, cu);
17678 if (range_type)
17679 return range_type;
17680
17681 low.kind = PROP_CONST;
17682 high.kind = PROP_CONST;
17683 high.data.const_val = 0;
17684
17685 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17686 omitting DW_AT_lower_bound. */
17687 switch (cu->language)
17688 {
17689 case language_c:
17690 case language_cplus:
17691 low.data.const_val = 0;
17692 low_default_is_valid = 1;
17693 break;
17694 case language_fortran:
17695 low.data.const_val = 1;
17696 low_default_is_valid = 1;
17697 break;
17698 case language_d:
17699 case language_objc:
17700 case language_rust:
17701 low.data.const_val = 0;
17702 low_default_is_valid = (cu->header.version >= 4);
17703 break;
17704 case language_ada:
17705 case language_m2:
17706 case language_pascal:
17707 low.data.const_val = 1;
17708 low_default_is_valid = (cu->header.version >= 4);
17709 break;
17710 default:
17711 low.data.const_val = 0;
17712 low_default_is_valid = 0;
17713 break;
17714 }
17715
17716 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17717 if (attr != nullptr)
17718 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17719 else if (!low_default_is_valid)
17720 complaint (_("Missing DW_AT_lower_bound "
17721 "- DIE at %s [in module %s]"),
17722 sect_offset_str (die->sect_off),
17723 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17724
17725 struct attribute *attr_ub, *attr_count;
17726 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17727 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17728 {
17729 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17730 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17731 {
17732 /* If bounds are constant do the final calculation here. */
17733 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17734 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17735 else
17736 high_bound_is_count = 1;
17737 }
17738 else
17739 {
17740 if (attr_ub != NULL)
17741 complaint (_("Unresolved DW_AT_upper_bound "
17742 "- DIE at %s [in module %s]"),
17743 sect_offset_str (die->sect_off),
17744 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17745 if (attr_count != NULL)
17746 complaint (_("Unresolved DW_AT_count "
17747 "- DIE at %s [in module %s]"),
17748 sect_offset_str (die->sect_off),
17749 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17750 }
17751 }
17752
17753 LONGEST bias = 0;
17754 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17755 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17756 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17757
17758 /* Normally, the DWARF producers are expected to use a signed
17759 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17760 But this is unfortunately not always the case, as witnessed
17761 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17762 is used instead. To work around that ambiguity, we treat
17763 the bounds as signed, and thus sign-extend their values, when
17764 the base type is signed. */
17765 negative_mask =
17766 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17767 if (low.kind == PROP_CONST
17768 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17769 low.data.const_val |= negative_mask;
17770 if (high.kind == PROP_CONST
17771 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17772 high.data.const_val |= negative_mask;
17773
17774 /* Check for bit and byte strides. */
17775 struct dynamic_prop byte_stride_prop;
17776 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17777 if (attr_byte_stride != nullptr)
17778 {
17779 struct type *prop_type
17780 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17781 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17782 prop_type);
17783 }
17784
17785 struct dynamic_prop bit_stride_prop;
17786 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17787 if (attr_bit_stride != nullptr)
17788 {
17789 /* It only makes sense to have either a bit or byte stride. */
17790 if (attr_byte_stride != nullptr)
17791 {
17792 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17793 "- DIE at %s [in module %s]"),
17794 sect_offset_str (die->sect_off),
17795 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17796 attr_bit_stride = nullptr;
17797 }
17798 else
17799 {
17800 struct type *prop_type
17801 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17802 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17803 prop_type);
17804 }
17805 }
17806
17807 if (attr_byte_stride != nullptr
17808 || attr_bit_stride != nullptr)
17809 {
17810 bool byte_stride_p = (attr_byte_stride != nullptr);
17811 struct dynamic_prop *stride
17812 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17813
17814 range_type
17815 = create_range_type_with_stride (NULL, orig_base_type, &low,
17816 &high, bias, stride, byte_stride_p);
17817 }
17818 else
17819 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17820
17821 if (high_bound_is_count)
17822 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17823
17824 /* Ada expects an empty array on no boundary attributes. */
17825 if (attr == NULL && cu->language != language_ada)
17826 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17827
17828 name = dwarf2_name (die, cu);
17829 if (name)
17830 TYPE_NAME (range_type) = name;
17831
17832 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17833 if (attr != nullptr)
17834 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17835
17836 maybe_set_alignment (cu, die, range_type);
17837
17838 set_die_type (die, range_type, cu);
17839
17840 /* set_die_type should be already done. */
17841 set_descriptive_type (range_type, die, cu);
17842
17843 return range_type;
17844 }
17845
17846 static struct type *
17847 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17848 {
17849 struct type *type;
17850
17851 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17852 NULL);
17853 TYPE_NAME (type) = dwarf2_name (die, cu);
17854
17855 /* In Ada, an unspecified type is typically used when the description
17856 of the type is deferred to a different unit. When encountering
17857 such a type, we treat it as a stub, and try to resolve it later on,
17858 when needed. */
17859 if (cu->language == language_ada)
17860 TYPE_STUB (type) = 1;
17861
17862 return set_die_type (die, type, cu);
17863 }
17864
17865 /* Read a single die and all its descendents. Set the die's sibling
17866 field to NULL; set other fields in the die correctly, and set all
17867 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17868 location of the info_ptr after reading all of those dies. PARENT
17869 is the parent of the die in question. */
17870
17871 static struct die_info *
17872 read_die_and_children (const struct die_reader_specs *reader,
17873 const gdb_byte *info_ptr,
17874 const gdb_byte **new_info_ptr,
17875 struct die_info *parent)
17876 {
17877 struct die_info *die;
17878 const gdb_byte *cur_ptr;
17879
17880 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17881 if (die == NULL)
17882 {
17883 *new_info_ptr = cur_ptr;
17884 return NULL;
17885 }
17886 store_in_ref_table (die, reader->cu);
17887
17888 if (die->has_children)
17889 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17890 else
17891 {
17892 die->child = NULL;
17893 *new_info_ptr = cur_ptr;
17894 }
17895
17896 die->sibling = NULL;
17897 die->parent = parent;
17898 return die;
17899 }
17900
17901 /* Read a die, all of its descendents, and all of its siblings; set
17902 all of the fields of all of the dies correctly. Arguments are as
17903 in read_die_and_children. */
17904
17905 static struct die_info *
17906 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17907 const gdb_byte *info_ptr,
17908 const gdb_byte **new_info_ptr,
17909 struct die_info *parent)
17910 {
17911 struct die_info *first_die, *last_sibling;
17912 const gdb_byte *cur_ptr;
17913
17914 cur_ptr = info_ptr;
17915 first_die = last_sibling = NULL;
17916
17917 while (1)
17918 {
17919 struct die_info *die
17920 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17921
17922 if (die == NULL)
17923 {
17924 *new_info_ptr = cur_ptr;
17925 return first_die;
17926 }
17927
17928 if (!first_die)
17929 first_die = die;
17930 else
17931 last_sibling->sibling = die;
17932
17933 last_sibling = die;
17934 }
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 This the main entry point for reading a DIE and all its children. */
17941
17942 static struct die_info *
17943 read_die_and_siblings (const struct die_reader_specs *reader,
17944 const gdb_byte *info_ptr,
17945 const gdb_byte **new_info_ptr,
17946 struct die_info *parent)
17947 {
17948 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17949 new_info_ptr, parent);
17950
17951 if (dwarf_die_debug)
17952 {
17953 fprintf_unfiltered (gdb_stdlog,
17954 "Read die from %s@0x%x of %s:\n",
17955 reader->die_section->get_name (),
17956 (unsigned) (info_ptr - reader->die_section->buffer),
17957 bfd_get_filename (reader->abfd));
17958 dump_die (die, dwarf_die_debug);
17959 }
17960
17961 return die;
17962 }
17963
17964 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17965 attributes.
17966 The caller is responsible for filling in the extra attributes
17967 and updating (*DIEP)->num_attrs.
17968 Set DIEP to point to a newly allocated die with its information,
17969 except for its child, sibling, and parent fields. */
17970
17971 static const gdb_byte *
17972 read_full_die_1 (const struct die_reader_specs *reader,
17973 struct die_info **diep, const gdb_byte *info_ptr,
17974 int num_extra_attrs)
17975 {
17976 unsigned int abbrev_number, bytes_read, i;
17977 struct abbrev_info *abbrev;
17978 struct die_info *die;
17979 struct dwarf2_cu *cu = reader->cu;
17980 bfd *abfd = reader->abfd;
17981
17982 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17983 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17984 info_ptr += bytes_read;
17985 if (!abbrev_number)
17986 {
17987 *diep = NULL;
17988 return info_ptr;
17989 }
17990
17991 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17992 if (!abbrev)
17993 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17994 abbrev_number,
17995 bfd_get_filename (abfd));
17996
17997 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17998 die->sect_off = sect_off;
17999 die->tag = abbrev->tag;
18000 die->abbrev = abbrev_number;
18001 die->has_children = abbrev->has_children;
18002
18003 /* Make the result usable.
18004 The caller needs to update num_attrs after adding the extra
18005 attributes. */
18006 die->num_attrs = abbrev->num_attrs;
18007
18008 std::vector<int> indexes_that_need_reprocess;
18009 for (i = 0; i < abbrev->num_attrs; ++i)
18010 {
18011 bool need_reprocess;
18012 info_ptr =
18013 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18014 info_ptr, &need_reprocess);
18015 if (need_reprocess)
18016 indexes_that_need_reprocess.push_back (i);
18017 }
18018
18019 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18020 if (attr != nullptr)
18021 cu->str_offsets_base = DW_UNSND (attr);
18022
18023 auto maybe_addr_base = lookup_addr_base(die);
18024 if (maybe_addr_base.has_value ())
18025 cu->addr_base = *maybe_addr_base;
18026 for (int index : indexes_that_need_reprocess)
18027 read_attribute_reprocess (reader, &die->attrs[index]);
18028 *diep = die;
18029 return info_ptr;
18030 }
18031
18032 /* Read a die and all its attributes.
18033 Set DIEP to point to a newly allocated die with its information,
18034 except for its child, sibling, and parent fields. */
18035
18036 static const gdb_byte *
18037 read_full_die (const struct die_reader_specs *reader,
18038 struct die_info **diep, const gdb_byte *info_ptr)
18039 {
18040 const gdb_byte *result;
18041
18042 result = read_full_die_1 (reader, diep, info_ptr, 0);
18043
18044 if (dwarf_die_debug)
18045 {
18046 fprintf_unfiltered (gdb_stdlog,
18047 "Read die from %s@0x%x of %s:\n",
18048 reader->die_section->get_name (),
18049 (unsigned) (info_ptr - reader->die_section->buffer),
18050 bfd_get_filename (reader->abfd));
18051 dump_die (*diep, dwarf_die_debug);
18052 }
18053
18054 return result;
18055 }
18056 \f
18057
18058 /* Returns nonzero if TAG represents a type that we might generate a partial
18059 symbol for. */
18060
18061 static int
18062 is_type_tag_for_partial (int tag)
18063 {
18064 switch (tag)
18065 {
18066 #if 0
18067 /* Some types that would be reasonable to generate partial symbols for,
18068 that we don't at present. */
18069 case DW_TAG_array_type:
18070 case DW_TAG_file_type:
18071 case DW_TAG_ptr_to_member_type:
18072 case DW_TAG_set_type:
18073 case DW_TAG_string_type:
18074 case DW_TAG_subroutine_type:
18075 #endif
18076 case DW_TAG_base_type:
18077 case DW_TAG_class_type:
18078 case DW_TAG_interface_type:
18079 case DW_TAG_enumeration_type:
18080 case DW_TAG_structure_type:
18081 case DW_TAG_subrange_type:
18082 case DW_TAG_typedef:
18083 case DW_TAG_union_type:
18084 return 1;
18085 default:
18086 return 0;
18087 }
18088 }
18089
18090 /* Load all DIEs that are interesting for partial symbols into memory. */
18091
18092 static struct partial_die_info *
18093 load_partial_dies (const struct die_reader_specs *reader,
18094 const gdb_byte *info_ptr, int building_psymtab)
18095 {
18096 struct dwarf2_cu *cu = reader->cu;
18097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18098 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18099 unsigned int bytes_read;
18100 unsigned int load_all = 0;
18101 int nesting_level = 1;
18102
18103 parent_die = NULL;
18104 last_die = NULL;
18105
18106 gdb_assert (cu->per_cu != NULL);
18107 if (cu->per_cu->load_all_dies)
18108 load_all = 1;
18109
18110 cu->partial_dies
18111 = htab_create_alloc_ex (cu->header.length / 12,
18112 partial_die_hash,
18113 partial_die_eq,
18114 NULL,
18115 &cu->comp_unit_obstack,
18116 hashtab_obstack_allocate,
18117 dummy_obstack_deallocate);
18118
18119 while (1)
18120 {
18121 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18122
18123 /* A NULL abbrev means the end of a series of children. */
18124 if (abbrev == NULL)
18125 {
18126 if (--nesting_level == 0)
18127 return first_die;
18128
18129 info_ptr += bytes_read;
18130 last_die = parent_die;
18131 parent_die = parent_die->die_parent;
18132 continue;
18133 }
18134
18135 /* Check for template arguments. We never save these; if
18136 they're seen, we just mark the parent, and go on our way. */
18137 if (parent_die != NULL
18138 && cu->language == language_cplus
18139 && (abbrev->tag == DW_TAG_template_type_param
18140 || abbrev->tag == DW_TAG_template_value_param))
18141 {
18142 parent_die->has_template_arguments = 1;
18143
18144 if (!load_all)
18145 {
18146 /* We don't need a partial DIE for the template argument. */
18147 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18148 continue;
18149 }
18150 }
18151
18152 /* We only recurse into c++ subprograms looking for template arguments.
18153 Skip their other children. */
18154 if (!load_all
18155 && cu->language == language_cplus
18156 && parent_die != NULL
18157 && parent_die->tag == DW_TAG_subprogram)
18158 {
18159 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18160 continue;
18161 }
18162
18163 /* Check whether this DIE is interesting enough to save. Normally
18164 we would not be interested in members here, but there may be
18165 later variables referencing them via DW_AT_specification (for
18166 static members). */
18167 if (!load_all
18168 && !is_type_tag_for_partial (abbrev->tag)
18169 && abbrev->tag != DW_TAG_constant
18170 && abbrev->tag != DW_TAG_enumerator
18171 && abbrev->tag != DW_TAG_subprogram
18172 && abbrev->tag != DW_TAG_inlined_subroutine
18173 && abbrev->tag != DW_TAG_lexical_block
18174 && abbrev->tag != DW_TAG_variable
18175 && abbrev->tag != DW_TAG_namespace
18176 && abbrev->tag != DW_TAG_module
18177 && abbrev->tag != DW_TAG_member
18178 && abbrev->tag != DW_TAG_imported_unit
18179 && abbrev->tag != DW_TAG_imported_declaration)
18180 {
18181 /* Otherwise we skip to the next sibling, if any. */
18182 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18183 continue;
18184 }
18185
18186 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18187 abbrev);
18188
18189 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18190
18191 /* This two-pass algorithm for processing partial symbols has a
18192 high cost in cache pressure. Thus, handle some simple cases
18193 here which cover the majority of C partial symbols. DIEs
18194 which neither have specification tags in them, nor could have
18195 specification tags elsewhere pointing at them, can simply be
18196 processed and discarded.
18197
18198 This segment is also optional; scan_partial_symbols and
18199 add_partial_symbol will handle these DIEs if we chain
18200 them in normally. When compilers which do not emit large
18201 quantities of duplicate debug information are more common,
18202 this code can probably be removed. */
18203
18204 /* Any complete simple types at the top level (pretty much all
18205 of them, for a language without namespaces), can be processed
18206 directly. */
18207 if (parent_die == NULL
18208 && pdi.has_specification == 0
18209 && pdi.is_declaration == 0
18210 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18211 || pdi.tag == DW_TAG_base_type
18212 || pdi.tag == DW_TAG_subrange_type))
18213 {
18214 if (building_psymtab && pdi.name != NULL)
18215 add_psymbol_to_list (pdi.name, false,
18216 VAR_DOMAIN, LOC_TYPEDEF, -1,
18217 psymbol_placement::STATIC,
18218 0, cu->language, objfile);
18219 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18220 continue;
18221 }
18222
18223 /* The exception for DW_TAG_typedef with has_children above is
18224 a workaround of GCC PR debug/47510. In the case of this complaint
18225 type_name_or_error will error on such types later.
18226
18227 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18228 it could not find the child DIEs referenced later, this is checked
18229 above. In correct DWARF DW_TAG_typedef should have no children. */
18230
18231 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18232 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18233 "- DIE at %s [in module %s]"),
18234 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18235
18236 /* If we're at the second level, and we're an enumerator, and
18237 our parent has no specification (meaning possibly lives in a
18238 namespace elsewhere), then we can add the partial symbol now
18239 instead of queueing it. */
18240 if (pdi.tag == DW_TAG_enumerator
18241 && parent_die != NULL
18242 && parent_die->die_parent == NULL
18243 && parent_die->tag == DW_TAG_enumeration_type
18244 && parent_die->has_specification == 0)
18245 {
18246 if (pdi.name == NULL)
18247 complaint (_("malformed enumerator DIE ignored"));
18248 else if (building_psymtab)
18249 add_psymbol_to_list (pdi.name, false,
18250 VAR_DOMAIN, LOC_CONST, -1,
18251 cu->language == language_cplus
18252 ? psymbol_placement::GLOBAL
18253 : psymbol_placement::STATIC,
18254 0, cu->language, objfile);
18255
18256 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18257 continue;
18258 }
18259
18260 struct partial_die_info *part_die
18261 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18262
18263 /* We'll save this DIE so link it in. */
18264 part_die->die_parent = parent_die;
18265 part_die->die_sibling = NULL;
18266 part_die->die_child = NULL;
18267
18268 if (last_die && last_die == parent_die)
18269 last_die->die_child = part_die;
18270 else if (last_die)
18271 last_die->die_sibling = part_die;
18272
18273 last_die = part_die;
18274
18275 if (first_die == NULL)
18276 first_die = part_die;
18277
18278 /* Maybe add the DIE to the hash table. Not all DIEs that we
18279 find interesting need to be in the hash table, because we
18280 also have the parent/sibling/child chains; only those that we
18281 might refer to by offset later during partial symbol reading.
18282
18283 For now this means things that might have be the target of a
18284 DW_AT_specification, DW_AT_abstract_origin, or
18285 DW_AT_extension. DW_AT_extension will refer only to
18286 namespaces; DW_AT_abstract_origin refers to functions (and
18287 many things under the function DIE, but we do not recurse
18288 into function DIEs during partial symbol reading) and
18289 possibly variables as well; DW_AT_specification refers to
18290 declarations. Declarations ought to have the DW_AT_declaration
18291 flag. It happens that GCC forgets to put it in sometimes, but
18292 only for functions, not for types.
18293
18294 Adding more things than necessary to the hash table is harmless
18295 except for the performance cost. Adding too few will result in
18296 wasted time in find_partial_die, when we reread the compilation
18297 unit with load_all_dies set. */
18298
18299 if (load_all
18300 || abbrev->tag == DW_TAG_constant
18301 || abbrev->tag == DW_TAG_subprogram
18302 || abbrev->tag == DW_TAG_variable
18303 || abbrev->tag == DW_TAG_namespace
18304 || part_die->is_declaration)
18305 {
18306 void **slot;
18307
18308 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18309 to_underlying (part_die->sect_off),
18310 INSERT);
18311 *slot = part_die;
18312 }
18313
18314 /* For some DIEs we want to follow their children (if any). For C
18315 we have no reason to follow the children of structures; for other
18316 languages we have to, so that we can get at method physnames
18317 to infer fully qualified class names, for DW_AT_specification,
18318 and for C++ template arguments. For C++, we also look one level
18319 inside functions to find template arguments (if the name of the
18320 function does not already contain the template arguments).
18321
18322 For Ada and Fortran, we need to scan the children of subprograms
18323 and lexical blocks as well because these languages allow the
18324 definition of nested entities that could be interesting for the
18325 debugger, such as nested subprograms for instance. */
18326 if (last_die->has_children
18327 && (load_all
18328 || last_die->tag == DW_TAG_namespace
18329 || last_die->tag == DW_TAG_module
18330 || last_die->tag == DW_TAG_enumeration_type
18331 || (cu->language == language_cplus
18332 && last_die->tag == DW_TAG_subprogram
18333 && (last_die->name == NULL
18334 || strchr (last_die->name, '<') == NULL))
18335 || (cu->language != language_c
18336 && (last_die->tag == DW_TAG_class_type
18337 || last_die->tag == DW_TAG_interface_type
18338 || last_die->tag == DW_TAG_structure_type
18339 || last_die->tag == DW_TAG_union_type))
18340 || ((cu->language == language_ada
18341 || cu->language == language_fortran)
18342 && (last_die->tag == DW_TAG_subprogram
18343 || last_die->tag == DW_TAG_lexical_block))))
18344 {
18345 nesting_level++;
18346 parent_die = last_die;
18347 continue;
18348 }
18349
18350 /* Otherwise we skip to the next sibling, if any. */
18351 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18352
18353 /* Back to the top, do it again. */
18354 }
18355 }
18356
18357 partial_die_info::partial_die_info (sect_offset sect_off_,
18358 struct abbrev_info *abbrev)
18359 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18360 {
18361 }
18362
18363 /* Read a minimal amount of information into the minimal die structure.
18364 INFO_PTR should point just after the initial uleb128 of a DIE. */
18365
18366 const gdb_byte *
18367 partial_die_info::read (const struct die_reader_specs *reader,
18368 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18369 {
18370 struct dwarf2_cu *cu = reader->cu;
18371 struct dwarf2_per_objfile *dwarf2_per_objfile
18372 = cu->per_cu->dwarf2_per_objfile;
18373 unsigned int i;
18374 int has_low_pc_attr = 0;
18375 int has_high_pc_attr = 0;
18376 int high_pc_relative = 0;
18377
18378 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18379 for (i = 0; i < abbrev.num_attrs; ++i)
18380 {
18381 bool need_reprocess;
18382 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18383 info_ptr, &need_reprocess);
18384 /* String and address offsets that need to do the reprocessing have
18385 already been read at this point, so there is no need to wait until
18386 the loop terminates to do the reprocessing. */
18387 if (need_reprocess)
18388 read_attribute_reprocess (reader, &attr_vec[i]);
18389 attribute &attr = attr_vec[i];
18390 /* Store the data if it is of an attribute we want to keep in a
18391 partial symbol table. */
18392 switch (attr.name)
18393 {
18394 case DW_AT_name:
18395 switch (tag)
18396 {
18397 case DW_TAG_compile_unit:
18398 case DW_TAG_partial_unit:
18399 case DW_TAG_type_unit:
18400 /* Compilation units have a DW_AT_name that is a filename, not
18401 a source language identifier. */
18402 case DW_TAG_enumeration_type:
18403 case DW_TAG_enumerator:
18404 /* These tags always have simple identifiers already; no need
18405 to canonicalize them. */
18406 name = DW_STRING (&attr);
18407 break;
18408 default:
18409 {
18410 struct objfile *objfile = dwarf2_per_objfile->objfile;
18411
18412 name
18413 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18414 &objfile->per_bfd->storage_obstack);
18415 }
18416 break;
18417 }
18418 break;
18419 case DW_AT_linkage_name:
18420 case DW_AT_MIPS_linkage_name:
18421 /* Note that both forms of linkage name might appear. We
18422 assume they will be the same, and we only store the last
18423 one we see. */
18424 linkage_name = DW_STRING (&attr);
18425 break;
18426 case DW_AT_low_pc:
18427 has_low_pc_attr = 1;
18428 lowpc = attr.value_as_address ();
18429 break;
18430 case DW_AT_high_pc:
18431 has_high_pc_attr = 1;
18432 highpc = attr.value_as_address ();
18433 if (cu->header.version >= 4 && attr.form_is_constant ())
18434 high_pc_relative = 1;
18435 break;
18436 case DW_AT_location:
18437 /* Support the .debug_loc offsets. */
18438 if (attr.form_is_block ())
18439 {
18440 d.locdesc = DW_BLOCK (&attr);
18441 }
18442 else if (attr.form_is_section_offset ())
18443 {
18444 dwarf2_complex_location_expr_complaint ();
18445 }
18446 else
18447 {
18448 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18449 "partial symbol information");
18450 }
18451 break;
18452 case DW_AT_external:
18453 is_external = DW_UNSND (&attr);
18454 break;
18455 case DW_AT_declaration:
18456 is_declaration = DW_UNSND (&attr);
18457 break;
18458 case DW_AT_type:
18459 has_type = 1;
18460 break;
18461 case DW_AT_abstract_origin:
18462 case DW_AT_specification:
18463 case DW_AT_extension:
18464 has_specification = 1;
18465 spec_offset = dwarf2_get_ref_die_offset (&attr);
18466 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18467 || cu->per_cu->is_dwz);
18468 break;
18469 case DW_AT_sibling:
18470 /* Ignore absolute siblings, they might point outside of
18471 the current compile unit. */
18472 if (attr.form == DW_FORM_ref_addr)
18473 complaint (_("ignoring absolute DW_AT_sibling"));
18474 else
18475 {
18476 const gdb_byte *buffer = reader->buffer;
18477 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18478 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18479
18480 if (sibling_ptr < info_ptr)
18481 complaint (_("DW_AT_sibling points backwards"));
18482 else if (sibling_ptr > reader->buffer_end)
18483 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18484 else
18485 sibling = sibling_ptr;
18486 }
18487 break;
18488 case DW_AT_byte_size:
18489 has_byte_size = 1;
18490 break;
18491 case DW_AT_const_value:
18492 has_const_value = 1;
18493 break;
18494 case DW_AT_calling_convention:
18495 /* DWARF doesn't provide a way to identify a program's source-level
18496 entry point. DW_AT_calling_convention attributes are only meant
18497 to describe functions' calling conventions.
18498
18499 However, because it's a necessary piece of information in
18500 Fortran, and before DWARF 4 DW_CC_program was the only
18501 piece of debugging information whose definition refers to
18502 a 'main program' at all, several compilers marked Fortran
18503 main programs with DW_CC_program --- even when those
18504 functions use the standard calling conventions.
18505
18506 Although DWARF now specifies a way to provide this
18507 information, we support this practice for backward
18508 compatibility. */
18509 if (DW_UNSND (&attr) == DW_CC_program
18510 && cu->language == language_fortran)
18511 main_subprogram = 1;
18512 break;
18513 case DW_AT_inline:
18514 if (DW_UNSND (&attr) == DW_INL_inlined
18515 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18516 may_be_inlined = 1;
18517 break;
18518
18519 case DW_AT_import:
18520 if (tag == DW_TAG_imported_unit)
18521 {
18522 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18523 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18524 || cu->per_cu->is_dwz);
18525 }
18526 break;
18527
18528 case DW_AT_main_subprogram:
18529 main_subprogram = DW_UNSND (&attr);
18530 break;
18531
18532 case DW_AT_ranges:
18533 {
18534 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18535 but that requires a full DIE, so instead we just
18536 reimplement it. */
18537 int need_ranges_base = tag != DW_TAG_compile_unit;
18538 unsigned int ranges_offset = (DW_UNSND (&attr)
18539 + (need_ranges_base
18540 ? cu->ranges_base
18541 : 0));
18542
18543 /* Value of the DW_AT_ranges attribute is the offset in the
18544 .debug_ranges section. */
18545 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18546 nullptr))
18547 has_pc_info = 1;
18548 }
18549 break;
18550
18551 default:
18552 break;
18553 }
18554 }
18555
18556 /* For Ada, if both the name and the linkage name appear, we prefer
18557 the latter. This lets "catch exception" work better, regardless
18558 of the order in which the name and linkage name were emitted.
18559 Really, though, this is just a workaround for the fact that gdb
18560 doesn't store both the name and the linkage name. */
18561 if (cu->language == language_ada && linkage_name != nullptr)
18562 name = linkage_name;
18563
18564 if (high_pc_relative)
18565 highpc += lowpc;
18566
18567 if (has_low_pc_attr && has_high_pc_attr)
18568 {
18569 /* When using the GNU linker, .gnu.linkonce. sections are used to
18570 eliminate duplicate copies of functions and vtables and such.
18571 The linker will arbitrarily choose one and discard the others.
18572 The AT_*_pc values for such functions refer to local labels in
18573 these sections. If the section from that file was discarded, the
18574 labels are not in the output, so the relocs get a value of 0.
18575 If this is a discarded function, mark the pc bounds as invalid,
18576 so that GDB will ignore it. */
18577 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18578 {
18579 struct objfile *objfile = dwarf2_per_objfile->objfile;
18580 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18581
18582 complaint (_("DW_AT_low_pc %s is zero "
18583 "for DIE at %s [in module %s]"),
18584 paddress (gdbarch, lowpc),
18585 sect_offset_str (sect_off),
18586 objfile_name (objfile));
18587 }
18588 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18589 else if (lowpc >= highpc)
18590 {
18591 struct objfile *objfile = dwarf2_per_objfile->objfile;
18592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18593
18594 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18595 "for DIE at %s [in module %s]"),
18596 paddress (gdbarch, lowpc),
18597 paddress (gdbarch, highpc),
18598 sect_offset_str (sect_off),
18599 objfile_name (objfile));
18600 }
18601 else
18602 has_pc_info = 1;
18603 }
18604
18605 return info_ptr;
18606 }
18607
18608 /* Find a cached partial DIE at OFFSET in CU. */
18609
18610 struct partial_die_info *
18611 dwarf2_cu::find_partial_die (sect_offset sect_off)
18612 {
18613 struct partial_die_info *lookup_die = NULL;
18614 struct partial_die_info part_die (sect_off);
18615
18616 lookup_die = ((struct partial_die_info *)
18617 htab_find_with_hash (partial_dies, &part_die,
18618 to_underlying (sect_off)));
18619
18620 return lookup_die;
18621 }
18622
18623 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18624 except in the case of .debug_types DIEs which do not reference
18625 outside their CU (they do however referencing other types via
18626 DW_FORM_ref_sig8). */
18627
18628 static const struct cu_partial_die_info
18629 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18630 {
18631 struct dwarf2_per_objfile *dwarf2_per_objfile
18632 = cu->per_cu->dwarf2_per_objfile;
18633 struct objfile *objfile = dwarf2_per_objfile->objfile;
18634 struct dwarf2_per_cu_data *per_cu = NULL;
18635 struct partial_die_info *pd = NULL;
18636
18637 if (offset_in_dwz == cu->per_cu->is_dwz
18638 && offset_in_cu_p (&cu->header, sect_off))
18639 {
18640 pd = cu->find_partial_die (sect_off);
18641 if (pd != NULL)
18642 return { cu, pd };
18643 /* We missed recording what we needed.
18644 Load all dies and try again. */
18645 per_cu = cu->per_cu;
18646 }
18647 else
18648 {
18649 /* TUs don't reference other CUs/TUs (except via type signatures). */
18650 if (cu->per_cu->is_debug_types)
18651 {
18652 error (_("Dwarf Error: Type Unit at offset %s contains"
18653 " external reference to offset %s [in module %s].\n"),
18654 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18655 bfd_get_filename (objfile->obfd));
18656 }
18657 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18658 dwarf2_per_objfile);
18659
18660 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18661 load_partial_comp_unit (per_cu);
18662
18663 per_cu->cu->last_used = 0;
18664 pd = per_cu->cu->find_partial_die (sect_off);
18665 }
18666
18667 /* If we didn't find it, and not all dies have been loaded,
18668 load them all and try again. */
18669
18670 if (pd == NULL && per_cu->load_all_dies == 0)
18671 {
18672 per_cu->load_all_dies = 1;
18673
18674 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18675 THIS_CU->cu may already be in use. So we can't just free it and
18676 replace its DIEs with the ones we read in. Instead, we leave those
18677 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18678 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18679 set. */
18680 load_partial_comp_unit (per_cu);
18681
18682 pd = per_cu->cu->find_partial_die (sect_off);
18683 }
18684
18685 if (pd == NULL)
18686 internal_error (__FILE__, __LINE__,
18687 _("could not find partial DIE %s "
18688 "in cache [from module %s]\n"),
18689 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18690 return { per_cu->cu, pd };
18691 }
18692
18693 /* See if we can figure out if the class lives in a namespace. We do
18694 this by looking for a member function; its demangled name will
18695 contain namespace info, if there is any. */
18696
18697 static void
18698 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18699 struct dwarf2_cu *cu)
18700 {
18701 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18702 what template types look like, because the demangler
18703 frequently doesn't give the same name as the debug info. We
18704 could fix this by only using the demangled name to get the
18705 prefix (but see comment in read_structure_type). */
18706
18707 struct partial_die_info *real_pdi;
18708 struct partial_die_info *child_pdi;
18709
18710 /* If this DIE (this DIE's specification, if any) has a parent, then
18711 we should not do this. We'll prepend the parent's fully qualified
18712 name when we create the partial symbol. */
18713
18714 real_pdi = struct_pdi;
18715 while (real_pdi->has_specification)
18716 {
18717 auto res = find_partial_die (real_pdi->spec_offset,
18718 real_pdi->spec_is_dwz, cu);
18719 real_pdi = res.pdi;
18720 cu = res.cu;
18721 }
18722
18723 if (real_pdi->die_parent != NULL)
18724 return;
18725
18726 for (child_pdi = struct_pdi->die_child;
18727 child_pdi != NULL;
18728 child_pdi = child_pdi->die_sibling)
18729 {
18730 if (child_pdi->tag == DW_TAG_subprogram
18731 && child_pdi->linkage_name != NULL)
18732 {
18733 gdb::unique_xmalloc_ptr<char> actual_class_name
18734 (language_class_name_from_physname (cu->language_defn,
18735 child_pdi->linkage_name));
18736 if (actual_class_name != NULL)
18737 {
18738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18739 struct_pdi->name
18740 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18741 actual_class_name.get ());
18742 }
18743 break;
18744 }
18745 }
18746 }
18747
18748 void
18749 partial_die_info::fixup (struct dwarf2_cu *cu)
18750 {
18751 /* Once we've fixed up a die, there's no point in doing so again.
18752 This also avoids a memory leak if we were to call
18753 guess_partial_die_structure_name multiple times. */
18754 if (fixup_called)
18755 return;
18756
18757 /* If we found a reference attribute and the DIE has no name, try
18758 to find a name in the referred to DIE. */
18759
18760 if (name == NULL && has_specification)
18761 {
18762 struct partial_die_info *spec_die;
18763
18764 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18765 spec_die = res.pdi;
18766 cu = res.cu;
18767
18768 spec_die->fixup (cu);
18769
18770 if (spec_die->name)
18771 {
18772 name = spec_die->name;
18773
18774 /* Copy DW_AT_external attribute if it is set. */
18775 if (spec_die->is_external)
18776 is_external = spec_die->is_external;
18777 }
18778 }
18779
18780 /* Set default names for some unnamed DIEs. */
18781
18782 if (name == NULL && tag == DW_TAG_namespace)
18783 name = CP_ANONYMOUS_NAMESPACE_STR;
18784
18785 /* If there is no parent die to provide a namespace, and there are
18786 children, see if we can determine the namespace from their linkage
18787 name. */
18788 if (cu->language == language_cplus
18789 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18790 && die_parent == NULL
18791 && has_children
18792 && (tag == DW_TAG_class_type
18793 || tag == DW_TAG_structure_type
18794 || tag == DW_TAG_union_type))
18795 guess_partial_die_structure_name (this, cu);
18796
18797 /* GCC might emit a nameless struct or union that has a linkage
18798 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18799 if (name == NULL
18800 && (tag == DW_TAG_class_type
18801 || tag == DW_TAG_interface_type
18802 || tag == DW_TAG_structure_type
18803 || tag == DW_TAG_union_type)
18804 && linkage_name != NULL)
18805 {
18806 gdb::unique_xmalloc_ptr<char> demangled
18807 (gdb_demangle (linkage_name, DMGL_TYPES));
18808 if (demangled != nullptr)
18809 {
18810 const char *base;
18811
18812 /* Strip any leading namespaces/classes, keep only the base name.
18813 DW_AT_name for named DIEs does not contain the prefixes. */
18814 base = strrchr (demangled.get (), ':');
18815 if (base && base > demangled.get () && base[-1] == ':')
18816 base++;
18817 else
18818 base = demangled.get ();
18819
18820 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18821 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18822 }
18823 }
18824
18825 fixup_called = 1;
18826 }
18827
18828 /* Process the attributes that had to be skipped in the first round. These
18829 attributes are the ones that need str_offsets_base or addr_base attributes.
18830 They could not have been processed in the first round, because at the time
18831 the values of str_offsets_base or addr_base may not have been known. */
18832 void read_attribute_reprocess (const struct die_reader_specs *reader,
18833 struct attribute *attr)
18834 {
18835 struct dwarf2_cu *cu = reader->cu;
18836 switch (attr->form)
18837 {
18838 case DW_FORM_addrx:
18839 case DW_FORM_GNU_addr_index:
18840 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18841 break;
18842 case DW_FORM_strx:
18843 case DW_FORM_strx1:
18844 case DW_FORM_strx2:
18845 case DW_FORM_strx3:
18846 case DW_FORM_strx4:
18847 case DW_FORM_GNU_str_index:
18848 {
18849 unsigned int str_index = DW_UNSND (attr);
18850 if (reader->dwo_file != NULL)
18851 {
18852 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18853 DW_STRING_IS_CANONICAL (attr) = 0;
18854 }
18855 else
18856 {
18857 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18858 DW_STRING_IS_CANONICAL (attr) = 0;
18859 }
18860 break;
18861 }
18862 default:
18863 gdb_assert_not_reached (_("Unexpected DWARF form."));
18864 }
18865 }
18866
18867 /* Read an attribute value described by an attribute form. */
18868
18869 static const gdb_byte *
18870 read_attribute_value (const struct die_reader_specs *reader,
18871 struct attribute *attr, unsigned form,
18872 LONGEST implicit_const, const gdb_byte *info_ptr,
18873 bool *need_reprocess)
18874 {
18875 struct dwarf2_cu *cu = reader->cu;
18876 struct dwarf2_per_objfile *dwarf2_per_objfile
18877 = cu->per_cu->dwarf2_per_objfile;
18878 struct objfile *objfile = dwarf2_per_objfile->objfile;
18879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18880 bfd *abfd = reader->abfd;
18881 struct comp_unit_head *cu_header = &cu->header;
18882 unsigned int bytes_read;
18883 struct dwarf_block *blk;
18884 *need_reprocess = false;
18885
18886 attr->form = (enum dwarf_form) form;
18887 switch (form)
18888 {
18889 case DW_FORM_ref_addr:
18890 if (cu->header.version == 2)
18891 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18892 else
18893 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18894 &cu->header, &bytes_read);
18895 info_ptr += bytes_read;
18896 break;
18897 case DW_FORM_GNU_ref_alt:
18898 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18899 info_ptr += bytes_read;
18900 break;
18901 case DW_FORM_addr:
18902 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18903 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18904 info_ptr += bytes_read;
18905 break;
18906 case DW_FORM_block2:
18907 blk = dwarf_alloc_block (cu);
18908 blk->size = read_2_bytes (abfd, info_ptr);
18909 info_ptr += 2;
18910 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18911 info_ptr += blk->size;
18912 DW_BLOCK (attr) = blk;
18913 break;
18914 case DW_FORM_block4:
18915 blk = dwarf_alloc_block (cu);
18916 blk->size = read_4_bytes (abfd, info_ptr);
18917 info_ptr += 4;
18918 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18919 info_ptr += blk->size;
18920 DW_BLOCK (attr) = blk;
18921 break;
18922 case DW_FORM_data2:
18923 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18924 info_ptr += 2;
18925 break;
18926 case DW_FORM_data4:
18927 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18928 info_ptr += 4;
18929 break;
18930 case DW_FORM_data8:
18931 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18932 info_ptr += 8;
18933 break;
18934 case DW_FORM_data16:
18935 blk = dwarf_alloc_block (cu);
18936 blk->size = 16;
18937 blk->data = read_n_bytes (abfd, info_ptr, 16);
18938 info_ptr += 16;
18939 DW_BLOCK (attr) = blk;
18940 break;
18941 case DW_FORM_sec_offset:
18942 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18943 info_ptr += bytes_read;
18944 break;
18945 case DW_FORM_string:
18946 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18947 DW_STRING_IS_CANONICAL (attr) = 0;
18948 info_ptr += bytes_read;
18949 break;
18950 case DW_FORM_strp:
18951 if (!cu->per_cu->is_dwz)
18952 {
18953 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18954 abfd, info_ptr, cu_header,
18955 &bytes_read);
18956 DW_STRING_IS_CANONICAL (attr) = 0;
18957 info_ptr += bytes_read;
18958 break;
18959 }
18960 /* FALLTHROUGH */
18961 case DW_FORM_line_strp:
18962 if (!cu->per_cu->is_dwz)
18963 {
18964 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18965 abfd, info_ptr,
18966 cu_header, &bytes_read);
18967 DW_STRING_IS_CANONICAL (attr) = 0;
18968 info_ptr += bytes_read;
18969 break;
18970 }
18971 /* FALLTHROUGH */
18972 case DW_FORM_GNU_strp_alt:
18973 {
18974 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18975 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18976 &bytes_read);
18977
18978 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18979 dwz, str_offset);
18980 DW_STRING_IS_CANONICAL (attr) = 0;
18981 info_ptr += bytes_read;
18982 }
18983 break;
18984 case DW_FORM_exprloc:
18985 case DW_FORM_block:
18986 blk = dwarf_alloc_block (cu);
18987 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18988 info_ptr += bytes_read;
18989 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18990 info_ptr += blk->size;
18991 DW_BLOCK (attr) = blk;
18992 break;
18993 case DW_FORM_block1:
18994 blk = dwarf_alloc_block (cu);
18995 blk->size = read_1_byte (abfd, info_ptr);
18996 info_ptr += 1;
18997 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18998 info_ptr += blk->size;
18999 DW_BLOCK (attr) = blk;
19000 break;
19001 case DW_FORM_data1:
19002 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19003 info_ptr += 1;
19004 break;
19005 case DW_FORM_flag:
19006 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19007 info_ptr += 1;
19008 break;
19009 case DW_FORM_flag_present:
19010 DW_UNSND (attr) = 1;
19011 break;
19012 case DW_FORM_sdata:
19013 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19014 info_ptr += bytes_read;
19015 break;
19016 case DW_FORM_udata:
19017 case DW_FORM_rnglistx:
19018 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19019 info_ptr += bytes_read;
19020 break;
19021 case DW_FORM_ref1:
19022 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19023 + read_1_byte (abfd, info_ptr));
19024 info_ptr += 1;
19025 break;
19026 case DW_FORM_ref2:
19027 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19028 + read_2_bytes (abfd, info_ptr));
19029 info_ptr += 2;
19030 break;
19031 case DW_FORM_ref4:
19032 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19033 + read_4_bytes (abfd, info_ptr));
19034 info_ptr += 4;
19035 break;
19036 case DW_FORM_ref8:
19037 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19038 + read_8_bytes (abfd, info_ptr));
19039 info_ptr += 8;
19040 break;
19041 case DW_FORM_ref_sig8:
19042 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19043 info_ptr += 8;
19044 break;
19045 case DW_FORM_ref_udata:
19046 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19047 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19048 info_ptr += bytes_read;
19049 break;
19050 case DW_FORM_indirect:
19051 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19052 info_ptr += bytes_read;
19053 if (form == DW_FORM_implicit_const)
19054 {
19055 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19056 info_ptr += bytes_read;
19057 }
19058 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19059 info_ptr, need_reprocess);
19060 break;
19061 case DW_FORM_implicit_const:
19062 DW_SND (attr) = implicit_const;
19063 break;
19064 case DW_FORM_addrx:
19065 case DW_FORM_GNU_addr_index:
19066 *need_reprocess = true;
19067 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19068 info_ptr += bytes_read;
19069 break;
19070 case DW_FORM_strx:
19071 case DW_FORM_strx1:
19072 case DW_FORM_strx2:
19073 case DW_FORM_strx3:
19074 case DW_FORM_strx4:
19075 case DW_FORM_GNU_str_index:
19076 {
19077 ULONGEST str_index;
19078 if (form == DW_FORM_strx1)
19079 {
19080 str_index = read_1_byte (abfd, info_ptr);
19081 info_ptr += 1;
19082 }
19083 else if (form == DW_FORM_strx2)
19084 {
19085 str_index = read_2_bytes (abfd, info_ptr);
19086 info_ptr += 2;
19087 }
19088 else if (form == DW_FORM_strx3)
19089 {
19090 str_index = read_3_bytes (abfd, info_ptr);
19091 info_ptr += 3;
19092 }
19093 else if (form == DW_FORM_strx4)
19094 {
19095 str_index = read_4_bytes (abfd, info_ptr);
19096 info_ptr += 4;
19097 }
19098 else
19099 {
19100 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19101 info_ptr += bytes_read;
19102 }
19103 *need_reprocess = true;
19104 DW_UNSND (attr) = str_index;
19105 }
19106 break;
19107 default:
19108 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19109 dwarf_form_name (form),
19110 bfd_get_filename (abfd));
19111 }
19112
19113 /* Super hack. */
19114 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19115 attr->form = DW_FORM_GNU_ref_alt;
19116
19117 /* We have seen instances where the compiler tried to emit a byte
19118 size attribute of -1 which ended up being encoded as an unsigned
19119 0xffffffff. Although 0xffffffff is technically a valid size value,
19120 an object of this size seems pretty unlikely so we can relatively
19121 safely treat these cases as if the size attribute was invalid and
19122 treat them as zero by default. */
19123 if (attr->name == DW_AT_byte_size
19124 && form == DW_FORM_data4
19125 && DW_UNSND (attr) >= 0xffffffff)
19126 {
19127 complaint
19128 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19129 hex_string (DW_UNSND (attr)));
19130 DW_UNSND (attr) = 0;
19131 }
19132
19133 return info_ptr;
19134 }
19135
19136 /* Read an attribute described by an abbreviated attribute. */
19137
19138 static const gdb_byte *
19139 read_attribute (const struct die_reader_specs *reader,
19140 struct attribute *attr, struct attr_abbrev *abbrev,
19141 const gdb_byte *info_ptr, bool *need_reprocess)
19142 {
19143 attr->name = abbrev->name;
19144 return read_attribute_value (reader, attr, abbrev->form,
19145 abbrev->implicit_const, info_ptr,
19146 need_reprocess);
19147 }
19148
19149 static CORE_ADDR
19150 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19151 unsigned int *bytes_read)
19152 {
19153 struct comp_unit_head *cu_header = &cu->header;
19154 CORE_ADDR retval = 0;
19155
19156 if (cu_header->signed_addr_p)
19157 {
19158 switch (cu_header->addr_size)
19159 {
19160 case 2:
19161 retval = bfd_get_signed_16 (abfd, buf);
19162 break;
19163 case 4:
19164 retval = bfd_get_signed_32 (abfd, buf);
19165 break;
19166 case 8:
19167 retval = bfd_get_signed_64 (abfd, buf);
19168 break;
19169 default:
19170 internal_error (__FILE__, __LINE__,
19171 _("read_address: bad switch, signed [in module %s]"),
19172 bfd_get_filename (abfd));
19173 }
19174 }
19175 else
19176 {
19177 switch (cu_header->addr_size)
19178 {
19179 case 2:
19180 retval = bfd_get_16 (abfd, buf);
19181 break;
19182 case 4:
19183 retval = bfd_get_32 (abfd, buf);
19184 break;
19185 case 8:
19186 retval = bfd_get_64 (abfd, buf);
19187 break;
19188 default:
19189 internal_error (__FILE__, __LINE__,
19190 _("read_address: bad switch, "
19191 "unsigned [in module %s]"),
19192 bfd_get_filename (abfd));
19193 }
19194 }
19195
19196 *bytes_read = cu_header->addr_size;
19197 return retval;
19198 }
19199
19200 /* Read the initial length from a section. The (draft) DWARF 3
19201 specification allows the initial length to take up either 4 bytes
19202 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19203 bytes describe the length and all offsets will be 8 bytes in length
19204 instead of 4.
19205
19206 An older, non-standard 64-bit format is also handled by this
19207 function. The older format in question stores the initial length
19208 as an 8-byte quantity without an escape value. Lengths greater
19209 than 2^32 aren't very common which means that the initial 4 bytes
19210 is almost always zero. Since a length value of zero doesn't make
19211 sense for the 32-bit format, this initial zero can be considered to
19212 be an escape value which indicates the presence of the older 64-bit
19213 format. As written, the code can't detect (old format) lengths
19214 greater than 4GB. If it becomes necessary to handle lengths
19215 somewhat larger than 4GB, we could allow other small values (such
19216 as the non-sensical values of 1, 2, and 3) to also be used as
19217 escape values indicating the presence of the old format.
19218
19219 The value returned via bytes_read should be used to increment the
19220 relevant pointer after calling read_initial_length().
19221
19222 [ Note: read_initial_length() and read_offset() are based on the
19223 document entitled "DWARF Debugging Information Format", revision
19224 3, draft 8, dated November 19, 2001. This document was obtained
19225 from:
19226
19227 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19228
19229 This document is only a draft and is subject to change. (So beware.)
19230
19231 Details regarding the older, non-standard 64-bit format were
19232 determined empirically by examining 64-bit ELF files produced by
19233 the SGI toolchain on an IRIX 6.5 machine.
19234
19235 - Kevin, July 16, 2002
19236 ] */
19237
19238 static LONGEST
19239 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19240 {
19241 LONGEST length = bfd_get_32 (abfd, buf);
19242
19243 if (length == 0xffffffff)
19244 {
19245 length = bfd_get_64 (abfd, buf + 4);
19246 *bytes_read = 12;
19247 }
19248 else if (length == 0)
19249 {
19250 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19251 length = bfd_get_64 (abfd, buf);
19252 *bytes_read = 8;
19253 }
19254 else
19255 {
19256 *bytes_read = 4;
19257 }
19258
19259 return length;
19260 }
19261
19262 /* Cover function for read_initial_length.
19263 Returns the length of the object at BUF, and stores the size of the
19264 initial length in *BYTES_READ and stores the size that offsets will be in
19265 *OFFSET_SIZE.
19266 If the initial length size is not equivalent to that specified in
19267 CU_HEADER then issue a complaint.
19268 This is useful when reading non-comp-unit headers. */
19269
19270 static LONGEST
19271 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19272 const struct comp_unit_head *cu_header,
19273 unsigned int *bytes_read,
19274 unsigned int *offset_size)
19275 {
19276 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19277
19278 gdb_assert (cu_header->initial_length_size == 4
19279 || cu_header->initial_length_size == 8
19280 || cu_header->initial_length_size == 12);
19281
19282 if (cu_header->initial_length_size != *bytes_read)
19283 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19284
19285 *offset_size = (*bytes_read == 4) ? 4 : 8;
19286 return length;
19287 }
19288
19289 /* Read an offset from the data stream. The size of the offset is
19290 given by cu_header->offset_size. */
19291
19292 static LONGEST
19293 read_offset (bfd *abfd, const gdb_byte *buf,
19294 const struct comp_unit_head *cu_header,
19295 unsigned int *bytes_read)
19296 {
19297 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19298
19299 *bytes_read = cu_header->offset_size;
19300 return offset;
19301 }
19302
19303 /* Read an offset from the data stream. */
19304
19305 static LONGEST
19306 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19307 {
19308 LONGEST retval = 0;
19309
19310 switch (offset_size)
19311 {
19312 case 4:
19313 retval = bfd_get_32 (abfd, buf);
19314 break;
19315 case 8:
19316 retval = bfd_get_64 (abfd, buf);
19317 break;
19318 default:
19319 internal_error (__FILE__, __LINE__,
19320 _("read_offset_1: bad switch [in module %s]"),
19321 bfd_get_filename (abfd));
19322 }
19323
19324 return retval;
19325 }
19326
19327 static const gdb_byte *
19328 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19329 {
19330 /* If the size of a host char is 8 bits, we can return a pointer
19331 to the buffer, otherwise we have to copy the data to a buffer
19332 allocated on the temporary obstack. */
19333 gdb_assert (HOST_CHAR_BIT == 8);
19334 return buf;
19335 }
19336
19337 static const char *
19338 read_direct_string (bfd *abfd, const gdb_byte *buf,
19339 unsigned int *bytes_read_ptr)
19340 {
19341 /* If the size of a host char is 8 bits, we can return a pointer
19342 to the string, otherwise we have to copy the string to a buffer
19343 allocated on the temporary obstack. */
19344 gdb_assert (HOST_CHAR_BIT == 8);
19345 if (*buf == '\0')
19346 {
19347 *bytes_read_ptr = 1;
19348 return NULL;
19349 }
19350 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19351 return (const char *) buf;
19352 }
19353
19354 /* Return pointer to string at section SECT offset STR_OFFSET with error
19355 reporting strings FORM_NAME and SECT_NAME. */
19356
19357 static const char *
19358 read_indirect_string_at_offset_from (struct objfile *objfile,
19359 bfd *abfd, LONGEST str_offset,
19360 struct dwarf2_section_info *sect,
19361 const char *form_name,
19362 const char *sect_name)
19363 {
19364 sect->read (objfile);
19365 if (sect->buffer == NULL)
19366 error (_("%s used without %s section [in module %s]"),
19367 form_name, sect_name, bfd_get_filename (abfd));
19368 if (str_offset >= sect->size)
19369 error (_("%s pointing outside of %s section [in module %s]"),
19370 form_name, sect_name, bfd_get_filename (abfd));
19371 gdb_assert (HOST_CHAR_BIT == 8);
19372 if (sect->buffer[str_offset] == '\0')
19373 return NULL;
19374 return (const char *) (sect->buffer + str_offset);
19375 }
19376
19377 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19378
19379 static const char *
19380 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19381 bfd *abfd, LONGEST str_offset)
19382 {
19383 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19384 abfd, str_offset,
19385 &dwarf2_per_objfile->str,
19386 "DW_FORM_strp", ".debug_str");
19387 }
19388
19389 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19390
19391 static const char *
19392 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19393 bfd *abfd, LONGEST str_offset)
19394 {
19395 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19396 abfd, str_offset,
19397 &dwarf2_per_objfile->line_str,
19398 "DW_FORM_line_strp",
19399 ".debug_line_str");
19400 }
19401
19402 /* Read a string at offset STR_OFFSET in the .debug_str section from
19403 the .dwz file DWZ. Throw an error if the offset is too large. If
19404 the string consists of a single NUL byte, return NULL; otherwise
19405 return a pointer to the string. */
19406
19407 static const char *
19408 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19409 LONGEST str_offset)
19410 {
19411 dwz->str.read (objfile);
19412
19413 if (dwz->str.buffer == NULL)
19414 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19415 "section [in module %s]"),
19416 bfd_get_filename (dwz->dwz_bfd.get ()));
19417 if (str_offset >= dwz->str.size)
19418 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19419 ".debug_str section [in module %s]"),
19420 bfd_get_filename (dwz->dwz_bfd.get ()));
19421 gdb_assert (HOST_CHAR_BIT == 8);
19422 if (dwz->str.buffer[str_offset] == '\0')
19423 return NULL;
19424 return (const char *) (dwz->str.buffer + str_offset);
19425 }
19426
19427 /* Return pointer to string at .debug_str offset as read from BUF.
19428 BUF is assumed to be in a compilation unit described by CU_HEADER.
19429 Return *BYTES_READ_PTR count of bytes read from BUF. */
19430
19431 static const char *
19432 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19433 const gdb_byte *buf,
19434 const struct comp_unit_head *cu_header,
19435 unsigned int *bytes_read_ptr)
19436 {
19437 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19438
19439 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19440 }
19441
19442 /* Return pointer to string at .debug_line_str offset as read from BUF.
19443 BUF is assumed to be in a compilation unit described by CU_HEADER.
19444 Return *BYTES_READ_PTR count of bytes read from BUF. */
19445
19446 static const char *
19447 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19448 bfd *abfd, const gdb_byte *buf,
19449 const struct comp_unit_head *cu_header,
19450 unsigned int *bytes_read_ptr)
19451 {
19452 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19453
19454 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19455 str_offset);
19456 }
19457
19458 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19459 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19460 ADDR_SIZE is the size of addresses from the CU header. */
19461
19462 static CORE_ADDR
19463 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19464 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19465 int addr_size)
19466 {
19467 struct objfile *objfile = dwarf2_per_objfile->objfile;
19468 bfd *abfd = objfile->obfd;
19469 const gdb_byte *info_ptr;
19470 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19471
19472 dwarf2_per_objfile->addr.read (objfile);
19473 if (dwarf2_per_objfile->addr.buffer == NULL)
19474 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19475 objfile_name (objfile));
19476 if (addr_base_or_zero + addr_index * addr_size
19477 >= dwarf2_per_objfile->addr.size)
19478 error (_("DW_FORM_addr_index pointing outside of "
19479 ".debug_addr section [in module %s]"),
19480 objfile_name (objfile));
19481 info_ptr = (dwarf2_per_objfile->addr.buffer
19482 + addr_base_or_zero + addr_index * addr_size);
19483 if (addr_size == 4)
19484 return bfd_get_32 (abfd, info_ptr);
19485 else
19486 return bfd_get_64 (abfd, info_ptr);
19487 }
19488
19489 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19490
19491 static CORE_ADDR
19492 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19493 {
19494 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19495 cu->addr_base, cu->header.addr_size);
19496 }
19497
19498 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19499
19500 static CORE_ADDR
19501 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19502 unsigned int *bytes_read)
19503 {
19504 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19505 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19506
19507 return read_addr_index (cu, addr_index);
19508 }
19509
19510 /* Given an index in .debug_addr, fetch the value.
19511 NOTE: This can be called during dwarf expression evaluation,
19512 long after the debug information has been read, and thus per_cu->cu
19513 may no longer exist. */
19514
19515 CORE_ADDR
19516 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19517 unsigned int addr_index)
19518 {
19519 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19520 struct dwarf2_cu *cu = per_cu->cu;
19521 gdb::optional<ULONGEST> addr_base;
19522 int addr_size;
19523
19524 /* We need addr_base and addr_size.
19525 If we don't have PER_CU->cu, we have to get it.
19526 Nasty, but the alternative is storing the needed info in PER_CU,
19527 which at this point doesn't seem justified: it's not clear how frequently
19528 it would get used and it would increase the size of every PER_CU.
19529 Entry points like dwarf2_per_cu_addr_size do a similar thing
19530 so we're not in uncharted territory here.
19531 Alas we need to be a bit more complicated as addr_base is contained
19532 in the DIE.
19533
19534 We don't need to read the entire CU(/TU).
19535 We just need the header and top level die.
19536
19537 IWBN to use the aging mechanism to let us lazily later discard the CU.
19538 For now we skip this optimization. */
19539
19540 if (cu != NULL)
19541 {
19542 addr_base = cu->addr_base;
19543 addr_size = cu->header.addr_size;
19544 }
19545 else
19546 {
19547 cutu_reader reader (per_cu, NULL, 0, 0, false);
19548 addr_base = reader.cu->addr_base;
19549 addr_size = reader.cu->header.addr_size;
19550 }
19551
19552 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19553 addr_size);
19554 }
19555
19556 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19557 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19558 DWO file. */
19559
19560 static const char *
19561 read_str_index (struct dwarf2_cu *cu,
19562 struct dwarf2_section_info *str_section,
19563 struct dwarf2_section_info *str_offsets_section,
19564 ULONGEST str_offsets_base, ULONGEST str_index)
19565 {
19566 struct dwarf2_per_objfile *dwarf2_per_objfile
19567 = cu->per_cu->dwarf2_per_objfile;
19568 struct objfile *objfile = dwarf2_per_objfile->objfile;
19569 const char *objf_name = objfile_name (objfile);
19570 bfd *abfd = objfile->obfd;
19571 const gdb_byte *info_ptr;
19572 ULONGEST str_offset;
19573 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19574
19575 str_section->read (objfile);
19576 str_offsets_section->read (objfile);
19577 if (str_section->buffer == NULL)
19578 error (_("%s used without %s section"
19579 " in CU at offset %s [in module %s]"),
19580 form_name, str_section->get_name (),
19581 sect_offset_str (cu->header.sect_off), objf_name);
19582 if (str_offsets_section->buffer == NULL)
19583 error (_("%s used without %s section"
19584 " in CU at offset %s [in module %s]"),
19585 form_name, str_section->get_name (),
19586 sect_offset_str (cu->header.sect_off), objf_name);
19587 info_ptr = (str_offsets_section->buffer
19588 + str_offsets_base
19589 + str_index * cu->header.offset_size);
19590 if (cu->header.offset_size == 4)
19591 str_offset = bfd_get_32 (abfd, info_ptr);
19592 else
19593 str_offset = bfd_get_64 (abfd, info_ptr);
19594 if (str_offset >= str_section->size)
19595 error (_("Offset from %s pointing outside of"
19596 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19597 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19598 return (const char *) (str_section->buffer + str_offset);
19599 }
19600
19601 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19602
19603 static const char *
19604 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19605 {
19606 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19607 ? reader->cu->header.addr_size : 0;
19608 return read_str_index (reader->cu,
19609 &reader->dwo_file->sections.str,
19610 &reader->dwo_file->sections.str_offsets,
19611 str_offsets_base, str_index);
19612 }
19613
19614 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19615
19616 static const char *
19617 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19618 {
19619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19620 const char *objf_name = objfile_name (objfile);
19621 static const char form_name[] = "DW_FORM_GNU_str_index";
19622 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19623
19624 if (!cu->str_offsets_base.has_value ())
19625 error (_("%s used in Fission stub without %s"
19626 " in CU at offset 0x%lx [in module %s]"),
19627 form_name, str_offsets_attr_name,
19628 (long) cu->header.offset_size, objf_name);
19629
19630 return read_str_index (cu,
19631 &cu->per_cu->dwarf2_per_objfile->str,
19632 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19633 *cu->str_offsets_base, str_index);
19634 }
19635
19636 /* Return the length of an LEB128 number in BUF. */
19637
19638 static int
19639 leb128_size (const gdb_byte *buf)
19640 {
19641 const gdb_byte *begin = buf;
19642 gdb_byte byte;
19643
19644 while (1)
19645 {
19646 byte = *buf++;
19647 if ((byte & 128) == 0)
19648 return buf - begin;
19649 }
19650 }
19651
19652 static void
19653 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19654 {
19655 switch (lang)
19656 {
19657 case DW_LANG_C89:
19658 case DW_LANG_C99:
19659 case DW_LANG_C11:
19660 case DW_LANG_C:
19661 case DW_LANG_UPC:
19662 cu->language = language_c;
19663 break;
19664 case DW_LANG_Java:
19665 case DW_LANG_C_plus_plus:
19666 case DW_LANG_C_plus_plus_11:
19667 case DW_LANG_C_plus_plus_14:
19668 cu->language = language_cplus;
19669 break;
19670 case DW_LANG_D:
19671 cu->language = language_d;
19672 break;
19673 case DW_LANG_Fortran77:
19674 case DW_LANG_Fortran90:
19675 case DW_LANG_Fortran95:
19676 case DW_LANG_Fortran03:
19677 case DW_LANG_Fortran08:
19678 cu->language = language_fortran;
19679 break;
19680 case DW_LANG_Go:
19681 cu->language = language_go;
19682 break;
19683 case DW_LANG_Mips_Assembler:
19684 cu->language = language_asm;
19685 break;
19686 case DW_LANG_Ada83:
19687 case DW_LANG_Ada95:
19688 cu->language = language_ada;
19689 break;
19690 case DW_LANG_Modula2:
19691 cu->language = language_m2;
19692 break;
19693 case DW_LANG_Pascal83:
19694 cu->language = language_pascal;
19695 break;
19696 case DW_LANG_ObjC:
19697 cu->language = language_objc;
19698 break;
19699 case DW_LANG_Rust:
19700 case DW_LANG_Rust_old:
19701 cu->language = language_rust;
19702 break;
19703 case DW_LANG_Cobol74:
19704 case DW_LANG_Cobol85:
19705 default:
19706 cu->language = language_minimal;
19707 break;
19708 }
19709 cu->language_defn = language_def (cu->language);
19710 }
19711
19712 /* Return the named attribute or NULL if not there. */
19713
19714 static struct attribute *
19715 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19716 {
19717 for (;;)
19718 {
19719 unsigned int i;
19720 struct attribute *spec = NULL;
19721
19722 for (i = 0; i < die->num_attrs; ++i)
19723 {
19724 if (die->attrs[i].name == name)
19725 return &die->attrs[i];
19726 if (die->attrs[i].name == DW_AT_specification
19727 || die->attrs[i].name == DW_AT_abstract_origin)
19728 spec = &die->attrs[i];
19729 }
19730
19731 if (!spec)
19732 break;
19733
19734 die = follow_die_ref (die, spec, &cu);
19735 }
19736
19737 return NULL;
19738 }
19739
19740 /* Return the named attribute or NULL if not there,
19741 but do not follow DW_AT_specification, etc.
19742 This is for use in contexts where we're reading .debug_types dies.
19743 Following DW_AT_specification, DW_AT_abstract_origin will take us
19744 back up the chain, and we want to go down. */
19745
19746 static struct attribute *
19747 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19748 {
19749 unsigned int i;
19750
19751 for (i = 0; i < die->num_attrs; ++i)
19752 if (die->attrs[i].name == name)
19753 return &die->attrs[i];
19754
19755 return NULL;
19756 }
19757
19758 /* Return the string associated with a string-typed attribute, or NULL if it
19759 is either not found or is of an incorrect type. */
19760
19761 static const char *
19762 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19763 {
19764 struct attribute *attr;
19765 const char *str = NULL;
19766
19767 attr = dwarf2_attr (die, name, cu);
19768
19769 if (attr != NULL)
19770 {
19771 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19772 || attr->form == DW_FORM_string
19773 || attr->form == DW_FORM_strx
19774 || attr->form == DW_FORM_strx1
19775 || attr->form == DW_FORM_strx2
19776 || attr->form == DW_FORM_strx3
19777 || attr->form == DW_FORM_strx4
19778 || attr->form == DW_FORM_GNU_str_index
19779 || attr->form == DW_FORM_GNU_strp_alt)
19780 str = DW_STRING (attr);
19781 else
19782 complaint (_("string type expected for attribute %s for "
19783 "DIE at %s in module %s"),
19784 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19785 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19786 }
19787
19788 return str;
19789 }
19790
19791 /* Return the dwo name or NULL if not present. If present, it is in either
19792 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19793 static const char *
19794 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19795 {
19796 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19797 if (dwo_name == nullptr)
19798 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19799 return dwo_name;
19800 }
19801
19802 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19803 and holds a non-zero value. This function should only be used for
19804 DW_FORM_flag or DW_FORM_flag_present attributes. */
19805
19806 static int
19807 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19808 {
19809 struct attribute *attr = dwarf2_attr (die, name, cu);
19810
19811 return (attr && DW_UNSND (attr));
19812 }
19813
19814 static int
19815 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19816 {
19817 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19818 which value is non-zero. However, we have to be careful with
19819 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19820 (via dwarf2_flag_true_p) follows this attribute. So we may
19821 end up accidently finding a declaration attribute that belongs
19822 to a different DIE referenced by the specification attribute,
19823 even though the given DIE does not have a declaration attribute. */
19824 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19825 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19826 }
19827
19828 /* Return the die giving the specification for DIE, if there is
19829 one. *SPEC_CU is the CU containing DIE on input, and the CU
19830 containing the return value on output. If there is no
19831 specification, but there is an abstract origin, that is
19832 returned. */
19833
19834 static struct die_info *
19835 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19836 {
19837 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19838 *spec_cu);
19839
19840 if (spec_attr == NULL)
19841 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19842
19843 if (spec_attr == NULL)
19844 return NULL;
19845 else
19846 return follow_die_ref (die, spec_attr, spec_cu);
19847 }
19848
19849 /* Stub for free_line_header to match void * callback types. */
19850
19851 static void
19852 free_line_header_voidp (void *arg)
19853 {
19854 struct line_header *lh = (struct line_header *) arg;
19855
19856 delete lh;
19857 }
19858
19859 void
19860 line_header::add_include_dir (const char *include_dir)
19861 {
19862 if (dwarf_line_debug >= 2)
19863 {
19864 size_t new_size;
19865 if (version >= 5)
19866 new_size = m_include_dirs.size ();
19867 else
19868 new_size = m_include_dirs.size () + 1;
19869 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19870 new_size, include_dir);
19871 }
19872 m_include_dirs.push_back (include_dir);
19873 }
19874
19875 void
19876 line_header::add_file_name (const char *name,
19877 dir_index d_index,
19878 unsigned int mod_time,
19879 unsigned int length)
19880 {
19881 if (dwarf_line_debug >= 2)
19882 {
19883 size_t new_size;
19884 if (version >= 5)
19885 new_size = file_names_size ();
19886 else
19887 new_size = file_names_size () + 1;
19888 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19889 new_size, name);
19890 }
19891 m_file_names.emplace_back (name, d_index, mod_time, length);
19892 }
19893
19894 /* A convenience function to find the proper .debug_line section for a CU. */
19895
19896 static struct dwarf2_section_info *
19897 get_debug_line_section (struct dwarf2_cu *cu)
19898 {
19899 struct dwarf2_section_info *section;
19900 struct dwarf2_per_objfile *dwarf2_per_objfile
19901 = cu->per_cu->dwarf2_per_objfile;
19902
19903 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19904 DWO file. */
19905 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19906 section = &cu->dwo_unit->dwo_file->sections.line;
19907 else if (cu->per_cu->is_dwz)
19908 {
19909 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19910
19911 section = &dwz->line;
19912 }
19913 else
19914 section = &dwarf2_per_objfile->line;
19915
19916 return section;
19917 }
19918
19919 /* Read directory or file name entry format, starting with byte of
19920 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19921 entries count and the entries themselves in the described entry
19922 format. */
19923
19924 static void
19925 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19926 bfd *abfd, const gdb_byte **bufp,
19927 struct line_header *lh,
19928 const struct comp_unit_head *cu_header,
19929 void (*callback) (struct line_header *lh,
19930 const char *name,
19931 dir_index d_index,
19932 unsigned int mod_time,
19933 unsigned int length))
19934 {
19935 gdb_byte format_count, formati;
19936 ULONGEST data_count, datai;
19937 const gdb_byte *buf = *bufp;
19938 const gdb_byte *format_header_data;
19939 unsigned int bytes_read;
19940
19941 format_count = read_1_byte (abfd, buf);
19942 buf += 1;
19943 format_header_data = buf;
19944 for (formati = 0; formati < format_count; formati++)
19945 {
19946 read_unsigned_leb128 (abfd, buf, &bytes_read);
19947 buf += bytes_read;
19948 read_unsigned_leb128 (abfd, buf, &bytes_read);
19949 buf += bytes_read;
19950 }
19951
19952 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19953 buf += bytes_read;
19954 for (datai = 0; datai < data_count; datai++)
19955 {
19956 const gdb_byte *format = format_header_data;
19957 struct file_entry fe;
19958
19959 for (formati = 0; formati < format_count; formati++)
19960 {
19961 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19962 format += bytes_read;
19963
19964 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19965 format += bytes_read;
19966
19967 gdb::optional<const char *> string;
19968 gdb::optional<unsigned int> uint;
19969
19970 switch (form)
19971 {
19972 case DW_FORM_string:
19973 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19974 buf += bytes_read;
19975 break;
19976
19977 case DW_FORM_line_strp:
19978 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19979 abfd, buf,
19980 cu_header,
19981 &bytes_read));
19982 buf += bytes_read;
19983 break;
19984
19985 case DW_FORM_data1:
19986 uint.emplace (read_1_byte (abfd, buf));
19987 buf += 1;
19988 break;
19989
19990 case DW_FORM_data2:
19991 uint.emplace (read_2_bytes (abfd, buf));
19992 buf += 2;
19993 break;
19994
19995 case DW_FORM_data4:
19996 uint.emplace (read_4_bytes (abfd, buf));
19997 buf += 4;
19998 break;
19999
20000 case DW_FORM_data8:
20001 uint.emplace (read_8_bytes (abfd, buf));
20002 buf += 8;
20003 break;
20004
20005 case DW_FORM_data16:
20006 /* This is used for MD5, but file_entry does not record MD5s. */
20007 buf += 16;
20008 break;
20009
20010 case DW_FORM_udata:
20011 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20012 buf += bytes_read;
20013 break;
20014
20015 case DW_FORM_block:
20016 /* It is valid only for DW_LNCT_timestamp which is ignored by
20017 current GDB. */
20018 break;
20019 }
20020
20021 switch (content_type)
20022 {
20023 case DW_LNCT_path:
20024 if (string.has_value ())
20025 fe.name = *string;
20026 break;
20027 case DW_LNCT_directory_index:
20028 if (uint.has_value ())
20029 fe.d_index = (dir_index) *uint;
20030 break;
20031 case DW_LNCT_timestamp:
20032 if (uint.has_value ())
20033 fe.mod_time = *uint;
20034 break;
20035 case DW_LNCT_size:
20036 if (uint.has_value ())
20037 fe.length = *uint;
20038 break;
20039 case DW_LNCT_MD5:
20040 break;
20041 default:
20042 complaint (_("Unknown format content type %s"),
20043 pulongest (content_type));
20044 }
20045 }
20046
20047 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20048 }
20049
20050 *bufp = buf;
20051 }
20052
20053 /* Read the statement program header starting at OFFSET in
20054 .debug_line, or .debug_line.dwo. Return a pointer
20055 to a struct line_header, allocated using xmalloc.
20056 Returns NULL if there is a problem reading the header, e.g., if it
20057 has a version we don't understand.
20058
20059 NOTE: the strings in the include directory and file name tables of
20060 the returned object point into the dwarf line section buffer,
20061 and must not be freed. */
20062
20063 static line_header_up
20064 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20065 {
20066 const gdb_byte *line_ptr;
20067 unsigned int bytes_read, offset_size;
20068 int i;
20069 const char *cur_dir, *cur_file;
20070 struct dwarf2_section_info *section;
20071 bfd *abfd;
20072 struct dwarf2_per_objfile *dwarf2_per_objfile
20073 = cu->per_cu->dwarf2_per_objfile;
20074
20075 section = get_debug_line_section (cu);
20076 section->read (dwarf2_per_objfile->objfile);
20077 if (section->buffer == NULL)
20078 {
20079 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20080 complaint (_("missing .debug_line.dwo section"));
20081 else
20082 complaint (_("missing .debug_line section"));
20083 return 0;
20084 }
20085
20086 /* We can't do this until we know the section is non-empty.
20087 Only then do we know we have such a section. */
20088 abfd = section->get_bfd_owner ();
20089
20090 /* Make sure that at least there's room for the total_length field.
20091 That could be 12 bytes long, but we're just going to fudge that. */
20092 if (to_underlying (sect_off) + 4 >= section->size)
20093 {
20094 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20095 return 0;
20096 }
20097
20098 line_header_up lh (new line_header ());
20099
20100 lh->sect_off = sect_off;
20101 lh->offset_in_dwz = cu->per_cu->is_dwz;
20102
20103 line_ptr = section->buffer + to_underlying (sect_off);
20104
20105 /* Read in the header. */
20106 lh->total_length =
20107 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20108 &bytes_read, &offset_size);
20109 line_ptr += bytes_read;
20110
20111 const gdb_byte *start_here = line_ptr;
20112
20113 if (line_ptr + lh->total_length > (section->buffer + section->size))
20114 {
20115 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20116 return 0;
20117 }
20118 lh->statement_program_end = start_here + lh->total_length;
20119 lh->version = read_2_bytes (abfd, line_ptr);
20120 line_ptr += 2;
20121 if (lh->version > 5)
20122 {
20123 /* This is a version we don't understand. The format could have
20124 changed in ways we don't handle properly so just punt. */
20125 complaint (_("unsupported version in .debug_line section"));
20126 return NULL;
20127 }
20128 if (lh->version >= 5)
20129 {
20130 gdb_byte segment_selector_size;
20131
20132 /* Skip address size. */
20133 read_1_byte (abfd, line_ptr);
20134 line_ptr += 1;
20135
20136 segment_selector_size = read_1_byte (abfd, line_ptr);
20137 line_ptr += 1;
20138 if (segment_selector_size != 0)
20139 {
20140 complaint (_("unsupported segment selector size %u "
20141 "in .debug_line section"),
20142 segment_selector_size);
20143 return NULL;
20144 }
20145 }
20146 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20147 line_ptr += offset_size;
20148 lh->statement_program_start = line_ptr + lh->header_length;
20149 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20150 line_ptr += 1;
20151 if (lh->version >= 4)
20152 {
20153 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20154 line_ptr += 1;
20155 }
20156 else
20157 lh->maximum_ops_per_instruction = 1;
20158
20159 if (lh->maximum_ops_per_instruction == 0)
20160 {
20161 lh->maximum_ops_per_instruction = 1;
20162 complaint (_("invalid maximum_ops_per_instruction "
20163 "in `.debug_line' section"));
20164 }
20165
20166 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20167 line_ptr += 1;
20168 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20169 line_ptr += 1;
20170 lh->line_range = read_1_byte (abfd, line_ptr);
20171 line_ptr += 1;
20172 lh->opcode_base = read_1_byte (abfd, line_ptr);
20173 line_ptr += 1;
20174 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20175
20176 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20177 for (i = 1; i < lh->opcode_base; ++i)
20178 {
20179 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20180 line_ptr += 1;
20181 }
20182
20183 if (lh->version >= 5)
20184 {
20185 /* Read directory table. */
20186 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20187 &cu->header,
20188 [] (struct line_header *header, const char *name,
20189 dir_index d_index, unsigned int mod_time,
20190 unsigned int length)
20191 {
20192 header->add_include_dir (name);
20193 });
20194
20195 /* Read file name table. */
20196 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20197 &cu->header,
20198 [] (struct line_header *header, const char *name,
20199 dir_index d_index, unsigned int mod_time,
20200 unsigned int length)
20201 {
20202 header->add_file_name (name, d_index, mod_time, length);
20203 });
20204 }
20205 else
20206 {
20207 /* Read directory table. */
20208 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20209 {
20210 line_ptr += bytes_read;
20211 lh->add_include_dir (cur_dir);
20212 }
20213 line_ptr += bytes_read;
20214
20215 /* Read file name table. */
20216 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20217 {
20218 unsigned int mod_time, length;
20219 dir_index d_index;
20220
20221 line_ptr += bytes_read;
20222 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20223 line_ptr += bytes_read;
20224 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20225 line_ptr += bytes_read;
20226 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20227 line_ptr += bytes_read;
20228
20229 lh->add_file_name (cur_file, d_index, mod_time, length);
20230 }
20231 line_ptr += bytes_read;
20232 }
20233
20234 if (line_ptr > (section->buffer + section->size))
20235 complaint (_("line number info header doesn't "
20236 "fit in `.debug_line' section"));
20237
20238 return lh;
20239 }
20240
20241 /* Subroutine of dwarf_decode_lines to simplify it.
20242 Return the file name of the psymtab for the given file_entry.
20243 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20244 If space for the result is malloc'd, *NAME_HOLDER will be set.
20245 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20246
20247 static const char *
20248 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20249 const dwarf2_psymtab *pst,
20250 const char *comp_dir,
20251 gdb::unique_xmalloc_ptr<char> *name_holder)
20252 {
20253 const char *include_name = fe.name;
20254 const char *include_name_to_compare = include_name;
20255 const char *pst_filename;
20256 int file_is_pst;
20257
20258 const char *dir_name = fe.include_dir (lh);
20259
20260 gdb::unique_xmalloc_ptr<char> hold_compare;
20261 if (!IS_ABSOLUTE_PATH (include_name)
20262 && (dir_name != NULL || comp_dir != NULL))
20263 {
20264 /* Avoid creating a duplicate psymtab for PST.
20265 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20266 Before we do the comparison, however, we need to account
20267 for DIR_NAME and COMP_DIR.
20268 First prepend dir_name (if non-NULL). If we still don't
20269 have an absolute path prepend comp_dir (if non-NULL).
20270 However, the directory we record in the include-file's
20271 psymtab does not contain COMP_DIR (to match the
20272 corresponding symtab(s)).
20273
20274 Example:
20275
20276 bash$ cd /tmp
20277 bash$ gcc -g ./hello.c
20278 include_name = "hello.c"
20279 dir_name = "."
20280 DW_AT_comp_dir = comp_dir = "/tmp"
20281 DW_AT_name = "./hello.c"
20282
20283 */
20284
20285 if (dir_name != NULL)
20286 {
20287 name_holder->reset (concat (dir_name, SLASH_STRING,
20288 include_name, (char *) NULL));
20289 include_name = name_holder->get ();
20290 include_name_to_compare = include_name;
20291 }
20292 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20293 {
20294 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20295 include_name, (char *) NULL));
20296 include_name_to_compare = hold_compare.get ();
20297 }
20298 }
20299
20300 pst_filename = pst->filename;
20301 gdb::unique_xmalloc_ptr<char> copied_name;
20302 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20303 {
20304 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20305 pst_filename, (char *) NULL));
20306 pst_filename = copied_name.get ();
20307 }
20308
20309 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20310
20311 if (file_is_pst)
20312 return NULL;
20313 return include_name;
20314 }
20315
20316 /* State machine to track the state of the line number program. */
20317
20318 class lnp_state_machine
20319 {
20320 public:
20321 /* Initialize a machine state for the start of a line number
20322 program. */
20323 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20324 bool record_lines_p);
20325
20326 file_entry *current_file ()
20327 {
20328 /* lh->file_names is 0-based, but the file name numbers in the
20329 statement program are 1-based. */
20330 return m_line_header->file_name_at (m_file);
20331 }
20332
20333 /* Record the line in the state machine. END_SEQUENCE is true if
20334 we're processing the end of a sequence. */
20335 void record_line (bool end_sequence);
20336
20337 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20338 nop-out rest of the lines in this sequence. */
20339 void check_line_address (struct dwarf2_cu *cu,
20340 const gdb_byte *line_ptr,
20341 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20342
20343 void handle_set_discriminator (unsigned int discriminator)
20344 {
20345 m_discriminator = discriminator;
20346 m_line_has_non_zero_discriminator |= discriminator != 0;
20347 }
20348
20349 /* Handle DW_LNE_set_address. */
20350 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20351 {
20352 m_op_index = 0;
20353 address += baseaddr;
20354 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20355 }
20356
20357 /* Handle DW_LNS_advance_pc. */
20358 void handle_advance_pc (CORE_ADDR adjust);
20359
20360 /* Handle a special opcode. */
20361 void handle_special_opcode (unsigned char op_code);
20362
20363 /* Handle DW_LNS_advance_line. */
20364 void handle_advance_line (int line_delta)
20365 {
20366 advance_line (line_delta);
20367 }
20368
20369 /* Handle DW_LNS_set_file. */
20370 void handle_set_file (file_name_index file);
20371
20372 /* Handle DW_LNS_negate_stmt. */
20373 void handle_negate_stmt ()
20374 {
20375 m_is_stmt = !m_is_stmt;
20376 }
20377
20378 /* Handle DW_LNS_const_add_pc. */
20379 void handle_const_add_pc ();
20380
20381 /* Handle DW_LNS_fixed_advance_pc. */
20382 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20383 {
20384 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20385 m_op_index = 0;
20386 }
20387
20388 /* Handle DW_LNS_copy. */
20389 void handle_copy ()
20390 {
20391 record_line (false);
20392 m_discriminator = 0;
20393 }
20394
20395 /* Handle DW_LNE_end_sequence. */
20396 void handle_end_sequence ()
20397 {
20398 m_currently_recording_lines = true;
20399 }
20400
20401 private:
20402 /* Advance the line by LINE_DELTA. */
20403 void advance_line (int line_delta)
20404 {
20405 m_line += line_delta;
20406
20407 if (line_delta != 0)
20408 m_line_has_non_zero_discriminator = m_discriminator != 0;
20409 }
20410
20411 struct dwarf2_cu *m_cu;
20412
20413 gdbarch *m_gdbarch;
20414
20415 /* True if we're recording lines.
20416 Otherwise we're building partial symtabs and are just interested in
20417 finding include files mentioned by the line number program. */
20418 bool m_record_lines_p;
20419
20420 /* The line number header. */
20421 line_header *m_line_header;
20422
20423 /* These are part of the standard DWARF line number state machine,
20424 and initialized according to the DWARF spec. */
20425
20426 unsigned char m_op_index = 0;
20427 /* The line table index of the current file. */
20428 file_name_index m_file = 1;
20429 unsigned int m_line = 1;
20430
20431 /* These are initialized in the constructor. */
20432
20433 CORE_ADDR m_address;
20434 bool m_is_stmt;
20435 unsigned int m_discriminator;
20436
20437 /* Additional bits of state we need to track. */
20438
20439 /* The last file that we called dwarf2_start_subfile for.
20440 This is only used for TLLs. */
20441 unsigned int m_last_file = 0;
20442 /* The last file a line number was recorded for. */
20443 struct subfile *m_last_subfile = NULL;
20444
20445 /* When true, record the lines we decode. */
20446 bool m_currently_recording_lines = false;
20447
20448 /* The last line number that was recorded, used to coalesce
20449 consecutive entries for the same line. This can happen, for
20450 example, when discriminators are present. PR 17276. */
20451 unsigned int m_last_line = 0;
20452 bool m_line_has_non_zero_discriminator = false;
20453 };
20454
20455 void
20456 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20457 {
20458 CORE_ADDR addr_adj = (((m_op_index + adjust)
20459 / m_line_header->maximum_ops_per_instruction)
20460 * m_line_header->minimum_instruction_length);
20461 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20462 m_op_index = ((m_op_index + adjust)
20463 % m_line_header->maximum_ops_per_instruction);
20464 }
20465
20466 void
20467 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20468 {
20469 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20470 CORE_ADDR addr_adj = (((m_op_index
20471 + (adj_opcode / m_line_header->line_range))
20472 / m_line_header->maximum_ops_per_instruction)
20473 * m_line_header->minimum_instruction_length);
20474 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20475 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20476 % m_line_header->maximum_ops_per_instruction);
20477
20478 int line_delta = (m_line_header->line_base
20479 + (adj_opcode % m_line_header->line_range));
20480 advance_line (line_delta);
20481 record_line (false);
20482 m_discriminator = 0;
20483 }
20484
20485 void
20486 lnp_state_machine::handle_set_file (file_name_index file)
20487 {
20488 m_file = file;
20489
20490 const file_entry *fe = current_file ();
20491 if (fe == NULL)
20492 dwarf2_debug_line_missing_file_complaint ();
20493 else if (m_record_lines_p)
20494 {
20495 const char *dir = fe->include_dir (m_line_header);
20496
20497 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20498 m_line_has_non_zero_discriminator = m_discriminator != 0;
20499 dwarf2_start_subfile (m_cu, fe->name, dir);
20500 }
20501 }
20502
20503 void
20504 lnp_state_machine::handle_const_add_pc ()
20505 {
20506 CORE_ADDR adjust
20507 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20508
20509 CORE_ADDR addr_adj
20510 = (((m_op_index + adjust)
20511 / m_line_header->maximum_ops_per_instruction)
20512 * m_line_header->minimum_instruction_length);
20513
20514 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20515 m_op_index = ((m_op_index + adjust)
20516 % m_line_header->maximum_ops_per_instruction);
20517 }
20518
20519 /* Return non-zero if we should add LINE to the line number table.
20520 LINE is the line to add, LAST_LINE is the last line that was added,
20521 LAST_SUBFILE is the subfile for LAST_LINE.
20522 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20523 had a non-zero discriminator.
20524
20525 We have to be careful in the presence of discriminators.
20526 E.g., for this line:
20527
20528 for (i = 0; i < 100000; i++);
20529
20530 clang can emit four line number entries for that one line,
20531 each with a different discriminator.
20532 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20533
20534 However, we want gdb to coalesce all four entries into one.
20535 Otherwise the user could stepi into the middle of the line and
20536 gdb would get confused about whether the pc really was in the
20537 middle of the line.
20538
20539 Things are further complicated by the fact that two consecutive
20540 line number entries for the same line is a heuristic used by gcc
20541 to denote the end of the prologue. So we can't just discard duplicate
20542 entries, we have to be selective about it. The heuristic we use is
20543 that we only collapse consecutive entries for the same line if at least
20544 one of those entries has a non-zero discriminator. PR 17276.
20545
20546 Note: Addresses in the line number state machine can never go backwards
20547 within one sequence, thus this coalescing is ok. */
20548
20549 static int
20550 dwarf_record_line_p (struct dwarf2_cu *cu,
20551 unsigned int line, unsigned int last_line,
20552 int line_has_non_zero_discriminator,
20553 struct subfile *last_subfile)
20554 {
20555 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20556 return 1;
20557 if (line != last_line)
20558 return 1;
20559 /* Same line for the same file that we've seen already.
20560 As a last check, for pr 17276, only record the line if the line
20561 has never had a non-zero discriminator. */
20562 if (!line_has_non_zero_discriminator)
20563 return 1;
20564 return 0;
20565 }
20566
20567 /* Use the CU's builder to record line number LINE beginning at
20568 address ADDRESS in the line table of subfile SUBFILE. */
20569
20570 static void
20571 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20572 unsigned int line, CORE_ADDR address,
20573 struct dwarf2_cu *cu)
20574 {
20575 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20576
20577 if (dwarf_line_debug)
20578 {
20579 fprintf_unfiltered (gdb_stdlog,
20580 "Recording line %u, file %s, address %s\n",
20581 line, lbasename (subfile->name),
20582 paddress (gdbarch, address));
20583 }
20584
20585 if (cu != nullptr)
20586 cu->get_builder ()->record_line (subfile, line, addr);
20587 }
20588
20589 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20590 Mark the end of a set of line number records.
20591 The arguments are the same as for dwarf_record_line_1.
20592 If SUBFILE is NULL the request is ignored. */
20593
20594 static void
20595 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20596 CORE_ADDR address, struct dwarf2_cu *cu)
20597 {
20598 if (subfile == NULL)
20599 return;
20600
20601 if (dwarf_line_debug)
20602 {
20603 fprintf_unfiltered (gdb_stdlog,
20604 "Finishing current line, file %s, address %s\n",
20605 lbasename (subfile->name),
20606 paddress (gdbarch, address));
20607 }
20608
20609 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20610 }
20611
20612 void
20613 lnp_state_machine::record_line (bool end_sequence)
20614 {
20615 if (dwarf_line_debug)
20616 {
20617 fprintf_unfiltered (gdb_stdlog,
20618 "Processing actual line %u: file %u,"
20619 " address %s, is_stmt %u, discrim %u%s\n",
20620 m_line, m_file,
20621 paddress (m_gdbarch, m_address),
20622 m_is_stmt, m_discriminator,
20623 (end_sequence ? "\t(end sequence)" : ""));
20624 }
20625
20626 file_entry *fe = current_file ();
20627
20628 if (fe == NULL)
20629 dwarf2_debug_line_missing_file_complaint ();
20630 /* For now we ignore lines not starting on an instruction boundary.
20631 But not when processing end_sequence for compatibility with the
20632 previous version of the code. */
20633 else if (m_op_index == 0 || end_sequence)
20634 {
20635 fe->included_p = 1;
20636 if (m_record_lines_p
20637 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20638 {
20639 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20640 || end_sequence)
20641 {
20642 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20643 m_currently_recording_lines ? m_cu : nullptr);
20644 }
20645
20646 if (!end_sequence)
20647 {
20648 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20649 m_line_has_non_zero_discriminator,
20650 m_last_subfile))
20651 {
20652 buildsym_compunit *builder = m_cu->get_builder ();
20653 dwarf_record_line_1 (m_gdbarch,
20654 builder->get_current_subfile (),
20655 m_line, m_address,
20656 m_currently_recording_lines ? m_cu : nullptr);
20657 }
20658 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20659 m_last_line = m_line;
20660 }
20661 }
20662 }
20663 }
20664
20665 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20666 line_header *lh, bool record_lines_p)
20667 {
20668 m_cu = cu;
20669 m_gdbarch = arch;
20670 m_record_lines_p = record_lines_p;
20671 m_line_header = lh;
20672
20673 m_currently_recording_lines = true;
20674
20675 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20676 was a line entry for it so that the backend has a chance to adjust it
20677 and also record it in case it needs it. This is currently used by MIPS
20678 code, cf. `mips_adjust_dwarf2_line'. */
20679 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20680 m_is_stmt = lh->default_is_stmt;
20681 m_discriminator = 0;
20682 }
20683
20684 void
20685 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20686 const gdb_byte *line_ptr,
20687 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20688 {
20689 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20690 the pc range of the CU. However, we restrict the test to only ADDRESS
20691 values of zero to preserve GDB's previous behaviour which is to handle
20692 the specific case of a function being GC'd by the linker. */
20693
20694 if (address == 0 && address < unrelocated_lowpc)
20695 {
20696 /* This line table is for a function which has been
20697 GCd by the linker. Ignore it. PR gdb/12528 */
20698
20699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20700 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20701
20702 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20703 line_offset, objfile_name (objfile));
20704 m_currently_recording_lines = false;
20705 /* Note: m_currently_recording_lines is left as false until we see
20706 DW_LNE_end_sequence. */
20707 }
20708 }
20709
20710 /* Subroutine of dwarf_decode_lines to simplify it.
20711 Process the line number information in LH.
20712 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20713 program in order to set included_p for every referenced header. */
20714
20715 static void
20716 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20717 const int decode_for_pst_p, CORE_ADDR lowpc)
20718 {
20719 const gdb_byte *line_ptr, *extended_end;
20720 const gdb_byte *line_end;
20721 unsigned int bytes_read, extended_len;
20722 unsigned char op_code, extended_op;
20723 CORE_ADDR baseaddr;
20724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20725 bfd *abfd = objfile->obfd;
20726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20727 /* True if we're recording line info (as opposed to building partial
20728 symtabs and just interested in finding include files mentioned by
20729 the line number program). */
20730 bool record_lines_p = !decode_for_pst_p;
20731
20732 baseaddr = objfile->text_section_offset ();
20733
20734 line_ptr = lh->statement_program_start;
20735 line_end = lh->statement_program_end;
20736
20737 /* Read the statement sequences until there's nothing left. */
20738 while (line_ptr < line_end)
20739 {
20740 /* The DWARF line number program state machine. Reset the state
20741 machine at the start of each sequence. */
20742 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20743 bool end_sequence = false;
20744
20745 if (record_lines_p)
20746 {
20747 /* Start a subfile for the current file of the state
20748 machine. */
20749 const file_entry *fe = state_machine.current_file ();
20750
20751 if (fe != NULL)
20752 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20753 }
20754
20755 /* Decode the table. */
20756 while (line_ptr < line_end && !end_sequence)
20757 {
20758 op_code = read_1_byte (abfd, line_ptr);
20759 line_ptr += 1;
20760
20761 if (op_code >= lh->opcode_base)
20762 {
20763 /* Special opcode. */
20764 state_machine.handle_special_opcode (op_code);
20765 }
20766 else switch (op_code)
20767 {
20768 case DW_LNS_extended_op:
20769 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20770 &bytes_read);
20771 line_ptr += bytes_read;
20772 extended_end = line_ptr + extended_len;
20773 extended_op = read_1_byte (abfd, line_ptr);
20774 line_ptr += 1;
20775 switch (extended_op)
20776 {
20777 case DW_LNE_end_sequence:
20778 state_machine.handle_end_sequence ();
20779 end_sequence = true;
20780 break;
20781 case DW_LNE_set_address:
20782 {
20783 CORE_ADDR address
20784 = read_address (abfd, line_ptr, cu, &bytes_read);
20785 line_ptr += bytes_read;
20786
20787 state_machine.check_line_address (cu, line_ptr,
20788 lowpc - baseaddr, address);
20789 state_machine.handle_set_address (baseaddr, address);
20790 }
20791 break;
20792 case DW_LNE_define_file:
20793 {
20794 const char *cur_file;
20795 unsigned int mod_time, length;
20796 dir_index dindex;
20797
20798 cur_file = read_direct_string (abfd, line_ptr,
20799 &bytes_read);
20800 line_ptr += bytes_read;
20801 dindex = (dir_index)
20802 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20803 line_ptr += bytes_read;
20804 mod_time =
20805 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20806 line_ptr += bytes_read;
20807 length =
20808 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20809 line_ptr += bytes_read;
20810 lh->add_file_name (cur_file, dindex, mod_time, length);
20811 }
20812 break;
20813 case DW_LNE_set_discriminator:
20814 {
20815 /* The discriminator is not interesting to the
20816 debugger; just ignore it. We still need to
20817 check its value though:
20818 if there are consecutive entries for the same
20819 (non-prologue) line we want to coalesce them.
20820 PR 17276. */
20821 unsigned int discr
20822 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20823 line_ptr += bytes_read;
20824
20825 state_machine.handle_set_discriminator (discr);
20826 }
20827 break;
20828 default:
20829 complaint (_("mangled .debug_line section"));
20830 return;
20831 }
20832 /* Make sure that we parsed the extended op correctly. If e.g.
20833 we expected a different address size than the producer used,
20834 we may have read the wrong number of bytes. */
20835 if (line_ptr != extended_end)
20836 {
20837 complaint (_("mangled .debug_line section"));
20838 return;
20839 }
20840 break;
20841 case DW_LNS_copy:
20842 state_machine.handle_copy ();
20843 break;
20844 case DW_LNS_advance_pc:
20845 {
20846 CORE_ADDR adjust
20847 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20848 line_ptr += bytes_read;
20849
20850 state_machine.handle_advance_pc (adjust);
20851 }
20852 break;
20853 case DW_LNS_advance_line:
20854 {
20855 int line_delta
20856 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20857 line_ptr += bytes_read;
20858
20859 state_machine.handle_advance_line (line_delta);
20860 }
20861 break;
20862 case DW_LNS_set_file:
20863 {
20864 file_name_index file
20865 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20866 &bytes_read);
20867 line_ptr += bytes_read;
20868
20869 state_machine.handle_set_file (file);
20870 }
20871 break;
20872 case DW_LNS_set_column:
20873 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20874 line_ptr += bytes_read;
20875 break;
20876 case DW_LNS_negate_stmt:
20877 state_machine.handle_negate_stmt ();
20878 break;
20879 case DW_LNS_set_basic_block:
20880 break;
20881 /* Add to the address register of the state machine the
20882 address increment value corresponding to special opcode
20883 255. I.e., this value is scaled by the minimum
20884 instruction length since special opcode 255 would have
20885 scaled the increment. */
20886 case DW_LNS_const_add_pc:
20887 state_machine.handle_const_add_pc ();
20888 break;
20889 case DW_LNS_fixed_advance_pc:
20890 {
20891 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20892 line_ptr += 2;
20893
20894 state_machine.handle_fixed_advance_pc (addr_adj);
20895 }
20896 break;
20897 default:
20898 {
20899 /* Unknown standard opcode, ignore it. */
20900 int i;
20901
20902 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20903 {
20904 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20905 line_ptr += bytes_read;
20906 }
20907 }
20908 }
20909 }
20910
20911 if (!end_sequence)
20912 dwarf2_debug_line_missing_end_sequence_complaint ();
20913
20914 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20915 in which case we still finish recording the last line). */
20916 state_machine.record_line (true);
20917 }
20918 }
20919
20920 /* Decode the Line Number Program (LNP) for the given line_header
20921 structure and CU. The actual information extracted and the type
20922 of structures created from the LNP depends on the value of PST.
20923
20924 1. If PST is NULL, then this procedure uses the data from the program
20925 to create all necessary symbol tables, and their linetables.
20926
20927 2. If PST is not NULL, this procedure reads the program to determine
20928 the list of files included by the unit represented by PST, and
20929 builds all the associated partial symbol tables.
20930
20931 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20932 It is used for relative paths in the line table.
20933 NOTE: When processing partial symtabs (pst != NULL),
20934 comp_dir == pst->dirname.
20935
20936 NOTE: It is important that psymtabs have the same file name (via strcmp)
20937 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20938 symtab we don't use it in the name of the psymtabs we create.
20939 E.g. expand_line_sal requires this when finding psymtabs to expand.
20940 A good testcase for this is mb-inline.exp.
20941
20942 LOWPC is the lowest address in CU (or 0 if not known).
20943
20944 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20945 for its PC<->lines mapping information. Otherwise only the filename
20946 table is read in. */
20947
20948 static void
20949 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20950 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20951 CORE_ADDR lowpc, int decode_mapping)
20952 {
20953 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20954 const int decode_for_pst_p = (pst != NULL);
20955
20956 if (decode_mapping)
20957 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20958
20959 if (decode_for_pst_p)
20960 {
20961 /* Now that we're done scanning the Line Header Program, we can
20962 create the psymtab of each included file. */
20963 for (auto &file_entry : lh->file_names ())
20964 if (file_entry.included_p == 1)
20965 {
20966 gdb::unique_xmalloc_ptr<char> name_holder;
20967 const char *include_name =
20968 psymtab_include_file_name (lh, file_entry, pst,
20969 comp_dir, &name_holder);
20970 if (include_name != NULL)
20971 dwarf2_create_include_psymtab (include_name, pst, objfile);
20972 }
20973 }
20974 else
20975 {
20976 /* Make sure a symtab is created for every file, even files
20977 which contain only variables (i.e. no code with associated
20978 line numbers). */
20979 buildsym_compunit *builder = cu->get_builder ();
20980 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20981
20982 for (auto &fe : lh->file_names ())
20983 {
20984 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20985 if (builder->get_current_subfile ()->symtab == NULL)
20986 {
20987 builder->get_current_subfile ()->symtab
20988 = allocate_symtab (cust,
20989 builder->get_current_subfile ()->name);
20990 }
20991 fe.symtab = builder->get_current_subfile ()->symtab;
20992 }
20993 }
20994 }
20995
20996 /* Start a subfile for DWARF. FILENAME is the name of the file and
20997 DIRNAME the name of the source directory which contains FILENAME
20998 or NULL if not known.
20999 This routine tries to keep line numbers from identical absolute and
21000 relative file names in a common subfile.
21001
21002 Using the `list' example from the GDB testsuite, which resides in
21003 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21004 of /srcdir/list0.c yields the following debugging information for list0.c:
21005
21006 DW_AT_name: /srcdir/list0.c
21007 DW_AT_comp_dir: /compdir
21008 files.files[0].name: list0.h
21009 files.files[0].dir: /srcdir
21010 files.files[1].name: list0.c
21011 files.files[1].dir: /srcdir
21012
21013 The line number information for list0.c has to end up in a single
21014 subfile, so that `break /srcdir/list0.c:1' works as expected.
21015 start_subfile will ensure that this happens provided that we pass the
21016 concatenation of files.files[1].dir and files.files[1].name as the
21017 subfile's name. */
21018
21019 static void
21020 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21021 const char *dirname)
21022 {
21023 gdb::unique_xmalloc_ptr<char> copy;
21024
21025 /* In order not to lose the line information directory,
21026 we concatenate it to the filename when it makes sense.
21027 Note that the Dwarf3 standard says (speaking of filenames in line
21028 information): ``The directory index is ignored for file names
21029 that represent full path names''. Thus ignoring dirname in the
21030 `else' branch below isn't an issue. */
21031
21032 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21033 {
21034 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21035 filename = copy.get ();
21036 }
21037
21038 cu->get_builder ()->start_subfile (filename);
21039 }
21040
21041 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21042 buildsym_compunit constructor. */
21043
21044 struct compunit_symtab *
21045 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21046 CORE_ADDR low_pc)
21047 {
21048 gdb_assert (m_builder == nullptr);
21049
21050 m_builder.reset (new struct buildsym_compunit
21051 (per_cu->dwarf2_per_objfile->objfile,
21052 name, comp_dir, language, low_pc));
21053
21054 list_in_scope = get_builder ()->get_file_symbols ();
21055
21056 get_builder ()->record_debugformat ("DWARF 2");
21057 get_builder ()->record_producer (producer);
21058
21059 processing_has_namespace_info = false;
21060
21061 return get_builder ()->get_compunit_symtab ();
21062 }
21063
21064 static void
21065 var_decode_location (struct attribute *attr, struct symbol *sym,
21066 struct dwarf2_cu *cu)
21067 {
21068 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21069 struct comp_unit_head *cu_header = &cu->header;
21070
21071 /* NOTE drow/2003-01-30: There used to be a comment and some special
21072 code here to turn a symbol with DW_AT_external and a
21073 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21074 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21075 with some versions of binutils) where shared libraries could have
21076 relocations against symbols in their debug information - the
21077 minimal symbol would have the right address, but the debug info
21078 would not. It's no longer necessary, because we will explicitly
21079 apply relocations when we read in the debug information now. */
21080
21081 /* A DW_AT_location attribute with no contents indicates that a
21082 variable has been optimized away. */
21083 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21084 {
21085 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21086 return;
21087 }
21088
21089 /* Handle one degenerate form of location expression specially, to
21090 preserve GDB's previous behavior when section offsets are
21091 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21092 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21093
21094 if (attr->form_is_block ()
21095 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21096 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21097 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21098 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21099 && (DW_BLOCK (attr)->size
21100 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21101 {
21102 unsigned int dummy;
21103
21104 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21105 SET_SYMBOL_VALUE_ADDRESS (sym,
21106 read_address (objfile->obfd,
21107 DW_BLOCK (attr)->data + 1,
21108 cu, &dummy));
21109 else
21110 SET_SYMBOL_VALUE_ADDRESS
21111 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21112 &dummy));
21113 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21114 fixup_symbol_section (sym, objfile);
21115 SET_SYMBOL_VALUE_ADDRESS
21116 (sym,
21117 SYMBOL_VALUE_ADDRESS (sym)
21118 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21119 return;
21120 }
21121
21122 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21123 expression evaluator, and use LOC_COMPUTED only when necessary
21124 (i.e. when the value of a register or memory location is
21125 referenced, or a thread-local block, etc.). Then again, it might
21126 not be worthwhile. I'm assuming that it isn't unless performance
21127 or memory numbers show me otherwise. */
21128
21129 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21130
21131 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21132 cu->has_loclist = true;
21133 }
21134
21135 /* Given a pointer to a DWARF information entry, figure out if we need
21136 to make a symbol table entry for it, and if so, create a new entry
21137 and return a pointer to it.
21138 If TYPE is NULL, determine symbol type from the die, otherwise
21139 used the passed type.
21140 If SPACE is not NULL, use it to hold the new symbol. If it is
21141 NULL, allocate a new symbol on the objfile's obstack. */
21142
21143 static struct symbol *
21144 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21145 struct symbol *space)
21146 {
21147 struct dwarf2_per_objfile *dwarf2_per_objfile
21148 = cu->per_cu->dwarf2_per_objfile;
21149 struct objfile *objfile = dwarf2_per_objfile->objfile;
21150 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21151 struct symbol *sym = NULL;
21152 const char *name;
21153 struct attribute *attr = NULL;
21154 struct attribute *attr2 = NULL;
21155 CORE_ADDR baseaddr;
21156 struct pending **list_to_add = NULL;
21157
21158 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21159
21160 baseaddr = objfile->text_section_offset ();
21161
21162 name = dwarf2_name (die, cu);
21163 if (name)
21164 {
21165 const char *linkagename;
21166 int suppress_add = 0;
21167
21168 if (space)
21169 sym = space;
21170 else
21171 sym = allocate_symbol (objfile);
21172 OBJSTAT (objfile, n_syms++);
21173
21174 /* Cache this symbol's name and the name's demangled form (if any). */
21175 sym->set_language (cu->language, &objfile->objfile_obstack);
21176 linkagename = dwarf2_physname (name, die, cu);
21177 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21178
21179 /* Fortran does not have mangling standard and the mangling does differ
21180 between gfortran, iFort etc. */
21181 if (cu->language == language_fortran
21182 && symbol_get_demangled_name (sym) == NULL)
21183 symbol_set_demangled_name (sym,
21184 dwarf2_full_name (name, die, cu),
21185 NULL);
21186
21187 /* Default assumptions.
21188 Use the passed type or decode it from the die. */
21189 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21190 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21191 if (type != NULL)
21192 SYMBOL_TYPE (sym) = type;
21193 else
21194 SYMBOL_TYPE (sym) = die_type (die, cu);
21195 attr = dwarf2_attr (die,
21196 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21197 cu);
21198 if (attr != nullptr)
21199 {
21200 SYMBOL_LINE (sym) = DW_UNSND (attr);
21201 }
21202
21203 attr = dwarf2_attr (die,
21204 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21205 cu);
21206 if (attr != nullptr)
21207 {
21208 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21209 struct file_entry *fe;
21210
21211 if (cu->line_header != NULL)
21212 fe = cu->line_header->file_name_at (file_index);
21213 else
21214 fe = NULL;
21215
21216 if (fe == NULL)
21217 complaint (_("file index out of range"));
21218 else
21219 symbol_set_symtab (sym, fe->symtab);
21220 }
21221
21222 switch (die->tag)
21223 {
21224 case DW_TAG_label:
21225 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21226 if (attr != nullptr)
21227 {
21228 CORE_ADDR addr;
21229
21230 addr = attr->value_as_address ();
21231 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21232 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21233 }
21234 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21235 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21236 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21237 add_symbol_to_list (sym, cu->list_in_scope);
21238 break;
21239 case DW_TAG_subprogram:
21240 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21241 finish_block. */
21242 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21243 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21244 if ((attr2 && (DW_UNSND (attr2) != 0))
21245 || cu->language == language_ada
21246 || cu->language == language_fortran)
21247 {
21248 /* Subprograms marked external are stored as a global symbol.
21249 Ada and Fortran subprograms, whether marked external or
21250 not, are always stored as a global symbol, because we want
21251 to be able to access them globally. For instance, we want
21252 to be able to break on a nested subprogram without having
21253 to specify the context. */
21254 list_to_add = cu->get_builder ()->get_global_symbols ();
21255 }
21256 else
21257 {
21258 list_to_add = cu->list_in_scope;
21259 }
21260 break;
21261 case DW_TAG_inlined_subroutine:
21262 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21263 finish_block. */
21264 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21265 SYMBOL_INLINED (sym) = 1;
21266 list_to_add = cu->list_in_scope;
21267 break;
21268 case DW_TAG_template_value_param:
21269 suppress_add = 1;
21270 /* Fall through. */
21271 case DW_TAG_constant:
21272 case DW_TAG_variable:
21273 case DW_TAG_member:
21274 /* Compilation with minimal debug info may result in
21275 variables with missing type entries. Change the
21276 misleading `void' type to something sensible. */
21277 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21278 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21279
21280 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21281 /* In the case of DW_TAG_member, we should only be called for
21282 static const members. */
21283 if (die->tag == DW_TAG_member)
21284 {
21285 /* dwarf2_add_field uses die_is_declaration,
21286 so we do the same. */
21287 gdb_assert (die_is_declaration (die, cu));
21288 gdb_assert (attr);
21289 }
21290 if (attr != nullptr)
21291 {
21292 dwarf2_const_value (attr, sym, cu);
21293 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21294 if (!suppress_add)
21295 {
21296 if (attr2 && (DW_UNSND (attr2) != 0))
21297 list_to_add = cu->get_builder ()->get_global_symbols ();
21298 else
21299 list_to_add = cu->list_in_scope;
21300 }
21301 break;
21302 }
21303 attr = dwarf2_attr (die, DW_AT_location, cu);
21304 if (attr != nullptr)
21305 {
21306 var_decode_location (attr, sym, cu);
21307 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21308
21309 /* Fortran explicitly imports any global symbols to the local
21310 scope by DW_TAG_common_block. */
21311 if (cu->language == language_fortran && die->parent
21312 && die->parent->tag == DW_TAG_common_block)
21313 attr2 = NULL;
21314
21315 if (SYMBOL_CLASS (sym) == LOC_STATIC
21316 && SYMBOL_VALUE_ADDRESS (sym) == 0
21317 && !dwarf2_per_objfile->has_section_at_zero)
21318 {
21319 /* When a static variable is eliminated by the linker,
21320 the corresponding debug information is not stripped
21321 out, but the variable address is set to null;
21322 do not add such variables into symbol table. */
21323 }
21324 else if (attr2 && (DW_UNSND (attr2) != 0))
21325 {
21326 if (SYMBOL_CLASS (sym) == LOC_STATIC
21327 && (objfile->flags & OBJF_MAINLINE) == 0
21328 && dwarf2_per_objfile->can_copy)
21329 {
21330 /* A global static variable might be subject to
21331 copy relocation. We first check for a local
21332 minsym, though, because maybe the symbol was
21333 marked hidden, in which case this would not
21334 apply. */
21335 bound_minimal_symbol found
21336 = (lookup_minimal_symbol_linkage
21337 (sym->linkage_name (), objfile));
21338 if (found.minsym != nullptr)
21339 sym->maybe_copied = 1;
21340 }
21341
21342 /* A variable with DW_AT_external is never static,
21343 but it may be block-scoped. */
21344 list_to_add
21345 = ((cu->list_in_scope
21346 == cu->get_builder ()->get_file_symbols ())
21347 ? cu->get_builder ()->get_global_symbols ()
21348 : cu->list_in_scope);
21349 }
21350 else
21351 list_to_add = cu->list_in_scope;
21352 }
21353 else
21354 {
21355 /* We do not know the address of this symbol.
21356 If it is an external symbol and we have type information
21357 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21358 The address of the variable will then be determined from
21359 the minimal symbol table whenever the variable is
21360 referenced. */
21361 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21362
21363 /* Fortran explicitly imports any global symbols to the local
21364 scope by DW_TAG_common_block. */
21365 if (cu->language == language_fortran && die->parent
21366 && die->parent->tag == DW_TAG_common_block)
21367 {
21368 /* SYMBOL_CLASS doesn't matter here because
21369 read_common_block is going to reset it. */
21370 if (!suppress_add)
21371 list_to_add = cu->list_in_scope;
21372 }
21373 else if (attr2 && (DW_UNSND (attr2) != 0)
21374 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21375 {
21376 /* A variable with DW_AT_external is never static, but it
21377 may be block-scoped. */
21378 list_to_add
21379 = ((cu->list_in_scope
21380 == cu->get_builder ()->get_file_symbols ())
21381 ? cu->get_builder ()->get_global_symbols ()
21382 : cu->list_in_scope);
21383
21384 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21385 }
21386 else if (!die_is_declaration (die, cu))
21387 {
21388 /* Use the default LOC_OPTIMIZED_OUT class. */
21389 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21390 if (!suppress_add)
21391 list_to_add = cu->list_in_scope;
21392 }
21393 }
21394 break;
21395 case DW_TAG_formal_parameter:
21396 {
21397 /* If we are inside a function, mark this as an argument. If
21398 not, we might be looking at an argument to an inlined function
21399 when we do not have enough information to show inlined frames;
21400 pretend it's a local variable in that case so that the user can
21401 still see it. */
21402 struct context_stack *curr
21403 = cu->get_builder ()->get_current_context_stack ();
21404 if (curr != nullptr && curr->name != nullptr)
21405 SYMBOL_IS_ARGUMENT (sym) = 1;
21406 attr = dwarf2_attr (die, DW_AT_location, cu);
21407 if (attr != nullptr)
21408 {
21409 var_decode_location (attr, sym, cu);
21410 }
21411 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21412 if (attr != nullptr)
21413 {
21414 dwarf2_const_value (attr, sym, cu);
21415 }
21416
21417 list_to_add = cu->list_in_scope;
21418 }
21419 break;
21420 case DW_TAG_unspecified_parameters:
21421 /* From varargs functions; gdb doesn't seem to have any
21422 interest in this information, so just ignore it for now.
21423 (FIXME?) */
21424 break;
21425 case DW_TAG_template_type_param:
21426 suppress_add = 1;
21427 /* Fall through. */
21428 case DW_TAG_class_type:
21429 case DW_TAG_interface_type:
21430 case DW_TAG_structure_type:
21431 case DW_TAG_union_type:
21432 case DW_TAG_set_type:
21433 case DW_TAG_enumeration_type:
21434 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21435 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21436
21437 {
21438 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21439 really ever be static objects: otherwise, if you try
21440 to, say, break of a class's method and you're in a file
21441 which doesn't mention that class, it won't work unless
21442 the check for all static symbols in lookup_symbol_aux
21443 saves you. See the OtherFileClass tests in
21444 gdb.c++/namespace.exp. */
21445
21446 if (!suppress_add)
21447 {
21448 buildsym_compunit *builder = cu->get_builder ();
21449 list_to_add
21450 = (cu->list_in_scope == builder->get_file_symbols ()
21451 && cu->language == language_cplus
21452 ? builder->get_global_symbols ()
21453 : cu->list_in_scope);
21454
21455 /* The semantics of C++ state that "struct foo {
21456 ... }" also defines a typedef for "foo". */
21457 if (cu->language == language_cplus
21458 || cu->language == language_ada
21459 || cu->language == language_d
21460 || cu->language == language_rust)
21461 {
21462 /* The symbol's name is already allocated along
21463 with this objfile, so we don't need to
21464 duplicate it for the type. */
21465 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21466 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21467 }
21468 }
21469 }
21470 break;
21471 case DW_TAG_typedef:
21472 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21473 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21474 list_to_add = cu->list_in_scope;
21475 break;
21476 case DW_TAG_base_type:
21477 case DW_TAG_subrange_type:
21478 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21479 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21480 list_to_add = cu->list_in_scope;
21481 break;
21482 case DW_TAG_enumerator:
21483 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21484 if (attr != nullptr)
21485 {
21486 dwarf2_const_value (attr, sym, cu);
21487 }
21488 {
21489 /* NOTE: carlton/2003-11-10: See comment above in the
21490 DW_TAG_class_type, etc. block. */
21491
21492 list_to_add
21493 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21494 && cu->language == language_cplus
21495 ? cu->get_builder ()->get_global_symbols ()
21496 : cu->list_in_scope);
21497 }
21498 break;
21499 case DW_TAG_imported_declaration:
21500 case DW_TAG_namespace:
21501 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21502 list_to_add = cu->get_builder ()->get_global_symbols ();
21503 break;
21504 case DW_TAG_module:
21505 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21506 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21507 list_to_add = cu->get_builder ()->get_global_symbols ();
21508 break;
21509 case DW_TAG_common_block:
21510 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21511 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21512 add_symbol_to_list (sym, cu->list_in_scope);
21513 break;
21514 default:
21515 /* Not a tag we recognize. Hopefully we aren't processing
21516 trash data, but since we must specifically ignore things
21517 we don't recognize, there is nothing else we should do at
21518 this point. */
21519 complaint (_("unsupported tag: '%s'"),
21520 dwarf_tag_name (die->tag));
21521 break;
21522 }
21523
21524 if (suppress_add)
21525 {
21526 sym->hash_next = objfile->template_symbols;
21527 objfile->template_symbols = sym;
21528 list_to_add = NULL;
21529 }
21530
21531 if (list_to_add != NULL)
21532 add_symbol_to_list (sym, list_to_add);
21533
21534 /* For the benefit of old versions of GCC, check for anonymous
21535 namespaces based on the demangled name. */
21536 if (!cu->processing_has_namespace_info
21537 && cu->language == language_cplus)
21538 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21539 }
21540 return (sym);
21541 }
21542
21543 /* Given an attr with a DW_FORM_dataN value in host byte order,
21544 zero-extend it as appropriate for the symbol's type. The DWARF
21545 standard (v4) is not entirely clear about the meaning of using
21546 DW_FORM_dataN for a constant with a signed type, where the type is
21547 wider than the data. The conclusion of a discussion on the DWARF
21548 list was that this is unspecified. We choose to always zero-extend
21549 because that is the interpretation long in use by GCC. */
21550
21551 static gdb_byte *
21552 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21553 struct dwarf2_cu *cu, LONGEST *value, int bits)
21554 {
21555 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21556 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21557 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21558 LONGEST l = DW_UNSND (attr);
21559
21560 if (bits < sizeof (*value) * 8)
21561 {
21562 l &= ((LONGEST) 1 << bits) - 1;
21563 *value = l;
21564 }
21565 else if (bits == sizeof (*value) * 8)
21566 *value = l;
21567 else
21568 {
21569 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21570 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21571 return bytes;
21572 }
21573
21574 return NULL;
21575 }
21576
21577 /* Read a constant value from an attribute. Either set *VALUE, or if
21578 the value does not fit in *VALUE, set *BYTES - either already
21579 allocated on the objfile obstack, or newly allocated on OBSTACK,
21580 or, set *BATON, if we translated the constant to a location
21581 expression. */
21582
21583 static void
21584 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21585 const char *name, struct obstack *obstack,
21586 struct dwarf2_cu *cu,
21587 LONGEST *value, const gdb_byte **bytes,
21588 struct dwarf2_locexpr_baton **baton)
21589 {
21590 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21591 struct comp_unit_head *cu_header = &cu->header;
21592 struct dwarf_block *blk;
21593 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21594 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21595
21596 *value = 0;
21597 *bytes = NULL;
21598 *baton = NULL;
21599
21600 switch (attr->form)
21601 {
21602 case DW_FORM_addr:
21603 case DW_FORM_addrx:
21604 case DW_FORM_GNU_addr_index:
21605 {
21606 gdb_byte *data;
21607
21608 if (TYPE_LENGTH (type) != cu_header->addr_size)
21609 dwarf2_const_value_length_mismatch_complaint (name,
21610 cu_header->addr_size,
21611 TYPE_LENGTH (type));
21612 /* Symbols of this form are reasonably rare, so we just
21613 piggyback on the existing location code rather than writing
21614 a new implementation of symbol_computed_ops. */
21615 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21616 (*baton)->per_cu = cu->per_cu;
21617 gdb_assert ((*baton)->per_cu);
21618
21619 (*baton)->size = 2 + cu_header->addr_size;
21620 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21621 (*baton)->data = data;
21622
21623 data[0] = DW_OP_addr;
21624 store_unsigned_integer (&data[1], cu_header->addr_size,
21625 byte_order, DW_ADDR (attr));
21626 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21627 }
21628 break;
21629 case DW_FORM_string:
21630 case DW_FORM_strp:
21631 case DW_FORM_strx:
21632 case DW_FORM_GNU_str_index:
21633 case DW_FORM_GNU_strp_alt:
21634 /* DW_STRING is already allocated on the objfile obstack, point
21635 directly to it. */
21636 *bytes = (const gdb_byte *) DW_STRING (attr);
21637 break;
21638 case DW_FORM_block1:
21639 case DW_FORM_block2:
21640 case DW_FORM_block4:
21641 case DW_FORM_block:
21642 case DW_FORM_exprloc:
21643 case DW_FORM_data16:
21644 blk = DW_BLOCK (attr);
21645 if (TYPE_LENGTH (type) != blk->size)
21646 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21647 TYPE_LENGTH (type));
21648 *bytes = blk->data;
21649 break;
21650
21651 /* The DW_AT_const_value attributes are supposed to carry the
21652 symbol's value "represented as it would be on the target
21653 architecture." By the time we get here, it's already been
21654 converted to host endianness, so we just need to sign- or
21655 zero-extend it as appropriate. */
21656 case DW_FORM_data1:
21657 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21658 break;
21659 case DW_FORM_data2:
21660 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21661 break;
21662 case DW_FORM_data4:
21663 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21664 break;
21665 case DW_FORM_data8:
21666 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21667 break;
21668
21669 case DW_FORM_sdata:
21670 case DW_FORM_implicit_const:
21671 *value = DW_SND (attr);
21672 break;
21673
21674 case DW_FORM_udata:
21675 *value = DW_UNSND (attr);
21676 break;
21677
21678 default:
21679 complaint (_("unsupported const value attribute form: '%s'"),
21680 dwarf_form_name (attr->form));
21681 *value = 0;
21682 break;
21683 }
21684 }
21685
21686
21687 /* Copy constant value from an attribute to a symbol. */
21688
21689 static void
21690 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21691 struct dwarf2_cu *cu)
21692 {
21693 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21694 LONGEST value;
21695 const gdb_byte *bytes;
21696 struct dwarf2_locexpr_baton *baton;
21697
21698 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21699 sym->print_name (),
21700 &objfile->objfile_obstack, cu,
21701 &value, &bytes, &baton);
21702
21703 if (baton != NULL)
21704 {
21705 SYMBOL_LOCATION_BATON (sym) = baton;
21706 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21707 }
21708 else if (bytes != NULL)
21709 {
21710 SYMBOL_VALUE_BYTES (sym) = bytes;
21711 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21712 }
21713 else
21714 {
21715 SYMBOL_VALUE (sym) = value;
21716 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21717 }
21718 }
21719
21720 /* Return the type of the die in question using its DW_AT_type attribute. */
21721
21722 static struct type *
21723 die_type (struct die_info *die, struct dwarf2_cu *cu)
21724 {
21725 struct attribute *type_attr;
21726
21727 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21728 if (!type_attr)
21729 {
21730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21731 /* A missing DW_AT_type represents a void type. */
21732 return objfile_type (objfile)->builtin_void;
21733 }
21734
21735 return lookup_die_type (die, type_attr, cu);
21736 }
21737
21738 /* True iff CU's producer generates GNAT Ada auxiliary information
21739 that allows to find parallel types through that information instead
21740 of having to do expensive parallel lookups by type name. */
21741
21742 static int
21743 need_gnat_info (struct dwarf2_cu *cu)
21744 {
21745 /* Assume that the Ada compiler was GNAT, which always produces
21746 the auxiliary information. */
21747 return (cu->language == language_ada);
21748 }
21749
21750 /* Return the auxiliary type of the die in question using its
21751 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21752 attribute is not present. */
21753
21754 static struct type *
21755 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21756 {
21757 struct attribute *type_attr;
21758
21759 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21760 if (!type_attr)
21761 return NULL;
21762
21763 return lookup_die_type (die, type_attr, cu);
21764 }
21765
21766 /* If DIE has a descriptive_type attribute, then set the TYPE's
21767 descriptive type accordingly. */
21768
21769 static void
21770 set_descriptive_type (struct type *type, struct die_info *die,
21771 struct dwarf2_cu *cu)
21772 {
21773 struct type *descriptive_type = die_descriptive_type (die, cu);
21774
21775 if (descriptive_type)
21776 {
21777 ALLOCATE_GNAT_AUX_TYPE (type);
21778 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21779 }
21780 }
21781
21782 /* Return the containing type of the die in question using its
21783 DW_AT_containing_type attribute. */
21784
21785 static struct type *
21786 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21787 {
21788 struct attribute *type_attr;
21789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21790
21791 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21792 if (!type_attr)
21793 error (_("Dwarf Error: Problem turning containing type into gdb type "
21794 "[in module %s]"), objfile_name (objfile));
21795
21796 return lookup_die_type (die, type_attr, cu);
21797 }
21798
21799 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21800
21801 static struct type *
21802 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21803 {
21804 struct dwarf2_per_objfile *dwarf2_per_objfile
21805 = cu->per_cu->dwarf2_per_objfile;
21806 struct objfile *objfile = dwarf2_per_objfile->objfile;
21807 char *saved;
21808
21809 std::string message
21810 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21811 objfile_name (objfile),
21812 sect_offset_str (cu->header.sect_off),
21813 sect_offset_str (die->sect_off));
21814 saved = obstack_strdup (&objfile->objfile_obstack, message);
21815
21816 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21817 }
21818
21819 /* Look up the type of DIE in CU using its type attribute ATTR.
21820 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21821 DW_AT_containing_type.
21822 If there is no type substitute an error marker. */
21823
21824 static struct type *
21825 lookup_die_type (struct die_info *die, const struct attribute *attr,
21826 struct dwarf2_cu *cu)
21827 {
21828 struct dwarf2_per_objfile *dwarf2_per_objfile
21829 = cu->per_cu->dwarf2_per_objfile;
21830 struct objfile *objfile = dwarf2_per_objfile->objfile;
21831 struct type *this_type;
21832
21833 gdb_assert (attr->name == DW_AT_type
21834 || attr->name == DW_AT_GNAT_descriptive_type
21835 || attr->name == DW_AT_containing_type);
21836
21837 /* First see if we have it cached. */
21838
21839 if (attr->form == DW_FORM_GNU_ref_alt)
21840 {
21841 struct dwarf2_per_cu_data *per_cu;
21842 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21843
21844 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21845 dwarf2_per_objfile);
21846 this_type = get_die_type_at_offset (sect_off, per_cu);
21847 }
21848 else if (attr->form_is_ref ())
21849 {
21850 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21851
21852 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21853 }
21854 else if (attr->form == DW_FORM_ref_sig8)
21855 {
21856 ULONGEST signature = DW_SIGNATURE (attr);
21857
21858 return get_signatured_type (die, signature, cu);
21859 }
21860 else
21861 {
21862 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21863 " at %s [in module %s]"),
21864 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21865 objfile_name (objfile));
21866 return build_error_marker_type (cu, die);
21867 }
21868
21869 /* If not cached we need to read it in. */
21870
21871 if (this_type == NULL)
21872 {
21873 struct die_info *type_die = NULL;
21874 struct dwarf2_cu *type_cu = cu;
21875
21876 if (attr->form_is_ref ())
21877 type_die = follow_die_ref (die, attr, &type_cu);
21878 if (type_die == NULL)
21879 return build_error_marker_type (cu, die);
21880 /* If we find the type now, it's probably because the type came
21881 from an inter-CU reference and the type's CU got expanded before
21882 ours. */
21883 this_type = read_type_die (type_die, type_cu);
21884 }
21885
21886 /* If we still don't have a type use an error marker. */
21887
21888 if (this_type == NULL)
21889 return build_error_marker_type (cu, die);
21890
21891 return this_type;
21892 }
21893
21894 /* Return the type in DIE, CU.
21895 Returns NULL for invalid types.
21896
21897 This first does a lookup in die_type_hash,
21898 and only reads the die in if necessary.
21899
21900 NOTE: This can be called when reading in partial or full symbols. */
21901
21902 static struct type *
21903 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21904 {
21905 struct type *this_type;
21906
21907 this_type = get_die_type (die, cu);
21908 if (this_type)
21909 return this_type;
21910
21911 return read_type_die_1 (die, cu);
21912 }
21913
21914 /* Read the type in DIE, CU.
21915 Returns NULL for invalid types. */
21916
21917 static struct type *
21918 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21919 {
21920 struct type *this_type = NULL;
21921
21922 switch (die->tag)
21923 {
21924 case DW_TAG_class_type:
21925 case DW_TAG_interface_type:
21926 case DW_TAG_structure_type:
21927 case DW_TAG_union_type:
21928 this_type = read_structure_type (die, cu);
21929 break;
21930 case DW_TAG_enumeration_type:
21931 this_type = read_enumeration_type (die, cu);
21932 break;
21933 case DW_TAG_subprogram:
21934 case DW_TAG_subroutine_type:
21935 case DW_TAG_inlined_subroutine:
21936 this_type = read_subroutine_type (die, cu);
21937 break;
21938 case DW_TAG_array_type:
21939 this_type = read_array_type (die, cu);
21940 break;
21941 case DW_TAG_set_type:
21942 this_type = read_set_type (die, cu);
21943 break;
21944 case DW_TAG_pointer_type:
21945 this_type = read_tag_pointer_type (die, cu);
21946 break;
21947 case DW_TAG_ptr_to_member_type:
21948 this_type = read_tag_ptr_to_member_type (die, cu);
21949 break;
21950 case DW_TAG_reference_type:
21951 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21952 break;
21953 case DW_TAG_rvalue_reference_type:
21954 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21955 break;
21956 case DW_TAG_const_type:
21957 this_type = read_tag_const_type (die, cu);
21958 break;
21959 case DW_TAG_volatile_type:
21960 this_type = read_tag_volatile_type (die, cu);
21961 break;
21962 case DW_TAG_restrict_type:
21963 this_type = read_tag_restrict_type (die, cu);
21964 break;
21965 case DW_TAG_string_type:
21966 this_type = read_tag_string_type (die, cu);
21967 break;
21968 case DW_TAG_typedef:
21969 this_type = read_typedef (die, cu);
21970 break;
21971 case DW_TAG_subrange_type:
21972 this_type = read_subrange_type (die, cu);
21973 break;
21974 case DW_TAG_base_type:
21975 this_type = read_base_type (die, cu);
21976 break;
21977 case DW_TAG_unspecified_type:
21978 this_type = read_unspecified_type (die, cu);
21979 break;
21980 case DW_TAG_namespace:
21981 this_type = read_namespace_type (die, cu);
21982 break;
21983 case DW_TAG_module:
21984 this_type = read_module_type (die, cu);
21985 break;
21986 case DW_TAG_atomic_type:
21987 this_type = read_tag_atomic_type (die, cu);
21988 break;
21989 default:
21990 complaint (_("unexpected tag in read_type_die: '%s'"),
21991 dwarf_tag_name (die->tag));
21992 break;
21993 }
21994
21995 return this_type;
21996 }
21997
21998 /* See if we can figure out if the class lives in a namespace. We do
21999 this by looking for a member function; its demangled name will
22000 contain namespace info, if there is any.
22001 Return the computed name or NULL.
22002 Space for the result is allocated on the objfile's obstack.
22003 This is the full-die version of guess_partial_die_structure_name.
22004 In this case we know DIE has no useful parent. */
22005
22006 static const char *
22007 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22008 {
22009 struct die_info *spec_die;
22010 struct dwarf2_cu *spec_cu;
22011 struct die_info *child;
22012 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22013
22014 spec_cu = cu;
22015 spec_die = die_specification (die, &spec_cu);
22016 if (spec_die != NULL)
22017 {
22018 die = spec_die;
22019 cu = spec_cu;
22020 }
22021
22022 for (child = die->child;
22023 child != NULL;
22024 child = child->sibling)
22025 {
22026 if (child->tag == DW_TAG_subprogram)
22027 {
22028 const char *linkage_name = dw2_linkage_name (child, cu);
22029
22030 if (linkage_name != NULL)
22031 {
22032 gdb::unique_xmalloc_ptr<char> actual_name
22033 (language_class_name_from_physname (cu->language_defn,
22034 linkage_name));
22035 const char *name = NULL;
22036
22037 if (actual_name != NULL)
22038 {
22039 const char *die_name = dwarf2_name (die, cu);
22040
22041 if (die_name != NULL
22042 && strcmp (die_name, actual_name.get ()) != 0)
22043 {
22044 /* Strip off the class name from the full name.
22045 We want the prefix. */
22046 int die_name_len = strlen (die_name);
22047 int actual_name_len = strlen (actual_name.get ());
22048 const char *ptr = actual_name.get ();
22049
22050 /* Test for '::' as a sanity check. */
22051 if (actual_name_len > die_name_len + 2
22052 && ptr[actual_name_len - die_name_len - 1] == ':')
22053 name = obstack_strndup (
22054 &objfile->per_bfd->storage_obstack,
22055 ptr, actual_name_len - die_name_len - 2);
22056 }
22057 }
22058 return name;
22059 }
22060 }
22061 }
22062
22063 return NULL;
22064 }
22065
22066 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22067 prefix part in such case. See
22068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22069
22070 static const char *
22071 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22072 {
22073 struct attribute *attr;
22074 const char *base;
22075
22076 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22077 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22078 return NULL;
22079
22080 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22081 return NULL;
22082
22083 attr = dw2_linkage_name_attr (die, cu);
22084 if (attr == NULL || DW_STRING (attr) == NULL)
22085 return NULL;
22086
22087 /* dwarf2_name had to be already called. */
22088 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22089
22090 /* Strip the base name, keep any leading namespaces/classes. */
22091 base = strrchr (DW_STRING (attr), ':');
22092 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22093 return "";
22094
22095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22096 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22097 DW_STRING (attr),
22098 &base[-1] - DW_STRING (attr));
22099 }
22100
22101 /* Return the name of the namespace/class that DIE is defined within,
22102 or "" if we can't tell. The caller should not xfree the result.
22103
22104 For example, if we're within the method foo() in the following
22105 code:
22106
22107 namespace N {
22108 class C {
22109 void foo () {
22110 }
22111 };
22112 }
22113
22114 then determine_prefix on foo's die will return "N::C". */
22115
22116 static const char *
22117 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22118 {
22119 struct dwarf2_per_objfile *dwarf2_per_objfile
22120 = cu->per_cu->dwarf2_per_objfile;
22121 struct die_info *parent, *spec_die;
22122 struct dwarf2_cu *spec_cu;
22123 struct type *parent_type;
22124 const char *retval;
22125
22126 if (cu->language != language_cplus
22127 && cu->language != language_fortran && cu->language != language_d
22128 && cu->language != language_rust)
22129 return "";
22130
22131 retval = anonymous_struct_prefix (die, cu);
22132 if (retval)
22133 return retval;
22134
22135 /* We have to be careful in the presence of DW_AT_specification.
22136 For example, with GCC 3.4, given the code
22137
22138 namespace N {
22139 void foo() {
22140 // Definition of N::foo.
22141 }
22142 }
22143
22144 then we'll have a tree of DIEs like this:
22145
22146 1: DW_TAG_compile_unit
22147 2: DW_TAG_namespace // N
22148 3: DW_TAG_subprogram // declaration of N::foo
22149 4: DW_TAG_subprogram // definition of N::foo
22150 DW_AT_specification // refers to die #3
22151
22152 Thus, when processing die #4, we have to pretend that we're in
22153 the context of its DW_AT_specification, namely the contex of die
22154 #3. */
22155 spec_cu = cu;
22156 spec_die = die_specification (die, &spec_cu);
22157 if (spec_die == NULL)
22158 parent = die->parent;
22159 else
22160 {
22161 parent = spec_die->parent;
22162 cu = spec_cu;
22163 }
22164
22165 if (parent == NULL)
22166 return "";
22167 else if (parent->building_fullname)
22168 {
22169 const char *name;
22170 const char *parent_name;
22171
22172 /* It has been seen on RealView 2.2 built binaries,
22173 DW_TAG_template_type_param types actually _defined_ as
22174 children of the parent class:
22175
22176 enum E {};
22177 template class <class Enum> Class{};
22178 Class<enum E> class_e;
22179
22180 1: DW_TAG_class_type (Class)
22181 2: DW_TAG_enumeration_type (E)
22182 3: DW_TAG_enumerator (enum1:0)
22183 3: DW_TAG_enumerator (enum2:1)
22184 ...
22185 2: DW_TAG_template_type_param
22186 DW_AT_type DW_FORM_ref_udata (E)
22187
22188 Besides being broken debug info, it can put GDB into an
22189 infinite loop. Consider:
22190
22191 When we're building the full name for Class<E>, we'll start
22192 at Class, and go look over its template type parameters,
22193 finding E. We'll then try to build the full name of E, and
22194 reach here. We're now trying to build the full name of E,
22195 and look over the parent DIE for containing scope. In the
22196 broken case, if we followed the parent DIE of E, we'd again
22197 find Class, and once again go look at its template type
22198 arguments, etc., etc. Simply don't consider such parent die
22199 as source-level parent of this die (it can't be, the language
22200 doesn't allow it), and break the loop here. */
22201 name = dwarf2_name (die, cu);
22202 parent_name = dwarf2_name (parent, cu);
22203 complaint (_("template param type '%s' defined within parent '%s'"),
22204 name ? name : "<unknown>",
22205 parent_name ? parent_name : "<unknown>");
22206 return "";
22207 }
22208 else
22209 switch (parent->tag)
22210 {
22211 case DW_TAG_namespace:
22212 parent_type = read_type_die (parent, cu);
22213 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22214 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22215 Work around this problem here. */
22216 if (cu->language == language_cplus
22217 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22218 return "";
22219 /* We give a name to even anonymous namespaces. */
22220 return TYPE_NAME (parent_type);
22221 case DW_TAG_class_type:
22222 case DW_TAG_interface_type:
22223 case DW_TAG_structure_type:
22224 case DW_TAG_union_type:
22225 case DW_TAG_module:
22226 parent_type = read_type_die (parent, cu);
22227 if (TYPE_NAME (parent_type) != NULL)
22228 return TYPE_NAME (parent_type);
22229 else
22230 /* An anonymous structure is only allowed non-static data
22231 members; no typedefs, no member functions, et cetera.
22232 So it does not need a prefix. */
22233 return "";
22234 case DW_TAG_compile_unit:
22235 case DW_TAG_partial_unit:
22236 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22237 if (cu->language == language_cplus
22238 && !dwarf2_per_objfile->types.empty ()
22239 && die->child != NULL
22240 && (die->tag == DW_TAG_class_type
22241 || die->tag == DW_TAG_structure_type
22242 || die->tag == DW_TAG_union_type))
22243 {
22244 const char *name = guess_full_die_structure_name (die, cu);
22245 if (name != NULL)
22246 return name;
22247 }
22248 return "";
22249 case DW_TAG_subprogram:
22250 /* Nested subroutines in Fortran get a prefix with the name
22251 of the parent's subroutine. */
22252 if (cu->language == language_fortran)
22253 {
22254 if ((die->tag == DW_TAG_subprogram)
22255 && (dwarf2_name (parent, cu) != NULL))
22256 return dwarf2_name (parent, cu);
22257 }
22258 return determine_prefix (parent, cu);
22259 case DW_TAG_enumeration_type:
22260 parent_type = read_type_die (parent, cu);
22261 if (TYPE_DECLARED_CLASS (parent_type))
22262 {
22263 if (TYPE_NAME (parent_type) != NULL)
22264 return TYPE_NAME (parent_type);
22265 return "";
22266 }
22267 /* Fall through. */
22268 default:
22269 return determine_prefix (parent, cu);
22270 }
22271 }
22272
22273 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22274 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22275 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22276 an obconcat, otherwise allocate storage for the result. The CU argument is
22277 used to determine the language and hence, the appropriate separator. */
22278
22279 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22280
22281 static char *
22282 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22283 int physname, struct dwarf2_cu *cu)
22284 {
22285 const char *lead = "";
22286 const char *sep;
22287
22288 if (suffix == NULL || suffix[0] == '\0'
22289 || prefix == NULL || prefix[0] == '\0')
22290 sep = "";
22291 else if (cu->language == language_d)
22292 {
22293 /* For D, the 'main' function could be defined in any module, but it
22294 should never be prefixed. */
22295 if (strcmp (suffix, "D main") == 0)
22296 {
22297 prefix = "";
22298 sep = "";
22299 }
22300 else
22301 sep = ".";
22302 }
22303 else if (cu->language == language_fortran && physname)
22304 {
22305 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22306 DW_AT_MIPS_linkage_name is preferred and used instead. */
22307
22308 lead = "__";
22309 sep = "_MOD_";
22310 }
22311 else
22312 sep = "::";
22313
22314 if (prefix == NULL)
22315 prefix = "";
22316 if (suffix == NULL)
22317 suffix = "";
22318
22319 if (obs == NULL)
22320 {
22321 char *retval
22322 = ((char *)
22323 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22324
22325 strcpy (retval, lead);
22326 strcat (retval, prefix);
22327 strcat (retval, sep);
22328 strcat (retval, suffix);
22329 return retval;
22330 }
22331 else
22332 {
22333 /* We have an obstack. */
22334 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22335 }
22336 }
22337
22338 /* Return sibling of die, NULL if no sibling. */
22339
22340 static struct die_info *
22341 sibling_die (struct die_info *die)
22342 {
22343 return die->sibling;
22344 }
22345
22346 /* Get name of a die, return NULL if not found. */
22347
22348 static const char *
22349 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22350 struct obstack *obstack)
22351 {
22352 if (name && cu->language == language_cplus)
22353 {
22354 std::string canon_name = cp_canonicalize_string (name);
22355
22356 if (!canon_name.empty ())
22357 {
22358 if (canon_name != name)
22359 name = obstack_strdup (obstack, canon_name);
22360 }
22361 }
22362
22363 return name;
22364 }
22365
22366 /* Get name of a die, return NULL if not found.
22367 Anonymous namespaces are converted to their magic string. */
22368
22369 static const char *
22370 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22371 {
22372 struct attribute *attr;
22373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22374
22375 attr = dwarf2_attr (die, DW_AT_name, cu);
22376 if ((!attr || !DW_STRING (attr))
22377 && die->tag != DW_TAG_namespace
22378 && die->tag != DW_TAG_class_type
22379 && die->tag != DW_TAG_interface_type
22380 && die->tag != DW_TAG_structure_type
22381 && die->tag != DW_TAG_union_type)
22382 return NULL;
22383
22384 switch (die->tag)
22385 {
22386 case DW_TAG_compile_unit:
22387 case DW_TAG_partial_unit:
22388 /* Compilation units have a DW_AT_name that is a filename, not
22389 a source language identifier. */
22390 case DW_TAG_enumeration_type:
22391 case DW_TAG_enumerator:
22392 /* These tags always have simple identifiers already; no need
22393 to canonicalize them. */
22394 return DW_STRING (attr);
22395
22396 case DW_TAG_namespace:
22397 if (attr != NULL && DW_STRING (attr) != NULL)
22398 return DW_STRING (attr);
22399 return CP_ANONYMOUS_NAMESPACE_STR;
22400
22401 case DW_TAG_class_type:
22402 case DW_TAG_interface_type:
22403 case DW_TAG_structure_type:
22404 case DW_TAG_union_type:
22405 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22406 structures or unions. These were of the form "._%d" in GCC 4.1,
22407 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22408 and GCC 4.4. We work around this problem by ignoring these. */
22409 if (attr && DW_STRING (attr)
22410 && (startswith (DW_STRING (attr), "._")
22411 || startswith (DW_STRING (attr), "<anonymous")))
22412 return NULL;
22413
22414 /* GCC might emit a nameless typedef that has a linkage name. See
22415 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22416 if (!attr || DW_STRING (attr) == NULL)
22417 {
22418 attr = dw2_linkage_name_attr (die, cu);
22419 if (attr == NULL || DW_STRING (attr) == NULL)
22420 return NULL;
22421
22422 /* Avoid demangling DW_STRING (attr) the second time on a second
22423 call for the same DIE. */
22424 if (!DW_STRING_IS_CANONICAL (attr))
22425 {
22426 gdb::unique_xmalloc_ptr<char> demangled
22427 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22428
22429 const char *base;
22430
22431 /* FIXME: we already did this for the partial symbol... */
22432 DW_STRING (attr)
22433 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22434 demangled.get ());
22435 DW_STRING_IS_CANONICAL (attr) = 1;
22436
22437 /* Strip any leading namespaces/classes, keep only the base name.
22438 DW_AT_name for named DIEs does not contain the prefixes. */
22439 base = strrchr (DW_STRING (attr), ':');
22440 if (base && base > DW_STRING (attr) && base[-1] == ':')
22441 return &base[1];
22442 else
22443 return DW_STRING (attr);
22444 }
22445 }
22446 break;
22447
22448 default:
22449 break;
22450 }
22451
22452 if (!DW_STRING_IS_CANONICAL (attr))
22453 {
22454 DW_STRING (attr)
22455 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22456 &objfile->per_bfd->storage_obstack);
22457 DW_STRING_IS_CANONICAL (attr) = 1;
22458 }
22459 return DW_STRING (attr);
22460 }
22461
22462 /* Return the die that this die in an extension of, or NULL if there
22463 is none. *EXT_CU is the CU containing DIE on input, and the CU
22464 containing the return value on output. */
22465
22466 static struct die_info *
22467 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22468 {
22469 struct attribute *attr;
22470
22471 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22472 if (attr == NULL)
22473 return NULL;
22474
22475 return follow_die_ref (die, attr, ext_cu);
22476 }
22477
22478 /* A convenience function that returns an "unknown" DWARF name,
22479 including the value of V. STR is the name of the entity being
22480 printed, e.g., "TAG". */
22481
22482 static const char *
22483 dwarf_unknown (const char *str, unsigned v)
22484 {
22485 char *cell = get_print_cell ();
22486 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22487 return cell;
22488 }
22489
22490 /* Convert a DIE tag into its string name. */
22491
22492 static const char *
22493 dwarf_tag_name (unsigned tag)
22494 {
22495 const char *name = get_DW_TAG_name (tag);
22496
22497 if (name == NULL)
22498 return dwarf_unknown ("TAG", tag);
22499
22500 return name;
22501 }
22502
22503 /* Convert a DWARF attribute code into its string name. */
22504
22505 static const char *
22506 dwarf_attr_name (unsigned attr)
22507 {
22508 const char *name;
22509
22510 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22511 if (attr == DW_AT_MIPS_fde)
22512 return "DW_AT_MIPS_fde";
22513 #else
22514 if (attr == DW_AT_HP_block_index)
22515 return "DW_AT_HP_block_index";
22516 #endif
22517
22518 name = get_DW_AT_name (attr);
22519
22520 if (name == NULL)
22521 return dwarf_unknown ("AT", attr);
22522
22523 return name;
22524 }
22525
22526 /* Convert a unit type to corresponding DW_UT name. */
22527
22528 static const char *
22529 dwarf_unit_type_name (int unit_type) {
22530 switch (unit_type)
22531 {
22532 case 0x01:
22533 return "DW_UT_compile (0x01)";
22534 case 0x02:
22535 return "DW_UT_type (0x02)";
22536 case 0x03:
22537 return "DW_UT_partial (0x03)";
22538 case 0x04:
22539 return "DW_UT_skeleton (0x04)";
22540 case 0x05:
22541 return "DW_UT_split_compile (0x05)";
22542 case 0x06:
22543 return "DW_UT_split_type (0x06)";
22544 case 0x80:
22545 return "DW_UT_lo_user (0x80)";
22546 case 0xff:
22547 return "DW_UT_hi_user (0xff)";
22548 default:
22549 return nullptr;
22550 }
22551 }
22552
22553 /* Convert a DWARF value form code into its string name. */
22554
22555 static const char *
22556 dwarf_form_name (unsigned form)
22557 {
22558 const char *name = get_DW_FORM_name (form);
22559
22560 if (name == NULL)
22561 return dwarf_unknown ("FORM", form);
22562
22563 return name;
22564 }
22565
22566 static const char *
22567 dwarf_bool_name (unsigned mybool)
22568 {
22569 if (mybool)
22570 return "TRUE";
22571 else
22572 return "FALSE";
22573 }
22574
22575 /* Convert a DWARF type code into its string name. */
22576
22577 static const char *
22578 dwarf_type_encoding_name (unsigned enc)
22579 {
22580 const char *name = get_DW_ATE_name (enc);
22581
22582 if (name == NULL)
22583 return dwarf_unknown ("ATE", enc);
22584
22585 return name;
22586 }
22587
22588 static void
22589 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22590 {
22591 unsigned int i;
22592
22593 print_spaces (indent, f);
22594 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22595 dwarf_tag_name (die->tag), die->abbrev,
22596 sect_offset_str (die->sect_off));
22597
22598 if (die->parent != NULL)
22599 {
22600 print_spaces (indent, f);
22601 fprintf_unfiltered (f, " parent at offset: %s\n",
22602 sect_offset_str (die->parent->sect_off));
22603 }
22604
22605 print_spaces (indent, f);
22606 fprintf_unfiltered (f, " has children: %s\n",
22607 dwarf_bool_name (die->child != NULL));
22608
22609 print_spaces (indent, f);
22610 fprintf_unfiltered (f, " attributes:\n");
22611
22612 for (i = 0; i < die->num_attrs; ++i)
22613 {
22614 print_spaces (indent, f);
22615 fprintf_unfiltered (f, " %s (%s) ",
22616 dwarf_attr_name (die->attrs[i].name),
22617 dwarf_form_name (die->attrs[i].form));
22618
22619 switch (die->attrs[i].form)
22620 {
22621 case DW_FORM_addr:
22622 case DW_FORM_addrx:
22623 case DW_FORM_GNU_addr_index:
22624 fprintf_unfiltered (f, "address: ");
22625 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22626 break;
22627 case DW_FORM_block2:
22628 case DW_FORM_block4:
22629 case DW_FORM_block:
22630 case DW_FORM_block1:
22631 fprintf_unfiltered (f, "block: size %s",
22632 pulongest (DW_BLOCK (&die->attrs[i])->size));
22633 break;
22634 case DW_FORM_exprloc:
22635 fprintf_unfiltered (f, "expression: size %s",
22636 pulongest (DW_BLOCK (&die->attrs[i])->size));
22637 break;
22638 case DW_FORM_data16:
22639 fprintf_unfiltered (f, "constant of 16 bytes");
22640 break;
22641 case DW_FORM_ref_addr:
22642 fprintf_unfiltered (f, "ref address: ");
22643 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22644 break;
22645 case DW_FORM_GNU_ref_alt:
22646 fprintf_unfiltered (f, "alt ref address: ");
22647 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22648 break;
22649 case DW_FORM_ref1:
22650 case DW_FORM_ref2:
22651 case DW_FORM_ref4:
22652 case DW_FORM_ref8:
22653 case DW_FORM_ref_udata:
22654 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22655 (long) (DW_UNSND (&die->attrs[i])));
22656 break;
22657 case DW_FORM_data1:
22658 case DW_FORM_data2:
22659 case DW_FORM_data4:
22660 case DW_FORM_data8:
22661 case DW_FORM_udata:
22662 case DW_FORM_sdata:
22663 fprintf_unfiltered (f, "constant: %s",
22664 pulongest (DW_UNSND (&die->attrs[i])));
22665 break;
22666 case DW_FORM_sec_offset:
22667 fprintf_unfiltered (f, "section offset: %s",
22668 pulongest (DW_UNSND (&die->attrs[i])));
22669 break;
22670 case DW_FORM_ref_sig8:
22671 fprintf_unfiltered (f, "signature: %s",
22672 hex_string (DW_SIGNATURE (&die->attrs[i])));
22673 break;
22674 case DW_FORM_string:
22675 case DW_FORM_strp:
22676 case DW_FORM_line_strp:
22677 case DW_FORM_strx:
22678 case DW_FORM_GNU_str_index:
22679 case DW_FORM_GNU_strp_alt:
22680 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22681 DW_STRING (&die->attrs[i])
22682 ? DW_STRING (&die->attrs[i]) : "",
22683 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22684 break;
22685 case DW_FORM_flag:
22686 if (DW_UNSND (&die->attrs[i]))
22687 fprintf_unfiltered (f, "flag: TRUE");
22688 else
22689 fprintf_unfiltered (f, "flag: FALSE");
22690 break;
22691 case DW_FORM_flag_present:
22692 fprintf_unfiltered (f, "flag: TRUE");
22693 break;
22694 case DW_FORM_indirect:
22695 /* The reader will have reduced the indirect form to
22696 the "base form" so this form should not occur. */
22697 fprintf_unfiltered (f,
22698 "unexpected attribute form: DW_FORM_indirect");
22699 break;
22700 case DW_FORM_implicit_const:
22701 fprintf_unfiltered (f, "constant: %s",
22702 plongest (DW_SND (&die->attrs[i])));
22703 break;
22704 default:
22705 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22706 die->attrs[i].form);
22707 break;
22708 }
22709 fprintf_unfiltered (f, "\n");
22710 }
22711 }
22712
22713 static void
22714 dump_die_for_error (struct die_info *die)
22715 {
22716 dump_die_shallow (gdb_stderr, 0, die);
22717 }
22718
22719 static void
22720 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22721 {
22722 int indent = level * 4;
22723
22724 gdb_assert (die != NULL);
22725
22726 if (level >= max_level)
22727 return;
22728
22729 dump_die_shallow (f, indent, die);
22730
22731 if (die->child != NULL)
22732 {
22733 print_spaces (indent, f);
22734 fprintf_unfiltered (f, " Children:");
22735 if (level + 1 < max_level)
22736 {
22737 fprintf_unfiltered (f, "\n");
22738 dump_die_1 (f, level + 1, max_level, die->child);
22739 }
22740 else
22741 {
22742 fprintf_unfiltered (f,
22743 " [not printed, max nesting level reached]\n");
22744 }
22745 }
22746
22747 if (die->sibling != NULL && level > 0)
22748 {
22749 dump_die_1 (f, level, max_level, die->sibling);
22750 }
22751 }
22752
22753 /* This is called from the pdie macro in gdbinit.in.
22754 It's not static so gcc will keep a copy callable from gdb. */
22755
22756 void
22757 dump_die (struct die_info *die, int max_level)
22758 {
22759 dump_die_1 (gdb_stdlog, 0, max_level, die);
22760 }
22761
22762 static void
22763 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22764 {
22765 void **slot;
22766
22767 slot = htab_find_slot_with_hash (cu->die_hash, die,
22768 to_underlying (die->sect_off),
22769 INSERT);
22770
22771 *slot = die;
22772 }
22773
22774 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22775 required kind. */
22776
22777 static sect_offset
22778 dwarf2_get_ref_die_offset (const struct attribute *attr)
22779 {
22780 if (attr->form_is_ref ())
22781 return (sect_offset) DW_UNSND (attr);
22782
22783 complaint (_("unsupported die ref attribute form: '%s'"),
22784 dwarf_form_name (attr->form));
22785 return {};
22786 }
22787
22788 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22789 * the value held by the attribute is not constant. */
22790
22791 static LONGEST
22792 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22793 {
22794 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22795 return DW_SND (attr);
22796 else if (attr->form == DW_FORM_udata
22797 || attr->form == DW_FORM_data1
22798 || attr->form == DW_FORM_data2
22799 || attr->form == DW_FORM_data4
22800 || attr->form == DW_FORM_data8)
22801 return DW_UNSND (attr);
22802 else
22803 {
22804 /* For DW_FORM_data16 see attribute::form_is_constant. */
22805 complaint (_("Attribute value is not a constant (%s)"),
22806 dwarf_form_name (attr->form));
22807 return default_value;
22808 }
22809 }
22810
22811 /* Follow reference or signature attribute ATTR of SRC_DIE.
22812 On entry *REF_CU is the CU of SRC_DIE.
22813 On exit *REF_CU is the CU of the result. */
22814
22815 static struct die_info *
22816 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22817 struct dwarf2_cu **ref_cu)
22818 {
22819 struct die_info *die;
22820
22821 if (attr->form_is_ref ())
22822 die = follow_die_ref (src_die, attr, ref_cu);
22823 else if (attr->form == DW_FORM_ref_sig8)
22824 die = follow_die_sig (src_die, attr, ref_cu);
22825 else
22826 {
22827 dump_die_for_error (src_die);
22828 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22829 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22830 }
22831
22832 return die;
22833 }
22834
22835 /* Follow reference OFFSET.
22836 On entry *REF_CU is the CU of the source die referencing OFFSET.
22837 On exit *REF_CU is the CU of the result.
22838 Returns NULL if OFFSET is invalid. */
22839
22840 static struct die_info *
22841 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22842 struct dwarf2_cu **ref_cu)
22843 {
22844 struct die_info temp_die;
22845 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22846 struct dwarf2_per_objfile *dwarf2_per_objfile
22847 = cu->per_cu->dwarf2_per_objfile;
22848
22849 gdb_assert (cu->per_cu != NULL);
22850
22851 target_cu = cu;
22852
22853 if (cu->per_cu->is_debug_types)
22854 {
22855 /* .debug_types CUs cannot reference anything outside their CU.
22856 If they need to, they have to reference a signatured type via
22857 DW_FORM_ref_sig8. */
22858 if (!offset_in_cu_p (&cu->header, sect_off))
22859 return NULL;
22860 }
22861 else if (offset_in_dwz != cu->per_cu->is_dwz
22862 || !offset_in_cu_p (&cu->header, sect_off))
22863 {
22864 struct dwarf2_per_cu_data *per_cu;
22865
22866 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22867 dwarf2_per_objfile);
22868
22869 /* If necessary, add it to the queue and load its DIEs. */
22870 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22871 load_full_comp_unit (per_cu, false, cu->language);
22872
22873 target_cu = per_cu->cu;
22874 }
22875 else if (cu->dies == NULL)
22876 {
22877 /* We're loading full DIEs during partial symbol reading. */
22878 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22879 load_full_comp_unit (cu->per_cu, false, language_minimal);
22880 }
22881
22882 *ref_cu = target_cu;
22883 temp_die.sect_off = sect_off;
22884
22885 if (target_cu != cu)
22886 target_cu->ancestor = cu;
22887
22888 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22889 &temp_die,
22890 to_underlying (sect_off));
22891 }
22892
22893 /* Follow reference attribute ATTR of SRC_DIE.
22894 On entry *REF_CU is the CU of SRC_DIE.
22895 On exit *REF_CU is the CU of the result. */
22896
22897 static struct die_info *
22898 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22899 struct dwarf2_cu **ref_cu)
22900 {
22901 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22902 struct dwarf2_cu *cu = *ref_cu;
22903 struct die_info *die;
22904
22905 die = follow_die_offset (sect_off,
22906 (attr->form == DW_FORM_GNU_ref_alt
22907 || cu->per_cu->is_dwz),
22908 ref_cu);
22909 if (!die)
22910 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22911 "at %s [in module %s]"),
22912 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22913 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22914
22915 return die;
22916 }
22917
22918 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22919 Returned value is intended for DW_OP_call*. Returned
22920 dwarf2_locexpr_baton->data has lifetime of
22921 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22922
22923 struct dwarf2_locexpr_baton
22924 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22925 struct dwarf2_per_cu_data *per_cu,
22926 CORE_ADDR (*get_frame_pc) (void *baton),
22927 void *baton, bool resolve_abstract_p)
22928 {
22929 struct dwarf2_cu *cu;
22930 struct die_info *die;
22931 struct attribute *attr;
22932 struct dwarf2_locexpr_baton retval;
22933 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22934 struct objfile *objfile = dwarf2_per_objfile->objfile;
22935
22936 if (per_cu->cu == NULL)
22937 load_cu (per_cu, false);
22938 cu = per_cu->cu;
22939 if (cu == NULL)
22940 {
22941 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22942 Instead just throw an error, not much else we can do. */
22943 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22944 sect_offset_str (sect_off), objfile_name (objfile));
22945 }
22946
22947 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22948 if (!die)
22949 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22950 sect_offset_str (sect_off), objfile_name (objfile));
22951
22952 attr = dwarf2_attr (die, DW_AT_location, cu);
22953 if (!attr && resolve_abstract_p
22954 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22955 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22956 {
22957 CORE_ADDR pc = (*get_frame_pc) (baton);
22958 CORE_ADDR baseaddr = objfile->text_section_offset ();
22959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22960
22961 for (const auto &cand_off
22962 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22963 {
22964 struct dwarf2_cu *cand_cu = cu;
22965 struct die_info *cand
22966 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22967 if (!cand
22968 || !cand->parent
22969 || cand->parent->tag != DW_TAG_subprogram)
22970 continue;
22971
22972 CORE_ADDR pc_low, pc_high;
22973 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22974 if (pc_low == ((CORE_ADDR) -1))
22975 continue;
22976 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22977 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22978 if (!(pc_low <= pc && pc < pc_high))
22979 continue;
22980
22981 die = cand;
22982 attr = dwarf2_attr (die, DW_AT_location, cu);
22983 break;
22984 }
22985 }
22986
22987 if (!attr)
22988 {
22989 /* DWARF: "If there is no such attribute, then there is no effect.".
22990 DATA is ignored if SIZE is 0. */
22991
22992 retval.data = NULL;
22993 retval.size = 0;
22994 }
22995 else if (attr->form_is_section_offset ())
22996 {
22997 struct dwarf2_loclist_baton loclist_baton;
22998 CORE_ADDR pc = (*get_frame_pc) (baton);
22999 size_t size;
23000
23001 fill_in_loclist_baton (cu, &loclist_baton, attr);
23002
23003 retval.data = dwarf2_find_location_expression (&loclist_baton,
23004 &size, pc);
23005 retval.size = size;
23006 }
23007 else
23008 {
23009 if (!attr->form_is_block ())
23010 error (_("Dwarf Error: DIE at %s referenced in module %s "
23011 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23012 sect_offset_str (sect_off), objfile_name (objfile));
23013
23014 retval.data = DW_BLOCK (attr)->data;
23015 retval.size = DW_BLOCK (attr)->size;
23016 }
23017 retval.per_cu = cu->per_cu;
23018
23019 age_cached_comp_units (dwarf2_per_objfile);
23020
23021 return retval;
23022 }
23023
23024 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23025 offset. */
23026
23027 struct dwarf2_locexpr_baton
23028 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23029 struct dwarf2_per_cu_data *per_cu,
23030 CORE_ADDR (*get_frame_pc) (void *baton),
23031 void *baton)
23032 {
23033 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23034
23035 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23036 }
23037
23038 /* Write a constant of a given type as target-ordered bytes into
23039 OBSTACK. */
23040
23041 static const gdb_byte *
23042 write_constant_as_bytes (struct obstack *obstack,
23043 enum bfd_endian byte_order,
23044 struct type *type,
23045 ULONGEST value,
23046 LONGEST *len)
23047 {
23048 gdb_byte *result;
23049
23050 *len = TYPE_LENGTH (type);
23051 result = (gdb_byte *) obstack_alloc (obstack, *len);
23052 store_unsigned_integer (result, *len, byte_order, value);
23053
23054 return result;
23055 }
23056
23057 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23058 pointer to the constant bytes and set LEN to the length of the
23059 data. If memory is needed, allocate it on OBSTACK. If the DIE
23060 does not have a DW_AT_const_value, return NULL. */
23061
23062 const gdb_byte *
23063 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23064 struct dwarf2_per_cu_data *per_cu,
23065 struct obstack *obstack,
23066 LONGEST *len)
23067 {
23068 struct dwarf2_cu *cu;
23069 struct die_info *die;
23070 struct attribute *attr;
23071 const gdb_byte *result = NULL;
23072 struct type *type;
23073 LONGEST value;
23074 enum bfd_endian byte_order;
23075 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23076
23077 if (per_cu->cu == NULL)
23078 load_cu (per_cu, false);
23079 cu = per_cu->cu;
23080 if (cu == NULL)
23081 {
23082 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23083 Instead just throw an error, not much else we can do. */
23084 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23085 sect_offset_str (sect_off), objfile_name (objfile));
23086 }
23087
23088 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23089 if (!die)
23090 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23091 sect_offset_str (sect_off), objfile_name (objfile));
23092
23093 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23094 if (attr == NULL)
23095 return NULL;
23096
23097 byte_order = (bfd_big_endian (objfile->obfd)
23098 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23099
23100 switch (attr->form)
23101 {
23102 case DW_FORM_addr:
23103 case DW_FORM_addrx:
23104 case DW_FORM_GNU_addr_index:
23105 {
23106 gdb_byte *tem;
23107
23108 *len = cu->header.addr_size;
23109 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23110 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23111 result = tem;
23112 }
23113 break;
23114 case DW_FORM_string:
23115 case DW_FORM_strp:
23116 case DW_FORM_strx:
23117 case DW_FORM_GNU_str_index:
23118 case DW_FORM_GNU_strp_alt:
23119 /* DW_STRING is already allocated on the objfile obstack, point
23120 directly to it. */
23121 result = (const gdb_byte *) DW_STRING (attr);
23122 *len = strlen (DW_STRING (attr));
23123 break;
23124 case DW_FORM_block1:
23125 case DW_FORM_block2:
23126 case DW_FORM_block4:
23127 case DW_FORM_block:
23128 case DW_FORM_exprloc:
23129 case DW_FORM_data16:
23130 result = DW_BLOCK (attr)->data;
23131 *len = DW_BLOCK (attr)->size;
23132 break;
23133
23134 /* The DW_AT_const_value attributes are supposed to carry the
23135 symbol's value "represented as it would be on the target
23136 architecture." By the time we get here, it's already been
23137 converted to host endianness, so we just need to sign- or
23138 zero-extend it as appropriate. */
23139 case DW_FORM_data1:
23140 type = die_type (die, cu);
23141 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23142 if (result == NULL)
23143 result = write_constant_as_bytes (obstack, byte_order,
23144 type, value, len);
23145 break;
23146 case DW_FORM_data2:
23147 type = die_type (die, cu);
23148 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23149 if (result == NULL)
23150 result = write_constant_as_bytes (obstack, byte_order,
23151 type, value, len);
23152 break;
23153 case DW_FORM_data4:
23154 type = die_type (die, cu);
23155 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23156 if (result == NULL)
23157 result = write_constant_as_bytes (obstack, byte_order,
23158 type, value, len);
23159 break;
23160 case DW_FORM_data8:
23161 type = die_type (die, cu);
23162 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23163 if (result == NULL)
23164 result = write_constant_as_bytes (obstack, byte_order,
23165 type, value, len);
23166 break;
23167
23168 case DW_FORM_sdata:
23169 case DW_FORM_implicit_const:
23170 type = die_type (die, cu);
23171 result = write_constant_as_bytes (obstack, byte_order,
23172 type, DW_SND (attr), len);
23173 break;
23174
23175 case DW_FORM_udata:
23176 type = die_type (die, cu);
23177 result = write_constant_as_bytes (obstack, byte_order,
23178 type, DW_UNSND (attr), len);
23179 break;
23180
23181 default:
23182 complaint (_("unsupported const value attribute form: '%s'"),
23183 dwarf_form_name (attr->form));
23184 break;
23185 }
23186
23187 return result;
23188 }
23189
23190 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23191 valid type for this die is found. */
23192
23193 struct type *
23194 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23195 struct dwarf2_per_cu_data *per_cu)
23196 {
23197 struct dwarf2_cu *cu;
23198 struct die_info *die;
23199
23200 if (per_cu->cu == NULL)
23201 load_cu (per_cu, false);
23202 cu = per_cu->cu;
23203 if (!cu)
23204 return NULL;
23205
23206 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23207 if (!die)
23208 return NULL;
23209
23210 return die_type (die, cu);
23211 }
23212
23213 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23214 PER_CU. */
23215
23216 struct type *
23217 dwarf2_get_die_type (cu_offset die_offset,
23218 struct dwarf2_per_cu_data *per_cu)
23219 {
23220 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23221 return get_die_type_at_offset (die_offset_sect, per_cu);
23222 }
23223
23224 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23225 On entry *REF_CU is the CU of SRC_DIE.
23226 On exit *REF_CU is the CU of the result.
23227 Returns NULL if the referenced DIE isn't found. */
23228
23229 static struct die_info *
23230 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23231 struct dwarf2_cu **ref_cu)
23232 {
23233 struct die_info temp_die;
23234 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23235 struct die_info *die;
23236
23237 /* While it might be nice to assert sig_type->type == NULL here,
23238 we can get here for DW_AT_imported_declaration where we need
23239 the DIE not the type. */
23240
23241 /* If necessary, add it to the queue and load its DIEs. */
23242
23243 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23244 read_signatured_type (sig_type);
23245
23246 sig_cu = sig_type->per_cu.cu;
23247 gdb_assert (sig_cu != NULL);
23248 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23249 temp_die.sect_off = sig_type->type_offset_in_section;
23250 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23251 to_underlying (temp_die.sect_off));
23252 if (die)
23253 {
23254 struct dwarf2_per_objfile *dwarf2_per_objfile
23255 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23256
23257 /* For .gdb_index version 7 keep track of included TUs.
23258 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23259 if (dwarf2_per_objfile->index_table != NULL
23260 && dwarf2_per_objfile->index_table->version <= 7)
23261 {
23262 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23263 }
23264
23265 *ref_cu = sig_cu;
23266 if (sig_cu != cu)
23267 sig_cu->ancestor = cu;
23268
23269 return die;
23270 }
23271
23272 return NULL;
23273 }
23274
23275 /* Follow signatured type referenced by ATTR in SRC_DIE.
23276 On entry *REF_CU is the CU of SRC_DIE.
23277 On exit *REF_CU is the CU of the result.
23278 The result is the DIE of the type.
23279 If the referenced type cannot be found an error is thrown. */
23280
23281 static struct die_info *
23282 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23283 struct dwarf2_cu **ref_cu)
23284 {
23285 ULONGEST signature = DW_SIGNATURE (attr);
23286 struct signatured_type *sig_type;
23287 struct die_info *die;
23288
23289 gdb_assert (attr->form == DW_FORM_ref_sig8);
23290
23291 sig_type = lookup_signatured_type (*ref_cu, signature);
23292 /* sig_type will be NULL if the signatured type is missing from
23293 the debug info. */
23294 if (sig_type == NULL)
23295 {
23296 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23297 " from DIE at %s [in module %s]"),
23298 hex_string (signature), sect_offset_str (src_die->sect_off),
23299 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23300 }
23301
23302 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23303 if (die == NULL)
23304 {
23305 dump_die_for_error (src_die);
23306 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23307 " from DIE at %s [in module %s]"),
23308 hex_string (signature), sect_offset_str (src_die->sect_off),
23309 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23310 }
23311
23312 return die;
23313 }
23314
23315 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23316 reading in and processing the type unit if necessary. */
23317
23318 static struct type *
23319 get_signatured_type (struct die_info *die, ULONGEST signature,
23320 struct dwarf2_cu *cu)
23321 {
23322 struct dwarf2_per_objfile *dwarf2_per_objfile
23323 = cu->per_cu->dwarf2_per_objfile;
23324 struct signatured_type *sig_type;
23325 struct dwarf2_cu *type_cu;
23326 struct die_info *type_die;
23327 struct type *type;
23328
23329 sig_type = lookup_signatured_type (cu, signature);
23330 /* sig_type will be NULL if the signatured type is missing from
23331 the debug info. */
23332 if (sig_type == NULL)
23333 {
23334 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23335 " from DIE at %s [in module %s]"),
23336 hex_string (signature), sect_offset_str (die->sect_off),
23337 objfile_name (dwarf2_per_objfile->objfile));
23338 return build_error_marker_type (cu, die);
23339 }
23340
23341 /* If we already know the type we're done. */
23342 if (sig_type->type != NULL)
23343 return sig_type->type;
23344
23345 type_cu = cu;
23346 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23347 if (type_die != NULL)
23348 {
23349 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23350 is created. This is important, for example, because for c++ classes
23351 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23352 type = read_type_die (type_die, type_cu);
23353 if (type == NULL)
23354 {
23355 complaint (_("Dwarf Error: Cannot build signatured type %s"
23356 " referenced from DIE at %s [in module %s]"),
23357 hex_string (signature), sect_offset_str (die->sect_off),
23358 objfile_name (dwarf2_per_objfile->objfile));
23359 type = build_error_marker_type (cu, die);
23360 }
23361 }
23362 else
23363 {
23364 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23365 " from DIE at %s [in module %s]"),
23366 hex_string (signature), sect_offset_str (die->sect_off),
23367 objfile_name (dwarf2_per_objfile->objfile));
23368 type = build_error_marker_type (cu, die);
23369 }
23370 sig_type->type = type;
23371
23372 return type;
23373 }
23374
23375 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23376 reading in and processing the type unit if necessary. */
23377
23378 static struct type *
23379 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23380 struct dwarf2_cu *cu) /* ARI: editCase function */
23381 {
23382 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23383 if (attr->form_is_ref ())
23384 {
23385 struct dwarf2_cu *type_cu = cu;
23386 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23387
23388 return read_type_die (type_die, type_cu);
23389 }
23390 else if (attr->form == DW_FORM_ref_sig8)
23391 {
23392 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23393 }
23394 else
23395 {
23396 struct dwarf2_per_objfile *dwarf2_per_objfile
23397 = cu->per_cu->dwarf2_per_objfile;
23398
23399 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23400 " at %s [in module %s]"),
23401 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23402 objfile_name (dwarf2_per_objfile->objfile));
23403 return build_error_marker_type (cu, die);
23404 }
23405 }
23406
23407 /* Load the DIEs associated with type unit PER_CU into memory. */
23408
23409 static void
23410 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23411 {
23412 struct signatured_type *sig_type;
23413
23414 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23415 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23416
23417 /* We have the per_cu, but we need the signatured_type.
23418 Fortunately this is an easy translation. */
23419 gdb_assert (per_cu->is_debug_types);
23420 sig_type = (struct signatured_type *) per_cu;
23421
23422 gdb_assert (per_cu->cu == NULL);
23423
23424 read_signatured_type (sig_type);
23425
23426 gdb_assert (per_cu->cu != NULL);
23427 }
23428
23429 /* Read in a signatured type and build its CU and DIEs.
23430 If the type is a stub for the real type in a DWO file,
23431 read in the real type from the DWO file as well. */
23432
23433 static void
23434 read_signatured_type (struct signatured_type *sig_type)
23435 {
23436 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23437
23438 gdb_assert (per_cu->is_debug_types);
23439 gdb_assert (per_cu->cu == NULL);
23440
23441 cutu_reader reader (per_cu, NULL, 0, 1, false);
23442
23443 if (!reader.dummy_p)
23444 {
23445 struct dwarf2_cu *cu = reader.cu;
23446 const gdb_byte *info_ptr = reader.info_ptr;
23447
23448 gdb_assert (cu->die_hash == NULL);
23449 cu->die_hash =
23450 htab_create_alloc_ex (cu->header.length / 12,
23451 die_hash,
23452 die_eq,
23453 NULL,
23454 &cu->comp_unit_obstack,
23455 hashtab_obstack_allocate,
23456 dummy_obstack_deallocate);
23457
23458 if (reader.comp_unit_die->has_children)
23459 reader.comp_unit_die->child
23460 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23461 reader.comp_unit_die);
23462 cu->dies = reader.comp_unit_die;
23463 /* comp_unit_die is not stored in die_hash, no need. */
23464
23465 /* We try not to read any attributes in this function, because
23466 not all CUs needed for references have been loaded yet, and
23467 symbol table processing isn't initialized. But we have to
23468 set the CU language, or we won't be able to build types
23469 correctly. Similarly, if we do not read the producer, we can
23470 not apply producer-specific interpretation. */
23471 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23472 }
23473
23474 sig_type->per_cu.tu_read = 1;
23475 }
23476
23477 /* Decode simple location descriptions.
23478 Given a pointer to a dwarf block that defines a location, compute
23479 the location and return the value.
23480
23481 NOTE drow/2003-11-18: This function is called in two situations
23482 now: for the address of static or global variables (partial symbols
23483 only) and for offsets into structures which are expected to be
23484 (more or less) constant. The partial symbol case should go away,
23485 and only the constant case should remain. That will let this
23486 function complain more accurately. A few special modes are allowed
23487 without complaint for global variables (for instance, global
23488 register values and thread-local values).
23489
23490 A location description containing no operations indicates that the
23491 object is optimized out. The return value is 0 for that case.
23492 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23493 callers will only want a very basic result and this can become a
23494 complaint.
23495
23496 Note that stack[0] is unused except as a default error return. */
23497
23498 static CORE_ADDR
23499 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23500 {
23501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23502 size_t i;
23503 size_t size = blk->size;
23504 const gdb_byte *data = blk->data;
23505 CORE_ADDR stack[64];
23506 int stacki;
23507 unsigned int bytes_read, unsnd;
23508 gdb_byte op;
23509
23510 i = 0;
23511 stacki = 0;
23512 stack[stacki] = 0;
23513 stack[++stacki] = 0;
23514
23515 while (i < size)
23516 {
23517 op = data[i++];
23518 switch (op)
23519 {
23520 case DW_OP_lit0:
23521 case DW_OP_lit1:
23522 case DW_OP_lit2:
23523 case DW_OP_lit3:
23524 case DW_OP_lit4:
23525 case DW_OP_lit5:
23526 case DW_OP_lit6:
23527 case DW_OP_lit7:
23528 case DW_OP_lit8:
23529 case DW_OP_lit9:
23530 case DW_OP_lit10:
23531 case DW_OP_lit11:
23532 case DW_OP_lit12:
23533 case DW_OP_lit13:
23534 case DW_OP_lit14:
23535 case DW_OP_lit15:
23536 case DW_OP_lit16:
23537 case DW_OP_lit17:
23538 case DW_OP_lit18:
23539 case DW_OP_lit19:
23540 case DW_OP_lit20:
23541 case DW_OP_lit21:
23542 case DW_OP_lit22:
23543 case DW_OP_lit23:
23544 case DW_OP_lit24:
23545 case DW_OP_lit25:
23546 case DW_OP_lit26:
23547 case DW_OP_lit27:
23548 case DW_OP_lit28:
23549 case DW_OP_lit29:
23550 case DW_OP_lit30:
23551 case DW_OP_lit31:
23552 stack[++stacki] = op - DW_OP_lit0;
23553 break;
23554
23555 case DW_OP_reg0:
23556 case DW_OP_reg1:
23557 case DW_OP_reg2:
23558 case DW_OP_reg3:
23559 case DW_OP_reg4:
23560 case DW_OP_reg5:
23561 case DW_OP_reg6:
23562 case DW_OP_reg7:
23563 case DW_OP_reg8:
23564 case DW_OP_reg9:
23565 case DW_OP_reg10:
23566 case DW_OP_reg11:
23567 case DW_OP_reg12:
23568 case DW_OP_reg13:
23569 case DW_OP_reg14:
23570 case DW_OP_reg15:
23571 case DW_OP_reg16:
23572 case DW_OP_reg17:
23573 case DW_OP_reg18:
23574 case DW_OP_reg19:
23575 case DW_OP_reg20:
23576 case DW_OP_reg21:
23577 case DW_OP_reg22:
23578 case DW_OP_reg23:
23579 case DW_OP_reg24:
23580 case DW_OP_reg25:
23581 case DW_OP_reg26:
23582 case DW_OP_reg27:
23583 case DW_OP_reg28:
23584 case DW_OP_reg29:
23585 case DW_OP_reg30:
23586 case DW_OP_reg31:
23587 stack[++stacki] = op - DW_OP_reg0;
23588 if (i < size)
23589 dwarf2_complex_location_expr_complaint ();
23590 break;
23591
23592 case DW_OP_regx:
23593 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23594 i += bytes_read;
23595 stack[++stacki] = unsnd;
23596 if (i < size)
23597 dwarf2_complex_location_expr_complaint ();
23598 break;
23599
23600 case DW_OP_addr:
23601 stack[++stacki] = read_address (objfile->obfd, &data[i],
23602 cu, &bytes_read);
23603 i += bytes_read;
23604 break;
23605
23606 case DW_OP_const1u:
23607 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23608 i += 1;
23609 break;
23610
23611 case DW_OP_const1s:
23612 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23613 i += 1;
23614 break;
23615
23616 case DW_OP_const2u:
23617 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23618 i += 2;
23619 break;
23620
23621 case DW_OP_const2s:
23622 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23623 i += 2;
23624 break;
23625
23626 case DW_OP_const4u:
23627 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23628 i += 4;
23629 break;
23630
23631 case DW_OP_const4s:
23632 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23633 i += 4;
23634 break;
23635
23636 case DW_OP_const8u:
23637 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23638 i += 8;
23639 break;
23640
23641 case DW_OP_constu:
23642 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23643 &bytes_read);
23644 i += bytes_read;
23645 break;
23646
23647 case DW_OP_consts:
23648 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23649 i += bytes_read;
23650 break;
23651
23652 case DW_OP_dup:
23653 stack[stacki + 1] = stack[stacki];
23654 stacki++;
23655 break;
23656
23657 case DW_OP_plus:
23658 stack[stacki - 1] += stack[stacki];
23659 stacki--;
23660 break;
23661
23662 case DW_OP_plus_uconst:
23663 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23664 &bytes_read);
23665 i += bytes_read;
23666 break;
23667
23668 case DW_OP_minus:
23669 stack[stacki - 1] -= stack[stacki];
23670 stacki--;
23671 break;
23672
23673 case DW_OP_deref:
23674 /* If we're not the last op, then we definitely can't encode
23675 this using GDB's address_class enum. This is valid for partial
23676 global symbols, although the variable's address will be bogus
23677 in the psymtab. */
23678 if (i < size)
23679 dwarf2_complex_location_expr_complaint ();
23680 break;
23681
23682 case DW_OP_GNU_push_tls_address:
23683 case DW_OP_form_tls_address:
23684 /* The top of the stack has the offset from the beginning
23685 of the thread control block at which the variable is located. */
23686 /* Nothing should follow this operator, so the top of stack would
23687 be returned. */
23688 /* This is valid for partial global symbols, but the variable's
23689 address will be bogus in the psymtab. Make it always at least
23690 non-zero to not look as a variable garbage collected by linker
23691 which have DW_OP_addr 0. */
23692 if (i < size)
23693 dwarf2_complex_location_expr_complaint ();
23694 stack[stacki]++;
23695 break;
23696
23697 case DW_OP_GNU_uninit:
23698 break;
23699
23700 case DW_OP_addrx:
23701 case DW_OP_GNU_addr_index:
23702 case DW_OP_GNU_const_index:
23703 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23704 &bytes_read);
23705 i += bytes_read;
23706 break;
23707
23708 default:
23709 {
23710 const char *name = get_DW_OP_name (op);
23711
23712 if (name)
23713 complaint (_("unsupported stack op: '%s'"),
23714 name);
23715 else
23716 complaint (_("unsupported stack op: '%02x'"),
23717 op);
23718 }
23719
23720 return (stack[stacki]);
23721 }
23722
23723 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23724 outside of the allocated space. Also enforce minimum>0. */
23725 if (stacki >= ARRAY_SIZE (stack) - 1)
23726 {
23727 complaint (_("location description stack overflow"));
23728 return 0;
23729 }
23730
23731 if (stacki <= 0)
23732 {
23733 complaint (_("location description stack underflow"));
23734 return 0;
23735 }
23736 }
23737 return (stack[stacki]);
23738 }
23739
23740 /* memory allocation interface */
23741
23742 static struct dwarf_block *
23743 dwarf_alloc_block (struct dwarf2_cu *cu)
23744 {
23745 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23746 }
23747
23748 static struct die_info *
23749 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23750 {
23751 struct die_info *die;
23752 size_t size = sizeof (struct die_info);
23753
23754 if (num_attrs > 1)
23755 size += (num_attrs - 1) * sizeof (struct attribute);
23756
23757 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23758 memset (die, 0, sizeof (struct die_info));
23759 return (die);
23760 }
23761
23762 \f
23763 /* Macro support. */
23764
23765 /* Return file name relative to the compilation directory of file number I in
23766 *LH's file name table. The result is allocated using xmalloc; the caller is
23767 responsible for freeing it. */
23768
23769 static char *
23770 file_file_name (int file, struct line_header *lh)
23771 {
23772 /* Is the file number a valid index into the line header's file name
23773 table? Remember that file numbers start with one, not zero. */
23774 if (lh->is_valid_file_index (file))
23775 {
23776 const file_entry *fe = lh->file_name_at (file);
23777
23778 if (!IS_ABSOLUTE_PATH (fe->name))
23779 {
23780 const char *dir = fe->include_dir (lh);
23781 if (dir != NULL)
23782 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
23783 }
23784 return xstrdup (fe->name);
23785 }
23786 else
23787 {
23788 /* The compiler produced a bogus file number. We can at least
23789 record the macro definitions made in the file, even if we
23790 won't be able to find the file by name. */
23791 char fake_name[80];
23792
23793 xsnprintf (fake_name, sizeof (fake_name),
23794 "<bad macro file number %d>", file);
23795
23796 complaint (_("bad file number in macro information (%d)"),
23797 file);
23798
23799 return xstrdup (fake_name);
23800 }
23801 }
23802
23803 /* Return the full name of file number I in *LH's file name table.
23804 Use COMP_DIR as the name of the current directory of the
23805 compilation. The result is allocated using xmalloc; the caller is
23806 responsible for freeing it. */
23807 static char *
23808 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23809 {
23810 /* Is the file number a valid index into the line header's file name
23811 table? Remember that file numbers start with one, not zero. */
23812 if (lh->is_valid_file_index (file))
23813 {
23814 char *relative = file_file_name (file, lh);
23815
23816 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23817 return relative;
23818 return reconcat (relative, comp_dir, SLASH_STRING,
23819 relative, (char *) NULL);
23820 }
23821 else
23822 return file_file_name (file, lh);
23823 }
23824
23825
23826 static struct macro_source_file *
23827 macro_start_file (struct dwarf2_cu *cu,
23828 int file, int line,
23829 struct macro_source_file *current_file,
23830 struct line_header *lh)
23831 {
23832 /* File name relative to the compilation directory of this source file. */
23833 char *file_name = file_file_name (file, lh);
23834
23835 if (! current_file)
23836 {
23837 /* Note: We don't create a macro table for this compilation unit
23838 at all until we actually get a filename. */
23839 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23840
23841 /* If we have no current file, then this must be the start_file
23842 directive for the compilation unit's main source file. */
23843 current_file = macro_set_main (macro_table, file_name);
23844 macro_define_special (macro_table);
23845 }
23846 else
23847 current_file = macro_include (current_file, line, file_name);
23848
23849 xfree (file_name);
23850
23851 return current_file;
23852 }
23853
23854 static const char *
23855 consume_improper_spaces (const char *p, const char *body)
23856 {
23857 if (*p == ' ')
23858 {
23859 complaint (_("macro definition contains spaces "
23860 "in formal argument list:\n`%s'"),
23861 body);
23862
23863 while (*p == ' ')
23864 p++;
23865 }
23866
23867 return p;
23868 }
23869
23870
23871 static void
23872 parse_macro_definition (struct macro_source_file *file, int line,
23873 const char *body)
23874 {
23875 const char *p;
23876
23877 /* The body string takes one of two forms. For object-like macro
23878 definitions, it should be:
23879
23880 <macro name> " " <definition>
23881
23882 For function-like macro definitions, it should be:
23883
23884 <macro name> "() " <definition>
23885 or
23886 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23887
23888 Spaces may appear only where explicitly indicated, and in the
23889 <definition>.
23890
23891 The Dwarf 2 spec says that an object-like macro's name is always
23892 followed by a space, but versions of GCC around March 2002 omit
23893 the space when the macro's definition is the empty string.
23894
23895 The Dwarf 2 spec says that there should be no spaces between the
23896 formal arguments in a function-like macro's formal argument list,
23897 but versions of GCC around March 2002 include spaces after the
23898 commas. */
23899
23900
23901 /* Find the extent of the macro name. The macro name is terminated
23902 by either a space or null character (for an object-like macro) or
23903 an opening paren (for a function-like macro). */
23904 for (p = body; *p; p++)
23905 if (*p == ' ' || *p == '(')
23906 break;
23907
23908 if (*p == ' ' || *p == '\0')
23909 {
23910 /* It's an object-like macro. */
23911 int name_len = p - body;
23912 std::string name (body, name_len);
23913 const char *replacement;
23914
23915 if (*p == ' ')
23916 replacement = body + name_len + 1;
23917 else
23918 {
23919 dwarf2_macro_malformed_definition_complaint (body);
23920 replacement = body + name_len;
23921 }
23922
23923 macro_define_object (file, line, name.c_str (), replacement);
23924 }
23925 else if (*p == '(')
23926 {
23927 /* It's a function-like macro. */
23928 std::string name (body, p - body);
23929 int argc = 0;
23930 int argv_size = 1;
23931 char **argv = XNEWVEC (char *, argv_size);
23932
23933 p++;
23934
23935 p = consume_improper_spaces (p, body);
23936
23937 /* Parse the formal argument list. */
23938 while (*p && *p != ')')
23939 {
23940 /* Find the extent of the current argument name. */
23941 const char *arg_start = p;
23942
23943 while (*p && *p != ',' && *p != ')' && *p != ' ')
23944 p++;
23945
23946 if (! *p || p == arg_start)
23947 dwarf2_macro_malformed_definition_complaint (body);
23948 else
23949 {
23950 /* Make sure argv has room for the new argument. */
23951 if (argc >= argv_size)
23952 {
23953 argv_size *= 2;
23954 argv = XRESIZEVEC (char *, argv, argv_size);
23955 }
23956
23957 argv[argc++] = savestring (arg_start, p - arg_start);
23958 }
23959
23960 p = consume_improper_spaces (p, body);
23961
23962 /* Consume the comma, if present. */
23963 if (*p == ',')
23964 {
23965 p++;
23966
23967 p = consume_improper_spaces (p, body);
23968 }
23969 }
23970
23971 if (*p == ')')
23972 {
23973 p++;
23974
23975 if (*p == ' ')
23976 /* Perfectly formed definition, no complaints. */
23977 macro_define_function (file, line, name.c_str (),
23978 argc, (const char **) argv,
23979 p + 1);
23980 else if (*p == '\0')
23981 {
23982 /* Complain, but do define it. */
23983 dwarf2_macro_malformed_definition_complaint (body);
23984 macro_define_function (file, line, name.c_str (),
23985 argc, (const char **) argv,
23986 p);
23987 }
23988 else
23989 /* Just complain. */
23990 dwarf2_macro_malformed_definition_complaint (body);
23991 }
23992 else
23993 /* Just complain. */
23994 dwarf2_macro_malformed_definition_complaint (body);
23995
23996 {
23997 int i;
23998
23999 for (i = 0; i < argc; i++)
24000 xfree (argv[i]);
24001 }
24002 xfree (argv);
24003 }
24004 else
24005 dwarf2_macro_malformed_definition_complaint (body);
24006 }
24007
24008 /* Skip some bytes from BYTES according to the form given in FORM.
24009 Returns the new pointer. */
24010
24011 static const gdb_byte *
24012 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24013 enum dwarf_form form,
24014 unsigned int offset_size,
24015 struct dwarf2_section_info *section)
24016 {
24017 unsigned int bytes_read;
24018
24019 switch (form)
24020 {
24021 case DW_FORM_data1:
24022 case DW_FORM_flag:
24023 ++bytes;
24024 break;
24025
24026 case DW_FORM_data2:
24027 bytes += 2;
24028 break;
24029
24030 case DW_FORM_data4:
24031 bytes += 4;
24032 break;
24033
24034 case DW_FORM_data8:
24035 bytes += 8;
24036 break;
24037
24038 case DW_FORM_data16:
24039 bytes += 16;
24040 break;
24041
24042 case DW_FORM_string:
24043 read_direct_string (abfd, bytes, &bytes_read);
24044 bytes += bytes_read;
24045 break;
24046
24047 case DW_FORM_sec_offset:
24048 case DW_FORM_strp:
24049 case DW_FORM_GNU_strp_alt:
24050 bytes += offset_size;
24051 break;
24052
24053 case DW_FORM_block:
24054 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24055 bytes += bytes_read;
24056 break;
24057
24058 case DW_FORM_block1:
24059 bytes += 1 + read_1_byte (abfd, bytes);
24060 break;
24061 case DW_FORM_block2:
24062 bytes += 2 + read_2_bytes (abfd, bytes);
24063 break;
24064 case DW_FORM_block4:
24065 bytes += 4 + read_4_bytes (abfd, bytes);
24066 break;
24067
24068 case DW_FORM_addrx:
24069 case DW_FORM_sdata:
24070 case DW_FORM_strx:
24071 case DW_FORM_udata:
24072 case DW_FORM_GNU_addr_index:
24073 case DW_FORM_GNU_str_index:
24074 bytes = gdb_skip_leb128 (bytes, buffer_end);
24075 if (bytes == NULL)
24076 {
24077 dwarf2_section_buffer_overflow_complaint (section);
24078 return NULL;
24079 }
24080 break;
24081
24082 case DW_FORM_implicit_const:
24083 break;
24084
24085 default:
24086 {
24087 complaint (_("invalid form 0x%x in `%s'"),
24088 form, section->get_name ());
24089 return NULL;
24090 }
24091 }
24092
24093 return bytes;
24094 }
24095
24096 /* A helper for dwarf_decode_macros that handles skipping an unknown
24097 opcode. Returns an updated pointer to the macro data buffer; or,
24098 on error, issues a complaint and returns NULL. */
24099
24100 static const gdb_byte *
24101 skip_unknown_opcode (unsigned int opcode,
24102 const gdb_byte **opcode_definitions,
24103 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24104 bfd *abfd,
24105 unsigned int offset_size,
24106 struct dwarf2_section_info *section)
24107 {
24108 unsigned int bytes_read, i;
24109 unsigned long arg;
24110 const gdb_byte *defn;
24111
24112 if (opcode_definitions[opcode] == NULL)
24113 {
24114 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24115 opcode);
24116 return NULL;
24117 }
24118
24119 defn = opcode_definitions[opcode];
24120 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24121 defn += bytes_read;
24122
24123 for (i = 0; i < arg; ++i)
24124 {
24125 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24126 (enum dwarf_form) defn[i], offset_size,
24127 section);
24128 if (mac_ptr == NULL)
24129 {
24130 /* skip_form_bytes already issued the complaint. */
24131 return NULL;
24132 }
24133 }
24134
24135 return mac_ptr;
24136 }
24137
24138 /* A helper function which parses the header of a macro section.
24139 If the macro section is the extended (for now called "GNU") type,
24140 then this updates *OFFSET_SIZE. Returns a pointer to just after
24141 the header, or issues a complaint and returns NULL on error. */
24142
24143 static const gdb_byte *
24144 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24145 bfd *abfd,
24146 const gdb_byte *mac_ptr,
24147 unsigned int *offset_size,
24148 int section_is_gnu)
24149 {
24150 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24151
24152 if (section_is_gnu)
24153 {
24154 unsigned int version, flags;
24155
24156 version = read_2_bytes (abfd, mac_ptr);
24157 if (version != 4 && version != 5)
24158 {
24159 complaint (_("unrecognized version `%d' in .debug_macro section"),
24160 version);
24161 return NULL;
24162 }
24163 mac_ptr += 2;
24164
24165 flags = read_1_byte (abfd, mac_ptr);
24166 ++mac_ptr;
24167 *offset_size = (flags & 1) ? 8 : 4;
24168
24169 if ((flags & 2) != 0)
24170 /* We don't need the line table offset. */
24171 mac_ptr += *offset_size;
24172
24173 /* Vendor opcode descriptions. */
24174 if ((flags & 4) != 0)
24175 {
24176 unsigned int i, count;
24177
24178 count = read_1_byte (abfd, mac_ptr);
24179 ++mac_ptr;
24180 for (i = 0; i < count; ++i)
24181 {
24182 unsigned int opcode, bytes_read;
24183 unsigned long arg;
24184
24185 opcode = read_1_byte (abfd, mac_ptr);
24186 ++mac_ptr;
24187 opcode_definitions[opcode] = mac_ptr;
24188 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24189 mac_ptr += bytes_read;
24190 mac_ptr += arg;
24191 }
24192 }
24193 }
24194
24195 return mac_ptr;
24196 }
24197
24198 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24199 including DW_MACRO_import. */
24200
24201 static void
24202 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24203 bfd *abfd,
24204 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24205 struct macro_source_file *current_file,
24206 struct line_header *lh,
24207 struct dwarf2_section_info *section,
24208 int section_is_gnu, int section_is_dwz,
24209 unsigned int offset_size,
24210 htab_t include_hash)
24211 {
24212 struct dwarf2_per_objfile *dwarf2_per_objfile
24213 = cu->per_cu->dwarf2_per_objfile;
24214 struct objfile *objfile = dwarf2_per_objfile->objfile;
24215 enum dwarf_macro_record_type macinfo_type;
24216 int at_commandline;
24217 const gdb_byte *opcode_definitions[256];
24218
24219 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24220 &offset_size, section_is_gnu);
24221 if (mac_ptr == NULL)
24222 {
24223 /* We already issued a complaint. */
24224 return;
24225 }
24226
24227 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24228 GDB is still reading the definitions from command line. First
24229 DW_MACINFO_start_file will need to be ignored as it was already executed
24230 to create CURRENT_FILE for the main source holding also the command line
24231 definitions. On first met DW_MACINFO_start_file this flag is reset to
24232 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24233
24234 at_commandline = 1;
24235
24236 do
24237 {
24238 /* Do we at least have room for a macinfo type byte? */
24239 if (mac_ptr >= mac_end)
24240 {
24241 dwarf2_section_buffer_overflow_complaint (section);
24242 break;
24243 }
24244
24245 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24246 mac_ptr++;
24247
24248 /* Note that we rely on the fact that the corresponding GNU and
24249 DWARF constants are the same. */
24250 DIAGNOSTIC_PUSH
24251 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24252 switch (macinfo_type)
24253 {
24254 /* A zero macinfo type indicates the end of the macro
24255 information. */
24256 case 0:
24257 break;
24258
24259 case DW_MACRO_define:
24260 case DW_MACRO_undef:
24261 case DW_MACRO_define_strp:
24262 case DW_MACRO_undef_strp:
24263 case DW_MACRO_define_sup:
24264 case DW_MACRO_undef_sup:
24265 {
24266 unsigned int bytes_read;
24267 int line;
24268 const char *body;
24269 int is_define;
24270
24271 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24272 mac_ptr += bytes_read;
24273
24274 if (macinfo_type == DW_MACRO_define
24275 || macinfo_type == DW_MACRO_undef)
24276 {
24277 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24278 mac_ptr += bytes_read;
24279 }
24280 else
24281 {
24282 LONGEST str_offset;
24283
24284 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24285 mac_ptr += offset_size;
24286
24287 if (macinfo_type == DW_MACRO_define_sup
24288 || macinfo_type == DW_MACRO_undef_sup
24289 || section_is_dwz)
24290 {
24291 struct dwz_file *dwz
24292 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24293
24294 body = read_indirect_string_from_dwz (objfile,
24295 dwz, str_offset);
24296 }
24297 else
24298 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24299 abfd, str_offset);
24300 }
24301
24302 is_define = (macinfo_type == DW_MACRO_define
24303 || macinfo_type == DW_MACRO_define_strp
24304 || macinfo_type == DW_MACRO_define_sup);
24305 if (! current_file)
24306 {
24307 /* DWARF violation as no main source is present. */
24308 complaint (_("debug info with no main source gives macro %s "
24309 "on line %d: %s"),
24310 is_define ? _("definition") : _("undefinition"),
24311 line, body);
24312 break;
24313 }
24314 if ((line == 0 && !at_commandline)
24315 || (line != 0 && at_commandline))
24316 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24317 at_commandline ? _("command-line") : _("in-file"),
24318 is_define ? _("definition") : _("undefinition"),
24319 line == 0 ? _("zero") : _("non-zero"), line, body);
24320
24321 if (body == NULL)
24322 {
24323 /* Fedora's rpm-build's "debugedit" binary
24324 corrupted .debug_macro sections.
24325
24326 For more info, see
24327 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24328 complaint (_("debug info gives %s invalid macro %s "
24329 "without body (corrupted?) at line %d "
24330 "on file %s"),
24331 at_commandline ? _("command-line") : _("in-file"),
24332 is_define ? _("definition") : _("undefinition"),
24333 line, current_file->filename);
24334 }
24335 else if (is_define)
24336 parse_macro_definition (current_file, line, body);
24337 else
24338 {
24339 gdb_assert (macinfo_type == DW_MACRO_undef
24340 || macinfo_type == DW_MACRO_undef_strp
24341 || macinfo_type == DW_MACRO_undef_sup);
24342 macro_undef (current_file, line, body);
24343 }
24344 }
24345 break;
24346
24347 case DW_MACRO_start_file:
24348 {
24349 unsigned int bytes_read;
24350 int line, file;
24351
24352 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24353 mac_ptr += bytes_read;
24354 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24355 mac_ptr += bytes_read;
24356
24357 if ((line == 0 && !at_commandline)
24358 || (line != 0 && at_commandline))
24359 complaint (_("debug info gives source %d included "
24360 "from %s at %s line %d"),
24361 file, at_commandline ? _("command-line") : _("file"),
24362 line == 0 ? _("zero") : _("non-zero"), line);
24363
24364 if (at_commandline)
24365 {
24366 /* This DW_MACRO_start_file was executed in the
24367 pass one. */
24368 at_commandline = 0;
24369 }
24370 else
24371 current_file = macro_start_file (cu, file, line, current_file,
24372 lh);
24373 }
24374 break;
24375
24376 case DW_MACRO_end_file:
24377 if (! current_file)
24378 complaint (_("macro debug info has an unmatched "
24379 "`close_file' directive"));
24380 else
24381 {
24382 current_file = current_file->included_by;
24383 if (! current_file)
24384 {
24385 enum dwarf_macro_record_type next_type;
24386
24387 /* GCC circa March 2002 doesn't produce the zero
24388 type byte marking the end of the compilation
24389 unit. Complain if it's not there, but exit no
24390 matter what. */
24391
24392 /* Do we at least have room for a macinfo type byte? */
24393 if (mac_ptr >= mac_end)
24394 {
24395 dwarf2_section_buffer_overflow_complaint (section);
24396 return;
24397 }
24398
24399 /* We don't increment mac_ptr here, so this is just
24400 a look-ahead. */
24401 next_type
24402 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24403 mac_ptr);
24404 if (next_type != 0)
24405 complaint (_("no terminating 0-type entry for "
24406 "macros in `.debug_macinfo' section"));
24407
24408 return;
24409 }
24410 }
24411 break;
24412
24413 case DW_MACRO_import:
24414 case DW_MACRO_import_sup:
24415 {
24416 LONGEST offset;
24417 void **slot;
24418 bfd *include_bfd = abfd;
24419 struct dwarf2_section_info *include_section = section;
24420 const gdb_byte *include_mac_end = mac_end;
24421 int is_dwz = section_is_dwz;
24422 const gdb_byte *new_mac_ptr;
24423
24424 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24425 mac_ptr += offset_size;
24426
24427 if (macinfo_type == DW_MACRO_import_sup)
24428 {
24429 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24430
24431 dwz->macro.read (objfile);
24432
24433 include_section = &dwz->macro;
24434 include_bfd = include_section->get_bfd_owner ();
24435 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24436 is_dwz = 1;
24437 }
24438
24439 new_mac_ptr = include_section->buffer + offset;
24440 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24441
24442 if (*slot != NULL)
24443 {
24444 /* This has actually happened; see
24445 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24446 complaint (_("recursive DW_MACRO_import in "
24447 ".debug_macro section"));
24448 }
24449 else
24450 {
24451 *slot = (void *) new_mac_ptr;
24452
24453 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24454 include_mac_end, current_file, lh,
24455 section, section_is_gnu, is_dwz,
24456 offset_size, include_hash);
24457
24458 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24459 }
24460 }
24461 break;
24462
24463 case DW_MACINFO_vendor_ext:
24464 if (!section_is_gnu)
24465 {
24466 unsigned int bytes_read;
24467
24468 /* This reads the constant, but since we don't recognize
24469 any vendor extensions, we ignore it. */
24470 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24471 mac_ptr += bytes_read;
24472 read_direct_string (abfd, mac_ptr, &bytes_read);
24473 mac_ptr += bytes_read;
24474
24475 /* We don't recognize any vendor extensions. */
24476 break;
24477 }
24478 /* FALLTHROUGH */
24479
24480 default:
24481 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24482 mac_ptr, mac_end, abfd, offset_size,
24483 section);
24484 if (mac_ptr == NULL)
24485 return;
24486 break;
24487 }
24488 DIAGNOSTIC_POP
24489 } while (macinfo_type != 0);
24490 }
24491
24492 static void
24493 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24494 int section_is_gnu)
24495 {
24496 struct dwarf2_per_objfile *dwarf2_per_objfile
24497 = cu->per_cu->dwarf2_per_objfile;
24498 struct objfile *objfile = dwarf2_per_objfile->objfile;
24499 struct line_header *lh = cu->line_header;
24500 bfd *abfd;
24501 const gdb_byte *mac_ptr, *mac_end;
24502 struct macro_source_file *current_file = 0;
24503 enum dwarf_macro_record_type macinfo_type;
24504 unsigned int offset_size = cu->header.offset_size;
24505 const gdb_byte *opcode_definitions[256];
24506 void **slot;
24507 struct dwarf2_section_info *section;
24508 const char *section_name;
24509
24510 if (cu->dwo_unit != NULL)
24511 {
24512 if (section_is_gnu)
24513 {
24514 section = &cu->dwo_unit->dwo_file->sections.macro;
24515 section_name = ".debug_macro.dwo";
24516 }
24517 else
24518 {
24519 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24520 section_name = ".debug_macinfo.dwo";
24521 }
24522 }
24523 else
24524 {
24525 if (section_is_gnu)
24526 {
24527 section = &dwarf2_per_objfile->macro;
24528 section_name = ".debug_macro";
24529 }
24530 else
24531 {
24532 section = &dwarf2_per_objfile->macinfo;
24533 section_name = ".debug_macinfo";
24534 }
24535 }
24536
24537 section->read (objfile);
24538 if (section->buffer == NULL)
24539 {
24540 complaint (_("missing %s section"), section_name);
24541 return;
24542 }
24543 abfd = section->get_bfd_owner ();
24544
24545 /* First pass: Find the name of the base filename.
24546 This filename is needed in order to process all macros whose definition
24547 (or undefinition) comes from the command line. These macros are defined
24548 before the first DW_MACINFO_start_file entry, and yet still need to be
24549 associated to the base file.
24550
24551 To determine the base file name, we scan the macro definitions until we
24552 reach the first DW_MACINFO_start_file entry. We then initialize
24553 CURRENT_FILE accordingly so that any macro definition found before the
24554 first DW_MACINFO_start_file can still be associated to the base file. */
24555
24556 mac_ptr = section->buffer + offset;
24557 mac_end = section->buffer + section->size;
24558
24559 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24560 &offset_size, section_is_gnu);
24561 if (mac_ptr == NULL)
24562 {
24563 /* We already issued a complaint. */
24564 return;
24565 }
24566
24567 do
24568 {
24569 /* Do we at least have room for a macinfo type byte? */
24570 if (mac_ptr >= mac_end)
24571 {
24572 /* Complaint is printed during the second pass as GDB will probably
24573 stop the first pass earlier upon finding
24574 DW_MACINFO_start_file. */
24575 break;
24576 }
24577
24578 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24579 mac_ptr++;
24580
24581 /* Note that we rely on the fact that the corresponding GNU and
24582 DWARF constants are the same. */
24583 DIAGNOSTIC_PUSH
24584 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24585 switch (macinfo_type)
24586 {
24587 /* A zero macinfo type indicates the end of the macro
24588 information. */
24589 case 0:
24590 break;
24591
24592 case DW_MACRO_define:
24593 case DW_MACRO_undef:
24594 /* Only skip the data by MAC_PTR. */
24595 {
24596 unsigned int bytes_read;
24597
24598 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24599 mac_ptr += bytes_read;
24600 read_direct_string (abfd, mac_ptr, &bytes_read);
24601 mac_ptr += bytes_read;
24602 }
24603 break;
24604
24605 case DW_MACRO_start_file:
24606 {
24607 unsigned int bytes_read;
24608 int line, file;
24609
24610 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24611 mac_ptr += bytes_read;
24612 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24613 mac_ptr += bytes_read;
24614
24615 current_file = macro_start_file (cu, file, line, current_file, lh);
24616 }
24617 break;
24618
24619 case DW_MACRO_end_file:
24620 /* No data to skip by MAC_PTR. */
24621 break;
24622
24623 case DW_MACRO_define_strp:
24624 case DW_MACRO_undef_strp:
24625 case DW_MACRO_define_sup:
24626 case DW_MACRO_undef_sup:
24627 {
24628 unsigned int bytes_read;
24629
24630 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24631 mac_ptr += bytes_read;
24632 mac_ptr += offset_size;
24633 }
24634 break;
24635
24636 case DW_MACRO_import:
24637 case DW_MACRO_import_sup:
24638 /* Note that, according to the spec, a transparent include
24639 chain cannot call DW_MACRO_start_file. So, we can just
24640 skip this opcode. */
24641 mac_ptr += offset_size;
24642 break;
24643
24644 case DW_MACINFO_vendor_ext:
24645 /* Only skip the data by MAC_PTR. */
24646 if (!section_is_gnu)
24647 {
24648 unsigned int bytes_read;
24649
24650 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24651 mac_ptr += bytes_read;
24652 read_direct_string (abfd, mac_ptr, &bytes_read);
24653 mac_ptr += bytes_read;
24654 }
24655 /* FALLTHROUGH */
24656
24657 default:
24658 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24659 mac_ptr, mac_end, abfd, offset_size,
24660 section);
24661 if (mac_ptr == NULL)
24662 return;
24663 break;
24664 }
24665 DIAGNOSTIC_POP
24666 } while (macinfo_type != 0 && current_file == NULL);
24667
24668 /* Second pass: Process all entries.
24669
24670 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24671 command-line macro definitions/undefinitions. This flag is unset when we
24672 reach the first DW_MACINFO_start_file entry. */
24673
24674 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24675 htab_eq_pointer,
24676 NULL, xcalloc, xfree));
24677 mac_ptr = section->buffer + offset;
24678 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24679 *slot = (void *) mac_ptr;
24680 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24681 current_file, lh, section,
24682 section_is_gnu, 0, offset_size,
24683 include_hash.get ());
24684 }
24685
24686 /* Return the .debug_loc section to use for CU.
24687 For DWO files use .debug_loc.dwo. */
24688
24689 static struct dwarf2_section_info *
24690 cu_debug_loc_section (struct dwarf2_cu *cu)
24691 {
24692 struct dwarf2_per_objfile *dwarf2_per_objfile
24693 = cu->per_cu->dwarf2_per_objfile;
24694
24695 if (cu->dwo_unit)
24696 {
24697 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24698
24699 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24700 }
24701 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24702 : &dwarf2_per_objfile->loc);
24703 }
24704
24705 /* A helper function that fills in a dwarf2_loclist_baton. */
24706
24707 static void
24708 fill_in_loclist_baton (struct dwarf2_cu *cu,
24709 struct dwarf2_loclist_baton *baton,
24710 const struct attribute *attr)
24711 {
24712 struct dwarf2_per_objfile *dwarf2_per_objfile
24713 = cu->per_cu->dwarf2_per_objfile;
24714 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24715
24716 section->read (dwarf2_per_objfile->objfile);
24717
24718 baton->per_cu = cu->per_cu;
24719 gdb_assert (baton->per_cu);
24720 /* We don't know how long the location list is, but make sure we
24721 don't run off the edge of the section. */
24722 baton->size = section->size - DW_UNSND (attr);
24723 baton->data = section->buffer + DW_UNSND (attr);
24724 baton->base_address = cu->base_address;
24725 baton->from_dwo = cu->dwo_unit != NULL;
24726 }
24727
24728 static void
24729 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24730 struct dwarf2_cu *cu, int is_block)
24731 {
24732 struct dwarf2_per_objfile *dwarf2_per_objfile
24733 = cu->per_cu->dwarf2_per_objfile;
24734 struct objfile *objfile = dwarf2_per_objfile->objfile;
24735 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24736
24737 if (attr->form_is_section_offset ()
24738 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24739 the section. If so, fall through to the complaint in the
24740 other branch. */
24741 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24742 {
24743 struct dwarf2_loclist_baton *baton;
24744
24745 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24746
24747 fill_in_loclist_baton (cu, baton, attr);
24748
24749 if (cu->base_known == 0)
24750 complaint (_("Location list used without "
24751 "specifying the CU base address."));
24752
24753 SYMBOL_ACLASS_INDEX (sym) = (is_block
24754 ? dwarf2_loclist_block_index
24755 : dwarf2_loclist_index);
24756 SYMBOL_LOCATION_BATON (sym) = baton;
24757 }
24758 else
24759 {
24760 struct dwarf2_locexpr_baton *baton;
24761
24762 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24763 baton->per_cu = cu->per_cu;
24764 gdb_assert (baton->per_cu);
24765
24766 if (attr->form_is_block ())
24767 {
24768 /* Note that we're just copying the block's data pointer
24769 here, not the actual data. We're still pointing into the
24770 info_buffer for SYM's objfile; right now we never release
24771 that buffer, but when we do clean up properly this may
24772 need to change. */
24773 baton->size = DW_BLOCK (attr)->size;
24774 baton->data = DW_BLOCK (attr)->data;
24775 }
24776 else
24777 {
24778 dwarf2_invalid_attrib_class_complaint ("location description",
24779 sym->natural_name ());
24780 baton->size = 0;
24781 }
24782
24783 SYMBOL_ACLASS_INDEX (sym) = (is_block
24784 ? dwarf2_locexpr_block_index
24785 : dwarf2_locexpr_index);
24786 SYMBOL_LOCATION_BATON (sym) = baton;
24787 }
24788 }
24789
24790 /* Return the OBJFILE associated with the compilation unit CU. If CU
24791 came from a separate debuginfo file, then the master objfile is
24792 returned. */
24793
24794 struct objfile *
24795 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24796 {
24797 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24798
24799 /* Return the master objfile, so that we can report and look up the
24800 correct file containing this variable. */
24801 if (objfile->separate_debug_objfile_backlink)
24802 objfile = objfile->separate_debug_objfile_backlink;
24803
24804 return objfile;
24805 }
24806
24807 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24808 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24809 CU_HEADERP first. */
24810
24811 static const struct comp_unit_head *
24812 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24813 struct dwarf2_per_cu_data *per_cu)
24814 {
24815 const gdb_byte *info_ptr;
24816
24817 if (per_cu->cu)
24818 return &per_cu->cu->header;
24819
24820 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24821
24822 memset (cu_headerp, 0, sizeof (*cu_headerp));
24823 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24824 rcuh_kind::COMPILE);
24825
24826 return cu_headerp;
24827 }
24828
24829 /* Return the address size given in the compilation unit header for CU. */
24830
24831 int
24832 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24833 {
24834 struct comp_unit_head cu_header_local;
24835 const struct comp_unit_head *cu_headerp;
24836
24837 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24838
24839 return cu_headerp->addr_size;
24840 }
24841
24842 /* Return the offset size given in the compilation unit header for CU. */
24843
24844 int
24845 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24846 {
24847 struct comp_unit_head cu_header_local;
24848 const struct comp_unit_head *cu_headerp;
24849
24850 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24851
24852 return cu_headerp->offset_size;
24853 }
24854
24855 /* See its dwarf2loc.h declaration. */
24856
24857 int
24858 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24859 {
24860 struct comp_unit_head cu_header_local;
24861 const struct comp_unit_head *cu_headerp;
24862
24863 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24864
24865 if (cu_headerp->version == 2)
24866 return cu_headerp->addr_size;
24867 else
24868 return cu_headerp->offset_size;
24869 }
24870
24871 /* Return the text offset of the CU. The returned offset comes from
24872 this CU's objfile. If this objfile came from a separate debuginfo
24873 file, then the offset may be different from the corresponding
24874 offset in the parent objfile. */
24875
24876 CORE_ADDR
24877 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24878 {
24879 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
24880 }
24881
24882 /* Return a type that is a generic pointer type, the size of which matches
24883 the address size given in the compilation unit header for PER_CU. */
24884 static struct type *
24885 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24886 {
24887 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24888 struct type *void_type = objfile_type (objfile)->builtin_void;
24889 struct type *addr_type = lookup_pointer_type (void_type);
24890 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24891
24892 if (TYPE_LENGTH (addr_type) == addr_size)
24893 return addr_type;
24894
24895 addr_type
24896 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24897 return addr_type;
24898 }
24899
24900 /* Return DWARF version number of PER_CU. */
24901
24902 short
24903 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24904 {
24905 return per_cu->dwarf_version;
24906 }
24907
24908 /* Locate the .debug_info compilation unit from CU's objfile which contains
24909 the DIE at OFFSET. Raises an error on failure. */
24910
24911 static struct dwarf2_per_cu_data *
24912 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24913 unsigned int offset_in_dwz,
24914 struct dwarf2_per_objfile *dwarf2_per_objfile)
24915 {
24916 struct dwarf2_per_cu_data *this_cu;
24917 int low, high;
24918
24919 low = 0;
24920 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24921 while (high > low)
24922 {
24923 struct dwarf2_per_cu_data *mid_cu;
24924 int mid = low + (high - low) / 2;
24925
24926 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24927 if (mid_cu->is_dwz > offset_in_dwz
24928 || (mid_cu->is_dwz == offset_in_dwz
24929 && mid_cu->sect_off + mid_cu->length >= sect_off))
24930 high = mid;
24931 else
24932 low = mid + 1;
24933 }
24934 gdb_assert (low == high);
24935 this_cu = dwarf2_per_objfile->all_comp_units[low];
24936 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24937 {
24938 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24939 error (_("Dwarf Error: could not find partial DIE containing "
24940 "offset %s [in module %s]"),
24941 sect_offset_str (sect_off),
24942 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24943
24944 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24945 <= sect_off);
24946 return dwarf2_per_objfile->all_comp_units[low-1];
24947 }
24948 else
24949 {
24950 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24951 && sect_off >= this_cu->sect_off + this_cu->length)
24952 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24953 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24954 return this_cu;
24955 }
24956 }
24957
24958 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24959
24960 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24961 : per_cu (per_cu_),
24962 mark (false),
24963 has_loclist (false),
24964 checked_producer (false),
24965 producer_is_gxx_lt_4_6 (false),
24966 producer_is_gcc_lt_4_3 (false),
24967 producer_is_icc (false),
24968 producer_is_icc_lt_14 (false),
24969 producer_is_codewarrior (false),
24970 processing_has_namespace_info (false)
24971 {
24972 per_cu->cu = this;
24973 }
24974
24975 /* Destroy a dwarf2_cu. */
24976
24977 dwarf2_cu::~dwarf2_cu ()
24978 {
24979 per_cu->cu = NULL;
24980 }
24981
24982 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24983
24984 static void
24985 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24986 enum language pretend_language)
24987 {
24988 struct attribute *attr;
24989
24990 /* Set the language we're debugging. */
24991 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24992 if (attr != nullptr)
24993 set_cu_language (DW_UNSND (attr), cu);
24994 else
24995 {
24996 cu->language = pretend_language;
24997 cu->language_defn = language_def (cu->language);
24998 }
24999
25000 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25001 }
25002
25003 /* Increase the age counter on each cached compilation unit, and free
25004 any that are too old. */
25005
25006 static void
25007 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25008 {
25009 struct dwarf2_per_cu_data *per_cu, **last_chain;
25010
25011 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25012 per_cu = dwarf2_per_objfile->read_in_chain;
25013 while (per_cu != NULL)
25014 {
25015 per_cu->cu->last_used ++;
25016 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25017 dwarf2_mark (per_cu->cu);
25018 per_cu = per_cu->cu->read_in_chain;
25019 }
25020
25021 per_cu = dwarf2_per_objfile->read_in_chain;
25022 last_chain = &dwarf2_per_objfile->read_in_chain;
25023 while (per_cu != NULL)
25024 {
25025 struct dwarf2_per_cu_data *next_cu;
25026
25027 next_cu = per_cu->cu->read_in_chain;
25028
25029 if (!per_cu->cu->mark)
25030 {
25031 delete per_cu->cu;
25032 *last_chain = next_cu;
25033 }
25034 else
25035 last_chain = &per_cu->cu->read_in_chain;
25036
25037 per_cu = next_cu;
25038 }
25039 }
25040
25041 /* Remove a single compilation unit from the cache. */
25042
25043 static void
25044 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25045 {
25046 struct dwarf2_per_cu_data *per_cu, **last_chain;
25047 struct dwarf2_per_objfile *dwarf2_per_objfile
25048 = target_per_cu->dwarf2_per_objfile;
25049
25050 per_cu = dwarf2_per_objfile->read_in_chain;
25051 last_chain = &dwarf2_per_objfile->read_in_chain;
25052 while (per_cu != NULL)
25053 {
25054 struct dwarf2_per_cu_data *next_cu;
25055
25056 next_cu = per_cu->cu->read_in_chain;
25057
25058 if (per_cu == target_per_cu)
25059 {
25060 delete per_cu->cu;
25061 per_cu->cu = NULL;
25062 *last_chain = next_cu;
25063 break;
25064 }
25065 else
25066 last_chain = &per_cu->cu->read_in_chain;
25067
25068 per_cu = next_cu;
25069 }
25070 }
25071
25072 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25073 We store these in a hash table separate from the DIEs, and preserve them
25074 when the DIEs are flushed out of cache.
25075
25076 The CU "per_cu" pointer is needed because offset alone is not enough to
25077 uniquely identify the type. A file may have multiple .debug_types sections,
25078 or the type may come from a DWO file. Furthermore, while it's more logical
25079 to use per_cu->section+offset, with Fission the section with the data is in
25080 the DWO file but we don't know that section at the point we need it.
25081 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25082 because we can enter the lookup routine, get_die_type_at_offset, from
25083 outside this file, and thus won't necessarily have PER_CU->cu.
25084 Fortunately, PER_CU is stable for the life of the objfile. */
25085
25086 struct dwarf2_per_cu_offset_and_type
25087 {
25088 const struct dwarf2_per_cu_data *per_cu;
25089 sect_offset sect_off;
25090 struct type *type;
25091 };
25092
25093 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25094
25095 static hashval_t
25096 per_cu_offset_and_type_hash (const void *item)
25097 {
25098 const struct dwarf2_per_cu_offset_and_type *ofs
25099 = (const struct dwarf2_per_cu_offset_and_type *) item;
25100
25101 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25102 }
25103
25104 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25105
25106 static int
25107 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25108 {
25109 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25110 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25111 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25112 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25113
25114 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25115 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25116 }
25117
25118 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25119 table if necessary. For convenience, return TYPE.
25120
25121 The DIEs reading must have careful ordering to:
25122 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25123 reading current DIE.
25124 * Not trying to dereference contents of still incompletely read in types
25125 while reading in other DIEs.
25126 * Enable referencing still incompletely read in types just by a pointer to
25127 the type without accessing its fields.
25128
25129 Therefore caller should follow these rules:
25130 * Try to fetch any prerequisite types we may need to build this DIE type
25131 before building the type and calling set_die_type.
25132 * After building type call set_die_type for current DIE as soon as
25133 possible before fetching more types to complete the current type.
25134 * Make the type as complete as possible before fetching more types. */
25135
25136 static struct type *
25137 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25138 {
25139 struct dwarf2_per_objfile *dwarf2_per_objfile
25140 = cu->per_cu->dwarf2_per_objfile;
25141 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25142 struct objfile *objfile = dwarf2_per_objfile->objfile;
25143 struct attribute *attr;
25144 struct dynamic_prop prop;
25145
25146 /* For Ada types, make sure that the gnat-specific data is always
25147 initialized (if not already set). There are a few types where
25148 we should not be doing so, because the type-specific area is
25149 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25150 where the type-specific area is used to store the floatformat).
25151 But this is not a problem, because the gnat-specific information
25152 is actually not needed for these types. */
25153 if (need_gnat_info (cu)
25154 && TYPE_CODE (type) != TYPE_CODE_FUNC
25155 && TYPE_CODE (type) != TYPE_CODE_FLT
25156 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25157 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25158 && TYPE_CODE (type) != TYPE_CODE_METHOD
25159 && !HAVE_GNAT_AUX_INFO (type))
25160 INIT_GNAT_SPECIFIC (type);
25161
25162 /* Read DW_AT_allocated and set in type. */
25163 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25164 if (attr != NULL && attr->form_is_block ())
25165 {
25166 struct type *prop_type
25167 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25168 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25169 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25170 }
25171 else if (attr != NULL)
25172 {
25173 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25174 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25175 sect_offset_str (die->sect_off));
25176 }
25177
25178 /* Read DW_AT_associated and set in type. */
25179 attr = dwarf2_attr (die, DW_AT_associated, cu);
25180 if (attr != NULL && attr->form_is_block ())
25181 {
25182 struct type *prop_type
25183 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25184 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25185 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25186 }
25187 else if (attr != NULL)
25188 {
25189 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25190 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25191 sect_offset_str (die->sect_off));
25192 }
25193
25194 /* Read DW_AT_data_location and set in type. */
25195 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25196 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25197 dwarf2_per_cu_addr_type (cu->per_cu)))
25198 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25199
25200 if (dwarf2_per_objfile->die_type_hash == NULL)
25201 {
25202 dwarf2_per_objfile->die_type_hash =
25203 htab_create_alloc_ex (127,
25204 per_cu_offset_and_type_hash,
25205 per_cu_offset_and_type_eq,
25206 NULL,
25207 &objfile->objfile_obstack,
25208 hashtab_obstack_allocate,
25209 dummy_obstack_deallocate);
25210 }
25211
25212 ofs.per_cu = cu->per_cu;
25213 ofs.sect_off = die->sect_off;
25214 ofs.type = type;
25215 slot = (struct dwarf2_per_cu_offset_and_type **)
25216 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25217 if (*slot)
25218 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25219 sect_offset_str (die->sect_off));
25220 *slot = XOBNEW (&objfile->objfile_obstack,
25221 struct dwarf2_per_cu_offset_and_type);
25222 **slot = ofs;
25223 return type;
25224 }
25225
25226 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25227 or return NULL if the die does not have a saved type. */
25228
25229 static struct type *
25230 get_die_type_at_offset (sect_offset sect_off,
25231 struct dwarf2_per_cu_data *per_cu)
25232 {
25233 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25234 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25235
25236 if (dwarf2_per_objfile->die_type_hash == NULL)
25237 return NULL;
25238
25239 ofs.per_cu = per_cu;
25240 ofs.sect_off = sect_off;
25241 slot = ((struct dwarf2_per_cu_offset_and_type *)
25242 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25243 if (slot)
25244 return slot->type;
25245 else
25246 return NULL;
25247 }
25248
25249 /* Look up the type for DIE in CU in die_type_hash,
25250 or return NULL if DIE does not have a saved type. */
25251
25252 static struct type *
25253 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25254 {
25255 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25256 }
25257
25258 /* Add a dependence relationship from CU to REF_PER_CU. */
25259
25260 static void
25261 dwarf2_add_dependence (struct dwarf2_cu *cu,
25262 struct dwarf2_per_cu_data *ref_per_cu)
25263 {
25264 void **slot;
25265
25266 if (cu->dependencies == NULL)
25267 cu->dependencies
25268 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25269 NULL, &cu->comp_unit_obstack,
25270 hashtab_obstack_allocate,
25271 dummy_obstack_deallocate);
25272
25273 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25274 if (*slot == NULL)
25275 *slot = ref_per_cu;
25276 }
25277
25278 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25279 Set the mark field in every compilation unit in the
25280 cache that we must keep because we are keeping CU. */
25281
25282 static int
25283 dwarf2_mark_helper (void **slot, void *data)
25284 {
25285 struct dwarf2_per_cu_data *per_cu;
25286
25287 per_cu = (struct dwarf2_per_cu_data *) *slot;
25288
25289 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25290 reading of the chain. As such dependencies remain valid it is not much
25291 useful to track and undo them during QUIT cleanups. */
25292 if (per_cu->cu == NULL)
25293 return 1;
25294
25295 if (per_cu->cu->mark)
25296 return 1;
25297 per_cu->cu->mark = true;
25298
25299 if (per_cu->cu->dependencies != NULL)
25300 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25301
25302 return 1;
25303 }
25304
25305 /* Set the mark field in CU and in every other compilation unit in the
25306 cache that we must keep because we are keeping CU. */
25307
25308 static void
25309 dwarf2_mark (struct dwarf2_cu *cu)
25310 {
25311 if (cu->mark)
25312 return;
25313 cu->mark = true;
25314 if (cu->dependencies != NULL)
25315 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25316 }
25317
25318 static void
25319 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25320 {
25321 while (per_cu)
25322 {
25323 per_cu->cu->mark = false;
25324 per_cu = per_cu->cu->read_in_chain;
25325 }
25326 }
25327
25328 /* Trivial hash function for partial_die_info: the hash value of a DIE
25329 is its offset in .debug_info for this objfile. */
25330
25331 static hashval_t
25332 partial_die_hash (const void *item)
25333 {
25334 const struct partial_die_info *part_die
25335 = (const struct partial_die_info *) item;
25336
25337 return to_underlying (part_die->sect_off);
25338 }
25339
25340 /* Trivial comparison function for partial_die_info structures: two DIEs
25341 are equal if they have the same offset. */
25342
25343 static int
25344 partial_die_eq (const void *item_lhs, const void *item_rhs)
25345 {
25346 const struct partial_die_info *part_die_lhs
25347 = (const struct partial_die_info *) item_lhs;
25348 const struct partial_die_info *part_die_rhs
25349 = (const struct partial_die_info *) item_rhs;
25350
25351 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25352 }
25353
25354 struct cmd_list_element *set_dwarf_cmdlist;
25355 struct cmd_list_element *show_dwarf_cmdlist;
25356
25357 static void
25358 set_dwarf_cmd (const char *args, int from_tty)
25359 {
25360 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25361 gdb_stdout);
25362 }
25363
25364 static void
25365 show_dwarf_cmd (const char *args, int from_tty)
25366 {
25367 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25368 }
25369
25370 bool dwarf_always_disassemble;
25371
25372 static void
25373 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25374 struct cmd_list_element *c, const char *value)
25375 {
25376 fprintf_filtered (file,
25377 _("Whether to always disassemble "
25378 "DWARF expressions is %s.\n"),
25379 value);
25380 }
25381
25382 static void
25383 show_check_physname (struct ui_file *file, int from_tty,
25384 struct cmd_list_element *c, const char *value)
25385 {
25386 fprintf_filtered (file,
25387 _("Whether to check \"physname\" is %s.\n"),
25388 value);
25389 }
25390
25391 void _initialize_dwarf2_read ();
25392 void
25393 _initialize_dwarf2_read ()
25394 {
25395 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25396 Set DWARF specific variables.\n\
25397 Configure DWARF variables such as the cache size."),
25398 &set_dwarf_cmdlist, "maintenance set dwarf ",
25399 0/*allow-unknown*/, &maintenance_set_cmdlist);
25400
25401 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25402 Show DWARF specific variables.\n\
25403 Show DWARF variables such as the cache size."),
25404 &show_dwarf_cmdlist, "maintenance show dwarf ",
25405 0/*allow-unknown*/, &maintenance_show_cmdlist);
25406
25407 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25408 &dwarf_max_cache_age, _("\
25409 Set the upper bound on the age of cached DWARF compilation units."), _("\
25410 Show the upper bound on the age of cached DWARF compilation units."), _("\
25411 A higher limit means that cached compilation units will be stored\n\
25412 in memory longer, and more total memory will be used. Zero disables\n\
25413 caching, which can slow down startup."),
25414 NULL,
25415 show_dwarf_max_cache_age,
25416 &set_dwarf_cmdlist,
25417 &show_dwarf_cmdlist);
25418
25419 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25420 &dwarf_always_disassemble, _("\
25421 Set whether `info address' always disassembles DWARF expressions."), _("\
25422 Show whether `info address' always disassembles DWARF expressions."), _("\
25423 When enabled, DWARF expressions are always printed in an assembly-like\n\
25424 syntax. When disabled, expressions will be printed in a more\n\
25425 conversational style, when possible."),
25426 NULL,
25427 show_dwarf_always_disassemble,
25428 &set_dwarf_cmdlist,
25429 &show_dwarf_cmdlist);
25430
25431 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25432 Set debugging of the DWARF reader."), _("\
25433 Show debugging of the DWARF reader."), _("\
25434 When enabled (non-zero), debugging messages are printed during DWARF\n\
25435 reading and symtab expansion. A value of 1 (one) provides basic\n\
25436 information. A value greater than 1 provides more verbose information."),
25437 NULL,
25438 NULL,
25439 &setdebuglist, &showdebuglist);
25440
25441 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25442 Set debugging of the DWARF DIE reader."), _("\
25443 Show debugging of the DWARF DIE reader."), _("\
25444 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25445 The value is the maximum depth to print."),
25446 NULL,
25447 NULL,
25448 &setdebuglist, &showdebuglist);
25449
25450 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25451 Set debugging of the dwarf line reader."), _("\
25452 Show debugging of the dwarf line reader."), _("\
25453 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25454 A value of 1 (one) provides basic information.\n\
25455 A value greater than 1 provides more verbose information."),
25456 NULL,
25457 NULL,
25458 &setdebuglist, &showdebuglist);
25459
25460 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25461 Set cross-checking of \"physname\" code against demangler."), _("\
25462 Show cross-checking of \"physname\" code against demangler."), _("\
25463 When enabled, GDB's internal \"physname\" code is checked against\n\
25464 the demangler."),
25465 NULL, show_check_physname,
25466 &setdebuglist, &showdebuglist);
25467
25468 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25469 no_class, &use_deprecated_index_sections, _("\
25470 Set whether to use deprecated gdb_index sections."), _("\
25471 Show whether to use deprecated gdb_index sections."), _("\
25472 When enabled, deprecated .gdb_index sections are used anyway.\n\
25473 Normally they are ignored either because of a missing feature or\n\
25474 performance issue.\n\
25475 Warning: This option must be enabled before gdb reads the file."),
25476 NULL,
25477 NULL,
25478 &setlist, &showlist);
25479
25480 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25481 &dwarf2_locexpr_funcs);
25482 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25483 &dwarf2_loclist_funcs);
25484
25485 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25486 &dwarf2_block_frame_base_locexpr_funcs);
25487 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25488 &dwarf2_block_frame_base_loclist_funcs);
25489
25490 #if GDB_SELF_TEST
25491 selftests::register_test ("dw2_expand_symtabs_matching",
25492 selftests::dw2_expand_symtabs_matching::run_test);
25493 #endif
25494 }
This page took 0.758892 seconds and 4 git commands to generate.