Introduce die_info::has_children
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/index-cache.h"
36 #include "dwarf2/index-common.h"
37 #include "dwarf2/leb.h"
38 #include "bfd.h"
39 #include "elf-bfd.h"
40 #include "symtab.h"
41 #include "gdbtypes.h"
42 #include "objfiles.h"
43 #include "dwarf2.h"
44 #include "buildsym.h"
45 #include "demangle.h"
46 #include "gdb-demangle.h"
47 #include "filenames.h" /* for DOSish file names */
48 #include "macrotab.h"
49 #include "language.h"
50 #include "complaints.h"
51 #include "dwarf2/expr.h"
52 #include "dwarf2/loc.h"
53 #include "cp-support.h"
54 #include "hashtab.h"
55 #include "command.h"
56 #include "gdbcmd.h"
57 #include "block.h"
58 #include "addrmap.h"
59 #include "typeprint.h"
60 #include "psympriv.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "build-id.h"
70 #include "namespace.h"
71 #include "gdbsupport/function-view.h"
72 #include "gdbsupport/gdb_optional.h"
73 #include "gdbsupport/underlying.h"
74 #include "gdbsupport/hash_enum.h"
75 #include "filename-seen-cache.h"
76 #include "producer.h"
77 #include <fcntl.h>
78 #include <algorithm>
79 #include <unordered_map>
80 #include "gdbsupport/selftest.h"
81 #include "rust-lang.h"
82 #include "gdbsupport/pathstuff.h"
83
84 /* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87 static unsigned int dwarf_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static unsigned int dwarf_die_debug = 0;
91
92 /* When non-zero, dump line number entries as they are read in. */
93 static unsigned int dwarf_line_debug = 0;
94
95 /* When true, cross-check physname against demangler. */
96 static bool check_physname = false;
97
98 /* When true, do not reject deprecated .gdb_index sections. */
99 static bool use_deprecated_index_sections = false;
100
101 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
102
103 /* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105 static int dwarf2_locexpr_index;
106 static int dwarf2_loclist_index;
107 static int dwarf2_locexpr_block_index;
108 static int dwarf2_loclist_block_index;
109
110 /* An index into a (C++) symbol name component in a symbol name as
111 recorded in the mapped_index's symbol table. For each C++ symbol
112 in the symbol table, we record one entry for the start of each
113 component in the symbol in a table of name components, and then
114 sort the table, in order to be able to binary search symbol names,
115 ignoring leading namespaces, both completion and regular look up.
116 For example, for symbol "A::B::C", we'll have an entry that points
117 to "A::B::C", another that points to "B::C", and another for "C".
118 Note that function symbols in GDB index have no parameter
119 information, just the function/method names. You can convert a
120 name_component to a "const char *" using the
121 'mapped_index::symbol_name_at(offset_type)' method. */
122
123 struct name_component
124 {
125 /* Offset in the symbol name where the component starts. Stored as
126 a (32-bit) offset instead of a pointer to save memory and improve
127 locality on 64-bit architectures. */
128 offset_type name_offset;
129
130 /* The symbol's index in the symbol and constant pool tables of a
131 mapped_index. */
132 offset_type idx;
133 };
134
135 /* Base class containing bits shared by both .gdb_index and
136 .debug_name indexes. */
137
138 struct mapped_index_base
139 {
140 mapped_index_base () = default;
141 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
142
143 /* The name_component table (a sorted vector). See name_component's
144 description above. */
145 std::vector<name_component> name_components;
146
147 /* How NAME_COMPONENTS is sorted. */
148 enum case_sensitivity name_components_casing;
149
150 /* Return the number of names in the symbol table. */
151 virtual size_t symbol_name_count () const = 0;
152
153 /* Get the name of the symbol at IDX in the symbol table. */
154 virtual const char *symbol_name_at (offset_type idx) const = 0;
155
156 /* Return whether the name at IDX in the symbol table should be
157 ignored. */
158 virtual bool symbol_name_slot_invalid (offset_type idx) const
159 {
160 return false;
161 }
162
163 /* Build the symbol name component sorted vector, if we haven't
164 yet. */
165 void build_name_components ();
166
167 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
168 possible matches for LN_NO_PARAMS in the name component
169 vector. */
170 std::pair<std::vector<name_component>::const_iterator,
171 std::vector<name_component>::const_iterator>
172 find_name_components_bounds (const lookup_name_info &ln_no_params,
173 enum language lang) const;
174
175 /* Prevent deleting/destroying via a base class pointer. */
176 protected:
177 ~mapped_index_base() = default;
178 };
179
180 /* A description of the mapped index. The file format is described in
181 a comment by the code that writes the index. */
182 struct mapped_index final : public mapped_index_base
183 {
184 /* A slot/bucket in the symbol table hash. */
185 struct symbol_table_slot
186 {
187 const offset_type name;
188 const offset_type vec;
189 };
190
191 /* Index data format version. */
192 int version = 0;
193
194 /* The address table data. */
195 gdb::array_view<const gdb_byte> address_table;
196
197 /* The symbol table, implemented as a hash table. */
198 gdb::array_view<symbol_table_slot> symbol_table;
199
200 /* A pointer to the constant pool. */
201 const char *constant_pool = nullptr;
202
203 bool symbol_name_slot_invalid (offset_type idx) const override
204 {
205 const auto &bucket = this->symbol_table[idx];
206 return bucket.name == 0 && bucket.vec == 0;
207 }
208
209 /* Convenience method to get at the name of the symbol at IDX in the
210 symbol table. */
211 const char *symbol_name_at (offset_type idx) const override
212 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
213
214 size_t symbol_name_count () const override
215 { return this->symbol_table.size (); }
216 };
217
218 /* A description of the mapped .debug_names.
219 Uninitialized map has CU_COUNT 0. */
220 struct mapped_debug_names final : public mapped_index_base
221 {
222 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
223 : dwarf2_per_objfile (dwarf2_per_objfile_)
224 {}
225
226 struct dwarf2_per_objfile *dwarf2_per_objfile;
227 bfd_endian dwarf5_byte_order;
228 bool dwarf5_is_dwarf64;
229 bool augmentation_is_gdb;
230 uint8_t offset_size;
231 uint32_t cu_count = 0;
232 uint32_t tu_count, bucket_count, name_count;
233 const gdb_byte *cu_table_reordered, *tu_table_reordered;
234 const uint32_t *bucket_table_reordered, *hash_table_reordered;
235 const gdb_byte *name_table_string_offs_reordered;
236 const gdb_byte *name_table_entry_offs_reordered;
237 const gdb_byte *entry_pool;
238
239 struct index_val
240 {
241 ULONGEST dwarf_tag;
242 struct attr
243 {
244 /* Attribute name DW_IDX_*. */
245 ULONGEST dw_idx;
246
247 /* Attribute form DW_FORM_*. */
248 ULONGEST form;
249
250 /* Value if FORM is DW_FORM_implicit_const. */
251 LONGEST implicit_const;
252 };
253 std::vector<attr> attr_vec;
254 };
255
256 std::unordered_map<ULONGEST, index_val> abbrev_map;
257
258 const char *namei_to_name (uint32_t namei) const;
259
260 /* Implementation of the mapped_index_base virtual interface, for
261 the name_components cache. */
262
263 const char *symbol_name_at (offset_type idx) const override
264 { return namei_to_name (idx); }
265
266 size_t symbol_name_count () const override
267 { return this->name_count; }
268 };
269
270 /* See dwarf2read.h. */
271
272 dwarf2_per_objfile *
273 get_dwarf2_per_objfile (struct objfile *objfile)
274 {
275 return dwarf2_objfile_data_key.get (objfile);
276 }
277
278 /* Default names of the debugging sections. */
279
280 /* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
283 static const struct dwarf2_debug_sections dwarf2_elf_names =
284 {
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_loclists", ".zdebug_loclists" },
290 { ".debug_macinfo", ".zdebug_macinfo" },
291 { ".debug_macro", ".zdebug_macro" },
292 { ".debug_str", ".zdebug_str" },
293 { ".debug_str_offsets", ".zdebug_str_offsets" },
294 { ".debug_line_str", ".zdebug_line_str" },
295 { ".debug_ranges", ".zdebug_ranges" },
296 { ".debug_rnglists", ".zdebug_rnglists" },
297 { ".debug_types", ".zdebug_types" },
298 { ".debug_addr", ".zdebug_addr" },
299 { ".debug_frame", ".zdebug_frame" },
300 { ".eh_frame", NULL },
301 { ".gdb_index", ".zgdb_index" },
302 { ".debug_names", ".zdebug_names" },
303 { ".debug_aranges", ".zdebug_aranges" },
304 23
305 };
306
307 /* List of DWO/DWP sections. */
308
309 static const struct dwop_section_names
310 {
311 struct dwarf2_section_names abbrev_dwo;
312 struct dwarf2_section_names info_dwo;
313 struct dwarf2_section_names line_dwo;
314 struct dwarf2_section_names loc_dwo;
315 struct dwarf2_section_names loclists_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
338 };
339
340 /* local data types */
341
342 /* The data in a compilation unit header, after target2host
343 translation, looks like this. */
344 struct comp_unit_head
345 {
346 unsigned int length;
347 short version;
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
350 sect_offset abbrev_sect_off;
351
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
354
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
357
358 enum dwarf_unit_type unit_type;
359
360 /* Offset to the first byte of this compilation unit header in the
361 .debug_info section, for resolving relative reference dies. */
362 sect_offset sect_off;
363
364 /* Offset to first die in this cu from the start of the cu.
365 This will be the first byte following the compilation unit header. */
366 cu_offset first_die_cu_offset;
367
368
369 /* 64-bit signature of this unit. For type units, it denotes the signature of
370 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
371 Also used in DWARF 5, to denote the dwo id when the unit type is
372 DW_UT_skeleton or DW_UT_split_compile. */
373 ULONGEST signature;
374
375 /* For types, offset in the type's DIE of the type defined by this TU. */
376 cu_offset type_cu_offset_in_tu;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
428
429 /* Non-zero if base_address has been set. */
430 int base_known = 0;
431
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
435
436 const char *producer = nullptr;
437
438 private:
439 /* The symtab builder for this CU. This is only non-NULL when full
440 symbols are being read. */
441 std::unique_ptr<buildsym_compunit> m_builder;
442
443 public:
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
453 struct pending **list_in_scope = nullptr;
454
455 /* Hash table holding all the loaded partial DIEs
456 with partial_die->offset.SECT_OFF as hash. */
457 htab_t partial_dies = nullptr;
458
459 /* Storage for things with the same lifetime as this read-in compilation
460 unit, including partial DIEs. */
461 auto_obstack comp_unit_obstack;
462
463 /* When multiple dwarf2_cu structures are living in memory, this field
464 chains them all together, so that they can be released efficiently.
465 We will probably also want a generation counter so that most-recently-used
466 compilation units are cached... */
467 struct dwarf2_per_cu_data *read_in_chain = nullptr;
468
469 /* Backlink to our per_cu entry. */
470 struct dwarf2_per_cu_data *per_cu;
471
472 /* How many compilation units ago was this CU last referenced? */
473 int last_used = 0;
474
475 /* A hash table of DIE cu_offset for following references with
476 die_info->offset.sect_off as hash. */
477 htab_t die_hash = nullptr;
478
479 /* Full DIEs if read in. */
480 struct die_info *dies = nullptr;
481
482 /* A set of pointers to dwarf2_per_cu_data objects for compilation
483 units referenced by this one. Only set during full symbol processing;
484 partial symbol tables do not have dependencies. */
485 htab_t dependencies = nullptr;
486
487 /* Header data from the line table, during full symbol processing. */
488 struct line_header *line_header = nullptr;
489 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
490 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
491 this is the DW_TAG_compile_unit die for this CU. We'll hold on
492 to the line header as long as this DIE is being processed. See
493 process_die_scope. */
494 die_info *line_header_die_owner = nullptr;
495
496 /* A list of methods which need to have physnames computed
497 after all type information has been read. */
498 std::vector<delayed_method_info> method_list;
499
500 /* To be copied to symtab->call_site_htab. */
501 htab_t call_site_htab = nullptr;
502
503 /* Non-NULL if this CU came from a DWO file.
504 There is an invariant here that is important to remember:
505 Except for attributes copied from the top level DIE in the "main"
506 (or "stub") file in preparation for reading the DWO file
507 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
508 Either there isn't a DWO file (in which case this is NULL and the point
509 is moot), or there is and either we're not going to read it (in which
510 case this is NULL) or there is and we are reading it (in which case this
511 is non-NULL). */
512 struct dwo_unit *dwo_unit = nullptr;
513
514 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
515 Note this value comes from the Fission stub CU/TU's DIE. */
516 gdb::optional<ULONGEST> addr_base;
517
518 /* The DW_AT_rnglists_base attribute if present.
519 Note this value comes from the Fission stub CU/TU's DIE.
520 Also note that the value is zero in the non-DWO case so this value can
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
527 ULONGEST ranges_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab.
611 This PER_CU is recognized by having no section. */
612 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
613 struct dwarf2_per_cu_data per_cu;
614
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
618 std::vector<signatured_type *> *tus;
619
620 /* The compunit symtab.
621 Type units in a group needn't all be defined in the same source file,
622 so we create an essentially anonymous symtab as the compunit symtab. */
623 struct compunit_symtab *compunit_symtab;
624
625 /* The data used to construct the hash key. */
626 struct stmt_list_hash hash;
627
628 /* The number of symtabs from the line header.
629 The value here must match line_header.num_file_names. */
630 unsigned int num_symtabs;
631
632 /* The symbol tables for this TU (obtained from the files listed in
633 DW_AT_stmt_list).
634 WARNING: The order of entries here must match the order of entries
635 in the line header. After the first TU using this type_unit_group, the
636 line header for the subsequent TUs is recreated from this. This is done
637 because we need to use the same symtabs for each TU using the same
638 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
639 there's no guarantee the line header doesn't have duplicate entries. */
640 struct symtab **symtabs;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_t cus {};
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_t tus {};
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_t loaded_cus {};
872 htab_t loaded_tus {};
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (struct dwarf2_per_cu_data *this_cu,
918 struct abbrev_table *abbrev_table,
919 int use_existing_cu, int keep,
920 bool skip_partial);
921
922 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 ~cutu_reader ();
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 bool dummy_p = false;
933
934 private:
935 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
936 int use_existing_cu, int keep);
937
938 struct dwarf2_per_cu_data *m_this_cu;
939 int m_keep = 0;
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947 };
948
949 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
950 later. */
951 typedef int dir_index;
952
953 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
954 and later. */
955 typedef int file_name_index;
956
957 struct file_entry
958 {
959 file_entry () = default;
960
961 file_entry (const char *name_, dir_index d_index_,
962 unsigned int mod_time_, unsigned int length_)
963 : name (name_),
964 d_index (d_index_),
965 mod_time (mod_time_),
966 length (length_)
967 {}
968
969 /* Return the include directory at D_INDEX stored in LH. Returns
970 NULL if D_INDEX is out of bounds. */
971 const char *include_dir (const line_header *lh) const;
972
973 /* The file name. Note this is an observing pointer. The memory is
974 owned by debug_line_buffer. */
975 const char *name {};
976
977 /* The directory index (1-based). */
978 dir_index d_index {};
979
980 unsigned int mod_time {};
981
982 unsigned int length {};
983
984 /* True if referenced by the Line Number Program. */
985 bool included_p {};
986
987 /* The associated symbol table, if any. */
988 struct symtab *symtab {};
989 };
990
991 /* The line number information for a compilation unit (found in the
992 .debug_line section) begins with a "statement program header",
993 which contains the following information. */
994 struct line_header
995 {
996 line_header ()
997 : offset_in_dwz {}
998 {}
999
1000 /* Add an entry to the include directory table. */
1001 void add_include_dir (const char *include_dir);
1002
1003 /* Add an entry to the file name table. */
1004 void add_file_name (const char *name, dir_index d_index,
1005 unsigned int mod_time, unsigned int length);
1006
1007 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1008 Returns NULL if INDEX is out of bounds. */
1009 const char *include_dir_at (dir_index index) const
1010 {
1011 int vec_index;
1012 if (version >= 5)
1013 vec_index = index;
1014 else
1015 vec_index = index - 1;
1016 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1017 return NULL;
1018 return m_include_dirs[vec_index];
1019 }
1020
1021 bool is_valid_file_index (int file_index)
1022 {
1023 if (version >= 5)
1024 return 0 <= file_index && file_index < file_names_size ();
1025 return 1 <= file_index && file_index <= file_names_size ();
1026 }
1027
1028 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1029 Returns NULL if INDEX is out of bounds. */
1030 file_entry *file_name_at (file_name_index index)
1031 {
1032 int vec_index;
1033 if (version >= 5)
1034 vec_index = index;
1035 else
1036 vec_index = index - 1;
1037 if (vec_index < 0 || vec_index >= m_file_names.size ())
1038 return NULL;
1039 return &m_file_names[vec_index];
1040 }
1041
1042 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1043 this method should only be used to iterate through all file entries in an
1044 index-agnostic manner. */
1045 std::vector<file_entry> &file_names ()
1046 { return m_file_names; }
1047
1048 /* Offset of line number information in .debug_line section. */
1049 sect_offset sect_off {};
1050
1051 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1052 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1053
1054 unsigned int total_length {};
1055 unsigned short version {};
1056 unsigned int header_length {};
1057 unsigned char minimum_instruction_length {};
1058 unsigned char maximum_ops_per_instruction {};
1059 unsigned char default_is_stmt {};
1060 int line_base {};
1061 unsigned char line_range {};
1062 unsigned char opcode_base {};
1063
1064 /* standard_opcode_lengths[i] is the number of operands for the
1065 standard opcode whose value is i. This means that
1066 standard_opcode_lengths[0] is unused, and the last meaningful
1067 element is standard_opcode_lengths[opcode_base - 1]. */
1068 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1069
1070 int file_names_size ()
1071 { return m_file_names.size(); }
1072
1073 /* The start and end of the statement program following this
1074 header. These point into dwarf2_per_objfile->line_buffer. */
1075 const gdb_byte *statement_program_start {}, *statement_program_end {};
1076
1077 private:
1078 /* The include_directories table. Note these are observing
1079 pointers. The memory is owned by debug_line_buffer. */
1080 std::vector<const char *> m_include_dirs;
1081
1082 /* The file_names table. This is private because the meaning of indexes
1083 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1084 before, and is 0 in DWARF 5 and later). So the client should use
1085 file_name_at method for access. */
1086 std::vector<file_entry> m_file_names;
1087 };
1088
1089 typedef std::unique_ptr<line_header> line_header_up;
1090
1091 const char *
1092 file_entry::include_dir (const line_header *lh) const
1093 {
1094 return lh->include_dir_at (d_index);
1095 }
1096
1097 /* When we construct a partial symbol table entry we only
1098 need this much information. */
1099 struct partial_die_info : public allocate_on_obstack
1100 {
1101 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1102
1103 /* Disable assign but still keep copy ctor, which is needed
1104 load_partial_dies. */
1105 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1106
1107 /* Adjust the partial die before generating a symbol for it. This
1108 function may set the is_external flag or change the DIE's
1109 name. */
1110 void fixup (struct dwarf2_cu *cu);
1111
1112 /* Read a minimal amount of information into the minimal die
1113 structure. */
1114 const gdb_byte *read (const struct die_reader_specs *reader,
1115 const struct abbrev_info &abbrev,
1116 const gdb_byte *info_ptr);
1117
1118 /* Offset of this DIE. */
1119 const sect_offset sect_off;
1120
1121 /* DWARF-2 tag for this DIE. */
1122 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1123
1124 /* Assorted flags describing the data found in this DIE. */
1125 const unsigned int has_children : 1;
1126
1127 unsigned int is_external : 1;
1128 unsigned int is_declaration : 1;
1129 unsigned int has_type : 1;
1130 unsigned int has_specification : 1;
1131 unsigned int has_pc_info : 1;
1132 unsigned int may_be_inlined : 1;
1133
1134 /* This DIE has been marked DW_AT_main_subprogram. */
1135 unsigned int main_subprogram : 1;
1136
1137 /* Flag set if the SCOPE field of this structure has been
1138 computed. */
1139 unsigned int scope_set : 1;
1140
1141 /* Flag set if the DIE has a byte_size attribute. */
1142 unsigned int has_byte_size : 1;
1143
1144 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1145 unsigned int has_const_value : 1;
1146
1147 /* Flag set if any of the DIE's children are template arguments. */
1148 unsigned int has_template_arguments : 1;
1149
1150 /* Flag set if fixup has been called on this die. */
1151 unsigned int fixup_called : 1;
1152
1153 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1154 unsigned int is_dwz : 1;
1155
1156 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1157 unsigned int spec_is_dwz : 1;
1158
1159 /* The name of this DIE. Normally the value of DW_AT_name, but
1160 sometimes a default name for unnamed DIEs. */
1161 const char *name = nullptr;
1162
1163 /* The linkage name, if present. */
1164 const char *linkage_name = nullptr;
1165
1166 /* The scope to prepend to our children. This is generally
1167 allocated on the comp_unit_obstack, so will disappear
1168 when this compilation unit leaves the cache. */
1169 const char *scope = nullptr;
1170
1171 /* Some data associated with the partial DIE. The tag determines
1172 which field is live. */
1173 union
1174 {
1175 /* The location description associated with this DIE, if any. */
1176 struct dwarf_block *locdesc;
1177 /* The offset of an import, for DW_TAG_imported_unit. */
1178 sect_offset sect_off;
1179 } d {};
1180
1181 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1182 CORE_ADDR lowpc = 0;
1183 CORE_ADDR highpc = 0;
1184
1185 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1186 DW_AT_sibling, if any. */
1187 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1188 could return DW_AT_sibling values to its caller load_partial_dies. */
1189 const gdb_byte *sibling = nullptr;
1190
1191 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1192 DW_AT_specification (or DW_AT_abstract_origin or
1193 DW_AT_extension). */
1194 sect_offset spec_offset {};
1195
1196 /* Pointers to this DIE's parent, first child, and next sibling,
1197 if any. */
1198 struct partial_die_info *die_parent = nullptr;
1199 struct partial_die_info *die_child = nullptr;
1200 struct partial_die_info *die_sibling = nullptr;
1201
1202 friend struct partial_die_info *
1203 dwarf2_cu::find_partial_die (sect_offset sect_off);
1204
1205 private:
1206 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1207 partial_die_info (sect_offset sect_off)
1208 : partial_die_info (sect_off, DW_TAG_padding, 0)
1209 {
1210 }
1211
1212 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1213 int has_children_)
1214 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1215 {
1216 is_external = 0;
1217 is_declaration = 0;
1218 has_type = 0;
1219 has_specification = 0;
1220 has_pc_info = 0;
1221 may_be_inlined = 0;
1222 main_subprogram = 0;
1223 scope_set = 0;
1224 has_byte_size = 0;
1225 has_const_value = 0;
1226 has_template_arguments = 0;
1227 fixup_called = 0;
1228 is_dwz = 0;
1229 spec_is_dwz = 0;
1230 }
1231 };
1232
1233 /* This data structure holds a complete die structure. */
1234 struct die_info
1235 {
1236 /* DWARF-2 tag for this DIE. */
1237 ENUM_BITFIELD(dwarf_tag) tag : 16;
1238
1239 /* Number of attributes */
1240 unsigned char num_attrs;
1241
1242 /* True if we're presently building the full type name for the
1243 type derived from this DIE. */
1244 unsigned char building_fullname : 1;
1245
1246 /* True if this die is in process. PR 16581. */
1247 unsigned char in_process : 1;
1248
1249 /* True if this DIE has children. */
1250 unsigned char has_children : 1;
1251
1252 /* Abbrev number */
1253 unsigned int abbrev;
1254
1255 /* Offset in .debug_info or .debug_types section. */
1256 sect_offset sect_off;
1257
1258 /* The dies in a compilation unit form an n-ary tree. PARENT
1259 points to this die's parent; CHILD points to the first child of
1260 this node; and all the children of a given node are chained
1261 together via their SIBLING fields. */
1262 struct die_info *child; /* Its first child, if any. */
1263 struct die_info *sibling; /* Its next sibling, if any. */
1264 struct die_info *parent; /* Its parent, if any. */
1265
1266 /* An array of attributes, with NUM_ATTRS elements. There may be
1267 zero, but it's not common and zero-sized arrays are not
1268 sufficiently portable C. */
1269 struct attribute attrs[1];
1270 };
1271
1272 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1273 but this would require a corresponding change in unpack_field_as_long
1274 and friends. */
1275 static int bits_per_byte = 8;
1276
1277 /* When reading a variant or variant part, we track a bit more
1278 information about the field, and store it in an object of this
1279 type. */
1280
1281 struct variant_field
1282 {
1283 /* If we see a DW_TAG_variant, then this will be the discriminant
1284 value. */
1285 ULONGEST discriminant_value;
1286 /* If we see a DW_TAG_variant, then this will be set if this is the
1287 default branch. */
1288 bool default_branch;
1289 /* While reading a DW_TAG_variant_part, this will be set if this
1290 field is the discriminant. */
1291 bool is_discriminant;
1292 };
1293
1294 struct nextfield
1295 {
1296 int accessibility = 0;
1297 int virtuality = 0;
1298 /* Extra information to describe a variant or variant part. */
1299 struct variant_field variant {};
1300 struct field field {};
1301 };
1302
1303 struct fnfieldlist
1304 {
1305 const char *name = nullptr;
1306 std::vector<struct fn_field> fnfields;
1307 };
1308
1309 /* The routines that read and process dies for a C struct or C++ class
1310 pass lists of data member fields and lists of member function fields
1311 in an instance of a field_info structure, as defined below. */
1312 struct field_info
1313 {
1314 /* List of data member and baseclasses fields. */
1315 std::vector<struct nextfield> fields;
1316 std::vector<struct nextfield> baseclasses;
1317
1318 /* Number of fields (including baseclasses). */
1319 int nfields = 0;
1320
1321 /* Set if the accessibility of one of the fields is not public. */
1322 int non_public_fields = 0;
1323
1324 /* Member function fieldlist array, contains name of possibly overloaded
1325 member function, number of overloaded member functions and a pointer
1326 to the head of the member function field chain. */
1327 std::vector<struct fnfieldlist> fnfieldlists;
1328
1329 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1330 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1331 std::vector<struct decl_field> typedef_field_list;
1332
1333 /* Nested types defined by this class and the number of elements in this
1334 list. */
1335 std::vector<struct decl_field> nested_types_list;
1336 };
1337
1338 /* One item on the queue of compilation units to read in full symbols
1339 for. */
1340 struct dwarf2_queue_item
1341 {
1342 struct dwarf2_per_cu_data *per_cu;
1343 enum language pretend_language;
1344 struct dwarf2_queue_item *next;
1345 };
1346
1347 /* The current queue. */
1348 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1349
1350 /* Loaded secondary compilation units are kept in memory until they
1351 have not been referenced for the processing of this many
1352 compilation units. Set this to zero to disable caching. Cache
1353 sizes of up to at least twenty will improve startup time for
1354 typical inter-CU-reference binaries, at an obvious memory cost. */
1355 static int dwarf_max_cache_age = 5;
1356 static void
1357 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1358 struct cmd_list_element *c, const char *value)
1359 {
1360 fprintf_filtered (file, _("The upper bound on the age of cached "
1361 "DWARF compilation units is %s.\n"),
1362 value);
1363 }
1364 \f
1365 /* local function prototypes */
1366
1367 static void dwarf2_find_base_address (struct die_info *die,
1368 struct dwarf2_cu *cu);
1369
1370 static dwarf2_psymtab *create_partial_symtab
1371 (struct dwarf2_per_cu_data *per_cu, const char *name);
1372
1373 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1374 const gdb_byte *info_ptr,
1375 struct die_info *type_unit_die);
1376
1377 static void dwarf2_build_psymtabs_hard
1378 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1379
1380 static void scan_partial_symbols (struct partial_die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 int, struct dwarf2_cu *);
1383
1384 static void add_partial_symbol (struct partial_die_info *,
1385 struct dwarf2_cu *);
1386
1387 static void add_partial_namespace (struct partial_die_info *pdi,
1388 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1389 int set_addrmap, struct dwarf2_cu *cu);
1390
1391 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1392 CORE_ADDR *highpc, int set_addrmap,
1393 struct dwarf2_cu *cu);
1394
1395 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1396 struct dwarf2_cu *cu);
1397
1398 static void add_partial_subprogram (struct partial_die_info *pdi,
1399 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1400 int need_pc, struct dwarf2_cu *cu);
1401
1402 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1403
1404 static struct partial_die_info *load_partial_dies
1405 (const struct die_reader_specs *, const gdb_byte *, int);
1406
1407 /* A pair of partial_die_info and compilation unit. */
1408 struct cu_partial_die_info
1409 {
1410 /* The compilation unit of the partial_die_info. */
1411 struct dwarf2_cu *cu;
1412 /* A partial_die_info. */
1413 struct partial_die_info *pdi;
1414
1415 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1416 : cu (cu),
1417 pdi (pdi)
1418 { /* Nothing. */ }
1419
1420 private:
1421 cu_partial_die_info () = delete;
1422 };
1423
1424 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1425 struct dwarf2_cu *);
1426
1427 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1428 struct attribute *, struct attr_abbrev *,
1429 const gdb_byte *, bool *need_reprocess);
1430
1431 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1432 struct attribute *attr);
1433
1434 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1435
1436 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1437 unsigned int *);
1438
1439 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1440
1441 static LONGEST read_checked_initial_length_and_offset
1442 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1443 unsigned int *, unsigned int *);
1444
1445 static LONGEST read_offset (bfd *, const gdb_byte *,
1446 const struct comp_unit_head *,
1447 unsigned int *);
1448
1449 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1450
1451 static sect_offset read_abbrev_offset
1452 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1453 struct dwarf2_section_info *, sect_offset);
1454
1455 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1456
1457 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1458
1459 static const char *read_indirect_string
1460 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1461 const struct comp_unit_head *, unsigned int *);
1462
1463 static const char *read_indirect_line_string
1464 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1465 const struct comp_unit_head *, unsigned int *);
1466
1467 static const char *read_indirect_string_at_offset
1468 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1469 LONGEST str_offset);
1470
1471 static const char *read_indirect_string_from_dwz
1472 (struct objfile *objfile, struct dwz_file *, LONGEST);
1473
1474 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1475 const gdb_byte *,
1476 unsigned int *);
1477
1478 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1479 ULONGEST str_index);
1480
1481 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1482 ULONGEST str_index);
1483
1484 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1485
1486 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1487 struct dwarf2_cu *);
1488
1489 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1490 unsigned int);
1491
1492 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1493 struct dwarf2_cu *cu);
1494
1495 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1496
1497 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1498 struct dwarf2_cu *cu);
1499
1500 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1501
1502 static struct die_info *die_specification (struct die_info *die,
1503 struct dwarf2_cu **);
1504
1505 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1506 struct dwarf2_cu *cu);
1507
1508 static void dwarf_decode_lines (struct line_header *, const char *,
1509 struct dwarf2_cu *, dwarf2_psymtab *,
1510 CORE_ADDR, int decode_mapping);
1511
1512 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1513 const char *);
1514
1515 static struct symbol *new_symbol (struct die_info *, struct type *,
1516 struct dwarf2_cu *, struct symbol * = NULL);
1517
1518 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1519 struct dwarf2_cu *);
1520
1521 static void dwarf2_const_value_attr (const struct attribute *attr,
1522 struct type *type,
1523 const char *name,
1524 struct obstack *obstack,
1525 struct dwarf2_cu *cu, LONGEST *value,
1526 const gdb_byte **bytes,
1527 struct dwarf2_locexpr_baton **baton);
1528
1529 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1530
1531 static int need_gnat_info (struct dwarf2_cu *);
1532
1533 static struct type *die_descriptive_type (struct die_info *,
1534 struct dwarf2_cu *);
1535
1536 static void set_descriptive_type (struct type *, struct die_info *,
1537 struct dwarf2_cu *);
1538
1539 static struct type *die_containing_type (struct die_info *,
1540 struct dwarf2_cu *);
1541
1542 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1543 struct dwarf2_cu *);
1544
1545 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1546
1547 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1548
1549 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1550
1551 static char *typename_concat (struct obstack *obs, const char *prefix,
1552 const char *suffix, int physname,
1553 struct dwarf2_cu *cu);
1554
1555 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1556
1557 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1558
1559 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1560
1561 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1562
1563 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1564
1565 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1566
1567 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1568 struct dwarf2_cu *, dwarf2_psymtab *);
1569
1570 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1571 values. Keep the items ordered with increasing constraints compliance. */
1572 enum pc_bounds_kind
1573 {
1574 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1575 PC_BOUNDS_NOT_PRESENT,
1576
1577 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1578 were present but they do not form a valid range of PC addresses. */
1579 PC_BOUNDS_INVALID,
1580
1581 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1582 PC_BOUNDS_RANGES,
1583
1584 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1585 PC_BOUNDS_HIGH_LOW,
1586 };
1587
1588 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1589 CORE_ADDR *, CORE_ADDR *,
1590 struct dwarf2_cu *,
1591 dwarf2_psymtab *);
1592
1593 static void get_scope_pc_bounds (struct die_info *,
1594 CORE_ADDR *, CORE_ADDR *,
1595 struct dwarf2_cu *);
1596
1597 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1598 CORE_ADDR, struct dwarf2_cu *);
1599
1600 static void dwarf2_add_field (struct field_info *, struct die_info *,
1601 struct dwarf2_cu *);
1602
1603 static void dwarf2_attach_fields_to_type (struct field_info *,
1604 struct type *, struct dwarf2_cu *);
1605
1606 static void dwarf2_add_member_fn (struct field_info *,
1607 struct die_info *, struct type *,
1608 struct dwarf2_cu *);
1609
1610 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1611 struct type *,
1612 struct dwarf2_cu *);
1613
1614 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1615
1616 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1617
1618 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1619
1620 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1621
1622 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1623
1624 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1625
1626 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1627
1628 static struct type *read_module_type (struct die_info *die,
1629 struct dwarf2_cu *cu);
1630
1631 static const char *namespace_name (struct die_info *die,
1632 int *is_anonymous, struct dwarf2_cu *);
1633
1634 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1635
1636 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1637
1638 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1639 struct dwarf2_cu *);
1640
1641 static struct die_info *read_die_and_siblings_1
1642 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1643 struct die_info *);
1644
1645 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1646 const gdb_byte *info_ptr,
1647 const gdb_byte **new_info_ptr,
1648 struct die_info *parent);
1649
1650 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1651 struct die_info **, const gdb_byte *,
1652 int);
1653
1654 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1655 struct die_info **, const gdb_byte *);
1656
1657 static void process_die (struct die_info *, struct dwarf2_cu *);
1658
1659 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1660 struct obstack *);
1661
1662 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1663
1664 static const char *dwarf2_full_name (const char *name,
1665 struct die_info *die,
1666 struct dwarf2_cu *cu);
1667
1668 static const char *dwarf2_physname (const char *name, struct die_info *die,
1669 struct dwarf2_cu *cu);
1670
1671 static struct die_info *dwarf2_extension (struct die_info *die,
1672 struct dwarf2_cu **);
1673
1674 static const char *dwarf_tag_name (unsigned int);
1675
1676 static const char *dwarf_attr_name (unsigned int);
1677
1678 static const char *dwarf_unit_type_name (int unit_type);
1679
1680 static const char *dwarf_form_name (unsigned int);
1681
1682 static const char *dwarf_bool_name (unsigned int);
1683
1684 static const char *dwarf_type_encoding_name (unsigned int);
1685
1686 static struct die_info *sibling_die (struct die_info *);
1687
1688 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1689
1690 static void dump_die_for_error (struct die_info *);
1691
1692 static void dump_die_1 (struct ui_file *, int level, int max_level,
1693 struct die_info *);
1694
1695 /*static*/ void dump_die (struct die_info *, int max_level);
1696
1697 static void store_in_ref_table (struct die_info *,
1698 struct dwarf2_cu *);
1699
1700 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1701
1702 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1703
1704 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1705 const struct attribute *,
1706 struct dwarf2_cu **);
1707
1708 static struct die_info *follow_die_ref (struct die_info *,
1709 const struct attribute *,
1710 struct dwarf2_cu **);
1711
1712 static struct die_info *follow_die_sig (struct die_info *,
1713 const struct attribute *,
1714 struct dwarf2_cu **);
1715
1716 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1717 struct dwarf2_cu *);
1718
1719 static struct type *get_DW_AT_signature_type (struct die_info *,
1720 const struct attribute *,
1721 struct dwarf2_cu *);
1722
1723 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1724
1725 static void read_signatured_type (struct signatured_type *);
1726
1727 static int attr_to_dynamic_prop (const struct attribute *attr,
1728 struct die_info *die, struct dwarf2_cu *cu,
1729 struct dynamic_prop *prop, struct type *type);
1730
1731 /* memory allocation interface */
1732
1733 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1734
1735 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1736
1737 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1738
1739 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1740 struct dwarf2_loclist_baton *baton,
1741 const struct attribute *attr);
1742
1743 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1744 struct symbol *sym,
1745 struct dwarf2_cu *cu,
1746 int is_block);
1747
1748 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1749 const gdb_byte *info_ptr,
1750 struct abbrev_info *abbrev);
1751
1752 static hashval_t partial_die_hash (const void *item);
1753
1754 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1755
1756 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1757 (sect_offset sect_off, unsigned int offset_in_dwz,
1758 struct dwarf2_per_objfile *dwarf2_per_objfile);
1759
1760 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1761 struct die_info *comp_unit_die,
1762 enum language pretend_language);
1763
1764 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1765
1766 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1767
1768 static struct type *set_die_type (struct die_info *, struct type *,
1769 struct dwarf2_cu *);
1770
1771 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1772
1773 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1774
1775 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1776 enum language);
1777
1778 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1779 enum language);
1780
1781 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1782 enum language);
1783
1784 static void dwarf2_add_dependence (struct dwarf2_cu *,
1785 struct dwarf2_per_cu_data *);
1786
1787 static void dwarf2_mark (struct dwarf2_cu *);
1788
1789 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1790
1791 static struct type *get_die_type_at_offset (sect_offset,
1792 struct dwarf2_per_cu_data *);
1793
1794 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1795
1796 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1797 enum language pretend_language);
1798
1799 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1800
1801 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1802 static struct type *dwarf2_per_cu_addr_sized_int_type
1803 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1804 static struct type *dwarf2_per_cu_int_type
1805 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1806 bool unsigned_p);
1807
1808 /* Class, the destructor of which frees all allocated queue entries. This
1809 will only have work to do if an error was thrown while processing the
1810 dwarf. If no error was thrown then the queue entries should have all
1811 been processed, and freed, as we went along. */
1812
1813 class dwarf2_queue_guard
1814 {
1815 public:
1816 dwarf2_queue_guard () = default;
1817
1818 /* Free any entries remaining on the queue. There should only be
1819 entries left if we hit an error while processing the dwarf. */
1820 ~dwarf2_queue_guard ()
1821 {
1822 struct dwarf2_queue_item *item, *last;
1823
1824 item = dwarf2_queue;
1825 while (item)
1826 {
1827 /* Anything still marked queued is likely to be in an
1828 inconsistent state, so discard it. */
1829 if (item->per_cu->queued)
1830 {
1831 if (item->per_cu->cu != NULL)
1832 free_one_cached_comp_unit (item->per_cu);
1833 item->per_cu->queued = 0;
1834 }
1835
1836 last = item;
1837 item = item->next;
1838 xfree (last);
1839 }
1840
1841 dwarf2_queue = dwarf2_queue_tail = NULL;
1842 }
1843 };
1844
1845 /* The return type of find_file_and_directory. Note, the enclosed
1846 string pointers are only valid while this object is valid. */
1847
1848 struct file_and_directory
1849 {
1850 /* The filename. This is never NULL. */
1851 const char *name;
1852
1853 /* The compilation directory. NULL if not known. If we needed to
1854 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1855 points directly to the DW_AT_comp_dir string attribute owned by
1856 the obstack that owns the DIE. */
1857 const char *comp_dir;
1858
1859 /* If we needed to build a new string for comp_dir, this is what
1860 owns the storage. */
1861 std::string comp_dir_storage;
1862 };
1863
1864 static file_and_directory find_file_and_directory (struct die_info *die,
1865 struct dwarf2_cu *cu);
1866
1867 static char *file_full_name (int file, struct line_header *lh,
1868 const char *comp_dir);
1869
1870 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1871 enum class rcuh_kind { COMPILE, TYPE };
1872
1873 static const gdb_byte *read_and_check_comp_unit_head
1874 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1875 struct comp_unit_head *header,
1876 struct dwarf2_section_info *section,
1877 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1878 rcuh_kind section_kind);
1879
1880 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1881
1882 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1883
1884 static struct dwo_unit *lookup_dwo_unit_in_dwp
1885 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1886 struct dwp_file *dwp_file, const char *comp_dir,
1887 ULONGEST signature, int is_debug_types);
1888
1889 static struct dwp_file *get_dwp_file
1890 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1891
1892 static struct dwo_unit *lookup_dwo_comp_unit
1893 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1894
1895 static struct dwo_unit *lookup_dwo_type_unit
1896 (struct signatured_type *, const char *, const char *);
1897
1898 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1899
1900 /* A unique pointer to a dwo_file. */
1901
1902 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1903
1904 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1905
1906 static void check_producer (struct dwarf2_cu *cu);
1907
1908 static void free_line_header_voidp (void *arg);
1909 \f
1910 /* Various complaints about symbol reading that don't abort the process. */
1911
1912 static void
1913 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1914 {
1915 complaint (_("statement list doesn't fit in .debug_line section"));
1916 }
1917
1918 static void
1919 dwarf2_debug_line_missing_file_complaint (void)
1920 {
1921 complaint (_(".debug_line section has line data without a file"));
1922 }
1923
1924 static void
1925 dwarf2_debug_line_missing_end_sequence_complaint (void)
1926 {
1927 complaint (_(".debug_line section has line "
1928 "program sequence without an end"));
1929 }
1930
1931 static void
1932 dwarf2_complex_location_expr_complaint (void)
1933 {
1934 complaint (_("location expression too complex"));
1935 }
1936
1937 static void
1938 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1939 int arg3)
1940 {
1941 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1942 arg1, arg2, arg3);
1943 }
1944
1945 static void
1946 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1947 {
1948 complaint (_("debug info runs off end of %s section"
1949 " [in module %s]"),
1950 section->get_name (),
1951 section->get_file_name ());
1952 }
1953
1954 static void
1955 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1956 {
1957 complaint (_("macro debug info contains a "
1958 "malformed macro definition:\n`%s'"),
1959 arg1);
1960 }
1961
1962 static void
1963 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1964 {
1965 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1966 arg1, arg2);
1967 }
1968
1969 /* Hash function for line_header_hash. */
1970
1971 static hashval_t
1972 line_header_hash (const struct line_header *ofs)
1973 {
1974 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1975 }
1976
1977 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1978
1979 static hashval_t
1980 line_header_hash_voidp (const void *item)
1981 {
1982 const struct line_header *ofs = (const struct line_header *) item;
1983
1984 return line_header_hash (ofs);
1985 }
1986
1987 /* Equality function for line_header_hash. */
1988
1989 static int
1990 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1991 {
1992 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1993 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1994
1995 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1996 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1997 }
1998
1999 \f
2000
2001 /* See declaration. */
2002
2003 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2004 const dwarf2_debug_sections *names,
2005 bool can_copy_)
2006 : objfile (objfile_),
2007 can_copy (can_copy_)
2008 {
2009 if (names == NULL)
2010 names = &dwarf2_elf_names;
2011
2012 bfd *obfd = objfile->obfd;
2013
2014 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2015 locate_sections (obfd, sec, *names);
2016 }
2017
2018 dwarf2_per_objfile::~dwarf2_per_objfile ()
2019 {
2020 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2021 free_cached_comp_units ();
2022
2023 if (quick_file_names_table)
2024 htab_delete (quick_file_names_table);
2025
2026 if (line_header_hash)
2027 htab_delete (line_header_hash);
2028
2029 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2030 per_cu->imported_symtabs_free ();
2031
2032 for (signatured_type *sig_type : all_type_units)
2033 sig_type->per_cu.imported_symtabs_free ();
2034
2035 /* Everything else should be on the objfile obstack. */
2036 }
2037
2038 /* See declaration. */
2039
2040 void
2041 dwarf2_per_objfile::free_cached_comp_units ()
2042 {
2043 dwarf2_per_cu_data *per_cu = read_in_chain;
2044 dwarf2_per_cu_data **last_chain = &read_in_chain;
2045 while (per_cu != NULL)
2046 {
2047 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2048
2049 delete per_cu->cu;
2050 *last_chain = next_cu;
2051 per_cu = next_cu;
2052 }
2053 }
2054
2055 /* A helper class that calls free_cached_comp_units on
2056 destruction. */
2057
2058 class free_cached_comp_units
2059 {
2060 public:
2061
2062 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2063 : m_per_objfile (per_objfile)
2064 {
2065 }
2066
2067 ~free_cached_comp_units ()
2068 {
2069 m_per_objfile->free_cached_comp_units ();
2070 }
2071
2072 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2073
2074 private:
2075
2076 dwarf2_per_objfile *m_per_objfile;
2077 };
2078
2079 /* Try to locate the sections we need for DWARF 2 debugging
2080 information and return true if we have enough to do something.
2081 NAMES points to the dwarf2 section names, or is NULL if the standard
2082 ELF names are used. CAN_COPY is true for formats where symbol
2083 interposition is possible and so symbol values must follow copy
2084 relocation rules. */
2085
2086 int
2087 dwarf2_has_info (struct objfile *objfile,
2088 const struct dwarf2_debug_sections *names,
2089 bool can_copy)
2090 {
2091 if (objfile->flags & OBJF_READNEVER)
2092 return 0;
2093
2094 struct dwarf2_per_objfile *dwarf2_per_objfile
2095 = get_dwarf2_per_objfile (objfile);
2096
2097 if (dwarf2_per_objfile == NULL)
2098 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2099 names,
2100 can_copy);
2101
2102 return (!dwarf2_per_objfile->info.is_virtual
2103 && dwarf2_per_objfile->info.s.section != NULL
2104 && !dwarf2_per_objfile->abbrev.is_virtual
2105 && dwarf2_per_objfile->abbrev.s.section != NULL);
2106 }
2107
2108 /* When loading sections, we look either for uncompressed section or for
2109 compressed section names. */
2110
2111 static int
2112 section_is_p (const char *section_name,
2113 const struct dwarf2_section_names *names)
2114 {
2115 if (names->normal != NULL
2116 && strcmp (section_name, names->normal) == 0)
2117 return 1;
2118 if (names->compressed != NULL
2119 && strcmp (section_name, names->compressed) == 0)
2120 return 1;
2121 return 0;
2122 }
2123
2124 /* See declaration. */
2125
2126 void
2127 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2128 const dwarf2_debug_sections &names)
2129 {
2130 flagword aflag = bfd_section_flags (sectp);
2131
2132 if ((aflag & SEC_HAS_CONTENTS) == 0)
2133 {
2134 }
2135 else if (elf_section_data (sectp)->this_hdr.sh_size
2136 > bfd_get_file_size (abfd))
2137 {
2138 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2139 warning (_("Discarding section %s which has a section size (%s"
2140 ") larger than the file size [in module %s]"),
2141 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2142 bfd_get_filename (abfd));
2143 }
2144 else if (section_is_p (sectp->name, &names.info))
2145 {
2146 this->info.s.section = sectp;
2147 this->info.size = bfd_section_size (sectp);
2148 }
2149 else if (section_is_p (sectp->name, &names.abbrev))
2150 {
2151 this->abbrev.s.section = sectp;
2152 this->abbrev.size = bfd_section_size (sectp);
2153 }
2154 else if (section_is_p (sectp->name, &names.line))
2155 {
2156 this->line.s.section = sectp;
2157 this->line.size = bfd_section_size (sectp);
2158 }
2159 else if (section_is_p (sectp->name, &names.loc))
2160 {
2161 this->loc.s.section = sectp;
2162 this->loc.size = bfd_section_size (sectp);
2163 }
2164 else if (section_is_p (sectp->name, &names.loclists))
2165 {
2166 this->loclists.s.section = sectp;
2167 this->loclists.size = bfd_section_size (sectp);
2168 }
2169 else if (section_is_p (sectp->name, &names.macinfo))
2170 {
2171 this->macinfo.s.section = sectp;
2172 this->macinfo.size = bfd_section_size (sectp);
2173 }
2174 else if (section_is_p (sectp->name, &names.macro))
2175 {
2176 this->macro.s.section = sectp;
2177 this->macro.size = bfd_section_size (sectp);
2178 }
2179 else if (section_is_p (sectp->name, &names.str))
2180 {
2181 this->str.s.section = sectp;
2182 this->str.size = bfd_section_size (sectp);
2183 }
2184 else if (section_is_p (sectp->name, &names.str_offsets))
2185 {
2186 this->str_offsets.s.section = sectp;
2187 this->str_offsets.size = bfd_section_size (sectp);
2188 }
2189 else if (section_is_p (sectp->name, &names.line_str))
2190 {
2191 this->line_str.s.section = sectp;
2192 this->line_str.size = bfd_section_size (sectp);
2193 }
2194 else if (section_is_p (sectp->name, &names.addr))
2195 {
2196 this->addr.s.section = sectp;
2197 this->addr.size = bfd_section_size (sectp);
2198 }
2199 else if (section_is_p (sectp->name, &names.frame))
2200 {
2201 this->frame.s.section = sectp;
2202 this->frame.size = bfd_section_size (sectp);
2203 }
2204 else if (section_is_p (sectp->name, &names.eh_frame))
2205 {
2206 this->eh_frame.s.section = sectp;
2207 this->eh_frame.size = bfd_section_size (sectp);
2208 }
2209 else if (section_is_p (sectp->name, &names.ranges))
2210 {
2211 this->ranges.s.section = sectp;
2212 this->ranges.size = bfd_section_size (sectp);
2213 }
2214 else if (section_is_p (sectp->name, &names.rnglists))
2215 {
2216 this->rnglists.s.section = sectp;
2217 this->rnglists.size = bfd_section_size (sectp);
2218 }
2219 else if (section_is_p (sectp->name, &names.types))
2220 {
2221 struct dwarf2_section_info type_section;
2222
2223 memset (&type_section, 0, sizeof (type_section));
2224 type_section.s.section = sectp;
2225 type_section.size = bfd_section_size (sectp);
2226
2227 this->types.push_back (type_section);
2228 }
2229 else if (section_is_p (sectp->name, &names.gdb_index))
2230 {
2231 this->gdb_index.s.section = sectp;
2232 this->gdb_index.size = bfd_section_size (sectp);
2233 }
2234 else if (section_is_p (sectp->name, &names.debug_names))
2235 {
2236 this->debug_names.s.section = sectp;
2237 this->debug_names.size = bfd_section_size (sectp);
2238 }
2239 else if (section_is_p (sectp->name, &names.debug_aranges))
2240 {
2241 this->debug_aranges.s.section = sectp;
2242 this->debug_aranges.size = bfd_section_size (sectp);
2243 }
2244
2245 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2246 && bfd_section_vma (sectp) == 0)
2247 this->has_section_at_zero = true;
2248 }
2249
2250 /* A helper function that returns the size of a section in a safe way.
2251 If you are positive that the section has been read before using the
2252 size, then it is safe to refer to the dwarf2_section_info object's
2253 "size" field directly. In other cases, you must call this
2254 function, because for compressed sections the size field is not set
2255 correctly until the section has been read. */
2256
2257 static bfd_size_type
2258 dwarf2_section_size (struct objfile *objfile,
2259 struct dwarf2_section_info *info)
2260 {
2261 if (!info->readin)
2262 info->read (objfile);
2263 return info->size;
2264 }
2265
2266 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2267 SECTION_NAME. */
2268
2269 void
2270 dwarf2_get_section_info (struct objfile *objfile,
2271 enum dwarf2_section_enum sect,
2272 asection **sectp, const gdb_byte **bufp,
2273 bfd_size_type *sizep)
2274 {
2275 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2276 struct dwarf2_section_info *info;
2277
2278 /* We may see an objfile without any DWARF, in which case we just
2279 return nothing. */
2280 if (data == NULL)
2281 {
2282 *sectp = NULL;
2283 *bufp = NULL;
2284 *sizep = 0;
2285 return;
2286 }
2287 switch (sect)
2288 {
2289 case DWARF2_DEBUG_FRAME:
2290 info = &data->frame;
2291 break;
2292 case DWARF2_EH_FRAME:
2293 info = &data->eh_frame;
2294 break;
2295 default:
2296 gdb_assert_not_reached ("unexpected section");
2297 }
2298
2299 info->read (objfile);
2300
2301 *sectp = info->get_bfd_section ();
2302 *bufp = info->buffer;
2303 *sizep = info->size;
2304 }
2305
2306 /* A helper function to find the sections for a .dwz file. */
2307
2308 static void
2309 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2310 {
2311 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2312
2313 /* Note that we only support the standard ELF names, because .dwz
2314 is ELF-only (at the time of writing). */
2315 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2316 {
2317 dwz_file->abbrev.s.section = sectp;
2318 dwz_file->abbrev.size = bfd_section_size (sectp);
2319 }
2320 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2321 {
2322 dwz_file->info.s.section = sectp;
2323 dwz_file->info.size = bfd_section_size (sectp);
2324 }
2325 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2326 {
2327 dwz_file->str.s.section = sectp;
2328 dwz_file->str.size = bfd_section_size (sectp);
2329 }
2330 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2331 {
2332 dwz_file->line.s.section = sectp;
2333 dwz_file->line.size = bfd_section_size (sectp);
2334 }
2335 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2336 {
2337 dwz_file->macro.s.section = sectp;
2338 dwz_file->macro.size = bfd_section_size (sectp);
2339 }
2340 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2341 {
2342 dwz_file->gdb_index.s.section = sectp;
2343 dwz_file->gdb_index.size = bfd_section_size (sectp);
2344 }
2345 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2346 {
2347 dwz_file->debug_names.s.section = sectp;
2348 dwz_file->debug_names.size = bfd_section_size (sectp);
2349 }
2350 }
2351
2352 /* See dwarf2read.h. */
2353
2354 struct dwz_file *
2355 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2356 {
2357 const char *filename;
2358 bfd_size_type buildid_len_arg;
2359 size_t buildid_len;
2360 bfd_byte *buildid;
2361
2362 if (dwarf2_per_objfile->dwz_file != NULL)
2363 return dwarf2_per_objfile->dwz_file.get ();
2364
2365 bfd_set_error (bfd_error_no_error);
2366 gdb::unique_xmalloc_ptr<char> data
2367 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2368 &buildid_len_arg, &buildid));
2369 if (data == NULL)
2370 {
2371 if (bfd_get_error () == bfd_error_no_error)
2372 return NULL;
2373 error (_("could not read '.gnu_debugaltlink' section: %s"),
2374 bfd_errmsg (bfd_get_error ()));
2375 }
2376
2377 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2378
2379 buildid_len = (size_t) buildid_len_arg;
2380
2381 filename = data.get ();
2382
2383 std::string abs_storage;
2384 if (!IS_ABSOLUTE_PATH (filename))
2385 {
2386 gdb::unique_xmalloc_ptr<char> abs
2387 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2388
2389 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2390 filename = abs_storage.c_str ();
2391 }
2392
2393 /* First try the file name given in the section. If that doesn't
2394 work, try to use the build-id instead. */
2395 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2396 if (dwz_bfd != NULL)
2397 {
2398 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2399 dwz_bfd.reset (nullptr);
2400 }
2401
2402 if (dwz_bfd == NULL)
2403 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2404
2405 if (dwz_bfd == NULL)
2406 error (_("could not find '.gnu_debugaltlink' file for %s"),
2407 objfile_name (dwarf2_per_objfile->objfile));
2408
2409 std::unique_ptr<struct dwz_file> result
2410 (new struct dwz_file (std::move (dwz_bfd)));
2411
2412 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2413 result.get ());
2414
2415 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2416 result->dwz_bfd.get ());
2417 dwarf2_per_objfile->dwz_file = std::move (result);
2418 return dwarf2_per_objfile->dwz_file.get ();
2419 }
2420 \f
2421 /* DWARF quick_symbols_functions support. */
2422
2423 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2424 unique line tables, so we maintain a separate table of all .debug_line
2425 derived entries to support the sharing.
2426 All the quick functions need is the list of file names. We discard the
2427 line_header when we're done and don't need to record it here. */
2428 struct quick_file_names
2429 {
2430 /* The data used to construct the hash key. */
2431 struct stmt_list_hash hash;
2432
2433 /* The number of entries in file_names, real_names. */
2434 unsigned int num_file_names;
2435
2436 /* The file names from the line table, after being run through
2437 file_full_name. */
2438 const char **file_names;
2439
2440 /* The file names from the line table after being run through
2441 gdb_realpath. These are computed lazily. */
2442 const char **real_names;
2443 };
2444
2445 /* When using the index (and thus not using psymtabs), each CU has an
2446 object of this type. This is used to hold information needed by
2447 the various "quick" methods. */
2448 struct dwarf2_per_cu_quick_data
2449 {
2450 /* The file table. This can be NULL if there was no file table
2451 or it's currently not read in.
2452 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2453 struct quick_file_names *file_names;
2454
2455 /* The corresponding symbol table. This is NULL if symbols for this
2456 CU have not yet been read. */
2457 struct compunit_symtab *compunit_symtab;
2458
2459 /* A temporary mark bit used when iterating over all CUs in
2460 expand_symtabs_matching. */
2461 unsigned int mark : 1;
2462
2463 /* True if we've tried to read the file table and found there isn't one.
2464 There will be no point in trying to read it again next time. */
2465 unsigned int no_file_data : 1;
2466 };
2467
2468 /* Utility hash function for a stmt_list_hash. */
2469
2470 static hashval_t
2471 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2472 {
2473 hashval_t v = 0;
2474
2475 if (stmt_list_hash->dwo_unit != NULL)
2476 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2477 v += to_underlying (stmt_list_hash->line_sect_off);
2478 return v;
2479 }
2480
2481 /* Utility equality function for a stmt_list_hash. */
2482
2483 static int
2484 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2485 const struct stmt_list_hash *rhs)
2486 {
2487 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2488 return 0;
2489 if (lhs->dwo_unit != NULL
2490 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2491 return 0;
2492
2493 return lhs->line_sect_off == rhs->line_sect_off;
2494 }
2495
2496 /* Hash function for a quick_file_names. */
2497
2498 static hashval_t
2499 hash_file_name_entry (const void *e)
2500 {
2501 const struct quick_file_names *file_data
2502 = (const struct quick_file_names *) e;
2503
2504 return hash_stmt_list_entry (&file_data->hash);
2505 }
2506
2507 /* Equality function for a quick_file_names. */
2508
2509 static int
2510 eq_file_name_entry (const void *a, const void *b)
2511 {
2512 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2513 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2514
2515 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2516 }
2517
2518 /* Delete function for a quick_file_names. */
2519
2520 static void
2521 delete_file_name_entry (void *e)
2522 {
2523 struct quick_file_names *file_data = (struct quick_file_names *) e;
2524 int i;
2525
2526 for (i = 0; i < file_data->num_file_names; ++i)
2527 {
2528 xfree ((void*) file_data->file_names[i]);
2529 if (file_data->real_names)
2530 xfree ((void*) file_data->real_names[i]);
2531 }
2532
2533 /* The space for the struct itself lives on objfile_obstack,
2534 so we don't free it here. */
2535 }
2536
2537 /* Create a quick_file_names hash table. */
2538
2539 static htab_t
2540 create_quick_file_names_table (unsigned int nr_initial_entries)
2541 {
2542 return htab_create_alloc (nr_initial_entries,
2543 hash_file_name_entry, eq_file_name_entry,
2544 delete_file_name_entry, xcalloc, xfree);
2545 }
2546
2547 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2548 have to be created afterwards. You should call age_cached_comp_units after
2549 processing PER_CU->CU. dw2_setup must have been already called. */
2550
2551 static void
2552 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2553 {
2554 if (per_cu->is_debug_types)
2555 load_full_type_unit (per_cu);
2556 else
2557 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2558
2559 if (per_cu->cu == NULL)
2560 return; /* Dummy CU. */
2561
2562 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2563 }
2564
2565 /* Read in the symbols for PER_CU. */
2566
2567 static void
2568 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2569 {
2570 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2571
2572 /* Skip type_unit_groups, reading the type units they contain
2573 is handled elsewhere. */
2574 if (IS_TYPE_UNIT_GROUP (per_cu))
2575 return;
2576
2577 /* The destructor of dwarf2_queue_guard frees any entries left on
2578 the queue. After this point we're guaranteed to leave this function
2579 with the dwarf queue empty. */
2580 dwarf2_queue_guard q_guard;
2581
2582 if (dwarf2_per_objfile->using_index
2583 ? per_cu->v.quick->compunit_symtab == NULL
2584 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2585 {
2586 queue_comp_unit (per_cu, language_minimal);
2587 load_cu (per_cu, skip_partial);
2588
2589 /* If we just loaded a CU from a DWO, and we're working with an index
2590 that may badly handle TUs, load all the TUs in that DWO as well.
2591 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2592 if (!per_cu->is_debug_types
2593 && per_cu->cu != NULL
2594 && per_cu->cu->dwo_unit != NULL
2595 && dwarf2_per_objfile->index_table != NULL
2596 && dwarf2_per_objfile->index_table->version <= 7
2597 /* DWP files aren't supported yet. */
2598 && get_dwp_file (dwarf2_per_objfile) == NULL)
2599 queue_and_load_all_dwo_tus (per_cu);
2600 }
2601
2602 process_queue (dwarf2_per_objfile);
2603
2604 /* Age the cache, releasing compilation units that have not
2605 been used recently. */
2606 age_cached_comp_units (dwarf2_per_objfile);
2607 }
2608
2609 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2610 the objfile from which this CU came. Returns the resulting symbol
2611 table. */
2612
2613 static struct compunit_symtab *
2614 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2615 {
2616 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2617
2618 gdb_assert (dwarf2_per_objfile->using_index);
2619 if (!per_cu->v.quick->compunit_symtab)
2620 {
2621 free_cached_comp_units freer (dwarf2_per_objfile);
2622 scoped_restore decrementer = increment_reading_symtab ();
2623 dw2_do_instantiate_symtab (per_cu, skip_partial);
2624 process_cu_includes (dwarf2_per_objfile);
2625 }
2626
2627 return per_cu->v.quick->compunit_symtab;
2628 }
2629
2630 /* See declaration. */
2631
2632 dwarf2_per_cu_data *
2633 dwarf2_per_objfile::get_cutu (int index)
2634 {
2635 if (index >= this->all_comp_units.size ())
2636 {
2637 index -= this->all_comp_units.size ();
2638 gdb_assert (index < this->all_type_units.size ());
2639 return &this->all_type_units[index]->per_cu;
2640 }
2641
2642 return this->all_comp_units[index];
2643 }
2644
2645 /* See declaration. */
2646
2647 dwarf2_per_cu_data *
2648 dwarf2_per_objfile::get_cu (int index)
2649 {
2650 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2651
2652 return this->all_comp_units[index];
2653 }
2654
2655 /* See declaration. */
2656
2657 signatured_type *
2658 dwarf2_per_objfile::get_tu (int index)
2659 {
2660 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2661
2662 return this->all_type_units[index];
2663 }
2664
2665 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2666 objfile_obstack, and constructed with the specified field
2667 values. */
2668
2669 static dwarf2_per_cu_data *
2670 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2671 struct dwarf2_section_info *section,
2672 int is_dwz,
2673 sect_offset sect_off, ULONGEST length)
2674 {
2675 struct objfile *objfile = dwarf2_per_objfile->objfile;
2676 dwarf2_per_cu_data *the_cu
2677 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2678 struct dwarf2_per_cu_data);
2679 the_cu->sect_off = sect_off;
2680 the_cu->length = length;
2681 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2682 the_cu->section = section;
2683 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2684 struct dwarf2_per_cu_quick_data);
2685 the_cu->is_dwz = is_dwz;
2686 return the_cu;
2687 }
2688
2689 /* A helper for create_cus_from_index that handles a given list of
2690 CUs. */
2691
2692 static void
2693 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2694 const gdb_byte *cu_list, offset_type n_elements,
2695 struct dwarf2_section_info *section,
2696 int is_dwz)
2697 {
2698 for (offset_type i = 0; i < n_elements; i += 2)
2699 {
2700 gdb_static_assert (sizeof (ULONGEST) >= 8);
2701
2702 sect_offset sect_off
2703 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2704 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2705 cu_list += 2 * 8;
2706
2707 dwarf2_per_cu_data *per_cu
2708 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2709 sect_off, length);
2710 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2711 }
2712 }
2713
2714 /* Read the CU list from the mapped index, and use it to create all
2715 the CU objects for this objfile. */
2716
2717 static void
2718 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2719 const gdb_byte *cu_list, offset_type cu_list_elements,
2720 const gdb_byte *dwz_list, offset_type dwz_elements)
2721 {
2722 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2723 dwarf2_per_objfile->all_comp_units.reserve
2724 ((cu_list_elements + dwz_elements) / 2);
2725
2726 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2727 &dwarf2_per_objfile->info, 0);
2728
2729 if (dwz_elements == 0)
2730 return;
2731
2732 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2733 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2734 &dwz->info, 1);
2735 }
2736
2737 /* Create the signatured type hash table from the index. */
2738
2739 static void
2740 create_signatured_type_table_from_index
2741 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2742 struct dwarf2_section_info *section,
2743 const gdb_byte *bytes,
2744 offset_type elements)
2745 {
2746 struct objfile *objfile = dwarf2_per_objfile->objfile;
2747
2748 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2749 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2750
2751 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2752
2753 for (offset_type i = 0; i < elements; i += 3)
2754 {
2755 struct signatured_type *sig_type;
2756 ULONGEST signature;
2757 void **slot;
2758 cu_offset type_offset_in_tu;
2759
2760 gdb_static_assert (sizeof (ULONGEST) >= 8);
2761 sect_offset sect_off
2762 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2763 type_offset_in_tu
2764 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2765 BFD_ENDIAN_LITTLE);
2766 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2767 bytes += 3 * 8;
2768
2769 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2770 struct signatured_type);
2771 sig_type->signature = signature;
2772 sig_type->type_offset_in_tu = type_offset_in_tu;
2773 sig_type->per_cu.is_debug_types = 1;
2774 sig_type->per_cu.section = section;
2775 sig_type->per_cu.sect_off = sect_off;
2776 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2777 sig_type->per_cu.v.quick
2778 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2779 struct dwarf2_per_cu_quick_data);
2780
2781 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2782 *slot = sig_type;
2783
2784 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2785 }
2786
2787 dwarf2_per_objfile->signatured_types = sig_types_hash;
2788 }
2789
2790 /* Create the signatured type hash table from .debug_names. */
2791
2792 static void
2793 create_signatured_type_table_from_debug_names
2794 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2795 const mapped_debug_names &map,
2796 struct dwarf2_section_info *section,
2797 struct dwarf2_section_info *abbrev_section)
2798 {
2799 struct objfile *objfile = dwarf2_per_objfile->objfile;
2800
2801 section->read (objfile);
2802 abbrev_section->read (objfile);
2803
2804 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2805 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2806
2807 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2808
2809 for (uint32_t i = 0; i < map.tu_count; ++i)
2810 {
2811 struct signatured_type *sig_type;
2812 void **slot;
2813
2814 sect_offset sect_off
2815 = (sect_offset) (extract_unsigned_integer
2816 (map.tu_table_reordered + i * map.offset_size,
2817 map.offset_size,
2818 map.dwarf5_byte_order));
2819
2820 comp_unit_head cu_header;
2821 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2822 abbrev_section,
2823 section->buffer + to_underlying (sect_off),
2824 rcuh_kind::TYPE);
2825
2826 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2827 struct signatured_type);
2828 sig_type->signature = cu_header.signature;
2829 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2830 sig_type->per_cu.is_debug_types = 1;
2831 sig_type->per_cu.section = section;
2832 sig_type->per_cu.sect_off = sect_off;
2833 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2834 sig_type->per_cu.v.quick
2835 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2836 struct dwarf2_per_cu_quick_data);
2837
2838 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2839 *slot = sig_type;
2840
2841 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2842 }
2843
2844 dwarf2_per_objfile->signatured_types = sig_types_hash;
2845 }
2846
2847 /* Read the address map data from the mapped index, and use it to
2848 populate the objfile's psymtabs_addrmap. */
2849
2850 static void
2851 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2852 struct mapped_index *index)
2853 {
2854 struct objfile *objfile = dwarf2_per_objfile->objfile;
2855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2856 const gdb_byte *iter, *end;
2857 struct addrmap *mutable_map;
2858 CORE_ADDR baseaddr;
2859
2860 auto_obstack temp_obstack;
2861
2862 mutable_map = addrmap_create_mutable (&temp_obstack);
2863
2864 iter = index->address_table.data ();
2865 end = iter + index->address_table.size ();
2866
2867 baseaddr = objfile->text_section_offset ();
2868
2869 while (iter < end)
2870 {
2871 ULONGEST hi, lo, cu_index;
2872 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2873 iter += 8;
2874 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2875 iter += 8;
2876 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2877 iter += 4;
2878
2879 if (lo > hi)
2880 {
2881 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2882 hex_string (lo), hex_string (hi));
2883 continue;
2884 }
2885
2886 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2887 {
2888 complaint (_(".gdb_index address table has invalid CU number %u"),
2889 (unsigned) cu_index);
2890 continue;
2891 }
2892
2893 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2894 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2895 addrmap_set_empty (mutable_map, lo, hi - 1,
2896 dwarf2_per_objfile->get_cu (cu_index));
2897 }
2898
2899 objfile->partial_symtabs->psymtabs_addrmap
2900 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2901 }
2902
2903 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2904 populate the objfile's psymtabs_addrmap. */
2905
2906 static void
2907 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2908 struct dwarf2_section_info *section)
2909 {
2910 struct objfile *objfile = dwarf2_per_objfile->objfile;
2911 bfd *abfd = objfile->obfd;
2912 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2913 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2914
2915 auto_obstack temp_obstack;
2916 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2917
2918 std::unordered_map<sect_offset,
2919 dwarf2_per_cu_data *,
2920 gdb::hash_enum<sect_offset>>
2921 debug_info_offset_to_per_cu;
2922 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2923 {
2924 const auto insertpair
2925 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2926 if (!insertpair.second)
2927 {
2928 warning (_("Section .debug_aranges in %s has duplicate "
2929 "debug_info_offset %s, ignoring .debug_aranges."),
2930 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2931 return;
2932 }
2933 }
2934
2935 section->read (objfile);
2936
2937 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2938
2939 const gdb_byte *addr = section->buffer;
2940
2941 while (addr < section->buffer + section->size)
2942 {
2943 const gdb_byte *const entry_addr = addr;
2944 unsigned int bytes_read;
2945
2946 const LONGEST entry_length = read_initial_length (abfd, addr,
2947 &bytes_read);
2948 addr += bytes_read;
2949
2950 const gdb_byte *const entry_end = addr + entry_length;
2951 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2952 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2953 if (addr + entry_length > section->buffer + section->size)
2954 {
2955 warning (_("Section .debug_aranges in %s entry at offset %s "
2956 "length %s exceeds section length %s, "
2957 "ignoring .debug_aranges."),
2958 objfile_name (objfile),
2959 plongest (entry_addr - section->buffer),
2960 plongest (bytes_read + entry_length),
2961 pulongest (section->size));
2962 return;
2963 }
2964
2965 /* The version number. */
2966 const uint16_t version = read_2_bytes (abfd, addr);
2967 addr += 2;
2968 if (version != 2)
2969 {
2970 warning (_("Section .debug_aranges in %s entry at offset %s "
2971 "has unsupported version %d, ignoring .debug_aranges."),
2972 objfile_name (objfile),
2973 plongest (entry_addr - section->buffer), version);
2974 return;
2975 }
2976
2977 const uint64_t debug_info_offset
2978 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2979 addr += offset_size;
2980 const auto per_cu_it
2981 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2982 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2983 {
2984 warning (_("Section .debug_aranges in %s entry at offset %s "
2985 "debug_info_offset %s does not exists, "
2986 "ignoring .debug_aranges."),
2987 objfile_name (objfile),
2988 plongest (entry_addr - section->buffer),
2989 pulongest (debug_info_offset));
2990 return;
2991 }
2992 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2993
2994 const uint8_t address_size = *addr++;
2995 if (address_size < 1 || address_size > 8)
2996 {
2997 warning (_("Section .debug_aranges in %s entry at offset %s "
2998 "address_size %u is invalid, ignoring .debug_aranges."),
2999 objfile_name (objfile),
3000 plongest (entry_addr - section->buffer), address_size);
3001 return;
3002 }
3003
3004 const uint8_t segment_selector_size = *addr++;
3005 if (segment_selector_size != 0)
3006 {
3007 warning (_("Section .debug_aranges in %s entry at offset %s "
3008 "segment_selector_size %u is not supported, "
3009 "ignoring .debug_aranges."),
3010 objfile_name (objfile),
3011 plongest (entry_addr - section->buffer),
3012 segment_selector_size);
3013 return;
3014 }
3015
3016 /* Must pad to an alignment boundary that is twice the address
3017 size. It is undocumented by the DWARF standard but GCC does
3018 use it. */
3019 for (size_t padding = ((-(addr - section->buffer))
3020 & (2 * address_size - 1));
3021 padding > 0; padding--)
3022 if (*addr++ != 0)
3023 {
3024 warning (_("Section .debug_aranges in %s entry at offset %s "
3025 "padding is not zero, ignoring .debug_aranges."),
3026 objfile_name (objfile),
3027 plongest (entry_addr - section->buffer));
3028 return;
3029 }
3030
3031 for (;;)
3032 {
3033 if (addr + 2 * address_size > entry_end)
3034 {
3035 warning (_("Section .debug_aranges in %s entry at offset %s "
3036 "address list is not properly terminated, "
3037 "ignoring .debug_aranges."),
3038 objfile_name (objfile),
3039 plongest (entry_addr - section->buffer));
3040 return;
3041 }
3042 ULONGEST start = extract_unsigned_integer (addr, address_size,
3043 dwarf5_byte_order);
3044 addr += address_size;
3045 ULONGEST length = extract_unsigned_integer (addr, address_size,
3046 dwarf5_byte_order);
3047 addr += address_size;
3048 if (start == 0 && length == 0)
3049 break;
3050 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3051 {
3052 /* Symbol was eliminated due to a COMDAT group. */
3053 continue;
3054 }
3055 ULONGEST end = start + length;
3056 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3057 - baseaddr);
3058 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3059 - baseaddr);
3060 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3061 }
3062 }
3063
3064 objfile->partial_symtabs->psymtabs_addrmap
3065 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3066 }
3067
3068 /* Find a slot in the mapped index INDEX for the object named NAME.
3069 If NAME is found, set *VEC_OUT to point to the CU vector in the
3070 constant pool and return true. If NAME cannot be found, return
3071 false. */
3072
3073 static bool
3074 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3075 offset_type **vec_out)
3076 {
3077 offset_type hash;
3078 offset_type slot, step;
3079 int (*cmp) (const char *, const char *);
3080
3081 gdb::unique_xmalloc_ptr<char> without_params;
3082 if (current_language->la_language == language_cplus
3083 || current_language->la_language == language_fortran
3084 || current_language->la_language == language_d)
3085 {
3086 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3087 not contain any. */
3088
3089 if (strchr (name, '(') != NULL)
3090 {
3091 without_params = cp_remove_params (name);
3092
3093 if (without_params != NULL)
3094 name = without_params.get ();
3095 }
3096 }
3097
3098 /* Index version 4 did not support case insensitive searches. But the
3099 indices for case insensitive languages are built in lowercase, therefore
3100 simulate our NAME being searched is also lowercased. */
3101 hash = mapped_index_string_hash ((index->version == 4
3102 && case_sensitivity == case_sensitive_off
3103 ? 5 : index->version),
3104 name);
3105
3106 slot = hash & (index->symbol_table.size () - 1);
3107 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3108 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3109
3110 for (;;)
3111 {
3112 const char *str;
3113
3114 const auto &bucket = index->symbol_table[slot];
3115 if (bucket.name == 0 && bucket.vec == 0)
3116 return false;
3117
3118 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3119 if (!cmp (name, str))
3120 {
3121 *vec_out = (offset_type *) (index->constant_pool
3122 + MAYBE_SWAP (bucket.vec));
3123 return true;
3124 }
3125
3126 slot = (slot + step) & (index->symbol_table.size () - 1);
3127 }
3128 }
3129
3130 /* A helper function that reads the .gdb_index from BUFFER and fills
3131 in MAP. FILENAME is the name of the file containing the data;
3132 it is used for error reporting. DEPRECATED_OK is true if it is
3133 ok to use deprecated sections.
3134
3135 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3136 out parameters that are filled in with information about the CU and
3137 TU lists in the section.
3138
3139 Returns true if all went well, false otherwise. */
3140
3141 static bool
3142 read_gdb_index_from_buffer (struct objfile *objfile,
3143 const char *filename,
3144 bool deprecated_ok,
3145 gdb::array_view<const gdb_byte> buffer,
3146 struct mapped_index *map,
3147 const gdb_byte **cu_list,
3148 offset_type *cu_list_elements,
3149 const gdb_byte **types_list,
3150 offset_type *types_list_elements)
3151 {
3152 const gdb_byte *addr = &buffer[0];
3153
3154 /* Version check. */
3155 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3156 /* Versions earlier than 3 emitted every copy of a psymbol. This
3157 causes the index to behave very poorly for certain requests. Version 3
3158 contained incomplete addrmap. So, it seems better to just ignore such
3159 indices. */
3160 if (version < 4)
3161 {
3162 static int warning_printed = 0;
3163 if (!warning_printed)
3164 {
3165 warning (_("Skipping obsolete .gdb_index section in %s."),
3166 filename);
3167 warning_printed = 1;
3168 }
3169 return 0;
3170 }
3171 /* Index version 4 uses a different hash function than index version
3172 5 and later.
3173
3174 Versions earlier than 6 did not emit psymbols for inlined
3175 functions. Using these files will cause GDB not to be able to
3176 set breakpoints on inlined functions by name, so we ignore these
3177 indices unless the user has done
3178 "set use-deprecated-index-sections on". */
3179 if (version < 6 && !deprecated_ok)
3180 {
3181 static int warning_printed = 0;
3182 if (!warning_printed)
3183 {
3184 warning (_("\
3185 Skipping deprecated .gdb_index section in %s.\n\
3186 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3187 to use the section anyway."),
3188 filename);
3189 warning_printed = 1;
3190 }
3191 return 0;
3192 }
3193 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3194 of the TU (for symbols coming from TUs),
3195 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3196 Plus gold-generated indices can have duplicate entries for global symbols,
3197 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3198 These are just performance bugs, and we can't distinguish gdb-generated
3199 indices from gold-generated ones, so issue no warning here. */
3200
3201 /* Indexes with higher version than the one supported by GDB may be no
3202 longer backward compatible. */
3203 if (version > 8)
3204 return 0;
3205
3206 map->version = version;
3207
3208 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3209
3210 int i = 0;
3211 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3212 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3213 / 8);
3214 ++i;
3215
3216 *types_list = addr + MAYBE_SWAP (metadata[i]);
3217 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3218 - MAYBE_SWAP (metadata[i]))
3219 / 8);
3220 ++i;
3221
3222 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3223 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3224 map->address_table
3225 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3226 ++i;
3227
3228 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3229 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3230 map->symbol_table
3231 = gdb::array_view<mapped_index::symbol_table_slot>
3232 ((mapped_index::symbol_table_slot *) symbol_table,
3233 (mapped_index::symbol_table_slot *) symbol_table_end);
3234
3235 ++i;
3236 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3237
3238 return 1;
3239 }
3240
3241 /* Callback types for dwarf2_read_gdb_index. */
3242
3243 typedef gdb::function_view
3244 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3245 get_gdb_index_contents_ftype;
3246 typedef gdb::function_view
3247 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3248 get_gdb_index_contents_dwz_ftype;
3249
3250 /* Read .gdb_index. If everything went ok, initialize the "quick"
3251 elements of all the CUs and return 1. Otherwise, return 0. */
3252
3253 static int
3254 dwarf2_read_gdb_index
3255 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3256 get_gdb_index_contents_ftype get_gdb_index_contents,
3257 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3258 {
3259 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3260 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3261 struct dwz_file *dwz;
3262 struct objfile *objfile = dwarf2_per_objfile->objfile;
3263
3264 gdb::array_view<const gdb_byte> main_index_contents
3265 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3266
3267 if (main_index_contents.empty ())
3268 return 0;
3269
3270 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3271 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3272 use_deprecated_index_sections,
3273 main_index_contents, map.get (), &cu_list,
3274 &cu_list_elements, &types_list,
3275 &types_list_elements))
3276 return 0;
3277
3278 /* Don't use the index if it's empty. */
3279 if (map->symbol_table.empty ())
3280 return 0;
3281
3282 /* If there is a .dwz file, read it so we can get its CU list as
3283 well. */
3284 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3285 if (dwz != NULL)
3286 {
3287 struct mapped_index dwz_map;
3288 const gdb_byte *dwz_types_ignore;
3289 offset_type dwz_types_elements_ignore;
3290
3291 gdb::array_view<const gdb_byte> dwz_index_content
3292 = get_gdb_index_contents_dwz (objfile, dwz);
3293
3294 if (dwz_index_content.empty ())
3295 return 0;
3296
3297 if (!read_gdb_index_from_buffer (objfile,
3298 bfd_get_filename (dwz->dwz_bfd.get ()),
3299 1, dwz_index_content, &dwz_map,
3300 &dwz_list, &dwz_list_elements,
3301 &dwz_types_ignore,
3302 &dwz_types_elements_ignore))
3303 {
3304 warning (_("could not read '.gdb_index' section from %s; skipping"),
3305 bfd_get_filename (dwz->dwz_bfd.get ()));
3306 return 0;
3307 }
3308 }
3309
3310 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3311 dwz_list, dwz_list_elements);
3312
3313 if (types_list_elements)
3314 {
3315 /* We can only handle a single .debug_types when we have an
3316 index. */
3317 if (dwarf2_per_objfile->types.size () != 1)
3318 return 0;
3319
3320 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3321
3322 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3323 types_list, types_list_elements);
3324 }
3325
3326 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3327
3328 dwarf2_per_objfile->index_table = std::move (map);
3329 dwarf2_per_objfile->using_index = 1;
3330 dwarf2_per_objfile->quick_file_names_table =
3331 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3332
3333 return 1;
3334 }
3335
3336 /* die_reader_func for dw2_get_file_names. */
3337
3338 static void
3339 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3340 const gdb_byte *info_ptr,
3341 struct die_info *comp_unit_die)
3342 {
3343 struct dwarf2_cu *cu = reader->cu;
3344 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3345 struct dwarf2_per_objfile *dwarf2_per_objfile
3346 = cu->per_cu->dwarf2_per_objfile;
3347 struct objfile *objfile = dwarf2_per_objfile->objfile;
3348 struct dwarf2_per_cu_data *lh_cu;
3349 struct attribute *attr;
3350 void **slot;
3351 struct quick_file_names *qfn;
3352
3353 gdb_assert (! this_cu->is_debug_types);
3354
3355 /* Our callers never want to match partial units -- instead they
3356 will match the enclosing full CU. */
3357 if (comp_unit_die->tag == DW_TAG_partial_unit)
3358 {
3359 this_cu->v.quick->no_file_data = 1;
3360 return;
3361 }
3362
3363 lh_cu = this_cu;
3364 slot = NULL;
3365
3366 line_header_up lh;
3367 sect_offset line_offset {};
3368
3369 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3370 if (attr != nullptr)
3371 {
3372 struct quick_file_names find_entry;
3373
3374 line_offset = (sect_offset) DW_UNSND (attr);
3375
3376 /* We may have already read in this line header (TU line header sharing).
3377 If we have we're done. */
3378 find_entry.hash.dwo_unit = cu->dwo_unit;
3379 find_entry.hash.line_sect_off = line_offset;
3380 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3381 &find_entry, INSERT);
3382 if (*slot != NULL)
3383 {
3384 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3385 return;
3386 }
3387
3388 lh = dwarf_decode_line_header (line_offset, cu);
3389 }
3390 if (lh == NULL)
3391 {
3392 lh_cu->v.quick->no_file_data = 1;
3393 return;
3394 }
3395
3396 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3397 qfn->hash.dwo_unit = cu->dwo_unit;
3398 qfn->hash.line_sect_off = line_offset;
3399 gdb_assert (slot != NULL);
3400 *slot = qfn;
3401
3402 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3403
3404 int offset = 0;
3405 if (strcmp (fnd.name, "<unknown>") != 0)
3406 ++offset;
3407
3408 qfn->num_file_names = offset + lh->file_names_size ();
3409 qfn->file_names =
3410 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3411 if (offset != 0)
3412 qfn->file_names[0] = xstrdup (fnd.name);
3413 for (int i = 0; i < lh->file_names_size (); ++i)
3414 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3415 qfn->real_names = NULL;
3416
3417 lh_cu->v.quick->file_names = qfn;
3418 }
3419
3420 /* A helper for the "quick" functions which attempts to read the line
3421 table for THIS_CU. */
3422
3423 static struct quick_file_names *
3424 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3425 {
3426 /* This should never be called for TUs. */
3427 gdb_assert (! this_cu->is_debug_types);
3428 /* Nor type unit groups. */
3429 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3430
3431 if (this_cu->v.quick->file_names != NULL)
3432 return this_cu->v.quick->file_names;
3433 /* If we know there is no line data, no point in looking again. */
3434 if (this_cu->v.quick->no_file_data)
3435 return NULL;
3436
3437 cutu_reader reader (this_cu);
3438 if (!reader.dummy_p)
3439 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3440
3441 if (this_cu->v.quick->no_file_data)
3442 return NULL;
3443 return this_cu->v.quick->file_names;
3444 }
3445
3446 /* A helper for the "quick" functions which computes and caches the
3447 real path for a given file name from the line table. */
3448
3449 static const char *
3450 dw2_get_real_path (struct objfile *objfile,
3451 struct quick_file_names *qfn, int index)
3452 {
3453 if (qfn->real_names == NULL)
3454 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3455 qfn->num_file_names, const char *);
3456
3457 if (qfn->real_names[index] == NULL)
3458 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3459
3460 return qfn->real_names[index];
3461 }
3462
3463 static struct symtab *
3464 dw2_find_last_source_symtab (struct objfile *objfile)
3465 {
3466 struct dwarf2_per_objfile *dwarf2_per_objfile
3467 = get_dwarf2_per_objfile (objfile);
3468 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3469 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3470
3471 if (cust == NULL)
3472 return NULL;
3473
3474 return compunit_primary_filetab (cust);
3475 }
3476
3477 /* Traversal function for dw2_forget_cached_source_info. */
3478
3479 static int
3480 dw2_free_cached_file_names (void **slot, void *info)
3481 {
3482 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3483
3484 if (file_data->real_names)
3485 {
3486 int i;
3487
3488 for (i = 0; i < file_data->num_file_names; ++i)
3489 {
3490 xfree ((void*) file_data->real_names[i]);
3491 file_data->real_names[i] = NULL;
3492 }
3493 }
3494
3495 return 1;
3496 }
3497
3498 static void
3499 dw2_forget_cached_source_info (struct objfile *objfile)
3500 {
3501 struct dwarf2_per_objfile *dwarf2_per_objfile
3502 = get_dwarf2_per_objfile (objfile);
3503
3504 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3505 dw2_free_cached_file_names, NULL);
3506 }
3507
3508 /* Helper function for dw2_map_symtabs_matching_filename that expands
3509 the symtabs and calls the iterator. */
3510
3511 static int
3512 dw2_map_expand_apply (struct objfile *objfile,
3513 struct dwarf2_per_cu_data *per_cu,
3514 const char *name, const char *real_path,
3515 gdb::function_view<bool (symtab *)> callback)
3516 {
3517 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3518
3519 /* Don't visit already-expanded CUs. */
3520 if (per_cu->v.quick->compunit_symtab)
3521 return 0;
3522
3523 /* This may expand more than one symtab, and we want to iterate over
3524 all of them. */
3525 dw2_instantiate_symtab (per_cu, false);
3526
3527 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3528 last_made, callback);
3529 }
3530
3531 /* Implementation of the map_symtabs_matching_filename method. */
3532
3533 static bool
3534 dw2_map_symtabs_matching_filename
3535 (struct objfile *objfile, const char *name, const char *real_path,
3536 gdb::function_view<bool (symtab *)> callback)
3537 {
3538 const char *name_basename = lbasename (name);
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
3541
3542 /* The rule is CUs specify all the files, including those used by
3543 any TU, so there's no need to scan TUs here. */
3544
3545 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3546 {
3547 /* We only need to look at symtabs not already expanded. */
3548 if (per_cu->v.quick->compunit_symtab)
3549 continue;
3550
3551 quick_file_names *file_data = dw2_get_file_names (per_cu);
3552 if (file_data == NULL)
3553 continue;
3554
3555 for (int j = 0; j < file_data->num_file_names; ++j)
3556 {
3557 const char *this_name = file_data->file_names[j];
3558 const char *this_real_name;
3559
3560 if (compare_filenames_for_search (this_name, name))
3561 {
3562 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3563 callback))
3564 return true;
3565 continue;
3566 }
3567
3568 /* Before we invoke realpath, which can get expensive when many
3569 files are involved, do a quick comparison of the basenames. */
3570 if (! basenames_may_differ
3571 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3572 continue;
3573
3574 this_real_name = dw2_get_real_path (objfile, file_data, j);
3575 if (compare_filenames_for_search (this_real_name, name))
3576 {
3577 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3578 callback))
3579 return true;
3580 continue;
3581 }
3582
3583 if (real_path != NULL)
3584 {
3585 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3586 gdb_assert (IS_ABSOLUTE_PATH (name));
3587 if (this_real_name != NULL
3588 && FILENAME_CMP (real_path, this_real_name) == 0)
3589 {
3590 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3591 callback))
3592 return true;
3593 continue;
3594 }
3595 }
3596 }
3597 }
3598
3599 return false;
3600 }
3601
3602 /* Struct used to manage iterating over all CUs looking for a symbol. */
3603
3604 struct dw2_symtab_iterator
3605 {
3606 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3607 struct dwarf2_per_objfile *dwarf2_per_objfile;
3608 /* If set, only look for symbols that match that block. Valid values are
3609 GLOBAL_BLOCK and STATIC_BLOCK. */
3610 gdb::optional<block_enum> block_index;
3611 /* The kind of symbol we're looking for. */
3612 domain_enum domain;
3613 /* The list of CUs from the index entry of the symbol,
3614 or NULL if not found. */
3615 offset_type *vec;
3616 /* The next element in VEC to look at. */
3617 int next;
3618 /* The number of elements in VEC, or zero if there is no match. */
3619 int length;
3620 /* Have we seen a global version of the symbol?
3621 If so we can ignore all further global instances.
3622 This is to work around gold/15646, inefficient gold-generated
3623 indices. */
3624 int global_seen;
3625 };
3626
3627 /* Initialize the index symtab iterator ITER. */
3628
3629 static void
3630 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3631 struct dwarf2_per_objfile *dwarf2_per_objfile,
3632 gdb::optional<block_enum> block_index,
3633 domain_enum domain,
3634 const char *name)
3635 {
3636 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3637 iter->block_index = block_index;
3638 iter->domain = domain;
3639 iter->next = 0;
3640 iter->global_seen = 0;
3641
3642 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3643
3644 /* index is NULL if OBJF_READNOW. */
3645 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3646 iter->length = MAYBE_SWAP (*iter->vec);
3647 else
3648 {
3649 iter->vec = NULL;
3650 iter->length = 0;
3651 }
3652 }
3653
3654 /* Return the next matching CU or NULL if there are no more. */
3655
3656 static struct dwarf2_per_cu_data *
3657 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3658 {
3659 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3660
3661 for ( ; iter->next < iter->length; ++iter->next)
3662 {
3663 offset_type cu_index_and_attrs =
3664 MAYBE_SWAP (iter->vec[iter->next + 1]);
3665 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3666 gdb_index_symbol_kind symbol_kind =
3667 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3668 /* Only check the symbol attributes if they're present.
3669 Indices prior to version 7 don't record them,
3670 and indices >= 7 may elide them for certain symbols
3671 (gold does this). */
3672 int attrs_valid =
3673 (dwarf2_per_objfile->index_table->version >= 7
3674 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3675
3676 /* Don't crash on bad data. */
3677 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3678 + dwarf2_per_objfile->all_type_units.size ()))
3679 {
3680 complaint (_(".gdb_index entry has bad CU index"
3681 " [in module %s]"),
3682 objfile_name (dwarf2_per_objfile->objfile));
3683 continue;
3684 }
3685
3686 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3687
3688 /* Skip if already read in. */
3689 if (per_cu->v.quick->compunit_symtab)
3690 continue;
3691
3692 /* Check static vs global. */
3693 if (attrs_valid)
3694 {
3695 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3696
3697 if (iter->block_index.has_value ())
3698 {
3699 bool want_static = *iter->block_index == STATIC_BLOCK;
3700
3701 if (is_static != want_static)
3702 continue;
3703 }
3704
3705 /* Work around gold/15646. */
3706 if (!is_static && iter->global_seen)
3707 continue;
3708 if (!is_static)
3709 iter->global_seen = 1;
3710 }
3711
3712 /* Only check the symbol's kind if it has one. */
3713 if (attrs_valid)
3714 {
3715 switch (iter->domain)
3716 {
3717 case VAR_DOMAIN:
3718 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3719 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3720 /* Some types are also in VAR_DOMAIN. */
3721 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3722 continue;
3723 break;
3724 case STRUCT_DOMAIN:
3725 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3726 continue;
3727 break;
3728 case LABEL_DOMAIN:
3729 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3730 continue;
3731 break;
3732 case MODULE_DOMAIN:
3733 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3734 continue;
3735 break;
3736 default:
3737 break;
3738 }
3739 }
3740
3741 ++iter->next;
3742 return per_cu;
3743 }
3744
3745 return NULL;
3746 }
3747
3748 static struct compunit_symtab *
3749 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3750 const char *name, domain_enum domain)
3751 {
3752 struct compunit_symtab *stab_best = NULL;
3753 struct dwarf2_per_objfile *dwarf2_per_objfile
3754 = get_dwarf2_per_objfile (objfile);
3755
3756 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3757
3758 struct dw2_symtab_iterator iter;
3759 struct dwarf2_per_cu_data *per_cu;
3760
3761 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3762
3763 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3764 {
3765 struct symbol *sym, *with_opaque = NULL;
3766 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3767 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3768 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3769
3770 sym = block_find_symbol (block, name, domain,
3771 block_find_non_opaque_type_preferred,
3772 &with_opaque);
3773
3774 /* Some caution must be observed with overloaded functions
3775 and methods, since the index will not contain any overload
3776 information (but NAME might contain it). */
3777
3778 if (sym != NULL
3779 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3780 return stab;
3781 if (with_opaque != NULL
3782 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3783 stab_best = stab;
3784
3785 /* Keep looking through other CUs. */
3786 }
3787
3788 return stab_best;
3789 }
3790
3791 static void
3792 dw2_print_stats (struct objfile *objfile)
3793 {
3794 struct dwarf2_per_objfile *dwarf2_per_objfile
3795 = get_dwarf2_per_objfile (objfile);
3796 int total = (dwarf2_per_objfile->all_comp_units.size ()
3797 + dwarf2_per_objfile->all_type_units.size ());
3798 int count = 0;
3799
3800 for (int i = 0; i < total; ++i)
3801 {
3802 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3803
3804 if (!per_cu->v.quick->compunit_symtab)
3805 ++count;
3806 }
3807 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3808 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3809 }
3810
3811 /* This dumps minimal information about the index.
3812 It is called via "mt print objfiles".
3813 One use is to verify .gdb_index has been loaded by the
3814 gdb.dwarf2/gdb-index.exp testcase. */
3815
3816 static void
3817 dw2_dump (struct objfile *objfile)
3818 {
3819 struct dwarf2_per_objfile *dwarf2_per_objfile
3820 = get_dwarf2_per_objfile (objfile);
3821
3822 gdb_assert (dwarf2_per_objfile->using_index);
3823 printf_filtered (".gdb_index:");
3824 if (dwarf2_per_objfile->index_table != NULL)
3825 {
3826 printf_filtered (" version %d\n",
3827 dwarf2_per_objfile->index_table->version);
3828 }
3829 else
3830 printf_filtered (" faked for \"readnow\"\n");
3831 printf_filtered ("\n");
3832 }
3833
3834 static void
3835 dw2_expand_symtabs_for_function (struct objfile *objfile,
3836 const char *func_name)
3837 {
3838 struct dwarf2_per_objfile *dwarf2_per_objfile
3839 = get_dwarf2_per_objfile (objfile);
3840
3841 struct dw2_symtab_iterator iter;
3842 struct dwarf2_per_cu_data *per_cu;
3843
3844 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3845
3846 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3847 dw2_instantiate_symtab (per_cu, false);
3848
3849 }
3850
3851 static void
3852 dw2_expand_all_symtabs (struct objfile *objfile)
3853 {
3854 struct dwarf2_per_objfile *dwarf2_per_objfile
3855 = get_dwarf2_per_objfile (objfile);
3856 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3857 + dwarf2_per_objfile->all_type_units.size ());
3858
3859 for (int i = 0; i < total_units; ++i)
3860 {
3861 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3862
3863 /* We don't want to directly expand a partial CU, because if we
3864 read it with the wrong language, then assertion failures can
3865 be triggered later on. See PR symtab/23010. So, tell
3866 dw2_instantiate_symtab to skip partial CUs -- any important
3867 partial CU will be read via DW_TAG_imported_unit anyway. */
3868 dw2_instantiate_symtab (per_cu, true);
3869 }
3870 }
3871
3872 static void
3873 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3874 const char *fullname)
3875 {
3876 struct dwarf2_per_objfile *dwarf2_per_objfile
3877 = get_dwarf2_per_objfile (objfile);
3878
3879 /* We don't need to consider type units here.
3880 This is only called for examining code, e.g. expand_line_sal.
3881 There can be an order of magnitude (or more) more type units
3882 than comp units, and we avoid them if we can. */
3883
3884 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3885 {
3886 /* We only need to look at symtabs not already expanded. */
3887 if (per_cu->v.quick->compunit_symtab)
3888 continue;
3889
3890 quick_file_names *file_data = dw2_get_file_names (per_cu);
3891 if (file_data == NULL)
3892 continue;
3893
3894 for (int j = 0; j < file_data->num_file_names; ++j)
3895 {
3896 const char *this_fullname = file_data->file_names[j];
3897
3898 if (filename_cmp (this_fullname, fullname) == 0)
3899 {
3900 dw2_instantiate_symtab (per_cu, false);
3901 break;
3902 }
3903 }
3904 }
3905 }
3906
3907 static void
3908 dw2_map_matching_symbols
3909 (struct objfile *objfile,
3910 const lookup_name_info &name, domain_enum domain,
3911 int global,
3912 gdb::function_view<symbol_found_callback_ftype> callback,
3913 symbol_compare_ftype *ordered_compare)
3914 {
3915 /* Currently unimplemented; used for Ada. The function can be called if the
3916 current language is Ada for a non-Ada objfile using GNU index. As Ada
3917 does not look for non-Ada symbols this function should just return. */
3918 }
3919
3920 /* Starting from a search name, return the string that finds the upper
3921 bound of all strings that start with SEARCH_NAME in a sorted name
3922 list. Returns the empty string to indicate that the upper bound is
3923 the end of the list. */
3924
3925 static std::string
3926 make_sort_after_prefix_name (const char *search_name)
3927 {
3928 /* When looking to complete "func", we find the upper bound of all
3929 symbols that start with "func" by looking for where we'd insert
3930 the closest string that would follow "func" in lexicographical
3931 order. Usually, that's "func"-with-last-character-incremented,
3932 i.e. "fund". Mind non-ASCII characters, though. Usually those
3933 will be UTF-8 multi-byte sequences, but we can't be certain.
3934 Especially mind the 0xff character, which is a valid character in
3935 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3936 rule out compilers allowing it in identifiers. Note that
3937 conveniently, strcmp/strcasecmp are specified to compare
3938 characters interpreted as unsigned char. So what we do is treat
3939 the whole string as a base 256 number composed of a sequence of
3940 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3941 to 0, and carries 1 to the following more-significant position.
3942 If the very first character in SEARCH_NAME ends up incremented
3943 and carries/overflows, then the upper bound is the end of the
3944 list. The string after the empty string is also the empty
3945 string.
3946
3947 Some examples of this operation:
3948
3949 SEARCH_NAME => "+1" RESULT
3950
3951 "abc" => "abd"
3952 "ab\xff" => "ac"
3953 "\xff" "a" "\xff" => "\xff" "b"
3954 "\xff" => ""
3955 "\xff\xff" => ""
3956 "" => ""
3957
3958 Then, with these symbols for example:
3959
3960 func
3961 func1
3962 fund
3963
3964 completing "func" looks for symbols between "func" and
3965 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3966 which finds "func" and "func1", but not "fund".
3967
3968 And with:
3969
3970 funcÿ (Latin1 'ÿ' [0xff])
3971 funcÿ1
3972 fund
3973
3974 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3975 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3976
3977 And with:
3978
3979 ÿÿ (Latin1 'ÿ' [0xff])
3980 ÿÿ1
3981
3982 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3983 the end of the list.
3984 */
3985 std::string after = search_name;
3986 while (!after.empty () && (unsigned char) after.back () == 0xff)
3987 after.pop_back ();
3988 if (!after.empty ())
3989 after.back () = (unsigned char) after.back () + 1;
3990 return after;
3991 }
3992
3993 /* See declaration. */
3994
3995 std::pair<std::vector<name_component>::const_iterator,
3996 std::vector<name_component>::const_iterator>
3997 mapped_index_base::find_name_components_bounds
3998 (const lookup_name_info &lookup_name_without_params, language lang) const
3999 {
4000 auto *name_cmp
4001 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4002
4003 const char *lang_name
4004 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4005
4006 /* Comparison function object for lower_bound that matches against a
4007 given symbol name. */
4008 auto lookup_compare_lower = [&] (const name_component &elem,
4009 const char *name)
4010 {
4011 const char *elem_qualified = this->symbol_name_at (elem.idx);
4012 const char *elem_name = elem_qualified + elem.name_offset;
4013 return name_cmp (elem_name, name) < 0;
4014 };
4015
4016 /* Comparison function object for upper_bound that matches against a
4017 given symbol name. */
4018 auto lookup_compare_upper = [&] (const char *name,
4019 const name_component &elem)
4020 {
4021 const char *elem_qualified = this->symbol_name_at (elem.idx);
4022 const char *elem_name = elem_qualified + elem.name_offset;
4023 return name_cmp (name, elem_name) < 0;
4024 };
4025
4026 auto begin = this->name_components.begin ();
4027 auto end = this->name_components.end ();
4028
4029 /* Find the lower bound. */
4030 auto lower = [&] ()
4031 {
4032 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4033 return begin;
4034 else
4035 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4036 } ();
4037
4038 /* Find the upper bound. */
4039 auto upper = [&] ()
4040 {
4041 if (lookup_name_without_params.completion_mode ())
4042 {
4043 /* In completion mode, we want UPPER to point past all
4044 symbols names that have the same prefix. I.e., with
4045 these symbols, and completing "func":
4046
4047 function << lower bound
4048 function1
4049 other_function << upper bound
4050
4051 We find the upper bound by looking for the insertion
4052 point of "func"-with-last-character-incremented,
4053 i.e. "fund". */
4054 std::string after = make_sort_after_prefix_name (lang_name);
4055 if (after.empty ())
4056 return end;
4057 return std::lower_bound (lower, end, after.c_str (),
4058 lookup_compare_lower);
4059 }
4060 else
4061 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4062 } ();
4063
4064 return {lower, upper};
4065 }
4066
4067 /* See declaration. */
4068
4069 void
4070 mapped_index_base::build_name_components ()
4071 {
4072 if (!this->name_components.empty ())
4073 return;
4074
4075 this->name_components_casing = case_sensitivity;
4076 auto *name_cmp
4077 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4078
4079 /* The code below only knows how to break apart components of C++
4080 symbol names (and other languages that use '::' as
4081 namespace/module separator) and Ada symbol names. */
4082 auto count = this->symbol_name_count ();
4083 for (offset_type idx = 0; idx < count; idx++)
4084 {
4085 if (this->symbol_name_slot_invalid (idx))
4086 continue;
4087
4088 const char *name = this->symbol_name_at (idx);
4089
4090 /* Add each name component to the name component table. */
4091 unsigned int previous_len = 0;
4092
4093 if (strstr (name, "::") != nullptr)
4094 {
4095 for (unsigned int current_len = cp_find_first_component (name);
4096 name[current_len] != '\0';
4097 current_len += cp_find_first_component (name + current_len))
4098 {
4099 gdb_assert (name[current_len] == ':');
4100 this->name_components.push_back ({previous_len, idx});
4101 /* Skip the '::'. */
4102 current_len += 2;
4103 previous_len = current_len;
4104 }
4105 }
4106 else
4107 {
4108 /* Handle the Ada encoded (aka mangled) form here. */
4109 for (const char *iter = strstr (name, "__");
4110 iter != nullptr;
4111 iter = strstr (iter, "__"))
4112 {
4113 this->name_components.push_back ({previous_len, idx});
4114 iter += 2;
4115 previous_len = iter - name;
4116 }
4117 }
4118
4119 this->name_components.push_back ({previous_len, idx});
4120 }
4121
4122 /* Sort name_components elements by name. */
4123 auto name_comp_compare = [&] (const name_component &left,
4124 const name_component &right)
4125 {
4126 const char *left_qualified = this->symbol_name_at (left.idx);
4127 const char *right_qualified = this->symbol_name_at (right.idx);
4128
4129 const char *left_name = left_qualified + left.name_offset;
4130 const char *right_name = right_qualified + right.name_offset;
4131
4132 return name_cmp (left_name, right_name) < 0;
4133 };
4134
4135 std::sort (this->name_components.begin (),
4136 this->name_components.end (),
4137 name_comp_compare);
4138 }
4139
4140 /* Helper for dw2_expand_symtabs_matching that works with a
4141 mapped_index_base instead of the containing objfile. This is split
4142 to a separate function in order to be able to unit test the
4143 name_components matching using a mock mapped_index_base. For each
4144 symbol name that matches, calls MATCH_CALLBACK, passing it the
4145 symbol's index in the mapped_index_base symbol table. */
4146
4147 static void
4148 dw2_expand_symtabs_matching_symbol
4149 (mapped_index_base &index,
4150 const lookup_name_info &lookup_name_in,
4151 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4152 enum search_domain kind,
4153 gdb::function_view<bool (offset_type)> match_callback)
4154 {
4155 lookup_name_info lookup_name_without_params
4156 = lookup_name_in.make_ignore_params ();
4157
4158 /* Build the symbol name component sorted vector, if we haven't
4159 yet. */
4160 index.build_name_components ();
4161
4162 /* The same symbol may appear more than once in the range though.
4163 E.g., if we're looking for symbols that complete "w", and we have
4164 a symbol named "w1::w2", we'll find the two name components for
4165 that same symbol in the range. To be sure we only call the
4166 callback once per symbol, we first collect the symbol name
4167 indexes that matched in a temporary vector and ignore
4168 duplicates. */
4169 std::vector<offset_type> matches;
4170
4171 struct name_and_matcher
4172 {
4173 symbol_name_matcher_ftype *matcher;
4174 const std::string &name;
4175
4176 bool operator== (const name_and_matcher &other) const
4177 {
4178 return matcher == other.matcher && name == other.name;
4179 }
4180 };
4181
4182 /* A vector holding all the different symbol name matchers, for all
4183 languages. */
4184 std::vector<name_and_matcher> matchers;
4185
4186 for (int i = 0; i < nr_languages; i++)
4187 {
4188 enum language lang_e = (enum language) i;
4189
4190 const language_defn *lang = language_def (lang_e);
4191 symbol_name_matcher_ftype *name_matcher
4192 = get_symbol_name_matcher (lang, lookup_name_without_params);
4193
4194 name_and_matcher key {
4195 name_matcher,
4196 lookup_name_without_params.language_lookup_name (lang_e)
4197 };
4198
4199 /* Don't insert the same comparison routine more than once.
4200 Note that we do this linear walk. This is not a problem in
4201 practice because the number of supported languages is
4202 low. */
4203 if (std::find (matchers.begin (), matchers.end (), key)
4204 != matchers.end ())
4205 continue;
4206 matchers.push_back (std::move (key));
4207
4208 auto bounds
4209 = index.find_name_components_bounds (lookup_name_without_params,
4210 lang_e);
4211
4212 /* Now for each symbol name in range, check to see if we have a name
4213 match, and if so, call the MATCH_CALLBACK callback. */
4214
4215 for (; bounds.first != bounds.second; ++bounds.first)
4216 {
4217 const char *qualified = index.symbol_name_at (bounds.first->idx);
4218
4219 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4220 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4221 continue;
4222
4223 matches.push_back (bounds.first->idx);
4224 }
4225 }
4226
4227 std::sort (matches.begin (), matches.end ());
4228
4229 /* Finally call the callback, once per match. */
4230 ULONGEST prev = -1;
4231 for (offset_type idx : matches)
4232 {
4233 if (prev != idx)
4234 {
4235 if (!match_callback (idx))
4236 break;
4237 prev = idx;
4238 }
4239 }
4240
4241 /* Above we use a type wider than idx's for 'prev', since 0 and
4242 (offset_type)-1 are both possible values. */
4243 static_assert (sizeof (prev) > sizeof (offset_type), "");
4244 }
4245
4246 #if GDB_SELF_TEST
4247
4248 namespace selftests { namespace dw2_expand_symtabs_matching {
4249
4250 /* A mock .gdb_index/.debug_names-like name index table, enough to
4251 exercise dw2_expand_symtabs_matching_symbol, which works with the
4252 mapped_index_base interface. Builds an index from the symbol list
4253 passed as parameter to the constructor. */
4254 class mock_mapped_index : public mapped_index_base
4255 {
4256 public:
4257 mock_mapped_index (gdb::array_view<const char *> symbols)
4258 : m_symbol_table (symbols)
4259 {}
4260
4261 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4262
4263 /* Return the number of names in the symbol table. */
4264 size_t symbol_name_count () const override
4265 {
4266 return m_symbol_table.size ();
4267 }
4268
4269 /* Get the name of the symbol at IDX in the symbol table. */
4270 const char *symbol_name_at (offset_type idx) const override
4271 {
4272 return m_symbol_table[idx];
4273 }
4274
4275 private:
4276 gdb::array_view<const char *> m_symbol_table;
4277 };
4278
4279 /* Convenience function that converts a NULL pointer to a "<null>"
4280 string, to pass to print routines. */
4281
4282 static const char *
4283 string_or_null (const char *str)
4284 {
4285 return str != NULL ? str : "<null>";
4286 }
4287
4288 /* Check if a lookup_name_info built from
4289 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4290 index. EXPECTED_LIST is the list of expected matches, in expected
4291 matching order. If no match expected, then an empty list is
4292 specified. Returns true on success. On failure prints a warning
4293 indicating the file:line that failed, and returns false. */
4294
4295 static bool
4296 check_match (const char *file, int line,
4297 mock_mapped_index &mock_index,
4298 const char *name, symbol_name_match_type match_type,
4299 bool completion_mode,
4300 std::initializer_list<const char *> expected_list)
4301 {
4302 lookup_name_info lookup_name (name, match_type, completion_mode);
4303
4304 bool matched = true;
4305
4306 auto mismatch = [&] (const char *expected_str,
4307 const char *got)
4308 {
4309 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4310 "expected=\"%s\", got=\"%s\"\n"),
4311 file, line,
4312 (match_type == symbol_name_match_type::FULL
4313 ? "FULL" : "WILD"),
4314 name, string_or_null (expected_str), string_or_null (got));
4315 matched = false;
4316 };
4317
4318 auto expected_it = expected_list.begin ();
4319 auto expected_end = expected_list.end ();
4320
4321 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4322 NULL, ALL_DOMAIN,
4323 [&] (offset_type idx)
4324 {
4325 const char *matched_name = mock_index.symbol_name_at (idx);
4326 const char *expected_str
4327 = expected_it == expected_end ? NULL : *expected_it++;
4328
4329 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4330 mismatch (expected_str, matched_name);
4331 return true;
4332 });
4333
4334 const char *expected_str
4335 = expected_it == expected_end ? NULL : *expected_it++;
4336 if (expected_str != NULL)
4337 mismatch (expected_str, NULL);
4338
4339 return matched;
4340 }
4341
4342 /* The symbols added to the mock mapped_index for testing (in
4343 canonical form). */
4344 static const char *test_symbols[] = {
4345 "function",
4346 "std::bar",
4347 "std::zfunction",
4348 "std::zfunction2",
4349 "w1::w2",
4350 "ns::foo<char*>",
4351 "ns::foo<int>",
4352 "ns::foo<long>",
4353 "ns2::tmpl<int>::foo2",
4354 "(anonymous namespace)::A::B::C",
4355
4356 /* These are used to check that the increment-last-char in the
4357 matching algorithm for completion doesn't match "t1_fund" when
4358 completing "t1_func". */
4359 "t1_func",
4360 "t1_func1",
4361 "t1_fund",
4362 "t1_fund1",
4363
4364 /* A UTF-8 name with multi-byte sequences to make sure that
4365 cp-name-parser understands this as a single identifier ("função"
4366 is "function" in PT). */
4367 u8"u8função",
4368
4369 /* \377 (0xff) is Latin1 'ÿ'. */
4370 "yfunc\377",
4371
4372 /* \377 (0xff) is Latin1 'ÿ'. */
4373 "\377",
4374 "\377\377123",
4375
4376 /* A name with all sorts of complications. Starts with "z" to make
4377 it easier for the completion tests below. */
4378 #define Z_SYM_NAME \
4379 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4380 "::tuple<(anonymous namespace)::ui*, " \
4381 "std::default_delete<(anonymous namespace)::ui>, void>"
4382
4383 Z_SYM_NAME
4384 };
4385
4386 /* Returns true if the mapped_index_base::find_name_component_bounds
4387 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4388 in completion mode. */
4389
4390 static bool
4391 check_find_bounds_finds (mapped_index_base &index,
4392 const char *search_name,
4393 gdb::array_view<const char *> expected_syms)
4394 {
4395 lookup_name_info lookup_name (search_name,
4396 symbol_name_match_type::FULL, true);
4397
4398 auto bounds = index.find_name_components_bounds (lookup_name,
4399 language_cplus);
4400
4401 size_t distance = std::distance (bounds.first, bounds.second);
4402 if (distance != expected_syms.size ())
4403 return false;
4404
4405 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4406 {
4407 auto nc_elem = bounds.first + exp_elem;
4408 const char *qualified = index.symbol_name_at (nc_elem->idx);
4409 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4410 return false;
4411 }
4412
4413 return true;
4414 }
4415
4416 /* Test the lower-level mapped_index::find_name_component_bounds
4417 method. */
4418
4419 static void
4420 test_mapped_index_find_name_component_bounds ()
4421 {
4422 mock_mapped_index mock_index (test_symbols);
4423
4424 mock_index.build_name_components ();
4425
4426 /* Test the lower-level mapped_index::find_name_component_bounds
4427 method in completion mode. */
4428 {
4429 static const char *expected_syms[] = {
4430 "t1_func",
4431 "t1_func1",
4432 };
4433
4434 SELF_CHECK (check_find_bounds_finds (mock_index,
4435 "t1_func", expected_syms));
4436 }
4437
4438 /* Check that the increment-last-char in the name matching algorithm
4439 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4440 {
4441 static const char *expected_syms1[] = {
4442 "\377",
4443 "\377\377123",
4444 };
4445 SELF_CHECK (check_find_bounds_finds (mock_index,
4446 "\377", expected_syms1));
4447
4448 static const char *expected_syms2[] = {
4449 "\377\377123",
4450 };
4451 SELF_CHECK (check_find_bounds_finds (mock_index,
4452 "\377\377", expected_syms2));
4453 }
4454 }
4455
4456 /* Test dw2_expand_symtabs_matching_symbol. */
4457
4458 static void
4459 test_dw2_expand_symtabs_matching_symbol ()
4460 {
4461 mock_mapped_index mock_index (test_symbols);
4462
4463 /* We let all tests run until the end even if some fails, for debug
4464 convenience. */
4465 bool any_mismatch = false;
4466
4467 /* Create the expected symbols list (an initializer_list). Needed
4468 because lists have commas, and we need to pass them to CHECK,
4469 which is a macro. */
4470 #define EXPECT(...) { __VA_ARGS__ }
4471
4472 /* Wrapper for check_match that passes down the current
4473 __FILE__/__LINE__. */
4474 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4475 any_mismatch |= !check_match (__FILE__, __LINE__, \
4476 mock_index, \
4477 NAME, MATCH_TYPE, COMPLETION_MODE, \
4478 EXPECTED_LIST)
4479
4480 /* Identity checks. */
4481 for (const char *sym : test_symbols)
4482 {
4483 /* Should be able to match all existing symbols. */
4484 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4485 EXPECT (sym));
4486
4487 /* Should be able to match all existing symbols with
4488 parameters. */
4489 std::string with_params = std::string (sym) + "(int)";
4490 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4491 EXPECT (sym));
4492
4493 /* Should be able to match all existing symbols with
4494 parameters and qualifiers. */
4495 with_params = std::string (sym) + " ( int ) const";
4496 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4497 EXPECT (sym));
4498
4499 /* This should really find sym, but cp-name-parser.y doesn't
4500 know about lvalue/rvalue qualifiers yet. */
4501 with_params = std::string (sym) + " ( int ) &&";
4502 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4503 {});
4504 }
4505
4506 /* Check that the name matching algorithm for completion doesn't get
4507 confused with Latin1 'ÿ' / 0xff. */
4508 {
4509 static const char str[] = "\377";
4510 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4511 EXPECT ("\377", "\377\377123"));
4512 }
4513
4514 /* Check that the increment-last-char in the matching algorithm for
4515 completion doesn't match "t1_fund" when completing "t1_func". */
4516 {
4517 static const char str[] = "t1_func";
4518 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4519 EXPECT ("t1_func", "t1_func1"));
4520 }
4521
4522 /* Check that completion mode works at each prefix of the expected
4523 symbol name. */
4524 {
4525 static const char str[] = "function(int)";
4526 size_t len = strlen (str);
4527 std::string lookup;
4528
4529 for (size_t i = 1; i < len; i++)
4530 {
4531 lookup.assign (str, i);
4532 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4533 EXPECT ("function"));
4534 }
4535 }
4536
4537 /* While "w" is a prefix of both components, the match function
4538 should still only be called once. */
4539 {
4540 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4541 EXPECT ("w1::w2"));
4542 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4543 EXPECT ("w1::w2"));
4544 }
4545
4546 /* Same, with a "complicated" symbol. */
4547 {
4548 static const char str[] = Z_SYM_NAME;
4549 size_t len = strlen (str);
4550 std::string lookup;
4551
4552 for (size_t i = 1; i < len; i++)
4553 {
4554 lookup.assign (str, i);
4555 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4556 EXPECT (Z_SYM_NAME));
4557 }
4558 }
4559
4560 /* In FULL mode, an incomplete symbol doesn't match. */
4561 {
4562 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4563 {});
4564 }
4565
4566 /* A complete symbol with parameters matches any overload, since the
4567 index has no overload info. */
4568 {
4569 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4570 EXPECT ("std::zfunction", "std::zfunction2"));
4571 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4572 EXPECT ("std::zfunction", "std::zfunction2"));
4573 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4574 EXPECT ("std::zfunction", "std::zfunction2"));
4575 }
4576
4577 /* Check that whitespace is ignored appropriately. A symbol with a
4578 template argument list. */
4579 {
4580 static const char expected[] = "ns::foo<int>";
4581 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4582 EXPECT (expected));
4583 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4584 EXPECT (expected));
4585 }
4586
4587 /* Check that whitespace is ignored appropriately. A symbol with a
4588 template argument list that includes a pointer. */
4589 {
4590 static const char expected[] = "ns::foo<char*>";
4591 /* Try both completion and non-completion modes. */
4592 static const bool completion_mode[2] = {false, true};
4593 for (size_t i = 0; i < 2; i++)
4594 {
4595 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4596 completion_mode[i], EXPECT (expected));
4597 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4598 completion_mode[i], EXPECT (expected));
4599
4600 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4601 completion_mode[i], EXPECT (expected));
4602 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4603 completion_mode[i], EXPECT (expected));
4604 }
4605 }
4606
4607 {
4608 /* Check method qualifiers are ignored. */
4609 static const char expected[] = "ns::foo<char*>";
4610 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4611 symbol_name_match_type::FULL, true, EXPECT (expected));
4612 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4613 symbol_name_match_type::FULL, true, EXPECT (expected));
4614 CHECK_MATCH ("foo < char * > ( int ) const",
4615 symbol_name_match_type::WILD, true, EXPECT (expected));
4616 CHECK_MATCH ("foo < char * > ( int ) &&",
4617 symbol_name_match_type::WILD, true, EXPECT (expected));
4618 }
4619
4620 /* Test lookup names that don't match anything. */
4621 {
4622 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4623 {});
4624
4625 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4626 {});
4627 }
4628
4629 /* Some wild matching tests, exercising "(anonymous namespace)",
4630 which should not be confused with a parameter list. */
4631 {
4632 static const char *syms[] = {
4633 "A::B::C",
4634 "B::C",
4635 "C",
4636 "A :: B :: C ( int )",
4637 "B :: C ( int )",
4638 "C ( int )",
4639 };
4640
4641 for (const char *s : syms)
4642 {
4643 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4644 EXPECT ("(anonymous namespace)::A::B::C"));
4645 }
4646 }
4647
4648 {
4649 static const char expected[] = "ns2::tmpl<int>::foo2";
4650 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4651 EXPECT (expected));
4652 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4653 EXPECT (expected));
4654 }
4655
4656 SELF_CHECK (!any_mismatch);
4657
4658 #undef EXPECT
4659 #undef CHECK_MATCH
4660 }
4661
4662 static void
4663 run_test ()
4664 {
4665 test_mapped_index_find_name_component_bounds ();
4666 test_dw2_expand_symtabs_matching_symbol ();
4667 }
4668
4669 }} // namespace selftests::dw2_expand_symtabs_matching
4670
4671 #endif /* GDB_SELF_TEST */
4672
4673 /* If FILE_MATCHER is NULL or if PER_CU has
4674 dwarf2_per_cu_quick_data::MARK set (see
4675 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4676 EXPANSION_NOTIFY on it. */
4677
4678 static void
4679 dw2_expand_symtabs_matching_one
4680 (struct dwarf2_per_cu_data *per_cu,
4681 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4682 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4683 {
4684 if (file_matcher == NULL || per_cu->v.quick->mark)
4685 {
4686 bool symtab_was_null
4687 = (per_cu->v.quick->compunit_symtab == NULL);
4688
4689 dw2_instantiate_symtab (per_cu, false);
4690
4691 if (expansion_notify != NULL
4692 && symtab_was_null
4693 && per_cu->v.quick->compunit_symtab != NULL)
4694 expansion_notify (per_cu->v.quick->compunit_symtab);
4695 }
4696 }
4697
4698 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4699 matched, to expand corresponding CUs that were marked. IDX is the
4700 index of the symbol name that matched. */
4701
4702 static void
4703 dw2_expand_marked_cus
4704 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4705 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4706 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4707 search_domain kind)
4708 {
4709 offset_type *vec, vec_len, vec_idx;
4710 bool global_seen = false;
4711 mapped_index &index = *dwarf2_per_objfile->index_table;
4712
4713 vec = (offset_type *) (index.constant_pool
4714 + MAYBE_SWAP (index.symbol_table[idx].vec));
4715 vec_len = MAYBE_SWAP (vec[0]);
4716 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4717 {
4718 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4719 /* This value is only valid for index versions >= 7. */
4720 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4721 gdb_index_symbol_kind symbol_kind =
4722 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4723 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4724 /* Only check the symbol attributes if they're present.
4725 Indices prior to version 7 don't record them,
4726 and indices >= 7 may elide them for certain symbols
4727 (gold does this). */
4728 int attrs_valid =
4729 (index.version >= 7
4730 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4731
4732 /* Work around gold/15646. */
4733 if (attrs_valid)
4734 {
4735 if (!is_static && global_seen)
4736 continue;
4737 if (!is_static)
4738 global_seen = true;
4739 }
4740
4741 /* Only check the symbol's kind if it has one. */
4742 if (attrs_valid)
4743 {
4744 switch (kind)
4745 {
4746 case VARIABLES_DOMAIN:
4747 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4748 continue;
4749 break;
4750 case FUNCTIONS_DOMAIN:
4751 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4752 continue;
4753 break;
4754 case TYPES_DOMAIN:
4755 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4756 continue;
4757 break;
4758 case MODULES_DOMAIN:
4759 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4760 continue;
4761 break;
4762 default:
4763 break;
4764 }
4765 }
4766
4767 /* Don't crash on bad data. */
4768 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4769 + dwarf2_per_objfile->all_type_units.size ()))
4770 {
4771 complaint (_(".gdb_index entry has bad CU index"
4772 " [in module %s]"),
4773 objfile_name (dwarf2_per_objfile->objfile));
4774 continue;
4775 }
4776
4777 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4778 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4779 expansion_notify);
4780 }
4781 }
4782
4783 /* If FILE_MATCHER is non-NULL, set all the
4784 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4785 that match FILE_MATCHER. */
4786
4787 static void
4788 dw_expand_symtabs_matching_file_matcher
4789 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4790 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4791 {
4792 if (file_matcher == NULL)
4793 return;
4794
4795 objfile *const objfile = dwarf2_per_objfile->objfile;
4796
4797 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4798 htab_eq_pointer,
4799 NULL, xcalloc, xfree));
4800 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4801 htab_eq_pointer,
4802 NULL, xcalloc, xfree));
4803
4804 /* The rule is CUs specify all the files, including those used by
4805 any TU, so there's no need to scan TUs here. */
4806
4807 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4808 {
4809 QUIT;
4810
4811 per_cu->v.quick->mark = 0;
4812
4813 /* We only need to look at symtabs not already expanded. */
4814 if (per_cu->v.quick->compunit_symtab)
4815 continue;
4816
4817 quick_file_names *file_data = dw2_get_file_names (per_cu);
4818 if (file_data == NULL)
4819 continue;
4820
4821 if (htab_find (visited_not_found.get (), file_data) != NULL)
4822 continue;
4823 else if (htab_find (visited_found.get (), file_data) != NULL)
4824 {
4825 per_cu->v.quick->mark = 1;
4826 continue;
4827 }
4828
4829 for (int j = 0; j < file_data->num_file_names; ++j)
4830 {
4831 const char *this_real_name;
4832
4833 if (file_matcher (file_data->file_names[j], false))
4834 {
4835 per_cu->v.quick->mark = 1;
4836 break;
4837 }
4838
4839 /* Before we invoke realpath, which can get expensive when many
4840 files are involved, do a quick comparison of the basenames. */
4841 if (!basenames_may_differ
4842 && !file_matcher (lbasename (file_data->file_names[j]),
4843 true))
4844 continue;
4845
4846 this_real_name = dw2_get_real_path (objfile, file_data, j);
4847 if (file_matcher (this_real_name, false))
4848 {
4849 per_cu->v.quick->mark = 1;
4850 break;
4851 }
4852 }
4853
4854 void **slot = htab_find_slot (per_cu->v.quick->mark
4855 ? visited_found.get ()
4856 : visited_not_found.get (),
4857 file_data, INSERT);
4858 *slot = file_data;
4859 }
4860 }
4861
4862 static void
4863 dw2_expand_symtabs_matching
4864 (struct objfile *objfile,
4865 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4866 const lookup_name_info &lookup_name,
4867 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4868 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4869 enum search_domain kind)
4870 {
4871 struct dwarf2_per_objfile *dwarf2_per_objfile
4872 = get_dwarf2_per_objfile (objfile);
4873
4874 /* index_table is NULL if OBJF_READNOW. */
4875 if (!dwarf2_per_objfile->index_table)
4876 return;
4877
4878 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4879
4880 mapped_index &index = *dwarf2_per_objfile->index_table;
4881
4882 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4883 symbol_matcher,
4884 kind, [&] (offset_type idx)
4885 {
4886 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4887 expansion_notify, kind);
4888 return true;
4889 });
4890 }
4891
4892 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4893 symtab. */
4894
4895 static struct compunit_symtab *
4896 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4897 CORE_ADDR pc)
4898 {
4899 int i;
4900
4901 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4902 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4903 return cust;
4904
4905 if (cust->includes == NULL)
4906 return NULL;
4907
4908 for (i = 0; cust->includes[i]; ++i)
4909 {
4910 struct compunit_symtab *s = cust->includes[i];
4911
4912 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4913 if (s != NULL)
4914 return s;
4915 }
4916
4917 return NULL;
4918 }
4919
4920 static struct compunit_symtab *
4921 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4922 struct bound_minimal_symbol msymbol,
4923 CORE_ADDR pc,
4924 struct obj_section *section,
4925 int warn_if_readin)
4926 {
4927 struct dwarf2_per_cu_data *data;
4928 struct compunit_symtab *result;
4929
4930 if (!objfile->partial_symtabs->psymtabs_addrmap)
4931 return NULL;
4932
4933 CORE_ADDR baseaddr = objfile->text_section_offset ();
4934 data = (struct dwarf2_per_cu_data *) addrmap_find
4935 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4936 if (!data)
4937 return NULL;
4938
4939 if (warn_if_readin && data->v.quick->compunit_symtab)
4940 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4941 paddress (get_objfile_arch (objfile), pc));
4942
4943 result
4944 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4945 false),
4946 pc);
4947 gdb_assert (result != NULL);
4948 return result;
4949 }
4950
4951 static void
4952 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4953 void *data, int need_fullname)
4954 {
4955 struct dwarf2_per_objfile *dwarf2_per_objfile
4956 = get_dwarf2_per_objfile (objfile);
4957
4958 if (!dwarf2_per_objfile->filenames_cache)
4959 {
4960 dwarf2_per_objfile->filenames_cache.emplace ();
4961
4962 htab_up visited (htab_create_alloc (10,
4963 htab_hash_pointer, htab_eq_pointer,
4964 NULL, xcalloc, xfree));
4965
4966 /* The rule is CUs specify all the files, including those used
4967 by any TU, so there's no need to scan TUs here. We can
4968 ignore file names coming from already-expanded CUs. */
4969
4970 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4971 {
4972 if (per_cu->v.quick->compunit_symtab)
4973 {
4974 void **slot = htab_find_slot (visited.get (),
4975 per_cu->v.quick->file_names,
4976 INSERT);
4977
4978 *slot = per_cu->v.quick->file_names;
4979 }
4980 }
4981
4982 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4983 {
4984 /* We only need to look at symtabs not already expanded. */
4985 if (per_cu->v.quick->compunit_symtab)
4986 continue;
4987
4988 quick_file_names *file_data = dw2_get_file_names (per_cu);
4989 if (file_data == NULL)
4990 continue;
4991
4992 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4993 if (*slot)
4994 {
4995 /* Already visited. */
4996 continue;
4997 }
4998 *slot = file_data;
4999
5000 for (int j = 0; j < file_data->num_file_names; ++j)
5001 {
5002 const char *filename = file_data->file_names[j];
5003 dwarf2_per_objfile->filenames_cache->seen (filename);
5004 }
5005 }
5006 }
5007
5008 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5009 {
5010 gdb::unique_xmalloc_ptr<char> this_real_name;
5011
5012 if (need_fullname)
5013 this_real_name = gdb_realpath (filename);
5014 (*fun) (filename, this_real_name.get (), data);
5015 });
5016 }
5017
5018 static int
5019 dw2_has_symbols (struct objfile *objfile)
5020 {
5021 return 1;
5022 }
5023
5024 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5025 {
5026 dw2_has_symbols,
5027 dw2_find_last_source_symtab,
5028 dw2_forget_cached_source_info,
5029 dw2_map_symtabs_matching_filename,
5030 dw2_lookup_symbol,
5031 dw2_print_stats,
5032 dw2_dump,
5033 dw2_expand_symtabs_for_function,
5034 dw2_expand_all_symtabs,
5035 dw2_expand_symtabs_with_fullname,
5036 dw2_map_matching_symbols,
5037 dw2_expand_symtabs_matching,
5038 dw2_find_pc_sect_compunit_symtab,
5039 NULL,
5040 dw2_map_symbol_filenames
5041 };
5042
5043 /* DWARF-5 debug_names reader. */
5044
5045 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5046 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5047
5048 /* A helper function that reads the .debug_names section in SECTION
5049 and fills in MAP. FILENAME is the name of the file containing the
5050 section; it is used for error reporting.
5051
5052 Returns true if all went well, false otherwise. */
5053
5054 static bool
5055 read_debug_names_from_section (struct objfile *objfile,
5056 const char *filename,
5057 struct dwarf2_section_info *section,
5058 mapped_debug_names &map)
5059 {
5060 if (section->empty ())
5061 return false;
5062
5063 /* Older elfutils strip versions could keep the section in the main
5064 executable while splitting it for the separate debug info file. */
5065 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5066 return false;
5067
5068 section->read (objfile);
5069
5070 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5071
5072 const gdb_byte *addr = section->buffer;
5073
5074 bfd *const abfd = section->get_bfd_owner ();
5075
5076 unsigned int bytes_read;
5077 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5078 addr += bytes_read;
5079
5080 map.dwarf5_is_dwarf64 = bytes_read != 4;
5081 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5082 if (bytes_read + length != section->size)
5083 {
5084 /* There may be multiple per-CU indices. */
5085 warning (_("Section .debug_names in %s length %s does not match "
5086 "section length %s, ignoring .debug_names."),
5087 filename, plongest (bytes_read + length),
5088 pulongest (section->size));
5089 return false;
5090 }
5091
5092 /* The version number. */
5093 uint16_t version = read_2_bytes (abfd, addr);
5094 addr += 2;
5095 if (version != 5)
5096 {
5097 warning (_("Section .debug_names in %s has unsupported version %d, "
5098 "ignoring .debug_names."),
5099 filename, version);
5100 return false;
5101 }
5102
5103 /* Padding. */
5104 uint16_t padding = read_2_bytes (abfd, addr);
5105 addr += 2;
5106 if (padding != 0)
5107 {
5108 warning (_("Section .debug_names in %s has unsupported padding %d, "
5109 "ignoring .debug_names."),
5110 filename, padding);
5111 return false;
5112 }
5113
5114 /* comp_unit_count - The number of CUs in the CU list. */
5115 map.cu_count = read_4_bytes (abfd, addr);
5116 addr += 4;
5117
5118 /* local_type_unit_count - The number of TUs in the local TU
5119 list. */
5120 map.tu_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* foreign_type_unit_count - The number of TUs in the foreign TU
5124 list. */
5125 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5126 addr += 4;
5127 if (foreign_tu_count != 0)
5128 {
5129 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5130 "ignoring .debug_names."),
5131 filename, static_cast<unsigned long> (foreign_tu_count));
5132 return false;
5133 }
5134
5135 /* bucket_count - The number of hash buckets in the hash lookup
5136 table. */
5137 map.bucket_count = read_4_bytes (abfd, addr);
5138 addr += 4;
5139
5140 /* name_count - The number of unique names in the index. */
5141 map.name_count = read_4_bytes (abfd, addr);
5142 addr += 4;
5143
5144 /* abbrev_table_size - The size in bytes of the abbreviations
5145 table. */
5146 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5147 addr += 4;
5148
5149 /* augmentation_string_size - The size in bytes of the augmentation
5150 string. This value is rounded up to a multiple of 4. */
5151 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5152 addr += 4;
5153 map.augmentation_is_gdb = ((augmentation_string_size
5154 == sizeof (dwarf5_augmentation))
5155 && memcmp (addr, dwarf5_augmentation,
5156 sizeof (dwarf5_augmentation)) == 0);
5157 augmentation_string_size += (-augmentation_string_size) & 3;
5158 addr += augmentation_string_size;
5159
5160 /* List of CUs */
5161 map.cu_table_reordered = addr;
5162 addr += map.cu_count * map.offset_size;
5163
5164 /* List of Local TUs */
5165 map.tu_table_reordered = addr;
5166 addr += map.tu_count * map.offset_size;
5167
5168 /* Hash Lookup Table */
5169 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5170 addr += map.bucket_count * 4;
5171 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5172 addr += map.name_count * 4;
5173
5174 /* Name Table */
5175 map.name_table_string_offs_reordered = addr;
5176 addr += map.name_count * map.offset_size;
5177 map.name_table_entry_offs_reordered = addr;
5178 addr += map.name_count * map.offset_size;
5179
5180 const gdb_byte *abbrev_table_start = addr;
5181 for (;;)
5182 {
5183 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5184 addr += bytes_read;
5185 if (index_num == 0)
5186 break;
5187
5188 const auto insertpair
5189 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5190 if (!insertpair.second)
5191 {
5192 warning (_("Section .debug_names in %s has duplicate index %s, "
5193 "ignoring .debug_names."),
5194 filename, pulongest (index_num));
5195 return false;
5196 }
5197 mapped_debug_names::index_val &indexval = insertpair.first->second;
5198 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5199 addr += bytes_read;
5200
5201 for (;;)
5202 {
5203 mapped_debug_names::index_val::attr attr;
5204 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5205 addr += bytes_read;
5206 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5207 addr += bytes_read;
5208 if (attr.form == DW_FORM_implicit_const)
5209 {
5210 attr.implicit_const = read_signed_leb128 (abfd, addr,
5211 &bytes_read);
5212 addr += bytes_read;
5213 }
5214 if (attr.dw_idx == 0 && attr.form == 0)
5215 break;
5216 indexval.attr_vec.push_back (std::move (attr));
5217 }
5218 }
5219 if (addr != abbrev_table_start + abbrev_table_size)
5220 {
5221 warning (_("Section .debug_names in %s has abbreviation_table "
5222 "of size %s vs. written as %u, ignoring .debug_names."),
5223 filename, plongest (addr - abbrev_table_start),
5224 abbrev_table_size);
5225 return false;
5226 }
5227 map.entry_pool = addr;
5228
5229 return true;
5230 }
5231
5232 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5233 list. */
5234
5235 static void
5236 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5237 const mapped_debug_names &map,
5238 dwarf2_section_info &section,
5239 bool is_dwz)
5240 {
5241 sect_offset sect_off_prev;
5242 for (uint32_t i = 0; i <= map.cu_count; ++i)
5243 {
5244 sect_offset sect_off_next;
5245 if (i < map.cu_count)
5246 {
5247 sect_off_next
5248 = (sect_offset) (extract_unsigned_integer
5249 (map.cu_table_reordered + i * map.offset_size,
5250 map.offset_size,
5251 map.dwarf5_byte_order));
5252 }
5253 else
5254 sect_off_next = (sect_offset) section.size;
5255 if (i >= 1)
5256 {
5257 const ULONGEST length = sect_off_next - sect_off_prev;
5258 dwarf2_per_cu_data *per_cu
5259 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5260 sect_off_prev, length);
5261 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5262 }
5263 sect_off_prev = sect_off_next;
5264 }
5265 }
5266
5267 /* Read the CU list from the mapped index, and use it to create all
5268 the CU objects for this dwarf2_per_objfile. */
5269
5270 static void
5271 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5272 const mapped_debug_names &map,
5273 const mapped_debug_names &dwz_map)
5274 {
5275 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5276 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5277
5278 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5279 dwarf2_per_objfile->info,
5280 false /* is_dwz */);
5281
5282 if (dwz_map.cu_count == 0)
5283 return;
5284
5285 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5286 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5287 true /* is_dwz */);
5288 }
5289
5290 /* Read .debug_names. If everything went ok, initialize the "quick"
5291 elements of all the CUs and return true. Otherwise, return false. */
5292
5293 static bool
5294 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5295 {
5296 std::unique_ptr<mapped_debug_names> map
5297 (new mapped_debug_names (dwarf2_per_objfile));
5298 mapped_debug_names dwz_map (dwarf2_per_objfile);
5299 struct objfile *objfile = dwarf2_per_objfile->objfile;
5300
5301 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5302 &dwarf2_per_objfile->debug_names,
5303 *map))
5304 return false;
5305
5306 /* Don't use the index if it's empty. */
5307 if (map->name_count == 0)
5308 return false;
5309
5310 /* If there is a .dwz file, read it so we can get its CU list as
5311 well. */
5312 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5313 if (dwz != NULL)
5314 {
5315 if (!read_debug_names_from_section (objfile,
5316 bfd_get_filename (dwz->dwz_bfd.get ()),
5317 &dwz->debug_names, dwz_map))
5318 {
5319 warning (_("could not read '.debug_names' section from %s; skipping"),
5320 bfd_get_filename (dwz->dwz_bfd.get ()));
5321 return false;
5322 }
5323 }
5324
5325 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5326
5327 if (map->tu_count != 0)
5328 {
5329 /* We can only handle a single .debug_types when we have an
5330 index. */
5331 if (dwarf2_per_objfile->types.size () != 1)
5332 return false;
5333
5334 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5335
5336 create_signatured_type_table_from_debug_names
5337 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5338 }
5339
5340 create_addrmap_from_aranges (dwarf2_per_objfile,
5341 &dwarf2_per_objfile->debug_aranges);
5342
5343 dwarf2_per_objfile->debug_names_table = std::move (map);
5344 dwarf2_per_objfile->using_index = 1;
5345 dwarf2_per_objfile->quick_file_names_table =
5346 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5347
5348 return true;
5349 }
5350
5351 /* Type used to manage iterating over all CUs looking for a symbol for
5352 .debug_names. */
5353
5354 class dw2_debug_names_iterator
5355 {
5356 public:
5357 dw2_debug_names_iterator (const mapped_debug_names &map,
5358 gdb::optional<block_enum> block_index,
5359 domain_enum domain,
5360 const char *name)
5361 : m_map (map), m_block_index (block_index), m_domain (domain),
5362 m_addr (find_vec_in_debug_names (map, name))
5363 {}
5364
5365 dw2_debug_names_iterator (const mapped_debug_names &map,
5366 search_domain search, uint32_t namei)
5367 : m_map (map),
5368 m_search (search),
5369 m_addr (find_vec_in_debug_names (map, namei))
5370 {}
5371
5372 dw2_debug_names_iterator (const mapped_debug_names &map,
5373 block_enum block_index, domain_enum domain,
5374 uint32_t namei)
5375 : m_map (map), m_block_index (block_index), m_domain (domain),
5376 m_addr (find_vec_in_debug_names (map, namei))
5377 {}
5378
5379 /* Return the next matching CU or NULL if there are no more. */
5380 dwarf2_per_cu_data *next ();
5381
5382 private:
5383 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5384 const char *name);
5385 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5386 uint32_t namei);
5387
5388 /* The internalized form of .debug_names. */
5389 const mapped_debug_names &m_map;
5390
5391 /* If set, only look for symbols that match that block. Valid values are
5392 GLOBAL_BLOCK and STATIC_BLOCK. */
5393 const gdb::optional<block_enum> m_block_index;
5394
5395 /* The kind of symbol we're looking for. */
5396 const domain_enum m_domain = UNDEF_DOMAIN;
5397 const search_domain m_search = ALL_DOMAIN;
5398
5399 /* The list of CUs from the index entry of the symbol, or NULL if
5400 not found. */
5401 const gdb_byte *m_addr;
5402 };
5403
5404 const char *
5405 mapped_debug_names::namei_to_name (uint32_t namei) const
5406 {
5407 const ULONGEST namei_string_offs
5408 = extract_unsigned_integer ((name_table_string_offs_reordered
5409 + namei * offset_size),
5410 offset_size,
5411 dwarf5_byte_order);
5412 return read_indirect_string_at_offset
5413 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5414 }
5415
5416 /* Find a slot in .debug_names for the object named NAME. If NAME is
5417 found, return pointer to its pool data. If NAME cannot be found,
5418 return NULL. */
5419
5420 const gdb_byte *
5421 dw2_debug_names_iterator::find_vec_in_debug_names
5422 (const mapped_debug_names &map, const char *name)
5423 {
5424 int (*cmp) (const char *, const char *);
5425
5426 gdb::unique_xmalloc_ptr<char> without_params;
5427 if (current_language->la_language == language_cplus
5428 || current_language->la_language == language_fortran
5429 || current_language->la_language == language_d)
5430 {
5431 /* NAME is already canonical. Drop any qualifiers as
5432 .debug_names does not contain any. */
5433
5434 if (strchr (name, '(') != NULL)
5435 {
5436 without_params = cp_remove_params (name);
5437 if (without_params != NULL)
5438 name = without_params.get ();
5439 }
5440 }
5441
5442 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5443
5444 const uint32_t full_hash = dwarf5_djb_hash (name);
5445 uint32_t namei
5446 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5447 (map.bucket_table_reordered
5448 + (full_hash % map.bucket_count)), 4,
5449 map.dwarf5_byte_order);
5450 if (namei == 0)
5451 return NULL;
5452 --namei;
5453 if (namei >= map.name_count)
5454 {
5455 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5456 "[in module %s]"),
5457 namei, map.name_count,
5458 objfile_name (map.dwarf2_per_objfile->objfile));
5459 return NULL;
5460 }
5461
5462 for (;;)
5463 {
5464 const uint32_t namei_full_hash
5465 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5466 (map.hash_table_reordered + namei), 4,
5467 map.dwarf5_byte_order);
5468 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5469 return NULL;
5470
5471 if (full_hash == namei_full_hash)
5472 {
5473 const char *const namei_string = map.namei_to_name (namei);
5474
5475 #if 0 /* An expensive sanity check. */
5476 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5477 {
5478 complaint (_("Wrong .debug_names hash for string at index %u "
5479 "[in module %s]"),
5480 namei, objfile_name (dwarf2_per_objfile->objfile));
5481 return NULL;
5482 }
5483 #endif
5484
5485 if (cmp (namei_string, name) == 0)
5486 {
5487 const ULONGEST namei_entry_offs
5488 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5489 + namei * map.offset_size),
5490 map.offset_size, map.dwarf5_byte_order);
5491 return map.entry_pool + namei_entry_offs;
5492 }
5493 }
5494
5495 ++namei;
5496 if (namei >= map.name_count)
5497 return NULL;
5498 }
5499 }
5500
5501 const gdb_byte *
5502 dw2_debug_names_iterator::find_vec_in_debug_names
5503 (const mapped_debug_names &map, uint32_t namei)
5504 {
5505 if (namei >= map.name_count)
5506 {
5507 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5508 "[in module %s]"),
5509 namei, map.name_count,
5510 objfile_name (map.dwarf2_per_objfile->objfile));
5511 return NULL;
5512 }
5513
5514 const ULONGEST namei_entry_offs
5515 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5516 + namei * map.offset_size),
5517 map.offset_size, map.dwarf5_byte_order);
5518 return map.entry_pool + namei_entry_offs;
5519 }
5520
5521 /* See dw2_debug_names_iterator. */
5522
5523 dwarf2_per_cu_data *
5524 dw2_debug_names_iterator::next ()
5525 {
5526 if (m_addr == NULL)
5527 return NULL;
5528
5529 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5530 struct objfile *objfile = dwarf2_per_objfile->objfile;
5531 bfd *const abfd = objfile->obfd;
5532
5533 again:
5534
5535 unsigned int bytes_read;
5536 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5537 m_addr += bytes_read;
5538 if (abbrev == 0)
5539 return NULL;
5540
5541 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5542 if (indexval_it == m_map.abbrev_map.cend ())
5543 {
5544 complaint (_("Wrong .debug_names undefined abbrev code %s "
5545 "[in module %s]"),
5546 pulongest (abbrev), objfile_name (objfile));
5547 return NULL;
5548 }
5549 const mapped_debug_names::index_val &indexval = indexval_it->second;
5550 enum class symbol_linkage {
5551 unknown,
5552 static_,
5553 extern_,
5554 } symbol_linkage_ = symbol_linkage::unknown;
5555 dwarf2_per_cu_data *per_cu = NULL;
5556 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5557 {
5558 ULONGEST ull;
5559 switch (attr.form)
5560 {
5561 case DW_FORM_implicit_const:
5562 ull = attr.implicit_const;
5563 break;
5564 case DW_FORM_flag_present:
5565 ull = 1;
5566 break;
5567 case DW_FORM_udata:
5568 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5569 m_addr += bytes_read;
5570 break;
5571 default:
5572 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5573 dwarf_form_name (attr.form),
5574 objfile_name (objfile));
5575 return NULL;
5576 }
5577 switch (attr.dw_idx)
5578 {
5579 case DW_IDX_compile_unit:
5580 /* Don't crash on bad data. */
5581 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5582 {
5583 complaint (_(".debug_names entry has bad CU index %s"
5584 " [in module %s]"),
5585 pulongest (ull),
5586 objfile_name (dwarf2_per_objfile->objfile));
5587 continue;
5588 }
5589 per_cu = dwarf2_per_objfile->get_cutu (ull);
5590 break;
5591 case DW_IDX_type_unit:
5592 /* Don't crash on bad data. */
5593 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5594 {
5595 complaint (_(".debug_names entry has bad TU index %s"
5596 " [in module %s]"),
5597 pulongest (ull),
5598 objfile_name (dwarf2_per_objfile->objfile));
5599 continue;
5600 }
5601 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5602 break;
5603 case DW_IDX_GNU_internal:
5604 if (!m_map.augmentation_is_gdb)
5605 break;
5606 symbol_linkage_ = symbol_linkage::static_;
5607 break;
5608 case DW_IDX_GNU_external:
5609 if (!m_map.augmentation_is_gdb)
5610 break;
5611 symbol_linkage_ = symbol_linkage::extern_;
5612 break;
5613 }
5614 }
5615
5616 /* Skip if already read in. */
5617 if (per_cu->v.quick->compunit_symtab)
5618 goto again;
5619
5620 /* Check static vs global. */
5621 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5622 {
5623 const bool want_static = *m_block_index == STATIC_BLOCK;
5624 const bool symbol_is_static =
5625 symbol_linkage_ == symbol_linkage::static_;
5626 if (want_static != symbol_is_static)
5627 goto again;
5628 }
5629
5630 /* Match dw2_symtab_iter_next, symbol_kind
5631 and debug_names::psymbol_tag. */
5632 switch (m_domain)
5633 {
5634 case VAR_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_variable:
5638 case DW_TAG_subprogram:
5639 /* Some types are also in VAR_DOMAIN. */
5640 case DW_TAG_typedef:
5641 case DW_TAG_structure_type:
5642 break;
5643 default:
5644 goto again;
5645 }
5646 break;
5647 case STRUCT_DOMAIN:
5648 switch (indexval.dwarf_tag)
5649 {
5650 case DW_TAG_typedef:
5651 case DW_TAG_structure_type:
5652 break;
5653 default:
5654 goto again;
5655 }
5656 break;
5657 case LABEL_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case 0:
5661 case DW_TAG_variable:
5662 break;
5663 default:
5664 goto again;
5665 }
5666 break;
5667 case MODULE_DOMAIN:
5668 switch (indexval.dwarf_tag)
5669 {
5670 case DW_TAG_module:
5671 break;
5672 default:
5673 goto again;
5674 }
5675 break;
5676 default:
5677 break;
5678 }
5679
5680 /* Match dw2_expand_symtabs_matching, symbol_kind and
5681 debug_names::psymbol_tag. */
5682 switch (m_search)
5683 {
5684 case VARIABLES_DOMAIN:
5685 switch (indexval.dwarf_tag)
5686 {
5687 case DW_TAG_variable:
5688 break;
5689 default:
5690 goto again;
5691 }
5692 break;
5693 case FUNCTIONS_DOMAIN:
5694 switch (indexval.dwarf_tag)
5695 {
5696 case DW_TAG_subprogram:
5697 break;
5698 default:
5699 goto again;
5700 }
5701 break;
5702 case TYPES_DOMAIN:
5703 switch (indexval.dwarf_tag)
5704 {
5705 case DW_TAG_typedef:
5706 case DW_TAG_structure_type:
5707 break;
5708 default:
5709 goto again;
5710 }
5711 break;
5712 case MODULES_DOMAIN:
5713 switch (indexval.dwarf_tag)
5714 {
5715 case DW_TAG_module:
5716 break;
5717 default:
5718 goto again;
5719 }
5720 default:
5721 break;
5722 }
5723
5724 return per_cu;
5725 }
5726
5727 static struct compunit_symtab *
5728 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5729 const char *name, domain_enum domain)
5730 {
5731 struct dwarf2_per_objfile *dwarf2_per_objfile
5732 = get_dwarf2_per_objfile (objfile);
5733
5734 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5735 if (!mapp)
5736 {
5737 /* index is NULL if OBJF_READNOW. */
5738 return NULL;
5739 }
5740 const auto &map = *mapp;
5741
5742 dw2_debug_names_iterator iter (map, block_index, domain, name);
5743
5744 struct compunit_symtab *stab_best = NULL;
5745 struct dwarf2_per_cu_data *per_cu;
5746 while ((per_cu = iter.next ()) != NULL)
5747 {
5748 struct symbol *sym, *with_opaque = NULL;
5749 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5750 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5751 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5752
5753 sym = block_find_symbol (block, name, domain,
5754 block_find_non_opaque_type_preferred,
5755 &with_opaque);
5756
5757 /* Some caution must be observed with overloaded functions and
5758 methods, since the index will not contain any overload
5759 information (but NAME might contain it). */
5760
5761 if (sym != NULL
5762 && strcmp_iw (sym->search_name (), name) == 0)
5763 return stab;
5764 if (with_opaque != NULL
5765 && strcmp_iw (with_opaque->search_name (), name) == 0)
5766 stab_best = stab;
5767
5768 /* Keep looking through other CUs. */
5769 }
5770
5771 return stab_best;
5772 }
5773
5774 /* This dumps minimal information about .debug_names. It is called
5775 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5776 uses this to verify that .debug_names has been loaded. */
5777
5778 static void
5779 dw2_debug_names_dump (struct objfile *objfile)
5780 {
5781 struct dwarf2_per_objfile *dwarf2_per_objfile
5782 = get_dwarf2_per_objfile (objfile);
5783
5784 gdb_assert (dwarf2_per_objfile->using_index);
5785 printf_filtered (".debug_names:");
5786 if (dwarf2_per_objfile->debug_names_table)
5787 printf_filtered (" exists\n");
5788 else
5789 printf_filtered (" faked for \"readnow\"\n");
5790 printf_filtered ("\n");
5791 }
5792
5793 static void
5794 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5795 const char *func_name)
5796 {
5797 struct dwarf2_per_objfile *dwarf2_per_objfile
5798 = get_dwarf2_per_objfile (objfile);
5799
5800 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5801 if (dwarf2_per_objfile->debug_names_table)
5802 {
5803 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5804
5805 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5806
5807 struct dwarf2_per_cu_data *per_cu;
5808 while ((per_cu = iter.next ()) != NULL)
5809 dw2_instantiate_symtab (per_cu, false);
5810 }
5811 }
5812
5813 static void
5814 dw2_debug_names_map_matching_symbols
5815 (struct objfile *objfile,
5816 const lookup_name_info &name, domain_enum domain,
5817 int global,
5818 gdb::function_view<symbol_found_callback_ftype> callback,
5819 symbol_compare_ftype *ordered_compare)
5820 {
5821 struct dwarf2_per_objfile *dwarf2_per_objfile
5822 = get_dwarf2_per_objfile (objfile);
5823
5824 /* debug_names_table is NULL if OBJF_READNOW. */
5825 if (!dwarf2_per_objfile->debug_names_table)
5826 return;
5827
5828 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5829 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5830
5831 const char *match_name = name.ada ().lookup_name ().c_str ();
5832 auto matcher = [&] (const char *symname)
5833 {
5834 if (ordered_compare == nullptr)
5835 return true;
5836 return ordered_compare (symname, match_name) == 0;
5837 };
5838
5839 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5840 [&] (offset_type namei)
5841 {
5842 /* The name was matched, now expand corresponding CUs that were
5843 marked. */
5844 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5845
5846 struct dwarf2_per_cu_data *per_cu;
5847 while ((per_cu = iter.next ()) != NULL)
5848 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5849 return true;
5850 });
5851
5852 /* It's a shame we couldn't do this inside the
5853 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5854 that have already been expanded. Instead, this loop matches what
5855 the psymtab code does. */
5856 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5857 {
5858 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5859 if (cust != nullptr)
5860 {
5861 const struct block *block
5862 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5863 if (!iterate_over_symbols_terminated (block, name,
5864 domain, callback))
5865 break;
5866 }
5867 }
5868 }
5869
5870 static void
5871 dw2_debug_names_expand_symtabs_matching
5872 (struct objfile *objfile,
5873 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5874 const lookup_name_info &lookup_name,
5875 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5876 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5877 enum search_domain kind)
5878 {
5879 struct dwarf2_per_objfile *dwarf2_per_objfile
5880 = get_dwarf2_per_objfile (objfile);
5881
5882 /* debug_names_table is NULL if OBJF_READNOW. */
5883 if (!dwarf2_per_objfile->debug_names_table)
5884 return;
5885
5886 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5887
5888 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5889
5890 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5891 symbol_matcher,
5892 kind, [&] (offset_type namei)
5893 {
5894 /* The name was matched, now expand corresponding CUs that were
5895 marked. */
5896 dw2_debug_names_iterator iter (map, kind, namei);
5897
5898 struct dwarf2_per_cu_data *per_cu;
5899 while ((per_cu = iter.next ()) != NULL)
5900 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5901 expansion_notify);
5902 return true;
5903 });
5904 }
5905
5906 const struct quick_symbol_functions dwarf2_debug_names_functions =
5907 {
5908 dw2_has_symbols,
5909 dw2_find_last_source_symtab,
5910 dw2_forget_cached_source_info,
5911 dw2_map_symtabs_matching_filename,
5912 dw2_debug_names_lookup_symbol,
5913 dw2_print_stats,
5914 dw2_debug_names_dump,
5915 dw2_debug_names_expand_symtabs_for_function,
5916 dw2_expand_all_symtabs,
5917 dw2_expand_symtabs_with_fullname,
5918 dw2_debug_names_map_matching_symbols,
5919 dw2_debug_names_expand_symtabs_matching,
5920 dw2_find_pc_sect_compunit_symtab,
5921 NULL,
5922 dw2_map_symbol_filenames
5923 };
5924
5925 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5926 to either a dwarf2_per_objfile or dwz_file object. */
5927
5928 template <typename T>
5929 static gdb::array_view<const gdb_byte>
5930 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5931 {
5932 dwarf2_section_info *section = &section_owner->gdb_index;
5933
5934 if (section->empty ())
5935 return {};
5936
5937 /* Older elfutils strip versions could keep the section in the main
5938 executable while splitting it for the separate debug info file. */
5939 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5940 return {};
5941
5942 section->read (obj);
5943
5944 /* dwarf2_section_info::size is a bfd_size_type, while
5945 gdb::array_view works with size_t. On 32-bit hosts, with
5946 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5947 is 32-bit. So we need an explicit narrowing conversion here.
5948 This is fine, because it's impossible to allocate or mmap an
5949 array/buffer larger than what size_t can represent. */
5950 return gdb::make_array_view (section->buffer, section->size);
5951 }
5952
5953 /* Lookup the index cache for the contents of the index associated to
5954 DWARF2_OBJ. */
5955
5956 static gdb::array_view<const gdb_byte>
5957 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5958 {
5959 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5960 if (build_id == nullptr)
5961 return {};
5962
5963 return global_index_cache.lookup_gdb_index (build_id,
5964 &dwarf2_obj->index_cache_res);
5965 }
5966
5967 /* Same as the above, but for DWZ. */
5968
5969 static gdb::array_view<const gdb_byte>
5970 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5971 {
5972 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5973 if (build_id == nullptr)
5974 return {};
5975
5976 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5977 }
5978
5979 /* See symfile.h. */
5980
5981 bool
5982 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5983 {
5984 struct dwarf2_per_objfile *dwarf2_per_objfile
5985 = get_dwarf2_per_objfile (objfile);
5986
5987 /* If we're about to read full symbols, don't bother with the
5988 indices. In this case we also don't care if some other debug
5989 format is making psymtabs, because they are all about to be
5990 expanded anyway. */
5991 if ((objfile->flags & OBJF_READNOW))
5992 {
5993 dwarf2_per_objfile->using_index = 1;
5994 create_all_comp_units (dwarf2_per_objfile);
5995 create_all_type_units (dwarf2_per_objfile);
5996 dwarf2_per_objfile->quick_file_names_table
5997 = create_quick_file_names_table
5998 (dwarf2_per_objfile->all_comp_units.size ());
5999
6000 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6001 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6002 {
6003 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6004
6005 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6006 struct dwarf2_per_cu_quick_data);
6007 }
6008
6009 /* Return 1 so that gdb sees the "quick" functions. However,
6010 these functions will be no-ops because we will have expanded
6011 all symtabs. */
6012 *index_kind = dw_index_kind::GDB_INDEX;
6013 return true;
6014 }
6015
6016 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6017 {
6018 *index_kind = dw_index_kind::DEBUG_NAMES;
6019 return true;
6020 }
6021
6022 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6023 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6024 get_gdb_index_contents_from_section<dwz_file>))
6025 {
6026 *index_kind = dw_index_kind::GDB_INDEX;
6027 return true;
6028 }
6029
6030 /* ... otherwise, try to find the index in the index cache. */
6031 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6032 get_gdb_index_contents_from_cache,
6033 get_gdb_index_contents_from_cache_dwz))
6034 {
6035 global_index_cache.hit ();
6036 *index_kind = dw_index_kind::GDB_INDEX;
6037 return true;
6038 }
6039
6040 global_index_cache.miss ();
6041 return false;
6042 }
6043
6044 \f
6045
6046 /* Build a partial symbol table. */
6047
6048 void
6049 dwarf2_build_psymtabs (struct objfile *objfile)
6050 {
6051 struct dwarf2_per_objfile *dwarf2_per_objfile
6052 = get_dwarf2_per_objfile (objfile);
6053
6054 init_psymbol_list (objfile, 1024);
6055
6056 try
6057 {
6058 /* This isn't really ideal: all the data we allocate on the
6059 objfile's obstack is still uselessly kept around. However,
6060 freeing it seems unsafe. */
6061 psymtab_discarder psymtabs (objfile);
6062 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6063 psymtabs.keep ();
6064
6065 /* (maybe) store an index in the cache. */
6066 global_index_cache.store (dwarf2_per_objfile);
6067 }
6068 catch (const gdb_exception_error &except)
6069 {
6070 exception_print (gdb_stderr, except);
6071 }
6072 }
6073
6074 /* Return the total length of the CU described by HEADER. */
6075
6076 static unsigned int
6077 get_cu_length (const struct comp_unit_head *header)
6078 {
6079 return header->initial_length_size + header->length;
6080 }
6081
6082 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6083
6084 static inline bool
6085 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6086 {
6087 sect_offset bottom = cu_header->sect_off;
6088 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6089
6090 return sect_off >= bottom && sect_off < top;
6091 }
6092
6093 /* Find the base address of the compilation unit for range lists and
6094 location lists. It will normally be specified by DW_AT_low_pc.
6095 In DWARF-3 draft 4, the base address could be overridden by
6096 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6097 compilation units with discontinuous ranges. */
6098
6099 static void
6100 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6101 {
6102 struct attribute *attr;
6103
6104 cu->base_known = 0;
6105 cu->base_address = 0;
6106
6107 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6108 if (attr != nullptr)
6109 {
6110 cu->base_address = attr->value_as_address ();
6111 cu->base_known = 1;
6112 }
6113 else
6114 {
6115 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6116 if (attr != nullptr)
6117 {
6118 cu->base_address = attr->value_as_address ();
6119 cu->base_known = 1;
6120 }
6121 }
6122 }
6123
6124 /* Read in the comp unit header information from the debug_info at info_ptr.
6125 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6126 NOTE: This leaves members offset, first_die_offset to be filled in
6127 by the caller. */
6128
6129 static const gdb_byte *
6130 read_comp_unit_head (struct comp_unit_head *cu_header,
6131 const gdb_byte *info_ptr,
6132 struct dwarf2_section_info *section,
6133 rcuh_kind section_kind)
6134 {
6135 int signed_addr;
6136 unsigned int bytes_read;
6137 const char *filename = section->get_file_name ();
6138 bfd *abfd = section->get_bfd_owner ();
6139
6140 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6141 cu_header->initial_length_size = bytes_read;
6142 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6143 info_ptr += bytes_read;
6144 cu_header->version = read_2_bytes (abfd, info_ptr);
6145 if (cu_header->version < 2 || cu_header->version > 5)
6146 error (_("Dwarf Error: wrong version in compilation unit header "
6147 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6148 cu_header->version, filename);
6149 info_ptr += 2;
6150 if (cu_header->version < 5)
6151 switch (section_kind)
6152 {
6153 case rcuh_kind::COMPILE:
6154 cu_header->unit_type = DW_UT_compile;
6155 break;
6156 case rcuh_kind::TYPE:
6157 cu_header->unit_type = DW_UT_type;
6158 break;
6159 default:
6160 internal_error (__FILE__, __LINE__,
6161 _("read_comp_unit_head: invalid section_kind"));
6162 }
6163 else
6164 {
6165 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6166 (read_1_byte (abfd, info_ptr));
6167 info_ptr += 1;
6168 switch (cu_header->unit_type)
6169 {
6170 case DW_UT_compile:
6171 case DW_UT_partial:
6172 case DW_UT_skeleton:
6173 case DW_UT_split_compile:
6174 if (section_kind != rcuh_kind::COMPILE)
6175 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6176 "(is %s, should be %s) [in module %s]"),
6177 dwarf_unit_type_name (cu_header->unit_type),
6178 dwarf_unit_type_name (DW_UT_type), filename);
6179 break;
6180 case DW_UT_type:
6181 case DW_UT_split_type:
6182 section_kind = rcuh_kind::TYPE;
6183 break;
6184 default:
6185 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6186 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6187 "[in module %s]"), cu_header->unit_type,
6188 dwarf_unit_type_name (DW_UT_compile),
6189 dwarf_unit_type_name (DW_UT_skeleton),
6190 dwarf_unit_type_name (DW_UT_split_compile),
6191 dwarf_unit_type_name (DW_UT_type),
6192 dwarf_unit_type_name (DW_UT_split_type), filename);
6193 }
6194
6195 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6196 info_ptr += 1;
6197 }
6198 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6199 cu_header,
6200 &bytes_read);
6201 info_ptr += bytes_read;
6202 if (cu_header->version < 5)
6203 {
6204 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6205 info_ptr += 1;
6206 }
6207 signed_addr = bfd_get_sign_extend_vma (abfd);
6208 if (signed_addr < 0)
6209 internal_error (__FILE__, __LINE__,
6210 _("read_comp_unit_head: dwarf from non elf file"));
6211 cu_header->signed_addr_p = signed_addr;
6212
6213 bool header_has_signature = section_kind == rcuh_kind::TYPE
6214 || cu_header->unit_type == DW_UT_skeleton
6215 || cu_header->unit_type == DW_UT_split_compile;
6216
6217 if (header_has_signature)
6218 {
6219 cu_header->signature = read_8_bytes (abfd, info_ptr);
6220 info_ptr += 8;
6221 }
6222
6223 if (section_kind == rcuh_kind::TYPE)
6224 {
6225 LONGEST type_offset;
6226 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6227 info_ptr += bytes_read;
6228 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6229 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6230 error (_("Dwarf Error: Too big type_offset in compilation unit "
6231 "header (is %s) [in module %s]"), plongest (type_offset),
6232 filename);
6233 }
6234
6235 return info_ptr;
6236 }
6237
6238 /* Helper function that returns the proper abbrev section for
6239 THIS_CU. */
6240
6241 static struct dwarf2_section_info *
6242 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6243 {
6244 struct dwarf2_section_info *abbrev;
6245 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6246
6247 if (this_cu->is_dwz)
6248 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6249 else
6250 abbrev = &dwarf2_per_objfile->abbrev;
6251
6252 return abbrev;
6253 }
6254
6255 /* Subroutine of read_and_check_comp_unit_head and
6256 read_and_check_type_unit_head to simplify them.
6257 Perform various error checking on the header. */
6258
6259 static void
6260 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6261 struct comp_unit_head *header,
6262 struct dwarf2_section_info *section,
6263 struct dwarf2_section_info *abbrev_section)
6264 {
6265 const char *filename = section->get_file_name ();
6266
6267 if (to_underlying (header->abbrev_sect_off)
6268 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6269 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6270 "(offset %s + 6) [in module %s]"),
6271 sect_offset_str (header->abbrev_sect_off),
6272 sect_offset_str (header->sect_off),
6273 filename);
6274
6275 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6276 avoid potential 32-bit overflow. */
6277 if (((ULONGEST) header->sect_off + get_cu_length (header))
6278 > section->size)
6279 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6280 "(offset %s + 0) [in module %s]"),
6281 header->length, sect_offset_str (header->sect_off),
6282 filename);
6283 }
6284
6285 /* Read in a CU/TU header and perform some basic error checking.
6286 The contents of the header are stored in HEADER.
6287 The result is a pointer to the start of the first DIE. */
6288
6289 static const gdb_byte *
6290 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6291 struct comp_unit_head *header,
6292 struct dwarf2_section_info *section,
6293 struct dwarf2_section_info *abbrev_section,
6294 const gdb_byte *info_ptr,
6295 rcuh_kind section_kind)
6296 {
6297 const gdb_byte *beg_of_comp_unit = info_ptr;
6298
6299 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6300
6301 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6302
6303 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6304
6305 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6306 abbrev_section);
6307
6308 return info_ptr;
6309 }
6310
6311 /* Fetch the abbreviation table offset from a comp or type unit header. */
6312
6313 static sect_offset
6314 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6315 struct dwarf2_section_info *section,
6316 sect_offset sect_off)
6317 {
6318 bfd *abfd = section->get_bfd_owner ();
6319 const gdb_byte *info_ptr;
6320 unsigned int initial_length_size, offset_size;
6321 uint16_t version;
6322
6323 section->read (dwarf2_per_objfile->objfile);
6324 info_ptr = section->buffer + to_underlying (sect_off);
6325 read_initial_length (abfd, info_ptr, &initial_length_size);
6326 offset_size = initial_length_size == 4 ? 4 : 8;
6327 info_ptr += initial_length_size;
6328
6329 version = read_2_bytes (abfd, info_ptr);
6330 info_ptr += 2;
6331 if (version >= 5)
6332 {
6333 /* Skip unit type and address size. */
6334 info_ptr += 2;
6335 }
6336
6337 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6338 }
6339
6340 /* Allocate a new partial symtab for file named NAME and mark this new
6341 partial symtab as being an include of PST. */
6342
6343 static void
6344 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6345 struct objfile *objfile)
6346 {
6347 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6348
6349 if (!IS_ABSOLUTE_PATH (subpst->filename))
6350 {
6351 /* It shares objfile->objfile_obstack. */
6352 subpst->dirname = pst->dirname;
6353 }
6354
6355 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6356 subpst->dependencies[0] = pst;
6357 subpst->number_of_dependencies = 1;
6358
6359 /* No private part is necessary for include psymtabs. This property
6360 can be used to differentiate between such include psymtabs and
6361 the regular ones. */
6362 subpst->per_cu_data = nullptr;
6363 }
6364
6365 /* Read the Line Number Program data and extract the list of files
6366 included by the source file represented by PST. Build an include
6367 partial symtab for each of these included files. */
6368
6369 static void
6370 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6371 struct die_info *die,
6372 dwarf2_psymtab *pst)
6373 {
6374 line_header_up lh;
6375 struct attribute *attr;
6376
6377 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6378 if (attr != nullptr)
6379 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6380 if (lh == NULL)
6381 return; /* No linetable, so no includes. */
6382
6383 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6384 that we pass in the raw text_low here; that is ok because we're
6385 only decoding the line table to make include partial symtabs, and
6386 so the addresses aren't really used. */
6387 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6388 pst->raw_text_low (), 1);
6389 }
6390
6391 static hashval_t
6392 hash_signatured_type (const void *item)
6393 {
6394 const struct signatured_type *sig_type
6395 = (const struct signatured_type *) item;
6396
6397 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6398 return sig_type->signature;
6399 }
6400
6401 static int
6402 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6403 {
6404 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6405 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6406
6407 return lhs->signature == rhs->signature;
6408 }
6409
6410 /* Allocate a hash table for signatured types. */
6411
6412 static htab_t
6413 allocate_signatured_type_table (struct objfile *objfile)
6414 {
6415 return htab_create_alloc_ex (41,
6416 hash_signatured_type,
6417 eq_signatured_type,
6418 NULL,
6419 &objfile->objfile_obstack,
6420 hashtab_obstack_allocate,
6421 dummy_obstack_deallocate);
6422 }
6423
6424 /* A helper function to add a signatured type CU to a table. */
6425
6426 static int
6427 add_signatured_type_cu_to_table (void **slot, void *datum)
6428 {
6429 struct signatured_type *sigt = (struct signatured_type *) *slot;
6430 std::vector<signatured_type *> *all_type_units
6431 = (std::vector<signatured_type *> *) datum;
6432
6433 all_type_units->push_back (sigt);
6434
6435 return 1;
6436 }
6437
6438 /* A helper for create_debug_types_hash_table. Read types from SECTION
6439 and fill them into TYPES_HTAB. It will process only type units,
6440 therefore DW_UT_type. */
6441
6442 static void
6443 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6444 struct dwo_file *dwo_file,
6445 dwarf2_section_info *section, htab_t &types_htab,
6446 rcuh_kind section_kind)
6447 {
6448 struct objfile *objfile = dwarf2_per_objfile->objfile;
6449 struct dwarf2_section_info *abbrev_section;
6450 bfd *abfd;
6451 const gdb_byte *info_ptr, *end_ptr;
6452
6453 abbrev_section = (dwo_file != NULL
6454 ? &dwo_file->sections.abbrev
6455 : &dwarf2_per_objfile->abbrev);
6456
6457 if (dwarf_read_debug)
6458 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6459 section->get_name (),
6460 abbrev_section->get_file_name ());
6461
6462 section->read (objfile);
6463 info_ptr = section->buffer;
6464
6465 if (info_ptr == NULL)
6466 return;
6467
6468 /* We can't set abfd until now because the section may be empty or
6469 not present, in which case the bfd is unknown. */
6470 abfd = section->get_bfd_owner ();
6471
6472 /* We don't use cutu_reader here because we don't need to read
6473 any dies: the signature is in the header. */
6474
6475 end_ptr = info_ptr + section->size;
6476 while (info_ptr < end_ptr)
6477 {
6478 struct signatured_type *sig_type;
6479 struct dwo_unit *dwo_tu;
6480 void **slot;
6481 const gdb_byte *ptr = info_ptr;
6482 struct comp_unit_head header;
6483 unsigned int length;
6484
6485 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6486
6487 /* Initialize it due to a false compiler warning. */
6488 header.signature = -1;
6489 header.type_cu_offset_in_tu = (cu_offset) -1;
6490
6491 /* We need to read the type's signature in order to build the hash
6492 table, but we don't need anything else just yet. */
6493
6494 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6495 abbrev_section, ptr, section_kind);
6496
6497 length = get_cu_length (&header);
6498
6499 /* Skip dummy type units. */
6500 if (ptr >= info_ptr + length
6501 || peek_abbrev_code (abfd, ptr) == 0
6502 || header.unit_type != DW_UT_type)
6503 {
6504 info_ptr += length;
6505 continue;
6506 }
6507
6508 if (types_htab == NULL)
6509 {
6510 if (dwo_file)
6511 types_htab = allocate_dwo_unit_table (objfile);
6512 else
6513 types_htab = allocate_signatured_type_table (objfile);
6514 }
6515
6516 if (dwo_file)
6517 {
6518 sig_type = NULL;
6519 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6520 struct dwo_unit);
6521 dwo_tu->dwo_file = dwo_file;
6522 dwo_tu->signature = header.signature;
6523 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6524 dwo_tu->section = section;
6525 dwo_tu->sect_off = sect_off;
6526 dwo_tu->length = length;
6527 }
6528 else
6529 {
6530 /* N.B.: type_offset is not usable if this type uses a DWO file.
6531 The real type_offset is in the DWO file. */
6532 dwo_tu = NULL;
6533 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6534 struct signatured_type);
6535 sig_type->signature = header.signature;
6536 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6537 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6538 sig_type->per_cu.is_debug_types = 1;
6539 sig_type->per_cu.section = section;
6540 sig_type->per_cu.sect_off = sect_off;
6541 sig_type->per_cu.length = length;
6542 }
6543
6544 slot = htab_find_slot (types_htab,
6545 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6546 INSERT);
6547 gdb_assert (slot != NULL);
6548 if (*slot != NULL)
6549 {
6550 sect_offset dup_sect_off;
6551
6552 if (dwo_file)
6553 {
6554 const struct dwo_unit *dup_tu
6555 = (const struct dwo_unit *) *slot;
6556
6557 dup_sect_off = dup_tu->sect_off;
6558 }
6559 else
6560 {
6561 const struct signatured_type *dup_tu
6562 = (const struct signatured_type *) *slot;
6563
6564 dup_sect_off = dup_tu->per_cu.sect_off;
6565 }
6566
6567 complaint (_("debug type entry at offset %s is duplicate to"
6568 " the entry at offset %s, signature %s"),
6569 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6570 hex_string (header.signature));
6571 }
6572 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6573
6574 if (dwarf_read_debug > 1)
6575 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6576 sect_offset_str (sect_off),
6577 hex_string (header.signature));
6578
6579 info_ptr += length;
6580 }
6581 }
6582
6583 /* Create the hash table of all entries in the .debug_types
6584 (or .debug_types.dwo) section(s).
6585 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6586 otherwise it is NULL.
6587
6588 The result is a pointer to the hash table or NULL if there are no types.
6589
6590 Note: This function processes DWO files only, not DWP files. */
6591
6592 static void
6593 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6594 struct dwo_file *dwo_file,
6595 gdb::array_view<dwarf2_section_info> type_sections,
6596 htab_t &types_htab)
6597 {
6598 for (dwarf2_section_info &section : type_sections)
6599 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6600 types_htab, rcuh_kind::TYPE);
6601 }
6602
6603 /* Create the hash table of all entries in the .debug_types section,
6604 and initialize all_type_units.
6605 The result is zero if there is an error (e.g. missing .debug_types section),
6606 otherwise non-zero. */
6607
6608 static int
6609 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6610 {
6611 htab_t types_htab = NULL;
6612
6613 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6614 &dwarf2_per_objfile->info, types_htab,
6615 rcuh_kind::COMPILE);
6616 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6617 dwarf2_per_objfile->types, types_htab);
6618 if (types_htab == NULL)
6619 {
6620 dwarf2_per_objfile->signatured_types = NULL;
6621 return 0;
6622 }
6623
6624 dwarf2_per_objfile->signatured_types = types_htab;
6625
6626 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6627 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6628
6629 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6630 &dwarf2_per_objfile->all_type_units);
6631
6632 return 1;
6633 }
6634
6635 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6636 If SLOT is non-NULL, it is the entry to use in the hash table.
6637 Otherwise we find one. */
6638
6639 static struct signatured_type *
6640 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6641 void **slot)
6642 {
6643 struct objfile *objfile = dwarf2_per_objfile->objfile;
6644
6645 if (dwarf2_per_objfile->all_type_units.size ()
6646 == dwarf2_per_objfile->all_type_units.capacity ())
6647 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6648
6649 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6650 struct signatured_type);
6651
6652 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6653 sig_type->signature = sig;
6654 sig_type->per_cu.is_debug_types = 1;
6655 if (dwarf2_per_objfile->using_index)
6656 {
6657 sig_type->per_cu.v.quick =
6658 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6659 struct dwarf2_per_cu_quick_data);
6660 }
6661
6662 if (slot == NULL)
6663 {
6664 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6665 sig_type, INSERT);
6666 }
6667 gdb_assert (*slot == NULL);
6668 *slot = sig_type;
6669 /* The rest of sig_type must be filled in by the caller. */
6670 return sig_type;
6671 }
6672
6673 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6674 Fill in SIG_ENTRY with DWO_ENTRY. */
6675
6676 static void
6677 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6678 struct signatured_type *sig_entry,
6679 struct dwo_unit *dwo_entry)
6680 {
6681 /* Make sure we're not clobbering something we don't expect to. */
6682 gdb_assert (! sig_entry->per_cu.queued);
6683 gdb_assert (sig_entry->per_cu.cu == NULL);
6684 if (dwarf2_per_objfile->using_index)
6685 {
6686 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6687 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6688 }
6689 else
6690 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6691 gdb_assert (sig_entry->signature == dwo_entry->signature);
6692 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6693 gdb_assert (sig_entry->type_unit_group == NULL);
6694 gdb_assert (sig_entry->dwo_unit == NULL);
6695
6696 sig_entry->per_cu.section = dwo_entry->section;
6697 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6698 sig_entry->per_cu.length = dwo_entry->length;
6699 sig_entry->per_cu.reading_dwo_directly = 1;
6700 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6701 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6702 sig_entry->dwo_unit = dwo_entry;
6703 }
6704
6705 /* Subroutine of lookup_signatured_type.
6706 If we haven't read the TU yet, create the signatured_type data structure
6707 for a TU to be read in directly from a DWO file, bypassing the stub.
6708 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6709 using .gdb_index, then when reading a CU we want to stay in the DWO file
6710 containing that CU. Otherwise we could end up reading several other DWO
6711 files (due to comdat folding) to process the transitive closure of all the
6712 mentioned TUs, and that can be slow. The current DWO file will have every
6713 type signature that it needs.
6714 We only do this for .gdb_index because in the psymtab case we already have
6715 to read all the DWOs to build the type unit groups. */
6716
6717 static struct signatured_type *
6718 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6719 {
6720 struct dwarf2_per_objfile *dwarf2_per_objfile
6721 = cu->per_cu->dwarf2_per_objfile;
6722 struct objfile *objfile = dwarf2_per_objfile->objfile;
6723 struct dwo_file *dwo_file;
6724 struct dwo_unit find_dwo_entry, *dwo_entry;
6725 struct signatured_type find_sig_entry, *sig_entry;
6726 void **slot;
6727
6728 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6729
6730 /* If TU skeletons have been removed then we may not have read in any
6731 TUs yet. */
6732 if (dwarf2_per_objfile->signatured_types == NULL)
6733 {
6734 dwarf2_per_objfile->signatured_types
6735 = allocate_signatured_type_table (objfile);
6736 }
6737
6738 /* We only ever need to read in one copy of a signatured type.
6739 Use the global signatured_types array to do our own comdat-folding
6740 of types. If this is the first time we're reading this TU, and
6741 the TU has an entry in .gdb_index, replace the recorded data from
6742 .gdb_index with this TU. */
6743
6744 find_sig_entry.signature = sig;
6745 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6746 &find_sig_entry, INSERT);
6747 sig_entry = (struct signatured_type *) *slot;
6748
6749 /* We can get here with the TU already read, *or* in the process of being
6750 read. Don't reassign the global entry to point to this DWO if that's
6751 the case. Also note that if the TU is already being read, it may not
6752 have come from a DWO, the program may be a mix of Fission-compiled
6753 code and non-Fission-compiled code. */
6754
6755 /* Have we already tried to read this TU?
6756 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6757 needn't exist in the global table yet). */
6758 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6759 return sig_entry;
6760
6761 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6762 dwo_unit of the TU itself. */
6763 dwo_file = cu->dwo_unit->dwo_file;
6764
6765 /* Ok, this is the first time we're reading this TU. */
6766 if (dwo_file->tus == NULL)
6767 return NULL;
6768 find_dwo_entry.signature = sig;
6769 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6770 if (dwo_entry == NULL)
6771 return NULL;
6772
6773 /* If the global table doesn't have an entry for this TU, add one. */
6774 if (sig_entry == NULL)
6775 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6776
6777 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6778 sig_entry->per_cu.tu_read = 1;
6779 return sig_entry;
6780 }
6781
6782 /* Subroutine of lookup_signatured_type.
6783 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6784 then try the DWP file. If the TU stub (skeleton) has been removed then
6785 it won't be in .gdb_index. */
6786
6787 static struct signatured_type *
6788 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6789 {
6790 struct dwarf2_per_objfile *dwarf2_per_objfile
6791 = cu->per_cu->dwarf2_per_objfile;
6792 struct objfile *objfile = dwarf2_per_objfile->objfile;
6793 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6794 struct dwo_unit *dwo_entry;
6795 struct signatured_type find_sig_entry, *sig_entry;
6796 void **slot;
6797
6798 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6799 gdb_assert (dwp_file != NULL);
6800
6801 /* If TU skeletons have been removed then we may not have read in any
6802 TUs yet. */
6803 if (dwarf2_per_objfile->signatured_types == NULL)
6804 {
6805 dwarf2_per_objfile->signatured_types
6806 = allocate_signatured_type_table (objfile);
6807 }
6808
6809 find_sig_entry.signature = sig;
6810 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6811 &find_sig_entry, INSERT);
6812 sig_entry = (struct signatured_type *) *slot;
6813
6814 /* Have we already tried to read this TU?
6815 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6816 needn't exist in the global table yet). */
6817 if (sig_entry != NULL)
6818 return sig_entry;
6819
6820 if (dwp_file->tus == NULL)
6821 return NULL;
6822 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6823 sig, 1 /* is_debug_types */);
6824 if (dwo_entry == NULL)
6825 return NULL;
6826
6827 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6828 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6829
6830 return sig_entry;
6831 }
6832
6833 /* Lookup a signature based type for DW_FORM_ref_sig8.
6834 Returns NULL if signature SIG is not present in the table.
6835 It is up to the caller to complain about this. */
6836
6837 static struct signatured_type *
6838 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6839 {
6840 struct dwarf2_per_objfile *dwarf2_per_objfile
6841 = cu->per_cu->dwarf2_per_objfile;
6842
6843 if (cu->dwo_unit
6844 && dwarf2_per_objfile->using_index)
6845 {
6846 /* We're in a DWO/DWP file, and we're using .gdb_index.
6847 These cases require special processing. */
6848 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6849 return lookup_dwo_signatured_type (cu, sig);
6850 else
6851 return lookup_dwp_signatured_type (cu, sig);
6852 }
6853 else
6854 {
6855 struct signatured_type find_entry, *entry;
6856
6857 if (dwarf2_per_objfile->signatured_types == NULL)
6858 return NULL;
6859 find_entry.signature = sig;
6860 entry = ((struct signatured_type *)
6861 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6862 return entry;
6863 }
6864 }
6865
6866 /* Return the address base of the compile unit, which, if exists, is stored
6867 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6868 static gdb::optional<ULONGEST>
6869 lookup_addr_base (struct die_info *comp_unit_die)
6870 {
6871 struct attribute *attr;
6872 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6873 if (attr == nullptr)
6874 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6875 if (attr == nullptr)
6876 return gdb::optional<ULONGEST> ();
6877 return DW_UNSND (attr);
6878 }
6879
6880 /* Return range lists base of the compile unit, which, if exists, is stored
6881 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6882 static ULONGEST
6883 lookup_ranges_base (struct die_info *comp_unit_die)
6884 {
6885 struct attribute *attr;
6886 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6887 if (attr == nullptr)
6888 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6889 if (attr == nullptr)
6890 return 0;
6891 return DW_UNSND (attr);
6892 }
6893
6894 /* Low level DIE reading support. */
6895
6896 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6897
6898 static void
6899 init_cu_die_reader (struct die_reader_specs *reader,
6900 struct dwarf2_cu *cu,
6901 struct dwarf2_section_info *section,
6902 struct dwo_file *dwo_file,
6903 struct abbrev_table *abbrev_table)
6904 {
6905 gdb_assert (section->readin && section->buffer != NULL);
6906 reader->abfd = section->get_bfd_owner ();
6907 reader->cu = cu;
6908 reader->dwo_file = dwo_file;
6909 reader->die_section = section;
6910 reader->buffer = section->buffer;
6911 reader->buffer_end = section->buffer + section->size;
6912 reader->abbrev_table = abbrev_table;
6913 }
6914
6915 /* Subroutine of cutu_reader to simplify it.
6916 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6917 There's just a lot of work to do, and cutu_reader is big enough
6918 already.
6919
6920 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6921 from it to the DIE in the DWO. If NULL we are skipping the stub.
6922 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6923 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6924 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6925 STUB_COMP_DIR may be non-NULL.
6926 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6927 are filled in with the info of the DIE from the DWO file.
6928 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6929 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6930 kept around for at least as long as *RESULT_READER.
6931
6932 The result is non-zero if a valid (non-dummy) DIE was found. */
6933
6934 static int
6935 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6936 struct dwo_unit *dwo_unit,
6937 struct die_info *stub_comp_unit_die,
6938 const char *stub_comp_dir,
6939 struct die_reader_specs *result_reader,
6940 const gdb_byte **result_info_ptr,
6941 struct die_info **result_comp_unit_die,
6942 abbrev_table_up *result_dwo_abbrev_table)
6943 {
6944 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6945 struct objfile *objfile = dwarf2_per_objfile->objfile;
6946 struct dwarf2_cu *cu = this_cu->cu;
6947 bfd *abfd;
6948 const gdb_byte *begin_info_ptr, *info_ptr;
6949 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6950 int i,num_extra_attrs;
6951 struct dwarf2_section_info *dwo_abbrev_section;
6952 struct die_info *comp_unit_die;
6953
6954 /* At most one of these may be provided. */
6955 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6956
6957 /* These attributes aren't processed until later:
6958 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6959 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6960 referenced later. However, these attributes are found in the stub
6961 which we won't have later. In order to not impose this complication
6962 on the rest of the code, we read them here and copy them to the
6963 DWO CU/TU die. */
6964
6965 stmt_list = NULL;
6966 low_pc = NULL;
6967 high_pc = NULL;
6968 ranges = NULL;
6969 comp_dir = NULL;
6970
6971 if (stub_comp_unit_die != NULL)
6972 {
6973 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6974 DWO file. */
6975 if (! this_cu->is_debug_types)
6976 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6977 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6978 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6979 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6980 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6981
6982 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6983
6984 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6985 here (if needed). We need the value before we can process
6986 DW_AT_ranges. */
6987 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6988 }
6989 else if (stub_comp_dir != NULL)
6990 {
6991 /* Reconstruct the comp_dir attribute to simplify the code below. */
6992 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6993 comp_dir->name = DW_AT_comp_dir;
6994 comp_dir->form = DW_FORM_string;
6995 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6996 DW_STRING (comp_dir) = stub_comp_dir;
6997 }
6998
6999 /* Set up for reading the DWO CU/TU. */
7000 cu->dwo_unit = dwo_unit;
7001 dwarf2_section_info *section = dwo_unit->section;
7002 section->read (objfile);
7003 abfd = section->get_bfd_owner ();
7004 begin_info_ptr = info_ptr = (section->buffer
7005 + to_underlying (dwo_unit->sect_off));
7006 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7007
7008 if (this_cu->is_debug_types)
7009 {
7010 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7011
7012 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7013 &cu->header, section,
7014 dwo_abbrev_section,
7015 info_ptr, rcuh_kind::TYPE);
7016 /* This is not an assert because it can be caused by bad debug info. */
7017 if (sig_type->signature != cu->header.signature)
7018 {
7019 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7020 " TU at offset %s [in module %s]"),
7021 hex_string (sig_type->signature),
7022 hex_string (cu->header.signature),
7023 sect_offset_str (dwo_unit->sect_off),
7024 bfd_get_filename (abfd));
7025 }
7026 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7027 /* For DWOs coming from DWP files, we don't know the CU length
7028 nor the type's offset in the TU until now. */
7029 dwo_unit->length = get_cu_length (&cu->header);
7030 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7031
7032 /* Establish the type offset that can be used to lookup the type.
7033 For DWO files, we don't know it until now. */
7034 sig_type->type_offset_in_section
7035 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7036 }
7037 else
7038 {
7039 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7040 &cu->header, section,
7041 dwo_abbrev_section,
7042 info_ptr, rcuh_kind::COMPILE);
7043 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7044 /* For DWOs coming from DWP files, we don't know the CU length
7045 until now. */
7046 dwo_unit->length = get_cu_length (&cu->header);
7047 }
7048
7049 *result_dwo_abbrev_table
7050 = abbrev_table_read_table (objfile, dwo_abbrev_section,
7051 cu->header.abbrev_sect_off);
7052 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7053 result_dwo_abbrev_table->get ());
7054
7055 /* Read in the die, but leave space to copy over the attributes
7056 from the stub. This has the benefit of simplifying the rest of
7057 the code - all the work to maintain the illusion of a single
7058 DW_TAG_{compile,type}_unit DIE is done here. */
7059 num_extra_attrs = ((stmt_list != NULL)
7060 + (low_pc != NULL)
7061 + (high_pc != NULL)
7062 + (ranges != NULL)
7063 + (comp_dir != NULL));
7064 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7065 num_extra_attrs);
7066
7067 /* Copy over the attributes from the stub to the DIE we just read in. */
7068 comp_unit_die = *result_comp_unit_die;
7069 i = comp_unit_die->num_attrs;
7070 if (stmt_list != NULL)
7071 comp_unit_die->attrs[i++] = *stmt_list;
7072 if (low_pc != NULL)
7073 comp_unit_die->attrs[i++] = *low_pc;
7074 if (high_pc != NULL)
7075 comp_unit_die->attrs[i++] = *high_pc;
7076 if (ranges != NULL)
7077 comp_unit_die->attrs[i++] = *ranges;
7078 if (comp_dir != NULL)
7079 comp_unit_die->attrs[i++] = *comp_dir;
7080 comp_unit_die->num_attrs += num_extra_attrs;
7081
7082 if (dwarf_die_debug)
7083 {
7084 fprintf_unfiltered (gdb_stdlog,
7085 "Read die from %s@0x%x of %s:\n",
7086 section->get_name (),
7087 (unsigned) (begin_info_ptr - section->buffer),
7088 bfd_get_filename (abfd));
7089 dump_die (comp_unit_die, dwarf_die_debug);
7090 }
7091
7092 /* Skip dummy compilation units. */
7093 if (info_ptr >= begin_info_ptr + dwo_unit->length
7094 || peek_abbrev_code (abfd, info_ptr) == 0)
7095 return 0;
7096
7097 *result_info_ptr = info_ptr;
7098 return 1;
7099 }
7100
7101 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7102 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7103 signature is part of the header. */
7104 static gdb::optional<ULONGEST>
7105 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7106 {
7107 if (cu->header.version >= 5)
7108 return cu->header.signature;
7109 struct attribute *attr;
7110 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7111 if (attr == nullptr)
7112 return gdb::optional<ULONGEST> ();
7113 return DW_UNSND (attr);
7114 }
7115
7116 /* Subroutine of cutu_reader to simplify it.
7117 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7118 Returns NULL if the specified DWO unit cannot be found. */
7119
7120 static struct dwo_unit *
7121 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7122 struct die_info *comp_unit_die,
7123 const char *dwo_name)
7124 {
7125 struct dwarf2_cu *cu = this_cu->cu;
7126 struct dwo_unit *dwo_unit;
7127 const char *comp_dir;
7128
7129 gdb_assert (cu != NULL);
7130
7131 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7132 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7133 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7134
7135 if (this_cu->is_debug_types)
7136 {
7137 struct signatured_type *sig_type;
7138
7139 /* Since this_cu is the first member of struct signatured_type,
7140 we can go from a pointer to one to a pointer to the other. */
7141 sig_type = (struct signatured_type *) this_cu;
7142 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7143 }
7144 else
7145 {
7146 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7147 if (!signature.has_value ())
7148 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7149 " [in module %s]"),
7150 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7151 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7152 *signature);
7153 }
7154
7155 return dwo_unit;
7156 }
7157
7158 /* Subroutine of cutu_reader to simplify it.
7159 See it for a description of the parameters.
7160 Read a TU directly from a DWO file, bypassing the stub. */
7161
7162 void
7163 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7164 int use_existing_cu, int keep)
7165 {
7166 struct signatured_type *sig_type;
7167 struct die_reader_specs reader;
7168
7169 /* Verify we can do the following downcast, and that we have the
7170 data we need. */
7171 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7172 sig_type = (struct signatured_type *) this_cu;
7173 gdb_assert (sig_type->dwo_unit != NULL);
7174
7175 if (use_existing_cu && this_cu->cu != NULL)
7176 {
7177 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7178 /* There's no need to do the rereading_dwo_cu handling that
7179 cutu_reader does since we don't read the stub. */
7180 }
7181 else
7182 {
7183 /* If !use_existing_cu, this_cu->cu must be NULL. */
7184 gdb_assert (this_cu->cu == NULL);
7185 m_new_cu.reset (new dwarf2_cu (this_cu));
7186 }
7187
7188 /* A future optimization, if needed, would be to use an existing
7189 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7190 could share abbrev tables. */
7191
7192 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7193 NULL /* stub_comp_unit_die */,
7194 sig_type->dwo_unit->dwo_file->comp_dir,
7195 &reader, &info_ptr,
7196 &comp_unit_die,
7197 &m_dwo_abbrev_table) == 0)
7198 {
7199 /* Dummy die. */
7200 dummy_p = true;
7201 }
7202 }
7203
7204 /* Initialize a CU (or TU) and read its DIEs.
7205 If the CU defers to a DWO file, read the DWO file as well.
7206
7207 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7208 Otherwise the table specified in the comp unit header is read in and used.
7209 This is an optimization for when we already have the abbrev table.
7210
7211 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7212 Otherwise, a new CU is allocated with xmalloc.
7213
7214 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7215 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7216 end. */
7217
7218 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7219 struct abbrev_table *abbrev_table,
7220 int use_existing_cu, int keep,
7221 bool skip_partial)
7222 : die_reader_specs {},
7223 m_this_cu (this_cu),
7224 m_keep (keep)
7225 {
7226 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7227 struct objfile *objfile = dwarf2_per_objfile->objfile;
7228 struct dwarf2_section_info *section = this_cu->section;
7229 bfd *abfd = section->get_bfd_owner ();
7230 struct dwarf2_cu *cu;
7231 const gdb_byte *begin_info_ptr;
7232 struct signatured_type *sig_type = NULL;
7233 struct dwarf2_section_info *abbrev_section;
7234 /* Non-zero if CU currently points to a DWO file and we need to
7235 reread it. When this happens we need to reread the skeleton die
7236 before we can reread the DWO file (this only applies to CUs, not TUs). */
7237 int rereading_dwo_cu = 0;
7238
7239 if (dwarf_die_debug)
7240 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7241 this_cu->is_debug_types ? "type" : "comp",
7242 sect_offset_str (this_cu->sect_off));
7243
7244 if (use_existing_cu)
7245 gdb_assert (keep);
7246
7247 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7248 file (instead of going through the stub), short-circuit all of this. */
7249 if (this_cu->reading_dwo_directly)
7250 {
7251 /* Narrow down the scope of possibilities to have to understand. */
7252 gdb_assert (this_cu->is_debug_types);
7253 gdb_assert (abbrev_table == NULL);
7254 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7255 return;
7256 }
7257
7258 /* This is cheap if the section is already read in. */
7259 section->read (objfile);
7260
7261 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7262
7263 abbrev_section = get_abbrev_section_for_cu (this_cu);
7264
7265 if (use_existing_cu && this_cu->cu != NULL)
7266 {
7267 cu = this_cu->cu;
7268 /* If this CU is from a DWO file we need to start over, we need to
7269 refetch the attributes from the skeleton CU.
7270 This could be optimized by retrieving those attributes from when we
7271 were here the first time: the previous comp_unit_die was stored in
7272 comp_unit_obstack. But there's no data yet that we need this
7273 optimization. */
7274 if (cu->dwo_unit != NULL)
7275 rereading_dwo_cu = 1;
7276 }
7277 else
7278 {
7279 /* If !use_existing_cu, this_cu->cu must be NULL. */
7280 gdb_assert (this_cu->cu == NULL);
7281 m_new_cu.reset (new dwarf2_cu (this_cu));
7282 cu = m_new_cu.get ();
7283 }
7284
7285 /* Get the header. */
7286 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7287 {
7288 /* We already have the header, there's no need to read it in again. */
7289 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7290 }
7291 else
7292 {
7293 if (this_cu->is_debug_types)
7294 {
7295 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7296 &cu->header, section,
7297 abbrev_section, info_ptr,
7298 rcuh_kind::TYPE);
7299
7300 /* Since per_cu is the first member of struct signatured_type,
7301 we can go from a pointer to one to a pointer to the other. */
7302 sig_type = (struct signatured_type *) this_cu;
7303 gdb_assert (sig_type->signature == cu->header.signature);
7304 gdb_assert (sig_type->type_offset_in_tu
7305 == cu->header.type_cu_offset_in_tu);
7306 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7307
7308 /* LENGTH has not been set yet for type units if we're
7309 using .gdb_index. */
7310 this_cu->length = get_cu_length (&cu->header);
7311
7312 /* Establish the type offset that can be used to lookup the type. */
7313 sig_type->type_offset_in_section =
7314 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7315
7316 this_cu->dwarf_version = cu->header.version;
7317 }
7318 else
7319 {
7320 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7321 &cu->header, section,
7322 abbrev_section,
7323 info_ptr,
7324 rcuh_kind::COMPILE);
7325
7326 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7327 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7328 this_cu->dwarf_version = cu->header.version;
7329 }
7330 }
7331
7332 /* Skip dummy compilation units. */
7333 if (info_ptr >= begin_info_ptr + this_cu->length
7334 || peek_abbrev_code (abfd, info_ptr) == 0)
7335 {
7336 dummy_p = true;
7337 return;
7338 }
7339
7340 /* If we don't have them yet, read the abbrevs for this compilation unit.
7341 And if we need to read them now, make sure they're freed when we're
7342 done. */
7343 if (abbrev_table != NULL)
7344 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7345 else
7346 {
7347 m_abbrev_table_holder
7348 = abbrev_table_read_table (objfile, abbrev_section,
7349 cu->header.abbrev_sect_off);
7350 abbrev_table = m_abbrev_table_holder.get ();
7351 }
7352
7353 /* Read the top level CU/TU die. */
7354 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7355 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7356
7357 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7358 {
7359 dummy_p = true;
7360 return;
7361 }
7362
7363 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7364 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7365 table from the DWO file and pass the ownership over to us. It will be
7366 referenced from READER, so we must make sure to free it after we're done
7367 with READER.
7368
7369 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7370 DWO CU, that this test will fail (the attribute will not be present). */
7371 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7372 if (dwo_name != nullptr)
7373 {
7374 struct dwo_unit *dwo_unit;
7375 struct die_info *dwo_comp_unit_die;
7376
7377 if (comp_unit_die->has_children)
7378 {
7379 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7380 " has children (offset %s) [in module %s]"),
7381 sect_offset_str (this_cu->sect_off),
7382 bfd_get_filename (abfd));
7383 }
7384 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7385 if (dwo_unit != NULL)
7386 {
7387 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7388 comp_unit_die, NULL,
7389 this, &info_ptr,
7390 &dwo_comp_unit_die,
7391 &m_dwo_abbrev_table) == 0)
7392 {
7393 /* Dummy die. */
7394 dummy_p = true;
7395 return;
7396 }
7397 comp_unit_die = dwo_comp_unit_die;
7398 }
7399 else
7400 {
7401 /* Yikes, we couldn't find the rest of the DIE, we only have
7402 the stub. A complaint has already been logged. There's
7403 not much more we can do except pass on the stub DIE to
7404 die_reader_func. We don't want to throw an error on bad
7405 debug info. */
7406 }
7407 }
7408 }
7409
7410 cutu_reader::~cutu_reader ()
7411 {
7412 /* Done, clean up. */
7413 if (m_new_cu != NULL && m_keep && !dummy_p)
7414 {
7415 struct dwarf2_per_objfile *dwarf2_per_objfile
7416 = m_this_cu->dwarf2_per_objfile;
7417 /* Link this CU into read_in_chain. */
7418 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7419 dwarf2_per_objfile->read_in_chain = m_this_cu;
7420 /* The chain owns it now. */
7421 m_new_cu.release ();
7422 }
7423 }
7424
7425 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7426 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7427 assumed to have already done the lookup to find the DWO file).
7428
7429 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7430 THIS_CU->is_debug_types, but nothing else.
7431
7432 We fill in THIS_CU->length.
7433
7434 THIS_CU->cu is always freed when done.
7435 This is done in order to not leave THIS_CU->cu in a state where we have
7436 to care whether it refers to the "main" CU or the DWO CU.
7437
7438 When parent_cu is passed, it is used to provide a default value for
7439 str_offsets_base and addr_base from the parent. */
7440
7441 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7442 struct dwarf2_cu *parent_cu,
7443 struct dwo_file *dwo_file)
7444 : die_reader_specs {},
7445 m_this_cu (this_cu)
7446 {
7447 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7448 struct objfile *objfile = dwarf2_per_objfile->objfile;
7449 struct dwarf2_section_info *section = this_cu->section;
7450 bfd *abfd = section->get_bfd_owner ();
7451 struct dwarf2_section_info *abbrev_section;
7452 const gdb_byte *begin_info_ptr, *info_ptr;
7453
7454 if (dwarf_die_debug)
7455 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7456 this_cu->is_debug_types ? "type" : "comp",
7457 sect_offset_str (this_cu->sect_off));
7458
7459 gdb_assert (this_cu->cu == NULL);
7460
7461 abbrev_section = (dwo_file != NULL
7462 ? &dwo_file->sections.abbrev
7463 : get_abbrev_section_for_cu (this_cu));
7464
7465 /* This is cheap if the section is already read in. */
7466 section->read (objfile);
7467
7468 m_new_cu.reset (new dwarf2_cu (this_cu));
7469
7470 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7471 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7472 &m_new_cu->header, section,
7473 abbrev_section, info_ptr,
7474 (this_cu->is_debug_types
7475 ? rcuh_kind::TYPE
7476 : rcuh_kind::COMPILE));
7477
7478 if (parent_cu != nullptr)
7479 {
7480 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7481 m_new_cu->addr_base = parent_cu->addr_base;
7482 }
7483 this_cu->length = get_cu_length (&m_new_cu->header);
7484
7485 /* Skip dummy compilation units. */
7486 if (info_ptr >= begin_info_ptr + this_cu->length
7487 || peek_abbrev_code (abfd, info_ptr) == 0)
7488 {
7489 dummy_p = true;
7490 return;
7491 }
7492
7493 m_abbrev_table_holder
7494 = abbrev_table_read_table (objfile, abbrev_section,
7495 m_new_cu->header.abbrev_sect_off);
7496
7497 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7498 m_abbrev_table_holder.get ());
7499 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7500 }
7501
7502 \f
7503 /* Type Unit Groups.
7504
7505 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7506 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7507 so that all types coming from the same compilation (.o file) are grouped
7508 together. A future step could be to put the types in the same symtab as
7509 the CU the types ultimately came from. */
7510
7511 static hashval_t
7512 hash_type_unit_group (const void *item)
7513 {
7514 const struct type_unit_group *tu_group
7515 = (const struct type_unit_group *) item;
7516
7517 return hash_stmt_list_entry (&tu_group->hash);
7518 }
7519
7520 static int
7521 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7522 {
7523 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7524 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7525
7526 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7527 }
7528
7529 /* Allocate a hash table for type unit groups. */
7530
7531 static htab_t
7532 allocate_type_unit_groups_table (struct objfile *objfile)
7533 {
7534 return htab_create_alloc_ex (3,
7535 hash_type_unit_group,
7536 eq_type_unit_group,
7537 NULL,
7538 &objfile->objfile_obstack,
7539 hashtab_obstack_allocate,
7540 dummy_obstack_deallocate);
7541 }
7542
7543 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7544 partial symtabs. We combine several TUs per psymtab to not let the size
7545 of any one psymtab grow too big. */
7546 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7547 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7548
7549 /* Helper routine for get_type_unit_group.
7550 Create the type_unit_group object used to hold one or more TUs. */
7551
7552 static struct type_unit_group *
7553 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7554 {
7555 struct dwarf2_per_objfile *dwarf2_per_objfile
7556 = cu->per_cu->dwarf2_per_objfile;
7557 struct objfile *objfile = dwarf2_per_objfile->objfile;
7558 struct dwarf2_per_cu_data *per_cu;
7559 struct type_unit_group *tu_group;
7560
7561 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7562 struct type_unit_group);
7563 per_cu = &tu_group->per_cu;
7564 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7565
7566 if (dwarf2_per_objfile->using_index)
7567 {
7568 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7569 struct dwarf2_per_cu_quick_data);
7570 }
7571 else
7572 {
7573 unsigned int line_offset = to_underlying (line_offset_struct);
7574 dwarf2_psymtab *pst;
7575 std::string name;
7576
7577 /* Give the symtab a useful name for debug purposes. */
7578 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7579 name = string_printf ("<type_units_%d>",
7580 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7581 else
7582 name = string_printf ("<type_units_at_0x%x>", line_offset);
7583
7584 pst = create_partial_symtab (per_cu, name.c_str ());
7585 pst->anonymous = true;
7586 }
7587
7588 tu_group->hash.dwo_unit = cu->dwo_unit;
7589 tu_group->hash.line_sect_off = line_offset_struct;
7590
7591 return tu_group;
7592 }
7593
7594 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7595 STMT_LIST is a DW_AT_stmt_list attribute. */
7596
7597 static struct type_unit_group *
7598 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7599 {
7600 struct dwarf2_per_objfile *dwarf2_per_objfile
7601 = cu->per_cu->dwarf2_per_objfile;
7602 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7603 struct type_unit_group *tu_group;
7604 void **slot;
7605 unsigned int line_offset;
7606 struct type_unit_group type_unit_group_for_lookup;
7607
7608 if (dwarf2_per_objfile->type_unit_groups == NULL)
7609 {
7610 dwarf2_per_objfile->type_unit_groups =
7611 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7612 }
7613
7614 /* Do we need to create a new group, or can we use an existing one? */
7615
7616 if (stmt_list)
7617 {
7618 line_offset = DW_UNSND (stmt_list);
7619 ++tu_stats->nr_symtab_sharers;
7620 }
7621 else
7622 {
7623 /* Ugh, no stmt_list. Rare, but we have to handle it.
7624 We can do various things here like create one group per TU or
7625 spread them over multiple groups to split up the expansion work.
7626 To avoid worst case scenarios (too many groups or too large groups)
7627 we, umm, group them in bunches. */
7628 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7629 | (tu_stats->nr_stmt_less_type_units
7630 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7631 ++tu_stats->nr_stmt_less_type_units;
7632 }
7633
7634 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7635 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7636 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7637 &type_unit_group_for_lookup, INSERT);
7638 if (*slot != NULL)
7639 {
7640 tu_group = (struct type_unit_group *) *slot;
7641 gdb_assert (tu_group != NULL);
7642 }
7643 else
7644 {
7645 sect_offset line_offset_struct = (sect_offset) line_offset;
7646 tu_group = create_type_unit_group (cu, line_offset_struct);
7647 *slot = tu_group;
7648 ++tu_stats->nr_symtabs;
7649 }
7650
7651 return tu_group;
7652 }
7653 \f
7654 /* Partial symbol tables. */
7655
7656 /* Create a psymtab named NAME and assign it to PER_CU.
7657
7658 The caller must fill in the following details:
7659 dirname, textlow, texthigh. */
7660
7661 static dwarf2_psymtab *
7662 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7663 {
7664 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7665 dwarf2_psymtab *pst;
7666
7667 pst = new dwarf2_psymtab (name, objfile, 0);
7668
7669 pst->psymtabs_addrmap_supported = true;
7670
7671 /* This is the glue that links PST into GDB's symbol API. */
7672 pst->per_cu_data = per_cu;
7673 per_cu->v.psymtab = pst;
7674
7675 return pst;
7676 }
7677
7678 /* DIE reader function for process_psymtab_comp_unit. */
7679
7680 static void
7681 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7682 const gdb_byte *info_ptr,
7683 struct die_info *comp_unit_die,
7684 int want_partial_unit,
7685 enum language pretend_language)
7686 {
7687 struct dwarf2_cu *cu = reader->cu;
7688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7690 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7691 CORE_ADDR baseaddr;
7692 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7693 dwarf2_psymtab *pst;
7694 enum pc_bounds_kind cu_bounds_kind;
7695 const char *filename;
7696
7697 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7698 return;
7699
7700 gdb_assert (! per_cu->is_debug_types);
7701
7702 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7703
7704 /* Allocate a new partial symbol table structure. */
7705 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7706 if (filename == NULL)
7707 filename = "";
7708
7709 pst = create_partial_symtab (per_cu, filename);
7710
7711 /* This must be done before calling dwarf2_build_include_psymtabs. */
7712 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7713
7714 baseaddr = objfile->text_section_offset ();
7715
7716 dwarf2_find_base_address (comp_unit_die, cu);
7717
7718 /* Possibly set the default values of LOWPC and HIGHPC from
7719 `DW_AT_ranges'. */
7720 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7721 &best_highpc, cu, pst);
7722 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7723 {
7724 CORE_ADDR low
7725 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7726 - baseaddr);
7727 CORE_ADDR high
7728 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7729 - baseaddr - 1);
7730 /* Store the contiguous range if it is not empty; it can be
7731 empty for CUs with no code. */
7732 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7733 low, high, pst);
7734 }
7735
7736 /* Check if comp unit has_children.
7737 If so, read the rest of the partial symbols from this comp unit.
7738 If not, there's no more debug_info for this comp unit. */
7739 if (comp_unit_die->has_children)
7740 {
7741 struct partial_die_info *first_die;
7742 CORE_ADDR lowpc, highpc;
7743
7744 lowpc = ((CORE_ADDR) -1);
7745 highpc = ((CORE_ADDR) 0);
7746
7747 first_die = load_partial_dies (reader, info_ptr, 1);
7748
7749 scan_partial_symbols (first_die, &lowpc, &highpc,
7750 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7751
7752 /* If we didn't find a lowpc, set it to highpc to avoid
7753 complaints from `maint check'. */
7754 if (lowpc == ((CORE_ADDR) -1))
7755 lowpc = highpc;
7756
7757 /* If the compilation unit didn't have an explicit address range,
7758 then use the information extracted from its child dies. */
7759 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7760 {
7761 best_lowpc = lowpc;
7762 best_highpc = highpc;
7763 }
7764 }
7765 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7766 best_lowpc + baseaddr)
7767 - baseaddr);
7768 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7769 best_highpc + baseaddr)
7770 - baseaddr);
7771
7772 end_psymtab_common (objfile, pst);
7773
7774 if (!cu->per_cu->imported_symtabs_empty ())
7775 {
7776 int i;
7777 int len = cu->per_cu->imported_symtabs_size ();
7778
7779 /* Fill in 'dependencies' here; we fill in 'users' in a
7780 post-pass. */
7781 pst->number_of_dependencies = len;
7782 pst->dependencies
7783 = objfile->partial_symtabs->allocate_dependencies (len);
7784 for (i = 0; i < len; ++i)
7785 {
7786 pst->dependencies[i]
7787 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7788 }
7789
7790 cu->per_cu->imported_symtabs_free ();
7791 }
7792
7793 /* Get the list of files included in the current compilation unit,
7794 and build a psymtab for each of them. */
7795 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7796
7797 if (dwarf_read_debug)
7798 fprintf_unfiltered (gdb_stdlog,
7799 "Psymtab for %s unit @%s: %s - %s"
7800 ", %d global, %d static syms\n",
7801 per_cu->is_debug_types ? "type" : "comp",
7802 sect_offset_str (per_cu->sect_off),
7803 paddress (gdbarch, pst->text_low (objfile)),
7804 paddress (gdbarch, pst->text_high (objfile)),
7805 pst->n_global_syms, pst->n_static_syms);
7806 }
7807
7808 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7809 Process compilation unit THIS_CU for a psymtab. */
7810
7811 static void
7812 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7813 int want_partial_unit,
7814 enum language pretend_language)
7815 {
7816 /* If this compilation unit was already read in, free the
7817 cached copy in order to read it in again. This is
7818 necessary because we skipped some symbols when we first
7819 read in the compilation unit (see load_partial_dies).
7820 This problem could be avoided, but the benefit is unclear. */
7821 if (this_cu->cu != NULL)
7822 free_one_cached_comp_unit (this_cu);
7823
7824 cutu_reader reader (this_cu, NULL, 0, 0, false);
7825
7826 if (reader.dummy_p)
7827 {
7828 /* Nothing. */
7829 }
7830 else if (this_cu->is_debug_types)
7831 build_type_psymtabs_reader (&reader, reader.info_ptr,
7832 reader.comp_unit_die);
7833 else
7834 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7835 reader.comp_unit_die,
7836 want_partial_unit,
7837 pretend_language);
7838
7839 /* Age out any secondary CUs. */
7840 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7841 }
7842
7843 /* Reader function for build_type_psymtabs. */
7844
7845 static void
7846 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7847 const gdb_byte *info_ptr,
7848 struct die_info *type_unit_die)
7849 {
7850 struct dwarf2_per_objfile *dwarf2_per_objfile
7851 = reader->cu->per_cu->dwarf2_per_objfile;
7852 struct objfile *objfile = dwarf2_per_objfile->objfile;
7853 struct dwarf2_cu *cu = reader->cu;
7854 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7855 struct signatured_type *sig_type;
7856 struct type_unit_group *tu_group;
7857 struct attribute *attr;
7858 struct partial_die_info *first_die;
7859 CORE_ADDR lowpc, highpc;
7860 dwarf2_psymtab *pst;
7861
7862 gdb_assert (per_cu->is_debug_types);
7863 sig_type = (struct signatured_type *) per_cu;
7864
7865 if (! type_unit_die->has_children)
7866 return;
7867
7868 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7869 tu_group = get_type_unit_group (cu, attr);
7870
7871 if (tu_group->tus == nullptr)
7872 tu_group->tus = new std::vector<signatured_type *>;
7873 tu_group->tus->push_back (sig_type);
7874
7875 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7876 pst = create_partial_symtab (per_cu, "");
7877 pst->anonymous = true;
7878
7879 first_die = load_partial_dies (reader, info_ptr, 1);
7880
7881 lowpc = (CORE_ADDR) -1;
7882 highpc = (CORE_ADDR) 0;
7883 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7884
7885 end_psymtab_common (objfile, pst);
7886 }
7887
7888 /* Struct used to sort TUs by their abbreviation table offset. */
7889
7890 struct tu_abbrev_offset
7891 {
7892 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7893 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7894 {}
7895
7896 signatured_type *sig_type;
7897 sect_offset abbrev_offset;
7898 };
7899
7900 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7901
7902 static bool
7903 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7904 const struct tu_abbrev_offset &b)
7905 {
7906 return a.abbrev_offset < b.abbrev_offset;
7907 }
7908
7909 /* Efficiently read all the type units.
7910 This does the bulk of the work for build_type_psymtabs.
7911
7912 The efficiency is because we sort TUs by the abbrev table they use and
7913 only read each abbrev table once. In one program there are 200K TUs
7914 sharing 8K abbrev tables.
7915
7916 The main purpose of this function is to support building the
7917 dwarf2_per_objfile->type_unit_groups table.
7918 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7919 can collapse the search space by grouping them by stmt_list.
7920 The savings can be significant, in the same program from above the 200K TUs
7921 share 8K stmt_list tables.
7922
7923 FUNC is expected to call get_type_unit_group, which will create the
7924 struct type_unit_group if necessary and add it to
7925 dwarf2_per_objfile->type_unit_groups. */
7926
7927 static void
7928 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7929 {
7930 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7931 abbrev_table_up abbrev_table;
7932 sect_offset abbrev_offset;
7933
7934 /* It's up to the caller to not call us multiple times. */
7935 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7936
7937 if (dwarf2_per_objfile->all_type_units.empty ())
7938 return;
7939
7940 /* TUs typically share abbrev tables, and there can be way more TUs than
7941 abbrev tables. Sort by abbrev table to reduce the number of times we
7942 read each abbrev table in.
7943 Alternatives are to punt or to maintain a cache of abbrev tables.
7944 This is simpler and efficient enough for now.
7945
7946 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7947 symtab to use). Typically TUs with the same abbrev offset have the same
7948 stmt_list value too so in practice this should work well.
7949
7950 The basic algorithm here is:
7951
7952 sort TUs by abbrev table
7953 for each TU with same abbrev table:
7954 read abbrev table if first user
7955 read TU top level DIE
7956 [IWBN if DWO skeletons had DW_AT_stmt_list]
7957 call FUNC */
7958
7959 if (dwarf_read_debug)
7960 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7961
7962 /* Sort in a separate table to maintain the order of all_type_units
7963 for .gdb_index: TU indices directly index all_type_units. */
7964 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7965 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7966
7967 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7968 sorted_by_abbrev.emplace_back
7969 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7970 sig_type->per_cu.section,
7971 sig_type->per_cu.sect_off));
7972
7973 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7974 sort_tu_by_abbrev_offset);
7975
7976 abbrev_offset = (sect_offset) ~(unsigned) 0;
7977
7978 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7979 {
7980 /* Switch to the next abbrev table if necessary. */
7981 if (abbrev_table == NULL
7982 || tu.abbrev_offset != abbrev_offset)
7983 {
7984 abbrev_offset = tu.abbrev_offset;
7985 abbrev_table =
7986 abbrev_table_read_table (dwarf2_per_objfile->objfile,
7987 &dwarf2_per_objfile->abbrev,
7988 abbrev_offset);
7989 ++tu_stats->nr_uniq_abbrev_tables;
7990 }
7991
7992 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7993 0, 0, false);
7994 if (!reader.dummy_p)
7995 build_type_psymtabs_reader (&reader, reader.info_ptr,
7996 reader.comp_unit_die);
7997 }
7998 }
7999
8000 /* Print collected type unit statistics. */
8001
8002 static void
8003 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8004 {
8005 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8006
8007 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8008 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8009 dwarf2_per_objfile->all_type_units.size ());
8010 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8011 tu_stats->nr_uniq_abbrev_tables);
8012 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8013 tu_stats->nr_symtabs);
8014 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8015 tu_stats->nr_symtab_sharers);
8016 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8017 tu_stats->nr_stmt_less_type_units);
8018 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8019 tu_stats->nr_all_type_units_reallocs);
8020 }
8021
8022 /* Traversal function for build_type_psymtabs. */
8023
8024 static int
8025 build_type_psymtab_dependencies (void **slot, void *info)
8026 {
8027 struct dwarf2_per_objfile *dwarf2_per_objfile
8028 = (struct dwarf2_per_objfile *) info;
8029 struct objfile *objfile = dwarf2_per_objfile->objfile;
8030 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8031 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8032 dwarf2_psymtab *pst = per_cu->v.psymtab;
8033 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8034 int i;
8035
8036 gdb_assert (len > 0);
8037 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8038
8039 pst->number_of_dependencies = len;
8040 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8041 for (i = 0; i < len; ++i)
8042 {
8043 struct signatured_type *iter = tu_group->tus->at (i);
8044 gdb_assert (iter->per_cu.is_debug_types);
8045 pst->dependencies[i] = iter->per_cu.v.psymtab;
8046 iter->type_unit_group = tu_group;
8047 }
8048
8049 delete tu_group->tus;
8050 tu_group->tus = nullptr;
8051
8052 return 1;
8053 }
8054
8055 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8056 Build partial symbol tables for the .debug_types comp-units. */
8057
8058 static void
8059 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8060 {
8061 if (! create_all_type_units (dwarf2_per_objfile))
8062 return;
8063
8064 build_type_psymtabs_1 (dwarf2_per_objfile);
8065 }
8066
8067 /* Traversal function for process_skeletonless_type_unit.
8068 Read a TU in a DWO file and build partial symbols for it. */
8069
8070 static int
8071 process_skeletonless_type_unit (void **slot, void *info)
8072 {
8073 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8074 struct dwarf2_per_objfile *dwarf2_per_objfile
8075 = (struct dwarf2_per_objfile *) info;
8076 struct signatured_type find_entry, *entry;
8077
8078 /* If this TU doesn't exist in the global table, add it and read it in. */
8079
8080 if (dwarf2_per_objfile->signatured_types == NULL)
8081 {
8082 dwarf2_per_objfile->signatured_types
8083 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8084 }
8085
8086 find_entry.signature = dwo_unit->signature;
8087 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8088 INSERT);
8089 /* If we've already seen this type there's nothing to do. What's happening
8090 is we're doing our own version of comdat-folding here. */
8091 if (*slot != NULL)
8092 return 1;
8093
8094 /* This does the job that create_all_type_units would have done for
8095 this TU. */
8096 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8097 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8098 *slot = entry;
8099
8100 /* This does the job that build_type_psymtabs_1 would have done. */
8101 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8102 if (!reader.dummy_p)
8103 build_type_psymtabs_reader (&reader, reader.info_ptr,
8104 reader.comp_unit_die);
8105
8106 return 1;
8107 }
8108
8109 /* Traversal function for process_skeletonless_type_units. */
8110
8111 static int
8112 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8113 {
8114 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8115
8116 if (dwo_file->tus != NULL)
8117 {
8118 htab_traverse_noresize (dwo_file->tus,
8119 process_skeletonless_type_unit, info);
8120 }
8121
8122 return 1;
8123 }
8124
8125 /* Scan all TUs of DWO files, verifying we've processed them.
8126 This is needed in case a TU was emitted without its skeleton.
8127 Note: This can't be done until we know what all the DWO files are. */
8128
8129 static void
8130 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8131 {
8132 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8133 if (get_dwp_file (dwarf2_per_objfile) == NULL
8134 && dwarf2_per_objfile->dwo_files != NULL)
8135 {
8136 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8137 process_dwo_file_for_skeletonless_type_units,
8138 dwarf2_per_objfile);
8139 }
8140 }
8141
8142 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8143
8144 static void
8145 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8146 {
8147 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8148 {
8149 dwarf2_psymtab *pst = per_cu->v.psymtab;
8150
8151 if (pst == NULL)
8152 continue;
8153
8154 for (int j = 0; j < pst->number_of_dependencies; ++j)
8155 {
8156 /* Set the 'user' field only if it is not already set. */
8157 if (pst->dependencies[j]->user == NULL)
8158 pst->dependencies[j]->user = pst;
8159 }
8160 }
8161 }
8162
8163 /* Build the partial symbol table by doing a quick pass through the
8164 .debug_info and .debug_abbrev sections. */
8165
8166 static void
8167 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8168 {
8169 struct objfile *objfile = dwarf2_per_objfile->objfile;
8170
8171 if (dwarf_read_debug)
8172 {
8173 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8174 objfile_name (objfile));
8175 }
8176
8177 dwarf2_per_objfile->reading_partial_symbols = 1;
8178
8179 dwarf2_per_objfile->info.read (objfile);
8180
8181 /* Any cached compilation units will be linked by the per-objfile
8182 read_in_chain. Make sure to free them when we're done. */
8183 free_cached_comp_units freer (dwarf2_per_objfile);
8184
8185 build_type_psymtabs (dwarf2_per_objfile);
8186
8187 create_all_comp_units (dwarf2_per_objfile);
8188
8189 /* Create a temporary address map on a temporary obstack. We later
8190 copy this to the final obstack. */
8191 auto_obstack temp_obstack;
8192
8193 scoped_restore save_psymtabs_addrmap
8194 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8195 addrmap_create_mutable (&temp_obstack));
8196
8197 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8198 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8199
8200 /* This has to wait until we read the CUs, we need the list of DWOs. */
8201 process_skeletonless_type_units (dwarf2_per_objfile);
8202
8203 /* Now that all TUs have been processed we can fill in the dependencies. */
8204 if (dwarf2_per_objfile->type_unit_groups != NULL)
8205 {
8206 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8207 build_type_psymtab_dependencies, dwarf2_per_objfile);
8208 }
8209
8210 if (dwarf_read_debug)
8211 print_tu_stats (dwarf2_per_objfile);
8212
8213 set_partial_user (dwarf2_per_objfile);
8214
8215 objfile->partial_symtabs->psymtabs_addrmap
8216 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8217 objfile->partial_symtabs->obstack ());
8218 /* At this point we want to keep the address map. */
8219 save_psymtabs_addrmap.release ();
8220
8221 if (dwarf_read_debug)
8222 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8223 objfile_name (objfile));
8224 }
8225
8226 /* Load the partial DIEs for a secondary CU into memory.
8227 This is also used when rereading a primary CU with load_all_dies. */
8228
8229 static void
8230 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8231 {
8232 cutu_reader reader (this_cu, NULL, 1, 1, false);
8233
8234 if (!reader.dummy_p)
8235 {
8236 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8237 language_minimal);
8238
8239 /* Check if comp unit has_children.
8240 If so, read the rest of the partial symbols from this comp unit.
8241 If not, there's no more debug_info for this comp unit. */
8242 if (reader.comp_unit_die->has_children)
8243 load_partial_dies (&reader, reader.info_ptr, 0);
8244 }
8245 }
8246
8247 static void
8248 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8249 struct dwarf2_section_info *section,
8250 struct dwarf2_section_info *abbrev_section,
8251 unsigned int is_dwz)
8252 {
8253 const gdb_byte *info_ptr;
8254 struct objfile *objfile = dwarf2_per_objfile->objfile;
8255
8256 if (dwarf_read_debug)
8257 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8258 section->get_name (),
8259 section->get_file_name ());
8260
8261 section->read (objfile);
8262
8263 info_ptr = section->buffer;
8264
8265 while (info_ptr < section->buffer + section->size)
8266 {
8267 struct dwarf2_per_cu_data *this_cu;
8268
8269 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8270
8271 comp_unit_head cu_header;
8272 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8273 abbrev_section, info_ptr,
8274 rcuh_kind::COMPILE);
8275
8276 /* Save the compilation unit for later lookup. */
8277 if (cu_header.unit_type != DW_UT_type)
8278 {
8279 this_cu = XOBNEW (&objfile->objfile_obstack,
8280 struct dwarf2_per_cu_data);
8281 memset (this_cu, 0, sizeof (*this_cu));
8282 }
8283 else
8284 {
8285 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8286 struct signatured_type);
8287 memset (sig_type, 0, sizeof (*sig_type));
8288 sig_type->signature = cu_header.signature;
8289 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8290 this_cu = &sig_type->per_cu;
8291 }
8292 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8293 this_cu->sect_off = sect_off;
8294 this_cu->length = cu_header.length + cu_header.initial_length_size;
8295 this_cu->is_dwz = is_dwz;
8296 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8297 this_cu->section = section;
8298
8299 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8300
8301 info_ptr = info_ptr + this_cu->length;
8302 }
8303 }
8304
8305 /* Create a list of all compilation units in OBJFILE.
8306 This is only done for -readnow and building partial symtabs. */
8307
8308 static void
8309 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8310 {
8311 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8312 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8313 &dwarf2_per_objfile->abbrev, 0);
8314
8315 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8316 if (dwz != NULL)
8317 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8318 1);
8319 }
8320
8321 /* Process all loaded DIEs for compilation unit CU, starting at
8322 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8323 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8324 DW_AT_ranges). See the comments of add_partial_subprogram on how
8325 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8326
8327 static void
8328 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8329 CORE_ADDR *highpc, int set_addrmap,
8330 struct dwarf2_cu *cu)
8331 {
8332 struct partial_die_info *pdi;
8333
8334 /* Now, march along the PDI's, descending into ones which have
8335 interesting children but skipping the children of the other ones,
8336 until we reach the end of the compilation unit. */
8337
8338 pdi = first_die;
8339
8340 while (pdi != NULL)
8341 {
8342 pdi->fixup (cu);
8343
8344 /* Anonymous namespaces or modules have no name but have interesting
8345 children, so we need to look at them. Ditto for anonymous
8346 enums. */
8347
8348 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8349 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8350 || pdi->tag == DW_TAG_imported_unit
8351 || pdi->tag == DW_TAG_inlined_subroutine)
8352 {
8353 switch (pdi->tag)
8354 {
8355 case DW_TAG_subprogram:
8356 case DW_TAG_inlined_subroutine:
8357 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8358 break;
8359 case DW_TAG_constant:
8360 case DW_TAG_variable:
8361 case DW_TAG_typedef:
8362 case DW_TAG_union_type:
8363 if (!pdi->is_declaration)
8364 {
8365 add_partial_symbol (pdi, cu);
8366 }
8367 break;
8368 case DW_TAG_class_type:
8369 case DW_TAG_interface_type:
8370 case DW_TAG_structure_type:
8371 if (!pdi->is_declaration)
8372 {
8373 add_partial_symbol (pdi, cu);
8374 }
8375 if ((cu->language == language_rust
8376 || cu->language == language_cplus) && pdi->has_children)
8377 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8378 set_addrmap, cu);
8379 break;
8380 case DW_TAG_enumeration_type:
8381 if (!pdi->is_declaration)
8382 add_partial_enumeration (pdi, cu);
8383 break;
8384 case DW_TAG_base_type:
8385 case DW_TAG_subrange_type:
8386 /* File scope base type definitions are added to the partial
8387 symbol table. */
8388 add_partial_symbol (pdi, cu);
8389 break;
8390 case DW_TAG_namespace:
8391 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8392 break;
8393 case DW_TAG_module:
8394 if (!pdi->is_declaration)
8395 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8396 break;
8397 case DW_TAG_imported_unit:
8398 {
8399 struct dwarf2_per_cu_data *per_cu;
8400
8401 /* For now we don't handle imported units in type units. */
8402 if (cu->per_cu->is_debug_types)
8403 {
8404 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8405 " supported in type units [in module %s]"),
8406 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8407 }
8408
8409 per_cu = dwarf2_find_containing_comp_unit
8410 (pdi->d.sect_off, pdi->is_dwz,
8411 cu->per_cu->dwarf2_per_objfile);
8412
8413 /* Go read the partial unit, if needed. */
8414 if (per_cu->v.psymtab == NULL)
8415 process_psymtab_comp_unit (per_cu, 1, cu->language);
8416
8417 cu->per_cu->imported_symtabs_push (per_cu);
8418 }
8419 break;
8420 case DW_TAG_imported_declaration:
8421 add_partial_symbol (pdi, cu);
8422 break;
8423 default:
8424 break;
8425 }
8426 }
8427
8428 /* If the die has a sibling, skip to the sibling. */
8429
8430 pdi = pdi->die_sibling;
8431 }
8432 }
8433
8434 /* Functions used to compute the fully scoped name of a partial DIE.
8435
8436 Normally, this is simple. For C++, the parent DIE's fully scoped
8437 name is concatenated with "::" and the partial DIE's name.
8438 Enumerators are an exception; they use the scope of their parent
8439 enumeration type, i.e. the name of the enumeration type is not
8440 prepended to the enumerator.
8441
8442 There are two complexities. One is DW_AT_specification; in this
8443 case "parent" means the parent of the target of the specification,
8444 instead of the direct parent of the DIE. The other is compilers
8445 which do not emit DW_TAG_namespace; in this case we try to guess
8446 the fully qualified name of structure types from their members'
8447 linkage names. This must be done using the DIE's children rather
8448 than the children of any DW_AT_specification target. We only need
8449 to do this for structures at the top level, i.e. if the target of
8450 any DW_AT_specification (if any; otherwise the DIE itself) does not
8451 have a parent. */
8452
8453 /* Compute the scope prefix associated with PDI's parent, in
8454 compilation unit CU. The result will be allocated on CU's
8455 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8456 field. NULL is returned if no prefix is necessary. */
8457 static const char *
8458 partial_die_parent_scope (struct partial_die_info *pdi,
8459 struct dwarf2_cu *cu)
8460 {
8461 const char *grandparent_scope;
8462 struct partial_die_info *parent, *real_pdi;
8463
8464 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8465 then this means the parent of the specification DIE. */
8466
8467 real_pdi = pdi;
8468 while (real_pdi->has_specification)
8469 {
8470 auto res = find_partial_die (real_pdi->spec_offset,
8471 real_pdi->spec_is_dwz, cu);
8472 real_pdi = res.pdi;
8473 cu = res.cu;
8474 }
8475
8476 parent = real_pdi->die_parent;
8477 if (parent == NULL)
8478 return NULL;
8479
8480 if (parent->scope_set)
8481 return parent->scope;
8482
8483 parent->fixup (cu);
8484
8485 grandparent_scope = partial_die_parent_scope (parent, cu);
8486
8487 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8488 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8489 Work around this problem here. */
8490 if (cu->language == language_cplus
8491 && parent->tag == DW_TAG_namespace
8492 && strcmp (parent->name, "::") == 0
8493 && grandparent_scope == NULL)
8494 {
8495 parent->scope = NULL;
8496 parent->scope_set = 1;
8497 return NULL;
8498 }
8499
8500 /* Nested subroutines in Fortran get a prefix. */
8501 if (pdi->tag == DW_TAG_enumerator)
8502 /* Enumerators should not get the name of the enumeration as a prefix. */
8503 parent->scope = grandparent_scope;
8504 else if (parent->tag == DW_TAG_namespace
8505 || parent->tag == DW_TAG_module
8506 || parent->tag == DW_TAG_structure_type
8507 || parent->tag == DW_TAG_class_type
8508 || parent->tag == DW_TAG_interface_type
8509 || parent->tag == DW_TAG_union_type
8510 || parent->tag == DW_TAG_enumeration_type
8511 || (cu->language == language_fortran
8512 && parent->tag == DW_TAG_subprogram
8513 && pdi->tag == DW_TAG_subprogram))
8514 {
8515 if (grandparent_scope == NULL)
8516 parent->scope = parent->name;
8517 else
8518 parent->scope = typename_concat (&cu->comp_unit_obstack,
8519 grandparent_scope,
8520 parent->name, 0, cu);
8521 }
8522 else
8523 {
8524 /* FIXME drow/2004-04-01: What should we be doing with
8525 function-local names? For partial symbols, we should probably be
8526 ignoring them. */
8527 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8528 dwarf_tag_name (parent->tag),
8529 sect_offset_str (pdi->sect_off));
8530 parent->scope = grandparent_scope;
8531 }
8532
8533 parent->scope_set = 1;
8534 return parent->scope;
8535 }
8536
8537 /* Return the fully scoped name associated with PDI, from compilation unit
8538 CU. The result will be allocated with malloc. */
8539
8540 static gdb::unique_xmalloc_ptr<char>
8541 partial_die_full_name (struct partial_die_info *pdi,
8542 struct dwarf2_cu *cu)
8543 {
8544 const char *parent_scope;
8545
8546 /* If this is a template instantiation, we can not work out the
8547 template arguments from partial DIEs. So, unfortunately, we have
8548 to go through the full DIEs. At least any work we do building
8549 types here will be reused if full symbols are loaded later. */
8550 if (pdi->has_template_arguments)
8551 {
8552 pdi->fixup (cu);
8553
8554 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8555 {
8556 struct die_info *die;
8557 struct attribute attr;
8558 struct dwarf2_cu *ref_cu = cu;
8559
8560 /* DW_FORM_ref_addr is using section offset. */
8561 attr.name = (enum dwarf_attribute) 0;
8562 attr.form = DW_FORM_ref_addr;
8563 attr.u.unsnd = to_underlying (pdi->sect_off);
8564 die = follow_die_ref (NULL, &attr, &ref_cu);
8565
8566 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8567 }
8568 }
8569
8570 parent_scope = partial_die_parent_scope (pdi, cu);
8571 if (parent_scope == NULL)
8572 return NULL;
8573 else
8574 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8575 pdi->name, 0, cu));
8576 }
8577
8578 static void
8579 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8580 {
8581 struct dwarf2_per_objfile *dwarf2_per_objfile
8582 = cu->per_cu->dwarf2_per_objfile;
8583 struct objfile *objfile = dwarf2_per_objfile->objfile;
8584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8585 CORE_ADDR addr = 0;
8586 const char *actual_name = NULL;
8587 CORE_ADDR baseaddr;
8588
8589 baseaddr = objfile->text_section_offset ();
8590
8591 gdb::unique_xmalloc_ptr<char> built_actual_name
8592 = partial_die_full_name (pdi, cu);
8593 if (built_actual_name != NULL)
8594 actual_name = built_actual_name.get ();
8595
8596 if (actual_name == NULL)
8597 actual_name = pdi->name;
8598
8599 switch (pdi->tag)
8600 {
8601 case DW_TAG_inlined_subroutine:
8602 case DW_TAG_subprogram:
8603 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8604 - baseaddr);
8605 if (pdi->is_external
8606 || cu->language == language_ada
8607 || (cu->language == language_fortran
8608 && pdi->die_parent != NULL
8609 && pdi->die_parent->tag == DW_TAG_subprogram))
8610 {
8611 /* Normally, only "external" DIEs are part of the global scope.
8612 But in Ada and Fortran, we want to be able to access nested
8613 procedures globally. So all Ada and Fortran subprograms are
8614 stored in the global scope. */
8615 add_psymbol_to_list (actual_name,
8616 built_actual_name != NULL,
8617 VAR_DOMAIN, LOC_BLOCK,
8618 SECT_OFF_TEXT (objfile),
8619 psymbol_placement::GLOBAL,
8620 addr,
8621 cu->language, objfile);
8622 }
8623 else
8624 {
8625 add_psymbol_to_list (actual_name,
8626 built_actual_name != NULL,
8627 VAR_DOMAIN, LOC_BLOCK,
8628 SECT_OFF_TEXT (objfile),
8629 psymbol_placement::STATIC,
8630 addr, cu->language, objfile);
8631 }
8632
8633 if (pdi->main_subprogram && actual_name != NULL)
8634 set_objfile_main_name (objfile, actual_name, cu->language);
8635 break;
8636 case DW_TAG_constant:
8637 add_psymbol_to_list (actual_name,
8638 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8639 -1, (pdi->is_external
8640 ? psymbol_placement::GLOBAL
8641 : psymbol_placement::STATIC),
8642 0, cu->language, objfile);
8643 break;
8644 case DW_TAG_variable:
8645 if (pdi->d.locdesc)
8646 addr = decode_locdesc (pdi->d.locdesc, cu);
8647
8648 if (pdi->d.locdesc
8649 && addr == 0
8650 && !dwarf2_per_objfile->has_section_at_zero)
8651 {
8652 /* A global or static variable may also have been stripped
8653 out by the linker if unused, in which case its address
8654 will be nullified; do not add such variables into partial
8655 symbol table then. */
8656 }
8657 else if (pdi->is_external)
8658 {
8659 /* Global Variable.
8660 Don't enter into the minimal symbol tables as there is
8661 a minimal symbol table entry from the ELF symbols already.
8662 Enter into partial symbol table if it has a location
8663 descriptor or a type.
8664 If the location descriptor is missing, new_symbol will create
8665 a LOC_UNRESOLVED symbol, the address of the variable will then
8666 be determined from the minimal symbol table whenever the variable
8667 is referenced.
8668 The address for the partial symbol table entry is not
8669 used by GDB, but it comes in handy for debugging partial symbol
8670 table building. */
8671
8672 if (pdi->d.locdesc || pdi->has_type)
8673 add_psymbol_to_list (actual_name,
8674 built_actual_name != NULL,
8675 VAR_DOMAIN, LOC_STATIC,
8676 SECT_OFF_TEXT (objfile),
8677 psymbol_placement::GLOBAL,
8678 addr, cu->language, objfile);
8679 }
8680 else
8681 {
8682 int has_loc = pdi->d.locdesc != NULL;
8683
8684 /* Static Variable. Skip symbols whose value we cannot know (those
8685 without location descriptors or constant values). */
8686 if (!has_loc && !pdi->has_const_value)
8687 return;
8688
8689 add_psymbol_to_list (actual_name,
8690 built_actual_name != NULL,
8691 VAR_DOMAIN, LOC_STATIC,
8692 SECT_OFF_TEXT (objfile),
8693 psymbol_placement::STATIC,
8694 has_loc ? addr : 0,
8695 cu->language, objfile);
8696 }
8697 break;
8698 case DW_TAG_typedef:
8699 case DW_TAG_base_type:
8700 case DW_TAG_subrange_type:
8701 add_psymbol_to_list (actual_name,
8702 built_actual_name != NULL,
8703 VAR_DOMAIN, LOC_TYPEDEF, -1,
8704 psymbol_placement::STATIC,
8705 0, cu->language, objfile);
8706 break;
8707 case DW_TAG_imported_declaration:
8708 case DW_TAG_namespace:
8709 add_psymbol_to_list (actual_name,
8710 built_actual_name != NULL,
8711 VAR_DOMAIN, LOC_TYPEDEF, -1,
8712 psymbol_placement::GLOBAL,
8713 0, cu->language, objfile);
8714 break;
8715 case DW_TAG_module:
8716 /* With Fortran 77 there might be a "BLOCK DATA" module
8717 available without any name. If so, we skip the module as it
8718 doesn't bring any value. */
8719 if (actual_name != nullptr)
8720 add_psymbol_to_list (actual_name,
8721 built_actual_name != NULL,
8722 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8723 psymbol_placement::GLOBAL,
8724 0, cu->language, objfile);
8725 break;
8726 case DW_TAG_class_type:
8727 case DW_TAG_interface_type:
8728 case DW_TAG_structure_type:
8729 case DW_TAG_union_type:
8730 case DW_TAG_enumeration_type:
8731 /* Skip external references. The DWARF standard says in the section
8732 about "Structure, Union, and Class Type Entries": "An incomplete
8733 structure, union or class type is represented by a structure,
8734 union or class entry that does not have a byte size attribute
8735 and that has a DW_AT_declaration attribute." */
8736 if (!pdi->has_byte_size && pdi->is_declaration)
8737 return;
8738
8739 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8740 static vs. global. */
8741 add_psymbol_to_list (actual_name,
8742 built_actual_name != NULL,
8743 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8744 cu->language == language_cplus
8745 ? psymbol_placement::GLOBAL
8746 : psymbol_placement::STATIC,
8747 0, cu->language, objfile);
8748
8749 break;
8750 case DW_TAG_enumerator:
8751 add_psymbol_to_list (actual_name,
8752 built_actual_name != NULL,
8753 VAR_DOMAIN, LOC_CONST, -1,
8754 cu->language == language_cplus
8755 ? psymbol_placement::GLOBAL
8756 : psymbol_placement::STATIC,
8757 0, cu->language, objfile);
8758 break;
8759 default:
8760 break;
8761 }
8762 }
8763
8764 /* Read a partial die corresponding to a namespace; also, add a symbol
8765 corresponding to that namespace to the symbol table. NAMESPACE is
8766 the name of the enclosing namespace. */
8767
8768 static void
8769 add_partial_namespace (struct partial_die_info *pdi,
8770 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8771 int set_addrmap, struct dwarf2_cu *cu)
8772 {
8773 /* Add a symbol for the namespace. */
8774
8775 add_partial_symbol (pdi, cu);
8776
8777 /* Now scan partial symbols in that namespace. */
8778
8779 if (pdi->has_children)
8780 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8781 }
8782
8783 /* Read a partial die corresponding to a Fortran module. */
8784
8785 static void
8786 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8787 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8788 {
8789 /* Add a symbol for the namespace. */
8790
8791 add_partial_symbol (pdi, cu);
8792
8793 /* Now scan partial symbols in that module. */
8794
8795 if (pdi->has_children)
8796 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8797 }
8798
8799 /* Read a partial die corresponding to a subprogram or an inlined
8800 subprogram and create a partial symbol for that subprogram.
8801 When the CU language allows it, this routine also defines a partial
8802 symbol for each nested subprogram that this subprogram contains.
8803 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8804 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8805
8806 PDI may also be a lexical block, in which case we simply search
8807 recursively for subprograms defined inside that lexical block.
8808 Again, this is only performed when the CU language allows this
8809 type of definitions. */
8810
8811 static void
8812 add_partial_subprogram (struct partial_die_info *pdi,
8813 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8814 int set_addrmap, struct dwarf2_cu *cu)
8815 {
8816 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8817 {
8818 if (pdi->has_pc_info)
8819 {
8820 if (pdi->lowpc < *lowpc)
8821 *lowpc = pdi->lowpc;
8822 if (pdi->highpc > *highpc)
8823 *highpc = pdi->highpc;
8824 if (set_addrmap)
8825 {
8826 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8828 CORE_ADDR baseaddr;
8829 CORE_ADDR this_highpc;
8830 CORE_ADDR this_lowpc;
8831
8832 baseaddr = objfile->text_section_offset ();
8833 this_lowpc
8834 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8835 pdi->lowpc + baseaddr)
8836 - baseaddr);
8837 this_highpc
8838 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8839 pdi->highpc + baseaddr)
8840 - baseaddr);
8841 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8842 this_lowpc, this_highpc - 1,
8843 cu->per_cu->v.psymtab);
8844 }
8845 }
8846
8847 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8848 {
8849 if (!pdi->is_declaration)
8850 /* Ignore subprogram DIEs that do not have a name, they are
8851 illegal. Do not emit a complaint at this point, we will
8852 do so when we convert this psymtab into a symtab. */
8853 if (pdi->name)
8854 add_partial_symbol (pdi, cu);
8855 }
8856 }
8857
8858 if (! pdi->has_children)
8859 return;
8860
8861 if (cu->language == language_ada || cu->language == language_fortran)
8862 {
8863 pdi = pdi->die_child;
8864 while (pdi != NULL)
8865 {
8866 pdi->fixup (cu);
8867 if (pdi->tag == DW_TAG_subprogram
8868 || pdi->tag == DW_TAG_inlined_subroutine
8869 || pdi->tag == DW_TAG_lexical_block)
8870 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8871 pdi = pdi->die_sibling;
8872 }
8873 }
8874 }
8875
8876 /* Read a partial die corresponding to an enumeration type. */
8877
8878 static void
8879 add_partial_enumeration (struct partial_die_info *enum_pdi,
8880 struct dwarf2_cu *cu)
8881 {
8882 struct partial_die_info *pdi;
8883
8884 if (enum_pdi->name != NULL)
8885 add_partial_symbol (enum_pdi, cu);
8886
8887 pdi = enum_pdi->die_child;
8888 while (pdi)
8889 {
8890 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8891 complaint (_("malformed enumerator DIE ignored"));
8892 else
8893 add_partial_symbol (pdi, cu);
8894 pdi = pdi->die_sibling;
8895 }
8896 }
8897
8898 /* Return the initial uleb128 in the die at INFO_PTR. */
8899
8900 static unsigned int
8901 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8902 {
8903 unsigned int bytes_read;
8904
8905 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8906 }
8907
8908 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8909 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8910
8911 Return the corresponding abbrev, or NULL if the number is zero (indicating
8912 an empty DIE). In either case *BYTES_READ will be set to the length of
8913 the initial number. */
8914
8915 static struct abbrev_info *
8916 peek_die_abbrev (const die_reader_specs &reader,
8917 const gdb_byte *info_ptr, unsigned int *bytes_read)
8918 {
8919 dwarf2_cu *cu = reader.cu;
8920 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8921 unsigned int abbrev_number
8922 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8923
8924 if (abbrev_number == 0)
8925 return NULL;
8926
8927 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8928 if (!abbrev)
8929 {
8930 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8931 " at offset %s [in module %s]"),
8932 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8933 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8934 }
8935
8936 return abbrev;
8937 }
8938
8939 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8940 Returns a pointer to the end of a series of DIEs, terminated by an empty
8941 DIE. Any children of the skipped DIEs will also be skipped. */
8942
8943 static const gdb_byte *
8944 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8945 {
8946 while (1)
8947 {
8948 unsigned int bytes_read;
8949 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8950
8951 if (abbrev == NULL)
8952 return info_ptr + bytes_read;
8953 else
8954 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8955 }
8956 }
8957
8958 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8959 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8960 abbrev corresponding to that skipped uleb128 should be passed in
8961 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8962 children. */
8963
8964 static const gdb_byte *
8965 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8966 struct abbrev_info *abbrev)
8967 {
8968 unsigned int bytes_read;
8969 struct attribute attr;
8970 bfd *abfd = reader->abfd;
8971 struct dwarf2_cu *cu = reader->cu;
8972 const gdb_byte *buffer = reader->buffer;
8973 const gdb_byte *buffer_end = reader->buffer_end;
8974 unsigned int form, i;
8975
8976 for (i = 0; i < abbrev->num_attrs; i++)
8977 {
8978 /* The only abbrev we care about is DW_AT_sibling. */
8979 if (abbrev->attrs[i].name == DW_AT_sibling)
8980 {
8981 bool ignored;
8982 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8983 &ignored);
8984 if (attr.form == DW_FORM_ref_addr)
8985 complaint (_("ignoring absolute DW_AT_sibling"));
8986 else
8987 {
8988 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8989 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8990
8991 if (sibling_ptr < info_ptr)
8992 complaint (_("DW_AT_sibling points backwards"));
8993 else if (sibling_ptr > reader->buffer_end)
8994 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8995 else
8996 return sibling_ptr;
8997 }
8998 }
8999
9000 /* If it isn't DW_AT_sibling, skip this attribute. */
9001 form = abbrev->attrs[i].form;
9002 skip_attribute:
9003 switch (form)
9004 {
9005 case DW_FORM_ref_addr:
9006 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9007 and later it is offset sized. */
9008 if (cu->header.version == 2)
9009 info_ptr += cu->header.addr_size;
9010 else
9011 info_ptr += cu->header.offset_size;
9012 break;
9013 case DW_FORM_GNU_ref_alt:
9014 info_ptr += cu->header.offset_size;
9015 break;
9016 case DW_FORM_addr:
9017 info_ptr += cu->header.addr_size;
9018 break;
9019 case DW_FORM_data1:
9020 case DW_FORM_ref1:
9021 case DW_FORM_flag:
9022 case DW_FORM_strx1:
9023 info_ptr += 1;
9024 break;
9025 case DW_FORM_flag_present:
9026 case DW_FORM_implicit_const:
9027 break;
9028 case DW_FORM_data2:
9029 case DW_FORM_ref2:
9030 case DW_FORM_strx2:
9031 info_ptr += 2;
9032 break;
9033 case DW_FORM_strx3:
9034 info_ptr += 3;
9035 break;
9036 case DW_FORM_data4:
9037 case DW_FORM_ref4:
9038 case DW_FORM_strx4:
9039 info_ptr += 4;
9040 break;
9041 case DW_FORM_data8:
9042 case DW_FORM_ref8:
9043 case DW_FORM_ref_sig8:
9044 info_ptr += 8;
9045 break;
9046 case DW_FORM_data16:
9047 info_ptr += 16;
9048 break;
9049 case DW_FORM_string:
9050 read_direct_string (abfd, info_ptr, &bytes_read);
9051 info_ptr += bytes_read;
9052 break;
9053 case DW_FORM_sec_offset:
9054 case DW_FORM_strp:
9055 case DW_FORM_GNU_strp_alt:
9056 info_ptr += cu->header.offset_size;
9057 break;
9058 case DW_FORM_exprloc:
9059 case DW_FORM_block:
9060 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9061 info_ptr += bytes_read;
9062 break;
9063 case DW_FORM_block1:
9064 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9065 break;
9066 case DW_FORM_block2:
9067 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9068 break;
9069 case DW_FORM_block4:
9070 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9071 break;
9072 case DW_FORM_addrx:
9073 case DW_FORM_strx:
9074 case DW_FORM_sdata:
9075 case DW_FORM_udata:
9076 case DW_FORM_ref_udata:
9077 case DW_FORM_GNU_addr_index:
9078 case DW_FORM_GNU_str_index:
9079 case DW_FORM_rnglistx:
9080 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9081 break;
9082 case DW_FORM_indirect:
9083 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9084 info_ptr += bytes_read;
9085 /* We need to continue parsing from here, so just go back to
9086 the top. */
9087 goto skip_attribute;
9088
9089 default:
9090 error (_("Dwarf Error: Cannot handle %s "
9091 "in DWARF reader [in module %s]"),
9092 dwarf_form_name (form),
9093 bfd_get_filename (abfd));
9094 }
9095 }
9096
9097 if (abbrev->has_children)
9098 return skip_children (reader, info_ptr);
9099 else
9100 return info_ptr;
9101 }
9102
9103 /* Locate ORIG_PDI's sibling.
9104 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9105
9106 static const gdb_byte *
9107 locate_pdi_sibling (const struct die_reader_specs *reader,
9108 struct partial_die_info *orig_pdi,
9109 const gdb_byte *info_ptr)
9110 {
9111 /* Do we know the sibling already? */
9112
9113 if (orig_pdi->sibling)
9114 return orig_pdi->sibling;
9115
9116 /* Are there any children to deal with? */
9117
9118 if (!orig_pdi->has_children)
9119 return info_ptr;
9120
9121 /* Skip the children the long way. */
9122
9123 return skip_children (reader, info_ptr);
9124 }
9125
9126 /* Expand this partial symbol table into a full symbol table. SELF is
9127 not NULL. */
9128
9129 void
9130 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9131 {
9132 struct dwarf2_per_objfile *dwarf2_per_objfile
9133 = get_dwarf2_per_objfile (objfile);
9134
9135 gdb_assert (!readin);
9136 /* If this psymtab is constructed from a debug-only objfile, the
9137 has_section_at_zero flag will not necessarily be correct. We
9138 can get the correct value for this flag by looking at the data
9139 associated with the (presumably stripped) associated objfile. */
9140 if (objfile->separate_debug_objfile_backlink)
9141 {
9142 struct dwarf2_per_objfile *dpo_backlink
9143 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9144
9145 dwarf2_per_objfile->has_section_at_zero
9146 = dpo_backlink->has_section_at_zero;
9147 }
9148
9149 dwarf2_per_objfile->reading_partial_symbols = 0;
9150
9151 expand_psymtab (objfile);
9152
9153 process_cu_includes (dwarf2_per_objfile);
9154 }
9155 \f
9156 /* Reading in full CUs. */
9157
9158 /* Add PER_CU to the queue. */
9159
9160 static void
9161 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9162 enum language pretend_language)
9163 {
9164 struct dwarf2_queue_item *item;
9165
9166 per_cu->queued = 1;
9167 item = XNEW (struct dwarf2_queue_item);
9168 item->per_cu = per_cu;
9169 item->pretend_language = pretend_language;
9170 item->next = NULL;
9171
9172 if (dwarf2_queue == NULL)
9173 dwarf2_queue = item;
9174 else
9175 dwarf2_queue_tail->next = item;
9176
9177 dwarf2_queue_tail = item;
9178 }
9179
9180 /* If PER_CU is not yet queued, add it to the queue.
9181 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9182 dependency.
9183 The result is non-zero if PER_CU was queued, otherwise the result is zero
9184 meaning either PER_CU is already queued or it is already loaded.
9185
9186 N.B. There is an invariant here that if a CU is queued then it is loaded.
9187 The caller is required to load PER_CU if we return non-zero. */
9188
9189 static int
9190 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9191 struct dwarf2_per_cu_data *per_cu,
9192 enum language pretend_language)
9193 {
9194 /* We may arrive here during partial symbol reading, if we need full
9195 DIEs to process an unusual case (e.g. template arguments). Do
9196 not queue PER_CU, just tell our caller to load its DIEs. */
9197 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9198 {
9199 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9200 return 1;
9201 return 0;
9202 }
9203
9204 /* Mark the dependence relation so that we don't flush PER_CU
9205 too early. */
9206 if (dependent_cu != NULL)
9207 dwarf2_add_dependence (dependent_cu, per_cu);
9208
9209 /* If it's already on the queue, we have nothing to do. */
9210 if (per_cu->queued)
9211 return 0;
9212
9213 /* If the compilation unit is already loaded, just mark it as
9214 used. */
9215 if (per_cu->cu != NULL)
9216 {
9217 per_cu->cu->last_used = 0;
9218 return 0;
9219 }
9220
9221 /* Add it to the queue. */
9222 queue_comp_unit (per_cu, pretend_language);
9223
9224 return 1;
9225 }
9226
9227 /* Process the queue. */
9228
9229 static void
9230 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9231 {
9232 struct dwarf2_queue_item *item, *next_item;
9233
9234 if (dwarf_read_debug)
9235 {
9236 fprintf_unfiltered (gdb_stdlog,
9237 "Expanding one or more symtabs of objfile %s ...\n",
9238 objfile_name (dwarf2_per_objfile->objfile));
9239 }
9240
9241 /* The queue starts out with one item, but following a DIE reference
9242 may load a new CU, adding it to the end of the queue. */
9243 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9244 {
9245 if ((dwarf2_per_objfile->using_index
9246 ? !item->per_cu->v.quick->compunit_symtab
9247 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9248 /* Skip dummy CUs. */
9249 && item->per_cu->cu != NULL)
9250 {
9251 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9252 unsigned int debug_print_threshold;
9253 char buf[100];
9254
9255 if (per_cu->is_debug_types)
9256 {
9257 struct signatured_type *sig_type =
9258 (struct signatured_type *) per_cu;
9259
9260 sprintf (buf, "TU %s at offset %s",
9261 hex_string (sig_type->signature),
9262 sect_offset_str (per_cu->sect_off));
9263 /* There can be 100s of TUs.
9264 Only print them in verbose mode. */
9265 debug_print_threshold = 2;
9266 }
9267 else
9268 {
9269 sprintf (buf, "CU at offset %s",
9270 sect_offset_str (per_cu->sect_off));
9271 debug_print_threshold = 1;
9272 }
9273
9274 if (dwarf_read_debug >= debug_print_threshold)
9275 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9276
9277 if (per_cu->is_debug_types)
9278 process_full_type_unit (per_cu, item->pretend_language);
9279 else
9280 process_full_comp_unit (per_cu, item->pretend_language);
9281
9282 if (dwarf_read_debug >= debug_print_threshold)
9283 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9284 }
9285
9286 item->per_cu->queued = 0;
9287 next_item = item->next;
9288 xfree (item);
9289 }
9290
9291 dwarf2_queue_tail = NULL;
9292
9293 if (dwarf_read_debug)
9294 {
9295 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9296 objfile_name (dwarf2_per_objfile->objfile));
9297 }
9298 }
9299
9300 /* Read in full symbols for PST, and anything it depends on. */
9301
9302 void
9303 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9304 {
9305 struct dwarf2_per_cu_data *per_cu;
9306
9307 if (readin)
9308 return;
9309
9310 read_dependencies (objfile);
9311
9312 per_cu = per_cu_data;
9313
9314 if (per_cu == NULL)
9315 {
9316 /* It's an include file, no symbols to read for it.
9317 Everything is in the parent symtab. */
9318 readin = true;
9319 return;
9320 }
9321
9322 dw2_do_instantiate_symtab (per_cu, false);
9323 }
9324
9325 /* Trivial hash function for die_info: the hash value of a DIE
9326 is its offset in .debug_info for this objfile. */
9327
9328 static hashval_t
9329 die_hash (const void *item)
9330 {
9331 const struct die_info *die = (const struct die_info *) item;
9332
9333 return to_underlying (die->sect_off);
9334 }
9335
9336 /* Trivial comparison function for die_info structures: two DIEs
9337 are equal if they have the same offset. */
9338
9339 static int
9340 die_eq (const void *item_lhs, const void *item_rhs)
9341 {
9342 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9343 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9344
9345 return die_lhs->sect_off == die_rhs->sect_off;
9346 }
9347
9348 /* Load the DIEs associated with PER_CU into memory. */
9349
9350 static void
9351 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9352 bool skip_partial,
9353 enum language pretend_language)
9354 {
9355 gdb_assert (! this_cu->is_debug_types);
9356
9357 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9358 if (reader.dummy_p)
9359 return;
9360
9361 struct dwarf2_cu *cu = reader.cu;
9362 const gdb_byte *info_ptr = reader.info_ptr;
9363
9364 gdb_assert (cu->die_hash == NULL);
9365 cu->die_hash =
9366 htab_create_alloc_ex (cu->header.length / 12,
9367 die_hash,
9368 die_eq,
9369 NULL,
9370 &cu->comp_unit_obstack,
9371 hashtab_obstack_allocate,
9372 dummy_obstack_deallocate);
9373
9374 if (reader.comp_unit_die->has_children)
9375 reader.comp_unit_die->child
9376 = read_die_and_siblings (&reader, reader.info_ptr,
9377 &info_ptr, reader.comp_unit_die);
9378 cu->dies = reader.comp_unit_die;
9379 /* comp_unit_die is not stored in die_hash, no need. */
9380
9381 /* We try not to read any attributes in this function, because not
9382 all CUs needed for references have been loaded yet, and symbol
9383 table processing isn't initialized. But we have to set the CU language,
9384 or we won't be able to build types correctly.
9385 Similarly, if we do not read the producer, we can not apply
9386 producer-specific interpretation. */
9387 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9388 }
9389
9390 /* Add a DIE to the delayed physname list. */
9391
9392 static void
9393 add_to_method_list (struct type *type, int fnfield_index, int index,
9394 const char *name, struct die_info *die,
9395 struct dwarf2_cu *cu)
9396 {
9397 struct delayed_method_info mi;
9398 mi.type = type;
9399 mi.fnfield_index = fnfield_index;
9400 mi.index = index;
9401 mi.name = name;
9402 mi.die = die;
9403 cu->method_list.push_back (mi);
9404 }
9405
9406 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9407 "const" / "volatile". If so, decrements LEN by the length of the
9408 modifier and return true. Otherwise return false. */
9409
9410 template<size_t N>
9411 static bool
9412 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9413 {
9414 size_t mod_len = sizeof (mod) - 1;
9415 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9416 {
9417 len -= mod_len;
9418 return true;
9419 }
9420 return false;
9421 }
9422
9423 /* Compute the physnames of any methods on the CU's method list.
9424
9425 The computation of method physnames is delayed in order to avoid the
9426 (bad) condition that one of the method's formal parameters is of an as yet
9427 incomplete type. */
9428
9429 static void
9430 compute_delayed_physnames (struct dwarf2_cu *cu)
9431 {
9432 /* Only C++ delays computing physnames. */
9433 if (cu->method_list.empty ())
9434 return;
9435 gdb_assert (cu->language == language_cplus);
9436
9437 for (const delayed_method_info &mi : cu->method_list)
9438 {
9439 const char *physname;
9440 struct fn_fieldlist *fn_flp
9441 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9442 physname = dwarf2_physname (mi.name, mi.die, cu);
9443 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9444 = physname ? physname : "";
9445
9446 /* Since there's no tag to indicate whether a method is a
9447 const/volatile overload, extract that information out of the
9448 demangled name. */
9449 if (physname != NULL)
9450 {
9451 size_t len = strlen (physname);
9452
9453 while (1)
9454 {
9455 if (physname[len] == ')') /* shortcut */
9456 break;
9457 else if (check_modifier (physname, len, " const"))
9458 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9459 else if (check_modifier (physname, len, " volatile"))
9460 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9461 else
9462 break;
9463 }
9464 }
9465 }
9466
9467 /* The list is no longer needed. */
9468 cu->method_list.clear ();
9469 }
9470
9471 /* Go objects should be embedded in a DW_TAG_module DIE,
9472 and it's not clear if/how imported objects will appear.
9473 To keep Go support simple until that's worked out,
9474 go back through what we've read and create something usable.
9475 We could do this while processing each DIE, and feels kinda cleaner,
9476 but that way is more invasive.
9477 This is to, for example, allow the user to type "p var" or "b main"
9478 without having to specify the package name, and allow lookups
9479 of module.object to work in contexts that use the expression
9480 parser. */
9481
9482 static void
9483 fixup_go_packaging (struct dwarf2_cu *cu)
9484 {
9485 gdb::unique_xmalloc_ptr<char> package_name;
9486 struct pending *list;
9487 int i;
9488
9489 for (list = *cu->get_builder ()->get_global_symbols ();
9490 list != NULL;
9491 list = list->next)
9492 {
9493 for (i = 0; i < list->nsyms; ++i)
9494 {
9495 struct symbol *sym = list->symbol[i];
9496
9497 if (sym->language () == language_go
9498 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9499 {
9500 gdb::unique_xmalloc_ptr<char> this_package_name
9501 (go_symbol_package_name (sym));
9502
9503 if (this_package_name == NULL)
9504 continue;
9505 if (package_name == NULL)
9506 package_name = std::move (this_package_name);
9507 else
9508 {
9509 struct objfile *objfile
9510 = cu->per_cu->dwarf2_per_objfile->objfile;
9511 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9512 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9513 (symbol_symtab (sym) != NULL
9514 ? symtab_to_filename_for_display
9515 (symbol_symtab (sym))
9516 : objfile_name (objfile)),
9517 this_package_name.get (), package_name.get ());
9518 }
9519 }
9520 }
9521 }
9522
9523 if (package_name != NULL)
9524 {
9525 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9526 const char *saved_package_name
9527 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9528 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9529 saved_package_name);
9530 struct symbol *sym;
9531
9532 sym = allocate_symbol (objfile);
9533 sym->set_language (language_go, &objfile->objfile_obstack);
9534 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9535 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9536 e.g., "main" finds the "main" module and not C's main(). */
9537 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9538 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9539 SYMBOL_TYPE (sym) = type;
9540
9541 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9542 }
9543 }
9544
9545 /* Allocate a fully-qualified name consisting of the two parts on the
9546 obstack. */
9547
9548 static const char *
9549 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9550 {
9551 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9552 }
9553
9554 /* A helper that allocates a struct discriminant_info to attach to a
9555 union type. */
9556
9557 static struct discriminant_info *
9558 alloc_discriminant_info (struct type *type, int discriminant_index,
9559 int default_index)
9560 {
9561 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9562 gdb_assert (discriminant_index == -1
9563 || (discriminant_index >= 0
9564 && discriminant_index < TYPE_NFIELDS (type)));
9565 gdb_assert (default_index == -1
9566 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9567
9568 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9569
9570 struct discriminant_info *disc
9571 = ((struct discriminant_info *)
9572 TYPE_ZALLOC (type,
9573 offsetof (struct discriminant_info, discriminants)
9574 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9575 disc->default_index = default_index;
9576 disc->discriminant_index = discriminant_index;
9577
9578 struct dynamic_prop prop;
9579 prop.kind = PROP_UNDEFINED;
9580 prop.data.baton = disc;
9581
9582 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9583
9584 return disc;
9585 }
9586
9587 /* Some versions of rustc emitted enums in an unusual way.
9588
9589 Ordinary enums were emitted as unions. The first element of each
9590 structure in the union was named "RUST$ENUM$DISR". This element
9591 held the discriminant.
9592
9593 These versions of Rust also implemented the "non-zero"
9594 optimization. When the enum had two values, and one is empty and
9595 the other holds a pointer that cannot be zero, the pointer is used
9596 as the discriminant, with a zero value meaning the empty variant.
9597 Here, the union's first member is of the form
9598 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9599 where the fieldnos are the indices of the fields that should be
9600 traversed in order to find the field (which may be several fields deep)
9601 and the variantname is the name of the variant of the case when the
9602 field is zero.
9603
9604 This function recognizes whether TYPE is of one of these forms,
9605 and, if so, smashes it to be a variant type. */
9606
9607 static void
9608 quirk_rust_enum (struct type *type, struct objfile *objfile)
9609 {
9610 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9611
9612 /* We don't need to deal with empty enums. */
9613 if (TYPE_NFIELDS (type) == 0)
9614 return;
9615
9616 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9617 if (TYPE_NFIELDS (type) == 1
9618 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9619 {
9620 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9621
9622 /* Decode the field name to find the offset of the
9623 discriminant. */
9624 ULONGEST bit_offset = 0;
9625 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9626 while (name[0] >= '0' && name[0] <= '9')
9627 {
9628 char *tail;
9629 unsigned long index = strtoul (name, &tail, 10);
9630 name = tail;
9631 if (*name != '$'
9632 || index >= TYPE_NFIELDS (field_type)
9633 || (TYPE_FIELD_LOC_KIND (field_type, index)
9634 != FIELD_LOC_KIND_BITPOS))
9635 {
9636 complaint (_("Could not parse Rust enum encoding string \"%s\""
9637 "[in module %s]"),
9638 TYPE_FIELD_NAME (type, 0),
9639 objfile_name (objfile));
9640 return;
9641 }
9642 ++name;
9643
9644 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9645 field_type = TYPE_FIELD_TYPE (field_type, index);
9646 }
9647
9648 /* Make a union to hold the variants. */
9649 struct type *union_type = alloc_type (objfile);
9650 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9651 TYPE_NFIELDS (union_type) = 3;
9652 TYPE_FIELDS (union_type)
9653 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9654 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9655 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9656
9657 /* Put the discriminant must at index 0. */
9658 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9659 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9660 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9661 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9662
9663 /* The order of fields doesn't really matter, so put the real
9664 field at index 1 and the data-less field at index 2. */
9665 struct discriminant_info *disc
9666 = alloc_discriminant_info (union_type, 0, 1);
9667 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9668 TYPE_FIELD_NAME (union_type, 1)
9669 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9670 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9671 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9672 TYPE_FIELD_NAME (union_type, 1));
9673
9674 const char *dataless_name
9675 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9676 name);
9677 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9678 dataless_name);
9679 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9680 /* NAME points into the original discriminant name, which
9681 already has the correct lifetime. */
9682 TYPE_FIELD_NAME (union_type, 2) = name;
9683 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9684 disc->discriminants[2] = 0;
9685
9686 /* Smash this type to be a structure type. We have to do this
9687 because the type has already been recorded. */
9688 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9689 TYPE_NFIELDS (type) = 1;
9690 TYPE_FIELDS (type)
9691 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9692
9693 /* Install the variant part. */
9694 TYPE_FIELD_TYPE (type, 0) = union_type;
9695 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9696 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9697 }
9698 /* A union with a single anonymous field is probably an old-style
9699 univariant enum. */
9700 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9701 {
9702 /* Smash this type to be a structure type. We have to do this
9703 because the type has already been recorded. */
9704 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9705
9706 /* Make a union to hold the variants. */
9707 struct type *union_type = alloc_type (objfile);
9708 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9709 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9710 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9711 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9712 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9713
9714 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9715 const char *variant_name
9716 = rust_last_path_segment (TYPE_NAME (field_type));
9717 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9718 TYPE_NAME (field_type)
9719 = rust_fully_qualify (&objfile->objfile_obstack,
9720 TYPE_NAME (type), variant_name);
9721
9722 /* Install the union in the outer struct type. */
9723 TYPE_NFIELDS (type) = 1;
9724 TYPE_FIELDS (type)
9725 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9726 TYPE_FIELD_TYPE (type, 0) = union_type;
9727 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9728 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9729
9730 alloc_discriminant_info (union_type, -1, 0);
9731 }
9732 else
9733 {
9734 struct type *disr_type = nullptr;
9735 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9736 {
9737 disr_type = TYPE_FIELD_TYPE (type, i);
9738
9739 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9740 {
9741 /* All fields of a true enum will be structs. */
9742 return;
9743 }
9744 else if (TYPE_NFIELDS (disr_type) == 0)
9745 {
9746 /* Could be data-less variant, so keep going. */
9747 disr_type = nullptr;
9748 }
9749 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9750 "RUST$ENUM$DISR") != 0)
9751 {
9752 /* Not a Rust enum. */
9753 return;
9754 }
9755 else
9756 {
9757 /* Found one. */
9758 break;
9759 }
9760 }
9761
9762 /* If we got here without a discriminant, then it's probably
9763 just a union. */
9764 if (disr_type == nullptr)
9765 return;
9766
9767 /* Smash this type to be a structure type. We have to do this
9768 because the type has already been recorded. */
9769 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9770
9771 /* Make a union to hold the variants. */
9772 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9773 struct type *union_type = alloc_type (objfile);
9774 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9775 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9776 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9777 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9778 TYPE_FIELDS (union_type)
9779 = (struct field *) TYPE_ZALLOC (union_type,
9780 (TYPE_NFIELDS (union_type)
9781 * sizeof (struct field)));
9782
9783 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9784 TYPE_NFIELDS (type) * sizeof (struct field));
9785
9786 /* Install the discriminant at index 0 in the union. */
9787 TYPE_FIELD (union_type, 0) = *disr_field;
9788 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9789 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9790
9791 /* Install the union in the outer struct type. */
9792 TYPE_FIELD_TYPE (type, 0) = union_type;
9793 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9794 TYPE_NFIELDS (type) = 1;
9795
9796 /* Set the size and offset of the union type. */
9797 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9798
9799 /* We need a way to find the correct discriminant given a
9800 variant name. For convenience we build a map here. */
9801 struct type *enum_type = FIELD_TYPE (*disr_field);
9802 std::unordered_map<std::string, ULONGEST> discriminant_map;
9803 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9804 {
9805 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9806 {
9807 const char *name
9808 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9809 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9810 }
9811 }
9812
9813 int n_fields = TYPE_NFIELDS (union_type);
9814 struct discriminant_info *disc
9815 = alloc_discriminant_info (union_type, 0, -1);
9816 /* Skip the discriminant here. */
9817 for (int i = 1; i < n_fields; ++i)
9818 {
9819 /* Find the final word in the name of this variant's type.
9820 That name can be used to look up the correct
9821 discriminant. */
9822 const char *variant_name
9823 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9824 i)));
9825
9826 auto iter = discriminant_map.find (variant_name);
9827 if (iter != discriminant_map.end ())
9828 disc->discriminants[i] = iter->second;
9829
9830 /* Remove the discriminant field, if it exists. */
9831 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9832 if (TYPE_NFIELDS (sub_type) > 0)
9833 {
9834 --TYPE_NFIELDS (sub_type);
9835 ++TYPE_FIELDS (sub_type);
9836 }
9837 TYPE_FIELD_NAME (union_type, i) = variant_name;
9838 TYPE_NAME (sub_type)
9839 = rust_fully_qualify (&objfile->objfile_obstack,
9840 TYPE_NAME (type), variant_name);
9841 }
9842 }
9843 }
9844
9845 /* Rewrite some Rust unions to be structures with variants parts. */
9846
9847 static void
9848 rust_union_quirks (struct dwarf2_cu *cu)
9849 {
9850 gdb_assert (cu->language == language_rust);
9851 for (type *type_ : cu->rust_unions)
9852 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9853 /* We don't need this any more. */
9854 cu->rust_unions.clear ();
9855 }
9856
9857 /* Return the symtab for PER_CU. This works properly regardless of
9858 whether we're using the index or psymtabs. */
9859
9860 static struct compunit_symtab *
9861 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9862 {
9863 return (per_cu->dwarf2_per_objfile->using_index
9864 ? per_cu->v.quick->compunit_symtab
9865 : per_cu->v.psymtab->compunit_symtab);
9866 }
9867
9868 /* A helper function for computing the list of all symbol tables
9869 included by PER_CU. */
9870
9871 static void
9872 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9873 htab_t all_children, htab_t all_type_symtabs,
9874 struct dwarf2_per_cu_data *per_cu,
9875 struct compunit_symtab *immediate_parent)
9876 {
9877 void **slot;
9878 struct compunit_symtab *cust;
9879
9880 slot = htab_find_slot (all_children, per_cu, INSERT);
9881 if (*slot != NULL)
9882 {
9883 /* This inclusion and its children have been processed. */
9884 return;
9885 }
9886
9887 *slot = per_cu;
9888 /* Only add a CU if it has a symbol table. */
9889 cust = get_compunit_symtab (per_cu);
9890 if (cust != NULL)
9891 {
9892 /* If this is a type unit only add its symbol table if we haven't
9893 seen it yet (type unit per_cu's can share symtabs). */
9894 if (per_cu->is_debug_types)
9895 {
9896 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9897 if (*slot == NULL)
9898 {
9899 *slot = cust;
9900 result->push_back (cust);
9901 if (cust->user == NULL)
9902 cust->user = immediate_parent;
9903 }
9904 }
9905 else
9906 {
9907 result->push_back (cust);
9908 if (cust->user == NULL)
9909 cust->user = immediate_parent;
9910 }
9911 }
9912
9913 if (!per_cu->imported_symtabs_empty ())
9914 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9915 {
9916 recursively_compute_inclusions (result, all_children,
9917 all_type_symtabs, ptr, cust);
9918 }
9919 }
9920
9921 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9922 PER_CU. */
9923
9924 static void
9925 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9926 {
9927 gdb_assert (! per_cu->is_debug_types);
9928
9929 if (!per_cu->imported_symtabs_empty ())
9930 {
9931 int len;
9932 std::vector<compunit_symtab *> result_symtabs;
9933 htab_t all_children, all_type_symtabs;
9934 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9935
9936 /* If we don't have a symtab, we can just skip this case. */
9937 if (cust == NULL)
9938 return;
9939
9940 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9941 NULL, xcalloc, xfree);
9942 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9943 NULL, xcalloc, xfree);
9944
9945 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9946 {
9947 recursively_compute_inclusions (&result_symtabs, all_children,
9948 all_type_symtabs, ptr, cust);
9949 }
9950
9951 /* Now we have a transitive closure of all the included symtabs. */
9952 len = result_symtabs.size ();
9953 cust->includes
9954 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9955 struct compunit_symtab *, len + 1);
9956 memcpy (cust->includes, result_symtabs.data (),
9957 len * sizeof (compunit_symtab *));
9958 cust->includes[len] = NULL;
9959
9960 htab_delete (all_children);
9961 htab_delete (all_type_symtabs);
9962 }
9963 }
9964
9965 /* Compute the 'includes' field for the symtabs of all the CUs we just
9966 read. */
9967
9968 static void
9969 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9970 {
9971 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9972 {
9973 if (! iter->is_debug_types)
9974 compute_compunit_symtab_includes (iter);
9975 }
9976
9977 dwarf2_per_objfile->just_read_cus.clear ();
9978 }
9979
9980 /* Generate full symbol information for PER_CU, whose DIEs have
9981 already been loaded into memory. */
9982
9983 static void
9984 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9985 enum language pretend_language)
9986 {
9987 struct dwarf2_cu *cu = per_cu->cu;
9988 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9989 struct objfile *objfile = dwarf2_per_objfile->objfile;
9990 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9991 CORE_ADDR lowpc, highpc;
9992 struct compunit_symtab *cust;
9993 CORE_ADDR baseaddr;
9994 struct block *static_block;
9995 CORE_ADDR addr;
9996
9997 baseaddr = objfile->text_section_offset ();
9998
9999 /* Clear the list here in case something was left over. */
10000 cu->method_list.clear ();
10001
10002 cu->language = pretend_language;
10003 cu->language_defn = language_def (cu->language);
10004
10005 /* Do line number decoding in read_file_scope () */
10006 process_die (cu->dies, cu);
10007
10008 /* For now fudge the Go package. */
10009 if (cu->language == language_go)
10010 fixup_go_packaging (cu);
10011
10012 /* Now that we have processed all the DIEs in the CU, all the types
10013 should be complete, and it should now be safe to compute all of the
10014 physnames. */
10015 compute_delayed_physnames (cu);
10016
10017 if (cu->language == language_rust)
10018 rust_union_quirks (cu);
10019
10020 /* Some compilers don't define a DW_AT_high_pc attribute for the
10021 compilation unit. If the DW_AT_high_pc is missing, synthesize
10022 it, by scanning the DIE's below the compilation unit. */
10023 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10024
10025 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10026 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10027
10028 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10029 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10030 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10031 addrmap to help ensure it has an accurate map of pc values belonging to
10032 this comp unit. */
10033 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10034
10035 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10036 SECT_OFF_TEXT (objfile),
10037 0);
10038
10039 if (cust != NULL)
10040 {
10041 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10042
10043 /* Set symtab language to language from DW_AT_language. If the
10044 compilation is from a C file generated by language preprocessors, do
10045 not set the language if it was already deduced by start_subfile. */
10046 if (!(cu->language == language_c
10047 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10048 COMPUNIT_FILETABS (cust)->language = cu->language;
10049
10050 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10051 produce DW_AT_location with location lists but it can be possibly
10052 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10053 there were bugs in prologue debug info, fixed later in GCC-4.5
10054 by "unwind info for epilogues" patch (which is not directly related).
10055
10056 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10057 needed, it would be wrong due to missing DW_AT_producer there.
10058
10059 Still one can confuse GDB by using non-standard GCC compilation
10060 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10061 */
10062 if (cu->has_loclist && gcc_4_minor >= 5)
10063 cust->locations_valid = 1;
10064
10065 if (gcc_4_minor >= 5)
10066 cust->epilogue_unwind_valid = 1;
10067
10068 cust->call_site_htab = cu->call_site_htab;
10069 }
10070
10071 if (dwarf2_per_objfile->using_index)
10072 per_cu->v.quick->compunit_symtab = cust;
10073 else
10074 {
10075 dwarf2_psymtab *pst = per_cu->v.psymtab;
10076 pst->compunit_symtab = cust;
10077 pst->readin = true;
10078 }
10079
10080 /* Push it for inclusion processing later. */
10081 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10082
10083 /* Not needed any more. */
10084 cu->reset_builder ();
10085 }
10086
10087 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10088 already been loaded into memory. */
10089
10090 static void
10091 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10092 enum language pretend_language)
10093 {
10094 struct dwarf2_cu *cu = per_cu->cu;
10095 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10096 struct objfile *objfile = dwarf2_per_objfile->objfile;
10097 struct compunit_symtab *cust;
10098 struct signatured_type *sig_type;
10099
10100 gdb_assert (per_cu->is_debug_types);
10101 sig_type = (struct signatured_type *) per_cu;
10102
10103 /* Clear the list here in case something was left over. */
10104 cu->method_list.clear ();
10105
10106 cu->language = pretend_language;
10107 cu->language_defn = language_def (cu->language);
10108
10109 /* The symbol tables are set up in read_type_unit_scope. */
10110 process_die (cu->dies, cu);
10111
10112 /* For now fudge the Go package. */
10113 if (cu->language == language_go)
10114 fixup_go_packaging (cu);
10115
10116 /* Now that we have processed all the DIEs in the CU, all the types
10117 should be complete, and it should now be safe to compute all of the
10118 physnames. */
10119 compute_delayed_physnames (cu);
10120
10121 if (cu->language == language_rust)
10122 rust_union_quirks (cu);
10123
10124 /* TUs share symbol tables.
10125 If this is the first TU to use this symtab, complete the construction
10126 of it with end_expandable_symtab. Otherwise, complete the addition of
10127 this TU's symbols to the existing symtab. */
10128 if (sig_type->type_unit_group->compunit_symtab == NULL)
10129 {
10130 buildsym_compunit *builder = cu->get_builder ();
10131 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10132 sig_type->type_unit_group->compunit_symtab = cust;
10133
10134 if (cust != NULL)
10135 {
10136 /* Set symtab language to language from DW_AT_language. If the
10137 compilation is from a C file generated by language preprocessors,
10138 do not set the language if it was already deduced by
10139 start_subfile. */
10140 if (!(cu->language == language_c
10141 && COMPUNIT_FILETABS (cust)->language != language_c))
10142 COMPUNIT_FILETABS (cust)->language = cu->language;
10143 }
10144 }
10145 else
10146 {
10147 cu->get_builder ()->augment_type_symtab ();
10148 cust = sig_type->type_unit_group->compunit_symtab;
10149 }
10150
10151 if (dwarf2_per_objfile->using_index)
10152 per_cu->v.quick->compunit_symtab = cust;
10153 else
10154 {
10155 dwarf2_psymtab *pst = per_cu->v.psymtab;
10156 pst->compunit_symtab = cust;
10157 pst->readin = true;
10158 }
10159
10160 /* Not needed any more. */
10161 cu->reset_builder ();
10162 }
10163
10164 /* Process an imported unit DIE. */
10165
10166 static void
10167 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10168 {
10169 struct attribute *attr;
10170
10171 /* For now we don't handle imported units in type units. */
10172 if (cu->per_cu->is_debug_types)
10173 {
10174 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10175 " supported in type units [in module %s]"),
10176 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10177 }
10178
10179 attr = dwarf2_attr (die, DW_AT_import, cu);
10180 if (attr != NULL)
10181 {
10182 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10183 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10184 dwarf2_per_cu_data *per_cu
10185 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10186 cu->per_cu->dwarf2_per_objfile);
10187
10188 /* If necessary, add it to the queue and load its DIEs. */
10189 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10190 load_full_comp_unit (per_cu, false, cu->language);
10191
10192 cu->per_cu->imported_symtabs_push (per_cu);
10193 }
10194 }
10195
10196 /* RAII object that represents a process_die scope: i.e.,
10197 starts/finishes processing a DIE. */
10198 class process_die_scope
10199 {
10200 public:
10201 process_die_scope (die_info *die, dwarf2_cu *cu)
10202 : m_die (die), m_cu (cu)
10203 {
10204 /* We should only be processing DIEs not already in process. */
10205 gdb_assert (!m_die->in_process);
10206 m_die->in_process = true;
10207 }
10208
10209 ~process_die_scope ()
10210 {
10211 m_die->in_process = false;
10212
10213 /* If we're done processing the DIE for the CU that owns the line
10214 header, we don't need the line header anymore. */
10215 if (m_cu->line_header_die_owner == m_die)
10216 {
10217 delete m_cu->line_header;
10218 m_cu->line_header = NULL;
10219 m_cu->line_header_die_owner = NULL;
10220 }
10221 }
10222
10223 private:
10224 die_info *m_die;
10225 dwarf2_cu *m_cu;
10226 };
10227
10228 /* Process a die and its children. */
10229
10230 static void
10231 process_die (struct die_info *die, struct dwarf2_cu *cu)
10232 {
10233 process_die_scope scope (die, cu);
10234
10235 switch (die->tag)
10236 {
10237 case DW_TAG_padding:
10238 break;
10239 case DW_TAG_compile_unit:
10240 case DW_TAG_partial_unit:
10241 read_file_scope (die, cu);
10242 break;
10243 case DW_TAG_type_unit:
10244 read_type_unit_scope (die, cu);
10245 break;
10246 case DW_TAG_subprogram:
10247 /* Nested subprograms in Fortran get a prefix. */
10248 if (cu->language == language_fortran
10249 && die->parent != NULL
10250 && die->parent->tag == DW_TAG_subprogram)
10251 cu->processing_has_namespace_info = true;
10252 /* Fall through. */
10253 case DW_TAG_inlined_subroutine:
10254 read_func_scope (die, cu);
10255 break;
10256 case DW_TAG_lexical_block:
10257 case DW_TAG_try_block:
10258 case DW_TAG_catch_block:
10259 read_lexical_block_scope (die, cu);
10260 break;
10261 case DW_TAG_call_site:
10262 case DW_TAG_GNU_call_site:
10263 read_call_site_scope (die, cu);
10264 break;
10265 case DW_TAG_class_type:
10266 case DW_TAG_interface_type:
10267 case DW_TAG_structure_type:
10268 case DW_TAG_union_type:
10269 process_structure_scope (die, cu);
10270 break;
10271 case DW_TAG_enumeration_type:
10272 process_enumeration_scope (die, cu);
10273 break;
10274
10275 /* These dies have a type, but processing them does not create
10276 a symbol or recurse to process the children. Therefore we can
10277 read them on-demand through read_type_die. */
10278 case DW_TAG_subroutine_type:
10279 case DW_TAG_set_type:
10280 case DW_TAG_array_type:
10281 case DW_TAG_pointer_type:
10282 case DW_TAG_ptr_to_member_type:
10283 case DW_TAG_reference_type:
10284 case DW_TAG_rvalue_reference_type:
10285 case DW_TAG_string_type:
10286 break;
10287
10288 case DW_TAG_base_type:
10289 case DW_TAG_subrange_type:
10290 case DW_TAG_typedef:
10291 /* Add a typedef symbol for the type definition, if it has a
10292 DW_AT_name. */
10293 new_symbol (die, read_type_die (die, cu), cu);
10294 break;
10295 case DW_TAG_common_block:
10296 read_common_block (die, cu);
10297 break;
10298 case DW_TAG_common_inclusion:
10299 break;
10300 case DW_TAG_namespace:
10301 cu->processing_has_namespace_info = true;
10302 read_namespace (die, cu);
10303 break;
10304 case DW_TAG_module:
10305 cu->processing_has_namespace_info = true;
10306 read_module (die, cu);
10307 break;
10308 case DW_TAG_imported_declaration:
10309 cu->processing_has_namespace_info = true;
10310 if (read_namespace_alias (die, cu))
10311 break;
10312 /* The declaration is not a global namespace alias. */
10313 /* Fall through. */
10314 case DW_TAG_imported_module:
10315 cu->processing_has_namespace_info = true;
10316 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10317 || cu->language != language_fortran))
10318 complaint (_("Tag '%s' has unexpected children"),
10319 dwarf_tag_name (die->tag));
10320 read_import_statement (die, cu);
10321 break;
10322
10323 case DW_TAG_imported_unit:
10324 process_imported_unit_die (die, cu);
10325 break;
10326
10327 case DW_TAG_variable:
10328 read_variable (die, cu);
10329 break;
10330
10331 default:
10332 new_symbol (die, NULL, cu);
10333 break;
10334 }
10335 }
10336 \f
10337 /* DWARF name computation. */
10338
10339 /* A helper function for dwarf2_compute_name which determines whether DIE
10340 needs to have the name of the scope prepended to the name listed in the
10341 die. */
10342
10343 static int
10344 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10345 {
10346 struct attribute *attr;
10347
10348 switch (die->tag)
10349 {
10350 case DW_TAG_namespace:
10351 case DW_TAG_typedef:
10352 case DW_TAG_class_type:
10353 case DW_TAG_interface_type:
10354 case DW_TAG_structure_type:
10355 case DW_TAG_union_type:
10356 case DW_TAG_enumeration_type:
10357 case DW_TAG_enumerator:
10358 case DW_TAG_subprogram:
10359 case DW_TAG_inlined_subroutine:
10360 case DW_TAG_member:
10361 case DW_TAG_imported_declaration:
10362 return 1;
10363
10364 case DW_TAG_variable:
10365 case DW_TAG_constant:
10366 /* We only need to prefix "globally" visible variables. These include
10367 any variable marked with DW_AT_external or any variable that
10368 lives in a namespace. [Variables in anonymous namespaces
10369 require prefixing, but they are not DW_AT_external.] */
10370
10371 if (dwarf2_attr (die, DW_AT_specification, cu))
10372 {
10373 struct dwarf2_cu *spec_cu = cu;
10374
10375 return die_needs_namespace (die_specification (die, &spec_cu),
10376 spec_cu);
10377 }
10378
10379 attr = dwarf2_attr (die, DW_AT_external, cu);
10380 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10381 && die->parent->tag != DW_TAG_module)
10382 return 0;
10383 /* A variable in a lexical block of some kind does not need a
10384 namespace, even though in C++ such variables may be external
10385 and have a mangled name. */
10386 if (die->parent->tag == DW_TAG_lexical_block
10387 || die->parent->tag == DW_TAG_try_block
10388 || die->parent->tag == DW_TAG_catch_block
10389 || die->parent->tag == DW_TAG_subprogram)
10390 return 0;
10391 return 1;
10392
10393 default:
10394 return 0;
10395 }
10396 }
10397
10398 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10399 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10400 defined for the given DIE. */
10401
10402 static struct attribute *
10403 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10404 {
10405 struct attribute *attr;
10406
10407 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10408 if (attr == NULL)
10409 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10410
10411 return attr;
10412 }
10413
10414 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10415 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10416 defined for the given DIE. */
10417
10418 static const char *
10419 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10420 {
10421 const char *linkage_name;
10422
10423 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10424 if (linkage_name == NULL)
10425 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10426
10427 return linkage_name;
10428 }
10429
10430 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10431 compute the physname for the object, which include a method's:
10432 - formal parameters (C++),
10433 - receiver type (Go),
10434
10435 The term "physname" is a bit confusing.
10436 For C++, for example, it is the demangled name.
10437 For Go, for example, it's the mangled name.
10438
10439 For Ada, return the DIE's linkage name rather than the fully qualified
10440 name. PHYSNAME is ignored..
10441
10442 The result is allocated on the objfile_obstack and canonicalized. */
10443
10444 static const char *
10445 dwarf2_compute_name (const char *name,
10446 struct die_info *die, struct dwarf2_cu *cu,
10447 int physname)
10448 {
10449 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10450
10451 if (name == NULL)
10452 name = dwarf2_name (die, cu);
10453
10454 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10455 but otherwise compute it by typename_concat inside GDB.
10456 FIXME: Actually this is not really true, or at least not always true.
10457 It's all very confusing. compute_and_set_names doesn't try to demangle
10458 Fortran names because there is no mangling standard. So new_symbol
10459 will set the demangled name to the result of dwarf2_full_name, and it is
10460 the demangled name that GDB uses if it exists. */
10461 if (cu->language == language_ada
10462 || (cu->language == language_fortran && physname))
10463 {
10464 /* For Ada unit, we prefer the linkage name over the name, as
10465 the former contains the exported name, which the user expects
10466 to be able to reference. Ideally, we want the user to be able
10467 to reference this entity using either natural or linkage name,
10468 but we haven't started looking at this enhancement yet. */
10469 const char *linkage_name = dw2_linkage_name (die, cu);
10470
10471 if (linkage_name != NULL)
10472 return linkage_name;
10473 }
10474
10475 /* These are the only languages we know how to qualify names in. */
10476 if (name != NULL
10477 && (cu->language == language_cplus
10478 || cu->language == language_fortran || cu->language == language_d
10479 || cu->language == language_rust))
10480 {
10481 if (die_needs_namespace (die, cu))
10482 {
10483 const char *prefix;
10484 const char *canonical_name = NULL;
10485
10486 string_file buf;
10487
10488 prefix = determine_prefix (die, cu);
10489 if (*prefix != '\0')
10490 {
10491 gdb::unique_xmalloc_ptr<char> prefixed_name
10492 (typename_concat (NULL, prefix, name, physname, cu));
10493
10494 buf.puts (prefixed_name.get ());
10495 }
10496 else
10497 buf.puts (name);
10498
10499 /* Template parameters may be specified in the DIE's DW_AT_name, or
10500 as children with DW_TAG_template_type_param or
10501 DW_TAG_value_type_param. If the latter, add them to the name
10502 here. If the name already has template parameters, then
10503 skip this step; some versions of GCC emit both, and
10504 it is more efficient to use the pre-computed name.
10505
10506 Something to keep in mind about this process: it is very
10507 unlikely, or in some cases downright impossible, to produce
10508 something that will match the mangled name of a function.
10509 If the definition of the function has the same debug info,
10510 we should be able to match up with it anyway. But fallbacks
10511 using the minimal symbol, for instance to find a method
10512 implemented in a stripped copy of libstdc++, will not work.
10513 If we do not have debug info for the definition, we will have to
10514 match them up some other way.
10515
10516 When we do name matching there is a related problem with function
10517 templates; two instantiated function templates are allowed to
10518 differ only by their return types, which we do not add here. */
10519
10520 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10521 {
10522 struct attribute *attr;
10523 struct die_info *child;
10524 int first = 1;
10525
10526 die->building_fullname = 1;
10527
10528 for (child = die->child; child != NULL; child = child->sibling)
10529 {
10530 struct type *type;
10531 LONGEST value;
10532 const gdb_byte *bytes;
10533 struct dwarf2_locexpr_baton *baton;
10534 struct value *v;
10535
10536 if (child->tag != DW_TAG_template_type_param
10537 && child->tag != DW_TAG_template_value_param)
10538 continue;
10539
10540 if (first)
10541 {
10542 buf.puts ("<");
10543 first = 0;
10544 }
10545 else
10546 buf.puts (", ");
10547
10548 attr = dwarf2_attr (child, DW_AT_type, cu);
10549 if (attr == NULL)
10550 {
10551 complaint (_("template parameter missing DW_AT_type"));
10552 buf.puts ("UNKNOWN_TYPE");
10553 continue;
10554 }
10555 type = die_type (child, cu);
10556
10557 if (child->tag == DW_TAG_template_type_param)
10558 {
10559 c_print_type (type, "", &buf, -1, 0, cu->language,
10560 &type_print_raw_options);
10561 continue;
10562 }
10563
10564 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10565 if (attr == NULL)
10566 {
10567 complaint (_("template parameter missing "
10568 "DW_AT_const_value"));
10569 buf.puts ("UNKNOWN_VALUE");
10570 continue;
10571 }
10572
10573 dwarf2_const_value_attr (attr, type, name,
10574 &cu->comp_unit_obstack, cu,
10575 &value, &bytes, &baton);
10576
10577 if (TYPE_NOSIGN (type))
10578 /* GDB prints characters as NUMBER 'CHAR'. If that's
10579 changed, this can use value_print instead. */
10580 c_printchar (value, type, &buf);
10581 else
10582 {
10583 struct value_print_options opts;
10584
10585 if (baton != NULL)
10586 v = dwarf2_evaluate_loc_desc (type, NULL,
10587 baton->data,
10588 baton->size,
10589 baton->per_cu);
10590 else if (bytes != NULL)
10591 {
10592 v = allocate_value (type);
10593 memcpy (value_contents_writeable (v), bytes,
10594 TYPE_LENGTH (type));
10595 }
10596 else
10597 v = value_from_longest (type, value);
10598
10599 /* Specify decimal so that we do not depend on
10600 the radix. */
10601 get_formatted_print_options (&opts, 'd');
10602 opts.raw = 1;
10603 value_print (v, &buf, &opts);
10604 release_value (v);
10605 }
10606 }
10607
10608 die->building_fullname = 0;
10609
10610 if (!first)
10611 {
10612 /* Close the argument list, with a space if necessary
10613 (nested templates). */
10614 if (!buf.empty () && buf.string ().back () == '>')
10615 buf.puts (" >");
10616 else
10617 buf.puts (">");
10618 }
10619 }
10620
10621 /* For C++ methods, append formal parameter type
10622 information, if PHYSNAME. */
10623
10624 if (physname && die->tag == DW_TAG_subprogram
10625 && cu->language == language_cplus)
10626 {
10627 struct type *type = read_type_die (die, cu);
10628
10629 c_type_print_args (type, &buf, 1, cu->language,
10630 &type_print_raw_options);
10631
10632 if (cu->language == language_cplus)
10633 {
10634 /* Assume that an artificial first parameter is
10635 "this", but do not crash if it is not. RealView
10636 marks unnamed (and thus unused) parameters as
10637 artificial; there is no way to differentiate
10638 the two cases. */
10639 if (TYPE_NFIELDS (type) > 0
10640 && TYPE_FIELD_ARTIFICIAL (type, 0)
10641 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10642 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10643 0))))
10644 buf.puts (" const");
10645 }
10646 }
10647
10648 const std::string &intermediate_name = buf.string ();
10649
10650 if (cu->language == language_cplus)
10651 canonical_name
10652 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10653 &objfile->per_bfd->storage_obstack);
10654
10655 /* If we only computed INTERMEDIATE_NAME, or if
10656 INTERMEDIATE_NAME is already canonical, then we need to
10657 copy it to the appropriate obstack. */
10658 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10659 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10660 intermediate_name);
10661 else
10662 name = canonical_name;
10663 }
10664 }
10665
10666 return name;
10667 }
10668
10669 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10670 If scope qualifiers are appropriate they will be added. The result
10671 will be allocated on the storage_obstack, or NULL if the DIE does
10672 not have a name. NAME may either be from a previous call to
10673 dwarf2_name or NULL.
10674
10675 The output string will be canonicalized (if C++). */
10676
10677 static const char *
10678 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10679 {
10680 return dwarf2_compute_name (name, die, cu, 0);
10681 }
10682
10683 /* Construct a physname for the given DIE in CU. NAME may either be
10684 from a previous call to dwarf2_name or NULL. The result will be
10685 allocated on the objfile_objstack or NULL if the DIE does not have a
10686 name.
10687
10688 The output string will be canonicalized (if C++). */
10689
10690 static const char *
10691 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10692 {
10693 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10694 const char *retval, *mangled = NULL, *canon = NULL;
10695 int need_copy = 1;
10696
10697 /* In this case dwarf2_compute_name is just a shortcut not building anything
10698 on its own. */
10699 if (!die_needs_namespace (die, cu))
10700 return dwarf2_compute_name (name, die, cu, 1);
10701
10702 mangled = dw2_linkage_name (die, cu);
10703
10704 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10705 See https://github.com/rust-lang/rust/issues/32925. */
10706 if (cu->language == language_rust && mangled != NULL
10707 && strchr (mangled, '{') != NULL)
10708 mangled = NULL;
10709
10710 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10711 has computed. */
10712 gdb::unique_xmalloc_ptr<char> demangled;
10713 if (mangled != NULL)
10714 {
10715
10716 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10717 {
10718 /* Do nothing (do not demangle the symbol name). */
10719 }
10720 else if (cu->language == language_go)
10721 {
10722 /* This is a lie, but we already lie to the caller new_symbol.
10723 new_symbol assumes we return the mangled name.
10724 This just undoes that lie until things are cleaned up. */
10725 }
10726 else
10727 {
10728 /* Use DMGL_RET_DROP for C++ template functions to suppress
10729 their return type. It is easier for GDB users to search
10730 for such functions as `name(params)' than `long name(params)'.
10731 In such case the minimal symbol names do not match the full
10732 symbol names but for template functions there is never a need
10733 to look up their definition from their declaration so
10734 the only disadvantage remains the minimal symbol variant
10735 `long name(params)' does not have the proper inferior type. */
10736 demangled.reset (gdb_demangle (mangled,
10737 (DMGL_PARAMS | DMGL_ANSI
10738 | DMGL_RET_DROP)));
10739 }
10740 if (demangled)
10741 canon = demangled.get ();
10742 else
10743 {
10744 canon = mangled;
10745 need_copy = 0;
10746 }
10747 }
10748
10749 if (canon == NULL || check_physname)
10750 {
10751 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10752
10753 if (canon != NULL && strcmp (physname, canon) != 0)
10754 {
10755 /* It may not mean a bug in GDB. The compiler could also
10756 compute DW_AT_linkage_name incorrectly. But in such case
10757 GDB would need to be bug-to-bug compatible. */
10758
10759 complaint (_("Computed physname <%s> does not match demangled <%s> "
10760 "(from linkage <%s>) - DIE at %s [in module %s]"),
10761 physname, canon, mangled, sect_offset_str (die->sect_off),
10762 objfile_name (objfile));
10763
10764 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10765 is available here - over computed PHYSNAME. It is safer
10766 against both buggy GDB and buggy compilers. */
10767
10768 retval = canon;
10769 }
10770 else
10771 {
10772 retval = physname;
10773 need_copy = 0;
10774 }
10775 }
10776 else
10777 retval = canon;
10778
10779 if (need_copy)
10780 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10781
10782 return retval;
10783 }
10784
10785 /* Inspect DIE in CU for a namespace alias. If one exists, record
10786 a new symbol for it.
10787
10788 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10789
10790 static int
10791 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10792 {
10793 struct attribute *attr;
10794
10795 /* If the die does not have a name, this is not a namespace
10796 alias. */
10797 attr = dwarf2_attr (die, DW_AT_name, cu);
10798 if (attr != NULL)
10799 {
10800 int num;
10801 struct die_info *d = die;
10802 struct dwarf2_cu *imported_cu = cu;
10803
10804 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10805 keep inspecting DIEs until we hit the underlying import. */
10806 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10807 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10808 {
10809 attr = dwarf2_attr (d, DW_AT_import, cu);
10810 if (attr == NULL)
10811 break;
10812
10813 d = follow_die_ref (d, attr, &imported_cu);
10814 if (d->tag != DW_TAG_imported_declaration)
10815 break;
10816 }
10817
10818 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10819 {
10820 complaint (_("DIE at %s has too many recursively imported "
10821 "declarations"), sect_offset_str (d->sect_off));
10822 return 0;
10823 }
10824
10825 if (attr != NULL)
10826 {
10827 struct type *type;
10828 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10829
10830 type = get_die_type_at_offset (sect_off, cu->per_cu);
10831 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10832 {
10833 /* This declaration is a global namespace alias. Add
10834 a symbol for it whose type is the aliased namespace. */
10835 new_symbol (die, type, cu);
10836 return 1;
10837 }
10838 }
10839 }
10840
10841 return 0;
10842 }
10843
10844 /* Return the using directives repository (global or local?) to use in the
10845 current context for CU.
10846
10847 For Ada, imported declarations can materialize renamings, which *may* be
10848 global. However it is impossible (for now?) in DWARF to distinguish
10849 "external" imported declarations and "static" ones. As all imported
10850 declarations seem to be static in all other languages, make them all CU-wide
10851 global only in Ada. */
10852
10853 static struct using_direct **
10854 using_directives (struct dwarf2_cu *cu)
10855 {
10856 if (cu->language == language_ada
10857 && cu->get_builder ()->outermost_context_p ())
10858 return cu->get_builder ()->get_global_using_directives ();
10859 else
10860 return cu->get_builder ()->get_local_using_directives ();
10861 }
10862
10863 /* Read the import statement specified by the given die and record it. */
10864
10865 static void
10866 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10867 {
10868 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10869 struct attribute *import_attr;
10870 struct die_info *imported_die, *child_die;
10871 struct dwarf2_cu *imported_cu;
10872 const char *imported_name;
10873 const char *imported_name_prefix;
10874 const char *canonical_name;
10875 const char *import_alias;
10876 const char *imported_declaration = NULL;
10877 const char *import_prefix;
10878 std::vector<const char *> excludes;
10879
10880 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10881 if (import_attr == NULL)
10882 {
10883 complaint (_("Tag '%s' has no DW_AT_import"),
10884 dwarf_tag_name (die->tag));
10885 return;
10886 }
10887
10888 imported_cu = cu;
10889 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10890 imported_name = dwarf2_name (imported_die, imported_cu);
10891 if (imported_name == NULL)
10892 {
10893 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10894
10895 The import in the following code:
10896 namespace A
10897 {
10898 typedef int B;
10899 }
10900
10901 int main ()
10902 {
10903 using A::B;
10904 B b;
10905 return b;
10906 }
10907
10908 ...
10909 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10910 <52> DW_AT_decl_file : 1
10911 <53> DW_AT_decl_line : 6
10912 <54> DW_AT_import : <0x75>
10913 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10914 <59> DW_AT_name : B
10915 <5b> DW_AT_decl_file : 1
10916 <5c> DW_AT_decl_line : 2
10917 <5d> DW_AT_type : <0x6e>
10918 ...
10919 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10920 <76> DW_AT_byte_size : 4
10921 <77> DW_AT_encoding : 5 (signed)
10922
10923 imports the wrong die ( 0x75 instead of 0x58 ).
10924 This case will be ignored until the gcc bug is fixed. */
10925 return;
10926 }
10927
10928 /* Figure out the local name after import. */
10929 import_alias = dwarf2_name (die, cu);
10930
10931 /* Figure out where the statement is being imported to. */
10932 import_prefix = determine_prefix (die, cu);
10933
10934 /* Figure out what the scope of the imported die is and prepend it
10935 to the name of the imported die. */
10936 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10937
10938 if (imported_die->tag != DW_TAG_namespace
10939 && imported_die->tag != DW_TAG_module)
10940 {
10941 imported_declaration = imported_name;
10942 canonical_name = imported_name_prefix;
10943 }
10944 else if (strlen (imported_name_prefix) > 0)
10945 canonical_name = obconcat (&objfile->objfile_obstack,
10946 imported_name_prefix,
10947 (cu->language == language_d ? "." : "::"),
10948 imported_name, (char *) NULL);
10949 else
10950 canonical_name = imported_name;
10951
10952 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10953 for (child_die = die->child; child_die && child_die->tag;
10954 child_die = sibling_die (child_die))
10955 {
10956 /* DWARF-4: A Fortran use statement with a “rename list” may be
10957 represented by an imported module entry with an import attribute
10958 referring to the module and owned entries corresponding to those
10959 entities that are renamed as part of being imported. */
10960
10961 if (child_die->tag != DW_TAG_imported_declaration)
10962 {
10963 complaint (_("child DW_TAG_imported_declaration expected "
10964 "- DIE at %s [in module %s]"),
10965 sect_offset_str (child_die->sect_off),
10966 objfile_name (objfile));
10967 continue;
10968 }
10969
10970 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10971 if (import_attr == NULL)
10972 {
10973 complaint (_("Tag '%s' has no DW_AT_import"),
10974 dwarf_tag_name (child_die->tag));
10975 continue;
10976 }
10977
10978 imported_cu = cu;
10979 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10980 &imported_cu);
10981 imported_name = dwarf2_name (imported_die, imported_cu);
10982 if (imported_name == NULL)
10983 {
10984 complaint (_("child DW_TAG_imported_declaration has unknown "
10985 "imported name - DIE at %s [in module %s]"),
10986 sect_offset_str (child_die->sect_off),
10987 objfile_name (objfile));
10988 continue;
10989 }
10990
10991 excludes.push_back (imported_name);
10992
10993 process_die (child_die, cu);
10994 }
10995
10996 add_using_directive (using_directives (cu),
10997 import_prefix,
10998 canonical_name,
10999 import_alias,
11000 imported_declaration,
11001 excludes,
11002 0,
11003 &objfile->objfile_obstack);
11004 }
11005
11006 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11007 types, but gives them a size of zero. Starting with version 14,
11008 ICC is compatible with GCC. */
11009
11010 static bool
11011 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11012 {
11013 if (!cu->checked_producer)
11014 check_producer (cu);
11015
11016 return cu->producer_is_icc_lt_14;
11017 }
11018
11019 /* ICC generates a DW_AT_type for C void functions. This was observed on
11020 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11021 which says that void functions should not have a DW_AT_type. */
11022
11023 static bool
11024 producer_is_icc (struct dwarf2_cu *cu)
11025 {
11026 if (!cu->checked_producer)
11027 check_producer (cu);
11028
11029 return cu->producer_is_icc;
11030 }
11031
11032 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11033 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11034 this, it was first present in GCC release 4.3.0. */
11035
11036 static bool
11037 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11038 {
11039 if (!cu->checked_producer)
11040 check_producer (cu);
11041
11042 return cu->producer_is_gcc_lt_4_3;
11043 }
11044
11045 static file_and_directory
11046 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11047 {
11048 file_and_directory res;
11049
11050 /* Find the filename. Do not use dwarf2_name here, since the filename
11051 is not a source language identifier. */
11052 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11053 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11054
11055 if (res.comp_dir == NULL
11056 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11057 && IS_ABSOLUTE_PATH (res.name))
11058 {
11059 res.comp_dir_storage = ldirname (res.name);
11060 if (!res.comp_dir_storage.empty ())
11061 res.comp_dir = res.comp_dir_storage.c_str ();
11062 }
11063 if (res.comp_dir != NULL)
11064 {
11065 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11066 directory, get rid of it. */
11067 const char *cp = strchr (res.comp_dir, ':');
11068
11069 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11070 res.comp_dir = cp + 1;
11071 }
11072
11073 if (res.name == NULL)
11074 res.name = "<unknown>";
11075
11076 return res;
11077 }
11078
11079 /* Handle DW_AT_stmt_list for a compilation unit.
11080 DIE is the DW_TAG_compile_unit die for CU.
11081 COMP_DIR is the compilation directory. LOWPC is passed to
11082 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11083
11084 static void
11085 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11086 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11087 {
11088 struct dwarf2_per_objfile *dwarf2_per_objfile
11089 = cu->per_cu->dwarf2_per_objfile;
11090 struct objfile *objfile = dwarf2_per_objfile->objfile;
11091 struct attribute *attr;
11092 struct line_header line_header_local;
11093 hashval_t line_header_local_hash;
11094 void **slot;
11095 int decode_mapping;
11096
11097 gdb_assert (! cu->per_cu->is_debug_types);
11098
11099 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11100 if (attr == NULL)
11101 return;
11102
11103 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11104
11105 /* The line header hash table is only created if needed (it exists to
11106 prevent redundant reading of the line table for partial_units).
11107 If we're given a partial_unit, we'll need it. If we're given a
11108 compile_unit, then use the line header hash table if it's already
11109 created, but don't create one just yet. */
11110
11111 if (dwarf2_per_objfile->line_header_hash == NULL
11112 && die->tag == DW_TAG_partial_unit)
11113 {
11114 dwarf2_per_objfile->line_header_hash
11115 = htab_create_alloc_ex (127, line_header_hash_voidp,
11116 line_header_eq_voidp,
11117 free_line_header_voidp,
11118 &objfile->objfile_obstack,
11119 hashtab_obstack_allocate,
11120 dummy_obstack_deallocate);
11121 }
11122
11123 line_header_local.sect_off = line_offset;
11124 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11125 line_header_local_hash = line_header_hash (&line_header_local);
11126 if (dwarf2_per_objfile->line_header_hash != NULL)
11127 {
11128 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11129 &line_header_local,
11130 line_header_local_hash, NO_INSERT);
11131
11132 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11133 is not present in *SLOT (since if there is something in *SLOT then
11134 it will be for a partial_unit). */
11135 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11136 {
11137 gdb_assert (*slot != NULL);
11138 cu->line_header = (struct line_header *) *slot;
11139 return;
11140 }
11141 }
11142
11143 /* dwarf_decode_line_header does not yet provide sufficient information.
11144 We always have to call also dwarf_decode_lines for it. */
11145 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11146 if (lh == NULL)
11147 return;
11148
11149 cu->line_header = lh.release ();
11150 cu->line_header_die_owner = die;
11151
11152 if (dwarf2_per_objfile->line_header_hash == NULL)
11153 slot = NULL;
11154 else
11155 {
11156 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11157 &line_header_local,
11158 line_header_local_hash, INSERT);
11159 gdb_assert (slot != NULL);
11160 }
11161 if (slot != NULL && *slot == NULL)
11162 {
11163 /* This newly decoded line number information unit will be owned
11164 by line_header_hash hash table. */
11165 *slot = cu->line_header;
11166 cu->line_header_die_owner = NULL;
11167 }
11168 else
11169 {
11170 /* We cannot free any current entry in (*slot) as that struct line_header
11171 may be already used by multiple CUs. Create only temporary decoded
11172 line_header for this CU - it may happen at most once for each line
11173 number information unit. And if we're not using line_header_hash
11174 then this is what we want as well. */
11175 gdb_assert (die->tag != DW_TAG_partial_unit);
11176 }
11177 decode_mapping = (die->tag != DW_TAG_partial_unit);
11178 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11179 decode_mapping);
11180
11181 }
11182
11183 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11184
11185 static void
11186 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11187 {
11188 struct dwarf2_per_objfile *dwarf2_per_objfile
11189 = cu->per_cu->dwarf2_per_objfile;
11190 struct objfile *objfile = dwarf2_per_objfile->objfile;
11191 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11192 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11193 CORE_ADDR highpc = ((CORE_ADDR) 0);
11194 struct attribute *attr;
11195 struct die_info *child_die;
11196 CORE_ADDR baseaddr;
11197
11198 prepare_one_comp_unit (cu, die, cu->language);
11199 baseaddr = objfile->text_section_offset ();
11200
11201 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11202
11203 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11204 from finish_block. */
11205 if (lowpc == ((CORE_ADDR) -1))
11206 lowpc = highpc;
11207 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11208
11209 file_and_directory fnd = find_file_and_directory (die, cu);
11210
11211 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11212 standardised yet. As a workaround for the language detection we fall
11213 back to the DW_AT_producer string. */
11214 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11215 cu->language = language_opencl;
11216
11217 /* Similar hack for Go. */
11218 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11219 set_cu_language (DW_LANG_Go, cu);
11220
11221 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11222
11223 /* Decode line number information if present. We do this before
11224 processing child DIEs, so that the line header table is available
11225 for DW_AT_decl_file. */
11226 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11227
11228 /* Process all dies in compilation unit. */
11229 if (die->child != NULL)
11230 {
11231 child_die = die->child;
11232 while (child_die && child_die->tag)
11233 {
11234 process_die (child_die, cu);
11235 child_die = sibling_die (child_die);
11236 }
11237 }
11238
11239 /* Decode macro information, if present. Dwarf 2 macro information
11240 refers to information in the line number info statement program
11241 header, so we can only read it if we've read the header
11242 successfully. */
11243 attr = dwarf2_attr (die, DW_AT_macros, cu);
11244 if (attr == NULL)
11245 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11246 if (attr && cu->line_header)
11247 {
11248 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11249 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11250
11251 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11252 }
11253 else
11254 {
11255 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11256 if (attr && cu->line_header)
11257 {
11258 unsigned int macro_offset = DW_UNSND (attr);
11259
11260 dwarf_decode_macros (cu, macro_offset, 0);
11261 }
11262 }
11263 }
11264
11265 void
11266 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11267 {
11268 struct type_unit_group *tu_group;
11269 int first_time;
11270 struct attribute *attr;
11271 unsigned int i;
11272 struct signatured_type *sig_type;
11273
11274 gdb_assert (per_cu->is_debug_types);
11275 sig_type = (struct signatured_type *) per_cu;
11276
11277 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11278
11279 /* If we're using .gdb_index (includes -readnow) then
11280 per_cu->type_unit_group may not have been set up yet. */
11281 if (sig_type->type_unit_group == NULL)
11282 sig_type->type_unit_group = get_type_unit_group (this, attr);
11283 tu_group = sig_type->type_unit_group;
11284
11285 /* If we've already processed this stmt_list there's no real need to
11286 do it again, we could fake it and just recreate the part we need
11287 (file name,index -> symtab mapping). If data shows this optimization
11288 is useful we can do it then. */
11289 first_time = tu_group->compunit_symtab == NULL;
11290
11291 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11292 debug info. */
11293 line_header_up lh;
11294 if (attr != NULL)
11295 {
11296 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11297 lh = dwarf_decode_line_header (line_offset, this);
11298 }
11299 if (lh == NULL)
11300 {
11301 if (first_time)
11302 start_symtab ("", NULL, 0);
11303 else
11304 {
11305 gdb_assert (tu_group->symtabs == NULL);
11306 gdb_assert (m_builder == nullptr);
11307 struct compunit_symtab *cust = tu_group->compunit_symtab;
11308 m_builder.reset (new struct buildsym_compunit
11309 (COMPUNIT_OBJFILE (cust), "",
11310 COMPUNIT_DIRNAME (cust),
11311 compunit_language (cust),
11312 0, cust));
11313 }
11314 return;
11315 }
11316
11317 line_header = lh.release ();
11318 line_header_die_owner = die;
11319
11320 if (first_time)
11321 {
11322 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11323
11324 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11325 still initializing it, and our caller (a few levels up)
11326 process_full_type_unit still needs to know if this is the first
11327 time. */
11328
11329 tu_group->num_symtabs = line_header->file_names_size ();
11330 tu_group->symtabs = XNEWVEC (struct symtab *,
11331 line_header->file_names_size ());
11332
11333 auto &file_names = line_header->file_names ();
11334 for (i = 0; i < file_names.size (); ++i)
11335 {
11336 file_entry &fe = file_names[i];
11337 dwarf2_start_subfile (this, fe.name,
11338 fe.include_dir (line_header));
11339 buildsym_compunit *b = get_builder ();
11340 if (b->get_current_subfile ()->symtab == NULL)
11341 {
11342 /* NOTE: start_subfile will recognize when it's been
11343 passed a file it has already seen. So we can't
11344 assume there's a simple mapping from
11345 cu->line_header->file_names to subfiles, plus
11346 cu->line_header->file_names may contain dups. */
11347 b->get_current_subfile ()->symtab
11348 = allocate_symtab (cust, b->get_current_subfile ()->name);
11349 }
11350
11351 fe.symtab = b->get_current_subfile ()->symtab;
11352 tu_group->symtabs[i] = fe.symtab;
11353 }
11354 }
11355 else
11356 {
11357 gdb_assert (m_builder == nullptr);
11358 struct compunit_symtab *cust = tu_group->compunit_symtab;
11359 m_builder.reset (new struct buildsym_compunit
11360 (COMPUNIT_OBJFILE (cust), "",
11361 COMPUNIT_DIRNAME (cust),
11362 compunit_language (cust),
11363 0, cust));
11364
11365 auto &file_names = line_header->file_names ();
11366 for (i = 0; i < file_names.size (); ++i)
11367 {
11368 file_entry &fe = file_names[i];
11369 fe.symtab = tu_group->symtabs[i];
11370 }
11371 }
11372
11373 /* The main symtab is allocated last. Type units don't have DW_AT_name
11374 so they don't have a "real" (so to speak) symtab anyway.
11375 There is later code that will assign the main symtab to all symbols
11376 that don't have one. We need to handle the case of a symbol with a
11377 missing symtab (DW_AT_decl_file) anyway. */
11378 }
11379
11380 /* Process DW_TAG_type_unit.
11381 For TUs we want to skip the first top level sibling if it's not the
11382 actual type being defined by this TU. In this case the first top
11383 level sibling is there to provide context only. */
11384
11385 static void
11386 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11387 {
11388 struct die_info *child_die;
11389
11390 prepare_one_comp_unit (cu, die, language_minimal);
11391
11392 /* Initialize (or reinitialize) the machinery for building symtabs.
11393 We do this before processing child DIEs, so that the line header table
11394 is available for DW_AT_decl_file. */
11395 cu->setup_type_unit_groups (die);
11396
11397 if (die->child != NULL)
11398 {
11399 child_die = die->child;
11400 while (child_die && child_die->tag)
11401 {
11402 process_die (child_die, cu);
11403 child_die = sibling_die (child_die);
11404 }
11405 }
11406 }
11407 \f
11408 /* DWO/DWP files.
11409
11410 http://gcc.gnu.org/wiki/DebugFission
11411 http://gcc.gnu.org/wiki/DebugFissionDWP
11412
11413 To simplify handling of both DWO files ("object" files with the DWARF info)
11414 and DWP files (a file with the DWOs packaged up into one file), we treat
11415 DWP files as having a collection of virtual DWO files. */
11416
11417 static hashval_t
11418 hash_dwo_file (const void *item)
11419 {
11420 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11421 hashval_t hash;
11422
11423 hash = htab_hash_string (dwo_file->dwo_name);
11424 if (dwo_file->comp_dir != NULL)
11425 hash += htab_hash_string (dwo_file->comp_dir);
11426 return hash;
11427 }
11428
11429 static int
11430 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11431 {
11432 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11433 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11434
11435 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11436 return 0;
11437 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11438 return lhs->comp_dir == rhs->comp_dir;
11439 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11440 }
11441
11442 /* Allocate a hash table for DWO files. */
11443
11444 static htab_up
11445 allocate_dwo_file_hash_table (struct objfile *objfile)
11446 {
11447 auto delete_dwo_file = [] (void *item)
11448 {
11449 struct dwo_file *dwo_file = (struct dwo_file *) item;
11450
11451 delete dwo_file;
11452 };
11453
11454 return htab_up (htab_create_alloc_ex (41,
11455 hash_dwo_file,
11456 eq_dwo_file,
11457 delete_dwo_file,
11458 &objfile->objfile_obstack,
11459 hashtab_obstack_allocate,
11460 dummy_obstack_deallocate));
11461 }
11462
11463 /* Lookup DWO file DWO_NAME. */
11464
11465 static void **
11466 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11467 const char *dwo_name,
11468 const char *comp_dir)
11469 {
11470 struct dwo_file find_entry;
11471 void **slot;
11472
11473 if (dwarf2_per_objfile->dwo_files == NULL)
11474 dwarf2_per_objfile->dwo_files
11475 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11476
11477 find_entry.dwo_name = dwo_name;
11478 find_entry.comp_dir = comp_dir;
11479 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11480 INSERT);
11481
11482 return slot;
11483 }
11484
11485 static hashval_t
11486 hash_dwo_unit (const void *item)
11487 {
11488 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11489
11490 /* This drops the top 32 bits of the id, but is ok for a hash. */
11491 return dwo_unit->signature;
11492 }
11493
11494 static int
11495 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11496 {
11497 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11498 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11499
11500 /* The signature is assumed to be unique within the DWO file.
11501 So while object file CU dwo_id's always have the value zero,
11502 that's OK, assuming each object file DWO file has only one CU,
11503 and that's the rule for now. */
11504 return lhs->signature == rhs->signature;
11505 }
11506
11507 /* Allocate a hash table for DWO CUs,TUs.
11508 There is one of these tables for each of CUs,TUs for each DWO file. */
11509
11510 static htab_t
11511 allocate_dwo_unit_table (struct objfile *objfile)
11512 {
11513 /* Start out with a pretty small number.
11514 Generally DWO files contain only one CU and maybe some TUs. */
11515 return htab_create_alloc_ex (3,
11516 hash_dwo_unit,
11517 eq_dwo_unit,
11518 NULL,
11519 &objfile->objfile_obstack,
11520 hashtab_obstack_allocate,
11521 dummy_obstack_deallocate);
11522 }
11523
11524 /* die_reader_func for create_dwo_cu. */
11525
11526 static void
11527 create_dwo_cu_reader (const struct die_reader_specs *reader,
11528 const gdb_byte *info_ptr,
11529 struct die_info *comp_unit_die,
11530 struct dwo_file *dwo_file,
11531 struct dwo_unit *dwo_unit)
11532 {
11533 struct dwarf2_cu *cu = reader->cu;
11534 sect_offset sect_off = cu->per_cu->sect_off;
11535 struct dwarf2_section_info *section = cu->per_cu->section;
11536
11537 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11538 if (!signature.has_value ())
11539 {
11540 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11541 " its dwo_id [in module %s]"),
11542 sect_offset_str (sect_off), dwo_file->dwo_name);
11543 return;
11544 }
11545
11546 dwo_unit->dwo_file = dwo_file;
11547 dwo_unit->signature = *signature;
11548 dwo_unit->section = section;
11549 dwo_unit->sect_off = sect_off;
11550 dwo_unit->length = cu->per_cu->length;
11551
11552 if (dwarf_read_debug)
11553 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11554 sect_offset_str (sect_off),
11555 hex_string (dwo_unit->signature));
11556 }
11557
11558 /* Create the dwo_units for the CUs in a DWO_FILE.
11559 Note: This function processes DWO files only, not DWP files. */
11560
11561 static void
11562 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11563 dwarf2_cu *cu, struct dwo_file &dwo_file,
11564 dwarf2_section_info &section, htab_t &cus_htab)
11565 {
11566 struct objfile *objfile = dwarf2_per_objfile->objfile;
11567 const gdb_byte *info_ptr, *end_ptr;
11568
11569 section.read (objfile);
11570 info_ptr = section.buffer;
11571
11572 if (info_ptr == NULL)
11573 return;
11574
11575 if (dwarf_read_debug)
11576 {
11577 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11578 section.get_name (),
11579 section.get_file_name ());
11580 }
11581
11582 end_ptr = info_ptr + section.size;
11583 while (info_ptr < end_ptr)
11584 {
11585 struct dwarf2_per_cu_data per_cu;
11586 struct dwo_unit read_unit {};
11587 struct dwo_unit *dwo_unit;
11588 void **slot;
11589 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11590
11591 memset (&per_cu, 0, sizeof (per_cu));
11592 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11593 per_cu.is_debug_types = 0;
11594 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11595 per_cu.section = &section;
11596
11597 cutu_reader reader (&per_cu, cu, &dwo_file);
11598 if (!reader.dummy_p)
11599 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11600 &dwo_file, &read_unit);
11601 info_ptr += per_cu.length;
11602
11603 // If the unit could not be parsed, skip it.
11604 if (read_unit.dwo_file == NULL)
11605 continue;
11606
11607 if (cus_htab == NULL)
11608 cus_htab = allocate_dwo_unit_table (objfile);
11609
11610 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11611 *dwo_unit = read_unit;
11612 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11613 gdb_assert (slot != NULL);
11614 if (*slot != NULL)
11615 {
11616 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11617 sect_offset dup_sect_off = dup_cu->sect_off;
11618
11619 complaint (_("debug cu entry at offset %s is duplicate to"
11620 " the entry at offset %s, signature %s"),
11621 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11622 hex_string (dwo_unit->signature));
11623 }
11624 *slot = (void *)dwo_unit;
11625 }
11626 }
11627
11628 /* DWP file .debug_{cu,tu}_index section format:
11629 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11630
11631 DWP Version 1:
11632
11633 Both index sections have the same format, and serve to map a 64-bit
11634 signature to a set of section numbers. Each section begins with a header,
11635 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11636 indexes, and a pool of 32-bit section numbers. The index sections will be
11637 aligned at 8-byte boundaries in the file.
11638
11639 The index section header consists of:
11640
11641 V, 32 bit version number
11642 -, 32 bits unused
11643 N, 32 bit number of compilation units or type units in the index
11644 M, 32 bit number of slots in the hash table
11645
11646 Numbers are recorded using the byte order of the application binary.
11647
11648 The hash table begins at offset 16 in the section, and consists of an array
11649 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11650 order of the application binary). Unused slots in the hash table are 0.
11651 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11652
11653 The parallel table begins immediately after the hash table
11654 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11655 array of 32-bit indexes (using the byte order of the application binary),
11656 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11657 table contains a 32-bit index into the pool of section numbers. For unused
11658 hash table slots, the corresponding entry in the parallel table will be 0.
11659
11660 The pool of section numbers begins immediately following the hash table
11661 (at offset 16 + 12 * M from the beginning of the section). The pool of
11662 section numbers consists of an array of 32-bit words (using the byte order
11663 of the application binary). Each item in the array is indexed starting
11664 from 0. The hash table entry provides the index of the first section
11665 number in the set. Additional section numbers in the set follow, and the
11666 set is terminated by a 0 entry (section number 0 is not used in ELF).
11667
11668 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11669 section must be the first entry in the set, and the .debug_abbrev.dwo must
11670 be the second entry. Other members of the set may follow in any order.
11671
11672 ---
11673
11674 DWP Version 2:
11675
11676 DWP Version 2 combines all the .debug_info, etc. sections into one,
11677 and the entries in the index tables are now offsets into these sections.
11678 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11679 section.
11680
11681 Index Section Contents:
11682 Header
11683 Hash Table of Signatures dwp_hash_table.hash_table
11684 Parallel Table of Indices dwp_hash_table.unit_table
11685 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11686 Table of Section Sizes dwp_hash_table.v2.sizes
11687
11688 The index section header consists of:
11689
11690 V, 32 bit version number
11691 L, 32 bit number of columns in the table of section offsets
11692 N, 32 bit number of compilation units or type units in the index
11693 M, 32 bit number of slots in the hash table
11694
11695 Numbers are recorded using the byte order of the application binary.
11696
11697 The hash table has the same format as version 1.
11698 The parallel table of indices has the same format as version 1,
11699 except that the entries are origin-1 indices into the table of sections
11700 offsets and the table of section sizes.
11701
11702 The table of offsets begins immediately following the parallel table
11703 (at offset 16 + 12 * M from the beginning of the section). The table is
11704 a two-dimensional array of 32-bit words (using the byte order of the
11705 application binary), with L columns and N+1 rows, in row-major order.
11706 Each row in the array is indexed starting from 0. The first row provides
11707 a key to the remaining rows: each column in this row provides an identifier
11708 for a debug section, and the offsets in the same column of subsequent rows
11709 refer to that section. The section identifiers are:
11710
11711 DW_SECT_INFO 1 .debug_info.dwo
11712 DW_SECT_TYPES 2 .debug_types.dwo
11713 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11714 DW_SECT_LINE 4 .debug_line.dwo
11715 DW_SECT_LOC 5 .debug_loc.dwo
11716 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11717 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11718 DW_SECT_MACRO 8 .debug_macro.dwo
11719
11720 The offsets provided by the CU and TU index sections are the base offsets
11721 for the contributions made by each CU or TU to the corresponding section
11722 in the package file. Each CU and TU header contains an abbrev_offset
11723 field, used to find the abbreviations table for that CU or TU within the
11724 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11725 be interpreted as relative to the base offset given in the index section.
11726 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11727 should be interpreted as relative to the base offset for .debug_line.dwo,
11728 and offsets into other debug sections obtained from DWARF attributes should
11729 also be interpreted as relative to the corresponding base offset.
11730
11731 The table of sizes begins immediately following the table of offsets.
11732 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11733 with L columns and N rows, in row-major order. Each row in the array is
11734 indexed starting from 1 (row 0 is shared by the two tables).
11735
11736 ---
11737
11738 Hash table lookup is handled the same in version 1 and 2:
11739
11740 We assume that N and M will not exceed 2^32 - 1.
11741 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11742
11743 Given a 64-bit compilation unit signature or a type signature S, an entry
11744 in the hash table is located as follows:
11745
11746 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11747 the low-order k bits all set to 1.
11748
11749 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11750
11751 3) If the hash table entry at index H matches the signature, use that
11752 entry. If the hash table entry at index H is unused (all zeroes),
11753 terminate the search: the signature is not present in the table.
11754
11755 4) Let H = (H + H') modulo M. Repeat at Step 3.
11756
11757 Because M > N and H' and M are relatively prime, the search is guaranteed
11758 to stop at an unused slot or find the match. */
11759
11760 /* Create a hash table to map DWO IDs to their CU/TU entry in
11761 .debug_{info,types}.dwo in DWP_FILE.
11762 Returns NULL if there isn't one.
11763 Note: This function processes DWP files only, not DWO files. */
11764
11765 static struct dwp_hash_table *
11766 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11767 struct dwp_file *dwp_file, int is_debug_types)
11768 {
11769 struct objfile *objfile = dwarf2_per_objfile->objfile;
11770 bfd *dbfd = dwp_file->dbfd.get ();
11771 const gdb_byte *index_ptr, *index_end;
11772 struct dwarf2_section_info *index;
11773 uint32_t version, nr_columns, nr_units, nr_slots;
11774 struct dwp_hash_table *htab;
11775
11776 if (is_debug_types)
11777 index = &dwp_file->sections.tu_index;
11778 else
11779 index = &dwp_file->sections.cu_index;
11780
11781 if (index->empty ())
11782 return NULL;
11783 index->read (objfile);
11784
11785 index_ptr = index->buffer;
11786 index_end = index_ptr + index->size;
11787
11788 version = read_4_bytes (dbfd, index_ptr);
11789 index_ptr += 4;
11790 if (version == 2)
11791 nr_columns = read_4_bytes (dbfd, index_ptr);
11792 else
11793 nr_columns = 0;
11794 index_ptr += 4;
11795 nr_units = read_4_bytes (dbfd, index_ptr);
11796 index_ptr += 4;
11797 nr_slots = read_4_bytes (dbfd, index_ptr);
11798 index_ptr += 4;
11799
11800 if (version != 1 && version != 2)
11801 {
11802 error (_("Dwarf Error: unsupported DWP file version (%s)"
11803 " [in module %s]"),
11804 pulongest (version), dwp_file->name);
11805 }
11806 if (nr_slots != (nr_slots & -nr_slots))
11807 {
11808 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11809 " is not power of 2 [in module %s]"),
11810 pulongest (nr_slots), dwp_file->name);
11811 }
11812
11813 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11814 htab->version = version;
11815 htab->nr_columns = nr_columns;
11816 htab->nr_units = nr_units;
11817 htab->nr_slots = nr_slots;
11818 htab->hash_table = index_ptr;
11819 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11820
11821 /* Exit early if the table is empty. */
11822 if (nr_slots == 0 || nr_units == 0
11823 || (version == 2 && nr_columns == 0))
11824 {
11825 /* All must be zero. */
11826 if (nr_slots != 0 || nr_units != 0
11827 || (version == 2 && nr_columns != 0))
11828 {
11829 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11830 " all zero [in modules %s]"),
11831 dwp_file->name);
11832 }
11833 return htab;
11834 }
11835
11836 if (version == 1)
11837 {
11838 htab->section_pool.v1.indices =
11839 htab->unit_table + sizeof (uint32_t) * nr_slots;
11840 /* It's harder to decide whether the section is too small in v1.
11841 V1 is deprecated anyway so we punt. */
11842 }
11843 else
11844 {
11845 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11846 int *ids = htab->section_pool.v2.section_ids;
11847 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11848 /* Reverse map for error checking. */
11849 int ids_seen[DW_SECT_MAX + 1];
11850 int i;
11851
11852 if (nr_columns < 2)
11853 {
11854 error (_("Dwarf Error: bad DWP hash table, too few columns"
11855 " in section table [in module %s]"),
11856 dwp_file->name);
11857 }
11858 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11859 {
11860 error (_("Dwarf Error: bad DWP hash table, too many columns"
11861 " in section table [in module %s]"),
11862 dwp_file->name);
11863 }
11864 memset (ids, 255, sizeof_ids);
11865 memset (ids_seen, 255, sizeof (ids_seen));
11866 for (i = 0; i < nr_columns; ++i)
11867 {
11868 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11869
11870 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11871 {
11872 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11873 " in section table [in module %s]"),
11874 id, dwp_file->name);
11875 }
11876 if (ids_seen[id] != -1)
11877 {
11878 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11879 " id %d in section table [in module %s]"),
11880 id, dwp_file->name);
11881 }
11882 ids_seen[id] = i;
11883 ids[i] = id;
11884 }
11885 /* Must have exactly one info or types section. */
11886 if (((ids_seen[DW_SECT_INFO] != -1)
11887 + (ids_seen[DW_SECT_TYPES] != -1))
11888 != 1)
11889 {
11890 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11891 " DWO info/types section [in module %s]"),
11892 dwp_file->name);
11893 }
11894 /* Must have an abbrev section. */
11895 if (ids_seen[DW_SECT_ABBREV] == -1)
11896 {
11897 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11898 " section [in module %s]"),
11899 dwp_file->name);
11900 }
11901 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11902 htab->section_pool.v2.sizes =
11903 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11904 * nr_units * nr_columns);
11905 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11906 * nr_units * nr_columns))
11907 > index_end)
11908 {
11909 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11910 " [in module %s]"),
11911 dwp_file->name);
11912 }
11913 }
11914
11915 return htab;
11916 }
11917
11918 /* Update SECTIONS with the data from SECTP.
11919
11920 This function is like the other "locate" section routines that are
11921 passed to bfd_map_over_sections, but in this context the sections to
11922 read comes from the DWP V1 hash table, not the full ELF section table.
11923
11924 The result is non-zero for success, or zero if an error was found. */
11925
11926 static int
11927 locate_v1_virtual_dwo_sections (asection *sectp,
11928 struct virtual_v1_dwo_sections *sections)
11929 {
11930 const struct dwop_section_names *names = &dwop_section_names;
11931
11932 if (section_is_p (sectp->name, &names->abbrev_dwo))
11933 {
11934 /* There can be only one. */
11935 if (sections->abbrev.s.section != NULL)
11936 return 0;
11937 sections->abbrev.s.section = sectp;
11938 sections->abbrev.size = bfd_section_size (sectp);
11939 }
11940 else if (section_is_p (sectp->name, &names->info_dwo)
11941 || section_is_p (sectp->name, &names->types_dwo))
11942 {
11943 /* There can be only one. */
11944 if (sections->info_or_types.s.section != NULL)
11945 return 0;
11946 sections->info_or_types.s.section = sectp;
11947 sections->info_or_types.size = bfd_section_size (sectp);
11948 }
11949 else if (section_is_p (sectp->name, &names->line_dwo))
11950 {
11951 /* There can be only one. */
11952 if (sections->line.s.section != NULL)
11953 return 0;
11954 sections->line.s.section = sectp;
11955 sections->line.size = bfd_section_size (sectp);
11956 }
11957 else if (section_is_p (sectp->name, &names->loc_dwo))
11958 {
11959 /* There can be only one. */
11960 if (sections->loc.s.section != NULL)
11961 return 0;
11962 sections->loc.s.section = sectp;
11963 sections->loc.size = bfd_section_size (sectp);
11964 }
11965 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11966 {
11967 /* There can be only one. */
11968 if (sections->macinfo.s.section != NULL)
11969 return 0;
11970 sections->macinfo.s.section = sectp;
11971 sections->macinfo.size = bfd_section_size (sectp);
11972 }
11973 else if (section_is_p (sectp->name, &names->macro_dwo))
11974 {
11975 /* There can be only one. */
11976 if (sections->macro.s.section != NULL)
11977 return 0;
11978 sections->macro.s.section = sectp;
11979 sections->macro.size = bfd_section_size (sectp);
11980 }
11981 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11982 {
11983 /* There can be only one. */
11984 if (sections->str_offsets.s.section != NULL)
11985 return 0;
11986 sections->str_offsets.s.section = sectp;
11987 sections->str_offsets.size = bfd_section_size (sectp);
11988 }
11989 else
11990 {
11991 /* No other kind of section is valid. */
11992 return 0;
11993 }
11994
11995 return 1;
11996 }
11997
11998 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11999 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12000 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12001 This is for DWP version 1 files. */
12002
12003 static struct dwo_unit *
12004 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12005 struct dwp_file *dwp_file,
12006 uint32_t unit_index,
12007 const char *comp_dir,
12008 ULONGEST signature, int is_debug_types)
12009 {
12010 struct objfile *objfile = dwarf2_per_objfile->objfile;
12011 const struct dwp_hash_table *dwp_htab =
12012 is_debug_types ? dwp_file->tus : dwp_file->cus;
12013 bfd *dbfd = dwp_file->dbfd.get ();
12014 const char *kind = is_debug_types ? "TU" : "CU";
12015 struct dwo_file *dwo_file;
12016 struct dwo_unit *dwo_unit;
12017 struct virtual_v1_dwo_sections sections;
12018 void **dwo_file_slot;
12019 int i;
12020
12021 gdb_assert (dwp_file->version == 1);
12022
12023 if (dwarf_read_debug)
12024 {
12025 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12026 kind,
12027 pulongest (unit_index), hex_string (signature),
12028 dwp_file->name);
12029 }
12030
12031 /* Fetch the sections of this DWO unit.
12032 Put a limit on the number of sections we look for so that bad data
12033 doesn't cause us to loop forever. */
12034
12035 #define MAX_NR_V1_DWO_SECTIONS \
12036 (1 /* .debug_info or .debug_types */ \
12037 + 1 /* .debug_abbrev */ \
12038 + 1 /* .debug_line */ \
12039 + 1 /* .debug_loc */ \
12040 + 1 /* .debug_str_offsets */ \
12041 + 1 /* .debug_macro or .debug_macinfo */ \
12042 + 1 /* trailing zero */)
12043
12044 memset (&sections, 0, sizeof (sections));
12045
12046 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12047 {
12048 asection *sectp;
12049 uint32_t section_nr =
12050 read_4_bytes (dbfd,
12051 dwp_htab->section_pool.v1.indices
12052 + (unit_index + i) * sizeof (uint32_t));
12053
12054 if (section_nr == 0)
12055 break;
12056 if (section_nr >= dwp_file->num_sections)
12057 {
12058 error (_("Dwarf Error: bad DWP hash table, section number too large"
12059 " [in module %s]"),
12060 dwp_file->name);
12061 }
12062
12063 sectp = dwp_file->elf_sections[section_nr];
12064 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12065 {
12066 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12067 " [in module %s]"),
12068 dwp_file->name);
12069 }
12070 }
12071
12072 if (i < 2
12073 || sections.info_or_types.empty ()
12074 || sections.abbrev.empty ())
12075 {
12076 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12077 " [in module %s]"),
12078 dwp_file->name);
12079 }
12080 if (i == MAX_NR_V1_DWO_SECTIONS)
12081 {
12082 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12083 " [in module %s]"),
12084 dwp_file->name);
12085 }
12086
12087 /* It's easier for the rest of the code if we fake a struct dwo_file and
12088 have dwo_unit "live" in that. At least for now.
12089
12090 The DWP file can be made up of a random collection of CUs and TUs.
12091 However, for each CU + set of TUs that came from the same original DWO
12092 file, we can combine them back into a virtual DWO file to save space
12093 (fewer struct dwo_file objects to allocate). Remember that for really
12094 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12095
12096 std::string virtual_dwo_name =
12097 string_printf ("virtual-dwo/%d-%d-%d-%d",
12098 sections.abbrev.get_id (),
12099 sections.line.get_id (),
12100 sections.loc.get_id (),
12101 sections.str_offsets.get_id ());
12102 /* Can we use an existing virtual DWO file? */
12103 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12104 virtual_dwo_name.c_str (),
12105 comp_dir);
12106 /* Create one if necessary. */
12107 if (*dwo_file_slot == NULL)
12108 {
12109 if (dwarf_read_debug)
12110 {
12111 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12112 virtual_dwo_name.c_str ());
12113 }
12114 dwo_file = new struct dwo_file;
12115 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12116 virtual_dwo_name);
12117 dwo_file->comp_dir = comp_dir;
12118 dwo_file->sections.abbrev = sections.abbrev;
12119 dwo_file->sections.line = sections.line;
12120 dwo_file->sections.loc = sections.loc;
12121 dwo_file->sections.macinfo = sections.macinfo;
12122 dwo_file->sections.macro = sections.macro;
12123 dwo_file->sections.str_offsets = sections.str_offsets;
12124 /* The "str" section is global to the entire DWP file. */
12125 dwo_file->sections.str = dwp_file->sections.str;
12126 /* The info or types section is assigned below to dwo_unit,
12127 there's no need to record it in dwo_file.
12128 Also, we can't simply record type sections in dwo_file because
12129 we record a pointer into the vector in dwo_unit. As we collect more
12130 types we'll grow the vector and eventually have to reallocate space
12131 for it, invalidating all copies of pointers into the previous
12132 contents. */
12133 *dwo_file_slot = dwo_file;
12134 }
12135 else
12136 {
12137 if (dwarf_read_debug)
12138 {
12139 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12140 virtual_dwo_name.c_str ());
12141 }
12142 dwo_file = (struct dwo_file *) *dwo_file_slot;
12143 }
12144
12145 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12146 dwo_unit->dwo_file = dwo_file;
12147 dwo_unit->signature = signature;
12148 dwo_unit->section =
12149 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12150 *dwo_unit->section = sections.info_or_types;
12151 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12152
12153 return dwo_unit;
12154 }
12155
12156 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12157 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12158 piece within that section used by a TU/CU, return a virtual section
12159 of just that piece. */
12160
12161 static struct dwarf2_section_info
12162 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12163 struct dwarf2_section_info *section,
12164 bfd_size_type offset, bfd_size_type size)
12165 {
12166 struct dwarf2_section_info result;
12167 asection *sectp;
12168
12169 gdb_assert (section != NULL);
12170 gdb_assert (!section->is_virtual);
12171
12172 memset (&result, 0, sizeof (result));
12173 result.s.containing_section = section;
12174 result.is_virtual = true;
12175
12176 if (size == 0)
12177 return result;
12178
12179 sectp = section->get_bfd_section ();
12180
12181 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12182 bounds of the real section. This is a pretty-rare event, so just
12183 flag an error (easier) instead of a warning and trying to cope. */
12184 if (sectp == NULL
12185 || offset + size > bfd_section_size (sectp))
12186 {
12187 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12188 " in section %s [in module %s]"),
12189 sectp ? bfd_section_name (sectp) : "<unknown>",
12190 objfile_name (dwarf2_per_objfile->objfile));
12191 }
12192
12193 result.virtual_offset = offset;
12194 result.size = size;
12195 return result;
12196 }
12197
12198 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12199 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12200 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12201 This is for DWP version 2 files. */
12202
12203 static struct dwo_unit *
12204 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12205 struct dwp_file *dwp_file,
12206 uint32_t unit_index,
12207 const char *comp_dir,
12208 ULONGEST signature, int is_debug_types)
12209 {
12210 struct objfile *objfile = dwarf2_per_objfile->objfile;
12211 const struct dwp_hash_table *dwp_htab =
12212 is_debug_types ? dwp_file->tus : dwp_file->cus;
12213 bfd *dbfd = dwp_file->dbfd.get ();
12214 const char *kind = is_debug_types ? "TU" : "CU";
12215 struct dwo_file *dwo_file;
12216 struct dwo_unit *dwo_unit;
12217 struct virtual_v2_dwo_sections sections;
12218 void **dwo_file_slot;
12219 int i;
12220
12221 gdb_assert (dwp_file->version == 2);
12222
12223 if (dwarf_read_debug)
12224 {
12225 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12226 kind,
12227 pulongest (unit_index), hex_string (signature),
12228 dwp_file->name);
12229 }
12230
12231 /* Fetch the section offsets of this DWO unit. */
12232
12233 memset (&sections, 0, sizeof (sections));
12234
12235 for (i = 0; i < dwp_htab->nr_columns; ++i)
12236 {
12237 uint32_t offset = read_4_bytes (dbfd,
12238 dwp_htab->section_pool.v2.offsets
12239 + (((unit_index - 1) * dwp_htab->nr_columns
12240 + i)
12241 * sizeof (uint32_t)));
12242 uint32_t size = read_4_bytes (dbfd,
12243 dwp_htab->section_pool.v2.sizes
12244 + (((unit_index - 1) * dwp_htab->nr_columns
12245 + i)
12246 * sizeof (uint32_t)));
12247
12248 switch (dwp_htab->section_pool.v2.section_ids[i])
12249 {
12250 case DW_SECT_INFO:
12251 case DW_SECT_TYPES:
12252 sections.info_or_types_offset = offset;
12253 sections.info_or_types_size = size;
12254 break;
12255 case DW_SECT_ABBREV:
12256 sections.abbrev_offset = offset;
12257 sections.abbrev_size = size;
12258 break;
12259 case DW_SECT_LINE:
12260 sections.line_offset = offset;
12261 sections.line_size = size;
12262 break;
12263 case DW_SECT_LOC:
12264 sections.loc_offset = offset;
12265 sections.loc_size = size;
12266 break;
12267 case DW_SECT_STR_OFFSETS:
12268 sections.str_offsets_offset = offset;
12269 sections.str_offsets_size = size;
12270 break;
12271 case DW_SECT_MACINFO:
12272 sections.macinfo_offset = offset;
12273 sections.macinfo_size = size;
12274 break;
12275 case DW_SECT_MACRO:
12276 sections.macro_offset = offset;
12277 sections.macro_size = size;
12278 break;
12279 }
12280 }
12281
12282 /* It's easier for the rest of the code if we fake a struct dwo_file and
12283 have dwo_unit "live" in that. At least for now.
12284
12285 The DWP file can be made up of a random collection of CUs and TUs.
12286 However, for each CU + set of TUs that came from the same original DWO
12287 file, we can combine them back into a virtual DWO file to save space
12288 (fewer struct dwo_file objects to allocate). Remember that for really
12289 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12290
12291 std::string virtual_dwo_name =
12292 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12293 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12294 (long) (sections.line_size ? sections.line_offset : 0),
12295 (long) (sections.loc_size ? sections.loc_offset : 0),
12296 (long) (sections.str_offsets_size
12297 ? sections.str_offsets_offset : 0));
12298 /* Can we use an existing virtual DWO file? */
12299 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12300 virtual_dwo_name.c_str (),
12301 comp_dir);
12302 /* Create one if necessary. */
12303 if (*dwo_file_slot == NULL)
12304 {
12305 if (dwarf_read_debug)
12306 {
12307 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12308 virtual_dwo_name.c_str ());
12309 }
12310 dwo_file = new struct dwo_file;
12311 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12312 virtual_dwo_name);
12313 dwo_file->comp_dir = comp_dir;
12314 dwo_file->sections.abbrev =
12315 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12316 sections.abbrev_offset, sections.abbrev_size);
12317 dwo_file->sections.line =
12318 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12319 sections.line_offset, sections.line_size);
12320 dwo_file->sections.loc =
12321 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12322 sections.loc_offset, sections.loc_size);
12323 dwo_file->sections.macinfo =
12324 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12325 sections.macinfo_offset, sections.macinfo_size);
12326 dwo_file->sections.macro =
12327 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12328 sections.macro_offset, sections.macro_size);
12329 dwo_file->sections.str_offsets =
12330 create_dwp_v2_section (dwarf2_per_objfile,
12331 &dwp_file->sections.str_offsets,
12332 sections.str_offsets_offset,
12333 sections.str_offsets_size);
12334 /* The "str" section is global to the entire DWP file. */
12335 dwo_file->sections.str = dwp_file->sections.str;
12336 /* The info or types section is assigned below to dwo_unit,
12337 there's no need to record it in dwo_file.
12338 Also, we can't simply record type sections in dwo_file because
12339 we record a pointer into the vector in dwo_unit. As we collect more
12340 types we'll grow the vector and eventually have to reallocate space
12341 for it, invalidating all copies of pointers into the previous
12342 contents. */
12343 *dwo_file_slot = dwo_file;
12344 }
12345 else
12346 {
12347 if (dwarf_read_debug)
12348 {
12349 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12350 virtual_dwo_name.c_str ());
12351 }
12352 dwo_file = (struct dwo_file *) *dwo_file_slot;
12353 }
12354
12355 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12356 dwo_unit->dwo_file = dwo_file;
12357 dwo_unit->signature = signature;
12358 dwo_unit->section =
12359 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12360 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12361 is_debug_types
12362 ? &dwp_file->sections.types
12363 : &dwp_file->sections.info,
12364 sections.info_or_types_offset,
12365 sections.info_or_types_size);
12366 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12367
12368 return dwo_unit;
12369 }
12370
12371 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12372 Returns NULL if the signature isn't found. */
12373
12374 static struct dwo_unit *
12375 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12376 struct dwp_file *dwp_file, const char *comp_dir,
12377 ULONGEST signature, int is_debug_types)
12378 {
12379 const struct dwp_hash_table *dwp_htab =
12380 is_debug_types ? dwp_file->tus : dwp_file->cus;
12381 bfd *dbfd = dwp_file->dbfd.get ();
12382 uint32_t mask = dwp_htab->nr_slots - 1;
12383 uint32_t hash = signature & mask;
12384 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12385 unsigned int i;
12386 void **slot;
12387 struct dwo_unit find_dwo_cu;
12388
12389 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12390 find_dwo_cu.signature = signature;
12391 slot = htab_find_slot (is_debug_types
12392 ? dwp_file->loaded_tus
12393 : dwp_file->loaded_cus,
12394 &find_dwo_cu, INSERT);
12395
12396 if (*slot != NULL)
12397 return (struct dwo_unit *) *slot;
12398
12399 /* Use a for loop so that we don't loop forever on bad debug info. */
12400 for (i = 0; i < dwp_htab->nr_slots; ++i)
12401 {
12402 ULONGEST signature_in_table;
12403
12404 signature_in_table =
12405 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12406 if (signature_in_table == signature)
12407 {
12408 uint32_t unit_index =
12409 read_4_bytes (dbfd,
12410 dwp_htab->unit_table + hash * sizeof (uint32_t));
12411
12412 if (dwp_file->version == 1)
12413 {
12414 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12415 dwp_file, unit_index,
12416 comp_dir, signature,
12417 is_debug_types);
12418 }
12419 else
12420 {
12421 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12422 dwp_file, unit_index,
12423 comp_dir, signature,
12424 is_debug_types);
12425 }
12426 return (struct dwo_unit *) *slot;
12427 }
12428 if (signature_in_table == 0)
12429 return NULL;
12430 hash = (hash + hash2) & mask;
12431 }
12432
12433 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12434 " [in module %s]"),
12435 dwp_file->name);
12436 }
12437
12438 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12439 Open the file specified by FILE_NAME and hand it off to BFD for
12440 preliminary analysis. Return a newly initialized bfd *, which
12441 includes a canonicalized copy of FILE_NAME.
12442 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12443 SEARCH_CWD is true if the current directory is to be searched.
12444 It will be searched before debug-file-directory.
12445 If successful, the file is added to the bfd include table of the
12446 objfile's bfd (see gdb_bfd_record_inclusion).
12447 If unable to find/open the file, return NULL.
12448 NOTE: This function is derived from symfile_bfd_open. */
12449
12450 static gdb_bfd_ref_ptr
12451 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12452 const char *file_name, int is_dwp, int search_cwd)
12453 {
12454 int desc;
12455 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12456 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12457 to debug_file_directory. */
12458 const char *search_path;
12459 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12460
12461 gdb::unique_xmalloc_ptr<char> search_path_holder;
12462 if (search_cwd)
12463 {
12464 if (*debug_file_directory != '\0')
12465 {
12466 search_path_holder.reset (concat (".", dirname_separator_string,
12467 debug_file_directory,
12468 (char *) NULL));
12469 search_path = search_path_holder.get ();
12470 }
12471 else
12472 search_path = ".";
12473 }
12474 else
12475 search_path = debug_file_directory;
12476
12477 openp_flags flags = OPF_RETURN_REALPATH;
12478 if (is_dwp)
12479 flags |= OPF_SEARCH_IN_PATH;
12480
12481 gdb::unique_xmalloc_ptr<char> absolute_name;
12482 desc = openp (search_path, flags, file_name,
12483 O_RDONLY | O_BINARY, &absolute_name);
12484 if (desc < 0)
12485 return NULL;
12486
12487 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12488 gnutarget, desc));
12489 if (sym_bfd == NULL)
12490 return NULL;
12491 bfd_set_cacheable (sym_bfd.get (), 1);
12492
12493 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12494 return NULL;
12495
12496 /* Success. Record the bfd as having been included by the objfile's bfd.
12497 This is important because things like demangled_names_hash lives in the
12498 objfile's per_bfd space and may have references to things like symbol
12499 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12500 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12501
12502 return sym_bfd;
12503 }
12504
12505 /* Try to open DWO file FILE_NAME.
12506 COMP_DIR is the DW_AT_comp_dir attribute.
12507 The result is the bfd handle of the file.
12508 If there is a problem finding or opening the file, return NULL.
12509 Upon success, the canonicalized path of the file is stored in the bfd,
12510 same as symfile_bfd_open. */
12511
12512 static gdb_bfd_ref_ptr
12513 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12514 const char *file_name, const char *comp_dir)
12515 {
12516 if (IS_ABSOLUTE_PATH (file_name))
12517 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12518 0 /*is_dwp*/, 0 /*search_cwd*/);
12519
12520 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12521
12522 if (comp_dir != NULL)
12523 {
12524 gdb::unique_xmalloc_ptr<char> path_to_try
12525 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12526
12527 /* NOTE: If comp_dir is a relative path, this will also try the
12528 search path, which seems useful. */
12529 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12530 path_to_try.get (),
12531 0 /*is_dwp*/,
12532 1 /*search_cwd*/));
12533 if (abfd != NULL)
12534 return abfd;
12535 }
12536
12537 /* That didn't work, try debug-file-directory, which, despite its name,
12538 is a list of paths. */
12539
12540 if (*debug_file_directory == '\0')
12541 return NULL;
12542
12543 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12544 0 /*is_dwp*/, 1 /*search_cwd*/);
12545 }
12546
12547 /* This function is mapped across the sections and remembers the offset and
12548 size of each of the DWO debugging sections we are interested in. */
12549
12550 static void
12551 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12552 {
12553 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12554 const struct dwop_section_names *names = &dwop_section_names;
12555
12556 if (section_is_p (sectp->name, &names->abbrev_dwo))
12557 {
12558 dwo_sections->abbrev.s.section = sectp;
12559 dwo_sections->abbrev.size = bfd_section_size (sectp);
12560 }
12561 else if (section_is_p (sectp->name, &names->info_dwo))
12562 {
12563 dwo_sections->info.s.section = sectp;
12564 dwo_sections->info.size = bfd_section_size (sectp);
12565 }
12566 else if (section_is_p (sectp->name, &names->line_dwo))
12567 {
12568 dwo_sections->line.s.section = sectp;
12569 dwo_sections->line.size = bfd_section_size (sectp);
12570 }
12571 else if (section_is_p (sectp->name, &names->loc_dwo))
12572 {
12573 dwo_sections->loc.s.section = sectp;
12574 dwo_sections->loc.size = bfd_section_size (sectp);
12575 }
12576 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12577 {
12578 dwo_sections->macinfo.s.section = sectp;
12579 dwo_sections->macinfo.size = bfd_section_size (sectp);
12580 }
12581 else if (section_is_p (sectp->name, &names->macro_dwo))
12582 {
12583 dwo_sections->macro.s.section = sectp;
12584 dwo_sections->macro.size = bfd_section_size (sectp);
12585 }
12586 else if (section_is_p (sectp->name, &names->str_dwo))
12587 {
12588 dwo_sections->str.s.section = sectp;
12589 dwo_sections->str.size = bfd_section_size (sectp);
12590 }
12591 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12592 {
12593 dwo_sections->str_offsets.s.section = sectp;
12594 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12595 }
12596 else if (section_is_p (sectp->name, &names->types_dwo))
12597 {
12598 struct dwarf2_section_info type_section;
12599
12600 memset (&type_section, 0, sizeof (type_section));
12601 type_section.s.section = sectp;
12602 type_section.size = bfd_section_size (sectp);
12603 dwo_sections->types.push_back (type_section);
12604 }
12605 }
12606
12607 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12608 by PER_CU. This is for the non-DWP case.
12609 The result is NULL if DWO_NAME can't be found. */
12610
12611 static struct dwo_file *
12612 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12613 const char *dwo_name, const char *comp_dir)
12614 {
12615 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12616
12617 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12618 if (dbfd == NULL)
12619 {
12620 if (dwarf_read_debug)
12621 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12622 return NULL;
12623 }
12624
12625 dwo_file_up dwo_file (new struct dwo_file);
12626 dwo_file->dwo_name = dwo_name;
12627 dwo_file->comp_dir = comp_dir;
12628 dwo_file->dbfd = std::move (dbfd);
12629
12630 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12631 &dwo_file->sections);
12632
12633 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12634 dwo_file->sections.info, dwo_file->cus);
12635
12636 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12637 dwo_file->sections.types, dwo_file->tus);
12638
12639 if (dwarf_read_debug)
12640 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12641
12642 return dwo_file.release ();
12643 }
12644
12645 /* This function is mapped across the sections and remembers the offset and
12646 size of each of the DWP debugging sections common to version 1 and 2 that
12647 we are interested in. */
12648
12649 static void
12650 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12651 void *dwp_file_ptr)
12652 {
12653 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12654 const struct dwop_section_names *names = &dwop_section_names;
12655 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12656
12657 /* Record the ELF section number for later lookup: this is what the
12658 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12659 gdb_assert (elf_section_nr < dwp_file->num_sections);
12660 dwp_file->elf_sections[elf_section_nr] = sectp;
12661
12662 /* Look for specific sections that we need. */
12663 if (section_is_p (sectp->name, &names->str_dwo))
12664 {
12665 dwp_file->sections.str.s.section = sectp;
12666 dwp_file->sections.str.size = bfd_section_size (sectp);
12667 }
12668 else if (section_is_p (sectp->name, &names->cu_index))
12669 {
12670 dwp_file->sections.cu_index.s.section = sectp;
12671 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12672 }
12673 else if (section_is_p (sectp->name, &names->tu_index))
12674 {
12675 dwp_file->sections.tu_index.s.section = sectp;
12676 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12677 }
12678 }
12679
12680 /* This function is mapped across the sections and remembers the offset and
12681 size of each of the DWP version 2 debugging sections that we are interested
12682 in. This is split into a separate function because we don't know if we
12683 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12684
12685 static void
12686 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12687 {
12688 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12689 const struct dwop_section_names *names = &dwop_section_names;
12690 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12691
12692 /* Record the ELF section number for later lookup: this is what the
12693 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12694 gdb_assert (elf_section_nr < dwp_file->num_sections);
12695 dwp_file->elf_sections[elf_section_nr] = sectp;
12696
12697 /* Look for specific sections that we need. */
12698 if (section_is_p (sectp->name, &names->abbrev_dwo))
12699 {
12700 dwp_file->sections.abbrev.s.section = sectp;
12701 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12702 }
12703 else if (section_is_p (sectp->name, &names->info_dwo))
12704 {
12705 dwp_file->sections.info.s.section = sectp;
12706 dwp_file->sections.info.size = bfd_section_size (sectp);
12707 }
12708 else if (section_is_p (sectp->name, &names->line_dwo))
12709 {
12710 dwp_file->sections.line.s.section = sectp;
12711 dwp_file->sections.line.size = bfd_section_size (sectp);
12712 }
12713 else if (section_is_p (sectp->name, &names->loc_dwo))
12714 {
12715 dwp_file->sections.loc.s.section = sectp;
12716 dwp_file->sections.loc.size = bfd_section_size (sectp);
12717 }
12718 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12719 {
12720 dwp_file->sections.macinfo.s.section = sectp;
12721 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12722 }
12723 else if (section_is_p (sectp->name, &names->macro_dwo))
12724 {
12725 dwp_file->sections.macro.s.section = sectp;
12726 dwp_file->sections.macro.size = bfd_section_size (sectp);
12727 }
12728 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12729 {
12730 dwp_file->sections.str_offsets.s.section = sectp;
12731 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12732 }
12733 else if (section_is_p (sectp->name, &names->types_dwo))
12734 {
12735 dwp_file->sections.types.s.section = sectp;
12736 dwp_file->sections.types.size = bfd_section_size (sectp);
12737 }
12738 }
12739
12740 /* Hash function for dwp_file loaded CUs/TUs. */
12741
12742 static hashval_t
12743 hash_dwp_loaded_cutus (const void *item)
12744 {
12745 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12746
12747 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12748 return dwo_unit->signature;
12749 }
12750
12751 /* Equality function for dwp_file loaded CUs/TUs. */
12752
12753 static int
12754 eq_dwp_loaded_cutus (const void *a, const void *b)
12755 {
12756 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12757 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12758
12759 return dua->signature == dub->signature;
12760 }
12761
12762 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12763
12764 static htab_t
12765 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12766 {
12767 return htab_create_alloc_ex (3,
12768 hash_dwp_loaded_cutus,
12769 eq_dwp_loaded_cutus,
12770 NULL,
12771 &objfile->objfile_obstack,
12772 hashtab_obstack_allocate,
12773 dummy_obstack_deallocate);
12774 }
12775
12776 /* Try to open DWP file FILE_NAME.
12777 The result is the bfd handle of the file.
12778 If there is a problem finding or opening the file, return NULL.
12779 Upon success, the canonicalized path of the file is stored in the bfd,
12780 same as symfile_bfd_open. */
12781
12782 static gdb_bfd_ref_ptr
12783 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12784 const char *file_name)
12785 {
12786 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12787 1 /*is_dwp*/,
12788 1 /*search_cwd*/));
12789 if (abfd != NULL)
12790 return abfd;
12791
12792 /* Work around upstream bug 15652.
12793 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12794 [Whether that's a "bug" is debatable, but it is getting in our way.]
12795 We have no real idea where the dwp file is, because gdb's realpath-ing
12796 of the executable's path may have discarded the needed info.
12797 [IWBN if the dwp file name was recorded in the executable, akin to
12798 .gnu_debuglink, but that doesn't exist yet.]
12799 Strip the directory from FILE_NAME and search again. */
12800 if (*debug_file_directory != '\0')
12801 {
12802 /* Don't implicitly search the current directory here.
12803 If the user wants to search "." to handle this case,
12804 it must be added to debug-file-directory. */
12805 return try_open_dwop_file (dwarf2_per_objfile,
12806 lbasename (file_name), 1 /*is_dwp*/,
12807 0 /*search_cwd*/);
12808 }
12809
12810 return NULL;
12811 }
12812
12813 /* Initialize the use of the DWP file for the current objfile.
12814 By convention the name of the DWP file is ${objfile}.dwp.
12815 The result is NULL if it can't be found. */
12816
12817 static std::unique_ptr<struct dwp_file>
12818 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12819 {
12820 struct objfile *objfile = dwarf2_per_objfile->objfile;
12821
12822 /* Try to find first .dwp for the binary file before any symbolic links
12823 resolving. */
12824
12825 /* If the objfile is a debug file, find the name of the real binary
12826 file and get the name of dwp file from there. */
12827 std::string dwp_name;
12828 if (objfile->separate_debug_objfile_backlink != NULL)
12829 {
12830 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12831 const char *backlink_basename = lbasename (backlink->original_name);
12832
12833 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12834 }
12835 else
12836 dwp_name = objfile->original_name;
12837
12838 dwp_name += ".dwp";
12839
12840 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12841 if (dbfd == NULL
12842 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12843 {
12844 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12845 dwp_name = objfile_name (objfile);
12846 dwp_name += ".dwp";
12847 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12848 }
12849
12850 if (dbfd == NULL)
12851 {
12852 if (dwarf_read_debug)
12853 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12854 return std::unique_ptr<dwp_file> ();
12855 }
12856
12857 const char *name = bfd_get_filename (dbfd.get ());
12858 std::unique_ptr<struct dwp_file> dwp_file
12859 (new struct dwp_file (name, std::move (dbfd)));
12860
12861 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12862 dwp_file->elf_sections =
12863 OBSTACK_CALLOC (&objfile->objfile_obstack,
12864 dwp_file->num_sections, asection *);
12865
12866 bfd_map_over_sections (dwp_file->dbfd.get (),
12867 dwarf2_locate_common_dwp_sections,
12868 dwp_file.get ());
12869
12870 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12871 0);
12872
12873 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12874 1);
12875
12876 /* The DWP file version is stored in the hash table. Oh well. */
12877 if (dwp_file->cus && dwp_file->tus
12878 && dwp_file->cus->version != dwp_file->tus->version)
12879 {
12880 /* Technically speaking, we should try to limp along, but this is
12881 pretty bizarre. We use pulongest here because that's the established
12882 portability solution (e.g, we cannot use %u for uint32_t). */
12883 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12884 " TU version %s [in DWP file %s]"),
12885 pulongest (dwp_file->cus->version),
12886 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12887 }
12888
12889 if (dwp_file->cus)
12890 dwp_file->version = dwp_file->cus->version;
12891 else if (dwp_file->tus)
12892 dwp_file->version = dwp_file->tus->version;
12893 else
12894 dwp_file->version = 2;
12895
12896 if (dwp_file->version == 2)
12897 bfd_map_over_sections (dwp_file->dbfd.get (),
12898 dwarf2_locate_v2_dwp_sections,
12899 dwp_file.get ());
12900
12901 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12902 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12903
12904 if (dwarf_read_debug)
12905 {
12906 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12907 fprintf_unfiltered (gdb_stdlog,
12908 " %s CUs, %s TUs\n",
12909 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12910 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12911 }
12912
12913 return dwp_file;
12914 }
12915
12916 /* Wrapper around open_and_init_dwp_file, only open it once. */
12917
12918 static struct dwp_file *
12919 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12920 {
12921 if (! dwarf2_per_objfile->dwp_checked)
12922 {
12923 dwarf2_per_objfile->dwp_file
12924 = open_and_init_dwp_file (dwarf2_per_objfile);
12925 dwarf2_per_objfile->dwp_checked = 1;
12926 }
12927 return dwarf2_per_objfile->dwp_file.get ();
12928 }
12929
12930 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12931 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12932 or in the DWP file for the objfile, referenced by THIS_UNIT.
12933 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12934 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12935
12936 This is called, for example, when wanting to read a variable with a
12937 complex location. Therefore we don't want to do file i/o for every call.
12938 Therefore we don't want to look for a DWO file on every call.
12939 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12940 then we check if we've already seen DWO_NAME, and only THEN do we check
12941 for a DWO file.
12942
12943 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12944 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12945
12946 static struct dwo_unit *
12947 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12948 const char *dwo_name, const char *comp_dir,
12949 ULONGEST signature, int is_debug_types)
12950 {
12951 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12952 struct objfile *objfile = dwarf2_per_objfile->objfile;
12953 const char *kind = is_debug_types ? "TU" : "CU";
12954 void **dwo_file_slot;
12955 struct dwo_file *dwo_file;
12956 struct dwp_file *dwp_file;
12957
12958 /* First see if there's a DWP file.
12959 If we have a DWP file but didn't find the DWO inside it, don't
12960 look for the original DWO file. It makes gdb behave differently
12961 depending on whether one is debugging in the build tree. */
12962
12963 dwp_file = get_dwp_file (dwarf2_per_objfile);
12964 if (dwp_file != NULL)
12965 {
12966 const struct dwp_hash_table *dwp_htab =
12967 is_debug_types ? dwp_file->tus : dwp_file->cus;
12968
12969 if (dwp_htab != NULL)
12970 {
12971 struct dwo_unit *dwo_cutu =
12972 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12973 signature, is_debug_types);
12974
12975 if (dwo_cutu != NULL)
12976 {
12977 if (dwarf_read_debug)
12978 {
12979 fprintf_unfiltered (gdb_stdlog,
12980 "Virtual DWO %s %s found: @%s\n",
12981 kind, hex_string (signature),
12982 host_address_to_string (dwo_cutu));
12983 }
12984 return dwo_cutu;
12985 }
12986 }
12987 }
12988 else
12989 {
12990 /* No DWP file, look for the DWO file. */
12991
12992 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12993 dwo_name, comp_dir);
12994 if (*dwo_file_slot == NULL)
12995 {
12996 /* Read in the file and build a table of the CUs/TUs it contains. */
12997 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12998 }
12999 /* NOTE: This will be NULL if unable to open the file. */
13000 dwo_file = (struct dwo_file *) *dwo_file_slot;
13001
13002 if (dwo_file != NULL)
13003 {
13004 struct dwo_unit *dwo_cutu = NULL;
13005
13006 if (is_debug_types && dwo_file->tus)
13007 {
13008 struct dwo_unit find_dwo_cutu;
13009
13010 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13011 find_dwo_cutu.signature = signature;
13012 dwo_cutu
13013 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13014 }
13015 else if (!is_debug_types && dwo_file->cus)
13016 {
13017 struct dwo_unit find_dwo_cutu;
13018
13019 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13020 find_dwo_cutu.signature = signature;
13021 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13022 &find_dwo_cutu);
13023 }
13024
13025 if (dwo_cutu != NULL)
13026 {
13027 if (dwarf_read_debug)
13028 {
13029 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13030 kind, dwo_name, hex_string (signature),
13031 host_address_to_string (dwo_cutu));
13032 }
13033 return dwo_cutu;
13034 }
13035 }
13036 }
13037
13038 /* We didn't find it. This could mean a dwo_id mismatch, or
13039 someone deleted the DWO/DWP file, or the search path isn't set up
13040 correctly to find the file. */
13041
13042 if (dwarf_read_debug)
13043 {
13044 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13045 kind, dwo_name, hex_string (signature));
13046 }
13047
13048 /* This is a warning and not a complaint because it can be caused by
13049 pilot error (e.g., user accidentally deleting the DWO). */
13050 {
13051 /* Print the name of the DWP file if we looked there, helps the user
13052 better diagnose the problem. */
13053 std::string dwp_text;
13054
13055 if (dwp_file != NULL)
13056 dwp_text = string_printf (" [in DWP file %s]",
13057 lbasename (dwp_file->name));
13058
13059 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13060 " [in module %s]"),
13061 kind, dwo_name, hex_string (signature),
13062 dwp_text.c_str (),
13063 this_unit->is_debug_types ? "TU" : "CU",
13064 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13065 }
13066 return NULL;
13067 }
13068
13069 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13070 See lookup_dwo_cutu_unit for details. */
13071
13072 static struct dwo_unit *
13073 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13074 const char *dwo_name, const char *comp_dir,
13075 ULONGEST signature)
13076 {
13077 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13078 }
13079
13080 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13081 See lookup_dwo_cutu_unit for details. */
13082
13083 static struct dwo_unit *
13084 lookup_dwo_type_unit (struct signatured_type *this_tu,
13085 const char *dwo_name, const char *comp_dir)
13086 {
13087 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13088 }
13089
13090 /* Traversal function for queue_and_load_all_dwo_tus. */
13091
13092 static int
13093 queue_and_load_dwo_tu (void **slot, void *info)
13094 {
13095 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13096 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13097 ULONGEST signature = dwo_unit->signature;
13098 struct signatured_type *sig_type =
13099 lookup_dwo_signatured_type (per_cu->cu, signature);
13100
13101 if (sig_type != NULL)
13102 {
13103 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13104
13105 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13106 a real dependency of PER_CU on SIG_TYPE. That is detected later
13107 while processing PER_CU. */
13108 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13109 load_full_type_unit (sig_cu);
13110 per_cu->imported_symtabs_push (sig_cu);
13111 }
13112
13113 return 1;
13114 }
13115
13116 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13117 The DWO may have the only definition of the type, though it may not be
13118 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13119 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13120
13121 static void
13122 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13123 {
13124 struct dwo_unit *dwo_unit;
13125 struct dwo_file *dwo_file;
13126
13127 gdb_assert (!per_cu->is_debug_types);
13128 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13129 gdb_assert (per_cu->cu != NULL);
13130
13131 dwo_unit = per_cu->cu->dwo_unit;
13132 gdb_assert (dwo_unit != NULL);
13133
13134 dwo_file = dwo_unit->dwo_file;
13135 if (dwo_file->tus != NULL)
13136 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13137 }
13138
13139 /* Read in various DIEs. */
13140
13141 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13142 Inherit only the children of the DW_AT_abstract_origin DIE not being
13143 already referenced by DW_AT_abstract_origin from the children of the
13144 current DIE. */
13145
13146 static void
13147 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13148 {
13149 struct die_info *child_die;
13150 sect_offset *offsetp;
13151 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13152 struct die_info *origin_die;
13153 /* Iterator of the ORIGIN_DIE children. */
13154 struct die_info *origin_child_die;
13155 struct attribute *attr;
13156 struct dwarf2_cu *origin_cu;
13157 struct pending **origin_previous_list_in_scope;
13158
13159 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13160 if (!attr)
13161 return;
13162
13163 /* Note that following die references may follow to a die in a
13164 different cu. */
13165
13166 origin_cu = cu;
13167 origin_die = follow_die_ref (die, attr, &origin_cu);
13168
13169 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13170 symbols in. */
13171 origin_previous_list_in_scope = origin_cu->list_in_scope;
13172 origin_cu->list_in_scope = cu->list_in_scope;
13173
13174 if (die->tag != origin_die->tag
13175 && !(die->tag == DW_TAG_inlined_subroutine
13176 && origin_die->tag == DW_TAG_subprogram))
13177 complaint (_("DIE %s and its abstract origin %s have different tags"),
13178 sect_offset_str (die->sect_off),
13179 sect_offset_str (origin_die->sect_off));
13180
13181 std::vector<sect_offset> offsets;
13182
13183 for (child_die = die->child;
13184 child_die && child_die->tag;
13185 child_die = sibling_die (child_die))
13186 {
13187 struct die_info *child_origin_die;
13188 struct dwarf2_cu *child_origin_cu;
13189
13190 /* We are trying to process concrete instance entries:
13191 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13192 it's not relevant to our analysis here. i.e. detecting DIEs that are
13193 present in the abstract instance but not referenced in the concrete
13194 one. */
13195 if (child_die->tag == DW_TAG_call_site
13196 || child_die->tag == DW_TAG_GNU_call_site)
13197 continue;
13198
13199 /* For each CHILD_DIE, find the corresponding child of
13200 ORIGIN_DIE. If there is more than one layer of
13201 DW_AT_abstract_origin, follow them all; there shouldn't be,
13202 but GCC versions at least through 4.4 generate this (GCC PR
13203 40573). */
13204 child_origin_die = child_die;
13205 child_origin_cu = cu;
13206 while (1)
13207 {
13208 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13209 child_origin_cu);
13210 if (attr == NULL)
13211 break;
13212 child_origin_die = follow_die_ref (child_origin_die, attr,
13213 &child_origin_cu);
13214 }
13215
13216 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13217 counterpart may exist. */
13218 if (child_origin_die != child_die)
13219 {
13220 if (child_die->tag != child_origin_die->tag
13221 && !(child_die->tag == DW_TAG_inlined_subroutine
13222 && child_origin_die->tag == DW_TAG_subprogram))
13223 complaint (_("Child DIE %s and its abstract origin %s have "
13224 "different tags"),
13225 sect_offset_str (child_die->sect_off),
13226 sect_offset_str (child_origin_die->sect_off));
13227 if (child_origin_die->parent != origin_die)
13228 complaint (_("Child DIE %s and its abstract origin %s have "
13229 "different parents"),
13230 sect_offset_str (child_die->sect_off),
13231 sect_offset_str (child_origin_die->sect_off));
13232 else
13233 offsets.push_back (child_origin_die->sect_off);
13234 }
13235 }
13236 std::sort (offsets.begin (), offsets.end ());
13237 sect_offset *offsets_end = offsets.data () + offsets.size ();
13238 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13239 if (offsetp[-1] == *offsetp)
13240 complaint (_("Multiple children of DIE %s refer "
13241 "to DIE %s as their abstract origin"),
13242 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13243
13244 offsetp = offsets.data ();
13245 origin_child_die = origin_die->child;
13246 while (origin_child_die && origin_child_die->tag)
13247 {
13248 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13249 while (offsetp < offsets_end
13250 && *offsetp < origin_child_die->sect_off)
13251 offsetp++;
13252 if (offsetp >= offsets_end
13253 || *offsetp > origin_child_die->sect_off)
13254 {
13255 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13256 Check whether we're already processing ORIGIN_CHILD_DIE.
13257 This can happen with mutually referenced abstract_origins.
13258 PR 16581. */
13259 if (!origin_child_die->in_process)
13260 process_die (origin_child_die, origin_cu);
13261 }
13262 origin_child_die = sibling_die (origin_child_die);
13263 }
13264 origin_cu->list_in_scope = origin_previous_list_in_scope;
13265
13266 if (cu != origin_cu)
13267 compute_delayed_physnames (origin_cu);
13268 }
13269
13270 static void
13271 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13272 {
13273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13274 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13275 struct context_stack *newobj;
13276 CORE_ADDR lowpc;
13277 CORE_ADDR highpc;
13278 struct die_info *child_die;
13279 struct attribute *attr, *call_line, *call_file;
13280 const char *name;
13281 CORE_ADDR baseaddr;
13282 struct block *block;
13283 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13284 std::vector<struct symbol *> template_args;
13285 struct template_symbol *templ_func = NULL;
13286
13287 if (inlined_func)
13288 {
13289 /* If we do not have call site information, we can't show the
13290 caller of this inlined function. That's too confusing, so
13291 only use the scope for local variables. */
13292 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13293 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13294 if (call_line == NULL || call_file == NULL)
13295 {
13296 read_lexical_block_scope (die, cu);
13297 return;
13298 }
13299 }
13300
13301 baseaddr = objfile->text_section_offset ();
13302
13303 name = dwarf2_name (die, cu);
13304
13305 /* Ignore functions with missing or empty names. These are actually
13306 illegal according to the DWARF standard. */
13307 if (name == NULL)
13308 {
13309 complaint (_("missing name for subprogram DIE at %s"),
13310 sect_offset_str (die->sect_off));
13311 return;
13312 }
13313
13314 /* Ignore functions with missing or invalid low and high pc attributes. */
13315 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13316 <= PC_BOUNDS_INVALID)
13317 {
13318 attr = dwarf2_attr (die, DW_AT_external, cu);
13319 if (!attr || !DW_UNSND (attr))
13320 complaint (_("cannot get low and high bounds "
13321 "for subprogram DIE at %s"),
13322 sect_offset_str (die->sect_off));
13323 return;
13324 }
13325
13326 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13327 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13328
13329 /* If we have any template arguments, then we must allocate a
13330 different sort of symbol. */
13331 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13332 {
13333 if (child_die->tag == DW_TAG_template_type_param
13334 || child_die->tag == DW_TAG_template_value_param)
13335 {
13336 templ_func = allocate_template_symbol (objfile);
13337 templ_func->subclass = SYMBOL_TEMPLATE;
13338 break;
13339 }
13340 }
13341
13342 newobj = cu->get_builder ()->push_context (0, lowpc);
13343 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13344 (struct symbol *) templ_func);
13345
13346 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13347 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13348 cu->language);
13349
13350 /* If there is a location expression for DW_AT_frame_base, record
13351 it. */
13352 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13353 if (attr != nullptr)
13354 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13355
13356 /* If there is a location for the static link, record it. */
13357 newobj->static_link = NULL;
13358 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13359 if (attr != nullptr)
13360 {
13361 newobj->static_link
13362 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13363 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13364 dwarf2_per_cu_addr_type (cu->per_cu));
13365 }
13366
13367 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13368
13369 if (die->child != NULL)
13370 {
13371 child_die = die->child;
13372 while (child_die && child_die->tag)
13373 {
13374 if (child_die->tag == DW_TAG_template_type_param
13375 || child_die->tag == DW_TAG_template_value_param)
13376 {
13377 struct symbol *arg = new_symbol (child_die, NULL, cu);
13378
13379 if (arg != NULL)
13380 template_args.push_back (arg);
13381 }
13382 else
13383 process_die (child_die, cu);
13384 child_die = sibling_die (child_die);
13385 }
13386 }
13387
13388 inherit_abstract_dies (die, cu);
13389
13390 /* If we have a DW_AT_specification, we might need to import using
13391 directives from the context of the specification DIE. See the
13392 comment in determine_prefix. */
13393 if (cu->language == language_cplus
13394 && dwarf2_attr (die, DW_AT_specification, cu))
13395 {
13396 struct dwarf2_cu *spec_cu = cu;
13397 struct die_info *spec_die = die_specification (die, &spec_cu);
13398
13399 while (spec_die)
13400 {
13401 child_die = spec_die->child;
13402 while (child_die && child_die->tag)
13403 {
13404 if (child_die->tag == DW_TAG_imported_module)
13405 process_die (child_die, spec_cu);
13406 child_die = sibling_die (child_die);
13407 }
13408
13409 /* In some cases, GCC generates specification DIEs that
13410 themselves contain DW_AT_specification attributes. */
13411 spec_die = die_specification (spec_die, &spec_cu);
13412 }
13413 }
13414
13415 struct context_stack cstk = cu->get_builder ()->pop_context ();
13416 /* Make a block for the local symbols within. */
13417 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13418 cstk.static_link, lowpc, highpc);
13419
13420 /* For C++, set the block's scope. */
13421 if ((cu->language == language_cplus
13422 || cu->language == language_fortran
13423 || cu->language == language_d
13424 || cu->language == language_rust)
13425 && cu->processing_has_namespace_info)
13426 block_set_scope (block, determine_prefix (die, cu),
13427 &objfile->objfile_obstack);
13428
13429 /* If we have address ranges, record them. */
13430 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13431
13432 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13433
13434 /* Attach template arguments to function. */
13435 if (!template_args.empty ())
13436 {
13437 gdb_assert (templ_func != NULL);
13438
13439 templ_func->n_template_arguments = template_args.size ();
13440 templ_func->template_arguments
13441 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13442 templ_func->n_template_arguments);
13443 memcpy (templ_func->template_arguments,
13444 template_args.data (),
13445 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13446
13447 /* Make sure that the symtab is set on the new symbols. Even
13448 though they don't appear in this symtab directly, other parts
13449 of gdb assume that symbols do, and this is reasonably
13450 true. */
13451 for (symbol *sym : template_args)
13452 symbol_set_symtab (sym, symbol_symtab (templ_func));
13453 }
13454
13455 /* In C++, we can have functions nested inside functions (e.g., when
13456 a function declares a class that has methods). This means that
13457 when we finish processing a function scope, we may need to go
13458 back to building a containing block's symbol lists. */
13459 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13460 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13461
13462 /* If we've finished processing a top-level function, subsequent
13463 symbols go in the file symbol list. */
13464 if (cu->get_builder ()->outermost_context_p ())
13465 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13466 }
13467
13468 /* Process all the DIES contained within a lexical block scope. Start
13469 a new scope, process the dies, and then close the scope. */
13470
13471 static void
13472 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13473 {
13474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13476 CORE_ADDR lowpc, highpc;
13477 struct die_info *child_die;
13478 CORE_ADDR baseaddr;
13479
13480 baseaddr = objfile->text_section_offset ();
13481
13482 /* Ignore blocks with missing or invalid low and high pc attributes. */
13483 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13484 as multiple lexical blocks? Handling children in a sane way would
13485 be nasty. Might be easier to properly extend generic blocks to
13486 describe ranges. */
13487 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13488 {
13489 case PC_BOUNDS_NOT_PRESENT:
13490 /* DW_TAG_lexical_block has no attributes, process its children as if
13491 there was no wrapping by that DW_TAG_lexical_block.
13492 GCC does no longer produces such DWARF since GCC r224161. */
13493 for (child_die = die->child;
13494 child_die != NULL && child_die->tag;
13495 child_die = sibling_die (child_die))
13496 process_die (child_die, cu);
13497 return;
13498 case PC_BOUNDS_INVALID:
13499 return;
13500 }
13501 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13502 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13503
13504 cu->get_builder ()->push_context (0, lowpc);
13505 if (die->child != NULL)
13506 {
13507 child_die = die->child;
13508 while (child_die && child_die->tag)
13509 {
13510 process_die (child_die, cu);
13511 child_die = sibling_die (child_die);
13512 }
13513 }
13514 inherit_abstract_dies (die, cu);
13515 struct context_stack cstk = cu->get_builder ()->pop_context ();
13516
13517 if (*cu->get_builder ()->get_local_symbols () != NULL
13518 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13519 {
13520 struct block *block
13521 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13522 cstk.start_addr, highpc);
13523
13524 /* Note that recording ranges after traversing children, as we
13525 do here, means that recording a parent's ranges entails
13526 walking across all its children's ranges as they appear in
13527 the address map, which is quadratic behavior.
13528
13529 It would be nicer to record the parent's ranges before
13530 traversing its children, simply overriding whatever you find
13531 there. But since we don't even decide whether to create a
13532 block until after we've traversed its children, that's hard
13533 to do. */
13534 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13535 }
13536 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13537 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13538 }
13539
13540 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13541
13542 static void
13543 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13544 {
13545 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13546 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13547 CORE_ADDR pc, baseaddr;
13548 struct attribute *attr;
13549 struct call_site *call_site, call_site_local;
13550 void **slot;
13551 int nparams;
13552 struct die_info *child_die;
13553
13554 baseaddr = objfile->text_section_offset ();
13555
13556 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13557 if (attr == NULL)
13558 {
13559 /* This was a pre-DWARF-5 GNU extension alias
13560 for DW_AT_call_return_pc. */
13561 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13562 }
13563 if (!attr)
13564 {
13565 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13566 "DIE %s [in module %s]"),
13567 sect_offset_str (die->sect_off), objfile_name (objfile));
13568 return;
13569 }
13570 pc = attr->value_as_address () + baseaddr;
13571 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13572
13573 if (cu->call_site_htab == NULL)
13574 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13575 NULL, &objfile->objfile_obstack,
13576 hashtab_obstack_allocate, NULL);
13577 call_site_local.pc = pc;
13578 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13579 if (*slot != NULL)
13580 {
13581 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13582 "DIE %s [in module %s]"),
13583 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13584 objfile_name (objfile));
13585 return;
13586 }
13587
13588 /* Count parameters at the caller. */
13589
13590 nparams = 0;
13591 for (child_die = die->child; child_die && child_die->tag;
13592 child_die = sibling_die (child_die))
13593 {
13594 if (child_die->tag != DW_TAG_call_site_parameter
13595 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13596 {
13597 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13598 "DW_TAG_call_site child DIE %s [in module %s]"),
13599 child_die->tag, sect_offset_str (child_die->sect_off),
13600 objfile_name (objfile));
13601 continue;
13602 }
13603
13604 nparams++;
13605 }
13606
13607 call_site
13608 = ((struct call_site *)
13609 obstack_alloc (&objfile->objfile_obstack,
13610 sizeof (*call_site)
13611 + (sizeof (*call_site->parameter) * (nparams - 1))));
13612 *slot = call_site;
13613 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13614 call_site->pc = pc;
13615
13616 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13617 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13618 {
13619 struct die_info *func_die;
13620
13621 /* Skip also over DW_TAG_inlined_subroutine. */
13622 for (func_die = die->parent;
13623 func_die && func_die->tag != DW_TAG_subprogram
13624 && func_die->tag != DW_TAG_subroutine_type;
13625 func_die = func_die->parent);
13626
13627 /* DW_AT_call_all_calls is a superset
13628 of DW_AT_call_all_tail_calls. */
13629 if (func_die
13630 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13631 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13632 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13633 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13634 {
13635 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13636 not complete. But keep CALL_SITE for look ups via call_site_htab,
13637 both the initial caller containing the real return address PC and
13638 the final callee containing the current PC of a chain of tail
13639 calls do not need to have the tail call list complete. But any
13640 function candidate for a virtual tail call frame searched via
13641 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13642 determined unambiguously. */
13643 }
13644 else
13645 {
13646 struct type *func_type = NULL;
13647
13648 if (func_die)
13649 func_type = get_die_type (func_die, cu);
13650 if (func_type != NULL)
13651 {
13652 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13653
13654 /* Enlist this call site to the function. */
13655 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13656 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13657 }
13658 else
13659 complaint (_("Cannot find function owning DW_TAG_call_site "
13660 "DIE %s [in module %s]"),
13661 sect_offset_str (die->sect_off), objfile_name (objfile));
13662 }
13663 }
13664
13665 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13666 if (attr == NULL)
13667 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13668 if (attr == NULL)
13669 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13670 if (attr == NULL)
13671 {
13672 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13673 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13674 }
13675 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13676 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13677 /* Keep NULL DWARF_BLOCK. */;
13678 else if (attr->form_is_block ())
13679 {
13680 struct dwarf2_locexpr_baton *dlbaton;
13681
13682 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13683 dlbaton->data = DW_BLOCK (attr)->data;
13684 dlbaton->size = DW_BLOCK (attr)->size;
13685 dlbaton->per_cu = cu->per_cu;
13686
13687 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13688 }
13689 else if (attr->form_is_ref ())
13690 {
13691 struct dwarf2_cu *target_cu = cu;
13692 struct die_info *target_die;
13693
13694 target_die = follow_die_ref (die, attr, &target_cu);
13695 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13696 if (die_is_declaration (target_die, target_cu))
13697 {
13698 const char *target_physname;
13699
13700 /* Prefer the mangled name; otherwise compute the demangled one. */
13701 target_physname = dw2_linkage_name (target_die, target_cu);
13702 if (target_physname == NULL)
13703 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13704 if (target_physname == NULL)
13705 complaint (_("DW_AT_call_target target DIE has invalid "
13706 "physname, for referencing DIE %s [in module %s]"),
13707 sect_offset_str (die->sect_off), objfile_name (objfile));
13708 else
13709 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13710 }
13711 else
13712 {
13713 CORE_ADDR lowpc;
13714
13715 /* DW_AT_entry_pc should be preferred. */
13716 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13717 <= PC_BOUNDS_INVALID)
13718 complaint (_("DW_AT_call_target target DIE has invalid "
13719 "low pc, for referencing DIE %s [in module %s]"),
13720 sect_offset_str (die->sect_off), objfile_name (objfile));
13721 else
13722 {
13723 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13724 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13725 }
13726 }
13727 }
13728 else
13729 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13730 "block nor reference, for DIE %s [in module %s]"),
13731 sect_offset_str (die->sect_off), objfile_name (objfile));
13732
13733 call_site->per_cu = cu->per_cu;
13734
13735 for (child_die = die->child;
13736 child_die && child_die->tag;
13737 child_die = sibling_die (child_die))
13738 {
13739 struct call_site_parameter *parameter;
13740 struct attribute *loc, *origin;
13741
13742 if (child_die->tag != DW_TAG_call_site_parameter
13743 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13744 {
13745 /* Already printed the complaint above. */
13746 continue;
13747 }
13748
13749 gdb_assert (call_site->parameter_count < nparams);
13750 parameter = &call_site->parameter[call_site->parameter_count];
13751
13752 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13753 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13754 register is contained in DW_AT_call_value. */
13755
13756 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13757 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13758 if (origin == NULL)
13759 {
13760 /* This was a pre-DWARF-5 GNU extension alias
13761 for DW_AT_call_parameter. */
13762 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13763 }
13764 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13765 {
13766 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13767
13768 sect_offset sect_off
13769 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13770 if (!offset_in_cu_p (&cu->header, sect_off))
13771 {
13772 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13773 binding can be done only inside one CU. Such referenced DIE
13774 therefore cannot be even moved to DW_TAG_partial_unit. */
13775 complaint (_("DW_AT_call_parameter offset is not in CU for "
13776 "DW_TAG_call_site child DIE %s [in module %s]"),
13777 sect_offset_str (child_die->sect_off),
13778 objfile_name (objfile));
13779 continue;
13780 }
13781 parameter->u.param_cu_off
13782 = (cu_offset) (sect_off - cu->header.sect_off);
13783 }
13784 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13785 {
13786 complaint (_("No DW_FORM_block* DW_AT_location for "
13787 "DW_TAG_call_site child DIE %s [in module %s]"),
13788 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13789 continue;
13790 }
13791 else
13792 {
13793 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13794 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13795 if (parameter->u.dwarf_reg != -1)
13796 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13797 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13798 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13799 &parameter->u.fb_offset))
13800 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13801 else
13802 {
13803 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13804 "for DW_FORM_block* DW_AT_location is supported for "
13805 "DW_TAG_call_site child DIE %s "
13806 "[in module %s]"),
13807 sect_offset_str (child_die->sect_off),
13808 objfile_name (objfile));
13809 continue;
13810 }
13811 }
13812
13813 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13814 if (attr == NULL)
13815 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13816 if (attr == NULL || !attr->form_is_block ())
13817 {
13818 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13819 "DW_TAG_call_site child DIE %s [in module %s]"),
13820 sect_offset_str (child_die->sect_off),
13821 objfile_name (objfile));
13822 continue;
13823 }
13824 parameter->value = DW_BLOCK (attr)->data;
13825 parameter->value_size = DW_BLOCK (attr)->size;
13826
13827 /* Parameters are not pre-cleared by memset above. */
13828 parameter->data_value = NULL;
13829 parameter->data_value_size = 0;
13830 call_site->parameter_count++;
13831
13832 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13833 if (attr == NULL)
13834 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13835 if (attr != nullptr)
13836 {
13837 if (!attr->form_is_block ())
13838 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13839 "DW_TAG_call_site child DIE %s [in module %s]"),
13840 sect_offset_str (child_die->sect_off),
13841 objfile_name (objfile));
13842 else
13843 {
13844 parameter->data_value = DW_BLOCK (attr)->data;
13845 parameter->data_value_size = DW_BLOCK (attr)->size;
13846 }
13847 }
13848 }
13849 }
13850
13851 /* Helper function for read_variable. If DIE represents a virtual
13852 table, then return the type of the concrete object that is
13853 associated with the virtual table. Otherwise, return NULL. */
13854
13855 static struct type *
13856 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13857 {
13858 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13859 if (attr == NULL)
13860 return NULL;
13861
13862 /* Find the type DIE. */
13863 struct die_info *type_die = NULL;
13864 struct dwarf2_cu *type_cu = cu;
13865
13866 if (attr->form_is_ref ())
13867 type_die = follow_die_ref (die, attr, &type_cu);
13868 if (type_die == NULL)
13869 return NULL;
13870
13871 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13872 return NULL;
13873 return die_containing_type (type_die, type_cu);
13874 }
13875
13876 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13877
13878 static void
13879 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13880 {
13881 struct rust_vtable_symbol *storage = NULL;
13882
13883 if (cu->language == language_rust)
13884 {
13885 struct type *containing_type = rust_containing_type (die, cu);
13886
13887 if (containing_type != NULL)
13888 {
13889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13890
13891 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13892 initialize_objfile_symbol (storage);
13893 storage->concrete_type = containing_type;
13894 storage->subclass = SYMBOL_RUST_VTABLE;
13895 }
13896 }
13897
13898 struct symbol *res = new_symbol (die, NULL, cu, storage);
13899 struct attribute *abstract_origin
13900 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13901 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13902 if (res == NULL && loc && abstract_origin)
13903 {
13904 /* We have a variable without a name, but with a location and an abstract
13905 origin. This may be a concrete instance of an abstract variable
13906 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13907 later. */
13908 struct dwarf2_cu *origin_cu = cu;
13909 struct die_info *origin_die
13910 = follow_die_ref (die, abstract_origin, &origin_cu);
13911 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13912 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13913 }
13914 }
13915
13916 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13917 reading .debug_rnglists.
13918 Callback's type should be:
13919 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13920 Return true if the attributes are present and valid, otherwise,
13921 return false. */
13922
13923 template <typename Callback>
13924 static bool
13925 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13926 Callback &&callback)
13927 {
13928 struct dwarf2_per_objfile *dwarf2_per_objfile
13929 = cu->per_cu->dwarf2_per_objfile;
13930 struct objfile *objfile = dwarf2_per_objfile->objfile;
13931 bfd *obfd = objfile->obfd;
13932 /* Base address selection entry. */
13933 CORE_ADDR base;
13934 int found_base;
13935 const gdb_byte *buffer;
13936 CORE_ADDR baseaddr;
13937 bool overflow = false;
13938
13939 found_base = cu->base_known;
13940 base = cu->base_address;
13941
13942 dwarf2_per_objfile->rnglists.read (objfile);
13943 if (offset >= dwarf2_per_objfile->rnglists.size)
13944 {
13945 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13946 offset);
13947 return false;
13948 }
13949 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13950
13951 baseaddr = objfile->text_section_offset ();
13952
13953 while (1)
13954 {
13955 /* Initialize it due to a false compiler warning. */
13956 CORE_ADDR range_beginning = 0, range_end = 0;
13957 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13958 + dwarf2_per_objfile->rnglists.size);
13959 unsigned int bytes_read;
13960
13961 if (buffer == buf_end)
13962 {
13963 overflow = true;
13964 break;
13965 }
13966 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13967 switch (rlet)
13968 {
13969 case DW_RLE_end_of_list:
13970 break;
13971 case DW_RLE_base_address:
13972 if (buffer + cu->header.addr_size > buf_end)
13973 {
13974 overflow = true;
13975 break;
13976 }
13977 base = read_address (obfd, buffer, cu, &bytes_read);
13978 found_base = 1;
13979 buffer += bytes_read;
13980 break;
13981 case DW_RLE_start_length:
13982 if (buffer + cu->header.addr_size > buf_end)
13983 {
13984 overflow = true;
13985 break;
13986 }
13987 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13988 buffer += bytes_read;
13989 range_end = (range_beginning
13990 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13991 buffer += bytes_read;
13992 if (buffer > buf_end)
13993 {
13994 overflow = true;
13995 break;
13996 }
13997 break;
13998 case DW_RLE_offset_pair:
13999 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14000 buffer += bytes_read;
14001 if (buffer > buf_end)
14002 {
14003 overflow = true;
14004 break;
14005 }
14006 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14007 buffer += bytes_read;
14008 if (buffer > buf_end)
14009 {
14010 overflow = true;
14011 break;
14012 }
14013 break;
14014 case DW_RLE_start_end:
14015 if (buffer + 2 * cu->header.addr_size > buf_end)
14016 {
14017 overflow = true;
14018 break;
14019 }
14020 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14021 buffer += bytes_read;
14022 range_end = read_address (obfd, buffer, cu, &bytes_read);
14023 buffer += bytes_read;
14024 break;
14025 default:
14026 complaint (_("Invalid .debug_rnglists data (no base address)"));
14027 return false;
14028 }
14029 if (rlet == DW_RLE_end_of_list || overflow)
14030 break;
14031 if (rlet == DW_RLE_base_address)
14032 continue;
14033
14034 if (!found_base)
14035 {
14036 /* We have no valid base address for the ranges
14037 data. */
14038 complaint (_("Invalid .debug_rnglists data (no base address)"));
14039 return false;
14040 }
14041
14042 if (range_beginning > range_end)
14043 {
14044 /* Inverted range entries are invalid. */
14045 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14046 return false;
14047 }
14048
14049 /* Empty range entries have no effect. */
14050 if (range_beginning == range_end)
14051 continue;
14052
14053 range_beginning += base;
14054 range_end += base;
14055
14056 /* A not-uncommon case of bad debug info.
14057 Don't pollute the addrmap with bad data. */
14058 if (range_beginning + baseaddr == 0
14059 && !dwarf2_per_objfile->has_section_at_zero)
14060 {
14061 complaint (_(".debug_rnglists entry has start address of zero"
14062 " [in module %s]"), objfile_name (objfile));
14063 continue;
14064 }
14065
14066 callback (range_beginning, range_end);
14067 }
14068
14069 if (overflow)
14070 {
14071 complaint (_("Offset %d is not terminated "
14072 "for DW_AT_ranges attribute"),
14073 offset);
14074 return false;
14075 }
14076
14077 return true;
14078 }
14079
14080 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14081 Callback's type should be:
14082 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14083 Return 1 if the attributes are present and valid, otherwise, return 0. */
14084
14085 template <typename Callback>
14086 static int
14087 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14088 Callback &&callback)
14089 {
14090 struct dwarf2_per_objfile *dwarf2_per_objfile
14091 = cu->per_cu->dwarf2_per_objfile;
14092 struct objfile *objfile = dwarf2_per_objfile->objfile;
14093 struct comp_unit_head *cu_header = &cu->header;
14094 bfd *obfd = objfile->obfd;
14095 unsigned int addr_size = cu_header->addr_size;
14096 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14097 /* Base address selection entry. */
14098 CORE_ADDR base;
14099 int found_base;
14100 unsigned int dummy;
14101 const gdb_byte *buffer;
14102 CORE_ADDR baseaddr;
14103
14104 if (cu_header->version >= 5)
14105 return dwarf2_rnglists_process (offset, cu, callback);
14106
14107 found_base = cu->base_known;
14108 base = cu->base_address;
14109
14110 dwarf2_per_objfile->ranges.read (objfile);
14111 if (offset >= dwarf2_per_objfile->ranges.size)
14112 {
14113 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14114 offset);
14115 return 0;
14116 }
14117 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14118
14119 baseaddr = objfile->text_section_offset ();
14120
14121 while (1)
14122 {
14123 CORE_ADDR range_beginning, range_end;
14124
14125 range_beginning = read_address (obfd, buffer, cu, &dummy);
14126 buffer += addr_size;
14127 range_end = read_address (obfd, buffer, cu, &dummy);
14128 buffer += addr_size;
14129 offset += 2 * addr_size;
14130
14131 /* An end of list marker is a pair of zero addresses. */
14132 if (range_beginning == 0 && range_end == 0)
14133 /* Found the end of list entry. */
14134 break;
14135
14136 /* Each base address selection entry is a pair of 2 values.
14137 The first is the largest possible address, the second is
14138 the base address. Check for a base address here. */
14139 if ((range_beginning & mask) == mask)
14140 {
14141 /* If we found the largest possible address, then we already
14142 have the base address in range_end. */
14143 base = range_end;
14144 found_base = 1;
14145 continue;
14146 }
14147
14148 if (!found_base)
14149 {
14150 /* We have no valid base address for the ranges
14151 data. */
14152 complaint (_("Invalid .debug_ranges data (no base address)"));
14153 return 0;
14154 }
14155
14156 if (range_beginning > range_end)
14157 {
14158 /* Inverted range entries are invalid. */
14159 complaint (_("Invalid .debug_ranges data (inverted range)"));
14160 return 0;
14161 }
14162
14163 /* Empty range entries have no effect. */
14164 if (range_beginning == range_end)
14165 continue;
14166
14167 range_beginning += base;
14168 range_end += base;
14169
14170 /* A not-uncommon case of bad debug info.
14171 Don't pollute the addrmap with bad data. */
14172 if (range_beginning + baseaddr == 0
14173 && !dwarf2_per_objfile->has_section_at_zero)
14174 {
14175 complaint (_(".debug_ranges entry has start address of zero"
14176 " [in module %s]"), objfile_name (objfile));
14177 continue;
14178 }
14179
14180 callback (range_beginning, range_end);
14181 }
14182
14183 return 1;
14184 }
14185
14186 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14187 Return 1 if the attributes are present and valid, otherwise, return 0.
14188 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14189
14190 static int
14191 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14192 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14193 dwarf2_psymtab *ranges_pst)
14194 {
14195 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14196 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14197 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14198 int low_set = 0;
14199 CORE_ADDR low = 0;
14200 CORE_ADDR high = 0;
14201 int retval;
14202
14203 retval = dwarf2_ranges_process (offset, cu,
14204 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14205 {
14206 if (ranges_pst != NULL)
14207 {
14208 CORE_ADDR lowpc;
14209 CORE_ADDR highpc;
14210
14211 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14212 range_beginning + baseaddr)
14213 - baseaddr);
14214 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14215 range_end + baseaddr)
14216 - baseaddr);
14217 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14218 lowpc, highpc - 1, ranges_pst);
14219 }
14220
14221 /* FIXME: This is recording everything as a low-high
14222 segment of consecutive addresses. We should have a
14223 data structure for discontiguous block ranges
14224 instead. */
14225 if (! low_set)
14226 {
14227 low = range_beginning;
14228 high = range_end;
14229 low_set = 1;
14230 }
14231 else
14232 {
14233 if (range_beginning < low)
14234 low = range_beginning;
14235 if (range_end > high)
14236 high = range_end;
14237 }
14238 });
14239 if (!retval)
14240 return 0;
14241
14242 if (! low_set)
14243 /* If the first entry is an end-of-list marker, the range
14244 describes an empty scope, i.e. no instructions. */
14245 return 0;
14246
14247 if (low_return)
14248 *low_return = low;
14249 if (high_return)
14250 *high_return = high;
14251 return 1;
14252 }
14253
14254 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14255 definition for the return value. *LOWPC and *HIGHPC are set iff
14256 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14257
14258 static enum pc_bounds_kind
14259 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14260 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14261 dwarf2_psymtab *pst)
14262 {
14263 struct dwarf2_per_objfile *dwarf2_per_objfile
14264 = cu->per_cu->dwarf2_per_objfile;
14265 struct attribute *attr;
14266 struct attribute *attr_high;
14267 CORE_ADDR low = 0;
14268 CORE_ADDR high = 0;
14269 enum pc_bounds_kind ret;
14270
14271 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14272 if (attr_high)
14273 {
14274 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14275 if (attr != nullptr)
14276 {
14277 low = attr->value_as_address ();
14278 high = attr_high->value_as_address ();
14279 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14280 high += low;
14281 }
14282 else
14283 /* Found high w/o low attribute. */
14284 return PC_BOUNDS_INVALID;
14285
14286 /* Found consecutive range of addresses. */
14287 ret = PC_BOUNDS_HIGH_LOW;
14288 }
14289 else
14290 {
14291 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14292 if (attr != NULL)
14293 {
14294 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14295 We take advantage of the fact that DW_AT_ranges does not appear
14296 in DW_TAG_compile_unit of DWO files. */
14297 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14298 unsigned int ranges_offset = (DW_UNSND (attr)
14299 + (need_ranges_base
14300 ? cu->ranges_base
14301 : 0));
14302
14303 /* Value of the DW_AT_ranges attribute is the offset in the
14304 .debug_ranges section. */
14305 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14306 return PC_BOUNDS_INVALID;
14307 /* Found discontinuous range of addresses. */
14308 ret = PC_BOUNDS_RANGES;
14309 }
14310 else
14311 return PC_BOUNDS_NOT_PRESENT;
14312 }
14313
14314 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14315 if (high <= low)
14316 return PC_BOUNDS_INVALID;
14317
14318 /* When using the GNU linker, .gnu.linkonce. sections are used to
14319 eliminate duplicate copies of functions and vtables and such.
14320 The linker will arbitrarily choose one and discard the others.
14321 The AT_*_pc values for such functions refer to local labels in
14322 these sections. If the section from that file was discarded, the
14323 labels are not in the output, so the relocs get a value of 0.
14324 If this is a discarded function, mark the pc bounds as invalid,
14325 so that GDB will ignore it. */
14326 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14327 return PC_BOUNDS_INVALID;
14328
14329 *lowpc = low;
14330 if (highpc)
14331 *highpc = high;
14332 return ret;
14333 }
14334
14335 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14336 its low and high PC addresses. Do nothing if these addresses could not
14337 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14338 and HIGHPC to the high address if greater than HIGHPC. */
14339
14340 static void
14341 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14342 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14343 struct dwarf2_cu *cu)
14344 {
14345 CORE_ADDR low, high;
14346 struct die_info *child = die->child;
14347
14348 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14349 {
14350 *lowpc = std::min (*lowpc, low);
14351 *highpc = std::max (*highpc, high);
14352 }
14353
14354 /* If the language does not allow nested subprograms (either inside
14355 subprograms or lexical blocks), we're done. */
14356 if (cu->language != language_ada)
14357 return;
14358
14359 /* Check all the children of the given DIE. If it contains nested
14360 subprograms, then check their pc bounds. Likewise, we need to
14361 check lexical blocks as well, as they may also contain subprogram
14362 definitions. */
14363 while (child && child->tag)
14364 {
14365 if (child->tag == DW_TAG_subprogram
14366 || child->tag == DW_TAG_lexical_block)
14367 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14368 child = sibling_die (child);
14369 }
14370 }
14371
14372 /* Get the low and high pc's represented by the scope DIE, and store
14373 them in *LOWPC and *HIGHPC. If the correct values can't be
14374 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14375
14376 static void
14377 get_scope_pc_bounds (struct die_info *die,
14378 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14379 struct dwarf2_cu *cu)
14380 {
14381 CORE_ADDR best_low = (CORE_ADDR) -1;
14382 CORE_ADDR best_high = (CORE_ADDR) 0;
14383 CORE_ADDR current_low, current_high;
14384
14385 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14386 >= PC_BOUNDS_RANGES)
14387 {
14388 best_low = current_low;
14389 best_high = current_high;
14390 }
14391 else
14392 {
14393 struct die_info *child = die->child;
14394
14395 while (child && child->tag)
14396 {
14397 switch (child->tag) {
14398 case DW_TAG_subprogram:
14399 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14400 break;
14401 case DW_TAG_namespace:
14402 case DW_TAG_module:
14403 /* FIXME: carlton/2004-01-16: Should we do this for
14404 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14405 that current GCC's always emit the DIEs corresponding
14406 to definitions of methods of classes as children of a
14407 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14408 the DIEs giving the declarations, which could be
14409 anywhere). But I don't see any reason why the
14410 standards says that they have to be there. */
14411 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14412
14413 if (current_low != ((CORE_ADDR) -1))
14414 {
14415 best_low = std::min (best_low, current_low);
14416 best_high = std::max (best_high, current_high);
14417 }
14418 break;
14419 default:
14420 /* Ignore. */
14421 break;
14422 }
14423
14424 child = sibling_die (child);
14425 }
14426 }
14427
14428 *lowpc = best_low;
14429 *highpc = best_high;
14430 }
14431
14432 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14433 in DIE. */
14434
14435 static void
14436 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14437 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14438 {
14439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14441 struct attribute *attr;
14442 struct attribute *attr_high;
14443
14444 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14445 if (attr_high)
14446 {
14447 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14448 if (attr != nullptr)
14449 {
14450 CORE_ADDR low = attr->value_as_address ();
14451 CORE_ADDR high = attr_high->value_as_address ();
14452
14453 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14454 high += low;
14455
14456 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14457 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14458 cu->get_builder ()->record_block_range (block, low, high - 1);
14459 }
14460 }
14461
14462 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14463 if (attr != nullptr)
14464 {
14465 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14466 We take advantage of the fact that DW_AT_ranges does not appear
14467 in DW_TAG_compile_unit of DWO files. */
14468 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14469
14470 /* The value of the DW_AT_ranges attribute is the offset of the
14471 address range list in the .debug_ranges section. */
14472 unsigned long offset = (DW_UNSND (attr)
14473 + (need_ranges_base ? cu->ranges_base : 0));
14474
14475 std::vector<blockrange> blockvec;
14476 dwarf2_ranges_process (offset, cu,
14477 [&] (CORE_ADDR start, CORE_ADDR end)
14478 {
14479 start += baseaddr;
14480 end += baseaddr;
14481 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14482 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14483 cu->get_builder ()->record_block_range (block, start, end - 1);
14484 blockvec.emplace_back (start, end);
14485 });
14486
14487 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14488 }
14489 }
14490
14491 /* Check whether the producer field indicates either of GCC < 4.6, or the
14492 Intel C/C++ compiler, and cache the result in CU. */
14493
14494 static void
14495 check_producer (struct dwarf2_cu *cu)
14496 {
14497 int major, minor;
14498
14499 if (cu->producer == NULL)
14500 {
14501 /* For unknown compilers expect their behavior is DWARF version
14502 compliant.
14503
14504 GCC started to support .debug_types sections by -gdwarf-4 since
14505 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14506 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14507 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14508 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14509 }
14510 else if (producer_is_gcc (cu->producer, &major, &minor))
14511 {
14512 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14513 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14514 }
14515 else if (producer_is_icc (cu->producer, &major, &minor))
14516 {
14517 cu->producer_is_icc = true;
14518 cu->producer_is_icc_lt_14 = major < 14;
14519 }
14520 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14521 cu->producer_is_codewarrior = true;
14522 else
14523 {
14524 /* For other non-GCC compilers, expect their behavior is DWARF version
14525 compliant. */
14526 }
14527
14528 cu->checked_producer = true;
14529 }
14530
14531 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14532 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14533 during 4.6.0 experimental. */
14534
14535 static bool
14536 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14537 {
14538 if (!cu->checked_producer)
14539 check_producer (cu);
14540
14541 return cu->producer_is_gxx_lt_4_6;
14542 }
14543
14544
14545 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14546 with incorrect is_stmt attributes. */
14547
14548 static bool
14549 producer_is_codewarrior (struct dwarf2_cu *cu)
14550 {
14551 if (!cu->checked_producer)
14552 check_producer (cu);
14553
14554 return cu->producer_is_codewarrior;
14555 }
14556
14557 /* Return the default accessibility type if it is not overridden by
14558 DW_AT_accessibility. */
14559
14560 static enum dwarf_access_attribute
14561 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14562 {
14563 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14564 {
14565 /* The default DWARF 2 accessibility for members is public, the default
14566 accessibility for inheritance is private. */
14567
14568 if (die->tag != DW_TAG_inheritance)
14569 return DW_ACCESS_public;
14570 else
14571 return DW_ACCESS_private;
14572 }
14573 else
14574 {
14575 /* DWARF 3+ defines the default accessibility a different way. The same
14576 rules apply now for DW_TAG_inheritance as for the members and it only
14577 depends on the container kind. */
14578
14579 if (die->parent->tag == DW_TAG_class_type)
14580 return DW_ACCESS_private;
14581 else
14582 return DW_ACCESS_public;
14583 }
14584 }
14585
14586 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14587 offset. If the attribute was not found return 0, otherwise return
14588 1. If it was found but could not properly be handled, set *OFFSET
14589 to 0. */
14590
14591 static int
14592 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14593 LONGEST *offset)
14594 {
14595 struct attribute *attr;
14596
14597 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14598 if (attr != NULL)
14599 {
14600 *offset = 0;
14601
14602 /* Note that we do not check for a section offset first here.
14603 This is because DW_AT_data_member_location is new in DWARF 4,
14604 so if we see it, we can assume that a constant form is really
14605 a constant and not a section offset. */
14606 if (attr->form_is_constant ())
14607 *offset = dwarf2_get_attr_constant_value (attr, 0);
14608 else if (attr->form_is_section_offset ())
14609 dwarf2_complex_location_expr_complaint ();
14610 else if (attr->form_is_block ())
14611 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14612 else
14613 dwarf2_complex_location_expr_complaint ();
14614
14615 return 1;
14616 }
14617
14618 return 0;
14619 }
14620
14621 /* Add an aggregate field to the field list. */
14622
14623 static void
14624 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14625 struct dwarf2_cu *cu)
14626 {
14627 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14628 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14629 struct nextfield *new_field;
14630 struct attribute *attr;
14631 struct field *fp;
14632 const char *fieldname = "";
14633
14634 if (die->tag == DW_TAG_inheritance)
14635 {
14636 fip->baseclasses.emplace_back ();
14637 new_field = &fip->baseclasses.back ();
14638 }
14639 else
14640 {
14641 fip->fields.emplace_back ();
14642 new_field = &fip->fields.back ();
14643 }
14644
14645 fip->nfields++;
14646
14647 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14648 if (attr != nullptr)
14649 new_field->accessibility = DW_UNSND (attr);
14650 else
14651 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14652 if (new_field->accessibility != DW_ACCESS_public)
14653 fip->non_public_fields = 1;
14654
14655 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14656 if (attr != nullptr)
14657 new_field->virtuality = DW_UNSND (attr);
14658 else
14659 new_field->virtuality = DW_VIRTUALITY_none;
14660
14661 fp = &new_field->field;
14662
14663 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14664 {
14665 LONGEST offset;
14666
14667 /* Data member other than a C++ static data member. */
14668
14669 /* Get type of field. */
14670 fp->type = die_type (die, cu);
14671
14672 SET_FIELD_BITPOS (*fp, 0);
14673
14674 /* Get bit size of field (zero if none). */
14675 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14676 if (attr != nullptr)
14677 {
14678 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14679 }
14680 else
14681 {
14682 FIELD_BITSIZE (*fp) = 0;
14683 }
14684
14685 /* Get bit offset of field. */
14686 if (handle_data_member_location (die, cu, &offset))
14687 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14688 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14689 if (attr != nullptr)
14690 {
14691 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14692 {
14693 /* For big endian bits, the DW_AT_bit_offset gives the
14694 additional bit offset from the MSB of the containing
14695 anonymous object to the MSB of the field. We don't
14696 have to do anything special since we don't need to
14697 know the size of the anonymous object. */
14698 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14699 }
14700 else
14701 {
14702 /* For little endian bits, compute the bit offset to the
14703 MSB of the anonymous object, subtract off the number of
14704 bits from the MSB of the field to the MSB of the
14705 object, and then subtract off the number of bits of
14706 the field itself. The result is the bit offset of
14707 the LSB of the field. */
14708 int anonymous_size;
14709 int bit_offset = DW_UNSND (attr);
14710
14711 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14712 if (attr != nullptr)
14713 {
14714 /* The size of the anonymous object containing
14715 the bit field is explicit, so use the
14716 indicated size (in bytes). */
14717 anonymous_size = DW_UNSND (attr);
14718 }
14719 else
14720 {
14721 /* The size of the anonymous object containing
14722 the bit field must be inferred from the type
14723 attribute of the data member containing the
14724 bit field. */
14725 anonymous_size = TYPE_LENGTH (fp->type);
14726 }
14727 SET_FIELD_BITPOS (*fp,
14728 (FIELD_BITPOS (*fp)
14729 + anonymous_size * bits_per_byte
14730 - bit_offset - FIELD_BITSIZE (*fp)));
14731 }
14732 }
14733 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14734 if (attr != NULL)
14735 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14736 + dwarf2_get_attr_constant_value (attr, 0)));
14737
14738 /* Get name of field. */
14739 fieldname = dwarf2_name (die, cu);
14740 if (fieldname == NULL)
14741 fieldname = "";
14742
14743 /* The name is already allocated along with this objfile, so we don't
14744 need to duplicate it for the type. */
14745 fp->name = fieldname;
14746
14747 /* Change accessibility for artificial fields (e.g. virtual table
14748 pointer or virtual base class pointer) to private. */
14749 if (dwarf2_attr (die, DW_AT_artificial, cu))
14750 {
14751 FIELD_ARTIFICIAL (*fp) = 1;
14752 new_field->accessibility = DW_ACCESS_private;
14753 fip->non_public_fields = 1;
14754 }
14755 }
14756 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14757 {
14758 /* C++ static member. */
14759
14760 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14761 is a declaration, but all versions of G++ as of this writing
14762 (so through at least 3.2.1) incorrectly generate
14763 DW_TAG_variable tags. */
14764
14765 const char *physname;
14766
14767 /* Get name of field. */
14768 fieldname = dwarf2_name (die, cu);
14769 if (fieldname == NULL)
14770 return;
14771
14772 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14773 if (attr
14774 /* Only create a symbol if this is an external value.
14775 new_symbol checks this and puts the value in the global symbol
14776 table, which we want. If it is not external, new_symbol
14777 will try to put the value in cu->list_in_scope which is wrong. */
14778 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14779 {
14780 /* A static const member, not much different than an enum as far as
14781 we're concerned, except that we can support more types. */
14782 new_symbol (die, NULL, cu);
14783 }
14784
14785 /* Get physical name. */
14786 physname = dwarf2_physname (fieldname, die, cu);
14787
14788 /* The name is already allocated along with this objfile, so we don't
14789 need to duplicate it for the type. */
14790 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14791 FIELD_TYPE (*fp) = die_type (die, cu);
14792 FIELD_NAME (*fp) = fieldname;
14793 }
14794 else if (die->tag == DW_TAG_inheritance)
14795 {
14796 LONGEST offset;
14797
14798 /* C++ base class field. */
14799 if (handle_data_member_location (die, cu, &offset))
14800 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14801 FIELD_BITSIZE (*fp) = 0;
14802 FIELD_TYPE (*fp) = die_type (die, cu);
14803 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14804 }
14805 else if (die->tag == DW_TAG_variant_part)
14806 {
14807 /* process_structure_scope will treat this DIE as a union. */
14808 process_structure_scope (die, cu);
14809
14810 /* The variant part is relative to the start of the enclosing
14811 structure. */
14812 SET_FIELD_BITPOS (*fp, 0);
14813 fp->type = get_die_type (die, cu);
14814 fp->artificial = 1;
14815 fp->name = "<<variant>>";
14816
14817 /* Normally a DW_TAG_variant_part won't have a size, but our
14818 representation requires one, so set it to the maximum of the
14819 child sizes, being sure to account for the offset at which
14820 each child is seen. */
14821 if (TYPE_LENGTH (fp->type) == 0)
14822 {
14823 unsigned max = 0;
14824 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14825 {
14826 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14827 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14828 if (len > max)
14829 max = len;
14830 }
14831 TYPE_LENGTH (fp->type) = max;
14832 }
14833 }
14834 else
14835 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14836 }
14837
14838 /* Can the type given by DIE define another type? */
14839
14840 static bool
14841 type_can_define_types (const struct die_info *die)
14842 {
14843 switch (die->tag)
14844 {
14845 case DW_TAG_typedef:
14846 case DW_TAG_class_type:
14847 case DW_TAG_structure_type:
14848 case DW_TAG_union_type:
14849 case DW_TAG_enumeration_type:
14850 return true;
14851
14852 default:
14853 return false;
14854 }
14855 }
14856
14857 /* Add a type definition defined in the scope of the FIP's class. */
14858
14859 static void
14860 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14861 struct dwarf2_cu *cu)
14862 {
14863 struct decl_field fp;
14864 memset (&fp, 0, sizeof (fp));
14865
14866 gdb_assert (type_can_define_types (die));
14867
14868 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14869 fp.name = dwarf2_name (die, cu);
14870 fp.type = read_type_die (die, cu);
14871
14872 /* Save accessibility. */
14873 enum dwarf_access_attribute accessibility;
14874 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14875 if (attr != NULL)
14876 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14877 else
14878 accessibility = dwarf2_default_access_attribute (die, cu);
14879 switch (accessibility)
14880 {
14881 case DW_ACCESS_public:
14882 /* The assumed value if neither private nor protected. */
14883 break;
14884 case DW_ACCESS_private:
14885 fp.is_private = 1;
14886 break;
14887 case DW_ACCESS_protected:
14888 fp.is_protected = 1;
14889 break;
14890 default:
14891 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14892 }
14893
14894 if (die->tag == DW_TAG_typedef)
14895 fip->typedef_field_list.push_back (fp);
14896 else
14897 fip->nested_types_list.push_back (fp);
14898 }
14899
14900 /* Create the vector of fields, and attach it to the type. */
14901
14902 static void
14903 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14904 struct dwarf2_cu *cu)
14905 {
14906 int nfields = fip->nfields;
14907
14908 /* Record the field count, allocate space for the array of fields,
14909 and create blank accessibility bitfields if necessary. */
14910 TYPE_NFIELDS (type) = nfields;
14911 TYPE_FIELDS (type) = (struct field *)
14912 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14913
14914 if (fip->non_public_fields && cu->language != language_ada)
14915 {
14916 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14917
14918 TYPE_FIELD_PRIVATE_BITS (type) =
14919 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14920 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14921
14922 TYPE_FIELD_PROTECTED_BITS (type) =
14923 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14924 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14925
14926 TYPE_FIELD_IGNORE_BITS (type) =
14927 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14928 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14929 }
14930
14931 /* If the type has baseclasses, allocate and clear a bit vector for
14932 TYPE_FIELD_VIRTUAL_BITS. */
14933 if (!fip->baseclasses.empty () && cu->language != language_ada)
14934 {
14935 int num_bytes = B_BYTES (fip->baseclasses.size ());
14936 unsigned char *pointer;
14937
14938 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14939 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14940 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14941 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14942 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14943 }
14944
14945 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14946 {
14947 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14948
14949 for (int index = 0; index < nfields; ++index)
14950 {
14951 struct nextfield &field = fip->fields[index];
14952
14953 if (field.variant.is_discriminant)
14954 di->discriminant_index = index;
14955 else if (field.variant.default_branch)
14956 di->default_index = index;
14957 else
14958 di->discriminants[index] = field.variant.discriminant_value;
14959 }
14960 }
14961
14962 /* Copy the saved-up fields into the field vector. */
14963 for (int i = 0; i < nfields; ++i)
14964 {
14965 struct nextfield &field
14966 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14967 : fip->fields[i - fip->baseclasses.size ()]);
14968
14969 TYPE_FIELD (type, i) = field.field;
14970 switch (field.accessibility)
14971 {
14972 case DW_ACCESS_private:
14973 if (cu->language != language_ada)
14974 SET_TYPE_FIELD_PRIVATE (type, i);
14975 break;
14976
14977 case DW_ACCESS_protected:
14978 if (cu->language != language_ada)
14979 SET_TYPE_FIELD_PROTECTED (type, i);
14980 break;
14981
14982 case DW_ACCESS_public:
14983 break;
14984
14985 default:
14986 /* Unknown accessibility. Complain and treat it as public. */
14987 {
14988 complaint (_("unsupported accessibility %d"),
14989 field.accessibility);
14990 }
14991 break;
14992 }
14993 if (i < fip->baseclasses.size ())
14994 {
14995 switch (field.virtuality)
14996 {
14997 case DW_VIRTUALITY_virtual:
14998 case DW_VIRTUALITY_pure_virtual:
14999 if (cu->language == language_ada)
15000 error (_("unexpected virtuality in component of Ada type"));
15001 SET_TYPE_FIELD_VIRTUAL (type, i);
15002 break;
15003 }
15004 }
15005 }
15006 }
15007
15008 /* Return true if this member function is a constructor, false
15009 otherwise. */
15010
15011 static int
15012 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15013 {
15014 const char *fieldname;
15015 const char *type_name;
15016 int len;
15017
15018 if (die->parent == NULL)
15019 return 0;
15020
15021 if (die->parent->tag != DW_TAG_structure_type
15022 && die->parent->tag != DW_TAG_union_type
15023 && die->parent->tag != DW_TAG_class_type)
15024 return 0;
15025
15026 fieldname = dwarf2_name (die, cu);
15027 type_name = dwarf2_name (die->parent, cu);
15028 if (fieldname == NULL || type_name == NULL)
15029 return 0;
15030
15031 len = strlen (fieldname);
15032 return (strncmp (fieldname, type_name, len) == 0
15033 && (type_name[len] == '\0' || type_name[len] == '<'));
15034 }
15035
15036 /* Check if the given VALUE is a recognized enum
15037 dwarf_defaulted_attribute constant according to DWARF5 spec,
15038 Table 7.24. */
15039
15040 static bool
15041 is_valid_DW_AT_defaulted (ULONGEST value)
15042 {
15043 switch (value)
15044 {
15045 case DW_DEFAULTED_no:
15046 case DW_DEFAULTED_in_class:
15047 case DW_DEFAULTED_out_of_class:
15048 return true;
15049 }
15050
15051 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15052 return false;
15053 }
15054
15055 /* Add a member function to the proper fieldlist. */
15056
15057 static void
15058 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15059 struct type *type, struct dwarf2_cu *cu)
15060 {
15061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15062 struct attribute *attr;
15063 int i;
15064 struct fnfieldlist *flp = nullptr;
15065 struct fn_field *fnp;
15066 const char *fieldname;
15067 struct type *this_type;
15068 enum dwarf_access_attribute accessibility;
15069
15070 if (cu->language == language_ada)
15071 error (_("unexpected member function in Ada type"));
15072
15073 /* Get name of member function. */
15074 fieldname = dwarf2_name (die, cu);
15075 if (fieldname == NULL)
15076 return;
15077
15078 /* Look up member function name in fieldlist. */
15079 for (i = 0; i < fip->fnfieldlists.size (); i++)
15080 {
15081 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15082 {
15083 flp = &fip->fnfieldlists[i];
15084 break;
15085 }
15086 }
15087
15088 /* Create a new fnfieldlist if necessary. */
15089 if (flp == nullptr)
15090 {
15091 fip->fnfieldlists.emplace_back ();
15092 flp = &fip->fnfieldlists.back ();
15093 flp->name = fieldname;
15094 i = fip->fnfieldlists.size () - 1;
15095 }
15096
15097 /* Create a new member function field and add it to the vector of
15098 fnfieldlists. */
15099 flp->fnfields.emplace_back ();
15100 fnp = &flp->fnfields.back ();
15101
15102 /* Delay processing of the physname until later. */
15103 if (cu->language == language_cplus)
15104 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15105 die, cu);
15106 else
15107 {
15108 const char *physname = dwarf2_physname (fieldname, die, cu);
15109 fnp->physname = physname ? physname : "";
15110 }
15111
15112 fnp->type = alloc_type (objfile);
15113 this_type = read_type_die (die, cu);
15114 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15115 {
15116 int nparams = TYPE_NFIELDS (this_type);
15117
15118 /* TYPE is the domain of this method, and THIS_TYPE is the type
15119 of the method itself (TYPE_CODE_METHOD). */
15120 smash_to_method_type (fnp->type, type,
15121 TYPE_TARGET_TYPE (this_type),
15122 TYPE_FIELDS (this_type),
15123 TYPE_NFIELDS (this_type),
15124 TYPE_VARARGS (this_type));
15125
15126 /* Handle static member functions.
15127 Dwarf2 has no clean way to discern C++ static and non-static
15128 member functions. G++ helps GDB by marking the first
15129 parameter for non-static member functions (which is the this
15130 pointer) as artificial. We obtain this information from
15131 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15132 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15133 fnp->voffset = VOFFSET_STATIC;
15134 }
15135 else
15136 complaint (_("member function type missing for '%s'"),
15137 dwarf2_full_name (fieldname, die, cu));
15138
15139 /* Get fcontext from DW_AT_containing_type if present. */
15140 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15141 fnp->fcontext = die_containing_type (die, cu);
15142
15143 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15144 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15145
15146 /* Get accessibility. */
15147 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15148 if (attr != nullptr)
15149 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15150 else
15151 accessibility = dwarf2_default_access_attribute (die, cu);
15152 switch (accessibility)
15153 {
15154 case DW_ACCESS_private:
15155 fnp->is_private = 1;
15156 break;
15157 case DW_ACCESS_protected:
15158 fnp->is_protected = 1;
15159 break;
15160 }
15161
15162 /* Check for artificial methods. */
15163 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15164 if (attr && DW_UNSND (attr) != 0)
15165 fnp->is_artificial = 1;
15166
15167 /* Check for defaulted methods. */
15168 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15169 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15170 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15171
15172 /* Check for deleted methods. */
15173 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15174 if (attr != nullptr && DW_UNSND (attr) != 0)
15175 fnp->is_deleted = 1;
15176
15177 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15178
15179 /* Get index in virtual function table if it is a virtual member
15180 function. For older versions of GCC, this is an offset in the
15181 appropriate virtual table, as specified by DW_AT_containing_type.
15182 For everyone else, it is an expression to be evaluated relative
15183 to the object address. */
15184
15185 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15186 if (attr != nullptr)
15187 {
15188 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15189 {
15190 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15191 {
15192 /* Old-style GCC. */
15193 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15194 }
15195 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15196 || (DW_BLOCK (attr)->size > 1
15197 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15198 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15199 {
15200 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15201 if ((fnp->voffset % cu->header.addr_size) != 0)
15202 dwarf2_complex_location_expr_complaint ();
15203 else
15204 fnp->voffset /= cu->header.addr_size;
15205 fnp->voffset += 2;
15206 }
15207 else
15208 dwarf2_complex_location_expr_complaint ();
15209
15210 if (!fnp->fcontext)
15211 {
15212 /* If there is no `this' field and no DW_AT_containing_type,
15213 we cannot actually find a base class context for the
15214 vtable! */
15215 if (TYPE_NFIELDS (this_type) == 0
15216 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15217 {
15218 complaint (_("cannot determine context for virtual member "
15219 "function \"%s\" (offset %s)"),
15220 fieldname, sect_offset_str (die->sect_off));
15221 }
15222 else
15223 {
15224 fnp->fcontext
15225 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15226 }
15227 }
15228 }
15229 else if (attr->form_is_section_offset ())
15230 {
15231 dwarf2_complex_location_expr_complaint ();
15232 }
15233 else
15234 {
15235 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15236 fieldname);
15237 }
15238 }
15239 else
15240 {
15241 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15242 if (attr && DW_UNSND (attr))
15243 {
15244 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15245 complaint (_("Member function \"%s\" (offset %s) is virtual "
15246 "but the vtable offset is not specified"),
15247 fieldname, sect_offset_str (die->sect_off));
15248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15249 TYPE_CPLUS_DYNAMIC (type) = 1;
15250 }
15251 }
15252 }
15253
15254 /* Create the vector of member function fields, and attach it to the type. */
15255
15256 static void
15257 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15258 struct dwarf2_cu *cu)
15259 {
15260 if (cu->language == language_ada)
15261 error (_("unexpected member functions in Ada type"));
15262
15263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15264 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15265 TYPE_ALLOC (type,
15266 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15267
15268 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15269 {
15270 struct fnfieldlist &nf = fip->fnfieldlists[i];
15271 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15272
15273 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15274 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15275 fn_flp->fn_fields = (struct fn_field *)
15276 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15277
15278 for (int k = 0; k < nf.fnfields.size (); ++k)
15279 fn_flp->fn_fields[k] = nf.fnfields[k];
15280 }
15281
15282 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15283 }
15284
15285 /* Returns non-zero if NAME is the name of a vtable member in CU's
15286 language, zero otherwise. */
15287 static int
15288 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15289 {
15290 static const char vptr[] = "_vptr";
15291
15292 /* Look for the C++ form of the vtable. */
15293 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15294 return 1;
15295
15296 return 0;
15297 }
15298
15299 /* GCC outputs unnamed structures that are really pointers to member
15300 functions, with the ABI-specified layout. If TYPE describes
15301 such a structure, smash it into a member function type.
15302
15303 GCC shouldn't do this; it should just output pointer to member DIEs.
15304 This is GCC PR debug/28767. */
15305
15306 static void
15307 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15308 {
15309 struct type *pfn_type, *self_type, *new_type;
15310
15311 /* Check for a structure with no name and two children. */
15312 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15313 return;
15314
15315 /* Check for __pfn and __delta members. */
15316 if (TYPE_FIELD_NAME (type, 0) == NULL
15317 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15318 || TYPE_FIELD_NAME (type, 1) == NULL
15319 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15320 return;
15321
15322 /* Find the type of the method. */
15323 pfn_type = TYPE_FIELD_TYPE (type, 0);
15324 if (pfn_type == NULL
15325 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15326 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15327 return;
15328
15329 /* Look for the "this" argument. */
15330 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15331 if (TYPE_NFIELDS (pfn_type) == 0
15332 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15333 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15334 return;
15335
15336 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15337 new_type = alloc_type (objfile);
15338 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15339 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15340 TYPE_VARARGS (pfn_type));
15341 smash_to_methodptr_type (type, new_type);
15342 }
15343
15344 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15345 appropriate error checking and issuing complaints if there is a
15346 problem. */
15347
15348 static ULONGEST
15349 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15350 {
15351 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15352
15353 if (attr == nullptr)
15354 return 0;
15355
15356 if (!attr->form_is_constant ())
15357 {
15358 complaint (_("DW_AT_alignment must have constant form"
15359 " - DIE at %s [in module %s]"),
15360 sect_offset_str (die->sect_off),
15361 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15362 return 0;
15363 }
15364
15365 ULONGEST align;
15366 if (attr->form == DW_FORM_sdata)
15367 {
15368 LONGEST val = DW_SND (attr);
15369 if (val < 0)
15370 {
15371 complaint (_("DW_AT_alignment value must not be negative"
15372 " - DIE at %s [in module %s]"),
15373 sect_offset_str (die->sect_off),
15374 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15375 return 0;
15376 }
15377 align = val;
15378 }
15379 else
15380 align = DW_UNSND (attr);
15381
15382 if (align == 0)
15383 {
15384 complaint (_("DW_AT_alignment value must not be zero"
15385 " - DIE at %s [in module %s]"),
15386 sect_offset_str (die->sect_off),
15387 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15388 return 0;
15389 }
15390 if ((align & (align - 1)) != 0)
15391 {
15392 complaint (_("DW_AT_alignment value must be a power of 2"
15393 " - DIE at %s [in module %s]"),
15394 sect_offset_str (die->sect_off),
15395 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15396 return 0;
15397 }
15398
15399 return align;
15400 }
15401
15402 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15403 the alignment for TYPE. */
15404
15405 static void
15406 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15407 struct type *type)
15408 {
15409 if (!set_type_align (type, get_alignment (cu, die)))
15410 complaint (_("DW_AT_alignment value too large"
15411 " - DIE at %s [in module %s]"),
15412 sect_offset_str (die->sect_off),
15413 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15414 }
15415
15416 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15417 constant for a type, according to DWARF5 spec, Table 5.5. */
15418
15419 static bool
15420 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15421 {
15422 switch (value)
15423 {
15424 case DW_CC_normal:
15425 case DW_CC_pass_by_reference:
15426 case DW_CC_pass_by_value:
15427 return true;
15428
15429 default:
15430 complaint (_("unrecognized DW_AT_calling_convention value "
15431 "(%s) for a type"), pulongest (value));
15432 return false;
15433 }
15434 }
15435
15436 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15437 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15438 also according to GNU-specific values (see include/dwarf2.h). */
15439
15440 static bool
15441 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15442 {
15443 switch (value)
15444 {
15445 case DW_CC_normal:
15446 case DW_CC_program:
15447 case DW_CC_nocall:
15448 return true;
15449
15450 case DW_CC_GNU_renesas_sh:
15451 case DW_CC_GNU_borland_fastcall_i386:
15452 case DW_CC_GDB_IBM_OpenCL:
15453 return true;
15454
15455 default:
15456 complaint (_("unrecognized DW_AT_calling_convention value "
15457 "(%s) for a subroutine"), pulongest (value));
15458 return false;
15459 }
15460 }
15461
15462 /* Called when we find the DIE that starts a structure or union scope
15463 (definition) to create a type for the structure or union. Fill in
15464 the type's name and general properties; the members will not be
15465 processed until process_structure_scope. A symbol table entry for
15466 the type will also not be done until process_structure_scope (assuming
15467 the type has a name).
15468
15469 NOTE: we need to call these functions regardless of whether or not the
15470 DIE has a DW_AT_name attribute, since it might be an anonymous
15471 structure or union. This gets the type entered into our set of
15472 user defined types. */
15473
15474 static struct type *
15475 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15476 {
15477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15478 struct type *type;
15479 struct attribute *attr;
15480 const char *name;
15481
15482 /* If the definition of this type lives in .debug_types, read that type.
15483 Don't follow DW_AT_specification though, that will take us back up
15484 the chain and we want to go down. */
15485 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15486 if (attr != nullptr)
15487 {
15488 type = get_DW_AT_signature_type (die, attr, cu);
15489
15490 /* The type's CU may not be the same as CU.
15491 Ensure TYPE is recorded with CU in die_type_hash. */
15492 return set_die_type (die, type, cu);
15493 }
15494
15495 type = alloc_type (objfile);
15496 INIT_CPLUS_SPECIFIC (type);
15497
15498 name = dwarf2_name (die, cu);
15499 if (name != NULL)
15500 {
15501 if (cu->language == language_cplus
15502 || cu->language == language_d
15503 || cu->language == language_rust)
15504 {
15505 const char *full_name = dwarf2_full_name (name, die, cu);
15506
15507 /* dwarf2_full_name might have already finished building the DIE's
15508 type. If so, there is no need to continue. */
15509 if (get_die_type (die, cu) != NULL)
15510 return get_die_type (die, cu);
15511
15512 TYPE_NAME (type) = full_name;
15513 }
15514 else
15515 {
15516 /* The name is already allocated along with this objfile, so
15517 we don't need to duplicate it for the type. */
15518 TYPE_NAME (type) = name;
15519 }
15520 }
15521
15522 if (die->tag == DW_TAG_structure_type)
15523 {
15524 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15525 }
15526 else if (die->tag == DW_TAG_union_type)
15527 {
15528 TYPE_CODE (type) = TYPE_CODE_UNION;
15529 }
15530 else if (die->tag == DW_TAG_variant_part)
15531 {
15532 TYPE_CODE (type) = TYPE_CODE_UNION;
15533 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15534 }
15535 else
15536 {
15537 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15538 }
15539
15540 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15541 TYPE_DECLARED_CLASS (type) = 1;
15542
15543 /* Store the calling convention in the type if it's available in
15544 the die. Otherwise the calling convention remains set to
15545 the default value DW_CC_normal. */
15546 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15547 if (attr != nullptr
15548 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15549 {
15550 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15551 TYPE_CPLUS_CALLING_CONVENTION (type)
15552 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15553 }
15554
15555 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15556 if (attr != nullptr)
15557 {
15558 if (attr->form_is_constant ())
15559 TYPE_LENGTH (type) = DW_UNSND (attr);
15560 else
15561 {
15562 /* For the moment, dynamic type sizes are not supported
15563 by GDB's struct type. The actual size is determined
15564 on-demand when resolving the type of a given object,
15565 so set the type's length to zero for now. Otherwise,
15566 we record an expression as the length, and that expression
15567 could lead to a very large value, which could eventually
15568 lead to us trying to allocate that much memory when creating
15569 a value of that type. */
15570 TYPE_LENGTH (type) = 0;
15571 }
15572 }
15573 else
15574 {
15575 TYPE_LENGTH (type) = 0;
15576 }
15577
15578 maybe_set_alignment (cu, die, type);
15579
15580 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15581 {
15582 /* ICC<14 does not output the required DW_AT_declaration on
15583 incomplete types, but gives them a size of zero. */
15584 TYPE_STUB (type) = 1;
15585 }
15586 else
15587 TYPE_STUB_SUPPORTED (type) = 1;
15588
15589 if (die_is_declaration (die, cu))
15590 TYPE_STUB (type) = 1;
15591 else if (attr == NULL && die->child == NULL
15592 && producer_is_realview (cu->producer))
15593 /* RealView does not output the required DW_AT_declaration
15594 on incomplete types. */
15595 TYPE_STUB (type) = 1;
15596
15597 /* We need to add the type field to the die immediately so we don't
15598 infinitely recurse when dealing with pointers to the structure
15599 type within the structure itself. */
15600 set_die_type (die, type, cu);
15601
15602 /* set_die_type should be already done. */
15603 set_descriptive_type (type, die, cu);
15604
15605 return type;
15606 }
15607
15608 /* A helper for process_structure_scope that handles a single member
15609 DIE. */
15610
15611 static void
15612 handle_struct_member_die (struct die_info *child_die, struct type *type,
15613 struct field_info *fi,
15614 std::vector<struct symbol *> *template_args,
15615 struct dwarf2_cu *cu)
15616 {
15617 if (child_die->tag == DW_TAG_member
15618 || child_die->tag == DW_TAG_variable
15619 || child_die->tag == DW_TAG_variant_part)
15620 {
15621 /* NOTE: carlton/2002-11-05: A C++ static data member
15622 should be a DW_TAG_member that is a declaration, but
15623 all versions of G++ as of this writing (so through at
15624 least 3.2.1) incorrectly generate DW_TAG_variable
15625 tags for them instead. */
15626 dwarf2_add_field (fi, child_die, cu);
15627 }
15628 else if (child_die->tag == DW_TAG_subprogram)
15629 {
15630 /* Rust doesn't have member functions in the C++ sense.
15631 However, it does emit ordinary functions as children
15632 of a struct DIE. */
15633 if (cu->language == language_rust)
15634 read_func_scope (child_die, cu);
15635 else
15636 {
15637 /* C++ member function. */
15638 dwarf2_add_member_fn (fi, child_die, type, cu);
15639 }
15640 }
15641 else if (child_die->tag == DW_TAG_inheritance)
15642 {
15643 /* C++ base class field. */
15644 dwarf2_add_field (fi, child_die, cu);
15645 }
15646 else if (type_can_define_types (child_die))
15647 dwarf2_add_type_defn (fi, child_die, cu);
15648 else if (child_die->tag == DW_TAG_template_type_param
15649 || child_die->tag == DW_TAG_template_value_param)
15650 {
15651 struct symbol *arg = new_symbol (child_die, NULL, cu);
15652
15653 if (arg != NULL)
15654 template_args->push_back (arg);
15655 }
15656 else if (child_die->tag == DW_TAG_variant)
15657 {
15658 /* In a variant we want to get the discriminant and also add a
15659 field for our sole member child. */
15660 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15661
15662 for (die_info *variant_child = child_die->child;
15663 variant_child != NULL;
15664 variant_child = sibling_die (variant_child))
15665 {
15666 if (variant_child->tag == DW_TAG_member)
15667 {
15668 handle_struct_member_die (variant_child, type, fi,
15669 template_args, cu);
15670 /* Only handle the one. */
15671 break;
15672 }
15673 }
15674
15675 /* We don't handle this but we might as well report it if we see
15676 it. */
15677 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15678 complaint (_("DW_AT_discr_list is not supported yet"
15679 " - DIE at %s [in module %s]"),
15680 sect_offset_str (child_die->sect_off),
15681 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15682
15683 /* The first field was just added, so we can stash the
15684 discriminant there. */
15685 gdb_assert (!fi->fields.empty ());
15686 if (discr == NULL)
15687 fi->fields.back ().variant.default_branch = true;
15688 else
15689 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15690 }
15691 }
15692
15693 /* Finish creating a structure or union type, including filling in
15694 its members and creating a symbol for it. */
15695
15696 static void
15697 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15698 {
15699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15700 struct die_info *child_die;
15701 struct type *type;
15702
15703 type = get_die_type (die, cu);
15704 if (type == NULL)
15705 type = read_structure_type (die, cu);
15706
15707 /* When reading a DW_TAG_variant_part, we need to notice when we
15708 read the discriminant member, so we can record it later in the
15709 discriminant_info. */
15710 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15711 sect_offset discr_offset {};
15712 bool has_template_parameters = false;
15713
15714 if (is_variant_part)
15715 {
15716 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15717 if (discr == NULL)
15718 {
15719 /* Maybe it's a univariant form, an extension we support.
15720 In this case arrange not to check the offset. */
15721 is_variant_part = false;
15722 }
15723 else if (discr->form_is_ref ())
15724 {
15725 struct dwarf2_cu *target_cu = cu;
15726 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15727
15728 discr_offset = target_die->sect_off;
15729 }
15730 else
15731 {
15732 complaint (_("DW_AT_discr does not have DIE reference form"
15733 " - DIE at %s [in module %s]"),
15734 sect_offset_str (die->sect_off),
15735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15736 is_variant_part = false;
15737 }
15738 }
15739
15740 if (die->child != NULL && ! die_is_declaration (die, cu))
15741 {
15742 struct field_info fi;
15743 std::vector<struct symbol *> template_args;
15744
15745 child_die = die->child;
15746
15747 while (child_die && child_die->tag)
15748 {
15749 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15750
15751 if (is_variant_part && discr_offset == child_die->sect_off)
15752 fi.fields.back ().variant.is_discriminant = true;
15753
15754 child_die = sibling_die (child_die);
15755 }
15756
15757 /* Attach template arguments to type. */
15758 if (!template_args.empty ())
15759 {
15760 has_template_parameters = true;
15761 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15762 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15763 TYPE_TEMPLATE_ARGUMENTS (type)
15764 = XOBNEWVEC (&objfile->objfile_obstack,
15765 struct symbol *,
15766 TYPE_N_TEMPLATE_ARGUMENTS (type));
15767 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15768 template_args.data (),
15769 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15770 * sizeof (struct symbol *)));
15771 }
15772
15773 /* Attach fields and member functions to the type. */
15774 if (fi.nfields)
15775 dwarf2_attach_fields_to_type (&fi, type, cu);
15776 if (!fi.fnfieldlists.empty ())
15777 {
15778 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15779
15780 /* Get the type which refers to the base class (possibly this
15781 class itself) which contains the vtable pointer for the current
15782 class from the DW_AT_containing_type attribute. This use of
15783 DW_AT_containing_type is a GNU extension. */
15784
15785 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15786 {
15787 struct type *t = die_containing_type (die, cu);
15788
15789 set_type_vptr_basetype (type, t);
15790 if (type == t)
15791 {
15792 int i;
15793
15794 /* Our own class provides vtbl ptr. */
15795 for (i = TYPE_NFIELDS (t) - 1;
15796 i >= TYPE_N_BASECLASSES (t);
15797 --i)
15798 {
15799 const char *fieldname = TYPE_FIELD_NAME (t, i);
15800
15801 if (is_vtable_name (fieldname, cu))
15802 {
15803 set_type_vptr_fieldno (type, i);
15804 break;
15805 }
15806 }
15807
15808 /* Complain if virtual function table field not found. */
15809 if (i < TYPE_N_BASECLASSES (t))
15810 complaint (_("virtual function table pointer "
15811 "not found when defining class '%s'"),
15812 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15813 }
15814 else
15815 {
15816 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15817 }
15818 }
15819 else if (cu->producer
15820 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15821 {
15822 /* The IBM XLC compiler does not provide direct indication
15823 of the containing type, but the vtable pointer is
15824 always named __vfp. */
15825
15826 int i;
15827
15828 for (i = TYPE_NFIELDS (type) - 1;
15829 i >= TYPE_N_BASECLASSES (type);
15830 --i)
15831 {
15832 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15833 {
15834 set_type_vptr_fieldno (type, i);
15835 set_type_vptr_basetype (type, type);
15836 break;
15837 }
15838 }
15839 }
15840 }
15841
15842 /* Copy fi.typedef_field_list linked list elements content into the
15843 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15844 if (!fi.typedef_field_list.empty ())
15845 {
15846 int count = fi.typedef_field_list.size ();
15847
15848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15849 TYPE_TYPEDEF_FIELD_ARRAY (type)
15850 = ((struct decl_field *)
15851 TYPE_ALLOC (type,
15852 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15853 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15854
15855 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15856 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15857 }
15858
15859 /* Copy fi.nested_types_list linked list elements content into the
15860 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15861 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15862 {
15863 int count = fi.nested_types_list.size ();
15864
15865 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15866 TYPE_NESTED_TYPES_ARRAY (type)
15867 = ((struct decl_field *)
15868 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15869 TYPE_NESTED_TYPES_COUNT (type) = count;
15870
15871 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15872 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15873 }
15874 }
15875
15876 quirk_gcc_member_function_pointer (type, objfile);
15877 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15878 cu->rust_unions.push_back (type);
15879
15880 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15881 snapshots) has been known to create a die giving a declaration
15882 for a class that has, as a child, a die giving a definition for a
15883 nested class. So we have to process our children even if the
15884 current die is a declaration. Normally, of course, a declaration
15885 won't have any children at all. */
15886
15887 child_die = die->child;
15888
15889 while (child_die != NULL && child_die->tag)
15890 {
15891 if (child_die->tag == DW_TAG_member
15892 || child_die->tag == DW_TAG_variable
15893 || child_die->tag == DW_TAG_inheritance
15894 || child_die->tag == DW_TAG_template_value_param
15895 || child_die->tag == DW_TAG_template_type_param)
15896 {
15897 /* Do nothing. */
15898 }
15899 else
15900 process_die (child_die, cu);
15901
15902 child_die = sibling_die (child_die);
15903 }
15904
15905 /* Do not consider external references. According to the DWARF standard,
15906 these DIEs are identified by the fact that they have no byte_size
15907 attribute, and a declaration attribute. */
15908 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15909 || !die_is_declaration (die, cu))
15910 {
15911 struct symbol *sym = new_symbol (die, type, cu);
15912
15913 if (has_template_parameters)
15914 {
15915 struct symtab *symtab;
15916 if (sym != nullptr)
15917 symtab = symbol_symtab (sym);
15918 else if (cu->line_header != nullptr)
15919 {
15920 /* Any related symtab will do. */
15921 symtab
15922 = cu->line_header->file_names ()[0].symtab;
15923 }
15924 else
15925 {
15926 symtab = nullptr;
15927 complaint (_("could not find suitable "
15928 "symtab for template parameter"
15929 " - DIE at %s [in module %s]"),
15930 sect_offset_str (die->sect_off),
15931 objfile_name (objfile));
15932 }
15933
15934 if (symtab != nullptr)
15935 {
15936 /* Make sure that the symtab is set on the new symbols.
15937 Even though they don't appear in this symtab directly,
15938 other parts of gdb assume that symbols do, and this is
15939 reasonably true. */
15940 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15941 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15942 }
15943 }
15944 }
15945 }
15946
15947 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15948 update TYPE using some information only available in DIE's children. */
15949
15950 static void
15951 update_enumeration_type_from_children (struct die_info *die,
15952 struct type *type,
15953 struct dwarf2_cu *cu)
15954 {
15955 struct die_info *child_die;
15956 int unsigned_enum = 1;
15957 int flag_enum = 1;
15958 ULONGEST mask = 0;
15959
15960 auto_obstack obstack;
15961
15962 for (child_die = die->child;
15963 child_die != NULL && child_die->tag;
15964 child_die = sibling_die (child_die))
15965 {
15966 struct attribute *attr;
15967 LONGEST value;
15968 const gdb_byte *bytes;
15969 struct dwarf2_locexpr_baton *baton;
15970 const char *name;
15971
15972 if (child_die->tag != DW_TAG_enumerator)
15973 continue;
15974
15975 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15976 if (attr == NULL)
15977 continue;
15978
15979 name = dwarf2_name (child_die, cu);
15980 if (name == NULL)
15981 name = "<anonymous enumerator>";
15982
15983 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15984 &value, &bytes, &baton);
15985 if (value < 0)
15986 {
15987 unsigned_enum = 0;
15988 flag_enum = 0;
15989 }
15990 else if ((mask & value) != 0)
15991 flag_enum = 0;
15992 else
15993 mask |= value;
15994
15995 /* If we already know that the enum type is neither unsigned, nor
15996 a flag type, no need to look at the rest of the enumerates. */
15997 if (!unsigned_enum && !flag_enum)
15998 break;
15999 }
16000
16001 if (unsigned_enum)
16002 TYPE_UNSIGNED (type) = 1;
16003 if (flag_enum)
16004 TYPE_FLAG_ENUM (type) = 1;
16005 }
16006
16007 /* Given a DW_AT_enumeration_type die, set its type. We do not
16008 complete the type's fields yet, or create any symbols. */
16009
16010 static struct type *
16011 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16012 {
16013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16014 struct type *type;
16015 struct attribute *attr;
16016 const char *name;
16017
16018 /* If the definition of this type lives in .debug_types, read that type.
16019 Don't follow DW_AT_specification though, that will take us back up
16020 the chain and we want to go down. */
16021 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16022 if (attr != nullptr)
16023 {
16024 type = get_DW_AT_signature_type (die, attr, cu);
16025
16026 /* The type's CU may not be the same as CU.
16027 Ensure TYPE is recorded with CU in die_type_hash. */
16028 return set_die_type (die, type, cu);
16029 }
16030
16031 type = alloc_type (objfile);
16032
16033 TYPE_CODE (type) = TYPE_CODE_ENUM;
16034 name = dwarf2_full_name (NULL, die, cu);
16035 if (name != NULL)
16036 TYPE_NAME (type) = name;
16037
16038 attr = dwarf2_attr (die, DW_AT_type, cu);
16039 if (attr != NULL)
16040 {
16041 struct type *underlying_type = die_type (die, cu);
16042
16043 TYPE_TARGET_TYPE (type) = underlying_type;
16044 }
16045
16046 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16047 if (attr != nullptr)
16048 {
16049 TYPE_LENGTH (type) = DW_UNSND (attr);
16050 }
16051 else
16052 {
16053 TYPE_LENGTH (type) = 0;
16054 }
16055
16056 maybe_set_alignment (cu, die, type);
16057
16058 /* The enumeration DIE can be incomplete. In Ada, any type can be
16059 declared as private in the package spec, and then defined only
16060 inside the package body. Such types are known as Taft Amendment
16061 Types. When another package uses such a type, an incomplete DIE
16062 may be generated by the compiler. */
16063 if (die_is_declaration (die, cu))
16064 TYPE_STUB (type) = 1;
16065
16066 /* Finish the creation of this type by using the enum's children.
16067 We must call this even when the underlying type has been provided
16068 so that we can determine if we're looking at a "flag" enum. */
16069 update_enumeration_type_from_children (die, type, cu);
16070
16071 /* If this type has an underlying type that is not a stub, then we
16072 may use its attributes. We always use the "unsigned" attribute
16073 in this situation, because ordinarily we guess whether the type
16074 is unsigned -- but the guess can be wrong and the underlying type
16075 can tell us the reality. However, we defer to a local size
16076 attribute if one exists, because this lets the compiler override
16077 the underlying type if needed. */
16078 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16079 {
16080 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16081 if (TYPE_LENGTH (type) == 0)
16082 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16083 if (TYPE_RAW_ALIGN (type) == 0
16084 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16085 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16086 }
16087
16088 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16089
16090 return set_die_type (die, type, cu);
16091 }
16092
16093 /* Given a pointer to a die which begins an enumeration, process all
16094 the dies that define the members of the enumeration, and create the
16095 symbol for the enumeration type.
16096
16097 NOTE: We reverse the order of the element list. */
16098
16099 static void
16100 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16101 {
16102 struct type *this_type;
16103
16104 this_type = get_die_type (die, cu);
16105 if (this_type == NULL)
16106 this_type = read_enumeration_type (die, cu);
16107
16108 if (die->child != NULL)
16109 {
16110 struct die_info *child_die;
16111 struct symbol *sym;
16112 std::vector<struct field> fields;
16113 const char *name;
16114
16115 child_die = die->child;
16116 while (child_die && child_die->tag)
16117 {
16118 if (child_die->tag != DW_TAG_enumerator)
16119 {
16120 process_die (child_die, cu);
16121 }
16122 else
16123 {
16124 name = dwarf2_name (child_die, cu);
16125 if (name)
16126 {
16127 sym = new_symbol (child_die, this_type, cu);
16128
16129 fields.emplace_back ();
16130 struct field &field = fields.back ();
16131
16132 FIELD_NAME (field) = sym->linkage_name ();
16133 FIELD_TYPE (field) = NULL;
16134 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16135 FIELD_BITSIZE (field) = 0;
16136 }
16137 }
16138
16139 child_die = sibling_die (child_die);
16140 }
16141
16142 if (!fields.empty ())
16143 {
16144 TYPE_NFIELDS (this_type) = fields.size ();
16145 TYPE_FIELDS (this_type) = (struct field *)
16146 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16147 memcpy (TYPE_FIELDS (this_type), fields.data (),
16148 sizeof (struct field) * fields.size ());
16149 }
16150 }
16151
16152 /* If we are reading an enum from a .debug_types unit, and the enum
16153 is a declaration, and the enum is not the signatured type in the
16154 unit, then we do not want to add a symbol for it. Adding a
16155 symbol would in some cases obscure the true definition of the
16156 enum, giving users an incomplete type when the definition is
16157 actually available. Note that we do not want to do this for all
16158 enums which are just declarations, because C++0x allows forward
16159 enum declarations. */
16160 if (cu->per_cu->is_debug_types
16161 && die_is_declaration (die, cu))
16162 {
16163 struct signatured_type *sig_type;
16164
16165 sig_type = (struct signatured_type *) cu->per_cu;
16166 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16167 if (sig_type->type_offset_in_section != die->sect_off)
16168 return;
16169 }
16170
16171 new_symbol (die, this_type, cu);
16172 }
16173
16174 /* Extract all information from a DW_TAG_array_type DIE and put it in
16175 the DIE's type field. For now, this only handles one dimensional
16176 arrays. */
16177
16178 static struct type *
16179 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16180 {
16181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16182 struct die_info *child_die;
16183 struct type *type;
16184 struct type *element_type, *range_type, *index_type;
16185 struct attribute *attr;
16186 const char *name;
16187 struct dynamic_prop *byte_stride_prop = NULL;
16188 unsigned int bit_stride = 0;
16189
16190 element_type = die_type (die, cu);
16191
16192 /* The die_type call above may have already set the type for this DIE. */
16193 type = get_die_type (die, cu);
16194 if (type)
16195 return type;
16196
16197 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16198 if (attr != NULL)
16199 {
16200 int stride_ok;
16201 struct type *prop_type
16202 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16203
16204 byte_stride_prop
16205 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16206 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16207 prop_type);
16208 if (!stride_ok)
16209 {
16210 complaint (_("unable to read array DW_AT_byte_stride "
16211 " - DIE at %s [in module %s]"),
16212 sect_offset_str (die->sect_off),
16213 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16214 /* Ignore this attribute. We will likely not be able to print
16215 arrays of this type correctly, but there is little we can do
16216 to help if we cannot read the attribute's value. */
16217 byte_stride_prop = NULL;
16218 }
16219 }
16220
16221 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16222 if (attr != NULL)
16223 bit_stride = DW_UNSND (attr);
16224
16225 /* Irix 6.2 native cc creates array types without children for
16226 arrays with unspecified length. */
16227 if (die->child == NULL)
16228 {
16229 index_type = objfile_type (objfile)->builtin_int;
16230 range_type = create_static_range_type (NULL, index_type, 0, -1);
16231 type = create_array_type_with_stride (NULL, element_type, range_type,
16232 byte_stride_prop, bit_stride);
16233 return set_die_type (die, type, cu);
16234 }
16235
16236 std::vector<struct type *> range_types;
16237 child_die = die->child;
16238 while (child_die && child_die->tag)
16239 {
16240 if (child_die->tag == DW_TAG_subrange_type)
16241 {
16242 struct type *child_type = read_type_die (child_die, cu);
16243
16244 if (child_type != NULL)
16245 {
16246 /* The range type was succesfully read. Save it for the
16247 array type creation. */
16248 range_types.push_back (child_type);
16249 }
16250 }
16251 child_die = sibling_die (child_die);
16252 }
16253
16254 /* Dwarf2 dimensions are output from left to right, create the
16255 necessary array types in backwards order. */
16256
16257 type = element_type;
16258
16259 if (read_array_order (die, cu) == DW_ORD_col_major)
16260 {
16261 int i = 0;
16262
16263 while (i < range_types.size ())
16264 type = create_array_type_with_stride (NULL, type, range_types[i++],
16265 byte_stride_prop, bit_stride);
16266 }
16267 else
16268 {
16269 size_t ndim = range_types.size ();
16270 while (ndim-- > 0)
16271 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16272 byte_stride_prop, bit_stride);
16273 }
16274
16275 /* Understand Dwarf2 support for vector types (like they occur on
16276 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16277 array type. This is not part of the Dwarf2/3 standard yet, but a
16278 custom vendor extension. The main difference between a regular
16279 array and the vector variant is that vectors are passed by value
16280 to functions. */
16281 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16282 if (attr != nullptr)
16283 make_vector_type (type);
16284
16285 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16286 implementation may choose to implement triple vectors using this
16287 attribute. */
16288 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16289 if (attr != nullptr)
16290 {
16291 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16292 TYPE_LENGTH (type) = DW_UNSND (attr);
16293 else
16294 complaint (_("DW_AT_byte_size for array type smaller "
16295 "than the total size of elements"));
16296 }
16297
16298 name = dwarf2_name (die, cu);
16299 if (name)
16300 TYPE_NAME (type) = name;
16301
16302 maybe_set_alignment (cu, die, type);
16303
16304 /* Install the type in the die. */
16305 set_die_type (die, type, cu);
16306
16307 /* set_die_type should be already done. */
16308 set_descriptive_type (type, die, cu);
16309
16310 return type;
16311 }
16312
16313 static enum dwarf_array_dim_ordering
16314 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16315 {
16316 struct attribute *attr;
16317
16318 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16319
16320 if (attr != nullptr)
16321 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16322
16323 /* GNU F77 is a special case, as at 08/2004 array type info is the
16324 opposite order to the dwarf2 specification, but data is still
16325 laid out as per normal fortran.
16326
16327 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16328 version checking. */
16329
16330 if (cu->language == language_fortran
16331 && cu->producer && strstr (cu->producer, "GNU F77"))
16332 {
16333 return DW_ORD_row_major;
16334 }
16335
16336 switch (cu->language_defn->la_array_ordering)
16337 {
16338 case array_column_major:
16339 return DW_ORD_col_major;
16340 case array_row_major:
16341 default:
16342 return DW_ORD_row_major;
16343 };
16344 }
16345
16346 /* Extract all information from a DW_TAG_set_type DIE and put it in
16347 the DIE's type field. */
16348
16349 static struct type *
16350 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16351 {
16352 struct type *domain_type, *set_type;
16353 struct attribute *attr;
16354
16355 domain_type = die_type (die, cu);
16356
16357 /* The die_type call above may have already set the type for this DIE. */
16358 set_type = get_die_type (die, cu);
16359 if (set_type)
16360 return set_type;
16361
16362 set_type = create_set_type (NULL, domain_type);
16363
16364 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16365 if (attr != nullptr)
16366 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16367
16368 maybe_set_alignment (cu, die, set_type);
16369
16370 return set_die_type (die, set_type, cu);
16371 }
16372
16373 /* A helper for read_common_block that creates a locexpr baton.
16374 SYM is the symbol which we are marking as computed.
16375 COMMON_DIE is the DIE for the common block.
16376 COMMON_LOC is the location expression attribute for the common
16377 block itself.
16378 MEMBER_LOC is the location expression attribute for the particular
16379 member of the common block that we are processing.
16380 CU is the CU from which the above come. */
16381
16382 static void
16383 mark_common_block_symbol_computed (struct symbol *sym,
16384 struct die_info *common_die,
16385 struct attribute *common_loc,
16386 struct attribute *member_loc,
16387 struct dwarf2_cu *cu)
16388 {
16389 struct dwarf2_per_objfile *dwarf2_per_objfile
16390 = cu->per_cu->dwarf2_per_objfile;
16391 struct objfile *objfile = dwarf2_per_objfile->objfile;
16392 struct dwarf2_locexpr_baton *baton;
16393 gdb_byte *ptr;
16394 unsigned int cu_off;
16395 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16396 LONGEST offset = 0;
16397
16398 gdb_assert (common_loc && member_loc);
16399 gdb_assert (common_loc->form_is_block ());
16400 gdb_assert (member_loc->form_is_block ()
16401 || member_loc->form_is_constant ());
16402
16403 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16404 baton->per_cu = cu->per_cu;
16405 gdb_assert (baton->per_cu);
16406
16407 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16408
16409 if (member_loc->form_is_constant ())
16410 {
16411 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16412 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16413 }
16414 else
16415 baton->size += DW_BLOCK (member_loc)->size;
16416
16417 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16418 baton->data = ptr;
16419
16420 *ptr++ = DW_OP_call4;
16421 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16422 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16423 ptr += 4;
16424
16425 if (member_loc->form_is_constant ())
16426 {
16427 *ptr++ = DW_OP_addr;
16428 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16429 ptr += cu->header.addr_size;
16430 }
16431 else
16432 {
16433 /* We have to copy the data here, because DW_OP_call4 will only
16434 use a DW_AT_location attribute. */
16435 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16436 ptr += DW_BLOCK (member_loc)->size;
16437 }
16438
16439 *ptr++ = DW_OP_plus;
16440 gdb_assert (ptr - baton->data == baton->size);
16441
16442 SYMBOL_LOCATION_BATON (sym) = baton;
16443 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16444 }
16445
16446 /* Create appropriate locally-scoped variables for all the
16447 DW_TAG_common_block entries. Also create a struct common_block
16448 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16449 is used to separate the common blocks name namespace from regular
16450 variable names. */
16451
16452 static void
16453 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16454 {
16455 struct attribute *attr;
16456
16457 attr = dwarf2_attr (die, DW_AT_location, cu);
16458 if (attr != nullptr)
16459 {
16460 /* Support the .debug_loc offsets. */
16461 if (attr->form_is_block ())
16462 {
16463 /* Ok. */
16464 }
16465 else if (attr->form_is_section_offset ())
16466 {
16467 dwarf2_complex_location_expr_complaint ();
16468 attr = NULL;
16469 }
16470 else
16471 {
16472 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16473 "common block member");
16474 attr = NULL;
16475 }
16476 }
16477
16478 if (die->child != NULL)
16479 {
16480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16481 struct die_info *child_die;
16482 size_t n_entries = 0, size;
16483 struct common_block *common_block;
16484 struct symbol *sym;
16485
16486 for (child_die = die->child;
16487 child_die && child_die->tag;
16488 child_die = sibling_die (child_die))
16489 ++n_entries;
16490
16491 size = (sizeof (struct common_block)
16492 + (n_entries - 1) * sizeof (struct symbol *));
16493 common_block
16494 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16495 size);
16496 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16497 common_block->n_entries = 0;
16498
16499 for (child_die = die->child;
16500 child_die && child_die->tag;
16501 child_die = sibling_die (child_die))
16502 {
16503 /* Create the symbol in the DW_TAG_common_block block in the current
16504 symbol scope. */
16505 sym = new_symbol (child_die, NULL, cu);
16506 if (sym != NULL)
16507 {
16508 struct attribute *member_loc;
16509
16510 common_block->contents[common_block->n_entries++] = sym;
16511
16512 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16513 cu);
16514 if (member_loc)
16515 {
16516 /* GDB has handled this for a long time, but it is
16517 not specified by DWARF. It seems to have been
16518 emitted by gfortran at least as recently as:
16519 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16520 complaint (_("Variable in common block has "
16521 "DW_AT_data_member_location "
16522 "- DIE at %s [in module %s]"),
16523 sect_offset_str (child_die->sect_off),
16524 objfile_name (objfile));
16525
16526 if (member_loc->form_is_section_offset ())
16527 dwarf2_complex_location_expr_complaint ();
16528 else if (member_loc->form_is_constant ()
16529 || member_loc->form_is_block ())
16530 {
16531 if (attr != nullptr)
16532 mark_common_block_symbol_computed (sym, die, attr,
16533 member_loc, cu);
16534 }
16535 else
16536 dwarf2_complex_location_expr_complaint ();
16537 }
16538 }
16539 }
16540
16541 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16542 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16543 }
16544 }
16545
16546 /* Create a type for a C++ namespace. */
16547
16548 static struct type *
16549 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16550 {
16551 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16552 const char *previous_prefix, *name;
16553 int is_anonymous;
16554 struct type *type;
16555
16556 /* For extensions, reuse the type of the original namespace. */
16557 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16558 {
16559 struct die_info *ext_die;
16560 struct dwarf2_cu *ext_cu = cu;
16561
16562 ext_die = dwarf2_extension (die, &ext_cu);
16563 type = read_type_die (ext_die, ext_cu);
16564
16565 /* EXT_CU may not be the same as CU.
16566 Ensure TYPE is recorded with CU in die_type_hash. */
16567 return set_die_type (die, type, cu);
16568 }
16569
16570 name = namespace_name (die, &is_anonymous, cu);
16571
16572 /* Now build the name of the current namespace. */
16573
16574 previous_prefix = determine_prefix (die, cu);
16575 if (previous_prefix[0] != '\0')
16576 name = typename_concat (&objfile->objfile_obstack,
16577 previous_prefix, name, 0, cu);
16578
16579 /* Create the type. */
16580 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16581
16582 return set_die_type (die, type, cu);
16583 }
16584
16585 /* Read a namespace scope. */
16586
16587 static void
16588 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16589 {
16590 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16591 int is_anonymous;
16592
16593 /* Add a symbol associated to this if we haven't seen the namespace
16594 before. Also, add a using directive if it's an anonymous
16595 namespace. */
16596
16597 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16598 {
16599 struct type *type;
16600
16601 type = read_type_die (die, cu);
16602 new_symbol (die, type, cu);
16603
16604 namespace_name (die, &is_anonymous, cu);
16605 if (is_anonymous)
16606 {
16607 const char *previous_prefix = determine_prefix (die, cu);
16608
16609 std::vector<const char *> excludes;
16610 add_using_directive (using_directives (cu),
16611 previous_prefix, TYPE_NAME (type), NULL,
16612 NULL, excludes, 0, &objfile->objfile_obstack);
16613 }
16614 }
16615
16616 if (die->child != NULL)
16617 {
16618 struct die_info *child_die = die->child;
16619
16620 while (child_die && child_die->tag)
16621 {
16622 process_die (child_die, cu);
16623 child_die = sibling_die (child_die);
16624 }
16625 }
16626 }
16627
16628 /* Read a Fortran module as type. This DIE can be only a declaration used for
16629 imported module. Still we need that type as local Fortran "use ... only"
16630 declaration imports depend on the created type in determine_prefix. */
16631
16632 static struct type *
16633 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16634 {
16635 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16636 const char *module_name;
16637 struct type *type;
16638
16639 module_name = dwarf2_name (die, cu);
16640 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16641
16642 return set_die_type (die, type, cu);
16643 }
16644
16645 /* Read a Fortran module. */
16646
16647 static void
16648 read_module (struct die_info *die, struct dwarf2_cu *cu)
16649 {
16650 struct die_info *child_die = die->child;
16651 struct type *type;
16652
16653 type = read_type_die (die, cu);
16654 new_symbol (die, type, cu);
16655
16656 while (child_die && child_die->tag)
16657 {
16658 process_die (child_die, cu);
16659 child_die = sibling_die (child_die);
16660 }
16661 }
16662
16663 /* Return the name of the namespace represented by DIE. Set
16664 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16665 namespace. */
16666
16667 static const char *
16668 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16669 {
16670 struct die_info *current_die;
16671 const char *name = NULL;
16672
16673 /* Loop through the extensions until we find a name. */
16674
16675 for (current_die = die;
16676 current_die != NULL;
16677 current_die = dwarf2_extension (die, &cu))
16678 {
16679 /* We don't use dwarf2_name here so that we can detect the absence
16680 of a name -> anonymous namespace. */
16681 name = dwarf2_string_attr (die, DW_AT_name, cu);
16682
16683 if (name != NULL)
16684 break;
16685 }
16686
16687 /* Is it an anonymous namespace? */
16688
16689 *is_anonymous = (name == NULL);
16690 if (*is_anonymous)
16691 name = CP_ANONYMOUS_NAMESPACE_STR;
16692
16693 return name;
16694 }
16695
16696 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16697 the user defined type vector. */
16698
16699 static struct type *
16700 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16701 {
16702 struct gdbarch *gdbarch
16703 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16704 struct comp_unit_head *cu_header = &cu->header;
16705 struct type *type;
16706 struct attribute *attr_byte_size;
16707 struct attribute *attr_address_class;
16708 int byte_size, addr_class;
16709 struct type *target_type;
16710
16711 target_type = die_type (die, cu);
16712
16713 /* The die_type call above may have already set the type for this DIE. */
16714 type = get_die_type (die, cu);
16715 if (type)
16716 return type;
16717
16718 type = lookup_pointer_type (target_type);
16719
16720 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16721 if (attr_byte_size)
16722 byte_size = DW_UNSND (attr_byte_size);
16723 else
16724 byte_size = cu_header->addr_size;
16725
16726 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16727 if (attr_address_class)
16728 addr_class = DW_UNSND (attr_address_class);
16729 else
16730 addr_class = DW_ADDR_none;
16731
16732 ULONGEST alignment = get_alignment (cu, die);
16733
16734 /* If the pointer size, alignment, or address class is different
16735 than the default, create a type variant marked as such and set
16736 the length accordingly. */
16737 if (TYPE_LENGTH (type) != byte_size
16738 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16739 && alignment != TYPE_RAW_ALIGN (type))
16740 || addr_class != DW_ADDR_none)
16741 {
16742 if (gdbarch_address_class_type_flags_p (gdbarch))
16743 {
16744 int type_flags;
16745
16746 type_flags = gdbarch_address_class_type_flags
16747 (gdbarch, byte_size, addr_class);
16748 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16749 == 0);
16750 type = make_type_with_address_space (type, type_flags);
16751 }
16752 else if (TYPE_LENGTH (type) != byte_size)
16753 {
16754 complaint (_("invalid pointer size %d"), byte_size);
16755 }
16756 else if (TYPE_RAW_ALIGN (type) != alignment)
16757 {
16758 complaint (_("Invalid DW_AT_alignment"
16759 " - DIE at %s [in module %s]"),
16760 sect_offset_str (die->sect_off),
16761 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16762 }
16763 else
16764 {
16765 /* Should we also complain about unhandled address classes? */
16766 }
16767 }
16768
16769 TYPE_LENGTH (type) = byte_size;
16770 set_type_align (type, alignment);
16771 return set_die_type (die, type, cu);
16772 }
16773
16774 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16775 the user defined type vector. */
16776
16777 static struct type *
16778 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16779 {
16780 struct type *type;
16781 struct type *to_type;
16782 struct type *domain;
16783
16784 to_type = die_type (die, cu);
16785 domain = die_containing_type (die, cu);
16786
16787 /* The calls above may have already set the type for this DIE. */
16788 type = get_die_type (die, cu);
16789 if (type)
16790 return type;
16791
16792 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16793 type = lookup_methodptr_type (to_type);
16794 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16795 {
16796 struct type *new_type
16797 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16798
16799 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16800 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16801 TYPE_VARARGS (to_type));
16802 type = lookup_methodptr_type (new_type);
16803 }
16804 else
16805 type = lookup_memberptr_type (to_type, domain);
16806
16807 return set_die_type (die, type, cu);
16808 }
16809
16810 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16811 the user defined type vector. */
16812
16813 static struct type *
16814 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16815 enum type_code refcode)
16816 {
16817 struct comp_unit_head *cu_header = &cu->header;
16818 struct type *type, *target_type;
16819 struct attribute *attr;
16820
16821 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16822
16823 target_type = die_type (die, cu);
16824
16825 /* The die_type call above may have already set the type for this DIE. */
16826 type = get_die_type (die, cu);
16827 if (type)
16828 return type;
16829
16830 type = lookup_reference_type (target_type, refcode);
16831 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16832 if (attr != nullptr)
16833 {
16834 TYPE_LENGTH (type) = DW_UNSND (attr);
16835 }
16836 else
16837 {
16838 TYPE_LENGTH (type) = cu_header->addr_size;
16839 }
16840 maybe_set_alignment (cu, die, type);
16841 return set_die_type (die, type, cu);
16842 }
16843
16844 /* Add the given cv-qualifiers to the element type of the array. GCC
16845 outputs DWARF type qualifiers that apply to an array, not the
16846 element type. But GDB relies on the array element type to carry
16847 the cv-qualifiers. This mimics section 6.7.3 of the C99
16848 specification. */
16849
16850 static struct type *
16851 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16852 struct type *base_type, int cnst, int voltl)
16853 {
16854 struct type *el_type, *inner_array;
16855
16856 base_type = copy_type (base_type);
16857 inner_array = base_type;
16858
16859 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16860 {
16861 TYPE_TARGET_TYPE (inner_array) =
16862 copy_type (TYPE_TARGET_TYPE (inner_array));
16863 inner_array = TYPE_TARGET_TYPE (inner_array);
16864 }
16865
16866 el_type = TYPE_TARGET_TYPE (inner_array);
16867 cnst |= TYPE_CONST (el_type);
16868 voltl |= TYPE_VOLATILE (el_type);
16869 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16870
16871 return set_die_type (die, base_type, cu);
16872 }
16873
16874 static struct type *
16875 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16876 {
16877 struct type *base_type, *cv_type;
16878
16879 base_type = die_type (die, cu);
16880
16881 /* The die_type call above may have already set the type for this DIE. */
16882 cv_type = get_die_type (die, cu);
16883 if (cv_type)
16884 return cv_type;
16885
16886 /* In case the const qualifier is applied to an array type, the element type
16887 is so qualified, not the array type (section 6.7.3 of C99). */
16888 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16889 return add_array_cv_type (die, cu, base_type, 1, 0);
16890
16891 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16892 return set_die_type (die, cv_type, cu);
16893 }
16894
16895 static struct type *
16896 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct type *base_type, *cv_type;
16899
16900 base_type = die_type (die, cu);
16901
16902 /* The die_type call above may have already set the type for this DIE. */
16903 cv_type = get_die_type (die, cu);
16904 if (cv_type)
16905 return cv_type;
16906
16907 /* In case the volatile qualifier is applied to an array type, the
16908 element type is so qualified, not the array type (section 6.7.3
16909 of C99). */
16910 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16911 return add_array_cv_type (die, cu, base_type, 0, 1);
16912
16913 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16914 return set_die_type (die, cv_type, cu);
16915 }
16916
16917 /* Handle DW_TAG_restrict_type. */
16918
16919 static struct type *
16920 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16921 {
16922 struct type *base_type, *cv_type;
16923
16924 base_type = die_type (die, cu);
16925
16926 /* The die_type call above may have already set the type for this DIE. */
16927 cv_type = get_die_type (die, cu);
16928 if (cv_type)
16929 return cv_type;
16930
16931 cv_type = make_restrict_type (base_type);
16932 return set_die_type (die, cv_type, cu);
16933 }
16934
16935 /* Handle DW_TAG_atomic_type. */
16936
16937 static struct type *
16938 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16939 {
16940 struct type *base_type, *cv_type;
16941
16942 base_type = die_type (die, cu);
16943
16944 /* The die_type call above may have already set the type for this DIE. */
16945 cv_type = get_die_type (die, cu);
16946 if (cv_type)
16947 return cv_type;
16948
16949 cv_type = make_atomic_type (base_type);
16950 return set_die_type (die, cv_type, cu);
16951 }
16952
16953 /* Extract all information from a DW_TAG_string_type DIE and add to
16954 the user defined type vector. It isn't really a user defined type,
16955 but it behaves like one, with other DIE's using an AT_user_def_type
16956 attribute to reference it. */
16957
16958 static struct type *
16959 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16960 {
16961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16963 struct type *type, *range_type, *index_type, *char_type;
16964 struct attribute *attr;
16965 struct dynamic_prop prop;
16966 bool length_is_constant = true;
16967 LONGEST length;
16968
16969 /* There are a couple of places where bit sizes might be made use of
16970 when parsing a DW_TAG_string_type, however, no producer that we know
16971 of make use of these. Handling bit sizes that are a multiple of the
16972 byte size is easy enough, but what about other bit sizes? Lets deal
16973 with that problem when we have to. Warn about these attributes being
16974 unsupported, then parse the type and ignore them like we always
16975 have. */
16976 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16977 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16978 {
16979 static bool warning_printed = false;
16980 if (!warning_printed)
16981 {
16982 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16983 "currently supported on DW_TAG_string_type."));
16984 warning_printed = true;
16985 }
16986 }
16987
16988 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16989 if (attr != nullptr && !attr->form_is_constant ())
16990 {
16991 /* The string length describes the location at which the length of
16992 the string can be found. The size of the length field can be
16993 specified with one of the attributes below. */
16994 struct type *prop_type;
16995 struct attribute *len
16996 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16997 if (len == nullptr)
16998 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16999 if (len != nullptr && len->form_is_constant ())
17000 {
17001 /* Pass 0 as the default as we know this attribute is constant
17002 and the default value will not be returned. */
17003 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17004 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17005 }
17006 else
17007 {
17008 /* If the size is not specified then we assume it is the size of
17009 an address on this target. */
17010 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17011 }
17012
17013 /* Convert the attribute into a dynamic property. */
17014 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17015 length = 1;
17016 else
17017 length_is_constant = false;
17018 }
17019 else if (attr != nullptr)
17020 {
17021 /* This DW_AT_string_length just contains the length with no
17022 indirection. There's no need to create a dynamic property in this
17023 case. Pass 0 for the default value as we know it will not be
17024 returned in this case. */
17025 length = dwarf2_get_attr_constant_value (attr, 0);
17026 }
17027 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17028 {
17029 /* We don't currently support non-constant byte sizes for strings. */
17030 length = dwarf2_get_attr_constant_value (attr, 1);
17031 }
17032 else
17033 {
17034 /* Use 1 as a fallback length if we have nothing else. */
17035 length = 1;
17036 }
17037
17038 index_type = objfile_type (objfile)->builtin_int;
17039 if (length_is_constant)
17040 range_type = create_static_range_type (NULL, index_type, 1, length);
17041 else
17042 {
17043 struct dynamic_prop low_bound;
17044
17045 low_bound.kind = PROP_CONST;
17046 low_bound.data.const_val = 1;
17047 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17048 }
17049 char_type = language_string_char_type (cu->language_defn, gdbarch);
17050 type = create_string_type (NULL, char_type, range_type);
17051
17052 return set_die_type (die, type, cu);
17053 }
17054
17055 /* Assuming that DIE corresponds to a function, returns nonzero
17056 if the function is prototyped. */
17057
17058 static int
17059 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17060 {
17061 struct attribute *attr;
17062
17063 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17064 if (attr && (DW_UNSND (attr) != 0))
17065 return 1;
17066
17067 /* The DWARF standard implies that the DW_AT_prototyped attribute
17068 is only meaningful for C, but the concept also extends to other
17069 languages that allow unprototyped functions (Eg: Objective C).
17070 For all other languages, assume that functions are always
17071 prototyped. */
17072 if (cu->language != language_c
17073 && cu->language != language_objc
17074 && cu->language != language_opencl)
17075 return 1;
17076
17077 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17078 prototyped and unprototyped functions; default to prototyped,
17079 since that is more common in modern code (and RealView warns
17080 about unprototyped functions). */
17081 if (producer_is_realview (cu->producer))
17082 return 1;
17083
17084 return 0;
17085 }
17086
17087 /* Handle DIES due to C code like:
17088
17089 struct foo
17090 {
17091 int (*funcp)(int a, long l);
17092 int b;
17093 };
17094
17095 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17096
17097 static struct type *
17098 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17099 {
17100 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17101 struct type *type; /* Type that this function returns. */
17102 struct type *ftype; /* Function that returns above type. */
17103 struct attribute *attr;
17104
17105 type = die_type (die, cu);
17106
17107 /* The die_type call above may have already set the type for this DIE. */
17108 ftype = get_die_type (die, cu);
17109 if (ftype)
17110 return ftype;
17111
17112 ftype = lookup_function_type (type);
17113
17114 if (prototyped_function_p (die, cu))
17115 TYPE_PROTOTYPED (ftype) = 1;
17116
17117 /* Store the calling convention in the type if it's available in
17118 the subroutine die. Otherwise set the calling convention to
17119 the default value DW_CC_normal. */
17120 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17121 if (attr != nullptr
17122 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17123 TYPE_CALLING_CONVENTION (ftype)
17124 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17125 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17126 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17127 else
17128 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17129
17130 /* Record whether the function returns normally to its caller or not
17131 if the DWARF producer set that information. */
17132 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17133 if (attr && (DW_UNSND (attr) != 0))
17134 TYPE_NO_RETURN (ftype) = 1;
17135
17136 /* We need to add the subroutine type to the die immediately so
17137 we don't infinitely recurse when dealing with parameters
17138 declared as the same subroutine type. */
17139 set_die_type (die, ftype, cu);
17140
17141 if (die->child != NULL)
17142 {
17143 struct type *void_type = objfile_type (objfile)->builtin_void;
17144 struct die_info *child_die;
17145 int nparams, iparams;
17146
17147 /* Count the number of parameters.
17148 FIXME: GDB currently ignores vararg functions, but knows about
17149 vararg member functions. */
17150 nparams = 0;
17151 child_die = die->child;
17152 while (child_die && child_die->tag)
17153 {
17154 if (child_die->tag == DW_TAG_formal_parameter)
17155 nparams++;
17156 else if (child_die->tag == DW_TAG_unspecified_parameters)
17157 TYPE_VARARGS (ftype) = 1;
17158 child_die = sibling_die (child_die);
17159 }
17160
17161 /* Allocate storage for parameters and fill them in. */
17162 TYPE_NFIELDS (ftype) = nparams;
17163 TYPE_FIELDS (ftype) = (struct field *)
17164 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17165
17166 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17167 even if we error out during the parameters reading below. */
17168 for (iparams = 0; iparams < nparams; iparams++)
17169 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17170
17171 iparams = 0;
17172 child_die = die->child;
17173 while (child_die && child_die->tag)
17174 {
17175 if (child_die->tag == DW_TAG_formal_parameter)
17176 {
17177 struct type *arg_type;
17178
17179 /* DWARF version 2 has no clean way to discern C++
17180 static and non-static member functions. G++ helps
17181 GDB by marking the first parameter for non-static
17182 member functions (which is the this pointer) as
17183 artificial. We pass this information to
17184 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17185
17186 DWARF version 3 added DW_AT_object_pointer, which GCC
17187 4.5 does not yet generate. */
17188 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17189 if (attr != nullptr)
17190 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17191 else
17192 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17193 arg_type = die_type (child_die, cu);
17194
17195 /* RealView does not mark THIS as const, which the testsuite
17196 expects. GCC marks THIS as const in method definitions,
17197 but not in the class specifications (GCC PR 43053). */
17198 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17199 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17200 {
17201 int is_this = 0;
17202 struct dwarf2_cu *arg_cu = cu;
17203 const char *name = dwarf2_name (child_die, cu);
17204
17205 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17206 if (attr != nullptr)
17207 {
17208 /* If the compiler emits this, use it. */
17209 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17210 is_this = 1;
17211 }
17212 else if (name && strcmp (name, "this") == 0)
17213 /* Function definitions will have the argument names. */
17214 is_this = 1;
17215 else if (name == NULL && iparams == 0)
17216 /* Declarations may not have the names, so like
17217 elsewhere in GDB, assume an artificial first
17218 argument is "this". */
17219 is_this = 1;
17220
17221 if (is_this)
17222 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17223 arg_type, 0);
17224 }
17225
17226 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17227 iparams++;
17228 }
17229 child_die = sibling_die (child_die);
17230 }
17231 }
17232
17233 return ftype;
17234 }
17235
17236 static struct type *
17237 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17238 {
17239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17240 const char *name = NULL;
17241 struct type *this_type, *target_type;
17242
17243 name = dwarf2_full_name (NULL, die, cu);
17244 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17245 TYPE_TARGET_STUB (this_type) = 1;
17246 set_die_type (die, this_type, cu);
17247 target_type = die_type (die, cu);
17248 if (target_type != this_type)
17249 TYPE_TARGET_TYPE (this_type) = target_type;
17250 else
17251 {
17252 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17253 spec and cause infinite loops in GDB. */
17254 complaint (_("Self-referential DW_TAG_typedef "
17255 "- DIE at %s [in module %s]"),
17256 sect_offset_str (die->sect_off), objfile_name (objfile));
17257 TYPE_TARGET_TYPE (this_type) = NULL;
17258 }
17259 return this_type;
17260 }
17261
17262 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17263 (which may be different from NAME) to the architecture back-end to allow
17264 it to guess the correct format if necessary. */
17265
17266 static struct type *
17267 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17268 const char *name_hint, enum bfd_endian byte_order)
17269 {
17270 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17271 const struct floatformat **format;
17272 struct type *type;
17273
17274 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17275 if (format)
17276 type = init_float_type (objfile, bits, name, format, byte_order);
17277 else
17278 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17279
17280 return type;
17281 }
17282
17283 /* Allocate an integer type of size BITS and name NAME. */
17284
17285 static struct type *
17286 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17287 int bits, int unsigned_p, const char *name)
17288 {
17289 struct type *type;
17290
17291 /* Versions of Intel's C Compiler generate an integer type called "void"
17292 instead of using DW_TAG_unspecified_type. This has been seen on
17293 at least versions 14, 17, and 18. */
17294 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17295 && strcmp (name, "void") == 0)
17296 type = objfile_type (objfile)->builtin_void;
17297 else
17298 type = init_integer_type (objfile, bits, unsigned_p, name);
17299
17300 return type;
17301 }
17302
17303 /* Initialise and return a floating point type of size BITS suitable for
17304 use as a component of a complex number. The NAME_HINT is passed through
17305 when initialising the floating point type and is the name of the complex
17306 type.
17307
17308 As DWARF doesn't currently provide an explicit name for the components
17309 of a complex number, but it can be helpful to have these components
17310 named, we try to select a suitable name based on the size of the
17311 component. */
17312 static struct type *
17313 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17314 struct objfile *objfile,
17315 int bits, const char *name_hint,
17316 enum bfd_endian byte_order)
17317 {
17318 gdbarch *gdbarch = get_objfile_arch (objfile);
17319 struct type *tt = nullptr;
17320
17321 /* Try to find a suitable floating point builtin type of size BITS.
17322 We're going to use the name of this type as the name for the complex
17323 target type that we are about to create. */
17324 switch (cu->language)
17325 {
17326 case language_fortran:
17327 switch (bits)
17328 {
17329 case 32:
17330 tt = builtin_f_type (gdbarch)->builtin_real;
17331 break;
17332 case 64:
17333 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17334 break;
17335 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17336 case 128:
17337 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17338 break;
17339 }
17340 break;
17341 default:
17342 switch (bits)
17343 {
17344 case 32:
17345 tt = builtin_type (gdbarch)->builtin_float;
17346 break;
17347 case 64:
17348 tt = builtin_type (gdbarch)->builtin_double;
17349 break;
17350 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17351 case 128:
17352 tt = builtin_type (gdbarch)->builtin_long_double;
17353 break;
17354 }
17355 break;
17356 }
17357
17358 /* If the type we found doesn't match the size we were looking for, then
17359 pretend we didn't find a type at all, the complex target type we
17360 create will then be nameless. */
17361 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17362 tt = nullptr;
17363
17364 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17365 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17366 }
17367
17368 /* Find a representation of a given base type and install
17369 it in the TYPE field of the die. */
17370
17371 static struct type *
17372 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17373 {
17374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17375 struct type *type;
17376 struct attribute *attr;
17377 int encoding = 0, bits = 0;
17378 const char *name;
17379 gdbarch *arch;
17380
17381 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17382 if (attr != nullptr)
17383 encoding = DW_UNSND (attr);
17384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17385 if (attr != nullptr)
17386 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17387 name = dwarf2_name (die, cu);
17388 if (!name)
17389 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17390
17391 arch = get_objfile_arch (objfile);
17392 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17393
17394 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17395 if (attr)
17396 {
17397 int endianity = DW_UNSND (attr);
17398
17399 switch (endianity)
17400 {
17401 case DW_END_big:
17402 byte_order = BFD_ENDIAN_BIG;
17403 break;
17404 case DW_END_little:
17405 byte_order = BFD_ENDIAN_LITTLE;
17406 break;
17407 default:
17408 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17409 break;
17410 }
17411 }
17412
17413 switch (encoding)
17414 {
17415 case DW_ATE_address:
17416 /* Turn DW_ATE_address into a void * pointer. */
17417 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17418 type = init_pointer_type (objfile, bits, name, type);
17419 break;
17420 case DW_ATE_boolean:
17421 type = init_boolean_type (objfile, bits, 1, name);
17422 break;
17423 case DW_ATE_complex_float:
17424 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17425 byte_order);
17426 type = init_complex_type (objfile, name, type);
17427 break;
17428 case DW_ATE_decimal_float:
17429 type = init_decfloat_type (objfile, bits, name);
17430 break;
17431 case DW_ATE_float:
17432 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17433 break;
17434 case DW_ATE_signed:
17435 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17436 break;
17437 case DW_ATE_unsigned:
17438 if (cu->language == language_fortran
17439 && name
17440 && startswith (name, "character("))
17441 type = init_character_type (objfile, bits, 1, name);
17442 else
17443 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17444 break;
17445 case DW_ATE_signed_char:
17446 if (cu->language == language_ada || cu->language == language_m2
17447 || cu->language == language_pascal
17448 || cu->language == language_fortran)
17449 type = init_character_type (objfile, bits, 0, name);
17450 else
17451 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17452 break;
17453 case DW_ATE_unsigned_char:
17454 if (cu->language == language_ada || cu->language == language_m2
17455 || cu->language == language_pascal
17456 || cu->language == language_fortran
17457 || cu->language == language_rust)
17458 type = init_character_type (objfile, bits, 1, name);
17459 else
17460 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17461 break;
17462 case DW_ATE_UTF:
17463 {
17464 if (bits == 16)
17465 type = builtin_type (arch)->builtin_char16;
17466 else if (bits == 32)
17467 type = builtin_type (arch)->builtin_char32;
17468 else
17469 {
17470 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17471 bits);
17472 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17473 }
17474 return set_die_type (die, type, cu);
17475 }
17476 break;
17477
17478 default:
17479 complaint (_("unsupported DW_AT_encoding: '%s'"),
17480 dwarf_type_encoding_name (encoding));
17481 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17482 break;
17483 }
17484
17485 if (name && strcmp (name, "char") == 0)
17486 TYPE_NOSIGN (type) = 1;
17487
17488 maybe_set_alignment (cu, die, type);
17489
17490 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17491
17492 return set_die_type (die, type, cu);
17493 }
17494
17495 /* Parse dwarf attribute if it's a block, reference or constant and put the
17496 resulting value of the attribute into struct bound_prop.
17497 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17498
17499 static int
17500 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17501 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17502 struct type *default_type)
17503 {
17504 struct dwarf2_property_baton *baton;
17505 struct obstack *obstack
17506 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17507
17508 gdb_assert (default_type != NULL);
17509
17510 if (attr == NULL || prop == NULL)
17511 return 0;
17512
17513 if (attr->form_is_block ())
17514 {
17515 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17516 baton->property_type = default_type;
17517 baton->locexpr.per_cu = cu->per_cu;
17518 baton->locexpr.size = DW_BLOCK (attr)->size;
17519 baton->locexpr.data = DW_BLOCK (attr)->data;
17520 switch (attr->name)
17521 {
17522 case DW_AT_string_length:
17523 baton->locexpr.is_reference = true;
17524 break;
17525 default:
17526 baton->locexpr.is_reference = false;
17527 break;
17528 }
17529 prop->data.baton = baton;
17530 prop->kind = PROP_LOCEXPR;
17531 gdb_assert (prop->data.baton != NULL);
17532 }
17533 else if (attr->form_is_ref ())
17534 {
17535 struct dwarf2_cu *target_cu = cu;
17536 struct die_info *target_die;
17537 struct attribute *target_attr;
17538
17539 target_die = follow_die_ref (die, attr, &target_cu);
17540 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17541 if (target_attr == NULL)
17542 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17543 target_cu);
17544 if (target_attr == NULL)
17545 return 0;
17546
17547 switch (target_attr->name)
17548 {
17549 case DW_AT_location:
17550 if (target_attr->form_is_section_offset ())
17551 {
17552 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17553 baton->property_type = die_type (target_die, target_cu);
17554 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17555 prop->data.baton = baton;
17556 prop->kind = PROP_LOCLIST;
17557 gdb_assert (prop->data.baton != NULL);
17558 }
17559 else if (target_attr->form_is_block ())
17560 {
17561 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17562 baton->property_type = die_type (target_die, target_cu);
17563 baton->locexpr.per_cu = cu->per_cu;
17564 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17565 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17566 baton->locexpr.is_reference = true;
17567 prop->data.baton = baton;
17568 prop->kind = PROP_LOCEXPR;
17569 gdb_assert (prop->data.baton != NULL);
17570 }
17571 else
17572 {
17573 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17574 "dynamic property");
17575 return 0;
17576 }
17577 break;
17578 case DW_AT_data_member_location:
17579 {
17580 LONGEST offset;
17581
17582 if (!handle_data_member_location (target_die, target_cu,
17583 &offset))
17584 return 0;
17585
17586 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17587 baton->property_type = read_type_die (target_die->parent,
17588 target_cu);
17589 baton->offset_info.offset = offset;
17590 baton->offset_info.type = die_type (target_die, target_cu);
17591 prop->data.baton = baton;
17592 prop->kind = PROP_ADDR_OFFSET;
17593 break;
17594 }
17595 }
17596 }
17597 else if (attr->form_is_constant ())
17598 {
17599 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17600 prop->kind = PROP_CONST;
17601 }
17602 else
17603 {
17604 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17605 dwarf2_name (die, cu));
17606 return 0;
17607 }
17608
17609 return 1;
17610 }
17611
17612 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17613 UNSIGNED_P controls if the integer is unsigned or not. */
17614
17615 static struct type *
17616 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17617 int size_in_bytes, bool unsigned_p)
17618 {
17619 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17620 struct type *int_type;
17621
17622 /* Helper macro to examine the various builtin types. */
17623 #define TRY_TYPE(F) \
17624 int_type = (unsigned_p \
17625 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17626 : objfile_type (objfile)->builtin_ ## F); \
17627 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17628 return int_type
17629
17630 TRY_TYPE (char);
17631 TRY_TYPE (short);
17632 TRY_TYPE (int);
17633 TRY_TYPE (long);
17634 TRY_TYPE (long_long);
17635
17636 #undef TRY_TYPE
17637
17638 gdb_assert_not_reached ("unable to find suitable integer type");
17639 }
17640
17641 /* Find an integer type the same size as the address size given in the
17642 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17643 is unsigned or not. */
17644
17645 static struct type *
17646 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17647 bool unsigned_p)
17648 {
17649 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17650 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17651 }
17652
17653 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17654 present (which is valid) then compute the default type based on the
17655 compilation units address size. */
17656
17657 static struct type *
17658 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17659 {
17660 struct type *index_type = die_type (die, cu);
17661
17662 /* Dwarf-2 specifications explicitly allows to create subrange types
17663 without specifying a base type.
17664 In that case, the base type must be set to the type of
17665 the lower bound, upper bound or count, in that order, if any of these
17666 three attributes references an object that has a type.
17667 If no base type is found, the Dwarf-2 specifications say that
17668 a signed integer type of size equal to the size of an address should
17669 be used.
17670 For the following C code: `extern char gdb_int [];'
17671 GCC produces an empty range DIE.
17672 FIXME: muller/2010-05-28: Possible references to object for low bound,
17673 high bound or count are not yet handled by this code. */
17674 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17675 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17676
17677 return index_type;
17678 }
17679
17680 /* Read the given DW_AT_subrange DIE. */
17681
17682 static struct type *
17683 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17684 {
17685 struct type *base_type, *orig_base_type;
17686 struct type *range_type;
17687 struct attribute *attr;
17688 struct dynamic_prop low, high;
17689 int low_default_is_valid;
17690 int high_bound_is_count = 0;
17691 const char *name;
17692 ULONGEST negative_mask;
17693
17694 orig_base_type = read_subrange_index_type (die, cu);
17695
17696 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17697 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17698 creating the range type, but we use the result of check_typedef
17699 when examining properties of the type. */
17700 base_type = check_typedef (orig_base_type);
17701
17702 /* The die_type call above may have already set the type for this DIE. */
17703 range_type = get_die_type (die, cu);
17704 if (range_type)
17705 return range_type;
17706
17707 low.kind = PROP_CONST;
17708 high.kind = PROP_CONST;
17709 high.data.const_val = 0;
17710
17711 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17712 omitting DW_AT_lower_bound. */
17713 switch (cu->language)
17714 {
17715 case language_c:
17716 case language_cplus:
17717 low.data.const_val = 0;
17718 low_default_is_valid = 1;
17719 break;
17720 case language_fortran:
17721 low.data.const_val = 1;
17722 low_default_is_valid = 1;
17723 break;
17724 case language_d:
17725 case language_objc:
17726 case language_rust:
17727 low.data.const_val = 0;
17728 low_default_is_valid = (cu->header.version >= 4);
17729 break;
17730 case language_ada:
17731 case language_m2:
17732 case language_pascal:
17733 low.data.const_val = 1;
17734 low_default_is_valid = (cu->header.version >= 4);
17735 break;
17736 default:
17737 low.data.const_val = 0;
17738 low_default_is_valid = 0;
17739 break;
17740 }
17741
17742 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17743 if (attr != nullptr)
17744 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17745 else if (!low_default_is_valid)
17746 complaint (_("Missing DW_AT_lower_bound "
17747 "- DIE at %s [in module %s]"),
17748 sect_offset_str (die->sect_off),
17749 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17750
17751 struct attribute *attr_ub, *attr_count;
17752 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17753 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17754 {
17755 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17756 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17757 {
17758 /* If bounds are constant do the final calculation here. */
17759 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17760 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17761 else
17762 high_bound_is_count = 1;
17763 }
17764 else
17765 {
17766 if (attr_ub != NULL)
17767 complaint (_("Unresolved DW_AT_upper_bound "
17768 "- DIE at %s [in module %s]"),
17769 sect_offset_str (die->sect_off),
17770 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17771 if (attr_count != NULL)
17772 complaint (_("Unresolved DW_AT_count "
17773 "- DIE at %s [in module %s]"),
17774 sect_offset_str (die->sect_off),
17775 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17776 }
17777 }
17778
17779 LONGEST bias = 0;
17780 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17781 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17782 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17783
17784 /* Normally, the DWARF producers are expected to use a signed
17785 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17786 But this is unfortunately not always the case, as witnessed
17787 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17788 is used instead. To work around that ambiguity, we treat
17789 the bounds as signed, and thus sign-extend their values, when
17790 the base type is signed. */
17791 negative_mask =
17792 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17793 if (low.kind == PROP_CONST
17794 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17795 low.data.const_val |= negative_mask;
17796 if (high.kind == PROP_CONST
17797 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17798 high.data.const_val |= negative_mask;
17799
17800 /* Check for bit and byte strides. */
17801 struct dynamic_prop byte_stride_prop;
17802 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17803 if (attr_byte_stride != nullptr)
17804 {
17805 struct type *prop_type
17806 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17807 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17808 prop_type);
17809 }
17810
17811 struct dynamic_prop bit_stride_prop;
17812 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17813 if (attr_bit_stride != nullptr)
17814 {
17815 /* It only makes sense to have either a bit or byte stride. */
17816 if (attr_byte_stride != nullptr)
17817 {
17818 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17819 "- DIE at %s [in module %s]"),
17820 sect_offset_str (die->sect_off),
17821 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17822 attr_bit_stride = nullptr;
17823 }
17824 else
17825 {
17826 struct type *prop_type
17827 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17828 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17829 prop_type);
17830 }
17831 }
17832
17833 if (attr_byte_stride != nullptr
17834 || attr_bit_stride != nullptr)
17835 {
17836 bool byte_stride_p = (attr_byte_stride != nullptr);
17837 struct dynamic_prop *stride
17838 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17839
17840 range_type
17841 = create_range_type_with_stride (NULL, orig_base_type, &low,
17842 &high, bias, stride, byte_stride_p);
17843 }
17844 else
17845 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17846
17847 if (high_bound_is_count)
17848 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17849
17850 /* Ada expects an empty array on no boundary attributes. */
17851 if (attr == NULL && cu->language != language_ada)
17852 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17853
17854 name = dwarf2_name (die, cu);
17855 if (name)
17856 TYPE_NAME (range_type) = name;
17857
17858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17859 if (attr != nullptr)
17860 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17861
17862 maybe_set_alignment (cu, die, range_type);
17863
17864 set_die_type (die, range_type, cu);
17865
17866 /* set_die_type should be already done. */
17867 set_descriptive_type (range_type, die, cu);
17868
17869 return range_type;
17870 }
17871
17872 static struct type *
17873 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17874 {
17875 struct type *type;
17876
17877 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17878 NULL);
17879 TYPE_NAME (type) = dwarf2_name (die, cu);
17880
17881 /* In Ada, an unspecified type is typically used when the description
17882 of the type is deferred to a different unit. When encountering
17883 such a type, we treat it as a stub, and try to resolve it later on,
17884 when needed. */
17885 if (cu->language == language_ada)
17886 TYPE_STUB (type) = 1;
17887
17888 return set_die_type (die, type, cu);
17889 }
17890
17891 /* Read a single die and all its descendents. Set the die's sibling
17892 field to NULL; set other fields in the die correctly, and set all
17893 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17894 location of the info_ptr after reading all of those dies. PARENT
17895 is the parent of the die in question. */
17896
17897 static struct die_info *
17898 read_die_and_children (const struct die_reader_specs *reader,
17899 const gdb_byte *info_ptr,
17900 const gdb_byte **new_info_ptr,
17901 struct die_info *parent)
17902 {
17903 struct die_info *die;
17904 const gdb_byte *cur_ptr;
17905
17906 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17907 if (die == NULL)
17908 {
17909 *new_info_ptr = cur_ptr;
17910 return NULL;
17911 }
17912 store_in_ref_table (die, reader->cu);
17913
17914 if (die->has_children)
17915 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17916 else
17917 {
17918 die->child = NULL;
17919 *new_info_ptr = cur_ptr;
17920 }
17921
17922 die->sibling = NULL;
17923 die->parent = parent;
17924 return die;
17925 }
17926
17927 /* Read a die, all of its descendents, and all of its siblings; set
17928 all of the fields of all of the dies correctly. Arguments are as
17929 in read_die_and_children. */
17930
17931 static struct die_info *
17932 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17933 const gdb_byte *info_ptr,
17934 const gdb_byte **new_info_ptr,
17935 struct die_info *parent)
17936 {
17937 struct die_info *first_die, *last_sibling;
17938 const gdb_byte *cur_ptr;
17939
17940 cur_ptr = info_ptr;
17941 first_die = last_sibling = NULL;
17942
17943 while (1)
17944 {
17945 struct die_info *die
17946 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17947
17948 if (die == NULL)
17949 {
17950 *new_info_ptr = cur_ptr;
17951 return first_die;
17952 }
17953
17954 if (!first_die)
17955 first_die = die;
17956 else
17957 last_sibling->sibling = die;
17958
17959 last_sibling = die;
17960 }
17961 }
17962
17963 /* Read a die, all of its descendents, and all of its siblings; set
17964 all of the fields of all of the dies correctly. Arguments are as
17965 in read_die_and_children.
17966 This the main entry point for reading a DIE and all its children. */
17967
17968 static struct die_info *
17969 read_die_and_siblings (const struct die_reader_specs *reader,
17970 const gdb_byte *info_ptr,
17971 const gdb_byte **new_info_ptr,
17972 struct die_info *parent)
17973 {
17974 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17975 new_info_ptr, parent);
17976
17977 if (dwarf_die_debug)
17978 {
17979 fprintf_unfiltered (gdb_stdlog,
17980 "Read die from %s@0x%x of %s:\n",
17981 reader->die_section->get_name (),
17982 (unsigned) (info_ptr - reader->die_section->buffer),
17983 bfd_get_filename (reader->abfd));
17984 dump_die (die, dwarf_die_debug);
17985 }
17986
17987 return die;
17988 }
17989
17990 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17991 attributes.
17992 The caller is responsible for filling in the extra attributes
17993 and updating (*DIEP)->num_attrs.
17994 Set DIEP to point to a newly allocated die with its information,
17995 except for its child, sibling, and parent fields. */
17996
17997 static const gdb_byte *
17998 read_full_die_1 (const struct die_reader_specs *reader,
17999 struct die_info **diep, const gdb_byte *info_ptr,
18000 int num_extra_attrs)
18001 {
18002 unsigned int abbrev_number, bytes_read, i;
18003 struct abbrev_info *abbrev;
18004 struct die_info *die;
18005 struct dwarf2_cu *cu = reader->cu;
18006 bfd *abfd = reader->abfd;
18007
18008 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18009 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18010 info_ptr += bytes_read;
18011 if (!abbrev_number)
18012 {
18013 *diep = NULL;
18014 return info_ptr;
18015 }
18016
18017 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18018 if (!abbrev)
18019 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18020 abbrev_number,
18021 bfd_get_filename (abfd));
18022
18023 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18024 die->sect_off = sect_off;
18025 die->tag = abbrev->tag;
18026 die->abbrev = abbrev_number;
18027 die->has_children = abbrev->has_children;
18028
18029 /* Make the result usable.
18030 The caller needs to update num_attrs after adding the extra
18031 attributes. */
18032 die->num_attrs = abbrev->num_attrs;
18033
18034 std::vector<int> indexes_that_need_reprocess;
18035 for (i = 0; i < abbrev->num_attrs; ++i)
18036 {
18037 bool need_reprocess;
18038 info_ptr =
18039 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18040 info_ptr, &need_reprocess);
18041 if (need_reprocess)
18042 indexes_that_need_reprocess.push_back (i);
18043 }
18044
18045 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18046 if (attr != nullptr)
18047 cu->str_offsets_base = DW_UNSND (attr);
18048
18049 auto maybe_addr_base = lookup_addr_base(die);
18050 if (maybe_addr_base.has_value ())
18051 cu->addr_base = *maybe_addr_base;
18052 for (int index : indexes_that_need_reprocess)
18053 read_attribute_reprocess (reader, &die->attrs[index]);
18054 *diep = die;
18055 return info_ptr;
18056 }
18057
18058 /* Read a die and all its attributes.
18059 Set DIEP to point to a newly allocated die with its information,
18060 except for its child, sibling, and parent fields. */
18061
18062 static const gdb_byte *
18063 read_full_die (const struct die_reader_specs *reader,
18064 struct die_info **diep, const gdb_byte *info_ptr)
18065 {
18066 const gdb_byte *result;
18067
18068 result = read_full_die_1 (reader, diep, info_ptr, 0);
18069
18070 if (dwarf_die_debug)
18071 {
18072 fprintf_unfiltered (gdb_stdlog,
18073 "Read die from %s@0x%x of %s:\n",
18074 reader->die_section->get_name (),
18075 (unsigned) (info_ptr - reader->die_section->buffer),
18076 bfd_get_filename (reader->abfd));
18077 dump_die (*diep, dwarf_die_debug);
18078 }
18079
18080 return result;
18081 }
18082 \f
18083
18084 /* Returns nonzero if TAG represents a type that we might generate a partial
18085 symbol for. */
18086
18087 static int
18088 is_type_tag_for_partial (int tag)
18089 {
18090 switch (tag)
18091 {
18092 #if 0
18093 /* Some types that would be reasonable to generate partial symbols for,
18094 that we don't at present. */
18095 case DW_TAG_array_type:
18096 case DW_TAG_file_type:
18097 case DW_TAG_ptr_to_member_type:
18098 case DW_TAG_set_type:
18099 case DW_TAG_string_type:
18100 case DW_TAG_subroutine_type:
18101 #endif
18102 case DW_TAG_base_type:
18103 case DW_TAG_class_type:
18104 case DW_TAG_interface_type:
18105 case DW_TAG_enumeration_type:
18106 case DW_TAG_structure_type:
18107 case DW_TAG_subrange_type:
18108 case DW_TAG_typedef:
18109 case DW_TAG_union_type:
18110 return 1;
18111 default:
18112 return 0;
18113 }
18114 }
18115
18116 /* Load all DIEs that are interesting for partial symbols into memory. */
18117
18118 static struct partial_die_info *
18119 load_partial_dies (const struct die_reader_specs *reader,
18120 const gdb_byte *info_ptr, int building_psymtab)
18121 {
18122 struct dwarf2_cu *cu = reader->cu;
18123 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18124 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18125 unsigned int bytes_read;
18126 unsigned int load_all = 0;
18127 int nesting_level = 1;
18128
18129 parent_die = NULL;
18130 last_die = NULL;
18131
18132 gdb_assert (cu->per_cu != NULL);
18133 if (cu->per_cu->load_all_dies)
18134 load_all = 1;
18135
18136 cu->partial_dies
18137 = htab_create_alloc_ex (cu->header.length / 12,
18138 partial_die_hash,
18139 partial_die_eq,
18140 NULL,
18141 &cu->comp_unit_obstack,
18142 hashtab_obstack_allocate,
18143 dummy_obstack_deallocate);
18144
18145 while (1)
18146 {
18147 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18148
18149 /* A NULL abbrev means the end of a series of children. */
18150 if (abbrev == NULL)
18151 {
18152 if (--nesting_level == 0)
18153 return first_die;
18154
18155 info_ptr += bytes_read;
18156 last_die = parent_die;
18157 parent_die = parent_die->die_parent;
18158 continue;
18159 }
18160
18161 /* Check for template arguments. We never save these; if
18162 they're seen, we just mark the parent, and go on our way. */
18163 if (parent_die != NULL
18164 && cu->language == language_cplus
18165 && (abbrev->tag == DW_TAG_template_type_param
18166 || abbrev->tag == DW_TAG_template_value_param))
18167 {
18168 parent_die->has_template_arguments = 1;
18169
18170 if (!load_all)
18171 {
18172 /* We don't need a partial DIE for the template argument. */
18173 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18174 continue;
18175 }
18176 }
18177
18178 /* We only recurse into c++ subprograms looking for template arguments.
18179 Skip their other children. */
18180 if (!load_all
18181 && cu->language == language_cplus
18182 && parent_die != NULL
18183 && parent_die->tag == DW_TAG_subprogram)
18184 {
18185 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18186 continue;
18187 }
18188
18189 /* Check whether this DIE is interesting enough to save. Normally
18190 we would not be interested in members here, but there may be
18191 later variables referencing them via DW_AT_specification (for
18192 static members). */
18193 if (!load_all
18194 && !is_type_tag_for_partial (abbrev->tag)
18195 && abbrev->tag != DW_TAG_constant
18196 && abbrev->tag != DW_TAG_enumerator
18197 && abbrev->tag != DW_TAG_subprogram
18198 && abbrev->tag != DW_TAG_inlined_subroutine
18199 && abbrev->tag != DW_TAG_lexical_block
18200 && abbrev->tag != DW_TAG_variable
18201 && abbrev->tag != DW_TAG_namespace
18202 && abbrev->tag != DW_TAG_module
18203 && abbrev->tag != DW_TAG_member
18204 && abbrev->tag != DW_TAG_imported_unit
18205 && abbrev->tag != DW_TAG_imported_declaration)
18206 {
18207 /* Otherwise we skip to the next sibling, if any. */
18208 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18209 continue;
18210 }
18211
18212 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18213 abbrev);
18214
18215 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18216
18217 /* This two-pass algorithm for processing partial symbols has a
18218 high cost in cache pressure. Thus, handle some simple cases
18219 here which cover the majority of C partial symbols. DIEs
18220 which neither have specification tags in them, nor could have
18221 specification tags elsewhere pointing at them, can simply be
18222 processed and discarded.
18223
18224 This segment is also optional; scan_partial_symbols and
18225 add_partial_symbol will handle these DIEs if we chain
18226 them in normally. When compilers which do not emit large
18227 quantities of duplicate debug information are more common,
18228 this code can probably be removed. */
18229
18230 /* Any complete simple types at the top level (pretty much all
18231 of them, for a language without namespaces), can be processed
18232 directly. */
18233 if (parent_die == NULL
18234 && pdi.has_specification == 0
18235 && pdi.is_declaration == 0
18236 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18237 || pdi.tag == DW_TAG_base_type
18238 || pdi.tag == DW_TAG_subrange_type))
18239 {
18240 if (building_psymtab && pdi.name != NULL)
18241 add_psymbol_to_list (pdi.name, false,
18242 VAR_DOMAIN, LOC_TYPEDEF, -1,
18243 psymbol_placement::STATIC,
18244 0, cu->language, objfile);
18245 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18246 continue;
18247 }
18248
18249 /* The exception for DW_TAG_typedef with has_children above is
18250 a workaround of GCC PR debug/47510. In the case of this complaint
18251 type_name_or_error will error on such types later.
18252
18253 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18254 it could not find the child DIEs referenced later, this is checked
18255 above. In correct DWARF DW_TAG_typedef should have no children. */
18256
18257 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18258 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18259 "- DIE at %s [in module %s]"),
18260 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18261
18262 /* If we're at the second level, and we're an enumerator, and
18263 our parent has no specification (meaning possibly lives in a
18264 namespace elsewhere), then we can add the partial symbol now
18265 instead of queueing it. */
18266 if (pdi.tag == DW_TAG_enumerator
18267 && parent_die != NULL
18268 && parent_die->die_parent == NULL
18269 && parent_die->tag == DW_TAG_enumeration_type
18270 && parent_die->has_specification == 0)
18271 {
18272 if (pdi.name == NULL)
18273 complaint (_("malformed enumerator DIE ignored"));
18274 else if (building_psymtab)
18275 add_psymbol_to_list (pdi.name, false,
18276 VAR_DOMAIN, LOC_CONST, -1,
18277 cu->language == language_cplus
18278 ? psymbol_placement::GLOBAL
18279 : psymbol_placement::STATIC,
18280 0, cu->language, objfile);
18281
18282 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18283 continue;
18284 }
18285
18286 struct partial_die_info *part_die
18287 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18288
18289 /* We'll save this DIE so link it in. */
18290 part_die->die_parent = parent_die;
18291 part_die->die_sibling = NULL;
18292 part_die->die_child = NULL;
18293
18294 if (last_die && last_die == parent_die)
18295 last_die->die_child = part_die;
18296 else if (last_die)
18297 last_die->die_sibling = part_die;
18298
18299 last_die = part_die;
18300
18301 if (first_die == NULL)
18302 first_die = part_die;
18303
18304 /* Maybe add the DIE to the hash table. Not all DIEs that we
18305 find interesting need to be in the hash table, because we
18306 also have the parent/sibling/child chains; only those that we
18307 might refer to by offset later during partial symbol reading.
18308
18309 For now this means things that might have be the target of a
18310 DW_AT_specification, DW_AT_abstract_origin, or
18311 DW_AT_extension. DW_AT_extension will refer only to
18312 namespaces; DW_AT_abstract_origin refers to functions (and
18313 many things under the function DIE, but we do not recurse
18314 into function DIEs during partial symbol reading) and
18315 possibly variables as well; DW_AT_specification refers to
18316 declarations. Declarations ought to have the DW_AT_declaration
18317 flag. It happens that GCC forgets to put it in sometimes, but
18318 only for functions, not for types.
18319
18320 Adding more things than necessary to the hash table is harmless
18321 except for the performance cost. Adding too few will result in
18322 wasted time in find_partial_die, when we reread the compilation
18323 unit with load_all_dies set. */
18324
18325 if (load_all
18326 || abbrev->tag == DW_TAG_constant
18327 || abbrev->tag == DW_TAG_subprogram
18328 || abbrev->tag == DW_TAG_variable
18329 || abbrev->tag == DW_TAG_namespace
18330 || part_die->is_declaration)
18331 {
18332 void **slot;
18333
18334 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18335 to_underlying (part_die->sect_off),
18336 INSERT);
18337 *slot = part_die;
18338 }
18339
18340 /* For some DIEs we want to follow their children (if any). For C
18341 we have no reason to follow the children of structures; for other
18342 languages we have to, so that we can get at method physnames
18343 to infer fully qualified class names, for DW_AT_specification,
18344 and for C++ template arguments. For C++, we also look one level
18345 inside functions to find template arguments (if the name of the
18346 function does not already contain the template arguments).
18347
18348 For Ada and Fortran, we need to scan the children of subprograms
18349 and lexical blocks as well because these languages allow the
18350 definition of nested entities that could be interesting for the
18351 debugger, such as nested subprograms for instance. */
18352 if (last_die->has_children
18353 && (load_all
18354 || last_die->tag == DW_TAG_namespace
18355 || last_die->tag == DW_TAG_module
18356 || last_die->tag == DW_TAG_enumeration_type
18357 || (cu->language == language_cplus
18358 && last_die->tag == DW_TAG_subprogram
18359 && (last_die->name == NULL
18360 || strchr (last_die->name, '<') == NULL))
18361 || (cu->language != language_c
18362 && (last_die->tag == DW_TAG_class_type
18363 || last_die->tag == DW_TAG_interface_type
18364 || last_die->tag == DW_TAG_structure_type
18365 || last_die->tag == DW_TAG_union_type))
18366 || ((cu->language == language_ada
18367 || cu->language == language_fortran)
18368 && (last_die->tag == DW_TAG_subprogram
18369 || last_die->tag == DW_TAG_lexical_block))))
18370 {
18371 nesting_level++;
18372 parent_die = last_die;
18373 continue;
18374 }
18375
18376 /* Otherwise we skip to the next sibling, if any. */
18377 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18378
18379 /* Back to the top, do it again. */
18380 }
18381 }
18382
18383 partial_die_info::partial_die_info (sect_offset sect_off_,
18384 struct abbrev_info *abbrev)
18385 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18386 {
18387 }
18388
18389 /* Read a minimal amount of information into the minimal die structure.
18390 INFO_PTR should point just after the initial uleb128 of a DIE. */
18391
18392 const gdb_byte *
18393 partial_die_info::read (const struct die_reader_specs *reader,
18394 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18395 {
18396 struct dwarf2_cu *cu = reader->cu;
18397 struct dwarf2_per_objfile *dwarf2_per_objfile
18398 = cu->per_cu->dwarf2_per_objfile;
18399 unsigned int i;
18400 int has_low_pc_attr = 0;
18401 int has_high_pc_attr = 0;
18402 int high_pc_relative = 0;
18403
18404 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18405 for (i = 0; i < abbrev.num_attrs; ++i)
18406 {
18407 bool need_reprocess;
18408 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18409 info_ptr, &need_reprocess);
18410 /* String and address offsets that need to do the reprocessing have
18411 already been read at this point, so there is no need to wait until
18412 the loop terminates to do the reprocessing. */
18413 if (need_reprocess)
18414 read_attribute_reprocess (reader, &attr_vec[i]);
18415 attribute &attr = attr_vec[i];
18416 /* Store the data if it is of an attribute we want to keep in a
18417 partial symbol table. */
18418 switch (attr.name)
18419 {
18420 case DW_AT_name:
18421 switch (tag)
18422 {
18423 case DW_TAG_compile_unit:
18424 case DW_TAG_partial_unit:
18425 case DW_TAG_type_unit:
18426 /* Compilation units have a DW_AT_name that is a filename, not
18427 a source language identifier. */
18428 case DW_TAG_enumeration_type:
18429 case DW_TAG_enumerator:
18430 /* These tags always have simple identifiers already; no need
18431 to canonicalize them. */
18432 name = DW_STRING (&attr);
18433 break;
18434 default:
18435 {
18436 struct objfile *objfile = dwarf2_per_objfile->objfile;
18437
18438 name
18439 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18440 &objfile->per_bfd->storage_obstack);
18441 }
18442 break;
18443 }
18444 break;
18445 case DW_AT_linkage_name:
18446 case DW_AT_MIPS_linkage_name:
18447 /* Note that both forms of linkage name might appear. We
18448 assume they will be the same, and we only store the last
18449 one we see. */
18450 linkage_name = DW_STRING (&attr);
18451 break;
18452 case DW_AT_low_pc:
18453 has_low_pc_attr = 1;
18454 lowpc = attr.value_as_address ();
18455 break;
18456 case DW_AT_high_pc:
18457 has_high_pc_attr = 1;
18458 highpc = attr.value_as_address ();
18459 if (cu->header.version >= 4 && attr.form_is_constant ())
18460 high_pc_relative = 1;
18461 break;
18462 case DW_AT_location:
18463 /* Support the .debug_loc offsets. */
18464 if (attr.form_is_block ())
18465 {
18466 d.locdesc = DW_BLOCK (&attr);
18467 }
18468 else if (attr.form_is_section_offset ())
18469 {
18470 dwarf2_complex_location_expr_complaint ();
18471 }
18472 else
18473 {
18474 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18475 "partial symbol information");
18476 }
18477 break;
18478 case DW_AT_external:
18479 is_external = DW_UNSND (&attr);
18480 break;
18481 case DW_AT_declaration:
18482 is_declaration = DW_UNSND (&attr);
18483 break;
18484 case DW_AT_type:
18485 has_type = 1;
18486 break;
18487 case DW_AT_abstract_origin:
18488 case DW_AT_specification:
18489 case DW_AT_extension:
18490 has_specification = 1;
18491 spec_offset = dwarf2_get_ref_die_offset (&attr);
18492 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18493 || cu->per_cu->is_dwz);
18494 break;
18495 case DW_AT_sibling:
18496 /* Ignore absolute siblings, they might point outside of
18497 the current compile unit. */
18498 if (attr.form == DW_FORM_ref_addr)
18499 complaint (_("ignoring absolute DW_AT_sibling"));
18500 else
18501 {
18502 const gdb_byte *buffer = reader->buffer;
18503 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18504 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18505
18506 if (sibling_ptr < info_ptr)
18507 complaint (_("DW_AT_sibling points backwards"));
18508 else if (sibling_ptr > reader->buffer_end)
18509 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18510 else
18511 sibling = sibling_ptr;
18512 }
18513 break;
18514 case DW_AT_byte_size:
18515 has_byte_size = 1;
18516 break;
18517 case DW_AT_const_value:
18518 has_const_value = 1;
18519 break;
18520 case DW_AT_calling_convention:
18521 /* DWARF doesn't provide a way to identify a program's source-level
18522 entry point. DW_AT_calling_convention attributes are only meant
18523 to describe functions' calling conventions.
18524
18525 However, because it's a necessary piece of information in
18526 Fortran, and before DWARF 4 DW_CC_program was the only
18527 piece of debugging information whose definition refers to
18528 a 'main program' at all, several compilers marked Fortran
18529 main programs with DW_CC_program --- even when those
18530 functions use the standard calling conventions.
18531
18532 Although DWARF now specifies a way to provide this
18533 information, we support this practice for backward
18534 compatibility. */
18535 if (DW_UNSND (&attr) == DW_CC_program
18536 && cu->language == language_fortran)
18537 main_subprogram = 1;
18538 break;
18539 case DW_AT_inline:
18540 if (DW_UNSND (&attr) == DW_INL_inlined
18541 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18542 may_be_inlined = 1;
18543 break;
18544
18545 case DW_AT_import:
18546 if (tag == DW_TAG_imported_unit)
18547 {
18548 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18549 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18550 || cu->per_cu->is_dwz);
18551 }
18552 break;
18553
18554 case DW_AT_main_subprogram:
18555 main_subprogram = DW_UNSND (&attr);
18556 break;
18557
18558 case DW_AT_ranges:
18559 {
18560 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18561 but that requires a full DIE, so instead we just
18562 reimplement it. */
18563 int need_ranges_base = tag != DW_TAG_compile_unit;
18564 unsigned int ranges_offset = (DW_UNSND (&attr)
18565 + (need_ranges_base
18566 ? cu->ranges_base
18567 : 0));
18568
18569 /* Value of the DW_AT_ranges attribute is the offset in the
18570 .debug_ranges section. */
18571 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18572 nullptr))
18573 has_pc_info = 1;
18574 }
18575 break;
18576
18577 default:
18578 break;
18579 }
18580 }
18581
18582 /* For Ada, if both the name and the linkage name appear, we prefer
18583 the latter. This lets "catch exception" work better, regardless
18584 of the order in which the name and linkage name were emitted.
18585 Really, though, this is just a workaround for the fact that gdb
18586 doesn't store both the name and the linkage name. */
18587 if (cu->language == language_ada && linkage_name != nullptr)
18588 name = linkage_name;
18589
18590 if (high_pc_relative)
18591 highpc += lowpc;
18592
18593 if (has_low_pc_attr && has_high_pc_attr)
18594 {
18595 /* When using the GNU linker, .gnu.linkonce. sections are used to
18596 eliminate duplicate copies of functions and vtables and such.
18597 The linker will arbitrarily choose one and discard the others.
18598 The AT_*_pc values for such functions refer to local labels in
18599 these sections. If the section from that file was discarded, the
18600 labels are not in the output, so the relocs get a value of 0.
18601 If this is a discarded function, mark the pc bounds as invalid,
18602 so that GDB will ignore it. */
18603 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18604 {
18605 struct objfile *objfile = dwarf2_per_objfile->objfile;
18606 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18607
18608 complaint (_("DW_AT_low_pc %s is zero "
18609 "for DIE at %s [in module %s]"),
18610 paddress (gdbarch, lowpc),
18611 sect_offset_str (sect_off),
18612 objfile_name (objfile));
18613 }
18614 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18615 else if (lowpc >= highpc)
18616 {
18617 struct objfile *objfile = dwarf2_per_objfile->objfile;
18618 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18619
18620 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18621 "for DIE at %s [in module %s]"),
18622 paddress (gdbarch, lowpc),
18623 paddress (gdbarch, highpc),
18624 sect_offset_str (sect_off),
18625 objfile_name (objfile));
18626 }
18627 else
18628 has_pc_info = 1;
18629 }
18630
18631 return info_ptr;
18632 }
18633
18634 /* Find a cached partial DIE at OFFSET in CU. */
18635
18636 struct partial_die_info *
18637 dwarf2_cu::find_partial_die (sect_offset sect_off)
18638 {
18639 struct partial_die_info *lookup_die = NULL;
18640 struct partial_die_info part_die (sect_off);
18641
18642 lookup_die = ((struct partial_die_info *)
18643 htab_find_with_hash (partial_dies, &part_die,
18644 to_underlying (sect_off)));
18645
18646 return lookup_die;
18647 }
18648
18649 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18650 except in the case of .debug_types DIEs which do not reference
18651 outside their CU (they do however referencing other types via
18652 DW_FORM_ref_sig8). */
18653
18654 static const struct cu_partial_die_info
18655 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18656 {
18657 struct dwarf2_per_objfile *dwarf2_per_objfile
18658 = cu->per_cu->dwarf2_per_objfile;
18659 struct objfile *objfile = dwarf2_per_objfile->objfile;
18660 struct dwarf2_per_cu_data *per_cu = NULL;
18661 struct partial_die_info *pd = NULL;
18662
18663 if (offset_in_dwz == cu->per_cu->is_dwz
18664 && offset_in_cu_p (&cu->header, sect_off))
18665 {
18666 pd = cu->find_partial_die (sect_off);
18667 if (pd != NULL)
18668 return { cu, pd };
18669 /* We missed recording what we needed.
18670 Load all dies and try again. */
18671 per_cu = cu->per_cu;
18672 }
18673 else
18674 {
18675 /* TUs don't reference other CUs/TUs (except via type signatures). */
18676 if (cu->per_cu->is_debug_types)
18677 {
18678 error (_("Dwarf Error: Type Unit at offset %s contains"
18679 " external reference to offset %s [in module %s].\n"),
18680 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18681 bfd_get_filename (objfile->obfd));
18682 }
18683 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18684 dwarf2_per_objfile);
18685
18686 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18687 load_partial_comp_unit (per_cu);
18688
18689 per_cu->cu->last_used = 0;
18690 pd = per_cu->cu->find_partial_die (sect_off);
18691 }
18692
18693 /* If we didn't find it, and not all dies have been loaded,
18694 load them all and try again. */
18695
18696 if (pd == NULL && per_cu->load_all_dies == 0)
18697 {
18698 per_cu->load_all_dies = 1;
18699
18700 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18701 THIS_CU->cu may already be in use. So we can't just free it and
18702 replace its DIEs with the ones we read in. Instead, we leave those
18703 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18704 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18705 set. */
18706 load_partial_comp_unit (per_cu);
18707
18708 pd = per_cu->cu->find_partial_die (sect_off);
18709 }
18710
18711 if (pd == NULL)
18712 internal_error (__FILE__, __LINE__,
18713 _("could not find partial DIE %s "
18714 "in cache [from module %s]\n"),
18715 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18716 return { per_cu->cu, pd };
18717 }
18718
18719 /* See if we can figure out if the class lives in a namespace. We do
18720 this by looking for a member function; its demangled name will
18721 contain namespace info, if there is any. */
18722
18723 static void
18724 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18725 struct dwarf2_cu *cu)
18726 {
18727 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18728 what template types look like, because the demangler
18729 frequently doesn't give the same name as the debug info. We
18730 could fix this by only using the demangled name to get the
18731 prefix (but see comment in read_structure_type). */
18732
18733 struct partial_die_info *real_pdi;
18734 struct partial_die_info *child_pdi;
18735
18736 /* If this DIE (this DIE's specification, if any) has a parent, then
18737 we should not do this. We'll prepend the parent's fully qualified
18738 name when we create the partial symbol. */
18739
18740 real_pdi = struct_pdi;
18741 while (real_pdi->has_specification)
18742 {
18743 auto res = find_partial_die (real_pdi->spec_offset,
18744 real_pdi->spec_is_dwz, cu);
18745 real_pdi = res.pdi;
18746 cu = res.cu;
18747 }
18748
18749 if (real_pdi->die_parent != NULL)
18750 return;
18751
18752 for (child_pdi = struct_pdi->die_child;
18753 child_pdi != NULL;
18754 child_pdi = child_pdi->die_sibling)
18755 {
18756 if (child_pdi->tag == DW_TAG_subprogram
18757 && child_pdi->linkage_name != NULL)
18758 {
18759 gdb::unique_xmalloc_ptr<char> actual_class_name
18760 (language_class_name_from_physname (cu->language_defn,
18761 child_pdi->linkage_name));
18762 if (actual_class_name != NULL)
18763 {
18764 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18765 struct_pdi->name
18766 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18767 actual_class_name.get ());
18768 }
18769 break;
18770 }
18771 }
18772 }
18773
18774 void
18775 partial_die_info::fixup (struct dwarf2_cu *cu)
18776 {
18777 /* Once we've fixed up a die, there's no point in doing so again.
18778 This also avoids a memory leak if we were to call
18779 guess_partial_die_structure_name multiple times. */
18780 if (fixup_called)
18781 return;
18782
18783 /* If we found a reference attribute and the DIE has no name, try
18784 to find a name in the referred to DIE. */
18785
18786 if (name == NULL && has_specification)
18787 {
18788 struct partial_die_info *spec_die;
18789
18790 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18791 spec_die = res.pdi;
18792 cu = res.cu;
18793
18794 spec_die->fixup (cu);
18795
18796 if (spec_die->name)
18797 {
18798 name = spec_die->name;
18799
18800 /* Copy DW_AT_external attribute if it is set. */
18801 if (spec_die->is_external)
18802 is_external = spec_die->is_external;
18803 }
18804 }
18805
18806 /* Set default names for some unnamed DIEs. */
18807
18808 if (name == NULL && tag == DW_TAG_namespace)
18809 name = CP_ANONYMOUS_NAMESPACE_STR;
18810
18811 /* If there is no parent die to provide a namespace, and there are
18812 children, see if we can determine the namespace from their linkage
18813 name. */
18814 if (cu->language == language_cplus
18815 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18816 && die_parent == NULL
18817 && has_children
18818 && (tag == DW_TAG_class_type
18819 || tag == DW_TAG_structure_type
18820 || tag == DW_TAG_union_type))
18821 guess_partial_die_structure_name (this, cu);
18822
18823 /* GCC might emit a nameless struct or union that has a linkage
18824 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18825 if (name == NULL
18826 && (tag == DW_TAG_class_type
18827 || tag == DW_TAG_interface_type
18828 || tag == DW_TAG_structure_type
18829 || tag == DW_TAG_union_type)
18830 && linkage_name != NULL)
18831 {
18832 gdb::unique_xmalloc_ptr<char> demangled
18833 (gdb_demangle (linkage_name, DMGL_TYPES));
18834 if (demangled != nullptr)
18835 {
18836 const char *base;
18837
18838 /* Strip any leading namespaces/classes, keep only the base name.
18839 DW_AT_name for named DIEs does not contain the prefixes. */
18840 base = strrchr (demangled.get (), ':');
18841 if (base && base > demangled.get () && base[-1] == ':')
18842 base++;
18843 else
18844 base = demangled.get ();
18845
18846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18847 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18848 }
18849 }
18850
18851 fixup_called = 1;
18852 }
18853
18854 /* Process the attributes that had to be skipped in the first round. These
18855 attributes are the ones that need str_offsets_base or addr_base attributes.
18856 They could not have been processed in the first round, because at the time
18857 the values of str_offsets_base or addr_base may not have been known. */
18858 void read_attribute_reprocess (const struct die_reader_specs *reader,
18859 struct attribute *attr)
18860 {
18861 struct dwarf2_cu *cu = reader->cu;
18862 switch (attr->form)
18863 {
18864 case DW_FORM_addrx:
18865 case DW_FORM_GNU_addr_index:
18866 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18867 break;
18868 case DW_FORM_strx:
18869 case DW_FORM_strx1:
18870 case DW_FORM_strx2:
18871 case DW_FORM_strx3:
18872 case DW_FORM_strx4:
18873 case DW_FORM_GNU_str_index:
18874 {
18875 unsigned int str_index = DW_UNSND (attr);
18876 if (reader->dwo_file != NULL)
18877 {
18878 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18879 DW_STRING_IS_CANONICAL (attr) = 0;
18880 }
18881 else
18882 {
18883 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18884 DW_STRING_IS_CANONICAL (attr) = 0;
18885 }
18886 break;
18887 }
18888 default:
18889 gdb_assert_not_reached (_("Unexpected DWARF form."));
18890 }
18891 }
18892
18893 /* Read an attribute value described by an attribute form. */
18894
18895 static const gdb_byte *
18896 read_attribute_value (const struct die_reader_specs *reader,
18897 struct attribute *attr, unsigned form,
18898 LONGEST implicit_const, const gdb_byte *info_ptr,
18899 bool *need_reprocess)
18900 {
18901 struct dwarf2_cu *cu = reader->cu;
18902 struct dwarf2_per_objfile *dwarf2_per_objfile
18903 = cu->per_cu->dwarf2_per_objfile;
18904 struct objfile *objfile = dwarf2_per_objfile->objfile;
18905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18906 bfd *abfd = reader->abfd;
18907 struct comp_unit_head *cu_header = &cu->header;
18908 unsigned int bytes_read;
18909 struct dwarf_block *blk;
18910 *need_reprocess = false;
18911
18912 attr->form = (enum dwarf_form) form;
18913 switch (form)
18914 {
18915 case DW_FORM_ref_addr:
18916 if (cu->header.version == 2)
18917 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18918 else
18919 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18920 &cu->header, &bytes_read);
18921 info_ptr += bytes_read;
18922 break;
18923 case DW_FORM_GNU_ref_alt:
18924 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18925 info_ptr += bytes_read;
18926 break;
18927 case DW_FORM_addr:
18928 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18929 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18930 info_ptr += bytes_read;
18931 break;
18932 case DW_FORM_block2:
18933 blk = dwarf_alloc_block (cu);
18934 blk->size = read_2_bytes (abfd, info_ptr);
18935 info_ptr += 2;
18936 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18937 info_ptr += blk->size;
18938 DW_BLOCK (attr) = blk;
18939 break;
18940 case DW_FORM_block4:
18941 blk = dwarf_alloc_block (cu);
18942 blk->size = read_4_bytes (abfd, info_ptr);
18943 info_ptr += 4;
18944 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18945 info_ptr += blk->size;
18946 DW_BLOCK (attr) = blk;
18947 break;
18948 case DW_FORM_data2:
18949 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18950 info_ptr += 2;
18951 break;
18952 case DW_FORM_data4:
18953 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18954 info_ptr += 4;
18955 break;
18956 case DW_FORM_data8:
18957 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18958 info_ptr += 8;
18959 break;
18960 case DW_FORM_data16:
18961 blk = dwarf_alloc_block (cu);
18962 blk->size = 16;
18963 blk->data = read_n_bytes (abfd, info_ptr, 16);
18964 info_ptr += 16;
18965 DW_BLOCK (attr) = blk;
18966 break;
18967 case DW_FORM_sec_offset:
18968 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18969 info_ptr += bytes_read;
18970 break;
18971 case DW_FORM_string:
18972 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18973 DW_STRING_IS_CANONICAL (attr) = 0;
18974 info_ptr += bytes_read;
18975 break;
18976 case DW_FORM_strp:
18977 if (!cu->per_cu->is_dwz)
18978 {
18979 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18980 abfd, info_ptr, cu_header,
18981 &bytes_read);
18982 DW_STRING_IS_CANONICAL (attr) = 0;
18983 info_ptr += bytes_read;
18984 break;
18985 }
18986 /* FALLTHROUGH */
18987 case DW_FORM_line_strp:
18988 if (!cu->per_cu->is_dwz)
18989 {
18990 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18991 abfd, info_ptr,
18992 cu_header, &bytes_read);
18993 DW_STRING_IS_CANONICAL (attr) = 0;
18994 info_ptr += bytes_read;
18995 break;
18996 }
18997 /* FALLTHROUGH */
18998 case DW_FORM_GNU_strp_alt:
18999 {
19000 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19001 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19002 &bytes_read);
19003
19004 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19005 dwz, str_offset);
19006 DW_STRING_IS_CANONICAL (attr) = 0;
19007 info_ptr += bytes_read;
19008 }
19009 break;
19010 case DW_FORM_exprloc:
19011 case DW_FORM_block:
19012 blk = dwarf_alloc_block (cu);
19013 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19014 info_ptr += bytes_read;
19015 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19016 info_ptr += blk->size;
19017 DW_BLOCK (attr) = blk;
19018 break;
19019 case DW_FORM_block1:
19020 blk = dwarf_alloc_block (cu);
19021 blk->size = read_1_byte (abfd, info_ptr);
19022 info_ptr += 1;
19023 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19024 info_ptr += blk->size;
19025 DW_BLOCK (attr) = blk;
19026 break;
19027 case DW_FORM_data1:
19028 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19029 info_ptr += 1;
19030 break;
19031 case DW_FORM_flag:
19032 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19033 info_ptr += 1;
19034 break;
19035 case DW_FORM_flag_present:
19036 DW_UNSND (attr) = 1;
19037 break;
19038 case DW_FORM_sdata:
19039 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19040 info_ptr += bytes_read;
19041 break;
19042 case DW_FORM_udata:
19043 case DW_FORM_rnglistx:
19044 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19045 info_ptr += bytes_read;
19046 break;
19047 case DW_FORM_ref1:
19048 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19049 + read_1_byte (abfd, info_ptr));
19050 info_ptr += 1;
19051 break;
19052 case DW_FORM_ref2:
19053 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19054 + read_2_bytes (abfd, info_ptr));
19055 info_ptr += 2;
19056 break;
19057 case DW_FORM_ref4:
19058 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19059 + read_4_bytes (abfd, info_ptr));
19060 info_ptr += 4;
19061 break;
19062 case DW_FORM_ref8:
19063 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19064 + read_8_bytes (abfd, info_ptr));
19065 info_ptr += 8;
19066 break;
19067 case DW_FORM_ref_sig8:
19068 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19069 info_ptr += 8;
19070 break;
19071 case DW_FORM_ref_udata:
19072 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19073 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19074 info_ptr += bytes_read;
19075 break;
19076 case DW_FORM_indirect:
19077 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19078 info_ptr += bytes_read;
19079 if (form == DW_FORM_implicit_const)
19080 {
19081 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19082 info_ptr += bytes_read;
19083 }
19084 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19085 info_ptr, need_reprocess);
19086 break;
19087 case DW_FORM_implicit_const:
19088 DW_SND (attr) = implicit_const;
19089 break;
19090 case DW_FORM_addrx:
19091 case DW_FORM_GNU_addr_index:
19092 *need_reprocess = true;
19093 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19094 info_ptr += bytes_read;
19095 break;
19096 case DW_FORM_strx:
19097 case DW_FORM_strx1:
19098 case DW_FORM_strx2:
19099 case DW_FORM_strx3:
19100 case DW_FORM_strx4:
19101 case DW_FORM_GNU_str_index:
19102 {
19103 ULONGEST str_index;
19104 if (form == DW_FORM_strx1)
19105 {
19106 str_index = read_1_byte (abfd, info_ptr);
19107 info_ptr += 1;
19108 }
19109 else if (form == DW_FORM_strx2)
19110 {
19111 str_index = read_2_bytes (abfd, info_ptr);
19112 info_ptr += 2;
19113 }
19114 else if (form == DW_FORM_strx3)
19115 {
19116 str_index = read_3_bytes (abfd, info_ptr);
19117 info_ptr += 3;
19118 }
19119 else if (form == DW_FORM_strx4)
19120 {
19121 str_index = read_4_bytes (abfd, info_ptr);
19122 info_ptr += 4;
19123 }
19124 else
19125 {
19126 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19127 info_ptr += bytes_read;
19128 }
19129 *need_reprocess = true;
19130 DW_UNSND (attr) = str_index;
19131 }
19132 break;
19133 default:
19134 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19135 dwarf_form_name (form),
19136 bfd_get_filename (abfd));
19137 }
19138
19139 /* Super hack. */
19140 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19141 attr->form = DW_FORM_GNU_ref_alt;
19142
19143 /* We have seen instances where the compiler tried to emit a byte
19144 size attribute of -1 which ended up being encoded as an unsigned
19145 0xffffffff. Although 0xffffffff is technically a valid size value,
19146 an object of this size seems pretty unlikely so we can relatively
19147 safely treat these cases as if the size attribute was invalid and
19148 treat them as zero by default. */
19149 if (attr->name == DW_AT_byte_size
19150 && form == DW_FORM_data4
19151 && DW_UNSND (attr) >= 0xffffffff)
19152 {
19153 complaint
19154 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19155 hex_string (DW_UNSND (attr)));
19156 DW_UNSND (attr) = 0;
19157 }
19158
19159 return info_ptr;
19160 }
19161
19162 /* Read an attribute described by an abbreviated attribute. */
19163
19164 static const gdb_byte *
19165 read_attribute (const struct die_reader_specs *reader,
19166 struct attribute *attr, struct attr_abbrev *abbrev,
19167 const gdb_byte *info_ptr, bool *need_reprocess)
19168 {
19169 attr->name = abbrev->name;
19170 return read_attribute_value (reader, attr, abbrev->form,
19171 abbrev->implicit_const, info_ptr,
19172 need_reprocess);
19173 }
19174
19175 static CORE_ADDR
19176 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19177 unsigned int *bytes_read)
19178 {
19179 struct comp_unit_head *cu_header = &cu->header;
19180 CORE_ADDR retval = 0;
19181
19182 if (cu_header->signed_addr_p)
19183 {
19184 switch (cu_header->addr_size)
19185 {
19186 case 2:
19187 retval = bfd_get_signed_16 (abfd, buf);
19188 break;
19189 case 4:
19190 retval = bfd_get_signed_32 (abfd, buf);
19191 break;
19192 case 8:
19193 retval = bfd_get_signed_64 (abfd, buf);
19194 break;
19195 default:
19196 internal_error (__FILE__, __LINE__,
19197 _("read_address: bad switch, signed [in module %s]"),
19198 bfd_get_filename (abfd));
19199 }
19200 }
19201 else
19202 {
19203 switch (cu_header->addr_size)
19204 {
19205 case 2:
19206 retval = bfd_get_16 (abfd, buf);
19207 break;
19208 case 4:
19209 retval = bfd_get_32 (abfd, buf);
19210 break;
19211 case 8:
19212 retval = bfd_get_64 (abfd, buf);
19213 break;
19214 default:
19215 internal_error (__FILE__, __LINE__,
19216 _("read_address: bad switch, "
19217 "unsigned [in module %s]"),
19218 bfd_get_filename (abfd));
19219 }
19220 }
19221
19222 *bytes_read = cu_header->addr_size;
19223 return retval;
19224 }
19225
19226 /* Read the initial length from a section. The (draft) DWARF 3
19227 specification allows the initial length to take up either 4 bytes
19228 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19229 bytes describe the length and all offsets will be 8 bytes in length
19230 instead of 4.
19231
19232 An older, non-standard 64-bit format is also handled by this
19233 function. The older format in question stores the initial length
19234 as an 8-byte quantity without an escape value. Lengths greater
19235 than 2^32 aren't very common which means that the initial 4 bytes
19236 is almost always zero. Since a length value of zero doesn't make
19237 sense for the 32-bit format, this initial zero can be considered to
19238 be an escape value which indicates the presence of the older 64-bit
19239 format. As written, the code can't detect (old format) lengths
19240 greater than 4GB. If it becomes necessary to handle lengths
19241 somewhat larger than 4GB, we could allow other small values (such
19242 as the non-sensical values of 1, 2, and 3) to also be used as
19243 escape values indicating the presence of the old format.
19244
19245 The value returned via bytes_read should be used to increment the
19246 relevant pointer after calling read_initial_length().
19247
19248 [ Note: read_initial_length() and read_offset() are based on the
19249 document entitled "DWARF Debugging Information Format", revision
19250 3, draft 8, dated November 19, 2001. This document was obtained
19251 from:
19252
19253 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19254
19255 This document is only a draft and is subject to change. (So beware.)
19256
19257 Details regarding the older, non-standard 64-bit format were
19258 determined empirically by examining 64-bit ELF files produced by
19259 the SGI toolchain on an IRIX 6.5 machine.
19260
19261 - Kevin, July 16, 2002
19262 ] */
19263
19264 static LONGEST
19265 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19266 {
19267 LONGEST length = bfd_get_32 (abfd, buf);
19268
19269 if (length == 0xffffffff)
19270 {
19271 length = bfd_get_64 (abfd, buf + 4);
19272 *bytes_read = 12;
19273 }
19274 else if (length == 0)
19275 {
19276 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19277 length = bfd_get_64 (abfd, buf);
19278 *bytes_read = 8;
19279 }
19280 else
19281 {
19282 *bytes_read = 4;
19283 }
19284
19285 return length;
19286 }
19287
19288 /* Cover function for read_initial_length.
19289 Returns the length of the object at BUF, and stores the size of the
19290 initial length in *BYTES_READ and stores the size that offsets will be in
19291 *OFFSET_SIZE.
19292 If the initial length size is not equivalent to that specified in
19293 CU_HEADER then issue a complaint.
19294 This is useful when reading non-comp-unit headers. */
19295
19296 static LONGEST
19297 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19298 const struct comp_unit_head *cu_header,
19299 unsigned int *bytes_read,
19300 unsigned int *offset_size)
19301 {
19302 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19303
19304 gdb_assert (cu_header->initial_length_size == 4
19305 || cu_header->initial_length_size == 8
19306 || cu_header->initial_length_size == 12);
19307
19308 if (cu_header->initial_length_size != *bytes_read)
19309 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19310
19311 *offset_size = (*bytes_read == 4) ? 4 : 8;
19312 return length;
19313 }
19314
19315 /* Read an offset from the data stream. The size of the offset is
19316 given by cu_header->offset_size. */
19317
19318 static LONGEST
19319 read_offset (bfd *abfd, const gdb_byte *buf,
19320 const struct comp_unit_head *cu_header,
19321 unsigned int *bytes_read)
19322 {
19323 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19324
19325 *bytes_read = cu_header->offset_size;
19326 return offset;
19327 }
19328
19329 /* Read an offset from the data stream. */
19330
19331 static LONGEST
19332 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19333 {
19334 LONGEST retval = 0;
19335
19336 switch (offset_size)
19337 {
19338 case 4:
19339 retval = bfd_get_32 (abfd, buf);
19340 break;
19341 case 8:
19342 retval = bfd_get_64 (abfd, buf);
19343 break;
19344 default:
19345 internal_error (__FILE__, __LINE__,
19346 _("read_offset_1: bad switch [in module %s]"),
19347 bfd_get_filename (abfd));
19348 }
19349
19350 return retval;
19351 }
19352
19353 static const gdb_byte *
19354 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19355 {
19356 /* If the size of a host char is 8 bits, we can return a pointer
19357 to the buffer, otherwise we have to copy the data to a buffer
19358 allocated on the temporary obstack. */
19359 gdb_assert (HOST_CHAR_BIT == 8);
19360 return buf;
19361 }
19362
19363 static const char *
19364 read_direct_string (bfd *abfd, const gdb_byte *buf,
19365 unsigned int *bytes_read_ptr)
19366 {
19367 /* If the size of a host char is 8 bits, we can return a pointer
19368 to the string, otherwise we have to copy the string to a buffer
19369 allocated on the temporary obstack. */
19370 gdb_assert (HOST_CHAR_BIT == 8);
19371 if (*buf == '\0')
19372 {
19373 *bytes_read_ptr = 1;
19374 return NULL;
19375 }
19376 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19377 return (const char *) buf;
19378 }
19379
19380 /* Return pointer to string at section SECT offset STR_OFFSET with error
19381 reporting strings FORM_NAME and SECT_NAME. */
19382
19383 static const char *
19384 read_indirect_string_at_offset_from (struct objfile *objfile,
19385 bfd *abfd, LONGEST str_offset,
19386 struct dwarf2_section_info *sect,
19387 const char *form_name,
19388 const char *sect_name)
19389 {
19390 sect->read (objfile);
19391 if (sect->buffer == NULL)
19392 error (_("%s used without %s section [in module %s]"),
19393 form_name, sect_name, bfd_get_filename (abfd));
19394 if (str_offset >= sect->size)
19395 error (_("%s pointing outside of %s section [in module %s]"),
19396 form_name, sect_name, bfd_get_filename (abfd));
19397 gdb_assert (HOST_CHAR_BIT == 8);
19398 if (sect->buffer[str_offset] == '\0')
19399 return NULL;
19400 return (const char *) (sect->buffer + str_offset);
19401 }
19402
19403 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19404
19405 static const char *
19406 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19407 bfd *abfd, LONGEST str_offset)
19408 {
19409 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19410 abfd, str_offset,
19411 &dwarf2_per_objfile->str,
19412 "DW_FORM_strp", ".debug_str");
19413 }
19414
19415 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19416
19417 static const char *
19418 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19419 bfd *abfd, LONGEST str_offset)
19420 {
19421 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19422 abfd, str_offset,
19423 &dwarf2_per_objfile->line_str,
19424 "DW_FORM_line_strp",
19425 ".debug_line_str");
19426 }
19427
19428 /* Read a string at offset STR_OFFSET in the .debug_str section from
19429 the .dwz file DWZ. Throw an error if the offset is too large. If
19430 the string consists of a single NUL byte, return NULL; otherwise
19431 return a pointer to the string. */
19432
19433 static const char *
19434 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19435 LONGEST str_offset)
19436 {
19437 dwz->str.read (objfile);
19438
19439 if (dwz->str.buffer == NULL)
19440 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19441 "section [in module %s]"),
19442 bfd_get_filename (dwz->dwz_bfd.get ()));
19443 if (str_offset >= dwz->str.size)
19444 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19445 ".debug_str section [in module %s]"),
19446 bfd_get_filename (dwz->dwz_bfd.get ()));
19447 gdb_assert (HOST_CHAR_BIT == 8);
19448 if (dwz->str.buffer[str_offset] == '\0')
19449 return NULL;
19450 return (const char *) (dwz->str.buffer + str_offset);
19451 }
19452
19453 /* Return pointer to string at .debug_str offset as read from BUF.
19454 BUF is assumed to be in a compilation unit described by CU_HEADER.
19455 Return *BYTES_READ_PTR count of bytes read from BUF. */
19456
19457 static const char *
19458 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19459 const gdb_byte *buf,
19460 const struct comp_unit_head *cu_header,
19461 unsigned int *bytes_read_ptr)
19462 {
19463 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19464
19465 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19466 }
19467
19468 /* Return pointer to string at .debug_line_str offset as read from BUF.
19469 BUF is assumed to be in a compilation unit described by CU_HEADER.
19470 Return *BYTES_READ_PTR count of bytes read from BUF. */
19471
19472 static const char *
19473 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19474 bfd *abfd, const gdb_byte *buf,
19475 const struct comp_unit_head *cu_header,
19476 unsigned int *bytes_read_ptr)
19477 {
19478 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19479
19480 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19481 str_offset);
19482 }
19483
19484 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19485 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19486 ADDR_SIZE is the size of addresses from the CU header. */
19487
19488 static CORE_ADDR
19489 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19490 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19491 int addr_size)
19492 {
19493 struct objfile *objfile = dwarf2_per_objfile->objfile;
19494 bfd *abfd = objfile->obfd;
19495 const gdb_byte *info_ptr;
19496 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19497
19498 dwarf2_per_objfile->addr.read (objfile);
19499 if (dwarf2_per_objfile->addr.buffer == NULL)
19500 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19501 objfile_name (objfile));
19502 if (addr_base_or_zero + addr_index * addr_size
19503 >= dwarf2_per_objfile->addr.size)
19504 error (_("DW_FORM_addr_index pointing outside of "
19505 ".debug_addr section [in module %s]"),
19506 objfile_name (objfile));
19507 info_ptr = (dwarf2_per_objfile->addr.buffer
19508 + addr_base_or_zero + addr_index * addr_size);
19509 if (addr_size == 4)
19510 return bfd_get_32 (abfd, info_ptr);
19511 else
19512 return bfd_get_64 (abfd, info_ptr);
19513 }
19514
19515 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19516
19517 static CORE_ADDR
19518 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19519 {
19520 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19521 cu->addr_base, cu->header.addr_size);
19522 }
19523
19524 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19525
19526 static CORE_ADDR
19527 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19528 unsigned int *bytes_read)
19529 {
19530 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19531 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19532
19533 return read_addr_index (cu, addr_index);
19534 }
19535
19536 /* Given an index in .debug_addr, fetch the value.
19537 NOTE: This can be called during dwarf expression evaluation,
19538 long after the debug information has been read, and thus per_cu->cu
19539 may no longer exist. */
19540
19541 CORE_ADDR
19542 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19543 unsigned int addr_index)
19544 {
19545 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19546 struct dwarf2_cu *cu = per_cu->cu;
19547 gdb::optional<ULONGEST> addr_base;
19548 int addr_size;
19549
19550 /* We need addr_base and addr_size.
19551 If we don't have PER_CU->cu, we have to get it.
19552 Nasty, but the alternative is storing the needed info in PER_CU,
19553 which at this point doesn't seem justified: it's not clear how frequently
19554 it would get used and it would increase the size of every PER_CU.
19555 Entry points like dwarf2_per_cu_addr_size do a similar thing
19556 so we're not in uncharted territory here.
19557 Alas we need to be a bit more complicated as addr_base is contained
19558 in the DIE.
19559
19560 We don't need to read the entire CU(/TU).
19561 We just need the header and top level die.
19562
19563 IWBN to use the aging mechanism to let us lazily later discard the CU.
19564 For now we skip this optimization. */
19565
19566 if (cu != NULL)
19567 {
19568 addr_base = cu->addr_base;
19569 addr_size = cu->header.addr_size;
19570 }
19571 else
19572 {
19573 cutu_reader reader (per_cu, NULL, 0, 0, false);
19574 addr_base = reader.cu->addr_base;
19575 addr_size = reader.cu->header.addr_size;
19576 }
19577
19578 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19579 addr_size);
19580 }
19581
19582 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19583 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19584 DWO file. */
19585
19586 static const char *
19587 read_str_index (struct dwarf2_cu *cu,
19588 struct dwarf2_section_info *str_section,
19589 struct dwarf2_section_info *str_offsets_section,
19590 ULONGEST str_offsets_base, ULONGEST str_index)
19591 {
19592 struct dwarf2_per_objfile *dwarf2_per_objfile
19593 = cu->per_cu->dwarf2_per_objfile;
19594 struct objfile *objfile = dwarf2_per_objfile->objfile;
19595 const char *objf_name = objfile_name (objfile);
19596 bfd *abfd = objfile->obfd;
19597 const gdb_byte *info_ptr;
19598 ULONGEST str_offset;
19599 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19600
19601 str_section->read (objfile);
19602 str_offsets_section->read (objfile);
19603 if (str_section->buffer == NULL)
19604 error (_("%s used without %s section"
19605 " in CU at offset %s [in module %s]"),
19606 form_name, str_section->get_name (),
19607 sect_offset_str (cu->header.sect_off), objf_name);
19608 if (str_offsets_section->buffer == NULL)
19609 error (_("%s used without %s section"
19610 " in CU at offset %s [in module %s]"),
19611 form_name, str_section->get_name (),
19612 sect_offset_str (cu->header.sect_off), objf_name);
19613 info_ptr = (str_offsets_section->buffer
19614 + str_offsets_base
19615 + str_index * cu->header.offset_size);
19616 if (cu->header.offset_size == 4)
19617 str_offset = bfd_get_32 (abfd, info_ptr);
19618 else
19619 str_offset = bfd_get_64 (abfd, info_ptr);
19620 if (str_offset >= str_section->size)
19621 error (_("Offset from %s pointing outside of"
19622 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19623 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19624 return (const char *) (str_section->buffer + str_offset);
19625 }
19626
19627 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19628
19629 static const char *
19630 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19631 {
19632 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19633 ? reader->cu->header.addr_size : 0;
19634 return read_str_index (reader->cu,
19635 &reader->dwo_file->sections.str,
19636 &reader->dwo_file->sections.str_offsets,
19637 str_offsets_base, str_index);
19638 }
19639
19640 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19641
19642 static const char *
19643 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19644 {
19645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19646 const char *objf_name = objfile_name (objfile);
19647 static const char form_name[] = "DW_FORM_GNU_str_index";
19648 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19649
19650 if (!cu->str_offsets_base.has_value ())
19651 error (_("%s used in Fission stub without %s"
19652 " in CU at offset 0x%lx [in module %s]"),
19653 form_name, str_offsets_attr_name,
19654 (long) cu->header.offset_size, objf_name);
19655
19656 return read_str_index (cu,
19657 &cu->per_cu->dwarf2_per_objfile->str,
19658 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19659 *cu->str_offsets_base, str_index);
19660 }
19661
19662 /* Return the length of an LEB128 number in BUF. */
19663
19664 static int
19665 leb128_size (const gdb_byte *buf)
19666 {
19667 const gdb_byte *begin = buf;
19668 gdb_byte byte;
19669
19670 while (1)
19671 {
19672 byte = *buf++;
19673 if ((byte & 128) == 0)
19674 return buf - begin;
19675 }
19676 }
19677
19678 static void
19679 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19680 {
19681 switch (lang)
19682 {
19683 case DW_LANG_C89:
19684 case DW_LANG_C99:
19685 case DW_LANG_C11:
19686 case DW_LANG_C:
19687 case DW_LANG_UPC:
19688 cu->language = language_c;
19689 break;
19690 case DW_LANG_Java:
19691 case DW_LANG_C_plus_plus:
19692 case DW_LANG_C_plus_plus_11:
19693 case DW_LANG_C_plus_plus_14:
19694 cu->language = language_cplus;
19695 break;
19696 case DW_LANG_D:
19697 cu->language = language_d;
19698 break;
19699 case DW_LANG_Fortran77:
19700 case DW_LANG_Fortran90:
19701 case DW_LANG_Fortran95:
19702 case DW_LANG_Fortran03:
19703 case DW_LANG_Fortran08:
19704 cu->language = language_fortran;
19705 break;
19706 case DW_LANG_Go:
19707 cu->language = language_go;
19708 break;
19709 case DW_LANG_Mips_Assembler:
19710 cu->language = language_asm;
19711 break;
19712 case DW_LANG_Ada83:
19713 case DW_LANG_Ada95:
19714 cu->language = language_ada;
19715 break;
19716 case DW_LANG_Modula2:
19717 cu->language = language_m2;
19718 break;
19719 case DW_LANG_Pascal83:
19720 cu->language = language_pascal;
19721 break;
19722 case DW_LANG_ObjC:
19723 cu->language = language_objc;
19724 break;
19725 case DW_LANG_Rust:
19726 case DW_LANG_Rust_old:
19727 cu->language = language_rust;
19728 break;
19729 case DW_LANG_Cobol74:
19730 case DW_LANG_Cobol85:
19731 default:
19732 cu->language = language_minimal;
19733 break;
19734 }
19735 cu->language_defn = language_def (cu->language);
19736 }
19737
19738 /* Return the named attribute or NULL if not there. */
19739
19740 static struct attribute *
19741 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19742 {
19743 for (;;)
19744 {
19745 unsigned int i;
19746 struct attribute *spec = NULL;
19747
19748 for (i = 0; i < die->num_attrs; ++i)
19749 {
19750 if (die->attrs[i].name == name)
19751 return &die->attrs[i];
19752 if (die->attrs[i].name == DW_AT_specification
19753 || die->attrs[i].name == DW_AT_abstract_origin)
19754 spec = &die->attrs[i];
19755 }
19756
19757 if (!spec)
19758 break;
19759
19760 die = follow_die_ref (die, spec, &cu);
19761 }
19762
19763 return NULL;
19764 }
19765
19766 /* Return the named attribute or NULL if not there,
19767 but do not follow DW_AT_specification, etc.
19768 This is for use in contexts where we're reading .debug_types dies.
19769 Following DW_AT_specification, DW_AT_abstract_origin will take us
19770 back up the chain, and we want to go down. */
19771
19772 static struct attribute *
19773 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19774 {
19775 unsigned int i;
19776
19777 for (i = 0; i < die->num_attrs; ++i)
19778 if (die->attrs[i].name == name)
19779 return &die->attrs[i];
19780
19781 return NULL;
19782 }
19783
19784 /* Return the string associated with a string-typed attribute, or NULL if it
19785 is either not found or is of an incorrect type. */
19786
19787 static const char *
19788 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19789 {
19790 struct attribute *attr;
19791 const char *str = NULL;
19792
19793 attr = dwarf2_attr (die, name, cu);
19794
19795 if (attr != NULL)
19796 {
19797 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19798 || attr->form == DW_FORM_string
19799 || attr->form == DW_FORM_strx
19800 || attr->form == DW_FORM_strx1
19801 || attr->form == DW_FORM_strx2
19802 || attr->form == DW_FORM_strx3
19803 || attr->form == DW_FORM_strx4
19804 || attr->form == DW_FORM_GNU_str_index
19805 || attr->form == DW_FORM_GNU_strp_alt)
19806 str = DW_STRING (attr);
19807 else
19808 complaint (_("string type expected for attribute %s for "
19809 "DIE at %s in module %s"),
19810 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19812 }
19813
19814 return str;
19815 }
19816
19817 /* Return the dwo name or NULL if not present. If present, it is in either
19818 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19819 static const char *
19820 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19821 {
19822 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19823 if (dwo_name == nullptr)
19824 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19825 return dwo_name;
19826 }
19827
19828 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19829 and holds a non-zero value. This function should only be used for
19830 DW_FORM_flag or DW_FORM_flag_present attributes. */
19831
19832 static int
19833 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19834 {
19835 struct attribute *attr = dwarf2_attr (die, name, cu);
19836
19837 return (attr && DW_UNSND (attr));
19838 }
19839
19840 static int
19841 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19842 {
19843 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19844 which value is non-zero. However, we have to be careful with
19845 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19846 (via dwarf2_flag_true_p) follows this attribute. So we may
19847 end up accidently finding a declaration attribute that belongs
19848 to a different DIE referenced by the specification attribute,
19849 even though the given DIE does not have a declaration attribute. */
19850 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19851 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19852 }
19853
19854 /* Return the die giving the specification for DIE, if there is
19855 one. *SPEC_CU is the CU containing DIE on input, and the CU
19856 containing the return value on output. If there is no
19857 specification, but there is an abstract origin, that is
19858 returned. */
19859
19860 static struct die_info *
19861 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19862 {
19863 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19864 *spec_cu);
19865
19866 if (spec_attr == NULL)
19867 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19868
19869 if (spec_attr == NULL)
19870 return NULL;
19871 else
19872 return follow_die_ref (die, spec_attr, spec_cu);
19873 }
19874
19875 /* Stub for free_line_header to match void * callback types. */
19876
19877 static void
19878 free_line_header_voidp (void *arg)
19879 {
19880 struct line_header *lh = (struct line_header *) arg;
19881
19882 delete lh;
19883 }
19884
19885 void
19886 line_header::add_include_dir (const char *include_dir)
19887 {
19888 if (dwarf_line_debug >= 2)
19889 {
19890 size_t new_size;
19891 if (version >= 5)
19892 new_size = m_include_dirs.size ();
19893 else
19894 new_size = m_include_dirs.size () + 1;
19895 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19896 new_size, include_dir);
19897 }
19898 m_include_dirs.push_back (include_dir);
19899 }
19900
19901 void
19902 line_header::add_file_name (const char *name,
19903 dir_index d_index,
19904 unsigned int mod_time,
19905 unsigned int length)
19906 {
19907 if (dwarf_line_debug >= 2)
19908 {
19909 size_t new_size;
19910 if (version >= 5)
19911 new_size = file_names_size ();
19912 else
19913 new_size = file_names_size () + 1;
19914 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
19915 new_size, name);
19916 }
19917 m_file_names.emplace_back (name, d_index, mod_time, length);
19918 }
19919
19920 /* A convenience function to find the proper .debug_line section for a CU. */
19921
19922 static struct dwarf2_section_info *
19923 get_debug_line_section (struct dwarf2_cu *cu)
19924 {
19925 struct dwarf2_section_info *section;
19926 struct dwarf2_per_objfile *dwarf2_per_objfile
19927 = cu->per_cu->dwarf2_per_objfile;
19928
19929 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19930 DWO file. */
19931 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19932 section = &cu->dwo_unit->dwo_file->sections.line;
19933 else if (cu->per_cu->is_dwz)
19934 {
19935 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19936
19937 section = &dwz->line;
19938 }
19939 else
19940 section = &dwarf2_per_objfile->line;
19941
19942 return section;
19943 }
19944
19945 /* Read directory or file name entry format, starting with byte of
19946 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19947 entries count and the entries themselves in the described entry
19948 format. */
19949
19950 static void
19951 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19952 bfd *abfd, const gdb_byte **bufp,
19953 struct line_header *lh,
19954 const struct comp_unit_head *cu_header,
19955 void (*callback) (struct line_header *lh,
19956 const char *name,
19957 dir_index d_index,
19958 unsigned int mod_time,
19959 unsigned int length))
19960 {
19961 gdb_byte format_count, formati;
19962 ULONGEST data_count, datai;
19963 const gdb_byte *buf = *bufp;
19964 const gdb_byte *format_header_data;
19965 unsigned int bytes_read;
19966
19967 format_count = read_1_byte (abfd, buf);
19968 buf += 1;
19969 format_header_data = buf;
19970 for (formati = 0; formati < format_count; formati++)
19971 {
19972 read_unsigned_leb128 (abfd, buf, &bytes_read);
19973 buf += bytes_read;
19974 read_unsigned_leb128 (abfd, buf, &bytes_read);
19975 buf += bytes_read;
19976 }
19977
19978 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19979 buf += bytes_read;
19980 for (datai = 0; datai < data_count; datai++)
19981 {
19982 const gdb_byte *format = format_header_data;
19983 struct file_entry fe;
19984
19985 for (formati = 0; formati < format_count; formati++)
19986 {
19987 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19988 format += bytes_read;
19989
19990 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19991 format += bytes_read;
19992
19993 gdb::optional<const char *> string;
19994 gdb::optional<unsigned int> uint;
19995
19996 switch (form)
19997 {
19998 case DW_FORM_string:
19999 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20000 buf += bytes_read;
20001 break;
20002
20003 case DW_FORM_line_strp:
20004 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20005 abfd, buf,
20006 cu_header,
20007 &bytes_read));
20008 buf += bytes_read;
20009 break;
20010
20011 case DW_FORM_data1:
20012 uint.emplace (read_1_byte (abfd, buf));
20013 buf += 1;
20014 break;
20015
20016 case DW_FORM_data2:
20017 uint.emplace (read_2_bytes (abfd, buf));
20018 buf += 2;
20019 break;
20020
20021 case DW_FORM_data4:
20022 uint.emplace (read_4_bytes (abfd, buf));
20023 buf += 4;
20024 break;
20025
20026 case DW_FORM_data8:
20027 uint.emplace (read_8_bytes (abfd, buf));
20028 buf += 8;
20029 break;
20030
20031 case DW_FORM_data16:
20032 /* This is used for MD5, but file_entry does not record MD5s. */
20033 buf += 16;
20034 break;
20035
20036 case DW_FORM_udata:
20037 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20038 buf += bytes_read;
20039 break;
20040
20041 case DW_FORM_block:
20042 /* It is valid only for DW_LNCT_timestamp which is ignored by
20043 current GDB. */
20044 break;
20045 }
20046
20047 switch (content_type)
20048 {
20049 case DW_LNCT_path:
20050 if (string.has_value ())
20051 fe.name = *string;
20052 break;
20053 case DW_LNCT_directory_index:
20054 if (uint.has_value ())
20055 fe.d_index = (dir_index) *uint;
20056 break;
20057 case DW_LNCT_timestamp:
20058 if (uint.has_value ())
20059 fe.mod_time = *uint;
20060 break;
20061 case DW_LNCT_size:
20062 if (uint.has_value ())
20063 fe.length = *uint;
20064 break;
20065 case DW_LNCT_MD5:
20066 break;
20067 default:
20068 complaint (_("Unknown format content type %s"),
20069 pulongest (content_type));
20070 }
20071 }
20072
20073 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20074 }
20075
20076 *bufp = buf;
20077 }
20078
20079 /* Read the statement program header starting at OFFSET in
20080 .debug_line, or .debug_line.dwo. Return a pointer
20081 to a struct line_header, allocated using xmalloc.
20082 Returns NULL if there is a problem reading the header, e.g., if it
20083 has a version we don't understand.
20084
20085 NOTE: the strings in the include directory and file name tables of
20086 the returned object point into the dwarf line section buffer,
20087 and must not be freed. */
20088
20089 static line_header_up
20090 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20091 {
20092 const gdb_byte *line_ptr;
20093 unsigned int bytes_read, offset_size;
20094 int i;
20095 const char *cur_dir, *cur_file;
20096 struct dwarf2_section_info *section;
20097 bfd *abfd;
20098 struct dwarf2_per_objfile *dwarf2_per_objfile
20099 = cu->per_cu->dwarf2_per_objfile;
20100
20101 section = get_debug_line_section (cu);
20102 section->read (dwarf2_per_objfile->objfile);
20103 if (section->buffer == NULL)
20104 {
20105 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20106 complaint (_("missing .debug_line.dwo section"));
20107 else
20108 complaint (_("missing .debug_line section"));
20109 return 0;
20110 }
20111
20112 /* We can't do this until we know the section is non-empty.
20113 Only then do we know we have such a section. */
20114 abfd = section->get_bfd_owner ();
20115
20116 /* Make sure that at least there's room for the total_length field.
20117 That could be 12 bytes long, but we're just going to fudge that. */
20118 if (to_underlying (sect_off) + 4 >= section->size)
20119 {
20120 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20121 return 0;
20122 }
20123
20124 line_header_up lh (new line_header ());
20125
20126 lh->sect_off = sect_off;
20127 lh->offset_in_dwz = cu->per_cu->is_dwz;
20128
20129 line_ptr = section->buffer + to_underlying (sect_off);
20130
20131 /* Read in the header. */
20132 lh->total_length =
20133 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20134 &bytes_read, &offset_size);
20135 line_ptr += bytes_read;
20136
20137 const gdb_byte *start_here = line_ptr;
20138
20139 if (line_ptr + lh->total_length > (section->buffer + section->size))
20140 {
20141 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20142 return 0;
20143 }
20144 lh->statement_program_end = start_here + lh->total_length;
20145 lh->version = read_2_bytes (abfd, line_ptr);
20146 line_ptr += 2;
20147 if (lh->version > 5)
20148 {
20149 /* This is a version we don't understand. The format could have
20150 changed in ways we don't handle properly so just punt. */
20151 complaint (_("unsupported version in .debug_line section"));
20152 return NULL;
20153 }
20154 if (lh->version >= 5)
20155 {
20156 gdb_byte segment_selector_size;
20157
20158 /* Skip address size. */
20159 read_1_byte (abfd, line_ptr);
20160 line_ptr += 1;
20161
20162 segment_selector_size = read_1_byte (abfd, line_ptr);
20163 line_ptr += 1;
20164 if (segment_selector_size != 0)
20165 {
20166 complaint (_("unsupported segment selector size %u "
20167 "in .debug_line section"),
20168 segment_selector_size);
20169 return NULL;
20170 }
20171 }
20172 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20173 line_ptr += offset_size;
20174 lh->statement_program_start = line_ptr + lh->header_length;
20175 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20176 line_ptr += 1;
20177 if (lh->version >= 4)
20178 {
20179 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20180 line_ptr += 1;
20181 }
20182 else
20183 lh->maximum_ops_per_instruction = 1;
20184
20185 if (lh->maximum_ops_per_instruction == 0)
20186 {
20187 lh->maximum_ops_per_instruction = 1;
20188 complaint (_("invalid maximum_ops_per_instruction "
20189 "in `.debug_line' section"));
20190 }
20191
20192 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20193 line_ptr += 1;
20194 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20195 line_ptr += 1;
20196 lh->line_range = read_1_byte (abfd, line_ptr);
20197 line_ptr += 1;
20198 lh->opcode_base = read_1_byte (abfd, line_ptr);
20199 line_ptr += 1;
20200 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20201
20202 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20203 for (i = 1; i < lh->opcode_base; ++i)
20204 {
20205 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20206 line_ptr += 1;
20207 }
20208
20209 if (lh->version >= 5)
20210 {
20211 /* Read directory table. */
20212 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20213 &cu->header,
20214 [] (struct line_header *header, const char *name,
20215 dir_index d_index, unsigned int mod_time,
20216 unsigned int length)
20217 {
20218 header->add_include_dir (name);
20219 });
20220
20221 /* Read file name table. */
20222 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20223 &cu->header,
20224 [] (struct line_header *header, const char *name,
20225 dir_index d_index, unsigned int mod_time,
20226 unsigned int length)
20227 {
20228 header->add_file_name (name, d_index, mod_time, length);
20229 });
20230 }
20231 else
20232 {
20233 /* Read directory table. */
20234 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20235 {
20236 line_ptr += bytes_read;
20237 lh->add_include_dir (cur_dir);
20238 }
20239 line_ptr += bytes_read;
20240
20241 /* Read file name table. */
20242 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20243 {
20244 unsigned int mod_time, length;
20245 dir_index d_index;
20246
20247 line_ptr += bytes_read;
20248 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20249 line_ptr += bytes_read;
20250 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20251 line_ptr += bytes_read;
20252 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20253 line_ptr += bytes_read;
20254
20255 lh->add_file_name (cur_file, d_index, mod_time, length);
20256 }
20257 line_ptr += bytes_read;
20258 }
20259
20260 if (line_ptr > (section->buffer + section->size))
20261 complaint (_("line number info header doesn't "
20262 "fit in `.debug_line' section"));
20263
20264 return lh;
20265 }
20266
20267 /* Subroutine of dwarf_decode_lines to simplify it.
20268 Return the file name of the psymtab for the given file_entry.
20269 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20270 If space for the result is malloc'd, *NAME_HOLDER will be set.
20271 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20272
20273 static const char *
20274 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20275 const dwarf2_psymtab *pst,
20276 const char *comp_dir,
20277 gdb::unique_xmalloc_ptr<char> *name_holder)
20278 {
20279 const char *include_name = fe.name;
20280 const char *include_name_to_compare = include_name;
20281 const char *pst_filename;
20282 int file_is_pst;
20283
20284 const char *dir_name = fe.include_dir (lh);
20285
20286 gdb::unique_xmalloc_ptr<char> hold_compare;
20287 if (!IS_ABSOLUTE_PATH (include_name)
20288 && (dir_name != NULL || comp_dir != NULL))
20289 {
20290 /* Avoid creating a duplicate psymtab for PST.
20291 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20292 Before we do the comparison, however, we need to account
20293 for DIR_NAME and COMP_DIR.
20294 First prepend dir_name (if non-NULL). If we still don't
20295 have an absolute path prepend comp_dir (if non-NULL).
20296 However, the directory we record in the include-file's
20297 psymtab does not contain COMP_DIR (to match the
20298 corresponding symtab(s)).
20299
20300 Example:
20301
20302 bash$ cd /tmp
20303 bash$ gcc -g ./hello.c
20304 include_name = "hello.c"
20305 dir_name = "."
20306 DW_AT_comp_dir = comp_dir = "/tmp"
20307 DW_AT_name = "./hello.c"
20308
20309 */
20310
20311 if (dir_name != NULL)
20312 {
20313 name_holder->reset (concat (dir_name, SLASH_STRING,
20314 include_name, (char *) NULL));
20315 include_name = name_holder->get ();
20316 include_name_to_compare = include_name;
20317 }
20318 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20319 {
20320 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20321 include_name, (char *) NULL));
20322 include_name_to_compare = hold_compare.get ();
20323 }
20324 }
20325
20326 pst_filename = pst->filename;
20327 gdb::unique_xmalloc_ptr<char> copied_name;
20328 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20329 {
20330 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20331 pst_filename, (char *) NULL));
20332 pst_filename = copied_name.get ();
20333 }
20334
20335 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20336
20337 if (file_is_pst)
20338 return NULL;
20339 return include_name;
20340 }
20341
20342 /* State machine to track the state of the line number program. */
20343
20344 class lnp_state_machine
20345 {
20346 public:
20347 /* Initialize a machine state for the start of a line number
20348 program. */
20349 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20350 bool record_lines_p);
20351
20352 file_entry *current_file ()
20353 {
20354 /* lh->file_names is 0-based, but the file name numbers in the
20355 statement program are 1-based. */
20356 return m_line_header->file_name_at (m_file);
20357 }
20358
20359 /* Record the line in the state machine. END_SEQUENCE is true if
20360 we're processing the end of a sequence. */
20361 void record_line (bool end_sequence);
20362
20363 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20364 nop-out rest of the lines in this sequence. */
20365 void check_line_address (struct dwarf2_cu *cu,
20366 const gdb_byte *line_ptr,
20367 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20368
20369 void handle_set_discriminator (unsigned int discriminator)
20370 {
20371 m_discriminator = discriminator;
20372 m_line_has_non_zero_discriminator |= discriminator != 0;
20373 }
20374
20375 /* Handle DW_LNE_set_address. */
20376 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20377 {
20378 m_op_index = 0;
20379 address += baseaddr;
20380 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20381 }
20382
20383 /* Handle DW_LNS_advance_pc. */
20384 void handle_advance_pc (CORE_ADDR adjust);
20385
20386 /* Handle a special opcode. */
20387 void handle_special_opcode (unsigned char op_code);
20388
20389 /* Handle DW_LNS_advance_line. */
20390 void handle_advance_line (int line_delta)
20391 {
20392 advance_line (line_delta);
20393 }
20394
20395 /* Handle DW_LNS_set_file. */
20396 void handle_set_file (file_name_index file);
20397
20398 /* Handle DW_LNS_negate_stmt. */
20399 void handle_negate_stmt ()
20400 {
20401 m_is_stmt = !m_is_stmt;
20402 }
20403
20404 /* Handle DW_LNS_const_add_pc. */
20405 void handle_const_add_pc ();
20406
20407 /* Handle DW_LNS_fixed_advance_pc. */
20408 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20409 {
20410 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20411 m_op_index = 0;
20412 }
20413
20414 /* Handle DW_LNS_copy. */
20415 void handle_copy ()
20416 {
20417 record_line (false);
20418 m_discriminator = 0;
20419 }
20420
20421 /* Handle DW_LNE_end_sequence. */
20422 void handle_end_sequence ()
20423 {
20424 m_currently_recording_lines = true;
20425 }
20426
20427 private:
20428 /* Advance the line by LINE_DELTA. */
20429 void advance_line (int line_delta)
20430 {
20431 m_line += line_delta;
20432
20433 if (line_delta != 0)
20434 m_line_has_non_zero_discriminator = m_discriminator != 0;
20435 }
20436
20437 struct dwarf2_cu *m_cu;
20438
20439 gdbarch *m_gdbarch;
20440
20441 /* True if we're recording lines.
20442 Otherwise we're building partial symtabs and are just interested in
20443 finding include files mentioned by the line number program. */
20444 bool m_record_lines_p;
20445
20446 /* The line number header. */
20447 line_header *m_line_header;
20448
20449 /* These are part of the standard DWARF line number state machine,
20450 and initialized according to the DWARF spec. */
20451
20452 unsigned char m_op_index = 0;
20453 /* The line table index of the current file. */
20454 file_name_index m_file = 1;
20455 unsigned int m_line = 1;
20456
20457 /* These are initialized in the constructor. */
20458
20459 CORE_ADDR m_address;
20460 bool m_is_stmt;
20461 unsigned int m_discriminator;
20462
20463 /* Additional bits of state we need to track. */
20464
20465 /* The last file that we called dwarf2_start_subfile for.
20466 This is only used for TLLs. */
20467 unsigned int m_last_file = 0;
20468 /* The last file a line number was recorded for. */
20469 struct subfile *m_last_subfile = NULL;
20470
20471 /* When true, record the lines we decode. */
20472 bool m_currently_recording_lines = false;
20473
20474 /* The last line number that was recorded, used to coalesce
20475 consecutive entries for the same line. This can happen, for
20476 example, when discriminators are present. PR 17276. */
20477 unsigned int m_last_line = 0;
20478 bool m_line_has_non_zero_discriminator = false;
20479 };
20480
20481 void
20482 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20483 {
20484 CORE_ADDR addr_adj = (((m_op_index + adjust)
20485 / m_line_header->maximum_ops_per_instruction)
20486 * m_line_header->minimum_instruction_length);
20487 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20488 m_op_index = ((m_op_index + adjust)
20489 % m_line_header->maximum_ops_per_instruction);
20490 }
20491
20492 void
20493 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20494 {
20495 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20496 CORE_ADDR addr_adj = (((m_op_index
20497 + (adj_opcode / m_line_header->line_range))
20498 / m_line_header->maximum_ops_per_instruction)
20499 * m_line_header->minimum_instruction_length);
20500 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20501 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20502 % m_line_header->maximum_ops_per_instruction);
20503
20504 int line_delta = (m_line_header->line_base
20505 + (adj_opcode % m_line_header->line_range));
20506 advance_line (line_delta);
20507 record_line (false);
20508 m_discriminator = 0;
20509 }
20510
20511 void
20512 lnp_state_machine::handle_set_file (file_name_index file)
20513 {
20514 m_file = file;
20515
20516 const file_entry *fe = current_file ();
20517 if (fe == NULL)
20518 dwarf2_debug_line_missing_file_complaint ();
20519 else if (m_record_lines_p)
20520 {
20521 const char *dir = fe->include_dir (m_line_header);
20522
20523 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20524 m_line_has_non_zero_discriminator = m_discriminator != 0;
20525 dwarf2_start_subfile (m_cu, fe->name, dir);
20526 }
20527 }
20528
20529 void
20530 lnp_state_machine::handle_const_add_pc ()
20531 {
20532 CORE_ADDR adjust
20533 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20534
20535 CORE_ADDR addr_adj
20536 = (((m_op_index + adjust)
20537 / m_line_header->maximum_ops_per_instruction)
20538 * m_line_header->minimum_instruction_length);
20539
20540 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20541 m_op_index = ((m_op_index + adjust)
20542 % m_line_header->maximum_ops_per_instruction);
20543 }
20544
20545 /* Return non-zero if we should add LINE to the line number table.
20546 LINE is the line to add, LAST_LINE is the last line that was added,
20547 LAST_SUBFILE is the subfile for LAST_LINE.
20548 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20549 had a non-zero discriminator.
20550
20551 We have to be careful in the presence of discriminators.
20552 E.g., for this line:
20553
20554 for (i = 0; i < 100000; i++);
20555
20556 clang can emit four line number entries for that one line,
20557 each with a different discriminator.
20558 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20559
20560 However, we want gdb to coalesce all four entries into one.
20561 Otherwise the user could stepi into the middle of the line and
20562 gdb would get confused about whether the pc really was in the
20563 middle of the line.
20564
20565 Things are further complicated by the fact that two consecutive
20566 line number entries for the same line is a heuristic used by gcc
20567 to denote the end of the prologue. So we can't just discard duplicate
20568 entries, we have to be selective about it. The heuristic we use is
20569 that we only collapse consecutive entries for the same line if at least
20570 one of those entries has a non-zero discriminator. PR 17276.
20571
20572 Note: Addresses in the line number state machine can never go backwards
20573 within one sequence, thus this coalescing is ok. */
20574
20575 static int
20576 dwarf_record_line_p (struct dwarf2_cu *cu,
20577 unsigned int line, unsigned int last_line,
20578 int line_has_non_zero_discriminator,
20579 struct subfile *last_subfile)
20580 {
20581 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20582 return 1;
20583 if (line != last_line)
20584 return 1;
20585 /* Same line for the same file that we've seen already.
20586 As a last check, for pr 17276, only record the line if the line
20587 has never had a non-zero discriminator. */
20588 if (!line_has_non_zero_discriminator)
20589 return 1;
20590 return 0;
20591 }
20592
20593 /* Use the CU's builder to record line number LINE beginning at
20594 address ADDRESS in the line table of subfile SUBFILE. */
20595
20596 static void
20597 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20598 unsigned int line, CORE_ADDR address,
20599 struct dwarf2_cu *cu)
20600 {
20601 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20602
20603 if (dwarf_line_debug)
20604 {
20605 fprintf_unfiltered (gdb_stdlog,
20606 "Recording line %u, file %s, address %s\n",
20607 line, lbasename (subfile->name),
20608 paddress (gdbarch, address));
20609 }
20610
20611 if (cu != nullptr)
20612 cu->get_builder ()->record_line (subfile, line, addr);
20613 }
20614
20615 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20616 Mark the end of a set of line number records.
20617 The arguments are the same as for dwarf_record_line_1.
20618 If SUBFILE is NULL the request is ignored. */
20619
20620 static void
20621 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20622 CORE_ADDR address, struct dwarf2_cu *cu)
20623 {
20624 if (subfile == NULL)
20625 return;
20626
20627 if (dwarf_line_debug)
20628 {
20629 fprintf_unfiltered (gdb_stdlog,
20630 "Finishing current line, file %s, address %s\n",
20631 lbasename (subfile->name),
20632 paddress (gdbarch, address));
20633 }
20634
20635 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20636 }
20637
20638 void
20639 lnp_state_machine::record_line (bool end_sequence)
20640 {
20641 if (dwarf_line_debug)
20642 {
20643 fprintf_unfiltered (gdb_stdlog,
20644 "Processing actual line %u: file %u,"
20645 " address %s, is_stmt %u, discrim %u%s\n",
20646 m_line, m_file,
20647 paddress (m_gdbarch, m_address),
20648 m_is_stmt, m_discriminator,
20649 (end_sequence ? "\t(end sequence)" : ""));
20650 }
20651
20652 file_entry *fe = current_file ();
20653
20654 if (fe == NULL)
20655 dwarf2_debug_line_missing_file_complaint ();
20656 /* For now we ignore lines not starting on an instruction boundary.
20657 But not when processing end_sequence for compatibility with the
20658 previous version of the code. */
20659 else if (m_op_index == 0 || end_sequence)
20660 {
20661 fe->included_p = 1;
20662 if (m_record_lines_p
20663 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20664 {
20665 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20666 || end_sequence)
20667 {
20668 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20669 m_currently_recording_lines ? m_cu : nullptr);
20670 }
20671
20672 if (!end_sequence)
20673 {
20674 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20675 m_line_has_non_zero_discriminator,
20676 m_last_subfile))
20677 {
20678 buildsym_compunit *builder = m_cu->get_builder ();
20679 dwarf_record_line_1 (m_gdbarch,
20680 builder->get_current_subfile (),
20681 m_line, m_address,
20682 m_currently_recording_lines ? m_cu : nullptr);
20683 }
20684 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20685 m_last_line = m_line;
20686 }
20687 }
20688 }
20689 }
20690
20691 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20692 line_header *lh, bool record_lines_p)
20693 {
20694 m_cu = cu;
20695 m_gdbarch = arch;
20696 m_record_lines_p = record_lines_p;
20697 m_line_header = lh;
20698
20699 m_currently_recording_lines = true;
20700
20701 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20702 was a line entry for it so that the backend has a chance to adjust it
20703 and also record it in case it needs it. This is currently used by MIPS
20704 code, cf. `mips_adjust_dwarf2_line'. */
20705 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20706 m_is_stmt = lh->default_is_stmt;
20707 m_discriminator = 0;
20708 }
20709
20710 void
20711 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20712 const gdb_byte *line_ptr,
20713 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20714 {
20715 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20716 the pc range of the CU. However, we restrict the test to only ADDRESS
20717 values of zero to preserve GDB's previous behaviour which is to handle
20718 the specific case of a function being GC'd by the linker. */
20719
20720 if (address == 0 && address < unrelocated_lowpc)
20721 {
20722 /* This line table is for a function which has been
20723 GCd by the linker. Ignore it. PR gdb/12528 */
20724
20725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20726 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20727
20728 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20729 line_offset, objfile_name (objfile));
20730 m_currently_recording_lines = false;
20731 /* Note: m_currently_recording_lines is left as false until we see
20732 DW_LNE_end_sequence. */
20733 }
20734 }
20735
20736 /* Subroutine of dwarf_decode_lines to simplify it.
20737 Process the line number information in LH.
20738 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20739 program in order to set included_p for every referenced header. */
20740
20741 static void
20742 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20743 const int decode_for_pst_p, CORE_ADDR lowpc)
20744 {
20745 const gdb_byte *line_ptr, *extended_end;
20746 const gdb_byte *line_end;
20747 unsigned int bytes_read, extended_len;
20748 unsigned char op_code, extended_op;
20749 CORE_ADDR baseaddr;
20750 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20751 bfd *abfd = objfile->obfd;
20752 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20753 /* True if we're recording line info (as opposed to building partial
20754 symtabs and just interested in finding include files mentioned by
20755 the line number program). */
20756 bool record_lines_p = !decode_for_pst_p;
20757
20758 baseaddr = objfile->text_section_offset ();
20759
20760 line_ptr = lh->statement_program_start;
20761 line_end = lh->statement_program_end;
20762
20763 /* Read the statement sequences until there's nothing left. */
20764 while (line_ptr < line_end)
20765 {
20766 /* The DWARF line number program state machine. Reset the state
20767 machine at the start of each sequence. */
20768 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20769 bool end_sequence = false;
20770
20771 if (record_lines_p)
20772 {
20773 /* Start a subfile for the current file of the state
20774 machine. */
20775 const file_entry *fe = state_machine.current_file ();
20776
20777 if (fe != NULL)
20778 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20779 }
20780
20781 /* Decode the table. */
20782 while (line_ptr < line_end && !end_sequence)
20783 {
20784 op_code = read_1_byte (abfd, line_ptr);
20785 line_ptr += 1;
20786
20787 if (op_code >= lh->opcode_base)
20788 {
20789 /* Special opcode. */
20790 state_machine.handle_special_opcode (op_code);
20791 }
20792 else switch (op_code)
20793 {
20794 case DW_LNS_extended_op:
20795 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20796 &bytes_read);
20797 line_ptr += bytes_read;
20798 extended_end = line_ptr + extended_len;
20799 extended_op = read_1_byte (abfd, line_ptr);
20800 line_ptr += 1;
20801 switch (extended_op)
20802 {
20803 case DW_LNE_end_sequence:
20804 state_machine.handle_end_sequence ();
20805 end_sequence = true;
20806 break;
20807 case DW_LNE_set_address:
20808 {
20809 CORE_ADDR address
20810 = read_address (abfd, line_ptr, cu, &bytes_read);
20811 line_ptr += bytes_read;
20812
20813 state_machine.check_line_address (cu, line_ptr,
20814 lowpc - baseaddr, address);
20815 state_machine.handle_set_address (baseaddr, address);
20816 }
20817 break;
20818 case DW_LNE_define_file:
20819 {
20820 const char *cur_file;
20821 unsigned int mod_time, length;
20822 dir_index dindex;
20823
20824 cur_file = read_direct_string (abfd, line_ptr,
20825 &bytes_read);
20826 line_ptr += bytes_read;
20827 dindex = (dir_index)
20828 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20829 line_ptr += bytes_read;
20830 mod_time =
20831 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20832 line_ptr += bytes_read;
20833 length =
20834 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20835 line_ptr += bytes_read;
20836 lh->add_file_name (cur_file, dindex, mod_time, length);
20837 }
20838 break;
20839 case DW_LNE_set_discriminator:
20840 {
20841 /* The discriminator is not interesting to the
20842 debugger; just ignore it. We still need to
20843 check its value though:
20844 if there are consecutive entries for the same
20845 (non-prologue) line we want to coalesce them.
20846 PR 17276. */
20847 unsigned int discr
20848 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20849 line_ptr += bytes_read;
20850
20851 state_machine.handle_set_discriminator (discr);
20852 }
20853 break;
20854 default:
20855 complaint (_("mangled .debug_line section"));
20856 return;
20857 }
20858 /* Make sure that we parsed the extended op correctly. If e.g.
20859 we expected a different address size than the producer used,
20860 we may have read the wrong number of bytes. */
20861 if (line_ptr != extended_end)
20862 {
20863 complaint (_("mangled .debug_line section"));
20864 return;
20865 }
20866 break;
20867 case DW_LNS_copy:
20868 state_machine.handle_copy ();
20869 break;
20870 case DW_LNS_advance_pc:
20871 {
20872 CORE_ADDR adjust
20873 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20874 line_ptr += bytes_read;
20875
20876 state_machine.handle_advance_pc (adjust);
20877 }
20878 break;
20879 case DW_LNS_advance_line:
20880 {
20881 int line_delta
20882 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20883 line_ptr += bytes_read;
20884
20885 state_machine.handle_advance_line (line_delta);
20886 }
20887 break;
20888 case DW_LNS_set_file:
20889 {
20890 file_name_index file
20891 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20892 &bytes_read);
20893 line_ptr += bytes_read;
20894
20895 state_machine.handle_set_file (file);
20896 }
20897 break;
20898 case DW_LNS_set_column:
20899 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20900 line_ptr += bytes_read;
20901 break;
20902 case DW_LNS_negate_stmt:
20903 state_machine.handle_negate_stmt ();
20904 break;
20905 case DW_LNS_set_basic_block:
20906 break;
20907 /* Add to the address register of the state machine the
20908 address increment value corresponding to special opcode
20909 255. I.e., this value is scaled by the minimum
20910 instruction length since special opcode 255 would have
20911 scaled the increment. */
20912 case DW_LNS_const_add_pc:
20913 state_machine.handle_const_add_pc ();
20914 break;
20915 case DW_LNS_fixed_advance_pc:
20916 {
20917 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20918 line_ptr += 2;
20919
20920 state_machine.handle_fixed_advance_pc (addr_adj);
20921 }
20922 break;
20923 default:
20924 {
20925 /* Unknown standard opcode, ignore it. */
20926 int i;
20927
20928 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20929 {
20930 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20931 line_ptr += bytes_read;
20932 }
20933 }
20934 }
20935 }
20936
20937 if (!end_sequence)
20938 dwarf2_debug_line_missing_end_sequence_complaint ();
20939
20940 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20941 in which case we still finish recording the last line). */
20942 state_machine.record_line (true);
20943 }
20944 }
20945
20946 /* Decode the Line Number Program (LNP) for the given line_header
20947 structure and CU. The actual information extracted and the type
20948 of structures created from the LNP depends on the value of PST.
20949
20950 1. If PST is NULL, then this procedure uses the data from the program
20951 to create all necessary symbol tables, and their linetables.
20952
20953 2. If PST is not NULL, this procedure reads the program to determine
20954 the list of files included by the unit represented by PST, and
20955 builds all the associated partial symbol tables.
20956
20957 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20958 It is used for relative paths in the line table.
20959 NOTE: When processing partial symtabs (pst != NULL),
20960 comp_dir == pst->dirname.
20961
20962 NOTE: It is important that psymtabs have the same file name (via strcmp)
20963 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20964 symtab we don't use it in the name of the psymtabs we create.
20965 E.g. expand_line_sal requires this when finding psymtabs to expand.
20966 A good testcase for this is mb-inline.exp.
20967
20968 LOWPC is the lowest address in CU (or 0 if not known).
20969
20970 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20971 for its PC<->lines mapping information. Otherwise only the filename
20972 table is read in. */
20973
20974 static void
20975 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20976 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20977 CORE_ADDR lowpc, int decode_mapping)
20978 {
20979 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20980 const int decode_for_pst_p = (pst != NULL);
20981
20982 if (decode_mapping)
20983 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20984
20985 if (decode_for_pst_p)
20986 {
20987 /* Now that we're done scanning the Line Header Program, we can
20988 create the psymtab of each included file. */
20989 for (auto &file_entry : lh->file_names ())
20990 if (file_entry.included_p == 1)
20991 {
20992 gdb::unique_xmalloc_ptr<char> name_holder;
20993 const char *include_name =
20994 psymtab_include_file_name (lh, file_entry, pst,
20995 comp_dir, &name_holder);
20996 if (include_name != NULL)
20997 dwarf2_create_include_psymtab (include_name, pst, objfile);
20998 }
20999 }
21000 else
21001 {
21002 /* Make sure a symtab is created for every file, even files
21003 which contain only variables (i.e. no code with associated
21004 line numbers). */
21005 buildsym_compunit *builder = cu->get_builder ();
21006 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21007
21008 for (auto &fe : lh->file_names ())
21009 {
21010 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21011 if (builder->get_current_subfile ()->symtab == NULL)
21012 {
21013 builder->get_current_subfile ()->symtab
21014 = allocate_symtab (cust,
21015 builder->get_current_subfile ()->name);
21016 }
21017 fe.symtab = builder->get_current_subfile ()->symtab;
21018 }
21019 }
21020 }
21021
21022 /* Start a subfile for DWARF. FILENAME is the name of the file and
21023 DIRNAME the name of the source directory which contains FILENAME
21024 or NULL if not known.
21025 This routine tries to keep line numbers from identical absolute and
21026 relative file names in a common subfile.
21027
21028 Using the `list' example from the GDB testsuite, which resides in
21029 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21030 of /srcdir/list0.c yields the following debugging information for list0.c:
21031
21032 DW_AT_name: /srcdir/list0.c
21033 DW_AT_comp_dir: /compdir
21034 files.files[0].name: list0.h
21035 files.files[0].dir: /srcdir
21036 files.files[1].name: list0.c
21037 files.files[1].dir: /srcdir
21038
21039 The line number information for list0.c has to end up in a single
21040 subfile, so that `break /srcdir/list0.c:1' works as expected.
21041 start_subfile will ensure that this happens provided that we pass the
21042 concatenation of files.files[1].dir and files.files[1].name as the
21043 subfile's name. */
21044
21045 static void
21046 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21047 const char *dirname)
21048 {
21049 gdb::unique_xmalloc_ptr<char> copy;
21050
21051 /* In order not to lose the line information directory,
21052 we concatenate it to the filename when it makes sense.
21053 Note that the Dwarf3 standard says (speaking of filenames in line
21054 information): ``The directory index is ignored for file names
21055 that represent full path names''. Thus ignoring dirname in the
21056 `else' branch below isn't an issue. */
21057
21058 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21059 {
21060 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21061 filename = copy.get ();
21062 }
21063
21064 cu->get_builder ()->start_subfile (filename);
21065 }
21066
21067 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21068 buildsym_compunit constructor. */
21069
21070 struct compunit_symtab *
21071 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21072 CORE_ADDR low_pc)
21073 {
21074 gdb_assert (m_builder == nullptr);
21075
21076 m_builder.reset (new struct buildsym_compunit
21077 (per_cu->dwarf2_per_objfile->objfile,
21078 name, comp_dir, language, low_pc));
21079
21080 list_in_scope = get_builder ()->get_file_symbols ();
21081
21082 get_builder ()->record_debugformat ("DWARF 2");
21083 get_builder ()->record_producer (producer);
21084
21085 processing_has_namespace_info = false;
21086
21087 return get_builder ()->get_compunit_symtab ();
21088 }
21089
21090 static void
21091 var_decode_location (struct attribute *attr, struct symbol *sym,
21092 struct dwarf2_cu *cu)
21093 {
21094 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21095 struct comp_unit_head *cu_header = &cu->header;
21096
21097 /* NOTE drow/2003-01-30: There used to be a comment and some special
21098 code here to turn a symbol with DW_AT_external and a
21099 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21100 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21101 with some versions of binutils) where shared libraries could have
21102 relocations against symbols in their debug information - the
21103 minimal symbol would have the right address, but the debug info
21104 would not. It's no longer necessary, because we will explicitly
21105 apply relocations when we read in the debug information now. */
21106
21107 /* A DW_AT_location attribute with no contents indicates that a
21108 variable has been optimized away. */
21109 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21110 {
21111 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21112 return;
21113 }
21114
21115 /* Handle one degenerate form of location expression specially, to
21116 preserve GDB's previous behavior when section offsets are
21117 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21118 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21119
21120 if (attr->form_is_block ()
21121 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21122 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21123 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21124 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21125 && (DW_BLOCK (attr)->size
21126 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21127 {
21128 unsigned int dummy;
21129
21130 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21131 SET_SYMBOL_VALUE_ADDRESS (sym,
21132 read_address (objfile->obfd,
21133 DW_BLOCK (attr)->data + 1,
21134 cu, &dummy));
21135 else
21136 SET_SYMBOL_VALUE_ADDRESS
21137 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21138 &dummy));
21139 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21140 fixup_symbol_section (sym, objfile);
21141 SET_SYMBOL_VALUE_ADDRESS
21142 (sym,
21143 SYMBOL_VALUE_ADDRESS (sym)
21144 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21145 return;
21146 }
21147
21148 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21149 expression evaluator, and use LOC_COMPUTED only when necessary
21150 (i.e. when the value of a register or memory location is
21151 referenced, or a thread-local block, etc.). Then again, it might
21152 not be worthwhile. I'm assuming that it isn't unless performance
21153 or memory numbers show me otherwise. */
21154
21155 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21156
21157 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21158 cu->has_loclist = true;
21159 }
21160
21161 /* Given a pointer to a DWARF information entry, figure out if we need
21162 to make a symbol table entry for it, and if so, create a new entry
21163 and return a pointer to it.
21164 If TYPE is NULL, determine symbol type from the die, otherwise
21165 used the passed type.
21166 If SPACE is not NULL, use it to hold the new symbol. If it is
21167 NULL, allocate a new symbol on the objfile's obstack. */
21168
21169 static struct symbol *
21170 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21171 struct symbol *space)
21172 {
21173 struct dwarf2_per_objfile *dwarf2_per_objfile
21174 = cu->per_cu->dwarf2_per_objfile;
21175 struct objfile *objfile = dwarf2_per_objfile->objfile;
21176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21177 struct symbol *sym = NULL;
21178 const char *name;
21179 struct attribute *attr = NULL;
21180 struct attribute *attr2 = NULL;
21181 CORE_ADDR baseaddr;
21182 struct pending **list_to_add = NULL;
21183
21184 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21185
21186 baseaddr = objfile->text_section_offset ();
21187
21188 name = dwarf2_name (die, cu);
21189 if (name)
21190 {
21191 const char *linkagename;
21192 int suppress_add = 0;
21193
21194 if (space)
21195 sym = space;
21196 else
21197 sym = allocate_symbol (objfile);
21198 OBJSTAT (objfile, n_syms++);
21199
21200 /* Cache this symbol's name and the name's demangled form (if any). */
21201 sym->set_language (cu->language, &objfile->objfile_obstack);
21202 linkagename = dwarf2_physname (name, die, cu);
21203 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21204
21205 /* Fortran does not have mangling standard and the mangling does differ
21206 between gfortran, iFort etc. */
21207 if (cu->language == language_fortran
21208 && symbol_get_demangled_name (sym) == NULL)
21209 symbol_set_demangled_name (sym,
21210 dwarf2_full_name (name, die, cu),
21211 NULL);
21212
21213 /* Default assumptions.
21214 Use the passed type or decode it from the die. */
21215 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21216 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21217 if (type != NULL)
21218 SYMBOL_TYPE (sym) = type;
21219 else
21220 SYMBOL_TYPE (sym) = die_type (die, cu);
21221 attr = dwarf2_attr (die,
21222 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21223 cu);
21224 if (attr != nullptr)
21225 {
21226 SYMBOL_LINE (sym) = DW_UNSND (attr);
21227 }
21228
21229 attr = dwarf2_attr (die,
21230 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21231 cu);
21232 if (attr != nullptr)
21233 {
21234 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21235 struct file_entry *fe;
21236
21237 if (cu->line_header != NULL)
21238 fe = cu->line_header->file_name_at (file_index);
21239 else
21240 fe = NULL;
21241
21242 if (fe == NULL)
21243 complaint (_("file index out of range"));
21244 else
21245 symbol_set_symtab (sym, fe->symtab);
21246 }
21247
21248 switch (die->tag)
21249 {
21250 case DW_TAG_label:
21251 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21252 if (attr != nullptr)
21253 {
21254 CORE_ADDR addr;
21255
21256 addr = attr->value_as_address ();
21257 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21258 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21259 }
21260 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21261 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21262 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21263 add_symbol_to_list (sym, cu->list_in_scope);
21264 break;
21265 case DW_TAG_subprogram:
21266 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21267 finish_block. */
21268 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21269 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21270 if ((attr2 && (DW_UNSND (attr2) != 0))
21271 || cu->language == language_ada
21272 || cu->language == language_fortran)
21273 {
21274 /* Subprograms marked external are stored as a global symbol.
21275 Ada and Fortran subprograms, whether marked external or
21276 not, are always stored as a global symbol, because we want
21277 to be able to access them globally. For instance, we want
21278 to be able to break on a nested subprogram without having
21279 to specify the context. */
21280 list_to_add = cu->get_builder ()->get_global_symbols ();
21281 }
21282 else
21283 {
21284 list_to_add = cu->list_in_scope;
21285 }
21286 break;
21287 case DW_TAG_inlined_subroutine:
21288 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21289 finish_block. */
21290 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21291 SYMBOL_INLINED (sym) = 1;
21292 list_to_add = cu->list_in_scope;
21293 break;
21294 case DW_TAG_template_value_param:
21295 suppress_add = 1;
21296 /* Fall through. */
21297 case DW_TAG_constant:
21298 case DW_TAG_variable:
21299 case DW_TAG_member:
21300 /* Compilation with minimal debug info may result in
21301 variables with missing type entries. Change the
21302 misleading `void' type to something sensible. */
21303 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21304 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21305
21306 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21307 /* In the case of DW_TAG_member, we should only be called for
21308 static const members. */
21309 if (die->tag == DW_TAG_member)
21310 {
21311 /* dwarf2_add_field uses die_is_declaration,
21312 so we do the same. */
21313 gdb_assert (die_is_declaration (die, cu));
21314 gdb_assert (attr);
21315 }
21316 if (attr != nullptr)
21317 {
21318 dwarf2_const_value (attr, sym, cu);
21319 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21320 if (!suppress_add)
21321 {
21322 if (attr2 && (DW_UNSND (attr2) != 0))
21323 list_to_add = cu->get_builder ()->get_global_symbols ();
21324 else
21325 list_to_add = cu->list_in_scope;
21326 }
21327 break;
21328 }
21329 attr = dwarf2_attr (die, DW_AT_location, cu);
21330 if (attr != nullptr)
21331 {
21332 var_decode_location (attr, sym, cu);
21333 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21334
21335 /* Fortran explicitly imports any global symbols to the local
21336 scope by DW_TAG_common_block. */
21337 if (cu->language == language_fortran && die->parent
21338 && die->parent->tag == DW_TAG_common_block)
21339 attr2 = NULL;
21340
21341 if (SYMBOL_CLASS (sym) == LOC_STATIC
21342 && SYMBOL_VALUE_ADDRESS (sym) == 0
21343 && !dwarf2_per_objfile->has_section_at_zero)
21344 {
21345 /* When a static variable is eliminated by the linker,
21346 the corresponding debug information is not stripped
21347 out, but the variable address is set to null;
21348 do not add such variables into symbol table. */
21349 }
21350 else if (attr2 && (DW_UNSND (attr2) != 0))
21351 {
21352 if (SYMBOL_CLASS (sym) == LOC_STATIC
21353 && (objfile->flags & OBJF_MAINLINE) == 0
21354 && dwarf2_per_objfile->can_copy)
21355 {
21356 /* A global static variable might be subject to
21357 copy relocation. We first check for a local
21358 minsym, though, because maybe the symbol was
21359 marked hidden, in which case this would not
21360 apply. */
21361 bound_minimal_symbol found
21362 = (lookup_minimal_symbol_linkage
21363 (sym->linkage_name (), objfile));
21364 if (found.minsym != nullptr)
21365 sym->maybe_copied = 1;
21366 }
21367
21368 /* A variable with DW_AT_external is never static,
21369 but it may be block-scoped. */
21370 list_to_add
21371 = ((cu->list_in_scope
21372 == cu->get_builder ()->get_file_symbols ())
21373 ? cu->get_builder ()->get_global_symbols ()
21374 : cu->list_in_scope);
21375 }
21376 else
21377 list_to_add = cu->list_in_scope;
21378 }
21379 else
21380 {
21381 /* We do not know the address of this symbol.
21382 If it is an external symbol and we have type information
21383 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21384 The address of the variable will then be determined from
21385 the minimal symbol table whenever the variable is
21386 referenced. */
21387 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21388
21389 /* Fortran explicitly imports any global symbols to the local
21390 scope by DW_TAG_common_block. */
21391 if (cu->language == language_fortran && die->parent
21392 && die->parent->tag == DW_TAG_common_block)
21393 {
21394 /* SYMBOL_CLASS doesn't matter here because
21395 read_common_block is going to reset it. */
21396 if (!suppress_add)
21397 list_to_add = cu->list_in_scope;
21398 }
21399 else if (attr2 && (DW_UNSND (attr2) != 0)
21400 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21401 {
21402 /* A variable with DW_AT_external is never static, but it
21403 may be block-scoped. */
21404 list_to_add
21405 = ((cu->list_in_scope
21406 == cu->get_builder ()->get_file_symbols ())
21407 ? cu->get_builder ()->get_global_symbols ()
21408 : cu->list_in_scope);
21409
21410 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21411 }
21412 else if (!die_is_declaration (die, cu))
21413 {
21414 /* Use the default LOC_OPTIMIZED_OUT class. */
21415 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21416 if (!suppress_add)
21417 list_to_add = cu->list_in_scope;
21418 }
21419 }
21420 break;
21421 case DW_TAG_formal_parameter:
21422 {
21423 /* If we are inside a function, mark this as an argument. If
21424 not, we might be looking at an argument to an inlined function
21425 when we do not have enough information to show inlined frames;
21426 pretend it's a local variable in that case so that the user can
21427 still see it. */
21428 struct context_stack *curr
21429 = cu->get_builder ()->get_current_context_stack ();
21430 if (curr != nullptr && curr->name != nullptr)
21431 SYMBOL_IS_ARGUMENT (sym) = 1;
21432 attr = dwarf2_attr (die, DW_AT_location, cu);
21433 if (attr != nullptr)
21434 {
21435 var_decode_location (attr, sym, cu);
21436 }
21437 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21438 if (attr != nullptr)
21439 {
21440 dwarf2_const_value (attr, sym, cu);
21441 }
21442
21443 list_to_add = cu->list_in_scope;
21444 }
21445 break;
21446 case DW_TAG_unspecified_parameters:
21447 /* From varargs functions; gdb doesn't seem to have any
21448 interest in this information, so just ignore it for now.
21449 (FIXME?) */
21450 break;
21451 case DW_TAG_template_type_param:
21452 suppress_add = 1;
21453 /* Fall through. */
21454 case DW_TAG_class_type:
21455 case DW_TAG_interface_type:
21456 case DW_TAG_structure_type:
21457 case DW_TAG_union_type:
21458 case DW_TAG_set_type:
21459 case DW_TAG_enumeration_type:
21460 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21461 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21462
21463 {
21464 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21465 really ever be static objects: otherwise, if you try
21466 to, say, break of a class's method and you're in a file
21467 which doesn't mention that class, it won't work unless
21468 the check for all static symbols in lookup_symbol_aux
21469 saves you. See the OtherFileClass tests in
21470 gdb.c++/namespace.exp. */
21471
21472 if (!suppress_add)
21473 {
21474 buildsym_compunit *builder = cu->get_builder ();
21475 list_to_add
21476 = (cu->list_in_scope == builder->get_file_symbols ()
21477 && cu->language == language_cplus
21478 ? builder->get_global_symbols ()
21479 : cu->list_in_scope);
21480
21481 /* The semantics of C++ state that "struct foo {
21482 ... }" also defines a typedef for "foo". */
21483 if (cu->language == language_cplus
21484 || cu->language == language_ada
21485 || cu->language == language_d
21486 || cu->language == language_rust)
21487 {
21488 /* The symbol's name is already allocated along
21489 with this objfile, so we don't need to
21490 duplicate it for the type. */
21491 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21492 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21493 }
21494 }
21495 }
21496 break;
21497 case DW_TAG_typedef:
21498 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21499 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21500 list_to_add = cu->list_in_scope;
21501 break;
21502 case DW_TAG_base_type:
21503 case DW_TAG_subrange_type:
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21505 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21506 list_to_add = cu->list_in_scope;
21507 break;
21508 case DW_TAG_enumerator:
21509 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21510 if (attr != nullptr)
21511 {
21512 dwarf2_const_value (attr, sym, cu);
21513 }
21514 {
21515 /* NOTE: carlton/2003-11-10: See comment above in the
21516 DW_TAG_class_type, etc. block. */
21517
21518 list_to_add
21519 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21520 && cu->language == language_cplus
21521 ? cu->get_builder ()->get_global_symbols ()
21522 : cu->list_in_scope);
21523 }
21524 break;
21525 case DW_TAG_imported_declaration:
21526 case DW_TAG_namespace:
21527 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21528 list_to_add = cu->get_builder ()->get_global_symbols ();
21529 break;
21530 case DW_TAG_module:
21531 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21532 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21533 list_to_add = cu->get_builder ()->get_global_symbols ();
21534 break;
21535 case DW_TAG_common_block:
21536 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21537 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21538 add_symbol_to_list (sym, cu->list_in_scope);
21539 break;
21540 default:
21541 /* Not a tag we recognize. Hopefully we aren't processing
21542 trash data, but since we must specifically ignore things
21543 we don't recognize, there is nothing else we should do at
21544 this point. */
21545 complaint (_("unsupported tag: '%s'"),
21546 dwarf_tag_name (die->tag));
21547 break;
21548 }
21549
21550 if (suppress_add)
21551 {
21552 sym->hash_next = objfile->template_symbols;
21553 objfile->template_symbols = sym;
21554 list_to_add = NULL;
21555 }
21556
21557 if (list_to_add != NULL)
21558 add_symbol_to_list (sym, list_to_add);
21559
21560 /* For the benefit of old versions of GCC, check for anonymous
21561 namespaces based on the demangled name. */
21562 if (!cu->processing_has_namespace_info
21563 && cu->language == language_cplus)
21564 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21565 }
21566 return (sym);
21567 }
21568
21569 /* Given an attr with a DW_FORM_dataN value in host byte order,
21570 zero-extend it as appropriate for the symbol's type. The DWARF
21571 standard (v4) is not entirely clear about the meaning of using
21572 DW_FORM_dataN for a constant with a signed type, where the type is
21573 wider than the data. The conclusion of a discussion on the DWARF
21574 list was that this is unspecified. We choose to always zero-extend
21575 because that is the interpretation long in use by GCC. */
21576
21577 static gdb_byte *
21578 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21579 struct dwarf2_cu *cu, LONGEST *value, int bits)
21580 {
21581 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21582 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21583 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21584 LONGEST l = DW_UNSND (attr);
21585
21586 if (bits < sizeof (*value) * 8)
21587 {
21588 l &= ((LONGEST) 1 << bits) - 1;
21589 *value = l;
21590 }
21591 else if (bits == sizeof (*value) * 8)
21592 *value = l;
21593 else
21594 {
21595 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21596 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21597 return bytes;
21598 }
21599
21600 return NULL;
21601 }
21602
21603 /* Read a constant value from an attribute. Either set *VALUE, or if
21604 the value does not fit in *VALUE, set *BYTES - either already
21605 allocated on the objfile obstack, or newly allocated on OBSTACK,
21606 or, set *BATON, if we translated the constant to a location
21607 expression. */
21608
21609 static void
21610 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21611 const char *name, struct obstack *obstack,
21612 struct dwarf2_cu *cu,
21613 LONGEST *value, const gdb_byte **bytes,
21614 struct dwarf2_locexpr_baton **baton)
21615 {
21616 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21617 struct comp_unit_head *cu_header = &cu->header;
21618 struct dwarf_block *blk;
21619 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21620 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21621
21622 *value = 0;
21623 *bytes = NULL;
21624 *baton = NULL;
21625
21626 switch (attr->form)
21627 {
21628 case DW_FORM_addr:
21629 case DW_FORM_addrx:
21630 case DW_FORM_GNU_addr_index:
21631 {
21632 gdb_byte *data;
21633
21634 if (TYPE_LENGTH (type) != cu_header->addr_size)
21635 dwarf2_const_value_length_mismatch_complaint (name,
21636 cu_header->addr_size,
21637 TYPE_LENGTH (type));
21638 /* Symbols of this form are reasonably rare, so we just
21639 piggyback on the existing location code rather than writing
21640 a new implementation of symbol_computed_ops. */
21641 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21642 (*baton)->per_cu = cu->per_cu;
21643 gdb_assert ((*baton)->per_cu);
21644
21645 (*baton)->size = 2 + cu_header->addr_size;
21646 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21647 (*baton)->data = data;
21648
21649 data[0] = DW_OP_addr;
21650 store_unsigned_integer (&data[1], cu_header->addr_size,
21651 byte_order, DW_ADDR (attr));
21652 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21653 }
21654 break;
21655 case DW_FORM_string:
21656 case DW_FORM_strp:
21657 case DW_FORM_strx:
21658 case DW_FORM_GNU_str_index:
21659 case DW_FORM_GNU_strp_alt:
21660 /* DW_STRING is already allocated on the objfile obstack, point
21661 directly to it. */
21662 *bytes = (const gdb_byte *) DW_STRING (attr);
21663 break;
21664 case DW_FORM_block1:
21665 case DW_FORM_block2:
21666 case DW_FORM_block4:
21667 case DW_FORM_block:
21668 case DW_FORM_exprloc:
21669 case DW_FORM_data16:
21670 blk = DW_BLOCK (attr);
21671 if (TYPE_LENGTH (type) != blk->size)
21672 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21673 TYPE_LENGTH (type));
21674 *bytes = blk->data;
21675 break;
21676
21677 /* The DW_AT_const_value attributes are supposed to carry the
21678 symbol's value "represented as it would be on the target
21679 architecture." By the time we get here, it's already been
21680 converted to host endianness, so we just need to sign- or
21681 zero-extend it as appropriate. */
21682 case DW_FORM_data1:
21683 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21684 break;
21685 case DW_FORM_data2:
21686 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21687 break;
21688 case DW_FORM_data4:
21689 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21690 break;
21691 case DW_FORM_data8:
21692 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21693 break;
21694
21695 case DW_FORM_sdata:
21696 case DW_FORM_implicit_const:
21697 *value = DW_SND (attr);
21698 break;
21699
21700 case DW_FORM_udata:
21701 *value = DW_UNSND (attr);
21702 break;
21703
21704 default:
21705 complaint (_("unsupported const value attribute form: '%s'"),
21706 dwarf_form_name (attr->form));
21707 *value = 0;
21708 break;
21709 }
21710 }
21711
21712
21713 /* Copy constant value from an attribute to a symbol. */
21714
21715 static void
21716 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21717 struct dwarf2_cu *cu)
21718 {
21719 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21720 LONGEST value;
21721 const gdb_byte *bytes;
21722 struct dwarf2_locexpr_baton *baton;
21723
21724 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21725 sym->print_name (),
21726 &objfile->objfile_obstack, cu,
21727 &value, &bytes, &baton);
21728
21729 if (baton != NULL)
21730 {
21731 SYMBOL_LOCATION_BATON (sym) = baton;
21732 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21733 }
21734 else if (bytes != NULL)
21735 {
21736 SYMBOL_VALUE_BYTES (sym) = bytes;
21737 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21738 }
21739 else
21740 {
21741 SYMBOL_VALUE (sym) = value;
21742 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21743 }
21744 }
21745
21746 /* Return the type of the die in question using its DW_AT_type attribute. */
21747
21748 static struct type *
21749 die_type (struct die_info *die, struct dwarf2_cu *cu)
21750 {
21751 struct attribute *type_attr;
21752
21753 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21754 if (!type_attr)
21755 {
21756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21757 /* A missing DW_AT_type represents a void type. */
21758 return objfile_type (objfile)->builtin_void;
21759 }
21760
21761 return lookup_die_type (die, type_attr, cu);
21762 }
21763
21764 /* True iff CU's producer generates GNAT Ada auxiliary information
21765 that allows to find parallel types through that information instead
21766 of having to do expensive parallel lookups by type name. */
21767
21768 static int
21769 need_gnat_info (struct dwarf2_cu *cu)
21770 {
21771 /* Assume that the Ada compiler was GNAT, which always produces
21772 the auxiliary information. */
21773 return (cu->language == language_ada);
21774 }
21775
21776 /* Return the auxiliary type of the die in question using its
21777 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21778 attribute is not present. */
21779
21780 static struct type *
21781 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21782 {
21783 struct attribute *type_attr;
21784
21785 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21786 if (!type_attr)
21787 return NULL;
21788
21789 return lookup_die_type (die, type_attr, cu);
21790 }
21791
21792 /* If DIE has a descriptive_type attribute, then set the TYPE's
21793 descriptive type accordingly. */
21794
21795 static void
21796 set_descriptive_type (struct type *type, struct die_info *die,
21797 struct dwarf2_cu *cu)
21798 {
21799 struct type *descriptive_type = die_descriptive_type (die, cu);
21800
21801 if (descriptive_type)
21802 {
21803 ALLOCATE_GNAT_AUX_TYPE (type);
21804 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21805 }
21806 }
21807
21808 /* Return the containing type of the die in question using its
21809 DW_AT_containing_type attribute. */
21810
21811 static struct type *
21812 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21813 {
21814 struct attribute *type_attr;
21815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21816
21817 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21818 if (!type_attr)
21819 error (_("Dwarf Error: Problem turning containing type into gdb type "
21820 "[in module %s]"), objfile_name (objfile));
21821
21822 return lookup_die_type (die, type_attr, cu);
21823 }
21824
21825 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21826
21827 static struct type *
21828 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21829 {
21830 struct dwarf2_per_objfile *dwarf2_per_objfile
21831 = cu->per_cu->dwarf2_per_objfile;
21832 struct objfile *objfile = dwarf2_per_objfile->objfile;
21833 char *saved;
21834
21835 std::string message
21836 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21837 objfile_name (objfile),
21838 sect_offset_str (cu->header.sect_off),
21839 sect_offset_str (die->sect_off));
21840 saved = obstack_strdup (&objfile->objfile_obstack, message);
21841
21842 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21843 }
21844
21845 /* Look up the type of DIE in CU using its type attribute ATTR.
21846 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21847 DW_AT_containing_type.
21848 If there is no type substitute an error marker. */
21849
21850 static struct type *
21851 lookup_die_type (struct die_info *die, const struct attribute *attr,
21852 struct dwarf2_cu *cu)
21853 {
21854 struct dwarf2_per_objfile *dwarf2_per_objfile
21855 = cu->per_cu->dwarf2_per_objfile;
21856 struct objfile *objfile = dwarf2_per_objfile->objfile;
21857 struct type *this_type;
21858
21859 gdb_assert (attr->name == DW_AT_type
21860 || attr->name == DW_AT_GNAT_descriptive_type
21861 || attr->name == DW_AT_containing_type);
21862
21863 /* First see if we have it cached. */
21864
21865 if (attr->form == DW_FORM_GNU_ref_alt)
21866 {
21867 struct dwarf2_per_cu_data *per_cu;
21868 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21869
21870 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21871 dwarf2_per_objfile);
21872 this_type = get_die_type_at_offset (sect_off, per_cu);
21873 }
21874 else if (attr->form_is_ref ())
21875 {
21876 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21877
21878 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21879 }
21880 else if (attr->form == DW_FORM_ref_sig8)
21881 {
21882 ULONGEST signature = DW_SIGNATURE (attr);
21883
21884 return get_signatured_type (die, signature, cu);
21885 }
21886 else
21887 {
21888 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21889 " at %s [in module %s]"),
21890 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21891 objfile_name (objfile));
21892 return build_error_marker_type (cu, die);
21893 }
21894
21895 /* If not cached we need to read it in. */
21896
21897 if (this_type == NULL)
21898 {
21899 struct die_info *type_die = NULL;
21900 struct dwarf2_cu *type_cu = cu;
21901
21902 if (attr->form_is_ref ())
21903 type_die = follow_die_ref (die, attr, &type_cu);
21904 if (type_die == NULL)
21905 return build_error_marker_type (cu, die);
21906 /* If we find the type now, it's probably because the type came
21907 from an inter-CU reference and the type's CU got expanded before
21908 ours. */
21909 this_type = read_type_die (type_die, type_cu);
21910 }
21911
21912 /* If we still don't have a type use an error marker. */
21913
21914 if (this_type == NULL)
21915 return build_error_marker_type (cu, die);
21916
21917 return this_type;
21918 }
21919
21920 /* Return the type in DIE, CU.
21921 Returns NULL for invalid types.
21922
21923 This first does a lookup in die_type_hash,
21924 and only reads the die in if necessary.
21925
21926 NOTE: This can be called when reading in partial or full symbols. */
21927
21928 static struct type *
21929 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21930 {
21931 struct type *this_type;
21932
21933 this_type = get_die_type (die, cu);
21934 if (this_type)
21935 return this_type;
21936
21937 return read_type_die_1 (die, cu);
21938 }
21939
21940 /* Read the type in DIE, CU.
21941 Returns NULL for invalid types. */
21942
21943 static struct type *
21944 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21945 {
21946 struct type *this_type = NULL;
21947
21948 switch (die->tag)
21949 {
21950 case DW_TAG_class_type:
21951 case DW_TAG_interface_type:
21952 case DW_TAG_structure_type:
21953 case DW_TAG_union_type:
21954 this_type = read_structure_type (die, cu);
21955 break;
21956 case DW_TAG_enumeration_type:
21957 this_type = read_enumeration_type (die, cu);
21958 break;
21959 case DW_TAG_subprogram:
21960 case DW_TAG_subroutine_type:
21961 case DW_TAG_inlined_subroutine:
21962 this_type = read_subroutine_type (die, cu);
21963 break;
21964 case DW_TAG_array_type:
21965 this_type = read_array_type (die, cu);
21966 break;
21967 case DW_TAG_set_type:
21968 this_type = read_set_type (die, cu);
21969 break;
21970 case DW_TAG_pointer_type:
21971 this_type = read_tag_pointer_type (die, cu);
21972 break;
21973 case DW_TAG_ptr_to_member_type:
21974 this_type = read_tag_ptr_to_member_type (die, cu);
21975 break;
21976 case DW_TAG_reference_type:
21977 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21978 break;
21979 case DW_TAG_rvalue_reference_type:
21980 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21981 break;
21982 case DW_TAG_const_type:
21983 this_type = read_tag_const_type (die, cu);
21984 break;
21985 case DW_TAG_volatile_type:
21986 this_type = read_tag_volatile_type (die, cu);
21987 break;
21988 case DW_TAG_restrict_type:
21989 this_type = read_tag_restrict_type (die, cu);
21990 break;
21991 case DW_TAG_string_type:
21992 this_type = read_tag_string_type (die, cu);
21993 break;
21994 case DW_TAG_typedef:
21995 this_type = read_typedef (die, cu);
21996 break;
21997 case DW_TAG_subrange_type:
21998 this_type = read_subrange_type (die, cu);
21999 break;
22000 case DW_TAG_base_type:
22001 this_type = read_base_type (die, cu);
22002 break;
22003 case DW_TAG_unspecified_type:
22004 this_type = read_unspecified_type (die, cu);
22005 break;
22006 case DW_TAG_namespace:
22007 this_type = read_namespace_type (die, cu);
22008 break;
22009 case DW_TAG_module:
22010 this_type = read_module_type (die, cu);
22011 break;
22012 case DW_TAG_atomic_type:
22013 this_type = read_tag_atomic_type (die, cu);
22014 break;
22015 default:
22016 complaint (_("unexpected tag in read_type_die: '%s'"),
22017 dwarf_tag_name (die->tag));
22018 break;
22019 }
22020
22021 return this_type;
22022 }
22023
22024 /* See if we can figure out if the class lives in a namespace. We do
22025 this by looking for a member function; its demangled name will
22026 contain namespace info, if there is any.
22027 Return the computed name or NULL.
22028 Space for the result is allocated on the objfile's obstack.
22029 This is the full-die version of guess_partial_die_structure_name.
22030 In this case we know DIE has no useful parent. */
22031
22032 static const char *
22033 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22034 {
22035 struct die_info *spec_die;
22036 struct dwarf2_cu *spec_cu;
22037 struct die_info *child;
22038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22039
22040 spec_cu = cu;
22041 spec_die = die_specification (die, &spec_cu);
22042 if (spec_die != NULL)
22043 {
22044 die = spec_die;
22045 cu = spec_cu;
22046 }
22047
22048 for (child = die->child;
22049 child != NULL;
22050 child = child->sibling)
22051 {
22052 if (child->tag == DW_TAG_subprogram)
22053 {
22054 const char *linkage_name = dw2_linkage_name (child, cu);
22055
22056 if (linkage_name != NULL)
22057 {
22058 gdb::unique_xmalloc_ptr<char> actual_name
22059 (language_class_name_from_physname (cu->language_defn,
22060 linkage_name));
22061 const char *name = NULL;
22062
22063 if (actual_name != NULL)
22064 {
22065 const char *die_name = dwarf2_name (die, cu);
22066
22067 if (die_name != NULL
22068 && strcmp (die_name, actual_name.get ()) != 0)
22069 {
22070 /* Strip off the class name from the full name.
22071 We want the prefix. */
22072 int die_name_len = strlen (die_name);
22073 int actual_name_len = strlen (actual_name.get ());
22074 const char *ptr = actual_name.get ();
22075
22076 /* Test for '::' as a sanity check. */
22077 if (actual_name_len > die_name_len + 2
22078 && ptr[actual_name_len - die_name_len - 1] == ':')
22079 name = obstack_strndup (
22080 &objfile->per_bfd->storage_obstack,
22081 ptr, actual_name_len - die_name_len - 2);
22082 }
22083 }
22084 return name;
22085 }
22086 }
22087 }
22088
22089 return NULL;
22090 }
22091
22092 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22093 prefix part in such case. See
22094 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22095
22096 static const char *
22097 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22098 {
22099 struct attribute *attr;
22100 const char *base;
22101
22102 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22103 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22104 return NULL;
22105
22106 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22107 return NULL;
22108
22109 attr = dw2_linkage_name_attr (die, cu);
22110 if (attr == NULL || DW_STRING (attr) == NULL)
22111 return NULL;
22112
22113 /* dwarf2_name had to be already called. */
22114 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22115
22116 /* Strip the base name, keep any leading namespaces/classes. */
22117 base = strrchr (DW_STRING (attr), ':');
22118 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22119 return "";
22120
22121 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22122 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22123 DW_STRING (attr),
22124 &base[-1] - DW_STRING (attr));
22125 }
22126
22127 /* Return the name of the namespace/class that DIE is defined within,
22128 or "" if we can't tell. The caller should not xfree the result.
22129
22130 For example, if we're within the method foo() in the following
22131 code:
22132
22133 namespace N {
22134 class C {
22135 void foo () {
22136 }
22137 };
22138 }
22139
22140 then determine_prefix on foo's die will return "N::C". */
22141
22142 static const char *
22143 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22144 {
22145 struct dwarf2_per_objfile *dwarf2_per_objfile
22146 = cu->per_cu->dwarf2_per_objfile;
22147 struct die_info *parent, *spec_die;
22148 struct dwarf2_cu *spec_cu;
22149 struct type *parent_type;
22150 const char *retval;
22151
22152 if (cu->language != language_cplus
22153 && cu->language != language_fortran && cu->language != language_d
22154 && cu->language != language_rust)
22155 return "";
22156
22157 retval = anonymous_struct_prefix (die, cu);
22158 if (retval)
22159 return retval;
22160
22161 /* We have to be careful in the presence of DW_AT_specification.
22162 For example, with GCC 3.4, given the code
22163
22164 namespace N {
22165 void foo() {
22166 // Definition of N::foo.
22167 }
22168 }
22169
22170 then we'll have a tree of DIEs like this:
22171
22172 1: DW_TAG_compile_unit
22173 2: DW_TAG_namespace // N
22174 3: DW_TAG_subprogram // declaration of N::foo
22175 4: DW_TAG_subprogram // definition of N::foo
22176 DW_AT_specification // refers to die #3
22177
22178 Thus, when processing die #4, we have to pretend that we're in
22179 the context of its DW_AT_specification, namely the contex of die
22180 #3. */
22181 spec_cu = cu;
22182 spec_die = die_specification (die, &spec_cu);
22183 if (spec_die == NULL)
22184 parent = die->parent;
22185 else
22186 {
22187 parent = spec_die->parent;
22188 cu = spec_cu;
22189 }
22190
22191 if (parent == NULL)
22192 return "";
22193 else if (parent->building_fullname)
22194 {
22195 const char *name;
22196 const char *parent_name;
22197
22198 /* It has been seen on RealView 2.2 built binaries,
22199 DW_TAG_template_type_param types actually _defined_ as
22200 children of the parent class:
22201
22202 enum E {};
22203 template class <class Enum> Class{};
22204 Class<enum E> class_e;
22205
22206 1: DW_TAG_class_type (Class)
22207 2: DW_TAG_enumeration_type (E)
22208 3: DW_TAG_enumerator (enum1:0)
22209 3: DW_TAG_enumerator (enum2:1)
22210 ...
22211 2: DW_TAG_template_type_param
22212 DW_AT_type DW_FORM_ref_udata (E)
22213
22214 Besides being broken debug info, it can put GDB into an
22215 infinite loop. Consider:
22216
22217 When we're building the full name for Class<E>, we'll start
22218 at Class, and go look over its template type parameters,
22219 finding E. We'll then try to build the full name of E, and
22220 reach here. We're now trying to build the full name of E,
22221 and look over the parent DIE for containing scope. In the
22222 broken case, if we followed the parent DIE of E, we'd again
22223 find Class, and once again go look at its template type
22224 arguments, etc., etc. Simply don't consider such parent die
22225 as source-level parent of this die (it can't be, the language
22226 doesn't allow it), and break the loop here. */
22227 name = dwarf2_name (die, cu);
22228 parent_name = dwarf2_name (parent, cu);
22229 complaint (_("template param type '%s' defined within parent '%s'"),
22230 name ? name : "<unknown>",
22231 parent_name ? parent_name : "<unknown>");
22232 return "";
22233 }
22234 else
22235 switch (parent->tag)
22236 {
22237 case DW_TAG_namespace:
22238 parent_type = read_type_die (parent, cu);
22239 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22240 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22241 Work around this problem here. */
22242 if (cu->language == language_cplus
22243 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22244 return "";
22245 /* We give a name to even anonymous namespaces. */
22246 return TYPE_NAME (parent_type);
22247 case DW_TAG_class_type:
22248 case DW_TAG_interface_type:
22249 case DW_TAG_structure_type:
22250 case DW_TAG_union_type:
22251 case DW_TAG_module:
22252 parent_type = read_type_die (parent, cu);
22253 if (TYPE_NAME (parent_type) != NULL)
22254 return TYPE_NAME (parent_type);
22255 else
22256 /* An anonymous structure is only allowed non-static data
22257 members; no typedefs, no member functions, et cetera.
22258 So it does not need a prefix. */
22259 return "";
22260 case DW_TAG_compile_unit:
22261 case DW_TAG_partial_unit:
22262 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22263 if (cu->language == language_cplus
22264 && !dwarf2_per_objfile->types.empty ()
22265 && die->child != NULL
22266 && (die->tag == DW_TAG_class_type
22267 || die->tag == DW_TAG_structure_type
22268 || die->tag == DW_TAG_union_type))
22269 {
22270 const char *name = guess_full_die_structure_name (die, cu);
22271 if (name != NULL)
22272 return name;
22273 }
22274 return "";
22275 case DW_TAG_subprogram:
22276 /* Nested subroutines in Fortran get a prefix with the name
22277 of the parent's subroutine. */
22278 if (cu->language == language_fortran)
22279 {
22280 if ((die->tag == DW_TAG_subprogram)
22281 && (dwarf2_name (parent, cu) != NULL))
22282 return dwarf2_name (parent, cu);
22283 }
22284 return determine_prefix (parent, cu);
22285 case DW_TAG_enumeration_type:
22286 parent_type = read_type_die (parent, cu);
22287 if (TYPE_DECLARED_CLASS (parent_type))
22288 {
22289 if (TYPE_NAME (parent_type) != NULL)
22290 return TYPE_NAME (parent_type);
22291 return "";
22292 }
22293 /* Fall through. */
22294 default:
22295 return determine_prefix (parent, cu);
22296 }
22297 }
22298
22299 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22300 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22301 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22302 an obconcat, otherwise allocate storage for the result. The CU argument is
22303 used to determine the language and hence, the appropriate separator. */
22304
22305 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22306
22307 static char *
22308 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22309 int physname, struct dwarf2_cu *cu)
22310 {
22311 const char *lead = "";
22312 const char *sep;
22313
22314 if (suffix == NULL || suffix[0] == '\0'
22315 || prefix == NULL || prefix[0] == '\0')
22316 sep = "";
22317 else if (cu->language == language_d)
22318 {
22319 /* For D, the 'main' function could be defined in any module, but it
22320 should never be prefixed. */
22321 if (strcmp (suffix, "D main") == 0)
22322 {
22323 prefix = "";
22324 sep = "";
22325 }
22326 else
22327 sep = ".";
22328 }
22329 else if (cu->language == language_fortran && physname)
22330 {
22331 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22332 DW_AT_MIPS_linkage_name is preferred and used instead. */
22333
22334 lead = "__";
22335 sep = "_MOD_";
22336 }
22337 else
22338 sep = "::";
22339
22340 if (prefix == NULL)
22341 prefix = "";
22342 if (suffix == NULL)
22343 suffix = "";
22344
22345 if (obs == NULL)
22346 {
22347 char *retval
22348 = ((char *)
22349 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22350
22351 strcpy (retval, lead);
22352 strcat (retval, prefix);
22353 strcat (retval, sep);
22354 strcat (retval, suffix);
22355 return retval;
22356 }
22357 else
22358 {
22359 /* We have an obstack. */
22360 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22361 }
22362 }
22363
22364 /* Return sibling of die, NULL if no sibling. */
22365
22366 static struct die_info *
22367 sibling_die (struct die_info *die)
22368 {
22369 return die->sibling;
22370 }
22371
22372 /* Get name of a die, return NULL if not found. */
22373
22374 static const char *
22375 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22376 struct obstack *obstack)
22377 {
22378 if (name && cu->language == language_cplus)
22379 {
22380 std::string canon_name = cp_canonicalize_string (name);
22381
22382 if (!canon_name.empty ())
22383 {
22384 if (canon_name != name)
22385 name = obstack_strdup (obstack, canon_name);
22386 }
22387 }
22388
22389 return name;
22390 }
22391
22392 /* Get name of a die, return NULL if not found.
22393 Anonymous namespaces are converted to their magic string. */
22394
22395 static const char *
22396 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22397 {
22398 struct attribute *attr;
22399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22400
22401 attr = dwarf2_attr (die, DW_AT_name, cu);
22402 if ((!attr || !DW_STRING (attr))
22403 && die->tag != DW_TAG_namespace
22404 && die->tag != DW_TAG_class_type
22405 && die->tag != DW_TAG_interface_type
22406 && die->tag != DW_TAG_structure_type
22407 && die->tag != DW_TAG_union_type)
22408 return NULL;
22409
22410 switch (die->tag)
22411 {
22412 case DW_TAG_compile_unit:
22413 case DW_TAG_partial_unit:
22414 /* Compilation units have a DW_AT_name that is a filename, not
22415 a source language identifier. */
22416 case DW_TAG_enumeration_type:
22417 case DW_TAG_enumerator:
22418 /* These tags always have simple identifiers already; no need
22419 to canonicalize them. */
22420 return DW_STRING (attr);
22421
22422 case DW_TAG_namespace:
22423 if (attr != NULL && DW_STRING (attr) != NULL)
22424 return DW_STRING (attr);
22425 return CP_ANONYMOUS_NAMESPACE_STR;
22426
22427 case DW_TAG_class_type:
22428 case DW_TAG_interface_type:
22429 case DW_TAG_structure_type:
22430 case DW_TAG_union_type:
22431 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22432 structures or unions. These were of the form "._%d" in GCC 4.1,
22433 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22434 and GCC 4.4. We work around this problem by ignoring these. */
22435 if (attr && DW_STRING (attr)
22436 && (startswith (DW_STRING (attr), "._")
22437 || startswith (DW_STRING (attr), "<anonymous")))
22438 return NULL;
22439
22440 /* GCC might emit a nameless typedef that has a linkage name. See
22441 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22442 if (!attr || DW_STRING (attr) == NULL)
22443 {
22444 attr = dw2_linkage_name_attr (die, cu);
22445 if (attr == NULL || DW_STRING (attr) == NULL)
22446 return NULL;
22447
22448 /* Avoid demangling DW_STRING (attr) the second time on a second
22449 call for the same DIE. */
22450 if (!DW_STRING_IS_CANONICAL (attr))
22451 {
22452 gdb::unique_xmalloc_ptr<char> demangled
22453 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22454
22455 const char *base;
22456
22457 /* FIXME: we already did this for the partial symbol... */
22458 DW_STRING (attr)
22459 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22460 demangled.get ());
22461 DW_STRING_IS_CANONICAL (attr) = 1;
22462
22463 /* Strip any leading namespaces/classes, keep only the base name.
22464 DW_AT_name for named DIEs does not contain the prefixes. */
22465 base = strrchr (DW_STRING (attr), ':');
22466 if (base && base > DW_STRING (attr) && base[-1] == ':')
22467 return &base[1];
22468 else
22469 return DW_STRING (attr);
22470 }
22471 }
22472 break;
22473
22474 default:
22475 break;
22476 }
22477
22478 if (!DW_STRING_IS_CANONICAL (attr))
22479 {
22480 DW_STRING (attr)
22481 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22482 &objfile->per_bfd->storage_obstack);
22483 DW_STRING_IS_CANONICAL (attr) = 1;
22484 }
22485 return DW_STRING (attr);
22486 }
22487
22488 /* Return the die that this die in an extension of, or NULL if there
22489 is none. *EXT_CU is the CU containing DIE on input, and the CU
22490 containing the return value on output. */
22491
22492 static struct die_info *
22493 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22494 {
22495 struct attribute *attr;
22496
22497 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22498 if (attr == NULL)
22499 return NULL;
22500
22501 return follow_die_ref (die, attr, ext_cu);
22502 }
22503
22504 /* A convenience function that returns an "unknown" DWARF name,
22505 including the value of V. STR is the name of the entity being
22506 printed, e.g., "TAG". */
22507
22508 static const char *
22509 dwarf_unknown (const char *str, unsigned v)
22510 {
22511 char *cell = get_print_cell ();
22512 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22513 return cell;
22514 }
22515
22516 /* Convert a DIE tag into its string name. */
22517
22518 static const char *
22519 dwarf_tag_name (unsigned tag)
22520 {
22521 const char *name = get_DW_TAG_name (tag);
22522
22523 if (name == NULL)
22524 return dwarf_unknown ("TAG", tag);
22525
22526 return name;
22527 }
22528
22529 /* Convert a DWARF attribute code into its string name. */
22530
22531 static const char *
22532 dwarf_attr_name (unsigned attr)
22533 {
22534 const char *name;
22535
22536 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22537 if (attr == DW_AT_MIPS_fde)
22538 return "DW_AT_MIPS_fde";
22539 #else
22540 if (attr == DW_AT_HP_block_index)
22541 return "DW_AT_HP_block_index";
22542 #endif
22543
22544 name = get_DW_AT_name (attr);
22545
22546 if (name == NULL)
22547 return dwarf_unknown ("AT", attr);
22548
22549 return name;
22550 }
22551
22552 /* Convert a unit type to corresponding DW_UT name. */
22553
22554 static const char *
22555 dwarf_unit_type_name (int unit_type) {
22556 switch (unit_type)
22557 {
22558 case 0x01:
22559 return "DW_UT_compile (0x01)";
22560 case 0x02:
22561 return "DW_UT_type (0x02)";
22562 case 0x03:
22563 return "DW_UT_partial (0x03)";
22564 case 0x04:
22565 return "DW_UT_skeleton (0x04)";
22566 case 0x05:
22567 return "DW_UT_split_compile (0x05)";
22568 case 0x06:
22569 return "DW_UT_split_type (0x06)";
22570 case 0x80:
22571 return "DW_UT_lo_user (0x80)";
22572 case 0xff:
22573 return "DW_UT_hi_user (0xff)";
22574 default:
22575 return nullptr;
22576 }
22577 }
22578
22579 /* Convert a DWARF value form code into its string name. */
22580
22581 static const char *
22582 dwarf_form_name (unsigned form)
22583 {
22584 const char *name = get_DW_FORM_name (form);
22585
22586 if (name == NULL)
22587 return dwarf_unknown ("FORM", form);
22588
22589 return name;
22590 }
22591
22592 static const char *
22593 dwarf_bool_name (unsigned mybool)
22594 {
22595 if (mybool)
22596 return "TRUE";
22597 else
22598 return "FALSE";
22599 }
22600
22601 /* Convert a DWARF type code into its string name. */
22602
22603 static const char *
22604 dwarf_type_encoding_name (unsigned enc)
22605 {
22606 const char *name = get_DW_ATE_name (enc);
22607
22608 if (name == NULL)
22609 return dwarf_unknown ("ATE", enc);
22610
22611 return name;
22612 }
22613
22614 static void
22615 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22616 {
22617 unsigned int i;
22618
22619 print_spaces (indent, f);
22620 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22621 dwarf_tag_name (die->tag), die->abbrev,
22622 sect_offset_str (die->sect_off));
22623
22624 if (die->parent != NULL)
22625 {
22626 print_spaces (indent, f);
22627 fprintf_unfiltered (f, " parent at offset: %s\n",
22628 sect_offset_str (die->parent->sect_off));
22629 }
22630
22631 print_spaces (indent, f);
22632 fprintf_unfiltered (f, " has children: %s\n",
22633 dwarf_bool_name (die->child != NULL));
22634
22635 print_spaces (indent, f);
22636 fprintf_unfiltered (f, " attributes:\n");
22637
22638 for (i = 0; i < die->num_attrs; ++i)
22639 {
22640 print_spaces (indent, f);
22641 fprintf_unfiltered (f, " %s (%s) ",
22642 dwarf_attr_name (die->attrs[i].name),
22643 dwarf_form_name (die->attrs[i].form));
22644
22645 switch (die->attrs[i].form)
22646 {
22647 case DW_FORM_addr:
22648 case DW_FORM_addrx:
22649 case DW_FORM_GNU_addr_index:
22650 fprintf_unfiltered (f, "address: ");
22651 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22652 break;
22653 case DW_FORM_block2:
22654 case DW_FORM_block4:
22655 case DW_FORM_block:
22656 case DW_FORM_block1:
22657 fprintf_unfiltered (f, "block: size %s",
22658 pulongest (DW_BLOCK (&die->attrs[i])->size));
22659 break;
22660 case DW_FORM_exprloc:
22661 fprintf_unfiltered (f, "expression: size %s",
22662 pulongest (DW_BLOCK (&die->attrs[i])->size));
22663 break;
22664 case DW_FORM_data16:
22665 fprintf_unfiltered (f, "constant of 16 bytes");
22666 break;
22667 case DW_FORM_ref_addr:
22668 fprintf_unfiltered (f, "ref address: ");
22669 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22670 break;
22671 case DW_FORM_GNU_ref_alt:
22672 fprintf_unfiltered (f, "alt ref address: ");
22673 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22674 break;
22675 case DW_FORM_ref1:
22676 case DW_FORM_ref2:
22677 case DW_FORM_ref4:
22678 case DW_FORM_ref8:
22679 case DW_FORM_ref_udata:
22680 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22681 (long) (DW_UNSND (&die->attrs[i])));
22682 break;
22683 case DW_FORM_data1:
22684 case DW_FORM_data2:
22685 case DW_FORM_data4:
22686 case DW_FORM_data8:
22687 case DW_FORM_udata:
22688 case DW_FORM_sdata:
22689 fprintf_unfiltered (f, "constant: %s",
22690 pulongest (DW_UNSND (&die->attrs[i])));
22691 break;
22692 case DW_FORM_sec_offset:
22693 fprintf_unfiltered (f, "section offset: %s",
22694 pulongest (DW_UNSND (&die->attrs[i])));
22695 break;
22696 case DW_FORM_ref_sig8:
22697 fprintf_unfiltered (f, "signature: %s",
22698 hex_string (DW_SIGNATURE (&die->attrs[i])));
22699 break;
22700 case DW_FORM_string:
22701 case DW_FORM_strp:
22702 case DW_FORM_line_strp:
22703 case DW_FORM_strx:
22704 case DW_FORM_GNU_str_index:
22705 case DW_FORM_GNU_strp_alt:
22706 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22707 DW_STRING (&die->attrs[i])
22708 ? DW_STRING (&die->attrs[i]) : "",
22709 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22710 break;
22711 case DW_FORM_flag:
22712 if (DW_UNSND (&die->attrs[i]))
22713 fprintf_unfiltered (f, "flag: TRUE");
22714 else
22715 fprintf_unfiltered (f, "flag: FALSE");
22716 break;
22717 case DW_FORM_flag_present:
22718 fprintf_unfiltered (f, "flag: TRUE");
22719 break;
22720 case DW_FORM_indirect:
22721 /* The reader will have reduced the indirect form to
22722 the "base form" so this form should not occur. */
22723 fprintf_unfiltered (f,
22724 "unexpected attribute form: DW_FORM_indirect");
22725 break;
22726 case DW_FORM_implicit_const:
22727 fprintf_unfiltered (f, "constant: %s",
22728 plongest (DW_SND (&die->attrs[i])));
22729 break;
22730 default:
22731 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22732 die->attrs[i].form);
22733 break;
22734 }
22735 fprintf_unfiltered (f, "\n");
22736 }
22737 }
22738
22739 static void
22740 dump_die_for_error (struct die_info *die)
22741 {
22742 dump_die_shallow (gdb_stderr, 0, die);
22743 }
22744
22745 static void
22746 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22747 {
22748 int indent = level * 4;
22749
22750 gdb_assert (die != NULL);
22751
22752 if (level >= max_level)
22753 return;
22754
22755 dump_die_shallow (f, indent, die);
22756
22757 if (die->child != NULL)
22758 {
22759 print_spaces (indent, f);
22760 fprintf_unfiltered (f, " Children:");
22761 if (level + 1 < max_level)
22762 {
22763 fprintf_unfiltered (f, "\n");
22764 dump_die_1 (f, level + 1, max_level, die->child);
22765 }
22766 else
22767 {
22768 fprintf_unfiltered (f,
22769 " [not printed, max nesting level reached]\n");
22770 }
22771 }
22772
22773 if (die->sibling != NULL && level > 0)
22774 {
22775 dump_die_1 (f, level, max_level, die->sibling);
22776 }
22777 }
22778
22779 /* This is called from the pdie macro in gdbinit.in.
22780 It's not static so gcc will keep a copy callable from gdb. */
22781
22782 void
22783 dump_die (struct die_info *die, int max_level)
22784 {
22785 dump_die_1 (gdb_stdlog, 0, max_level, die);
22786 }
22787
22788 static void
22789 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22790 {
22791 void **slot;
22792
22793 slot = htab_find_slot_with_hash (cu->die_hash, die,
22794 to_underlying (die->sect_off),
22795 INSERT);
22796
22797 *slot = die;
22798 }
22799
22800 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22801 required kind. */
22802
22803 static sect_offset
22804 dwarf2_get_ref_die_offset (const struct attribute *attr)
22805 {
22806 if (attr->form_is_ref ())
22807 return (sect_offset) DW_UNSND (attr);
22808
22809 complaint (_("unsupported die ref attribute form: '%s'"),
22810 dwarf_form_name (attr->form));
22811 return {};
22812 }
22813
22814 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22815 * the value held by the attribute is not constant. */
22816
22817 static LONGEST
22818 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22819 {
22820 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22821 return DW_SND (attr);
22822 else if (attr->form == DW_FORM_udata
22823 || attr->form == DW_FORM_data1
22824 || attr->form == DW_FORM_data2
22825 || attr->form == DW_FORM_data4
22826 || attr->form == DW_FORM_data8)
22827 return DW_UNSND (attr);
22828 else
22829 {
22830 /* For DW_FORM_data16 see attribute::form_is_constant. */
22831 complaint (_("Attribute value is not a constant (%s)"),
22832 dwarf_form_name (attr->form));
22833 return default_value;
22834 }
22835 }
22836
22837 /* Follow reference or signature attribute ATTR of SRC_DIE.
22838 On entry *REF_CU is the CU of SRC_DIE.
22839 On exit *REF_CU is the CU of the result. */
22840
22841 static struct die_info *
22842 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22843 struct dwarf2_cu **ref_cu)
22844 {
22845 struct die_info *die;
22846
22847 if (attr->form_is_ref ())
22848 die = follow_die_ref (src_die, attr, ref_cu);
22849 else if (attr->form == DW_FORM_ref_sig8)
22850 die = follow_die_sig (src_die, attr, ref_cu);
22851 else
22852 {
22853 dump_die_for_error (src_die);
22854 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22855 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22856 }
22857
22858 return die;
22859 }
22860
22861 /* Follow reference OFFSET.
22862 On entry *REF_CU is the CU of the source die referencing OFFSET.
22863 On exit *REF_CU is the CU of the result.
22864 Returns NULL if OFFSET is invalid. */
22865
22866 static struct die_info *
22867 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22868 struct dwarf2_cu **ref_cu)
22869 {
22870 struct die_info temp_die;
22871 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22872 struct dwarf2_per_objfile *dwarf2_per_objfile
22873 = cu->per_cu->dwarf2_per_objfile;
22874
22875 gdb_assert (cu->per_cu != NULL);
22876
22877 target_cu = cu;
22878
22879 if (cu->per_cu->is_debug_types)
22880 {
22881 /* .debug_types CUs cannot reference anything outside their CU.
22882 If they need to, they have to reference a signatured type via
22883 DW_FORM_ref_sig8. */
22884 if (!offset_in_cu_p (&cu->header, sect_off))
22885 return NULL;
22886 }
22887 else if (offset_in_dwz != cu->per_cu->is_dwz
22888 || !offset_in_cu_p (&cu->header, sect_off))
22889 {
22890 struct dwarf2_per_cu_data *per_cu;
22891
22892 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22893 dwarf2_per_objfile);
22894
22895 /* If necessary, add it to the queue and load its DIEs. */
22896 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22897 load_full_comp_unit (per_cu, false, cu->language);
22898
22899 target_cu = per_cu->cu;
22900 }
22901 else if (cu->dies == NULL)
22902 {
22903 /* We're loading full DIEs during partial symbol reading. */
22904 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22905 load_full_comp_unit (cu->per_cu, false, language_minimal);
22906 }
22907
22908 *ref_cu = target_cu;
22909 temp_die.sect_off = sect_off;
22910
22911 if (target_cu != cu)
22912 target_cu->ancestor = cu;
22913
22914 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22915 &temp_die,
22916 to_underlying (sect_off));
22917 }
22918
22919 /* Follow reference attribute ATTR of SRC_DIE.
22920 On entry *REF_CU is the CU of SRC_DIE.
22921 On exit *REF_CU is the CU of the result. */
22922
22923 static struct die_info *
22924 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22925 struct dwarf2_cu **ref_cu)
22926 {
22927 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22928 struct dwarf2_cu *cu = *ref_cu;
22929 struct die_info *die;
22930
22931 die = follow_die_offset (sect_off,
22932 (attr->form == DW_FORM_GNU_ref_alt
22933 || cu->per_cu->is_dwz),
22934 ref_cu);
22935 if (!die)
22936 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22937 "at %s [in module %s]"),
22938 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22940
22941 return die;
22942 }
22943
22944 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22945 Returned value is intended for DW_OP_call*. Returned
22946 dwarf2_locexpr_baton->data has lifetime of
22947 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22948
22949 struct dwarf2_locexpr_baton
22950 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22951 struct dwarf2_per_cu_data *per_cu,
22952 CORE_ADDR (*get_frame_pc) (void *baton),
22953 void *baton, bool resolve_abstract_p)
22954 {
22955 struct dwarf2_cu *cu;
22956 struct die_info *die;
22957 struct attribute *attr;
22958 struct dwarf2_locexpr_baton retval;
22959 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22960 struct objfile *objfile = dwarf2_per_objfile->objfile;
22961
22962 if (per_cu->cu == NULL)
22963 load_cu (per_cu, false);
22964 cu = per_cu->cu;
22965 if (cu == NULL)
22966 {
22967 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22968 Instead just throw an error, not much else we can do. */
22969 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22970 sect_offset_str (sect_off), objfile_name (objfile));
22971 }
22972
22973 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22974 if (!die)
22975 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22976 sect_offset_str (sect_off), objfile_name (objfile));
22977
22978 attr = dwarf2_attr (die, DW_AT_location, cu);
22979 if (!attr && resolve_abstract_p
22980 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22981 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22982 {
22983 CORE_ADDR pc = (*get_frame_pc) (baton);
22984 CORE_ADDR baseaddr = objfile->text_section_offset ();
22985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22986
22987 for (const auto &cand_off
22988 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22989 {
22990 struct dwarf2_cu *cand_cu = cu;
22991 struct die_info *cand
22992 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22993 if (!cand
22994 || !cand->parent
22995 || cand->parent->tag != DW_TAG_subprogram)
22996 continue;
22997
22998 CORE_ADDR pc_low, pc_high;
22999 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23000 if (pc_low == ((CORE_ADDR) -1))
23001 continue;
23002 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23003 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23004 if (!(pc_low <= pc && pc < pc_high))
23005 continue;
23006
23007 die = cand;
23008 attr = dwarf2_attr (die, DW_AT_location, cu);
23009 break;
23010 }
23011 }
23012
23013 if (!attr)
23014 {
23015 /* DWARF: "If there is no such attribute, then there is no effect.".
23016 DATA is ignored if SIZE is 0. */
23017
23018 retval.data = NULL;
23019 retval.size = 0;
23020 }
23021 else if (attr->form_is_section_offset ())
23022 {
23023 struct dwarf2_loclist_baton loclist_baton;
23024 CORE_ADDR pc = (*get_frame_pc) (baton);
23025 size_t size;
23026
23027 fill_in_loclist_baton (cu, &loclist_baton, attr);
23028
23029 retval.data = dwarf2_find_location_expression (&loclist_baton,
23030 &size, pc);
23031 retval.size = size;
23032 }
23033 else
23034 {
23035 if (!attr->form_is_block ())
23036 error (_("Dwarf Error: DIE at %s referenced in module %s "
23037 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23038 sect_offset_str (sect_off), objfile_name (objfile));
23039
23040 retval.data = DW_BLOCK (attr)->data;
23041 retval.size = DW_BLOCK (attr)->size;
23042 }
23043 retval.per_cu = cu->per_cu;
23044
23045 age_cached_comp_units (dwarf2_per_objfile);
23046
23047 return retval;
23048 }
23049
23050 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23051 offset. */
23052
23053 struct dwarf2_locexpr_baton
23054 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23055 struct dwarf2_per_cu_data *per_cu,
23056 CORE_ADDR (*get_frame_pc) (void *baton),
23057 void *baton)
23058 {
23059 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23060
23061 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23062 }
23063
23064 /* Write a constant of a given type as target-ordered bytes into
23065 OBSTACK. */
23066
23067 static const gdb_byte *
23068 write_constant_as_bytes (struct obstack *obstack,
23069 enum bfd_endian byte_order,
23070 struct type *type,
23071 ULONGEST value,
23072 LONGEST *len)
23073 {
23074 gdb_byte *result;
23075
23076 *len = TYPE_LENGTH (type);
23077 result = (gdb_byte *) obstack_alloc (obstack, *len);
23078 store_unsigned_integer (result, *len, byte_order, value);
23079
23080 return result;
23081 }
23082
23083 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23084 pointer to the constant bytes and set LEN to the length of the
23085 data. If memory is needed, allocate it on OBSTACK. If the DIE
23086 does not have a DW_AT_const_value, return NULL. */
23087
23088 const gdb_byte *
23089 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23090 struct dwarf2_per_cu_data *per_cu,
23091 struct obstack *obstack,
23092 LONGEST *len)
23093 {
23094 struct dwarf2_cu *cu;
23095 struct die_info *die;
23096 struct attribute *attr;
23097 const gdb_byte *result = NULL;
23098 struct type *type;
23099 LONGEST value;
23100 enum bfd_endian byte_order;
23101 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23102
23103 if (per_cu->cu == NULL)
23104 load_cu (per_cu, false);
23105 cu = per_cu->cu;
23106 if (cu == NULL)
23107 {
23108 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23109 Instead just throw an error, not much else we can do. */
23110 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23111 sect_offset_str (sect_off), objfile_name (objfile));
23112 }
23113
23114 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23115 if (!die)
23116 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23117 sect_offset_str (sect_off), objfile_name (objfile));
23118
23119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23120 if (attr == NULL)
23121 return NULL;
23122
23123 byte_order = (bfd_big_endian (objfile->obfd)
23124 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23125
23126 switch (attr->form)
23127 {
23128 case DW_FORM_addr:
23129 case DW_FORM_addrx:
23130 case DW_FORM_GNU_addr_index:
23131 {
23132 gdb_byte *tem;
23133
23134 *len = cu->header.addr_size;
23135 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23136 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23137 result = tem;
23138 }
23139 break;
23140 case DW_FORM_string:
23141 case DW_FORM_strp:
23142 case DW_FORM_strx:
23143 case DW_FORM_GNU_str_index:
23144 case DW_FORM_GNU_strp_alt:
23145 /* DW_STRING is already allocated on the objfile obstack, point
23146 directly to it. */
23147 result = (const gdb_byte *) DW_STRING (attr);
23148 *len = strlen (DW_STRING (attr));
23149 break;
23150 case DW_FORM_block1:
23151 case DW_FORM_block2:
23152 case DW_FORM_block4:
23153 case DW_FORM_block:
23154 case DW_FORM_exprloc:
23155 case DW_FORM_data16:
23156 result = DW_BLOCK (attr)->data;
23157 *len = DW_BLOCK (attr)->size;
23158 break;
23159
23160 /* The DW_AT_const_value attributes are supposed to carry the
23161 symbol's value "represented as it would be on the target
23162 architecture." By the time we get here, it's already been
23163 converted to host endianness, so we just need to sign- or
23164 zero-extend it as appropriate. */
23165 case DW_FORM_data1:
23166 type = die_type (die, cu);
23167 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23168 if (result == NULL)
23169 result = write_constant_as_bytes (obstack, byte_order,
23170 type, value, len);
23171 break;
23172 case DW_FORM_data2:
23173 type = die_type (die, cu);
23174 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23175 if (result == NULL)
23176 result = write_constant_as_bytes (obstack, byte_order,
23177 type, value, len);
23178 break;
23179 case DW_FORM_data4:
23180 type = die_type (die, cu);
23181 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23182 if (result == NULL)
23183 result = write_constant_as_bytes (obstack, byte_order,
23184 type, value, len);
23185 break;
23186 case DW_FORM_data8:
23187 type = die_type (die, cu);
23188 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23189 if (result == NULL)
23190 result = write_constant_as_bytes (obstack, byte_order,
23191 type, value, len);
23192 break;
23193
23194 case DW_FORM_sdata:
23195 case DW_FORM_implicit_const:
23196 type = die_type (die, cu);
23197 result = write_constant_as_bytes (obstack, byte_order,
23198 type, DW_SND (attr), len);
23199 break;
23200
23201 case DW_FORM_udata:
23202 type = die_type (die, cu);
23203 result = write_constant_as_bytes (obstack, byte_order,
23204 type, DW_UNSND (attr), len);
23205 break;
23206
23207 default:
23208 complaint (_("unsupported const value attribute form: '%s'"),
23209 dwarf_form_name (attr->form));
23210 break;
23211 }
23212
23213 return result;
23214 }
23215
23216 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23217 valid type for this die is found. */
23218
23219 struct type *
23220 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23221 struct dwarf2_per_cu_data *per_cu)
23222 {
23223 struct dwarf2_cu *cu;
23224 struct die_info *die;
23225
23226 if (per_cu->cu == NULL)
23227 load_cu (per_cu, false);
23228 cu = per_cu->cu;
23229 if (!cu)
23230 return NULL;
23231
23232 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23233 if (!die)
23234 return NULL;
23235
23236 return die_type (die, cu);
23237 }
23238
23239 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23240 PER_CU. */
23241
23242 struct type *
23243 dwarf2_get_die_type (cu_offset die_offset,
23244 struct dwarf2_per_cu_data *per_cu)
23245 {
23246 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23247 return get_die_type_at_offset (die_offset_sect, per_cu);
23248 }
23249
23250 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23251 On entry *REF_CU is the CU of SRC_DIE.
23252 On exit *REF_CU is the CU of the result.
23253 Returns NULL if the referenced DIE isn't found. */
23254
23255 static struct die_info *
23256 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23257 struct dwarf2_cu **ref_cu)
23258 {
23259 struct die_info temp_die;
23260 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23261 struct die_info *die;
23262
23263 /* While it might be nice to assert sig_type->type == NULL here,
23264 we can get here for DW_AT_imported_declaration where we need
23265 the DIE not the type. */
23266
23267 /* If necessary, add it to the queue and load its DIEs. */
23268
23269 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23270 read_signatured_type (sig_type);
23271
23272 sig_cu = sig_type->per_cu.cu;
23273 gdb_assert (sig_cu != NULL);
23274 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23275 temp_die.sect_off = sig_type->type_offset_in_section;
23276 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23277 to_underlying (temp_die.sect_off));
23278 if (die)
23279 {
23280 struct dwarf2_per_objfile *dwarf2_per_objfile
23281 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23282
23283 /* For .gdb_index version 7 keep track of included TUs.
23284 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23285 if (dwarf2_per_objfile->index_table != NULL
23286 && dwarf2_per_objfile->index_table->version <= 7)
23287 {
23288 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23289 }
23290
23291 *ref_cu = sig_cu;
23292 if (sig_cu != cu)
23293 sig_cu->ancestor = cu;
23294
23295 return die;
23296 }
23297
23298 return NULL;
23299 }
23300
23301 /* Follow signatured type referenced by ATTR in SRC_DIE.
23302 On entry *REF_CU is the CU of SRC_DIE.
23303 On exit *REF_CU is the CU of the result.
23304 The result is the DIE of the type.
23305 If the referenced type cannot be found an error is thrown. */
23306
23307 static struct die_info *
23308 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23309 struct dwarf2_cu **ref_cu)
23310 {
23311 ULONGEST signature = DW_SIGNATURE (attr);
23312 struct signatured_type *sig_type;
23313 struct die_info *die;
23314
23315 gdb_assert (attr->form == DW_FORM_ref_sig8);
23316
23317 sig_type = lookup_signatured_type (*ref_cu, signature);
23318 /* sig_type will be NULL if the signatured type is missing from
23319 the debug info. */
23320 if (sig_type == NULL)
23321 {
23322 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23323 " from DIE at %s [in module %s]"),
23324 hex_string (signature), sect_offset_str (src_die->sect_off),
23325 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23326 }
23327
23328 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23329 if (die == NULL)
23330 {
23331 dump_die_for_error (src_die);
23332 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23333 " from DIE at %s [in module %s]"),
23334 hex_string (signature), sect_offset_str (src_die->sect_off),
23335 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23336 }
23337
23338 return die;
23339 }
23340
23341 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23342 reading in and processing the type unit if necessary. */
23343
23344 static struct type *
23345 get_signatured_type (struct die_info *die, ULONGEST signature,
23346 struct dwarf2_cu *cu)
23347 {
23348 struct dwarf2_per_objfile *dwarf2_per_objfile
23349 = cu->per_cu->dwarf2_per_objfile;
23350 struct signatured_type *sig_type;
23351 struct dwarf2_cu *type_cu;
23352 struct die_info *type_die;
23353 struct type *type;
23354
23355 sig_type = lookup_signatured_type (cu, signature);
23356 /* sig_type will be NULL if the signatured type is missing from
23357 the debug info. */
23358 if (sig_type == NULL)
23359 {
23360 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23361 " from DIE at %s [in module %s]"),
23362 hex_string (signature), sect_offset_str (die->sect_off),
23363 objfile_name (dwarf2_per_objfile->objfile));
23364 return build_error_marker_type (cu, die);
23365 }
23366
23367 /* If we already know the type we're done. */
23368 if (sig_type->type != NULL)
23369 return sig_type->type;
23370
23371 type_cu = cu;
23372 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23373 if (type_die != NULL)
23374 {
23375 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23376 is created. This is important, for example, because for c++ classes
23377 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23378 type = read_type_die (type_die, type_cu);
23379 if (type == NULL)
23380 {
23381 complaint (_("Dwarf Error: Cannot build signatured type %s"
23382 " referenced from DIE at %s [in module %s]"),
23383 hex_string (signature), sect_offset_str (die->sect_off),
23384 objfile_name (dwarf2_per_objfile->objfile));
23385 type = build_error_marker_type (cu, die);
23386 }
23387 }
23388 else
23389 {
23390 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23391 " from DIE at %s [in module %s]"),
23392 hex_string (signature), sect_offset_str (die->sect_off),
23393 objfile_name (dwarf2_per_objfile->objfile));
23394 type = build_error_marker_type (cu, die);
23395 }
23396 sig_type->type = type;
23397
23398 return type;
23399 }
23400
23401 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23402 reading in and processing the type unit if necessary. */
23403
23404 static struct type *
23405 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23406 struct dwarf2_cu *cu) /* ARI: editCase function */
23407 {
23408 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23409 if (attr->form_is_ref ())
23410 {
23411 struct dwarf2_cu *type_cu = cu;
23412 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23413
23414 return read_type_die (type_die, type_cu);
23415 }
23416 else if (attr->form == DW_FORM_ref_sig8)
23417 {
23418 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23419 }
23420 else
23421 {
23422 struct dwarf2_per_objfile *dwarf2_per_objfile
23423 = cu->per_cu->dwarf2_per_objfile;
23424
23425 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23426 " at %s [in module %s]"),
23427 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23428 objfile_name (dwarf2_per_objfile->objfile));
23429 return build_error_marker_type (cu, die);
23430 }
23431 }
23432
23433 /* Load the DIEs associated with type unit PER_CU into memory. */
23434
23435 static void
23436 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23437 {
23438 struct signatured_type *sig_type;
23439
23440 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23441 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23442
23443 /* We have the per_cu, but we need the signatured_type.
23444 Fortunately this is an easy translation. */
23445 gdb_assert (per_cu->is_debug_types);
23446 sig_type = (struct signatured_type *) per_cu;
23447
23448 gdb_assert (per_cu->cu == NULL);
23449
23450 read_signatured_type (sig_type);
23451
23452 gdb_assert (per_cu->cu != NULL);
23453 }
23454
23455 /* Read in a signatured type and build its CU and DIEs.
23456 If the type is a stub for the real type in a DWO file,
23457 read in the real type from the DWO file as well. */
23458
23459 static void
23460 read_signatured_type (struct signatured_type *sig_type)
23461 {
23462 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23463
23464 gdb_assert (per_cu->is_debug_types);
23465 gdb_assert (per_cu->cu == NULL);
23466
23467 cutu_reader reader (per_cu, NULL, 0, 1, false);
23468
23469 if (!reader.dummy_p)
23470 {
23471 struct dwarf2_cu *cu = reader.cu;
23472 const gdb_byte *info_ptr = reader.info_ptr;
23473
23474 gdb_assert (cu->die_hash == NULL);
23475 cu->die_hash =
23476 htab_create_alloc_ex (cu->header.length / 12,
23477 die_hash,
23478 die_eq,
23479 NULL,
23480 &cu->comp_unit_obstack,
23481 hashtab_obstack_allocate,
23482 dummy_obstack_deallocate);
23483
23484 if (reader.comp_unit_die->has_children)
23485 reader.comp_unit_die->child
23486 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23487 reader.comp_unit_die);
23488 cu->dies = reader.comp_unit_die;
23489 /* comp_unit_die is not stored in die_hash, no need. */
23490
23491 /* We try not to read any attributes in this function, because
23492 not all CUs needed for references have been loaded yet, and
23493 symbol table processing isn't initialized. But we have to
23494 set the CU language, or we won't be able to build types
23495 correctly. Similarly, if we do not read the producer, we can
23496 not apply producer-specific interpretation. */
23497 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23498 }
23499
23500 sig_type->per_cu.tu_read = 1;
23501 }
23502
23503 /* Decode simple location descriptions.
23504 Given a pointer to a dwarf block that defines a location, compute
23505 the location and return the value.
23506
23507 NOTE drow/2003-11-18: This function is called in two situations
23508 now: for the address of static or global variables (partial symbols
23509 only) and for offsets into structures which are expected to be
23510 (more or less) constant. The partial symbol case should go away,
23511 and only the constant case should remain. That will let this
23512 function complain more accurately. A few special modes are allowed
23513 without complaint for global variables (for instance, global
23514 register values and thread-local values).
23515
23516 A location description containing no operations indicates that the
23517 object is optimized out. The return value is 0 for that case.
23518 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23519 callers will only want a very basic result and this can become a
23520 complaint.
23521
23522 Note that stack[0] is unused except as a default error return. */
23523
23524 static CORE_ADDR
23525 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23526 {
23527 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23528 size_t i;
23529 size_t size = blk->size;
23530 const gdb_byte *data = blk->data;
23531 CORE_ADDR stack[64];
23532 int stacki;
23533 unsigned int bytes_read, unsnd;
23534 gdb_byte op;
23535
23536 i = 0;
23537 stacki = 0;
23538 stack[stacki] = 0;
23539 stack[++stacki] = 0;
23540
23541 while (i < size)
23542 {
23543 op = data[i++];
23544 switch (op)
23545 {
23546 case DW_OP_lit0:
23547 case DW_OP_lit1:
23548 case DW_OP_lit2:
23549 case DW_OP_lit3:
23550 case DW_OP_lit4:
23551 case DW_OP_lit5:
23552 case DW_OP_lit6:
23553 case DW_OP_lit7:
23554 case DW_OP_lit8:
23555 case DW_OP_lit9:
23556 case DW_OP_lit10:
23557 case DW_OP_lit11:
23558 case DW_OP_lit12:
23559 case DW_OP_lit13:
23560 case DW_OP_lit14:
23561 case DW_OP_lit15:
23562 case DW_OP_lit16:
23563 case DW_OP_lit17:
23564 case DW_OP_lit18:
23565 case DW_OP_lit19:
23566 case DW_OP_lit20:
23567 case DW_OP_lit21:
23568 case DW_OP_lit22:
23569 case DW_OP_lit23:
23570 case DW_OP_lit24:
23571 case DW_OP_lit25:
23572 case DW_OP_lit26:
23573 case DW_OP_lit27:
23574 case DW_OP_lit28:
23575 case DW_OP_lit29:
23576 case DW_OP_lit30:
23577 case DW_OP_lit31:
23578 stack[++stacki] = op - DW_OP_lit0;
23579 break;
23580
23581 case DW_OP_reg0:
23582 case DW_OP_reg1:
23583 case DW_OP_reg2:
23584 case DW_OP_reg3:
23585 case DW_OP_reg4:
23586 case DW_OP_reg5:
23587 case DW_OP_reg6:
23588 case DW_OP_reg7:
23589 case DW_OP_reg8:
23590 case DW_OP_reg9:
23591 case DW_OP_reg10:
23592 case DW_OP_reg11:
23593 case DW_OP_reg12:
23594 case DW_OP_reg13:
23595 case DW_OP_reg14:
23596 case DW_OP_reg15:
23597 case DW_OP_reg16:
23598 case DW_OP_reg17:
23599 case DW_OP_reg18:
23600 case DW_OP_reg19:
23601 case DW_OP_reg20:
23602 case DW_OP_reg21:
23603 case DW_OP_reg22:
23604 case DW_OP_reg23:
23605 case DW_OP_reg24:
23606 case DW_OP_reg25:
23607 case DW_OP_reg26:
23608 case DW_OP_reg27:
23609 case DW_OP_reg28:
23610 case DW_OP_reg29:
23611 case DW_OP_reg30:
23612 case DW_OP_reg31:
23613 stack[++stacki] = op - DW_OP_reg0;
23614 if (i < size)
23615 dwarf2_complex_location_expr_complaint ();
23616 break;
23617
23618 case DW_OP_regx:
23619 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23620 i += bytes_read;
23621 stack[++stacki] = unsnd;
23622 if (i < size)
23623 dwarf2_complex_location_expr_complaint ();
23624 break;
23625
23626 case DW_OP_addr:
23627 stack[++stacki] = read_address (objfile->obfd, &data[i],
23628 cu, &bytes_read);
23629 i += bytes_read;
23630 break;
23631
23632 case DW_OP_const1u:
23633 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23634 i += 1;
23635 break;
23636
23637 case DW_OP_const1s:
23638 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23639 i += 1;
23640 break;
23641
23642 case DW_OP_const2u:
23643 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23644 i += 2;
23645 break;
23646
23647 case DW_OP_const2s:
23648 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23649 i += 2;
23650 break;
23651
23652 case DW_OP_const4u:
23653 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23654 i += 4;
23655 break;
23656
23657 case DW_OP_const4s:
23658 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23659 i += 4;
23660 break;
23661
23662 case DW_OP_const8u:
23663 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23664 i += 8;
23665 break;
23666
23667 case DW_OP_constu:
23668 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23669 &bytes_read);
23670 i += bytes_read;
23671 break;
23672
23673 case DW_OP_consts:
23674 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23675 i += bytes_read;
23676 break;
23677
23678 case DW_OP_dup:
23679 stack[stacki + 1] = stack[stacki];
23680 stacki++;
23681 break;
23682
23683 case DW_OP_plus:
23684 stack[stacki - 1] += stack[stacki];
23685 stacki--;
23686 break;
23687
23688 case DW_OP_plus_uconst:
23689 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23690 &bytes_read);
23691 i += bytes_read;
23692 break;
23693
23694 case DW_OP_minus:
23695 stack[stacki - 1] -= stack[stacki];
23696 stacki--;
23697 break;
23698
23699 case DW_OP_deref:
23700 /* If we're not the last op, then we definitely can't encode
23701 this using GDB's address_class enum. This is valid for partial
23702 global symbols, although the variable's address will be bogus
23703 in the psymtab. */
23704 if (i < size)
23705 dwarf2_complex_location_expr_complaint ();
23706 break;
23707
23708 case DW_OP_GNU_push_tls_address:
23709 case DW_OP_form_tls_address:
23710 /* The top of the stack has the offset from the beginning
23711 of the thread control block at which the variable is located. */
23712 /* Nothing should follow this operator, so the top of stack would
23713 be returned. */
23714 /* This is valid for partial global symbols, but the variable's
23715 address will be bogus in the psymtab. Make it always at least
23716 non-zero to not look as a variable garbage collected by linker
23717 which have DW_OP_addr 0. */
23718 if (i < size)
23719 dwarf2_complex_location_expr_complaint ();
23720 stack[stacki]++;
23721 break;
23722
23723 case DW_OP_GNU_uninit:
23724 break;
23725
23726 case DW_OP_addrx:
23727 case DW_OP_GNU_addr_index:
23728 case DW_OP_GNU_const_index:
23729 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23730 &bytes_read);
23731 i += bytes_read;
23732 break;
23733
23734 default:
23735 {
23736 const char *name = get_DW_OP_name (op);
23737
23738 if (name)
23739 complaint (_("unsupported stack op: '%s'"),
23740 name);
23741 else
23742 complaint (_("unsupported stack op: '%02x'"),
23743 op);
23744 }
23745
23746 return (stack[stacki]);
23747 }
23748
23749 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23750 outside of the allocated space. Also enforce minimum>0. */
23751 if (stacki >= ARRAY_SIZE (stack) - 1)
23752 {
23753 complaint (_("location description stack overflow"));
23754 return 0;
23755 }
23756
23757 if (stacki <= 0)
23758 {
23759 complaint (_("location description stack underflow"));
23760 return 0;
23761 }
23762 }
23763 return (stack[stacki]);
23764 }
23765
23766 /* memory allocation interface */
23767
23768 static struct dwarf_block *
23769 dwarf_alloc_block (struct dwarf2_cu *cu)
23770 {
23771 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23772 }
23773
23774 static struct die_info *
23775 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23776 {
23777 struct die_info *die;
23778 size_t size = sizeof (struct die_info);
23779
23780 if (num_attrs > 1)
23781 size += (num_attrs - 1) * sizeof (struct attribute);
23782
23783 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23784 memset (die, 0, sizeof (struct die_info));
23785 return (die);
23786 }
23787
23788 \f
23789 /* Macro support. */
23790
23791 /* Return file name relative to the compilation directory of file number I in
23792 *LH's file name table. The result is allocated using xmalloc; the caller is
23793 responsible for freeing it. */
23794
23795 static char *
23796 file_file_name (int file, struct line_header *lh)
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 (lh->is_valid_file_index (file))
23801 {
23802 const file_entry *fe = lh->file_name_at (file);
23803
23804 if (!IS_ABSOLUTE_PATH (fe->name))
23805 {
23806 const char *dir = fe->include_dir (lh);
23807 if (dir != NULL)
23808 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
23809 }
23810 return xstrdup (fe->name);
23811 }
23812 else
23813 {
23814 /* The compiler produced a bogus file number. We can at least
23815 record the macro definitions made in the file, even if we
23816 won't be able to find the file by name. */
23817 char fake_name[80];
23818
23819 xsnprintf (fake_name, sizeof (fake_name),
23820 "<bad macro file number %d>", file);
23821
23822 complaint (_("bad file number in macro information (%d)"),
23823 file);
23824
23825 return xstrdup (fake_name);
23826 }
23827 }
23828
23829 /* Return the full name of file number I in *LH's file name table.
23830 Use COMP_DIR as the name of the current directory of the
23831 compilation. The result is allocated using xmalloc; the caller is
23832 responsible for freeing it. */
23833 static char *
23834 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23835 {
23836 /* Is the file number a valid index into the line header's file name
23837 table? Remember that file numbers start with one, not zero. */
23838 if (lh->is_valid_file_index (file))
23839 {
23840 char *relative = file_file_name (file, lh);
23841
23842 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23843 return relative;
23844 return reconcat (relative, comp_dir, SLASH_STRING,
23845 relative, (char *) NULL);
23846 }
23847 else
23848 return file_file_name (file, lh);
23849 }
23850
23851
23852 static struct macro_source_file *
23853 macro_start_file (struct dwarf2_cu *cu,
23854 int file, int line,
23855 struct macro_source_file *current_file,
23856 struct line_header *lh)
23857 {
23858 /* File name relative to the compilation directory of this source file. */
23859 char *file_name = file_file_name (file, lh);
23860
23861 if (! current_file)
23862 {
23863 /* Note: We don't create a macro table for this compilation unit
23864 at all until we actually get a filename. */
23865 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23866
23867 /* If we have no current file, then this must be the start_file
23868 directive for the compilation unit's main source file. */
23869 current_file = macro_set_main (macro_table, file_name);
23870 macro_define_special (macro_table);
23871 }
23872 else
23873 current_file = macro_include (current_file, line, file_name);
23874
23875 xfree (file_name);
23876
23877 return current_file;
23878 }
23879
23880 static const char *
23881 consume_improper_spaces (const char *p, const char *body)
23882 {
23883 if (*p == ' ')
23884 {
23885 complaint (_("macro definition contains spaces "
23886 "in formal argument list:\n`%s'"),
23887 body);
23888
23889 while (*p == ' ')
23890 p++;
23891 }
23892
23893 return p;
23894 }
23895
23896
23897 static void
23898 parse_macro_definition (struct macro_source_file *file, int line,
23899 const char *body)
23900 {
23901 const char *p;
23902
23903 /* The body string takes one of two forms. For object-like macro
23904 definitions, it should be:
23905
23906 <macro name> " " <definition>
23907
23908 For function-like macro definitions, it should be:
23909
23910 <macro name> "() " <definition>
23911 or
23912 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23913
23914 Spaces may appear only where explicitly indicated, and in the
23915 <definition>.
23916
23917 The Dwarf 2 spec says that an object-like macro's name is always
23918 followed by a space, but versions of GCC around March 2002 omit
23919 the space when the macro's definition is the empty string.
23920
23921 The Dwarf 2 spec says that there should be no spaces between the
23922 formal arguments in a function-like macro's formal argument list,
23923 but versions of GCC around March 2002 include spaces after the
23924 commas. */
23925
23926
23927 /* Find the extent of the macro name. The macro name is terminated
23928 by either a space or null character (for an object-like macro) or
23929 an opening paren (for a function-like macro). */
23930 for (p = body; *p; p++)
23931 if (*p == ' ' || *p == '(')
23932 break;
23933
23934 if (*p == ' ' || *p == '\0')
23935 {
23936 /* It's an object-like macro. */
23937 int name_len = p - body;
23938 std::string name (body, name_len);
23939 const char *replacement;
23940
23941 if (*p == ' ')
23942 replacement = body + name_len + 1;
23943 else
23944 {
23945 dwarf2_macro_malformed_definition_complaint (body);
23946 replacement = body + name_len;
23947 }
23948
23949 macro_define_object (file, line, name.c_str (), replacement);
23950 }
23951 else if (*p == '(')
23952 {
23953 /* It's a function-like macro. */
23954 std::string name (body, p - body);
23955 int argc = 0;
23956 int argv_size = 1;
23957 char **argv = XNEWVEC (char *, argv_size);
23958
23959 p++;
23960
23961 p = consume_improper_spaces (p, body);
23962
23963 /* Parse the formal argument list. */
23964 while (*p && *p != ')')
23965 {
23966 /* Find the extent of the current argument name. */
23967 const char *arg_start = p;
23968
23969 while (*p && *p != ',' && *p != ')' && *p != ' ')
23970 p++;
23971
23972 if (! *p || p == arg_start)
23973 dwarf2_macro_malformed_definition_complaint (body);
23974 else
23975 {
23976 /* Make sure argv has room for the new argument. */
23977 if (argc >= argv_size)
23978 {
23979 argv_size *= 2;
23980 argv = XRESIZEVEC (char *, argv, argv_size);
23981 }
23982
23983 argv[argc++] = savestring (arg_start, p - arg_start);
23984 }
23985
23986 p = consume_improper_spaces (p, body);
23987
23988 /* Consume the comma, if present. */
23989 if (*p == ',')
23990 {
23991 p++;
23992
23993 p = consume_improper_spaces (p, body);
23994 }
23995 }
23996
23997 if (*p == ')')
23998 {
23999 p++;
24000
24001 if (*p == ' ')
24002 /* Perfectly formed definition, no complaints. */
24003 macro_define_function (file, line, name.c_str (),
24004 argc, (const char **) argv,
24005 p + 1);
24006 else if (*p == '\0')
24007 {
24008 /* Complain, but do define it. */
24009 dwarf2_macro_malformed_definition_complaint (body);
24010 macro_define_function (file, line, name.c_str (),
24011 argc, (const char **) argv,
24012 p);
24013 }
24014 else
24015 /* Just complain. */
24016 dwarf2_macro_malformed_definition_complaint (body);
24017 }
24018 else
24019 /* Just complain. */
24020 dwarf2_macro_malformed_definition_complaint (body);
24021
24022 {
24023 int i;
24024
24025 for (i = 0; i < argc; i++)
24026 xfree (argv[i]);
24027 }
24028 xfree (argv);
24029 }
24030 else
24031 dwarf2_macro_malformed_definition_complaint (body);
24032 }
24033
24034 /* Skip some bytes from BYTES according to the form given in FORM.
24035 Returns the new pointer. */
24036
24037 static const gdb_byte *
24038 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24039 enum dwarf_form form,
24040 unsigned int offset_size,
24041 struct dwarf2_section_info *section)
24042 {
24043 unsigned int bytes_read;
24044
24045 switch (form)
24046 {
24047 case DW_FORM_data1:
24048 case DW_FORM_flag:
24049 ++bytes;
24050 break;
24051
24052 case DW_FORM_data2:
24053 bytes += 2;
24054 break;
24055
24056 case DW_FORM_data4:
24057 bytes += 4;
24058 break;
24059
24060 case DW_FORM_data8:
24061 bytes += 8;
24062 break;
24063
24064 case DW_FORM_data16:
24065 bytes += 16;
24066 break;
24067
24068 case DW_FORM_string:
24069 read_direct_string (abfd, bytes, &bytes_read);
24070 bytes += bytes_read;
24071 break;
24072
24073 case DW_FORM_sec_offset:
24074 case DW_FORM_strp:
24075 case DW_FORM_GNU_strp_alt:
24076 bytes += offset_size;
24077 break;
24078
24079 case DW_FORM_block:
24080 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24081 bytes += bytes_read;
24082 break;
24083
24084 case DW_FORM_block1:
24085 bytes += 1 + read_1_byte (abfd, bytes);
24086 break;
24087 case DW_FORM_block2:
24088 bytes += 2 + read_2_bytes (abfd, bytes);
24089 break;
24090 case DW_FORM_block4:
24091 bytes += 4 + read_4_bytes (abfd, bytes);
24092 break;
24093
24094 case DW_FORM_addrx:
24095 case DW_FORM_sdata:
24096 case DW_FORM_strx:
24097 case DW_FORM_udata:
24098 case DW_FORM_GNU_addr_index:
24099 case DW_FORM_GNU_str_index:
24100 bytes = gdb_skip_leb128 (bytes, buffer_end);
24101 if (bytes == NULL)
24102 {
24103 dwarf2_section_buffer_overflow_complaint (section);
24104 return NULL;
24105 }
24106 break;
24107
24108 case DW_FORM_implicit_const:
24109 break;
24110
24111 default:
24112 {
24113 complaint (_("invalid form 0x%x in `%s'"),
24114 form, section->get_name ());
24115 return NULL;
24116 }
24117 }
24118
24119 return bytes;
24120 }
24121
24122 /* A helper for dwarf_decode_macros that handles skipping an unknown
24123 opcode. Returns an updated pointer to the macro data buffer; or,
24124 on error, issues a complaint and returns NULL. */
24125
24126 static const gdb_byte *
24127 skip_unknown_opcode (unsigned int opcode,
24128 const gdb_byte **opcode_definitions,
24129 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24130 bfd *abfd,
24131 unsigned int offset_size,
24132 struct dwarf2_section_info *section)
24133 {
24134 unsigned int bytes_read, i;
24135 unsigned long arg;
24136 const gdb_byte *defn;
24137
24138 if (opcode_definitions[opcode] == NULL)
24139 {
24140 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24141 opcode);
24142 return NULL;
24143 }
24144
24145 defn = opcode_definitions[opcode];
24146 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24147 defn += bytes_read;
24148
24149 for (i = 0; i < arg; ++i)
24150 {
24151 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24152 (enum dwarf_form) defn[i], offset_size,
24153 section);
24154 if (mac_ptr == NULL)
24155 {
24156 /* skip_form_bytes already issued the complaint. */
24157 return NULL;
24158 }
24159 }
24160
24161 return mac_ptr;
24162 }
24163
24164 /* A helper function which parses the header of a macro section.
24165 If the macro section is the extended (for now called "GNU") type,
24166 then this updates *OFFSET_SIZE. Returns a pointer to just after
24167 the header, or issues a complaint and returns NULL on error. */
24168
24169 static const gdb_byte *
24170 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24171 bfd *abfd,
24172 const gdb_byte *mac_ptr,
24173 unsigned int *offset_size,
24174 int section_is_gnu)
24175 {
24176 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24177
24178 if (section_is_gnu)
24179 {
24180 unsigned int version, flags;
24181
24182 version = read_2_bytes (abfd, mac_ptr);
24183 if (version != 4 && version != 5)
24184 {
24185 complaint (_("unrecognized version `%d' in .debug_macro section"),
24186 version);
24187 return NULL;
24188 }
24189 mac_ptr += 2;
24190
24191 flags = read_1_byte (abfd, mac_ptr);
24192 ++mac_ptr;
24193 *offset_size = (flags & 1) ? 8 : 4;
24194
24195 if ((flags & 2) != 0)
24196 /* We don't need the line table offset. */
24197 mac_ptr += *offset_size;
24198
24199 /* Vendor opcode descriptions. */
24200 if ((flags & 4) != 0)
24201 {
24202 unsigned int i, count;
24203
24204 count = read_1_byte (abfd, mac_ptr);
24205 ++mac_ptr;
24206 for (i = 0; i < count; ++i)
24207 {
24208 unsigned int opcode, bytes_read;
24209 unsigned long arg;
24210
24211 opcode = read_1_byte (abfd, mac_ptr);
24212 ++mac_ptr;
24213 opcode_definitions[opcode] = mac_ptr;
24214 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24215 mac_ptr += bytes_read;
24216 mac_ptr += arg;
24217 }
24218 }
24219 }
24220
24221 return mac_ptr;
24222 }
24223
24224 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24225 including DW_MACRO_import. */
24226
24227 static void
24228 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24229 bfd *abfd,
24230 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24231 struct macro_source_file *current_file,
24232 struct line_header *lh,
24233 struct dwarf2_section_info *section,
24234 int section_is_gnu, int section_is_dwz,
24235 unsigned int offset_size,
24236 htab_t include_hash)
24237 {
24238 struct dwarf2_per_objfile *dwarf2_per_objfile
24239 = cu->per_cu->dwarf2_per_objfile;
24240 struct objfile *objfile = dwarf2_per_objfile->objfile;
24241 enum dwarf_macro_record_type macinfo_type;
24242 int at_commandline;
24243 const gdb_byte *opcode_definitions[256];
24244
24245 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24246 &offset_size, section_is_gnu);
24247 if (mac_ptr == NULL)
24248 {
24249 /* We already issued a complaint. */
24250 return;
24251 }
24252
24253 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24254 GDB is still reading the definitions from command line. First
24255 DW_MACINFO_start_file will need to be ignored as it was already executed
24256 to create CURRENT_FILE for the main source holding also the command line
24257 definitions. On first met DW_MACINFO_start_file this flag is reset to
24258 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24259
24260 at_commandline = 1;
24261
24262 do
24263 {
24264 /* Do we at least have room for a macinfo type byte? */
24265 if (mac_ptr >= mac_end)
24266 {
24267 dwarf2_section_buffer_overflow_complaint (section);
24268 break;
24269 }
24270
24271 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24272 mac_ptr++;
24273
24274 /* Note that we rely on the fact that the corresponding GNU and
24275 DWARF constants are the same. */
24276 DIAGNOSTIC_PUSH
24277 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24278 switch (macinfo_type)
24279 {
24280 /* A zero macinfo type indicates the end of the macro
24281 information. */
24282 case 0:
24283 break;
24284
24285 case DW_MACRO_define:
24286 case DW_MACRO_undef:
24287 case DW_MACRO_define_strp:
24288 case DW_MACRO_undef_strp:
24289 case DW_MACRO_define_sup:
24290 case DW_MACRO_undef_sup:
24291 {
24292 unsigned int bytes_read;
24293 int line;
24294 const char *body;
24295 int is_define;
24296
24297 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24298 mac_ptr += bytes_read;
24299
24300 if (macinfo_type == DW_MACRO_define
24301 || macinfo_type == DW_MACRO_undef)
24302 {
24303 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24304 mac_ptr += bytes_read;
24305 }
24306 else
24307 {
24308 LONGEST str_offset;
24309
24310 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24311 mac_ptr += offset_size;
24312
24313 if (macinfo_type == DW_MACRO_define_sup
24314 || macinfo_type == DW_MACRO_undef_sup
24315 || section_is_dwz)
24316 {
24317 struct dwz_file *dwz
24318 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24319
24320 body = read_indirect_string_from_dwz (objfile,
24321 dwz, str_offset);
24322 }
24323 else
24324 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24325 abfd, str_offset);
24326 }
24327
24328 is_define = (macinfo_type == DW_MACRO_define
24329 || macinfo_type == DW_MACRO_define_strp
24330 || macinfo_type == DW_MACRO_define_sup);
24331 if (! current_file)
24332 {
24333 /* DWARF violation as no main source is present. */
24334 complaint (_("debug info with no main source gives macro %s "
24335 "on line %d: %s"),
24336 is_define ? _("definition") : _("undefinition"),
24337 line, body);
24338 break;
24339 }
24340 if ((line == 0 && !at_commandline)
24341 || (line != 0 && at_commandline))
24342 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24343 at_commandline ? _("command-line") : _("in-file"),
24344 is_define ? _("definition") : _("undefinition"),
24345 line == 0 ? _("zero") : _("non-zero"), line, body);
24346
24347 if (body == NULL)
24348 {
24349 /* Fedora's rpm-build's "debugedit" binary
24350 corrupted .debug_macro sections.
24351
24352 For more info, see
24353 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24354 complaint (_("debug info gives %s invalid macro %s "
24355 "without body (corrupted?) at line %d "
24356 "on file %s"),
24357 at_commandline ? _("command-line") : _("in-file"),
24358 is_define ? _("definition") : _("undefinition"),
24359 line, current_file->filename);
24360 }
24361 else if (is_define)
24362 parse_macro_definition (current_file, line, body);
24363 else
24364 {
24365 gdb_assert (macinfo_type == DW_MACRO_undef
24366 || macinfo_type == DW_MACRO_undef_strp
24367 || macinfo_type == DW_MACRO_undef_sup);
24368 macro_undef (current_file, line, body);
24369 }
24370 }
24371 break;
24372
24373 case DW_MACRO_start_file:
24374 {
24375 unsigned int bytes_read;
24376 int line, file;
24377
24378 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24379 mac_ptr += bytes_read;
24380 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24381 mac_ptr += bytes_read;
24382
24383 if ((line == 0 && !at_commandline)
24384 || (line != 0 && at_commandline))
24385 complaint (_("debug info gives source %d included "
24386 "from %s at %s line %d"),
24387 file, at_commandline ? _("command-line") : _("file"),
24388 line == 0 ? _("zero") : _("non-zero"), line);
24389
24390 if (at_commandline)
24391 {
24392 /* This DW_MACRO_start_file was executed in the
24393 pass one. */
24394 at_commandline = 0;
24395 }
24396 else
24397 current_file = macro_start_file (cu, file, line, current_file,
24398 lh);
24399 }
24400 break;
24401
24402 case DW_MACRO_end_file:
24403 if (! current_file)
24404 complaint (_("macro debug info has an unmatched "
24405 "`close_file' directive"));
24406 else
24407 {
24408 current_file = current_file->included_by;
24409 if (! current_file)
24410 {
24411 enum dwarf_macro_record_type next_type;
24412
24413 /* GCC circa March 2002 doesn't produce the zero
24414 type byte marking the end of the compilation
24415 unit. Complain if it's not there, but exit no
24416 matter what. */
24417
24418 /* Do we at least have room for a macinfo type byte? */
24419 if (mac_ptr >= mac_end)
24420 {
24421 dwarf2_section_buffer_overflow_complaint (section);
24422 return;
24423 }
24424
24425 /* We don't increment mac_ptr here, so this is just
24426 a look-ahead. */
24427 next_type
24428 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24429 mac_ptr);
24430 if (next_type != 0)
24431 complaint (_("no terminating 0-type entry for "
24432 "macros in `.debug_macinfo' section"));
24433
24434 return;
24435 }
24436 }
24437 break;
24438
24439 case DW_MACRO_import:
24440 case DW_MACRO_import_sup:
24441 {
24442 LONGEST offset;
24443 void **slot;
24444 bfd *include_bfd = abfd;
24445 struct dwarf2_section_info *include_section = section;
24446 const gdb_byte *include_mac_end = mac_end;
24447 int is_dwz = section_is_dwz;
24448 const gdb_byte *new_mac_ptr;
24449
24450 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24451 mac_ptr += offset_size;
24452
24453 if (macinfo_type == DW_MACRO_import_sup)
24454 {
24455 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24456
24457 dwz->macro.read (objfile);
24458
24459 include_section = &dwz->macro;
24460 include_bfd = include_section->get_bfd_owner ();
24461 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24462 is_dwz = 1;
24463 }
24464
24465 new_mac_ptr = include_section->buffer + offset;
24466 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24467
24468 if (*slot != NULL)
24469 {
24470 /* This has actually happened; see
24471 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24472 complaint (_("recursive DW_MACRO_import in "
24473 ".debug_macro section"));
24474 }
24475 else
24476 {
24477 *slot = (void *) new_mac_ptr;
24478
24479 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24480 include_mac_end, current_file, lh,
24481 section, section_is_gnu, is_dwz,
24482 offset_size, include_hash);
24483
24484 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24485 }
24486 }
24487 break;
24488
24489 case DW_MACINFO_vendor_ext:
24490 if (!section_is_gnu)
24491 {
24492 unsigned int bytes_read;
24493
24494 /* This reads the constant, but since we don't recognize
24495 any vendor extensions, we ignore it. */
24496 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24497 mac_ptr += bytes_read;
24498 read_direct_string (abfd, mac_ptr, &bytes_read);
24499 mac_ptr += bytes_read;
24500
24501 /* We don't recognize any vendor extensions. */
24502 break;
24503 }
24504 /* FALLTHROUGH */
24505
24506 default:
24507 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24508 mac_ptr, mac_end, abfd, offset_size,
24509 section);
24510 if (mac_ptr == NULL)
24511 return;
24512 break;
24513 }
24514 DIAGNOSTIC_POP
24515 } while (macinfo_type != 0);
24516 }
24517
24518 static void
24519 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24520 int section_is_gnu)
24521 {
24522 struct dwarf2_per_objfile *dwarf2_per_objfile
24523 = cu->per_cu->dwarf2_per_objfile;
24524 struct objfile *objfile = dwarf2_per_objfile->objfile;
24525 struct line_header *lh = cu->line_header;
24526 bfd *abfd;
24527 const gdb_byte *mac_ptr, *mac_end;
24528 struct macro_source_file *current_file = 0;
24529 enum dwarf_macro_record_type macinfo_type;
24530 unsigned int offset_size = cu->header.offset_size;
24531 const gdb_byte *opcode_definitions[256];
24532 void **slot;
24533 struct dwarf2_section_info *section;
24534 const char *section_name;
24535
24536 if (cu->dwo_unit != NULL)
24537 {
24538 if (section_is_gnu)
24539 {
24540 section = &cu->dwo_unit->dwo_file->sections.macro;
24541 section_name = ".debug_macro.dwo";
24542 }
24543 else
24544 {
24545 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24546 section_name = ".debug_macinfo.dwo";
24547 }
24548 }
24549 else
24550 {
24551 if (section_is_gnu)
24552 {
24553 section = &dwarf2_per_objfile->macro;
24554 section_name = ".debug_macro";
24555 }
24556 else
24557 {
24558 section = &dwarf2_per_objfile->macinfo;
24559 section_name = ".debug_macinfo";
24560 }
24561 }
24562
24563 section->read (objfile);
24564 if (section->buffer == NULL)
24565 {
24566 complaint (_("missing %s section"), section_name);
24567 return;
24568 }
24569 abfd = section->get_bfd_owner ();
24570
24571 /* First pass: Find the name of the base filename.
24572 This filename is needed in order to process all macros whose definition
24573 (or undefinition) comes from the command line. These macros are defined
24574 before the first DW_MACINFO_start_file entry, and yet still need to be
24575 associated to the base file.
24576
24577 To determine the base file name, we scan the macro definitions until we
24578 reach the first DW_MACINFO_start_file entry. We then initialize
24579 CURRENT_FILE accordingly so that any macro definition found before the
24580 first DW_MACINFO_start_file can still be associated to the base file. */
24581
24582 mac_ptr = section->buffer + offset;
24583 mac_end = section->buffer + section->size;
24584
24585 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24586 &offset_size, section_is_gnu);
24587 if (mac_ptr == NULL)
24588 {
24589 /* We already issued a complaint. */
24590 return;
24591 }
24592
24593 do
24594 {
24595 /* Do we at least have room for a macinfo type byte? */
24596 if (mac_ptr >= mac_end)
24597 {
24598 /* Complaint is printed during the second pass as GDB will probably
24599 stop the first pass earlier upon finding
24600 DW_MACINFO_start_file. */
24601 break;
24602 }
24603
24604 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24605 mac_ptr++;
24606
24607 /* Note that we rely on the fact that the corresponding GNU and
24608 DWARF constants are the same. */
24609 DIAGNOSTIC_PUSH
24610 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24611 switch (macinfo_type)
24612 {
24613 /* A zero macinfo type indicates the end of the macro
24614 information. */
24615 case 0:
24616 break;
24617
24618 case DW_MACRO_define:
24619 case DW_MACRO_undef:
24620 /* Only skip the data by MAC_PTR. */
24621 {
24622 unsigned int bytes_read;
24623
24624 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24625 mac_ptr += bytes_read;
24626 read_direct_string (abfd, mac_ptr, &bytes_read);
24627 mac_ptr += bytes_read;
24628 }
24629 break;
24630
24631 case DW_MACRO_start_file:
24632 {
24633 unsigned int bytes_read;
24634 int line, file;
24635
24636 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24637 mac_ptr += bytes_read;
24638 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24639 mac_ptr += bytes_read;
24640
24641 current_file = macro_start_file (cu, file, line, current_file, lh);
24642 }
24643 break;
24644
24645 case DW_MACRO_end_file:
24646 /* No data to skip by MAC_PTR. */
24647 break;
24648
24649 case DW_MACRO_define_strp:
24650 case DW_MACRO_undef_strp:
24651 case DW_MACRO_define_sup:
24652 case DW_MACRO_undef_sup:
24653 {
24654 unsigned int bytes_read;
24655
24656 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24657 mac_ptr += bytes_read;
24658 mac_ptr += offset_size;
24659 }
24660 break;
24661
24662 case DW_MACRO_import:
24663 case DW_MACRO_import_sup:
24664 /* Note that, according to the spec, a transparent include
24665 chain cannot call DW_MACRO_start_file. So, we can just
24666 skip this opcode. */
24667 mac_ptr += offset_size;
24668 break;
24669
24670 case DW_MACINFO_vendor_ext:
24671 /* Only skip the data by MAC_PTR. */
24672 if (!section_is_gnu)
24673 {
24674 unsigned int bytes_read;
24675
24676 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24677 mac_ptr += bytes_read;
24678 read_direct_string (abfd, mac_ptr, &bytes_read);
24679 mac_ptr += bytes_read;
24680 }
24681 /* FALLTHROUGH */
24682
24683 default:
24684 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24685 mac_ptr, mac_end, abfd, offset_size,
24686 section);
24687 if (mac_ptr == NULL)
24688 return;
24689 break;
24690 }
24691 DIAGNOSTIC_POP
24692 } while (macinfo_type != 0 && current_file == NULL);
24693
24694 /* Second pass: Process all entries.
24695
24696 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24697 command-line macro definitions/undefinitions. This flag is unset when we
24698 reach the first DW_MACINFO_start_file entry. */
24699
24700 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24701 htab_eq_pointer,
24702 NULL, xcalloc, xfree));
24703 mac_ptr = section->buffer + offset;
24704 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24705 *slot = (void *) mac_ptr;
24706 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24707 current_file, lh, section,
24708 section_is_gnu, 0, offset_size,
24709 include_hash.get ());
24710 }
24711
24712 /* Return the .debug_loc section to use for CU.
24713 For DWO files use .debug_loc.dwo. */
24714
24715 static struct dwarf2_section_info *
24716 cu_debug_loc_section (struct dwarf2_cu *cu)
24717 {
24718 struct dwarf2_per_objfile *dwarf2_per_objfile
24719 = cu->per_cu->dwarf2_per_objfile;
24720
24721 if (cu->dwo_unit)
24722 {
24723 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24724
24725 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24726 }
24727 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24728 : &dwarf2_per_objfile->loc);
24729 }
24730
24731 /* A helper function that fills in a dwarf2_loclist_baton. */
24732
24733 static void
24734 fill_in_loclist_baton (struct dwarf2_cu *cu,
24735 struct dwarf2_loclist_baton *baton,
24736 const struct attribute *attr)
24737 {
24738 struct dwarf2_per_objfile *dwarf2_per_objfile
24739 = cu->per_cu->dwarf2_per_objfile;
24740 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24741
24742 section->read (dwarf2_per_objfile->objfile);
24743
24744 baton->per_cu = cu->per_cu;
24745 gdb_assert (baton->per_cu);
24746 /* We don't know how long the location list is, but make sure we
24747 don't run off the edge of the section. */
24748 baton->size = section->size - DW_UNSND (attr);
24749 baton->data = section->buffer + DW_UNSND (attr);
24750 baton->base_address = cu->base_address;
24751 baton->from_dwo = cu->dwo_unit != NULL;
24752 }
24753
24754 static void
24755 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24756 struct dwarf2_cu *cu, int is_block)
24757 {
24758 struct dwarf2_per_objfile *dwarf2_per_objfile
24759 = cu->per_cu->dwarf2_per_objfile;
24760 struct objfile *objfile = dwarf2_per_objfile->objfile;
24761 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24762
24763 if (attr->form_is_section_offset ()
24764 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24765 the section. If so, fall through to the complaint in the
24766 other branch. */
24767 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24768 {
24769 struct dwarf2_loclist_baton *baton;
24770
24771 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24772
24773 fill_in_loclist_baton (cu, baton, attr);
24774
24775 if (cu->base_known == 0)
24776 complaint (_("Location list used without "
24777 "specifying the CU base address."));
24778
24779 SYMBOL_ACLASS_INDEX (sym) = (is_block
24780 ? dwarf2_loclist_block_index
24781 : dwarf2_loclist_index);
24782 SYMBOL_LOCATION_BATON (sym) = baton;
24783 }
24784 else
24785 {
24786 struct dwarf2_locexpr_baton *baton;
24787
24788 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24789 baton->per_cu = cu->per_cu;
24790 gdb_assert (baton->per_cu);
24791
24792 if (attr->form_is_block ())
24793 {
24794 /* Note that we're just copying the block's data pointer
24795 here, not the actual data. We're still pointing into the
24796 info_buffer for SYM's objfile; right now we never release
24797 that buffer, but when we do clean up properly this may
24798 need to change. */
24799 baton->size = DW_BLOCK (attr)->size;
24800 baton->data = DW_BLOCK (attr)->data;
24801 }
24802 else
24803 {
24804 dwarf2_invalid_attrib_class_complaint ("location description",
24805 sym->natural_name ());
24806 baton->size = 0;
24807 }
24808
24809 SYMBOL_ACLASS_INDEX (sym) = (is_block
24810 ? dwarf2_locexpr_block_index
24811 : dwarf2_locexpr_index);
24812 SYMBOL_LOCATION_BATON (sym) = baton;
24813 }
24814 }
24815
24816 /* Return the OBJFILE associated with the compilation unit CU. If CU
24817 came from a separate debuginfo file, then the master objfile is
24818 returned. */
24819
24820 struct objfile *
24821 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24822 {
24823 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24824
24825 /* Return the master objfile, so that we can report and look up the
24826 correct file containing this variable. */
24827 if (objfile->separate_debug_objfile_backlink)
24828 objfile = objfile->separate_debug_objfile_backlink;
24829
24830 return objfile;
24831 }
24832
24833 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24834 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24835 CU_HEADERP first. */
24836
24837 static const struct comp_unit_head *
24838 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24839 struct dwarf2_per_cu_data *per_cu)
24840 {
24841 const gdb_byte *info_ptr;
24842
24843 if (per_cu->cu)
24844 return &per_cu->cu->header;
24845
24846 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24847
24848 memset (cu_headerp, 0, sizeof (*cu_headerp));
24849 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24850 rcuh_kind::COMPILE);
24851
24852 return cu_headerp;
24853 }
24854
24855 /* Return the address size given in the compilation unit header for CU. */
24856
24857 int
24858 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24859 {
24860 struct comp_unit_head cu_header_local;
24861 const struct comp_unit_head *cu_headerp;
24862
24863 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24864
24865 return cu_headerp->addr_size;
24866 }
24867
24868 /* Return the offset size given in the compilation unit header for CU. */
24869
24870 int
24871 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24872 {
24873 struct comp_unit_head cu_header_local;
24874 const struct comp_unit_head *cu_headerp;
24875
24876 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24877
24878 return cu_headerp->offset_size;
24879 }
24880
24881 /* See its dwarf2loc.h declaration. */
24882
24883 int
24884 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24885 {
24886 struct comp_unit_head cu_header_local;
24887 const struct comp_unit_head *cu_headerp;
24888
24889 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24890
24891 if (cu_headerp->version == 2)
24892 return cu_headerp->addr_size;
24893 else
24894 return cu_headerp->offset_size;
24895 }
24896
24897 /* Return the text offset of the CU. The returned offset comes from
24898 this CU's objfile. If this objfile came from a separate debuginfo
24899 file, then the offset may be different from the corresponding
24900 offset in the parent objfile. */
24901
24902 CORE_ADDR
24903 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24904 {
24905 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
24906 }
24907
24908 /* Return a type that is a generic pointer type, the size of which matches
24909 the address size given in the compilation unit header for PER_CU. */
24910 static struct type *
24911 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24912 {
24913 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24914 struct type *void_type = objfile_type (objfile)->builtin_void;
24915 struct type *addr_type = lookup_pointer_type (void_type);
24916 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24917
24918 if (TYPE_LENGTH (addr_type) == addr_size)
24919 return addr_type;
24920
24921 addr_type
24922 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24923 return addr_type;
24924 }
24925
24926 /* Return DWARF version number of PER_CU. */
24927
24928 short
24929 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24930 {
24931 return per_cu->dwarf_version;
24932 }
24933
24934 /* Locate the .debug_info compilation unit from CU's objfile which contains
24935 the DIE at OFFSET. Raises an error on failure. */
24936
24937 static struct dwarf2_per_cu_data *
24938 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24939 unsigned int offset_in_dwz,
24940 struct dwarf2_per_objfile *dwarf2_per_objfile)
24941 {
24942 struct dwarf2_per_cu_data *this_cu;
24943 int low, high;
24944
24945 low = 0;
24946 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24947 while (high > low)
24948 {
24949 struct dwarf2_per_cu_data *mid_cu;
24950 int mid = low + (high - low) / 2;
24951
24952 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24953 if (mid_cu->is_dwz > offset_in_dwz
24954 || (mid_cu->is_dwz == offset_in_dwz
24955 && mid_cu->sect_off + mid_cu->length >= sect_off))
24956 high = mid;
24957 else
24958 low = mid + 1;
24959 }
24960 gdb_assert (low == high);
24961 this_cu = dwarf2_per_objfile->all_comp_units[low];
24962 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24963 {
24964 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24965 error (_("Dwarf Error: could not find partial DIE containing "
24966 "offset %s [in module %s]"),
24967 sect_offset_str (sect_off),
24968 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24969
24970 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24971 <= sect_off);
24972 return dwarf2_per_objfile->all_comp_units[low-1];
24973 }
24974 else
24975 {
24976 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24977 && sect_off >= this_cu->sect_off + this_cu->length)
24978 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24979 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24980 return this_cu;
24981 }
24982 }
24983
24984 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24985
24986 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24987 : per_cu (per_cu_),
24988 mark (false),
24989 has_loclist (false),
24990 checked_producer (false),
24991 producer_is_gxx_lt_4_6 (false),
24992 producer_is_gcc_lt_4_3 (false),
24993 producer_is_icc (false),
24994 producer_is_icc_lt_14 (false),
24995 producer_is_codewarrior (false),
24996 processing_has_namespace_info (false)
24997 {
24998 per_cu->cu = this;
24999 }
25000
25001 /* Destroy a dwarf2_cu. */
25002
25003 dwarf2_cu::~dwarf2_cu ()
25004 {
25005 per_cu->cu = NULL;
25006 }
25007
25008 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25009
25010 static void
25011 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25012 enum language pretend_language)
25013 {
25014 struct attribute *attr;
25015
25016 /* Set the language we're debugging. */
25017 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25018 if (attr != nullptr)
25019 set_cu_language (DW_UNSND (attr), cu);
25020 else
25021 {
25022 cu->language = pretend_language;
25023 cu->language_defn = language_def (cu->language);
25024 }
25025
25026 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25027 }
25028
25029 /* Increase the age counter on each cached compilation unit, and free
25030 any that are too old. */
25031
25032 static void
25033 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25034 {
25035 struct dwarf2_per_cu_data *per_cu, **last_chain;
25036
25037 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25038 per_cu = dwarf2_per_objfile->read_in_chain;
25039 while (per_cu != NULL)
25040 {
25041 per_cu->cu->last_used ++;
25042 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25043 dwarf2_mark (per_cu->cu);
25044 per_cu = per_cu->cu->read_in_chain;
25045 }
25046
25047 per_cu = dwarf2_per_objfile->read_in_chain;
25048 last_chain = &dwarf2_per_objfile->read_in_chain;
25049 while (per_cu != NULL)
25050 {
25051 struct dwarf2_per_cu_data *next_cu;
25052
25053 next_cu = per_cu->cu->read_in_chain;
25054
25055 if (!per_cu->cu->mark)
25056 {
25057 delete per_cu->cu;
25058 *last_chain = next_cu;
25059 }
25060 else
25061 last_chain = &per_cu->cu->read_in_chain;
25062
25063 per_cu = next_cu;
25064 }
25065 }
25066
25067 /* Remove a single compilation unit from the cache. */
25068
25069 static void
25070 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25071 {
25072 struct dwarf2_per_cu_data *per_cu, **last_chain;
25073 struct dwarf2_per_objfile *dwarf2_per_objfile
25074 = target_per_cu->dwarf2_per_objfile;
25075
25076 per_cu = dwarf2_per_objfile->read_in_chain;
25077 last_chain = &dwarf2_per_objfile->read_in_chain;
25078 while (per_cu != NULL)
25079 {
25080 struct dwarf2_per_cu_data *next_cu;
25081
25082 next_cu = per_cu->cu->read_in_chain;
25083
25084 if (per_cu == target_per_cu)
25085 {
25086 delete per_cu->cu;
25087 per_cu->cu = NULL;
25088 *last_chain = next_cu;
25089 break;
25090 }
25091 else
25092 last_chain = &per_cu->cu->read_in_chain;
25093
25094 per_cu = next_cu;
25095 }
25096 }
25097
25098 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25099 We store these in a hash table separate from the DIEs, and preserve them
25100 when the DIEs are flushed out of cache.
25101
25102 The CU "per_cu" pointer is needed because offset alone is not enough to
25103 uniquely identify the type. A file may have multiple .debug_types sections,
25104 or the type may come from a DWO file. Furthermore, while it's more logical
25105 to use per_cu->section+offset, with Fission the section with the data is in
25106 the DWO file but we don't know that section at the point we need it.
25107 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25108 because we can enter the lookup routine, get_die_type_at_offset, from
25109 outside this file, and thus won't necessarily have PER_CU->cu.
25110 Fortunately, PER_CU is stable for the life of the objfile. */
25111
25112 struct dwarf2_per_cu_offset_and_type
25113 {
25114 const struct dwarf2_per_cu_data *per_cu;
25115 sect_offset sect_off;
25116 struct type *type;
25117 };
25118
25119 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25120
25121 static hashval_t
25122 per_cu_offset_and_type_hash (const void *item)
25123 {
25124 const struct dwarf2_per_cu_offset_and_type *ofs
25125 = (const struct dwarf2_per_cu_offset_and_type *) item;
25126
25127 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25128 }
25129
25130 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25131
25132 static int
25133 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25134 {
25135 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25136 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25137 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25138 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25139
25140 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25141 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25142 }
25143
25144 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25145 table if necessary. For convenience, return TYPE.
25146
25147 The DIEs reading must have careful ordering to:
25148 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25149 reading current DIE.
25150 * Not trying to dereference contents of still incompletely read in types
25151 while reading in other DIEs.
25152 * Enable referencing still incompletely read in types just by a pointer to
25153 the type without accessing its fields.
25154
25155 Therefore caller should follow these rules:
25156 * Try to fetch any prerequisite types we may need to build this DIE type
25157 before building the type and calling set_die_type.
25158 * After building type call set_die_type for current DIE as soon as
25159 possible before fetching more types to complete the current type.
25160 * Make the type as complete as possible before fetching more types. */
25161
25162 static struct type *
25163 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25164 {
25165 struct dwarf2_per_objfile *dwarf2_per_objfile
25166 = cu->per_cu->dwarf2_per_objfile;
25167 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25168 struct objfile *objfile = dwarf2_per_objfile->objfile;
25169 struct attribute *attr;
25170 struct dynamic_prop prop;
25171
25172 /* For Ada types, make sure that the gnat-specific data is always
25173 initialized (if not already set). There are a few types where
25174 we should not be doing so, because the type-specific area is
25175 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25176 where the type-specific area is used to store the floatformat).
25177 But this is not a problem, because the gnat-specific information
25178 is actually not needed for these types. */
25179 if (need_gnat_info (cu)
25180 && TYPE_CODE (type) != TYPE_CODE_FUNC
25181 && TYPE_CODE (type) != TYPE_CODE_FLT
25182 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25183 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25184 && TYPE_CODE (type) != TYPE_CODE_METHOD
25185 && !HAVE_GNAT_AUX_INFO (type))
25186 INIT_GNAT_SPECIFIC (type);
25187
25188 /* Read DW_AT_allocated and set in type. */
25189 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25190 if (attr != NULL && attr->form_is_block ())
25191 {
25192 struct type *prop_type
25193 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25194 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25195 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25196 }
25197 else if (attr != NULL)
25198 {
25199 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25200 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25201 sect_offset_str (die->sect_off));
25202 }
25203
25204 /* Read DW_AT_associated and set in type. */
25205 attr = dwarf2_attr (die, DW_AT_associated, cu);
25206 if (attr != NULL && attr->form_is_block ())
25207 {
25208 struct type *prop_type
25209 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25210 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25211 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25212 }
25213 else if (attr != NULL)
25214 {
25215 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25216 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25217 sect_offset_str (die->sect_off));
25218 }
25219
25220 /* Read DW_AT_data_location and set in type. */
25221 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25222 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25223 dwarf2_per_cu_addr_type (cu->per_cu)))
25224 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25225
25226 if (dwarf2_per_objfile->die_type_hash == NULL)
25227 {
25228 dwarf2_per_objfile->die_type_hash =
25229 htab_create_alloc_ex (127,
25230 per_cu_offset_and_type_hash,
25231 per_cu_offset_and_type_eq,
25232 NULL,
25233 &objfile->objfile_obstack,
25234 hashtab_obstack_allocate,
25235 dummy_obstack_deallocate);
25236 }
25237
25238 ofs.per_cu = cu->per_cu;
25239 ofs.sect_off = die->sect_off;
25240 ofs.type = type;
25241 slot = (struct dwarf2_per_cu_offset_and_type **)
25242 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25243 if (*slot)
25244 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25245 sect_offset_str (die->sect_off));
25246 *slot = XOBNEW (&objfile->objfile_obstack,
25247 struct dwarf2_per_cu_offset_and_type);
25248 **slot = ofs;
25249 return type;
25250 }
25251
25252 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25253 or return NULL if the die does not have a saved type. */
25254
25255 static struct type *
25256 get_die_type_at_offset (sect_offset sect_off,
25257 struct dwarf2_per_cu_data *per_cu)
25258 {
25259 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25260 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25261
25262 if (dwarf2_per_objfile->die_type_hash == NULL)
25263 return NULL;
25264
25265 ofs.per_cu = per_cu;
25266 ofs.sect_off = sect_off;
25267 slot = ((struct dwarf2_per_cu_offset_and_type *)
25268 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25269 if (slot)
25270 return slot->type;
25271 else
25272 return NULL;
25273 }
25274
25275 /* Look up the type for DIE in CU in die_type_hash,
25276 or return NULL if DIE does not have a saved type. */
25277
25278 static struct type *
25279 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25280 {
25281 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25282 }
25283
25284 /* Add a dependence relationship from CU to REF_PER_CU. */
25285
25286 static void
25287 dwarf2_add_dependence (struct dwarf2_cu *cu,
25288 struct dwarf2_per_cu_data *ref_per_cu)
25289 {
25290 void **slot;
25291
25292 if (cu->dependencies == NULL)
25293 cu->dependencies
25294 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25295 NULL, &cu->comp_unit_obstack,
25296 hashtab_obstack_allocate,
25297 dummy_obstack_deallocate);
25298
25299 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25300 if (*slot == NULL)
25301 *slot = ref_per_cu;
25302 }
25303
25304 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25305 Set the mark field in every compilation unit in the
25306 cache that we must keep because we are keeping CU. */
25307
25308 static int
25309 dwarf2_mark_helper (void **slot, void *data)
25310 {
25311 struct dwarf2_per_cu_data *per_cu;
25312
25313 per_cu = (struct dwarf2_per_cu_data *) *slot;
25314
25315 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25316 reading of the chain. As such dependencies remain valid it is not much
25317 useful to track and undo them during QUIT cleanups. */
25318 if (per_cu->cu == NULL)
25319 return 1;
25320
25321 if (per_cu->cu->mark)
25322 return 1;
25323 per_cu->cu->mark = true;
25324
25325 if (per_cu->cu->dependencies != NULL)
25326 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25327
25328 return 1;
25329 }
25330
25331 /* Set the mark field in CU and in every other compilation unit in the
25332 cache that we must keep because we are keeping CU. */
25333
25334 static void
25335 dwarf2_mark (struct dwarf2_cu *cu)
25336 {
25337 if (cu->mark)
25338 return;
25339 cu->mark = true;
25340 if (cu->dependencies != NULL)
25341 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25342 }
25343
25344 static void
25345 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25346 {
25347 while (per_cu)
25348 {
25349 per_cu->cu->mark = false;
25350 per_cu = per_cu->cu->read_in_chain;
25351 }
25352 }
25353
25354 /* Trivial hash function for partial_die_info: the hash value of a DIE
25355 is its offset in .debug_info for this objfile. */
25356
25357 static hashval_t
25358 partial_die_hash (const void *item)
25359 {
25360 const struct partial_die_info *part_die
25361 = (const struct partial_die_info *) item;
25362
25363 return to_underlying (part_die->sect_off);
25364 }
25365
25366 /* Trivial comparison function for partial_die_info structures: two DIEs
25367 are equal if they have the same offset. */
25368
25369 static int
25370 partial_die_eq (const void *item_lhs, const void *item_rhs)
25371 {
25372 const struct partial_die_info *part_die_lhs
25373 = (const struct partial_die_info *) item_lhs;
25374 const struct partial_die_info *part_die_rhs
25375 = (const struct partial_die_info *) item_rhs;
25376
25377 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25378 }
25379
25380 struct cmd_list_element *set_dwarf_cmdlist;
25381 struct cmd_list_element *show_dwarf_cmdlist;
25382
25383 static void
25384 set_dwarf_cmd (const char *args, int from_tty)
25385 {
25386 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25387 gdb_stdout);
25388 }
25389
25390 static void
25391 show_dwarf_cmd (const char *args, int from_tty)
25392 {
25393 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25394 }
25395
25396 bool dwarf_always_disassemble;
25397
25398 static void
25399 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25400 struct cmd_list_element *c, const char *value)
25401 {
25402 fprintf_filtered (file,
25403 _("Whether to always disassemble "
25404 "DWARF expressions is %s.\n"),
25405 value);
25406 }
25407
25408 static void
25409 show_check_physname (struct ui_file *file, int from_tty,
25410 struct cmd_list_element *c, const char *value)
25411 {
25412 fprintf_filtered (file,
25413 _("Whether to check \"physname\" is %s.\n"),
25414 value);
25415 }
25416
25417 void _initialize_dwarf2_read ();
25418 void
25419 _initialize_dwarf2_read ()
25420 {
25421 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25422 Set DWARF specific variables.\n\
25423 Configure DWARF variables such as the cache size."),
25424 &set_dwarf_cmdlist, "maintenance set dwarf ",
25425 0/*allow-unknown*/, &maintenance_set_cmdlist);
25426
25427 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25428 Show DWARF specific variables.\n\
25429 Show DWARF variables such as the cache size."),
25430 &show_dwarf_cmdlist, "maintenance show dwarf ",
25431 0/*allow-unknown*/, &maintenance_show_cmdlist);
25432
25433 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25434 &dwarf_max_cache_age, _("\
25435 Set the upper bound on the age of cached DWARF compilation units."), _("\
25436 Show the upper bound on the age of cached DWARF compilation units."), _("\
25437 A higher limit means that cached compilation units will be stored\n\
25438 in memory longer, and more total memory will be used. Zero disables\n\
25439 caching, which can slow down startup."),
25440 NULL,
25441 show_dwarf_max_cache_age,
25442 &set_dwarf_cmdlist,
25443 &show_dwarf_cmdlist);
25444
25445 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25446 &dwarf_always_disassemble, _("\
25447 Set whether `info address' always disassembles DWARF expressions."), _("\
25448 Show whether `info address' always disassembles DWARF expressions."), _("\
25449 When enabled, DWARF expressions are always printed in an assembly-like\n\
25450 syntax. When disabled, expressions will be printed in a more\n\
25451 conversational style, when possible."),
25452 NULL,
25453 show_dwarf_always_disassemble,
25454 &set_dwarf_cmdlist,
25455 &show_dwarf_cmdlist);
25456
25457 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25458 Set debugging of the DWARF reader."), _("\
25459 Show debugging of the DWARF reader."), _("\
25460 When enabled (non-zero), debugging messages are printed during DWARF\n\
25461 reading and symtab expansion. A value of 1 (one) provides basic\n\
25462 information. A value greater than 1 provides more verbose information."),
25463 NULL,
25464 NULL,
25465 &setdebuglist, &showdebuglist);
25466
25467 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25468 Set debugging of the DWARF DIE reader."), _("\
25469 Show debugging of the DWARF DIE reader."), _("\
25470 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25471 The value is the maximum depth to print."),
25472 NULL,
25473 NULL,
25474 &setdebuglist, &showdebuglist);
25475
25476 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25477 Set debugging of the dwarf line reader."), _("\
25478 Show debugging of the dwarf line reader."), _("\
25479 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25480 A value of 1 (one) provides basic information.\n\
25481 A value greater than 1 provides more verbose information."),
25482 NULL,
25483 NULL,
25484 &setdebuglist, &showdebuglist);
25485
25486 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25487 Set cross-checking of \"physname\" code against demangler."), _("\
25488 Show cross-checking of \"physname\" code against demangler."), _("\
25489 When enabled, GDB's internal \"physname\" code is checked against\n\
25490 the demangler."),
25491 NULL, show_check_physname,
25492 &setdebuglist, &showdebuglist);
25493
25494 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25495 no_class, &use_deprecated_index_sections, _("\
25496 Set whether to use deprecated gdb_index sections."), _("\
25497 Show whether to use deprecated gdb_index sections."), _("\
25498 When enabled, deprecated .gdb_index sections are used anyway.\n\
25499 Normally they are ignored either because of a missing feature or\n\
25500 performance issue.\n\
25501 Warning: This option must be enabled before gdb reads the file."),
25502 NULL,
25503 NULL,
25504 &setlist, &showlist);
25505
25506 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25507 &dwarf2_locexpr_funcs);
25508 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25509 &dwarf2_loclist_funcs);
25510
25511 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25512 &dwarf2_block_frame_base_locexpr_funcs);
25513 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25514 &dwarf2_block_frame_base_loclist_funcs);
25515
25516 #if GDB_SELF_TEST
25517 selftests::register_test ("dw2_expand_symtabs_matching",
25518 selftests::dw2_expand_symtabs_matching::run_test);
25519 #endif
25520 }
This page took 0.550134 seconds and 4 git commands to generate.