Change attr_form_is_block to be a method
[deliverable/binutils-gdb.git] / gdb / dwarf2read.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 "dwarf2read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf-index-cache.h"
36 #include "dwarf-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 "dwarf2expr.h"
52 #include "dwarf2loc.h"
53 #include "cp-support.h"
54 #include "hashtab.h"
55 #include "command.h"
56 #include "gdbcmd.h"
57 #include "block.h"
58 #include "addrmap.h"
59 #include "typeprint.h"
60 #include "psympriv.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "build-id.h"
70 #include "namespace.h"
71 #include "gdbsupport/function-view.h"
72 #include "gdbsupport/gdb_optional.h"
73 #include "gdbsupport/underlying.h"
74 #include "gdbsupport/hash_enum.h"
75 #include "filename-seen-cache.h"
76 #include "producer.h"
77 #include <fcntl.h>
78 #include <algorithm>
79 #include <unordered_map>
80 #include "gdbsupport/selftest.h"
81 #include "rust-lang.h"
82 #include "gdbsupport/pathstuff.h"
83
84 /* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87 static unsigned int dwarf_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static unsigned int dwarf_die_debug = 0;
91
92 /* When non-zero, dump line number entries as they are read in. */
93 static unsigned int dwarf_line_debug = 0;
94
95 /* When true, cross-check physname against demangler. */
96 static bool check_physname = false;
97
98 /* When true, do not reject deprecated .gdb_index sections. */
99 static bool use_deprecated_index_sections = false;
100
101 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
102
103 /* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105 static int dwarf2_locexpr_index;
106 static int dwarf2_loclist_index;
107 static int dwarf2_locexpr_block_index;
108 static int dwarf2_loclist_block_index;
109
110 /* An index into a (C++) symbol name component in a symbol name as
111 recorded in the mapped_index's symbol table. For each C++ symbol
112 in the symbol table, we record one entry for the start of each
113 component in the symbol in a table of name components, and then
114 sort the table, in order to be able to binary search symbol names,
115 ignoring leading namespaces, both completion and regular look up.
116 For example, for symbol "A::B::C", we'll have an entry that points
117 to "A::B::C", another that points to "B::C", and another for "C".
118 Note that function symbols in GDB index have no parameter
119 information, just the function/method names. You can convert a
120 name_component to a "const char *" using the
121 'mapped_index::symbol_name_at(offset_type)' method. */
122
123 struct name_component
124 {
125 /* Offset in the symbol name where the component starts. Stored as
126 a (32-bit) offset instead of a pointer to save memory and improve
127 locality on 64-bit architectures. */
128 offset_type name_offset;
129
130 /* The symbol's index in the symbol and constant pool tables of a
131 mapped_index. */
132 offset_type idx;
133 };
134
135 /* Base class containing bits shared by both .gdb_index and
136 .debug_name indexes. */
137
138 struct mapped_index_base
139 {
140 mapped_index_base () = default;
141 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
142
143 /* The name_component table (a sorted vector). See name_component's
144 description above. */
145 std::vector<name_component> name_components;
146
147 /* How NAME_COMPONENTS is sorted. */
148 enum case_sensitivity name_components_casing;
149
150 /* Return the number of names in the symbol table. */
151 virtual size_t symbol_name_count () const = 0;
152
153 /* Get the name of the symbol at IDX in the symbol table. */
154 virtual const char *symbol_name_at (offset_type idx) const = 0;
155
156 /* Return whether the name at IDX in the symbol table should be
157 ignored. */
158 virtual bool symbol_name_slot_invalid (offset_type idx) const
159 {
160 return false;
161 }
162
163 /* Build the symbol name component sorted vector, if we haven't
164 yet. */
165 void build_name_components ();
166
167 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
168 possible matches for LN_NO_PARAMS in the name component
169 vector. */
170 std::pair<std::vector<name_component>::const_iterator,
171 std::vector<name_component>::const_iterator>
172 find_name_components_bounds (const lookup_name_info &ln_no_params,
173 enum language lang) const;
174
175 /* Prevent deleting/destroying via a base class pointer. */
176 protected:
177 ~mapped_index_base() = default;
178 };
179
180 /* A description of the mapped index. The file format is described in
181 a comment by the code that writes the index. */
182 struct mapped_index final : public mapped_index_base
183 {
184 /* A slot/bucket in the symbol table hash. */
185 struct symbol_table_slot
186 {
187 const offset_type name;
188 const offset_type vec;
189 };
190
191 /* Index data format version. */
192 int version = 0;
193
194 /* The address table data. */
195 gdb::array_view<const gdb_byte> address_table;
196
197 /* The symbol table, implemented as a hash table. */
198 gdb::array_view<symbol_table_slot> symbol_table;
199
200 /* A pointer to the constant pool. */
201 const char *constant_pool = nullptr;
202
203 bool symbol_name_slot_invalid (offset_type idx) const override
204 {
205 const auto &bucket = this->symbol_table[idx];
206 return bucket.name == 0 && bucket.vec == 0;
207 }
208
209 /* Convenience method to get at the name of the symbol at IDX in the
210 symbol table. */
211 const char *symbol_name_at (offset_type idx) const override
212 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
213
214 size_t symbol_name_count () const override
215 { return this->symbol_table.size (); }
216 };
217
218 /* A description of the mapped .debug_names.
219 Uninitialized map has CU_COUNT 0. */
220 struct mapped_debug_names final : public mapped_index_base
221 {
222 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
223 : dwarf2_per_objfile (dwarf2_per_objfile_)
224 {}
225
226 struct dwarf2_per_objfile *dwarf2_per_objfile;
227 bfd_endian dwarf5_byte_order;
228 bool dwarf5_is_dwarf64;
229 bool augmentation_is_gdb;
230 uint8_t offset_size;
231 uint32_t cu_count = 0;
232 uint32_t tu_count, bucket_count, name_count;
233 const gdb_byte *cu_table_reordered, *tu_table_reordered;
234 const uint32_t *bucket_table_reordered, *hash_table_reordered;
235 const gdb_byte *name_table_string_offs_reordered;
236 const gdb_byte *name_table_entry_offs_reordered;
237 const gdb_byte *entry_pool;
238
239 struct index_val
240 {
241 ULONGEST dwarf_tag;
242 struct attr
243 {
244 /* Attribute name DW_IDX_*. */
245 ULONGEST dw_idx;
246
247 /* Attribute form DW_FORM_*. */
248 ULONGEST form;
249
250 /* Value if FORM is DW_FORM_implicit_const. */
251 LONGEST implicit_const;
252 };
253 std::vector<attr> attr_vec;
254 };
255
256 std::unordered_map<ULONGEST, index_val> abbrev_map;
257
258 const char *namei_to_name (uint32_t namei) const;
259
260 /* Implementation of the mapped_index_base virtual interface, for
261 the name_components cache. */
262
263 const char *symbol_name_at (offset_type idx) const override
264 { return namei_to_name (idx); }
265
266 size_t symbol_name_count () const override
267 { return this->name_count; }
268 };
269
270 /* See dwarf2read.h. */
271
272 dwarf2_per_objfile *
273 get_dwarf2_per_objfile (struct objfile *objfile)
274 {
275 return dwarf2_objfile_data_key.get (objfile);
276 }
277
278 /* Default names of the debugging sections. */
279
280 /* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
283 static const struct dwarf2_debug_sections dwarf2_elf_names =
284 {
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_loclists", ".zdebug_loclists" },
290 { ".debug_macinfo", ".zdebug_macinfo" },
291 { ".debug_macro", ".zdebug_macro" },
292 { ".debug_str", ".zdebug_str" },
293 { ".debug_str_offsets", ".zdebug_str_offsets" },
294 { ".debug_line_str", ".zdebug_line_str" },
295 { ".debug_ranges", ".zdebug_ranges" },
296 { ".debug_rnglists", ".zdebug_rnglists" },
297 { ".debug_types", ".zdebug_types" },
298 { ".debug_addr", ".zdebug_addr" },
299 { ".debug_frame", ".zdebug_frame" },
300 { ".eh_frame", NULL },
301 { ".gdb_index", ".zgdb_index" },
302 { ".debug_names", ".zdebug_names" },
303 { ".debug_aranges", ".zdebug_aranges" },
304 23
305 };
306
307 /* List of DWO/DWP sections. */
308
309 static const struct dwop_section_names
310 {
311 struct dwarf2_section_names abbrev_dwo;
312 struct dwarf2_section_names info_dwo;
313 struct dwarf2_section_names line_dwo;
314 struct dwarf2_section_names loc_dwo;
315 struct dwarf2_section_names loclists_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
338 };
339
340 /* local data types */
341
342 /* The data in a compilation unit header, after target2host
343 translation, looks like this. */
344 struct comp_unit_head
345 {
346 unsigned int length;
347 short version;
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
350 sect_offset abbrev_sect_off;
351
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
354
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
357
358 enum dwarf_unit_type unit_type;
359
360 /* Offset to the first byte of this compilation unit header in the
361 .debug_info section, for resolving relative reference dies. */
362 sect_offset sect_off;
363
364 /* Offset to first die in this cu from the start of the cu.
365 This will be the first byte following the compilation unit header. */
366 cu_offset first_die_cu_offset;
367
368
369 /* 64-bit signature of this unit. For type units, it denotes the signature of
370 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
371 Also used in DWARF 5, to denote the dwo id when the unit type is
372 DW_UT_skeleton or DW_UT_split_compile. */
373 ULONGEST signature;
374
375 /* For types, offset in the type's DIE of the type defined by this TU. */
376 cu_offset type_cu_offset_in_tu;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
428
429 /* Non-zero if base_address has been set. */
430 int base_known = 0;
431
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
435
436 const char *producer = nullptr;
437
438 private:
439 /* The symtab builder for this CU. This is only non-NULL when full
440 symbols are being read. */
441 std::unique_ptr<buildsym_compunit> m_builder;
442
443 public:
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
453 struct pending **list_in_scope = nullptr;
454
455 /* Hash table holding all the loaded partial DIEs
456 with partial_die->offset.SECT_OFF as hash. */
457 htab_t partial_dies = nullptr;
458
459 /* Storage for things with the same lifetime as this read-in compilation
460 unit, including partial DIEs. */
461 auto_obstack comp_unit_obstack;
462
463 /* When multiple dwarf2_cu structures are living in memory, this field
464 chains them all together, so that they can be released efficiently.
465 We will probably also want a generation counter so that most-recently-used
466 compilation units are cached... */
467 struct dwarf2_per_cu_data *read_in_chain = nullptr;
468
469 /* Backlink to our per_cu entry. */
470 struct dwarf2_per_cu_data *per_cu;
471
472 /* How many compilation units ago was this CU last referenced? */
473 int last_used = 0;
474
475 /* A hash table of DIE cu_offset for following references with
476 die_info->offset.sect_off as hash. */
477 htab_t die_hash = nullptr;
478
479 /* Full DIEs if read in. */
480 struct die_info *dies = nullptr;
481
482 /* A set of pointers to dwarf2_per_cu_data objects for compilation
483 units referenced by this one. Only set during full symbol processing;
484 partial symbol tables do not have dependencies. */
485 htab_t dependencies = nullptr;
486
487 /* Header data from the line table, during full symbol processing. */
488 struct line_header *line_header = nullptr;
489 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
490 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
491 this is the DW_TAG_compile_unit die for this CU. We'll hold on
492 to the line header as long as this DIE is being processed. See
493 process_die_scope. */
494 die_info *line_header_die_owner = nullptr;
495
496 /* A list of methods which need to have physnames computed
497 after all type information has been read. */
498 std::vector<delayed_method_info> method_list;
499
500 /* To be copied to symtab->call_site_htab. */
501 htab_t call_site_htab = nullptr;
502
503 /* Non-NULL if this CU came from a DWO file.
504 There is an invariant here that is important to remember:
505 Except for attributes copied from the top level DIE in the "main"
506 (or "stub") file in preparation for reading the DWO file
507 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
508 Either there isn't a DWO file (in which case this is NULL and the point
509 is moot), or there is and either we're not going to read it (in which
510 case this is NULL) or there is and we are reading it (in which case this
511 is non-NULL). */
512 struct dwo_unit *dwo_unit = nullptr;
513
514 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
515 Note this value comes from the Fission stub CU/TU's DIE. */
516 gdb::optional<ULONGEST> addr_base;
517
518 /* The DW_AT_rnglists_base attribute if present.
519 Note this value comes from the Fission stub CU/TU's DIE.
520 Also note that the value is zero in the non-DWO case so this value can
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
527 ULONGEST ranges_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab.
611 This PER_CU is recognized by having no section. */
612 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
613 struct dwarf2_per_cu_data per_cu;
614
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
618 std::vector<signatured_type *> *tus;
619
620 /* The compunit symtab.
621 Type units in a group needn't all be defined in the same source file,
622 so we create an essentially anonymous symtab as the compunit symtab. */
623 struct compunit_symtab *compunit_symtab;
624
625 /* The data used to construct the hash key. */
626 struct stmt_list_hash hash;
627
628 /* The number of symtabs from the line header.
629 The value here must match line_header.num_file_names. */
630 unsigned int num_symtabs;
631
632 /* The symbol tables for this TU (obtained from the files listed in
633 DW_AT_stmt_list).
634 WARNING: The order of entries here must match the order of entries
635 in the line header. After the first TU using this type_unit_group, the
636 line header for the subsequent TUs is recreated from this. This is done
637 because we need to use the same symtabs for each TU using the same
638 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
639 there's no guarantee the line header doesn't have duplicate entries. */
640 struct symtab **symtabs;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_t cus {};
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_t tus {};
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_t loaded_cus {};
872 htab_t loaded_tus {};
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The value of the DW_AT_comp_dir attribute. */
907 const char *comp_dir;
908
909 /* The abbreviation table to use when reading the DIEs. */
910 struct abbrev_table *abbrev_table;
911 };
912
913 /* A subclass of die_reader_specs that holds storage and has complex
914 constructor and destructor behavior. */
915
916 class cutu_reader : public die_reader_specs
917 {
918 public:
919
920 cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct abbrev_table *abbrev_table,
922 int use_existing_cu, int keep,
923 bool skip_partial);
924
925 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
926 struct dwarf2_cu *parent_cu = nullptr,
927 struct dwo_file *dwo_file = nullptr);
928
929 ~cutu_reader ();
930
931 DISABLE_COPY_AND_ASSIGN (cutu_reader);
932
933 const gdb_byte *info_ptr = nullptr;
934 struct die_info *comp_unit_die = nullptr;
935 int has_children = 0;
936 bool dummy_p = false;
937
938 private:
939 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
940 int use_existing_cu, int keep);
941
942 struct dwarf2_per_cu_data *m_this_cu;
943 int m_keep = 0;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
954 later. */
955 typedef int dir_index;
956
957 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
958 and later. */
959 typedef int file_name_index;
960
961 struct file_entry
962 {
963 file_entry () = default;
964
965 file_entry (const char *name_, dir_index d_index_,
966 unsigned int mod_time_, unsigned int length_)
967 : name (name_),
968 d_index (d_index_),
969 mod_time (mod_time_),
970 length (length_)
971 {}
972
973 /* Return the include directory at D_INDEX stored in LH. Returns
974 NULL if D_INDEX is out of bounds. */
975 const char *include_dir (const line_header *lh) const;
976
977 /* The file name. Note this is an observing pointer. The memory is
978 owned by debug_line_buffer. */
979 const char *name {};
980
981 /* The directory index (1-based). */
982 dir_index d_index {};
983
984 unsigned int mod_time {};
985
986 unsigned int length {};
987
988 /* True if referenced by the Line Number Program. */
989 bool included_p {};
990
991 /* The associated symbol table, if any. */
992 struct symtab *symtab {};
993 };
994
995 /* The line number information for a compilation unit (found in the
996 .debug_line section) begins with a "statement program header",
997 which contains the following information. */
998 struct line_header
999 {
1000 line_header ()
1001 : offset_in_dwz {}
1002 {}
1003
1004 /* Add an entry to the include directory table. */
1005 void add_include_dir (const char *include_dir);
1006
1007 /* Add an entry to the file name table. */
1008 void add_file_name (const char *name, dir_index d_index,
1009 unsigned int mod_time, unsigned int length);
1010
1011 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1012 Returns NULL if INDEX is out of bounds. */
1013 const char *include_dir_at (dir_index index) const
1014 {
1015 int vec_index;
1016 if (version >= 5)
1017 vec_index = index;
1018 else
1019 vec_index = index - 1;
1020 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1021 return NULL;
1022 return m_include_dirs[vec_index];
1023 }
1024
1025 bool is_valid_file_index (int file_index)
1026 {
1027 if (version >= 5)
1028 return 0 <= file_index && file_index < file_names_size ();
1029 return 1 <= file_index && file_index <= file_names_size ();
1030 }
1031
1032 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1033 Returns NULL if INDEX is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 int vec_index;
1037 if (version >= 5)
1038 vec_index = index;
1039 else
1040 vec_index = index - 1;
1041 if (vec_index < 0 || vec_index >= m_file_names.size ())
1042 return NULL;
1043 return &m_file_names[vec_index];
1044 }
1045
1046 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1047 this method should only be used to iterate through all file entries in an
1048 index-agnostic manner. */
1049 std::vector<file_entry> &file_names ()
1050 { return m_file_names; }
1051
1052 /* Offset of line number information in .debug_line section. */
1053 sect_offset sect_off {};
1054
1055 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1056 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1057
1058 unsigned int total_length {};
1059 unsigned short version {};
1060 unsigned int header_length {};
1061 unsigned char minimum_instruction_length {};
1062 unsigned char maximum_ops_per_instruction {};
1063 unsigned char default_is_stmt {};
1064 int line_base {};
1065 unsigned char line_range {};
1066 unsigned char opcode_base {};
1067
1068 /* standard_opcode_lengths[i] is the number of operands for the
1069 standard opcode whose value is i. This means that
1070 standard_opcode_lengths[0] is unused, and the last meaningful
1071 element is standard_opcode_lengths[opcode_base - 1]. */
1072 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1073
1074 int file_names_size ()
1075 { return m_file_names.size(); }
1076
1077 /* The start and end of the statement program following this
1078 header. These point into dwarf2_per_objfile->line_buffer. */
1079 const gdb_byte *statement_program_start {}, *statement_program_end {};
1080
1081 private:
1082 /* The include_directories table. Note these are observing
1083 pointers. The memory is owned by debug_line_buffer. */
1084 std::vector<const char *> m_include_dirs;
1085
1086 /* The file_names table. This is private because the meaning of indexes
1087 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1088 before, and is 0 in DWARF 5 and later). So the client should use
1089 file_name_at method for access. */
1090 std::vector<file_entry> m_file_names;
1091 };
1092
1093 typedef std::unique_ptr<line_header> line_header_up;
1094
1095 const char *
1096 file_entry::include_dir (const line_header *lh) const
1097 {
1098 return lh->include_dir_at (d_index);
1099 }
1100
1101 /* When we construct a partial symbol table entry we only
1102 need this much information. */
1103 struct partial_die_info : public allocate_on_obstack
1104 {
1105 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1106
1107 /* Disable assign but still keep copy ctor, which is needed
1108 load_partial_dies. */
1109 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1110
1111 /* Adjust the partial die before generating a symbol for it. This
1112 function may set the is_external flag or change the DIE's
1113 name. */
1114 void fixup (struct dwarf2_cu *cu);
1115
1116 /* Read a minimal amount of information into the minimal die
1117 structure. */
1118 const gdb_byte *read (const struct die_reader_specs *reader,
1119 const struct abbrev_info &abbrev,
1120 const gdb_byte *info_ptr);
1121
1122 /* Offset of this DIE. */
1123 const sect_offset sect_off;
1124
1125 /* DWARF-2 tag for this DIE. */
1126 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1127
1128 /* Assorted flags describing the data found in this DIE. */
1129 const unsigned int has_children : 1;
1130
1131 unsigned int is_external : 1;
1132 unsigned int is_declaration : 1;
1133 unsigned int has_type : 1;
1134 unsigned int has_specification : 1;
1135 unsigned int has_pc_info : 1;
1136 unsigned int may_be_inlined : 1;
1137
1138 /* This DIE has been marked DW_AT_main_subprogram. */
1139 unsigned int main_subprogram : 1;
1140
1141 /* Flag set if the SCOPE field of this structure has been
1142 computed. */
1143 unsigned int scope_set : 1;
1144
1145 /* Flag set if the DIE has a byte_size attribute. */
1146 unsigned int has_byte_size : 1;
1147
1148 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1149 unsigned int has_const_value : 1;
1150
1151 /* Flag set if any of the DIE's children are template arguments. */
1152 unsigned int has_template_arguments : 1;
1153
1154 /* Flag set if fixup has been called on this die. */
1155 unsigned int fixup_called : 1;
1156
1157 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1158 unsigned int is_dwz : 1;
1159
1160 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1161 unsigned int spec_is_dwz : 1;
1162
1163 /* The name of this DIE. Normally the value of DW_AT_name, but
1164 sometimes a default name for unnamed DIEs. */
1165 const char *name = nullptr;
1166
1167 /* The linkage name, if present. */
1168 const char *linkage_name = nullptr;
1169
1170 /* The scope to prepend to our children. This is generally
1171 allocated on the comp_unit_obstack, so will disappear
1172 when this compilation unit leaves the cache. */
1173 const char *scope = nullptr;
1174
1175 /* Some data associated with the partial DIE. The tag determines
1176 which field is live. */
1177 union
1178 {
1179 /* The location description associated with this DIE, if any. */
1180 struct dwarf_block *locdesc;
1181 /* The offset of an import, for DW_TAG_imported_unit. */
1182 sect_offset sect_off;
1183 } d {};
1184
1185 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1186 CORE_ADDR lowpc = 0;
1187 CORE_ADDR highpc = 0;
1188
1189 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1190 DW_AT_sibling, if any. */
1191 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1192 could return DW_AT_sibling values to its caller load_partial_dies. */
1193 const gdb_byte *sibling = nullptr;
1194
1195 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1196 DW_AT_specification (or DW_AT_abstract_origin or
1197 DW_AT_extension). */
1198 sect_offset spec_offset {};
1199
1200 /* Pointers to this DIE's parent, first child, and next sibling,
1201 if any. */
1202 struct partial_die_info *die_parent = nullptr;
1203 struct partial_die_info *die_child = nullptr;
1204 struct partial_die_info *die_sibling = nullptr;
1205
1206 friend struct partial_die_info *
1207 dwarf2_cu::find_partial_die (sect_offset sect_off);
1208
1209 private:
1210 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1211 partial_die_info (sect_offset sect_off)
1212 : partial_die_info (sect_off, DW_TAG_padding, 0)
1213 {
1214 }
1215
1216 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1217 int has_children_)
1218 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1219 {
1220 is_external = 0;
1221 is_declaration = 0;
1222 has_type = 0;
1223 has_specification = 0;
1224 has_pc_info = 0;
1225 may_be_inlined = 0;
1226 main_subprogram = 0;
1227 scope_set = 0;
1228 has_byte_size = 0;
1229 has_const_value = 0;
1230 has_template_arguments = 0;
1231 fixup_called = 0;
1232 is_dwz = 0;
1233 spec_is_dwz = 0;
1234 }
1235 };
1236
1237 /* This data structure holds a complete die structure. */
1238 struct die_info
1239 {
1240 /* DWARF-2 tag for this DIE. */
1241 ENUM_BITFIELD(dwarf_tag) tag : 16;
1242
1243 /* Number of attributes */
1244 unsigned char num_attrs;
1245
1246 /* True if we're presently building the full type name for the
1247 type derived from this DIE. */
1248 unsigned char building_fullname : 1;
1249
1250 /* True if this die is in process. PR 16581. */
1251 unsigned char in_process : 1;
1252
1253 /* Abbrev number */
1254 unsigned int abbrev;
1255
1256 /* Offset in .debug_info or .debug_types section. */
1257 sect_offset sect_off;
1258
1259 /* The dies in a compilation unit form an n-ary tree. PARENT
1260 points to this die's parent; CHILD points to the first child of
1261 this node; and all the children of a given node are chained
1262 together via their SIBLING fields. */
1263 struct die_info *child; /* Its first child, if any. */
1264 struct die_info *sibling; /* Its next sibling, if any. */
1265 struct die_info *parent; /* Its parent, if any. */
1266
1267 /* An array of attributes, with NUM_ATTRS elements. There may be
1268 zero, but it's not common and zero-sized arrays are not
1269 sufficiently portable C. */
1270 struct attribute attrs[1];
1271 };
1272
1273 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1274 but this would require a corresponding change in unpack_field_as_long
1275 and friends. */
1276 static int bits_per_byte = 8;
1277
1278 /* When reading a variant or variant part, we track a bit more
1279 information about the field, and store it in an object of this
1280 type. */
1281
1282 struct variant_field
1283 {
1284 /* If we see a DW_TAG_variant, then this will be the discriminant
1285 value. */
1286 ULONGEST discriminant_value;
1287 /* If we see a DW_TAG_variant, then this will be set if this is the
1288 default branch. */
1289 bool default_branch;
1290 /* While reading a DW_TAG_variant_part, this will be set if this
1291 field is the discriminant. */
1292 bool is_discriminant;
1293 };
1294
1295 struct nextfield
1296 {
1297 int accessibility = 0;
1298 int virtuality = 0;
1299 /* Extra information to describe a variant or variant part. */
1300 struct variant_field variant {};
1301 struct field field {};
1302 };
1303
1304 struct fnfieldlist
1305 {
1306 const char *name = nullptr;
1307 std::vector<struct fn_field> fnfields;
1308 };
1309
1310 /* The routines that read and process dies for a C struct or C++ class
1311 pass lists of data member fields and lists of member function fields
1312 in an instance of a field_info structure, as defined below. */
1313 struct field_info
1314 {
1315 /* List of data member and baseclasses fields. */
1316 std::vector<struct nextfield> fields;
1317 std::vector<struct nextfield> baseclasses;
1318
1319 /* Number of fields (including baseclasses). */
1320 int nfields = 0;
1321
1322 /* Set if the accessibility of one of the fields is not public. */
1323 int non_public_fields = 0;
1324
1325 /* Member function fieldlist array, contains name of possibly overloaded
1326 member function, number of overloaded member functions and a pointer
1327 to the head of the member function field chain. */
1328 std::vector<struct fnfieldlist> fnfieldlists;
1329
1330 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1331 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1332 std::vector<struct decl_field> typedef_field_list;
1333
1334 /* Nested types defined by this class and the number of elements in this
1335 list. */
1336 std::vector<struct decl_field> nested_types_list;
1337 };
1338
1339 /* One item on the queue of compilation units to read in full symbols
1340 for. */
1341 struct dwarf2_queue_item
1342 {
1343 struct dwarf2_per_cu_data *per_cu;
1344 enum language pretend_language;
1345 struct dwarf2_queue_item *next;
1346 };
1347
1348 /* The current queue. */
1349 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1350
1351 /* Loaded secondary compilation units are kept in memory until they
1352 have not been referenced for the processing of this many
1353 compilation units. Set this to zero to disable caching. Cache
1354 sizes of up to at least twenty will improve startup time for
1355 typical inter-CU-reference binaries, at an obvious memory cost. */
1356 static int dwarf_max_cache_age = 5;
1357 static void
1358 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1359 struct cmd_list_element *c, const char *value)
1360 {
1361 fprintf_filtered (file, _("The upper bound on the age of cached "
1362 "DWARF compilation units is %s.\n"),
1363 value);
1364 }
1365 \f
1366 /* local function prototypes */
1367
1368 static void dwarf2_find_base_address (struct die_info *die,
1369 struct dwarf2_cu *cu);
1370
1371 static dwarf2_psymtab *create_partial_symtab
1372 (struct dwarf2_per_cu_data *per_cu, const char *name);
1373
1374 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1375 const gdb_byte *info_ptr,
1376 struct die_info *type_unit_die,
1377 int has_children);
1378
1379 static void dwarf2_build_psymtabs_hard
1380 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1381
1382 static void scan_partial_symbols (struct partial_die_info *,
1383 CORE_ADDR *, CORE_ADDR *,
1384 int, struct dwarf2_cu *);
1385
1386 static void add_partial_symbol (struct partial_die_info *,
1387 struct dwarf2_cu *);
1388
1389 static void add_partial_namespace (struct partial_die_info *pdi,
1390 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1391 int set_addrmap, struct dwarf2_cu *cu);
1392
1393 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1394 CORE_ADDR *highpc, int set_addrmap,
1395 struct dwarf2_cu *cu);
1396
1397 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1398 struct dwarf2_cu *cu);
1399
1400 static void add_partial_subprogram (struct partial_die_info *pdi,
1401 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1402 int need_pc, struct dwarf2_cu *cu);
1403
1404 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1405
1406 static struct partial_die_info *load_partial_dies
1407 (const struct die_reader_specs *, const gdb_byte *, int);
1408
1409 /* A pair of partial_die_info and compilation unit. */
1410 struct cu_partial_die_info
1411 {
1412 /* The compilation unit of the partial_die_info. */
1413 struct dwarf2_cu *cu;
1414 /* A partial_die_info. */
1415 struct partial_die_info *pdi;
1416
1417 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1418 : cu (cu),
1419 pdi (pdi)
1420 { /* Nothing. */ }
1421
1422 private:
1423 cu_partial_die_info () = delete;
1424 };
1425
1426 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1427 struct dwarf2_cu *);
1428
1429 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1430 struct attribute *, struct attr_abbrev *,
1431 const gdb_byte *, bool *need_reprocess);
1432
1433 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1434 struct attribute *attr);
1435
1436 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1437
1438 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1439 unsigned int *);
1440
1441 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1442
1443 static LONGEST read_checked_initial_length_and_offset
1444 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1445 unsigned int *, unsigned int *);
1446
1447 static LONGEST read_offset (bfd *, const gdb_byte *,
1448 const struct comp_unit_head *,
1449 unsigned int *);
1450
1451 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1452
1453 static sect_offset read_abbrev_offset
1454 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1455 struct dwarf2_section_info *, sect_offset);
1456
1457 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1458
1459 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1460
1461 static const char *read_indirect_string
1462 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1463 const struct comp_unit_head *, unsigned int *);
1464
1465 static const char *read_indirect_line_string
1466 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1467 const struct comp_unit_head *, unsigned int *);
1468
1469 static const char *read_indirect_string_at_offset
1470 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1471 LONGEST str_offset);
1472
1473 static const char *read_indirect_string_from_dwz
1474 (struct objfile *objfile, struct dwz_file *, LONGEST);
1475
1476 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1477 const gdb_byte *,
1478 unsigned int *);
1479
1480 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1481 ULONGEST str_index);
1482
1483 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1484 ULONGEST str_index);
1485
1486 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1487
1488 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1489 struct dwarf2_cu *);
1490
1491 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1492 unsigned int);
1493
1494 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1495 struct dwarf2_cu *cu);
1496
1497 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1498
1499 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1500 struct dwarf2_cu *cu);
1501
1502 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1503
1504 static struct die_info *die_specification (struct die_info *die,
1505 struct dwarf2_cu **);
1506
1507 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1508 struct dwarf2_cu *cu);
1509
1510 static void dwarf_decode_lines (struct line_header *, const char *,
1511 struct dwarf2_cu *, dwarf2_psymtab *,
1512 CORE_ADDR, int decode_mapping);
1513
1514 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1515 const char *);
1516
1517 static struct symbol *new_symbol (struct die_info *, struct type *,
1518 struct dwarf2_cu *, struct symbol * = NULL);
1519
1520 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1521 struct dwarf2_cu *);
1522
1523 static void dwarf2_const_value_attr (const struct attribute *attr,
1524 struct type *type,
1525 const char *name,
1526 struct obstack *obstack,
1527 struct dwarf2_cu *cu, LONGEST *value,
1528 const gdb_byte **bytes,
1529 struct dwarf2_locexpr_baton **baton);
1530
1531 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1532
1533 static int need_gnat_info (struct dwarf2_cu *);
1534
1535 static struct type *die_descriptive_type (struct die_info *,
1536 struct dwarf2_cu *);
1537
1538 static void set_descriptive_type (struct type *, struct die_info *,
1539 struct dwarf2_cu *);
1540
1541 static struct type *die_containing_type (struct die_info *,
1542 struct dwarf2_cu *);
1543
1544 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1545 struct dwarf2_cu *);
1546
1547 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1548
1549 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1550
1551 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1552
1553 static char *typename_concat (struct obstack *obs, const char *prefix,
1554 const char *suffix, int physname,
1555 struct dwarf2_cu *cu);
1556
1557 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1558
1559 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1560
1561 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1562
1563 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1564
1565 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1566
1567 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1568
1569 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1570 struct dwarf2_cu *, dwarf2_psymtab *);
1571
1572 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1573 values. Keep the items ordered with increasing constraints compliance. */
1574 enum pc_bounds_kind
1575 {
1576 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1577 PC_BOUNDS_NOT_PRESENT,
1578
1579 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1580 were present but they do not form a valid range of PC addresses. */
1581 PC_BOUNDS_INVALID,
1582
1583 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1584 PC_BOUNDS_RANGES,
1585
1586 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1587 PC_BOUNDS_HIGH_LOW,
1588 };
1589
1590 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1591 CORE_ADDR *, CORE_ADDR *,
1592 struct dwarf2_cu *,
1593 dwarf2_psymtab *);
1594
1595 static void get_scope_pc_bounds (struct die_info *,
1596 CORE_ADDR *, CORE_ADDR *,
1597 struct dwarf2_cu *);
1598
1599 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1600 CORE_ADDR, struct dwarf2_cu *);
1601
1602 static void dwarf2_add_field (struct field_info *, struct die_info *,
1603 struct dwarf2_cu *);
1604
1605 static void dwarf2_attach_fields_to_type (struct field_info *,
1606 struct type *, struct dwarf2_cu *);
1607
1608 static void dwarf2_add_member_fn (struct field_info *,
1609 struct die_info *, struct type *,
1610 struct dwarf2_cu *);
1611
1612 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1613 struct type *,
1614 struct dwarf2_cu *);
1615
1616 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1617
1618 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1619
1620 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1621
1622 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1623
1624 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1625
1626 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1627
1628 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1629
1630 static struct type *read_module_type (struct die_info *die,
1631 struct dwarf2_cu *cu);
1632
1633 static const char *namespace_name (struct die_info *die,
1634 int *is_anonymous, struct dwarf2_cu *);
1635
1636 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1637
1638 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1639
1640 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1641 struct dwarf2_cu *);
1642
1643 static struct die_info *read_die_and_siblings_1
1644 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1645 struct die_info *);
1646
1647 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1648 const gdb_byte *info_ptr,
1649 const gdb_byte **new_info_ptr,
1650 struct die_info *parent);
1651
1652 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1653 struct die_info **, const gdb_byte *,
1654 int *, int);
1655
1656 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1657 struct die_info **, const gdb_byte *,
1658 int *);
1659
1660 static void process_die (struct die_info *, struct dwarf2_cu *);
1661
1662 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1663 struct obstack *);
1664
1665 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1666
1667 static const char *dwarf2_full_name (const char *name,
1668 struct die_info *die,
1669 struct dwarf2_cu *cu);
1670
1671 static const char *dwarf2_physname (const char *name, struct die_info *die,
1672 struct dwarf2_cu *cu);
1673
1674 static struct die_info *dwarf2_extension (struct die_info *die,
1675 struct dwarf2_cu **);
1676
1677 static const char *dwarf_tag_name (unsigned int);
1678
1679 static const char *dwarf_attr_name (unsigned int);
1680
1681 static const char *dwarf_unit_type_name (int unit_type);
1682
1683 static const char *dwarf_form_name (unsigned int);
1684
1685 static const char *dwarf_bool_name (unsigned int);
1686
1687 static const char *dwarf_type_encoding_name (unsigned int);
1688
1689 static struct die_info *sibling_die (struct die_info *);
1690
1691 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1692
1693 static void dump_die_for_error (struct die_info *);
1694
1695 static void dump_die_1 (struct ui_file *, int level, int max_level,
1696 struct die_info *);
1697
1698 /*static*/ void dump_die (struct die_info *, int max_level);
1699
1700 static void store_in_ref_table (struct die_info *,
1701 struct dwarf2_cu *);
1702
1703 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1704
1705 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1706
1707 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1708 const struct attribute *,
1709 struct dwarf2_cu **);
1710
1711 static struct die_info *follow_die_ref (struct die_info *,
1712 const struct attribute *,
1713 struct dwarf2_cu **);
1714
1715 static struct die_info *follow_die_sig (struct die_info *,
1716 const struct attribute *,
1717 struct dwarf2_cu **);
1718
1719 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1720 struct dwarf2_cu *);
1721
1722 static struct type *get_DW_AT_signature_type (struct die_info *,
1723 const struct attribute *,
1724 struct dwarf2_cu *);
1725
1726 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1727
1728 static void read_signatured_type (struct signatured_type *);
1729
1730 static int attr_to_dynamic_prop (const struct attribute *attr,
1731 struct die_info *die, struct dwarf2_cu *cu,
1732 struct dynamic_prop *prop, struct type *type);
1733
1734 /* memory allocation interface */
1735
1736 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1737
1738 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1739
1740 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1741
1742 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1743 struct dwarf2_loclist_baton *baton,
1744 const struct attribute *attr);
1745
1746 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1747 struct symbol *sym,
1748 struct dwarf2_cu *cu,
1749 int is_block);
1750
1751 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1752 const gdb_byte *info_ptr,
1753 struct abbrev_info *abbrev);
1754
1755 static hashval_t partial_die_hash (const void *item);
1756
1757 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1758
1759 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1760 (sect_offset sect_off, unsigned int offset_in_dwz,
1761 struct dwarf2_per_objfile *dwarf2_per_objfile);
1762
1763 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1764 struct die_info *comp_unit_die,
1765 enum language pretend_language);
1766
1767 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1768
1769 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1770
1771 static struct type *set_die_type (struct die_info *, struct type *,
1772 struct dwarf2_cu *);
1773
1774 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1775
1776 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1777
1778 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1779 enum language);
1780
1781 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1782 enum language);
1783
1784 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1785 enum language);
1786
1787 static void dwarf2_add_dependence (struct dwarf2_cu *,
1788 struct dwarf2_per_cu_data *);
1789
1790 static void dwarf2_mark (struct dwarf2_cu *);
1791
1792 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1793
1794 static struct type *get_die_type_at_offset (sect_offset,
1795 struct dwarf2_per_cu_data *);
1796
1797 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1798
1799 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1800 enum language pretend_language);
1801
1802 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1803
1804 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1805 static struct type *dwarf2_per_cu_addr_sized_int_type
1806 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1807 static struct type *dwarf2_per_cu_int_type
1808 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1809 bool unsigned_p);
1810
1811 /* Class, the destructor of which frees all allocated queue entries. This
1812 will only have work to do if an error was thrown while processing the
1813 dwarf. If no error was thrown then the queue entries should have all
1814 been processed, and freed, as we went along. */
1815
1816 class dwarf2_queue_guard
1817 {
1818 public:
1819 dwarf2_queue_guard () = default;
1820
1821 /* Free any entries remaining on the queue. There should only be
1822 entries left if we hit an error while processing the dwarf. */
1823 ~dwarf2_queue_guard ()
1824 {
1825 struct dwarf2_queue_item *item, *last;
1826
1827 item = dwarf2_queue;
1828 while (item)
1829 {
1830 /* Anything still marked queued is likely to be in an
1831 inconsistent state, so discard it. */
1832 if (item->per_cu->queued)
1833 {
1834 if (item->per_cu->cu != NULL)
1835 free_one_cached_comp_unit (item->per_cu);
1836 item->per_cu->queued = 0;
1837 }
1838
1839 last = item;
1840 item = item->next;
1841 xfree (last);
1842 }
1843
1844 dwarf2_queue = dwarf2_queue_tail = NULL;
1845 }
1846 };
1847
1848 /* The return type of find_file_and_directory. Note, the enclosed
1849 string pointers are only valid while this object is valid. */
1850
1851 struct file_and_directory
1852 {
1853 /* The filename. This is never NULL. */
1854 const char *name;
1855
1856 /* The compilation directory. NULL if not known. If we needed to
1857 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1858 points directly to the DW_AT_comp_dir string attribute owned by
1859 the obstack that owns the DIE. */
1860 const char *comp_dir;
1861
1862 /* If we needed to build a new string for comp_dir, this is what
1863 owns the storage. */
1864 std::string comp_dir_storage;
1865 };
1866
1867 static file_and_directory find_file_and_directory (struct die_info *die,
1868 struct dwarf2_cu *cu);
1869
1870 static char *file_full_name (int file, struct line_header *lh,
1871 const char *comp_dir);
1872
1873 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1874 enum class rcuh_kind { COMPILE, TYPE };
1875
1876 static const gdb_byte *read_and_check_comp_unit_head
1877 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1878 struct comp_unit_head *header,
1879 struct dwarf2_section_info *section,
1880 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1881 rcuh_kind section_kind);
1882
1883 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1884
1885 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1886
1887 static struct dwo_unit *lookup_dwo_unit_in_dwp
1888 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1889 struct dwp_file *dwp_file, const char *comp_dir,
1890 ULONGEST signature, int is_debug_types);
1891
1892 static struct dwp_file *get_dwp_file
1893 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1894
1895 static struct dwo_unit *lookup_dwo_comp_unit
1896 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1897
1898 static struct dwo_unit *lookup_dwo_type_unit
1899 (struct signatured_type *, const char *, const char *);
1900
1901 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1902
1903 /* A unique pointer to a dwo_file. */
1904
1905 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1906
1907 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1908
1909 static void check_producer (struct dwarf2_cu *cu);
1910
1911 static void free_line_header_voidp (void *arg);
1912 \f
1913 /* Various complaints about symbol reading that don't abort the process. */
1914
1915 static void
1916 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1917 {
1918 complaint (_("statement list doesn't fit in .debug_line section"));
1919 }
1920
1921 static void
1922 dwarf2_debug_line_missing_file_complaint (void)
1923 {
1924 complaint (_(".debug_line section has line data without a file"));
1925 }
1926
1927 static void
1928 dwarf2_debug_line_missing_end_sequence_complaint (void)
1929 {
1930 complaint (_(".debug_line section has line "
1931 "program sequence without an end"));
1932 }
1933
1934 static void
1935 dwarf2_complex_location_expr_complaint (void)
1936 {
1937 complaint (_("location expression too complex"));
1938 }
1939
1940 static void
1941 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1942 int arg3)
1943 {
1944 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1945 arg1, arg2, arg3);
1946 }
1947
1948 static void
1949 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1950 {
1951 complaint (_("debug info runs off end of %s section"
1952 " [in module %s]"),
1953 section->get_name (),
1954 section->get_file_name ());
1955 }
1956
1957 static void
1958 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1959 {
1960 complaint (_("macro debug info contains a "
1961 "malformed macro definition:\n`%s'"),
1962 arg1);
1963 }
1964
1965 static void
1966 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1967 {
1968 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1969 arg1, arg2);
1970 }
1971
1972 /* Hash function for line_header_hash. */
1973
1974 static hashval_t
1975 line_header_hash (const struct line_header *ofs)
1976 {
1977 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1978 }
1979
1980 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1981
1982 static hashval_t
1983 line_header_hash_voidp (const void *item)
1984 {
1985 const struct line_header *ofs = (const struct line_header *) item;
1986
1987 return line_header_hash (ofs);
1988 }
1989
1990 /* Equality function for line_header_hash. */
1991
1992 static int
1993 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1994 {
1995 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1996 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1997
1998 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1999 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2000 }
2001
2002 \f
2003
2004 /* See declaration. */
2005
2006 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2007 const dwarf2_debug_sections *names,
2008 bool can_copy_)
2009 : objfile (objfile_),
2010 can_copy (can_copy_)
2011 {
2012 if (names == NULL)
2013 names = &dwarf2_elf_names;
2014
2015 bfd *obfd = objfile->obfd;
2016
2017 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2018 locate_sections (obfd, sec, *names);
2019 }
2020
2021 dwarf2_per_objfile::~dwarf2_per_objfile ()
2022 {
2023 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2024 free_cached_comp_units ();
2025
2026 if (quick_file_names_table)
2027 htab_delete (quick_file_names_table);
2028
2029 if (line_header_hash)
2030 htab_delete (line_header_hash);
2031
2032 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2033 per_cu->imported_symtabs_free ();
2034
2035 for (signatured_type *sig_type : all_type_units)
2036 sig_type->per_cu.imported_symtabs_free ();
2037
2038 /* Everything else should be on the objfile obstack. */
2039 }
2040
2041 /* See declaration. */
2042
2043 void
2044 dwarf2_per_objfile::free_cached_comp_units ()
2045 {
2046 dwarf2_per_cu_data *per_cu = read_in_chain;
2047 dwarf2_per_cu_data **last_chain = &read_in_chain;
2048 while (per_cu != NULL)
2049 {
2050 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2051
2052 delete per_cu->cu;
2053 *last_chain = next_cu;
2054 per_cu = next_cu;
2055 }
2056 }
2057
2058 /* A helper class that calls free_cached_comp_units on
2059 destruction. */
2060
2061 class free_cached_comp_units
2062 {
2063 public:
2064
2065 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2066 : m_per_objfile (per_objfile)
2067 {
2068 }
2069
2070 ~free_cached_comp_units ()
2071 {
2072 m_per_objfile->free_cached_comp_units ();
2073 }
2074
2075 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2076
2077 private:
2078
2079 dwarf2_per_objfile *m_per_objfile;
2080 };
2081
2082 /* Try to locate the sections we need for DWARF 2 debugging
2083 information and return true if we have enough to do something.
2084 NAMES points to the dwarf2 section names, or is NULL if the standard
2085 ELF names are used. CAN_COPY is true for formats where symbol
2086 interposition is possible and so symbol values must follow copy
2087 relocation rules. */
2088
2089 int
2090 dwarf2_has_info (struct objfile *objfile,
2091 const struct dwarf2_debug_sections *names,
2092 bool can_copy)
2093 {
2094 if (objfile->flags & OBJF_READNEVER)
2095 return 0;
2096
2097 struct dwarf2_per_objfile *dwarf2_per_objfile
2098 = get_dwarf2_per_objfile (objfile);
2099
2100 if (dwarf2_per_objfile == NULL)
2101 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2102 names,
2103 can_copy);
2104
2105 return (!dwarf2_per_objfile->info.is_virtual
2106 && dwarf2_per_objfile->info.s.section != NULL
2107 && !dwarf2_per_objfile->abbrev.is_virtual
2108 && dwarf2_per_objfile->abbrev.s.section != NULL);
2109 }
2110
2111 /* When loading sections, we look either for uncompressed section or for
2112 compressed section names. */
2113
2114 static int
2115 section_is_p (const char *section_name,
2116 const struct dwarf2_section_names *names)
2117 {
2118 if (names->normal != NULL
2119 && strcmp (section_name, names->normal) == 0)
2120 return 1;
2121 if (names->compressed != NULL
2122 && strcmp (section_name, names->compressed) == 0)
2123 return 1;
2124 return 0;
2125 }
2126
2127 /* See declaration. */
2128
2129 void
2130 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2131 const dwarf2_debug_sections &names)
2132 {
2133 flagword aflag = bfd_section_flags (sectp);
2134
2135 if ((aflag & SEC_HAS_CONTENTS) == 0)
2136 {
2137 }
2138 else if (elf_section_data (sectp)->this_hdr.sh_size
2139 > bfd_get_file_size (abfd))
2140 {
2141 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2142 warning (_("Discarding section %s which has a section size (%s"
2143 ") larger than the file size [in module %s]"),
2144 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2145 bfd_get_filename (abfd));
2146 }
2147 else if (section_is_p (sectp->name, &names.info))
2148 {
2149 this->info.s.section = sectp;
2150 this->info.size = bfd_section_size (sectp);
2151 }
2152 else if (section_is_p (sectp->name, &names.abbrev))
2153 {
2154 this->abbrev.s.section = sectp;
2155 this->abbrev.size = bfd_section_size (sectp);
2156 }
2157 else if (section_is_p (sectp->name, &names.line))
2158 {
2159 this->line.s.section = sectp;
2160 this->line.size = bfd_section_size (sectp);
2161 }
2162 else if (section_is_p (sectp->name, &names.loc))
2163 {
2164 this->loc.s.section = sectp;
2165 this->loc.size = bfd_section_size (sectp);
2166 }
2167 else if (section_is_p (sectp->name, &names.loclists))
2168 {
2169 this->loclists.s.section = sectp;
2170 this->loclists.size = bfd_section_size (sectp);
2171 }
2172 else if (section_is_p (sectp->name, &names.macinfo))
2173 {
2174 this->macinfo.s.section = sectp;
2175 this->macinfo.size = bfd_section_size (sectp);
2176 }
2177 else if (section_is_p (sectp->name, &names.macro))
2178 {
2179 this->macro.s.section = sectp;
2180 this->macro.size = bfd_section_size (sectp);
2181 }
2182 else if (section_is_p (sectp->name, &names.str))
2183 {
2184 this->str.s.section = sectp;
2185 this->str.size = bfd_section_size (sectp);
2186 }
2187 else if (section_is_p (sectp->name, &names.str_offsets))
2188 {
2189 this->str_offsets.s.section = sectp;
2190 this->str_offsets.size = bfd_section_size (sectp);
2191 }
2192 else if (section_is_p (sectp->name, &names.line_str))
2193 {
2194 this->line_str.s.section = sectp;
2195 this->line_str.size = bfd_section_size (sectp);
2196 }
2197 else if (section_is_p (sectp->name, &names.addr))
2198 {
2199 this->addr.s.section = sectp;
2200 this->addr.size = bfd_section_size (sectp);
2201 }
2202 else if (section_is_p (sectp->name, &names.frame))
2203 {
2204 this->frame.s.section = sectp;
2205 this->frame.size = bfd_section_size (sectp);
2206 }
2207 else if (section_is_p (sectp->name, &names.eh_frame))
2208 {
2209 this->eh_frame.s.section = sectp;
2210 this->eh_frame.size = bfd_section_size (sectp);
2211 }
2212 else if (section_is_p (sectp->name, &names.ranges))
2213 {
2214 this->ranges.s.section = sectp;
2215 this->ranges.size = bfd_section_size (sectp);
2216 }
2217 else if (section_is_p (sectp->name, &names.rnglists))
2218 {
2219 this->rnglists.s.section = sectp;
2220 this->rnglists.size = bfd_section_size (sectp);
2221 }
2222 else if (section_is_p (sectp->name, &names.types))
2223 {
2224 struct dwarf2_section_info type_section;
2225
2226 memset (&type_section, 0, sizeof (type_section));
2227 type_section.s.section = sectp;
2228 type_section.size = bfd_section_size (sectp);
2229
2230 this->types.push_back (type_section);
2231 }
2232 else if (section_is_p (sectp->name, &names.gdb_index))
2233 {
2234 this->gdb_index.s.section = sectp;
2235 this->gdb_index.size = bfd_section_size (sectp);
2236 }
2237 else if (section_is_p (sectp->name, &names.debug_names))
2238 {
2239 this->debug_names.s.section = sectp;
2240 this->debug_names.size = bfd_section_size (sectp);
2241 }
2242 else if (section_is_p (sectp->name, &names.debug_aranges))
2243 {
2244 this->debug_aranges.s.section = sectp;
2245 this->debug_aranges.size = bfd_section_size (sectp);
2246 }
2247
2248 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2249 && bfd_section_vma (sectp) == 0)
2250 this->has_section_at_zero = true;
2251 }
2252
2253 /* A helper function that returns the size of a section in a safe way.
2254 If you are positive that the section has been read before using the
2255 size, then it is safe to refer to the dwarf2_section_info object's
2256 "size" field directly. In other cases, you must call this
2257 function, because for compressed sections the size field is not set
2258 correctly until the section has been read. */
2259
2260 static bfd_size_type
2261 dwarf2_section_size (struct objfile *objfile,
2262 struct dwarf2_section_info *info)
2263 {
2264 if (!info->readin)
2265 info->read (objfile);
2266 return info->size;
2267 }
2268
2269 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2270 SECTION_NAME. */
2271
2272 void
2273 dwarf2_get_section_info (struct objfile *objfile,
2274 enum dwarf2_section_enum sect,
2275 asection **sectp, const gdb_byte **bufp,
2276 bfd_size_type *sizep)
2277 {
2278 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2279 struct dwarf2_section_info *info;
2280
2281 /* We may see an objfile without any DWARF, in which case we just
2282 return nothing. */
2283 if (data == NULL)
2284 {
2285 *sectp = NULL;
2286 *bufp = NULL;
2287 *sizep = 0;
2288 return;
2289 }
2290 switch (sect)
2291 {
2292 case DWARF2_DEBUG_FRAME:
2293 info = &data->frame;
2294 break;
2295 case DWARF2_EH_FRAME:
2296 info = &data->eh_frame;
2297 break;
2298 default:
2299 gdb_assert_not_reached ("unexpected section");
2300 }
2301
2302 info->read (objfile);
2303
2304 *sectp = info->get_bfd_section ();
2305 *bufp = info->buffer;
2306 *sizep = info->size;
2307 }
2308
2309 /* A helper function to find the sections for a .dwz file. */
2310
2311 static void
2312 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2313 {
2314 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2315
2316 /* Note that we only support the standard ELF names, because .dwz
2317 is ELF-only (at the time of writing). */
2318 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2319 {
2320 dwz_file->abbrev.s.section = sectp;
2321 dwz_file->abbrev.size = bfd_section_size (sectp);
2322 }
2323 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2324 {
2325 dwz_file->info.s.section = sectp;
2326 dwz_file->info.size = bfd_section_size (sectp);
2327 }
2328 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2329 {
2330 dwz_file->str.s.section = sectp;
2331 dwz_file->str.size = bfd_section_size (sectp);
2332 }
2333 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2334 {
2335 dwz_file->line.s.section = sectp;
2336 dwz_file->line.size = bfd_section_size (sectp);
2337 }
2338 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2339 {
2340 dwz_file->macro.s.section = sectp;
2341 dwz_file->macro.size = bfd_section_size (sectp);
2342 }
2343 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2344 {
2345 dwz_file->gdb_index.s.section = sectp;
2346 dwz_file->gdb_index.size = bfd_section_size (sectp);
2347 }
2348 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2349 {
2350 dwz_file->debug_names.s.section = sectp;
2351 dwz_file->debug_names.size = bfd_section_size (sectp);
2352 }
2353 }
2354
2355 /* See dwarf2read.h. */
2356
2357 struct dwz_file *
2358 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2359 {
2360 const char *filename;
2361 bfd_size_type buildid_len_arg;
2362 size_t buildid_len;
2363 bfd_byte *buildid;
2364
2365 if (dwarf2_per_objfile->dwz_file != NULL)
2366 return dwarf2_per_objfile->dwz_file.get ();
2367
2368 bfd_set_error (bfd_error_no_error);
2369 gdb::unique_xmalloc_ptr<char> data
2370 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2371 &buildid_len_arg, &buildid));
2372 if (data == NULL)
2373 {
2374 if (bfd_get_error () == bfd_error_no_error)
2375 return NULL;
2376 error (_("could not read '.gnu_debugaltlink' section: %s"),
2377 bfd_errmsg (bfd_get_error ()));
2378 }
2379
2380 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2381
2382 buildid_len = (size_t) buildid_len_arg;
2383
2384 filename = data.get ();
2385
2386 std::string abs_storage;
2387 if (!IS_ABSOLUTE_PATH (filename))
2388 {
2389 gdb::unique_xmalloc_ptr<char> abs
2390 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2391
2392 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2393 filename = abs_storage.c_str ();
2394 }
2395
2396 /* First try the file name given in the section. If that doesn't
2397 work, try to use the build-id instead. */
2398 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2399 if (dwz_bfd != NULL)
2400 {
2401 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2402 dwz_bfd.reset (nullptr);
2403 }
2404
2405 if (dwz_bfd == NULL)
2406 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2407
2408 if (dwz_bfd == NULL)
2409 error (_("could not find '.gnu_debugaltlink' file for %s"),
2410 objfile_name (dwarf2_per_objfile->objfile));
2411
2412 std::unique_ptr<struct dwz_file> result
2413 (new struct dwz_file (std::move (dwz_bfd)));
2414
2415 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2416 result.get ());
2417
2418 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2419 result->dwz_bfd.get ());
2420 dwarf2_per_objfile->dwz_file = std::move (result);
2421 return dwarf2_per_objfile->dwz_file.get ();
2422 }
2423 \f
2424 /* DWARF quick_symbols_functions support. */
2425
2426 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2427 unique line tables, so we maintain a separate table of all .debug_line
2428 derived entries to support the sharing.
2429 All the quick functions need is the list of file names. We discard the
2430 line_header when we're done and don't need to record it here. */
2431 struct quick_file_names
2432 {
2433 /* The data used to construct the hash key. */
2434 struct stmt_list_hash hash;
2435
2436 /* The number of entries in file_names, real_names. */
2437 unsigned int num_file_names;
2438
2439 /* The file names from the line table, after being run through
2440 file_full_name. */
2441 const char **file_names;
2442
2443 /* The file names from the line table after being run through
2444 gdb_realpath. These are computed lazily. */
2445 const char **real_names;
2446 };
2447
2448 /* When using the index (and thus not using psymtabs), each CU has an
2449 object of this type. This is used to hold information needed by
2450 the various "quick" methods. */
2451 struct dwarf2_per_cu_quick_data
2452 {
2453 /* The file table. This can be NULL if there was no file table
2454 or it's currently not read in.
2455 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2456 struct quick_file_names *file_names;
2457
2458 /* The corresponding symbol table. This is NULL if symbols for this
2459 CU have not yet been read. */
2460 struct compunit_symtab *compunit_symtab;
2461
2462 /* A temporary mark bit used when iterating over all CUs in
2463 expand_symtabs_matching. */
2464 unsigned int mark : 1;
2465
2466 /* True if we've tried to read the file table and found there isn't one.
2467 There will be no point in trying to read it again next time. */
2468 unsigned int no_file_data : 1;
2469 };
2470
2471 /* Utility hash function for a stmt_list_hash. */
2472
2473 static hashval_t
2474 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2475 {
2476 hashval_t v = 0;
2477
2478 if (stmt_list_hash->dwo_unit != NULL)
2479 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2480 v += to_underlying (stmt_list_hash->line_sect_off);
2481 return v;
2482 }
2483
2484 /* Utility equality function for a stmt_list_hash. */
2485
2486 static int
2487 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2488 const struct stmt_list_hash *rhs)
2489 {
2490 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2491 return 0;
2492 if (lhs->dwo_unit != NULL
2493 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2494 return 0;
2495
2496 return lhs->line_sect_off == rhs->line_sect_off;
2497 }
2498
2499 /* Hash function for a quick_file_names. */
2500
2501 static hashval_t
2502 hash_file_name_entry (const void *e)
2503 {
2504 const struct quick_file_names *file_data
2505 = (const struct quick_file_names *) e;
2506
2507 return hash_stmt_list_entry (&file_data->hash);
2508 }
2509
2510 /* Equality function for a quick_file_names. */
2511
2512 static int
2513 eq_file_name_entry (const void *a, const void *b)
2514 {
2515 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2516 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2517
2518 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2519 }
2520
2521 /* Delete function for a quick_file_names. */
2522
2523 static void
2524 delete_file_name_entry (void *e)
2525 {
2526 struct quick_file_names *file_data = (struct quick_file_names *) e;
2527 int i;
2528
2529 for (i = 0; i < file_data->num_file_names; ++i)
2530 {
2531 xfree ((void*) file_data->file_names[i]);
2532 if (file_data->real_names)
2533 xfree ((void*) file_data->real_names[i]);
2534 }
2535
2536 /* The space for the struct itself lives on objfile_obstack,
2537 so we don't free it here. */
2538 }
2539
2540 /* Create a quick_file_names hash table. */
2541
2542 static htab_t
2543 create_quick_file_names_table (unsigned int nr_initial_entries)
2544 {
2545 return htab_create_alloc (nr_initial_entries,
2546 hash_file_name_entry, eq_file_name_entry,
2547 delete_file_name_entry, xcalloc, xfree);
2548 }
2549
2550 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2551 have to be created afterwards. You should call age_cached_comp_units after
2552 processing PER_CU->CU. dw2_setup must have been already called. */
2553
2554 static void
2555 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2556 {
2557 if (per_cu->is_debug_types)
2558 load_full_type_unit (per_cu);
2559 else
2560 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2561
2562 if (per_cu->cu == NULL)
2563 return; /* Dummy CU. */
2564
2565 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2566 }
2567
2568 /* Read in the symbols for PER_CU. */
2569
2570 static void
2571 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2572 {
2573 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2574
2575 /* Skip type_unit_groups, reading the type units they contain
2576 is handled elsewhere. */
2577 if (IS_TYPE_UNIT_GROUP (per_cu))
2578 return;
2579
2580 /* The destructor of dwarf2_queue_guard frees any entries left on
2581 the queue. After this point we're guaranteed to leave this function
2582 with the dwarf queue empty. */
2583 dwarf2_queue_guard q_guard;
2584
2585 if (dwarf2_per_objfile->using_index
2586 ? per_cu->v.quick->compunit_symtab == NULL
2587 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2588 {
2589 queue_comp_unit (per_cu, language_minimal);
2590 load_cu (per_cu, skip_partial);
2591
2592 /* If we just loaded a CU from a DWO, and we're working with an index
2593 that may badly handle TUs, load all the TUs in that DWO as well.
2594 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2595 if (!per_cu->is_debug_types
2596 && per_cu->cu != NULL
2597 && per_cu->cu->dwo_unit != NULL
2598 && dwarf2_per_objfile->index_table != NULL
2599 && dwarf2_per_objfile->index_table->version <= 7
2600 /* DWP files aren't supported yet. */
2601 && get_dwp_file (dwarf2_per_objfile) == NULL)
2602 queue_and_load_all_dwo_tus (per_cu);
2603 }
2604
2605 process_queue (dwarf2_per_objfile);
2606
2607 /* Age the cache, releasing compilation units that have not
2608 been used recently. */
2609 age_cached_comp_units (dwarf2_per_objfile);
2610 }
2611
2612 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2613 the objfile from which this CU came. Returns the resulting symbol
2614 table. */
2615
2616 static struct compunit_symtab *
2617 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2618 {
2619 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2620
2621 gdb_assert (dwarf2_per_objfile->using_index);
2622 if (!per_cu->v.quick->compunit_symtab)
2623 {
2624 free_cached_comp_units freer (dwarf2_per_objfile);
2625 scoped_restore decrementer = increment_reading_symtab ();
2626 dw2_do_instantiate_symtab (per_cu, skip_partial);
2627 process_cu_includes (dwarf2_per_objfile);
2628 }
2629
2630 return per_cu->v.quick->compunit_symtab;
2631 }
2632
2633 /* See declaration. */
2634
2635 dwarf2_per_cu_data *
2636 dwarf2_per_objfile::get_cutu (int index)
2637 {
2638 if (index >= this->all_comp_units.size ())
2639 {
2640 index -= this->all_comp_units.size ();
2641 gdb_assert (index < this->all_type_units.size ());
2642 return &this->all_type_units[index]->per_cu;
2643 }
2644
2645 return this->all_comp_units[index];
2646 }
2647
2648 /* See declaration. */
2649
2650 dwarf2_per_cu_data *
2651 dwarf2_per_objfile::get_cu (int index)
2652 {
2653 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2654
2655 return this->all_comp_units[index];
2656 }
2657
2658 /* See declaration. */
2659
2660 signatured_type *
2661 dwarf2_per_objfile::get_tu (int index)
2662 {
2663 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2664
2665 return this->all_type_units[index];
2666 }
2667
2668 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2669 objfile_obstack, and constructed with the specified field
2670 values. */
2671
2672 static dwarf2_per_cu_data *
2673 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2674 struct dwarf2_section_info *section,
2675 int is_dwz,
2676 sect_offset sect_off, ULONGEST length)
2677 {
2678 struct objfile *objfile = dwarf2_per_objfile->objfile;
2679 dwarf2_per_cu_data *the_cu
2680 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2681 struct dwarf2_per_cu_data);
2682 the_cu->sect_off = sect_off;
2683 the_cu->length = length;
2684 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2685 the_cu->section = section;
2686 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2687 struct dwarf2_per_cu_quick_data);
2688 the_cu->is_dwz = is_dwz;
2689 return the_cu;
2690 }
2691
2692 /* A helper for create_cus_from_index that handles a given list of
2693 CUs. */
2694
2695 static void
2696 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2697 const gdb_byte *cu_list, offset_type n_elements,
2698 struct dwarf2_section_info *section,
2699 int is_dwz)
2700 {
2701 for (offset_type i = 0; i < n_elements; i += 2)
2702 {
2703 gdb_static_assert (sizeof (ULONGEST) >= 8);
2704
2705 sect_offset sect_off
2706 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2707 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2708 cu_list += 2 * 8;
2709
2710 dwarf2_per_cu_data *per_cu
2711 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2712 sect_off, length);
2713 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2714 }
2715 }
2716
2717 /* Read the CU list from the mapped index, and use it to create all
2718 the CU objects for this objfile. */
2719
2720 static void
2721 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2722 const gdb_byte *cu_list, offset_type cu_list_elements,
2723 const gdb_byte *dwz_list, offset_type dwz_elements)
2724 {
2725 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2726 dwarf2_per_objfile->all_comp_units.reserve
2727 ((cu_list_elements + dwz_elements) / 2);
2728
2729 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2730 &dwarf2_per_objfile->info, 0);
2731
2732 if (dwz_elements == 0)
2733 return;
2734
2735 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2736 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2737 &dwz->info, 1);
2738 }
2739
2740 /* Create the signatured type hash table from the index. */
2741
2742 static void
2743 create_signatured_type_table_from_index
2744 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2745 struct dwarf2_section_info *section,
2746 const gdb_byte *bytes,
2747 offset_type elements)
2748 {
2749 struct objfile *objfile = dwarf2_per_objfile->objfile;
2750
2751 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2752 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2753
2754 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2755
2756 for (offset_type i = 0; i < elements; i += 3)
2757 {
2758 struct signatured_type *sig_type;
2759 ULONGEST signature;
2760 void **slot;
2761 cu_offset type_offset_in_tu;
2762
2763 gdb_static_assert (sizeof (ULONGEST) >= 8);
2764 sect_offset sect_off
2765 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2766 type_offset_in_tu
2767 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2768 BFD_ENDIAN_LITTLE);
2769 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2770 bytes += 3 * 8;
2771
2772 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2773 struct signatured_type);
2774 sig_type->signature = signature;
2775 sig_type->type_offset_in_tu = type_offset_in_tu;
2776 sig_type->per_cu.is_debug_types = 1;
2777 sig_type->per_cu.section = section;
2778 sig_type->per_cu.sect_off = sect_off;
2779 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2780 sig_type->per_cu.v.quick
2781 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2782 struct dwarf2_per_cu_quick_data);
2783
2784 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2785 *slot = sig_type;
2786
2787 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2788 }
2789
2790 dwarf2_per_objfile->signatured_types = sig_types_hash;
2791 }
2792
2793 /* Create the signatured type hash table from .debug_names. */
2794
2795 static void
2796 create_signatured_type_table_from_debug_names
2797 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2798 const mapped_debug_names &map,
2799 struct dwarf2_section_info *section,
2800 struct dwarf2_section_info *abbrev_section)
2801 {
2802 struct objfile *objfile = dwarf2_per_objfile->objfile;
2803
2804 section->read (objfile);
2805 abbrev_section->read (objfile);
2806
2807 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2808 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2809
2810 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2811
2812 for (uint32_t i = 0; i < map.tu_count; ++i)
2813 {
2814 struct signatured_type *sig_type;
2815 void **slot;
2816
2817 sect_offset sect_off
2818 = (sect_offset) (extract_unsigned_integer
2819 (map.tu_table_reordered + i * map.offset_size,
2820 map.offset_size,
2821 map.dwarf5_byte_order));
2822
2823 comp_unit_head cu_header;
2824 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2825 abbrev_section,
2826 section->buffer + to_underlying (sect_off),
2827 rcuh_kind::TYPE);
2828
2829 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2830 struct signatured_type);
2831 sig_type->signature = cu_header.signature;
2832 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2833 sig_type->per_cu.is_debug_types = 1;
2834 sig_type->per_cu.section = section;
2835 sig_type->per_cu.sect_off = sect_off;
2836 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2837 sig_type->per_cu.v.quick
2838 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2839 struct dwarf2_per_cu_quick_data);
2840
2841 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2842 *slot = sig_type;
2843
2844 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2845 }
2846
2847 dwarf2_per_objfile->signatured_types = sig_types_hash;
2848 }
2849
2850 /* Read the address map data from the mapped index, and use it to
2851 populate the objfile's psymtabs_addrmap. */
2852
2853 static void
2854 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2855 struct mapped_index *index)
2856 {
2857 struct objfile *objfile = dwarf2_per_objfile->objfile;
2858 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2859 const gdb_byte *iter, *end;
2860 struct addrmap *mutable_map;
2861 CORE_ADDR baseaddr;
2862
2863 auto_obstack temp_obstack;
2864
2865 mutable_map = addrmap_create_mutable (&temp_obstack);
2866
2867 iter = index->address_table.data ();
2868 end = iter + index->address_table.size ();
2869
2870 baseaddr = objfile->text_section_offset ();
2871
2872 while (iter < end)
2873 {
2874 ULONGEST hi, lo, cu_index;
2875 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2876 iter += 8;
2877 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2878 iter += 8;
2879 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2880 iter += 4;
2881
2882 if (lo > hi)
2883 {
2884 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2885 hex_string (lo), hex_string (hi));
2886 continue;
2887 }
2888
2889 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2890 {
2891 complaint (_(".gdb_index address table has invalid CU number %u"),
2892 (unsigned) cu_index);
2893 continue;
2894 }
2895
2896 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2897 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2898 addrmap_set_empty (mutable_map, lo, hi - 1,
2899 dwarf2_per_objfile->get_cu (cu_index));
2900 }
2901
2902 objfile->partial_symtabs->psymtabs_addrmap
2903 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2904 }
2905
2906 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2907 populate the objfile's psymtabs_addrmap. */
2908
2909 static void
2910 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2911 struct dwarf2_section_info *section)
2912 {
2913 struct objfile *objfile = dwarf2_per_objfile->objfile;
2914 bfd *abfd = objfile->obfd;
2915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2916 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2917
2918 auto_obstack temp_obstack;
2919 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2920
2921 std::unordered_map<sect_offset,
2922 dwarf2_per_cu_data *,
2923 gdb::hash_enum<sect_offset>>
2924 debug_info_offset_to_per_cu;
2925 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2926 {
2927 const auto insertpair
2928 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2929 if (!insertpair.second)
2930 {
2931 warning (_("Section .debug_aranges in %s has duplicate "
2932 "debug_info_offset %s, ignoring .debug_aranges."),
2933 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2934 return;
2935 }
2936 }
2937
2938 section->read (objfile);
2939
2940 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2941
2942 const gdb_byte *addr = section->buffer;
2943
2944 while (addr < section->buffer + section->size)
2945 {
2946 const gdb_byte *const entry_addr = addr;
2947 unsigned int bytes_read;
2948
2949 const LONGEST entry_length = read_initial_length (abfd, addr,
2950 &bytes_read);
2951 addr += bytes_read;
2952
2953 const gdb_byte *const entry_end = addr + entry_length;
2954 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2955 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2956 if (addr + entry_length > section->buffer + section->size)
2957 {
2958 warning (_("Section .debug_aranges in %s entry at offset %s "
2959 "length %s exceeds section length %s, "
2960 "ignoring .debug_aranges."),
2961 objfile_name (objfile),
2962 plongest (entry_addr - section->buffer),
2963 plongest (bytes_read + entry_length),
2964 pulongest (section->size));
2965 return;
2966 }
2967
2968 /* The version number. */
2969 const uint16_t version = read_2_bytes (abfd, addr);
2970 addr += 2;
2971 if (version != 2)
2972 {
2973 warning (_("Section .debug_aranges in %s entry at offset %s "
2974 "has unsupported version %d, ignoring .debug_aranges."),
2975 objfile_name (objfile),
2976 plongest (entry_addr - section->buffer), version);
2977 return;
2978 }
2979
2980 const uint64_t debug_info_offset
2981 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2982 addr += offset_size;
2983 const auto per_cu_it
2984 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2985 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2986 {
2987 warning (_("Section .debug_aranges in %s entry at offset %s "
2988 "debug_info_offset %s does not exists, "
2989 "ignoring .debug_aranges."),
2990 objfile_name (objfile),
2991 plongest (entry_addr - section->buffer),
2992 pulongest (debug_info_offset));
2993 return;
2994 }
2995 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2996
2997 const uint8_t address_size = *addr++;
2998 if (address_size < 1 || address_size > 8)
2999 {
3000 warning (_("Section .debug_aranges in %s entry at offset %s "
3001 "address_size %u is invalid, ignoring .debug_aranges."),
3002 objfile_name (objfile),
3003 plongest (entry_addr - section->buffer), address_size);
3004 return;
3005 }
3006
3007 const uint8_t segment_selector_size = *addr++;
3008 if (segment_selector_size != 0)
3009 {
3010 warning (_("Section .debug_aranges in %s entry at offset %s "
3011 "segment_selector_size %u is not supported, "
3012 "ignoring .debug_aranges."),
3013 objfile_name (objfile),
3014 plongest (entry_addr - section->buffer),
3015 segment_selector_size);
3016 return;
3017 }
3018
3019 /* Must pad to an alignment boundary that is twice the address
3020 size. It is undocumented by the DWARF standard but GCC does
3021 use it. */
3022 for (size_t padding = ((-(addr - section->buffer))
3023 & (2 * address_size - 1));
3024 padding > 0; padding--)
3025 if (*addr++ != 0)
3026 {
3027 warning (_("Section .debug_aranges in %s entry at offset %s "
3028 "padding is not zero, ignoring .debug_aranges."),
3029 objfile_name (objfile),
3030 plongest (entry_addr - section->buffer));
3031 return;
3032 }
3033
3034 for (;;)
3035 {
3036 if (addr + 2 * address_size > entry_end)
3037 {
3038 warning (_("Section .debug_aranges in %s entry at offset %s "
3039 "address list is not properly terminated, "
3040 "ignoring .debug_aranges."),
3041 objfile_name (objfile),
3042 plongest (entry_addr - section->buffer));
3043 return;
3044 }
3045 ULONGEST start = extract_unsigned_integer (addr, address_size,
3046 dwarf5_byte_order);
3047 addr += address_size;
3048 ULONGEST length = extract_unsigned_integer (addr, address_size,
3049 dwarf5_byte_order);
3050 addr += address_size;
3051 if (start == 0 && length == 0)
3052 break;
3053 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3054 {
3055 /* Symbol was eliminated due to a COMDAT group. */
3056 continue;
3057 }
3058 ULONGEST end = start + length;
3059 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3060 - baseaddr);
3061 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3062 - baseaddr);
3063 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3064 }
3065 }
3066
3067 objfile->partial_symtabs->psymtabs_addrmap
3068 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3069 }
3070
3071 /* Find a slot in the mapped index INDEX for the object named NAME.
3072 If NAME is found, set *VEC_OUT to point to the CU vector in the
3073 constant pool and return true. If NAME cannot be found, return
3074 false. */
3075
3076 static bool
3077 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3078 offset_type **vec_out)
3079 {
3080 offset_type hash;
3081 offset_type slot, step;
3082 int (*cmp) (const char *, const char *);
3083
3084 gdb::unique_xmalloc_ptr<char> without_params;
3085 if (current_language->la_language == language_cplus
3086 || current_language->la_language == language_fortran
3087 || current_language->la_language == language_d)
3088 {
3089 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3090 not contain any. */
3091
3092 if (strchr (name, '(') != NULL)
3093 {
3094 without_params = cp_remove_params (name);
3095
3096 if (without_params != NULL)
3097 name = without_params.get ();
3098 }
3099 }
3100
3101 /* Index version 4 did not support case insensitive searches. But the
3102 indices for case insensitive languages are built in lowercase, therefore
3103 simulate our NAME being searched is also lowercased. */
3104 hash = mapped_index_string_hash ((index->version == 4
3105 && case_sensitivity == case_sensitive_off
3106 ? 5 : index->version),
3107 name);
3108
3109 slot = hash & (index->symbol_table.size () - 1);
3110 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3111 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3112
3113 for (;;)
3114 {
3115 const char *str;
3116
3117 const auto &bucket = index->symbol_table[slot];
3118 if (bucket.name == 0 && bucket.vec == 0)
3119 return false;
3120
3121 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3122 if (!cmp (name, str))
3123 {
3124 *vec_out = (offset_type *) (index->constant_pool
3125 + MAYBE_SWAP (bucket.vec));
3126 return true;
3127 }
3128
3129 slot = (slot + step) & (index->symbol_table.size () - 1);
3130 }
3131 }
3132
3133 /* A helper function that reads the .gdb_index from BUFFER and fills
3134 in MAP. FILENAME is the name of the file containing the data;
3135 it is used for error reporting. DEPRECATED_OK is true if it is
3136 ok to use deprecated sections.
3137
3138 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3139 out parameters that are filled in with information about the CU and
3140 TU lists in the section.
3141
3142 Returns true if all went well, false otherwise. */
3143
3144 static bool
3145 read_gdb_index_from_buffer (struct objfile *objfile,
3146 const char *filename,
3147 bool deprecated_ok,
3148 gdb::array_view<const gdb_byte> buffer,
3149 struct mapped_index *map,
3150 const gdb_byte **cu_list,
3151 offset_type *cu_list_elements,
3152 const gdb_byte **types_list,
3153 offset_type *types_list_elements)
3154 {
3155 const gdb_byte *addr = &buffer[0];
3156
3157 /* Version check. */
3158 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3159 /* Versions earlier than 3 emitted every copy of a psymbol. This
3160 causes the index to behave very poorly for certain requests. Version 3
3161 contained incomplete addrmap. So, it seems better to just ignore such
3162 indices. */
3163 if (version < 4)
3164 {
3165 static int warning_printed = 0;
3166 if (!warning_printed)
3167 {
3168 warning (_("Skipping obsolete .gdb_index section in %s."),
3169 filename);
3170 warning_printed = 1;
3171 }
3172 return 0;
3173 }
3174 /* Index version 4 uses a different hash function than index version
3175 5 and later.
3176
3177 Versions earlier than 6 did not emit psymbols for inlined
3178 functions. Using these files will cause GDB not to be able to
3179 set breakpoints on inlined functions by name, so we ignore these
3180 indices unless the user has done
3181 "set use-deprecated-index-sections on". */
3182 if (version < 6 && !deprecated_ok)
3183 {
3184 static int warning_printed = 0;
3185 if (!warning_printed)
3186 {
3187 warning (_("\
3188 Skipping deprecated .gdb_index section in %s.\n\
3189 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3190 to use the section anyway."),
3191 filename);
3192 warning_printed = 1;
3193 }
3194 return 0;
3195 }
3196 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3197 of the TU (for symbols coming from TUs),
3198 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3199 Plus gold-generated indices can have duplicate entries for global symbols,
3200 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3201 These are just performance bugs, and we can't distinguish gdb-generated
3202 indices from gold-generated ones, so issue no warning here. */
3203
3204 /* Indexes with higher version than the one supported by GDB may be no
3205 longer backward compatible. */
3206 if (version > 8)
3207 return 0;
3208
3209 map->version = version;
3210
3211 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3212
3213 int i = 0;
3214 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3215 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3216 / 8);
3217 ++i;
3218
3219 *types_list = addr + MAYBE_SWAP (metadata[i]);
3220 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3221 - MAYBE_SWAP (metadata[i]))
3222 / 8);
3223 ++i;
3224
3225 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3226 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3227 map->address_table
3228 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3229 ++i;
3230
3231 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3232 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3233 map->symbol_table
3234 = gdb::array_view<mapped_index::symbol_table_slot>
3235 ((mapped_index::symbol_table_slot *) symbol_table,
3236 (mapped_index::symbol_table_slot *) symbol_table_end);
3237
3238 ++i;
3239 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3240
3241 return 1;
3242 }
3243
3244 /* Callback types for dwarf2_read_gdb_index. */
3245
3246 typedef gdb::function_view
3247 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3248 get_gdb_index_contents_ftype;
3249 typedef gdb::function_view
3250 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3251 get_gdb_index_contents_dwz_ftype;
3252
3253 /* Read .gdb_index. If everything went ok, initialize the "quick"
3254 elements of all the CUs and return 1. Otherwise, return 0. */
3255
3256 static int
3257 dwarf2_read_gdb_index
3258 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3259 get_gdb_index_contents_ftype get_gdb_index_contents,
3260 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3261 {
3262 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3263 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3264 struct dwz_file *dwz;
3265 struct objfile *objfile = dwarf2_per_objfile->objfile;
3266
3267 gdb::array_view<const gdb_byte> main_index_contents
3268 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3269
3270 if (main_index_contents.empty ())
3271 return 0;
3272
3273 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3274 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3275 use_deprecated_index_sections,
3276 main_index_contents, map.get (), &cu_list,
3277 &cu_list_elements, &types_list,
3278 &types_list_elements))
3279 return 0;
3280
3281 /* Don't use the index if it's empty. */
3282 if (map->symbol_table.empty ())
3283 return 0;
3284
3285 /* If there is a .dwz file, read it so we can get its CU list as
3286 well. */
3287 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3288 if (dwz != NULL)
3289 {
3290 struct mapped_index dwz_map;
3291 const gdb_byte *dwz_types_ignore;
3292 offset_type dwz_types_elements_ignore;
3293
3294 gdb::array_view<const gdb_byte> dwz_index_content
3295 = get_gdb_index_contents_dwz (objfile, dwz);
3296
3297 if (dwz_index_content.empty ())
3298 return 0;
3299
3300 if (!read_gdb_index_from_buffer (objfile,
3301 bfd_get_filename (dwz->dwz_bfd.get ()),
3302 1, dwz_index_content, &dwz_map,
3303 &dwz_list, &dwz_list_elements,
3304 &dwz_types_ignore,
3305 &dwz_types_elements_ignore))
3306 {
3307 warning (_("could not read '.gdb_index' section from %s; skipping"),
3308 bfd_get_filename (dwz->dwz_bfd.get ()));
3309 return 0;
3310 }
3311 }
3312
3313 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3314 dwz_list, dwz_list_elements);
3315
3316 if (types_list_elements)
3317 {
3318 /* We can only handle a single .debug_types when we have an
3319 index. */
3320 if (dwarf2_per_objfile->types.size () != 1)
3321 return 0;
3322
3323 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3324
3325 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3326 types_list, types_list_elements);
3327 }
3328
3329 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3330
3331 dwarf2_per_objfile->index_table = std::move (map);
3332 dwarf2_per_objfile->using_index = 1;
3333 dwarf2_per_objfile->quick_file_names_table =
3334 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3335
3336 return 1;
3337 }
3338
3339 /* die_reader_func for dw2_get_file_names. */
3340
3341 static void
3342 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3343 const gdb_byte *info_ptr,
3344 struct die_info *comp_unit_die,
3345 int has_children)
3346 {
3347 struct dwarf2_cu *cu = reader->cu;
3348 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3349 struct dwarf2_per_objfile *dwarf2_per_objfile
3350 = cu->per_cu->dwarf2_per_objfile;
3351 struct objfile *objfile = dwarf2_per_objfile->objfile;
3352 struct dwarf2_per_cu_data *lh_cu;
3353 struct attribute *attr;
3354 void **slot;
3355 struct quick_file_names *qfn;
3356
3357 gdb_assert (! this_cu->is_debug_types);
3358
3359 /* Our callers never want to match partial units -- instead they
3360 will match the enclosing full CU. */
3361 if (comp_unit_die->tag == DW_TAG_partial_unit)
3362 {
3363 this_cu->v.quick->no_file_data = 1;
3364 return;
3365 }
3366
3367 lh_cu = this_cu;
3368 slot = NULL;
3369
3370 line_header_up lh;
3371 sect_offset line_offset {};
3372
3373 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3374 if (attr != nullptr)
3375 {
3376 struct quick_file_names find_entry;
3377
3378 line_offset = (sect_offset) DW_UNSND (attr);
3379
3380 /* We may have already read in this line header (TU line header sharing).
3381 If we have we're done. */
3382 find_entry.hash.dwo_unit = cu->dwo_unit;
3383 find_entry.hash.line_sect_off = line_offset;
3384 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3385 &find_entry, INSERT);
3386 if (*slot != NULL)
3387 {
3388 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3389 return;
3390 }
3391
3392 lh = dwarf_decode_line_header (line_offset, cu);
3393 }
3394 if (lh == NULL)
3395 {
3396 lh_cu->v.quick->no_file_data = 1;
3397 return;
3398 }
3399
3400 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3401 qfn->hash.dwo_unit = cu->dwo_unit;
3402 qfn->hash.line_sect_off = line_offset;
3403 gdb_assert (slot != NULL);
3404 *slot = qfn;
3405
3406 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3407
3408 int offset = 0;
3409 if (strcmp (fnd.name, "<unknown>") != 0)
3410 ++offset;
3411
3412 qfn->num_file_names = offset + lh->file_names_size ();
3413 qfn->file_names =
3414 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3415 if (offset != 0)
3416 qfn->file_names[0] = xstrdup (fnd.name);
3417 for (int i = 0; i < lh->file_names_size (); ++i)
3418 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3419 qfn->real_names = NULL;
3420
3421 lh_cu->v.quick->file_names = qfn;
3422 }
3423
3424 /* A helper for the "quick" functions which attempts to read the line
3425 table for THIS_CU. */
3426
3427 static struct quick_file_names *
3428 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3429 {
3430 /* This should never be called for TUs. */
3431 gdb_assert (! this_cu->is_debug_types);
3432 /* Nor type unit groups. */
3433 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3434
3435 if (this_cu->v.quick->file_names != NULL)
3436 return this_cu->v.quick->file_names;
3437 /* If we know there is no line data, no point in looking again. */
3438 if (this_cu->v.quick->no_file_data)
3439 return NULL;
3440
3441 cutu_reader reader (this_cu);
3442 if (!reader.dummy_p)
3443 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3444 reader.has_children);
3445
3446 if (this_cu->v.quick->no_file_data)
3447 return NULL;
3448 return this_cu->v.quick->file_names;
3449 }
3450
3451 /* A helper for the "quick" functions which computes and caches the
3452 real path for a given file name from the line table. */
3453
3454 static const char *
3455 dw2_get_real_path (struct objfile *objfile,
3456 struct quick_file_names *qfn, int index)
3457 {
3458 if (qfn->real_names == NULL)
3459 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3460 qfn->num_file_names, const char *);
3461
3462 if (qfn->real_names[index] == NULL)
3463 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3464
3465 return qfn->real_names[index];
3466 }
3467
3468 static struct symtab *
3469 dw2_find_last_source_symtab (struct objfile *objfile)
3470 {
3471 struct dwarf2_per_objfile *dwarf2_per_objfile
3472 = get_dwarf2_per_objfile (objfile);
3473 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3474 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3475
3476 if (cust == NULL)
3477 return NULL;
3478
3479 return compunit_primary_filetab (cust);
3480 }
3481
3482 /* Traversal function for dw2_forget_cached_source_info. */
3483
3484 static int
3485 dw2_free_cached_file_names (void **slot, void *info)
3486 {
3487 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3488
3489 if (file_data->real_names)
3490 {
3491 int i;
3492
3493 for (i = 0; i < file_data->num_file_names; ++i)
3494 {
3495 xfree ((void*) file_data->real_names[i]);
3496 file_data->real_names[i] = NULL;
3497 }
3498 }
3499
3500 return 1;
3501 }
3502
3503 static void
3504 dw2_forget_cached_source_info (struct objfile *objfile)
3505 {
3506 struct dwarf2_per_objfile *dwarf2_per_objfile
3507 = get_dwarf2_per_objfile (objfile);
3508
3509 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3510 dw2_free_cached_file_names, NULL);
3511 }
3512
3513 /* Helper function for dw2_map_symtabs_matching_filename that expands
3514 the symtabs and calls the iterator. */
3515
3516 static int
3517 dw2_map_expand_apply (struct objfile *objfile,
3518 struct dwarf2_per_cu_data *per_cu,
3519 const char *name, const char *real_path,
3520 gdb::function_view<bool (symtab *)> callback)
3521 {
3522 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3523
3524 /* Don't visit already-expanded CUs. */
3525 if (per_cu->v.quick->compunit_symtab)
3526 return 0;
3527
3528 /* This may expand more than one symtab, and we want to iterate over
3529 all of them. */
3530 dw2_instantiate_symtab (per_cu, false);
3531
3532 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3533 last_made, callback);
3534 }
3535
3536 /* Implementation of the map_symtabs_matching_filename method. */
3537
3538 static bool
3539 dw2_map_symtabs_matching_filename
3540 (struct objfile *objfile, const char *name, const char *real_path,
3541 gdb::function_view<bool (symtab *)> callback)
3542 {
3543 const char *name_basename = lbasename (name);
3544 struct dwarf2_per_objfile *dwarf2_per_objfile
3545 = get_dwarf2_per_objfile (objfile);
3546
3547 /* The rule is CUs specify all the files, including those used by
3548 any TU, so there's no need to scan TUs here. */
3549
3550 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3551 {
3552 /* We only need to look at symtabs not already expanded. */
3553 if (per_cu->v.quick->compunit_symtab)
3554 continue;
3555
3556 quick_file_names *file_data = dw2_get_file_names (per_cu);
3557 if (file_data == NULL)
3558 continue;
3559
3560 for (int j = 0; j < file_data->num_file_names; ++j)
3561 {
3562 const char *this_name = file_data->file_names[j];
3563 const char *this_real_name;
3564
3565 if (compare_filenames_for_search (this_name, name))
3566 {
3567 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3568 callback))
3569 return true;
3570 continue;
3571 }
3572
3573 /* Before we invoke realpath, which can get expensive when many
3574 files are involved, do a quick comparison of the basenames. */
3575 if (! basenames_may_differ
3576 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3577 continue;
3578
3579 this_real_name = dw2_get_real_path (objfile, file_data, j);
3580 if (compare_filenames_for_search (this_real_name, name))
3581 {
3582 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3583 callback))
3584 return true;
3585 continue;
3586 }
3587
3588 if (real_path != NULL)
3589 {
3590 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3591 gdb_assert (IS_ABSOLUTE_PATH (name));
3592 if (this_real_name != NULL
3593 && FILENAME_CMP (real_path, this_real_name) == 0)
3594 {
3595 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3596 callback))
3597 return true;
3598 continue;
3599 }
3600 }
3601 }
3602 }
3603
3604 return false;
3605 }
3606
3607 /* Struct used to manage iterating over all CUs looking for a symbol. */
3608
3609 struct dw2_symtab_iterator
3610 {
3611 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3612 struct dwarf2_per_objfile *dwarf2_per_objfile;
3613 /* If set, only look for symbols that match that block. Valid values are
3614 GLOBAL_BLOCK and STATIC_BLOCK. */
3615 gdb::optional<block_enum> block_index;
3616 /* The kind of symbol we're looking for. */
3617 domain_enum domain;
3618 /* The list of CUs from the index entry of the symbol,
3619 or NULL if not found. */
3620 offset_type *vec;
3621 /* The next element in VEC to look at. */
3622 int next;
3623 /* The number of elements in VEC, or zero if there is no match. */
3624 int length;
3625 /* Have we seen a global version of the symbol?
3626 If so we can ignore all further global instances.
3627 This is to work around gold/15646, inefficient gold-generated
3628 indices. */
3629 int global_seen;
3630 };
3631
3632 /* Initialize the index symtab iterator ITER. */
3633
3634 static void
3635 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3636 struct dwarf2_per_objfile *dwarf2_per_objfile,
3637 gdb::optional<block_enum> block_index,
3638 domain_enum domain,
3639 const char *name)
3640 {
3641 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3642 iter->block_index = block_index;
3643 iter->domain = domain;
3644 iter->next = 0;
3645 iter->global_seen = 0;
3646
3647 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3648
3649 /* index is NULL if OBJF_READNOW. */
3650 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3651 iter->length = MAYBE_SWAP (*iter->vec);
3652 else
3653 {
3654 iter->vec = NULL;
3655 iter->length = 0;
3656 }
3657 }
3658
3659 /* Return the next matching CU or NULL if there are no more. */
3660
3661 static struct dwarf2_per_cu_data *
3662 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3663 {
3664 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3665
3666 for ( ; iter->next < iter->length; ++iter->next)
3667 {
3668 offset_type cu_index_and_attrs =
3669 MAYBE_SWAP (iter->vec[iter->next + 1]);
3670 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3671 gdb_index_symbol_kind symbol_kind =
3672 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3673 /* Only check the symbol attributes if they're present.
3674 Indices prior to version 7 don't record them,
3675 and indices >= 7 may elide them for certain symbols
3676 (gold does this). */
3677 int attrs_valid =
3678 (dwarf2_per_objfile->index_table->version >= 7
3679 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3680
3681 /* Don't crash on bad data. */
3682 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3683 + dwarf2_per_objfile->all_type_units.size ()))
3684 {
3685 complaint (_(".gdb_index entry has bad CU index"
3686 " [in module %s]"),
3687 objfile_name (dwarf2_per_objfile->objfile));
3688 continue;
3689 }
3690
3691 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3692
3693 /* Skip if already read in. */
3694 if (per_cu->v.quick->compunit_symtab)
3695 continue;
3696
3697 /* Check static vs global. */
3698 if (attrs_valid)
3699 {
3700 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3701
3702 if (iter->block_index.has_value ())
3703 {
3704 bool want_static = *iter->block_index == STATIC_BLOCK;
3705
3706 if (is_static != want_static)
3707 continue;
3708 }
3709
3710 /* Work around gold/15646. */
3711 if (!is_static && iter->global_seen)
3712 continue;
3713 if (!is_static)
3714 iter->global_seen = 1;
3715 }
3716
3717 /* Only check the symbol's kind if it has one. */
3718 if (attrs_valid)
3719 {
3720 switch (iter->domain)
3721 {
3722 case VAR_DOMAIN:
3723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3724 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3725 /* Some types are also in VAR_DOMAIN. */
3726 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3727 continue;
3728 break;
3729 case STRUCT_DOMAIN:
3730 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3731 continue;
3732 break;
3733 case LABEL_DOMAIN:
3734 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3735 continue;
3736 break;
3737 case MODULE_DOMAIN:
3738 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3739 continue;
3740 break;
3741 default:
3742 break;
3743 }
3744 }
3745
3746 ++iter->next;
3747 return per_cu;
3748 }
3749
3750 return NULL;
3751 }
3752
3753 static struct compunit_symtab *
3754 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3755 const char *name, domain_enum domain)
3756 {
3757 struct compunit_symtab *stab_best = NULL;
3758 struct dwarf2_per_objfile *dwarf2_per_objfile
3759 = get_dwarf2_per_objfile (objfile);
3760
3761 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3762
3763 struct dw2_symtab_iterator iter;
3764 struct dwarf2_per_cu_data *per_cu;
3765
3766 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3767
3768 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3769 {
3770 struct symbol *sym, *with_opaque = NULL;
3771 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3772 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3773 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3774
3775 sym = block_find_symbol (block, name, domain,
3776 block_find_non_opaque_type_preferred,
3777 &with_opaque);
3778
3779 /* Some caution must be observed with overloaded functions
3780 and methods, since the index will not contain any overload
3781 information (but NAME might contain it). */
3782
3783 if (sym != NULL
3784 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3785 return stab;
3786 if (with_opaque != NULL
3787 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3788 stab_best = stab;
3789
3790 /* Keep looking through other CUs. */
3791 }
3792
3793 return stab_best;
3794 }
3795
3796 static void
3797 dw2_print_stats (struct objfile *objfile)
3798 {
3799 struct dwarf2_per_objfile *dwarf2_per_objfile
3800 = get_dwarf2_per_objfile (objfile);
3801 int total = (dwarf2_per_objfile->all_comp_units.size ()
3802 + dwarf2_per_objfile->all_type_units.size ());
3803 int count = 0;
3804
3805 for (int i = 0; i < total; ++i)
3806 {
3807 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3808
3809 if (!per_cu->v.quick->compunit_symtab)
3810 ++count;
3811 }
3812 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3813 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3814 }
3815
3816 /* This dumps minimal information about the index.
3817 It is called via "mt print objfiles".
3818 One use is to verify .gdb_index has been loaded by the
3819 gdb.dwarf2/gdb-index.exp testcase. */
3820
3821 static void
3822 dw2_dump (struct objfile *objfile)
3823 {
3824 struct dwarf2_per_objfile *dwarf2_per_objfile
3825 = get_dwarf2_per_objfile (objfile);
3826
3827 gdb_assert (dwarf2_per_objfile->using_index);
3828 printf_filtered (".gdb_index:");
3829 if (dwarf2_per_objfile->index_table != NULL)
3830 {
3831 printf_filtered (" version %d\n",
3832 dwarf2_per_objfile->index_table->version);
3833 }
3834 else
3835 printf_filtered (" faked for \"readnow\"\n");
3836 printf_filtered ("\n");
3837 }
3838
3839 static void
3840 dw2_expand_symtabs_for_function (struct objfile *objfile,
3841 const char *func_name)
3842 {
3843 struct dwarf2_per_objfile *dwarf2_per_objfile
3844 = get_dwarf2_per_objfile (objfile);
3845
3846 struct dw2_symtab_iterator iter;
3847 struct dwarf2_per_cu_data *per_cu;
3848
3849 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3850
3851 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3852 dw2_instantiate_symtab (per_cu, false);
3853
3854 }
3855
3856 static void
3857 dw2_expand_all_symtabs (struct objfile *objfile)
3858 {
3859 struct dwarf2_per_objfile *dwarf2_per_objfile
3860 = get_dwarf2_per_objfile (objfile);
3861 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3862 + dwarf2_per_objfile->all_type_units.size ());
3863
3864 for (int i = 0; i < total_units; ++i)
3865 {
3866 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3867
3868 /* We don't want to directly expand a partial CU, because if we
3869 read it with the wrong language, then assertion failures can
3870 be triggered later on. See PR symtab/23010. So, tell
3871 dw2_instantiate_symtab to skip partial CUs -- any important
3872 partial CU will be read via DW_TAG_imported_unit anyway. */
3873 dw2_instantiate_symtab (per_cu, true);
3874 }
3875 }
3876
3877 static void
3878 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3879 const char *fullname)
3880 {
3881 struct dwarf2_per_objfile *dwarf2_per_objfile
3882 = get_dwarf2_per_objfile (objfile);
3883
3884 /* We don't need to consider type units here.
3885 This is only called for examining code, e.g. expand_line_sal.
3886 There can be an order of magnitude (or more) more type units
3887 than comp units, and we avoid them if we can. */
3888
3889 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3890 {
3891 /* We only need to look at symtabs not already expanded. */
3892 if (per_cu->v.quick->compunit_symtab)
3893 continue;
3894
3895 quick_file_names *file_data = dw2_get_file_names (per_cu);
3896 if (file_data == NULL)
3897 continue;
3898
3899 for (int j = 0; j < file_data->num_file_names; ++j)
3900 {
3901 const char *this_fullname = file_data->file_names[j];
3902
3903 if (filename_cmp (this_fullname, fullname) == 0)
3904 {
3905 dw2_instantiate_symtab (per_cu, false);
3906 break;
3907 }
3908 }
3909 }
3910 }
3911
3912 static void
3913 dw2_map_matching_symbols
3914 (struct objfile *objfile,
3915 const lookup_name_info &name, domain_enum domain,
3916 int global,
3917 gdb::function_view<symbol_found_callback_ftype> callback,
3918 symbol_compare_ftype *ordered_compare)
3919 {
3920 /* Currently unimplemented; used for Ada. The function can be called if the
3921 current language is Ada for a non-Ada objfile using GNU index. As Ada
3922 does not look for non-Ada symbols this function should just return. */
3923 }
3924
3925 /* Starting from a search name, return the string that finds the upper
3926 bound of all strings that start with SEARCH_NAME in a sorted name
3927 list. Returns the empty string to indicate that the upper bound is
3928 the end of the list. */
3929
3930 static std::string
3931 make_sort_after_prefix_name (const char *search_name)
3932 {
3933 /* When looking to complete "func", we find the upper bound of all
3934 symbols that start with "func" by looking for where we'd insert
3935 the closest string that would follow "func" in lexicographical
3936 order. Usually, that's "func"-with-last-character-incremented,
3937 i.e. "fund". Mind non-ASCII characters, though. Usually those
3938 will be UTF-8 multi-byte sequences, but we can't be certain.
3939 Especially mind the 0xff character, which is a valid character in
3940 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3941 rule out compilers allowing it in identifiers. Note that
3942 conveniently, strcmp/strcasecmp are specified to compare
3943 characters interpreted as unsigned char. So what we do is treat
3944 the whole string as a base 256 number composed of a sequence of
3945 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3946 to 0, and carries 1 to the following more-significant position.
3947 If the very first character in SEARCH_NAME ends up incremented
3948 and carries/overflows, then the upper bound is the end of the
3949 list. The string after the empty string is also the empty
3950 string.
3951
3952 Some examples of this operation:
3953
3954 SEARCH_NAME => "+1" RESULT
3955
3956 "abc" => "abd"
3957 "ab\xff" => "ac"
3958 "\xff" "a" "\xff" => "\xff" "b"
3959 "\xff" => ""
3960 "\xff\xff" => ""
3961 "" => ""
3962
3963 Then, with these symbols for example:
3964
3965 func
3966 func1
3967 fund
3968
3969 completing "func" looks for symbols between "func" and
3970 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3971 which finds "func" and "func1", but not "fund".
3972
3973 And with:
3974
3975 funcÿ (Latin1 'ÿ' [0xff])
3976 funcÿ1
3977 fund
3978
3979 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3980 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3981
3982 And with:
3983
3984 ÿÿ (Latin1 'ÿ' [0xff])
3985 ÿÿ1
3986
3987 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3988 the end of the list.
3989 */
3990 std::string after = search_name;
3991 while (!after.empty () && (unsigned char) after.back () == 0xff)
3992 after.pop_back ();
3993 if (!after.empty ())
3994 after.back () = (unsigned char) after.back () + 1;
3995 return after;
3996 }
3997
3998 /* See declaration. */
3999
4000 std::pair<std::vector<name_component>::const_iterator,
4001 std::vector<name_component>::const_iterator>
4002 mapped_index_base::find_name_components_bounds
4003 (const lookup_name_info &lookup_name_without_params, language lang) const
4004 {
4005 auto *name_cmp
4006 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4007
4008 const char *lang_name
4009 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4010
4011 /* Comparison function object for lower_bound that matches against a
4012 given symbol name. */
4013 auto lookup_compare_lower = [&] (const name_component &elem,
4014 const char *name)
4015 {
4016 const char *elem_qualified = this->symbol_name_at (elem.idx);
4017 const char *elem_name = elem_qualified + elem.name_offset;
4018 return name_cmp (elem_name, name) < 0;
4019 };
4020
4021 /* Comparison function object for upper_bound that matches against a
4022 given symbol name. */
4023 auto lookup_compare_upper = [&] (const char *name,
4024 const name_component &elem)
4025 {
4026 const char *elem_qualified = this->symbol_name_at (elem.idx);
4027 const char *elem_name = elem_qualified + elem.name_offset;
4028 return name_cmp (name, elem_name) < 0;
4029 };
4030
4031 auto begin = this->name_components.begin ();
4032 auto end = this->name_components.end ();
4033
4034 /* Find the lower bound. */
4035 auto lower = [&] ()
4036 {
4037 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4038 return begin;
4039 else
4040 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4041 } ();
4042
4043 /* Find the upper bound. */
4044 auto upper = [&] ()
4045 {
4046 if (lookup_name_without_params.completion_mode ())
4047 {
4048 /* In completion mode, we want UPPER to point past all
4049 symbols names that have the same prefix. I.e., with
4050 these symbols, and completing "func":
4051
4052 function << lower bound
4053 function1
4054 other_function << upper bound
4055
4056 We find the upper bound by looking for the insertion
4057 point of "func"-with-last-character-incremented,
4058 i.e. "fund". */
4059 std::string after = make_sort_after_prefix_name (lang_name);
4060 if (after.empty ())
4061 return end;
4062 return std::lower_bound (lower, end, after.c_str (),
4063 lookup_compare_lower);
4064 }
4065 else
4066 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4067 } ();
4068
4069 return {lower, upper};
4070 }
4071
4072 /* See declaration. */
4073
4074 void
4075 mapped_index_base::build_name_components ()
4076 {
4077 if (!this->name_components.empty ())
4078 return;
4079
4080 this->name_components_casing = case_sensitivity;
4081 auto *name_cmp
4082 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4083
4084 /* The code below only knows how to break apart components of C++
4085 symbol names (and other languages that use '::' as
4086 namespace/module separator) and Ada symbol names. */
4087 auto count = this->symbol_name_count ();
4088 for (offset_type idx = 0; idx < count; idx++)
4089 {
4090 if (this->symbol_name_slot_invalid (idx))
4091 continue;
4092
4093 const char *name = this->symbol_name_at (idx);
4094
4095 /* Add each name component to the name component table. */
4096 unsigned int previous_len = 0;
4097
4098 if (strstr (name, "::") != nullptr)
4099 {
4100 for (unsigned int current_len = cp_find_first_component (name);
4101 name[current_len] != '\0';
4102 current_len += cp_find_first_component (name + current_len))
4103 {
4104 gdb_assert (name[current_len] == ':');
4105 this->name_components.push_back ({previous_len, idx});
4106 /* Skip the '::'. */
4107 current_len += 2;
4108 previous_len = current_len;
4109 }
4110 }
4111 else
4112 {
4113 /* Handle the Ada encoded (aka mangled) form here. */
4114 for (const char *iter = strstr (name, "__");
4115 iter != nullptr;
4116 iter = strstr (iter, "__"))
4117 {
4118 this->name_components.push_back ({previous_len, idx});
4119 iter += 2;
4120 previous_len = iter - name;
4121 }
4122 }
4123
4124 this->name_components.push_back ({previous_len, idx});
4125 }
4126
4127 /* Sort name_components elements by name. */
4128 auto name_comp_compare = [&] (const name_component &left,
4129 const name_component &right)
4130 {
4131 const char *left_qualified = this->symbol_name_at (left.idx);
4132 const char *right_qualified = this->symbol_name_at (right.idx);
4133
4134 const char *left_name = left_qualified + left.name_offset;
4135 const char *right_name = right_qualified + right.name_offset;
4136
4137 return name_cmp (left_name, right_name) < 0;
4138 };
4139
4140 std::sort (this->name_components.begin (),
4141 this->name_components.end (),
4142 name_comp_compare);
4143 }
4144
4145 /* Helper for dw2_expand_symtabs_matching that works with a
4146 mapped_index_base instead of the containing objfile. This is split
4147 to a separate function in order to be able to unit test the
4148 name_components matching using a mock mapped_index_base. For each
4149 symbol name that matches, calls MATCH_CALLBACK, passing it the
4150 symbol's index in the mapped_index_base symbol table. */
4151
4152 static void
4153 dw2_expand_symtabs_matching_symbol
4154 (mapped_index_base &index,
4155 const lookup_name_info &lookup_name_in,
4156 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4157 enum search_domain kind,
4158 gdb::function_view<bool (offset_type)> match_callback)
4159 {
4160 lookup_name_info lookup_name_without_params
4161 = lookup_name_in.make_ignore_params ();
4162
4163 /* Build the symbol name component sorted vector, if we haven't
4164 yet. */
4165 index.build_name_components ();
4166
4167 /* The same symbol may appear more than once in the range though.
4168 E.g., if we're looking for symbols that complete "w", and we have
4169 a symbol named "w1::w2", we'll find the two name components for
4170 that same symbol in the range. To be sure we only call the
4171 callback once per symbol, we first collect the symbol name
4172 indexes that matched in a temporary vector and ignore
4173 duplicates. */
4174 std::vector<offset_type> matches;
4175
4176 struct name_and_matcher
4177 {
4178 symbol_name_matcher_ftype *matcher;
4179 const std::string &name;
4180
4181 bool operator== (const name_and_matcher &other) const
4182 {
4183 return matcher == other.matcher && name == other.name;
4184 }
4185 };
4186
4187 /* A vector holding all the different symbol name matchers, for all
4188 languages. */
4189 std::vector<name_and_matcher> matchers;
4190
4191 for (int i = 0; i < nr_languages; i++)
4192 {
4193 enum language lang_e = (enum language) i;
4194
4195 const language_defn *lang = language_def (lang_e);
4196 symbol_name_matcher_ftype *name_matcher
4197 = get_symbol_name_matcher (lang, lookup_name_without_params);
4198
4199 name_and_matcher key {
4200 name_matcher,
4201 lookup_name_without_params.language_lookup_name (lang_e)
4202 };
4203
4204 /* Don't insert the same comparison routine more than once.
4205 Note that we do this linear walk. This is not a problem in
4206 practice because the number of supported languages is
4207 low. */
4208 if (std::find (matchers.begin (), matchers.end (), key)
4209 != matchers.end ())
4210 continue;
4211 matchers.push_back (std::move (key));
4212
4213 auto bounds
4214 = index.find_name_components_bounds (lookup_name_without_params,
4215 lang_e);
4216
4217 /* Now for each symbol name in range, check to see if we have a name
4218 match, and if so, call the MATCH_CALLBACK callback. */
4219
4220 for (; bounds.first != bounds.second; ++bounds.first)
4221 {
4222 const char *qualified = index.symbol_name_at (bounds.first->idx);
4223
4224 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4225 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4226 continue;
4227
4228 matches.push_back (bounds.first->idx);
4229 }
4230 }
4231
4232 std::sort (matches.begin (), matches.end ());
4233
4234 /* Finally call the callback, once per match. */
4235 ULONGEST prev = -1;
4236 for (offset_type idx : matches)
4237 {
4238 if (prev != idx)
4239 {
4240 if (!match_callback (idx))
4241 break;
4242 prev = idx;
4243 }
4244 }
4245
4246 /* Above we use a type wider than idx's for 'prev', since 0 and
4247 (offset_type)-1 are both possible values. */
4248 static_assert (sizeof (prev) > sizeof (offset_type), "");
4249 }
4250
4251 #if GDB_SELF_TEST
4252
4253 namespace selftests { namespace dw2_expand_symtabs_matching {
4254
4255 /* A mock .gdb_index/.debug_names-like name index table, enough to
4256 exercise dw2_expand_symtabs_matching_symbol, which works with the
4257 mapped_index_base interface. Builds an index from the symbol list
4258 passed as parameter to the constructor. */
4259 class mock_mapped_index : public mapped_index_base
4260 {
4261 public:
4262 mock_mapped_index (gdb::array_view<const char *> symbols)
4263 : m_symbol_table (symbols)
4264 {}
4265
4266 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4267
4268 /* Return the number of names in the symbol table. */
4269 size_t symbol_name_count () const override
4270 {
4271 return m_symbol_table.size ();
4272 }
4273
4274 /* Get the name of the symbol at IDX in the symbol table. */
4275 const char *symbol_name_at (offset_type idx) const override
4276 {
4277 return m_symbol_table[idx];
4278 }
4279
4280 private:
4281 gdb::array_view<const char *> m_symbol_table;
4282 };
4283
4284 /* Convenience function that converts a NULL pointer to a "<null>"
4285 string, to pass to print routines. */
4286
4287 static const char *
4288 string_or_null (const char *str)
4289 {
4290 return str != NULL ? str : "<null>";
4291 }
4292
4293 /* Check if a lookup_name_info built from
4294 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4295 index. EXPECTED_LIST is the list of expected matches, in expected
4296 matching order. If no match expected, then an empty list is
4297 specified. Returns true on success. On failure prints a warning
4298 indicating the file:line that failed, and returns false. */
4299
4300 static bool
4301 check_match (const char *file, int line,
4302 mock_mapped_index &mock_index,
4303 const char *name, symbol_name_match_type match_type,
4304 bool completion_mode,
4305 std::initializer_list<const char *> expected_list)
4306 {
4307 lookup_name_info lookup_name (name, match_type, completion_mode);
4308
4309 bool matched = true;
4310
4311 auto mismatch = [&] (const char *expected_str,
4312 const char *got)
4313 {
4314 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4315 "expected=\"%s\", got=\"%s\"\n"),
4316 file, line,
4317 (match_type == symbol_name_match_type::FULL
4318 ? "FULL" : "WILD"),
4319 name, string_or_null (expected_str), string_or_null (got));
4320 matched = false;
4321 };
4322
4323 auto expected_it = expected_list.begin ();
4324 auto expected_end = expected_list.end ();
4325
4326 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4327 NULL, ALL_DOMAIN,
4328 [&] (offset_type idx)
4329 {
4330 const char *matched_name = mock_index.symbol_name_at (idx);
4331 const char *expected_str
4332 = expected_it == expected_end ? NULL : *expected_it++;
4333
4334 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4335 mismatch (expected_str, matched_name);
4336 return true;
4337 });
4338
4339 const char *expected_str
4340 = expected_it == expected_end ? NULL : *expected_it++;
4341 if (expected_str != NULL)
4342 mismatch (expected_str, NULL);
4343
4344 return matched;
4345 }
4346
4347 /* The symbols added to the mock mapped_index for testing (in
4348 canonical form). */
4349 static const char *test_symbols[] = {
4350 "function",
4351 "std::bar",
4352 "std::zfunction",
4353 "std::zfunction2",
4354 "w1::w2",
4355 "ns::foo<char*>",
4356 "ns::foo<int>",
4357 "ns::foo<long>",
4358 "ns2::tmpl<int>::foo2",
4359 "(anonymous namespace)::A::B::C",
4360
4361 /* These are used to check that the increment-last-char in the
4362 matching algorithm for completion doesn't match "t1_fund" when
4363 completing "t1_func". */
4364 "t1_func",
4365 "t1_func1",
4366 "t1_fund",
4367 "t1_fund1",
4368
4369 /* A UTF-8 name with multi-byte sequences to make sure that
4370 cp-name-parser understands this as a single identifier ("função"
4371 is "function" in PT). */
4372 u8"u8função",
4373
4374 /* \377 (0xff) is Latin1 'ÿ'. */
4375 "yfunc\377",
4376
4377 /* \377 (0xff) is Latin1 'ÿ'. */
4378 "\377",
4379 "\377\377123",
4380
4381 /* A name with all sorts of complications. Starts with "z" to make
4382 it easier for the completion tests below. */
4383 #define Z_SYM_NAME \
4384 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4385 "::tuple<(anonymous namespace)::ui*, " \
4386 "std::default_delete<(anonymous namespace)::ui>, void>"
4387
4388 Z_SYM_NAME
4389 };
4390
4391 /* Returns true if the mapped_index_base::find_name_component_bounds
4392 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4393 in completion mode. */
4394
4395 static bool
4396 check_find_bounds_finds (mapped_index_base &index,
4397 const char *search_name,
4398 gdb::array_view<const char *> expected_syms)
4399 {
4400 lookup_name_info lookup_name (search_name,
4401 symbol_name_match_type::FULL, true);
4402
4403 auto bounds = index.find_name_components_bounds (lookup_name,
4404 language_cplus);
4405
4406 size_t distance = std::distance (bounds.first, bounds.second);
4407 if (distance != expected_syms.size ())
4408 return false;
4409
4410 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4411 {
4412 auto nc_elem = bounds.first + exp_elem;
4413 const char *qualified = index.symbol_name_at (nc_elem->idx);
4414 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4415 return false;
4416 }
4417
4418 return true;
4419 }
4420
4421 /* Test the lower-level mapped_index::find_name_component_bounds
4422 method. */
4423
4424 static void
4425 test_mapped_index_find_name_component_bounds ()
4426 {
4427 mock_mapped_index mock_index (test_symbols);
4428
4429 mock_index.build_name_components ();
4430
4431 /* Test the lower-level mapped_index::find_name_component_bounds
4432 method in completion mode. */
4433 {
4434 static const char *expected_syms[] = {
4435 "t1_func",
4436 "t1_func1",
4437 };
4438
4439 SELF_CHECK (check_find_bounds_finds (mock_index,
4440 "t1_func", expected_syms));
4441 }
4442
4443 /* Check that the increment-last-char in the name matching algorithm
4444 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4445 {
4446 static const char *expected_syms1[] = {
4447 "\377",
4448 "\377\377123",
4449 };
4450 SELF_CHECK (check_find_bounds_finds (mock_index,
4451 "\377", expected_syms1));
4452
4453 static const char *expected_syms2[] = {
4454 "\377\377123",
4455 };
4456 SELF_CHECK (check_find_bounds_finds (mock_index,
4457 "\377\377", expected_syms2));
4458 }
4459 }
4460
4461 /* Test dw2_expand_symtabs_matching_symbol. */
4462
4463 static void
4464 test_dw2_expand_symtabs_matching_symbol ()
4465 {
4466 mock_mapped_index mock_index (test_symbols);
4467
4468 /* We let all tests run until the end even if some fails, for debug
4469 convenience. */
4470 bool any_mismatch = false;
4471
4472 /* Create the expected symbols list (an initializer_list). Needed
4473 because lists have commas, and we need to pass them to CHECK,
4474 which is a macro. */
4475 #define EXPECT(...) { __VA_ARGS__ }
4476
4477 /* Wrapper for check_match that passes down the current
4478 __FILE__/__LINE__. */
4479 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4480 any_mismatch |= !check_match (__FILE__, __LINE__, \
4481 mock_index, \
4482 NAME, MATCH_TYPE, COMPLETION_MODE, \
4483 EXPECTED_LIST)
4484
4485 /* Identity checks. */
4486 for (const char *sym : test_symbols)
4487 {
4488 /* Should be able to match all existing symbols. */
4489 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4490 EXPECT (sym));
4491
4492 /* Should be able to match all existing symbols with
4493 parameters. */
4494 std::string with_params = std::string (sym) + "(int)";
4495 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4496 EXPECT (sym));
4497
4498 /* Should be able to match all existing symbols with
4499 parameters and qualifiers. */
4500 with_params = std::string (sym) + " ( int ) const";
4501 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4502 EXPECT (sym));
4503
4504 /* This should really find sym, but cp-name-parser.y doesn't
4505 know about lvalue/rvalue qualifiers yet. */
4506 with_params = std::string (sym) + " ( int ) &&";
4507 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4508 {});
4509 }
4510
4511 /* Check that the name matching algorithm for completion doesn't get
4512 confused with Latin1 'ÿ' / 0xff. */
4513 {
4514 static const char str[] = "\377";
4515 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4516 EXPECT ("\377", "\377\377123"));
4517 }
4518
4519 /* Check that the increment-last-char in the matching algorithm for
4520 completion doesn't match "t1_fund" when completing "t1_func". */
4521 {
4522 static const char str[] = "t1_func";
4523 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4524 EXPECT ("t1_func", "t1_func1"));
4525 }
4526
4527 /* Check that completion mode works at each prefix of the expected
4528 symbol name. */
4529 {
4530 static const char str[] = "function(int)";
4531 size_t len = strlen (str);
4532 std::string lookup;
4533
4534 for (size_t i = 1; i < len; i++)
4535 {
4536 lookup.assign (str, i);
4537 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4538 EXPECT ("function"));
4539 }
4540 }
4541
4542 /* While "w" is a prefix of both components, the match function
4543 should still only be called once. */
4544 {
4545 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4546 EXPECT ("w1::w2"));
4547 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4548 EXPECT ("w1::w2"));
4549 }
4550
4551 /* Same, with a "complicated" symbol. */
4552 {
4553 static const char str[] = Z_SYM_NAME;
4554 size_t len = strlen (str);
4555 std::string lookup;
4556
4557 for (size_t i = 1; i < len; i++)
4558 {
4559 lookup.assign (str, i);
4560 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4561 EXPECT (Z_SYM_NAME));
4562 }
4563 }
4564
4565 /* In FULL mode, an incomplete symbol doesn't match. */
4566 {
4567 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4568 {});
4569 }
4570
4571 /* A complete symbol with parameters matches any overload, since the
4572 index has no overload info. */
4573 {
4574 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4575 EXPECT ("std::zfunction", "std::zfunction2"));
4576 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4577 EXPECT ("std::zfunction", "std::zfunction2"));
4578 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4579 EXPECT ("std::zfunction", "std::zfunction2"));
4580 }
4581
4582 /* Check that whitespace is ignored appropriately. A symbol with a
4583 template argument list. */
4584 {
4585 static const char expected[] = "ns::foo<int>";
4586 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4587 EXPECT (expected));
4588 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4589 EXPECT (expected));
4590 }
4591
4592 /* Check that whitespace is ignored appropriately. A symbol with a
4593 template argument list that includes a pointer. */
4594 {
4595 static const char expected[] = "ns::foo<char*>";
4596 /* Try both completion and non-completion modes. */
4597 static const bool completion_mode[2] = {false, true};
4598 for (size_t i = 0; i < 2; i++)
4599 {
4600 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4601 completion_mode[i], EXPECT (expected));
4602 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4603 completion_mode[i], EXPECT (expected));
4604
4605 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4606 completion_mode[i], EXPECT (expected));
4607 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4608 completion_mode[i], EXPECT (expected));
4609 }
4610 }
4611
4612 {
4613 /* Check method qualifiers are ignored. */
4614 static const char expected[] = "ns::foo<char*>";
4615 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4616 symbol_name_match_type::FULL, true, EXPECT (expected));
4617 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4618 symbol_name_match_type::FULL, true, EXPECT (expected));
4619 CHECK_MATCH ("foo < char * > ( int ) const",
4620 symbol_name_match_type::WILD, true, EXPECT (expected));
4621 CHECK_MATCH ("foo < char * > ( int ) &&",
4622 symbol_name_match_type::WILD, true, EXPECT (expected));
4623 }
4624
4625 /* Test lookup names that don't match anything. */
4626 {
4627 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4628 {});
4629
4630 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4631 {});
4632 }
4633
4634 /* Some wild matching tests, exercising "(anonymous namespace)",
4635 which should not be confused with a parameter list. */
4636 {
4637 static const char *syms[] = {
4638 "A::B::C",
4639 "B::C",
4640 "C",
4641 "A :: B :: C ( int )",
4642 "B :: C ( int )",
4643 "C ( int )",
4644 };
4645
4646 for (const char *s : syms)
4647 {
4648 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4649 EXPECT ("(anonymous namespace)::A::B::C"));
4650 }
4651 }
4652
4653 {
4654 static const char expected[] = "ns2::tmpl<int>::foo2";
4655 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4656 EXPECT (expected));
4657 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4658 EXPECT (expected));
4659 }
4660
4661 SELF_CHECK (!any_mismatch);
4662
4663 #undef EXPECT
4664 #undef CHECK_MATCH
4665 }
4666
4667 static void
4668 run_test ()
4669 {
4670 test_mapped_index_find_name_component_bounds ();
4671 test_dw2_expand_symtabs_matching_symbol ();
4672 }
4673
4674 }} // namespace selftests::dw2_expand_symtabs_matching
4675
4676 #endif /* GDB_SELF_TEST */
4677
4678 /* If FILE_MATCHER is NULL or if PER_CU has
4679 dwarf2_per_cu_quick_data::MARK set (see
4680 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4681 EXPANSION_NOTIFY on it. */
4682
4683 static void
4684 dw2_expand_symtabs_matching_one
4685 (struct dwarf2_per_cu_data *per_cu,
4686 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4687 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4688 {
4689 if (file_matcher == NULL || per_cu->v.quick->mark)
4690 {
4691 bool symtab_was_null
4692 = (per_cu->v.quick->compunit_symtab == NULL);
4693
4694 dw2_instantiate_symtab (per_cu, false);
4695
4696 if (expansion_notify != NULL
4697 && symtab_was_null
4698 && per_cu->v.quick->compunit_symtab != NULL)
4699 expansion_notify (per_cu->v.quick->compunit_symtab);
4700 }
4701 }
4702
4703 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4704 matched, to expand corresponding CUs that were marked. IDX is the
4705 index of the symbol name that matched. */
4706
4707 static void
4708 dw2_expand_marked_cus
4709 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4710 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4711 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4712 search_domain kind)
4713 {
4714 offset_type *vec, vec_len, vec_idx;
4715 bool global_seen = false;
4716 mapped_index &index = *dwarf2_per_objfile->index_table;
4717
4718 vec = (offset_type *) (index.constant_pool
4719 + MAYBE_SWAP (index.symbol_table[idx].vec));
4720 vec_len = MAYBE_SWAP (vec[0]);
4721 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4722 {
4723 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4724 /* This value is only valid for index versions >= 7. */
4725 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4726 gdb_index_symbol_kind symbol_kind =
4727 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4728 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4729 /* Only check the symbol attributes if they're present.
4730 Indices prior to version 7 don't record them,
4731 and indices >= 7 may elide them for certain symbols
4732 (gold does this). */
4733 int attrs_valid =
4734 (index.version >= 7
4735 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4736
4737 /* Work around gold/15646. */
4738 if (attrs_valid)
4739 {
4740 if (!is_static && global_seen)
4741 continue;
4742 if (!is_static)
4743 global_seen = true;
4744 }
4745
4746 /* Only check the symbol's kind if it has one. */
4747 if (attrs_valid)
4748 {
4749 switch (kind)
4750 {
4751 case VARIABLES_DOMAIN:
4752 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4753 continue;
4754 break;
4755 case FUNCTIONS_DOMAIN:
4756 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4757 continue;
4758 break;
4759 case TYPES_DOMAIN:
4760 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4761 continue;
4762 break;
4763 case MODULES_DOMAIN:
4764 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4765 continue;
4766 break;
4767 default:
4768 break;
4769 }
4770 }
4771
4772 /* Don't crash on bad data. */
4773 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4774 + dwarf2_per_objfile->all_type_units.size ()))
4775 {
4776 complaint (_(".gdb_index entry has bad CU index"
4777 " [in module %s]"),
4778 objfile_name (dwarf2_per_objfile->objfile));
4779 continue;
4780 }
4781
4782 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4783 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4784 expansion_notify);
4785 }
4786 }
4787
4788 /* If FILE_MATCHER is non-NULL, set all the
4789 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4790 that match FILE_MATCHER. */
4791
4792 static void
4793 dw_expand_symtabs_matching_file_matcher
4794 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4795 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4796 {
4797 if (file_matcher == NULL)
4798 return;
4799
4800 objfile *const objfile = dwarf2_per_objfile->objfile;
4801
4802 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4803 htab_eq_pointer,
4804 NULL, xcalloc, xfree));
4805 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4806 htab_eq_pointer,
4807 NULL, xcalloc, xfree));
4808
4809 /* The rule is CUs specify all the files, including those used by
4810 any TU, so there's no need to scan TUs here. */
4811
4812 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4813 {
4814 QUIT;
4815
4816 per_cu->v.quick->mark = 0;
4817
4818 /* We only need to look at symtabs not already expanded. */
4819 if (per_cu->v.quick->compunit_symtab)
4820 continue;
4821
4822 quick_file_names *file_data = dw2_get_file_names (per_cu);
4823 if (file_data == NULL)
4824 continue;
4825
4826 if (htab_find (visited_not_found.get (), file_data) != NULL)
4827 continue;
4828 else if (htab_find (visited_found.get (), file_data) != NULL)
4829 {
4830 per_cu->v.quick->mark = 1;
4831 continue;
4832 }
4833
4834 for (int j = 0; j < file_data->num_file_names; ++j)
4835 {
4836 const char *this_real_name;
4837
4838 if (file_matcher (file_data->file_names[j], false))
4839 {
4840 per_cu->v.quick->mark = 1;
4841 break;
4842 }
4843
4844 /* Before we invoke realpath, which can get expensive when many
4845 files are involved, do a quick comparison of the basenames. */
4846 if (!basenames_may_differ
4847 && !file_matcher (lbasename (file_data->file_names[j]),
4848 true))
4849 continue;
4850
4851 this_real_name = dw2_get_real_path (objfile, file_data, j);
4852 if (file_matcher (this_real_name, false))
4853 {
4854 per_cu->v.quick->mark = 1;
4855 break;
4856 }
4857 }
4858
4859 void **slot = htab_find_slot (per_cu->v.quick->mark
4860 ? visited_found.get ()
4861 : visited_not_found.get (),
4862 file_data, INSERT);
4863 *slot = file_data;
4864 }
4865 }
4866
4867 static void
4868 dw2_expand_symtabs_matching
4869 (struct objfile *objfile,
4870 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4871 const lookup_name_info &lookup_name,
4872 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4873 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4874 enum search_domain kind)
4875 {
4876 struct dwarf2_per_objfile *dwarf2_per_objfile
4877 = get_dwarf2_per_objfile (objfile);
4878
4879 /* index_table is NULL if OBJF_READNOW. */
4880 if (!dwarf2_per_objfile->index_table)
4881 return;
4882
4883 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4884
4885 mapped_index &index = *dwarf2_per_objfile->index_table;
4886
4887 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4888 symbol_matcher,
4889 kind, [&] (offset_type idx)
4890 {
4891 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4892 expansion_notify, kind);
4893 return true;
4894 });
4895 }
4896
4897 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4898 symtab. */
4899
4900 static struct compunit_symtab *
4901 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4902 CORE_ADDR pc)
4903 {
4904 int i;
4905
4906 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4907 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4908 return cust;
4909
4910 if (cust->includes == NULL)
4911 return NULL;
4912
4913 for (i = 0; cust->includes[i]; ++i)
4914 {
4915 struct compunit_symtab *s = cust->includes[i];
4916
4917 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4918 if (s != NULL)
4919 return s;
4920 }
4921
4922 return NULL;
4923 }
4924
4925 static struct compunit_symtab *
4926 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4927 struct bound_minimal_symbol msymbol,
4928 CORE_ADDR pc,
4929 struct obj_section *section,
4930 int warn_if_readin)
4931 {
4932 struct dwarf2_per_cu_data *data;
4933 struct compunit_symtab *result;
4934
4935 if (!objfile->partial_symtabs->psymtabs_addrmap)
4936 return NULL;
4937
4938 CORE_ADDR baseaddr = objfile->text_section_offset ();
4939 data = (struct dwarf2_per_cu_data *) addrmap_find
4940 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4941 if (!data)
4942 return NULL;
4943
4944 if (warn_if_readin && data->v.quick->compunit_symtab)
4945 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4946 paddress (get_objfile_arch (objfile), pc));
4947
4948 result
4949 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4950 false),
4951 pc);
4952 gdb_assert (result != NULL);
4953 return result;
4954 }
4955
4956 static void
4957 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4958 void *data, int need_fullname)
4959 {
4960 struct dwarf2_per_objfile *dwarf2_per_objfile
4961 = get_dwarf2_per_objfile (objfile);
4962
4963 if (!dwarf2_per_objfile->filenames_cache)
4964 {
4965 dwarf2_per_objfile->filenames_cache.emplace ();
4966
4967 htab_up visited (htab_create_alloc (10,
4968 htab_hash_pointer, htab_eq_pointer,
4969 NULL, xcalloc, xfree));
4970
4971 /* The rule is CUs specify all the files, including those used
4972 by any TU, so there's no need to scan TUs here. We can
4973 ignore file names coming from already-expanded CUs. */
4974
4975 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4976 {
4977 if (per_cu->v.quick->compunit_symtab)
4978 {
4979 void **slot = htab_find_slot (visited.get (),
4980 per_cu->v.quick->file_names,
4981 INSERT);
4982
4983 *slot = per_cu->v.quick->file_names;
4984 }
4985 }
4986
4987 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4988 {
4989 /* We only need to look at symtabs not already expanded. */
4990 if (per_cu->v.quick->compunit_symtab)
4991 continue;
4992
4993 quick_file_names *file_data = dw2_get_file_names (per_cu);
4994 if (file_data == NULL)
4995 continue;
4996
4997 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4998 if (*slot)
4999 {
5000 /* Already visited. */
5001 continue;
5002 }
5003 *slot = file_data;
5004
5005 for (int j = 0; j < file_data->num_file_names; ++j)
5006 {
5007 const char *filename = file_data->file_names[j];
5008 dwarf2_per_objfile->filenames_cache->seen (filename);
5009 }
5010 }
5011 }
5012
5013 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5014 {
5015 gdb::unique_xmalloc_ptr<char> this_real_name;
5016
5017 if (need_fullname)
5018 this_real_name = gdb_realpath (filename);
5019 (*fun) (filename, this_real_name.get (), data);
5020 });
5021 }
5022
5023 static int
5024 dw2_has_symbols (struct objfile *objfile)
5025 {
5026 return 1;
5027 }
5028
5029 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5030 {
5031 dw2_has_symbols,
5032 dw2_find_last_source_symtab,
5033 dw2_forget_cached_source_info,
5034 dw2_map_symtabs_matching_filename,
5035 dw2_lookup_symbol,
5036 dw2_print_stats,
5037 dw2_dump,
5038 dw2_expand_symtabs_for_function,
5039 dw2_expand_all_symtabs,
5040 dw2_expand_symtabs_with_fullname,
5041 dw2_map_matching_symbols,
5042 dw2_expand_symtabs_matching,
5043 dw2_find_pc_sect_compunit_symtab,
5044 NULL,
5045 dw2_map_symbol_filenames
5046 };
5047
5048 /* DWARF-5 debug_names reader. */
5049
5050 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5051 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5052
5053 /* A helper function that reads the .debug_names section in SECTION
5054 and fills in MAP. FILENAME is the name of the file containing the
5055 section; it is used for error reporting.
5056
5057 Returns true if all went well, false otherwise. */
5058
5059 static bool
5060 read_debug_names_from_section (struct objfile *objfile,
5061 const char *filename,
5062 struct dwarf2_section_info *section,
5063 mapped_debug_names &map)
5064 {
5065 if (section->empty ())
5066 return false;
5067
5068 /* Older elfutils strip versions could keep the section in the main
5069 executable while splitting it for the separate debug info file. */
5070 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5071 return false;
5072
5073 section->read (objfile);
5074
5075 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5076
5077 const gdb_byte *addr = section->buffer;
5078
5079 bfd *const abfd = section->get_bfd_owner ();
5080
5081 unsigned int bytes_read;
5082 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 map.dwarf5_is_dwarf64 = bytes_read != 4;
5086 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5087 if (bytes_read + length != section->size)
5088 {
5089 /* There may be multiple per-CU indices. */
5090 warning (_("Section .debug_names in %s length %s does not match "
5091 "section length %s, ignoring .debug_names."),
5092 filename, plongest (bytes_read + length),
5093 pulongest (section->size));
5094 return false;
5095 }
5096
5097 /* The version number. */
5098 uint16_t version = read_2_bytes (abfd, addr);
5099 addr += 2;
5100 if (version != 5)
5101 {
5102 warning (_("Section .debug_names in %s has unsupported version %d, "
5103 "ignoring .debug_names."),
5104 filename, version);
5105 return false;
5106 }
5107
5108 /* Padding. */
5109 uint16_t padding = read_2_bytes (abfd, addr);
5110 addr += 2;
5111 if (padding != 0)
5112 {
5113 warning (_("Section .debug_names in %s has unsupported padding %d, "
5114 "ignoring .debug_names."),
5115 filename, padding);
5116 return false;
5117 }
5118
5119 /* comp_unit_count - The number of CUs in the CU list. */
5120 map.cu_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* local_type_unit_count - The number of TUs in the local TU
5124 list. */
5125 map.tu_count = read_4_bytes (abfd, addr);
5126 addr += 4;
5127
5128 /* foreign_type_unit_count - The number of TUs in the foreign TU
5129 list. */
5130 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5131 addr += 4;
5132 if (foreign_tu_count != 0)
5133 {
5134 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5135 "ignoring .debug_names."),
5136 filename, static_cast<unsigned long> (foreign_tu_count));
5137 return false;
5138 }
5139
5140 /* bucket_count - The number of hash buckets in the hash lookup
5141 table. */
5142 map.bucket_count = read_4_bytes (abfd, addr);
5143 addr += 4;
5144
5145 /* name_count - The number of unique names in the index. */
5146 map.name_count = read_4_bytes (abfd, addr);
5147 addr += 4;
5148
5149 /* abbrev_table_size - The size in bytes of the abbreviations
5150 table. */
5151 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5152 addr += 4;
5153
5154 /* augmentation_string_size - The size in bytes of the augmentation
5155 string. This value is rounded up to a multiple of 4. */
5156 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5157 addr += 4;
5158 map.augmentation_is_gdb = ((augmentation_string_size
5159 == sizeof (dwarf5_augmentation))
5160 && memcmp (addr, dwarf5_augmentation,
5161 sizeof (dwarf5_augmentation)) == 0);
5162 augmentation_string_size += (-augmentation_string_size) & 3;
5163 addr += augmentation_string_size;
5164
5165 /* List of CUs */
5166 map.cu_table_reordered = addr;
5167 addr += map.cu_count * map.offset_size;
5168
5169 /* List of Local TUs */
5170 map.tu_table_reordered = addr;
5171 addr += map.tu_count * map.offset_size;
5172
5173 /* Hash Lookup Table */
5174 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5175 addr += map.bucket_count * 4;
5176 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5177 addr += map.name_count * 4;
5178
5179 /* Name Table */
5180 map.name_table_string_offs_reordered = addr;
5181 addr += map.name_count * map.offset_size;
5182 map.name_table_entry_offs_reordered = addr;
5183 addr += map.name_count * map.offset_size;
5184
5185 const gdb_byte *abbrev_table_start = addr;
5186 for (;;)
5187 {
5188 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5189 addr += bytes_read;
5190 if (index_num == 0)
5191 break;
5192
5193 const auto insertpair
5194 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5195 if (!insertpair.second)
5196 {
5197 warning (_("Section .debug_names in %s has duplicate index %s, "
5198 "ignoring .debug_names."),
5199 filename, pulongest (index_num));
5200 return false;
5201 }
5202 mapped_debug_names::index_val &indexval = insertpair.first->second;
5203 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5204 addr += bytes_read;
5205
5206 for (;;)
5207 {
5208 mapped_debug_names::index_val::attr attr;
5209 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5210 addr += bytes_read;
5211 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5212 addr += bytes_read;
5213 if (attr.form == DW_FORM_implicit_const)
5214 {
5215 attr.implicit_const = read_signed_leb128 (abfd, addr,
5216 &bytes_read);
5217 addr += bytes_read;
5218 }
5219 if (attr.dw_idx == 0 && attr.form == 0)
5220 break;
5221 indexval.attr_vec.push_back (std::move (attr));
5222 }
5223 }
5224 if (addr != abbrev_table_start + abbrev_table_size)
5225 {
5226 warning (_("Section .debug_names in %s has abbreviation_table "
5227 "of size %s vs. written as %u, ignoring .debug_names."),
5228 filename, plongest (addr - abbrev_table_start),
5229 abbrev_table_size);
5230 return false;
5231 }
5232 map.entry_pool = addr;
5233
5234 return true;
5235 }
5236
5237 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5238 list. */
5239
5240 static void
5241 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5242 const mapped_debug_names &map,
5243 dwarf2_section_info &section,
5244 bool is_dwz)
5245 {
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
5263 dwarf2_per_cu_data *per_cu
5264 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5265 sect_off_prev, length);
5266 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270 }
5271
5272 /* Read the CU list from the mapped index, and use it to create all
5273 the CU objects for this dwarf2_per_objfile. */
5274
5275 static void
5276 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279 {
5280 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5281 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5282
5283 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5284 dwarf2_per_objfile->info,
5285 false /* is_dwz */);
5286
5287 if (dwz_map.cu_count == 0)
5288 return;
5289
5290 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5291 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5292 true /* is_dwz */);
5293 }
5294
5295 /* Read .debug_names. If everything went ok, initialize the "quick"
5296 elements of all the CUs and return true. Otherwise, return false. */
5297
5298 static bool
5299 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5300 {
5301 std::unique_ptr<mapped_debug_names> map
5302 (new mapped_debug_names (dwarf2_per_objfile));
5303 mapped_debug_names dwz_map (dwarf2_per_objfile);
5304 struct objfile *objfile = dwarf2_per_objfile->objfile;
5305
5306 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5307 &dwarf2_per_objfile->debug_names,
5308 *map))
5309 return false;
5310
5311 /* Don't use the index if it's empty. */
5312 if (map->name_count == 0)
5313 return false;
5314
5315 /* If there is a .dwz file, read it so we can get its CU list as
5316 well. */
5317 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5318 if (dwz != NULL)
5319 {
5320 if (!read_debug_names_from_section (objfile,
5321 bfd_get_filename (dwz->dwz_bfd.get ()),
5322 &dwz->debug_names, dwz_map))
5323 {
5324 warning (_("could not read '.debug_names' section from %s; skipping"),
5325 bfd_get_filename (dwz->dwz_bfd.get ()));
5326 return false;
5327 }
5328 }
5329
5330 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5331
5332 if (map->tu_count != 0)
5333 {
5334 /* We can only handle a single .debug_types when we have an
5335 index. */
5336 if (dwarf2_per_objfile->types.size () != 1)
5337 return false;
5338
5339 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5340
5341 create_signatured_type_table_from_debug_names
5342 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5343 }
5344
5345 create_addrmap_from_aranges (dwarf2_per_objfile,
5346 &dwarf2_per_objfile->debug_aranges);
5347
5348 dwarf2_per_objfile->debug_names_table = std::move (map);
5349 dwarf2_per_objfile->using_index = 1;
5350 dwarf2_per_objfile->quick_file_names_table =
5351 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5352
5353 return true;
5354 }
5355
5356 /* Type used to manage iterating over all CUs looking for a symbol for
5357 .debug_names. */
5358
5359 class dw2_debug_names_iterator
5360 {
5361 public:
5362 dw2_debug_names_iterator (const mapped_debug_names &map,
5363 gdb::optional<block_enum> block_index,
5364 domain_enum domain,
5365 const char *name)
5366 : m_map (map), m_block_index (block_index), m_domain (domain),
5367 m_addr (find_vec_in_debug_names (map, name))
5368 {}
5369
5370 dw2_debug_names_iterator (const mapped_debug_names &map,
5371 search_domain search, uint32_t namei)
5372 : m_map (map),
5373 m_search (search),
5374 m_addr (find_vec_in_debug_names (map, namei))
5375 {}
5376
5377 dw2_debug_names_iterator (const mapped_debug_names &map,
5378 block_enum block_index, domain_enum domain,
5379 uint32_t namei)
5380 : m_map (map), m_block_index (block_index), m_domain (domain),
5381 m_addr (find_vec_in_debug_names (map, namei))
5382 {}
5383
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387 private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5389 const char *name);
5390 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5391 uint32_t namei);
5392
5393 /* The internalized form of .debug_names. */
5394 const mapped_debug_names &m_map;
5395
5396 /* If set, only look for symbols that match that block. Valid values are
5397 GLOBAL_BLOCK and STATIC_BLOCK. */
5398 const gdb::optional<block_enum> m_block_index;
5399
5400 /* The kind of symbol we're looking for. */
5401 const domain_enum m_domain = UNDEF_DOMAIN;
5402 const search_domain m_search = ALL_DOMAIN;
5403
5404 /* The list of CUs from the index entry of the symbol, or NULL if
5405 not found. */
5406 const gdb_byte *m_addr;
5407 };
5408
5409 const char *
5410 mapped_debug_names::namei_to_name (uint32_t namei) const
5411 {
5412 const ULONGEST namei_string_offs
5413 = extract_unsigned_integer ((name_table_string_offs_reordered
5414 + namei * offset_size),
5415 offset_size,
5416 dwarf5_byte_order);
5417 return read_indirect_string_at_offset
5418 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5419 }
5420
5421 /* Find a slot in .debug_names for the object named NAME. If NAME is
5422 found, return pointer to its pool data. If NAME cannot be found,
5423 return NULL. */
5424
5425 const gdb_byte *
5426 dw2_debug_names_iterator::find_vec_in_debug_names
5427 (const mapped_debug_names &map, const char *name)
5428 {
5429 int (*cmp) (const char *, const char *);
5430
5431 gdb::unique_xmalloc_ptr<char> without_params;
5432 if (current_language->la_language == language_cplus
5433 || current_language->la_language == language_fortran
5434 || current_language->la_language == language_d)
5435 {
5436 /* NAME is already canonical. Drop any qualifiers as
5437 .debug_names does not contain any. */
5438
5439 if (strchr (name, '(') != NULL)
5440 {
5441 without_params = cp_remove_params (name);
5442 if (without_params != NULL)
5443 name = without_params.get ();
5444 }
5445 }
5446
5447 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5448
5449 const uint32_t full_hash = dwarf5_djb_hash (name);
5450 uint32_t namei
5451 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5452 (map.bucket_table_reordered
5453 + (full_hash % map.bucket_count)), 4,
5454 map.dwarf5_byte_order);
5455 if (namei == 0)
5456 return NULL;
5457 --namei;
5458 if (namei >= map.name_count)
5459 {
5460 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5461 "[in module %s]"),
5462 namei, map.name_count,
5463 objfile_name (map.dwarf2_per_objfile->objfile));
5464 return NULL;
5465 }
5466
5467 for (;;)
5468 {
5469 const uint32_t namei_full_hash
5470 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5471 (map.hash_table_reordered + namei), 4,
5472 map.dwarf5_byte_order);
5473 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5474 return NULL;
5475
5476 if (full_hash == namei_full_hash)
5477 {
5478 const char *const namei_string = map.namei_to_name (namei);
5479
5480 #if 0 /* An expensive sanity check. */
5481 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5482 {
5483 complaint (_("Wrong .debug_names hash for string at index %u "
5484 "[in module %s]"),
5485 namei, objfile_name (dwarf2_per_objfile->objfile));
5486 return NULL;
5487 }
5488 #endif
5489
5490 if (cmp (namei_string, name) == 0)
5491 {
5492 const ULONGEST namei_entry_offs
5493 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5494 + namei * map.offset_size),
5495 map.offset_size, map.dwarf5_byte_order);
5496 return map.entry_pool + namei_entry_offs;
5497 }
5498 }
5499
5500 ++namei;
5501 if (namei >= map.name_count)
5502 return NULL;
5503 }
5504 }
5505
5506 const gdb_byte *
5507 dw2_debug_names_iterator::find_vec_in_debug_names
5508 (const mapped_debug_names &map, uint32_t namei)
5509 {
5510 if (namei >= map.name_count)
5511 {
5512 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5513 "[in module %s]"),
5514 namei, map.name_count,
5515 objfile_name (map.dwarf2_per_objfile->objfile));
5516 return NULL;
5517 }
5518
5519 const ULONGEST namei_entry_offs
5520 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5521 + namei * map.offset_size),
5522 map.offset_size, map.dwarf5_byte_order);
5523 return map.entry_pool + namei_entry_offs;
5524 }
5525
5526 /* See dw2_debug_names_iterator. */
5527
5528 dwarf2_per_cu_data *
5529 dw2_debug_names_iterator::next ()
5530 {
5531 if (m_addr == NULL)
5532 return NULL;
5533
5534 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5535 struct objfile *objfile = dwarf2_per_objfile->objfile;
5536 bfd *const abfd = objfile->obfd;
5537
5538 again:
5539
5540 unsigned int bytes_read;
5541 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5542 m_addr += bytes_read;
5543 if (abbrev == 0)
5544 return NULL;
5545
5546 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5547 if (indexval_it == m_map.abbrev_map.cend ())
5548 {
5549 complaint (_("Wrong .debug_names undefined abbrev code %s "
5550 "[in module %s]"),
5551 pulongest (abbrev), objfile_name (objfile));
5552 return NULL;
5553 }
5554 const mapped_debug_names::index_val &indexval = indexval_it->second;
5555 enum class symbol_linkage {
5556 unknown,
5557 static_,
5558 extern_,
5559 } symbol_linkage_ = symbol_linkage::unknown;
5560 dwarf2_per_cu_data *per_cu = NULL;
5561 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5562 {
5563 ULONGEST ull;
5564 switch (attr.form)
5565 {
5566 case DW_FORM_implicit_const:
5567 ull = attr.implicit_const;
5568 break;
5569 case DW_FORM_flag_present:
5570 ull = 1;
5571 break;
5572 case DW_FORM_udata:
5573 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5574 m_addr += bytes_read;
5575 break;
5576 default:
5577 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5578 dwarf_form_name (attr.form),
5579 objfile_name (objfile));
5580 return NULL;
5581 }
5582 switch (attr.dw_idx)
5583 {
5584 case DW_IDX_compile_unit:
5585 /* Don't crash on bad data. */
5586 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5587 {
5588 complaint (_(".debug_names entry has bad CU index %s"
5589 " [in module %s]"),
5590 pulongest (ull),
5591 objfile_name (dwarf2_per_objfile->objfile));
5592 continue;
5593 }
5594 per_cu = dwarf2_per_objfile->get_cutu (ull);
5595 break;
5596 case DW_IDX_type_unit:
5597 /* Don't crash on bad data. */
5598 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5599 {
5600 complaint (_(".debug_names entry has bad TU index %s"
5601 " [in module %s]"),
5602 pulongest (ull),
5603 objfile_name (dwarf2_per_objfile->objfile));
5604 continue;
5605 }
5606 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5607 break;
5608 case DW_IDX_GNU_internal:
5609 if (!m_map.augmentation_is_gdb)
5610 break;
5611 symbol_linkage_ = symbol_linkage::static_;
5612 break;
5613 case DW_IDX_GNU_external:
5614 if (!m_map.augmentation_is_gdb)
5615 break;
5616 symbol_linkage_ = symbol_linkage::extern_;
5617 break;
5618 }
5619 }
5620
5621 /* Skip if already read in. */
5622 if (per_cu->v.quick->compunit_symtab)
5623 goto again;
5624
5625 /* Check static vs global. */
5626 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5627 {
5628 const bool want_static = *m_block_index == STATIC_BLOCK;
5629 const bool symbol_is_static =
5630 symbol_linkage_ == symbol_linkage::static_;
5631 if (want_static != symbol_is_static)
5632 goto again;
5633 }
5634
5635 /* Match dw2_symtab_iter_next, symbol_kind
5636 and debug_names::psymbol_tag. */
5637 switch (m_domain)
5638 {
5639 case VAR_DOMAIN:
5640 switch (indexval.dwarf_tag)
5641 {
5642 case DW_TAG_variable:
5643 case DW_TAG_subprogram:
5644 /* Some types are also in VAR_DOMAIN. */
5645 case DW_TAG_typedef:
5646 case DW_TAG_structure_type:
5647 break;
5648 default:
5649 goto again;
5650 }
5651 break;
5652 case STRUCT_DOMAIN:
5653 switch (indexval.dwarf_tag)
5654 {
5655 case DW_TAG_typedef:
5656 case DW_TAG_structure_type:
5657 break;
5658 default:
5659 goto again;
5660 }
5661 break;
5662 case LABEL_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case 0:
5666 case DW_TAG_variable:
5667 break;
5668 default:
5669 goto again;
5670 }
5671 break;
5672 case MODULE_DOMAIN:
5673 switch (indexval.dwarf_tag)
5674 {
5675 case DW_TAG_module:
5676 break;
5677 default:
5678 goto again;
5679 }
5680 break;
5681 default:
5682 break;
5683 }
5684
5685 /* Match dw2_expand_symtabs_matching, symbol_kind and
5686 debug_names::psymbol_tag. */
5687 switch (m_search)
5688 {
5689 case VARIABLES_DOMAIN:
5690 switch (indexval.dwarf_tag)
5691 {
5692 case DW_TAG_variable:
5693 break;
5694 default:
5695 goto again;
5696 }
5697 break;
5698 case FUNCTIONS_DOMAIN:
5699 switch (indexval.dwarf_tag)
5700 {
5701 case DW_TAG_subprogram:
5702 break;
5703 default:
5704 goto again;
5705 }
5706 break;
5707 case TYPES_DOMAIN:
5708 switch (indexval.dwarf_tag)
5709 {
5710 case DW_TAG_typedef:
5711 case DW_TAG_structure_type:
5712 break;
5713 default:
5714 goto again;
5715 }
5716 break;
5717 case MODULES_DOMAIN:
5718 switch (indexval.dwarf_tag)
5719 {
5720 case DW_TAG_module:
5721 break;
5722 default:
5723 goto again;
5724 }
5725 default:
5726 break;
5727 }
5728
5729 return per_cu;
5730 }
5731
5732 static struct compunit_symtab *
5733 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5734 const char *name, domain_enum domain)
5735 {
5736 struct dwarf2_per_objfile *dwarf2_per_objfile
5737 = get_dwarf2_per_objfile (objfile);
5738
5739 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5740 if (!mapp)
5741 {
5742 /* index is NULL if OBJF_READNOW. */
5743 return NULL;
5744 }
5745 const auto &map = *mapp;
5746
5747 dw2_debug_names_iterator iter (map, block_index, domain, name);
5748
5749 struct compunit_symtab *stab_best = NULL;
5750 struct dwarf2_per_cu_data *per_cu;
5751 while ((per_cu = iter.next ()) != NULL)
5752 {
5753 struct symbol *sym, *with_opaque = NULL;
5754 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5755 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5756 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5757
5758 sym = block_find_symbol (block, name, domain,
5759 block_find_non_opaque_type_preferred,
5760 &with_opaque);
5761
5762 /* Some caution must be observed with overloaded functions and
5763 methods, since the index will not contain any overload
5764 information (but NAME might contain it). */
5765
5766 if (sym != NULL
5767 && strcmp_iw (sym->search_name (), name) == 0)
5768 return stab;
5769 if (with_opaque != NULL
5770 && strcmp_iw (with_opaque->search_name (), name) == 0)
5771 stab_best = stab;
5772
5773 /* Keep looking through other CUs. */
5774 }
5775
5776 return stab_best;
5777 }
5778
5779 /* This dumps minimal information about .debug_names. It is called
5780 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5781 uses this to verify that .debug_names has been loaded. */
5782
5783 static void
5784 dw2_debug_names_dump (struct objfile *objfile)
5785 {
5786 struct dwarf2_per_objfile *dwarf2_per_objfile
5787 = get_dwarf2_per_objfile (objfile);
5788
5789 gdb_assert (dwarf2_per_objfile->using_index);
5790 printf_filtered (".debug_names:");
5791 if (dwarf2_per_objfile->debug_names_table)
5792 printf_filtered (" exists\n");
5793 else
5794 printf_filtered (" faked for \"readnow\"\n");
5795 printf_filtered ("\n");
5796 }
5797
5798 static void
5799 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5800 const char *func_name)
5801 {
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
5804
5805 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5806 if (dwarf2_per_objfile->debug_names_table)
5807 {
5808 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5809
5810 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5811
5812 struct dwarf2_per_cu_data *per_cu;
5813 while ((per_cu = iter.next ()) != NULL)
5814 dw2_instantiate_symtab (per_cu, false);
5815 }
5816 }
5817
5818 static void
5819 dw2_debug_names_map_matching_symbols
5820 (struct objfile *objfile,
5821 const lookup_name_info &name, domain_enum domain,
5822 int global,
5823 gdb::function_view<symbol_found_callback_ftype> callback,
5824 symbol_compare_ftype *ordered_compare)
5825 {
5826 struct dwarf2_per_objfile *dwarf2_per_objfile
5827 = get_dwarf2_per_objfile (objfile);
5828
5829 /* debug_names_table is NULL if OBJF_READNOW. */
5830 if (!dwarf2_per_objfile->debug_names_table)
5831 return;
5832
5833 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5834 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5835
5836 const char *match_name = name.ada ().lookup_name ().c_str ();
5837 auto matcher = [&] (const char *symname)
5838 {
5839 if (ordered_compare == nullptr)
5840 return true;
5841 return ordered_compare (symname, match_name) == 0;
5842 };
5843
5844 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5845 [&] (offset_type namei)
5846 {
5847 /* The name was matched, now expand corresponding CUs that were
5848 marked. */
5849 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5850
5851 struct dwarf2_per_cu_data *per_cu;
5852 while ((per_cu = iter.next ()) != NULL)
5853 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5854 return true;
5855 });
5856
5857 /* It's a shame we couldn't do this inside the
5858 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5859 that have already been expanded. Instead, this loop matches what
5860 the psymtab code does. */
5861 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5862 {
5863 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5864 if (cust != nullptr)
5865 {
5866 const struct block *block
5867 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5868 if (!iterate_over_symbols_terminated (block, name,
5869 domain, callback))
5870 break;
5871 }
5872 }
5873 }
5874
5875 static void
5876 dw2_debug_names_expand_symtabs_matching
5877 (struct objfile *objfile,
5878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5879 const lookup_name_info &lookup_name,
5880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5882 enum search_domain kind)
5883 {
5884 struct dwarf2_per_objfile *dwarf2_per_objfile
5885 = get_dwarf2_per_objfile (objfile);
5886
5887 /* debug_names_table is NULL if OBJF_READNOW. */
5888 if (!dwarf2_per_objfile->debug_names_table)
5889 return;
5890
5891 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5892
5893 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5894
5895 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5896 symbol_matcher,
5897 kind, [&] (offset_type namei)
5898 {
5899 /* The name was matched, now expand corresponding CUs that were
5900 marked. */
5901 dw2_debug_names_iterator iter (map, kind, namei);
5902
5903 struct dwarf2_per_cu_data *per_cu;
5904 while ((per_cu = iter.next ()) != NULL)
5905 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5906 expansion_notify);
5907 return true;
5908 });
5909 }
5910
5911 const struct quick_symbol_functions dwarf2_debug_names_functions =
5912 {
5913 dw2_has_symbols,
5914 dw2_find_last_source_symtab,
5915 dw2_forget_cached_source_info,
5916 dw2_map_symtabs_matching_filename,
5917 dw2_debug_names_lookup_symbol,
5918 dw2_print_stats,
5919 dw2_debug_names_dump,
5920 dw2_debug_names_expand_symtabs_for_function,
5921 dw2_expand_all_symtabs,
5922 dw2_expand_symtabs_with_fullname,
5923 dw2_debug_names_map_matching_symbols,
5924 dw2_debug_names_expand_symtabs_matching,
5925 dw2_find_pc_sect_compunit_symtab,
5926 NULL,
5927 dw2_map_symbol_filenames
5928 };
5929
5930 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5931 to either a dwarf2_per_objfile or dwz_file object. */
5932
5933 template <typename T>
5934 static gdb::array_view<const gdb_byte>
5935 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5936 {
5937 dwarf2_section_info *section = &section_owner->gdb_index;
5938
5939 if (section->empty ())
5940 return {};
5941
5942 /* Older elfutils strip versions could keep the section in the main
5943 executable while splitting it for the separate debug info file. */
5944 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5945 return {};
5946
5947 section->read (obj);
5948
5949 /* dwarf2_section_info::size is a bfd_size_type, while
5950 gdb::array_view works with size_t. On 32-bit hosts, with
5951 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5952 is 32-bit. So we need an explicit narrowing conversion here.
5953 This is fine, because it's impossible to allocate or mmap an
5954 array/buffer larger than what size_t can represent. */
5955 return gdb::make_array_view (section->buffer, section->size);
5956 }
5957
5958 /* Lookup the index cache for the contents of the index associated to
5959 DWARF2_OBJ. */
5960
5961 static gdb::array_view<const gdb_byte>
5962 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5963 {
5964 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5965 if (build_id == nullptr)
5966 return {};
5967
5968 return global_index_cache.lookup_gdb_index (build_id,
5969 &dwarf2_obj->index_cache_res);
5970 }
5971
5972 /* Same as the above, but for DWZ. */
5973
5974 static gdb::array_view<const gdb_byte>
5975 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5976 {
5977 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5978 if (build_id == nullptr)
5979 return {};
5980
5981 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5982 }
5983
5984 /* See symfile.h. */
5985
5986 bool
5987 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5988 {
5989 struct dwarf2_per_objfile *dwarf2_per_objfile
5990 = get_dwarf2_per_objfile (objfile);
5991
5992 /* If we're about to read full symbols, don't bother with the
5993 indices. In this case we also don't care if some other debug
5994 format is making psymtabs, because they are all about to be
5995 expanded anyway. */
5996 if ((objfile->flags & OBJF_READNOW))
5997 {
5998 dwarf2_per_objfile->using_index = 1;
5999 create_all_comp_units (dwarf2_per_objfile);
6000 create_all_type_units (dwarf2_per_objfile);
6001 dwarf2_per_objfile->quick_file_names_table
6002 = create_quick_file_names_table
6003 (dwarf2_per_objfile->all_comp_units.size ());
6004
6005 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6006 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6007 {
6008 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6009
6010 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6011 struct dwarf2_per_cu_quick_data);
6012 }
6013
6014 /* Return 1 so that gdb sees the "quick" functions. However,
6015 these functions will be no-ops because we will have expanded
6016 all symtabs. */
6017 *index_kind = dw_index_kind::GDB_INDEX;
6018 return true;
6019 }
6020
6021 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6022 {
6023 *index_kind = dw_index_kind::DEBUG_NAMES;
6024 return true;
6025 }
6026
6027 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6028 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6029 get_gdb_index_contents_from_section<dwz_file>))
6030 {
6031 *index_kind = dw_index_kind::GDB_INDEX;
6032 return true;
6033 }
6034
6035 /* ... otherwise, try to find the index in the index cache. */
6036 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6037 get_gdb_index_contents_from_cache,
6038 get_gdb_index_contents_from_cache_dwz))
6039 {
6040 global_index_cache.hit ();
6041 *index_kind = dw_index_kind::GDB_INDEX;
6042 return true;
6043 }
6044
6045 global_index_cache.miss ();
6046 return false;
6047 }
6048
6049 \f
6050
6051 /* Build a partial symbol table. */
6052
6053 void
6054 dwarf2_build_psymtabs (struct objfile *objfile)
6055 {
6056 struct dwarf2_per_objfile *dwarf2_per_objfile
6057 = get_dwarf2_per_objfile (objfile);
6058
6059 init_psymbol_list (objfile, 1024);
6060
6061 try
6062 {
6063 /* This isn't really ideal: all the data we allocate on the
6064 objfile's obstack is still uselessly kept around. However,
6065 freeing it seems unsafe. */
6066 psymtab_discarder psymtabs (objfile);
6067 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6068 psymtabs.keep ();
6069
6070 /* (maybe) store an index in the cache. */
6071 global_index_cache.store (dwarf2_per_objfile);
6072 }
6073 catch (const gdb_exception_error &except)
6074 {
6075 exception_print (gdb_stderr, except);
6076 }
6077 }
6078
6079 /* Return the total length of the CU described by HEADER. */
6080
6081 static unsigned int
6082 get_cu_length (const struct comp_unit_head *header)
6083 {
6084 return header->initial_length_size + header->length;
6085 }
6086
6087 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6088
6089 static inline bool
6090 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6091 {
6092 sect_offset bottom = cu_header->sect_off;
6093 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6094
6095 return sect_off >= bottom && sect_off < top;
6096 }
6097
6098 /* Find the base address of the compilation unit for range lists and
6099 location lists. It will normally be specified by DW_AT_low_pc.
6100 In DWARF-3 draft 4, the base address could be overridden by
6101 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6102 compilation units with discontinuous ranges. */
6103
6104 static void
6105 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6106 {
6107 struct attribute *attr;
6108
6109 cu->base_known = 0;
6110 cu->base_address = 0;
6111
6112 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6113 if (attr != nullptr)
6114 {
6115 cu->base_address = attr->value_as_address ();
6116 cu->base_known = 1;
6117 }
6118 else
6119 {
6120 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6121 if (attr != nullptr)
6122 {
6123 cu->base_address = attr->value_as_address ();
6124 cu->base_known = 1;
6125 }
6126 }
6127 }
6128
6129 /* Read in the comp unit header information from the debug_info at info_ptr.
6130 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6131 NOTE: This leaves members offset, first_die_offset to be filled in
6132 by the caller. */
6133
6134 static const gdb_byte *
6135 read_comp_unit_head (struct comp_unit_head *cu_header,
6136 const gdb_byte *info_ptr,
6137 struct dwarf2_section_info *section,
6138 rcuh_kind section_kind)
6139 {
6140 int signed_addr;
6141 unsigned int bytes_read;
6142 const char *filename = section->get_file_name ();
6143 bfd *abfd = section->get_bfd_owner ();
6144
6145 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6146 cu_header->initial_length_size = bytes_read;
6147 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6148 info_ptr += bytes_read;
6149 cu_header->version = read_2_bytes (abfd, info_ptr);
6150 if (cu_header->version < 2 || cu_header->version > 5)
6151 error (_("Dwarf Error: wrong version in compilation unit header "
6152 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6153 cu_header->version, filename);
6154 info_ptr += 2;
6155 if (cu_header->version < 5)
6156 switch (section_kind)
6157 {
6158 case rcuh_kind::COMPILE:
6159 cu_header->unit_type = DW_UT_compile;
6160 break;
6161 case rcuh_kind::TYPE:
6162 cu_header->unit_type = DW_UT_type;
6163 break;
6164 default:
6165 internal_error (__FILE__, __LINE__,
6166 _("read_comp_unit_head: invalid section_kind"));
6167 }
6168 else
6169 {
6170 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6171 (read_1_byte (abfd, info_ptr));
6172 info_ptr += 1;
6173 switch (cu_header->unit_type)
6174 {
6175 case DW_UT_compile:
6176 case DW_UT_partial:
6177 case DW_UT_skeleton:
6178 case DW_UT_split_compile:
6179 if (section_kind != rcuh_kind::COMPILE)
6180 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6181 "(is %s, should be %s) [in module %s]"),
6182 dwarf_unit_type_name (cu_header->unit_type),
6183 dwarf_unit_type_name (DW_UT_type), filename);
6184 break;
6185 case DW_UT_type:
6186 case DW_UT_split_type:
6187 section_kind = rcuh_kind::TYPE;
6188 break;
6189 default:
6190 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6191 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6192 "[in module %s]"), cu_header->unit_type,
6193 dwarf_unit_type_name (DW_UT_compile),
6194 dwarf_unit_type_name (DW_UT_skeleton),
6195 dwarf_unit_type_name (DW_UT_split_compile),
6196 dwarf_unit_type_name (DW_UT_type),
6197 dwarf_unit_type_name (DW_UT_split_type), filename);
6198 }
6199
6200 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6201 info_ptr += 1;
6202 }
6203 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6204 cu_header,
6205 &bytes_read);
6206 info_ptr += bytes_read;
6207 if (cu_header->version < 5)
6208 {
6209 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6210 info_ptr += 1;
6211 }
6212 signed_addr = bfd_get_sign_extend_vma (abfd);
6213 if (signed_addr < 0)
6214 internal_error (__FILE__, __LINE__,
6215 _("read_comp_unit_head: dwarf from non elf file"));
6216 cu_header->signed_addr_p = signed_addr;
6217
6218 bool header_has_signature = section_kind == rcuh_kind::TYPE
6219 || cu_header->unit_type == DW_UT_skeleton
6220 || cu_header->unit_type == DW_UT_split_compile;
6221
6222 if (header_has_signature)
6223 {
6224 cu_header->signature = read_8_bytes (abfd, info_ptr);
6225 info_ptr += 8;
6226 }
6227
6228 if (section_kind == rcuh_kind::TYPE)
6229 {
6230 LONGEST type_offset;
6231 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6232 info_ptr += bytes_read;
6233 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6234 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6235 error (_("Dwarf Error: Too big type_offset in compilation unit "
6236 "header (is %s) [in module %s]"), plongest (type_offset),
6237 filename);
6238 }
6239
6240 return info_ptr;
6241 }
6242
6243 /* Helper function that returns the proper abbrev section for
6244 THIS_CU. */
6245
6246 static struct dwarf2_section_info *
6247 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6248 {
6249 struct dwarf2_section_info *abbrev;
6250 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6251
6252 if (this_cu->is_dwz)
6253 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6254 else
6255 abbrev = &dwarf2_per_objfile->abbrev;
6256
6257 return abbrev;
6258 }
6259
6260 /* Subroutine of read_and_check_comp_unit_head and
6261 read_and_check_type_unit_head to simplify them.
6262 Perform various error checking on the header. */
6263
6264 static void
6265 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6266 struct comp_unit_head *header,
6267 struct dwarf2_section_info *section,
6268 struct dwarf2_section_info *abbrev_section)
6269 {
6270 const char *filename = section->get_file_name ();
6271
6272 if (to_underlying (header->abbrev_sect_off)
6273 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6274 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6275 "(offset %s + 6) [in module %s]"),
6276 sect_offset_str (header->abbrev_sect_off),
6277 sect_offset_str (header->sect_off),
6278 filename);
6279
6280 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6281 avoid potential 32-bit overflow. */
6282 if (((ULONGEST) header->sect_off + get_cu_length (header))
6283 > section->size)
6284 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6285 "(offset %s + 0) [in module %s]"),
6286 header->length, sect_offset_str (header->sect_off),
6287 filename);
6288 }
6289
6290 /* Read in a CU/TU header and perform some basic error checking.
6291 The contents of the header are stored in HEADER.
6292 The result is a pointer to the start of the first DIE. */
6293
6294 static const gdb_byte *
6295 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6296 struct comp_unit_head *header,
6297 struct dwarf2_section_info *section,
6298 struct dwarf2_section_info *abbrev_section,
6299 const gdb_byte *info_ptr,
6300 rcuh_kind section_kind)
6301 {
6302 const gdb_byte *beg_of_comp_unit = info_ptr;
6303
6304 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6305
6306 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6307
6308 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6309
6310 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6311 abbrev_section);
6312
6313 return info_ptr;
6314 }
6315
6316 /* Fetch the abbreviation table offset from a comp or type unit header. */
6317
6318 static sect_offset
6319 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6320 struct dwarf2_section_info *section,
6321 sect_offset sect_off)
6322 {
6323 bfd *abfd = section->get_bfd_owner ();
6324 const gdb_byte *info_ptr;
6325 unsigned int initial_length_size, offset_size;
6326 uint16_t version;
6327
6328 section->read (dwarf2_per_objfile->objfile);
6329 info_ptr = section->buffer + to_underlying (sect_off);
6330 read_initial_length (abfd, info_ptr, &initial_length_size);
6331 offset_size = initial_length_size == 4 ? 4 : 8;
6332 info_ptr += initial_length_size;
6333
6334 version = read_2_bytes (abfd, info_ptr);
6335 info_ptr += 2;
6336 if (version >= 5)
6337 {
6338 /* Skip unit type and address size. */
6339 info_ptr += 2;
6340 }
6341
6342 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6343 }
6344
6345 /* Allocate a new partial symtab for file named NAME and mark this new
6346 partial symtab as being an include of PST. */
6347
6348 static void
6349 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6350 struct objfile *objfile)
6351 {
6352 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6353
6354 if (!IS_ABSOLUTE_PATH (subpst->filename))
6355 {
6356 /* It shares objfile->objfile_obstack. */
6357 subpst->dirname = pst->dirname;
6358 }
6359
6360 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6361 subpst->dependencies[0] = pst;
6362 subpst->number_of_dependencies = 1;
6363
6364 /* No private part is necessary for include psymtabs. This property
6365 can be used to differentiate between such include psymtabs and
6366 the regular ones. */
6367 subpst->per_cu_data = nullptr;
6368 }
6369
6370 /* Read the Line Number Program data and extract the list of files
6371 included by the source file represented by PST. Build an include
6372 partial symtab for each of these included files. */
6373
6374 static void
6375 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6376 struct die_info *die,
6377 dwarf2_psymtab *pst)
6378 {
6379 line_header_up lh;
6380 struct attribute *attr;
6381
6382 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6383 if (attr != nullptr)
6384 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6385 if (lh == NULL)
6386 return; /* No linetable, so no includes. */
6387
6388 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6389 that we pass in the raw text_low here; that is ok because we're
6390 only decoding the line table to make include partial symtabs, and
6391 so the addresses aren't really used. */
6392 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6393 pst->raw_text_low (), 1);
6394 }
6395
6396 static hashval_t
6397 hash_signatured_type (const void *item)
6398 {
6399 const struct signatured_type *sig_type
6400 = (const struct signatured_type *) item;
6401
6402 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6403 return sig_type->signature;
6404 }
6405
6406 static int
6407 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6408 {
6409 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6410 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6411
6412 return lhs->signature == rhs->signature;
6413 }
6414
6415 /* Allocate a hash table for signatured types. */
6416
6417 static htab_t
6418 allocate_signatured_type_table (struct objfile *objfile)
6419 {
6420 return htab_create_alloc_ex (41,
6421 hash_signatured_type,
6422 eq_signatured_type,
6423 NULL,
6424 &objfile->objfile_obstack,
6425 hashtab_obstack_allocate,
6426 dummy_obstack_deallocate);
6427 }
6428
6429 /* A helper function to add a signatured type CU to a table. */
6430
6431 static int
6432 add_signatured_type_cu_to_table (void **slot, void *datum)
6433 {
6434 struct signatured_type *sigt = (struct signatured_type *) *slot;
6435 std::vector<signatured_type *> *all_type_units
6436 = (std::vector<signatured_type *> *) datum;
6437
6438 all_type_units->push_back (sigt);
6439
6440 return 1;
6441 }
6442
6443 /* A helper for create_debug_types_hash_table. Read types from SECTION
6444 and fill them into TYPES_HTAB. It will process only type units,
6445 therefore DW_UT_type. */
6446
6447 static void
6448 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6449 struct dwo_file *dwo_file,
6450 dwarf2_section_info *section, htab_t &types_htab,
6451 rcuh_kind section_kind)
6452 {
6453 struct objfile *objfile = dwarf2_per_objfile->objfile;
6454 struct dwarf2_section_info *abbrev_section;
6455 bfd *abfd;
6456 const gdb_byte *info_ptr, *end_ptr;
6457
6458 abbrev_section = (dwo_file != NULL
6459 ? &dwo_file->sections.abbrev
6460 : &dwarf2_per_objfile->abbrev);
6461
6462 if (dwarf_read_debug)
6463 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6464 section->get_name (),
6465 abbrev_section->get_file_name ());
6466
6467 section->read (objfile);
6468 info_ptr = section->buffer;
6469
6470 if (info_ptr == NULL)
6471 return;
6472
6473 /* We can't set abfd until now because the section may be empty or
6474 not present, in which case the bfd is unknown. */
6475 abfd = section->get_bfd_owner ();
6476
6477 /* We don't use cutu_reader here because we don't need to read
6478 any dies: the signature is in the header. */
6479
6480 end_ptr = info_ptr + section->size;
6481 while (info_ptr < end_ptr)
6482 {
6483 struct signatured_type *sig_type;
6484 struct dwo_unit *dwo_tu;
6485 void **slot;
6486 const gdb_byte *ptr = info_ptr;
6487 struct comp_unit_head header;
6488 unsigned int length;
6489
6490 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6491
6492 /* Initialize it due to a false compiler warning. */
6493 header.signature = -1;
6494 header.type_cu_offset_in_tu = (cu_offset) -1;
6495
6496 /* We need to read the type's signature in order to build the hash
6497 table, but we don't need anything else just yet. */
6498
6499 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6500 abbrev_section, ptr, section_kind);
6501
6502 length = get_cu_length (&header);
6503
6504 /* Skip dummy type units. */
6505 if (ptr >= info_ptr + length
6506 || peek_abbrev_code (abfd, ptr) == 0
6507 || header.unit_type != DW_UT_type)
6508 {
6509 info_ptr += length;
6510 continue;
6511 }
6512
6513 if (types_htab == NULL)
6514 {
6515 if (dwo_file)
6516 types_htab = allocate_dwo_unit_table (objfile);
6517 else
6518 types_htab = allocate_signatured_type_table (objfile);
6519 }
6520
6521 if (dwo_file)
6522 {
6523 sig_type = NULL;
6524 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6525 struct dwo_unit);
6526 dwo_tu->dwo_file = dwo_file;
6527 dwo_tu->signature = header.signature;
6528 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6529 dwo_tu->section = section;
6530 dwo_tu->sect_off = sect_off;
6531 dwo_tu->length = length;
6532 }
6533 else
6534 {
6535 /* N.B.: type_offset is not usable if this type uses a DWO file.
6536 The real type_offset is in the DWO file. */
6537 dwo_tu = NULL;
6538 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6539 struct signatured_type);
6540 sig_type->signature = header.signature;
6541 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6542 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6543 sig_type->per_cu.is_debug_types = 1;
6544 sig_type->per_cu.section = section;
6545 sig_type->per_cu.sect_off = sect_off;
6546 sig_type->per_cu.length = length;
6547 }
6548
6549 slot = htab_find_slot (types_htab,
6550 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6551 INSERT);
6552 gdb_assert (slot != NULL);
6553 if (*slot != NULL)
6554 {
6555 sect_offset dup_sect_off;
6556
6557 if (dwo_file)
6558 {
6559 const struct dwo_unit *dup_tu
6560 = (const struct dwo_unit *) *slot;
6561
6562 dup_sect_off = dup_tu->sect_off;
6563 }
6564 else
6565 {
6566 const struct signatured_type *dup_tu
6567 = (const struct signatured_type *) *slot;
6568
6569 dup_sect_off = dup_tu->per_cu.sect_off;
6570 }
6571
6572 complaint (_("debug type entry at offset %s is duplicate to"
6573 " the entry at offset %s, signature %s"),
6574 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6575 hex_string (header.signature));
6576 }
6577 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6578
6579 if (dwarf_read_debug > 1)
6580 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6581 sect_offset_str (sect_off),
6582 hex_string (header.signature));
6583
6584 info_ptr += length;
6585 }
6586 }
6587
6588 /* Create the hash table of all entries in the .debug_types
6589 (or .debug_types.dwo) section(s).
6590 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6591 otherwise it is NULL.
6592
6593 The result is a pointer to the hash table or NULL if there are no types.
6594
6595 Note: This function processes DWO files only, not DWP files. */
6596
6597 static void
6598 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6599 struct dwo_file *dwo_file,
6600 gdb::array_view<dwarf2_section_info> type_sections,
6601 htab_t &types_htab)
6602 {
6603 for (dwarf2_section_info &section : type_sections)
6604 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6605 types_htab, rcuh_kind::TYPE);
6606 }
6607
6608 /* Create the hash table of all entries in the .debug_types section,
6609 and initialize all_type_units.
6610 The result is zero if there is an error (e.g. missing .debug_types section),
6611 otherwise non-zero. */
6612
6613 static int
6614 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6615 {
6616 htab_t types_htab = NULL;
6617
6618 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6619 &dwarf2_per_objfile->info, types_htab,
6620 rcuh_kind::COMPILE);
6621 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6622 dwarf2_per_objfile->types, types_htab);
6623 if (types_htab == NULL)
6624 {
6625 dwarf2_per_objfile->signatured_types = NULL;
6626 return 0;
6627 }
6628
6629 dwarf2_per_objfile->signatured_types = types_htab;
6630
6631 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6632 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6633
6634 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6635 &dwarf2_per_objfile->all_type_units);
6636
6637 return 1;
6638 }
6639
6640 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6641 If SLOT is non-NULL, it is the entry to use in the hash table.
6642 Otherwise we find one. */
6643
6644 static struct signatured_type *
6645 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6646 void **slot)
6647 {
6648 struct objfile *objfile = dwarf2_per_objfile->objfile;
6649
6650 if (dwarf2_per_objfile->all_type_units.size ()
6651 == dwarf2_per_objfile->all_type_units.capacity ())
6652 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6653
6654 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6655 struct signatured_type);
6656
6657 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6658 sig_type->signature = sig;
6659 sig_type->per_cu.is_debug_types = 1;
6660 if (dwarf2_per_objfile->using_index)
6661 {
6662 sig_type->per_cu.v.quick =
6663 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6664 struct dwarf2_per_cu_quick_data);
6665 }
6666
6667 if (slot == NULL)
6668 {
6669 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6670 sig_type, INSERT);
6671 }
6672 gdb_assert (*slot == NULL);
6673 *slot = sig_type;
6674 /* The rest of sig_type must be filled in by the caller. */
6675 return sig_type;
6676 }
6677
6678 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6679 Fill in SIG_ENTRY with DWO_ENTRY. */
6680
6681 static void
6682 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6683 struct signatured_type *sig_entry,
6684 struct dwo_unit *dwo_entry)
6685 {
6686 /* Make sure we're not clobbering something we don't expect to. */
6687 gdb_assert (! sig_entry->per_cu.queued);
6688 gdb_assert (sig_entry->per_cu.cu == NULL);
6689 if (dwarf2_per_objfile->using_index)
6690 {
6691 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6692 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6693 }
6694 else
6695 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6696 gdb_assert (sig_entry->signature == dwo_entry->signature);
6697 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6698 gdb_assert (sig_entry->type_unit_group == NULL);
6699 gdb_assert (sig_entry->dwo_unit == NULL);
6700
6701 sig_entry->per_cu.section = dwo_entry->section;
6702 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6703 sig_entry->per_cu.length = dwo_entry->length;
6704 sig_entry->per_cu.reading_dwo_directly = 1;
6705 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6706 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6707 sig_entry->dwo_unit = dwo_entry;
6708 }
6709
6710 /* Subroutine of lookup_signatured_type.
6711 If we haven't read the TU yet, create the signatured_type data structure
6712 for a TU to be read in directly from a DWO file, bypassing the stub.
6713 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6714 using .gdb_index, then when reading a CU we want to stay in the DWO file
6715 containing that CU. Otherwise we could end up reading several other DWO
6716 files (due to comdat folding) to process the transitive closure of all the
6717 mentioned TUs, and that can be slow. The current DWO file will have every
6718 type signature that it needs.
6719 We only do this for .gdb_index because in the psymtab case we already have
6720 to read all the DWOs to build the type unit groups. */
6721
6722 static struct signatured_type *
6723 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6724 {
6725 struct dwarf2_per_objfile *dwarf2_per_objfile
6726 = cu->per_cu->dwarf2_per_objfile;
6727 struct objfile *objfile = dwarf2_per_objfile->objfile;
6728 struct dwo_file *dwo_file;
6729 struct dwo_unit find_dwo_entry, *dwo_entry;
6730 struct signatured_type find_sig_entry, *sig_entry;
6731 void **slot;
6732
6733 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6734
6735 /* If TU skeletons have been removed then we may not have read in any
6736 TUs yet. */
6737 if (dwarf2_per_objfile->signatured_types == NULL)
6738 {
6739 dwarf2_per_objfile->signatured_types
6740 = allocate_signatured_type_table (objfile);
6741 }
6742
6743 /* We only ever need to read in one copy of a signatured type.
6744 Use the global signatured_types array to do our own comdat-folding
6745 of types. If this is the first time we're reading this TU, and
6746 the TU has an entry in .gdb_index, replace the recorded data from
6747 .gdb_index with this TU. */
6748
6749 find_sig_entry.signature = sig;
6750 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6751 &find_sig_entry, INSERT);
6752 sig_entry = (struct signatured_type *) *slot;
6753
6754 /* We can get here with the TU already read, *or* in the process of being
6755 read. Don't reassign the global entry to point to this DWO if that's
6756 the case. Also note that if the TU is already being read, it may not
6757 have come from a DWO, the program may be a mix of Fission-compiled
6758 code and non-Fission-compiled code. */
6759
6760 /* Have we already tried to read this TU?
6761 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6762 needn't exist in the global table yet). */
6763 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6764 return sig_entry;
6765
6766 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6767 dwo_unit of the TU itself. */
6768 dwo_file = cu->dwo_unit->dwo_file;
6769
6770 /* Ok, this is the first time we're reading this TU. */
6771 if (dwo_file->tus == NULL)
6772 return NULL;
6773 find_dwo_entry.signature = sig;
6774 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6775 if (dwo_entry == NULL)
6776 return NULL;
6777
6778 /* If the global table doesn't have an entry for this TU, add one. */
6779 if (sig_entry == NULL)
6780 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6781
6782 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6783 sig_entry->per_cu.tu_read = 1;
6784 return sig_entry;
6785 }
6786
6787 /* Subroutine of lookup_signatured_type.
6788 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6789 then try the DWP file. If the TU stub (skeleton) has been removed then
6790 it won't be in .gdb_index. */
6791
6792 static struct signatured_type *
6793 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6794 {
6795 struct dwarf2_per_objfile *dwarf2_per_objfile
6796 = cu->per_cu->dwarf2_per_objfile;
6797 struct objfile *objfile = dwarf2_per_objfile->objfile;
6798 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6799 struct dwo_unit *dwo_entry;
6800 struct signatured_type find_sig_entry, *sig_entry;
6801 void **slot;
6802
6803 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6804 gdb_assert (dwp_file != NULL);
6805
6806 /* If TU skeletons have been removed then we may not have read in any
6807 TUs yet. */
6808 if (dwarf2_per_objfile->signatured_types == NULL)
6809 {
6810 dwarf2_per_objfile->signatured_types
6811 = allocate_signatured_type_table (objfile);
6812 }
6813
6814 find_sig_entry.signature = sig;
6815 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6816 &find_sig_entry, INSERT);
6817 sig_entry = (struct signatured_type *) *slot;
6818
6819 /* Have we already tried to read this TU?
6820 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6821 needn't exist in the global table yet). */
6822 if (sig_entry != NULL)
6823 return sig_entry;
6824
6825 if (dwp_file->tus == NULL)
6826 return NULL;
6827 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6828 sig, 1 /* is_debug_types */);
6829 if (dwo_entry == NULL)
6830 return NULL;
6831
6832 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6833 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6834
6835 return sig_entry;
6836 }
6837
6838 /* Lookup a signature based type for DW_FORM_ref_sig8.
6839 Returns NULL if signature SIG is not present in the table.
6840 It is up to the caller to complain about this. */
6841
6842 static struct signatured_type *
6843 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6844 {
6845 struct dwarf2_per_objfile *dwarf2_per_objfile
6846 = cu->per_cu->dwarf2_per_objfile;
6847
6848 if (cu->dwo_unit
6849 && dwarf2_per_objfile->using_index)
6850 {
6851 /* We're in a DWO/DWP file, and we're using .gdb_index.
6852 These cases require special processing. */
6853 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6854 return lookup_dwo_signatured_type (cu, sig);
6855 else
6856 return lookup_dwp_signatured_type (cu, sig);
6857 }
6858 else
6859 {
6860 struct signatured_type find_entry, *entry;
6861
6862 if (dwarf2_per_objfile->signatured_types == NULL)
6863 return NULL;
6864 find_entry.signature = sig;
6865 entry = ((struct signatured_type *)
6866 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6867 return entry;
6868 }
6869 }
6870
6871 /* Return the address base of the compile unit, which, if exists, is stored
6872 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6873 static gdb::optional<ULONGEST>
6874 lookup_addr_base (struct die_info *comp_unit_die)
6875 {
6876 struct attribute *attr;
6877 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6878 if (attr == nullptr)
6879 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6880 if (attr == nullptr)
6881 return gdb::optional<ULONGEST> ();
6882 return DW_UNSND (attr);
6883 }
6884
6885 /* Return range lists base of the compile unit, which, if exists, is stored
6886 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6887 static ULONGEST
6888 lookup_ranges_base (struct die_info *comp_unit_die)
6889 {
6890 struct attribute *attr;
6891 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6892 if (attr == nullptr)
6893 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6894 if (attr == nullptr)
6895 return 0;
6896 return DW_UNSND (attr);
6897 }
6898
6899 /* Low level DIE reading support. */
6900
6901 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6902
6903 static void
6904 init_cu_die_reader (struct die_reader_specs *reader,
6905 struct dwarf2_cu *cu,
6906 struct dwarf2_section_info *section,
6907 struct dwo_file *dwo_file,
6908 struct abbrev_table *abbrev_table)
6909 {
6910 gdb_assert (section->readin && section->buffer != NULL);
6911 reader->abfd = section->get_bfd_owner ();
6912 reader->cu = cu;
6913 reader->dwo_file = dwo_file;
6914 reader->die_section = section;
6915 reader->buffer = section->buffer;
6916 reader->buffer_end = section->buffer + section->size;
6917 reader->comp_dir = NULL;
6918 reader->abbrev_table = abbrev_table;
6919 }
6920
6921 /* Subroutine of cutu_reader to simplify it.
6922 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6923 There's just a lot of work to do, and cutu_reader is big enough
6924 already.
6925
6926 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6927 from it to the DIE in the DWO. If NULL we are skipping the stub.
6928 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6929 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6930 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6931 STUB_COMP_DIR may be non-NULL.
6932 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6933 are filled in with the info of the DIE from the DWO file.
6934 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6935 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6936 kept around for at least as long as *RESULT_READER.
6937
6938 The result is non-zero if a valid (non-dummy) DIE was found. */
6939
6940 static int
6941 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6942 struct dwo_unit *dwo_unit,
6943 struct die_info *stub_comp_unit_die,
6944 const char *stub_comp_dir,
6945 struct die_reader_specs *result_reader,
6946 const gdb_byte **result_info_ptr,
6947 struct die_info **result_comp_unit_die,
6948 int *result_has_children,
6949 abbrev_table_up *result_dwo_abbrev_table)
6950 {
6951 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6952 struct objfile *objfile = dwarf2_per_objfile->objfile;
6953 struct dwarf2_cu *cu = this_cu->cu;
6954 bfd *abfd;
6955 const gdb_byte *begin_info_ptr, *info_ptr;
6956 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6957 int i,num_extra_attrs;
6958 struct dwarf2_section_info *dwo_abbrev_section;
6959 struct die_info *comp_unit_die;
6960
6961 /* At most one of these may be provided. */
6962 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6963
6964 /* These attributes aren't processed until later:
6965 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6966 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6967 referenced later. However, these attributes are found in the stub
6968 which we won't have later. In order to not impose this complication
6969 on the rest of the code, we read them here and copy them to the
6970 DWO CU/TU die. */
6971
6972 stmt_list = NULL;
6973 low_pc = NULL;
6974 high_pc = NULL;
6975 ranges = NULL;
6976 comp_dir = NULL;
6977
6978 if (stub_comp_unit_die != NULL)
6979 {
6980 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6981 DWO file. */
6982 if (! this_cu->is_debug_types)
6983 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6984 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6985 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6986 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6987 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6988
6989 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6990
6991 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6992 here (if needed). We need the value before we can process
6993 DW_AT_ranges. */
6994 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6995 }
6996 else if (stub_comp_dir != NULL)
6997 {
6998 /* Reconstruct the comp_dir attribute to simplify the code below. */
6999 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7000 comp_dir->name = DW_AT_comp_dir;
7001 comp_dir->form = DW_FORM_string;
7002 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7003 DW_STRING (comp_dir) = stub_comp_dir;
7004 }
7005
7006 /* Set up for reading the DWO CU/TU. */
7007 cu->dwo_unit = dwo_unit;
7008 dwarf2_section_info *section = dwo_unit->section;
7009 section->read (objfile);
7010 abfd = section->get_bfd_owner ();
7011 begin_info_ptr = info_ptr = (section->buffer
7012 + to_underlying (dwo_unit->sect_off));
7013 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7014
7015 if (this_cu->is_debug_types)
7016 {
7017 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7018
7019 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7020 &cu->header, section,
7021 dwo_abbrev_section,
7022 info_ptr, rcuh_kind::TYPE);
7023 /* This is not an assert because it can be caused by bad debug info. */
7024 if (sig_type->signature != cu->header.signature)
7025 {
7026 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7027 " TU at offset %s [in module %s]"),
7028 hex_string (sig_type->signature),
7029 hex_string (cu->header.signature),
7030 sect_offset_str (dwo_unit->sect_off),
7031 bfd_get_filename (abfd));
7032 }
7033 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7034 /* For DWOs coming from DWP files, we don't know the CU length
7035 nor the type's offset in the TU until now. */
7036 dwo_unit->length = get_cu_length (&cu->header);
7037 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7038
7039 /* Establish the type offset that can be used to lookup the type.
7040 For DWO files, we don't know it until now. */
7041 sig_type->type_offset_in_section
7042 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7043 }
7044 else
7045 {
7046 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7047 &cu->header, section,
7048 dwo_abbrev_section,
7049 info_ptr, rcuh_kind::COMPILE);
7050 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7051 /* For DWOs coming from DWP files, we don't know the CU length
7052 until now. */
7053 dwo_unit->length = get_cu_length (&cu->header);
7054 }
7055
7056 *result_dwo_abbrev_table
7057 = abbrev_table_read_table (objfile, dwo_abbrev_section,
7058 cu->header.abbrev_sect_off);
7059 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7060 result_dwo_abbrev_table->get ());
7061
7062 /* Read in the die, but leave space to copy over the attributes
7063 from the stub. This has the benefit of simplifying the rest of
7064 the code - all the work to maintain the illusion of a single
7065 DW_TAG_{compile,type}_unit DIE is done here. */
7066 num_extra_attrs = ((stmt_list != NULL)
7067 + (low_pc != NULL)
7068 + (high_pc != NULL)
7069 + (ranges != NULL)
7070 + (comp_dir != NULL));
7071 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7072 result_has_children, num_extra_attrs);
7073
7074 /* Copy over the attributes from the stub to the DIE we just read in. */
7075 comp_unit_die = *result_comp_unit_die;
7076 i = comp_unit_die->num_attrs;
7077 if (stmt_list != NULL)
7078 comp_unit_die->attrs[i++] = *stmt_list;
7079 if (low_pc != NULL)
7080 comp_unit_die->attrs[i++] = *low_pc;
7081 if (high_pc != NULL)
7082 comp_unit_die->attrs[i++] = *high_pc;
7083 if (ranges != NULL)
7084 comp_unit_die->attrs[i++] = *ranges;
7085 if (comp_dir != NULL)
7086 comp_unit_die->attrs[i++] = *comp_dir;
7087 comp_unit_die->num_attrs += num_extra_attrs;
7088
7089 if (dwarf_die_debug)
7090 {
7091 fprintf_unfiltered (gdb_stdlog,
7092 "Read die from %s@0x%x of %s:\n",
7093 section->get_name (),
7094 (unsigned) (begin_info_ptr - section->buffer),
7095 bfd_get_filename (abfd));
7096 dump_die (comp_unit_die, dwarf_die_debug);
7097 }
7098
7099 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7100 TUs by skipping the stub and going directly to the entry in the DWO file.
7101 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7102 to get it via circuitous means. Blech. */
7103 if (comp_dir != NULL)
7104 result_reader->comp_dir = DW_STRING (comp_dir);
7105
7106 /* Skip dummy compilation units. */
7107 if (info_ptr >= begin_info_ptr + dwo_unit->length
7108 || peek_abbrev_code (abfd, info_ptr) == 0)
7109 return 0;
7110
7111 *result_info_ptr = info_ptr;
7112 return 1;
7113 }
7114
7115 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7116 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7117 signature is part of the header. */
7118 static gdb::optional<ULONGEST>
7119 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7120 {
7121 if (cu->header.version >= 5)
7122 return cu->header.signature;
7123 struct attribute *attr;
7124 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7125 if (attr == nullptr)
7126 return gdb::optional<ULONGEST> ();
7127 return DW_UNSND (attr);
7128 }
7129
7130 /* Subroutine of cutu_reader to simplify it.
7131 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7132 Returns NULL if the specified DWO unit cannot be found. */
7133
7134 static struct dwo_unit *
7135 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7136 struct die_info *comp_unit_die,
7137 const char *dwo_name)
7138 {
7139 struct dwarf2_cu *cu = this_cu->cu;
7140 struct dwo_unit *dwo_unit;
7141 const char *comp_dir;
7142
7143 gdb_assert (cu != NULL);
7144
7145 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7146 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7147 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7148
7149 if (this_cu->is_debug_types)
7150 {
7151 struct signatured_type *sig_type;
7152
7153 /* Since this_cu is the first member of struct signatured_type,
7154 we can go from a pointer to one to a pointer to the other. */
7155 sig_type = (struct signatured_type *) this_cu;
7156 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7157 }
7158 else
7159 {
7160 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7161 if (!signature.has_value ())
7162 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7163 " [in module %s]"),
7164 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7165 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7166 *signature);
7167 }
7168
7169 return dwo_unit;
7170 }
7171
7172 /* Subroutine of cutu_reader to simplify it.
7173 See it for a description of the parameters.
7174 Read a TU directly from a DWO file, bypassing the stub. */
7175
7176 void
7177 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7178 int use_existing_cu, int keep)
7179 {
7180 struct signatured_type *sig_type;
7181 struct die_reader_specs reader;
7182
7183 /* Verify we can do the following downcast, and that we have the
7184 data we need. */
7185 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7186 sig_type = (struct signatured_type *) this_cu;
7187 gdb_assert (sig_type->dwo_unit != NULL);
7188
7189 if (use_existing_cu && this_cu->cu != NULL)
7190 {
7191 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7192 /* There's no need to do the rereading_dwo_cu handling that
7193 cutu_reader does since we don't read the stub. */
7194 }
7195 else
7196 {
7197 /* If !use_existing_cu, this_cu->cu must be NULL. */
7198 gdb_assert (this_cu->cu == NULL);
7199 m_new_cu.reset (new dwarf2_cu (this_cu));
7200 }
7201
7202 /* A future optimization, if needed, would be to use an existing
7203 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7204 could share abbrev tables. */
7205
7206 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7207 NULL /* stub_comp_unit_die */,
7208 sig_type->dwo_unit->dwo_file->comp_dir,
7209 &reader, &info_ptr,
7210 &comp_unit_die, &has_children,
7211 &m_dwo_abbrev_table) == 0)
7212 {
7213 /* Dummy die. */
7214 dummy_p = true;
7215 }
7216 }
7217
7218 /* Initialize a CU (or TU) and read its DIEs.
7219 If the CU defers to a DWO file, read the DWO file as well.
7220
7221 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7222 Otherwise the table specified in the comp unit header is read in and used.
7223 This is an optimization for when we already have the abbrev table.
7224
7225 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7226 Otherwise, a new CU is allocated with xmalloc.
7227
7228 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7229 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7230 end. */
7231
7232 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7233 struct abbrev_table *abbrev_table,
7234 int use_existing_cu, int keep,
7235 bool skip_partial)
7236 : die_reader_specs {},
7237 m_this_cu (this_cu),
7238 m_keep (keep)
7239 {
7240 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7241 struct objfile *objfile = dwarf2_per_objfile->objfile;
7242 struct dwarf2_section_info *section = this_cu->section;
7243 bfd *abfd = section->get_bfd_owner ();
7244 struct dwarf2_cu *cu;
7245 const gdb_byte *begin_info_ptr;
7246 struct signatured_type *sig_type = NULL;
7247 struct dwarf2_section_info *abbrev_section;
7248 /* Non-zero if CU currently points to a DWO file and we need to
7249 reread it. When this happens we need to reread the skeleton die
7250 before we can reread the DWO file (this only applies to CUs, not TUs). */
7251 int rereading_dwo_cu = 0;
7252
7253 if (dwarf_die_debug)
7254 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7255 this_cu->is_debug_types ? "type" : "comp",
7256 sect_offset_str (this_cu->sect_off));
7257
7258 if (use_existing_cu)
7259 gdb_assert (keep);
7260
7261 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7262 file (instead of going through the stub), short-circuit all of this. */
7263 if (this_cu->reading_dwo_directly)
7264 {
7265 /* Narrow down the scope of possibilities to have to understand. */
7266 gdb_assert (this_cu->is_debug_types);
7267 gdb_assert (abbrev_table == NULL);
7268 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7269 return;
7270 }
7271
7272 /* This is cheap if the section is already read in. */
7273 section->read (objfile);
7274
7275 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7276
7277 abbrev_section = get_abbrev_section_for_cu (this_cu);
7278
7279 if (use_existing_cu && this_cu->cu != NULL)
7280 {
7281 cu = this_cu->cu;
7282 /* If this CU is from a DWO file we need to start over, we need to
7283 refetch the attributes from the skeleton CU.
7284 This could be optimized by retrieving those attributes from when we
7285 were here the first time: the previous comp_unit_die was stored in
7286 comp_unit_obstack. But there's no data yet that we need this
7287 optimization. */
7288 if (cu->dwo_unit != NULL)
7289 rereading_dwo_cu = 1;
7290 }
7291 else
7292 {
7293 /* If !use_existing_cu, this_cu->cu must be NULL. */
7294 gdb_assert (this_cu->cu == NULL);
7295 m_new_cu.reset (new dwarf2_cu (this_cu));
7296 cu = m_new_cu.get ();
7297 }
7298
7299 /* Get the header. */
7300 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7301 {
7302 /* We already have the header, there's no need to read it in again. */
7303 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7304 }
7305 else
7306 {
7307 if (this_cu->is_debug_types)
7308 {
7309 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7310 &cu->header, section,
7311 abbrev_section, info_ptr,
7312 rcuh_kind::TYPE);
7313
7314 /* Since per_cu is the first member of struct signatured_type,
7315 we can go from a pointer to one to a pointer to the other. */
7316 sig_type = (struct signatured_type *) this_cu;
7317 gdb_assert (sig_type->signature == cu->header.signature);
7318 gdb_assert (sig_type->type_offset_in_tu
7319 == cu->header.type_cu_offset_in_tu);
7320 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7321
7322 /* LENGTH has not been set yet for type units if we're
7323 using .gdb_index. */
7324 this_cu->length = get_cu_length (&cu->header);
7325
7326 /* Establish the type offset that can be used to lookup the type. */
7327 sig_type->type_offset_in_section =
7328 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7329
7330 this_cu->dwarf_version = cu->header.version;
7331 }
7332 else
7333 {
7334 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7335 &cu->header, section,
7336 abbrev_section,
7337 info_ptr,
7338 rcuh_kind::COMPILE);
7339
7340 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7341 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7342 this_cu->dwarf_version = cu->header.version;
7343 }
7344 }
7345
7346 /* Skip dummy compilation units. */
7347 if (info_ptr >= begin_info_ptr + this_cu->length
7348 || peek_abbrev_code (abfd, info_ptr) == 0)
7349 {
7350 dummy_p = true;
7351 return;
7352 }
7353
7354 /* If we don't have them yet, read the abbrevs for this compilation unit.
7355 And if we need to read them now, make sure they're freed when we're
7356 done. */
7357 if (abbrev_table != NULL)
7358 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7359 else
7360 {
7361 m_abbrev_table_holder
7362 = abbrev_table_read_table (objfile, abbrev_section,
7363 cu->header.abbrev_sect_off);
7364 abbrev_table = m_abbrev_table_holder.get ();
7365 }
7366
7367 /* Read the top level CU/TU die. */
7368 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7369 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7370
7371 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7372 {
7373 dummy_p = true;
7374 return;
7375 }
7376
7377 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7378 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7379 table from the DWO file and pass the ownership over to us. It will be
7380 referenced from READER, so we must make sure to free it after we're done
7381 with READER.
7382
7383 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7384 DWO CU, that this test will fail (the attribute will not be present). */
7385 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7386 if (dwo_name != nullptr)
7387 {
7388 struct dwo_unit *dwo_unit;
7389 struct die_info *dwo_comp_unit_die;
7390
7391 if (has_children)
7392 {
7393 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7394 " has children (offset %s) [in module %s]"),
7395 sect_offset_str (this_cu->sect_off),
7396 bfd_get_filename (abfd));
7397 }
7398 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7399 if (dwo_unit != NULL)
7400 {
7401 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7402 comp_unit_die, NULL,
7403 this, &info_ptr,
7404 &dwo_comp_unit_die, &has_children,
7405 &m_dwo_abbrev_table) == 0)
7406 {
7407 /* Dummy die. */
7408 dummy_p = true;
7409 return;
7410 }
7411 comp_unit_die = dwo_comp_unit_die;
7412 }
7413 else
7414 {
7415 /* Yikes, we couldn't find the rest of the DIE, we only have
7416 the stub. A complaint has already been logged. There's
7417 not much more we can do except pass on the stub DIE to
7418 die_reader_func. We don't want to throw an error on bad
7419 debug info. */
7420 }
7421 }
7422 }
7423
7424 cutu_reader::~cutu_reader ()
7425 {
7426 /* Done, clean up. */
7427 if (m_new_cu != NULL && m_keep && !dummy_p)
7428 {
7429 struct dwarf2_per_objfile *dwarf2_per_objfile
7430 = m_this_cu->dwarf2_per_objfile;
7431 /* Link this CU into read_in_chain. */
7432 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7433 dwarf2_per_objfile->read_in_chain = m_this_cu;
7434 /* The chain owns it now. */
7435 m_new_cu.release ();
7436 }
7437 }
7438
7439 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7440 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7441 assumed to have already done the lookup to find the DWO file).
7442
7443 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7444 THIS_CU->is_debug_types, but nothing else.
7445
7446 We fill in THIS_CU->length.
7447
7448 THIS_CU->cu is always freed when done.
7449 This is done in order to not leave THIS_CU->cu in a state where we have
7450 to care whether it refers to the "main" CU or the DWO CU.
7451
7452 When parent_cu is passed, it is used to provide a default value for
7453 str_offsets_base and addr_base from the parent. */
7454
7455 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7456 struct dwarf2_cu *parent_cu,
7457 struct dwo_file *dwo_file)
7458 : die_reader_specs {},
7459 m_this_cu (this_cu)
7460 {
7461 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7462 struct objfile *objfile = dwarf2_per_objfile->objfile;
7463 struct dwarf2_section_info *section = this_cu->section;
7464 bfd *abfd = section->get_bfd_owner ();
7465 struct dwarf2_section_info *abbrev_section;
7466 const gdb_byte *begin_info_ptr, *info_ptr;
7467 int has_children;
7468
7469 if (dwarf_die_debug)
7470 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7471 this_cu->is_debug_types ? "type" : "comp",
7472 sect_offset_str (this_cu->sect_off));
7473
7474 gdb_assert (this_cu->cu == NULL);
7475
7476 abbrev_section = (dwo_file != NULL
7477 ? &dwo_file->sections.abbrev
7478 : get_abbrev_section_for_cu (this_cu));
7479
7480 /* This is cheap if the section is already read in. */
7481 section->read (objfile);
7482
7483 m_new_cu.reset (new dwarf2_cu (this_cu));
7484
7485 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7486 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7487 &m_new_cu->header, section,
7488 abbrev_section, info_ptr,
7489 (this_cu->is_debug_types
7490 ? rcuh_kind::TYPE
7491 : rcuh_kind::COMPILE));
7492
7493 if (parent_cu != nullptr)
7494 {
7495 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7496 m_new_cu->addr_base = parent_cu->addr_base;
7497 }
7498 this_cu->length = get_cu_length (&m_new_cu->header);
7499
7500 /* Skip dummy compilation units. */
7501 if (info_ptr >= begin_info_ptr + this_cu->length
7502 || peek_abbrev_code (abfd, info_ptr) == 0)
7503 {
7504 dummy_p = true;
7505 return;
7506 }
7507
7508 m_abbrev_table_holder
7509 = abbrev_table_read_table (objfile, abbrev_section,
7510 m_new_cu->header.abbrev_sect_off);
7511
7512 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7513 m_abbrev_table_holder.get ());
7514 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7515 }
7516
7517 \f
7518 /* Type Unit Groups.
7519
7520 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7521 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7522 so that all types coming from the same compilation (.o file) are grouped
7523 together. A future step could be to put the types in the same symtab as
7524 the CU the types ultimately came from. */
7525
7526 static hashval_t
7527 hash_type_unit_group (const void *item)
7528 {
7529 const struct type_unit_group *tu_group
7530 = (const struct type_unit_group *) item;
7531
7532 return hash_stmt_list_entry (&tu_group->hash);
7533 }
7534
7535 static int
7536 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7537 {
7538 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7539 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7540
7541 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7542 }
7543
7544 /* Allocate a hash table for type unit groups. */
7545
7546 static htab_t
7547 allocate_type_unit_groups_table (struct objfile *objfile)
7548 {
7549 return htab_create_alloc_ex (3,
7550 hash_type_unit_group,
7551 eq_type_unit_group,
7552 NULL,
7553 &objfile->objfile_obstack,
7554 hashtab_obstack_allocate,
7555 dummy_obstack_deallocate);
7556 }
7557
7558 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7559 partial symtabs. We combine several TUs per psymtab to not let the size
7560 of any one psymtab grow too big. */
7561 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7562 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7563
7564 /* Helper routine for get_type_unit_group.
7565 Create the type_unit_group object used to hold one or more TUs. */
7566
7567 static struct type_unit_group *
7568 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7569 {
7570 struct dwarf2_per_objfile *dwarf2_per_objfile
7571 = cu->per_cu->dwarf2_per_objfile;
7572 struct objfile *objfile = dwarf2_per_objfile->objfile;
7573 struct dwarf2_per_cu_data *per_cu;
7574 struct type_unit_group *tu_group;
7575
7576 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7577 struct type_unit_group);
7578 per_cu = &tu_group->per_cu;
7579 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7580
7581 if (dwarf2_per_objfile->using_index)
7582 {
7583 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7584 struct dwarf2_per_cu_quick_data);
7585 }
7586 else
7587 {
7588 unsigned int line_offset = to_underlying (line_offset_struct);
7589 dwarf2_psymtab *pst;
7590 std::string name;
7591
7592 /* Give the symtab a useful name for debug purposes. */
7593 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7594 name = string_printf ("<type_units_%d>",
7595 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7596 else
7597 name = string_printf ("<type_units_at_0x%x>", line_offset);
7598
7599 pst = create_partial_symtab (per_cu, name.c_str ());
7600 pst->anonymous = true;
7601 }
7602
7603 tu_group->hash.dwo_unit = cu->dwo_unit;
7604 tu_group->hash.line_sect_off = line_offset_struct;
7605
7606 return tu_group;
7607 }
7608
7609 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7610 STMT_LIST is a DW_AT_stmt_list attribute. */
7611
7612 static struct type_unit_group *
7613 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7614 {
7615 struct dwarf2_per_objfile *dwarf2_per_objfile
7616 = cu->per_cu->dwarf2_per_objfile;
7617 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7618 struct type_unit_group *tu_group;
7619 void **slot;
7620 unsigned int line_offset;
7621 struct type_unit_group type_unit_group_for_lookup;
7622
7623 if (dwarf2_per_objfile->type_unit_groups == NULL)
7624 {
7625 dwarf2_per_objfile->type_unit_groups =
7626 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7627 }
7628
7629 /* Do we need to create a new group, or can we use an existing one? */
7630
7631 if (stmt_list)
7632 {
7633 line_offset = DW_UNSND (stmt_list);
7634 ++tu_stats->nr_symtab_sharers;
7635 }
7636 else
7637 {
7638 /* Ugh, no stmt_list. Rare, but we have to handle it.
7639 We can do various things here like create one group per TU or
7640 spread them over multiple groups to split up the expansion work.
7641 To avoid worst case scenarios (too many groups or too large groups)
7642 we, umm, group them in bunches. */
7643 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7644 | (tu_stats->nr_stmt_less_type_units
7645 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7646 ++tu_stats->nr_stmt_less_type_units;
7647 }
7648
7649 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7650 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7651 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7652 &type_unit_group_for_lookup, INSERT);
7653 if (*slot != NULL)
7654 {
7655 tu_group = (struct type_unit_group *) *slot;
7656 gdb_assert (tu_group != NULL);
7657 }
7658 else
7659 {
7660 sect_offset line_offset_struct = (sect_offset) line_offset;
7661 tu_group = create_type_unit_group (cu, line_offset_struct);
7662 *slot = tu_group;
7663 ++tu_stats->nr_symtabs;
7664 }
7665
7666 return tu_group;
7667 }
7668 \f
7669 /* Partial symbol tables. */
7670
7671 /* Create a psymtab named NAME and assign it to PER_CU.
7672
7673 The caller must fill in the following details:
7674 dirname, textlow, texthigh. */
7675
7676 static dwarf2_psymtab *
7677 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7678 {
7679 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7680 dwarf2_psymtab *pst;
7681
7682 pst = new dwarf2_psymtab (name, objfile, 0);
7683
7684 pst->psymtabs_addrmap_supported = true;
7685
7686 /* This is the glue that links PST into GDB's symbol API. */
7687 pst->per_cu_data = per_cu;
7688 per_cu->v.psymtab = pst;
7689
7690 return pst;
7691 }
7692
7693 /* DIE reader function for process_psymtab_comp_unit. */
7694
7695 static void
7696 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7697 const gdb_byte *info_ptr,
7698 struct die_info *comp_unit_die,
7699 int has_children,
7700 int want_partial_unit,
7701 enum language pretend_language)
7702 {
7703 struct dwarf2_cu *cu = reader->cu;
7704 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7705 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7706 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7707 CORE_ADDR baseaddr;
7708 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7709 dwarf2_psymtab *pst;
7710 enum pc_bounds_kind cu_bounds_kind;
7711 const char *filename;
7712
7713 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7714 return;
7715
7716 gdb_assert (! per_cu->is_debug_types);
7717
7718 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7719
7720 /* Allocate a new partial symbol table structure. */
7721 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7722 if (filename == NULL)
7723 filename = "";
7724
7725 pst = create_partial_symtab (per_cu, filename);
7726
7727 /* This must be done before calling dwarf2_build_include_psymtabs. */
7728 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7729
7730 baseaddr = objfile->text_section_offset ();
7731
7732 dwarf2_find_base_address (comp_unit_die, cu);
7733
7734 /* Possibly set the default values of LOWPC and HIGHPC from
7735 `DW_AT_ranges'. */
7736 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7737 &best_highpc, cu, pst);
7738 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7739 {
7740 CORE_ADDR low
7741 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7742 - baseaddr);
7743 CORE_ADDR high
7744 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7745 - baseaddr - 1);
7746 /* Store the contiguous range if it is not empty; it can be
7747 empty for CUs with no code. */
7748 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7749 low, high, pst);
7750 }
7751
7752 /* Check if comp unit has_children.
7753 If so, read the rest of the partial symbols from this comp unit.
7754 If not, there's no more debug_info for this comp unit. */
7755 if (has_children)
7756 {
7757 struct partial_die_info *first_die;
7758 CORE_ADDR lowpc, highpc;
7759
7760 lowpc = ((CORE_ADDR) -1);
7761 highpc = ((CORE_ADDR) 0);
7762
7763 first_die = load_partial_dies (reader, info_ptr, 1);
7764
7765 scan_partial_symbols (first_die, &lowpc, &highpc,
7766 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7767
7768 /* If we didn't find a lowpc, set it to highpc to avoid
7769 complaints from `maint check'. */
7770 if (lowpc == ((CORE_ADDR) -1))
7771 lowpc = highpc;
7772
7773 /* If the compilation unit didn't have an explicit address range,
7774 then use the information extracted from its child dies. */
7775 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7776 {
7777 best_lowpc = lowpc;
7778 best_highpc = highpc;
7779 }
7780 }
7781 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7782 best_lowpc + baseaddr)
7783 - baseaddr);
7784 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7785 best_highpc + baseaddr)
7786 - baseaddr);
7787
7788 end_psymtab_common (objfile, pst);
7789
7790 if (!cu->per_cu->imported_symtabs_empty ())
7791 {
7792 int i;
7793 int len = cu->per_cu->imported_symtabs_size ();
7794
7795 /* Fill in 'dependencies' here; we fill in 'users' in a
7796 post-pass. */
7797 pst->number_of_dependencies = len;
7798 pst->dependencies
7799 = objfile->partial_symtabs->allocate_dependencies (len);
7800 for (i = 0; i < len; ++i)
7801 {
7802 pst->dependencies[i]
7803 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7804 }
7805
7806 cu->per_cu->imported_symtabs_free ();
7807 }
7808
7809 /* Get the list of files included in the current compilation unit,
7810 and build a psymtab for each of them. */
7811 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7812
7813 if (dwarf_read_debug)
7814 fprintf_unfiltered (gdb_stdlog,
7815 "Psymtab for %s unit @%s: %s - %s"
7816 ", %d global, %d static syms\n",
7817 per_cu->is_debug_types ? "type" : "comp",
7818 sect_offset_str (per_cu->sect_off),
7819 paddress (gdbarch, pst->text_low (objfile)),
7820 paddress (gdbarch, pst->text_high (objfile)),
7821 pst->n_global_syms, pst->n_static_syms);
7822 }
7823
7824 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7825 Process compilation unit THIS_CU for a psymtab. */
7826
7827 static void
7828 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7829 int want_partial_unit,
7830 enum language pretend_language)
7831 {
7832 /* If this compilation unit was already read in, free the
7833 cached copy in order to read it in again. This is
7834 necessary because we skipped some symbols when we first
7835 read in the compilation unit (see load_partial_dies).
7836 This problem could be avoided, but the benefit is unclear. */
7837 if (this_cu->cu != NULL)
7838 free_one_cached_comp_unit (this_cu);
7839
7840 cutu_reader reader (this_cu, NULL, 0, 0, false);
7841
7842 if (reader.dummy_p)
7843 {
7844 /* Nothing. */
7845 }
7846 else if (this_cu->is_debug_types)
7847 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7848 reader.has_children);
7849 else
7850 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7851 reader.comp_unit_die,
7852 reader.has_children,
7853 want_partial_unit,
7854 pretend_language);
7855
7856 /* Age out any secondary CUs. */
7857 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7858 }
7859
7860 /* Reader function for build_type_psymtabs. */
7861
7862 static void
7863 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7864 const gdb_byte *info_ptr,
7865 struct die_info *type_unit_die,
7866 int has_children)
7867 {
7868 struct dwarf2_per_objfile *dwarf2_per_objfile
7869 = reader->cu->per_cu->dwarf2_per_objfile;
7870 struct objfile *objfile = dwarf2_per_objfile->objfile;
7871 struct dwarf2_cu *cu = reader->cu;
7872 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7873 struct signatured_type *sig_type;
7874 struct type_unit_group *tu_group;
7875 struct attribute *attr;
7876 struct partial_die_info *first_die;
7877 CORE_ADDR lowpc, highpc;
7878 dwarf2_psymtab *pst;
7879
7880 gdb_assert (per_cu->is_debug_types);
7881 sig_type = (struct signatured_type *) per_cu;
7882
7883 if (! has_children)
7884 return;
7885
7886 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7887 tu_group = get_type_unit_group (cu, attr);
7888
7889 if (tu_group->tus == nullptr)
7890 tu_group->tus = new std::vector<signatured_type *>;
7891 tu_group->tus->push_back (sig_type);
7892
7893 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7894 pst = create_partial_symtab (per_cu, "");
7895 pst->anonymous = true;
7896
7897 first_die = load_partial_dies (reader, info_ptr, 1);
7898
7899 lowpc = (CORE_ADDR) -1;
7900 highpc = (CORE_ADDR) 0;
7901 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7902
7903 end_psymtab_common (objfile, pst);
7904 }
7905
7906 /* Struct used to sort TUs by their abbreviation table offset. */
7907
7908 struct tu_abbrev_offset
7909 {
7910 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7911 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7912 {}
7913
7914 signatured_type *sig_type;
7915 sect_offset abbrev_offset;
7916 };
7917
7918 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7919
7920 static bool
7921 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7922 const struct tu_abbrev_offset &b)
7923 {
7924 return a.abbrev_offset < b.abbrev_offset;
7925 }
7926
7927 /* Efficiently read all the type units.
7928 This does the bulk of the work for build_type_psymtabs.
7929
7930 The efficiency is because we sort TUs by the abbrev table they use and
7931 only read each abbrev table once. In one program there are 200K TUs
7932 sharing 8K abbrev tables.
7933
7934 The main purpose of this function is to support building the
7935 dwarf2_per_objfile->type_unit_groups table.
7936 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7937 can collapse the search space by grouping them by stmt_list.
7938 The savings can be significant, in the same program from above the 200K TUs
7939 share 8K stmt_list tables.
7940
7941 FUNC is expected to call get_type_unit_group, which will create the
7942 struct type_unit_group if necessary and add it to
7943 dwarf2_per_objfile->type_unit_groups. */
7944
7945 static void
7946 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7947 {
7948 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7949 abbrev_table_up abbrev_table;
7950 sect_offset abbrev_offset;
7951
7952 /* It's up to the caller to not call us multiple times. */
7953 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7954
7955 if (dwarf2_per_objfile->all_type_units.empty ())
7956 return;
7957
7958 /* TUs typically share abbrev tables, and there can be way more TUs than
7959 abbrev tables. Sort by abbrev table to reduce the number of times we
7960 read each abbrev table in.
7961 Alternatives are to punt or to maintain a cache of abbrev tables.
7962 This is simpler and efficient enough for now.
7963
7964 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7965 symtab to use). Typically TUs with the same abbrev offset have the same
7966 stmt_list value too so in practice this should work well.
7967
7968 The basic algorithm here is:
7969
7970 sort TUs by abbrev table
7971 for each TU with same abbrev table:
7972 read abbrev table if first user
7973 read TU top level DIE
7974 [IWBN if DWO skeletons had DW_AT_stmt_list]
7975 call FUNC */
7976
7977 if (dwarf_read_debug)
7978 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7979
7980 /* Sort in a separate table to maintain the order of all_type_units
7981 for .gdb_index: TU indices directly index all_type_units. */
7982 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7983 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7984
7985 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7986 sorted_by_abbrev.emplace_back
7987 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7988 sig_type->per_cu.section,
7989 sig_type->per_cu.sect_off));
7990
7991 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7992 sort_tu_by_abbrev_offset);
7993
7994 abbrev_offset = (sect_offset) ~(unsigned) 0;
7995
7996 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7997 {
7998 /* Switch to the next abbrev table if necessary. */
7999 if (abbrev_table == NULL
8000 || tu.abbrev_offset != abbrev_offset)
8001 {
8002 abbrev_offset = tu.abbrev_offset;
8003 abbrev_table =
8004 abbrev_table_read_table (dwarf2_per_objfile->objfile,
8005 &dwarf2_per_objfile->abbrev,
8006 abbrev_offset);
8007 ++tu_stats->nr_uniq_abbrev_tables;
8008 }
8009
8010 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8011 0, 0, false);
8012 if (!reader.dummy_p)
8013 build_type_psymtabs_reader (&reader, reader.info_ptr,
8014 reader.comp_unit_die,
8015 reader.has_children);
8016 }
8017 }
8018
8019 /* Print collected type unit statistics. */
8020
8021 static void
8022 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8023 {
8024 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8025
8026 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8027 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8028 dwarf2_per_objfile->all_type_units.size ());
8029 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8030 tu_stats->nr_uniq_abbrev_tables);
8031 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8032 tu_stats->nr_symtabs);
8033 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8034 tu_stats->nr_symtab_sharers);
8035 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8036 tu_stats->nr_stmt_less_type_units);
8037 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8038 tu_stats->nr_all_type_units_reallocs);
8039 }
8040
8041 /* Traversal function for build_type_psymtabs. */
8042
8043 static int
8044 build_type_psymtab_dependencies (void **slot, void *info)
8045 {
8046 struct dwarf2_per_objfile *dwarf2_per_objfile
8047 = (struct dwarf2_per_objfile *) info;
8048 struct objfile *objfile = dwarf2_per_objfile->objfile;
8049 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8050 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8051 dwarf2_psymtab *pst = per_cu->v.psymtab;
8052 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8053 int i;
8054
8055 gdb_assert (len > 0);
8056 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8057
8058 pst->number_of_dependencies = len;
8059 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8060 for (i = 0; i < len; ++i)
8061 {
8062 struct signatured_type *iter = tu_group->tus->at (i);
8063 gdb_assert (iter->per_cu.is_debug_types);
8064 pst->dependencies[i] = iter->per_cu.v.psymtab;
8065 iter->type_unit_group = tu_group;
8066 }
8067
8068 delete tu_group->tus;
8069 tu_group->tus = nullptr;
8070
8071 return 1;
8072 }
8073
8074 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8075 Build partial symbol tables for the .debug_types comp-units. */
8076
8077 static void
8078 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8079 {
8080 if (! create_all_type_units (dwarf2_per_objfile))
8081 return;
8082
8083 build_type_psymtabs_1 (dwarf2_per_objfile);
8084 }
8085
8086 /* Traversal function for process_skeletonless_type_unit.
8087 Read a TU in a DWO file and build partial symbols for it. */
8088
8089 static int
8090 process_skeletonless_type_unit (void **slot, void *info)
8091 {
8092 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8093 struct dwarf2_per_objfile *dwarf2_per_objfile
8094 = (struct dwarf2_per_objfile *) info;
8095 struct signatured_type find_entry, *entry;
8096
8097 /* If this TU doesn't exist in the global table, add it and read it in. */
8098
8099 if (dwarf2_per_objfile->signatured_types == NULL)
8100 {
8101 dwarf2_per_objfile->signatured_types
8102 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8103 }
8104
8105 find_entry.signature = dwo_unit->signature;
8106 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8107 INSERT);
8108 /* If we've already seen this type there's nothing to do. What's happening
8109 is we're doing our own version of comdat-folding here. */
8110 if (*slot != NULL)
8111 return 1;
8112
8113 /* This does the job that create_all_type_units would have done for
8114 this TU. */
8115 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8116 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8117 *slot = entry;
8118
8119 /* This does the job that build_type_psymtabs_1 would have done. */
8120 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8121 if (!reader.dummy_p)
8122 build_type_psymtabs_reader (&reader, reader.info_ptr,
8123 reader.comp_unit_die, reader.has_children);
8124
8125 return 1;
8126 }
8127
8128 /* Traversal function for process_skeletonless_type_units. */
8129
8130 static int
8131 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8132 {
8133 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8134
8135 if (dwo_file->tus != NULL)
8136 {
8137 htab_traverse_noresize (dwo_file->tus,
8138 process_skeletonless_type_unit, info);
8139 }
8140
8141 return 1;
8142 }
8143
8144 /* Scan all TUs of DWO files, verifying we've processed them.
8145 This is needed in case a TU was emitted without its skeleton.
8146 Note: This can't be done until we know what all the DWO files are. */
8147
8148 static void
8149 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8150 {
8151 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8152 if (get_dwp_file (dwarf2_per_objfile) == NULL
8153 && dwarf2_per_objfile->dwo_files != NULL)
8154 {
8155 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8156 process_dwo_file_for_skeletonless_type_units,
8157 dwarf2_per_objfile);
8158 }
8159 }
8160
8161 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8162
8163 static void
8164 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8165 {
8166 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8167 {
8168 dwarf2_psymtab *pst = per_cu->v.psymtab;
8169
8170 if (pst == NULL)
8171 continue;
8172
8173 for (int j = 0; j < pst->number_of_dependencies; ++j)
8174 {
8175 /* Set the 'user' field only if it is not already set. */
8176 if (pst->dependencies[j]->user == NULL)
8177 pst->dependencies[j]->user = pst;
8178 }
8179 }
8180 }
8181
8182 /* Build the partial symbol table by doing a quick pass through the
8183 .debug_info and .debug_abbrev sections. */
8184
8185 static void
8186 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8187 {
8188 struct objfile *objfile = dwarf2_per_objfile->objfile;
8189
8190 if (dwarf_read_debug)
8191 {
8192 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8193 objfile_name (objfile));
8194 }
8195
8196 dwarf2_per_objfile->reading_partial_symbols = 1;
8197
8198 dwarf2_per_objfile->info.read (objfile);
8199
8200 /* Any cached compilation units will be linked by the per-objfile
8201 read_in_chain. Make sure to free them when we're done. */
8202 free_cached_comp_units freer (dwarf2_per_objfile);
8203
8204 build_type_psymtabs (dwarf2_per_objfile);
8205
8206 create_all_comp_units (dwarf2_per_objfile);
8207
8208 /* Create a temporary address map on a temporary obstack. We later
8209 copy this to the final obstack. */
8210 auto_obstack temp_obstack;
8211
8212 scoped_restore save_psymtabs_addrmap
8213 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8214 addrmap_create_mutable (&temp_obstack));
8215
8216 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8217 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8218
8219 /* This has to wait until we read the CUs, we need the list of DWOs. */
8220 process_skeletonless_type_units (dwarf2_per_objfile);
8221
8222 /* Now that all TUs have been processed we can fill in the dependencies. */
8223 if (dwarf2_per_objfile->type_unit_groups != NULL)
8224 {
8225 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8226 build_type_psymtab_dependencies, dwarf2_per_objfile);
8227 }
8228
8229 if (dwarf_read_debug)
8230 print_tu_stats (dwarf2_per_objfile);
8231
8232 set_partial_user (dwarf2_per_objfile);
8233
8234 objfile->partial_symtabs->psymtabs_addrmap
8235 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8236 objfile->partial_symtabs->obstack ());
8237 /* At this point we want to keep the address map. */
8238 save_psymtabs_addrmap.release ();
8239
8240 if (dwarf_read_debug)
8241 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8242 objfile_name (objfile));
8243 }
8244
8245 /* Load the partial DIEs for a secondary CU into memory.
8246 This is also used when rereading a primary CU with load_all_dies. */
8247
8248 static void
8249 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8250 {
8251 cutu_reader reader (this_cu, NULL, 1, 1, false);
8252
8253 if (!reader.dummy_p)
8254 {
8255 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8256 language_minimal);
8257
8258 /* Check if comp unit has_children.
8259 If so, read the rest of the partial symbols from this comp unit.
8260 If not, there's no more debug_info for this comp unit. */
8261 if (reader.has_children)
8262 load_partial_dies (&reader, reader.info_ptr, 0);
8263 }
8264 }
8265
8266 static void
8267 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8268 struct dwarf2_section_info *section,
8269 struct dwarf2_section_info *abbrev_section,
8270 unsigned int is_dwz)
8271 {
8272 const gdb_byte *info_ptr;
8273 struct objfile *objfile = dwarf2_per_objfile->objfile;
8274
8275 if (dwarf_read_debug)
8276 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8277 section->get_name (),
8278 section->get_file_name ());
8279
8280 section->read (objfile);
8281
8282 info_ptr = section->buffer;
8283
8284 while (info_ptr < section->buffer + section->size)
8285 {
8286 struct dwarf2_per_cu_data *this_cu;
8287
8288 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8289
8290 comp_unit_head cu_header;
8291 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8292 abbrev_section, info_ptr,
8293 rcuh_kind::COMPILE);
8294
8295 /* Save the compilation unit for later lookup. */
8296 if (cu_header.unit_type != DW_UT_type)
8297 {
8298 this_cu = XOBNEW (&objfile->objfile_obstack,
8299 struct dwarf2_per_cu_data);
8300 memset (this_cu, 0, sizeof (*this_cu));
8301 }
8302 else
8303 {
8304 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8305 struct signatured_type);
8306 memset (sig_type, 0, sizeof (*sig_type));
8307 sig_type->signature = cu_header.signature;
8308 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8309 this_cu = &sig_type->per_cu;
8310 }
8311 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8312 this_cu->sect_off = sect_off;
8313 this_cu->length = cu_header.length + cu_header.initial_length_size;
8314 this_cu->is_dwz = is_dwz;
8315 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8316 this_cu->section = section;
8317
8318 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8319
8320 info_ptr = info_ptr + this_cu->length;
8321 }
8322 }
8323
8324 /* Create a list of all compilation units in OBJFILE.
8325 This is only done for -readnow and building partial symtabs. */
8326
8327 static void
8328 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8329 {
8330 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8331 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8332 &dwarf2_per_objfile->abbrev, 0);
8333
8334 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8335 if (dwz != NULL)
8336 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8337 1);
8338 }
8339
8340 /* Process all loaded DIEs for compilation unit CU, starting at
8341 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8342 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8343 DW_AT_ranges). See the comments of add_partial_subprogram on how
8344 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8345
8346 static void
8347 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8348 CORE_ADDR *highpc, int set_addrmap,
8349 struct dwarf2_cu *cu)
8350 {
8351 struct partial_die_info *pdi;
8352
8353 /* Now, march along the PDI's, descending into ones which have
8354 interesting children but skipping the children of the other ones,
8355 until we reach the end of the compilation unit. */
8356
8357 pdi = first_die;
8358
8359 while (pdi != NULL)
8360 {
8361 pdi->fixup (cu);
8362
8363 /* Anonymous namespaces or modules have no name but have interesting
8364 children, so we need to look at them. Ditto for anonymous
8365 enums. */
8366
8367 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8368 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8369 || pdi->tag == DW_TAG_imported_unit
8370 || pdi->tag == DW_TAG_inlined_subroutine)
8371 {
8372 switch (pdi->tag)
8373 {
8374 case DW_TAG_subprogram:
8375 case DW_TAG_inlined_subroutine:
8376 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8377 break;
8378 case DW_TAG_constant:
8379 case DW_TAG_variable:
8380 case DW_TAG_typedef:
8381 case DW_TAG_union_type:
8382 if (!pdi->is_declaration)
8383 {
8384 add_partial_symbol (pdi, cu);
8385 }
8386 break;
8387 case DW_TAG_class_type:
8388 case DW_TAG_interface_type:
8389 case DW_TAG_structure_type:
8390 if (!pdi->is_declaration)
8391 {
8392 add_partial_symbol (pdi, cu);
8393 }
8394 if ((cu->language == language_rust
8395 || cu->language == language_cplus) && pdi->has_children)
8396 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8397 set_addrmap, cu);
8398 break;
8399 case DW_TAG_enumeration_type:
8400 if (!pdi->is_declaration)
8401 add_partial_enumeration (pdi, cu);
8402 break;
8403 case DW_TAG_base_type:
8404 case DW_TAG_subrange_type:
8405 /* File scope base type definitions are added to the partial
8406 symbol table. */
8407 add_partial_symbol (pdi, cu);
8408 break;
8409 case DW_TAG_namespace:
8410 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8411 break;
8412 case DW_TAG_module:
8413 if (!pdi->is_declaration)
8414 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8415 break;
8416 case DW_TAG_imported_unit:
8417 {
8418 struct dwarf2_per_cu_data *per_cu;
8419
8420 /* For now we don't handle imported units in type units. */
8421 if (cu->per_cu->is_debug_types)
8422 {
8423 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8424 " supported in type units [in module %s]"),
8425 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8426 }
8427
8428 per_cu = dwarf2_find_containing_comp_unit
8429 (pdi->d.sect_off, pdi->is_dwz,
8430 cu->per_cu->dwarf2_per_objfile);
8431
8432 /* Go read the partial unit, if needed. */
8433 if (per_cu->v.psymtab == NULL)
8434 process_psymtab_comp_unit (per_cu, 1, cu->language);
8435
8436 cu->per_cu->imported_symtabs_push (per_cu);
8437 }
8438 break;
8439 case DW_TAG_imported_declaration:
8440 add_partial_symbol (pdi, cu);
8441 break;
8442 default:
8443 break;
8444 }
8445 }
8446
8447 /* If the die has a sibling, skip to the sibling. */
8448
8449 pdi = pdi->die_sibling;
8450 }
8451 }
8452
8453 /* Functions used to compute the fully scoped name of a partial DIE.
8454
8455 Normally, this is simple. For C++, the parent DIE's fully scoped
8456 name is concatenated with "::" and the partial DIE's name.
8457 Enumerators are an exception; they use the scope of their parent
8458 enumeration type, i.e. the name of the enumeration type is not
8459 prepended to the enumerator.
8460
8461 There are two complexities. One is DW_AT_specification; in this
8462 case "parent" means the parent of the target of the specification,
8463 instead of the direct parent of the DIE. The other is compilers
8464 which do not emit DW_TAG_namespace; in this case we try to guess
8465 the fully qualified name of structure types from their members'
8466 linkage names. This must be done using the DIE's children rather
8467 than the children of any DW_AT_specification target. We only need
8468 to do this for structures at the top level, i.e. if the target of
8469 any DW_AT_specification (if any; otherwise the DIE itself) does not
8470 have a parent. */
8471
8472 /* Compute the scope prefix associated with PDI's parent, in
8473 compilation unit CU. The result will be allocated on CU's
8474 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8475 field. NULL is returned if no prefix is necessary. */
8476 static const char *
8477 partial_die_parent_scope (struct partial_die_info *pdi,
8478 struct dwarf2_cu *cu)
8479 {
8480 const char *grandparent_scope;
8481 struct partial_die_info *parent, *real_pdi;
8482
8483 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8484 then this means the parent of the specification DIE. */
8485
8486 real_pdi = pdi;
8487 while (real_pdi->has_specification)
8488 {
8489 auto res = find_partial_die (real_pdi->spec_offset,
8490 real_pdi->spec_is_dwz, cu);
8491 real_pdi = res.pdi;
8492 cu = res.cu;
8493 }
8494
8495 parent = real_pdi->die_parent;
8496 if (parent == NULL)
8497 return NULL;
8498
8499 if (parent->scope_set)
8500 return parent->scope;
8501
8502 parent->fixup (cu);
8503
8504 grandparent_scope = partial_die_parent_scope (parent, cu);
8505
8506 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8507 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8508 Work around this problem here. */
8509 if (cu->language == language_cplus
8510 && parent->tag == DW_TAG_namespace
8511 && strcmp (parent->name, "::") == 0
8512 && grandparent_scope == NULL)
8513 {
8514 parent->scope = NULL;
8515 parent->scope_set = 1;
8516 return NULL;
8517 }
8518
8519 /* Nested subroutines in Fortran get a prefix. */
8520 if (pdi->tag == DW_TAG_enumerator)
8521 /* Enumerators should not get the name of the enumeration as a prefix. */
8522 parent->scope = grandparent_scope;
8523 else if (parent->tag == DW_TAG_namespace
8524 || parent->tag == DW_TAG_module
8525 || parent->tag == DW_TAG_structure_type
8526 || parent->tag == DW_TAG_class_type
8527 || parent->tag == DW_TAG_interface_type
8528 || parent->tag == DW_TAG_union_type
8529 || parent->tag == DW_TAG_enumeration_type
8530 || (cu->language == language_fortran
8531 && parent->tag == DW_TAG_subprogram
8532 && pdi->tag == DW_TAG_subprogram))
8533 {
8534 if (grandparent_scope == NULL)
8535 parent->scope = parent->name;
8536 else
8537 parent->scope = typename_concat (&cu->comp_unit_obstack,
8538 grandparent_scope,
8539 parent->name, 0, cu);
8540 }
8541 else
8542 {
8543 /* FIXME drow/2004-04-01: What should we be doing with
8544 function-local names? For partial symbols, we should probably be
8545 ignoring them. */
8546 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8547 dwarf_tag_name (parent->tag),
8548 sect_offset_str (pdi->sect_off));
8549 parent->scope = grandparent_scope;
8550 }
8551
8552 parent->scope_set = 1;
8553 return parent->scope;
8554 }
8555
8556 /* Return the fully scoped name associated with PDI, from compilation unit
8557 CU. The result will be allocated with malloc. */
8558
8559 static gdb::unique_xmalloc_ptr<char>
8560 partial_die_full_name (struct partial_die_info *pdi,
8561 struct dwarf2_cu *cu)
8562 {
8563 const char *parent_scope;
8564
8565 /* If this is a template instantiation, we can not work out the
8566 template arguments from partial DIEs. So, unfortunately, we have
8567 to go through the full DIEs. At least any work we do building
8568 types here will be reused if full symbols are loaded later. */
8569 if (pdi->has_template_arguments)
8570 {
8571 pdi->fixup (cu);
8572
8573 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8574 {
8575 struct die_info *die;
8576 struct attribute attr;
8577 struct dwarf2_cu *ref_cu = cu;
8578
8579 /* DW_FORM_ref_addr is using section offset. */
8580 attr.name = (enum dwarf_attribute) 0;
8581 attr.form = DW_FORM_ref_addr;
8582 attr.u.unsnd = to_underlying (pdi->sect_off);
8583 die = follow_die_ref (NULL, &attr, &ref_cu);
8584
8585 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8586 }
8587 }
8588
8589 parent_scope = partial_die_parent_scope (pdi, cu);
8590 if (parent_scope == NULL)
8591 return NULL;
8592 else
8593 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8594 pdi->name, 0, cu));
8595 }
8596
8597 static void
8598 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8599 {
8600 struct dwarf2_per_objfile *dwarf2_per_objfile
8601 = cu->per_cu->dwarf2_per_objfile;
8602 struct objfile *objfile = dwarf2_per_objfile->objfile;
8603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8604 CORE_ADDR addr = 0;
8605 const char *actual_name = NULL;
8606 CORE_ADDR baseaddr;
8607
8608 baseaddr = objfile->text_section_offset ();
8609
8610 gdb::unique_xmalloc_ptr<char> built_actual_name
8611 = partial_die_full_name (pdi, cu);
8612 if (built_actual_name != NULL)
8613 actual_name = built_actual_name.get ();
8614
8615 if (actual_name == NULL)
8616 actual_name = pdi->name;
8617
8618 switch (pdi->tag)
8619 {
8620 case DW_TAG_inlined_subroutine:
8621 case DW_TAG_subprogram:
8622 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8623 - baseaddr);
8624 if (pdi->is_external
8625 || cu->language == language_ada
8626 || (cu->language == language_fortran
8627 && pdi->die_parent != NULL
8628 && pdi->die_parent->tag == DW_TAG_subprogram))
8629 {
8630 /* Normally, only "external" DIEs are part of the global scope.
8631 But in Ada and Fortran, we want to be able to access nested
8632 procedures globally. So all Ada and Fortran subprograms are
8633 stored in the global scope. */
8634 add_psymbol_to_list (actual_name,
8635 built_actual_name != NULL,
8636 VAR_DOMAIN, LOC_BLOCK,
8637 SECT_OFF_TEXT (objfile),
8638 psymbol_placement::GLOBAL,
8639 addr,
8640 cu->language, objfile);
8641 }
8642 else
8643 {
8644 add_psymbol_to_list (actual_name,
8645 built_actual_name != NULL,
8646 VAR_DOMAIN, LOC_BLOCK,
8647 SECT_OFF_TEXT (objfile),
8648 psymbol_placement::STATIC,
8649 addr, cu->language, objfile);
8650 }
8651
8652 if (pdi->main_subprogram && actual_name != NULL)
8653 set_objfile_main_name (objfile, actual_name, cu->language);
8654 break;
8655 case DW_TAG_constant:
8656 add_psymbol_to_list (actual_name,
8657 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8658 -1, (pdi->is_external
8659 ? psymbol_placement::GLOBAL
8660 : psymbol_placement::STATIC),
8661 0, cu->language, objfile);
8662 break;
8663 case DW_TAG_variable:
8664 if (pdi->d.locdesc)
8665 addr = decode_locdesc (pdi->d.locdesc, cu);
8666
8667 if (pdi->d.locdesc
8668 && addr == 0
8669 && !dwarf2_per_objfile->has_section_at_zero)
8670 {
8671 /* A global or static variable may also have been stripped
8672 out by the linker if unused, in which case its address
8673 will be nullified; do not add such variables into partial
8674 symbol table then. */
8675 }
8676 else if (pdi->is_external)
8677 {
8678 /* Global Variable.
8679 Don't enter into the minimal symbol tables as there is
8680 a minimal symbol table entry from the ELF symbols already.
8681 Enter into partial symbol table if it has a location
8682 descriptor or a type.
8683 If the location descriptor is missing, new_symbol will create
8684 a LOC_UNRESOLVED symbol, the address of the variable will then
8685 be determined from the minimal symbol table whenever the variable
8686 is referenced.
8687 The address for the partial symbol table entry is not
8688 used by GDB, but it comes in handy for debugging partial symbol
8689 table building. */
8690
8691 if (pdi->d.locdesc || pdi->has_type)
8692 add_psymbol_to_list (actual_name,
8693 built_actual_name != NULL,
8694 VAR_DOMAIN, LOC_STATIC,
8695 SECT_OFF_TEXT (objfile),
8696 psymbol_placement::GLOBAL,
8697 addr, cu->language, objfile);
8698 }
8699 else
8700 {
8701 int has_loc = pdi->d.locdesc != NULL;
8702
8703 /* Static Variable. Skip symbols whose value we cannot know (those
8704 without location descriptors or constant values). */
8705 if (!has_loc && !pdi->has_const_value)
8706 return;
8707
8708 add_psymbol_to_list (actual_name,
8709 built_actual_name != NULL,
8710 VAR_DOMAIN, LOC_STATIC,
8711 SECT_OFF_TEXT (objfile),
8712 psymbol_placement::STATIC,
8713 has_loc ? addr : 0,
8714 cu->language, objfile);
8715 }
8716 break;
8717 case DW_TAG_typedef:
8718 case DW_TAG_base_type:
8719 case DW_TAG_subrange_type:
8720 add_psymbol_to_list (actual_name,
8721 built_actual_name != NULL,
8722 VAR_DOMAIN, LOC_TYPEDEF, -1,
8723 psymbol_placement::STATIC,
8724 0, cu->language, objfile);
8725 break;
8726 case DW_TAG_imported_declaration:
8727 case DW_TAG_namespace:
8728 add_psymbol_to_list (actual_name,
8729 built_actual_name != NULL,
8730 VAR_DOMAIN, LOC_TYPEDEF, -1,
8731 psymbol_placement::GLOBAL,
8732 0, cu->language, objfile);
8733 break;
8734 case DW_TAG_module:
8735 /* With Fortran 77 there might be a "BLOCK DATA" module
8736 available without any name. If so, we skip the module as it
8737 doesn't bring any value. */
8738 if (actual_name != nullptr)
8739 add_psymbol_to_list (actual_name,
8740 built_actual_name != NULL,
8741 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8742 psymbol_placement::GLOBAL,
8743 0, cu->language, objfile);
8744 break;
8745 case DW_TAG_class_type:
8746 case DW_TAG_interface_type:
8747 case DW_TAG_structure_type:
8748 case DW_TAG_union_type:
8749 case DW_TAG_enumeration_type:
8750 /* Skip external references. The DWARF standard says in the section
8751 about "Structure, Union, and Class Type Entries": "An incomplete
8752 structure, union or class type is represented by a structure,
8753 union or class entry that does not have a byte size attribute
8754 and that has a DW_AT_declaration attribute." */
8755 if (!pdi->has_byte_size && pdi->is_declaration)
8756 return;
8757
8758 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8759 static vs. global. */
8760 add_psymbol_to_list (actual_name,
8761 built_actual_name != NULL,
8762 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8763 cu->language == language_cplus
8764 ? psymbol_placement::GLOBAL
8765 : psymbol_placement::STATIC,
8766 0, cu->language, objfile);
8767
8768 break;
8769 case DW_TAG_enumerator:
8770 add_psymbol_to_list (actual_name,
8771 built_actual_name != NULL,
8772 VAR_DOMAIN, LOC_CONST, -1,
8773 cu->language == language_cplus
8774 ? psymbol_placement::GLOBAL
8775 : psymbol_placement::STATIC,
8776 0, cu->language, objfile);
8777 break;
8778 default:
8779 break;
8780 }
8781 }
8782
8783 /* Read a partial die corresponding to a namespace; also, add a symbol
8784 corresponding to that namespace to the symbol table. NAMESPACE is
8785 the name of the enclosing namespace. */
8786
8787 static void
8788 add_partial_namespace (struct partial_die_info *pdi,
8789 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8790 int set_addrmap, struct dwarf2_cu *cu)
8791 {
8792 /* Add a symbol for the namespace. */
8793
8794 add_partial_symbol (pdi, cu);
8795
8796 /* Now scan partial symbols in that namespace. */
8797
8798 if (pdi->has_children)
8799 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8800 }
8801
8802 /* Read a partial die corresponding to a Fortran module. */
8803
8804 static void
8805 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8806 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8807 {
8808 /* Add a symbol for the namespace. */
8809
8810 add_partial_symbol (pdi, cu);
8811
8812 /* Now scan partial symbols in that module. */
8813
8814 if (pdi->has_children)
8815 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8816 }
8817
8818 /* Read a partial die corresponding to a subprogram or an inlined
8819 subprogram and create a partial symbol for that subprogram.
8820 When the CU language allows it, this routine also defines a partial
8821 symbol for each nested subprogram that this subprogram contains.
8822 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8823 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8824
8825 PDI may also be a lexical block, in which case we simply search
8826 recursively for subprograms defined inside that lexical block.
8827 Again, this is only performed when the CU language allows this
8828 type of definitions. */
8829
8830 static void
8831 add_partial_subprogram (struct partial_die_info *pdi,
8832 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8833 int set_addrmap, struct dwarf2_cu *cu)
8834 {
8835 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8836 {
8837 if (pdi->has_pc_info)
8838 {
8839 if (pdi->lowpc < *lowpc)
8840 *lowpc = pdi->lowpc;
8841 if (pdi->highpc > *highpc)
8842 *highpc = pdi->highpc;
8843 if (set_addrmap)
8844 {
8845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8847 CORE_ADDR baseaddr;
8848 CORE_ADDR this_highpc;
8849 CORE_ADDR this_lowpc;
8850
8851 baseaddr = objfile->text_section_offset ();
8852 this_lowpc
8853 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8854 pdi->lowpc + baseaddr)
8855 - baseaddr);
8856 this_highpc
8857 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8858 pdi->highpc + baseaddr)
8859 - baseaddr);
8860 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8861 this_lowpc, this_highpc - 1,
8862 cu->per_cu->v.psymtab);
8863 }
8864 }
8865
8866 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8867 {
8868 if (!pdi->is_declaration)
8869 /* Ignore subprogram DIEs that do not have a name, they are
8870 illegal. Do not emit a complaint at this point, we will
8871 do so when we convert this psymtab into a symtab. */
8872 if (pdi->name)
8873 add_partial_symbol (pdi, cu);
8874 }
8875 }
8876
8877 if (! pdi->has_children)
8878 return;
8879
8880 if (cu->language == language_ada || cu->language == language_fortran)
8881 {
8882 pdi = pdi->die_child;
8883 while (pdi != NULL)
8884 {
8885 pdi->fixup (cu);
8886 if (pdi->tag == DW_TAG_subprogram
8887 || pdi->tag == DW_TAG_inlined_subroutine
8888 || pdi->tag == DW_TAG_lexical_block)
8889 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8890 pdi = pdi->die_sibling;
8891 }
8892 }
8893 }
8894
8895 /* Read a partial die corresponding to an enumeration type. */
8896
8897 static void
8898 add_partial_enumeration (struct partial_die_info *enum_pdi,
8899 struct dwarf2_cu *cu)
8900 {
8901 struct partial_die_info *pdi;
8902
8903 if (enum_pdi->name != NULL)
8904 add_partial_symbol (enum_pdi, cu);
8905
8906 pdi = enum_pdi->die_child;
8907 while (pdi)
8908 {
8909 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8910 complaint (_("malformed enumerator DIE ignored"));
8911 else
8912 add_partial_symbol (pdi, cu);
8913 pdi = pdi->die_sibling;
8914 }
8915 }
8916
8917 /* Return the initial uleb128 in the die at INFO_PTR. */
8918
8919 static unsigned int
8920 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8921 {
8922 unsigned int bytes_read;
8923
8924 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8925 }
8926
8927 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8928 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8929
8930 Return the corresponding abbrev, or NULL if the number is zero (indicating
8931 an empty DIE). In either case *BYTES_READ will be set to the length of
8932 the initial number. */
8933
8934 static struct abbrev_info *
8935 peek_die_abbrev (const die_reader_specs &reader,
8936 const gdb_byte *info_ptr, unsigned int *bytes_read)
8937 {
8938 dwarf2_cu *cu = reader.cu;
8939 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8940 unsigned int abbrev_number
8941 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8942
8943 if (abbrev_number == 0)
8944 return NULL;
8945
8946 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8947 if (!abbrev)
8948 {
8949 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8950 " at offset %s [in module %s]"),
8951 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8952 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8953 }
8954
8955 return abbrev;
8956 }
8957
8958 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8959 Returns a pointer to the end of a series of DIEs, terminated by an empty
8960 DIE. Any children of the skipped DIEs will also be skipped. */
8961
8962 static const gdb_byte *
8963 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8964 {
8965 while (1)
8966 {
8967 unsigned int bytes_read;
8968 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8969
8970 if (abbrev == NULL)
8971 return info_ptr + bytes_read;
8972 else
8973 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8974 }
8975 }
8976
8977 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8978 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8979 abbrev corresponding to that skipped uleb128 should be passed in
8980 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8981 children. */
8982
8983 static const gdb_byte *
8984 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8985 struct abbrev_info *abbrev)
8986 {
8987 unsigned int bytes_read;
8988 struct attribute attr;
8989 bfd *abfd = reader->abfd;
8990 struct dwarf2_cu *cu = reader->cu;
8991 const gdb_byte *buffer = reader->buffer;
8992 const gdb_byte *buffer_end = reader->buffer_end;
8993 unsigned int form, i;
8994
8995 for (i = 0; i < abbrev->num_attrs; i++)
8996 {
8997 /* The only abbrev we care about is DW_AT_sibling. */
8998 if (abbrev->attrs[i].name == DW_AT_sibling)
8999 {
9000 bool ignored;
9001 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9002 &ignored);
9003 if (attr.form == DW_FORM_ref_addr)
9004 complaint (_("ignoring absolute DW_AT_sibling"));
9005 else
9006 {
9007 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9008 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9009
9010 if (sibling_ptr < info_ptr)
9011 complaint (_("DW_AT_sibling points backwards"));
9012 else if (sibling_ptr > reader->buffer_end)
9013 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9014 else
9015 return sibling_ptr;
9016 }
9017 }
9018
9019 /* If it isn't DW_AT_sibling, skip this attribute. */
9020 form = abbrev->attrs[i].form;
9021 skip_attribute:
9022 switch (form)
9023 {
9024 case DW_FORM_ref_addr:
9025 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9026 and later it is offset sized. */
9027 if (cu->header.version == 2)
9028 info_ptr += cu->header.addr_size;
9029 else
9030 info_ptr += cu->header.offset_size;
9031 break;
9032 case DW_FORM_GNU_ref_alt:
9033 info_ptr += cu->header.offset_size;
9034 break;
9035 case DW_FORM_addr:
9036 info_ptr += cu->header.addr_size;
9037 break;
9038 case DW_FORM_data1:
9039 case DW_FORM_ref1:
9040 case DW_FORM_flag:
9041 case DW_FORM_strx1:
9042 info_ptr += 1;
9043 break;
9044 case DW_FORM_flag_present:
9045 case DW_FORM_implicit_const:
9046 break;
9047 case DW_FORM_data2:
9048 case DW_FORM_ref2:
9049 case DW_FORM_strx2:
9050 info_ptr += 2;
9051 break;
9052 case DW_FORM_strx3:
9053 info_ptr += 3;
9054 break;
9055 case DW_FORM_data4:
9056 case DW_FORM_ref4:
9057 case DW_FORM_strx4:
9058 info_ptr += 4;
9059 break;
9060 case DW_FORM_data8:
9061 case DW_FORM_ref8:
9062 case DW_FORM_ref_sig8:
9063 info_ptr += 8;
9064 break;
9065 case DW_FORM_data16:
9066 info_ptr += 16;
9067 break;
9068 case DW_FORM_string:
9069 read_direct_string (abfd, info_ptr, &bytes_read);
9070 info_ptr += bytes_read;
9071 break;
9072 case DW_FORM_sec_offset:
9073 case DW_FORM_strp:
9074 case DW_FORM_GNU_strp_alt:
9075 info_ptr += cu->header.offset_size;
9076 break;
9077 case DW_FORM_exprloc:
9078 case DW_FORM_block:
9079 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9080 info_ptr += bytes_read;
9081 break;
9082 case DW_FORM_block1:
9083 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9084 break;
9085 case DW_FORM_block2:
9086 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9087 break;
9088 case DW_FORM_block4:
9089 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9090 break;
9091 case DW_FORM_addrx:
9092 case DW_FORM_strx:
9093 case DW_FORM_sdata:
9094 case DW_FORM_udata:
9095 case DW_FORM_ref_udata:
9096 case DW_FORM_GNU_addr_index:
9097 case DW_FORM_GNU_str_index:
9098 case DW_FORM_rnglistx:
9099 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9100 break;
9101 case DW_FORM_indirect:
9102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9103 info_ptr += bytes_read;
9104 /* We need to continue parsing from here, so just go back to
9105 the top. */
9106 goto skip_attribute;
9107
9108 default:
9109 error (_("Dwarf Error: Cannot handle %s "
9110 "in DWARF reader [in module %s]"),
9111 dwarf_form_name (form),
9112 bfd_get_filename (abfd));
9113 }
9114 }
9115
9116 if (abbrev->has_children)
9117 return skip_children (reader, info_ptr);
9118 else
9119 return info_ptr;
9120 }
9121
9122 /* Locate ORIG_PDI's sibling.
9123 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9124
9125 static const gdb_byte *
9126 locate_pdi_sibling (const struct die_reader_specs *reader,
9127 struct partial_die_info *orig_pdi,
9128 const gdb_byte *info_ptr)
9129 {
9130 /* Do we know the sibling already? */
9131
9132 if (orig_pdi->sibling)
9133 return orig_pdi->sibling;
9134
9135 /* Are there any children to deal with? */
9136
9137 if (!orig_pdi->has_children)
9138 return info_ptr;
9139
9140 /* Skip the children the long way. */
9141
9142 return skip_children (reader, info_ptr);
9143 }
9144
9145 /* Expand this partial symbol table into a full symbol table. SELF is
9146 not NULL. */
9147
9148 void
9149 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9150 {
9151 struct dwarf2_per_objfile *dwarf2_per_objfile
9152 = get_dwarf2_per_objfile (objfile);
9153
9154 gdb_assert (!readin);
9155 /* If this psymtab is constructed from a debug-only objfile, the
9156 has_section_at_zero flag will not necessarily be correct. We
9157 can get the correct value for this flag by looking at the data
9158 associated with the (presumably stripped) associated objfile. */
9159 if (objfile->separate_debug_objfile_backlink)
9160 {
9161 struct dwarf2_per_objfile *dpo_backlink
9162 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9163
9164 dwarf2_per_objfile->has_section_at_zero
9165 = dpo_backlink->has_section_at_zero;
9166 }
9167
9168 dwarf2_per_objfile->reading_partial_symbols = 0;
9169
9170 expand_psymtab (objfile);
9171
9172 process_cu_includes (dwarf2_per_objfile);
9173 }
9174 \f
9175 /* Reading in full CUs. */
9176
9177 /* Add PER_CU to the queue. */
9178
9179 static void
9180 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9181 enum language pretend_language)
9182 {
9183 struct dwarf2_queue_item *item;
9184
9185 per_cu->queued = 1;
9186 item = XNEW (struct dwarf2_queue_item);
9187 item->per_cu = per_cu;
9188 item->pretend_language = pretend_language;
9189 item->next = NULL;
9190
9191 if (dwarf2_queue == NULL)
9192 dwarf2_queue = item;
9193 else
9194 dwarf2_queue_tail->next = item;
9195
9196 dwarf2_queue_tail = item;
9197 }
9198
9199 /* If PER_CU is not yet queued, add it to the queue.
9200 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9201 dependency.
9202 The result is non-zero if PER_CU was queued, otherwise the result is zero
9203 meaning either PER_CU is already queued or it is already loaded.
9204
9205 N.B. There is an invariant here that if a CU is queued then it is loaded.
9206 The caller is required to load PER_CU if we return non-zero. */
9207
9208 static int
9209 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9210 struct dwarf2_per_cu_data *per_cu,
9211 enum language pretend_language)
9212 {
9213 /* We may arrive here during partial symbol reading, if we need full
9214 DIEs to process an unusual case (e.g. template arguments). Do
9215 not queue PER_CU, just tell our caller to load its DIEs. */
9216 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9217 {
9218 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9219 return 1;
9220 return 0;
9221 }
9222
9223 /* Mark the dependence relation so that we don't flush PER_CU
9224 too early. */
9225 if (dependent_cu != NULL)
9226 dwarf2_add_dependence (dependent_cu, per_cu);
9227
9228 /* If it's already on the queue, we have nothing to do. */
9229 if (per_cu->queued)
9230 return 0;
9231
9232 /* If the compilation unit is already loaded, just mark it as
9233 used. */
9234 if (per_cu->cu != NULL)
9235 {
9236 per_cu->cu->last_used = 0;
9237 return 0;
9238 }
9239
9240 /* Add it to the queue. */
9241 queue_comp_unit (per_cu, pretend_language);
9242
9243 return 1;
9244 }
9245
9246 /* Process the queue. */
9247
9248 static void
9249 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9250 {
9251 struct dwarf2_queue_item *item, *next_item;
9252
9253 if (dwarf_read_debug)
9254 {
9255 fprintf_unfiltered (gdb_stdlog,
9256 "Expanding one or more symtabs of objfile %s ...\n",
9257 objfile_name (dwarf2_per_objfile->objfile));
9258 }
9259
9260 /* The queue starts out with one item, but following a DIE reference
9261 may load a new CU, adding it to the end of the queue. */
9262 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9263 {
9264 if ((dwarf2_per_objfile->using_index
9265 ? !item->per_cu->v.quick->compunit_symtab
9266 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9267 /* Skip dummy CUs. */
9268 && item->per_cu->cu != NULL)
9269 {
9270 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9271 unsigned int debug_print_threshold;
9272 char buf[100];
9273
9274 if (per_cu->is_debug_types)
9275 {
9276 struct signatured_type *sig_type =
9277 (struct signatured_type *) per_cu;
9278
9279 sprintf (buf, "TU %s at offset %s",
9280 hex_string (sig_type->signature),
9281 sect_offset_str (per_cu->sect_off));
9282 /* There can be 100s of TUs.
9283 Only print them in verbose mode. */
9284 debug_print_threshold = 2;
9285 }
9286 else
9287 {
9288 sprintf (buf, "CU at offset %s",
9289 sect_offset_str (per_cu->sect_off));
9290 debug_print_threshold = 1;
9291 }
9292
9293 if (dwarf_read_debug >= debug_print_threshold)
9294 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9295
9296 if (per_cu->is_debug_types)
9297 process_full_type_unit (per_cu, item->pretend_language);
9298 else
9299 process_full_comp_unit (per_cu, item->pretend_language);
9300
9301 if (dwarf_read_debug >= debug_print_threshold)
9302 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9303 }
9304
9305 item->per_cu->queued = 0;
9306 next_item = item->next;
9307 xfree (item);
9308 }
9309
9310 dwarf2_queue_tail = NULL;
9311
9312 if (dwarf_read_debug)
9313 {
9314 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9315 objfile_name (dwarf2_per_objfile->objfile));
9316 }
9317 }
9318
9319 /* Read in full symbols for PST, and anything it depends on. */
9320
9321 void
9322 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9323 {
9324 struct dwarf2_per_cu_data *per_cu;
9325
9326 if (readin)
9327 return;
9328
9329 read_dependencies (objfile);
9330
9331 per_cu = per_cu_data;
9332
9333 if (per_cu == NULL)
9334 {
9335 /* It's an include file, no symbols to read for it.
9336 Everything is in the parent symtab. */
9337 readin = true;
9338 return;
9339 }
9340
9341 dw2_do_instantiate_symtab (per_cu, false);
9342 }
9343
9344 /* Trivial hash function for die_info: the hash value of a DIE
9345 is its offset in .debug_info for this objfile. */
9346
9347 static hashval_t
9348 die_hash (const void *item)
9349 {
9350 const struct die_info *die = (const struct die_info *) item;
9351
9352 return to_underlying (die->sect_off);
9353 }
9354
9355 /* Trivial comparison function for die_info structures: two DIEs
9356 are equal if they have the same offset. */
9357
9358 static int
9359 die_eq (const void *item_lhs, const void *item_rhs)
9360 {
9361 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9362 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9363
9364 return die_lhs->sect_off == die_rhs->sect_off;
9365 }
9366
9367 /* Load the DIEs associated with PER_CU into memory. */
9368
9369 static void
9370 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9371 bool skip_partial,
9372 enum language pretend_language)
9373 {
9374 gdb_assert (! this_cu->is_debug_types);
9375
9376 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9377 if (reader.dummy_p)
9378 return;
9379
9380 struct dwarf2_cu *cu = reader.cu;
9381 const gdb_byte *info_ptr = reader.info_ptr;
9382
9383 gdb_assert (cu->die_hash == NULL);
9384 cu->die_hash =
9385 htab_create_alloc_ex (cu->header.length / 12,
9386 die_hash,
9387 die_eq,
9388 NULL,
9389 &cu->comp_unit_obstack,
9390 hashtab_obstack_allocate,
9391 dummy_obstack_deallocate);
9392
9393 if (reader.has_children)
9394 reader.comp_unit_die->child
9395 = read_die_and_siblings (&reader, reader.info_ptr,
9396 &info_ptr, reader.comp_unit_die);
9397 cu->dies = reader.comp_unit_die;
9398 /* comp_unit_die is not stored in die_hash, no need. */
9399
9400 /* We try not to read any attributes in this function, because not
9401 all CUs needed for references have been loaded yet, and symbol
9402 table processing isn't initialized. But we have to set the CU language,
9403 or we won't be able to build types correctly.
9404 Similarly, if we do not read the producer, we can not apply
9405 producer-specific interpretation. */
9406 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9407 }
9408
9409 /* Add a DIE to the delayed physname list. */
9410
9411 static void
9412 add_to_method_list (struct type *type, int fnfield_index, int index,
9413 const char *name, struct die_info *die,
9414 struct dwarf2_cu *cu)
9415 {
9416 struct delayed_method_info mi;
9417 mi.type = type;
9418 mi.fnfield_index = fnfield_index;
9419 mi.index = index;
9420 mi.name = name;
9421 mi.die = die;
9422 cu->method_list.push_back (mi);
9423 }
9424
9425 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9426 "const" / "volatile". If so, decrements LEN by the length of the
9427 modifier and return true. Otherwise return false. */
9428
9429 template<size_t N>
9430 static bool
9431 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9432 {
9433 size_t mod_len = sizeof (mod) - 1;
9434 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9435 {
9436 len -= mod_len;
9437 return true;
9438 }
9439 return false;
9440 }
9441
9442 /* Compute the physnames of any methods on the CU's method list.
9443
9444 The computation of method physnames is delayed in order to avoid the
9445 (bad) condition that one of the method's formal parameters is of an as yet
9446 incomplete type. */
9447
9448 static void
9449 compute_delayed_physnames (struct dwarf2_cu *cu)
9450 {
9451 /* Only C++ delays computing physnames. */
9452 if (cu->method_list.empty ())
9453 return;
9454 gdb_assert (cu->language == language_cplus);
9455
9456 for (const delayed_method_info &mi : cu->method_list)
9457 {
9458 const char *physname;
9459 struct fn_fieldlist *fn_flp
9460 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9461 physname = dwarf2_physname (mi.name, mi.die, cu);
9462 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9463 = physname ? physname : "";
9464
9465 /* Since there's no tag to indicate whether a method is a
9466 const/volatile overload, extract that information out of the
9467 demangled name. */
9468 if (physname != NULL)
9469 {
9470 size_t len = strlen (physname);
9471
9472 while (1)
9473 {
9474 if (physname[len] == ')') /* shortcut */
9475 break;
9476 else if (check_modifier (physname, len, " const"))
9477 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9478 else if (check_modifier (physname, len, " volatile"))
9479 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9480 else
9481 break;
9482 }
9483 }
9484 }
9485
9486 /* The list is no longer needed. */
9487 cu->method_list.clear ();
9488 }
9489
9490 /* Go objects should be embedded in a DW_TAG_module DIE,
9491 and it's not clear if/how imported objects will appear.
9492 To keep Go support simple until that's worked out,
9493 go back through what we've read and create something usable.
9494 We could do this while processing each DIE, and feels kinda cleaner,
9495 but that way is more invasive.
9496 This is to, for example, allow the user to type "p var" or "b main"
9497 without having to specify the package name, and allow lookups
9498 of module.object to work in contexts that use the expression
9499 parser. */
9500
9501 static void
9502 fixup_go_packaging (struct dwarf2_cu *cu)
9503 {
9504 gdb::unique_xmalloc_ptr<char> package_name;
9505 struct pending *list;
9506 int i;
9507
9508 for (list = *cu->get_builder ()->get_global_symbols ();
9509 list != NULL;
9510 list = list->next)
9511 {
9512 for (i = 0; i < list->nsyms; ++i)
9513 {
9514 struct symbol *sym = list->symbol[i];
9515
9516 if (sym->language () == language_go
9517 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9518 {
9519 gdb::unique_xmalloc_ptr<char> this_package_name
9520 (go_symbol_package_name (sym));
9521
9522 if (this_package_name == NULL)
9523 continue;
9524 if (package_name == NULL)
9525 package_name = std::move (this_package_name);
9526 else
9527 {
9528 struct objfile *objfile
9529 = cu->per_cu->dwarf2_per_objfile->objfile;
9530 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9531 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9532 (symbol_symtab (sym) != NULL
9533 ? symtab_to_filename_for_display
9534 (symbol_symtab (sym))
9535 : objfile_name (objfile)),
9536 this_package_name.get (), package_name.get ());
9537 }
9538 }
9539 }
9540 }
9541
9542 if (package_name != NULL)
9543 {
9544 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9545 const char *saved_package_name
9546 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9547 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9548 saved_package_name);
9549 struct symbol *sym;
9550
9551 sym = allocate_symbol (objfile);
9552 sym->set_language (language_go, &objfile->objfile_obstack);
9553 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9554 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9555 e.g., "main" finds the "main" module and not C's main(). */
9556 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9557 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9558 SYMBOL_TYPE (sym) = type;
9559
9560 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9561 }
9562 }
9563
9564 /* Allocate a fully-qualified name consisting of the two parts on the
9565 obstack. */
9566
9567 static const char *
9568 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9569 {
9570 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9571 }
9572
9573 /* A helper that allocates a struct discriminant_info to attach to a
9574 union type. */
9575
9576 static struct discriminant_info *
9577 alloc_discriminant_info (struct type *type, int discriminant_index,
9578 int default_index)
9579 {
9580 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9581 gdb_assert (discriminant_index == -1
9582 || (discriminant_index >= 0
9583 && discriminant_index < TYPE_NFIELDS (type)));
9584 gdb_assert (default_index == -1
9585 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9586
9587 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9588
9589 struct discriminant_info *disc
9590 = ((struct discriminant_info *)
9591 TYPE_ZALLOC (type,
9592 offsetof (struct discriminant_info, discriminants)
9593 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9594 disc->default_index = default_index;
9595 disc->discriminant_index = discriminant_index;
9596
9597 struct dynamic_prop prop;
9598 prop.kind = PROP_UNDEFINED;
9599 prop.data.baton = disc;
9600
9601 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9602
9603 return disc;
9604 }
9605
9606 /* Some versions of rustc emitted enums in an unusual way.
9607
9608 Ordinary enums were emitted as unions. The first element of each
9609 structure in the union was named "RUST$ENUM$DISR". This element
9610 held the discriminant.
9611
9612 These versions of Rust also implemented the "non-zero"
9613 optimization. When the enum had two values, and one is empty and
9614 the other holds a pointer that cannot be zero, the pointer is used
9615 as the discriminant, with a zero value meaning the empty variant.
9616 Here, the union's first member is of the form
9617 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9618 where the fieldnos are the indices of the fields that should be
9619 traversed in order to find the field (which may be several fields deep)
9620 and the variantname is the name of the variant of the case when the
9621 field is zero.
9622
9623 This function recognizes whether TYPE is of one of these forms,
9624 and, if so, smashes it to be a variant type. */
9625
9626 static void
9627 quirk_rust_enum (struct type *type, struct objfile *objfile)
9628 {
9629 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9630
9631 /* We don't need to deal with empty enums. */
9632 if (TYPE_NFIELDS (type) == 0)
9633 return;
9634
9635 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9636 if (TYPE_NFIELDS (type) == 1
9637 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9638 {
9639 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9640
9641 /* Decode the field name to find the offset of the
9642 discriminant. */
9643 ULONGEST bit_offset = 0;
9644 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9645 while (name[0] >= '0' && name[0] <= '9')
9646 {
9647 char *tail;
9648 unsigned long index = strtoul (name, &tail, 10);
9649 name = tail;
9650 if (*name != '$'
9651 || index >= TYPE_NFIELDS (field_type)
9652 || (TYPE_FIELD_LOC_KIND (field_type, index)
9653 != FIELD_LOC_KIND_BITPOS))
9654 {
9655 complaint (_("Could not parse Rust enum encoding string \"%s\""
9656 "[in module %s]"),
9657 TYPE_FIELD_NAME (type, 0),
9658 objfile_name (objfile));
9659 return;
9660 }
9661 ++name;
9662
9663 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9664 field_type = TYPE_FIELD_TYPE (field_type, index);
9665 }
9666
9667 /* Make a union to hold the variants. */
9668 struct type *union_type = alloc_type (objfile);
9669 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9670 TYPE_NFIELDS (union_type) = 3;
9671 TYPE_FIELDS (union_type)
9672 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9673 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9674 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9675
9676 /* Put the discriminant must at index 0. */
9677 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9678 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9679 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9680 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9681
9682 /* The order of fields doesn't really matter, so put the real
9683 field at index 1 and the data-less field at index 2. */
9684 struct discriminant_info *disc
9685 = alloc_discriminant_info (union_type, 0, 1);
9686 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9687 TYPE_FIELD_NAME (union_type, 1)
9688 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9689 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9690 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9691 TYPE_FIELD_NAME (union_type, 1));
9692
9693 const char *dataless_name
9694 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9695 name);
9696 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9697 dataless_name);
9698 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9699 /* NAME points into the original discriminant name, which
9700 already has the correct lifetime. */
9701 TYPE_FIELD_NAME (union_type, 2) = name;
9702 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9703 disc->discriminants[2] = 0;
9704
9705 /* Smash this type to be a structure type. We have to do this
9706 because the type has already been recorded. */
9707 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9708 TYPE_NFIELDS (type) = 1;
9709 TYPE_FIELDS (type)
9710 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9711
9712 /* Install the variant part. */
9713 TYPE_FIELD_TYPE (type, 0) = union_type;
9714 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9715 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9716 }
9717 /* A union with a single anonymous field is probably an old-style
9718 univariant enum. */
9719 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9720 {
9721 /* Smash this type to be a structure type. We have to do this
9722 because the type has already been recorded. */
9723 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9724
9725 /* Make a union to hold the variants. */
9726 struct type *union_type = alloc_type (objfile);
9727 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9728 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9729 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9730 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9731 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9732
9733 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9734 const char *variant_name
9735 = rust_last_path_segment (TYPE_NAME (field_type));
9736 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9737 TYPE_NAME (field_type)
9738 = rust_fully_qualify (&objfile->objfile_obstack,
9739 TYPE_NAME (type), variant_name);
9740
9741 /* Install the union in the outer struct type. */
9742 TYPE_NFIELDS (type) = 1;
9743 TYPE_FIELDS (type)
9744 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9745 TYPE_FIELD_TYPE (type, 0) = union_type;
9746 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9747 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9748
9749 alloc_discriminant_info (union_type, -1, 0);
9750 }
9751 else
9752 {
9753 struct type *disr_type = nullptr;
9754 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9755 {
9756 disr_type = TYPE_FIELD_TYPE (type, i);
9757
9758 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9759 {
9760 /* All fields of a true enum will be structs. */
9761 return;
9762 }
9763 else if (TYPE_NFIELDS (disr_type) == 0)
9764 {
9765 /* Could be data-less variant, so keep going. */
9766 disr_type = nullptr;
9767 }
9768 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9769 "RUST$ENUM$DISR") != 0)
9770 {
9771 /* Not a Rust enum. */
9772 return;
9773 }
9774 else
9775 {
9776 /* Found one. */
9777 break;
9778 }
9779 }
9780
9781 /* If we got here without a discriminant, then it's probably
9782 just a union. */
9783 if (disr_type == nullptr)
9784 return;
9785
9786 /* Smash this type to be a structure type. We have to do this
9787 because the type has already been recorded. */
9788 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9789
9790 /* Make a union to hold the variants. */
9791 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9792 struct type *union_type = alloc_type (objfile);
9793 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9794 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9795 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9796 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9797 TYPE_FIELDS (union_type)
9798 = (struct field *) TYPE_ZALLOC (union_type,
9799 (TYPE_NFIELDS (union_type)
9800 * sizeof (struct field)));
9801
9802 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9803 TYPE_NFIELDS (type) * sizeof (struct field));
9804
9805 /* Install the discriminant at index 0 in the union. */
9806 TYPE_FIELD (union_type, 0) = *disr_field;
9807 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9808 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9809
9810 /* Install the union in the outer struct type. */
9811 TYPE_FIELD_TYPE (type, 0) = union_type;
9812 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9813 TYPE_NFIELDS (type) = 1;
9814
9815 /* Set the size and offset of the union type. */
9816 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9817
9818 /* We need a way to find the correct discriminant given a
9819 variant name. For convenience we build a map here. */
9820 struct type *enum_type = FIELD_TYPE (*disr_field);
9821 std::unordered_map<std::string, ULONGEST> discriminant_map;
9822 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9823 {
9824 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9825 {
9826 const char *name
9827 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9828 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9829 }
9830 }
9831
9832 int n_fields = TYPE_NFIELDS (union_type);
9833 struct discriminant_info *disc
9834 = alloc_discriminant_info (union_type, 0, -1);
9835 /* Skip the discriminant here. */
9836 for (int i = 1; i < n_fields; ++i)
9837 {
9838 /* Find the final word in the name of this variant's type.
9839 That name can be used to look up the correct
9840 discriminant. */
9841 const char *variant_name
9842 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9843 i)));
9844
9845 auto iter = discriminant_map.find (variant_name);
9846 if (iter != discriminant_map.end ())
9847 disc->discriminants[i] = iter->second;
9848
9849 /* Remove the discriminant field, if it exists. */
9850 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9851 if (TYPE_NFIELDS (sub_type) > 0)
9852 {
9853 --TYPE_NFIELDS (sub_type);
9854 ++TYPE_FIELDS (sub_type);
9855 }
9856 TYPE_FIELD_NAME (union_type, i) = variant_name;
9857 TYPE_NAME (sub_type)
9858 = rust_fully_qualify (&objfile->objfile_obstack,
9859 TYPE_NAME (type), variant_name);
9860 }
9861 }
9862 }
9863
9864 /* Rewrite some Rust unions to be structures with variants parts. */
9865
9866 static void
9867 rust_union_quirks (struct dwarf2_cu *cu)
9868 {
9869 gdb_assert (cu->language == language_rust);
9870 for (type *type_ : cu->rust_unions)
9871 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9872 /* We don't need this any more. */
9873 cu->rust_unions.clear ();
9874 }
9875
9876 /* Return the symtab for PER_CU. This works properly regardless of
9877 whether we're using the index or psymtabs. */
9878
9879 static struct compunit_symtab *
9880 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9881 {
9882 return (per_cu->dwarf2_per_objfile->using_index
9883 ? per_cu->v.quick->compunit_symtab
9884 : per_cu->v.psymtab->compunit_symtab);
9885 }
9886
9887 /* A helper function for computing the list of all symbol tables
9888 included by PER_CU. */
9889
9890 static void
9891 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9892 htab_t all_children, htab_t all_type_symtabs,
9893 struct dwarf2_per_cu_data *per_cu,
9894 struct compunit_symtab *immediate_parent)
9895 {
9896 void **slot;
9897 struct compunit_symtab *cust;
9898
9899 slot = htab_find_slot (all_children, per_cu, INSERT);
9900 if (*slot != NULL)
9901 {
9902 /* This inclusion and its children have been processed. */
9903 return;
9904 }
9905
9906 *slot = per_cu;
9907 /* Only add a CU if it has a symbol table. */
9908 cust = get_compunit_symtab (per_cu);
9909 if (cust != NULL)
9910 {
9911 /* If this is a type unit only add its symbol table if we haven't
9912 seen it yet (type unit per_cu's can share symtabs). */
9913 if (per_cu->is_debug_types)
9914 {
9915 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9916 if (*slot == NULL)
9917 {
9918 *slot = cust;
9919 result->push_back (cust);
9920 if (cust->user == NULL)
9921 cust->user = immediate_parent;
9922 }
9923 }
9924 else
9925 {
9926 result->push_back (cust);
9927 if (cust->user == NULL)
9928 cust->user = immediate_parent;
9929 }
9930 }
9931
9932 if (!per_cu->imported_symtabs_empty ())
9933 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9934 {
9935 recursively_compute_inclusions (result, all_children,
9936 all_type_symtabs, ptr, cust);
9937 }
9938 }
9939
9940 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9941 PER_CU. */
9942
9943 static void
9944 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9945 {
9946 gdb_assert (! per_cu->is_debug_types);
9947
9948 if (!per_cu->imported_symtabs_empty ())
9949 {
9950 int len;
9951 std::vector<compunit_symtab *> result_symtabs;
9952 htab_t all_children, all_type_symtabs;
9953 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9954
9955 /* If we don't have a symtab, we can just skip this case. */
9956 if (cust == NULL)
9957 return;
9958
9959 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9960 NULL, xcalloc, xfree);
9961 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9962 NULL, xcalloc, xfree);
9963
9964 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9965 {
9966 recursively_compute_inclusions (&result_symtabs, all_children,
9967 all_type_symtabs, ptr, cust);
9968 }
9969
9970 /* Now we have a transitive closure of all the included symtabs. */
9971 len = result_symtabs.size ();
9972 cust->includes
9973 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9974 struct compunit_symtab *, len + 1);
9975 memcpy (cust->includes, result_symtabs.data (),
9976 len * sizeof (compunit_symtab *));
9977 cust->includes[len] = NULL;
9978
9979 htab_delete (all_children);
9980 htab_delete (all_type_symtabs);
9981 }
9982 }
9983
9984 /* Compute the 'includes' field for the symtabs of all the CUs we just
9985 read. */
9986
9987 static void
9988 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9989 {
9990 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9991 {
9992 if (! iter->is_debug_types)
9993 compute_compunit_symtab_includes (iter);
9994 }
9995
9996 dwarf2_per_objfile->just_read_cus.clear ();
9997 }
9998
9999 /* Generate full symbol information for PER_CU, whose DIEs have
10000 already been loaded into memory. */
10001
10002 static void
10003 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10004 enum language pretend_language)
10005 {
10006 struct dwarf2_cu *cu = per_cu->cu;
10007 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10008 struct objfile *objfile = dwarf2_per_objfile->objfile;
10009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10010 CORE_ADDR lowpc, highpc;
10011 struct compunit_symtab *cust;
10012 CORE_ADDR baseaddr;
10013 struct block *static_block;
10014 CORE_ADDR addr;
10015
10016 baseaddr = objfile->text_section_offset ();
10017
10018 /* Clear the list here in case something was left over. */
10019 cu->method_list.clear ();
10020
10021 cu->language = pretend_language;
10022 cu->language_defn = language_def (cu->language);
10023
10024 /* Do line number decoding in read_file_scope () */
10025 process_die (cu->dies, cu);
10026
10027 /* For now fudge the Go package. */
10028 if (cu->language == language_go)
10029 fixup_go_packaging (cu);
10030
10031 /* Now that we have processed all the DIEs in the CU, all the types
10032 should be complete, and it should now be safe to compute all of the
10033 physnames. */
10034 compute_delayed_physnames (cu);
10035
10036 if (cu->language == language_rust)
10037 rust_union_quirks (cu);
10038
10039 /* Some compilers don't define a DW_AT_high_pc attribute for the
10040 compilation unit. If the DW_AT_high_pc is missing, synthesize
10041 it, by scanning the DIE's below the compilation unit. */
10042 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10043
10044 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10045 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10046
10047 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10048 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10049 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10050 addrmap to help ensure it has an accurate map of pc values belonging to
10051 this comp unit. */
10052 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10053
10054 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10055 SECT_OFF_TEXT (objfile),
10056 0);
10057
10058 if (cust != NULL)
10059 {
10060 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10061
10062 /* Set symtab language to language from DW_AT_language. If the
10063 compilation is from a C file generated by language preprocessors, do
10064 not set the language if it was already deduced by start_subfile. */
10065 if (!(cu->language == language_c
10066 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10067 COMPUNIT_FILETABS (cust)->language = cu->language;
10068
10069 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10070 produce DW_AT_location with location lists but it can be possibly
10071 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10072 there were bugs in prologue debug info, fixed later in GCC-4.5
10073 by "unwind info for epilogues" patch (which is not directly related).
10074
10075 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10076 needed, it would be wrong due to missing DW_AT_producer there.
10077
10078 Still one can confuse GDB by using non-standard GCC compilation
10079 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10080 */
10081 if (cu->has_loclist && gcc_4_minor >= 5)
10082 cust->locations_valid = 1;
10083
10084 if (gcc_4_minor >= 5)
10085 cust->epilogue_unwind_valid = 1;
10086
10087 cust->call_site_htab = cu->call_site_htab;
10088 }
10089
10090 if (dwarf2_per_objfile->using_index)
10091 per_cu->v.quick->compunit_symtab = cust;
10092 else
10093 {
10094 dwarf2_psymtab *pst = per_cu->v.psymtab;
10095 pst->compunit_symtab = cust;
10096 pst->readin = true;
10097 }
10098
10099 /* Push it for inclusion processing later. */
10100 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10101
10102 /* Not needed any more. */
10103 cu->reset_builder ();
10104 }
10105
10106 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10107 already been loaded into memory. */
10108
10109 static void
10110 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10111 enum language pretend_language)
10112 {
10113 struct dwarf2_cu *cu = per_cu->cu;
10114 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10115 struct objfile *objfile = dwarf2_per_objfile->objfile;
10116 struct compunit_symtab *cust;
10117 struct signatured_type *sig_type;
10118
10119 gdb_assert (per_cu->is_debug_types);
10120 sig_type = (struct signatured_type *) per_cu;
10121
10122 /* Clear the list here in case something was left over. */
10123 cu->method_list.clear ();
10124
10125 cu->language = pretend_language;
10126 cu->language_defn = language_def (cu->language);
10127
10128 /* The symbol tables are set up in read_type_unit_scope. */
10129 process_die (cu->dies, cu);
10130
10131 /* For now fudge the Go package. */
10132 if (cu->language == language_go)
10133 fixup_go_packaging (cu);
10134
10135 /* Now that we have processed all the DIEs in the CU, all the types
10136 should be complete, and it should now be safe to compute all of the
10137 physnames. */
10138 compute_delayed_physnames (cu);
10139
10140 if (cu->language == language_rust)
10141 rust_union_quirks (cu);
10142
10143 /* TUs share symbol tables.
10144 If this is the first TU to use this symtab, complete the construction
10145 of it with end_expandable_symtab. Otherwise, complete the addition of
10146 this TU's symbols to the existing symtab. */
10147 if (sig_type->type_unit_group->compunit_symtab == NULL)
10148 {
10149 buildsym_compunit *builder = cu->get_builder ();
10150 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10151 sig_type->type_unit_group->compunit_symtab = cust;
10152
10153 if (cust != NULL)
10154 {
10155 /* Set symtab language to language from DW_AT_language. If the
10156 compilation is from a C file generated by language preprocessors,
10157 do not set the language if it was already deduced by
10158 start_subfile. */
10159 if (!(cu->language == language_c
10160 && COMPUNIT_FILETABS (cust)->language != language_c))
10161 COMPUNIT_FILETABS (cust)->language = cu->language;
10162 }
10163 }
10164 else
10165 {
10166 cu->get_builder ()->augment_type_symtab ();
10167 cust = sig_type->type_unit_group->compunit_symtab;
10168 }
10169
10170 if (dwarf2_per_objfile->using_index)
10171 per_cu->v.quick->compunit_symtab = cust;
10172 else
10173 {
10174 dwarf2_psymtab *pst = per_cu->v.psymtab;
10175 pst->compunit_symtab = cust;
10176 pst->readin = true;
10177 }
10178
10179 /* Not needed any more. */
10180 cu->reset_builder ();
10181 }
10182
10183 /* Process an imported unit DIE. */
10184
10185 static void
10186 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10187 {
10188 struct attribute *attr;
10189
10190 /* For now we don't handle imported units in type units. */
10191 if (cu->per_cu->is_debug_types)
10192 {
10193 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10194 " supported in type units [in module %s]"),
10195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10196 }
10197
10198 attr = dwarf2_attr (die, DW_AT_import, cu);
10199 if (attr != NULL)
10200 {
10201 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10202 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10203 dwarf2_per_cu_data *per_cu
10204 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10205 cu->per_cu->dwarf2_per_objfile);
10206
10207 /* If necessary, add it to the queue and load its DIEs. */
10208 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10209 load_full_comp_unit (per_cu, false, cu->language);
10210
10211 cu->per_cu->imported_symtabs_push (per_cu);
10212 }
10213 }
10214
10215 /* RAII object that represents a process_die scope: i.e.,
10216 starts/finishes processing a DIE. */
10217 class process_die_scope
10218 {
10219 public:
10220 process_die_scope (die_info *die, dwarf2_cu *cu)
10221 : m_die (die), m_cu (cu)
10222 {
10223 /* We should only be processing DIEs not already in process. */
10224 gdb_assert (!m_die->in_process);
10225 m_die->in_process = true;
10226 }
10227
10228 ~process_die_scope ()
10229 {
10230 m_die->in_process = false;
10231
10232 /* If we're done processing the DIE for the CU that owns the line
10233 header, we don't need the line header anymore. */
10234 if (m_cu->line_header_die_owner == m_die)
10235 {
10236 delete m_cu->line_header;
10237 m_cu->line_header = NULL;
10238 m_cu->line_header_die_owner = NULL;
10239 }
10240 }
10241
10242 private:
10243 die_info *m_die;
10244 dwarf2_cu *m_cu;
10245 };
10246
10247 /* Process a die and its children. */
10248
10249 static void
10250 process_die (struct die_info *die, struct dwarf2_cu *cu)
10251 {
10252 process_die_scope scope (die, cu);
10253
10254 switch (die->tag)
10255 {
10256 case DW_TAG_padding:
10257 break;
10258 case DW_TAG_compile_unit:
10259 case DW_TAG_partial_unit:
10260 read_file_scope (die, cu);
10261 break;
10262 case DW_TAG_type_unit:
10263 read_type_unit_scope (die, cu);
10264 break;
10265 case DW_TAG_subprogram:
10266 /* Nested subprograms in Fortran get a prefix. */
10267 if (cu->language == language_fortran
10268 && die->parent != NULL
10269 && die->parent->tag == DW_TAG_subprogram)
10270 cu->processing_has_namespace_info = true;
10271 /* Fall through. */
10272 case DW_TAG_inlined_subroutine:
10273 read_func_scope (die, cu);
10274 break;
10275 case DW_TAG_lexical_block:
10276 case DW_TAG_try_block:
10277 case DW_TAG_catch_block:
10278 read_lexical_block_scope (die, cu);
10279 break;
10280 case DW_TAG_call_site:
10281 case DW_TAG_GNU_call_site:
10282 read_call_site_scope (die, cu);
10283 break;
10284 case DW_TAG_class_type:
10285 case DW_TAG_interface_type:
10286 case DW_TAG_structure_type:
10287 case DW_TAG_union_type:
10288 process_structure_scope (die, cu);
10289 break;
10290 case DW_TAG_enumeration_type:
10291 process_enumeration_scope (die, cu);
10292 break;
10293
10294 /* These dies have a type, but processing them does not create
10295 a symbol or recurse to process the children. Therefore we can
10296 read them on-demand through read_type_die. */
10297 case DW_TAG_subroutine_type:
10298 case DW_TAG_set_type:
10299 case DW_TAG_array_type:
10300 case DW_TAG_pointer_type:
10301 case DW_TAG_ptr_to_member_type:
10302 case DW_TAG_reference_type:
10303 case DW_TAG_rvalue_reference_type:
10304 case DW_TAG_string_type:
10305 break;
10306
10307 case DW_TAG_base_type:
10308 case DW_TAG_subrange_type:
10309 case DW_TAG_typedef:
10310 /* Add a typedef symbol for the type definition, if it has a
10311 DW_AT_name. */
10312 new_symbol (die, read_type_die (die, cu), cu);
10313 break;
10314 case DW_TAG_common_block:
10315 read_common_block (die, cu);
10316 break;
10317 case DW_TAG_common_inclusion:
10318 break;
10319 case DW_TAG_namespace:
10320 cu->processing_has_namespace_info = true;
10321 read_namespace (die, cu);
10322 break;
10323 case DW_TAG_module:
10324 cu->processing_has_namespace_info = true;
10325 read_module (die, cu);
10326 break;
10327 case DW_TAG_imported_declaration:
10328 cu->processing_has_namespace_info = true;
10329 if (read_namespace_alias (die, cu))
10330 break;
10331 /* The declaration is not a global namespace alias. */
10332 /* Fall through. */
10333 case DW_TAG_imported_module:
10334 cu->processing_has_namespace_info = true;
10335 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10336 || cu->language != language_fortran))
10337 complaint (_("Tag '%s' has unexpected children"),
10338 dwarf_tag_name (die->tag));
10339 read_import_statement (die, cu);
10340 break;
10341
10342 case DW_TAG_imported_unit:
10343 process_imported_unit_die (die, cu);
10344 break;
10345
10346 case DW_TAG_variable:
10347 read_variable (die, cu);
10348 break;
10349
10350 default:
10351 new_symbol (die, NULL, cu);
10352 break;
10353 }
10354 }
10355 \f
10356 /* DWARF name computation. */
10357
10358 /* A helper function for dwarf2_compute_name which determines whether DIE
10359 needs to have the name of the scope prepended to the name listed in the
10360 die. */
10361
10362 static int
10363 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10364 {
10365 struct attribute *attr;
10366
10367 switch (die->tag)
10368 {
10369 case DW_TAG_namespace:
10370 case DW_TAG_typedef:
10371 case DW_TAG_class_type:
10372 case DW_TAG_interface_type:
10373 case DW_TAG_structure_type:
10374 case DW_TAG_union_type:
10375 case DW_TAG_enumeration_type:
10376 case DW_TAG_enumerator:
10377 case DW_TAG_subprogram:
10378 case DW_TAG_inlined_subroutine:
10379 case DW_TAG_member:
10380 case DW_TAG_imported_declaration:
10381 return 1;
10382
10383 case DW_TAG_variable:
10384 case DW_TAG_constant:
10385 /* We only need to prefix "globally" visible variables. These include
10386 any variable marked with DW_AT_external or any variable that
10387 lives in a namespace. [Variables in anonymous namespaces
10388 require prefixing, but they are not DW_AT_external.] */
10389
10390 if (dwarf2_attr (die, DW_AT_specification, cu))
10391 {
10392 struct dwarf2_cu *spec_cu = cu;
10393
10394 return die_needs_namespace (die_specification (die, &spec_cu),
10395 spec_cu);
10396 }
10397
10398 attr = dwarf2_attr (die, DW_AT_external, cu);
10399 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10400 && die->parent->tag != DW_TAG_module)
10401 return 0;
10402 /* A variable in a lexical block of some kind does not need a
10403 namespace, even though in C++ such variables may be external
10404 and have a mangled name. */
10405 if (die->parent->tag == DW_TAG_lexical_block
10406 || die->parent->tag == DW_TAG_try_block
10407 || die->parent->tag == DW_TAG_catch_block
10408 || die->parent->tag == DW_TAG_subprogram)
10409 return 0;
10410 return 1;
10411
10412 default:
10413 return 0;
10414 }
10415 }
10416
10417 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10418 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10419 defined for the given DIE. */
10420
10421 static struct attribute *
10422 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10423 {
10424 struct attribute *attr;
10425
10426 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10427 if (attr == NULL)
10428 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10429
10430 return attr;
10431 }
10432
10433 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10434 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10435 defined for the given DIE. */
10436
10437 static const char *
10438 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10439 {
10440 const char *linkage_name;
10441
10442 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10443 if (linkage_name == NULL)
10444 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10445
10446 return linkage_name;
10447 }
10448
10449 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10450 compute the physname for the object, which include a method's:
10451 - formal parameters (C++),
10452 - receiver type (Go),
10453
10454 The term "physname" is a bit confusing.
10455 For C++, for example, it is the demangled name.
10456 For Go, for example, it's the mangled name.
10457
10458 For Ada, return the DIE's linkage name rather than the fully qualified
10459 name. PHYSNAME is ignored..
10460
10461 The result is allocated on the objfile_obstack and canonicalized. */
10462
10463 static const char *
10464 dwarf2_compute_name (const char *name,
10465 struct die_info *die, struct dwarf2_cu *cu,
10466 int physname)
10467 {
10468 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10469
10470 if (name == NULL)
10471 name = dwarf2_name (die, cu);
10472
10473 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10474 but otherwise compute it by typename_concat inside GDB.
10475 FIXME: Actually this is not really true, or at least not always true.
10476 It's all very confusing. compute_and_set_names doesn't try to demangle
10477 Fortran names because there is no mangling standard. So new_symbol
10478 will set the demangled name to the result of dwarf2_full_name, and it is
10479 the demangled name that GDB uses if it exists. */
10480 if (cu->language == language_ada
10481 || (cu->language == language_fortran && physname))
10482 {
10483 /* For Ada unit, we prefer the linkage name over the name, as
10484 the former contains the exported name, which the user expects
10485 to be able to reference. Ideally, we want the user to be able
10486 to reference this entity using either natural or linkage name,
10487 but we haven't started looking at this enhancement yet. */
10488 const char *linkage_name = dw2_linkage_name (die, cu);
10489
10490 if (linkage_name != NULL)
10491 return linkage_name;
10492 }
10493
10494 /* These are the only languages we know how to qualify names in. */
10495 if (name != NULL
10496 && (cu->language == language_cplus
10497 || cu->language == language_fortran || cu->language == language_d
10498 || cu->language == language_rust))
10499 {
10500 if (die_needs_namespace (die, cu))
10501 {
10502 const char *prefix;
10503 const char *canonical_name = NULL;
10504
10505 string_file buf;
10506
10507 prefix = determine_prefix (die, cu);
10508 if (*prefix != '\0')
10509 {
10510 gdb::unique_xmalloc_ptr<char> prefixed_name
10511 (typename_concat (NULL, prefix, name, physname, cu));
10512
10513 buf.puts (prefixed_name.get ());
10514 }
10515 else
10516 buf.puts (name);
10517
10518 /* Template parameters may be specified in the DIE's DW_AT_name, or
10519 as children with DW_TAG_template_type_param or
10520 DW_TAG_value_type_param. If the latter, add them to the name
10521 here. If the name already has template parameters, then
10522 skip this step; some versions of GCC emit both, and
10523 it is more efficient to use the pre-computed name.
10524
10525 Something to keep in mind about this process: it is very
10526 unlikely, or in some cases downright impossible, to produce
10527 something that will match the mangled name of a function.
10528 If the definition of the function has the same debug info,
10529 we should be able to match up with it anyway. But fallbacks
10530 using the minimal symbol, for instance to find a method
10531 implemented in a stripped copy of libstdc++, will not work.
10532 If we do not have debug info for the definition, we will have to
10533 match them up some other way.
10534
10535 When we do name matching there is a related problem with function
10536 templates; two instantiated function templates are allowed to
10537 differ only by their return types, which we do not add here. */
10538
10539 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10540 {
10541 struct attribute *attr;
10542 struct die_info *child;
10543 int first = 1;
10544
10545 die->building_fullname = 1;
10546
10547 for (child = die->child; child != NULL; child = child->sibling)
10548 {
10549 struct type *type;
10550 LONGEST value;
10551 const gdb_byte *bytes;
10552 struct dwarf2_locexpr_baton *baton;
10553 struct value *v;
10554
10555 if (child->tag != DW_TAG_template_type_param
10556 && child->tag != DW_TAG_template_value_param)
10557 continue;
10558
10559 if (first)
10560 {
10561 buf.puts ("<");
10562 first = 0;
10563 }
10564 else
10565 buf.puts (", ");
10566
10567 attr = dwarf2_attr (child, DW_AT_type, cu);
10568 if (attr == NULL)
10569 {
10570 complaint (_("template parameter missing DW_AT_type"));
10571 buf.puts ("UNKNOWN_TYPE");
10572 continue;
10573 }
10574 type = die_type (child, cu);
10575
10576 if (child->tag == DW_TAG_template_type_param)
10577 {
10578 c_print_type (type, "", &buf, -1, 0, cu->language,
10579 &type_print_raw_options);
10580 continue;
10581 }
10582
10583 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10584 if (attr == NULL)
10585 {
10586 complaint (_("template parameter missing "
10587 "DW_AT_const_value"));
10588 buf.puts ("UNKNOWN_VALUE");
10589 continue;
10590 }
10591
10592 dwarf2_const_value_attr (attr, type, name,
10593 &cu->comp_unit_obstack, cu,
10594 &value, &bytes, &baton);
10595
10596 if (TYPE_NOSIGN (type))
10597 /* GDB prints characters as NUMBER 'CHAR'. If that's
10598 changed, this can use value_print instead. */
10599 c_printchar (value, type, &buf);
10600 else
10601 {
10602 struct value_print_options opts;
10603
10604 if (baton != NULL)
10605 v = dwarf2_evaluate_loc_desc (type, NULL,
10606 baton->data,
10607 baton->size,
10608 baton->per_cu);
10609 else if (bytes != NULL)
10610 {
10611 v = allocate_value (type);
10612 memcpy (value_contents_writeable (v), bytes,
10613 TYPE_LENGTH (type));
10614 }
10615 else
10616 v = value_from_longest (type, value);
10617
10618 /* Specify decimal so that we do not depend on
10619 the radix. */
10620 get_formatted_print_options (&opts, 'd');
10621 opts.raw = 1;
10622 value_print (v, &buf, &opts);
10623 release_value (v);
10624 }
10625 }
10626
10627 die->building_fullname = 0;
10628
10629 if (!first)
10630 {
10631 /* Close the argument list, with a space if necessary
10632 (nested templates). */
10633 if (!buf.empty () && buf.string ().back () == '>')
10634 buf.puts (" >");
10635 else
10636 buf.puts (">");
10637 }
10638 }
10639
10640 /* For C++ methods, append formal parameter type
10641 information, if PHYSNAME. */
10642
10643 if (physname && die->tag == DW_TAG_subprogram
10644 && cu->language == language_cplus)
10645 {
10646 struct type *type = read_type_die (die, cu);
10647
10648 c_type_print_args (type, &buf, 1, cu->language,
10649 &type_print_raw_options);
10650
10651 if (cu->language == language_cplus)
10652 {
10653 /* Assume that an artificial first parameter is
10654 "this", but do not crash if it is not. RealView
10655 marks unnamed (and thus unused) parameters as
10656 artificial; there is no way to differentiate
10657 the two cases. */
10658 if (TYPE_NFIELDS (type) > 0
10659 && TYPE_FIELD_ARTIFICIAL (type, 0)
10660 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10661 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10662 0))))
10663 buf.puts (" const");
10664 }
10665 }
10666
10667 const std::string &intermediate_name = buf.string ();
10668
10669 if (cu->language == language_cplus)
10670 canonical_name
10671 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10672 &objfile->per_bfd->storage_obstack);
10673
10674 /* If we only computed INTERMEDIATE_NAME, or if
10675 INTERMEDIATE_NAME is already canonical, then we need to
10676 copy it to the appropriate obstack. */
10677 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10678 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10679 intermediate_name);
10680 else
10681 name = canonical_name;
10682 }
10683 }
10684
10685 return name;
10686 }
10687
10688 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10689 If scope qualifiers are appropriate they will be added. The result
10690 will be allocated on the storage_obstack, or NULL if the DIE does
10691 not have a name. NAME may either be from a previous call to
10692 dwarf2_name or NULL.
10693
10694 The output string will be canonicalized (if C++). */
10695
10696 static const char *
10697 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10698 {
10699 return dwarf2_compute_name (name, die, cu, 0);
10700 }
10701
10702 /* Construct a physname for the given DIE in CU. NAME may either be
10703 from a previous call to dwarf2_name or NULL. The result will be
10704 allocated on the objfile_objstack or NULL if the DIE does not have a
10705 name.
10706
10707 The output string will be canonicalized (if C++). */
10708
10709 static const char *
10710 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10711 {
10712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10713 const char *retval, *mangled = NULL, *canon = NULL;
10714 int need_copy = 1;
10715
10716 /* In this case dwarf2_compute_name is just a shortcut not building anything
10717 on its own. */
10718 if (!die_needs_namespace (die, cu))
10719 return dwarf2_compute_name (name, die, cu, 1);
10720
10721 mangled = dw2_linkage_name (die, cu);
10722
10723 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10724 See https://github.com/rust-lang/rust/issues/32925. */
10725 if (cu->language == language_rust && mangled != NULL
10726 && strchr (mangled, '{') != NULL)
10727 mangled = NULL;
10728
10729 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10730 has computed. */
10731 gdb::unique_xmalloc_ptr<char> demangled;
10732 if (mangled != NULL)
10733 {
10734
10735 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10736 {
10737 /* Do nothing (do not demangle the symbol name). */
10738 }
10739 else if (cu->language == language_go)
10740 {
10741 /* This is a lie, but we already lie to the caller new_symbol.
10742 new_symbol assumes we return the mangled name.
10743 This just undoes that lie until things are cleaned up. */
10744 }
10745 else
10746 {
10747 /* Use DMGL_RET_DROP for C++ template functions to suppress
10748 their return type. It is easier for GDB users to search
10749 for such functions as `name(params)' than `long name(params)'.
10750 In such case the minimal symbol names do not match the full
10751 symbol names but for template functions there is never a need
10752 to look up their definition from their declaration so
10753 the only disadvantage remains the minimal symbol variant
10754 `long name(params)' does not have the proper inferior type. */
10755 demangled.reset (gdb_demangle (mangled,
10756 (DMGL_PARAMS | DMGL_ANSI
10757 | DMGL_RET_DROP)));
10758 }
10759 if (demangled)
10760 canon = demangled.get ();
10761 else
10762 {
10763 canon = mangled;
10764 need_copy = 0;
10765 }
10766 }
10767
10768 if (canon == NULL || check_physname)
10769 {
10770 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10771
10772 if (canon != NULL && strcmp (physname, canon) != 0)
10773 {
10774 /* It may not mean a bug in GDB. The compiler could also
10775 compute DW_AT_linkage_name incorrectly. But in such case
10776 GDB would need to be bug-to-bug compatible. */
10777
10778 complaint (_("Computed physname <%s> does not match demangled <%s> "
10779 "(from linkage <%s>) - DIE at %s [in module %s]"),
10780 physname, canon, mangled, sect_offset_str (die->sect_off),
10781 objfile_name (objfile));
10782
10783 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10784 is available here - over computed PHYSNAME. It is safer
10785 against both buggy GDB and buggy compilers. */
10786
10787 retval = canon;
10788 }
10789 else
10790 {
10791 retval = physname;
10792 need_copy = 0;
10793 }
10794 }
10795 else
10796 retval = canon;
10797
10798 if (need_copy)
10799 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10800
10801 return retval;
10802 }
10803
10804 /* Inspect DIE in CU for a namespace alias. If one exists, record
10805 a new symbol for it.
10806
10807 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10808
10809 static int
10810 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10811 {
10812 struct attribute *attr;
10813
10814 /* If the die does not have a name, this is not a namespace
10815 alias. */
10816 attr = dwarf2_attr (die, DW_AT_name, cu);
10817 if (attr != NULL)
10818 {
10819 int num;
10820 struct die_info *d = die;
10821 struct dwarf2_cu *imported_cu = cu;
10822
10823 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10824 keep inspecting DIEs until we hit the underlying import. */
10825 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10826 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10827 {
10828 attr = dwarf2_attr (d, DW_AT_import, cu);
10829 if (attr == NULL)
10830 break;
10831
10832 d = follow_die_ref (d, attr, &imported_cu);
10833 if (d->tag != DW_TAG_imported_declaration)
10834 break;
10835 }
10836
10837 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10838 {
10839 complaint (_("DIE at %s has too many recursively imported "
10840 "declarations"), sect_offset_str (d->sect_off));
10841 return 0;
10842 }
10843
10844 if (attr != NULL)
10845 {
10846 struct type *type;
10847 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10848
10849 type = get_die_type_at_offset (sect_off, cu->per_cu);
10850 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10851 {
10852 /* This declaration is a global namespace alias. Add
10853 a symbol for it whose type is the aliased namespace. */
10854 new_symbol (die, type, cu);
10855 return 1;
10856 }
10857 }
10858 }
10859
10860 return 0;
10861 }
10862
10863 /* Return the using directives repository (global or local?) to use in the
10864 current context for CU.
10865
10866 For Ada, imported declarations can materialize renamings, which *may* be
10867 global. However it is impossible (for now?) in DWARF to distinguish
10868 "external" imported declarations and "static" ones. As all imported
10869 declarations seem to be static in all other languages, make them all CU-wide
10870 global only in Ada. */
10871
10872 static struct using_direct **
10873 using_directives (struct dwarf2_cu *cu)
10874 {
10875 if (cu->language == language_ada
10876 && cu->get_builder ()->outermost_context_p ())
10877 return cu->get_builder ()->get_global_using_directives ();
10878 else
10879 return cu->get_builder ()->get_local_using_directives ();
10880 }
10881
10882 /* Read the import statement specified by the given die and record it. */
10883
10884 static void
10885 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10886 {
10887 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10888 struct attribute *import_attr;
10889 struct die_info *imported_die, *child_die;
10890 struct dwarf2_cu *imported_cu;
10891 const char *imported_name;
10892 const char *imported_name_prefix;
10893 const char *canonical_name;
10894 const char *import_alias;
10895 const char *imported_declaration = NULL;
10896 const char *import_prefix;
10897 std::vector<const char *> excludes;
10898
10899 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10900 if (import_attr == NULL)
10901 {
10902 complaint (_("Tag '%s' has no DW_AT_import"),
10903 dwarf_tag_name (die->tag));
10904 return;
10905 }
10906
10907 imported_cu = cu;
10908 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10909 imported_name = dwarf2_name (imported_die, imported_cu);
10910 if (imported_name == NULL)
10911 {
10912 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10913
10914 The import in the following code:
10915 namespace A
10916 {
10917 typedef int B;
10918 }
10919
10920 int main ()
10921 {
10922 using A::B;
10923 B b;
10924 return b;
10925 }
10926
10927 ...
10928 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10929 <52> DW_AT_decl_file : 1
10930 <53> DW_AT_decl_line : 6
10931 <54> DW_AT_import : <0x75>
10932 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10933 <59> DW_AT_name : B
10934 <5b> DW_AT_decl_file : 1
10935 <5c> DW_AT_decl_line : 2
10936 <5d> DW_AT_type : <0x6e>
10937 ...
10938 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10939 <76> DW_AT_byte_size : 4
10940 <77> DW_AT_encoding : 5 (signed)
10941
10942 imports the wrong die ( 0x75 instead of 0x58 ).
10943 This case will be ignored until the gcc bug is fixed. */
10944 return;
10945 }
10946
10947 /* Figure out the local name after import. */
10948 import_alias = dwarf2_name (die, cu);
10949
10950 /* Figure out where the statement is being imported to. */
10951 import_prefix = determine_prefix (die, cu);
10952
10953 /* Figure out what the scope of the imported die is and prepend it
10954 to the name of the imported die. */
10955 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10956
10957 if (imported_die->tag != DW_TAG_namespace
10958 && imported_die->tag != DW_TAG_module)
10959 {
10960 imported_declaration = imported_name;
10961 canonical_name = imported_name_prefix;
10962 }
10963 else if (strlen (imported_name_prefix) > 0)
10964 canonical_name = obconcat (&objfile->objfile_obstack,
10965 imported_name_prefix,
10966 (cu->language == language_d ? "." : "::"),
10967 imported_name, (char *) NULL);
10968 else
10969 canonical_name = imported_name;
10970
10971 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10972 for (child_die = die->child; child_die && child_die->tag;
10973 child_die = sibling_die (child_die))
10974 {
10975 /* DWARF-4: A Fortran use statement with a “rename list” may be
10976 represented by an imported module entry with an import attribute
10977 referring to the module and owned entries corresponding to those
10978 entities that are renamed as part of being imported. */
10979
10980 if (child_die->tag != DW_TAG_imported_declaration)
10981 {
10982 complaint (_("child DW_TAG_imported_declaration expected "
10983 "- DIE at %s [in module %s]"),
10984 sect_offset_str (child_die->sect_off),
10985 objfile_name (objfile));
10986 continue;
10987 }
10988
10989 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10990 if (import_attr == NULL)
10991 {
10992 complaint (_("Tag '%s' has no DW_AT_import"),
10993 dwarf_tag_name (child_die->tag));
10994 continue;
10995 }
10996
10997 imported_cu = cu;
10998 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10999 &imported_cu);
11000 imported_name = dwarf2_name (imported_die, imported_cu);
11001 if (imported_name == NULL)
11002 {
11003 complaint (_("child DW_TAG_imported_declaration has unknown "
11004 "imported name - DIE at %s [in module %s]"),
11005 sect_offset_str (child_die->sect_off),
11006 objfile_name (objfile));
11007 continue;
11008 }
11009
11010 excludes.push_back (imported_name);
11011
11012 process_die (child_die, cu);
11013 }
11014
11015 add_using_directive (using_directives (cu),
11016 import_prefix,
11017 canonical_name,
11018 import_alias,
11019 imported_declaration,
11020 excludes,
11021 0,
11022 &objfile->objfile_obstack);
11023 }
11024
11025 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11026 types, but gives them a size of zero. Starting with version 14,
11027 ICC is compatible with GCC. */
11028
11029 static bool
11030 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11031 {
11032 if (!cu->checked_producer)
11033 check_producer (cu);
11034
11035 return cu->producer_is_icc_lt_14;
11036 }
11037
11038 /* ICC generates a DW_AT_type for C void functions. This was observed on
11039 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11040 which says that void functions should not have a DW_AT_type. */
11041
11042 static bool
11043 producer_is_icc (struct dwarf2_cu *cu)
11044 {
11045 if (!cu->checked_producer)
11046 check_producer (cu);
11047
11048 return cu->producer_is_icc;
11049 }
11050
11051 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11052 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11053 this, it was first present in GCC release 4.3.0. */
11054
11055 static bool
11056 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11057 {
11058 if (!cu->checked_producer)
11059 check_producer (cu);
11060
11061 return cu->producer_is_gcc_lt_4_3;
11062 }
11063
11064 static file_and_directory
11065 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11066 {
11067 file_and_directory res;
11068
11069 /* Find the filename. Do not use dwarf2_name here, since the filename
11070 is not a source language identifier. */
11071 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11072 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11073
11074 if (res.comp_dir == NULL
11075 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11076 && IS_ABSOLUTE_PATH (res.name))
11077 {
11078 res.comp_dir_storage = ldirname (res.name);
11079 if (!res.comp_dir_storage.empty ())
11080 res.comp_dir = res.comp_dir_storage.c_str ();
11081 }
11082 if (res.comp_dir != NULL)
11083 {
11084 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11085 directory, get rid of it. */
11086 const char *cp = strchr (res.comp_dir, ':');
11087
11088 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11089 res.comp_dir = cp + 1;
11090 }
11091
11092 if (res.name == NULL)
11093 res.name = "<unknown>";
11094
11095 return res;
11096 }
11097
11098 /* Handle DW_AT_stmt_list for a compilation unit.
11099 DIE is the DW_TAG_compile_unit die for CU.
11100 COMP_DIR is the compilation directory. LOWPC is passed to
11101 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11102
11103 static void
11104 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11105 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11106 {
11107 struct dwarf2_per_objfile *dwarf2_per_objfile
11108 = cu->per_cu->dwarf2_per_objfile;
11109 struct objfile *objfile = dwarf2_per_objfile->objfile;
11110 struct attribute *attr;
11111 struct line_header line_header_local;
11112 hashval_t line_header_local_hash;
11113 void **slot;
11114 int decode_mapping;
11115
11116 gdb_assert (! cu->per_cu->is_debug_types);
11117
11118 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11119 if (attr == NULL)
11120 return;
11121
11122 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11123
11124 /* The line header hash table is only created if needed (it exists to
11125 prevent redundant reading of the line table for partial_units).
11126 If we're given a partial_unit, we'll need it. If we're given a
11127 compile_unit, then use the line header hash table if it's already
11128 created, but don't create one just yet. */
11129
11130 if (dwarf2_per_objfile->line_header_hash == NULL
11131 && die->tag == DW_TAG_partial_unit)
11132 {
11133 dwarf2_per_objfile->line_header_hash
11134 = htab_create_alloc_ex (127, line_header_hash_voidp,
11135 line_header_eq_voidp,
11136 free_line_header_voidp,
11137 &objfile->objfile_obstack,
11138 hashtab_obstack_allocate,
11139 dummy_obstack_deallocate);
11140 }
11141
11142 line_header_local.sect_off = line_offset;
11143 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11144 line_header_local_hash = line_header_hash (&line_header_local);
11145 if (dwarf2_per_objfile->line_header_hash != NULL)
11146 {
11147 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11148 &line_header_local,
11149 line_header_local_hash, NO_INSERT);
11150
11151 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11152 is not present in *SLOT (since if there is something in *SLOT then
11153 it will be for a partial_unit). */
11154 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11155 {
11156 gdb_assert (*slot != NULL);
11157 cu->line_header = (struct line_header *) *slot;
11158 return;
11159 }
11160 }
11161
11162 /* dwarf_decode_line_header does not yet provide sufficient information.
11163 We always have to call also dwarf_decode_lines for it. */
11164 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11165 if (lh == NULL)
11166 return;
11167
11168 cu->line_header = lh.release ();
11169 cu->line_header_die_owner = die;
11170
11171 if (dwarf2_per_objfile->line_header_hash == NULL)
11172 slot = NULL;
11173 else
11174 {
11175 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11176 &line_header_local,
11177 line_header_local_hash, INSERT);
11178 gdb_assert (slot != NULL);
11179 }
11180 if (slot != NULL && *slot == NULL)
11181 {
11182 /* This newly decoded line number information unit will be owned
11183 by line_header_hash hash table. */
11184 *slot = cu->line_header;
11185 cu->line_header_die_owner = NULL;
11186 }
11187 else
11188 {
11189 /* We cannot free any current entry in (*slot) as that struct line_header
11190 may be already used by multiple CUs. Create only temporary decoded
11191 line_header for this CU - it may happen at most once for each line
11192 number information unit. And if we're not using line_header_hash
11193 then this is what we want as well. */
11194 gdb_assert (die->tag != DW_TAG_partial_unit);
11195 }
11196 decode_mapping = (die->tag != DW_TAG_partial_unit);
11197 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11198 decode_mapping);
11199
11200 }
11201
11202 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11203
11204 static void
11205 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11206 {
11207 struct dwarf2_per_objfile *dwarf2_per_objfile
11208 = cu->per_cu->dwarf2_per_objfile;
11209 struct objfile *objfile = dwarf2_per_objfile->objfile;
11210 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11211 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11212 CORE_ADDR highpc = ((CORE_ADDR) 0);
11213 struct attribute *attr;
11214 struct die_info *child_die;
11215 CORE_ADDR baseaddr;
11216
11217 prepare_one_comp_unit (cu, die, cu->language);
11218 baseaddr = objfile->text_section_offset ();
11219
11220 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11221
11222 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11223 from finish_block. */
11224 if (lowpc == ((CORE_ADDR) -1))
11225 lowpc = highpc;
11226 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11227
11228 file_and_directory fnd = find_file_and_directory (die, cu);
11229
11230 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11231 standardised yet. As a workaround for the language detection we fall
11232 back to the DW_AT_producer string. */
11233 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11234 cu->language = language_opencl;
11235
11236 /* Similar hack for Go. */
11237 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11238 set_cu_language (DW_LANG_Go, cu);
11239
11240 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11241
11242 /* Decode line number information if present. We do this before
11243 processing child DIEs, so that the line header table is available
11244 for DW_AT_decl_file. */
11245 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11246
11247 /* Process all dies in compilation unit. */
11248 if (die->child != NULL)
11249 {
11250 child_die = die->child;
11251 while (child_die && child_die->tag)
11252 {
11253 process_die (child_die, cu);
11254 child_die = sibling_die (child_die);
11255 }
11256 }
11257
11258 /* Decode macro information, if present. Dwarf 2 macro information
11259 refers to information in the line number info statement program
11260 header, so we can only read it if we've read the header
11261 successfully. */
11262 attr = dwarf2_attr (die, DW_AT_macros, cu);
11263 if (attr == NULL)
11264 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11265 if (attr && cu->line_header)
11266 {
11267 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11268 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11269
11270 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11271 }
11272 else
11273 {
11274 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11275 if (attr && cu->line_header)
11276 {
11277 unsigned int macro_offset = DW_UNSND (attr);
11278
11279 dwarf_decode_macros (cu, macro_offset, 0);
11280 }
11281 }
11282 }
11283
11284 void
11285 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11286 {
11287 struct type_unit_group *tu_group;
11288 int first_time;
11289 struct attribute *attr;
11290 unsigned int i;
11291 struct signatured_type *sig_type;
11292
11293 gdb_assert (per_cu->is_debug_types);
11294 sig_type = (struct signatured_type *) per_cu;
11295
11296 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11297
11298 /* If we're using .gdb_index (includes -readnow) then
11299 per_cu->type_unit_group may not have been set up yet. */
11300 if (sig_type->type_unit_group == NULL)
11301 sig_type->type_unit_group = get_type_unit_group (this, attr);
11302 tu_group = sig_type->type_unit_group;
11303
11304 /* If we've already processed this stmt_list there's no real need to
11305 do it again, we could fake it and just recreate the part we need
11306 (file name,index -> symtab mapping). If data shows this optimization
11307 is useful we can do it then. */
11308 first_time = tu_group->compunit_symtab == NULL;
11309
11310 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11311 debug info. */
11312 line_header_up lh;
11313 if (attr != NULL)
11314 {
11315 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11316 lh = dwarf_decode_line_header (line_offset, this);
11317 }
11318 if (lh == NULL)
11319 {
11320 if (first_time)
11321 start_symtab ("", NULL, 0);
11322 else
11323 {
11324 gdb_assert (tu_group->symtabs == NULL);
11325 gdb_assert (m_builder == nullptr);
11326 struct compunit_symtab *cust = tu_group->compunit_symtab;
11327 m_builder.reset (new struct buildsym_compunit
11328 (COMPUNIT_OBJFILE (cust), "",
11329 COMPUNIT_DIRNAME (cust),
11330 compunit_language (cust),
11331 0, cust));
11332 }
11333 return;
11334 }
11335
11336 line_header = lh.release ();
11337 line_header_die_owner = die;
11338
11339 if (first_time)
11340 {
11341 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11342
11343 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11344 still initializing it, and our caller (a few levels up)
11345 process_full_type_unit still needs to know if this is the first
11346 time. */
11347
11348 tu_group->num_symtabs = line_header->file_names_size ();
11349 tu_group->symtabs = XNEWVEC (struct symtab *,
11350 line_header->file_names_size ());
11351
11352 auto &file_names = line_header->file_names ();
11353 for (i = 0; i < file_names.size (); ++i)
11354 {
11355 file_entry &fe = file_names[i];
11356 dwarf2_start_subfile (this, fe.name,
11357 fe.include_dir (line_header));
11358 buildsym_compunit *b = get_builder ();
11359 if (b->get_current_subfile ()->symtab == NULL)
11360 {
11361 /* NOTE: start_subfile will recognize when it's been
11362 passed a file it has already seen. So we can't
11363 assume there's a simple mapping from
11364 cu->line_header->file_names to subfiles, plus
11365 cu->line_header->file_names may contain dups. */
11366 b->get_current_subfile ()->symtab
11367 = allocate_symtab (cust, b->get_current_subfile ()->name);
11368 }
11369
11370 fe.symtab = b->get_current_subfile ()->symtab;
11371 tu_group->symtabs[i] = fe.symtab;
11372 }
11373 }
11374 else
11375 {
11376 gdb_assert (m_builder == nullptr);
11377 struct compunit_symtab *cust = tu_group->compunit_symtab;
11378 m_builder.reset (new struct buildsym_compunit
11379 (COMPUNIT_OBJFILE (cust), "",
11380 COMPUNIT_DIRNAME (cust),
11381 compunit_language (cust),
11382 0, cust));
11383
11384 auto &file_names = line_header->file_names ();
11385 for (i = 0; i < file_names.size (); ++i)
11386 {
11387 file_entry &fe = file_names[i];
11388 fe.symtab = tu_group->symtabs[i];
11389 }
11390 }
11391
11392 /* The main symtab is allocated last. Type units don't have DW_AT_name
11393 so they don't have a "real" (so to speak) symtab anyway.
11394 There is later code that will assign the main symtab to all symbols
11395 that don't have one. We need to handle the case of a symbol with a
11396 missing symtab (DW_AT_decl_file) anyway. */
11397 }
11398
11399 /* Process DW_TAG_type_unit.
11400 For TUs we want to skip the first top level sibling if it's not the
11401 actual type being defined by this TU. In this case the first top
11402 level sibling is there to provide context only. */
11403
11404 static void
11405 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11406 {
11407 struct die_info *child_die;
11408
11409 prepare_one_comp_unit (cu, die, language_minimal);
11410
11411 /* Initialize (or reinitialize) the machinery for building symtabs.
11412 We do this before processing child DIEs, so that the line header table
11413 is available for DW_AT_decl_file. */
11414 cu->setup_type_unit_groups (die);
11415
11416 if (die->child != NULL)
11417 {
11418 child_die = die->child;
11419 while (child_die && child_die->tag)
11420 {
11421 process_die (child_die, cu);
11422 child_die = sibling_die (child_die);
11423 }
11424 }
11425 }
11426 \f
11427 /* DWO/DWP files.
11428
11429 http://gcc.gnu.org/wiki/DebugFission
11430 http://gcc.gnu.org/wiki/DebugFissionDWP
11431
11432 To simplify handling of both DWO files ("object" files with the DWARF info)
11433 and DWP files (a file with the DWOs packaged up into one file), we treat
11434 DWP files as having a collection of virtual DWO files. */
11435
11436 static hashval_t
11437 hash_dwo_file (const void *item)
11438 {
11439 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11440 hashval_t hash;
11441
11442 hash = htab_hash_string (dwo_file->dwo_name);
11443 if (dwo_file->comp_dir != NULL)
11444 hash += htab_hash_string (dwo_file->comp_dir);
11445 return hash;
11446 }
11447
11448 static int
11449 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11450 {
11451 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11452 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11453
11454 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11455 return 0;
11456 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11457 return lhs->comp_dir == rhs->comp_dir;
11458 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11459 }
11460
11461 /* Allocate a hash table for DWO files. */
11462
11463 static htab_up
11464 allocate_dwo_file_hash_table (struct objfile *objfile)
11465 {
11466 auto delete_dwo_file = [] (void *item)
11467 {
11468 struct dwo_file *dwo_file = (struct dwo_file *) item;
11469
11470 delete dwo_file;
11471 };
11472
11473 return htab_up (htab_create_alloc_ex (41,
11474 hash_dwo_file,
11475 eq_dwo_file,
11476 delete_dwo_file,
11477 &objfile->objfile_obstack,
11478 hashtab_obstack_allocate,
11479 dummy_obstack_deallocate));
11480 }
11481
11482 /* Lookup DWO file DWO_NAME. */
11483
11484 static void **
11485 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11486 const char *dwo_name,
11487 const char *comp_dir)
11488 {
11489 struct dwo_file find_entry;
11490 void **slot;
11491
11492 if (dwarf2_per_objfile->dwo_files == NULL)
11493 dwarf2_per_objfile->dwo_files
11494 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11495
11496 find_entry.dwo_name = dwo_name;
11497 find_entry.comp_dir = comp_dir;
11498 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11499 INSERT);
11500
11501 return slot;
11502 }
11503
11504 static hashval_t
11505 hash_dwo_unit (const void *item)
11506 {
11507 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11508
11509 /* This drops the top 32 bits of the id, but is ok for a hash. */
11510 return dwo_unit->signature;
11511 }
11512
11513 static int
11514 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11515 {
11516 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11517 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11518
11519 /* The signature is assumed to be unique within the DWO file.
11520 So while object file CU dwo_id's always have the value zero,
11521 that's OK, assuming each object file DWO file has only one CU,
11522 and that's the rule for now. */
11523 return lhs->signature == rhs->signature;
11524 }
11525
11526 /* Allocate a hash table for DWO CUs,TUs.
11527 There is one of these tables for each of CUs,TUs for each DWO file. */
11528
11529 static htab_t
11530 allocate_dwo_unit_table (struct objfile *objfile)
11531 {
11532 /* Start out with a pretty small number.
11533 Generally DWO files contain only one CU and maybe some TUs. */
11534 return htab_create_alloc_ex (3,
11535 hash_dwo_unit,
11536 eq_dwo_unit,
11537 NULL,
11538 &objfile->objfile_obstack,
11539 hashtab_obstack_allocate,
11540 dummy_obstack_deallocate);
11541 }
11542
11543 /* die_reader_func for create_dwo_cu. */
11544
11545 static void
11546 create_dwo_cu_reader (const struct die_reader_specs *reader,
11547 const gdb_byte *info_ptr,
11548 struct die_info *comp_unit_die,
11549 int has_children,
11550 struct dwo_file *dwo_file,
11551 struct dwo_unit *dwo_unit)
11552 {
11553 struct dwarf2_cu *cu = reader->cu;
11554 sect_offset sect_off = cu->per_cu->sect_off;
11555 struct dwarf2_section_info *section = cu->per_cu->section;
11556
11557 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11558 if (!signature.has_value ())
11559 {
11560 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11561 " its dwo_id [in module %s]"),
11562 sect_offset_str (sect_off), dwo_file->dwo_name);
11563 return;
11564 }
11565
11566 dwo_unit->dwo_file = dwo_file;
11567 dwo_unit->signature = *signature;
11568 dwo_unit->section = section;
11569 dwo_unit->sect_off = sect_off;
11570 dwo_unit->length = cu->per_cu->length;
11571
11572 if (dwarf_read_debug)
11573 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11574 sect_offset_str (sect_off),
11575 hex_string (dwo_unit->signature));
11576 }
11577
11578 /* Create the dwo_units for the CUs in a DWO_FILE.
11579 Note: This function processes DWO files only, not DWP files. */
11580
11581 static void
11582 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11583 dwarf2_cu *cu, struct dwo_file &dwo_file,
11584 dwarf2_section_info &section, htab_t &cus_htab)
11585 {
11586 struct objfile *objfile = dwarf2_per_objfile->objfile;
11587 const gdb_byte *info_ptr, *end_ptr;
11588
11589 section.read (objfile);
11590 info_ptr = section.buffer;
11591
11592 if (info_ptr == NULL)
11593 return;
11594
11595 if (dwarf_read_debug)
11596 {
11597 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11598 section.get_name (),
11599 section.get_file_name ());
11600 }
11601
11602 end_ptr = info_ptr + section.size;
11603 while (info_ptr < end_ptr)
11604 {
11605 struct dwarf2_per_cu_data per_cu;
11606 struct dwo_unit read_unit {};
11607 struct dwo_unit *dwo_unit;
11608 void **slot;
11609 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11610
11611 memset (&per_cu, 0, sizeof (per_cu));
11612 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11613 per_cu.is_debug_types = 0;
11614 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11615 per_cu.section = &section;
11616
11617 cutu_reader reader (&per_cu, cu, &dwo_file);
11618 if (!reader.dummy_p)
11619 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11620 reader.has_children, &dwo_file, &read_unit);
11621 info_ptr += per_cu.length;
11622
11623 // If the unit could not be parsed, skip it.
11624 if (read_unit.dwo_file == NULL)
11625 continue;
11626
11627 if (cus_htab == NULL)
11628 cus_htab = allocate_dwo_unit_table (objfile);
11629
11630 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11631 *dwo_unit = read_unit;
11632 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11633 gdb_assert (slot != NULL);
11634 if (*slot != NULL)
11635 {
11636 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11637 sect_offset dup_sect_off = dup_cu->sect_off;
11638
11639 complaint (_("debug cu entry at offset %s is duplicate to"
11640 " the entry at offset %s, signature %s"),
11641 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11642 hex_string (dwo_unit->signature));
11643 }
11644 *slot = (void *)dwo_unit;
11645 }
11646 }
11647
11648 /* DWP file .debug_{cu,tu}_index section format:
11649 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11650
11651 DWP Version 1:
11652
11653 Both index sections have the same format, and serve to map a 64-bit
11654 signature to a set of section numbers. Each section begins with a header,
11655 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11656 indexes, and a pool of 32-bit section numbers. The index sections will be
11657 aligned at 8-byte boundaries in the file.
11658
11659 The index section header consists of:
11660
11661 V, 32 bit version number
11662 -, 32 bits unused
11663 N, 32 bit number of compilation units or type units in the index
11664 M, 32 bit number of slots in the hash table
11665
11666 Numbers are recorded using the byte order of the application binary.
11667
11668 The hash table begins at offset 16 in the section, and consists of an array
11669 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11670 order of the application binary). Unused slots in the hash table are 0.
11671 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11672
11673 The parallel table begins immediately after the hash table
11674 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11675 array of 32-bit indexes (using the byte order of the application binary),
11676 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11677 table contains a 32-bit index into the pool of section numbers. For unused
11678 hash table slots, the corresponding entry in the parallel table will be 0.
11679
11680 The pool of section numbers begins immediately following the hash table
11681 (at offset 16 + 12 * M from the beginning of the section). The pool of
11682 section numbers consists of an array of 32-bit words (using the byte order
11683 of the application binary). Each item in the array is indexed starting
11684 from 0. The hash table entry provides the index of the first section
11685 number in the set. Additional section numbers in the set follow, and the
11686 set is terminated by a 0 entry (section number 0 is not used in ELF).
11687
11688 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11689 section must be the first entry in the set, and the .debug_abbrev.dwo must
11690 be the second entry. Other members of the set may follow in any order.
11691
11692 ---
11693
11694 DWP Version 2:
11695
11696 DWP Version 2 combines all the .debug_info, etc. sections into one,
11697 and the entries in the index tables are now offsets into these sections.
11698 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11699 section.
11700
11701 Index Section Contents:
11702 Header
11703 Hash Table of Signatures dwp_hash_table.hash_table
11704 Parallel Table of Indices dwp_hash_table.unit_table
11705 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11706 Table of Section Sizes dwp_hash_table.v2.sizes
11707
11708 The index section header consists of:
11709
11710 V, 32 bit version number
11711 L, 32 bit number of columns in the table of section offsets
11712 N, 32 bit number of compilation units or type units in the index
11713 M, 32 bit number of slots in the hash table
11714
11715 Numbers are recorded using the byte order of the application binary.
11716
11717 The hash table has the same format as version 1.
11718 The parallel table of indices has the same format as version 1,
11719 except that the entries are origin-1 indices into the table of sections
11720 offsets and the table of section sizes.
11721
11722 The table of offsets begins immediately following the parallel table
11723 (at offset 16 + 12 * M from the beginning of the section). The table is
11724 a two-dimensional array of 32-bit words (using the byte order of the
11725 application binary), with L columns and N+1 rows, in row-major order.
11726 Each row in the array is indexed starting from 0. The first row provides
11727 a key to the remaining rows: each column in this row provides an identifier
11728 for a debug section, and the offsets in the same column of subsequent rows
11729 refer to that section. The section identifiers are:
11730
11731 DW_SECT_INFO 1 .debug_info.dwo
11732 DW_SECT_TYPES 2 .debug_types.dwo
11733 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11734 DW_SECT_LINE 4 .debug_line.dwo
11735 DW_SECT_LOC 5 .debug_loc.dwo
11736 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11737 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11738 DW_SECT_MACRO 8 .debug_macro.dwo
11739
11740 The offsets provided by the CU and TU index sections are the base offsets
11741 for the contributions made by each CU or TU to the corresponding section
11742 in the package file. Each CU and TU header contains an abbrev_offset
11743 field, used to find the abbreviations table for that CU or TU within the
11744 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11745 be interpreted as relative to the base offset given in the index section.
11746 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11747 should be interpreted as relative to the base offset for .debug_line.dwo,
11748 and offsets into other debug sections obtained from DWARF attributes should
11749 also be interpreted as relative to the corresponding base offset.
11750
11751 The table of sizes begins immediately following the table of offsets.
11752 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11753 with L columns and N rows, in row-major order. Each row in the array is
11754 indexed starting from 1 (row 0 is shared by the two tables).
11755
11756 ---
11757
11758 Hash table lookup is handled the same in version 1 and 2:
11759
11760 We assume that N and M will not exceed 2^32 - 1.
11761 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11762
11763 Given a 64-bit compilation unit signature or a type signature S, an entry
11764 in the hash table is located as follows:
11765
11766 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11767 the low-order k bits all set to 1.
11768
11769 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11770
11771 3) If the hash table entry at index H matches the signature, use that
11772 entry. If the hash table entry at index H is unused (all zeroes),
11773 terminate the search: the signature is not present in the table.
11774
11775 4) Let H = (H + H') modulo M. Repeat at Step 3.
11776
11777 Because M > N and H' and M are relatively prime, the search is guaranteed
11778 to stop at an unused slot or find the match. */
11779
11780 /* Create a hash table to map DWO IDs to their CU/TU entry in
11781 .debug_{info,types}.dwo in DWP_FILE.
11782 Returns NULL if there isn't one.
11783 Note: This function processes DWP files only, not DWO files. */
11784
11785 static struct dwp_hash_table *
11786 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11787 struct dwp_file *dwp_file, int is_debug_types)
11788 {
11789 struct objfile *objfile = dwarf2_per_objfile->objfile;
11790 bfd *dbfd = dwp_file->dbfd.get ();
11791 const gdb_byte *index_ptr, *index_end;
11792 struct dwarf2_section_info *index;
11793 uint32_t version, nr_columns, nr_units, nr_slots;
11794 struct dwp_hash_table *htab;
11795
11796 if (is_debug_types)
11797 index = &dwp_file->sections.tu_index;
11798 else
11799 index = &dwp_file->sections.cu_index;
11800
11801 if (index->empty ())
11802 return NULL;
11803 index->read (objfile);
11804
11805 index_ptr = index->buffer;
11806 index_end = index_ptr + index->size;
11807
11808 version = read_4_bytes (dbfd, index_ptr);
11809 index_ptr += 4;
11810 if (version == 2)
11811 nr_columns = read_4_bytes (dbfd, index_ptr);
11812 else
11813 nr_columns = 0;
11814 index_ptr += 4;
11815 nr_units = read_4_bytes (dbfd, index_ptr);
11816 index_ptr += 4;
11817 nr_slots = read_4_bytes (dbfd, index_ptr);
11818 index_ptr += 4;
11819
11820 if (version != 1 && version != 2)
11821 {
11822 error (_("Dwarf Error: unsupported DWP file version (%s)"
11823 " [in module %s]"),
11824 pulongest (version), dwp_file->name);
11825 }
11826 if (nr_slots != (nr_slots & -nr_slots))
11827 {
11828 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11829 " is not power of 2 [in module %s]"),
11830 pulongest (nr_slots), dwp_file->name);
11831 }
11832
11833 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11834 htab->version = version;
11835 htab->nr_columns = nr_columns;
11836 htab->nr_units = nr_units;
11837 htab->nr_slots = nr_slots;
11838 htab->hash_table = index_ptr;
11839 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11840
11841 /* Exit early if the table is empty. */
11842 if (nr_slots == 0 || nr_units == 0
11843 || (version == 2 && nr_columns == 0))
11844 {
11845 /* All must be zero. */
11846 if (nr_slots != 0 || nr_units != 0
11847 || (version == 2 && nr_columns != 0))
11848 {
11849 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11850 " all zero [in modules %s]"),
11851 dwp_file->name);
11852 }
11853 return htab;
11854 }
11855
11856 if (version == 1)
11857 {
11858 htab->section_pool.v1.indices =
11859 htab->unit_table + sizeof (uint32_t) * nr_slots;
11860 /* It's harder to decide whether the section is too small in v1.
11861 V1 is deprecated anyway so we punt. */
11862 }
11863 else
11864 {
11865 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11866 int *ids = htab->section_pool.v2.section_ids;
11867 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11868 /* Reverse map for error checking. */
11869 int ids_seen[DW_SECT_MAX + 1];
11870 int i;
11871
11872 if (nr_columns < 2)
11873 {
11874 error (_("Dwarf Error: bad DWP hash table, too few columns"
11875 " in section table [in module %s]"),
11876 dwp_file->name);
11877 }
11878 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, too many columns"
11881 " in section table [in module %s]"),
11882 dwp_file->name);
11883 }
11884 memset (ids, 255, sizeof_ids);
11885 memset (ids_seen, 255, sizeof (ids_seen));
11886 for (i = 0; i < nr_columns; ++i)
11887 {
11888 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11889
11890 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11891 {
11892 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11893 " in section table [in module %s]"),
11894 id, dwp_file->name);
11895 }
11896 if (ids_seen[id] != -1)
11897 {
11898 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11899 " id %d in section table [in module %s]"),
11900 id, dwp_file->name);
11901 }
11902 ids_seen[id] = i;
11903 ids[i] = id;
11904 }
11905 /* Must have exactly one info or types section. */
11906 if (((ids_seen[DW_SECT_INFO] != -1)
11907 + (ids_seen[DW_SECT_TYPES] != -1))
11908 != 1)
11909 {
11910 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11911 " DWO info/types section [in module %s]"),
11912 dwp_file->name);
11913 }
11914 /* Must have an abbrev section. */
11915 if (ids_seen[DW_SECT_ABBREV] == -1)
11916 {
11917 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11918 " section [in module %s]"),
11919 dwp_file->name);
11920 }
11921 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11922 htab->section_pool.v2.sizes =
11923 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11924 * nr_units * nr_columns);
11925 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11926 * nr_units * nr_columns))
11927 > index_end)
11928 {
11929 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
11933 }
11934
11935 return htab;
11936 }
11937
11938 /* Update SECTIONS with the data from SECTP.
11939
11940 This function is like the other "locate" section routines that are
11941 passed to bfd_map_over_sections, but in this context the sections to
11942 read comes from the DWP V1 hash table, not the full ELF section table.
11943
11944 The result is non-zero for success, or zero if an error was found. */
11945
11946 static int
11947 locate_v1_virtual_dwo_sections (asection *sectp,
11948 struct virtual_v1_dwo_sections *sections)
11949 {
11950 const struct dwop_section_names *names = &dwop_section_names;
11951
11952 if (section_is_p (sectp->name, &names->abbrev_dwo))
11953 {
11954 /* There can be only one. */
11955 if (sections->abbrev.s.section != NULL)
11956 return 0;
11957 sections->abbrev.s.section = sectp;
11958 sections->abbrev.size = bfd_section_size (sectp);
11959 }
11960 else if (section_is_p (sectp->name, &names->info_dwo)
11961 || section_is_p (sectp->name, &names->types_dwo))
11962 {
11963 /* There can be only one. */
11964 if (sections->info_or_types.s.section != NULL)
11965 return 0;
11966 sections->info_or_types.s.section = sectp;
11967 sections->info_or_types.size = bfd_section_size (sectp);
11968 }
11969 else if (section_is_p (sectp->name, &names->line_dwo))
11970 {
11971 /* There can be only one. */
11972 if (sections->line.s.section != NULL)
11973 return 0;
11974 sections->line.s.section = sectp;
11975 sections->line.size = bfd_section_size (sectp);
11976 }
11977 else if (section_is_p (sectp->name, &names->loc_dwo))
11978 {
11979 /* There can be only one. */
11980 if (sections->loc.s.section != NULL)
11981 return 0;
11982 sections->loc.s.section = sectp;
11983 sections->loc.size = bfd_section_size (sectp);
11984 }
11985 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11986 {
11987 /* There can be only one. */
11988 if (sections->macinfo.s.section != NULL)
11989 return 0;
11990 sections->macinfo.s.section = sectp;
11991 sections->macinfo.size = bfd_section_size (sectp);
11992 }
11993 else if (section_is_p (sectp->name, &names->macro_dwo))
11994 {
11995 /* There can be only one. */
11996 if (sections->macro.s.section != NULL)
11997 return 0;
11998 sections->macro.s.section = sectp;
11999 sections->macro.size = bfd_section_size (sectp);
12000 }
12001 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12002 {
12003 /* There can be only one. */
12004 if (sections->str_offsets.s.section != NULL)
12005 return 0;
12006 sections->str_offsets.s.section = sectp;
12007 sections->str_offsets.size = bfd_section_size (sectp);
12008 }
12009 else
12010 {
12011 /* No other kind of section is valid. */
12012 return 0;
12013 }
12014
12015 return 1;
12016 }
12017
12018 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12019 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12020 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12021 This is for DWP version 1 files. */
12022
12023 static struct dwo_unit *
12024 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12025 struct dwp_file *dwp_file,
12026 uint32_t unit_index,
12027 const char *comp_dir,
12028 ULONGEST signature, int is_debug_types)
12029 {
12030 struct objfile *objfile = dwarf2_per_objfile->objfile;
12031 const struct dwp_hash_table *dwp_htab =
12032 is_debug_types ? dwp_file->tus : dwp_file->cus;
12033 bfd *dbfd = dwp_file->dbfd.get ();
12034 const char *kind = is_debug_types ? "TU" : "CU";
12035 struct dwo_file *dwo_file;
12036 struct dwo_unit *dwo_unit;
12037 struct virtual_v1_dwo_sections sections;
12038 void **dwo_file_slot;
12039 int i;
12040
12041 gdb_assert (dwp_file->version == 1);
12042
12043 if (dwarf_read_debug)
12044 {
12045 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12046 kind,
12047 pulongest (unit_index), hex_string (signature),
12048 dwp_file->name);
12049 }
12050
12051 /* Fetch the sections of this DWO unit.
12052 Put a limit on the number of sections we look for so that bad data
12053 doesn't cause us to loop forever. */
12054
12055 #define MAX_NR_V1_DWO_SECTIONS \
12056 (1 /* .debug_info or .debug_types */ \
12057 + 1 /* .debug_abbrev */ \
12058 + 1 /* .debug_line */ \
12059 + 1 /* .debug_loc */ \
12060 + 1 /* .debug_str_offsets */ \
12061 + 1 /* .debug_macro or .debug_macinfo */ \
12062 + 1 /* trailing zero */)
12063
12064 memset (&sections, 0, sizeof (sections));
12065
12066 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12067 {
12068 asection *sectp;
12069 uint32_t section_nr =
12070 read_4_bytes (dbfd,
12071 dwp_htab->section_pool.v1.indices
12072 + (unit_index + i) * sizeof (uint32_t));
12073
12074 if (section_nr == 0)
12075 break;
12076 if (section_nr >= dwp_file->num_sections)
12077 {
12078 error (_("Dwarf Error: bad DWP hash table, section number too large"
12079 " [in module %s]"),
12080 dwp_file->name);
12081 }
12082
12083 sectp = dwp_file->elf_sections[section_nr];
12084 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12085 {
12086 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12087 " [in module %s]"),
12088 dwp_file->name);
12089 }
12090 }
12091
12092 if (i < 2
12093 || sections.info_or_types.empty ()
12094 || sections.abbrev.empty ())
12095 {
12096 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12097 " [in module %s]"),
12098 dwp_file->name);
12099 }
12100 if (i == MAX_NR_V1_DWO_SECTIONS)
12101 {
12102 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12103 " [in module %s]"),
12104 dwp_file->name);
12105 }
12106
12107 /* It's easier for the rest of the code if we fake a struct dwo_file and
12108 have dwo_unit "live" in that. At least for now.
12109
12110 The DWP file can be made up of a random collection of CUs and TUs.
12111 However, for each CU + set of TUs that came from the same original DWO
12112 file, we can combine them back into a virtual DWO file to save space
12113 (fewer struct dwo_file objects to allocate). Remember that for really
12114 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12115
12116 std::string virtual_dwo_name =
12117 string_printf ("virtual-dwo/%d-%d-%d-%d",
12118 sections.abbrev.get_id (),
12119 sections.line.get_id (),
12120 sections.loc.get_id (),
12121 sections.str_offsets.get_id ());
12122 /* Can we use an existing virtual DWO file? */
12123 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12124 virtual_dwo_name.c_str (),
12125 comp_dir);
12126 /* Create one if necessary. */
12127 if (*dwo_file_slot == NULL)
12128 {
12129 if (dwarf_read_debug)
12130 {
12131 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12132 virtual_dwo_name.c_str ());
12133 }
12134 dwo_file = new struct dwo_file;
12135 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12136 virtual_dwo_name);
12137 dwo_file->comp_dir = comp_dir;
12138 dwo_file->sections.abbrev = sections.abbrev;
12139 dwo_file->sections.line = sections.line;
12140 dwo_file->sections.loc = sections.loc;
12141 dwo_file->sections.macinfo = sections.macinfo;
12142 dwo_file->sections.macro = sections.macro;
12143 dwo_file->sections.str_offsets = sections.str_offsets;
12144 /* The "str" section is global to the entire DWP file. */
12145 dwo_file->sections.str = dwp_file->sections.str;
12146 /* The info or types section is assigned below to dwo_unit,
12147 there's no need to record it in dwo_file.
12148 Also, we can't simply record type sections in dwo_file because
12149 we record a pointer into the vector in dwo_unit. As we collect more
12150 types we'll grow the vector and eventually have to reallocate space
12151 for it, invalidating all copies of pointers into the previous
12152 contents. */
12153 *dwo_file_slot = dwo_file;
12154 }
12155 else
12156 {
12157 if (dwarf_read_debug)
12158 {
12159 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12160 virtual_dwo_name.c_str ());
12161 }
12162 dwo_file = (struct dwo_file *) *dwo_file_slot;
12163 }
12164
12165 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12166 dwo_unit->dwo_file = dwo_file;
12167 dwo_unit->signature = signature;
12168 dwo_unit->section =
12169 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12170 *dwo_unit->section = sections.info_or_types;
12171 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12172
12173 return dwo_unit;
12174 }
12175
12176 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12177 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12178 piece within that section used by a TU/CU, return a virtual section
12179 of just that piece. */
12180
12181 static struct dwarf2_section_info
12182 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12183 struct dwarf2_section_info *section,
12184 bfd_size_type offset, bfd_size_type size)
12185 {
12186 struct dwarf2_section_info result;
12187 asection *sectp;
12188
12189 gdb_assert (section != NULL);
12190 gdb_assert (!section->is_virtual);
12191
12192 memset (&result, 0, sizeof (result));
12193 result.s.containing_section = section;
12194 result.is_virtual = true;
12195
12196 if (size == 0)
12197 return result;
12198
12199 sectp = section->get_bfd_section ();
12200
12201 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12202 bounds of the real section. This is a pretty-rare event, so just
12203 flag an error (easier) instead of a warning and trying to cope. */
12204 if (sectp == NULL
12205 || offset + size > bfd_section_size (sectp))
12206 {
12207 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12208 " in section %s [in module %s]"),
12209 sectp ? bfd_section_name (sectp) : "<unknown>",
12210 objfile_name (dwarf2_per_objfile->objfile));
12211 }
12212
12213 result.virtual_offset = offset;
12214 result.size = size;
12215 return result;
12216 }
12217
12218 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12219 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12220 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12221 This is for DWP version 2 files. */
12222
12223 static struct dwo_unit *
12224 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12225 struct dwp_file *dwp_file,
12226 uint32_t unit_index,
12227 const char *comp_dir,
12228 ULONGEST signature, int is_debug_types)
12229 {
12230 struct objfile *objfile = dwarf2_per_objfile->objfile;
12231 const struct dwp_hash_table *dwp_htab =
12232 is_debug_types ? dwp_file->tus : dwp_file->cus;
12233 bfd *dbfd = dwp_file->dbfd.get ();
12234 const char *kind = is_debug_types ? "TU" : "CU";
12235 struct dwo_file *dwo_file;
12236 struct dwo_unit *dwo_unit;
12237 struct virtual_v2_dwo_sections sections;
12238 void **dwo_file_slot;
12239 int i;
12240
12241 gdb_assert (dwp_file->version == 2);
12242
12243 if (dwarf_read_debug)
12244 {
12245 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12246 kind,
12247 pulongest (unit_index), hex_string (signature),
12248 dwp_file->name);
12249 }
12250
12251 /* Fetch the section offsets of this DWO unit. */
12252
12253 memset (&sections, 0, sizeof (sections));
12254
12255 for (i = 0; i < dwp_htab->nr_columns; ++i)
12256 {
12257 uint32_t offset = read_4_bytes (dbfd,
12258 dwp_htab->section_pool.v2.offsets
12259 + (((unit_index - 1) * dwp_htab->nr_columns
12260 + i)
12261 * sizeof (uint32_t)));
12262 uint32_t size = read_4_bytes (dbfd,
12263 dwp_htab->section_pool.v2.sizes
12264 + (((unit_index - 1) * dwp_htab->nr_columns
12265 + i)
12266 * sizeof (uint32_t)));
12267
12268 switch (dwp_htab->section_pool.v2.section_ids[i])
12269 {
12270 case DW_SECT_INFO:
12271 case DW_SECT_TYPES:
12272 sections.info_or_types_offset = offset;
12273 sections.info_or_types_size = size;
12274 break;
12275 case DW_SECT_ABBREV:
12276 sections.abbrev_offset = offset;
12277 sections.abbrev_size = size;
12278 break;
12279 case DW_SECT_LINE:
12280 sections.line_offset = offset;
12281 sections.line_size = size;
12282 break;
12283 case DW_SECT_LOC:
12284 sections.loc_offset = offset;
12285 sections.loc_size = size;
12286 break;
12287 case DW_SECT_STR_OFFSETS:
12288 sections.str_offsets_offset = offset;
12289 sections.str_offsets_size = size;
12290 break;
12291 case DW_SECT_MACINFO:
12292 sections.macinfo_offset = offset;
12293 sections.macinfo_size = size;
12294 break;
12295 case DW_SECT_MACRO:
12296 sections.macro_offset = offset;
12297 sections.macro_size = size;
12298 break;
12299 }
12300 }
12301
12302 /* It's easier for the rest of the code if we fake a struct dwo_file and
12303 have dwo_unit "live" in that. At least for now.
12304
12305 The DWP file can be made up of a random collection of CUs and TUs.
12306 However, for each CU + set of TUs that came from the same original DWO
12307 file, we can combine them back into a virtual DWO file to save space
12308 (fewer struct dwo_file objects to allocate). Remember that for really
12309 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12310
12311 std::string virtual_dwo_name =
12312 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12313 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12314 (long) (sections.line_size ? sections.line_offset : 0),
12315 (long) (sections.loc_size ? sections.loc_offset : 0),
12316 (long) (sections.str_offsets_size
12317 ? sections.str_offsets_offset : 0));
12318 /* Can we use an existing virtual DWO file? */
12319 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12320 virtual_dwo_name.c_str (),
12321 comp_dir);
12322 /* Create one if necessary. */
12323 if (*dwo_file_slot == NULL)
12324 {
12325 if (dwarf_read_debug)
12326 {
12327 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12328 virtual_dwo_name.c_str ());
12329 }
12330 dwo_file = new struct dwo_file;
12331 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12332 virtual_dwo_name);
12333 dwo_file->comp_dir = comp_dir;
12334 dwo_file->sections.abbrev =
12335 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12336 sections.abbrev_offset, sections.abbrev_size);
12337 dwo_file->sections.line =
12338 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12339 sections.line_offset, sections.line_size);
12340 dwo_file->sections.loc =
12341 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12342 sections.loc_offset, sections.loc_size);
12343 dwo_file->sections.macinfo =
12344 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12345 sections.macinfo_offset, sections.macinfo_size);
12346 dwo_file->sections.macro =
12347 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12348 sections.macro_offset, sections.macro_size);
12349 dwo_file->sections.str_offsets =
12350 create_dwp_v2_section (dwarf2_per_objfile,
12351 &dwp_file->sections.str_offsets,
12352 sections.str_offsets_offset,
12353 sections.str_offsets_size);
12354 /* The "str" section is global to the entire DWP file. */
12355 dwo_file->sections.str = dwp_file->sections.str;
12356 /* The info or types section is assigned below to dwo_unit,
12357 there's no need to record it in dwo_file.
12358 Also, we can't simply record type sections in dwo_file because
12359 we record a pointer into the vector in dwo_unit. As we collect more
12360 types we'll grow the vector and eventually have to reallocate space
12361 for it, invalidating all copies of pointers into the previous
12362 contents. */
12363 *dwo_file_slot = dwo_file;
12364 }
12365 else
12366 {
12367 if (dwarf_read_debug)
12368 {
12369 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12370 virtual_dwo_name.c_str ());
12371 }
12372 dwo_file = (struct dwo_file *) *dwo_file_slot;
12373 }
12374
12375 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12376 dwo_unit->dwo_file = dwo_file;
12377 dwo_unit->signature = signature;
12378 dwo_unit->section =
12379 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12380 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12381 is_debug_types
12382 ? &dwp_file->sections.types
12383 : &dwp_file->sections.info,
12384 sections.info_or_types_offset,
12385 sections.info_or_types_size);
12386 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12387
12388 return dwo_unit;
12389 }
12390
12391 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12392 Returns NULL if the signature isn't found. */
12393
12394 static struct dwo_unit *
12395 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12396 struct dwp_file *dwp_file, const char *comp_dir,
12397 ULONGEST signature, int is_debug_types)
12398 {
12399 const struct dwp_hash_table *dwp_htab =
12400 is_debug_types ? dwp_file->tus : dwp_file->cus;
12401 bfd *dbfd = dwp_file->dbfd.get ();
12402 uint32_t mask = dwp_htab->nr_slots - 1;
12403 uint32_t hash = signature & mask;
12404 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12405 unsigned int i;
12406 void **slot;
12407 struct dwo_unit find_dwo_cu;
12408
12409 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12410 find_dwo_cu.signature = signature;
12411 slot = htab_find_slot (is_debug_types
12412 ? dwp_file->loaded_tus
12413 : dwp_file->loaded_cus,
12414 &find_dwo_cu, INSERT);
12415
12416 if (*slot != NULL)
12417 return (struct dwo_unit *) *slot;
12418
12419 /* Use a for loop so that we don't loop forever on bad debug info. */
12420 for (i = 0; i < dwp_htab->nr_slots; ++i)
12421 {
12422 ULONGEST signature_in_table;
12423
12424 signature_in_table =
12425 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12426 if (signature_in_table == signature)
12427 {
12428 uint32_t unit_index =
12429 read_4_bytes (dbfd,
12430 dwp_htab->unit_table + hash * sizeof (uint32_t));
12431
12432 if (dwp_file->version == 1)
12433 {
12434 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12435 dwp_file, unit_index,
12436 comp_dir, signature,
12437 is_debug_types);
12438 }
12439 else
12440 {
12441 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12442 dwp_file, unit_index,
12443 comp_dir, signature,
12444 is_debug_types);
12445 }
12446 return (struct dwo_unit *) *slot;
12447 }
12448 if (signature_in_table == 0)
12449 return NULL;
12450 hash = (hash + hash2) & mask;
12451 }
12452
12453 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12454 " [in module %s]"),
12455 dwp_file->name);
12456 }
12457
12458 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12459 Open the file specified by FILE_NAME and hand it off to BFD for
12460 preliminary analysis. Return a newly initialized bfd *, which
12461 includes a canonicalized copy of FILE_NAME.
12462 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12463 SEARCH_CWD is true if the current directory is to be searched.
12464 It will be searched before debug-file-directory.
12465 If successful, the file is added to the bfd include table of the
12466 objfile's bfd (see gdb_bfd_record_inclusion).
12467 If unable to find/open the file, return NULL.
12468 NOTE: This function is derived from symfile_bfd_open. */
12469
12470 static gdb_bfd_ref_ptr
12471 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12472 const char *file_name, int is_dwp, int search_cwd)
12473 {
12474 int desc;
12475 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12476 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12477 to debug_file_directory. */
12478 const char *search_path;
12479 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12480
12481 gdb::unique_xmalloc_ptr<char> search_path_holder;
12482 if (search_cwd)
12483 {
12484 if (*debug_file_directory != '\0')
12485 {
12486 search_path_holder.reset (concat (".", dirname_separator_string,
12487 debug_file_directory,
12488 (char *) NULL));
12489 search_path = search_path_holder.get ();
12490 }
12491 else
12492 search_path = ".";
12493 }
12494 else
12495 search_path = debug_file_directory;
12496
12497 openp_flags flags = OPF_RETURN_REALPATH;
12498 if (is_dwp)
12499 flags |= OPF_SEARCH_IN_PATH;
12500
12501 gdb::unique_xmalloc_ptr<char> absolute_name;
12502 desc = openp (search_path, flags, file_name,
12503 O_RDONLY | O_BINARY, &absolute_name);
12504 if (desc < 0)
12505 return NULL;
12506
12507 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12508 gnutarget, desc));
12509 if (sym_bfd == NULL)
12510 return NULL;
12511 bfd_set_cacheable (sym_bfd.get (), 1);
12512
12513 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12514 return NULL;
12515
12516 /* Success. Record the bfd as having been included by the objfile's bfd.
12517 This is important because things like demangled_names_hash lives in the
12518 objfile's per_bfd space and may have references to things like symbol
12519 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12520 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12521
12522 return sym_bfd;
12523 }
12524
12525 /* Try to open DWO file FILE_NAME.
12526 COMP_DIR is the DW_AT_comp_dir attribute.
12527 The result is the bfd handle of the file.
12528 If there is a problem finding or opening the file, return NULL.
12529 Upon success, the canonicalized path of the file is stored in the bfd,
12530 same as symfile_bfd_open. */
12531
12532 static gdb_bfd_ref_ptr
12533 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12534 const char *file_name, const char *comp_dir)
12535 {
12536 if (IS_ABSOLUTE_PATH (file_name))
12537 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12538 0 /*is_dwp*/, 0 /*search_cwd*/);
12539
12540 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12541
12542 if (comp_dir != NULL)
12543 {
12544 gdb::unique_xmalloc_ptr<char> path_to_try
12545 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12546
12547 /* NOTE: If comp_dir is a relative path, this will also try the
12548 search path, which seems useful. */
12549 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12550 path_to_try.get (),
12551 0 /*is_dwp*/,
12552 1 /*search_cwd*/));
12553 if (abfd != NULL)
12554 return abfd;
12555 }
12556
12557 /* That didn't work, try debug-file-directory, which, despite its name,
12558 is a list of paths. */
12559
12560 if (*debug_file_directory == '\0')
12561 return NULL;
12562
12563 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12564 0 /*is_dwp*/, 1 /*search_cwd*/);
12565 }
12566
12567 /* This function is mapped across the sections and remembers the offset and
12568 size of each of the DWO debugging sections we are interested in. */
12569
12570 static void
12571 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12572 {
12573 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12574 const struct dwop_section_names *names = &dwop_section_names;
12575
12576 if (section_is_p (sectp->name, &names->abbrev_dwo))
12577 {
12578 dwo_sections->abbrev.s.section = sectp;
12579 dwo_sections->abbrev.size = bfd_section_size (sectp);
12580 }
12581 else if (section_is_p (sectp->name, &names->info_dwo))
12582 {
12583 dwo_sections->info.s.section = sectp;
12584 dwo_sections->info.size = bfd_section_size (sectp);
12585 }
12586 else if (section_is_p (sectp->name, &names->line_dwo))
12587 {
12588 dwo_sections->line.s.section = sectp;
12589 dwo_sections->line.size = bfd_section_size (sectp);
12590 }
12591 else if (section_is_p (sectp->name, &names->loc_dwo))
12592 {
12593 dwo_sections->loc.s.section = sectp;
12594 dwo_sections->loc.size = bfd_section_size (sectp);
12595 }
12596 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12597 {
12598 dwo_sections->macinfo.s.section = sectp;
12599 dwo_sections->macinfo.size = bfd_section_size (sectp);
12600 }
12601 else if (section_is_p (sectp->name, &names->macro_dwo))
12602 {
12603 dwo_sections->macro.s.section = sectp;
12604 dwo_sections->macro.size = bfd_section_size (sectp);
12605 }
12606 else if (section_is_p (sectp->name, &names->str_dwo))
12607 {
12608 dwo_sections->str.s.section = sectp;
12609 dwo_sections->str.size = bfd_section_size (sectp);
12610 }
12611 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12612 {
12613 dwo_sections->str_offsets.s.section = sectp;
12614 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12615 }
12616 else if (section_is_p (sectp->name, &names->types_dwo))
12617 {
12618 struct dwarf2_section_info type_section;
12619
12620 memset (&type_section, 0, sizeof (type_section));
12621 type_section.s.section = sectp;
12622 type_section.size = bfd_section_size (sectp);
12623 dwo_sections->types.push_back (type_section);
12624 }
12625 }
12626
12627 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12628 by PER_CU. This is for the non-DWP case.
12629 The result is NULL if DWO_NAME can't be found. */
12630
12631 static struct dwo_file *
12632 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12633 const char *dwo_name, const char *comp_dir)
12634 {
12635 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12636
12637 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12638 if (dbfd == NULL)
12639 {
12640 if (dwarf_read_debug)
12641 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12642 return NULL;
12643 }
12644
12645 dwo_file_up dwo_file (new struct dwo_file);
12646 dwo_file->dwo_name = dwo_name;
12647 dwo_file->comp_dir = comp_dir;
12648 dwo_file->dbfd = std::move (dbfd);
12649
12650 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12651 &dwo_file->sections);
12652
12653 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12654 dwo_file->sections.info, dwo_file->cus);
12655
12656 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12657 dwo_file->sections.types, dwo_file->tus);
12658
12659 if (dwarf_read_debug)
12660 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12661
12662 return dwo_file.release ();
12663 }
12664
12665 /* This function is mapped across the sections and remembers the offset and
12666 size of each of the DWP debugging sections common to version 1 and 2 that
12667 we are interested in. */
12668
12669 static void
12670 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12671 void *dwp_file_ptr)
12672 {
12673 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12674 const struct dwop_section_names *names = &dwop_section_names;
12675 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12676
12677 /* Record the ELF section number for later lookup: this is what the
12678 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12679 gdb_assert (elf_section_nr < dwp_file->num_sections);
12680 dwp_file->elf_sections[elf_section_nr] = sectp;
12681
12682 /* Look for specific sections that we need. */
12683 if (section_is_p (sectp->name, &names->str_dwo))
12684 {
12685 dwp_file->sections.str.s.section = sectp;
12686 dwp_file->sections.str.size = bfd_section_size (sectp);
12687 }
12688 else if (section_is_p (sectp->name, &names->cu_index))
12689 {
12690 dwp_file->sections.cu_index.s.section = sectp;
12691 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12692 }
12693 else if (section_is_p (sectp->name, &names->tu_index))
12694 {
12695 dwp_file->sections.tu_index.s.section = sectp;
12696 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12697 }
12698 }
12699
12700 /* This function is mapped across the sections and remembers the offset and
12701 size of each of the DWP version 2 debugging sections that we are interested
12702 in. This is split into a separate function because we don't know if we
12703 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12704
12705 static void
12706 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12707 {
12708 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12709 const struct dwop_section_names *names = &dwop_section_names;
12710 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12711
12712 /* Record the ELF section number for later lookup: this is what the
12713 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12714 gdb_assert (elf_section_nr < dwp_file->num_sections);
12715 dwp_file->elf_sections[elf_section_nr] = sectp;
12716
12717 /* Look for specific sections that we need. */
12718 if (section_is_p (sectp->name, &names->abbrev_dwo))
12719 {
12720 dwp_file->sections.abbrev.s.section = sectp;
12721 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12722 }
12723 else if (section_is_p (sectp->name, &names->info_dwo))
12724 {
12725 dwp_file->sections.info.s.section = sectp;
12726 dwp_file->sections.info.size = bfd_section_size (sectp);
12727 }
12728 else if (section_is_p (sectp->name, &names->line_dwo))
12729 {
12730 dwp_file->sections.line.s.section = sectp;
12731 dwp_file->sections.line.size = bfd_section_size (sectp);
12732 }
12733 else if (section_is_p (sectp->name, &names->loc_dwo))
12734 {
12735 dwp_file->sections.loc.s.section = sectp;
12736 dwp_file->sections.loc.size = bfd_section_size (sectp);
12737 }
12738 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12739 {
12740 dwp_file->sections.macinfo.s.section = sectp;
12741 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12742 }
12743 else if (section_is_p (sectp->name, &names->macro_dwo))
12744 {
12745 dwp_file->sections.macro.s.section = sectp;
12746 dwp_file->sections.macro.size = bfd_section_size (sectp);
12747 }
12748 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12749 {
12750 dwp_file->sections.str_offsets.s.section = sectp;
12751 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12752 }
12753 else if (section_is_p (sectp->name, &names->types_dwo))
12754 {
12755 dwp_file->sections.types.s.section = sectp;
12756 dwp_file->sections.types.size = bfd_section_size (sectp);
12757 }
12758 }
12759
12760 /* Hash function for dwp_file loaded CUs/TUs. */
12761
12762 static hashval_t
12763 hash_dwp_loaded_cutus (const void *item)
12764 {
12765 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12766
12767 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12768 return dwo_unit->signature;
12769 }
12770
12771 /* Equality function for dwp_file loaded CUs/TUs. */
12772
12773 static int
12774 eq_dwp_loaded_cutus (const void *a, const void *b)
12775 {
12776 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12777 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12778
12779 return dua->signature == dub->signature;
12780 }
12781
12782 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12783
12784 static htab_t
12785 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12786 {
12787 return htab_create_alloc_ex (3,
12788 hash_dwp_loaded_cutus,
12789 eq_dwp_loaded_cutus,
12790 NULL,
12791 &objfile->objfile_obstack,
12792 hashtab_obstack_allocate,
12793 dummy_obstack_deallocate);
12794 }
12795
12796 /* Try to open DWP file FILE_NAME.
12797 The result is the bfd handle of the file.
12798 If there is a problem finding or opening the file, return NULL.
12799 Upon success, the canonicalized path of the file is stored in the bfd,
12800 same as symfile_bfd_open. */
12801
12802 static gdb_bfd_ref_ptr
12803 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12804 const char *file_name)
12805 {
12806 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12807 1 /*is_dwp*/,
12808 1 /*search_cwd*/));
12809 if (abfd != NULL)
12810 return abfd;
12811
12812 /* Work around upstream bug 15652.
12813 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12814 [Whether that's a "bug" is debatable, but it is getting in our way.]
12815 We have no real idea where the dwp file is, because gdb's realpath-ing
12816 of the executable's path may have discarded the needed info.
12817 [IWBN if the dwp file name was recorded in the executable, akin to
12818 .gnu_debuglink, but that doesn't exist yet.]
12819 Strip the directory from FILE_NAME and search again. */
12820 if (*debug_file_directory != '\0')
12821 {
12822 /* Don't implicitly search the current directory here.
12823 If the user wants to search "." to handle this case,
12824 it must be added to debug-file-directory. */
12825 return try_open_dwop_file (dwarf2_per_objfile,
12826 lbasename (file_name), 1 /*is_dwp*/,
12827 0 /*search_cwd*/);
12828 }
12829
12830 return NULL;
12831 }
12832
12833 /* Initialize the use of the DWP file for the current objfile.
12834 By convention the name of the DWP file is ${objfile}.dwp.
12835 The result is NULL if it can't be found. */
12836
12837 static std::unique_ptr<struct dwp_file>
12838 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12839 {
12840 struct objfile *objfile = dwarf2_per_objfile->objfile;
12841
12842 /* Try to find first .dwp for the binary file before any symbolic links
12843 resolving. */
12844
12845 /* If the objfile is a debug file, find the name of the real binary
12846 file and get the name of dwp file from there. */
12847 std::string dwp_name;
12848 if (objfile->separate_debug_objfile_backlink != NULL)
12849 {
12850 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12851 const char *backlink_basename = lbasename (backlink->original_name);
12852
12853 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12854 }
12855 else
12856 dwp_name = objfile->original_name;
12857
12858 dwp_name += ".dwp";
12859
12860 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12861 if (dbfd == NULL
12862 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12863 {
12864 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12865 dwp_name = objfile_name (objfile);
12866 dwp_name += ".dwp";
12867 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12868 }
12869
12870 if (dbfd == NULL)
12871 {
12872 if (dwarf_read_debug)
12873 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12874 return std::unique_ptr<dwp_file> ();
12875 }
12876
12877 const char *name = bfd_get_filename (dbfd.get ());
12878 std::unique_ptr<struct dwp_file> dwp_file
12879 (new struct dwp_file (name, std::move (dbfd)));
12880
12881 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12882 dwp_file->elf_sections =
12883 OBSTACK_CALLOC (&objfile->objfile_obstack,
12884 dwp_file->num_sections, asection *);
12885
12886 bfd_map_over_sections (dwp_file->dbfd.get (),
12887 dwarf2_locate_common_dwp_sections,
12888 dwp_file.get ());
12889
12890 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12891 0);
12892
12893 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12894 1);
12895
12896 /* The DWP file version is stored in the hash table. Oh well. */
12897 if (dwp_file->cus && dwp_file->tus
12898 && dwp_file->cus->version != dwp_file->tus->version)
12899 {
12900 /* Technically speaking, we should try to limp along, but this is
12901 pretty bizarre. We use pulongest here because that's the established
12902 portability solution (e.g, we cannot use %u for uint32_t). */
12903 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12904 " TU version %s [in DWP file %s]"),
12905 pulongest (dwp_file->cus->version),
12906 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12907 }
12908
12909 if (dwp_file->cus)
12910 dwp_file->version = dwp_file->cus->version;
12911 else if (dwp_file->tus)
12912 dwp_file->version = dwp_file->tus->version;
12913 else
12914 dwp_file->version = 2;
12915
12916 if (dwp_file->version == 2)
12917 bfd_map_over_sections (dwp_file->dbfd.get (),
12918 dwarf2_locate_v2_dwp_sections,
12919 dwp_file.get ());
12920
12921 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12922 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12923
12924 if (dwarf_read_debug)
12925 {
12926 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12927 fprintf_unfiltered (gdb_stdlog,
12928 " %s CUs, %s TUs\n",
12929 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12930 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12931 }
12932
12933 return dwp_file;
12934 }
12935
12936 /* Wrapper around open_and_init_dwp_file, only open it once. */
12937
12938 static struct dwp_file *
12939 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12940 {
12941 if (! dwarf2_per_objfile->dwp_checked)
12942 {
12943 dwarf2_per_objfile->dwp_file
12944 = open_and_init_dwp_file (dwarf2_per_objfile);
12945 dwarf2_per_objfile->dwp_checked = 1;
12946 }
12947 return dwarf2_per_objfile->dwp_file.get ();
12948 }
12949
12950 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12951 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12952 or in the DWP file for the objfile, referenced by THIS_UNIT.
12953 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12954 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12955
12956 This is called, for example, when wanting to read a variable with a
12957 complex location. Therefore we don't want to do file i/o for every call.
12958 Therefore we don't want to look for a DWO file on every call.
12959 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12960 then we check if we've already seen DWO_NAME, and only THEN do we check
12961 for a DWO file.
12962
12963 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12964 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12965
12966 static struct dwo_unit *
12967 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12968 const char *dwo_name, const char *comp_dir,
12969 ULONGEST signature, int is_debug_types)
12970 {
12971 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12972 struct objfile *objfile = dwarf2_per_objfile->objfile;
12973 const char *kind = is_debug_types ? "TU" : "CU";
12974 void **dwo_file_slot;
12975 struct dwo_file *dwo_file;
12976 struct dwp_file *dwp_file;
12977
12978 /* First see if there's a DWP file.
12979 If we have a DWP file but didn't find the DWO inside it, don't
12980 look for the original DWO file. It makes gdb behave differently
12981 depending on whether one is debugging in the build tree. */
12982
12983 dwp_file = get_dwp_file (dwarf2_per_objfile);
12984 if (dwp_file != NULL)
12985 {
12986 const struct dwp_hash_table *dwp_htab =
12987 is_debug_types ? dwp_file->tus : dwp_file->cus;
12988
12989 if (dwp_htab != NULL)
12990 {
12991 struct dwo_unit *dwo_cutu =
12992 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12993 signature, is_debug_types);
12994
12995 if (dwo_cutu != NULL)
12996 {
12997 if (dwarf_read_debug)
12998 {
12999 fprintf_unfiltered (gdb_stdlog,
13000 "Virtual DWO %s %s found: @%s\n",
13001 kind, hex_string (signature),
13002 host_address_to_string (dwo_cutu));
13003 }
13004 return dwo_cutu;
13005 }
13006 }
13007 }
13008 else
13009 {
13010 /* No DWP file, look for the DWO file. */
13011
13012 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13013 dwo_name, comp_dir);
13014 if (*dwo_file_slot == NULL)
13015 {
13016 /* Read in the file and build a table of the CUs/TUs it contains. */
13017 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13018 }
13019 /* NOTE: This will be NULL if unable to open the file. */
13020 dwo_file = (struct dwo_file *) *dwo_file_slot;
13021
13022 if (dwo_file != NULL)
13023 {
13024 struct dwo_unit *dwo_cutu = NULL;
13025
13026 if (is_debug_types && dwo_file->tus)
13027 {
13028 struct dwo_unit find_dwo_cutu;
13029
13030 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13031 find_dwo_cutu.signature = signature;
13032 dwo_cutu
13033 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13034 }
13035 else if (!is_debug_types && dwo_file->cus)
13036 {
13037 struct dwo_unit find_dwo_cutu;
13038
13039 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13040 find_dwo_cutu.signature = signature;
13041 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13042 &find_dwo_cutu);
13043 }
13044
13045 if (dwo_cutu != NULL)
13046 {
13047 if (dwarf_read_debug)
13048 {
13049 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13050 kind, dwo_name, hex_string (signature),
13051 host_address_to_string (dwo_cutu));
13052 }
13053 return dwo_cutu;
13054 }
13055 }
13056 }
13057
13058 /* We didn't find it. This could mean a dwo_id mismatch, or
13059 someone deleted the DWO/DWP file, or the search path isn't set up
13060 correctly to find the file. */
13061
13062 if (dwarf_read_debug)
13063 {
13064 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13065 kind, dwo_name, hex_string (signature));
13066 }
13067
13068 /* This is a warning and not a complaint because it can be caused by
13069 pilot error (e.g., user accidentally deleting the DWO). */
13070 {
13071 /* Print the name of the DWP file if we looked there, helps the user
13072 better diagnose the problem. */
13073 std::string dwp_text;
13074
13075 if (dwp_file != NULL)
13076 dwp_text = string_printf (" [in DWP file %s]",
13077 lbasename (dwp_file->name));
13078
13079 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13080 " [in module %s]"),
13081 kind, dwo_name, hex_string (signature),
13082 dwp_text.c_str (),
13083 this_unit->is_debug_types ? "TU" : "CU",
13084 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13085 }
13086 return NULL;
13087 }
13088
13089 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13090 See lookup_dwo_cutu_unit for details. */
13091
13092 static struct dwo_unit *
13093 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13094 const char *dwo_name, const char *comp_dir,
13095 ULONGEST signature)
13096 {
13097 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13098 }
13099
13100 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13101 See lookup_dwo_cutu_unit for details. */
13102
13103 static struct dwo_unit *
13104 lookup_dwo_type_unit (struct signatured_type *this_tu,
13105 const char *dwo_name, const char *comp_dir)
13106 {
13107 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13108 }
13109
13110 /* Traversal function for queue_and_load_all_dwo_tus. */
13111
13112 static int
13113 queue_and_load_dwo_tu (void **slot, void *info)
13114 {
13115 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13116 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13117 ULONGEST signature = dwo_unit->signature;
13118 struct signatured_type *sig_type =
13119 lookup_dwo_signatured_type (per_cu->cu, signature);
13120
13121 if (sig_type != NULL)
13122 {
13123 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13124
13125 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13126 a real dependency of PER_CU on SIG_TYPE. That is detected later
13127 while processing PER_CU. */
13128 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13129 load_full_type_unit (sig_cu);
13130 per_cu->imported_symtabs_push (sig_cu);
13131 }
13132
13133 return 1;
13134 }
13135
13136 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13137 The DWO may have the only definition of the type, though it may not be
13138 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13139 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13140
13141 static void
13142 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13143 {
13144 struct dwo_unit *dwo_unit;
13145 struct dwo_file *dwo_file;
13146
13147 gdb_assert (!per_cu->is_debug_types);
13148 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13149 gdb_assert (per_cu->cu != NULL);
13150
13151 dwo_unit = per_cu->cu->dwo_unit;
13152 gdb_assert (dwo_unit != NULL);
13153
13154 dwo_file = dwo_unit->dwo_file;
13155 if (dwo_file->tus != NULL)
13156 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13157 }
13158
13159 /* Read in various DIEs. */
13160
13161 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13162 Inherit only the children of the DW_AT_abstract_origin DIE not being
13163 already referenced by DW_AT_abstract_origin from the children of the
13164 current DIE. */
13165
13166 static void
13167 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13168 {
13169 struct die_info *child_die;
13170 sect_offset *offsetp;
13171 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13172 struct die_info *origin_die;
13173 /* Iterator of the ORIGIN_DIE children. */
13174 struct die_info *origin_child_die;
13175 struct attribute *attr;
13176 struct dwarf2_cu *origin_cu;
13177 struct pending **origin_previous_list_in_scope;
13178
13179 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13180 if (!attr)
13181 return;
13182
13183 /* Note that following die references may follow to a die in a
13184 different cu. */
13185
13186 origin_cu = cu;
13187 origin_die = follow_die_ref (die, attr, &origin_cu);
13188
13189 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13190 symbols in. */
13191 origin_previous_list_in_scope = origin_cu->list_in_scope;
13192 origin_cu->list_in_scope = cu->list_in_scope;
13193
13194 if (die->tag != origin_die->tag
13195 && !(die->tag == DW_TAG_inlined_subroutine
13196 && origin_die->tag == DW_TAG_subprogram))
13197 complaint (_("DIE %s and its abstract origin %s have different tags"),
13198 sect_offset_str (die->sect_off),
13199 sect_offset_str (origin_die->sect_off));
13200
13201 std::vector<sect_offset> offsets;
13202
13203 for (child_die = die->child;
13204 child_die && child_die->tag;
13205 child_die = sibling_die (child_die))
13206 {
13207 struct die_info *child_origin_die;
13208 struct dwarf2_cu *child_origin_cu;
13209
13210 /* We are trying to process concrete instance entries:
13211 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13212 it's not relevant to our analysis here. i.e. detecting DIEs that are
13213 present in the abstract instance but not referenced in the concrete
13214 one. */
13215 if (child_die->tag == DW_TAG_call_site
13216 || child_die->tag == DW_TAG_GNU_call_site)
13217 continue;
13218
13219 /* For each CHILD_DIE, find the corresponding child of
13220 ORIGIN_DIE. If there is more than one layer of
13221 DW_AT_abstract_origin, follow them all; there shouldn't be,
13222 but GCC versions at least through 4.4 generate this (GCC PR
13223 40573). */
13224 child_origin_die = child_die;
13225 child_origin_cu = cu;
13226 while (1)
13227 {
13228 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13229 child_origin_cu);
13230 if (attr == NULL)
13231 break;
13232 child_origin_die = follow_die_ref (child_origin_die, attr,
13233 &child_origin_cu);
13234 }
13235
13236 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13237 counterpart may exist. */
13238 if (child_origin_die != child_die)
13239 {
13240 if (child_die->tag != child_origin_die->tag
13241 && !(child_die->tag == DW_TAG_inlined_subroutine
13242 && child_origin_die->tag == DW_TAG_subprogram))
13243 complaint (_("Child DIE %s and its abstract origin %s have "
13244 "different tags"),
13245 sect_offset_str (child_die->sect_off),
13246 sect_offset_str (child_origin_die->sect_off));
13247 if (child_origin_die->parent != origin_die)
13248 complaint (_("Child DIE %s and its abstract origin %s have "
13249 "different parents"),
13250 sect_offset_str (child_die->sect_off),
13251 sect_offset_str (child_origin_die->sect_off));
13252 else
13253 offsets.push_back (child_origin_die->sect_off);
13254 }
13255 }
13256 std::sort (offsets.begin (), offsets.end ());
13257 sect_offset *offsets_end = offsets.data () + offsets.size ();
13258 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13259 if (offsetp[-1] == *offsetp)
13260 complaint (_("Multiple children of DIE %s refer "
13261 "to DIE %s as their abstract origin"),
13262 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13263
13264 offsetp = offsets.data ();
13265 origin_child_die = origin_die->child;
13266 while (origin_child_die && origin_child_die->tag)
13267 {
13268 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13269 while (offsetp < offsets_end
13270 && *offsetp < origin_child_die->sect_off)
13271 offsetp++;
13272 if (offsetp >= offsets_end
13273 || *offsetp > origin_child_die->sect_off)
13274 {
13275 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13276 Check whether we're already processing ORIGIN_CHILD_DIE.
13277 This can happen with mutually referenced abstract_origins.
13278 PR 16581. */
13279 if (!origin_child_die->in_process)
13280 process_die (origin_child_die, origin_cu);
13281 }
13282 origin_child_die = sibling_die (origin_child_die);
13283 }
13284 origin_cu->list_in_scope = origin_previous_list_in_scope;
13285
13286 if (cu != origin_cu)
13287 compute_delayed_physnames (origin_cu);
13288 }
13289
13290 static void
13291 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13292 {
13293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13295 struct context_stack *newobj;
13296 CORE_ADDR lowpc;
13297 CORE_ADDR highpc;
13298 struct die_info *child_die;
13299 struct attribute *attr, *call_line, *call_file;
13300 const char *name;
13301 CORE_ADDR baseaddr;
13302 struct block *block;
13303 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13304 std::vector<struct symbol *> template_args;
13305 struct template_symbol *templ_func = NULL;
13306
13307 if (inlined_func)
13308 {
13309 /* If we do not have call site information, we can't show the
13310 caller of this inlined function. That's too confusing, so
13311 only use the scope for local variables. */
13312 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13313 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13314 if (call_line == NULL || call_file == NULL)
13315 {
13316 read_lexical_block_scope (die, cu);
13317 return;
13318 }
13319 }
13320
13321 baseaddr = objfile->text_section_offset ();
13322
13323 name = dwarf2_name (die, cu);
13324
13325 /* Ignore functions with missing or empty names. These are actually
13326 illegal according to the DWARF standard. */
13327 if (name == NULL)
13328 {
13329 complaint (_("missing name for subprogram DIE at %s"),
13330 sect_offset_str (die->sect_off));
13331 return;
13332 }
13333
13334 /* Ignore functions with missing or invalid low and high pc attributes. */
13335 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13336 <= PC_BOUNDS_INVALID)
13337 {
13338 attr = dwarf2_attr (die, DW_AT_external, cu);
13339 if (!attr || !DW_UNSND (attr))
13340 complaint (_("cannot get low and high bounds "
13341 "for subprogram DIE at %s"),
13342 sect_offset_str (die->sect_off));
13343 return;
13344 }
13345
13346 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13347 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13348
13349 /* If we have any template arguments, then we must allocate a
13350 different sort of symbol. */
13351 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13352 {
13353 if (child_die->tag == DW_TAG_template_type_param
13354 || child_die->tag == DW_TAG_template_value_param)
13355 {
13356 templ_func = allocate_template_symbol (objfile);
13357 templ_func->subclass = SYMBOL_TEMPLATE;
13358 break;
13359 }
13360 }
13361
13362 newobj = cu->get_builder ()->push_context (0, lowpc);
13363 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13364 (struct symbol *) templ_func);
13365
13366 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13367 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13368 cu->language);
13369
13370 /* If there is a location expression for DW_AT_frame_base, record
13371 it. */
13372 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13373 if (attr != nullptr)
13374 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13375
13376 /* If there is a location for the static link, record it. */
13377 newobj->static_link = NULL;
13378 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13379 if (attr != nullptr)
13380 {
13381 newobj->static_link
13382 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13383 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13384 dwarf2_per_cu_addr_type (cu->per_cu));
13385 }
13386
13387 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13388
13389 if (die->child != NULL)
13390 {
13391 child_die = die->child;
13392 while (child_die && child_die->tag)
13393 {
13394 if (child_die->tag == DW_TAG_template_type_param
13395 || child_die->tag == DW_TAG_template_value_param)
13396 {
13397 struct symbol *arg = new_symbol (child_die, NULL, cu);
13398
13399 if (arg != NULL)
13400 template_args.push_back (arg);
13401 }
13402 else
13403 process_die (child_die, cu);
13404 child_die = sibling_die (child_die);
13405 }
13406 }
13407
13408 inherit_abstract_dies (die, cu);
13409
13410 /* If we have a DW_AT_specification, we might need to import using
13411 directives from the context of the specification DIE. See the
13412 comment in determine_prefix. */
13413 if (cu->language == language_cplus
13414 && dwarf2_attr (die, DW_AT_specification, cu))
13415 {
13416 struct dwarf2_cu *spec_cu = cu;
13417 struct die_info *spec_die = die_specification (die, &spec_cu);
13418
13419 while (spec_die)
13420 {
13421 child_die = spec_die->child;
13422 while (child_die && child_die->tag)
13423 {
13424 if (child_die->tag == DW_TAG_imported_module)
13425 process_die (child_die, spec_cu);
13426 child_die = sibling_die (child_die);
13427 }
13428
13429 /* In some cases, GCC generates specification DIEs that
13430 themselves contain DW_AT_specification attributes. */
13431 spec_die = die_specification (spec_die, &spec_cu);
13432 }
13433 }
13434
13435 struct context_stack cstk = cu->get_builder ()->pop_context ();
13436 /* Make a block for the local symbols within. */
13437 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13438 cstk.static_link, lowpc, highpc);
13439
13440 /* For C++, set the block's scope. */
13441 if ((cu->language == language_cplus
13442 || cu->language == language_fortran
13443 || cu->language == language_d
13444 || cu->language == language_rust)
13445 && cu->processing_has_namespace_info)
13446 block_set_scope (block, determine_prefix (die, cu),
13447 &objfile->objfile_obstack);
13448
13449 /* If we have address ranges, record them. */
13450 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13451
13452 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13453
13454 /* Attach template arguments to function. */
13455 if (!template_args.empty ())
13456 {
13457 gdb_assert (templ_func != NULL);
13458
13459 templ_func->n_template_arguments = template_args.size ();
13460 templ_func->template_arguments
13461 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13462 templ_func->n_template_arguments);
13463 memcpy (templ_func->template_arguments,
13464 template_args.data (),
13465 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13466
13467 /* Make sure that the symtab is set on the new symbols. Even
13468 though they don't appear in this symtab directly, other parts
13469 of gdb assume that symbols do, and this is reasonably
13470 true. */
13471 for (symbol *sym : template_args)
13472 symbol_set_symtab (sym, symbol_symtab (templ_func));
13473 }
13474
13475 /* In C++, we can have functions nested inside functions (e.g., when
13476 a function declares a class that has methods). This means that
13477 when we finish processing a function scope, we may need to go
13478 back to building a containing block's symbol lists. */
13479 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13480 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13481
13482 /* If we've finished processing a top-level function, subsequent
13483 symbols go in the file symbol list. */
13484 if (cu->get_builder ()->outermost_context_p ())
13485 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13486 }
13487
13488 /* Process all the DIES contained within a lexical block scope. Start
13489 a new scope, process the dies, and then close the scope. */
13490
13491 static void
13492 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13493 {
13494 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13495 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13496 CORE_ADDR lowpc, highpc;
13497 struct die_info *child_die;
13498 CORE_ADDR baseaddr;
13499
13500 baseaddr = objfile->text_section_offset ();
13501
13502 /* Ignore blocks with missing or invalid low and high pc attributes. */
13503 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13504 as multiple lexical blocks? Handling children in a sane way would
13505 be nasty. Might be easier to properly extend generic blocks to
13506 describe ranges. */
13507 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13508 {
13509 case PC_BOUNDS_NOT_PRESENT:
13510 /* DW_TAG_lexical_block has no attributes, process its children as if
13511 there was no wrapping by that DW_TAG_lexical_block.
13512 GCC does no longer produces such DWARF since GCC r224161. */
13513 for (child_die = die->child;
13514 child_die != NULL && child_die->tag;
13515 child_die = sibling_die (child_die))
13516 process_die (child_die, cu);
13517 return;
13518 case PC_BOUNDS_INVALID:
13519 return;
13520 }
13521 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13522 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13523
13524 cu->get_builder ()->push_context (0, lowpc);
13525 if (die->child != NULL)
13526 {
13527 child_die = die->child;
13528 while (child_die && child_die->tag)
13529 {
13530 process_die (child_die, cu);
13531 child_die = sibling_die (child_die);
13532 }
13533 }
13534 inherit_abstract_dies (die, cu);
13535 struct context_stack cstk = cu->get_builder ()->pop_context ();
13536
13537 if (*cu->get_builder ()->get_local_symbols () != NULL
13538 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13539 {
13540 struct block *block
13541 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13542 cstk.start_addr, highpc);
13543
13544 /* Note that recording ranges after traversing children, as we
13545 do here, means that recording a parent's ranges entails
13546 walking across all its children's ranges as they appear in
13547 the address map, which is quadratic behavior.
13548
13549 It would be nicer to record the parent's ranges before
13550 traversing its children, simply overriding whatever you find
13551 there. But since we don't even decide whether to create a
13552 block until after we've traversed its children, that's hard
13553 to do. */
13554 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13555 }
13556 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13557 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13558 }
13559
13560 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13561
13562 static void
13563 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13564 {
13565 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13566 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13567 CORE_ADDR pc, baseaddr;
13568 struct attribute *attr;
13569 struct call_site *call_site, call_site_local;
13570 void **slot;
13571 int nparams;
13572 struct die_info *child_die;
13573
13574 baseaddr = objfile->text_section_offset ();
13575
13576 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13577 if (attr == NULL)
13578 {
13579 /* This was a pre-DWARF-5 GNU extension alias
13580 for DW_AT_call_return_pc. */
13581 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13582 }
13583 if (!attr)
13584 {
13585 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13586 "DIE %s [in module %s]"),
13587 sect_offset_str (die->sect_off), objfile_name (objfile));
13588 return;
13589 }
13590 pc = attr->value_as_address () + baseaddr;
13591 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13592
13593 if (cu->call_site_htab == NULL)
13594 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13595 NULL, &objfile->objfile_obstack,
13596 hashtab_obstack_allocate, NULL);
13597 call_site_local.pc = pc;
13598 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13599 if (*slot != NULL)
13600 {
13601 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13602 "DIE %s [in module %s]"),
13603 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13604 objfile_name (objfile));
13605 return;
13606 }
13607
13608 /* Count parameters at the caller. */
13609
13610 nparams = 0;
13611 for (child_die = die->child; child_die && child_die->tag;
13612 child_die = sibling_die (child_die))
13613 {
13614 if (child_die->tag != DW_TAG_call_site_parameter
13615 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13616 {
13617 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13618 "DW_TAG_call_site child DIE %s [in module %s]"),
13619 child_die->tag, sect_offset_str (child_die->sect_off),
13620 objfile_name (objfile));
13621 continue;
13622 }
13623
13624 nparams++;
13625 }
13626
13627 call_site
13628 = ((struct call_site *)
13629 obstack_alloc (&objfile->objfile_obstack,
13630 sizeof (*call_site)
13631 + (sizeof (*call_site->parameter) * (nparams - 1))));
13632 *slot = call_site;
13633 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13634 call_site->pc = pc;
13635
13636 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13637 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13638 {
13639 struct die_info *func_die;
13640
13641 /* Skip also over DW_TAG_inlined_subroutine. */
13642 for (func_die = die->parent;
13643 func_die && func_die->tag != DW_TAG_subprogram
13644 && func_die->tag != DW_TAG_subroutine_type;
13645 func_die = func_die->parent);
13646
13647 /* DW_AT_call_all_calls is a superset
13648 of DW_AT_call_all_tail_calls. */
13649 if (func_die
13650 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13651 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13652 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13653 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13654 {
13655 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13656 not complete. But keep CALL_SITE for look ups via call_site_htab,
13657 both the initial caller containing the real return address PC and
13658 the final callee containing the current PC of a chain of tail
13659 calls do not need to have the tail call list complete. But any
13660 function candidate for a virtual tail call frame searched via
13661 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13662 determined unambiguously. */
13663 }
13664 else
13665 {
13666 struct type *func_type = NULL;
13667
13668 if (func_die)
13669 func_type = get_die_type (func_die, cu);
13670 if (func_type != NULL)
13671 {
13672 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13673
13674 /* Enlist this call site to the function. */
13675 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13676 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13677 }
13678 else
13679 complaint (_("Cannot find function owning DW_TAG_call_site "
13680 "DIE %s [in module %s]"),
13681 sect_offset_str (die->sect_off), objfile_name (objfile));
13682 }
13683 }
13684
13685 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13686 if (attr == NULL)
13687 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13688 if (attr == NULL)
13689 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13690 if (attr == NULL)
13691 {
13692 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13693 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13694 }
13695 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13696 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13697 /* Keep NULL DWARF_BLOCK. */;
13698 else if (attr->form_is_block ())
13699 {
13700 struct dwarf2_locexpr_baton *dlbaton;
13701
13702 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13703 dlbaton->data = DW_BLOCK (attr)->data;
13704 dlbaton->size = DW_BLOCK (attr)->size;
13705 dlbaton->per_cu = cu->per_cu;
13706
13707 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13708 }
13709 else if (attr->form_is_ref ())
13710 {
13711 struct dwarf2_cu *target_cu = cu;
13712 struct die_info *target_die;
13713
13714 target_die = follow_die_ref (die, attr, &target_cu);
13715 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13716 if (die_is_declaration (target_die, target_cu))
13717 {
13718 const char *target_physname;
13719
13720 /* Prefer the mangled name; otherwise compute the demangled one. */
13721 target_physname = dw2_linkage_name (target_die, target_cu);
13722 if (target_physname == NULL)
13723 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13724 if (target_physname == NULL)
13725 complaint (_("DW_AT_call_target target DIE has invalid "
13726 "physname, for referencing DIE %s [in module %s]"),
13727 sect_offset_str (die->sect_off), objfile_name (objfile));
13728 else
13729 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13730 }
13731 else
13732 {
13733 CORE_ADDR lowpc;
13734
13735 /* DW_AT_entry_pc should be preferred. */
13736 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13737 <= PC_BOUNDS_INVALID)
13738 complaint (_("DW_AT_call_target target DIE has invalid "
13739 "low pc, for referencing DIE %s [in module %s]"),
13740 sect_offset_str (die->sect_off), objfile_name (objfile));
13741 else
13742 {
13743 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13744 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13745 }
13746 }
13747 }
13748 else
13749 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13750 "block nor reference, for DIE %s [in module %s]"),
13751 sect_offset_str (die->sect_off), objfile_name (objfile));
13752
13753 call_site->per_cu = cu->per_cu;
13754
13755 for (child_die = die->child;
13756 child_die && child_die->tag;
13757 child_die = sibling_die (child_die))
13758 {
13759 struct call_site_parameter *parameter;
13760 struct attribute *loc, *origin;
13761
13762 if (child_die->tag != DW_TAG_call_site_parameter
13763 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13764 {
13765 /* Already printed the complaint above. */
13766 continue;
13767 }
13768
13769 gdb_assert (call_site->parameter_count < nparams);
13770 parameter = &call_site->parameter[call_site->parameter_count];
13771
13772 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13773 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13774 register is contained in DW_AT_call_value. */
13775
13776 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13777 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13778 if (origin == NULL)
13779 {
13780 /* This was a pre-DWARF-5 GNU extension alias
13781 for DW_AT_call_parameter. */
13782 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13783 }
13784 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13785 {
13786 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13787
13788 sect_offset sect_off
13789 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13790 if (!offset_in_cu_p (&cu->header, sect_off))
13791 {
13792 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13793 binding can be done only inside one CU. Such referenced DIE
13794 therefore cannot be even moved to DW_TAG_partial_unit. */
13795 complaint (_("DW_AT_call_parameter offset is not in CU for "
13796 "DW_TAG_call_site child DIE %s [in module %s]"),
13797 sect_offset_str (child_die->sect_off),
13798 objfile_name (objfile));
13799 continue;
13800 }
13801 parameter->u.param_cu_off
13802 = (cu_offset) (sect_off - cu->header.sect_off);
13803 }
13804 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13805 {
13806 complaint (_("No DW_FORM_block* DW_AT_location for "
13807 "DW_TAG_call_site child DIE %s [in module %s]"),
13808 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13809 continue;
13810 }
13811 else
13812 {
13813 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13814 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13815 if (parameter->u.dwarf_reg != -1)
13816 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13817 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13818 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13819 &parameter->u.fb_offset))
13820 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13821 else
13822 {
13823 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13824 "for DW_FORM_block* DW_AT_location is supported for "
13825 "DW_TAG_call_site child DIE %s "
13826 "[in module %s]"),
13827 sect_offset_str (child_die->sect_off),
13828 objfile_name (objfile));
13829 continue;
13830 }
13831 }
13832
13833 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13834 if (attr == NULL)
13835 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13836 if (attr == NULL || !attr->form_is_block ())
13837 {
13838 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13839 "DW_TAG_call_site child DIE %s [in module %s]"),
13840 sect_offset_str (child_die->sect_off),
13841 objfile_name (objfile));
13842 continue;
13843 }
13844 parameter->value = DW_BLOCK (attr)->data;
13845 parameter->value_size = DW_BLOCK (attr)->size;
13846
13847 /* Parameters are not pre-cleared by memset above. */
13848 parameter->data_value = NULL;
13849 parameter->data_value_size = 0;
13850 call_site->parameter_count++;
13851
13852 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13853 if (attr == NULL)
13854 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13855 if (attr != nullptr)
13856 {
13857 if (!attr->form_is_block ())
13858 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13859 "DW_TAG_call_site child DIE %s [in module %s]"),
13860 sect_offset_str (child_die->sect_off),
13861 objfile_name (objfile));
13862 else
13863 {
13864 parameter->data_value = DW_BLOCK (attr)->data;
13865 parameter->data_value_size = DW_BLOCK (attr)->size;
13866 }
13867 }
13868 }
13869 }
13870
13871 /* Helper function for read_variable. If DIE represents a virtual
13872 table, then return the type of the concrete object that is
13873 associated with the virtual table. Otherwise, return NULL. */
13874
13875 static struct type *
13876 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13877 {
13878 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13879 if (attr == NULL)
13880 return NULL;
13881
13882 /* Find the type DIE. */
13883 struct die_info *type_die = NULL;
13884 struct dwarf2_cu *type_cu = cu;
13885
13886 if (attr->form_is_ref ())
13887 type_die = follow_die_ref (die, attr, &type_cu);
13888 if (type_die == NULL)
13889 return NULL;
13890
13891 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13892 return NULL;
13893 return die_containing_type (type_die, type_cu);
13894 }
13895
13896 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13897
13898 static void
13899 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13900 {
13901 struct rust_vtable_symbol *storage = NULL;
13902
13903 if (cu->language == language_rust)
13904 {
13905 struct type *containing_type = rust_containing_type (die, cu);
13906
13907 if (containing_type != NULL)
13908 {
13909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13910
13911 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13912 initialize_objfile_symbol (storage);
13913 storage->concrete_type = containing_type;
13914 storage->subclass = SYMBOL_RUST_VTABLE;
13915 }
13916 }
13917
13918 struct symbol *res = new_symbol (die, NULL, cu, storage);
13919 struct attribute *abstract_origin
13920 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13921 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13922 if (res == NULL && loc && abstract_origin)
13923 {
13924 /* We have a variable without a name, but with a location and an abstract
13925 origin. This may be a concrete instance of an abstract variable
13926 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13927 later. */
13928 struct dwarf2_cu *origin_cu = cu;
13929 struct die_info *origin_die
13930 = follow_die_ref (die, abstract_origin, &origin_cu);
13931 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13932 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13933 }
13934 }
13935
13936 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13937 reading .debug_rnglists.
13938 Callback's type should be:
13939 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13940 Return true if the attributes are present and valid, otherwise,
13941 return false. */
13942
13943 template <typename Callback>
13944 static bool
13945 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13946 Callback &&callback)
13947 {
13948 struct dwarf2_per_objfile *dwarf2_per_objfile
13949 = cu->per_cu->dwarf2_per_objfile;
13950 struct objfile *objfile = dwarf2_per_objfile->objfile;
13951 bfd *obfd = objfile->obfd;
13952 /* Base address selection entry. */
13953 CORE_ADDR base;
13954 int found_base;
13955 const gdb_byte *buffer;
13956 CORE_ADDR baseaddr;
13957 bool overflow = false;
13958
13959 found_base = cu->base_known;
13960 base = cu->base_address;
13961
13962 dwarf2_per_objfile->rnglists.read (objfile);
13963 if (offset >= dwarf2_per_objfile->rnglists.size)
13964 {
13965 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13966 offset);
13967 return false;
13968 }
13969 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13970
13971 baseaddr = objfile->text_section_offset ();
13972
13973 while (1)
13974 {
13975 /* Initialize it due to a false compiler warning. */
13976 CORE_ADDR range_beginning = 0, range_end = 0;
13977 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13978 + dwarf2_per_objfile->rnglists.size);
13979 unsigned int bytes_read;
13980
13981 if (buffer == buf_end)
13982 {
13983 overflow = true;
13984 break;
13985 }
13986 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13987 switch (rlet)
13988 {
13989 case DW_RLE_end_of_list:
13990 break;
13991 case DW_RLE_base_address:
13992 if (buffer + cu->header.addr_size > buf_end)
13993 {
13994 overflow = true;
13995 break;
13996 }
13997 base = read_address (obfd, buffer, cu, &bytes_read);
13998 found_base = 1;
13999 buffer += bytes_read;
14000 break;
14001 case DW_RLE_start_length:
14002 if (buffer + cu->header.addr_size > buf_end)
14003 {
14004 overflow = true;
14005 break;
14006 }
14007 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14008 buffer += bytes_read;
14009 range_end = (range_beginning
14010 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14011 buffer += bytes_read;
14012 if (buffer > buf_end)
14013 {
14014 overflow = true;
14015 break;
14016 }
14017 break;
14018 case DW_RLE_offset_pair:
14019 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14020 buffer += bytes_read;
14021 if (buffer > buf_end)
14022 {
14023 overflow = true;
14024 break;
14025 }
14026 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14027 buffer += bytes_read;
14028 if (buffer > buf_end)
14029 {
14030 overflow = true;
14031 break;
14032 }
14033 break;
14034 case DW_RLE_start_end:
14035 if (buffer + 2 * cu->header.addr_size > buf_end)
14036 {
14037 overflow = true;
14038 break;
14039 }
14040 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14041 buffer += bytes_read;
14042 range_end = read_address (obfd, buffer, cu, &bytes_read);
14043 buffer += bytes_read;
14044 break;
14045 default:
14046 complaint (_("Invalid .debug_rnglists data (no base address)"));
14047 return false;
14048 }
14049 if (rlet == DW_RLE_end_of_list || overflow)
14050 break;
14051 if (rlet == DW_RLE_base_address)
14052 continue;
14053
14054 if (!found_base)
14055 {
14056 /* We have no valid base address for the ranges
14057 data. */
14058 complaint (_("Invalid .debug_rnglists data (no base address)"));
14059 return false;
14060 }
14061
14062 if (range_beginning > range_end)
14063 {
14064 /* Inverted range entries are invalid. */
14065 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14066 return false;
14067 }
14068
14069 /* Empty range entries have no effect. */
14070 if (range_beginning == range_end)
14071 continue;
14072
14073 range_beginning += base;
14074 range_end += base;
14075
14076 /* A not-uncommon case of bad debug info.
14077 Don't pollute the addrmap with bad data. */
14078 if (range_beginning + baseaddr == 0
14079 && !dwarf2_per_objfile->has_section_at_zero)
14080 {
14081 complaint (_(".debug_rnglists entry has start address of zero"
14082 " [in module %s]"), objfile_name (objfile));
14083 continue;
14084 }
14085
14086 callback (range_beginning, range_end);
14087 }
14088
14089 if (overflow)
14090 {
14091 complaint (_("Offset %d is not terminated "
14092 "for DW_AT_ranges attribute"),
14093 offset);
14094 return false;
14095 }
14096
14097 return true;
14098 }
14099
14100 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14101 Callback's type should be:
14102 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14103 Return 1 if the attributes are present and valid, otherwise, return 0. */
14104
14105 template <typename Callback>
14106 static int
14107 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14108 Callback &&callback)
14109 {
14110 struct dwarf2_per_objfile *dwarf2_per_objfile
14111 = cu->per_cu->dwarf2_per_objfile;
14112 struct objfile *objfile = dwarf2_per_objfile->objfile;
14113 struct comp_unit_head *cu_header = &cu->header;
14114 bfd *obfd = objfile->obfd;
14115 unsigned int addr_size = cu_header->addr_size;
14116 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14117 /* Base address selection entry. */
14118 CORE_ADDR base;
14119 int found_base;
14120 unsigned int dummy;
14121 const gdb_byte *buffer;
14122 CORE_ADDR baseaddr;
14123
14124 if (cu_header->version >= 5)
14125 return dwarf2_rnglists_process (offset, cu, callback);
14126
14127 found_base = cu->base_known;
14128 base = cu->base_address;
14129
14130 dwarf2_per_objfile->ranges.read (objfile);
14131 if (offset >= dwarf2_per_objfile->ranges.size)
14132 {
14133 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14134 offset);
14135 return 0;
14136 }
14137 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14138
14139 baseaddr = objfile->text_section_offset ();
14140
14141 while (1)
14142 {
14143 CORE_ADDR range_beginning, range_end;
14144
14145 range_beginning = read_address (obfd, buffer, cu, &dummy);
14146 buffer += addr_size;
14147 range_end = read_address (obfd, buffer, cu, &dummy);
14148 buffer += addr_size;
14149 offset += 2 * addr_size;
14150
14151 /* An end of list marker is a pair of zero addresses. */
14152 if (range_beginning == 0 && range_end == 0)
14153 /* Found the end of list entry. */
14154 break;
14155
14156 /* Each base address selection entry is a pair of 2 values.
14157 The first is the largest possible address, the second is
14158 the base address. Check for a base address here. */
14159 if ((range_beginning & mask) == mask)
14160 {
14161 /* If we found the largest possible address, then we already
14162 have the base address in range_end. */
14163 base = range_end;
14164 found_base = 1;
14165 continue;
14166 }
14167
14168 if (!found_base)
14169 {
14170 /* We have no valid base address for the ranges
14171 data. */
14172 complaint (_("Invalid .debug_ranges data (no base address)"));
14173 return 0;
14174 }
14175
14176 if (range_beginning > range_end)
14177 {
14178 /* Inverted range entries are invalid. */
14179 complaint (_("Invalid .debug_ranges data (inverted range)"));
14180 return 0;
14181 }
14182
14183 /* Empty range entries have no effect. */
14184 if (range_beginning == range_end)
14185 continue;
14186
14187 range_beginning += base;
14188 range_end += base;
14189
14190 /* A not-uncommon case of bad debug info.
14191 Don't pollute the addrmap with bad data. */
14192 if (range_beginning + baseaddr == 0
14193 && !dwarf2_per_objfile->has_section_at_zero)
14194 {
14195 complaint (_(".debug_ranges entry has start address of zero"
14196 " [in module %s]"), objfile_name (objfile));
14197 continue;
14198 }
14199
14200 callback (range_beginning, range_end);
14201 }
14202
14203 return 1;
14204 }
14205
14206 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14207 Return 1 if the attributes are present and valid, otherwise, return 0.
14208 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14209
14210 static int
14211 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14212 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14213 dwarf2_psymtab *ranges_pst)
14214 {
14215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14217 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14218 int low_set = 0;
14219 CORE_ADDR low = 0;
14220 CORE_ADDR high = 0;
14221 int retval;
14222
14223 retval = dwarf2_ranges_process (offset, cu,
14224 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14225 {
14226 if (ranges_pst != NULL)
14227 {
14228 CORE_ADDR lowpc;
14229 CORE_ADDR highpc;
14230
14231 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14232 range_beginning + baseaddr)
14233 - baseaddr);
14234 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14235 range_end + baseaddr)
14236 - baseaddr);
14237 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14238 lowpc, highpc - 1, ranges_pst);
14239 }
14240
14241 /* FIXME: This is recording everything as a low-high
14242 segment of consecutive addresses. We should have a
14243 data structure for discontiguous block ranges
14244 instead. */
14245 if (! low_set)
14246 {
14247 low = range_beginning;
14248 high = range_end;
14249 low_set = 1;
14250 }
14251 else
14252 {
14253 if (range_beginning < low)
14254 low = range_beginning;
14255 if (range_end > high)
14256 high = range_end;
14257 }
14258 });
14259 if (!retval)
14260 return 0;
14261
14262 if (! low_set)
14263 /* If the first entry is an end-of-list marker, the range
14264 describes an empty scope, i.e. no instructions. */
14265 return 0;
14266
14267 if (low_return)
14268 *low_return = low;
14269 if (high_return)
14270 *high_return = high;
14271 return 1;
14272 }
14273
14274 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14275 definition for the return value. *LOWPC and *HIGHPC are set iff
14276 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14277
14278 static enum pc_bounds_kind
14279 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14280 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14281 dwarf2_psymtab *pst)
14282 {
14283 struct dwarf2_per_objfile *dwarf2_per_objfile
14284 = cu->per_cu->dwarf2_per_objfile;
14285 struct attribute *attr;
14286 struct attribute *attr_high;
14287 CORE_ADDR low = 0;
14288 CORE_ADDR high = 0;
14289 enum pc_bounds_kind ret;
14290
14291 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14292 if (attr_high)
14293 {
14294 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14295 if (attr != nullptr)
14296 {
14297 low = attr->value_as_address ();
14298 high = attr_high->value_as_address ();
14299 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14300 high += low;
14301 }
14302 else
14303 /* Found high w/o low attribute. */
14304 return PC_BOUNDS_INVALID;
14305
14306 /* Found consecutive range of addresses. */
14307 ret = PC_BOUNDS_HIGH_LOW;
14308 }
14309 else
14310 {
14311 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14312 if (attr != NULL)
14313 {
14314 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14315 We take advantage of the fact that DW_AT_ranges does not appear
14316 in DW_TAG_compile_unit of DWO files. */
14317 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14318 unsigned int ranges_offset = (DW_UNSND (attr)
14319 + (need_ranges_base
14320 ? cu->ranges_base
14321 : 0));
14322
14323 /* Value of the DW_AT_ranges attribute is the offset in the
14324 .debug_ranges section. */
14325 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14326 return PC_BOUNDS_INVALID;
14327 /* Found discontinuous range of addresses. */
14328 ret = PC_BOUNDS_RANGES;
14329 }
14330 else
14331 return PC_BOUNDS_NOT_PRESENT;
14332 }
14333
14334 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14335 if (high <= low)
14336 return PC_BOUNDS_INVALID;
14337
14338 /* When using the GNU linker, .gnu.linkonce. sections are used to
14339 eliminate duplicate copies of functions and vtables and such.
14340 The linker will arbitrarily choose one and discard the others.
14341 The AT_*_pc values for such functions refer to local labels in
14342 these sections. If the section from that file was discarded, the
14343 labels are not in the output, so the relocs get a value of 0.
14344 If this is a discarded function, mark the pc bounds as invalid,
14345 so that GDB will ignore it. */
14346 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14347 return PC_BOUNDS_INVALID;
14348
14349 *lowpc = low;
14350 if (highpc)
14351 *highpc = high;
14352 return ret;
14353 }
14354
14355 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14356 its low and high PC addresses. Do nothing if these addresses could not
14357 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14358 and HIGHPC to the high address if greater than HIGHPC. */
14359
14360 static void
14361 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14362 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14363 struct dwarf2_cu *cu)
14364 {
14365 CORE_ADDR low, high;
14366 struct die_info *child = die->child;
14367
14368 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14369 {
14370 *lowpc = std::min (*lowpc, low);
14371 *highpc = std::max (*highpc, high);
14372 }
14373
14374 /* If the language does not allow nested subprograms (either inside
14375 subprograms or lexical blocks), we're done. */
14376 if (cu->language != language_ada)
14377 return;
14378
14379 /* Check all the children of the given DIE. If it contains nested
14380 subprograms, then check their pc bounds. Likewise, we need to
14381 check lexical blocks as well, as they may also contain subprogram
14382 definitions. */
14383 while (child && child->tag)
14384 {
14385 if (child->tag == DW_TAG_subprogram
14386 || child->tag == DW_TAG_lexical_block)
14387 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14388 child = sibling_die (child);
14389 }
14390 }
14391
14392 /* Get the low and high pc's represented by the scope DIE, and store
14393 them in *LOWPC and *HIGHPC. If the correct values can't be
14394 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14395
14396 static void
14397 get_scope_pc_bounds (struct die_info *die,
14398 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14399 struct dwarf2_cu *cu)
14400 {
14401 CORE_ADDR best_low = (CORE_ADDR) -1;
14402 CORE_ADDR best_high = (CORE_ADDR) 0;
14403 CORE_ADDR current_low, current_high;
14404
14405 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14406 >= PC_BOUNDS_RANGES)
14407 {
14408 best_low = current_low;
14409 best_high = current_high;
14410 }
14411 else
14412 {
14413 struct die_info *child = die->child;
14414
14415 while (child && child->tag)
14416 {
14417 switch (child->tag) {
14418 case DW_TAG_subprogram:
14419 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14420 break;
14421 case DW_TAG_namespace:
14422 case DW_TAG_module:
14423 /* FIXME: carlton/2004-01-16: Should we do this for
14424 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14425 that current GCC's always emit the DIEs corresponding
14426 to definitions of methods of classes as children of a
14427 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14428 the DIEs giving the declarations, which could be
14429 anywhere). But I don't see any reason why the
14430 standards says that they have to be there. */
14431 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14432
14433 if (current_low != ((CORE_ADDR) -1))
14434 {
14435 best_low = std::min (best_low, current_low);
14436 best_high = std::max (best_high, current_high);
14437 }
14438 break;
14439 default:
14440 /* Ignore. */
14441 break;
14442 }
14443
14444 child = sibling_die (child);
14445 }
14446 }
14447
14448 *lowpc = best_low;
14449 *highpc = best_high;
14450 }
14451
14452 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14453 in DIE. */
14454
14455 static void
14456 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14457 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14458 {
14459 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14461 struct attribute *attr;
14462 struct attribute *attr_high;
14463
14464 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14465 if (attr_high)
14466 {
14467 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14468 if (attr != nullptr)
14469 {
14470 CORE_ADDR low = attr->value_as_address ();
14471 CORE_ADDR high = attr_high->value_as_address ();
14472
14473 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14474 high += low;
14475
14476 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14477 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14478 cu->get_builder ()->record_block_range (block, low, high - 1);
14479 }
14480 }
14481
14482 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14483 if (attr != nullptr)
14484 {
14485 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14486 We take advantage of the fact that DW_AT_ranges does not appear
14487 in DW_TAG_compile_unit of DWO files. */
14488 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14489
14490 /* The value of the DW_AT_ranges attribute is the offset of the
14491 address range list in the .debug_ranges section. */
14492 unsigned long offset = (DW_UNSND (attr)
14493 + (need_ranges_base ? cu->ranges_base : 0));
14494
14495 std::vector<blockrange> blockvec;
14496 dwarf2_ranges_process (offset, cu,
14497 [&] (CORE_ADDR start, CORE_ADDR end)
14498 {
14499 start += baseaddr;
14500 end += baseaddr;
14501 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14502 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14503 cu->get_builder ()->record_block_range (block, start, end - 1);
14504 blockvec.emplace_back (start, end);
14505 });
14506
14507 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14508 }
14509 }
14510
14511 /* Check whether the producer field indicates either of GCC < 4.6, or the
14512 Intel C/C++ compiler, and cache the result in CU. */
14513
14514 static void
14515 check_producer (struct dwarf2_cu *cu)
14516 {
14517 int major, minor;
14518
14519 if (cu->producer == NULL)
14520 {
14521 /* For unknown compilers expect their behavior is DWARF version
14522 compliant.
14523
14524 GCC started to support .debug_types sections by -gdwarf-4 since
14525 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14526 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14527 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14528 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14529 }
14530 else if (producer_is_gcc (cu->producer, &major, &minor))
14531 {
14532 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14533 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14534 }
14535 else if (producer_is_icc (cu->producer, &major, &minor))
14536 {
14537 cu->producer_is_icc = true;
14538 cu->producer_is_icc_lt_14 = major < 14;
14539 }
14540 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14541 cu->producer_is_codewarrior = true;
14542 else
14543 {
14544 /* For other non-GCC compilers, expect their behavior is DWARF version
14545 compliant. */
14546 }
14547
14548 cu->checked_producer = true;
14549 }
14550
14551 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14552 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14553 during 4.6.0 experimental. */
14554
14555 static bool
14556 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14557 {
14558 if (!cu->checked_producer)
14559 check_producer (cu);
14560
14561 return cu->producer_is_gxx_lt_4_6;
14562 }
14563
14564
14565 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14566 with incorrect is_stmt attributes. */
14567
14568 static bool
14569 producer_is_codewarrior (struct dwarf2_cu *cu)
14570 {
14571 if (!cu->checked_producer)
14572 check_producer (cu);
14573
14574 return cu->producer_is_codewarrior;
14575 }
14576
14577 /* Return the default accessibility type if it is not overridden by
14578 DW_AT_accessibility. */
14579
14580 static enum dwarf_access_attribute
14581 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14582 {
14583 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14584 {
14585 /* The default DWARF 2 accessibility for members is public, the default
14586 accessibility for inheritance is private. */
14587
14588 if (die->tag != DW_TAG_inheritance)
14589 return DW_ACCESS_public;
14590 else
14591 return DW_ACCESS_private;
14592 }
14593 else
14594 {
14595 /* DWARF 3+ defines the default accessibility a different way. The same
14596 rules apply now for DW_TAG_inheritance as for the members and it only
14597 depends on the container kind. */
14598
14599 if (die->parent->tag == DW_TAG_class_type)
14600 return DW_ACCESS_private;
14601 else
14602 return DW_ACCESS_public;
14603 }
14604 }
14605
14606 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14607 offset. If the attribute was not found return 0, otherwise return
14608 1. If it was found but could not properly be handled, set *OFFSET
14609 to 0. */
14610
14611 static int
14612 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14613 LONGEST *offset)
14614 {
14615 struct attribute *attr;
14616
14617 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14618 if (attr != NULL)
14619 {
14620 *offset = 0;
14621
14622 /* Note that we do not check for a section offset first here.
14623 This is because DW_AT_data_member_location is new in DWARF 4,
14624 so if we see it, we can assume that a constant form is really
14625 a constant and not a section offset. */
14626 if (attr->form_is_constant ())
14627 *offset = dwarf2_get_attr_constant_value (attr, 0);
14628 else if (attr->form_is_section_offset ())
14629 dwarf2_complex_location_expr_complaint ();
14630 else if (attr->form_is_block ())
14631 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14632 else
14633 dwarf2_complex_location_expr_complaint ();
14634
14635 return 1;
14636 }
14637
14638 return 0;
14639 }
14640
14641 /* Add an aggregate field to the field list. */
14642
14643 static void
14644 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14645 struct dwarf2_cu *cu)
14646 {
14647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14649 struct nextfield *new_field;
14650 struct attribute *attr;
14651 struct field *fp;
14652 const char *fieldname = "";
14653
14654 if (die->tag == DW_TAG_inheritance)
14655 {
14656 fip->baseclasses.emplace_back ();
14657 new_field = &fip->baseclasses.back ();
14658 }
14659 else
14660 {
14661 fip->fields.emplace_back ();
14662 new_field = &fip->fields.back ();
14663 }
14664
14665 fip->nfields++;
14666
14667 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14668 if (attr != nullptr)
14669 new_field->accessibility = DW_UNSND (attr);
14670 else
14671 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14672 if (new_field->accessibility != DW_ACCESS_public)
14673 fip->non_public_fields = 1;
14674
14675 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14676 if (attr != nullptr)
14677 new_field->virtuality = DW_UNSND (attr);
14678 else
14679 new_field->virtuality = DW_VIRTUALITY_none;
14680
14681 fp = &new_field->field;
14682
14683 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14684 {
14685 LONGEST offset;
14686
14687 /* Data member other than a C++ static data member. */
14688
14689 /* Get type of field. */
14690 fp->type = die_type (die, cu);
14691
14692 SET_FIELD_BITPOS (*fp, 0);
14693
14694 /* Get bit size of field (zero if none). */
14695 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14696 if (attr != nullptr)
14697 {
14698 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14699 }
14700 else
14701 {
14702 FIELD_BITSIZE (*fp) = 0;
14703 }
14704
14705 /* Get bit offset of field. */
14706 if (handle_data_member_location (die, cu, &offset))
14707 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14708 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14709 if (attr != nullptr)
14710 {
14711 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14712 {
14713 /* For big endian bits, the DW_AT_bit_offset gives the
14714 additional bit offset from the MSB of the containing
14715 anonymous object to the MSB of the field. We don't
14716 have to do anything special since we don't need to
14717 know the size of the anonymous object. */
14718 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14719 }
14720 else
14721 {
14722 /* For little endian bits, compute the bit offset to the
14723 MSB of the anonymous object, subtract off the number of
14724 bits from the MSB of the field to the MSB of the
14725 object, and then subtract off the number of bits of
14726 the field itself. The result is the bit offset of
14727 the LSB of the field. */
14728 int anonymous_size;
14729 int bit_offset = DW_UNSND (attr);
14730
14731 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14732 if (attr != nullptr)
14733 {
14734 /* The size of the anonymous object containing
14735 the bit field is explicit, so use the
14736 indicated size (in bytes). */
14737 anonymous_size = DW_UNSND (attr);
14738 }
14739 else
14740 {
14741 /* The size of the anonymous object containing
14742 the bit field must be inferred from the type
14743 attribute of the data member containing the
14744 bit field. */
14745 anonymous_size = TYPE_LENGTH (fp->type);
14746 }
14747 SET_FIELD_BITPOS (*fp,
14748 (FIELD_BITPOS (*fp)
14749 + anonymous_size * bits_per_byte
14750 - bit_offset - FIELD_BITSIZE (*fp)));
14751 }
14752 }
14753 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14754 if (attr != NULL)
14755 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14756 + dwarf2_get_attr_constant_value (attr, 0)));
14757
14758 /* Get name of field. */
14759 fieldname = dwarf2_name (die, cu);
14760 if (fieldname == NULL)
14761 fieldname = "";
14762
14763 /* The name is already allocated along with this objfile, so we don't
14764 need to duplicate it for the type. */
14765 fp->name = fieldname;
14766
14767 /* Change accessibility for artificial fields (e.g. virtual table
14768 pointer or virtual base class pointer) to private. */
14769 if (dwarf2_attr (die, DW_AT_artificial, cu))
14770 {
14771 FIELD_ARTIFICIAL (*fp) = 1;
14772 new_field->accessibility = DW_ACCESS_private;
14773 fip->non_public_fields = 1;
14774 }
14775 }
14776 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14777 {
14778 /* C++ static member. */
14779
14780 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14781 is a declaration, but all versions of G++ as of this writing
14782 (so through at least 3.2.1) incorrectly generate
14783 DW_TAG_variable tags. */
14784
14785 const char *physname;
14786
14787 /* Get name of field. */
14788 fieldname = dwarf2_name (die, cu);
14789 if (fieldname == NULL)
14790 return;
14791
14792 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14793 if (attr
14794 /* Only create a symbol if this is an external value.
14795 new_symbol checks this and puts the value in the global symbol
14796 table, which we want. If it is not external, new_symbol
14797 will try to put the value in cu->list_in_scope which is wrong. */
14798 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14799 {
14800 /* A static const member, not much different than an enum as far as
14801 we're concerned, except that we can support more types. */
14802 new_symbol (die, NULL, cu);
14803 }
14804
14805 /* Get physical name. */
14806 physname = dwarf2_physname (fieldname, die, cu);
14807
14808 /* The name is already allocated along with this objfile, so we don't
14809 need to duplicate it for the type. */
14810 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14811 FIELD_TYPE (*fp) = die_type (die, cu);
14812 FIELD_NAME (*fp) = fieldname;
14813 }
14814 else if (die->tag == DW_TAG_inheritance)
14815 {
14816 LONGEST offset;
14817
14818 /* C++ base class field. */
14819 if (handle_data_member_location (die, cu, &offset))
14820 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14821 FIELD_BITSIZE (*fp) = 0;
14822 FIELD_TYPE (*fp) = die_type (die, cu);
14823 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14824 }
14825 else if (die->tag == DW_TAG_variant_part)
14826 {
14827 /* process_structure_scope will treat this DIE as a union. */
14828 process_structure_scope (die, cu);
14829
14830 /* The variant part is relative to the start of the enclosing
14831 structure. */
14832 SET_FIELD_BITPOS (*fp, 0);
14833 fp->type = get_die_type (die, cu);
14834 fp->artificial = 1;
14835 fp->name = "<<variant>>";
14836
14837 /* Normally a DW_TAG_variant_part won't have a size, but our
14838 representation requires one, so set it to the maximum of the
14839 child sizes, being sure to account for the offset at which
14840 each child is seen. */
14841 if (TYPE_LENGTH (fp->type) == 0)
14842 {
14843 unsigned max = 0;
14844 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14845 {
14846 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14847 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14848 if (len > max)
14849 max = len;
14850 }
14851 TYPE_LENGTH (fp->type) = max;
14852 }
14853 }
14854 else
14855 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14856 }
14857
14858 /* Can the type given by DIE define another type? */
14859
14860 static bool
14861 type_can_define_types (const struct die_info *die)
14862 {
14863 switch (die->tag)
14864 {
14865 case DW_TAG_typedef:
14866 case DW_TAG_class_type:
14867 case DW_TAG_structure_type:
14868 case DW_TAG_union_type:
14869 case DW_TAG_enumeration_type:
14870 return true;
14871
14872 default:
14873 return false;
14874 }
14875 }
14876
14877 /* Add a type definition defined in the scope of the FIP's class. */
14878
14879 static void
14880 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14881 struct dwarf2_cu *cu)
14882 {
14883 struct decl_field fp;
14884 memset (&fp, 0, sizeof (fp));
14885
14886 gdb_assert (type_can_define_types (die));
14887
14888 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14889 fp.name = dwarf2_name (die, cu);
14890 fp.type = read_type_die (die, cu);
14891
14892 /* Save accessibility. */
14893 enum dwarf_access_attribute accessibility;
14894 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14895 if (attr != NULL)
14896 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14897 else
14898 accessibility = dwarf2_default_access_attribute (die, cu);
14899 switch (accessibility)
14900 {
14901 case DW_ACCESS_public:
14902 /* The assumed value if neither private nor protected. */
14903 break;
14904 case DW_ACCESS_private:
14905 fp.is_private = 1;
14906 break;
14907 case DW_ACCESS_protected:
14908 fp.is_protected = 1;
14909 break;
14910 default:
14911 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14912 }
14913
14914 if (die->tag == DW_TAG_typedef)
14915 fip->typedef_field_list.push_back (fp);
14916 else
14917 fip->nested_types_list.push_back (fp);
14918 }
14919
14920 /* Create the vector of fields, and attach it to the type. */
14921
14922 static void
14923 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14924 struct dwarf2_cu *cu)
14925 {
14926 int nfields = fip->nfields;
14927
14928 /* Record the field count, allocate space for the array of fields,
14929 and create blank accessibility bitfields if necessary. */
14930 TYPE_NFIELDS (type) = nfields;
14931 TYPE_FIELDS (type) = (struct field *)
14932 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14933
14934 if (fip->non_public_fields && cu->language != language_ada)
14935 {
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14937
14938 TYPE_FIELD_PRIVATE_BITS (type) =
14939 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14940 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14941
14942 TYPE_FIELD_PROTECTED_BITS (type) =
14943 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14944 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14945
14946 TYPE_FIELD_IGNORE_BITS (type) =
14947 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14948 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14949 }
14950
14951 /* If the type has baseclasses, allocate and clear a bit vector for
14952 TYPE_FIELD_VIRTUAL_BITS. */
14953 if (!fip->baseclasses.empty () && cu->language != language_ada)
14954 {
14955 int num_bytes = B_BYTES (fip->baseclasses.size ());
14956 unsigned char *pointer;
14957
14958 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14959 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14960 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14961 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14962 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14963 }
14964
14965 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14966 {
14967 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14968
14969 for (int index = 0; index < nfields; ++index)
14970 {
14971 struct nextfield &field = fip->fields[index];
14972
14973 if (field.variant.is_discriminant)
14974 di->discriminant_index = index;
14975 else if (field.variant.default_branch)
14976 di->default_index = index;
14977 else
14978 di->discriminants[index] = field.variant.discriminant_value;
14979 }
14980 }
14981
14982 /* Copy the saved-up fields into the field vector. */
14983 for (int i = 0; i < nfields; ++i)
14984 {
14985 struct nextfield &field
14986 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14987 : fip->fields[i - fip->baseclasses.size ()]);
14988
14989 TYPE_FIELD (type, i) = field.field;
14990 switch (field.accessibility)
14991 {
14992 case DW_ACCESS_private:
14993 if (cu->language != language_ada)
14994 SET_TYPE_FIELD_PRIVATE (type, i);
14995 break;
14996
14997 case DW_ACCESS_protected:
14998 if (cu->language != language_ada)
14999 SET_TYPE_FIELD_PROTECTED (type, i);
15000 break;
15001
15002 case DW_ACCESS_public:
15003 break;
15004
15005 default:
15006 /* Unknown accessibility. Complain and treat it as public. */
15007 {
15008 complaint (_("unsupported accessibility %d"),
15009 field.accessibility);
15010 }
15011 break;
15012 }
15013 if (i < fip->baseclasses.size ())
15014 {
15015 switch (field.virtuality)
15016 {
15017 case DW_VIRTUALITY_virtual:
15018 case DW_VIRTUALITY_pure_virtual:
15019 if (cu->language == language_ada)
15020 error (_("unexpected virtuality in component of Ada type"));
15021 SET_TYPE_FIELD_VIRTUAL (type, i);
15022 break;
15023 }
15024 }
15025 }
15026 }
15027
15028 /* Return true if this member function is a constructor, false
15029 otherwise. */
15030
15031 static int
15032 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15033 {
15034 const char *fieldname;
15035 const char *type_name;
15036 int len;
15037
15038 if (die->parent == NULL)
15039 return 0;
15040
15041 if (die->parent->tag != DW_TAG_structure_type
15042 && die->parent->tag != DW_TAG_union_type
15043 && die->parent->tag != DW_TAG_class_type)
15044 return 0;
15045
15046 fieldname = dwarf2_name (die, cu);
15047 type_name = dwarf2_name (die->parent, cu);
15048 if (fieldname == NULL || type_name == NULL)
15049 return 0;
15050
15051 len = strlen (fieldname);
15052 return (strncmp (fieldname, type_name, len) == 0
15053 && (type_name[len] == '\0' || type_name[len] == '<'));
15054 }
15055
15056 /* Check if the given VALUE is a recognized enum
15057 dwarf_defaulted_attribute constant according to DWARF5 spec,
15058 Table 7.24. */
15059
15060 static bool
15061 is_valid_DW_AT_defaulted (ULONGEST value)
15062 {
15063 switch (value)
15064 {
15065 case DW_DEFAULTED_no:
15066 case DW_DEFAULTED_in_class:
15067 case DW_DEFAULTED_out_of_class:
15068 return true;
15069 }
15070
15071 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15072 return false;
15073 }
15074
15075 /* Add a member function to the proper fieldlist. */
15076
15077 static void
15078 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15079 struct type *type, struct dwarf2_cu *cu)
15080 {
15081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15082 struct attribute *attr;
15083 int i;
15084 struct fnfieldlist *flp = nullptr;
15085 struct fn_field *fnp;
15086 const char *fieldname;
15087 struct type *this_type;
15088 enum dwarf_access_attribute accessibility;
15089
15090 if (cu->language == language_ada)
15091 error (_("unexpected member function in Ada type"));
15092
15093 /* Get name of member function. */
15094 fieldname = dwarf2_name (die, cu);
15095 if (fieldname == NULL)
15096 return;
15097
15098 /* Look up member function name in fieldlist. */
15099 for (i = 0; i < fip->fnfieldlists.size (); i++)
15100 {
15101 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15102 {
15103 flp = &fip->fnfieldlists[i];
15104 break;
15105 }
15106 }
15107
15108 /* Create a new fnfieldlist if necessary. */
15109 if (flp == nullptr)
15110 {
15111 fip->fnfieldlists.emplace_back ();
15112 flp = &fip->fnfieldlists.back ();
15113 flp->name = fieldname;
15114 i = fip->fnfieldlists.size () - 1;
15115 }
15116
15117 /* Create a new member function field and add it to the vector of
15118 fnfieldlists. */
15119 flp->fnfields.emplace_back ();
15120 fnp = &flp->fnfields.back ();
15121
15122 /* Delay processing of the physname until later. */
15123 if (cu->language == language_cplus)
15124 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15125 die, cu);
15126 else
15127 {
15128 const char *physname = dwarf2_physname (fieldname, die, cu);
15129 fnp->physname = physname ? physname : "";
15130 }
15131
15132 fnp->type = alloc_type (objfile);
15133 this_type = read_type_die (die, cu);
15134 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15135 {
15136 int nparams = TYPE_NFIELDS (this_type);
15137
15138 /* TYPE is the domain of this method, and THIS_TYPE is the type
15139 of the method itself (TYPE_CODE_METHOD). */
15140 smash_to_method_type (fnp->type, type,
15141 TYPE_TARGET_TYPE (this_type),
15142 TYPE_FIELDS (this_type),
15143 TYPE_NFIELDS (this_type),
15144 TYPE_VARARGS (this_type));
15145
15146 /* Handle static member functions.
15147 Dwarf2 has no clean way to discern C++ static and non-static
15148 member functions. G++ helps GDB by marking the first
15149 parameter for non-static member functions (which is the this
15150 pointer) as artificial. We obtain this information from
15151 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15152 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15153 fnp->voffset = VOFFSET_STATIC;
15154 }
15155 else
15156 complaint (_("member function type missing for '%s'"),
15157 dwarf2_full_name (fieldname, die, cu));
15158
15159 /* Get fcontext from DW_AT_containing_type if present. */
15160 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15161 fnp->fcontext = die_containing_type (die, cu);
15162
15163 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15164 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15165
15166 /* Get accessibility. */
15167 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15168 if (attr != nullptr)
15169 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15170 else
15171 accessibility = dwarf2_default_access_attribute (die, cu);
15172 switch (accessibility)
15173 {
15174 case DW_ACCESS_private:
15175 fnp->is_private = 1;
15176 break;
15177 case DW_ACCESS_protected:
15178 fnp->is_protected = 1;
15179 break;
15180 }
15181
15182 /* Check for artificial methods. */
15183 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15184 if (attr && DW_UNSND (attr) != 0)
15185 fnp->is_artificial = 1;
15186
15187 /* Check for defaulted methods. */
15188 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15189 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15190 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15191
15192 /* Check for deleted methods. */
15193 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15194 if (attr != nullptr && DW_UNSND (attr) != 0)
15195 fnp->is_deleted = 1;
15196
15197 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15198
15199 /* Get index in virtual function table if it is a virtual member
15200 function. For older versions of GCC, this is an offset in the
15201 appropriate virtual table, as specified by DW_AT_containing_type.
15202 For everyone else, it is an expression to be evaluated relative
15203 to the object address. */
15204
15205 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15206 if (attr != nullptr)
15207 {
15208 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15209 {
15210 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15211 {
15212 /* Old-style GCC. */
15213 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15214 }
15215 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15216 || (DW_BLOCK (attr)->size > 1
15217 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15218 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15219 {
15220 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15221 if ((fnp->voffset % cu->header.addr_size) != 0)
15222 dwarf2_complex_location_expr_complaint ();
15223 else
15224 fnp->voffset /= cu->header.addr_size;
15225 fnp->voffset += 2;
15226 }
15227 else
15228 dwarf2_complex_location_expr_complaint ();
15229
15230 if (!fnp->fcontext)
15231 {
15232 /* If there is no `this' field and no DW_AT_containing_type,
15233 we cannot actually find a base class context for the
15234 vtable! */
15235 if (TYPE_NFIELDS (this_type) == 0
15236 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15237 {
15238 complaint (_("cannot determine context for virtual member "
15239 "function \"%s\" (offset %s)"),
15240 fieldname, sect_offset_str (die->sect_off));
15241 }
15242 else
15243 {
15244 fnp->fcontext
15245 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15246 }
15247 }
15248 }
15249 else if (attr->form_is_section_offset ())
15250 {
15251 dwarf2_complex_location_expr_complaint ();
15252 }
15253 else
15254 {
15255 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15256 fieldname);
15257 }
15258 }
15259 else
15260 {
15261 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15262 if (attr && DW_UNSND (attr))
15263 {
15264 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15265 complaint (_("Member function \"%s\" (offset %s) is virtual "
15266 "but the vtable offset is not specified"),
15267 fieldname, sect_offset_str (die->sect_off));
15268 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15269 TYPE_CPLUS_DYNAMIC (type) = 1;
15270 }
15271 }
15272 }
15273
15274 /* Create the vector of member function fields, and attach it to the type. */
15275
15276 static void
15277 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15278 struct dwarf2_cu *cu)
15279 {
15280 if (cu->language == language_ada)
15281 error (_("unexpected member functions in Ada type"));
15282
15283 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15284 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15285 TYPE_ALLOC (type,
15286 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15287
15288 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15289 {
15290 struct fnfieldlist &nf = fip->fnfieldlists[i];
15291 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15292
15293 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15294 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15295 fn_flp->fn_fields = (struct fn_field *)
15296 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15297
15298 for (int k = 0; k < nf.fnfields.size (); ++k)
15299 fn_flp->fn_fields[k] = nf.fnfields[k];
15300 }
15301
15302 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15303 }
15304
15305 /* Returns non-zero if NAME is the name of a vtable member in CU's
15306 language, zero otherwise. */
15307 static int
15308 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15309 {
15310 static const char vptr[] = "_vptr";
15311
15312 /* Look for the C++ form of the vtable. */
15313 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15314 return 1;
15315
15316 return 0;
15317 }
15318
15319 /* GCC outputs unnamed structures that are really pointers to member
15320 functions, with the ABI-specified layout. If TYPE describes
15321 such a structure, smash it into a member function type.
15322
15323 GCC shouldn't do this; it should just output pointer to member DIEs.
15324 This is GCC PR debug/28767. */
15325
15326 static void
15327 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15328 {
15329 struct type *pfn_type, *self_type, *new_type;
15330
15331 /* Check for a structure with no name and two children. */
15332 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15333 return;
15334
15335 /* Check for __pfn and __delta members. */
15336 if (TYPE_FIELD_NAME (type, 0) == NULL
15337 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15338 || TYPE_FIELD_NAME (type, 1) == NULL
15339 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15340 return;
15341
15342 /* Find the type of the method. */
15343 pfn_type = TYPE_FIELD_TYPE (type, 0);
15344 if (pfn_type == NULL
15345 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15346 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15347 return;
15348
15349 /* Look for the "this" argument. */
15350 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15351 if (TYPE_NFIELDS (pfn_type) == 0
15352 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15353 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15354 return;
15355
15356 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15357 new_type = alloc_type (objfile);
15358 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15359 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15360 TYPE_VARARGS (pfn_type));
15361 smash_to_methodptr_type (type, new_type);
15362 }
15363
15364 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15365 appropriate error checking and issuing complaints if there is a
15366 problem. */
15367
15368 static ULONGEST
15369 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15370 {
15371 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15372
15373 if (attr == nullptr)
15374 return 0;
15375
15376 if (!attr->form_is_constant ())
15377 {
15378 complaint (_("DW_AT_alignment must have constant form"
15379 " - DIE at %s [in module %s]"),
15380 sect_offset_str (die->sect_off),
15381 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15382 return 0;
15383 }
15384
15385 ULONGEST align;
15386 if (attr->form == DW_FORM_sdata)
15387 {
15388 LONGEST val = DW_SND (attr);
15389 if (val < 0)
15390 {
15391 complaint (_("DW_AT_alignment value must not be negative"
15392 " - DIE at %s [in module %s]"),
15393 sect_offset_str (die->sect_off),
15394 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15395 return 0;
15396 }
15397 align = val;
15398 }
15399 else
15400 align = DW_UNSND (attr);
15401
15402 if (align == 0)
15403 {
15404 complaint (_("DW_AT_alignment value must not be zero"
15405 " - DIE at %s [in module %s]"),
15406 sect_offset_str (die->sect_off),
15407 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15408 return 0;
15409 }
15410 if ((align & (align - 1)) != 0)
15411 {
15412 complaint (_("DW_AT_alignment value must be a power of 2"
15413 " - DIE at %s [in module %s]"),
15414 sect_offset_str (die->sect_off),
15415 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15416 return 0;
15417 }
15418
15419 return align;
15420 }
15421
15422 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15423 the alignment for TYPE. */
15424
15425 static void
15426 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15427 struct type *type)
15428 {
15429 if (!set_type_align (type, get_alignment (cu, die)))
15430 complaint (_("DW_AT_alignment value too large"
15431 " - DIE at %s [in module %s]"),
15432 sect_offset_str (die->sect_off),
15433 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15434 }
15435
15436 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15437 constant for a type, according to DWARF5 spec, Table 5.5. */
15438
15439 static bool
15440 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15441 {
15442 switch (value)
15443 {
15444 case DW_CC_normal:
15445 case DW_CC_pass_by_reference:
15446 case DW_CC_pass_by_value:
15447 return true;
15448
15449 default:
15450 complaint (_("unrecognized DW_AT_calling_convention value "
15451 "(%s) for a type"), pulongest (value));
15452 return false;
15453 }
15454 }
15455
15456 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15457 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15458 also according to GNU-specific values (see include/dwarf2.h). */
15459
15460 static bool
15461 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15462 {
15463 switch (value)
15464 {
15465 case DW_CC_normal:
15466 case DW_CC_program:
15467 case DW_CC_nocall:
15468 return true;
15469
15470 case DW_CC_GNU_renesas_sh:
15471 case DW_CC_GNU_borland_fastcall_i386:
15472 case DW_CC_GDB_IBM_OpenCL:
15473 return true;
15474
15475 default:
15476 complaint (_("unrecognized DW_AT_calling_convention value "
15477 "(%s) for a subroutine"), pulongest (value));
15478 return false;
15479 }
15480 }
15481
15482 /* Called when we find the DIE that starts a structure or union scope
15483 (definition) to create a type for the structure or union. Fill in
15484 the type's name and general properties; the members will not be
15485 processed until process_structure_scope. A symbol table entry for
15486 the type will also not be done until process_structure_scope (assuming
15487 the type has a name).
15488
15489 NOTE: we need to call these functions regardless of whether or not the
15490 DIE has a DW_AT_name attribute, since it might be an anonymous
15491 structure or union. This gets the type entered into our set of
15492 user defined types. */
15493
15494 static struct type *
15495 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15496 {
15497 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15498 struct type *type;
15499 struct attribute *attr;
15500 const char *name;
15501
15502 /* If the definition of this type lives in .debug_types, read that type.
15503 Don't follow DW_AT_specification though, that will take us back up
15504 the chain and we want to go down. */
15505 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15506 if (attr != nullptr)
15507 {
15508 type = get_DW_AT_signature_type (die, attr, cu);
15509
15510 /* The type's CU may not be the same as CU.
15511 Ensure TYPE is recorded with CU in die_type_hash. */
15512 return set_die_type (die, type, cu);
15513 }
15514
15515 type = alloc_type (objfile);
15516 INIT_CPLUS_SPECIFIC (type);
15517
15518 name = dwarf2_name (die, cu);
15519 if (name != NULL)
15520 {
15521 if (cu->language == language_cplus
15522 || cu->language == language_d
15523 || cu->language == language_rust)
15524 {
15525 const char *full_name = dwarf2_full_name (name, die, cu);
15526
15527 /* dwarf2_full_name might have already finished building the DIE's
15528 type. If so, there is no need to continue. */
15529 if (get_die_type (die, cu) != NULL)
15530 return get_die_type (die, cu);
15531
15532 TYPE_NAME (type) = full_name;
15533 }
15534 else
15535 {
15536 /* The name is already allocated along with this objfile, so
15537 we don't need to duplicate it for the type. */
15538 TYPE_NAME (type) = name;
15539 }
15540 }
15541
15542 if (die->tag == DW_TAG_structure_type)
15543 {
15544 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15545 }
15546 else if (die->tag == DW_TAG_union_type)
15547 {
15548 TYPE_CODE (type) = TYPE_CODE_UNION;
15549 }
15550 else if (die->tag == DW_TAG_variant_part)
15551 {
15552 TYPE_CODE (type) = TYPE_CODE_UNION;
15553 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15554 }
15555 else
15556 {
15557 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15558 }
15559
15560 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15561 TYPE_DECLARED_CLASS (type) = 1;
15562
15563 /* Store the calling convention in the type if it's available in
15564 the die. Otherwise the calling convention remains set to
15565 the default value DW_CC_normal. */
15566 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15567 if (attr != nullptr
15568 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15569 {
15570 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15571 TYPE_CPLUS_CALLING_CONVENTION (type)
15572 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15573 }
15574
15575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15576 if (attr != nullptr)
15577 {
15578 if (attr->form_is_constant ())
15579 TYPE_LENGTH (type) = DW_UNSND (attr);
15580 else
15581 {
15582 /* For the moment, dynamic type sizes are not supported
15583 by GDB's struct type. The actual size is determined
15584 on-demand when resolving the type of a given object,
15585 so set the type's length to zero for now. Otherwise,
15586 we record an expression as the length, and that expression
15587 could lead to a very large value, which could eventually
15588 lead to us trying to allocate that much memory when creating
15589 a value of that type. */
15590 TYPE_LENGTH (type) = 0;
15591 }
15592 }
15593 else
15594 {
15595 TYPE_LENGTH (type) = 0;
15596 }
15597
15598 maybe_set_alignment (cu, die, type);
15599
15600 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15601 {
15602 /* ICC<14 does not output the required DW_AT_declaration on
15603 incomplete types, but gives them a size of zero. */
15604 TYPE_STUB (type) = 1;
15605 }
15606 else
15607 TYPE_STUB_SUPPORTED (type) = 1;
15608
15609 if (die_is_declaration (die, cu))
15610 TYPE_STUB (type) = 1;
15611 else if (attr == NULL && die->child == NULL
15612 && producer_is_realview (cu->producer))
15613 /* RealView does not output the required DW_AT_declaration
15614 on incomplete types. */
15615 TYPE_STUB (type) = 1;
15616
15617 /* We need to add the type field to the die immediately so we don't
15618 infinitely recurse when dealing with pointers to the structure
15619 type within the structure itself. */
15620 set_die_type (die, type, cu);
15621
15622 /* set_die_type should be already done. */
15623 set_descriptive_type (type, die, cu);
15624
15625 return type;
15626 }
15627
15628 /* A helper for process_structure_scope that handles a single member
15629 DIE. */
15630
15631 static void
15632 handle_struct_member_die (struct die_info *child_die, struct type *type,
15633 struct field_info *fi,
15634 std::vector<struct symbol *> *template_args,
15635 struct dwarf2_cu *cu)
15636 {
15637 if (child_die->tag == DW_TAG_member
15638 || child_die->tag == DW_TAG_variable
15639 || child_die->tag == DW_TAG_variant_part)
15640 {
15641 /* NOTE: carlton/2002-11-05: A C++ static data member
15642 should be a DW_TAG_member that is a declaration, but
15643 all versions of G++ as of this writing (so through at
15644 least 3.2.1) incorrectly generate DW_TAG_variable
15645 tags for them instead. */
15646 dwarf2_add_field (fi, child_die, cu);
15647 }
15648 else if (child_die->tag == DW_TAG_subprogram)
15649 {
15650 /* Rust doesn't have member functions in the C++ sense.
15651 However, it does emit ordinary functions as children
15652 of a struct DIE. */
15653 if (cu->language == language_rust)
15654 read_func_scope (child_die, cu);
15655 else
15656 {
15657 /* C++ member function. */
15658 dwarf2_add_member_fn (fi, child_die, type, cu);
15659 }
15660 }
15661 else if (child_die->tag == DW_TAG_inheritance)
15662 {
15663 /* C++ base class field. */
15664 dwarf2_add_field (fi, child_die, cu);
15665 }
15666 else if (type_can_define_types (child_die))
15667 dwarf2_add_type_defn (fi, child_die, cu);
15668 else if (child_die->tag == DW_TAG_template_type_param
15669 || child_die->tag == DW_TAG_template_value_param)
15670 {
15671 struct symbol *arg = new_symbol (child_die, NULL, cu);
15672
15673 if (arg != NULL)
15674 template_args->push_back (arg);
15675 }
15676 else if (child_die->tag == DW_TAG_variant)
15677 {
15678 /* In a variant we want to get the discriminant and also add a
15679 field for our sole member child. */
15680 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15681
15682 for (die_info *variant_child = child_die->child;
15683 variant_child != NULL;
15684 variant_child = sibling_die (variant_child))
15685 {
15686 if (variant_child->tag == DW_TAG_member)
15687 {
15688 handle_struct_member_die (variant_child, type, fi,
15689 template_args, cu);
15690 /* Only handle the one. */
15691 break;
15692 }
15693 }
15694
15695 /* We don't handle this but we might as well report it if we see
15696 it. */
15697 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15698 complaint (_("DW_AT_discr_list is not supported yet"
15699 " - DIE at %s [in module %s]"),
15700 sect_offset_str (child_die->sect_off),
15701 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15702
15703 /* The first field was just added, so we can stash the
15704 discriminant there. */
15705 gdb_assert (!fi->fields.empty ());
15706 if (discr == NULL)
15707 fi->fields.back ().variant.default_branch = true;
15708 else
15709 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15710 }
15711 }
15712
15713 /* Finish creating a structure or union type, including filling in
15714 its members and creating a symbol for it. */
15715
15716 static void
15717 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15718 {
15719 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15720 struct die_info *child_die;
15721 struct type *type;
15722
15723 type = get_die_type (die, cu);
15724 if (type == NULL)
15725 type = read_structure_type (die, cu);
15726
15727 /* When reading a DW_TAG_variant_part, we need to notice when we
15728 read the discriminant member, so we can record it later in the
15729 discriminant_info. */
15730 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15731 sect_offset discr_offset {};
15732 bool has_template_parameters = false;
15733
15734 if (is_variant_part)
15735 {
15736 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15737 if (discr == NULL)
15738 {
15739 /* Maybe it's a univariant form, an extension we support.
15740 In this case arrange not to check the offset. */
15741 is_variant_part = false;
15742 }
15743 else if (discr->form_is_ref ())
15744 {
15745 struct dwarf2_cu *target_cu = cu;
15746 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15747
15748 discr_offset = target_die->sect_off;
15749 }
15750 else
15751 {
15752 complaint (_("DW_AT_discr does not have DIE reference form"
15753 " - DIE at %s [in module %s]"),
15754 sect_offset_str (die->sect_off),
15755 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15756 is_variant_part = false;
15757 }
15758 }
15759
15760 if (die->child != NULL && ! die_is_declaration (die, cu))
15761 {
15762 struct field_info fi;
15763 std::vector<struct symbol *> template_args;
15764
15765 child_die = die->child;
15766
15767 while (child_die && child_die->tag)
15768 {
15769 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15770
15771 if (is_variant_part && discr_offset == child_die->sect_off)
15772 fi.fields.back ().variant.is_discriminant = true;
15773
15774 child_die = sibling_die (child_die);
15775 }
15776
15777 /* Attach template arguments to type. */
15778 if (!template_args.empty ())
15779 {
15780 has_template_parameters = true;
15781 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15782 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15783 TYPE_TEMPLATE_ARGUMENTS (type)
15784 = XOBNEWVEC (&objfile->objfile_obstack,
15785 struct symbol *,
15786 TYPE_N_TEMPLATE_ARGUMENTS (type));
15787 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15788 template_args.data (),
15789 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15790 * sizeof (struct symbol *)));
15791 }
15792
15793 /* Attach fields and member functions to the type. */
15794 if (fi.nfields)
15795 dwarf2_attach_fields_to_type (&fi, type, cu);
15796 if (!fi.fnfieldlists.empty ())
15797 {
15798 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15799
15800 /* Get the type which refers to the base class (possibly this
15801 class itself) which contains the vtable pointer for the current
15802 class from the DW_AT_containing_type attribute. This use of
15803 DW_AT_containing_type is a GNU extension. */
15804
15805 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15806 {
15807 struct type *t = die_containing_type (die, cu);
15808
15809 set_type_vptr_basetype (type, t);
15810 if (type == t)
15811 {
15812 int i;
15813
15814 /* Our own class provides vtbl ptr. */
15815 for (i = TYPE_NFIELDS (t) - 1;
15816 i >= TYPE_N_BASECLASSES (t);
15817 --i)
15818 {
15819 const char *fieldname = TYPE_FIELD_NAME (t, i);
15820
15821 if (is_vtable_name (fieldname, cu))
15822 {
15823 set_type_vptr_fieldno (type, i);
15824 break;
15825 }
15826 }
15827
15828 /* Complain if virtual function table field not found. */
15829 if (i < TYPE_N_BASECLASSES (t))
15830 complaint (_("virtual function table pointer "
15831 "not found when defining class '%s'"),
15832 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15833 }
15834 else
15835 {
15836 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15837 }
15838 }
15839 else if (cu->producer
15840 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15841 {
15842 /* The IBM XLC compiler does not provide direct indication
15843 of the containing type, but the vtable pointer is
15844 always named __vfp. */
15845
15846 int i;
15847
15848 for (i = TYPE_NFIELDS (type) - 1;
15849 i >= TYPE_N_BASECLASSES (type);
15850 --i)
15851 {
15852 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15853 {
15854 set_type_vptr_fieldno (type, i);
15855 set_type_vptr_basetype (type, type);
15856 break;
15857 }
15858 }
15859 }
15860 }
15861
15862 /* Copy fi.typedef_field_list linked list elements content into the
15863 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15864 if (!fi.typedef_field_list.empty ())
15865 {
15866 int count = fi.typedef_field_list.size ();
15867
15868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15869 TYPE_TYPEDEF_FIELD_ARRAY (type)
15870 = ((struct decl_field *)
15871 TYPE_ALLOC (type,
15872 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15873 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15874
15875 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15876 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15877 }
15878
15879 /* Copy fi.nested_types_list linked list elements content into the
15880 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15881 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15882 {
15883 int count = fi.nested_types_list.size ();
15884
15885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15886 TYPE_NESTED_TYPES_ARRAY (type)
15887 = ((struct decl_field *)
15888 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15889 TYPE_NESTED_TYPES_COUNT (type) = count;
15890
15891 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15892 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15893 }
15894 }
15895
15896 quirk_gcc_member_function_pointer (type, objfile);
15897 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15898 cu->rust_unions.push_back (type);
15899
15900 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15901 snapshots) has been known to create a die giving a declaration
15902 for a class that has, as a child, a die giving a definition for a
15903 nested class. So we have to process our children even if the
15904 current die is a declaration. Normally, of course, a declaration
15905 won't have any children at all. */
15906
15907 child_die = die->child;
15908
15909 while (child_die != NULL && child_die->tag)
15910 {
15911 if (child_die->tag == DW_TAG_member
15912 || child_die->tag == DW_TAG_variable
15913 || child_die->tag == DW_TAG_inheritance
15914 || child_die->tag == DW_TAG_template_value_param
15915 || child_die->tag == DW_TAG_template_type_param)
15916 {
15917 /* Do nothing. */
15918 }
15919 else
15920 process_die (child_die, cu);
15921
15922 child_die = sibling_die (child_die);
15923 }
15924
15925 /* Do not consider external references. According to the DWARF standard,
15926 these DIEs are identified by the fact that they have no byte_size
15927 attribute, and a declaration attribute. */
15928 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15929 || !die_is_declaration (die, cu))
15930 {
15931 struct symbol *sym = new_symbol (die, type, cu);
15932
15933 if (has_template_parameters)
15934 {
15935 struct symtab *symtab;
15936 if (sym != nullptr)
15937 symtab = symbol_symtab (sym);
15938 else if (cu->line_header != nullptr)
15939 {
15940 /* Any related symtab will do. */
15941 symtab
15942 = cu->line_header->file_names ()[0].symtab;
15943 }
15944 else
15945 {
15946 symtab = nullptr;
15947 complaint (_("could not find suitable "
15948 "symtab for template parameter"
15949 " - DIE at %s [in module %s]"),
15950 sect_offset_str (die->sect_off),
15951 objfile_name (objfile));
15952 }
15953
15954 if (symtab != nullptr)
15955 {
15956 /* Make sure that the symtab is set on the new symbols.
15957 Even though they don't appear in this symtab directly,
15958 other parts of gdb assume that symbols do, and this is
15959 reasonably true. */
15960 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15961 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15962 }
15963 }
15964 }
15965 }
15966
15967 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15968 update TYPE using some information only available in DIE's children. */
15969
15970 static void
15971 update_enumeration_type_from_children (struct die_info *die,
15972 struct type *type,
15973 struct dwarf2_cu *cu)
15974 {
15975 struct die_info *child_die;
15976 int unsigned_enum = 1;
15977 int flag_enum = 1;
15978 ULONGEST mask = 0;
15979
15980 auto_obstack obstack;
15981
15982 for (child_die = die->child;
15983 child_die != NULL && child_die->tag;
15984 child_die = sibling_die (child_die))
15985 {
15986 struct attribute *attr;
15987 LONGEST value;
15988 const gdb_byte *bytes;
15989 struct dwarf2_locexpr_baton *baton;
15990 const char *name;
15991
15992 if (child_die->tag != DW_TAG_enumerator)
15993 continue;
15994
15995 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15996 if (attr == NULL)
15997 continue;
15998
15999 name = dwarf2_name (child_die, cu);
16000 if (name == NULL)
16001 name = "<anonymous enumerator>";
16002
16003 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16004 &value, &bytes, &baton);
16005 if (value < 0)
16006 {
16007 unsigned_enum = 0;
16008 flag_enum = 0;
16009 }
16010 else if ((mask & value) != 0)
16011 flag_enum = 0;
16012 else
16013 mask |= value;
16014
16015 /* If we already know that the enum type is neither unsigned, nor
16016 a flag type, no need to look at the rest of the enumerates. */
16017 if (!unsigned_enum && !flag_enum)
16018 break;
16019 }
16020
16021 if (unsigned_enum)
16022 TYPE_UNSIGNED (type) = 1;
16023 if (flag_enum)
16024 TYPE_FLAG_ENUM (type) = 1;
16025 }
16026
16027 /* Given a DW_AT_enumeration_type die, set its type. We do not
16028 complete the type's fields yet, or create any symbols. */
16029
16030 static struct type *
16031 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16032 {
16033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16034 struct type *type;
16035 struct attribute *attr;
16036 const char *name;
16037
16038 /* If the definition of this type lives in .debug_types, read that type.
16039 Don't follow DW_AT_specification though, that will take us back up
16040 the chain and we want to go down. */
16041 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16042 if (attr != nullptr)
16043 {
16044 type = get_DW_AT_signature_type (die, attr, cu);
16045
16046 /* The type's CU may not be the same as CU.
16047 Ensure TYPE is recorded with CU in die_type_hash. */
16048 return set_die_type (die, type, cu);
16049 }
16050
16051 type = alloc_type (objfile);
16052
16053 TYPE_CODE (type) = TYPE_CODE_ENUM;
16054 name = dwarf2_full_name (NULL, die, cu);
16055 if (name != NULL)
16056 TYPE_NAME (type) = name;
16057
16058 attr = dwarf2_attr (die, DW_AT_type, cu);
16059 if (attr != NULL)
16060 {
16061 struct type *underlying_type = die_type (die, cu);
16062
16063 TYPE_TARGET_TYPE (type) = underlying_type;
16064 }
16065
16066 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16067 if (attr != nullptr)
16068 {
16069 TYPE_LENGTH (type) = DW_UNSND (attr);
16070 }
16071 else
16072 {
16073 TYPE_LENGTH (type) = 0;
16074 }
16075
16076 maybe_set_alignment (cu, die, type);
16077
16078 /* The enumeration DIE can be incomplete. In Ada, any type can be
16079 declared as private in the package spec, and then defined only
16080 inside the package body. Such types are known as Taft Amendment
16081 Types. When another package uses such a type, an incomplete DIE
16082 may be generated by the compiler. */
16083 if (die_is_declaration (die, cu))
16084 TYPE_STUB (type) = 1;
16085
16086 /* Finish the creation of this type by using the enum's children.
16087 We must call this even when the underlying type has been provided
16088 so that we can determine if we're looking at a "flag" enum. */
16089 update_enumeration_type_from_children (die, type, cu);
16090
16091 /* If this type has an underlying type that is not a stub, then we
16092 may use its attributes. We always use the "unsigned" attribute
16093 in this situation, because ordinarily we guess whether the type
16094 is unsigned -- but the guess can be wrong and the underlying type
16095 can tell us the reality. However, we defer to a local size
16096 attribute if one exists, because this lets the compiler override
16097 the underlying type if needed. */
16098 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16099 {
16100 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16101 if (TYPE_LENGTH (type) == 0)
16102 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16103 if (TYPE_RAW_ALIGN (type) == 0
16104 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16105 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16106 }
16107
16108 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16109
16110 return set_die_type (die, type, cu);
16111 }
16112
16113 /* Given a pointer to a die which begins an enumeration, process all
16114 the dies that define the members of the enumeration, and create the
16115 symbol for the enumeration type.
16116
16117 NOTE: We reverse the order of the element list. */
16118
16119 static void
16120 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16121 {
16122 struct type *this_type;
16123
16124 this_type = get_die_type (die, cu);
16125 if (this_type == NULL)
16126 this_type = read_enumeration_type (die, cu);
16127
16128 if (die->child != NULL)
16129 {
16130 struct die_info *child_die;
16131 struct symbol *sym;
16132 std::vector<struct field> fields;
16133 const char *name;
16134
16135 child_die = die->child;
16136 while (child_die && child_die->tag)
16137 {
16138 if (child_die->tag != DW_TAG_enumerator)
16139 {
16140 process_die (child_die, cu);
16141 }
16142 else
16143 {
16144 name = dwarf2_name (child_die, cu);
16145 if (name)
16146 {
16147 sym = new_symbol (child_die, this_type, cu);
16148
16149 fields.emplace_back ();
16150 struct field &field = fields.back ();
16151
16152 FIELD_NAME (field) = sym->linkage_name ();
16153 FIELD_TYPE (field) = NULL;
16154 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16155 FIELD_BITSIZE (field) = 0;
16156 }
16157 }
16158
16159 child_die = sibling_die (child_die);
16160 }
16161
16162 if (!fields.empty ())
16163 {
16164 TYPE_NFIELDS (this_type) = fields.size ();
16165 TYPE_FIELDS (this_type) = (struct field *)
16166 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16167 memcpy (TYPE_FIELDS (this_type), fields.data (),
16168 sizeof (struct field) * fields.size ());
16169 }
16170 }
16171
16172 /* If we are reading an enum from a .debug_types unit, and the enum
16173 is a declaration, and the enum is not the signatured type in the
16174 unit, then we do not want to add a symbol for it. Adding a
16175 symbol would in some cases obscure the true definition of the
16176 enum, giving users an incomplete type when the definition is
16177 actually available. Note that we do not want to do this for all
16178 enums which are just declarations, because C++0x allows forward
16179 enum declarations. */
16180 if (cu->per_cu->is_debug_types
16181 && die_is_declaration (die, cu))
16182 {
16183 struct signatured_type *sig_type;
16184
16185 sig_type = (struct signatured_type *) cu->per_cu;
16186 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16187 if (sig_type->type_offset_in_section != die->sect_off)
16188 return;
16189 }
16190
16191 new_symbol (die, this_type, cu);
16192 }
16193
16194 /* Extract all information from a DW_TAG_array_type DIE and put it in
16195 the DIE's type field. For now, this only handles one dimensional
16196 arrays. */
16197
16198 static struct type *
16199 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16200 {
16201 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16202 struct die_info *child_die;
16203 struct type *type;
16204 struct type *element_type, *range_type, *index_type;
16205 struct attribute *attr;
16206 const char *name;
16207 struct dynamic_prop *byte_stride_prop = NULL;
16208 unsigned int bit_stride = 0;
16209
16210 element_type = die_type (die, cu);
16211
16212 /* The die_type call above may have already set the type for this DIE. */
16213 type = get_die_type (die, cu);
16214 if (type)
16215 return type;
16216
16217 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16218 if (attr != NULL)
16219 {
16220 int stride_ok;
16221 struct type *prop_type
16222 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16223
16224 byte_stride_prop
16225 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16226 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16227 prop_type);
16228 if (!stride_ok)
16229 {
16230 complaint (_("unable to read array DW_AT_byte_stride "
16231 " - DIE at %s [in module %s]"),
16232 sect_offset_str (die->sect_off),
16233 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16234 /* Ignore this attribute. We will likely not be able to print
16235 arrays of this type correctly, but there is little we can do
16236 to help if we cannot read the attribute's value. */
16237 byte_stride_prop = NULL;
16238 }
16239 }
16240
16241 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16242 if (attr != NULL)
16243 bit_stride = DW_UNSND (attr);
16244
16245 /* Irix 6.2 native cc creates array types without children for
16246 arrays with unspecified length. */
16247 if (die->child == NULL)
16248 {
16249 index_type = objfile_type (objfile)->builtin_int;
16250 range_type = create_static_range_type (NULL, index_type, 0, -1);
16251 type = create_array_type_with_stride (NULL, element_type, range_type,
16252 byte_stride_prop, bit_stride);
16253 return set_die_type (die, type, cu);
16254 }
16255
16256 std::vector<struct type *> range_types;
16257 child_die = die->child;
16258 while (child_die && child_die->tag)
16259 {
16260 if (child_die->tag == DW_TAG_subrange_type)
16261 {
16262 struct type *child_type = read_type_die (child_die, cu);
16263
16264 if (child_type != NULL)
16265 {
16266 /* The range type was succesfully read. Save it for the
16267 array type creation. */
16268 range_types.push_back (child_type);
16269 }
16270 }
16271 child_die = sibling_die (child_die);
16272 }
16273
16274 /* Dwarf2 dimensions are output from left to right, create the
16275 necessary array types in backwards order. */
16276
16277 type = element_type;
16278
16279 if (read_array_order (die, cu) == DW_ORD_col_major)
16280 {
16281 int i = 0;
16282
16283 while (i < range_types.size ())
16284 type = create_array_type_with_stride (NULL, type, range_types[i++],
16285 byte_stride_prop, bit_stride);
16286 }
16287 else
16288 {
16289 size_t ndim = range_types.size ();
16290 while (ndim-- > 0)
16291 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16292 byte_stride_prop, bit_stride);
16293 }
16294
16295 /* Understand Dwarf2 support for vector types (like they occur on
16296 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16297 array type. This is not part of the Dwarf2/3 standard yet, but a
16298 custom vendor extension. The main difference between a regular
16299 array and the vector variant is that vectors are passed by value
16300 to functions. */
16301 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16302 if (attr != nullptr)
16303 make_vector_type (type);
16304
16305 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16306 implementation may choose to implement triple vectors using this
16307 attribute. */
16308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16309 if (attr != nullptr)
16310 {
16311 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16312 TYPE_LENGTH (type) = DW_UNSND (attr);
16313 else
16314 complaint (_("DW_AT_byte_size for array type smaller "
16315 "than the total size of elements"));
16316 }
16317
16318 name = dwarf2_name (die, cu);
16319 if (name)
16320 TYPE_NAME (type) = name;
16321
16322 maybe_set_alignment (cu, die, type);
16323
16324 /* Install the type in the die. */
16325 set_die_type (die, type, cu);
16326
16327 /* set_die_type should be already done. */
16328 set_descriptive_type (type, die, cu);
16329
16330 return type;
16331 }
16332
16333 static enum dwarf_array_dim_ordering
16334 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16335 {
16336 struct attribute *attr;
16337
16338 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16339
16340 if (attr != nullptr)
16341 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16342
16343 /* GNU F77 is a special case, as at 08/2004 array type info is the
16344 opposite order to the dwarf2 specification, but data is still
16345 laid out as per normal fortran.
16346
16347 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16348 version checking. */
16349
16350 if (cu->language == language_fortran
16351 && cu->producer && strstr (cu->producer, "GNU F77"))
16352 {
16353 return DW_ORD_row_major;
16354 }
16355
16356 switch (cu->language_defn->la_array_ordering)
16357 {
16358 case array_column_major:
16359 return DW_ORD_col_major;
16360 case array_row_major:
16361 default:
16362 return DW_ORD_row_major;
16363 };
16364 }
16365
16366 /* Extract all information from a DW_TAG_set_type DIE and put it in
16367 the DIE's type field. */
16368
16369 static struct type *
16370 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16371 {
16372 struct type *domain_type, *set_type;
16373 struct attribute *attr;
16374
16375 domain_type = die_type (die, cu);
16376
16377 /* The die_type call above may have already set the type for this DIE. */
16378 set_type = get_die_type (die, cu);
16379 if (set_type)
16380 return set_type;
16381
16382 set_type = create_set_type (NULL, domain_type);
16383
16384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16385 if (attr != nullptr)
16386 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16387
16388 maybe_set_alignment (cu, die, set_type);
16389
16390 return set_die_type (die, set_type, cu);
16391 }
16392
16393 /* A helper for read_common_block that creates a locexpr baton.
16394 SYM is the symbol which we are marking as computed.
16395 COMMON_DIE is the DIE for the common block.
16396 COMMON_LOC is the location expression attribute for the common
16397 block itself.
16398 MEMBER_LOC is the location expression attribute for the particular
16399 member of the common block that we are processing.
16400 CU is the CU from which the above come. */
16401
16402 static void
16403 mark_common_block_symbol_computed (struct symbol *sym,
16404 struct die_info *common_die,
16405 struct attribute *common_loc,
16406 struct attribute *member_loc,
16407 struct dwarf2_cu *cu)
16408 {
16409 struct dwarf2_per_objfile *dwarf2_per_objfile
16410 = cu->per_cu->dwarf2_per_objfile;
16411 struct objfile *objfile = dwarf2_per_objfile->objfile;
16412 struct dwarf2_locexpr_baton *baton;
16413 gdb_byte *ptr;
16414 unsigned int cu_off;
16415 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16416 LONGEST offset = 0;
16417
16418 gdb_assert (common_loc && member_loc);
16419 gdb_assert (common_loc->form_is_block ());
16420 gdb_assert (member_loc->form_is_block ()
16421 || member_loc->form_is_constant ());
16422
16423 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16424 baton->per_cu = cu->per_cu;
16425 gdb_assert (baton->per_cu);
16426
16427 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16428
16429 if (member_loc->form_is_constant ())
16430 {
16431 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16432 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16433 }
16434 else
16435 baton->size += DW_BLOCK (member_loc)->size;
16436
16437 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16438 baton->data = ptr;
16439
16440 *ptr++ = DW_OP_call4;
16441 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16442 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16443 ptr += 4;
16444
16445 if (member_loc->form_is_constant ())
16446 {
16447 *ptr++ = DW_OP_addr;
16448 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16449 ptr += cu->header.addr_size;
16450 }
16451 else
16452 {
16453 /* We have to copy the data here, because DW_OP_call4 will only
16454 use a DW_AT_location attribute. */
16455 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16456 ptr += DW_BLOCK (member_loc)->size;
16457 }
16458
16459 *ptr++ = DW_OP_plus;
16460 gdb_assert (ptr - baton->data == baton->size);
16461
16462 SYMBOL_LOCATION_BATON (sym) = baton;
16463 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16464 }
16465
16466 /* Create appropriate locally-scoped variables for all the
16467 DW_TAG_common_block entries. Also create a struct common_block
16468 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16469 is used to separate the common blocks name namespace from regular
16470 variable names. */
16471
16472 static void
16473 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16474 {
16475 struct attribute *attr;
16476
16477 attr = dwarf2_attr (die, DW_AT_location, cu);
16478 if (attr != nullptr)
16479 {
16480 /* Support the .debug_loc offsets. */
16481 if (attr->form_is_block ())
16482 {
16483 /* Ok. */
16484 }
16485 else if (attr->form_is_section_offset ())
16486 {
16487 dwarf2_complex_location_expr_complaint ();
16488 attr = NULL;
16489 }
16490 else
16491 {
16492 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16493 "common block member");
16494 attr = NULL;
16495 }
16496 }
16497
16498 if (die->child != NULL)
16499 {
16500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16501 struct die_info *child_die;
16502 size_t n_entries = 0, size;
16503 struct common_block *common_block;
16504 struct symbol *sym;
16505
16506 for (child_die = die->child;
16507 child_die && child_die->tag;
16508 child_die = sibling_die (child_die))
16509 ++n_entries;
16510
16511 size = (sizeof (struct common_block)
16512 + (n_entries - 1) * sizeof (struct symbol *));
16513 common_block
16514 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16515 size);
16516 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16517 common_block->n_entries = 0;
16518
16519 for (child_die = die->child;
16520 child_die && child_die->tag;
16521 child_die = sibling_die (child_die))
16522 {
16523 /* Create the symbol in the DW_TAG_common_block block in the current
16524 symbol scope. */
16525 sym = new_symbol (child_die, NULL, cu);
16526 if (sym != NULL)
16527 {
16528 struct attribute *member_loc;
16529
16530 common_block->contents[common_block->n_entries++] = sym;
16531
16532 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16533 cu);
16534 if (member_loc)
16535 {
16536 /* GDB has handled this for a long time, but it is
16537 not specified by DWARF. It seems to have been
16538 emitted by gfortran at least as recently as:
16539 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16540 complaint (_("Variable in common block has "
16541 "DW_AT_data_member_location "
16542 "- DIE at %s [in module %s]"),
16543 sect_offset_str (child_die->sect_off),
16544 objfile_name (objfile));
16545
16546 if (member_loc->form_is_section_offset ())
16547 dwarf2_complex_location_expr_complaint ();
16548 else if (member_loc->form_is_constant ()
16549 || member_loc->form_is_block ())
16550 {
16551 if (attr != nullptr)
16552 mark_common_block_symbol_computed (sym, die, attr,
16553 member_loc, cu);
16554 }
16555 else
16556 dwarf2_complex_location_expr_complaint ();
16557 }
16558 }
16559 }
16560
16561 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16562 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16563 }
16564 }
16565
16566 /* Create a type for a C++ namespace. */
16567
16568 static struct type *
16569 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16570 {
16571 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16572 const char *previous_prefix, *name;
16573 int is_anonymous;
16574 struct type *type;
16575
16576 /* For extensions, reuse the type of the original namespace. */
16577 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16578 {
16579 struct die_info *ext_die;
16580 struct dwarf2_cu *ext_cu = cu;
16581
16582 ext_die = dwarf2_extension (die, &ext_cu);
16583 type = read_type_die (ext_die, ext_cu);
16584
16585 /* EXT_CU may not be the same as CU.
16586 Ensure TYPE is recorded with CU in die_type_hash. */
16587 return set_die_type (die, type, cu);
16588 }
16589
16590 name = namespace_name (die, &is_anonymous, cu);
16591
16592 /* Now build the name of the current namespace. */
16593
16594 previous_prefix = determine_prefix (die, cu);
16595 if (previous_prefix[0] != '\0')
16596 name = typename_concat (&objfile->objfile_obstack,
16597 previous_prefix, name, 0, cu);
16598
16599 /* Create the type. */
16600 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16601
16602 return set_die_type (die, type, cu);
16603 }
16604
16605 /* Read a namespace scope. */
16606
16607 static void
16608 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16609 {
16610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16611 int is_anonymous;
16612
16613 /* Add a symbol associated to this if we haven't seen the namespace
16614 before. Also, add a using directive if it's an anonymous
16615 namespace. */
16616
16617 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16618 {
16619 struct type *type;
16620
16621 type = read_type_die (die, cu);
16622 new_symbol (die, type, cu);
16623
16624 namespace_name (die, &is_anonymous, cu);
16625 if (is_anonymous)
16626 {
16627 const char *previous_prefix = determine_prefix (die, cu);
16628
16629 std::vector<const char *> excludes;
16630 add_using_directive (using_directives (cu),
16631 previous_prefix, TYPE_NAME (type), NULL,
16632 NULL, excludes, 0, &objfile->objfile_obstack);
16633 }
16634 }
16635
16636 if (die->child != NULL)
16637 {
16638 struct die_info *child_die = die->child;
16639
16640 while (child_die && child_die->tag)
16641 {
16642 process_die (child_die, cu);
16643 child_die = sibling_die (child_die);
16644 }
16645 }
16646 }
16647
16648 /* Read a Fortran module as type. This DIE can be only a declaration used for
16649 imported module. Still we need that type as local Fortran "use ... only"
16650 declaration imports depend on the created type in determine_prefix. */
16651
16652 static struct type *
16653 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16654 {
16655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16656 const char *module_name;
16657 struct type *type;
16658
16659 module_name = dwarf2_name (die, cu);
16660 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16661
16662 return set_die_type (die, type, cu);
16663 }
16664
16665 /* Read a Fortran module. */
16666
16667 static void
16668 read_module (struct die_info *die, struct dwarf2_cu *cu)
16669 {
16670 struct die_info *child_die = die->child;
16671 struct type *type;
16672
16673 type = read_type_die (die, cu);
16674 new_symbol (die, type, cu);
16675
16676 while (child_die && child_die->tag)
16677 {
16678 process_die (child_die, cu);
16679 child_die = sibling_die (child_die);
16680 }
16681 }
16682
16683 /* Return the name of the namespace represented by DIE. Set
16684 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16685 namespace. */
16686
16687 static const char *
16688 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16689 {
16690 struct die_info *current_die;
16691 const char *name = NULL;
16692
16693 /* Loop through the extensions until we find a name. */
16694
16695 for (current_die = die;
16696 current_die != NULL;
16697 current_die = dwarf2_extension (die, &cu))
16698 {
16699 /* We don't use dwarf2_name here so that we can detect the absence
16700 of a name -> anonymous namespace. */
16701 name = dwarf2_string_attr (die, DW_AT_name, cu);
16702
16703 if (name != NULL)
16704 break;
16705 }
16706
16707 /* Is it an anonymous namespace? */
16708
16709 *is_anonymous = (name == NULL);
16710 if (*is_anonymous)
16711 name = CP_ANONYMOUS_NAMESPACE_STR;
16712
16713 return name;
16714 }
16715
16716 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16717 the user defined type vector. */
16718
16719 static struct type *
16720 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16721 {
16722 struct gdbarch *gdbarch
16723 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16724 struct comp_unit_head *cu_header = &cu->header;
16725 struct type *type;
16726 struct attribute *attr_byte_size;
16727 struct attribute *attr_address_class;
16728 int byte_size, addr_class;
16729 struct type *target_type;
16730
16731 target_type = die_type (die, cu);
16732
16733 /* The die_type call above may have already set the type for this DIE. */
16734 type = get_die_type (die, cu);
16735 if (type)
16736 return type;
16737
16738 type = lookup_pointer_type (target_type);
16739
16740 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16741 if (attr_byte_size)
16742 byte_size = DW_UNSND (attr_byte_size);
16743 else
16744 byte_size = cu_header->addr_size;
16745
16746 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16747 if (attr_address_class)
16748 addr_class = DW_UNSND (attr_address_class);
16749 else
16750 addr_class = DW_ADDR_none;
16751
16752 ULONGEST alignment = get_alignment (cu, die);
16753
16754 /* If the pointer size, alignment, or address class is different
16755 than the default, create a type variant marked as such and set
16756 the length accordingly. */
16757 if (TYPE_LENGTH (type) != byte_size
16758 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16759 && alignment != TYPE_RAW_ALIGN (type))
16760 || addr_class != DW_ADDR_none)
16761 {
16762 if (gdbarch_address_class_type_flags_p (gdbarch))
16763 {
16764 int type_flags;
16765
16766 type_flags = gdbarch_address_class_type_flags
16767 (gdbarch, byte_size, addr_class);
16768 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16769 == 0);
16770 type = make_type_with_address_space (type, type_flags);
16771 }
16772 else if (TYPE_LENGTH (type) != byte_size)
16773 {
16774 complaint (_("invalid pointer size %d"), byte_size);
16775 }
16776 else if (TYPE_RAW_ALIGN (type) != alignment)
16777 {
16778 complaint (_("Invalid DW_AT_alignment"
16779 " - DIE at %s [in module %s]"),
16780 sect_offset_str (die->sect_off),
16781 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16782 }
16783 else
16784 {
16785 /* Should we also complain about unhandled address classes? */
16786 }
16787 }
16788
16789 TYPE_LENGTH (type) = byte_size;
16790 set_type_align (type, alignment);
16791 return set_die_type (die, type, cu);
16792 }
16793
16794 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16795 the user defined type vector. */
16796
16797 static struct type *
16798 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16799 {
16800 struct type *type;
16801 struct type *to_type;
16802 struct type *domain;
16803
16804 to_type = die_type (die, cu);
16805 domain = die_containing_type (die, cu);
16806
16807 /* The calls above may have already set the type for this DIE. */
16808 type = get_die_type (die, cu);
16809 if (type)
16810 return type;
16811
16812 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16813 type = lookup_methodptr_type (to_type);
16814 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16815 {
16816 struct type *new_type
16817 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16818
16819 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16820 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16821 TYPE_VARARGS (to_type));
16822 type = lookup_methodptr_type (new_type);
16823 }
16824 else
16825 type = lookup_memberptr_type (to_type, domain);
16826
16827 return set_die_type (die, type, cu);
16828 }
16829
16830 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16831 the user defined type vector. */
16832
16833 static struct type *
16834 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16835 enum type_code refcode)
16836 {
16837 struct comp_unit_head *cu_header = &cu->header;
16838 struct type *type, *target_type;
16839 struct attribute *attr;
16840
16841 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16842
16843 target_type = die_type (die, cu);
16844
16845 /* The die_type call above may have already set the type for this DIE. */
16846 type = get_die_type (die, cu);
16847 if (type)
16848 return type;
16849
16850 type = lookup_reference_type (target_type, refcode);
16851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16852 if (attr != nullptr)
16853 {
16854 TYPE_LENGTH (type) = DW_UNSND (attr);
16855 }
16856 else
16857 {
16858 TYPE_LENGTH (type) = cu_header->addr_size;
16859 }
16860 maybe_set_alignment (cu, die, type);
16861 return set_die_type (die, type, cu);
16862 }
16863
16864 /* Add the given cv-qualifiers to the element type of the array. GCC
16865 outputs DWARF type qualifiers that apply to an array, not the
16866 element type. But GDB relies on the array element type to carry
16867 the cv-qualifiers. This mimics section 6.7.3 of the C99
16868 specification. */
16869
16870 static struct type *
16871 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16872 struct type *base_type, int cnst, int voltl)
16873 {
16874 struct type *el_type, *inner_array;
16875
16876 base_type = copy_type (base_type);
16877 inner_array = base_type;
16878
16879 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16880 {
16881 TYPE_TARGET_TYPE (inner_array) =
16882 copy_type (TYPE_TARGET_TYPE (inner_array));
16883 inner_array = TYPE_TARGET_TYPE (inner_array);
16884 }
16885
16886 el_type = TYPE_TARGET_TYPE (inner_array);
16887 cnst |= TYPE_CONST (el_type);
16888 voltl |= TYPE_VOLATILE (el_type);
16889 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16890
16891 return set_die_type (die, base_type, cu);
16892 }
16893
16894 static struct type *
16895 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16896 {
16897 struct type *base_type, *cv_type;
16898
16899 base_type = die_type (die, cu);
16900
16901 /* The die_type call above may have already set the type for this DIE. */
16902 cv_type = get_die_type (die, cu);
16903 if (cv_type)
16904 return cv_type;
16905
16906 /* In case the const qualifier is applied to an array type, the element type
16907 is so qualified, not the array type (section 6.7.3 of C99). */
16908 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16909 return add_array_cv_type (die, cu, base_type, 1, 0);
16910
16911 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16912 return set_die_type (die, cv_type, cu);
16913 }
16914
16915 static struct type *
16916 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16917 {
16918 struct type *base_type, *cv_type;
16919
16920 base_type = die_type (die, cu);
16921
16922 /* The die_type call above may have already set the type for this DIE. */
16923 cv_type = get_die_type (die, cu);
16924 if (cv_type)
16925 return cv_type;
16926
16927 /* In case the volatile qualifier is applied to an array type, the
16928 element type is so qualified, not the array type (section 6.7.3
16929 of C99). */
16930 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16931 return add_array_cv_type (die, cu, base_type, 0, 1);
16932
16933 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16934 return set_die_type (die, cv_type, cu);
16935 }
16936
16937 /* Handle DW_TAG_restrict_type. */
16938
16939 static struct type *
16940 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16941 {
16942 struct type *base_type, *cv_type;
16943
16944 base_type = die_type (die, cu);
16945
16946 /* The die_type call above may have already set the type for this DIE. */
16947 cv_type = get_die_type (die, cu);
16948 if (cv_type)
16949 return cv_type;
16950
16951 cv_type = make_restrict_type (base_type);
16952 return set_die_type (die, cv_type, cu);
16953 }
16954
16955 /* Handle DW_TAG_atomic_type. */
16956
16957 static struct type *
16958 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16959 {
16960 struct type *base_type, *cv_type;
16961
16962 base_type = die_type (die, cu);
16963
16964 /* The die_type call above may have already set the type for this DIE. */
16965 cv_type = get_die_type (die, cu);
16966 if (cv_type)
16967 return cv_type;
16968
16969 cv_type = make_atomic_type (base_type);
16970 return set_die_type (die, cv_type, cu);
16971 }
16972
16973 /* Extract all information from a DW_TAG_string_type DIE and add to
16974 the user defined type vector. It isn't really a user defined type,
16975 but it behaves like one, with other DIE's using an AT_user_def_type
16976 attribute to reference it. */
16977
16978 static struct type *
16979 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16980 {
16981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16982 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16983 struct type *type, *range_type, *index_type, *char_type;
16984 struct attribute *attr;
16985 struct dynamic_prop prop;
16986 bool length_is_constant = true;
16987 LONGEST length;
16988
16989 /* There are a couple of places where bit sizes might be made use of
16990 when parsing a DW_TAG_string_type, however, no producer that we know
16991 of make use of these. Handling bit sizes that are a multiple of the
16992 byte size is easy enough, but what about other bit sizes? Lets deal
16993 with that problem when we have to. Warn about these attributes being
16994 unsupported, then parse the type and ignore them like we always
16995 have. */
16996 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16997 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16998 {
16999 static bool warning_printed = false;
17000 if (!warning_printed)
17001 {
17002 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17003 "currently supported on DW_TAG_string_type."));
17004 warning_printed = true;
17005 }
17006 }
17007
17008 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17009 if (attr != nullptr && !attr->form_is_constant ())
17010 {
17011 /* The string length describes the location at which the length of
17012 the string can be found. The size of the length field can be
17013 specified with one of the attributes below. */
17014 struct type *prop_type;
17015 struct attribute *len
17016 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17017 if (len == nullptr)
17018 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17019 if (len != nullptr && len->form_is_constant ())
17020 {
17021 /* Pass 0 as the default as we know this attribute is constant
17022 and the default value will not be returned. */
17023 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17024 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17025 }
17026 else
17027 {
17028 /* If the size is not specified then we assume it is the size of
17029 an address on this target. */
17030 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17031 }
17032
17033 /* Convert the attribute into a dynamic property. */
17034 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17035 length = 1;
17036 else
17037 length_is_constant = false;
17038 }
17039 else if (attr != nullptr)
17040 {
17041 /* This DW_AT_string_length just contains the length with no
17042 indirection. There's no need to create a dynamic property in this
17043 case. Pass 0 for the default value as we know it will not be
17044 returned in this case. */
17045 length = dwarf2_get_attr_constant_value (attr, 0);
17046 }
17047 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17048 {
17049 /* We don't currently support non-constant byte sizes for strings. */
17050 length = dwarf2_get_attr_constant_value (attr, 1);
17051 }
17052 else
17053 {
17054 /* Use 1 as a fallback length if we have nothing else. */
17055 length = 1;
17056 }
17057
17058 index_type = objfile_type (objfile)->builtin_int;
17059 if (length_is_constant)
17060 range_type = create_static_range_type (NULL, index_type, 1, length);
17061 else
17062 {
17063 struct dynamic_prop low_bound;
17064
17065 low_bound.kind = PROP_CONST;
17066 low_bound.data.const_val = 1;
17067 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17068 }
17069 char_type = language_string_char_type (cu->language_defn, gdbarch);
17070 type = create_string_type (NULL, char_type, range_type);
17071
17072 return set_die_type (die, type, cu);
17073 }
17074
17075 /* Assuming that DIE corresponds to a function, returns nonzero
17076 if the function is prototyped. */
17077
17078 static int
17079 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17080 {
17081 struct attribute *attr;
17082
17083 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17084 if (attr && (DW_UNSND (attr) != 0))
17085 return 1;
17086
17087 /* The DWARF standard implies that the DW_AT_prototyped attribute
17088 is only meaningful for C, but the concept also extends to other
17089 languages that allow unprototyped functions (Eg: Objective C).
17090 For all other languages, assume that functions are always
17091 prototyped. */
17092 if (cu->language != language_c
17093 && cu->language != language_objc
17094 && cu->language != language_opencl)
17095 return 1;
17096
17097 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17098 prototyped and unprototyped functions; default to prototyped,
17099 since that is more common in modern code (and RealView warns
17100 about unprototyped functions). */
17101 if (producer_is_realview (cu->producer))
17102 return 1;
17103
17104 return 0;
17105 }
17106
17107 /* Handle DIES due to C code like:
17108
17109 struct foo
17110 {
17111 int (*funcp)(int a, long l);
17112 int b;
17113 };
17114
17115 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17116
17117 static struct type *
17118 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17119 {
17120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17121 struct type *type; /* Type that this function returns. */
17122 struct type *ftype; /* Function that returns above type. */
17123 struct attribute *attr;
17124
17125 type = die_type (die, cu);
17126
17127 /* The die_type call above may have already set the type for this DIE. */
17128 ftype = get_die_type (die, cu);
17129 if (ftype)
17130 return ftype;
17131
17132 ftype = lookup_function_type (type);
17133
17134 if (prototyped_function_p (die, cu))
17135 TYPE_PROTOTYPED (ftype) = 1;
17136
17137 /* Store the calling convention in the type if it's available in
17138 the subroutine die. Otherwise set the calling convention to
17139 the default value DW_CC_normal. */
17140 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17141 if (attr != nullptr
17142 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17143 TYPE_CALLING_CONVENTION (ftype)
17144 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17145 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17146 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17147 else
17148 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17149
17150 /* Record whether the function returns normally to its caller or not
17151 if the DWARF producer set that information. */
17152 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17153 if (attr && (DW_UNSND (attr) != 0))
17154 TYPE_NO_RETURN (ftype) = 1;
17155
17156 /* We need to add the subroutine type to the die immediately so
17157 we don't infinitely recurse when dealing with parameters
17158 declared as the same subroutine type. */
17159 set_die_type (die, ftype, cu);
17160
17161 if (die->child != NULL)
17162 {
17163 struct type *void_type = objfile_type (objfile)->builtin_void;
17164 struct die_info *child_die;
17165 int nparams, iparams;
17166
17167 /* Count the number of parameters.
17168 FIXME: GDB currently ignores vararg functions, but knows about
17169 vararg member functions. */
17170 nparams = 0;
17171 child_die = die->child;
17172 while (child_die && child_die->tag)
17173 {
17174 if (child_die->tag == DW_TAG_formal_parameter)
17175 nparams++;
17176 else if (child_die->tag == DW_TAG_unspecified_parameters)
17177 TYPE_VARARGS (ftype) = 1;
17178 child_die = sibling_die (child_die);
17179 }
17180
17181 /* Allocate storage for parameters and fill them in. */
17182 TYPE_NFIELDS (ftype) = nparams;
17183 TYPE_FIELDS (ftype) = (struct field *)
17184 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17185
17186 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17187 even if we error out during the parameters reading below. */
17188 for (iparams = 0; iparams < nparams; iparams++)
17189 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17190
17191 iparams = 0;
17192 child_die = die->child;
17193 while (child_die && child_die->tag)
17194 {
17195 if (child_die->tag == DW_TAG_formal_parameter)
17196 {
17197 struct type *arg_type;
17198
17199 /* DWARF version 2 has no clean way to discern C++
17200 static and non-static member functions. G++ helps
17201 GDB by marking the first parameter for non-static
17202 member functions (which is the this pointer) as
17203 artificial. We pass this information to
17204 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17205
17206 DWARF version 3 added DW_AT_object_pointer, which GCC
17207 4.5 does not yet generate. */
17208 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17209 if (attr != nullptr)
17210 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17211 else
17212 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17213 arg_type = die_type (child_die, cu);
17214
17215 /* RealView does not mark THIS as const, which the testsuite
17216 expects. GCC marks THIS as const in method definitions,
17217 but not in the class specifications (GCC PR 43053). */
17218 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17219 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17220 {
17221 int is_this = 0;
17222 struct dwarf2_cu *arg_cu = cu;
17223 const char *name = dwarf2_name (child_die, cu);
17224
17225 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17226 if (attr != nullptr)
17227 {
17228 /* If the compiler emits this, use it. */
17229 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17230 is_this = 1;
17231 }
17232 else if (name && strcmp (name, "this") == 0)
17233 /* Function definitions will have the argument names. */
17234 is_this = 1;
17235 else if (name == NULL && iparams == 0)
17236 /* Declarations may not have the names, so like
17237 elsewhere in GDB, assume an artificial first
17238 argument is "this". */
17239 is_this = 1;
17240
17241 if (is_this)
17242 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17243 arg_type, 0);
17244 }
17245
17246 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17247 iparams++;
17248 }
17249 child_die = sibling_die (child_die);
17250 }
17251 }
17252
17253 return ftype;
17254 }
17255
17256 static struct type *
17257 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17258 {
17259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17260 const char *name = NULL;
17261 struct type *this_type, *target_type;
17262
17263 name = dwarf2_full_name (NULL, die, cu);
17264 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17265 TYPE_TARGET_STUB (this_type) = 1;
17266 set_die_type (die, this_type, cu);
17267 target_type = die_type (die, cu);
17268 if (target_type != this_type)
17269 TYPE_TARGET_TYPE (this_type) = target_type;
17270 else
17271 {
17272 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17273 spec and cause infinite loops in GDB. */
17274 complaint (_("Self-referential DW_TAG_typedef "
17275 "- DIE at %s [in module %s]"),
17276 sect_offset_str (die->sect_off), objfile_name (objfile));
17277 TYPE_TARGET_TYPE (this_type) = NULL;
17278 }
17279 return this_type;
17280 }
17281
17282 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17283 (which may be different from NAME) to the architecture back-end to allow
17284 it to guess the correct format if necessary. */
17285
17286 static struct type *
17287 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17288 const char *name_hint, enum bfd_endian byte_order)
17289 {
17290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17291 const struct floatformat **format;
17292 struct type *type;
17293
17294 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17295 if (format)
17296 type = init_float_type (objfile, bits, name, format, byte_order);
17297 else
17298 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17299
17300 return type;
17301 }
17302
17303 /* Allocate an integer type of size BITS and name NAME. */
17304
17305 static struct type *
17306 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17307 int bits, int unsigned_p, const char *name)
17308 {
17309 struct type *type;
17310
17311 /* Versions of Intel's C Compiler generate an integer type called "void"
17312 instead of using DW_TAG_unspecified_type. This has been seen on
17313 at least versions 14, 17, and 18. */
17314 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17315 && strcmp (name, "void") == 0)
17316 type = objfile_type (objfile)->builtin_void;
17317 else
17318 type = init_integer_type (objfile, bits, unsigned_p, name);
17319
17320 return type;
17321 }
17322
17323 /* Initialise and return a floating point type of size BITS suitable for
17324 use as a component of a complex number. The NAME_HINT is passed through
17325 when initialising the floating point type and is the name of the complex
17326 type.
17327
17328 As DWARF doesn't currently provide an explicit name for the components
17329 of a complex number, but it can be helpful to have these components
17330 named, we try to select a suitable name based on the size of the
17331 component. */
17332 static struct type *
17333 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17334 struct objfile *objfile,
17335 int bits, const char *name_hint,
17336 enum bfd_endian byte_order)
17337 {
17338 gdbarch *gdbarch = get_objfile_arch (objfile);
17339 struct type *tt = nullptr;
17340
17341 /* Try to find a suitable floating point builtin type of size BITS.
17342 We're going to use the name of this type as the name for the complex
17343 target type that we are about to create. */
17344 switch (cu->language)
17345 {
17346 case language_fortran:
17347 switch (bits)
17348 {
17349 case 32:
17350 tt = builtin_f_type (gdbarch)->builtin_real;
17351 break;
17352 case 64:
17353 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17354 break;
17355 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17356 case 128:
17357 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17358 break;
17359 }
17360 break;
17361 default:
17362 switch (bits)
17363 {
17364 case 32:
17365 tt = builtin_type (gdbarch)->builtin_float;
17366 break;
17367 case 64:
17368 tt = builtin_type (gdbarch)->builtin_double;
17369 break;
17370 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17371 case 128:
17372 tt = builtin_type (gdbarch)->builtin_long_double;
17373 break;
17374 }
17375 break;
17376 }
17377
17378 /* If the type we found doesn't match the size we were looking for, then
17379 pretend we didn't find a type at all, the complex target type we
17380 create will then be nameless. */
17381 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17382 tt = nullptr;
17383
17384 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17385 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17386 }
17387
17388 /* Find a representation of a given base type and install
17389 it in the TYPE field of the die. */
17390
17391 static struct type *
17392 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17393 {
17394 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17395 struct type *type;
17396 struct attribute *attr;
17397 int encoding = 0, bits = 0;
17398 const char *name;
17399 gdbarch *arch;
17400
17401 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17402 if (attr != nullptr)
17403 encoding = DW_UNSND (attr);
17404 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17405 if (attr != nullptr)
17406 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17407 name = dwarf2_name (die, cu);
17408 if (!name)
17409 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17410
17411 arch = get_objfile_arch (objfile);
17412 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17413
17414 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17415 if (attr)
17416 {
17417 int endianity = DW_UNSND (attr);
17418
17419 switch (endianity)
17420 {
17421 case DW_END_big:
17422 byte_order = BFD_ENDIAN_BIG;
17423 break;
17424 case DW_END_little:
17425 byte_order = BFD_ENDIAN_LITTLE;
17426 break;
17427 default:
17428 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17429 break;
17430 }
17431 }
17432
17433 switch (encoding)
17434 {
17435 case DW_ATE_address:
17436 /* Turn DW_ATE_address into a void * pointer. */
17437 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17438 type = init_pointer_type (objfile, bits, name, type);
17439 break;
17440 case DW_ATE_boolean:
17441 type = init_boolean_type (objfile, bits, 1, name);
17442 break;
17443 case DW_ATE_complex_float:
17444 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17445 byte_order);
17446 type = init_complex_type (objfile, name, type);
17447 break;
17448 case DW_ATE_decimal_float:
17449 type = init_decfloat_type (objfile, bits, name);
17450 break;
17451 case DW_ATE_float:
17452 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17453 break;
17454 case DW_ATE_signed:
17455 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17456 break;
17457 case DW_ATE_unsigned:
17458 if (cu->language == language_fortran
17459 && name
17460 && startswith (name, "character("))
17461 type = init_character_type (objfile, bits, 1, name);
17462 else
17463 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17464 break;
17465 case DW_ATE_signed_char:
17466 if (cu->language == language_ada || cu->language == language_m2
17467 || cu->language == language_pascal
17468 || cu->language == language_fortran)
17469 type = init_character_type (objfile, bits, 0, name);
17470 else
17471 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17472 break;
17473 case DW_ATE_unsigned_char:
17474 if (cu->language == language_ada || cu->language == language_m2
17475 || cu->language == language_pascal
17476 || cu->language == language_fortran
17477 || cu->language == language_rust)
17478 type = init_character_type (objfile, bits, 1, name);
17479 else
17480 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17481 break;
17482 case DW_ATE_UTF:
17483 {
17484 if (bits == 16)
17485 type = builtin_type (arch)->builtin_char16;
17486 else if (bits == 32)
17487 type = builtin_type (arch)->builtin_char32;
17488 else
17489 {
17490 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17491 bits);
17492 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17493 }
17494 return set_die_type (die, type, cu);
17495 }
17496 break;
17497
17498 default:
17499 complaint (_("unsupported DW_AT_encoding: '%s'"),
17500 dwarf_type_encoding_name (encoding));
17501 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17502 break;
17503 }
17504
17505 if (name && strcmp (name, "char") == 0)
17506 TYPE_NOSIGN (type) = 1;
17507
17508 maybe_set_alignment (cu, die, type);
17509
17510 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17511
17512 return set_die_type (die, type, cu);
17513 }
17514
17515 /* Parse dwarf attribute if it's a block, reference or constant and put the
17516 resulting value of the attribute into struct bound_prop.
17517 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17518
17519 static int
17520 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17521 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17522 struct type *default_type)
17523 {
17524 struct dwarf2_property_baton *baton;
17525 struct obstack *obstack
17526 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17527
17528 gdb_assert (default_type != NULL);
17529
17530 if (attr == NULL || prop == NULL)
17531 return 0;
17532
17533 if (attr->form_is_block ())
17534 {
17535 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17536 baton->property_type = default_type;
17537 baton->locexpr.per_cu = cu->per_cu;
17538 baton->locexpr.size = DW_BLOCK (attr)->size;
17539 baton->locexpr.data = DW_BLOCK (attr)->data;
17540 switch (attr->name)
17541 {
17542 case DW_AT_string_length:
17543 baton->locexpr.is_reference = true;
17544 break;
17545 default:
17546 baton->locexpr.is_reference = false;
17547 break;
17548 }
17549 prop->data.baton = baton;
17550 prop->kind = PROP_LOCEXPR;
17551 gdb_assert (prop->data.baton != NULL);
17552 }
17553 else if (attr->form_is_ref ())
17554 {
17555 struct dwarf2_cu *target_cu = cu;
17556 struct die_info *target_die;
17557 struct attribute *target_attr;
17558
17559 target_die = follow_die_ref (die, attr, &target_cu);
17560 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17561 if (target_attr == NULL)
17562 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17563 target_cu);
17564 if (target_attr == NULL)
17565 return 0;
17566
17567 switch (target_attr->name)
17568 {
17569 case DW_AT_location:
17570 if (target_attr->form_is_section_offset ())
17571 {
17572 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17573 baton->property_type = die_type (target_die, target_cu);
17574 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17575 prop->data.baton = baton;
17576 prop->kind = PROP_LOCLIST;
17577 gdb_assert (prop->data.baton != NULL);
17578 }
17579 else if (target_attr->form_is_block ())
17580 {
17581 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17582 baton->property_type = die_type (target_die, target_cu);
17583 baton->locexpr.per_cu = cu->per_cu;
17584 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17585 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17586 baton->locexpr.is_reference = true;
17587 prop->data.baton = baton;
17588 prop->kind = PROP_LOCEXPR;
17589 gdb_assert (prop->data.baton != NULL);
17590 }
17591 else
17592 {
17593 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17594 "dynamic property");
17595 return 0;
17596 }
17597 break;
17598 case DW_AT_data_member_location:
17599 {
17600 LONGEST offset;
17601
17602 if (!handle_data_member_location (target_die, target_cu,
17603 &offset))
17604 return 0;
17605
17606 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17607 baton->property_type = read_type_die (target_die->parent,
17608 target_cu);
17609 baton->offset_info.offset = offset;
17610 baton->offset_info.type = die_type (target_die, target_cu);
17611 prop->data.baton = baton;
17612 prop->kind = PROP_ADDR_OFFSET;
17613 break;
17614 }
17615 }
17616 }
17617 else if (attr->form_is_constant ())
17618 {
17619 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17620 prop->kind = PROP_CONST;
17621 }
17622 else
17623 {
17624 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17625 dwarf2_name (die, cu));
17626 return 0;
17627 }
17628
17629 return 1;
17630 }
17631
17632 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17633 UNSIGNED_P controls if the integer is unsigned or not. */
17634
17635 static struct type *
17636 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17637 int size_in_bytes, bool unsigned_p)
17638 {
17639 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17640 struct type *int_type;
17641
17642 /* Helper macro to examine the various builtin types. */
17643 #define TRY_TYPE(F) \
17644 int_type = (unsigned_p \
17645 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17646 : objfile_type (objfile)->builtin_ ## F); \
17647 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17648 return int_type
17649
17650 TRY_TYPE (char);
17651 TRY_TYPE (short);
17652 TRY_TYPE (int);
17653 TRY_TYPE (long);
17654 TRY_TYPE (long_long);
17655
17656 #undef TRY_TYPE
17657
17658 gdb_assert_not_reached ("unable to find suitable integer type");
17659 }
17660
17661 /* Find an integer type the same size as the address size given in the
17662 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17663 is unsigned or not. */
17664
17665 static struct type *
17666 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17667 bool unsigned_p)
17668 {
17669 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17670 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17671 }
17672
17673 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17674 present (which is valid) then compute the default type based on the
17675 compilation units address size. */
17676
17677 static struct type *
17678 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17679 {
17680 struct type *index_type = die_type (die, cu);
17681
17682 /* Dwarf-2 specifications explicitly allows to create subrange types
17683 without specifying a base type.
17684 In that case, the base type must be set to the type of
17685 the lower bound, upper bound or count, in that order, if any of these
17686 three attributes references an object that has a type.
17687 If no base type is found, the Dwarf-2 specifications say that
17688 a signed integer type of size equal to the size of an address should
17689 be used.
17690 For the following C code: `extern char gdb_int [];'
17691 GCC produces an empty range DIE.
17692 FIXME: muller/2010-05-28: Possible references to object for low bound,
17693 high bound or count are not yet handled by this code. */
17694 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17695 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17696
17697 return index_type;
17698 }
17699
17700 /* Read the given DW_AT_subrange DIE. */
17701
17702 static struct type *
17703 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17704 {
17705 struct type *base_type, *orig_base_type;
17706 struct type *range_type;
17707 struct attribute *attr;
17708 struct dynamic_prop low, high;
17709 int low_default_is_valid;
17710 int high_bound_is_count = 0;
17711 const char *name;
17712 ULONGEST negative_mask;
17713
17714 orig_base_type = read_subrange_index_type (die, cu);
17715
17716 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17717 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17718 creating the range type, but we use the result of check_typedef
17719 when examining properties of the type. */
17720 base_type = check_typedef (orig_base_type);
17721
17722 /* The die_type call above may have already set the type for this DIE. */
17723 range_type = get_die_type (die, cu);
17724 if (range_type)
17725 return range_type;
17726
17727 low.kind = PROP_CONST;
17728 high.kind = PROP_CONST;
17729 high.data.const_val = 0;
17730
17731 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17732 omitting DW_AT_lower_bound. */
17733 switch (cu->language)
17734 {
17735 case language_c:
17736 case language_cplus:
17737 low.data.const_val = 0;
17738 low_default_is_valid = 1;
17739 break;
17740 case language_fortran:
17741 low.data.const_val = 1;
17742 low_default_is_valid = 1;
17743 break;
17744 case language_d:
17745 case language_objc:
17746 case language_rust:
17747 low.data.const_val = 0;
17748 low_default_is_valid = (cu->header.version >= 4);
17749 break;
17750 case language_ada:
17751 case language_m2:
17752 case language_pascal:
17753 low.data.const_val = 1;
17754 low_default_is_valid = (cu->header.version >= 4);
17755 break;
17756 default:
17757 low.data.const_val = 0;
17758 low_default_is_valid = 0;
17759 break;
17760 }
17761
17762 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17763 if (attr != nullptr)
17764 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17765 else if (!low_default_is_valid)
17766 complaint (_("Missing DW_AT_lower_bound "
17767 "- DIE at %s [in module %s]"),
17768 sect_offset_str (die->sect_off),
17769 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17770
17771 struct attribute *attr_ub, *attr_count;
17772 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17773 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17774 {
17775 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17776 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17777 {
17778 /* If bounds are constant do the final calculation here. */
17779 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17780 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17781 else
17782 high_bound_is_count = 1;
17783 }
17784 else
17785 {
17786 if (attr_ub != NULL)
17787 complaint (_("Unresolved DW_AT_upper_bound "
17788 "- DIE at %s [in module %s]"),
17789 sect_offset_str (die->sect_off),
17790 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17791 if (attr_count != NULL)
17792 complaint (_("Unresolved DW_AT_count "
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 }
17797 }
17798
17799 LONGEST bias = 0;
17800 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17801 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17802 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17803
17804 /* Normally, the DWARF producers are expected to use a signed
17805 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17806 But this is unfortunately not always the case, as witnessed
17807 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17808 is used instead. To work around that ambiguity, we treat
17809 the bounds as signed, and thus sign-extend their values, when
17810 the base type is signed. */
17811 negative_mask =
17812 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17813 if (low.kind == PROP_CONST
17814 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17815 low.data.const_val |= negative_mask;
17816 if (high.kind == PROP_CONST
17817 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17818 high.data.const_val |= negative_mask;
17819
17820 /* Check for bit and byte strides. */
17821 struct dynamic_prop byte_stride_prop;
17822 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17823 if (attr_byte_stride != nullptr)
17824 {
17825 struct type *prop_type
17826 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17827 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17828 prop_type);
17829 }
17830
17831 struct dynamic_prop bit_stride_prop;
17832 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17833 if (attr_bit_stride != nullptr)
17834 {
17835 /* It only makes sense to have either a bit or byte stride. */
17836 if (attr_byte_stride != nullptr)
17837 {
17838 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17839 "- DIE at %s [in module %s]"),
17840 sect_offset_str (die->sect_off),
17841 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17842 attr_bit_stride = nullptr;
17843 }
17844 else
17845 {
17846 struct type *prop_type
17847 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17848 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17849 prop_type);
17850 }
17851 }
17852
17853 if (attr_byte_stride != nullptr
17854 || attr_bit_stride != nullptr)
17855 {
17856 bool byte_stride_p = (attr_byte_stride != nullptr);
17857 struct dynamic_prop *stride
17858 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17859
17860 range_type
17861 = create_range_type_with_stride (NULL, orig_base_type, &low,
17862 &high, bias, stride, byte_stride_p);
17863 }
17864 else
17865 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17866
17867 if (high_bound_is_count)
17868 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17869
17870 /* Ada expects an empty array on no boundary attributes. */
17871 if (attr == NULL && cu->language != language_ada)
17872 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17873
17874 name = dwarf2_name (die, cu);
17875 if (name)
17876 TYPE_NAME (range_type) = name;
17877
17878 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17879 if (attr != nullptr)
17880 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17881
17882 maybe_set_alignment (cu, die, range_type);
17883
17884 set_die_type (die, range_type, cu);
17885
17886 /* set_die_type should be already done. */
17887 set_descriptive_type (range_type, die, cu);
17888
17889 return range_type;
17890 }
17891
17892 static struct type *
17893 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17894 {
17895 struct type *type;
17896
17897 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17898 NULL);
17899 TYPE_NAME (type) = dwarf2_name (die, cu);
17900
17901 /* In Ada, an unspecified type is typically used when the description
17902 of the type is deferred to a different unit. When encountering
17903 such a type, we treat it as a stub, and try to resolve it later on,
17904 when needed. */
17905 if (cu->language == language_ada)
17906 TYPE_STUB (type) = 1;
17907
17908 return set_die_type (die, type, cu);
17909 }
17910
17911 /* Read a single die and all its descendents. Set the die's sibling
17912 field to NULL; set other fields in the die correctly, and set all
17913 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17914 location of the info_ptr after reading all of those dies. PARENT
17915 is the parent of the die in question. */
17916
17917 static struct die_info *
17918 read_die_and_children (const struct die_reader_specs *reader,
17919 const gdb_byte *info_ptr,
17920 const gdb_byte **new_info_ptr,
17921 struct die_info *parent)
17922 {
17923 struct die_info *die;
17924 const gdb_byte *cur_ptr;
17925 int has_children;
17926
17927 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17928 if (die == NULL)
17929 {
17930 *new_info_ptr = cur_ptr;
17931 return NULL;
17932 }
17933 store_in_ref_table (die, reader->cu);
17934
17935 if (has_children)
17936 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17937 else
17938 {
17939 die->child = NULL;
17940 *new_info_ptr = cur_ptr;
17941 }
17942
17943 die->sibling = NULL;
17944 die->parent = parent;
17945 return die;
17946 }
17947
17948 /* Read a die, all of its descendents, and all of its siblings; set
17949 all of the fields of all of the dies correctly. Arguments are as
17950 in read_die_and_children. */
17951
17952 static struct die_info *
17953 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17954 const gdb_byte *info_ptr,
17955 const gdb_byte **new_info_ptr,
17956 struct die_info *parent)
17957 {
17958 struct die_info *first_die, *last_sibling;
17959 const gdb_byte *cur_ptr;
17960
17961 cur_ptr = info_ptr;
17962 first_die = last_sibling = NULL;
17963
17964 while (1)
17965 {
17966 struct die_info *die
17967 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17968
17969 if (die == NULL)
17970 {
17971 *new_info_ptr = cur_ptr;
17972 return first_die;
17973 }
17974
17975 if (!first_die)
17976 first_die = die;
17977 else
17978 last_sibling->sibling = die;
17979
17980 last_sibling = die;
17981 }
17982 }
17983
17984 /* Read a die, all of its descendents, and all of its siblings; set
17985 all of the fields of all of the dies correctly. Arguments are as
17986 in read_die_and_children.
17987 This the main entry point for reading a DIE and all its children. */
17988
17989 static struct die_info *
17990 read_die_and_siblings (const struct die_reader_specs *reader,
17991 const gdb_byte *info_ptr,
17992 const gdb_byte **new_info_ptr,
17993 struct die_info *parent)
17994 {
17995 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17996 new_info_ptr, parent);
17997
17998 if (dwarf_die_debug)
17999 {
18000 fprintf_unfiltered (gdb_stdlog,
18001 "Read die from %s@0x%x of %s:\n",
18002 reader->die_section->get_name (),
18003 (unsigned) (info_ptr - reader->die_section->buffer),
18004 bfd_get_filename (reader->abfd));
18005 dump_die (die, dwarf_die_debug);
18006 }
18007
18008 return die;
18009 }
18010
18011 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18012 attributes.
18013 The caller is responsible for filling in the extra attributes
18014 and updating (*DIEP)->num_attrs.
18015 Set DIEP to point to a newly allocated die with its information,
18016 except for its child, sibling, and parent fields.
18017 Set HAS_CHILDREN to tell whether the die has children or not. */
18018
18019 static const gdb_byte *
18020 read_full_die_1 (const struct die_reader_specs *reader,
18021 struct die_info **diep, const gdb_byte *info_ptr,
18022 int *has_children, int num_extra_attrs)
18023 {
18024 unsigned int abbrev_number, bytes_read, i;
18025 struct abbrev_info *abbrev;
18026 struct die_info *die;
18027 struct dwarf2_cu *cu = reader->cu;
18028 bfd *abfd = reader->abfd;
18029
18030 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18031 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18032 info_ptr += bytes_read;
18033 if (!abbrev_number)
18034 {
18035 *diep = NULL;
18036 *has_children = 0;
18037 return info_ptr;
18038 }
18039
18040 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18041 if (!abbrev)
18042 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18043 abbrev_number,
18044 bfd_get_filename (abfd));
18045
18046 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18047 die->sect_off = sect_off;
18048 die->tag = abbrev->tag;
18049 die->abbrev = abbrev_number;
18050
18051 /* Make the result usable.
18052 The caller needs to update num_attrs after adding the extra
18053 attributes. */
18054 die->num_attrs = abbrev->num_attrs;
18055
18056 std::vector<int> indexes_that_need_reprocess;
18057 for (i = 0; i < abbrev->num_attrs; ++i)
18058 {
18059 bool need_reprocess;
18060 info_ptr =
18061 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18062 info_ptr, &need_reprocess);
18063 if (need_reprocess)
18064 indexes_that_need_reprocess.push_back (i);
18065 }
18066
18067 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18068 if (attr != nullptr)
18069 cu->str_offsets_base = DW_UNSND (attr);
18070
18071 auto maybe_addr_base = lookup_addr_base(die);
18072 if (maybe_addr_base.has_value ())
18073 cu->addr_base = *maybe_addr_base;
18074 for (int index : indexes_that_need_reprocess)
18075 read_attribute_reprocess (reader, &die->attrs[index]);
18076 *diep = die;
18077 *has_children = abbrev->has_children;
18078 return info_ptr;
18079 }
18080
18081 /* Read a die and all its attributes.
18082 Set DIEP to point to a newly allocated die with its information,
18083 except for its child, sibling, and parent fields.
18084 Set HAS_CHILDREN to tell whether the die has children or not. */
18085
18086 static const gdb_byte *
18087 read_full_die (const struct die_reader_specs *reader,
18088 struct die_info **diep, const gdb_byte *info_ptr,
18089 int *has_children)
18090 {
18091 const gdb_byte *result;
18092
18093 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18094
18095 if (dwarf_die_debug)
18096 {
18097 fprintf_unfiltered (gdb_stdlog,
18098 "Read die from %s@0x%x of %s:\n",
18099 reader->die_section->get_name (),
18100 (unsigned) (info_ptr - reader->die_section->buffer),
18101 bfd_get_filename (reader->abfd));
18102 dump_die (*diep, dwarf_die_debug);
18103 }
18104
18105 return result;
18106 }
18107 \f
18108
18109 /* Returns nonzero if TAG represents a type that we might generate a partial
18110 symbol for. */
18111
18112 static int
18113 is_type_tag_for_partial (int tag)
18114 {
18115 switch (tag)
18116 {
18117 #if 0
18118 /* Some types that would be reasonable to generate partial symbols for,
18119 that we don't at present. */
18120 case DW_TAG_array_type:
18121 case DW_TAG_file_type:
18122 case DW_TAG_ptr_to_member_type:
18123 case DW_TAG_set_type:
18124 case DW_TAG_string_type:
18125 case DW_TAG_subroutine_type:
18126 #endif
18127 case DW_TAG_base_type:
18128 case DW_TAG_class_type:
18129 case DW_TAG_interface_type:
18130 case DW_TAG_enumeration_type:
18131 case DW_TAG_structure_type:
18132 case DW_TAG_subrange_type:
18133 case DW_TAG_typedef:
18134 case DW_TAG_union_type:
18135 return 1;
18136 default:
18137 return 0;
18138 }
18139 }
18140
18141 /* Load all DIEs that are interesting for partial symbols into memory. */
18142
18143 static struct partial_die_info *
18144 load_partial_dies (const struct die_reader_specs *reader,
18145 const gdb_byte *info_ptr, int building_psymtab)
18146 {
18147 struct dwarf2_cu *cu = reader->cu;
18148 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18149 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18150 unsigned int bytes_read;
18151 unsigned int load_all = 0;
18152 int nesting_level = 1;
18153
18154 parent_die = NULL;
18155 last_die = NULL;
18156
18157 gdb_assert (cu->per_cu != NULL);
18158 if (cu->per_cu->load_all_dies)
18159 load_all = 1;
18160
18161 cu->partial_dies
18162 = htab_create_alloc_ex (cu->header.length / 12,
18163 partial_die_hash,
18164 partial_die_eq,
18165 NULL,
18166 &cu->comp_unit_obstack,
18167 hashtab_obstack_allocate,
18168 dummy_obstack_deallocate);
18169
18170 while (1)
18171 {
18172 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18173
18174 /* A NULL abbrev means the end of a series of children. */
18175 if (abbrev == NULL)
18176 {
18177 if (--nesting_level == 0)
18178 return first_die;
18179
18180 info_ptr += bytes_read;
18181 last_die = parent_die;
18182 parent_die = parent_die->die_parent;
18183 continue;
18184 }
18185
18186 /* Check for template arguments. We never save these; if
18187 they're seen, we just mark the parent, and go on our way. */
18188 if (parent_die != NULL
18189 && cu->language == language_cplus
18190 && (abbrev->tag == DW_TAG_template_type_param
18191 || abbrev->tag == DW_TAG_template_value_param))
18192 {
18193 parent_die->has_template_arguments = 1;
18194
18195 if (!load_all)
18196 {
18197 /* We don't need a partial DIE for the template argument. */
18198 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18199 continue;
18200 }
18201 }
18202
18203 /* We only recurse into c++ subprograms looking for template arguments.
18204 Skip their other children. */
18205 if (!load_all
18206 && cu->language == language_cplus
18207 && parent_die != NULL
18208 && parent_die->tag == DW_TAG_subprogram)
18209 {
18210 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18211 continue;
18212 }
18213
18214 /* Check whether this DIE is interesting enough to save. Normally
18215 we would not be interested in members here, but there may be
18216 later variables referencing them via DW_AT_specification (for
18217 static members). */
18218 if (!load_all
18219 && !is_type_tag_for_partial (abbrev->tag)
18220 && abbrev->tag != DW_TAG_constant
18221 && abbrev->tag != DW_TAG_enumerator
18222 && abbrev->tag != DW_TAG_subprogram
18223 && abbrev->tag != DW_TAG_inlined_subroutine
18224 && abbrev->tag != DW_TAG_lexical_block
18225 && abbrev->tag != DW_TAG_variable
18226 && abbrev->tag != DW_TAG_namespace
18227 && abbrev->tag != DW_TAG_module
18228 && abbrev->tag != DW_TAG_member
18229 && abbrev->tag != DW_TAG_imported_unit
18230 && abbrev->tag != DW_TAG_imported_declaration)
18231 {
18232 /* Otherwise we skip to the next sibling, if any. */
18233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18234 continue;
18235 }
18236
18237 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18238 abbrev);
18239
18240 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18241
18242 /* This two-pass algorithm for processing partial symbols has a
18243 high cost in cache pressure. Thus, handle some simple cases
18244 here which cover the majority of C partial symbols. DIEs
18245 which neither have specification tags in them, nor could have
18246 specification tags elsewhere pointing at them, can simply be
18247 processed and discarded.
18248
18249 This segment is also optional; scan_partial_symbols and
18250 add_partial_symbol will handle these DIEs if we chain
18251 them in normally. When compilers which do not emit large
18252 quantities of duplicate debug information are more common,
18253 this code can probably be removed. */
18254
18255 /* Any complete simple types at the top level (pretty much all
18256 of them, for a language without namespaces), can be processed
18257 directly. */
18258 if (parent_die == NULL
18259 && pdi.has_specification == 0
18260 && pdi.is_declaration == 0
18261 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18262 || pdi.tag == DW_TAG_base_type
18263 || pdi.tag == DW_TAG_subrange_type))
18264 {
18265 if (building_psymtab && pdi.name != NULL)
18266 add_psymbol_to_list (pdi.name, false,
18267 VAR_DOMAIN, LOC_TYPEDEF, -1,
18268 psymbol_placement::STATIC,
18269 0, cu->language, objfile);
18270 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18271 continue;
18272 }
18273
18274 /* The exception for DW_TAG_typedef with has_children above is
18275 a workaround of GCC PR debug/47510. In the case of this complaint
18276 type_name_or_error will error on such types later.
18277
18278 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18279 it could not find the child DIEs referenced later, this is checked
18280 above. In correct DWARF DW_TAG_typedef should have no children. */
18281
18282 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18283 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18284 "- DIE at %s [in module %s]"),
18285 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18286
18287 /* If we're at the second level, and we're an enumerator, and
18288 our parent has no specification (meaning possibly lives in a
18289 namespace elsewhere), then we can add the partial symbol now
18290 instead of queueing it. */
18291 if (pdi.tag == DW_TAG_enumerator
18292 && parent_die != NULL
18293 && parent_die->die_parent == NULL
18294 && parent_die->tag == DW_TAG_enumeration_type
18295 && parent_die->has_specification == 0)
18296 {
18297 if (pdi.name == NULL)
18298 complaint (_("malformed enumerator DIE ignored"));
18299 else if (building_psymtab)
18300 add_psymbol_to_list (pdi.name, false,
18301 VAR_DOMAIN, LOC_CONST, -1,
18302 cu->language == language_cplus
18303 ? psymbol_placement::GLOBAL
18304 : psymbol_placement::STATIC,
18305 0, cu->language, objfile);
18306
18307 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18308 continue;
18309 }
18310
18311 struct partial_die_info *part_die
18312 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18313
18314 /* We'll save this DIE so link it in. */
18315 part_die->die_parent = parent_die;
18316 part_die->die_sibling = NULL;
18317 part_die->die_child = NULL;
18318
18319 if (last_die && last_die == parent_die)
18320 last_die->die_child = part_die;
18321 else if (last_die)
18322 last_die->die_sibling = part_die;
18323
18324 last_die = part_die;
18325
18326 if (first_die == NULL)
18327 first_die = part_die;
18328
18329 /* Maybe add the DIE to the hash table. Not all DIEs that we
18330 find interesting need to be in the hash table, because we
18331 also have the parent/sibling/child chains; only those that we
18332 might refer to by offset later during partial symbol reading.
18333
18334 For now this means things that might have be the target of a
18335 DW_AT_specification, DW_AT_abstract_origin, or
18336 DW_AT_extension. DW_AT_extension will refer only to
18337 namespaces; DW_AT_abstract_origin refers to functions (and
18338 many things under the function DIE, but we do not recurse
18339 into function DIEs during partial symbol reading) and
18340 possibly variables as well; DW_AT_specification refers to
18341 declarations. Declarations ought to have the DW_AT_declaration
18342 flag. It happens that GCC forgets to put it in sometimes, but
18343 only for functions, not for types.
18344
18345 Adding more things than necessary to the hash table is harmless
18346 except for the performance cost. Adding too few will result in
18347 wasted time in find_partial_die, when we reread the compilation
18348 unit with load_all_dies set. */
18349
18350 if (load_all
18351 || abbrev->tag == DW_TAG_constant
18352 || abbrev->tag == DW_TAG_subprogram
18353 || abbrev->tag == DW_TAG_variable
18354 || abbrev->tag == DW_TAG_namespace
18355 || part_die->is_declaration)
18356 {
18357 void **slot;
18358
18359 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18360 to_underlying (part_die->sect_off),
18361 INSERT);
18362 *slot = part_die;
18363 }
18364
18365 /* For some DIEs we want to follow their children (if any). For C
18366 we have no reason to follow the children of structures; for other
18367 languages we have to, so that we can get at method physnames
18368 to infer fully qualified class names, for DW_AT_specification,
18369 and for C++ template arguments. For C++, we also look one level
18370 inside functions to find template arguments (if the name of the
18371 function does not already contain the template arguments).
18372
18373 For Ada and Fortran, we need to scan the children of subprograms
18374 and lexical blocks as well because these languages allow the
18375 definition of nested entities that could be interesting for the
18376 debugger, such as nested subprograms for instance. */
18377 if (last_die->has_children
18378 && (load_all
18379 || last_die->tag == DW_TAG_namespace
18380 || last_die->tag == DW_TAG_module
18381 || last_die->tag == DW_TAG_enumeration_type
18382 || (cu->language == language_cplus
18383 && last_die->tag == DW_TAG_subprogram
18384 && (last_die->name == NULL
18385 || strchr (last_die->name, '<') == NULL))
18386 || (cu->language != language_c
18387 && (last_die->tag == DW_TAG_class_type
18388 || last_die->tag == DW_TAG_interface_type
18389 || last_die->tag == DW_TAG_structure_type
18390 || last_die->tag == DW_TAG_union_type))
18391 || ((cu->language == language_ada
18392 || cu->language == language_fortran)
18393 && (last_die->tag == DW_TAG_subprogram
18394 || last_die->tag == DW_TAG_lexical_block))))
18395 {
18396 nesting_level++;
18397 parent_die = last_die;
18398 continue;
18399 }
18400
18401 /* Otherwise we skip to the next sibling, if any. */
18402 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18403
18404 /* Back to the top, do it again. */
18405 }
18406 }
18407
18408 partial_die_info::partial_die_info (sect_offset sect_off_,
18409 struct abbrev_info *abbrev)
18410 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18411 {
18412 }
18413
18414 /* Read a minimal amount of information into the minimal die structure.
18415 INFO_PTR should point just after the initial uleb128 of a DIE. */
18416
18417 const gdb_byte *
18418 partial_die_info::read (const struct die_reader_specs *reader,
18419 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18420 {
18421 struct dwarf2_cu *cu = reader->cu;
18422 struct dwarf2_per_objfile *dwarf2_per_objfile
18423 = cu->per_cu->dwarf2_per_objfile;
18424 unsigned int i;
18425 int has_low_pc_attr = 0;
18426 int has_high_pc_attr = 0;
18427 int high_pc_relative = 0;
18428
18429 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18430 for (i = 0; i < abbrev.num_attrs; ++i)
18431 {
18432 bool need_reprocess;
18433 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18434 info_ptr, &need_reprocess);
18435 /* String and address offsets that need to do the reprocessing have
18436 already been read at this point, so there is no need to wait until
18437 the loop terminates to do the reprocessing. */
18438 if (need_reprocess)
18439 read_attribute_reprocess (reader, &attr_vec[i]);
18440 attribute &attr = attr_vec[i];
18441 /* Store the data if it is of an attribute we want to keep in a
18442 partial symbol table. */
18443 switch (attr.name)
18444 {
18445 case DW_AT_name:
18446 switch (tag)
18447 {
18448 case DW_TAG_compile_unit:
18449 case DW_TAG_partial_unit:
18450 case DW_TAG_type_unit:
18451 /* Compilation units have a DW_AT_name that is a filename, not
18452 a source language identifier. */
18453 case DW_TAG_enumeration_type:
18454 case DW_TAG_enumerator:
18455 /* These tags always have simple identifiers already; no need
18456 to canonicalize them. */
18457 name = DW_STRING (&attr);
18458 break;
18459 default:
18460 {
18461 struct objfile *objfile = dwarf2_per_objfile->objfile;
18462
18463 name
18464 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18465 &objfile->per_bfd->storage_obstack);
18466 }
18467 break;
18468 }
18469 break;
18470 case DW_AT_linkage_name:
18471 case DW_AT_MIPS_linkage_name:
18472 /* Note that both forms of linkage name might appear. We
18473 assume they will be the same, and we only store the last
18474 one we see. */
18475 linkage_name = DW_STRING (&attr);
18476 break;
18477 case DW_AT_low_pc:
18478 has_low_pc_attr = 1;
18479 lowpc = attr.value_as_address ();
18480 break;
18481 case DW_AT_high_pc:
18482 has_high_pc_attr = 1;
18483 highpc = attr.value_as_address ();
18484 if (cu->header.version >= 4 && attr.form_is_constant ())
18485 high_pc_relative = 1;
18486 break;
18487 case DW_AT_location:
18488 /* Support the .debug_loc offsets. */
18489 if (attr.form_is_block ())
18490 {
18491 d.locdesc = DW_BLOCK (&attr);
18492 }
18493 else if (attr.form_is_section_offset ())
18494 {
18495 dwarf2_complex_location_expr_complaint ();
18496 }
18497 else
18498 {
18499 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18500 "partial symbol information");
18501 }
18502 break;
18503 case DW_AT_external:
18504 is_external = DW_UNSND (&attr);
18505 break;
18506 case DW_AT_declaration:
18507 is_declaration = DW_UNSND (&attr);
18508 break;
18509 case DW_AT_type:
18510 has_type = 1;
18511 break;
18512 case DW_AT_abstract_origin:
18513 case DW_AT_specification:
18514 case DW_AT_extension:
18515 has_specification = 1;
18516 spec_offset = dwarf2_get_ref_die_offset (&attr);
18517 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18518 || cu->per_cu->is_dwz);
18519 break;
18520 case DW_AT_sibling:
18521 /* Ignore absolute siblings, they might point outside of
18522 the current compile unit. */
18523 if (attr.form == DW_FORM_ref_addr)
18524 complaint (_("ignoring absolute DW_AT_sibling"));
18525 else
18526 {
18527 const gdb_byte *buffer = reader->buffer;
18528 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18529 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18530
18531 if (sibling_ptr < info_ptr)
18532 complaint (_("DW_AT_sibling points backwards"));
18533 else if (sibling_ptr > reader->buffer_end)
18534 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18535 else
18536 sibling = sibling_ptr;
18537 }
18538 break;
18539 case DW_AT_byte_size:
18540 has_byte_size = 1;
18541 break;
18542 case DW_AT_const_value:
18543 has_const_value = 1;
18544 break;
18545 case DW_AT_calling_convention:
18546 /* DWARF doesn't provide a way to identify a program's source-level
18547 entry point. DW_AT_calling_convention attributes are only meant
18548 to describe functions' calling conventions.
18549
18550 However, because it's a necessary piece of information in
18551 Fortran, and before DWARF 4 DW_CC_program was the only
18552 piece of debugging information whose definition refers to
18553 a 'main program' at all, several compilers marked Fortran
18554 main programs with DW_CC_program --- even when those
18555 functions use the standard calling conventions.
18556
18557 Although DWARF now specifies a way to provide this
18558 information, we support this practice for backward
18559 compatibility. */
18560 if (DW_UNSND (&attr) == DW_CC_program
18561 && cu->language == language_fortran)
18562 main_subprogram = 1;
18563 break;
18564 case DW_AT_inline:
18565 if (DW_UNSND (&attr) == DW_INL_inlined
18566 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18567 may_be_inlined = 1;
18568 break;
18569
18570 case DW_AT_import:
18571 if (tag == DW_TAG_imported_unit)
18572 {
18573 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18574 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18575 || cu->per_cu->is_dwz);
18576 }
18577 break;
18578
18579 case DW_AT_main_subprogram:
18580 main_subprogram = DW_UNSND (&attr);
18581 break;
18582
18583 case DW_AT_ranges:
18584 {
18585 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18586 but that requires a full DIE, so instead we just
18587 reimplement it. */
18588 int need_ranges_base = tag != DW_TAG_compile_unit;
18589 unsigned int ranges_offset = (DW_UNSND (&attr)
18590 + (need_ranges_base
18591 ? cu->ranges_base
18592 : 0));
18593
18594 /* Value of the DW_AT_ranges attribute is the offset in the
18595 .debug_ranges section. */
18596 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18597 nullptr))
18598 has_pc_info = 1;
18599 }
18600 break;
18601
18602 default:
18603 break;
18604 }
18605 }
18606
18607 /* For Ada, if both the name and the linkage name appear, we prefer
18608 the latter. This lets "catch exception" work better, regardless
18609 of the order in which the name and linkage name were emitted.
18610 Really, though, this is just a workaround for the fact that gdb
18611 doesn't store both the name and the linkage name. */
18612 if (cu->language == language_ada && linkage_name != nullptr)
18613 name = linkage_name;
18614
18615 if (high_pc_relative)
18616 highpc += lowpc;
18617
18618 if (has_low_pc_attr && has_high_pc_attr)
18619 {
18620 /* When using the GNU linker, .gnu.linkonce. sections are used to
18621 eliminate duplicate copies of functions and vtables and such.
18622 The linker will arbitrarily choose one and discard the others.
18623 The AT_*_pc values for such functions refer to local labels in
18624 these sections. If the section from that file was discarded, the
18625 labels are not in the output, so the relocs get a value of 0.
18626 If this is a discarded function, mark the pc bounds as invalid,
18627 so that GDB will ignore it. */
18628 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18629 {
18630 struct objfile *objfile = dwarf2_per_objfile->objfile;
18631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18632
18633 complaint (_("DW_AT_low_pc %s is zero "
18634 "for DIE at %s [in module %s]"),
18635 paddress (gdbarch, lowpc),
18636 sect_offset_str (sect_off),
18637 objfile_name (objfile));
18638 }
18639 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18640 else if (lowpc >= highpc)
18641 {
18642 struct objfile *objfile = dwarf2_per_objfile->objfile;
18643 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18644
18645 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18646 "for DIE at %s [in module %s]"),
18647 paddress (gdbarch, lowpc),
18648 paddress (gdbarch, highpc),
18649 sect_offset_str (sect_off),
18650 objfile_name (objfile));
18651 }
18652 else
18653 has_pc_info = 1;
18654 }
18655
18656 return info_ptr;
18657 }
18658
18659 /* Find a cached partial DIE at OFFSET in CU. */
18660
18661 struct partial_die_info *
18662 dwarf2_cu::find_partial_die (sect_offset sect_off)
18663 {
18664 struct partial_die_info *lookup_die = NULL;
18665 struct partial_die_info part_die (sect_off);
18666
18667 lookup_die = ((struct partial_die_info *)
18668 htab_find_with_hash (partial_dies, &part_die,
18669 to_underlying (sect_off)));
18670
18671 return lookup_die;
18672 }
18673
18674 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18675 except in the case of .debug_types DIEs which do not reference
18676 outside their CU (they do however referencing other types via
18677 DW_FORM_ref_sig8). */
18678
18679 static const struct cu_partial_die_info
18680 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18681 {
18682 struct dwarf2_per_objfile *dwarf2_per_objfile
18683 = cu->per_cu->dwarf2_per_objfile;
18684 struct objfile *objfile = dwarf2_per_objfile->objfile;
18685 struct dwarf2_per_cu_data *per_cu = NULL;
18686 struct partial_die_info *pd = NULL;
18687
18688 if (offset_in_dwz == cu->per_cu->is_dwz
18689 && offset_in_cu_p (&cu->header, sect_off))
18690 {
18691 pd = cu->find_partial_die (sect_off);
18692 if (pd != NULL)
18693 return { cu, pd };
18694 /* We missed recording what we needed.
18695 Load all dies and try again. */
18696 per_cu = cu->per_cu;
18697 }
18698 else
18699 {
18700 /* TUs don't reference other CUs/TUs (except via type signatures). */
18701 if (cu->per_cu->is_debug_types)
18702 {
18703 error (_("Dwarf Error: Type Unit at offset %s contains"
18704 " external reference to offset %s [in module %s].\n"),
18705 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18706 bfd_get_filename (objfile->obfd));
18707 }
18708 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18709 dwarf2_per_objfile);
18710
18711 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18712 load_partial_comp_unit (per_cu);
18713
18714 per_cu->cu->last_used = 0;
18715 pd = per_cu->cu->find_partial_die (sect_off);
18716 }
18717
18718 /* If we didn't find it, and not all dies have been loaded,
18719 load them all and try again. */
18720
18721 if (pd == NULL && per_cu->load_all_dies == 0)
18722 {
18723 per_cu->load_all_dies = 1;
18724
18725 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18726 THIS_CU->cu may already be in use. So we can't just free it and
18727 replace its DIEs with the ones we read in. Instead, we leave those
18728 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18729 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18730 set. */
18731 load_partial_comp_unit (per_cu);
18732
18733 pd = per_cu->cu->find_partial_die (sect_off);
18734 }
18735
18736 if (pd == NULL)
18737 internal_error (__FILE__, __LINE__,
18738 _("could not find partial DIE %s "
18739 "in cache [from module %s]\n"),
18740 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18741 return { per_cu->cu, pd };
18742 }
18743
18744 /* See if we can figure out if the class lives in a namespace. We do
18745 this by looking for a member function; its demangled name will
18746 contain namespace info, if there is any. */
18747
18748 static void
18749 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18750 struct dwarf2_cu *cu)
18751 {
18752 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18753 what template types look like, because the demangler
18754 frequently doesn't give the same name as the debug info. We
18755 could fix this by only using the demangled name to get the
18756 prefix (but see comment in read_structure_type). */
18757
18758 struct partial_die_info *real_pdi;
18759 struct partial_die_info *child_pdi;
18760
18761 /* If this DIE (this DIE's specification, if any) has a parent, then
18762 we should not do this. We'll prepend the parent's fully qualified
18763 name when we create the partial symbol. */
18764
18765 real_pdi = struct_pdi;
18766 while (real_pdi->has_specification)
18767 {
18768 auto res = find_partial_die (real_pdi->spec_offset,
18769 real_pdi->spec_is_dwz, cu);
18770 real_pdi = res.pdi;
18771 cu = res.cu;
18772 }
18773
18774 if (real_pdi->die_parent != NULL)
18775 return;
18776
18777 for (child_pdi = struct_pdi->die_child;
18778 child_pdi != NULL;
18779 child_pdi = child_pdi->die_sibling)
18780 {
18781 if (child_pdi->tag == DW_TAG_subprogram
18782 && child_pdi->linkage_name != NULL)
18783 {
18784 gdb::unique_xmalloc_ptr<char> actual_class_name
18785 (language_class_name_from_physname (cu->language_defn,
18786 child_pdi->linkage_name));
18787 if (actual_class_name != NULL)
18788 {
18789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18790 struct_pdi->name
18791 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18792 actual_class_name.get ());
18793 }
18794 break;
18795 }
18796 }
18797 }
18798
18799 void
18800 partial_die_info::fixup (struct dwarf2_cu *cu)
18801 {
18802 /* Once we've fixed up a die, there's no point in doing so again.
18803 This also avoids a memory leak if we were to call
18804 guess_partial_die_structure_name multiple times. */
18805 if (fixup_called)
18806 return;
18807
18808 /* If we found a reference attribute and the DIE has no name, try
18809 to find a name in the referred to DIE. */
18810
18811 if (name == NULL && has_specification)
18812 {
18813 struct partial_die_info *spec_die;
18814
18815 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18816 spec_die = res.pdi;
18817 cu = res.cu;
18818
18819 spec_die->fixup (cu);
18820
18821 if (spec_die->name)
18822 {
18823 name = spec_die->name;
18824
18825 /* Copy DW_AT_external attribute if it is set. */
18826 if (spec_die->is_external)
18827 is_external = spec_die->is_external;
18828 }
18829 }
18830
18831 /* Set default names for some unnamed DIEs. */
18832
18833 if (name == NULL && tag == DW_TAG_namespace)
18834 name = CP_ANONYMOUS_NAMESPACE_STR;
18835
18836 /* If there is no parent die to provide a namespace, and there are
18837 children, see if we can determine the namespace from their linkage
18838 name. */
18839 if (cu->language == language_cplus
18840 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18841 && die_parent == NULL
18842 && has_children
18843 && (tag == DW_TAG_class_type
18844 || tag == DW_TAG_structure_type
18845 || tag == DW_TAG_union_type))
18846 guess_partial_die_structure_name (this, cu);
18847
18848 /* GCC might emit a nameless struct or union that has a linkage
18849 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18850 if (name == NULL
18851 && (tag == DW_TAG_class_type
18852 || tag == DW_TAG_interface_type
18853 || tag == DW_TAG_structure_type
18854 || tag == DW_TAG_union_type)
18855 && linkage_name != NULL)
18856 {
18857 gdb::unique_xmalloc_ptr<char> demangled
18858 (gdb_demangle (linkage_name, DMGL_TYPES));
18859 if (demangled != nullptr)
18860 {
18861 const char *base;
18862
18863 /* Strip any leading namespaces/classes, keep only the base name.
18864 DW_AT_name for named DIEs does not contain the prefixes. */
18865 base = strrchr (demangled.get (), ':');
18866 if (base && base > demangled.get () && base[-1] == ':')
18867 base++;
18868 else
18869 base = demangled.get ();
18870
18871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18872 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18873 }
18874 }
18875
18876 fixup_called = 1;
18877 }
18878
18879 /* Process the attributes that had to be skipped in the first round. These
18880 attributes are the ones that need str_offsets_base or addr_base attributes.
18881 They could not have been processed in the first round, because at the time
18882 the values of str_offsets_base or addr_base may not have been known. */
18883 void read_attribute_reprocess (const struct die_reader_specs *reader,
18884 struct attribute *attr)
18885 {
18886 struct dwarf2_cu *cu = reader->cu;
18887 switch (attr->form)
18888 {
18889 case DW_FORM_addrx:
18890 case DW_FORM_GNU_addr_index:
18891 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18892 break;
18893 case DW_FORM_strx:
18894 case DW_FORM_strx1:
18895 case DW_FORM_strx2:
18896 case DW_FORM_strx3:
18897 case DW_FORM_strx4:
18898 case DW_FORM_GNU_str_index:
18899 {
18900 unsigned int str_index = DW_UNSND (attr);
18901 if (reader->dwo_file != NULL)
18902 {
18903 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18904 DW_STRING_IS_CANONICAL (attr) = 0;
18905 }
18906 else
18907 {
18908 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18909 DW_STRING_IS_CANONICAL (attr) = 0;
18910 }
18911 break;
18912 }
18913 default:
18914 gdb_assert_not_reached (_("Unexpected DWARF form."));
18915 }
18916 }
18917
18918 /* Read an attribute value described by an attribute form. */
18919
18920 static const gdb_byte *
18921 read_attribute_value (const struct die_reader_specs *reader,
18922 struct attribute *attr, unsigned form,
18923 LONGEST implicit_const, const gdb_byte *info_ptr,
18924 bool *need_reprocess)
18925 {
18926 struct dwarf2_cu *cu = reader->cu;
18927 struct dwarf2_per_objfile *dwarf2_per_objfile
18928 = cu->per_cu->dwarf2_per_objfile;
18929 struct objfile *objfile = dwarf2_per_objfile->objfile;
18930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18931 bfd *abfd = reader->abfd;
18932 struct comp_unit_head *cu_header = &cu->header;
18933 unsigned int bytes_read;
18934 struct dwarf_block *blk;
18935 *need_reprocess = false;
18936
18937 attr->form = (enum dwarf_form) form;
18938 switch (form)
18939 {
18940 case DW_FORM_ref_addr:
18941 if (cu->header.version == 2)
18942 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18943 else
18944 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18945 &cu->header, &bytes_read);
18946 info_ptr += bytes_read;
18947 break;
18948 case DW_FORM_GNU_ref_alt:
18949 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18950 info_ptr += bytes_read;
18951 break;
18952 case DW_FORM_addr:
18953 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18954 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18955 info_ptr += bytes_read;
18956 break;
18957 case DW_FORM_block2:
18958 blk = dwarf_alloc_block (cu);
18959 blk->size = read_2_bytes (abfd, info_ptr);
18960 info_ptr += 2;
18961 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18962 info_ptr += blk->size;
18963 DW_BLOCK (attr) = blk;
18964 break;
18965 case DW_FORM_block4:
18966 blk = dwarf_alloc_block (cu);
18967 blk->size = read_4_bytes (abfd, info_ptr);
18968 info_ptr += 4;
18969 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18970 info_ptr += blk->size;
18971 DW_BLOCK (attr) = blk;
18972 break;
18973 case DW_FORM_data2:
18974 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18975 info_ptr += 2;
18976 break;
18977 case DW_FORM_data4:
18978 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18979 info_ptr += 4;
18980 break;
18981 case DW_FORM_data8:
18982 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18983 info_ptr += 8;
18984 break;
18985 case DW_FORM_data16:
18986 blk = dwarf_alloc_block (cu);
18987 blk->size = 16;
18988 blk->data = read_n_bytes (abfd, info_ptr, 16);
18989 info_ptr += 16;
18990 DW_BLOCK (attr) = blk;
18991 break;
18992 case DW_FORM_sec_offset:
18993 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18994 info_ptr += bytes_read;
18995 break;
18996 case DW_FORM_string:
18997 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18998 DW_STRING_IS_CANONICAL (attr) = 0;
18999 info_ptr += bytes_read;
19000 break;
19001 case DW_FORM_strp:
19002 if (!cu->per_cu->is_dwz)
19003 {
19004 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19005 abfd, info_ptr, cu_header,
19006 &bytes_read);
19007 DW_STRING_IS_CANONICAL (attr) = 0;
19008 info_ptr += bytes_read;
19009 break;
19010 }
19011 /* FALLTHROUGH */
19012 case DW_FORM_line_strp:
19013 if (!cu->per_cu->is_dwz)
19014 {
19015 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19016 abfd, info_ptr,
19017 cu_header, &bytes_read);
19018 DW_STRING_IS_CANONICAL (attr) = 0;
19019 info_ptr += bytes_read;
19020 break;
19021 }
19022 /* FALLTHROUGH */
19023 case DW_FORM_GNU_strp_alt:
19024 {
19025 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19026 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19027 &bytes_read);
19028
19029 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19030 dwz, str_offset);
19031 DW_STRING_IS_CANONICAL (attr) = 0;
19032 info_ptr += bytes_read;
19033 }
19034 break;
19035 case DW_FORM_exprloc:
19036 case DW_FORM_block:
19037 blk = dwarf_alloc_block (cu);
19038 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19039 info_ptr += bytes_read;
19040 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19041 info_ptr += blk->size;
19042 DW_BLOCK (attr) = blk;
19043 break;
19044 case DW_FORM_block1:
19045 blk = dwarf_alloc_block (cu);
19046 blk->size = read_1_byte (abfd, info_ptr);
19047 info_ptr += 1;
19048 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19049 info_ptr += blk->size;
19050 DW_BLOCK (attr) = blk;
19051 break;
19052 case DW_FORM_data1:
19053 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19054 info_ptr += 1;
19055 break;
19056 case DW_FORM_flag:
19057 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19058 info_ptr += 1;
19059 break;
19060 case DW_FORM_flag_present:
19061 DW_UNSND (attr) = 1;
19062 break;
19063 case DW_FORM_sdata:
19064 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19065 info_ptr += bytes_read;
19066 break;
19067 case DW_FORM_udata:
19068 case DW_FORM_rnglistx:
19069 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19070 info_ptr += bytes_read;
19071 break;
19072 case DW_FORM_ref1:
19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19074 + read_1_byte (abfd, info_ptr));
19075 info_ptr += 1;
19076 break;
19077 case DW_FORM_ref2:
19078 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19079 + read_2_bytes (abfd, info_ptr));
19080 info_ptr += 2;
19081 break;
19082 case DW_FORM_ref4:
19083 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19084 + read_4_bytes (abfd, info_ptr));
19085 info_ptr += 4;
19086 break;
19087 case DW_FORM_ref8:
19088 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19089 + read_8_bytes (abfd, info_ptr));
19090 info_ptr += 8;
19091 break;
19092 case DW_FORM_ref_sig8:
19093 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19094 info_ptr += 8;
19095 break;
19096 case DW_FORM_ref_udata:
19097 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19098 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19099 info_ptr += bytes_read;
19100 break;
19101 case DW_FORM_indirect:
19102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19103 info_ptr += bytes_read;
19104 if (form == DW_FORM_implicit_const)
19105 {
19106 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19107 info_ptr += bytes_read;
19108 }
19109 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19110 info_ptr, need_reprocess);
19111 break;
19112 case DW_FORM_implicit_const:
19113 DW_SND (attr) = implicit_const;
19114 break;
19115 case DW_FORM_addrx:
19116 case DW_FORM_GNU_addr_index:
19117 *need_reprocess = true;
19118 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19119 info_ptr += bytes_read;
19120 break;
19121 case DW_FORM_strx:
19122 case DW_FORM_strx1:
19123 case DW_FORM_strx2:
19124 case DW_FORM_strx3:
19125 case DW_FORM_strx4:
19126 case DW_FORM_GNU_str_index:
19127 {
19128 ULONGEST str_index;
19129 if (form == DW_FORM_strx1)
19130 {
19131 str_index = read_1_byte (abfd, info_ptr);
19132 info_ptr += 1;
19133 }
19134 else if (form == DW_FORM_strx2)
19135 {
19136 str_index = read_2_bytes (abfd, info_ptr);
19137 info_ptr += 2;
19138 }
19139 else if (form == DW_FORM_strx3)
19140 {
19141 str_index = read_3_bytes (abfd, info_ptr);
19142 info_ptr += 3;
19143 }
19144 else if (form == DW_FORM_strx4)
19145 {
19146 str_index = read_4_bytes (abfd, info_ptr);
19147 info_ptr += 4;
19148 }
19149 else
19150 {
19151 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19152 info_ptr += bytes_read;
19153 }
19154 *need_reprocess = true;
19155 DW_UNSND (attr) = str_index;
19156 }
19157 break;
19158 default:
19159 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19160 dwarf_form_name (form),
19161 bfd_get_filename (abfd));
19162 }
19163
19164 /* Super hack. */
19165 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19166 attr->form = DW_FORM_GNU_ref_alt;
19167
19168 /* We have seen instances where the compiler tried to emit a byte
19169 size attribute of -1 which ended up being encoded as an unsigned
19170 0xffffffff. Although 0xffffffff is technically a valid size value,
19171 an object of this size seems pretty unlikely so we can relatively
19172 safely treat these cases as if the size attribute was invalid and
19173 treat them as zero by default. */
19174 if (attr->name == DW_AT_byte_size
19175 && form == DW_FORM_data4
19176 && DW_UNSND (attr) >= 0xffffffff)
19177 {
19178 complaint
19179 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19180 hex_string (DW_UNSND (attr)));
19181 DW_UNSND (attr) = 0;
19182 }
19183
19184 return info_ptr;
19185 }
19186
19187 /* Read an attribute described by an abbreviated attribute. */
19188
19189 static const gdb_byte *
19190 read_attribute (const struct die_reader_specs *reader,
19191 struct attribute *attr, struct attr_abbrev *abbrev,
19192 const gdb_byte *info_ptr, bool *need_reprocess)
19193 {
19194 attr->name = abbrev->name;
19195 return read_attribute_value (reader, attr, abbrev->form,
19196 abbrev->implicit_const, info_ptr,
19197 need_reprocess);
19198 }
19199
19200 static CORE_ADDR
19201 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19202 unsigned int *bytes_read)
19203 {
19204 struct comp_unit_head *cu_header = &cu->header;
19205 CORE_ADDR retval = 0;
19206
19207 if (cu_header->signed_addr_p)
19208 {
19209 switch (cu_header->addr_size)
19210 {
19211 case 2:
19212 retval = bfd_get_signed_16 (abfd, buf);
19213 break;
19214 case 4:
19215 retval = bfd_get_signed_32 (abfd, buf);
19216 break;
19217 case 8:
19218 retval = bfd_get_signed_64 (abfd, buf);
19219 break;
19220 default:
19221 internal_error (__FILE__, __LINE__,
19222 _("read_address: bad switch, signed [in module %s]"),
19223 bfd_get_filename (abfd));
19224 }
19225 }
19226 else
19227 {
19228 switch (cu_header->addr_size)
19229 {
19230 case 2:
19231 retval = bfd_get_16 (abfd, buf);
19232 break;
19233 case 4:
19234 retval = bfd_get_32 (abfd, buf);
19235 break;
19236 case 8:
19237 retval = bfd_get_64 (abfd, buf);
19238 break;
19239 default:
19240 internal_error (__FILE__, __LINE__,
19241 _("read_address: bad switch, "
19242 "unsigned [in module %s]"),
19243 bfd_get_filename (abfd));
19244 }
19245 }
19246
19247 *bytes_read = cu_header->addr_size;
19248 return retval;
19249 }
19250
19251 /* Read the initial length from a section. The (draft) DWARF 3
19252 specification allows the initial length to take up either 4 bytes
19253 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19254 bytes describe the length and all offsets will be 8 bytes in length
19255 instead of 4.
19256
19257 An older, non-standard 64-bit format is also handled by this
19258 function. The older format in question stores the initial length
19259 as an 8-byte quantity without an escape value. Lengths greater
19260 than 2^32 aren't very common which means that the initial 4 bytes
19261 is almost always zero. Since a length value of zero doesn't make
19262 sense for the 32-bit format, this initial zero can be considered to
19263 be an escape value which indicates the presence of the older 64-bit
19264 format. As written, the code can't detect (old format) lengths
19265 greater than 4GB. If it becomes necessary to handle lengths
19266 somewhat larger than 4GB, we could allow other small values (such
19267 as the non-sensical values of 1, 2, and 3) to also be used as
19268 escape values indicating the presence of the old format.
19269
19270 The value returned via bytes_read should be used to increment the
19271 relevant pointer after calling read_initial_length().
19272
19273 [ Note: read_initial_length() and read_offset() are based on the
19274 document entitled "DWARF Debugging Information Format", revision
19275 3, draft 8, dated November 19, 2001. This document was obtained
19276 from:
19277
19278 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19279
19280 This document is only a draft and is subject to change. (So beware.)
19281
19282 Details regarding the older, non-standard 64-bit format were
19283 determined empirically by examining 64-bit ELF files produced by
19284 the SGI toolchain on an IRIX 6.5 machine.
19285
19286 - Kevin, July 16, 2002
19287 ] */
19288
19289 static LONGEST
19290 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19291 {
19292 LONGEST length = bfd_get_32 (abfd, buf);
19293
19294 if (length == 0xffffffff)
19295 {
19296 length = bfd_get_64 (abfd, buf + 4);
19297 *bytes_read = 12;
19298 }
19299 else if (length == 0)
19300 {
19301 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19302 length = bfd_get_64 (abfd, buf);
19303 *bytes_read = 8;
19304 }
19305 else
19306 {
19307 *bytes_read = 4;
19308 }
19309
19310 return length;
19311 }
19312
19313 /* Cover function for read_initial_length.
19314 Returns the length of the object at BUF, and stores the size of the
19315 initial length in *BYTES_READ and stores the size that offsets will be in
19316 *OFFSET_SIZE.
19317 If the initial length size is not equivalent to that specified in
19318 CU_HEADER then issue a complaint.
19319 This is useful when reading non-comp-unit headers. */
19320
19321 static LONGEST
19322 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19323 const struct comp_unit_head *cu_header,
19324 unsigned int *bytes_read,
19325 unsigned int *offset_size)
19326 {
19327 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19328
19329 gdb_assert (cu_header->initial_length_size == 4
19330 || cu_header->initial_length_size == 8
19331 || cu_header->initial_length_size == 12);
19332
19333 if (cu_header->initial_length_size != *bytes_read)
19334 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19335
19336 *offset_size = (*bytes_read == 4) ? 4 : 8;
19337 return length;
19338 }
19339
19340 /* Read an offset from the data stream. The size of the offset is
19341 given by cu_header->offset_size. */
19342
19343 static LONGEST
19344 read_offset (bfd *abfd, const gdb_byte *buf,
19345 const struct comp_unit_head *cu_header,
19346 unsigned int *bytes_read)
19347 {
19348 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19349
19350 *bytes_read = cu_header->offset_size;
19351 return offset;
19352 }
19353
19354 /* Read an offset from the data stream. */
19355
19356 static LONGEST
19357 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19358 {
19359 LONGEST retval = 0;
19360
19361 switch (offset_size)
19362 {
19363 case 4:
19364 retval = bfd_get_32 (abfd, buf);
19365 break;
19366 case 8:
19367 retval = bfd_get_64 (abfd, buf);
19368 break;
19369 default:
19370 internal_error (__FILE__, __LINE__,
19371 _("read_offset_1: bad switch [in module %s]"),
19372 bfd_get_filename (abfd));
19373 }
19374
19375 return retval;
19376 }
19377
19378 static const gdb_byte *
19379 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19380 {
19381 /* If the size of a host char is 8 bits, we can return a pointer
19382 to the buffer, otherwise we have to copy the data to a buffer
19383 allocated on the temporary obstack. */
19384 gdb_assert (HOST_CHAR_BIT == 8);
19385 return buf;
19386 }
19387
19388 static const char *
19389 read_direct_string (bfd *abfd, const gdb_byte *buf,
19390 unsigned int *bytes_read_ptr)
19391 {
19392 /* If the size of a host char is 8 bits, we can return a pointer
19393 to the string, otherwise we have to copy the string to a buffer
19394 allocated on the temporary obstack. */
19395 gdb_assert (HOST_CHAR_BIT == 8);
19396 if (*buf == '\0')
19397 {
19398 *bytes_read_ptr = 1;
19399 return NULL;
19400 }
19401 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19402 return (const char *) buf;
19403 }
19404
19405 /* Return pointer to string at section SECT offset STR_OFFSET with error
19406 reporting strings FORM_NAME and SECT_NAME. */
19407
19408 static const char *
19409 read_indirect_string_at_offset_from (struct objfile *objfile,
19410 bfd *abfd, LONGEST str_offset,
19411 struct dwarf2_section_info *sect,
19412 const char *form_name,
19413 const char *sect_name)
19414 {
19415 sect->read (objfile);
19416 if (sect->buffer == NULL)
19417 error (_("%s used without %s section [in module %s]"),
19418 form_name, sect_name, bfd_get_filename (abfd));
19419 if (str_offset >= sect->size)
19420 error (_("%s pointing outside of %s section [in module %s]"),
19421 form_name, sect_name, bfd_get_filename (abfd));
19422 gdb_assert (HOST_CHAR_BIT == 8);
19423 if (sect->buffer[str_offset] == '\0')
19424 return NULL;
19425 return (const char *) (sect->buffer + str_offset);
19426 }
19427
19428 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19429
19430 static const char *
19431 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19432 bfd *abfd, LONGEST str_offset)
19433 {
19434 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19435 abfd, str_offset,
19436 &dwarf2_per_objfile->str,
19437 "DW_FORM_strp", ".debug_str");
19438 }
19439
19440 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19441
19442 static const char *
19443 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19444 bfd *abfd, LONGEST str_offset)
19445 {
19446 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19447 abfd, str_offset,
19448 &dwarf2_per_objfile->line_str,
19449 "DW_FORM_line_strp",
19450 ".debug_line_str");
19451 }
19452
19453 /* Read a string at offset STR_OFFSET in the .debug_str section from
19454 the .dwz file DWZ. Throw an error if the offset is too large. If
19455 the string consists of a single NUL byte, return NULL; otherwise
19456 return a pointer to the string. */
19457
19458 static const char *
19459 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19460 LONGEST str_offset)
19461 {
19462 dwz->str.read (objfile);
19463
19464 if (dwz->str.buffer == NULL)
19465 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19466 "section [in module %s]"),
19467 bfd_get_filename (dwz->dwz_bfd.get ()));
19468 if (str_offset >= dwz->str.size)
19469 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19470 ".debug_str section [in module %s]"),
19471 bfd_get_filename (dwz->dwz_bfd.get ()));
19472 gdb_assert (HOST_CHAR_BIT == 8);
19473 if (dwz->str.buffer[str_offset] == '\0')
19474 return NULL;
19475 return (const char *) (dwz->str.buffer + str_offset);
19476 }
19477
19478 /* Return pointer to string at .debug_str offset as read from BUF.
19479 BUF is assumed to be in a compilation unit described by CU_HEADER.
19480 Return *BYTES_READ_PTR count of bytes read from BUF. */
19481
19482 static const char *
19483 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19484 const gdb_byte *buf,
19485 const struct comp_unit_head *cu_header,
19486 unsigned int *bytes_read_ptr)
19487 {
19488 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19489
19490 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19491 }
19492
19493 /* Return pointer to string at .debug_line_str offset as read from BUF.
19494 BUF is assumed to be in a compilation unit described by CU_HEADER.
19495 Return *BYTES_READ_PTR count of bytes read from BUF. */
19496
19497 static const char *
19498 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19499 bfd *abfd, const gdb_byte *buf,
19500 const struct comp_unit_head *cu_header,
19501 unsigned int *bytes_read_ptr)
19502 {
19503 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19504
19505 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19506 str_offset);
19507 }
19508
19509 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19510 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19511 ADDR_SIZE is the size of addresses from the CU header. */
19512
19513 static CORE_ADDR
19514 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19515 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19516 int addr_size)
19517 {
19518 struct objfile *objfile = dwarf2_per_objfile->objfile;
19519 bfd *abfd = objfile->obfd;
19520 const gdb_byte *info_ptr;
19521 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19522
19523 dwarf2_per_objfile->addr.read (objfile);
19524 if (dwarf2_per_objfile->addr.buffer == NULL)
19525 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19526 objfile_name (objfile));
19527 if (addr_base_or_zero + addr_index * addr_size
19528 >= dwarf2_per_objfile->addr.size)
19529 error (_("DW_FORM_addr_index pointing outside of "
19530 ".debug_addr section [in module %s]"),
19531 objfile_name (objfile));
19532 info_ptr = (dwarf2_per_objfile->addr.buffer
19533 + addr_base_or_zero + addr_index * addr_size);
19534 if (addr_size == 4)
19535 return bfd_get_32 (abfd, info_ptr);
19536 else
19537 return bfd_get_64 (abfd, info_ptr);
19538 }
19539
19540 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19541
19542 static CORE_ADDR
19543 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19544 {
19545 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19546 cu->addr_base, cu->header.addr_size);
19547 }
19548
19549 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19550
19551 static CORE_ADDR
19552 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19553 unsigned int *bytes_read)
19554 {
19555 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19556 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19557
19558 return read_addr_index (cu, addr_index);
19559 }
19560
19561 /* Given an index in .debug_addr, fetch the value.
19562 NOTE: This can be called during dwarf expression evaluation,
19563 long after the debug information has been read, and thus per_cu->cu
19564 may no longer exist. */
19565
19566 CORE_ADDR
19567 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19568 unsigned int addr_index)
19569 {
19570 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19571 struct dwarf2_cu *cu = per_cu->cu;
19572 gdb::optional<ULONGEST> addr_base;
19573 int addr_size;
19574
19575 /* We need addr_base and addr_size.
19576 If we don't have PER_CU->cu, we have to get it.
19577 Nasty, but the alternative is storing the needed info in PER_CU,
19578 which at this point doesn't seem justified: it's not clear how frequently
19579 it would get used and it would increase the size of every PER_CU.
19580 Entry points like dwarf2_per_cu_addr_size do a similar thing
19581 so we're not in uncharted territory here.
19582 Alas we need to be a bit more complicated as addr_base is contained
19583 in the DIE.
19584
19585 We don't need to read the entire CU(/TU).
19586 We just need the header and top level die.
19587
19588 IWBN to use the aging mechanism to let us lazily later discard the CU.
19589 For now we skip this optimization. */
19590
19591 if (cu != NULL)
19592 {
19593 addr_base = cu->addr_base;
19594 addr_size = cu->header.addr_size;
19595 }
19596 else
19597 {
19598 cutu_reader reader (per_cu, NULL, 0, 0, false);
19599 addr_base = reader.cu->addr_base;
19600 addr_size = reader.cu->header.addr_size;
19601 }
19602
19603 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19604 addr_size);
19605 }
19606
19607 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19608 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19609 DWO file. */
19610
19611 static const char *
19612 read_str_index (struct dwarf2_cu *cu,
19613 struct dwarf2_section_info *str_section,
19614 struct dwarf2_section_info *str_offsets_section,
19615 ULONGEST str_offsets_base, ULONGEST str_index)
19616 {
19617 struct dwarf2_per_objfile *dwarf2_per_objfile
19618 = cu->per_cu->dwarf2_per_objfile;
19619 struct objfile *objfile = dwarf2_per_objfile->objfile;
19620 const char *objf_name = objfile_name (objfile);
19621 bfd *abfd = objfile->obfd;
19622 const gdb_byte *info_ptr;
19623 ULONGEST str_offset;
19624 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19625
19626 str_section->read (objfile);
19627 str_offsets_section->read (objfile);
19628 if (str_section->buffer == NULL)
19629 error (_("%s used without %s section"
19630 " in CU at offset %s [in module %s]"),
19631 form_name, str_section->get_name (),
19632 sect_offset_str (cu->header.sect_off), objf_name);
19633 if (str_offsets_section->buffer == NULL)
19634 error (_("%s used without %s section"
19635 " in CU at offset %s [in module %s]"),
19636 form_name, str_section->get_name (),
19637 sect_offset_str (cu->header.sect_off), objf_name);
19638 info_ptr = (str_offsets_section->buffer
19639 + str_offsets_base
19640 + str_index * cu->header.offset_size);
19641 if (cu->header.offset_size == 4)
19642 str_offset = bfd_get_32 (abfd, info_ptr);
19643 else
19644 str_offset = bfd_get_64 (abfd, info_ptr);
19645 if (str_offset >= str_section->size)
19646 error (_("Offset from %s pointing outside of"
19647 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19648 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19649 return (const char *) (str_section->buffer + str_offset);
19650 }
19651
19652 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19653
19654 static const char *
19655 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19656 {
19657 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19658 ? reader->cu->header.addr_size : 0;
19659 return read_str_index (reader->cu,
19660 &reader->dwo_file->sections.str,
19661 &reader->dwo_file->sections.str_offsets,
19662 str_offsets_base, str_index);
19663 }
19664
19665 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19666
19667 static const char *
19668 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19669 {
19670 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19671 const char *objf_name = objfile_name (objfile);
19672 static const char form_name[] = "DW_FORM_GNU_str_index";
19673 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19674
19675 if (!cu->str_offsets_base.has_value ())
19676 error (_("%s used in Fission stub without %s"
19677 " in CU at offset 0x%lx [in module %s]"),
19678 form_name, str_offsets_attr_name,
19679 (long) cu->header.offset_size, objf_name);
19680
19681 return read_str_index (cu,
19682 &cu->per_cu->dwarf2_per_objfile->str,
19683 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19684 *cu->str_offsets_base, str_index);
19685 }
19686
19687 /* Return the length of an LEB128 number in BUF. */
19688
19689 static int
19690 leb128_size (const gdb_byte *buf)
19691 {
19692 const gdb_byte *begin = buf;
19693 gdb_byte byte;
19694
19695 while (1)
19696 {
19697 byte = *buf++;
19698 if ((byte & 128) == 0)
19699 return buf - begin;
19700 }
19701 }
19702
19703 static void
19704 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19705 {
19706 switch (lang)
19707 {
19708 case DW_LANG_C89:
19709 case DW_LANG_C99:
19710 case DW_LANG_C11:
19711 case DW_LANG_C:
19712 case DW_LANG_UPC:
19713 cu->language = language_c;
19714 break;
19715 case DW_LANG_Java:
19716 case DW_LANG_C_plus_plus:
19717 case DW_LANG_C_plus_plus_11:
19718 case DW_LANG_C_plus_plus_14:
19719 cu->language = language_cplus;
19720 break;
19721 case DW_LANG_D:
19722 cu->language = language_d;
19723 break;
19724 case DW_LANG_Fortran77:
19725 case DW_LANG_Fortran90:
19726 case DW_LANG_Fortran95:
19727 case DW_LANG_Fortran03:
19728 case DW_LANG_Fortran08:
19729 cu->language = language_fortran;
19730 break;
19731 case DW_LANG_Go:
19732 cu->language = language_go;
19733 break;
19734 case DW_LANG_Mips_Assembler:
19735 cu->language = language_asm;
19736 break;
19737 case DW_LANG_Ada83:
19738 case DW_LANG_Ada95:
19739 cu->language = language_ada;
19740 break;
19741 case DW_LANG_Modula2:
19742 cu->language = language_m2;
19743 break;
19744 case DW_LANG_Pascal83:
19745 cu->language = language_pascal;
19746 break;
19747 case DW_LANG_ObjC:
19748 cu->language = language_objc;
19749 break;
19750 case DW_LANG_Rust:
19751 case DW_LANG_Rust_old:
19752 cu->language = language_rust;
19753 break;
19754 case DW_LANG_Cobol74:
19755 case DW_LANG_Cobol85:
19756 default:
19757 cu->language = language_minimal;
19758 break;
19759 }
19760 cu->language_defn = language_def (cu->language);
19761 }
19762
19763 /* Return the named attribute or NULL if not there. */
19764
19765 static struct attribute *
19766 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19767 {
19768 for (;;)
19769 {
19770 unsigned int i;
19771 struct attribute *spec = NULL;
19772
19773 for (i = 0; i < die->num_attrs; ++i)
19774 {
19775 if (die->attrs[i].name == name)
19776 return &die->attrs[i];
19777 if (die->attrs[i].name == DW_AT_specification
19778 || die->attrs[i].name == DW_AT_abstract_origin)
19779 spec = &die->attrs[i];
19780 }
19781
19782 if (!spec)
19783 break;
19784
19785 die = follow_die_ref (die, spec, &cu);
19786 }
19787
19788 return NULL;
19789 }
19790
19791 /* Return the named attribute or NULL if not there,
19792 but do not follow DW_AT_specification, etc.
19793 This is for use in contexts where we're reading .debug_types dies.
19794 Following DW_AT_specification, DW_AT_abstract_origin will take us
19795 back up the chain, and we want to go down. */
19796
19797 static struct attribute *
19798 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19799 {
19800 unsigned int i;
19801
19802 for (i = 0; i < die->num_attrs; ++i)
19803 if (die->attrs[i].name == name)
19804 return &die->attrs[i];
19805
19806 return NULL;
19807 }
19808
19809 /* Return the string associated with a string-typed attribute, or NULL if it
19810 is either not found or is of an incorrect type. */
19811
19812 static const char *
19813 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19814 {
19815 struct attribute *attr;
19816 const char *str = NULL;
19817
19818 attr = dwarf2_attr (die, name, cu);
19819
19820 if (attr != NULL)
19821 {
19822 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19823 || attr->form == DW_FORM_string
19824 || attr->form == DW_FORM_strx
19825 || attr->form == DW_FORM_strx1
19826 || attr->form == DW_FORM_strx2
19827 || attr->form == DW_FORM_strx3
19828 || attr->form == DW_FORM_strx4
19829 || attr->form == DW_FORM_GNU_str_index
19830 || attr->form == DW_FORM_GNU_strp_alt)
19831 str = DW_STRING (attr);
19832 else
19833 complaint (_("string type expected for attribute %s for "
19834 "DIE at %s in module %s"),
19835 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19836 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19837 }
19838
19839 return str;
19840 }
19841
19842 /* Return the dwo name or NULL if not present. If present, it is in either
19843 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19844 static const char *
19845 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19846 {
19847 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19848 if (dwo_name == nullptr)
19849 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19850 return dwo_name;
19851 }
19852
19853 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19854 and holds a non-zero value. This function should only be used for
19855 DW_FORM_flag or DW_FORM_flag_present attributes. */
19856
19857 static int
19858 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19859 {
19860 struct attribute *attr = dwarf2_attr (die, name, cu);
19861
19862 return (attr && DW_UNSND (attr));
19863 }
19864
19865 static int
19866 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19867 {
19868 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19869 which value is non-zero. However, we have to be careful with
19870 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19871 (via dwarf2_flag_true_p) follows this attribute. So we may
19872 end up accidently finding a declaration attribute that belongs
19873 to a different DIE referenced by the specification attribute,
19874 even though the given DIE does not have a declaration attribute. */
19875 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19876 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19877 }
19878
19879 /* Return the die giving the specification for DIE, if there is
19880 one. *SPEC_CU is the CU containing DIE on input, and the CU
19881 containing the return value on output. If there is no
19882 specification, but there is an abstract origin, that is
19883 returned. */
19884
19885 static struct die_info *
19886 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19887 {
19888 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19889 *spec_cu);
19890
19891 if (spec_attr == NULL)
19892 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19893
19894 if (spec_attr == NULL)
19895 return NULL;
19896 else
19897 return follow_die_ref (die, spec_attr, spec_cu);
19898 }
19899
19900 /* Stub for free_line_header to match void * callback types. */
19901
19902 static void
19903 free_line_header_voidp (void *arg)
19904 {
19905 struct line_header *lh = (struct line_header *) arg;
19906
19907 delete lh;
19908 }
19909
19910 void
19911 line_header::add_include_dir (const char *include_dir)
19912 {
19913 if (dwarf_line_debug >= 2)
19914 {
19915 size_t new_size;
19916 if (version >= 5)
19917 new_size = m_include_dirs.size ();
19918 else
19919 new_size = m_include_dirs.size () + 1;
19920 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19921 new_size, include_dir);
19922 }
19923 m_include_dirs.push_back (include_dir);
19924 }
19925
19926 void
19927 line_header::add_file_name (const char *name,
19928 dir_index d_index,
19929 unsigned int mod_time,
19930 unsigned int length)
19931 {
19932 if (dwarf_line_debug >= 2)
19933 {
19934 size_t new_size;
19935 if (version >= 5)
19936 new_size = file_names_size ();
19937 else
19938 new_size = file_names_size () + 1;
19939 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19940 new_size, name);
19941 }
19942 m_file_names.emplace_back (name, d_index, mod_time, length);
19943 }
19944
19945 /* A convenience function to find the proper .debug_line section for a CU. */
19946
19947 static struct dwarf2_section_info *
19948 get_debug_line_section (struct dwarf2_cu *cu)
19949 {
19950 struct dwarf2_section_info *section;
19951 struct dwarf2_per_objfile *dwarf2_per_objfile
19952 = cu->per_cu->dwarf2_per_objfile;
19953
19954 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19955 DWO file. */
19956 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19957 section = &cu->dwo_unit->dwo_file->sections.line;
19958 else if (cu->per_cu->is_dwz)
19959 {
19960 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19961
19962 section = &dwz->line;
19963 }
19964 else
19965 section = &dwarf2_per_objfile->line;
19966
19967 return section;
19968 }
19969
19970 /* Read directory or file name entry format, starting with byte of
19971 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19972 entries count and the entries themselves in the described entry
19973 format. */
19974
19975 static void
19976 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19977 bfd *abfd, const gdb_byte **bufp,
19978 struct line_header *lh,
19979 const struct comp_unit_head *cu_header,
19980 void (*callback) (struct line_header *lh,
19981 const char *name,
19982 dir_index d_index,
19983 unsigned int mod_time,
19984 unsigned int length))
19985 {
19986 gdb_byte format_count, formati;
19987 ULONGEST data_count, datai;
19988 const gdb_byte *buf = *bufp;
19989 const gdb_byte *format_header_data;
19990 unsigned int bytes_read;
19991
19992 format_count = read_1_byte (abfd, buf);
19993 buf += 1;
19994 format_header_data = buf;
19995 for (formati = 0; formati < format_count; formati++)
19996 {
19997 read_unsigned_leb128 (abfd, buf, &bytes_read);
19998 buf += bytes_read;
19999 read_unsigned_leb128 (abfd, buf, &bytes_read);
20000 buf += bytes_read;
20001 }
20002
20003 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20004 buf += bytes_read;
20005 for (datai = 0; datai < data_count; datai++)
20006 {
20007 const gdb_byte *format = format_header_data;
20008 struct file_entry fe;
20009
20010 for (formati = 0; formati < format_count; formati++)
20011 {
20012 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20013 format += bytes_read;
20014
20015 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20016 format += bytes_read;
20017
20018 gdb::optional<const char *> string;
20019 gdb::optional<unsigned int> uint;
20020
20021 switch (form)
20022 {
20023 case DW_FORM_string:
20024 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20025 buf += bytes_read;
20026 break;
20027
20028 case DW_FORM_line_strp:
20029 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20030 abfd, buf,
20031 cu_header,
20032 &bytes_read));
20033 buf += bytes_read;
20034 break;
20035
20036 case DW_FORM_data1:
20037 uint.emplace (read_1_byte (abfd, buf));
20038 buf += 1;
20039 break;
20040
20041 case DW_FORM_data2:
20042 uint.emplace (read_2_bytes (abfd, buf));
20043 buf += 2;
20044 break;
20045
20046 case DW_FORM_data4:
20047 uint.emplace (read_4_bytes (abfd, buf));
20048 buf += 4;
20049 break;
20050
20051 case DW_FORM_data8:
20052 uint.emplace (read_8_bytes (abfd, buf));
20053 buf += 8;
20054 break;
20055
20056 case DW_FORM_data16:
20057 /* This is used for MD5, but file_entry does not record MD5s. */
20058 buf += 16;
20059 break;
20060
20061 case DW_FORM_udata:
20062 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20063 buf += bytes_read;
20064 break;
20065
20066 case DW_FORM_block:
20067 /* It is valid only for DW_LNCT_timestamp which is ignored by
20068 current GDB. */
20069 break;
20070 }
20071
20072 switch (content_type)
20073 {
20074 case DW_LNCT_path:
20075 if (string.has_value ())
20076 fe.name = *string;
20077 break;
20078 case DW_LNCT_directory_index:
20079 if (uint.has_value ())
20080 fe.d_index = (dir_index) *uint;
20081 break;
20082 case DW_LNCT_timestamp:
20083 if (uint.has_value ())
20084 fe.mod_time = *uint;
20085 break;
20086 case DW_LNCT_size:
20087 if (uint.has_value ())
20088 fe.length = *uint;
20089 break;
20090 case DW_LNCT_MD5:
20091 break;
20092 default:
20093 complaint (_("Unknown format content type %s"),
20094 pulongest (content_type));
20095 }
20096 }
20097
20098 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20099 }
20100
20101 *bufp = buf;
20102 }
20103
20104 /* Read the statement program header starting at OFFSET in
20105 .debug_line, or .debug_line.dwo. Return a pointer
20106 to a struct line_header, allocated using xmalloc.
20107 Returns NULL if there is a problem reading the header, e.g., if it
20108 has a version we don't understand.
20109
20110 NOTE: the strings in the include directory and file name tables of
20111 the returned object point into the dwarf line section buffer,
20112 and must not be freed. */
20113
20114 static line_header_up
20115 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20116 {
20117 const gdb_byte *line_ptr;
20118 unsigned int bytes_read, offset_size;
20119 int i;
20120 const char *cur_dir, *cur_file;
20121 struct dwarf2_section_info *section;
20122 bfd *abfd;
20123 struct dwarf2_per_objfile *dwarf2_per_objfile
20124 = cu->per_cu->dwarf2_per_objfile;
20125
20126 section = get_debug_line_section (cu);
20127 section->read (dwarf2_per_objfile->objfile);
20128 if (section->buffer == NULL)
20129 {
20130 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20131 complaint (_("missing .debug_line.dwo section"));
20132 else
20133 complaint (_("missing .debug_line section"));
20134 return 0;
20135 }
20136
20137 /* We can't do this until we know the section is non-empty.
20138 Only then do we know we have such a section. */
20139 abfd = section->get_bfd_owner ();
20140
20141 /* Make sure that at least there's room for the total_length field.
20142 That could be 12 bytes long, but we're just going to fudge that. */
20143 if (to_underlying (sect_off) + 4 >= section->size)
20144 {
20145 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20146 return 0;
20147 }
20148
20149 line_header_up lh (new line_header ());
20150
20151 lh->sect_off = sect_off;
20152 lh->offset_in_dwz = cu->per_cu->is_dwz;
20153
20154 line_ptr = section->buffer + to_underlying (sect_off);
20155
20156 /* Read in the header. */
20157 lh->total_length =
20158 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20159 &bytes_read, &offset_size);
20160 line_ptr += bytes_read;
20161
20162 const gdb_byte *start_here = line_ptr;
20163
20164 if (line_ptr + lh->total_length > (section->buffer + section->size))
20165 {
20166 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20167 return 0;
20168 }
20169 lh->statement_program_end = start_here + lh->total_length;
20170 lh->version = read_2_bytes (abfd, line_ptr);
20171 line_ptr += 2;
20172 if (lh->version > 5)
20173 {
20174 /* This is a version we don't understand. The format could have
20175 changed in ways we don't handle properly so just punt. */
20176 complaint (_("unsupported version in .debug_line section"));
20177 return NULL;
20178 }
20179 if (lh->version >= 5)
20180 {
20181 gdb_byte segment_selector_size;
20182
20183 /* Skip address size. */
20184 read_1_byte (abfd, line_ptr);
20185 line_ptr += 1;
20186
20187 segment_selector_size = read_1_byte (abfd, line_ptr);
20188 line_ptr += 1;
20189 if (segment_selector_size != 0)
20190 {
20191 complaint (_("unsupported segment selector size %u "
20192 "in .debug_line section"),
20193 segment_selector_size);
20194 return NULL;
20195 }
20196 }
20197 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20198 line_ptr += offset_size;
20199 lh->statement_program_start = line_ptr + lh->header_length;
20200 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20201 line_ptr += 1;
20202 if (lh->version >= 4)
20203 {
20204 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20205 line_ptr += 1;
20206 }
20207 else
20208 lh->maximum_ops_per_instruction = 1;
20209
20210 if (lh->maximum_ops_per_instruction == 0)
20211 {
20212 lh->maximum_ops_per_instruction = 1;
20213 complaint (_("invalid maximum_ops_per_instruction "
20214 "in `.debug_line' section"));
20215 }
20216
20217 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20218 line_ptr += 1;
20219 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20220 line_ptr += 1;
20221 lh->line_range = read_1_byte (abfd, line_ptr);
20222 line_ptr += 1;
20223 lh->opcode_base = read_1_byte (abfd, line_ptr);
20224 line_ptr += 1;
20225 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20226
20227 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20228 for (i = 1; i < lh->opcode_base; ++i)
20229 {
20230 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
20232 }
20233
20234 if (lh->version >= 5)
20235 {
20236 /* Read directory table. */
20237 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20238 &cu->header,
20239 [] (struct line_header *header, const char *name,
20240 dir_index d_index, unsigned int mod_time,
20241 unsigned int length)
20242 {
20243 header->add_include_dir (name);
20244 });
20245
20246 /* Read file name table. */
20247 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20248 &cu->header,
20249 [] (struct line_header *header, const char *name,
20250 dir_index d_index, unsigned int mod_time,
20251 unsigned int length)
20252 {
20253 header->add_file_name (name, d_index, mod_time, length);
20254 });
20255 }
20256 else
20257 {
20258 /* Read directory table. */
20259 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20260 {
20261 line_ptr += bytes_read;
20262 lh->add_include_dir (cur_dir);
20263 }
20264 line_ptr += bytes_read;
20265
20266 /* Read file name table. */
20267 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20268 {
20269 unsigned int mod_time, length;
20270 dir_index d_index;
20271
20272 line_ptr += bytes_read;
20273 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20274 line_ptr += bytes_read;
20275 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20276 line_ptr += bytes_read;
20277 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20278 line_ptr += bytes_read;
20279
20280 lh->add_file_name (cur_file, d_index, mod_time, length);
20281 }
20282 line_ptr += bytes_read;
20283 }
20284
20285 if (line_ptr > (section->buffer + section->size))
20286 complaint (_("line number info header doesn't "
20287 "fit in `.debug_line' section"));
20288
20289 return lh;
20290 }
20291
20292 /* Subroutine of dwarf_decode_lines to simplify it.
20293 Return the file name of the psymtab for the given file_entry.
20294 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20295 If space for the result is malloc'd, *NAME_HOLDER will be set.
20296 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20297
20298 static const char *
20299 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20300 const dwarf2_psymtab *pst,
20301 const char *comp_dir,
20302 gdb::unique_xmalloc_ptr<char> *name_holder)
20303 {
20304 const char *include_name = fe.name;
20305 const char *include_name_to_compare = include_name;
20306 const char *pst_filename;
20307 int file_is_pst;
20308
20309 const char *dir_name = fe.include_dir (lh);
20310
20311 gdb::unique_xmalloc_ptr<char> hold_compare;
20312 if (!IS_ABSOLUTE_PATH (include_name)
20313 && (dir_name != NULL || comp_dir != NULL))
20314 {
20315 /* Avoid creating a duplicate psymtab for PST.
20316 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20317 Before we do the comparison, however, we need to account
20318 for DIR_NAME and COMP_DIR.
20319 First prepend dir_name (if non-NULL). If we still don't
20320 have an absolute path prepend comp_dir (if non-NULL).
20321 However, the directory we record in the include-file's
20322 psymtab does not contain COMP_DIR (to match the
20323 corresponding symtab(s)).
20324
20325 Example:
20326
20327 bash$ cd /tmp
20328 bash$ gcc -g ./hello.c
20329 include_name = "hello.c"
20330 dir_name = "."
20331 DW_AT_comp_dir = comp_dir = "/tmp"
20332 DW_AT_name = "./hello.c"
20333
20334 */
20335
20336 if (dir_name != NULL)
20337 {
20338 name_holder->reset (concat (dir_name, SLASH_STRING,
20339 include_name, (char *) NULL));
20340 include_name = name_holder->get ();
20341 include_name_to_compare = include_name;
20342 }
20343 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20344 {
20345 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20346 include_name, (char *) NULL));
20347 include_name_to_compare = hold_compare.get ();
20348 }
20349 }
20350
20351 pst_filename = pst->filename;
20352 gdb::unique_xmalloc_ptr<char> copied_name;
20353 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20354 {
20355 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20356 pst_filename, (char *) NULL));
20357 pst_filename = copied_name.get ();
20358 }
20359
20360 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20361
20362 if (file_is_pst)
20363 return NULL;
20364 return include_name;
20365 }
20366
20367 /* State machine to track the state of the line number program. */
20368
20369 class lnp_state_machine
20370 {
20371 public:
20372 /* Initialize a machine state for the start of a line number
20373 program. */
20374 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20375 bool record_lines_p);
20376
20377 file_entry *current_file ()
20378 {
20379 /* lh->file_names is 0-based, but the file name numbers in the
20380 statement program are 1-based. */
20381 return m_line_header->file_name_at (m_file);
20382 }
20383
20384 /* Record the line in the state machine. END_SEQUENCE is true if
20385 we're processing the end of a sequence. */
20386 void record_line (bool end_sequence);
20387
20388 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20389 nop-out rest of the lines in this sequence. */
20390 void check_line_address (struct dwarf2_cu *cu,
20391 const gdb_byte *line_ptr,
20392 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20393
20394 void handle_set_discriminator (unsigned int discriminator)
20395 {
20396 m_discriminator = discriminator;
20397 m_line_has_non_zero_discriminator |= discriminator != 0;
20398 }
20399
20400 /* Handle DW_LNE_set_address. */
20401 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20402 {
20403 m_op_index = 0;
20404 address += baseaddr;
20405 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20406 }
20407
20408 /* Handle DW_LNS_advance_pc. */
20409 void handle_advance_pc (CORE_ADDR adjust);
20410
20411 /* Handle a special opcode. */
20412 void handle_special_opcode (unsigned char op_code);
20413
20414 /* Handle DW_LNS_advance_line. */
20415 void handle_advance_line (int line_delta)
20416 {
20417 advance_line (line_delta);
20418 }
20419
20420 /* Handle DW_LNS_set_file. */
20421 void handle_set_file (file_name_index file);
20422
20423 /* Handle DW_LNS_negate_stmt. */
20424 void handle_negate_stmt ()
20425 {
20426 m_is_stmt = !m_is_stmt;
20427 }
20428
20429 /* Handle DW_LNS_const_add_pc. */
20430 void handle_const_add_pc ();
20431
20432 /* Handle DW_LNS_fixed_advance_pc. */
20433 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20434 {
20435 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20436 m_op_index = 0;
20437 }
20438
20439 /* Handle DW_LNS_copy. */
20440 void handle_copy ()
20441 {
20442 record_line (false);
20443 m_discriminator = 0;
20444 }
20445
20446 /* Handle DW_LNE_end_sequence. */
20447 void handle_end_sequence ()
20448 {
20449 m_currently_recording_lines = true;
20450 }
20451
20452 private:
20453 /* Advance the line by LINE_DELTA. */
20454 void advance_line (int line_delta)
20455 {
20456 m_line += line_delta;
20457
20458 if (line_delta != 0)
20459 m_line_has_non_zero_discriminator = m_discriminator != 0;
20460 }
20461
20462 struct dwarf2_cu *m_cu;
20463
20464 gdbarch *m_gdbarch;
20465
20466 /* True if we're recording lines.
20467 Otherwise we're building partial symtabs and are just interested in
20468 finding include files mentioned by the line number program. */
20469 bool m_record_lines_p;
20470
20471 /* The line number header. */
20472 line_header *m_line_header;
20473
20474 /* These are part of the standard DWARF line number state machine,
20475 and initialized according to the DWARF spec. */
20476
20477 unsigned char m_op_index = 0;
20478 /* The line table index of the current file. */
20479 file_name_index m_file = 1;
20480 unsigned int m_line = 1;
20481
20482 /* These are initialized in the constructor. */
20483
20484 CORE_ADDR m_address;
20485 bool m_is_stmt;
20486 unsigned int m_discriminator;
20487
20488 /* Additional bits of state we need to track. */
20489
20490 /* The last file that we called dwarf2_start_subfile for.
20491 This is only used for TLLs. */
20492 unsigned int m_last_file = 0;
20493 /* The last file a line number was recorded for. */
20494 struct subfile *m_last_subfile = NULL;
20495
20496 /* When true, record the lines we decode. */
20497 bool m_currently_recording_lines = false;
20498
20499 /* The last line number that was recorded, used to coalesce
20500 consecutive entries for the same line. This can happen, for
20501 example, when discriminators are present. PR 17276. */
20502 unsigned int m_last_line = 0;
20503 bool m_line_has_non_zero_discriminator = false;
20504 };
20505
20506 void
20507 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20508 {
20509 CORE_ADDR addr_adj = (((m_op_index + adjust)
20510 / m_line_header->maximum_ops_per_instruction)
20511 * m_line_header->minimum_instruction_length);
20512 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20513 m_op_index = ((m_op_index + adjust)
20514 % m_line_header->maximum_ops_per_instruction);
20515 }
20516
20517 void
20518 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20519 {
20520 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20521 CORE_ADDR addr_adj = (((m_op_index
20522 + (adj_opcode / m_line_header->line_range))
20523 / m_line_header->maximum_ops_per_instruction)
20524 * m_line_header->minimum_instruction_length);
20525 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20526 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20527 % m_line_header->maximum_ops_per_instruction);
20528
20529 int line_delta = (m_line_header->line_base
20530 + (adj_opcode % m_line_header->line_range));
20531 advance_line (line_delta);
20532 record_line (false);
20533 m_discriminator = 0;
20534 }
20535
20536 void
20537 lnp_state_machine::handle_set_file (file_name_index file)
20538 {
20539 m_file = file;
20540
20541 const file_entry *fe = current_file ();
20542 if (fe == NULL)
20543 dwarf2_debug_line_missing_file_complaint ();
20544 else if (m_record_lines_p)
20545 {
20546 const char *dir = fe->include_dir (m_line_header);
20547
20548 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20549 m_line_has_non_zero_discriminator = m_discriminator != 0;
20550 dwarf2_start_subfile (m_cu, fe->name, dir);
20551 }
20552 }
20553
20554 void
20555 lnp_state_machine::handle_const_add_pc ()
20556 {
20557 CORE_ADDR adjust
20558 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20559
20560 CORE_ADDR addr_adj
20561 = (((m_op_index + adjust)
20562 / m_line_header->maximum_ops_per_instruction)
20563 * m_line_header->minimum_instruction_length);
20564
20565 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20566 m_op_index = ((m_op_index + adjust)
20567 % m_line_header->maximum_ops_per_instruction);
20568 }
20569
20570 /* Return non-zero if we should add LINE to the line number table.
20571 LINE is the line to add, LAST_LINE is the last line that was added,
20572 LAST_SUBFILE is the subfile for LAST_LINE.
20573 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20574 had a non-zero discriminator.
20575
20576 We have to be careful in the presence of discriminators.
20577 E.g., for this line:
20578
20579 for (i = 0; i < 100000; i++);
20580
20581 clang can emit four line number entries for that one line,
20582 each with a different discriminator.
20583 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20584
20585 However, we want gdb to coalesce all four entries into one.
20586 Otherwise the user could stepi into the middle of the line and
20587 gdb would get confused about whether the pc really was in the
20588 middle of the line.
20589
20590 Things are further complicated by the fact that two consecutive
20591 line number entries for the same line is a heuristic used by gcc
20592 to denote the end of the prologue. So we can't just discard duplicate
20593 entries, we have to be selective about it. The heuristic we use is
20594 that we only collapse consecutive entries for the same line if at least
20595 one of those entries has a non-zero discriminator. PR 17276.
20596
20597 Note: Addresses in the line number state machine can never go backwards
20598 within one sequence, thus this coalescing is ok. */
20599
20600 static int
20601 dwarf_record_line_p (struct dwarf2_cu *cu,
20602 unsigned int line, unsigned int last_line,
20603 int line_has_non_zero_discriminator,
20604 struct subfile *last_subfile)
20605 {
20606 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20607 return 1;
20608 if (line != last_line)
20609 return 1;
20610 /* Same line for the same file that we've seen already.
20611 As a last check, for pr 17276, only record the line if the line
20612 has never had a non-zero discriminator. */
20613 if (!line_has_non_zero_discriminator)
20614 return 1;
20615 return 0;
20616 }
20617
20618 /* Use the CU's builder to record line number LINE beginning at
20619 address ADDRESS in the line table of subfile SUBFILE. */
20620
20621 static void
20622 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20623 unsigned int line, CORE_ADDR address,
20624 struct dwarf2_cu *cu)
20625 {
20626 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20627
20628 if (dwarf_line_debug)
20629 {
20630 fprintf_unfiltered (gdb_stdlog,
20631 "Recording line %u, file %s, address %s\n",
20632 line, lbasename (subfile->name),
20633 paddress (gdbarch, address));
20634 }
20635
20636 if (cu != nullptr)
20637 cu->get_builder ()->record_line (subfile, line, addr);
20638 }
20639
20640 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20641 Mark the end of a set of line number records.
20642 The arguments are the same as for dwarf_record_line_1.
20643 If SUBFILE is NULL the request is ignored. */
20644
20645 static void
20646 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20647 CORE_ADDR address, struct dwarf2_cu *cu)
20648 {
20649 if (subfile == NULL)
20650 return;
20651
20652 if (dwarf_line_debug)
20653 {
20654 fprintf_unfiltered (gdb_stdlog,
20655 "Finishing current line, file %s, address %s\n",
20656 lbasename (subfile->name),
20657 paddress (gdbarch, address));
20658 }
20659
20660 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20661 }
20662
20663 void
20664 lnp_state_machine::record_line (bool end_sequence)
20665 {
20666 if (dwarf_line_debug)
20667 {
20668 fprintf_unfiltered (gdb_stdlog,
20669 "Processing actual line %u: file %u,"
20670 " address %s, is_stmt %u, discrim %u%s\n",
20671 m_line, m_file,
20672 paddress (m_gdbarch, m_address),
20673 m_is_stmt, m_discriminator,
20674 (end_sequence ? "\t(end sequence)" : ""));
20675 }
20676
20677 file_entry *fe = current_file ();
20678
20679 if (fe == NULL)
20680 dwarf2_debug_line_missing_file_complaint ();
20681 /* For now we ignore lines not starting on an instruction boundary.
20682 But not when processing end_sequence for compatibility with the
20683 previous version of the code. */
20684 else if (m_op_index == 0 || end_sequence)
20685 {
20686 fe->included_p = 1;
20687 if (m_record_lines_p
20688 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20689 {
20690 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20691 || end_sequence)
20692 {
20693 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20694 m_currently_recording_lines ? m_cu : nullptr);
20695 }
20696
20697 if (!end_sequence)
20698 {
20699 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20700 m_line_has_non_zero_discriminator,
20701 m_last_subfile))
20702 {
20703 buildsym_compunit *builder = m_cu->get_builder ();
20704 dwarf_record_line_1 (m_gdbarch,
20705 builder->get_current_subfile (),
20706 m_line, m_address,
20707 m_currently_recording_lines ? m_cu : nullptr);
20708 }
20709 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20710 m_last_line = m_line;
20711 }
20712 }
20713 }
20714 }
20715
20716 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20717 line_header *lh, bool record_lines_p)
20718 {
20719 m_cu = cu;
20720 m_gdbarch = arch;
20721 m_record_lines_p = record_lines_p;
20722 m_line_header = lh;
20723
20724 m_currently_recording_lines = true;
20725
20726 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20727 was a line entry for it so that the backend has a chance to adjust it
20728 and also record it in case it needs it. This is currently used by MIPS
20729 code, cf. `mips_adjust_dwarf2_line'. */
20730 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20731 m_is_stmt = lh->default_is_stmt;
20732 m_discriminator = 0;
20733 }
20734
20735 void
20736 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20737 const gdb_byte *line_ptr,
20738 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20739 {
20740 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20741 the pc range of the CU. However, we restrict the test to only ADDRESS
20742 values of zero to preserve GDB's previous behaviour which is to handle
20743 the specific case of a function being GC'd by the linker. */
20744
20745 if (address == 0 && address < unrelocated_lowpc)
20746 {
20747 /* This line table is for a function which has been
20748 GCd by the linker. Ignore it. PR gdb/12528 */
20749
20750 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20751 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20752
20753 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20754 line_offset, objfile_name (objfile));
20755 m_currently_recording_lines = false;
20756 /* Note: m_currently_recording_lines is left as false until we see
20757 DW_LNE_end_sequence. */
20758 }
20759 }
20760
20761 /* Subroutine of dwarf_decode_lines to simplify it.
20762 Process the line number information in LH.
20763 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20764 program in order to set included_p for every referenced header. */
20765
20766 static void
20767 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20768 const int decode_for_pst_p, CORE_ADDR lowpc)
20769 {
20770 const gdb_byte *line_ptr, *extended_end;
20771 const gdb_byte *line_end;
20772 unsigned int bytes_read, extended_len;
20773 unsigned char op_code, extended_op;
20774 CORE_ADDR baseaddr;
20775 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20776 bfd *abfd = objfile->obfd;
20777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20778 /* True if we're recording line info (as opposed to building partial
20779 symtabs and just interested in finding include files mentioned by
20780 the line number program). */
20781 bool record_lines_p = !decode_for_pst_p;
20782
20783 baseaddr = objfile->text_section_offset ();
20784
20785 line_ptr = lh->statement_program_start;
20786 line_end = lh->statement_program_end;
20787
20788 /* Read the statement sequences until there's nothing left. */
20789 while (line_ptr < line_end)
20790 {
20791 /* The DWARF line number program state machine. Reset the state
20792 machine at the start of each sequence. */
20793 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20794 bool end_sequence = false;
20795
20796 if (record_lines_p)
20797 {
20798 /* Start a subfile for the current file of the state
20799 machine. */
20800 const file_entry *fe = state_machine.current_file ();
20801
20802 if (fe != NULL)
20803 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20804 }
20805
20806 /* Decode the table. */
20807 while (line_ptr < line_end && !end_sequence)
20808 {
20809 op_code = read_1_byte (abfd, line_ptr);
20810 line_ptr += 1;
20811
20812 if (op_code >= lh->opcode_base)
20813 {
20814 /* Special opcode. */
20815 state_machine.handle_special_opcode (op_code);
20816 }
20817 else switch (op_code)
20818 {
20819 case DW_LNS_extended_op:
20820 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20821 &bytes_read);
20822 line_ptr += bytes_read;
20823 extended_end = line_ptr + extended_len;
20824 extended_op = read_1_byte (abfd, line_ptr);
20825 line_ptr += 1;
20826 switch (extended_op)
20827 {
20828 case DW_LNE_end_sequence:
20829 state_machine.handle_end_sequence ();
20830 end_sequence = true;
20831 break;
20832 case DW_LNE_set_address:
20833 {
20834 CORE_ADDR address
20835 = read_address (abfd, line_ptr, cu, &bytes_read);
20836 line_ptr += bytes_read;
20837
20838 state_machine.check_line_address (cu, line_ptr,
20839 lowpc - baseaddr, address);
20840 state_machine.handle_set_address (baseaddr, address);
20841 }
20842 break;
20843 case DW_LNE_define_file:
20844 {
20845 const char *cur_file;
20846 unsigned int mod_time, length;
20847 dir_index dindex;
20848
20849 cur_file = read_direct_string (abfd, line_ptr,
20850 &bytes_read);
20851 line_ptr += bytes_read;
20852 dindex = (dir_index)
20853 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20854 line_ptr += bytes_read;
20855 mod_time =
20856 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20857 line_ptr += bytes_read;
20858 length =
20859 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20860 line_ptr += bytes_read;
20861 lh->add_file_name (cur_file, dindex, mod_time, length);
20862 }
20863 break;
20864 case DW_LNE_set_discriminator:
20865 {
20866 /* The discriminator is not interesting to the
20867 debugger; just ignore it. We still need to
20868 check its value though:
20869 if there are consecutive entries for the same
20870 (non-prologue) line we want to coalesce them.
20871 PR 17276. */
20872 unsigned int discr
20873 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20874 line_ptr += bytes_read;
20875
20876 state_machine.handle_set_discriminator (discr);
20877 }
20878 break;
20879 default:
20880 complaint (_("mangled .debug_line section"));
20881 return;
20882 }
20883 /* Make sure that we parsed the extended op correctly. If e.g.
20884 we expected a different address size than the producer used,
20885 we may have read the wrong number of bytes. */
20886 if (line_ptr != extended_end)
20887 {
20888 complaint (_("mangled .debug_line section"));
20889 return;
20890 }
20891 break;
20892 case DW_LNS_copy:
20893 state_machine.handle_copy ();
20894 break;
20895 case DW_LNS_advance_pc:
20896 {
20897 CORE_ADDR adjust
20898 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20899 line_ptr += bytes_read;
20900
20901 state_machine.handle_advance_pc (adjust);
20902 }
20903 break;
20904 case DW_LNS_advance_line:
20905 {
20906 int line_delta
20907 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20908 line_ptr += bytes_read;
20909
20910 state_machine.handle_advance_line (line_delta);
20911 }
20912 break;
20913 case DW_LNS_set_file:
20914 {
20915 file_name_index file
20916 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20917 &bytes_read);
20918 line_ptr += bytes_read;
20919
20920 state_machine.handle_set_file (file);
20921 }
20922 break;
20923 case DW_LNS_set_column:
20924 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20925 line_ptr += bytes_read;
20926 break;
20927 case DW_LNS_negate_stmt:
20928 state_machine.handle_negate_stmt ();
20929 break;
20930 case DW_LNS_set_basic_block:
20931 break;
20932 /* Add to the address register of the state machine the
20933 address increment value corresponding to special opcode
20934 255. I.e., this value is scaled by the minimum
20935 instruction length since special opcode 255 would have
20936 scaled the increment. */
20937 case DW_LNS_const_add_pc:
20938 state_machine.handle_const_add_pc ();
20939 break;
20940 case DW_LNS_fixed_advance_pc:
20941 {
20942 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20943 line_ptr += 2;
20944
20945 state_machine.handle_fixed_advance_pc (addr_adj);
20946 }
20947 break;
20948 default:
20949 {
20950 /* Unknown standard opcode, ignore it. */
20951 int i;
20952
20953 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20954 {
20955 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20956 line_ptr += bytes_read;
20957 }
20958 }
20959 }
20960 }
20961
20962 if (!end_sequence)
20963 dwarf2_debug_line_missing_end_sequence_complaint ();
20964
20965 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20966 in which case we still finish recording the last line). */
20967 state_machine.record_line (true);
20968 }
20969 }
20970
20971 /* Decode the Line Number Program (LNP) for the given line_header
20972 structure and CU. The actual information extracted and the type
20973 of structures created from the LNP depends on the value of PST.
20974
20975 1. If PST is NULL, then this procedure uses the data from the program
20976 to create all necessary symbol tables, and their linetables.
20977
20978 2. If PST is not NULL, this procedure reads the program to determine
20979 the list of files included by the unit represented by PST, and
20980 builds all the associated partial symbol tables.
20981
20982 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20983 It is used for relative paths in the line table.
20984 NOTE: When processing partial symtabs (pst != NULL),
20985 comp_dir == pst->dirname.
20986
20987 NOTE: It is important that psymtabs have the same file name (via strcmp)
20988 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20989 symtab we don't use it in the name of the psymtabs we create.
20990 E.g. expand_line_sal requires this when finding psymtabs to expand.
20991 A good testcase for this is mb-inline.exp.
20992
20993 LOWPC is the lowest address in CU (or 0 if not known).
20994
20995 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20996 for its PC<->lines mapping information. Otherwise only the filename
20997 table is read in. */
20998
20999 static void
21000 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21001 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21002 CORE_ADDR lowpc, int decode_mapping)
21003 {
21004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21005 const int decode_for_pst_p = (pst != NULL);
21006
21007 if (decode_mapping)
21008 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21009
21010 if (decode_for_pst_p)
21011 {
21012 /* Now that we're done scanning the Line Header Program, we can
21013 create the psymtab of each included file. */
21014 for (auto &file_entry : lh->file_names ())
21015 if (file_entry.included_p == 1)
21016 {
21017 gdb::unique_xmalloc_ptr<char> name_holder;
21018 const char *include_name =
21019 psymtab_include_file_name (lh, file_entry, pst,
21020 comp_dir, &name_holder);
21021 if (include_name != NULL)
21022 dwarf2_create_include_psymtab (include_name, pst, objfile);
21023 }
21024 }
21025 else
21026 {
21027 /* Make sure a symtab is created for every file, even files
21028 which contain only variables (i.e. no code with associated
21029 line numbers). */
21030 buildsym_compunit *builder = cu->get_builder ();
21031 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21032
21033 for (auto &fe : lh->file_names ())
21034 {
21035 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21036 if (builder->get_current_subfile ()->symtab == NULL)
21037 {
21038 builder->get_current_subfile ()->symtab
21039 = allocate_symtab (cust,
21040 builder->get_current_subfile ()->name);
21041 }
21042 fe.symtab = builder->get_current_subfile ()->symtab;
21043 }
21044 }
21045 }
21046
21047 /* Start a subfile for DWARF. FILENAME is the name of the file and
21048 DIRNAME the name of the source directory which contains FILENAME
21049 or NULL if not known.
21050 This routine tries to keep line numbers from identical absolute and
21051 relative file names in a common subfile.
21052
21053 Using the `list' example from the GDB testsuite, which resides in
21054 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21055 of /srcdir/list0.c yields the following debugging information for list0.c:
21056
21057 DW_AT_name: /srcdir/list0.c
21058 DW_AT_comp_dir: /compdir
21059 files.files[0].name: list0.h
21060 files.files[0].dir: /srcdir
21061 files.files[1].name: list0.c
21062 files.files[1].dir: /srcdir
21063
21064 The line number information for list0.c has to end up in a single
21065 subfile, so that `break /srcdir/list0.c:1' works as expected.
21066 start_subfile will ensure that this happens provided that we pass the
21067 concatenation of files.files[1].dir and files.files[1].name as the
21068 subfile's name. */
21069
21070 static void
21071 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21072 const char *dirname)
21073 {
21074 gdb::unique_xmalloc_ptr<char> copy;
21075
21076 /* In order not to lose the line information directory,
21077 we concatenate it to the filename when it makes sense.
21078 Note that the Dwarf3 standard says (speaking of filenames in line
21079 information): ``The directory index is ignored for file names
21080 that represent full path names''. Thus ignoring dirname in the
21081 `else' branch below isn't an issue. */
21082
21083 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21084 {
21085 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21086 filename = copy.get ();
21087 }
21088
21089 cu->get_builder ()->start_subfile (filename);
21090 }
21091
21092 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21093 buildsym_compunit constructor. */
21094
21095 struct compunit_symtab *
21096 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21097 CORE_ADDR low_pc)
21098 {
21099 gdb_assert (m_builder == nullptr);
21100
21101 m_builder.reset (new struct buildsym_compunit
21102 (per_cu->dwarf2_per_objfile->objfile,
21103 name, comp_dir, language, low_pc));
21104
21105 list_in_scope = get_builder ()->get_file_symbols ();
21106
21107 get_builder ()->record_debugformat ("DWARF 2");
21108 get_builder ()->record_producer (producer);
21109
21110 processing_has_namespace_info = false;
21111
21112 return get_builder ()->get_compunit_symtab ();
21113 }
21114
21115 static void
21116 var_decode_location (struct attribute *attr, struct symbol *sym,
21117 struct dwarf2_cu *cu)
21118 {
21119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21120 struct comp_unit_head *cu_header = &cu->header;
21121
21122 /* NOTE drow/2003-01-30: There used to be a comment and some special
21123 code here to turn a symbol with DW_AT_external and a
21124 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21125 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21126 with some versions of binutils) where shared libraries could have
21127 relocations against symbols in their debug information - the
21128 minimal symbol would have the right address, but the debug info
21129 would not. It's no longer necessary, because we will explicitly
21130 apply relocations when we read in the debug information now. */
21131
21132 /* A DW_AT_location attribute with no contents indicates that a
21133 variable has been optimized away. */
21134 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21135 {
21136 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21137 return;
21138 }
21139
21140 /* Handle one degenerate form of location expression specially, to
21141 preserve GDB's previous behavior when section offsets are
21142 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21143 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21144
21145 if (attr->form_is_block ()
21146 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21147 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21148 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21149 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21150 && (DW_BLOCK (attr)->size
21151 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21152 {
21153 unsigned int dummy;
21154
21155 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21156 SET_SYMBOL_VALUE_ADDRESS (sym,
21157 read_address (objfile->obfd,
21158 DW_BLOCK (attr)->data + 1,
21159 cu, &dummy));
21160 else
21161 SET_SYMBOL_VALUE_ADDRESS
21162 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21163 &dummy));
21164 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21165 fixup_symbol_section (sym, objfile);
21166 SET_SYMBOL_VALUE_ADDRESS
21167 (sym,
21168 SYMBOL_VALUE_ADDRESS (sym)
21169 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21170 return;
21171 }
21172
21173 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21174 expression evaluator, and use LOC_COMPUTED only when necessary
21175 (i.e. when the value of a register or memory location is
21176 referenced, or a thread-local block, etc.). Then again, it might
21177 not be worthwhile. I'm assuming that it isn't unless performance
21178 or memory numbers show me otherwise. */
21179
21180 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21181
21182 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21183 cu->has_loclist = true;
21184 }
21185
21186 /* Given a pointer to a DWARF information entry, figure out if we need
21187 to make a symbol table entry for it, and if so, create a new entry
21188 and return a pointer to it.
21189 If TYPE is NULL, determine symbol type from the die, otherwise
21190 used the passed type.
21191 If SPACE is not NULL, use it to hold the new symbol. If it is
21192 NULL, allocate a new symbol on the objfile's obstack. */
21193
21194 static struct symbol *
21195 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21196 struct symbol *space)
21197 {
21198 struct dwarf2_per_objfile *dwarf2_per_objfile
21199 = cu->per_cu->dwarf2_per_objfile;
21200 struct objfile *objfile = dwarf2_per_objfile->objfile;
21201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21202 struct symbol *sym = NULL;
21203 const char *name;
21204 struct attribute *attr = NULL;
21205 struct attribute *attr2 = NULL;
21206 CORE_ADDR baseaddr;
21207 struct pending **list_to_add = NULL;
21208
21209 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21210
21211 baseaddr = objfile->text_section_offset ();
21212
21213 name = dwarf2_name (die, cu);
21214 if (name)
21215 {
21216 const char *linkagename;
21217 int suppress_add = 0;
21218
21219 if (space)
21220 sym = space;
21221 else
21222 sym = allocate_symbol (objfile);
21223 OBJSTAT (objfile, n_syms++);
21224
21225 /* Cache this symbol's name and the name's demangled form (if any). */
21226 sym->set_language (cu->language, &objfile->objfile_obstack);
21227 linkagename = dwarf2_physname (name, die, cu);
21228 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21229
21230 /* Fortran does not have mangling standard and the mangling does differ
21231 between gfortran, iFort etc. */
21232 if (cu->language == language_fortran
21233 && symbol_get_demangled_name (sym) == NULL)
21234 symbol_set_demangled_name (sym,
21235 dwarf2_full_name (name, die, cu),
21236 NULL);
21237
21238 /* Default assumptions.
21239 Use the passed type or decode it from the die. */
21240 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21241 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21242 if (type != NULL)
21243 SYMBOL_TYPE (sym) = type;
21244 else
21245 SYMBOL_TYPE (sym) = die_type (die, cu);
21246 attr = dwarf2_attr (die,
21247 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21248 cu);
21249 if (attr != nullptr)
21250 {
21251 SYMBOL_LINE (sym) = DW_UNSND (attr);
21252 }
21253
21254 attr = dwarf2_attr (die,
21255 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21256 cu);
21257 if (attr != nullptr)
21258 {
21259 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21260 struct file_entry *fe;
21261
21262 if (cu->line_header != NULL)
21263 fe = cu->line_header->file_name_at (file_index);
21264 else
21265 fe = NULL;
21266
21267 if (fe == NULL)
21268 complaint (_("file index out of range"));
21269 else
21270 symbol_set_symtab (sym, fe->symtab);
21271 }
21272
21273 switch (die->tag)
21274 {
21275 case DW_TAG_label:
21276 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21277 if (attr != nullptr)
21278 {
21279 CORE_ADDR addr;
21280
21281 addr = attr->value_as_address ();
21282 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21283 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21284 }
21285 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21286 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21287 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21288 add_symbol_to_list (sym, cu->list_in_scope);
21289 break;
21290 case DW_TAG_subprogram:
21291 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21292 finish_block. */
21293 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21294 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21295 if ((attr2 && (DW_UNSND (attr2) != 0))
21296 || cu->language == language_ada
21297 || cu->language == language_fortran)
21298 {
21299 /* Subprograms marked external are stored as a global symbol.
21300 Ada and Fortran subprograms, whether marked external or
21301 not, are always stored as a global symbol, because we want
21302 to be able to access them globally. For instance, we want
21303 to be able to break on a nested subprogram without having
21304 to specify the context. */
21305 list_to_add = cu->get_builder ()->get_global_symbols ();
21306 }
21307 else
21308 {
21309 list_to_add = cu->list_in_scope;
21310 }
21311 break;
21312 case DW_TAG_inlined_subroutine:
21313 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21314 finish_block. */
21315 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21316 SYMBOL_INLINED (sym) = 1;
21317 list_to_add = cu->list_in_scope;
21318 break;
21319 case DW_TAG_template_value_param:
21320 suppress_add = 1;
21321 /* Fall through. */
21322 case DW_TAG_constant:
21323 case DW_TAG_variable:
21324 case DW_TAG_member:
21325 /* Compilation with minimal debug info may result in
21326 variables with missing type entries. Change the
21327 misleading `void' type to something sensible. */
21328 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21329 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21330
21331 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21332 /* In the case of DW_TAG_member, we should only be called for
21333 static const members. */
21334 if (die->tag == DW_TAG_member)
21335 {
21336 /* dwarf2_add_field uses die_is_declaration,
21337 so we do the same. */
21338 gdb_assert (die_is_declaration (die, cu));
21339 gdb_assert (attr);
21340 }
21341 if (attr != nullptr)
21342 {
21343 dwarf2_const_value (attr, sym, cu);
21344 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21345 if (!suppress_add)
21346 {
21347 if (attr2 && (DW_UNSND (attr2) != 0))
21348 list_to_add = cu->get_builder ()->get_global_symbols ();
21349 else
21350 list_to_add = cu->list_in_scope;
21351 }
21352 break;
21353 }
21354 attr = dwarf2_attr (die, DW_AT_location, cu);
21355 if (attr != nullptr)
21356 {
21357 var_decode_location (attr, sym, cu);
21358 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21359
21360 /* Fortran explicitly imports any global symbols to the local
21361 scope by DW_TAG_common_block. */
21362 if (cu->language == language_fortran && die->parent
21363 && die->parent->tag == DW_TAG_common_block)
21364 attr2 = NULL;
21365
21366 if (SYMBOL_CLASS (sym) == LOC_STATIC
21367 && SYMBOL_VALUE_ADDRESS (sym) == 0
21368 && !dwarf2_per_objfile->has_section_at_zero)
21369 {
21370 /* When a static variable is eliminated by the linker,
21371 the corresponding debug information is not stripped
21372 out, but the variable address is set to null;
21373 do not add such variables into symbol table. */
21374 }
21375 else if (attr2 && (DW_UNSND (attr2) != 0))
21376 {
21377 if (SYMBOL_CLASS (sym) == LOC_STATIC
21378 && (objfile->flags & OBJF_MAINLINE) == 0
21379 && dwarf2_per_objfile->can_copy)
21380 {
21381 /* A global static variable might be subject to
21382 copy relocation. We first check for a local
21383 minsym, though, because maybe the symbol was
21384 marked hidden, in which case this would not
21385 apply. */
21386 bound_minimal_symbol found
21387 = (lookup_minimal_symbol_linkage
21388 (sym->linkage_name (), objfile));
21389 if (found.minsym != nullptr)
21390 sym->maybe_copied = 1;
21391 }
21392
21393 /* A variable with DW_AT_external is never static,
21394 but it may be block-scoped. */
21395 list_to_add
21396 = ((cu->list_in_scope
21397 == cu->get_builder ()->get_file_symbols ())
21398 ? cu->get_builder ()->get_global_symbols ()
21399 : cu->list_in_scope);
21400 }
21401 else
21402 list_to_add = cu->list_in_scope;
21403 }
21404 else
21405 {
21406 /* We do not know the address of this symbol.
21407 If it is an external symbol and we have type information
21408 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21409 The address of the variable will then be determined from
21410 the minimal symbol table whenever the variable is
21411 referenced. */
21412 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21413
21414 /* Fortran explicitly imports any global symbols to the local
21415 scope by DW_TAG_common_block. */
21416 if (cu->language == language_fortran && die->parent
21417 && die->parent->tag == DW_TAG_common_block)
21418 {
21419 /* SYMBOL_CLASS doesn't matter here because
21420 read_common_block is going to reset it. */
21421 if (!suppress_add)
21422 list_to_add = cu->list_in_scope;
21423 }
21424 else if (attr2 && (DW_UNSND (attr2) != 0)
21425 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21426 {
21427 /* A variable with DW_AT_external is never static, but it
21428 may be block-scoped. */
21429 list_to_add
21430 = ((cu->list_in_scope
21431 == cu->get_builder ()->get_file_symbols ())
21432 ? cu->get_builder ()->get_global_symbols ()
21433 : cu->list_in_scope);
21434
21435 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21436 }
21437 else if (!die_is_declaration (die, cu))
21438 {
21439 /* Use the default LOC_OPTIMIZED_OUT class. */
21440 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21441 if (!suppress_add)
21442 list_to_add = cu->list_in_scope;
21443 }
21444 }
21445 break;
21446 case DW_TAG_formal_parameter:
21447 {
21448 /* If we are inside a function, mark this as an argument. If
21449 not, we might be looking at an argument to an inlined function
21450 when we do not have enough information to show inlined frames;
21451 pretend it's a local variable in that case so that the user can
21452 still see it. */
21453 struct context_stack *curr
21454 = cu->get_builder ()->get_current_context_stack ();
21455 if (curr != nullptr && curr->name != nullptr)
21456 SYMBOL_IS_ARGUMENT (sym) = 1;
21457 attr = dwarf2_attr (die, DW_AT_location, cu);
21458 if (attr != nullptr)
21459 {
21460 var_decode_location (attr, sym, cu);
21461 }
21462 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21463 if (attr != nullptr)
21464 {
21465 dwarf2_const_value (attr, sym, cu);
21466 }
21467
21468 list_to_add = cu->list_in_scope;
21469 }
21470 break;
21471 case DW_TAG_unspecified_parameters:
21472 /* From varargs functions; gdb doesn't seem to have any
21473 interest in this information, so just ignore it for now.
21474 (FIXME?) */
21475 break;
21476 case DW_TAG_template_type_param:
21477 suppress_add = 1;
21478 /* Fall through. */
21479 case DW_TAG_class_type:
21480 case DW_TAG_interface_type:
21481 case DW_TAG_structure_type:
21482 case DW_TAG_union_type:
21483 case DW_TAG_set_type:
21484 case DW_TAG_enumeration_type:
21485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21486 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21487
21488 {
21489 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21490 really ever be static objects: otherwise, if you try
21491 to, say, break of a class's method and you're in a file
21492 which doesn't mention that class, it won't work unless
21493 the check for all static symbols in lookup_symbol_aux
21494 saves you. See the OtherFileClass tests in
21495 gdb.c++/namespace.exp. */
21496
21497 if (!suppress_add)
21498 {
21499 buildsym_compunit *builder = cu->get_builder ();
21500 list_to_add
21501 = (cu->list_in_scope == builder->get_file_symbols ()
21502 && cu->language == language_cplus
21503 ? builder->get_global_symbols ()
21504 : cu->list_in_scope);
21505
21506 /* The semantics of C++ state that "struct foo {
21507 ... }" also defines a typedef for "foo". */
21508 if (cu->language == language_cplus
21509 || cu->language == language_ada
21510 || cu->language == language_d
21511 || cu->language == language_rust)
21512 {
21513 /* The symbol's name is already allocated along
21514 with this objfile, so we don't need to
21515 duplicate it for the type. */
21516 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21517 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21518 }
21519 }
21520 }
21521 break;
21522 case DW_TAG_typedef:
21523 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21524 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21525 list_to_add = cu->list_in_scope;
21526 break;
21527 case DW_TAG_base_type:
21528 case DW_TAG_subrange_type:
21529 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21530 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21531 list_to_add = cu->list_in_scope;
21532 break;
21533 case DW_TAG_enumerator:
21534 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21535 if (attr != nullptr)
21536 {
21537 dwarf2_const_value (attr, sym, cu);
21538 }
21539 {
21540 /* NOTE: carlton/2003-11-10: See comment above in the
21541 DW_TAG_class_type, etc. block. */
21542
21543 list_to_add
21544 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21545 && cu->language == language_cplus
21546 ? cu->get_builder ()->get_global_symbols ()
21547 : cu->list_in_scope);
21548 }
21549 break;
21550 case DW_TAG_imported_declaration:
21551 case DW_TAG_namespace:
21552 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21553 list_to_add = cu->get_builder ()->get_global_symbols ();
21554 break;
21555 case DW_TAG_module:
21556 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21557 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21558 list_to_add = cu->get_builder ()->get_global_symbols ();
21559 break;
21560 case DW_TAG_common_block:
21561 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21562 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21563 add_symbol_to_list (sym, cu->list_in_scope);
21564 break;
21565 default:
21566 /* Not a tag we recognize. Hopefully we aren't processing
21567 trash data, but since we must specifically ignore things
21568 we don't recognize, there is nothing else we should do at
21569 this point. */
21570 complaint (_("unsupported tag: '%s'"),
21571 dwarf_tag_name (die->tag));
21572 break;
21573 }
21574
21575 if (suppress_add)
21576 {
21577 sym->hash_next = objfile->template_symbols;
21578 objfile->template_symbols = sym;
21579 list_to_add = NULL;
21580 }
21581
21582 if (list_to_add != NULL)
21583 add_symbol_to_list (sym, list_to_add);
21584
21585 /* For the benefit of old versions of GCC, check for anonymous
21586 namespaces based on the demangled name. */
21587 if (!cu->processing_has_namespace_info
21588 && cu->language == language_cplus)
21589 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21590 }
21591 return (sym);
21592 }
21593
21594 /* Given an attr with a DW_FORM_dataN value in host byte order,
21595 zero-extend it as appropriate for the symbol's type. The DWARF
21596 standard (v4) is not entirely clear about the meaning of using
21597 DW_FORM_dataN for a constant with a signed type, where the type is
21598 wider than the data. The conclusion of a discussion on the DWARF
21599 list was that this is unspecified. We choose to always zero-extend
21600 because that is the interpretation long in use by GCC. */
21601
21602 static gdb_byte *
21603 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21604 struct dwarf2_cu *cu, LONGEST *value, int bits)
21605 {
21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21607 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21608 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21609 LONGEST l = DW_UNSND (attr);
21610
21611 if (bits < sizeof (*value) * 8)
21612 {
21613 l &= ((LONGEST) 1 << bits) - 1;
21614 *value = l;
21615 }
21616 else if (bits == sizeof (*value) * 8)
21617 *value = l;
21618 else
21619 {
21620 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21621 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21622 return bytes;
21623 }
21624
21625 return NULL;
21626 }
21627
21628 /* Read a constant value from an attribute. Either set *VALUE, or if
21629 the value does not fit in *VALUE, set *BYTES - either already
21630 allocated on the objfile obstack, or newly allocated on OBSTACK,
21631 or, set *BATON, if we translated the constant to a location
21632 expression. */
21633
21634 static void
21635 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21636 const char *name, struct obstack *obstack,
21637 struct dwarf2_cu *cu,
21638 LONGEST *value, const gdb_byte **bytes,
21639 struct dwarf2_locexpr_baton **baton)
21640 {
21641 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21642 struct comp_unit_head *cu_header = &cu->header;
21643 struct dwarf_block *blk;
21644 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21645 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21646
21647 *value = 0;
21648 *bytes = NULL;
21649 *baton = NULL;
21650
21651 switch (attr->form)
21652 {
21653 case DW_FORM_addr:
21654 case DW_FORM_addrx:
21655 case DW_FORM_GNU_addr_index:
21656 {
21657 gdb_byte *data;
21658
21659 if (TYPE_LENGTH (type) != cu_header->addr_size)
21660 dwarf2_const_value_length_mismatch_complaint (name,
21661 cu_header->addr_size,
21662 TYPE_LENGTH (type));
21663 /* Symbols of this form are reasonably rare, so we just
21664 piggyback on the existing location code rather than writing
21665 a new implementation of symbol_computed_ops. */
21666 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21667 (*baton)->per_cu = cu->per_cu;
21668 gdb_assert ((*baton)->per_cu);
21669
21670 (*baton)->size = 2 + cu_header->addr_size;
21671 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21672 (*baton)->data = data;
21673
21674 data[0] = DW_OP_addr;
21675 store_unsigned_integer (&data[1], cu_header->addr_size,
21676 byte_order, DW_ADDR (attr));
21677 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21678 }
21679 break;
21680 case DW_FORM_string:
21681 case DW_FORM_strp:
21682 case DW_FORM_strx:
21683 case DW_FORM_GNU_str_index:
21684 case DW_FORM_GNU_strp_alt:
21685 /* DW_STRING is already allocated on the objfile obstack, point
21686 directly to it. */
21687 *bytes = (const gdb_byte *) DW_STRING (attr);
21688 break;
21689 case DW_FORM_block1:
21690 case DW_FORM_block2:
21691 case DW_FORM_block4:
21692 case DW_FORM_block:
21693 case DW_FORM_exprloc:
21694 case DW_FORM_data16:
21695 blk = DW_BLOCK (attr);
21696 if (TYPE_LENGTH (type) != blk->size)
21697 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21698 TYPE_LENGTH (type));
21699 *bytes = blk->data;
21700 break;
21701
21702 /* The DW_AT_const_value attributes are supposed to carry the
21703 symbol's value "represented as it would be on the target
21704 architecture." By the time we get here, it's already been
21705 converted to host endianness, so we just need to sign- or
21706 zero-extend it as appropriate. */
21707 case DW_FORM_data1:
21708 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21709 break;
21710 case DW_FORM_data2:
21711 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21712 break;
21713 case DW_FORM_data4:
21714 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21715 break;
21716 case DW_FORM_data8:
21717 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21718 break;
21719
21720 case DW_FORM_sdata:
21721 case DW_FORM_implicit_const:
21722 *value = DW_SND (attr);
21723 break;
21724
21725 case DW_FORM_udata:
21726 *value = DW_UNSND (attr);
21727 break;
21728
21729 default:
21730 complaint (_("unsupported const value attribute form: '%s'"),
21731 dwarf_form_name (attr->form));
21732 *value = 0;
21733 break;
21734 }
21735 }
21736
21737
21738 /* Copy constant value from an attribute to a symbol. */
21739
21740 static void
21741 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21742 struct dwarf2_cu *cu)
21743 {
21744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21745 LONGEST value;
21746 const gdb_byte *bytes;
21747 struct dwarf2_locexpr_baton *baton;
21748
21749 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21750 sym->print_name (),
21751 &objfile->objfile_obstack, cu,
21752 &value, &bytes, &baton);
21753
21754 if (baton != NULL)
21755 {
21756 SYMBOL_LOCATION_BATON (sym) = baton;
21757 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21758 }
21759 else if (bytes != NULL)
21760 {
21761 SYMBOL_VALUE_BYTES (sym) = bytes;
21762 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21763 }
21764 else
21765 {
21766 SYMBOL_VALUE (sym) = value;
21767 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21768 }
21769 }
21770
21771 /* Return the type of the die in question using its DW_AT_type attribute. */
21772
21773 static struct type *
21774 die_type (struct die_info *die, struct dwarf2_cu *cu)
21775 {
21776 struct attribute *type_attr;
21777
21778 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21779 if (!type_attr)
21780 {
21781 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21782 /* A missing DW_AT_type represents a void type. */
21783 return objfile_type (objfile)->builtin_void;
21784 }
21785
21786 return lookup_die_type (die, type_attr, cu);
21787 }
21788
21789 /* True iff CU's producer generates GNAT Ada auxiliary information
21790 that allows to find parallel types through that information instead
21791 of having to do expensive parallel lookups by type name. */
21792
21793 static int
21794 need_gnat_info (struct dwarf2_cu *cu)
21795 {
21796 /* Assume that the Ada compiler was GNAT, which always produces
21797 the auxiliary information. */
21798 return (cu->language == language_ada);
21799 }
21800
21801 /* Return the auxiliary type of the die in question using its
21802 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21803 attribute is not present. */
21804
21805 static struct type *
21806 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21807 {
21808 struct attribute *type_attr;
21809
21810 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21811 if (!type_attr)
21812 return NULL;
21813
21814 return lookup_die_type (die, type_attr, cu);
21815 }
21816
21817 /* If DIE has a descriptive_type attribute, then set the TYPE's
21818 descriptive type accordingly. */
21819
21820 static void
21821 set_descriptive_type (struct type *type, struct die_info *die,
21822 struct dwarf2_cu *cu)
21823 {
21824 struct type *descriptive_type = die_descriptive_type (die, cu);
21825
21826 if (descriptive_type)
21827 {
21828 ALLOCATE_GNAT_AUX_TYPE (type);
21829 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21830 }
21831 }
21832
21833 /* Return the containing type of the die in question using its
21834 DW_AT_containing_type attribute. */
21835
21836 static struct type *
21837 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21838 {
21839 struct attribute *type_attr;
21840 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21841
21842 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21843 if (!type_attr)
21844 error (_("Dwarf Error: Problem turning containing type into gdb type "
21845 "[in module %s]"), objfile_name (objfile));
21846
21847 return lookup_die_type (die, type_attr, cu);
21848 }
21849
21850 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21851
21852 static struct type *
21853 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21854 {
21855 struct dwarf2_per_objfile *dwarf2_per_objfile
21856 = cu->per_cu->dwarf2_per_objfile;
21857 struct objfile *objfile = dwarf2_per_objfile->objfile;
21858 char *saved;
21859
21860 std::string message
21861 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21862 objfile_name (objfile),
21863 sect_offset_str (cu->header.sect_off),
21864 sect_offset_str (die->sect_off));
21865 saved = obstack_strdup (&objfile->objfile_obstack, message);
21866
21867 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21868 }
21869
21870 /* Look up the type of DIE in CU using its type attribute ATTR.
21871 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21872 DW_AT_containing_type.
21873 If there is no type substitute an error marker. */
21874
21875 static struct type *
21876 lookup_die_type (struct die_info *die, const struct attribute *attr,
21877 struct dwarf2_cu *cu)
21878 {
21879 struct dwarf2_per_objfile *dwarf2_per_objfile
21880 = cu->per_cu->dwarf2_per_objfile;
21881 struct objfile *objfile = dwarf2_per_objfile->objfile;
21882 struct type *this_type;
21883
21884 gdb_assert (attr->name == DW_AT_type
21885 || attr->name == DW_AT_GNAT_descriptive_type
21886 || attr->name == DW_AT_containing_type);
21887
21888 /* First see if we have it cached. */
21889
21890 if (attr->form == DW_FORM_GNU_ref_alt)
21891 {
21892 struct dwarf2_per_cu_data *per_cu;
21893 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21894
21895 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21896 dwarf2_per_objfile);
21897 this_type = get_die_type_at_offset (sect_off, per_cu);
21898 }
21899 else if (attr->form_is_ref ())
21900 {
21901 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21902
21903 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21904 }
21905 else if (attr->form == DW_FORM_ref_sig8)
21906 {
21907 ULONGEST signature = DW_SIGNATURE (attr);
21908
21909 return get_signatured_type (die, signature, cu);
21910 }
21911 else
21912 {
21913 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21914 " at %s [in module %s]"),
21915 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21916 objfile_name (objfile));
21917 return build_error_marker_type (cu, die);
21918 }
21919
21920 /* If not cached we need to read it in. */
21921
21922 if (this_type == NULL)
21923 {
21924 struct die_info *type_die = NULL;
21925 struct dwarf2_cu *type_cu = cu;
21926
21927 if (attr->form_is_ref ())
21928 type_die = follow_die_ref (die, attr, &type_cu);
21929 if (type_die == NULL)
21930 return build_error_marker_type (cu, die);
21931 /* If we find the type now, it's probably because the type came
21932 from an inter-CU reference and the type's CU got expanded before
21933 ours. */
21934 this_type = read_type_die (type_die, type_cu);
21935 }
21936
21937 /* If we still don't have a type use an error marker. */
21938
21939 if (this_type == NULL)
21940 return build_error_marker_type (cu, die);
21941
21942 return this_type;
21943 }
21944
21945 /* Return the type in DIE, CU.
21946 Returns NULL for invalid types.
21947
21948 This first does a lookup in die_type_hash,
21949 and only reads the die in if necessary.
21950
21951 NOTE: This can be called when reading in partial or full symbols. */
21952
21953 static struct type *
21954 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21955 {
21956 struct type *this_type;
21957
21958 this_type = get_die_type (die, cu);
21959 if (this_type)
21960 return this_type;
21961
21962 return read_type_die_1 (die, cu);
21963 }
21964
21965 /* Read the type in DIE, CU.
21966 Returns NULL for invalid types. */
21967
21968 static struct type *
21969 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21970 {
21971 struct type *this_type = NULL;
21972
21973 switch (die->tag)
21974 {
21975 case DW_TAG_class_type:
21976 case DW_TAG_interface_type:
21977 case DW_TAG_structure_type:
21978 case DW_TAG_union_type:
21979 this_type = read_structure_type (die, cu);
21980 break;
21981 case DW_TAG_enumeration_type:
21982 this_type = read_enumeration_type (die, cu);
21983 break;
21984 case DW_TAG_subprogram:
21985 case DW_TAG_subroutine_type:
21986 case DW_TAG_inlined_subroutine:
21987 this_type = read_subroutine_type (die, cu);
21988 break;
21989 case DW_TAG_array_type:
21990 this_type = read_array_type (die, cu);
21991 break;
21992 case DW_TAG_set_type:
21993 this_type = read_set_type (die, cu);
21994 break;
21995 case DW_TAG_pointer_type:
21996 this_type = read_tag_pointer_type (die, cu);
21997 break;
21998 case DW_TAG_ptr_to_member_type:
21999 this_type = read_tag_ptr_to_member_type (die, cu);
22000 break;
22001 case DW_TAG_reference_type:
22002 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22003 break;
22004 case DW_TAG_rvalue_reference_type:
22005 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22006 break;
22007 case DW_TAG_const_type:
22008 this_type = read_tag_const_type (die, cu);
22009 break;
22010 case DW_TAG_volatile_type:
22011 this_type = read_tag_volatile_type (die, cu);
22012 break;
22013 case DW_TAG_restrict_type:
22014 this_type = read_tag_restrict_type (die, cu);
22015 break;
22016 case DW_TAG_string_type:
22017 this_type = read_tag_string_type (die, cu);
22018 break;
22019 case DW_TAG_typedef:
22020 this_type = read_typedef (die, cu);
22021 break;
22022 case DW_TAG_subrange_type:
22023 this_type = read_subrange_type (die, cu);
22024 break;
22025 case DW_TAG_base_type:
22026 this_type = read_base_type (die, cu);
22027 break;
22028 case DW_TAG_unspecified_type:
22029 this_type = read_unspecified_type (die, cu);
22030 break;
22031 case DW_TAG_namespace:
22032 this_type = read_namespace_type (die, cu);
22033 break;
22034 case DW_TAG_module:
22035 this_type = read_module_type (die, cu);
22036 break;
22037 case DW_TAG_atomic_type:
22038 this_type = read_tag_atomic_type (die, cu);
22039 break;
22040 default:
22041 complaint (_("unexpected tag in read_type_die: '%s'"),
22042 dwarf_tag_name (die->tag));
22043 break;
22044 }
22045
22046 return this_type;
22047 }
22048
22049 /* See if we can figure out if the class lives in a namespace. We do
22050 this by looking for a member function; its demangled name will
22051 contain namespace info, if there is any.
22052 Return the computed name or NULL.
22053 Space for the result is allocated on the objfile's obstack.
22054 This is the full-die version of guess_partial_die_structure_name.
22055 In this case we know DIE has no useful parent. */
22056
22057 static const char *
22058 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22059 {
22060 struct die_info *spec_die;
22061 struct dwarf2_cu *spec_cu;
22062 struct die_info *child;
22063 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22064
22065 spec_cu = cu;
22066 spec_die = die_specification (die, &spec_cu);
22067 if (spec_die != NULL)
22068 {
22069 die = spec_die;
22070 cu = spec_cu;
22071 }
22072
22073 for (child = die->child;
22074 child != NULL;
22075 child = child->sibling)
22076 {
22077 if (child->tag == DW_TAG_subprogram)
22078 {
22079 const char *linkage_name = dw2_linkage_name (child, cu);
22080
22081 if (linkage_name != NULL)
22082 {
22083 gdb::unique_xmalloc_ptr<char> actual_name
22084 (language_class_name_from_physname (cu->language_defn,
22085 linkage_name));
22086 const char *name = NULL;
22087
22088 if (actual_name != NULL)
22089 {
22090 const char *die_name = dwarf2_name (die, cu);
22091
22092 if (die_name != NULL
22093 && strcmp (die_name, actual_name.get ()) != 0)
22094 {
22095 /* Strip off the class name from the full name.
22096 We want the prefix. */
22097 int die_name_len = strlen (die_name);
22098 int actual_name_len = strlen (actual_name.get ());
22099 const char *ptr = actual_name.get ();
22100
22101 /* Test for '::' as a sanity check. */
22102 if (actual_name_len > die_name_len + 2
22103 && ptr[actual_name_len - die_name_len - 1] == ':')
22104 name = obstack_strndup (
22105 &objfile->per_bfd->storage_obstack,
22106 ptr, actual_name_len - die_name_len - 2);
22107 }
22108 }
22109 return name;
22110 }
22111 }
22112 }
22113
22114 return NULL;
22115 }
22116
22117 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22118 prefix part in such case. See
22119 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22120
22121 static const char *
22122 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22123 {
22124 struct attribute *attr;
22125 const char *base;
22126
22127 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22128 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22129 return NULL;
22130
22131 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22132 return NULL;
22133
22134 attr = dw2_linkage_name_attr (die, cu);
22135 if (attr == NULL || DW_STRING (attr) == NULL)
22136 return NULL;
22137
22138 /* dwarf2_name had to be already called. */
22139 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22140
22141 /* Strip the base name, keep any leading namespaces/classes. */
22142 base = strrchr (DW_STRING (attr), ':');
22143 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22144 return "";
22145
22146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22147 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22148 DW_STRING (attr),
22149 &base[-1] - DW_STRING (attr));
22150 }
22151
22152 /* Return the name of the namespace/class that DIE is defined within,
22153 or "" if we can't tell. The caller should not xfree the result.
22154
22155 For example, if we're within the method foo() in the following
22156 code:
22157
22158 namespace N {
22159 class C {
22160 void foo () {
22161 }
22162 };
22163 }
22164
22165 then determine_prefix on foo's die will return "N::C". */
22166
22167 static const char *
22168 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22169 {
22170 struct dwarf2_per_objfile *dwarf2_per_objfile
22171 = cu->per_cu->dwarf2_per_objfile;
22172 struct die_info *parent, *spec_die;
22173 struct dwarf2_cu *spec_cu;
22174 struct type *parent_type;
22175 const char *retval;
22176
22177 if (cu->language != language_cplus
22178 && cu->language != language_fortran && cu->language != language_d
22179 && cu->language != language_rust)
22180 return "";
22181
22182 retval = anonymous_struct_prefix (die, cu);
22183 if (retval)
22184 return retval;
22185
22186 /* We have to be careful in the presence of DW_AT_specification.
22187 For example, with GCC 3.4, given the code
22188
22189 namespace N {
22190 void foo() {
22191 // Definition of N::foo.
22192 }
22193 }
22194
22195 then we'll have a tree of DIEs like this:
22196
22197 1: DW_TAG_compile_unit
22198 2: DW_TAG_namespace // N
22199 3: DW_TAG_subprogram // declaration of N::foo
22200 4: DW_TAG_subprogram // definition of N::foo
22201 DW_AT_specification // refers to die #3
22202
22203 Thus, when processing die #4, we have to pretend that we're in
22204 the context of its DW_AT_specification, namely the contex of die
22205 #3. */
22206 spec_cu = cu;
22207 spec_die = die_specification (die, &spec_cu);
22208 if (spec_die == NULL)
22209 parent = die->parent;
22210 else
22211 {
22212 parent = spec_die->parent;
22213 cu = spec_cu;
22214 }
22215
22216 if (parent == NULL)
22217 return "";
22218 else if (parent->building_fullname)
22219 {
22220 const char *name;
22221 const char *parent_name;
22222
22223 /* It has been seen on RealView 2.2 built binaries,
22224 DW_TAG_template_type_param types actually _defined_ as
22225 children of the parent class:
22226
22227 enum E {};
22228 template class <class Enum> Class{};
22229 Class<enum E> class_e;
22230
22231 1: DW_TAG_class_type (Class)
22232 2: DW_TAG_enumeration_type (E)
22233 3: DW_TAG_enumerator (enum1:0)
22234 3: DW_TAG_enumerator (enum2:1)
22235 ...
22236 2: DW_TAG_template_type_param
22237 DW_AT_type DW_FORM_ref_udata (E)
22238
22239 Besides being broken debug info, it can put GDB into an
22240 infinite loop. Consider:
22241
22242 When we're building the full name for Class<E>, we'll start
22243 at Class, and go look over its template type parameters,
22244 finding E. We'll then try to build the full name of E, and
22245 reach here. We're now trying to build the full name of E,
22246 and look over the parent DIE for containing scope. In the
22247 broken case, if we followed the parent DIE of E, we'd again
22248 find Class, and once again go look at its template type
22249 arguments, etc., etc. Simply don't consider such parent die
22250 as source-level parent of this die (it can't be, the language
22251 doesn't allow it), and break the loop here. */
22252 name = dwarf2_name (die, cu);
22253 parent_name = dwarf2_name (parent, cu);
22254 complaint (_("template param type '%s' defined within parent '%s'"),
22255 name ? name : "<unknown>",
22256 parent_name ? parent_name : "<unknown>");
22257 return "";
22258 }
22259 else
22260 switch (parent->tag)
22261 {
22262 case DW_TAG_namespace:
22263 parent_type = read_type_die (parent, cu);
22264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22266 Work around this problem here. */
22267 if (cu->language == language_cplus
22268 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22269 return "";
22270 /* We give a name to even anonymous namespaces. */
22271 return TYPE_NAME (parent_type);
22272 case DW_TAG_class_type:
22273 case DW_TAG_interface_type:
22274 case DW_TAG_structure_type:
22275 case DW_TAG_union_type:
22276 case DW_TAG_module:
22277 parent_type = read_type_die (parent, cu);
22278 if (TYPE_NAME (parent_type) != NULL)
22279 return TYPE_NAME (parent_type);
22280 else
22281 /* An anonymous structure is only allowed non-static data
22282 members; no typedefs, no member functions, et cetera.
22283 So it does not need a prefix. */
22284 return "";
22285 case DW_TAG_compile_unit:
22286 case DW_TAG_partial_unit:
22287 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22288 if (cu->language == language_cplus
22289 && !dwarf2_per_objfile->types.empty ()
22290 && die->child != NULL
22291 && (die->tag == DW_TAG_class_type
22292 || die->tag == DW_TAG_structure_type
22293 || die->tag == DW_TAG_union_type))
22294 {
22295 const char *name = guess_full_die_structure_name (die, cu);
22296 if (name != NULL)
22297 return name;
22298 }
22299 return "";
22300 case DW_TAG_subprogram:
22301 /* Nested subroutines in Fortran get a prefix with the name
22302 of the parent's subroutine. */
22303 if (cu->language == language_fortran)
22304 {
22305 if ((die->tag == DW_TAG_subprogram)
22306 && (dwarf2_name (parent, cu) != NULL))
22307 return dwarf2_name (parent, cu);
22308 }
22309 return determine_prefix (parent, cu);
22310 case DW_TAG_enumeration_type:
22311 parent_type = read_type_die (parent, cu);
22312 if (TYPE_DECLARED_CLASS (parent_type))
22313 {
22314 if (TYPE_NAME (parent_type) != NULL)
22315 return TYPE_NAME (parent_type);
22316 return "";
22317 }
22318 /* Fall through. */
22319 default:
22320 return determine_prefix (parent, cu);
22321 }
22322 }
22323
22324 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22325 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22326 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22327 an obconcat, otherwise allocate storage for the result. The CU argument is
22328 used to determine the language and hence, the appropriate separator. */
22329
22330 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22331
22332 static char *
22333 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22334 int physname, struct dwarf2_cu *cu)
22335 {
22336 const char *lead = "";
22337 const char *sep;
22338
22339 if (suffix == NULL || suffix[0] == '\0'
22340 || prefix == NULL || prefix[0] == '\0')
22341 sep = "";
22342 else if (cu->language == language_d)
22343 {
22344 /* For D, the 'main' function could be defined in any module, but it
22345 should never be prefixed. */
22346 if (strcmp (suffix, "D main") == 0)
22347 {
22348 prefix = "";
22349 sep = "";
22350 }
22351 else
22352 sep = ".";
22353 }
22354 else if (cu->language == language_fortran && physname)
22355 {
22356 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22357 DW_AT_MIPS_linkage_name is preferred and used instead. */
22358
22359 lead = "__";
22360 sep = "_MOD_";
22361 }
22362 else
22363 sep = "::";
22364
22365 if (prefix == NULL)
22366 prefix = "";
22367 if (suffix == NULL)
22368 suffix = "";
22369
22370 if (obs == NULL)
22371 {
22372 char *retval
22373 = ((char *)
22374 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22375
22376 strcpy (retval, lead);
22377 strcat (retval, prefix);
22378 strcat (retval, sep);
22379 strcat (retval, suffix);
22380 return retval;
22381 }
22382 else
22383 {
22384 /* We have an obstack. */
22385 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22386 }
22387 }
22388
22389 /* Return sibling of die, NULL if no sibling. */
22390
22391 static struct die_info *
22392 sibling_die (struct die_info *die)
22393 {
22394 return die->sibling;
22395 }
22396
22397 /* Get name of a die, return NULL if not found. */
22398
22399 static const char *
22400 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22401 struct obstack *obstack)
22402 {
22403 if (name && cu->language == language_cplus)
22404 {
22405 std::string canon_name = cp_canonicalize_string (name);
22406
22407 if (!canon_name.empty ())
22408 {
22409 if (canon_name != name)
22410 name = obstack_strdup (obstack, canon_name);
22411 }
22412 }
22413
22414 return name;
22415 }
22416
22417 /* Get name of a die, return NULL if not found.
22418 Anonymous namespaces are converted to their magic string. */
22419
22420 static const char *
22421 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22422 {
22423 struct attribute *attr;
22424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22425
22426 attr = dwarf2_attr (die, DW_AT_name, cu);
22427 if ((!attr || !DW_STRING (attr))
22428 && die->tag != DW_TAG_namespace
22429 && die->tag != DW_TAG_class_type
22430 && die->tag != DW_TAG_interface_type
22431 && die->tag != DW_TAG_structure_type
22432 && die->tag != DW_TAG_union_type)
22433 return NULL;
22434
22435 switch (die->tag)
22436 {
22437 case DW_TAG_compile_unit:
22438 case DW_TAG_partial_unit:
22439 /* Compilation units have a DW_AT_name that is a filename, not
22440 a source language identifier. */
22441 case DW_TAG_enumeration_type:
22442 case DW_TAG_enumerator:
22443 /* These tags always have simple identifiers already; no need
22444 to canonicalize them. */
22445 return DW_STRING (attr);
22446
22447 case DW_TAG_namespace:
22448 if (attr != NULL && DW_STRING (attr) != NULL)
22449 return DW_STRING (attr);
22450 return CP_ANONYMOUS_NAMESPACE_STR;
22451
22452 case DW_TAG_class_type:
22453 case DW_TAG_interface_type:
22454 case DW_TAG_structure_type:
22455 case DW_TAG_union_type:
22456 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22457 structures or unions. These were of the form "._%d" in GCC 4.1,
22458 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22459 and GCC 4.4. We work around this problem by ignoring these. */
22460 if (attr && DW_STRING (attr)
22461 && (startswith (DW_STRING (attr), "._")
22462 || startswith (DW_STRING (attr), "<anonymous")))
22463 return NULL;
22464
22465 /* GCC might emit a nameless typedef that has a linkage name. See
22466 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22467 if (!attr || DW_STRING (attr) == NULL)
22468 {
22469 attr = dw2_linkage_name_attr (die, cu);
22470 if (attr == NULL || DW_STRING (attr) == NULL)
22471 return NULL;
22472
22473 /* Avoid demangling DW_STRING (attr) the second time on a second
22474 call for the same DIE. */
22475 if (!DW_STRING_IS_CANONICAL (attr))
22476 {
22477 gdb::unique_xmalloc_ptr<char> demangled
22478 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22479
22480 const char *base;
22481
22482 /* FIXME: we already did this for the partial symbol... */
22483 DW_STRING (attr)
22484 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22485 demangled.get ());
22486 DW_STRING_IS_CANONICAL (attr) = 1;
22487
22488 /* Strip any leading namespaces/classes, keep only the base name.
22489 DW_AT_name for named DIEs does not contain the prefixes. */
22490 base = strrchr (DW_STRING (attr), ':');
22491 if (base && base > DW_STRING (attr) && base[-1] == ':')
22492 return &base[1];
22493 else
22494 return DW_STRING (attr);
22495 }
22496 }
22497 break;
22498
22499 default:
22500 break;
22501 }
22502
22503 if (!DW_STRING_IS_CANONICAL (attr))
22504 {
22505 DW_STRING (attr)
22506 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22507 &objfile->per_bfd->storage_obstack);
22508 DW_STRING_IS_CANONICAL (attr) = 1;
22509 }
22510 return DW_STRING (attr);
22511 }
22512
22513 /* Return the die that this die in an extension of, or NULL if there
22514 is none. *EXT_CU is the CU containing DIE on input, and the CU
22515 containing the return value on output. */
22516
22517 static struct die_info *
22518 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22519 {
22520 struct attribute *attr;
22521
22522 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22523 if (attr == NULL)
22524 return NULL;
22525
22526 return follow_die_ref (die, attr, ext_cu);
22527 }
22528
22529 /* A convenience function that returns an "unknown" DWARF name,
22530 including the value of V. STR is the name of the entity being
22531 printed, e.g., "TAG". */
22532
22533 static const char *
22534 dwarf_unknown (const char *str, unsigned v)
22535 {
22536 char *cell = get_print_cell ();
22537 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22538 return cell;
22539 }
22540
22541 /* Convert a DIE tag into its string name. */
22542
22543 static const char *
22544 dwarf_tag_name (unsigned tag)
22545 {
22546 const char *name = get_DW_TAG_name (tag);
22547
22548 if (name == NULL)
22549 return dwarf_unknown ("TAG", tag);
22550
22551 return name;
22552 }
22553
22554 /* Convert a DWARF attribute code into its string name. */
22555
22556 static const char *
22557 dwarf_attr_name (unsigned attr)
22558 {
22559 const char *name;
22560
22561 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22562 if (attr == DW_AT_MIPS_fde)
22563 return "DW_AT_MIPS_fde";
22564 #else
22565 if (attr == DW_AT_HP_block_index)
22566 return "DW_AT_HP_block_index";
22567 #endif
22568
22569 name = get_DW_AT_name (attr);
22570
22571 if (name == NULL)
22572 return dwarf_unknown ("AT", attr);
22573
22574 return name;
22575 }
22576
22577 /* Convert a unit type to corresponding DW_UT name. */
22578
22579 static const char *
22580 dwarf_unit_type_name (int unit_type) {
22581 switch (unit_type)
22582 {
22583 case 0x01:
22584 return "DW_UT_compile (0x01)";
22585 case 0x02:
22586 return "DW_UT_type (0x02)";
22587 case 0x03:
22588 return "DW_UT_partial (0x03)";
22589 case 0x04:
22590 return "DW_UT_skeleton (0x04)";
22591 case 0x05:
22592 return "DW_UT_split_compile (0x05)";
22593 case 0x06:
22594 return "DW_UT_split_type (0x06)";
22595 case 0x80:
22596 return "DW_UT_lo_user (0x80)";
22597 case 0xff:
22598 return "DW_UT_hi_user (0xff)";
22599 default:
22600 return nullptr;
22601 }
22602 }
22603
22604 /* Convert a DWARF value form code into its string name. */
22605
22606 static const char *
22607 dwarf_form_name (unsigned form)
22608 {
22609 const char *name = get_DW_FORM_name (form);
22610
22611 if (name == NULL)
22612 return dwarf_unknown ("FORM", form);
22613
22614 return name;
22615 }
22616
22617 static const char *
22618 dwarf_bool_name (unsigned mybool)
22619 {
22620 if (mybool)
22621 return "TRUE";
22622 else
22623 return "FALSE";
22624 }
22625
22626 /* Convert a DWARF type code into its string name. */
22627
22628 static const char *
22629 dwarf_type_encoding_name (unsigned enc)
22630 {
22631 const char *name = get_DW_ATE_name (enc);
22632
22633 if (name == NULL)
22634 return dwarf_unknown ("ATE", enc);
22635
22636 return name;
22637 }
22638
22639 static void
22640 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22641 {
22642 unsigned int i;
22643
22644 print_spaces (indent, f);
22645 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22646 dwarf_tag_name (die->tag), die->abbrev,
22647 sect_offset_str (die->sect_off));
22648
22649 if (die->parent != NULL)
22650 {
22651 print_spaces (indent, f);
22652 fprintf_unfiltered (f, " parent at offset: %s\n",
22653 sect_offset_str (die->parent->sect_off));
22654 }
22655
22656 print_spaces (indent, f);
22657 fprintf_unfiltered (f, " has children: %s\n",
22658 dwarf_bool_name (die->child != NULL));
22659
22660 print_spaces (indent, f);
22661 fprintf_unfiltered (f, " attributes:\n");
22662
22663 for (i = 0; i < die->num_attrs; ++i)
22664 {
22665 print_spaces (indent, f);
22666 fprintf_unfiltered (f, " %s (%s) ",
22667 dwarf_attr_name (die->attrs[i].name),
22668 dwarf_form_name (die->attrs[i].form));
22669
22670 switch (die->attrs[i].form)
22671 {
22672 case DW_FORM_addr:
22673 case DW_FORM_addrx:
22674 case DW_FORM_GNU_addr_index:
22675 fprintf_unfiltered (f, "address: ");
22676 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22677 break;
22678 case DW_FORM_block2:
22679 case DW_FORM_block4:
22680 case DW_FORM_block:
22681 case DW_FORM_block1:
22682 fprintf_unfiltered (f, "block: size %s",
22683 pulongest (DW_BLOCK (&die->attrs[i])->size));
22684 break;
22685 case DW_FORM_exprloc:
22686 fprintf_unfiltered (f, "expression: size %s",
22687 pulongest (DW_BLOCK (&die->attrs[i])->size));
22688 break;
22689 case DW_FORM_data16:
22690 fprintf_unfiltered (f, "constant of 16 bytes");
22691 break;
22692 case DW_FORM_ref_addr:
22693 fprintf_unfiltered (f, "ref address: ");
22694 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22695 break;
22696 case DW_FORM_GNU_ref_alt:
22697 fprintf_unfiltered (f, "alt ref address: ");
22698 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22699 break;
22700 case DW_FORM_ref1:
22701 case DW_FORM_ref2:
22702 case DW_FORM_ref4:
22703 case DW_FORM_ref8:
22704 case DW_FORM_ref_udata:
22705 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22706 (long) (DW_UNSND (&die->attrs[i])));
22707 break;
22708 case DW_FORM_data1:
22709 case DW_FORM_data2:
22710 case DW_FORM_data4:
22711 case DW_FORM_data8:
22712 case DW_FORM_udata:
22713 case DW_FORM_sdata:
22714 fprintf_unfiltered (f, "constant: %s",
22715 pulongest (DW_UNSND (&die->attrs[i])));
22716 break;
22717 case DW_FORM_sec_offset:
22718 fprintf_unfiltered (f, "section offset: %s",
22719 pulongest (DW_UNSND (&die->attrs[i])));
22720 break;
22721 case DW_FORM_ref_sig8:
22722 fprintf_unfiltered (f, "signature: %s",
22723 hex_string (DW_SIGNATURE (&die->attrs[i])));
22724 break;
22725 case DW_FORM_string:
22726 case DW_FORM_strp:
22727 case DW_FORM_line_strp:
22728 case DW_FORM_strx:
22729 case DW_FORM_GNU_str_index:
22730 case DW_FORM_GNU_strp_alt:
22731 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22732 DW_STRING (&die->attrs[i])
22733 ? DW_STRING (&die->attrs[i]) : "",
22734 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22735 break;
22736 case DW_FORM_flag:
22737 if (DW_UNSND (&die->attrs[i]))
22738 fprintf_unfiltered (f, "flag: TRUE");
22739 else
22740 fprintf_unfiltered (f, "flag: FALSE");
22741 break;
22742 case DW_FORM_flag_present:
22743 fprintf_unfiltered (f, "flag: TRUE");
22744 break;
22745 case DW_FORM_indirect:
22746 /* The reader will have reduced the indirect form to
22747 the "base form" so this form should not occur. */
22748 fprintf_unfiltered (f,
22749 "unexpected attribute form: DW_FORM_indirect");
22750 break;
22751 case DW_FORM_implicit_const:
22752 fprintf_unfiltered (f, "constant: %s",
22753 plongest (DW_SND (&die->attrs[i])));
22754 break;
22755 default:
22756 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22757 die->attrs[i].form);
22758 break;
22759 }
22760 fprintf_unfiltered (f, "\n");
22761 }
22762 }
22763
22764 static void
22765 dump_die_for_error (struct die_info *die)
22766 {
22767 dump_die_shallow (gdb_stderr, 0, die);
22768 }
22769
22770 static void
22771 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22772 {
22773 int indent = level * 4;
22774
22775 gdb_assert (die != NULL);
22776
22777 if (level >= max_level)
22778 return;
22779
22780 dump_die_shallow (f, indent, die);
22781
22782 if (die->child != NULL)
22783 {
22784 print_spaces (indent, f);
22785 fprintf_unfiltered (f, " Children:");
22786 if (level + 1 < max_level)
22787 {
22788 fprintf_unfiltered (f, "\n");
22789 dump_die_1 (f, level + 1, max_level, die->child);
22790 }
22791 else
22792 {
22793 fprintf_unfiltered (f,
22794 " [not printed, max nesting level reached]\n");
22795 }
22796 }
22797
22798 if (die->sibling != NULL && level > 0)
22799 {
22800 dump_die_1 (f, level, max_level, die->sibling);
22801 }
22802 }
22803
22804 /* This is called from the pdie macro in gdbinit.in.
22805 It's not static so gcc will keep a copy callable from gdb. */
22806
22807 void
22808 dump_die (struct die_info *die, int max_level)
22809 {
22810 dump_die_1 (gdb_stdlog, 0, max_level, die);
22811 }
22812
22813 static void
22814 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22815 {
22816 void **slot;
22817
22818 slot = htab_find_slot_with_hash (cu->die_hash, die,
22819 to_underlying (die->sect_off),
22820 INSERT);
22821
22822 *slot = die;
22823 }
22824
22825 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22826 required kind. */
22827
22828 static sect_offset
22829 dwarf2_get_ref_die_offset (const struct attribute *attr)
22830 {
22831 if (attr->form_is_ref ())
22832 return (sect_offset) DW_UNSND (attr);
22833
22834 complaint (_("unsupported die ref attribute form: '%s'"),
22835 dwarf_form_name (attr->form));
22836 return {};
22837 }
22838
22839 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22840 * the value held by the attribute is not constant. */
22841
22842 static LONGEST
22843 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22844 {
22845 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22846 return DW_SND (attr);
22847 else if (attr->form == DW_FORM_udata
22848 || attr->form == DW_FORM_data1
22849 || attr->form == DW_FORM_data2
22850 || attr->form == DW_FORM_data4
22851 || attr->form == DW_FORM_data8)
22852 return DW_UNSND (attr);
22853 else
22854 {
22855 /* For DW_FORM_data16 see attribute::form_is_constant. */
22856 complaint (_("Attribute value is not a constant (%s)"),
22857 dwarf_form_name (attr->form));
22858 return default_value;
22859 }
22860 }
22861
22862 /* Follow reference or signature attribute ATTR of SRC_DIE.
22863 On entry *REF_CU is the CU of SRC_DIE.
22864 On exit *REF_CU is the CU of the result. */
22865
22866 static struct die_info *
22867 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22868 struct dwarf2_cu **ref_cu)
22869 {
22870 struct die_info *die;
22871
22872 if (attr->form_is_ref ())
22873 die = follow_die_ref (src_die, attr, ref_cu);
22874 else if (attr->form == DW_FORM_ref_sig8)
22875 die = follow_die_sig (src_die, attr, ref_cu);
22876 else
22877 {
22878 dump_die_for_error (src_die);
22879 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22880 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22881 }
22882
22883 return die;
22884 }
22885
22886 /* Follow reference OFFSET.
22887 On entry *REF_CU is the CU of the source die referencing OFFSET.
22888 On exit *REF_CU is the CU of the result.
22889 Returns NULL if OFFSET is invalid. */
22890
22891 static struct die_info *
22892 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22893 struct dwarf2_cu **ref_cu)
22894 {
22895 struct die_info temp_die;
22896 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22897 struct dwarf2_per_objfile *dwarf2_per_objfile
22898 = cu->per_cu->dwarf2_per_objfile;
22899
22900 gdb_assert (cu->per_cu != NULL);
22901
22902 target_cu = cu;
22903
22904 if (cu->per_cu->is_debug_types)
22905 {
22906 /* .debug_types CUs cannot reference anything outside their CU.
22907 If they need to, they have to reference a signatured type via
22908 DW_FORM_ref_sig8. */
22909 if (!offset_in_cu_p (&cu->header, sect_off))
22910 return NULL;
22911 }
22912 else if (offset_in_dwz != cu->per_cu->is_dwz
22913 || !offset_in_cu_p (&cu->header, sect_off))
22914 {
22915 struct dwarf2_per_cu_data *per_cu;
22916
22917 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22918 dwarf2_per_objfile);
22919
22920 /* If necessary, add it to the queue and load its DIEs. */
22921 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22922 load_full_comp_unit (per_cu, false, cu->language);
22923
22924 target_cu = per_cu->cu;
22925 }
22926 else if (cu->dies == NULL)
22927 {
22928 /* We're loading full DIEs during partial symbol reading. */
22929 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22930 load_full_comp_unit (cu->per_cu, false, language_minimal);
22931 }
22932
22933 *ref_cu = target_cu;
22934 temp_die.sect_off = sect_off;
22935
22936 if (target_cu != cu)
22937 target_cu->ancestor = cu;
22938
22939 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22940 &temp_die,
22941 to_underlying (sect_off));
22942 }
22943
22944 /* Follow reference attribute ATTR of SRC_DIE.
22945 On entry *REF_CU is the CU of SRC_DIE.
22946 On exit *REF_CU is the CU of the result. */
22947
22948 static struct die_info *
22949 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22950 struct dwarf2_cu **ref_cu)
22951 {
22952 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22953 struct dwarf2_cu *cu = *ref_cu;
22954 struct die_info *die;
22955
22956 die = follow_die_offset (sect_off,
22957 (attr->form == DW_FORM_GNU_ref_alt
22958 || cu->per_cu->is_dwz),
22959 ref_cu);
22960 if (!die)
22961 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22962 "at %s [in module %s]"),
22963 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22964 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22965
22966 return die;
22967 }
22968
22969 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22970 Returned value is intended for DW_OP_call*. Returned
22971 dwarf2_locexpr_baton->data has lifetime of
22972 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22973
22974 struct dwarf2_locexpr_baton
22975 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22976 struct dwarf2_per_cu_data *per_cu,
22977 CORE_ADDR (*get_frame_pc) (void *baton),
22978 void *baton, bool resolve_abstract_p)
22979 {
22980 struct dwarf2_cu *cu;
22981 struct die_info *die;
22982 struct attribute *attr;
22983 struct dwarf2_locexpr_baton retval;
22984 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22985 struct objfile *objfile = dwarf2_per_objfile->objfile;
22986
22987 if (per_cu->cu == NULL)
22988 load_cu (per_cu, false);
22989 cu = per_cu->cu;
22990 if (cu == NULL)
22991 {
22992 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22993 Instead just throw an error, not much else we can do. */
22994 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22995 sect_offset_str (sect_off), objfile_name (objfile));
22996 }
22997
22998 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22999 if (!die)
23000 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23001 sect_offset_str (sect_off), objfile_name (objfile));
23002
23003 attr = dwarf2_attr (die, DW_AT_location, cu);
23004 if (!attr && resolve_abstract_p
23005 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23006 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23007 {
23008 CORE_ADDR pc = (*get_frame_pc) (baton);
23009 CORE_ADDR baseaddr = objfile->text_section_offset ();
23010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23011
23012 for (const auto &cand_off
23013 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23014 {
23015 struct dwarf2_cu *cand_cu = cu;
23016 struct die_info *cand
23017 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23018 if (!cand
23019 || !cand->parent
23020 || cand->parent->tag != DW_TAG_subprogram)
23021 continue;
23022
23023 CORE_ADDR pc_low, pc_high;
23024 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23025 if (pc_low == ((CORE_ADDR) -1))
23026 continue;
23027 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23028 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23029 if (!(pc_low <= pc && pc < pc_high))
23030 continue;
23031
23032 die = cand;
23033 attr = dwarf2_attr (die, DW_AT_location, cu);
23034 break;
23035 }
23036 }
23037
23038 if (!attr)
23039 {
23040 /* DWARF: "If there is no such attribute, then there is no effect.".
23041 DATA is ignored if SIZE is 0. */
23042
23043 retval.data = NULL;
23044 retval.size = 0;
23045 }
23046 else if (attr->form_is_section_offset ())
23047 {
23048 struct dwarf2_loclist_baton loclist_baton;
23049 CORE_ADDR pc = (*get_frame_pc) (baton);
23050 size_t size;
23051
23052 fill_in_loclist_baton (cu, &loclist_baton, attr);
23053
23054 retval.data = dwarf2_find_location_expression (&loclist_baton,
23055 &size, pc);
23056 retval.size = size;
23057 }
23058 else
23059 {
23060 if (!attr->form_is_block ())
23061 error (_("Dwarf Error: DIE at %s referenced in module %s "
23062 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23063 sect_offset_str (sect_off), objfile_name (objfile));
23064
23065 retval.data = DW_BLOCK (attr)->data;
23066 retval.size = DW_BLOCK (attr)->size;
23067 }
23068 retval.per_cu = cu->per_cu;
23069
23070 age_cached_comp_units (dwarf2_per_objfile);
23071
23072 return retval;
23073 }
23074
23075 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23076 offset. */
23077
23078 struct dwarf2_locexpr_baton
23079 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23080 struct dwarf2_per_cu_data *per_cu,
23081 CORE_ADDR (*get_frame_pc) (void *baton),
23082 void *baton)
23083 {
23084 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23085
23086 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23087 }
23088
23089 /* Write a constant of a given type as target-ordered bytes into
23090 OBSTACK. */
23091
23092 static const gdb_byte *
23093 write_constant_as_bytes (struct obstack *obstack,
23094 enum bfd_endian byte_order,
23095 struct type *type,
23096 ULONGEST value,
23097 LONGEST *len)
23098 {
23099 gdb_byte *result;
23100
23101 *len = TYPE_LENGTH (type);
23102 result = (gdb_byte *) obstack_alloc (obstack, *len);
23103 store_unsigned_integer (result, *len, byte_order, value);
23104
23105 return result;
23106 }
23107
23108 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23109 pointer to the constant bytes and set LEN to the length of the
23110 data. If memory is needed, allocate it on OBSTACK. If the DIE
23111 does not have a DW_AT_const_value, return NULL. */
23112
23113 const gdb_byte *
23114 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23115 struct dwarf2_per_cu_data *per_cu,
23116 struct obstack *obstack,
23117 LONGEST *len)
23118 {
23119 struct dwarf2_cu *cu;
23120 struct die_info *die;
23121 struct attribute *attr;
23122 const gdb_byte *result = NULL;
23123 struct type *type;
23124 LONGEST value;
23125 enum bfd_endian byte_order;
23126 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23127
23128 if (per_cu->cu == NULL)
23129 load_cu (per_cu, false);
23130 cu = per_cu->cu;
23131 if (cu == NULL)
23132 {
23133 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23134 Instead just throw an error, not much else we can do. */
23135 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23136 sect_offset_str (sect_off), objfile_name (objfile));
23137 }
23138
23139 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23140 if (!die)
23141 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23142 sect_offset_str (sect_off), objfile_name (objfile));
23143
23144 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23145 if (attr == NULL)
23146 return NULL;
23147
23148 byte_order = (bfd_big_endian (objfile->obfd)
23149 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23150
23151 switch (attr->form)
23152 {
23153 case DW_FORM_addr:
23154 case DW_FORM_addrx:
23155 case DW_FORM_GNU_addr_index:
23156 {
23157 gdb_byte *tem;
23158
23159 *len = cu->header.addr_size;
23160 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23161 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23162 result = tem;
23163 }
23164 break;
23165 case DW_FORM_string:
23166 case DW_FORM_strp:
23167 case DW_FORM_strx:
23168 case DW_FORM_GNU_str_index:
23169 case DW_FORM_GNU_strp_alt:
23170 /* DW_STRING is already allocated on the objfile obstack, point
23171 directly to it. */
23172 result = (const gdb_byte *) DW_STRING (attr);
23173 *len = strlen (DW_STRING (attr));
23174 break;
23175 case DW_FORM_block1:
23176 case DW_FORM_block2:
23177 case DW_FORM_block4:
23178 case DW_FORM_block:
23179 case DW_FORM_exprloc:
23180 case DW_FORM_data16:
23181 result = DW_BLOCK (attr)->data;
23182 *len = DW_BLOCK (attr)->size;
23183 break;
23184
23185 /* The DW_AT_const_value attributes are supposed to carry the
23186 symbol's value "represented as it would be on the target
23187 architecture." By the time we get here, it's already been
23188 converted to host endianness, so we just need to sign- or
23189 zero-extend it as appropriate. */
23190 case DW_FORM_data1:
23191 type = die_type (die, cu);
23192 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23193 if (result == NULL)
23194 result = write_constant_as_bytes (obstack, byte_order,
23195 type, value, len);
23196 break;
23197 case DW_FORM_data2:
23198 type = die_type (die, cu);
23199 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23200 if (result == NULL)
23201 result = write_constant_as_bytes (obstack, byte_order,
23202 type, value, len);
23203 break;
23204 case DW_FORM_data4:
23205 type = die_type (die, cu);
23206 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23207 if (result == NULL)
23208 result = write_constant_as_bytes (obstack, byte_order,
23209 type, value, len);
23210 break;
23211 case DW_FORM_data8:
23212 type = die_type (die, cu);
23213 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23214 if (result == NULL)
23215 result = write_constant_as_bytes (obstack, byte_order,
23216 type, value, len);
23217 break;
23218
23219 case DW_FORM_sdata:
23220 case DW_FORM_implicit_const:
23221 type = die_type (die, cu);
23222 result = write_constant_as_bytes (obstack, byte_order,
23223 type, DW_SND (attr), len);
23224 break;
23225
23226 case DW_FORM_udata:
23227 type = die_type (die, cu);
23228 result = write_constant_as_bytes (obstack, byte_order,
23229 type, DW_UNSND (attr), len);
23230 break;
23231
23232 default:
23233 complaint (_("unsupported const value attribute form: '%s'"),
23234 dwarf_form_name (attr->form));
23235 break;
23236 }
23237
23238 return result;
23239 }
23240
23241 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23242 valid type for this die is found. */
23243
23244 struct type *
23245 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23246 struct dwarf2_per_cu_data *per_cu)
23247 {
23248 struct dwarf2_cu *cu;
23249 struct die_info *die;
23250
23251 if (per_cu->cu == NULL)
23252 load_cu (per_cu, false);
23253 cu = per_cu->cu;
23254 if (!cu)
23255 return NULL;
23256
23257 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23258 if (!die)
23259 return NULL;
23260
23261 return die_type (die, cu);
23262 }
23263
23264 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23265 PER_CU. */
23266
23267 struct type *
23268 dwarf2_get_die_type (cu_offset die_offset,
23269 struct dwarf2_per_cu_data *per_cu)
23270 {
23271 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23272 return get_die_type_at_offset (die_offset_sect, per_cu);
23273 }
23274
23275 /* Follow type unit SIG_TYPE referenced by 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 Returns NULL if the referenced DIE isn't found. */
23279
23280 static struct die_info *
23281 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23282 struct dwarf2_cu **ref_cu)
23283 {
23284 struct die_info temp_die;
23285 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23286 struct die_info *die;
23287
23288 /* While it might be nice to assert sig_type->type == NULL here,
23289 we can get here for DW_AT_imported_declaration where we need
23290 the DIE not the type. */
23291
23292 /* If necessary, add it to the queue and load its DIEs. */
23293
23294 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23295 read_signatured_type (sig_type);
23296
23297 sig_cu = sig_type->per_cu.cu;
23298 gdb_assert (sig_cu != NULL);
23299 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23300 temp_die.sect_off = sig_type->type_offset_in_section;
23301 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23302 to_underlying (temp_die.sect_off));
23303 if (die)
23304 {
23305 struct dwarf2_per_objfile *dwarf2_per_objfile
23306 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23307
23308 /* For .gdb_index version 7 keep track of included TUs.
23309 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23310 if (dwarf2_per_objfile->index_table != NULL
23311 && dwarf2_per_objfile->index_table->version <= 7)
23312 {
23313 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23314 }
23315
23316 *ref_cu = sig_cu;
23317 if (sig_cu != cu)
23318 sig_cu->ancestor = cu;
23319
23320 return die;
23321 }
23322
23323 return NULL;
23324 }
23325
23326 /* Follow signatured type referenced by ATTR in SRC_DIE.
23327 On entry *REF_CU is the CU of SRC_DIE.
23328 On exit *REF_CU is the CU of the result.
23329 The result is the DIE of the type.
23330 If the referenced type cannot be found an error is thrown. */
23331
23332 static struct die_info *
23333 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23334 struct dwarf2_cu **ref_cu)
23335 {
23336 ULONGEST signature = DW_SIGNATURE (attr);
23337 struct signatured_type *sig_type;
23338 struct die_info *die;
23339
23340 gdb_assert (attr->form == DW_FORM_ref_sig8);
23341
23342 sig_type = lookup_signatured_type (*ref_cu, signature);
23343 /* sig_type will be NULL if the signatured type is missing from
23344 the debug info. */
23345 if (sig_type == NULL)
23346 {
23347 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23348 " from DIE at %s [in module %s]"),
23349 hex_string (signature), sect_offset_str (src_die->sect_off),
23350 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23351 }
23352
23353 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23354 if (die == NULL)
23355 {
23356 dump_die_for_error (src_die);
23357 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23358 " from DIE at %s [in module %s]"),
23359 hex_string (signature), sect_offset_str (src_die->sect_off),
23360 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23361 }
23362
23363 return die;
23364 }
23365
23366 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23367 reading in and processing the type unit if necessary. */
23368
23369 static struct type *
23370 get_signatured_type (struct die_info *die, ULONGEST signature,
23371 struct dwarf2_cu *cu)
23372 {
23373 struct dwarf2_per_objfile *dwarf2_per_objfile
23374 = cu->per_cu->dwarf2_per_objfile;
23375 struct signatured_type *sig_type;
23376 struct dwarf2_cu *type_cu;
23377 struct die_info *type_die;
23378 struct type *type;
23379
23380 sig_type = lookup_signatured_type (cu, signature);
23381 /* sig_type will be NULL if the signatured type is missing from
23382 the debug info. */
23383 if (sig_type == NULL)
23384 {
23385 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23386 " from DIE at %s [in module %s]"),
23387 hex_string (signature), sect_offset_str (die->sect_off),
23388 objfile_name (dwarf2_per_objfile->objfile));
23389 return build_error_marker_type (cu, die);
23390 }
23391
23392 /* If we already know the type we're done. */
23393 if (sig_type->type != NULL)
23394 return sig_type->type;
23395
23396 type_cu = cu;
23397 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23398 if (type_die != NULL)
23399 {
23400 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23401 is created. This is important, for example, because for c++ classes
23402 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23403 type = read_type_die (type_die, type_cu);
23404 if (type == NULL)
23405 {
23406 complaint (_("Dwarf Error: Cannot build signatured type %s"
23407 " referenced from DIE at %s [in module %s]"),
23408 hex_string (signature), sect_offset_str (die->sect_off),
23409 objfile_name (dwarf2_per_objfile->objfile));
23410 type = build_error_marker_type (cu, die);
23411 }
23412 }
23413 else
23414 {
23415 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23416 " from DIE at %s [in module %s]"),
23417 hex_string (signature), sect_offset_str (die->sect_off),
23418 objfile_name (dwarf2_per_objfile->objfile));
23419 type = build_error_marker_type (cu, die);
23420 }
23421 sig_type->type = type;
23422
23423 return type;
23424 }
23425
23426 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23427 reading in and processing the type unit if necessary. */
23428
23429 static struct type *
23430 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23431 struct dwarf2_cu *cu) /* ARI: editCase function */
23432 {
23433 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23434 if (attr->form_is_ref ())
23435 {
23436 struct dwarf2_cu *type_cu = cu;
23437 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23438
23439 return read_type_die (type_die, type_cu);
23440 }
23441 else if (attr->form == DW_FORM_ref_sig8)
23442 {
23443 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23444 }
23445 else
23446 {
23447 struct dwarf2_per_objfile *dwarf2_per_objfile
23448 = cu->per_cu->dwarf2_per_objfile;
23449
23450 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23451 " at %s [in module %s]"),
23452 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23453 objfile_name (dwarf2_per_objfile->objfile));
23454 return build_error_marker_type (cu, die);
23455 }
23456 }
23457
23458 /* Load the DIEs associated with type unit PER_CU into memory. */
23459
23460 static void
23461 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23462 {
23463 struct signatured_type *sig_type;
23464
23465 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23466 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23467
23468 /* We have the per_cu, but we need the signatured_type.
23469 Fortunately this is an easy translation. */
23470 gdb_assert (per_cu->is_debug_types);
23471 sig_type = (struct signatured_type *) per_cu;
23472
23473 gdb_assert (per_cu->cu == NULL);
23474
23475 read_signatured_type (sig_type);
23476
23477 gdb_assert (per_cu->cu != NULL);
23478 }
23479
23480 /* Read in a signatured type and build its CU and DIEs.
23481 If the type is a stub for the real type in a DWO file,
23482 read in the real type from the DWO file as well. */
23483
23484 static void
23485 read_signatured_type (struct signatured_type *sig_type)
23486 {
23487 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23488
23489 gdb_assert (per_cu->is_debug_types);
23490 gdb_assert (per_cu->cu == NULL);
23491
23492 cutu_reader reader (per_cu, NULL, 0, 1, false);
23493
23494 if (!reader.dummy_p)
23495 {
23496 struct dwarf2_cu *cu = reader.cu;
23497 const gdb_byte *info_ptr = reader.info_ptr;
23498
23499 gdb_assert (cu->die_hash == NULL);
23500 cu->die_hash =
23501 htab_create_alloc_ex (cu->header.length / 12,
23502 die_hash,
23503 die_eq,
23504 NULL,
23505 &cu->comp_unit_obstack,
23506 hashtab_obstack_allocate,
23507 dummy_obstack_deallocate);
23508
23509 if (reader.has_children)
23510 reader.comp_unit_die->child
23511 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23512 reader.comp_unit_die);
23513 cu->dies = reader.comp_unit_die;
23514 /* comp_unit_die is not stored in die_hash, no need. */
23515
23516 /* We try not to read any attributes in this function, because
23517 not all CUs needed for references have been loaded yet, and
23518 symbol table processing isn't initialized. But we have to
23519 set the CU language, or we won't be able to build types
23520 correctly. Similarly, if we do not read the producer, we can
23521 not apply producer-specific interpretation. */
23522 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23523 }
23524
23525 sig_type->per_cu.tu_read = 1;
23526 }
23527
23528 /* Decode simple location descriptions.
23529 Given a pointer to a dwarf block that defines a location, compute
23530 the location and return the value.
23531
23532 NOTE drow/2003-11-18: This function is called in two situations
23533 now: for the address of static or global variables (partial symbols
23534 only) and for offsets into structures which are expected to be
23535 (more or less) constant. The partial symbol case should go away,
23536 and only the constant case should remain. That will let this
23537 function complain more accurately. A few special modes are allowed
23538 without complaint for global variables (for instance, global
23539 register values and thread-local values).
23540
23541 A location description containing no operations indicates that the
23542 object is optimized out. The return value is 0 for that case.
23543 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23544 callers will only want a very basic result and this can become a
23545 complaint.
23546
23547 Note that stack[0] is unused except as a default error return. */
23548
23549 static CORE_ADDR
23550 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23551 {
23552 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23553 size_t i;
23554 size_t size = blk->size;
23555 const gdb_byte *data = blk->data;
23556 CORE_ADDR stack[64];
23557 int stacki;
23558 unsigned int bytes_read, unsnd;
23559 gdb_byte op;
23560
23561 i = 0;
23562 stacki = 0;
23563 stack[stacki] = 0;
23564 stack[++stacki] = 0;
23565
23566 while (i < size)
23567 {
23568 op = data[i++];
23569 switch (op)
23570 {
23571 case DW_OP_lit0:
23572 case DW_OP_lit1:
23573 case DW_OP_lit2:
23574 case DW_OP_lit3:
23575 case DW_OP_lit4:
23576 case DW_OP_lit5:
23577 case DW_OP_lit6:
23578 case DW_OP_lit7:
23579 case DW_OP_lit8:
23580 case DW_OP_lit9:
23581 case DW_OP_lit10:
23582 case DW_OP_lit11:
23583 case DW_OP_lit12:
23584 case DW_OP_lit13:
23585 case DW_OP_lit14:
23586 case DW_OP_lit15:
23587 case DW_OP_lit16:
23588 case DW_OP_lit17:
23589 case DW_OP_lit18:
23590 case DW_OP_lit19:
23591 case DW_OP_lit20:
23592 case DW_OP_lit21:
23593 case DW_OP_lit22:
23594 case DW_OP_lit23:
23595 case DW_OP_lit24:
23596 case DW_OP_lit25:
23597 case DW_OP_lit26:
23598 case DW_OP_lit27:
23599 case DW_OP_lit28:
23600 case DW_OP_lit29:
23601 case DW_OP_lit30:
23602 case DW_OP_lit31:
23603 stack[++stacki] = op - DW_OP_lit0;
23604 break;
23605
23606 case DW_OP_reg0:
23607 case DW_OP_reg1:
23608 case DW_OP_reg2:
23609 case DW_OP_reg3:
23610 case DW_OP_reg4:
23611 case DW_OP_reg5:
23612 case DW_OP_reg6:
23613 case DW_OP_reg7:
23614 case DW_OP_reg8:
23615 case DW_OP_reg9:
23616 case DW_OP_reg10:
23617 case DW_OP_reg11:
23618 case DW_OP_reg12:
23619 case DW_OP_reg13:
23620 case DW_OP_reg14:
23621 case DW_OP_reg15:
23622 case DW_OP_reg16:
23623 case DW_OP_reg17:
23624 case DW_OP_reg18:
23625 case DW_OP_reg19:
23626 case DW_OP_reg20:
23627 case DW_OP_reg21:
23628 case DW_OP_reg22:
23629 case DW_OP_reg23:
23630 case DW_OP_reg24:
23631 case DW_OP_reg25:
23632 case DW_OP_reg26:
23633 case DW_OP_reg27:
23634 case DW_OP_reg28:
23635 case DW_OP_reg29:
23636 case DW_OP_reg30:
23637 case DW_OP_reg31:
23638 stack[++stacki] = op - DW_OP_reg0;
23639 if (i < size)
23640 dwarf2_complex_location_expr_complaint ();
23641 break;
23642
23643 case DW_OP_regx:
23644 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23645 i += bytes_read;
23646 stack[++stacki] = unsnd;
23647 if (i < size)
23648 dwarf2_complex_location_expr_complaint ();
23649 break;
23650
23651 case DW_OP_addr:
23652 stack[++stacki] = read_address (objfile->obfd, &data[i],
23653 cu, &bytes_read);
23654 i += bytes_read;
23655 break;
23656
23657 case DW_OP_const1u:
23658 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23659 i += 1;
23660 break;
23661
23662 case DW_OP_const1s:
23663 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23664 i += 1;
23665 break;
23666
23667 case DW_OP_const2u:
23668 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23669 i += 2;
23670 break;
23671
23672 case DW_OP_const2s:
23673 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23674 i += 2;
23675 break;
23676
23677 case DW_OP_const4u:
23678 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23679 i += 4;
23680 break;
23681
23682 case DW_OP_const4s:
23683 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23684 i += 4;
23685 break;
23686
23687 case DW_OP_const8u:
23688 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23689 i += 8;
23690 break;
23691
23692 case DW_OP_constu:
23693 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23694 &bytes_read);
23695 i += bytes_read;
23696 break;
23697
23698 case DW_OP_consts:
23699 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23700 i += bytes_read;
23701 break;
23702
23703 case DW_OP_dup:
23704 stack[stacki + 1] = stack[stacki];
23705 stacki++;
23706 break;
23707
23708 case DW_OP_plus:
23709 stack[stacki - 1] += stack[stacki];
23710 stacki--;
23711 break;
23712
23713 case DW_OP_plus_uconst:
23714 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23715 &bytes_read);
23716 i += bytes_read;
23717 break;
23718
23719 case DW_OP_minus:
23720 stack[stacki - 1] -= stack[stacki];
23721 stacki--;
23722 break;
23723
23724 case DW_OP_deref:
23725 /* If we're not the last op, then we definitely can't encode
23726 this using GDB's address_class enum. This is valid for partial
23727 global symbols, although the variable's address will be bogus
23728 in the psymtab. */
23729 if (i < size)
23730 dwarf2_complex_location_expr_complaint ();
23731 break;
23732
23733 case DW_OP_GNU_push_tls_address:
23734 case DW_OP_form_tls_address:
23735 /* The top of the stack has the offset from the beginning
23736 of the thread control block at which the variable is located. */
23737 /* Nothing should follow this operator, so the top of stack would
23738 be returned. */
23739 /* This is valid for partial global symbols, but the variable's
23740 address will be bogus in the psymtab. Make it always at least
23741 non-zero to not look as a variable garbage collected by linker
23742 which have DW_OP_addr 0. */
23743 if (i < size)
23744 dwarf2_complex_location_expr_complaint ();
23745 stack[stacki]++;
23746 break;
23747
23748 case DW_OP_GNU_uninit:
23749 break;
23750
23751 case DW_OP_addrx:
23752 case DW_OP_GNU_addr_index:
23753 case DW_OP_GNU_const_index:
23754 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23755 &bytes_read);
23756 i += bytes_read;
23757 break;
23758
23759 default:
23760 {
23761 const char *name = get_DW_OP_name (op);
23762
23763 if (name)
23764 complaint (_("unsupported stack op: '%s'"),
23765 name);
23766 else
23767 complaint (_("unsupported stack op: '%02x'"),
23768 op);
23769 }
23770
23771 return (stack[stacki]);
23772 }
23773
23774 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23775 outside of the allocated space. Also enforce minimum>0. */
23776 if (stacki >= ARRAY_SIZE (stack) - 1)
23777 {
23778 complaint (_("location description stack overflow"));
23779 return 0;
23780 }
23781
23782 if (stacki <= 0)
23783 {
23784 complaint (_("location description stack underflow"));
23785 return 0;
23786 }
23787 }
23788 return (stack[stacki]);
23789 }
23790
23791 /* memory allocation interface */
23792
23793 static struct dwarf_block *
23794 dwarf_alloc_block (struct dwarf2_cu *cu)
23795 {
23796 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23797 }
23798
23799 static struct die_info *
23800 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23801 {
23802 struct die_info *die;
23803 size_t size = sizeof (struct die_info);
23804
23805 if (num_attrs > 1)
23806 size += (num_attrs - 1) * sizeof (struct attribute);
23807
23808 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23809 memset (die, 0, sizeof (struct die_info));
23810 return (die);
23811 }
23812
23813 \f
23814 /* Macro support. */
23815
23816 /* Return file name relative to the compilation directory of file number I in
23817 *LH's file name table. The result is allocated using xmalloc; the caller is
23818 responsible for freeing it. */
23819
23820 static char *
23821 file_file_name (int file, struct line_header *lh)
23822 {
23823 /* Is the file number a valid index into the line header's file name
23824 table? Remember that file numbers start with one, not zero. */
23825 if (lh->is_valid_file_index (file))
23826 {
23827 const file_entry *fe = lh->file_name_at (file);
23828
23829 if (!IS_ABSOLUTE_PATH (fe->name))
23830 {
23831 const char *dir = fe->include_dir (lh);
23832 if (dir != NULL)
23833 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
23834 }
23835 return xstrdup (fe->name);
23836 }
23837 else
23838 {
23839 /* The compiler produced a bogus file number. We can at least
23840 record the macro definitions made in the file, even if we
23841 won't be able to find the file by name. */
23842 char fake_name[80];
23843
23844 xsnprintf (fake_name, sizeof (fake_name),
23845 "<bad macro file number %d>", file);
23846
23847 complaint (_("bad file number in macro information (%d)"),
23848 file);
23849
23850 return xstrdup (fake_name);
23851 }
23852 }
23853
23854 /* Return the full name of file number I in *LH's file name table.
23855 Use COMP_DIR as the name of the current directory of the
23856 compilation. The result is allocated using xmalloc; the caller is
23857 responsible for freeing it. */
23858 static char *
23859 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23860 {
23861 /* Is the file number a valid index into the line header's file name
23862 table? Remember that file numbers start with one, not zero. */
23863 if (lh->is_valid_file_index (file))
23864 {
23865 char *relative = file_file_name (file, lh);
23866
23867 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23868 return relative;
23869 return reconcat (relative, comp_dir, SLASH_STRING,
23870 relative, (char *) NULL);
23871 }
23872 else
23873 return file_file_name (file, lh);
23874 }
23875
23876
23877 static struct macro_source_file *
23878 macro_start_file (struct dwarf2_cu *cu,
23879 int file, int line,
23880 struct macro_source_file *current_file,
23881 struct line_header *lh)
23882 {
23883 /* File name relative to the compilation directory of this source file. */
23884 char *file_name = file_file_name (file, lh);
23885
23886 if (! current_file)
23887 {
23888 /* Note: We don't create a macro table for this compilation unit
23889 at all until we actually get a filename. */
23890 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23891
23892 /* If we have no current file, then this must be the start_file
23893 directive for the compilation unit's main source file. */
23894 current_file = macro_set_main (macro_table, file_name);
23895 macro_define_special (macro_table);
23896 }
23897 else
23898 current_file = macro_include (current_file, line, file_name);
23899
23900 xfree (file_name);
23901
23902 return current_file;
23903 }
23904
23905 static const char *
23906 consume_improper_spaces (const char *p, const char *body)
23907 {
23908 if (*p == ' ')
23909 {
23910 complaint (_("macro definition contains spaces "
23911 "in formal argument list:\n`%s'"),
23912 body);
23913
23914 while (*p == ' ')
23915 p++;
23916 }
23917
23918 return p;
23919 }
23920
23921
23922 static void
23923 parse_macro_definition (struct macro_source_file *file, int line,
23924 const char *body)
23925 {
23926 const char *p;
23927
23928 /* The body string takes one of two forms. For object-like macro
23929 definitions, it should be:
23930
23931 <macro name> " " <definition>
23932
23933 For function-like macro definitions, it should be:
23934
23935 <macro name> "() " <definition>
23936 or
23937 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23938
23939 Spaces may appear only where explicitly indicated, and in the
23940 <definition>.
23941
23942 The Dwarf 2 spec says that an object-like macro's name is always
23943 followed by a space, but versions of GCC around March 2002 omit
23944 the space when the macro's definition is the empty string.
23945
23946 The Dwarf 2 spec says that there should be no spaces between the
23947 formal arguments in a function-like macro's formal argument list,
23948 but versions of GCC around March 2002 include spaces after the
23949 commas. */
23950
23951
23952 /* Find the extent of the macro name. The macro name is terminated
23953 by either a space or null character (for an object-like macro) or
23954 an opening paren (for a function-like macro). */
23955 for (p = body; *p; p++)
23956 if (*p == ' ' || *p == '(')
23957 break;
23958
23959 if (*p == ' ' || *p == '\0')
23960 {
23961 /* It's an object-like macro. */
23962 int name_len = p - body;
23963 std::string name (body, name_len);
23964 const char *replacement;
23965
23966 if (*p == ' ')
23967 replacement = body + name_len + 1;
23968 else
23969 {
23970 dwarf2_macro_malformed_definition_complaint (body);
23971 replacement = body + name_len;
23972 }
23973
23974 macro_define_object (file, line, name.c_str (), replacement);
23975 }
23976 else if (*p == '(')
23977 {
23978 /* It's a function-like macro. */
23979 std::string name (body, p - body);
23980 int argc = 0;
23981 int argv_size = 1;
23982 char **argv = XNEWVEC (char *, argv_size);
23983
23984 p++;
23985
23986 p = consume_improper_spaces (p, body);
23987
23988 /* Parse the formal argument list. */
23989 while (*p && *p != ')')
23990 {
23991 /* Find the extent of the current argument name. */
23992 const char *arg_start = p;
23993
23994 while (*p && *p != ',' && *p != ')' && *p != ' ')
23995 p++;
23996
23997 if (! *p || p == arg_start)
23998 dwarf2_macro_malformed_definition_complaint (body);
23999 else
24000 {
24001 /* Make sure argv has room for the new argument. */
24002 if (argc >= argv_size)
24003 {
24004 argv_size *= 2;
24005 argv = XRESIZEVEC (char *, argv, argv_size);
24006 }
24007
24008 argv[argc++] = savestring (arg_start, p - arg_start);
24009 }
24010
24011 p = consume_improper_spaces (p, body);
24012
24013 /* Consume the comma, if present. */
24014 if (*p == ',')
24015 {
24016 p++;
24017
24018 p = consume_improper_spaces (p, body);
24019 }
24020 }
24021
24022 if (*p == ')')
24023 {
24024 p++;
24025
24026 if (*p == ' ')
24027 /* Perfectly formed definition, no complaints. */
24028 macro_define_function (file, line, name.c_str (),
24029 argc, (const char **) argv,
24030 p + 1);
24031 else if (*p == '\0')
24032 {
24033 /* Complain, but do define it. */
24034 dwarf2_macro_malformed_definition_complaint (body);
24035 macro_define_function (file, line, name.c_str (),
24036 argc, (const char **) argv,
24037 p);
24038 }
24039 else
24040 /* Just complain. */
24041 dwarf2_macro_malformed_definition_complaint (body);
24042 }
24043 else
24044 /* Just complain. */
24045 dwarf2_macro_malformed_definition_complaint (body);
24046
24047 {
24048 int i;
24049
24050 for (i = 0; i < argc; i++)
24051 xfree (argv[i]);
24052 }
24053 xfree (argv);
24054 }
24055 else
24056 dwarf2_macro_malformed_definition_complaint (body);
24057 }
24058
24059 /* Skip some bytes from BYTES according to the form given in FORM.
24060 Returns the new pointer. */
24061
24062 static const gdb_byte *
24063 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24064 enum dwarf_form form,
24065 unsigned int offset_size,
24066 struct dwarf2_section_info *section)
24067 {
24068 unsigned int bytes_read;
24069
24070 switch (form)
24071 {
24072 case DW_FORM_data1:
24073 case DW_FORM_flag:
24074 ++bytes;
24075 break;
24076
24077 case DW_FORM_data2:
24078 bytes += 2;
24079 break;
24080
24081 case DW_FORM_data4:
24082 bytes += 4;
24083 break;
24084
24085 case DW_FORM_data8:
24086 bytes += 8;
24087 break;
24088
24089 case DW_FORM_data16:
24090 bytes += 16;
24091 break;
24092
24093 case DW_FORM_string:
24094 read_direct_string (abfd, bytes, &bytes_read);
24095 bytes += bytes_read;
24096 break;
24097
24098 case DW_FORM_sec_offset:
24099 case DW_FORM_strp:
24100 case DW_FORM_GNU_strp_alt:
24101 bytes += offset_size;
24102 break;
24103
24104 case DW_FORM_block:
24105 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24106 bytes += bytes_read;
24107 break;
24108
24109 case DW_FORM_block1:
24110 bytes += 1 + read_1_byte (abfd, bytes);
24111 break;
24112 case DW_FORM_block2:
24113 bytes += 2 + read_2_bytes (abfd, bytes);
24114 break;
24115 case DW_FORM_block4:
24116 bytes += 4 + read_4_bytes (abfd, bytes);
24117 break;
24118
24119 case DW_FORM_addrx:
24120 case DW_FORM_sdata:
24121 case DW_FORM_strx:
24122 case DW_FORM_udata:
24123 case DW_FORM_GNU_addr_index:
24124 case DW_FORM_GNU_str_index:
24125 bytes = gdb_skip_leb128 (bytes, buffer_end);
24126 if (bytes == NULL)
24127 {
24128 dwarf2_section_buffer_overflow_complaint (section);
24129 return NULL;
24130 }
24131 break;
24132
24133 case DW_FORM_implicit_const:
24134 break;
24135
24136 default:
24137 {
24138 complaint (_("invalid form 0x%x in `%s'"),
24139 form, section->get_name ());
24140 return NULL;
24141 }
24142 }
24143
24144 return bytes;
24145 }
24146
24147 /* A helper for dwarf_decode_macros that handles skipping an unknown
24148 opcode. Returns an updated pointer to the macro data buffer; or,
24149 on error, issues a complaint and returns NULL. */
24150
24151 static const gdb_byte *
24152 skip_unknown_opcode (unsigned int opcode,
24153 const gdb_byte **opcode_definitions,
24154 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24155 bfd *abfd,
24156 unsigned int offset_size,
24157 struct dwarf2_section_info *section)
24158 {
24159 unsigned int bytes_read, i;
24160 unsigned long arg;
24161 const gdb_byte *defn;
24162
24163 if (opcode_definitions[opcode] == NULL)
24164 {
24165 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24166 opcode);
24167 return NULL;
24168 }
24169
24170 defn = opcode_definitions[opcode];
24171 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24172 defn += bytes_read;
24173
24174 for (i = 0; i < arg; ++i)
24175 {
24176 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24177 (enum dwarf_form) defn[i], offset_size,
24178 section);
24179 if (mac_ptr == NULL)
24180 {
24181 /* skip_form_bytes already issued the complaint. */
24182 return NULL;
24183 }
24184 }
24185
24186 return mac_ptr;
24187 }
24188
24189 /* A helper function which parses the header of a macro section.
24190 If the macro section is the extended (for now called "GNU") type,
24191 then this updates *OFFSET_SIZE. Returns a pointer to just after
24192 the header, or issues a complaint and returns NULL on error. */
24193
24194 static const gdb_byte *
24195 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24196 bfd *abfd,
24197 const gdb_byte *mac_ptr,
24198 unsigned int *offset_size,
24199 int section_is_gnu)
24200 {
24201 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24202
24203 if (section_is_gnu)
24204 {
24205 unsigned int version, flags;
24206
24207 version = read_2_bytes (abfd, mac_ptr);
24208 if (version != 4 && version != 5)
24209 {
24210 complaint (_("unrecognized version `%d' in .debug_macro section"),
24211 version);
24212 return NULL;
24213 }
24214 mac_ptr += 2;
24215
24216 flags = read_1_byte (abfd, mac_ptr);
24217 ++mac_ptr;
24218 *offset_size = (flags & 1) ? 8 : 4;
24219
24220 if ((flags & 2) != 0)
24221 /* We don't need the line table offset. */
24222 mac_ptr += *offset_size;
24223
24224 /* Vendor opcode descriptions. */
24225 if ((flags & 4) != 0)
24226 {
24227 unsigned int i, count;
24228
24229 count = read_1_byte (abfd, mac_ptr);
24230 ++mac_ptr;
24231 for (i = 0; i < count; ++i)
24232 {
24233 unsigned int opcode, bytes_read;
24234 unsigned long arg;
24235
24236 opcode = read_1_byte (abfd, mac_ptr);
24237 ++mac_ptr;
24238 opcode_definitions[opcode] = mac_ptr;
24239 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24240 mac_ptr += bytes_read;
24241 mac_ptr += arg;
24242 }
24243 }
24244 }
24245
24246 return mac_ptr;
24247 }
24248
24249 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24250 including DW_MACRO_import. */
24251
24252 static void
24253 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24254 bfd *abfd,
24255 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24256 struct macro_source_file *current_file,
24257 struct line_header *lh,
24258 struct dwarf2_section_info *section,
24259 int section_is_gnu, int section_is_dwz,
24260 unsigned int offset_size,
24261 htab_t include_hash)
24262 {
24263 struct dwarf2_per_objfile *dwarf2_per_objfile
24264 = cu->per_cu->dwarf2_per_objfile;
24265 struct objfile *objfile = dwarf2_per_objfile->objfile;
24266 enum dwarf_macro_record_type macinfo_type;
24267 int at_commandline;
24268 const gdb_byte *opcode_definitions[256];
24269
24270 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24271 &offset_size, section_is_gnu);
24272 if (mac_ptr == NULL)
24273 {
24274 /* We already issued a complaint. */
24275 return;
24276 }
24277
24278 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24279 GDB is still reading the definitions from command line. First
24280 DW_MACINFO_start_file will need to be ignored as it was already executed
24281 to create CURRENT_FILE for the main source holding also the command line
24282 definitions. On first met DW_MACINFO_start_file this flag is reset to
24283 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24284
24285 at_commandline = 1;
24286
24287 do
24288 {
24289 /* Do we at least have room for a macinfo type byte? */
24290 if (mac_ptr >= mac_end)
24291 {
24292 dwarf2_section_buffer_overflow_complaint (section);
24293 break;
24294 }
24295
24296 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24297 mac_ptr++;
24298
24299 /* Note that we rely on the fact that the corresponding GNU and
24300 DWARF constants are the same. */
24301 DIAGNOSTIC_PUSH
24302 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24303 switch (macinfo_type)
24304 {
24305 /* A zero macinfo type indicates the end of the macro
24306 information. */
24307 case 0:
24308 break;
24309
24310 case DW_MACRO_define:
24311 case DW_MACRO_undef:
24312 case DW_MACRO_define_strp:
24313 case DW_MACRO_undef_strp:
24314 case DW_MACRO_define_sup:
24315 case DW_MACRO_undef_sup:
24316 {
24317 unsigned int bytes_read;
24318 int line;
24319 const char *body;
24320 int is_define;
24321
24322 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24323 mac_ptr += bytes_read;
24324
24325 if (macinfo_type == DW_MACRO_define
24326 || macinfo_type == DW_MACRO_undef)
24327 {
24328 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24329 mac_ptr += bytes_read;
24330 }
24331 else
24332 {
24333 LONGEST str_offset;
24334
24335 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24336 mac_ptr += offset_size;
24337
24338 if (macinfo_type == DW_MACRO_define_sup
24339 || macinfo_type == DW_MACRO_undef_sup
24340 || section_is_dwz)
24341 {
24342 struct dwz_file *dwz
24343 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24344
24345 body = read_indirect_string_from_dwz (objfile,
24346 dwz, str_offset);
24347 }
24348 else
24349 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24350 abfd, str_offset);
24351 }
24352
24353 is_define = (macinfo_type == DW_MACRO_define
24354 || macinfo_type == DW_MACRO_define_strp
24355 || macinfo_type == DW_MACRO_define_sup);
24356 if (! current_file)
24357 {
24358 /* DWARF violation as no main source is present. */
24359 complaint (_("debug info with no main source gives macro %s "
24360 "on line %d: %s"),
24361 is_define ? _("definition") : _("undefinition"),
24362 line, body);
24363 break;
24364 }
24365 if ((line == 0 && !at_commandline)
24366 || (line != 0 && at_commandline))
24367 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24368 at_commandline ? _("command-line") : _("in-file"),
24369 is_define ? _("definition") : _("undefinition"),
24370 line == 0 ? _("zero") : _("non-zero"), line, body);
24371
24372 if (body == NULL)
24373 {
24374 /* Fedora's rpm-build's "debugedit" binary
24375 corrupted .debug_macro sections.
24376
24377 For more info, see
24378 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24379 complaint (_("debug info gives %s invalid macro %s "
24380 "without body (corrupted?) at line %d "
24381 "on file %s"),
24382 at_commandline ? _("command-line") : _("in-file"),
24383 is_define ? _("definition") : _("undefinition"),
24384 line, current_file->filename);
24385 }
24386 else if (is_define)
24387 parse_macro_definition (current_file, line, body);
24388 else
24389 {
24390 gdb_assert (macinfo_type == DW_MACRO_undef
24391 || macinfo_type == DW_MACRO_undef_strp
24392 || macinfo_type == DW_MACRO_undef_sup);
24393 macro_undef (current_file, line, body);
24394 }
24395 }
24396 break;
24397
24398 case DW_MACRO_start_file:
24399 {
24400 unsigned int bytes_read;
24401 int line, file;
24402
24403 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24404 mac_ptr += bytes_read;
24405 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24406 mac_ptr += bytes_read;
24407
24408 if ((line == 0 && !at_commandline)
24409 || (line != 0 && at_commandline))
24410 complaint (_("debug info gives source %d included "
24411 "from %s at %s line %d"),
24412 file, at_commandline ? _("command-line") : _("file"),
24413 line == 0 ? _("zero") : _("non-zero"), line);
24414
24415 if (at_commandline)
24416 {
24417 /* This DW_MACRO_start_file was executed in the
24418 pass one. */
24419 at_commandline = 0;
24420 }
24421 else
24422 current_file = macro_start_file (cu, file, line, current_file,
24423 lh);
24424 }
24425 break;
24426
24427 case DW_MACRO_end_file:
24428 if (! current_file)
24429 complaint (_("macro debug info has an unmatched "
24430 "`close_file' directive"));
24431 else
24432 {
24433 current_file = current_file->included_by;
24434 if (! current_file)
24435 {
24436 enum dwarf_macro_record_type next_type;
24437
24438 /* GCC circa March 2002 doesn't produce the zero
24439 type byte marking the end of the compilation
24440 unit. Complain if it's not there, but exit no
24441 matter what. */
24442
24443 /* Do we at least have room for a macinfo type byte? */
24444 if (mac_ptr >= mac_end)
24445 {
24446 dwarf2_section_buffer_overflow_complaint (section);
24447 return;
24448 }
24449
24450 /* We don't increment mac_ptr here, so this is just
24451 a look-ahead. */
24452 next_type
24453 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24454 mac_ptr);
24455 if (next_type != 0)
24456 complaint (_("no terminating 0-type entry for "
24457 "macros in `.debug_macinfo' section"));
24458
24459 return;
24460 }
24461 }
24462 break;
24463
24464 case DW_MACRO_import:
24465 case DW_MACRO_import_sup:
24466 {
24467 LONGEST offset;
24468 void **slot;
24469 bfd *include_bfd = abfd;
24470 struct dwarf2_section_info *include_section = section;
24471 const gdb_byte *include_mac_end = mac_end;
24472 int is_dwz = section_is_dwz;
24473 const gdb_byte *new_mac_ptr;
24474
24475 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24476 mac_ptr += offset_size;
24477
24478 if (macinfo_type == DW_MACRO_import_sup)
24479 {
24480 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24481
24482 dwz->macro.read (objfile);
24483
24484 include_section = &dwz->macro;
24485 include_bfd = include_section->get_bfd_owner ();
24486 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24487 is_dwz = 1;
24488 }
24489
24490 new_mac_ptr = include_section->buffer + offset;
24491 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24492
24493 if (*slot != NULL)
24494 {
24495 /* This has actually happened; see
24496 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24497 complaint (_("recursive DW_MACRO_import in "
24498 ".debug_macro section"));
24499 }
24500 else
24501 {
24502 *slot = (void *) new_mac_ptr;
24503
24504 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24505 include_mac_end, current_file, lh,
24506 section, section_is_gnu, is_dwz,
24507 offset_size, include_hash);
24508
24509 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24510 }
24511 }
24512 break;
24513
24514 case DW_MACINFO_vendor_ext:
24515 if (!section_is_gnu)
24516 {
24517 unsigned int bytes_read;
24518
24519 /* This reads the constant, but since we don't recognize
24520 any vendor extensions, we ignore it. */
24521 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24522 mac_ptr += bytes_read;
24523 read_direct_string (abfd, mac_ptr, &bytes_read);
24524 mac_ptr += bytes_read;
24525
24526 /* We don't recognize any vendor extensions. */
24527 break;
24528 }
24529 /* FALLTHROUGH */
24530
24531 default:
24532 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24533 mac_ptr, mac_end, abfd, offset_size,
24534 section);
24535 if (mac_ptr == NULL)
24536 return;
24537 break;
24538 }
24539 DIAGNOSTIC_POP
24540 } while (macinfo_type != 0);
24541 }
24542
24543 static void
24544 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24545 int section_is_gnu)
24546 {
24547 struct dwarf2_per_objfile *dwarf2_per_objfile
24548 = cu->per_cu->dwarf2_per_objfile;
24549 struct objfile *objfile = dwarf2_per_objfile->objfile;
24550 struct line_header *lh = cu->line_header;
24551 bfd *abfd;
24552 const gdb_byte *mac_ptr, *mac_end;
24553 struct macro_source_file *current_file = 0;
24554 enum dwarf_macro_record_type macinfo_type;
24555 unsigned int offset_size = cu->header.offset_size;
24556 const gdb_byte *opcode_definitions[256];
24557 void **slot;
24558 struct dwarf2_section_info *section;
24559 const char *section_name;
24560
24561 if (cu->dwo_unit != NULL)
24562 {
24563 if (section_is_gnu)
24564 {
24565 section = &cu->dwo_unit->dwo_file->sections.macro;
24566 section_name = ".debug_macro.dwo";
24567 }
24568 else
24569 {
24570 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24571 section_name = ".debug_macinfo.dwo";
24572 }
24573 }
24574 else
24575 {
24576 if (section_is_gnu)
24577 {
24578 section = &dwarf2_per_objfile->macro;
24579 section_name = ".debug_macro";
24580 }
24581 else
24582 {
24583 section = &dwarf2_per_objfile->macinfo;
24584 section_name = ".debug_macinfo";
24585 }
24586 }
24587
24588 section->read (objfile);
24589 if (section->buffer == NULL)
24590 {
24591 complaint (_("missing %s section"), section_name);
24592 return;
24593 }
24594 abfd = section->get_bfd_owner ();
24595
24596 /* First pass: Find the name of the base filename.
24597 This filename is needed in order to process all macros whose definition
24598 (or undefinition) comes from the command line. These macros are defined
24599 before the first DW_MACINFO_start_file entry, and yet still need to be
24600 associated to the base file.
24601
24602 To determine the base file name, we scan the macro definitions until we
24603 reach the first DW_MACINFO_start_file entry. We then initialize
24604 CURRENT_FILE accordingly so that any macro definition found before the
24605 first DW_MACINFO_start_file can still be associated to the base file. */
24606
24607 mac_ptr = section->buffer + offset;
24608 mac_end = section->buffer + section->size;
24609
24610 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24611 &offset_size, section_is_gnu);
24612 if (mac_ptr == NULL)
24613 {
24614 /* We already issued a complaint. */
24615 return;
24616 }
24617
24618 do
24619 {
24620 /* Do we at least have room for a macinfo type byte? */
24621 if (mac_ptr >= mac_end)
24622 {
24623 /* Complaint is printed during the second pass as GDB will probably
24624 stop the first pass earlier upon finding
24625 DW_MACINFO_start_file. */
24626 break;
24627 }
24628
24629 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24630 mac_ptr++;
24631
24632 /* Note that we rely on the fact that the corresponding GNU and
24633 DWARF constants are the same. */
24634 DIAGNOSTIC_PUSH
24635 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24636 switch (macinfo_type)
24637 {
24638 /* A zero macinfo type indicates the end of the macro
24639 information. */
24640 case 0:
24641 break;
24642
24643 case DW_MACRO_define:
24644 case DW_MACRO_undef:
24645 /* Only skip the data by MAC_PTR. */
24646 {
24647 unsigned int bytes_read;
24648
24649 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24650 mac_ptr += bytes_read;
24651 read_direct_string (abfd, mac_ptr, &bytes_read);
24652 mac_ptr += bytes_read;
24653 }
24654 break;
24655
24656 case DW_MACRO_start_file:
24657 {
24658 unsigned int bytes_read;
24659 int line, file;
24660
24661 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24662 mac_ptr += bytes_read;
24663 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24664 mac_ptr += bytes_read;
24665
24666 current_file = macro_start_file (cu, file, line, current_file, lh);
24667 }
24668 break;
24669
24670 case DW_MACRO_end_file:
24671 /* No data to skip by MAC_PTR. */
24672 break;
24673
24674 case DW_MACRO_define_strp:
24675 case DW_MACRO_undef_strp:
24676 case DW_MACRO_define_sup:
24677 case DW_MACRO_undef_sup:
24678 {
24679 unsigned int bytes_read;
24680
24681 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24682 mac_ptr += bytes_read;
24683 mac_ptr += offset_size;
24684 }
24685 break;
24686
24687 case DW_MACRO_import:
24688 case DW_MACRO_import_sup:
24689 /* Note that, according to the spec, a transparent include
24690 chain cannot call DW_MACRO_start_file. So, we can just
24691 skip this opcode. */
24692 mac_ptr += offset_size;
24693 break;
24694
24695 case DW_MACINFO_vendor_ext:
24696 /* Only skip the data by MAC_PTR. */
24697 if (!section_is_gnu)
24698 {
24699 unsigned int bytes_read;
24700
24701 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24702 mac_ptr += bytes_read;
24703 read_direct_string (abfd, mac_ptr, &bytes_read);
24704 mac_ptr += bytes_read;
24705 }
24706 /* FALLTHROUGH */
24707
24708 default:
24709 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24710 mac_ptr, mac_end, abfd, offset_size,
24711 section);
24712 if (mac_ptr == NULL)
24713 return;
24714 break;
24715 }
24716 DIAGNOSTIC_POP
24717 } while (macinfo_type != 0 && current_file == NULL);
24718
24719 /* Second pass: Process all entries.
24720
24721 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24722 command-line macro definitions/undefinitions. This flag is unset when we
24723 reach the first DW_MACINFO_start_file entry. */
24724
24725 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24726 htab_eq_pointer,
24727 NULL, xcalloc, xfree));
24728 mac_ptr = section->buffer + offset;
24729 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24730 *slot = (void *) mac_ptr;
24731 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24732 current_file, lh, section,
24733 section_is_gnu, 0, offset_size,
24734 include_hash.get ());
24735 }
24736
24737 /* Return the .debug_loc section to use for CU.
24738 For DWO files use .debug_loc.dwo. */
24739
24740 static struct dwarf2_section_info *
24741 cu_debug_loc_section (struct dwarf2_cu *cu)
24742 {
24743 struct dwarf2_per_objfile *dwarf2_per_objfile
24744 = cu->per_cu->dwarf2_per_objfile;
24745
24746 if (cu->dwo_unit)
24747 {
24748 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24749
24750 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24751 }
24752 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24753 : &dwarf2_per_objfile->loc);
24754 }
24755
24756 /* A helper function that fills in a dwarf2_loclist_baton. */
24757
24758 static void
24759 fill_in_loclist_baton (struct dwarf2_cu *cu,
24760 struct dwarf2_loclist_baton *baton,
24761 const struct attribute *attr)
24762 {
24763 struct dwarf2_per_objfile *dwarf2_per_objfile
24764 = cu->per_cu->dwarf2_per_objfile;
24765 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24766
24767 section->read (dwarf2_per_objfile->objfile);
24768
24769 baton->per_cu = cu->per_cu;
24770 gdb_assert (baton->per_cu);
24771 /* We don't know how long the location list is, but make sure we
24772 don't run off the edge of the section. */
24773 baton->size = section->size - DW_UNSND (attr);
24774 baton->data = section->buffer + DW_UNSND (attr);
24775 baton->base_address = cu->base_address;
24776 baton->from_dwo = cu->dwo_unit != NULL;
24777 }
24778
24779 static void
24780 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24781 struct dwarf2_cu *cu, int is_block)
24782 {
24783 struct dwarf2_per_objfile *dwarf2_per_objfile
24784 = cu->per_cu->dwarf2_per_objfile;
24785 struct objfile *objfile = dwarf2_per_objfile->objfile;
24786 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24787
24788 if (attr->form_is_section_offset ()
24789 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24790 the section. If so, fall through to the complaint in the
24791 other branch. */
24792 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24793 {
24794 struct dwarf2_loclist_baton *baton;
24795
24796 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24797
24798 fill_in_loclist_baton (cu, baton, attr);
24799
24800 if (cu->base_known == 0)
24801 complaint (_("Location list used without "
24802 "specifying the CU base address."));
24803
24804 SYMBOL_ACLASS_INDEX (sym) = (is_block
24805 ? dwarf2_loclist_block_index
24806 : dwarf2_loclist_index);
24807 SYMBOL_LOCATION_BATON (sym) = baton;
24808 }
24809 else
24810 {
24811 struct dwarf2_locexpr_baton *baton;
24812
24813 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24814 baton->per_cu = cu->per_cu;
24815 gdb_assert (baton->per_cu);
24816
24817 if (attr->form_is_block ())
24818 {
24819 /* Note that we're just copying the block's data pointer
24820 here, not the actual data. We're still pointing into the
24821 info_buffer for SYM's objfile; right now we never release
24822 that buffer, but when we do clean up properly this may
24823 need to change. */
24824 baton->size = DW_BLOCK (attr)->size;
24825 baton->data = DW_BLOCK (attr)->data;
24826 }
24827 else
24828 {
24829 dwarf2_invalid_attrib_class_complaint ("location description",
24830 sym->natural_name ());
24831 baton->size = 0;
24832 }
24833
24834 SYMBOL_ACLASS_INDEX (sym) = (is_block
24835 ? dwarf2_locexpr_block_index
24836 : dwarf2_locexpr_index);
24837 SYMBOL_LOCATION_BATON (sym) = baton;
24838 }
24839 }
24840
24841 /* Return the OBJFILE associated with the compilation unit CU. If CU
24842 came from a separate debuginfo file, then the master objfile is
24843 returned. */
24844
24845 struct objfile *
24846 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24847 {
24848 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24849
24850 /* Return the master objfile, so that we can report and look up the
24851 correct file containing this variable. */
24852 if (objfile->separate_debug_objfile_backlink)
24853 objfile = objfile->separate_debug_objfile_backlink;
24854
24855 return objfile;
24856 }
24857
24858 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24859 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24860 CU_HEADERP first. */
24861
24862 static const struct comp_unit_head *
24863 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24864 struct dwarf2_per_cu_data *per_cu)
24865 {
24866 const gdb_byte *info_ptr;
24867
24868 if (per_cu->cu)
24869 return &per_cu->cu->header;
24870
24871 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24872
24873 memset (cu_headerp, 0, sizeof (*cu_headerp));
24874 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24875 rcuh_kind::COMPILE);
24876
24877 return cu_headerp;
24878 }
24879
24880 /* Return the address size given in the compilation unit header for CU. */
24881
24882 int
24883 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24884 {
24885 struct comp_unit_head cu_header_local;
24886 const struct comp_unit_head *cu_headerp;
24887
24888 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24889
24890 return cu_headerp->addr_size;
24891 }
24892
24893 /* Return the offset size given in the compilation unit header for CU. */
24894
24895 int
24896 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24897 {
24898 struct comp_unit_head cu_header_local;
24899 const struct comp_unit_head *cu_headerp;
24900
24901 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24902
24903 return cu_headerp->offset_size;
24904 }
24905
24906 /* See its dwarf2loc.h declaration. */
24907
24908 int
24909 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24910 {
24911 struct comp_unit_head cu_header_local;
24912 const struct comp_unit_head *cu_headerp;
24913
24914 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24915
24916 if (cu_headerp->version == 2)
24917 return cu_headerp->addr_size;
24918 else
24919 return cu_headerp->offset_size;
24920 }
24921
24922 /* Return the text offset of the CU. The returned offset comes from
24923 this CU's objfile. If this objfile came from a separate debuginfo
24924 file, then the offset may be different from the corresponding
24925 offset in the parent objfile. */
24926
24927 CORE_ADDR
24928 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24929 {
24930 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
24931 }
24932
24933 /* Return a type that is a generic pointer type, the size of which matches
24934 the address size given in the compilation unit header for PER_CU. */
24935 static struct type *
24936 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24937 {
24938 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24939 struct type *void_type = objfile_type (objfile)->builtin_void;
24940 struct type *addr_type = lookup_pointer_type (void_type);
24941 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24942
24943 if (TYPE_LENGTH (addr_type) == addr_size)
24944 return addr_type;
24945
24946 addr_type
24947 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24948 return addr_type;
24949 }
24950
24951 /* Return DWARF version number of PER_CU. */
24952
24953 short
24954 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24955 {
24956 return per_cu->dwarf_version;
24957 }
24958
24959 /* Locate the .debug_info compilation unit from CU's objfile which contains
24960 the DIE at OFFSET. Raises an error on failure. */
24961
24962 static struct dwarf2_per_cu_data *
24963 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24964 unsigned int offset_in_dwz,
24965 struct dwarf2_per_objfile *dwarf2_per_objfile)
24966 {
24967 struct dwarf2_per_cu_data *this_cu;
24968 int low, high;
24969
24970 low = 0;
24971 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24972 while (high > low)
24973 {
24974 struct dwarf2_per_cu_data *mid_cu;
24975 int mid = low + (high - low) / 2;
24976
24977 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24978 if (mid_cu->is_dwz > offset_in_dwz
24979 || (mid_cu->is_dwz == offset_in_dwz
24980 && mid_cu->sect_off + mid_cu->length >= sect_off))
24981 high = mid;
24982 else
24983 low = mid + 1;
24984 }
24985 gdb_assert (low == high);
24986 this_cu = dwarf2_per_objfile->all_comp_units[low];
24987 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24988 {
24989 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24990 error (_("Dwarf Error: could not find partial DIE containing "
24991 "offset %s [in module %s]"),
24992 sect_offset_str (sect_off),
24993 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24994
24995 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24996 <= sect_off);
24997 return dwarf2_per_objfile->all_comp_units[low-1];
24998 }
24999 else
25000 {
25001 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25002 && sect_off >= this_cu->sect_off + this_cu->length)
25003 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25004 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25005 return this_cu;
25006 }
25007 }
25008
25009 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25010
25011 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25012 : per_cu (per_cu_),
25013 mark (false),
25014 has_loclist (false),
25015 checked_producer (false),
25016 producer_is_gxx_lt_4_6 (false),
25017 producer_is_gcc_lt_4_3 (false),
25018 producer_is_icc (false),
25019 producer_is_icc_lt_14 (false),
25020 producer_is_codewarrior (false),
25021 processing_has_namespace_info (false)
25022 {
25023 per_cu->cu = this;
25024 }
25025
25026 /* Destroy a dwarf2_cu. */
25027
25028 dwarf2_cu::~dwarf2_cu ()
25029 {
25030 per_cu->cu = NULL;
25031 }
25032
25033 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25034
25035 static void
25036 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25037 enum language pretend_language)
25038 {
25039 struct attribute *attr;
25040
25041 /* Set the language we're debugging. */
25042 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25043 if (attr != nullptr)
25044 set_cu_language (DW_UNSND (attr), cu);
25045 else
25046 {
25047 cu->language = pretend_language;
25048 cu->language_defn = language_def (cu->language);
25049 }
25050
25051 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25052 }
25053
25054 /* Increase the age counter on each cached compilation unit, and free
25055 any that are too old. */
25056
25057 static void
25058 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25059 {
25060 struct dwarf2_per_cu_data *per_cu, **last_chain;
25061
25062 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25063 per_cu = dwarf2_per_objfile->read_in_chain;
25064 while (per_cu != NULL)
25065 {
25066 per_cu->cu->last_used ++;
25067 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25068 dwarf2_mark (per_cu->cu);
25069 per_cu = per_cu->cu->read_in_chain;
25070 }
25071
25072 per_cu = dwarf2_per_objfile->read_in_chain;
25073 last_chain = &dwarf2_per_objfile->read_in_chain;
25074 while (per_cu != NULL)
25075 {
25076 struct dwarf2_per_cu_data *next_cu;
25077
25078 next_cu = per_cu->cu->read_in_chain;
25079
25080 if (!per_cu->cu->mark)
25081 {
25082 delete per_cu->cu;
25083 *last_chain = next_cu;
25084 }
25085 else
25086 last_chain = &per_cu->cu->read_in_chain;
25087
25088 per_cu = next_cu;
25089 }
25090 }
25091
25092 /* Remove a single compilation unit from the cache. */
25093
25094 static void
25095 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25096 {
25097 struct dwarf2_per_cu_data *per_cu, **last_chain;
25098 struct dwarf2_per_objfile *dwarf2_per_objfile
25099 = target_per_cu->dwarf2_per_objfile;
25100
25101 per_cu = dwarf2_per_objfile->read_in_chain;
25102 last_chain = &dwarf2_per_objfile->read_in_chain;
25103 while (per_cu != NULL)
25104 {
25105 struct dwarf2_per_cu_data *next_cu;
25106
25107 next_cu = per_cu->cu->read_in_chain;
25108
25109 if (per_cu == target_per_cu)
25110 {
25111 delete per_cu->cu;
25112 per_cu->cu = NULL;
25113 *last_chain = next_cu;
25114 break;
25115 }
25116 else
25117 last_chain = &per_cu->cu->read_in_chain;
25118
25119 per_cu = next_cu;
25120 }
25121 }
25122
25123 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25124 We store these in a hash table separate from the DIEs, and preserve them
25125 when the DIEs are flushed out of cache.
25126
25127 The CU "per_cu" pointer is needed because offset alone is not enough to
25128 uniquely identify the type. A file may have multiple .debug_types sections,
25129 or the type may come from a DWO file. Furthermore, while it's more logical
25130 to use per_cu->section+offset, with Fission the section with the data is in
25131 the DWO file but we don't know that section at the point we need it.
25132 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25133 because we can enter the lookup routine, get_die_type_at_offset, from
25134 outside this file, and thus won't necessarily have PER_CU->cu.
25135 Fortunately, PER_CU is stable for the life of the objfile. */
25136
25137 struct dwarf2_per_cu_offset_and_type
25138 {
25139 const struct dwarf2_per_cu_data *per_cu;
25140 sect_offset sect_off;
25141 struct type *type;
25142 };
25143
25144 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25145
25146 static hashval_t
25147 per_cu_offset_and_type_hash (const void *item)
25148 {
25149 const struct dwarf2_per_cu_offset_and_type *ofs
25150 = (const struct dwarf2_per_cu_offset_and_type *) item;
25151
25152 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25153 }
25154
25155 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25156
25157 static int
25158 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25159 {
25160 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25161 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25162 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25163 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25164
25165 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25166 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25167 }
25168
25169 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25170 table if necessary. For convenience, return TYPE.
25171
25172 The DIEs reading must have careful ordering to:
25173 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25174 reading current DIE.
25175 * Not trying to dereference contents of still incompletely read in types
25176 while reading in other DIEs.
25177 * Enable referencing still incompletely read in types just by a pointer to
25178 the type without accessing its fields.
25179
25180 Therefore caller should follow these rules:
25181 * Try to fetch any prerequisite types we may need to build this DIE type
25182 before building the type and calling set_die_type.
25183 * After building type call set_die_type for current DIE as soon as
25184 possible before fetching more types to complete the current type.
25185 * Make the type as complete as possible before fetching more types. */
25186
25187 static struct type *
25188 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25189 {
25190 struct dwarf2_per_objfile *dwarf2_per_objfile
25191 = cu->per_cu->dwarf2_per_objfile;
25192 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25193 struct objfile *objfile = dwarf2_per_objfile->objfile;
25194 struct attribute *attr;
25195 struct dynamic_prop prop;
25196
25197 /* For Ada types, make sure that the gnat-specific data is always
25198 initialized (if not already set). There are a few types where
25199 we should not be doing so, because the type-specific area is
25200 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25201 where the type-specific area is used to store the floatformat).
25202 But this is not a problem, because the gnat-specific information
25203 is actually not needed for these types. */
25204 if (need_gnat_info (cu)
25205 && TYPE_CODE (type) != TYPE_CODE_FUNC
25206 && TYPE_CODE (type) != TYPE_CODE_FLT
25207 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25208 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25209 && TYPE_CODE (type) != TYPE_CODE_METHOD
25210 && !HAVE_GNAT_AUX_INFO (type))
25211 INIT_GNAT_SPECIFIC (type);
25212
25213 /* Read DW_AT_allocated and set in type. */
25214 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25215 if (attr != NULL && attr->form_is_block ())
25216 {
25217 struct type *prop_type
25218 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25219 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25220 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25221 }
25222 else if (attr != NULL)
25223 {
25224 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25225 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25226 sect_offset_str (die->sect_off));
25227 }
25228
25229 /* Read DW_AT_associated and set in type. */
25230 attr = dwarf2_attr (die, DW_AT_associated, cu);
25231 if (attr != NULL && attr->form_is_block ())
25232 {
25233 struct type *prop_type
25234 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25235 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25236 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25237 }
25238 else if (attr != NULL)
25239 {
25240 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25241 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25242 sect_offset_str (die->sect_off));
25243 }
25244
25245 /* Read DW_AT_data_location and set in type. */
25246 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25247 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25248 dwarf2_per_cu_addr_type (cu->per_cu)))
25249 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25250
25251 if (dwarf2_per_objfile->die_type_hash == NULL)
25252 {
25253 dwarf2_per_objfile->die_type_hash =
25254 htab_create_alloc_ex (127,
25255 per_cu_offset_and_type_hash,
25256 per_cu_offset_and_type_eq,
25257 NULL,
25258 &objfile->objfile_obstack,
25259 hashtab_obstack_allocate,
25260 dummy_obstack_deallocate);
25261 }
25262
25263 ofs.per_cu = cu->per_cu;
25264 ofs.sect_off = die->sect_off;
25265 ofs.type = type;
25266 slot = (struct dwarf2_per_cu_offset_and_type **)
25267 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25268 if (*slot)
25269 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25270 sect_offset_str (die->sect_off));
25271 *slot = XOBNEW (&objfile->objfile_obstack,
25272 struct dwarf2_per_cu_offset_and_type);
25273 **slot = ofs;
25274 return type;
25275 }
25276
25277 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25278 or return NULL if the die does not have a saved type. */
25279
25280 static struct type *
25281 get_die_type_at_offset (sect_offset sect_off,
25282 struct dwarf2_per_cu_data *per_cu)
25283 {
25284 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25286
25287 if (dwarf2_per_objfile->die_type_hash == NULL)
25288 return NULL;
25289
25290 ofs.per_cu = per_cu;
25291 ofs.sect_off = sect_off;
25292 slot = ((struct dwarf2_per_cu_offset_and_type *)
25293 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25294 if (slot)
25295 return slot->type;
25296 else
25297 return NULL;
25298 }
25299
25300 /* Look up the type for DIE in CU in die_type_hash,
25301 or return NULL if DIE does not have a saved type. */
25302
25303 static struct type *
25304 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25305 {
25306 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25307 }
25308
25309 /* Add a dependence relationship from CU to REF_PER_CU. */
25310
25311 static void
25312 dwarf2_add_dependence (struct dwarf2_cu *cu,
25313 struct dwarf2_per_cu_data *ref_per_cu)
25314 {
25315 void **slot;
25316
25317 if (cu->dependencies == NULL)
25318 cu->dependencies
25319 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25320 NULL, &cu->comp_unit_obstack,
25321 hashtab_obstack_allocate,
25322 dummy_obstack_deallocate);
25323
25324 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25325 if (*slot == NULL)
25326 *slot = ref_per_cu;
25327 }
25328
25329 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25330 Set the mark field in every compilation unit in the
25331 cache that we must keep because we are keeping CU. */
25332
25333 static int
25334 dwarf2_mark_helper (void **slot, void *data)
25335 {
25336 struct dwarf2_per_cu_data *per_cu;
25337
25338 per_cu = (struct dwarf2_per_cu_data *) *slot;
25339
25340 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25341 reading of the chain. As such dependencies remain valid it is not much
25342 useful to track and undo them during QUIT cleanups. */
25343 if (per_cu->cu == NULL)
25344 return 1;
25345
25346 if (per_cu->cu->mark)
25347 return 1;
25348 per_cu->cu->mark = true;
25349
25350 if (per_cu->cu->dependencies != NULL)
25351 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25352
25353 return 1;
25354 }
25355
25356 /* Set the mark field in CU and in every other compilation unit in the
25357 cache that we must keep because we are keeping CU. */
25358
25359 static void
25360 dwarf2_mark (struct dwarf2_cu *cu)
25361 {
25362 if (cu->mark)
25363 return;
25364 cu->mark = true;
25365 if (cu->dependencies != NULL)
25366 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25367 }
25368
25369 static void
25370 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25371 {
25372 while (per_cu)
25373 {
25374 per_cu->cu->mark = false;
25375 per_cu = per_cu->cu->read_in_chain;
25376 }
25377 }
25378
25379 /* Trivial hash function for partial_die_info: the hash value of a DIE
25380 is its offset in .debug_info for this objfile. */
25381
25382 static hashval_t
25383 partial_die_hash (const void *item)
25384 {
25385 const struct partial_die_info *part_die
25386 = (const struct partial_die_info *) item;
25387
25388 return to_underlying (part_die->sect_off);
25389 }
25390
25391 /* Trivial comparison function for partial_die_info structures: two DIEs
25392 are equal if they have the same offset. */
25393
25394 static int
25395 partial_die_eq (const void *item_lhs, const void *item_rhs)
25396 {
25397 const struct partial_die_info *part_die_lhs
25398 = (const struct partial_die_info *) item_lhs;
25399 const struct partial_die_info *part_die_rhs
25400 = (const struct partial_die_info *) item_rhs;
25401
25402 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25403 }
25404
25405 struct cmd_list_element *set_dwarf_cmdlist;
25406 struct cmd_list_element *show_dwarf_cmdlist;
25407
25408 static void
25409 set_dwarf_cmd (const char *args, int from_tty)
25410 {
25411 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25412 gdb_stdout);
25413 }
25414
25415 static void
25416 show_dwarf_cmd (const char *args, int from_tty)
25417 {
25418 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25419 }
25420
25421 bool dwarf_always_disassemble;
25422
25423 static void
25424 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25425 struct cmd_list_element *c, const char *value)
25426 {
25427 fprintf_filtered (file,
25428 _("Whether to always disassemble "
25429 "DWARF expressions is %s.\n"),
25430 value);
25431 }
25432
25433 static void
25434 show_check_physname (struct ui_file *file, int from_tty,
25435 struct cmd_list_element *c, const char *value)
25436 {
25437 fprintf_filtered (file,
25438 _("Whether to check \"physname\" is %s.\n"),
25439 value);
25440 }
25441
25442 void _initialize_dwarf2_read ();
25443 void
25444 _initialize_dwarf2_read ()
25445 {
25446 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25447 Set DWARF specific variables.\n\
25448 Configure DWARF variables such as the cache size."),
25449 &set_dwarf_cmdlist, "maintenance set dwarf ",
25450 0/*allow-unknown*/, &maintenance_set_cmdlist);
25451
25452 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25453 Show DWARF specific variables.\n\
25454 Show DWARF variables such as the cache size."),
25455 &show_dwarf_cmdlist, "maintenance show dwarf ",
25456 0/*allow-unknown*/, &maintenance_show_cmdlist);
25457
25458 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25459 &dwarf_max_cache_age, _("\
25460 Set the upper bound on the age of cached DWARF compilation units."), _("\
25461 Show the upper bound on the age of cached DWARF compilation units."), _("\
25462 A higher limit means that cached compilation units will be stored\n\
25463 in memory longer, and more total memory will be used. Zero disables\n\
25464 caching, which can slow down startup."),
25465 NULL,
25466 show_dwarf_max_cache_age,
25467 &set_dwarf_cmdlist,
25468 &show_dwarf_cmdlist);
25469
25470 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25471 &dwarf_always_disassemble, _("\
25472 Set whether `info address' always disassembles DWARF expressions."), _("\
25473 Show whether `info address' always disassembles DWARF expressions."), _("\
25474 When enabled, DWARF expressions are always printed in an assembly-like\n\
25475 syntax. When disabled, expressions will be printed in a more\n\
25476 conversational style, when possible."),
25477 NULL,
25478 show_dwarf_always_disassemble,
25479 &set_dwarf_cmdlist,
25480 &show_dwarf_cmdlist);
25481
25482 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25483 Set debugging of the DWARF reader."), _("\
25484 Show debugging of the DWARF reader."), _("\
25485 When enabled (non-zero), debugging messages are printed during DWARF\n\
25486 reading and symtab expansion. A value of 1 (one) provides basic\n\
25487 information. A value greater than 1 provides more verbose information."),
25488 NULL,
25489 NULL,
25490 &setdebuglist, &showdebuglist);
25491
25492 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25493 Set debugging of the DWARF DIE reader."), _("\
25494 Show debugging of the DWARF DIE reader."), _("\
25495 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25496 The value is the maximum depth to print."),
25497 NULL,
25498 NULL,
25499 &setdebuglist, &showdebuglist);
25500
25501 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25502 Set debugging of the dwarf line reader."), _("\
25503 Show debugging of the dwarf line reader."), _("\
25504 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25505 A value of 1 (one) provides basic information.\n\
25506 A value greater than 1 provides more verbose information."),
25507 NULL,
25508 NULL,
25509 &setdebuglist, &showdebuglist);
25510
25511 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25512 Set cross-checking of \"physname\" code against demangler."), _("\
25513 Show cross-checking of \"physname\" code against demangler."), _("\
25514 When enabled, GDB's internal \"physname\" code is checked against\n\
25515 the demangler."),
25516 NULL, show_check_physname,
25517 &setdebuglist, &showdebuglist);
25518
25519 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25520 no_class, &use_deprecated_index_sections, _("\
25521 Set whether to use deprecated gdb_index sections."), _("\
25522 Show whether to use deprecated gdb_index sections."), _("\
25523 When enabled, deprecated .gdb_index sections are used anyway.\n\
25524 Normally they are ignored either because of a missing feature or\n\
25525 performance issue.\n\
25526 Warning: This option must be enabled before gdb reads the file."),
25527 NULL,
25528 NULL,
25529 &setlist, &showlist);
25530
25531 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25532 &dwarf2_locexpr_funcs);
25533 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25534 &dwarf2_loclist_funcs);
25535
25536 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25537 &dwarf2_block_frame_base_locexpr_funcs);
25538 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25539 &dwarf2_block_frame_base_loclist_funcs);
25540
25541 #if GDB_SELF_TEST
25542 selftests::register_test ("dw2_expand_symtabs_matching",
25543 selftests::dw2_expand_symtabs_matching::run_test);
25544 #endif
25545 }
This page took 0.598575 seconds and 4 git commands to generate.