Change line_table methods to return unique_xmalloc_ptr
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/index-cache.h"
36 #include "dwarf2/index-common.h"
37 #include "dwarf2/leb.h"
38 #include "bfd.h"
39 #include "elf-bfd.h"
40 #include "symtab.h"
41 #include "gdbtypes.h"
42 #include "objfiles.h"
43 #include "dwarf2.h"
44 #include "buildsym.h"
45 #include "demangle.h"
46 #include "gdb-demangle.h"
47 #include "filenames.h" /* for DOSish file names */
48 #include "macrotab.h"
49 #include "language.h"
50 #include "complaints.h"
51 #include "dwarf2/expr.h"
52 #include "dwarf2/loc.h"
53 #include "cp-support.h"
54 #include "hashtab.h"
55 #include "command.h"
56 #include "gdbcmd.h"
57 #include "block.h"
58 #include "addrmap.h"
59 #include "typeprint.h"
60 #include "psympriv.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "build-id.h"
70 #include "namespace.h"
71 #include "gdbsupport/function-view.h"
72 #include "gdbsupport/gdb_optional.h"
73 #include "gdbsupport/underlying.h"
74 #include "gdbsupport/hash_enum.h"
75 #include "filename-seen-cache.h"
76 #include "producer.h"
77 #include <fcntl.h>
78 #include <algorithm>
79 #include <unordered_map>
80 #include "gdbsupport/selftest.h"
81 #include "rust-lang.h"
82 #include "gdbsupport/pathstuff.h"
83
84 /* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87 static unsigned int dwarf_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static unsigned int dwarf_die_debug = 0;
91
92 /* When non-zero, dump line number entries as they are read in. */
93 static unsigned int dwarf_line_debug = 0;
94
95 /* When true, cross-check physname against demangler. */
96 static bool check_physname = false;
97
98 /* When true, do not reject deprecated .gdb_index sections. */
99 static bool use_deprecated_index_sections = false;
100
101 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
102
103 /* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105 static int dwarf2_locexpr_index;
106 static int dwarf2_loclist_index;
107 static int dwarf2_locexpr_block_index;
108 static int dwarf2_loclist_block_index;
109
110 /* An index into a (C++) symbol name component in a symbol name as
111 recorded in the mapped_index's symbol table. For each C++ symbol
112 in the symbol table, we record one entry for the start of each
113 component in the symbol in a table of name components, and then
114 sort the table, in order to be able to binary search symbol names,
115 ignoring leading namespaces, both completion and regular look up.
116 For example, for symbol "A::B::C", we'll have an entry that points
117 to "A::B::C", another that points to "B::C", and another for "C".
118 Note that function symbols in GDB index have no parameter
119 information, just the function/method names. You can convert a
120 name_component to a "const char *" using the
121 'mapped_index::symbol_name_at(offset_type)' method. */
122
123 struct name_component
124 {
125 /* Offset in the symbol name where the component starts. Stored as
126 a (32-bit) offset instead of a pointer to save memory and improve
127 locality on 64-bit architectures. */
128 offset_type name_offset;
129
130 /* The symbol's index in the symbol and constant pool tables of a
131 mapped_index. */
132 offset_type idx;
133 };
134
135 /* Base class containing bits shared by both .gdb_index and
136 .debug_name indexes. */
137
138 struct mapped_index_base
139 {
140 mapped_index_base () = default;
141 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
142
143 /* The name_component table (a sorted vector). See name_component's
144 description above. */
145 std::vector<name_component> name_components;
146
147 /* How NAME_COMPONENTS is sorted. */
148 enum case_sensitivity name_components_casing;
149
150 /* Return the number of names in the symbol table. */
151 virtual size_t symbol_name_count () const = 0;
152
153 /* Get the name of the symbol at IDX in the symbol table. */
154 virtual const char *symbol_name_at (offset_type idx) const = 0;
155
156 /* Return whether the name at IDX in the symbol table should be
157 ignored. */
158 virtual bool symbol_name_slot_invalid (offset_type idx) const
159 {
160 return false;
161 }
162
163 /* Build the symbol name component sorted vector, if we haven't
164 yet. */
165 void build_name_components ();
166
167 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
168 possible matches for LN_NO_PARAMS in the name component
169 vector. */
170 std::pair<std::vector<name_component>::const_iterator,
171 std::vector<name_component>::const_iterator>
172 find_name_components_bounds (const lookup_name_info &ln_no_params,
173 enum language lang) const;
174
175 /* Prevent deleting/destroying via a base class pointer. */
176 protected:
177 ~mapped_index_base() = default;
178 };
179
180 /* A description of the mapped index. The file format is described in
181 a comment by the code that writes the index. */
182 struct mapped_index final : public mapped_index_base
183 {
184 /* A slot/bucket in the symbol table hash. */
185 struct symbol_table_slot
186 {
187 const offset_type name;
188 const offset_type vec;
189 };
190
191 /* Index data format version. */
192 int version = 0;
193
194 /* The address table data. */
195 gdb::array_view<const gdb_byte> address_table;
196
197 /* The symbol table, implemented as a hash table. */
198 gdb::array_view<symbol_table_slot> symbol_table;
199
200 /* A pointer to the constant pool. */
201 const char *constant_pool = nullptr;
202
203 bool symbol_name_slot_invalid (offset_type idx) const override
204 {
205 const auto &bucket = this->symbol_table[idx];
206 return bucket.name == 0 && bucket.vec == 0;
207 }
208
209 /* Convenience method to get at the name of the symbol at IDX in the
210 symbol table. */
211 const char *symbol_name_at (offset_type idx) const override
212 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
213
214 size_t symbol_name_count () const override
215 { return this->symbol_table.size (); }
216 };
217
218 /* A description of the mapped .debug_names.
219 Uninitialized map has CU_COUNT 0. */
220 struct mapped_debug_names final : public mapped_index_base
221 {
222 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
223 : dwarf2_per_objfile (dwarf2_per_objfile_)
224 {}
225
226 struct dwarf2_per_objfile *dwarf2_per_objfile;
227 bfd_endian dwarf5_byte_order;
228 bool dwarf5_is_dwarf64;
229 bool augmentation_is_gdb;
230 uint8_t offset_size;
231 uint32_t cu_count = 0;
232 uint32_t tu_count, bucket_count, name_count;
233 const gdb_byte *cu_table_reordered, *tu_table_reordered;
234 const uint32_t *bucket_table_reordered, *hash_table_reordered;
235 const gdb_byte *name_table_string_offs_reordered;
236 const gdb_byte *name_table_entry_offs_reordered;
237 const gdb_byte *entry_pool;
238
239 struct index_val
240 {
241 ULONGEST dwarf_tag;
242 struct attr
243 {
244 /* Attribute name DW_IDX_*. */
245 ULONGEST dw_idx;
246
247 /* Attribute form DW_FORM_*. */
248 ULONGEST form;
249
250 /* Value if FORM is DW_FORM_implicit_const. */
251 LONGEST implicit_const;
252 };
253 std::vector<attr> attr_vec;
254 };
255
256 std::unordered_map<ULONGEST, index_val> abbrev_map;
257
258 const char *namei_to_name (uint32_t namei) const;
259
260 /* Implementation of the mapped_index_base virtual interface, for
261 the name_components cache. */
262
263 const char *symbol_name_at (offset_type idx) const override
264 { return namei_to_name (idx); }
265
266 size_t symbol_name_count () const override
267 { return this->name_count; }
268 };
269
270 /* See dwarf2read.h. */
271
272 dwarf2_per_objfile *
273 get_dwarf2_per_objfile (struct objfile *objfile)
274 {
275 return dwarf2_objfile_data_key.get (objfile);
276 }
277
278 /* Default names of the debugging sections. */
279
280 /* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
283 static const struct dwarf2_debug_sections dwarf2_elf_names =
284 {
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_loclists", ".zdebug_loclists" },
290 { ".debug_macinfo", ".zdebug_macinfo" },
291 { ".debug_macro", ".zdebug_macro" },
292 { ".debug_str", ".zdebug_str" },
293 { ".debug_str_offsets", ".zdebug_str_offsets" },
294 { ".debug_line_str", ".zdebug_line_str" },
295 { ".debug_ranges", ".zdebug_ranges" },
296 { ".debug_rnglists", ".zdebug_rnglists" },
297 { ".debug_types", ".zdebug_types" },
298 { ".debug_addr", ".zdebug_addr" },
299 { ".debug_frame", ".zdebug_frame" },
300 { ".eh_frame", NULL },
301 { ".gdb_index", ".zgdb_index" },
302 { ".debug_names", ".zdebug_names" },
303 { ".debug_aranges", ".zdebug_aranges" },
304 23
305 };
306
307 /* List of DWO/DWP sections. */
308
309 static const struct dwop_section_names
310 {
311 struct dwarf2_section_names abbrev_dwo;
312 struct dwarf2_section_names info_dwo;
313 struct dwarf2_section_names line_dwo;
314 struct dwarf2_section_names loc_dwo;
315 struct dwarf2_section_names loclists_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
338 };
339
340 /* local data types */
341
342 /* The data in a compilation unit header, after target2host
343 translation, looks like this. */
344 struct comp_unit_head
345 {
346 unsigned int length;
347 short version;
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
350 sect_offset abbrev_sect_off;
351
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
354
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
357
358 enum dwarf_unit_type unit_type;
359
360 /* Offset to the first byte of this compilation unit header in the
361 .debug_info section, for resolving relative reference dies. */
362 sect_offset sect_off;
363
364 /* Offset to first die in this cu from the start of the cu.
365 This will be the first byte following the compilation unit header. */
366 cu_offset first_die_cu_offset;
367
368
369 /* 64-bit signature of this unit. For type units, it denotes the signature of
370 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
371 Also used in DWARF 5, to denote the dwo id when the unit type is
372 DW_UT_skeleton or DW_UT_split_compile. */
373 ULONGEST signature;
374
375 /* For types, offset in the type's DIE of the type defined by this TU. */
376 cu_offset type_cu_offset_in_tu;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
428
429 /* Non-zero if base_address has been set. */
430 int base_known = 0;
431
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
435
436 const char *producer = nullptr;
437
438 private:
439 /* The symtab builder for this CU. This is only non-NULL when full
440 symbols are being read. */
441 std::unique_ptr<buildsym_compunit> m_builder;
442
443 public:
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
453 struct pending **list_in_scope = nullptr;
454
455 /* Hash table holding all the loaded partial DIEs
456 with partial_die->offset.SECT_OFF as hash. */
457 htab_t partial_dies = nullptr;
458
459 /* Storage for things with the same lifetime as this read-in compilation
460 unit, including partial DIEs. */
461 auto_obstack comp_unit_obstack;
462
463 /* When multiple dwarf2_cu structures are living in memory, this field
464 chains them all together, so that they can be released efficiently.
465 We will probably also want a generation counter so that most-recently-used
466 compilation units are cached... */
467 struct dwarf2_per_cu_data *read_in_chain = nullptr;
468
469 /* Backlink to our per_cu entry. */
470 struct dwarf2_per_cu_data *per_cu;
471
472 /* How many compilation units ago was this CU last referenced? */
473 int last_used = 0;
474
475 /* A hash table of DIE cu_offset for following references with
476 die_info->offset.sect_off as hash. */
477 htab_t die_hash = nullptr;
478
479 /* Full DIEs if read in. */
480 struct die_info *dies = nullptr;
481
482 /* A set of pointers to dwarf2_per_cu_data objects for compilation
483 units referenced by this one. Only set during full symbol processing;
484 partial symbol tables do not have dependencies. */
485 htab_t dependencies = nullptr;
486
487 /* Header data from the line table, during full symbol processing. */
488 struct line_header *line_header = nullptr;
489 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
490 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
491 this is the DW_TAG_compile_unit die for this CU. We'll hold on
492 to the line header as long as this DIE is being processed. See
493 process_die_scope. */
494 die_info *line_header_die_owner = nullptr;
495
496 /* A list of methods which need to have physnames computed
497 after all type information has been read. */
498 std::vector<delayed_method_info> method_list;
499
500 /* To be copied to symtab->call_site_htab. */
501 htab_t call_site_htab = nullptr;
502
503 /* Non-NULL if this CU came from a DWO file.
504 There is an invariant here that is important to remember:
505 Except for attributes copied from the top level DIE in the "main"
506 (or "stub") file in preparation for reading the DWO file
507 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
508 Either there isn't a DWO file (in which case this is NULL and the point
509 is moot), or there is and either we're not going to read it (in which
510 case this is NULL) or there is and we are reading it (in which case this
511 is non-NULL). */
512 struct dwo_unit *dwo_unit = nullptr;
513
514 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
515 Note this value comes from the Fission stub CU/TU's DIE. */
516 gdb::optional<ULONGEST> addr_base;
517
518 /* The DW_AT_rnglists_base attribute if present.
519 Note this value comes from the Fission stub CU/TU's DIE.
520 Also note that the value is zero in the non-DWO case so this value can
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
527 ULONGEST ranges_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab.
611 This PER_CU is recognized by having no section. */
612 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
613 struct dwarf2_per_cu_data per_cu;
614
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
618 std::vector<signatured_type *> *tus;
619
620 /* The compunit symtab.
621 Type units in a group needn't all be defined in the same source file,
622 so we create an essentially anonymous symtab as the compunit symtab. */
623 struct compunit_symtab *compunit_symtab;
624
625 /* The data used to construct the hash key. */
626 struct stmt_list_hash hash;
627
628 /* The number of symtabs from the line header.
629 The value here must match line_header.num_file_names. */
630 unsigned int num_symtabs;
631
632 /* The symbol tables for this TU (obtained from the files listed in
633 DW_AT_stmt_list).
634 WARNING: The order of entries here must match the order of entries
635 in the line header. After the first TU using this type_unit_group, the
636 line header for the subsequent TUs is recreated from this. This is done
637 because we need to use the same symtabs for each TU using the same
638 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
639 there's no guarantee the line header doesn't have duplicate entries. */
640 struct symtab **symtabs;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_up cus;
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_up tus;
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_up loaded_cus;
872 htab_up loaded_tus;
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (struct dwarf2_per_cu_data *this_cu,
918 struct abbrev_table *abbrev_table,
919 int use_existing_cu, int keep,
920 bool skip_partial);
921
922 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 ~cutu_reader ();
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 bool dummy_p = false;
933
934 private:
935 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
936 int use_existing_cu, int keep);
937
938 struct dwarf2_per_cu_data *m_this_cu;
939 int m_keep = 0;
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947 };
948
949 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
950 later. */
951 typedef int dir_index;
952
953 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
954 and later. */
955 typedef int file_name_index;
956
957 struct file_entry
958 {
959 file_entry () = default;
960
961 file_entry (const char *name_, dir_index d_index_,
962 unsigned int mod_time_, unsigned int length_)
963 : name (name_),
964 d_index (d_index_),
965 mod_time (mod_time_),
966 length (length_)
967 {}
968
969 /* Return the include directory at D_INDEX stored in LH. Returns
970 NULL if D_INDEX is out of bounds. */
971 const char *include_dir (const line_header *lh) const;
972
973 /* The file name. Note this is an observing pointer. The memory is
974 owned by debug_line_buffer. */
975 const char *name {};
976
977 /* The directory index (1-based). */
978 dir_index d_index {};
979
980 unsigned int mod_time {};
981
982 unsigned int length {};
983
984 /* True if referenced by the Line Number Program. */
985 bool included_p {};
986
987 /* The associated symbol table, if any. */
988 struct symtab *symtab {};
989 };
990
991 /* The line number information for a compilation unit (found in the
992 .debug_line section) begins with a "statement program header",
993 which contains the following information. */
994 struct line_header
995 {
996 line_header ()
997 : offset_in_dwz {}
998 {}
999
1000 /* Add an entry to the include directory table. */
1001 void add_include_dir (const char *include_dir);
1002
1003 /* Add an entry to the file name table. */
1004 void add_file_name (const char *name, dir_index d_index,
1005 unsigned int mod_time, unsigned int length);
1006
1007 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1008 Returns NULL if INDEX is out of bounds. */
1009 const char *include_dir_at (dir_index index) const
1010 {
1011 int vec_index;
1012 if (version >= 5)
1013 vec_index = index;
1014 else
1015 vec_index = index - 1;
1016 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1017 return NULL;
1018 return m_include_dirs[vec_index];
1019 }
1020
1021 bool is_valid_file_index (int file_index)
1022 {
1023 if (version >= 5)
1024 return 0 <= file_index && file_index < file_names_size ();
1025 return 1 <= file_index && file_index <= file_names_size ();
1026 }
1027
1028 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1029 Returns NULL if INDEX is out of bounds. */
1030 file_entry *file_name_at (file_name_index index)
1031 {
1032 int vec_index;
1033 if (version >= 5)
1034 vec_index = index;
1035 else
1036 vec_index = index - 1;
1037 if (vec_index < 0 || vec_index >= m_file_names.size ())
1038 return NULL;
1039 return &m_file_names[vec_index];
1040 }
1041
1042 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1043 this method should only be used to iterate through all file entries in an
1044 index-agnostic manner. */
1045 std::vector<file_entry> &file_names ()
1046 { return m_file_names; }
1047
1048 /* Offset of line number information in .debug_line section. */
1049 sect_offset sect_off {};
1050
1051 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1052 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1053
1054 unsigned int total_length {};
1055 unsigned short version {};
1056 unsigned int header_length {};
1057 unsigned char minimum_instruction_length {};
1058 unsigned char maximum_ops_per_instruction {};
1059 unsigned char default_is_stmt {};
1060 int line_base {};
1061 unsigned char line_range {};
1062 unsigned char opcode_base {};
1063
1064 /* standard_opcode_lengths[i] is the number of operands for the
1065 standard opcode whose value is i. This means that
1066 standard_opcode_lengths[0] is unused, and the last meaningful
1067 element is standard_opcode_lengths[opcode_base - 1]. */
1068 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1069
1070 int file_names_size ()
1071 { return m_file_names.size(); }
1072
1073 /* The start and end of the statement program following this
1074 header. These point into dwarf2_per_objfile->line_buffer. */
1075 const gdb_byte *statement_program_start {}, *statement_program_end {};
1076
1077 /* Return the full name of file number I in this object's file name
1078 table. Use COMP_DIR as the name of the current directory of the
1079 compilation. The result is allocated using xmalloc; the caller
1080 is responsible for freeing it. */
1081 gdb::unique_xmalloc_ptr<char> file_full_name (int file,
1082 const char *comp_dir);
1083
1084 /* Return file name relative to the compilation directory of file
1085 number I in this object's file name table. The result is
1086 allocated using xmalloc; the caller is responsible for freeing
1087 it. */
1088 gdb::unique_xmalloc_ptr<char> file_file_name (int file);
1089
1090 private:
1091 /* The include_directories table. Note these are observing
1092 pointers. The memory is owned by debug_line_buffer. */
1093 std::vector<const char *> m_include_dirs;
1094
1095 /* The file_names table. This is private because the meaning of indexes
1096 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1097 before, and is 0 in DWARF 5 and later). So the client should use
1098 file_name_at method for access. */
1099 std::vector<file_entry> m_file_names;
1100 };
1101
1102 typedef std::unique_ptr<line_header> line_header_up;
1103
1104 const char *
1105 file_entry::include_dir (const line_header *lh) const
1106 {
1107 return lh->include_dir_at (d_index);
1108 }
1109
1110 /* When we construct a partial symbol table entry we only
1111 need this much information. */
1112 struct partial_die_info : public allocate_on_obstack
1113 {
1114 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1115
1116 /* Disable assign but still keep copy ctor, which is needed
1117 load_partial_dies. */
1118 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1119
1120 /* Adjust the partial die before generating a symbol for it. This
1121 function may set the is_external flag or change the DIE's
1122 name. */
1123 void fixup (struct dwarf2_cu *cu);
1124
1125 /* Read a minimal amount of information into the minimal die
1126 structure. */
1127 const gdb_byte *read (const struct die_reader_specs *reader,
1128 const struct abbrev_info &abbrev,
1129 const gdb_byte *info_ptr);
1130
1131 /* Offset of this DIE. */
1132 const sect_offset sect_off;
1133
1134 /* DWARF-2 tag for this DIE. */
1135 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1136
1137 /* Assorted flags describing the data found in this DIE. */
1138 const unsigned int has_children : 1;
1139
1140 unsigned int is_external : 1;
1141 unsigned int is_declaration : 1;
1142 unsigned int has_type : 1;
1143 unsigned int has_specification : 1;
1144 unsigned int has_pc_info : 1;
1145 unsigned int may_be_inlined : 1;
1146
1147 /* This DIE has been marked DW_AT_main_subprogram. */
1148 unsigned int main_subprogram : 1;
1149
1150 /* Flag set if the SCOPE field of this structure has been
1151 computed. */
1152 unsigned int scope_set : 1;
1153
1154 /* Flag set if the DIE has a byte_size attribute. */
1155 unsigned int has_byte_size : 1;
1156
1157 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1158 unsigned int has_const_value : 1;
1159
1160 /* Flag set if any of the DIE's children are template arguments. */
1161 unsigned int has_template_arguments : 1;
1162
1163 /* Flag set if fixup has been called on this die. */
1164 unsigned int fixup_called : 1;
1165
1166 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1167 unsigned int is_dwz : 1;
1168
1169 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1170 unsigned int spec_is_dwz : 1;
1171
1172 /* The name of this DIE. Normally the value of DW_AT_name, but
1173 sometimes a default name for unnamed DIEs. */
1174 const char *name = nullptr;
1175
1176 /* The linkage name, if present. */
1177 const char *linkage_name = nullptr;
1178
1179 /* The scope to prepend to our children. This is generally
1180 allocated on the comp_unit_obstack, so will disappear
1181 when this compilation unit leaves the cache. */
1182 const char *scope = nullptr;
1183
1184 /* Some data associated with the partial DIE. The tag determines
1185 which field is live. */
1186 union
1187 {
1188 /* The location description associated with this DIE, if any. */
1189 struct dwarf_block *locdesc;
1190 /* The offset of an import, for DW_TAG_imported_unit. */
1191 sect_offset sect_off;
1192 } d {};
1193
1194 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1195 CORE_ADDR lowpc = 0;
1196 CORE_ADDR highpc = 0;
1197
1198 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1199 DW_AT_sibling, if any. */
1200 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1201 could return DW_AT_sibling values to its caller load_partial_dies. */
1202 const gdb_byte *sibling = nullptr;
1203
1204 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1205 DW_AT_specification (or DW_AT_abstract_origin or
1206 DW_AT_extension). */
1207 sect_offset spec_offset {};
1208
1209 /* Pointers to this DIE's parent, first child, and next sibling,
1210 if any. */
1211 struct partial_die_info *die_parent = nullptr;
1212 struct partial_die_info *die_child = nullptr;
1213 struct partial_die_info *die_sibling = nullptr;
1214
1215 friend struct partial_die_info *
1216 dwarf2_cu::find_partial_die (sect_offset sect_off);
1217
1218 private:
1219 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1220 partial_die_info (sect_offset sect_off)
1221 : partial_die_info (sect_off, DW_TAG_padding, 0)
1222 {
1223 }
1224
1225 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1226 int has_children_)
1227 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1228 {
1229 is_external = 0;
1230 is_declaration = 0;
1231 has_type = 0;
1232 has_specification = 0;
1233 has_pc_info = 0;
1234 may_be_inlined = 0;
1235 main_subprogram = 0;
1236 scope_set = 0;
1237 has_byte_size = 0;
1238 has_const_value = 0;
1239 has_template_arguments = 0;
1240 fixup_called = 0;
1241 is_dwz = 0;
1242 spec_is_dwz = 0;
1243 }
1244 };
1245
1246 /* This data structure holds a complete die structure. */
1247 struct die_info
1248 {
1249 /* DWARF-2 tag for this DIE. */
1250 ENUM_BITFIELD(dwarf_tag) tag : 16;
1251
1252 /* Number of attributes */
1253 unsigned char num_attrs;
1254
1255 /* True if we're presently building the full type name for the
1256 type derived from this DIE. */
1257 unsigned char building_fullname : 1;
1258
1259 /* True if this die is in process. PR 16581. */
1260 unsigned char in_process : 1;
1261
1262 /* True if this DIE has children. */
1263 unsigned char has_children : 1;
1264
1265 /* Abbrev number */
1266 unsigned int abbrev;
1267
1268 /* Offset in .debug_info or .debug_types section. */
1269 sect_offset sect_off;
1270
1271 /* The dies in a compilation unit form an n-ary tree. PARENT
1272 points to this die's parent; CHILD points to the first child of
1273 this node; and all the children of a given node are chained
1274 together via their SIBLING fields. */
1275 struct die_info *child; /* Its first child, if any. */
1276 struct die_info *sibling; /* Its next sibling, if any. */
1277 struct die_info *parent; /* Its parent, if any. */
1278
1279 /* An array of attributes, with NUM_ATTRS elements. There may be
1280 zero, but it's not common and zero-sized arrays are not
1281 sufficiently portable C. */
1282 struct attribute attrs[1];
1283 };
1284
1285 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1286 but this would require a corresponding change in unpack_field_as_long
1287 and friends. */
1288 static int bits_per_byte = 8;
1289
1290 /* When reading a variant or variant part, we track a bit more
1291 information about the field, and store it in an object of this
1292 type. */
1293
1294 struct variant_field
1295 {
1296 /* If we see a DW_TAG_variant, then this will be the discriminant
1297 value. */
1298 ULONGEST discriminant_value;
1299 /* If we see a DW_TAG_variant, then this will be set if this is the
1300 default branch. */
1301 bool default_branch;
1302 /* While reading a DW_TAG_variant_part, this will be set if this
1303 field is the discriminant. */
1304 bool is_discriminant;
1305 };
1306
1307 struct nextfield
1308 {
1309 int accessibility = 0;
1310 int virtuality = 0;
1311 /* Extra information to describe a variant or variant part. */
1312 struct variant_field variant {};
1313 struct field field {};
1314 };
1315
1316 struct fnfieldlist
1317 {
1318 const char *name = nullptr;
1319 std::vector<struct fn_field> fnfields;
1320 };
1321
1322 /* The routines that read and process dies for a C struct or C++ class
1323 pass lists of data member fields and lists of member function fields
1324 in an instance of a field_info structure, as defined below. */
1325 struct field_info
1326 {
1327 /* List of data member and baseclasses fields. */
1328 std::vector<struct nextfield> fields;
1329 std::vector<struct nextfield> baseclasses;
1330
1331 /* Number of fields (including baseclasses). */
1332 int nfields = 0;
1333
1334 /* Set if the accessibility of one of the fields is not public. */
1335 int non_public_fields = 0;
1336
1337 /* Member function fieldlist array, contains name of possibly overloaded
1338 member function, number of overloaded member functions and a pointer
1339 to the head of the member function field chain. */
1340 std::vector<struct fnfieldlist> fnfieldlists;
1341
1342 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1343 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1344 std::vector<struct decl_field> typedef_field_list;
1345
1346 /* Nested types defined by this class and the number of elements in this
1347 list. */
1348 std::vector<struct decl_field> nested_types_list;
1349 };
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
1378 static void dwarf2_build_psymtabs_hard
1379 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1380
1381 static void scan_partial_symbols (struct partial_die_info *,
1382 CORE_ADDR *, CORE_ADDR *,
1383 int, struct dwarf2_cu *);
1384
1385 static void add_partial_symbol (struct partial_die_info *,
1386 struct dwarf2_cu *);
1387
1388 static void add_partial_namespace (struct partial_die_info *pdi,
1389 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1390 int set_addrmap, struct dwarf2_cu *cu);
1391
1392 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1393 CORE_ADDR *highpc, int set_addrmap,
1394 struct dwarf2_cu *cu);
1395
1396 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1397 struct dwarf2_cu *cu);
1398
1399 static void add_partial_subprogram (struct partial_die_info *pdi,
1400 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1401 int need_pc, struct dwarf2_cu *cu);
1402
1403 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1404
1405 static struct partial_die_info *load_partial_dies
1406 (const struct die_reader_specs *, const gdb_byte *, int);
1407
1408 /* A pair of partial_die_info and compilation unit. */
1409 struct cu_partial_die_info
1410 {
1411 /* The compilation unit of the partial_die_info. */
1412 struct dwarf2_cu *cu;
1413 /* A partial_die_info. */
1414 struct partial_die_info *pdi;
1415
1416 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1417 : cu (cu),
1418 pdi (pdi)
1419 { /* Nothing. */ }
1420
1421 private:
1422 cu_partial_die_info () = delete;
1423 };
1424
1425 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1426 struct dwarf2_cu *);
1427
1428 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1429 struct attribute *, struct attr_abbrev *,
1430 const gdb_byte *, bool *need_reprocess);
1431
1432 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1433 struct attribute *attr);
1434
1435 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1436
1437 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1438 unsigned int *);
1439
1440 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1441
1442 static LONGEST read_checked_initial_length_and_offset
1443 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1444 unsigned int *, unsigned int *);
1445
1446 static LONGEST read_offset (bfd *, const gdb_byte *,
1447 const struct comp_unit_head *,
1448 unsigned int *);
1449
1450 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1451
1452 static sect_offset read_abbrev_offset
1453 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1454 struct dwarf2_section_info *, sect_offset);
1455
1456 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1457
1458 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1459
1460 static const char *read_indirect_string
1461 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1462 const struct comp_unit_head *, unsigned int *);
1463
1464 static const char *read_indirect_line_string
1465 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1466 const struct comp_unit_head *, unsigned int *);
1467
1468 static const char *read_indirect_string_at_offset
1469 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1470 LONGEST str_offset);
1471
1472 static const char *read_indirect_string_from_dwz
1473 (struct objfile *objfile, struct dwz_file *, LONGEST);
1474
1475 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1476 const gdb_byte *,
1477 unsigned int *);
1478
1479 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1480 ULONGEST str_index);
1481
1482 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1483 ULONGEST str_index);
1484
1485 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1486
1487 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1488 struct dwarf2_cu *);
1489
1490 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1491 unsigned int);
1492
1493 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1494 struct dwarf2_cu *cu);
1495
1496 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1497
1498 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1499 struct dwarf2_cu *cu);
1500
1501 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1502
1503 static struct die_info *die_specification (struct die_info *die,
1504 struct dwarf2_cu **);
1505
1506 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1507 struct dwarf2_cu *cu);
1508
1509 static void dwarf_decode_lines (struct line_header *, const char *,
1510 struct dwarf2_cu *, dwarf2_psymtab *,
1511 CORE_ADDR, int decode_mapping);
1512
1513 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1514 const char *);
1515
1516 static struct symbol *new_symbol (struct die_info *, struct type *,
1517 struct dwarf2_cu *, struct symbol * = NULL);
1518
1519 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1520 struct dwarf2_cu *);
1521
1522 static void dwarf2_const_value_attr (const struct attribute *attr,
1523 struct type *type,
1524 const char *name,
1525 struct obstack *obstack,
1526 struct dwarf2_cu *cu, LONGEST *value,
1527 const gdb_byte **bytes,
1528 struct dwarf2_locexpr_baton **baton);
1529
1530 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1531
1532 static int need_gnat_info (struct dwarf2_cu *);
1533
1534 static struct type *die_descriptive_type (struct die_info *,
1535 struct dwarf2_cu *);
1536
1537 static void set_descriptive_type (struct type *, struct die_info *,
1538 struct dwarf2_cu *);
1539
1540 static struct type *die_containing_type (struct die_info *,
1541 struct dwarf2_cu *);
1542
1543 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1544 struct dwarf2_cu *);
1545
1546 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1547
1548 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1549
1550 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1551
1552 static char *typename_concat (struct obstack *obs, const char *prefix,
1553 const char *suffix, int physname,
1554 struct dwarf2_cu *cu);
1555
1556 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1557
1558 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1559
1560 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1561
1562 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1563
1564 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1565
1566 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1567
1568 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1569 struct dwarf2_cu *, dwarf2_psymtab *);
1570
1571 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1572 values. Keep the items ordered with increasing constraints compliance. */
1573 enum pc_bounds_kind
1574 {
1575 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1576 PC_BOUNDS_NOT_PRESENT,
1577
1578 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1579 were present but they do not form a valid range of PC addresses. */
1580 PC_BOUNDS_INVALID,
1581
1582 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1583 PC_BOUNDS_RANGES,
1584
1585 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1586 PC_BOUNDS_HIGH_LOW,
1587 };
1588
1589 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1590 CORE_ADDR *, CORE_ADDR *,
1591 struct dwarf2_cu *,
1592 dwarf2_psymtab *);
1593
1594 static void get_scope_pc_bounds (struct die_info *,
1595 CORE_ADDR *, CORE_ADDR *,
1596 struct dwarf2_cu *);
1597
1598 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1599 CORE_ADDR, struct dwarf2_cu *);
1600
1601 static void dwarf2_add_field (struct field_info *, struct die_info *,
1602 struct dwarf2_cu *);
1603
1604 static void dwarf2_attach_fields_to_type (struct field_info *,
1605 struct type *, struct dwarf2_cu *);
1606
1607 static void dwarf2_add_member_fn (struct field_info *,
1608 struct die_info *, struct type *,
1609 struct dwarf2_cu *);
1610
1611 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1612 struct type *,
1613 struct dwarf2_cu *);
1614
1615 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1616
1617 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1618
1619 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1620
1621 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1622
1623 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1624
1625 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1626
1627 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1628
1629 static struct type *read_module_type (struct die_info *die,
1630 struct dwarf2_cu *cu);
1631
1632 static const char *namespace_name (struct die_info *die,
1633 int *is_anonymous, struct dwarf2_cu *);
1634
1635 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1636
1637 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1638
1639 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1640 struct dwarf2_cu *);
1641
1642 static struct die_info *read_die_and_siblings_1
1643 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1644 struct die_info *);
1645
1646 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1647 const gdb_byte *info_ptr,
1648 const gdb_byte **new_info_ptr,
1649 struct die_info *parent);
1650
1651 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1652 struct die_info **, const gdb_byte *,
1653 int);
1654
1655 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1656 struct die_info **, const gdb_byte *);
1657
1658 static void process_die (struct die_info *, struct dwarf2_cu *);
1659
1660 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1661 struct obstack *);
1662
1663 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1664
1665 static const char *dwarf2_full_name (const char *name,
1666 struct die_info *die,
1667 struct dwarf2_cu *cu);
1668
1669 static const char *dwarf2_physname (const char *name, struct die_info *die,
1670 struct dwarf2_cu *cu);
1671
1672 static struct die_info *dwarf2_extension (struct die_info *die,
1673 struct dwarf2_cu **);
1674
1675 static const char *dwarf_tag_name (unsigned int);
1676
1677 static const char *dwarf_attr_name (unsigned int);
1678
1679 static const char *dwarf_unit_type_name (int unit_type);
1680
1681 static const char *dwarf_form_name (unsigned int);
1682
1683 static const char *dwarf_bool_name (unsigned int);
1684
1685 static const char *dwarf_type_encoding_name (unsigned int);
1686
1687 static struct die_info *sibling_die (struct die_info *);
1688
1689 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1690
1691 static void dump_die_for_error (struct die_info *);
1692
1693 static void dump_die_1 (struct ui_file *, int level, int max_level,
1694 struct die_info *);
1695
1696 /*static*/ void dump_die (struct die_info *, int max_level);
1697
1698 static void store_in_ref_table (struct die_info *,
1699 struct dwarf2_cu *);
1700
1701 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1702
1703 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1704
1705 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1706 const struct attribute *,
1707 struct dwarf2_cu **);
1708
1709 static struct die_info *follow_die_ref (struct die_info *,
1710 const struct attribute *,
1711 struct dwarf2_cu **);
1712
1713 static struct die_info *follow_die_sig (struct die_info *,
1714 const struct attribute *,
1715 struct dwarf2_cu **);
1716
1717 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1718 struct dwarf2_cu *);
1719
1720 static struct type *get_DW_AT_signature_type (struct die_info *,
1721 const struct attribute *,
1722 struct dwarf2_cu *);
1723
1724 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1725
1726 static void read_signatured_type (struct signatured_type *);
1727
1728 static int attr_to_dynamic_prop (const struct attribute *attr,
1729 struct die_info *die, struct dwarf2_cu *cu,
1730 struct dynamic_prop *prop, struct type *type);
1731
1732 /* memory allocation interface */
1733
1734 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1735
1736 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1737
1738 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1739
1740 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1741 struct dwarf2_loclist_baton *baton,
1742 const struct attribute *attr);
1743
1744 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1745 struct symbol *sym,
1746 struct dwarf2_cu *cu,
1747 int is_block);
1748
1749 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1750 const gdb_byte *info_ptr,
1751 struct abbrev_info *abbrev);
1752
1753 static hashval_t partial_die_hash (const void *item);
1754
1755 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1756
1757 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1758 (sect_offset sect_off, unsigned int offset_in_dwz,
1759 struct dwarf2_per_objfile *dwarf2_per_objfile);
1760
1761 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1762 struct die_info *comp_unit_die,
1763 enum language pretend_language);
1764
1765 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1766
1767 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1768
1769 static struct type *set_die_type (struct die_info *, struct type *,
1770 struct dwarf2_cu *);
1771
1772 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1773
1774 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1775
1776 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1777 enum language);
1778
1779 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1780 enum language);
1781
1782 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1783 enum language);
1784
1785 static void dwarf2_add_dependence (struct dwarf2_cu *,
1786 struct dwarf2_per_cu_data *);
1787
1788 static void dwarf2_mark (struct dwarf2_cu *);
1789
1790 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1791
1792 static struct type *get_die_type_at_offset (sect_offset,
1793 struct dwarf2_per_cu_data *);
1794
1795 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1796
1797 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1798 enum language pretend_language);
1799
1800 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1801
1802 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1803 static struct type *dwarf2_per_cu_addr_sized_int_type
1804 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1805 static struct type *dwarf2_per_cu_int_type
1806 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1807 bool unsigned_p);
1808
1809 /* Class, the destructor of which frees all allocated queue entries. This
1810 will only have work to do if an error was thrown while processing the
1811 dwarf. If no error was thrown then the queue entries should have all
1812 been processed, and freed, as we went along. */
1813
1814 class dwarf2_queue_guard
1815 {
1816 public:
1817 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1818 : m_per_objfile (per_objfile)
1819 {
1820 }
1821
1822 /* Free any entries remaining on the queue. There should only be
1823 entries left if we hit an error while processing the dwarf. */
1824 ~dwarf2_queue_guard ()
1825 {
1826 /* Ensure that no memory is allocated by the queue. */
1827 std::queue<dwarf2_queue_item> empty;
1828 std::swap (m_per_objfile->queue, empty);
1829 }
1830
1831 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1832
1833 private:
1834 dwarf2_per_objfile *m_per_objfile;
1835 };
1836
1837 dwarf2_queue_item::~dwarf2_queue_item ()
1838 {
1839 /* Anything still marked queued is likely to be in an
1840 inconsistent state, so discard it. */
1841 if (per_cu->queued)
1842 {
1843 if (per_cu->cu != NULL)
1844 free_one_cached_comp_unit (per_cu);
1845 per_cu->queued = 0;
1846 }
1847 }
1848
1849 /* The return type of find_file_and_directory. Note, the enclosed
1850 string pointers are only valid while this object is valid. */
1851
1852 struct file_and_directory
1853 {
1854 /* The filename. This is never NULL. */
1855 const char *name;
1856
1857 /* The compilation directory. NULL if not known. If we needed to
1858 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1859 points directly to the DW_AT_comp_dir string attribute owned by
1860 the obstack that owns the DIE. */
1861 const char *comp_dir;
1862
1863 /* If we needed to build a new string for comp_dir, this is what
1864 owns the storage. */
1865 std::string comp_dir_storage;
1866 };
1867
1868 static file_and_directory find_file_and_directory (struct die_info *die,
1869 struct dwarf2_cu *cu);
1870
1871 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1872 enum class rcuh_kind { COMPILE, TYPE };
1873
1874 static const gdb_byte *read_and_check_comp_unit_head
1875 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1876 struct comp_unit_head *header,
1877 struct dwarf2_section_info *section,
1878 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1879 rcuh_kind section_kind);
1880
1881 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1882
1883 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1884
1885 static struct dwo_unit *lookup_dwo_unit_in_dwp
1886 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1887 struct dwp_file *dwp_file, const char *comp_dir,
1888 ULONGEST signature, int is_debug_types);
1889
1890 static struct dwp_file *get_dwp_file
1891 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1892
1893 static struct dwo_unit *lookup_dwo_comp_unit
1894 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1895
1896 static struct dwo_unit *lookup_dwo_type_unit
1897 (struct signatured_type *, const char *, const char *);
1898
1899 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1900
1901 /* A unique pointer to a dwo_file. */
1902
1903 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1904
1905 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1906
1907 static void check_producer (struct dwarf2_cu *cu);
1908
1909 static void free_line_header_voidp (void *arg);
1910 \f
1911 /* Various complaints about symbol reading that don't abort the process. */
1912
1913 static void
1914 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1915 {
1916 complaint (_("statement list doesn't fit in .debug_line section"));
1917 }
1918
1919 static void
1920 dwarf2_debug_line_missing_file_complaint (void)
1921 {
1922 complaint (_(".debug_line section has line data without a file"));
1923 }
1924
1925 static void
1926 dwarf2_debug_line_missing_end_sequence_complaint (void)
1927 {
1928 complaint (_(".debug_line section has line "
1929 "program sequence without an end"));
1930 }
1931
1932 static void
1933 dwarf2_complex_location_expr_complaint (void)
1934 {
1935 complaint (_("location expression too complex"));
1936 }
1937
1938 static void
1939 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1940 int arg3)
1941 {
1942 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1943 arg1, arg2, arg3);
1944 }
1945
1946 static void
1947 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1948 {
1949 complaint (_("debug info runs off end of %s section"
1950 " [in module %s]"),
1951 section->get_name (),
1952 section->get_file_name ());
1953 }
1954
1955 static void
1956 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1957 {
1958 complaint (_("macro debug info contains a "
1959 "malformed macro definition:\n`%s'"),
1960 arg1);
1961 }
1962
1963 static void
1964 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1965 {
1966 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1967 arg1, arg2);
1968 }
1969
1970 /* Hash function for line_header_hash. */
1971
1972 static hashval_t
1973 line_header_hash (const struct line_header *ofs)
1974 {
1975 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1976 }
1977
1978 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1979
1980 static hashval_t
1981 line_header_hash_voidp (const void *item)
1982 {
1983 const struct line_header *ofs = (const struct line_header *) item;
1984
1985 return line_header_hash (ofs);
1986 }
1987
1988 /* Equality function for line_header_hash. */
1989
1990 static int
1991 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1992 {
1993 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1994 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1995
1996 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1997 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1998 }
1999
2000 \f
2001
2002 /* See declaration. */
2003
2004 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2005 const dwarf2_debug_sections *names,
2006 bool can_copy_)
2007 : objfile (objfile_),
2008 can_copy (can_copy_)
2009 {
2010 if (names == NULL)
2011 names = &dwarf2_elf_names;
2012
2013 bfd *obfd = objfile->obfd;
2014
2015 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2016 locate_sections (obfd, sec, *names);
2017 }
2018
2019 dwarf2_per_objfile::~dwarf2_per_objfile ()
2020 {
2021 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2022 free_cached_comp_units ();
2023
2024 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2025 per_cu->imported_symtabs_free ();
2026
2027 for (signatured_type *sig_type : all_type_units)
2028 sig_type->per_cu.imported_symtabs_free ();
2029
2030 /* Everything else should be on the objfile obstack. */
2031 }
2032
2033 /* See declaration. */
2034
2035 void
2036 dwarf2_per_objfile::free_cached_comp_units ()
2037 {
2038 dwarf2_per_cu_data *per_cu = read_in_chain;
2039 dwarf2_per_cu_data **last_chain = &read_in_chain;
2040 while (per_cu != NULL)
2041 {
2042 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2043
2044 delete per_cu->cu;
2045 *last_chain = next_cu;
2046 per_cu = next_cu;
2047 }
2048 }
2049
2050 /* A helper class that calls free_cached_comp_units on
2051 destruction. */
2052
2053 class free_cached_comp_units
2054 {
2055 public:
2056
2057 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2058 : m_per_objfile (per_objfile)
2059 {
2060 }
2061
2062 ~free_cached_comp_units ()
2063 {
2064 m_per_objfile->free_cached_comp_units ();
2065 }
2066
2067 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2068
2069 private:
2070
2071 dwarf2_per_objfile *m_per_objfile;
2072 };
2073
2074 /* Try to locate the sections we need for DWARF 2 debugging
2075 information and return true if we have enough to do something.
2076 NAMES points to the dwarf2 section names, or is NULL if the standard
2077 ELF names are used. CAN_COPY is true for formats where symbol
2078 interposition is possible and so symbol values must follow copy
2079 relocation rules. */
2080
2081 int
2082 dwarf2_has_info (struct objfile *objfile,
2083 const struct dwarf2_debug_sections *names,
2084 bool can_copy)
2085 {
2086 if (objfile->flags & OBJF_READNEVER)
2087 return 0;
2088
2089 struct dwarf2_per_objfile *dwarf2_per_objfile
2090 = get_dwarf2_per_objfile (objfile);
2091
2092 if (dwarf2_per_objfile == NULL)
2093 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2094 names,
2095 can_copy);
2096
2097 return (!dwarf2_per_objfile->info.is_virtual
2098 && dwarf2_per_objfile->info.s.section != NULL
2099 && !dwarf2_per_objfile->abbrev.is_virtual
2100 && dwarf2_per_objfile->abbrev.s.section != NULL);
2101 }
2102
2103 /* When loading sections, we look either for uncompressed section or for
2104 compressed section names. */
2105
2106 static int
2107 section_is_p (const char *section_name,
2108 const struct dwarf2_section_names *names)
2109 {
2110 if (names->normal != NULL
2111 && strcmp (section_name, names->normal) == 0)
2112 return 1;
2113 if (names->compressed != NULL
2114 && strcmp (section_name, names->compressed) == 0)
2115 return 1;
2116 return 0;
2117 }
2118
2119 /* See declaration. */
2120
2121 void
2122 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2123 const dwarf2_debug_sections &names)
2124 {
2125 flagword aflag = bfd_section_flags (sectp);
2126
2127 if ((aflag & SEC_HAS_CONTENTS) == 0)
2128 {
2129 }
2130 else if (elf_section_data (sectp)->this_hdr.sh_size
2131 > bfd_get_file_size (abfd))
2132 {
2133 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2134 warning (_("Discarding section %s which has a section size (%s"
2135 ") larger than the file size [in module %s]"),
2136 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2137 bfd_get_filename (abfd));
2138 }
2139 else if (section_is_p (sectp->name, &names.info))
2140 {
2141 this->info.s.section = sectp;
2142 this->info.size = bfd_section_size (sectp);
2143 }
2144 else if (section_is_p (sectp->name, &names.abbrev))
2145 {
2146 this->abbrev.s.section = sectp;
2147 this->abbrev.size = bfd_section_size (sectp);
2148 }
2149 else if (section_is_p (sectp->name, &names.line))
2150 {
2151 this->line.s.section = sectp;
2152 this->line.size = bfd_section_size (sectp);
2153 }
2154 else if (section_is_p (sectp->name, &names.loc))
2155 {
2156 this->loc.s.section = sectp;
2157 this->loc.size = bfd_section_size (sectp);
2158 }
2159 else if (section_is_p (sectp->name, &names.loclists))
2160 {
2161 this->loclists.s.section = sectp;
2162 this->loclists.size = bfd_section_size (sectp);
2163 }
2164 else if (section_is_p (sectp->name, &names.macinfo))
2165 {
2166 this->macinfo.s.section = sectp;
2167 this->macinfo.size = bfd_section_size (sectp);
2168 }
2169 else if (section_is_p (sectp->name, &names.macro))
2170 {
2171 this->macro.s.section = sectp;
2172 this->macro.size = bfd_section_size (sectp);
2173 }
2174 else if (section_is_p (sectp->name, &names.str))
2175 {
2176 this->str.s.section = sectp;
2177 this->str.size = bfd_section_size (sectp);
2178 }
2179 else if (section_is_p (sectp->name, &names.str_offsets))
2180 {
2181 this->str_offsets.s.section = sectp;
2182 this->str_offsets.size = bfd_section_size (sectp);
2183 }
2184 else if (section_is_p (sectp->name, &names.line_str))
2185 {
2186 this->line_str.s.section = sectp;
2187 this->line_str.size = bfd_section_size (sectp);
2188 }
2189 else if (section_is_p (sectp->name, &names.addr))
2190 {
2191 this->addr.s.section = sectp;
2192 this->addr.size = bfd_section_size (sectp);
2193 }
2194 else if (section_is_p (sectp->name, &names.frame))
2195 {
2196 this->frame.s.section = sectp;
2197 this->frame.size = bfd_section_size (sectp);
2198 }
2199 else if (section_is_p (sectp->name, &names.eh_frame))
2200 {
2201 this->eh_frame.s.section = sectp;
2202 this->eh_frame.size = bfd_section_size (sectp);
2203 }
2204 else if (section_is_p (sectp->name, &names.ranges))
2205 {
2206 this->ranges.s.section = sectp;
2207 this->ranges.size = bfd_section_size (sectp);
2208 }
2209 else if (section_is_p (sectp->name, &names.rnglists))
2210 {
2211 this->rnglists.s.section = sectp;
2212 this->rnglists.size = bfd_section_size (sectp);
2213 }
2214 else if (section_is_p (sectp->name, &names.types))
2215 {
2216 struct dwarf2_section_info type_section;
2217
2218 memset (&type_section, 0, sizeof (type_section));
2219 type_section.s.section = sectp;
2220 type_section.size = bfd_section_size (sectp);
2221
2222 this->types.push_back (type_section);
2223 }
2224 else if (section_is_p (sectp->name, &names.gdb_index))
2225 {
2226 this->gdb_index.s.section = sectp;
2227 this->gdb_index.size = bfd_section_size (sectp);
2228 }
2229 else if (section_is_p (sectp->name, &names.debug_names))
2230 {
2231 this->debug_names.s.section = sectp;
2232 this->debug_names.size = bfd_section_size (sectp);
2233 }
2234 else if (section_is_p (sectp->name, &names.debug_aranges))
2235 {
2236 this->debug_aranges.s.section = sectp;
2237 this->debug_aranges.size = bfd_section_size (sectp);
2238 }
2239
2240 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2241 && bfd_section_vma (sectp) == 0)
2242 this->has_section_at_zero = true;
2243 }
2244
2245 /* A helper function that returns the size of a section in a safe way.
2246 If you are positive that the section has been read before using the
2247 size, then it is safe to refer to the dwarf2_section_info object's
2248 "size" field directly. In other cases, you must call this
2249 function, because for compressed sections the size field is not set
2250 correctly until the section has been read. */
2251
2252 static bfd_size_type
2253 dwarf2_section_size (struct objfile *objfile,
2254 struct dwarf2_section_info *info)
2255 {
2256 if (!info->readin)
2257 info->read (objfile);
2258 return info->size;
2259 }
2260
2261 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2262 SECTION_NAME. */
2263
2264 void
2265 dwarf2_get_section_info (struct objfile *objfile,
2266 enum dwarf2_section_enum sect,
2267 asection **sectp, const gdb_byte **bufp,
2268 bfd_size_type *sizep)
2269 {
2270 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2271 struct dwarf2_section_info *info;
2272
2273 /* We may see an objfile without any DWARF, in which case we just
2274 return nothing. */
2275 if (data == NULL)
2276 {
2277 *sectp = NULL;
2278 *bufp = NULL;
2279 *sizep = 0;
2280 return;
2281 }
2282 switch (sect)
2283 {
2284 case DWARF2_DEBUG_FRAME:
2285 info = &data->frame;
2286 break;
2287 case DWARF2_EH_FRAME:
2288 info = &data->eh_frame;
2289 break;
2290 default:
2291 gdb_assert_not_reached ("unexpected section");
2292 }
2293
2294 info->read (objfile);
2295
2296 *sectp = info->get_bfd_section ();
2297 *bufp = info->buffer;
2298 *sizep = info->size;
2299 }
2300
2301 /* A helper function to find the sections for a .dwz file. */
2302
2303 static void
2304 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2305 {
2306 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2307
2308 /* Note that we only support the standard ELF names, because .dwz
2309 is ELF-only (at the time of writing). */
2310 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2311 {
2312 dwz_file->abbrev.s.section = sectp;
2313 dwz_file->abbrev.size = bfd_section_size (sectp);
2314 }
2315 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2316 {
2317 dwz_file->info.s.section = sectp;
2318 dwz_file->info.size = bfd_section_size (sectp);
2319 }
2320 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2321 {
2322 dwz_file->str.s.section = sectp;
2323 dwz_file->str.size = bfd_section_size (sectp);
2324 }
2325 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2326 {
2327 dwz_file->line.s.section = sectp;
2328 dwz_file->line.size = bfd_section_size (sectp);
2329 }
2330 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2331 {
2332 dwz_file->macro.s.section = sectp;
2333 dwz_file->macro.size = bfd_section_size (sectp);
2334 }
2335 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2336 {
2337 dwz_file->gdb_index.s.section = sectp;
2338 dwz_file->gdb_index.size = bfd_section_size (sectp);
2339 }
2340 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2341 {
2342 dwz_file->debug_names.s.section = sectp;
2343 dwz_file->debug_names.size = bfd_section_size (sectp);
2344 }
2345 }
2346
2347 /* See dwarf2read.h. */
2348
2349 struct dwz_file *
2350 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2351 {
2352 const char *filename;
2353 bfd_size_type buildid_len_arg;
2354 size_t buildid_len;
2355 bfd_byte *buildid;
2356
2357 if (dwarf2_per_objfile->dwz_file != NULL)
2358 return dwarf2_per_objfile->dwz_file.get ();
2359
2360 bfd_set_error (bfd_error_no_error);
2361 gdb::unique_xmalloc_ptr<char> data
2362 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2363 &buildid_len_arg, &buildid));
2364 if (data == NULL)
2365 {
2366 if (bfd_get_error () == bfd_error_no_error)
2367 return NULL;
2368 error (_("could not read '.gnu_debugaltlink' section: %s"),
2369 bfd_errmsg (bfd_get_error ()));
2370 }
2371
2372 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2373
2374 buildid_len = (size_t) buildid_len_arg;
2375
2376 filename = data.get ();
2377
2378 std::string abs_storage;
2379 if (!IS_ABSOLUTE_PATH (filename))
2380 {
2381 gdb::unique_xmalloc_ptr<char> abs
2382 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2383
2384 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2385 filename = abs_storage.c_str ();
2386 }
2387
2388 /* First try the file name given in the section. If that doesn't
2389 work, try to use the build-id instead. */
2390 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2391 if (dwz_bfd != NULL)
2392 {
2393 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2394 dwz_bfd.reset (nullptr);
2395 }
2396
2397 if (dwz_bfd == NULL)
2398 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2399
2400 if (dwz_bfd == NULL)
2401 error (_("could not find '.gnu_debugaltlink' file for %s"),
2402 objfile_name (dwarf2_per_objfile->objfile));
2403
2404 std::unique_ptr<struct dwz_file> result
2405 (new struct dwz_file (std::move (dwz_bfd)));
2406
2407 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2408 result.get ());
2409
2410 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2411 result->dwz_bfd.get ());
2412 dwarf2_per_objfile->dwz_file = std::move (result);
2413 return dwarf2_per_objfile->dwz_file.get ();
2414 }
2415 \f
2416 /* DWARF quick_symbols_functions support. */
2417
2418 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2419 unique line tables, so we maintain a separate table of all .debug_line
2420 derived entries to support the sharing.
2421 All the quick functions need is the list of file names. We discard the
2422 line_header when we're done and don't need to record it here. */
2423 struct quick_file_names
2424 {
2425 /* The data used to construct the hash key. */
2426 struct stmt_list_hash hash;
2427
2428 /* The number of entries in file_names, real_names. */
2429 unsigned int num_file_names;
2430
2431 /* The file names from the line table, after being run through
2432 file_full_name. */
2433 const char **file_names;
2434
2435 /* The file names from the line table after being run through
2436 gdb_realpath. These are computed lazily. */
2437 const char **real_names;
2438 };
2439
2440 /* When using the index (and thus not using psymtabs), each CU has an
2441 object of this type. This is used to hold information needed by
2442 the various "quick" methods. */
2443 struct dwarf2_per_cu_quick_data
2444 {
2445 /* The file table. This can be NULL if there was no file table
2446 or it's currently not read in.
2447 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2448 struct quick_file_names *file_names;
2449
2450 /* The corresponding symbol table. This is NULL if symbols for this
2451 CU have not yet been read. */
2452 struct compunit_symtab *compunit_symtab;
2453
2454 /* A temporary mark bit used when iterating over all CUs in
2455 expand_symtabs_matching. */
2456 unsigned int mark : 1;
2457
2458 /* True if we've tried to read the file table and found there isn't one.
2459 There will be no point in trying to read it again next time. */
2460 unsigned int no_file_data : 1;
2461 };
2462
2463 /* Utility hash function for a stmt_list_hash. */
2464
2465 static hashval_t
2466 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2467 {
2468 hashval_t v = 0;
2469
2470 if (stmt_list_hash->dwo_unit != NULL)
2471 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2472 v += to_underlying (stmt_list_hash->line_sect_off);
2473 return v;
2474 }
2475
2476 /* Utility equality function for a stmt_list_hash. */
2477
2478 static int
2479 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2480 const struct stmt_list_hash *rhs)
2481 {
2482 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2483 return 0;
2484 if (lhs->dwo_unit != NULL
2485 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2486 return 0;
2487
2488 return lhs->line_sect_off == rhs->line_sect_off;
2489 }
2490
2491 /* Hash function for a quick_file_names. */
2492
2493 static hashval_t
2494 hash_file_name_entry (const void *e)
2495 {
2496 const struct quick_file_names *file_data
2497 = (const struct quick_file_names *) e;
2498
2499 return hash_stmt_list_entry (&file_data->hash);
2500 }
2501
2502 /* Equality function for a quick_file_names. */
2503
2504 static int
2505 eq_file_name_entry (const void *a, const void *b)
2506 {
2507 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2508 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2509
2510 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2511 }
2512
2513 /* Delete function for a quick_file_names. */
2514
2515 static void
2516 delete_file_name_entry (void *e)
2517 {
2518 struct quick_file_names *file_data = (struct quick_file_names *) e;
2519 int i;
2520
2521 for (i = 0; i < file_data->num_file_names; ++i)
2522 {
2523 xfree ((void*) file_data->file_names[i]);
2524 if (file_data->real_names)
2525 xfree ((void*) file_data->real_names[i]);
2526 }
2527
2528 /* The space for the struct itself lives on objfile_obstack,
2529 so we don't free it here. */
2530 }
2531
2532 /* Create a quick_file_names hash table. */
2533
2534 static htab_up
2535 create_quick_file_names_table (unsigned int nr_initial_entries)
2536 {
2537 return htab_up (htab_create_alloc (nr_initial_entries,
2538 hash_file_name_entry, eq_file_name_entry,
2539 delete_file_name_entry, xcalloc, xfree));
2540 }
2541
2542 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2543 have to be created afterwards. You should call age_cached_comp_units after
2544 processing PER_CU->CU. dw2_setup must have been already called. */
2545
2546 static void
2547 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2548 {
2549 if (per_cu->is_debug_types)
2550 load_full_type_unit (per_cu);
2551 else
2552 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2553
2554 if (per_cu->cu == NULL)
2555 return; /* Dummy CU. */
2556
2557 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2558 }
2559
2560 /* Read in the symbols for PER_CU. */
2561
2562 static void
2563 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2564 {
2565 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2566
2567 /* Skip type_unit_groups, reading the type units they contain
2568 is handled elsewhere. */
2569 if (IS_TYPE_UNIT_GROUP (per_cu))
2570 return;
2571
2572 /* The destructor of dwarf2_queue_guard frees any entries left on
2573 the queue. After this point we're guaranteed to leave this function
2574 with the dwarf queue empty. */
2575 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2576
2577 if (dwarf2_per_objfile->using_index
2578 ? per_cu->v.quick->compunit_symtab == NULL
2579 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2580 {
2581 queue_comp_unit (per_cu, language_minimal);
2582 load_cu (per_cu, skip_partial);
2583
2584 /* If we just loaded a CU from a DWO, and we're working with an index
2585 that may badly handle TUs, load all the TUs in that DWO as well.
2586 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2587 if (!per_cu->is_debug_types
2588 && per_cu->cu != NULL
2589 && per_cu->cu->dwo_unit != NULL
2590 && dwarf2_per_objfile->index_table != NULL
2591 && dwarf2_per_objfile->index_table->version <= 7
2592 /* DWP files aren't supported yet. */
2593 && get_dwp_file (dwarf2_per_objfile) == NULL)
2594 queue_and_load_all_dwo_tus (per_cu);
2595 }
2596
2597 process_queue (dwarf2_per_objfile);
2598
2599 /* Age the cache, releasing compilation units that have not
2600 been used recently. */
2601 age_cached_comp_units (dwarf2_per_objfile);
2602 }
2603
2604 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2605 the objfile from which this CU came. Returns the resulting symbol
2606 table. */
2607
2608 static struct compunit_symtab *
2609 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2610 {
2611 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2612
2613 gdb_assert (dwarf2_per_objfile->using_index);
2614 if (!per_cu->v.quick->compunit_symtab)
2615 {
2616 free_cached_comp_units freer (dwarf2_per_objfile);
2617 scoped_restore decrementer = increment_reading_symtab ();
2618 dw2_do_instantiate_symtab (per_cu, skip_partial);
2619 process_cu_includes (dwarf2_per_objfile);
2620 }
2621
2622 return per_cu->v.quick->compunit_symtab;
2623 }
2624
2625 /* See declaration. */
2626
2627 dwarf2_per_cu_data *
2628 dwarf2_per_objfile::get_cutu (int index)
2629 {
2630 if (index >= this->all_comp_units.size ())
2631 {
2632 index -= this->all_comp_units.size ();
2633 gdb_assert (index < this->all_type_units.size ());
2634 return &this->all_type_units[index]->per_cu;
2635 }
2636
2637 return this->all_comp_units[index];
2638 }
2639
2640 /* See declaration. */
2641
2642 dwarf2_per_cu_data *
2643 dwarf2_per_objfile::get_cu (int index)
2644 {
2645 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2646
2647 return this->all_comp_units[index];
2648 }
2649
2650 /* See declaration. */
2651
2652 signatured_type *
2653 dwarf2_per_objfile::get_tu (int index)
2654 {
2655 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2656
2657 return this->all_type_units[index];
2658 }
2659
2660 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2661 objfile_obstack, and constructed with the specified field
2662 values. */
2663
2664 static dwarf2_per_cu_data *
2665 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2666 struct dwarf2_section_info *section,
2667 int is_dwz,
2668 sect_offset sect_off, ULONGEST length)
2669 {
2670 struct objfile *objfile = dwarf2_per_objfile->objfile;
2671 dwarf2_per_cu_data *the_cu
2672 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2673 struct dwarf2_per_cu_data);
2674 the_cu->sect_off = sect_off;
2675 the_cu->length = length;
2676 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2677 the_cu->section = section;
2678 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2679 struct dwarf2_per_cu_quick_data);
2680 the_cu->is_dwz = is_dwz;
2681 return the_cu;
2682 }
2683
2684 /* A helper for create_cus_from_index that handles a given list of
2685 CUs. */
2686
2687 static void
2688 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2689 const gdb_byte *cu_list, offset_type n_elements,
2690 struct dwarf2_section_info *section,
2691 int is_dwz)
2692 {
2693 for (offset_type i = 0; i < n_elements; i += 2)
2694 {
2695 gdb_static_assert (sizeof (ULONGEST) >= 8);
2696
2697 sect_offset sect_off
2698 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2699 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2700 cu_list += 2 * 8;
2701
2702 dwarf2_per_cu_data *per_cu
2703 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2704 sect_off, length);
2705 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2706 }
2707 }
2708
2709 /* Read the CU list from the mapped index, and use it to create all
2710 the CU objects for this objfile. */
2711
2712 static void
2713 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2714 const gdb_byte *cu_list, offset_type cu_list_elements,
2715 const gdb_byte *dwz_list, offset_type dwz_elements)
2716 {
2717 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2718 dwarf2_per_objfile->all_comp_units.reserve
2719 ((cu_list_elements + dwz_elements) / 2);
2720
2721 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2722 &dwarf2_per_objfile->info, 0);
2723
2724 if (dwz_elements == 0)
2725 return;
2726
2727 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2728 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2729 &dwz->info, 1);
2730 }
2731
2732 /* Create the signatured type hash table from the index. */
2733
2734 static void
2735 create_signatured_type_table_from_index
2736 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2737 struct dwarf2_section_info *section,
2738 const gdb_byte *bytes,
2739 offset_type elements)
2740 {
2741 struct objfile *objfile = dwarf2_per_objfile->objfile;
2742
2743 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2744 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2745
2746 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2747
2748 for (offset_type i = 0; i < elements; i += 3)
2749 {
2750 struct signatured_type *sig_type;
2751 ULONGEST signature;
2752 void **slot;
2753 cu_offset type_offset_in_tu;
2754
2755 gdb_static_assert (sizeof (ULONGEST) >= 8);
2756 sect_offset sect_off
2757 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2758 type_offset_in_tu
2759 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2760 BFD_ENDIAN_LITTLE);
2761 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2762 bytes += 3 * 8;
2763
2764 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2765 struct signatured_type);
2766 sig_type->signature = signature;
2767 sig_type->type_offset_in_tu = type_offset_in_tu;
2768 sig_type->per_cu.is_debug_types = 1;
2769 sig_type->per_cu.section = section;
2770 sig_type->per_cu.sect_off = sect_off;
2771 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2772 sig_type->per_cu.v.quick
2773 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2774 struct dwarf2_per_cu_quick_data);
2775
2776 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2777 *slot = sig_type;
2778
2779 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2780 }
2781
2782 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2783 }
2784
2785 /* Create the signatured type hash table from .debug_names. */
2786
2787 static void
2788 create_signatured_type_table_from_debug_names
2789 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2790 const mapped_debug_names &map,
2791 struct dwarf2_section_info *section,
2792 struct dwarf2_section_info *abbrev_section)
2793 {
2794 struct objfile *objfile = dwarf2_per_objfile->objfile;
2795
2796 section->read (objfile);
2797 abbrev_section->read (objfile);
2798
2799 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2800 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2801
2802 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2803
2804 for (uint32_t i = 0; i < map.tu_count; ++i)
2805 {
2806 struct signatured_type *sig_type;
2807 void **slot;
2808
2809 sect_offset sect_off
2810 = (sect_offset) (extract_unsigned_integer
2811 (map.tu_table_reordered + i * map.offset_size,
2812 map.offset_size,
2813 map.dwarf5_byte_order));
2814
2815 comp_unit_head cu_header;
2816 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2817 abbrev_section,
2818 section->buffer + to_underlying (sect_off),
2819 rcuh_kind::TYPE);
2820
2821 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2822 struct signatured_type);
2823 sig_type->signature = cu_header.signature;
2824 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2825 sig_type->per_cu.is_debug_types = 1;
2826 sig_type->per_cu.section = section;
2827 sig_type->per_cu.sect_off = sect_off;
2828 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2829 sig_type->per_cu.v.quick
2830 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2831 struct dwarf2_per_cu_quick_data);
2832
2833 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2834 *slot = sig_type;
2835
2836 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2837 }
2838
2839 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2840 }
2841
2842 /* Read the address map data from the mapped index, and use it to
2843 populate the objfile's psymtabs_addrmap. */
2844
2845 static void
2846 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2847 struct mapped_index *index)
2848 {
2849 struct objfile *objfile = dwarf2_per_objfile->objfile;
2850 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2851 const gdb_byte *iter, *end;
2852 struct addrmap *mutable_map;
2853 CORE_ADDR baseaddr;
2854
2855 auto_obstack temp_obstack;
2856
2857 mutable_map = addrmap_create_mutable (&temp_obstack);
2858
2859 iter = index->address_table.data ();
2860 end = iter + index->address_table.size ();
2861
2862 baseaddr = objfile->text_section_offset ();
2863
2864 while (iter < end)
2865 {
2866 ULONGEST hi, lo, cu_index;
2867 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2868 iter += 8;
2869 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2870 iter += 8;
2871 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2872 iter += 4;
2873
2874 if (lo > hi)
2875 {
2876 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2877 hex_string (lo), hex_string (hi));
2878 continue;
2879 }
2880
2881 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2882 {
2883 complaint (_(".gdb_index address table has invalid CU number %u"),
2884 (unsigned) cu_index);
2885 continue;
2886 }
2887
2888 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2889 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2890 addrmap_set_empty (mutable_map, lo, hi - 1,
2891 dwarf2_per_objfile->get_cu (cu_index));
2892 }
2893
2894 objfile->partial_symtabs->psymtabs_addrmap
2895 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2896 }
2897
2898 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2899 populate the objfile's psymtabs_addrmap. */
2900
2901 static void
2902 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2903 struct dwarf2_section_info *section)
2904 {
2905 struct objfile *objfile = dwarf2_per_objfile->objfile;
2906 bfd *abfd = objfile->obfd;
2907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2908 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2909
2910 auto_obstack temp_obstack;
2911 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2912
2913 std::unordered_map<sect_offset,
2914 dwarf2_per_cu_data *,
2915 gdb::hash_enum<sect_offset>>
2916 debug_info_offset_to_per_cu;
2917 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2918 {
2919 const auto insertpair
2920 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2921 if (!insertpair.second)
2922 {
2923 warning (_("Section .debug_aranges in %s has duplicate "
2924 "debug_info_offset %s, ignoring .debug_aranges."),
2925 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2926 return;
2927 }
2928 }
2929
2930 section->read (objfile);
2931
2932 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2933
2934 const gdb_byte *addr = section->buffer;
2935
2936 while (addr < section->buffer + section->size)
2937 {
2938 const gdb_byte *const entry_addr = addr;
2939 unsigned int bytes_read;
2940
2941 const LONGEST entry_length = read_initial_length (abfd, addr,
2942 &bytes_read);
2943 addr += bytes_read;
2944
2945 const gdb_byte *const entry_end = addr + entry_length;
2946 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2947 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2948 if (addr + entry_length > section->buffer + section->size)
2949 {
2950 warning (_("Section .debug_aranges in %s entry at offset %s "
2951 "length %s exceeds section length %s, "
2952 "ignoring .debug_aranges."),
2953 objfile_name (objfile),
2954 plongest (entry_addr - section->buffer),
2955 plongest (bytes_read + entry_length),
2956 pulongest (section->size));
2957 return;
2958 }
2959
2960 /* The version number. */
2961 const uint16_t version = read_2_bytes (abfd, addr);
2962 addr += 2;
2963 if (version != 2)
2964 {
2965 warning (_("Section .debug_aranges in %s entry at offset %s "
2966 "has unsupported version %d, ignoring .debug_aranges."),
2967 objfile_name (objfile),
2968 plongest (entry_addr - section->buffer), version);
2969 return;
2970 }
2971
2972 const uint64_t debug_info_offset
2973 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2974 addr += offset_size;
2975 const auto per_cu_it
2976 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2977 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2978 {
2979 warning (_("Section .debug_aranges in %s entry at offset %s "
2980 "debug_info_offset %s does not exists, "
2981 "ignoring .debug_aranges."),
2982 objfile_name (objfile),
2983 plongest (entry_addr - section->buffer),
2984 pulongest (debug_info_offset));
2985 return;
2986 }
2987 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2988
2989 const uint8_t address_size = *addr++;
2990 if (address_size < 1 || address_size > 8)
2991 {
2992 warning (_("Section .debug_aranges in %s entry at offset %s "
2993 "address_size %u is invalid, ignoring .debug_aranges."),
2994 objfile_name (objfile),
2995 plongest (entry_addr - section->buffer), address_size);
2996 return;
2997 }
2998
2999 const uint8_t segment_selector_size = *addr++;
3000 if (segment_selector_size != 0)
3001 {
3002 warning (_("Section .debug_aranges in %s entry at offset %s "
3003 "segment_selector_size %u is not supported, "
3004 "ignoring .debug_aranges."),
3005 objfile_name (objfile),
3006 plongest (entry_addr - section->buffer),
3007 segment_selector_size);
3008 return;
3009 }
3010
3011 /* Must pad to an alignment boundary that is twice the address
3012 size. It is undocumented by the DWARF standard but GCC does
3013 use it. */
3014 for (size_t padding = ((-(addr - section->buffer))
3015 & (2 * address_size - 1));
3016 padding > 0; padding--)
3017 if (*addr++ != 0)
3018 {
3019 warning (_("Section .debug_aranges in %s entry at offset %s "
3020 "padding is not zero, ignoring .debug_aranges."),
3021 objfile_name (objfile),
3022 plongest (entry_addr - section->buffer));
3023 return;
3024 }
3025
3026 for (;;)
3027 {
3028 if (addr + 2 * address_size > entry_end)
3029 {
3030 warning (_("Section .debug_aranges in %s entry at offset %s "
3031 "address list is not properly terminated, "
3032 "ignoring .debug_aranges."),
3033 objfile_name (objfile),
3034 plongest (entry_addr - section->buffer));
3035 return;
3036 }
3037 ULONGEST start = extract_unsigned_integer (addr, address_size,
3038 dwarf5_byte_order);
3039 addr += address_size;
3040 ULONGEST length = extract_unsigned_integer (addr, address_size,
3041 dwarf5_byte_order);
3042 addr += address_size;
3043 if (start == 0 && length == 0)
3044 break;
3045 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3046 {
3047 /* Symbol was eliminated due to a COMDAT group. */
3048 continue;
3049 }
3050 ULONGEST end = start + length;
3051 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3052 - baseaddr);
3053 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3054 - baseaddr);
3055 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3056 }
3057 }
3058
3059 objfile->partial_symtabs->psymtabs_addrmap
3060 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3061 }
3062
3063 /* Find a slot in the mapped index INDEX for the object named NAME.
3064 If NAME is found, set *VEC_OUT to point to the CU vector in the
3065 constant pool and return true. If NAME cannot be found, return
3066 false. */
3067
3068 static bool
3069 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3070 offset_type **vec_out)
3071 {
3072 offset_type hash;
3073 offset_type slot, step;
3074 int (*cmp) (const char *, const char *);
3075
3076 gdb::unique_xmalloc_ptr<char> without_params;
3077 if (current_language->la_language == language_cplus
3078 || current_language->la_language == language_fortran
3079 || current_language->la_language == language_d)
3080 {
3081 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3082 not contain any. */
3083
3084 if (strchr (name, '(') != NULL)
3085 {
3086 without_params = cp_remove_params (name);
3087
3088 if (without_params != NULL)
3089 name = without_params.get ();
3090 }
3091 }
3092
3093 /* Index version 4 did not support case insensitive searches. But the
3094 indices for case insensitive languages are built in lowercase, therefore
3095 simulate our NAME being searched is also lowercased. */
3096 hash = mapped_index_string_hash ((index->version == 4
3097 && case_sensitivity == case_sensitive_off
3098 ? 5 : index->version),
3099 name);
3100
3101 slot = hash & (index->symbol_table.size () - 1);
3102 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3103 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3104
3105 for (;;)
3106 {
3107 const char *str;
3108
3109 const auto &bucket = index->symbol_table[slot];
3110 if (bucket.name == 0 && bucket.vec == 0)
3111 return false;
3112
3113 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3114 if (!cmp (name, str))
3115 {
3116 *vec_out = (offset_type *) (index->constant_pool
3117 + MAYBE_SWAP (bucket.vec));
3118 return true;
3119 }
3120
3121 slot = (slot + step) & (index->symbol_table.size () - 1);
3122 }
3123 }
3124
3125 /* A helper function that reads the .gdb_index from BUFFER and fills
3126 in MAP. FILENAME is the name of the file containing the data;
3127 it is used for error reporting. DEPRECATED_OK is true if it is
3128 ok to use deprecated sections.
3129
3130 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3131 out parameters that are filled in with information about the CU and
3132 TU lists in the section.
3133
3134 Returns true if all went well, false otherwise. */
3135
3136 static bool
3137 read_gdb_index_from_buffer (struct objfile *objfile,
3138 const char *filename,
3139 bool deprecated_ok,
3140 gdb::array_view<const gdb_byte> buffer,
3141 struct mapped_index *map,
3142 const gdb_byte **cu_list,
3143 offset_type *cu_list_elements,
3144 const gdb_byte **types_list,
3145 offset_type *types_list_elements)
3146 {
3147 const gdb_byte *addr = &buffer[0];
3148
3149 /* Version check. */
3150 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3151 /* Versions earlier than 3 emitted every copy of a psymbol. This
3152 causes the index to behave very poorly for certain requests. Version 3
3153 contained incomplete addrmap. So, it seems better to just ignore such
3154 indices. */
3155 if (version < 4)
3156 {
3157 static int warning_printed = 0;
3158 if (!warning_printed)
3159 {
3160 warning (_("Skipping obsolete .gdb_index section in %s."),
3161 filename);
3162 warning_printed = 1;
3163 }
3164 return 0;
3165 }
3166 /* Index version 4 uses a different hash function than index version
3167 5 and later.
3168
3169 Versions earlier than 6 did not emit psymbols for inlined
3170 functions. Using these files will cause GDB not to be able to
3171 set breakpoints on inlined functions by name, so we ignore these
3172 indices unless the user has done
3173 "set use-deprecated-index-sections on". */
3174 if (version < 6 && !deprecated_ok)
3175 {
3176 static int warning_printed = 0;
3177 if (!warning_printed)
3178 {
3179 warning (_("\
3180 Skipping deprecated .gdb_index section in %s.\n\
3181 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3182 to use the section anyway."),
3183 filename);
3184 warning_printed = 1;
3185 }
3186 return 0;
3187 }
3188 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3189 of the TU (for symbols coming from TUs),
3190 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3191 Plus gold-generated indices can have duplicate entries for global symbols,
3192 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3193 These are just performance bugs, and we can't distinguish gdb-generated
3194 indices from gold-generated ones, so issue no warning here. */
3195
3196 /* Indexes with higher version than the one supported by GDB may be no
3197 longer backward compatible. */
3198 if (version > 8)
3199 return 0;
3200
3201 map->version = version;
3202
3203 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3204
3205 int i = 0;
3206 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3207 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3208 / 8);
3209 ++i;
3210
3211 *types_list = addr + MAYBE_SWAP (metadata[i]);
3212 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3213 - MAYBE_SWAP (metadata[i]))
3214 / 8);
3215 ++i;
3216
3217 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3218 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3219 map->address_table
3220 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3221 ++i;
3222
3223 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3224 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3225 map->symbol_table
3226 = gdb::array_view<mapped_index::symbol_table_slot>
3227 ((mapped_index::symbol_table_slot *) symbol_table,
3228 (mapped_index::symbol_table_slot *) symbol_table_end);
3229
3230 ++i;
3231 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3232
3233 return 1;
3234 }
3235
3236 /* Callback types for dwarf2_read_gdb_index. */
3237
3238 typedef gdb::function_view
3239 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3240 get_gdb_index_contents_ftype;
3241 typedef gdb::function_view
3242 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3243 get_gdb_index_contents_dwz_ftype;
3244
3245 /* Read .gdb_index. If everything went ok, initialize the "quick"
3246 elements of all the CUs and return 1. Otherwise, return 0. */
3247
3248 static int
3249 dwarf2_read_gdb_index
3250 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3251 get_gdb_index_contents_ftype get_gdb_index_contents,
3252 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3253 {
3254 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3255 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3256 struct dwz_file *dwz;
3257 struct objfile *objfile = dwarf2_per_objfile->objfile;
3258
3259 gdb::array_view<const gdb_byte> main_index_contents
3260 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3261
3262 if (main_index_contents.empty ())
3263 return 0;
3264
3265 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3266 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3267 use_deprecated_index_sections,
3268 main_index_contents, map.get (), &cu_list,
3269 &cu_list_elements, &types_list,
3270 &types_list_elements))
3271 return 0;
3272
3273 /* Don't use the index if it's empty. */
3274 if (map->symbol_table.empty ())
3275 return 0;
3276
3277 /* If there is a .dwz file, read it so we can get its CU list as
3278 well. */
3279 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3280 if (dwz != NULL)
3281 {
3282 struct mapped_index dwz_map;
3283 const gdb_byte *dwz_types_ignore;
3284 offset_type dwz_types_elements_ignore;
3285
3286 gdb::array_view<const gdb_byte> dwz_index_content
3287 = get_gdb_index_contents_dwz (objfile, dwz);
3288
3289 if (dwz_index_content.empty ())
3290 return 0;
3291
3292 if (!read_gdb_index_from_buffer (objfile,
3293 bfd_get_filename (dwz->dwz_bfd.get ()),
3294 1, dwz_index_content, &dwz_map,
3295 &dwz_list, &dwz_list_elements,
3296 &dwz_types_ignore,
3297 &dwz_types_elements_ignore))
3298 {
3299 warning (_("could not read '.gdb_index' section from %s; skipping"),
3300 bfd_get_filename (dwz->dwz_bfd.get ()));
3301 return 0;
3302 }
3303 }
3304
3305 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3306 dwz_list, dwz_list_elements);
3307
3308 if (types_list_elements)
3309 {
3310 /* We can only handle a single .debug_types when we have an
3311 index. */
3312 if (dwarf2_per_objfile->types.size () != 1)
3313 return 0;
3314
3315 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3316
3317 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3318 types_list, types_list_elements);
3319 }
3320
3321 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3322
3323 dwarf2_per_objfile->index_table = std::move (map);
3324 dwarf2_per_objfile->using_index = 1;
3325 dwarf2_per_objfile->quick_file_names_table =
3326 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3327
3328 return 1;
3329 }
3330
3331 /* die_reader_func for dw2_get_file_names. */
3332
3333 static void
3334 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3335 const gdb_byte *info_ptr,
3336 struct die_info *comp_unit_die)
3337 {
3338 struct dwarf2_cu *cu = reader->cu;
3339 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3340 struct dwarf2_per_objfile *dwarf2_per_objfile
3341 = cu->per_cu->dwarf2_per_objfile;
3342 struct objfile *objfile = dwarf2_per_objfile->objfile;
3343 struct dwarf2_per_cu_data *lh_cu;
3344 struct attribute *attr;
3345 void **slot;
3346 struct quick_file_names *qfn;
3347
3348 gdb_assert (! this_cu->is_debug_types);
3349
3350 /* Our callers never want to match partial units -- instead they
3351 will match the enclosing full CU. */
3352 if (comp_unit_die->tag == DW_TAG_partial_unit)
3353 {
3354 this_cu->v.quick->no_file_data = 1;
3355 return;
3356 }
3357
3358 lh_cu = this_cu;
3359 slot = NULL;
3360
3361 line_header_up lh;
3362 sect_offset line_offset {};
3363
3364 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3365 if (attr != nullptr)
3366 {
3367 struct quick_file_names find_entry;
3368
3369 line_offset = (sect_offset) DW_UNSND (attr);
3370
3371 /* We may have already read in this line header (TU line header sharing).
3372 If we have we're done. */
3373 find_entry.hash.dwo_unit = cu->dwo_unit;
3374 find_entry.hash.line_sect_off = line_offset;
3375 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3376 &find_entry, INSERT);
3377 if (*slot != NULL)
3378 {
3379 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3380 return;
3381 }
3382
3383 lh = dwarf_decode_line_header (line_offset, cu);
3384 }
3385 if (lh == NULL)
3386 {
3387 lh_cu->v.quick->no_file_data = 1;
3388 return;
3389 }
3390
3391 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3392 qfn->hash.dwo_unit = cu->dwo_unit;
3393 qfn->hash.line_sect_off = line_offset;
3394 gdb_assert (slot != NULL);
3395 *slot = qfn;
3396
3397 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3398
3399 int offset = 0;
3400 if (strcmp (fnd.name, "<unknown>") != 0)
3401 ++offset;
3402
3403 qfn->num_file_names = offset + lh->file_names_size ();
3404 qfn->file_names =
3405 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3406 if (offset != 0)
3407 qfn->file_names[0] = xstrdup (fnd.name);
3408 for (int i = 0; i < lh->file_names_size (); ++i)
3409 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3410 fnd.comp_dir).release ();
3411 qfn->real_names = NULL;
3412
3413 lh_cu->v.quick->file_names = qfn;
3414 }
3415
3416 /* A helper for the "quick" functions which attempts to read the line
3417 table for THIS_CU. */
3418
3419 static struct quick_file_names *
3420 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3421 {
3422 /* This should never be called for TUs. */
3423 gdb_assert (! this_cu->is_debug_types);
3424 /* Nor type unit groups. */
3425 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3426
3427 if (this_cu->v.quick->file_names != NULL)
3428 return this_cu->v.quick->file_names;
3429 /* If we know there is no line data, no point in looking again. */
3430 if (this_cu->v.quick->no_file_data)
3431 return NULL;
3432
3433 cutu_reader reader (this_cu);
3434 if (!reader.dummy_p)
3435 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3436
3437 if (this_cu->v.quick->no_file_data)
3438 return NULL;
3439 return this_cu->v.quick->file_names;
3440 }
3441
3442 /* A helper for the "quick" functions which computes and caches the
3443 real path for a given file name from the line table. */
3444
3445 static const char *
3446 dw2_get_real_path (struct objfile *objfile,
3447 struct quick_file_names *qfn, int index)
3448 {
3449 if (qfn->real_names == NULL)
3450 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3451 qfn->num_file_names, const char *);
3452
3453 if (qfn->real_names[index] == NULL)
3454 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3455
3456 return qfn->real_names[index];
3457 }
3458
3459 static struct symtab *
3460 dw2_find_last_source_symtab (struct objfile *objfile)
3461 {
3462 struct dwarf2_per_objfile *dwarf2_per_objfile
3463 = get_dwarf2_per_objfile (objfile);
3464 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3465 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3466
3467 if (cust == NULL)
3468 return NULL;
3469
3470 return compunit_primary_filetab (cust);
3471 }
3472
3473 /* Traversal function for dw2_forget_cached_source_info. */
3474
3475 static int
3476 dw2_free_cached_file_names (void **slot, void *info)
3477 {
3478 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3479
3480 if (file_data->real_names)
3481 {
3482 int i;
3483
3484 for (i = 0; i < file_data->num_file_names; ++i)
3485 {
3486 xfree ((void*) file_data->real_names[i]);
3487 file_data->real_names[i] = NULL;
3488 }
3489 }
3490
3491 return 1;
3492 }
3493
3494 static void
3495 dw2_forget_cached_source_info (struct objfile *objfile)
3496 {
3497 struct dwarf2_per_objfile *dwarf2_per_objfile
3498 = get_dwarf2_per_objfile (objfile);
3499
3500 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3501 dw2_free_cached_file_names, NULL);
3502 }
3503
3504 /* Helper function for dw2_map_symtabs_matching_filename that expands
3505 the symtabs and calls the iterator. */
3506
3507 static int
3508 dw2_map_expand_apply (struct objfile *objfile,
3509 struct dwarf2_per_cu_data *per_cu,
3510 const char *name, const char *real_path,
3511 gdb::function_view<bool (symtab *)> callback)
3512 {
3513 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3514
3515 /* Don't visit already-expanded CUs. */
3516 if (per_cu->v.quick->compunit_symtab)
3517 return 0;
3518
3519 /* This may expand more than one symtab, and we want to iterate over
3520 all of them. */
3521 dw2_instantiate_symtab (per_cu, false);
3522
3523 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3524 last_made, callback);
3525 }
3526
3527 /* Implementation of the map_symtabs_matching_filename method. */
3528
3529 static bool
3530 dw2_map_symtabs_matching_filename
3531 (struct objfile *objfile, const char *name, const char *real_path,
3532 gdb::function_view<bool (symtab *)> callback)
3533 {
3534 const char *name_basename = lbasename (name);
3535 struct dwarf2_per_objfile *dwarf2_per_objfile
3536 = get_dwarf2_per_objfile (objfile);
3537
3538 /* The rule is CUs specify all the files, including those used by
3539 any TU, so there's no need to scan TUs here. */
3540
3541 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3542 {
3543 /* We only need to look at symtabs not already expanded. */
3544 if (per_cu->v.quick->compunit_symtab)
3545 continue;
3546
3547 quick_file_names *file_data = dw2_get_file_names (per_cu);
3548 if (file_data == NULL)
3549 continue;
3550
3551 for (int j = 0; j < file_data->num_file_names; ++j)
3552 {
3553 const char *this_name = file_data->file_names[j];
3554 const char *this_real_name;
3555
3556 if (compare_filenames_for_search (this_name, name))
3557 {
3558 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3559 callback))
3560 return true;
3561 continue;
3562 }
3563
3564 /* Before we invoke realpath, which can get expensive when many
3565 files are involved, do a quick comparison of the basenames. */
3566 if (! basenames_may_differ
3567 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3568 continue;
3569
3570 this_real_name = dw2_get_real_path (objfile, file_data, j);
3571 if (compare_filenames_for_search (this_real_name, name))
3572 {
3573 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3574 callback))
3575 return true;
3576 continue;
3577 }
3578
3579 if (real_path != NULL)
3580 {
3581 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3582 gdb_assert (IS_ABSOLUTE_PATH (name));
3583 if (this_real_name != NULL
3584 && FILENAME_CMP (real_path, this_real_name) == 0)
3585 {
3586 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3587 callback))
3588 return true;
3589 continue;
3590 }
3591 }
3592 }
3593 }
3594
3595 return false;
3596 }
3597
3598 /* Struct used to manage iterating over all CUs looking for a symbol. */
3599
3600 struct dw2_symtab_iterator
3601 {
3602 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3603 struct dwarf2_per_objfile *dwarf2_per_objfile;
3604 /* If set, only look for symbols that match that block. Valid values are
3605 GLOBAL_BLOCK and STATIC_BLOCK. */
3606 gdb::optional<block_enum> block_index;
3607 /* The kind of symbol we're looking for. */
3608 domain_enum domain;
3609 /* The list of CUs from the index entry of the symbol,
3610 or NULL if not found. */
3611 offset_type *vec;
3612 /* The next element in VEC to look at. */
3613 int next;
3614 /* The number of elements in VEC, or zero if there is no match. */
3615 int length;
3616 /* Have we seen a global version of the symbol?
3617 If so we can ignore all further global instances.
3618 This is to work around gold/15646, inefficient gold-generated
3619 indices. */
3620 int global_seen;
3621 };
3622
3623 /* Initialize the index symtab iterator ITER. */
3624
3625 static void
3626 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3627 struct dwarf2_per_objfile *dwarf2_per_objfile,
3628 gdb::optional<block_enum> block_index,
3629 domain_enum domain,
3630 const char *name)
3631 {
3632 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3633 iter->block_index = block_index;
3634 iter->domain = domain;
3635 iter->next = 0;
3636 iter->global_seen = 0;
3637
3638 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3639
3640 /* index is NULL if OBJF_READNOW. */
3641 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3642 iter->length = MAYBE_SWAP (*iter->vec);
3643 else
3644 {
3645 iter->vec = NULL;
3646 iter->length = 0;
3647 }
3648 }
3649
3650 /* Return the next matching CU or NULL if there are no more. */
3651
3652 static struct dwarf2_per_cu_data *
3653 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3654 {
3655 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3656
3657 for ( ; iter->next < iter->length; ++iter->next)
3658 {
3659 offset_type cu_index_and_attrs =
3660 MAYBE_SWAP (iter->vec[iter->next + 1]);
3661 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3662 gdb_index_symbol_kind symbol_kind =
3663 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3664 /* Only check the symbol attributes if they're present.
3665 Indices prior to version 7 don't record them,
3666 and indices >= 7 may elide them for certain symbols
3667 (gold does this). */
3668 int attrs_valid =
3669 (dwarf2_per_objfile->index_table->version >= 7
3670 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3671
3672 /* Don't crash on bad data. */
3673 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3674 + dwarf2_per_objfile->all_type_units.size ()))
3675 {
3676 complaint (_(".gdb_index entry has bad CU index"
3677 " [in module %s]"),
3678 objfile_name (dwarf2_per_objfile->objfile));
3679 continue;
3680 }
3681
3682 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3683
3684 /* Skip if already read in. */
3685 if (per_cu->v.quick->compunit_symtab)
3686 continue;
3687
3688 /* Check static vs global. */
3689 if (attrs_valid)
3690 {
3691 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3692
3693 if (iter->block_index.has_value ())
3694 {
3695 bool want_static = *iter->block_index == STATIC_BLOCK;
3696
3697 if (is_static != want_static)
3698 continue;
3699 }
3700
3701 /* Work around gold/15646. */
3702 if (!is_static && iter->global_seen)
3703 continue;
3704 if (!is_static)
3705 iter->global_seen = 1;
3706 }
3707
3708 /* Only check the symbol's kind if it has one. */
3709 if (attrs_valid)
3710 {
3711 switch (iter->domain)
3712 {
3713 case VAR_DOMAIN:
3714 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3715 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3716 /* Some types are also in VAR_DOMAIN. */
3717 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3718 continue;
3719 break;
3720 case STRUCT_DOMAIN:
3721 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3722 continue;
3723 break;
3724 case LABEL_DOMAIN:
3725 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3726 continue;
3727 break;
3728 case MODULE_DOMAIN:
3729 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3730 continue;
3731 break;
3732 default:
3733 break;
3734 }
3735 }
3736
3737 ++iter->next;
3738 return per_cu;
3739 }
3740
3741 return NULL;
3742 }
3743
3744 static struct compunit_symtab *
3745 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3746 const char *name, domain_enum domain)
3747 {
3748 struct compunit_symtab *stab_best = NULL;
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
3751
3752 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3753
3754 struct dw2_symtab_iterator iter;
3755 struct dwarf2_per_cu_data *per_cu;
3756
3757 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3758
3759 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3760 {
3761 struct symbol *sym, *with_opaque = NULL;
3762 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3763 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3764 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3765
3766 sym = block_find_symbol (block, name, domain,
3767 block_find_non_opaque_type_preferred,
3768 &with_opaque);
3769
3770 /* Some caution must be observed with overloaded functions
3771 and methods, since the index will not contain any overload
3772 information (but NAME might contain it). */
3773
3774 if (sym != NULL
3775 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3776 return stab;
3777 if (with_opaque != NULL
3778 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3779 stab_best = stab;
3780
3781 /* Keep looking through other CUs. */
3782 }
3783
3784 return stab_best;
3785 }
3786
3787 static void
3788 dw2_print_stats (struct objfile *objfile)
3789 {
3790 struct dwarf2_per_objfile *dwarf2_per_objfile
3791 = get_dwarf2_per_objfile (objfile);
3792 int total = (dwarf2_per_objfile->all_comp_units.size ()
3793 + dwarf2_per_objfile->all_type_units.size ());
3794 int count = 0;
3795
3796 for (int i = 0; i < total; ++i)
3797 {
3798 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3799
3800 if (!per_cu->v.quick->compunit_symtab)
3801 ++count;
3802 }
3803 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3804 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3805 }
3806
3807 /* This dumps minimal information about the index.
3808 It is called via "mt print objfiles".
3809 One use is to verify .gdb_index has been loaded by the
3810 gdb.dwarf2/gdb-index.exp testcase. */
3811
3812 static void
3813 dw2_dump (struct objfile *objfile)
3814 {
3815 struct dwarf2_per_objfile *dwarf2_per_objfile
3816 = get_dwarf2_per_objfile (objfile);
3817
3818 gdb_assert (dwarf2_per_objfile->using_index);
3819 printf_filtered (".gdb_index:");
3820 if (dwarf2_per_objfile->index_table != NULL)
3821 {
3822 printf_filtered (" version %d\n",
3823 dwarf2_per_objfile->index_table->version);
3824 }
3825 else
3826 printf_filtered (" faked for \"readnow\"\n");
3827 printf_filtered ("\n");
3828 }
3829
3830 static void
3831 dw2_expand_symtabs_for_function (struct objfile *objfile,
3832 const char *func_name)
3833 {
3834 struct dwarf2_per_objfile *dwarf2_per_objfile
3835 = get_dwarf2_per_objfile (objfile);
3836
3837 struct dw2_symtab_iterator iter;
3838 struct dwarf2_per_cu_data *per_cu;
3839
3840 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3841
3842 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3843 dw2_instantiate_symtab (per_cu, false);
3844
3845 }
3846
3847 static void
3848 dw2_expand_all_symtabs (struct objfile *objfile)
3849 {
3850 struct dwarf2_per_objfile *dwarf2_per_objfile
3851 = get_dwarf2_per_objfile (objfile);
3852 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3853 + dwarf2_per_objfile->all_type_units.size ());
3854
3855 for (int i = 0; i < total_units; ++i)
3856 {
3857 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3858
3859 /* We don't want to directly expand a partial CU, because if we
3860 read it with the wrong language, then assertion failures can
3861 be triggered later on. See PR symtab/23010. So, tell
3862 dw2_instantiate_symtab to skip partial CUs -- any important
3863 partial CU will be read via DW_TAG_imported_unit anyway. */
3864 dw2_instantiate_symtab (per_cu, true);
3865 }
3866 }
3867
3868 static void
3869 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3870 const char *fullname)
3871 {
3872 struct dwarf2_per_objfile *dwarf2_per_objfile
3873 = get_dwarf2_per_objfile (objfile);
3874
3875 /* We don't need to consider type units here.
3876 This is only called for examining code, e.g. expand_line_sal.
3877 There can be an order of magnitude (or more) more type units
3878 than comp units, and we avoid them if we can. */
3879
3880 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3881 {
3882 /* We only need to look at symtabs not already expanded. */
3883 if (per_cu->v.quick->compunit_symtab)
3884 continue;
3885
3886 quick_file_names *file_data = dw2_get_file_names (per_cu);
3887 if (file_data == NULL)
3888 continue;
3889
3890 for (int j = 0; j < file_data->num_file_names; ++j)
3891 {
3892 const char *this_fullname = file_data->file_names[j];
3893
3894 if (filename_cmp (this_fullname, fullname) == 0)
3895 {
3896 dw2_instantiate_symtab (per_cu, false);
3897 break;
3898 }
3899 }
3900 }
3901 }
3902
3903 static void
3904 dw2_map_matching_symbols
3905 (struct objfile *objfile,
3906 const lookup_name_info &name, domain_enum domain,
3907 int global,
3908 gdb::function_view<symbol_found_callback_ftype> callback,
3909 symbol_compare_ftype *ordered_compare)
3910 {
3911 /* Currently unimplemented; used for Ada. The function can be called if the
3912 current language is Ada for a non-Ada objfile using GNU index. As Ada
3913 does not look for non-Ada symbols this function should just return. */
3914 }
3915
3916 /* Starting from a search name, return the string that finds the upper
3917 bound of all strings that start with SEARCH_NAME in a sorted name
3918 list. Returns the empty string to indicate that the upper bound is
3919 the end of the list. */
3920
3921 static std::string
3922 make_sort_after_prefix_name (const char *search_name)
3923 {
3924 /* When looking to complete "func", we find the upper bound of all
3925 symbols that start with "func" by looking for where we'd insert
3926 the closest string that would follow "func" in lexicographical
3927 order. Usually, that's "func"-with-last-character-incremented,
3928 i.e. "fund". Mind non-ASCII characters, though. Usually those
3929 will be UTF-8 multi-byte sequences, but we can't be certain.
3930 Especially mind the 0xff character, which is a valid character in
3931 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3932 rule out compilers allowing it in identifiers. Note that
3933 conveniently, strcmp/strcasecmp are specified to compare
3934 characters interpreted as unsigned char. So what we do is treat
3935 the whole string as a base 256 number composed of a sequence of
3936 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3937 to 0, and carries 1 to the following more-significant position.
3938 If the very first character in SEARCH_NAME ends up incremented
3939 and carries/overflows, then the upper bound is the end of the
3940 list. The string after the empty string is also the empty
3941 string.
3942
3943 Some examples of this operation:
3944
3945 SEARCH_NAME => "+1" RESULT
3946
3947 "abc" => "abd"
3948 "ab\xff" => "ac"
3949 "\xff" "a" "\xff" => "\xff" "b"
3950 "\xff" => ""
3951 "\xff\xff" => ""
3952 "" => ""
3953
3954 Then, with these symbols for example:
3955
3956 func
3957 func1
3958 fund
3959
3960 completing "func" looks for symbols between "func" and
3961 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3962 which finds "func" and "func1", but not "fund".
3963
3964 And with:
3965
3966 funcÿ (Latin1 'ÿ' [0xff])
3967 funcÿ1
3968 fund
3969
3970 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3971 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3972
3973 And with:
3974
3975 ÿÿ (Latin1 'ÿ' [0xff])
3976 ÿÿ1
3977
3978 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3979 the end of the list.
3980 */
3981 std::string after = search_name;
3982 while (!after.empty () && (unsigned char) after.back () == 0xff)
3983 after.pop_back ();
3984 if (!after.empty ())
3985 after.back () = (unsigned char) after.back () + 1;
3986 return after;
3987 }
3988
3989 /* See declaration. */
3990
3991 std::pair<std::vector<name_component>::const_iterator,
3992 std::vector<name_component>::const_iterator>
3993 mapped_index_base::find_name_components_bounds
3994 (const lookup_name_info &lookup_name_without_params, language lang) const
3995 {
3996 auto *name_cmp
3997 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3998
3999 const char *lang_name
4000 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4001
4002 /* Comparison function object for lower_bound that matches against a
4003 given symbol name. */
4004 auto lookup_compare_lower = [&] (const name_component &elem,
4005 const char *name)
4006 {
4007 const char *elem_qualified = this->symbol_name_at (elem.idx);
4008 const char *elem_name = elem_qualified + elem.name_offset;
4009 return name_cmp (elem_name, name) < 0;
4010 };
4011
4012 /* Comparison function object for upper_bound that matches against a
4013 given symbol name. */
4014 auto lookup_compare_upper = [&] (const char *name,
4015 const name_component &elem)
4016 {
4017 const char *elem_qualified = this->symbol_name_at (elem.idx);
4018 const char *elem_name = elem_qualified + elem.name_offset;
4019 return name_cmp (name, elem_name) < 0;
4020 };
4021
4022 auto begin = this->name_components.begin ();
4023 auto end = this->name_components.end ();
4024
4025 /* Find the lower bound. */
4026 auto lower = [&] ()
4027 {
4028 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4029 return begin;
4030 else
4031 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4032 } ();
4033
4034 /* Find the upper bound. */
4035 auto upper = [&] ()
4036 {
4037 if (lookup_name_without_params.completion_mode ())
4038 {
4039 /* In completion mode, we want UPPER to point past all
4040 symbols names that have the same prefix. I.e., with
4041 these symbols, and completing "func":
4042
4043 function << lower bound
4044 function1
4045 other_function << upper bound
4046
4047 We find the upper bound by looking for the insertion
4048 point of "func"-with-last-character-incremented,
4049 i.e. "fund". */
4050 std::string after = make_sort_after_prefix_name (lang_name);
4051 if (after.empty ())
4052 return end;
4053 return std::lower_bound (lower, end, after.c_str (),
4054 lookup_compare_lower);
4055 }
4056 else
4057 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4058 } ();
4059
4060 return {lower, upper};
4061 }
4062
4063 /* See declaration. */
4064
4065 void
4066 mapped_index_base::build_name_components ()
4067 {
4068 if (!this->name_components.empty ())
4069 return;
4070
4071 this->name_components_casing = case_sensitivity;
4072 auto *name_cmp
4073 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4074
4075 /* The code below only knows how to break apart components of C++
4076 symbol names (and other languages that use '::' as
4077 namespace/module separator) and Ada symbol names. */
4078 auto count = this->symbol_name_count ();
4079 for (offset_type idx = 0; idx < count; idx++)
4080 {
4081 if (this->symbol_name_slot_invalid (idx))
4082 continue;
4083
4084 const char *name = this->symbol_name_at (idx);
4085
4086 /* Add each name component to the name component table. */
4087 unsigned int previous_len = 0;
4088
4089 if (strstr (name, "::") != nullptr)
4090 {
4091 for (unsigned int current_len = cp_find_first_component (name);
4092 name[current_len] != '\0';
4093 current_len += cp_find_first_component (name + current_len))
4094 {
4095 gdb_assert (name[current_len] == ':');
4096 this->name_components.push_back ({previous_len, idx});
4097 /* Skip the '::'. */
4098 current_len += 2;
4099 previous_len = current_len;
4100 }
4101 }
4102 else
4103 {
4104 /* Handle the Ada encoded (aka mangled) form here. */
4105 for (const char *iter = strstr (name, "__");
4106 iter != nullptr;
4107 iter = strstr (iter, "__"))
4108 {
4109 this->name_components.push_back ({previous_len, idx});
4110 iter += 2;
4111 previous_len = iter - name;
4112 }
4113 }
4114
4115 this->name_components.push_back ({previous_len, idx});
4116 }
4117
4118 /* Sort name_components elements by name. */
4119 auto name_comp_compare = [&] (const name_component &left,
4120 const name_component &right)
4121 {
4122 const char *left_qualified = this->symbol_name_at (left.idx);
4123 const char *right_qualified = this->symbol_name_at (right.idx);
4124
4125 const char *left_name = left_qualified + left.name_offset;
4126 const char *right_name = right_qualified + right.name_offset;
4127
4128 return name_cmp (left_name, right_name) < 0;
4129 };
4130
4131 std::sort (this->name_components.begin (),
4132 this->name_components.end (),
4133 name_comp_compare);
4134 }
4135
4136 /* Helper for dw2_expand_symtabs_matching that works with a
4137 mapped_index_base instead of the containing objfile. This is split
4138 to a separate function in order to be able to unit test the
4139 name_components matching using a mock mapped_index_base. For each
4140 symbol name that matches, calls MATCH_CALLBACK, passing it the
4141 symbol's index in the mapped_index_base symbol table. */
4142
4143 static void
4144 dw2_expand_symtabs_matching_symbol
4145 (mapped_index_base &index,
4146 const lookup_name_info &lookup_name_in,
4147 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4148 enum search_domain kind,
4149 gdb::function_view<bool (offset_type)> match_callback)
4150 {
4151 lookup_name_info lookup_name_without_params
4152 = lookup_name_in.make_ignore_params ();
4153
4154 /* Build the symbol name component sorted vector, if we haven't
4155 yet. */
4156 index.build_name_components ();
4157
4158 /* The same symbol may appear more than once in the range though.
4159 E.g., if we're looking for symbols that complete "w", and we have
4160 a symbol named "w1::w2", we'll find the two name components for
4161 that same symbol in the range. To be sure we only call the
4162 callback once per symbol, we first collect the symbol name
4163 indexes that matched in a temporary vector and ignore
4164 duplicates. */
4165 std::vector<offset_type> matches;
4166
4167 struct name_and_matcher
4168 {
4169 symbol_name_matcher_ftype *matcher;
4170 const std::string &name;
4171
4172 bool operator== (const name_and_matcher &other) const
4173 {
4174 return matcher == other.matcher && name == other.name;
4175 }
4176 };
4177
4178 /* A vector holding all the different symbol name matchers, for all
4179 languages. */
4180 std::vector<name_and_matcher> matchers;
4181
4182 for (int i = 0; i < nr_languages; i++)
4183 {
4184 enum language lang_e = (enum language) i;
4185
4186 const language_defn *lang = language_def (lang_e);
4187 symbol_name_matcher_ftype *name_matcher
4188 = get_symbol_name_matcher (lang, lookup_name_without_params);
4189
4190 name_and_matcher key {
4191 name_matcher,
4192 lookup_name_without_params.language_lookup_name (lang_e)
4193 };
4194
4195 /* Don't insert the same comparison routine more than once.
4196 Note that we do this linear walk. This is not a problem in
4197 practice because the number of supported languages is
4198 low. */
4199 if (std::find (matchers.begin (), matchers.end (), key)
4200 != matchers.end ())
4201 continue;
4202 matchers.push_back (std::move (key));
4203
4204 auto bounds
4205 = index.find_name_components_bounds (lookup_name_without_params,
4206 lang_e);
4207
4208 /* Now for each symbol name in range, check to see if we have a name
4209 match, and if so, call the MATCH_CALLBACK callback. */
4210
4211 for (; bounds.first != bounds.second; ++bounds.first)
4212 {
4213 const char *qualified = index.symbol_name_at (bounds.first->idx);
4214
4215 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4216 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4217 continue;
4218
4219 matches.push_back (bounds.first->idx);
4220 }
4221 }
4222
4223 std::sort (matches.begin (), matches.end ());
4224
4225 /* Finally call the callback, once per match. */
4226 ULONGEST prev = -1;
4227 for (offset_type idx : matches)
4228 {
4229 if (prev != idx)
4230 {
4231 if (!match_callback (idx))
4232 break;
4233 prev = idx;
4234 }
4235 }
4236
4237 /* Above we use a type wider than idx's for 'prev', since 0 and
4238 (offset_type)-1 are both possible values. */
4239 static_assert (sizeof (prev) > sizeof (offset_type), "");
4240 }
4241
4242 #if GDB_SELF_TEST
4243
4244 namespace selftests { namespace dw2_expand_symtabs_matching {
4245
4246 /* A mock .gdb_index/.debug_names-like name index table, enough to
4247 exercise dw2_expand_symtabs_matching_symbol, which works with the
4248 mapped_index_base interface. Builds an index from the symbol list
4249 passed as parameter to the constructor. */
4250 class mock_mapped_index : public mapped_index_base
4251 {
4252 public:
4253 mock_mapped_index (gdb::array_view<const char *> symbols)
4254 : m_symbol_table (symbols)
4255 {}
4256
4257 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4258
4259 /* Return the number of names in the symbol table. */
4260 size_t symbol_name_count () const override
4261 {
4262 return m_symbol_table.size ();
4263 }
4264
4265 /* Get the name of the symbol at IDX in the symbol table. */
4266 const char *symbol_name_at (offset_type idx) const override
4267 {
4268 return m_symbol_table[idx];
4269 }
4270
4271 private:
4272 gdb::array_view<const char *> m_symbol_table;
4273 };
4274
4275 /* Convenience function that converts a NULL pointer to a "<null>"
4276 string, to pass to print routines. */
4277
4278 static const char *
4279 string_or_null (const char *str)
4280 {
4281 return str != NULL ? str : "<null>";
4282 }
4283
4284 /* Check if a lookup_name_info built from
4285 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4286 index. EXPECTED_LIST is the list of expected matches, in expected
4287 matching order. If no match expected, then an empty list is
4288 specified. Returns true on success. On failure prints a warning
4289 indicating the file:line that failed, and returns false. */
4290
4291 static bool
4292 check_match (const char *file, int line,
4293 mock_mapped_index &mock_index,
4294 const char *name, symbol_name_match_type match_type,
4295 bool completion_mode,
4296 std::initializer_list<const char *> expected_list)
4297 {
4298 lookup_name_info lookup_name (name, match_type, completion_mode);
4299
4300 bool matched = true;
4301
4302 auto mismatch = [&] (const char *expected_str,
4303 const char *got)
4304 {
4305 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4306 "expected=\"%s\", got=\"%s\"\n"),
4307 file, line,
4308 (match_type == symbol_name_match_type::FULL
4309 ? "FULL" : "WILD"),
4310 name, string_or_null (expected_str), string_or_null (got));
4311 matched = false;
4312 };
4313
4314 auto expected_it = expected_list.begin ();
4315 auto expected_end = expected_list.end ();
4316
4317 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4318 NULL, ALL_DOMAIN,
4319 [&] (offset_type idx)
4320 {
4321 const char *matched_name = mock_index.symbol_name_at (idx);
4322 const char *expected_str
4323 = expected_it == expected_end ? NULL : *expected_it++;
4324
4325 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4326 mismatch (expected_str, matched_name);
4327 return true;
4328 });
4329
4330 const char *expected_str
4331 = expected_it == expected_end ? NULL : *expected_it++;
4332 if (expected_str != NULL)
4333 mismatch (expected_str, NULL);
4334
4335 return matched;
4336 }
4337
4338 /* The symbols added to the mock mapped_index for testing (in
4339 canonical form). */
4340 static const char *test_symbols[] = {
4341 "function",
4342 "std::bar",
4343 "std::zfunction",
4344 "std::zfunction2",
4345 "w1::w2",
4346 "ns::foo<char*>",
4347 "ns::foo<int>",
4348 "ns::foo<long>",
4349 "ns2::tmpl<int>::foo2",
4350 "(anonymous namespace)::A::B::C",
4351
4352 /* These are used to check that the increment-last-char in the
4353 matching algorithm for completion doesn't match "t1_fund" when
4354 completing "t1_func". */
4355 "t1_func",
4356 "t1_func1",
4357 "t1_fund",
4358 "t1_fund1",
4359
4360 /* A UTF-8 name with multi-byte sequences to make sure that
4361 cp-name-parser understands this as a single identifier ("função"
4362 is "function" in PT). */
4363 u8"u8função",
4364
4365 /* \377 (0xff) is Latin1 'ÿ'. */
4366 "yfunc\377",
4367
4368 /* \377 (0xff) is Latin1 'ÿ'. */
4369 "\377",
4370 "\377\377123",
4371
4372 /* A name with all sorts of complications. Starts with "z" to make
4373 it easier for the completion tests below. */
4374 #define Z_SYM_NAME \
4375 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4376 "::tuple<(anonymous namespace)::ui*, " \
4377 "std::default_delete<(anonymous namespace)::ui>, void>"
4378
4379 Z_SYM_NAME
4380 };
4381
4382 /* Returns true if the mapped_index_base::find_name_component_bounds
4383 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4384 in completion mode. */
4385
4386 static bool
4387 check_find_bounds_finds (mapped_index_base &index,
4388 const char *search_name,
4389 gdb::array_view<const char *> expected_syms)
4390 {
4391 lookup_name_info lookup_name (search_name,
4392 symbol_name_match_type::FULL, true);
4393
4394 auto bounds = index.find_name_components_bounds (lookup_name,
4395 language_cplus);
4396
4397 size_t distance = std::distance (bounds.first, bounds.second);
4398 if (distance != expected_syms.size ())
4399 return false;
4400
4401 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4402 {
4403 auto nc_elem = bounds.first + exp_elem;
4404 const char *qualified = index.symbol_name_at (nc_elem->idx);
4405 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4406 return false;
4407 }
4408
4409 return true;
4410 }
4411
4412 /* Test the lower-level mapped_index::find_name_component_bounds
4413 method. */
4414
4415 static void
4416 test_mapped_index_find_name_component_bounds ()
4417 {
4418 mock_mapped_index mock_index (test_symbols);
4419
4420 mock_index.build_name_components ();
4421
4422 /* Test the lower-level mapped_index::find_name_component_bounds
4423 method in completion mode. */
4424 {
4425 static const char *expected_syms[] = {
4426 "t1_func",
4427 "t1_func1",
4428 };
4429
4430 SELF_CHECK (check_find_bounds_finds (mock_index,
4431 "t1_func", expected_syms));
4432 }
4433
4434 /* Check that the increment-last-char in the name matching algorithm
4435 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4436 {
4437 static const char *expected_syms1[] = {
4438 "\377",
4439 "\377\377123",
4440 };
4441 SELF_CHECK (check_find_bounds_finds (mock_index,
4442 "\377", expected_syms1));
4443
4444 static const char *expected_syms2[] = {
4445 "\377\377123",
4446 };
4447 SELF_CHECK (check_find_bounds_finds (mock_index,
4448 "\377\377", expected_syms2));
4449 }
4450 }
4451
4452 /* Test dw2_expand_symtabs_matching_symbol. */
4453
4454 static void
4455 test_dw2_expand_symtabs_matching_symbol ()
4456 {
4457 mock_mapped_index mock_index (test_symbols);
4458
4459 /* We let all tests run until the end even if some fails, for debug
4460 convenience. */
4461 bool any_mismatch = false;
4462
4463 /* Create the expected symbols list (an initializer_list). Needed
4464 because lists have commas, and we need to pass them to CHECK,
4465 which is a macro. */
4466 #define EXPECT(...) { __VA_ARGS__ }
4467
4468 /* Wrapper for check_match that passes down the current
4469 __FILE__/__LINE__. */
4470 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4471 any_mismatch |= !check_match (__FILE__, __LINE__, \
4472 mock_index, \
4473 NAME, MATCH_TYPE, COMPLETION_MODE, \
4474 EXPECTED_LIST)
4475
4476 /* Identity checks. */
4477 for (const char *sym : test_symbols)
4478 {
4479 /* Should be able to match all existing symbols. */
4480 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4481 EXPECT (sym));
4482
4483 /* Should be able to match all existing symbols with
4484 parameters. */
4485 std::string with_params = std::string (sym) + "(int)";
4486 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4487 EXPECT (sym));
4488
4489 /* Should be able to match all existing symbols with
4490 parameters and qualifiers. */
4491 with_params = std::string (sym) + " ( int ) const";
4492 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4493 EXPECT (sym));
4494
4495 /* This should really find sym, but cp-name-parser.y doesn't
4496 know about lvalue/rvalue qualifiers yet. */
4497 with_params = std::string (sym) + " ( int ) &&";
4498 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4499 {});
4500 }
4501
4502 /* Check that the name matching algorithm for completion doesn't get
4503 confused with Latin1 'ÿ' / 0xff. */
4504 {
4505 static const char str[] = "\377";
4506 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4507 EXPECT ("\377", "\377\377123"));
4508 }
4509
4510 /* Check that the increment-last-char in the matching algorithm for
4511 completion doesn't match "t1_fund" when completing "t1_func". */
4512 {
4513 static const char str[] = "t1_func";
4514 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4515 EXPECT ("t1_func", "t1_func1"));
4516 }
4517
4518 /* Check that completion mode works at each prefix of the expected
4519 symbol name. */
4520 {
4521 static const char str[] = "function(int)";
4522 size_t len = strlen (str);
4523 std::string lookup;
4524
4525 for (size_t i = 1; i < len; i++)
4526 {
4527 lookup.assign (str, i);
4528 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4529 EXPECT ("function"));
4530 }
4531 }
4532
4533 /* While "w" is a prefix of both components, the match function
4534 should still only be called once. */
4535 {
4536 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4537 EXPECT ("w1::w2"));
4538 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4539 EXPECT ("w1::w2"));
4540 }
4541
4542 /* Same, with a "complicated" symbol. */
4543 {
4544 static const char str[] = Z_SYM_NAME;
4545 size_t len = strlen (str);
4546 std::string lookup;
4547
4548 for (size_t i = 1; i < len; i++)
4549 {
4550 lookup.assign (str, i);
4551 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4552 EXPECT (Z_SYM_NAME));
4553 }
4554 }
4555
4556 /* In FULL mode, an incomplete symbol doesn't match. */
4557 {
4558 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4559 {});
4560 }
4561
4562 /* A complete symbol with parameters matches any overload, since the
4563 index has no overload info. */
4564 {
4565 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4566 EXPECT ("std::zfunction", "std::zfunction2"));
4567 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4568 EXPECT ("std::zfunction", "std::zfunction2"));
4569 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4570 EXPECT ("std::zfunction", "std::zfunction2"));
4571 }
4572
4573 /* Check that whitespace is ignored appropriately. A symbol with a
4574 template argument list. */
4575 {
4576 static const char expected[] = "ns::foo<int>";
4577 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4578 EXPECT (expected));
4579 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4580 EXPECT (expected));
4581 }
4582
4583 /* Check that whitespace is ignored appropriately. A symbol with a
4584 template argument list that includes a pointer. */
4585 {
4586 static const char expected[] = "ns::foo<char*>";
4587 /* Try both completion and non-completion modes. */
4588 static const bool completion_mode[2] = {false, true};
4589 for (size_t i = 0; i < 2; i++)
4590 {
4591 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4592 completion_mode[i], EXPECT (expected));
4593 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4594 completion_mode[i], EXPECT (expected));
4595
4596 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4597 completion_mode[i], EXPECT (expected));
4598 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4599 completion_mode[i], EXPECT (expected));
4600 }
4601 }
4602
4603 {
4604 /* Check method qualifiers are ignored. */
4605 static const char expected[] = "ns::foo<char*>";
4606 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4607 symbol_name_match_type::FULL, true, EXPECT (expected));
4608 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4609 symbol_name_match_type::FULL, true, EXPECT (expected));
4610 CHECK_MATCH ("foo < char * > ( int ) const",
4611 symbol_name_match_type::WILD, true, EXPECT (expected));
4612 CHECK_MATCH ("foo < char * > ( int ) &&",
4613 symbol_name_match_type::WILD, true, EXPECT (expected));
4614 }
4615
4616 /* Test lookup names that don't match anything. */
4617 {
4618 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4619 {});
4620
4621 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4622 {});
4623 }
4624
4625 /* Some wild matching tests, exercising "(anonymous namespace)",
4626 which should not be confused with a parameter list. */
4627 {
4628 static const char *syms[] = {
4629 "A::B::C",
4630 "B::C",
4631 "C",
4632 "A :: B :: C ( int )",
4633 "B :: C ( int )",
4634 "C ( int )",
4635 };
4636
4637 for (const char *s : syms)
4638 {
4639 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4640 EXPECT ("(anonymous namespace)::A::B::C"));
4641 }
4642 }
4643
4644 {
4645 static const char expected[] = "ns2::tmpl<int>::foo2";
4646 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4647 EXPECT (expected));
4648 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4649 EXPECT (expected));
4650 }
4651
4652 SELF_CHECK (!any_mismatch);
4653
4654 #undef EXPECT
4655 #undef CHECK_MATCH
4656 }
4657
4658 static void
4659 run_test ()
4660 {
4661 test_mapped_index_find_name_component_bounds ();
4662 test_dw2_expand_symtabs_matching_symbol ();
4663 }
4664
4665 }} // namespace selftests::dw2_expand_symtabs_matching
4666
4667 #endif /* GDB_SELF_TEST */
4668
4669 /* If FILE_MATCHER is NULL or if PER_CU has
4670 dwarf2_per_cu_quick_data::MARK set (see
4671 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4672 EXPANSION_NOTIFY on it. */
4673
4674 static void
4675 dw2_expand_symtabs_matching_one
4676 (struct dwarf2_per_cu_data *per_cu,
4677 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4678 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4679 {
4680 if (file_matcher == NULL || per_cu->v.quick->mark)
4681 {
4682 bool symtab_was_null
4683 = (per_cu->v.quick->compunit_symtab == NULL);
4684
4685 dw2_instantiate_symtab (per_cu, false);
4686
4687 if (expansion_notify != NULL
4688 && symtab_was_null
4689 && per_cu->v.quick->compunit_symtab != NULL)
4690 expansion_notify (per_cu->v.quick->compunit_symtab);
4691 }
4692 }
4693
4694 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4695 matched, to expand corresponding CUs that were marked. IDX is the
4696 index of the symbol name that matched. */
4697
4698 static void
4699 dw2_expand_marked_cus
4700 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4701 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4702 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4703 search_domain kind)
4704 {
4705 offset_type *vec, vec_len, vec_idx;
4706 bool global_seen = false;
4707 mapped_index &index = *dwarf2_per_objfile->index_table;
4708
4709 vec = (offset_type *) (index.constant_pool
4710 + MAYBE_SWAP (index.symbol_table[idx].vec));
4711 vec_len = MAYBE_SWAP (vec[0]);
4712 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4713 {
4714 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4715 /* This value is only valid for index versions >= 7. */
4716 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4717 gdb_index_symbol_kind symbol_kind =
4718 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4719 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4720 /* Only check the symbol attributes if they're present.
4721 Indices prior to version 7 don't record them,
4722 and indices >= 7 may elide them for certain symbols
4723 (gold does this). */
4724 int attrs_valid =
4725 (index.version >= 7
4726 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4727
4728 /* Work around gold/15646. */
4729 if (attrs_valid)
4730 {
4731 if (!is_static && global_seen)
4732 continue;
4733 if (!is_static)
4734 global_seen = true;
4735 }
4736
4737 /* Only check the symbol's kind if it has one. */
4738 if (attrs_valid)
4739 {
4740 switch (kind)
4741 {
4742 case VARIABLES_DOMAIN:
4743 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4744 continue;
4745 break;
4746 case FUNCTIONS_DOMAIN:
4747 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4748 continue;
4749 break;
4750 case TYPES_DOMAIN:
4751 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4752 continue;
4753 break;
4754 case MODULES_DOMAIN:
4755 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4756 continue;
4757 break;
4758 default:
4759 break;
4760 }
4761 }
4762
4763 /* Don't crash on bad data. */
4764 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4765 + dwarf2_per_objfile->all_type_units.size ()))
4766 {
4767 complaint (_(".gdb_index entry has bad CU index"
4768 " [in module %s]"),
4769 objfile_name (dwarf2_per_objfile->objfile));
4770 continue;
4771 }
4772
4773 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4774 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4775 expansion_notify);
4776 }
4777 }
4778
4779 /* If FILE_MATCHER is non-NULL, set all the
4780 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4781 that match FILE_MATCHER. */
4782
4783 static void
4784 dw_expand_symtabs_matching_file_matcher
4785 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4786 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4787 {
4788 if (file_matcher == NULL)
4789 return;
4790
4791 objfile *const objfile = dwarf2_per_objfile->objfile;
4792
4793 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4794 htab_eq_pointer,
4795 NULL, xcalloc, xfree));
4796 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4797 htab_eq_pointer,
4798 NULL, xcalloc, xfree));
4799
4800 /* The rule is CUs specify all the files, including those used by
4801 any TU, so there's no need to scan TUs here. */
4802
4803 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4804 {
4805 QUIT;
4806
4807 per_cu->v.quick->mark = 0;
4808
4809 /* We only need to look at symtabs not already expanded. */
4810 if (per_cu->v.quick->compunit_symtab)
4811 continue;
4812
4813 quick_file_names *file_data = dw2_get_file_names (per_cu);
4814 if (file_data == NULL)
4815 continue;
4816
4817 if (htab_find (visited_not_found.get (), file_data) != NULL)
4818 continue;
4819 else if (htab_find (visited_found.get (), file_data) != NULL)
4820 {
4821 per_cu->v.quick->mark = 1;
4822 continue;
4823 }
4824
4825 for (int j = 0; j < file_data->num_file_names; ++j)
4826 {
4827 const char *this_real_name;
4828
4829 if (file_matcher (file_data->file_names[j], false))
4830 {
4831 per_cu->v.quick->mark = 1;
4832 break;
4833 }
4834
4835 /* Before we invoke realpath, which can get expensive when many
4836 files are involved, do a quick comparison of the basenames. */
4837 if (!basenames_may_differ
4838 && !file_matcher (lbasename (file_data->file_names[j]),
4839 true))
4840 continue;
4841
4842 this_real_name = dw2_get_real_path (objfile, file_data, j);
4843 if (file_matcher (this_real_name, false))
4844 {
4845 per_cu->v.quick->mark = 1;
4846 break;
4847 }
4848 }
4849
4850 void **slot = htab_find_slot (per_cu->v.quick->mark
4851 ? visited_found.get ()
4852 : visited_not_found.get (),
4853 file_data, INSERT);
4854 *slot = file_data;
4855 }
4856 }
4857
4858 static void
4859 dw2_expand_symtabs_matching
4860 (struct objfile *objfile,
4861 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4862 const lookup_name_info &lookup_name,
4863 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4864 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4865 enum search_domain kind)
4866 {
4867 struct dwarf2_per_objfile *dwarf2_per_objfile
4868 = get_dwarf2_per_objfile (objfile);
4869
4870 /* index_table is NULL if OBJF_READNOW. */
4871 if (!dwarf2_per_objfile->index_table)
4872 return;
4873
4874 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4875
4876 mapped_index &index = *dwarf2_per_objfile->index_table;
4877
4878 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4879 symbol_matcher,
4880 kind, [&] (offset_type idx)
4881 {
4882 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4883 expansion_notify, kind);
4884 return true;
4885 });
4886 }
4887
4888 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4889 symtab. */
4890
4891 static struct compunit_symtab *
4892 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4893 CORE_ADDR pc)
4894 {
4895 int i;
4896
4897 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4898 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4899 return cust;
4900
4901 if (cust->includes == NULL)
4902 return NULL;
4903
4904 for (i = 0; cust->includes[i]; ++i)
4905 {
4906 struct compunit_symtab *s = cust->includes[i];
4907
4908 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4909 if (s != NULL)
4910 return s;
4911 }
4912
4913 return NULL;
4914 }
4915
4916 static struct compunit_symtab *
4917 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4918 struct bound_minimal_symbol msymbol,
4919 CORE_ADDR pc,
4920 struct obj_section *section,
4921 int warn_if_readin)
4922 {
4923 struct dwarf2_per_cu_data *data;
4924 struct compunit_symtab *result;
4925
4926 if (!objfile->partial_symtabs->psymtabs_addrmap)
4927 return NULL;
4928
4929 CORE_ADDR baseaddr = objfile->text_section_offset ();
4930 data = (struct dwarf2_per_cu_data *) addrmap_find
4931 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4932 if (!data)
4933 return NULL;
4934
4935 if (warn_if_readin && data->v.quick->compunit_symtab)
4936 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4937 paddress (get_objfile_arch (objfile), pc));
4938
4939 result
4940 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4941 false),
4942 pc);
4943 gdb_assert (result != NULL);
4944 return result;
4945 }
4946
4947 static void
4948 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4949 void *data, int need_fullname)
4950 {
4951 struct dwarf2_per_objfile *dwarf2_per_objfile
4952 = get_dwarf2_per_objfile (objfile);
4953
4954 if (!dwarf2_per_objfile->filenames_cache)
4955 {
4956 dwarf2_per_objfile->filenames_cache.emplace ();
4957
4958 htab_up visited (htab_create_alloc (10,
4959 htab_hash_pointer, htab_eq_pointer,
4960 NULL, xcalloc, xfree));
4961
4962 /* The rule is CUs specify all the files, including those used
4963 by any TU, so there's no need to scan TUs here. We can
4964 ignore file names coming from already-expanded CUs. */
4965
4966 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4967 {
4968 if (per_cu->v.quick->compunit_symtab)
4969 {
4970 void **slot = htab_find_slot (visited.get (),
4971 per_cu->v.quick->file_names,
4972 INSERT);
4973
4974 *slot = per_cu->v.quick->file_names;
4975 }
4976 }
4977
4978 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4979 {
4980 /* We only need to look at symtabs not already expanded. */
4981 if (per_cu->v.quick->compunit_symtab)
4982 continue;
4983
4984 quick_file_names *file_data = dw2_get_file_names (per_cu);
4985 if (file_data == NULL)
4986 continue;
4987
4988 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4989 if (*slot)
4990 {
4991 /* Already visited. */
4992 continue;
4993 }
4994 *slot = file_data;
4995
4996 for (int j = 0; j < file_data->num_file_names; ++j)
4997 {
4998 const char *filename = file_data->file_names[j];
4999 dwarf2_per_objfile->filenames_cache->seen (filename);
5000 }
5001 }
5002 }
5003
5004 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5005 {
5006 gdb::unique_xmalloc_ptr<char> this_real_name;
5007
5008 if (need_fullname)
5009 this_real_name = gdb_realpath (filename);
5010 (*fun) (filename, this_real_name.get (), data);
5011 });
5012 }
5013
5014 static int
5015 dw2_has_symbols (struct objfile *objfile)
5016 {
5017 return 1;
5018 }
5019
5020 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5021 {
5022 dw2_has_symbols,
5023 dw2_find_last_source_symtab,
5024 dw2_forget_cached_source_info,
5025 dw2_map_symtabs_matching_filename,
5026 dw2_lookup_symbol,
5027 dw2_print_stats,
5028 dw2_dump,
5029 dw2_expand_symtabs_for_function,
5030 dw2_expand_all_symtabs,
5031 dw2_expand_symtabs_with_fullname,
5032 dw2_map_matching_symbols,
5033 dw2_expand_symtabs_matching,
5034 dw2_find_pc_sect_compunit_symtab,
5035 NULL,
5036 dw2_map_symbol_filenames
5037 };
5038
5039 /* DWARF-5 debug_names reader. */
5040
5041 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5042 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5043
5044 /* A helper function that reads the .debug_names section in SECTION
5045 and fills in MAP. FILENAME is the name of the file containing the
5046 section; it is used for error reporting.
5047
5048 Returns true if all went well, false otherwise. */
5049
5050 static bool
5051 read_debug_names_from_section (struct objfile *objfile,
5052 const char *filename,
5053 struct dwarf2_section_info *section,
5054 mapped_debug_names &map)
5055 {
5056 if (section->empty ())
5057 return false;
5058
5059 /* Older elfutils strip versions could keep the section in the main
5060 executable while splitting it for the separate debug info file. */
5061 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5062 return false;
5063
5064 section->read (objfile);
5065
5066 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5067
5068 const gdb_byte *addr = section->buffer;
5069
5070 bfd *const abfd = section->get_bfd_owner ();
5071
5072 unsigned int bytes_read;
5073 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5074 addr += bytes_read;
5075
5076 map.dwarf5_is_dwarf64 = bytes_read != 4;
5077 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5078 if (bytes_read + length != section->size)
5079 {
5080 /* There may be multiple per-CU indices. */
5081 warning (_("Section .debug_names in %s length %s does not match "
5082 "section length %s, ignoring .debug_names."),
5083 filename, plongest (bytes_read + length),
5084 pulongest (section->size));
5085 return false;
5086 }
5087
5088 /* The version number. */
5089 uint16_t version = read_2_bytes (abfd, addr);
5090 addr += 2;
5091 if (version != 5)
5092 {
5093 warning (_("Section .debug_names in %s has unsupported version %d, "
5094 "ignoring .debug_names."),
5095 filename, version);
5096 return false;
5097 }
5098
5099 /* Padding. */
5100 uint16_t padding = read_2_bytes (abfd, addr);
5101 addr += 2;
5102 if (padding != 0)
5103 {
5104 warning (_("Section .debug_names in %s has unsupported padding %d, "
5105 "ignoring .debug_names."),
5106 filename, padding);
5107 return false;
5108 }
5109
5110 /* comp_unit_count - The number of CUs in the CU list. */
5111 map.cu_count = read_4_bytes (abfd, addr);
5112 addr += 4;
5113
5114 /* local_type_unit_count - The number of TUs in the local TU
5115 list. */
5116 map.tu_count = read_4_bytes (abfd, addr);
5117 addr += 4;
5118
5119 /* foreign_type_unit_count - The number of TUs in the foreign TU
5120 list. */
5121 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5122 addr += 4;
5123 if (foreign_tu_count != 0)
5124 {
5125 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5126 "ignoring .debug_names."),
5127 filename, static_cast<unsigned long> (foreign_tu_count));
5128 return false;
5129 }
5130
5131 /* bucket_count - The number of hash buckets in the hash lookup
5132 table. */
5133 map.bucket_count = read_4_bytes (abfd, addr);
5134 addr += 4;
5135
5136 /* name_count - The number of unique names in the index. */
5137 map.name_count = read_4_bytes (abfd, addr);
5138 addr += 4;
5139
5140 /* abbrev_table_size - The size in bytes of the abbreviations
5141 table. */
5142 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5143 addr += 4;
5144
5145 /* augmentation_string_size - The size in bytes of the augmentation
5146 string. This value is rounded up to a multiple of 4. */
5147 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5148 addr += 4;
5149 map.augmentation_is_gdb = ((augmentation_string_size
5150 == sizeof (dwarf5_augmentation))
5151 && memcmp (addr, dwarf5_augmentation,
5152 sizeof (dwarf5_augmentation)) == 0);
5153 augmentation_string_size += (-augmentation_string_size) & 3;
5154 addr += augmentation_string_size;
5155
5156 /* List of CUs */
5157 map.cu_table_reordered = addr;
5158 addr += map.cu_count * map.offset_size;
5159
5160 /* List of Local TUs */
5161 map.tu_table_reordered = addr;
5162 addr += map.tu_count * map.offset_size;
5163
5164 /* Hash Lookup Table */
5165 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5166 addr += map.bucket_count * 4;
5167 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5168 addr += map.name_count * 4;
5169
5170 /* Name Table */
5171 map.name_table_string_offs_reordered = addr;
5172 addr += map.name_count * map.offset_size;
5173 map.name_table_entry_offs_reordered = addr;
5174 addr += map.name_count * map.offset_size;
5175
5176 const gdb_byte *abbrev_table_start = addr;
5177 for (;;)
5178 {
5179 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5180 addr += bytes_read;
5181 if (index_num == 0)
5182 break;
5183
5184 const auto insertpair
5185 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5186 if (!insertpair.second)
5187 {
5188 warning (_("Section .debug_names in %s has duplicate index %s, "
5189 "ignoring .debug_names."),
5190 filename, pulongest (index_num));
5191 return false;
5192 }
5193 mapped_debug_names::index_val &indexval = insertpair.first->second;
5194 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5195 addr += bytes_read;
5196
5197 for (;;)
5198 {
5199 mapped_debug_names::index_val::attr attr;
5200 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5201 addr += bytes_read;
5202 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5203 addr += bytes_read;
5204 if (attr.form == DW_FORM_implicit_const)
5205 {
5206 attr.implicit_const = read_signed_leb128 (abfd, addr,
5207 &bytes_read);
5208 addr += bytes_read;
5209 }
5210 if (attr.dw_idx == 0 && attr.form == 0)
5211 break;
5212 indexval.attr_vec.push_back (std::move (attr));
5213 }
5214 }
5215 if (addr != abbrev_table_start + abbrev_table_size)
5216 {
5217 warning (_("Section .debug_names in %s has abbreviation_table "
5218 "of size %s vs. written as %u, ignoring .debug_names."),
5219 filename, plongest (addr - abbrev_table_start),
5220 abbrev_table_size);
5221 return false;
5222 }
5223 map.entry_pool = addr;
5224
5225 return true;
5226 }
5227
5228 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5229 list. */
5230
5231 static void
5232 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5233 const mapped_debug_names &map,
5234 dwarf2_section_info &section,
5235 bool is_dwz)
5236 {
5237 sect_offset sect_off_prev;
5238 for (uint32_t i = 0; i <= map.cu_count; ++i)
5239 {
5240 sect_offset sect_off_next;
5241 if (i < map.cu_count)
5242 {
5243 sect_off_next
5244 = (sect_offset) (extract_unsigned_integer
5245 (map.cu_table_reordered + i * map.offset_size,
5246 map.offset_size,
5247 map.dwarf5_byte_order));
5248 }
5249 else
5250 sect_off_next = (sect_offset) section.size;
5251 if (i >= 1)
5252 {
5253 const ULONGEST length = sect_off_next - sect_off_prev;
5254 dwarf2_per_cu_data *per_cu
5255 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5256 sect_off_prev, length);
5257 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5258 }
5259 sect_off_prev = sect_off_next;
5260 }
5261 }
5262
5263 /* Read the CU list from the mapped index, and use it to create all
5264 the CU objects for this dwarf2_per_objfile. */
5265
5266 static void
5267 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5268 const mapped_debug_names &map,
5269 const mapped_debug_names &dwz_map)
5270 {
5271 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5272 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5273
5274 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5275 dwarf2_per_objfile->info,
5276 false /* is_dwz */);
5277
5278 if (dwz_map.cu_count == 0)
5279 return;
5280
5281 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5282 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5283 true /* is_dwz */);
5284 }
5285
5286 /* Read .debug_names. If everything went ok, initialize the "quick"
5287 elements of all the CUs and return true. Otherwise, return false. */
5288
5289 static bool
5290 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5291 {
5292 std::unique_ptr<mapped_debug_names> map
5293 (new mapped_debug_names (dwarf2_per_objfile));
5294 mapped_debug_names dwz_map (dwarf2_per_objfile);
5295 struct objfile *objfile = dwarf2_per_objfile->objfile;
5296
5297 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5298 &dwarf2_per_objfile->debug_names,
5299 *map))
5300 return false;
5301
5302 /* Don't use the index if it's empty. */
5303 if (map->name_count == 0)
5304 return false;
5305
5306 /* If there is a .dwz file, read it so we can get its CU list as
5307 well. */
5308 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5309 if (dwz != NULL)
5310 {
5311 if (!read_debug_names_from_section (objfile,
5312 bfd_get_filename (dwz->dwz_bfd.get ()),
5313 &dwz->debug_names, dwz_map))
5314 {
5315 warning (_("could not read '.debug_names' section from %s; skipping"),
5316 bfd_get_filename (dwz->dwz_bfd.get ()));
5317 return false;
5318 }
5319 }
5320
5321 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5322
5323 if (map->tu_count != 0)
5324 {
5325 /* We can only handle a single .debug_types when we have an
5326 index. */
5327 if (dwarf2_per_objfile->types.size () != 1)
5328 return false;
5329
5330 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5331
5332 create_signatured_type_table_from_debug_names
5333 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5334 }
5335
5336 create_addrmap_from_aranges (dwarf2_per_objfile,
5337 &dwarf2_per_objfile->debug_aranges);
5338
5339 dwarf2_per_objfile->debug_names_table = std::move (map);
5340 dwarf2_per_objfile->using_index = 1;
5341 dwarf2_per_objfile->quick_file_names_table =
5342 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5343
5344 return true;
5345 }
5346
5347 /* Type used to manage iterating over all CUs looking for a symbol for
5348 .debug_names. */
5349
5350 class dw2_debug_names_iterator
5351 {
5352 public:
5353 dw2_debug_names_iterator (const mapped_debug_names &map,
5354 gdb::optional<block_enum> block_index,
5355 domain_enum domain,
5356 const char *name)
5357 : m_map (map), m_block_index (block_index), m_domain (domain),
5358 m_addr (find_vec_in_debug_names (map, name))
5359 {}
5360
5361 dw2_debug_names_iterator (const mapped_debug_names &map,
5362 search_domain search, uint32_t namei)
5363 : m_map (map),
5364 m_search (search),
5365 m_addr (find_vec_in_debug_names (map, namei))
5366 {}
5367
5368 dw2_debug_names_iterator (const mapped_debug_names &map,
5369 block_enum block_index, domain_enum domain,
5370 uint32_t namei)
5371 : m_map (map), m_block_index (block_index), m_domain (domain),
5372 m_addr (find_vec_in_debug_names (map, namei))
5373 {}
5374
5375 /* Return the next matching CU or NULL if there are no more. */
5376 dwarf2_per_cu_data *next ();
5377
5378 private:
5379 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5380 const char *name);
5381 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5382 uint32_t namei);
5383
5384 /* The internalized form of .debug_names. */
5385 const mapped_debug_names &m_map;
5386
5387 /* If set, only look for symbols that match that block. Valid values are
5388 GLOBAL_BLOCK and STATIC_BLOCK. */
5389 const gdb::optional<block_enum> m_block_index;
5390
5391 /* The kind of symbol we're looking for. */
5392 const domain_enum m_domain = UNDEF_DOMAIN;
5393 const search_domain m_search = ALL_DOMAIN;
5394
5395 /* The list of CUs from the index entry of the symbol, or NULL if
5396 not found. */
5397 const gdb_byte *m_addr;
5398 };
5399
5400 const char *
5401 mapped_debug_names::namei_to_name (uint32_t namei) const
5402 {
5403 const ULONGEST namei_string_offs
5404 = extract_unsigned_integer ((name_table_string_offs_reordered
5405 + namei * offset_size),
5406 offset_size,
5407 dwarf5_byte_order);
5408 return read_indirect_string_at_offset
5409 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5410 }
5411
5412 /* Find a slot in .debug_names for the object named NAME. If NAME is
5413 found, return pointer to its pool data. If NAME cannot be found,
5414 return NULL. */
5415
5416 const gdb_byte *
5417 dw2_debug_names_iterator::find_vec_in_debug_names
5418 (const mapped_debug_names &map, const char *name)
5419 {
5420 int (*cmp) (const char *, const char *);
5421
5422 gdb::unique_xmalloc_ptr<char> without_params;
5423 if (current_language->la_language == language_cplus
5424 || current_language->la_language == language_fortran
5425 || current_language->la_language == language_d)
5426 {
5427 /* NAME is already canonical. Drop any qualifiers as
5428 .debug_names does not contain any. */
5429
5430 if (strchr (name, '(') != NULL)
5431 {
5432 without_params = cp_remove_params (name);
5433 if (without_params != NULL)
5434 name = without_params.get ();
5435 }
5436 }
5437
5438 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5439
5440 const uint32_t full_hash = dwarf5_djb_hash (name);
5441 uint32_t namei
5442 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5443 (map.bucket_table_reordered
5444 + (full_hash % map.bucket_count)), 4,
5445 map.dwarf5_byte_order);
5446 if (namei == 0)
5447 return NULL;
5448 --namei;
5449 if (namei >= map.name_count)
5450 {
5451 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5452 "[in module %s]"),
5453 namei, map.name_count,
5454 objfile_name (map.dwarf2_per_objfile->objfile));
5455 return NULL;
5456 }
5457
5458 for (;;)
5459 {
5460 const uint32_t namei_full_hash
5461 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5462 (map.hash_table_reordered + namei), 4,
5463 map.dwarf5_byte_order);
5464 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5465 return NULL;
5466
5467 if (full_hash == namei_full_hash)
5468 {
5469 const char *const namei_string = map.namei_to_name (namei);
5470
5471 #if 0 /* An expensive sanity check. */
5472 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5473 {
5474 complaint (_("Wrong .debug_names hash for string at index %u "
5475 "[in module %s]"),
5476 namei, objfile_name (dwarf2_per_objfile->objfile));
5477 return NULL;
5478 }
5479 #endif
5480
5481 if (cmp (namei_string, name) == 0)
5482 {
5483 const ULONGEST namei_entry_offs
5484 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5485 + namei * map.offset_size),
5486 map.offset_size, map.dwarf5_byte_order);
5487 return map.entry_pool + namei_entry_offs;
5488 }
5489 }
5490
5491 ++namei;
5492 if (namei >= map.name_count)
5493 return NULL;
5494 }
5495 }
5496
5497 const gdb_byte *
5498 dw2_debug_names_iterator::find_vec_in_debug_names
5499 (const mapped_debug_names &map, uint32_t namei)
5500 {
5501 if (namei >= map.name_count)
5502 {
5503 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5504 "[in module %s]"),
5505 namei, map.name_count,
5506 objfile_name (map.dwarf2_per_objfile->objfile));
5507 return NULL;
5508 }
5509
5510 const ULONGEST namei_entry_offs
5511 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5512 + namei * map.offset_size),
5513 map.offset_size, map.dwarf5_byte_order);
5514 return map.entry_pool + namei_entry_offs;
5515 }
5516
5517 /* See dw2_debug_names_iterator. */
5518
5519 dwarf2_per_cu_data *
5520 dw2_debug_names_iterator::next ()
5521 {
5522 if (m_addr == NULL)
5523 return NULL;
5524
5525 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5526 struct objfile *objfile = dwarf2_per_objfile->objfile;
5527 bfd *const abfd = objfile->obfd;
5528
5529 again:
5530
5531 unsigned int bytes_read;
5532 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5533 m_addr += bytes_read;
5534 if (abbrev == 0)
5535 return NULL;
5536
5537 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5538 if (indexval_it == m_map.abbrev_map.cend ())
5539 {
5540 complaint (_("Wrong .debug_names undefined abbrev code %s "
5541 "[in module %s]"),
5542 pulongest (abbrev), objfile_name (objfile));
5543 return NULL;
5544 }
5545 const mapped_debug_names::index_val &indexval = indexval_it->second;
5546 enum class symbol_linkage {
5547 unknown,
5548 static_,
5549 extern_,
5550 } symbol_linkage_ = symbol_linkage::unknown;
5551 dwarf2_per_cu_data *per_cu = NULL;
5552 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5553 {
5554 ULONGEST ull;
5555 switch (attr.form)
5556 {
5557 case DW_FORM_implicit_const:
5558 ull = attr.implicit_const;
5559 break;
5560 case DW_FORM_flag_present:
5561 ull = 1;
5562 break;
5563 case DW_FORM_udata:
5564 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5565 m_addr += bytes_read;
5566 break;
5567 default:
5568 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5569 dwarf_form_name (attr.form),
5570 objfile_name (objfile));
5571 return NULL;
5572 }
5573 switch (attr.dw_idx)
5574 {
5575 case DW_IDX_compile_unit:
5576 /* Don't crash on bad data. */
5577 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5578 {
5579 complaint (_(".debug_names entry has bad CU index %s"
5580 " [in module %s]"),
5581 pulongest (ull),
5582 objfile_name (dwarf2_per_objfile->objfile));
5583 continue;
5584 }
5585 per_cu = dwarf2_per_objfile->get_cutu (ull);
5586 break;
5587 case DW_IDX_type_unit:
5588 /* Don't crash on bad data. */
5589 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5590 {
5591 complaint (_(".debug_names entry has bad TU index %s"
5592 " [in module %s]"),
5593 pulongest (ull),
5594 objfile_name (dwarf2_per_objfile->objfile));
5595 continue;
5596 }
5597 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5598 break;
5599 case DW_IDX_GNU_internal:
5600 if (!m_map.augmentation_is_gdb)
5601 break;
5602 symbol_linkage_ = symbol_linkage::static_;
5603 break;
5604 case DW_IDX_GNU_external:
5605 if (!m_map.augmentation_is_gdb)
5606 break;
5607 symbol_linkage_ = symbol_linkage::extern_;
5608 break;
5609 }
5610 }
5611
5612 /* Skip if already read in. */
5613 if (per_cu->v.quick->compunit_symtab)
5614 goto again;
5615
5616 /* Check static vs global. */
5617 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5618 {
5619 const bool want_static = *m_block_index == STATIC_BLOCK;
5620 const bool symbol_is_static =
5621 symbol_linkage_ == symbol_linkage::static_;
5622 if (want_static != symbol_is_static)
5623 goto again;
5624 }
5625
5626 /* Match dw2_symtab_iter_next, symbol_kind
5627 and debug_names::psymbol_tag. */
5628 switch (m_domain)
5629 {
5630 case VAR_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case DW_TAG_variable:
5634 case DW_TAG_subprogram:
5635 /* Some types are also in VAR_DOMAIN. */
5636 case DW_TAG_typedef:
5637 case DW_TAG_structure_type:
5638 break;
5639 default:
5640 goto again;
5641 }
5642 break;
5643 case STRUCT_DOMAIN:
5644 switch (indexval.dwarf_tag)
5645 {
5646 case DW_TAG_typedef:
5647 case DW_TAG_structure_type:
5648 break;
5649 default:
5650 goto again;
5651 }
5652 break;
5653 case LABEL_DOMAIN:
5654 switch (indexval.dwarf_tag)
5655 {
5656 case 0:
5657 case DW_TAG_variable:
5658 break;
5659 default:
5660 goto again;
5661 }
5662 break;
5663 case MODULE_DOMAIN:
5664 switch (indexval.dwarf_tag)
5665 {
5666 case DW_TAG_module:
5667 break;
5668 default:
5669 goto again;
5670 }
5671 break;
5672 default:
5673 break;
5674 }
5675
5676 /* Match dw2_expand_symtabs_matching, symbol_kind and
5677 debug_names::psymbol_tag. */
5678 switch (m_search)
5679 {
5680 case VARIABLES_DOMAIN:
5681 switch (indexval.dwarf_tag)
5682 {
5683 case DW_TAG_variable:
5684 break;
5685 default:
5686 goto again;
5687 }
5688 break;
5689 case FUNCTIONS_DOMAIN:
5690 switch (indexval.dwarf_tag)
5691 {
5692 case DW_TAG_subprogram:
5693 break;
5694 default:
5695 goto again;
5696 }
5697 break;
5698 case TYPES_DOMAIN:
5699 switch (indexval.dwarf_tag)
5700 {
5701 case DW_TAG_typedef:
5702 case DW_TAG_structure_type:
5703 break;
5704 default:
5705 goto again;
5706 }
5707 break;
5708 case MODULES_DOMAIN:
5709 switch (indexval.dwarf_tag)
5710 {
5711 case DW_TAG_module:
5712 break;
5713 default:
5714 goto again;
5715 }
5716 default:
5717 break;
5718 }
5719
5720 return per_cu;
5721 }
5722
5723 static struct compunit_symtab *
5724 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5725 const char *name, domain_enum domain)
5726 {
5727 struct dwarf2_per_objfile *dwarf2_per_objfile
5728 = get_dwarf2_per_objfile (objfile);
5729
5730 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5731 if (!mapp)
5732 {
5733 /* index is NULL if OBJF_READNOW. */
5734 return NULL;
5735 }
5736 const auto &map = *mapp;
5737
5738 dw2_debug_names_iterator iter (map, block_index, domain, name);
5739
5740 struct compunit_symtab *stab_best = NULL;
5741 struct dwarf2_per_cu_data *per_cu;
5742 while ((per_cu = iter.next ()) != NULL)
5743 {
5744 struct symbol *sym, *with_opaque = NULL;
5745 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5746 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5747 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5748
5749 sym = block_find_symbol (block, name, domain,
5750 block_find_non_opaque_type_preferred,
5751 &with_opaque);
5752
5753 /* Some caution must be observed with overloaded functions and
5754 methods, since the index will not contain any overload
5755 information (but NAME might contain it). */
5756
5757 if (sym != NULL
5758 && strcmp_iw (sym->search_name (), name) == 0)
5759 return stab;
5760 if (with_opaque != NULL
5761 && strcmp_iw (with_opaque->search_name (), name) == 0)
5762 stab_best = stab;
5763
5764 /* Keep looking through other CUs. */
5765 }
5766
5767 return stab_best;
5768 }
5769
5770 /* This dumps minimal information about .debug_names. It is called
5771 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5772 uses this to verify that .debug_names has been loaded. */
5773
5774 static void
5775 dw2_debug_names_dump (struct objfile *objfile)
5776 {
5777 struct dwarf2_per_objfile *dwarf2_per_objfile
5778 = get_dwarf2_per_objfile (objfile);
5779
5780 gdb_assert (dwarf2_per_objfile->using_index);
5781 printf_filtered (".debug_names:");
5782 if (dwarf2_per_objfile->debug_names_table)
5783 printf_filtered (" exists\n");
5784 else
5785 printf_filtered (" faked for \"readnow\"\n");
5786 printf_filtered ("\n");
5787 }
5788
5789 static void
5790 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5791 const char *func_name)
5792 {
5793 struct dwarf2_per_objfile *dwarf2_per_objfile
5794 = get_dwarf2_per_objfile (objfile);
5795
5796 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5797 if (dwarf2_per_objfile->debug_names_table)
5798 {
5799 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5800
5801 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5802
5803 struct dwarf2_per_cu_data *per_cu;
5804 while ((per_cu = iter.next ()) != NULL)
5805 dw2_instantiate_symtab (per_cu, false);
5806 }
5807 }
5808
5809 static void
5810 dw2_debug_names_map_matching_symbols
5811 (struct objfile *objfile,
5812 const lookup_name_info &name, domain_enum domain,
5813 int global,
5814 gdb::function_view<symbol_found_callback_ftype> callback,
5815 symbol_compare_ftype *ordered_compare)
5816 {
5817 struct dwarf2_per_objfile *dwarf2_per_objfile
5818 = get_dwarf2_per_objfile (objfile);
5819
5820 /* debug_names_table is NULL if OBJF_READNOW. */
5821 if (!dwarf2_per_objfile->debug_names_table)
5822 return;
5823
5824 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5825 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5826
5827 const char *match_name = name.ada ().lookup_name ().c_str ();
5828 auto matcher = [&] (const char *symname)
5829 {
5830 if (ordered_compare == nullptr)
5831 return true;
5832 return ordered_compare (symname, match_name) == 0;
5833 };
5834
5835 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5836 [&] (offset_type namei)
5837 {
5838 /* The name was matched, now expand corresponding CUs that were
5839 marked. */
5840 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5841
5842 struct dwarf2_per_cu_data *per_cu;
5843 while ((per_cu = iter.next ()) != NULL)
5844 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5845 return true;
5846 });
5847
5848 /* It's a shame we couldn't do this inside the
5849 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5850 that have already been expanded. Instead, this loop matches what
5851 the psymtab code does. */
5852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5853 {
5854 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5855 if (cust != nullptr)
5856 {
5857 const struct block *block
5858 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5859 if (!iterate_over_symbols_terminated (block, name,
5860 domain, callback))
5861 break;
5862 }
5863 }
5864 }
5865
5866 static void
5867 dw2_debug_names_expand_symtabs_matching
5868 (struct objfile *objfile,
5869 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5870 const lookup_name_info &lookup_name,
5871 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5872 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5873 enum search_domain kind)
5874 {
5875 struct dwarf2_per_objfile *dwarf2_per_objfile
5876 = get_dwarf2_per_objfile (objfile);
5877
5878 /* debug_names_table is NULL if OBJF_READNOW. */
5879 if (!dwarf2_per_objfile->debug_names_table)
5880 return;
5881
5882 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5883
5884 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5885
5886 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5887 symbol_matcher,
5888 kind, [&] (offset_type namei)
5889 {
5890 /* The name was matched, now expand corresponding CUs that were
5891 marked. */
5892 dw2_debug_names_iterator iter (map, kind, namei);
5893
5894 struct dwarf2_per_cu_data *per_cu;
5895 while ((per_cu = iter.next ()) != NULL)
5896 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5897 expansion_notify);
5898 return true;
5899 });
5900 }
5901
5902 const struct quick_symbol_functions dwarf2_debug_names_functions =
5903 {
5904 dw2_has_symbols,
5905 dw2_find_last_source_symtab,
5906 dw2_forget_cached_source_info,
5907 dw2_map_symtabs_matching_filename,
5908 dw2_debug_names_lookup_symbol,
5909 dw2_print_stats,
5910 dw2_debug_names_dump,
5911 dw2_debug_names_expand_symtabs_for_function,
5912 dw2_expand_all_symtabs,
5913 dw2_expand_symtabs_with_fullname,
5914 dw2_debug_names_map_matching_symbols,
5915 dw2_debug_names_expand_symtabs_matching,
5916 dw2_find_pc_sect_compunit_symtab,
5917 NULL,
5918 dw2_map_symbol_filenames
5919 };
5920
5921 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5922 to either a dwarf2_per_objfile or dwz_file object. */
5923
5924 template <typename T>
5925 static gdb::array_view<const gdb_byte>
5926 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5927 {
5928 dwarf2_section_info *section = &section_owner->gdb_index;
5929
5930 if (section->empty ())
5931 return {};
5932
5933 /* Older elfutils strip versions could keep the section in the main
5934 executable while splitting it for the separate debug info file. */
5935 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5936 return {};
5937
5938 section->read (obj);
5939
5940 /* dwarf2_section_info::size is a bfd_size_type, while
5941 gdb::array_view works with size_t. On 32-bit hosts, with
5942 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5943 is 32-bit. So we need an explicit narrowing conversion here.
5944 This is fine, because it's impossible to allocate or mmap an
5945 array/buffer larger than what size_t can represent. */
5946 return gdb::make_array_view (section->buffer, section->size);
5947 }
5948
5949 /* Lookup the index cache for the contents of the index associated to
5950 DWARF2_OBJ. */
5951
5952 static gdb::array_view<const gdb_byte>
5953 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5954 {
5955 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5956 if (build_id == nullptr)
5957 return {};
5958
5959 return global_index_cache.lookup_gdb_index (build_id,
5960 &dwarf2_obj->index_cache_res);
5961 }
5962
5963 /* Same as the above, but for DWZ. */
5964
5965 static gdb::array_view<const gdb_byte>
5966 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5967 {
5968 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5969 if (build_id == nullptr)
5970 return {};
5971
5972 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5973 }
5974
5975 /* See symfile.h. */
5976
5977 bool
5978 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5979 {
5980 struct dwarf2_per_objfile *dwarf2_per_objfile
5981 = get_dwarf2_per_objfile (objfile);
5982
5983 /* If we're about to read full symbols, don't bother with the
5984 indices. In this case we also don't care if some other debug
5985 format is making psymtabs, because they are all about to be
5986 expanded anyway. */
5987 if ((objfile->flags & OBJF_READNOW))
5988 {
5989 dwarf2_per_objfile->using_index = 1;
5990 create_all_comp_units (dwarf2_per_objfile);
5991 create_all_type_units (dwarf2_per_objfile);
5992 dwarf2_per_objfile->quick_file_names_table
5993 = create_quick_file_names_table
5994 (dwarf2_per_objfile->all_comp_units.size ());
5995
5996 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5997 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5998 {
5999 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6000
6001 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6002 struct dwarf2_per_cu_quick_data);
6003 }
6004
6005 /* Return 1 so that gdb sees the "quick" functions. However,
6006 these functions will be no-ops because we will have expanded
6007 all symtabs. */
6008 *index_kind = dw_index_kind::GDB_INDEX;
6009 return true;
6010 }
6011
6012 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6013 {
6014 *index_kind = dw_index_kind::DEBUG_NAMES;
6015 return true;
6016 }
6017
6018 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6019 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6020 get_gdb_index_contents_from_section<dwz_file>))
6021 {
6022 *index_kind = dw_index_kind::GDB_INDEX;
6023 return true;
6024 }
6025
6026 /* ... otherwise, try to find the index in the index cache. */
6027 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6028 get_gdb_index_contents_from_cache,
6029 get_gdb_index_contents_from_cache_dwz))
6030 {
6031 global_index_cache.hit ();
6032 *index_kind = dw_index_kind::GDB_INDEX;
6033 return true;
6034 }
6035
6036 global_index_cache.miss ();
6037 return false;
6038 }
6039
6040 \f
6041
6042 /* Build a partial symbol table. */
6043
6044 void
6045 dwarf2_build_psymtabs (struct objfile *objfile)
6046 {
6047 struct dwarf2_per_objfile *dwarf2_per_objfile
6048 = get_dwarf2_per_objfile (objfile);
6049
6050 init_psymbol_list (objfile, 1024);
6051
6052 try
6053 {
6054 /* This isn't really ideal: all the data we allocate on the
6055 objfile's obstack is still uselessly kept around. However,
6056 freeing it seems unsafe. */
6057 psymtab_discarder psymtabs (objfile);
6058 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6059 psymtabs.keep ();
6060
6061 /* (maybe) store an index in the cache. */
6062 global_index_cache.store (dwarf2_per_objfile);
6063 }
6064 catch (const gdb_exception_error &except)
6065 {
6066 exception_print (gdb_stderr, except);
6067 }
6068 }
6069
6070 /* Return the total length of the CU described by HEADER. */
6071
6072 static unsigned int
6073 get_cu_length (const struct comp_unit_head *header)
6074 {
6075 return header->initial_length_size + header->length;
6076 }
6077
6078 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6079
6080 static inline bool
6081 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6082 {
6083 sect_offset bottom = cu_header->sect_off;
6084 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6085
6086 return sect_off >= bottom && sect_off < top;
6087 }
6088
6089 /* Find the base address of the compilation unit for range lists and
6090 location lists. It will normally be specified by DW_AT_low_pc.
6091 In DWARF-3 draft 4, the base address could be overridden by
6092 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6093 compilation units with discontinuous ranges. */
6094
6095 static void
6096 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6097 {
6098 struct attribute *attr;
6099
6100 cu->base_known = 0;
6101 cu->base_address = 0;
6102
6103 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6104 if (attr != nullptr)
6105 {
6106 cu->base_address = attr->value_as_address ();
6107 cu->base_known = 1;
6108 }
6109 else
6110 {
6111 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6112 if (attr != nullptr)
6113 {
6114 cu->base_address = attr->value_as_address ();
6115 cu->base_known = 1;
6116 }
6117 }
6118 }
6119
6120 /* Read in the comp unit header information from the debug_info at info_ptr.
6121 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6122 NOTE: This leaves members offset, first_die_offset to be filled in
6123 by the caller. */
6124
6125 static const gdb_byte *
6126 read_comp_unit_head (struct comp_unit_head *cu_header,
6127 const gdb_byte *info_ptr,
6128 struct dwarf2_section_info *section,
6129 rcuh_kind section_kind)
6130 {
6131 int signed_addr;
6132 unsigned int bytes_read;
6133 const char *filename = section->get_file_name ();
6134 bfd *abfd = section->get_bfd_owner ();
6135
6136 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6137 cu_header->initial_length_size = bytes_read;
6138 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6139 info_ptr += bytes_read;
6140 cu_header->version = read_2_bytes (abfd, info_ptr);
6141 if (cu_header->version < 2 || cu_header->version > 5)
6142 error (_("Dwarf Error: wrong version in compilation unit header "
6143 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6144 cu_header->version, filename);
6145 info_ptr += 2;
6146 if (cu_header->version < 5)
6147 switch (section_kind)
6148 {
6149 case rcuh_kind::COMPILE:
6150 cu_header->unit_type = DW_UT_compile;
6151 break;
6152 case rcuh_kind::TYPE:
6153 cu_header->unit_type = DW_UT_type;
6154 break;
6155 default:
6156 internal_error (__FILE__, __LINE__,
6157 _("read_comp_unit_head: invalid section_kind"));
6158 }
6159 else
6160 {
6161 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6162 (read_1_byte (abfd, info_ptr));
6163 info_ptr += 1;
6164 switch (cu_header->unit_type)
6165 {
6166 case DW_UT_compile:
6167 case DW_UT_partial:
6168 case DW_UT_skeleton:
6169 case DW_UT_split_compile:
6170 if (section_kind != rcuh_kind::COMPILE)
6171 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6172 "(is %s, should be %s) [in module %s]"),
6173 dwarf_unit_type_name (cu_header->unit_type),
6174 dwarf_unit_type_name (DW_UT_type), filename);
6175 break;
6176 case DW_UT_type:
6177 case DW_UT_split_type:
6178 section_kind = rcuh_kind::TYPE;
6179 break;
6180 default:
6181 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6182 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6183 "[in module %s]"), cu_header->unit_type,
6184 dwarf_unit_type_name (DW_UT_compile),
6185 dwarf_unit_type_name (DW_UT_skeleton),
6186 dwarf_unit_type_name (DW_UT_split_compile),
6187 dwarf_unit_type_name (DW_UT_type),
6188 dwarf_unit_type_name (DW_UT_split_type), filename);
6189 }
6190
6191 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6192 info_ptr += 1;
6193 }
6194 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6195 cu_header,
6196 &bytes_read);
6197 info_ptr += bytes_read;
6198 if (cu_header->version < 5)
6199 {
6200 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6201 info_ptr += 1;
6202 }
6203 signed_addr = bfd_get_sign_extend_vma (abfd);
6204 if (signed_addr < 0)
6205 internal_error (__FILE__, __LINE__,
6206 _("read_comp_unit_head: dwarf from non elf file"));
6207 cu_header->signed_addr_p = signed_addr;
6208
6209 bool header_has_signature = section_kind == rcuh_kind::TYPE
6210 || cu_header->unit_type == DW_UT_skeleton
6211 || cu_header->unit_type == DW_UT_split_compile;
6212
6213 if (header_has_signature)
6214 {
6215 cu_header->signature = read_8_bytes (abfd, info_ptr);
6216 info_ptr += 8;
6217 }
6218
6219 if (section_kind == rcuh_kind::TYPE)
6220 {
6221 LONGEST type_offset;
6222 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6223 info_ptr += bytes_read;
6224 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6225 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6226 error (_("Dwarf Error: Too big type_offset in compilation unit "
6227 "header (is %s) [in module %s]"), plongest (type_offset),
6228 filename);
6229 }
6230
6231 return info_ptr;
6232 }
6233
6234 /* Helper function that returns the proper abbrev section for
6235 THIS_CU. */
6236
6237 static struct dwarf2_section_info *
6238 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6239 {
6240 struct dwarf2_section_info *abbrev;
6241 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6242
6243 if (this_cu->is_dwz)
6244 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6245 else
6246 abbrev = &dwarf2_per_objfile->abbrev;
6247
6248 return abbrev;
6249 }
6250
6251 /* Subroutine of read_and_check_comp_unit_head and
6252 read_and_check_type_unit_head to simplify them.
6253 Perform various error checking on the header. */
6254
6255 static void
6256 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6257 struct comp_unit_head *header,
6258 struct dwarf2_section_info *section,
6259 struct dwarf2_section_info *abbrev_section)
6260 {
6261 const char *filename = section->get_file_name ();
6262
6263 if (to_underlying (header->abbrev_sect_off)
6264 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6265 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6266 "(offset %s + 6) [in module %s]"),
6267 sect_offset_str (header->abbrev_sect_off),
6268 sect_offset_str (header->sect_off),
6269 filename);
6270
6271 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6272 avoid potential 32-bit overflow. */
6273 if (((ULONGEST) header->sect_off + get_cu_length (header))
6274 > section->size)
6275 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6276 "(offset %s + 0) [in module %s]"),
6277 header->length, sect_offset_str (header->sect_off),
6278 filename);
6279 }
6280
6281 /* Read in a CU/TU header and perform some basic error checking.
6282 The contents of the header are stored in HEADER.
6283 The result is a pointer to the start of the first DIE. */
6284
6285 static const gdb_byte *
6286 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6287 struct comp_unit_head *header,
6288 struct dwarf2_section_info *section,
6289 struct dwarf2_section_info *abbrev_section,
6290 const gdb_byte *info_ptr,
6291 rcuh_kind section_kind)
6292 {
6293 const gdb_byte *beg_of_comp_unit = info_ptr;
6294
6295 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6296
6297 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6298
6299 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6300
6301 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6302 abbrev_section);
6303
6304 return info_ptr;
6305 }
6306
6307 /* Fetch the abbreviation table offset from a comp or type unit header. */
6308
6309 static sect_offset
6310 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6311 struct dwarf2_section_info *section,
6312 sect_offset sect_off)
6313 {
6314 bfd *abfd = section->get_bfd_owner ();
6315 const gdb_byte *info_ptr;
6316 unsigned int initial_length_size, offset_size;
6317 uint16_t version;
6318
6319 section->read (dwarf2_per_objfile->objfile);
6320 info_ptr = section->buffer + to_underlying (sect_off);
6321 read_initial_length (abfd, info_ptr, &initial_length_size);
6322 offset_size = initial_length_size == 4 ? 4 : 8;
6323 info_ptr += initial_length_size;
6324
6325 version = read_2_bytes (abfd, info_ptr);
6326 info_ptr += 2;
6327 if (version >= 5)
6328 {
6329 /* Skip unit type and address size. */
6330 info_ptr += 2;
6331 }
6332
6333 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6334 }
6335
6336 /* Allocate a new partial symtab for file named NAME and mark this new
6337 partial symtab as being an include of PST. */
6338
6339 static void
6340 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6341 struct objfile *objfile)
6342 {
6343 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6344
6345 if (!IS_ABSOLUTE_PATH (subpst->filename))
6346 {
6347 /* It shares objfile->objfile_obstack. */
6348 subpst->dirname = pst->dirname;
6349 }
6350
6351 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6352 subpst->dependencies[0] = pst;
6353 subpst->number_of_dependencies = 1;
6354
6355 /* No private part is necessary for include psymtabs. This property
6356 can be used to differentiate between such include psymtabs and
6357 the regular ones. */
6358 subpst->per_cu_data = nullptr;
6359 }
6360
6361 /* Read the Line Number Program data and extract the list of files
6362 included by the source file represented by PST. Build an include
6363 partial symtab for each of these included files. */
6364
6365 static void
6366 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6367 struct die_info *die,
6368 dwarf2_psymtab *pst)
6369 {
6370 line_header_up lh;
6371 struct attribute *attr;
6372
6373 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6374 if (attr != nullptr)
6375 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6376 if (lh == NULL)
6377 return; /* No linetable, so no includes. */
6378
6379 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6380 that we pass in the raw text_low here; that is ok because we're
6381 only decoding the line table to make include partial symtabs, and
6382 so the addresses aren't really used. */
6383 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6384 pst->raw_text_low (), 1);
6385 }
6386
6387 static hashval_t
6388 hash_signatured_type (const void *item)
6389 {
6390 const struct signatured_type *sig_type
6391 = (const struct signatured_type *) item;
6392
6393 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6394 return sig_type->signature;
6395 }
6396
6397 static int
6398 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6399 {
6400 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6401 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6402
6403 return lhs->signature == rhs->signature;
6404 }
6405
6406 /* Allocate a hash table for signatured types. */
6407
6408 static htab_up
6409 allocate_signatured_type_table (struct objfile *objfile)
6410 {
6411 return htab_up (htab_create_alloc (41,
6412 hash_signatured_type,
6413 eq_signatured_type,
6414 NULL, xcalloc, xfree));
6415 }
6416
6417 /* A helper function to add a signatured type CU to a table. */
6418
6419 static int
6420 add_signatured_type_cu_to_table (void **slot, void *datum)
6421 {
6422 struct signatured_type *sigt = (struct signatured_type *) *slot;
6423 std::vector<signatured_type *> *all_type_units
6424 = (std::vector<signatured_type *> *) datum;
6425
6426 all_type_units->push_back (sigt);
6427
6428 return 1;
6429 }
6430
6431 /* A helper for create_debug_types_hash_table. Read types from SECTION
6432 and fill them into TYPES_HTAB. It will process only type units,
6433 therefore DW_UT_type. */
6434
6435 static void
6436 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6437 struct dwo_file *dwo_file,
6438 dwarf2_section_info *section, htab_up &types_htab,
6439 rcuh_kind section_kind)
6440 {
6441 struct objfile *objfile = dwarf2_per_objfile->objfile;
6442 struct dwarf2_section_info *abbrev_section;
6443 bfd *abfd;
6444 const gdb_byte *info_ptr, *end_ptr;
6445
6446 abbrev_section = (dwo_file != NULL
6447 ? &dwo_file->sections.abbrev
6448 : &dwarf2_per_objfile->abbrev);
6449
6450 if (dwarf_read_debug)
6451 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6452 section->get_name (),
6453 abbrev_section->get_file_name ());
6454
6455 section->read (objfile);
6456 info_ptr = section->buffer;
6457
6458 if (info_ptr == NULL)
6459 return;
6460
6461 /* We can't set abfd until now because the section may be empty or
6462 not present, in which case the bfd is unknown. */
6463 abfd = section->get_bfd_owner ();
6464
6465 /* We don't use cutu_reader here because we don't need to read
6466 any dies: the signature is in the header. */
6467
6468 end_ptr = info_ptr + section->size;
6469 while (info_ptr < end_ptr)
6470 {
6471 struct signatured_type *sig_type;
6472 struct dwo_unit *dwo_tu;
6473 void **slot;
6474 const gdb_byte *ptr = info_ptr;
6475 struct comp_unit_head header;
6476 unsigned int length;
6477
6478 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6479
6480 /* Initialize it due to a false compiler warning. */
6481 header.signature = -1;
6482 header.type_cu_offset_in_tu = (cu_offset) -1;
6483
6484 /* We need to read the type's signature in order to build the hash
6485 table, but we don't need anything else just yet. */
6486
6487 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6488 abbrev_section, ptr, section_kind);
6489
6490 length = get_cu_length (&header);
6491
6492 /* Skip dummy type units. */
6493 if (ptr >= info_ptr + length
6494 || peek_abbrev_code (abfd, ptr) == 0
6495 || header.unit_type != DW_UT_type)
6496 {
6497 info_ptr += length;
6498 continue;
6499 }
6500
6501 if (types_htab == NULL)
6502 {
6503 if (dwo_file)
6504 types_htab = allocate_dwo_unit_table (objfile);
6505 else
6506 types_htab = allocate_signatured_type_table (objfile);
6507 }
6508
6509 if (dwo_file)
6510 {
6511 sig_type = NULL;
6512 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6513 struct dwo_unit);
6514 dwo_tu->dwo_file = dwo_file;
6515 dwo_tu->signature = header.signature;
6516 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6517 dwo_tu->section = section;
6518 dwo_tu->sect_off = sect_off;
6519 dwo_tu->length = length;
6520 }
6521 else
6522 {
6523 /* N.B.: type_offset is not usable if this type uses a DWO file.
6524 The real type_offset is in the DWO file. */
6525 dwo_tu = NULL;
6526 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6527 struct signatured_type);
6528 sig_type->signature = header.signature;
6529 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6530 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6531 sig_type->per_cu.is_debug_types = 1;
6532 sig_type->per_cu.section = section;
6533 sig_type->per_cu.sect_off = sect_off;
6534 sig_type->per_cu.length = length;
6535 }
6536
6537 slot = htab_find_slot (types_htab.get (),
6538 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6539 INSERT);
6540 gdb_assert (slot != NULL);
6541 if (*slot != NULL)
6542 {
6543 sect_offset dup_sect_off;
6544
6545 if (dwo_file)
6546 {
6547 const struct dwo_unit *dup_tu
6548 = (const struct dwo_unit *) *slot;
6549
6550 dup_sect_off = dup_tu->sect_off;
6551 }
6552 else
6553 {
6554 const struct signatured_type *dup_tu
6555 = (const struct signatured_type *) *slot;
6556
6557 dup_sect_off = dup_tu->per_cu.sect_off;
6558 }
6559
6560 complaint (_("debug type entry at offset %s is duplicate to"
6561 " the entry at offset %s, signature %s"),
6562 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6563 hex_string (header.signature));
6564 }
6565 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6566
6567 if (dwarf_read_debug > 1)
6568 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6569 sect_offset_str (sect_off),
6570 hex_string (header.signature));
6571
6572 info_ptr += length;
6573 }
6574 }
6575
6576 /* Create the hash table of all entries in the .debug_types
6577 (or .debug_types.dwo) section(s).
6578 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6579 otherwise it is NULL.
6580
6581 The result is a pointer to the hash table or NULL if there are no types.
6582
6583 Note: This function processes DWO files only, not DWP files. */
6584
6585 static void
6586 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6587 struct dwo_file *dwo_file,
6588 gdb::array_view<dwarf2_section_info> type_sections,
6589 htab_up &types_htab)
6590 {
6591 for (dwarf2_section_info &section : type_sections)
6592 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6593 types_htab, rcuh_kind::TYPE);
6594 }
6595
6596 /* Create the hash table of all entries in the .debug_types section,
6597 and initialize all_type_units.
6598 The result is zero if there is an error (e.g. missing .debug_types section),
6599 otherwise non-zero. */
6600
6601 static int
6602 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6603 {
6604 htab_up types_htab;
6605
6606 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6607 &dwarf2_per_objfile->info, types_htab,
6608 rcuh_kind::COMPILE);
6609 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6610 dwarf2_per_objfile->types, types_htab);
6611 if (types_htab == NULL)
6612 {
6613 dwarf2_per_objfile->signatured_types = NULL;
6614 return 0;
6615 }
6616
6617 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6618
6619 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6620 dwarf2_per_objfile->all_type_units.reserve
6621 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6622
6623 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6624 add_signatured_type_cu_to_table,
6625 &dwarf2_per_objfile->all_type_units);
6626
6627 return 1;
6628 }
6629
6630 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6631 If SLOT is non-NULL, it is the entry to use in the hash table.
6632 Otherwise we find one. */
6633
6634 static struct signatured_type *
6635 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6636 void **slot)
6637 {
6638 struct objfile *objfile = dwarf2_per_objfile->objfile;
6639
6640 if (dwarf2_per_objfile->all_type_units.size ()
6641 == dwarf2_per_objfile->all_type_units.capacity ())
6642 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6643
6644 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6645 struct signatured_type);
6646
6647 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6648 sig_type->signature = sig;
6649 sig_type->per_cu.is_debug_types = 1;
6650 if (dwarf2_per_objfile->using_index)
6651 {
6652 sig_type->per_cu.v.quick =
6653 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6654 struct dwarf2_per_cu_quick_data);
6655 }
6656
6657 if (slot == NULL)
6658 {
6659 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6660 sig_type, INSERT);
6661 }
6662 gdb_assert (*slot == NULL);
6663 *slot = sig_type;
6664 /* The rest of sig_type must be filled in by the caller. */
6665 return sig_type;
6666 }
6667
6668 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6669 Fill in SIG_ENTRY with DWO_ENTRY. */
6670
6671 static void
6672 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6673 struct signatured_type *sig_entry,
6674 struct dwo_unit *dwo_entry)
6675 {
6676 /* Make sure we're not clobbering something we don't expect to. */
6677 gdb_assert (! sig_entry->per_cu.queued);
6678 gdb_assert (sig_entry->per_cu.cu == NULL);
6679 if (dwarf2_per_objfile->using_index)
6680 {
6681 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6682 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6683 }
6684 else
6685 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6686 gdb_assert (sig_entry->signature == dwo_entry->signature);
6687 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6688 gdb_assert (sig_entry->type_unit_group == NULL);
6689 gdb_assert (sig_entry->dwo_unit == NULL);
6690
6691 sig_entry->per_cu.section = dwo_entry->section;
6692 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6693 sig_entry->per_cu.length = dwo_entry->length;
6694 sig_entry->per_cu.reading_dwo_directly = 1;
6695 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6696 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6697 sig_entry->dwo_unit = dwo_entry;
6698 }
6699
6700 /* Subroutine of lookup_signatured_type.
6701 If we haven't read the TU yet, create the signatured_type data structure
6702 for a TU to be read in directly from a DWO file, bypassing the stub.
6703 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6704 using .gdb_index, then when reading a CU we want to stay in the DWO file
6705 containing that CU. Otherwise we could end up reading several other DWO
6706 files (due to comdat folding) to process the transitive closure of all the
6707 mentioned TUs, and that can be slow. The current DWO file will have every
6708 type signature that it needs.
6709 We only do this for .gdb_index because in the psymtab case we already have
6710 to read all the DWOs to build the type unit groups. */
6711
6712 static struct signatured_type *
6713 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6714 {
6715 struct dwarf2_per_objfile *dwarf2_per_objfile
6716 = cu->per_cu->dwarf2_per_objfile;
6717 struct objfile *objfile = dwarf2_per_objfile->objfile;
6718 struct dwo_file *dwo_file;
6719 struct dwo_unit find_dwo_entry, *dwo_entry;
6720 struct signatured_type find_sig_entry, *sig_entry;
6721 void **slot;
6722
6723 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6724
6725 /* If TU skeletons have been removed then we may not have read in any
6726 TUs yet. */
6727 if (dwarf2_per_objfile->signatured_types == NULL)
6728 {
6729 dwarf2_per_objfile->signatured_types
6730 = allocate_signatured_type_table (objfile);
6731 }
6732
6733 /* We only ever need to read in one copy of a signatured type.
6734 Use the global signatured_types array to do our own comdat-folding
6735 of types. If this is the first time we're reading this TU, and
6736 the TU has an entry in .gdb_index, replace the recorded data from
6737 .gdb_index with this TU. */
6738
6739 find_sig_entry.signature = sig;
6740 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6741 &find_sig_entry, INSERT);
6742 sig_entry = (struct signatured_type *) *slot;
6743
6744 /* We can get here with the TU already read, *or* in the process of being
6745 read. Don't reassign the global entry to point to this DWO if that's
6746 the case. Also note that if the TU is already being read, it may not
6747 have come from a DWO, the program may be a mix of Fission-compiled
6748 code and non-Fission-compiled code. */
6749
6750 /* Have we already tried to read this TU?
6751 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6752 needn't exist in the global table yet). */
6753 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6754 return sig_entry;
6755
6756 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6757 dwo_unit of the TU itself. */
6758 dwo_file = cu->dwo_unit->dwo_file;
6759
6760 /* Ok, this is the first time we're reading this TU. */
6761 if (dwo_file->tus == NULL)
6762 return NULL;
6763 find_dwo_entry.signature = sig;
6764 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6765 &find_dwo_entry);
6766 if (dwo_entry == NULL)
6767 return NULL;
6768
6769 /* If the global table doesn't have an entry for this TU, add one. */
6770 if (sig_entry == NULL)
6771 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6772
6773 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6774 sig_entry->per_cu.tu_read = 1;
6775 return sig_entry;
6776 }
6777
6778 /* Subroutine of lookup_signatured_type.
6779 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6780 then try the DWP file. If the TU stub (skeleton) has been removed then
6781 it won't be in .gdb_index. */
6782
6783 static struct signatured_type *
6784 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6785 {
6786 struct dwarf2_per_objfile *dwarf2_per_objfile
6787 = cu->per_cu->dwarf2_per_objfile;
6788 struct objfile *objfile = dwarf2_per_objfile->objfile;
6789 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6790 struct dwo_unit *dwo_entry;
6791 struct signatured_type find_sig_entry, *sig_entry;
6792 void **slot;
6793
6794 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6795 gdb_assert (dwp_file != NULL);
6796
6797 /* If TU skeletons have been removed then we may not have read in any
6798 TUs yet. */
6799 if (dwarf2_per_objfile->signatured_types == NULL)
6800 {
6801 dwarf2_per_objfile->signatured_types
6802 = allocate_signatured_type_table (objfile);
6803 }
6804
6805 find_sig_entry.signature = sig;
6806 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6807 &find_sig_entry, INSERT);
6808 sig_entry = (struct signatured_type *) *slot;
6809
6810 /* Have we already tried to read this TU?
6811 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6812 needn't exist in the global table yet). */
6813 if (sig_entry != NULL)
6814 return sig_entry;
6815
6816 if (dwp_file->tus == NULL)
6817 return NULL;
6818 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6819 sig, 1 /* is_debug_types */);
6820 if (dwo_entry == NULL)
6821 return NULL;
6822
6823 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6824 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6825
6826 return sig_entry;
6827 }
6828
6829 /* Lookup a signature based type for DW_FORM_ref_sig8.
6830 Returns NULL if signature SIG is not present in the table.
6831 It is up to the caller to complain about this. */
6832
6833 static struct signatured_type *
6834 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6835 {
6836 struct dwarf2_per_objfile *dwarf2_per_objfile
6837 = cu->per_cu->dwarf2_per_objfile;
6838
6839 if (cu->dwo_unit
6840 && dwarf2_per_objfile->using_index)
6841 {
6842 /* We're in a DWO/DWP file, and we're using .gdb_index.
6843 These cases require special processing. */
6844 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6845 return lookup_dwo_signatured_type (cu, sig);
6846 else
6847 return lookup_dwp_signatured_type (cu, sig);
6848 }
6849 else
6850 {
6851 struct signatured_type find_entry, *entry;
6852
6853 if (dwarf2_per_objfile->signatured_types == NULL)
6854 return NULL;
6855 find_entry.signature = sig;
6856 entry = ((struct signatured_type *)
6857 htab_find (dwarf2_per_objfile->signatured_types.get (),
6858 &find_entry));
6859 return entry;
6860 }
6861 }
6862
6863 /* Return the address base of the compile unit, which, if exists, is stored
6864 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6865 static gdb::optional<ULONGEST>
6866 lookup_addr_base (struct die_info *comp_unit_die)
6867 {
6868 struct attribute *attr;
6869 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6870 if (attr == nullptr)
6871 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6872 if (attr == nullptr)
6873 return gdb::optional<ULONGEST> ();
6874 return DW_UNSND (attr);
6875 }
6876
6877 /* Return range lists base of the compile unit, which, if exists, is stored
6878 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6879 static ULONGEST
6880 lookup_ranges_base (struct die_info *comp_unit_die)
6881 {
6882 struct attribute *attr;
6883 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6884 if (attr == nullptr)
6885 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6886 if (attr == nullptr)
6887 return 0;
6888 return DW_UNSND (attr);
6889 }
6890
6891 /* Low level DIE reading support. */
6892
6893 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6894
6895 static void
6896 init_cu_die_reader (struct die_reader_specs *reader,
6897 struct dwarf2_cu *cu,
6898 struct dwarf2_section_info *section,
6899 struct dwo_file *dwo_file,
6900 struct abbrev_table *abbrev_table)
6901 {
6902 gdb_assert (section->readin && section->buffer != NULL);
6903 reader->abfd = section->get_bfd_owner ();
6904 reader->cu = cu;
6905 reader->dwo_file = dwo_file;
6906 reader->die_section = section;
6907 reader->buffer = section->buffer;
6908 reader->buffer_end = section->buffer + section->size;
6909 reader->abbrev_table = abbrev_table;
6910 }
6911
6912 /* Subroutine of cutu_reader to simplify it.
6913 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6914 There's just a lot of work to do, and cutu_reader is big enough
6915 already.
6916
6917 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6918 from it to the DIE in the DWO. If NULL we are skipping the stub.
6919 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6920 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6921 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6922 STUB_COMP_DIR may be non-NULL.
6923 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6924 are filled in with the info of the DIE from the DWO file.
6925 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6926 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6927 kept around for at least as long as *RESULT_READER.
6928
6929 The result is non-zero if a valid (non-dummy) DIE was found. */
6930
6931 static int
6932 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6933 struct dwo_unit *dwo_unit,
6934 struct die_info *stub_comp_unit_die,
6935 const char *stub_comp_dir,
6936 struct die_reader_specs *result_reader,
6937 const gdb_byte **result_info_ptr,
6938 struct die_info **result_comp_unit_die,
6939 abbrev_table_up *result_dwo_abbrev_table)
6940 {
6941 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6942 struct objfile *objfile = dwarf2_per_objfile->objfile;
6943 struct dwarf2_cu *cu = this_cu->cu;
6944 bfd *abfd;
6945 const gdb_byte *begin_info_ptr, *info_ptr;
6946 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6947 int i,num_extra_attrs;
6948 struct dwarf2_section_info *dwo_abbrev_section;
6949 struct die_info *comp_unit_die;
6950
6951 /* At most one of these may be provided. */
6952 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6953
6954 /* These attributes aren't processed until later:
6955 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6956 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6957 referenced later. However, these attributes are found in the stub
6958 which we won't have later. In order to not impose this complication
6959 on the rest of the code, we read them here and copy them to the
6960 DWO CU/TU die. */
6961
6962 stmt_list = NULL;
6963 low_pc = NULL;
6964 high_pc = NULL;
6965 ranges = NULL;
6966 comp_dir = NULL;
6967
6968 if (stub_comp_unit_die != NULL)
6969 {
6970 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6971 DWO file. */
6972 if (! this_cu->is_debug_types)
6973 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6974 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6975 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6976 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6977 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6978
6979 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6980
6981 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6982 here (if needed). We need the value before we can process
6983 DW_AT_ranges. */
6984 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6985 }
6986 else if (stub_comp_dir != NULL)
6987 {
6988 /* Reconstruct the comp_dir attribute to simplify the code below. */
6989 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6990 comp_dir->name = DW_AT_comp_dir;
6991 comp_dir->form = DW_FORM_string;
6992 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6993 DW_STRING (comp_dir) = stub_comp_dir;
6994 }
6995
6996 /* Set up for reading the DWO CU/TU. */
6997 cu->dwo_unit = dwo_unit;
6998 dwarf2_section_info *section = dwo_unit->section;
6999 section->read (objfile);
7000 abfd = section->get_bfd_owner ();
7001 begin_info_ptr = info_ptr = (section->buffer
7002 + to_underlying (dwo_unit->sect_off));
7003 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7004
7005 if (this_cu->is_debug_types)
7006 {
7007 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7008
7009 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7010 &cu->header, section,
7011 dwo_abbrev_section,
7012 info_ptr, rcuh_kind::TYPE);
7013 /* This is not an assert because it can be caused by bad debug info. */
7014 if (sig_type->signature != cu->header.signature)
7015 {
7016 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7017 " TU at offset %s [in module %s]"),
7018 hex_string (sig_type->signature),
7019 hex_string (cu->header.signature),
7020 sect_offset_str (dwo_unit->sect_off),
7021 bfd_get_filename (abfd));
7022 }
7023 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7024 /* For DWOs coming from DWP files, we don't know the CU length
7025 nor the type's offset in the TU until now. */
7026 dwo_unit->length = get_cu_length (&cu->header);
7027 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7028
7029 /* Establish the type offset that can be used to lookup the type.
7030 For DWO files, we don't know it until now. */
7031 sig_type->type_offset_in_section
7032 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7033 }
7034 else
7035 {
7036 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7037 &cu->header, section,
7038 dwo_abbrev_section,
7039 info_ptr, rcuh_kind::COMPILE);
7040 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7041 /* For DWOs coming from DWP files, we don't know the CU length
7042 until now. */
7043 dwo_unit->length = get_cu_length (&cu->header);
7044 }
7045
7046 *result_dwo_abbrev_table
7047 = abbrev_table::read (objfile, dwo_abbrev_section,
7048 cu->header.abbrev_sect_off);
7049 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7050 result_dwo_abbrev_table->get ());
7051
7052 /* Read in the die, but leave space to copy over the attributes
7053 from the stub. This has the benefit of simplifying the rest of
7054 the code - all the work to maintain the illusion of a single
7055 DW_TAG_{compile,type}_unit DIE is done here. */
7056 num_extra_attrs = ((stmt_list != NULL)
7057 + (low_pc != NULL)
7058 + (high_pc != NULL)
7059 + (ranges != NULL)
7060 + (comp_dir != NULL));
7061 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7062 num_extra_attrs);
7063
7064 /* Copy over the attributes from the stub to the DIE we just read in. */
7065 comp_unit_die = *result_comp_unit_die;
7066 i = comp_unit_die->num_attrs;
7067 if (stmt_list != NULL)
7068 comp_unit_die->attrs[i++] = *stmt_list;
7069 if (low_pc != NULL)
7070 comp_unit_die->attrs[i++] = *low_pc;
7071 if (high_pc != NULL)
7072 comp_unit_die->attrs[i++] = *high_pc;
7073 if (ranges != NULL)
7074 comp_unit_die->attrs[i++] = *ranges;
7075 if (comp_dir != NULL)
7076 comp_unit_die->attrs[i++] = *comp_dir;
7077 comp_unit_die->num_attrs += num_extra_attrs;
7078
7079 if (dwarf_die_debug)
7080 {
7081 fprintf_unfiltered (gdb_stdlog,
7082 "Read die from %s@0x%x of %s:\n",
7083 section->get_name (),
7084 (unsigned) (begin_info_ptr - section->buffer),
7085 bfd_get_filename (abfd));
7086 dump_die (comp_unit_die, dwarf_die_debug);
7087 }
7088
7089 /* Skip dummy compilation units. */
7090 if (info_ptr >= begin_info_ptr + dwo_unit->length
7091 || peek_abbrev_code (abfd, info_ptr) == 0)
7092 return 0;
7093
7094 *result_info_ptr = info_ptr;
7095 return 1;
7096 }
7097
7098 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7099 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7100 signature is part of the header. */
7101 static gdb::optional<ULONGEST>
7102 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7103 {
7104 if (cu->header.version >= 5)
7105 return cu->header.signature;
7106 struct attribute *attr;
7107 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7108 if (attr == nullptr)
7109 return gdb::optional<ULONGEST> ();
7110 return DW_UNSND (attr);
7111 }
7112
7113 /* Subroutine of cutu_reader to simplify it.
7114 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7115 Returns NULL if the specified DWO unit cannot be found. */
7116
7117 static struct dwo_unit *
7118 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7119 struct die_info *comp_unit_die,
7120 const char *dwo_name)
7121 {
7122 struct dwarf2_cu *cu = this_cu->cu;
7123 struct dwo_unit *dwo_unit;
7124 const char *comp_dir;
7125
7126 gdb_assert (cu != NULL);
7127
7128 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7129 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7130 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7131
7132 if (this_cu->is_debug_types)
7133 {
7134 struct signatured_type *sig_type;
7135
7136 /* Since this_cu is the first member of struct signatured_type,
7137 we can go from a pointer to one to a pointer to the other. */
7138 sig_type = (struct signatured_type *) this_cu;
7139 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7140 }
7141 else
7142 {
7143 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7144 if (!signature.has_value ())
7145 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7146 " [in module %s]"),
7147 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7148 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7149 *signature);
7150 }
7151
7152 return dwo_unit;
7153 }
7154
7155 /* Subroutine of cutu_reader to simplify it.
7156 See it for a description of the parameters.
7157 Read a TU directly from a DWO file, bypassing the stub. */
7158
7159 void
7160 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7161 int use_existing_cu, int keep)
7162 {
7163 struct signatured_type *sig_type;
7164 struct die_reader_specs reader;
7165
7166 /* Verify we can do the following downcast, and that we have the
7167 data we need. */
7168 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7169 sig_type = (struct signatured_type *) this_cu;
7170 gdb_assert (sig_type->dwo_unit != NULL);
7171
7172 if (use_existing_cu && this_cu->cu != NULL)
7173 {
7174 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7175 /* There's no need to do the rereading_dwo_cu handling that
7176 cutu_reader does since we don't read the stub. */
7177 }
7178 else
7179 {
7180 /* If !use_existing_cu, this_cu->cu must be NULL. */
7181 gdb_assert (this_cu->cu == NULL);
7182 m_new_cu.reset (new dwarf2_cu (this_cu));
7183 }
7184
7185 /* A future optimization, if needed, would be to use an existing
7186 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7187 could share abbrev tables. */
7188
7189 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7190 NULL /* stub_comp_unit_die */,
7191 sig_type->dwo_unit->dwo_file->comp_dir,
7192 &reader, &info_ptr,
7193 &comp_unit_die,
7194 &m_dwo_abbrev_table) == 0)
7195 {
7196 /* Dummy die. */
7197 dummy_p = true;
7198 }
7199 }
7200
7201 /* Initialize a CU (or TU) and read its DIEs.
7202 If the CU defers to a DWO file, read the DWO file as well.
7203
7204 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7205 Otherwise the table specified in the comp unit header is read in and used.
7206 This is an optimization for when we already have the abbrev table.
7207
7208 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7209 Otherwise, a new CU is allocated with xmalloc.
7210
7211 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7212 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7213 end. */
7214
7215 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7216 struct abbrev_table *abbrev_table,
7217 int use_existing_cu, int keep,
7218 bool skip_partial)
7219 : die_reader_specs {},
7220 m_this_cu (this_cu),
7221 m_keep (keep)
7222 {
7223 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7224 struct objfile *objfile = dwarf2_per_objfile->objfile;
7225 struct dwarf2_section_info *section = this_cu->section;
7226 bfd *abfd = section->get_bfd_owner ();
7227 struct dwarf2_cu *cu;
7228 const gdb_byte *begin_info_ptr;
7229 struct signatured_type *sig_type = NULL;
7230 struct dwarf2_section_info *abbrev_section;
7231 /* Non-zero if CU currently points to a DWO file and we need to
7232 reread it. When this happens we need to reread the skeleton die
7233 before we can reread the DWO file (this only applies to CUs, not TUs). */
7234 int rereading_dwo_cu = 0;
7235
7236 if (dwarf_die_debug)
7237 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7238 this_cu->is_debug_types ? "type" : "comp",
7239 sect_offset_str (this_cu->sect_off));
7240
7241 if (use_existing_cu)
7242 gdb_assert (keep);
7243
7244 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7245 file (instead of going through the stub), short-circuit all of this. */
7246 if (this_cu->reading_dwo_directly)
7247 {
7248 /* Narrow down the scope of possibilities to have to understand. */
7249 gdb_assert (this_cu->is_debug_types);
7250 gdb_assert (abbrev_table == NULL);
7251 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7252 return;
7253 }
7254
7255 /* This is cheap if the section is already read in. */
7256 section->read (objfile);
7257
7258 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7259
7260 abbrev_section = get_abbrev_section_for_cu (this_cu);
7261
7262 if (use_existing_cu && this_cu->cu != NULL)
7263 {
7264 cu = this_cu->cu;
7265 /* If this CU is from a DWO file we need to start over, we need to
7266 refetch the attributes from the skeleton CU.
7267 This could be optimized by retrieving those attributes from when we
7268 were here the first time: the previous comp_unit_die was stored in
7269 comp_unit_obstack. But there's no data yet that we need this
7270 optimization. */
7271 if (cu->dwo_unit != NULL)
7272 rereading_dwo_cu = 1;
7273 }
7274 else
7275 {
7276 /* If !use_existing_cu, this_cu->cu must be NULL. */
7277 gdb_assert (this_cu->cu == NULL);
7278 m_new_cu.reset (new dwarf2_cu (this_cu));
7279 cu = m_new_cu.get ();
7280 }
7281
7282 /* Get the header. */
7283 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7284 {
7285 /* We already have the header, there's no need to read it in again. */
7286 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7287 }
7288 else
7289 {
7290 if (this_cu->is_debug_types)
7291 {
7292 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7293 &cu->header, section,
7294 abbrev_section, info_ptr,
7295 rcuh_kind::TYPE);
7296
7297 /* Since per_cu is the first member of struct signatured_type,
7298 we can go from a pointer to one to a pointer to the other. */
7299 sig_type = (struct signatured_type *) this_cu;
7300 gdb_assert (sig_type->signature == cu->header.signature);
7301 gdb_assert (sig_type->type_offset_in_tu
7302 == cu->header.type_cu_offset_in_tu);
7303 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7304
7305 /* LENGTH has not been set yet for type units if we're
7306 using .gdb_index. */
7307 this_cu->length = get_cu_length (&cu->header);
7308
7309 /* Establish the type offset that can be used to lookup the type. */
7310 sig_type->type_offset_in_section =
7311 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7312
7313 this_cu->dwarf_version = cu->header.version;
7314 }
7315 else
7316 {
7317 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7318 &cu->header, section,
7319 abbrev_section,
7320 info_ptr,
7321 rcuh_kind::COMPILE);
7322
7323 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7324 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7325 this_cu->dwarf_version = cu->header.version;
7326 }
7327 }
7328
7329 /* Skip dummy compilation units. */
7330 if (info_ptr >= begin_info_ptr + this_cu->length
7331 || peek_abbrev_code (abfd, info_ptr) == 0)
7332 {
7333 dummy_p = true;
7334 return;
7335 }
7336
7337 /* If we don't have them yet, read the abbrevs for this compilation unit.
7338 And if we need to read them now, make sure they're freed when we're
7339 done. */
7340 if (abbrev_table != NULL)
7341 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7342 else
7343 {
7344 m_abbrev_table_holder
7345 = abbrev_table::read (objfile, abbrev_section,
7346 cu->header.abbrev_sect_off);
7347 abbrev_table = m_abbrev_table_holder.get ();
7348 }
7349
7350 /* Read the top level CU/TU die. */
7351 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7352 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7353
7354 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7355 {
7356 dummy_p = true;
7357 return;
7358 }
7359
7360 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7361 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7362 table from the DWO file and pass the ownership over to us. It will be
7363 referenced from READER, so we must make sure to free it after we're done
7364 with READER.
7365
7366 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7367 DWO CU, that this test will fail (the attribute will not be present). */
7368 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7369 if (dwo_name != nullptr)
7370 {
7371 struct dwo_unit *dwo_unit;
7372 struct die_info *dwo_comp_unit_die;
7373
7374 if (comp_unit_die->has_children)
7375 {
7376 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7377 " has children (offset %s) [in module %s]"),
7378 sect_offset_str (this_cu->sect_off),
7379 bfd_get_filename (abfd));
7380 }
7381 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7382 if (dwo_unit != NULL)
7383 {
7384 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7385 comp_unit_die, NULL,
7386 this, &info_ptr,
7387 &dwo_comp_unit_die,
7388 &m_dwo_abbrev_table) == 0)
7389 {
7390 /* Dummy die. */
7391 dummy_p = true;
7392 return;
7393 }
7394 comp_unit_die = dwo_comp_unit_die;
7395 }
7396 else
7397 {
7398 /* Yikes, we couldn't find the rest of the DIE, we only have
7399 the stub. A complaint has already been logged. There's
7400 not much more we can do except pass on the stub DIE to
7401 die_reader_func. We don't want to throw an error on bad
7402 debug info. */
7403 }
7404 }
7405 }
7406
7407 cutu_reader::~cutu_reader ()
7408 {
7409 /* Done, clean up. */
7410 if (m_new_cu != NULL && m_keep && !dummy_p)
7411 {
7412 struct dwarf2_per_objfile *dwarf2_per_objfile
7413 = m_this_cu->dwarf2_per_objfile;
7414 /* Link this CU into read_in_chain. */
7415 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7416 dwarf2_per_objfile->read_in_chain = m_this_cu;
7417 /* The chain owns it now. */
7418 m_new_cu.release ();
7419 }
7420 }
7421
7422 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7423 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7424 assumed to have already done the lookup to find the DWO file).
7425
7426 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7427 THIS_CU->is_debug_types, but nothing else.
7428
7429 We fill in THIS_CU->length.
7430
7431 THIS_CU->cu is always freed when done.
7432 This is done in order to not leave THIS_CU->cu in a state where we have
7433 to care whether it refers to the "main" CU or the DWO CU.
7434
7435 When parent_cu is passed, it is used to provide a default value for
7436 str_offsets_base and addr_base from the parent. */
7437
7438 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7439 struct dwarf2_cu *parent_cu,
7440 struct dwo_file *dwo_file)
7441 : die_reader_specs {},
7442 m_this_cu (this_cu)
7443 {
7444 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7445 struct objfile *objfile = dwarf2_per_objfile->objfile;
7446 struct dwarf2_section_info *section = this_cu->section;
7447 bfd *abfd = section->get_bfd_owner ();
7448 struct dwarf2_section_info *abbrev_section;
7449 const gdb_byte *begin_info_ptr, *info_ptr;
7450
7451 if (dwarf_die_debug)
7452 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7453 this_cu->is_debug_types ? "type" : "comp",
7454 sect_offset_str (this_cu->sect_off));
7455
7456 gdb_assert (this_cu->cu == NULL);
7457
7458 abbrev_section = (dwo_file != NULL
7459 ? &dwo_file->sections.abbrev
7460 : get_abbrev_section_for_cu (this_cu));
7461
7462 /* This is cheap if the section is already read in. */
7463 section->read (objfile);
7464
7465 m_new_cu.reset (new dwarf2_cu (this_cu));
7466
7467 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7468 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7469 &m_new_cu->header, section,
7470 abbrev_section, info_ptr,
7471 (this_cu->is_debug_types
7472 ? rcuh_kind::TYPE
7473 : rcuh_kind::COMPILE));
7474
7475 if (parent_cu != nullptr)
7476 {
7477 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7478 m_new_cu->addr_base = parent_cu->addr_base;
7479 }
7480 this_cu->length = get_cu_length (&m_new_cu->header);
7481
7482 /* Skip dummy compilation units. */
7483 if (info_ptr >= begin_info_ptr + this_cu->length
7484 || peek_abbrev_code (abfd, info_ptr) == 0)
7485 {
7486 dummy_p = true;
7487 return;
7488 }
7489
7490 m_abbrev_table_holder
7491 = abbrev_table::read (objfile, abbrev_section,
7492 m_new_cu->header.abbrev_sect_off);
7493
7494 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7495 m_abbrev_table_holder.get ());
7496 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7497 }
7498
7499 \f
7500 /* Type Unit Groups.
7501
7502 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7503 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7504 so that all types coming from the same compilation (.o file) are grouped
7505 together. A future step could be to put the types in the same symtab as
7506 the CU the types ultimately came from. */
7507
7508 static hashval_t
7509 hash_type_unit_group (const void *item)
7510 {
7511 const struct type_unit_group *tu_group
7512 = (const struct type_unit_group *) item;
7513
7514 return hash_stmt_list_entry (&tu_group->hash);
7515 }
7516
7517 static int
7518 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7519 {
7520 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7521 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7522
7523 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7524 }
7525
7526 /* Allocate a hash table for type unit groups. */
7527
7528 static htab_up
7529 allocate_type_unit_groups_table (struct objfile *objfile)
7530 {
7531 return htab_up (htab_create_alloc (3,
7532 hash_type_unit_group,
7533 eq_type_unit_group,
7534 NULL, xcalloc, xfree));
7535 }
7536
7537 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7538 partial symtabs. We combine several TUs per psymtab to not let the size
7539 of any one psymtab grow too big. */
7540 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7541 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7542
7543 /* Helper routine for get_type_unit_group.
7544 Create the type_unit_group object used to hold one or more TUs. */
7545
7546 static struct type_unit_group *
7547 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7548 {
7549 struct dwarf2_per_objfile *dwarf2_per_objfile
7550 = cu->per_cu->dwarf2_per_objfile;
7551 struct objfile *objfile = dwarf2_per_objfile->objfile;
7552 struct dwarf2_per_cu_data *per_cu;
7553 struct type_unit_group *tu_group;
7554
7555 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7556 struct type_unit_group);
7557 per_cu = &tu_group->per_cu;
7558 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7559
7560 if (dwarf2_per_objfile->using_index)
7561 {
7562 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7563 struct dwarf2_per_cu_quick_data);
7564 }
7565 else
7566 {
7567 unsigned int line_offset = to_underlying (line_offset_struct);
7568 dwarf2_psymtab *pst;
7569 std::string name;
7570
7571 /* Give the symtab a useful name for debug purposes. */
7572 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7573 name = string_printf ("<type_units_%d>",
7574 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7575 else
7576 name = string_printf ("<type_units_at_0x%x>", line_offset);
7577
7578 pst = create_partial_symtab (per_cu, name.c_str ());
7579 pst->anonymous = true;
7580 }
7581
7582 tu_group->hash.dwo_unit = cu->dwo_unit;
7583 tu_group->hash.line_sect_off = line_offset_struct;
7584
7585 return tu_group;
7586 }
7587
7588 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7589 STMT_LIST is a DW_AT_stmt_list attribute. */
7590
7591 static struct type_unit_group *
7592 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7593 {
7594 struct dwarf2_per_objfile *dwarf2_per_objfile
7595 = cu->per_cu->dwarf2_per_objfile;
7596 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7597 struct type_unit_group *tu_group;
7598 void **slot;
7599 unsigned int line_offset;
7600 struct type_unit_group type_unit_group_for_lookup;
7601
7602 if (dwarf2_per_objfile->type_unit_groups == NULL)
7603 {
7604 dwarf2_per_objfile->type_unit_groups =
7605 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7606 }
7607
7608 /* Do we need to create a new group, or can we use an existing one? */
7609
7610 if (stmt_list)
7611 {
7612 line_offset = DW_UNSND (stmt_list);
7613 ++tu_stats->nr_symtab_sharers;
7614 }
7615 else
7616 {
7617 /* Ugh, no stmt_list. Rare, but we have to handle it.
7618 We can do various things here like create one group per TU or
7619 spread them over multiple groups to split up the expansion work.
7620 To avoid worst case scenarios (too many groups or too large groups)
7621 we, umm, group them in bunches. */
7622 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7623 | (tu_stats->nr_stmt_less_type_units
7624 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7625 ++tu_stats->nr_stmt_less_type_units;
7626 }
7627
7628 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7629 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7630 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7631 &type_unit_group_for_lookup, INSERT);
7632 if (*slot != NULL)
7633 {
7634 tu_group = (struct type_unit_group *) *slot;
7635 gdb_assert (tu_group != NULL);
7636 }
7637 else
7638 {
7639 sect_offset line_offset_struct = (sect_offset) line_offset;
7640 tu_group = create_type_unit_group (cu, line_offset_struct);
7641 *slot = tu_group;
7642 ++tu_stats->nr_symtabs;
7643 }
7644
7645 return tu_group;
7646 }
7647 \f
7648 /* Partial symbol tables. */
7649
7650 /* Create a psymtab named NAME and assign it to PER_CU.
7651
7652 The caller must fill in the following details:
7653 dirname, textlow, texthigh. */
7654
7655 static dwarf2_psymtab *
7656 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7657 {
7658 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7659 dwarf2_psymtab *pst;
7660
7661 pst = new dwarf2_psymtab (name, objfile, 0);
7662
7663 pst->psymtabs_addrmap_supported = true;
7664
7665 /* This is the glue that links PST into GDB's symbol API. */
7666 pst->per_cu_data = per_cu;
7667 per_cu->v.psymtab = pst;
7668
7669 return pst;
7670 }
7671
7672 /* DIE reader function for process_psymtab_comp_unit. */
7673
7674 static void
7675 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7676 const gdb_byte *info_ptr,
7677 struct die_info *comp_unit_die,
7678 int want_partial_unit,
7679 enum language pretend_language)
7680 {
7681 struct dwarf2_cu *cu = reader->cu;
7682 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7684 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7685 CORE_ADDR baseaddr;
7686 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7687 dwarf2_psymtab *pst;
7688 enum pc_bounds_kind cu_bounds_kind;
7689 const char *filename;
7690
7691 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7692 return;
7693
7694 gdb_assert (! per_cu->is_debug_types);
7695
7696 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7697
7698 /* Allocate a new partial symbol table structure. */
7699 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7700 if (filename == NULL)
7701 filename = "";
7702
7703 pst = create_partial_symtab (per_cu, filename);
7704
7705 /* This must be done before calling dwarf2_build_include_psymtabs. */
7706 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7707
7708 baseaddr = objfile->text_section_offset ();
7709
7710 dwarf2_find_base_address (comp_unit_die, cu);
7711
7712 /* Possibly set the default values of LOWPC and HIGHPC from
7713 `DW_AT_ranges'. */
7714 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7715 &best_highpc, cu, pst);
7716 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7717 {
7718 CORE_ADDR low
7719 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7720 - baseaddr);
7721 CORE_ADDR high
7722 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7723 - baseaddr - 1);
7724 /* Store the contiguous range if it is not empty; it can be
7725 empty for CUs with no code. */
7726 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7727 low, high, pst);
7728 }
7729
7730 /* Check if comp unit has_children.
7731 If so, read the rest of the partial symbols from this comp unit.
7732 If not, there's no more debug_info for this comp unit. */
7733 if (comp_unit_die->has_children)
7734 {
7735 struct partial_die_info *first_die;
7736 CORE_ADDR lowpc, highpc;
7737
7738 lowpc = ((CORE_ADDR) -1);
7739 highpc = ((CORE_ADDR) 0);
7740
7741 first_die = load_partial_dies (reader, info_ptr, 1);
7742
7743 scan_partial_symbols (first_die, &lowpc, &highpc,
7744 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7745
7746 /* If we didn't find a lowpc, set it to highpc to avoid
7747 complaints from `maint check'. */
7748 if (lowpc == ((CORE_ADDR) -1))
7749 lowpc = highpc;
7750
7751 /* If the compilation unit didn't have an explicit address range,
7752 then use the information extracted from its child dies. */
7753 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7754 {
7755 best_lowpc = lowpc;
7756 best_highpc = highpc;
7757 }
7758 }
7759 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7760 best_lowpc + baseaddr)
7761 - baseaddr);
7762 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7763 best_highpc + baseaddr)
7764 - baseaddr);
7765
7766 end_psymtab_common (objfile, pst);
7767
7768 if (!cu->per_cu->imported_symtabs_empty ())
7769 {
7770 int i;
7771 int len = cu->per_cu->imported_symtabs_size ();
7772
7773 /* Fill in 'dependencies' here; we fill in 'users' in a
7774 post-pass. */
7775 pst->number_of_dependencies = len;
7776 pst->dependencies
7777 = objfile->partial_symtabs->allocate_dependencies (len);
7778 for (i = 0; i < len; ++i)
7779 {
7780 pst->dependencies[i]
7781 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7782 }
7783
7784 cu->per_cu->imported_symtabs_free ();
7785 }
7786
7787 /* Get the list of files included in the current compilation unit,
7788 and build a psymtab for each of them. */
7789 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7790
7791 if (dwarf_read_debug)
7792 fprintf_unfiltered (gdb_stdlog,
7793 "Psymtab for %s unit @%s: %s - %s"
7794 ", %d global, %d static syms\n",
7795 per_cu->is_debug_types ? "type" : "comp",
7796 sect_offset_str (per_cu->sect_off),
7797 paddress (gdbarch, pst->text_low (objfile)),
7798 paddress (gdbarch, pst->text_high (objfile)),
7799 pst->n_global_syms, pst->n_static_syms);
7800 }
7801
7802 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7803 Process compilation unit THIS_CU for a psymtab. */
7804
7805 static void
7806 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7807 int want_partial_unit,
7808 enum language pretend_language)
7809 {
7810 /* If this compilation unit was already read in, free the
7811 cached copy in order to read it in again. This is
7812 necessary because we skipped some symbols when we first
7813 read in the compilation unit (see load_partial_dies).
7814 This problem could be avoided, but the benefit is unclear. */
7815 if (this_cu->cu != NULL)
7816 free_one_cached_comp_unit (this_cu);
7817
7818 cutu_reader reader (this_cu, NULL, 0, 0, false);
7819
7820 if (reader.dummy_p)
7821 {
7822 /* Nothing. */
7823 }
7824 else if (this_cu->is_debug_types)
7825 build_type_psymtabs_reader (&reader, reader.info_ptr,
7826 reader.comp_unit_die);
7827 else
7828 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7829 reader.comp_unit_die,
7830 want_partial_unit,
7831 pretend_language);
7832
7833 /* Age out any secondary CUs. */
7834 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7835 }
7836
7837 /* Reader function for build_type_psymtabs. */
7838
7839 static void
7840 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7841 const gdb_byte *info_ptr,
7842 struct die_info *type_unit_die)
7843 {
7844 struct dwarf2_per_objfile *dwarf2_per_objfile
7845 = reader->cu->per_cu->dwarf2_per_objfile;
7846 struct objfile *objfile = dwarf2_per_objfile->objfile;
7847 struct dwarf2_cu *cu = reader->cu;
7848 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7849 struct signatured_type *sig_type;
7850 struct type_unit_group *tu_group;
7851 struct attribute *attr;
7852 struct partial_die_info *first_die;
7853 CORE_ADDR lowpc, highpc;
7854 dwarf2_psymtab *pst;
7855
7856 gdb_assert (per_cu->is_debug_types);
7857 sig_type = (struct signatured_type *) per_cu;
7858
7859 if (! type_unit_die->has_children)
7860 return;
7861
7862 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7863 tu_group = get_type_unit_group (cu, attr);
7864
7865 if (tu_group->tus == nullptr)
7866 tu_group->tus = new std::vector<signatured_type *>;
7867 tu_group->tus->push_back (sig_type);
7868
7869 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7870 pst = create_partial_symtab (per_cu, "");
7871 pst->anonymous = true;
7872
7873 first_die = load_partial_dies (reader, info_ptr, 1);
7874
7875 lowpc = (CORE_ADDR) -1;
7876 highpc = (CORE_ADDR) 0;
7877 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7878
7879 end_psymtab_common (objfile, pst);
7880 }
7881
7882 /* Struct used to sort TUs by their abbreviation table offset. */
7883
7884 struct tu_abbrev_offset
7885 {
7886 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7887 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7888 {}
7889
7890 signatured_type *sig_type;
7891 sect_offset abbrev_offset;
7892 };
7893
7894 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7895
7896 static bool
7897 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7898 const struct tu_abbrev_offset &b)
7899 {
7900 return a.abbrev_offset < b.abbrev_offset;
7901 }
7902
7903 /* Efficiently read all the type units.
7904 This does the bulk of the work for build_type_psymtabs.
7905
7906 The efficiency is because we sort TUs by the abbrev table they use and
7907 only read each abbrev table once. In one program there are 200K TUs
7908 sharing 8K abbrev tables.
7909
7910 The main purpose of this function is to support building the
7911 dwarf2_per_objfile->type_unit_groups table.
7912 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7913 can collapse the search space by grouping them by stmt_list.
7914 The savings can be significant, in the same program from above the 200K TUs
7915 share 8K stmt_list tables.
7916
7917 FUNC is expected to call get_type_unit_group, which will create the
7918 struct type_unit_group if necessary and add it to
7919 dwarf2_per_objfile->type_unit_groups. */
7920
7921 static void
7922 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7923 {
7924 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7925 abbrev_table_up abbrev_table;
7926 sect_offset abbrev_offset;
7927
7928 /* It's up to the caller to not call us multiple times. */
7929 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7930
7931 if (dwarf2_per_objfile->all_type_units.empty ())
7932 return;
7933
7934 /* TUs typically share abbrev tables, and there can be way more TUs than
7935 abbrev tables. Sort by abbrev table to reduce the number of times we
7936 read each abbrev table in.
7937 Alternatives are to punt or to maintain a cache of abbrev tables.
7938 This is simpler and efficient enough for now.
7939
7940 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7941 symtab to use). Typically TUs with the same abbrev offset have the same
7942 stmt_list value too so in practice this should work well.
7943
7944 The basic algorithm here is:
7945
7946 sort TUs by abbrev table
7947 for each TU with same abbrev table:
7948 read abbrev table if first user
7949 read TU top level DIE
7950 [IWBN if DWO skeletons had DW_AT_stmt_list]
7951 call FUNC */
7952
7953 if (dwarf_read_debug)
7954 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7955
7956 /* Sort in a separate table to maintain the order of all_type_units
7957 for .gdb_index: TU indices directly index all_type_units. */
7958 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7959 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7960
7961 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7962 sorted_by_abbrev.emplace_back
7963 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7964 sig_type->per_cu.section,
7965 sig_type->per_cu.sect_off));
7966
7967 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7968 sort_tu_by_abbrev_offset);
7969
7970 abbrev_offset = (sect_offset) ~(unsigned) 0;
7971
7972 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7973 {
7974 /* Switch to the next abbrev table if necessary. */
7975 if (abbrev_table == NULL
7976 || tu.abbrev_offset != abbrev_offset)
7977 {
7978 abbrev_offset = tu.abbrev_offset;
7979 abbrev_table =
7980 abbrev_table::read (dwarf2_per_objfile->objfile,
7981 &dwarf2_per_objfile->abbrev,
7982 abbrev_offset);
7983 ++tu_stats->nr_uniq_abbrev_tables;
7984 }
7985
7986 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7987 0, 0, false);
7988 if (!reader.dummy_p)
7989 build_type_psymtabs_reader (&reader, reader.info_ptr,
7990 reader.comp_unit_die);
7991 }
7992 }
7993
7994 /* Print collected type unit statistics. */
7995
7996 static void
7997 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7998 {
7999 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8000
8001 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8002 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8003 dwarf2_per_objfile->all_type_units.size ());
8004 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8005 tu_stats->nr_uniq_abbrev_tables);
8006 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8007 tu_stats->nr_symtabs);
8008 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8009 tu_stats->nr_symtab_sharers);
8010 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8011 tu_stats->nr_stmt_less_type_units);
8012 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8013 tu_stats->nr_all_type_units_reallocs);
8014 }
8015
8016 /* Traversal function for build_type_psymtabs. */
8017
8018 static int
8019 build_type_psymtab_dependencies (void **slot, void *info)
8020 {
8021 struct dwarf2_per_objfile *dwarf2_per_objfile
8022 = (struct dwarf2_per_objfile *) info;
8023 struct objfile *objfile = dwarf2_per_objfile->objfile;
8024 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8025 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8026 dwarf2_psymtab *pst = per_cu->v.psymtab;
8027 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8028 int i;
8029
8030 gdb_assert (len > 0);
8031 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8032
8033 pst->number_of_dependencies = len;
8034 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8035 for (i = 0; i < len; ++i)
8036 {
8037 struct signatured_type *iter = tu_group->tus->at (i);
8038 gdb_assert (iter->per_cu.is_debug_types);
8039 pst->dependencies[i] = iter->per_cu.v.psymtab;
8040 iter->type_unit_group = tu_group;
8041 }
8042
8043 delete tu_group->tus;
8044 tu_group->tus = nullptr;
8045
8046 return 1;
8047 }
8048
8049 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8050 Build partial symbol tables for the .debug_types comp-units. */
8051
8052 static void
8053 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8054 {
8055 if (! create_all_type_units (dwarf2_per_objfile))
8056 return;
8057
8058 build_type_psymtabs_1 (dwarf2_per_objfile);
8059 }
8060
8061 /* Traversal function for process_skeletonless_type_unit.
8062 Read a TU in a DWO file and build partial symbols for it. */
8063
8064 static int
8065 process_skeletonless_type_unit (void **slot, void *info)
8066 {
8067 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8068 struct dwarf2_per_objfile *dwarf2_per_objfile
8069 = (struct dwarf2_per_objfile *) info;
8070 struct signatured_type find_entry, *entry;
8071
8072 /* If this TU doesn't exist in the global table, add it and read it in. */
8073
8074 if (dwarf2_per_objfile->signatured_types == NULL)
8075 {
8076 dwarf2_per_objfile->signatured_types
8077 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8078 }
8079
8080 find_entry.signature = dwo_unit->signature;
8081 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
8082 &find_entry, INSERT);
8083 /* If we've already seen this type there's nothing to do. What's happening
8084 is we're doing our own version of comdat-folding here. */
8085 if (*slot != NULL)
8086 return 1;
8087
8088 /* This does the job that create_all_type_units would have done for
8089 this TU. */
8090 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8091 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8092 *slot = entry;
8093
8094 /* This does the job that build_type_psymtabs_1 would have done. */
8095 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8096 if (!reader.dummy_p)
8097 build_type_psymtabs_reader (&reader, reader.info_ptr,
8098 reader.comp_unit_die);
8099
8100 return 1;
8101 }
8102
8103 /* Traversal function for process_skeletonless_type_units. */
8104
8105 static int
8106 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8107 {
8108 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8109
8110 if (dwo_file->tus != NULL)
8111 htab_traverse_noresize (dwo_file->tus.get (),
8112 process_skeletonless_type_unit, info);
8113
8114 return 1;
8115 }
8116
8117 /* Scan all TUs of DWO files, verifying we've processed them.
8118 This is needed in case a TU was emitted without its skeleton.
8119 Note: This can't be done until we know what all the DWO files are. */
8120
8121 static void
8122 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8123 {
8124 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8125 if (get_dwp_file (dwarf2_per_objfile) == NULL
8126 && dwarf2_per_objfile->dwo_files != NULL)
8127 {
8128 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8129 process_dwo_file_for_skeletonless_type_units,
8130 dwarf2_per_objfile);
8131 }
8132 }
8133
8134 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8135
8136 static void
8137 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8138 {
8139 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8140 {
8141 dwarf2_psymtab *pst = per_cu->v.psymtab;
8142
8143 if (pst == NULL)
8144 continue;
8145
8146 for (int j = 0; j < pst->number_of_dependencies; ++j)
8147 {
8148 /* Set the 'user' field only if it is not already set. */
8149 if (pst->dependencies[j]->user == NULL)
8150 pst->dependencies[j]->user = pst;
8151 }
8152 }
8153 }
8154
8155 /* Build the partial symbol table by doing a quick pass through the
8156 .debug_info and .debug_abbrev sections. */
8157
8158 static void
8159 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8160 {
8161 struct objfile *objfile = dwarf2_per_objfile->objfile;
8162
8163 if (dwarf_read_debug)
8164 {
8165 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8166 objfile_name (objfile));
8167 }
8168
8169 dwarf2_per_objfile->reading_partial_symbols = 1;
8170
8171 dwarf2_per_objfile->info.read (objfile);
8172
8173 /* Any cached compilation units will be linked by the per-objfile
8174 read_in_chain. Make sure to free them when we're done. */
8175 free_cached_comp_units freer (dwarf2_per_objfile);
8176
8177 build_type_psymtabs (dwarf2_per_objfile);
8178
8179 create_all_comp_units (dwarf2_per_objfile);
8180
8181 /* Create a temporary address map on a temporary obstack. We later
8182 copy this to the final obstack. */
8183 auto_obstack temp_obstack;
8184
8185 scoped_restore save_psymtabs_addrmap
8186 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8187 addrmap_create_mutable (&temp_obstack));
8188
8189 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8190 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8191
8192 /* This has to wait until we read the CUs, we need the list of DWOs. */
8193 process_skeletonless_type_units (dwarf2_per_objfile);
8194
8195 /* Now that all TUs have been processed we can fill in the dependencies. */
8196 if (dwarf2_per_objfile->type_unit_groups != NULL)
8197 {
8198 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
8199 build_type_psymtab_dependencies, dwarf2_per_objfile);
8200 }
8201
8202 if (dwarf_read_debug)
8203 print_tu_stats (dwarf2_per_objfile);
8204
8205 set_partial_user (dwarf2_per_objfile);
8206
8207 objfile->partial_symtabs->psymtabs_addrmap
8208 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8209 objfile->partial_symtabs->obstack ());
8210 /* At this point we want to keep the address map. */
8211 save_psymtabs_addrmap.release ();
8212
8213 if (dwarf_read_debug)
8214 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8215 objfile_name (objfile));
8216 }
8217
8218 /* Load the partial DIEs for a secondary CU into memory.
8219 This is also used when rereading a primary CU with load_all_dies. */
8220
8221 static void
8222 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8223 {
8224 cutu_reader reader (this_cu, NULL, 1, 1, false);
8225
8226 if (!reader.dummy_p)
8227 {
8228 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8229 language_minimal);
8230
8231 /* Check if comp unit has_children.
8232 If so, read the rest of the partial symbols from this comp unit.
8233 If not, there's no more debug_info for this comp unit. */
8234 if (reader.comp_unit_die->has_children)
8235 load_partial_dies (&reader, reader.info_ptr, 0);
8236 }
8237 }
8238
8239 static void
8240 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8241 struct dwarf2_section_info *section,
8242 struct dwarf2_section_info *abbrev_section,
8243 unsigned int is_dwz)
8244 {
8245 const gdb_byte *info_ptr;
8246 struct objfile *objfile = dwarf2_per_objfile->objfile;
8247
8248 if (dwarf_read_debug)
8249 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8250 section->get_name (),
8251 section->get_file_name ());
8252
8253 section->read (objfile);
8254
8255 info_ptr = section->buffer;
8256
8257 while (info_ptr < section->buffer + section->size)
8258 {
8259 struct dwarf2_per_cu_data *this_cu;
8260
8261 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8262
8263 comp_unit_head cu_header;
8264 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8265 abbrev_section, info_ptr,
8266 rcuh_kind::COMPILE);
8267
8268 /* Save the compilation unit for later lookup. */
8269 if (cu_header.unit_type != DW_UT_type)
8270 {
8271 this_cu = XOBNEW (&objfile->objfile_obstack,
8272 struct dwarf2_per_cu_data);
8273 memset (this_cu, 0, sizeof (*this_cu));
8274 }
8275 else
8276 {
8277 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8278 struct signatured_type);
8279 memset (sig_type, 0, sizeof (*sig_type));
8280 sig_type->signature = cu_header.signature;
8281 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8282 this_cu = &sig_type->per_cu;
8283 }
8284 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8285 this_cu->sect_off = sect_off;
8286 this_cu->length = cu_header.length + cu_header.initial_length_size;
8287 this_cu->is_dwz = is_dwz;
8288 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8289 this_cu->section = section;
8290
8291 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8292
8293 info_ptr = info_ptr + this_cu->length;
8294 }
8295 }
8296
8297 /* Create a list of all compilation units in OBJFILE.
8298 This is only done for -readnow and building partial symtabs. */
8299
8300 static void
8301 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8302 {
8303 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8304 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8305 &dwarf2_per_objfile->abbrev, 0);
8306
8307 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8308 if (dwz != NULL)
8309 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8310 1);
8311 }
8312
8313 /* Process all loaded DIEs for compilation unit CU, starting at
8314 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8315 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8316 DW_AT_ranges). See the comments of add_partial_subprogram on how
8317 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8318
8319 static void
8320 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8321 CORE_ADDR *highpc, int set_addrmap,
8322 struct dwarf2_cu *cu)
8323 {
8324 struct partial_die_info *pdi;
8325
8326 /* Now, march along the PDI's, descending into ones which have
8327 interesting children but skipping the children of the other ones,
8328 until we reach the end of the compilation unit. */
8329
8330 pdi = first_die;
8331
8332 while (pdi != NULL)
8333 {
8334 pdi->fixup (cu);
8335
8336 /* Anonymous namespaces or modules have no name but have interesting
8337 children, so we need to look at them. Ditto for anonymous
8338 enums. */
8339
8340 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8341 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8342 || pdi->tag == DW_TAG_imported_unit
8343 || pdi->tag == DW_TAG_inlined_subroutine)
8344 {
8345 switch (pdi->tag)
8346 {
8347 case DW_TAG_subprogram:
8348 case DW_TAG_inlined_subroutine:
8349 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8350 break;
8351 case DW_TAG_constant:
8352 case DW_TAG_variable:
8353 case DW_TAG_typedef:
8354 case DW_TAG_union_type:
8355 if (!pdi->is_declaration)
8356 {
8357 add_partial_symbol (pdi, cu);
8358 }
8359 break;
8360 case DW_TAG_class_type:
8361 case DW_TAG_interface_type:
8362 case DW_TAG_structure_type:
8363 if (!pdi->is_declaration)
8364 {
8365 add_partial_symbol (pdi, cu);
8366 }
8367 if ((cu->language == language_rust
8368 || cu->language == language_cplus) && pdi->has_children)
8369 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8370 set_addrmap, cu);
8371 break;
8372 case DW_TAG_enumeration_type:
8373 if (!pdi->is_declaration)
8374 add_partial_enumeration (pdi, cu);
8375 break;
8376 case DW_TAG_base_type:
8377 case DW_TAG_subrange_type:
8378 /* File scope base type definitions are added to the partial
8379 symbol table. */
8380 add_partial_symbol (pdi, cu);
8381 break;
8382 case DW_TAG_namespace:
8383 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8384 break;
8385 case DW_TAG_module:
8386 if (!pdi->is_declaration)
8387 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8388 break;
8389 case DW_TAG_imported_unit:
8390 {
8391 struct dwarf2_per_cu_data *per_cu;
8392
8393 /* For now we don't handle imported units in type units. */
8394 if (cu->per_cu->is_debug_types)
8395 {
8396 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8397 " supported in type units [in module %s]"),
8398 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8399 }
8400
8401 per_cu = dwarf2_find_containing_comp_unit
8402 (pdi->d.sect_off, pdi->is_dwz,
8403 cu->per_cu->dwarf2_per_objfile);
8404
8405 /* Go read the partial unit, if needed. */
8406 if (per_cu->v.psymtab == NULL)
8407 process_psymtab_comp_unit (per_cu, 1, cu->language);
8408
8409 cu->per_cu->imported_symtabs_push (per_cu);
8410 }
8411 break;
8412 case DW_TAG_imported_declaration:
8413 add_partial_symbol (pdi, cu);
8414 break;
8415 default:
8416 break;
8417 }
8418 }
8419
8420 /* If the die has a sibling, skip to the sibling. */
8421
8422 pdi = pdi->die_sibling;
8423 }
8424 }
8425
8426 /* Functions used to compute the fully scoped name of a partial DIE.
8427
8428 Normally, this is simple. For C++, the parent DIE's fully scoped
8429 name is concatenated with "::" and the partial DIE's name.
8430 Enumerators are an exception; they use the scope of their parent
8431 enumeration type, i.e. the name of the enumeration type is not
8432 prepended to the enumerator.
8433
8434 There are two complexities. One is DW_AT_specification; in this
8435 case "parent" means the parent of the target of the specification,
8436 instead of the direct parent of the DIE. The other is compilers
8437 which do not emit DW_TAG_namespace; in this case we try to guess
8438 the fully qualified name of structure types from their members'
8439 linkage names. This must be done using the DIE's children rather
8440 than the children of any DW_AT_specification target. We only need
8441 to do this for structures at the top level, i.e. if the target of
8442 any DW_AT_specification (if any; otherwise the DIE itself) does not
8443 have a parent. */
8444
8445 /* Compute the scope prefix associated with PDI's parent, in
8446 compilation unit CU. The result will be allocated on CU's
8447 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8448 field. NULL is returned if no prefix is necessary. */
8449 static const char *
8450 partial_die_parent_scope (struct partial_die_info *pdi,
8451 struct dwarf2_cu *cu)
8452 {
8453 const char *grandparent_scope;
8454 struct partial_die_info *parent, *real_pdi;
8455
8456 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8457 then this means the parent of the specification DIE. */
8458
8459 real_pdi = pdi;
8460 while (real_pdi->has_specification)
8461 {
8462 auto res = find_partial_die (real_pdi->spec_offset,
8463 real_pdi->spec_is_dwz, cu);
8464 real_pdi = res.pdi;
8465 cu = res.cu;
8466 }
8467
8468 parent = real_pdi->die_parent;
8469 if (parent == NULL)
8470 return NULL;
8471
8472 if (parent->scope_set)
8473 return parent->scope;
8474
8475 parent->fixup (cu);
8476
8477 grandparent_scope = partial_die_parent_scope (parent, cu);
8478
8479 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8480 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8481 Work around this problem here. */
8482 if (cu->language == language_cplus
8483 && parent->tag == DW_TAG_namespace
8484 && strcmp (parent->name, "::") == 0
8485 && grandparent_scope == NULL)
8486 {
8487 parent->scope = NULL;
8488 parent->scope_set = 1;
8489 return NULL;
8490 }
8491
8492 /* Nested subroutines in Fortran get a prefix. */
8493 if (pdi->tag == DW_TAG_enumerator)
8494 /* Enumerators should not get the name of the enumeration as a prefix. */
8495 parent->scope = grandparent_scope;
8496 else if (parent->tag == DW_TAG_namespace
8497 || parent->tag == DW_TAG_module
8498 || parent->tag == DW_TAG_structure_type
8499 || parent->tag == DW_TAG_class_type
8500 || parent->tag == DW_TAG_interface_type
8501 || parent->tag == DW_TAG_union_type
8502 || parent->tag == DW_TAG_enumeration_type
8503 || (cu->language == language_fortran
8504 && parent->tag == DW_TAG_subprogram
8505 && pdi->tag == DW_TAG_subprogram))
8506 {
8507 if (grandparent_scope == NULL)
8508 parent->scope = parent->name;
8509 else
8510 parent->scope = typename_concat (&cu->comp_unit_obstack,
8511 grandparent_scope,
8512 parent->name, 0, cu);
8513 }
8514 else
8515 {
8516 /* FIXME drow/2004-04-01: What should we be doing with
8517 function-local names? For partial symbols, we should probably be
8518 ignoring them. */
8519 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8520 dwarf_tag_name (parent->tag),
8521 sect_offset_str (pdi->sect_off));
8522 parent->scope = grandparent_scope;
8523 }
8524
8525 parent->scope_set = 1;
8526 return parent->scope;
8527 }
8528
8529 /* Return the fully scoped name associated with PDI, from compilation unit
8530 CU. The result will be allocated with malloc. */
8531
8532 static gdb::unique_xmalloc_ptr<char>
8533 partial_die_full_name (struct partial_die_info *pdi,
8534 struct dwarf2_cu *cu)
8535 {
8536 const char *parent_scope;
8537
8538 /* If this is a template instantiation, we can not work out the
8539 template arguments from partial DIEs. So, unfortunately, we have
8540 to go through the full DIEs. At least any work we do building
8541 types here will be reused if full symbols are loaded later. */
8542 if (pdi->has_template_arguments)
8543 {
8544 pdi->fixup (cu);
8545
8546 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8547 {
8548 struct die_info *die;
8549 struct attribute attr;
8550 struct dwarf2_cu *ref_cu = cu;
8551
8552 /* DW_FORM_ref_addr is using section offset. */
8553 attr.name = (enum dwarf_attribute) 0;
8554 attr.form = DW_FORM_ref_addr;
8555 attr.u.unsnd = to_underlying (pdi->sect_off);
8556 die = follow_die_ref (NULL, &attr, &ref_cu);
8557
8558 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8559 }
8560 }
8561
8562 parent_scope = partial_die_parent_scope (pdi, cu);
8563 if (parent_scope == NULL)
8564 return NULL;
8565 else
8566 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8567 pdi->name, 0, cu));
8568 }
8569
8570 static void
8571 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8572 {
8573 struct dwarf2_per_objfile *dwarf2_per_objfile
8574 = cu->per_cu->dwarf2_per_objfile;
8575 struct objfile *objfile = dwarf2_per_objfile->objfile;
8576 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8577 CORE_ADDR addr = 0;
8578 const char *actual_name = NULL;
8579 CORE_ADDR baseaddr;
8580
8581 baseaddr = objfile->text_section_offset ();
8582
8583 gdb::unique_xmalloc_ptr<char> built_actual_name
8584 = partial_die_full_name (pdi, cu);
8585 if (built_actual_name != NULL)
8586 actual_name = built_actual_name.get ();
8587
8588 if (actual_name == NULL)
8589 actual_name = pdi->name;
8590
8591 switch (pdi->tag)
8592 {
8593 case DW_TAG_inlined_subroutine:
8594 case DW_TAG_subprogram:
8595 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8596 - baseaddr);
8597 if (pdi->is_external
8598 || cu->language == language_ada
8599 || (cu->language == language_fortran
8600 && pdi->die_parent != NULL
8601 && pdi->die_parent->tag == DW_TAG_subprogram))
8602 {
8603 /* Normally, only "external" DIEs are part of the global scope.
8604 But in Ada and Fortran, we want to be able to access nested
8605 procedures globally. So all Ada and Fortran subprograms are
8606 stored in the global scope. */
8607 add_psymbol_to_list (actual_name,
8608 built_actual_name != NULL,
8609 VAR_DOMAIN, LOC_BLOCK,
8610 SECT_OFF_TEXT (objfile),
8611 psymbol_placement::GLOBAL,
8612 addr,
8613 cu->language, objfile);
8614 }
8615 else
8616 {
8617 add_psymbol_to_list (actual_name,
8618 built_actual_name != NULL,
8619 VAR_DOMAIN, LOC_BLOCK,
8620 SECT_OFF_TEXT (objfile),
8621 psymbol_placement::STATIC,
8622 addr, cu->language, objfile);
8623 }
8624
8625 if (pdi->main_subprogram && actual_name != NULL)
8626 set_objfile_main_name (objfile, actual_name, cu->language);
8627 break;
8628 case DW_TAG_constant:
8629 add_psymbol_to_list (actual_name,
8630 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8631 -1, (pdi->is_external
8632 ? psymbol_placement::GLOBAL
8633 : psymbol_placement::STATIC),
8634 0, cu->language, objfile);
8635 break;
8636 case DW_TAG_variable:
8637 if (pdi->d.locdesc)
8638 addr = decode_locdesc (pdi->d.locdesc, cu);
8639
8640 if (pdi->d.locdesc
8641 && addr == 0
8642 && !dwarf2_per_objfile->has_section_at_zero)
8643 {
8644 /* A global or static variable may also have been stripped
8645 out by the linker if unused, in which case its address
8646 will be nullified; do not add such variables into partial
8647 symbol table then. */
8648 }
8649 else if (pdi->is_external)
8650 {
8651 /* Global Variable.
8652 Don't enter into the minimal symbol tables as there is
8653 a minimal symbol table entry from the ELF symbols already.
8654 Enter into partial symbol table if it has a location
8655 descriptor or a type.
8656 If the location descriptor is missing, new_symbol will create
8657 a LOC_UNRESOLVED symbol, the address of the variable will then
8658 be determined from the minimal symbol table whenever the variable
8659 is referenced.
8660 The address for the partial symbol table entry is not
8661 used by GDB, but it comes in handy for debugging partial symbol
8662 table building. */
8663
8664 if (pdi->d.locdesc || pdi->has_type)
8665 add_psymbol_to_list (actual_name,
8666 built_actual_name != NULL,
8667 VAR_DOMAIN, LOC_STATIC,
8668 SECT_OFF_TEXT (objfile),
8669 psymbol_placement::GLOBAL,
8670 addr, cu->language, objfile);
8671 }
8672 else
8673 {
8674 int has_loc = pdi->d.locdesc != NULL;
8675
8676 /* Static Variable. Skip symbols whose value we cannot know (those
8677 without location descriptors or constant values). */
8678 if (!has_loc && !pdi->has_const_value)
8679 return;
8680
8681 add_psymbol_to_list (actual_name,
8682 built_actual_name != NULL,
8683 VAR_DOMAIN, LOC_STATIC,
8684 SECT_OFF_TEXT (objfile),
8685 psymbol_placement::STATIC,
8686 has_loc ? addr : 0,
8687 cu->language, objfile);
8688 }
8689 break;
8690 case DW_TAG_typedef:
8691 case DW_TAG_base_type:
8692 case DW_TAG_subrange_type:
8693 add_psymbol_to_list (actual_name,
8694 built_actual_name != NULL,
8695 VAR_DOMAIN, LOC_TYPEDEF, -1,
8696 psymbol_placement::STATIC,
8697 0, cu->language, objfile);
8698 break;
8699 case DW_TAG_imported_declaration:
8700 case DW_TAG_namespace:
8701 add_psymbol_to_list (actual_name,
8702 built_actual_name != NULL,
8703 VAR_DOMAIN, LOC_TYPEDEF, -1,
8704 psymbol_placement::GLOBAL,
8705 0, cu->language, objfile);
8706 break;
8707 case DW_TAG_module:
8708 /* With Fortran 77 there might be a "BLOCK DATA" module
8709 available without any name. If so, we skip the module as it
8710 doesn't bring any value. */
8711 if (actual_name != nullptr)
8712 add_psymbol_to_list (actual_name,
8713 built_actual_name != NULL,
8714 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8715 psymbol_placement::GLOBAL,
8716 0, cu->language, objfile);
8717 break;
8718 case DW_TAG_class_type:
8719 case DW_TAG_interface_type:
8720 case DW_TAG_structure_type:
8721 case DW_TAG_union_type:
8722 case DW_TAG_enumeration_type:
8723 /* Skip external references. The DWARF standard says in the section
8724 about "Structure, Union, and Class Type Entries": "An incomplete
8725 structure, union or class type is represented by a structure,
8726 union or class entry that does not have a byte size attribute
8727 and that has a DW_AT_declaration attribute." */
8728 if (!pdi->has_byte_size && pdi->is_declaration)
8729 return;
8730
8731 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8732 static vs. global. */
8733 add_psymbol_to_list (actual_name,
8734 built_actual_name != NULL,
8735 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8736 cu->language == language_cplus
8737 ? psymbol_placement::GLOBAL
8738 : psymbol_placement::STATIC,
8739 0, cu->language, objfile);
8740
8741 break;
8742 case DW_TAG_enumerator:
8743 add_psymbol_to_list (actual_name,
8744 built_actual_name != NULL,
8745 VAR_DOMAIN, LOC_CONST, -1,
8746 cu->language == language_cplus
8747 ? psymbol_placement::GLOBAL
8748 : psymbol_placement::STATIC,
8749 0, cu->language, objfile);
8750 break;
8751 default:
8752 break;
8753 }
8754 }
8755
8756 /* Read a partial die corresponding to a namespace; also, add a symbol
8757 corresponding to that namespace to the symbol table. NAMESPACE is
8758 the name of the enclosing namespace. */
8759
8760 static void
8761 add_partial_namespace (struct partial_die_info *pdi,
8762 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8763 int set_addrmap, struct dwarf2_cu *cu)
8764 {
8765 /* Add a symbol for the namespace. */
8766
8767 add_partial_symbol (pdi, cu);
8768
8769 /* Now scan partial symbols in that namespace. */
8770
8771 if (pdi->has_children)
8772 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8773 }
8774
8775 /* Read a partial die corresponding to a Fortran module. */
8776
8777 static void
8778 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8779 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8780 {
8781 /* Add a symbol for the namespace. */
8782
8783 add_partial_symbol (pdi, cu);
8784
8785 /* Now scan partial symbols in that module. */
8786
8787 if (pdi->has_children)
8788 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8789 }
8790
8791 /* Read a partial die corresponding to a subprogram or an inlined
8792 subprogram and create a partial symbol for that subprogram.
8793 When the CU language allows it, this routine also defines a partial
8794 symbol for each nested subprogram that this subprogram contains.
8795 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8796 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8797
8798 PDI may also be a lexical block, in which case we simply search
8799 recursively for subprograms defined inside that lexical block.
8800 Again, this is only performed when the CU language allows this
8801 type of definitions. */
8802
8803 static void
8804 add_partial_subprogram (struct partial_die_info *pdi,
8805 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8806 int set_addrmap, struct dwarf2_cu *cu)
8807 {
8808 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8809 {
8810 if (pdi->has_pc_info)
8811 {
8812 if (pdi->lowpc < *lowpc)
8813 *lowpc = pdi->lowpc;
8814 if (pdi->highpc > *highpc)
8815 *highpc = pdi->highpc;
8816 if (set_addrmap)
8817 {
8818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8820 CORE_ADDR baseaddr;
8821 CORE_ADDR this_highpc;
8822 CORE_ADDR this_lowpc;
8823
8824 baseaddr = objfile->text_section_offset ();
8825 this_lowpc
8826 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8827 pdi->lowpc + baseaddr)
8828 - baseaddr);
8829 this_highpc
8830 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8831 pdi->highpc + baseaddr)
8832 - baseaddr);
8833 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8834 this_lowpc, this_highpc - 1,
8835 cu->per_cu->v.psymtab);
8836 }
8837 }
8838
8839 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8840 {
8841 if (!pdi->is_declaration)
8842 /* Ignore subprogram DIEs that do not have a name, they are
8843 illegal. Do not emit a complaint at this point, we will
8844 do so when we convert this psymtab into a symtab. */
8845 if (pdi->name)
8846 add_partial_symbol (pdi, cu);
8847 }
8848 }
8849
8850 if (! pdi->has_children)
8851 return;
8852
8853 if (cu->language == language_ada || cu->language == language_fortran)
8854 {
8855 pdi = pdi->die_child;
8856 while (pdi != NULL)
8857 {
8858 pdi->fixup (cu);
8859 if (pdi->tag == DW_TAG_subprogram
8860 || pdi->tag == DW_TAG_inlined_subroutine
8861 || pdi->tag == DW_TAG_lexical_block)
8862 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8863 pdi = pdi->die_sibling;
8864 }
8865 }
8866 }
8867
8868 /* Read a partial die corresponding to an enumeration type. */
8869
8870 static void
8871 add_partial_enumeration (struct partial_die_info *enum_pdi,
8872 struct dwarf2_cu *cu)
8873 {
8874 struct partial_die_info *pdi;
8875
8876 if (enum_pdi->name != NULL)
8877 add_partial_symbol (enum_pdi, cu);
8878
8879 pdi = enum_pdi->die_child;
8880 while (pdi)
8881 {
8882 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8883 complaint (_("malformed enumerator DIE ignored"));
8884 else
8885 add_partial_symbol (pdi, cu);
8886 pdi = pdi->die_sibling;
8887 }
8888 }
8889
8890 /* Return the initial uleb128 in the die at INFO_PTR. */
8891
8892 static unsigned int
8893 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8894 {
8895 unsigned int bytes_read;
8896
8897 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8898 }
8899
8900 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8901 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8902
8903 Return the corresponding abbrev, or NULL if the number is zero (indicating
8904 an empty DIE). In either case *BYTES_READ will be set to the length of
8905 the initial number. */
8906
8907 static struct abbrev_info *
8908 peek_die_abbrev (const die_reader_specs &reader,
8909 const gdb_byte *info_ptr, unsigned int *bytes_read)
8910 {
8911 dwarf2_cu *cu = reader.cu;
8912 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8913 unsigned int abbrev_number
8914 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8915
8916 if (abbrev_number == 0)
8917 return NULL;
8918
8919 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8920 if (!abbrev)
8921 {
8922 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8923 " at offset %s [in module %s]"),
8924 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8925 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8926 }
8927
8928 return abbrev;
8929 }
8930
8931 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8932 Returns a pointer to the end of a series of DIEs, terminated by an empty
8933 DIE. Any children of the skipped DIEs will also be skipped. */
8934
8935 static const gdb_byte *
8936 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8937 {
8938 while (1)
8939 {
8940 unsigned int bytes_read;
8941 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8942
8943 if (abbrev == NULL)
8944 return info_ptr + bytes_read;
8945 else
8946 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8947 }
8948 }
8949
8950 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8951 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8952 abbrev corresponding to that skipped uleb128 should be passed in
8953 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8954 children. */
8955
8956 static const gdb_byte *
8957 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8958 struct abbrev_info *abbrev)
8959 {
8960 unsigned int bytes_read;
8961 struct attribute attr;
8962 bfd *abfd = reader->abfd;
8963 struct dwarf2_cu *cu = reader->cu;
8964 const gdb_byte *buffer = reader->buffer;
8965 const gdb_byte *buffer_end = reader->buffer_end;
8966 unsigned int form, i;
8967
8968 for (i = 0; i < abbrev->num_attrs; i++)
8969 {
8970 /* The only abbrev we care about is DW_AT_sibling. */
8971 if (abbrev->attrs[i].name == DW_AT_sibling)
8972 {
8973 bool ignored;
8974 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8975 &ignored);
8976 if (attr.form == DW_FORM_ref_addr)
8977 complaint (_("ignoring absolute DW_AT_sibling"));
8978 else
8979 {
8980 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8981 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8982
8983 if (sibling_ptr < info_ptr)
8984 complaint (_("DW_AT_sibling points backwards"));
8985 else if (sibling_ptr > reader->buffer_end)
8986 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8987 else
8988 return sibling_ptr;
8989 }
8990 }
8991
8992 /* If it isn't DW_AT_sibling, skip this attribute. */
8993 form = abbrev->attrs[i].form;
8994 skip_attribute:
8995 switch (form)
8996 {
8997 case DW_FORM_ref_addr:
8998 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8999 and later it is offset sized. */
9000 if (cu->header.version == 2)
9001 info_ptr += cu->header.addr_size;
9002 else
9003 info_ptr += cu->header.offset_size;
9004 break;
9005 case DW_FORM_GNU_ref_alt:
9006 info_ptr += cu->header.offset_size;
9007 break;
9008 case DW_FORM_addr:
9009 info_ptr += cu->header.addr_size;
9010 break;
9011 case DW_FORM_data1:
9012 case DW_FORM_ref1:
9013 case DW_FORM_flag:
9014 case DW_FORM_strx1:
9015 info_ptr += 1;
9016 break;
9017 case DW_FORM_flag_present:
9018 case DW_FORM_implicit_const:
9019 break;
9020 case DW_FORM_data2:
9021 case DW_FORM_ref2:
9022 case DW_FORM_strx2:
9023 info_ptr += 2;
9024 break;
9025 case DW_FORM_strx3:
9026 info_ptr += 3;
9027 break;
9028 case DW_FORM_data4:
9029 case DW_FORM_ref4:
9030 case DW_FORM_strx4:
9031 info_ptr += 4;
9032 break;
9033 case DW_FORM_data8:
9034 case DW_FORM_ref8:
9035 case DW_FORM_ref_sig8:
9036 info_ptr += 8;
9037 break;
9038 case DW_FORM_data16:
9039 info_ptr += 16;
9040 break;
9041 case DW_FORM_string:
9042 read_direct_string (abfd, info_ptr, &bytes_read);
9043 info_ptr += bytes_read;
9044 break;
9045 case DW_FORM_sec_offset:
9046 case DW_FORM_strp:
9047 case DW_FORM_GNU_strp_alt:
9048 info_ptr += cu->header.offset_size;
9049 break;
9050 case DW_FORM_exprloc:
9051 case DW_FORM_block:
9052 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9053 info_ptr += bytes_read;
9054 break;
9055 case DW_FORM_block1:
9056 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9057 break;
9058 case DW_FORM_block2:
9059 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9060 break;
9061 case DW_FORM_block4:
9062 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9063 break;
9064 case DW_FORM_addrx:
9065 case DW_FORM_strx:
9066 case DW_FORM_sdata:
9067 case DW_FORM_udata:
9068 case DW_FORM_ref_udata:
9069 case DW_FORM_GNU_addr_index:
9070 case DW_FORM_GNU_str_index:
9071 case DW_FORM_rnglistx:
9072 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9073 break;
9074 case DW_FORM_indirect:
9075 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9076 info_ptr += bytes_read;
9077 /* We need to continue parsing from here, so just go back to
9078 the top. */
9079 goto skip_attribute;
9080
9081 default:
9082 error (_("Dwarf Error: Cannot handle %s "
9083 "in DWARF reader [in module %s]"),
9084 dwarf_form_name (form),
9085 bfd_get_filename (abfd));
9086 }
9087 }
9088
9089 if (abbrev->has_children)
9090 return skip_children (reader, info_ptr);
9091 else
9092 return info_ptr;
9093 }
9094
9095 /* Locate ORIG_PDI's sibling.
9096 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9097
9098 static const gdb_byte *
9099 locate_pdi_sibling (const struct die_reader_specs *reader,
9100 struct partial_die_info *orig_pdi,
9101 const gdb_byte *info_ptr)
9102 {
9103 /* Do we know the sibling already? */
9104
9105 if (orig_pdi->sibling)
9106 return orig_pdi->sibling;
9107
9108 /* Are there any children to deal with? */
9109
9110 if (!orig_pdi->has_children)
9111 return info_ptr;
9112
9113 /* Skip the children the long way. */
9114
9115 return skip_children (reader, info_ptr);
9116 }
9117
9118 /* Expand this partial symbol table into a full symbol table. SELF is
9119 not NULL. */
9120
9121 void
9122 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9123 {
9124 struct dwarf2_per_objfile *dwarf2_per_objfile
9125 = get_dwarf2_per_objfile (objfile);
9126
9127 gdb_assert (!readin);
9128 /* If this psymtab is constructed from a debug-only objfile, the
9129 has_section_at_zero flag will not necessarily be correct. We
9130 can get the correct value for this flag by looking at the data
9131 associated with the (presumably stripped) associated objfile. */
9132 if (objfile->separate_debug_objfile_backlink)
9133 {
9134 struct dwarf2_per_objfile *dpo_backlink
9135 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9136
9137 dwarf2_per_objfile->has_section_at_zero
9138 = dpo_backlink->has_section_at_zero;
9139 }
9140
9141 dwarf2_per_objfile->reading_partial_symbols = 0;
9142
9143 expand_psymtab (objfile);
9144
9145 process_cu_includes (dwarf2_per_objfile);
9146 }
9147 \f
9148 /* Reading in full CUs. */
9149
9150 /* Add PER_CU to the queue. */
9151
9152 static void
9153 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9154 enum language pretend_language)
9155 {
9156 per_cu->queued = 1;
9157 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
9158 }
9159
9160 /* If PER_CU is not yet queued, add it to the queue.
9161 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9162 dependency.
9163 The result is non-zero if PER_CU was queued, otherwise the result is zero
9164 meaning either PER_CU is already queued or it is already loaded.
9165
9166 N.B. There is an invariant here that if a CU is queued then it is loaded.
9167 The caller is required to load PER_CU if we return non-zero. */
9168
9169 static int
9170 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9171 struct dwarf2_per_cu_data *per_cu,
9172 enum language pretend_language)
9173 {
9174 /* We may arrive here during partial symbol reading, if we need full
9175 DIEs to process an unusual case (e.g. template arguments). Do
9176 not queue PER_CU, just tell our caller to load its DIEs. */
9177 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9178 {
9179 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9180 return 1;
9181 return 0;
9182 }
9183
9184 /* Mark the dependence relation so that we don't flush PER_CU
9185 too early. */
9186 if (dependent_cu != NULL)
9187 dwarf2_add_dependence (dependent_cu, per_cu);
9188
9189 /* If it's already on the queue, we have nothing to do. */
9190 if (per_cu->queued)
9191 return 0;
9192
9193 /* If the compilation unit is already loaded, just mark it as
9194 used. */
9195 if (per_cu->cu != NULL)
9196 {
9197 per_cu->cu->last_used = 0;
9198 return 0;
9199 }
9200
9201 /* Add it to the queue. */
9202 queue_comp_unit (per_cu, pretend_language);
9203
9204 return 1;
9205 }
9206
9207 /* Process the queue. */
9208
9209 static void
9210 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9211 {
9212 if (dwarf_read_debug)
9213 {
9214 fprintf_unfiltered (gdb_stdlog,
9215 "Expanding one or more symtabs of objfile %s ...\n",
9216 objfile_name (dwarf2_per_objfile->objfile));
9217 }
9218
9219 /* The queue starts out with one item, but following a DIE reference
9220 may load a new CU, adding it to the end of the queue. */
9221 while (!dwarf2_per_objfile->queue.empty ())
9222 {
9223 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
9224
9225 if ((dwarf2_per_objfile->using_index
9226 ? !item.per_cu->v.quick->compunit_symtab
9227 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
9228 /* Skip dummy CUs. */
9229 && item.per_cu->cu != NULL)
9230 {
9231 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9232 unsigned int debug_print_threshold;
9233 char buf[100];
9234
9235 if (per_cu->is_debug_types)
9236 {
9237 struct signatured_type *sig_type =
9238 (struct signatured_type *) per_cu;
9239
9240 sprintf (buf, "TU %s at offset %s",
9241 hex_string (sig_type->signature),
9242 sect_offset_str (per_cu->sect_off));
9243 /* There can be 100s of TUs.
9244 Only print them in verbose mode. */
9245 debug_print_threshold = 2;
9246 }
9247 else
9248 {
9249 sprintf (buf, "CU at offset %s",
9250 sect_offset_str (per_cu->sect_off));
9251 debug_print_threshold = 1;
9252 }
9253
9254 if (dwarf_read_debug >= debug_print_threshold)
9255 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9256
9257 if (per_cu->is_debug_types)
9258 process_full_type_unit (per_cu, item.pretend_language);
9259 else
9260 process_full_comp_unit (per_cu, item.pretend_language);
9261
9262 if (dwarf_read_debug >= debug_print_threshold)
9263 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9264 }
9265
9266 item.per_cu->queued = 0;
9267 dwarf2_per_objfile->queue.pop ();
9268 }
9269
9270 if (dwarf_read_debug)
9271 {
9272 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9273 objfile_name (dwarf2_per_objfile->objfile));
9274 }
9275 }
9276
9277 /* Read in full symbols for PST, and anything it depends on. */
9278
9279 void
9280 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9281 {
9282 struct dwarf2_per_cu_data *per_cu;
9283
9284 if (readin)
9285 return;
9286
9287 read_dependencies (objfile);
9288
9289 per_cu = per_cu_data;
9290
9291 if (per_cu == NULL)
9292 {
9293 /* It's an include file, no symbols to read for it.
9294 Everything is in the parent symtab. */
9295 readin = true;
9296 return;
9297 }
9298
9299 dw2_do_instantiate_symtab (per_cu, false);
9300 }
9301
9302 /* Trivial hash function for die_info: the hash value of a DIE
9303 is its offset in .debug_info for this objfile. */
9304
9305 static hashval_t
9306 die_hash (const void *item)
9307 {
9308 const struct die_info *die = (const struct die_info *) item;
9309
9310 return to_underlying (die->sect_off);
9311 }
9312
9313 /* Trivial comparison function for die_info structures: two DIEs
9314 are equal if they have the same offset. */
9315
9316 static int
9317 die_eq (const void *item_lhs, const void *item_rhs)
9318 {
9319 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9320 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9321
9322 return die_lhs->sect_off == die_rhs->sect_off;
9323 }
9324
9325 /* Load the DIEs associated with PER_CU into memory. */
9326
9327 static void
9328 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9329 bool skip_partial,
9330 enum language pretend_language)
9331 {
9332 gdb_assert (! this_cu->is_debug_types);
9333
9334 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9335 if (reader.dummy_p)
9336 return;
9337
9338 struct dwarf2_cu *cu = reader.cu;
9339 const gdb_byte *info_ptr = reader.info_ptr;
9340
9341 gdb_assert (cu->die_hash == NULL);
9342 cu->die_hash =
9343 htab_create_alloc_ex (cu->header.length / 12,
9344 die_hash,
9345 die_eq,
9346 NULL,
9347 &cu->comp_unit_obstack,
9348 hashtab_obstack_allocate,
9349 dummy_obstack_deallocate);
9350
9351 if (reader.comp_unit_die->has_children)
9352 reader.comp_unit_die->child
9353 = read_die_and_siblings (&reader, reader.info_ptr,
9354 &info_ptr, reader.comp_unit_die);
9355 cu->dies = reader.comp_unit_die;
9356 /* comp_unit_die is not stored in die_hash, no need. */
9357
9358 /* We try not to read any attributes in this function, because not
9359 all CUs needed for references have been loaded yet, and symbol
9360 table processing isn't initialized. But we have to set the CU language,
9361 or we won't be able to build types correctly.
9362 Similarly, if we do not read the producer, we can not apply
9363 producer-specific interpretation. */
9364 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9365 }
9366
9367 /* Add a DIE to the delayed physname list. */
9368
9369 static void
9370 add_to_method_list (struct type *type, int fnfield_index, int index,
9371 const char *name, struct die_info *die,
9372 struct dwarf2_cu *cu)
9373 {
9374 struct delayed_method_info mi;
9375 mi.type = type;
9376 mi.fnfield_index = fnfield_index;
9377 mi.index = index;
9378 mi.name = name;
9379 mi.die = die;
9380 cu->method_list.push_back (mi);
9381 }
9382
9383 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9384 "const" / "volatile". If so, decrements LEN by the length of the
9385 modifier and return true. Otherwise return false. */
9386
9387 template<size_t N>
9388 static bool
9389 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9390 {
9391 size_t mod_len = sizeof (mod) - 1;
9392 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9393 {
9394 len -= mod_len;
9395 return true;
9396 }
9397 return false;
9398 }
9399
9400 /* Compute the physnames of any methods on the CU's method list.
9401
9402 The computation of method physnames is delayed in order to avoid the
9403 (bad) condition that one of the method's formal parameters is of an as yet
9404 incomplete type. */
9405
9406 static void
9407 compute_delayed_physnames (struct dwarf2_cu *cu)
9408 {
9409 /* Only C++ delays computing physnames. */
9410 if (cu->method_list.empty ())
9411 return;
9412 gdb_assert (cu->language == language_cplus);
9413
9414 for (const delayed_method_info &mi : cu->method_list)
9415 {
9416 const char *physname;
9417 struct fn_fieldlist *fn_flp
9418 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9419 physname = dwarf2_physname (mi.name, mi.die, cu);
9420 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9421 = physname ? physname : "";
9422
9423 /* Since there's no tag to indicate whether a method is a
9424 const/volatile overload, extract that information out of the
9425 demangled name. */
9426 if (physname != NULL)
9427 {
9428 size_t len = strlen (physname);
9429
9430 while (1)
9431 {
9432 if (physname[len] == ')') /* shortcut */
9433 break;
9434 else if (check_modifier (physname, len, " const"))
9435 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9436 else if (check_modifier (physname, len, " volatile"))
9437 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9438 else
9439 break;
9440 }
9441 }
9442 }
9443
9444 /* The list is no longer needed. */
9445 cu->method_list.clear ();
9446 }
9447
9448 /* Go objects should be embedded in a DW_TAG_module DIE,
9449 and it's not clear if/how imported objects will appear.
9450 To keep Go support simple until that's worked out,
9451 go back through what we've read and create something usable.
9452 We could do this while processing each DIE, and feels kinda cleaner,
9453 but that way is more invasive.
9454 This is to, for example, allow the user to type "p var" or "b main"
9455 without having to specify the package name, and allow lookups
9456 of module.object to work in contexts that use the expression
9457 parser. */
9458
9459 static void
9460 fixup_go_packaging (struct dwarf2_cu *cu)
9461 {
9462 gdb::unique_xmalloc_ptr<char> package_name;
9463 struct pending *list;
9464 int i;
9465
9466 for (list = *cu->get_builder ()->get_global_symbols ();
9467 list != NULL;
9468 list = list->next)
9469 {
9470 for (i = 0; i < list->nsyms; ++i)
9471 {
9472 struct symbol *sym = list->symbol[i];
9473
9474 if (sym->language () == language_go
9475 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9476 {
9477 gdb::unique_xmalloc_ptr<char> this_package_name
9478 (go_symbol_package_name (sym));
9479
9480 if (this_package_name == NULL)
9481 continue;
9482 if (package_name == NULL)
9483 package_name = std::move (this_package_name);
9484 else
9485 {
9486 struct objfile *objfile
9487 = cu->per_cu->dwarf2_per_objfile->objfile;
9488 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9489 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9490 (symbol_symtab (sym) != NULL
9491 ? symtab_to_filename_for_display
9492 (symbol_symtab (sym))
9493 : objfile_name (objfile)),
9494 this_package_name.get (), package_name.get ());
9495 }
9496 }
9497 }
9498 }
9499
9500 if (package_name != NULL)
9501 {
9502 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9503 const char *saved_package_name
9504 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9505 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9506 saved_package_name);
9507 struct symbol *sym;
9508
9509 sym = allocate_symbol (objfile);
9510 sym->set_language (language_go, &objfile->objfile_obstack);
9511 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9512 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9513 e.g., "main" finds the "main" module and not C's main(). */
9514 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9515 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9516 SYMBOL_TYPE (sym) = type;
9517
9518 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9519 }
9520 }
9521
9522 /* Allocate a fully-qualified name consisting of the two parts on the
9523 obstack. */
9524
9525 static const char *
9526 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9527 {
9528 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9529 }
9530
9531 /* A helper that allocates a struct discriminant_info to attach to a
9532 union type. */
9533
9534 static struct discriminant_info *
9535 alloc_discriminant_info (struct type *type, int discriminant_index,
9536 int default_index)
9537 {
9538 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9539 gdb_assert (discriminant_index == -1
9540 || (discriminant_index >= 0
9541 && discriminant_index < TYPE_NFIELDS (type)));
9542 gdb_assert (default_index == -1
9543 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9544
9545 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9546
9547 struct discriminant_info *disc
9548 = ((struct discriminant_info *)
9549 TYPE_ZALLOC (type,
9550 offsetof (struct discriminant_info, discriminants)
9551 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9552 disc->default_index = default_index;
9553 disc->discriminant_index = discriminant_index;
9554
9555 struct dynamic_prop prop;
9556 prop.kind = PROP_UNDEFINED;
9557 prop.data.baton = disc;
9558
9559 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9560
9561 return disc;
9562 }
9563
9564 /* Some versions of rustc emitted enums in an unusual way.
9565
9566 Ordinary enums were emitted as unions. The first element of each
9567 structure in the union was named "RUST$ENUM$DISR". This element
9568 held the discriminant.
9569
9570 These versions of Rust also implemented the "non-zero"
9571 optimization. When the enum had two values, and one is empty and
9572 the other holds a pointer that cannot be zero, the pointer is used
9573 as the discriminant, with a zero value meaning the empty variant.
9574 Here, the union's first member is of the form
9575 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9576 where the fieldnos are the indices of the fields that should be
9577 traversed in order to find the field (which may be several fields deep)
9578 and the variantname is the name of the variant of the case when the
9579 field is zero.
9580
9581 This function recognizes whether TYPE is of one of these forms,
9582 and, if so, smashes it to be a variant type. */
9583
9584 static void
9585 quirk_rust_enum (struct type *type, struct objfile *objfile)
9586 {
9587 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9588
9589 /* We don't need to deal with empty enums. */
9590 if (TYPE_NFIELDS (type) == 0)
9591 return;
9592
9593 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9594 if (TYPE_NFIELDS (type) == 1
9595 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9596 {
9597 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9598
9599 /* Decode the field name to find the offset of the
9600 discriminant. */
9601 ULONGEST bit_offset = 0;
9602 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9603 while (name[0] >= '0' && name[0] <= '9')
9604 {
9605 char *tail;
9606 unsigned long index = strtoul (name, &tail, 10);
9607 name = tail;
9608 if (*name != '$'
9609 || index >= TYPE_NFIELDS (field_type)
9610 || (TYPE_FIELD_LOC_KIND (field_type, index)
9611 != FIELD_LOC_KIND_BITPOS))
9612 {
9613 complaint (_("Could not parse Rust enum encoding string \"%s\""
9614 "[in module %s]"),
9615 TYPE_FIELD_NAME (type, 0),
9616 objfile_name (objfile));
9617 return;
9618 }
9619 ++name;
9620
9621 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9622 field_type = TYPE_FIELD_TYPE (field_type, index);
9623 }
9624
9625 /* Make a union to hold the variants. */
9626 struct type *union_type = alloc_type (objfile);
9627 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9628 TYPE_NFIELDS (union_type) = 3;
9629 TYPE_FIELDS (union_type)
9630 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9631 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9632 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9633
9634 /* Put the discriminant must at index 0. */
9635 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9636 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9637 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9638 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9639
9640 /* The order of fields doesn't really matter, so put the real
9641 field at index 1 and the data-less field at index 2. */
9642 struct discriminant_info *disc
9643 = alloc_discriminant_info (union_type, 0, 1);
9644 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9645 TYPE_FIELD_NAME (union_type, 1)
9646 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9647 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9648 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9649 TYPE_FIELD_NAME (union_type, 1));
9650
9651 const char *dataless_name
9652 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9653 name);
9654 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9655 dataless_name);
9656 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9657 /* NAME points into the original discriminant name, which
9658 already has the correct lifetime. */
9659 TYPE_FIELD_NAME (union_type, 2) = name;
9660 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9661 disc->discriminants[2] = 0;
9662
9663 /* Smash this type to be a structure type. We have to do this
9664 because the type has already been recorded. */
9665 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9666 TYPE_NFIELDS (type) = 1;
9667 TYPE_FIELDS (type)
9668 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9669
9670 /* Install the variant part. */
9671 TYPE_FIELD_TYPE (type, 0) = union_type;
9672 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9673 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9674 }
9675 /* A union with a single anonymous field is probably an old-style
9676 univariant enum. */
9677 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9678 {
9679 /* Smash this type to be a structure type. We have to do this
9680 because the type has already been recorded. */
9681 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9682
9683 /* Make a union to hold the variants. */
9684 struct type *union_type = alloc_type (objfile);
9685 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9686 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9687 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9688 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9689 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9690
9691 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9692 const char *variant_name
9693 = rust_last_path_segment (TYPE_NAME (field_type));
9694 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9695 TYPE_NAME (field_type)
9696 = rust_fully_qualify (&objfile->objfile_obstack,
9697 TYPE_NAME (type), variant_name);
9698
9699 /* Install the union in the outer struct type. */
9700 TYPE_NFIELDS (type) = 1;
9701 TYPE_FIELDS (type)
9702 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9703 TYPE_FIELD_TYPE (type, 0) = union_type;
9704 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9705 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9706
9707 alloc_discriminant_info (union_type, -1, 0);
9708 }
9709 else
9710 {
9711 struct type *disr_type = nullptr;
9712 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9713 {
9714 disr_type = TYPE_FIELD_TYPE (type, i);
9715
9716 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9717 {
9718 /* All fields of a true enum will be structs. */
9719 return;
9720 }
9721 else if (TYPE_NFIELDS (disr_type) == 0)
9722 {
9723 /* Could be data-less variant, so keep going. */
9724 disr_type = nullptr;
9725 }
9726 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9727 "RUST$ENUM$DISR") != 0)
9728 {
9729 /* Not a Rust enum. */
9730 return;
9731 }
9732 else
9733 {
9734 /* Found one. */
9735 break;
9736 }
9737 }
9738
9739 /* If we got here without a discriminant, then it's probably
9740 just a union. */
9741 if (disr_type == nullptr)
9742 return;
9743
9744 /* Smash this type to be a structure type. We have to do this
9745 because the type has already been recorded. */
9746 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9747
9748 /* Make a union to hold the variants. */
9749 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9750 struct type *union_type = alloc_type (objfile);
9751 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9752 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9753 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9754 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9755 TYPE_FIELDS (union_type)
9756 = (struct field *) TYPE_ZALLOC (union_type,
9757 (TYPE_NFIELDS (union_type)
9758 * sizeof (struct field)));
9759
9760 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9761 TYPE_NFIELDS (type) * sizeof (struct field));
9762
9763 /* Install the discriminant at index 0 in the union. */
9764 TYPE_FIELD (union_type, 0) = *disr_field;
9765 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9766 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9767
9768 /* Install the union in the outer struct type. */
9769 TYPE_FIELD_TYPE (type, 0) = union_type;
9770 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9771 TYPE_NFIELDS (type) = 1;
9772
9773 /* Set the size and offset of the union type. */
9774 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9775
9776 /* We need a way to find the correct discriminant given a
9777 variant name. For convenience we build a map here. */
9778 struct type *enum_type = FIELD_TYPE (*disr_field);
9779 std::unordered_map<std::string, ULONGEST> discriminant_map;
9780 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9781 {
9782 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9783 {
9784 const char *name
9785 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9786 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9787 }
9788 }
9789
9790 int n_fields = TYPE_NFIELDS (union_type);
9791 struct discriminant_info *disc
9792 = alloc_discriminant_info (union_type, 0, -1);
9793 /* Skip the discriminant here. */
9794 for (int i = 1; i < n_fields; ++i)
9795 {
9796 /* Find the final word in the name of this variant's type.
9797 That name can be used to look up the correct
9798 discriminant. */
9799 const char *variant_name
9800 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9801 i)));
9802
9803 auto iter = discriminant_map.find (variant_name);
9804 if (iter != discriminant_map.end ())
9805 disc->discriminants[i] = iter->second;
9806
9807 /* Remove the discriminant field, if it exists. */
9808 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9809 if (TYPE_NFIELDS (sub_type) > 0)
9810 {
9811 --TYPE_NFIELDS (sub_type);
9812 ++TYPE_FIELDS (sub_type);
9813 }
9814 TYPE_FIELD_NAME (union_type, i) = variant_name;
9815 TYPE_NAME (sub_type)
9816 = rust_fully_qualify (&objfile->objfile_obstack,
9817 TYPE_NAME (type), variant_name);
9818 }
9819 }
9820 }
9821
9822 /* Rewrite some Rust unions to be structures with variants parts. */
9823
9824 static void
9825 rust_union_quirks (struct dwarf2_cu *cu)
9826 {
9827 gdb_assert (cu->language == language_rust);
9828 for (type *type_ : cu->rust_unions)
9829 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9830 /* We don't need this any more. */
9831 cu->rust_unions.clear ();
9832 }
9833
9834 /* Return the symtab for PER_CU. This works properly regardless of
9835 whether we're using the index or psymtabs. */
9836
9837 static struct compunit_symtab *
9838 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9839 {
9840 return (per_cu->dwarf2_per_objfile->using_index
9841 ? per_cu->v.quick->compunit_symtab
9842 : per_cu->v.psymtab->compunit_symtab);
9843 }
9844
9845 /* A helper function for computing the list of all symbol tables
9846 included by PER_CU. */
9847
9848 static void
9849 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9850 htab_t all_children, htab_t all_type_symtabs,
9851 struct dwarf2_per_cu_data *per_cu,
9852 struct compunit_symtab *immediate_parent)
9853 {
9854 void **slot;
9855 struct compunit_symtab *cust;
9856
9857 slot = htab_find_slot (all_children, per_cu, INSERT);
9858 if (*slot != NULL)
9859 {
9860 /* This inclusion and its children have been processed. */
9861 return;
9862 }
9863
9864 *slot = per_cu;
9865 /* Only add a CU if it has a symbol table. */
9866 cust = get_compunit_symtab (per_cu);
9867 if (cust != NULL)
9868 {
9869 /* If this is a type unit only add its symbol table if we haven't
9870 seen it yet (type unit per_cu's can share symtabs). */
9871 if (per_cu->is_debug_types)
9872 {
9873 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9874 if (*slot == NULL)
9875 {
9876 *slot = cust;
9877 result->push_back (cust);
9878 if (cust->user == NULL)
9879 cust->user = immediate_parent;
9880 }
9881 }
9882 else
9883 {
9884 result->push_back (cust);
9885 if (cust->user == NULL)
9886 cust->user = immediate_parent;
9887 }
9888 }
9889
9890 if (!per_cu->imported_symtabs_empty ())
9891 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9892 {
9893 recursively_compute_inclusions (result, all_children,
9894 all_type_symtabs, ptr, cust);
9895 }
9896 }
9897
9898 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9899 PER_CU. */
9900
9901 static void
9902 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9903 {
9904 gdb_assert (! per_cu->is_debug_types);
9905
9906 if (!per_cu->imported_symtabs_empty ())
9907 {
9908 int len;
9909 std::vector<compunit_symtab *> result_symtabs;
9910 htab_t all_children, all_type_symtabs;
9911 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9912
9913 /* If we don't have a symtab, we can just skip this case. */
9914 if (cust == NULL)
9915 return;
9916
9917 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9918 NULL, xcalloc, xfree);
9919 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9920 NULL, xcalloc, xfree);
9921
9922 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9923 {
9924 recursively_compute_inclusions (&result_symtabs, all_children,
9925 all_type_symtabs, ptr, cust);
9926 }
9927
9928 /* Now we have a transitive closure of all the included symtabs. */
9929 len = result_symtabs.size ();
9930 cust->includes
9931 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9932 struct compunit_symtab *, len + 1);
9933 memcpy (cust->includes, result_symtabs.data (),
9934 len * sizeof (compunit_symtab *));
9935 cust->includes[len] = NULL;
9936
9937 htab_delete (all_children);
9938 htab_delete (all_type_symtabs);
9939 }
9940 }
9941
9942 /* Compute the 'includes' field for the symtabs of all the CUs we just
9943 read. */
9944
9945 static void
9946 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9947 {
9948 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9949 {
9950 if (! iter->is_debug_types)
9951 compute_compunit_symtab_includes (iter);
9952 }
9953
9954 dwarf2_per_objfile->just_read_cus.clear ();
9955 }
9956
9957 /* Generate full symbol information for PER_CU, whose DIEs have
9958 already been loaded into memory. */
9959
9960 static void
9961 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9962 enum language pretend_language)
9963 {
9964 struct dwarf2_cu *cu = per_cu->cu;
9965 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9966 struct objfile *objfile = dwarf2_per_objfile->objfile;
9967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9968 CORE_ADDR lowpc, highpc;
9969 struct compunit_symtab *cust;
9970 CORE_ADDR baseaddr;
9971 struct block *static_block;
9972 CORE_ADDR addr;
9973
9974 baseaddr = objfile->text_section_offset ();
9975
9976 /* Clear the list here in case something was left over. */
9977 cu->method_list.clear ();
9978
9979 cu->language = pretend_language;
9980 cu->language_defn = language_def (cu->language);
9981
9982 /* Do line number decoding in read_file_scope () */
9983 process_die (cu->dies, cu);
9984
9985 /* For now fudge the Go package. */
9986 if (cu->language == language_go)
9987 fixup_go_packaging (cu);
9988
9989 /* Now that we have processed all the DIEs in the CU, all the types
9990 should be complete, and it should now be safe to compute all of the
9991 physnames. */
9992 compute_delayed_physnames (cu);
9993
9994 if (cu->language == language_rust)
9995 rust_union_quirks (cu);
9996
9997 /* Some compilers don't define a DW_AT_high_pc attribute for the
9998 compilation unit. If the DW_AT_high_pc is missing, synthesize
9999 it, by scanning the DIE's below the compilation unit. */
10000 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10001
10002 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10003 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10004
10005 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10006 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10007 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10008 addrmap to help ensure it has an accurate map of pc values belonging to
10009 this comp unit. */
10010 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10011
10012 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10013 SECT_OFF_TEXT (objfile),
10014 0);
10015
10016 if (cust != NULL)
10017 {
10018 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10019
10020 /* Set symtab language to language from DW_AT_language. If the
10021 compilation is from a C file generated by language preprocessors, do
10022 not set the language if it was already deduced by start_subfile. */
10023 if (!(cu->language == language_c
10024 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10025 COMPUNIT_FILETABS (cust)->language = cu->language;
10026
10027 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10028 produce DW_AT_location with location lists but it can be possibly
10029 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10030 there were bugs in prologue debug info, fixed later in GCC-4.5
10031 by "unwind info for epilogues" patch (which is not directly related).
10032
10033 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10034 needed, it would be wrong due to missing DW_AT_producer there.
10035
10036 Still one can confuse GDB by using non-standard GCC compilation
10037 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10038 */
10039 if (cu->has_loclist && gcc_4_minor >= 5)
10040 cust->locations_valid = 1;
10041
10042 if (gcc_4_minor >= 5)
10043 cust->epilogue_unwind_valid = 1;
10044
10045 cust->call_site_htab = cu->call_site_htab;
10046 }
10047
10048 if (dwarf2_per_objfile->using_index)
10049 per_cu->v.quick->compunit_symtab = cust;
10050 else
10051 {
10052 dwarf2_psymtab *pst = per_cu->v.psymtab;
10053 pst->compunit_symtab = cust;
10054 pst->readin = true;
10055 }
10056
10057 /* Push it for inclusion processing later. */
10058 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10059
10060 /* Not needed any more. */
10061 cu->reset_builder ();
10062 }
10063
10064 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10065 already been loaded into memory. */
10066
10067 static void
10068 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10069 enum language pretend_language)
10070 {
10071 struct dwarf2_cu *cu = per_cu->cu;
10072 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10073 struct objfile *objfile = dwarf2_per_objfile->objfile;
10074 struct compunit_symtab *cust;
10075 struct signatured_type *sig_type;
10076
10077 gdb_assert (per_cu->is_debug_types);
10078 sig_type = (struct signatured_type *) per_cu;
10079
10080 /* Clear the list here in case something was left over. */
10081 cu->method_list.clear ();
10082
10083 cu->language = pretend_language;
10084 cu->language_defn = language_def (cu->language);
10085
10086 /* The symbol tables are set up in read_type_unit_scope. */
10087 process_die (cu->dies, cu);
10088
10089 /* For now fudge the Go package. */
10090 if (cu->language == language_go)
10091 fixup_go_packaging (cu);
10092
10093 /* Now that we have processed all the DIEs in the CU, all the types
10094 should be complete, and it should now be safe to compute all of the
10095 physnames. */
10096 compute_delayed_physnames (cu);
10097
10098 if (cu->language == language_rust)
10099 rust_union_quirks (cu);
10100
10101 /* TUs share symbol tables.
10102 If this is the first TU to use this symtab, complete the construction
10103 of it with end_expandable_symtab. Otherwise, complete the addition of
10104 this TU's symbols to the existing symtab. */
10105 if (sig_type->type_unit_group->compunit_symtab == NULL)
10106 {
10107 buildsym_compunit *builder = cu->get_builder ();
10108 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10109 sig_type->type_unit_group->compunit_symtab = cust;
10110
10111 if (cust != NULL)
10112 {
10113 /* Set symtab language to language from DW_AT_language. If the
10114 compilation is from a C file generated by language preprocessors,
10115 do not set the language if it was already deduced by
10116 start_subfile. */
10117 if (!(cu->language == language_c
10118 && COMPUNIT_FILETABS (cust)->language != language_c))
10119 COMPUNIT_FILETABS (cust)->language = cu->language;
10120 }
10121 }
10122 else
10123 {
10124 cu->get_builder ()->augment_type_symtab ();
10125 cust = sig_type->type_unit_group->compunit_symtab;
10126 }
10127
10128 if (dwarf2_per_objfile->using_index)
10129 per_cu->v.quick->compunit_symtab = cust;
10130 else
10131 {
10132 dwarf2_psymtab *pst = per_cu->v.psymtab;
10133 pst->compunit_symtab = cust;
10134 pst->readin = true;
10135 }
10136
10137 /* Not needed any more. */
10138 cu->reset_builder ();
10139 }
10140
10141 /* Process an imported unit DIE. */
10142
10143 static void
10144 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10145 {
10146 struct attribute *attr;
10147
10148 /* For now we don't handle imported units in type units. */
10149 if (cu->per_cu->is_debug_types)
10150 {
10151 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10152 " supported in type units [in module %s]"),
10153 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10154 }
10155
10156 attr = dwarf2_attr (die, DW_AT_import, cu);
10157 if (attr != NULL)
10158 {
10159 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10160 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10161 dwarf2_per_cu_data *per_cu
10162 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10163 cu->per_cu->dwarf2_per_objfile);
10164
10165 /* If necessary, add it to the queue and load its DIEs. */
10166 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10167 load_full_comp_unit (per_cu, false, cu->language);
10168
10169 cu->per_cu->imported_symtabs_push (per_cu);
10170 }
10171 }
10172
10173 /* RAII object that represents a process_die scope: i.e.,
10174 starts/finishes processing a DIE. */
10175 class process_die_scope
10176 {
10177 public:
10178 process_die_scope (die_info *die, dwarf2_cu *cu)
10179 : m_die (die), m_cu (cu)
10180 {
10181 /* We should only be processing DIEs not already in process. */
10182 gdb_assert (!m_die->in_process);
10183 m_die->in_process = true;
10184 }
10185
10186 ~process_die_scope ()
10187 {
10188 m_die->in_process = false;
10189
10190 /* If we're done processing the DIE for the CU that owns the line
10191 header, we don't need the line header anymore. */
10192 if (m_cu->line_header_die_owner == m_die)
10193 {
10194 delete m_cu->line_header;
10195 m_cu->line_header = NULL;
10196 m_cu->line_header_die_owner = NULL;
10197 }
10198 }
10199
10200 private:
10201 die_info *m_die;
10202 dwarf2_cu *m_cu;
10203 };
10204
10205 /* Process a die and its children. */
10206
10207 static void
10208 process_die (struct die_info *die, struct dwarf2_cu *cu)
10209 {
10210 process_die_scope scope (die, cu);
10211
10212 switch (die->tag)
10213 {
10214 case DW_TAG_padding:
10215 break;
10216 case DW_TAG_compile_unit:
10217 case DW_TAG_partial_unit:
10218 read_file_scope (die, cu);
10219 break;
10220 case DW_TAG_type_unit:
10221 read_type_unit_scope (die, cu);
10222 break;
10223 case DW_TAG_subprogram:
10224 /* Nested subprograms in Fortran get a prefix. */
10225 if (cu->language == language_fortran
10226 && die->parent != NULL
10227 && die->parent->tag == DW_TAG_subprogram)
10228 cu->processing_has_namespace_info = true;
10229 /* Fall through. */
10230 case DW_TAG_inlined_subroutine:
10231 read_func_scope (die, cu);
10232 break;
10233 case DW_TAG_lexical_block:
10234 case DW_TAG_try_block:
10235 case DW_TAG_catch_block:
10236 read_lexical_block_scope (die, cu);
10237 break;
10238 case DW_TAG_call_site:
10239 case DW_TAG_GNU_call_site:
10240 read_call_site_scope (die, cu);
10241 break;
10242 case DW_TAG_class_type:
10243 case DW_TAG_interface_type:
10244 case DW_TAG_structure_type:
10245 case DW_TAG_union_type:
10246 process_structure_scope (die, cu);
10247 break;
10248 case DW_TAG_enumeration_type:
10249 process_enumeration_scope (die, cu);
10250 break;
10251
10252 /* These dies have a type, but processing them does not create
10253 a symbol or recurse to process the children. Therefore we can
10254 read them on-demand through read_type_die. */
10255 case DW_TAG_subroutine_type:
10256 case DW_TAG_set_type:
10257 case DW_TAG_array_type:
10258 case DW_TAG_pointer_type:
10259 case DW_TAG_ptr_to_member_type:
10260 case DW_TAG_reference_type:
10261 case DW_TAG_rvalue_reference_type:
10262 case DW_TAG_string_type:
10263 break;
10264
10265 case DW_TAG_base_type:
10266 case DW_TAG_subrange_type:
10267 case DW_TAG_typedef:
10268 /* Add a typedef symbol for the type definition, if it has a
10269 DW_AT_name. */
10270 new_symbol (die, read_type_die (die, cu), cu);
10271 break;
10272 case DW_TAG_common_block:
10273 read_common_block (die, cu);
10274 break;
10275 case DW_TAG_common_inclusion:
10276 break;
10277 case DW_TAG_namespace:
10278 cu->processing_has_namespace_info = true;
10279 read_namespace (die, cu);
10280 break;
10281 case DW_TAG_module:
10282 cu->processing_has_namespace_info = true;
10283 read_module (die, cu);
10284 break;
10285 case DW_TAG_imported_declaration:
10286 cu->processing_has_namespace_info = true;
10287 if (read_namespace_alias (die, cu))
10288 break;
10289 /* The declaration is not a global namespace alias. */
10290 /* Fall through. */
10291 case DW_TAG_imported_module:
10292 cu->processing_has_namespace_info = true;
10293 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10294 || cu->language != language_fortran))
10295 complaint (_("Tag '%s' has unexpected children"),
10296 dwarf_tag_name (die->tag));
10297 read_import_statement (die, cu);
10298 break;
10299
10300 case DW_TAG_imported_unit:
10301 process_imported_unit_die (die, cu);
10302 break;
10303
10304 case DW_TAG_variable:
10305 read_variable (die, cu);
10306 break;
10307
10308 default:
10309 new_symbol (die, NULL, cu);
10310 break;
10311 }
10312 }
10313 \f
10314 /* DWARF name computation. */
10315
10316 /* A helper function for dwarf2_compute_name which determines whether DIE
10317 needs to have the name of the scope prepended to the name listed in the
10318 die. */
10319
10320 static int
10321 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10322 {
10323 struct attribute *attr;
10324
10325 switch (die->tag)
10326 {
10327 case DW_TAG_namespace:
10328 case DW_TAG_typedef:
10329 case DW_TAG_class_type:
10330 case DW_TAG_interface_type:
10331 case DW_TAG_structure_type:
10332 case DW_TAG_union_type:
10333 case DW_TAG_enumeration_type:
10334 case DW_TAG_enumerator:
10335 case DW_TAG_subprogram:
10336 case DW_TAG_inlined_subroutine:
10337 case DW_TAG_member:
10338 case DW_TAG_imported_declaration:
10339 return 1;
10340
10341 case DW_TAG_variable:
10342 case DW_TAG_constant:
10343 /* We only need to prefix "globally" visible variables. These include
10344 any variable marked with DW_AT_external or any variable that
10345 lives in a namespace. [Variables in anonymous namespaces
10346 require prefixing, but they are not DW_AT_external.] */
10347
10348 if (dwarf2_attr (die, DW_AT_specification, cu))
10349 {
10350 struct dwarf2_cu *spec_cu = cu;
10351
10352 return die_needs_namespace (die_specification (die, &spec_cu),
10353 spec_cu);
10354 }
10355
10356 attr = dwarf2_attr (die, DW_AT_external, cu);
10357 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10358 && die->parent->tag != DW_TAG_module)
10359 return 0;
10360 /* A variable in a lexical block of some kind does not need a
10361 namespace, even though in C++ such variables may be external
10362 and have a mangled name. */
10363 if (die->parent->tag == DW_TAG_lexical_block
10364 || die->parent->tag == DW_TAG_try_block
10365 || die->parent->tag == DW_TAG_catch_block
10366 || die->parent->tag == DW_TAG_subprogram)
10367 return 0;
10368 return 1;
10369
10370 default:
10371 return 0;
10372 }
10373 }
10374
10375 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10376 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10377 defined for the given DIE. */
10378
10379 static struct attribute *
10380 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10381 {
10382 struct attribute *attr;
10383
10384 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10385 if (attr == NULL)
10386 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10387
10388 return attr;
10389 }
10390
10391 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10392 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10393 defined for the given DIE. */
10394
10395 static const char *
10396 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10397 {
10398 const char *linkage_name;
10399
10400 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10401 if (linkage_name == NULL)
10402 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10403
10404 return linkage_name;
10405 }
10406
10407 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10408 compute the physname for the object, which include a method's:
10409 - formal parameters (C++),
10410 - receiver type (Go),
10411
10412 The term "physname" is a bit confusing.
10413 For C++, for example, it is the demangled name.
10414 For Go, for example, it's the mangled name.
10415
10416 For Ada, return the DIE's linkage name rather than the fully qualified
10417 name. PHYSNAME is ignored..
10418
10419 The result is allocated on the objfile_obstack and canonicalized. */
10420
10421 static const char *
10422 dwarf2_compute_name (const char *name,
10423 struct die_info *die, struct dwarf2_cu *cu,
10424 int physname)
10425 {
10426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10427
10428 if (name == NULL)
10429 name = dwarf2_name (die, cu);
10430
10431 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10432 but otherwise compute it by typename_concat inside GDB.
10433 FIXME: Actually this is not really true, or at least not always true.
10434 It's all very confusing. compute_and_set_names doesn't try to demangle
10435 Fortran names because there is no mangling standard. So new_symbol
10436 will set the demangled name to the result of dwarf2_full_name, and it is
10437 the demangled name that GDB uses if it exists. */
10438 if (cu->language == language_ada
10439 || (cu->language == language_fortran && physname))
10440 {
10441 /* For Ada unit, we prefer the linkage name over the name, as
10442 the former contains the exported name, which the user expects
10443 to be able to reference. Ideally, we want the user to be able
10444 to reference this entity using either natural or linkage name,
10445 but we haven't started looking at this enhancement yet. */
10446 const char *linkage_name = dw2_linkage_name (die, cu);
10447
10448 if (linkage_name != NULL)
10449 return linkage_name;
10450 }
10451
10452 /* These are the only languages we know how to qualify names in. */
10453 if (name != NULL
10454 && (cu->language == language_cplus
10455 || cu->language == language_fortran || cu->language == language_d
10456 || cu->language == language_rust))
10457 {
10458 if (die_needs_namespace (die, cu))
10459 {
10460 const char *prefix;
10461 const char *canonical_name = NULL;
10462
10463 string_file buf;
10464
10465 prefix = determine_prefix (die, cu);
10466 if (*prefix != '\0')
10467 {
10468 gdb::unique_xmalloc_ptr<char> prefixed_name
10469 (typename_concat (NULL, prefix, name, physname, cu));
10470
10471 buf.puts (prefixed_name.get ());
10472 }
10473 else
10474 buf.puts (name);
10475
10476 /* Template parameters may be specified in the DIE's DW_AT_name, or
10477 as children with DW_TAG_template_type_param or
10478 DW_TAG_value_type_param. If the latter, add them to the name
10479 here. If the name already has template parameters, then
10480 skip this step; some versions of GCC emit both, and
10481 it is more efficient to use the pre-computed name.
10482
10483 Something to keep in mind about this process: it is very
10484 unlikely, or in some cases downright impossible, to produce
10485 something that will match the mangled name of a function.
10486 If the definition of the function has the same debug info,
10487 we should be able to match up with it anyway. But fallbacks
10488 using the minimal symbol, for instance to find a method
10489 implemented in a stripped copy of libstdc++, will not work.
10490 If we do not have debug info for the definition, we will have to
10491 match them up some other way.
10492
10493 When we do name matching there is a related problem with function
10494 templates; two instantiated function templates are allowed to
10495 differ only by their return types, which we do not add here. */
10496
10497 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10498 {
10499 struct attribute *attr;
10500 struct die_info *child;
10501 int first = 1;
10502
10503 die->building_fullname = 1;
10504
10505 for (child = die->child; child != NULL; child = child->sibling)
10506 {
10507 struct type *type;
10508 LONGEST value;
10509 const gdb_byte *bytes;
10510 struct dwarf2_locexpr_baton *baton;
10511 struct value *v;
10512
10513 if (child->tag != DW_TAG_template_type_param
10514 && child->tag != DW_TAG_template_value_param)
10515 continue;
10516
10517 if (first)
10518 {
10519 buf.puts ("<");
10520 first = 0;
10521 }
10522 else
10523 buf.puts (", ");
10524
10525 attr = dwarf2_attr (child, DW_AT_type, cu);
10526 if (attr == NULL)
10527 {
10528 complaint (_("template parameter missing DW_AT_type"));
10529 buf.puts ("UNKNOWN_TYPE");
10530 continue;
10531 }
10532 type = die_type (child, cu);
10533
10534 if (child->tag == DW_TAG_template_type_param)
10535 {
10536 c_print_type (type, "", &buf, -1, 0, cu->language,
10537 &type_print_raw_options);
10538 continue;
10539 }
10540
10541 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10542 if (attr == NULL)
10543 {
10544 complaint (_("template parameter missing "
10545 "DW_AT_const_value"));
10546 buf.puts ("UNKNOWN_VALUE");
10547 continue;
10548 }
10549
10550 dwarf2_const_value_attr (attr, type, name,
10551 &cu->comp_unit_obstack, cu,
10552 &value, &bytes, &baton);
10553
10554 if (TYPE_NOSIGN (type))
10555 /* GDB prints characters as NUMBER 'CHAR'. If that's
10556 changed, this can use value_print instead. */
10557 c_printchar (value, type, &buf);
10558 else
10559 {
10560 struct value_print_options opts;
10561
10562 if (baton != NULL)
10563 v = dwarf2_evaluate_loc_desc (type, NULL,
10564 baton->data,
10565 baton->size,
10566 baton->per_cu);
10567 else if (bytes != NULL)
10568 {
10569 v = allocate_value (type);
10570 memcpy (value_contents_writeable (v), bytes,
10571 TYPE_LENGTH (type));
10572 }
10573 else
10574 v = value_from_longest (type, value);
10575
10576 /* Specify decimal so that we do not depend on
10577 the radix. */
10578 get_formatted_print_options (&opts, 'd');
10579 opts.raw = 1;
10580 value_print (v, &buf, &opts);
10581 release_value (v);
10582 }
10583 }
10584
10585 die->building_fullname = 0;
10586
10587 if (!first)
10588 {
10589 /* Close the argument list, with a space if necessary
10590 (nested templates). */
10591 if (!buf.empty () && buf.string ().back () == '>')
10592 buf.puts (" >");
10593 else
10594 buf.puts (">");
10595 }
10596 }
10597
10598 /* For C++ methods, append formal parameter type
10599 information, if PHYSNAME. */
10600
10601 if (physname && die->tag == DW_TAG_subprogram
10602 && cu->language == language_cplus)
10603 {
10604 struct type *type = read_type_die (die, cu);
10605
10606 c_type_print_args (type, &buf, 1, cu->language,
10607 &type_print_raw_options);
10608
10609 if (cu->language == language_cplus)
10610 {
10611 /* Assume that an artificial first parameter is
10612 "this", but do not crash if it is not. RealView
10613 marks unnamed (and thus unused) parameters as
10614 artificial; there is no way to differentiate
10615 the two cases. */
10616 if (TYPE_NFIELDS (type) > 0
10617 && TYPE_FIELD_ARTIFICIAL (type, 0)
10618 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10619 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10620 0))))
10621 buf.puts (" const");
10622 }
10623 }
10624
10625 const std::string &intermediate_name = buf.string ();
10626
10627 if (cu->language == language_cplus)
10628 canonical_name
10629 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10630 &objfile->per_bfd->storage_obstack);
10631
10632 /* If we only computed INTERMEDIATE_NAME, or if
10633 INTERMEDIATE_NAME is already canonical, then we need to
10634 copy it to the appropriate obstack. */
10635 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10636 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10637 intermediate_name);
10638 else
10639 name = canonical_name;
10640 }
10641 }
10642
10643 return name;
10644 }
10645
10646 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10647 If scope qualifiers are appropriate they will be added. The result
10648 will be allocated on the storage_obstack, or NULL if the DIE does
10649 not have a name. NAME may either be from a previous call to
10650 dwarf2_name or NULL.
10651
10652 The output string will be canonicalized (if C++). */
10653
10654 static const char *
10655 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10656 {
10657 return dwarf2_compute_name (name, die, cu, 0);
10658 }
10659
10660 /* Construct a physname for the given DIE in CU. NAME may either be
10661 from a previous call to dwarf2_name or NULL. The result will be
10662 allocated on the objfile_objstack or NULL if the DIE does not have a
10663 name.
10664
10665 The output string will be canonicalized (if C++). */
10666
10667 static const char *
10668 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10669 {
10670 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10671 const char *retval, *mangled = NULL, *canon = NULL;
10672 int need_copy = 1;
10673
10674 /* In this case dwarf2_compute_name is just a shortcut not building anything
10675 on its own. */
10676 if (!die_needs_namespace (die, cu))
10677 return dwarf2_compute_name (name, die, cu, 1);
10678
10679 mangled = dw2_linkage_name (die, cu);
10680
10681 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10682 See https://github.com/rust-lang/rust/issues/32925. */
10683 if (cu->language == language_rust && mangled != NULL
10684 && strchr (mangled, '{') != NULL)
10685 mangled = NULL;
10686
10687 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10688 has computed. */
10689 gdb::unique_xmalloc_ptr<char> demangled;
10690 if (mangled != NULL)
10691 {
10692
10693 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10694 {
10695 /* Do nothing (do not demangle the symbol name). */
10696 }
10697 else if (cu->language == language_go)
10698 {
10699 /* This is a lie, but we already lie to the caller new_symbol.
10700 new_symbol assumes we return the mangled name.
10701 This just undoes that lie until things are cleaned up. */
10702 }
10703 else
10704 {
10705 /* Use DMGL_RET_DROP for C++ template functions to suppress
10706 their return type. It is easier for GDB users to search
10707 for such functions as `name(params)' than `long name(params)'.
10708 In such case the minimal symbol names do not match the full
10709 symbol names but for template functions there is never a need
10710 to look up their definition from their declaration so
10711 the only disadvantage remains the minimal symbol variant
10712 `long name(params)' does not have the proper inferior type. */
10713 demangled.reset (gdb_demangle (mangled,
10714 (DMGL_PARAMS | DMGL_ANSI
10715 | DMGL_RET_DROP)));
10716 }
10717 if (demangled)
10718 canon = demangled.get ();
10719 else
10720 {
10721 canon = mangled;
10722 need_copy = 0;
10723 }
10724 }
10725
10726 if (canon == NULL || check_physname)
10727 {
10728 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10729
10730 if (canon != NULL && strcmp (physname, canon) != 0)
10731 {
10732 /* It may not mean a bug in GDB. The compiler could also
10733 compute DW_AT_linkage_name incorrectly. But in such case
10734 GDB would need to be bug-to-bug compatible. */
10735
10736 complaint (_("Computed physname <%s> does not match demangled <%s> "
10737 "(from linkage <%s>) - DIE at %s [in module %s]"),
10738 physname, canon, mangled, sect_offset_str (die->sect_off),
10739 objfile_name (objfile));
10740
10741 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10742 is available here - over computed PHYSNAME. It is safer
10743 against both buggy GDB and buggy compilers. */
10744
10745 retval = canon;
10746 }
10747 else
10748 {
10749 retval = physname;
10750 need_copy = 0;
10751 }
10752 }
10753 else
10754 retval = canon;
10755
10756 if (need_copy)
10757 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10758
10759 return retval;
10760 }
10761
10762 /* Inspect DIE in CU for a namespace alias. If one exists, record
10763 a new symbol for it.
10764
10765 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10766
10767 static int
10768 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10769 {
10770 struct attribute *attr;
10771
10772 /* If the die does not have a name, this is not a namespace
10773 alias. */
10774 attr = dwarf2_attr (die, DW_AT_name, cu);
10775 if (attr != NULL)
10776 {
10777 int num;
10778 struct die_info *d = die;
10779 struct dwarf2_cu *imported_cu = cu;
10780
10781 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10782 keep inspecting DIEs until we hit the underlying import. */
10783 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10784 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10785 {
10786 attr = dwarf2_attr (d, DW_AT_import, cu);
10787 if (attr == NULL)
10788 break;
10789
10790 d = follow_die_ref (d, attr, &imported_cu);
10791 if (d->tag != DW_TAG_imported_declaration)
10792 break;
10793 }
10794
10795 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10796 {
10797 complaint (_("DIE at %s has too many recursively imported "
10798 "declarations"), sect_offset_str (d->sect_off));
10799 return 0;
10800 }
10801
10802 if (attr != NULL)
10803 {
10804 struct type *type;
10805 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10806
10807 type = get_die_type_at_offset (sect_off, cu->per_cu);
10808 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10809 {
10810 /* This declaration is a global namespace alias. Add
10811 a symbol for it whose type is the aliased namespace. */
10812 new_symbol (die, type, cu);
10813 return 1;
10814 }
10815 }
10816 }
10817
10818 return 0;
10819 }
10820
10821 /* Return the using directives repository (global or local?) to use in the
10822 current context for CU.
10823
10824 For Ada, imported declarations can materialize renamings, which *may* be
10825 global. However it is impossible (for now?) in DWARF to distinguish
10826 "external" imported declarations and "static" ones. As all imported
10827 declarations seem to be static in all other languages, make them all CU-wide
10828 global only in Ada. */
10829
10830 static struct using_direct **
10831 using_directives (struct dwarf2_cu *cu)
10832 {
10833 if (cu->language == language_ada
10834 && cu->get_builder ()->outermost_context_p ())
10835 return cu->get_builder ()->get_global_using_directives ();
10836 else
10837 return cu->get_builder ()->get_local_using_directives ();
10838 }
10839
10840 /* Read the import statement specified by the given die and record it. */
10841
10842 static void
10843 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10844 {
10845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10846 struct attribute *import_attr;
10847 struct die_info *imported_die, *child_die;
10848 struct dwarf2_cu *imported_cu;
10849 const char *imported_name;
10850 const char *imported_name_prefix;
10851 const char *canonical_name;
10852 const char *import_alias;
10853 const char *imported_declaration = NULL;
10854 const char *import_prefix;
10855 std::vector<const char *> excludes;
10856
10857 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10858 if (import_attr == NULL)
10859 {
10860 complaint (_("Tag '%s' has no DW_AT_import"),
10861 dwarf_tag_name (die->tag));
10862 return;
10863 }
10864
10865 imported_cu = cu;
10866 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10867 imported_name = dwarf2_name (imported_die, imported_cu);
10868 if (imported_name == NULL)
10869 {
10870 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10871
10872 The import in the following code:
10873 namespace A
10874 {
10875 typedef int B;
10876 }
10877
10878 int main ()
10879 {
10880 using A::B;
10881 B b;
10882 return b;
10883 }
10884
10885 ...
10886 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10887 <52> DW_AT_decl_file : 1
10888 <53> DW_AT_decl_line : 6
10889 <54> DW_AT_import : <0x75>
10890 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10891 <59> DW_AT_name : B
10892 <5b> DW_AT_decl_file : 1
10893 <5c> DW_AT_decl_line : 2
10894 <5d> DW_AT_type : <0x6e>
10895 ...
10896 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10897 <76> DW_AT_byte_size : 4
10898 <77> DW_AT_encoding : 5 (signed)
10899
10900 imports the wrong die ( 0x75 instead of 0x58 ).
10901 This case will be ignored until the gcc bug is fixed. */
10902 return;
10903 }
10904
10905 /* Figure out the local name after import. */
10906 import_alias = dwarf2_name (die, cu);
10907
10908 /* Figure out where the statement is being imported to. */
10909 import_prefix = determine_prefix (die, cu);
10910
10911 /* Figure out what the scope of the imported die is and prepend it
10912 to the name of the imported die. */
10913 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10914
10915 if (imported_die->tag != DW_TAG_namespace
10916 && imported_die->tag != DW_TAG_module)
10917 {
10918 imported_declaration = imported_name;
10919 canonical_name = imported_name_prefix;
10920 }
10921 else if (strlen (imported_name_prefix) > 0)
10922 canonical_name = obconcat (&objfile->objfile_obstack,
10923 imported_name_prefix,
10924 (cu->language == language_d ? "." : "::"),
10925 imported_name, (char *) NULL);
10926 else
10927 canonical_name = imported_name;
10928
10929 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10930 for (child_die = die->child; child_die && child_die->tag;
10931 child_die = sibling_die (child_die))
10932 {
10933 /* DWARF-4: A Fortran use statement with a “rename list” may be
10934 represented by an imported module entry with an import attribute
10935 referring to the module and owned entries corresponding to those
10936 entities that are renamed as part of being imported. */
10937
10938 if (child_die->tag != DW_TAG_imported_declaration)
10939 {
10940 complaint (_("child DW_TAG_imported_declaration expected "
10941 "- DIE at %s [in module %s]"),
10942 sect_offset_str (child_die->sect_off),
10943 objfile_name (objfile));
10944 continue;
10945 }
10946
10947 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10948 if (import_attr == NULL)
10949 {
10950 complaint (_("Tag '%s' has no DW_AT_import"),
10951 dwarf_tag_name (child_die->tag));
10952 continue;
10953 }
10954
10955 imported_cu = cu;
10956 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10957 &imported_cu);
10958 imported_name = dwarf2_name (imported_die, imported_cu);
10959 if (imported_name == NULL)
10960 {
10961 complaint (_("child DW_TAG_imported_declaration has unknown "
10962 "imported name - DIE at %s [in module %s]"),
10963 sect_offset_str (child_die->sect_off),
10964 objfile_name (objfile));
10965 continue;
10966 }
10967
10968 excludes.push_back (imported_name);
10969
10970 process_die (child_die, cu);
10971 }
10972
10973 add_using_directive (using_directives (cu),
10974 import_prefix,
10975 canonical_name,
10976 import_alias,
10977 imported_declaration,
10978 excludes,
10979 0,
10980 &objfile->objfile_obstack);
10981 }
10982
10983 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10984 types, but gives them a size of zero. Starting with version 14,
10985 ICC is compatible with GCC. */
10986
10987 static bool
10988 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10989 {
10990 if (!cu->checked_producer)
10991 check_producer (cu);
10992
10993 return cu->producer_is_icc_lt_14;
10994 }
10995
10996 /* ICC generates a DW_AT_type for C void functions. This was observed on
10997 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10998 which says that void functions should not have a DW_AT_type. */
10999
11000 static bool
11001 producer_is_icc (struct dwarf2_cu *cu)
11002 {
11003 if (!cu->checked_producer)
11004 check_producer (cu);
11005
11006 return cu->producer_is_icc;
11007 }
11008
11009 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11010 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11011 this, it was first present in GCC release 4.3.0. */
11012
11013 static bool
11014 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11015 {
11016 if (!cu->checked_producer)
11017 check_producer (cu);
11018
11019 return cu->producer_is_gcc_lt_4_3;
11020 }
11021
11022 static file_and_directory
11023 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11024 {
11025 file_and_directory res;
11026
11027 /* Find the filename. Do not use dwarf2_name here, since the filename
11028 is not a source language identifier. */
11029 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11030 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11031
11032 if (res.comp_dir == NULL
11033 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11034 && IS_ABSOLUTE_PATH (res.name))
11035 {
11036 res.comp_dir_storage = ldirname (res.name);
11037 if (!res.comp_dir_storage.empty ())
11038 res.comp_dir = res.comp_dir_storage.c_str ();
11039 }
11040 if (res.comp_dir != NULL)
11041 {
11042 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11043 directory, get rid of it. */
11044 const char *cp = strchr (res.comp_dir, ':');
11045
11046 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11047 res.comp_dir = cp + 1;
11048 }
11049
11050 if (res.name == NULL)
11051 res.name = "<unknown>";
11052
11053 return res;
11054 }
11055
11056 /* Handle DW_AT_stmt_list for a compilation unit.
11057 DIE is the DW_TAG_compile_unit die for CU.
11058 COMP_DIR is the compilation directory. LOWPC is passed to
11059 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11060
11061 static void
11062 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11063 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11064 {
11065 struct dwarf2_per_objfile *dwarf2_per_objfile
11066 = cu->per_cu->dwarf2_per_objfile;
11067 struct attribute *attr;
11068 struct line_header line_header_local;
11069 hashval_t line_header_local_hash;
11070 void **slot;
11071 int decode_mapping;
11072
11073 gdb_assert (! cu->per_cu->is_debug_types);
11074
11075 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11076 if (attr == NULL)
11077 return;
11078
11079 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11080
11081 /* The line header hash table is only created if needed (it exists to
11082 prevent redundant reading of the line table for partial_units).
11083 If we're given a partial_unit, we'll need it. If we're given a
11084 compile_unit, then use the line header hash table if it's already
11085 created, but don't create one just yet. */
11086
11087 if (dwarf2_per_objfile->line_header_hash == NULL
11088 && die->tag == DW_TAG_partial_unit)
11089 {
11090 dwarf2_per_objfile->line_header_hash
11091 .reset (htab_create_alloc (127, line_header_hash_voidp,
11092 line_header_eq_voidp,
11093 free_line_header_voidp,
11094 xcalloc, xfree));
11095 }
11096
11097 line_header_local.sect_off = line_offset;
11098 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11099 line_header_local_hash = line_header_hash (&line_header_local);
11100 if (dwarf2_per_objfile->line_header_hash != NULL)
11101 {
11102 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
11103 &line_header_local,
11104 line_header_local_hash, NO_INSERT);
11105
11106 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11107 is not present in *SLOT (since if there is something in *SLOT then
11108 it will be for a partial_unit). */
11109 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11110 {
11111 gdb_assert (*slot != NULL);
11112 cu->line_header = (struct line_header *) *slot;
11113 return;
11114 }
11115 }
11116
11117 /* dwarf_decode_line_header does not yet provide sufficient information.
11118 We always have to call also dwarf_decode_lines for it. */
11119 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11120 if (lh == NULL)
11121 return;
11122
11123 cu->line_header = lh.release ();
11124 cu->line_header_die_owner = die;
11125
11126 if (dwarf2_per_objfile->line_header_hash == NULL)
11127 slot = NULL;
11128 else
11129 {
11130 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
11131 &line_header_local,
11132 line_header_local_hash, INSERT);
11133 gdb_assert (slot != NULL);
11134 }
11135 if (slot != NULL && *slot == NULL)
11136 {
11137 /* This newly decoded line number information unit will be owned
11138 by line_header_hash hash table. */
11139 *slot = cu->line_header;
11140 cu->line_header_die_owner = NULL;
11141 }
11142 else
11143 {
11144 /* We cannot free any current entry in (*slot) as that struct line_header
11145 may be already used by multiple CUs. Create only temporary decoded
11146 line_header for this CU - it may happen at most once for each line
11147 number information unit. And if we're not using line_header_hash
11148 then this is what we want as well. */
11149 gdb_assert (die->tag != DW_TAG_partial_unit);
11150 }
11151 decode_mapping = (die->tag != DW_TAG_partial_unit);
11152 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11153 decode_mapping);
11154
11155 }
11156
11157 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11158
11159 static void
11160 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11161 {
11162 struct dwarf2_per_objfile *dwarf2_per_objfile
11163 = cu->per_cu->dwarf2_per_objfile;
11164 struct objfile *objfile = dwarf2_per_objfile->objfile;
11165 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11166 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11167 CORE_ADDR highpc = ((CORE_ADDR) 0);
11168 struct attribute *attr;
11169 struct die_info *child_die;
11170 CORE_ADDR baseaddr;
11171
11172 prepare_one_comp_unit (cu, die, cu->language);
11173 baseaddr = objfile->text_section_offset ();
11174
11175 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11176
11177 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11178 from finish_block. */
11179 if (lowpc == ((CORE_ADDR) -1))
11180 lowpc = highpc;
11181 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11182
11183 file_and_directory fnd = find_file_and_directory (die, cu);
11184
11185 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11186 standardised yet. As a workaround for the language detection we fall
11187 back to the DW_AT_producer string. */
11188 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11189 cu->language = language_opencl;
11190
11191 /* Similar hack for Go. */
11192 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11193 set_cu_language (DW_LANG_Go, cu);
11194
11195 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11196
11197 /* Decode line number information if present. We do this before
11198 processing child DIEs, so that the line header table is available
11199 for DW_AT_decl_file. */
11200 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11201
11202 /* Process all dies in compilation unit. */
11203 if (die->child != NULL)
11204 {
11205 child_die = die->child;
11206 while (child_die && child_die->tag)
11207 {
11208 process_die (child_die, cu);
11209 child_die = sibling_die (child_die);
11210 }
11211 }
11212
11213 /* Decode macro information, if present. Dwarf 2 macro information
11214 refers to information in the line number info statement program
11215 header, so we can only read it if we've read the header
11216 successfully. */
11217 attr = dwarf2_attr (die, DW_AT_macros, cu);
11218 if (attr == NULL)
11219 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11220 if (attr && cu->line_header)
11221 {
11222 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11223 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11224
11225 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11226 }
11227 else
11228 {
11229 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11230 if (attr && cu->line_header)
11231 {
11232 unsigned int macro_offset = DW_UNSND (attr);
11233
11234 dwarf_decode_macros (cu, macro_offset, 0);
11235 }
11236 }
11237 }
11238
11239 void
11240 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11241 {
11242 struct type_unit_group *tu_group;
11243 int first_time;
11244 struct attribute *attr;
11245 unsigned int i;
11246 struct signatured_type *sig_type;
11247
11248 gdb_assert (per_cu->is_debug_types);
11249 sig_type = (struct signatured_type *) per_cu;
11250
11251 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11252
11253 /* If we're using .gdb_index (includes -readnow) then
11254 per_cu->type_unit_group may not have been set up yet. */
11255 if (sig_type->type_unit_group == NULL)
11256 sig_type->type_unit_group = get_type_unit_group (this, attr);
11257 tu_group = sig_type->type_unit_group;
11258
11259 /* If we've already processed this stmt_list there's no real need to
11260 do it again, we could fake it and just recreate the part we need
11261 (file name,index -> symtab mapping). If data shows this optimization
11262 is useful we can do it then. */
11263 first_time = tu_group->compunit_symtab == NULL;
11264
11265 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11266 debug info. */
11267 line_header_up lh;
11268 if (attr != NULL)
11269 {
11270 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11271 lh = dwarf_decode_line_header (line_offset, this);
11272 }
11273 if (lh == NULL)
11274 {
11275 if (first_time)
11276 start_symtab ("", NULL, 0);
11277 else
11278 {
11279 gdb_assert (tu_group->symtabs == NULL);
11280 gdb_assert (m_builder == nullptr);
11281 struct compunit_symtab *cust = tu_group->compunit_symtab;
11282 m_builder.reset (new struct buildsym_compunit
11283 (COMPUNIT_OBJFILE (cust), "",
11284 COMPUNIT_DIRNAME (cust),
11285 compunit_language (cust),
11286 0, cust));
11287 }
11288 return;
11289 }
11290
11291 line_header = lh.release ();
11292 line_header_die_owner = die;
11293
11294 if (first_time)
11295 {
11296 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11297
11298 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11299 still initializing it, and our caller (a few levels up)
11300 process_full_type_unit still needs to know if this is the first
11301 time. */
11302
11303 tu_group->num_symtabs = line_header->file_names_size ();
11304 tu_group->symtabs = XNEWVEC (struct symtab *,
11305 line_header->file_names_size ());
11306
11307 auto &file_names = line_header->file_names ();
11308 for (i = 0; i < file_names.size (); ++i)
11309 {
11310 file_entry &fe = file_names[i];
11311 dwarf2_start_subfile (this, fe.name,
11312 fe.include_dir (line_header));
11313 buildsym_compunit *b = get_builder ();
11314 if (b->get_current_subfile ()->symtab == NULL)
11315 {
11316 /* NOTE: start_subfile will recognize when it's been
11317 passed a file it has already seen. So we can't
11318 assume there's a simple mapping from
11319 cu->line_header->file_names to subfiles, plus
11320 cu->line_header->file_names may contain dups. */
11321 b->get_current_subfile ()->symtab
11322 = allocate_symtab (cust, b->get_current_subfile ()->name);
11323 }
11324
11325 fe.symtab = b->get_current_subfile ()->symtab;
11326 tu_group->symtabs[i] = fe.symtab;
11327 }
11328 }
11329 else
11330 {
11331 gdb_assert (m_builder == nullptr);
11332 struct compunit_symtab *cust = tu_group->compunit_symtab;
11333 m_builder.reset (new struct buildsym_compunit
11334 (COMPUNIT_OBJFILE (cust), "",
11335 COMPUNIT_DIRNAME (cust),
11336 compunit_language (cust),
11337 0, cust));
11338
11339 auto &file_names = line_header->file_names ();
11340 for (i = 0; i < file_names.size (); ++i)
11341 {
11342 file_entry &fe = file_names[i];
11343 fe.symtab = tu_group->symtabs[i];
11344 }
11345 }
11346
11347 /* The main symtab is allocated last. Type units don't have DW_AT_name
11348 so they don't have a "real" (so to speak) symtab anyway.
11349 There is later code that will assign the main symtab to all symbols
11350 that don't have one. We need to handle the case of a symbol with a
11351 missing symtab (DW_AT_decl_file) anyway. */
11352 }
11353
11354 /* Process DW_TAG_type_unit.
11355 For TUs we want to skip the first top level sibling if it's not the
11356 actual type being defined by this TU. In this case the first top
11357 level sibling is there to provide context only. */
11358
11359 static void
11360 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11361 {
11362 struct die_info *child_die;
11363
11364 prepare_one_comp_unit (cu, die, language_minimal);
11365
11366 /* Initialize (or reinitialize) the machinery for building symtabs.
11367 We do this before processing child DIEs, so that the line header table
11368 is available for DW_AT_decl_file. */
11369 cu->setup_type_unit_groups (die);
11370
11371 if (die->child != NULL)
11372 {
11373 child_die = die->child;
11374 while (child_die && child_die->tag)
11375 {
11376 process_die (child_die, cu);
11377 child_die = sibling_die (child_die);
11378 }
11379 }
11380 }
11381 \f
11382 /* DWO/DWP files.
11383
11384 http://gcc.gnu.org/wiki/DebugFission
11385 http://gcc.gnu.org/wiki/DebugFissionDWP
11386
11387 To simplify handling of both DWO files ("object" files with the DWARF info)
11388 and DWP files (a file with the DWOs packaged up into one file), we treat
11389 DWP files as having a collection of virtual DWO files. */
11390
11391 static hashval_t
11392 hash_dwo_file (const void *item)
11393 {
11394 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11395 hashval_t hash;
11396
11397 hash = htab_hash_string (dwo_file->dwo_name);
11398 if (dwo_file->comp_dir != NULL)
11399 hash += htab_hash_string (dwo_file->comp_dir);
11400 return hash;
11401 }
11402
11403 static int
11404 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11405 {
11406 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11407 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11408
11409 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11410 return 0;
11411 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11412 return lhs->comp_dir == rhs->comp_dir;
11413 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11414 }
11415
11416 /* Allocate a hash table for DWO files. */
11417
11418 static htab_up
11419 allocate_dwo_file_hash_table (struct objfile *objfile)
11420 {
11421 auto delete_dwo_file = [] (void *item)
11422 {
11423 struct dwo_file *dwo_file = (struct dwo_file *) item;
11424
11425 delete dwo_file;
11426 };
11427
11428 return htab_up (htab_create_alloc (41,
11429 hash_dwo_file,
11430 eq_dwo_file,
11431 delete_dwo_file,
11432 xcalloc, xfree));
11433 }
11434
11435 /* Lookup DWO file DWO_NAME. */
11436
11437 static void **
11438 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11439 const char *dwo_name,
11440 const char *comp_dir)
11441 {
11442 struct dwo_file find_entry;
11443 void **slot;
11444
11445 if (dwarf2_per_objfile->dwo_files == NULL)
11446 dwarf2_per_objfile->dwo_files
11447 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11448
11449 find_entry.dwo_name = dwo_name;
11450 find_entry.comp_dir = comp_dir;
11451 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11452 INSERT);
11453
11454 return slot;
11455 }
11456
11457 static hashval_t
11458 hash_dwo_unit (const void *item)
11459 {
11460 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11461
11462 /* This drops the top 32 bits of the id, but is ok for a hash. */
11463 return dwo_unit->signature;
11464 }
11465
11466 static int
11467 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11468 {
11469 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11470 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11471
11472 /* The signature is assumed to be unique within the DWO file.
11473 So while object file CU dwo_id's always have the value zero,
11474 that's OK, assuming each object file DWO file has only one CU,
11475 and that's the rule for now. */
11476 return lhs->signature == rhs->signature;
11477 }
11478
11479 /* Allocate a hash table for DWO CUs,TUs.
11480 There is one of these tables for each of CUs,TUs for each DWO file. */
11481
11482 static htab_up
11483 allocate_dwo_unit_table (struct objfile *objfile)
11484 {
11485 /* Start out with a pretty small number.
11486 Generally DWO files contain only one CU and maybe some TUs. */
11487 return htab_up (htab_create_alloc (3,
11488 hash_dwo_unit,
11489 eq_dwo_unit,
11490 NULL, xcalloc, xfree));
11491 }
11492
11493 /* die_reader_func for create_dwo_cu. */
11494
11495 static void
11496 create_dwo_cu_reader (const struct die_reader_specs *reader,
11497 const gdb_byte *info_ptr,
11498 struct die_info *comp_unit_die,
11499 struct dwo_file *dwo_file,
11500 struct dwo_unit *dwo_unit)
11501 {
11502 struct dwarf2_cu *cu = reader->cu;
11503 sect_offset sect_off = cu->per_cu->sect_off;
11504 struct dwarf2_section_info *section = cu->per_cu->section;
11505
11506 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11507 if (!signature.has_value ())
11508 {
11509 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11510 " its dwo_id [in module %s]"),
11511 sect_offset_str (sect_off), dwo_file->dwo_name);
11512 return;
11513 }
11514
11515 dwo_unit->dwo_file = dwo_file;
11516 dwo_unit->signature = *signature;
11517 dwo_unit->section = section;
11518 dwo_unit->sect_off = sect_off;
11519 dwo_unit->length = cu->per_cu->length;
11520
11521 if (dwarf_read_debug)
11522 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11523 sect_offset_str (sect_off),
11524 hex_string (dwo_unit->signature));
11525 }
11526
11527 /* Create the dwo_units for the CUs in a DWO_FILE.
11528 Note: This function processes DWO files only, not DWP files. */
11529
11530 static void
11531 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11532 dwarf2_cu *cu, struct dwo_file &dwo_file,
11533 dwarf2_section_info &section, htab_up &cus_htab)
11534 {
11535 struct objfile *objfile = dwarf2_per_objfile->objfile;
11536 const gdb_byte *info_ptr, *end_ptr;
11537
11538 section.read (objfile);
11539 info_ptr = section.buffer;
11540
11541 if (info_ptr == NULL)
11542 return;
11543
11544 if (dwarf_read_debug)
11545 {
11546 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11547 section.get_name (),
11548 section.get_file_name ());
11549 }
11550
11551 end_ptr = info_ptr + section.size;
11552 while (info_ptr < end_ptr)
11553 {
11554 struct dwarf2_per_cu_data per_cu;
11555 struct dwo_unit read_unit {};
11556 struct dwo_unit *dwo_unit;
11557 void **slot;
11558 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11559
11560 memset (&per_cu, 0, sizeof (per_cu));
11561 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11562 per_cu.is_debug_types = 0;
11563 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11564 per_cu.section = &section;
11565
11566 cutu_reader reader (&per_cu, cu, &dwo_file);
11567 if (!reader.dummy_p)
11568 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11569 &dwo_file, &read_unit);
11570 info_ptr += per_cu.length;
11571
11572 // If the unit could not be parsed, skip it.
11573 if (read_unit.dwo_file == NULL)
11574 continue;
11575
11576 if (cus_htab == NULL)
11577 cus_htab = allocate_dwo_unit_table (objfile);
11578
11579 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11580 *dwo_unit = read_unit;
11581 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11582 gdb_assert (slot != NULL);
11583 if (*slot != NULL)
11584 {
11585 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11586 sect_offset dup_sect_off = dup_cu->sect_off;
11587
11588 complaint (_("debug cu entry at offset %s is duplicate to"
11589 " the entry at offset %s, signature %s"),
11590 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11591 hex_string (dwo_unit->signature));
11592 }
11593 *slot = (void *)dwo_unit;
11594 }
11595 }
11596
11597 /* DWP file .debug_{cu,tu}_index section format:
11598 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11599
11600 DWP Version 1:
11601
11602 Both index sections have the same format, and serve to map a 64-bit
11603 signature to a set of section numbers. Each section begins with a header,
11604 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11605 indexes, and a pool of 32-bit section numbers. The index sections will be
11606 aligned at 8-byte boundaries in the file.
11607
11608 The index section header consists of:
11609
11610 V, 32 bit version number
11611 -, 32 bits unused
11612 N, 32 bit number of compilation units or type units in the index
11613 M, 32 bit number of slots in the hash table
11614
11615 Numbers are recorded using the byte order of the application binary.
11616
11617 The hash table begins at offset 16 in the section, and consists of an array
11618 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11619 order of the application binary). Unused slots in the hash table are 0.
11620 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11621
11622 The parallel table begins immediately after the hash table
11623 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11624 array of 32-bit indexes (using the byte order of the application binary),
11625 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11626 table contains a 32-bit index into the pool of section numbers. For unused
11627 hash table slots, the corresponding entry in the parallel table will be 0.
11628
11629 The pool of section numbers begins immediately following the hash table
11630 (at offset 16 + 12 * M from the beginning of the section). The pool of
11631 section numbers consists of an array of 32-bit words (using the byte order
11632 of the application binary). Each item in the array is indexed starting
11633 from 0. The hash table entry provides the index of the first section
11634 number in the set. Additional section numbers in the set follow, and the
11635 set is terminated by a 0 entry (section number 0 is not used in ELF).
11636
11637 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11638 section must be the first entry in the set, and the .debug_abbrev.dwo must
11639 be the second entry. Other members of the set may follow in any order.
11640
11641 ---
11642
11643 DWP Version 2:
11644
11645 DWP Version 2 combines all the .debug_info, etc. sections into one,
11646 and the entries in the index tables are now offsets into these sections.
11647 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11648 section.
11649
11650 Index Section Contents:
11651 Header
11652 Hash Table of Signatures dwp_hash_table.hash_table
11653 Parallel Table of Indices dwp_hash_table.unit_table
11654 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11655 Table of Section Sizes dwp_hash_table.v2.sizes
11656
11657 The index section header consists of:
11658
11659 V, 32 bit version number
11660 L, 32 bit number of columns in the table of section offsets
11661 N, 32 bit number of compilation units or type units in the index
11662 M, 32 bit number of slots in the hash table
11663
11664 Numbers are recorded using the byte order of the application binary.
11665
11666 The hash table has the same format as version 1.
11667 The parallel table of indices has the same format as version 1,
11668 except that the entries are origin-1 indices into the table of sections
11669 offsets and the table of section sizes.
11670
11671 The table of offsets begins immediately following the parallel table
11672 (at offset 16 + 12 * M from the beginning of the section). The table is
11673 a two-dimensional array of 32-bit words (using the byte order of the
11674 application binary), with L columns and N+1 rows, in row-major order.
11675 Each row in the array is indexed starting from 0. The first row provides
11676 a key to the remaining rows: each column in this row provides an identifier
11677 for a debug section, and the offsets in the same column of subsequent rows
11678 refer to that section. The section identifiers are:
11679
11680 DW_SECT_INFO 1 .debug_info.dwo
11681 DW_SECT_TYPES 2 .debug_types.dwo
11682 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11683 DW_SECT_LINE 4 .debug_line.dwo
11684 DW_SECT_LOC 5 .debug_loc.dwo
11685 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11686 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11687 DW_SECT_MACRO 8 .debug_macro.dwo
11688
11689 The offsets provided by the CU and TU index sections are the base offsets
11690 for the contributions made by each CU or TU to the corresponding section
11691 in the package file. Each CU and TU header contains an abbrev_offset
11692 field, used to find the abbreviations table for that CU or TU within the
11693 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11694 be interpreted as relative to the base offset given in the index section.
11695 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11696 should be interpreted as relative to the base offset for .debug_line.dwo,
11697 and offsets into other debug sections obtained from DWARF attributes should
11698 also be interpreted as relative to the corresponding base offset.
11699
11700 The table of sizes begins immediately following the table of offsets.
11701 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11702 with L columns and N rows, in row-major order. Each row in the array is
11703 indexed starting from 1 (row 0 is shared by the two tables).
11704
11705 ---
11706
11707 Hash table lookup is handled the same in version 1 and 2:
11708
11709 We assume that N and M will not exceed 2^32 - 1.
11710 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11711
11712 Given a 64-bit compilation unit signature or a type signature S, an entry
11713 in the hash table is located as follows:
11714
11715 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11716 the low-order k bits all set to 1.
11717
11718 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11719
11720 3) If the hash table entry at index H matches the signature, use that
11721 entry. If the hash table entry at index H is unused (all zeroes),
11722 terminate the search: the signature is not present in the table.
11723
11724 4) Let H = (H + H') modulo M. Repeat at Step 3.
11725
11726 Because M > N and H' and M are relatively prime, the search is guaranteed
11727 to stop at an unused slot or find the match. */
11728
11729 /* Create a hash table to map DWO IDs to their CU/TU entry in
11730 .debug_{info,types}.dwo in DWP_FILE.
11731 Returns NULL if there isn't one.
11732 Note: This function processes DWP files only, not DWO files. */
11733
11734 static struct dwp_hash_table *
11735 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11736 struct dwp_file *dwp_file, int is_debug_types)
11737 {
11738 struct objfile *objfile = dwarf2_per_objfile->objfile;
11739 bfd *dbfd = dwp_file->dbfd.get ();
11740 const gdb_byte *index_ptr, *index_end;
11741 struct dwarf2_section_info *index;
11742 uint32_t version, nr_columns, nr_units, nr_slots;
11743 struct dwp_hash_table *htab;
11744
11745 if (is_debug_types)
11746 index = &dwp_file->sections.tu_index;
11747 else
11748 index = &dwp_file->sections.cu_index;
11749
11750 if (index->empty ())
11751 return NULL;
11752 index->read (objfile);
11753
11754 index_ptr = index->buffer;
11755 index_end = index_ptr + index->size;
11756
11757 version = read_4_bytes (dbfd, index_ptr);
11758 index_ptr += 4;
11759 if (version == 2)
11760 nr_columns = read_4_bytes (dbfd, index_ptr);
11761 else
11762 nr_columns = 0;
11763 index_ptr += 4;
11764 nr_units = read_4_bytes (dbfd, index_ptr);
11765 index_ptr += 4;
11766 nr_slots = read_4_bytes (dbfd, index_ptr);
11767 index_ptr += 4;
11768
11769 if (version != 1 && version != 2)
11770 {
11771 error (_("Dwarf Error: unsupported DWP file version (%s)"
11772 " [in module %s]"),
11773 pulongest (version), dwp_file->name);
11774 }
11775 if (nr_slots != (nr_slots & -nr_slots))
11776 {
11777 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11778 " is not power of 2 [in module %s]"),
11779 pulongest (nr_slots), dwp_file->name);
11780 }
11781
11782 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11783 htab->version = version;
11784 htab->nr_columns = nr_columns;
11785 htab->nr_units = nr_units;
11786 htab->nr_slots = nr_slots;
11787 htab->hash_table = index_ptr;
11788 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11789
11790 /* Exit early if the table is empty. */
11791 if (nr_slots == 0 || nr_units == 0
11792 || (version == 2 && nr_columns == 0))
11793 {
11794 /* All must be zero. */
11795 if (nr_slots != 0 || nr_units != 0
11796 || (version == 2 && nr_columns != 0))
11797 {
11798 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11799 " all zero [in modules %s]"),
11800 dwp_file->name);
11801 }
11802 return htab;
11803 }
11804
11805 if (version == 1)
11806 {
11807 htab->section_pool.v1.indices =
11808 htab->unit_table + sizeof (uint32_t) * nr_slots;
11809 /* It's harder to decide whether the section is too small in v1.
11810 V1 is deprecated anyway so we punt. */
11811 }
11812 else
11813 {
11814 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11815 int *ids = htab->section_pool.v2.section_ids;
11816 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11817 /* Reverse map for error checking. */
11818 int ids_seen[DW_SECT_MAX + 1];
11819 int i;
11820
11821 if (nr_columns < 2)
11822 {
11823 error (_("Dwarf Error: bad DWP hash table, too few columns"
11824 " in section table [in module %s]"),
11825 dwp_file->name);
11826 }
11827 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11828 {
11829 error (_("Dwarf Error: bad DWP hash table, too many columns"
11830 " in section table [in module %s]"),
11831 dwp_file->name);
11832 }
11833 memset (ids, 255, sizeof_ids);
11834 memset (ids_seen, 255, sizeof (ids_seen));
11835 for (i = 0; i < nr_columns; ++i)
11836 {
11837 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11838
11839 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11840 {
11841 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11842 " in section table [in module %s]"),
11843 id, dwp_file->name);
11844 }
11845 if (ids_seen[id] != -1)
11846 {
11847 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11848 " id %d in section table [in module %s]"),
11849 id, dwp_file->name);
11850 }
11851 ids_seen[id] = i;
11852 ids[i] = id;
11853 }
11854 /* Must have exactly one info or types section. */
11855 if (((ids_seen[DW_SECT_INFO] != -1)
11856 + (ids_seen[DW_SECT_TYPES] != -1))
11857 != 1)
11858 {
11859 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11860 " DWO info/types section [in module %s]"),
11861 dwp_file->name);
11862 }
11863 /* Must have an abbrev section. */
11864 if (ids_seen[DW_SECT_ABBREV] == -1)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11867 " section [in module %s]"),
11868 dwp_file->name);
11869 }
11870 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11871 htab->section_pool.v2.sizes =
11872 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11873 * nr_units * nr_columns);
11874 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11875 * nr_units * nr_columns))
11876 > index_end)
11877 {
11878 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11879 " [in module %s]"),
11880 dwp_file->name);
11881 }
11882 }
11883
11884 return htab;
11885 }
11886
11887 /* Update SECTIONS with the data from SECTP.
11888
11889 This function is like the other "locate" section routines that are
11890 passed to bfd_map_over_sections, but in this context the sections to
11891 read comes from the DWP V1 hash table, not the full ELF section table.
11892
11893 The result is non-zero for success, or zero if an error was found. */
11894
11895 static int
11896 locate_v1_virtual_dwo_sections (asection *sectp,
11897 struct virtual_v1_dwo_sections *sections)
11898 {
11899 const struct dwop_section_names *names = &dwop_section_names;
11900
11901 if (section_is_p (sectp->name, &names->abbrev_dwo))
11902 {
11903 /* There can be only one. */
11904 if (sections->abbrev.s.section != NULL)
11905 return 0;
11906 sections->abbrev.s.section = sectp;
11907 sections->abbrev.size = bfd_section_size (sectp);
11908 }
11909 else if (section_is_p (sectp->name, &names->info_dwo)
11910 || section_is_p (sectp->name, &names->types_dwo))
11911 {
11912 /* There can be only one. */
11913 if (sections->info_or_types.s.section != NULL)
11914 return 0;
11915 sections->info_or_types.s.section = sectp;
11916 sections->info_or_types.size = bfd_section_size (sectp);
11917 }
11918 else if (section_is_p (sectp->name, &names->line_dwo))
11919 {
11920 /* There can be only one. */
11921 if (sections->line.s.section != NULL)
11922 return 0;
11923 sections->line.s.section = sectp;
11924 sections->line.size = bfd_section_size (sectp);
11925 }
11926 else if (section_is_p (sectp->name, &names->loc_dwo))
11927 {
11928 /* There can be only one. */
11929 if (sections->loc.s.section != NULL)
11930 return 0;
11931 sections->loc.s.section = sectp;
11932 sections->loc.size = bfd_section_size (sectp);
11933 }
11934 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11935 {
11936 /* There can be only one. */
11937 if (sections->macinfo.s.section != NULL)
11938 return 0;
11939 sections->macinfo.s.section = sectp;
11940 sections->macinfo.size = bfd_section_size (sectp);
11941 }
11942 else if (section_is_p (sectp->name, &names->macro_dwo))
11943 {
11944 /* There can be only one. */
11945 if (sections->macro.s.section != NULL)
11946 return 0;
11947 sections->macro.s.section = sectp;
11948 sections->macro.size = bfd_section_size (sectp);
11949 }
11950 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11951 {
11952 /* There can be only one. */
11953 if (sections->str_offsets.s.section != NULL)
11954 return 0;
11955 sections->str_offsets.s.section = sectp;
11956 sections->str_offsets.size = bfd_section_size (sectp);
11957 }
11958 else
11959 {
11960 /* No other kind of section is valid. */
11961 return 0;
11962 }
11963
11964 return 1;
11965 }
11966
11967 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11968 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11969 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11970 This is for DWP version 1 files. */
11971
11972 static struct dwo_unit *
11973 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11974 struct dwp_file *dwp_file,
11975 uint32_t unit_index,
11976 const char *comp_dir,
11977 ULONGEST signature, int is_debug_types)
11978 {
11979 struct objfile *objfile = dwarf2_per_objfile->objfile;
11980 const struct dwp_hash_table *dwp_htab =
11981 is_debug_types ? dwp_file->tus : dwp_file->cus;
11982 bfd *dbfd = dwp_file->dbfd.get ();
11983 const char *kind = is_debug_types ? "TU" : "CU";
11984 struct dwo_file *dwo_file;
11985 struct dwo_unit *dwo_unit;
11986 struct virtual_v1_dwo_sections sections;
11987 void **dwo_file_slot;
11988 int i;
11989
11990 gdb_assert (dwp_file->version == 1);
11991
11992 if (dwarf_read_debug)
11993 {
11994 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11995 kind,
11996 pulongest (unit_index), hex_string (signature),
11997 dwp_file->name);
11998 }
11999
12000 /* Fetch the sections of this DWO unit.
12001 Put a limit on the number of sections we look for so that bad data
12002 doesn't cause us to loop forever. */
12003
12004 #define MAX_NR_V1_DWO_SECTIONS \
12005 (1 /* .debug_info or .debug_types */ \
12006 + 1 /* .debug_abbrev */ \
12007 + 1 /* .debug_line */ \
12008 + 1 /* .debug_loc */ \
12009 + 1 /* .debug_str_offsets */ \
12010 + 1 /* .debug_macro or .debug_macinfo */ \
12011 + 1 /* trailing zero */)
12012
12013 memset (&sections, 0, sizeof (sections));
12014
12015 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12016 {
12017 asection *sectp;
12018 uint32_t section_nr =
12019 read_4_bytes (dbfd,
12020 dwp_htab->section_pool.v1.indices
12021 + (unit_index + i) * sizeof (uint32_t));
12022
12023 if (section_nr == 0)
12024 break;
12025 if (section_nr >= dwp_file->num_sections)
12026 {
12027 error (_("Dwarf Error: bad DWP hash table, section number too large"
12028 " [in module %s]"),
12029 dwp_file->name);
12030 }
12031
12032 sectp = dwp_file->elf_sections[section_nr];
12033 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12034 {
12035 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12036 " [in module %s]"),
12037 dwp_file->name);
12038 }
12039 }
12040
12041 if (i < 2
12042 || sections.info_or_types.empty ()
12043 || sections.abbrev.empty ())
12044 {
12045 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12046 " [in module %s]"),
12047 dwp_file->name);
12048 }
12049 if (i == MAX_NR_V1_DWO_SECTIONS)
12050 {
12051 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12052 " [in module %s]"),
12053 dwp_file->name);
12054 }
12055
12056 /* It's easier for the rest of the code if we fake a struct dwo_file and
12057 have dwo_unit "live" in that. At least for now.
12058
12059 The DWP file can be made up of a random collection of CUs and TUs.
12060 However, for each CU + set of TUs that came from the same original DWO
12061 file, we can combine them back into a virtual DWO file to save space
12062 (fewer struct dwo_file objects to allocate). Remember that for really
12063 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12064
12065 std::string virtual_dwo_name =
12066 string_printf ("virtual-dwo/%d-%d-%d-%d",
12067 sections.abbrev.get_id (),
12068 sections.line.get_id (),
12069 sections.loc.get_id (),
12070 sections.str_offsets.get_id ());
12071 /* Can we use an existing virtual DWO file? */
12072 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12073 virtual_dwo_name.c_str (),
12074 comp_dir);
12075 /* Create one if necessary. */
12076 if (*dwo_file_slot == NULL)
12077 {
12078 if (dwarf_read_debug)
12079 {
12080 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12081 virtual_dwo_name.c_str ());
12082 }
12083 dwo_file = new struct dwo_file;
12084 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12085 virtual_dwo_name);
12086 dwo_file->comp_dir = comp_dir;
12087 dwo_file->sections.abbrev = sections.abbrev;
12088 dwo_file->sections.line = sections.line;
12089 dwo_file->sections.loc = sections.loc;
12090 dwo_file->sections.macinfo = sections.macinfo;
12091 dwo_file->sections.macro = sections.macro;
12092 dwo_file->sections.str_offsets = sections.str_offsets;
12093 /* The "str" section is global to the entire DWP file. */
12094 dwo_file->sections.str = dwp_file->sections.str;
12095 /* The info or types section is assigned below to dwo_unit,
12096 there's no need to record it in dwo_file.
12097 Also, we can't simply record type sections in dwo_file because
12098 we record a pointer into the vector in dwo_unit. As we collect more
12099 types we'll grow the vector and eventually have to reallocate space
12100 for it, invalidating all copies of pointers into the previous
12101 contents. */
12102 *dwo_file_slot = dwo_file;
12103 }
12104 else
12105 {
12106 if (dwarf_read_debug)
12107 {
12108 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12109 virtual_dwo_name.c_str ());
12110 }
12111 dwo_file = (struct dwo_file *) *dwo_file_slot;
12112 }
12113
12114 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12115 dwo_unit->dwo_file = dwo_file;
12116 dwo_unit->signature = signature;
12117 dwo_unit->section =
12118 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12119 *dwo_unit->section = sections.info_or_types;
12120 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12121
12122 return dwo_unit;
12123 }
12124
12125 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12126 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12127 piece within that section used by a TU/CU, return a virtual section
12128 of just that piece. */
12129
12130 static struct dwarf2_section_info
12131 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12132 struct dwarf2_section_info *section,
12133 bfd_size_type offset, bfd_size_type size)
12134 {
12135 struct dwarf2_section_info result;
12136 asection *sectp;
12137
12138 gdb_assert (section != NULL);
12139 gdb_assert (!section->is_virtual);
12140
12141 memset (&result, 0, sizeof (result));
12142 result.s.containing_section = section;
12143 result.is_virtual = true;
12144
12145 if (size == 0)
12146 return result;
12147
12148 sectp = section->get_bfd_section ();
12149
12150 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12151 bounds of the real section. This is a pretty-rare event, so just
12152 flag an error (easier) instead of a warning and trying to cope. */
12153 if (sectp == NULL
12154 || offset + size > bfd_section_size (sectp))
12155 {
12156 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12157 " in section %s [in module %s]"),
12158 sectp ? bfd_section_name (sectp) : "<unknown>",
12159 objfile_name (dwarf2_per_objfile->objfile));
12160 }
12161
12162 result.virtual_offset = offset;
12163 result.size = size;
12164 return result;
12165 }
12166
12167 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12168 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12169 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12170 This is for DWP version 2 files. */
12171
12172 static struct dwo_unit *
12173 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12174 struct dwp_file *dwp_file,
12175 uint32_t unit_index,
12176 const char *comp_dir,
12177 ULONGEST signature, int is_debug_types)
12178 {
12179 struct objfile *objfile = dwarf2_per_objfile->objfile;
12180 const struct dwp_hash_table *dwp_htab =
12181 is_debug_types ? dwp_file->tus : dwp_file->cus;
12182 bfd *dbfd = dwp_file->dbfd.get ();
12183 const char *kind = is_debug_types ? "TU" : "CU";
12184 struct dwo_file *dwo_file;
12185 struct dwo_unit *dwo_unit;
12186 struct virtual_v2_dwo_sections sections;
12187 void **dwo_file_slot;
12188 int i;
12189
12190 gdb_assert (dwp_file->version == 2);
12191
12192 if (dwarf_read_debug)
12193 {
12194 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12195 kind,
12196 pulongest (unit_index), hex_string (signature),
12197 dwp_file->name);
12198 }
12199
12200 /* Fetch the section offsets of this DWO unit. */
12201
12202 memset (&sections, 0, sizeof (sections));
12203
12204 for (i = 0; i < dwp_htab->nr_columns; ++i)
12205 {
12206 uint32_t offset = read_4_bytes (dbfd,
12207 dwp_htab->section_pool.v2.offsets
12208 + (((unit_index - 1) * dwp_htab->nr_columns
12209 + i)
12210 * sizeof (uint32_t)));
12211 uint32_t size = read_4_bytes (dbfd,
12212 dwp_htab->section_pool.v2.sizes
12213 + (((unit_index - 1) * dwp_htab->nr_columns
12214 + i)
12215 * sizeof (uint32_t)));
12216
12217 switch (dwp_htab->section_pool.v2.section_ids[i])
12218 {
12219 case DW_SECT_INFO:
12220 case DW_SECT_TYPES:
12221 sections.info_or_types_offset = offset;
12222 sections.info_or_types_size = size;
12223 break;
12224 case DW_SECT_ABBREV:
12225 sections.abbrev_offset = offset;
12226 sections.abbrev_size = size;
12227 break;
12228 case DW_SECT_LINE:
12229 sections.line_offset = offset;
12230 sections.line_size = size;
12231 break;
12232 case DW_SECT_LOC:
12233 sections.loc_offset = offset;
12234 sections.loc_size = size;
12235 break;
12236 case DW_SECT_STR_OFFSETS:
12237 sections.str_offsets_offset = offset;
12238 sections.str_offsets_size = size;
12239 break;
12240 case DW_SECT_MACINFO:
12241 sections.macinfo_offset = offset;
12242 sections.macinfo_size = size;
12243 break;
12244 case DW_SECT_MACRO:
12245 sections.macro_offset = offset;
12246 sections.macro_size = size;
12247 break;
12248 }
12249 }
12250
12251 /* It's easier for the rest of the code if we fake a struct dwo_file and
12252 have dwo_unit "live" in that. At least for now.
12253
12254 The DWP file can be made up of a random collection of CUs and TUs.
12255 However, for each CU + set of TUs that came from the same original DWO
12256 file, we can combine them back into a virtual DWO file to save space
12257 (fewer struct dwo_file objects to allocate). Remember that for really
12258 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12259
12260 std::string virtual_dwo_name =
12261 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12262 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12263 (long) (sections.line_size ? sections.line_offset : 0),
12264 (long) (sections.loc_size ? sections.loc_offset : 0),
12265 (long) (sections.str_offsets_size
12266 ? sections.str_offsets_offset : 0));
12267 /* Can we use an existing virtual DWO file? */
12268 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12269 virtual_dwo_name.c_str (),
12270 comp_dir);
12271 /* Create one if necessary. */
12272 if (*dwo_file_slot == NULL)
12273 {
12274 if (dwarf_read_debug)
12275 {
12276 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12277 virtual_dwo_name.c_str ());
12278 }
12279 dwo_file = new struct dwo_file;
12280 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12281 virtual_dwo_name);
12282 dwo_file->comp_dir = comp_dir;
12283 dwo_file->sections.abbrev =
12284 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12285 sections.abbrev_offset, sections.abbrev_size);
12286 dwo_file->sections.line =
12287 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12288 sections.line_offset, sections.line_size);
12289 dwo_file->sections.loc =
12290 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12291 sections.loc_offset, sections.loc_size);
12292 dwo_file->sections.macinfo =
12293 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12294 sections.macinfo_offset, sections.macinfo_size);
12295 dwo_file->sections.macro =
12296 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12297 sections.macro_offset, sections.macro_size);
12298 dwo_file->sections.str_offsets =
12299 create_dwp_v2_section (dwarf2_per_objfile,
12300 &dwp_file->sections.str_offsets,
12301 sections.str_offsets_offset,
12302 sections.str_offsets_size);
12303 /* The "str" section is global to the entire DWP file. */
12304 dwo_file->sections.str = dwp_file->sections.str;
12305 /* The info or types section is assigned below to dwo_unit,
12306 there's no need to record it in dwo_file.
12307 Also, we can't simply record type sections in dwo_file because
12308 we record a pointer into the vector in dwo_unit. As we collect more
12309 types we'll grow the vector and eventually have to reallocate space
12310 for it, invalidating all copies of pointers into the previous
12311 contents. */
12312 *dwo_file_slot = dwo_file;
12313 }
12314 else
12315 {
12316 if (dwarf_read_debug)
12317 {
12318 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12319 virtual_dwo_name.c_str ());
12320 }
12321 dwo_file = (struct dwo_file *) *dwo_file_slot;
12322 }
12323
12324 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12325 dwo_unit->dwo_file = dwo_file;
12326 dwo_unit->signature = signature;
12327 dwo_unit->section =
12328 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12329 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12330 is_debug_types
12331 ? &dwp_file->sections.types
12332 : &dwp_file->sections.info,
12333 sections.info_or_types_offset,
12334 sections.info_or_types_size);
12335 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12336
12337 return dwo_unit;
12338 }
12339
12340 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12341 Returns NULL if the signature isn't found. */
12342
12343 static struct dwo_unit *
12344 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12345 struct dwp_file *dwp_file, const char *comp_dir,
12346 ULONGEST signature, int is_debug_types)
12347 {
12348 const struct dwp_hash_table *dwp_htab =
12349 is_debug_types ? dwp_file->tus : dwp_file->cus;
12350 bfd *dbfd = dwp_file->dbfd.get ();
12351 uint32_t mask = dwp_htab->nr_slots - 1;
12352 uint32_t hash = signature & mask;
12353 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12354 unsigned int i;
12355 void **slot;
12356 struct dwo_unit find_dwo_cu;
12357
12358 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12359 find_dwo_cu.signature = signature;
12360 slot = htab_find_slot (is_debug_types
12361 ? dwp_file->loaded_tus.get ()
12362 : dwp_file->loaded_cus.get (),
12363 &find_dwo_cu, INSERT);
12364
12365 if (*slot != NULL)
12366 return (struct dwo_unit *) *slot;
12367
12368 /* Use a for loop so that we don't loop forever on bad debug info. */
12369 for (i = 0; i < dwp_htab->nr_slots; ++i)
12370 {
12371 ULONGEST signature_in_table;
12372
12373 signature_in_table =
12374 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12375 if (signature_in_table == signature)
12376 {
12377 uint32_t unit_index =
12378 read_4_bytes (dbfd,
12379 dwp_htab->unit_table + hash * sizeof (uint32_t));
12380
12381 if (dwp_file->version == 1)
12382 {
12383 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12384 dwp_file, unit_index,
12385 comp_dir, signature,
12386 is_debug_types);
12387 }
12388 else
12389 {
12390 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12391 dwp_file, unit_index,
12392 comp_dir, signature,
12393 is_debug_types);
12394 }
12395 return (struct dwo_unit *) *slot;
12396 }
12397 if (signature_in_table == 0)
12398 return NULL;
12399 hash = (hash + hash2) & mask;
12400 }
12401
12402 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12403 " [in module %s]"),
12404 dwp_file->name);
12405 }
12406
12407 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12408 Open the file specified by FILE_NAME and hand it off to BFD for
12409 preliminary analysis. Return a newly initialized bfd *, which
12410 includes a canonicalized copy of FILE_NAME.
12411 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12412 SEARCH_CWD is true if the current directory is to be searched.
12413 It will be searched before debug-file-directory.
12414 If successful, the file is added to the bfd include table of the
12415 objfile's bfd (see gdb_bfd_record_inclusion).
12416 If unable to find/open the file, return NULL.
12417 NOTE: This function is derived from symfile_bfd_open. */
12418
12419 static gdb_bfd_ref_ptr
12420 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12421 const char *file_name, int is_dwp, int search_cwd)
12422 {
12423 int desc;
12424 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12425 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12426 to debug_file_directory. */
12427 const char *search_path;
12428 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12429
12430 gdb::unique_xmalloc_ptr<char> search_path_holder;
12431 if (search_cwd)
12432 {
12433 if (*debug_file_directory != '\0')
12434 {
12435 search_path_holder.reset (concat (".", dirname_separator_string,
12436 debug_file_directory,
12437 (char *) NULL));
12438 search_path = search_path_holder.get ();
12439 }
12440 else
12441 search_path = ".";
12442 }
12443 else
12444 search_path = debug_file_directory;
12445
12446 openp_flags flags = OPF_RETURN_REALPATH;
12447 if (is_dwp)
12448 flags |= OPF_SEARCH_IN_PATH;
12449
12450 gdb::unique_xmalloc_ptr<char> absolute_name;
12451 desc = openp (search_path, flags, file_name,
12452 O_RDONLY | O_BINARY, &absolute_name);
12453 if (desc < 0)
12454 return NULL;
12455
12456 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12457 gnutarget, desc));
12458 if (sym_bfd == NULL)
12459 return NULL;
12460 bfd_set_cacheable (sym_bfd.get (), 1);
12461
12462 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12463 return NULL;
12464
12465 /* Success. Record the bfd as having been included by the objfile's bfd.
12466 This is important because things like demangled_names_hash lives in the
12467 objfile's per_bfd space and may have references to things like symbol
12468 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12469 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12470
12471 return sym_bfd;
12472 }
12473
12474 /* Try to open DWO file FILE_NAME.
12475 COMP_DIR is the DW_AT_comp_dir attribute.
12476 The result is the bfd handle of the file.
12477 If there is a problem finding or opening the file, return NULL.
12478 Upon success, the canonicalized path of the file is stored in the bfd,
12479 same as symfile_bfd_open. */
12480
12481 static gdb_bfd_ref_ptr
12482 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12483 const char *file_name, const char *comp_dir)
12484 {
12485 if (IS_ABSOLUTE_PATH (file_name))
12486 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12487 0 /*is_dwp*/, 0 /*search_cwd*/);
12488
12489 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12490
12491 if (comp_dir != NULL)
12492 {
12493 gdb::unique_xmalloc_ptr<char> path_to_try
12494 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12495
12496 /* NOTE: If comp_dir is a relative path, this will also try the
12497 search path, which seems useful. */
12498 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12499 path_to_try.get (),
12500 0 /*is_dwp*/,
12501 1 /*search_cwd*/));
12502 if (abfd != NULL)
12503 return abfd;
12504 }
12505
12506 /* That didn't work, try debug-file-directory, which, despite its name,
12507 is a list of paths. */
12508
12509 if (*debug_file_directory == '\0')
12510 return NULL;
12511
12512 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12513 0 /*is_dwp*/, 1 /*search_cwd*/);
12514 }
12515
12516 /* This function is mapped across the sections and remembers the offset and
12517 size of each of the DWO debugging sections we are interested in. */
12518
12519 static void
12520 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12521 {
12522 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12523 const struct dwop_section_names *names = &dwop_section_names;
12524
12525 if (section_is_p (sectp->name, &names->abbrev_dwo))
12526 {
12527 dwo_sections->abbrev.s.section = sectp;
12528 dwo_sections->abbrev.size = bfd_section_size (sectp);
12529 }
12530 else if (section_is_p (sectp->name, &names->info_dwo))
12531 {
12532 dwo_sections->info.s.section = sectp;
12533 dwo_sections->info.size = bfd_section_size (sectp);
12534 }
12535 else if (section_is_p (sectp->name, &names->line_dwo))
12536 {
12537 dwo_sections->line.s.section = sectp;
12538 dwo_sections->line.size = bfd_section_size (sectp);
12539 }
12540 else if (section_is_p (sectp->name, &names->loc_dwo))
12541 {
12542 dwo_sections->loc.s.section = sectp;
12543 dwo_sections->loc.size = bfd_section_size (sectp);
12544 }
12545 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12546 {
12547 dwo_sections->macinfo.s.section = sectp;
12548 dwo_sections->macinfo.size = bfd_section_size (sectp);
12549 }
12550 else if (section_is_p (sectp->name, &names->macro_dwo))
12551 {
12552 dwo_sections->macro.s.section = sectp;
12553 dwo_sections->macro.size = bfd_section_size (sectp);
12554 }
12555 else if (section_is_p (sectp->name, &names->str_dwo))
12556 {
12557 dwo_sections->str.s.section = sectp;
12558 dwo_sections->str.size = bfd_section_size (sectp);
12559 }
12560 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12561 {
12562 dwo_sections->str_offsets.s.section = sectp;
12563 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12564 }
12565 else if (section_is_p (sectp->name, &names->types_dwo))
12566 {
12567 struct dwarf2_section_info type_section;
12568
12569 memset (&type_section, 0, sizeof (type_section));
12570 type_section.s.section = sectp;
12571 type_section.size = bfd_section_size (sectp);
12572 dwo_sections->types.push_back (type_section);
12573 }
12574 }
12575
12576 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12577 by PER_CU. This is for the non-DWP case.
12578 The result is NULL if DWO_NAME can't be found. */
12579
12580 static struct dwo_file *
12581 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12582 const char *dwo_name, const char *comp_dir)
12583 {
12584 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12585
12586 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12587 if (dbfd == NULL)
12588 {
12589 if (dwarf_read_debug)
12590 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12591 return NULL;
12592 }
12593
12594 dwo_file_up dwo_file (new struct dwo_file);
12595 dwo_file->dwo_name = dwo_name;
12596 dwo_file->comp_dir = comp_dir;
12597 dwo_file->dbfd = std::move (dbfd);
12598
12599 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12600 &dwo_file->sections);
12601
12602 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12603 dwo_file->sections.info, dwo_file->cus);
12604
12605 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12606 dwo_file->sections.types, dwo_file->tus);
12607
12608 if (dwarf_read_debug)
12609 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12610
12611 return dwo_file.release ();
12612 }
12613
12614 /* This function is mapped across the sections and remembers the offset and
12615 size of each of the DWP debugging sections common to version 1 and 2 that
12616 we are interested in. */
12617
12618 static void
12619 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12620 void *dwp_file_ptr)
12621 {
12622 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12623 const struct dwop_section_names *names = &dwop_section_names;
12624 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12625
12626 /* Record the ELF section number for later lookup: this is what the
12627 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12628 gdb_assert (elf_section_nr < dwp_file->num_sections);
12629 dwp_file->elf_sections[elf_section_nr] = sectp;
12630
12631 /* Look for specific sections that we need. */
12632 if (section_is_p (sectp->name, &names->str_dwo))
12633 {
12634 dwp_file->sections.str.s.section = sectp;
12635 dwp_file->sections.str.size = bfd_section_size (sectp);
12636 }
12637 else if (section_is_p (sectp->name, &names->cu_index))
12638 {
12639 dwp_file->sections.cu_index.s.section = sectp;
12640 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12641 }
12642 else if (section_is_p (sectp->name, &names->tu_index))
12643 {
12644 dwp_file->sections.tu_index.s.section = sectp;
12645 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12646 }
12647 }
12648
12649 /* This function is mapped across the sections and remembers the offset and
12650 size of each of the DWP version 2 debugging sections that we are interested
12651 in. This is split into a separate function because we don't know if we
12652 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12653
12654 static void
12655 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12656 {
12657 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12658 const struct dwop_section_names *names = &dwop_section_names;
12659 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12660
12661 /* Record the ELF section number for later lookup: this is what the
12662 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12663 gdb_assert (elf_section_nr < dwp_file->num_sections);
12664 dwp_file->elf_sections[elf_section_nr] = sectp;
12665
12666 /* Look for specific sections that we need. */
12667 if (section_is_p (sectp->name, &names->abbrev_dwo))
12668 {
12669 dwp_file->sections.abbrev.s.section = sectp;
12670 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12671 }
12672 else if (section_is_p (sectp->name, &names->info_dwo))
12673 {
12674 dwp_file->sections.info.s.section = sectp;
12675 dwp_file->sections.info.size = bfd_section_size (sectp);
12676 }
12677 else if (section_is_p (sectp->name, &names->line_dwo))
12678 {
12679 dwp_file->sections.line.s.section = sectp;
12680 dwp_file->sections.line.size = bfd_section_size (sectp);
12681 }
12682 else if (section_is_p (sectp->name, &names->loc_dwo))
12683 {
12684 dwp_file->sections.loc.s.section = sectp;
12685 dwp_file->sections.loc.size = bfd_section_size (sectp);
12686 }
12687 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12688 {
12689 dwp_file->sections.macinfo.s.section = sectp;
12690 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12691 }
12692 else if (section_is_p (sectp->name, &names->macro_dwo))
12693 {
12694 dwp_file->sections.macro.s.section = sectp;
12695 dwp_file->sections.macro.size = bfd_section_size (sectp);
12696 }
12697 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12698 {
12699 dwp_file->sections.str_offsets.s.section = sectp;
12700 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12701 }
12702 else if (section_is_p (sectp->name, &names->types_dwo))
12703 {
12704 dwp_file->sections.types.s.section = sectp;
12705 dwp_file->sections.types.size = bfd_section_size (sectp);
12706 }
12707 }
12708
12709 /* Hash function for dwp_file loaded CUs/TUs. */
12710
12711 static hashval_t
12712 hash_dwp_loaded_cutus (const void *item)
12713 {
12714 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12715
12716 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12717 return dwo_unit->signature;
12718 }
12719
12720 /* Equality function for dwp_file loaded CUs/TUs. */
12721
12722 static int
12723 eq_dwp_loaded_cutus (const void *a, const void *b)
12724 {
12725 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12726 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12727
12728 return dua->signature == dub->signature;
12729 }
12730
12731 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12732
12733 static htab_up
12734 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12735 {
12736 return htab_up (htab_create_alloc (3,
12737 hash_dwp_loaded_cutus,
12738 eq_dwp_loaded_cutus,
12739 NULL, xcalloc, xfree));
12740 }
12741
12742 /* Try to open DWP file FILE_NAME.
12743 The result is the bfd handle of the file.
12744 If there is a problem finding or opening the file, return NULL.
12745 Upon success, the canonicalized path of the file is stored in the bfd,
12746 same as symfile_bfd_open. */
12747
12748 static gdb_bfd_ref_ptr
12749 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12750 const char *file_name)
12751 {
12752 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12753 1 /*is_dwp*/,
12754 1 /*search_cwd*/));
12755 if (abfd != NULL)
12756 return abfd;
12757
12758 /* Work around upstream bug 15652.
12759 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12760 [Whether that's a "bug" is debatable, but it is getting in our way.]
12761 We have no real idea where the dwp file is, because gdb's realpath-ing
12762 of the executable's path may have discarded the needed info.
12763 [IWBN if the dwp file name was recorded in the executable, akin to
12764 .gnu_debuglink, but that doesn't exist yet.]
12765 Strip the directory from FILE_NAME and search again. */
12766 if (*debug_file_directory != '\0')
12767 {
12768 /* Don't implicitly search the current directory here.
12769 If the user wants to search "." to handle this case,
12770 it must be added to debug-file-directory. */
12771 return try_open_dwop_file (dwarf2_per_objfile,
12772 lbasename (file_name), 1 /*is_dwp*/,
12773 0 /*search_cwd*/);
12774 }
12775
12776 return NULL;
12777 }
12778
12779 /* Initialize the use of the DWP file for the current objfile.
12780 By convention the name of the DWP file is ${objfile}.dwp.
12781 The result is NULL if it can't be found. */
12782
12783 static std::unique_ptr<struct dwp_file>
12784 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12785 {
12786 struct objfile *objfile = dwarf2_per_objfile->objfile;
12787
12788 /* Try to find first .dwp for the binary file before any symbolic links
12789 resolving. */
12790
12791 /* If the objfile is a debug file, find the name of the real binary
12792 file and get the name of dwp file from there. */
12793 std::string dwp_name;
12794 if (objfile->separate_debug_objfile_backlink != NULL)
12795 {
12796 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12797 const char *backlink_basename = lbasename (backlink->original_name);
12798
12799 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12800 }
12801 else
12802 dwp_name = objfile->original_name;
12803
12804 dwp_name += ".dwp";
12805
12806 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12807 if (dbfd == NULL
12808 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12809 {
12810 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12811 dwp_name = objfile_name (objfile);
12812 dwp_name += ".dwp";
12813 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12814 }
12815
12816 if (dbfd == NULL)
12817 {
12818 if (dwarf_read_debug)
12819 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12820 return std::unique_ptr<dwp_file> ();
12821 }
12822
12823 const char *name = bfd_get_filename (dbfd.get ());
12824 std::unique_ptr<struct dwp_file> dwp_file
12825 (new struct dwp_file (name, std::move (dbfd)));
12826
12827 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12828 dwp_file->elf_sections =
12829 OBSTACK_CALLOC (&objfile->objfile_obstack,
12830 dwp_file->num_sections, asection *);
12831
12832 bfd_map_over_sections (dwp_file->dbfd.get (),
12833 dwarf2_locate_common_dwp_sections,
12834 dwp_file.get ());
12835
12836 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12837 0);
12838
12839 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12840 1);
12841
12842 /* The DWP file version is stored in the hash table. Oh well. */
12843 if (dwp_file->cus && dwp_file->tus
12844 && dwp_file->cus->version != dwp_file->tus->version)
12845 {
12846 /* Technically speaking, we should try to limp along, but this is
12847 pretty bizarre. We use pulongest here because that's the established
12848 portability solution (e.g, we cannot use %u for uint32_t). */
12849 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12850 " TU version %s [in DWP file %s]"),
12851 pulongest (dwp_file->cus->version),
12852 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12853 }
12854
12855 if (dwp_file->cus)
12856 dwp_file->version = dwp_file->cus->version;
12857 else if (dwp_file->tus)
12858 dwp_file->version = dwp_file->tus->version;
12859 else
12860 dwp_file->version = 2;
12861
12862 if (dwp_file->version == 2)
12863 bfd_map_over_sections (dwp_file->dbfd.get (),
12864 dwarf2_locate_v2_dwp_sections,
12865 dwp_file.get ());
12866
12867 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12868 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12869
12870 if (dwarf_read_debug)
12871 {
12872 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12873 fprintf_unfiltered (gdb_stdlog,
12874 " %s CUs, %s TUs\n",
12875 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12876 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12877 }
12878
12879 return dwp_file;
12880 }
12881
12882 /* Wrapper around open_and_init_dwp_file, only open it once. */
12883
12884 static struct dwp_file *
12885 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12886 {
12887 if (! dwarf2_per_objfile->dwp_checked)
12888 {
12889 dwarf2_per_objfile->dwp_file
12890 = open_and_init_dwp_file (dwarf2_per_objfile);
12891 dwarf2_per_objfile->dwp_checked = 1;
12892 }
12893 return dwarf2_per_objfile->dwp_file.get ();
12894 }
12895
12896 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12897 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12898 or in the DWP file for the objfile, referenced by THIS_UNIT.
12899 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12900 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12901
12902 This is called, for example, when wanting to read a variable with a
12903 complex location. Therefore we don't want to do file i/o for every call.
12904 Therefore we don't want to look for a DWO file on every call.
12905 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12906 then we check if we've already seen DWO_NAME, and only THEN do we check
12907 for a DWO file.
12908
12909 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12910 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12911
12912 static struct dwo_unit *
12913 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12914 const char *dwo_name, const char *comp_dir,
12915 ULONGEST signature, int is_debug_types)
12916 {
12917 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12918 struct objfile *objfile = dwarf2_per_objfile->objfile;
12919 const char *kind = is_debug_types ? "TU" : "CU";
12920 void **dwo_file_slot;
12921 struct dwo_file *dwo_file;
12922 struct dwp_file *dwp_file;
12923
12924 /* First see if there's a DWP file.
12925 If we have a DWP file but didn't find the DWO inside it, don't
12926 look for the original DWO file. It makes gdb behave differently
12927 depending on whether one is debugging in the build tree. */
12928
12929 dwp_file = get_dwp_file (dwarf2_per_objfile);
12930 if (dwp_file != NULL)
12931 {
12932 const struct dwp_hash_table *dwp_htab =
12933 is_debug_types ? dwp_file->tus : dwp_file->cus;
12934
12935 if (dwp_htab != NULL)
12936 {
12937 struct dwo_unit *dwo_cutu =
12938 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12939 signature, is_debug_types);
12940
12941 if (dwo_cutu != NULL)
12942 {
12943 if (dwarf_read_debug)
12944 {
12945 fprintf_unfiltered (gdb_stdlog,
12946 "Virtual DWO %s %s found: @%s\n",
12947 kind, hex_string (signature),
12948 host_address_to_string (dwo_cutu));
12949 }
12950 return dwo_cutu;
12951 }
12952 }
12953 }
12954 else
12955 {
12956 /* No DWP file, look for the DWO file. */
12957
12958 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12959 dwo_name, comp_dir);
12960 if (*dwo_file_slot == NULL)
12961 {
12962 /* Read in the file and build a table of the CUs/TUs it contains. */
12963 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12964 }
12965 /* NOTE: This will be NULL if unable to open the file. */
12966 dwo_file = (struct dwo_file *) *dwo_file_slot;
12967
12968 if (dwo_file != NULL)
12969 {
12970 struct dwo_unit *dwo_cutu = NULL;
12971
12972 if (is_debug_types && dwo_file->tus)
12973 {
12974 struct dwo_unit find_dwo_cutu;
12975
12976 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12977 find_dwo_cutu.signature = signature;
12978 dwo_cutu
12979 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12980 &find_dwo_cutu);
12981 }
12982 else if (!is_debug_types && dwo_file->cus)
12983 {
12984 struct dwo_unit find_dwo_cutu;
12985
12986 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12987 find_dwo_cutu.signature = signature;
12988 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12989 &find_dwo_cutu);
12990 }
12991
12992 if (dwo_cutu != NULL)
12993 {
12994 if (dwarf_read_debug)
12995 {
12996 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12997 kind, dwo_name, hex_string (signature),
12998 host_address_to_string (dwo_cutu));
12999 }
13000 return dwo_cutu;
13001 }
13002 }
13003 }
13004
13005 /* We didn't find it. This could mean a dwo_id mismatch, or
13006 someone deleted the DWO/DWP file, or the search path isn't set up
13007 correctly to find the file. */
13008
13009 if (dwarf_read_debug)
13010 {
13011 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13012 kind, dwo_name, hex_string (signature));
13013 }
13014
13015 /* This is a warning and not a complaint because it can be caused by
13016 pilot error (e.g., user accidentally deleting the DWO). */
13017 {
13018 /* Print the name of the DWP file if we looked there, helps the user
13019 better diagnose the problem. */
13020 std::string dwp_text;
13021
13022 if (dwp_file != NULL)
13023 dwp_text = string_printf (" [in DWP file %s]",
13024 lbasename (dwp_file->name));
13025
13026 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13027 " [in module %s]"),
13028 kind, dwo_name, hex_string (signature),
13029 dwp_text.c_str (),
13030 this_unit->is_debug_types ? "TU" : "CU",
13031 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13032 }
13033 return NULL;
13034 }
13035
13036 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13037 See lookup_dwo_cutu_unit for details. */
13038
13039 static struct dwo_unit *
13040 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13041 const char *dwo_name, const char *comp_dir,
13042 ULONGEST signature)
13043 {
13044 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13045 }
13046
13047 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13048 See lookup_dwo_cutu_unit for details. */
13049
13050 static struct dwo_unit *
13051 lookup_dwo_type_unit (struct signatured_type *this_tu,
13052 const char *dwo_name, const char *comp_dir)
13053 {
13054 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13055 }
13056
13057 /* Traversal function for queue_and_load_all_dwo_tus. */
13058
13059 static int
13060 queue_and_load_dwo_tu (void **slot, void *info)
13061 {
13062 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13063 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13064 ULONGEST signature = dwo_unit->signature;
13065 struct signatured_type *sig_type =
13066 lookup_dwo_signatured_type (per_cu->cu, signature);
13067
13068 if (sig_type != NULL)
13069 {
13070 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13071
13072 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13073 a real dependency of PER_CU on SIG_TYPE. That is detected later
13074 while processing PER_CU. */
13075 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13076 load_full_type_unit (sig_cu);
13077 per_cu->imported_symtabs_push (sig_cu);
13078 }
13079
13080 return 1;
13081 }
13082
13083 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13084 The DWO may have the only definition of the type, though it may not be
13085 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13086 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13087
13088 static void
13089 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13090 {
13091 struct dwo_unit *dwo_unit;
13092 struct dwo_file *dwo_file;
13093
13094 gdb_assert (!per_cu->is_debug_types);
13095 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13096 gdb_assert (per_cu->cu != NULL);
13097
13098 dwo_unit = per_cu->cu->dwo_unit;
13099 gdb_assert (dwo_unit != NULL);
13100
13101 dwo_file = dwo_unit->dwo_file;
13102 if (dwo_file->tus != NULL)
13103 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
13104 per_cu);
13105 }
13106
13107 /* Read in various DIEs. */
13108
13109 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13110 Inherit only the children of the DW_AT_abstract_origin DIE not being
13111 already referenced by DW_AT_abstract_origin from the children of the
13112 current DIE. */
13113
13114 static void
13115 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13116 {
13117 struct die_info *child_die;
13118 sect_offset *offsetp;
13119 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13120 struct die_info *origin_die;
13121 /* Iterator of the ORIGIN_DIE children. */
13122 struct die_info *origin_child_die;
13123 struct attribute *attr;
13124 struct dwarf2_cu *origin_cu;
13125 struct pending **origin_previous_list_in_scope;
13126
13127 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13128 if (!attr)
13129 return;
13130
13131 /* Note that following die references may follow to a die in a
13132 different cu. */
13133
13134 origin_cu = cu;
13135 origin_die = follow_die_ref (die, attr, &origin_cu);
13136
13137 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13138 symbols in. */
13139 origin_previous_list_in_scope = origin_cu->list_in_scope;
13140 origin_cu->list_in_scope = cu->list_in_scope;
13141
13142 if (die->tag != origin_die->tag
13143 && !(die->tag == DW_TAG_inlined_subroutine
13144 && origin_die->tag == DW_TAG_subprogram))
13145 complaint (_("DIE %s and its abstract origin %s have different tags"),
13146 sect_offset_str (die->sect_off),
13147 sect_offset_str (origin_die->sect_off));
13148
13149 std::vector<sect_offset> offsets;
13150
13151 for (child_die = die->child;
13152 child_die && child_die->tag;
13153 child_die = sibling_die (child_die))
13154 {
13155 struct die_info *child_origin_die;
13156 struct dwarf2_cu *child_origin_cu;
13157
13158 /* We are trying to process concrete instance entries:
13159 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13160 it's not relevant to our analysis here. i.e. detecting DIEs that are
13161 present in the abstract instance but not referenced in the concrete
13162 one. */
13163 if (child_die->tag == DW_TAG_call_site
13164 || child_die->tag == DW_TAG_GNU_call_site)
13165 continue;
13166
13167 /* For each CHILD_DIE, find the corresponding child of
13168 ORIGIN_DIE. If there is more than one layer of
13169 DW_AT_abstract_origin, follow them all; there shouldn't be,
13170 but GCC versions at least through 4.4 generate this (GCC PR
13171 40573). */
13172 child_origin_die = child_die;
13173 child_origin_cu = cu;
13174 while (1)
13175 {
13176 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13177 child_origin_cu);
13178 if (attr == NULL)
13179 break;
13180 child_origin_die = follow_die_ref (child_origin_die, attr,
13181 &child_origin_cu);
13182 }
13183
13184 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13185 counterpart may exist. */
13186 if (child_origin_die != child_die)
13187 {
13188 if (child_die->tag != child_origin_die->tag
13189 && !(child_die->tag == DW_TAG_inlined_subroutine
13190 && child_origin_die->tag == DW_TAG_subprogram))
13191 complaint (_("Child DIE %s and its abstract origin %s have "
13192 "different tags"),
13193 sect_offset_str (child_die->sect_off),
13194 sect_offset_str (child_origin_die->sect_off));
13195 if (child_origin_die->parent != origin_die)
13196 complaint (_("Child DIE %s and its abstract origin %s have "
13197 "different parents"),
13198 sect_offset_str (child_die->sect_off),
13199 sect_offset_str (child_origin_die->sect_off));
13200 else
13201 offsets.push_back (child_origin_die->sect_off);
13202 }
13203 }
13204 std::sort (offsets.begin (), offsets.end ());
13205 sect_offset *offsets_end = offsets.data () + offsets.size ();
13206 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13207 if (offsetp[-1] == *offsetp)
13208 complaint (_("Multiple children of DIE %s refer "
13209 "to DIE %s as their abstract origin"),
13210 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13211
13212 offsetp = offsets.data ();
13213 origin_child_die = origin_die->child;
13214 while (origin_child_die && origin_child_die->tag)
13215 {
13216 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13217 while (offsetp < offsets_end
13218 && *offsetp < origin_child_die->sect_off)
13219 offsetp++;
13220 if (offsetp >= offsets_end
13221 || *offsetp > origin_child_die->sect_off)
13222 {
13223 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13224 Check whether we're already processing ORIGIN_CHILD_DIE.
13225 This can happen with mutually referenced abstract_origins.
13226 PR 16581. */
13227 if (!origin_child_die->in_process)
13228 process_die (origin_child_die, origin_cu);
13229 }
13230 origin_child_die = sibling_die (origin_child_die);
13231 }
13232 origin_cu->list_in_scope = origin_previous_list_in_scope;
13233
13234 if (cu != origin_cu)
13235 compute_delayed_physnames (origin_cu);
13236 }
13237
13238 static void
13239 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13240 {
13241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13243 struct context_stack *newobj;
13244 CORE_ADDR lowpc;
13245 CORE_ADDR highpc;
13246 struct die_info *child_die;
13247 struct attribute *attr, *call_line, *call_file;
13248 const char *name;
13249 CORE_ADDR baseaddr;
13250 struct block *block;
13251 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13252 std::vector<struct symbol *> template_args;
13253 struct template_symbol *templ_func = NULL;
13254
13255 if (inlined_func)
13256 {
13257 /* If we do not have call site information, we can't show the
13258 caller of this inlined function. That's too confusing, so
13259 only use the scope for local variables. */
13260 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13261 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13262 if (call_line == NULL || call_file == NULL)
13263 {
13264 read_lexical_block_scope (die, cu);
13265 return;
13266 }
13267 }
13268
13269 baseaddr = objfile->text_section_offset ();
13270
13271 name = dwarf2_name (die, cu);
13272
13273 /* Ignore functions with missing or empty names. These are actually
13274 illegal according to the DWARF standard. */
13275 if (name == NULL)
13276 {
13277 complaint (_("missing name for subprogram DIE at %s"),
13278 sect_offset_str (die->sect_off));
13279 return;
13280 }
13281
13282 /* Ignore functions with missing or invalid low and high pc attributes. */
13283 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13284 <= PC_BOUNDS_INVALID)
13285 {
13286 attr = dwarf2_attr (die, DW_AT_external, cu);
13287 if (!attr || !DW_UNSND (attr))
13288 complaint (_("cannot get low and high bounds "
13289 "for subprogram DIE at %s"),
13290 sect_offset_str (die->sect_off));
13291 return;
13292 }
13293
13294 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13295 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13296
13297 /* If we have any template arguments, then we must allocate a
13298 different sort of symbol. */
13299 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13300 {
13301 if (child_die->tag == DW_TAG_template_type_param
13302 || child_die->tag == DW_TAG_template_value_param)
13303 {
13304 templ_func = allocate_template_symbol (objfile);
13305 templ_func->subclass = SYMBOL_TEMPLATE;
13306 break;
13307 }
13308 }
13309
13310 newobj = cu->get_builder ()->push_context (0, lowpc);
13311 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13312 (struct symbol *) templ_func);
13313
13314 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13315 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13316 cu->language);
13317
13318 /* If there is a location expression for DW_AT_frame_base, record
13319 it. */
13320 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13321 if (attr != nullptr)
13322 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13323
13324 /* If there is a location for the static link, record it. */
13325 newobj->static_link = NULL;
13326 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13327 if (attr != nullptr)
13328 {
13329 newobj->static_link
13330 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13331 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13332 dwarf2_per_cu_addr_type (cu->per_cu));
13333 }
13334
13335 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13336
13337 if (die->child != NULL)
13338 {
13339 child_die = die->child;
13340 while (child_die && child_die->tag)
13341 {
13342 if (child_die->tag == DW_TAG_template_type_param
13343 || child_die->tag == DW_TAG_template_value_param)
13344 {
13345 struct symbol *arg = new_symbol (child_die, NULL, cu);
13346
13347 if (arg != NULL)
13348 template_args.push_back (arg);
13349 }
13350 else
13351 process_die (child_die, cu);
13352 child_die = sibling_die (child_die);
13353 }
13354 }
13355
13356 inherit_abstract_dies (die, cu);
13357
13358 /* If we have a DW_AT_specification, we might need to import using
13359 directives from the context of the specification DIE. See the
13360 comment in determine_prefix. */
13361 if (cu->language == language_cplus
13362 && dwarf2_attr (die, DW_AT_specification, cu))
13363 {
13364 struct dwarf2_cu *spec_cu = cu;
13365 struct die_info *spec_die = die_specification (die, &spec_cu);
13366
13367 while (spec_die)
13368 {
13369 child_die = spec_die->child;
13370 while (child_die && child_die->tag)
13371 {
13372 if (child_die->tag == DW_TAG_imported_module)
13373 process_die (child_die, spec_cu);
13374 child_die = sibling_die (child_die);
13375 }
13376
13377 /* In some cases, GCC generates specification DIEs that
13378 themselves contain DW_AT_specification attributes. */
13379 spec_die = die_specification (spec_die, &spec_cu);
13380 }
13381 }
13382
13383 struct context_stack cstk = cu->get_builder ()->pop_context ();
13384 /* Make a block for the local symbols within. */
13385 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13386 cstk.static_link, lowpc, highpc);
13387
13388 /* For C++, set the block's scope. */
13389 if ((cu->language == language_cplus
13390 || cu->language == language_fortran
13391 || cu->language == language_d
13392 || cu->language == language_rust)
13393 && cu->processing_has_namespace_info)
13394 block_set_scope (block, determine_prefix (die, cu),
13395 &objfile->objfile_obstack);
13396
13397 /* If we have address ranges, record them. */
13398 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13399
13400 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13401
13402 /* Attach template arguments to function. */
13403 if (!template_args.empty ())
13404 {
13405 gdb_assert (templ_func != NULL);
13406
13407 templ_func->n_template_arguments = template_args.size ();
13408 templ_func->template_arguments
13409 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13410 templ_func->n_template_arguments);
13411 memcpy (templ_func->template_arguments,
13412 template_args.data (),
13413 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13414
13415 /* Make sure that the symtab is set on the new symbols. Even
13416 though they don't appear in this symtab directly, other parts
13417 of gdb assume that symbols do, and this is reasonably
13418 true. */
13419 for (symbol *sym : template_args)
13420 symbol_set_symtab (sym, symbol_symtab (templ_func));
13421 }
13422
13423 /* In C++, we can have functions nested inside functions (e.g., when
13424 a function declares a class that has methods). This means that
13425 when we finish processing a function scope, we may need to go
13426 back to building a containing block's symbol lists. */
13427 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13428 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13429
13430 /* If we've finished processing a top-level function, subsequent
13431 symbols go in the file symbol list. */
13432 if (cu->get_builder ()->outermost_context_p ())
13433 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13434 }
13435
13436 /* Process all the DIES contained within a lexical block scope. Start
13437 a new scope, process the dies, and then close the scope. */
13438
13439 static void
13440 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13441 {
13442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13443 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13444 CORE_ADDR lowpc, highpc;
13445 struct die_info *child_die;
13446 CORE_ADDR baseaddr;
13447
13448 baseaddr = objfile->text_section_offset ();
13449
13450 /* Ignore blocks with missing or invalid low and high pc attributes. */
13451 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13452 as multiple lexical blocks? Handling children in a sane way would
13453 be nasty. Might be easier to properly extend generic blocks to
13454 describe ranges. */
13455 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13456 {
13457 case PC_BOUNDS_NOT_PRESENT:
13458 /* DW_TAG_lexical_block has no attributes, process its children as if
13459 there was no wrapping by that DW_TAG_lexical_block.
13460 GCC does no longer produces such DWARF since GCC r224161. */
13461 for (child_die = die->child;
13462 child_die != NULL && child_die->tag;
13463 child_die = sibling_die (child_die))
13464 process_die (child_die, cu);
13465 return;
13466 case PC_BOUNDS_INVALID:
13467 return;
13468 }
13469 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13470 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13471
13472 cu->get_builder ()->push_context (0, lowpc);
13473 if (die->child != NULL)
13474 {
13475 child_die = die->child;
13476 while (child_die && child_die->tag)
13477 {
13478 process_die (child_die, cu);
13479 child_die = sibling_die (child_die);
13480 }
13481 }
13482 inherit_abstract_dies (die, cu);
13483 struct context_stack cstk = cu->get_builder ()->pop_context ();
13484
13485 if (*cu->get_builder ()->get_local_symbols () != NULL
13486 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13487 {
13488 struct block *block
13489 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13490 cstk.start_addr, highpc);
13491
13492 /* Note that recording ranges after traversing children, as we
13493 do here, means that recording a parent's ranges entails
13494 walking across all its children's ranges as they appear in
13495 the address map, which is quadratic behavior.
13496
13497 It would be nicer to record the parent's ranges before
13498 traversing its children, simply overriding whatever you find
13499 there. But since we don't even decide whether to create a
13500 block until after we've traversed its children, that's hard
13501 to do. */
13502 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13503 }
13504 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13505 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13506 }
13507
13508 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13509
13510 static void
13511 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13512 {
13513 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13515 CORE_ADDR pc, baseaddr;
13516 struct attribute *attr;
13517 struct call_site *call_site, call_site_local;
13518 void **slot;
13519 int nparams;
13520 struct die_info *child_die;
13521
13522 baseaddr = objfile->text_section_offset ();
13523
13524 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13525 if (attr == NULL)
13526 {
13527 /* This was a pre-DWARF-5 GNU extension alias
13528 for DW_AT_call_return_pc. */
13529 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13530 }
13531 if (!attr)
13532 {
13533 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13534 "DIE %s [in module %s]"),
13535 sect_offset_str (die->sect_off), objfile_name (objfile));
13536 return;
13537 }
13538 pc = attr->value_as_address () + baseaddr;
13539 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13540
13541 if (cu->call_site_htab == NULL)
13542 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13543 NULL, &objfile->objfile_obstack,
13544 hashtab_obstack_allocate, NULL);
13545 call_site_local.pc = pc;
13546 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13547 if (*slot != NULL)
13548 {
13549 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13550 "DIE %s [in module %s]"),
13551 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13552 objfile_name (objfile));
13553 return;
13554 }
13555
13556 /* Count parameters at the caller. */
13557
13558 nparams = 0;
13559 for (child_die = die->child; child_die && child_die->tag;
13560 child_die = sibling_die (child_die))
13561 {
13562 if (child_die->tag != DW_TAG_call_site_parameter
13563 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13564 {
13565 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13566 "DW_TAG_call_site child DIE %s [in module %s]"),
13567 child_die->tag, sect_offset_str (child_die->sect_off),
13568 objfile_name (objfile));
13569 continue;
13570 }
13571
13572 nparams++;
13573 }
13574
13575 call_site
13576 = ((struct call_site *)
13577 obstack_alloc (&objfile->objfile_obstack,
13578 sizeof (*call_site)
13579 + (sizeof (*call_site->parameter) * (nparams - 1))));
13580 *slot = call_site;
13581 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13582 call_site->pc = pc;
13583
13584 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13585 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13586 {
13587 struct die_info *func_die;
13588
13589 /* Skip also over DW_TAG_inlined_subroutine. */
13590 for (func_die = die->parent;
13591 func_die && func_die->tag != DW_TAG_subprogram
13592 && func_die->tag != DW_TAG_subroutine_type;
13593 func_die = func_die->parent);
13594
13595 /* DW_AT_call_all_calls is a superset
13596 of DW_AT_call_all_tail_calls. */
13597 if (func_die
13598 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13599 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13600 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13601 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13602 {
13603 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13604 not complete. But keep CALL_SITE for look ups via call_site_htab,
13605 both the initial caller containing the real return address PC and
13606 the final callee containing the current PC of a chain of tail
13607 calls do not need to have the tail call list complete. But any
13608 function candidate for a virtual tail call frame searched via
13609 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13610 determined unambiguously. */
13611 }
13612 else
13613 {
13614 struct type *func_type = NULL;
13615
13616 if (func_die)
13617 func_type = get_die_type (func_die, cu);
13618 if (func_type != NULL)
13619 {
13620 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13621
13622 /* Enlist this call site to the function. */
13623 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13624 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13625 }
13626 else
13627 complaint (_("Cannot find function owning DW_TAG_call_site "
13628 "DIE %s [in module %s]"),
13629 sect_offset_str (die->sect_off), objfile_name (objfile));
13630 }
13631 }
13632
13633 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13634 if (attr == NULL)
13635 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13636 if (attr == NULL)
13637 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13638 if (attr == NULL)
13639 {
13640 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13641 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13642 }
13643 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13644 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13645 /* Keep NULL DWARF_BLOCK. */;
13646 else if (attr->form_is_block ())
13647 {
13648 struct dwarf2_locexpr_baton *dlbaton;
13649
13650 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13651 dlbaton->data = DW_BLOCK (attr)->data;
13652 dlbaton->size = DW_BLOCK (attr)->size;
13653 dlbaton->per_cu = cu->per_cu;
13654
13655 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13656 }
13657 else if (attr->form_is_ref ())
13658 {
13659 struct dwarf2_cu *target_cu = cu;
13660 struct die_info *target_die;
13661
13662 target_die = follow_die_ref (die, attr, &target_cu);
13663 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13664 if (die_is_declaration (target_die, target_cu))
13665 {
13666 const char *target_physname;
13667
13668 /* Prefer the mangled name; otherwise compute the demangled one. */
13669 target_physname = dw2_linkage_name (target_die, target_cu);
13670 if (target_physname == NULL)
13671 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13672 if (target_physname == NULL)
13673 complaint (_("DW_AT_call_target target DIE has invalid "
13674 "physname, for referencing DIE %s [in module %s]"),
13675 sect_offset_str (die->sect_off), objfile_name (objfile));
13676 else
13677 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13678 }
13679 else
13680 {
13681 CORE_ADDR lowpc;
13682
13683 /* DW_AT_entry_pc should be preferred. */
13684 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13685 <= PC_BOUNDS_INVALID)
13686 complaint (_("DW_AT_call_target target DIE has invalid "
13687 "low pc, for referencing DIE %s [in module %s]"),
13688 sect_offset_str (die->sect_off), objfile_name (objfile));
13689 else
13690 {
13691 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13692 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13693 }
13694 }
13695 }
13696 else
13697 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13698 "block nor reference, for DIE %s [in module %s]"),
13699 sect_offset_str (die->sect_off), objfile_name (objfile));
13700
13701 call_site->per_cu = cu->per_cu;
13702
13703 for (child_die = die->child;
13704 child_die && child_die->tag;
13705 child_die = sibling_die (child_die))
13706 {
13707 struct call_site_parameter *parameter;
13708 struct attribute *loc, *origin;
13709
13710 if (child_die->tag != DW_TAG_call_site_parameter
13711 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13712 {
13713 /* Already printed the complaint above. */
13714 continue;
13715 }
13716
13717 gdb_assert (call_site->parameter_count < nparams);
13718 parameter = &call_site->parameter[call_site->parameter_count];
13719
13720 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13721 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13722 register is contained in DW_AT_call_value. */
13723
13724 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13725 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13726 if (origin == NULL)
13727 {
13728 /* This was a pre-DWARF-5 GNU extension alias
13729 for DW_AT_call_parameter. */
13730 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13731 }
13732 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13733 {
13734 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13735
13736 sect_offset sect_off
13737 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13738 if (!offset_in_cu_p (&cu->header, sect_off))
13739 {
13740 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13741 binding can be done only inside one CU. Such referenced DIE
13742 therefore cannot be even moved to DW_TAG_partial_unit. */
13743 complaint (_("DW_AT_call_parameter offset is not in CU for "
13744 "DW_TAG_call_site child DIE %s [in module %s]"),
13745 sect_offset_str (child_die->sect_off),
13746 objfile_name (objfile));
13747 continue;
13748 }
13749 parameter->u.param_cu_off
13750 = (cu_offset) (sect_off - cu->header.sect_off);
13751 }
13752 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13753 {
13754 complaint (_("No DW_FORM_block* DW_AT_location for "
13755 "DW_TAG_call_site child DIE %s [in module %s]"),
13756 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13757 continue;
13758 }
13759 else
13760 {
13761 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13762 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13763 if (parameter->u.dwarf_reg != -1)
13764 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13765 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13766 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13767 &parameter->u.fb_offset))
13768 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13769 else
13770 {
13771 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13772 "for DW_FORM_block* DW_AT_location is supported for "
13773 "DW_TAG_call_site child DIE %s "
13774 "[in module %s]"),
13775 sect_offset_str (child_die->sect_off),
13776 objfile_name (objfile));
13777 continue;
13778 }
13779 }
13780
13781 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13782 if (attr == NULL)
13783 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13784 if (attr == NULL || !attr->form_is_block ())
13785 {
13786 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13787 "DW_TAG_call_site child DIE %s [in module %s]"),
13788 sect_offset_str (child_die->sect_off),
13789 objfile_name (objfile));
13790 continue;
13791 }
13792 parameter->value = DW_BLOCK (attr)->data;
13793 parameter->value_size = DW_BLOCK (attr)->size;
13794
13795 /* Parameters are not pre-cleared by memset above. */
13796 parameter->data_value = NULL;
13797 parameter->data_value_size = 0;
13798 call_site->parameter_count++;
13799
13800 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13801 if (attr == NULL)
13802 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13803 if (attr != nullptr)
13804 {
13805 if (!attr->form_is_block ())
13806 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13807 "DW_TAG_call_site child DIE %s [in module %s]"),
13808 sect_offset_str (child_die->sect_off),
13809 objfile_name (objfile));
13810 else
13811 {
13812 parameter->data_value = DW_BLOCK (attr)->data;
13813 parameter->data_value_size = DW_BLOCK (attr)->size;
13814 }
13815 }
13816 }
13817 }
13818
13819 /* Helper function for read_variable. If DIE represents a virtual
13820 table, then return the type of the concrete object that is
13821 associated with the virtual table. Otherwise, return NULL. */
13822
13823 static struct type *
13824 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13825 {
13826 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13827 if (attr == NULL)
13828 return NULL;
13829
13830 /* Find the type DIE. */
13831 struct die_info *type_die = NULL;
13832 struct dwarf2_cu *type_cu = cu;
13833
13834 if (attr->form_is_ref ())
13835 type_die = follow_die_ref (die, attr, &type_cu);
13836 if (type_die == NULL)
13837 return NULL;
13838
13839 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13840 return NULL;
13841 return die_containing_type (type_die, type_cu);
13842 }
13843
13844 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13845
13846 static void
13847 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13848 {
13849 struct rust_vtable_symbol *storage = NULL;
13850
13851 if (cu->language == language_rust)
13852 {
13853 struct type *containing_type = rust_containing_type (die, cu);
13854
13855 if (containing_type != NULL)
13856 {
13857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13858
13859 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13860 initialize_objfile_symbol (storage);
13861 storage->concrete_type = containing_type;
13862 storage->subclass = SYMBOL_RUST_VTABLE;
13863 }
13864 }
13865
13866 struct symbol *res = new_symbol (die, NULL, cu, storage);
13867 struct attribute *abstract_origin
13868 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13869 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13870 if (res == NULL && loc && abstract_origin)
13871 {
13872 /* We have a variable without a name, but with a location and an abstract
13873 origin. This may be a concrete instance of an abstract variable
13874 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13875 later. */
13876 struct dwarf2_cu *origin_cu = cu;
13877 struct die_info *origin_die
13878 = follow_die_ref (die, abstract_origin, &origin_cu);
13879 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13880 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13881 }
13882 }
13883
13884 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13885 reading .debug_rnglists.
13886 Callback's type should be:
13887 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13888 Return true if the attributes are present and valid, otherwise,
13889 return false. */
13890
13891 template <typename Callback>
13892 static bool
13893 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13894 Callback &&callback)
13895 {
13896 struct dwarf2_per_objfile *dwarf2_per_objfile
13897 = cu->per_cu->dwarf2_per_objfile;
13898 struct objfile *objfile = dwarf2_per_objfile->objfile;
13899 bfd *obfd = objfile->obfd;
13900 /* Base address selection entry. */
13901 CORE_ADDR base;
13902 int found_base;
13903 const gdb_byte *buffer;
13904 CORE_ADDR baseaddr;
13905 bool overflow = false;
13906
13907 found_base = cu->base_known;
13908 base = cu->base_address;
13909
13910 dwarf2_per_objfile->rnglists.read (objfile);
13911 if (offset >= dwarf2_per_objfile->rnglists.size)
13912 {
13913 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13914 offset);
13915 return false;
13916 }
13917 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13918
13919 baseaddr = objfile->text_section_offset ();
13920
13921 while (1)
13922 {
13923 /* Initialize it due to a false compiler warning. */
13924 CORE_ADDR range_beginning = 0, range_end = 0;
13925 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13926 + dwarf2_per_objfile->rnglists.size);
13927 unsigned int bytes_read;
13928
13929 if (buffer == buf_end)
13930 {
13931 overflow = true;
13932 break;
13933 }
13934 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13935 switch (rlet)
13936 {
13937 case DW_RLE_end_of_list:
13938 break;
13939 case DW_RLE_base_address:
13940 if (buffer + cu->header.addr_size > buf_end)
13941 {
13942 overflow = true;
13943 break;
13944 }
13945 base = read_address (obfd, buffer, cu, &bytes_read);
13946 found_base = 1;
13947 buffer += bytes_read;
13948 break;
13949 case DW_RLE_start_length:
13950 if (buffer + cu->header.addr_size > buf_end)
13951 {
13952 overflow = true;
13953 break;
13954 }
13955 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13956 buffer += bytes_read;
13957 range_end = (range_beginning
13958 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13959 buffer += bytes_read;
13960 if (buffer > buf_end)
13961 {
13962 overflow = true;
13963 break;
13964 }
13965 break;
13966 case DW_RLE_offset_pair:
13967 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13968 buffer += bytes_read;
13969 if (buffer > buf_end)
13970 {
13971 overflow = true;
13972 break;
13973 }
13974 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13975 buffer += bytes_read;
13976 if (buffer > buf_end)
13977 {
13978 overflow = true;
13979 break;
13980 }
13981 break;
13982 case DW_RLE_start_end:
13983 if (buffer + 2 * cu->header.addr_size > buf_end)
13984 {
13985 overflow = true;
13986 break;
13987 }
13988 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13989 buffer += bytes_read;
13990 range_end = read_address (obfd, buffer, cu, &bytes_read);
13991 buffer += bytes_read;
13992 break;
13993 default:
13994 complaint (_("Invalid .debug_rnglists data (no base address)"));
13995 return false;
13996 }
13997 if (rlet == DW_RLE_end_of_list || overflow)
13998 break;
13999 if (rlet == DW_RLE_base_address)
14000 continue;
14001
14002 if (!found_base)
14003 {
14004 /* We have no valid base address for the ranges
14005 data. */
14006 complaint (_("Invalid .debug_rnglists data (no base address)"));
14007 return false;
14008 }
14009
14010 if (range_beginning > range_end)
14011 {
14012 /* Inverted range entries are invalid. */
14013 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14014 return false;
14015 }
14016
14017 /* Empty range entries have no effect. */
14018 if (range_beginning == range_end)
14019 continue;
14020
14021 range_beginning += base;
14022 range_end += base;
14023
14024 /* A not-uncommon case of bad debug info.
14025 Don't pollute the addrmap with bad data. */
14026 if (range_beginning + baseaddr == 0
14027 && !dwarf2_per_objfile->has_section_at_zero)
14028 {
14029 complaint (_(".debug_rnglists entry has start address of zero"
14030 " [in module %s]"), objfile_name (objfile));
14031 continue;
14032 }
14033
14034 callback (range_beginning, range_end);
14035 }
14036
14037 if (overflow)
14038 {
14039 complaint (_("Offset %d is not terminated "
14040 "for DW_AT_ranges attribute"),
14041 offset);
14042 return false;
14043 }
14044
14045 return true;
14046 }
14047
14048 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14049 Callback's type should be:
14050 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14051 Return 1 if the attributes are present and valid, otherwise, return 0. */
14052
14053 template <typename Callback>
14054 static int
14055 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14056 Callback &&callback)
14057 {
14058 struct dwarf2_per_objfile *dwarf2_per_objfile
14059 = cu->per_cu->dwarf2_per_objfile;
14060 struct objfile *objfile = dwarf2_per_objfile->objfile;
14061 struct comp_unit_head *cu_header = &cu->header;
14062 bfd *obfd = objfile->obfd;
14063 unsigned int addr_size = cu_header->addr_size;
14064 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14065 /* Base address selection entry. */
14066 CORE_ADDR base;
14067 int found_base;
14068 unsigned int dummy;
14069 const gdb_byte *buffer;
14070 CORE_ADDR baseaddr;
14071
14072 if (cu_header->version >= 5)
14073 return dwarf2_rnglists_process (offset, cu, callback);
14074
14075 found_base = cu->base_known;
14076 base = cu->base_address;
14077
14078 dwarf2_per_objfile->ranges.read (objfile);
14079 if (offset >= dwarf2_per_objfile->ranges.size)
14080 {
14081 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14082 offset);
14083 return 0;
14084 }
14085 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14086
14087 baseaddr = objfile->text_section_offset ();
14088
14089 while (1)
14090 {
14091 CORE_ADDR range_beginning, range_end;
14092
14093 range_beginning = read_address (obfd, buffer, cu, &dummy);
14094 buffer += addr_size;
14095 range_end = read_address (obfd, buffer, cu, &dummy);
14096 buffer += addr_size;
14097 offset += 2 * addr_size;
14098
14099 /* An end of list marker is a pair of zero addresses. */
14100 if (range_beginning == 0 && range_end == 0)
14101 /* Found the end of list entry. */
14102 break;
14103
14104 /* Each base address selection entry is a pair of 2 values.
14105 The first is the largest possible address, the second is
14106 the base address. Check for a base address here. */
14107 if ((range_beginning & mask) == mask)
14108 {
14109 /* If we found the largest possible address, then we already
14110 have the base address in range_end. */
14111 base = range_end;
14112 found_base = 1;
14113 continue;
14114 }
14115
14116 if (!found_base)
14117 {
14118 /* We have no valid base address for the ranges
14119 data. */
14120 complaint (_("Invalid .debug_ranges data (no base address)"));
14121 return 0;
14122 }
14123
14124 if (range_beginning > range_end)
14125 {
14126 /* Inverted range entries are invalid. */
14127 complaint (_("Invalid .debug_ranges data (inverted range)"));
14128 return 0;
14129 }
14130
14131 /* Empty range entries have no effect. */
14132 if (range_beginning == range_end)
14133 continue;
14134
14135 range_beginning += base;
14136 range_end += base;
14137
14138 /* A not-uncommon case of bad debug info.
14139 Don't pollute the addrmap with bad data. */
14140 if (range_beginning + baseaddr == 0
14141 && !dwarf2_per_objfile->has_section_at_zero)
14142 {
14143 complaint (_(".debug_ranges entry has start address of zero"
14144 " [in module %s]"), objfile_name (objfile));
14145 continue;
14146 }
14147
14148 callback (range_beginning, range_end);
14149 }
14150
14151 return 1;
14152 }
14153
14154 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14155 Return 1 if the attributes are present and valid, otherwise, return 0.
14156 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14157
14158 static int
14159 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14160 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14161 dwarf2_psymtab *ranges_pst)
14162 {
14163 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14165 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14166 int low_set = 0;
14167 CORE_ADDR low = 0;
14168 CORE_ADDR high = 0;
14169 int retval;
14170
14171 retval = dwarf2_ranges_process (offset, cu,
14172 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14173 {
14174 if (ranges_pst != NULL)
14175 {
14176 CORE_ADDR lowpc;
14177 CORE_ADDR highpc;
14178
14179 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14180 range_beginning + baseaddr)
14181 - baseaddr);
14182 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14183 range_end + baseaddr)
14184 - baseaddr);
14185 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14186 lowpc, highpc - 1, ranges_pst);
14187 }
14188
14189 /* FIXME: This is recording everything as a low-high
14190 segment of consecutive addresses. We should have a
14191 data structure for discontiguous block ranges
14192 instead. */
14193 if (! low_set)
14194 {
14195 low = range_beginning;
14196 high = range_end;
14197 low_set = 1;
14198 }
14199 else
14200 {
14201 if (range_beginning < low)
14202 low = range_beginning;
14203 if (range_end > high)
14204 high = range_end;
14205 }
14206 });
14207 if (!retval)
14208 return 0;
14209
14210 if (! low_set)
14211 /* If the first entry is an end-of-list marker, the range
14212 describes an empty scope, i.e. no instructions. */
14213 return 0;
14214
14215 if (low_return)
14216 *low_return = low;
14217 if (high_return)
14218 *high_return = high;
14219 return 1;
14220 }
14221
14222 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14223 definition for the return value. *LOWPC and *HIGHPC are set iff
14224 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14225
14226 static enum pc_bounds_kind
14227 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14228 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14229 dwarf2_psymtab *pst)
14230 {
14231 struct dwarf2_per_objfile *dwarf2_per_objfile
14232 = cu->per_cu->dwarf2_per_objfile;
14233 struct attribute *attr;
14234 struct attribute *attr_high;
14235 CORE_ADDR low = 0;
14236 CORE_ADDR high = 0;
14237 enum pc_bounds_kind ret;
14238
14239 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14240 if (attr_high)
14241 {
14242 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14243 if (attr != nullptr)
14244 {
14245 low = attr->value_as_address ();
14246 high = attr_high->value_as_address ();
14247 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14248 high += low;
14249 }
14250 else
14251 /* Found high w/o low attribute. */
14252 return PC_BOUNDS_INVALID;
14253
14254 /* Found consecutive range of addresses. */
14255 ret = PC_BOUNDS_HIGH_LOW;
14256 }
14257 else
14258 {
14259 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14260 if (attr != NULL)
14261 {
14262 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14263 We take advantage of the fact that DW_AT_ranges does not appear
14264 in DW_TAG_compile_unit of DWO files. */
14265 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14266 unsigned int ranges_offset = (DW_UNSND (attr)
14267 + (need_ranges_base
14268 ? cu->ranges_base
14269 : 0));
14270
14271 /* Value of the DW_AT_ranges attribute is the offset in the
14272 .debug_ranges section. */
14273 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14274 return PC_BOUNDS_INVALID;
14275 /* Found discontinuous range of addresses. */
14276 ret = PC_BOUNDS_RANGES;
14277 }
14278 else
14279 return PC_BOUNDS_NOT_PRESENT;
14280 }
14281
14282 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14283 if (high <= low)
14284 return PC_BOUNDS_INVALID;
14285
14286 /* When using the GNU linker, .gnu.linkonce. sections are used to
14287 eliminate duplicate copies of functions and vtables and such.
14288 The linker will arbitrarily choose one and discard the others.
14289 The AT_*_pc values for such functions refer to local labels in
14290 these sections. If the section from that file was discarded, the
14291 labels are not in the output, so the relocs get a value of 0.
14292 If this is a discarded function, mark the pc bounds as invalid,
14293 so that GDB will ignore it. */
14294 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14295 return PC_BOUNDS_INVALID;
14296
14297 *lowpc = low;
14298 if (highpc)
14299 *highpc = high;
14300 return ret;
14301 }
14302
14303 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14304 its low and high PC addresses. Do nothing if these addresses could not
14305 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14306 and HIGHPC to the high address if greater than HIGHPC. */
14307
14308 static void
14309 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14310 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14311 struct dwarf2_cu *cu)
14312 {
14313 CORE_ADDR low, high;
14314 struct die_info *child = die->child;
14315
14316 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14317 {
14318 *lowpc = std::min (*lowpc, low);
14319 *highpc = std::max (*highpc, high);
14320 }
14321
14322 /* If the language does not allow nested subprograms (either inside
14323 subprograms or lexical blocks), we're done. */
14324 if (cu->language != language_ada)
14325 return;
14326
14327 /* Check all the children of the given DIE. If it contains nested
14328 subprograms, then check their pc bounds. Likewise, we need to
14329 check lexical blocks as well, as they may also contain subprogram
14330 definitions. */
14331 while (child && child->tag)
14332 {
14333 if (child->tag == DW_TAG_subprogram
14334 || child->tag == DW_TAG_lexical_block)
14335 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14336 child = sibling_die (child);
14337 }
14338 }
14339
14340 /* Get the low and high pc's represented by the scope DIE, and store
14341 them in *LOWPC and *HIGHPC. If the correct values can't be
14342 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14343
14344 static void
14345 get_scope_pc_bounds (struct die_info *die,
14346 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14347 struct dwarf2_cu *cu)
14348 {
14349 CORE_ADDR best_low = (CORE_ADDR) -1;
14350 CORE_ADDR best_high = (CORE_ADDR) 0;
14351 CORE_ADDR current_low, current_high;
14352
14353 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14354 >= PC_BOUNDS_RANGES)
14355 {
14356 best_low = current_low;
14357 best_high = current_high;
14358 }
14359 else
14360 {
14361 struct die_info *child = die->child;
14362
14363 while (child && child->tag)
14364 {
14365 switch (child->tag) {
14366 case DW_TAG_subprogram:
14367 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14368 break;
14369 case DW_TAG_namespace:
14370 case DW_TAG_module:
14371 /* FIXME: carlton/2004-01-16: Should we do this for
14372 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14373 that current GCC's always emit the DIEs corresponding
14374 to definitions of methods of classes as children of a
14375 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14376 the DIEs giving the declarations, which could be
14377 anywhere). But I don't see any reason why the
14378 standards says that they have to be there. */
14379 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14380
14381 if (current_low != ((CORE_ADDR) -1))
14382 {
14383 best_low = std::min (best_low, current_low);
14384 best_high = std::max (best_high, current_high);
14385 }
14386 break;
14387 default:
14388 /* Ignore. */
14389 break;
14390 }
14391
14392 child = sibling_die (child);
14393 }
14394 }
14395
14396 *lowpc = best_low;
14397 *highpc = best_high;
14398 }
14399
14400 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14401 in DIE. */
14402
14403 static void
14404 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14405 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14406 {
14407 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14408 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14409 struct attribute *attr;
14410 struct attribute *attr_high;
14411
14412 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14413 if (attr_high)
14414 {
14415 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14416 if (attr != nullptr)
14417 {
14418 CORE_ADDR low = attr->value_as_address ();
14419 CORE_ADDR high = attr_high->value_as_address ();
14420
14421 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14422 high += low;
14423
14424 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14425 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14426 cu->get_builder ()->record_block_range (block, low, high - 1);
14427 }
14428 }
14429
14430 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14431 if (attr != nullptr)
14432 {
14433 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14434 We take advantage of the fact that DW_AT_ranges does not appear
14435 in DW_TAG_compile_unit of DWO files. */
14436 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14437
14438 /* The value of the DW_AT_ranges attribute is the offset of the
14439 address range list in the .debug_ranges section. */
14440 unsigned long offset = (DW_UNSND (attr)
14441 + (need_ranges_base ? cu->ranges_base : 0));
14442
14443 std::vector<blockrange> blockvec;
14444 dwarf2_ranges_process (offset, cu,
14445 [&] (CORE_ADDR start, CORE_ADDR end)
14446 {
14447 start += baseaddr;
14448 end += baseaddr;
14449 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14450 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14451 cu->get_builder ()->record_block_range (block, start, end - 1);
14452 blockvec.emplace_back (start, end);
14453 });
14454
14455 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14456 }
14457 }
14458
14459 /* Check whether the producer field indicates either of GCC < 4.6, or the
14460 Intel C/C++ compiler, and cache the result in CU. */
14461
14462 static void
14463 check_producer (struct dwarf2_cu *cu)
14464 {
14465 int major, minor;
14466
14467 if (cu->producer == NULL)
14468 {
14469 /* For unknown compilers expect their behavior is DWARF version
14470 compliant.
14471
14472 GCC started to support .debug_types sections by -gdwarf-4 since
14473 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14474 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14475 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14476 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14477 }
14478 else if (producer_is_gcc (cu->producer, &major, &minor))
14479 {
14480 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14481 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14482 }
14483 else if (producer_is_icc (cu->producer, &major, &minor))
14484 {
14485 cu->producer_is_icc = true;
14486 cu->producer_is_icc_lt_14 = major < 14;
14487 }
14488 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14489 cu->producer_is_codewarrior = true;
14490 else
14491 {
14492 /* For other non-GCC compilers, expect their behavior is DWARF version
14493 compliant. */
14494 }
14495
14496 cu->checked_producer = true;
14497 }
14498
14499 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14500 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14501 during 4.6.0 experimental. */
14502
14503 static bool
14504 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14505 {
14506 if (!cu->checked_producer)
14507 check_producer (cu);
14508
14509 return cu->producer_is_gxx_lt_4_6;
14510 }
14511
14512
14513 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14514 with incorrect is_stmt attributes. */
14515
14516 static bool
14517 producer_is_codewarrior (struct dwarf2_cu *cu)
14518 {
14519 if (!cu->checked_producer)
14520 check_producer (cu);
14521
14522 return cu->producer_is_codewarrior;
14523 }
14524
14525 /* Return the default accessibility type if it is not overridden by
14526 DW_AT_accessibility. */
14527
14528 static enum dwarf_access_attribute
14529 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14530 {
14531 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14532 {
14533 /* The default DWARF 2 accessibility for members is public, the default
14534 accessibility for inheritance is private. */
14535
14536 if (die->tag != DW_TAG_inheritance)
14537 return DW_ACCESS_public;
14538 else
14539 return DW_ACCESS_private;
14540 }
14541 else
14542 {
14543 /* DWARF 3+ defines the default accessibility a different way. The same
14544 rules apply now for DW_TAG_inheritance as for the members and it only
14545 depends on the container kind. */
14546
14547 if (die->parent->tag == DW_TAG_class_type)
14548 return DW_ACCESS_private;
14549 else
14550 return DW_ACCESS_public;
14551 }
14552 }
14553
14554 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14555 offset. If the attribute was not found return 0, otherwise return
14556 1. If it was found but could not properly be handled, set *OFFSET
14557 to 0. */
14558
14559 static int
14560 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14561 LONGEST *offset)
14562 {
14563 struct attribute *attr;
14564
14565 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14566 if (attr != NULL)
14567 {
14568 *offset = 0;
14569
14570 /* Note that we do not check for a section offset first here.
14571 This is because DW_AT_data_member_location is new in DWARF 4,
14572 so if we see it, we can assume that a constant form is really
14573 a constant and not a section offset. */
14574 if (attr->form_is_constant ())
14575 *offset = dwarf2_get_attr_constant_value (attr, 0);
14576 else if (attr->form_is_section_offset ())
14577 dwarf2_complex_location_expr_complaint ();
14578 else if (attr->form_is_block ())
14579 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14580 else
14581 dwarf2_complex_location_expr_complaint ();
14582
14583 return 1;
14584 }
14585
14586 return 0;
14587 }
14588
14589 /* Add an aggregate field to the field list. */
14590
14591 static void
14592 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14593 struct dwarf2_cu *cu)
14594 {
14595 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14597 struct nextfield *new_field;
14598 struct attribute *attr;
14599 struct field *fp;
14600 const char *fieldname = "";
14601
14602 if (die->tag == DW_TAG_inheritance)
14603 {
14604 fip->baseclasses.emplace_back ();
14605 new_field = &fip->baseclasses.back ();
14606 }
14607 else
14608 {
14609 fip->fields.emplace_back ();
14610 new_field = &fip->fields.back ();
14611 }
14612
14613 fip->nfields++;
14614
14615 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14616 if (attr != nullptr)
14617 new_field->accessibility = DW_UNSND (attr);
14618 else
14619 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14620 if (new_field->accessibility != DW_ACCESS_public)
14621 fip->non_public_fields = 1;
14622
14623 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14624 if (attr != nullptr)
14625 new_field->virtuality = DW_UNSND (attr);
14626 else
14627 new_field->virtuality = DW_VIRTUALITY_none;
14628
14629 fp = &new_field->field;
14630
14631 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14632 {
14633 LONGEST offset;
14634
14635 /* Data member other than a C++ static data member. */
14636
14637 /* Get type of field. */
14638 fp->type = die_type (die, cu);
14639
14640 SET_FIELD_BITPOS (*fp, 0);
14641
14642 /* Get bit size of field (zero if none). */
14643 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14644 if (attr != nullptr)
14645 {
14646 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14647 }
14648 else
14649 {
14650 FIELD_BITSIZE (*fp) = 0;
14651 }
14652
14653 /* Get bit offset of field. */
14654 if (handle_data_member_location (die, cu, &offset))
14655 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14656 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14657 if (attr != nullptr)
14658 {
14659 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14660 {
14661 /* For big endian bits, the DW_AT_bit_offset gives the
14662 additional bit offset from the MSB of the containing
14663 anonymous object to the MSB of the field. We don't
14664 have to do anything special since we don't need to
14665 know the size of the anonymous object. */
14666 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14667 }
14668 else
14669 {
14670 /* For little endian bits, compute the bit offset to the
14671 MSB of the anonymous object, subtract off the number of
14672 bits from the MSB of the field to the MSB of the
14673 object, and then subtract off the number of bits of
14674 the field itself. The result is the bit offset of
14675 the LSB of the field. */
14676 int anonymous_size;
14677 int bit_offset = DW_UNSND (attr);
14678
14679 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14680 if (attr != nullptr)
14681 {
14682 /* The size of the anonymous object containing
14683 the bit field is explicit, so use the
14684 indicated size (in bytes). */
14685 anonymous_size = DW_UNSND (attr);
14686 }
14687 else
14688 {
14689 /* The size of the anonymous object containing
14690 the bit field must be inferred from the type
14691 attribute of the data member containing the
14692 bit field. */
14693 anonymous_size = TYPE_LENGTH (fp->type);
14694 }
14695 SET_FIELD_BITPOS (*fp,
14696 (FIELD_BITPOS (*fp)
14697 + anonymous_size * bits_per_byte
14698 - bit_offset - FIELD_BITSIZE (*fp)));
14699 }
14700 }
14701 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14702 if (attr != NULL)
14703 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14704 + dwarf2_get_attr_constant_value (attr, 0)));
14705
14706 /* Get name of field. */
14707 fieldname = dwarf2_name (die, cu);
14708 if (fieldname == NULL)
14709 fieldname = "";
14710
14711 /* The name is already allocated along with this objfile, so we don't
14712 need to duplicate it for the type. */
14713 fp->name = fieldname;
14714
14715 /* Change accessibility for artificial fields (e.g. virtual table
14716 pointer or virtual base class pointer) to private. */
14717 if (dwarf2_attr (die, DW_AT_artificial, cu))
14718 {
14719 FIELD_ARTIFICIAL (*fp) = 1;
14720 new_field->accessibility = DW_ACCESS_private;
14721 fip->non_public_fields = 1;
14722 }
14723 }
14724 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14725 {
14726 /* C++ static member. */
14727
14728 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14729 is a declaration, but all versions of G++ as of this writing
14730 (so through at least 3.2.1) incorrectly generate
14731 DW_TAG_variable tags. */
14732
14733 const char *physname;
14734
14735 /* Get name of field. */
14736 fieldname = dwarf2_name (die, cu);
14737 if (fieldname == NULL)
14738 return;
14739
14740 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14741 if (attr
14742 /* Only create a symbol if this is an external value.
14743 new_symbol checks this and puts the value in the global symbol
14744 table, which we want. If it is not external, new_symbol
14745 will try to put the value in cu->list_in_scope which is wrong. */
14746 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14747 {
14748 /* A static const member, not much different than an enum as far as
14749 we're concerned, except that we can support more types. */
14750 new_symbol (die, NULL, cu);
14751 }
14752
14753 /* Get physical name. */
14754 physname = dwarf2_physname (fieldname, die, cu);
14755
14756 /* The name is already allocated along with this objfile, so we don't
14757 need to duplicate it for the type. */
14758 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14759 FIELD_TYPE (*fp) = die_type (die, cu);
14760 FIELD_NAME (*fp) = fieldname;
14761 }
14762 else if (die->tag == DW_TAG_inheritance)
14763 {
14764 LONGEST offset;
14765
14766 /* C++ base class field. */
14767 if (handle_data_member_location (die, cu, &offset))
14768 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14769 FIELD_BITSIZE (*fp) = 0;
14770 FIELD_TYPE (*fp) = die_type (die, cu);
14771 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14772 }
14773 else if (die->tag == DW_TAG_variant_part)
14774 {
14775 /* process_structure_scope will treat this DIE as a union. */
14776 process_structure_scope (die, cu);
14777
14778 /* The variant part is relative to the start of the enclosing
14779 structure. */
14780 SET_FIELD_BITPOS (*fp, 0);
14781 fp->type = get_die_type (die, cu);
14782 fp->artificial = 1;
14783 fp->name = "<<variant>>";
14784
14785 /* Normally a DW_TAG_variant_part won't have a size, but our
14786 representation requires one, so set it to the maximum of the
14787 child sizes, being sure to account for the offset at which
14788 each child is seen. */
14789 if (TYPE_LENGTH (fp->type) == 0)
14790 {
14791 unsigned max = 0;
14792 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14793 {
14794 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14795 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14796 if (len > max)
14797 max = len;
14798 }
14799 TYPE_LENGTH (fp->type) = max;
14800 }
14801 }
14802 else
14803 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14804 }
14805
14806 /* Can the type given by DIE define another type? */
14807
14808 static bool
14809 type_can_define_types (const struct die_info *die)
14810 {
14811 switch (die->tag)
14812 {
14813 case DW_TAG_typedef:
14814 case DW_TAG_class_type:
14815 case DW_TAG_structure_type:
14816 case DW_TAG_union_type:
14817 case DW_TAG_enumeration_type:
14818 return true;
14819
14820 default:
14821 return false;
14822 }
14823 }
14824
14825 /* Add a type definition defined in the scope of the FIP's class. */
14826
14827 static void
14828 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14829 struct dwarf2_cu *cu)
14830 {
14831 struct decl_field fp;
14832 memset (&fp, 0, sizeof (fp));
14833
14834 gdb_assert (type_can_define_types (die));
14835
14836 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14837 fp.name = dwarf2_name (die, cu);
14838 fp.type = read_type_die (die, cu);
14839
14840 /* Save accessibility. */
14841 enum dwarf_access_attribute accessibility;
14842 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14843 if (attr != NULL)
14844 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14845 else
14846 accessibility = dwarf2_default_access_attribute (die, cu);
14847 switch (accessibility)
14848 {
14849 case DW_ACCESS_public:
14850 /* The assumed value if neither private nor protected. */
14851 break;
14852 case DW_ACCESS_private:
14853 fp.is_private = 1;
14854 break;
14855 case DW_ACCESS_protected:
14856 fp.is_protected = 1;
14857 break;
14858 default:
14859 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14860 }
14861
14862 if (die->tag == DW_TAG_typedef)
14863 fip->typedef_field_list.push_back (fp);
14864 else
14865 fip->nested_types_list.push_back (fp);
14866 }
14867
14868 /* Create the vector of fields, and attach it to the type. */
14869
14870 static void
14871 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14872 struct dwarf2_cu *cu)
14873 {
14874 int nfields = fip->nfields;
14875
14876 /* Record the field count, allocate space for the array of fields,
14877 and create blank accessibility bitfields if necessary. */
14878 TYPE_NFIELDS (type) = nfields;
14879 TYPE_FIELDS (type) = (struct field *)
14880 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14881
14882 if (fip->non_public_fields && cu->language != language_ada)
14883 {
14884 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14885
14886 TYPE_FIELD_PRIVATE_BITS (type) =
14887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14888 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14889
14890 TYPE_FIELD_PROTECTED_BITS (type) =
14891 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14892 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14893
14894 TYPE_FIELD_IGNORE_BITS (type) =
14895 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14896 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14897 }
14898
14899 /* If the type has baseclasses, allocate and clear a bit vector for
14900 TYPE_FIELD_VIRTUAL_BITS. */
14901 if (!fip->baseclasses.empty () && cu->language != language_ada)
14902 {
14903 int num_bytes = B_BYTES (fip->baseclasses.size ());
14904 unsigned char *pointer;
14905
14906 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14907 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14908 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14909 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14910 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14911 }
14912
14913 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14914 {
14915 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14916
14917 for (int index = 0; index < nfields; ++index)
14918 {
14919 struct nextfield &field = fip->fields[index];
14920
14921 if (field.variant.is_discriminant)
14922 di->discriminant_index = index;
14923 else if (field.variant.default_branch)
14924 di->default_index = index;
14925 else
14926 di->discriminants[index] = field.variant.discriminant_value;
14927 }
14928 }
14929
14930 /* Copy the saved-up fields into the field vector. */
14931 for (int i = 0; i < nfields; ++i)
14932 {
14933 struct nextfield &field
14934 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14935 : fip->fields[i - fip->baseclasses.size ()]);
14936
14937 TYPE_FIELD (type, i) = field.field;
14938 switch (field.accessibility)
14939 {
14940 case DW_ACCESS_private:
14941 if (cu->language != language_ada)
14942 SET_TYPE_FIELD_PRIVATE (type, i);
14943 break;
14944
14945 case DW_ACCESS_protected:
14946 if (cu->language != language_ada)
14947 SET_TYPE_FIELD_PROTECTED (type, i);
14948 break;
14949
14950 case DW_ACCESS_public:
14951 break;
14952
14953 default:
14954 /* Unknown accessibility. Complain and treat it as public. */
14955 {
14956 complaint (_("unsupported accessibility %d"),
14957 field.accessibility);
14958 }
14959 break;
14960 }
14961 if (i < fip->baseclasses.size ())
14962 {
14963 switch (field.virtuality)
14964 {
14965 case DW_VIRTUALITY_virtual:
14966 case DW_VIRTUALITY_pure_virtual:
14967 if (cu->language == language_ada)
14968 error (_("unexpected virtuality in component of Ada type"));
14969 SET_TYPE_FIELD_VIRTUAL (type, i);
14970 break;
14971 }
14972 }
14973 }
14974 }
14975
14976 /* Return true if this member function is a constructor, false
14977 otherwise. */
14978
14979 static int
14980 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14981 {
14982 const char *fieldname;
14983 const char *type_name;
14984 int len;
14985
14986 if (die->parent == NULL)
14987 return 0;
14988
14989 if (die->parent->tag != DW_TAG_structure_type
14990 && die->parent->tag != DW_TAG_union_type
14991 && die->parent->tag != DW_TAG_class_type)
14992 return 0;
14993
14994 fieldname = dwarf2_name (die, cu);
14995 type_name = dwarf2_name (die->parent, cu);
14996 if (fieldname == NULL || type_name == NULL)
14997 return 0;
14998
14999 len = strlen (fieldname);
15000 return (strncmp (fieldname, type_name, len) == 0
15001 && (type_name[len] == '\0' || type_name[len] == '<'));
15002 }
15003
15004 /* Check if the given VALUE is a recognized enum
15005 dwarf_defaulted_attribute constant according to DWARF5 spec,
15006 Table 7.24. */
15007
15008 static bool
15009 is_valid_DW_AT_defaulted (ULONGEST value)
15010 {
15011 switch (value)
15012 {
15013 case DW_DEFAULTED_no:
15014 case DW_DEFAULTED_in_class:
15015 case DW_DEFAULTED_out_of_class:
15016 return true;
15017 }
15018
15019 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15020 return false;
15021 }
15022
15023 /* Add a member function to the proper fieldlist. */
15024
15025 static void
15026 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15027 struct type *type, struct dwarf2_cu *cu)
15028 {
15029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15030 struct attribute *attr;
15031 int i;
15032 struct fnfieldlist *flp = nullptr;
15033 struct fn_field *fnp;
15034 const char *fieldname;
15035 struct type *this_type;
15036 enum dwarf_access_attribute accessibility;
15037
15038 if (cu->language == language_ada)
15039 error (_("unexpected member function in Ada type"));
15040
15041 /* Get name of member function. */
15042 fieldname = dwarf2_name (die, cu);
15043 if (fieldname == NULL)
15044 return;
15045
15046 /* Look up member function name in fieldlist. */
15047 for (i = 0; i < fip->fnfieldlists.size (); i++)
15048 {
15049 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15050 {
15051 flp = &fip->fnfieldlists[i];
15052 break;
15053 }
15054 }
15055
15056 /* Create a new fnfieldlist if necessary. */
15057 if (flp == nullptr)
15058 {
15059 fip->fnfieldlists.emplace_back ();
15060 flp = &fip->fnfieldlists.back ();
15061 flp->name = fieldname;
15062 i = fip->fnfieldlists.size () - 1;
15063 }
15064
15065 /* Create a new member function field and add it to the vector of
15066 fnfieldlists. */
15067 flp->fnfields.emplace_back ();
15068 fnp = &flp->fnfields.back ();
15069
15070 /* Delay processing of the physname until later. */
15071 if (cu->language == language_cplus)
15072 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15073 die, cu);
15074 else
15075 {
15076 const char *physname = dwarf2_physname (fieldname, die, cu);
15077 fnp->physname = physname ? physname : "";
15078 }
15079
15080 fnp->type = alloc_type (objfile);
15081 this_type = read_type_die (die, cu);
15082 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15083 {
15084 int nparams = TYPE_NFIELDS (this_type);
15085
15086 /* TYPE is the domain of this method, and THIS_TYPE is the type
15087 of the method itself (TYPE_CODE_METHOD). */
15088 smash_to_method_type (fnp->type, type,
15089 TYPE_TARGET_TYPE (this_type),
15090 TYPE_FIELDS (this_type),
15091 TYPE_NFIELDS (this_type),
15092 TYPE_VARARGS (this_type));
15093
15094 /* Handle static member functions.
15095 Dwarf2 has no clean way to discern C++ static and non-static
15096 member functions. G++ helps GDB by marking the first
15097 parameter for non-static member functions (which is the this
15098 pointer) as artificial. We obtain this information from
15099 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15100 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15101 fnp->voffset = VOFFSET_STATIC;
15102 }
15103 else
15104 complaint (_("member function type missing for '%s'"),
15105 dwarf2_full_name (fieldname, die, cu));
15106
15107 /* Get fcontext from DW_AT_containing_type if present. */
15108 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15109 fnp->fcontext = die_containing_type (die, cu);
15110
15111 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15112 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15113
15114 /* Get accessibility. */
15115 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15116 if (attr != nullptr)
15117 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15118 else
15119 accessibility = dwarf2_default_access_attribute (die, cu);
15120 switch (accessibility)
15121 {
15122 case DW_ACCESS_private:
15123 fnp->is_private = 1;
15124 break;
15125 case DW_ACCESS_protected:
15126 fnp->is_protected = 1;
15127 break;
15128 }
15129
15130 /* Check for artificial methods. */
15131 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15132 if (attr && DW_UNSND (attr) != 0)
15133 fnp->is_artificial = 1;
15134
15135 /* Check for defaulted methods. */
15136 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15137 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15138 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15139
15140 /* Check for deleted methods. */
15141 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15142 if (attr != nullptr && DW_UNSND (attr) != 0)
15143 fnp->is_deleted = 1;
15144
15145 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15146
15147 /* Get index in virtual function table if it is a virtual member
15148 function. For older versions of GCC, this is an offset in the
15149 appropriate virtual table, as specified by DW_AT_containing_type.
15150 For everyone else, it is an expression to be evaluated relative
15151 to the object address. */
15152
15153 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15154 if (attr != nullptr)
15155 {
15156 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15157 {
15158 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15159 {
15160 /* Old-style GCC. */
15161 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15162 }
15163 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15164 || (DW_BLOCK (attr)->size > 1
15165 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15166 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15167 {
15168 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15169 if ((fnp->voffset % cu->header.addr_size) != 0)
15170 dwarf2_complex_location_expr_complaint ();
15171 else
15172 fnp->voffset /= cu->header.addr_size;
15173 fnp->voffset += 2;
15174 }
15175 else
15176 dwarf2_complex_location_expr_complaint ();
15177
15178 if (!fnp->fcontext)
15179 {
15180 /* If there is no `this' field and no DW_AT_containing_type,
15181 we cannot actually find a base class context for the
15182 vtable! */
15183 if (TYPE_NFIELDS (this_type) == 0
15184 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15185 {
15186 complaint (_("cannot determine context for virtual member "
15187 "function \"%s\" (offset %s)"),
15188 fieldname, sect_offset_str (die->sect_off));
15189 }
15190 else
15191 {
15192 fnp->fcontext
15193 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15194 }
15195 }
15196 }
15197 else if (attr->form_is_section_offset ())
15198 {
15199 dwarf2_complex_location_expr_complaint ();
15200 }
15201 else
15202 {
15203 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15204 fieldname);
15205 }
15206 }
15207 else
15208 {
15209 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15210 if (attr && DW_UNSND (attr))
15211 {
15212 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15213 complaint (_("Member function \"%s\" (offset %s) is virtual "
15214 "but the vtable offset is not specified"),
15215 fieldname, sect_offset_str (die->sect_off));
15216 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15217 TYPE_CPLUS_DYNAMIC (type) = 1;
15218 }
15219 }
15220 }
15221
15222 /* Create the vector of member function fields, and attach it to the type. */
15223
15224 static void
15225 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15226 struct dwarf2_cu *cu)
15227 {
15228 if (cu->language == language_ada)
15229 error (_("unexpected member functions in Ada type"));
15230
15231 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15232 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15233 TYPE_ALLOC (type,
15234 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15235
15236 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15237 {
15238 struct fnfieldlist &nf = fip->fnfieldlists[i];
15239 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15240
15241 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15242 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15243 fn_flp->fn_fields = (struct fn_field *)
15244 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15245
15246 for (int k = 0; k < nf.fnfields.size (); ++k)
15247 fn_flp->fn_fields[k] = nf.fnfields[k];
15248 }
15249
15250 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15251 }
15252
15253 /* Returns non-zero if NAME is the name of a vtable member in CU's
15254 language, zero otherwise. */
15255 static int
15256 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15257 {
15258 static const char vptr[] = "_vptr";
15259
15260 /* Look for the C++ form of the vtable. */
15261 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15262 return 1;
15263
15264 return 0;
15265 }
15266
15267 /* GCC outputs unnamed structures that are really pointers to member
15268 functions, with the ABI-specified layout. If TYPE describes
15269 such a structure, smash it into a member function type.
15270
15271 GCC shouldn't do this; it should just output pointer to member DIEs.
15272 This is GCC PR debug/28767. */
15273
15274 static void
15275 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15276 {
15277 struct type *pfn_type, *self_type, *new_type;
15278
15279 /* Check for a structure with no name and two children. */
15280 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15281 return;
15282
15283 /* Check for __pfn and __delta members. */
15284 if (TYPE_FIELD_NAME (type, 0) == NULL
15285 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15286 || TYPE_FIELD_NAME (type, 1) == NULL
15287 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15288 return;
15289
15290 /* Find the type of the method. */
15291 pfn_type = TYPE_FIELD_TYPE (type, 0);
15292 if (pfn_type == NULL
15293 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15294 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15295 return;
15296
15297 /* Look for the "this" argument. */
15298 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15299 if (TYPE_NFIELDS (pfn_type) == 0
15300 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15301 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15302 return;
15303
15304 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15305 new_type = alloc_type (objfile);
15306 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15307 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15308 TYPE_VARARGS (pfn_type));
15309 smash_to_methodptr_type (type, new_type);
15310 }
15311
15312 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15313 appropriate error checking and issuing complaints if there is a
15314 problem. */
15315
15316 static ULONGEST
15317 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15318 {
15319 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15320
15321 if (attr == nullptr)
15322 return 0;
15323
15324 if (!attr->form_is_constant ())
15325 {
15326 complaint (_("DW_AT_alignment must have constant form"
15327 " - DIE at %s [in module %s]"),
15328 sect_offset_str (die->sect_off),
15329 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15330 return 0;
15331 }
15332
15333 ULONGEST align;
15334 if (attr->form == DW_FORM_sdata)
15335 {
15336 LONGEST val = DW_SND (attr);
15337 if (val < 0)
15338 {
15339 complaint (_("DW_AT_alignment value must not be negative"
15340 " - DIE at %s [in module %s]"),
15341 sect_offset_str (die->sect_off),
15342 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15343 return 0;
15344 }
15345 align = val;
15346 }
15347 else
15348 align = DW_UNSND (attr);
15349
15350 if (align == 0)
15351 {
15352 complaint (_("DW_AT_alignment value must not be zero"
15353 " - DIE at %s [in module %s]"),
15354 sect_offset_str (die->sect_off),
15355 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15356 return 0;
15357 }
15358 if ((align & (align - 1)) != 0)
15359 {
15360 complaint (_("DW_AT_alignment value must be a power of 2"
15361 " - DIE at %s [in module %s]"),
15362 sect_offset_str (die->sect_off),
15363 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15364 return 0;
15365 }
15366
15367 return align;
15368 }
15369
15370 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15371 the alignment for TYPE. */
15372
15373 static void
15374 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15375 struct type *type)
15376 {
15377 if (!set_type_align (type, get_alignment (cu, die)))
15378 complaint (_("DW_AT_alignment value too large"
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 }
15383
15384 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15385 constant for a type, according to DWARF5 spec, Table 5.5. */
15386
15387 static bool
15388 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15389 {
15390 switch (value)
15391 {
15392 case DW_CC_normal:
15393 case DW_CC_pass_by_reference:
15394 case DW_CC_pass_by_value:
15395 return true;
15396
15397 default:
15398 complaint (_("unrecognized DW_AT_calling_convention value "
15399 "(%s) for a type"), pulongest (value));
15400 return false;
15401 }
15402 }
15403
15404 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15405 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15406 also according to GNU-specific values (see include/dwarf2.h). */
15407
15408 static bool
15409 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15410 {
15411 switch (value)
15412 {
15413 case DW_CC_normal:
15414 case DW_CC_program:
15415 case DW_CC_nocall:
15416 return true;
15417
15418 case DW_CC_GNU_renesas_sh:
15419 case DW_CC_GNU_borland_fastcall_i386:
15420 case DW_CC_GDB_IBM_OpenCL:
15421 return true;
15422
15423 default:
15424 complaint (_("unrecognized DW_AT_calling_convention value "
15425 "(%s) for a subroutine"), pulongest (value));
15426 return false;
15427 }
15428 }
15429
15430 /* Called when we find the DIE that starts a structure or union scope
15431 (definition) to create a type for the structure or union. Fill in
15432 the type's name and general properties; the members will not be
15433 processed until process_structure_scope. A symbol table entry for
15434 the type will also not be done until process_structure_scope (assuming
15435 the type has a name).
15436
15437 NOTE: we need to call these functions regardless of whether or not the
15438 DIE has a DW_AT_name attribute, since it might be an anonymous
15439 structure or union. This gets the type entered into our set of
15440 user defined types. */
15441
15442 static struct type *
15443 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15444 {
15445 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15446 struct type *type;
15447 struct attribute *attr;
15448 const char *name;
15449
15450 /* If the definition of this type lives in .debug_types, read that type.
15451 Don't follow DW_AT_specification though, that will take us back up
15452 the chain and we want to go down. */
15453 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15454 if (attr != nullptr)
15455 {
15456 type = get_DW_AT_signature_type (die, attr, cu);
15457
15458 /* The type's CU may not be the same as CU.
15459 Ensure TYPE is recorded with CU in die_type_hash. */
15460 return set_die_type (die, type, cu);
15461 }
15462
15463 type = alloc_type (objfile);
15464 INIT_CPLUS_SPECIFIC (type);
15465
15466 name = dwarf2_name (die, cu);
15467 if (name != NULL)
15468 {
15469 if (cu->language == language_cplus
15470 || cu->language == language_d
15471 || cu->language == language_rust)
15472 {
15473 const char *full_name = dwarf2_full_name (name, die, cu);
15474
15475 /* dwarf2_full_name might have already finished building the DIE's
15476 type. If so, there is no need to continue. */
15477 if (get_die_type (die, cu) != NULL)
15478 return get_die_type (die, cu);
15479
15480 TYPE_NAME (type) = full_name;
15481 }
15482 else
15483 {
15484 /* The name is already allocated along with this objfile, so
15485 we don't need to duplicate it for the type. */
15486 TYPE_NAME (type) = name;
15487 }
15488 }
15489
15490 if (die->tag == DW_TAG_structure_type)
15491 {
15492 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15493 }
15494 else if (die->tag == DW_TAG_union_type)
15495 {
15496 TYPE_CODE (type) = TYPE_CODE_UNION;
15497 }
15498 else if (die->tag == DW_TAG_variant_part)
15499 {
15500 TYPE_CODE (type) = TYPE_CODE_UNION;
15501 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15502 }
15503 else
15504 {
15505 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15506 }
15507
15508 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15509 TYPE_DECLARED_CLASS (type) = 1;
15510
15511 /* Store the calling convention in the type if it's available in
15512 the die. Otherwise the calling convention remains set to
15513 the default value DW_CC_normal. */
15514 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15515 if (attr != nullptr
15516 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15517 {
15518 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15519 TYPE_CPLUS_CALLING_CONVENTION (type)
15520 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15521 }
15522
15523 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15524 if (attr != nullptr)
15525 {
15526 if (attr->form_is_constant ())
15527 TYPE_LENGTH (type) = DW_UNSND (attr);
15528 else
15529 {
15530 /* For the moment, dynamic type sizes are not supported
15531 by GDB's struct type. The actual size is determined
15532 on-demand when resolving the type of a given object,
15533 so set the type's length to zero for now. Otherwise,
15534 we record an expression as the length, and that expression
15535 could lead to a very large value, which could eventually
15536 lead to us trying to allocate that much memory when creating
15537 a value of that type. */
15538 TYPE_LENGTH (type) = 0;
15539 }
15540 }
15541 else
15542 {
15543 TYPE_LENGTH (type) = 0;
15544 }
15545
15546 maybe_set_alignment (cu, die, type);
15547
15548 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15549 {
15550 /* ICC<14 does not output the required DW_AT_declaration on
15551 incomplete types, but gives them a size of zero. */
15552 TYPE_STUB (type) = 1;
15553 }
15554 else
15555 TYPE_STUB_SUPPORTED (type) = 1;
15556
15557 if (die_is_declaration (die, cu))
15558 TYPE_STUB (type) = 1;
15559 else if (attr == NULL && die->child == NULL
15560 && producer_is_realview (cu->producer))
15561 /* RealView does not output the required DW_AT_declaration
15562 on incomplete types. */
15563 TYPE_STUB (type) = 1;
15564
15565 /* We need to add the type field to the die immediately so we don't
15566 infinitely recurse when dealing with pointers to the structure
15567 type within the structure itself. */
15568 set_die_type (die, type, cu);
15569
15570 /* set_die_type should be already done. */
15571 set_descriptive_type (type, die, cu);
15572
15573 return type;
15574 }
15575
15576 /* A helper for process_structure_scope that handles a single member
15577 DIE. */
15578
15579 static void
15580 handle_struct_member_die (struct die_info *child_die, struct type *type,
15581 struct field_info *fi,
15582 std::vector<struct symbol *> *template_args,
15583 struct dwarf2_cu *cu)
15584 {
15585 if (child_die->tag == DW_TAG_member
15586 || child_die->tag == DW_TAG_variable
15587 || child_die->tag == DW_TAG_variant_part)
15588 {
15589 /* NOTE: carlton/2002-11-05: A C++ static data member
15590 should be a DW_TAG_member that is a declaration, but
15591 all versions of G++ as of this writing (so through at
15592 least 3.2.1) incorrectly generate DW_TAG_variable
15593 tags for them instead. */
15594 dwarf2_add_field (fi, child_die, cu);
15595 }
15596 else if (child_die->tag == DW_TAG_subprogram)
15597 {
15598 /* Rust doesn't have member functions in the C++ sense.
15599 However, it does emit ordinary functions as children
15600 of a struct DIE. */
15601 if (cu->language == language_rust)
15602 read_func_scope (child_die, cu);
15603 else
15604 {
15605 /* C++ member function. */
15606 dwarf2_add_member_fn (fi, child_die, type, cu);
15607 }
15608 }
15609 else if (child_die->tag == DW_TAG_inheritance)
15610 {
15611 /* C++ base class field. */
15612 dwarf2_add_field (fi, child_die, cu);
15613 }
15614 else if (type_can_define_types (child_die))
15615 dwarf2_add_type_defn (fi, child_die, cu);
15616 else if (child_die->tag == DW_TAG_template_type_param
15617 || child_die->tag == DW_TAG_template_value_param)
15618 {
15619 struct symbol *arg = new_symbol (child_die, NULL, cu);
15620
15621 if (arg != NULL)
15622 template_args->push_back (arg);
15623 }
15624 else if (child_die->tag == DW_TAG_variant)
15625 {
15626 /* In a variant we want to get the discriminant and also add a
15627 field for our sole member child. */
15628 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15629
15630 for (die_info *variant_child = child_die->child;
15631 variant_child != NULL;
15632 variant_child = sibling_die (variant_child))
15633 {
15634 if (variant_child->tag == DW_TAG_member)
15635 {
15636 handle_struct_member_die (variant_child, type, fi,
15637 template_args, cu);
15638 /* Only handle the one. */
15639 break;
15640 }
15641 }
15642
15643 /* We don't handle this but we might as well report it if we see
15644 it. */
15645 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15646 complaint (_("DW_AT_discr_list is not supported yet"
15647 " - DIE at %s [in module %s]"),
15648 sect_offset_str (child_die->sect_off),
15649 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15650
15651 /* The first field was just added, so we can stash the
15652 discriminant there. */
15653 gdb_assert (!fi->fields.empty ());
15654 if (discr == NULL)
15655 fi->fields.back ().variant.default_branch = true;
15656 else
15657 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15658 }
15659 }
15660
15661 /* Finish creating a structure or union type, including filling in
15662 its members and creating a symbol for it. */
15663
15664 static void
15665 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15666 {
15667 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15668 struct die_info *child_die;
15669 struct type *type;
15670
15671 type = get_die_type (die, cu);
15672 if (type == NULL)
15673 type = read_structure_type (die, cu);
15674
15675 /* When reading a DW_TAG_variant_part, we need to notice when we
15676 read the discriminant member, so we can record it later in the
15677 discriminant_info. */
15678 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15679 sect_offset discr_offset {};
15680 bool has_template_parameters = false;
15681
15682 if (is_variant_part)
15683 {
15684 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15685 if (discr == NULL)
15686 {
15687 /* Maybe it's a univariant form, an extension we support.
15688 In this case arrange not to check the offset. */
15689 is_variant_part = false;
15690 }
15691 else if (discr->form_is_ref ())
15692 {
15693 struct dwarf2_cu *target_cu = cu;
15694 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15695
15696 discr_offset = target_die->sect_off;
15697 }
15698 else
15699 {
15700 complaint (_("DW_AT_discr does not have DIE reference form"
15701 " - DIE at %s [in module %s]"),
15702 sect_offset_str (die->sect_off),
15703 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15704 is_variant_part = false;
15705 }
15706 }
15707
15708 if (die->child != NULL && ! die_is_declaration (die, cu))
15709 {
15710 struct field_info fi;
15711 std::vector<struct symbol *> template_args;
15712
15713 child_die = die->child;
15714
15715 while (child_die && child_die->tag)
15716 {
15717 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15718
15719 if (is_variant_part && discr_offset == child_die->sect_off)
15720 fi.fields.back ().variant.is_discriminant = true;
15721
15722 child_die = sibling_die (child_die);
15723 }
15724
15725 /* Attach template arguments to type. */
15726 if (!template_args.empty ())
15727 {
15728 has_template_parameters = true;
15729 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15730 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15731 TYPE_TEMPLATE_ARGUMENTS (type)
15732 = XOBNEWVEC (&objfile->objfile_obstack,
15733 struct symbol *,
15734 TYPE_N_TEMPLATE_ARGUMENTS (type));
15735 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15736 template_args.data (),
15737 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15738 * sizeof (struct symbol *)));
15739 }
15740
15741 /* Attach fields and member functions to the type. */
15742 if (fi.nfields)
15743 dwarf2_attach_fields_to_type (&fi, type, cu);
15744 if (!fi.fnfieldlists.empty ())
15745 {
15746 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15747
15748 /* Get the type which refers to the base class (possibly this
15749 class itself) which contains the vtable pointer for the current
15750 class from the DW_AT_containing_type attribute. This use of
15751 DW_AT_containing_type is a GNU extension. */
15752
15753 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15754 {
15755 struct type *t = die_containing_type (die, cu);
15756
15757 set_type_vptr_basetype (type, t);
15758 if (type == t)
15759 {
15760 int i;
15761
15762 /* Our own class provides vtbl ptr. */
15763 for (i = TYPE_NFIELDS (t) - 1;
15764 i >= TYPE_N_BASECLASSES (t);
15765 --i)
15766 {
15767 const char *fieldname = TYPE_FIELD_NAME (t, i);
15768
15769 if (is_vtable_name (fieldname, cu))
15770 {
15771 set_type_vptr_fieldno (type, i);
15772 break;
15773 }
15774 }
15775
15776 /* Complain if virtual function table field not found. */
15777 if (i < TYPE_N_BASECLASSES (t))
15778 complaint (_("virtual function table pointer "
15779 "not found when defining class '%s'"),
15780 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15781 }
15782 else
15783 {
15784 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15785 }
15786 }
15787 else if (cu->producer
15788 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15789 {
15790 /* The IBM XLC compiler does not provide direct indication
15791 of the containing type, but the vtable pointer is
15792 always named __vfp. */
15793
15794 int i;
15795
15796 for (i = TYPE_NFIELDS (type) - 1;
15797 i >= TYPE_N_BASECLASSES (type);
15798 --i)
15799 {
15800 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15801 {
15802 set_type_vptr_fieldno (type, i);
15803 set_type_vptr_basetype (type, type);
15804 break;
15805 }
15806 }
15807 }
15808 }
15809
15810 /* Copy fi.typedef_field_list linked list elements content into the
15811 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15812 if (!fi.typedef_field_list.empty ())
15813 {
15814 int count = fi.typedef_field_list.size ();
15815
15816 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15817 TYPE_TYPEDEF_FIELD_ARRAY (type)
15818 = ((struct decl_field *)
15819 TYPE_ALLOC (type,
15820 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15821 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15822
15823 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15824 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15825 }
15826
15827 /* Copy fi.nested_types_list linked list elements content into the
15828 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15829 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15830 {
15831 int count = fi.nested_types_list.size ();
15832
15833 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15834 TYPE_NESTED_TYPES_ARRAY (type)
15835 = ((struct decl_field *)
15836 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15837 TYPE_NESTED_TYPES_COUNT (type) = count;
15838
15839 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15840 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15841 }
15842 }
15843
15844 quirk_gcc_member_function_pointer (type, objfile);
15845 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15846 cu->rust_unions.push_back (type);
15847
15848 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15849 snapshots) has been known to create a die giving a declaration
15850 for a class that has, as a child, a die giving a definition for a
15851 nested class. So we have to process our children even if the
15852 current die is a declaration. Normally, of course, a declaration
15853 won't have any children at all. */
15854
15855 child_die = die->child;
15856
15857 while (child_die != NULL && child_die->tag)
15858 {
15859 if (child_die->tag == DW_TAG_member
15860 || child_die->tag == DW_TAG_variable
15861 || child_die->tag == DW_TAG_inheritance
15862 || child_die->tag == DW_TAG_template_value_param
15863 || child_die->tag == DW_TAG_template_type_param)
15864 {
15865 /* Do nothing. */
15866 }
15867 else
15868 process_die (child_die, cu);
15869
15870 child_die = sibling_die (child_die);
15871 }
15872
15873 /* Do not consider external references. According to the DWARF standard,
15874 these DIEs are identified by the fact that they have no byte_size
15875 attribute, and a declaration attribute. */
15876 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15877 || !die_is_declaration (die, cu))
15878 {
15879 struct symbol *sym = new_symbol (die, type, cu);
15880
15881 if (has_template_parameters)
15882 {
15883 struct symtab *symtab;
15884 if (sym != nullptr)
15885 symtab = symbol_symtab (sym);
15886 else if (cu->line_header != nullptr)
15887 {
15888 /* Any related symtab will do. */
15889 symtab
15890 = cu->line_header->file_names ()[0].symtab;
15891 }
15892 else
15893 {
15894 symtab = nullptr;
15895 complaint (_("could not find suitable "
15896 "symtab for template parameter"
15897 " - DIE at %s [in module %s]"),
15898 sect_offset_str (die->sect_off),
15899 objfile_name (objfile));
15900 }
15901
15902 if (symtab != nullptr)
15903 {
15904 /* Make sure that the symtab is set on the new symbols.
15905 Even though they don't appear in this symtab directly,
15906 other parts of gdb assume that symbols do, and this is
15907 reasonably true. */
15908 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15909 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15910 }
15911 }
15912 }
15913 }
15914
15915 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15916 update TYPE using some information only available in DIE's children. */
15917
15918 static void
15919 update_enumeration_type_from_children (struct die_info *die,
15920 struct type *type,
15921 struct dwarf2_cu *cu)
15922 {
15923 struct die_info *child_die;
15924 int unsigned_enum = 1;
15925 int flag_enum = 1;
15926 ULONGEST mask = 0;
15927
15928 auto_obstack obstack;
15929
15930 for (child_die = die->child;
15931 child_die != NULL && child_die->tag;
15932 child_die = sibling_die (child_die))
15933 {
15934 struct attribute *attr;
15935 LONGEST value;
15936 const gdb_byte *bytes;
15937 struct dwarf2_locexpr_baton *baton;
15938 const char *name;
15939
15940 if (child_die->tag != DW_TAG_enumerator)
15941 continue;
15942
15943 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15944 if (attr == NULL)
15945 continue;
15946
15947 name = dwarf2_name (child_die, cu);
15948 if (name == NULL)
15949 name = "<anonymous enumerator>";
15950
15951 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15952 &value, &bytes, &baton);
15953 if (value < 0)
15954 {
15955 unsigned_enum = 0;
15956 flag_enum = 0;
15957 }
15958 else if ((mask & value) != 0)
15959 flag_enum = 0;
15960 else
15961 mask |= value;
15962
15963 /* If we already know that the enum type is neither unsigned, nor
15964 a flag type, no need to look at the rest of the enumerates. */
15965 if (!unsigned_enum && !flag_enum)
15966 break;
15967 }
15968
15969 if (unsigned_enum)
15970 TYPE_UNSIGNED (type) = 1;
15971 if (flag_enum)
15972 TYPE_FLAG_ENUM (type) = 1;
15973 }
15974
15975 /* Given a DW_AT_enumeration_type die, set its type. We do not
15976 complete the type's fields yet, or create any symbols. */
15977
15978 static struct type *
15979 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15980 {
15981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15982 struct type *type;
15983 struct attribute *attr;
15984 const char *name;
15985
15986 /* If the definition of this type lives in .debug_types, read that type.
15987 Don't follow DW_AT_specification though, that will take us back up
15988 the chain and we want to go down. */
15989 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15990 if (attr != nullptr)
15991 {
15992 type = get_DW_AT_signature_type (die, attr, cu);
15993
15994 /* The type's CU may not be the same as CU.
15995 Ensure TYPE is recorded with CU in die_type_hash. */
15996 return set_die_type (die, type, cu);
15997 }
15998
15999 type = alloc_type (objfile);
16000
16001 TYPE_CODE (type) = TYPE_CODE_ENUM;
16002 name = dwarf2_full_name (NULL, die, cu);
16003 if (name != NULL)
16004 TYPE_NAME (type) = name;
16005
16006 attr = dwarf2_attr (die, DW_AT_type, cu);
16007 if (attr != NULL)
16008 {
16009 struct type *underlying_type = die_type (die, cu);
16010
16011 TYPE_TARGET_TYPE (type) = underlying_type;
16012 }
16013
16014 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16015 if (attr != nullptr)
16016 {
16017 TYPE_LENGTH (type) = DW_UNSND (attr);
16018 }
16019 else
16020 {
16021 TYPE_LENGTH (type) = 0;
16022 }
16023
16024 maybe_set_alignment (cu, die, type);
16025
16026 /* The enumeration DIE can be incomplete. In Ada, any type can be
16027 declared as private in the package spec, and then defined only
16028 inside the package body. Such types are known as Taft Amendment
16029 Types. When another package uses such a type, an incomplete DIE
16030 may be generated by the compiler. */
16031 if (die_is_declaration (die, cu))
16032 TYPE_STUB (type) = 1;
16033
16034 /* Finish the creation of this type by using the enum's children.
16035 We must call this even when the underlying type has been provided
16036 so that we can determine if we're looking at a "flag" enum. */
16037 update_enumeration_type_from_children (die, type, cu);
16038
16039 /* If this type has an underlying type that is not a stub, then we
16040 may use its attributes. We always use the "unsigned" attribute
16041 in this situation, because ordinarily we guess whether the type
16042 is unsigned -- but the guess can be wrong and the underlying type
16043 can tell us the reality. However, we defer to a local size
16044 attribute if one exists, because this lets the compiler override
16045 the underlying type if needed. */
16046 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16047 {
16048 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16049 if (TYPE_LENGTH (type) == 0)
16050 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16051 if (TYPE_RAW_ALIGN (type) == 0
16052 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16053 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16054 }
16055
16056 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16057
16058 return set_die_type (die, type, cu);
16059 }
16060
16061 /* Given a pointer to a die which begins an enumeration, process all
16062 the dies that define the members of the enumeration, and create the
16063 symbol for the enumeration type.
16064
16065 NOTE: We reverse the order of the element list. */
16066
16067 static void
16068 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16069 {
16070 struct type *this_type;
16071
16072 this_type = get_die_type (die, cu);
16073 if (this_type == NULL)
16074 this_type = read_enumeration_type (die, cu);
16075
16076 if (die->child != NULL)
16077 {
16078 struct die_info *child_die;
16079 struct symbol *sym;
16080 std::vector<struct field> fields;
16081 const char *name;
16082
16083 child_die = die->child;
16084 while (child_die && child_die->tag)
16085 {
16086 if (child_die->tag != DW_TAG_enumerator)
16087 {
16088 process_die (child_die, cu);
16089 }
16090 else
16091 {
16092 name = dwarf2_name (child_die, cu);
16093 if (name)
16094 {
16095 sym = new_symbol (child_die, this_type, cu);
16096
16097 fields.emplace_back ();
16098 struct field &field = fields.back ();
16099
16100 FIELD_NAME (field) = sym->linkage_name ();
16101 FIELD_TYPE (field) = NULL;
16102 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16103 FIELD_BITSIZE (field) = 0;
16104 }
16105 }
16106
16107 child_die = sibling_die (child_die);
16108 }
16109
16110 if (!fields.empty ())
16111 {
16112 TYPE_NFIELDS (this_type) = fields.size ();
16113 TYPE_FIELDS (this_type) = (struct field *)
16114 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16115 memcpy (TYPE_FIELDS (this_type), fields.data (),
16116 sizeof (struct field) * fields.size ());
16117 }
16118 }
16119
16120 /* If we are reading an enum from a .debug_types unit, and the enum
16121 is a declaration, and the enum is not the signatured type in the
16122 unit, then we do not want to add a symbol for it. Adding a
16123 symbol would in some cases obscure the true definition of the
16124 enum, giving users an incomplete type when the definition is
16125 actually available. Note that we do not want to do this for all
16126 enums which are just declarations, because C++0x allows forward
16127 enum declarations. */
16128 if (cu->per_cu->is_debug_types
16129 && die_is_declaration (die, cu))
16130 {
16131 struct signatured_type *sig_type;
16132
16133 sig_type = (struct signatured_type *) cu->per_cu;
16134 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16135 if (sig_type->type_offset_in_section != die->sect_off)
16136 return;
16137 }
16138
16139 new_symbol (die, this_type, cu);
16140 }
16141
16142 /* Extract all information from a DW_TAG_array_type DIE and put it in
16143 the DIE's type field. For now, this only handles one dimensional
16144 arrays. */
16145
16146 static struct type *
16147 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16148 {
16149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16150 struct die_info *child_die;
16151 struct type *type;
16152 struct type *element_type, *range_type, *index_type;
16153 struct attribute *attr;
16154 const char *name;
16155 struct dynamic_prop *byte_stride_prop = NULL;
16156 unsigned int bit_stride = 0;
16157
16158 element_type = die_type (die, cu);
16159
16160 /* The die_type call above may have already set the type for this DIE. */
16161 type = get_die_type (die, cu);
16162 if (type)
16163 return type;
16164
16165 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16166 if (attr != NULL)
16167 {
16168 int stride_ok;
16169 struct type *prop_type
16170 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16171
16172 byte_stride_prop
16173 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16174 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16175 prop_type);
16176 if (!stride_ok)
16177 {
16178 complaint (_("unable to read array DW_AT_byte_stride "
16179 " - DIE at %s [in module %s]"),
16180 sect_offset_str (die->sect_off),
16181 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16182 /* Ignore this attribute. We will likely not be able to print
16183 arrays of this type correctly, but there is little we can do
16184 to help if we cannot read the attribute's value. */
16185 byte_stride_prop = NULL;
16186 }
16187 }
16188
16189 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16190 if (attr != NULL)
16191 bit_stride = DW_UNSND (attr);
16192
16193 /* Irix 6.2 native cc creates array types without children for
16194 arrays with unspecified length. */
16195 if (die->child == NULL)
16196 {
16197 index_type = objfile_type (objfile)->builtin_int;
16198 range_type = create_static_range_type (NULL, index_type, 0, -1);
16199 type = create_array_type_with_stride (NULL, element_type, range_type,
16200 byte_stride_prop, bit_stride);
16201 return set_die_type (die, type, cu);
16202 }
16203
16204 std::vector<struct type *> range_types;
16205 child_die = die->child;
16206 while (child_die && child_die->tag)
16207 {
16208 if (child_die->tag == DW_TAG_subrange_type)
16209 {
16210 struct type *child_type = read_type_die (child_die, cu);
16211
16212 if (child_type != NULL)
16213 {
16214 /* The range type was succesfully read. Save it for the
16215 array type creation. */
16216 range_types.push_back (child_type);
16217 }
16218 }
16219 child_die = sibling_die (child_die);
16220 }
16221
16222 /* Dwarf2 dimensions are output from left to right, create the
16223 necessary array types in backwards order. */
16224
16225 type = element_type;
16226
16227 if (read_array_order (die, cu) == DW_ORD_col_major)
16228 {
16229 int i = 0;
16230
16231 while (i < range_types.size ())
16232 type = create_array_type_with_stride (NULL, type, range_types[i++],
16233 byte_stride_prop, bit_stride);
16234 }
16235 else
16236 {
16237 size_t ndim = range_types.size ();
16238 while (ndim-- > 0)
16239 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16240 byte_stride_prop, bit_stride);
16241 }
16242
16243 /* Understand Dwarf2 support for vector types (like they occur on
16244 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16245 array type. This is not part of the Dwarf2/3 standard yet, but a
16246 custom vendor extension. The main difference between a regular
16247 array and the vector variant is that vectors are passed by value
16248 to functions. */
16249 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16250 if (attr != nullptr)
16251 make_vector_type (type);
16252
16253 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16254 implementation may choose to implement triple vectors using this
16255 attribute. */
16256 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16257 if (attr != nullptr)
16258 {
16259 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16260 TYPE_LENGTH (type) = DW_UNSND (attr);
16261 else
16262 complaint (_("DW_AT_byte_size for array type smaller "
16263 "than the total size of elements"));
16264 }
16265
16266 name = dwarf2_name (die, cu);
16267 if (name)
16268 TYPE_NAME (type) = name;
16269
16270 maybe_set_alignment (cu, die, type);
16271
16272 /* Install the type in the die. */
16273 set_die_type (die, type, cu);
16274
16275 /* set_die_type should be already done. */
16276 set_descriptive_type (type, die, cu);
16277
16278 return type;
16279 }
16280
16281 static enum dwarf_array_dim_ordering
16282 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16283 {
16284 struct attribute *attr;
16285
16286 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16287
16288 if (attr != nullptr)
16289 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16290
16291 /* GNU F77 is a special case, as at 08/2004 array type info is the
16292 opposite order to the dwarf2 specification, but data is still
16293 laid out as per normal fortran.
16294
16295 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16296 version checking. */
16297
16298 if (cu->language == language_fortran
16299 && cu->producer && strstr (cu->producer, "GNU F77"))
16300 {
16301 return DW_ORD_row_major;
16302 }
16303
16304 switch (cu->language_defn->la_array_ordering)
16305 {
16306 case array_column_major:
16307 return DW_ORD_col_major;
16308 case array_row_major:
16309 default:
16310 return DW_ORD_row_major;
16311 };
16312 }
16313
16314 /* Extract all information from a DW_TAG_set_type DIE and put it in
16315 the DIE's type field. */
16316
16317 static struct type *
16318 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16319 {
16320 struct type *domain_type, *set_type;
16321 struct attribute *attr;
16322
16323 domain_type = die_type (die, cu);
16324
16325 /* The die_type call above may have already set the type for this DIE. */
16326 set_type = get_die_type (die, cu);
16327 if (set_type)
16328 return set_type;
16329
16330 set_type = create_set_type (NULL, domain_type);
16331
16332 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16333 if (attr != nullptr)
16334 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16335
16336 maybe_set_alignment (cu, die, set_type);
16337
16338 return set_die_type (die, set_type, cu);
16339 }
16340
16341 /* A helper for read_common_block that creates a locexpr baton.
16342 SYM is the symbol which we are marking as computed.
16343 COMMON_DIE is the DIE for the common block.
16344 COMMON_LOC is the location expression attribute for the common
16345 block itself.
16346 MEMBER_LOC is the location expression attribute for the particular
16347 member of the common block that we are processing.
16348 CU is the CU from which the above come. */
16349
16350 static void
16351 mark_common_block_symbol_computed (struct symbol *sym,
16352 struct die_info *common_die,
16353 struct attribute *common_loc,
16354 struct attribute *member_loc,
16355 struct dwarf2_cu *cu)
16356 {
16357 struct dwarf2_per_objfile *dwarf2_per_objfile
16358 = cu->per_cu->dwarf2_per_objfile;
16359 struct objfile *objfile = dwarf2_per_objfile->objfile;
16360 struct dwarf2_locexpr_baton *baton;
16361 gdb_byte *ptr;
16362 unsigned int cu_off;
16363 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16364 LONGEST offset = 0;
16365
16366 gdb_assert (common_loc && member_loc);
16367 gdb_assert (common_loc->form_is_block ());
16368 gdb_assert (member_loc->form_is_block ()
16369 || member_loc->form_is_constant ());
16370
16371 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16372 baton->per_cu = cu->per_cu;
16373 gdb_assert (baton->per_cu);
16374
16375 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16376
16377 if (member_loc->form_is_constant ())
16378 {
16379 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16380 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16381 }
16382 else
16383 baton->size += DW_BLOCK (member_loc)->size;
16384
16385 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16386 baton->data = ptr;
16387
16388 *ptr++ = DW_OP_call4;
16389 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16390 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16391 ptr += 4;
16392
16393 if (member_loc->form_is_constant ())
16394 {
16395 *ptr++ = DW_OP_addr;
16396 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16397 ptr += cu->header.addr_size;
16398 }
16399 else
16400 {
16401 /* We have to copy the data here, because DW_OP_call4 will only
16402 use a DW_AT_location attribute. */
16403 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16404 ptr += DW_BLOCK (member_loc)->size;
16405 }
16406
16407 *ptr++ = DW_OP_plus;
16408 gdb_assert (ptr - baton->data == baton->size);
16409
16410 SYMBOL_LOCATION_BATON (sym) = baton;
16411 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16412 }
16413
16414 /* Create appropriate locally-scoped variables for all the
16415 DW_TAG_common_block entries. Also create a struct common_block
16416 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16417 is used to separate the common blocks name namespace from regular
16418 variable names. */
16419
16420 static void
16421 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16422 {
16423 struct attribute *attr;
16424
16425 attr = dwarf2_attr (die, DW_AT_location, cu);
16426 if (attr != nullptr)
16427 {
16428 /* Support the .debug_loc offsets. */
16429 if (attr->form_is_block ())
16430 {
16431 /* Ok. */
16432 }
16433 else if (attr->form_is_section_offset ())
16434 {
16435 dwarf2_complex_location_expr_complaint ();
16436 attr = NULL;
16437 }
16438 else
16439 {
16440 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16441 "common block member");
16442 attr = NULL;
16443 }
16444 }
16445
16446 if (die->child != NULL)
16447 {
16448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16449 struct die_info *child_die;
16450 size_t n_entries = 0, size;
16451 struct common_block *common_block;
16452 struct symbol *sym;
16453
16454 for (child_die = die->child;
16455 child_die && child_die->tag;
16456 child_die = sibling_die (child_die))
16457 ++n_entries;
16458
16459 size = (sizeof (struct common_block)
16460 + (n_entries - 1) * sizeof (struct symbol *));
16461 common_block
16462 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16463 size);
16464 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16465 common_block->n_entries = 0;
16466
16467 for (child_die = die->child;
16468 child_die && child_die->tag;
16469 child_die = sibling_die (child_die))
16470 {
16471 /* Create the symbol in the DW_TAG_common_block block in the current
16472 symbol scope. */
16473 sym = new_symbol (child_die, NULL, cu);
16474 if (sym != NULL)
16475 {
16476 struct attribute *member_loc;
16477
16478 common_block->contents[common_block->n_entries++] = sym;
16479
16480 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16481 cu);
16482 if (member_loc)
16483 {
16484 /* GDB has handled this for a long time, but it is
16485 not specified by DWARF. It seems to have been
16486 emitted by gfortran at least as recently as:
16487 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16488 complaint (_("Variable in common block has "
16489 "DW_AT_data_member_location "
16490 "- DIE at %s [in module %s]"),
16491 sect_offset_str (child_die->sect_off),
16492 objfile_name (objfile));
16493
16494 if (member_loc->form_is_section_offset ())
16495 dwarf2_complex_location_expr_complaint ();
16496 else if (member_loc->form_is_constant ()
16497 || member_loc->form_is_block ())
16498 {
16499 if (attr != nullptr)
16500 mark_common_block_symbol_computed (sym, die, attr,
16501 member_loc, cu);
16502 }
16503 else
16504 dwarf2_complex_location_expr_complaint ();
16505 }
16506 }
16507 }
16508
16509 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16510 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16511 }
16512 }
16513
16514 /* Create a type for a C++ namespace. */
16515
16516 static struct type *
16517 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16518 {
16519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16520 const char *previous_prefix, *name;
16521 int is_anonymous;
16522 struct type *type;
16523
16524 /* For extensions, reuse the type of the original namespace. */
16525 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16526 {
16527 struct die_info *ext_die;
16528 struct dwarf2_cu *ext_cu = cu;
16529
16530 ext_die = dwarf2_extension (die, &ext_cu);
16531 type = read_type_die (ext_die, ext_cu);
16532
16533 /* EXT_CU may not be the same as CU.
16534 Ensure TYPE is recorded with CU in die_type_hash. */
16535 return set_die_type (die, type, cu);
16536 }
16537
16538 name = namespace_name (die, &is_anonymous, cu);
16539
16540 /* Now build the name of the current namespace. */
16541
16542 previous_prefix = determine_prefix (die, cu);
16543 if (previous_prefix[0] != '\0')
16544 name = typename_concat (&objfile->objfile_obstack,
16545 previous_prefix, name, 0, cu);
16546
16547 /* Create the type. */
16548 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16549
16550 return set_die_type (die, type, cu);
16551 }
16552
16553 /* Read a namespace scope. */
16554
16555 static void
16556 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16557 {
16558 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16559 int is_anonymous;
16560
16561 /* Add a symbol associated to this if we haven't seen the namespace
16562 before. Also, add a using directive if it's an anonymous
16563 namespace. */
16564
16565 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16566 {
16567 struct type *type;
16568
16569 type = read_type_die (die, cu);
16570 new_symbol (die, type, cu);
16571
16572 namespace_name (die, &is_anonymous, cu);
16573 if (is_anonymous)
16574 {
16575 const char *previous_prefix = determine_prefix (die, cu);
16576
16577 std::vector<const char *> excludes;
16578 add_using_directive (using_directives (cu),
16579 previous_prefix, TYPE_NAME (type), NULL,
16580 NULL, excludes, 0, &objfile->objfile_obstack);
16581 }
16582 }
16583
16584 if (die->child != NULL)
16585 {
16586 struct die_info *child_die = die->child;
16587
16588 while (child_die && child_die->tag)
16589 {
16590 process_die (child_die, cu);
16591 child_die = sibling_die (child_die);
16592 }
16593 }
16594 }
16595
16596 /* Read a Fortran module as type. This DIE can be only a declaration used for
16597 imported module. Still we need that type as local Fortran "use ... only"
16598 declaration imports depend on the created type in determine_prefix. */
16599
16600 static struct type *
16601 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16602 {
16603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16604 const char *module_name;
16605 struct type *type;
16606
16607 module_name = dwarf2_name (die, cu);
16608 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16609
16610 return set_die_type (die, type, cu);
16611 }
16612
16613 /* Read a Fortran module. */
16614
16615 static void
16616 read_module (struct die_info *die, struct dwarf2_cu *cu)
16617 {
16618 struct die_info *child_die = die->child;
16619 struct type *type;
16620
16621 type = read_type_die (die, cu);
16622 new_symbol (die, type, cu);
16623
16624 while (child_die && child_die->tag)
16625 {
16626 process_die (child_die, cu);
16627 child_die = sibling_die (child_die);
16628 }
16629 }
16630
16631 /* Return the name of the namespace represented by DIE. Set
16632 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16633 namespace. */
16634
16635 static const char *
16636 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16637 {
16638 struct die_info *current_die;
16639 const char *name = NULL;
16640
16641 /* Loop through the extensions until we find a name. */
16642
16643 for (current_die = die;
16644 current_die != NULL;
16645 current_die = dwarf2_extension (die, &cu))
16646 {
16647 /* We don't use dwarf2_name here so that we can detect the absence
16648 of a name -> anonymous namespace. */
16649 name = dwarf2_string_attr (die, DW_AT_name, cu);
16650
16651 if (name != NULL)
16652 break;
16653 }
16654
16655 /* Is it an anonymous namespace? */
16656
16657 *is_anonymous = (name == NULL);
16658 if (*is_anonymous)
16659 name = CP_ANONYMOUS_NAMESPACE_STR;
16660
16661 return name;
16662 }
16663
16664 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16665 the user defined type vector. */
16666
16667 static struct type *
16668 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16669 {
16670 struct gdbarch *gdbarch
16671 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16672 struct comp_unit_head *cu_header = &cu->header;
16673 struct type *type;
16674 struct attribute *attr_byte_size;
16675 struct attribute *attr_address_class;
16676 int byte_size, addr_class;
16677 struct type *target_type;
16678
16679 target_type = die_type (die, cu);
16680
16681 /* The die_type call above may have already set the type for this DIE. */
16682 type = get_die_type (die, cu);
16683 if (type)
16684 return type;
16685
16686 type = lookup_pointer_type (target_type);
16687
16688 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16689 if (attr_byte_size)
16690 byte_size = DW_UNSND (attr_byte_size);
16691 else
16692 byte_size = cu_header->addr_size;
16693
16694 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16695 if (attr_address_class)
16696 addr_class = DW_UNSND (attr_address_class);
16697 else
16698 addr_class = DW_ADDR_none;
16699
16700 ULONGEST alignment = get_alignment (cu, die);
16701
16702 /* If the pointer size, alignment, or address class is different
16703 than the default, create a type variant marked as such and set
16704 the length accordingly. */
16705 if (TYPE_LENGTH (type) != byte_size
16706 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16707 && alignment != TYPE_RAW_ALIGN (type))
16708 || addr_class != DW_ADDR_none)
16709 {
16710 if (gdbarch_address_class_type_flags_p (gdbarch))
16711 {
16712 int type_flags;
16713
16714 type_flags = gdbarch_address_class_type_flags
16715 (gdbarch, byte_size, addr_class);
16716 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16717 == 0);
16718 type = make_type_with_address_space (type, type_flags);
16719 }
16720 else if (TYPE_LENGTH (type) != byte_size)
16721 {
16722 complaint (_("invalid pointer size %d"), byte_size);
16723 }
16724 else if (TYPE_RAW_ALIGN (type) != alignment)
16725 {
16726 complaint (_("Invalid DW_AT_alignment"
16727 " - DIE at %s [in module %s]"),
16728 sect_offset_str (die->sect_off),
16729 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16730 }
16731 else
16732 {
16733 /* Should we also complain about unhandled address classes? */
16734 }
16735 }
16736
16737 TYPE_LENGTH (type) = byte_size;
16738 set_type_align (type, alignment);
16739 return set_die_type (die, type, cu);
16740 }
16741
16742 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16743 the user defined type vector. */
16744
16745 static struct type *
16746 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16747 {
16748 struct type *type;
16749 struct type *to_type;
16750 struct type *domain;
16751
16752 to_type = die_type (die, cu);
16753 domain = die_containing_type (die, cu);
16754
16755 /* The calls above may have already set the type for this DIE. */
16756 type = get_die_type (die, cu);
16757 if (type)
16758 return type;
16759
16760 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16761 type = lookup_methodptr_type (to_type);
16762 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16763 {
16764 struct type *new_type
16765 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16766
16767 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16768 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16769 TYPE_VARARGS (to_type));
16770 type = lookup_methodptr_type (new_type);
16771 }
16772 else
16773 type = lookup_memberptr_type (to_type, domain);
16774
16775 return set_die_type (die, type, cu);
16776 }
16777
16778 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16779 the user defined type vector. */
16780
16781 static struct type *
16782 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16783 enum type_code refcode)
16784 {
16785 struct comp_unit_head *cu_header = &cu->header;
16786 struct type *type, *target_type;
16787 struct attribute *attr;
16788
16789 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16790
16791 target_type = die_type (die, cu);
16792
16793 /* The die_type call above may have already set the type for this DIE. */
16794 type = get_die_type (die, cu);
16795 if (type)
16796 return type;
16797
16798 type = lookup_reference_type (target_type, refcode);
16799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16800 if (attr != nullptr)
16801 {
16802 TYPE_LENGTH (type) = DW_UNSND (attr);
16803 }
16804 else
16805 {
16806 TYPE_LENGTH (type) = cu_header->addr_size;
16807 }
16808 maybe_set_alignment (cu, die, type);
16809 return set_die_type (die, type, cu);
16810 }
16811
16812 /* Add the given cv-qualifiers to the element type of the array. GCC
16813 outputs DWARF type qualifiers that apply to an array, not the
16814 element type. But GDB relies on the array element type to carry
16815 the cv-qualifiers. This mimics section 6.7.3 of the C99
16816 specification. */
16817
16818 static struct type *
16819 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16820 struct type *base_type, int cnst, int voltl)
16821 {
16822 struct type *el_type, *inner_array;
16823
16824 base_type = copy_type (base_type);
16825 inner_array = base_type;
16826
16827 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16828 {
16829 TYPE_TARGET_TYPE (inner_array) =
16830 copy_type (TYPE_TARGET_TYPE (inner_array));
16831 inner_array = TYPE_TARGET_TYPE (inner_array);
16832 }
16833
16834 el_type = TYPE_TARGET_TYPE (inner_array);
16835 cnst |= TYPE_CONST (el_type);
16836 voltl |= TYPE_VOLATILE (el_type);
16837 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16838
16839 return set_die_type (die, base_type, cu);
16840 }
16841
16842 static struct type *
16843 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16844 {
16845 struct type *base_type, *cv_type;
16846
16847 base_type = die_type (die, cu);
16848
16849 /* The die_type call above may have already set the type for this DIE. */
16850 cv_type = get_die_type (die, cu);
16851 if (cv_type)
16852 return cv_type;
16853
16854 /* In case the const qualifier is applied to an array type, the element type
16855 is so qualified, not the array type (section 6.7.3 of C99). */
16856 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16857 return add_array_cv_type (die, cu, base_type, 1, 0);
16858
16859 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16860 return set_die_type (die, cv_type, cu);
16861 }
16862
16863 static struct type *
16864 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16865 {
16866 struct type *base_type, *cv_type;
16867
16868 base_type = die_type (die, cu);
16869
16870 /* The die_type call above may have already set the type for this DIE. */
16871 cv_type = get_die_type (die, cu);
16872 if (cv_type)
16873 return cv_type;
16874
16875 /* In case the volatile qualifier is applied to an array type, the
16876 element type is so qualified, not the array type (section 6.7.3
16877 of C99). */
16878 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16879 return add_array_cv_type (die, cu, base_type, 0, 1);
16880
16881 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16882 return set_die_type (die, cv_type, cu);
16883 }
16884
16885 /* Handle DW_TAG_restrict_type. */
16886
16887 static struct type *
16888 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16889 {
16890 struct type *base_type, *cv_type;
16891
16892 base_type = die_type (die, cu);
16893
16894 /* The die_type call above may have already set the type for this DIE. */
16895 cv_type = get_die_type (die, cu);
16896 if (cv_type)
16897 return cv_type;
16898
16899 cv_type = make_restrict_type (base_type);
16900 return set_die_type (die, cv_type, cu);
16901 }
16902
16903 /* Handle DW_TAG_atomic_type. */
16904
16905 static struct type *
16906 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16907 {
16908 struct type *base_type, *cv_type;
16909
16910 base_type = die_type (die, cu);
16911
16912 /* The die_type call above may have already set the type for this DIE. */
16913 cv_type = get_die_type (die, cu);
16914 if (cv_type)
16915 return cv_type;
16916
16917 cv_type = make_atomic_type (base_type);
16918 return set_die_type (die, cv_type, cu);
16919 }
16920
16921 /* Extract all information from a DW_TAG_string_type DIE and add to
16922 the user defined type vector. It isn't really a user defined type,
16923 but it behaves like one, with other DIE's using an AT_user_def_type
16924 attribute to reference it. */
16925
16926 static struct type *
16927 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16928 {
16929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16931 struct type *type, *range_type, *index_type, *char_type;
16932 struct attribute *attr;
16933 struct dynamic_prop prop;
16934 bool length_is_constant = true;
16935 LONGEST length;
16936
16937 /* There are a couple of places where bit sizes might be made use of
16938 when parsing a DW_TAG_string_type, however, no producer that we know
16939 of make use of these. Handling bit sizes that are a multiple of the
16940 byte size is easy enough, but what about other bit sizes? Lets deal
16941 with that problem when we have to. Warn about these attributes being
16942 unsupported, then parse the type and ignore them like we always
16943 have. */
16944 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16945 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16946 {
16947 static bool warning_printed = false;
16948 if (!warning_printed)
16949 {
16950 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16951 "currently supported on DW_TAG_string_type."));
16952 warning_printed = true;
16953 }
16954 }
16955
16956 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16957 if (attr != nullptr && !attr->form_is_constant ())
16958 {
16959 /* The string length describes the location at which the length of
16960 the string can be found. The size of the length field can be
16961 specified with one of the attributes below. */
16962 struct type *prop_type;
16963 struct attribute *len
16964 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16965 if (len == nullptr)
16966 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16967 if (len != nullptr && len->form_is_constant ())
16968 {
16969 /* Pass 0 as the default as we know this attribute is constant
16970 and the default value will not be returned. */
16971 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16972 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
16973 }
16974 else
16975 {
16976 /* If the size is not specified then we assume it is the size of
16977 an address on this target. */
16978 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
16979 }
16980
16981 /* Convert the attribute into a dynamic property. */
16982 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16983 length = 1;
16984 else
16985 length_is_constant = false;
16986 }
16987 else if (attr != nullptr)
16988 {
16989 /* This DW_AT_string_length just contains the length with no
16990 indirection. There's no need to create a dynamic property in this
16991 case. Pass 0 for the default value as we know it will not be
16992 returned in this case. */
16993 length = dwarf2_get_attr_constant_value (attr, 0);
16994 }
16995 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16996 {
16997 /* We don't currently support non-constant byte sizes for strings. */
16998 length = dwarf2_get_attr_constant_value (attr, 1);
16999 }
17000 else
17001 {
17002 /* Use 1 as a fallback length if we have nothing else. */
17003 length = 1;
17004 }
17005
17006 index_type = objfile_type (objfile)->builtin_int;
17007 if (length_is_constant)
17008 range_type = create_static_range_type (NULL, index_type, 1, length);
17009 else
17010 {
17011 struct dynamic_prop low_bound;
17012
17013 low_bound.kind = PROP_CONST;
17014 low_bound.data.const_val = 1;
17015 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17016 }
17017 char_type = language_string_char_type (cu->language_defn, gdbarch);
17018 type = create_string_type (NULL, char_type, range_type);
17019
17020 return set_die_type (die, type, cu);
17021 }
17022
17023 /* Assuming that DIE corresponds to a function, returns nonzero
17024 if the function is prototyped. */
17025
17026 static int
17027 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17028 {
17029 struct attribute *attr;
17030
17031 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17032 if (attr && (DW_UNSND (attr) != 0))
17033 return 1;
17034
17035 /* The DWARF standard implies that the DW_AT_prototyped attribute
17036 is only meaningful for C, but the concept also extends to other
17037 languages that allow unprototyped functions (Eg: Objective C).
17038 For all other languages, assume that functions are always
17039 prototyped. */
17040 if (cu->language != language_c
17041 && cu->language != language_objc
17042 && cu->language != language_opencl)
17043 return 1;
17044
17045 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17046 prototyped and unprototyped functions; default to prototyped,
17047 since that is more common in modern code (and RealView warns
17048 about unprototyped functions). */
17049 if (producer_is_realview (cu->producer))
17050 return 1;
17051
17052 return 0;
17053 }
17054
17055 /* Handle DIES due to C code like:
17056
17057 struct foo
17058 {
17059 int (*funcp)(int a, long l);
17060 int b;
17061 };
17062
17063 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17064
17065 static struct type *
17066 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17067 {
17068 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17069 struct type *type; /* Type that this function returns. */
17070 struct type *ftype; /* Function that returns above type. */
17071 struct attribute *attr;
17072
17073 type = die_type (die, cu);
17074
17075 /* The die_type call above may have already set the type for this DIE. */
17076 ftype = get_die_type (die, cu);
17077 if (ftype)
17078 return ftype;
17079
17080 ftype = lookup_function_type (type);
17081
17082 if (prototyped_function_p (die, cu))
17083 TYPE_PROTOTYPED (ftype) = 1;
17084
17085 /* Store the calling convention in the type if it's available in
17086 the subroutine die. Otherwise set the calling convention to
17087 the default value DW_CC_normal. */
17088 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17089 if (attr != nullptr
17090 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17091 TYPE_CALLING_CONVENTION (ftype)
17092 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17093 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17094 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17095 else
17096 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17097
17098 /* Record whether the function returns normally to its caller or not
17099 if the DWARF producer set that information. */
17100 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17101 if (attr && (DW_UNSND (attr) != 0))
17102 TYPE_NO_RETURN (ftype) = 1;
17103
17104 /* We need to add the subroutine type to the die immediately so
17105 we don't infinitely recurse when dealing with parameters
17106 declared as the same subroutine type. */
17107 set_die_type (die, ftype, cu);
17108
17109 if (die->child != NULL)
17110 {
17111 struct type *void_type = objfile_type (objfile)->builtin_void;
17112 struct die_info *child_die;
17113 int nparams, iparams;
17114
17115 /* Count the number of parameters.
17116 FIXME: GDB currently ignores vararg functions, but knows about
17117 vararg member functions. */
17118 nparams = 0;
17119 child_die = die->child;
17120 while (child_die && child_die->tag)
17121 {
17122 if (child_die->tag == DW_TAG_formal_parameter)
17123 nparams++;
17124 else if (child_die->tag == DW_TAG_unspecified_parameters)
17125 TYPE_VARARGS (ftype) = 1;
17126 child_die = sibling_die (child_die);
17127 }
17128
17129 /* Allocate storage for parameters and fill them in. */
17130 TYPE_NFIELDS (ftype) = nparams;
17131 TYPE_FIELDS (ftype) = (struct field *)
17132 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17133
17134 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17135 even if we error out during the parameters reading below. */
17136 for (iparams = 0; iparams < nparams; iparams++)
17137 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17138
17139 iparams = 0;
17140 child_die = die->child;
17141 while (child_die && child_die->tag)
17142 {
17143 if (child_die->tag == DW_TAG_formal_parameter)
17144 {
17145 struct type *arg_type;
17146
17147 /* DWARF version 2 has no clean way to discern C++
17148 static and non-static member functions. G++ helps
17149 GDB by marking the first parameter for non-static
17150 member functions (which is the this pointer) as
17151 artificial. We pass this information to
17152 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17153
17154 DWARF version 3 added DW_AT_object_pointer, which GCC
17155 4.5 does not yet generate. */
17156 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17157 if (attr != nullptr)
17158 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17159 else
17160 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17161 arg_type = die_type (child_die, cu);
17162
17163 /* RealView does not mark THIS as const, which the testsuite
17164 expects. GCC marks THIS as const in method definitions,
17165 but not in the class specifications (GCC PR 43053). */
17166 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17167 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17168 {
17169 int is_this = 0;
17170 struct dwarf2_cu *arg_cu = cu;
17171 const char *name = dwarf2_name (child_die, cu);
17172
17173 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17174 if (attr != nullptr)
17175 {
17176 /* If the compiler emits this, use it. */
17177 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17178 is_this = 1;
17179 }
17180 else if (name && strcmp (name, "this") == 0)
17181 /* Function definitions will have the argument names. */
17182 is_this = 1;
17183 else if (name == NULL && iparams == 0)
17184 /* Declarations may not have the names, so like
17185 elsewhere in GDB, assume an artificial first
17186 argument is "this". */
17187 is_this = 1;
17188
17189 if (is_this)
17190 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17191 arg_type, 0);
17192 }
17193
17194 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17195 iparams++;
17196 }
17197 child_die = sibling_die (child_die);
17198 }
17199 }
17200
17201 return ftype;
17202 }
17203
17204 static struct type *
17205 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17206 {
17207 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17208 const char *name = NULL;
17209 struct type *this_type, *target_type;
17210
17211 name = dwarf2_full_name (NULL, die, cu);
17212 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17213 TYPE_TARGET_STUB (this_type) = 1;
17214 set_die_type (die, this_type, cu);
17215 target_type = die_type (die, cu);
17216 if (target_type != this_type)
17217 TYPE_TARGET_TYPE (this_type) = target_type;
17218 else
17219 {
17220 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17221 spec and cause infinite loops in GDB. */
17222 complaint (_("Self-referential DW_TAG_typedef "
17223 "- DIE at %s [in module %s]"),
17224 sect_offset_str (die->sect_off), objfile_name (objfile));
17225 TYPE_TARGET_TYPE (this_type) = NULL;
17226 }
17227 return this_type;
17228 }
17229
17230 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17231 (which may be different from NAME) to the architecture back-end to allow
17232 it to guess the correct format if necessary. */
17233
17234 static struct type *
17235 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17236 const char *name_hint, enum bfd_endian byte_order)
17237 {
17238 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17239 const struct floatformat **format;
17240 struct type *type;
17241
17242 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17243 if (format)
17244 type = init_float_type (objfile, bits, name, format, byte_order);
17245 else
17246 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17247
17248 return type;
17249 }
17250
17251 /* Allocate an integer type of size BITS and name NAME. */
17252
17253 static struct type *
17254 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17255 int bits, int unsigned_p, const char *name)
17256 {
17257 struct type *type;
17258
17259 /* Versions of Intel's C Compiler generate an integer type called "void"
17260 instead of using DW_TAG_unspecified_type. This has been seen on
17261 at least versions 14, 17, and 18. */
17262 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17263 && strcmp (name, "void") == 0)
17264 type = objfile_type (objfile)->builtin_void;
17265 else
17266 type = init_integer_type (objfile, bits, unsigned_p, name);
17267
17268 return type;
17269 }
17270
17271 /* Initialise and return a floating point type of size BITS suitable for
17272 use as a component of a complex number. The NAME_HINT is passed through
17273 when initialising the floating point type and is the name of the complex
17274 type.
17275
17276 As DWARF doesn't currently provide an explicit name for the components
17277 of a complex number, but it can be helpful to have these components
17278 named, we try to select a suitable name based on the size of the
17279 component. */
17280 static struct type *
17281 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17282 struct objfile *objfile,
17283 int bits, const char *name_hint,
17284 enum bfd_endian byte_order)
17285 {
17286 gdbarch *gdbarch = get_objfile_arch (objfile);
17287 struct type *tt = nullptr;
17288
17289 /* Try to find a suitable floating point builtin type of size BITS.
17290 We're going to use the name of this type as the name for the complex
17291 target type that we are about to create. */
17292 switch (cu->language)
17293 {
17294 case language_fortran:
17295 switch (bits)
17296 {
17297 case 32:
17298 tt = builtin_f_type (gdbarch)->builtin_real;
17299 break;
17300 case 64:
17301 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17302 break;
17303 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17304 case 128:
17305 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17306 break;
17307 }
17308 break;
17309 default:
17310 switch (bits)
17311 {
17312 case 32:
17313 tt = builtin_type (gdbarch)->builtin_float;
17314 break;
17315 case 64:
17316 tt = builtin_type (gdbarch)->builtin_double;
17317 break;
17318 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17319 case 128:
17320 tt = builtin_type (gdbarch)->builtin_long_double;
17321 break;
17322 }
17323 break;
17324 }
17325
17326 /* If the type we found doesn't match the size we were looking for, then
17327 pretend we didn't find a type at all, the complex target type we
17328 create will then be nameless. */
17329 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17330 tt = nullptr;
17331
17332 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17333 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17334 }
17335
17336 /* Find a representation of a given base type and install
17337 it in the TYPE field of the die. */
17338
17339 static struct type *
17340 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17341 {
17342 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17343 struct type *type;
17344 struct attribute *attr;
17345 int encoding = 0, bits = 0;
17346 const char *name;
17347 gdbarch *arch;
17348
17349 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17350 if (attr != nullptr)
17351 encoding = DW_UNSND (attr);
17352 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17353 if (attr != nullptr)
17354 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17355 name = dwarf2_name (die, cu);
17356 if (!name)
17357 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17358
17359 arch = get_objfile_arch (objfile);
17360 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17361
17362 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17363 if (attr)
17364 {
17365 int endianity = DW_UNSND (attr);
17366
17367 switch (endianity)
17368 {
17369 case DW_END_big:
17370 byte_order = BFD_ENDIAN_BIG;
17371 break;
17372 case DW_END_little:
17373 byte_order = BFD_ENDIAN_LITTLE;
17374 break;
17375 default:
17376 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17377 break;
17378 }
17379 }
17380
17381 switch (encoding)
17382 {
17383 case DW_ATE_address:
17384 /* Turn DW_ATE_address into a void * pointer. */
17385 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17386 type = init_pointer_type (objfile, bits, name, type);
17387 break;
17388 case DW_ATE_boolean:
17389 type = init_boolean_type (objfile, bits, 1, name);
17390 break;
17391 case DW_ATE_complex_float:
17392 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17393 byte_order);
17394 type = init_complex_type (objfile, name, type);
17395 break;
17396 case DW_ATE_decimal_float:
17397 type = init_decfloat_type (objfile, bits, name);
17398 break;
17399 case DW_ATE_float:
17400 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17401 break;
17402 case DW_ATE_signed:
17403 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17404 break;
17405 case DW_ATE_unsigned:
17406 if (cu->language == language_fortran
17407 && name
17408 && startswith (name, "character("))
17409 type = init_character_type (objfile, bits, 1, name);
17410 else
17411 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17412 break;
17413 case DW_ATE_signed_char:
17414 if (cu->language == language_ada || cu->language == language_m2
17415 || cu->language == language_pascal
17416 || cu->language == language_fortran)
17417 type = init_character_type (objfile, bits, 0, name);
17418 else
17419 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17420 break;
17421 case DW_ATE_unsigned_char:
17422 if (cu->language == language_ada || cu->language == language_m2
17423 || cu->language == language_pascal
17424 || cu->language == language_fortran
17425 || cu->language == language_rust)
17426 type = init_character_type (objfile, bits, 1, name);
17427 else
17428 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17429 break;
17430 case DW_ATE_UTF:
17431 {
17432 if (bits == 16)
17433 type = builtin_type (arch)->builtin_char16;
17434 else if (bits == 32)
17435 type = builtin_type (arch)->builtin_char32;
17436 else
17437 {
17438 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17439 bits);
17440 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17441 }
17442 return set_die_type (die, type, cu);
17443 }
17444 break;
17445
17446 default:
17447 complaint (_("unsupported DW_AT_encoding: '%s'"),
17448 dwarf_type_encoding_name (encoding));
17449 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17450 break;
17451 }
17452
17453 if (name && strcmp (name, "char") == 0)
17454 TYPE_NOSIGN (type) = 1;
17455
17456 maybe_set_alignment (cu, die, type);
17457
17458 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17459
17460 return set_die_type (die, type, cu);
17461 }
17462
17463 /* Parse dwarf attribute if it's a block, reference or constant and put the
17464 resulting value of the attribute into struct bound_prop.
17465 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17466
17467 static int
17468 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17469 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17470 struct type *default_type)
17471 {
17472 struct dwarf2_property_baton *baton;
17473 struct obstack *obstack
17474 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17475
17476 gdb_assert (default_type != NULL);
17477
17478 if (attr == NULL || prop == NULL)
17479 return 0;
17480
17481 if (attr->form_is_block ())
17482 {
17483 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17484 baton->property_type = default_type;
17485 baton->locexpr.per_cu = cu->per_cu;
17486 baton->locexpr.size = DW_BLOCK (attr)->size;
17487 baton->locexpr.data = DW_BLOCK (attr)->data;
17488 switch (attr->name)
17489 {
17490 case DW_AT_string_length:
17491 baton->locexpr.is_reference = true;
17492 break;
17493 default:
17494 baton->locexpr.is_reference = false;
17495 break;
17496 }
17497 prop->data.baton = baton;
17498 prop->kind = PROP_LOCEXPR;
17499 gdb_assert (prop->data.baton != NULL);
17500 }
17501 else if (attr->form_is_ref ())
17502 {
17503 struct dwarf2_cu *target_cu = cu;
17504 struct die_info *target_die;
17505 struct attribute *target_attr;
17506
17507 target_die = follow_die_ref (die, attr, &target_cu);
17508 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17509 if (target_attr == NULL)
17510 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17511 target_cu);
17512 if (target_attr == NULL)
17513 return 0;
17514
17515 switch (target_attr->name)
17516 {
17517 case DW_AT_location:
17518 if (target_attr->form_is_section_offset ())
17519 {
17520 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17521 baton->property_type = die_type (target_die, target_cu);
17522 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17523 prop->data.baton = baton;
17524 prop->kind = PROP_LOCLIST;
17525 gdb_assert (prop->data.baton != NULL);
17526 }
17527 else if (target_attr->form_is_block ())
17528 {
17529 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17530 baton->property_type = die_type (target_die, target_cu);
17531 baton->locexpr.per_cu = cu->per_cu;
17532 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17533 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17534 baton->locexpr.is_reference = true;
17535 prop->data.baton = baton;
17536 prop->kind = PROP_LOCEXPR;
17537 gdb_assert (prop->data.baton != NULL);
17538 }
17539 else
17540 {
17541 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17542 "dynamic property");
17543 return 0;
17544 }
17545 break;
17546 case DW_AT_data_member_location:
17547 {
17548 LONGEST offset;
17549
17550 if (!handle_data_member_location (target_die, target_cu,
17551 &offset))
17552 return 0;
17553
17554 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17555 baton->property_type = read_type_die (target_die->parent,
17556 target_cu);
17557 baton->offset_info.offset = offset;
17558 baton->offset_info.type = die_type (target_die, target_cu);
17559 prop->data.baton = baton;
17560 prop->kind = PROP_ADDR_OFFSET;
17561 break;
17562 }
17563 }
17564 }
17565 else if (attr->form_is_constant ())
17566 {
17567 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17568 prop->kind = PROP_CONST;
17569 }
17570 else
17571 {
17572 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17573 dwarf2_name (die, cu));
17574 return 0;
17575 }
17576
17577 return 1;
17578 }
17579
17580 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17581 UNSIGNED_P controls if the integer is unsigned or not. */
17582
17583 static struct type *
17584 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17585 int size_in_bytes, bool unsigned_p)
17586 {
17587 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17588 struct type *int_type;
17589
17590 /* Helper macro to examine the various builtin types. */
17591 #define TRY_TYPE(F) \
17592 int_type = (unsigned_p \
17593 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17594 : objfile_type (objfile)->builtin_ ## F); \
17595 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17596 return int_type
17597
17598 TRY_TYPE (char);
17599 TRY_TYPE (short);
17600 TRY_TYPE (int);
17601 TRY_TYPE (long);
17602 TRY_TYPE (long_long);
17603
17604 #undef TRY_TYPE
17605
17606 gdb_assert_not_reached ("unable to find suitable integer type");
17607 }
17608
17609 /* Find an integer type the same size as the address size given in the
17610 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17611 is unsigned or not. */
17612
17613 static struct type *
17614 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17615 bool unsigned_p)
17616 {
17617 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17618 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17619 }
17620
17621 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17622 present (which is valid) then compute the default type based on the
17623 compilation units address size. */
17624
17625 static struct type *
17626 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17627 {
17628 struct type *index_type = die_type (die, cu);
17629
17630 /* Dwarf-2 specifications explicitly allows to create subrange types
17631 without specifying a base type.
17632 In that case, the base type must be set to the type of
17633 the lower bound, upper bound or count, in that order, if any of these
17634 three attributes references an object that has a type.
17635 If no base type is found, the Dwarf-2 specifications say that
17636 a signed integer type of size equal to the size of an address should
17637 be used.
17638 For the following C code: `extern char gdb_int [];'
17639 GCC produces an empty range DIE.
17640 FIXME: muller/2010-05-28: Possible references to object for low bound,
17641 high bound or count are not yet handled by this code. */
17642 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17643 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17644
17645 return index_type;
17646 }
17647
17648 /* Read the given DW_AT_subrange DIE. */
17649
17650 static struct type *
17651 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17652 {
17653 struct type *base_type, *orig_base_type;
17654 struct type *range_type;
17655 struct attribute *attr;
17656 struct dynamic_prop low, high;
17657 int low_default_is_valid;
17658 int high_bound_is_count = 0;
17659 const char *name;
17660 ULONGEST negative_mask;
17661
17662 orig_base_type = read_subrange_index_type (die, cu);
17663
17664 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17665 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17666 creating the range type, but we use the result of check_typedef
17667 when examining properties of the type. */
17668 base_type = check_typedef (orig_base_type);
17669
17670 /* The die_type call above may have already set the type for this DIE. */
17671 range_type = get_die_type (die, cu);
17672 if (range_type)
17673 return range_type;
17674
17675 low.kind = PROP_CONST;
17676 high.kind = PROP_CONST;
17677 high.data.const_val = 0;
17678
17679 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17680 omitting DW_AT_lower_bound. */
17681 switch (cu->language)
17682 {
17683 case language_c:
17684 case language_cplus:
17685 low.data.const_val = 0;
17686 low_default_is_valid = 1;
17687 break;
17688 case language_fortran:
17689 low.data.const_val = 1;
17690 low_default_is_valid = 1;
17691 break;
17692 case language_d:
17693 case language_objc:
17694 case language_rust:
17695 low.data.const_val = 0;
17696 low_default_is_valid = (cu->header.version >= 4);
17697 break;
17698 case language_ada:
17699 case language_m2:
17700 case language_pascal:
17701 low.data.const_val = 1;
17702 low_default_is_valid = (cu->header.version >= 4);
17703 break;
17704 default:
17705 low.data.const_val = 0;
17706 low_default_is_valid = 0;
17707 break;
17708 }
17709
17710 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17711 if (attr != nullptr)
17712 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17713 else if (!low_default_is_valid)
17714 complaint (_("Missing DW_AT_lower_bound "
17715 "- DIE at %s [in module %s]"),
17716 sect_offset_str (die->sect_off),
17717 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17718
17719 struct attribute *attr_ub, *attr_count;
17720 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17721 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17722 {
17723 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17724 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17725 {
17726 /* If bounds are constant do the final calculation here. */
17727 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17728 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17729 else
17730 high_bound_is_count = 1;
17731 }
17732 else
17733 {
17734 if (attr_ub != NULL)
17735 complaint (_("Unresolved DW_AT_upper_bound "
17736 "- DIE at %s [in module %s]"),
17737 sect_offset_str (die->sect_off),
17738 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17739 if (attr_count != NULL)
17740 complaint (_("Unresolved DW_AT_count "
17741 "- DIE at %s [in module %s]"),
17742 sect_offset_str (die->sect_off),
17743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17744 }
17745 }
17746
17747 LONGEST bias = 0;
17748 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17749 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17750 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17751
17752 /* Normally, the DWARF producers are expected to use a signed
17753 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17754 But this is unfortunately not always the case, as witnessed
17755 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17756 is used instead. To work around that ambiguity, we treat
17757 the bounds as signed, and thus sign-extend their values, when
17758 the base type is signed. */
17759 negative_mask =
17760 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17761 if (low.kind == PROP_CONST
17762 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17763 low.data.const_val |= negative_mask;
17764 if (high.kind == PROP_CONST
17765 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17766 high.data.const_val |= negative_mask;
17767
17768 /* Check for bit and byte strides. */
17769 struct dynamic_prop byte_stride_prop;
17770 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17771 if (attr_byte_stride != nullptr)
17772 {
17773 struct type *prop_type
17774 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17775 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17776 prop_type);
17777 }
17778
17779 struct dynamic_prop bit_stride_prop;
17780 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17781 if (attr_bit_stride != nullptr)
17782 {
17783 /* It only makes sense to have either a bit or byte stride. */
17784 if (attr_byte_stride != nullptr)
17785 {
17786 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17787 "- DIE at %s [in module %s]"),
17788 sect_offset_str (die->sect_off),
17789 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17790 attr_bit_stride = nullptr;
17791 }
17792 else
17793 {
17794 struct type *prop_type
17795 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17796 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17797 prop_type);
17798 }
17799 }
17800
17801 if (attr_byte_stride != nullptr
17802 || attr_bit_stride != nullptr)
17803 {
17804 bool byte_stride_p = (attr_byte_stride != nullptr);
17805 struct dynamic_prop *stride
17806 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17807
17808 range_type
17809 = create_range_type_with_stride (NULL, orig_base_type, &low,
17810 &high, bias, stride, byte_stride_p);
17811 }
17812 else
17813 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17814
17815 if (high_bound_is_count)
17816 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17817
17818 /* Ada expects an empty array on no boundary attributes. */
17819 if (attr == NULL && cu->language != language_ada)
17820 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17821
17822 name = dwarf2_name (die, cu);
17823 if (name)
17824 TYPE_NAME (range_type) = name;
17825
17826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17827 if (attr != nullptr)
17828 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17829
17830 maybe_set_alignment (cu, die, range_type);
17831
17832 set_die_type (die, range_type, cu);
17833
17834 /* set_die_type should be already done. */
17835 set_descriptive_type (range_type, die, cu);
17836
17837 return range_type;
17838 }
17839
17840 static struct type *
17841 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17842 {
17843 struct type *type;
17844
17845 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17846 NULL);
17847 TYPE_NAME (type) = dwarf2_name (die, cu);
17848
17849 /* In Ada, an unspecified type is typically used when the description
17850 of the type is deferred to a different unit. When encountering
17851 such a type, we treat it as a stub, and try to resolve it later on,
17852 when needed. */
17853 if (cu->language == language_ada)
17854 TYPE_STUB (type) = 1;
17855
17856 return set_die_type (die, type, cu);
17857 }
17858
17859 /* Read a single die and all its descendents. Set the die's sibling
17860 field to NULL; set other fields in the die correctly, and set all
17861 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17862 location of the info_ptr after reading all of those dies. PARENT
17863 is the parent of the die in question. */
17864
17865 static struct die_info *
17866 read_die_and_children (const struct die_reader_specs *reader,
17867 const gdb_byte *info_ptr,
17868 const gdb_byte **new_info_ptr,
17869 struct die_info *parent)
17870 {
17871 struct die_info *die;
17872 const gdb_byte *cur_ptr;
17873
17874 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17875 if (die == NULL)
17876 {
17877 *new_info_ptr = cur_ptr;
17878 return NULL;
17879 }
17880 store_in_ref_table (die, reader->cu);
17881
17882 if (die->has_children)
17883 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17884 else
17885 {
17886 die->child = NULL;
17887 *new_info_ptr = cur_ptr;
17888 }
17889
17890 die->sibling = NULL;
17891 die->parent = parent;
17892 return die;
17893 }
17894
17895 /* Read a die, all of its descendents, and all of its siblings; set
17896 all of the fields of all of the dies correctly. Arguments are as
17897 in read_die_and_children. */
17898
17899 static struct die_info *
17900 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17901 const gdb_byte *info_ptr,
17902 const gdb_byte **new_info_ptr,
17903 struct die_info *parent)
17904 {
17905 struct die_info *first_die, *last_sibling;
17906 const gdb_byte *cur_ptr;
17907
17908 cur_ptr = info_ptr;
17909 first_die = last_sibling = NULL;
17910
17911 while (1)
17912 {
17913 struct die_info *die
17914 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17915
17916 if (die == NULL)
17917 {
17918 *new_info_ptr = cur_ptr;
17919 return first_die;
17920 }
17921
17922 if (!first_die)
17923 first_die = die;
17924 else
17925 last_sibling->sibling = die;
17926
17927 last_sibling = die;
17928 }
17929 }
17930
17931 /* Read a die, all of its descendents, and all of its siblings; set
17932 all of the fields of all of the dies correctly. Arguments are as
17933 in read_die_and_children.
17934 This the main entry point for reading a DIE and all its children. */
17935
17936 static struct die_info *
17937 read_die_and_siblings (const struct die_reader_specs *reader,
17938 const gdb_byte *info_ptr,
17939 const gdb_byte **new_info_ptr,
17940 struct die_info *parent)
17941 {
17942 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17943 new_info_ptr, parent);
17944
17945 if (dwarf_die_debug)
17946 {
17947 fprintf_unfiltered (gdb_stdlog,
17948 "Read die from %s@0x%x of %s:\n",
17949 reader->die_section->get_name (),
17950 (unsigned) (info_ptr - reader->die_section->buffer),
17951 bfd_get_filename (reader->abfd));
17952 dump_die (die, dwarf_die_debug);
17953 }
17954
17955 return die;
17956 }
17957
17958 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17959 attributes.
17960 The caller is responsible for filling in the extra attributes
17961 and updating (*DIEP)->num_attrs.
17962 Set DIEP to point to a newly allocated die with its information,
17963 except for its child, sibling, and parent fields. */
17964
17965 static const gdb_byte *
17966 read_full_die_1 (const struct die_reader_specs *reader,
17967 struct die_info **diep, const gdb_byte *info_ptr,
17968 int num_extra_attrs)
17969 {
17970 unsigned int abbrev_number, bytes_read, i;
17971 struct abbrev_info *abbrev;
17972 struct die_info *die;
17973 struct dwarf2_cu *cu = reader->cu;
17974 bfd *abfd = reader->abfd;
17975
17976 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17977 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17978 info_ptr += bytes_read;
17979 if (!abbrev_number)
17980 {
17981 *diep = NULL;
17982 return info_ptr;
17983 }
17984
17985 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17986 if (!abbrev)
17987 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17988 abbrev_number,
17989 bfd_get_filename (abfd));
17990
17991 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17992 die->sect_off = sect_off;
17993 die->tag = abbrev->tag;
17994 die->abbrev = abbrev_number;
17995 die->has_children = abbrev->has_children;
17996
17997 /* Make the result usable.
17998 The caller needs to update num_attrs after adding the extra
17999 attributes. */
18000 die->num_attrs = abbrev->num_attrs;
18001
18002 std::vector<int> indexes_that_need_reprocess;
18003 for (i = 0; i < abbrev->num_attrs; ++i)
18004 {
18005 bool need_reprocess;
18006 info_ptr =
18007 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18008 info_ptr, &need_reprocess);
18009 if (need_reprocess)
18010 indexes_that_need_reprocess.push_back (i);
18011 }
18012
18013 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18014 if (attr != nullptr)
18015 cu->str_offsets_base = DW_UNSND (attr);
18016
18017 auto maybe_addr_base = lookup_addr_base(die);
18018 if (maybe_addr_base.has_value ())
18019 cu->addr_base = *maybe_addr_base;
18020 for (int index : indexes_that_need_reprocess)
18021 read_attribute_reprocess (reader, &die->attrs[index]);
18022 *diep = die;
18023 return info_ptr;
18024 }
18025
18026 /* Read a die and all its attributes.
18027 Set DIEP to point to a newly allocated die with its information,
18028 except for its child, sibling, and parent fields. */
18029
18030 static const gdb_byte *
18031 read_full_die (const struct die_reader_specs *reader,
18032 struct die_info **diep, const gdb_byte *info_ptr)
18033 {
18034 const gdb_byte *result;
18035
18036 result = read_full_die_1 (reader, diep, info_ptr, 0);
18037
18038 if (dwarf_die_debug)
18039 {
18040 fprintf_unfiltered (gdb_stdlog,
18041 "Read die from %s@0x%x of %s:\n",
18042 reader->die_section->get_name (),
18043 (unsigned) (info_ptr - reader->die_section->buffer),
18044 bfd_get_filename (reader->abfd));
18045 dump_die (*diep, dwarf_die_debug);
18046 }
18047
18048 return result;
18049 }
18050 \f
18051
18052 /* Returns nonzero if TAG represents a type that we might generate a partial
18053 symbol for. */
18054
18055 static int
18056 is_type_tag_for_partial (int tag)
18057 {
18058 switch (tag)
18059 {
18060 #if 0
18061 /* Some types that would be reasonable to generate partial symbols for,
18062 that we don't at present. */
18063 case DW_TAG_array_type:
18064 case DW_TAG_file_type:
18065 case DW_TAG_ptr_to_member_type:
18066 case DW_TAG_set_type:
18067 case DW_TAG_string_type:
18068 case DW_TAG_subroutine_type:
18069 #endif
18070 case DW_TAG_base_type:
18071 case DW_TAG_class_type:
18072 case DW_TAG_interface_type:
18073 case DW_TAG_enumeration_type:
18074 case DW_TAG_structure_type:
18075 case DW_TAG_subrange_type:
18076 case DW_TAG_typedef:
18077 case DW_TAG_union_type:
18078 return 1;
18079 default:
18080 return 0;
18081 }
18082 }
18083
18084 /* Load all DIEs that are interesting for partial symbols into memory. */
18085
18086 static struct partial_die_info *
18087 load_partial_dies (const struct die_reader_specs *reader,
18088 const gdb_byte *info_ptr, int building_psymtab)
18089 {
18090 struct dwarf2_cu *cu = reader->cu;
18091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18092 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18093 unsigned int bytes_read;
18094 unsigned int load_all = 0;
18095 int nesting_level = 1;
18096
18097 parent_die = NULL;
18098 last_die = NULL;
18099
18100 gdb_assert (cu->per_cu != NULL);
18101 if (cu->per_cu->load_all_dies)
18102 load_all = 1;
18103
18104 cu->partial_dies
18105 = htab_create_alloc_ex (cu->header.length / 12,
18106 partial_die_hash,
18107 partial_die_eq,
18108 NULL,
18109 &cu->comp_unit_obstack,
18110 hashtab_obstack_allocate,
18111 dummy_obstack_deallocate);
18112
18113 while (1)
18114 {
18115 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18116
18117 /* A NULL abbrev means the end of a series of children. */
18118 if (abbrev == NULL)
18119 {
18120 if (--nesting_level == 0)
18121 return first_die;
18122
18123 info_ptr += bytes_read;
18124 last_die = parent_die;
18125 parent_die = parent_die->die_parent;
18126 continue;
18127 }
18128
18129 /* Check for template arguments. We never save these; if
18130 they're seen, we just mark the parent, and go on our way. */
18131 if (parent_die != NULL
18132 && cu->language == language_cplus
18133 && (abbrev->tag == DW_TAG_template_type_param
18134 || abbrev->tag == DW_TAG_template_value_param))
18135 {
18136 parent_die->has_template_arguments = 1;
18137
18138 if (!load_all)
18139 {
18140 /* We don't need a partial DIE for the template argument. */
18141 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18142 continue;
18143 }
18144 }
18145
18146 /* We only recurse into c++ subprograms looking for template arguments.
18147 Skip their other children. */
18148 if (!load_all
18149 && cu->language == language_cplus
18150 && parent_die != NULL
18151 && parent_die->tag == DW_TAG_subprogram)
18152 {
18153 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18154 continue;
18155 }
18156
18157 /* Check whether this DIE is interesting enough to save. Normally
18158 we would not be interested in members here, but there may be
18159 later variables referencing them via DW_AT_specification (for
18160 static members). */
18161 if (!load_all
18162 && !is_type_tag_for_partial (abbrev->tag)
18163 && abbrev->tag != DW_TAG_constant
18164 && abbrev->tag != DW_TAG_enumerator
18165 && abbrev->tag != DW_TAG_subprogram
18166 && abbrev->tag != DW_TAG_inlined_subroutine
18167 && abbrev->tag != DW_TAG_lexical_block
18168 && abbrev->tag != DW_TAG_variable
18169 && abbrev->tag != DW_TAG_namespace
18170 && abbrev->tag != DW_TAG_module
18171 && abbrev->tag != DW_TAG_member
18172 && abbrev->tag != DW_TAG_imported_unit
18173 && abbrev->tag != DW_TAG_imported_declaration)
18174 {
18175 /* Otherwise we skip to the next sibling, if any. */
18176 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18177 continue;
18178 }
18179
18180 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18181 abbrev);
18182
18183 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18184
18185 /* This two-pass algorithm for processing partial symbols has a
18186 high cost in cache pressure. Thus, handle some simple cases
18187 here which cover the majority of C partial symbols. DIEs
18188 which neither have specification tags in them, nor could have
18189 specification tags elsewhere pointing at them, can simply be
18190 processed and discarded.
18191
18192 This segment is also optional; scan_partial_symbols and
18193 add_partial_symbol will handle these DIEs if we chain
18194 them in normally. When compilers which do not emit large
18195 quantities of duplicate debug information are more common,
18196 this code can probably be removed. */
18197
18198 /* Any complete simple types at the top level (pretty much all
18199 of them, for a language without namespaces), can be processed
18200 directly. */
18201 if (parent_die == NULL
18202 && pdi.has_specification == 0
18203 && pdi.is_declaration == 0
18204 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18205 || pdi.tag == DW_TAG_base_type
18206 || pdi.tag == DW_TAG_subrange_type))
18207 {
18208 if (building_psymtab && pdi.name != NULL)
18209 add_psymbol_to_list (pdi.name, false,
18210 VAR_DOMAIN, LOC_TYPEDEF, -1,
18211 psymbol_placement::STATIC,
18212 0, cu->language, objfile);
18213 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18214 continue;
18215 }
18216
18217 /* The exception for DW_TAG_typedef with has_children above is
18218 a workaround of GCC PR debug/47510. In the case of this complaint
18219 type_name_or_error will error on such types later.
18220
18221 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18222 it could not find the child DIEs referenced later, this is checked
18223 above. In correct DWARF DW_TAG_typedef should have no children. */
18224
18225 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18226 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18227 "- DIE at %s [in module %s]"),
18228 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18229
18230 /* If we're at the second level, and we're an enumerator, and
18231 our parent has no specification (meaning possibly lives in a
18232 namespace elsewhere), then we can add the partial symbol now
18233 instead of queueing it. */
18234 if (pdi.tag == DW_TAG_enumerator
18235 && parent_die != NULL
18236 && parent_die->die_parent == NULL
18237 && parent_die->tag == DW_TAG_enumeration_type
18238 && parent_die->has_specification == 0)
18239 {
18240 if (pdi.name == NULL)
18241 complaint (_("malformed enumerator DIE ignored"));
18242 else if (building_psymtab)
18243 add_psymbol_to_list (pdi.name, false,
18244 VAR_DOMAIN, LOC_CONST, -1,
18245 cu->language == language_cplus
18246 ? psymbol_placement::GLOBAL
18247 : psymbol_placement::STATIC,
18248 0, cu->language, objfile);
18249
18250 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18251 continue;
18252 }
18253
18254 struct partial_die_info *part_die
18255 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18256
18257 /* We'll save this DIE so link it in. */
18258 part_die->die_parent = parent_die;
18259 part_die->die_sibling = NULL;
18260 part_die->die_child = NULL;
18261
18262 if (last_die && last_die == parent_die)
18263 last_die->die_child = part_die;
18264 else if (last_die)
18265 last_die->die_sibling = part_die;
18266
18267 last_die = part_die;
18268
18269 if (first_die == NULL)
18270 first_die = part_die;
18271
18272 /* Maybe add the DIE to the hash table. Not all DIEs that we
18273 find interesting need to be in the hash table, because we
18274 also have the parent/sibling/child chains; only those that we
18275 might refer to by offset later during partial symbol reading.
18276
18277 For now this means things that might have be the target of a
18278 DW_AT_specification, DW_AT_abstract_origin, or
18279 DW_AT_extension. DW_AT_extension will refer only to
18280 namespaces; DW_AT_abstract_origin refers to functions (and
18281 many things under the function DIE, but we do not recurse
18282 into function DIEs during partial symbol reading) and
18283 possibly variables as well; DW_AT_specification refers to
18284 declarations. Declarations ought to have the DW_AT_declaration
18285 flag. It happens that GCC forgets to put it in sometimes, but
18286 only for functions, not for types.
18287
18288 Adding more things than necessary to the hash table is harmless
18289 except for the performance cost. Adding too few will result in
18290 wasted time in find_partial_die, when we reread the compilation
18291 unit with load_all_dies set. */
18292
18293 if (load_all
18294 || abbrev->tag == DW_TAG_constant
18295 || abbrev->tag == DW_TAG_subprogram
18296 || abbrev->tag == DW_TAG_variable
18297 || abbrev->tag == DW_TAG_namespace
18298 || part_die->is_declaration)
18299 {
18300 void **slot;
18301
18302 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18303 to_underlying (part_die->sect_off),
18304 INSERT);
18305 *slot = part_die;
18306 }
18307
18308 /* For some DIEs we want to follow their children (if any). For C
18309 we have no reason to follow the children of structures; for other
18310 languages we have to, so that we can get at method physnames
18311 to infer fully qualified class names, for DW_AT_specification,
18312 and for C++ template arguments. For C++, we also look one level
18313 inside functions to find template arguments (if the name of the
18314 function does not already contain the template arguments).
18315
18316 For Ada and Fortran, we need to scan the children of subprograms
18317 and lexical blocks as well because these languages allow the
18318 definition of nested entities that could be interesting for the
18319 debugger, such as nested subprograms for instance. */
18320 if (last_die->has_children
18321 && (load_all
18322 || last_die->tag == DW_TAG_namespace
18323 || last_die->tag == DW_TAG_module
18324 || last_die->tag == DW_TAG_enumeration_type
18325 || (cu->language == language_cplus
18326 && last_die->tag == DW_TAG_subprogram
18327 && (last_die->name == NULL
18328 || strchr (last_die->name, '<') == NULL))
18329 || (cu->language != language_c
18330 && (last_die->tag == DW_TAG_class_type
18331 || last_die->tag == DW_TAG_interface_type
18332 || last_die->tag == DW_TAG_structure_type
18333 || last_die->tag == DW_TAG_union_type))
18334 || ((cu->language == language_ada
18335 || cu->language == language_fortran)
18336 && (last_die->tag == DW_TAG_subprogram
18337 || last_die->tag == DW_TAG_lexical_block))))
18338 {
18339 nesting_level++;
18340 parent_die = last_die;
18341 continue;
18342 }
18343
18344 /* Otherwise we skip to the next sibling, if any. */
18345 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18346
18347 /* Back to the top, do it again. */
18348 }
18349 }
18350
18351 partial_die_info::partial_die_info (sect_offset sect_off_,
18352 struct abbrev_info *abbrev)
18353 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18354 {
18355 }
18356
18357 /* Read a minimal amount of information into the minimal die structure.
18358 INFO_PTR should point just after the initial uleb128 of a DIE. */
18359
18360 const gdb_byte *
18361 partial_die_info::read (const struct die_reader_specs *reader,
18362 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18363 {
18364 struct dwarf2_cu *cu = reader->cu;
18365 struct dwarf2_per_objfile *dwarf2_per_objfile
18366 = cu->per_cu->dwarf2_per_objfile;
18367 unsigned int i;
18368 int has_low_pc_attr = 0;
18369 int has_high_pc_attr = 0;
18370 int high_pc_relative = 0;
18371
18372 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18373 for (i = 0; i < abbrev.num_attrs; ++i)
18374 {
18375 bool need_reprocess;
18376 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18377 info_ptr, &need_reprocess);
18378 /* String and address offsets that need to do the reprocessing have
18379 already been read at this point, so there is no need to wait until
18380 the loop terminates to do the reprocessing. */
18381 if (need_reprocess)
18382 read_attribute_reprocess (reader, &attr_vec[i]);
18383 attribute &attr = attr_vec[i];
18384 /* Store the data if it is of an attribute we want to keep in a
18385 partial symbol table. */
18386 switch (attr.name)
18387 {
18388 case DW_AT_name:
18389 switch (tag)
18390 {
18391 case DW_TAG_compile_unit:
18392 case DW_TAG_partial_unit:
18393 case DW_TAG_type_unit:
18394 /* Compilation units have a DW_AT_name that is a filename, not
18395 a source language identifier. */
18396 case DW_TAG_enumeration_type:
18397 case DW_TAG_enumerator:
18398 /* These tags always have simple identifiers already; no need
18399 to canonicalize them. */
18400 name = DW_STRING (&attr);
18401 break;
18402 default:
18403 {
18404 struct objfile *objfile = dwarf2_per_objfile->objfile;
18405
18406 name
18407 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18408 &objfile->per_bfd->storage_obstack);
18409 }
18410 break;
18411 }
18412 break;
18413 case DW_AT_linkage_name:
18414 case DW_AT_MIPS_linkage_name:
18415 /* Note that both forms of linkage name might appear. We
18416 assume they will be the same, and we only store the last
18417 one we see. */
18418 linkage_name = DW_STRING (&attr);
18419 break;
18420 case DW_AT_low_pc:
18421 has_low_pc_attr = 1;
18422 lowpc = attr.value_as_address ();
18423 break;
18424 case DW_AT_high_pc:
18425 has_high_pc_attr = 1;
18426 highpc = attr.value_as_address ();
18427 if (cu->header.version >= 4 && attr.form_is_constant ())
18428 high_pc_relative = 1;
18429 break;
18430 case DW_AT_location:
18431 /* Support the .debug_loc offsets. */
18432 if (attr.form_is_block ())
18433 {
18434 d.locdesc = DW_BLOCK (&attr);
18435 }
18436 else if (attr.form_is_section_offset ())
18437 {
18438 dwarf2_complex_location_expr_complaint ();
18439 }
18440 else
18441 {
18442 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18443 "partial symbol information");
18444 }
18445 break;
18446 case DW_AT_external:
18447 is_external = DW_UNSND (&attr);
18448 break;
18449 case DW_AT_declaration:
18450 is_declaration = DW_UNSND (&attr);
18451 break;
18452 case DW_AT_type:
18453 has_type = 1;
18454 break;
18455 case DW_AT_abstract_origin:
18456 case DW_AT_specification:
18457 case DW_AT_extension:
18458 has_specification = 1;
18459 spec_offset = dwarf2_get_ref_die_offset (&attr);
18460 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18461 || cu->per_cu->is_dwz);
18462 break;
18463 case DW_AT_sibling:
18464 /* Ignore absolute siblings, they might point outside of
18465 the current compile unit. */
18466 if (attr.form == DW_FORM_ref_addr)
18467 complaint (_("ignoring absolute DW_AT_sibling"));
18468 else
18469 {
18470 const gdb_byte *buffer = reader->buffer;
18471 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18472 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18473
18474 if (sibling_ptr < info_ptr)
18475 complaint (_("DW_AT_sibling points backwards"));
18476 else if (sibling_ptr > reader->buffer_end)
18477 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18478 else
18479 sibling = sibling_ptr;
18480 }
18481 break;
18482 case DW_AT_byte_size:
18483 has_byte_size = 1;
18484 break;
18485 case DW_AT_const_value:
18486 has_const_value = 1;
18487 break;
18488 case DW_AT_calling_convention:
18489 /* DWARF doesn't provide a way to identify a program's source-level
18490 entry point. DW_AT_calling_convention attributes are only meant
18491 to describe functions' calling conventions.
18492
18493 However, because it's a necessary piece of information in
18494 Fortran, and before DWARF 4 DW_CC_program was the only
18495 piece of debugging information whose definition refers to
18496 a 'main program' at all, several compilers marked Fortran
18497 main programs with DW_CC_program --- even when those
18498 functions use the standard calling conventions.
18499
18500 Although DWARF now specifies a way to provide this
18501 information, we support this practice for backward
18502 compatibility. */
18503 if (DW_UNSND (&attr) == DW_CC_program
18504 && cu->language == language_fortran)
18505 main_subprogram = 1;
18506 break;
18507 case DW_AT_inline:
18508 if (DW_UNSND (&attr) == DW_INL_inlined
18509 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18510 may_be_inlined = 1;
18511 break;
18512
18513 case DW_AT_import:
18514 if (tag == DW_TAG_imported_unit)
18515 {
18516 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18517 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18518 || cu->per_cu->is_dwz);
18519 }
18520 break;
18521
18522 case DW_AT_main_subprogram:
18523 main_subprogram = DW_UNSND (&attr);
18524 break;
18525
18526 case DW_AT_ranges:
18527 {
18528 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18529 but that requires a full DIE, so instead we just
18530 reimplement it. */
18531 int need_ranges_base = tag != DW_TAG_compile_unit;
18532 unsigned int ranges_offset = (DW_UNSND (&attr)
18533 + (need_ranges_base
18534 ? cu->ranges_base
18535 : 0));
18536
18537 /* Value of the DW_AT_ranges attribute is the offset in the
18538 .debug_ranges section. */
18539 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18540 nullptr))
18541 has_pc_info = 1;
18542 }
18543 break;
18544
18545 default:
18546 break;
18547 }
18548 }
18549
18550 /* For Ada, if both the name and the linkage name appear, we prefer
18551 the latter. This lets "catch exception" work better, regardless
18552 of the order in which the name and linkage name were emitted.
18553 Really, though, this is just a workaround for the fact that gdb
18554 doesn't store both the name and the linkage name. */
18555 if (cu->language == language_ada && linkage_name != nullptr)
18556 name = linkage_name;
18557
18558 if (high_pc_relative)
18559 highpc += lowpc;
18560
18561 if (has_low_pc_attr && has_high_pc_attr)
18562 {
18563 /* When using the GNU linker, .gnu.linkonce. sections are used to
18564 eliminate duplicate copies of functions and vtables and such.
18565 The linker will arbitrarily choose one and discard the others.
18566 The AT_*_pc values for such functions refer to local labels in
18567 these sections. If the section from that file was discarded, the
18568 labels are not in the output, so the relocs get a value of 0.
18569 If this is a discarded function, mark the pc bounds as invalid,
18570 so that GDB will ignore it. */
18571 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18572 {
18573 struct objfile *objfile = dwarf2_per_objfile->objfile;
18574 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18575
18576 complaint (_("DW_AT_low_pc %s is zero "
18577 "for DIE at %s [in module %s]"),
18578 paddress (gdbarch, lowpc),
18579 sect_offset_str (sect_off),
18580 objfile_name (objfile));
18581 }
18582 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18583 else if (lowpc >= highpc)
18584 {
18585 struct objfile *objfile = dwarf2_per_objfile->objfile;
18586 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18587
18588 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18589 "for DIE at %s [in module %s]"),
18590 paddress (gdbarch, lowpc),
18591 paddress (gdbarch, highpc),
18592 sect_offset_str (sect_off),
18593 objfile_name (objfile));
18594 }
18595 else
18596 has_pc_info = 1;
18597 }
18598
18599 return info_ptr;
18600 }
18601
18602 /* Find a cached partial DIE at OFFSET in CU. */
18603
18604 struct partial_die_info *
18605 dwarf2_cu::find_partial_die (sect_offset sect_off)
18606 {
18607 struct partial_die_info *lookup_die = NULL;
18608 struct partial_die_info part_die (sect_off);
18609
18610 lookup_die = ((struct partial_die_info *)
18611 htab_find_with_hash (partial_dies, &part_die,
18612 to_underlying (sect_off)));
18613
18614 return lookup_die;
18615 }
18616
18617 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18618 except in the case of .debug_types DIEs which do not reference
18619 outside their CU (they do however referencing other types via
18620 DW_FORM_ref_sig8). */
18621
18622 static const struct cu_partial_die_info
18623 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18624 {
18625 struct dwarf2_per_objfile *dwarf2_per_objfile
18626 = cu->per_cu->dwarf2_per_objfile;
18627 struct objfile *objfile = dwarf2_per_objfile->objfile;
18628 struct dwarf2_per_cu_data *per_cu = NULL;
18629 struct partial_die_info *pd = NULL;
18630
18631 if (offset_in_dwz == cu->per_cu->is_dwz
18632 && offset_in_cu_p (&cu->header, sect_off))
18633 {
18634 pd = cu->find_partial_die (sect_off);
18635 if (pd != NULL)
18636 return { cu, pd };
18637 /* We missed recording what we needed.
18638 Load all dies and try again. */
18639 per_cu = cu->per_cu;
18640 }
18641 else
18642 {
18643 /* TUs don't reference other CUs/TUs (except via type signatures). */
18644 if (cu->per_cu->is_debug_types)
18645 {
18646 error (_("Dwarf Error: Type Unit at offset %s contains"
18647 " external reference to offset %s [in module %s].\n"),
18648 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18649 bfd_get_filename (objfile->obfd));
18650 }
18651 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18652 dwarf2_per_objfile);
18653
18654 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18655 load_partial_comp_unit (per_cu);
18656
18657 per_cu->cu->last_used = 0;
18658 pd = per_cu->cu->find_partial_die (sect_off);
18659 }
18660
18661 /* If we didn't find it, and not all dies have been loaded,
18662 load them all and try again. */
18663
18664 if (pd == NULL && per_cu->load_all_dies == 0)
18665 {
18666 per_cu->load_all_dies = 1;
18667
18668 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18669 THIS_CU->cu may already be in use. So we can't just free it and
18670 replace its DIEs with the ones we read in. Instead, we leave those
18671 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18672 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18673 set. */
18674 load_partial_comp_unit (per_cu);
18675
18676 pd = per_cu->cu->find_partial_die (sect_off);
18677 }
18678
18679 if (pd == NULL)
18680 internal_error (__FILE__, __LINE__,
18681 _("could not find partial DIE %s "
18682 "in cache [from module %s]\n"),
18683 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18684 return { per_cu->cu, pd };
18685 }
18686
18687 /* See if we can figure out if the class lives in a namespace. We do
18688 this by looking for a member function; its demangled name will
18689 contain namespace info, if there is any. */
18690
18691 static void
18692 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18693 struct dwarf2_cu *cu)
18694 {
18695 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18696 what template types look like, because the demangler
18697 frequently doesn't give the same name as the debug info. We
18698 could fix this by only using the demangled name to get the
18699 prefix (but see comment in read_structure_type). */
18700
18701 struct partial_die_info *real_pdi;
18702 struct partial_die_info *child_pdi;
18703
18704 /* If this DIE (this DIE's specification, if any) has a parent, then
18705 we should not do this. We'll prepend the parent's fully qualified
18706 name when we create the partial symbol. */
18707
18708 real_pdi = struct_pdi;
18709 while (real_pdi->has_specification)
18710 {
18711 auto res = find_partial_die (real_pdi->spec_offset,
18712 real_pdi->spec_is_dwz, cu);
18713 real_pdi = res.pdi;
18714 cu = res.cu;
18715 }
18716
18717 if (real_pdi->die_parent != NULL)
18718 return;
18719
18720 for (child_pdi = struct_pdi->die_child;
18721 child_pdi != NULL;
18722 child_pdi = child_pdi->die_sibling)
18723 {
18724 if (child_pdi->tag == DW_TAG_subprogram
18725 && child_pdi->linkage_name != NULL)
18726 {
18727 gdb::unique_xmalloc_ptr<char> actual_class_name
18728 (language_class_name_from_physname (cu->language_defn,
18729 child_pdi->linkage_name));
18730 if (actual_class_name != NULL)
18731 {
18732 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18733 struct_pdi->name
18734 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18735 actual_class_name.get ());
18736 }
18737 break;
18738 }
18739 }
18740 }
18741
18742 void
18743 partial_die_info::fixup (struct dwarf2_cu *cu)
18744 {
18745 /* Once we've fixed up a die, there's no point in doing so again.
18746 This also avoids a memory leak if we were to call
18747 guess_partial_die_structure_name multiple times. */
18748 if (fixup_called)
18749 return;
18750
18751 /* If we found a reference attribute and the DIE has no name, try
18752 to find a name in the referred to DIE. */
18753
18754 if (name == NULL && has_specification)
18755 {
18756 struct partial_die_info *spec_die;
18757
18758 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18759 spec_die = res.pdi;
18760 cu = res.cu;
18761
18762 spec_die->fixup (cu);
18763
18764 if (spec_die->name)
18765 {
18766 name = spec_die->name;
18767
18768 /* Copy DW_AT_external attribute if it is set. */
18769 if (spec_die->is_external)
18770 is_external = spec_die->is_external;
18771 }
18772 }
18773
18774 /* Set default names for some unnamed DIEs. */
18775
18776 if (name == NULL && tag == DW_TAG_namespace)
18777 name = CP_ANONYMOUS_NAMESPACE_STR;
18778
18779 /* If there is no parent die to provide a namespace, and there are
18780 children, see if we can determine the namespace from their linkage
18781 name. */
18782 if (cu->language == language_cplus
18783 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18784 && die_parent == NULL
18785 && has_children
18786 && (tag == DW_TAG_class_type
18787 || tag == DW_TAG_structure_type
18788 || tag == DW_TAG_union_type))
18789 guess_partial_die_structure_name (this, cu);
18790
18791 /* GCC might emit a nameless struct or union that has a linkage
18792 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18793 if (name == NULL
18794 && (tag == DW_TAG_class_type
18795 || tag == DW_TAG_interface_type
18796 || tag == DW_TAG_structure_type
18797 || tag == DW_TAG_union_type)
18798 && linkage_name != NULL)
18799 {
18800 gdb::unique_xmalloc_ptr<char> demangled
18801 (gdb_demangle (linkage_name, DMGL_TYPES));
18802 if (demangled != nullptr)
18803 {
18804 const char *base;
18805
18806 /* Strip any leading namespaces/classes, keep only the base name.
18807 DW_AT_name for named DIEs does not contain the prefixes. */
18808 base = strrchr (demangled.get (), ':');
18809 if (base && base > demangled.get () && base[-1] == ':')
18810 base++;
18811 else
18812 base = demangled.get ();
18813
18814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18815 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18816 }
18817 }
18818
18819 fixup_called = 1;
18820 }
18821
18822 /* Process the attributes that had to be skipped in the first round. These
18823 attributes are the ones that need str_offsets_base or addr_base attributes.
18824 They could not have been processed in the first round, because at the time
18825 the values of str_offsets_base or addr_base may not have been known. */
18826 void read_attribute_reprocess (const struct die_reader_specs *reader,
18827 struct attribute *attr)
18828 {
18829 struct dwarf2_cu *cu = reader->cu;
18830 switch (attr->form)
18831 {
18832 case DW_FORM_addrx:
18833 case DW_FORM_GNU_addr_index:
18834 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18835 break;
18836 case DW_FORM_strx:
18837 case DW_FORM_strx1:
18838 case DW_FORM_strx2:
18839 case DW_FORM_strx3:
18840 case DW_FORM_strx4:
18841 case DW_FORM_GNU_str_index:
18842 {
18843 unsigned int str_index = DW_UNSND (attr);
18844 if (reader->dwo_file != NULL)
18845 {
18846 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18847 DW_STRING_IS_CANONICAL (attr) = 0;
18848 }
18849 else
18850 {
18851 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18852 DW_STRING_IS_CANONICAL (attr) = 0;
18853 }
18854 break;
18855 }
18856 default:
18857 gdb_assert_not_reached (_("Unexpected DWARF form."));
18858 }
18859 }
18860
18861 /* Read an attribute value described by an attribute form. */
18862
18863 static const gdb_byte *
18864 read_attribute_value (const struct die_reader_specs *reader,
18865 struct attribute *attr, unsigned form,
18866 LONGEST implicit_const, const gdb_byte *info_ptr,
18867 bool *need_reprocess)
18868 {
18869 struct dwarf2_cu *cu = reader->cu;
18870 struct dwarf2_per_objfile *dwarf2_per_objfile
18871 = cu->per_cu->dwarf2_per_objfile;
18872 struct objfile *objfile = dwarf2_per_objfile->objfile;
18873 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18874 bfd *abfd = reader->abfd;
18875 struct comp_unit_head *cu_header = &cu->header;
18876 unsigned int bytes_read;
18877 struct dwarf_block *blk;
18878 *need_reprocess = false;
18879
18880 attr->form = (enum dwarf_form) form;
18881 switch (form)
18882 {
18883 case DW_FORM_ref_addr:
18884 if (cu->header.version == 2)
18885 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18886 else
18887 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18888 &cu->header, &bytes_read);
18889 info_ptr += bytes_read;
18890 break;
18891 case DW_FORM_GNU_ref_alt:
18892 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18893 info_ptr += bytes_read;
18894 break;
18895 case DW_FORM_addr:
18896 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18897 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18898 info_ptr += bytes_read;
18899 break;
18900 case DW_FORM_block2:
18901 blk = dwarf_alloc_block (cu);
18902 blk->size = read_2_bytes (abfd, info_ptr);
18903 info_ptr += 2;
18904 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18905 info_ptr += blk->size;
18906 DW_BLOCK (attr) = blk;
18907 break;
18908 case DW_FORM_block4:
18909 blk = dwarf_alloc_block (cu);
18910 blk->size = read_4_bytes (abfd, info_ptr);
18911 info_ptr += 4;
18912 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18913 info_ptr += blk->size;
18914 DW_BLOCK (attr) = blk;
18915 break;
18916 case DW_FORM_data2:
18917 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18918 info_ptr += 2;
18919 break;
18920 case DW_FORM_data4:
18921 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18922 info_ptr += 4;
18923 break;
18924 case DW_FORM_data8:
18925 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18926 info_ptr += 8;
18927 break;
18928 case DW_FORM_data16:
18929 blk = dwarf_alloc_block (cu);
18930 blk->size = 16;
18931 blk->data = read_n_bytes (abfd, info_ptr, 16);
18932 info_ptr += 16;
18933 DW_BLOCK (attr) = blk;
18934 break;
18935 case DW_FORM_sec_offset:
18936 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18937 info_ptr += bytes_read;
18938 break;
18939 case DW_FORM_string:
18940 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18941 DW_STRING_IS_CANONICAL (attr) = 0;
18942 info_ptr += bytes_read;
18943 break;
18944 case DW_FORM_strp:
18945 if (!cu->per_cu->is_dwz)
18946 {
18947 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18948 abfd, info_ptr, cu_header,
18949 &bytes_read);
18950 DW_STRING_IS_CANONICAL (attr) = 0;
18951 info_ptr += bytes_read;
18952 break;
18953 }
18954 /* FALLTHROUGH */
18955 case DW_FORM_line_strp:
18956 if (!cu->per_cu->is_dwz)
18957 {
18958 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18959 abfd, info_ptr,
18960 cu_header, &bytes_read);
18961 DW_STRING_IS_CANONICAL (attr) = 0;
18962 info_ptr += bytes_read;
18963 break;
18964 }
18965 /* FALLTHROUGH */
18966 case DW_FORM_GNU_strp_alt:
18967 {
18968 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18969 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18970 &bytes_read);
18971
18972 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18973 dwz, str_offset);
18974 DW_STRING_IS_CANONICAL (attr) = 0;
18975 info_ptr += bytes_read;
18976 }
18977 break;
18978 case DW_FORM_exprloc:
18979 case DW_FORM_block:
18980 blk = dwarf_alloc_block (cu);
18981 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18982 info_ptr += bytes_read;
18983 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18984 info_ptr += blk->size;
18985 DW_BLOCK (attr) = blk;
18986 break;
18987 case DW_FORM_block1:
18988 blk = dwarf_alloc_block (cu);
18989 blk->size = read_1_byte (abfd, info_ptr);
18990 info_ptr += 1;
18991 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18992 info_ptr += blk->size;
18993 DW_BLOCK (attr) = blk;
18994 break;
18995 case DW_FORM_data1:
18996 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18997 info_ptr += 1;
18998 break;
18999 case DW_FORM_flag:
19000 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19001 info_ptr += 1;
19002 break;
19003 case DW_FORM_flag_present:
19004 DW_UNSND (attr) = 1;
19005 break;
19006 case DW_FORM_sdata:
19007 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19008 info_ptr += bytes_read;
19009 break;
19010 case DW_FORM_udata:
19011 case DW_FORM_rnglistx:
19012 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19013 info_ptr += bytes_read;
19014 break;
19015 case DW_FORM_ref1:
19016 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19017 + read_1_byte (abfd, info_ptr));
19018 info_ptr += 1;
19019 break;
19020 case DW_FORM_ref2:
19021 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19022 + read_2_bytes (abfd, info_ptr));
19023 info_ptr += 2;
19024 break;
19025 case DW_FORM_ref4:
19026 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19027 + read_4_bytes (abfd, info_ptr));
19028 info_ptr += 4;
19029 break;
19030 case DW_FORM_ref8:
19031 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19032 + read_8_bytes (abfd, info_ptr));
19033 info_ptr += 8;
19034 break;
19035 case DW_FORM_ref_sig8:
19036 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19037 info_ptr += 8;
19038 break;
19039 case DW_FORM_ref_udata:
19040 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19041 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19042 info_ptr += bytes_read;
19043 break;
19044 case DW_FORM_indirect:
19045 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19046 info_ptr += bytes_read;
19047 if (form == DW_FORM_implicit_const)
19048 {
19049 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19050 info_ptr += bytes_read;
19051 }
19052 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19053 info_ptr, need_reprocess);
19054 break;
19055 case DW_FORM_implicit_const:
19056 DW_SND (attr) = implicit_const;
19057 break;
19058 case DW_FORM_addrx:
19059 case DW_FORM_GNU_addr_index:
19060 *need_reprocess = true;
19061 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19062 info_ptr += bytes_read;
19063 break;
19064 case DW_FORM_strx:
19065 case DW_FORM_strx1:
19066 case DW_FORM_strx2:
19067 case DW_FORM_strx3:
19068 case DW_FORM_strx4:
19069 case DW_FORM_GNU_str_index:
19070 {
19071 ULONGEST str_index;
19072 if (form == DW_FORM_strx1)
19073 {
19074 str_index = read_1_byte (abfd, info_ptr);
19075 info_ptr += 1;
19076 }
19077 else if (form == DW_FORM_strx2)
19078 {
19079 str_index = read_2_bytes (abfd, info_ptr);
19080 info_ptr += 2;
19081 }
19082 else if (form == DW_FORM_strx3)
19083 {
19084 str_index = read_3_bytes (abfd, info_ptr);
19085 info_ptr += 3;
19086 }
19087 else if (form == DW_FORM_strx4)
19088 {
19089 str_index = read_4_bytes (abfd, info_ptr);
19090 info_ptr += 4;
19091 }
19092 else
19093 {
19094 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19095 info_ptr += bytes_read;
19096 }
19097 *need_reprocess = true;
19098 DW_UNSND (attr) = str_index;
19099 }
19100 break;
19101 default:
19102 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19103 dwarf_form_name (form),
19104 bfd_get_filename (abfd));
19105 }
19106
19107 /* Super hack. */
19108 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19109 attr->form = DW_FORM_GNU_ref_alt;
19110
19111 /* We have seen instances where the compiler tried to emit a byte
19112 size attribute of -1 which ended up being encoded as an unsigned
19113 0xffffffff. Although 0xffffffff is technically a valid size value,
19114 an object of this size seems pretty unlikely so we can relatively
19115 safely treat these cases as if the size attribute was invalid and
19116 treat them as zero by default. */
19117 if (attr->name == DW_AT_byte_size
19118 && form == DW_FORM_data4
19119 && DW_UNSND (attr) >= 0xffffffff)
19120 {
19121 complaint
19122 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19123 hex_string (DW_UNSND (attr)));
19124 DW_UNSND (attr) = 0;
19125 }
19126
19127 return info_ptr;
19128 }
19129
19130 /* Read an attribute described by an abbreviated attribute. */
19131
19132 static const gdb_byte *
19133 read_attribute (const struct die_reader_specs *reader,
19134 struct attribute *attr, struct attr_abbrev *abbrev,
19135 const gdb_byte *info_ptr, bool *need_reprocess)
19136 {
19137 attr->name = abbrev->name;
19138 return read_attribute_value (reader, attr, abbrev->form,
19139 abbrev->implicit_const, info_ptr,
19140 need_reprocess);
19141 }
19142
19143 static CORE_ADDR
19144 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19145 unsigned int *bytes_read)
19146 {
19147 struct comp_unit_head *cu_header = &cu->header;
19148 CORE_ADDR retval = 0;
19149
19150 if (cu_header->signed_addr_p)
19151 {
19152 switch (cu_header->addr_size)
19153 {
19154 case 2:
19155 retval = bfd_get_signed_16 (abfd, buf);
19156 break;
19157 case 4:
19158 retval = bfd_get_signed_32 (abfd, buf);
19159 break;
19160 case 8:
19161 retval = bfd_get_signed_64 (abfd, buf);
19162 break;
19163 default:
19164 internal_error (__FILE__, __LINE__,
19165 _("read_address: bad switch, signed [in module %s]"),
19166 bfd_get_filename (abfd));
19167 }
19168 }
19169 else
19170 {
19171 switch (cu_header->addr_size)
19172 {
19173 case 2:
19174 retval = bfd_get_16 (abfd, buf);
19175 break;
19176 case 4:
19177 retval = bfd_get_32 (abfd, buf);
19178 break;
19179 case 8:
19180 retval = bfd_get_64 (abfd, buf);
19181 break;
19182 default:
19183 internal_error (__FILE__, __LINE__,
19184 _("read_address: bad switch, "
19185 "unsigned [in module %s]"),
19186 bfd_get_filename (abfd));
19187 }
19188 }
19189
19190 *bytes_read = cu_header->addr_size;
19191 return retval;
19192 }
19193
19194 /* Read the initial length from a section. The (draft) DWARF 3
19195 specification allows the initial length to take up either 4 bytes
19196 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19197 bytes describe the length and all offsets will be 8 bytes in length
19198 instead of 4.
19199
19200 An older, non-standard 64-bit format is also handled by this
19201 function. The older format in question stores the initial length
19202 as an 8-byte quantity without an escape value. Lengths greater
19203 than 2^32 aren't very common which means that the initial 4 bytes
19204 is almost always zero. Since a length value of zero doesn't make
19205 sense for the 32-bit format, this initial zero can be considered to
19206 be an escape value which indicates the presence of the older 64-bit
19207 format. As written, the code can't detect (old format) lengths
19208 greater than 4GB. If it becomes necessary to handle lengths
19209 somewhat larger than 4GB, we could allow other small values (such
19210 as the non-sensical values of 1, 2, and 3) to also be used as
19211 escape values indicating the presence of the old format.
19212
19213 The value returned via bytes_read should be used to increment the
19214 relevant pointer after calling read_initial_length().
19215
19216 [ Note: read_initial_length() and read_offset() are based on the
19217 document entitled "DWARF Debugging Information Format", revision
19218 3, draft 8, dated November 19, 2001. This document was obtained
19219 from:
19220
19221 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19222
19223 This document is only a draft and is subject to change. (So beware.)
19224
19225 Details regarding the older, non-standard 64-bit format were
19226 determined empirically by examining 64-bit ELF files produced by
19227 the SGI toolchain on an IRIX 6.5 machine.
19228
19229 - Kevin, July 16, 2002
19230 ] */
19231
19232 static LONGEST
19233 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19234 {
19235 LONGEST length = bfd_get_32 (abfd, buf);
19236
19237 if (length == 0xffffffff)
19238 {
19239 length = bfd_get_64 (abfd, buf + 4);
19240 *bytes_read = 12;
19241 }
19242 else if (length == 0)
19243 {
19244 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19245 length = bfd_get_64 (abfd, buf);
19246 *bytes_read = 8;
19247 }
19248 else
19249 {
19250 *bytes_read = 4;
19251 }
19252
19253 return length;
19254 }
19255
19256 /* Cover function for read_initial_length.
19257 Returns the length of the object at BUF, and stores the size of the
19258 initial length in *BYTES_READ and stores the size that offsets will be in
19259 *OFFSET_SIZE.
19260 If the initial length size is not equivalent to that specified in
19261 CU_HEADER then issue a complaint.
19262 This is useful when reading non-comp-unit headers. */
19263
19264 static LONGEST
19265 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19266 const struct comp_unit_head *cu_header,
19267 unsigned int *bytes_read,
19268 unsigned int *offset_size)
19269 {
19270 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19271
19272 gdb_assert (cu_header->initial_length_size == 4
19273 || cu_header->initial_length_size == 8
19274 || cu_header->initial_length_size == 12);
19275
19276 if (cu_header->initial_length_size != *bytes_read)
19277 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19278
19279 *offset_size = (*bytes_read == 4) ? 4 : 8;
19280 return length;
19281 }
19282
19283 /* Read an offset from the data stream. The size of the offset is
19284 given by cu_header->offset_size. */
19285
19286 static LONGEST
19287 read_offset (bfd *abfd, const gdb_byte *buf,
19288 const struct comp_unit_head *cu_header,
19289 unsigned int *bytes_read)
19290 {
19291 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19292
19293 *bytes_read = cu_header->offset_size;
19294 return offset;
19295 }
19296
19297 /* Read an offset from the data stream. */
19298
19299 static LONGEST
19300 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19301 {
19302 LONGEST retval = 0;
19303
19304 switch (offset_size)
19305 {
19306 case 4:
19307 retval = bfd_get_32 (abfd, buf);
19308 break;
19309 case 8:
19310 retval = bfd_get_64 (abfd, buf);
19311 break;
19312 default:
19313 internal_error (__FILE__, __LINE__,
19314 _("read_offset_1: bad switch [in module %s]"),
19315 bfd_get_filename (abfd));
19316 }
19317
19318 return retval;
19319 }
19320
19321 static const gdb_byte *
19322 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19323 {
19324 /* If the size of a host char is 8 bits, we can return a pointer
19325 to the buffer, otherwise we have to copy the data to a buffer
19326 allocated on the temporary obstack. */
19327 gdb_assert (HOST_CHAR_BIT == 8);
19328 return buf;
19329 }
19330
19331 static const char *
19332 read_direct_string (bfd *abfd, const gdb_byte *buf,
19333 unsigned int *bytes_read_ptr)
19334 {
19335 /* If the size of a host char is 8 bits, we can return a pointer
19336 to the string, otherwise we have to copy the string to a buffer
19337 allocated on the temporary obstack. */
19338 gdb_assert (HOST_CHAR_BIT == 8);
19339 if (*buf == '\0')
19340 {
19341 *bytes_read_ptr = 1;
19342 return NULL;
19343 }
19344 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19345 return (const char *) buf;
19346 }
19347
19348 /* Return pointer to string at section SECT offset STR_OFFSET with error
19349 reporting strings FORM_NAME and SECT_NAME. */
19350
19351 static const char *
19352 read_indirect_string_at_offset_from (struct objfile *objfile,
19353 bfd *abfd, LONGEST str_offset,
19354 struct dwarf2_section_info *sect,
19355 const char *form_name,
19356 const char *sect_name)
19357 {
19358 sect->read (objfile);
19359 if (sect->buffer == NULL)
19360 error (_("%s used without %s section [in module %s]"),
19361 form_name, sect_name, bfd_get_filename (abfd));
19362 if (str_offset >= sect->size)
19363 error (_("%s pointing outside of %s section [in module %s]"),
19364 form_name, sect_name, bfd_get_filename (abfd));
19365 gdb_assert (HOST_CHAR_BIT == 8);
19366 if (sect->buffer[str_offset] == '\0')
19367 return NULL;
19368 return (const char *) (sect->buffer + str_offset);
19369 }
19370
19371 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19372
19373 static const char *
19374 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19375 bfd *abfd, LONGEST str_offset)
19376 {
19377 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19378 abfd, str_offset,
19379 &dwarf2_per_objfile->str,
19380 "DW_FORM_strp", ".debug_str");
19381 }
19382
19383 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19384
19385 static const char *
19386 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19387 bfd *abfd, LONGEST str_offset)
19388 {
19389 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19390 abfd, str_offset,
19391 &dwarf2_per_objfile->line_str,
19392 "DW_FORM_line_strp",
19393 ".debug_line_str");
19394 }
19395
19396 /* Read a string at offset STR_OFFSET in the .debug_str section from
19397 the .dwz file DWZ. Throw an error if the offset is too large. If
19398 the string consists of a single NUL byte, return NULL; otherwise
19399 return a pointer to the string. */
19400
19401 static const char *
19402 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19403 LONGEST str_offset)
19404 {
19405 dwz->str.read (objfile);
19406
19407 if (dwz->str.buffer == NULL)
19408 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19409 "section [in module %s]"),
19410 bfd_get_filename (dwz->dwz_bfd.get ()));
19411 if (str_offset >= dwz->str.size)
19412 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19413 ".debug_str section [in module %s]"),
19414 bfd_get_filename (dwz->dwz_bfd.get ()));
19415 gdb_assert (HOST_CHAR_BIT == 8);
19416 if (dwz->str.buffer[str_offset] == '\0')
19417 return NULL;
19418 return (const char *) (dwz->str.buffer + str_offset);
19419 }
19420
19421 /* Return pointer to string at .debug_str offset as read from BUF.
19422 BUF is assumed to be in a compilation unit described by CU_HEADER.
19423 Return *BYTES_READ_PTR count of bytes read from BUF. */
19424
19425 static const char *
19426 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19427 const gdb_byte *buf,
19428 const struct comp_unit_head *cu_header,
19429 unsigned int *bytes_read_ptr)
19430 {
19431 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19432
19433 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19434 }
19435
19436 /* Return pointer to string at .debug_line_str offset as read from BUF.
19437 BUF is assumed to be in a compilation unit described by CU_HEADER.
19438 Return *BYTES_READ_PTR count of bytes read from BUF. */
19439
19440 static const char *
19441 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19442 bfd *abfd, const gdb_byte *buf,
19443 const struct comp_unit_head *cu_header,
19444 unsigned int *bytes_read_ptr)
19445 {
19446 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19447
19448 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19449 str_offset);
19450 }
19451
19452 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19453 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19454 ADDR_SIZE is the size of addresses from the CU header. */
19455
19456 static CORE_ADDR
19457 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19458 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19459 int addr_size)
19460 {
19461 struct objfile *objfile = dwarf2_per_objfile->objfile;
19462 bfd *abfd = objfile->obfd;
19463 const gdb_byte *info_ptr;
19464 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19465
19466 dwarf2_per_objfile->addr.read (objfile);
19467 if (dwarf2_per_objfile->addr.buffer == NULL)
19468 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19469 objfile_name (objfile));
19470 if (addr_base_or_zero + addr_index * addr_size
19471 >= dwarf2_per_objfile->addr.size)
19472 error (_("DW_FORM_addr_index pointing outside of "
19473 ".debug_addr section [in module %s]"),
19474 objfile_name (objfile));
19475 info_ptr = (dwarf2_per_objfile->addr.buffer
19476 + addr_base_or_zero + addr_index * addr_size);
19477 if (addr_size == 4)
19478 return bfd_get_32 (abfd, info_ptr);
19479 else
19480 return bfd_get_64 (abfd, info_ptr);
19481 }
19482
19483 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19484
19485 static CORE_ADDR
19486 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19487 {
19488 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19489 cu->addr_base, cu->header.addr_size);
19490 }
19491
19492 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19493
19494 static CORE_ADDR
19495 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19496 unsigned int *bytes_read)
19497 {
19498 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19499 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19500
19501 return read_addr_index (cu, addr_index);
19502 }
19503
19504 /* Given an index in .debug_addr, fetch the value.
19505 NOTE: This can be called during dwarf expression evaluation,
19506 long after the debug information has been read, and thus per_cu->cu
19507 may no longer exist. */
19508
19509 CORE_ADDR
19510 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19511 unsigned int addr_index)
19512 {
19513 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19514 struct dwarf2_cu *cu = per_cu->cu;
19515 gdb::optional<ULONGEST> addr_base;
19516 int addr_size;
19517
19518 /* We need addr_base and addr_size.
19519 If we don't have PER_CU->cu, we have to get it.
19520 Nasty, but the alternative is storing the needed info in PER_CU,
19521 which at this point doesn't seem justified: it's not clear how frequently
19522 it would get used and it would increase the size of every PER_CU.
19523 Entry points like dwarf2_per_cu_addr_size do a similar thing
19524 so we're not in uncharted territory here.
19525 Alas we need to be a bit more complicated as addr_base is contained
19526 in the DIE.
19527
19528 We don't need to read the entire CU(/TU).
19529 We just need the header and top level die.
19530
19531 IWBN to use the aging mechanism to let us lazily later discard the CU.
19532 For now we skip this optimization. */
19533
19534 if (cu != NULL)
19535 {
19536 addr_base = cu->addr_base;
19537 addr_size = cu->header.addr_size;
19538 }
19539 else
19540 {
19541 cutu_reader reader (per_cu, NULL, 0, 0, false);
19542 addr_base = reader.cu->addr_base;
19543 addr_size = reader.cu->header.addr_size;
19544 }
19545
19546 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19547 addr_size);
19548 }
19549
19550 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19551 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19552 DWO file. */
19553
19554 static const char *
19555 read_str_index (struct dwarf2_cu *cu,
19556 struct dwarf2_section_info *str_section,
19557 struct dwarf2_section_info *str_offsets_section,
19558 ULONGEST str_offsets_base, ULONGEST str_index)
19559 {
19560 struct dwarf2_per_objfile *dwarf2_per_objfile
19561 = cu->per_cu->dwarf2_per_objfile;
19562 struct objfile *objfile = dwarf2_per_objfile->objfile;
19563 const char *objf_name = objfile_name (objfile);
19564 bfd *abfd = objfile->obfd;
19565 const gdb_byte *info_ptr;
19566 ULONGEST str_offset;
19567 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19568
19569 str_section->read (objfile);
19570 str_offsets_section->read (objfile);
19571 if (str_section->buffer == NULL)
19572 error (_("%s used without %s section"
19573 " in CU at offset %s [in module %s]"),
19574 form_name, str_section->get_name (),
19575 sect_offset_str (cu->header.sect_off), objf_name);
19576 if (str_offsets_section->buffer == NULL)
19577 error (_("%s used without %s section"
19578 " in CU at offset %s [in module %s]"),
19579 form_name, str_section->get_name (),
19580 sect_offset_str (cu->header.sect_off), objf_name);
19581 info_ptr = (str_offsets_section->buffer
19582 + str_offsets_base
19583 + str_index * cu->header.offset_size);
19584 if (cu->header.offset_size == 4)
19585 str_offset = bfd_get_32 (abfd, info_ptr);
19586 else
19587 str_offset = bfd_get_64 (abfd, info_ptr);
19588 if (str_offset >= str_section->size)
19589 error (_("Offset from %s pointing outside of"
19590 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19591 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19592 return (const char *) (str_section->buffer + str_offset);
19593 }
19594
19595 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19596
19597 static const char *
19598 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19599 {
19600 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19601 ? reader->cu->header.addr_size : 0;
19602 return read_str_index (reader->cu,
19603 &reader->dwo_file->sections.str,
19604 &reader->dwo_file->sections.str_offsets,
19605 str_offsets_base, str_index);
19606 }
19607
19608 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19609
19610 static const char *
19611 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19612 {
19613 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19614 const char *objf_name = objfile_name (objfile);
19615 static const char form_name[] = "DW_FORM_GNU_str_index";
19616 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19617
19618 if (!cu->str_offsets_base.has_value ())
19619 error (_("%s used in Fission stub without %s"
19620 " in CU at offset 0x%lx [in module %s]"),
19621 form_name, str_offsets_attr_name,
19622 (long) cu->header.offset_size, objf_name);
19623
19624 return read_str_index (cu,
19625 &cu->per_cu->dwarf2_per_objfile->str,
19626 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19627 *cu->str_offsets_base, str_index);
19628 }
19629
19630 /* Return the length of an LEB128 number in BUF. */
19631
19632 static int
19633 leb128_size (const gdb_byte *buf)
19634 {
19635 const gdb_byte *begin = buf;
19636 gdb_byte byte;
19637
19638 while (1)
19639 {
19640 byte = *buf++;
19641 if ((byte & 128) == 0)
19642 return buf - begin;
19643 }
19644 }
19645
19646 static void
19647 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19648 {
19649 switch (lang)
19650 {
19651 case DW_LANG_C89:
19652 case DW_LANG_C99:
19653 case DW_LANG_C11:
19654 case DW_LANG_C:
19655 case DW_LANG_UPC:
19656 cu->language = language_c;
19657 break;
19658 case DW_LANG_Java:
19659 case DW_LANG_C_plus_plus:
19660 case DW_LANG_C_plus_plus_11:
19661 case DW_LANG_C_plus_plus_14:
19662 cu->language = language_cplus;
19663 break;
19664 case DW_LANG_D:
19665 cu->language = language_d;
19666 break;
19667 case DW_LANG_Fortran77:
19668 case DW_LANG_Fortran90:
19669 case DW_LANG_Fortran95:
19670 case DW_LANG_Fortran03:
19671 case DW_LANG_Fortran08:
19672 cu->language = language_fortran;
19673 break;
19674 case DW_LANG_Go:
19675 cu->language = language_go;
19676 break;
19677 case DW_LANG_Mips_Assembler:
19678 cu->language = language_asm;
19679 break;
19680 case DW_LANG_Ada83:
19681 case DW_LANG_Ada95:
19682 cu->language = language_ada;
19683 break;
19684 case DW_LANG_Modula2:
19685 cu->language = language_m2;
19686 break;
19687 case DW_LANG_Pascal83:
19688 cu->language = language_pascal;
19689 break;
19690 case DW_LANG_ObjC:
19691 cu->language = language_objc;
19692 break;
19693 case DW_LANG_Rust:
19694 case DW_LANG_Rust_old:
19695 cu->language = language_rust;
19696 break;
19697 case DW_LANG_Cobol74:
19698 case DW_LANG_Cobol85:
19699 default:
19700 cu->language = language_minimal;
19701 break;
19702 }
19703 cu->language_defn = language_def (cu->language);
19704 }
19705
19706 /* Return the named attribute or NULL if not there. */
19707
19708 static struct attribute *
19709 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19710 {
19711 for (;;)
19712 {
19713 unsigned int i;
19714 struct attribute *spec = NULL;
19715
19716 for (i = 0; i < die->num_attrs; ++i)
19717 {
19718 if (die->attrs[i].name == name)
19719 return &die->attrs[i];
19720 if (die->attrs[i].name == DW_AT_specification
19721 || die->attrs[i].name == DW_AT_abstract_origin)
19722 spec = &die->attrs[i];
19723 }
19724
19725 if (!spec)
19726 break;
19727
19728 die = follow_die_ref (die, spec, &cu);
19729 }
19730
19731 return NULL;
19732 }
19733
19734 /* Return the named attribute or NULL if not there,
19735 but do not follow DW_AT_specification, etc.
19736 This is for use in contexts where we're reading .debug_types dies.
19737 Following DW_AT_specification, DW_AT_abstract_origin will take us
19738 back up the chain, and we want to go down. */
19739
19740 static struct attribute *
19741 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19742 {
19743 unsigned int i;
19744
19745 for (i = 0; i < die->num_attrs; ++i)
19746 if (die->attrs[i].name == name)
19747 return &die->attrs[i];
19748
19749 return NULL;
19750 }
19751
19752 /* Return the string associated with a string-typed attribute, or NULL if it
19753 is either not found or is of an incorrect type. */
19754
19755 static const char *
19756 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19757 {
19758 struct attribute *attr;
19759 const char *str = NULL;
19760
19761 attr = dwarf2_attr (die, name, cu);
19762
19763 if (attr != NULL)
19764 {
19765 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19766 || attr->form == DW_FORM_string
19767 || attr->form == DW_FORM_strx
19768 || attr->form == DW_FORM_strx1
19769 || attr->form == DW_FORM_strx2
19770 || attr->form == DW_FORM_strx3
19771 || attr->form == DW_FORM_strx4
19772 || attr->form == DW_FORM_GNU_str_index
19773 || attr->form == DW_FORM_GNU_strp_alt)
19774 str = DW_STRING (attr);
19775 else
19776 complaint (_("string type expected for attribute %s for "
19777 "DIE at %s in module %s"),
19778 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19779 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19780 }
19781
19782 return str;
19783 }
19784
19785 /* Return the dwo name or NULL if not present. If present, it is in either
19786 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19787 static const char *
19788 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19789 {
19790 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19791 if (dwo_name == nullptr)
19792 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19793 return dwo_name;
19794 }
19795
19796 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19797 and holds a non-zero value. This function should only be used for
19798 DW_FORM_flag or DW_FORM_flag_present attributes. */
19799
19800 static int
19801 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19802 {
19803 struct attribute *attr = dwarf2_attr (die, name, cu);
19804
19805 return (attr && DW_UNSND (attr));
19806 }
19807
19808 static int
19809 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19810 {
19811 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19812 which value is non-zero. However, we have to be careful with
19813 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19814 (via dwarf2_flag_true_p) follows this attribute. So we may
19815 end up accidently finding a declaration attribute that belongs
19816 to a different DIE referenced by the specification attribute,
19817 even though the given DIE does not have a declaration attribute. */
19818 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19819 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19820 }
19821
19822 /* Return the die giving the specification for DIE, if there is
19823 one. *SPEC_CU is the CU containing DIE on input, and the CU
19824 containing the return value on output. If there is no
19825 specification, but there is an abstract origin, that is
19826 returned. */
19827
19828 static struct die_info *
19829 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19830 {
19831 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19832 *spec_cu);
19833
19834 if (spec_attr == NULL)
19835 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19836
19837 if (spec_attr == NULL)
19838 return NULL;
19839 else
19840 return follow_die_ref (die, spec_attr, spec_cu);
19841 }
19842
19843 /* Stub for free_line_header to match void * callback types. */
19844
19845 static void
19846 free_line_header_voidp (void *arg)
19847 {
19848 struct line_header *lh = (struct line_header *) arg;
19849
19850 delete lh;
19851 }
19852
19853 void
19854 line_header::add_include_dir (const char *include_dir)
19855 {
19856 if (dwarf_line_debug >= 2)
19857 {
19858 size_t new_size;
19859 if (version >= 5)
19860 new_size = m_include_dirs.size ();
19861 else
19862 new_size = m_include_dirs.size () + 1;
19863 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19864 new_size, include_dir);
19865 }
19866 m_include_dirs.push_back (include_dir);
19867 }
19868
19869 void
19870 line_header::add_file_name (const char *name,
19871 dir_index d_index,
19872 unsigned int mod_time,
19873 unsigned int length)
19874 {
19875 if (dwarf_line_debug >= 2)
19876 {
19877 size_t new_size;
19878 if (version >= 5)
19879 new_size = file_names_size ();
19880 else
19881 new_size = file_names_size () + 1;
19882 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19883 new_size, name);
19884 }
19885 m_file_names.emplace_back (name, d_index, mod_time, length);
19886 }
19887
19888 /* A convenience function to find the proper .debug_line section for a CU. */
19889
19890 static struct dwarf2_section_info *
19891 get_debug_line_section (struct dwarf2_cu *cu)
19892 {
19893 struct dwarf2_section_info *section;
19894 struct dwarf2_per_objfile *dwarf2_per_objfile
19895 = cu->per_cu->dwarf2_per_objfile;
19896
19897 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19898 DWO file. */
19899 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19900 section = &cu->dwo_unit->dwo_file->sections.line;
19901 else if (cu->per_cu->is_dwz)
19902 {
19903 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19904
19905 section = &dwz->line;
19906 }
19907 else
19908 section = &dwarf2_per_objfile->line;
19909
19910 return section;
19911 }
19912
19913 /* Read directory or file name entry format, starting with byte of
19914 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19915 entries count and the entries themselves in the described entry
19916 format. */
19917
19918 static void
19919 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19920 bfd *abfd, const gdb_byte **bufp,
19921 struct line_header *lh,
19922 const struct comp_unit_head *cu_header,
19923 void (*callback) (struct line_header *lh,
19924 const char *name,
19925 dir_index d_index,
19926 unsigned int mod_time,
19927 unsigned int length))
19928 {
19929 gdb_byte format_count, formati;
19930 ULONGEST data_count, datai;
19931 const gdb_byte *buf = *bufp;
19932 const gdb_byte *format_header_data;
19933 unsigned int bytes_read;
19934
19935 format_count = read_1_byte (abfd, buf);
19936 buf += 1;
19937 format_header_data = buf;
19938 for (formati = 0; formati < format_count; formati++)
19939 {
19940 read_unsigned_leb128 (abfd, buf, &bytes_read);
19941 buf += bytes_read;
19942 read_unsigned_leb128 (abfd, buf, &bytes_read);
19943 buf += bytes_read;
19944 }
19945
19946 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19947 buf += bytes_read;
19948 for (datai = 0; datai < data_count; datai++)
19949 {
19950 const gdb_byte *format = format_header_data;
19951 struct file_entry fe;
19952
19953 for (formati = 0; formati < format_count; formati++)
19954 {
19955 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19956 format += bytes_read;
19957
19958 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19959 format += bytes_read;
19960
19961 gdb::optional<const char *> string;
19962 gdb::optional<unsigned int> uint;
19963
19964 switch (form)
19965 {
19966 case DW_FORM_string:
19967 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19968 buf += bytes_read;
19969 break;
19970
19971 case DW_FORM_line_strp:
19972 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19973 abfd, buf,
19974 cu_header,
19975 &bytes_read));
19976 buf += bytes_read;
19977 break;
19978
19979 case DW_FORM_data1:
19980 uint.emplace (read_1_byte (abfd, buf));
19981 buf += 1;
19982 break;
19983
19984 case DW_FORM_data2:
19985 uint.emplace (read_2_bytes (abfd, buf));
19986 buf += 2;
19987 break;
19988
19989 case DW_FORM_data4:
19990 uint.emplace (read_4_bytes (abfd, buf));
19991 buf += 4;
19992 break;
19993
19994 case DW_FORM_data8:
19995 uint.emplace (read_8_bytes (abfd, buf));
19996 buf += 8;
19997 break;
19998
19999 case DW_FORM_data16:
20000 /* This is used for MD5, but file_entry does not record MD5s. */
20001 buf += 16;
20002 break;
20003
20004 case DW_FORM_udata:
20005 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20006 buf += bytes_read;
20007 break;
20008
20009 case DW_FORM_block:
20010 /* It is valid only for DW_LNCT_timestamp which is ignored by
20011 current GDB. */
20012 break;
20013 }
20014
20015 switch (content_type)
20016 {
20017 case DW_LNCT_path:
20018 if (string.has_value ())
20019 fe.name = *string;
20020 break;
20021 case DW_LNCT_directory_index:
20022 if (uint.has_value ())
20023 fe.d_index = (dir_index) *uint;
20024 break;
20025 case DW_LNCT_timestamp:
20026 if (uint.has_value ())
20027 fe.mod_time = *uint;
20028 break;
20029 case DW_LNCT_size:
20030 if (uint.has_value ())
20031 fe.length = *uint;
20032 break;
20033 case DW_LNCT_MD5:
20034 break;
20035 default:
20036 complaint (_("Unknown format content type %s"),
20037 pulongest (content_type));
20038 }
20039 }
20040
20041 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20042 }
20043
20044 *bufp = buf;
20045 }
20046
20047 /* Read the statement program header starting at OFFSET in
20048 .debug_line, or .debug_line.dwo. Return a pointer
20049 to a struct line_header, allocated using xmalloc.
20050 Returns NULL if there is a problem reading the header, e.g., if it
20051 has a version we don't understand.
20052
20053 NOTE: the strings in the include directory and file name tables of
20054 the returned object point into the dwarf line section buffer,
20055 and must not be freed. */
20056
20057 static line_header_up
20058 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20059 {
20060 const gdb_byte *line_ptr;
20061 unsigned int bytes_read, offset_size;
20062 int i;
20063 const char *cur_dir, *cur_file;
20064 struct dwarf2_section_info *section;
20065 bfd *abfd;
20066 struct dwarf2_per_objfile *dwarf2_per_objfile
20067 = cu->per_cu->dwarf2_per_objfile;
20068
20069 section = get_debug_line_section (cu);
20070 section->read (dwarf2_per_objfile->objfile);
20071 if (section->buffer == NULL)
20072 {
20073 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20074 complaint (_("missing .debug_line.dwo section"));
20075 else
20076 complaint (_("missing .debug_line section"));
20077 return 0;
20078 }
20079
20080 /* We can't do this until we know the section is non-empty.
20081 Only then do we know we have such a section. */
20082 abfd = section->get_bfd_owner ();
20083
20084 /* Make sure that at least there's room for the total_length field.
20085 That could be 12 bytes long, but we're just going to fudge that. */
20086 if (to_underlying (sect_off) + 4 >= section->size)
20087 {
20088 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20089 return 0;
20090 }
20091
20092 line_header_up lh (new line_header ());
20093
20094 lh->sect_off = sect_off;
20095 lh->offset_in_dwz = cu->per_cu->is_dwz;
20096
20097 line_ptr = section->buffer + to_underlying (sect_off);
20098
20099 /* Read in the header. */
20100 lh->total_length =
20101 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20102 &bytes_read, &offset_size);
20103 line_ptr += bytes_read;
20104
20105 const gdb_byte *start_here = line_ptr;
20106
20107 if (line_ptr + lh->total_length > (section->buffer + section->size))
20108 {
20109 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20110 return 0;
20111 }
20112 lh->statement_program_end = start_here + lh->total_length;
20113 lh->version = read_2_bytes (abfd, line_ptr);
20114 line_ptr += 2;
20115 if (lh->version > 5)
20116 {
20117 /* This is a version we don't understand. The format could have
20118 changed in ways we don't handle properly so just punt. */
20119 complaint (_("unsupported version in .debug_line section"));
20120 return NULL;
20121 }
20122 if (lh->version >= 5)
20123 {
20124 gdb_byte segment_selector_size;
20125
20126 /* Skip address size. */
20127 read_1_byte (abfd, line_ptr);
20128 line_ptr += 1;
20129
20130 segment_selector_size = read_1_byte (abfd, line_ptr);
20131 line_ptr += 1;
20132 if (segment_selector_size != 0)
20133 {
20134 complaint (_("unsupported segment selector size %u "
20135 "in .debug_line section"),
20136 segment_selector_size);
20137 return NULL;
20138 }
20139 }
20140 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20141 line_ptr += offset_size;
20142 lh->statement_program_start = line_ptr + lh->header_length;
20143 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20144 line_ptr += 1;
20145 if (lh->version >= 4)
20146 {
20147 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20148 line_ptr += 1;
20149 }
20150 else
20151 lh->maximum_ops_per_instruction = 1;
20152
20153 if (lh->maximum_ops_per_instruction == 0)
20154 {
20155 lh->maximum_ops_per_instruction = 1;
20156 complaint (_("invalid maximum_ops_per_instruction "
20157 "in `.debug_line' section"));
20158 }
20159
20160 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20161 line_ptr += 1;
20162 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20163 line_ptr += 1;
20164 lh->line_range = read_1_byte (abfd, line_ptr);
20165 line_ptr += 1;
20166 lh->opcode_base = read_1_byte (abfd, line_ptr);
20167 line_ptr += 1;
20168 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20169
20170 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20171 for (i = 1; i < lh->opcode_base; ++i)
20172 {
20173 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20174 line_ptr += 1;
20175 }
20176
20177 if (lh->version >= 5)
20178 {
20179 /* Read directory table. */
20180 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20181 &cu->header,
20182 [] (struct line_header *header, const char *name,
20183 dir_index d_index, unsigned int mod_time,
20184 unsigned int length)
20185 {
20186 header->add_include_dir (name);
20187 });
20188
20189 /* Read file name table. */
20190 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20191 &cu->header,
20192 [] (struct line_header *header, const char *name,
20193 dir_index d_index, unsigned int mod_time,
20194 unsigned int length)
20195 {
20196 header->add_file_name (name, d_index, mod_time, length);
20197 });
20198 }
20199 else
20200 {
20201 /* Read directory table. */
20202 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20203 {
20204 line_ptr += bytes_read;
20205 lh->add_include_dir (cur_dir);
20206 }
20207 line_ptr += bytes_read;
20208
20209 /* Read file name table. */
20210 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20211 {
20212 unsigned int mod_time, length;
20213 dir_index d_index;
20214
20215 line_ptr += bytes_read;
20216 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20217 line_ptr += bytes_read;
20218 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20219 line_ptr += bytes_read;
20220 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20221 line_ptr += bytes_read;
20222
20223 lh->add_file_name (cur_file, d_index, mod_time, length);
20224 }
20225 line_ptr += bytes_read;
20226 }
20227
20228 if (line_ptr > (section->buffer + section->size))
20229 complaint (_("line number info header doesn't "
20230 "fit in `.debug_line' section"));
20231
20232 return lh;
20233 }
20234
20235 /* Subroutine of dwarf_decode_lines to simplify it.
20236 Return the file name of the psymtab for the given file_entry.
20237 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20238 If space for the result is malloc'd, *NAME_HOLDER will be set.
20239 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20240
20241 static const char *
20242 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20243 const dwarf2_psymtab *pst,
20244 const char *comp_dir,
20245 gdb::unique_xmalloc_ptr<char> *name_holder)
20246 {
20247 const char *include_name = fe.name;
20248 const char *include_name_to_compare = include_name;
20249 const char *pst_filename;
20250 int file_is_pst;
20251
20252 const char *dir_name = fe.include_dir (lh);
20253
20254 gdb::unique_xmalloc_ptr<char> hold_compare;
20255 if (!IS_ABSOLUTE_PATH (include_name)
20256 && (dir_name != NULL || comp_dir != NULL))
20257 {
20258 /* Avoid creating a duplicate psymtab for PST.
20259 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20260 Before we do the comparison, however, we need to account
20261 for DIR_NAME and COMP_DIR.
20262 First prepend dir_name (if non-NULL). If we still don't
20263 have an absolute path prepend comp_dir (if non-NULL).
20264 However, the directory we record in the include-file's
20265 psymtab does not contain COMP_DIR (to match the
20266 corresponding symtab(s)).
20267
20268 Example:
20269
20270 bash$ cd /tmp
20271 bash$ gcc -g ./hello.c
20272 include_name = "hello.c"
20273 dir_name = "."
20274 DW_AT_comp_dir = comp_dir = "/tmp"
20275 DW_AT_name = "./hello.c"
20276
20277 */
20278
20279 if (dir_name != NULL)
20280 {
20281 name_holder->reset (concat (dir_name, SLASH_STRING,
20282 include_name, (char *) NULL));
20283 include_name = name_holder->get ();
20284 include_name_to_compare = include_name;
20285 }
20286 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20287 {
20288 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20289 include_name, (char *) NULL));
20290 include_name_to_compare = hold_compare.get ();
20291 }
20292 }
20293
20294 pst_filename = pst->filename;
20295 gdb::unique_xmalloc_ptr<char> copied_name;
20296 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20297 {
20298 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20299 pst_filename, (char *) NULL));
20300 pst_filename = copied_name.get ();
20301 }
20302
20303 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20304
20305 if (file_is_pst)
20306 return NULL;
20307 return include_name;
20308 }
20309
20310 /* State machine to track the state of the line number program. */
20311
20312 class lnp_state_machine
20313 {
20314 public:
20315 /* Initialize a machine state for the start of a line number
20316 program. */
20317 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20318 bool record_lines_p);
20319
20320 file_entry *current_file ()
20321 {
20322 /* lh->file_names is 0-based, but the file name numbers in the
20323 statement program are 1-based. */
20324 return m_line_header->file_name_at (m_file);
20325 }
20326
20327 /* Record the line in the state machine. END_SEQUENCE is true if
20328 we're processing the end of a sequence. */
20329 void record_line (bool end_sequence);
20330
20331 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20332 nop-out rest of the lines in this sequence. */
20333 void check_line_address (struct dwarf2_cu *cu,
20334 const gdb_byte *line_ptr,
20335 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20336
20337 void handle_set_discriminator (unsigned int discriminator)
20338 {
20339 m_discriminator = discriminator;
20340 m_line_has_non_zero_discriminator |= discriminator != 0;
20341 }
20342
20343 /* Handle DW_LNE_set_address. */
20344 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20345 {
20346 m_op_index = 0;
20347 address += baseaddr;
20348 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20349 }
20350
20351 /* Handle DW_LNS_advance_pc. */
20352 void handle_advance_pc (CORE_ADDR adjust);
20353
20354 /* Handle a special opcode. */
20355 void handle_special_opcode (unsigned char op_code);
20356
20357 /* Handle DW_LNS_advance_line. */
20358 void handle_advance_line (int line_delta)
20359 {
20360 advance_line (line_delta);
20361 }
20362
20363 /* Handle DW_LNS_set_file. */
20364 void handle_set_file (file_name_index file);
20365
20366 /* Handle DW_LNS_negate_stmt. */
20367 void handle_negate_stmt ()
20368 {
20369 m_is_stmt = !m_is_stmt;
20370 }
20371
20372 /* Handle DW_LNS_const_add_pc. */
20373 void handle_const_add_pc ();
20374
20375 /* Handle DW_LNS_fixed_advance_pc. */
20376 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20377 {
20378 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20379 m_op_index = 0;
20380 }
20381
20382 /* Handle DW_LNS_copy. */
20383 void handle_copy ()
20384 {
20385 record_line (false);
20386 m_discriminator = 0;
20387 }
20388
20389 /* Handle DW_LNE_end_sequence. */
20390 void handle_end_sequence ()
20391 {
20392 m_currently_recording_lines = true;
20393 }
20394
20395 private:
20396 /* Advance the line by LINE_DELTA. */
20397 void advance_line (int line_delta)
20398 {
20399 m_line += line_delta;
20400
20401 if (line_delta != 0)
20402 m_line_has_non_zero_discriminator = m_discriminator != 0;
20403 }
20404
20405 struct dwarf2_cu *m_cu;
20406
20407 gdbarch *m_gdbarch;
20408
20409 /* True if we're recording lines.
20410 Otherwise we're building partial symtabs and are just interested in
20411 finding include files mentioned by the line number program. */
20412 bool m_record_lines_p;
20413
20414 /* The line number header. */
20415 line_header *m_line_header;
20416
20417 /* These are part of the standard DWARF line number state machine,
20418 and initialized according to the DWARF spec. */
20419
20420 unsigned char m_op_index = 0;
20421 /* The line table index of the current file. */
20422 file_name_index m_file = 1;
20423 unsigned int m_line = 1;
20424
20425 /* These are initialized in the constructor. */
20426
20427 CORE_ADDR m_address;
20428 bool m_is_stmt;
20429 unsigned int m_discriminator;
20430
20431 /* Additional bits of state we need to track. */
20432
20433 /* The last file that we called dwarf2_start_subfile for.
20434 This is only used for TLLs. */
20435 unsigned int m_last_file = 0;
20436 /* The last file a line number was recorded for. */
20437 struct subfile *m_last_subfile = NULL;
20438
20439 /* When true, record the lines we decode. */
20440 bool m_currently_recording_lines = false;
20441
20442 /* The last line number that was recorded, used to coalesce
20443 consecutive entries for the same line. This can happen, for
20444 example, when discriminators are present. PR 17276. */
20445 unsigned int m_last_line = 0;
20446 bool m_line_has_non_zero_discriminator = false;
20447 };
20448
20449 void
20450 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20451 {
20452 CORE_ADDR addr_adj = (((m_op_index + adjust)
20453 / m_line_header->maximum_ops_per_instruction)
20454 * m_line_header->minimum_instruction_length);
20455 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20456 m_op_index = ((m_op_index + adjust)
20457 % m_line_header->maximum_ops_per_instruction);
20458 }
20459
20460 void
20461 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20462 {
20463 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20464 CORE_ADDR addr_adj = (((m_op_index
20465 + (adj_opcode / m_line_header->line_range))
20466 / m_line_header->maximum_ops_per_instruction)
20467 * m_line_header->minimum_instruction_length);
20468 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20469 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20470 % m_line_header->maximum_ops_per_instruction);
20471
20472 int line_delta = (m_line_header->line_base
20473 + (adj_opcode % m_line_header->line_range));
20474 advance_line (line_delta);
20475 record_line (false);
20476 m_discriminator = 0;
20477 }
20478
20479 void
20480 lnp_state_machine::handle_set_file (file_name_index file)
20481 {
20482 m_file = file;
20483
20484 const file_entry *fe = current_file ();
20485 if (fe == NULL)
20486 dwarf2_debug_line_missing_file_complaint ();
20487 else if (m_record_lines_p)
20488 {
20489 const char *dir = fe->include_dir (m_line_header);
20490
20491 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20492 m_line_has_non_zero_discriminator = m_discriminator != 0;
20493 dwarf2_start_subfile (m_cu, fe->name, dir);
20494 }
20495 }
20496
20497 void
20498 lnp_state_machine::handle_const_add_pc ()
20499 {
20500 CORE_ADDR adjust
20501 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20502
20503 CORE_ADDR addr_adj
20504 = (((m_op_index + adjust)
20505 / m_line_header->maximum_ops_per_instruction)
20506 * m_line_header->minimum_instruction_length);
20507
20508 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20509 m_op_index = ((m_op_index + adjust)
20510 % m_line_header->maximum_ops_per_instruction);
20511 }
20512
20513 /* Return non-zero if we should add LINE to the line number table.
20514 LINE is the line to add, LAST_LINE is the last line that was added,
20515 LAST_SUBFILE is the subfile for LAST_LINE.
20516 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20517 had a non-zero discriminator.
20518
20519 We have to be careful in the presence of discriminators.
20520 E.g., for this line:
20521
20522 for (i = 0; i < 100000; i++);
20523
20524 clang can emit four line number entries for that one line,
20525 each with a different discriminator.
20526 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20527
20528 However, we want gdb to coalesce all four entries into one.
20529 Otherwise the user could stepi into the middle of the line and
20530 gdb would get confused about whether the pc really was in the
20531 middle of the line.
20532
20533 Things are further complicated by the fact that two consecutive
20534 line number entries for the same line is a heuristic used by gcc
20535 to denote the end of the prologue. So we can't just discard duplicate
20536 entries, we have to be selective about it. The heuristic we use is
20537 that we only collapse consecutive entries for the same line if at least
20538 one of those entries has a non-zero discriminator. PR 17276.
20539
20540 Note: Addresses in the line number state machine can never go backwards
20541 within one sequence, thus this coalescing is ok. */
20542
20543 static int
20544 dwarf_record_line_p (struct dwarf2_cu *cu,
20545 unsigned int line, unsigned int last_line,
20546 int line_has_non_zero_discriminator,
20547 struct subfile *last_subfile)
20548 {
20549 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20550 return 1;
20551 if (line != last_line)
20552 return 1;
20553 /* Same line for the same file that we've seen already.
20554 As a last check, for pr 17276, only record the line if the line
20555 has never had a non-zero discriminator. */
20556 if (!line_has_non_zero_discriminator)
20557 return 1;
20558 return 0;
20559 }
20560
20561 /* Use the CU's builder to record line number LINE beginning at
20562 address ADDRESS in the line table of subfile SUBFILE. */
20563
20564 static void
20565 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20566 unsigned int line, CORE_ADDR address,
20567 struct dwarf2_cu *cu)
20568 {
20569 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20570
20571 if (dwarf_line_debug)
20572 {
20573 fprintf_unfiltered (gdb_stdlog,
20574 "Recording line %u, file %s, address %s\n",
20575 line, lbasename (subfile->name),
20576 paddress (gdbarch, address));
20577 }
20578
20579 if (cu != nullptr)
20580 cu->get_builder ()->record_line (subfile, line, addr);
20581 }
20582
20583 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20584 Mark the end of a set of line number records.
20585 The arguments are the same as for dwarf_record_line_1.
20586 If SUBFILE is NULL the request is ignored. */
20587
20588 static void
20589 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20590 CORE_ADDR address, struct dwarf2_cu *cu)
20591 {
20592 if (subfile == NULL)
20593 return;
20594
20595 if (dwarf_line_debug)
20596 {
20597 fprintf_unfiltered (gdb_stdlog,
20598 "Finishing current line, file %s, address %s\n",
20599 lbasename (subfile->name),
20600 paddress (gdbarch, address));
20601 }
20602
20603 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20604 }
20605
20606 void
20607 lnp_state_machine::record_line (bool end_sequence)
20608 {
20609 if (dwarf_line_debug)
20610 {
20611 fprintf_unfiltered (gdb_stdlog,
20612 "Processing actual line %u: file %u,"
20613 " address %s, is_stmt %u, discrim %u%s\n",
20614 m_line, m_file,
20615 paddress (m_gdbarch, m_address),
20616 m_is_stmt, m_discriminator,
20617 (end_sequence ? "\t(end sequence)" : ""));
20618 }
20619
20620 file_entry *fe = current_file ();
20621
20622 if (fe == NULL)
20623 dwarf2_debug_line_missing_file_complaint ();
20624 /* For now we ignore lines not starting on an instruction boundary.
20625 But not when processing end_sequence for compatibility with the
20626 previous version of the code. */
20627 else if (m_op_index == 0 || end_sequence)
20628 {
20629 fe->included_p = 1;
20630 if (m_record_lines_p
20631 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20632 {
20633 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20634 || end_sequence)
20635 {
20636 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20637 m_currently_recording_lines ? m_cu : nullptr);
20638 }
20639
20640 if (!end_sequence)
20641 {
20642 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20643 m_line_has_non_zero_discriminator,
20644 m_last_subfile))
20645 {
20646 buildsym_compunit *builder = m_cu->get_builder ();
20647 dwarf_record_line_1 (m_gdbarch,
20648 builder->get_current_subfile (),
20649 m_line, m_address,
20650 m_currently_recording_lines ? m_cu : nullptr);
20651 }
20652 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20653 m_last_line = m_line;
20654 }
20655 }
20656 }
20657 }
20658
20659 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20660 line_header *lh, bool record_lines_p)
20661 {
20662 m_cu = cu;
20663 m_gdbarch = arch;
20664 m_record_lines_p = record_lines_p;
20665 m_line_header = lh;
20666
20667 m_currently_recording_lines = true;
20668
20669 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20670 was a line entry for it so that the backend has a chance to adjust it
20671 and also record it in case it needs it. This is currently used by MIPS
20672 code, cf. `mips_adjust_dwarf2_line'. */
20673 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20674 m_is_stmt = lh->default_is_stmt;
20675 m_discriminator = 0;
20676 }
20677
20678 void
20679 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20680 const gdb_byte *line_ptr,
20681 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20682 {
20683 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20684 the pc range of the CU. However, we restrict the test to only ADDRESS
20685 values of zero to preserve GDB's previous behaviour which is to handle
20686 the specific case of a function being GC'd by the linker. */
20687
20688 if (address == 0 && address < unrelocated_lowpc)
20689 {
20690 /* This line table is for a function which has been
20691 GCd by the linker. Ignore it. PR gdb/12528 */
20692
20693 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20694 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20695
20696 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20697 line_offset, objfile_name (objfile));
20698 m_currently_recording_lines = false;
20699 /* Note: m_currently_recording_lines is left as false until we see
20700 DW_LNE_end_sequence. */
20701 }
20702 }
20703
20704 /* Subroutine of dwarf_decode_lines to simplify it.
20705 Process the line number information in LH.
20706 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20707 program in order to set included_p for every referenced header. */
20708
20709 static void
20710 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20711 const int decode_for_pst_p, CORE_ADDR lowpc)
20712 {
20713 const gdb_byte *line_ptr, *extended_end;
20714 const gdb_byte *line_end;
20715 unsigned int bytes_read, extended_len;
20716 unsigned char op_code, extended_op;
20717 CORE_ADDR baseaddr;
20718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20719 bfd *abfd = objfile->obfd;
20720 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20721 /* True if we're recording line info (as opposed to building partial
20722 symtabs and just interested in finding include files mentioned by
20723 the line number program). */
20724 bool record_lines_p = !decode_for_pst_p;
20725
20726 baseaddr = objfile->text_section_offset ();
20727
20728 line_ptr = lh->statement_program_start;
20729 line_end = lh->statement_program_end;
20730
20731 /* Read the statement sequences until there's nothing left. */
20732 while (line_ptr < line_end)
20733 {
20734 /* The DWARF line number program state machine. Reset the state
20735 machine at the start of each sequence. */
20736 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20737 bool end_sequence = false;
20738
20739 if (record_lines_p)
20740 {
20741 /* Start a subfile for the current file of the state
20742 machine. */
20743 const file_entry *fe = state_machine.current_file ();
20744
20745 if (fe != NULL)
20746 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20747 }
20748
20749 /* Decode the table. */
20750 while (line_ptr < line_end && !end_sequence)
20751 {
20752 op_code = read_1_byte (abfd, line_ptr);
20753 line_ptr += 1;
20754
20755 if (op_code >= lh->opcode_base)
20756 {
20757 /* Special opcode. */
20758 state_machine.handle_special_opcode (op_code);
20759 }
20760 else switch (op_code)
20761 {
20762 case DW_LNS_extended_op:
20763 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20764 &bytes_read);
20765 line_ptr += bytes_read;
20766 extended_end = line_ptr + extended_len;
20767 extended_op = read_1_byte (abfd, line_ptr);
20768 line_ptr += 1;
20769 switch (extended_op)
20770 {
20771 case DW_LNE_end_sequence:
20772 state_machine.handle_end_sequence ();
20773 end_sequence = true;
20774 break;
20775 case DW_LNE_set_address:
20776 {
20777 CORE_ADDR address
20778 = read_address (abfd, line_ptr, cu, &bytes_read);
20779 line_ptr += bytes_read;
20780
20781 state_machine.check_line_address (cu, line_ptr,
20782 lowpc - baseaddr, address);
20783 state_machine.handle_set_address (baseaddr, address);
20784 }
20785 break;
20786 case DW_LNE_define_file:
20787 {
20788 const char *cur_file;
20789 unsigned int mod_time, length;
20790 dir_index dindex;
20791
20792 cur_file = read_direct_string (abfd, line_ptr,
20793 &bytes_read);
20794 line_ptr += bytes_read;
20795 dindex = (dir_index)
20796 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20797 line_ptr += bytes_read;
20798 mod_time =
20799 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20800 line_ptr += bytes_read;
20801 length =
20802 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20803 line_ptr += bytes_read;
20804 lh->add_file_name (cur_file, dindex, mod_time, length);
20805 }
20806 break;
20807 case DW_LNE_set_discriminator:
20808 {
20809 /* The discriminator is not interesting to the
20810 debugger; just ignore it. We still need to
20811 check its value though:
20812 if there are consecutive entries for the same
20813 (non-prologue) line we want to coalesce them.
20814 PR 17276. */
20815 unsigned int discr
20816 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20817 line_ptr += bytes_read;
20818
20819 state_machine.handle_set_discriminator (discr);
20820 }
20821 break;
20822 default:
20823 complaint (_("mangled .debug_line section"));
20824 return;
20825 }
20826 /* Make sure that we parsed the extended op correctly. If e.g.
20827 we expected a different address size than the producer used,
20828 we may have read the wrong number of bytes. */
20829 if (line_ptr != extended_end)
20830 {
20831 complaint (_("mangled .debug_line section"));
20832 return;
20833 }
20834 break;
20835 case DW_LNS_copy:
20836 state_machine.handle_copy ();
20837 break;
20838 case DW_LNS_advance_pc:
20839 {
20840 CORE_ADDR adjust
20841 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20842 line_ptr += bytes_read;
20843
20844 state_machine.handle_advance_pc (adjust);
20845 }
20846 break;
20847 case DW_LNS_advance_line:
20848 {
20849 int line_delta
20850 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20851 line_ptr += bytes_read;
20852
20853 state_machine.handle_advance_line (line_delta);
20854 }
20855 break;
20856 case DW_LNS_set_file:
20857 {
20858 file_name_index file
20859 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20860 &bytes_read);
20861 line_ptr += bytes_read;
20862
20863 state_machine.handle_set_file (file);
20864 }
20865 break;
20866 case DW_LNS_set_column:
20867 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20868 line_ptr += bytes_read;
20869 break;
20870 case DW_LNS_negate_stmt:
20871 state_machine.handle_negate_stmt ();
20872 break;
20873 case DW_LNS_set_basic_block:
20874 break;
20875 /* Add to the address register of the state machine the
20876 address increment value corresponding to special opcode
20877 255. I.e., this value is scaled by the minimum
20878 instruction length since special opcode 255 would have
20879 scaled the increment. */
20880 case DW_LNS_const_add_pc:
20881 state_machine.handle_const_add_pc ();
20882 break;
20883 case DW_LNS_fixed_advance_pc:
20884 {
20885 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20886 line_ptr += 2;
20887
20888 state_machine.handle_fixed_advance_pc (addr_adj);
20889 }
20890 break;
20891 default:
20892 {
20893 /* Unknown standard opcode, ignore it. */
20894 int i;
20895
20896 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20897 {
20898 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20899 line_ptr += bytes_read;
20900 }
20901 }
20902 }
20903 }
20904
20905 if (!end_sequence)
20906 dwarf2_debug_line_missing_end_sequence_complaint ();
20907
20908 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20909 in which case we still finish recording the last line). */
20910 state_machine.record_line (true);
20911 }
20912 }
20913
20914 /* Decode the Line Number Program (LNP) for the given line_header
20915 structure and CU. The actual information extracted and the type
20916 of structures created from the LNP depends on the value of PST.
20917
20918 1. If PST is NULL, then this procedure uses the data from the program
20919 to create all necessary symbol tables, and their linetables.
20920
20921 2. If PST is not NULL, this procedure reads the program to determine
20922 the list of files included by the unit represented by PST, and
20923 builds all the associated partial symbol tables.
20924
20925 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20926 It is used for relative paths in the line table.
20927 NOTE: When processing partial symtabs (pst != NULL),
20928 comp_dir == pst->dirname.
20929
20930 NOTE: It is important that psymtabs have the same file name (via strcmp)
20931 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20932 symtab we don't use it in the name of the psymtabs we create.
20933 E.g. expand_line_sal requires this when finding psymtabs to expand.
20934 A good testcase for this is mb-inline.exp.
20935
20936 LOWPC is the lowest address in CU (or 0 if not known).
20937
20938 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20939 for its PC<->lines mapping information. Otherwise only the filename
20940 table is read in. */
20941
20942 static void
20943 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20944 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20945 CORE_ADDR lowpc, int decode_mapping)
20946 {
20947 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20948 const int decode_for_pst_p = (pst != NULL);
20949
20950 if (decode_mapping)
20951 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20952
20953 if (decode_for_pst_p)
20954 {
20955 /* Now that we're done scanning the Line Header Program, we can
20956 create the psymtab of each included file. */
20957 for (auto &file_entry : lh->file_names ())
20958 if (file_entry.included_p == 1)
20959 {
20960 gdb::unique_xmalloc_ptr<char> name_holder;
20961 const char *include_name =
20962 psymtab_include_file_name (lh, file_entry, pst,
20963 comp_dir, &name_holder);
20964 if (include_name != NULL)
20965 dwarf2_create_include_psymtab (include_name, pst, objfile);
20966 }
20967 }
20968 else
20969 {
20970 /* Make sure a symtab is created for every file, even files
20971 which contain only variables (i.e. no code with associated
20972 line numbers). */
20973 buildsym_compunit *builder = cu->get_builder ();
20974 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20975
20976 for (auto &fe : lh->file_names ())
20977 {
20978 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20979 if (builder->get_current_subfile ()->symtab == NULL)
20980 {
20981 builder->get_current_subfile ()->symtab
20982 = allocate_symtab (cust,
20983 builder->get_current_subfile ()->name);
20984 }
20985 fe.symtab = builder->get_current_subfile ()->symtab;
20986 }
20987 }
20988 }
20989
20990 /* Start a subfile for DWARF. FILENAME is the name of the file and
20991 DIRNAME the name of the source directory which contains FILENAME
20992 or NULL if not known.
20993 This routine tries to keep line numbers from identical absolute and
20994 relative file names in a common subfile.
20995
20996 Using the `list' example from the GDB testsuite, which resides in
20997 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20998 of /srcdir/list0.c yields the following debugging information for list0.c:
20999
21000 DW_AT_name: /srcdir/list0.c
21001 DW_AT_comp_dir: /compdir
21002 files.files[0].name: list0.h
21003 files.files[0].dir: /srcdir
21004 files.files[1].name: list0.c
21005 files.files[1].dir: /srcdir
21006
21007 The line number information for list0.c has to end up in a single
21008 subfile, so that `break /srcdir/list0.c:1' works as expected.
21009 start_subfile will ensure that this happens provided that we pass the
21010 concatenation of files.files[1].dir and files.files[1].name as the
21011 subfile's name. */
21012
21013 static void
21014 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21015 const char *dirname)
21016 {
21017 gdb::unique_xmalloc_ptr<char> copy;
21018
21019 /* In order not to lose the line information directory,
21020 we concatenate it to the filename when it makes sense.
21021 Note that the Dwarf3 standard says (speaking of filenames in line
21022 information): ``The directory index is ignored for file names
21023 that represent full path names''. Thus ignoring dirname in the
21024 `else' branch below isn't an issue. */
21025
21026 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21027 {
21028 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21029 filename = copy.get ();
21030 }
21031
21032 cu->get_builder ()->start_subfile (filename);
21033 }
21034
21035 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21036 buildsym_compunit constructor. */
21037
21038 struct compunit_symtab *
21039 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21040 CORE_ADDR low_pc)
21041 {
21042 gdb_assert (m_builder == nullptr);
21043
21044 m_builder.reset (new struct buildsym_compunit
21045 (per_cu->dwarf2_per_objfile->objfile,
21046 name, comp_dir, language, low_pc));
21047
21048 list_in_scope = get_builder ()->get_file_symbols ();
21049
21050 get_builder ()->record_debugformat ("DWARF 2");
21051 get_builder ()->record_producer (producer);
21052
21053 processing_has_namespace_info = false;
21054
21055 return get_builder ()->get_compunit_symtab ();
21056 }
21057
21058 static void
21059 var_decode_location (struct attribute *attr, struct symbol *sym,
21060 struct dwarf2_cu *cu)
21061 {
21062 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21063 struct comp_unit_head *cu_header = &cu->header;
21064
21065 /* NOTE drow/2003-01-30: There used to be a comment and some special
21066 code here to turn a symbol with DW_AT_external and a
21067 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21068 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21069 with some versions of binutils) where shared libraries could have
21070 relocations against symbols in their debug information - the
21071 minimal symbol would have the right address, but the debug info
21072 would not. It's no longer necessary, because we will explicitly
21073 apply relocations when we read in the debug information now. */
21074
21075 /* A DW_AT_location attribute with no contents indicates that a
21076 variable has been optimized away. */
21077 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21078 {
21079 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21080 return;
21081 }
21082
21083 /* Handle one degenerate form of location expression specially, to
21084 preserve GDB's previous behavior when section offsets are
21085 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21086 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21087
21088 if (attr->form_is_block ()
21089 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21090 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21091 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21092 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21093 && (DW_BLOCK (attr)->size
21094 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21095 {
21096 unsigned int dummy;
21097
21098 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21099 SET_SYMBOL_VALUE_ADDRESS (sym,
21100 read_address (objfile->obfd,
21101 DW_BLOCK (attr)->data + 1,
21102 cu, &dummy));
21103 else
21104 SET_SYMBOL_VALUE_ADDRESS
21105 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21106 &dummy));
21107 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21108 fixup_symbol_section (sym, objfile);
21109 SET_SYMBOL_VALUE_ADDRESS
21110 (sym,
21111 SYMBOL_VALUE_ADDRESS (sym)
21112 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21113 return;
21114 }
21115
21116 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21117 expression evaluator, and use LOC_COMPUTED only when necessary
21118 (i.e. when the value of a register or memory location is
21119 referenced, or a thread-local block, etc.). Then again, it might
21120 not be worthwhile. I'm assuming that it isn't unless performance
21121 or memory numbers show me otherwise. */
21122
21123 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21124
21125 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21126 cu->has_loclist = true;
21127 }
21128
21129 /* Given a pointer to a DWARF information entry, figure out if we need
21130 to make a symbol table entry for it, and if so, create a new entry
21131 and return a pointer to it.
21132 If TYPE is NULL, determine symbol type from the die, otherwise
21133 used the passed type.
21134 If SPACE is not NULL, use it to hold the new symbol. If it is
21135 NULL, allocate a new symbol on the objfile's obstack. */
21136
21137 static struct symbol *
21138 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21139 struct symbol *space)
21140 {
21141 struct dwarf2_per_objfile *dwarf2_per_objfile
21142 = cu->per_cu->dwarf2_per_objfile;
21143 struct objfile *objfile = dwarf2_per_objfile->objfile;
21144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21145 struct symbol *sym = NULL;
21146 const char *name;
21147 struct attribute *attr = NULL;
21148 struct attribute *attr2 = NULL;
21149 CORE_ADDR baseaddr;
21150 struct pending **list_to_add = NULL;
21151
21152 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21153
21154 baseaddr = objfile->text_section_offset ();
21155
21156 name = dwarf2_name (die, cu);
21157 if (name)
21158 {
21159 const char *linkagename;
21160 int suppress_add = 0;
21161
21162 if (space)
21163 sym = space;
21164 else
21165 sym = allocate_symbol (objfile);
21166 OBJSTAT (objfile, n_syms++);
21167
21168 /* Cache this symbol's name and the name's demangled form (if any). */
21169 sym->set_language (cu->language, &objfile->objfile_obstack);
21170 linkagename = dwarf2_physname (name, die, cu);
21171 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21172
21173 /* Fortran does not have mangling standard and the mangling does differ
21174 between gfortran, iFort etc. */
21175 if (cu->language == language_fortran
21176 && symbol_get_demangled_name (sym) == NULL)
21177 symbol_set_demangled_name (sym,
21178 dwarf2_full_name (name, die, cu),
21179 NULL);
21180
21181 /* Default assumptions.
21182 Use the passed type or decode it from the die. */
21183 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21184 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21185 if (type != NULL)
21186 SYMBOL_TYPE (sym) = type;
21187 else
21188 SYMBOL_TYPE (sym) = die_type (die, cu);
21189 attr = dwarf2_attr (die,
21190 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21191 cu);
21192 if (attr != nullptr)
21193 {
21194 SYMBOL_LINE (sym) = DW_UNSND (attr);
21195 }
21196
21197 attr = dwarf2_attr (die,
21198 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21199 cu);
21200 if (attr != nullptr)
21201 {
21202 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21203 struct file_entry *fe;
21204
21205 if (cu->line_header != NULL)
21206 fe = cu->line_header->file_name_at (file_index);
21207 else
21208 fe = NULL;
21209
21210 if (fe == NULL)
21211 complaint (_("file index out of range"));
21212 else
21213 symbol_set_symtab (sym, fe->symtab);
21214 }
21215
21216 switch (die->tag)
21217 {
21218 case DW_TAG_label:
21219 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21220 if (attr != nullptr)
21221 {
21222 CORE_ADDR addr;
21223
21224 addr = attr->value_as_address ();
21225 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21226 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21227 }
21228 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21229 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21230 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21231 add_symbol_to_list (sym, cu->list_in_scope);
21232 break;
21233 case DW_TAG_subprogram:
21234 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21235 finish_block. */
21236 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21237 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21238 if ((attr2 && (DW_UNSND (attr2) != 0))
21239 || cu->language == language_ada
21240 || cu->language == language_fortran)
21241 {
21242 /* Subprograms marked external are stored as a global symbol.
21243 Ada and Fortran subprograms, whether marked external or
21244 not, are always stored as a global symbol, because we want
21245 to be able to access them globally. For instance, we want
21246 to be able to break on a nested subprogram without having
21247 to specify the context. */
21248 list_to_add = cu->get_builder ()->get_global_symbols ();
21249 }
21250 else
21251 {
21252 list_to_add = cu->list_in_scope;
21253 }
21254 break;
21255 case DW_TAG_inlined_subroutine:
21256 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21257 finish_block. */
21258 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21259 SYMBOL_INLINED (sym) = 1;
21260 list_to_add = cu->list_in_scope;
21261 break;
21262 case DW_TAG_template_value_param:
21263 suppress_add = 1;
21264 /* Fall through. */
21265 case DW_TAG_constant:
21266 case DW_TAG_variable:
21267 case DW_TAG_member:
21268 /* Compilation with minimal debug info may result in
21269 variables with missing type entries. Change the
21270 misleading `void' type to something sensible. */
21271 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21272 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21273
21274 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21275 /* In the case of DW_TAG_member, we should only be called for
21276 static const members. */
21277 if (die->tag == DW_TAG_member)
21278 {
21279 /* dwarf2_add_field uses die_is_declaration,
21280 so we do the same. */
21281 gdb_assert (die_is_declaration (die, cu));
21282 gdb_assert (attr);
21283 }
21284 if (attr != nullptr)
21285 {
21286 dwarf2_const_value (attr, sym, cu);
21287 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21288 if (!suppress_add)
21289 {
21290 if (attr2 && (DW_UNSND (attr2) != 0))
21291 list_to_add = cu->get_builder ()->get_global_symbols ();
21292 else
21293 list_to_add = cu->list_in_scope;
21294 }
21295 break;
21296 }
21297 attr = dwarf2_attr (die, DW_AT_location, cu);
21298 if (attr != nullptr)
21299 {
21300 var_decode_location (attr, sym, cu);
21301 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21302
21303 /* Fortran explicitly imports any global symbols to the local
21304 scope by DW_TAG_common_block. */
21305 if (cu->language == language_fortran && die->parent
21306 && die->parent->tag == DW_TAG_common_block)
21307 attr2 = NULL;
21308
21309 if (SYMBOL_CLASS (sym) == LOC_STATIC
21310 && SYMBOL_VALUE_ADDRESS (sym) == 0
21311 && !dwarf2_per_objfile->has_section_at_zero)
21312 {
21313 /* When a static variable is eliminated by the linker,
21314 the corresponding debug information is not stripped
21315 out, but the variable address is set to null;
21316 do not add such variables into symbol table. */
21317 }
21318 else if (attr2 && (DW_UNSND (attr2) != 0))
21319 {
21320 if (SYMBOL_CLASS (sym) == LOC_STATIC
21321 && (objfile->flags & OBJF_MAINLINE) == 0
21322 && dwarf2_per_objfile->can_copy)
21323 {
21324 /* A global static variable might be subject to
21325 copy relocation. We first check for a local
21326 minsym, though, because maybe the symbol was
21327 marked hidden, in which case this would not
21328 apply. */
21329 bound_minimal_symbol found
21330 = (lookup_minimal_symbol_linkage
21331 (sym->linkage_name (), objfile));
21332 if (found.minsym != nullptr)
21333 sym->maybe_copied = 1;
21334 }
21335
21336 /* A variable with DW_AT_external is never static,
21337 but it may be block-scoped. */
21338 list_to_add
21339 = ((cu->list_in_scope
21340 == cu->get_builder ()->get_file_symbols ())
21341 ? cu->get_builder ()->get_global_symbols ()
21342 : cu->list_in_scope);
21343 }
21344 else
21345 list_to_add = cu->list_in_scope;
21346 }
21347 else
21348 {
21349 /* We do not know the address of this symbol.
21350 If it is an external symbol and we have type information
21351 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21352 The address of the variable will then be determined from
21353 the minimal symbol table whenever the variable is
21354 referenced. */
21355 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21356
21357 /* Fortran explicitly imports any global symbols to the local
21358 scope by DW_TAG_common_block. */
21359 if (cu->language == language_fortran && die->parent
21360 && die->parent->tag == DW_TAG_common_block)
21361 {
21362 /* SYMBOL_CLASS doesn't matter here because
21363 read_common_block is going to reset it. */
21364 if (!suppress_add)
21365 list_to_add = cu->list_in_scope;
21366 }
21367 else if (attr2 && (DW_UNSND (attr2) != 0)
21368 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21369 {
21370 /* A variable with DW_AT_external is never static, but it
21371 may be block-scoped. */
21372 list_to_add
21373 = ((cu->list_in_scope
21374 == cu->get_builder ()->get_file_symbols ())
21375 ? cu->get_builder ()->get_global_symbols ()
21376 : cu->list_in_scope);
21377
21378 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21379 }
21380 else if (!die_is_declaration (die, cu))
21381 {
21382 /* Use the default LOC_OPTIMIZED_OUT class. */
21383 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21384 if (!suppress_add)
21385 list_to_add = cu->list_in_scope;
21386 }
21387 }
21388 break;
21389 case DW_TAG_formal_parameter:
21390 {
21391 /* If we are inside a function, mark this as an argument. If
21392 not, we might be looking at an argument to an inlined function
21393 when we do not have enough information to show inlined frames;
21394 pretend it's a local variable in that case so that the user can
21395 still see it. */
21396 struct context_stack *curr
21397 = cu->get_builder ()->get_current_context_stack ();
21398 if (curr != nullptr && curr->name != nullptr)
21399 SYMBOL_IS_ARGUMENT (sym) = 1;
21400 attr = dwarf2_attr (die, DW_AT_location, cu);
21401 if (attr != nullptr)
21402 {
21403 var_decode_location (attr, sym, cu);
21404 }
21405 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21406 if (attr != nullptr)
21407 {
21408 dwarf2_const_value (attr, sym, cu);
21409 }
21410
21411 list_to_add = cu->list_in_scope;
21412 }
21413 break;
21414 case DW_TAG_unspecified_parameters:
21415 /* From varargs functions; gdb doesn't seem to have any
21416 interest in this information, so just ignore it for now.
21417 (FIXME?) */
21418 break;
21419 case DW_TAG_template_type_param:
21420 suppress_add = 1;
21421 /* Fall through. */
21422 case DW_TAG_class_type:
21423 case DW_TAG_interface_type:
21424 case DW_TAG_structure_type:
21425 case DW_TAG_union_type:
21426 case DW_TAG_set_type:
21427 case DW_TAG_enumeration_type:
21428 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21429 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21430
21431 {
21432 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21433 really ever be static objects: otherwise, if you try
21434 to, say, break of a class's method and you're in a file
21435 which doesn't mention that class, it won't work unless
21436 the check for all static symbols in lookup_symbol_aux
21437 saves you. See the OtherFileClass tests in
21438 gdb.c++/namespace.exp. */
21439
21440 if (!suppress_add)
21441 {
21442 buildsym_compunit *builder = cu->get_builder ();
21443 list_to_add
21444 = (cu->list_in_scope == builder->get_file_symbols ()
21445 && cu->language == language_cplus
21446 ? builder->get_global_symbols ()
21447 : cu->list_in_scope);
21448
21449 /* The semantics of C++ state that "struct foo {
21450 ... }" also defines a typedef for "foo". */
21451 if (cu->language == language_cplus
21452 || cu->language == language_ada
21453 || cu->language == language_d
21454 || cu->language == language_rust)
21455 {
21456 /* The symbol's name is already allocated along
21457 with this objfile, so we don't need to
21458 duplicate it for the type. */
21459 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21460 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21461 }
21462 }
21463 }
21464 break;
21465 case DW_TAG_typedef:
21466 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21467 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21468 list_to_add = cu->list_in_scope;
21469 break;
21470 case DW_TAG_base_type:
21471 case DW_TAG_subrange_type:
21472 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21473 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21474 list_to_add = cu->list_in_scope;
21475 break;
21476 case DW_TAG_enumerator:
21477 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21478 if (attr != nullptr)
21479 {
21480 dwarf2_const_value (attr, sym, cu);
21481 }
21482 {
21483 /* NOTE: carlton/2003-11-10: See comment above in the
21484 DW_TAG_class_type, etc. block. */
21485
21486 list_to_add
21487 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21488 && cu->language == language_cplus
21489 ? cu->get_builder ()->get_global_symbols ()
21490 : cu->list_in_scope);
21491 }
21492 break;
21493 case DW_TAG_imported_declaration:
21494 case DW_TAG_namespace:
21495 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21496 list_to_add = cu->get_builder ()->get_global_symbols ();
21497 break;
21498 case DW_TAG_module:
21499 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21500 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21501 list_to_add = cu->get_builder ()->get_global_symbols ();
21502 break;
21503 case DW_TAG_common_block:
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21505 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21506 add_symbol_to_list (sym, cu->list_in_scope);
21507 break;
21508 default:
21509 /* Not a tag we recognize. Hopefully we aren't processing
21510 trash data, but since we must specifically ignore things
21511 we don't recognize, there is nothing else we should do at
21512 this point. */
21513 complaint (_("unsupported tag: '%s'"),
21514 dwarf_tag_name (die->tag));
21515 break;
21516 }
21517
21518 if (suppress_add)
21519 {
21520 sym->hash_next = objfile->template_symbols;
21521 objfile->template_symbols = sym;
21522 list_to_add = NULL;
21523 }
21524
21525 if (list_to_add != NULL)
21526 add_symbol_to_list (sym, list_to_add);
21527
21528 /* For the benefit of old versions of GCC, check for anonymous
21529 namespaces based on the demangled name. */
21530 if (!cu->processing_has_namespace_info
21531 && cu->language == language_cplus)
21532 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21533 }
21534 return (sym);
21535 }
21536
21537 /* Given an attr with a DW_FORM_dataN value in host byte order,
21538 zero-extend it as appropriate for the symbol's type. The DWARF
21539 standard (v4) is not entirely clear about the meaning of using
21540 DW_FORM_dataN for a constant with a signed type, where the type is
21541 wider than the data. The conclusion of a discussion on the DWARF
21542 list was that this is unspecified. We choose to always zero-extend
21543 because that is the interpretation long in use by GCC. */
21544
21545 static gdb_byte *
21546 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21547 struct dwarf2_cu *cu, LONGEST *value, int bits)
21548 {
21549 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21550 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21551 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21552 LONGEST l = DW_UNSND (attr);
21553
21554 if (bits < sizeof (*value) * 8)
21555 {
21556 l &= ((LONGEST) 1 << bits) - 1;
21557 *value = l;
21558 }
21559 else if (bits == sizeof (*value) * 8)
21560 *value = l;
21561 else
21562 {
21563 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21564 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21565 return bytes;
21566 }
21567
21568 return NULL;
21569 }
21570
21571 /* Read a constant value from an attribute. Either set *VALUE, or if
21572 the value does not fit in *VALUE, set *BYTES - either already
21573 allocated on the objfile obstack, or newly allocated on OBSTACK,
21574 or, set *BATON, if we translated the constant to a location
21575 expression. */
21576
21577 static void
21578 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21579 const char *name, struct obstack *obstack,
21580 struct dwarf2_cu *cu,
21581 LONGEST *value, const gdb_byte **bytes,
21582 struct dwarf2_locexpr_baton **baton)
21583 {
21584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21585 struct comp_unit_head *cu_header = &cu->header;
21586 struct dwarf_block *blk;
21587 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21588 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21589
21590 *value = 0;
21591 *bytes = NULL;
21592 *baton = NULL;
21593
21594 switch (attr->form)
21595 {
21596 case DW_FORM_addr:
21597 case DW_FORM_addrx:
21598 case DW_FORM_GNU_addr_index:
21599 {
21600 gdb_byte *data;
21601
21602 if (TYPE_LENGTH (type) != cu_header->addr_size)
21603 dwarf2_const_value_length_mismatch_complaint (name,
21604 cu_header->addr_size,
21605 TYPE_LENGTH (type));
21606 /* Symbols of this form are reasonably rare, so we just
21607 piggyback on the existing location code rather than writing
21608 a new implementation of symbol_computed_ops. */
21609 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21610 (*baton)->per_cu = cu->per_cu;
21611 gdb_assert ((*baton)->per_cu);
21612
21613 (*baton)->size = 2 + cu_header->addr_size;
21614 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21615 (*baton)->data = data;
21616
21617 data[0] = DW_OP_addr;
21618 store_unsigned_integer (&data[1], cu_header->addr_size,
21619 byte_order, DW_ADDR (attr));
21620 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21621 }
21622 break;
21623 case DW_FORM_string:
21624 case DW_FORM_strp:
21625 case DW_FORM_strx:
21626 case DW_FORM_GNU_str_index:
21627 case DW_FORM_GNU_strp_alt:
21628 /* DW_STRING is already allocated on the objfile obstack, point
21629 directly to it. */
21630 *bytes = (const gdb_byte *) DW_STRING (attr);
21631 break;
21632 case DW_FORM_block1:
21633 case DW_FORM_block2:
21634 case DW_FORM_block4:
21635 case DW_FORM_block:
21636 case DW_FORM_exprloc:
21637 case DW_FORM_data16:
21638 blk = DW_BLOCK (attr);
21639 if (TYPE_LENGTH (type) != blk->size)
21640 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21641 TYPE_LENGTH (type));
21642 *bytes = blk->data;
21643 break;
21644
21645 /* The DW_AT_const_value attributes are supposed to carry the
21646 symbol's value "represented as it would be on the target
21647 architecture." By the time we get here, it's already been
21648 converted to host endianness, so we just need to sign- or
21649 zero-extend it as appropriate. */
21650 case DW_FORM_data1:
21651 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21652 break;
21653 case DW_FORM_data2:
21654 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21655 break;
21656 case DW_FORM_data4:
21657 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21658 break;
21659 case DW_FORM_data8:
21660 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21661 break;
21662
21663 case DW_FORM_sdata:
21664 case DW_FORM_implicit_const:
21665 *value = DW_SND (attr);
21666 break;
21667
21668 case DW_FORM_udata:
21669 *value = DW_UNSND (attr);
21670 break;
21671
21672 default:
21673 complaint (_("unsupported const value attribute form: '%s'"),
21674 dwarf_form_name (attr->form));
21675 *value = 0;
21676 break;
21677 }
21678 }
21679
21680
21681 /* Copy constant value from an attribute to a symbol. */
21682
21683 static void
21684 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21685 struct dwarf2_cu *cu)
21686 {
21687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21688 LONGEST value;
21689 const gdb_byte *bytes;
21690 struct dwarf2_locexpr_baton *baton;
21691
21692 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21693 sym->print_name (),
21694 &objfile->objfile_obstack, cu,
21695 &value, &bytes, &baton);
21696
21697 if (baton != NULL)
21698 {
21699 SYMBOL_LOCATION_BATON (sym) = baton;
21700 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21701 }
21702 else if (bytes != NULL)
21703 {
21704 SYMBOL_VALUE_BYTES (sym) = bytes;
21705 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21706 }
21707 else
21708 {
21709 SYMBOL_VALUE (sym) = value;
21710 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21711 }
21712 }
21713
21714 /* Return the type of the die in question using its DW_AT_type attribute. */
21715
21716 static struct type *
21717 die_type (struct die_info *die, struct dwarf2_cu *cu)
21718 {
21719 struct attribute *type_attr;
21720
21721 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21722 if (!type_attr)
21723 {
21724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21725 /* A missing DW_AT_type represents a void type. */
21726 return objfile_type (objfile)->builtin_void;
21727 }
21728
21729 return lookup_die_type (die, type_attr, cu);
21730 }
21731
21732 /* True iff CU's producer generates GNAT Ada auxiliary information
21733 that allows to find parallel types through that information instead
21734 of having to do expensive parallel lookups by type name. */
21735
21736 static int
21737 need_gnat_info (struct dwarf2_cu *cu)
21738 {
21739 /* Assume that the Ada compiler was GNAT, which always produces
21740 the auxiliary information. */
21741 return (cu->language == language_ada);
21742 }
21743
21744 /* Return the auxiliary type of the die in question using its
21745 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21746 attribute is not present. */
21747
21748 static struct type *
21749 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21750 {
21751 struct attribute *type_attr;
21752
21753 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21754 if (!type_attr)
21755 return NULL;
21756
21757 return lookup_die_type (die, type_attr, cu);
21758 }
21759
21760 /* If DIE has a descriptive_type attribute, then set the TYPE's
21761 descriptive type accordingly. */
21762
21763 static void
21764 set_descriptive_type (struct type *type, struct die_info *die,
21765 struct dwarf2_cu *cu)
21766 {
21767 struct type *descriptive_type = die_descriptive_type (die, cu);
21768
21769 if (descriptive_type)
21770 {
21771 ALLOCATE_GNAT_AUX_TYPE (type);
21772 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21773 }
21774 }
21775
21776 /* Return the containing type of the die in question using its
21777 DW_AT_containing_type attribute. */
21778
21779 static struct type *
21780 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21781 {
21782 struct attribute *type_attr;
21783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21784
21785 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21786 if (!type_attr)
21787 error (_("Dwarf Error: Problem turning containing type into gdb type "
21788 "[in module %s]"), objfile_name (objfile));
21789
21790 return lookup_die_type (die, type_attr, cu);
21791 }
21792
21793 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21794
21795 static struct type *
21796 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21797 {
21798 struct dwarf2_per_objfile *dwarf2_per_objfile
21799 = cu->per_cu->dwarf2_per_objfile;
21800 struct objfile *objfile = dwarf2_per_objfile->objfile;
21801 char *saved;
21802
21803 std::string message
21804 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21805 objfile_name (objfile),
21806 sect_offset_str (cu->header.sect_off),
21807 sect_offset_str (die->sect_off));
21808 saved = obstack_strdup (&objfile->objfile_obstack, message);
21809
21810 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21811 }
21812
21813 /* Look up the type of DIE in CU using its type attribute ATTR.
21814 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21815 DW_AT_containing_type.
21816 If there is no type substitute an error marker. */
21817
21818 static struct type *
21819 lookup_die_type (struct die_info *die, const struct attribute *attr,
21820 struct dwarf2_cu *cu)
21821 {
21822 struct dwarf2_per_objfile *dwarf2_per_objfile
21823 = cu->per_cu->dwarf2_per_objfile;
21824 struct objfile *objfile = dwarf2_per_objfile->objfile;
21825 struct type *this_type;
21826
21827 gdb_assert (attr->name == DW_AT_type
21828 || attr->name == DW_AT_GNAT_descriptive_type
21829 || attr->name == DW_AT_containing_type);
21830
21831 /* First see if we have it cached. */
21832
21833 if (attr->form == DW_FORM_GNU_ref_alt)
21834 {
21835 struct dwarf2_per_cu_data *per_cu;
21836 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21837
21838 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21839 dwarf2_per_objfile);
21840 this_type = get_die_type_at_offset (sect_off, per_cu);
21841 }
21842 else if (attr->form_is_ref ())
21843 {
21844 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21845
21846 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21847 }
21848 else if (attr->form == DW_FORM_ref_sig8)
21849 {
21850 ULONGEST signature = DW_SIGNATURE (attr);
21851
21852 return get_signatured_type (die, signature, cu);
21853 }
21854 else
21855 {
21856 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21857 " at %s [in module %s]"),
21858 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21859 objfile_name (objfile));
21860 return build_error_marker_type (cu, die);
21861 }
21862
21863 /* If not cached we need to read it in. */
21864
21865 if (this_type == NULL)
21866 {
21867 struct die_info *type_die = NULL;
21868 struct dwarf2_cu *type_cu = cu;
21869
21870 if (attr->form_is_ref ())
21871 type_die = follow_die_ref (die, attr, &type_cu);
21872 if (type_die == NULL)
21873 return build_error_marker_type (cu, die);
21874 /* If we find the type now, it's probably because the type came
21875 from an inter-CU reference and the type's CU got expanded before
21876 ours. */
21877 this_type = read_type_die (type_die, type_cu);
21878 }
21879
21880 /* If we still don't have a type use an error marker. */
21881
21882 if (this_type == NULL)
21883 return build_error_marker_type (cu, die);
21884
21885 return this_type;
21886 }
21887
21888 /* Return the type in DIE, CU.
21889 Returns NULL for invalid types.
21890
21891 This first does a lookup in die_type_hash,
21892 and only reads the die in if necessary.
21893
21894 NOTE: This can be called when reading in partial or full symbols. */
21895
21896 static struct type *
21897 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21898 {
21899 struct type *this_type;
21900
21901 this_type = get_die_type (die, cu);
21902 if (this_type)
21903 return this_type;
21904
21905 return read_type_die_1 (die, cu);
21906 }
21907
21908 /* Read the type in DIE, CU.
21909 Returns NULL for invalid types. */
21910
21911 static struct type *
21912 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21913 {
21914 struct type *this_type = NULL;
21915
21916 switch (die->tag)
21917 {
21918 case DW_TAG_class_type:
21919 case DW_TAG_interface_type:
21920 case DW_TAG_structure_type:
21921 case DW_TAG_union_type:
21922 this_type = read_structure_type (die, cu);
21923 break;
21924 case DW_TAG_enumeration_type:
21925 this_type = read_enumeration_type (die, cu);
21926 break;
21927 case DW_TAG_subprogram:
21928 case DW_TAG_subroutine_type:
21929 case DW_TAG_inlined_subroutine:
21930 this_type = read_subroutine_type (die, cu);
21931 break;
21932 case DW_TAG_array_type:
21933 this_type = read_array_type (die, cu);
21934 break;
21935 case DW_TAG_set_type:
21936 this_type = read_set_type (die, cu);
21937 break;
21938 case DW_TAG_pointer_type:
21939 this_type = read_tag_pointer_type (die, cu);
21940 break;
21941 case DW_TAG_ptr_to_member_type:
21942 this_type = read_tag_ptr_to_member_type (die, cu);
21943 break;
21944 case DW_TAG_reference_type:
21945 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21946 break;
21947 case DW_TAG_rvalue_reference_type:
21948 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21949 break;
21950 case DW_TAG_const_type:
21951 this_type = read_tag_const_type (die, cu);
21952 break;
21953 case DW_TAG_volatile_type:
21954 this_type = read_tag_volatile_type (die, cu);
21955 break;
21956 case DW_TAG_restrict_type:
21957 this_type = read_tag_restrict_type (die, cu);
21958 break;
21959 case DW_TAG_string_type:
21960 this_type = read_tag_string_type (die, cu);
21961 break;
21962 case DW_TAG_typedef:
21963 this_type = read_typedef (die, cu);
21964 break;
21965 case DW_TAG_subrange_type:
21966 this_type = read_subrange_type (die, cu);
21967 break;
21968 case DW_TAG_base_type:
21969 this_type = read_base_type (die, cu);
21970 break;
21971 case DW_TAG_unspecified_type:
21972 this_type = read_unspecified_type (die, cu);
21973 break;
21974 case DW_TAG_namespace:
21975 this_type = read_namespace_type (die, cu);
21976 break;
21977 case DW_TAG_module:
21978 this_type = read_module_type (die, cu);
21979 break;
21980 case DW_TAG_atomic_type:
21981 this_type = read_tag_atomic_type (die, cu);
21982 break;
21983 default:
21984 complaint (_("unexpected tag in read_type_die: '%s'"),
21985 dwarf_tag_name (die->tag));
21986 break;
21987 }
21988
21989 return this_type;
21990 }
21991
21992 /* See if we can figure out if the class lives in a namespace. We do
21993 this by looking for a member function; its demangled name will
21994 contain namespace info, if there is any.
21995 Return the computed name or NULL.
21996 Space for the result is allocated on the objfile's obstack.
21997 This is the full-die version of guess_partial_die_structure_name.
21998 In this case we know DIE has no useful parent. */
21999
22000 static const char *
22001 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22002 {
22003 struct die_info *spec_die;
22004 struct dwarf2_cu *spec_cu;
22005 struct die_info *child;
22006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22007
22008 spec_cu = cu;
22009 spec_die = die_specification (die, &spec_cu);
22010 if (spec_die != NULL)
22011 {
22012 die = spec_die;
22013 cu = spec_cu;
22014 }
22015
22016 for (child = die->child;
22017 child != NULL;
22018 child = child->sibling)
22019 {
22020 if (child->tag == DW_TAG_subprogram)
22021 {
22022 const char *linkage_name = dw2_linkage_name (child, cu);
22023
22024 if (linkage_name != NULL)
22025 {
22026 gdb::unique_xmalloc_ptr<char> actual_name
22027 (language_class_name_from_physname (cu->language_defn,
22028 linkage_name));
22029 const char *name = NULL;
22030
22031 if (actual_name != NULL)
22032 {
22033 const char *die_name = dwarf2_name (die, cu);
22034
22035 if (die_name != NULL
22036 && strcmp (die_name, actual_name.get ()) != 0)
22037 {
22038 /* Strip off the class name from the full name.
22039 We want the prefix. */
22040 int die_name_len = strlen (die_name);
22041 int actual_name_len = strlen (actual_name.get ());
22042 const char *ptr = actual_name.get ();
22043
22044 /* Test for '::' as a sanity check. */
22045 if (actual_name_len > die_name_len + 2
22046 && ptr[actual_name_len - die_name_len - 1] == ':')
22047 name = obstack_strndup (
22048 &objfile->per_bfd->storage_obstack,
22049 ptr, actual_name_len - die_name_len - 2);
22050 }
22051 }
22052 return name;
22053 }
22054 }
22055 }
22056
22057 return NULL;
22058 }
22059
22060 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22061 prefix part in such case. See
22062 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22063
22064 static const char *
22065 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22066 {
22067 struct attribute *attr;
22068 const char *base;
22069
22070 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22071 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22072 return NULL;
22073
22074 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22075 return NULL;
22076
22077 attr = dw2_linkage_name_attr (die, cu);
22078 if (attr == NULL || DW_STRING (attr) == NULL)
22079 return NULL;
22080
22081 /* dwarf2_name had to be already called. */
22082 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22083
22084 /* Strip the base name, keep any leading namespaces/classes. */
22085 base = strrchr (DW_STRING (attr), ':');
22086 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22087 return "";
22088
22089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22090 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22091 DW_STRING (attr),
22092 &base[-1] - DW_STRING (attr));
22093 }
22094
22095 /* Return the name of the namespace/class that DIE is defined within,
22096 or "" if we can't tell. The caller should not xfree the result.
22097
22098 For example, if we're within the method foo() in the following
22099 code:
22100
22101 namespace N {
22102 class C {
22103 void foo () {
22104 }
22105 };
22106 }
22107
22108 then determine_prefix on foo's die will return "N::C". */
22109
22110 static const char *
22111 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22112 {
22113 struct dwarf2_per_objfile *dwarf2_per_objfile
22114 = cu->per_cu->dwarf2_per_objfile;
22115 struct die_info *parent, *spec_die;
22116 struct dwarf2_cu *spec_cu;
22117 struct type *parent_type;
22118 const char *retval;
22119
22120 if (cu->language != language_cplus
22121 && cu->language != language_fortran && cu->language != language_d
22122 && cu->language != language_rust)
22123 return "";
22124
22125 retval = anonymous_struct_prefix (die, cu);
22126 if (retval)
22127 return retval;
22128
22129 /* We have to be careful in the presence of DW_AT_specification.
22130 For example, with GCC 3.4, given the code
22131
22132 namespace N {
22133 void foo() {
22134 // Definition of N::foo.
22135 }
22136 }
22137
22138 then we'll have a tree of DIEs like this:
22139
22140 1: DW_TAG_compile_unit
22141 2: DW_TAG_namespace // N
22142 3: DW_TAG_subprogram // declaration of N::foo
22143 4: DW_TAG_subprogram // definition of N::foo
22144 DW_AT_specification // refers to die #3
22145
22146 Thus, when processing die #4, we have to pretend that we're in
22147 the context of its DW_AT_specification, namely the contex of die
22148 #3. */
22149 spec_cu = cu;
22150 spec_die = die_specification (die, &spec_cu);
22151 if (spec_die == NULL)
22152 parent = die->parent;
22153 else
22154 {
22155 parent = spec_die->parent;
22156 cu = spec_cu;
22157 }
22158
22159 if (parent == NULL)
22160 return "";
22161 else if (parent->building_fullname)
22162 {
22163 const char *name;
22164 const char *parent_name;
22165
22166 /* It has been seen on RealView 2.2 built binaries,
22167 DW_TAG_template_type_param types actually _defined_ as
22168 children of the parent class:
22169
22170 enum E {};
22171 template class <class Enum> Class{};
22172 Class<enum E> class_e;
22173
22174 1: DW_TAG_class_type (Class)
22175 2: DW_TAG_enumeration_type (E)
22176 3: DW_TAG_enumerator (enum1:0)
22177 3: DW_TAG_enumerator (enum2:1)
22178 ...
22179 2: DW_TAG_template_type_param
22180 DW_AT_type DW_FORM_ref_udata (E)
22181
22182 Besides being broken debug info, it can put GDB into an
22183 infinite loop. Consider:
22184
22185 When we're building the full name for Class<E>, we'll start
22186 at Class, and go look over its template type parameters,
22187 finding E. We'll then try to build the full name of E, and
22188 reach here. We're now trying to build the full name of E,
22189 and look over the parent DIE for containing scope. In the
22190 broken case, if we followed the parent DIE of E, we'd again
22191 find Class, and once again go look at its template type
22192 arguments, etc., etc. Simply don't consider such parent die
22193 as source-level parent of this die (it can't be, the language
22194 doesn't allow it), and break the loop here. */
22195 name = dwarf2_name (die, cu);
22196 parent_name = dwarf2_name (parent, cu);
22197 complaint (_("template param type '%s' defined within parent '%s'"),
22198 name ? name : "<unknown>",
22199 parent_name ? parent_name : "<unknown>");
22200 return "";
22201 }
22202 else
22203 switch (parent->tag)
22204 {
22205 case DW_TAG_namespace:
22206 parent_type = read_type_die (parent, cu);
22207 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22208 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22209 Work around this problem here. */
22210 if (cu->language == language_cplus
22211 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22212 return "";
22213 /* We give a name to even anonymous namespaces. */
22214 return TYPE_NAME (parent_type);
22215 case DW_TAG_class_type:
22216 case DW_TAG_interface_type:
22217 case DW_TAG_structure_type:
22218 case DW_TAG_union_type:
22219 case DW_TAG_module:
22220 parent_type = read_type_die (parent, cu);
22221 if (TYPE_NAME (parent_type) != NULL)
22222 return TYPE_NAME (parent_type);
22223 else
22224 /* An anonymous structure is only allowed non-static data
22225 members; no typedefs, no member functions, et cetera.
22226 So it does not need a prefix. */
22227 return "";
22228 case DW_TAG_compile_unit:
22229 case DW_TAG_partial_unit:
22230 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22231 if (cu->language == language_cplus
22232 && !dwarf2_per_objfile->types.empty ()
22233 && die->child != NULL
22234 && (die->tag == DW_TAG_class_type
22235 || die->tag == DW_TAG_structure_type
22236 || die->tag == DW_TAG_union_type))
22237 {
22238 const char *name = guess_full_die_structure_name (die, cu);
22239 if (name != NULL)
22240 return name;
22241 }
22242 return "";
22243 case DW_TAG_subprogram:
22244 /* Nested subroutines in Fortran get a prefix with the name
22245 of the parent's subroutine. */
22246 if (cu->language == language_fortran)
22247 {
22248 if ((die->tag == DW_TAG_subprogram)
22249 && (dwarf2_name (parent, cu) != NULL))
22250 return dwarf2_name (parent, cu);
22251 }
22252 return determine_prefix (parent, cu);
22253 case DW_TAG_enumeration_type:
22254 parent_type = read_type_die (parent, cu);
22255 if (TYPE_DECLARED_CLASS (parent_type))
22256 {
22257 if (TYPE_NAME (parent_type) != NULL)
22258 return TYPE_NAME (parent_type);
22259 return "";
22260 }
22261 /* Fall through. */
22262 default:
22263 return determine_prefix (parent, cu);
22264 }
22265 }
22266
22267 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22268 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22269 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22270 an obconcat, otherwise allocate storage for the result. The CU argument is
22271 used to determine the language and hence, the appropriate separator. */
22272
22273 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22274
22275 static char *
22276 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22277 int physname, struct dwarf2_cu *cu)
22278 {
22279 const char *lead = "";
22280 const char *sep;
22281
22282 if (suffix == NULL || suffix[0] == '\0'
22283 || prefix == NULL || prefix[0] == '\0')
22284 sep = "";
22285 else if (cu->language == language_d)
22286 {
22287 /* For D, the 'main' function could be defined in any module, but it
22288 should never be prefixed. */
22289 if (strcmp (suffix, "D main") == 0)
22290 {
22291 prefix = "";
22292 sep = "";
22293 }
22294 else
22295 sep = ".";
22296 }
22297 else if (cu->language == language_fortran && physname)
22298 {
22299 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22300 DW_AT_MIPS_linkage_name is preferred and used instead. */
22301
22302 lead = "__";
22303 sep = "_MOD_";
22304 }
22305 else
22306 sep = "::";
22307
22308 if (prefix == NULL)
22309 prefix = "";
22310 if (suffix == NULL)
22311 suffix = "";
22312
22313 if (obs == NULL)
22314 {
22315 char *retval
22316 = ((char *)
22317 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22318
22319 strcpy (retval, lead);
22320 strcat (retval, prefix);
22321 strcat (retval, sep);
22322 strcat (retval, suffix);
22323 return retval;
22324 }
22325 else
22326 {
22327 /* We have an obstack. */
22328 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22329 }
22330 }
22331
22332 /* Return sibling of die, NULL if no sibling. */
22333
22334 static struct die_info *
22335 sibling_die (struct die_info *die)
22336 {
22337 return die->sibling;
22338 }
22339
22340 /* Get name of a die, return NULL if not found. */
22341
22342 static const char *
22343 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22344 struct obstack *obstack)
22345 {
22346 if (name && cu->language == language_cplus)
22347 {
22348 std::string canon_name = cp_canonicalize_string (name);
22349
22350 if (!canon_name.empty ())
22351 {
22352 if (canon_name != name)
22353 name = obstack_strdup (obstack, canon_name);
22354 }
22355 }
22356
22357 return name;
22358 }
22359
22360 /* Get name of a die, return NULL if not found.
22361 Anonymous namespaces are converted to their magic string. */
22362
22363 static const char *
22364 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22365 {
22366 struct attribute *attr;
22367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22368
22369 attr = dwarf2_attr (die, DW_AT_name, cu);
22370 if ((!attr || !DW_STRING (attr))
22371 && die->tag != DW_TAG_namespace
22372 && die->tag != DW_TAG_class_type
22373 && die->tag != DW_TAG_interface_type
22374 && die->tag != DW_TAG_structure_type
22375 && die->tag != DW_TAG_union_type)
22376 return NULL;
22377
22378 switch (die->tag)
22379 {
22380 case DW_TAG_compile_unit:
22381 case DW_TAG_partial_unit:
22382 /* Compilation units have a DW_AT_name that is a filename, not
22383 a source language identifier. */
22384 case DW_TAG_enumeration_type:
22385 case DW_TAG_enumerator:
22386 /* These tags always have simple identifiers already; no need
22387 to canonicalize them. */
22388 return DW_STRING (attr);
22389
22390 case DW_TAG_namespace:
22391 if (attr != NULL && DW_STRING (attr) != NULL)
22392 return DW_STRING (attr);
22393 return CP_ANONYMOUS_NAMESPACE_STR;
22394
22395 case DW_TAG_class_type:
22396 case DW_TAG_interface_type:
22397 case DW_TAG_structure_type:
22398 case DW_TAG_union_type:
22399 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22400 structures or unions. These were of the form "._%d" in GCC 4.1,
22401 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22402 and GCC 4.4. We work around this problem by ignoring these. */
22403 if (attr && DW_STRING (attr)
22404 && (startswith (DW_STRING (attr), "._")
22405 || startswith (DW_STRING (attr), "<anonymous")))
22406 return NULL;
22407
22408 /* GCC might emit a nameless typedef that has a linkage name. See
22409 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22410 if (!attr || DW_STRING (attr) == NULL)
22411 {
22412 attr = dw2_linkage_name_attr (die, cu);
22413 if (attr == NULL || DW_STRING (attr) == NULL)
22414 return NULL;
22415
22416 /* Avoid demangling DW_STRING (attr) the second time on a second
22417 call for the same DIE. */
22418 if (!DW_STRING_IS_CANONICAL (attr))
22419 {
22420 gdb::unique_xmalloc_ptr<char> demangled
22421 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22422
22423 const char *base;
22424
22425 /* FIXME: we already did this for the partial symbol... */
22426 DW_STRING (attr)
22427 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22428 demangled.get ());
22429 DW_STRING_IS_CANONICAL (attr) = 1;
22430
22431 /* Strip any leading namespaces/classes, keep only the base name.
22432 DW_AT_name for named DIEs does not contain the prefixes. */
22433 base = strrchr (DW_STRING (attr), ':');
22434 if (base && base > DW_STRING (attr) && base[-1] == ':')
22435 return &base[1];
22436 else
22437 return DW_STRING (attr);
22438 }
22439 }
22440 break;
22441
22442 default:
22443 break;
22444 }
22445
22446 if (!DW_STRING_IS_CANONICAL (attr))
22447 {
22448 DW_STRING (attr)
22449 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22450 &objfile->per_bfd->storage_obstack);
22451 DW_STRING_IS_CANONICAL (attr) = 1;
22452 }
22453 return DW_STRING (attr);
22454 }
22455
22456 /* Return the die that this die in an extension of, or NULL if there
22457 is none. *EXT_CU is the CU containing DIE on input, and the CU
22458 containing the return value on output. */
22459
22460 static struct die_info *
22461 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22462 {
22463 struct attribute *attr;
22464
22465 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22466 if (attr == NULL)
22467 return NULL;
22468
22469 return follow_die_ref (die, attr, ext_cu);
22470 }
22471
22472 /* A convenience function that returns an "unknown" DWARF name,
22473 including the value of V. STR is the name of the entity being
22474 printed, e.g., "TAG". */
22475
22476 static const char *
22477 dwarf_unknown (const char *str, unsigned v)
22478 {
22479 char *cell = get_print_cell ();
22480 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22481 return cell;
22482 }
22483
22484 /* Convert a DIE tag into its string name. */
22485
22486 static const char *
22487 dwarf_tag_name (unsigned tag)
22488 {
22489 const char *name = get_DW_TAG_name (tag);
22490
22491 if (name == NULL)
22492 return dwarf_unknown ("TAG", tag);
22493
22494 return name;
22495 }
22496
22497 /* Convert a DWARF attribute code into its string name. */
22498
22499 static const char *
22500 dwarf_attr_name (unsigned attr)
22501 {
22502 const char *name;
22503
22504 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22505 if (attr == DW_AT_MIPS_fde)
22506 return "DW_AT_MIPS_fde";
22507 #else
22508 if (attr == DW_AT_HP_block_index)
22509 return "DW_AT_HP_block_index";
22510 #endif
22511
22512 name = get_DW_AT_name (attr);
22513
22514 if (name == NULL)
22515 return dwarf_unknown ("AT", attr);
22516
22517 return name;
22518 }
22519
22520 /* Convert a unit type to corresponding DW_UT name. */
22521
22522 static const char *
22523 dwarf_unit_type_name (int unit_type) {
22524 switch (unit_type)
22525 {
22526 case 0x01:
22527 return "DW_UT_compile (0x01)";
22528 case 0x02:
22529 return "DW_UT_type (0x02)";
22530 case 0x03:
22531 return "DW_UT_partial (0x03)";
22532 case 0x04:
22533 return "DW_UT_skeleton (0x04)";
22534 case 0x05:
22535 return "DW_UT_split_compile (0x05)";
22536 case 0x06:
22537 return "DW_UT_split_type (0x06)";
22538 case 0x80:
22539 return "DW_UT_lo_user (0x80)";
22540 case 0xff:
22541 return "DW_UT_hi_user (0xff)";
22542 default:
22543 return nullptr;
22544 }
22545 }
22546
22547 /* Convert a DWARF value form code into its string name. */
22548
22549 static const char *
22550 dwarf_form_name (unsigned form)
22551 {
22552 const char *name = get_DW_FORM_name (form);
22553
22554 if (name == NULL)
22555 return dwarf_unknown ("FORM", form);
22556
22557 return name;
22558 }
22559
22560 static const char *
22561 dwarf_bool_name (unsigned mybool)
22562 {
22563 if (mybool)
22564 return "TRUE";
22565 else
22566 return "FALSE";
22567 }
22568
22569 /* Convert a DWARF type code into its string name. */
22570
22571 static const char *
22572 dwarf_type_encoding_name (unsigned enc)
22573 {
22574 const char *name = get_DW_ATE_name (enc);
22575
22576 if (name == NULL)
22577 return dwarf_unknown ("ATE", enc);
22578
22579 return name;
22580 }
22581
22582 static void
22583 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22584 {
22585 unsigned int i;
22586
22587 print_spaces (indent, f);
22588 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22589 dwarf_tag_name (die->tag), die->abbrev,
22590 sect_offset_str (die->sect_off));
22591
22592 if (die->parent != NULL)
22593 {
22594 print_spaces (indent, f);
22595 fprintf_unfiltered (f, " parent at offset: %s\n",
22596 sect_offset_str (die->parent->sect_off));
22597 }
22598
22599 print_spaces (indent, f);
22600 fprintf_unfiltered (f, " has children: %s\n",
22601 dwarf_bool_name (die->child != NULL));
22602
22603 print_spaces (indent, f);
22604 fprintf_unfiltered (f, " attributes:\n");
22605
22606 for (i = 0; i < die->num_attrs; ++i)
22607 {
22608 print_spaces (indent, f);
22609 fprintf_unfiltered (f, " %s (%s) ",
22610 dwarf_attr_name (die->attrs[i].name),
22611 dwarf_form_name (die->attrs[i].form));
22612
22613 switch (die->attrs[i].form)
22614 {
22615 case DW_FORM_addr:
22616 case DW_FORM_addrx:
22617 case DW_FORM_GNU_addr_index:
22618 fprintf_unfiltered (f, "address: ");
22619 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22620 break;
22621 case DW_FORM_block2:
22622 case DW_FORM_block4:
22623 case DW_FORM_block:
22624 case DW_FORM_block1:
22625 fprintf_unfiltered (f, "block: size %s",
22626 pulongest (DW_BLOCK (&die->attrs[i])->size));
22627 break;
22628 case DW_FORM_exprloc:
22629 fprintf_unfiltered (f, "expression: size %s",
22630 pulongest (DW_BLOCK (&die->attrs[i])->size));
22631 break;
22632 case DW_FORM_data16:
22633 fprintf_unfiltered (f, "constant of 16 bytes");
22634 break;
22635 case DW_FORM_ref_addr:
22636 fprintf_unfiltered (f, "ref address: ");
22637 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22638 break;
22639 case DW_FORM_GNU_ref_alt:
22640 fprintf_unfiltered (f, "alt ref address: ");
22641 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22642 break;
22643 case DW_FORM_ref1:
22644 case DW_FORM_ref2:
22645 case DW_FORM_ref4:
22646 case DW_FORM_ref8:
22647 case DW_FORM_ref_udata:
22648 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22649 (long) (DW_UNSND (&die->attrs[i])));
22650 break;
22651 case DW_FORM_data1:
22652 case DW_FORM_data2:
22653 case DW_FORM_data4:
22654 case DW_FORM_data8:
22655 case DW_FORM_udata:
22656 case DW_FORM_sdata:
22657 fprintf_unfiltered (f, "constant: %s",
22658 pulongest (DW_UNSND (&die->attrs[i])));
22659 break;
22660 case DW_FORM_sec_offset:
22661 fprintf_unfiltered (f, "section offset: %s",
22662 pulongest (DW_UNSND (&die->attrs[i])));
22663 break;
22664 case DW_FORM_ref_sig8:
22665 fprintf_unfiltered (f, "signature: %s",
22666 hex_string (DW_SIGNATURE (&die->attrs[i])));
22667 break;
22668 case DW_FORM_string:
22669 case DW_FORM_strp:
22670 case DW_FORM_line_strp:
22671 case DW_FORM_strx:
22672 case DW_FORM_GNU_str_index:
22673 case DW_FORM_GNU_strp_alt:
22674 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22675 DW_STRING (&die->attrs[i])
22676 ? DW_STRING (&die->attrs[i]) : "",
22677 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22678 break;
22679 case DW_FORM_flag:
22680 if (DW_UNSND (&die->attrs[i]))
22681 fprintf_unfiltered (f, "flag: TRUE");
22682 else
22683 fprintf_unfiltered (f, "flag: FALSE");
22684 break;
22685 case DW_FORM_flag_present:
22686 fprintf_unfiltered (f, "flag: TRUE");
22687 break;
22688 case DW_FORM_indirect:
22689 /* The reader will have reduced the indirect form to
22690 the "base form" so this form should not occur. */
22691 fprintf_unfiltered (f,
22692 "unexpected attribute form: DW_FORM_indirect");
22693 break;
22694 case DW_FORM_implicit_const:
22695 fprintf_unfiltered (f, "constant: %s",
22696 plongest (DW_SND (&die->attrs[i])));
22697 break;
22698 default:
22699 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22700 die->attrs[i].form);
22701 break;
22702 }
22703 fprintf_unfiltered (f, "\n");
22704 }
22705 }
22706
22707 static void
22708 dump_die_for_error (struct die_info *die)
22709 {
22710 dump_die_shallow (gdb_stderr, 0, die);
22711 }
22712
22713 static void
22714 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22715 {
22716 int indent = level * 4;
22717
22718 gdb_assert (die != NULL);
22719
22720 if (level >= max_level)
22721 return;
22722
22723 dump_die_shallow (f, indent, die);
22724
22725 if (die->child != NULL)
22726 {
22727 print_spaces (indent, f);
22728 fprintf_unfiltered (f, " Children:");
22729 if (level + 1 < max_level)
22730 {
22731 fprintf_unfiltered (f, "\n");
22732 dump_die_1 (f, level + 1, max_level, die->child);
22733 }
22734 else
22735 {
22736 fprintf_unfiltered (f,
22737 " [not printed, max nesting level reached]\n");
22738 }
22739 }
22740
22741 if (die->sibling != NULL && level > 0)
22742 {
22743 dump_die_1 (f, level, max_level, die->sibling);
22744 }
22745 }
22746
22747 /* This is called from the pdie macro in gdbinit.in.
22748 It's not static so gcc will keep a copy callable from gdb. */
22749
22750 void
22751 dump_die (struct die_info *die, int max_level)
22752 {
22753 dump_die_1 (gdb_stdlog, 0, max_level, die);
22754 }
22755
22756 static void
22757 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22758 {
22759 void **slot;
22760
22761 slot = htab_find_slot_with_hash (cu->die_hash, die,
22762 to_underlying (die->sect_off),
22763 INSERT);
22764
22765 *slot = die;
22766 }
22767
22768 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22769 required kind. */
22770
22771 static sect_offset
22772 dwarf2_get_ref_die_offset (const struct attribute *attr)
22773 {
22774 if (attr->form_is_ref ())
22775 return (sect_offset) DW_UNSND (attr);
22776
22777 complaint (_("unsupported die ref attribute form: '%s'"),
22778 dwarf_form_name (attr->form));
22779 return {};
22780 }
22781
22782 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22783 * the value held by the attribute is not constant. */
22784
22785 static LONGEST
22786 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22787 {
22788 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22789 return DW_SND (attr);
22790 else if (attr->form == DW_FORM_udata
22791 || attr->form == DW_FORM_data1
22792 || attr->form == DW_FORM_data2
22793 || attr->form == DW_FORM_data4
22794 || attr->form == DW_FORM_data8)
22795 return DW_UNSND (attr);
22796 else
22797 {
22798 /* For DW_FORM_data16 see attribute::form_is_constant. */
22799 complaint (_("Attribute value is not a constant (%s)"),
22800 dwarf_form_name (attr->form));
22801 return default_value;
22802 }
22803 }
22804
22805 /* Follow reference or signature attribute ATTR of SRC_DIE.
22806 On entry *REF_CU is the CU of SRC_DIE.
22807 On exit *REF_CU is the CU of the result. */
22808
22809 static struct die_info *
22810 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22811 struct dwarf2_cu **ref_cu)
22812 {
22813 struct die_info *die;
22814
22815 if (attr->form_is_ref ())
22816 die = follow_die_ref (src_die, attr, ref_cu);
22817 else if (attr->form == DW_FORM_ref_sig8)
22818 die = follow_die_sig (src_die, attr, ref_cu);
22819 else
22820 {
22821 dump_die_for_error (src_die);
22822 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22823 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22824 }
22825
22826 return die;
22827 }
22828
22829 /* Follow reference OFFSET.
22830 On entry *REF_CU is the CU of the source die referencing OFFSET.
22831 On exit *REF_CU is the CU of the result.
22832 Returns NULL if OFFSET is invalid. */
22833
22834 static struct die_info *
22835 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22836 struct dwarf2_cu **ref_cu)
22837 {
22838 struct die_info temp_die;
22839 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22840 struct dwarf2_per_objfile *dwarf2_per_objfile
22841 = cu->per_cu->dwarf2_per_objfile;
22842
22843 gdb_assert (cu->per_cu != NULL);
22844
22845 target_cu = cu;
22846
22847 if (cu->per_cu->is_debug_types)
22848 {
22849 /* .debug_types CUs cannot reference anything outside their CU.
22850 If they need to, they have to reference a signatured type via
22851 DW_FORM_ref_sig8. */
22852 if (!offset_in_cu_p (&cu->header, sect_off))
22853 return NULL;
22854 }
22855 else if (offset_in_dwz != cu->per_cu->is_dwz
22856 || !offset_in_cu_p (&cu->header, sect_off))
22857 {
22858 struct dwarf2_per_cu_data *per_cu;
22859
22860 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22861 dwarf2_per_objfile);
22862
22863 /* If necessary, add it to the queue and load its DIEs. */
22864 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22865 load_full_comp_unit (per_cu, false, cu->language);
22866
22867 target_cu = per_cu->cu;
22868 }
22869 else if (cu->dies == NULL)
22870 {
22871 /* We're loading full DIEs during partial symbol reading. */
22872 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22873 load_full_comp_unit (cu->per_cu, false, language_minimal);
22874 }
22875
22876 *ref_cu = target_cu;
22877 temp_die.sect_off = sect_off;
22878
22879 if (target_cu != cu)
22880 target_cu->ancestor = cu;
22881
22882 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22883 &temp_die,
22884 to_underlying (sect_off));
22885 }
22886
22887 /* Follow reference attribute ATTR of SRC_DIE.
22888 On entry *REF_CU is the CU of SRC_DIE.
22889 On exit *REF_CU is the CU of the result. */
22890
22891 static struct die_info *
22892 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22893 struct dwarf2_cu **ref_cu)
22894 {
22895 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22896 struct dwarf2_cu *cu = *ref_cu;
22897 struct die_info *die;
22898
22899 die = follow_die_offset (sect_off,
22900 (attr->form == DW_FORM_GNU_ref_alt
22901 || cu->per_cu->is_dwz),
22902 ref_cu);
22903 if (!die)
22904 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22905 "at %s [in module %s]"),
22906 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22907 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22908
22909 return die;
22910 }
22911
22912 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22913 Returned value is intended for DW_OP_call*. Returned
22914 dwarf2_locexpr_baton->data has lifetime of
22915 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22916
22917 struct dwarf2_locexpr_baton
22918 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22919 struct dwarf2_per_cu_data *per_cu,
22920 CORE_ADDR (*get_frame_pc) (void *baton),
22921 void *baton, bool resolve_abstract_p)
22922 {
22923 struct dwarf2_cu *cu;
22924 struct die_info *die;
22925 struct attribute *attr;
22926 struct dwarf2_locexpr_baton retval;
22927 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22928 struct objfile *objfile = dwarf2_per_objfile->objfile;
22929
22930 if (per_cu->cu == NULL)
22931 load_cu (per_cu, false);
22932 cu = per_cu->cu;
22933 if (cu == NULL)
22934 {
22935 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22936 Instead just throw an error, not much else we can do. */
22937 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22938 sect_offset_str (sect_off), objfile_name (objfile));
22939 }
22940
22941 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22942 if (!die)
22943 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22944 sect_offset_str (sect_off), objfile_name (objfile));
22945
22946 attr = dwarf2_attr (die, DW_AT_location, cu);
22947 if (!attr && resolve_abstract_p
22948 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22949 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22950 {
22951 CORE_ADDR pc = (*get_frame_pc) (baton);
22952 CORE_ADDR baseaddr = objfile->text_section_offset ();
22953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22954
22955 for (const auto &cand_off
22956 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22957 {
22958 struct dwarf2_cu *cand_cu = cu;
22959 struct die_info *cand
22960 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22961 if (!cand
22962 || !cand->parent
22963 || cand->parent->tag != DW_TAG_subprogram)
22964 continue;
22965
22966 CORE_ADDR pc_low, pc_high;
22967 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22968 if (pc_low == ((CORE_ADDR) -1))
22969 continue;
22970 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22971 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22972 if (!(pc_low <= pc && pc < pc_high))
22973 continue;
22974
22975 die = cand;
22976 attr = dwarf2_attr (die, DW_AT_location, cu);
22977 break;
22978 }
22979 }
22980
22981 if (!attr)
22982 {
22983 /* DWARF: "If there is no such attribute, then there is no effect.".
22984 DATA is ignored if SIZE is 0. */
22985
22986 retval.data = NULL;
22987 retval.size = 0;
22988 }
22989 else if (attr->form_is_section_offset ())
22990 {
22991 struct dwarf2_loclist_baton loclist_baton;
22992 CORE_ADDR pc = (*get_frame_pc) (baton);
22993 size_t size;
22994
22995 fill_in_loclist_baton (cu, &loclist_baton, attr);
22996
22997 retval.data = dwarf2_find_location_expression (&loclist_baton,
22998 &size, pc);
22999 retval.size = size;
23000 }
23001 else
23002 {
23003 if (!attr->form_is_block ())
23004 error (_("Dwarf Error: DIE at %s referenced in module %s "
23005 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23006 sect_offset_str (sect_off), objfile_name (objfile));
23007
23008 retval.data = DW_BLOCK (attr)->data;
23009 retval.size = DW_BLOCK (attr)->size;
23010 }
23011 retval.per_cu = cu->per_cu;
23012
23013 age_cached_comp_units (dwarf2_per_objfile);
23014
23015 return retval;
23016 }
23017
23018 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23019 offset. */
23020
23021 struct dwarf2_locexpr_baton
23022 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23023 struct dwarf2_per_cu_data *per_cu,
23024 CORE_ADDR (*get_frame_pc) (void *baton),
23025 void *baton)
23026 {
23027 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23028
23029 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23030 }
23031
23032 /* Write a constant of a given type as target-ordered bytes into
23033 OBSTACK. */
23034
23035 static const gdb_byte *
23036 write_constant_as_bytes (struct obstack *obstack,
23037 enum bfd_endian byte_order,
23038 struct type *type,
23039 ULONGEST value,
23040 LONGEST *len)
23041 {
23042 gdb_byte *result;
23043
23044 *len = TYPE_LENGTH (type);
23045 result = (gdb_byte *) obstack_alloc (obstack, *len);
23046 store_unsigned_integer (result, *len, byte_order, value);
23047
23048 return result;
23049 }
23050
23051 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23052 pointer to the constant bytes and set LEN to the length of the
23053 data. If memory is needed, allocate it on OBSTACK. If the DIE
23054 does not have a DW_AT_const_value, return NULL. */
23055
23056 const gdb_byte *
23057 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23058 struct dwarf2_per_cu_data *per_cu,
23059 struct obstack *obstack,
23060 LONGEST *len)
23061 {
23062 struct dwarf2_cu *cu;
23063 struct die_info *die;
23064 struct attribute *attr;
23065 const gdb_byte *result = NULL;
23066 struct type *type;
23067 LONGEST value;
23068 enum bfd_endian byte_order;
23069 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23070
23071 if (per_cu->cu == NULL)
23072 load_cu (per_cu, false);
23073 cu = per_cu->cu;
23074 if (cu == NULL)
23075 {
23076 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23077 Instead just throw an error, not much else we can do. */
23078 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23079 sect_offset_str (sect_off), objfile_name (objfile));
23080 }
23081
23082 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23083 if (!die)
23084 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23085 sect_offset_str (sect_off), objfile_name (objfile));
23086
23087 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23088 if (attr == NULL)
23089 return NULL;
23090
23091 byte_order = (bfd_big_endian (objfile->obfd)
23092 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23093
23094 switch (attr->form)
23095 {
23096 case DW_FORM_addr:
23097 case DW_FORM_addrx:
23098 case DW_FORM_GNU_addr_index:
23099 {
23100 gdb_byte *tem;
23101
23102 *len = cu->header.addr_size;
23103 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23104 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23105 result = tem;
23106 }
23107 break;
23108 case DW_FORM_string:
23109 case DW_FORM_strp:
23110 case DW_FORM_strx:
23111 case DW_FORM_GNU_str_index:
23112 case DW_FORM_GNU_strp_alt:
23113 /* DW_STRING is already allocated on the objfile obstack, point
23114 directly to it. */
23115 result = (const gdb_byte *) DW_STRING (attr);
23116 *len = strlen (DW_STRING (attr));
23117 break;
23118 case DW_FORM_block1:
23119 case DW_FORM_block2:
23120 case DW_FORM_block4:
23121 case DW_FORM_block:
23122 case DW_FORM_exprloc:
23123 case DW_FORM_data16:
23124 result = DW_BLOCK (attr)->data;
23125 *len = DW_BLOCK (attr)->size;
23126 break;
23127
23128 /* The DW_AT_const_value attributes are supposed to carry the
23129 symbol's value "represented as it would be on the target
23130 architecture." By the time we get here, it's already been
23131 converted to host endianness, so we just need to sign- or
23132 zero-extend it as appropriate. */
23133 case DW_FORM_data1:
23134 type = die_type (die, cu);
23135 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23136 if (result == NULL)
23137 result = write_constant_as_bytes (obstack, byte_order,
23138 type, value, len);
23139 break;
23140 case DW_FORM_data2:
23141 type = die_type (die, cu);
23142 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23143 if (result == NULL)
23144 result = write_constant_as_bytes (obstack, byte_order,
23145 type, value, len);
23146 break;
23147 case DW_FORM_data4:
23148 type = die_type (die, cu);
23149 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23150 if (result == NULL)
23151 result = write_constant_as_bytes (obstack, byte_order,
23152 type, value, len);
23153 break;
23154 case DW_FORM_data8:
23155 type = die_type (die, cu);
23156 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23157 if (result == NULL)
23158 result = write_constant_as_bytes (obstack, byte_order,
23159 type, value, len);
23160 break;
23161
23162 case DW_FORM_sdata:
23163 case DW_FORM_implicit_const:
23164 type = die_type (die, cu);
23165 result = write_constant_as_bytes (obstack, byte_order,
23166 type, DW_SND (attr), len);
23167 break;
23168
23169 case DW_FORM_udata:
23170 type = die_type (die, cu);
23171 result = write_constant_as_bytes (obstack, byte_order,
23172 type, DW_UNSND (attr), len);
23173 break;
23174
23175 default:
23176 complaint (_("unsupported const value attribute form: '%s'"),
23177 dwarf_form_name (attr->form));
23178 break;
23179 }
23180
23181 return result;
23182 }
23183
23184 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23185 valid type for this die is found. */
23186
23187 struct type *
23188 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23189 struct dwarf2_per_cu_data *per_cu)
23190 {
23191 struct dwarf2_cu *cu;
23192 struct die_info *die;
23193
23194 if (per_cu->cu == NULL)
23195 load_cu (per_cu, false);
23196 cu = per_cu->cu;
23197 if (!cu)
23198 return NULL;
23199
23200 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23201 if (!die)
23202 return NULL;
23203
23204 return die_type (die, cu);
23205 }
23206
23207 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23208 PER_CU. */
23209
23210 struct type *
23211 dwarf2_get_die_type (cu_offset die_offset,
23212 struct dwarf2_per_cu_data *per_cu)
23213 {
23214 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23215 return get_die_type_at_offset (die_offset_sect, per_cu);
23216 }
23217
23218 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23219 On entry *REF_CU is the CU of SRC_DIE.
23220 On exit *REF_CU is the CU of the result.
23221 Returns NULL if the referenced DIE isn't found. */
23222
23223 static struct die_info *
23224 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23225 struct dwarf2_cu **ref_cu)
23226 {
23227 struct die_info temp_die;
23228 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23229 struct die_info *die;
23230
23231 /* While it might be nice to assert sig_type->type == NULL here,
23232 we can get here for DW_AT_imported_declaration where we need
23233 the DIE not the type. */
23234
23235 /* If necessary, add it to the queue and load its DIEs. */
23236
23237 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23238 read_signatured_type (sig_type);
23239
23240 sig_cu = sig_type->per_cu.cu;
23241 gdb_assert (sig_cu != NULL);
23242 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23243 temp_die.sect_off = sig_type->type_offset_in_section;
23244 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23245 to_underlying (temp_die.sect_off));
23246 if (die)
23247 {
23248 struct dwarf2_per_objfile *dwarf2_per_objfile
23249 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23250
23251 /* For .gdb_index version 7 keep track of included TUs.
23252 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23253 if (dwarf2_per_objfile->index_table != NULL
23254 && dwarf2_per_objfile->index_table->version <= 7)
23255 {
23256 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23257 }
23258
23259 *ref_cu = sig_cu;
23260 if (sig_cu != cu)
23261 sig_cu->ancestor = cu;
23262
23263 return die;
23264 }
23265
23266 return NULL;
23267 }
23268
23269 /* Follow signatured type referenced by ATTR in SRC_DIE.
23270 On entry *REF_CU is the CU of SRC_DIE.
23271 On exit *REF_CU is the CU of the result.
23272 The result is the DIE of the type.
23273 If the referenced type cannot be found an error is thrown. */
23274
23275 static struct die_info *
23276 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23277 struct dwarf2_cu **ref_cu)
23278 {
23279 ULONGEST signature = DW_SIGNATURE (attr);
23280 struct signatured_type *sig_type;
23281 struct die_info *die;
23282
23283 gdb_assert (attr->form == DW_FORM_ref_sig8);
23284
23285 sig_type = lookup_signatured_type (*ref_cu, signature);
23286 /* sig_type will be NULL if the signatured type is missing from
23287 the debug info. */
23288 if (sig_type == NULL)
23289 {
23290 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23291 " from DIE at %s [in module %s]"),
23292 hex_string (signature), sect_offset_str (src_die->sect_off),
23293 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23294 }
23295
23296 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23297 if (die == NULL)
23298 {
23299 dump_die_for_error (src_die);
23300 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23301 " from DIE at %s [in module %s]"),
23302 hex_string (signature), sect_offset_str (src_die->sect_off),
23303 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23304 }
23305
23306 return die;
23307 }
23308
23309 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23310 reading in and processing the type unit if necessary. */
23311
23312 static struct type *
23313 get_signatured_type (struct die_info *die, ULONGEST signature,
23314 struct dwarf2_cu *cu)
23315 {
23316 struct dwarf2_per_objfile *dwarf2_per_objfile
23317 = cu->per_cu->dwarf2_per_objfile;
23318 struct signatured_type *sig_type;
23319 struct dwarf2_cu *type_cu;
23320 struct die_info *type_die;
23321 struct type *type;
23322
23323 sig_type = lookup_signatured_type (cu, signature);
23324 /* sig_type will be NULL if the signatured type is missing from
23325 the debug info. */
23326 if (sig_type == NULL)
23327 {
23328 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23329 " from DIE at %s [in module %s]"),
23330 hex_string (signature), sect_offset_str (die->sect_off),
23331 objfile_name (dwarf2_per_objfile->objfile));
23332 return build_error_marker_type (cu, die);
23333 }
23334
23335 /* If we already know the type we're done. */
23336 if (sig_type->type != NULL)
23337 return sig_type->type;
23338
23339 type_cu = cu;
23340 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23341 if (type_die != NULL)
23342 {
23343 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23344 is created. This is important, for example, because for c++ classes
23345 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23346 type = read_type_die (type_die, type_cu);
23347 if (type == NULL)
23348 {
23349 complaint (_("Dwarf Error: Cannot build signatured type %s"
23350 " referenced from DIE at %s [in module %s]"),
23351 hex_string (signature), sect_offset_str (die->sect_off),
23352 objfile_name (dwarf2_per_objfile->objfile));
23353 type = build_error_marker_type (cu, die);
23354 }
23355 }
23356 else
23357 {
23358 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23359 " from DIE at %s [in module %s]"),
23360 hex_string (signature), sect_offset_str (die->sect_off),
23361 objfile_name (dwarf2_per_objfile->objfile));
23362 type = build_error_marker_type (cu, die);
23363 }
23364 sig_type->type = type;
23365
23366 return type;
23367 }
23368
23369 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23370 reading in and processing the type unit if necessary. */
23371
23372 static struct type *
23373 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23374 struct dwarf2_cu *cu) /* ARI: editCase function */
23375 {
23376 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23377 if (attr->form_is_ref ())
23378 {
23379 struct dwarf2_cu *type_cu = cu;
23380 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23381
23382 return read_type_die (type_die, type_cu);
23383 }
23384 else if (attr->form == DW_FORM_ref_sig8)
23385 {
23386 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23387 }
23388 else
23389 {
23390 struct dwarf2_per_objfile *dwarf2_per_objfile
23391 = cu->per_cu->dwarf2_per_objfile;
23392
23393 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23394 " at %s [in module %s]"),
23395 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23396 objfile_name (dwarf2_per_objfile->objfile));
23397 return build_error_marker_type (cu, die);
23398 }
23399 }
23400
23401 /* Load the DIEs associated with type unit PER_CU into memory. */
23402
23403 static void
23404 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23405 {
23406 struct signatured_type *sig_type;
23407
23408 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23409 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23410
23411 /* We have the per_cu, but we need the signatured_type.
23412 Fortunately this is an easy translation. */
23413 gdb_assert (per_cu->is_debug_types);
23414 sig_type = (struct signatured_type *) per_cu;
23415
23416 gdb_assert (per_cu->cu == NULL);
23417
23418 read_signatured_type (sig_type);
23419
23420 gdb_assert (per_cu->cu != NULL);
23421 }
23422
23423 /* Read in a signatured type and build its CU and DIEs.
23424 If the type is a stub for the real type in a DWO file,
23425 read in the real type from the DWO file as well. */
23426
23427 static void
23428 read_signatured_type (struct signatured_type *sig_type)
23429 {
23430 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23431
23432 gdb_assert (per_cu->is_debug_types);
23433 gdb_assert (per_cu->cu == NULL);
23434
23435 cutu_reader reader (per_cu, NULL, 0, 1, false);
23436
23437 if (!reader.dummy_p)
23438 {
23439 struct dwarf2_cu *cu = reader.cu;
23440 const gdb_byte *info_ptr = reader.info_ptr;
23441
23442 gdb_assert (cu->die_hash == NULL);
23443 cu->die_hash =
23444 htab_create_alloc_ex (cu->header.length / 12,
23445 die_hash,
23446 die_eq,
23447 NULL,
23448 &cu->comp_unit_obstack,
23449 hashtab_obstack_allocate,
23450 dummy_obstack_deallocate);
23451
23452 if (reader.comp_unit_die->has_children)
23453 reader.comp_unit_die->child
23454 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23455 reader.comp_unit_die);
23456 cu->dies = reader.comp_unit_die;
23457 /* comp_unit_die is not stored in die_hash, no need. */
23458
23459 /* We try not to read any attributes in this function, because
23460 not all CUs needed for references have been loaded yet, and
23461 symbol table processing isn't initialized. But we have to
23462 set the CU language, or we won't be able to build types
23463 correctly. Similarly, if we do not read the producer, we can
23464 not apply producer-specific interpretation. */
23465 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23466 }
23467
23468 sig_type->per_cu.tu_read = 1;
23469 }
23470
23471 /* Decode simple location descriptions.
23472 Given a pointer to a dwarf block that defines a location, compute
23473 the location and return the value.
23474
23475 NOTE drow/2003-11-18: This function is called in two situations
23476 now: for the address of static or global variables (partial symbols
23477 only) and for offsets into structures which are expected to be
23478 (more or less) constant. The partial symbol case should go away,
23479 and only the constant case should remain. That will let this
23480 function complain more accurately. A few special modes are allowed
23481 without complaint for global variables (for instance, global
23482 register values and thread-local values).
23483
23484 A location description containing no operations indicates that the
23485 object is optimized out. The return value is 0 for that case.
23486 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23487 callers will only want a very basic result and this can become a
23488 complaint.
23489
23490 Note that stack[0] is unused except as a default error return. */
23491
23492 static CORE_ADDR
23493 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23494 {
23495 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23496 size_t i;
23497 size_t size = blk->size;
23498 const gdb_byte *data = blk->data;
23499 CORE_ADDR stack[64];
23500 int stacki;
23501 unsigned int bytes_read, unsnd;
23502 gdb_byte op;
23503
23504 i = 0;
23505 stacki = 0;
23506 stack[stacki] = 0;
23507 stack[++stacki] = 0;
23508
23509 while (i < size)
23510 {
23511 op = data[i++];
23512 switch (op)
23513 {
23514 case DW_OP_lit0:
23515 case DW_OP_lit1:
23516 case DW_OP_lit2:
23517 case DW_OP_lit3:
23518 case DW_OP_lit4:
23519 case DW_OP_lit5:
23520 case DW_OP_lit6:
23521 case DW_OP_lit7:
23522 case DW_OP_lit8:
23523 case DW_OP_lit9:
23524 case DW_OP_lit10:
23525 case DW_OP_lit11:
23526 case DW_OP_lit12:
23527 case DW_OP_lit13:
23528 case DW_OP_lit14:
23529 case DW_OP_lit15:
23530 case DW_OP_lit16:
23531 case DW_OP_lit17:
23532 case DW_OP_lit18:
23533 case DW_OP_lit19:
23534 case DW_OP_lit20:
23535 case DW_OP_lit21:
23536 case DW_OP_lit22:
23537 case DW_OP_lit23:
23538 case DW_OP_lit24:
23539 case DW_OP_lit25:
23540 case DW_OP_lit26:
23541 case DW_OP_lit27:
23542 case DW_OP_lit28:
23543 case DW_OP_lit29:
23544 case DW_OP_lit30:
23545 case DW_OP_lit31:
23546 stack[++stacki] = op - DW_OP_lit0;
23547 break;
23548
23549 case DW_OP_reg0:
23550 case DW_OP_reg1:
23551 case DW_OP_reg2:
23552 case DW_OP_reg3:
23553 case DW_OP_reg4:
23554 case DW_OP_reg5:
23555 case DW_OP_reg6:
23556 case DW_OP_reg7:
23557 case DW_OP_reg8:
23558 case DW_OP_reg9:
23559 case DW_OP_reg10:
23560 case DW_OP_reg11:
23561 case DW_OP_reg12:
23562 case DW_OP_reg13:
23563 case DW_OP_reg14:
23564 case DW_OP_reg15:
23565 case DW_OP_reg16:
23566 case DW_OP_reg17:
23567 case DW_OP_reg18:
23568 case DW_OP_reg19:
23569 case DW_OP_reg20:
23570 case DW_OP_reg21:
23571 case DW_OP_reg22:
23572 case DW_OP_reg23:
23573 case DW_OP_reg24:
23574 case DW_OP_reg25:
23575 case DW_OP_reg26:
23576 case DW_OP_reg27:
23577 case DW_OP_reg28:
23578 case DW_OP_reg29:
23579 case DW_OP_reg30:
23580 case DW_OP_reg31:
23581 stack[++stacki] = op - DW_OP_reg0;
23582 if (i < size)
23583 dwarf2_complex_location_expr_complaint ();
23584 break;
23585
23586 case DW_OP_regx:
23587 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23588 i += bytes_read;
23589 stack[++stacki] = unsnd;
23590 if (i < size)
23591 dwarf2_complex_location_expr_complaint ();
23592 break;
23593
23594 case DW_OP_addr:
23595 stack[++stacki] = read_address (objfile->obfd, &data[i],
23596 cu, &bytes_read);
23597 i += bytes_read;
23598 break;
23599
23600 case DW_OP_const1u:
23601 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23602 i += 1;
23603 break;
23604
23605 case DW_OP_const1s:
23606 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23607 i += 1;
23608 break;
23609
23610 case DW_OP_const2u:
23611 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23612 i += 2;
23613 break;
23614
23615 case DW_OP_const2s:
23616 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23617 i += 2;
23618 break;
23619
23620 case DW_OP_const4u:
23621 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23622 i += 4;
23623 break;
23624
23625 case DW_OP_const4s:
23626 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23627 i += 4;
23628 break;
23629
23630 case DW_OP_const8u:
23631 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23632 i += 8;
23633 break;
23634
23635 case DW_OP_constu:
23636 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23637 &bytes_read);
23638 i += bytes_read;
23639 break;
23640
23641 case DW_OP_consts:
23642 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23643 i += bytes_read;
23644 break;
23645
23646 case DW_OP_dup:
23647 stack[stacki + 1] = stack[stacki];
23648 stacki++;
23649 break;
23650
23651 case DW_OP_plus:
23652 stack[stacki - 1] += stack[stacki];
23653 stacki--;
23654 break;
23655
23656 case DW_OP_plus_uconst:
23657 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23658 &bytes_read);
23659 i += bytes_read;
23660 break;
23661
23662 case DW_OP_minus:
23663 stack[stacki - 1] -= stack[stacki];
23664 stacki--;
23665 break;
23666
23667 case DW_OP_deref:
23668 /* If we're not the last op, then we definitely can't encode
23669 this using GDB's address_class enum. This is valid for partial
23670 global symbols, although the variable's address will be bogus
23671 in the psymtab. */
23672 if (i < size)
23673 dwarf2_complex_location_expr_complaint ();
23674 break;
23675
23676 case DW_OP_GNU_push_tls_address:
23677 case DW_OP_form_tls_address:
23678 /* The top of the stack has the offset from the beginning
23679 of the thread control block at which the variable is located. */
23680 /* Nothing should follow this operator, so the top of stack would
23681 be returned. */
23682 /* This is valid for partial global symbols, but the variable's
23683 address will be bogus in the psymtab. Make it always at least
23684 non-zero to not look as a variable garbage collected by linker
23685 which have DW_OP_addr 0. */
23686 if (i < size)
23687 dwarf2_complex_location_expr_complaint ();
23688 stack[stacki]++;
23689 break;
23690
23691 case DW_OP_GNU_uninit:
23692 break;
23693
23694 case DW_OP_addrx:
23695 case DW_OP_GNU_addr_index:
23696 case DW_OP_GNU_const_index:
23697 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23698 &bytes_read);
23699 i += bytes_read;
23700 break;
23701
23702 default:
23703 {
23704 const char *name = get_DW_OP_name (op);
23705
23706 if (name)
23707 complaint (_("unsupported stack op: '%s'"),
23708 name);
23709 else
23710 complaint (_("unsupported stack op: '%02x'"),
23711 op);
23712 }
23713
23714 return (stack[stacki]);
23715 }
23716
23717 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23718 outside of the allocated space. Also enforce minimum>0. */
23719 if (stacki >= ARRAY_SIZE (stack) - 1)
23720 {
23721 complaint (_("location description stack overflow"));
23722 return 0;
23723 }
23724
23725 if (stacki <= 0)
23726 {
23727 complaint (_("location description stack underflow"));
23728 return 0;
23729 }
23730 }
23731 return (stack[stacki]);
23732 }
23733
23734 /* memory allocation interface */
23735
23736 static struct dwarf_block *
23737 dwarf_alloc_block (struct dwarf2_cu *cu)
23738 {
23739 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23740 }
23741
23742 static struct die_info *
23743 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23744 {
23745 struct die_info *die;
23746 size_t size = sizeof (struct die_info);
23747
23748 if (num_attrs > 1)
23749 size += (num_attrs - 1) * sizeof (struct attribute);
23750
23751 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23752 memset (die, 0, sizeof (struct die_info));
23753 return (die);
23754 }
23755
23756 \f
23757 /* Macro support. */
23758
23759 gdb::unique_xmalloc_ptr<char>
23760 line_header::file_file_name (int file)
23761 {
23762 /* Is the file number a valid index into the line header's file name
23763 table? Remember that file numbers start with one, not zero. */
23764 if (is_valid_file_index (file))
23765 {
23766 const file_entry *fe = file_name_at (file);
23767
23768 if (!IS_ABSOLUTE_PATH (fe->name))
23769 {
23770 const char *dir = fe->include_dir (this);
23771 if (dir != NULL)
23772 return gdb::unique_xmalloc_ptr<char> (concat (dir, SLASH_STRING,
23773 fe->name,
23774 (char *) NULL));
23775 }
23776 return make_unique_xstrdup (fe->name);
23777 }
23778 else
23779 {
23780 /* The compiler produced a bogus file number. We can at least
23781 record the macro definitions made in the file, even if we
23782 won't be able to find the file by name. */
23783 char fake_name[80];
23784
23785 xsnprintf (fake_name, sizeof (fake_name),
23786 "<bad macro file number %d>", file);
23787
23788 complaint (_("bad file number in macro information (%d)"),
23789 file);
23790
23791 return make_unique_xstrdup (fake_name);
23792 }
23793 }
23794
23795 gdb::unique_xmalloc_ptr<char>
23796 line_header::file_full_name (int file, const char *comp_dir)
23797 {
23798 /* Is the file number a valid index into the line header's file name
23799 table? Remember that file numbers start with one, not zero. */
23800 if (is_valid_file_index (file))
23801 {
23802 gdb::unique_xmalloc_ptr<char> relative = file_file_name (file);
23803
23804 if (IS_ABSOLUTE_PATH (relative.get ()) || comp_dir == NULL)
23805 return relative;
23806 return gdb::unique_xmalloc_ptr<char> (concat (comp_dir, SLASH_STRING,
23807 relative.get (),
23808 (char *) NULL));
23809 }
23810 else
23811 return file_file_name (file);
23812 }
23813
23814
23815 static struct macro_source_file *
23816 macro_start_file (struct dwarf2_cu *cu,
23817 int file, int line,
23818 struct macro_source_file *current_file,
23819 struct line_header *lh)
23820 {
23821 /* File name relative to the compilation directory of this source file. */
23822 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23823
23824 if (! current_file)
23825 {
23826 /* Note: We don't create a macro table for this compilation unit
23827 at all until we actually get a filename. */
23828 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23829
23830 /* If we have no current file, then this must be the start_file
23831 directive for the compilation unit's main source file. */
23832 current_file = macro_set_main (macro_table, file_name.get ());
23833 macro_define_special (macro_table);
23834 }
23835 else
23836 current_file = macro_include (current_file, line, file_name.get ());
23837
23838 return current_file;
23839 }
23840
23841 static const char *
23842 consume_improper_spaces (const char *p, const char *body)
23843 {
23844 if (*p == ' ')
23845 {
23846 complaint (_("macro definition contains spaces "
23847 "in formal argument list:\n`%s'"),
23848 body);
23849
23850 while (*p == ' ')
23851 p++;
23852 }
23853
23854 return p;
23855 }
23856
23857
23858 static void
23859 parse_macro_definition (struct macro_source_file *file, int line,
23860 const char *body)
23861 {
23862 const char *p;
23863
23864 /* The body string takes one of two forms. For object-like macro
23865 definitions, it should be:
23866
23867 <macro name> " " <definition>
23868
23869 For function-like macro definitions, it should be:
23870
23871 <macro name> "() " <definition>
23872 or
23873 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23874
23875 Spaces may appear only where explicitly indicated, and in the
23876 <definition>.
23877
23878 The Dwarf 2 spec says that an object-like macro's name is always
23879 followed by a space, but versions of GCC around March 2002 omit
23880 the space when the macro's definition is the empty string.
23881
23882 The Dwarf 2 spec says that there should be no spaces between the
23883 formal arguments in a function-like macro's formal argument list,
23884 but versions of GCC around March 2002 include spaces after the
23885 commas. */
23886
23887
23888 /* Find the extent of the macro name. The macro name is terminated
23889 by either a space or null character (for an object-like macro) or
23890 an opening paren (for a function-like macro). */
23891 for (p = body; *p; p++)
23892 if (*p == ' ' || *p == '(')
23893 break;
23894
23895 if (*p == ' ' || *p == '\0')
23896 {
23897 /* It's an object-like macro. */
23898 int name_len = p - body;
23899 std::string name (body, name_len);
23900 const char *replacement;
23901
23902 if (*p == ' ')
23903 replacement = body + name_len + 1;
23904 else
23905 {
23906 dwarf2_macro_malformed_definition_complaint (body);
23907 replacement = body + name_len;
23908 }
23909
23910 macro_define_object (file, line, name.c_str (), replacement);
23911 }
23912 else if (*p == '(')
23913 {
23914 /* It's a function-like macro. */
23915 std::string name (body, p - body);
23916 int argc = 0;
23917 int argv_size = 1;
23918 char **argv = XNEWVEC (char *, argv_size);
23919
23920 p++;
23921
23922 p = consume_improper_spaces (p, body);
23923
23924 /* Parse the formal argument list. */
23925 while (*p && *p != ')')
23926 {
23927 /* Find the extent of the current argument name. */
23928 const char *arg_start = p;
23929
23930 while (*p && *p != ',' && *p != ')' && *p != ' ')
23931 p++;
23932
23933 if (! *p || p == arg_start)
23934 dwarf2_macro_malformed_definition_complaint (body);
23935 else
23936 {
23937 /* Make sure argv has room for the new argument. */
23938 if (argc >= argv_size)
23939 {
23940 argv_size *= 2;
23941 argv = XRESIZEVEC (char *, argv, argv_size);
23942 }
23943
23944 argv[argc++] = savestring (arg_start, p - arg_start);
23945 }
23946
23947 p = consume_improper_spaces (p, body);
23948
23949 /* Consume the comma, if present. */
23950 if (*p == ',')
23951 {
23952 p++;
23953
23954 p = consume_improper_spaces (p, body);
23955 }
23956 }
23957
23958 if (*p == ')')
23959 {
23960 p++;
23961
23962 if (*p == ' ')
23963 /* Perfectly formed definition, no complaints. */
23964 macro_define_function (file, line, name.c_str (),
23965 argc, (const char **) argv,
23966 p + 1);
23967 else if (*p == '\0')
23968 {
23969 /* Complain, but do define it. */
23970 dwarf2_macro_malformed_definition_complaint (body);
23971 macro_define_function (file, line, name.c_str (),
23972 argc, (const char **) argv,
23973 p);
23974 }
23975 else
23976 /* Just complain. */
23977 dwarf2_macro_malformed_definition_complaint (body);
23978 }
23979 else
23980 /* Just complain. */
23981 dwarf2_macro_malformed_definition_complaint (body);
23982
23983 {
23984 int i;
23985
23986 for (i = 0; i < argc; i++)
23987 xfree (argv[i]);
23988 }
23989 xfree (argv);
23990 }
23991 else
23992 dwarf2_macro_malformed_definition_complaint (body);
23993 }
23994
23995 /* Skip some bytes from BYTES according to the form given in FORM.
23996 Returns the new pointer. */
23997
23998 static const gdb_byte *
23999 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24000 enum dwarf_form form,
24001 unsigned int offset_size,
24002 struct dwarf2_section_info *section)
24003 {
24004 unsigned int bytes_read;
24005
24006 switch (form)
24007 {
24008 case DW_FORM_data1:
24009 case DW_FORM_flag:
24010 ++bytes;
24011 break;
24012
24013 case DW_FORM_data2:
24014 bytes += 2;
24015 break;
24016
24017 case DW_FORM_data4:
24018 bytes += 4;
24019 break;
24020
24021 case DW_FORM_data8:
24022 bytes += 8;
24023 break;
24024
24025 case DW_FORM_data16:
24026 bytes += 16;
24027 break;
24028
24029 case DW_FORM_string:
24030 read_direct_string (abfd, bytes, &bytes_read);
24031 bytes += bytes_read;
24032 break;
24033
24034 case DW_FORM_sec_offset:
24035 case DW_FORM_strp:
24036 case DW_FORM_GNU_strp_alt:
24037 bytes += offset_size;
24038 break;
24039
24040 case DW_FORM_block:
24041 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24042 bytes += bytes_read;
24043 break;
24044
24045 case DW_FORM_block1:
24046 bytes += 1 + read_1_byte (abfd, bytes);
24047 break;
24048 case DW_FORM_block2:
24049 bytes += 2 + read_2_bytes (abfd, bytes);
24050 break;
24051 case DW_FORM_block4:
24052 bytes += 4 + read_4_bytes (abfd, bytes);
24053 break;
24054
24055 case DW_FORM_addrx:
24056 case DW_FORM_sdata:
24057 case DW_FORM_strx:
24058 case DW_FORM_udata:
24059 case DW_FORM_GNU_addr_index:
24060 case DW_FORM_GNU_str_index:
24061 bytes = gdb_skip_leb128 (bytes, buffer_end);
24062 if (bytes == NULL)
24063 {
24064 dwarf2_section_buffer_overflow_complaint (section);
24065 return NULL;
24066 }
24067 break;
24068
24069 case DW_FORM_implicit_const:
24070 break;
24071
24072 default:
24073 {
24074 complaint (_("invalid form 0x%x in `%s'"),
24075 form, section->get_name ());
24076 return NULL;
24077 }
24078 }
24079
24080 return bytes;
24081 }
24082
24083 /* A helper for dwarf_decode_macros that handles skipping an unknown
24084 opcode. Returns an updated pointer to the macro data buffer; or,
24085 on error, issues a complaint and returns NULL. */
24086
24087 static const gdb_byte *
24088 skip_unknown_opcode (unsigned int opcode,
24089 const gdb_byte **opcode_definitions,
24090 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24091 bfd *abfd,
24092 unsigned int offset_size,
24093 struct dwarf2_section_info *section)
24094 {
24095 unsigned int bytes_read, i;
24096 unsigned long arg;
24097 const gdb_byte *defn;
24098
24099 if (opcode_definitions[opcode] == NULL)
24100 {
24101 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24102 opcode);
24103 return NULL;
24104 }
24105
24106 defn = opcode_definitions[opcode];
24107 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24108 defn += bytes_read;
24109
24110 for (i = 0; i < arg; ++i)
24111 {
24112 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24113 (enum dwarf_form) defn[i], offset_size,
24114 section);
24115 if (mac_ptr == NULL)
24116 {
24117 /* skip_form_bytes already issued the complaint. */
24118 return NULL;
24119 }
24120 }
24121
24122 return mac_ptr;
24123 }
24124
24125 /* A helper function which parses the header of a macro section.
24126 If the macro section is the extended (for now called "GNU") type,
24127 then this updates *OFFSET_SIZE. Returns a pointer to just after
24128 the header, or issues a complaint and returns NULL on error. */
24129
24130 static const gdb_byte *
24131 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24132 bfd *abfd,
24133 const gdb_byte *mac_ptr,
24134 unsigned int *offset_size,
24135 int section_is_gnu)
24136 {
24137 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24138
24139 if (section_is_gnu)
24140 {
24141 unsigned int version, flags;
24142
24143 version = read_2_bytes (abfd, mac_ptr);
24144 if (version != 4 && version != 5)
24145 {
24146 complaint (_("unrecognized version `%d' in .debug_macro section"),
24147 version);
24148 return NULL;
24149 }
24150 mac_ptr += 2;
24151
24152 flags = read_1_byte (abfd, mac_ptr);
24153 ++mac_ptr;
24154 *offset_size = (flags & 1) ? 8 : 4;
24155
24156 if ((flags & 2) != 0)
24157 /* We don't need the line table offset. */
24158 mac_ptr += *offset_size;
24159
24160 /* Vendor opcode descriptions. */
24161 if ((flags & 4) != 0)
24162 {
24163 unsigned int i, count;
24164
24165 count = read_1_byte (abfd, mac_ptr);
24166 ++mac_ptr;
24167 for (i = 0; i < count; ++i)
24168 {
24169 unsigned int opcode, bytes_read;
24170 unsigned long arg;
24171
24172 opcode = read_1_byte (abfd, mac_ptr);
24173 ++mac_ptr;
24174 opcode_definitions[opcode] = mac_ptr;
24175 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24176 mac_ptr += bytes_read;
24177 mac_ptr += arg;
24178 }
24179 }
24180 }
24181
24182 return mac_ptr;
24183 }
24184
24185 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24186 including DW_MACRO_import. */
24187
24188 static void
24189 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24190 bfd *abfd,
24191 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24192 struct macro_source_file *current_file,
24193 struct line_header *lh,
24194 struct dwarf2_section_info *section,
24195 int section_is_gnu, int section_is_dwz,
24196 unsigned int offset_size,
24197 htab_t include_hash)
24198 {
24199 struct dwarf2_per_objfile *dwarf2_per_objfile
24200 = cu->per_cu->dwarf2_per_objfile;
24201 struct objfile *objfile = dwarf2_per_objfile->objfile;
24202 enum dwarf_macro_record_type macinfo_type;
24203 int at_commandline;
24204 const gdb_byte *opcode_definitions[256];
24205
24206 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24207 &offset_size, section_is_gnu);
24208 if (mac_ptr == NULL)
24209 {
24210 /* We already issued a complaint. */
24211 return;
24212 }
24213
24214 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24215 GDB is still reading the definitions from command line. First
24216 DW_MACINFO_start_file will need to be ignored as it was already executed
24217 to create CURRENT_FILE for the main source holding also the command line
24218 definitions. On first met DW_MACINFO_start_file this flag is reset to
24219 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24220
24221 at_commandline = 1;
24222
24223 do
24224 {
24225 /* Do we at least have room for a macinfo type byte? */
24226 if (mac_ptr >= mac_end)
24227 {
24228 dwarf2_section_buffer_overflow_complaint (section);
24229 break;
24230 }
24231
24232 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24233 mac_ptr++;
24234
24235 /* Note that we rely on the fact that the corresponding GNU and
24236 DWARF constants are the same. */
24237 DIAGNOSTIC_PUSH
24238 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24239 switch (macinfo_type)
24240 {
24241 /* A zero macinfo type indicates the end of the macro
24242 information. */
24243 case 0:
24244 break;
24245
24246 case DW_MACRO_define:
24247 case DW_MACRO_undef:
24248 case DW_MACRO_define_strp:
24249 case DW_MACRO_undef_strp:
24250 case DW_MACRO_define_sup:
24251 case DW_MACRO_undef_sup:
24252 {
24253 unsigned int bytes_read;
24254 int line;
24255 const char *body;
24256 int is_define;
24257
24258 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24259 mac_ptr += bytes_read;
24260
24261 if (macinfo_type == DW_MACRO_define
24262 || macinfo_type == DW_MACRO_undef)
24263 {
24264 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24265 mac_ptr += bytes_read;
24266 }
24267 else
24268 {
24269 LONGEST str_offset;
24270
24271 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24272 mac_ptr += offset_size;
24273
24274 if (macinfo_type == DW_MACRO_define_sup
24275 || macinfo_type == DW_MACRO_undef_sup
24276 || section_is_dwz)
24277 {
24278 struct dwz_file *dwz
24279 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24280
24281 body = read_indirect_string_from_dwz (objfile,
24282 dwz, str_offset);
24283 }
24284 else
24285 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24286 abfd, str_offset);
24287 }
24288
24289 is_define = (macinfo_type == DW_MACRO_define
24290 || macinfo_type == DW_MACRO_define_strp
24291 || macinfo_type == DW_MACRO_define_sup);
24292 if (! current_file)
24293 {
24294 /* DWARF violation as no main source is present. */
24295 complaint (_("debug info with no main source gives macro %s "
24296 "on line %d: %s"),
24297 is_define ? _("definition") : _("undefinition"),
24298 line, body);
24299 break;
24300 }
24301 if ((line == 0 && !at_commandline)
24302 || (line != 0 && at_commandline))
24303 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24304 at_commandline ? _("command-line") : _("in-file"),
24305 is_define ? _("definition") : _("undefinition"),
24306 line == 0 ? _("zero") : _("non-zero"), line, body);
24307
24308 if (body == NULL)
24309 {
24310 /* Fedora's rpm-build's "debugedit" binary
24311 corrupted .debug_macro sections.
24312
24313 For more info, see
24314 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24315 complaint (_("debug info gives %s invalid macro %s "
24316 "without body (corrupted?) at line %d "
24317 "on file %s"),
24318 at_commandline ? _("command-line") : _("in-file"),
24319 is_define ? _("definition") : _("undefinition"),
24320 line, current_file->filename);
24321 }
24322 else if (is_define)
24323 parse_macro_definition (current_file, line, body);
24324 else
24325 {
24326 gdb_assert (macinfo_type == DW_MACRO_undef
24327 || macinfo_type == DW_MACRO_undef_strp
24328 || macinfo_type == DW_MACRO_undef_sup);
24329 macro_undef (current_file, line, body);
24330 }
24331 }
24332 break;
24333
24334 case DW_MACRO_start_file:
24335 {
24336 unsigned int bytes_read;
24337 int line, file;
24338
24339 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24340 mac_ptr += bytes_read;
24341 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24342 mac_ptr += bytes_read;
24343
24344 if ((line == 0 && !at_commandline)
24345 || (line != 0 && at_commandline))
24346 complaint (_("debug info gives source %d included "
24347 "from %s at %s line %d"),
24348 file, at_commandline ? _("command-line") : _("file"),
24349 line == 0 ? _("zero") : _("non-zero"), line);
24350
24351 if (at_commandline)
24352 {
24353 /* This DW_MACRO_start_file was executed in the
24354 pass one. */
24355 at_commandline = 0;
24356 }
24357 else
24358 current_file = macro_start_file (cu, file, line, current_file,
24359 lh);
24360 }
24361 break;
24362
24363 case DW_MACRO_end_file:
24364 if (! current_file)
24365 complaint (_("macro debug info has an unmatched "
24366 "`close_file' directive"));
24367 else
24368 {
24369 current_file = current_file->included_by;
24370 if (! current_file)
24371 {
24372 enum dwarf_macro_record_type next_type;
24373
24374 /* GCC circa March 2002 doesn't produce the zero
24375 type byte marking the end of the compilation
24376 unit. Complain if it's not there, but exit no
24377 matter what. */
24378
24379 /* Do we at least have room for a macinfo type byte? */
24380 if (mac_ptr >= mac_end)
24381 {
24382 dwarf2_section_buffer_overflow_complaint (section);
24383 return;
24384 }
24385
24386 /* We don't increment mac_ptr here, so this is just
24387 a look-ahead. */
24388 next_type
24389 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24390 mac_ptr);
24391 if (next_type != 0)
24392 complaint (_("no terminating 0-type entry for "
24393 "macros in `.debug_macinfo' section"));
24394
24395 return;
24396 }
24397 }
24398 break;
24399
24400 case DW_MACRO_import:
24401 case DW_MACRO_import_sup:
24402 {
24403 LONGEST offset;
24404 void **slot;
24405 bfd *include_bfd = abfd;
24406 struct dwarf2_section_info *include_section = section;
24407 const gdb_byte *include_mac_end = mac_end;
24408 int is_dwz = section_is_dwz;
24409 const gdb_byte *new_mac_ptr;
24410
24411 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24412 mac_ptr += offset_size;
24413
24414 if (macinfo_type == DW_MACRO_import_sup)
24415 {
24416 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24417
24418 dwz->macro.read (objfile);
24419
24420 include_section = &dwz->macro;
24421 include_bfd = include_section->get_bfd_owner ();
24422 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24423 is_dwz = 1;
24424 }
24425
24426 new_mac_ptr = include_section->buffer + offset;
24427 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24428
24429 if (*slot != NULL)
24430 {
24431 /* This has actually happened; see
24432 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24433 complaint (_("recursive DW_MACRO_import in "
24434 ".debug_macro section"));
24435 }
24436 else
24437 {
24438 *slot = (void *) new_mac_ptr;
24439
24440 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24441 include_mac_end, current_file, lh,
24442 section, section_is_gnu, is_dwz,
24443 offset_size, include_hash);
24444
24445 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24446 }
24447 }
24448 break;
24449
24450 case DW_MACINFO_vendor_ext:
24451 if (!section_is_gnu)
24452 {
24453 unsigned int bytes_read;
24454
24455 /* This reads the constant, but since we don't recognize
24456 any vendor extensions, we ignore it. */
24457 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24458 mac_ptr += bytes_read;
24459 read_direct_string (abfd, mac_ptr, &bytes_read);
24460 mac_ptr += bytes_read;
24461
24462 /* We don't recognize any vendor extensions. */
24463 break;
24464 }
24465 /* FALLTHROUGH */
24466
24467 default:
24468 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24469 mac_ptr, mac_end, abfd, offset_size,
24470 section);
24471 if (mac_ptr == NULL)
24472 return;
24473 break;
24474 }
24475 DIAGNOSTIC_POP
24476 } while (macinfo_type != 0);
24477 }
24478
24479 static void
24480 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24481 int section_is_gnu)
24482 {
24483 struct dwarf2_per_objfile *dwarf2_per_objfile
24484 = cu->per_cu->dwarf2_per_objfile;
24485 struct objfile *objfile = dwarf2_per_objfile->objfile;
24486 struct line_header *lh = cu->line_header;
24487 bfd *abfd;
24488 const gdb_byte *mac_ptr, *mac_end;
24489 struct macro_source_file *current_file = 0;
24490 enum dwarf_macro_record_type macinfo_type;
24491 unsigned int offset_size = cu->header.offset_size;
24492 const gdb_byte *opcode_definitions[256];
24493 void **slot;
24494 struct dwarf2_section_info *section;
24495 const char *section_name;
24496
24497 if (cu->dwo_unit != NULL)
24498 {
24499 if (section_is_gnu)
24500 {
24501 section = &cu->dwo_unit->dwo_file->sections.macro;
24502 section_name = ".debug_macro.dwo";
24503 }
24504 else
24505 {
24506 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24507 section_name = ".debug_macinfo.dwo";
24508 }
24509 }
24510 else
24511 {
24512 if (section_is_gnu)
24513 {
24514 section = &dwarf2_per_objfile->macro;
24515 section_name = ".debug_macro";
24516 }
24517 else
24518 {
24519 section = &dwarf2_per_objfile->macinfo;
24520 section_name = ".debug_macinfo";
24521 }
24522 }
24523
24524 section->read (objfile);
24525 if (section->buffer == NULL)
24526 {
24527 complaint (_("missing %s section"), section_name);
24528 return;
24529 }
24530 abfd = section->get_bfd_owner ();
24531
24532 /* First pass: Find the name of the base filename.
24533 This filename is needed in order to process all macros whose definition
24534 (or undefinition) comes from the command line. These macros are defined
24535 before the first DW_MACINFO_start_file entry, and yet still need to be
24536 associated to the base file.
24537
24538 To determine the base file name, we scan the macro definitions until we
24539 reach the first DW_MACINFO_start_file entry. We then initialize
24540 CURRENT_FILE accordingly so that any macro definition found before the
24541 first DW_MACINFO_start_file can still be associated to the base file. */
24542
24543 mac_ptr = section->buffer + offset;
24544 mac_end = section->buffer + section->size;
24545
24546 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24547 &offset_size, section_is_gnu);
24548 if (mac_ptr == NULL)
24549 {
24550 /* We already issued a complaint. */
24551 return;
24552 }
24553
24554 do
24555 {
24556 /* Do we at least have room for a macinfo type byte? */
24557 if (mac_ptr >= mac_end)
24558 {
24559 /* Complaint is printed during the second pass as GDB will probably
24560 stop the first pass earlier upon finding
24561 DW_MACINFO_start_file. */
24562 break;
24563 }
24564
24565 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24566 mac_ptr++;
24567
24568 /* Note that we rely on the fact that the corresponding GNU and
24569 DWARF constants are the same. */
24570 DIAGNOSTIC_PUSH
24571 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24572 switch (macinfo_type)
24573 {
24574 /* A zero macinfo type indicates the end of the macro
24575 information. */
24576 case 0:
24577 break;
24578
24579 case DW_MACRO_define:
24580 case DW_MACRO_undef:
24581 /* Only skip the data by MAC_PTR. */
24582 {
24583 unsigned int bytes_read;
24584
24585 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24586 mac_ptr += bytes_read;
24587 read_direct_string (abfd, mac_ptr, &bytes_read);
24588 mac_ptr += bytes_read;
24589 }
24590 break;
24591
24592 case DW_MACRO_start_file:
24593 {
24594 unsigned int bytes_read;
24595 int line, file;
24596
24597 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24598 mac_ptr += bytes_read;
24599 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24600 mac_ptr += bytes_read;
24601
24602 current_file = macro_start_file (cu, file, line, current_file, lh);
24603 }
24604 break;
24605
24606 case DW_MACRO_end_file:
24607 /* No data to skip by MAC_PTR. */
24608 break;
24609
24610 case DW_MACRO_define_strp:
24611 case DW_MACRO_undef_strp:
24612 case DW_MACRO_define_sup:
24613 case DW_MACRO_undef_sup:
24614 {
24615 unsigned int bytes_read;
24616
24617 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24618 mac_ptr += bytes_read;
24619 mac_ptr += offset_size;
24620 }
24621 break;
24622
24623 case DW_MACRO_import:
24624 case DW_MACRO_import_sup:
24625 /* Note that, according to the spec, a transparent include
24626 chain cannot call DW_MACRO_start_file. So, we can just
24627 skip this opcode. */
24628 mac_ptr += offset_size;
24629 break;
24630
24631 case DW_MACINFO_vendor_ext:
24632 /* Only skip the data by MAC_PTR. */
24633 if (!section_is_gnu)
24634 {
24635 unsigned int bytes_read;
24636
24637 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24638 mac_ptr += bytes_read;
24639 read_direct_string (abfd, mac_ptr, &bytes_read);
24640 mac_ptr += bytes_read;
24641 }
24642 /* FALLTHROUGH */
24643
24644 default:
24645 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24646 mac_ptr, mac_end, abfd, offset_size,
24647 section);
24648 if (mac_ptr == NULL)
24649 return;
24650 break;
24651 }
24652 DIAGNOSTIC_POP
24653 } while (macinfo_type != 0 && current_file == NULL);
24654
24655 /* Second pass: Process all entries.
24656
24657 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24658 command-line macro definitions/undefinitions. This flag is unset when we
24659 reach the first DW_MACINFO_start_file entry. */
24660
24661 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24662 htab_eq_pointer,
24663 NULL, xcalloc, xfree));
24664 mac_ptr = section->buffer + offset;
24665 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24666 *slot = (void *) mac_ptr;
24667 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24668 current_file, lh, section,
24669 section_is_gnu, 0, offset_size,
24670 include_hash.get ());
24671 }
24672
24673 /* Return the .debug_loc section to use for CU.
24674 For DWO files use .debug_loc.dwo. */
24675
24676 static struct dwarf2_section_info *
24677 cu_debug_loc_section (struct dwarf2_cu *cu)
24678 {
24679 struct dwarf2_per_objfile *dwarf2_per_objfile
24680 = cu->per_cu->dwarf2_per_objfile;
24681
24682 if (cu->dwo_unit)
24683 {
24684 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24685
24686 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24687 }
24688 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24689 : &dwarf2_per_objfile->loc);
24690 }
24691
24692 /* A helper function that fills in a dwarf2_loclist_baton. */
24693
24694 static void
24695 fill_in_loclist_baton (struct dwarf2_cu *cu,
24696 struct dwarf2_loclist_baton *baton,
24697 const struct attribute *attr)
24698 {
24699 struct dwarf2_per_objfile *dwarf2_per_objfile
24700 = cu->per_cu->dwarf2_per_objfile;
24701 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24702
24703 section->read (dwarf2_per_objfile->objfile);
24704
24705 baton->per_cu = cu->per_cu;
24706 gdb_assert (baton->per_cu);
24707 /* We don't know how long the location list is, but make sure we
24708 don't run off the edge of the section. */
24709 baton->size = section->size - DW_UNSND (attr);
24710 baton->data = section->buffer + DW_UNSND (attr);
24711 baton->base_address = cu->base_address;
24712 baton->from_dwo = cu->dwo_unit != NULL;
24713 }
24714
24715 static void
24716 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24717 struct dwarf2_cu *cu, int is_block)
24718 {
24719 struct dwarf2_per_objfile *dwarf2_per_objfile
24720 = cu->per_cu->dwarf2_per_objfile;
24721 struct objfile *objfile = dwarf2_per_objfile->objfile;
24722 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24723
24724 if (attr->form_is_section_offset ()
24725 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24726 the section. If so, fall through to the complaint in the
24727 other branch. */
24728 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24729 {
24730 struct dwarf2_loclist_baton *baton;
24731
24732 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24733
24734 fill_in_loclist_baton (cu, baton, attr);
24735
24736 if (cu->base_known == 0)
24737 complaint (_("Location list used without "
24738 "specifying the CU base address."));
24739
24740 SYMBOL_ACLASS_INDEX (sym) = (is_block
24741 ? dwarf2_loclist_block_index
24742 : dwarf2_loclist_index);
24743 SYMBOL_LOCATION_BATON (sym) = baton;
24744 }
24745 else
24746 {
24747 struct dwarf2_locexpr_baton *baton;
24748
24749 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24750 baton->per_cu = cu->per_cu;
24751 gdb_assert (baton->per_cu);
24752
24753 if (attr->form_is_block ())
24754 {
24755 /* Note that we're just copying the block's data pointer
24756 here, not the actual data. We're still pointing into the
24757 info_buffer for SYM's objfile; right now we never release
24758 that buffer, but when we do clean up properly this may
24759 need to change. */
24760 baton->size = DW_BLOCK (attr)->size;
24761 baton->data = DW_BLOCK (attr)->data;
24762 }
24763 else
24764 {
24765 dwarf2_invalid_attrib_class_complaint ("location description",
24766 sym->natural_name ());
24767 baton->size = 0;
24768 }
24769
24770 SYMBOL_ACLASS_INDEX (sym) = (is_block
24771 ? dwarf2_locexpr_block_index
24772 : dwarf2_locexpr_index);
24773 SYMBOL_LOCATION_BATON (sym) = baton;
24774 }
24775 }
24776
24777 /* Return the OBJFILE associated with the compilation unit CU. If CU
24778 came from a separate debuginfo file, then the master objfile is
24779 returned. */
24780
24781 struct objfile *
24782 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24783 {
24784 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24785
24786 /* Return the master objfile, so that we can report and look up the
24787 correct file containing this variable. */
24788 if (objfile->separate_debug_objfile_backlink)
24789 objfile = objfile->separate_debug_objfile_backlink;
24790
24791 return objfile;
24792 }
24793
24794 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24795 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24796 CU_HEADERP first. */
24797
24798 static const struct comp_unit_head *
24799 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24800 struct dwarf2_per_cu_data *per_cu)
24801 {
24802 const gdb_byte *info_ptr;
24803
24804 if (per_cu->cu)
24805 return &per_cu->cu->header;
24806
24807 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24808
24809 memset (cu_headerp, 0, sizeof (*cu_headerp));
24810 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24811 rcuh_kind::COMPILE);
24812
24813 return cu_headerp;
24814 }
24815
24816 /* Return the address size given in the compilation unit header for CU. */
24817
24818 int
24819 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24820 {
24821 struct comp_unit_head cu_header_local;
24822 const struct comp_unit_head *cu_headerp;
24823
24824 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24825
24826 return cu_headerp->addr_size;
24827 }
24828
24829 /* Return the offset size given in the compilation unit header for CU. */
24830
24831 int
24832 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24833 {
24834 struct comp_unit_head cu_header_local;
24835 const struct comp_unit_head *cu_headerp;
24836
24837 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24838
24839 return cu_headerp->offset_size;
24840 }
24841
24842 /* See its dwarf2loc.h declaration. */
24843
24844 int
24845 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24846 {
24847 struct comp_unit_head cu_header_local;
24848 const struct comp_unit_head *cu_headerp;
24849
24850 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24851
24852 if (cu_headerp->version == 2)
24853 return cu_headerp->addr_size;
24854 else
24855 return cu_headerp->offset_size;
24856 }
24857
24858 /* Return the text offset of the CU. The returned offset comes from
24859 this CU's objfile. If this objfile came from a separate debuginfo
24860 file, then the offset may be different from the corresponding
24861 offset in the parent objfile. */
24862
24863 CORE_ADDR
24864 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24865 {
24866 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
24867 }
24868
24869 /* Return a type that is a generic pointer type, the size of which matches
24870 the address size given in the compilation unit header for PER_CU. */
24871 static struct type *
24872 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24873 {
24874 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24875 struct type *void_type = objfile_type (objfile)->builtin_void;
24876 struct type *addr_type = lookup_pointer_type (void_type);
24877 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24878
24879 if (TYPE_LENGTH (addr_type) == addr_size)
24880 return addr_type;
24881
24882 addr_type
24883 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24884 return addr_type;
24885 }
24886
24887 /* Return DWARF version number of PER_CU. */
24888
24889 short
24890 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24891 {
24892 return per_cu->dwarf_version;
24893 }
24894
24895 /* Locate the .debug_info compilation unit from CU's objfile which contains
24896 the DIE at OFFSET. Raises an error on failure. */
24897
24898 static struct dwarf2_per_cu_data *
24899 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24900 unsigned int offset_in_dwz,
24901 struct dwarf2_per_objfile *dwarf2_per_objfile)
24902 {
24903 struct dwarf2_per_cu_data *this_cu;
24904 int low, high;
24905
24906 low = 0;
24907 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24908 while (high > low)
24909 {
24910 struct dwarf2_per_cu_data *mid_cu;
24911 int mid = low + (high - low) / 2;
24912
24913 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24914 if (mid_cu->is_dwz > offset_in_dwz
24915 || (mid_cu->is_dwz == offset_in_dwz
24916 && mid_cu->sect_off + mid_cu->length >= sect_off))
24917 high = mid;
24918 else
24919 low = mid + 1;
24920 }
24921 gdb_assert (low == high);
24922 this_cu = dwarf2_per_objfile->all_comp_units[low];
24923 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24924 {
24925 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24926 error (_("Dwarf Error: could not find partial DIE containing "
24927 "offset %s [in module %s]"),
24928 sect_offset_str (sect_off),
24929 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24930
24931 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24932 <= sect_off);
24933 return dwarf2_per_objfile->all_comp_units[low-1];
24934 }
24935 else
24936 {
24937 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24938 && sect_off >= this_cu->sect_off + this_cu->length)
24939 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24940 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24941 return this_cu;
24942 }
24943 }
24944
24945 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24946
24947 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24948 : per_cu (per_cu_),
24949 mark (false),
24950 has_loclist (false),
24951 checked_producer (false),
24952 producer_is_gxx_lt_4_6 (false),
24953 producer_is_gcc_lt_4_3 (false),
24954 producer_is_icc (false),
24955 producer_is_icc_lt_14 (false),
24956 producer_is_codewarrior (false),
24957 processing_has_namespace_info (false)
24958 {
24959 per_cu->cu = this;
24960 }
24961
24962 /* Destroy a dwarf2_cu. */
24963
24964 dwarf2_cu::~dwarf2_cu ()
24965 {
24966 per_cu->cu = NULL;
24967 }
24968
24969 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24970
24971 static void
24972 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24973 enum language pretend_language)
24974 {
24975 struct attribute *attr;
24976
24977 /* Set the language we're debugging. */
24978 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24979 if (attr != nullptr)
24980 set_cu_language (DW_UNSND (attr), cu);
24981 else
24982 {
24983 cu->language = pretend_language;
24984 cu->language_defn = language_def (cu->language);
24985 }
24986
24987 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24988 }
24989
24990 /* Increase the age counter on each cached compilation unit, and free
24991 any that are too old. */
24992
24993 static void
24994 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24995 {
24996 struct dwarf2_per_cu_data *per_cu, **last_chain;
24997
24998 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24999 per_cu = dwarf2_per_objfile->read_in_chain;
25000 while (per_cu != NULL)
25001 {
25002 per_cu->cu->last_used ++;
25003 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25004 dwarf2_mark (per_cu->cu);
25005 per_cu = per_cu->cu->read_in_chain;
25006 }
25007
25008 per_cu = dwarf2_per_objfile->read_in_chain;
25009 last_chain = &dwarf2_per_objfile->read_in_chain;
25010 while (per_cu != NULL)
25011 {
25012 struct dwarf2_per_cu_data *next_cu;
25013
25014 next_cu = per_cu->cu->read_in_chain;
25015
25016 if (!per_cu->cu->mark)
25017 {
25018 delete per_cu->cu;
25019 *last_chain = next_cu;
25020 }
25021 else
25022 last_chain = &per_cu->cu->read_in_chain;
25023
25024 per_cu = next_cu;
25025 }
25026 }
25027
25028 /* Remove a single compilation unit from the cache. */
25029
25030 static void
25031 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25032 {
25033 struct dwarf2_per_cu_data *per_cu, **last_chain;
25034 struct dwarf2_per_objfile *dwarf2_per_objfile
25035 = target_per_cu->dwarf2_per_objfile;
25036
25037 per_cu = dwarf2_per_objfile->read_in_chain;
25038 last_chain = &dwarf2_per_objfile->read_in_chain;
25039 while (per_cu != NULL)
25040 {
25041 struct dwarf2_per_cu_data *next_cu;
25042
25043 next_cu = per_cu->cu->read_in_chain;
25044
25045 if (per_cu == target_per_cu)
25046 {
25047 delete per_cu->cu;
25048 per_cu->cu = NULL;
25049 *last_chain = next_cu;
25050 break;
25051 }
25052 else
25053 last_chain = &per_cu->cu->read_in_chain;
25054
25055 per_cu = next_cu;
25056 }
25057 }
25058
25059 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25060 We store these in a hash table separate from the DIEs, and preserve them
25061 when the DIEs are flushed out of cache.
25062
25063 The CU "per_cu" pointer is needed because offset alone is not enough to
25064 uniquely identify the type. A file may have multiple .debug_types sections,
25065 or the type may come from a DWO file. Furthermore, while it's more logical
25066 to use per_cu->section+offset, with Fission the section with the data is in
25067 the DWO file but we don't know that section at the point we need it.
25068 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25069 because we can enter the lookup routine, get_die_type_at_offset, from
25070 outside this file, and thus won't necessarily have PER_CU->cu.
25071 Fortunately, PER_CU is stable for the life of the objfile. */
25072
25073 struct dwarf2_per_cu_offset_and_type
25074 {
25075 const struct dwarf2_per_cu_data *per_cu;
25076 sect_offset sect_off;
25077 struct type *type;
25078 };
25079
25080 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25081
25082 static hashval_t
25083 per_cu_offset_and_type_hash (const void *item)
25084 {
25085 const struct dwarf2_per_cu_offset_and_type *ofs
25086 = (const struct dwarf2_per_cu_offset_and_type *) item;
25087
25088 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25089 }
25090
25091 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25092
25093 static int
25094 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25095 {
25096 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25097 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25098 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25099 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25100
25101 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25102 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25103 }
25104
25105 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25106 table if necessary. For convenience, return TYPE.
25107
25108 The DIEs reading must have careful ordering to:
25109 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25110 reading current DIE.
25111 * Not trying to dereference contents of still incompletely read in types
25112 while reading in other DIEs.
25113 * Enable referencing still incompletely read in types just by a pointer to
25114 the type without accessing its fields.
25115
25116 Therefore caller should follow these rules:
25117 * Try to fetch any prerequisite types we may need to build this DIE type
25118 before building the type and calling set_die_type.
25119 * After building type call set_die_type for current DIE as soon as
25120 possible before fetching more types to complete the current type.
25121 * Make the type as complete as possible before fetching more types. */
25122
25123 static struct type *
25124 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25125 {
25126 struct dwarf2_per_objfile *dwarf2_per_objfile
25127 = cu->per_cu->dwarf2_per_objfile;
25128 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25129 struct objfile *objfile = dwarf2_per_objfile->objfile;
25130 struct attribute *attr;
25131 struct dynamic_prop prop;
25132
25133 /* For Ada types, make sure that the gnat-specific data is always
25134 initialized (if not already set). There are a few types where
25135 we should not be doing so, because the type-specific area is
25136 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25137 where the type-specific area is used to store the floatformat).
25138 But this is not a problem, because the gnat-specific information
25139 is actually not needed for these types. */
25140 if (need_gnat_info (cu)
25141 && TYPE_CODE (type) != TYPE_CODE_FUNC
25142 && TYPE_CODE (type) != TYPE_CODE_FLT
25143 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25144 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25145 && TYPE_CODE (type) != TYPE_CODE_METHOD
25146 && !HAVE_GNAT_AUX_INFO (type))
25147 INIT_GNAT_SPECIFIC (type);
25148
25149 /* Read DW_AT_allocated and set in type. */
25150 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25151 if (attr != NULL && attr->form_is_block ())
25152 {
25153 struct type *prop_type
25154 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25155 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25156 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25157 }
25158 else if (attr != NULL)
25159 {
25160 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25161 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25162 sect_offset_str (die->sect_off));
25163 }
25164
25165 /* Read DW_AT_associated and set in type. */
25166 attr = dwarf2_attr (die, DW_AT_associated, cu);
25167 if (attr != NULL && attr->form_is_block ())
25168 {
25169 struct type *prop_type
25170 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25171 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25172 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25173 }
25174 else if (attr != NULL)
25175 {
25176 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25177 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25178 sect_offset_str (die->sect_off));
25179 }
25180
25181 /* Read DW_AT_data_location and set in type. */
25182 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25183 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25184 dwarf2_per_cu_addr_type (cu->per_cu)))
25185 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25186
25187 if (dwarf2_per_objfile->die_type_hash == NULL)
25188 dwarf2_per_objfile->die_type_hash
25189 = htab_up (htab_create_alloc (127,
25190 per_cu_offset_and_type_hash,
25191 per_cu_offset_and_type_eq,
25192 NULL, xcalloc, xfree));
25193
25194 ofs.per_cu = cu->per_cu;
25195 ofs.sect_off = die->sect_off;
25196 ofs.type = type;
25197 slot = (struct dwarf2_per_cu_offset_and_type **)
25198 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
25199 if (*slot)
25200 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25201 sect_offset_str (die->sect_off));
25202 *slot = XOBNEW (&objfile->objfile_obstack,
25203 struct dwarf2_per_cu_offset_and_type);
25204 **slot = ofs;
25205 return type;
25206 }
25207
25208 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25209 or return NULL if the die does not have a saved type. */
25210
25211 static struct type *
25212 get_die_type_at_offset (sect_offset sect_off,
25213 struct dwarf2_per_cu_data *per_cu)
25214 {
25215 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25216 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25217
25218 if (dwarf2_per_objfile->die_type_hash == NULL)
25219 return NULL;
25220
25221 ofs.per_cu = per_cu;
25222 ofs.sect_off = sect_off;
25223 slot = ((struct dwarf2_per_cu_offset_and_type *)
25224 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
25225 if (slot)
25226 return slot->type;
25227 else
25228 return NULL;
25229 }
25230
25231 /* Look up the type for DIE in CU in die_type_hash,
25232 or return NULL if DIE does not have a saved type. */
25233
25234 static struct type *
25235 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25236 {
25237 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25238 }
25239
25240 /* Add a dependence relationship from CU to REF_PER_CU. */
25241
25242 static void
25243 dwarf2_add_dependence (struct dwarf2_cu *cu,
25244 struct dwarf2_per_cu_data *ref_per_cu)
25245 {
25246 void **slot;
25247
25248 if (cu->dependencies == NULL)
25249 cu->dependencies
25250 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25251 NULL, &cu->comp_unit_obstack,
25252 hashtab_obstack_allocate,
25253 dummy_obstack_deallocate);
25254
25255 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25256 if (*slot == NULL)
25257 *slot = ref_per_cu;
25258 }
25259
25260 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25261 Set the mark field in every compilation unit in the
25262 cache that we must keep because we are keeping CU. */
25263
25264 static int
25265 dwarf2_mark_helper (void **slot, void *data)
25266 {
25267 struct dwarf2_per_cu_data *per_cu;
25268
25269 per_cu = (struct dwarf2_per_cu_data *) *slot;
25270
25271 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25272 reading of the chain. As such dependencies remain valid it is not much
25273 useful to track and undo them during QUIT cleanups. */
25274 if (per_cu->cu == NULL)
25275 return 1;
25276
25277 if (per_cu->cu->mark)
25278 return 1;
25279 per_cu->cu->mark = true;
25280
25281 if (per_cu->cu->dependencies != NULL)
25282 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25283
25284 return 1;
25285 }
25286
25287 /* Set the mark field in CU and in every other compilation unit in the
25288 cache that we must keep because we are keeping CU. */
25289
25290 static void
25291 dwarf2_mark (struct dwarf2_cu *cu)
25292 {
25293 if (cu->mark)
25294 return;
25295 cu->mark = true;
25296 if (cu->dependencies != NULL)
25297 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25298 }
25299
25300 static void
25301 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25302 {
25303 while (per_cu)
25304 {
25305 per_cu->cu->mark = false;
25306 per_cu = per_cu->cu->read_in_chain;
25307 }
25308 }
25309
25310 /* Trivial hash function for partial_die_info: the hash value of a DIE
25311 is its offset in .debug_info for this objfile. */
25312
25313 static hashval_t
25314 partial_die_hash (const void *item)
25315 {
25316 const struct partial_die_info *part_die
25317 = (const struct partial_die_info *) item;
25318
25319 return to_underlying (part_die->sect_off);
25320 }
25321
25322 /* Trivial comparison function for partial_die_info structures: two DIEs
25323 are equal if they have the same offset. */
25324
25325 static int
25326 partial_die_eq (const void *item_lhs, const void *item_rhs)
25327 {
25328 const struct partial_die_info *part_die_lhs
25329 = (const struct partial_die_info *) item_lhs;
25330 const struct partial_die_info *part_die_rhs
25331 = (const struct partial_die_info *) item_rhs;
25332
25333 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25334 }
25335
25336 struct cmd_list_element *set_dwarf_cmdlist;
25337 struct cmd_list_element *show_dwarf_cmdlist;
25338
25339 static void
25340 set_dwarf_cmd (const char *args, int from_tty)
25341 {
25342 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25343 gdb_stdout);
25344 }
25345
25346 static void
25347 show_dwarf_cmd (const char *args, int from_tty)
25348 {
25349 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25350 }
25351
25352 static void
25353 show_check_physname (struct ui_file *file, int from_tty,
25354 struct cmd_list_element *c, const char *value)
25355 {
25356 fprintf_filtered (file,
25357 _("Whether to check \"physname\" is %s.\n"),
25358 value);
25359 }
25360
25361 void _initialize_dwarf2_read ();
25362 void
25363 _initialize_dwarf2_read ()
25364 {
25365 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25366 Set DWARF specific variables.\n\
25367 Configure DWARF variables such as the cache size."),
25368 &set_dwarf_cmdlist, "maintenance set dwarf ",
25369 0/*allow-unknown*/, &maintenance_set_cmdlist);
25370
25371 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25372 Show DWARF specific variables.\n\
25373 Show DWARF variables such as the cache size."),
25374 &show_dwarf_cmdlist, "maintenance show dwarf ",
25375 0/*allow-unknown*/, &maintenance_show_cmdlist);
25376
25377 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25378 &dwarf_max_cache_age, _("\
25379 Set the upper bound on the age of cached DWARF compilation units."), _("\
25380 Show the upper bound on the age of cached DWARF compilation units."), _("\
25381 A higher limit means that cached compilation units will be stored\n\
25382 in memory longer, and more total memory will be used. Zero disables\n\
25383 caching, which can slow down startup."),
25384 NULL,
25385 show_dwarf_max_cache_age,
25386 &set_dwarf_cmdlist,
25387 &show_dwarf_cmdlist);
25388
25389 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25390 Set debugging of the DWARF reader."), _("\
25391 Show debugging of the DWARF reader."), _("\
25392 When enabled (non-zero), debugging messages are printed during DWARF\n\
25393 reading and symtab expansion. A value of 1 (one) provides basic\n\
25394 information. A value greater than 1 provides more verbose information."),
25395 NULL,
25396 NULL,
25397 &setdebuglist, &showdebuglist);
25398
25399 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25400 Set debugging of the DWARF DIE reader."), _("\
25401 Show debugging of the DWARF DIE reader."), _("\
25402 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25403 The value is the maximum depth to print."),
25404 NULL,
25405 NULL,
25406 &setdebuglist, &showdebuglist);
25407
25408 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25409 Set debugging of the dwarf line reader."), _("\
25410 Show debugging of the dwarf line reader."), _("\
25411 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25412 A value of 1 (one) provides basic information.\n\
25413 A value greater than 1 provides more verbose information."),
25414 NULL,
25415 NULL,
25416 &setdebuglist, &showdebuglist);
25417
25418 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25419 Set cross-checking of \"physname\" code against demangler."), _("\
25420 Show cross-checking of \"physname\" code against demangler."), _("\
25421 When enabled, GDB's internal \"physname\" code is checked against\n\
25422 the demangler."),
25423 NULL, show_check_physname,
25424 &setdebuglist, &showdebuglist);
25425
25426 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25427 no_class, &use_deprecated_index_sections, _("\
25428 Set whether to use deprecated gdb_index sections."), _("\
25429 Show whether to use deprecated gdb_index sections."), _("\
25430 When enabled, deprecated .gdb_index sections are used anyway.\n\
25431 Normally they are ignored either because of a missing feature or\n\
25432 performance issue.\n\
25433 Warning: This option must be enabled before gdb reads the file."),
25434 NULL,
25435 NULL,
25436 &setlist, &showlist);
25437
25438 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25439 &dwarf2_locexpr_funcs);
25440 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25441 &dwarf2_loclist_funcs);
25442
25443 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25444 &dwarf2_block_frame_base_locexpr_funcs);
25445 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25446 &dwarf2_block_frame_base_loclist_funcs);
25447
25448 #if GDB_SELF_TEST
25449 selftests::register_test ("dw2_expand_symtabs_matching",
25450 selftests::dw2_expand_symtabs_matching::run_test);
25451 #endif
25452 }
This page took 0.737314 seconds and 4 git commands to generate.