Create dwarf2/abbrev.[ch]
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf-index-cache.h"
35 #include "dwarf-index-common.h"
36 #include "dwarf2/leb.h"
37 #include "bfd.h"
38 #include "elf-bfd.h"
39 #include "symtab.h"
40 #include "gdbtypes.h"
41 #include "objfiles.h"
42 #include "dwarf2.h"
43 #include "buildsym.h"
44 #include "demangle.h"
45 #include "gdb-demangle.h"
46 #include "filenames.h" /* for DOSish file names */
47 #include "macrotab.h"
48 #include "language.h"
49 #include "complaints.h"
50 #include "dwarf2expr.h"
51 #include "dwarf2loc.h"
52 #include "cp-support.h"
53 #include "hashtab.h"
54 #include "command.h"
55 #include "gdbcmd.h"
56 #include "block.h"
57 #include "addrmap.h"
58 #include "typeprint.h"
59 #include "psympriv.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include "gdb_bfd.h"
66 #include "f-lang.h"
67 #include "source.h"
68 #include "build-id.h"
69 #include "namespace.h"
70 #include "gdbsupport/function-view.h"
71 #include "gdbsupport/gdb_optional.h"
72 #include "gdbsupport/underlying.h"
73 #include "gdbsupport/hash_enum.h"
74 #include "filename-seen-cache.h"
75 #include "producer.h"
76 #include <fcntl.h>
77 #include <algorithm>
78 #include <unordered_map>
79 #include "gdbsupport/selftest.h"
80 #include "rust-lang.h"
81 #include "gdbsupport/pathstuff.h"
82
83 /* When == 1, print basic high level tracing messages.
84 When > 1, be more verbose.
85 This is in contrast to the low level DIE reading of dwarf_die_debug. */
86 static unsigned int dwarf_read_debug = 0;
87
88 /* When non-zero, dump DIEs after they are read in. */
89 static unsigned int dwarf_die_debug = 0;
90
91 /* When non-zero, dump line number entries as they are read in. */
92 static unsigned int dwarf_line_debug = 0;
93
94 /* When true, cross-check physname against demangler. */
95 static bool check_physname = false;
96
97 /* When true, do not reject deprecated .gdb_index sections. */
98 static bool use_deprecated_index_sections = false;
99
100 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
101
102 /* The "aclass" indices for various kinds of computed DWARF symbols. */
103
104 static int dwarf2_locexpr_index;
105 static int dwarf2_loclist_index;
106 static int dwarf2_locexpr_block_index;
107 static int dwarf2_loclist_block_index;
108
109 /* An index into a (C++) symbol name component in a symbol name as
110 recorded in the mapped_index's symbol table. For each C++ symbol
111 in the symbol table, we record one entry for the start of each
112 component in the symbol in a table of name components, and then
113 sort the table, in order to be able to binary search symbol names,
114 ignoring leading namespaces, both completion and regular look up.
115 For example, for symbol "A::B::C", we'll have an entry that points
116 to "A::B::C", another that points to "B::C", and another for "C".
117 Note that function symbols in GDB index have no parameter
118 information, just the function/method names. You can convert a
119 name_component to a "const char *" using the
120 'mapped_index::symbol_name_at(offset_type)' method. */
121
122 struct name_component
123 {
124 /* Offset in the symbol name where the component starts. Stored as
125 a (32-bit) offset instead of a pointer to save memory and improve
126 locality on 64-bit architectures. */
127 offset_type name_offset;
128
129 /* The symbol's index in the symbol and constant pool tables of a
130 mapped_index. */
131 offset_type idx;
132 };
133
134 /* Base class containing bits shared by both .gdb_index and
135 .debug_name indexes. */
136
137 struct mapped_index_base
138 {
139 mapped_index_base () = default;
140 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
141
142 /* The name_component table (a sorted vector). See name_component's
143 description above. */
144 std::vector<name_component> name_components;
145
146 /* How NAME_COMPONENTS is sorted. */
147 enum case_sensitivity name_components_casing;
148
149 /* Return the number of names in the symbol table. */
150 virtual size_t symbol_name_count () const = 0;
151
152 /* Get the name of the symbol at IDX in the symbol table. */
153 virtual const char *symbol_name_at (offset_type idx) const = 0;
154
155 /* Return whether the name at IDX in the symbol table should be
156 ignored. */
157 virtual bool symbol_name_slot_invalid (offset_type idx) const
158 {
159 return false;
160 }
161
162 /* Build the symbol name component sorted vector, if we haven't
163 yet. */
164 void build_name_components ();
165
166 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
167 possible matches for LN_NO_PARAMS in the name component
168 vector. */
169 std::pair<std::vector<name_component>::const_iterator,
170 std::vector<name_component>::const_iterator>
171 find_name_components_bounds (const lookup_name_info &ln_no_params,
172 enum language lang) const;
173
174 /* Prevent deleting/destroying via a base class pointer. */
175 protected:
176 ~mapped_index_base() = default;
177 };
178
179 /* A description of the mapped index. The file format is described in
180 a comment by the code that writes the index. */
181 struct mapped_index final : public mapped_index_base
182 {
183 /* A slot/bucket in the symbol table hash. */
184 struct symbol_table_slot
185 {
186 const offset_type name;
187 const offset_type vec;
188 };
189
190 /* Index data format version. */
191 int version = 0;
192
193 /* The address table data. */
194 gdb::array_view<const gdb_byte> address_table;
195
196 /* The symbol table, implemented as a hash table. */
197 gdb::array_view<symbol_table_slot> symbol_table;
198
199 /* A pointer to the constant pool. */
200 const char *constant_pool = nullptr;
201
202 bool symbol_name_slot_invalid (offset_type idx) const override
203 {
204 const auto &bucket = this->symbol_table[idx];
205 return bucket.name == 0 && bucket.vec == 0;
206 }
207
208 /* Convenience method to get at the name of the symbol at IDX in the
209 symbol table. */
210 const char *symbol_name_at (offset_type idx) const override
211 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
212
213 size_t symbol_name_count () const override
214 { return this->symbol_table.size (); }
215 };
216
217 /* A description of the mapped .debug_names.
218 Uninitialized map has CU_COUNT 0. */
219 struct mapped_debug_names final : public mapped_index_base
220 {
221 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
222 : dwarf2_per_objfile (dwarf2_per_objfile_)
223 {}
224
225 struct dwarf2_per_objfile *dwarf2_per_objfile;
226 bfd_endian dwarf5_byte_order;
227 bool dwarf5_is_dwarf64;
228 bool augmentation_is_gdb;
229 uint8_t offset_size;
230 uint32_t cu_count = 0;
231 uint32_t tu_count, bucket_count, name_count;
232 const gdb_byte *cu_table_reordered, *tu_table_reordered;
233 const uint32_t *bucket_table_reordered, *hash_table_reordered;
234 const gdb_byte *name_table_string_offs_reordered;
235 const gdb_byte *name_table_entry_offs_reordered;
236 const gdb_byte *entry_pool;
237
238 struct index_val
239 {
240 ULONGEST dwarf_tag;
241 struct attr
242 {
243 /* Attribute name DW_IDX_*. */
244 ULONGEST dw_idx;
245
246 /* Attribute form DW_FORM_*. */
247 ULONGEST form;
248
249 /* Value if FORM is DW_FORM_implicit_const. */
250 LONGEST implicit_const;
251 };
252 std::vector<attr> attr_vec;
253 };
254
255 std::unordered_map<ULONGEST, index_val> abbrev_map;
256
257 const char *namei_to_name (uint32_t namei) const;
258
259 /* Implementation of the mapped_index_base virtual interface, for
260 the name_components cache. */
261
262 const char *symbol_name_at (offset_type idx) const override
263 { return namei_to_name (idx); }
264
265 size_t symbol_name_count () const override
266 { return this->name_count; }
267 };
268
269 /* See dwarf2read.h. */
270
271 dwarf2_per_objfile *
272 get_dwarf2_per_objfile (struct objfile *objfile)
273 {
274 return dwarf2_objfile_data_key.get (objfile);
275 }
276
277 /* Default names of the debugging sections. */
278
279 /* Note that if the debugging section has been compressed, it might
280 have a name like .zdebug_info. */
281
282 static const struct dwarf2_debug_sections dwarf2_elf_names =
283 {
284 { ".debug_info", ".zdebug_info" },
285 { ".debug_abbrev", ".zdebug_abbrev" },
286 { ".debug_line", ".zdebug_line" },
287 { ".debug_loc", ".zdebug_loc" },
288 { ".debug_loclists", ".zdebug_loclists" },
289 { ".debug_macinfo", ".zdebug_macinfo" },
290 { ".debug_macro", ".zdebug_macro" },
291 { ".debug_str", ".zdebug_str" },
292 { ".debug_str_offsets", ".zdebug_str_offsets" },
293 { ".debug_line_str", ".zdebug_line_str" },
294 { ".debug_ranges", ".zdebug_ranges" },
295 { ".debug_rnglists", ".zdebug_rnglists" },
296 { ".debug_types", ".zdebug_types" },
297 { ".debug_addr", ".zdebug_addr" },
298 { ".debug_frame", ".zdebug_frame" },
299 { ".eh_frame", NULL },
300 { ".gdb_index", ".zgdb_index" },
301 { ".debug_names", ".zdebug_names" },
302 { ".debug_aranges", ".zdebug_aranges" },
303 23
304 };
305
306 /* List of DWO/DWP sections. */
307
308 static const struct dwop_section_names
309 {
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
314 struct dwarf2_section_names loclists_dwo;
315 struct dwarf2_section_names macinfo_dwo;
316 struct dwarf2_section_names macro_dwo;
317 struct dwarf2_section_names str_dwo;
318 struct dwarf2_section_names str_offsets_dwo;
319 struct dwarf2_section_names types_dwo;
320 struct dwarf2_section_names cu_index;
321 struct dwarf2_section_names tu_index;
322 }
323 dwop_section_names =
324 {
325 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
326 { ".debug_info.dwo", ".zdebug_info.dwo" },
327 { ".debug_line.dwo", ".zdebug_line.dwo" },
328 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
329 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
337 };
338
339 /* local data types */
340
341 /* The data in a compilation unit header, after target2host
342 translation, looks like this. */
343 struct comp_unit_head
344 {
345 unsigned int length;
346 short version;
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
349 sect_offset abbrev_sect_off;
350
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
353
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
356
357 enum dwarf_unit_type unit_type;
358
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
361 sect_offset sect_off;
362
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
365 cu_offset first_die_cu_offset;
366
367
368 /* 64-bit signature of this unit. For type units, it denotes the signature of
369 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
370 Also used in DWARF 5, to denote the dwo id when the unit type is
371 DW_UT_skeleton or DW_UT_split_compile. */
372 ULONGEST signature;
373
374 /* For types, offset in the type's DIE of the type defined by this TU. */
375 cu_offset type_cu_offset_in_tu;
376 };
377
378 /* Type used for delaying computation of method physnames.
379 See comments for compute_delayed_physnames. */
380 struct delayed_method_info
381 {
382 /* The type to which the method is attached, i.e., its parent class. */
383 struct type *type;
384
385 /* The index of the method in the type's function fieldlists. */
386 int fnfield_index;
387
388 /* The index of the method in the fieldlist. */
389 int index;
390
391 /* The name of the DIE. */
392 const char *name;
393
394 /* The DIE associated with this method. */
395 struct die_info *die;
396 };
397
398 /* Internal state when decoding a particular compilation unit. */
399 struct dwarf2_cu
400 {
401 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
402 ~dwarf2_cu ();
403
404 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
405
406 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
407 Create the set of symtabs used by this TU, or if this TU is sharing
408 symtabs with another TU and the symtabs have already been created
409 then restore those symtabs in the line header.
410 We don't need the pc/line-number mapping for type units. */
411 void setup_type_unit_groups (struct die_info *die);
412
413 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
414 buildsym_compunit constructor. */
415 struct compunit_symtab *start_symtab (const char *name,
416 const char *comp_dir,
417 CORE_ADDR low_pc);
418
419 /* Reset the builder. */
420 void reset_builder () { m_builder.reset (); }
421
422 /* The header of the compilation unit. */
423 struct comp_unit_head header {};
424
425 /* Base address of this compilation unit. */
426 CORE_ADDR base_address = 0;
427
428 /* Non-zero if base_address has been set. */
429 int base_known = 0;
430
431 /* The language we are debugging. */
432 enum language language = language_unknown;
433 const struct language_defn *language_defn = nullptr;
434
435 const char *producer = nullptr;
436
437 private:
438 /* The symtab builder for this CU. This is only non-NULL when full
439 symbols are being read. */
440 std::unique_ptr<buildsym_compunit> m_builder;
441
442 public:
443 /* The generic symbol table building routines have separate lists for
444 file scope symbols and all all other scopes (local scopes). So
445 we need to select the right one to pass to add_symbol_to_list().
446 We do it by keeping a pointer to the correct list in list_in_scope.
447
448 FIXME: The original dwarf code just treated the file scope as the
449 first local scope, and all other local scopes as nested local
450 scopes, and worked fine. Check to see if we really need to
451 distinguish these in buildsym.c. */
452 struct pending **list_in_scope = nullptr;
453
454 /* Hash table holding all the loaded partial DIEs
455 with partial_die->offset.SECT_OFF as hash. */
456 htab_t partial_dies = nullptr;
457
458 /* Storage for things with the same lifetime as this read-in compilation
459 unit, including partial DIEs. */
460 auto_obstack comp_unit_obstack;
461
462 /* When multiple dwarf2_cu structures are living in memory, this field
463 chains them all together, so that they can be released efficiently.
464 We will probably also want a generation counter so that most-recently-used
465 compilation units are cached... */
466 struct dwarf2_per_cu_data *read_in_chain = nullptr;
467
468 /* Backlink to our per_cu entry. */
469 struct dwarf2_per_cu_data *per_cu;
470
471 /* How many compilation units ago was this CU last referenced? */
472 int last_used = 0;
473
474 /* A hash table of DIE cu_offset for following references with
475 die_info->offset.sect_off as hash. */
476 htab_t die_hash = nullptr;
477
478 /* Full DIEs if read in. */
479 struct die_info *dies = nullptr;
480
481 /* A set of pointers to dwarf2_per_cu_data objects for compilation
482 units referenced by this one. Only set during full symbol processing;
483 partial symbol tables do not have dependencies. */
484 htab_t dependencies = nullptr;
485
486 /* Header data from the line table, during full symbol processing. */
487 struct line_header *line_header = nullptr;
488 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
489 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
490 this is the DW_TAG_compile_unit die for this CU. We'll hold on
491 to the line header as long as this DIE is being processed. See
492 process_die_scope. */
493 die_info *line_header_die_owner = nullptr;
494
495 /* A list of methods which need to have physnames computed
496 after all type information has been read. */
497 std::vector<delayed_method_info> method_list;
498
499 /* To be copied to symtab->call_site_htab. */
500 htab_t call_site_htab = nullptr;
501
502 /* Non-NULL if this CU came from a DWO file.
503 There is an invariant here that is important to remember:
504 Except for attributes copied from the top level DIE in the "main"
505 (or "stub") file in preparation for reading the DWO file
506 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
507 Either there isn't a DWO file (in which case this is NULL and the point
508 is moot), or there is and either we're not going to read it (in which
509 case this is NULL) or there is and we are reading it (in which case this
510 is non-NULL). */
511 struct dwo_unit *dwo_unit = nullptr;
512
513 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
514 Note this value comes from the Fission stub CU/TU's DIE. */
515 gdb::optional<ULONGEST> addr_base;
516
517 /* The DW_AT_rnglists_base attribute if present.
518 Note this value comes from the Fission stub CU/TU's DIE.
519 Also note that the value is zero in the non-DWO case so this value can
520 be used without needing to know whether DWO files are in use or not.
521 N.B. This does not apply to DW_AT_ranges appearing in
522 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
523 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
524 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
525 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
526 ULONGEST ranges_base = 0;
527
528 /* When reading debug info generated by older versions of rustc, we
529 have to rewrite some union types to be struct types with a
530 variant part. This rewriting must be done after the CU is fully
531 read in, because otherwise at the point of rewriting some struct
532 type might not have been fully processed. So, we keep a list of
533 all such types here and process them after expansion. */
534 std::vector<struct type *> rust_unions;
535
536 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
537 files, the value is implicitly zero. For DWARF 5 version DWO files, the
538 value is often implicit and is the size of the header of
539 .debug_str_offsets section (8 or 4, depending on the address size). */
540 gdb::optional<ULONGEST> str_offsets_base;
541
542 /* Mark used when releasing cached dies. */
543 bool mark : 1;
544
545 /* This CU references .debug_loc. See the symtab->locations_valid field.
546 This test is imperfect as there may exist optimized debug code not using
547 any location list and still facing inlining issues if handled as
548 unoptimized code. For a future better test see GCC PR other/32998. */
549 bool has_loclist : 1;
550
551 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
552 if all the producer_is_* fields are valid. This information is cached
553 because profiling CU expansion showed excessive time spent in
554 producer_is_gxx_lt_4_6. */
555 bool checked_producer : 1;
556 bool producer_is_gxx_lt_4_6 : 1;
557 bool producer_is_gcc_lt_4_3 : 1;
558 bool producer_is_icc : 1;
559 bool producer_is_icc_lt_14 : 1;
560 bool producer_is_codewarrior : 1;
561
562 /* When true, the file that we're processing is known to have
563 debugging info for C++ namespaces. GCC 3.3.x did not produce
564 this information, but later versions do. */
565
566 bool processing_has_namespace_info : 1;
567
568 struct partial_die_info *find_partial_die (sect_offset sect_off);
569
570 /* If this CU was inherited by another CU (via specification,
571 abstract_origin, etc), this is the ancestor CU. */
572 dwarf2_cu *ancestor;
573
574 /* Get the buildsym_compunit for this CU. */
575 buildsym_compunit *get_builder ()
576 {
577 /* If this CU has a builder associated with it, use that. */
578 if (m_builder != nullptr)
579 return m_builder.get ();
580
581 /* Otherwise, search ancestors for a valid builder. */
582 if (ancestor != nullptr)
583 return ancestor->get_builder ();
584
585 return nullptr;
586 }
587 };
588
589 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
590 This includes type_unit_group and quick_file_names. */
591
592 struct stmt_list_hash
593 {
594 /* The DWO unit this table is from or NULL if there is none. */
595 struct dwo_unit *dwo_unit;
596
597 /* Offset in .debug_line or .debug_line.dwo. */
598 sect_offset line_sect_off;
599 };
600
601 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
602 an object of this type. */
603
604 struct type_unit_group
605 {
606 /* dwarf2read.c's main "handle" on a TU symtab.
607 To simplify things we create an artificial CU that "includes" all the
608 type units using this stmt_list so that the rest of the code still has
609 a "per_cu" handle on the symtab.
610 This PER_CU is recognized by having no section. */
611 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
612 struct dwarf2_per_cu_data per_cu;
613
614 /* The TUs that share this DW_AT_stmt_list entry.
615 This is added to while parsing type units to build partial symtabs,
616 and is deleted afterwards and not used again. */
617 std::vector<signatured_type *> *tus;
618
619 /* The compunit symtab.
620 Type units in a group needn't all be defined in the same source file,
621 so we create an essentially anonymous symtab as the compunit symtab. */
622 struct compunit_symtab *compunit_symtab;
623
624 /* The data used to construct the hash key. */
625 struct stmt_list_hash hash;
626
627 /* The number of symtabs from the line header.
628 The value here must match line_header.num_file_names. */
629 unsigned int num_symtabs;
630
631 /* The symbol tables for this TU (obtained from the files listed in
632 DW_AT_stmt_list).
633 WARNING: The order of entries here must match the order of entries
634 in the line header. After the first TU using this type_unit_group, the
635 line header for the subsequent TUs is recreated from this. This is done
636 because we need to use the same symtabs for each TU using the same
637 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
638 there's no guarantee the line header doesn't have duplicate entries. */
639 struct symtab **symtabs;
640 };
641
642 /* These sections are what may appear in a (real or virtual) DWO file. */
643
644 struct dwo_sections
645 {
646 struct dwarf2_section_info abbrev;
647 struct dwarf2_section_info line;
648 struct dwarf2_section_info loc;
649 struct dwarf2_section_info loclists;
650 struct dwarf2_section_info macinfo;
651 struct dwarf2_section_info macro;
652 struct dwarf2_section_info str;
653 struct dwarf2_section_info str_offsets;
654 /* In the case of a virtual DWO file, these two are unused. */
655 struct dwarf2_section_info info;
656 std::vector<dwarf2_section_info> types;
657 };
658
659 /* CUs/TUs in DWP/DWO files. */
660
661 struct dwo_unit
662 {
663 /* Backlink to the containing struct dwo_file. */
664 struct dwo_file *dwo_file;
665
666 /* The "id" that distinguishes this CU/TU.
667 .debug_info calls this "dwo_id", .debug_types calls this "signature".
668 Since signatures came first, we stick with it for consistency. */
669 ULONGEST signature;
670
671 /* The section this CU/TU lives in, in the DWO file. */
672 struct dwarf2_section_info *section;
673
674 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
675 sect_offset sect_off;
676 unsigned int length;
677
678 /* For types, offset in the type's DIE of the type defined by this TU. */
679 cu_offset type_offset_in_tu;
680 };
681
682 /* include/dwarf2.h defines the DWP section codes.
683 It defines a max value but it doesn't define a min value, which we
684 use for error checking, so provide one. */
685
686 enum dwp_v2_section_ids
687 {
688 DW_SECT_MIN = 1
689 };
690
691 /* Data for one DWO file.
692
693 This includes virtual DWO files (a virtual DWO file is a DWO file as it
694 appears in a DWP file). DWP files don't really have DWO files per se -
695 comdat folding of types "loses" the DWO file they came from, and from
696 a high level view DWP files appear to contain a mass of random types.
697 However, to maintain consistency with the non-DWP case we pretend DWP
698 files contain virtual DWO files, and we assign each TU with one virtual
699 DWO file (generally based on the line and abbrev section offsets -
700 a heuristic that seems to work in practice). */
701
702 struct dwo_file
703 {
704 dwo_file () = default;
705 DISABLE_COPY_AND_ASSIGN (dwo_file);
706
707 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
708 For virtual DWO files the name is constructed from the section offsets
709 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
710 from related CU+TUs. */
711 const char *dwo_name = nullptr;
712
713 /* The DW_AT_comp_dir attribute. */
714 const char *comp_dir = nullptr;
715
716 /* The bfd, when the file is open. Otherwise this is NULL.
717 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
718 gdb_bfd_ref_ptr dbfd;
719
720 /* The sections that make up this DWO file.
721 Remember that for virtual DWO files in DWP V2, these are virtual
722 sections (for lack of a better name). */
723 struct dwo_sections sections {};
724
725 /* The CUs in the file.
726 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
727 an extension to handle LLVM's Link Time Optimization output (where
728 multiple source files may be compiled into a single object/dwo pair). */
729 htab_t cus {};
730
731 /* Table of TUs in the file.
732 Each element is a struct dwo_unit. */
733 htab_t tus {};
734 };
735
736 /* These sections are what may appear in a DWP file. */
737
738 struct dwp_sections
739 {
740 /* These are used by both DWP version 1 and 2. */
741 struct dwarf2_section_info str;
742 struct dwarf2_section_info cu_index;
743 struct dwarf2_section_info tu_index;
744
745 /* These are only used by DWP version 2 files.
746 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
747 sections are referenced by section number, and are not recorded here.
748 In DWP version 2 there is at most one copy of all these sections, each
749 section being (effectively) comprised of the concatenation of all of the
750 individual sections that exist in the version 1 format.
751 To keep the code simple we treat each of these concatenated pieces as a
752 section itself (a virtual section?). */
753 struct dwarf2_section_info abbrev;
754 struct dwarf2_section_info info;
755 struct dwarf2_section_info line;
756 struct dwarf2_section_info loc;
757 struct dwarf2_section_info macinfo;
758 struct dwarf2_section_info macro;
759 struct dwarf2_section_info str_offsets;
760 struct dwarf2_section_info types;
761 };
762
763 /* These sections are what may appear in a virtual DWO file in DWP version 1.
764 A virtual DWO file is a DWO file as it appears in a DWP file. */
765
766 struct virtual_v1_dwo_sections
767 {
768 struct dwarf2_section_info abbrev;
769 struct dwarf2_section_info line;
770 struct dwarf2_section_info loc;
771 struct dwarf2_section_info macinfo;
772 struct dwarf2_section_info macro;
773 struct dwarf2_section_info str_offsets;
774 /* Each DWP hash table entry records one CU or one TU.
775 That is recorded here, and copied to dwo_unit.section. */
776 struct dwarf2_section_info info_or_types;
777 };
778
779 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
780 In version 2, the sections of the DWO files are concatenated together
781 and stored in one section of that name. Thus each ELF section contains
782 several "virtual" sections. */
783
784 struct virtual_v2_dwo_sections
785 {
786 bfd_size_type abbrev_offset;
787 bfd_size_type abbrev_size;
788
789 bfd_size_type line_offset;
790 bfd_size_type line_size;
791
792 bfd_size_type loc_offset;
793 bfd_size_type loc_size;
794
795 bfd_size_type macinfo_offset;
796 bfd_size_type macinfo_size;
797
798 bfd_size_type macro_offset;
799 bfd_size_type macro_size;
800
801 bfd_size_type str_offsets_offset;
802 bfd_size_type str_offsets_size;
803
804 /* Each DWP hash table entry records one CU or one TU.
805 That is recorded here, and copied to dwo_unit.section. */
806 bfd_size_type info_or_types_offset;
807 bfd_size_type info_or_types_size;
808 };
809
810 /* Contents of DWP hash tables. */
811
812 struct dwp_hash_table
813 {
814 uint32_t version, nr_columns;
815 uint32_t nr_units, nr_slots;
816 const gdb_byte *hash_table, *unit_table;
817 union
818 {
819 struct
820 {
821 const gdb_byte *indices;
822 } v1;
823 struct
824 {
825 /* This is indexed by column number and gives the id of the section
826 in that column. */
827 #define MAX_NR_V2_DWO_SECTIONS \
828 (1 /* .debug_info or .debug_types */ \
829 + 1 /* .debug_abbrev */ \
830 + 1 /* .debug_line */ \
831 + 1 /* .debug_loc */ \
832 + 1 /* .debug_str_offsets */ \
833 + 1 /* .debug_macro or .debug_macinfo */)
834 int section_ids[MAX_NR_V2_DWO_SECTIONS];
835 const gdb_byte *offsets;
836 const gdb_byte *sizes;
837 } v2;
838 } section_pool;
839 };
840
841 /* Data for one DWP file. */
842
843 struct dwp_file
844 {
845 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
846 : name (name_),
847 dbfd (std::move (abfd))
848 {
849 }
850
851 /* Name of the file. */
852 const char *name;
853
854 /* File format version. */
855 int version = 0;
856
857 /* The bfd. */
858 gdb_bfd_ref_ptr dbfd;
859
860 /* Section info for this file. */
861 struct dwp_sections sections {};
862
863 /* Table of CUs in the file. */
864 const struct dwp_hash_table *cus = nullptr;
865
866 /* Table of TUs in the file. */
867 const struct dwp_hash_table *tus = nullptr;
868
869 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
870 htab_t loaded_cus {};
871 htab_t loaded_tus {};
872
873 /* Table to map ELF section numbers to their sections.
874 This is only needed for the DWP V1 file format. */
875 unsigned int num_sections = 0;
876 asection **elf_sections = nullptr;
877 };
878
879 /* Struct used to pass misc. parameters to read_die_and_children, et
880 al. which are used for both .debug_info and .debug_types dies.
881 All parameters here are unchanging for the life of the call. This
882 struct exists to abstract away the constant parameters of die reading. */
883
884 struct die_reader_specs
885 {
886 /* The bfd of die_section. */
887 bfd* abfd;
888
889 /* The CU of the DIE we are parsing. */
890 struct dwarf2_cu *cu;
891
892 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
893 struct dwo_file *dwo_file;
894
895 /* The section the die comes from.
896 This is either .debug_info or .debug_types, or the .dwo variants. */
897 struct dwarf2_section_info *die_section;
898
899 /* die_section->buffer. */
900 const gdb_byte *buffer;
901
902 /* The end of the buffer. */
903 const gdb_byte *buffer_end;
904
905 /* The value of the DW_AT_comp_dir attribute. */
906 const char *comp_dir;
907
908 /* The abbreviation table to use when reading the DIEs. */
909 struct abbrev_table *abbrev_table;
910 };
911
912 /* A subclass of die_reader_specs that holds storage and has complex
913 constructor and destructor behavior. */
914
915 class cutu_reader : public die_reader_specs
916 {
917 public:
918
919 cutu_reader (struct dwarf2_per_cu_data *this_cu,
920 struct abbrev_table *abbrev_table,
921 int use_existing_cu, int keep,
922 bool skip_partial);
923
924 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
928 ~cutu_reader ();
929
930 DISABLE_COPY_AND_ASSIGN (cutu_reader);
931
932 const gdb_byte *info_ptr = nullptr;
933 struct die_info *comp_unit_die = nullptr;
934 int has_children = 0;
935 bool dummy_p = false;
936
937 private:
938 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
939 int use_existing_cu, int keep);
940
941 struct dwarf2_per_cu_data *m_this_cu;
942 int m_keep = 0;
943 std::unique_ptr<dwarf2_cu> m_new_cu;
944
945 /* The ordinary abbreviation table. */
946 abbrev_table_up m_abbrev_table_holder;
947
948 /* The DWO abbreviation table. */
949 abbrev_table_up m_dwo_abbrev_table;
950 };
951
952 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
953 later. */
954 typedef int dir_index;
955
956 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
957 and later. */
958 typedef int file_name_index;
959
960 struct file_entry
961 {
962 file_entry () = default;
963
964 file_entry (const char *name_, dir_index d_index_,
965 unsigned int mod_time_, unsigned int length_)
966 : name (name_),
967 d_index (d_index_),
968 mod_time (mod_time_),
969 length (length_)
970 {}
971
972 /* Return the include directory at D_INDEX stored in LH. Returns
973 NULL if D_INDEX is out of bounds. */
974 const char *include_dir (const line_header *lh) const;
975
976 /* The file name. Note this is an observing pointer. The memory is
977 owned by debug_line_buffer. */
978 const char *name {};
979
980 /* The directory index (1-based). */
981 dir_index d_index {};
982
983 unsigned int mod_time {};
984
985 unsigned int length {};
986
987 /* True if referenced by the Line Number Program. */
988 bool included_p {};
989
990 /* The associated symbol table, if any. */
991 struct symtab *symtab {};
992 };
993
994 /* The line number information for a compilation unit (found in the
995 .debug_line section) begins with a "statement program header",
996 which contains the following information. */
997 struct line_header
998 {
999 line_header ()
1000 : offset_in_dwz {}
1001 {}
1002
1003 /* Add an entry to the include directory table. */
1004 void add_include_dir (const char *include_dir);
1005
1006 /* Add an entry to the file name table. */
1007 void add_file_name (const char *name, dir_index d_index,
1008 unsigned int mod_time, unsigned int length);
1009
1010 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1011 Returns NULL if INDEX is out of bounds. */
1012 const char *include_dir_at (dir_index index) const
1013 {
1014 int vec_index;
1015 if (version >= 5)
1016 vec_index = index;
1017 else
1018 vec_index = index - 1;
1019 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1020 return NULL;
1021 return m_include_dirs[vec_index];
1022 }
1023
1024 bool is_valid_file_index (int file_index)
1025 {
1026 if (version >= 5)
1027 return 0 <= file_index && file_index < file_names_size ();
1028 return 1 <= file_index && file_index <= file_names_size ();
1029 }
1030
1031 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1032 Returns NULL if INDEX is out of bounds. */
1033 file_entry *file_name_at (file_name_index index)
1034 {
1035 int vec_index;
1036 if (version >= 5)
1037 vec_index = index;
1038 else
1039 vec_index = index - 1;
1040 if (vec_index < 0 || vec_index >= m_file_names.size ())
1041 return NULL;
1042 return &m_file_names[vec_index];
1043 }
1044
1045 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1046 this method should only be used to iterate through all file entries in an
1047 index-agnostic manner. */
1048 std::vector<file_entry> &file_names ()
1049 { return m_file_names; }
1050
1051 /* Offset of line number information in .debug_line section. */
1052 sect_offset sect_off {};
1053
1054 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1055 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1056
1057 unsigned int total_length {};
1058 unsigned short version {};
1059 unsigned int header_length {};
1060 unsigned char minimum_instruction_length {};
1061 unsigned char maximum_ops_per_instruction {};
1062 unsigned char default_is_stmt {};
1063 int line_base {};
1064 unsigned char line_range {};
1065 unsigned char opcode_base {};
1066
1067 /* standard_opcode_lengths[i] is the number of operands for the
1068 standard opcode whose value is i. This means that
1069 standard_opcode_lengths[0] is unused, and the last meaningful
1070 element is standard_opcode_lengths[opcode_base - 1]. */
1071 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1072
1073 int file_names_size ()
1074 { return m_file_names.size(); }
1075
1076 /* The start and end of the statement program following this
1077 header. These point into dwarf2_per_objfile->line_buffer. */
1078 const gdb_byte *statement_program_start {}, *statement_program_end {};
1079
1080 private:
1081 /* The include_directories table. Note these are observing
1082 pointers. The memory is owned by debug_line_buffer. */
1083 std::vector<const char *> m_include_dirs;
1084
1085 /* The file_names table. This is private because the meaning of indexes
1086 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1087 before, and is 0 in DWARF 5 and later). So the client should use
1088 file_name_at method for access. */
1089 std::vector<file_entry> m_file_names;
1090 };
1091
1092 typedef std::unique_ptr<line_header> line_header_up;
1093
1094 const char *
1095 file_entry::include_dir (const line_header *lh) const
1096 {
1097 return lh->include_dir_at (d_index);
1098 }
1099
1100 /* When we construct a partial symbol table entry we only
1101 need this much information. */
1102 struct partial_die_info : public allocate_on_obstack
1103 {
1104 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1105
1106 /* Disable assign but still keep copy ctor, which is needed
1107 load_partial_dies. */
1108 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1109
1110 /* Adjust the partial die before generating a symbol for it. This
1111 function may set the is_external flag or change the DIE's
1112 name. */
1113 void fixup (struct dwarf2_cu *cu);
1114
1115 /* Read a minimal amount of information into the minimal die
1116 structure. */
1117 const gdb_byte *read (const struct die_reader_specs *reader,
1118 const struct abbrev_info &abbrev,
1119 const gdb_byte *info_ptr);
1120
1121 /* Offset of this DIE. */
1122 const sect_offset sect_off;
1123
1124 /* DWARF-2 tag for this DIE. */
1125 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1126
1127 /* Assorted flags describing the data found in this DIE. */
1128 const unsigned int has_children : 1;
1129
1130 unsigned int is_external : 1;
1131 unsigned int is_declaration : 1;
1132 unsigned int has_type : 1;
1133 unsigned int has_specification : 1;
1134 unsigned int has_pc_info : 1;
1135 unsigned int may_be_inlined : 1;
1136
1137 /* This DIE has been marked DW_AT_main_subprogram. */
1138 unsigned int main_subprogram : 1;
1139
1140 /* Flag set if the SCOPE field of this structure has been
1141 computed. */
1142 unsigned int scope_set : 1;
1143
1144 /* Flag set if the DIE has a byte_size attribute. */
1145 unsigned int has_byte_size : 1;
1146
1147 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1148 unsigned int has_const_value : 1;
1149
1150 /* Flag set if any of the DIE's children are template arguments. */
1151 unsigned int has_template_arguments : 1;
1152
1153 /* Flag set if fixup has been called on this die. */
1154 unsigned int fixup_called : 1;
1155
1156 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1157 unsigned int is_dwz : 1;
1158
1159 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1160 unsigned int spec_is_dwz : 1;
1161
1162 /* The name of this DIE. Normally the value of DW_AT_name, but
1163 sometimes a default name for unnamed DIEs. */
1164 const char *name = nullptr;
1165
1166 /* The linkage name, if present. */
1167 const char *linkage_name = nullptr;
1168
1169 /* The scope to prepend to our children. This is generally
1170 allocated on the comp_unit_obstack, so will disappear
1171 when this compilation unit leaves the cache. */
1172 const char *scope = nullptr;
1173
1174 /* Some data associated with the partial DIE. The tag determines
1175 which field is live. */
1176 union
1177 {
1178 /* The location description associated with this DIE, if any. */
1179 struct dwarf_block *locdesc;
1180 /* The offset of an import, for DW_TAG_imported_unit. */
1181 sect_offset sect_off;
1182 } d {};
1183
1184 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1185 CORE_ADDR lowpc = 0;
1186 CORE_ADDR highpc = 0;
1187
1188 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1189 DW_AT_sibling, if any. */
1190 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1191 could return DW_AT_sibling values to its caller load_partial_dies. */
1192 const gdb_byte *sibling = nullptr;
1193
1194 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1195 DW_AT_specification (or DW_AT_abstract_origin or
1196 DW_AT_extension). */
1197 sect_offset spec_offset {};
1198
1199 /* Pointers to this DIE's parent, first child, and next sibling,
1200 if any. */
1201 struct partial_die_info *die_parent = nullptr;
1202 struct partial_die_info *die_child = nullptr;
1203 struct partial_die_info *die_sibling = nullptr;
1204
1205 friend struct partial_die_info *
1206 dwarf2_cu::find_partial_die (sect_offset sect_off);
1207
1208 private:
1209 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1210 partial_die_info (sect_offset sect_off)
1211 : partial_die_info (sect_off, DW_TAG_padding, 0)
1212 {
1213 }
1214
1215 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1216 int has_children_)
1217 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1218 {
1219 is_external = 0;
1220 is_declaration = 0;
1221 has_type = 0;
1222 has_specification = 0;
1223 has_pc_info = 0;
1224 may_be_inlined = 0;
1225 main_subprogram = 0;
1226 scope_set = 0;
1227 has_byte_size = 0;
1228 has_const_value = 0;
1229 has_template_arguments = 0;
1230 fixup_called = 0;
1231 is_dwz = 0;
1232 spec_is_dwz = 0;
1233 }
1234 };
1235
1236 /* Attributes have a name and a value. */
1237 struct attribute
1238 {
1239 ENUM_BITFIELD(dwarf_attribute) name : 16;
1240 ENUM_BITFIELD(dwarf_form) form : 15;
1241
1242 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1243 field should be in u.str (existing only for DW_STRING) but it is kept
1244 here for better struct attribute alignment. */
1245 unsigned int string_is_canonical : 1;
1246
1247 union
1248 {
1249 const char *str;
1250 struct dwarf_block *blk;
1251 ULONGEST unsnd;
1252 LONGEST snd;
1253 CORE_ADDR addr;
1254 ULONGEST signature;
1255 }
1256 u;
1257 };
1258
1259 /* This data structure holds a complete die structure. */
1260 struct die_info
1261 {
1262 /* DWARF-2 tag for this DIE. */
1263 ENUM_BITFIELD(dwarf_tag) tag : 16;
1264
1265 /* Number of attributes */
1266 unsigned char num_attrs;
1267
1268 /* True if we're presently building the full type name for the
1269 type derived from this DIE. */
1270 unsigned char building_fullname : 1;
1271
1272 /* True if this die is in process. PR 16581. */
1273 unsigned char in_process : 1;
1274
1275 /* Abbrev number */
1276 unsigned int abbrev;
1277
1278 /* Offset in .debug_info or .debug_types section. */
1279 sect_offset sect_off;
1280
1281 /* The dies in a compilation unit form an n-ary tree. PARENT
1282 points to this die's parent; CHILD points to the first child of
1283 this node; and all the children of a given node are chained
1284 together via their SIBLING fields. */
1285 struct die_info *child; /* Its first child, if any. */
1286 struct die_info *sibling; /* Its next sibling, if any. */
1287 struct die_info *parent; /* Its parent, if any. */
1288
1289 /* An array of attributes, with NUM_ATTRS elements. There may be
1290 zero, but it's not common and zero-sized arrays are not
1291 sufficiently portable C. */
1292 struct attribute attrs[1];
1293 };
1294
1295 /* Get at parts of an attribute structure. */
1296
1297 #define DW_STRING(attr) ((attr)->u.str)
1298 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1299 #define DW_UNSND(attr) ((attr)->u.unsnd)
1300 #define DW_BLOCK(attr) ((attr)->u.blk)
1301 #define DW_SND(attr) ((attr)->u.snd)
1302 #define DW_ADDR(attr) ((attr)->u.addr)
1303 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1304
1305 /* Blocks are a bunch of untyped bytes. */
1306 struct dwarf_block
1307 {
1308 size_t size;
1309
1310 /* Valid only if SIZE is not zero. */
1311 const gdb_byte *data;
1312 };
1313
1314 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1315 but this would require a corresponding change in unpack_field_as_long
1316 and friends. */
1317 static int bits_per_byte = 8;
1318
1319 /* When reading a variant or variant part, we track a bit more
1320 information about the field, and store it in an object of this
1321 type. */
1322
1323 struct variant_field
1324 {
1325 /* If we see a DW_TAG_variant, then this will be the discriminant
1326 value. */
1327 ULONGEST discriminant_value;
1328 /* If we see a DW_TAG_variant, then this will be set if this is the
1329 default branch. */
1330 bool default_branch;
1331 /* While reading a DW_TAG_variant_part, this will be set if this
1332 field is the discriminant. */
1333 bool is_discriminant;
1334 };
1335
1336 struct nextfield
1337 {
1338 int accessibility = 0;
1339 int virtuality = 0;
1340 /* Extra information to describe a variant or variant part. */
1341 struct variant_field variant {};
1342 struct field field {};
1343 };
1344
1345 struct fnfieldlist
1346 {
1347 const char *name = nullptr;
1348 std::vector<struct fn_field> fnfields;
1349 };
1350
1351 /* The routines that read and process dies for a C struct or C++ class
1352 pass lists of data member fields and lists of member function fields
1353 in an instance of a field_info structure, as defined below. */
1354 struct field_info
1355 {
1356 /* List of data member and baseclasses fields. */
1357 std::vector<struct nextfield> fields;
1358 std::vector<struct nextfield> baseclasses;
1359
1360 /* Number of fields (including baseclasses). */
1361 int nfields = 0;
1362
1363 /* Set if the accessibility of one of the fields is not public. */
1364 int non_public_fields = 0;
1365
1366 /* Member function fieldlist array, contains name of possibly overloaded
1367 member function, number of overloaded member functions and a pointer
1368 to the head of the member function field chain. */
1369 std::vector<struct fnfieldlist> fnfieldlists;
1370
1371 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1372 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1373 std::vector<struct decl_field> typedef_field_list;
1374
1375 /* Nested types defined by this class and the number of elements in this
1376 list. */
1377 std::vector<struct decl_field> nested_types_list;
1378 };
1379
1380 /* One item on the queue of compilation units to read in full symbols
1381 for. */
1382 struct dwarf2_queue_item
1383 {
1384 struct dwarf2_per_cu_data *per_cu;
1385 enum language pretend_language;
1386 struct dwarf2_queue_item *next;
1387 };
1388
1389 /* The current queue. */
1390 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1391
1392 /* Loaded secondary compilation units are kept in memory until they
1393 have not been referenced for the processing of this many
1394 compilation units. Set this to zero to disable caching. Cache
1395 sizes of up to at least twenty will improve startup time for
1396 typical inter-CU-reference binaries, at an obvious memory cost. */
1397 static int dwarf_max_cache_age = 5;
1398 static void
1399 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1400 struct cmd_list_element *c, const char *value)
1401 {
1402 fprintf_filtered (file, _("The upper bound on the age of cached "
1403 "DWARF compilation units is %s.\n"),
1404 value);
1405 }
1406 \f
1407 /* local function prototypes */
1408
1409 static void dwarf2_find_base_address (struct die_info *die,
1410 struct dwarf2_cu *cu);
1411
1412 static dwarf2_psymtab *create_partial_symtab
1413 (struct dwarf2_per_cu_data *per_cu, const char *name);
1414
1415 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1416 const gdb_byte *info_ptr,
1417 struct die_info *type_unit_die,
1418 int has_children);
1419
1420 static void dwarf2_build_psymtabs_hard
1421 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1422
1423 static void scan_partial_symbols (struct partial_die_info *,
1424 CORE_ADDR *, CORE_ADDR *,
1425 int, struct dwarf2_cu *);
1426
1427 static void add_partial_symbol (struct partial_die_info *,
1428 struct dwarf2_cu *);
1429
1430 static void add_partial_namespace (struct partial_die_info *pdi,
1431 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1432 int set_addrmap, struct dwarf2_cu *cu);
1433
1434 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1435 CORE_ADDR *highpc, int set_addrmap,
1436 struct dwarf2_cu *cu);
1437
1438 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1439 struct dwarf2_cu *cu);
1440
1441 static void add_partial_subprogram (struct partial_die_info *pdi,
1442 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1443 int need_pc, struct dwarf2_cu *cu);
1444
1445 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1446
1447 static struct partial_die_info *load_partial_dies
1448 (const struct die_reader_specs *, const gdb_byte *, int);
1449
1450 /* A pair of partial_die_info and compilation unit. */
1451 struct cu_partial_die_info
1452 {
1453 /* The compilation unit of the partial_die_info. */
1454 struct dwarf2_cu *cu;
1455 /* A partial_die_info. */
1456 struct partial_die_info *pdi;
1457
1458 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1459 : cu (cu),
1460 pdi (pdi)
1461 { /* Nothing. */ }
1462
1463 private:
1464 cu_partial_die_info () = delete;
1465 };
1466
1467 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1468 struct dwarf2_cu *);
1469
1470 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1471 struct attribute *, struct attr_abbrev *,
1472 const gdb_byte *, bool *need_reprocess);
1473
1474 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1475 struct attribute *attr);
1476
1477 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1478
1479 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1480 unsigned int *);
1481
1482 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1483
1484 static LONGEST read_checked_initial_length_and_offset
1485 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1486 unsigned int *, unsigned int *);
1487
1488 static LONGEST read_offset (bfd *, const gdb_byte *,
1489 const struct comp_unit_head *,
1490 unsigned int *);
1491
1492 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1493
1494 static sect_offset read_abbrev_offset
1495 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1496 struct dwarf2_section_info *, sect_offset);
1497
1498 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1499
1500 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1501
1502 static const char *read_indirect_string
1503 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1504 const struct comp_unit_head *, unsigned int *);
1505
1506 static const char *read_indirect_line_string
1507 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1508 const struct comp_unit_head *, unsigned int *);
1509
1510 static const char *read_indirect_string_at_offset
1511 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1512 LONGEST str_offset);
1513
1514 static const char *read_indirect_string_from_dwz
1515 (struct objfile *objfile, struct dwz_file *, LONGEST);
1516
1517 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1518 const gdb_byte *,
1519 unsigned int *);
1520
1521 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1522 ULONGEST str_index);
1523
1524 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1525 ULONGEST str_index);
1526
1527 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1528
1529 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1530 struct dwarf2_cu *);
1531
1532 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1533 unsigned int);
1534
1535 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1536 struct dwarf2_cu *cu);
1537
1538 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1539
1540 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1541 struct dwarf2_cu *cu);
1542
1543 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1544
1545 static struct die_info *die_specification (struct die_info *die,
1546 struct dwarf2_cu **);
1547
1548 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1549 struct dwarf2_cu *cu);
1550
1551 static void dwarf_decode_lines (struct line_header *, const char *,
1552 struct dwarf2_cu *, dwarf2_psymtab *,
1553 CORE_ADDR, int decode_mapping);
1554
1555 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1556 const char *);
1557
1558 static struct symbol *new_symbol (struct die_info *, struct type *,
1559 struct dwarf2_cu *, struct symbol * = NULL);
1560
1561 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1562 struct dwarf2_cu *);
1563
1564 static void dwarf2_const_value_attr (const struct attribute *attr,
1565 struct type *type,
1566 const char *name,
1567 struct obstack *obstack,
1568 struct dwarf2_cu *cu, LONGEST *value,
1569 const gdb_byte **bytes,
1570 struct dwarf2_locexpr_baton **baton);
1571
1572 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1573
1574 static int need_gnat_info (struct dwarf2_cu *);
1575
1576 static struct type *die_descriptive_type (struct die_info *,
1577 struct dwarf2_cu *);
1578
1579 static void set_descriptive_type (struct type *, struct die_info *,
1580 struct dwarf2_cu *);
1581
1582 static struct type *die_containing_type (struct die_info *,
1583 struct dwarf2_cu *);
1584
1585 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1586 struct dwarf2_cu *);
1587
1588 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1589
1590 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1591
1592 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1593
1594 static char *typename_concat (struct obstack *obs, const char *prefix,
1595 const char *suffix, int physname,
1596 struct dwarf2_cu *cu);
1597
1598 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1599
1600 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1601
1602 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1603
1604 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1605
1606 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1607
1608 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1609
1610 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1611 struct dwarf2_cu *, dwarf2_psymtab *);
1612
1613 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1614 values. Keep the items ordered with increasing constraints compliance. */
1615 enum pc_bounds_kind
1616 {
1617 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1618 PC_BOUNDS_NOT_PRESENT,
1619
1620 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1621 were present but they do not form a valid range of PC addresses. */
1622 PC_BOUNDS_INVALID,
1623
1624 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1625 PC_BOUNDS_RANGES,
1626
1627 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1628 PC_BOUNDS_HIGH_LOW,
1629 };
1630
1631 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1632 CORE_ADDR *, CORE_ADDR *,
1633 struct dwarf2_cu *,
1634 dwarf2_psymtab *);
1635
1636 static void get_scope_pc_bounds (struct die_info *,
1637 CORE_ADDR *, CORE_ADDR *,
1638 struct dwarf2_cu *);
1639
1640 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1641 CORE_ADDR, struct dwarf2_cu *);
1642
1643 static void dwarf2_add_field (struct field_info *, struct die_info *,
1644 struct dwarf2_cu *);
1645
1646 static void dwarf2_attach_fields_to_type (struct field_info *,
1647 struct type *, struct dwarf2_cu *);
1648
1649 static void dwarf2_add_member_fn (struct field_info *,
1650 struct die_info *, struct type *,
1651 struct dwarf2_cu *);
1652
1653 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1654 struct type *,
1655 struct dwarf2_cu *);
1656
1657 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1658
1659 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1660
1661 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1662
1663 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1664
1665 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1666
1667 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1668
1669 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1670
1671 static struct type *read_module_type (struct die_info *die,
1672 struct dwarf2_cu *cu);
1673
1674 static const char *namespace_name (struct die_info *die,
1675 int *is_anonymous, struct dwarf2_cu *);
1676
1677 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1678
1679 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1680
1681 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1682 struct dwarf2_cu *);
1683
1684 static struct die_info *read_die_and_siblings_1
1685 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1686 struct die_info *);
1687
1688 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1689 const gdb_byte *info_ptr,
1690 const gdb_byte **new_info_ptr,
1691 struct die_info *parent);
1692
1693 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1694 struct die_info **, const gdb_byte *,
1695 int *, int);
1696
1697 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1698 struct die_info **, const gdb_byte *,
1699 int *);
1700
1701 static void process_die (struct die_info *, struct dwarf2_cu *);
1702
1703 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1704 struct obstack *);
1705
1706 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1707
1708 static const char *dwarf2_full_name (const char *name,
1709 struct die_info *die,
1710 struct dwarf2_cu *cu);
1711
1712 static const char *dwarf2_physname (const char *name, struct die_info *die,
1713 struct dwarf2_cu *cu);
1714
1715 static struct die_info *dwarf2_extension (struct die_info *die,
1716 struct dwarf2_cu **);
1717
1718 static const char *dwarf_tag_name (unsigned int);
1719
1720 static const char *dwarf_attr_name (unsigned int);
1721
1722 static const char *dwarf_unit_type_name (int unit_type);
1723
1724 static const char *dwarf_form_name (unsigned int);
1725
1726 static const char *dwarf_bool_name (unsigned int);
1727
1728 static const char *dwarf_type_encoding_name (unsigned int);
1729
1730 static struct die_info *sibling_die (struct die_info *);
1731
1732 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1733
1734 static void dump_die_for_error (struct die_info *);
1735
1736 static void dump_die_1 (struct ui_file *, int level, int max_level,
1737 struct die_info *);
1738
1739 /*static*/ void dump_die (struct die_info *, int max_level);
1740
1741 static void store_in_ref_table (struct die_info *,
1742 struct dwarf2_cu *);
1743
1744 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1745
1746 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1747
1748 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1749 const struct attribute *,
1750 struct dwarf2_cu **);
1751
1752 static struct die_info *follow_die_ref (struct die_info *,
1753 const struct attribute *,
1754 struct dwarf2_cu **);
1755
1756 static struct die_info *follow_die_sig (struct die_info *,
1757 const struct attribute *,
1758 struct dwarf2_cu **);
1759
1760 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1761 struct dwarf2_cu *);
1762
1763 static struct type *get_DW_AT_signature_type (struct die_info *,
1764 const struct attribute *,
1765 struct dwarf2_cu *);
1766
1767 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1768
1769 static void read_signatured_type (struct signatured_type *);
1770
1771 static int attr_to_dynamic_prop (const struct attribute *attr,
1772 struct die_info *die, struct dwarf2_cu *cu,
1773 struct dynamic_prop *prop, struct type *type);
1774
1775 /* memory allocation interface */
1776
1777 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1778
1779 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1780
1781 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1782
1783 static int attr_form_is_block (const struct attribute *);
1784
1785 static int attr_form_is_section_offset (const struct attribute *);
1786
1787 static int attr_form_is_constant (const struct attribute *);
1788
1789 static int attr_form_is_ref (const struct attribute *);
1790
1791 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1792 struct dwarf2_loclist_baton *baton,
1793 const struct attribute *attr);
1794
1795 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1796 struct symbol *sym,
1797 struct dwarf2_cu *cu,
1798 int is_block);
1799
1800 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1801 const gdb_byte *info_ptr,
1802 struct abbrev_info *abbrev);
1803
1804 static hashval_t partial_die_hash (const void *item);
1805
1806 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1807
1808 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1809 (sect_offset sect_off, unsigned int offset_in_dwz,
1810 struct dwarf2_per_objfile *dwarf2_per_objfile);
1811
1812 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1813 struct die_info *comp_unit_die,
1814 enum language pretend_language);
1815
1816 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1817
1818 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1819
1820 static struct type *set_die_type (struct die_info *, struct type *,
1821 struct dwarf2_cu *);
1822
1823 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1824
1825 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1826
1827 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1828 enum language);
1829
1830 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1831 enum language);
1832
1833 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1834 enum language);
1835
1836 static void dwarf2_add_dependence (struct dwarf2_cu *,
1837 struct dwarf2_per_cu_data *);
1838
1839 static void dwarf2_mark (struct dwarf2_cu *);
1840
1841 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1842
1843 static struct type *get_die_type_at_offset (sect_offset,
1844 struct dwarf2_per_cu_data *);
1845
1846 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1847
1848 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1849 enum language pretend_language);
1850
1851 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1852
1853 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1854 static struct type *dwarf2_per_cu_addr_sized_int_type
1855 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1856 static struct type *dwarf2_per_cu_int_type
1857 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1858 bool unsigned_p);
1859
1860 /* Class, the destructor of which frees all allocated queue entries. This
1861 will only have work to do if an error was thrown while processing the
1862 dwarf. If no error was thrown then the queue entries should have all
1863 been processed, and freed, as we went along. */
1864
1865 class dwarf2_queue_guard
1866 {
1867 public:
1868 dwarf2_queue_guard () = default;
1869
1870 /* Free any entries remaining on the queue. There should only be
1871 entries left if we hit an error while processing the dwarf. */
1872 ~dwarf2_queue_guard ()
1873 {
1874 struct dwarf2_queue_item *item, *last;
1875
1876 item = dwarf2_queue;
1877 while (item)
1878 {
1879 /* Anything still marked queued is likely to be in an
1880 inconsistent state, so discard it. */
1881 if (item->per_cu->queued)
1882 {
1883 if (item->per_cu->cu != NULL)
1884 free_one_cached_comp_unit (item->per_cu);
1885 item->per_cu->queued = 0;
1886 }
1887
1888 last = item;
1889 item = item->next;
1890 xfree (last);
1891 }
1892
1893 dwarf2_queue = dwarf2_queue_tail = NULL;
1894 }
1895 };
1896
1897 /* The return type of find_file_and_directory. Note, the enclosed
1898 string pointers are only valid while this object is valid. */
1899
1900 struct file_and_directory
1901 {
1902 /* The filename. This is never NULL. */
1903 const char *name;
1904
1905 /* The compilation directory. NULL if not known. If we needed to
1906 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1907 points directly to the DW_AT_comp_dir string attribute owned by
1908 the obstack that owns the DIE. */
1909 const char *comp_dir;
1910
1911 /* If we needed to build a new string for comp_dir, this is what
1912 owns the storage. */
1913 std::string comp_dir_storage;
1914 };
1915
1916 static file_and_directory find_file_and_directory (struct die_info *die,
1917 struct dwarf2_cu *cu);
1918
1919 static char *file_full_name (int file, struct line_header *lh,
1920 const char *comp_dir);
1921
1922 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1923 enum class rcuh_kind { COMPILE, TYPE };
1924
1925 static const gdb_byte *read_and_check_comp_unit_head
1926 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1927 struct comp_unit_head *header,
1928 struct dwarf2_section_info *section,
1929 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1930 rcuh_kind section_kind);
1931
1932 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1933
1934 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1935
1936 static struct dwo_unit *lookup_dwo_unit_in_dwp
1937 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1938 struct dwp_file *dwp_file, const char *comp_dir,
1939 ULONGEST signature, int is_debug_types);
1940
1941 static struct dwp_file *get_dwp_file
1942 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1943
1944 static struct dwo_unit *lookup_dwo_comp_unit
1945 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1946
1947 static struct dwo_unit *lookup_dwo_type_unit
1948 (struct signatured_type *, const char *, const char *);
1949
1950 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1951
1952 /* A unique pointer to a dwo_file. */
1953
1954 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1955
1956 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1957
1958 static void check_producer (struct dwarf2_cu *cu);
1959
1960 static void free_line_header_voidp (void *arg);
1961 \f
1962 /* Various complaints about symbol reading that don't abort the process. */
1963
1964 static void
1965 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1966 {
1967 complaint (_("statement list doesn't fit in .debug_line section"));
1968 }
1969
1970 static void
1971 dwarf2_debug_line_missing_file_complaint (void)
1972 {
1973 complaint (_(".debug_line section has line data without a file"));
1974 }
1975
1976 static void
1977 dwarf2_debug_line_missing_end_sequence_complaint (void)
1978 {
1979 complaint (_(".debug_line section has line "
1980 "program sequence without an end"));
1981 }
1982
1983 static void
1984 dwarf2_complex_location_expr_complaint (void)
1985 {
1986 complaint (_("location expression too complex"));
1987 }
1988
1989 static void
1990 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1991 int arg3)
1992 {
1993 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1994 arg1, arg2, arg3);
1995 }
1996
1997 static void
1998 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1999 {
2000 complaint (_("debug info runs off end of %s section"
2001 " [in module %s]"),
2002 section->get_name (),
2003 section->get_file_name ());
2004 }
2005
2006 static void
2007 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2008 {
2009 complaint (_("macro debug info contains a "
2010 "malformed macro definition:\n`%s'"),
2011 arg1);
2012 }
2013
2014 static void
2015 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2016 {
2017 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2018 arg1, arg2);
2019 }
2020
2021 /* Hash function for line_header_hash. */
2022
2023 static hashval_t
2024 line_header_hash (const struct line_header *ofs)
2025 {
2026 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2027 }
2028
2029 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2030
2031 static hashval_t
2032 line_header_hash_voidp (const void *item)
2033 {
2034 const struct line_header *ofs = (const struct line_header *) item;
2035
2036 return line_header_hash (ofs);
2037 }
2038
2039 /* Equality function for line_header_hash. */
2040
2041 static int
2042 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2043 {
2044 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2045 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2046
2047 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2048 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2049 }
2050
2051 \f
2052
2053 /* Read the given attribute value as an address, taking the attribute's
2054 form into account. */
2055
2056 static CORE_ADDR
2057 attr_value_as_address (struct attribute *attr)
2058 {
2059 CORE_ADDR addr;
2060
2061 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2062 && attr->form != DW_FORM_GNU_addr_index)
2063 {
2064 /* Aside from a few clearly defined exceptions, attributes that
2065 contain an address must always be in DW_FORM_addr form.
2066 Unfortunately, some compilers happen to be violating this
2067 requirement by encoding addresses using other forms, such
2068 as DW_FORM_data4 for example. For those broken compilers,
2069 we try to do our best, without any guarantee of success,
2070 to interpret the address correctly. It would also be nice
2071 to generate a complaint, but that would require us to maintain
2072 a list of legitimate cases where a non-address form is allowed,
2073 as well as update callers to pass in at least the CU's DWARF
2074 version. This is more overhead than what we're willing to
2075 expand for a pretty rare case. */
2076 addr = DW_UNSND (attr);
2077 }
2078 else
2079 addr = DW_ADDR (attr);
2080
2081 return addr;
2082 }
2083
2084 /* See declaration. */
2085
2086 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2087 const dwarf2_debug_sections *names,
2088 bool can_copy_)
2089 : objfile (objfile_),
2090 can_copy (can_copy_)
2091 {
2092 if (names == NULL)
2093 names = &dwarf2_elf_names;
2094
2095 bfd *obfd = objfile->obfd;
2096
2097 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2098 locate_sections (obfd, sec, *names);
2099 }
2100
2101 dwarf2_per_objfile::~dwarf2_per_objfile ()
2102 {
2103 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2104 free_cached_comp_units ();
2105
2106 if (quick_file_names_table)
2107 htab_delete (quick_file_names_table);
2108
2109 if (line_header_hash)
2110 htab_delete (line_header_hash);
2111
2112 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2113 per_cu->imported_symtabs_free ();
2114
2115 for (signatured_type *sig_type : all_type_units)
2116 sig_type->per_cu.imported_symtabs_free ();
2117
2118 /* Everything else should be on the objfile obstack. */
2119 }
2120
2121 /* See declaration. */
2122
2123 void
2124 dwarf2_per_objfile::free_cached_comp_units ()
2125 {
2126 dwarf2_per_cu_data *per_cu = read_in_chain;
2127 dwarf2_per_cu_data **last_chain = &read_in_chain;
2128 while (per_cu != NULL)
2129 {
2130 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2131
2132 delete per_cu->cu;
2133 *last_chain = next_cu;
2134 per_cu = next_cu;
2135 }
2136 }
2137
2138 /* A helper class that calls free_cached_comp_units on
2139 destruction. */
2140
2141 class free_cached_comp_units
2142 {
2143 public:
2144
2145 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2146 : m_per_objfile (per_objfile)
2147 {
2148 }
2149
2150 ~free_cached_comp_units ()
2151 {
2152 m_per_objfile->free_cached_comp_units ();
2153 }
2154
2155 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2156
2157 private:
2158
2159 dwarf2_per_objfile *m_per_objfile;
2160 };
2161
2162 /* Try to locate the sections we need for DWARF 2 debugging
2163 information and return true if we have enough to do something.
2164 NAMES points to the dwarf2 section names, or is NULL if the standard
2165 ELF names are used. CAN_COPY is true for formats where symbol
2166 interposition is possible and so symbol values must follow copy
2167 relocation rules. */
2168
2169 int
2170 dwarf2_has_info (struct objfile *objfile,
2171 const struct dwarf2_debug_sections *names,
2172 bool can_copy)
2173 {
2174 if (objfile->flags & OBJF_READNEVER)
2175 return 0;
2176
2177 struct dwarf2_per_objfile *dwarf2_per_objfile
2178 = get_dwarf2_per_objfile (objfile);
2179
2180 if (dwarf2_per_objfile == NULL)
2181 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2182 names,
2183 can_copy);
2184
2185 return (!dwarf2_per_objfile->info.is_virtual
2186 && dwarf2_per_objfile->info.s.section != NULL
2187 && !dwarf2_per_objfile->abbrev.is_virtual
2188 && dwarf2_per_objfile->abbrev.s.section != NULL);
2189 }
2190
2191 /* When loading sections, we look either for uncompressed section or for
2192 compressed section names. */
2193
2194 static int
2195 section_is_p (const char *section_name,
2196 const struct dwarf2_section_names *names)
2197 {
2198 if (names->normal != NULL
2199 && strcmp (section_name, names->normal) == 0)
2200 return 1;
2201 if (names->compressed != NULL
2202 && strcmp (section_name, names->compressed) == 0)
2203 return 1;
2204 return 0;
2205 }
2206
2207 /* See declaration. */
2208
2209 void
2210 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2211 const dwarf2_debug_sections &names)
2212 {
2213 flagword aflag = bfd_section_flags (sectp);
2214
2215 if ((aflag & SEC_HAS_CONTENTS) == 0)
2216 {
2217 }
2218 else if (elf_section_data (sectp)->this_hdr.sh_size
2219 > bfd_get_file_size (abfd))
2220 {
2221 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2222 warning (_("Discarding section %s which has a section size (%s"
2223 ") larger than the file size [in module %s]"),
2224 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2225 bfd_get_filename (abfd));
2226 }
2227 else if (section_is_p (sectp->name, &names.info))
2228 {
2229 this->info.s.section = sectp;
2230 this->info.size = bfd_section_size (sectp);
2231 }
2232 else if (section_is_p (sectp->name, &names.abbrev))
2233 {
2234 this->abbrev.s.section = sectp;
2235 this->abbrev.size = bfd_section_size (sectp);
2236 }
2237 else if (section_is_p (sectp->name, &names.line))
2238 {
2239 this->line.s.section = sectp;
2240 this->line.size = bfd_section_size (sectp);
2241 }
2242 else if (section_is_p (sectp->name, &names.loc))
2243 {
2244 this->loc.s.section = sectp;
2245 this->loc.size = bfd_section_size (sectp);
2246 }
2247 else if (section_is_p (sectp->name, &names.loclists))
2248 {
2249 this->loclists.s.section = sectp;
2250 this->loclists.size = bfd_section_size (sectp);
2251 }
2252 else if (section_is_p (sectp->name, &names.macinfo))
2253 {
2254 this->macinfo.s.section = sectp;
2255 this->macinfo.size = bfd_section_size (sectp);
2256 }
2257 else if (section_is_p (sectp->name, &names.macro))
2258 {
2259 this->macro.s.section = sectp;
2260 this->macro.size = bfd_section_size (sectp);
2261 }
2262 else if (section_is_p (sectp->name, &names.str))
2263 {
2264 this->str.s.section = sectp;
2265 this->str.size = bfd_section_size (sectp);
2266 }
2267 else if (section_is_p (sectp->name, &names.str_offsets))
2268 {
2269 this->str_offsets.s.section = sectp;
2270 this->str_offsets.size = bfd_section_size (sectp);
2271 }
2272 else if (section_is_p (sectp->name, &names.line_str))
2273 {
2274 this->line_str.s.section = sectp;
2275 this->line_str.size = bfd_section_size (sectp);
2276 }
2277 else if (section_is_p (sectp->name, &names.addr))
2278 {
2279 this->addr.s.section = sectp;
2280 this->addr.size = bfd_section_size (sectp);
2281 }
2282 else if (section_is_p (sectp->name, &names.frame))
2283 {
2284 this->frame.s.section = sectp;
2285 this->frame.size = bfd_section_size (sectp);
2286 }
2287 else if (section_is_p (sectp->name, &names.eh_frame))
2288 {
2289 this->eh_frame.s.section = sectp;
2290 this->eh_frame.size = bfd_section_size (sectp);
2291 }
2292 else if (section_is_p (sectp->name, &names.ranges))
2293 {
2294 this->ranges.s.section = sectp;
2295 this->ranges.size = bfd_section_size (sectp);
2296 }
2297 else if (section_is_p (sectp->name, &names.rnglists))
2298 {
2299 this->rnglists.s.section = sectp;
2300 this->rnglists.size = bfd_section_size (sectp);
2301 }
2302 else if (section_is_p (sectp->name, &names.types))
2303 {
2304 struct dwarf2_section_info type_section;
2305
2306 memset (&type_section, 0, sizeof (type_section));
2307 type_section.s.section = sectp;
2308 type_section.size = bfd_section_size (sectp);
2309
2310 this->types.push_back (type_section);
2311 }
2312 else if (section_is_p (sectp->name, &names.gdb_index))
2313 {
2314 this->gdb_index.s.section = sectp;
2315 this->gdb_index.size = bfd_section_size (sectp);
2316 }
2317 else if (section_is_p (sectp->name, &names.debug_names))
2318 {
2319 this->debug_names.s.section = sectp;
2320 this->debug_names.size = bfd_section_size (sectp);
2321 }
2322 else if (section_is_p (sectp->name, &names.debug_aranges))
2323 {
2324 this->debug_aranges.s.section = sectp;
2325 this->debug_aranges.size = bfd_section_size (sectp);
2326 }
2327
2328 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2329 && bfd_section_vma (sectp) == 0)
2330 this->has_section_at_zero = true;
2331 }
2332
2333 /* A helper function that returns the size of a section in a safe way.
2334 If you are positive that the section has been read before using the
2335 size, then it is safe to refer to the dwarf2_section_info object's
2336 "size" field directly. In other cases, you must call this
2337 function, because for compressed sections the size field is not set
2338 correctly until the section has been read. */
2339
2340 static bfd_size_type
2341 dwarf2_section_size (struct objfile *objfile,
2342 struct dwarf2_section_info *info)
2343 {
2344 if (!info->readin)
2345 info->read (objfile);
2346 return info->size;
2347 }
2348
2349 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2350 SECTION_NAME. */
2351
2352 void
2353 dwarf2_get_section_info (struct objfile *objfile,
2354 enum dwarf2_section_enum sect,
2355 asection **sectp, const gdb_byte **bufp,
2356 bfd_size_type *sizep)
2357 {
2358 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2359 struct dwarf2_section_info *info;
2360
2361 /* We may see an objfile without any DWARF, in which case we just
2362 return nothing. */
2363 if (data == NULL)
2364 {
2365 *sectp = NULL;
2366 *bufp = NULL;
2367 *sizep = 0;
2368 return;
2369 }
2370 switch (sect)
2371 {
2372 case DWARF2_DEBUG_FRAME:
2373 info = &data->frame;
2374 break;
2375 case DWARF2_EH_FRAME:
2376 info = &data->eh_frame;
2377 break;
2378 default:
2379 gdb_assert_not_reached ("unexpected section");
2380 }
2381
2382 info->read (objfile);
2383
2384 *sectp = info->get_bfd_section ();
2385 *bufp = info->buffer;
2386 *sizep = info->size;
2387 }
2388
2389 /* A helper function to find the sections for a .dwz file. */
2390
2391 static void
2392 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2393 {
2394 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2395
2396 /* Note that we only support the standard ELF names, because .dwz
2397 is ELF-only (at the time of writing). */
2398 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2399 {
2400 dwz_file->abbrev.s.section = sectp;
2401 dwz_file->abbrev.size = bfd_section_size (sectp);
2402 }
2403 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2404 {
2405 dwz_file->info.s.section = sectp;
2406 dwz_file->info.size = bfd_section_size (sectp);
2407 }
2408 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2409 {
2410 dwz_file->str.s.section = sectp;
2411 dwz_file->str.size = bfd_section_size (sectp);
2412 }
2413 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2414 {
2415 dwz_file->line.s.section = sectp;
2416 dwz_file->line.size = bfd_section_size (sectp);
2417 }
2418 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2419 {
2420 dwz_file->macro.s.section = sectp;
2421 dwz_file->macro.size = bfd_section_size (sectp);
2422 }
2423 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2424 {
2425 dwz_file->gdb_index.s.section = sectp;
2426 dwz_file->gdb_index.size = bfd_section_size (sectp);
2427 }
2428 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2429 {
2430 dwz_file->debug_names.s.section = sectp;
2431 dwz_file->debug_names.size = bfd_section_size (sectp);
2432 }
2433 }
2434
2435 /* See dwarf2read.h. */
2436
2437 struct dwz_file *
2438 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2439 {
2440 const char *filename;
2441 bfd_size_type buildid_len_arg;
2442 size_t buildid_len;
2443 bfd_byte *buildid;
2444
2445 if (dwarf2_per_objfile->dwz_file != NULL)
2446 return dwarf2_per_objfile->dwz_file.get ();
2447
2448 bfd_set_error (bfd_error_no_error);
2449 gdb::unique_xmalloc_ptr<char> data
2450 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2451 &buildid_len_arg, &buildid));
2452 if (data == NULL)
2453 {
2454 if (bfd_get_error () == bfd_error_no_error)
2455 return NULL;
2456 error (_("could not read '.gnu_debugaltlink' section: %s"),
2457 bfd_errmsg (bfd_get_error ()));
2458 }
2459
2460 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2461
2462 buildid_len = (size_t) buildid_len_arg;
2463
2464 filename = data.get ();
2465
2466 std::string abs_storage;
2467 if (!IS_ABSOLUTE_PATH (filename))
2468 {
2469 gdb::unique_xmalloc_ptr<char> abs
2470 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2471
2472 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2473 filename = abs_storage.c_str ();
2474 }
2475
2476 /* First try the file name given in the section. If that doesn't
2477 work, try to use the build-id instead. */
2478 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2479 if (dwz_bfd != NULL)
2480 {
2481 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2482 dwz_bfd.reset (nullptr);
2483 }
2484
2485 if (dwz_bfd == NULL)
2486 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2487
2488 if (dwz_bfd == NULL)
2489 error (_("could not find '.gnu_debugaltlink' file for %s"),
2490 objfile_name (dwarf2_per_objfile->objfile));
2491
2492 std::unique_ptr<struct dwz_file> result
2493 (new struct dwz_file (std::move (dwz_bfd)));
2494
2495 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2496 result.get ());
2497
2498 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2499 result->dwz_bfd.get ());
2500 dwarf2_per_objfile->dwz_file = std::move (result);
2501 return dwarf2_per_objfile->dwz_file.get ();
2502 }
2503 \f
2504 /* DWARF quick_symbols_functions support. */
2505
2506 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2507 unique line tables, so we maintain a separate table of all .debug_line
2508 derived entries to support the sharing.
2509 All the quick functions need is the list of file names. We discard the
2510 line_header when we're done and don't need to record it here. */
2511 struct quick_file_names
2512 {
2513 /* The data used to construct the hash key. */
2514 struct stmt_list_hash hash;
2515
2516 /* The number of entries in file_names, real_names. */
2517 unsigned int num_file_names;
2518
2519 /* The file names from the line table, after being run through
2520 file_full_name. */
2521 const char **file_names;
2522
2523 /* The file names from the line table after being run through
2524 gdb_realpath. These are computed lazily. */
2525 const char **real_names;
2526 };
2527
2528 /* When using the index (and thus not using psymtabs), each CU has an
2529 object of this type. This is used to hold information needed by
2530 the various "quick" methods. */
2531 struct dwarf2_per_cu_quick_data
2532 {
2533 /* The file table. This can be NULL if there was no file table
2534 or it's currently not read in.
2535 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2536 struct quick_file_names *file_names;
2537
2538 /* The corresponding symbol table. This is NULL if symbols for this
2539 CU have not yet been read. */
2540 struct compunit_symtab *compunit_symtab;
2541
2542 /* A temporary mark bit used when iterating over all CUs in
2543 expand_symtabs_matching. */
2544 unsigned int mark : 1;
2545
2546 /* True if we've tried to read the file table and found there isn't one.
2547 There will be no point in trying to read it again next time. */
2548 unsigned int no_file_data : 1;
2549 };
2550
2551 /* Utility hash function for a stmt_list_hash. */
2552
2553 static hashval_t
2554 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2555 {
2556 hashval_t v = 0;
2557
2558 if (stmt_list_hash->dwo_unit != NULL)
2559 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2560 v += to_underlying (stmt_list_hash->line_sect_off);
2561 return v;
2562 }
2563
2564 /* Utility equality function for a stmt_list_hash. */
2565
2566 static int
2567 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2568 const struct stmt_list_hash *rhs)
2569 {
2570 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2571 return 0;
2572 if (lhs->dwo_unit != NULL
2573 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2574 return 0;
2575
2576 return lhs->line_sect_off == rhs->line_sect_off;
2577 }
2578
2579 /* Hash function for a quick_file_names. */
2580
2581 static hashval_t
2582 hash_file_name_entry (const void *e)
2583 {
2584 const struct quick_file_names *file_data
2585 = (const struct quick_file_names *) e;
2586
2587 return hash_stmt_list_entry (&file_data->hash);
2588 }
2589
2590 /* Equality function for a quick_file_names. */
2591
2592 static int
2593 eq_file_name_entry (const void *a, const void *b)
2594 {
2595 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2596 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2597
2598 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2599 }
2600
2601 /* Delete function for a quick_file_names. */
2602
2603 static void
2604 delete_file_name_entry (void *e)
2605 {
2606 struct quick_file_names *file_data = (struct quick_file_names *) e;
2607 int i;
2608
2609 for (i = 0; i < file_data->num_file_names; ++i)
2610 {
2611 xfree ((void*) file_data->file_names[i]);
2612 if (file_data->real_names)
2613 xfree ((void*) file_data->real_names[i]);
2614 }
2615
2616 /* The space for the struct itself lives on objfile_obstack,
2617 so we don't free it here. */
2618 }
2619
2620 /* Create a quick_file_names hash table. */
2621
2622 static htab_t
2623 create_quick_file_names_table (unsigned int nr_initial_entries)
2624 {
2625 return htab_create_alloc (nr_initial_entries,
2626 hash_file_name_entry, eq_file_name_entry,
2627 delete_file_name_entry, xcalloc, xfree);
2628 }
2629
2630 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2631 have to be created afterwards. You should call age_cached_comp_units after
2632 processing PER_CU->CU. dw2_setup must have been already called. */
2633
2634 static void
2635 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2636 {
2637 if (per_cu->is_debug_types)
2638 load_full_type_unit (per_cu);
2639 else
2640 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2641
2642 if (per_cu->cu == NULL)
2643 return; /* Dummy CU. */
2644
2645 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2646 }
2647
2648 /* Read in the symbols for PER_CU. */
2649
2650 static void
2651 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2652 {
2653 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2654
2655 /* Skip type_unit_groups, reading the type units they contain
2656 is handled elsewhere. */
2657 if (IS_TYPE_UNIT_GROUP (per_cu))
2658 return;
2659
2660 /* The destructor of dwarf2_queue_guard frees any entries left on
2661 the queue. After this point we're guaranteed to leave this function
2662 with the dwarf queue empty. */
2663 dwarf2_queue_guard q_guard;
2664
2665 if (dwarf2_per_objfile->using_index
2666 ? per_cu->v.quick->compunit_symtab == NULL
2667 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2668 {
2669 queue_comp_unit (per_cu, language_minimal);
2670 load_cu (per_cu, skip_partial);
2671
2672 /* If we just loaded a CU from a DWO, and we're working with an index
2673 that may badly handle TUs, load all the TUs in that DWO as well.
2674 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2675 if (!per_cu->is_debug_types
2676 && per_cu->cu != NULL
2677 && per_cu->cu->dwo_unit != NULL
2678 && dwarf2_per_objfile->index_table != NULL
2679 && dwarf2_per_objfile->index_table->version <= 7
2680 /* DWP files aren't supported yet. */
2681 && get_dwp_file (dwarf2_per_objfile) == NULL)
2682 queue_and_load_all_dwo_tus (per_cu);
2683 }
2684
2685 process_queue (dwarf2_per_objfile);
2686
2687 /* Age the cache, releasing compilation units that have not
2688 been used recently. */
2689 age_cached_comp_units (dwarf2_per_objfile);
2690 }
2691
2692 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2693 the objfile from which this CU came. Returns the resulting symbol
2694 table. */
2695
2696 static struct compunit_symtab *
2697 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2698 {
2699 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2700
2701 gdb_assert (dwarf2_per_objfile->using_index);
2702 if (!per_cu->v.quick->compunit_symtab)
2703 {
2704 free_cached_comp_units freer (dwarf2_per_objfile);
2705 scoped_restore decrementer = increment_reading_symtab ();
2706 dw2_do_instantiate_symtab (per_cu, skip_partial);
2707 process_cu_includes (dwarf2_per_objfile);
2708 }
2709
2710 return per_cu->v.quick->compunit_symtab;
2711 }
2712
2713 /* See declaration. */
2714
2715 dwarf2_per_cu_data *
2716 dwarf2_per_objfile::get_cutu (int index)
2717 {
2718 if (index >= this->all_comp_units.size ())
2719 {
2720 index -= this->all_comp_units.size ();
2721 gdb_assert (index < this->all_type_units.size ());
2722 return &this->all_type_units[index]->per_cu;
2723 }
2724
2725 return this->all_comp_units[index];
2726 }
2727
2728 /* See declaration. */
2729
2730 dwarf2_per_cu_data *
2731 dwarf2_per_objfile::get_cu (int index)
2732 {
2733 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2734
2735 return this->all_comp_units[index];
2736 }
2737
2738 /* See declaration. */
2739
2740 signatured_type *
2741 dwarf2_per_objfile::get_tu (int index)
2742 {
2743 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2744
2745 return this->all_type_units[index];
2746 }
2747
2748 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2749 objfile_obstack, and constructed with the specified field
2750 values. */
2751
2752 static dwarf2_per_cu_data *
2753 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2754 struct dwarf2_section_info *section,
2755 int is_dwz,
2756 sect_offset sect_off, ULONGEST length)
2757 {
2758 struct objfile *objfile = dwarf2_per_objfile->objfile;
2759 dwarf2_per_cu_data *the_cu
2760 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2761 struct dwarf2_per_cu_data);
2762 the_cu->sect_off = sect_off;
2763 the_cu->length = length;
2764 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2765 the_cu->section = section;
2766 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2767 struct dwarf2_per_cu_quick_data);
2768 the_cu->is_dwz = is_dwz;
2769 return the_cu;
2770 }
2771
2772 /* A helper for create_cus_from_index that handles a given list of
2773 CUs. */
2774
2775 static void
2776 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2777 const gdb_byte *cu_list, offset_type n_elements,
2778 struct dwarf2_section_info *section,
2779 int is_dwz)
2780 {
2781 for (offset_type i = 0; i < n_elements; i += 2)
2782 {
2783 gdb_static_assert (sizeof (ULONGEST) >= 8);
2784
2785 sect_offset sect_off
2786 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2787 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2788 cu_list += 2 * 8;
2789
2790 dwarf2_per_cu_data *per_cu
2791 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2792 sect_off, length);
2793 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2794 }
2795 }
2796
2797 /* Read the CU list from the mapped index, and use it to create all
2798 the CU objects for this objfile. */
2799
2800 static void
2801 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2802 const gdb_byte *cu_list, offset_type cu_list_elements,
2803 const gdb_byte *dwz_list, offset_type dwz_elements)
2804 {
2805 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2806 dwarf2_per_objfile->all_comp_units.reserve
2807 ((cu_list_elements + dwz_elements) / 2);
2808
2809 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2810 &dwarf2_per_objfile->info, 0);
2811
2812 if (dwz_elements == 0)
2813 return;
2814
2815 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2816 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2817 &dwz->info, 1);
2818 }
2819
2820 /* Create the signatured type hash table from the index. */
2821
2822 static void
2823 create_signatured_type_table_from_index
2824 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2825 struct dwarf2_section_info *section,
2826 const gdb_byte *bytes,
2827 offset_type elements)
2828 {
2829 struct objfile *objfile = dwarf2_per_objfile->objfile;
2830
2831 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2832 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2833
2834 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2835
2836 for (offset_type i = 0; i < elements; i += 3)
2837 {
2838 struct signatured_type *sig_type;
2839 ULONGEST signature;
2840 void **slot;
2841 cu_offset type_offset_in_tu;
2842
2843 gdb_static_assert (sizeof (ULONGEST) >= 8);
2844 sect_offset sect_off
2845 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2846 type_offset_in_tu
2847 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2848 BFD_ENDIAN_LITTLE);
2849 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2850 bytes += 3 * 8;
2851
2852 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2853 struct signatured_type);
2854 sig_type->signature = signature;
2855 sig_type->type_offset_in_tu = type_offset_in_tu;
2856 sig_type->per_cu.is_debug_types = 1;
2857 sig_type->per_cu.section = section;
2858 sig_type->per_cu.sect_off = sect_off;
2859 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2860 sig_type->per_cu.v.quick
2861 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2862 struct dwarf2_per_cu_quick_data);
2863
2864 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2865 *slot = sig_type;
2866
2867 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2868 }
2869
2870 dwarf2_per_objfile->signatured_types = sig_types_hash;
2871 }
2872
2873 /* Create the signatured type hash table from .debug_names. */
2874
2875 static void
2876 create_signatured_type_table_from_debug_names
2877 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2878 const mapped_debug_names &map,
2879 struct dwarf2_section_info *section,
2880 struct dwarf2_section_info *abbrev_section)
2881 {
2882 struct objfile *objfile = dwarf2_per_objfile->objfile;
2883
2884 section->read (objfile);
2885 abbrev_section->read (objfile);
2886
2887 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2888 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2889
2890 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
2891
2892 for (uint32_t i = 0; i < map.tu_count; ++i)
2893 {
2894 struct signatured_type *sig_type;
2895 void **slot;
2896
2897 sect_offset sect_off
2898 = (sect_offset) (extract_unsigned_integer
2899 (map.tu_table_reordered + i * map.offset_size,
2900 map.offset_size,
2901 map.dwarf5_byte_order));
2902
2903 comp_unit_head cu_header;
2904 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2905 abbrev_section,
2906 section->buffer + to_underlying (sect_off),
2907 rcuh_kind::TYPE);
2908
2909 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2910 struct signatured_type);
2911 sig_type->signature = cu_header.signature;
2912 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2913 sig_type->per_cu.is_debug_types = 1;
2914 sig_type->per_cu.section = section;
2915 sig_type->per_cu.sect_off = sect_off;
2916 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2917 sig_type->per_cu.v.quick
2918 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2919 struct dwarf2_per_cu_quick_data);
2920
2921 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2922 *slot = sig_type;
2923
2924 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2925 }
2926
2927 dwarf2_per_objfile->signatured_types = sig_types_hash;
2928 }
2929
2930 /* Read the address map data from the mapped index, and use it to
2931 populate the objfile's psymtabs_addrmap. */
2932
2933 static void
2934 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2935 struct mapped_index *index)
2936 {
2937 struct objfile *objfile = dwarf2_per_objfile->objfile;
2938 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2939 const gdb_byte *iter, *end;
2940 struct addrmap *mutable_map;
2941 CORE_ADDR baseaddr;
2942
2943 auto_obstack temp_obstack;
2944
2945 mutable_map = addrmap_create_mutable (&temp_obstack);
2946
2947 iter = index->address_table.data ();
2948 end = iter + index->address_table.size ();
2949
2950 baseaddr = objfile->text_section_offset ();
2951
2952 while (iter < end)
2953 {
2954 ULONGEST hi, lo, cu_index;
2955 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2956 iter += 8;
2957 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2958 iter += 8;
2959 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2960 iter += 4;
2961
2962 if (lo > hi)
2963 {
2964 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2965 hex_string (lo), hex_string (hi));
2966 continue;
2967 }
2968
2969 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2970 {
2971 complaint (_(".gdb_index address table has invalid CU number %u"),
2972 (unsigned) cu_index);
2973 continue;
2974 }
2975
2976 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2977 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2978 addrmap_set_empty (mutable_map, lo, hi - 1,
2979 dwarf2_per_objfile->get_cu (cu_index));
2980 }
2981
2982 objfile->partial_symtabs->psymtabs_addrmap
2983 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2984 }
2985
2986 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2987 populate the objfile's psymtabs_addrmap. */
2988
2989 static void
2990 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2991 struct dwarf2_section_info *section)
2992 {
2993 struct objfile *objfile = dwarf2_per_objfile->objfile;
2994 bfd *abfd = objfile->obfd;
2995 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2996 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2997
2998 auto_obstack temp_obstack;
2999 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3000
3001 std::unordered_map<sect_offset,
3002 dwarf2_per_cu_data *,
3003 gdb::hash_enum<sect_offset>>
3004 debug_info_offset_to_per_cu;
3005 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3006 {
3007 const auto insertpair
3008 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3009 if (!insertpair.second)
3010 {
3011 warning (_("Section .debug_aranges in %s has duplicate "
3012 "debug_info_offset %s, ignoring .debug_aranges."),
3013 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3014 return;
3015 }
3016 }
3017
3018 section->read (objfile);
3019
3020 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3021
3022 const gdb_byte *addr = section->buffer;
3023
3024 while (addr < section->buffer + section->size)
3025 {
3026 const gdb_byte *const entry_addr = addr;
3027 unsigned int bytes_read;
3028
3029 const LONGEST entry_length = read_initial_length (abfd, addr,
3030 &bytes_read);
3031 addr += bytes_read;
3032
3033 const gdb_byte *const entry_end = addr + entry_length;
3034 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3035 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3036 if (addr + entry_length > section->buffer + section->size)
3037 {
3038 warning (_("Section .debug_aranges in %s entry at offset %s "
3039 "length %s exceeds section length %s, "
3040 "ignoring .debug_aranges."),
3041 objfile_name (objfile),
3042 plongest (entry_addr - section->buffer),
3043 plongest (bytes_read + entry_length),
3044 pulongest (section->size));
3045 return;
3046 }
3047
3048 /* The version number. */
3049 const uint16_t version = read_2_bytes (abfd, addr);
3050 addr += 2;
3051 if (version != 2)
3052 {
3053 warning (_("Section .debug_aranges in %s entry at offset %s "
3054 "has unsupported version %d, ignoring .debug_aranges."),
3055 objfile_name (objfile),
3056 plongest (entry_addr - section->buffer), version);
3057 return;
3058 }
3059
3060 const uint64_t debug_info_offset
3061 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3062 addr += offset_size;
3063 const auto per_cu_it
3064 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3065 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3066 {
3067 warning (_("Section .debug_aranges in %s entry at offset %s "
3068 "debug_info_offset %s does not exists, "
3069 "ignoring .debug_aranges."),
3070 objfile_name (objfile),
3071 plongest (entry_addr - section->buffer),
3072 pulongest (debug_info_offset));
3073 return;
3074 }
3075 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3076
3077 const uint8_t address_size = *addr++;
3078 if (address_size < 1 || address_size > 8)
3079 {
3080 warning (_("Section .debug_aranges in %s entry at offset %s "
3081 "address_size %u is invalid, ignoring .debug_aranges."),
3082 objfile_name (objfile),
3083 plongest (entry_addr - section->buffer), address_size);
3084 return;
3085 }
3086
3087 const uint8_t segment_selector_size = *addr++;
3088 if (segment_selector_size != 0)
3089 {
3090 warning (_("Section .debug_aranges in %s entry at offset %s "
3091 "segment_selector_size %u is not supported, "
3092 "ignoring .debug_aranges."),
3093 objfile_name (objfile),
3094 plongest (entry_addr - section->buffer),
3095 segment_selector_size);
3096 return;
3097 }
3098
3099 /* Must pad to an alignment boundary that is twice the address
3100 size. It is undocumented by the DWARF standard but GCC does
3101 use it. */
3102 for (size_t padding = ((-(addr - section->buffer))
3103 & (2 * address_size - 1));
3104 padding > 0; padding--)
3105 if (*addr++ != 0)
3106 {
3107 warning (_("Section .debug_aranges in %s entry at offset %s "
3108 "padding is not zero, ignoring .debug_aranges."),
3109 objfile_name (objfile),
3110 plongest (entry_addr - section->buffer));
3111 return;
3112 }
3113
3114 for (;;)
3115 {
3116 if (addr + 2 * address_size > entry_end)
3117 {
3118 warning (_("Section .debug_aranges in %s entry at offset %s "
3119 "address list is not properly terminated, "
3120 "ignoring .debug_aranges."),
3121 objfile_name (objfile),
3122 plongest (entry_addr - section->buffer));
3123 return;
3124 }
3125 ULONGEST start = extract_unsigned_integer (addr, address_size,
3126 dwarf5_byte_order);
3127 addr += address_size;
3128 ULONGEST length = extract_unsigned_integer (addr, address_size,
3129 dwarf5_byte_order);
3130 addr += address_size;
3131 if (start == 0 && length == 0)
3132 break;
3133 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3134 {
3135 /* Symbol was eliminated due to a COMDAT group. */
3136 continue;
3137 }
3138 ULONGEST end = start + length;
3139 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3140 - baseaddr);
3141 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3142 - baseaddr);
3143 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3144 }
3145 }
3146
3147 objfile->partial_symtabs->psymtabs_addrmap
3148 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3149 }
3150
3151 /* Find a slot in the mapped index INDEX for the object named NAME.
3152 If NAME is found, set *VEC_OUT to point to the CU vector in the
3153 constant pool and return true. If NAME cannot be found, return
3154 false. */
3155
3156 static bool
3157 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3158 offset_type **vec_out)
3159 {
3160 offset_type hash;
3161 offset_type slot, step;
3162 int (*cmp) (const char *, const char *);
3163
3164 gdb::unique_xmalloc_ptr<char> without_params;
3165 if (current_language->la_language == language_cplus
3166 || current_language->la_language == language_fortran
3167 || current_language->la_language == language_d)
3168 {
3169 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3170 not contain any. */
3171
3172 if (strchr (name, '(') != NULL)
3173 {
3174 without_params = cp_remove_params (name);
3175
3176 if (without_params != NULL)
3177 name = without_params.get ();
3178 }
3179 }
3180
3181 /* Index version 4 did not support case insensitive searches. But the
3182 indices for case insensitive languages are built in lowercase, therefore
3183 simulate our NAME being searched is also lowercased. */
3184 hash = mapped_index_string_hash ((index->version == 4
3185 && case_sensitivity == case_sensitive_off
3186 ? 5 : index->version),
3187 name);
3188
3189 slot = hash & (index->symbol_table.size () - 1);
3190 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3191 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3192
3193 for (;;)
3194 {
3195 const char *str;
3196
3197 const auto &bucket = index->symbol_table[slot];
3198 if (bucket.name == 0 && bucket.vec == 0)
3199 return false;
3200
3201 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3202 if (!cmp (name, str))
3203 {
3204 *vec_out = (offset_type *) (index->constant_pool
3205 + MAYBE_SWAP (bucket.vec));
3206 return true;
3207 }
3208
3209 slot = (slot + step) & (index->symbol_table.size () - 1);
3210 }
3211 }
3212
3213 /* A helper function that reads the .gdb_index from BUFFER and fills
3214 in MAP. FILENAME is the name of the file containing the data;
3215 it is used for error reporting. DEPRECATED_OK is true if it is
3216 ok to use deprecated sections.
3217
3218 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3219 out parameters that are filled in with information about the CU and
3220 TU lists in the section.
3221
3222 Returns true if all went well, false otherwise. */
3223
3224 static bool
3225 read_gdb_index_from_buffer (struct objfile *objfile,
3226 const char *filename,
3227 bool deprecated_ok,
3228 gdb::array_view<const gdb_byte> buffer,
3229 struct mapped_index *map,
3230 const gdb_byte **cu_list,
3231 offset_type *cu_list_elements,
3232 const gdb_byte **types_list,
3233 offset_type *types_list_elements)
3234 {
3235 const gdb_byte *addr = &buffer[0];
3236
3237 /* Version check. */
3238 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3239 /* Versions earlier than 3 emitted every copy of a psymbol. This
3240 causes the index to behave very poorly for certain requests. Version 3
3241 contained incomplete addrmap. So, it seems better to just ignore such
3242 indices. */
3243 if (version < 4)
3244 {
3245 static int warning_printed = 0;
3246 if (!warning_printed)
3247 {
3248 warning (_("Skipping obsolete .gdb_index section in %s."),
3249 filename);
3250 warning_printed = 1;
3251 }
3252 return 0;
3253 }
3254 /* Index version 4 uses a different hash function than index version
3255 5 and later.
3256
3257 Versions earlier than 6 did not emit psymbols for inlined
3258 functions. Using these files will cause GDB not to be able to
3259 set breakpoints on inlined functions by name, so we ignore these
3260 indices unless the user has done
3261 "set use-deprecated-index-sections on". */
3262 if (version < 6 && !deprecated_ok)
3263 {
3264 static int warning_printed = 0;
3265 if (!warning_printed)
3266 {
3267 warning (_("\
3268 Skipping deprecated .gdb_index section in %s.\n\
3269 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3270 to use the section anyway."),
3271 filename);
3272 warning_printed = 1;
3273 }
3274 return 0;
3275 }
3276 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3277 of the TU (for symbols coming from TUs),
3278 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3279 Plus gold-generated indices can have duplicate entries for global symbols,
3280 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3281 These are just performance bugs, and we can't distinguish gdb-generated
3282 indices from gold-generated ones, so issue no warning here. */
3283
3284 /* Indexes with higher version than the one supported by GDB may be no
3285 longer backward compatible. */
3286 if (version > 8)
3287 return 0;
3288
3289 map->version = version;
3290
3291 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3292
3293 int i = 0;
3294 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3295 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3296 / 8);
3297 ++i;
3298
3299 *types_list = addr + MAYBE_SWAP (metadata[i]);
3300 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3301 - MAYBE_SWAP (metadata[i]))
3302 / 8);
3303 ++i;
3304
3305 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3306 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3307 map->address_table
3308 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3309 ++i;
3310
3311 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3312 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3313 map->symbol_table
3314 = gdb::array_view<mapped_index::symbol_table_slot>
3315 ((mapped_index::symbol_table_slot *) symbol_table,
3316 (mapped_index::symbol_table_slot *) symbol_table_end);
3317
3318 ++i;
3319 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3320
3321 return 1;
3322 }
3323
3324 /* Callback types for dwarf2_read_gdb_index. */
3325
3326 typedef gdb::function_view
3327 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3328 get_gdb_index_contents_ftype;
3329 typedef gdb::function_view
3330 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3331 get_gdb_index_contents_dwz_ftype;
3332
3333 /* Read .gdb_index. If everything went ok, initialize the "quick"
3334 elements of all the CUs and return 1. Otherwise, return 0. */
3335
3336 static int
3337 dwarf2_read_gdb_index
3338 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3339 get_gdb_index_contents_ftype get_gdb_index_contents,
3340 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3341 {
3342 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3343 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3344 struct dwz_file *dwz;
3345 struct objfile *objfile = dwarf2_per_objfile->objfile;
3346
3347 gdb::array_view<const gdb_byte> main_index_contents
3348 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3349
3350 if (main_index_contents.empty ())
3351 return 0;
3352
3353 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3354 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3355 use_deprecated_index_sections,
3356 main_index_contents, map.get (), &cu_list,
3357 &cu_list_elements, &types_list,
3358 &types_list_elements))
3359 return 0;
3360
3361 /* Don't use the index if it's empty. */
3362 if (map->symbol_table.empty ())
3363 return 0;
3364
3365 /* If there is a .dwz file, read it so we can get its CU list as
3366 well. */
3367 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3368 if (dwz != NULL)
3369 {
3370 struct mapped_index dwz_map;
3371 const gdb_byte *dwz_types_ignore;
3372 offset_type dwz_types_elements_ignore;
3373
3374 gdb::array_view<const gdb_byte> dwz_index_content
3375 = get_gdb_index_contents_dwz (objfile, dwz);
3376
3377 if (dwz_index_content.empty ())
3378 return 0;
3379
3380 if (!read_gdb_index_from_buffer (objfile,
3381 bfd_get_filename (dwz->dwz_bfd.get ()),
3382 1, dwz_index_content, &dwz_map,
3383 &dwz_list, &dwz_list_elements,
3384 &dwz_types_ignore,
3385 &dwz_types_elements_ignore))
3386 {
3387 warning (_("could not read '.gdb_index' section from %s; skipping"),
3388 bfd_get_filename (dwz->dwz_bfd.get ()));
3389 return 0;
3390 }
3391 }
3392
3393 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3394 dwz_list, dwz_list_elements);
3395
3396 if (types_list_elements)
3397 {
3398 /* We can only handle a single .debug_types when we have an
3399 index. */
3400 if (dwarf2_per_objfile->types.size () != 1)
3401 return 0;
3402
3403 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3404
3405 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3406 types_list, types_list_elements);
3407 }
3408
3409 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3410
3411 dwarf2_per_objfile->index_table = std::move (map);
3412 dwarf2_per_objfile->using_index = 1;
3413 dwarf2_per_objfile->quick_file_names_table =
3414 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3415
3416 return 1;
3417 }
3418
3419 /* die_reader_func for dw2_get_file_names. */
3420
3421 static void
3422 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3423 const gdb_byte *info_ptr,
3424 struct die_info *comp_unit_die,
3425 int has_children)
3426 {
3427 struct dwarf2_cu *cu = reader->cu;
3428 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3429 struct dwarf2_per_objfile *dwarf2_per_objfile
3430 = cu->per_cu->dwarf2_per_objfile;
3431 struct objfile *objfile = dwarf2_per_objfile->objfile;
3432 struct dwarf2_per_cu_data *lh_cu;
3433 struct attribute *attr;
3434 void **slot;
3435 struct quick_file_names *qfn;
3436
3437 gdb_assert (! this_cu->is_debug_types);
3438
3439 /* Our callers never want to match partial units -- instead they
3440 will match the enclosing full CU. */
3441 if (comp_unit_die->tag == DW_TAG_partial_unit)
3442 {
3443 this_cu->v.quick->no_file_data = 1;
3444 return;
3445 }
3446
3447 lh_cu = this_cu;
3448 slot = NULL;
3449
3450 line_header_up lh;
3451 sect_offset line_offset {};
3452
3453 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3454 if (attr != nullptr)
3455 {
3456 struct quick_file_names find_entry;
3457
3458 line_offset = (sect_offset) DW_UNSND (attr);
3459
3460 /* We may have already read in this line header (TU line header sharing).
3461 If we have we're done. */
3462 find_entry.hash.dwo_unit = cu->dwo_unit;
3463 find_entry.hash.line_sect_off = line_offset;
3464 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3465 &find_entry, INSERT);
3466 if (*slot != NULL)
3467 {
3468 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3469 return;
3470 }
3471
3472 lh = dwarf_decode_line_header (line_offset, cu);
3473 }
3474 if (lh == NULL)
3475 {
3476 lh_cu->v.quick->no_file_data = 1;
3477 return;
3478 }
3479
3480 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3481 qfn->hash.dwo_unit = cu->dwo_unit;
3482 qfn->hash.line_sect_off = line_offset;
3483 gdb_assert (slot != NULL);
3484 *slot = qfn;
3485
3486 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3487
3488 int offset = 0;
3489 if (strcmp (fnd.name, "<unknown>") != 0)
3490 ++offset;
3491
3492 qfn->num_file_names = offset + lh->file_names_size ();
3493 qfn->file_names =
3494 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3495 if (offset != 0)
3496 qfn->file_names[0] = xstrdup (fnd.name);
3497 for (int i = 0; i < lh->file_names_size (); ++i)
3498 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3499 qfn->real_names = NULL;
3500
3501 lh_cu->v.quick->file_names = qfn;
3502 }
3503
3504 /* A helper for the "quick" functions which attempts to read the line
3505 table for THIS_CU. */
3506
3507 static struct quick_file_names *
3508 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3509 {
3510 /* This should never be called for TUs. */
3511 gdb_assert (! this_cu->is_debug_types);
3512 /* Nor type unit groups. */
3513 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3514
3515 if (this_cu->v.quick->file_names != NULL)
3516 return this_cu->v.quick->file_names;
3517 /* If we know there is no line data, no point in looking again. */
3518 if (this_cu->v.quick->no_file_data)
3519 return NULL;
3520
3521 cutu_reader reader (this_cu);
3522 if (!reader.dummy_p)
3523 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3524 reader.has_children);
3525
3526 if (this_cu->v.quick->no_file_data)
3527 return NULL;
3528 return this_cu->v.quick->file_names;
3529 }
3530
3531 /* A helper for the "quick" functions which computes and caches the
3532 real path for a given file name from the line table. */
3533
3534 static const char *
3535 dw2_get_real_path (struct objfile *objfile,
3536 struct quick_file_names *qfn, int index)
3537 {
3538 if (qfn->real_names == NULL)
3539 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3540 qfn->num_file_names, const char *);
3541
3542 if (qfn->real_names[index] == NULL)
3543 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3544
3545 return qfn->real_names[index];
3546 }
3547
3548 static struct symtab *
3549 dw2_find_last_source_symtab (struct objfile *objfile)
3550 {
3551 struct dwarf2_per_objfile *dwarf2_per_objfile
3552 = get_dwarf2_per_objfile (objfile);
3553 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3554 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3555
3556 if (cust == NULL)
3557 return NULL;
3558
3559 return compunit_primary_filetab (cust);
3560 }
3561
3562 /* Traversal function for dw2_forget_cached_source_info. */
3563
3564 static int
3565 dw2_free_cached_file_names (void **slot, void *info)
3566 {
3567 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3568
3569 if (file_data->real_names)
3570 {
3571 int i;
3572
3573 for (i = 0; i < file_data->num_file_names; ++i)
3574 {
3575 xfree ((void*) file_data->real_names[i]);
3576 file_data->real_names[i] = NULL;
3577 }
3578 }
3579
3580 return 1;
3581 }
3582
3583 static void
3584 dw2_forget_cached_source_info (struct objfile *objfile)
3585 {
3586 struct dwarf2_per_objfile *dwarf2_per_objfile
3587 = get_dwarf2_per_objfile (objfile);
3588
3589 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3590 dw2_free_cached_file_names, NULL);
3591 }
3592
3593 /* Helper function for dw2_map_symtabs_matching_filename that expands
3594 the symtabs and calls the iterator. */
3595
3596 static int
3597 dw2_map_expand_apply (struct objfile *objfile,
3598 struct dwarf2_per_cu_data *per_cu,
3599 const char *name, const char *real_path,
3600 gdb::function_view<bool (symtab *)> callback)
3601 {
3602 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3603
3604 /* Don't visit already-expanded CUs. */
3605 if (per_cu->v.quick->compunit_symtab)
3606 return 0;
3607
3608 /* This may expand more than one symtab, and we want to iterate over
3609 all of them. */
3610 dw2_instantiate_symtab (per_cu, false);
3611
3612 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3613 last_made, callback);
3614 }
3615
3616 /* Implementation of the map_symtabs_matching_filename method. */
3617
3618 static bool
3619 dw2_map_symtabs_matching_filename
3620 (struct objfile *objfile, const char *name, const char *real_path,
3621 gdb::function_view<bool (symtab *)> callback)
3622 {
3623 const char *name_basename = lbasename (name);
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
3627 /* The rule is CUs specify all the files, including those used by
3628 any TU, so there's no need to scan TUs here. */
3629
3630 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3631 {
3632 /* We only need to look at symtabs not already expanded. */
3633 if (per_cu->v.quick->compunit_symtab)
3634 continue;
3635
3636 quick_file_names *file_data = dw2_get_file_names (per_cu);
3637 if (file_data == NULL)
3638 continue;
3639
3640 for (int j = 0; j < file_data->num_file_names; ++j)
3641 {
3642 const char *this_name = file_data->file_names[j];
3643 const char *this_real_name;
3644
3645 if (compare_filenames_for_search (this_name, name))
3646 {
3647 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3648 callback))
3649 return true;
3650 continue;
3651 }
3652
3653 /* Before we invoke realpath, which can get expensive when many
3654 files are involved, do a quick comparison of the basenames. */
3655 if (! basenames_may_differ
3656 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3657 continue;
3658
3659 this_real_name = dw2_get_real_path (objfile, file_data, j);
3660 if (compare_filenames_for_search (this_real_name, name))
3661 {
3662 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3663 callback))
3664 return true;
3665 continue;
3666 }
3667
3668 if (real_path != NULL)
3669 {
3670 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3671 gdb_assert (IS_ABSOLUTE_PATH (name));
3672 if (this_real_name != NULL
3673 && FILENAME_CMP (real_path, this_real_name) == 0)
3674 {
3675 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3676 callback))
3677 return true;
3678 continue;
3679 }
3680 }
3681 }
3682 }
3683
3684 return false;
3685 }
3686
3687 /* Struct used to manage iterating over all CUs looking for a symbol. */
3688
3689 struct dw2_symtab_iterator
3690 {
3691 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3692 struct dwarf2_per_objfile *dwarf2_per_objfile;
3693 /* If set, only look for symbols that match that block. Valid values are
3694 GLOBAL_BLOCK and STATIC_BLOCK. */
3695 gdb::optional<block_enum> block_index;
3696 /* The kind of symbol we're looking for. */
3697 domain_enum domain;
3698 /* The list of CUs from the index entry of the symbol,
3699 or NULL if not found. */
3700 offset_type *vec;
3701 /* The next element in VEC to look at. */
3702 int next;
3703 /* The number of elements in VEC, or zero if there is no match. */
3704 int length;
3705 /* Have we seen a global version of the symbol?
3706 If so we can ignore all further global instances.
3707 This is to work around gold/15646, inefficient gold-generated
3708 indices. */
3709 int global_seen;
3710 };
3711
3712 /* Initialize the index symtab iterator ITER. */
3713
3714 static void
3715 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3716 struct dwarf2_per_objfile *dwarf2_per_objfile,
3717 gdb::optional<block_enum> block_index,
3718 domain_enum domain,
3719 const char *name)
3720 {
3721 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3722 iter->block_index = block_index;
3723 iter->domain = domain;
3724 iter->next = 0;
3725 iter->global_seen = 0;
3726
3727 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3728
3729 /* index is NULL if OBJF_READNOW. */
3730 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3731 iter->length = MAYBE_SWAP (*iter->vec);
3732 else
3733 {
3734 iter->vec = NULL;
3735 iter->length = 0;
3736 }
3737 }
3738
3739 /* Return the next matching CU or NULL if there are no more. */
3740
3741 static struct dwarf2_per_cu_data *
3742 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3743 {
3744 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3745
3746 for ( ; iter->next < iter->length; ++iter->next)
3747 {
3748 offset_type cu_index_and_attrs =
3749 MAYBE_SWAP (iter->vec[iter->next + 1]);
3750 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3751 gdb_index_symbol_kind symbol_kind =
3752 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3753 /* Only check the symbol attributes if they're present.
3754 Indices prior to version 7 don't record them,
3755 and indices >= 7 may elide them for certain symbols
3756 (gold does this). */
3757 int attrs_valid =
3758 (dwarf2_per_objfile->index_table->version >= 7
3759 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3760
3761 /* Don't crash on bad data. */
3762 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3763 + dwarf2_per_objfile->all_type_units.size ()))
3764 {
3765 complaint (_(".gdb_index entry has bad CU index"
3766 " [in module %s]"),
3767 objfile_name (dwarf2_per_objfile->objfile));
3768 continue;
3769 }
3770
3771 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3772
3773 /* Skip if already read in. */
3774 if (per_cu->v.quick->compunit_symtab)
3775 continue;
3776
3777 /* Check static vs global. */
3778 if (attrs_valid)
3779 {
3780 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3781
3782 if (iter->block_index.has_value ())
3783 {
3784 bool want_static = *iter->block_index == STATIC_BLOCK;
3785
3786 if (is_static != want_static)
3787 continue;
3788 }
3789
3790 /* Work around gold/15646. */
3791 if (!is_static && iter->global_seen)
3792 continue;
3793 if (!is_static)
3794 iter->global_seen = 1;
3795 }
3796
3797 /* Only check the symbol's kind if it has one. */
3798 if (attrs_valid)
3799 {
3800 switch (iter->domain)
3801 {
3802 case VAR_DOMAIN:
3803 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3804 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3805 /* Some types are also in VAR_DOMAIN. */
3806 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3807 continue;
3808 break;
3809 case STRUCT_DOMAIN:
3810 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3811 continue;
3812 break;
3813 case LABEL_DOMAIN:
3814 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3815 continue;
3816 break;
3817 case MODULE_DOMAIN:
3818 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3819 continue;
3820 break;
3821 default:
3822 break;
3823 }
3824 }
3825
3826 ++iter->next;
3827 return per_cu;
3828 }
3829
3830 return NULL;
3831 }
3832
3833 static struct compunit_symtab *
3834 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3835 const char *name, domain_enum domain)
3836 {
3837 struct compunit_symtab *stab_best = NULL;
3838 struct dwarf2_per_objfile *dwarf2_per_objfile
3839 = get_dwarf2_per_objfile (objfile);
3840
3841 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3842
3843 struct dw2_symtab_iterator iter;
3844 struct dwarf2_per_cu_data *per_cu;
3845
3846 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3847
3848 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3849 {
3850 struct symbol *sym, *with_opaque = NULL;
3851 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3852 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3853 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3854
3855 sym = block_find_symbol (block, name, domain,
3856 block_find_non_opaque_type_preferred,
3857 &with_opaque);
3858
3859 /* Some caution must be observed with overloaded functions
3860 and methods, since the index will not contain any overload
3861 information (but NAME might contain it). */
3862
3863 if (sym != NULL
3864 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3865 return stab;
3866 if (with_opaque != NULL
3867 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3868 stab_best = stab;
3869
3870 /* Keep looking through other CUs. */
3871 }
3872
3873 return stab_best;
3874 }
3875
3876 static void
3877 dw2_print_stats (struct objfile *objfile)
3878 {
3879 struct dwarf2_per_objfile *dwarf2_per_objfile
3880 = get_dwarf2_per_objfile (objfile);
3881 int total = (dwarf2_per_objfile->all_comp_units.size ()
3882 + dwarf2_per_objfile->all_type_units.size ());
3883 int count = 0;
3884
3885 for (int i = 0; i < total; ++i)
3886 {
3887 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3888
3889 if (!per_cu->v.quick->compunit_symtab)
3890 ++count;
3891 }
3892 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3893 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3894 }
3895
3896 /* This dumps minimal information about the index.
3897 It is called via "mt print objfiles".
3898 One use is to verify .gdb_index has been loaded by the
3899 gdb.dwarf2/gdb-index.exp testcase. */
3900
3901 static void
3902 dw2_dump (struct objfile *objfile)
3903 {
3904 struct dwarf2_per_objfile *dwarf2_per_objfile
3905 = get_dwarf2_per_objfile (objfile);
3906
3907 gdb_assert (dwarf2_per_objfile->using_index);
3908 printf_filtered (".gdb_index:");
3909 if (dwarf2_per_objfile->index_table != NULL)
3910 {
3911 printf_filtered (" version %d\n",
3912 dwarf2_per_objfile->index_table->version);
3913 }
3914 else
3915 printf_filtered (" faked for \"readnow\"\n");
3916 printf_filtered ("\n");
3917 }
3918
3919 static void
3920 dw2_expand_symtabs_for_function (struct objfile *objfile,
3921 const char *func_name)
3922 {
3923 struct dwarf2_per_objfile *dwarf2_per_objfile
3924 = get_dwarf2_per_objfile (objfile);
3925
3926 struct dw2_symtab_iterator iter;
3927 struct dwarf2_per_cu_data *per_cu;
3928
3929 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3930
3931 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3932 dw2_instantiate_symtab (per_cu, false);
3933
3934 }
3935
3936 static void
3937 dw2_expand_all_symtabs (struct objfile *objfile)
3938 {
3939 struct dwarf2_per_objfile *dwarf2_per_objfile
3940 = get_dwarf2_per_objfile (objfile);
3941 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3942 + dwarf2_per_objfile->all_type_units.size ());
3943
3944 for (int i = 0; i < total_units; ++i)
3945 {
3946 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3947
3948 /* We don't want to directly expand a partial CU, because if we
3949 read it with the wrong language, then assertion failures can
3950 be triggered later on. See PR symtab/23010. So, tell
3951 dw2_instantiate_symtab to skip partial CUs -- any important
3952 partial CU will be read via DW_TAG_imported_unit anyway. */
3953 dw2_instantiate_symtab (per_cu, true);
3954 }
3955 }
3956
3957 static void
3958 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3959 const char *fullname)
3960 {
3961 struct dwarf2_per_objfile *dwarf2_per_objfile
3962 = get_dwarf2_per_objfile (objfile);
3963
3964 /* We don't need to consider type units here.
3965 This is only called for examining code, e.g. expand_line_sal.
3966 There can be an order of magnitude (or more) more type units
3967 than comp units, and we avoid them if we can. */
3968
3969 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3970 {
3971 /* We only need to look at symtabs not already expanded. */
3972 if (per_cu->v.quick->compunit_symtab)
3973 continue;
3974
3975 quick_file_names *file_data = dw2_get_file_names (per_cu);
3976 if (file_data == NULL)
3977 continue;
3978
3979 for (int j = 0; j < file_data->num_file_names; ++j)
3980 {
3981 const char *this_fullname = file_data->file_names[j];
3982
3983 if (filename_cmp (this_fullname, fullname) == 0)
3984 {
3985 dw2_instantiate_symtab (per_cu, false);
3986 break;
3987 }
3988 }
3989 }
3990 }
3991
3992 static void
3993 dw2_map_matching_symbols
3994 (struct objfile *objfile,
3995 const lookup_name_info &name, domain_enum domain,
3996 int global,
3997 gdb::function_view<symbol_found_callback_ftype> callback,
3998 symbol_compare_ftype *ordered_compare)
3999 {
4000 /* Currently unimplemented; used for Ada. The function can be called if the
4001 current language is Ada for a non-Ada objfile using GNU index. As Ada
4002 does not look for non-Ada symbols this function should just return. */
4003 }
4004
4005 /* Starting from a search name, return the string that finds the upper
4006 bound of all strings that start with SEARCH_NAME in a sorted name
4007 list. Returns the empty string to indicate that the upper bound is
4008 the end of the list. */
4009
4010 static std::string
4011 make_sort_after_prefix_name (const char *search_name)
4012 {
4013 /* When looking to complete "func", we find the upper bound of all
4014 symbols that start with "func" by looking for where we'd insert
4015 the closest string that would follow "func" in lexicographical
4016 order. Usually, that's "func"-with-last-character-incremented,
4017 i.e. "fund". Mind non-ASCII characters, though. Usually those
4018 will be UTF-8 multi-byte sequences, but we can't be certain.
4019 Especially mind the 0xff character, which is a valid character in
4020 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4021 rule out compilers allowing it in identifiers. Note that
4022 conveniently, strcmp/strcasecmp are specified to compare
4023 characters interpreted as unsigned char. So what we do is treat
4024 the whole string as a base 256 number composed of a sequence of
4025 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4026 to 0, and carries 1 to the following more-significant position.
4027 If the very first character in SEARCH_NAME ends up incremented
4028 and carries/overflows, then the upper bound is the end of the
4029 list. The string after the empty string is also the empty
4030 string.
4031
4032 Some examples of this operation:
4033
4034 SEARCH_NAME => "+1" RESULT
4035
4036 "abc" => "abd"
4037 "ab\xff" => "ac"
4038 "\xff" "a" "\xff" => "\xff" "b"
4039 "\xff" => ""
4040 "\xff\xff" => ""
4041 "" => ""
4042
4043 Then, with these symbols for example:
4044
4045 func
4046 func1
4047 fund
4048
4049 completing "func" looks for symbols between "func" and
4050 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4051 which finds "func" and "func1", but not "fund".
4052
4053 And with:
4054
4055 funcÿ (Latin1 'ÿ' [0xff])
4056 funcÿ1
4057 fund
4058
4059 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4060 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4061
4062 And with:
4063
4064 ÿÿ (Latin1 'ÿ' [0xff])
4065 ÿÿ1
4066
4067 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4068 the end of the list.
4069 */
4070 std::string after = search_name;
4071 while (!after.empty () && (unsigned char) after.back () == 0xff)
4072 after.pop_back ();
4073 if (!after.empty ())
4074 after.back () = (unsigned char) after.back () + 1;
4075 return after;
4076 }
4077
4078 /* See declaration. */
4079
4080 std::pair<std::vector<name_component>::const_iterator,
4081 std::vector<name_component>::const_iterator>
4082 mapped_index_base::find_name_components_bounds
4083 (const lookup_name_info &lookup_name_without_params, language lang) const
4084 {
4085 auto *name_cmp
4086 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4087
4088 const char *lang_name
4089 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4090
4091 /* Comparison function object for lower_bound that matches against a
4092 given symbol name. */
4093 auto lookup_compare_lower = [&] (const name_component &elem,
4094 const char *name)
4095 {
4096 const char *elem_qualified = this->symbol_name_at (elem.idx);
4097 const char *elem_name = elem_qualified + elem.name_offset;
4098 return name_cmp (elem_name, name) < 0;
4099 };
4100
4101 /* Comparison function object for upper_bound that matches against a
4102 given symbol name. */
4103 auto lookup_compare_upper = [&] (const char *name,
4104 const name_component &elem)
4105 {
4106 const char *elem_qualified = this->symbol_name_at (elem.idx);
4107 const char *elem_name = elem_qualified + elem.name_offset;
4108 return name_cmp (name, elem_name) < 0;
4109 };
4110
4111 auto begin = this->name_components.begin ();
4112 auto end = this->name_components.end ();
4113
4114 /* Find the lower bound. */
4115 auto lower = [&] ()
4116 {
4117 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4118 return begin;
4119 else
4120 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4121 } ();
4122
4123 /* Find the upper bound. */
4124 auto upper = [&] ()
4125 {
4126 if (lookup_name_without_params.completion_mode ())
4127 {
4128 /* In completion mode, we want UPPER to point past all
4129 symbols names that have the same prefix. I.e., with
4130 these symbols, and completing "func":
4131
4132 function << lower bound
4133 function1
4134 other_function << upper bound
4135
4136 We find the upper bound by looking for the insertion
4137 point of "func"-with-last-character-incremented,
4138 i.e. "fund". */
4139 std::string after = make_sort_after_prefix_name (lang_name);
4140 if (after.empty ())
4141 return end;
4142 return std::lower_bound (lower, end, after.c_str (),
4143 lookup_compare_lower);
4144 }
4145 else
4146 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4147 } ();
4148
4149 return {lower, upper};
4150 }
4151
4152 /* See declaration. */
4153
4154 void
4155 mapped_index_base::build_name_components ()
4156 {
4157 if (!this->name_components.empty ())
4158 return;
4159
4160 this->name_components_casing = case_sensitivity;
4161 auto *name_cmp
4162 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4163
4164 /* The code below only knows how to break apart components of C++
4165 symbol names (and other languages that use '::' as
4166 namespace/module separator) and Ada symbol names. */
4167 auto count = this->symbol_name_count ();
4168 for (offset_type idx = 0; idx < count; idx++)
4169 {
4170 if (this->symbol_name_slot_invalid (idx))
4171 continue;
4172
4173 const char *name = this->symbol_name_at (idx);
4174
4175 /* Add each name component to the name component table. */
4176 unsigned int previous_len = 0;
4177
4178 if (strstr (name, "::") != nullptr)
4179 {
4180 for (unsigned int current_len = cp_find_first_component (name);
4181 name[current_len] != '\0';
4182 current_len += cp_find_first_component (name + current_len))
4183 {
4184 gdb_assert (name[current_len] == ':');
4185 this->name_components.push_back ({previous_len, idx});
4186 /* Skip the '::'. */
4187 current_len += 2;
4188 previous_len = current_len;
4189 }
4190 }
4191 else
4192 {
4193 /* Handle the Ada encoded (aka mangled) form here. */
4194 for (const char *iter = strstr (name, "__");
4195 iter != nullptr;
4196 iter = strstr (iter, "__"))
4197 {
4198 this->name_components.push_back ({previous_len, idx});
4199 iter += 2;
4200 previous_len = iter - name;
4201 }
4202 }
4203
4204 this->name_components.push_back ({previous_len, idx});
4205 }
4206
4207 /* Sort name_components elements by name. */
4208 auto name_comp_compare = [&] (const name_component &left,
4209 const name_component &right)
4210 {
4211 const char *left_qualified = this->symbol_name_at (left.idx);
4212 const char *right_qualified = this->symbol_name_at (right.idx);
4213
4214 const char *left_name = left_qualified + left.name_offset;
4215 const char *right_name = right_qualified + right.name_offset;
4216
4217 return name_cmp (left_name, right_name) < 0;
4218 };
4219
4220 std::sort (this->name_components.begin (),
4221 this->name_components.end (),
4222 name_comp_compare);
4223 }
4224
4225 /* Helper for dw2_expand_symtabs_matching that works with a
4226 mapped_index_base instead of the containing objfile. This is split
4227 to a separate function in order to be able to unit test the
4228 name_components matching using a mock mapped_index_base. For each
4229 symbol name that matches, calls MATCH_CALLBACK, passing it the
4230 symbol's index in the mapped_index_base symbol table. */
4231
4232 static void
4233 dw2_expand_symtabs_matching_symbol
4234 (mapped_index_base &index,
4235 const lookup_name_info &lookup_name_in,
4236 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4237 enum search_domain kind,
4238 gdb::function_view<bool (offset_type)> match_callback)
4239 {
4240 lookup_name_info lookup_name_without_params
4241 = lookup_name_in.make_ignore_params ();
4242
4243 /* Build the symbol name component sorted vector, if we haven't
4244 yet. */
4245 index.build_name_components ();
4246
4247 /* The same symbol may appear more than once in the range though.
4248 E.g., if we're looking for symbols that complete "w", and we have
4249 a symbol named "w1::w2", we'll find the two name components for
4250 that same symbol in the range. To be sure we only call the
4251 callback once per symbol, we first collect the symbol name
4252 indexes that matched in a temporary vector and ignore
4253 duplicates. */
4254 std::vector<offset_type> matches;
4255
4256 struct name_and_matcher
4257 {
4258 symbol_name_matcher_ftype *matcher;
4259 const std::string &name;
4260
4261 bool operator== (const name_and_matcher &other) const
4262 {
4263 return matcher == other.matcher && name == other.name;
4264 }
4265 };
4266
4267 /* A vector holding all the different symbol name matchers, for all
4268 languages. */
4269 std::vector<name_and_matcher> matchers;
4270
4271 for (int i = 0; i < nr_languages; i++)
4272 {
4273 enum language lang_e = (enum language) i;
4274
4275 const language_defn *lang = language_def (lang_e);
4276 symbol_name_matcher_ftype *name_matcher
4277 = get_symbol_name_matcher (lang, lookup_name_without_params);
4278
4279 name_and_matcher key {
4280 name_matcher,
4281 lookup_name_without_params.language_lookup_name (lang_e)
4282 };
4283
4284 /* Don't insert the same comparison routine more than once.
4285 Note that we do this linear walk. This is not a problem in
4286 practice because the number of supported languages is
4287 low. */
4288 if (std::find (matchers.begin (), matchers.end (), key)
4289 != matchers.end ())
4290 continue;
4291 matchers.push_back (std::move (key));
4292
4293 auto bounds
4294 = index.find_name_components_bounds (lookup_name_without_params,
4295 lang_e);
4296
4297 /* Now for each symbol name in range, check to see if we have a name
4298 match, and if so, call the MATCH_CALLBACK callback. */
4299
4300 for (; bounds.first != bounds.second; ++bounds.first)
4301 {
4302 const char *qualified = index.symbol_name_at (bounds.first->idx);
4303
4304 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4305 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4306 continue;
4307
4308 matches.push_back (bounds.first->idx);
4309 }
4310 }
4311
4312 std::sort (matches.begin (), matches.end ());
4313
4314 /* Finally call the callback, once per match. */
4315 ULONGEST prev = -1;
4316 for (offset_type idx : matches)
4317 {
4318 if (prev != idx)
4319 {
4320 if (!match_callback (idx))
4321 break;
4322 prev = idx;
4323 }
4324 }
4325
4326 /* Above we use a type wider than idx's for 'prev', since 0 and
4327 (offset_type)-1 are both possible values. */
4328 static_assert (sizeof (prev) > sizeof (offset_type), "");
4329 }
4330
4331 #if GDB_SELF_TEST
4332
4333 namespace selftests { namespace dw2_expand_symtabs_matching {
4334
4335 /* A mock .gdb_index/.debug_names-like name index table, enough to
4336 exercise dw2_expand_symtabs_matching_symbol, which works with the
4337 mapped_index_base interface. Builds an index from the symbol list
4338 passed as parameter to the constructor. */
4339 class mock_mapped_index : public mapped_index_base
4340 {
4341 public:
4342 mock_mapped_index (gdb::array_view<const char *> symbols)
4343 : m_symbol_table (symbols)
4344 {}
4345
4346 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4347
4348 /* Return the number of names in the symbol table. */
4349 size_t symbol_name_count () const override
4350 {
4351 return m_symbol_table.size ();
4352 }
4353
4354 /* Get the name of the symbol at IDX in the symbol table. */
4355 const char *symbol_name_at (offset_type idx) const override
4356 {
4357 return m_symbol_table[idx];
4358 }
4359
4360 private:
4361 gdb::array_view<const char *> m_symbol_table;
4362 };
4363
4364 /* Convenience function that converts a NULL pointer to a "<null>"
4365 string, to pass to print routines. */
4366
4367 static const char *
4368 string_or_null (const char *str)
4369 {
4370 return str != NULL ? str : "<null>";
4371 }
4372
4373 /* Check if a lookup_name_info built from
4374 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4375 index. EXPECTED_LIST is the list of expected matches, in expected
4376 matching order. If no match expected, then an empty list is
4377 specified. Returns true on success. On failure prints a warning
4378 indicating the file:line that failed, and returns false. */
4379
4380 static bool
4381 check_match (const char *file, int line,
4382 mock_mapped_index &mock_index,
4383 const char *name, symbol_name_match_type match_type,
4384 bool completion_mode,
4385 std::initializer_list<const char *> expected_list)
4386 {
4387 lookup_name_info lookup_name (name, match_type, completion_mode);
4388
4389 bool matched = true;
4390
4391 auto mismatch = [&] (const char *expected_str,
4392 const char *got)
4393 {
4394 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4395 "expected=\"%s\", got=\"%s\"\n"),
4396 file, line,
4397 (match_type == symbol_name_match_type::FULL
4398 ? "FULL" : "WILD"),
4399 name, string_or_null (expected_str), string_or_null (got));
4400 matched = false;
4401 };
4402
4403 auto expected_it = expected_list.begin ();
4404 auto expected_end = expected_list.end ();
4405
4406 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4407 NULL, ALL_DOMAIN,
4408 [&] (offset_type idx)
4409 {
4410 const char *matched_name = mock_index.symbol_name_at (idx);
4411 const char *expected_str
4412 = expected_it == expected_end ? NULL : *expected_it++;
4413
4414 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4415 mismatch (expected_str, matched_name);
4416 return true;
4417 });
4418
4419 const char *expected_str
4420 = expected_it == expected_end ? NULL : *expected_it++;
4421 if (expected_str != NULL)
4422 mismatch (expected_str, NULL);
4423
4424 return matched;
4425 }
4426
4427 /* The symbols added to the mock mapped_index for testing (in
4428 canonical form). */
4429 static const char *test_symbols[] = {
4430 "function",
4431 "std::bar",
4432 "std::zfunction",
4433 "std::zfunction2",
4434 "w1::w2",
4435 "ns::foo<char*>",
4436 "ns::foo<int>",
4437 "ns::foo<long>",
4438 "ns2::tmpl<int>::foo2",
4439 "(anonymous namespace)::A::B::C",
4440
4441 /* These are used to check that the increment-last-char in the
4442 matching algorithm for completion doesn't match "t1_fund" when
4443 completing "t1_func". */
4444 "t1_func",
4445 "t1_func1",
4446 "t1_fund",
4447 "t1_fund1",
4448
4449 /* A UTF-8 name with multi-byte sequences to make sure that
4450 cp-name-parser understands this as a single identifier ("função"
4451 is "function" in PT). */
4452 u8"u8função",
4453
4454 /* \377 (0xff) is Latin1 'ÿ'. */
4455 "yfunc\377",
4456
4457 /* \377 (0xff) is Latin1 'ÿ'. */
4458 "\377",
4459 "\377\377123",
4460
4461 /* A name with all sorts of complications. Starts with "z" to make
4462 it easier for the completion tests below. */
4463 #define Z_SYM_NAME \
4464 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4465 "::tuple<(anonymous namespace)::ui*, " \
4466 "std::default_delete<(anonymous namespace)::ui>, void>"
4467
4468 Z_SYM_NAME
4469 };
4470
4471 /* Returns true if the mapped_index_base::find_name_component_bounds
4472 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4473 in completion mode. */
4474
4475 static bool
4476 check_find_bounds_finds (mapped_index_base &index,
4477 const char *search_name,
4478 gdb::array_view<const char *> expected_syms)
4479 {
4480 lookup_name_info lookup_name (search_name,
4481 symbol_name_match_type::FULL, true);
4482
4483 auto bounds = index.find_name_components_bounds (lookup_name,
4484 language_cplus);
4485
4486 size_t distance = std::distance (bounds.first, bounds.second);
4487 if (distance != expected_syms.size ())
4488 return false;
4489
4490 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4491 {
4492 auto nc_elem = bounds.first + exp_elem;
4493 const char *qualified = index.symbol_name_at (nc_elem->idx);
4494 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4495 return false;
4496 }
4497
4498 return true;
4499 }
4500
4501 /* Test the lower-level mapped_index::find_name_component_bounds
4502 method. */
4503
4504 static void
4505 test_mapped_index_find_name_component_bounds ()
4506 {
4507 mock_mapped_index mock_index (test_symbols);
4508
4509 mock_index.build_name_components ();
4510
4511 /* Test the lower-level mapped_index::find_name_component_bounds
4512 method in completion mode. */
4513 {
4514 static const char *expected_syms[] = {
4515 "t1_func",
4516 "t1_func1",
4517 };
4518
4519 SELF_CHECK (check_find_bounds_finds (mock_index,
4520 "t1_func", expected_syms));
4521 }
4522
4523 /* Check that the increment-last-char in the name matching algorithm
4524 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4525 {
4526 static const char *expected_syms1[] = {
4527 "\377",
4528 "\377\377123",
4529 };
4530 SELF_CHECK (check_find_bounds_finds (mock_index,
4531 "\377", expected_syms1));
4532
4533 static const char *expected_syms2[] = {
4534 "\377\377123",
4535 };
4536 SELF_CHECK (check_find_bounds_finds (mock_index,
4537 "\377\377", expected_syms2));
4538 }
4539 }
4540
4541 /* Test dw2_expand_symtabs_matching_symbol. */
4542
4543 static void
4544 test_dw2_expand_symtabs_matching_symbol ()
4545 {
4546 mock_mapped_index mock_index (test_symbols);
4547
4548 /* We let all tests run until the end even if some fails, for debug
4549 convenience. */
4550 bool any_mismatch = false;
4551
4552 /* Create the expected symbols list (an initializer_list). Needed
4553 because lists have commas, and we need to pass them to CHECK,
4554 which is a macro. */
4555 #define EXPECT(...) { __VA_ARGS__ }
4556
4557 /* Wrapper for check_match that passes down the current
4558 __FILE__/__LINE__. */
4559 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4560 any_mismatch |= !check_match (__FILE__, __LINE__, \
4561 mock_index, \
4562 NAME, MATCH_TYPE, COMPLETION_MODE, \
4563 EXPECTED_LIST)
4564
4565 /* Identity checks. */
4566 for (const char *sym : test_symbols)
4567 {
4568 /* Should be able to match all existing symbols. */
4569 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4570 EXPECT (sym));
4571
4572 /* Should be able to match all existing symbols with
4573 parameters. */
4574 std::string with_params = std::string (sym) + "(int)";
4575 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4576 EXPECT (sym));
4577
4578 /* Should be able to match all existing symbols with
4579 parameters and qualifiers. */
4580 with_params = std::string (sym) + " ( int ) const";
4581 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4582 EXPECT (sym));
4583
4584 /* This should really find sym, but cp-name-parser.y doesn't
4585 know about lvalue/rvalue qualifiers yet. */
4586 with_params = std::string (sym) + " ( int ) &&";
4587 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4588 {});
4589 }
4590
4591 /* Check that the name matching algorithm for completion doesn't get
4592 confused with Latin1 'ÿ' / 0xff. */
4593 {
4594 static const char str[] = "\377";
4595 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4596 EXPECT ("\377", "\377\377123"));
4597 }
4598
4599 /* Check that the increment-last-char in the matching algorithm for
4600 completion doesn't match "t1_fund" when completing "t1_func". */
4601 {
4602 static const char str[] = "t1_func";
4603 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4604 EXPECT ("t1_func", "t1_func1"));
4605 }
4606
4607 /* Check that completion mode works at each prefix of the expected
4608 symbol name. */
4609 {
4610 static const char str[] = "function(int)";
4611 size_t len = strlen (str);
4612 std::string lookup;
4613
4614 for (size_t i = 1; i < len; i++)
4615 {
4616 lookup.assign (str, i);
4617 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4618 EXPECT ("function"));
4619 }
4620 }
4621
4622 /* While "w" is a prefix of both components, the match function
4623 should still only be called once. */
4624 {
4625 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4626 EXPECT ("w1::w2"));
4627 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4628 EXPECT ("w1::w2"));
4629 }
4630
4631 /* Same, with a "complicated" symbol. */
4632 {
4633 static const char str[] = Z_SYM_NAME;
4634 size_t len = strlen (str);
4635 std::string lookup;
4636
4637 for (size_t i = 1; i < len; i++)
4638 {
4639 lookup.assign (str, i);
4640 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4641 EXPECT (Z_SYM_NAME));
4642 }
4643 }
4644
4645 /* In FULL mode, an incomplete symbol doesn't match. */
4646 {
4647 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4648 {});
4649 }
4650
4651 /* A complete symbol with parameters matches any overload, since the
4652 index has no overload info. */
4653 {
4654 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4655 EXPECT ("std::zfunction", "std::zfunction2"));
4656 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4657 EXPECT ("std::zfunction", "std::zfunction2"));
4658 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4659 EXPECT ("std::zfunction", "std::zfunction2"));
4660 }
4661
4662 /* Check that whitespace is ignored appropriately. A symbol with a
4663 template argument list. */
4664 {
4665 static const char expected[] = "ns::foo<int>";
4666 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4667 EXPECT (expected));
4668 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4669 EXPECT (expected));
4670 }
4671
4672 /* Check that whitespace is ignored appropriately. A symbol with a
4673 template argument list that includes a pointer. */
4674 {
4675 static const char expected[] = "ns::foo<char*>";
4676 /* Try both completion and non-completion modes. */
4677 static const bool completion_mode[2] = {false, true};
4678 for (size_t i = 0; i < 2; i++)
4679 {
4680 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4681 completion_mode[i], EXPECT (expected));
4682 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4683 completion_mode[i], EXPECT (expected));
4684
4685 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4686 completion_mode[i], EXPECT (expected));
4687 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4688 completion_mode[i], EXPECT (expected));
4689 }
4690 }
4691
4692 {
4693 /* Check method qualifiers are ignored. */
4694 static const char expected[] = "ns::foo<char*>";
4695 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4696 symbol_name_match_type::FULL, true, EXPECT (expected));
4697 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4698 symbol_name_match_type::FULL, true, EXPECT (expected));
4699 CHECK_MATCH ("foo < char * > ( int ) const",
4700 symbol_name_match_type::WILD, true, EXPECT (expected));
4701 CHECK_MATCH ("foo < char * > ( int ) &&",
4702 symbol_name_match_type::WILD, true, EXPECT (expected));
4703 }
4704
4705 /* Test lookup names that don't match anything. */
4706 {
4707 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4708 {});
4709
4710 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4711 {});
4712 }
4713
4714 /* Some wild matching tests, exercising "(anonymous namespace)",
4715 which should not be confused with a parameter list. */
4716 {
4717 static const char *syms[] = {
4718 "A::B::C",
4719 "B::C",
4720 "C",
4721 "A :: B :: C ( int )",
4722 "B :: C ( int )",
4723 "C ( int )",
4724 };
4725
4726 for (const char *s : syms)
4727 {
4728 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4729 EXPECT ("(anonymous namespace)::A::B::C"));
4730 }
4731 }
4732
4733 {
4734 static const char expected[] = "ns2::tmpl<int>::foo2";
4735 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4736 EXPECT (expected));
4737 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4738 EXPECT (expected));
4739 }
4740
4741 SELF_CHECK (!any_mismatch);
4742
4743 #undef EXPECT
4744 #undef CHECK_MATCH
4745 }
4746
4747 static void
4748 run_test ()
4749 {
4750 test_mapped_index_find_name_component_bounds ();
4751 test_dw2_expand_symtabs_matching_symbol ();
4752 }
4753
4754 }} // namespace selftests::dw2_expand_symtabs_matching
4755
4756 #endif /* GDB_SELF_TEST */
4757
4758 /* If FILE_MATCHER is NULL or if PER_CU has
4759 dwarf2_per_cu_quick_data::MARK set (see
4760 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4761 EXPANSION_NOTIFY on it. */
4762
4763 static void
4764 dw2_expand_symtabs_matching_one
4765 (struct dwarf2_per_cu_data *per_cu,
4766 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4767 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4768 {
4769 if (file_matcher == NULL || per_cu->v.quick->mark)
4770 {
4771 bool symtab_was_null
4772 = (per_cu->v.quick->compunit_symtab == NULL);
4773
4774 dw2_instantiate_symtab (per_cu, false);
4775
4776 if (expansion_notify != NULL
4777 && symtab_was_null
4778 && per_cu->v.quick->compunit_symtab != NULL)
4779 expansion_notify (per_cu->v.quick->compunit_symtab);
4780 }
4781 }
4782
4783 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4784 matched, to expand corresponding CUs that were marked. IDX is the
4785 index of the symbol name that matched. */
4786
4787 static void
4788 dw2_expand_marked_cus
4789 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4790 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4791 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4792 search_domain kind)
4793 {
4794 offset_type *vec, vec_len, vec_idx;
4795 bool global_seen = false;
4796 mapped_index &index = *dwarf2_per_objfile->index_table;
4797
4798 vec = (offset_type *) (index.constant_pool
4799 + MAYBE_SWAP (index.symbol_table[idx].vec));
4800 vec_len = MAYBE_SWAP (vec[0]);
4801 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4802 {
4803 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4804 /* This value is only valid for index versions >= 7. */
4805 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4806 gdb_index_symbol_kind symbol_kind =
4807 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4808 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4809 /* Only check the symbol attributes if they're present.
4810 Indices prior to version 7 don't record them,
4811 and indices >= 7 may elide them for certain symbols
4812 (gold does this). */
4813 int attrs_valid =
4814 (index.version >= 7
4815 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4816
4817 /* Work around gold/15646. */
4818 if (attrs_valid)
4819 {
4820 if (!is_static && global_seen)
4821 continue;
4822 if (!is_static)
4823 global_seen = true;
4824 }
4825
4826 /* Only check the symbol's kind if it has one. */
4827 if (attrs_valid)
4828 {
4829 switch (kind)
4830 {
4831 case VARIABLES_DOMAIN:
4832 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4833 continue;
4834 break;
4835 case FUNCTIONS_DOMAIN:
4836 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4837 continue;
4838 break;
4839 case TYPES_DOMAIN:
4840 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4841 continue;
4842 break;
4843 case MODULES_DOMAIN:
4844 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4845 continue;
4846 break;
4847 default:
4848 break;
4849 }
4850 }
4851
4852 /* Don't crash on bad data. */
4853 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4854 + dwarf2_per_objfile->all_type_units.size ()))
4855 {
4856 complaint (_(".gdb_index entry has bad CU index"
4857 " [in module %s]"),
4858 objfile_name (dwarf2_per_objfile->objfile));
4859 continue;
4860 }
4861
4862 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4863 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4864 expansion_notify);
4865 }
4866 }
4867
4868 /* If FILE_MATCHER is non-NULL, set all the
4869 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4870 that match FILE_MATCHER. */
4871
4872 static void
4873 dw_expand_symtabs_matching_file_matcher
4874 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4875 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4876 {
4877 if (file_matcher == NULL)
4878 return;
4879
4880 objfile *const objfile = dwarf2_per_objfile->objfile;
4881
4882 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4883 htab_eq_pointer,
4884 NULL, xcalloc, xfree));
4885 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4886 htab_eq_pointer,
4887 NULL, xcalloc, xfree));
4888
4889 /* The rule is CUs specify all the files, including those used by
4890 any TU, so there's no need to scan TUs here. */
4891
4892 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4893 {
4894 QUIT;
4895
4896 per_cu->v.quick->mark = 0;
4897
4898 /* We only need to look at symtabs not already expanded. */
4899 if (per_cu->v.quick->compunit_symtab)
4900 continue;
4901
4902 quick_file_names *file_data = dw2_get_file_names (per_cu);
4903 if (file_data == NULL)
4904 continue;
4905
4906 if (htab_find (visited_not_found.get (), file_data) != NULL)
4907 continue;
4908 else if (htab_find (visited_found.get (), file_data) != NULL)
4909 {
4910 per_cu->v.quick->mark = 1;
4911 continue;
4912 }
4913
4914 for (int j = 0; j < file_data->num_file_names; ++j)
4915 {
4916 const char *this_real_name;
4917
4918 if (file_matcher (file_data->file_names[j], false))
4919 {
4920 per_cu->v.quick->mark = 1;
4921 break;
4922 }
4923
4924 /* Before we invoke realpath, which can get expensive when many
4925 files are involved, do a quick comparison of the basenames. */
4926 if (!basenames_may_differ
4927 && !file_matcher (lbasename (file_data->file_names[j]),
4928 true))
4929 continue;
4930
4931 this_real_name = dw2_get_real_path (objfile, file_data, j);
4932 if (file_matcher (this_real_name, false))
4933 {
4934 per_cu->v.quick->mark = 1;
4935 break;
4936 }
4937 }
4938
4939 void **slot = htab_find_slot (per_cu->v.quick->mark
4940 ? visited_found.get ()
4941 : visited_not_found.get (),
4942 file_data, INSERT);
4943 *slot = file_data;
4944 }
4945 }
4946
4947 static void
4948 dw2_expand_symtabs_matching
4949 (struct objfile *objfile,
4950 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4951 const lookup_name_info &lookup_name,
4952 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4953 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4954 enum search_domain kind)
4955 {
4956 struct dwarf2_per_objfile *dwarf2_per_objfile
4957 = get_dwarf2_per_objfile (objfile);
4958
4959 /* index_table is NULL if OBJF_READNOW. */
4960 if (!dwarf2_per_objfile->index_table)
4961 return;
4962
4963 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4964
4965 mapped_index &index = *dwarf2_per_objfile->index_table;
4966
4967 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4968 symbol_matcher,
4969 kind, [&] (offset_type idx)
4970 {
4971 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4972 expansion_notify, kind);
4973 return true;
4974 });
4975 }
4976
4977 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4978 symtab. */
4979
4980 static struct compunit_symtab *
4981 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4982 CORE_ADDR pc)
4983 {
4984 int i;
4985
4986 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4987 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4988 return cust;
4989
4990 if (cust->includes == NULL)
4991 return NULL;
4992
4993 for (i = 0; cust->includes[i]; ++i)
4994 {
4995 struct compunit_symtab *s = cust->includes[i];
4996
4997 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4998 if (s != NULL)
4999 return s;
5000 }
5001
5002 return NULL;
5003 }
5004
5005 static struct compunit_symtab *
5006 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5007 struct bound_minimal_symbol msymbol,
5008 CORE_ADDR pc,
5009 struct obj_section *section,
5010 int warn_if_readin)
5011 {
5012 struct dwarf2_per_cu_data *data;
5013 struct compunit_symtab *result;
5014
5015 if (!objfile->partial_symtabs->psymtabs_addrmap)
5016 return NULL;
5017
5018 CORE_ADDR baseaddr = objfile->text_section_offset ();
5019 data = (struct dwarf2_per_cu_data *) addrmap_find
5020 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5021 if (!data)
5022 return NULL;
5023
5024 if (warn_if_readin && data->v.quick->compunit_symtab)
5025 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5026 paddress (get_objfile_arch (objfile), pc));
5027
5028 result
5029 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5030 false),
5031 pc);
5032 gdb_assert (result != NULL);
5033 return result;
5034 }
5035
5036 static void
5037 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5038 void *data, int need_fullname)
5039 {
5040 struct dwarf2_per_objfile *dwarf2_per_objfile
5041 = get_dwarf2_per_objfile (objfile);
5042
5043 if (!dwarf2_per_objfile->filenames_cache)
5044 {
5045 dwarf2_per_objfile->filenames_cache.emplace ();
5046
5047 htab_up visited (htab_create_alloc (10,
5048 htab_hash_pointer, htab_eq_pointer,
5049 NULL, xcalloc, xfree));
5050
5051 /* The rule is CUs specify all the files, including those used
5052 by any TU, so there's no need to scan TUs here. We can
5053 ignore file names coming from already-expanded CUs. */
5054
5055 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5056 {
5057 if (per_cu->v.quick->compunit_symtab)
5058 {
5059 void **slot = htab_find_slot (visited.get (),
5060 per_cu->v.quick->file_names,
5061 INSERT);
5062
5063 *slot = per_cu->v.quick->file_names;
5064 }
5065 }
5066
5067 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5068 {
5069 /* We only need to look at symtabs not already expanded. */
5070 if (per_cu->v.quick->compunit_symtab)
5071 continue;
5072
5073 quick_file_names *file_data = dw2_get_file_names (per_cu);
5074 if (file_data == NULL)
5075 continue;
5076
5077 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5078 if (*slot)
5079 {
5080 /* Already visited. */
5081 continue;
5082 }
5083 *slot = file_data;
5084
5085 for (int j = 0; j < file_data->num_file_names; ++j)
5086 {
5087 const char *filename = file_data->file_names[j];
5088 dwarf2_per_objfile->filenames_cache->seen (filename);
5089 }
5090 }
5091 }
5092
5093 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5094 {
5095 gdb::unique_xmalloc_ptr<char> this_real_name;
5096
5097 if (need_fullname)
5098 this_real_name = gdb_realpath (filename);
5099 (*fun) (filename, this_real_name.get (), data);
5100 });
5101 }
5102
5103 static int
5104 dw2_has_symbols (struct objfile *objfile)
5105 {
5106 return 1;
5107 }
5108
5109 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5110 {
5111 dw2_has_symbols,
5112 dw2_find_last_source_symtab,
5113 dw2_forget_cached_source_info,
5114 dw2_map_symtabs_matching_filename,
5115 dw2_lookup_symbol,
5116 dw2_print_stats,
5117 dw2_dump,
5118 dw2_expand_symtabs_for_function,
5119 dw2_expand_all_symtabs,
5120 dw2_expand_symtabs_with_fullname,
5121 dw2_map_matching_symbols,
5122 dw2_expand_symtabs_matching,
5123 dw2_find_pc_sect_compunit_symtab,
5124 NULL,
5125 dw2_map_symbol_filenames
5126 };
5127
5128 /* DWARF-5 debug_names reader. */
5129
5130 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5131 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5132
5133 /* A helper function that reads the .debug_names section in SECTION
5134 and fills in MAP. FILENAME is the name of the file containing the
5135 section; it is used for error reporting.
5136
5137 Returns true if all went well, false otherwise. */
5138
5139 static bool
5140 read_debug_names_from_section (struct objfile *objfile,
5141 const char *filename,
5142 struct dwarf2_section_info *section,
5143 mapped_debug_names &map)
5144 {
5145 if (section->empty ())
5146 return false;
5147
5148 /* Older elfutils strip versions could keep the section in the main
5149 executable while splitting it for the separate debug info file. */
5150 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5151 return false;
5152
5153 section->read (objfile);
5154
5155 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5156
5157 const gdb_byte *addr = section->buffer;
5158
5159 bfd *const abfd = section->get_bfd_owner ();
5160
5161 unsigned int bytes_read;
5162 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5163 addr += bytes_read;
5164
5165 map.dwarf5_is_dwarf64 = bytes_read != 4;
5166 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5167 if (bytes_read + length != section->size)
5168 {
5169 /* There may be multiple per-CU indices. */
5170 warning (_("Section .debug_names in %s length %s does not match "
5171 "section length %s, ignoring .debug_names."),
5172 filename, plongest (bytes_read + length),
5173 pulongest (section->size));
5174 return false;
5175 }
5176
5177 /* The version number. */
5178 uint16_t version = read_2_bytes (abfd, addr);
5179 addr += 2;
5180 if (version != 5)
5181 {
5182 warning (_("Section .debug_names in %s has unsupported version %d, "
5183 "ignoring .debug_names."),
5184 filename, version);
5185 return false;
5186 }
5187
5188 /* Padding. */
5189 uint16_t padding = read_2_bytes (abfd, addr);
5190 addr += 2;
5191 if (padding != 0)
5192 {
5193 warning (_("Section .debug_names in %s has unsupported padding %d, "
5194 "ignoring .debug_names."),
5195 filename, padding);
5196 return false;
5197 }
5198
5199 /* comp_unit_count - The number of CUs in the CU list. */
5200 map.cu_count = read_4_bytes (abfd, addr);
5201 addr += 4;
5202
5203 /* local_type_unit_count - The number of TUs in the local TU
5204 list. */
5205 map.tu_count = read_4_bytes (abfd, addr);
5206 addr += 4;
5207
5208 /* foreign_type_unit_count - The number of TUs in the foreign TU
5209 list. */
5210 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5211 addr += 4;
5212 if (foreign_tu_count != 0)
5213 {
5214 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5215 "ignoring .debug_names."),
5216 filename, static_cast<unsigned long> (foreign_tu_count));
5217 return false;
5218 }
5219
5220 /* bucket_count - The number of hash buckets in the hash lookup
5221 table. */
5222 map.bucket_count = read_4_bytes (abfd, addr);
5223 addr += 4;
5224
5225 /* name_count - The number of unique names in the index. */
5226 map.name_count = read_4_bytes (abfd, addr);
5227 addr += 4;
5228
5229 /* abbrev_table_size - The size in bytes of the abbreviations
5230 table. */
5231 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5232 addr += 4;
5233
5234 /* augmentation_string_size - The size in bytes of the augmentation
5235 string. This value is rounded up to a multiple of 4. */
5236 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5237 addr += 4;
5238 map.augmentation_is_gdb = ((augmentation_string_size
5239 == sizeof (dwarf5_augmentation))
5240 && memcmp (addr, dwarf5_augmentation,
5241 sizeof (dwarf5_augmentation)) == 0);
5242 augmentation_string_size += (-augmentation_string_size) & 3;
5243 addr += augmentation_string_size;
5244
5245 /* List of CUs */
5246 map.cu_table_reordered = addr;
5247 addr += map.cu_count * map.offset_size;
5248
5249 /* List of Local TUs */
5250 map.tu_table_reordered = addr;
5251 addr += map.tu_count * map.offset_size;
5252
5253 /* Hash Lookup Table */
5254 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5255 addr += map.bucket_count * 4;
5256 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5257 addr += map.name_count * 4;
5258
5259 /* Name Table */
5260 map.name_table_string_offs_reordered = addr;
5261 addr += map.name_count * map.offset_size;
5262 map.name_table_entry_offs_reordered = addr;
5263 addr += map.name_count * map.offset_size;
5264
5265 const gdb_byte *abbrev_table_start = addr;
5266 for (;;)
5267 {
5268 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5269 addr += bytes_read;
5270 if (index_num == 0)
5271 break;
5272
5273 const auto insertpair
5274 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5275 if (!insertpair.second)
5276 {
5277 warning (_("Section .debug_names in %s has duplicate index %s, "
5278 "ignoring .debug_names."),
5279 filename, pulongest (index_num));
5280 return false;
5281 }
5282 mapped_debug_names::index_val &indexval = insertpair.first->second;
5283 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5284 addr += bytes_read;
5285
5286 for (;;)
5287 {
5288 mapped_debug_names::index_val::attr attr;
5289 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5290 addr += bytes_read;
5291 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5292 addr += bytes_read;
5293 if (attr.form == DW_FORM_implicit_const)
5294 {
5295 attr.implicit_const = read_signed_leb128 (abfd, addr,
5296 &bytes_read);
5297 addr += bytes_read;
5298 }
5299 if (attr.dw_idx == 0 && attr.form == 0)
5300 break;
5301 indexval.attr_vec.push_back (std::move (attr));
5302 }
5303 }
5304 if (addr != abbrev_table_start + abbrev_table_size)
5305 {
5306 warning (_("Section .debug_names in %s has abbreviation_table "
5307 "of size %s vs. written as %u, ignoring .debug_names."),
5308 filename, plongest (addr - abbrev_table_start),
5309 abbrev_table_size);
5310 return false;
5311 }
5312 map.entry_pool = addr;
5313
5314 return true;
5315 }
5316
5317 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5318 list. */
5319
5320 static void
5321 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5322 const mapped_debug_names &map,
5323 dwarf2_section_info &section,
5324 bool is_dwz)
5325 {
5326 sect_offset sect_off_prev;
5327 for (uint32_t i = 0; i <= map.cu_count; ++i)
5328 {
5329 sect_offset sect_off_next;
5330 if (i < map.cu_count)
5331 {
5332 sect_off_next
5333 = (sect_offset) (extract_unsigned_integer
5334 (map.cu_table_reordered + i * map.offset_size,
5335 map.offset_size,
5336 map.dwarf5_byte_order));
5337 }
5338 else
5339 sect_off_next = (sect_offset) section.size;
5340 if (i >= 1)
5341 {
5342 const ULONGEST length = sect_off_next - sect_off_prev;
5343 dwarf2_per_cu_data *per_cu
5344 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5345 sect_off_prev, length);
5346 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5347 }
5348 sect_off_prev = sect_off_next;
5349 }
5350 }
5351
5352 /* Read the CU list from the mapped index, and use it to create all
5353 the CU objects for this dwarf2_per_objfile. */
5354
5355 static void
5356 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5357 const mapped_debug_names &map,
5358 const mapped_debug_names &dwz_map)
5359 {
5360 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5361 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5362
5363 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5364 dwarf2_per_objfile->info,
5365 false /* is_dwz */);
5366
5367 if (dwz_map.cu_count == 0)
5368 return;
5369
5370 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5371 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5372 true /* is_dwz */);
5373 }
5374
5375 /* Read .debug_names. If everything went ok, initialize the "quick"
5376 elements of all the CUs and return true. Otherwise, return false. */
5377
5378 static bool
5379 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5380 {
5381 std::unique_ptr<mapped_debug_names> map
5382 (new mapped_debug_names (dwarf2_per_objfile));
5383 mapped_debug_names dwz_map (dwarf2_per_objfile);
5384 struct objfile *objfile = dwarf2_per_objfile->objfile;
5385
5386 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5387 &dwarf2_per_objfile->debug_names,
5388 *map))
5389 return false;
5390
5391 /* Don't use the index if it's empty. */
5392 if (map->name_count == 0)
5393 return false;
5394
5395 /* If there is a .dwz file, read it so we can get its CU list as
5396 well. */
5397 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5398 if (dwz != NULL)
5399 {
5400 if (!read_debug_names_from_section (objfile,
5401 bfd_get_filename (dwz->dwz_bfd.get ()),
5402 &dwz->debug_names, dwz_map))
5403 {
5404 warning (_("could not read '.debug_names' section from %s; skipping"),
5405 bfd_get_filename (dwz->dwz_bfd.get ()));
5406 return false;
5407 }
5408 }
5409
5410 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5411
5412 if (map->tu_count != 0)
5413 {
5414 /* We can only handle a single .debug_types when we have an
5415 index. */
5416 if (dwarf2_per_objfile->types.size () != 1)
5417 return false;
5418
5419 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5420
5421 create_signatured_type_table_from_debug_names
5422 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5423 }
5424
5425 create_addrmap_from_aranges (dwarf2_per_objfile,
5426 &dwarf2_per_objfile->debug_aranges);
5427
5428 dwarf2_per_objfile->debug_names_table = std::move (map);
5429 dwarf2_per_objfile->using_index = 1;
5430 dwarf2_per_objfile->quick_file_names_table =
5431 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5432
5433 return true;
5434 }
5435
5436 /* Type used to manage iterating over all CUs looking for a symbol for
5437 .debug_names. */
5438
5439 class dw2_debug_names_iterator
5440 {
5441 public:
5442 dw2_debug_names_iterator (const mapped_debug_names &map,
5443 gdb::optional<block_enum> block_index,
5444 domain_enum domain,
5445 const char *name)
5446 : m_map (map), m_block_index (block_index), m_domain (domain),
5447 m_addr (find_vec_in_debug_names (map, name))
5448 {}
5449
5450 dw2_debug_names_iterator (const mapped_debug_names &map,
5451 search_domain search, uint32_t namei)
5452 : m_map (map),
5453 m_search (search),
5454 m_addr (find_vec_in_debug_names (map, namei))
5455 {}
5456
5457 dw2_debug_names_iterator (const mapped_debug_names &map,
5458 block_enum block_index, domain_enum domain,
5459 uint32_t namei)
5460 : m_map (map), m_block_index (block_index), m_domain (domain),
5461 m_addr (find_vec_in_debug_names (map, namei))
5462 {}
5463
5464 /* Return the next matching CU or NULL if there are no more. */
5465 dwarf2_per_cu_data *next ();
5466
5467 private:
5468 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5469 const char *name);
5470 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5471 uint32_t namei);
5472
5473 /* The internalized form of .debug_names. */
5474 const mapped_debug_names &m_map;
5475
5476 /* If set, only look for symbols that match that block. Valid values are
5477 GLOBAL_BLOCK and STATIC_BLOCK. */
5478 const gdb::optional<block_enum> m_block_index;
5479
5480 /* The kind of symbol we're looking for. */
5481 const domain_enum m_domain = UNDEF_DOMAIN;
5482 const search_domain m_search = ALL_DOMAIN;
5483
5484 /* The list of CUs from the index entry of the symbol, or NULL if
5485 not found. */
5486 const gdb_byte *m_addr;
5487 };
5488
5489 const char *
5490 mapped_debug_names::namei_to_name (uint32_t namei) const
5491 {
5492 const ULONGEST namei_string_offs
5493 = extract_unsigned_integer ((name_table_string_offs_reordered
5494 + namei * offset_size),
5495 offset_size,
5496 dwarf5_byte_order);
5497 return read_indirect_string_at_offset
5498 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5499 }
5500
5501 /* Find a slot in .debug_names for the object named NAME. If NAME is
5502 found, return pointer to its pool data. If NAME cannot be found,
5503 return NULL. */
5504
5505 const gdb_byte *
5506 dw2_debug_names_iterator::find_vec_in_debug_names
5507 (const mapped_debug_names &map, const char *name)
5508 {
5509 int (*cmp) (const char *, const char *);
5510
5511 gdb::unique_xmalloc_ptr<char> without_params;
5512 if (current_language->la_language == language_cplus
5513 || current_language->la_language == language_fortran
5514 || current_language->la_language == language_d)
5515 {
5516 /* NAME is already canonical. Drop any qualifiers as
5517 .debug_names does not contain any. */
5518
5519 if (strchr (name, '(') != NULL)
5520 {
5521 without_params = cp_remove_params (name);
5522 if (without_params != NULL)
5523 name = without_params.get ();
5524 }
5525 }
5526
5527 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5528
5529 const uint32_t full_hash = dwarf5_djb_hash (name);
5530 uint32_t namei
5531 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5532 (map.bucket_table_reordered
5533 + (full_hash % map.bucket_count)), 4,
5534 map.dwarf5_byte_order);
5535 if (namei == 0)
5536 return NULL;
5537 --namei;
5538 if (namei >= map.name_count)
5539 {
5540 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5541 "[in module %s]"),
5542 namei, map.name_count,
5543 objfile_name (map.dwarf2_per_objfile->objfile));
5544 return NULL;
5545 }
5546
5547 for (;;)
5548 {
5549 const uint32_t namei_full_hash
5550 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5551 (map.hash_table_reordered + namei), 4,
5552 map.dwarf5_byte_order);
5553 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5554 return NULL;
5555
5556 if (full_hash == namei_full_hash)
5557 {
5558 const char *const namei_string = map.namei_to_name (namei);
5559
5560 #if 0 /* An expensive sanity check. */
5561 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5562 {
5563 complaint (_("Wrong .debug_names hash for string at index %u "
5564 "[in module %s]"),
5565 namei, objfile_name (dwarf2_per_objfile->objfile));
5566 return NULL;
5567 }
5568 #endif
5569
5570 if (cmp (namei_string, name) == 0)
5571 {
5572 const ULONGEST namei_entry_offs
5573 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5574 + namei * map.offset_size),
5575 map.offset_size, map.dwarf5_byte_order);
5576 return map.entry_pool + namei_entry_offs;
5577 }
5578 }
5579
5580 ++namei;
5581 if (namei >= map.name_count)
5582 return NULL;
5583 }
5584 }
5585
5586 const gdb_byte *
5587 dw2_debug_names_iterator::find_vec_in_debug_names
5588 (const mapped_debug_names &map, uint32_t namei)
5589 {
5590 if (namei >= map.name_count)
5591 {
5592 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5593 "[in module %s]"),
5594 namei, map.name_count,
5595 objfile_name (map.dwarf2_per_objfile->objfile));
5596 return NULL;
5597 }
5598
5599 const ULONGEST namei_entry_offs
5600 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5601 + namei * map.offset_size),
5602 map.offset_size, map.dwarf5_byte_order);
5603 return map.entry_pool + namei_entry_offs;
5604 }
5605
5606 /* See dw2_debug_names_iterator. */
5607
5608 dwarf2_per_cu_data *
5609 dw2_debug_names_iterator::next ()
5610 {
5611 if (m_addr == NULL)
5612 return NULL;
5613
5614 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5615 struct objfile *objfile = dwarf2_per_objfile->objfile;
5616 bfd *const abfd = objfile->obfd;
5617
5618 again:
5619
5620 unsigned int bytes_read;
5621 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5622 m_addr += bytes_read;
5623 if (abbrev == 0)
5624 return NULL;
5625
5626 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5627 if (indexval_it == m_map.abbrev_map.cend ())
5628 {
5629 complaint (_("Wrong .debug_names undefined abbrev code %s "
5630 "[in module %s]"),
5631 pulongest (abbrev), objfile_name (objfile));
5632 return NULL;
5633 }
5634 const mapped_debug_names::index_val &indexval = indexval_it->second;
5635 enum class symbol_linkage {
5636 unknown,
5637 static_,
5638 extern_,
5639 } symbol_linkage_ = symbol_linkage::unknown;
5640 dwarf2_per_cu_data *per_cu = NULL;
5641 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5642 {
5643 ULONGEST ull;
5644 switch (attr.form)
5645 {
5646 case DW_FORM_implicit_const:
5647 ull = attr.implicit_const;
5648 break;
5649 case DW_FORM_flag_present:
5650 ull = 1;
5651 break;
5652 case DW_FORM_udata:
5653 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5654 m_addr += bytes_read;
5655 break;
5656 default:
5657 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5658 dwarf_form_name (attr.form),
5659 objfile_name (objfile));
5660 return NULL;
5661 }
5662 switch (attr.dw_idx)
5663 {
5664 case DW_IDX_compile_unit:
5665 /* Don't crash on bad data. */
5666 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5667 {
5668 complaint (_(".debug_names entry has bad CU index %s"
5669 " [in module %s]"),
5670 pulongest (ull),
5671 objfile_name (dwarf2_per_objfile->objfile));
5672 continue;
5673 }
5674 per_cu = dwarf2_per_objfile->get_cutu (ull);
5675 break;
5676 case DW_IDX_type_unit:
5677 /* Don't crash on bad data. */
5678 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5679 {
5680 complaint (_(".debug_names entry has bad TU index %s"
5681 " [in module %s]"),
5682 pulongest (ull),
5683 objfile_name (dwarf2_per_objfile->objfile));
5684 continue;
5685 }
5686 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5687 break;
5688 case DW_IDX_GNU_internal:
5689 if (!m_map.augmentation_is_gdb)
5690 break;
5691 symbol_linkage_ = symbol_linkage::static_;
5692 break;
5693 case DW_IDX_GNU_external:
5694 if (!m_map.augmentation_is_gdb)
5695 break;
5696 symbol_linkage_ = symbol_linkage::extern_;
5697 break;
5698 }
5699 }
5700
5701 /* Skip if already read in. */
5702 if (per_cu->v.quick->compunit_symtab)
5703 goto again;
5704
5705 /* Check static vs global. */
5706 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5707 {
5708 const bool want_static = *m_block_index == STATIC_BLOCK;
5709 const bool symbol_is_static =
5710 symbol_linkage_ == symbol_linkage::static_;
5711 if (want_static != symbol_is_static)
5712 goto again;
5713 }
5714
5715 /* Match dw2_symtab_iter_next, symbol_kind
5716 and debug_names::psymbol_tag. */
5717 switch (m_domain)
5718 {
5719 case VAR_DOMAIN:
5720 switch (indexval.dwarf_tag)
5721 {
5722 case DW_TAG_variable:
5723 case DW_TAG_subprogram:
5724 /* Some types are also in VAR_DOMAIN. */
5725 case DW_TAG_typedef:
5726 case DW_TAG_structure_type:
5727 break;
5728 default:
5729 goto again;
5730 }
5731 break;
5732 case STRUCT_DOMAIN:
5733 switch (indexval.dwarf_tag)
5734 {
5735 case DW_TAG_typedef:
5736 case DW_TAG_structure_type:
5737 break;
5738 default:
5739 goto again;
5740 }
5741 break;
5742 case LABEL_DOMAIN:
5743 switch (indexval.dwarf_tag)
5744 {
5745 case 0:
5746 case DW_TAG_variable:
5747 break;
5748 default:
5749 goto again;
5750 }
5751 break;
5752 case MODULE_DOMAIN:
5753 switch (indexval.dwarf_tag)
5754 {
5755 case DW_TAG_module:
5756 break;
5757 default:
5758 goto again;
5759 }
5760 break;
5761 default:
5762 break;
5763 }
5764
5765 /* Match dw2_expand_symtabs_matching, symbol_kind and
5766 debug_names::psymbol_tag. */
5767 switch (m_search)
5768 {
5769 case VARIABLES_DOMAIN:
5770 switch (indexval.dwarf_tag)
5771 {
5772 case DW_TAG_variable:
5773 break;
5774 default:
5775 goto again;
5776 }
5777 break;
5778 case FUNCTIONS_DOMAIN:
5779 switch (indexval.dwarf_tag)
5780 {
5781 case DW_TAG_subprogram:
5782 break;
5783 default:
5784 goto again;
5785 }
5786 break;
5787 case TYPES_DOMAIN:
5788 switch (indexval.dwarf_tag)
5789 {
5790 case DW_TAG_typedef:
5791 case DW_TAG_structure_type:
5792 break;
5793 default:
5794 goto again;
5795 }
5796 break;
5797 case MODULES_DOMAIN:
5798 switch (indexval.dwarf_tag)
5799 {
5800 case DW_TAG_module:
5801 break;
5802 default:
5803 goto again;
5804 }
5805 default:
5806 break;
5807 }
5808
5809 return per_cu;
5810 }
5811
5812 static struct compunit_symtab *
5813 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5814 const char *name, domain_enum domain)
5815 {
5816 struct dwarf2_per_objfile *dwarf2_per_objfile
5817 = get_dwarf2_per_objfile (objfile);
5818
5819 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5820 if (!mapp)
5821 {
5822 /* index is NULL if OBJF_READNOW. */
5823 return NULL;
5824 }
5825 const auto &map = *mapp;
5826
5827 dw2_debug_names_iterator iter (map, block_index, domain, name);
5828
5829 struct compunit_symtab *stab_best = NULL;
5830 struct dwarf2_per_cu_data *per_cu;
5831 while ((per_cu = iter.next ()) != NULL)
5832 {
5833 struct symbol *sym, *with_opaque = NULL;
5834 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5835 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5836 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5837
5838 sym = block_find_symbol (block, name, domain,
5839 block_find_non_opaque_type_preferred,
5840 &with_opaque);
5841
5842 /* Some caution must be observed with overloaded functions and
5843 methods, since the index will not contain any overload
5844 information (but NAME might contain it). */
5845
5846 if (sym != NULL
5847 && strcmp_iw (sym->search_name (), name) == 0)
5848 return stab;
5849 if (with_opaque != NULL
5850 && strcmp_iw (with_opaque->search_name (), name) == 0)
5851 stab_best = stab;
5852
5853 /* Keep looking through other CUs. */
5854 }
5855
5856 return stab_best;
5857 }
5858
5859 /* This dumps minimal information about .debug_names. It is called
5860 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5861 uses this to verify that .debug_names has been loaded. */
5862
5863 static void
5864 dw2_debug_names_dump (struct objfile *objfile)
5865 {
5866 struct dwarf2_per_objfile *dwarf2_per_objfile
5867 = get_dwarf2_per_objfile (objfile);
5868
5869 gdb_assert (dwarf2_per_objfile->using_index);
5870 printf_filtered (".debug_names:");
5871 if (dwarf2_per_objfile->debug_names_table)
5872 printf_filtered (" exists\n");
5873 else
5874 printf_filtered (" faked for \"readnow\"\n");
5875 printf_filtered ("\n");
5876 }
5877
5878 static void
5879 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5880 const char *func_name)
5881 {
5882 struct dwarf2_per_objfile *dwarf2_per_objfile
5883 = get_dwarf2_per_objfile (objfile);
5884
5885 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5886 if (dwarf2_per_objfile->debug_names_table)
5887 {
5888 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5889
5890 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5891
5892 struct dwarf2_per_cu_data *per_cu;
5893 while ((per_cu = iter.next ()) != NULL)
5894 dw2_instantiate_symtab (per_cu, false);
5895 }
5896 }
5897
5898 static void
5899 dw2_debug_names_map_matching_symbols
5900 (struct objfile *objfile,
5901 const lookup_name_info &name, domain_enum domain,
5902 int global,
5903 gdb::function_view<symbol_found_callback_ftype> callback,
5904 symbol_compare_ftype *ordered_compare)
5905 {
5906 struct dwarf2_per_objfile *dwarf2_per_objfile
5907 = get_dwarf2_per_objfile (objfile);
5908
5909 /* debug_names_table is NULL if OBJF_READNOW. */
5910 if (!dwarf2_per_objfile->debug_names_table)
5911 return;
5912
5913 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5914 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5915
5916 const char *match_name = name.ada ().lookup_name ().c_str ();
5917 auto matcher = [&] (const char *symname)
5918 {
5919 if (ordered_compare == nullptr)
5920 return true;
5921 return ordered_compare (symname, match_name) == 0;
5922 };
5923
5924 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5925 [&] (offset_type namei)
5926 {
5927 /* The name was matched, now expand corresponding CUs that were
5928 marked. */
5929 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5930
5931 struct dwarf2_per_cu_data *per_cu;
5932 while ((per_cu = iter.next ()) != NULL)
5933 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5934 return true;
5935 });
5936
5937 /* It's a shame we couldn't do this inside the
5938 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5939 that have already been expanded. Instead, this loop matches what
5940 the psymtab code does. */
5941 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5942 {
5943 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5944 if (cust != nullptr)
5945 {
5946 const struct block *block
5947 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5948 if (!iterate_over_symbols_terminated (block, name,
5949 domain, callback))
5950 break;
5951 }
5952 }
5953 }
5954
5955 static void
5956 dw2_debug_names_expand_symtabs_matching
5957 (struct objfile *objfile,
5958 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5959 const lookup_name_info &lookup_name,
5960 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5961 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5962 enum search_domain kind)
5963 {
5964 struct dwarf2_per_objfile *dwarf2_per_objfile
5965 = get_dwarf2_per_objfile (objfile);
5966
5967 /* debug_names_table is NULL if OBJF_READNOW. */
5968 if (!dwarf2_per_objfile->debug_names_table)
5969 return;
5970
5971 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5972
5973 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5974
5975 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5976 symbol_matcher,
5977 kind, [&] (offset_type namei)
5978 {
5979 /* The name was matched, now expand corresponding CUs that were
5980 marked. */
5981 dw2_debug_names_iterator iter (map, kind, namei);
5982
5983 struct dwarf2_per_cu_data *per_cu;
5984 while ((per_cu = iter.next ()) != NULL)
5985 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5986 expansion_notify);
5987 return true;
5988 });
5989 }
5990
5991 const struct quick_symbol_functions dwarf2_debug_names_functions =
5992 {
5993 dw2_has_symbols,
5994 dw2_find_last_source_symtab,
5995 dw2_forget_cached_source_info,
5996 dw2_map_symtabs_matching_filename,
5997 dw2_debug_names_lookup_symbol,
5998 dw2_print_stats,
5999 dw2_debug_names_dump,
6000 dw2_debug_names_expand_symtabs_for_function,
6001 dw2_expand_all_symtabs,
6002 dw2_expand_symtabs_with_fullname,
6003 dw2_debug_names_map_matching_symbols,
6004 dw2_debug_names_expand_symtabs_matching,
6005 dw2_find_pc_sect_compunit_symtab,
6006 NULL,
6007 dw2_map_symbol_filenames
6008 };
6009
6010 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6011 to either a dwarf2_per_objfile or dwz_file object. */
6012
6013 template <typename T>
6014 static gdb::array_view<const gdb_byte>
6015 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6016 {
6017 dwarf2_section_info *section = &section_owner->gdb_index;
6018
6019 if (section->empty ())
6020 return {};
6021
6022 /* Older elfutils strip versions could keep the section in the main
6023 executable while splitting it for the separate debug info file. */
6024 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6025 return {};
6026
6027 section->read (obj);
6028
6029 /* dwarf2_section_info::size is a bfd_size_type, while
6030 gdb::array_view works with size_t. On 32-bit hosts, with
6031 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6032 is 32-bit. So we need an explicit narrowing conversion here.
6033 This is fine, because it's impossible to allocate or mmap an
6034 array/buffer larger than what size_t can represent. */
6035 return gdb::make_array_view (section->buffer, section->size);
6036 }
6037
6038 /* Lookup the index cache for the contents of the index associated to
6039 DWARF2_OBJ. */
6040
6041 static gdb::array_view<const gdb_byte>
6042 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6043 {
6044 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6045 if (build_id == nullptr)
6046 return {};
6047
6048 return global_index_cache.lookup_gdb_index (build_id,
6049 &dwarf2_obj->index_cache_res);
6050 }
6051
6052 /* Same as the above, but for DWZ. */
6053
6054 static gdb::array_view<const gdb_byte>
6055 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6056 {
6057 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6058 if (build_id == nullptr)
6059 return {};
6060
6061 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6062 }
6063
6064 /* See symfile.h. */
6065
6066 bool
6067 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6068 {
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6071
6072 /* If we're about to read full symbols, don't bother with the
6073 indices. In this case we also don't care if some other debug
6074 format is making psymtabs, because they are all about to be
6075 expanded anyway. */
6076 if ((objfile->flags & OBJF_READNOW))
6077 {
6078 dwarf2_per_objfile->using_index = 1;
6079 create_all_comp_units (dwarf2_per_objfile);
6080 create_all_type_units (dwarf2_per_objfile);
6081 dwarf2_per_objfile->quick_file_names_table
6082 = create_quick_file_names_table
6083 (dwarf2_per_objfile->all_comp_units.size ());
6084
6085 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6086 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6087 {
6088 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6089
6090 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6091 struct dwarf2_per_cu_quick_data);
6092 }
6093
6094 /* Return 1 so that gdb sees the "quick" functions. However,
6095 these functions will be no-ops because we will have expanded
6096 all symtabs. */
6097 *index_kind = dw_index_kind::GDB_INDEX;
6098 return true;
6099 }
6100
6101 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6102 {
6103 *index_kind = dw_index_kind::DEBUG_NAMES;
6104 return true;
6105 }
6106
6107 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6108 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6109 get_gdb_index_contents_from_section<dwz_file>))
6110 {
6111 *index_kind = dw_index_kind::GDB_INDEX;
6112 return true;
6113 }
6114
6115 /* ... otherwise, try to find the index in the index cache. */
6116 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6117 get_gdb_index_contents_from_cache,
6118 get_gdb_index_contents_from_cache_dwz))
6119 {
6120 global_index_cache.hit ();
6121 *index_kind = dw_index_kind::GDB_INDEX;
6122 return true;
6123 }
6124
6125 global_index_cache.miss ();
6126 return false;
6127 }
6128
6129 \f
6130
6131 /* Build a partial symbol table. */
6132
6133 void
6134 dwarf2_build_psymtabs (struct objfile *objfile)
6135 {
6136 struct dwarf2_per_objfile *dwarf2_per_objfile
6137 = get_dwarf2_per_objfile (objfile);
6138
6139 init_psymbol_list (objfile, 1024);
6140
6141 try
6142 {
6143 /* This isn't really ideal: all the data we allocate on the
6144 objfile's obstack is still uselessly kept around. However,
6145 freeing it seems unsafe. */
6146 psymtab_discarder psymtabs (objfile);
6147 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6148 psymtabs.keep ();
6149
6150 /* (maybe) store an index in the cache. */
6151 global_index_cache.store (dwarf2_per_objfile);
6152 }
6153 catch (const gdb_exception_error &except)
6154 {
6155 exception_print (gdb_stderr, except);
6156 }
6157 }
6158
6159 /* Return the total length of the CU described by HEADER. */
6160
6161 static unsigned int
6162 get_cu_length (const struct comp_unit_head *header)
6163 {
6164 return header->initial_length_size + header->length;
6165 }
6166
6167 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6168
6169 static inline bool
6170 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6171 {
6172 sect_offset bottom = cu_header->sect_off;
6173 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6174
6175 return sect_off >= bottom && sect_off < top;
6176 }
6177
6178 /* Find the base address of the compilation unit for range lists and
6179 location lists. It will normally be specified by DW_AT_low_pc.
6180 In DWARF-3 draft 4, the base address could be overridden by
6181 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6182 compilation units with discontinuous ranges. */
6183
6184 static void
6185 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6186 {
6187 struct attribute *attr;
6188
6189 cu->base_known = 0;
6190 cu->base_address = 0;
6191
6192 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6193 if (attr != nullptr)
6194 {
6195 cu->base_address = attr_value_as_address (attr);
6196 cu->base_known = 1;
6197 }
6198 else
6199 {
6200 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6201 if (attr != nullptr)
6202 {
6203 cu->base_address = attr_value_as_address (attr);
6204 cu->base_known = 1;
6205 }
6206 }
6207 }
6208
6209 /* Read in the comp unit header information from the debug_info at info_ptr.
6210 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6211 NOTE: This leaves members offset, first_die_offset to be filled in
6212 by the caller. */
6213
6214 static const gdb_byte *
6215 read_comp_unit_head (struct comp_unit_head *cu_header,
6216 const gdb_byte *info_ptr,
6217 struct dwarf2_section_info *section,
6218 rcuh_kind section_kind)
6219 {
6220 int signed_addr;
6221 unsigned int bytes_read;
6222 const char *filename = section->get_file_name ();
6223 bfd *abfd = section->get_bfd_owner ();
6224
6225 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6226 cu_header->initial_length_size = bytes_read;
6227 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6228 info_ptr += bytes_read;
6229 cu_header->version = read_2_bytes (abfd, info_ptr);
6230 if (cu_header->version < 2 || cu_header->version > 5)
6231 error (_("Dwarf Error: wrong version in compilation unit header "
6232 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6233 cu_header->version, filename);
6234 info_ptr += 2;
6235 if (cu_header->version < 5)
6236 switch (section_kind)
6237 {
6238 case rcuh_kind::COMPILE:
6239 cu_header->unit_type = DW_UT_compile;
6240 break;
6241 case rcuh_kind::TYPE:
6242 cu_header->unit_type = DW_UT_type;
6243 break;
6244 default:
6245 internal_error (__FILE__, __LINE__,
6246 _("read_comp_unit_head: invalid section_kind"));
6247 }
6248 else
6249 {
6250 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6251 (read_1_byte (abfd, info_ptr));
6252 info_ptr += 1;
6253 switch (cu_header->unit_type)
6254 {
6255 case DW_UT_compile:
6256 case DW_UT_partial:
6257 case DW_UT_skeleton:
6258 case DW_UT_split_compile:
6259 if (section_kind != rcuh_kind::COMPILE)
6260 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6261 "(is %s, should be %s) [in module %s]"),
6262 dwarf_unit_type_name (cu_header->unit_type),
6263 dwarf_unit_type_name (DW_UT_type), filename);
6264 break;
6265 case DW_UT_type:
6266 case DW_UT_split_type:
6267 section_kind = rcuh_kind::TYPE;
6268 break;
6269 default:
6270 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6271 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6272 "[in module %s]"), cu_header->unit_type,
6273 dwarf_unit_type_name (DW_UT_compile),
6274 dwarf_unit_type_name (DW_UT_skeleton),
6275 dwarf_unit_type_name (DW_UT_split_compile),
6276 dwarf_unit_type_name (DW_UT_type),
6277 dwarf_unit_type_name (DW_UT_split_type), filename);
6278 }
6279
6280 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6281 info_ptr += 1;
6282 }
6283 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6284 cu_header,
6285 &bytes_read);
6286 info_ptr += bytes_read;
6287 if (cu_header->version < 5)
6288 {
6289 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6290 info_ptr += 1;
6291 }
6292 signed_addr = bfd_get_sign_extend_vma (abfd);
6293 if (signed_addr < 0)
6294 internal_error (__FILE__, __LINE__,
6295 _("read_comp_unit_head: dwarf from non elf file"));
6296 cu_header->signed_addr_p = signed_addr;
6297
6298 bool header_has_signature = section_kind == rcuh_kind::TYPE
6299 || cu_header->unit_type == DW_UT_skeleton
6300 || cu_header->unit_type == DW_UT_split_compile;
6301
6302 if (header_has_signature)
6303 {
6304 cu_header->signature = read_8_bytes (abfd, info_ptr);
6305 info_ptr += 8;
6306 }
6307
6308 if (section_kind == rcuh_kind::TYPE)
6309 {
6310 LONGEST type_offset;
6311 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6312 info_ptr += bytes_read;
6313 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6314 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6315 error (_("Dwarf Error: Too big type_offset in compilation unit "
6316 "header (is %s) [in module %s]"), plongest (type_offset),
6317 filename);
6318 }
6319
6320 return info_ptr;
6321 }
6322
6323 /* Helper function that returns the proper abbrev section for
6324 THIS_CU. */
6325
6326 static struct dwarf2_section_info *
6327 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6328 {
6329 struct dwarf2_section_info *abbrev;
6330 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6331
6332 if (this_cu->is_dwz)
6333 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6334 else
6335 abbrev = &dwarf2_per_objfile->abbrev;
6336
6337 return abbrev;
6338 }
6339
6340 /* Subroutine of read_and_check_comp_unit_head and
6341 read_and_check_type_unit_head to simplify them.
6342 Perform various error checking on the header. */
6343
6344 static void
6345 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6346 struct comp_unit_head *header,
6347 struct dwarf2_section_info *section,
6348 struct dwarf2_section_info *abbrev_section)
6349 {
6350 const char *filename = section->get_file_name ();
6351
6352 if (to_underlying (header->abbrev_sect_off)
6353 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6354 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6355 "(offset %s + 6) [in module %s]"),
6356 sect_offset_str (header->abbrev_sect_off),
6357 sect_offset_str (header->sect_off),
6358 filename);
6359
6360 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6361 avoid potential 32-bit overflow. */
6362 if (((ULONGEST) header->sect_off + get_cu_length (header))
6363 > section->size)
6364 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6365 "(offset %s + 0) [in module %s]"),
6366 header->length, sect_offset_str (header->sect_off),
6367 filename);
6368 }
6369
6370 /* Read in a CU/TU header and perform some basic error checking.
6371 The contents of the header are stored in HEADER.
6372 The result is a pointer to the start of the first DIE. */
6373
6374 static const gdb_byte *
6375 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6376 struct comp_unit_head *header,
6377 struct dwarf2_section_info *section,
6378 struct dwarf2_section_info *abbrev_section,
6379 const gdb_byte *info_ptr,
6380 rcuh_kind section_kind)
6381 {
6382 const gdb_byte *beg_of_comp_unit = info_ptr;
6383
6384 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6385
6386 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6387
6388 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6389
6390 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6391 abbrev_section);
6392
6393 return info_ptr;
6394 }
6395
6396 /* Fetch the abbreviation table offset from a comp or type unit header. */
6397
6398 static sect_offset
6399 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6400 struct dwarf2_section_info *section,
6401 sect_offset sect_off)
6402 {
6403 bfd *abfd = section->get_bfd_owner ();
6404 const gdb_byte *info_ptr;
6405 unsigned int initial_length_size, offset_size;
6406 uint16_t version;
6407
6408 section->read (dwarf2_per_objfile->objfile);
6409 info_ptr = section->buffer + to_underlying (sect_off);
6410 read_initial_length (abfd, info_ptr, &initial_length_size);
6411 offset_size = initial_length_size == 4 ? 4 : 8;
6412 info_ptr += initial_length_size;
6413
6414 version = read_2_bytes (abfd, info_ptr);
6415 info_ptr += 2;
6416 if (version >= 5)
6417 {
6418 /* Skip unit type and address size. */
6419 info_ptr += 2;
6420 }
6421
6422 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6423 }
6424
6425 /* Allocate a new partial symtab for file named NAME and mark this new
6426 partial symtab as being an include of PST. */
6427
6428 static void
6429 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6430 struct objfile *objfile)
6431 {
6432 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6433
6434 if (!IS_ABSOLUTE_PATH (subpst->filename))
6435 {
6436 /* It shares objfile->objfile_obstack. */
6437 subpst->dirname = pst->dirname;
6438 }
6439
6440 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6441 subpst->dependencies[0] = pst;
6442 subpst->number_of_dependencies = 1;
6443
6444 /* No private part is necessary for include psymtabs. This property
6445 can be used to differentiate between such include psymtabs and
6446 the regular ones. */
6447 subpst->per_cu_data = nullptr;
6448 }
6449
6450 /* Read the Line Number Program data and extract the list of files
6451 included by the source file represented by PST. Build an include
6452 partial symtab for each of these included files. */
6453
6454 static void
6455 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6456 struct die_info *die,
6457 dwarf2_psymtab *pst)
6458 {
6459 line_header_up lh;
6460 struct attribute *attr;
6461
6462 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6463 if (attr != nullptr)
6464 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6465 if (lh == NULL)
6466 return; /* No linetable, so no includes. */
6467
6468 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6469 that we pass in the raw text_low here; that is ok because we're
6470 only decoding the line table to make include partial symtabs, and
6471 so the addresses aren't really used. */
6472 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6473 pst->raw_text_low (), 1);
6474 }
6475
6476 static hashval_t
6477 hash_signatured_type (const void *item)
6478 {
6479 const struct signatured_type *sig_type
6480 = (const struct signatured_type *) item;
6481
6482 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6483 return sig_type->signature;
6484 }
6485
6486 static int
6487 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6488 {
6489 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6490 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6491
6492 return lhs->signature == rhs->signature;
6493 }
6494
6495 /* Allocate a hash table for signatured types. */
6496
6497 static htab_t
6498 allocate_signatured_type_table (struct objfile *objfile)
6499 {
6500 return htab_create_alloc_ex (41,
6501 hash_signatured_type,
6502 eq_signatured_type,
6503 NULL,
6504 &objfile->objfile_obstack,
6505 hashtab_obstack_allocate,
6506 dummy_obstack_deallocate);
6507 }
6508
6509 /* A helper function to add a signatured type CU to a table. */
6510
6511 static int
6512 add_signatured_type_cu_to_table (void **slot, void *datum)
6513 {
6514 struct signatured_type *sigt = (struct signatured_type *) *slot;
6515 std::vector<signatured_type *> *all_type_units
6516 = (std::vector<signatured_type *> *) datum;
6517
6518 all_type_units->push_back (sigt);
6519
6520 return 1;
6521 }
6522
6523 /* A helper for create_debug_types_hash_table. Read types from SECTION
6524 and fill them into TYPES_HTAB. It will process only type units,
6525 therefore DW_UT_type. */
6526
6527 static void
6528 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6529 struct dwo_file *dwo_file,
6530 dwarf2_section_info *section, htab_t &types_htab,
6531 rcuh_kind section_kind)
6532 {
6533 struct objfile *objfile = dwarf2_per_objfile->objfile;
6534 struct dwarf2_section_info *abbrev_section;
6535 bfd *abfd;
6536 const gdb_byte *info_ptr, *end_ptr;
6537
6538 abbrev_section = (dwo_file != NULL
6539 ? &dwo_file->sections.abbrev
6540 : &dwarf2_per_objfile->abbrev);
6541
6542 if (dwarf_read_debug)
6543 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6544 section->get_name (),
6545 abbrev_section->get_file_name ());
6546
6547 section->read (objfile);
6548 info_ptr = section->buffer;
6549
6550 if (info_ptr == NULL)
6551 return;
6552
6553 /* We can't set abfd until now because the section may be empty or
6554 not present, in which case the bfd is unknown. */
6555 abfd = section->get_bfd_owner ();
6556
6557 /* We don't use cutu_reader here because we don't need to read
6558 any dies: the signature is in the header. */
6559
6560 end_ptr = info_ptr + section->size;
6561 while (info_ptr < end_ptr)
6562 {
6563 struct signatured_type *sig_type;
6564 struct dwo_unit *dwo_tu;
6565 void **slot;
6566 const gdb_byte *ptr = info_ptr;
6567 struct comp_unit_head header;
6568 unsigned int length;
6569
6570 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6571
6572 /* Initialize it due to a false compiler warning. */
6573 header.signature = -1;
6574 header.type_cu_offset_in_tu = (cu_offset) -1;
6575
6576 /* We need to read the type's signature in order to build the hash
6577 table, but we don't need anything else just yet. */
6578
6579 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6580 abbrev_section, ptr, section_kind);
6581
6582 length = get_cu_length (&header);
6583
6584 /* Skip dummy type units. */
6585 if (ptr >= info_ptr + length
6586 || peek_abbrev_code (abfd, ptr) == 0
6587 || header.unit_type != DW_UT_type)
6588 {
6589 info_ptr += length;
6590 continue;
6591 }
6592
6593 if (types_htab == NULL)
6594 {
6595 if (dwo_file)
6596 types_htab = allocate_dwo_unit_table (objfile);
6597 else
6598 types_htab = allocate_signatured_type_table (objfile);
6599 }
6600
6601 if (dwo_file)
6602 {
6603 sig_type = NULL;
6604 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6605 struct dwo_unit);
6606 dwo_tu->dwo_file = dwo_file;
6607 dwo_tu->signature = header.signature;
6608 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6609 dwo_tu->section = section;
6610 dwo_tu->sect_off = sect_off;
6611 dwo_tu->length = length;
6612 }
6613 else
6614 {
6615 /* N.B.: type_offset is not usable if this type uses a DWO file.
6616 The real type_offset is in the DWO file. */
6617 dwo_tu = NULL;
6618 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6619 struct signatured_type);
6620 sig_type->signature = header.signature;
6621 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6622 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6623 sig_type->per_cu.is_debug_types = 1;
6624 sig_type->per_cu.section = section;
6625 sig_type->per_cu.sect_off = sect_off;
6626 sig_type->per_cu.length = length;
6627 }
6628
6629 slot = htab_find_slot (types_htab,
6630 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6631 INSERT);
6632 gdb_assert (slot != NULL);
6633 if (*slot != NULL)
6634 {
6635 sect_offset dup_sect_off;
6636
6637 if (dwo_file)
6638 {
6639 const struct dwo_unit *dup_tu
6640 = (const struct dwo_unit *) *slot;
6641
6642 dup_sect_off = dup_tu->sect_off;
6643 }
6644 else
6645 {
6646 const struct signatured_type *dup_tu
6647 = (const struct signatured_type *) *slot;
6648
6649 dup_sect_off = dup_tu->per_cu.sect_off;
6650 }
6651
6652 complaint (_("debug type entry at offset %s is duplicate to"
6653 " the entry at offset %s, signature %s"),
6654 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6655 hex_string (header.signature));
6656 }
6657 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6658
6659 if (dwarf_read_debug > 1)
6660 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6661 sect_offset_str (sect_off),
6662 hex_string (header.signature));
6663
6664 info_ptr += length;
6665 }
6666 }
6667
6668 /* Create the hash table of all entries in the .debug_types
6669 (or .debug_types.dwo) section(s).
6670 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6671 otherwise it is NULL.
6672
6673 The result is a pointer to the hash table or NULL if there are no types.
6674
6675 Note: This function processes DWO files only, not DWP files. */
6676
6677 static void
6678 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6679 struct dwo_file *dwo_file,
6680 gdb::array_view<dwarf2_section_info> type_sections,
6681 htab_t &types_htab)
6682 {
6683 for (dwarf2_section_info &section : type_sections)
6684 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6685 types_htab, rcuh_kind::TYPE);
6686 }
6687
6688 /* Create the hash table of all entries in the .debug_types section,
6689 and initialize all_type_units.
6690 The result is zero if there is an error (e.g. missing .debug_types section),
6691 otherwise non-zero. */
6692
6693 static int
6694 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6695 {
6696 htab_t types_htab = NULL;
6697
6698 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6699 &dwarf2_per_objfile->info, types_htab,
6700 rcuh_kind::COMPILE);
6701 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6702 dwarf2_per_objfile->types, types_htab);
6703 if (types_htab == NULL)
6704 {
6705 dwarf2_per_objfile->signatured_types = NULL;
6706 return 0;
6707 }
6708
6709 dwarf2_per_objfile->signatured_types = types_htab;
6710
6711 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6712 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6713
6714 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6715 &dwarf2_per_objfile->all_type_units);
6716
6717 return 1;
6718 }
6719
6720 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6721 If SLOT is non-NULL, it is the entry to use in the hash table.
6722 Otherwise we find one. */
6723
6724 static struct signatured_type *
6725 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6726 void **slot)
6727 {
6728 struct objfile *objfile = dwarf2_per_objfile->objfile;
6729
6730 if (dwarf2_per_objfile->all_type_units.size ()
6731 == dwarf2_per_objfile->all_type_units.capacity ())
6732 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6733
6734 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6735 struct signatured_type);
6736
6737 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6738 sig_type->signature = sig;
6739 sig_type->per_cu.is_debug_types = 1;
6740 if (dwarf2_per_objfile->using_index)
6741 {
6742 sig_type->per_cu.v.quick =
6743 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6744 struct dwarf2_per_cu_quick_data);
6745 }
6746
6747 if (slot == NULL)
6748 {
6749 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6750 sig_type, INSERT);
6751 }
6752 gdb_assert (*slot == NULL);
6753 *slot = sig_type;
6754 /* The rest of sig_type must be filled in by the caller. */
6755 return sig_type;
6756 }
6757
6758 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6759 Fill in SIG_ENTRY with DWO_ENTRY. */
6760
6761 static void
6762 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6763 struct signatured_type *sig_entry,
6764 struct dwo_unit *dwo_entry)
6765 {
6766 /* Make sure we're not clobbering something we don't expect to. */
6767 gdb_assert (! sig_entry->per_cu.queued);
6768 gdb_assert (sig_entry->per_cu.cu == NULL);
6769 if (dwarf2_per_objfile->using_index)
6770 {
6771 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6772 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6773 }
6774 else
6775 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6776 gdb_assert (sig_entry->signature == dwo_entry->signature);
6777 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6778 gdb_assert (sig_entry->type_unit_group == NULL);
6779 gdb_assert (sig_entry->dwo_unit == NULL);
6780
6781 sig_entry->per_cu.section = dwo_entry->section;
6782 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6783 sig_entry->per_cu.length = dwo_entry->length;
6784 sig_entry->per_cu.reading_dwo_directly = 1;
6785 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6786 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6787 sig_entry->dwo_unit = dwo_entry;
6788 }
6789
6790 /* Subroutine of lookup_signatured_type.
6791 If we haven't read the TU yet, create the signatured_type data structure
6792 for a TU to be read in directly from a DWO file, bypassing the stub.
6793 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6794 using .gdb_index, then when reading a CU we want to stay in the DWO file
6795 containing that CU. Otherwise we could end up reading several other DWO
6796 files (due to comdat folding) to process the transitive closure of all the
6797 mentioned TUs, and that can be slow. The current DWO file will have every
6798 type signature that it needs.
6799 We only do this for .gdb_index because in the psymtab case we already have
6800 to read all the DWOs to build the type unit groups. */
6801
6802 static struct signatured_type *
6803 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6804 {
6805 struct dwarf2_per_objfile *dwarf2_per_objfile
6806 = cu->per_cu->dwarf2_per_objfile;
6807 struct objfile *objfile = dwarf2_per_objfile->objfile;
6808 struct dwo_file *dwo_file;
6809 struct dwo_unit find_dwo_entry, *dwo_entry;
6810 struct signatured_type find_sig_entry, *sig_entry;
6811 void **slot;
6812
6813 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6814
6815 /* If TU skeletons have been removed then we may not have read in any
6816 TUs yet. */
6817 if (dwarf2_per_objfile->signatured_types == NULL)
6818 {
6819 dwarf2_per_objfile->signatured_types
6820 = allocate_signatured_type_table (objfile);
6821 }
6822
6823 /* We only ever need to read in one copy of a signatured type.
6824 Use the global signatured_types array to do our own comdat-folding
6825 of types. If this is the first time we're reading this TU, and
6826 the TU has an entry in .gdb_index, replace the recorded data from
6827 .gdb_index with this TU. */
6828
6829 find_sig_entry.signature = sig;
6830 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6831 &find_sig_entry, INSERT);
6832 sig_entry = (struct signatured_type *) *slot;
6833
6834 /* We can get here with the TU already read, *or* in the process of being
6835 read. Don't reassign the global entry to point to this DWO if that's
6836 the case. Also note that if the TU is already being read, it may not
6837 have come from a DWO, the program may be a mix of Fission-compiled
6838 code and non-Fission-compiled code. */
6839
6840 /* Have we already tried to read this TU?
6841 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6842 needn't exist in the global table yet). */
6843 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6844 return sig_entry;
6845
6846 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6847 dwo_unit of the TU itself. */
6848 dwo_file = cu->dwo_unit->dwo_file;
6849
6850 /* Ok, this is the first time we're reading this TU. */
6851 if (dwo_file->tus == NULL)
6852 return NULL;
6853 find_dwo_entry.signature = sig;
6854 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6855 if (dwo_entry == NULL)
6856 return NULL;
6857
6858 /* If the global table doesn't have an entry for this TU, add one. */
6859 if (sig_entry == NULL)
6860 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6861
6862 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6863 sig_entry->per_cu.tu_read = 1;
6864 return sig_entry;
6865 }
6866
6867 /* Subroutine of lookup_signatured_type.
6868 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6869 then try the DWP file. If the TU stub (skeleton) has been removed then
6870 it won't be in .gdb_index. */
6871
6872 static struct signatured_type *
6873 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6874 {
6875 struct dwarf2_per_objfile *dwarf2_per_objfile
6876 = cu->per_cu->dwarf2_per_objfile;
6877 struct objfile *objfile = dwarf2_per_objfile->objfile;
6878 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6879 struct dwo_unit *dwo_entry;
6880 struct signatured_type find_sig_entry, *sig_entry;
6881 void **slot;
6882
6883 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6884 gdb_assert (dwp_file != NULL);
6885
6886 /* If TU skeletons have been removed then we may not have read in any
6887 TUs yet. */
6888 if (dwarf2_per_objfile->signatured_types == NULL)
6889 {
6890 dwarf2_per_objfile->signatured_types
6891 = allocate_signatured_type_table (objfile);
6892 }
6893
6894 find_sig_entry.signature = sig;
6895 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6896 &find_sig_entry, INSERT);
6897 sig_entry = (struct signatured_type *) *slot;
6898
6899 /* Have we already tried to read this TU?
6900 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6901 needn't exist in the global table yet). */
6902 if (sig_entry != NULL)
6903 return sig_entry;
6904
6905 if (dwp_file->tus == NULL)
6906 return NULL;
6907 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6908 sig, 1 /* is_debug_types */);
6909 if (dwo_entry == NULL)
6910 return NULL;
6911
6912 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6913 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6914
6915 return sig_entry;
6916 }
6917
6918 /* Lookup a signature based type for DW_FORM_ref_sig8.
6919 Returns NULL if signature SIG is not present in the table.
6920 It is up to the caller to complain about this. */
6921
6922 static struct signatured_type *
6923 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6924 {
6925 struct dwarf2_per_objfile *dwarf2_per_objfile
6926 = cu->per_cu->dwarf2_per_objfile;
6927
6928 if (cu->dwo_unit
6929 && dwarf2_per_objfile->using_index)
6930 {
6931 /* We're in a DWO/DWP file, and we're using .gdb_index.
6932 These cases require special processing. */
6933 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6934 return lookup_dwo_signatured_type (cu, sig);
6935 else
6936 return lookup_dwp_signatured_type (cu, sig);
6937 }
6938 else
6939 {
6940 struct signatured_type find_entry, *entry;
6941
6942 if (dwarf2_per_objfile->signatured_types == NULL)
6943 return NULL;
6944 find_entry.signature = sig;
6945 entry = ((struct signatured_type *)
6946 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6947 return entry;
6948 }
6949 }
6950
6951 /* Return the address base of the compile unit, which, if exists, is stored
6952 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6953 static gdb::optional<ULONGEST>
6954 lookup_addr_base (struct die_info *comp_unit_die)
6955 {
6956 struct attribute *attr;
6957 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6958 if (attr == nullptr)
6959 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6960 if (attr == nullptr)
6961 return gdb::optional<ULONGEST> ();
6962 return DW_UNSND (attr);
6963 }
6964
6965 /* Return range lists base of the compile unit, which, if exists, is stored
6966 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6967 static ULONGEST
6968 lookup_ranges_base (struct die_info *comp_unit_die)
6969 {
6970 struct attribute *attr;
6971 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6972 if (attr == nullptr)
6973 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6974 if (attr == nullptr)
6975 return 0;
6976 return DW_UNSND (attr);
6977 }
6978
6979 /* Low level DIE reading support. */
6980
6981 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6982
6983 static void
6984 init_cu_die_reader (struct die_reader_specs *reader,
6985 struct dwarf2_cu *cu,
6986 struct dwarf2_section_info *section,
6987 struct dwo_file *dwo_file,
6988 struct abbrev_table *abbrev_table)
6989 {
6990 gdb_assert (section->readin && section->buffer != NULL);
6991 reader->abfd = section->get_bfd_owner ();
6992 reader->cu = cu;
6993 reader->dwo_file = dwo_file;
6994 reader->die_section = section;
6995 reader->buffer = section->buffer;
6996 reader->buffer_end = section->buffer + section->size;
6997 reader->comp_dir = NULL;
6998 reader->abbrev_table = abbrev_table;
6999 }
7000
7001 /* Subroutine of cutu_reader to simplify it.
7002 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7003 There's just a lot of work to do, and cutu_reader is big enough
7004 already.
7005
7006 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7007 from it to the DIE in the DWO. If NULL we are skipping the stub.
7008 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7009 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7010 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7011 STUB_COMP_DIR may be non-NULL.
7012 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7013 are filled in with the info of the DIE from the DWO file.
7014 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7015 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7016 kept around for at least as long as *RESULT_READER.
7017
7018 The result is non-zero if a valid (non-dummy) DIE was found. */
7019
7020 static int
7021 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7022 struct dwo_unit *dwo_unit,
7023 struct die_info *stub_comp_unit_die,
7024 const char *stub_comp_dir,
7025 struct die_reader_specs *result_reader,
7026 const gdb_byte **result_info_ptr,
7027 struct die_info **result_comp_unit_die,
7028 int *result_has_children,
7029 abbrev_table_up *result_dwo_abbrev_table)
7030 {
7031 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7032 struct objfile *objfile = dwarf2_per_objfile->objfile;
7033 struct dwarf2_cu *cu = this_cu->cu;
7034 bfd *abfd;
7035 const gdb_byte *begin_info_ptr, *info_ptr;
7036 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7037 int i,num_extra_attrs;
7038 struct dwarf2_section_info *dwo_abbrev_section;
7039 struct die_info *comp_unit_die;
7040
7041 /* At most one of these may be provided. */
7042 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7043
7044 /* These attributes aren't processed until later:
7045 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7046 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7047 referenced later. However, these attributes are found in the stub
7048 which we won't have later. In order to not impose this complication
7049 on the rest of the code, we read them here and copy them to the
7050 DWO CU/TU die. */
7051
7052 stmt_list = NULL;
7053 low_pc = NULL;
7054 high_pc = NULL;
7055 ranges = NULL;
7056 comp_dir = NULL;
7057
7058 if (stub_comp_unit_die != NULL)
7059 {
7060 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7061 DWO file. */
7062 if (! this_cu->is_debug_types)
7063 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7064 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7065 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7066 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7067 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7068
7069 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
7070
7071 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7072 here (if needed). We need the value before we can process
7073 DW_AT_ranges. */
7074 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
7075 }
7076 else if (stub_comp_dir != NULL)
7077 {
7078 /* Reconstruct the comp_dir attribute to simplify the code below. */
7079 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7080 comp_dir->name = DW_AT_comp_dir;
7081 comp_dir->form = DW_FORM_string;
7082 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7083 DW_STRING (comp_dir) = stub_comp_dir;
7084 }
7085
7086 /* Set up for reading the DWO CU/TU. */
7087 cu->dwo_unit = dwo_unit;
7088 dwarf2_section_info *section = dwo_unit->section;
7089 section->read (objfile);
7090 abfd = section->get_bfd_owner ();
7091 begin_info_ptr = info_ptr = (section->buffer
7092 + to_underlying (dwo_unit->sect_off));
7093 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7094
7095 if (this_cu->is_debug_types)
7096 {
7097 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7098
7099 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7100 &cu->header, section,
7101 dwo_abbrev_section,
7102 info_ptr, rcuh_kind::TYPE);
7103 /* This is not an assert because it can be caused by bad debug info. */
7104 if (sig_type->signature != cu->header.signature)
7105 {
7106 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7107 " TU at offset %s [in module %s]"),
7108 hex_string (sig_type->signature),
7109 hex_string (cu->header.signature),
7110 sect_offset_str (dwo_unit->sect_off),
7111 bfd_get_filename (abfd));
7112 }
7113 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7114 /* For DWOs coming from DWP files, we don't know the CU length
7115 nor the type's offset in the TU until now. */
7116 dwo_unit->length = get_cu_length (&cu->header);
7117 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7118
7119 /* Establish the type offset that can be used to lookup the type.
7120 For DWO files, we don't know it until now. */
7121 sig_type->type_offset_in_section
7122 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7123 }
7124 else
7125 {
7126 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7127 &cu->header, section,
7128 dwo_abbrev_section,
7129 info_ptr, rcuh_kind::COMPILE);
7130 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7131 /* For DWOs coming from DWP files, we don't know the CU length
7132 until now. */
7133 dwo_unit->length = get_cu_length (&cu->header);
7134 }
7135
7136 *result_dwo_abbrev_table
7137 = abbrev_table_read_table (objfile, dwo_abbrev_section,
7138 cu->header.abbrev_sect_off);
7139 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7140 result_dwo_abbrev_table->get ());
7141
7142 /* Read in the die, but leave space to copy over the attributes
7143 from the stub. This has the benefit of simplifying the rest of
7144 the code - all the work to maintain the illusion of a single
7145 DW_TAG_{compile,type}_unit DIE is done here. */
7146 num_extra_attrs = ((stmt_list != NULL)
7147 + (low_pc != NULL)
7148 + (high_pc != NULL)
7149 + (ranges != NULL)
7150 + (comp_dir != NULL));
7151 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7152 result_has_children, num_extra_attrs);
7153
7154 /* Copy over the attributes from the stub to the DIE we just read in. */
7155 comp_unit_die = *result_comp_unit_die;
7156 i = comp_unit_die->num_attrs;
7157 if (stmt_list != NULL)
7158 comp_unit_die->attrs[i++] = *stmt_list;
7159 if (low_pc != NULL)
7160 comp_unit_die->attrs[i++] = *low_pc;
7161 if (high_pc != NULL)
7162 comp_unit_die->attrs[i++] = *high_pc;
7163 if (ranges != NULL)
7164 comp_unit_die->attrs[i++] = *ranges;
7165 if (comp_dir != NULL)
7166 comp_unit_die->attrs[i++] = *comp_dir;
7167 comp_unit_die->num_attrs += num_extra_attrs;
7168
7169 if (dwarf_die_debug)
7170 {
7171 fprintf_unfiltered (gdb_stdlog,
7172 "Read die from %s@0x%x of %s:\n",
7173 section->get_name (),
7174 (unsigned) (begin_info_ptr - section->buffer),
7175 bfd_get_filename (abfd));
7176 dump_die (comp_unit_die, dwarf_die_debug);
7177 }
7178
7179 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7180 TUs by skipping the stub and going directly to the entry in the DWO file.
7181 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7182 to get it via circuitous means. Blech. */
7183 if (comp_dir != NULL)
7184 result_reader->comp_dir = DW_STRING (comp_dir);
7185
7186 /* Skip dummy compilation units. */
7187 if (info_ptr >= begin_info_ptr + dwo_unit->length
7188 || peek_abbrev_code (abfd, info_ptr) == 0)
7189 return 0;
7190
7191 *result_info_ptr = info_ptr;
7192 return 1;
7193 }
7194
7195 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7196 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7197 signature is part of the header. */
7198 static gdb::optional<ULONGEST>
7199 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7200 {
7201 if (cu->header.version >= 5)
7202 return cu->header.signature;
7203 struct attribute *attr;
7204 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7205 if (attr == nullptr)
7206 return gdb::optional<ULONGEST> ();
7207 return DW_UNSND (attr);
7208 }
7209
7210 /* Subroutine of cutu_reader to simplify it.
7211 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7212 Returns NULL if the specified DWO unit cannot be found. */
7213
7214 static struct dwo_unit *
7215 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7216 struct die_info *comp_unit_die,
7217 const char *dwo_name)
7218 {
7219 struct dwarf2_cu *cu = this_cu->cu;
7220 struct dwo_unit *dwo_unit;
7221 const char *comp_dir;
7222
7223 gdb_assert (cu != NULL);
7224
7225 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7226 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7227 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7228
7229 if (this_cu->is_debug_types)
7230 {
7231 struct signatured_type *sig_type;
7232
7233 /* Since this_cu is the first member of struct signatured_type,
7234 we can go from a pointer to one to a pointer to the other. */
7235 sig_type = (struct signatured_type *) this_cu;
7236 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7237 }
7238 else
7239 {
7240 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7241 if (!signature.has_value ())
7242 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7243 " [in module %s]"),
7244 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7245 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7246 *signature);
7247 }
7248
7249 return dwo_unit;
7250 }
7251
7252 /* Subroutine of cutu_reader to simplify it.
7253 See it for a description of the parameters.
7254 Read a TU directly from a DWO file, bypassing the stub. */
7255
7256 void
7257 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7258 int use_existing_cu, int keep)
7259 {
7260 struct signatured_type *sig_type;
7261 struct die_reader_specs reader;
7262
7263 /* Verify we can do the following downcast, and that we have the
7264 data we need. */
7265 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7266 sig_type = (struct signatured_type *) this_cu;
7267 gdb_assert (sig_type->dwo_unit != NULL);
7268
7269 if (use_existing_cu && this_cu->cu != NULL)
7270 {
7271 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7272 /* There's no need to do the rereading_dwo_cu handling that
7273 cutu_reader does since we don't read the stub. */
7274 }
7275 else
7276 {
7277 /* If !use_existing_cu, this_cu->cu must be NULL. */
7278 gdb_assert (this_cu->cu == NULL);
7279 m_new_cu.reset (new dwarf2_cu (this_cu));
7280 }
7281
7282 /* A future optimization, if needed, would be to use an existing
7283 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7284 could share abbrev tables. */
7285
7286 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7287 NULL /* stub_comp_unit_die */,
7288 sig_type->dwo_unit->dwo_file->comp_dir,
7289 &reader, &info_ptr,
7290 &comp_unit_die, &has_children,
7291 &m_dwo_abbrev_table) == 0)
7292 {
7293 /* Dummy die. */
7294 dummy_p = true;
7295 }
7296 }
7297
7298 /* Initialize a CU (or TU) and read its DIEs.
7299 If the CU defers to a DWO file, read the DWO file as well.
7300
7301 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7302 Otherwise the table specified in the comp unit header is read in and used.
7303 This is an optimization for when we already have the abbrev table.
7304
7305 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7306 Otherwise, a new CU is allocated with xmalloc.
7307
7308 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7309 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7310 end. */
7311
7312 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7313 struct abbrev_table *abbrev_table,
7314 int use_existing_cu, int keep,
7315 bool skip_partial)
7316 : die_reader_specs {},
7317 m_this_cu (this_cu),
7318 m_keep (keep)
7319 {
7320 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7321 struct objfile *objfile = dwarf2_per_objfile->objfile;
7322 struct dwarf2_section_info *section = this_cu->section;
7323 bfd *abfd = section->get_bfd_owner ();
7324 struct dwarf2_cu *cu;
7325 const gdb_byte *begin_info_ptr;
7326 struct signatured_type *sig_type = NULL;
7327 struct dwarf2_section_info *abbrev_section;
7328 /* Non-zero if CU currently points to a DWO file and we need to
7329 reread it. When this happens we need to reread the skeleton die
7330 before we can reread the DWO file (this only applies to CUs, not TUs). */
7331 int rereading_dwo_cu = 0;
7332
7333 if (dwarf_die_debug)
7334 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7335 this_cu->is_debug_types ? "type" : "comp",
7336 sect_offset_str (this_cu->sect_off));
7337
7338 if (use_existing_cu)
7339 gdb_assert (keep);
7340
7341 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7342 file (instead of going through the stub), short-circuit all of this. */
7343 if (this_cu->reading_dwo_directly)
7344 {
7345 /* Narrow down the scope of possibilities to have to understand. */
7346 gdb_assert (this_cu->is_debug_types);
7347 gdb_assert (abbrev_table == NULL);
7348 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7349 return;
7350 }
7351
7352 /* This is cheap if the section is already read in. */
7353 section->read (objfile);
7354
7355 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7356
7357 abbrev_section = get_abbrev_section_for_cu (this_cu);
7358
7359 if (use_existing_cu && this_cu->cu != NULL)
7360 {
7361 cu = this_cu->cu;
7362 /* If this CU is from a DWO file we need to start over, we need to
7363 refetch the attributes from the skeleton CU.
7364 This could be optimized by retrieving those attributes from when we
7365 were here the first time: the previous comp_unit_die was stored in
7366 comp_unit_obstack. But there's no data yet that we need this
7367 optimization. */
7368 if (cu->dwo_unit != NULL)
7369 rereading_dwo_cu = 1;
7370 }
7371 else
7372 {
7373 /* If !use_existing_cu, this_cu->cu must be NULL. */
7374 gdb_assert (this_cu->cu == NULL);
7375 m_new_cu.reset (new dwarf2_cu (this_cu));
7376 cu = m_new_cu.get ();
7377 }
7378
7379 /* Get the header. */
7380 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7381 {
7382 /* We already have the header, there's no need to read it in again. */
7383 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7384 }
7385 else
7386 {
7387 if (this_cu->is_debug_types)
7388 {
7389 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7390 &cu->header, section,
7391 abbrev_section, info_ptr,
7392 rcuh_kind::TYPE);
7393
7394 /* Since per_cu is the first member of struct signatured_type,
7395 we can go from a pointer to one to a pointer to the other. */
7396 sig_type = (struct signatured_type *) this_cu;
7397 gdb_assert (sig_type->signature == cu->header.signature);
7398 gdb_assert (sig_type->type_offset_in_tu
7399 == cu->header.type_cu_offset_in_tu);
7400 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7401
7402 /* LENGTH has not been set yet for type units if we're
7403 using .gdb_index. */
7404 this_cu->length = get_cu_length (&cu->header);
7405
7406 /* Establish the type offset that can be used to lookup the type. */
7407 sig_type->type_offset_in_section =
7408 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7409
7410 this_cu->dwarf_version = cu->header.version;
7411 }
7412 else
7413 {
7414 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7415 &cu->header, section,
7416 abbrev_section,
7417 info_ptr,
7418 rcuh_kind::COMPILE);
7419
7420 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7421 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7422 this_cu->dwarf_version = cu->header.version;
7423 }
7424 }
7425
7426 /* Skip dummy compilation units. */
7427 if (info_ptr >= begin_info_ptr + this_cu->length
7428 || peek_abbrev_code (abfd, info_ptr) == 0)
7429 {
7430 dummy_p = true;
7431 return;
7432 }
7433
7434 /* If we don't have them yet, read the abbrevs for this compilation unit.
7435 And if we need to read them now, make sure they're freed when we're
7436 done. */
7437 if (abbrev_table != NULL)
7438 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7439 else
7440 {
7441 m_abbrev_table_holder
7442 = abbrev_table_read_table (objfile, abbrev_section,
7443 cu->header.abbrev_sect_off);
7444 abbrev_table = m_abbrev_table_holder.get ();
7445 }
7446
7447 /* Read the top level CU/TU die. */
7448 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7449 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7450
7451 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7452 {
7453 dummy_p = true;
7454 return;
7455 }
7456
7457 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7458 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7459 table from the DWO file and pass the ownership over to us. It will be
7460 referenced from READER, so we must make sure to free it after we're done
7461 with READER.
7462
7463 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7464 DWO CU, that this test will fail (the attribute will not be present). */
7465 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7466 if (dwo_name != nullptr)
7467 {
7468 struct dwo_unit *dwo_unit;
7469 struct die_info *dwo_comp_unit_die;
7470
7471 if (has_children)
7472 {
7473 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7474 " has children (offset %s) [in module %s]"),
7475 sect_offset_str (this_cu->sect_off),
7476 bfd_get_filename (abfd));
7477 }
7478 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7479 if (dwo_unit != NULL)
7480 {
7481 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7482 comp_unit_die, NULL,
7483 this, &info_ptr,
7484 &dwo_comp_unit_die, &has_children,
7485 &m_dwo_abbrev_table) == 0)
7486 {
7487 /* Dummy die. */
7488 dummy_p = true;
7489 return;
7490 }
7491 comp_unit_die = dwo_comp_unit_die;
7492 }
7493 else
7494 {
7495 /* Yikes, we couldn't find the rest of the DIE, we only have
7496 the stub. A complaint has already been logged. There's
7497 not much more we can do except pass on the stub DIE to
7498 die_reader_func. We don't want to throw an error on bad
7499 debug info. */
7500 }
7501 }
7502 }
7503
7504 cutu_reader::~cutu_reader ()
7505 {
7506 /* Done, clean up. */
7507 if (m_new_cu != NULL && m_keep && !dummy_p)
7508 {
7509 struct dwarf2_per_objfile *dwarf2_per_objfile
7510 = m_this_cu->dwarf2_per_objfile;
7511 /* Link this CU into read_in_chain. */
7512 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7513 dwarf2_per_objfile->read_in_chain = m_this_cu;
7514 /* The chain owns it now. */
7515 m_new_cu.release ();
7516 }
7517 }
7518
7519 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7520 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7521 assumed to have already done the lookup to find the DWO file).
7522
7523 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7524 THIS_CU->is_debug_types, but nothing else.
7525
7526 We fill in THIS_CU->length.
7527
7528 THIS_CU->cu is always freed when done.
7529 This is done in order to not leave THIS_CU->cu in a state where we have
7530 to care whether it refers to the "main" CU or the DWO CU.
7531
7532 When parent_cu is passed, it is used to provide a default value for
7533 str_offsets_base and addr_base from the parent. */
7534
7535 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7536 struct dwarf2_cu *parent_cu,
7537 struct dwo_file *dwo_file)
7538 : die_reader_specs {},
7539 m_this_cu (this_cu)
7540 {
7541 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7542 struct objfile *objfile = dwarf2_per_objfile->objfile;
7543 struct dwarf2_section_info *section = this_cu->section;
7544 bfd *abfd = section->get_bfd_owner ();
7545 struct dwarf2_section_info *abbrev_section;
7546 const gdb_byte *begin_info_ptr, *info_ptr;
7547 int has_children;
7548
7549 if (dwarf_die_debug)
7550 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7551 this_cu->is_debug_types ? "type" : "comp",
7552 sect_offset_str (this_cu->sect_off));
7553
7554 gdb_assert (this_cu->cu == NULL);
7555
7556 abbrev_section = (dwo_file != NULL
7557 ? &dwo_file->sections.abbrev
7558 : get_abbrev_section_for_cu (this_cu));
7559
7560 /* This is cheap if the section is already read in. */
7561 section->read (objfile);
7562
7563 m_new_cu.reset (new dwarf2_cu (this_cu));
7564
7565 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7566 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7567 &m_new_cu->header, section,
7568 abbrev_section, info_ptr,
7569 (this_cu->is_debug_types
7570 ? rcuh_kind::TYPE
7571 : rcuh_kind::COMPILE));
7572
7573 if (parent_cu != nullptr)
7574 {
7575 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7576 m_new_cu->addr_base = parent_cu->addr_base;
7577 }
7578 this_cu->length = get_cu_length (&m_new_cu->header);
7579
7580 /* Skip dummy compilation units. */
7581 if (info_ptr >= begin_info_ptr + this_cu->length
7582 || peek_abbrev_code (abfd, info_ptr) == 0)
7583 {
7584 dummy_p = true;
7585 return;
7586 }
7587
7588 m_abbrev_table_holder
7589 = abbrev_table_read_table (objfile, abbrev_section,
7590 m_new_cu->header.abbrev_sect_off);
7591
7592 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7593 m_abbrev_table_holder.get ());
7594 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7595 }
7596
7597 \f
7598 /* Type Unit Groups.
7599
7600 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7601 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7602 so that all types coming from the same compilation (.o file) are grouped
7603 together. A future step could be to put the types in the same symtab as
7604 the CU the types ultimately came from. */
7605
7606 static hashval_t
7607 hash_type_unit_group (const void *item)
7608 {
7609 const struct type_unit_group *tu_group
7610 = (const struct type_unit_group *) item;
7611
7612 return hash_stmt_list_entry (&tu_group->hash);
7613 }
7614
7615 static int
7616 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7617 {
7618 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7619 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7620
7621 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7622 }
7623
7624 /* Allocate a hash table for type unit groups. */
7625
7626 static htab_t
7627 allocate_type_unit_groups_table (struct objfile *objfile)
7628 {
7629 return htab_create_alloc_ex (3,
7630 hash_type_unit_group,
7631 eq_type_unit_group,
7632 NULL,
7633 &objfile->objfile_obstack,
7634 hashtab_obstack_allocate,
7635 dummy_obstack_deallocate);
7636 }
7637
7638 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7639 partial symtabs. We combine several TUs per psymtab to not let the size
7640 of any one psymtab grow too big. */
7641 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7642 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7643
7644 /* Helper routine for get_type_unit_group.
7645 Create the type_unit_group object used to hold one or more TUs. */
7646
7647 static struct type_unit_group *
7648 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7649 {
7650 struct dwarf2_per_objfile *dwarf2_per_objfile
7651 = cu->per_cu->dwarf2_per_objfile;
7652 struct objfile *objfile = dwarf2_per_objfile->objfile;
7653 struct dwarf2_per_cu_data *per_cu;
7654 struct type_unit_group *tu_group;
7655
7656 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7657 struct type_unit_group);
7658 per_cu = &tu_group->per_cu;
7659 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7660
7661 if (dwarf2_per_objfile->using_index)
7662 {
7663 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7664 struct dwarf2_per_cu_quick_data);
7665 }
7666 else
7667 {
7668 unsigned int line_offset = to_underlying (line_offset_struct);
7669 dwarf2_psymtab *pst;
7670 std::string name;
7671
7672 /* Give the symtab a useful name for debug purposes. */
7673 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7674 name = string_printf ("<type_units_%d>",
7675 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7676 else
7677 name = string_printf ("<type_units_at_0x%x>", line_offset);
7678
7679 pst = create_partial_symtab (per_cu, name.c_str ());
7680 pst->anonymous = true;
7681 }
7682
7683 tu_group->hash.dwo_unit = cu->dwo_unit;
7684 tu_group->hash.line_sect_off = line_offset_struct;
7685
7686 return tu_group;
7687 }
7688
7689 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7690 STMT_LIST is a DW_AT_stmt_list attribute. */
7691
7692 static struct type_unit_group *
7693 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7694 {
7695 struct dwarf2_per_objfile *dwarf2_per_objfile
7696 = cu->per_cu->dwarf2_per_objfile;
7697 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7698 struct type_unit_group *tu_group;
7699 void **slot;
7700 unsigned int line_offset;
7701 struct type_unit_group type_unit_group_for_lookup;
7702
7703 if (dwarf2_per_objfile->type_unit_groups == NULL)
7704 {
7705 dwarf2_per_objfile->type_unit_groups =
7706 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7707 }
7708
7709 /* Do we need to create a new group, or can we use an existing one? */
7710
7711 if (stmt_list)
7712 {
7713 line_offset = DW_UNSND (stmt_list);
7714 ++tu_stats->nr_symtab_sharers;
7715 }
7716 else
7717 {
7718 /* Ugh, no stmt_list. Rare, but we have to handle it.
7719 We can do various things here like create one group per TU or
7720 spread them over multiple groups to split up the expansion work.
7721 To avoid worst case scenarios (too many groups or too large groups)
7722 we, umm, group them in bunches. */
7723 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7724 | (tu_stats->nr_stmt_less_type_units
7725 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7726 ++tu_stats->nr_stmt_less_type_units;
7727 }
7728
7729 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7730 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7731 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7732 &type_unit_group_for_lookup, INSERT);
7733 if (*slot != NULL)
7734 {
7735 tu_group = (struct type_unit_group *) *slot;
7736 gdb_assert (tu_group != NULL);
7737 }
7738 else
7739 {
7740 sect_offset line_offset_struct = (sect_offset) line_offset;
7741 tu_group = create_type_unit_group (cu, line_offset_struct);
7742 *slot = tu_group;
7743 ++tu_stats->nr_symtabs;
7744 }
7745
7746 return tu_group;
7747 }
7748 \f
7749 /* Partial symbol tables. */
7750
7751 /* Create a psymtab named NAME and assign it to PER_CU.
7752
7753 The caller must fill in the following details:
7754 dirname, textlow, texthigh. */
7755
7756 static dwarf2_psymtab *
7757 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7758 {
7759 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7760 dwarf2_psymtab *pst;
7761
7762 pst = new dwarf2_psymtab (name, objfile, 0);
7763
7764 pst->psymtabs_addrmap_supported = true;
7765
7766 /* This is the glue that links PST into GDB's symbol API. */
7767 pst->per_cu_data = per_cu;
7768 per_cu->v.psymtab = pst;
7769
7770 return pst;
7771 }
7772
7773 /* DIE reader function for process_psymtab_comp_unit. */
7774
7775 static void
7776 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7777 const gdb_byte *info_ptr,
7778 struct die_info *comp_unit_die,
7779 int has_children,
7780 int want_partial_unit,
7781 enum language pretend_language)
7782 {
7783 struct dwarf2_cu *cu = reader->cu;
7784 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7786 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7787 CORE_ADDR baseaddr;
7788 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7789 dwarf2_psymtab *pst;
7790 enum pc_bounds_kind cu_bounds_kind;
7791 const char *filename;
7792
7793 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7794 return;
7795
7796 gdb_assert (! per_cu->is_debug_types);
7797
7798 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7799
7800 /* Allocate a new partial symbol table structure. */
7801 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7802 if (filename == NULL)
7803 filename = "";
7804
7805 pst = create_partial_symtab (per_cu, filename);
7806
7807 /* This must be done before calling dwarf2_build_include_psymtabs. */
7808 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7809
7810 baseaddr = objfile->text_section_offset ();
7811
7812 dwarf2_find_base_address (comp_unit_die, cu);
7813
7814 /* Possibly set the default values of LOWPC and HIGHPC from
7815 `DW_AT_ranges'. */
7816 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7817 &best_highpc, cu, pst);
7818 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7819 {
7820 CORE_ADDR low
7821 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7822 - baseaddr);
7823 CORE_ADDR high
7824 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7825 - baseaddr - 1);
7826 /* Store the contiguous range if it is not empty; it can be
7827 empty for CUs with no code. */
7828 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7829 low, high, pst);
7830 }
7831
7832 /* Check if comp unit has_children.
7833 If so, read the rest of the partial symbols from this comp unit.
7834 If not, there's no more debug_info for this comp unit. */
7835 if (has_children)
7836 {
7837 struct partial_die_info *first_die;
7838 CORE_ADDR lowpc, highpc;
7839
7840 lowpc = ((CORE_ADDR) -1);
7841 highpc = ((CORE_ADDR) 0);
7842
7843 first_die = load_partial_dies (reader, info_ptr, 1);
7844
7845 scan_partial_symbols (first_die, &lowpc, &highpc,
7846 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7847
7848 /* If we didn't find a lowpc, set it to highpc to avoid
7849 complaints from `maint check'. */
7850 if (lowpc == ((CORE_ADDR) -1))
7851 lowpc = highpc;
7852
7853 /* If the compilation unit didn't have an explicit address range,
7854 then use the information extracted from its child dies. */
7855 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7856 {
7857 best_lowpc = lowpc;
7858 best_highpc = highpc;
7859 }
7860 }
7861 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7862 best_lowpc + baseaddr)
7863 - baseaddr);
7864 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7865 best_highpc + baseaddr)
7866 - baseaddr);
7867
7868 end_psymtab_common (objfile, pst);
7869
7870 if (!cu->per_cu->imported_symtabs_empty ())
7871 {
7872 int i;
7873 int len = cu->per_cu->imported_symtabs_size ();
7874
7875 /* Fill in 'dependencies' here; we fill in 'users' in a
7876 post-pass. */
7877 pst->number_of_dependencies = len;
7878 pst->dependencies
7879 = objfile->partial_symtabs->allocate_dependencies (len);
7880 for (i = 0; i < len; ++i)
7881 {
7882 pst->dependencies[i]
7883 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7884 }
7885
7886 cu->per_cu->imported_symtabs_free ();
7887 }
7888
7889 /* Get the list of files included in the current compilation unit,
7890 and build a psymtab for each of them. */
7891 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7892
7893 if (dwarf_read_debug)
7894 fprintf_unfiltered (gdb_stdlog,
7895 "Psymtab for %s unit @%s: %s - %s"
7896 ", %d global, %d static syms\n",
7897 per_cu->is_debug_types ? "type" : "comp",
7898 sect_offset_str (per_cu->sect_off),
7899 paddress (gdbarch, pst->text_low (objfile)),
7900 paddress (gdbarch, pst->text_high (objfile)),
7901 pst->n_global_syms, pst->n_static_syms);
7902 }
7903
7904 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7905 Process compilation unit THIS_CU for a psymtab. */
7906
7907 static void
7908 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7909 int want_partial_unit,
7910 enum language pretend_language)
7911 {
7912 /* If this compilation unit was already read in, free the
7913 cached copy in order to read it in again. This is
7914 necessary because we skipped some symbols when we first
7915 read in the compilation unit (see load_partial_dies).
7916 This problem could be avoided, but the benefit is unclear. */
7917 if (this_cu->cu != NULL)
7918 free_one_cached_comp_unit (this_cu);
7919
7920 cutu_reader reader (this_cu, NULL, 0, 0, false);
7921
7922 if (reader.dummy_p)
7923 {
7924 /* Nothing. */
7925 }
7926 else if (this_cu->is_debug_types)
7927 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7928 reader.has_children);
7929 else
7930 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7931 reader.comp_unit_die,
7932 reader.has_children,
7933 want_partial_unit,
7934 pretend_language);
7935
7936 /* Age out any secondary CUs. */
7937 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7938 }
7939
7940 /* Reader function for build_type_psymtabs. */
7941
7942 static void
7943 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7944 const gdb_byte *info_ptr,
7945 struct die_info *type_unit_die,
7946 int has_children)
7947 {
7948 struct dwarf2_per_objfile *dwarf2_per_objfile
7949 = reader->cu->per_cu->dwarf2_per_objfile;
7950 struct objfile *objfile = dwarf2_per_objfile->objfile;
7951 struct dwarf2_cu *cu = reader->cu;
7952 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7953 struct signatured_type *sig_type;
7954 struct type_unit_group *tu_group;
7955 struct attribute *attr;
7956 struct partial_die_info *first_die;
7957 CORE_ADDR lowpc, highpc;
7958 dwarf2_psymtab *pst;
7959
7960 gdb_assert (per_cu->is_debug_types);
7961 sig_type = (struct signatured_type *) per_cu;
7962
7963 if (! has_children)
7964 return;
7965
7966 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7967 tu_group = get_type_unit_group (cu, attr);
7968
7969 if (tu_group->tus == nullptr)
7970 tu_group->tus = new std::vector<signatured_type *>;
7971 tu_group->tus->push_back (sig_type);
7972
7973 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7974 pst = create_partial_symtab (per_cu, "");
7975 pst->anonymous = true;
7976
7977 first_die = load_partial_dies (reader, info_ptr, 1);
7978
7979 lowpc = (CORE_ADDR) -1;
7980 highpc = (CORE_ADDR) 0;
7981 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7982
7983 end_psymtab_common (objfile, pst);
7984 }
7985
7986 /* Struct used to sort TUs by their abbreviation table offset. */
7987
7988 struct tu_abbrev_offset
7989 {
7990 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7991 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7992 {}
7993
7994 signatured_type *sig_type;
7995 sect_offset abbrev_offset;
7996 };
7997
7998 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7999
8000 static bool
8001 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8002 const struct tu_abbrev_offset &b)
8003 {
8004 return a.abbrev_offset < b.abbrev_offset;
8005 }
8006
8007 /* Efficiently read all the type units.
8008 This does the bulk of the work for build_type_psymtabs.
8009
8010 The efficiency is because we sort TUs by the abbrev table they use and
8011 only read each abbrev table once. In one program there are 200K TUs
8012 sharing 8K abbrev tables.
8013
8014 The main purpose of this function is to support building the
8015 dwarf2_per_objfile->type_unit_groups table.
8016 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8017 can collapse the search space by grouping them by stmt_list.
8018 The savings can be significant, in the same program from above the 200K TUs
8019 share 8K stmt_list tables.
8020
8021 FUNC is expected to call get_type_unit_group, which will create the
8022 struct type_unit_group if necessary and add it to
8023 dwarf2_per_objfile->type_unit_groups. */
8024
8025 static void
8026 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8027 {
8028 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8029 abbrev_table_up abbrev_table;
8030 sect_offset abbrev_offset;
8031
8032 /* It's up to the caller to not call us multiple times. */
8033 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8034
8035 if (dwarf2_per_objfile->all_type_units.empty ())
8036 return;
8037
8038 /* TUs typically share abbrev tables, and there can be way more TUs than
8039 abbrev tables. Sort by abbrev table to reduce the number of times we
8040 read each abbrev table in.
8041 Alternatives are to punt or to maintain a cache of abbrev tables.
8042 This is simpler and efficient enough for now.
8043
8044 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8045 symtab to use). Typically TUs with the same abbrev offset have the same
8046 stmt_list value too so in practice this should work well.
8047
8048 The basic algorithm here is:
8049
8050 sort TUs by abbrev table
8051 for each TU with same abbrev table:
8052 read abbrev table if first user
8053 read TU top level DIE
8054 [IWBN if DWO skeletons had DW_AT_stmt_list]
8055 call FUNC */
8056
8057 if (dwarf_read_debug)
8058 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8059
8060 /* Sort in a separate table to maintain the order of all_type_units
8061 for .gdb_index: TU indices directly index all_type_units. */
8062 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8063 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8064
8065 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8066 sorted_by_abbrev.emplace_back
8067 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8068 sig_type->per_cu.section,
8069 sig_type->per_cu.sect_off));
8070
8071 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8072 sort_tu_by_abbrev_offset);
8073
8074 abbrev_offset = (sect_offset) ~(unsigned) 0;
8075
8076 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8077 {
8078 /* Switch to the next abbrev table if necessary. */
8079 if (abbrev_table == NULL
8080 || tu.abbrev_offset != abbrev_offset)
8081 {
8082 abbrev_offset = tu.abbrev_offset;
8083 abbrev_table =
8084 abbrev_table_read_table (dwarf2_per_objfile->objfile,
8085 &dwarf2_per_objfile->abbrev,
8086 abbrev_offset);
8087 ++tu_stats->nr_uniq_abbrev_tables;
8088 }
8089
8090 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8091 0, 0, false);
8092 if (!reader.dummy_p)
8093 build_type_psymtabs_reader (&reader, reader.info_ptr,
8094 reader.comp_unit_die,
8095 reader.has_children);
8096 }
8097 }
8098
8099 /* Print collected type unit statistics. */
8100
8101 static void
8102 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8103 {
8104 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8105
8106 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8107 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8108 dwarf2_per_objfile->all_type_units.size ());
8109 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8110 tu_stats->nr_uniq_abbrev_tables);
8111 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8112 tu_stats->nr_symtabs);
8113 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8114 tu_stats->nr_symtab_sharers);
8115 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8116 tu_stats->nr_stmt_less_type_units);
8117 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8118 tu_stats->nr_all_type_units_reallocs);
8119 }
8120
8121 /* Traversal function for build_type_psymtabs. */
8122
8123 static int
8124 build_type_psymtab_dependencies (void **slot, void *info)
8125 {
8126 struct dwarf2_per_objfile *dwarf2_per_objfile
8127 = (struct dwarf2_per_objfile *) info;
8128 struct objfile *objfile = dwarf2_per_objfile->objfile;
8129 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8130 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8131 dwarf2_psymtab *pst = per_cu->v.psymtab;
8132 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8133 int i;
8134
8135 gdb_assert (len > 0);
8136 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8137
8138 pst->number_of_dependencies = len;
8139 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8140 for (i = 0; i < len; ++i)
8141 {
8142 struct signatured_type *iter = tu_group->tus->at (i);
8143 gdb_assert (iter->per_cu.is_debug_types);
8144 pst->dependencies[i] = iter->per_cu.v.psymtab;
8145 iter->type_unit_group = tu_group;
8146 }
8147
8148 delete tu_group->tus;
8149 tu_group->tus = nullptr;
8150
8151 return 1;
8152 }
8153
8154 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8155 Build partial symbol tables for the .debug_types comp-units. */
8156
8157 static void
8158 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8159 {
8160 if (! create_all_type_units (dwarf2_per_objfile))
8161 return;
8162
8163 build_type_psymtabs_1 (dwarf2_per_objfile);
8164 }
8165
8166 /* Traversal function for process_skeletonless_type_unit.
8167 Read a TU in a DWO file and build partial symbols for it. */
8168
8169 static int
8170 process_skeletonless_type_unit (void **slot, void *info)
8171 {
8172 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8173 struct dwarf2_per_objfile *dwarf2_per_objfile
8174 = (struct dwarf2_per_objfile *) info;
8175 struct signatured_type find_entry, *entry;
8176
8177 /* If this TU doesn't exist in the global table, add it and read it in. */
8178
8179 if (dwarf2_per_objfile->signatured_types == NULL)
8180 {
8181 dwarf2_per_objfile->signatured_types
8182 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8183 }
8184
8185 find_entry.signature = dwo_unit->signature;
8186 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8187 INSERT);
8188 /* If we've already seen this type there's nothing to do. What's happening
8189 is we're doing our own version of comdat-folding here. */
8190 if (*slot != NULL)
8191 return 1;
8192
8193 /* This does the job that create_all_type_units would have done for
8194 this TU. */
8195 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8196 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8197 *slot = entry;
8198
8199 /* This does the job that build_type_psymtabs_1 would have done. */
8200 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8201 if (!reader.dummy_p)
8202 build_type_psymtabs_reader (&reader, reader.info_ptr,
8203 reader.comp_unit_die, reader.has_children);
8204
8205 return 1;
8206 }
8207
8208 /* Traversal function for process_skeletonless_type_units. */
8209
8210 static int
8211 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8212 {
8213 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8214
8215 if (dwo_file->tus != NULL)
8216 {
8217 htab_traverse_noresize (dwo_file->tus,
8218 process_skeletonless_type_unit, info);
8219 }
8220
8221 return 1;
8222 }
8223
8224 /* Scan all TUs of DWO files, verifying we've processed them.
8225 This is needed in case a TU was emitted without its skeleton.
8226 Note: This can't be done until we know what all the DWO files are. */
8227
8228 static void
8229 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8230 {
8231 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8232 if (get_dwp_file (dwarf2_per_objfile) == NULL
8233 && dwarf2_per_objfile->dwo_files != NULL)
8234 {
8235 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8236 process_dwo_file_for_skeletonless_type_units,
8237 dwarf2_per_objfile);
8238 }
8239 }
8240
8241 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8242
8243 static void
8244 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8245 {
8246 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8247 {
8248 dwarf2_psymtab *pst = per_cu->v.psymtab;
8249
8250 if (pst == NULL)
8251 continue;
8252
8253 for (int j = 0; j < pst->number_of_dependencies; ++j)
8254 {
8255 /* Set the 'user' field only if it is not already set. */
8256 if (pst->dependencies[j]->user == NULL)
8257 pst->dependencies[j]->user = pst;
8258 }
8259 }
8260 }
8261
8262 /* Build the partial symbol table by doing a quick pass through the
8263 .debug_info and .debug_abbrev sections. */
8264
8265 static void
8266 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8267 {
8268 struct objfile *objfile = dwarf2_per_objfile->objfile;
8269
8270 if (dwarf_read_debug)
8271 {
8272 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8273 objfile_name (objfile));
8274 }
8275
8276 dwarf2_per_objfile->reading_partial_symbols = 1;
8277
8278 dwarf2_per_objfile->info.read (objfile);
8279
8280 /* Any cached compilation units will be linked by the per-objfile
8281 read_in_chain. Make sure to free them when we're done. */
8282 free_cached_comp_units freer (dwarf2_per_objfile);
8283
8284 build_type_psymtabs (dwarf2_per_objfile);
8285
8286 create_all_comp_units (dwarf2_per_objfile);
8287
8288 /* Create a temporary address map on a temporary obstack. We later
8289 copy this to the final obstack. */
8290 auto_obstack temp_obstack;
8291
8292 scoped_restore save_psymtabs_addrmap
8293 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8294 addrmap_create_mutable (&temp_obstack));
8295
8296 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8297 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8298
8299 /* This has to wait until we read the CUs, we need the list of DWOs. */
8300 process_skeletonless_type_units (dwarf2_per_objfile);
8301
8302 /* Now that all TUs have been processed we can fill in the dependencies. */
8303 if (dwarf2_per_objfile->type_unit_groups != NULL)
8304 {
8305 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8306 build_type_psymtab_dependencies, dwarf2_per_objfile);
8307 }
8308
8309 if (dwarf_read_debug)
8310 print_tu_stats (dwarf2_per_objfile);
8311
8312 set_partial_user (dwarf2_per_objfile);
8313
8314 objfile->partial_symtabs->psymtabs_addrmap
8315 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8316 objfile->partial_symtabs->obstack ());
8317 /* At this point we want to keep the address map. */
8318 save_psymtabs_addrmap.release ();
8319
8320 if (dwarf_read_debug)
8321 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8322 objfile_name (objfile));
8323 }
8324
8325 /* Load the partial DIEs for a secondary CU into memory.
8326 This is also used when rereading a primary CU with load_all_dies. */
8327
8328 static void
8329 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8330 {
8331 cutu_reader reader (this_cu, NULL, 1, 1, false);
8332
8333 if (!reader.dummy_p)
8334 {
8335 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8336 language_minimal);
8337
8338 /* Check if comp unit has_children.
8339 If so, read the rest of the partial symbols from this comp unit.
8340 If not, there's no more debug_info for this comp unit. */
8341 if (reader.has_children)
8342 load_partial_dies (&reader, reader.info_ptr, 0);
8343 }
8344 }
8345
8346 static void
8347 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8348 struct dwarf2_section_info *section,
8349 struct dwarf2_section_info *abbrev_section,
8350 unsigned int is_dwz)
8351 {
8352 const gdb_byte *info_ptr;
8353 struct objfile *objfile = dwarf2_per_objfile->objfile;
8354
8355 if (dwarf_read_debug)
8356 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8357 section->get_name (),
8358 section->get_file_name ());
8359
8360 section->read (objfile);
8361
8362 info_ptr = section->buffer;
8363
8364 while (info_ptr < section->buffer + section->size)
8365 {
8366 struct dwarf2_per_cu_data *this_cu;
8367
8368 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8369
8370 comp_unit_head cu_header;
8371 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8372 abbrev_section, info_ptr,
8373 rcuh_kind::COMPILE);
8374
8375 /* Save the compilation unit for later lookup. */
8376 if (cu_header.unit_type != DW_UT_type)
8377 {
8378 this_cu = XOBNEW (&objfile->objfile_obstack,
8379 struct dwarf2_per_cu_data);
8380 memset (this_cu, 0, sizeof (*this_cu));
8381 }
8382 else
8383 {
8384 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8385 struct signatured_type);
8386 memset (sig_type, 0, sizeof (*sig_type));
8387 sig_type->signature = cu_header.signature;
8388 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8389 this_cu = &sig_type->per_cu;
8390 }
8391 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8392 this_cu->sect_off = sect_off;
8393 this_cu->length = cu_header.length + cu_header.initial_length_size;
8394 this_cu->is_dwz = is_dwz;
8395 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8396 this_cu->section = section;
8397
8398 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8399
8400 info_ptr = info_ptr + this_cu->length;
8401 }
8402 }
8403
8404 /* Create a list of all compilation units in OBJFILE.
8405 This is only done for -readnow and building partial symtabs. */
8406
8407 static void
8408 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8409 {
8410 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8411 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8412 &dwarf2_per_objfile->abbrev, 0);
8413
8414 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8415 if (dwz != NULL)
8416 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8417 1);
8418 }
8419
8420 /* Process all loaded DIEs for compilation unit CU, starting at
8421 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8422 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8423 DW_AT_ranges). See the comments of add_partial_subprogram on how
8424 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8425
8426 static void
8427 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8428 CORE_ADDR *highpc, int set_addrmap,
8429 struct dwarf2_cu *cu)
8430 {
8431 struct partial_die_info *pdi;
8432
8433 /* Now, march along the PDI's, descending into ones which have
8434 interesting children but skipping the children of the other ones,
8435 until we reach the end of the compilation unit. */
8436
8437 pdi = first_die;
8438
8439 while (pdi != NULL)
8440 {
8441 pdi->fixup (cu);
8442
8443 /* Anonymous namespaces or modules have no name but have interesting
8444 children, so we need to look at them. Ditto for anonymous
8445 enums. */
8446
8447 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8448 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8449 || pdi->tag == DW_TAG_imported_unit
8450 || pdi->tag == DW_TAG_inlined_subroutine)
8451 {
8452 switch (pdi->tag)
8453 {
8454 case DW_TAG_subprogram:
8455 case DW_TAG_inlined_subroutine:
8456 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8457 break;
8458 case DW_TAG_constant:
8459 case DW_TAG_variable:
8460 case DW_TAG_typedef:
8461 case DW_TAG_union_type:
8462 if (!pdi->is_declaration)
8463 {
8464 add_partial_symbol (pdi, cu);
8465 }
8466 break;
8467 case DW_TAG_class_type:
8468 case DW_TAG_interface_type:
8469 case DW_TAG_structure_type:
8470 if (!pdi->is_declaration)
8471 {
8472 add_partial_symbol (pdi, cu);
8473 }
8474 if ((cu->language == language_rust
8475 || cu->language == language_cplus) && pdi->has_children)
8476 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8477 set_addrmap, cu);
8478 break;
8479 case DW_TAG_enumeration_type:
8480 if (!pdi->is_declaration)
8481 add_partial_enumeration (pdi, cu);
8482 break;
8483 case DW_TAG_base_type:
8484 case DW_TAG_subrange_type:
8485 /* File scope base type definitions are added to the partial
8486 symbol table. */
8487 add_partial_symbol (pdi, cu);
8488 break;
8489 case DW_TAG_namespace:
8490 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8491 break;
8492 case DW_TAG_module:
8493 if (!pdi->is_declaration)
8494 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8495 break;
8496 case DW_TAG_imported_unit:
8497 {
8498 struct dwarf2_per_cu_data *per_cu;
8499
8500 /* For now we don't handle imported units in type units. */
8501 if (cu->per_cu->is_debug_types)
8502 {
8503 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8504 " supported in type units [in module %s]"),
8505 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8506 }
8507
8508 per_cu = dwarf2_find_containing_comp_unit
8509 (pdi->d.sect_off, pdi->is_dwz,
8510 cu->per_cu->dwarf2_per_objfile);
8511
8512 /* Go read the partial unit, if needed. */
8513 if (per_cu->v.psymtab == NULL)
8514 process_psymtab_comp_unit (per_cu, 1, cu->language);
8515
8516 cu->per_cu->imported_symtabs_push (per_cu);
8517 }
8518 break;
8519 case DW_TAG_imported_declaration:
8520 add_partial_symbol (pdi, cu);
8521 break;
8522 default:
8523 break;
8524 }
8525 }
8526
8527 /* If the die has a sibling, skip to the sibling. */
8528
8529 pdi = pdi->die_sibling;
8530 }
8531 }
8532
8533 /* Functions used to compute the fully scoped name of a partial DIE.
8534
8535 Normally, this is simple. For C++, the parent DIE's fully scoped
8536 name is concatenated with "::" and the partial DIE's name.
8537 Enumerators are an exception; they use the scope of their parent
8538 enumeration type, i.e. the name of the enumeration type is not
8539 prepended to the enumerator.
8540
8541 There are two complexities. One is DW_AT_specification; in this
8542 case "parent" means the parent of the target of the specification,
8543 instead of the direct parent of the DIE. The other is compilers
8544 which do not emit DW_TAG_namespace; in this case we try to guess
8545 the fully qualified name of structure types from their members'
8546 linkage names. This must be done using the DIE's children rather
8547 than the children of any DW_AT_specification target. We only need
8548 to do this for structures at the top level, i.e. if the target of
8549 any DW_AT_specification (if any; otherwise the DIE itself) does not
8550 have a parent. */
8551
8552 /* Compute the scope prefix associated with PDI's parent, in
8553 compilation unit CU. The result will be allocated on CU's
8554 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8555 field. NULL is returned if no prefix is necessary. */
8556 static const char *
8557 partial_die_parent_scope (struct partial_die_info *pdi,
8558 struct dwarf2_cu *cu)
8559 {
8560 const char *grandparent_scope;
8561 struct partial_die_info *parent, *real_pdi;
8562
8563 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8564 then this means the parent of the specification DIE. */
8565
8566 real_pdi = pdi;
8567 while (real_pdi->has_specification)
8568 {
8569 auto res = find_partial_die (real_pdi->spec_offset,
8570 real_pdi->spec_is_dwz, cu);
8571 real_pdi = res.pdi;
8572 cu = res.cu;
8573 }
8574
8575 parent = real_pdi->die_parent;
8576 if (parent == NULL)
8577 return NULL;
8578
8579 if (parent->scope_set)
8580 return parent->scope;
8581
8582 parent->fixup (cu);
8583
8584 grandparent_scope = partial_die_parent_scope (parent, cu);
8585
8586 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8587 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8588 Work around this problem here. */
8589 if (cu->language == language_cplus
8590 && parent->tag == DW_TAG_namespace
8591 && strcmp (parent->name, "::") == 0
8592 && grandparent_scope == NULL)
8593 {
8594 parent->scope = NULL;
8595 parent->scope_set = 1;
8596 return NULL;
8597 }
8598
8599 /* Nested subroutines in Fortran get a prefix. */
8600 if (pdi->tag == DW_TAG_enumerator)
8601 /* Enumerators should not get the name of the enumeration as a prefix. */
8602 parent->scope = grandparent_scope;
8603 else if (parent->tag == DW_TAG_namespace
8604 || parent->tag == DW_TAG_module
8605 || parent->tag == DW_TAG_structure_type
8606 || parent->tag == DW_TAG_class_type
8607 || parent->tag == DW_TAG_interface_type
8608 || parent->tag == DW_TAG_union_type
8609 || parent->tag == DW_TAG_enumeration_type
8610 || (cu->language == language_fortran
8611 && parent->tag == DW_TAG_subprogram
8612 && pdi->tag == DW_TAG_subprogram))
8613 {
8614 if (grandparent_scope == NULL)
8615 parent->scope = parent->name;
8616 else
8617 parent->scope = typename_concat (&cu->comp_unit_obstack,
8618 grandparent_scope,
8619 parent->name, 0, cu);
8620 }
8621 else
8622 {
8623 /* FIXME drow/2004-04-01: What should we be doing with
8624 function-local names? For partial symbols, we should probably be
8625 ignoring them. */
8626 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8627 dwarf_tag_name (parent->tag),
8628 sect_offset_str (pdi->sect_off));
8629 parent->scope = grandparent_scope;
8630 }
8631
8632 parent->scope_set = 1;
8633 return parent->scope;
8634 }
8635
8636 /* Return the fully scoped name associated with PDI, from compilation unit
8637 CU. The result will be allocated with malloc. */
8638
8639 static gdb::unique_xmalloc_ptr<char>
8640 partial_die_full_name (struct partial_die_info *pdi,
8641 struct dwarf2_cu *cu)
8642 {
8643 const char *parent_scope;
8644
8645 /* If this is a template instantiation, we can not work out the
8646 template arguments from partial DIEs. So, unfortunately, we have
8647 to go through the full DIEs. At least any work we do building
8648 types here will be reused if full symbols are loaded later. */
8649 if (pdi->has_template_arguments)
8650 {
8651 pdi->fixup (cu);
8652
8653 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8654 {
8655 struct die_info *die;
8656 struct attribute attr;
8657 struct dwarf2_cu *ref_cu = cu;
8658
8659 /* DW_FORM_ref_addr is using section offset. */
8660 attr.name = (enum dwarf_attribute) 0;
8661 attr.form = DW_FORM_ref_addr;
8662 attr.u.unsnd = to_underlying (pdi->sect_off);
8663 die = follow_die_ref (NULL, &attr, &ref_cu);
8664
8665 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8666 }
8667 }
8668
8669 parent_scope = partial_die_parent_scope (pdi, cu);
8670 if (parent_scope == NULL)
8671 return NULL;
8672 else
8673 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8674 pdi->name, 0, cu));
8675 }
8676
8677 static void
8678 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8679 {
8680 struct dwarf2_per_objfile *dwarf2_per_objfile
8681 = cu->per_cu->dwarf2_per_objfile;
8682 struct objfile *objfile = dwarf2_per_objfile->objfile;
8683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8684 CORE_ADDR addr = 0;
8685 const char *actual_name = NULL;
8686 CORE_ADDR baseaddr;
8687
8688 baseaddr = objfile->text_section_offset ();
8689
8690 gdb::unique_xmalloc_ptr<char> built_actual_name
8691 = partial_die_full_name (pdi, cu);
8692 if (built_actual_name != NULL)
8693 actual_name = built_actual_name.get ();
8694
8695 if (actual_name == NULL)
8696 actual_name = pdi->name;
8697
8698 switch (pdi->tag)
8699 {
8700 case DW_TAG_inlined_subroutine:
8701 case DW_TAG_subprogram:
8702 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8703 - baseaddr);
8704 if (pdi->is_external
8705 || cu->language == language_ada
8706 || (cu->language == language_fortran
8707 && pdi->die_parent != NULL
8708 && pdi->die_parent->tag == DW_TAG_subprogram))
8709 {
8710 /* Normally, only "external" DIEs are part of the global scope.
8711 But in Ada and Fortran, we want to be able to access nested
8712 procedures globally. So all Ada and Fortran subprograms are
8713 stored in the global scope. */
8714 add_psymbol_to_list (actual_name,
8715 built_actual_name != NULL,
8716 VAR_DOMAIN, LOC_BLOCK,
8717 SECT_OFF_TEXT (objfile),
8718 psymbol_placement::GLOBAL,
8719 addr,
8720 cu->language, objfile);
8721 }
8722 else
8723 {
8724 add_psymbol_to_list (actual_name,
8725 built_actual_name != NULL,
8726 VAR_DOMAIN, LOC_BLOCK,
8727 SECT_OFF_TEXT (objfile),
8728 psymbol_placement::STATIC,
8729 addr, cu->language, objfile);
8730 }
8731
8732 if (pdi->main_subprogram && actual_name != NULL)
8733 set_objfile_main_name (objfile, actual_name, cu->language);
8734 break;
8735 case DW_TAG_constant:
8736 add_psymbol_to_list (actual_name,
8737 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8738 -1, (pdi->is_external
8739 ? psymbol_placement::GLOBAL
8740 : psymbol_placement::STATIC),
8741 0, cu->language, objfile);
8742 break;
8743 case DW_TAG_variable:
8744 if (pdi->d.locdesc)
8745 addr = decode_locdesc (pdi->d.locdesc, cu);
8746
8747 if (pdi->d.locdesc
8748 && addr == 0
8749 && !dwarf2_per_objfile->has_section_at_zero)
8750 {
8751 /* A global or static variable may also have been stripped
8752 out by the linker if unused, in which case its address
8753 will be nullified; do not add such variables into partial
8754 symbol table then. */
8755 }
8756 else if (pdi->is_external)
8757 {
8758 /* Global Variable.
8759 Don't enter into the minimal symbol tables as there is
8760 a minimal symbol table entry from the ELF symbols already.
8761 Enter into partial symbol table if it has a location
8762 descriptor or a type.
8763 If the location descriptor is missing, new_symbol will create
8764 a LOC_UNRESOLVED symbol, the address of the variable will then
8765 be determined from the minimal symbol table whenever the variable
8766 is referenced.
8767 The address for the partial symbol table entry is not
8768 used by GDB, but it comes in handy for debugging partial symbol
8769 table building. */
8770
8771 if (pdi->d.locdesc || pdi->has_type)
8772 add_psymbol_to_list (actual_name,
8773 built_actual_name != NULL,
8774 VAR_DOMAIN, LOC_STATIC,
8775 SECT_OFF_TEXT (objfile),
8776 psymbol_placement::GLOBAL,
8777 addr, cu->language, objfile);
8778 }
8779 else
8780 {
8781 int has_loc = pdi->d.locdesc != NULL;
8782
8783 /* Static Variable. Skip symbols whose value we cannot know (those
8784 without location descriptors or constant values). */
8785 if (!has_loc && !pdi->has_const_value)
8786 return;
8787
8788 add_psymbol_to_list (actual_name,
8789 built_actual_name != NULL,
8790 VAR_DOMAIN, LOC_STATIC,
8791 SECT_OFF_TEXT (objfile),
8792 psymbol_placement::STATIC,
8793 has_loc ? addr : 0,
8794 cu->language, objfile);
8795 }
8796 break;
8797 case DW_TAG_typedef:
8798 case DW_TAG_base_type:
8799 case DW_TAG_subrange_type:
8800 add_psymbol_to_list (actual_name,
8801 built_actual_name != NULL,
8802 VAR_DOMAIN, LOC_TYPEDEF, -1,
8803 psymbol_placement::STATIC,
8804 0, cu->language, objfile);
8805 break;
8806 case DW_TAG_imported_declaration:
8807 case DW_TAG_namespace:
8808 add_psymbol_to_list (actual_name,
8809 built_actual_name != NULL,
8810 VAR_DOMAIN, LOC_TYPEDEF, -1,
8811 psymbol_placement::GLOBAL,
8812 0, cu->language, objfile);
8813 break;
8814 case DW_TAG_module:
8815 /* With Fortran 77 there might be a "BLOCK DATA" module
8816 available without any name. If so, we skip the module as it
8817 doesn't bring any value. */
8818 if (actual_name != nullptr)
8819 add_psymbol_to_list (actual_name,
8820 built_actual_name != NULL,
8821 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8822 psymbol_placement::GLOBAL,
8823 0, cu->language, objfile);
8824 break;
8825 case DW_TAG_class_type:
8826 case DW_TAG_interface_type:
8827 case DW_TAG_structure_type:
8828 case DW_TAG_union_type:
8829 case DW_TAG_enumeration_type:
8830 /* Skip external references. The DWARF standard says in the section
8831 about "Structure, Union, and Class Type Entries": "An incomplete
8832 structure, union or class type is represented by a structure,
8833 union or class entry that does not have a byte size attribute
8834 and that has a DW_AT_declaration attribute." */
8835 if (!pdi->has_byte_size && pdi->is_declaration)
8836 return;
8837
8838 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8839 static vs. global. */
8840 add_psymbol_to_list (actual_name,
8841 built_actual_name != NULL,
8842 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8843 cu->language == language_cplus
8844 ? psymbol_placement::GLOBAL
8845 : psymbol_placement::STATIC,
8846 0, cu->language, objfile);
8847
8848 break;
8849 case DW_TAG_enumerator:
8850 add_psymbol_to_list (actual_name,
8851 built_actual_name != NULL,
8852 VAR_DOMAIN, LOC_CONST, -1,
8853 cu->language == language_cplus
8854 ? psymbol_placement::GLOBAL
8855 : psymbol_placement::STATIC,
8856 0, cu->language, objfile);
8857 break;
8858 default:
8859 break;
8860 }
8861 }
8862
8863 /* Read a partial die corresponding to a namespace; also, add a symbol
8864 corresponding to that namespace to the symbol table. NAMESPACE is
8865 the name of the enclosing namespace. */
8866
8867 static void
8868 add_partial_namespace (struct partial_die_info *pdi,
8869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8870 int set_addrmap, struct dwarf2_cu *cu)
8871 {
8872 /* Add a symbol for the namespace. */
8873
8874 add_partial_symbol (pdi, cu);
8875
8876 /* Now scan partial symbols in that namespace. */
8877
8878 if (pdi->has_children)
8879 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8880 }
8881
8882 /* Read a partial die corresponding to a Fortran module. */
8883
8884 static void
8885 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8886 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8887 {
8888 /* Add a symbol for the namespace. */
8889
8890 add_partial_symbol (pdi, cu);
8891
8892 /* Now scan partial symbols in that module. */
8893
8894 if (pdi->has_children)
8895 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8896 }
8897
8898 /* Read a partial die corresponding to a subprogram or an inlined
8899 subprogram and create a partial symbol for that subprogram.
8900 When the CU language allows it, this routine also defines a partial
8901 symbol for each nested subprogram that this subprogram contains.
8902 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8903 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8904
8905 PDI may also be a lexical block, in which case we simply search
8906 recursively for subprograms defined inside that lexical block.
8907 Again, this is only performed when the CU language allows this
8908 type of definitions. */
8909
8910 static void
8911 add_partial_subprogram (struct partial_die_info *pdi,
8912 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8913 int set_addrmap, struct dwarf2_cu *cu)
8914 {
8915 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8916 {
8917 if (pdi->has_pc_info)
8918 {
8919 if (pdi->lowpc < *lowpc)
8920 *lowpc = pdi->lowpc;
8921 if (pdi->highpc > *highpc)
8922 *highpc = pdi->highpc;
8923 if (set_addrmap)
8924 {
8925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8927 CORE_ADDR baseaddr;
8928 CORE_ADDR this_highpc;
8929 CORE_ADDR this_lowpc;
8930
8931 baseaddr = objfile->text_section_offset ();
8932 this_lowpc
8933 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8934 pdi->lowpc + baseaddr)
8935 - baseaddr);
8936 this_highpc
8937 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8938 pdi->highpc + baseaddr)
8939 - baseaddr);
8940 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8941 this_lowpc, this_highpc - 1,
8942 cu->per_cu->v.psymtab);
8943 }
8944 }
8945
8946 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8947 {
8948 if (!pdi->is_declaration)
8949 /* Ignore subprogram DIEs that do not have a name, they are
8950 illegal. Do not emit a complaint at this point, we will
8951 do so when we convert this psymtab into a symtab. */
8952 if (pdi->name)
8953 add_partial_symbol (pdi, cu);
8954 }
8955 }
8956
8957 if (! pdi->has_children)
8958 return;
8959
8960 if (cu->language == language_ada || cu->language == language_fortran)
8961 {
8962 pdi = pdi->die_child;
8963 while (pdi != NULL)
8964 {
8965 pdi->fixup (cu);
8966 if (pdi->tag == DW_TAG_subprogram
8967 || pdi->tag == DW_TAG_inlined_subroutine
8968 || pdi->tag == DW_TAG_lexical_block)
8969 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8970 pdi = pdi->die_sibling;
8971 }
8972 }
8973 }
8974
8975 /* Read a partial die corresponding to an enumeration type. */
8976
8977 static void
8978 add_partial_enumeration (struct partial_die_info *enum_pdi,
8979 struct dwarf2_cu *cu)
8980 {
8981 struct partial_die_info *pdi;
8982
8983 if (enum_pdi->name != NULL)
8984 add_partial_symbol (enum_pdi, cu);
8985
8986 pdi = enum_pdi->die_child;
8987 while (pdi)
8988 {
8989 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8990 complaint (_("malformed enumerator DIE ignored"));
8991 else
8992 add_partial_symbol (pdi, cu);
8993 pdi = pdi->die_sibling;
8994 }
8995 }
8996
8997 /* Return the initial uleb128 in the die at INFO_PTR. */
8998
8999 static unsigned int
9000 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9001 {
9002 unsigned int bytes_read;
9003
9004 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9005 }
9006
9007 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9008 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9009
9010 Return the corresponding abbrev, or NULL if the number is zero (indicating
9011 an empty DIE). In either case *BYTES_READ will be set to the length of
9012 the initial number. */
9013
9014 static struct abbrev_info *
9015 peek_die_abbrev (const die_reader_specs &reader,
9016 const gdb_byte *info_ptr, unsigned int *bytes_read)
9017 {
9018 dwarf2_cu *cu = reader.cu;
9019 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9020 unsigned int abbrev_number
9021 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9022
9023 if (abbrev_number == 0)
9024 return NULL;
9025
9026 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9027 if (!abbrev)
9028 {
9029 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9030 " at offset %s [in module %s]"),
9031 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9032 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9033 }
9034
9035 return abbrev;
9036 }
9037
9038 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9039 Returns a pointer to the end of a series of DIEs, terminated by an empty
9040 DIE. Any children of the skipped DIEs will also be skipped. */
9041
9042 static const gdb_byte *
9043 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9044 {
9045 while (1)
9046 {
9047 unsigned int bytes_read;
9048 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9049
9050 if (abbrev == NULL)
9051 return info_ptr + bytes_read;
9052 else
9053 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9054 }
9055 }
9056
9057 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9058 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9059 abbrev corresponding to that skipped uleb128 should be passed in
9060 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9061 children. */
9062
9063 static const gdb_byte *
9064 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9065 struct abbrev_info *abbrev)
9066 {
9067 unsigned int bytes_read;
9068 struct attribute attr;
9069 bfd *abfd = reader->abfd;
9070 struct dwarf2_cu *cu = reader->cu;
9071 const gdb_byte *buffer = reader->buffer;
9072 const gdb_byte *buffer_end = reader->buffer_end;
9073 unsigned int form, i;
9074
9075 for (i = 0; i < abbrev->num_attrs; i++)
9076 {
9077 /* The only abbrev we care about is DW_AT_sibling. */
9078 if (abbrev->attrs[i].name == DW_AT_sibling)
9079 {
9080 bool ignored;
9081 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9082 &ignored);
9083 if (attr.form == DW_FORM_ref_addr)
9084 complaint (_("ignoring absolute DW_AT_sibling"));
9085 else
9086 {
9087 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9088 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9089
9090 if (sibling_ptr < info_ptr)
9091 complaint (_("DW_AT_sibling points backwards"));
9092 else if (sibling_ptr > reader->buffer_end)
9093 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9094 else
9095 return sibling_ptr;
9096 }
9097 }
9098
9099 /* If it isn't DW_AT_sibling, skip this attribute. */
9100 form = abbrev->attrs[i].form;
9101 skip_attribute:
9102 switch (form)
9103 {
9104 case DW_FORM_ref_addr:
9105 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9106 and later it is offset sized. */
9107 if (cu->header.version == 2)
9108 info_ptr += cu->header.addr_size;
9109 else
9110 info_ptr += cu->header.offset_size;
9111 break;
9112 case DW_FORM_GNU_ref_alt:
9113 info_ptr += cu->header.offset_size;
9114 break;
9115 case DW_FORM_addr:
9116 info_ptr += cu->header.addr_size;
9117 break;
9118 case DW_FORM_data1:
9119 case DW_FORM_ref1:
9120 case DW_FORM_flag:
9121 case DW_FORM_strx1:
9122 info_ptr += 1;
9123 break;
9124 case DW_FORM_flag_present:
9125 case DW_FORM_implicit_const:
9126 break;
9127 case DW_FORM_data2:
9128 case DW_FORM_ref2:
9129 case DW_FORM_strx2:
9130 info_ptr += 2;
9131 break;
9132 case DW_FORM_strx3:
9133 info_ptr += 3;
9134 break;
9135 case DW_FORM_data4:
9136 case DW_FORM_ref4:
9137 case DW_FORM_strx4:
9138 info_ptr += 4;
9139 break;
9140 case DW_FORM_data8:
9141 case DW_FORM_ref8:
9142 case DW_FORM_ref_sig8:
9143 info_ptr += 8;
9144 break;
9145 case DW_FORM_data16:
9146 info_ptr += 16;
9147 break;
9148 case DW_FORM_string:
9149 read_direct_string (abfd, info_ptr, &bytes_read);
9150 info_ptr += bytes_read;
9151 break;
9152 case DW_FORM_sec_offset:
9153 case DW_FORM_strp:
9154 case DW_FORM_GNU_strp_alt:
9155 info_ptr += cu->header.offset_size;
9156 break;
9157 case DW_FORM_exprloc:
9158 case DW_FORM_block:
9159 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9160 info_ptr += bytes_read;
9161 break;
9162 case DW_FORM_block1:
9163 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9164 break;
9165 case DW_FORM_block2:
9166 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9167 break;
9168 case DW_FORM_block4:
9169 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9170 break;
9171 case DW_FORM_addrx:
9172 case DW_FORM_strx:
9173 case DW_FORM_sdata:
9174 case DW_FORM_udata:
9175 case DW_FORM_ref_udata:
9176 case DW_FORM_GNU_addr_index:
9177 case DW_FORM_GNU_str_index:
9178 case DW_FORM_rnglistx:
9179 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9180 break;
9181 case DW_FORM_indirect:
9182 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9183 info_ptr += bytes_read;
9184 /* We need to continue parsing from here, so just go back to
9185 the top. */
9186 goto skip_attribute;
9187
9188 default:
9189 error (_("Dwarf Error: Cannot handle %s "
9190 "in DWARF reader [in module %s]"),
9191 dwarf_form_name (form),
9192 bfd_get_filename (abfd));
9193 }
9194 }
9195
9196 if (abbrev->has_children)
9197 return skip_children (reader, info_ptr);
9198 else
9199 return info_ptr;
9200 }
9201
9202 /* Locate ORIG_PDI's sibling.
9203 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9204
9205 static const gdb_byte *
9206 locate_pdi_sibling (const struct die_reader_specs *reader,
9207 struct partial_die_info *orig_pdi,
9208 const gdb_byte *info_ptr)
9209 {
9210 /* Do we know the sibling already? */
9211
9212 if (orig_pdi->sibling)
9213 return orig_pdi->sibling;
9214
9215 /* Are there any children to deal with? */
9216
9217 if (!orig_pdi->has_children)
9218 return info_ptr;
9219
9220 /* Skip the children the long way. */
9221
9222 return skip_children (reader, info_ptr);
9223 }
9224
9225 /* Expand this partial symbol table into a full symbol table. SELF is
9226 not NULL. */
9227
9228 void
9229 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9230 {
9231 struct dwarf2_per_objfile *dwarf2_per_objfile
9232 = get_dwarf2_per_objfile (objfile);
9233
9234 gdb_assert (!readin);
9235 /* If this psymtab is constructed from a debug-only objfile, the
9236 has_section_at_zero flag will not necessarily be correct. We
9237 can get the correct value for this flag by looking at the data
9238 associated with the (presumably stripped) associated objfile. */
9239 if (objfile->separate_debug_objfile_backlink)
9240 {
9241 struct dwarf2_per_objfile *dpo_backlink
9242 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9243
9244 dwarf2_per_objfile->has_section_at_zero
9245 = dpo_backlink->has_section_at_zero;
9246 }
9247
9248 dwarf2_per_objfile->reading_partial_symbols = 0;
9249
9250 expand_psymtab (objfile);
9251
9252 process_cu_includes (dwarf2_per_objfile);
9253 }
9254 \f
9255 /* Reading in full CUs. */
9256
9257 /* Add PER_CU to the queue. */
9258
9259 static void
9260 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9261 enum language pretend_language)
9262 {
9263 struct dwarf2_queue_item *item;
9264
9265 per_cu->queued = 1;
9266 item = XNEW (struct dwarf2_queue_item);
9267 item->per_cu = per_cu;
9268 item->pretend_language = pretend_language;
9269 item->next = NULL;
9270
9271 if (dwarf2_queue == NULL)
9272 dwarf2_queue = item;
9273 else
9274 dwarf2_queue_tail->next = item;
9275
9276 dwarf2_queue_tail = item;
9277 }
9278
9279 /* If PER_CU is not yet queued, add it to the queue.
9280 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9281 dependency.
9282 The result is non-zero if PER_CU was queued, otherwise the result is zero
9283 meaning either PER_CU is already queued or it is already loaded.
9284
9285 N.B. There is an invariant here that if a CU is queued then it is loaded.
9286 The caller is required to load PER_CU if we return non-zero. */
9287
9288 static int
9289 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9290 struct dwarf2_per_cu_data *per_cu,
9291 enum language pretend_language)
9292 {
9293 /* We may arrive here during partial symbol reading, if we need full
9294 DIEs to process an unusual case (e.g. template arguments). Do
9295 not queue PER_CU, just tell our caller to load its DIEs. */
9296 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9297 {
9298 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9299 return 1;
9300 return 0;
9301 }
9302
9303 /* Mark the dependence relation so that we don't flush PER_CU
9304 too early. */
9305 if (dependent_cu != NULL)
9306 dwarf2_add_dependence (dependent_cu, per_cu);
9307
9308 /* If it's already on the queue, we have nothing to do. */
9309 if (per_cu->queued)
9310 return 0;
9311
9312 /* If the compilation unit is already loaded, just mark it as
9313 used. */
9314 if (per_cu->cu != NULL)
9315 {
9316 per_cu->cu->last_used = 0;
9317 return 0;
9318 }
9319
9320 /* Add it to the queue. */
9321 queue_comp_unit (per_cu, pretend_language);
9322
9323 return 1;
9324 }
9325
9326 /* Process the queue. */
9327
9328 static void
9329 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9330 {
9331 struct dwarf2_queue_item *item, *next_item;
9332
9333 if (dwarf_read_debug)
9334 {
9335 fprintf_unfiltered (gdb_stdlog,
9336 "Expanding one or more symtabs of objfile %s ...\n",
9337 objfile_name (dwarf2_per_objfile->objfile));
9338 }
9339
9340 /* The queue starts out with one item, but following a DIE reference
9341 may load a new CU, adding it to the end of the queue. */
9342 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9343 {
9344 if ((dwarf2_per_objfile->using_index
9345 ? !item->per_cu->v.quick->compunit_symtab
9346 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9347 /* Skip dummy CUs. */
9348 && item->per_cu->cu != NULL)
9349 {
9350 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9351 unsigned int debug_print_threshold;
9352 char buf[100];
9353
9354 if (per_cu->is_debug_types)
9355 {
9356 struct signatured_type *sig_type =
9357 (struct signatured_type *) per_cu;
9358
9359 sprintf (buf, "TU %s at offset %s",
9360 hex_string (sig_type->signature),
9361 sect_offset_str (per_cu->sect_off));
9362 /* There can be 100s of TUs.
9363 Only print them in verbose mode. */
9364 debug_print_threshold = 2;
9365 }
9366 else
9367 {
9368 sprintf (buf, "CU at offset %s",
9369 sect_offset_str (per_cu->sect_off));
9370 debug_print_threshold = 1;
9371 }
9372
9373 if (dwarf_read_debug >= debug_print_threshold)
9374 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9375
9376 if (per_cu->is_debug_types)
9377 process_full_type_unit (per_cu, item->pretend_language);
9378 else
9379 process_full_comp_unit (per_cu, item->pretend_language);
9380
9381 if (dwarf_read_debug >= debug_print_threshold)
9382 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9383 }
9384
9385 item->per_cu->queued = 0;
9386 next_item = item->next;
9387 xfree (item);
9388 }
9389
9390 dwarf2_queue_tail = NULL;
9391
9392 if (dwarf_read_debug)
9393 {
9394 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9395 objfile_name (dwarf2_per_objfile->objfile));
9396 }
9397 }
9398
9399 /* Read in full symbols for PST, and anything it depends on. */
9400
9401 void
9402 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9403 {
9404 struct dwarf2_per_cu_data *per_cu;
9405
9406 if (readin)
9407 return;
9408
9409 read_dependencies (objfile);
9410
9411 per_cu = per_cu_data;
9412
9413 if (per_cu == NULL)
9414 {
9415 /* It's an include file, no symbols to read for it.
9416 Everything is in the parent symtab. */
9417 readin = true;
9418 return;
9419 }
9420
9421 dw2_do_instantiate_symtab (per_cu, false);
9422 }
9423
9424 /* Trivial hash function for die_info: the hash value of a DIE
9425 is its offset in .debug_info for this objfile. */
9426
9427 static hashval_t
9428 die_hash (const void *item)
9429 {
9430 const struct die_info *die = (const struct die_info *) item;
9431
9432 return to_underlying (die->sect_off);
9433 }
9434
9435 /* Trivial comparison function for die_info structures: two DIEs
9436 are equal if they have the same offset. */
9437
9438 static int
9439 die_eq (const void *item_lhs, const void *item_rhs)
9440 {
9441 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9442 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9443
9444 return die_lhs->sect_off == die_rhs->sect_off;
9445 }
9446
9447 /* Load the DIEs associated with PER_CU into memory. */
9448
9449 static void
9450 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9451 bool skip_partial,
9452 enum language pretend_language)
9453 {
9454 gdb_assert (! this_cu->is_debug_types);
9455
9456 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9457 if (reader.dummy_p)
9458 return;
9459
9460 struct dwarf2_cu *cu = reader.cu;
9461 const gdb_byte *info_ptr = reader.info_ptr;
9462
9463 gdb_assert (cu->die_hash == NULL);
9464 cu->die_hash =
9465 htab_create_alloc_ex (cu->header.length / 12,
9466 die_hash,
9467 die_eq,
9468 NULL,
9469 &cu->comp_unit_obstack,
9470 hashtab_obstack_allocate,
9471 dummy_obstack_deallocate);
9472
9473 if (reader.has_children)
9474 reader.comp_unit_die->child
9475 = read_die_and_siblings (&reader, reader.info_ptr,
9476 &info_ptr, reader.comp_unit_die);
9477 cu->dies = reader.comp_unit_die;
9478 /* comp_unit_die is not stored in die_hash, no need. */
9479
9480 /* We try not to read any attributes in this function, because not
9481 all CUs needed for references have been loaded yet, and symbol
9482 table processing isn't initialized. But we have to set the CU language,
9483 or we won't be able to build types correctly.
9484 Similarly, if we do not read the producer, we can not apply
9485 producer-specific interpretation. */
9486 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9487 }
9488
9489 /* Add a DIE to the delayed physname list. */
9490
9491 static void
9492 add_to_method_list (struct type *type, int fnfield_index, int index,
9493 const char *name, struct die_info *die,
9494 struct dwarf2_cu *cu)
9495 {
9496 struct delayed_method_info mi;
9497 mi.type = type;
9498 mi.fnfield_index = fnfield_index;
9499 mi.index = index;
9500 mi.name = name;
9501 mi.die = die;
9502 cu->method_list.push_back (mi);
9503 }
9504
9505 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9506 "const" / "volatile". If so, decrements LEN by the length of the
9507 modifier and return true. Otherwise return false. */
9508
9509 template<size_t N>
9510 static bool
9511 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9512 {
9513 size_t mod_len = sizeof (mod) - 1;
9514 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9515 {
9516 len -= mod_len;
9517 return true;
9518 }
9519 return false;
9520 }
9521
9522 /* Compute the physnames of any methods on the CU's method list.
9523
9524 The computation of method physnames is delayed in order to avoid the
9525 (bad) condition that one of the method's formal parameters is of an as yet
9526 incomplete type. */
9527
9528 static void
9529 compute_delayed_physnames (struct dwarf2_cu *cu)
9530 {
9531 /* Only C++ delays computing physnames. */
9532 if (cu->method_list.empty ())
9533 return;
9534 gdb_assert (cu->language == language_cplus);
9535
9536 for (const delayed_method_info &mi : cu->method_list)
9537 {
9538 const char *physname;
9539 struct fn_fieldlist *fn_flp
9540 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9541 physname = dwarf2_physname (mi.name, mi.die, cu);
9542 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9543 = physname ? physname : "";
9544
9545 /* Since there's no tag to indicate whether a method is a
9546 const/volatile overload, extract that information out of the
9547 demangled name. */
9548 if (physname != NULL)
9549 {
9550 size_t len = strlen (physname);
9551
9552 while (1)
9553 {
9554 if (physname[len] == ')') /* shortcut */
9555 break;
9556 else if (check_modifier (physname, len, " const"))
9557 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9558 else if (check_modifier (physname, len, " volatile"))
9559 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9560 else
9561 break;
9562 }
9563 }
9564 }
9565
9566 /* The list is no longer needed. */
9567 cu->method_list.clear ();
9568 }
9569
9570 /* Go objects should be embedded in a DW_TAG_module DIE,
9571 and it's not clear if/how imported objects will appear.
9572 To keep Go support simple until that's worked out,
9573 go back through what we've read and create something usable.
9574 We could do this while processing each DIE, and feels kinda cleaner,
9575 but that way is more invasive.
9576 This is to, for example, allow the user to type "p var" or "b main"
9577 without having to specify the package name, and allow lookups
9578 of module.object to work in contexts that use the expression
9579 parser. */
9580
9581 static void
9582 fixup_go_packaging (struct dwarf2_cu *cu)
9583 {
9584 gdb::unique_xmalloc_ptr<char> package_name;
9585 struct pending *list;
9586 int i;
9587
9588 for (list = *cu->get_builder ()->get_global_symbols ();
9589 list != NULL;
9590 list = list->next)
9591 {
9592 for (i = 0; i < list->nsyms; ++i)
9593 {
9594 struct symbol *sym = list->symbol[i];
9595
9596 if (sym->language () == language_go
9597 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9598 {
9599 gdb::unique_xmalloc_ptr<char> this_package_name
9600 (go_symbol_package_name (sym));
9601
9602 if (this_package_name == NULL)
9603 continue;
9604 if (package_name == NULL)
9605 package_name = std::move (this_package_name);
9606 else
9607 {
9608 struct objfile *objfile
9609 = cu->per_cu->dwarf2_per_objfile->objfile;
9610 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9611 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9612 (symbol_symtab (sym) != NULL
9613 ? symtab_to_filename_for_display
9614 (symbol_symtab (sym))
9615 : objfile_name (objfile)),
9616 this_package_name.get (), package_name.get ());
9617 }
9618 }
9619 }
9620 }
9621
9622 if (package_name != NULL)
9623 {
9624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9625 const char *saved_package_name
9626 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9627 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9628 saved_package_name);
9629 struct symbol *sym;
9630
9631 sym = allocate_symbol (objfile);
9632 sym->set_language (language_go, &objfile->objfile_obstack);
9633 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9634 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9635 e.g., "main" finds the "main" module and not C's main(). */
9636 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9637 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9638 SYMBOL_TYPE (sym) = type;
9639
9640 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9641 }
9642 }
9643
9644 /* Allocate a fully-qualified name consisting of the two parts on the
9645 obstack. */
9646
9647 static const char *
9648 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9649 {
9650 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9651 }
9652
9653 /* A helper that allocates a struct discriminant_info to attach to a
9654 union type. */
9655
9656 static struct discriminant_info *
9657 alloc_discriminant_info (struct type *type, int discriminant_index,
9658 int default_index)
9659 {
9660 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9661 gdb_assert (discriminant_index == -1
9662 || (discriminant_index >= 0
9663 && discriminant_index < TYPE_NFIELDS (type)));
9664 gdb_assert (default_index == -1
9665 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9666
9667 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9668
9669 struct discriminant_info *disc
9670 = ((struct discriminant_info *)
9671 TYPE_ZALLOC (type,
9672 offsetof (struct discriminant_info, discriminants)
9673 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9674 disc->default_index = default_index;
9675 disc->discriminant_index = discriminant_index;
9676
9677 struct dynamic_prop prop;
9678 prop.kind = PROP_UNDEFINED;
9679 prop.data.baton = disc;
9680
9681 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9682
9683 return disc;
9684 }
9685
9686 /* Some versions of rustc emitted enums in an unusual way.
9687
9688 Ordinary enums were emitted as unions. The first element of each
9689 structure in the union was named "RUST$ENUM$DISR". This element
9690 held the discriminant.
9691
9692 These versions of Rust also implemented the "non-zero"
9693 optimization. When the enum had two values, and one is empty and
9694 the other holds a pointer that cannot be zero, the pointer is used
9695 as the discriminant, with a zero value meaning the empty variant.
9696 Here, the union's first member is of the form
9697 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9698 where the fieldnos are the indices of the fields that should be
9699 traversed in order to find the field (which may be several fields deep)
9700 and the variantname is the name of the variant of the case when the
9701 field is zero.
9702
9703 This function recognizes whether TYPE is of one of these forms,
9704 and, if so, smashes it to be a variant type. */
9705
9706 static void
9707 quirk_rust_enum (struct type *type, struct objfile *objfile)
9708 {
9709 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9710
9711 /* We don't need to deal with empty enums. */
9712 if (TYPE_NFIELDS (type) == 0)
9713 return;
9714
9715 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9716 if (TYPE_NFIELDS (type) == 1
9717 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9718 {
9719 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9720
9721 /* Decode the field name to find the offset of the
9722 discriminant. */
9723 ULONGEST bit_offset = 0;
9724 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9725 while (name[0] >= '0' && name[0] <= '9')
9726 {
9727 char *tail;
9728 unsigned long index = strtoul (name, &tail, 10);
9729 name = tail;
9730 if (*name != '$'
9731 || index >= TYPE_NFIELDS (field_type)
9732 || (TYPE_FIELD_LOC_KIND (field_type, index)
9733 != FIELD_LOC_KIND_BITPOS))
9734 {
9735 complaint (_("Could not parse Rust enum encoding string \"%s\""
9736 "[in module %s]"),
9737 TYPE_FIELD_NAME (type, 0),
9738 objfile_name (objfile));
9739 return;
9740 }
9741 ++name;
9742
9743 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9744 field_type = TYPE_FIELD_TYPE (field_type, index);
9745 }
9746
9747 /* Make a union to hold the variants. */
9748 struct type *union_type = alloc_type (objfile);
9749 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9750 TYPE_NFIELDS (union_type) = 3;
9751 TYPE_FIELDS (union_type)
9752 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9753 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9754 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9755
9756 /* Put the discriminant must at index 0. */
9757 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9758 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9759 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9760 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9761
9762 /* The order of fields doesn't really matter, so put the real
9763 field at index 1 and the data-less field at index 2. */
9764 struct discriminant_info *disc
9765 = alloc_discriminant_info (union_type, 0, 1);
9766 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9767 TYPE_FIELD_NAME (union_type, 1)
9768 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9769 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9770 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9771 TYPE_FIELD_NAME (union_type, 1));
9772
9773 const char *dataless_name
9774 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9775 name);
9776 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9777 dataless_name);
9778 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9779 /* NAME points into the original discriminant name, which
9780 already has the correct lifetime. */
9781 TYPE_FIELD_NAME (union_type, 2) = name;
9782 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9783 disc->discriminants[2] = 0;
9784
9785 /* Smash this type to be a structure type. We have to do this
9786 because the type has already been recorded. */
9787 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9788 TYPE_NFIELDS (type) = 1;
9789 TYPE_FIELDS (type)
9790 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9791
9792 /* Install the variant part. */
9793 TYPE_FIELD_TYPE (type, 0) = union_type;
9794 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9795 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9796 }
9797 /* A union with a single anonymous field is probably an old-style
9798 univariant enum. */
9799 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9800 {
9801 /* Smash this type to be a structure type. We have to do this
9802 because the type has already been recorded. */
9803 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9804
9805 /* Make a union to hold the variants. */
9806 struct type *union_type = alloc_type (objfile);
9807 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9808 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9809 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9810 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9811 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9812
9813 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9814 const char *variant_name
9815 = rust_last_path_segment (TYPE_NAME (field_type));
9816 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9817 TYPE_NAME (field_type)
9818 = rust_fully_qualify (&objfile->objfile_obstack,
9819 TYPE_NAME (type), variant_name);
9820
9821 /* Install the union in the outer struct type. */
9822 TYPE_NFIELDS (type) = 1;
9823 TYPE_FIELDS (type)
9824 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9825 TYPE_FIELD_TYPE (type, 0) = union_type;
9826 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9827 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9828
9829 alloc_discriminant_info (union_type, -1, 0);
9830 }
9831 else
9832 {
9833 struct type *disr_type = nullptr;
9834 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9835 {
9836 disr_type = TYPE_FIELD_TYPE (type, i);
9837
9838 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9839 {
9840 /* All fields of a true enum will be structs. */
9841 return;
9842 }
9843 else if (TYPE_NFIELDS (disr_type) == 0)
9844 {
9845 /* Could be data-less variant, so keep going. */
9846 disr_type = nullptr;
9847 }
9848 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9849 "RUST$ENUM$DISR") != 0)
9850 {
9851 /* Not a Rust enum. */
9852 return;
9853 }
9854 else
9855 {
9856 /* Found one. */
9857 break;
9858 }
9859 }
9860
9861 /* If we got here without a discriminant, then it's probably
9862 just a union. */
9863 if (disr_type == nullptr)
9864 return;
9865
9866 /* Smash this type to be a structure type. We have to do this
9867 because the type has already been recorded. */
9868 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9869
9870 /* Make a union to hold the variants. */
9871 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9872 struct type *union_type = alloc_type (objfile);
9873 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9874 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9875 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9876 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9877 TYPE_FIELDS (union_type)
9878 = (struct field *) TYPE_ZALLOC (union_type,
9879 (TYPE_NFIELDS (union_type)
9880 * sizeof (struct field)));
9881
9882 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9883 TYPE_NFIELDS (type) * sizeof (struct field));
9884
9885 /* Install the discriminant at index 0 in the union. */
9886 TYPE_FIELD (union_type, 0) = *disr_field;
9887 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9888 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9889
9890 /* Install the union in the outer struct type. */
9891 TYPE_FIELD_TYPE (type, 0) = union_type;
9892 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9893 TYPE_NFIELDS (type) = 1;
9894
9895 /* Set the size and offset of the union type. */
9896 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9897
9898 /* We need a way to find the correct discriminant given a
9899 variant name. For convenience we build a map here. */
9900 struct type *enum_type = FIELD_TYPE (*disr_field);
9901 std::unordered_map<std::string, ULONGEST> discriminant_map;
9902 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9903 {
9904 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9905 {
9906 const char *name
9907 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9908 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9909 }
9910 }
9911
9912 int n_fields = TYPE_NFIELDS (union_type);
9913 struct discriminant_info *disc
9914 = alloc_discriminant_info (union_type, 0, -1);
9915 /* Skip the discriminant here. */
9916 for (int i = 1; i < n_fields; ++i)
9917 {
9918 /* Find the final word in the name of this variant's type.
9919 That name can be used to look up the correct
9920 discriminant. */
9921 const char *variant_name
9922 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9923 i)));
9924
9925 auto iter = discriminant_map.find (variant_name);
9926 if (iter != discriminant_map.end ())
9927 disc->discriminants[i] = iter->second;
9928
9929 /* Remove the discriminant field, if it exists. */
9930 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9931 if (TYPE_NFIELDS (sub_type) > 0)
9932 {
9933 --TYPE_NFIELDS (sub_type);
9934 ++TYPE_FIELDS (sub_type);
9935 }
9936 TYPE_FIELD_NAME (union_type, i) = variant_name;
9937 TYPE_NAME (sub_type)
9938 = rust_fully_qualify (&objfile->objfile_obstack,
9939 TYPE_NAME (type), variant_name);
9940 }
9941 }
9942 }
9943
9944 /* Rewrite some Rust unions to be structures with variants parts. */
9945
9946 static void
9947 rust_union_quirks (struct dwarf2_cu *cu)
9948 {
9949 gdb_assert (cu->language == language_rust);
9950 for (type *type_ : cu->rust_unions)
9951 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9952 /* We don't need this any more. */
9953 cu->rust_unions.clear ();
9954 }
9955
9956 /* Return the symtab for PER_CU. This works properly regardless of
9957 whether we're using the index or psymtabs. */
9958
9959 static struct compunit_symtab *
9960 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9961 {
9962 return (per_cu->dwarf2_per_objfile->using_index
9963 ? per_cu->v.quick->compunit_symtab
9964 : per_cu->v.psymtab->compunit_symtab);
9965 }
9966
9967 /* A helper function for computing the list of all symbol tables
9968 included by PER_CU. */
9969
9970 static void
9971 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9972 htab_t all_children, htab_t all_type_symtabs,
9973 struct dwarf2_per_cu_data *per_cu,
9974 struct compunit_symtab *immediate_parent)
9975 {
9976 void **slot;
9977 struct compunit_symtab *cust;
9978
9979 slot = htab_find_slot (all_children, per_cu, INSERT);
9980 if (*slot != NULL)
9981 {
9982 /* This inclusion and its children have been processed. */
9983 return;
9984 }
9985
9986 *slot = per_cu;
9987 /* Only add a CU if it has a symbol table. */
9988 cust = get_compunit_symtab (per_cu);
9989 if (cust != NULL)
9990 {
9991 /* If this is a type unit only add its symbol table if we haven't
9992 seen it yet (type unit per_cu's can share symtabs). */
9993 if (per_cu->is_debug_types)
9994 {
9995 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9996 if (*slot == NULL)
9997 {
9998 *slot = cust;
9999 result->push_back (cust);
10000 if (cust->user == NULL)
10001 cust->user = immediate_parent;
10002 }
10003 }
10004 else
10005 {
10006 result->push_back (cust);
10007 if (cust->user == NULL)
10008 cust->user = immediate_parent;
10009 }
10010 }
10011
10012 if (!per_cu->imported_symtabs_empty ())
10013 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10014 {
10015 recursively_compute_inclusions (result, all_children,
10016 all_type_symtabs, ptr, cust);
10017 }
10018 }
10019
10020 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10021 PER_CU. */
10022
10023 static void
10024 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10025 {
10026 gdb_assert (! per_cu->is_debug_types);
10027
10028 if (!per_cu->imported_symtabs_empty ())
10029 {
10030 int len;
10031 std::vector<compunit_symtab *> result_symtabs;
10032 htab_t all_children, all_type_symtabs;
10033 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10034
10035 /* If we don't have a symtab, we can just skip this case. */
10036 if (cust == NULL)
10037 return;
10038
10039 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10040 NULL, xcalloc, xfree);
10041 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10042 NULL, xcalloc, xfree);
10043
10044 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10045 {
10046 recursively_compute_inclusions (&result_symtabs, all_children,
10047 all_type_symtabs, ptr, cust);
10048 }
10049
10050 /* Now we have a transitive closure of all the included symtabs. */
10051 len = result_symtabs.size ();
10052 cust->includes
10053 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10054 struct compunit_symtab *, len + 1);
10055 memcpy (cust->includes, result_symtabs.data (),
10056 len * sizeof (compunit_symtab *));
10057 cust->includes[len] = NULL;
10058
10059 htab_delete (all_children);
10060 htab_delete (all_type_symtabs);
10061 }
10062 }
10063
10064 /* Compute the 'includes' field for the symtabs of all the CUs we just
10065 read. */
10066
10067 static void
10068 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10069 {
10070 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10071 {
10072 if (! iter->is_debug_types)
10073 compute_compunit_symtab_includes (iter);
10074 }
10075
10076 dwarf2_per_objfile->just_read_cus.clear ();
10077 }
10078
10079 /* Generate full symbol information for PER_CU, whose DIEs have
10080 already been loaded into memory. */
10081
10082 static void
10083 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10084 enum language pretend_language)
10085 {
10086 struct dwarf2_cu *cu = per_cu->cu;
10087 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10088 struct objfile *objfile = dwarf2_per_objfile->objfile;
10089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10090 CORE_ADDR lowpc, highpc;
10091 struct compunit_symtab *cust;
10092 CORE_ADDR baseaddr;
10093 struct block *static_block;
10094 CORE_ADDR addr;
10095
10096 baseaddr = objfile->text_section_offset ();
10097
10098 /* Clear the list here in case something was left over. */
10099 cu->method_list.clear ();
10100
10101 cu->language = pretend_language;
10102 cu->language_defn = language_def (cu->language);
10103
10104 /* Do line number decoding in read_file_scope () */
10105 process_die (cu->dies, cu);
10106
10107 /* For now fudge the Go package. */
10108 if (cu->language == language_go)
10109 fixup_go_packaging (cu);
10110
10111 /* Now that we have processed all the DIEs in the CU, all the types
10112 should be complete, and it should now be safe to compute all of the
10113 physnames. */
10114 compute_delayed_physnames (cu);
10115
10116 if (cu->language == language_rust)
10117 rust_union_quirks (cu);
10118
10119 /* Some compilers don't define a DW_AT_high_pc attribute for the
10120 compilation unit. If the DW_AT_high_pc is missing, synthesize
10121 it, by scanning the DIE's below the compilation unit. */
10122 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10123
10124 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10125 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10126
10127 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10128 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10129 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10130 addrmap to help ensure it has an accurate map of pc values belonging to
10131 this comp unit. */
10132 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10133
10134 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10135 SECT_OFF_TEXT (objfile),
10136 0);
10137
10138 if (cust != NULL)
10139 {
10140 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10141
10142 /* Set symtab language to language from DW_AT_language. If the
10143 compilation is from a C file generated by language preprocessors, do
10144 not set the language if it was already deduced by start_subfile. */
10145 if (!(cu->language == language_c
10146 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10147 COMPUNIT_FILETABS (cust)->language = cu->language;
10148
10149 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10150 produce DW_AT_location with location lists but it can be possibly
10151 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10152 there were bugs in prologue debug info, fixed later in GCC-4.5
10153 by "unwind info for epilogues" patch (which is not directly related).
10154
10155 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10156 needed, it would be wrong due to missing DW_AT_producer there.
10157
10158 Still one can confuse GDB by using non-standard GCC compilation
10159 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10160 */
10161 if (cu->has_loclist && gcc_4_minor >= 5)
10162 cust->locations_valid = 1;
10163
10164 if (gcc_4_minor >= 5)
10165 cust->epilogue_unwind_valid = 1;
10166
10167 cust->call_site_htab = cu->call_site_htab;
10168 }
10169
10170 if (dwarf2_per_objfile->using_index)
10171 per_cu->v.quick->compunit_symtab = cust;
10172 else
10173 {
10174 dwarf2_psymtab *pst = per_cu->v.psymtab;
10175 pst->compunit_symtab = cust;
10176 pst->readin = true;
10177 }
10178
10179 /* Push it for inclusion processing later. */
10180 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10181
10182 /* Not needed any more. */
10183 cu->reset_builder ();
10184 }
10185
10186 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10187 already been loaded into memory. */
10188
10189 static void
10190 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10191 enum language pretend_language)
10192 {
10193 struct dwarf2_cu *cu = per_cu->cu;
10194 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10195 struct objfile *objfile = dwarf2_per_objfile->objfile;
10196 struct compunit_symtab *cust;
10197 struct signatured_type *sig_type;
10198
10199 gdb_assert (per_cu->is_debug_types);
10200 sig_type = (struct signatured_type *) per_cu;
10201
10202 /* Clear the list here in case something was left over. */
10203 cu->method_list.clear ();
10204
10205 cu->language = pretend_language;
10206 cu->language_defn = language_def (cu->language);
10207
10208 /* The symbol tables are set up in read_type_unit_scope. */
10209 process_die (cu->dies, cu);
10210
10211 /* For now fudge the Go package. */
10212 if (cu->language == language_go)
10213 fixup_go_packaging (cu);
10214
10215 /* Now that we have processed all the DIEs in the CU, all the types
10216 should be complete, and it should now be safe to compute all of the
10217 physnames. */
10218 compute_delayed_physnames (cu);
10219
10220 if (cu->language == language_rust)
10221 rust_union_quirks (cu);
10222
10223 /* TUs share symbol tables.
10224 If this is the first TU to use this symtab, complete the construction
10225 of it with end_expandable_symtab. Otherwise, complete the addition of
10226 this TU's symbols to the existing symtab. */
10227 if (sig_type->type_unit_group->compunit_symtab == NULL)
10228 {
10229 buildsym_compunit *builder = cu->get_builder ();
10230 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10231 sig_type->type_unit_group->compunit_symtab = cust;
10232
10233 if (cust != NULL)
10234 {
10235 /* Set symtab language to language from DW_AT_language. If the
10236 compilation is from a C file generated by language preprocessors,
10237 do not set the language if it was already deduced by
10238 start_subfile. */
10239 if (!(cu->language == language_c
10240 && COMPUNIT_FILETABS (cust)->language != language_c))
10241 COMPUNIT_FILETABS (cust)->language = cu->language;
10242 }
10243 }
10244 else
10245 {
10246 cu->get_builder ()->augment_type_symtab ();
10247 cust = sig_type->type_unit_group->compunit_symtab;
10248 }
10249
10250 if (dwarf2_per_objfile->using_index)
10251 per_cu->v.quick->compunit_symtab = cust;
10252 else
10253 {
10254 dwarf2_psymtab *pst = per_cu->v.psymtab;
10255 pst->compunit_symtab = cust;
10256 pst->readin = true;
10257 }
10258
10259 /* Not needed any more. */
10260 cu->reset_builder ();
10261 }
10262
10263 /* Process an imported unit DIE. */
10264
10265 static void
10266 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10267 {
10268 struct attribute *attr;
10269
10270 /* For now we don't handle imported units in type units. */
10271 if (cu->per_cu->is_debug_types)
10272 {
10273 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10274 " supported in type units [in module %s]"),
10275 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10276 }
10277
10278 attr = dwarf2_attr (die, DW_AT_import, cu);
10279 if (attr != NULL)
10280 {
10281 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10282 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10283 dwarf2_per_cu_data *per_cu
10284 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10285 cu->per_cu->dwarf2_per_objfile);
10286
10287 /* If necessary, add it to the queue and load its DIEs. */
10288 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10289 load_full_comp_unit (per_cu, false, cu->language);
10290
10291 cu->per_cu->imported_symtabs_push (per_cu);
10292 }
10293 }
10294
10295 /* RAII object that represents a process_die scope: i.e.,
10296 starts/finishes processing a DIE. */
10297 class process_die_scope
10298 {
10299 public:
10300 process_die_scope (die_info *die, dwarf2_cu *cu)
10301 : m_die (die), m_cu (cu)
10302 {
10303 /* We should only be processing DIEs not already in process. */
10304 gdb_assert (!m_die->in_process);
10305 m_die->in_process = true;
10306 }
10307
10308 ~process_die_scope ()
10309 {
10310 m_die->in_process = false;
10311
10312 /* If we're done processing the DIE for the CU that owns the line
10313 header, we don't need the line header anymore. */
10314 if (m_cu->line_header_die_owner == m_die)
10315 {
10316 delete m_cu->line_header;
10317 m_cu->line_header = NULL;
10318 m_cu->line_header_die_owner = NULL;
10319 }
10320 }
10321
10322 private:
10323 die_info *m_die;
10324 dwarf2_cu *m_cu;
10325 };
10326
10327 /* Process a die and its children. */
10328
10329 static void
10330 process_die (struct die_info *die, struct dwarf2_cu *cu)
10331 {
10332 process_die_scope scope (die, cu);
10333
10334 switch (die->tag)
10335 {
10336 case DW_TAG_padding:
10337 break;
10338 case DW_TAG_compile_unit:
10339 case DW_TAG_partial_unit:
10340 read_file_scope (die, cu);
10341 break;
10342 case DW_TAG_type_unit:
10343 read_type_unit_scope (die, cu);
10344 break;
10345 case DW_TAG_subprogram:
10346 /* Nested subprograms in Fortran get a prefix. */
10347 if (cu->language == language_fortran
10348 && die->parent != NULL
10349 && die->parent->tag == DW_TAG_subprogram)
10350 cu->processing_has_namespace_info = true;
10351 /* Fall through. */
10352 case DW_TAG_inlined_subroutine:
10353 read_func_scope (die, cu);
10354 break;
10355 case DW_TAG_lexical_block:
10356 case DW_TAG_try_block:
10357 case DW_TAG_catch_block:
10358 read_lexical_block_scope (die, cu);
10359 break;
10360 case DW_TAG_call_site:
10361 case DW_TAG_GNU_call_site:
10362 read_call_site_scope (die, cu);
10363 break;
10364 case DW_TAG_class_type:
10365 case DW_TAG_interface_type:
10366 case DW_TAG_structure_type:
10367 case DW_TAG_union_type:
10368 process_structure_scope (die, cu);
10369 break;
10370 case DW_TAG_enumeration_type:
10371 process_enumeration_scope (die, cu);
10372 break;
10373
10374 /* These dies have a type, but processing them does not create
10375 a symbol or recurse to process the children. Therefore we can
10376 read them on-demand through read_type_die. */
10377 case DW_TAG_subroutine_type:
10378 case DW_TAG_set_type:
10379 case DW_TAG_array_type:
10380 case DW_TAG_pointer_type:
10381 case DW_TAG_ptr_to_member_type:
10382 case DW_TAG_reference_type:
10383 case DW_TAG_rvalue_reference_type:
10384 case DW_TAG_string_type:
10385 break;
10386
10387 case DW_TAG_base_type:
10388 case DW_TAG_subrange_type:
10389 case DW_TAG_typedef:
10390 /* Add a typedef symbol for the type definition, if it has a
10391 DW_AT_name. */
10392 new_symbol (die, read_type_die (die, cu), cu);
10393 break;
10394 case DW_TAG_common_block:
10395 read_common_block (die, cu);
10396 break;
10397 case DW_TAG_common_inclusion:
10398 break;
10399 case DW_TAG_namespace:
10400 cu->processing_has_namespace_info = true;
10401 read_namespace (die, cu);
10402 break;
10403 case DW_TAG_module:
10404 cu->processing_has_namespace_info = true;
10405 read_module (die, cu);
10406 break;
10407 case DW_TAG_imported_declaration:
10408 cu->processing_has_namespace_info = true;
10409 if (read_namespace_alias (die, cu))
10410 break;
10411 /* The declaration is not a global namespace alias. */
10412 /* Fall through. */
10413 case DW_TAG_imported_module:
10414 cu->processing_has_namespace_info = true;
10415 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10416 || cu->language != language_fortran))
10417 complaint (_("Tag '%s' has unexpected children"),
10418 dwarf_tag_name (die->tag));
10419 read_import_statement (die, cu);
10420 break;
10421
10422 case DW_TAG_imported_unit:
10423 process_imported_unit_die (die, cu);
10424 break;
10425
10426 case DW_TAG_variable:
10427 read_variable (die, cu);
10428 break;
10429
10430 default:
10431 new_symbol (die, NULL, cu);
10432 break;
10433 }
10434 }
10435 \f
10436 /* DWARF name computation. */
10437
10438 /* A helper function for dwarf2_compute_name which determines whether DIE
10439 needs to have the name of the scope prepended to the name listed in the
10440 die. */
10441
10442 static int
10443 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10444 {
10445 struct attribute *attr;
10446
10447 switch (die->tag)
10448 {
10449 case DW_TAG_namespace:
10450 case DW_TAG_typedef:
10451 case DW_TAG_class_type:
10452 case DW_TAG_interface_type:
10453 case DW_TAG_structure_type:
10454 case DW_TAG_union_type:
10455 case DW_TAG_enumeration_type:
10456 case DW_TAG_enumerator:
10457 case DW_TAG_subprogram:
10458 case DW_TAG_inlined_subroutine:
10459 case DW_TAG_member:
10460 case DW_TAG_imported_declaration:
10461 return 1;
10462
10463 case DW_TAG_variable:
10464 case DW_TAG_constant:
10465 /* We only need to prefix "globally" visible variables. These include
10466 any variable marked with DW_AT_external or any variable that
10467 lives in a namespace. [Variables in anonymous namespaces
10468 require prefixing, but they are not DW_AT_external.] */
10469
10470 if (dwarf2_attr (die, DW_AT_specification, cu))
10471 {
10472 struct dwarf2_cu *spec_cu = cu;
10473
10474 return die_needs_namespace (die_specification (die, &spec_cu),
10475 spec_cu);
10476 }
10477
10478 attr = dwarf2_attr (die, DW_AT_external, cu);
10479 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10480 && die->parent->tag != DW_TAG_module)
10481 return 0;
10482 /* A variable in a lexical block of some kind does not need a
10483 namespace, even though in C++ such variables may be external
10484 and have a mangled name. */
10485 if (die->parent->tag == DW_TAG_lexical_block
10486 || die->parent->tag == DW_TAG_try_block
10487 || die->parent->tag == DW_TAG_catch_block
10488 || die->parent->tag == DW_TAG_subprogram)
10489 return 0;
10490 return 1;
10491
10492 default:
10493 return 0;
10494 }
10495 }
10496
10497 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10498 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10499 defined for the given DIE. */
10500
10501 static struct attribute *
10502 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10503 {
10504 struct attribute *attr;
10505
10506 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10507 if (attr == NULL)
10508 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10509
10510 return attr;
10511 }
10512
10513 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10514 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10515 defined for the given DIE. */
10516
10517 static const char *
10518 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10519 {
10520 const char *linkage_name;
10521
10522 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10523 if (linkage_name == NULL)
10524 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10525
10526 return linkage_name;
10527 }
10528
10529 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10530 compute the physname for the object, which include a method's:
10531 - formal parameters (C++),
10532 - receiver type (Go),
10533
10534 The term "physname" is a bit confusing.
10535 For C++, for example, it is the demangled name.
10536 For Go, for example, it's the mangled name.
10537
10538 For Ada, return the DIE's linkage name rather than the fully qualified
10539 name. PHYSNAME is ignored..
10540
10541 The result is allocated on the objfile_obstack and canonicalized. */
10542
10543 static const char *
10544 dwarf2_compute_name (const char *name,
10545 struct die_info *die, struct dwarf2_cu *cu,
10546 int physname)
10547 {
10548 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10549
10550 if (name == NULL)
10551 name = dwarf2_name (die, cu);
10552
10553 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10554 but otherwise compute it by typename_concat inside GDB.
10555 FIXME: Actually this is not really true, or at least not always true.
10556 It's all very confusing. compute_and_set_names doesn't try to demangle
10557 Fortran names because there is no mangling standard. So new_symbol
10558 will set the demangled name to the result of dwarf2_full_name, and it is
10559 the demangled name that GDB uses if it exists. */
10560 if (cu->language == language_ada
10561 || (cu->language == language_fortran && physname))
10562 {
10563 /* For Ada unit, we prefer the linkage name over the name, as
10564 the former contains the exported name, which the user expects
10565 to be able to reference. Ideally, we want the user to be able
10566 to reference this entity using either natural or linkage name,
10567 but we haven't started looking at this enhancement yet. */
10568 const char *linkage_name = dw2_linkage_name (die, cu);
10569
10570 if (linkage_name != NULL)
10571 return linkage_name;
10572 }
10573
10574 /* These are the only languages we know how to qualify names in. */
10575 if (name != NULL
10576 && (cu->language == language_cplus
10577 || cu->language == language_fortran || cu->language == language_d
10578 || cu->language == language_rust))
10579 {
10580 if (die_needs_namespace (die, cu))
10581 {
10582 const char *prefix;
10583 const char *canonical_name = NULL;
10584
10585 string_file buf;
10586
10587 prefix = determine_prefix (die, cu);
10588 if (*prefix != '\0')
10589 {
10590 gdb::unique_xmalloc_ptr<char> prefixed_name
10591 (typename_concat (NULL, prefix, name, physname, cu));
10592
10593 buf.puts (prefixed_name.get ());
10594 }
10595 else
10596 buf.puts (name);
10597
10598 /* Template parameters may be specified in the DIE's DW_AT_name, or
10599 as children with DW_TAG_template_type_param or
10600 DW_TAG_value_type_param. If the latter, add them to the name
10601 here. If the name already has template parameters, then
10602 skip this step; some versions of GCC emit both, and
10603 it is more efficient to use the pre-computed name.
10604
10605 Something to keep in mind about this process: it is very
10606 unlikely, or in some cases downright impossible, to produce
10607 something that will match the mangled name of a function.
10608 If the definition of the function has the same debug info,
10609 we should be able to match up with it anyway. But fallbacks
10610 using the minimal symbol, for instance to find a method
10611 implemented in a stripped copy of libstdc++, will not work.
10612 If we do not have debug info for the definition, we will have to
10613 match them up some other way.
10614
10615 When we do name matching there is a related problem with function
10616 templates; two instantiated function templates are allowed to
10617 differ only by their return types, which we do not add here. */
10618
10619 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10620 {
10621 struct attribute *attr;
10622 struct die_info *child;
10623 int first = 1;
10624
10625 die->building_fullname = 1;
10626
10627 for (child = die->child; child != NULL; child = child->sibling)
10628 {
10629 struct type *type;
10630 LONGEST value;
10631 const gdb_byte *bytes;
10632 struct dwarf2_locexpr_baton *baton;
10633 struct value *v;
10634
10635 if (child->tag != DW_TAG_template_type_param
10636 && child->tag != DW_TAG_template_value_param)
10637 continue;
10638
10639 if (first)
10640 {
10641 buf.puts ("<");
10642 first = 0;
10643 }
10644 else
10645 buf.puts (", ");
10646
10647 attr = dwarf2_attr (child, DW_AT_type, cu);
10648 if (attr == NULL)
10649 {
10650 complaint (_("template parameter missing DW_AT_type"));
10651 buf.puts ("UNKNOWN_TYPE");
10652 continue;
10653 }
10654 type = die_type (child, cu);
10655
10656 if (child->tag == DW_TAG_template_type_param)
10657 {
10658 c_print_type (type, "", &buf, -1, 0, cu->language,
10659 &type_print_raw_options);
10660 continue;
10661 }
10662
10663 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10664 if (attr == NULL)
10665 {
10666 complaint (_("template parameter missing "
10667 "DW_AT_const_value"));
10668 buf.puts ("UNKNOWN_VALUE");
10669 continue;
10670 }
10671
10672 dwarf2_const_value_attr (attr, type, name,
10673 &cu->comp_unit_obstack, cu,
10674 &value, &bytes, &baton);
10675
10676 if (TYPE_NOSIGN (type))
10677 /* GDB prints characters as NUMBER 'CHAR'. If that's
10678 changed, this can use value_print instead. */
10679 c_printchar (value, type, &buf);
10680 else
10681 {
10682 struct value_print_options opts;
10683
10684 if (baton != NULL)
10685 v = dwarf2_evaluate_loc_desc (type, NULL,
10686 baton->data,
10687 baton->size,
10688 baton->per_cu);
10689 else if (bytes != NULL)
10690 {
10691 v = allocate_value (type);
10692 memcpy (value_contents_writeable (v), bytes,
10693 TYPE_LENGTH (type));
10694 }
10695 else
10696 v = value_from_longest (type, value);
10697
10698 /* Specify decimal so that we do not depend on
10699 the radix. */
10700 get_formatted_print_options (&opts, 'd');
10701 opts.raw = 1;
10702 value_print (v, &buf, &opts);
10703 release_value (v);
10704 }
10705 }
10706
10707 die->building_fullname = 0;
10708
10709 if (!first)
10710 {
10711 /* Close the argument list, with a space if necessary
10712 (nested templates). */
10713 if (!buf.empty () && buf.string ().back () == '>')
10714 buf.puts (" >");
10715 else
10716 buf.puts (">");
10717 }
10718 }
10719
10720 /* For C++ methods, append formal parameter type
10721 information, if PHYSNAME. */
10722
10723 if (physname && die->tag == DW_TAG_subprogram
10724 && cu->language == language_cplus)
10725 {
10726 struct type *type = read_type_die (die, cu);
10727
10728 c_type_print_args (type, &buf, 1, cu->language,
10729 &type_print_raw_options);
10730
10731 if (cu->language == language_cplus)
10732 {
10733 /* Assume that an artificial first parameter is
10734 "this", but do not crash if it is not. RealView
10735 marks unnamed (and thus unused) parameters as
10736 artificial; there is no way to differentiate
10737 the two cases. */
10738 if (TYPE_NFIELDS (type) > 0
10739 && TYPE_FIELD_ARTIFICIAL (type, 0)
10740 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10741 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10742 0))))
10743 buf.puts (" const");
10744 }
10745 }
10746
10747 const std::string &intermediate_name = buf.string ();
10748
10749 if (cu->language == language_cplus)
10750 canonical_name
10751 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10752 &objfile->per_bfd->storage_obstack);
10753
10754 /* If we only computed INTERMEDIATE_NAME, or if
10755 INTERMEDIATE_NAME is already canonical, then we need to
10756 copy it to the appropriate obstack. */
10757 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10758 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10759 intermediate_name);
10760 else
10761 name = canonical_name;
10762 }
10763 }
10764
10765 return name;
10766 }
10767
10768 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10769 If scope qualifiers are appropriate they will be added. The result
10770 will be allocated on the storage_obstack, or NULL if the DIE does
10771 not have a name. NAME may either be from a previous call to
10772 dwarf2_name or NULL.
10773
10774 The output string will be canonicalized (if C++). */
10775
10776 static const char *
10777 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10778 {
10779 return dwarf2_compute_name (name, die, cu, 0);
10780 }
10781
10782 /* Construct a physname for the given DIE in CU. NAME may either be
10783 from a previous call to dwarf2_name or NULL. The result will be
10784 allocated on the objfile_objstack or NULL if the DIE does not have a
10785 name.
10786
10787 The output string will be canonicalized (if C++). */
10788
10789 static const char *
10790 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10791 {
10792 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10793 const char *retval, *mangled = NULL, *canon = NULL;
10794 int need_copy = 1;
10795
10796 /* In this case dwarf2_compute_name is just a shortcut not building anything
10797 on its own. */
10798 if (!die_needs_namespace (die, cu))
10799 return dwarf2_compute_name (name, die, cu, 1);
10800
10801 mangled = dw2_linkage_name (die, cu);
10802
10803 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10804 See https://github.com/rust-lang/rust/issues/32925. */
10805 if (cu->language == language_rust && mangled != NULL
10806 && strchr (mangled, '{') != NULL)
10807 mangled = NULL;
10808
10809 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10810 has computed. */
10811 gdb::unique_xmalloc_ptr<char> demangled;
10812 if (mangled != NULL)
10813 {
10814
10815 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10816 {
10817 /* Do nothing (do not demangle the symbol name). */
10818 }
10819 else if (cu->language == language_go)
10820 {
10821 /* This is a lie, but we already lie to the caller new_symbol.
10822 new_symbol assumes we return the mangled name.
10823 This just undoes that lie until things are cleaned up. */
10824 }
10825 else
10826 {
10827 /* Use DMGL_RET_DROP for C++ template functions to suppress
10828 their return type. It is easier for GDB users to search
10829 for such functions as `name(params)' than `long name(params)'.
10830 In such case the minimal symbol names do not match the full
10831 symbol names but for template functions there is never a need
10832 to look up their definition from their declaration so
10833 the only disadvantage remains the minimal symbol variant
10834 `long name(params)' does not have the proper inferior type. */
10835 demangled.reset (gdb_demangle (mangled,
10836 (DMGL_PARAMS | DMGL_ANSI
10837 | DMGL_RET_DROP)));
10838 }
10839 if (demangled)
10840 canon = demangled.get ();
10841 else
10842 {
10843 canon = mangled;
10844 need_copy = 0;
10845 }
10846 }
10847
10848 if (canon == NULL || check_physname)
10849 {
10850 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10851
10852 if (canon != NULL && strcmp (physname, canon) != 0)
10853 {
10854 /* It may not mean a bug in GDB. The compiler could also
10855 compute DW_AT_linkage_name incorrectly. But in such case
10856 GDB would need to be bug-to-bug compatible. */
10857
10858 complaint (_("Computed physname <%s> does not match demangled <%s> "
10859 "(from linkage <%s>) - DIE at %s [in module %s]"),
10860 physname, canon, mangled, sect_offset_str (die->sect_off),
10861 objfile_name (objfile));
10862
10863 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10864 is available here - over computed PHYSNAME. It is safer
10865 against both buggy GDB and buggy compilers. */
10866
10867 retval = canon;
10868 }
10869 else
10870 {
10871 retval = physname;
10872 need_copy = 0;
10873 }
10874 }
10875 else
10876 retval = canon;
10877
10878 if (need_copy)
10879 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10880
10881 return retval;
10882 }
10883
10884 /* Inspect DIE in CU for a namespace alias. If one exists, record
10885 a new symbol for it.
10886
10887 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10888
10889 static int
10890 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10891 {
10892 struct attribute *attr;
10893
10894 /* If the die does not have a name, this is not a namespace
10895 alias. */
10896 attr = dwarf2_attr (die, DW_AT_name, cu);
10897 if (attr != NULL)
10898 {
10899 int num;
10900 struct die_info *d = die;
10901 struct dwarf2_cu *imported_cu = cu;
10902
10903 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10904 keep inspecting DIEs until we hit the underlying import. */
10905 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10906 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10907 {
10908 attr = dwarf2_attr (d, DW_AT_import, cu);
10909 if (attr == NULL)
10910 break;
10911
10912 d = follow_die_ref (d, attr, &imported_cu);
10913 if (d->tag != DW_TAG_imported_declaration)
10914 break;
10915 }
10916
10917 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10918 {
10919 complaint (_("DIE at %s has too many recursively imported "
10920 "declarations"), sect_offset_str (d->sect_off));
10921 return 0;
10922 }
10923
10924 if (attr != NULL)
10925 {
10926 struct type *type;
10927 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10928
10929 type = get_die_type_at_offset (sect_off, cu->per_cu);
10930 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10931 {
10932 /* This declaration is a global namespace alias. Add
10933 a symbol for it whose type is the aliased namespace. */
10934 new_symbol (die, type, cu);
10935 return 1;
10936 }
10937 }
10938 }
10939
10940 return 0;
10941 }
10942
10943 /* Return the using directives repository (global or local?) to use in the
10944 current context for CU.
10945
10946 For Ada, imported declarations can materialize renamings, which *may* be
10947 global. However it is impossible (for now?) in DWARF to distinguish
10948 "external" imported declarations and "static" ones. As all imported
10949 declarations seem to be static in all other languages, make them all CU-wide
10950 global only in Ada. */
10951
10952 static struct using_direct **
10953 using_directives (struct dwarf2_cu *cu)
10954 {
10955 if (cu->language == language_ada
10956 && cu->get_builder ()->outermost_context_p ())
10957 return cu->get_builder ()->get_global_using_directives ();
10958 else
10959 return cu->get_builder ()->get_local_using_directives ();
10960 }
10961
10962 /* Read the import statement specified by the given die and record it. */
10963
10964 static void
10965 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10966 {
10967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10968 struct attribute *import_attr;
10969 struct die_info *imported_die, *child_die;
10970 struct dwarf2_cu *imported_cu;
10971 const char *imported_name;
10972 const char *imported_name_prefix;
10973 const char *canonical_name;
10974 const char *import_alias;
10975 const char *imported_declaration = NULL;
10976 const char *import_prefix;
10977 std::vector<const char *> excludes;
10978
10979 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10980 if (import_attr == NULL)
10981 {
10982 complaint (_("Tag '%s' has no DW_AT_import"),
10983 dwarf_tag_name (die->tag));
10984 return;
10985 }
10986
10987 imported_cu = cu;
10988 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10989 imported_name = dwarf2_name (imported_die, imported_cu);
10990 if (imported_name == NULL)
10991 {
10992 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10993
10994 The import in the following code:
10995 namespace A
10996 {
10997 typedef int B;
10998 }
10999
11000 int main ()
11001 {
11002 using A::B;
11003 B b;
11004 return b;
11005 }
11006
11007 ...
11008 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11009 <52> DW_AT_decl_file : 1
11010 <53> DW_AT_decl_line : 6
11011 <54> DW_AT_import : <0x75>
11012 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11013 <59> DW_AT_name : B
11014 <5b> DW_AT_decl_file : 1
11015 <5c> DW_AT_decl_line : 2
11016 <5d> DW_AT_type : <0x6e>
11017 ...
11018 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11019 <76> DW_AT_byte_size : 4
11020 <77> DW_AT_encoding : 5 (signed)
11021
11022 imports the wrong die ( 0x75 instead of 0x58 ).
11023 This case will be ignored until the gcc bug is fixed. */
11024 return;
11025 }
11026
11027 /* Figure out the local name after import. */
11028 import_alias = dwarf2_name (die, cu);
11029
11030 /* Figure out where the statement is being imported to. */
11031 import_prefix = determine_prefix (die, cu);
11032
11033 /* Figure out what the scope of the imported die is and prepend it
11034 to the name of the imported die. */
11035 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11036
11037 if (imported_die->tag != DW_TAG_namespace
11038 && imported_die->tag != DW_TAG_module)
11039 {
11040 imported_declaration = imported_name;
11041 canonical_name = imported_name_prefix;
11042 }
11043 else if (strlen (imported_name_prefix) > 0)
11044 canonical_name = obconcat (&objfile->objfile_obstack,
11045 imported_name_prefix,
11046 (cu->language == language_d ? "." : "::"),
11047 imported_name, (char *) NULL);
11048 else
11049 canonical_name = imported_name;
11050
11051 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11052 for (child_die = die->child; child_die && child_die->tag;
11053 child_die = sibling_die (child_die))
11054 {
11055 /* DWARF-4: A Fortran use statement with a “rename list” may be
11056 represented by an imported module entry with an import attribute
11057 referring to the module and owned entries corresponding to those
11058 entities that are renamed as part of being imported. */
11059
11060 if (child_die->tag != DW_TAG_imported_declaration)
11061 {
11062 complaint (_("child DW_TAG_imported_declaration expected "
11063 "- DIE at %s [in module %s]"),
11064 sect_offset_str (child_die->sect_off),
11065 objfile_name (objfile));
11066 continue;
11067 }
11068
11069 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11070 if (import_attr == NULL)
11071 {
11072 complaint (_("Tag '%s' has no DW_AT_import"),
11073 dwarf_tag_name (child_die->tag));
11074 continue;
11075 }
11076
11077 imported_cu = cu;
11078 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11079 &imported_cu);
11080 imported_name = dwarf2_name (imported_die, imported_cu);
11081 if (imported_name == NULL)
11082 {
11083 complaint (_("child DW_TAG_imported_declaration has unknown "
11084 "imported name - DIE at %s [in module %s]"),
11085 sect_offset_str (child_die->sect_off),
11086 objfile_name (objfile));
11087 continue;
11088 }
11089
11090 excludes.push_back (imported_name);
11091
11092 process_die (child_die, cu);
11093 }
11094
11095 add_using_directive (using_directives (cu),
11096 import_prefix,
11097 canonical_name,
11098 import_alias,
11099 imported_declaration,
11100 excludes,
11101 0,
11102 &objfile->objfile_obstack);
11103 }
11104
11105 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11106 types, but gives them a size of zero. Starting with version 14,
11107 ICC is compatible with GCC. */
11108
11109 static bool
11110 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11111 {
11112 if (!cu->checked_producer)
11113 check_producer (cu);
11114
11115 return cu->producer_is_icc_lt_14;
11116 }
11117
11118 /* ICC generates a DW_AT_type for C void functions. This was observed on
11119 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11120 which says that void functions should not have a DW_AT_type. */
11121
11122 static bool
11123 producer_is_icc (struct dwarf2_cu *cu)
11124 {
11125 if (!cu->checked_producer)
11126 check_producer (cu);
11127
11128 return cu->producer_is_icc;
11129 }
11130
11131 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11132 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11133 this, it was first present in GCC release 4.3.0. */
11134
11135 static bool
11136 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11137 {
11138 if (!cu->checked_producer)
11139 check_producer (cu);
11140
11141 return cu->producer_is_gcc_lt_4_3;
11142 }
11143
11144 static file_and_directory
11145 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11146 {
11147 file_and_directory res;
11148
11149 /* Find the filename. Do not use dwarf2_name here, since the filename
11150 is not a source language identifier. */
11151 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11152 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11153
11154 if (res.comp_dir == NULL
11155 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11156 && IS_ABSOLUTE_PATH (res.name))
11157 {
11158 res.comp_dir_storage = ldirname (res.name);
11159 if (!res.comp_dir_storage.empty ())
11160 res.comp_dir = res.comp_dir_storage.c_str ();
11161 }
11162 if (res.comp_dir != NULL)
11163 {
11164 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11165 directory, get rid of it. */
11166 const char *cp = strchr (res.comp_dir, ':');
11167
11168 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11169 res.comp_dir = cp + 1;
11170 }
11171
11172 if (res.name == NULL)
11173 res.name = "<unknown>";
11174
11175 return res;
11176 }
11177
11178 /* Handle DW_AT_stmt_list for a compilation unit.
11179 DIE is the DW_TAG_compile_unit die for CU.
11180 COMP_DIR is the compilation directory. LOWPC is passed to
11181 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11182
11183 static void
11184 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11185 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11186 {
11187 struct dwarf2_per_objfile *dwarf2_per_objfile
11188 = cu->per_cu->dwarf2_per_objfile;
11189 struct objfile *objfile = dwarf2_per_objfile->objfile;
11190 struct attribute *attr;
11191 struct line_header line_header_local;
11192 hashval_t line_header_local_hash;
11193 void **slot;
11194 int decode_mapping;
11195
11196 gdb_assert (! cu->per_cu->is_debug_types);
11197
11198 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11199 if (attr == NULL)
11200 return;
11201
11202 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11203
11204 /* The line header hash table is only created if needed (it exists to
11205 prevent redundant reading of the line table for partial_units).
11206 If we're given a partial_unit, we'll need it. If we're given a
11207 compile_unit, then use the line header hash table if it's already
11208 created, but don't create one just yet. */
11209
11210 if (dwarf2_per_objfile->line_header_hash == NULL
11211 && die->tag == DW_TAG_partial_unit)
11212 {
11213 dwarf2_per_objfile->line_header_hash
11214 = htab_create_alloc_ex (127, line_header_hash_voidp,
11215 line_header_eq_voidp,
11216 free_line_header_voidp,
11217 &objfile->objfile_obstack,
11218 hashtab_obstack_allocate,
11219 dummy_obstack_deallocate);
11220 }
11221
11222 line_header_local.sect_off = line_offset;
11223 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11224 line_header_local_hash = line_header_hash (&line_header_local);
11225 if (dwarf2_per_objfile->line_header_hash != NULL)
11226 {
11227 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11228 &line_header_local,
11229 line_header_local_hash, NO_INSERT);
11230
11231 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11232 is not present in *SLOT (since if there is something in *SLOT then
11233 it will be for a partial_unit). */
11234 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11235 {
11236 gdb_assert (*slot != NULL);
11237 cu->line_header = (struct line_header *) *slot;
11238 return;
11239 }
11240 }
11241
11242 /* dwarf_decode_line_header does not yet provide sufficient information.
11243 We always have to call also dwarf_decode_lines for it. */
11244 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11245 if (lh == NULL)
11246 return;
11247
11248 cu->line_header = lh.release ();
11249 cu->line_header_die_owner = die;
11250
11251 if (dwarf2_per_objfile->line_header_hash == NULL)
11252 slot = NULL;
11253 else
11254 {
11255 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11256 &line_header_local,
11257 line_header_local_hash, INSERT);
11258 gdb_assert (slot != NULL);
11259 }
11260 if (slot != NULL && *slot == NULL)
11261 {
11262 /* This newly decoded line number information unit will be owned
11263 by line_header_hash hash table. */
11264 *slot = cu->line_header;
11265 cu->line_header_die_owner = NULL;
11266 }
11267 else
11268 {
11269 /* We cannot free any current entry in (*slot) as that struct line_header
11270 may be already used by multiple CUs. Create only temporary decoded
11271 line_header for this CU - it may happen at most once for each line
11272 number information unit. And if we're not using line_header_hash
11273 then this is what we want as well. */
11274 gdb_assert (die->tag != DW_TAG_partial_unit);
11275 }
11276 decode_mapping = (die->tag != DW_TAG_partial_unit);
11277 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11278 decode_mapping);
11279
11280 }
11281
11282 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11283
11284 static void
11285 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11286 {
11287 struct dwarf2_per_objfile *dwarf2_per_objfile
11288 = cu->per_cu->dwarf2_per_objfile;
11289 struct objfile *objfile = dwarf2_per_objfile->objfile;
11290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11291 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11292 CORE_ADDR highpc = ((CORE_ADDR) 0);
11293 struct attribute *attr;
11294 struct die_info *child_die;
11295 CORE_ADDR baseaddr;
11296
11297 prepare_one_comp_unit (cu, die, cu->language);
11298 baseaddr = objfile->text_section_offset ();
11299
11300 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11301
11302 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11303 from finish_block. */
11304 if (lowpc == ((CORE_ADDR) -1))
11305 lowpc = highpc;
11306 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11307
11308 file_and_directory fnd = find_file_and_directory (die, cu);
11309
11310 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11311 standardised yet. As a workaround for the language detection we fall
11312 back to the DW_AT_producer string. */
11313 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11314 cu->language = language_opencl;
11315
11316 /* Similar hack for Go. */
11317 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11318 set_cu_language (DW_LANG_Go, cu);
11319
11320 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11321
11322 /* Decode line number information if present. We do this before
11323 processing child DIEs, so that the line header table is available
11324 for DW_AT_decl_file. */
11325 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11326
11327 /* Process all dies in compilation unit. */
11328 if (die->child != NULL)
11329 {
11330 child_die = die->child;
11331 while (child_die && child_die->tag)
11332 {
11333 process_die (child_die, cu);
11334 child_die = sibling_die (child_die);
11335 }
11336 }
11337
11338 /* Decode macro information, if present. Dwarf 2 macro information
11339 refers to information in the line number info statement program
11340 header, so we can only read it if we've read the header
11341 successfully. */
11342 attr = dwarf2_attr (die, DW_AT_macros, cu);
11343 if (attr == NULL)
11344 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11345 if (attr && cu->line_header)
11346 {
11347 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11348 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11349
11350 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11351 }
11352 else
11353 {
11354 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11355 if (attr && cu->line_header)
11356 {
11357 unsigned int macro_offset = DW_UNSND (attr);
11358
11359 dwarf_decode_macros (cu, macro_offset, 0);
11360 }
11361 }
11362 }
11363
11364 void
11365 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11366 {
11367 struct type_unit_group *tu_group;
11368 int first_time;
11369 struct attribute *attr;
11370 unsigned int i;
11371 struct signatured_type *sig_type;
11372
11373 gdb_assert (per_cu->is_debug_types);
11374 sig_type = (struct signatured_type *) per_cu;
11375
11376 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11377
11378 /* If we're using .gdb_index (includes -readnow) then
11379 per_cu->type_unit_group may not have been set up yet. */
11380 if (sig_type->type_unit_group == NULL)
11381 sig_type->type_unit_group = get_type_unit_group (this, attr);
11382 tu_group = sig_type->type_unit_group;
11383
11384 /* If we've already processed this stmt_list there's no real need to
11385 do it again, we could fake it and just recreate the part we need
11386 (file name,index -> symtab mapping). If data shows this optimization
11387 is useful we can do it then. */
11388 first_time = tu_group->compunit_symtab == NULL;
11389
11390 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11391 debug info. */
11392 line_header_up lh;
11393 if (attr != NULL)
11394 {
11395 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11396 lh = dwarf_decode_line_header (line_offset, this);
11397 }
11398 if (lh == NULL)
11399 {
11400 if (first_time)
11401 start_symtab ("", NULL, 0);
11402 else
11403 {
11404 gdb_assert (tu_group->symtabs == NULL);
11405 gdb_assert (m_builder == nullptr);
11406 struct compunit_symtab *cust = tu_group->compunit_symtab;
11407 m_builder.reset (new struct buildsym_compunit
11408 (COMPUNIT_OBJFILE (cust), "",
11409 COMPUNIT_DIRNAME (cust),
11410 compunit_language (cust),
11411 0, cust));
11412 }
11413 return;
11414 }
11415
11416 line_header = lh.release ();
11417 line_header_die_owner = die;
11418
11419 if (first_time)
11420 {
11421 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11422
11423 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11424 still initializing it, and our caller (a few levels up)
11425 process_full_type_unit still needs to know if this is the first
11426 time. */
11427
11428 tu_group->num_symtabs = line_header->file_names_size ();
11429 tu_group->symtabs = XNEWVEC (struct symtab *,
11430 line_header->file_names_size ());
11431
11432 auto &file_names = line_header->file_names ();
11433 for (i = 0; i < file_names.size (); ++i)
11434 {
11435 file_entry &fe = file_names[i];
11436 dwarf2_start_subfile (this, fe.name,
11437 fe.include_dir (line_header));
11438 buildsym_compunit *b = get_builder ();
11439 if (b->get_current_subfile ()->symtab == NULL)
11440 {
11441 /* NOTE: start_subfile will recognize when it's been
11442 passed a file it has already seen. So we can't
11443 assume there's a simple mapping from
11444 cu->line_header->file_names to subfiles, plus
11445 cu->line_header->file_names may contain dups. */
11446 b->get_current_subfile ()->symtab
11447 = allocate_symtab (cust, b->get_current_subfile ()->name);
11448 }
11449
11450 fe.symtab = b->get_current_subfile ()->symtab;
11451 tu_group->symtabs[i] = fe.symtab;
11452 }
11453 }
11454 else
11455 {
11456 gdb_assert (m_builder == nullptr);
11457 struct compunit_symtab *cust = tu_group->compunit_symtab;
11458 m_builder.reset (new struct buildsym_compunit
11459 (COMPUNIT_OBJFILE (cust), "",
11460 COMPUNIT_DIRNAME (cust),
11461 compunit_language (cust),
11462 0, cust));
11463
11464 auto &file_names = line_header->file_names ();
11465 for (i = 0; i < file_names.size (); ++i)
11466 {
11467 file_entry &fe = file_names[i];
11468 fe.symtab = tu_group->symtabs[i];
11469 }
11470 }
11471
11472 /* The main symtab is allocated last. Type units don't have DW_AT_name
11473 so they don't have a "real" (so to speak) symtab anyway.
11474 There is later code that will assign the main symtab to all symbols
11475 that don't have one. We need to handle the case of a symbol with a
11476 missing symtab (DW_AT_decl_file) anyway. */
11477 }
11478
11479 /* Process DW_TAG_type_unit.
11480 For TUs we want to skip the first top level sibling if it's not the
11481 actual type being defined by this TU. In this case the first top
11482 level sibling is there to provide context only. */
11483
11484 static void
11485 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11486 {
11487 struct die_info *child_die;
11488
11489 prepare_one_comp_unit (cu, die, language_minimal);
11490
11491 /* Initialize (or reinitialize) the machinery for building symtabs.
11492 We do this before processing child DIEs, so that the line header table
11493 is available for DW_AT_decl_file. */
11494 cu->setup_type_unit_groups (die);
11495
11496 if (die->child != NULL)
11497 {
11498 child_die = die->child;
11499 while (child_die && child_die->tag)
11500 {
11501 process_die (child_die, cu);
11502 child_die = sibling_die (child_die);
11503 }
11504 }
11505 }
11506 \f
11507 /* DWO/DWP files.
11508
11509 http://gcc.gnu.org/wiki/DebugFission
11510 http://gcc.gnu.org/wiki/DebugFissionDWP
11511
11512 To simplify handling of both DWO files ("object" files with the DWARF info)
11513 and DWP files (a file with the DWOs packaged up into one file), we treat
11514 DWP files as having a collection of virtual DWO files. */
11515
11516 static hashval_t
11517 hash_dwo_file (const void *item)
11518 {
11519 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11520 hashval_t hash;
11521
11522 hash = htab_hash_string (dwo_file->dwo_name);
11523 if (dwo_file->comp_dir != NULL)
11524 hash += htab_hash_string (dwo_file->comp_dir);
11525 return hash;
11526 }
11527
11528 static int
11529 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11530 {
11531 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11532 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11533
11534 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11535 return 0;
11536 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11537 return lhs->comp_dir == rhs->comp_dir;
11538 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11539 }
11540
11541 /* Allocate a hash table for DWO files. */
11542
11543 static htab_up
11544 allocate_dwo_file_hash_table (struct objfile *objfile)
11545 {
11546 auto delete_dwo_file = [] (void *item)
11547 {
11548 struct dwo_file *dwo_file = (struct dwo_file *) item;
11549
11550 delete dwo_file;
11551 };
11552
11553 return htab_up (htab_create_alloc_ex (41,
11554 hash_dwo_file,
11555 eq_dwo_file,
11556 delete_dwo_file,
11557 &objfile->objfile_obstack,
11558 hashtab_obstack_allocate,
11559 dummy_obstack_deallocate));
11560 }
11561
11562 /* Lookup DWO file DWO_NAME. */
11563
11564 static void **
11565 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11566 const char *dwo_name,
11567 const char *comp_dir)
11568 {
11569 struct dwo_file find_entry;
11570 void **slot;
11571
11572 if (dwarf2_per_objfile->dwo_files == NULL)
11573 dwarf2_per_objfile->dwo_files
11574 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11575
11576 find_entry.dwo_name = dwo_name;
11577 find_entry.comp_dir = comp_dir;
11578 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11579 INSERT);
11580
11581 return slot;
11582 }
11583
11584 static hashval_t
11585 hash_dwo_unit (const void *item)
11586 {
11587 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11588
11589 /* This drops the top 32 bits of the id, but is ok for a hash. */
11590 return dwo_unit->signature;
11591 }
11592
11593 static int
11594 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11595 {
11596 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11597 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11598
11599 /* The signature is assumed to be unique within the DWO file.
11600 So while object file CU dwo_id's always have the value zero,
11601 that's OK, assuming each object file DWO file has only one CU,
11602 and that's the rule for now. */
11603 return lhs->signature == rhs->signature;
11604 }
11605
11606 /* Allocate a hash table for DWO CUs,TUs.
11607 There is one of these tables for each of CUs,TUs for each DWO file. */
11608
11609 static htab_t
11610 allocate_dwo_unit_table (struct objfile *objfile)
11611 {
11612 /* Start out with a pretty small number.
11613 Generally DWO files contain only one CU and maybe some TUs. */
11614 return htab_create_alloc_ex (3,
11615 hash_dwo_unit,
11616 eq_dwo_unit,
11617 NULL,
11618 &objfile->objfile_obstack,
11619 hashtab_obstack_allocate,
11620 dummy_obstack_deallocate);
11621 }
11622
11623 /* die_reader_func for create_dwo_cu. */
11624
11625 static void
11626 create_dwo_cu_reader (const struct die_reader_specs *reader,
11627 const gdb_byte *info_ptr,
11628 struct die_info *comp_unit_die,
11629 int has_children,
11630 struct dwo_file *dwo_file,
11631 struct dwo_unit *dwo_unit)
11632 {
11633 struct dwarf2_cu *cu = reader->cu;
11634 sect_offset sect_off = cu->per_cu->sect_off;
11635 struct dwarf2_section_info *section = cu->per_cu->section;
11636
11637 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11638 if (!signature.has_value ())
11639 {
11640 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11641 " its dwo_id [in module %s]"),
11642 sect_offset_str (sect_off), dwo_file->dwo_name);
11643 return;
11644 }
11645
11646 dwo_unit->dwo_file = dwo_file;
11647 dwo_unit->signature = *signature;
11648 dwo_unit->section = section;
11649 dwo_unit->sect_off = sect_off;
11650 dwo_unit->length = cu->per_cu->length;
11651
11652 if (dwarf_read_debug)
11653 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11654 sect_offset_str (sect_off),
11655 hex_string (dwo_unit->signature));
11656 }
11657
11658 /* Create the dwo_units for the CUs in a DWO_FILE.
11659 Note: This function processes DWO files only, not DWP files. */
11660
11661 static void
11662 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11663 dwarf2_cu *cu, struct dwo_file &dwo_file,
11664 dwarf2_section_info &section, htab_t &cus_htab)
11665 {
11666 struct objfile *objfile = dwarf2_per_objfile->objfile;
11667 const gdb_byte *info_ptr, *end_ptr;
11668
11669 section.read (objfile);
11670 info_ptr = section.buffer;
11671
11672 if (info_ptr == NULL)
11673 return;
11674
11675 if (dwarf_read_debug)
11676 {
11677 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11678 section.get_name (),
11679 section.get_file_name ());
11680 }
11681
11682 end_ptr = info_ptr + section.size;
11683 while (info_ptr < end_ptr)
11684 {
11685 struct dwarf2_per_cu_data per_cu;
11686 struct dwo_unit read_unit {};
11687 struct dwo_unit *dwo_unit;
11688 void **slot;
11689 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11690
11691 memset (&per_cu, 0, sizeof (per_cu));
11692 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11693 per_cu.is_debug_types = 0;
11694 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11695 per_cu.section = &section;
11696
11697 cutu_reader reader (&per_cu, cu, &dwo_file);
11698 if (!reader.dummy_p)
11699 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11700 reader.has_children, &dwo_file, &read_unit);
11701 info_ptr += per_cu.length;
11702
11703 // If the unit could not be parsed, skip it.
11704 if (read_unit.dwo_file == NULL)
11705 continue;
11706
11707 if (cus_htab == NULL)
11708 cus_htab = allocate_dwo_unit_table (objfile);
11709
11710 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11711 *dwo_unit = read_unit;
11712 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11713 gdb_assert (slot != NULL);
11714 if (*slot != NULL)
11715 {
11716 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11717 sect_offset dup_sect_off = dup_cu->sect_off;
11718
11719 complaint (_("debug cu entry at offset %s is duplicate to"
11720 " the entry at offset %s, signature %s"),
11721 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11722 hex_string (dwo_unit->signature));
11723 }
11724 *slot = (void *)dwo_unit;
11725 }
11726 }
11727
11728 /* DWP file .debug_{cu,tu}_index section format:
11729 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11730
11731 DWP Version 1:
11732
11733 Both index sections have the same format, and serve to map a 64-bit
11734 signature to a set of section numbers. Each section begins with a header,
11735 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11736 indexes, and a pool of 32-bit section numbers. The index sections will be
11737 aligned at 8-byte boundaries in the file.
11738
11739 The index section header consists of:
11740
11741 V, 32 bit version number
11742 -, 32 bits unused
11743 N, 32 bit number of compilation units or type units in the index
11744 M, 32 bit number of slots in the hash table
11745
11746 Numbers are recorded using the byte order of the application binary.
11747
11748 The hash table begins at offset 16 in the section, and consists of an array
11749 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11750 order of the application binary). Unused slots in the hash table are 0.
11751 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11752
11753 The parallel table begins immediately after the hash table
11754 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11755 array of 32-bit indexes (using the byte order of the application binary),
11756 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11757 table contains a 32-bit index into the pool of section numbers. For unused
11758 hash table slots, the corresponding entry in the parallel table will be 0.
11759
11760 The pool of section numbers begins immediately following the hash table
11761 (at offset 16 + 12 * M from the beginning of the section). The pool of
11762 section numbers consists of an array of 32-bit words (using the byte order
11763 of the application binary). Each item in the array is indexed starting
11764 from 0. The hash table entry provides the index of the first section
11765 number in the set. Additional section numbers in the set follow, and the
11766 set is terminated by a 0 entry (section number 0 is not used in ELF).
11767
11768 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11769 section must be the first entry in the set, and the .debug_abbrev.dwo must
11770 be the second entry. Other members of the set may follow in any order.
11771
11772 ---
11773
11774 DWP Version 2:
11775
11776 DWP Version 2 combines all the .debug_info, etc. sections into one,
11777 and the entries in the index tables are now offsets into these sections.
11778 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11779 section.
11780
11781 Index Section Contents:
11782 Header
11783 Hash Table of Signatures dwp_hash_table.hash_table
11784 Parallel Table of Indices dwp_hash_table.unit_table
11785 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11786 Table of Section Sizes dwp_hash_table.v2.sizes
11787
11788 The index section header consists of:
11789
11790 V, 32 bit version number
11791 L, 32 bit number of columns in the table of section offsets
11792 N, 32 bit number of compilation units or type units in the index
11793 M, 32 bit number of slots in the hash table
11794
11795 Numbers are recorded using the byte order of the application binary.
11796
11797 The hash table has the same format as version 1.
11798 The parallel table of indices has the same format as version 1,
11799 except that the entries are origin-1 indices into the table of sections
11800 offsets and the table of section sizes.
11801
11802 The table of offsets begins immediately following the parallel table
11803 (at offset 16 + 12 * M from the beginning of the section). The table is
11804 a two-dimensional array of 32-bit words (using the byte order of the
11805 application binary), with L columns and N+1 rows, in row-major order.
11806 Each row in the array is indexed starting from 0. The first row provides
11807 a key to the remaining rows: each column in this row provides an identifier
11808 for a debug section, and the offsets in the same column of subsequent rows
11809 refer to that section. The section identifiers are:
11810
11811 DW_SECT_INFO 1 .debug_info.dwo
11812 DW_SECT_TYPES 2 .debug_types.dwo
11813 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11814 DW_SECT_LINE 4 .debug_line.dwo
11815 DW_SECT_LOC 5 .debug_loc.dwo
11816 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11817 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11818 DW_SECT_MACRO 8 .debug_macro.dwo
11819
11820 The offsets provided by the CU and TU index sections are the base offsets
11821 for the contributions made by each CU or TU to the corresponding section
11822 in the package file. Each CU and TU header contains an abbrev_offset
11823 field, used to find the abbreviations table for that CU or TU within the
11824 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11825 be interpreted as relative to the base offset given in the index section.
11826 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11827 should be interpreted as relative to the base offset for .debug_line.dwo,
11828 and offsets into other debug sections obtained from DWARF attributes should
11829 also be interpreted as relative to the corresponding base offset.
11830
11831 The table of sizes begins immediately following the table of offsets.
11832 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11833 with L columns and N rows, in row-major order. Each row in the array is
11834 indexed starting from 1 (row 0 is shared by the two tables).
11835
11836 ---
11837
11838 Hash table lookup is handled the same in version 1 and 2:
11839
11840 We assume that N and M will not exceed 2^32 - 1.
11841 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11842
11843 Given a 64-bit compilation unit signature or a type signature S, an entry
11844 in the hash table is located as follows:
11845
11846 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11847 the low-order k bits all set to 1.
11848
11849 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11850
11851 3) If the hash table entry at index H matches the signature, use that
11852 entry. If the hash table entry at index H is unused (all zeroes),
11853 terminate the search: the signature is not present in the table.
11854
11855 4) Let H = (H + H') modulo M. Repeat at Step 3.
11856
11857 Because M > N and H' and M are relatively prime, the search is guaranteed
11858 to stop at an unused slot or find the match. */
11859
11860 /* Create a hash table to map DWO IDs to their CU/TU entry in
11861 .debug_{info,types}.dwo in DWP_FILE.
11862 Returns NULL if there isn't one.
11863 Note: This function processes DWP files only, not DWO files. */
11864
11865 static struct dwp_hash_table *
11866 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11867 struct dwp_file *dwp_file, int is_debug_types)
11868 {
11869 struct objfile *objfile = dwarf2_per_objfile->objfile;
11870 bfd *dbfd = dwp_file->dbfd.get ();
11871 const gdb_byte *index_ptr, *index_end;
11872 struct dwarf2_section_info *index;
11873 uint32_t version, nr_columns, nr_units, nr_slots;
11874 struct dwp_hash_table *htab;
11875
11876 if (is_debug_types)
11877 index = &dwp_file->sections.tu_index;
11878 else
11879 index = &dwp_file->sections.cu_index;
11880
11881 if (index->empty ())
11882 return NULL;
11883 index->read (objfile);
11884
11885 index_ptr = index->buffer;
11886 index_end = index_ptr + index->size;
11887
11888 version = read_4_bytes (dbfd, index_ptr);
11889 index_ptr += 4;
11890 if (version == 2)
11891 nr_columns = read_4_bytes (dbfd, index_ptr);
11892 else
11893 nr_columns = 0;
11894 index_ptr += 4;
11895 nr_units = read_4_bytes (dbfd, index_ptr);
11896 index_ptr += 4;
11897 nr_slots = read_4_bytes (dbfd, index_ptr);
11898 index_ptr += 4;
11899
11900 if (version != 1 && version != 2)
11901 {
11902 error (_("Dwarf Error: unsupported DWP file version (%s)"
11903 " [in module %s]"),
11904 pulongest (version), dwp_file->name);
11905 }
11906 if (nr_slots != (nr_slots & -nr_slots))
11907 {
11908 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11909 " is not power of 2 [in module %s]"),
11910 pulongest (nr_slots), dwp_file->name);
11911 }
11912
11913 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11914 htab->version = version;
11915 htab->nr_columns = nr_columns;
11916 htab->nr_units = nr_units;
11917 htab->nr_slots = nr_slots;
11918 htab->hash_table = index_ptr;
11919 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11920
11921 /* Exit early if the table is empty. */
11922 if (nr_slots == 0 || nr_units == 0
11923 || (version == 2 && nr_columns == 0))
11924 {
11925 /* All must be zero. */
11926 if (nr_slots != 0 || nr_units != 0
11927 || (version == 2 && nr_columns != 0))
11928 {
11929 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11930 " all zero [in modules %s]"),
11931 dwp_file->name);
11932 }
11933 return htab;
11934 }
11935
11936 if (version == 1)
11937 {
11938 htab->section_pool.v1.indices =
11939 htab->unit_table + sizeof (uint32_t) * nr_slots;
11940 /* It's harder to decide whether the section is too small in v1.
11941 V1 is deprecated anyway so we punt. */
11942 }
11943 else
11944 {
11945 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11946 int *ids = htab->section_pool.v2.section_ids;
11947 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11948 /* Reverse map for error checking. */
11949 int ids_seen[DW_SECT_MAX + 1];
11950 int i;
11951
11952 if (nr_columns < 2)
11953 {
11954 error (_("Dwarf Error: bad DWP hash table, too few columns"
11955 " in section table [in module %s]"),
11956 dwp_file->name);
11957 }
11958 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11959 {
11960 error (_("Dwarf Error: bad DWP hash table, too many columns"
11961 " in section table [in module %s]"),
11962 dwp_file->name);
11963 }
11964 memset (ids, 255, sizeof_ids);
11965 memset (ids_seen, 255, sizeof (ids_seen));
11966 for (i = 0; i < nr_columns; ++i)
11967 {
11968 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11969
11970 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11971 {
11972 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11973 " in section table [in module %s]"),
11974 id, dwp_file->name);
11975 }
11976 if (ids_seen[id] != -1)
11977 {
11978 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11979 " id %d in section table [in module %s]"),
11980 id, dwp_file->name);
11981 }
11982 ids_seen[id] = i;
11983 ids[i] = id;
11984 }
11985 /* Must have exactly one info or types section. */
11986 if (((ids_seen[DW_SECT_INFO] != -1)
11987 + (ids_seen[DW_SECT_TYPES] != -1))
11988 != 1)
11989 {
11990 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11991 " DWO info/types section [in module %s]"),
11992 dwp_file->name);
11993 }
11994 /* Must have an abbrev section. */
11995 if (ids_seen[DW_SECT_ABBREV] == -1)
11996 {
11997 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11998 " section [in module %s]"),
11999 dwp_file->name);
12000 }
12001 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12002 htab->section_pool.v2.sizes =
12003 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12004 * nr_units * nr_columns);
12005 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12006 * nr_units * nr_columns))
12007 > index_end)
12008 {
12009 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12010 " [in module %s]"),
12011 dwp_file->name);
12012 }
12013 }
12014
12015 return htab;
12016 }
12017
12018 /* Update SECTIONS with the data from SECTP.
12019
12020 This function is like the other "locate" section routines that are
12021 passed to bfd_map_over_sections, but in this context the sections to
12022 read comes from the DWP V1 hash table, not the full ELF section table.
12023
12024 The result is non-zero for success, or zero if an error was found. */
12025
12026 static int
12027 locate_v1_virtual_dwo_sections (asection *sectp,
12028 struct virtual_v1_dwo_sections *sections)
12029 {
12030 const struct dwop_section_names *names = &dwop_section_names;
12031
12032 if (section_is_p (sectp->name, &names->abbrev_dwo))
12033 {
12034 /* There can be only one. */
12035 if (sections->abbrev.s.section != NULL)
12036 return 0;
12037 sections->abbrev.s.section = sectp;
12038 sections->abbrev.size = bfd_section_size (sectp);
12039 }
12040 else if (section_is_p (sectp->name, &names->info_dwo)
12041 || section_is_p (sectp->name, &names->types_dwo))
12042 {
12043 /* There can be only one. */
12044 if (sections->info_or_types.s.section != NULL)
12045 return 0;
12046 sections->info_or_types.s.section = sectp;
12047 sections->info_or_types.size = bfd_section_size (sectp);
12048 }
12049 else if (section_is_p (sectp->name, &names->line_dwo))
12050 {
12051 /* There can be only one. */
12052 if (sections->line.s.section != NULL)
12053 return 0;
12054 sections->line.s.section = sectp;
12055 sections->line.size = bfd_section_size (sectp);
12056 }
12057 else if (section_is_p (sectp->name, &names->loc_dwo))
12058 {
12059 /* There can be only one. */
12060 if (sections->loc.s.section != NULL)
12061 return 0;
12062 sections->loc.s.section = sectp;
12063 sections->loc.size = bfd_section_size (sectp);
12064 }
12065 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12066 {
12067 /* There can be only one. */
12068 if (sections->macinfo.s.section != NULL)
12069 return 0;
12070 sections->macinfo.s.section = sectp;
12071 sections->macinfo.size = bfd_section_size (sectp);
12072 }
12073 else if (section_is_p (sectp->name, &names->macro_dwo))
12074 {
12075 /* There can be only one. */
12076 if (sections->macro.s.section != NULL)
12077 return 0;
12078 sections->macro.s.section = sectp;
12079 sections->macro.size = bfd_section_size (sectp);
12080 }
12081 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12082 {
12083 /* There can be only one. */
12084 if (sections->str_offsets.s.section != NULL)
12085 return 0;
12086 sections->str_offsets.s.section = sectp;
12087 sections->str_offsets.size = bfd_section_size (sectp);
12088 }
12089 else
12090 {
12091 /* No other kind of section is valid. */
12092 return 0;
12093 }
12094
12095 return 1;
12096 }
12097
12098 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12099 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12100 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12101 This is for DWP version 1 files. */
12102
12103 static struct dwo_unit *
12104 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12105 struct dwp_file *dwp_file,
12106 uint32_t unit_index,
12107 const char *comp_dir,
12108 ULONGEST signature, int is_debug_types)
12109 {
12110 struct objfile *objfile = dwarf2_per_objfile->objfile;
12111 const struct dwp_hash_table *dwp_htab =
12112 is_debug_types ? dwp_file->tus : dwp_file->cus;
12113 bfd *dbfd = dwp_file->dbfd.get ();
12114 const char *kind = is_debug_types ? "TU" : "CU";
12115 struct dwo_file *dwo_file;
12116 struct dwo_unit *dwo_unit;
12117 struct virtual_v1_dwo_sections sections;
12118 void **dwo_file_slot;
12119 int i;
12120
12121 gdb_assert (dwp_file->version == 1);
12122
12123 if (dwarf_read_debug)
12124 {
12125 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12126 kind,
12127 pulongest (unit_index), hex_string (signature),
12128 dwp_file->name);
12129 }
12130
12131 /* Fetch the sections of this DWO unit.
12132 Put a limit on the number of sections we look for so that bad data
12133 doesn't cause us to loop forever. */
12134
12135 #define MAX_NR_V1_DWO_SECTIONS \
12136 (1 /* .debug_info or .debug_types */ \
12137 + 1 /* .debug_abbrev */ \
12138 + 1 /* .debug_line */ \
12139 + 1 /* .debug_loc */ \
12140 + 1 /* .debug_str_offsets */ \
12141 + 1 /* .debug_macro or .debug_macinfo */ \
12142 + 1 /* trailing zero */)
12143
12144 memset (&sections, 0, sizeof (sections));
12145
12146 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12147 {
12148 asection *sectp;
12149 uint32_t section_nr =
12150 read_4_bytes (dbfd,
12151 dwp_htab->section_pool.v1.indices
12152 + (unit_index + i) * sizeof (uint32_t));
12153
12154 if (section_nr == 0)
12155 break;
12156 if (section_nr >= dwp_file->num_sections)
12157 {
12158 error (_("Dwarf Error: bad DWP hash table, section number too large"
12159 " [in module %s]"),
12160 dwp_file->name);
12161 }
12162
12163 sectp = dwp_file->elf_sections[section_nr];
12164 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12165 {
12166 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12167 " [in module %s]"),
12168 dwp_file->name);
12169 }
12170 }
12171
12172 if (i < 2
12173 || sections.info_or_types.empty ()
12174 || sections.abbrev.empty ())
12175 {
12176 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12177 " [in module %s]"),
12178 dwp_file->name);
12179 }
12180 if (i == MAX_NR_V1_DWO_SECTIONS)
12181 {
12182 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12183 " [in module %s]"),
12184 dwp_file->name);
12185 }
12186
12187 /* It's easier for the rest of the code if we fake a struct dwo_file and
12188 have dwo_unit "live" in that. At least for now.
12189
12190 The DWP file can be made up of a random collection of CUs and TUs.
12191 However, for each CU + set of TUs that came from the same original DWO
12192 file, we can combine them back into a virtual DWO file to save space
12193 (fewer struct dwo_file objects to allocate). Remember that for really
12194 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12195
12196 std::string virtual_dwo_name =
12197 string_printf ("virtual-dwo/%d-%d-%d-%d",
12198 sections.abbrev.get_id (),
12199 sections.line.get_id (),
12200 sections.loc.get_id (),
12201 sections.str_offsets.get_id ());
12202 /* Can we use an existing virtual DWO file? */
12203 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12204 virtual_dwo_name.c_str (),
12205 comp_dir);
12206 /* Create one if necessary. */
12207 if (*dwo_file_slot == NULL)
12208 {
12209 if (dwarf_read_debug)
12210 {
12211 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12212 virtual_dwo_name.c_str ());
12213 }
12214 dwo_file = new struct dwo_file;
12215 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12216 virtual_dwo_name);
12217 dwo_file->comp_dir = comp_dir;
12218 dwo_file->sections.abbrev = sections.abbrev;
12219 dwo_file->sections.line = sections.line;
12220 dwo_file->sections.loc = sections.loc;
12221 dwo_file->sections.macinfo = sections.macinfo;
12222 dwo_file->sections.macro = sections.macro;
12223 dwo_file->sections.str_offsets = sections.str_offsets;
12224 /* The "str" section is global to the entire DWP file. */
12225 dwo_file->sections.str = dwp_file->sections.str;
12226 /* The info or types section is assigned below to dwo_unit,
12227 there's no need to record it in dwo_file.
12228 Also, we can't simply record type sections in dwo_file because
12229 we record a pointer into the vector in dwo_unit. As we collect more
12230 types we'll grow the vector and eventually have to reallocate space
12231 for it, invalidating all copies of pointers into the previous
12232 contents. */
12233 *dwo_file_slot = dwo_file;
12234 }
12235 else
12236 {
12237 if (dwarf_read_debug)
12238 {
12239 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12240 virtual_dwo_name.c_str ());
12241 }
12242 dwo_file = (struct dwo_file *) *dwo_file_slot;
12243 }
12244
12245 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12246 dwo_unit->dwo_file = dwo_file;
12247 dwo_unit->signature = signature;
12248 dwo_unit->section =
12249 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12250 *dwo_unit->section = sections.info_or_types;
12251 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12252
12253 return dwo_unit;
12254 }
12255
12256 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12257 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12258 piece within that section used by a TU/CU, return a virtual section
12259 of just that piece. */
12260
12261 static struct dwarf2_section_info
12262 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12263 struct dwarf2_section_info *section,
12264 bfd_size_type offset, bfd_size_type size)
12265 {
12266 struct dwarf2_section_info result;
12267 asection *sectp;
12268
12269 gdb_assert (section != NULL);
12270 gdb_assert (!section->is_virtual);
12271
12272 memset (&result, 0, sizeof (result));
12273 result.s.containing_section = section;
12274 result.is_virtual = true;
12275
12276 if (size == 0)
12277 return result;
12278
12279 sectp = section->get_bfd_section ();
12280
12281 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12282 bounds of the real section. This is a pretty-rare event, so just
12283 flag an error (easier) instead of a warning and trying to cope. */
12284 if (sectp == NULL
12285 || offset + size > bfd_section_size (sectp))
12286 {
12287 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12288 " in section %s [in module %s]"),
12289 sectp ? bfd_section_name (sectp) : "<unknown>",
12290 objfile_name (dwarf2_per_objfile->objfile));
12291 }
12292
12293 result.virtual_offset = offset;
12294 result.size = size;
12295 return result;
12296 }
12297
12298 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12299 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12300 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12301 This is for DWP version 2 files. */
12302
12303 static struct dwo_unit *
12304 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12305 struct dwp_file *dwp_file,
12306 uint32_t unit_index,
12307 const char *comp_dir,
12308 ULONGEST signature, int is_debug_types)
12309 {
12310 struct objfile *objfile = dwarf2_per_objfile->objfile;
12311 const struct dwp_hash_table *dwp_htab =
12312 is_debug_types ? dwp_file->tus : dwp_file->cus;
12313 bfd *dbfd = dwp_file->dbfd.get ();
12314 const char *kind = is_debug_types ? "TU" : "CU";
12315 struct dwo_file *dwo_file;
12316 struct dwo_unit *dwo_unit;
12317 struct virtual_v2_dwo_sections sections;
12318 void **dwo_file_slot;
12319 int i;
12320
12321 gdb_assert (dwp_file->version == 2);
12322
12323 if (dwarf_read_debug)
12324 {
12325 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12326 kind,
12327 pulongest (unit_index), hex_string (signature),
12328 dwp_file->name);
12329 }
12330
12331 /* Fetch the section offsets of this DWO unit. */
12332
12333 memset (&sections, 0, sizeof (sections));
12334
12335 for (i = 0; i < dwp_htab->nr_columns; ++i)
12336 {
12337 uint32_t offset = read_4_bytes (dbfd,
12338 dwp_htab->section_pool.v2.offsets
12339 + (((unit_index - 1) * dwp_htab->nr_columns
12340 + i)
12341 * sizeof (uint32_t)));
12342 uint32_t size = read_4_bytes (dbfd,
12343 dwp_htab->section_pool.v2.sizes
12344 + (((unit_index - 1) * dwp_htab->nr_columns
12345 + i)
12346 * sizeof (uint32_t)));
12347
12348 switch (dwp_htab->section_pool.v2.section_ids[i])
12349 {
12350 case DW_SECT_INFO:
12351 case DW_SECT_TYPES:
12352 sections.info_or_types_offset = offset;
12353 sections.info_or_types_size = size;
12354 break;
12355 case DW_SECT_ABBREV:
12356 sections.abbrev_offset = offset;
12357 sections.abbrev_size = size;
12358 break;
12359 case DW_SECT_LINE:
12360 sections.line_offset = offset;
12361 sections.line_size = size;
12362 break;
12363 case DW_SECT_LOC:
12364 sections.loc_offset = offset;
12365 sections.loc_size = size;
12366 break;
12367 case DW_SECT_STR_OFFSETS:
12368 sections.str_offsets_offset = offset;
12369 sections.str_offsets_size = size;
12370 break;
12371 case DW_SECT_MACINFO:
12372 sections.macinfo_offset = offset;
12373 sections.macinfo_size = size;
12374 break;
12375 case DW_SECT_MACRO:
12376 sections.macro_offset = offset;
12377 sections.macro_size = size;
12378 break;
12379 }
12380 }
12381
12382 /* It's easier for the rest of the code if we fake a struct dwo_file and
12383 have dwo_unit "live" in that. At least for now.
12384
12385 The DWP file can be made up of a random collection of CUs and TUs.
12386 However, for each CU + set of TUs that came from the same original DWO
12387 file, we can combine them back into a virtual DWO file to save space
12388 (fewer struct dwo_file objects to allocate). Remember that for really
12389 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12390
12391 std::string virtual_dwo_name =
12392 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12393 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12394 (long) (sections.line_size ? sections.line_offset : 0),
12395 (long) (sections.loc_size ? sections.loc_offset : 0),
12396 (long) (sections.str_offsets_size
12397 ? sections.str_offsets_offset : 0));
12398 /* Can we use an existing virtual DWO file? */
12399 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12400 virtual_dwo_name.c_str (),
12401 comp_dir);
12402 /* Create one if necessary. */
12403 if (*dwo_file_slot == NULL)
12404 {
12405 if (dwarf_read_debug)
12406 {
12407 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12408 virtual_dwo_name.c_str ());
12409 }
12410 dwo_file = new struct dwo_file;
12411 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12412 virtual_dwo_name);
12413 dwo_file->comp_dir = comp_dir;
12414 dwo_file->sections.abbrev =
12415 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12416 sections.abbrev_offset, sections.abbrev_size);
12417 dwo_file->sections.line =
12418 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12419 sections.line_offset, sections.line_size);
12420 dwo_file->sections.loc =
12421 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12422 sections.loc_offset, sections.loc_size);
12423 dwo_file->sections.macinfo =
12424 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12425 sections.macinfo_offset, sections.macinfo_size);
12426 dwo_file->sections.macro =
12427 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12428 sections.macro_offset, sections.macro_size);
12429 dwo_file->sections.str_offsets =
12430 create_dwp_v2_section (dwarf2_per_objfile,
12431 &dwp_file->sections.str_offsets,
12432 sections.str_offsets_offset,
12433 sections.str_offsets_size);
12434 /* The "str" section is global to the entire DWP file. */
12435 dwo_file->sections.str = dwp_file->sections.str;
12436 /* The info or types section is assigned below to dwo_unit,
12437 there's no need to record it in dwo_file.
12438 Also, we can't simply record type sections in dwo_file because
12439 we record a pointer into the vector in dwo_unit. As we collect more
12440 types we'll grow the vector and eventually have to reallocate space
12441 for it, invalidating all copies of pointers into the previous
12442 contents. */
12443 *dwo_file_slot = dwo_file;
12444 }
12445 else
12446 {
12447 if (dwarf_read_debug)
12448 {
12449 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12450 virtual_dwo_name.c_str ());
12451 }
12452 dwo_file = (struct dwo_file *) *dwo_file_slot;
12453 }
12454
12455 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12456 dwo_unit->dwo_file = dwo_file;
12457 dwo_unit->signature = signature;
12458 dwo_unit->section =
12459 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12460 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12461 is_debug_types
12462 ? &dwp_file->sections.types
12463 : &dwp_file->sections.info,
12464 sections.info_or_types_offset,
12465 sections.info_or_types_size);
12466 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12467
12468 return dwo_unit;
12469 }
12470
12471 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12472 Returns NULL if the signature isn't found. */
12473
12474 static struct dwo_unit *
12475 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12476 struct dwp_file *dwp_file, const char *comp_dir,
12477 ULONGEST signature, int is_debug_types)
12478 {
12479 const struct dwp_hash_table *dwp_htab =
12480 is_debug_types ? dwp_file->tus : dwp_file->cus;
12481 bfd *dbfd = dwp_file->dbfd.get ();
12482 uint32_t mask = dwp_htab->nr_slots - 1;
12483 uint32_t hash = signature & mask;
12484 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12485 unsigned int i;
12486 void **slot;
12487 struct dwo_unit find_dwo_cu;
12488
12489 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12490 find_dwo_cu.signature = signature;
12491 slot = htab_find_slot (is_debug_types
12492 ? dwp_file->loaded_tus
12493 : dwp_file->loaded_cus,
12494 &find_dwo_cu, INSERT);
12495
12496 if (*slot != NULL)
12497 return (struct dwo_unit *) *slot;
12498
12499 /* Use a for loop so that we don't loop forever on bad debug info. */
12500 for (i = 0; i < dwp_htab->nr_slots; ++i)
12501 {
12502 ULONGEST signature_in_table;
12503
12504 signature_in_table =
12505 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12506 if (signature_in_table == signature)
12507 {
12508 uint32_t unit_index =
12509 read_4_bytes (dbfd,
12510 dwp_htab->unit_table + hash * sizeof (uint32_t));
12511
12512 if (dwp_file->version == 1)
12513 {
12514 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12515 dwp_file, unit_index,
12516 comp_dir, signature,
12517 is_debug_types);
12518 }
12519 else
12520 {
12521 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12522 dwp_file, unit_index,
12523 comp_dir, signature,
12524 is_debug_types);
12525 }
12526 return (struct dwo_unit *) *slot;
12527 }
12528 if (signature_in_table == 0)
12529 return NULL;
12530 hash = (hash + hash2) & mask;
12531 }
12532
12533 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12534 " [in module %s]"),
12535 dwp_file->name);
12536 }
12537
12538 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12539 Open the file specified by FILE_NAME and hand it off to BFD for
12540 preliminary analysis. Return a newly initialized bfd *, which
12541 includes a canonicalized copy of FILE_NAME.
12542 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12543 SEARCH_CWD is true if the current directory is to be searched.
12544 It will be searched before debug-file-directory.
12545 If successful, the file is added to the bfd include table of the
12546 objfile's bfd (see gdb_bfd_record_inclusion).
12547 If unable to find/open the file, return NULL.
12548 NOTE: This function is derived from symfile_bfd_open. */
12549
12550 static gdb_bfd_ref_ptr
12551 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12552 const char *file_name, int is_dwp, int search_cwd)
12553 {
12554 int desc;
12555 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12556 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12557 to debug_file_directory. */
12558 const char *search_path;
12559 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12560
12561 gdb::unique_xmalloc_ptr<char> search_path_holder;
12562 if (search_cwd)
12563 {
12564 if (*debug_file_directory != '\0')
12565 {
12566 search_path_holder.reset (concat (".", dirname_separator_string,
12567 debug_file_directory,
12568 (char *) NULL));
12569 search_path = search_path_holder.get ();
12570 }
12571 else
12572 search_path = ".";
12573 }
12574 else
12575 search_path = debug_file_directory;
12576
12577 openp_flags flags = OPF_RETURN_REALPATH;
12578 if (is_dwp)
12579 flags |= OPF_SEARCH_IN_PATH;
12580
12581 gdb::unique_xmalloc_ptr<char> absolute_name;
12582 desc = openp (search_path, flags, file_name,
12583 O_RDONLY | O_BINARY, &absolute_name);
12584 if (desc < 0)
12585 return NULL;
12586
12587 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12588 gnutarget, desc));
12589 if (sym_bfd == NULL)
12590 return NULL;
12591 bfd_set_cacheable (sym_bfd.get (), 1);
12592
12593 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12594 return NULL;
12595
12596 /* Success. Record the bfd as having been included by the objfile's bfd.
12597 This is important because things like demangled_names_hash lives in the
12598 objfile's per_bfd space and may have references to things like symbol
12599 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12600 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12601
12602 return sym_bfd;
12603 }
12604
12605 /* Try to open DWO file FILE_NAME.
12606 COMP_DIR is the DW_AT_comp_dir attribute.
12607 The result is the bfd handle of the file.
12608 If there is a problem finding or opening the file, return NULL.
12609 Upon success, the canonicalized path of the file is stored in the bfd,
12610 same as symfile_bfd_open. */
12611
12612 static gdb_bfd_ref_ptr
12613 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12614 const char *file_name, const char *comp_dir)
12615 {
12616 if (IS_ABSOLUTE_PATH (file_name))
12617 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12618 0 /*is_dwp*/, 0 /*search_cwd*/);
12619
12620 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12621
12622 if (comp_dir != NULL)
12623 {
12624 gdb::unique_xmalloc_ptr<char> path_to_try
12625 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12626
12627 /* NOTE: If comp_dir is a relative path, this will also try the
12628 search path, which seems useful. */
12629 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12630 path_to_try.get (),
12631 0 /*is_dwp*/,
12632 1 /*search_cwd*/));
12633 if (abfd != NULL)
12634 return abfd;
12635 }
12636
12637 /* That didn't work, try debug-file-directory, which, despite its name,
12638 is a list of paths. */
12639
12640 if (*debug_file_directory == '\0')
12641 return NULL;
12642
12643 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12644 0 /*is_dwp*/, 1 /*search_cwd*/);
12645 }
12646
12647 /* This function is mapped across the sections and remembers the offset and
12648 size of each of the DWO debugging sections we are interested in. */
12649
12650 static void
12651 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12652 {
12653 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12654 const struct dwop_section_names *names = &dwop_section_names;
12655
12656 if (section_is_p (sectp->name, &names->abbrev_dwo))
12657 {
12658 dwo_sections->abbrev.s.section = sectp;
12659 dwo_sections->abbrev.size = bfd_section_size (sectp);
12660 }
12661 else if (section_is_p (sectp->name, &names->info_dwo))
12662 {
12663 dwo_sections->info.s.section = sectp;
12664 dwo_sections->info.size = bfd_section_size (sectp);
12665 }
12666 else if (section_is_p (sectp->name, &names->line_dwo))
12667 {
12668 dwo_sections->line.s.section = sectp;
12669 dwo_sections->line.size = bfd_section_size (sectp);
12670 }
12671 else if (section_is_p (sectp->name, &names->loc_dwo))
12672 {
12673 dwo_sections->loc.s.section = sectp;
12674 dwo_sections->loc.size = bfd_section_size (sectp);
12675 }
12676 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12677 {
12678 dwo_sections->macinfo.s.section = sectp;
12679 dwo_sections->macinfo.size = bfd_section_size (sectp);
12680 }
12681 else if (section_is_p (sectp->name, &names->macro_dwo))
12682 {
12683 dwo_sections->macro.s.section = sectp;
12684 dwo_sections->macro.size = bfd_section_size (sectp);
12685 }
12686 else if (section_is_p (sectp->name, &names->str_dwo))
12687 {
12688 dwo_sections->str.s.section = sectp;
12689 dwo_sections->str.size = bfd_section_size (sectp);
12690 }
12691 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12692 {
12693 dwo_sections->str_offsets.s.section = sectp;
12694 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12695 }
12696 else if (section_is_p (sectp->name, &names->types_dwo))
12697 {
12698 struct dwarf2_section_info type_section;
12699
12700 memset (&type_section, 0, sizeof (type_section));
12701 type_section.s.section = sectp;
12702 type_section.size = bfd_section_size (sectp);
12703 dwo_sections->types.push_back (type_section);
12704 }
12705 }
12706
12707 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12708 by PER_CU. This is for the non-DWP case.
12709 The result is NULL if DWO_NAME can't be found. */
12710
12711 static struct dwo_file *
12712 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12713 const char *dwo_name, const char *comp_dir)
12714 {
12715 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12716
12717 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12718 if (dbfd == NULL)
12719 {
12720 if (dwarf_read_debug)
12721 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12722 return NULL;
12723 }
12724
12725 dwo_file_up dwo_file (new struct dwo_file);
12726 dwo_file->dwo_name = dwo_name;
12727 dwo_file->comp_dir = comp_dir;
12728 dwo_file->dbfd = std::move (dbfd);
12729
12730 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12731 &dwo_file->sections);
12732
12733 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12734 dwo_file->sections.info, dwo_file->cus);
12735
12736 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12737 dwo_file->sections.types, dwo_file->tus);
12738
12739 if (dwarf_read_debug)
12740 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12741
12742 return dwo_file.release ();
12743 }
12744
12745 /* This function is mapped across the sections and remembers the offset and
12746 size of each of the DWP debugging sections common to version 1 and 2 that
12747 we are interested in. */
12748
12749 static void
12750 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12751 void *dwp_file_ptr)
12752 {
12753 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12754 const struct dwop_section_names *names = &dwop_section_names;
12755 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12756
12757 /* Record the ELF section number for later lookup: this is what the
12758 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12759 gdb_assert (elf_section_nr < dwp_file->num_sections);
12760 dwp_file->elf_sections[elf_section_nr] = sectp;
12761
12762 /* Look for specific sections that we need. */
12763 if (section_is_p (sectp->name, &names->str_dwo))
12764 {
12765 dwp_file->sections.str.s.section = sectp;
12766 dwp_file->sections.str.size = bfd_section_size (sectp);
12767 }
12768 else if (section_is_p (sectp->name, &names->cu_index))
12769 {
12770 dwp_file->sections.cu_index.s.section = sectp;
12771 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12772 }
12773 else if (section_is_p (sectp->name, &names->tu_index))
12774 {
12775 dwp_file->sections.tu_index.s.section = sectp;
12776 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12777 }
12778 }
12779
12780 /* This function is mapped across the sections and remembers the offset and
12781 size of each of the DWP version 2 debugging sections that we are interested
12782 in. This is split into a separate function because we don't know if we
12783 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12784
12785 static void
12786 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12787 {
12788 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12789 const struct dwop_section_names *names = &dwop_section_names;
12790 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12791
12792 /* Record the ELF section number for later lookup: this is what the
12793 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12794 gdb_assert (elf_section_nr < dwp_file->num_sections);
12795 dwp_file->elf_sections[elf_section_nr] = sectp;
12796
12797 /* Look for specific sections that we need. */
12798 if (section_is_p (sectp->name, &names->abbrev_dwo))
12799 {
12800 dwp_file->sections.abbrev.s.section = sectp;
12801 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12802 }
12803 else if (section_is_p (sectp->name, &names->info_dwo))
12804 {
12805 dwp_file->sections.info.s.section = sectp;
12806 dwp_file->sections.info.size = bfd_section_size (sectp);
12807 }
12808 else if (section_is_p (sectp->name, &names->line_dwo))
12809 {
12810 dwp_file->sections.line.s.section = sectp;
12811 dwp_file->sections.line.size = bfd_section_size (sectp);
12812 }
12813 else if (section_is_p (sectp->name, &names->loc_dwo))
12814 {
12815 dwp_file->sections.loc.s.section = sectp;
12816 dwp_file->sections.loc.size = bfd_section_size (sectp);
12817 }
12818 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12819 {
12820 dwp_file->sections.macinfo.s.section = sectp;
12821 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12822 }
12823 else if (section_is_p (sectp->name, &names->macro_dwo))
12824 {
12825 dwp_file->sections.macro.s.section = sectp;
12826 dwp_file->sections.macro.size = bfd_section_size (sectp);
12827 }
12828 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12829 {
12830 dwp_file->sections.str_offsets.s.section = sectp;
12831 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12832 }
12833 else if (section_is_p (sectp->name, &names->types_dwo))
12834 {
12835 dwp_file->sections.types.s.section = sectp;
12836 dwp_file->sections.types.size = bfd_section_size (sectp);
12837 }
12838 }
12839
12840 /* Hash function for dwp_file loaded CUs/TUs. */
12841
12842 static hashval_t
12843 hash_dwp_loaded_cutus (const void *item)
12844 {
12845 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12846
12847 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12848 return dwo_unit->signature;
12849 }
12850
12851 /* Equality function for dwp_file loaded CUs/TUs. */
12852
12853 static int
12854 eq_dwp_loaded_cutus (const void *a, const void *b)
12855 {
12856 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12857 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12858
12859 return dua->signature == dub->signature;
12860 }
12861
12862 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12863
12864 static htab_t
12865 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12866 {
12867 return htab_create_alloc_ex (3,
12868 hash_dwp_loaded_cutus,
12869 eq_dwp_loaded_cutus,
12870 NULL,
12871 &objfile->objfile_obstack,
12872 hashtab_obstack_allocate,
12873 dummy_obstack_deallocate);
12874 }
12875
12876 /* Try to open DWP file FILE_NAME.
12877 The result is the bfd handle of the file.
12878 If there is a problem finding or opening the file, return NULL.
12879 Upon success, the canonicalized path of the file is stored in the bfd,
12880 same as symfile_bfd_open. */
12881
12882 static gdb_bfd_ref_ptr
12883 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12884 const char *file_name)
12885 {
12886 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12887 1 /*is_dwp*/,
12888 1 /*search_cwd*/));
12889 if (abfd != NULL)
12890 return abfd;
12891
12892 /* Work around upstream bug 15652.
12893 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12894 [Whether that's a "bug" is debatable, but it is getting in our way.]
12895 We have no real idea where the dwp file is, because gdb's realpath-ing
12896 of the executable's path may have discarded the needed info.
12897 [IWBN if the dwp file name was recorded in the executable, akin to
12898 .gnu_debuglink, but that doesn't exist yet.]
12899 Strip the directory from FILE_NAME and search again. */
12900 if (*debug_file_directory != '\0')
12901 {
12902 /* Don't implicitly search the current directory here.
12903 If the user wants to search "." to handle this case,
12904 it must be added to debug-file-directory. */
12905 return try_open_dwop_file (dwarf2_per_objfile,
12906 lbasename (file_name), 1 /*is_dwp*/,
12907 0 /*search_cwd*/);
12908 }
12909
12910 return NULL;
12911 }
12912
12913 /* Initialize the use of the DWP file for the current objfile.
12914 By convention the name of the DWP file is ${objfile}.dwp.
12915 The result is NULL if it can't be found. */
12916
12917 static std::unique_ptr<struct dwp_file>
12918 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12919 {
12920 struct objfile *objfile = dwarf2_per_objfile->objfile;
12921
12922 /* Try to find first .dwp for the binary file before any symbolic links
12923 resolving. */
12924
12925 /* If the objfile is a debug file, find the name of the real binary
12926 file and get the name of dwp file from there. */
12927 std::string dwp_name;
12928 if (objfile->separate_debug_objfile_backlink != NULL)
12929 {
12930 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12931 const char *backlink_basename = lbasename (backlink->original_name);
12932
12933 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12934 }
12935 else
12936 dwp_name = objfile->original_name;
12937
12938 dwp_name += ".dwp";
12939
12940 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12941 if (dbfd == NULL
12942 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12943 {
12944 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12945 dwp_name = objfile_name (objfile);
12946 dwp_name += ".dwp";
12947 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12948 }
12949
12950 if (dbfd == NULL)
12951 {
12952 if (dwarf_read_debug)
12953 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12954 return std::unique_ptr<dwp_file> ();
12955 }
12956
12957 const char *name = bfd_get_filename (dbfd.get ());
12958 std::unique_ptr<struct dwp_file> dwp_file
12959 (new struct dwp_file (name, std::move (dbfd)));
12960
12961 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12962 dwp_file->elf_sections =
12963 OBSTACK_CALLOC (&objfile->objfile_obstack,
12964 dwp_file->num_sections, asection *);
12965
12966 bfd_map_over_sections (dwp_file->dbfd.get (),
12967 dwarf2_locate_common_dwp_sections,
12968 dwp_file.get ());
12969
12970 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12971 0);
12972
12973 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12974 1);
12975
12976 /* The DWP file version is stored in the hash table. Oh well. */
12977 if (dwp_file->cus && dwp_file->tus
12978 && dwp_file->cus->version != dwp_file->tus->version)
12979 {
12980 /* Technically speaking, we should try to limp along, but this is
12981 pretty bizarre. We use pulongest here because that's the established
12982 portability solution (e.g, we cannot use %u for uint32_t). */
12983 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12984 " TU version %s [in DWP file %s]"),
12985 pulongest (dwp_file->cus->version),
12986 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12987 }
12988
12989 if (dwp_file->cus)
12990 dwp_file->version = dwp_file->cus->version;
12991 else if (dwp_file->tus)
12992 dwp_file->version = dwp_file->tus->version;
12993 else
12994 dwp_file->version = 2;
12995
12996 if (dwp_file->version == 2)
12997 bfd_map_over_sections (dwp_file->dbfd.get (),
12998 dwarf2_locate_v2_dwp_sections,
12999 dwp_file.get ());
13000
13001 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13002 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13003
13004 if (dwarf_read_debug)
13005 {
13006 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13007 fprintf_unfiltered (gdb_stdlog,
13008 " %s CUs, %s TUs\n",
13009 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13010 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13011 }
13012
13013 return dwp_file;
13014 }
13015
13016 /* Wrapper around open_and_init_dwp_file, only open it once. */
13017
13018 static struct dwp_file *
13019 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13020 {
13021 if (! dwarf2_per_objfile->dwp_checked)
13022 {
13023 dwarf2_per_objfile->dwp_file
13024 = open_and_init_dwp_file (dwarf2_per_objfile);
13025 dwarf2_per_objfile->dwp_checked = 1;
13026 }
13027 return dwarf2_per_objfile->dwp_file.get ();
13028 }
13029
13030 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13031 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13032 or in the DWP file for the objfile, referenced by THIS_UNIT.
13033 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13034 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13035
13036 This is called, for example, when wanting to read a variable with a
13037 complex location. Therefore we don't want to do file i/o for every call.
13038 Therefore we don't want to look for a DWO file on every call.
13039 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13040 then we check if we've already seen DWO_NAME, and only THEN do we check
13041 for a DWO file.
13042
13043 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13044 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13045
13046 static struct dwo_unit *
13047 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13048 const char *dwo_name, const char *comp_dir,
13049 ULONGEST signature, int is_debug_types)
13050 {
13051 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13052 struct objfile *objfile = dwarf2_per_objfile->objfile;
13053 const char *kind = is_debug_types ? "TU" : "CU";
13054 void **dwo_file_slot;
13055 struct dwo_file *dwo_file;
13056 struct dwp_file *dwp_file;
13057
13058 /* First see if there's a DWP file.
13059 If we have a DWP file but didn't find the DWO inside it, don't
13060 look for the original DWO file. It makes gdb behave differently
13061 depending on whether one is debugging in the build tree. */
13062
13063 dwp_file = get_dwp_file (dwarf2_per_objfile);
13064 if (dwp_file != NULL)
13065 {
13066 const struct dwp_hash_table *dwp_htab =
13067 is_debug_types ? dwp_file->tus : dwp_file->cus;
13068
13069 if (dwp_htab != NULL)
13070 {
13071 struct dwo_unit *dwo_cutu =
13072 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13073 signature, is_debug_types);
13074
13075 if (dwo_cutu != NULL)
13076 {
13077 if (dwarf_read_debug)
13078 {
13079 fprintf_unfiltered (gdb_stdlog,
13080 "Virtual DWO %s %s found: @%s\n",
13081 kind, hex_string (signature),
13082 host_address_to_string (dwo_cutu));
13083 }
13084 return dwo_cutu;
13085 }
13086 }
13087 }
13088 else
13089 {
13090 /* No DWP file, look for the DWO file. */
13091
13092 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13093 dwo_name, comp_dir);
13094 if (*dwo_file_slot == NULL)
13095 {
13096 /* Read in the file and build a table of the CUs/TUs it contains. */
13097 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13098 }
13099 /* NOTE: This will be NULL if unable to open the file. */
13100 dwo_file = (struct dwo_file *) *dwo_file_slot;
13101
13102 if (dwo_file != NULL)
13103 {
13104 struct dwo_unit *dwo_cutu = NULL;
13105
13106 if (is_debug_types && dwo_file->tus)
13107 {
13108 struct dwo_unit find_dwo_cutu;
13109
13110 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13111 find_dwo_cutu.signature = signature;
13112 dwo_cutu
13113 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13114 }
13115 else if (!is_debug_types && dwo_file->cus)
13116 {
13117 struct dwo_unit find_dwo_cutu;
13118
13119 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13120 find_dwo_cutu.signature = signature;
13121 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13122 &find_dwo_cutu);
13123 }
13124
13125 if (dwo_cutu != NULL)
13126 {
13127 if (dwarf_read_debug)
13128 {
13129 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13130 kind, dwo_name, hex_string (signature),
13131 host_address_to_string (dwo_cutu));
13132 }
13133 return dwo_cutu;
13134 }
13135 }
13136 }
13137
13138 /* We didn't find it. This could mean a dwo_id mismatch, or
13139 someone deleted the DWO/DWP file, or the search path isn't set up
13140 correctly to find the file. */
13141
13142 if (dwarf_read_debug)
13143 {
13144 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13145 kind, dwo_name, hex_string (signature));
13146 }
13147
13148 /* This is a warning and not a complaint because it can be caused by
13149 pilot error (e.g., user accidentally deleting the DWO). */
13150 {
13151 /* Print the name of the DWP file if we looked there, helps the user
13152 better diagnose the problem. */
13153 std::string dwp_text;
13154
13155 if (dwp_file != NULL)
13156 dwp_text = string_printf (" [in DWP file %s]",
13157 lbasename (dwp_file->name));
13158
13159 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13160 " [in module %s]"),
13161 kind, dwo_name, hex_string (signature),
13162 dwp_text.c_str (),
13163 this_unit->is_debug_types ? "TU" : "CU",
13164 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13165 }
13166 return NULL;
13167 }
13168
13169 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13170 See lookup_dwo_cutu_unit for details. */
13171
13172 static struct dwo_unit *
13173 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13174 const char *dwo_name, const char *comp_dir,
13175 ULONGEST signature)
13176 {
13177 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13178 }
13179
13180 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13181 See lookup_dwo_cutu_unit for details. */
13182
13183 static struct dwo_unit *
13184 lookup_dwo_type_unit (struct signatured_type *this_tu,
13185 const char *dwo_name, const char *comp_dir)
13186 {
13187 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13188 }
13189
13190 /* Traversal function for queue_and_load_all_dwo_tus. */
13191
13192 static int
13193 queue_and_load_dwo_tu (void **slot, void *info)
13194 {
13195 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13196 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13197 ULONGEST signature = dwo_unit->signature;
13198 struct signatured_type *sig_type =
13199 lookup_dwo_signatured_type (per_cu->cu, signature);
13200
13201 if (sig_type != NULL)
13202 {
13203 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13204
13205 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13206 a real dependency of PER_CU on SIG_TYPE. That is detected later
13207 while processing PER_CU. */
13208 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13209 load_full_type_unit (sig_cu);
13210 per_cu->imported_symtabs_push (sig_cu);
13211 }
13212
13213 return 1;
13214 }
13215
13216 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13217 The DWO may have the only definition of the type, though it may not be
13218 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13219 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13220
13221 static void
13222 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13223 {
13224 struct dwo_unit *dwo_unit;
13225 struct dwo_file *dwo_file;
13226
13227 gdb_assert (!per_cu->is_debug_types);
13228 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13229 gdb_assert (per_cu->cu != NULL);
13230
13231 dwo_unit = per_cu->cu->dwo_unit;
13232 gdb_assert (dwo_unit != NULL);
13233
13234 dwo_file = dwo_unit->dwo_file;
13235 if (dwo_file->tus != NULL)
13236 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13237 }
13238
13239 /* Read in various DIEs. */
13240
13241 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13242 Inherit only the children of the DW_AT_abstract_origin DIE not being
13243 already referenced by DW_AT_abstract_origin from the children of the
13244 current DIE. */
13245
13246 static void
13247 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13248 {
13249 struct die_info *child_die;
13250 sect_offset *offsetp;
13251 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13252 struct die_info *origin_die;
13253 /* Iterator of the ORIGIN_DIE children. */
13254 struct die_info *origin_child_die;
13255 struct attribute *attr;
13256 struct dwarf2_cu *origin_cu;
13257 struct pending **origin_previous_list_in_scope;
13258
13259 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13260 if (!attr)
13261 return;
13262
13263 /* Note that following die references may follow to a die in a
13264 different cu. */
13265
13266 origin_cu = cu;
13267 origin_die = follow_die_ref (die, attr, &origin_cu);
13268
13269 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13270 symbols in. */
13271 origin_previous_list_in_scope = origin_cu->list_in_scope;
13272 origin_cu->list_in_scope = cu->list_in_scope;
13273
13274 if (die->tag != origin_die->tag
13275 && !(die->tag == DW_TAG_inlined_subroutine
13276 && origin_die->tag == DW_TAG_subprogram))
13277 complaint (_("DIE %s and its abstract origin %s have different tags"),
13278 sect_offset_str (die->sect_off),
13279 sect_offset_str (origin_die->sect_off));
13280
13281 std::vector<sect_offset> offsets;
13282
13283 for (child_die = die->child;
13284 child_die && child_die->tag;
13285 child_die = sibling_die (child_die))
13286 {
13287 struct die_info *child_origin_die;
13288 struct dwarf2_cu *child_origin_cu;
13289
13290 /* We are trying to process concrete instance entries:
13291 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13292 it's not relevant to our analysis here. i.e. detecting DIEs that are
13293 present in the abstract instance but not referenced in the concrete
13294 one. */
13295 if (child_die->tag == DW_TAG_call_site
13296 || child_die->tag == DW_TAG_GNU_call_site)
13297 continue;
13298
13299 /* For each CHILD_DIE, find the corresponding child of
13300 ORIGIN_DIE. If there is more than one layer of
13301 DW_AT_abstract_origin, follow them all; there shouldn't be,
13302 but GCC versions at least through 4.4 generate this (GCC PR
13303 40573). */
13304 child_origin_die = child_die;
13305 child_origin_cu = cu;
13306 while (1)
13307 {
13308 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13309 child_origin_cu);
13310 if (attr == NULL)
13311 break;
13312 child_origin_die = follow_die_ref (child_origin_die, attr,
13313 &child_origin_cu);
13314 }
13315
13316 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13317 counterpart may exist. */
13318 if (child_origin_die != child_die)
13319 {
13320 if (child_die->tag != child_origin_die->tag
13321 && !(child_die->tag == DW_TAG_inlined_subroutine
13322 && child_origin_die->tag == DW_TAG_subprogram))
13323 complaint (_("Child DIE %s and its abstract origin %s have "
13324 "different tags"),
13325 sect_offset_str (child_die->sect_off),
13326 sect_offset_str (child_origin_die->sect_off));
13327 if (child_origin_die->parent != origin_die)
13328 complaint (_("Child DIE %s and its abstract origin %s have "
13329 "different parents"),
13330 sect_offset_str (child_die->sect_off),
13331 sect_offset_str (child_origin_die->sect_off));
13332 else
13333 offsets.push_back (child_origin_die->sect_off);
13334 }
13335 }
13336 std::sort (offsets.begin (), offsets.end ());
13337 sect_offset *offsets_end = offsets.data () + offsets.size ();
13338 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13339 if (offsetp[-1] == *offsetp)
13340 complaint (_("Multiple children of DIE %s refer "
13341 "to DIE %s as their abstract origin"),
13342 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13343
13344 offsetp = offsets.data ();
13345 origin_child_die = origin_die->child;
13346 while (origin_child_die && origin_child_die->tag)
13347 {
13348 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13349 while (offsetp < offsets_end
13350 && *offsetp < origin_child_die->sect_off)
13351 offsetp++;
13352 if (offsetp >= offsets_end
13353 || *offsetp > origin_child_die->sect_off)
13354 {
13355 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13356 Check whether we're already processing ORIGIN_CHILD_DIE.
13357 This can happen with mutually referenced abstract_origins.
13358 PR 16581. */
13359 if (!origin_child_die->in_process)
13360 process_die (origin_child_die, origin_cu);
13361 }
13362 origin_child_die = sibling_die (origin_child_die);
13363 }
13364 origin_cu->list_in_scope = origin_previous_list_in_scope;
13365
13366 if (cu != origin_cu)
13367 compute_delayed_physnames (origin_cu);
13368 }
13369
13370 static void
13371 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13372 {
13373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13374 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13375 struct context_stack *newobj;
13376 CORE_ADDR lowpc;
13377 CORE_ADDR highpc;
13378 struct die_info *child_die;
13379 struct attribute *attr, *call_line, *call_file;
13380 const char *name;
13381 CORE_ADDR baseaddr;
13382 struct block *block;
13383 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13384 std::vector<struct symbol *> template_args;
13385 struct template_symbol *templ_func = NULL;
13386
13387 if (inlined_func)
13388 {
13389 /* If we do not have call site information, we can't show the
13390 caller of this inlined function. That's too confusing, so
13391 only use the scope for local variables. */
13392 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13393 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13394 if (call_line == NULL || call_file == NULL)
13395 {
13396 read_lexical_block_scope (die, cu);
13397 return;
13398 }
13399 }
13400
13401 baseaddr = objfile->text_section_offset ();
13402
13403 name = dwarf2_name (die, cu);
13404
13405 /* Ignore functions with missing or empty names. These are actually
13406 illegal according to the DWARF standard. */
13407 if (name == NULL)
13408 {
13409 complaint (_("missing name for subprogram DIE at %s"),
13410 sect_offset_str (die->sect_off));
13411 return;
13412 }
13413
13414 /* Ignore functions with missing or invalid low and high pc attributes. */
13415 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13416 <= PC_BOUNDS_INVALID)
13417 {
13418 attr = dwarf2_attr (die, DW_AT_external, cu);
13419 if (!attr || !DW_UNSND (attr))
13420 complaint (_("cannot get low and high bounds "
13421 "for subprogram DIE at %s"),
13422 sect_offset_str (die->sect_off));
13423 return;
13424 }
13425
13426 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13427 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13428
13429 /* If we have any template arguments, then we must allocate a
13430 different sort of symbol. */
13431 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13432 {
13433 if (child_die->tag == DW_TAG_template_type_param
13434 || child_die->tag == DW_TAG_template_value_param)
13435 {
13436 templ_func = allocate_template_symbol (objfile);
13437 templ_func->subclass = SYMBOL_TEMPLATE;
13438 break;
13439 }
13440 }
13441
13442 newobj = cu->get_builder ()->push_context (0, lowpc);
13443 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13444 (struct symbol *) templ_func);
13445
13446 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13447 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13448 cu->language);
13449
13450 /* If there is a location expression for DW_AT_frame_base, record
13451 it. */
13452 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13453 if (attr != nullptr)
13454 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13455
13456 /* If there is a location for the static link, record it. */
13457 newobj->static_link = NULL;
13458 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13459 if (attr != nullptr)
13460 {
13461 newobj->static_link
13462 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13463 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13464 dwarf2_per_cu_addr_type (cu->per_cu));
13465 }
13466
13467 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13468
13469 if (die->child != NULL)
13470 {
13471 child_die = die->child;
13472 while (child_die && child_die->tag)
13473 {
13474 if (child_die->tag == DW_TAG_template_type_param
13475 || child_die->tag == DW_TAG_template_value_param)
13476 {
13477 struct symbol *arg = new_symbol (child_die, NULL, cu);
13478
13479 if (arg != NULL)
13480 template_args.push_back (arg);
13481 }
13482 else
13483 process_die (child_die, cu);
13484 child_die = sibling_die (child_die);
13485 }
13486 }
13487
13488 inherit_abstract_dies (die, cu);
13489
13490 /* If we have a DW_AT_specification, we might need to import using
13491 directives from the context of the specification DIE. See the
13492 comment in determine_prefix. */
13493 if (cu->language == language_cplus
13494 && dwarf2_attr (die, DW_AT_specification, cu))
13495 {
13496 struct dwarf2_cu *spec_cu = cu;
13497 struct die_info *spec_die = die_specification (die, &spec_cu);
13498
13499 while (spec_die)
13500 {
13501 child_die = spec_die->child;
13502 while (child_die && child_die->tag)
13503 {
13504 if (child_die->tag == DW_TAG_imported_module)
13505 process_die (child_die, spec_cu);
13506 child_die = sibling_die (child_die);
13507 }
13508
13509 /* In some cases, GCC generates specification DIEs that
13510 themselves contain DW_AT_specification attributes. */
13511 spec_die = die_specification (spec_die, &spec_cu);
13512 }
13513 }
13514
13515 struct context_stack cstk = cu->get_builder ()->pop_context ();
13516 /* Make a block for the local symbols within. */
13517 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13518 cstk.static_link, lowpc, highpc);
13519
13520 /* For C++, set the block's scope. */
13521 if ((cu->language == language_cplus
13522 || cu->language == language_fortran
13523 || cu->language == language_d
13524 || cu->language == language_rust)
13525 && cu->processing_has_namespace_info)
13526 block_set_scope (block, determine_prefix (die, cu),
13527 &objfile->objfile_obstack);
13528
13529 /* If we have address ranges, record them. */
13530 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13531
13532 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13533
13534 /* Attach template arguments to function. */
13535 if (!template_args.empty ())
13536 {
13537 gdb_assert (templ_func != NULL);
13538
13539 templ_func->n_template_arguments = template_args.size ();
13540 templ_func->template_arguments
13541 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13542 templ_func->n_template_arguments);
13543 memcpy (templ_func->template_arguments,
13544 template_args.data (),
13545 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13546
13547 /* Make sure that the symtab is set on the new symbols. Even
13548 though they don't appear in this symtab directly, other parts
13549 of gdb assume that symbols do, and this is reasonably
13550 true. */
13551 for (symbol *sym : template_args)
13552 symbol_set_symtab (sym, symbol_symtab (templ_func));
13553 }
13554
13555 /* In C++, we can have functions nested inside functions (e.g., when
13556 a function declares a class that has methods). This means that
13557 when we finish processing a function scope, we may need to go
13558 back to building a containing block's symbol lists. */
13559 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13560 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13561
13562 /* If we've finished processing a top-level function, subsequent
13563 symbols go in the file symbol list. */
13564 if (cu->get_builder ()->outermost_context_p ())
13565 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13566 }
13567
13568 /* Process all the DIES contained within a lexical block scope. Start
13569 a new scope, process the dies, and then close the scope. */
13570
13571 static void
13572 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13573 {
13574 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13576 CORE_ADDR lowpc, highpc;
13577 struct die_info *child_die;
13578 CORE_ADDR baseaddr;
13579
13580 baseaddr = objfile->text_section_offset ();
13581
13582 /* Ignore blocks with missing or invalid low and high pc attributes. */
13583 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13584 as multiple lexical blocks? Handling children in a sane way would
13585 be nasty. Might be easier to properly extend generic blocks to
13586 describe ranges. */
13587 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13588 {
13589 case PC_BOUNDS_NOT_PRESENT:
13590 /* DW_TAG_lexical_block has no attributes, process its children as if
13591 there was no wrapping by that DW_TAG_lexical_block.
13592 GCC does no longer produces such DWARF since GCC r224161. */
13593 for (child_die = die->child;
13594 child_die != NULL && child_die->tag;
13595 child_die = sibling_die (child_die))
13596 process_die (child_die, cu);
13597 return;
13598 case PC_BOUNDS_INVALID:
13599 return;
13600 }
13601 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13602 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13603
13604 cu->get_builder ()->push_context (0, lowpc);
13605 if (die->child != NULL)
13606 {
13607 child_die = die->child;
13608 while (child_die && child_die->tag)
13609 {
13610 process_die (child_die, cu);
13611 child_die = sibling_die (child_die);
13612 }
13613 }
13614 inherit_abstract_dies (die, cu);
13615 struct context_stack cstk = cu->get_builder ()->pop_context ();
13616
13617 if (*cu->get_builder ()->get_local_symbols () != NULL
13618 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13619 {
13620 struct block *block
13621 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13622 cstk.start_addr, highpc);
13623
13624 /* Note that recording ranges after traversing children, as we
13625 do here, means that recording a parent's ranges entails
13626 walking across all its children's ranges as they appear in
13627 the address map, which is quadratic behavior.
13628
13629 It would be nicer to record the parent's ranges before
13630 traversing its children, simply overriding whatever you find
13631 there. But since we don't even decide whether to create a
13632 block until after we've traversed its children, that's hard
13633 to do. */
13634 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13635 }
13636 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13637 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13638 }
13639
13640 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13641
13642 static void
13643 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13644 {
13645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13646 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13647 CORE_ADDR pc, baseaddr;
13648 struct attribute *attr;
13649 struct call_site *call_site, call_site_local;
13650 void **slot;
13651 int nparams;
13652 struct die_info *child_die;
13653
13654 baseaddr = objfile->text_section_offset ();
13655
13656 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13657 if (attr == NULL)
13658 {
13659 /* This was a pre-DWARF-5 GNU extension alias
13660 for DW_AT_call_return_pc. */
13661 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13662 }
13663 if (!attr)
13664 {
13665 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13666 "DIE %s [in module %s]"),
13667 sect_offset_str (die->sect_off), objfile_name (objfile));
13668 return;
13669 }
13670 pc = attr_value_as_address (attr) + baseaddr;
13671 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13672
13673 if (cu->call_site_htab == NULL)
13674 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13675 NULL, &objfile->objfile_obstack,
13676 hashtab_obstack_allocate, NULL);
13677 call_site_local.pc = pc;
13678 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13679 if (*slot != NULL)
13680 {
13681 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13682 "DIE %s [in module %s]"),
13683 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13684 objfile_name (objfile));
13685 return;
13686 }
13687
13688 /* Count parameters at the caller. */
13689
13690 nparams = 0;
13691 for (child_die = die->child; child_die && child_die->tag;
13692 child_die = sibling_die (child_die))
13693 {
13694 if (child_die->tag != DW_TAG_call_site_parameter
13695 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13696 {
13697 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13698 "DW_TAG_call_site child DIE %s [in module %s]"),
13699 child_die->tag, sect_offset_str (child_die->sect_off),
13700 objfile_name (objfile));
13701 continue;
13702 }
13703
13704 nparams++;
13705 }
13706
13707 call_site
13708 = ((struct call_site *)
13709 obstack_alloc (&objfile->objfile_obstack,
13710 sizeof (*call_site)
13711 + (sizeof (*call_site->parameter) * (nparams - 1))));
13712 *slot = call_site;
13713 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13714 call_site->pc = pc;
13715
13716 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13717 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13718 {
13719 struct die_info *func_die;
13720
13721 /* Skip also over DW_TAG_inlined_subroutine. */
13722 for (func_die = die->parent;
13723 func_die && func_die->tag != DW_TAG_subprogram
13724 && func_die->tag != DW_TAG_subroutine_type;
13725 func_die = func_die->parent);
13726
13727 /* DW_AT_call_all_calls is a superset
13728 of DW_AT_call_all_tail_calls. */
13729 if (func_die
13730 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13731 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13732 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13733 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13734 {
13735 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13736 not complete. But keep CALL_SITE for look ups via call_site_htab,
13737 both the initial caller containing the real return address PC and
13738 the final callee containing the current PC of a chain of tail
13739 calls do not need to have the tail call list complete. But any
13740 function candidate for a virtual tail call frame searched via
13741 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13742 determined unambiguously. */
13743 }
13744 else
13745 {
13746 struct type *func_type = NULL;
13747
13748 if (func_die)
13749 func_type = get_die_type (func_die, cu);
13750 if (func_type != NULL)
13751 {
13752 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13753
13754 /* Enlist this call site to the function. */
13755 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13756 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13757 }
13758 else
13759 complaint (_("Cannot find function owning DW_TAG_call_site "
13760 "DIE %s [in module %s]"),
13761 sect_offset_str (die->sect_off), objfile_name (objfile));
13762 }
13763 }
13764
13765 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13766 if (attr == NULL)
13767 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13768 if (attr == NULL)
13769 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13770 if (attr == NULL)
13771 {
13772 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13773 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13774 }
13775 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13776 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13777 /* Keep NULL DWARF_BLOCK. */;
13778 else if (attr_form_is_block (attr))
13779 {
13780 struct dwarf2_locexpr_baton *dlbaton;
13781
13782 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13783 dlbaton->data = DW_BLOCK (attr)->data;
13784 dlbaton->size = DW_BLOCK (attr)->size;
13785 dlbaton->per_cu = cu->per_cu;
13786
13787 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13788 }
13789 else if (attr_form_is_ref (attr))
13790 {
13791 struct dwarf2_cu *target_cu = cu;
13792 struct die_info *target_die;
13793
13794 target_die = follow_die_ref (die, attr, &target_cu);
13795 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13796 if (die_is_declaration (target_die, target_cu))
13797 {
13798 const char *target_physname;
13799
13800 /* Prefer the mangled name; otherwise compute the demangled one. */
13801 target_physname = dw2_linkage_name (target_die, target_cu);
13802 if (target_physname == NULL)
13803 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13804 if (target_physname == NULL)
13805 complaint (_("DW_AT_call_target target DIE has invalid "
13806 "physname, for referencing DIE %s [in module %s]"),
13807 sect_offset_str (die->sect_off), objfile_name (objfile));
13808 else
13809 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13810 }
13811 else
13812 {
13813 CORE_ADDR lowpc;
13814
13815 /* DW_AT_entry_pc should be preferred. */
13816 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13817 <= PC_BOUNDS_INVALID)
13818 complaint (_("DW_AT_call_target target DIE has invalid "
13819 "low pc, for referencing DIE %s [in module %s]"),
13820 sect_offset_str (die->sect_off), objfile_name (objfile));
13821 else
13822 {
13823 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13824 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13825 }
13826 }
13827 }
13828 else
13829 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13830 "block nor reference, for DIE %s [in module %s]"),
13831 sect_offset_str (die->sect_off), objfile_name (objfile));
13832
13833 call_site->per_cu = cu->per_cu;
13834
13835 for (child_die = die->child;
13836 child_die && child_die->tag;
13837 child_die = sibling_die (child_die))
13838 {
13839 struct call_site_parameter *parameter;
13840 struct attribute *loc, *origin;
13841
13842 if (child_die->tag != DW_TAG_call_site_parameter
13843 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13844 {
13845 /* Already printed the complaint above. */
13846 continue;
13847 }
13848
13849 gdb_assert (call_site->parameter_count < nparams);
13850 parameter = &call_site->parameter[call_site->parameter_count];
13851
13852 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13853 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13854 register is contained in DW_AT_call_value. */
13855
13856 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13857 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13858 if (origin == NULL)
13859 {
13860 /* This was a pre-DWARF-5 GNU extension alias
13861 for DW_AT_call_parameter. */
13862 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13863 }
13864 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
13865 {
13866 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13867
13868 sect_offset sect_off
13869 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13870 if (!offset_in_cu_p (&cu->header, sect_off))
13871 {
13872 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13873 binding can be done only inside one CU. Such referenced DIE
13874 therefore cannot be even moved to DW_TAG_partial_unit. */
13875 complaint (_("DW_AT_call_parameter offset is not in CU for "
13876 "DW_TAG_call_site child DIE %s [in module %s]"),
13877 sect_offset_str (child_die->sect_off),
13878 objfile_name (objfile));
13879 continue;
13880 }
13881 parameter->u.param_cu_off
13882 = (cu_offset) (sect_off - cu->header.sect_off);
13883 }
13884 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
13885 {
13886 complaint (_("No DW_FORM_block* DW_AT_location for "
13887 "DW_TAG_call_site child DIE %s [in module %s]"),
13888 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13889 continue;
13890 }
13891 else
13892 {
13893 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13894 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13895 if (parameter->u.dwarf_reg != -1)
13896 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13897 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13898 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13899 &parameter->u.fb_offset))
13900 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13901 else
13902 {
13903 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13904 "for DW_FORM_block* DW_AT_location is supported for "
13905 "DW_TAG_call_site child DIE %s "
13906 "[in module %s]"),
13907 sect_offset_str (child_die->sect_off),
13908 objfile_name (objfile));
13909 continue;
13910 }
13911 }
13912
13913 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13914 if (attr == NULL)
13915 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13916 if (!attr_form_is_block (attr))
13917 {
13918 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13919 "DW_TAG_call_site child DIE %s [in module %s]"),
13920 sect_offset_str (child_die->sect_off),
13921 objfile_name (objfile));
13922 continue;
13923 }
13924 parameter->value = DW_BLOCK (attr)->data;
13925 parameter->value_size = DW_BLOCK (attr)->size;
13926
13927 /* Parameters are not pre-cleared by memset above. */
13928 parameter->data_value = NULL;
13929 parameter->data_value_size = 0;
13930 call_site->parameter_count++;
13931
13932 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13933 if (attr == NULL)
13934 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13935 if (attr != nullptr)
13936 {
13937 if (!attr_form_is_block (attr))
13938 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13939 "DW_TAG_call_site child DIE %s [in module %s]"),
13940 sect_offset_str (child_die->sect_off),
13941 objfile_name (objfile));
13942 else
13943 {
13944 parameter->data_value = DW_BLOCK (attr)->data;
13945 parameter->data_value_size = DW_BLOCK (attr)->size;
13946 }
13947 }
13948 }
13949 }
13950
13951 /* Helper function for read_variable. If DIE represents a virtual
13952 table, then return the type of the concrete object that is
13953 associated with the virtual table. Otherwise, return NULL. */
13954
13955 static struct type *
13956 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13957 {
13958 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13959 if (attr == NULL)
13960 return NULL;
13961
13962 /* Find the type DIE. */
13963 struct die_info *type_die = NULL;
13964 struct dwarf2_cu *type_cu = cu;
13965
13966 if (attr_form_is_ref (attr))
13967 type_die = follow_die_ref (die, attr, &type_cu);
13968 if (type_die == NULL)
13969 return NULL;
13970
13971 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13972 return NULL;
13973 return die_containing_type (type_die, type_cu);
13974 }
13975
13976 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13977
13978 static void
13979 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13980 {
13981 struct rust_vtable_symbol *storage = NULL;
13982
13983 if (cu->language == language_rust)
13984 {
13985 struct type *containing_type = rust_containing_type (die, cu);
13986
13987 if (containing_type != NULL)
13988 {
13989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13990
13991 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13992 initialize_objfile_symbol (storage);
13993 storage->concrete_type = containing_type;
13994 storage->subclass = SYMBOL_RUST_VTABLE;
13995 }
13996 }
13997
13998 struct symbol *res = new_symbol (die, NULL, cu, storage);
13999 struct attribute *abstract_origin
14000 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14001 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14002 if (res == NULL && loc && abstract_origin)
14003 {
14004 /* We have a variable without a name, but with a location and an abstract
14005 origin. This may be a concrete instance of an abstract variable
14006 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14007 later. */
14008 struct dwarf2_cu *origin_cu = cu;
14009 struct die_info *origin_die
14010 = follow_die_ref (die, abstract_origin, &origin_cu);
14011 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14012 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14013 }
14014 }
14015
14016 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14017 reading .debug_rnglists.
14018 Callback's type should be:
14019 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14020 Return true if the attributes are present and valid, otherwise,
14021 return false. */
14022
14023 template <typename Callback>
14024 static bool
14025 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14026 Callback &&callback)
14027 {
14028 struct dwarf2_per_objfile *dwarf2_per_objfile
14029 = cu->per_cu->dwarf2_per_objfile;
14030 struct objfile *objfile = dwarf2_per_objfile->objfile;
14031 bfd *obfd = objfile->obfd;
14032 /* Base address selection entry. */
14033 CORE_ADDR base;
14034 int found_base;
14035 const gdb_byte *buffer;
14036 CORE_ADDR baseaddr;
14037 bool overflow = false;
14038
14039 found_base = cu->base_known;
14040 base = cu->base_address;
14041
14042 dwarf2_per_objfile->rnglists.read (objfile);
14043 if (offset >= dwarf2_per_objfile->rnglists.size)
14044 {
14045 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14046 offset);
14047 return false;
14048 }
14049 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14050
14051 baseaddr = objfile->text_section_offset ();
14052
14053 while (1)
14054 {
14055 /* Initialize it due to a false compiler warning. */
14056 CORE_ADDR range_beginning = 0, range_end = 0;
14057 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14058 + dwarf2_per_objfile->rnglists.size);
14059 unsigned int bytes_read;
14060
14061 if (buffer == buf_end)
14062 {
14063 overflow = true;
14064 break;
14065 }
14066 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14067 switch (rlet)
14068 {
14069 case DW_RLE_end_of_list:
14070 break;
14071 case DW_RLE_base_address:
14072 if (buffer + cu->header.addr_size > buf_end)
14073 {
14074 overflow = true;
14075 break;
14076 }
14077 base = read_address (obfd, buffer, cu, &bytes_read);
14078 found_base = 1;
14079 buffer += bytes_read;
14080 break;
14081 case DW_RLE_start_length:
14082 if (buffer + cu->header.addr_size > buf_end)
14083 {
14084 overflow = true;
14085 break;
14086 }
14087 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14088 buffer += bytes_read;
14089 range_end = (range_beginning
14090 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14091 buffer += bytes_read;
14092 if (buffer > buf_end)
14093 {
14094 overflow = true;
14095 break;
14096 }
14097 break;
14098 case DW_RLE_offset_pair:
14099 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14100 buffer += bytes_read;
14101 if (buffer > buf_end)
14102 {
14103 overflow = true;
14104 break;
14105 }
14106 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14107 buffer += bytes_read;
14108 if (buffer > buf_end)
14109 {
14110 overflow = true;
14111 break;
14112 }
14113 break;
14114 case DW_RLE_start_end:
14115 if (buffer + 2 * cu->header.addr_size > buf_end)
14116 {
14117 overflow = true;
14118 break;
14119 }
14120 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14121 buffer += bytes_read;
14122 range_end = read_address (obfd, buffer, cu, &bytes_read);
14123 buffer += bytes_read;
14124 break;
14125 default:
14126 complaint (_("Invalid .debug_rnglists data (no base address)"));
14127 return false;
14128 }
14129 if (rlet == DW_RLE_end_of_list || overflow)
14130 break;
14131 if (rlet == DW_RLE_base_address)
14132 continue;
14133
14134 if (!found_base)
14135 {
14136 /* We have no valid base address for the ranges
14137 data. */
14138 complaint (_("Invalid .debug_rnglists data (no base address)"));
14139 return false;
14140 }
14141
14142 if (range_beginning > range_end)
14143 {
14144 /* Inverted range entries are invalid. */
14145 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14146 return false;
14147 }
14148
14149 /* Empty range entries have no effect. */
14150 if (range_beginning == range_end)
14151 continue;
14152
14153 range_beginning += base;
14154 range_end += base;
14155
14156 /* A not-uncommon case of bad debug info.
14157 Don't pollute the addrmap with bad data. */
14158 if (range_beginning + baseaddr == 0
14159 && !dwarf2_per_objfile->has_section_at_zero)
14160 {
14161 complaint (_(".debug_rnglists entry has start address of zero"
14162 " [in module %s]"), objfile_name (objfile));
14163 continue;
14164 }
14165
14166 callback (range_beginning, range_end);
14167 }
14168
14169 if (overflow)
14170 {
14171 complaint (_("Offset %d is not terminated "
14172 "for DW_AT_ranges attribute"),
14173 offset);
14174 return false;
14175 }
14176
14177 return true;
14178 }
14179
14180 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14181 Callback's type should be:
14182 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14183 Return 1 if the attributes are present and valid, otherwise, return 0. */
14184
14185 template <typename Callback>
14186 static int
14187 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14188 Callback &&callback)
14189 {
14190 struct dwarf2_per_objfile *dwarf2_per_objfile
14191 = cu->per_cu->dwarf2_per_objfile;
14192 struct objfile *objfile = dwarf2_per_objfile->objfile;
14193 struct comp_unit_head *cu_header = &cu->header;
14194 bfd *obfd = objfile->obfd;
14195 unsigned int addr_size = cu_header->addr_size;
14196 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14197 /* Base address selection entry. */
14198 CORE_ADDR base;
14199 int found_base;
14200 unsigned int dummy;
14201 const gdb_byte *buffer;
14202 CORE_ADDR baseaddr;
14203
14204 if (cu_header->version >= 5)
14205 return dwarf2_rnglists_process (offset, cu, callback);
14206
14207 found_base = cu->base_known;
14208 base = cu->base_address;
14209
14210 dwarf2_per_objfile->ranges.read (objfile);
14211 if (offset >= dwarf2_per_objfile->ranges.size)
14212 {
14213 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14214 offset);
14215 return 0;
14216 }
14217 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14218
14219 baseaddr = objfile->text_section_offset ();
14220
14221 while (1)
14222 {
14223 CORE_ADDR range_beginning, range_end;
14224
14225 range_beginning = read_address (obfd, buffer, cu, &dummy);
14226 buffer += addr_size;
14227 range_end = read_address (obfd, buffer, cu, &dummy);
14228 buffer += addr_size;
14229 offset += 2 * addr_size;
14230
14231 /* An end of list marker is a pair of zero addresses. */
14232 if (range_beginning == 0 && range_end == 0)
14233 /* Found the end of list entry. */
14234 break;
14235
14236 /* Each base address selection entry is a pair of 2 values.
14237 The first is the largest possible address, the second is
14238 the base address. Check for a base address here. */
14239 if ((range_beginning & mask) == mask)
14240 {
14241 /* If we found the largest possible address, then we already
14242 have the base address in range_end. */
14243 base = range_end;
14244 found_base = 1;
14245 continue;
14246 }
14247
14248 if (!found_base)
14249 {
14250 /* We have no valid base address for the ranges
14251 data. */
14252 complaint (_("Invalid .debug_ranges data (no base address)"));
14253 return 0;
14254 }
14255
14256 if (range_beginning > range_end)
14257 {
14258 /* Inverted range entries are invalid. */
14259 complaint (_("Invalid .debug_ranges data (inverted range)"));
14260 return 0;
14261 }
14262
14263 /* Empty range entries have no effect. */
14264 if (range_beginning == range_end)
14265 continue;
14266
14267 range_beginning += base;
14268 range_end += base;
14269
14270 /* A not-uncommon case of bad debug info.
14271 Don't pollute the addrmap with bad data. */
14272 if (range_beginning + baseaddr == 0
14273 && !dwarf2_per_objfile->has_section_at_zero)
14274 {
14275 complaint (_(".debug_ranges entry has start address of zero"
14276 " [in module %s]"), objfile_name (objfile));
14277 continue;
14278 }
14279
14280 callback (range_beginning, range_end);
14281 }
14282
14283 return 1;
14284 }
14285
14286 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14287 Return 1 if the attributes are present and valid, otherwise, return 0.
14288 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14289
14290 static int
14291 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14292 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14293 dwarf2_psymtab *ranges_pst)
14294 {
14295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14296 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14297 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14298 int low_set = 0;
14299 CORE_ADDR low = 0;
14300 CORE_ADDR high = 0;
14301 int retval;
14302
14303 retval = dwarf2_ranges_process (offset, cu,
14304 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14305 {
14306 if (ranges_pst != NULL)
14307 {
14308 CORE_ADDR lowpc;
14309 CORE_ADDR highpc;
14310
14311 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14312 range_beginning + baseaddr)
14313 - baseaddr);
14314 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14315 range_end + baseaddr)
14316 - baseaddr);
14317 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14318 lowpc, highpc - 1, ranges_pst);
14319 }
14320
14321 /* FIXME: This is recording everything as a low-high
14322 segment of consecutive addresses. We should have a
14323 data structure for discontiguous block ranges
14324 instead. */
14325 if (! low_set)
14326 {
14327 low = range_beginning;
14328 high = range_end;
14329 low_set = 1;
14330 }
14331 else
14332 {
14333 if (range_beginning < low)
14334 low = range_beginning;
14335 if (range_end > high)
14336 high = range_end;
14337 }
14338 });
14339 if (!retval)
14340 return 0;
14341
14342 if (! low_set)
14343 /* If the first entry is an end-of-list marker, the range
14344 describes an empty scope, i.e. no instructions. */
14345 return 0;
14346
14347 if (low_return)
14348 *low_return = low;
14349 if (high_return)
14350 *high_return = high;
14351 return 1;
14352 }
14353
14354 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14355 definition for the return value. *LOWPC and *HIGHPC are set iff
14356 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14357
14358 static enum pc_bounds_kind
14359 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14360 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14361 dwarf2_psymtab *pst)
14362 {
14363 struct dwarf2_per_objfile *dwarf2_per_objfile
14364 = cu->per_cu->dwarf2_per_objfile;
14365 struct attribute *attr;
14366 struct attribute *attr_high;
14367 CORE_ADDR low = 0;
14368 CORE_ADDR high = 0;
14369 enum pc_bounds_kind ret;
14370
14371 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14372 if (attr_high)
14373 {
14374 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14375 if (attr != nullptr)
14376 {
14377 low = attr_value_as_address (attr);
14378 high = attr_value_as_address (attr_high);
14379 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14380 high += low;
14381 }
14382 else
14383 /* Found high w/o low attribute. */
14384 return PC_BOUNDS_INVALID;
14385
14386 /* Found consecutive range of addresses. */
14387 ret = PC_BOUNDS_HIGH_LOW;
14388 }
14389 else
14390 {
14391 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14392 if (attr != NULL)
14393 {
14394 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14395 We take advantage of the fact that DW_AT_ranges does not appear
14396 in DW_TAG_compile_unit of DWO files. */
14397 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14398 unsigned int ranges_offset = (DW_UNSND (attr)
14399 + (need_ranges_base
14400 ? cu->ranges_base
14401 : 0));
14402
14403 /* Value of the DW_AT_ranges attribute is the offset in the
14404 .debug_ranges section. */
14405 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14406 return PC_BOUNDS_INVALID;
14407 /* Found discontinuous range of addresses. */
14408 ret = PC_BOUNDS_RANGES;
14409 }
14410 else
14411 return PC_BOUNDS_NOT_PRESENT;
14412 }
14413
14414 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14415 if (high <= low)
14416 return PC_BOUNDS_INVALID;
14417
14418 /* When using the GNU linker, .gnu.linkonce. sections are used to
14419 eliminate duplicate copies of functions and vtables and such.
14420 The linker will arbitrarily choose one and discard the others.
14421 The AT_*_pc values for such functions refer to local labels in
14422 these sections. If the section from that file was discarded, the
14423 labels are not in the output, so the relocs get a value of 0.
14424 If this is a discarded function, mark the pc bounds as invalid,
14425 so that GDB will ignore it. */
14426 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14427 return PC_BOUNDS_INVALID;
14428
14429 *lowpc = low;
14430 if (highpc)
14431 *highpc = high;
14432 return ret;
14433 }
14434
14435 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14436 its low and high PC addresses. Do nothing if these addresses could not
14437 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14438 and HIGHPC to the high address if greater than HIGHPC. */
14439
14440 static void
14441 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14442 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14443 struct dwarf2_cu *cu)
14444 {
14445 CORE_ADDR low, high;
14446 struct die_info *child = die->child;
14447
14448 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14449 {
14450 *lowpc = std::min (*lowpc, low);
14451 *highpc = std::max (*highpc, high);
14452 }
14453
14454 /* If the language does not allow nested subprograms (either inside
14455 subprograms or lexical blocks), we're done. */
14456 if (cu->language != language_ada)
14457 return;
14458
14459 /* Check all the children of the given DIE. If it contains nested
14460 subprograms, then check their pc bounds. Likewise, we need to
14461 check lexical blocks as well, as they may also contain subprogram
14462 definitions. */
14463 while (child && child->tag)
14464 {
14465 if (child->tag == DW_TAG_subprogram
14466 || child->tag == DW_TAG_lexical_block)
14467 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14468 child = sibling_die (child);
14469 }
14470 }
14471
14472 /* Get the low and high pc's represented by the scope DIE, and store
14473 them in *LOWPC and *HIGHPC. If the correct values can't be
14474 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14475
14476 static void
14477 get_scope_pc_bounds (struct die_info *die,
14478 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14479 struct dwarf2_cu *cu)
14480 {
14481 CORE_ADDR best_low = (CORE_ADDR) -1;
14482 CORE_ADDR best_high = (CORE_ADDR) 0;
14483 CORE_ADDR current_low, current_high;
14484
14485 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14486 >= PC_BOUNDS_RANGES)
14487 {
14488 best_low = current_low;
14489 best_high = current_high;
14490 }
14491 else
14492 {
14493 struct die_info *child = die->child;
14494
14495 while (child && child->tag)
14496 {
14497 switch (child->tag) {
14498 case DW_TAG_subprogram:
14499 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14500 break;
14501 case DW_TAG_namespace:
14502 case DW_TAG_module:
14503 /* FIXME: carlton/2004-01-16: Should we do this for
14504 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14505 that current GCC's always emit the DIEs corresponding
14506 to definitions of methods of classes as children of a
14507 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14508 the DIEs giving the declarations, which could be
14509 anywhere). But I don't see any reason why the
14510 standards says that they have to be there. */
14511 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14512
14513 if (current_low != ((CORE_ADDR) -1))
14514 {
14515 best_low = std::min (best_low, current_low);
14516 best_high = std::max (best_high, current_high);
14517 }
14518 break;
14519 default:
14520 /* Ignore. */
14521 break;
14522 }
14523
14524 child = sibling_die (child);
14525 }
14526 }
14527
14528 *lowpc = best_low;
14529 *highpc = best_high;
14530 }
14531
14532 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14533 in DIE. */
14534
14535 static void
14536 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14537 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14538 {
14539 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14540 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14541 struct attribute *attr;
14542 struct attribute *attr_high;
14543
14544 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14545 if (attr_high)
14546 {
14547 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14548 if (attr != nullptr)
14549 {
14550 CORE_ADDR low = attr_value_as_address (attr);
14551 CORE_ADDR high = attr_value_as_address (attr_high);
14552
14553 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14554 high += low;
14555
14556 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14557 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14558 cu->get_builder ()->record_block_range (block, low, high - 1);
14559 }
14560 }
14561
14562 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14563 if (attr != nullptr)
14564 {
14565 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14566 We take advantage of the fact that DW_AT_ranges does not appear
14567 in DW_TAG_compile_unit of DWO files. */
14568 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14569
14570 /* The value of the DW_AT_ranges attribute is the offset of the
14571 address range list in the .debug_ranges section. */
14572 unsigned long offset = (DW_UNSND (attr)
14573 + (need_ranges_base ? cu->ranges_base : 0));
14574
14575 std::vector<blockrange> blockvec;
14576 dwarf2_ranges_process (offset, cu,
14577 [&] (CORE_ADDR start, CORE_ADDR end)
14578 {
14579 start += baseaddr;
14580 end += baseaddr;
14581 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14582 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14583 cu->get_builder ()->record_block_range (block, start, end - 1);
14584 blockvec.emplace_back (start, end);
14585 });
14586
14587 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14588 }
14589 }
14590
14591 /* Check whether the producer field indicates either of GCC < 4.6, or the
14592 Intel C/C++ compiler, and cache the result in CU. */
14593
14594 static void
14595 check_producer (struct dwarf2_cu *cu)
14596 {
14597 int major, minor;
14598
14599 if (cu->producer == NULL)
14600 {
14601 /* For unknown compilers expect their behavior is DWARF version
14602 compliant.
14603
14604 GCC started to support .debug_types sections by -gdwarf-4 since
14605 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14606 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14607 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14608 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14609 }
14610 else if (producer_is_gcc (cu->producer, &major, &minor))
14611 {
14612 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14613 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14614 }
14615 else if (producer_is_icc (cu->producer, &major, &minor))
14616 {
14617 cu->producer_is_icc = true;
14618 cu->producer_is_icc_lt_14 = major < 14;
14619 }
14620 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14621 cu->producer_is_codewarrior = true;
14622 else
14623 {
14624 /* For other non-GCC compilers, expect their behavior is DWARF version
14625 compliant. */
14626 }
14627
14628 cu->checked_producer = true;
14629 }
14630
14631 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14632 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14633 during 4.6.0 experimental. */
14634
14635 static bool
14636 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14637 {
14638 if (!cu->checked_producer)
14639 check_producer (cu);
14640
14641 return cu->producer_is_gxx_lt_4_6;
14642 }
14643
14644
14645 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14646 with incorrect is_stmt attributes. */
14647
14648 static bool
14649 producer_is_codewarrior (struct dwarf2_cu *cu)
14650 {
14651 if (!cu->checked_producer)
14652 check_producer (cu);
14653
14654 return cu->producer_is_codewarrior;
14655 }
14656
14657 /* Return the default accessibility type if it is not overridden by
14658 DW_AT_accessibility. */
14659
14660 static enum dwarf_access_attribute
14661 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14662 {
14663 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14664 {
14665 /* The default DWARF 2 accessibility for members is public, the default
14666 accessibility for inheritance is private. */
14667
14668 if (die->tag != DW_TAG_inheritance)
14669 return DW_ACCESS_public;
14670 else
14671 return DW_ACCESS_private;
14672 }
14673 else
14674 {
14675 /* DWARF 3+ defines the default accessibility a different way. The same
14676 rules apply now for DW_TAG_inheritance as for the members and it only
14677 depends on the container kind. */
14678
14679 if (die->parent->tag == DW_TAG_class_type)
14680 return DW_ACCESS_private;
14681 else
14682 return DW_ACCESS_public;
14683 }
14684 }
14685
14686 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14687 offset. If the attribute was not found return 0, otherwise return
14688 1. If it was found but could not properly be handled, set *OFFSET
14689 to 0. */
14690
14691 static int
14692 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14693 LONGEST *offset)
14694 {
14695 struct attribute *attr;
14696
14697 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14698 if (attr != NULL)
14699 {
14700 *offset = 0;
14701
14702 /* Note that we do not check for a section offset first here.
14703 This is because DW_AT_data_member_location is new in DWARF 4,
14704 so if we see it, we can assume that a constant form is really
14705 a constant and not a section offset. */
14706 if (attr_form_is_constant (attr))
14707 *offset = dwarf2_get_attr_constant_value (attr, 0);
14708 else if (attr_form_is_section_offset (attr))
14709 dwarf2_complex_location_expr_complaint ();
14710 else if (attr_form_is_block (attr))
14711 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14712 else
14713 dwarf2_complex_location_expr_complaint ();
14714
14715 return 1;
14716 }
14717
14718 return 0;
14719 }
14720
14721 /* Add an aggregate field to the field list. */
14722
14723 static void
14724 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14725 struct dwarf2_cu *cu)
14726 {
14727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14729 struct nextfield *new_field;
14730 struct attribute *attr;
14731 struct field *fp;
14732 const char *fieldname = "";
14733
14734 if (die->tag == DW_TAG_inheritance)
14735 {
14736 fip->baseclasses.emplace_back ();
14737 new_field = &fip->baseclasses.back ();
14738 }
14739 else
14740 {
14741 fip->fields.emplace_back ();
14742 new_field = &fip->fields.back ();
14743 }
14744
14745 fip->nfields++;
14746
14747 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14748 if (attr != nullptr)
14749 new_field->accessibility = DW_UNSND (attr);
14750 else
14751 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14752 if (new_field->accessibility != DW_ACCESS_public)
14753 fip->non_public_fields = 1;
14754
14755 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14756 if (attr != nullptr)
14757 new_field->virtuality = DW_UNSND (attr);
14758 else
14759 new_field->virtuality = DW_VIRTUALITY_none;
14760
14761 fp = &new_field->field;
14762
14763 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14764 {
14765 LONGEST offset;
14766
14767 /* Data member other than a C++ static data member. */
14768
14769 /* Get type of field. */
14770 fp->type = die_type (die, cu);
14771
14772 SET_FIELD_BITPOS (*fp, 0);
14773
14774 /* Get bit size of field (zero if none). */
14775 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14776 if (attr != nullptr)
14777 {
14778 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14779 }
14780 else
14781 {
14782 FIELD_BITSIZE (*fp) = 0;
14783 }
14784
14785 /* Get bit offset of field. */
14786 if (handle_data_member_location (die, cu, &offset))
14787 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14788 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14789 if (attr != nullptr)
14790 {
14791 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14792 {
14793 /* For big endian bits, the DW_AT_bit_offset gives the
14794 additional bit offset from the MSB of the containing
14795 anonymous object to the MSB of the field. We don't
14796 have to do anything special since we don't need to
14797 know the size of the anonymous object. */
14798 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14799 }
14800 else
14801 {
14802 /* For little endian bits, compute the bit offset to the
14803 MSB of the anonymous object, subtract off the number of
14804 bits from the MSB of the field to the MSB of the
14805 object, and then subtract off the number of bits of
14806 the field itself. The result is the bit offset of
14807 the LSB of the field. */
14808 int anonymous_size;
14809 int bit_offset = DW_UNSND (attr);
14810
14811 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14812 if (attr != nullptr)
14813 {
14814 /* The size of the anonymous object containing
14815 the bit field is explicit, so use the
14816 indicated size (in bytes). */
14817 anonymous_size = DW_UNSND (attr);
14818 }
14819 else
14820 {
14821 /* The size of the anonymous object containing
14822 the bit field must be inferred from the type
14823 attribute of the data member containing the
14824 bit field. */
14825 anonymous_size = TYPE_LENGTH (fp->type);
14826 }
14827 SET_FIELD_BITPOS (*fp,
14828 (FIELD_BITPOS (*fp)
14829 + anonymous_size * bits_per_byte
14830 - bit_offset - FIELD_BITSIZE (*fp)));
14831 }
14832 }
14833 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14834 if (attr != NULL)
14835 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14836 + dwarf2_get_attr_constant_value (attr, 0)));
14837
14838 /* Get name of field. */
14839 fieldname = dwarf2_name (die, cu);
14840 if (fieldname == NULL)
14841 fieldname = "";
14842
14843 /* The name is already allocated along with this objfile, so we don't
14844 need to duplicate it for the type. */
14845 fp->name = fieldname;
14846
14847 /* Change accessibility for artificial fields (e.g. virtual table
14848 pointer or virtual base class pointer) to private. */
14849 if (dwarf2_attr (die, DW_AT_artificial, cu))
14850 {
14851 FIELD_ARTIFICIAL (*fp) = 1;
14852 new_field->accessibility = DW_ACCESS_private;
14853 fip->non_public_fields = 1;
14854 }
14855 }
14856 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14857 {
14858 /* C++ static member. */
14859
14860 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14861 is a declaration, but all versions of G++ as of this writing
14862 (so through at least 3.2.1) incorrectly generate
14863 DW_TAG_variable tags. */
14864
14865 const char *physname;
14866
14867 /* Get name of field. */
14868 fieldname = dwarf2_name (die, cu);
14869 if (fieldname == NULL)
14870 return;
14871
14872 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14873 if (attr
14874 /* Only create a symbol if this is an external value.
14875 new_symbol checks this and puts the value in the global symbol
14876 table, which we want. If it is not external, new_symbol
14877 will try to put the value in cu->list_in_scope which is wrong. */
14878 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14879 {
14880 /* A static const member, not much different than an enum as far as
14881 we're concerned, except that we can support more types. */
14882 new_symbol (die, NULL, cu);
14883 }
14884
14885 /* Get physical name. */
14886 physname = dwarf2_physname (fieldname, die, cu);
14887
14888 /* The name is already allocated along with this objfile, so we don't
14889 need to duplicate it for the type. */
14890 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14891 FIELD_TYPE (*fp) = die_type (die, cu);
14892 FIELD_NAME (*fp) = fieldname;
14893 }
14894 else if (die->tag == DW_TAG_inheritance)
14895 {
14896 LONGEST offset;
14897
14898 /* C++ base class field. */
14899 if (handle_data_member_location (die, cu, &offset))
14900 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14901 FIELD_BITSIZE (*fp) = 0;
14902 FIELD_TYPE (*fp) = die_type (die, cu);
14903 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14904 }
14905 else if (die->tag == DW_TAG_variant_part)
14906 {
14907 /* process_structure_scope will treat this DIE as a union. */
14908 process_structure_scope (die, cu);
14909
14910 /* The variant part is relative to the start of the enclosing
14911 structure. */
14912 SET_FIELD_BITPOS (*fp, 0);
14913 fp->type = get_die_type (die, cu);
14914 fp->artificial = 1;
14915 fp->name = "<<variant>>";
14916
14917 /* Normally a DW_TAG_variant_part won't have a size, but our
14918 representation requires one, so set it to the maximum of the
14919 child sizes, being sure to account for the offset at which
14920 each child is seen. */
14921 if (TYPE_LENGTH (fp->type) == 0)
14922 {
14923 unsigned max = 0;
14924 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14925 {
14926 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14927 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14928 if (len > max)
14929 max = len;
14930 }
14931 TYPE_LENGTH (fp->type) = max;
14932 }
14933 }
14934 else
14935 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14936 }
14937
14938 /* Can the type given by DIE define another type? */
14939
14940 static bool
14941 type_can_define_types (const struct die_info *die)
14942 {
14943 switch (die->tag)
14944 {
14945 case DW_TAG_typedef:
14946 case DW_TAG_class_type:
14947 case DW_TAG_structure_type:
14948 case DW_TAG_union_type:
14949 case DW_TAG_enumeration_type:
14950 return true;
14951
14952 default:
14953 return false;
14954 }
14955 }
14956
14957 /* Add a type definition defined in the scope of the FIP's class. */
14958
14959 static void
14960 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14961 struct dwarf2_cu *cu)
14962 {
14963 struct decl_field fp;
14964 memset (&fp, 0, sizeof (fp));
14965
14966 gdb_assert (type_can_define_types (die));
14967
14968 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14969 fp.name = dwarf2_name (die, cu);
14970 fp.type = read_type_die (die, cu);
14971
14972 /* Save accessibility. */
14973 enum dwarf_access_attribute accessibility;
14974 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14975 if (attr != NULL)
14976 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14977 else
14978 accessibility = dwarf2_default_access_attribute (die, cu);
14979 switch (accessibility)
14980 {
14981 case DW_ACCESS_public:
14982 /* The assumed value if neither private nor protected. */
14983 break;
14984 case DW_ACCESS_private:
14985 fp.is_private = 1;
14986 break;
14987 case DW_ACCESS_protected:
14988 fp.is_protected = 1;
14989 break;
14990 default:
14991 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14992 }
14993
14994 if (die->tag == DW_TAG_typedef)
14995 fip->typedef_field_list.push_back (fp);
14996 else
14997 fip->nested_types_list.push_back (fp);
14998 }
14999
15000 /* Create the vector of fields, and attach it to the type. */
15001
15002 static void
15003 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15004 struct dwarf2_cu *cu)
15005 {
15006 int nfields = fip->nfields;
15007
15008 /* Record the field count, allocate space for the array of fields,
15009 and create blank accessibility bitfields if necessary. */
15010 TYPE_NFIELDS (type) = nfields;
15011 TYPE_FIELDS (type) = (struct field *)
15012 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15013
15014 if (fip->non_public_fields && cu->language != language_ada)
15015 {
15016 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15017
15018 TYPE_FIELD_PRIVATE_BITS (type) =
15019 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15020 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15021
15022 TYPE_FIELD_PROTECTED_BITS (type) =
15023 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15024 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15025
15026 TYPE_FIELD_IGNORE_BITS (type) =
15027 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15028 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15029 }
15030
15031 /* If the type has baseclasses, allocate and clear a bit vector for
15032 TYPE_FIELD_VIRTUAL_BITS. */
15033 if (!fip->baseclasses.empty () && cu->language != language_ada)
15034 {
15035 int num_bytes = B_BYTES (fip->baseclasses.size ());
15036 unsigned char *pointer;
15037
15038 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15039 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15040 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15041 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15042 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15043 }
15044
15045 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15046 {
15047 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15048
15049 for (int index = 0; index < nfields; ++index)
15050 {
15051 struct nextfield &field = fip->fields[index];
15052
15053 if (field.variant.is_discriminant)
15054 di->discriminant_index = index;
15055 else if (field.variant.default_branch)
15056 di->default_index = index;
15057 else
15058 di->discriminants[index] = field.variant.discriminant_value;
15059 }
15060 }
15061
15062 /* Copy the saved-up fields into the field vector. */
15063 for (int i = 0; i < nfields; ++i)
15064 {
15065 struct nextfield &field
15066 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15067 : fip->fields[i - fip->baseclasses.size ()]);
15068
15069 TYPE_FIELD (type, i) = field.field;
15070 switch (field.accessibility)
15071 {
15072 case DW_ACCESS_private:
15073 if (cu->language != language_ada)
15074 SET_TYPE_FIELD_PRIVATE (type, i);
15075 break;
15076
15077 case DW_ACCESS_protected:
15078 if (cu->language != language_ada)
15079 SET_TYPE_FIELD_PROTECTED (type, i);
15080 break;
15081
15082 case DW_ACCESS_public:
15083 break;
15084
15085 default:
15086 /* Unknown accessibility. Complain and treat it as public. */
15087 {
15088 complaint (_("unsupported accessibility %d"),
15089 field.accessibility);
15090 }
15091 break;
15092 }
15093 if (i < fip->baseclasses.size ())
15094 {
15095 switch (field.virtuality)
15096 {
15097 case DW_VIRTUALITY_virtual:
15098 case DW_VIRTUALITY_pure_virtual:
15099 if (cu->language == language_ada)
15100 error (_("unexpected virtuality in component of Ada type"));
15101 SET_TYPE_FIELD_VIRTUAL (type, i);
15102 break;
15103 }
15104 }
15105 }
15106 }
15107
15108 /* Return true if this member function is a constructor, false
15109 otherwise. */
15110
15111 static int
15112 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15113 {
15114 const char *fieldname;
15115 const char *type_name;
15116 int len;
15117
15118 if (die->parent == NULL)
15119 return 0;
15120
15121 if (die->parent->tag != DW_TAG_structure_type
15122 && die->parent->tag != DW_TAG_union_type
15123 && die->parent->tag != DW_TAG_class_type)
15124 return 0;
15125
15126 fieldname = dwarf2_name (die, cu);
15127 type_name = dwarf2_name (die->parent, cu);
15128 if (fieldname == NULL || type_name == NULL)
15129 return 0;
15130
15131 len = strlen (fieldname);
15132 return (strncmp (fieldname, type_name, len) == 0
15133 && (type_name[len] == '\0' || type_name[len] == '<'));
15134 }
15135
15136 /* Check if the given VALUE is a recognized enum
15137 dwarf_defaulted_attribute constant according to DWARF5 spec,
15138 Table 7.24. */
15139
15140 static bool
15141 is_valid_DW_AT_defaulted (ULONGEST value)
15142 {
15143 switch (value)
15144 {
15145 case DW_DEFAULTED_no:
15146 case DW_DEFAULTED_in_class:
15147 case DW_DEFAULTED_out_of_class:
15148 return true;
15149 }
15150
15151 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15152 return false;
15153 }
15154
15155 /* Add a member function to the proper fieldlist. */
15156
15157 static void
15158 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15159 struct type *type, struct dwarf2_cu *cu)
15160 {
15161 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15162 struct attribute *attr;
15163 int i;
15164 struct fnfieldlist *flp = nullptr;
15165 struct fn_field *fnp;
15166 const char *fieldname;
15167 struct type *this_type;
15168 enum dwarf_access_attribute accessibility;
15169
15170 if (cu->language == language_ada)
15171 error (_("unexpected member function in Ada type"));
15172
15173 /* Get name of member function. */
15174 fieldname = dwarf2_name (die, cu);
15175 if (fieldname == NULL)
15176 return;
15177
15178 /* Look up member function name in fieldlist. */
15179 for (i = 0; i < fip->fnfieldlists.size (); i++)
15180 {
15181 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15182 {
15183 flp = &fip->fnfieldlists[i];
15184 break;
15185 }
15186 }
15187
15188 /* Create a new fnfieldlist if necessary. */
15189 if (flp == nullptr)
15190 {
15191 fip->fnfieldlists.emplace_back ();
15192 flp = &fip->fnfieldlists.back ();
15193 flp->name = fieldname;
15194 i = fip->fnfieldlists.size () - 1;
15195 }
15196
15197 /* Create a new member function field and add it to the vector of
15198 fnfieldlists. */
15199 flp->fnfields.emplace_back ();
15200 fnp = &flp->fnfields.back ();
15201
15202 /* Delay processing of the physname until later. */
15203 if (cu->language == language_cplus)
15204 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15205 die, cu);
15206 else
15207 {
15208 const char *physname = dwarf2_physname (fieldname, die, cu);
15209 fnp->physname = physname ? physname : "";
15210 }
15211
15212 fnp->type = alloc_type (objfile);
15213 this_type = read_type_die (die, cu);
15214 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15215 {
15216 int nparams = TYPE_NFIELDS (this_type);
15217
15218 /* TYPE is the domain of this method, and THIS_TYPE is the type
15219 of the method itself (TYPE_CODE_METHOD). */
15220 smash_to_method_type (fnp->type, type,
15221 TYPE_TARGET_TYPE (this_type),
15222 TYPE_FIELDS (this_type),
15223 TYPE_NFIELDS (this_type),
15224 TYPE_VARARGS (this_type));
15225
15226 /* Handle static member functions.
15227 Dwarf2 has no clean way to discern C++ static and non-static
15228 member functions. G++ helps GDB by marking the first
15229 parameter for non-static member functions (which is the this
15230 pointer) as artificial. We obtain this information from
15231 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15232 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15233 fnp->voffset = VOFFSET_STATIC;
15234 }
15235 else
15236 complaint (_("member function type missing for '%s'"),
15237 dwarf2_full_name (fieldname, die, cu));
15238
15239 /* Get fcontext from DW_AT_containing_type if present. */
15240 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15241 fnp->fcontext = die_containing_type (die, cu);
15242
15243 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15244 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15245
15246 /* Get accessibility. */
15247 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15248 if (attr != nullptr)
15249 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15250 else
15251 accessibility = dwarf2_default_access_attribute (die, cu);
15252 switch (accessibility)
15253 {
15254 case DW_ACCESS_private:
15255 fnp->is_private = 1;
15256 break;
15257 case DW_ACCESS_protected:
15258 fnp->is_protected = 1;
15259 break;
15260 }
15261
15262 /* Check for artificial methods. */
15263 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15264 if (attr && DW_UNSND (attr) != 0)
15265 fnp->is_artificial = 1;
15266
15267 /* Check for defaulted methods. */
15268 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15269 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15270 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15271
15272 /* Check for deleted methods. */
15273 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15274 if (attr != nullptr && DW_UNSND (attr) != 0)
15275 fnp->is_deleted = 1;
15276
15277 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15278
15279 /* Get index in virtual function table if it is a virtual member
15280 function. For older versions of GCC, this is an offset in the
15281 appropriate virtual table, as specified by DW_AT_containing_type.
15282 For everyone else, it is an expression to be evaluated relative
15283 to the object address. */
15284
15285 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15286 if (attr != nullptr)
15287 {
15288 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15289 {
15290 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15291 {
15292 /* Old-style GCC. */
15293 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15294 }
15295 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15296 || (DW_BLOCK (attr)->size > 1
15297 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15298 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15299 {
15300 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15301 if ((fnp->voffset % cu->header.addr_size) != 0)
15302 dwarf2_complex_location_expr_complaint ();
15303 else
15304 fnp->voffset /= cu->header.addr_size;
15305 fnp->voffset += 2;
15306 }
15307 else
15308 dwarf2_complex_location_expr_complaint ();
15309
15310 if (!fnp->fcontext)
15311 {
15312 /* If there is no `this' field and no DW_AT_containing_type,
15313 we cannot actually find a base class context for the
15314 vtable! */
15315 if (TYPE_NFIELDS (this_type) == 0
15316 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15317 {
15318 complaint (_("cannot determine context for virtual member "
15319 "function \"%s\" (offset %s)"),
15320 fieldname, sect_offset_str (die->sect_off));
15321 }
15322 else
15323 {
15324 fnp->fcontext
15325 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15326 }
15327 }
15328 }
15329 else if (attr_form_is_section_offset (attr))
15330 {
15331 dwarf2_complex_location_expr_complaint ();
15332 }
15333 else
15334 {
15335 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15336 fieldname);
15337 }
15338 }
15339 else
15340 {
15341 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15342 if (attr && DW_UNSND (attr))
15343 {
15344 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15345 complaint (_("Member function \"%s\" (offset %s) is virtual "
15346 "but the vtable offset is not specified"),
15347 fieldname, sect_offset_str (die->sect_off));
15348 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15349 TYPE_CPLUS_DYNAMIC (type) = 1;
15350 }
15351 }
15352 }
15353
15354 /* Create the vector of member function fields, and attach it to the type. */
15355
15356 static void
15357 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15358 struct dwarf2_cu *cu)
15359 {
15360 if (cu->language == language_ada)
15361 error (_("unexpected member functions in Ada type"));
15362
15363 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15364 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15365 TYPE_ALLOC (type,
15366 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15367
15368 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15369 {
15370 struct fnfieldlist &nf = fip->fnfieldlists[i];
15371 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15372
15373 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15374 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15375 fn_flp->fn_fields = (struct fn_field *)
15376 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15377
15378 for (int k = 0; k < nf.fnfields.size (); ++k)
15379 fn_flp->fn_fields[k] = nf.fnfields[k];
15380 }
15381
15382 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15383 }
15384
15385 /* Returns non-zero if NAME is the name of a vtable member in CU's
15386 language, zero otherwise. */
15387 static int
15388 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15389 {
15390 static const char vptr[] = "_vptr";
15391
15392 /* Look for the C++ form of the vtable. */
15393 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15394 return 1;
15395
15396 return 0;
15397 }
15398
15399 /* GCC outputs unnamed structures that are really pointers to member
15400 functions, with the ABI-specified layout. If TYPE describes
15401 such a structure, smash it into a member function type.
15402
15403 GCC shouldn't do this; it should just output pointer to member DIEs.
15404 This is GCC PR debug/28767. */
15405
15406 static void
15407 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15408 {
15409 struct type *pfn_type, *self_type, *new_type;
15410
15411 /* Check for a structure with no name and two children. */
15412 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15413 return;
15414
15415 /* Check for __pfn and __delta members. */
15416 if (TYPE_FIELD_NAME (type, 0) == NULL
15417 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15418 || TYPE_FIELD_NAME (type, 1) == NULL
15419 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15420 return;
15421
15422 /* Find the type of the method. */
15423 pfn_type = TYPE_FIELD_TYPE (type, 0);
15424 if (pfn_type == NULL
15425 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15426 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15427 return;
15428
15429 /* Look for the "this" argument. */
15430 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15431 if (TYPE_NFIELDS (pfn_type) == 0
15432 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15433 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15434 return;
15435
15436 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15437 new_type = alloc_type (objfile);
15438 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15439 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15440 TYPE_VARARGS (pfn_type));
15441 smash_to_methodptr_type (type, new_type);
15442 }
15443
15444 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15445 appropriate error checking and issuing complaints if there is a
15446 problem. */
15447
15448 static ULONGEST
15449 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15450 {
15451 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15452
15453 if (attr == nullptr)
15454 return 0;
15455
15456 if (!attr_form_is_constant (attr))
15457 {
15458 complaint (_("DW_AT_alignment must have constant form"
15459 " - DIE at %s [in module %s]"),
15460 sect_offset_str (die->sect_off),
15461 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15462 return 0;
15463 }
15464
15465 ULONGEST align;
15466 if (attr->form == DW_FORM_sdata)
15467 {
15468 LONGEST val = DW_SND (attr);
15469 if (val < 0)
15470 {
15471 complaint (_("DW_AT_alignment value must not be negative"
15472 " - DIE at %s [in module %s]"),
15473 sect_offset_str (die->sect_off),
15474 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15475 return 0;
15476 }
15477 align = val;
15478 }
15479 else
15480 align = DW_UNSND (attr);
15481
15482 if (align == 0)
15483 {
15484 complaint (_("DW_AT_alignment value must not be zero"
15485 " - DIE at %s [in module %s]"),
15486 sect_offset_str (die->sect_off),
15487 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15488 return 0;
15489 }
15490 if ((align & (align - 1)) != 0)
15491 {
15492 complaint (_("DW_AT_alignment value must be a power of 2"
15493 " - DIE at %s [in module %s]"),
15494 sect_offset_str (die->sect_off),
15495 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15496 return 0;
15497 }
15498
15499 return align;
15500 }
15501
15502 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15503 the alignment for TYPE. */
15504
15505 static void
15506 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15507 struct type *type)
15508 {
15509 if (!set_type_align (type, get_alignment (cu, die)))
15510 complaint (_("DW_AT_alignment value too large"
15511 " - DIE at %s [in module %s]"),
15512 sect_offset_str (die->sect_off),
15513 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15514 }
15515
15516 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15517 constant for a type, according to DWARF5 spec, Table 5.5. */
15518
15519 static bool
15520 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15521 {
15522 switch (value)
15523 {
15524 case DW_CC_normal:
15525 case DW_CC_pass_by_reference:
15526 case DW_CC_pass_by_value:
15527 return true;
15528
15529 default:
15530 complaint (_("unrecognized DW_AT_calling_convention value "
15531 "(%s) for a type"), pulongest (value));
15532 return false;
15533 }
15534 }
15535
15536 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15537 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15538 also according to GNU-specific values (see include/dwarf2.h). */
15539
15540 static bool
15541 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15542 {
15543 switch (value)
15544 {
15545 case DW_CC_normal:
15546 case DW_CC_program:
15547 case DW_CC_nocall:
15548 return true;
15549
15550 case DW_CC_GNU_renesas_sh:
15551 case DW_CC_GNU_borland_fastcall_i386:
15552 case DW_CC_GDB_IBM_OpenCL:
15553 return true;
15554
15555 default:
15556 complaint (_("unrecognized DW_AT_calling_convention value "
15557 "(%s) for a subroutine"), pulongest (value));
15558 return false;
15559 }
15560 }
15561
15562 /* Called when we find the DIE that starts a structure or union scope
15563 (definition) to create a type for the structure or union. Fill in
15564 the type's name and general properties; the members will not be
15565 processed until process_structure_scope. A symbol table entry for
15566 the type will also not be done until process_structure_scope (assuming
15567 the type has a name).
15568
15569 NOTE: we need to call these functions regardless of whether or not the
15570 DIE has a DW_AT_name attribute, since it might be an anonymous
15571 structure or union. This gets the type entered into our set of
15572 user defined types. */
15573
15574 static struct type *
15575 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15576 {
15577 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15578 struct type *type;
15579 struct attribute *attr;
15580 const char *name;
15581
15582 /* If the definition of this type lives in .debug_types, read that type.
15583 Don't follow DW_AT_specification though, that will take us back up
15584 the chain and we want to go down. */
15585 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15586 if (attr != nullptr)
15587 {
15588 type = get_DW_AT_signature_type (die, attr, cu);
15589
15590 /* The type's CU may not be the same as CU.
15591 Ensure TYPE is recorded with CU in die_type_hash. */
15592 return set_die_type (die, type, cu);
15593 }
15594
15595 type = alloc_type (objfile);
15596 INIT_CPLUS_SPECIFIC (type);
15597
15598 name = dwarf2_name (die, cu);
15599 if (name != NULL)
15600 {
15601 if (cu->language == language_cplus
15602 || cu->language == language_d
15603 || cu->language == language_rust)
15604 {
15605 const char *full_name = dwarf2_full_name (name, die, cu);
15606
15607 /* dwarf2_full_name might have already finished building the DIE's
15608 type. If so, there is no need to continue. */
15609 if (get_die_type (die, cu) != NULL)
15610 return get_die_type (die, cu);
15611
15612 TYPE_NAME (type) = full_name;
15613 }
15614 else
15615 {
15616 /* The name is already allocated along with this objfile, so
15617 we don't need to duplicate it for the type. */
15618 TYPE_NAME (type) = name;
15619 }
15620 }
15621
15622 if (die->tag == DW_TAG_structure_type)
15623 {
15624 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15625 }
15626 else if (die->tag == DW_TAG_union_type)
15627 {
15628 TYPE_CODE (type) = TYPE_CODE_UNION;
15629 }
15630 else if (die->tag == DW_TAG_variant_part)
15631 {
15632 TYPE_CODE (type) = TYPE_CODE_UNION;
15633 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15634 }
15635 else
15636 {
15637 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15638 }
15639
15640 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15641 TYPE_DECLARED_CLASS (type) = 1;
15642
15643 /* Store the calling convention in the type if it's available in
15644 the die. Otherwise the calling convention remains set to
15645 the default value DW_CC_normal. */
15646 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15647 if (attr != nullptr
15648 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15649 {
15650 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15651 TYPE_CPLUS_CALLING_CONVENTION (type)
15652 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15653 }
15654
15655 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15656 if (attr != nullptr)
15657 {
15658 if (attr_form_is_constant (attr))
15659 TYPE_LENGTH (type) = DW_UNSND (attr);
15660 else
15661 {
15662 /* For the moment, dynamic type sizes are not supported
15663 by GDB's struct type. The actual size is determined
15664 on-demand when resolving the type of a given object,
15665 so set the type's length to zero for now. Otherwise,
15666 we record an expression as the length, and that expression
15667 could lead to a very large value, which could eventually
15668 lead to us trying to allocate that much memory when creating
15669 a value of that type. */
15670 TYPE_LENGTH (type) = 0;
15671 }
15672 }
15673 else
15674 {
15675 TYPE_LENGTH (type) = 0;
15676 }
15677
15678 maybe_set_alignment (cu, die, type);
15679
15680 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15681 {
15682 /* ICC<14 does not output the required DW_AT_declaration on
15683 incomplete types, but gives them a size of zero. */
15684 TYPE_STUB (type) = 1;
15685 }
15686 else
15687 TYPE_STUB_SUPPORTED (type) = 1;
15688
15689 if (die_is_declaration (die, cu))
15690 TYPE_STUB (type) = 1;
15691 else if (attr == NULL && die->child == NULL
15692 && producer_is_realview (cu->producer))
15693 /* RealView does not output the required DW_AT_declaration
15694 on incomplete types. */
15695 TYPE_STUB (type) = 1;
15696
15697 /* We need to add the type field to the die immediately so we don't
15698 infinitely recurse when dealing with pointers to the structure
15699 type within the structure itself. */
15700 set_die_type (die, type, cu);
15701
15702 /* set_die_type should be already done. */
15703 set_descriptive_type (type, die, cu);
15704
15705 return type;
15706 }
15707
15708 /* A helper for process_structure_scope that handles a single member
15709 DIE. */
15710
15711 static void
15712 handle_struct_member_die (struct die_info *child_die, struct type *type,
15713 struct field_info *fi,
15714 std::vector<struct symbol *> *template_args,
15715 struct dwarf2_cu *cu)
15716 {
15717 if (child_die->tag == DW_TAG_member
15718 || child_die->tag == DW_TAG_variable
15719 || child_die->tag == DW_TAG_variant_part)
15720 {
15721 /* NOTE: carlton/2002-11-05: A C++ static data member
15722 should be a DW_TAG_member that is a declaration, but
15723 all versions of G++ as of this writing (so through at
15724 least 3.2.1) incorrectly generate DW_TAG_variable
15725 tags for them instead. */
15726 dwarf2_add_field (fi, child_die, cu);
15727 }
15728 else if (child_die->tag == DW_TAG_subprogram)
15729 {
15730 /* Rust doesn't have member functions in the C++ sense.
15731 However, it does emit ordinary functions as children
15732 of a struct DIE. */
15733 if (cu->language == language_rust)
15734 read_func_scope (child_die, cu);
15735 else
15736 {
15737 /* C++ member function. */
15738 dwarf2_add_member_fn (fi, child_die, type, cu);
15739 }
15740 }
15741 else if (child_die->tag == DW_TAG_inheritance)
15742 {
15743 /* C++ base class field. */
15744 dwarf2_add_field (fi, child_die, cu);
15745 }
15746 else if (type_can_define_types (child_die))
15747 dwarf2_add_type_defn (fi, child_die, cu);
15748 else if (child_die->tag == DW_TAG_template_type_param
15749 || child_die->tag == DW_TAG_template_value_param)
15750 {
15751 struct symbol *arg = new_symbol (child_die, NULL, cu);
15752
15753 if (arg != NULL)
15754 template_args->push_back (arg);
15755 }
15756 else if (child_die->tag == DW_TAG_variant)
15757 {
15758 /* In a variant we want to get the discriminant and also add a
15759 field for our sole member child. */
15760 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15761
15762 for (die_info *variant_child = child_die->child;
15763 variant_child != NULL;
15764 variant_child = sibling_die (variant_child))
15765 {
15766 if (variant_child->tag == DW_TAG_member)
15767 {
15768 handle_struct_member_die (variant_child, type, fi,
15769 template_args, cu);
15770 /* Only handle the one. */
15771 break;
15772 }
15773 }
15774
15775 /* We don't handle this but we might as well report it if we see
15776 it. */
15777 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15778 complaint (_("DW_AT_discr_list is not supported yet"
15779 " - DIE at %s [in module %s]"),
15780 sect_offset_str (child_die->sect_off),
15781 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15782
15783 /* The first field was just added, so we can stash the
15784 discriminant there. */
15785 gdb_assert (!fi->fields.empty ());
15786 if (discr == NULL)
15787 fi->fields.back ().variant.default_branch = true;
15788 else
15789 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15790 }
15791 }
15792
15793 /* Finish creating a structure or union type, including filling in
15794 its members and creating a symbol for it. */
15795
15796 static void
15797 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15798 {
15799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15800 struct die_info *child_die;
15801 struct type *type;
15802
15803 type = get_die_type (die, cu);
15804 if (type == NULL)
15805 type = read_structure_type (die, cu);
15806
15807 /* When reading a DW_TAG_variant_part, we need to notice when we
15808 read the discriminant member, so we can record it later in the
15809 discriminant_info. */
15810 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15811 sect_offset discr_offset {};
15812 bool has_template_parameters = false;
15813
15814 if (is_variant_part)
15815 {
15816 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15817 if (discr == NULL)
15818 {
15819 /* Maybe it's a univariant form, an extension we support.
15820 In this case arrange not to check the offset. */
15821 is_variant_part = false;
15822 }
15823 else if (attr_form_is_ref (discr))
15824 {
15825 struct dwarf2_cu *target_cu = cu;
15826 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15827
15828 discr_offset = target_die->sect_off;
15829 }
15830 else
15831 {
15832 complaint (_("DW_AT_discr does not have DIE reference form"
15833 " - DIE at %s [in module %s]"),
15834 sect_offset_str (die->sect_off),
15835 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15836 is_variant_part = false;
15837 }
15838 }
15839
15840 if (die->child != NULL && ! die_is_declaration (die, cu))
15841 {
15842 struct field_info fi;
15843 std::vector<struct symbol *> template_args;
15844
15845 child_die = die->child;
15846
15847 while (child_die && child_die->tag)
15848 {
15849 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15850
15851 if (is_variant_part && discr_offset == child_die->sect_off)
15852 fi.fields.back ().variant.is_discriminant = true;
15853
15854 child_die = sibling_die (child_die);
15855 }
15856
15857 /* Attach template arguments to type. */
15858 if (!template_args.empty ())
15859 {
15860 has_template_parameters = true;
15861 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15862 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15863 TYPE_TEMPLATE_ARGUMENTS (type)
15864 = XOBNEWVEC (&objfile->objfile_obstack,
15865 struct symbol *,
15866 TYPE_N_TEMPLATE_ARGUMENTS (type));
15867 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15868 template_args.data (),
15869 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15870 * sizeof (struct symbol *)));
15871 }
15872
15873 /* Attach fields and member functions to the type. */
15874 if (fi.nfields)
15875 dwarf2_attach_fields_to_type (&fi, type, cu);
15876 if (!fi.fnfieldlists.empty ())
15877 {
15878 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15879
15880 /* Get the type which refers to the base class (possibly this
15881 class itself) which contains the vtable pointer for the current
15882 class from the DW_AT_containing_type attribute. This use of
15883 DW_AT_containing_type is a GNU extension. */
15884
15885 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15886 {
15887 struct type *t = die_containing_type (die, cu);
15888
15889 set_type_vptr_basetype (type, t);
15890 if (type == t)
15891 {
15892 int i;
15893
15894 /* Our own class provides vtbl ptr. */
15895 for (i = TYPE_NFIELDS (t) - 1;
15896 i >= TYPE_N_BASECLASSES (t);
15897 --i)
15898 {
15899 const char *fieldname = TYPE_FIELD_NAME (t, i);
15900
15901 if (is_vtable_name (fieldname, cu))
15902 {
15903 set_type_vptr_fieldno (type, i);
15904 break;
15905 }
15906 }
15907
15908 /* Complain if virtual function table field not found. */
15909 if (i < TYPE_N_BASECLASSES (t))
15910 complaint (_("virtual function table pointer "
15911 "not found when defining class '%s'"),
15912 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15913 }
15914 else
15915 {
15916 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15917 }
15918 }
15919 else if (cu->producer
15920 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15921 {
15922 /* The IBM XLC compiler does not provide direct indication
15923 of the containing type, but the vtable pointer is
15924 always named __vfp. */
15925
15926 int i;
15927
15928 for (i = TYPE_NFIELDS (type) - 1;
15929 i >= TYPE_N_BASECLASSES (type);
15930 --i)
15931 {
15932 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15933 {
15934 set_type_vptr_fieldno (type, i);
15935 set_type_vptr_basetype (type, type);
15936 break;
15937 }
15938 }
15939 }
15940 }
15941
15942 /* Copy fi.typedef_field_list linked list elements content into the
15943 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15944 if (!fi.typedef_field_list.empty ())
15945 {
15946 int count = fi.typedef_field_list.size ();
15947
15948 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15949 TYPE_TYPEDEF_FIELD_ARRAY (type)
15950 = ((struct decl_field *)
15951 TYPE_ALLOC (type,
15952 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15953 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15954
15955 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15956 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15957 }
15958
15959 /* Copy fi.nested_types_list linked list elements content into the
15960 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15961 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15962 {
15963 int count = fi.nested_types_list.size ();
15964
15965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15966 TYPE_NESTED_TYPES_ARRAY (type)
15967 = ((struct decl_field *)
15968 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15969 TYPE_NESTED_TYPES_COUNT (type) = count;
15970
15971 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15972 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15973 }
15974 }
15975
15976 quirk_gcc_member_function_pointer (type, objfile);
15977 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15978 cu->rust_unions.push_back (type);
15979
15980 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15981 snapshots) has been known to create a die giving a declaration
15982 for a class that has, as a child, a die giving a definition for a
15983 nested class. So we have to process our children even if the
15984 current die is a declaration. Normally, of course, a declaration
15985 won't have any children at all. */
15986
15987 child_die = die->child;
15988
15989 while (child_die != NULL && child_die->tag)
15990 {
15991 if (child_die->tag == DW_TAG_member
15992 || child_die->tag == DW_TAG_variable
15993 || child_die->tag == DW_TAG_inheritance
15994 || child_die->tag == DW_TAG_template_value_param
15995 || child_die->tag == DW_TAG_template_type_param)
15996 {
15997 /* Do nothing. */
15998 }
15999 else
16000 process_die (child_die, cu);
16001
16002 child_die = sibling_die (child_die);
16003 }
16004
16005 /* Do not consider external references. According to the DWARF standard,
16006 these DIEs are identified by the fact that they have no byte_size
16007 attribute, and a declaration attribute. */
16008 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16009 || !die_is_declaration (die, cu))
16010 {
16011 struct symbol *sym = new_symbol (die, type, cu);
16012
16013 if (has_template_parameters)
16014 {
16015 struct symtab *symtab;
16016 if (sym != nullptr)
16017 symtab = symbol_symtab (sym);
16018 else if (cu->line_header != nullptr)
16019 {
16020 /* Any related symtab will do. */
16021 symtab
16022 = cu->line_header->file_names ()[0].symtab;
16023 }
16024 else
16025 {
16026 symtab = nullptr;
16027 complaint (_("could not find suitable "
16028 "symtab for template parameter"
16029 " - DIE at %s [in module %s]"),
16030 sect_offset_str (die->sect_off),
16031 objfile_name (objfile));
16032 }
16033
16034 if (symtab != nullptr)
16035 {
16036 /* Make sure that the symtab is set on the new symbols.
16037 Even though they don't appear in this symtab directly,
16038 other parts of gdb assume that symbols do, and this is
16039 reasonably true. */
16040 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16041 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16042 }
16043 }
16044 }
16045 }
16046
16047 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16048 update TYPE using some information only available in DIE's children. */
16049
16050 static void
16051 update_enumeration_type_from_children (struct die_info *die,
16052 struct type *type,
16053 struct dwarf2_cu *cu)
16054 {
16055 struct die_info *child_die;
16056 int unsigned_enum = 1;
16057 int flag_enum = 1;
16058 ULONGEST mask = 0;
16059
16060 auto_obstack obstack;
16061
16062 for (child_die = die->child;
16063 child_die != NULL && child_die->tag;
16064 child_die = sibling_die (child_die))
16065 {
16066 struct attribute *attr;
16067 LONGEST value;
16068 const gdb_byte *bytes;
16069 struct dwarf2_locexpr_baton *baton;
16070 const char *name;
16071
16072 if (child_die->tag != DW_TAG_enumerator)
16073 continue;
16074
16075 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16076 if (attr == NULL)
16077 continue;
16078
16079 name = dwarf2_name (child_die, cu);
16080 if (name == NULL)
16081 name = "<anonymous enumerator>";
16082
16083 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16084 &value, &bytes, &baton);
16085 if (value < 0)
16086 {
16087 unsigned_enum = 0;
16088 flag_enum = 0;
16089 }
16090 else if ((mask & value) != 0)
16091 flag_enum = 0;
16092 else
16093 mask |= value;
16094
16095 /* If we already know that the enum type is neither unsigned, nor
16096 a flag type, no need to look at the rest of the enumerates. */
16097 if (!unsigned_enum && !flag_enum)
16098 break;
16099 }
16100
16101 if (unsigned_enum)
16102 TYPE_UNSIGNED (type) = 1;
16103 if (flag_enum)
16104 TYPE_FLAG_ENUM (type) = 1;
16105 }
16106
16107 /* Given a DW_AT_enumeration_type die, set its type. We do not
16108 complete the type's fields yet, or create any symbols. */
16109
16110 static struct type *
16111 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16112 {
16113 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16114 struct type *type;
16115 struct attribute *attr;
16116 const char *name;
16117
16118 /* If the definition of this type lives in .debug_types, read that type.
16119 Don't follow DW_AT_specification though, that will take us back up
16120 the chain and we want to go down. */
16121 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16122 if (attr != nullptr)
16123 {
16124 type = get_DW_AT_signature_type (die, attr, cu);
16125
16126 /* The type's CU may not be the same as CU.
16127 Ensure TYPE is recorded with CU in die_type_hash. */
16128 return set_die_type (die, type, cu);
16129 }
16130
16131 type = alloc_type (objfile);
16132
16133 TYPE_CODE (type) = TYPE_CODE_ENUM;
16134 name = dwarf2_full_name (NULL, die, cu);
16135 if (name != NULL)
16136 TYPE_NAME (type) = name;
16137
16138 attr = dwarf2_attr (die, DW_AT_type, cu);
16139 if (attr != NULL)
16140 {
16141 struct type *underlying_type = die_type (die, cu);
16142
16143 TYPE_TARGET_TYPE (type) = underlying_type;
16144 }
16145
16146 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16147 if (attr != nullptr)
16148 {
16149 TYPE_LENGTH (type) = DW_UNSND (attr);
16150 }
16151 else
16152 {
16153 TYPE_LENGTH (type) = 0;
16154 }
16155
16156 maybe_set_alignment (cu, die, type);
16157
16158 /* The enumeration DIE can be incomplete. In Ada, any type can be
16159 declared as private in the package spec, and then defined only
16160 inside the package body. Such types are known as Taft Amendment
16161 Types. When another package uses such a type, an incomplete DIE
16162 may be generated by the compiler. */
16163 if (die_is_declaration (die, cu))
16164 TYPE_STUB (type) = 1;
16165
16166 /* Finish the creation of this type by using the enum's children.
16167 We must call this even when the underlying type has been provided
16168 so that we can determine if we're looking at a "flag" enum. */
16169 update_enumeration_type_from_children (die, type, cu);
16170
16171 /* If this type has an underlying type that is not a stub, then we
16172 may use its attributes. We always use the "unsigned" attribute
16173 in this situation, because ordinarily we guess whether the type
16174 is unsigned -- but the guess can be wrong and the underlying type
16175 can tell us the reality. However, we defer to a local size
16176 attribute if one exists, because this lets the compiler override
16177 the underlying type if needed. */
16178 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16179 {
16180 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16181 if (TYPE_LENGTH (type) == 0)
16182 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16183 if (TYPE_RAW_ALIGN (type) == 0
16184 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16185 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16186 }
16187
16188 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16189
16190 return set_die_type (die, type, cu);
16191 }
16192
16193 /* Given a pointer to a die which begins an enumeration, process all
16194 the dies that define the members of the enumeration, and create the
16195 symbol for the enumeration type.
16196
16197 NOTE: We reverse the order of the element list. */
16198
16199 static void
16200 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16201 {
16202 struct type *this_type;
16203
16204 this_type = get_die_type (die, cu);
16205 if (this_type == NULL)
16206 this_type = read_enumeration_type (die, cu);
16207
16208 if (die->child != NULL)
16209 {
16210 struct die_info *child_die;
16211 struct symbol *sym;
16212 std::vector<struct field> fields;
16213 const char *name;
16214
16215 child_die = die->child;
16216 while (child_die && child_die->tag)
16217 {
16218 if (child_die->tag != DW_TAG_enumerator)
16219 {
16220 process_die (child_die, cu);
16221 }
16222 else
16223 {
16224 name = dwarf2_name (child_die, cu);
16225 if (name)
16226 {
16227 sym = new_symbol (child_die, this_type, cu);
16228
16229 fields.emplace_back ();
16230 struct field &field = fields.back ();
16231
16232 FIELD_NAME (field) = sym->linkage_name ();
16233 FIELD_TYPE (field) = NULL;
16234 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16235 FIELD_BITSIZE (field) = 0;
16236 }
16237 }
16238
16239 child_die = sibling_die (child_die);
16240 }
16241
16242 if (!fields.empty ())
16243 {
16244 TYPE_NFIELDS (this_type) = fields.size ();
16245 TYPE_FIELDS (this_type) = (struct field *)
16246 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16247 memcpy (TYPE_FIELDS (this_type), fields.data (),
16248 sizeof (struct field) * fields.size ());
16249 }
16250 }
16251
16252 /* If we are reading an enum from a .debug_types unit, and the enum
16253 is a declaration, and the enum is not the signatured type in the
16254 unit, then we do not want to add a symbol for it. Adding a
16255 symbol would in some cases obscure the true definition of the
16256 enum, giving users an incomplete type when the definition is
16257 actually available. Note that we do not want to do this for all
16258 enums which are just declarations, because C++0x allows forward
16259 enum declarations. */
16260 if (cu->per_cu->is_debug_types
16261 && die_is_declaration (die, cu))
16262 {
16263 struct signatured_type *sig_type;
16264
16265 sig_type = (struct signatured_type *) cu->per_cu;
16266 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16267 if (sig_type->type_offset_in_section != die->sect_off)
16268 return;
16269 }
16270
16271 new_symbol (die, this_type, cu);
16272 }
16273
16274 /* Extract all information from a DW_TAG_array_type DIE and put it in
16275 the DIE's type field. For now, this only handles one dimensional
16276 arrays. */
16277
16278 static struct type *
16279 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16280 {
16281 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16282 struct die_info *child_die;
16283 struct type *type;
16284 struct type *element_type, *range_type, *index_type;
16285 struct attribute *attr;
16286 const char *name;
16287 struct dynamic_prop *byte_stride_prop = NULL;
16288 unsigned int bit_stride = 0;
16289
16290 element_type = die_type (die, cu);
16291
16292 /* The die_type call above may have already set the type for this DIE. */
16293 type = get_die_type (die, cu);
16294 if (type)
16295 return type;
16296
16297 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16298 if (attr != NULL)
16299 {
16300 int stride_ok;
16301 struct type *prop_type
16302 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16303
16304 byte_stride_prop
16305 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16306 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16307 prop_type);
16308 if (!stride_ok)
16309 {
16310 complaint (_("unable to read array DW_AT_byte_stride "
16311 " - DIE at %s [in module %s]"),
16312 sect_offset_str (die->sect_off),
16313 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16314 /* Ignore this attribute. We will likely not be able to print
16315 arrays of this type correctly, but there is little we can do
16316 to help if we cannot read the attribute's value. */
16317 byte_stride_prop = NULL;
16318 }
16319 }
16320
16321 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16322 if (attr != NULL)
16323 bit_stride = DW_UNSND (attr);
16324
16325 /* Irix 6.2 native cc creates array types without children for
16326 arrays with unspecified length. */
16327 if (die->child == NULL)
16328 {
16329 index_type = objfile_type (objfile)->builtin_int;
16330 range_type = create_static_range_type (NULL, index_type, 0, -1);
16331 type = create_array_type_with_stride (NULL, element_type, range_type,
16332 byte_stride_prop, bit_stride);
16333 return set_die_type (die, type, cu);
16334 }
16335
16336 std::vector<struct type *> range_types;
16337 child_die = die->child;
16338 while (child_die && child_die->tag)
16339 {
16340 if (child_die->tag == DW_TAG_subrange_type)
16341 {
16342 struct type *child_type = read_type_die (child_die, cu);
16343
16344 if (child_type != NULL)
16345 {
16346 /* The range type was succesfully read. Save it for the
16347 array type creation. */
16348 range_types.push_back (child_type);
16349 }
16350 }
16351 child_die = sibling_die (child_die);
16352 }
16353
16354 /* Dwarf2 dimensions are output from left to right, create the
16355 necessary array types in backwards order. */
16356
16357 type = element_type;
16358
16359 if (read_array_order (die, cu) == DW_ORD_col_major)
16360 {
16361 int i = 0;
16362
16363 while (i < range_types.size ())
16364 type = create_array_type_with_stride (NULL, type, range_types[i++],
16365 byte_stride_prop, bit_stride);
16366 }
16367 else
16368 {
16369 size_t ndim = range_types.size ();
16370 while (ndim-- > 0)
16371 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16372 byte_stride_prop, bit_stride);
16373 }
16374
16375 /* Understand Dwarf2 support for vector types (like they occur on
16376 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16377 array type. This is not part of the Dwarf2/3 standard yet, but a
16378 custom vendor extension. The main difference between a regular
16379 array and the vector variant is that vectors are passed by value
16380 to functions. */
16381 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16382 if (attr != nullptr)
16383 make_vector_type (type);
16384
16385 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16386 implementation may choose to implement triple vectors using this
16387 attribute. */
16388 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16389 if (attr != nullptr)
16390 {
16391 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16392 TYPE_LENGTH (type) = DW_UNSND (attr);
16393 else
16394 complaint (_("DW_AT_byte_size for array type smaller "
16395 "than the total size of elements"));
16396 }
16397
16398 name = dwarf2_name (die, cu);
16399 if (name)
16400 TYPE_NAME (type) = name;
16401
16402 maybe_set_alignment (cu, die, type);
16403
16404 /* Install the type in the die. */
16405 set_die_type (die, type, cu);
16406
16407 /* set_die_type should be already done. */
16408 set_descriptive_type (type, die, cu);
16409
16410 return type;
16411 }
16412
16413 static enum dwarf_array_dim_ordering
16414 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct attribute *attr;
16417
16418 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16419
16420 if (attr != nullptr)
16421 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16422
16423 /* GNU F77 is a special case, as at 08/2004 array type info is the
16424 opposite order to the dwarf2 specification, but data is still
16425 laid out as per normal fortran.
16426
16427 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16428 version checking. */
16429
16430 if (cu->language == language_fortran
16431 && cu->producer && strstr (cu->producer, "GNU F77"))
16432 {
16433 return DW_ORD_row_major;
16434 }
16435
16436 switch (cu->language_defn->la_array_ordering)
16437 {
16438 case array_column_major:
16439 return DW_ORD_col_major;
16440 case array_row_major:
16441 default:
16442 return DW_ORD_row_major;
16443 };
16444 }
16445
16446 /* Extract all information from a DW_TAG_set_type DIE and put it in
16447 the DIE's type field. */
16448
16449 static struct type *
16450 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16451 {
16452 struct type *domain_type, *set_type;
16453 struct attribute *attr;
16454
16455 domain_type = die_type (die, cu);
16456
16457 /* The die_type call above may have already set the type for this DIE. */
16458 set_type = get_die_type (die, cu);
16459 if (set_type)
16460 return set_type;
16461
16462 set_type = create_set_type (NULL, domain_type);
16463
16464 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16465 if (attr != nullptr)
16466 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16467
16468 maybe_set_alignment (cu, die, set_type);
16469
16470 return set_die_type (die, set_type, cu);
16471 }
16472
16473 /* A helper for read_common_block that creates a locexpr baton.
16474 SYM is the symbol which we are marking as computed.
16475 COMMON_DIE is the DIE for the common block.
16476 COMMON_LOC is the location expression attribute for the common
16477 block itself.
16478 MEMBER_LOC is the location expression attribute for the particular
16479 member of the common block that we are processing.
16480 CU is the CU from which the above come. */
16481
16482 static void
16483 mark_common_block_symbol_computed (struct symbol *sym,
16484 struct die_info *common_die,
16485 struct attribute *common_loc,
16486 struct attribute *member_loc,
16487 struct dwarf2_cu *cu)
16488 {
16489 struct dwarf2_per_objfile *dwarf2_per_objfile
16490 = cu->per_cu->dwarf2_per_objfile;
16491 struct objfile *objfile = dwarf2_per_objfile->objfile;
16492 struct dwarf2_locexpr_baton *baton;
16493 gdb_byte *ptr;
16494 unsigned int cu_off;
16495 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16496 LONGEST offset = 0;
16497
16498 gdb_assert (common_loc && member_loc);
16499 gdb_assert (attr_form_is_block (common_loc));
16500 gdb_assert (attr_form_is_block (member_loc)
16501 || attr_form_is_constant (member_loc));
16502
16503 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16504 baton->per_cu = cu->per_cu;
16505 gdb_assert (baton->per_cu);
16506
16507 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16508
16509 if (attr_form_is_constant (member_loc))
16510 {
16511 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16512 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16513 }
16514 else
16515 baton->size += DW_BLOCK (member_loc)->size;
16516
16517 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16518 baton->data = ptr;
16519
16520 *ptr++ = DW_OP_call4;
16521 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16522 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16523 ptr += 4;
16524
16525 if (attr_form_is_constant (member_loc))
16526 {
16527 *ptr++ = DW_OP_addr;
16528 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16529 ptr += cu->header.addr_size;
16530 }
16531 else
16532 {
16533 /* We have to copy the data here, because DW_OP_call4 will only
16534 use a DW_AT_location attribute. */
16535 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16536 ptr += DW_BLOCK (member_loc)->size;
16537 }
16538
16539 *ptr++ = DW_OP_plus;
16540 gdb_assert (ptr - baton->data == baton->size);
16541
16542 SYMBOL_LOCATION_BATON (sym) = baton;
16543 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16544 }
16545
16546 /* Create appropriate locally-scoped variables for all the
16547 DW_TAG_common_block entries. Also create a struct common_block
16548 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16549 is used to separate the common blocks name namespace from regular
16550 variable names. */
16551
16552 static void
16553 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16554 {
16555 struct attribute *attr;
16556
16557 attr = dwarf2_attr (die, DW_AT_location, cu);
16558 if (attr != nullptr)
16559 {
16560 /* Support the .debug_loc offsets. */
16561 if (attr_form_is_block (attr))
16562 {
16563 /* Ok. */
16564 }
16565 else if (attr_form_is_section_offset (attr))
16566 {
16567 dwarf2_complex_location_expr_complaint ();
16568 attr = NULL;
16569 }
16570 else
16571 {
16572 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16573 "common block member");
16574 attr = NULL;
16575 }
16576 }
16577
16578 if (die->child != NULL)
16579 {
16580 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16581 struct die_info *child_die;
16582 size_t n_entries = 0, size;
16583 struct common_block *common_block;
16584 struct symbol *sym;
16585
16586 for (child_die = die->child;
16587 child_die && child_die->tag;
16588 child_die = sibling_die (child_die))
16589 ++n_entries;
16590
16591 size = (sizeof (struct common_block)
16592 + (n_entries - 1) * sizeof (struct symbol *));
16593 common_block
16594 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16595 size);
16596 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16597 common_block->n_entries = 0;
16598
16599 for (child_die = die->child;
16600 child_die && child_die->tag;
16601 child_die = sibling_die (child_die))
16602 {
16603 /* Create the symbol in the DW_TAG_common_block block in the current
16604 symbol scope. */
16605 sym = new_symbol (child_die, NULL, cu);
16606 if (sym != NULL)
16607 {
16608 struct attribute *member_loc;
16609
16610 common_block->contents[common_block->n_entries++] = sym;
16611
16612 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16613 cu);
16614 if (member_loc)
16615 {
16616 /* GDB has handled this for a long time, but it is
16617 not specified by DWARF. It seems to have been
16618 emitted by gfortran at least as recently as:
16619 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16620 complaint (_("Variable in common block has "
16621 "DW_AT_data_member_location "
16622 "- DIE at %s [in module %s]"),
16623 sect_offset_str (child_die->sect_off),
16624 objfile_name (objfile));
16625
16626 if (attr_form_is_section_offset (member_loc))
16627 dwarf2_complex_location_expr_complaint ();
16628 else if (attr_form_is_constant (member_loc)
16629 || attr_form_is_block (member_loc))
16630 {
16631 if (attr != nullptr)
16632 mark_common_block_symbol_computed (sym, die, attr,
16633 member_loc, cu);
16634 }
16635 else
16636 dwarf2_complex_location_expr_complaint ();
16637 }
16638 }
16639 }
16640
16641 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16642 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16643 }
16644 }
16645
16646 /* Create a type for a C++ namespace. */
16647
16648 static struct type *
16649 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16650 {
16651 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16652 const char *previous_prefix, *name;
16653 int is_anonymous;
16654 struct type *type;
16655
16656 /* For extensions, reuse the type of the original namespace. */
16657 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16658 {
16659 struct die_info *ext_die;
16660 struct dwarf2_cu *ext_cu = cu;
16661
16662 ext_die = dwarf2_extension (die, &ext_cu);
16663 type = read_type_die (ext_die, ext_cu);
16664
16665 /* EXT_CU may not be the same as CU.
16666 Ensure TYPE is recorded with CU in die_type_hash. */
16667 return set_die_type (die, type, cu);
16668 }
16669
16670 name = namespace_name (die, &is_anonymous, cu);
16671
16672 /* Now build the name of the current namespace. */
16673
16674 previous_prefix = determine_prefix (die, cu);
16675 if (previous_prefix[0] != '\0')
16676 name = typename_concat (&objfile->objfile_obstack,
16677 previous_prefix, name, 0, cu);
16678
16679 /* Create the type. */
16680 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16681
16682 return set_die_type (die, type, cu);
16683 }
16684
16685 /* Read a namespace scope. */
16686
16687 static void
16688 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16689 {
16690 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16691 int is_anonymous;
16692
16693 /* Add a symbol associated to this if we haven't seen the namespace
16694 before. Also, add a using directive if it's an anonymous
16695 namespace. */
16696
16697 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16698 {
16699 struct type *type;
16700
16701 type = read_type_die (die, cu);
16702 new_symbol (die, type, cu);
16703
16704 namespace_name (die, &is_anonymous, cu);
16705 if (is_anonymous)
16706 {
16707 const char *previous_prefix = determine_prefix (die, cu);
16708
16709 std::vector<const char *> excludes;
16710 add_using_directive (using_directives (cu),
16711 previous_prefix, TYPE_NAME (type), NULL,
16712 NULL, excludes, 0, &objfile->objfile_obstack);
16713 }
16714 }
16715
16716 if (die->child != NULL)
16717 {
16718 struct die_info *child_die = die->child;
16719
16720 while (child_die && child_die->tag)
16721 {
16722 process_die (child_die, cu);
16723 child_die = sibling_die (child_die);
16724 }
16725 }
16726 }
16727
16728 /* Read a Fortran module as type. This DIE can be only a declaration used for
16729 imported module. Still we need that type as local Fortran "use ... only"
16730 declaration imports depend on the created type in determine_prefix. */
16731
16732 static struct type *
16733 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16734 {
16735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16736 const char *module_name;
16737 struct type *type;
16738
16739 module_name = dwarf2_name (die, cu);
16740 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16741
16742 return set_die_type (die, type, cu);
16743 }
16744
16745 /* Read a Fortran module. */
16746
16747 static void
16748 read_module (struct die_info *die, struct dwarf2_cu *cu)
16749 {
16750 struct die_info *child_die = die->child;
16751 struct type *type;
16752
16753 type = read_type_die (die, cu);
16754 new_symbol (die, type, cu);
16755
16756 while (child_die && child_die->tag)
16757 {
16758 process_die (child_die, cu);
16759 child_die = sibling_die (child_die);
16760 }
16761 }
16762
16763 /* Return the name of the namespace represented by DIE. Set
16764 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16765 namespace. */
16766
16767 static const char *
16768 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16769 {
16770 struct die_info *current_die;
16771 const char *name = NULL;
16772
16773 /* Loop through the extensions until we find a name. */
16774
16775 for (current_die = die;
16776 current_die != NULL;
16777 current_die = dwarf2_extension (die, &cu))
16778 {
16779 /* We don't use dwarf2_name here so that we can detect the absence
16780 of a name -> anonymous namespace. */
16781 name = dwarf2_string_attr (die, DW_AT_name, cu);
16782
16783 if (name != NULL)
16784 break;
16785 }
16786
16787 /* Is it an anonymous namespace? */
16788
16789 *is_anonymous = (name == NULL);
16790 if (*is_anonymous)
16791 name = CP_ANONYMOUS_NAMESPACE_STR;
16792
16793 return name;
16794 }
16795
16796 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16797 the user defined type vector. */
16798
16799 static struct type *
16800 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16801 {
16802 struct gdbarch *gdbarch
16803 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16804 struct comp_unit_head *cu_header = &cu->header;
16805 struct type *type;
16806 struct attribute *attr_byte_size;
16807 struct attribute *attr_address_class;
16808 int byte_size, addr_class;
16809 struct type *target_type;
16810
16811 target_type = die_type (die, cu);
16812
16813 /* The die_type call above may have already set the type for this DIE. */
16814 type = get_die_type (die, cu);
16815 if (type)
16816 return type;
16817
16818 type = lookup_pointer_type (target_type);
16819
16820 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16821 if (attr_byte_size)
16822 byte_size = DW_UNSND (attr_byte_size);
16823 else
16824 byte_size = cu_header->addr_size;
16825
16826 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16827 if (attr_address_class)
16828 addr_class = DW_UNSND (attr_address_class);
16829 else
16830 addr_class = DW_ADDR_none;
16831
16832 ULONGEST alignment = get_alignment (cu, die);
16833
16834 /* If the pointer size, alignment, or address class is different
16835 than the default, create a type variant marked as such and set
16836 the length accordingly. */
16837 if (TYPE_LENGTH (type) != byte_size
16838 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16839 && alignment != TYPE_RAW_ALIGN (type))
16840 || addr_class != DW_ADDR_none)
16841 {
16842 if (gdbarch_address_class_type_flags_p (gdbarch))
16843 {
16844 int type_flags;
16845
16846 type_flags = gdbarch_address_class_type_flags
16847 (gdbarch, byte_size, addr_class);
16848 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16849 == 0);
16850 type = make_type_with_address_space (type, type_flags);
16851 }
16852 else if (TYPE_LENGTH (type) != byte_size)
16853 {
16854 complaint (_("invalid pointer size %d"), byte_size);
16855 }
16856 else if (TYPE_RAW_ALIGN (type) != alignment)
16857 {
16858 complaint (_("Invalid DW_AT_alignment"
16859 " - DIE at %s [in module %s]"),
16860 sect_offset_str (die->sect_off),
16861 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16862 }
16863 else
16864 {
16865 /* Should we also complain about unhandled address classes? */
16866 }
16867 }
16868
16869 TYPE_LENGTH (type) = byte_size;
16870 set_type_align (type, alignment);
16871 return set_die_type (die, type, cu);
16872 }
16873
16874 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16875 the user defined type vector. */
16876
16877 static struct type *
16878 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16879 {
16880 struct type *type;
16881 struct type *to_type;
16882 struct type *domain;
16883
16884 to_type = die_type (die, cu);
16885 domain = die_containing_type (die, cu);
16886
16887 /* The calls above may have already set the type for this DIE. */
16888 type = get_die_type (die, cu);
16889 if (type)
16890 return type;
16891
16892 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16893 type = lookup_methodptr_type (to_type);
16894 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16895 {
16896 struct type *new_type
16897 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16898
16899 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16900 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16901 TYPE_VARARGS (to_type));
16902 type = lookup_methodptr_type (new_type);
16903 }
16904 else
16905 type = lookup_memberptr_type (to_type, domain);
16906
16907 return set_die_type (die, type, cu);
16908 }
16909
16910 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16911 the user defined type vector. */
16912
16913 static struct type *
16914 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16915 enum type_code refcode)
16916 {
16917 struct comp_unit_head *cu_header = &cu->header;
16918 struct type *type, *target_type;
16919 struct attribute *attr;
16920
16921 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16922
16923 target_type = die_type (die, cu);
16924
16925 /* The die_type call above may have already set the type for this DIE. */
16926 type = get_die_type (die, cu);
16927 if (type)
16928 return type;
16929
16930 type = lookup_reference_type (target_type, refcode);
16931 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16932 if (attr != nullptr)
16933 {
16934 TYPE_LENGTH (type) = DW_UNSND (attr);
16935 }
16936 else
16937 {
16938 TYPE_LENGTH (type) = cu_header->addr_size;
16939 }
16940 maybe_set_alignment (cu, die, type);
16941 return set_die_type (die, type, cu);
16942 }
16943
16944 /* Add the given cv-qualifiers to the element type of the array. GCC
16945 outputs DWARF type qualifiers that apply to an array, not the
16946 element type. But GDB relies on the array element type to carry
16947 the cv-qualifiers. This mimics section 6.7.3 of the C99
16948 specification. */
16949
16950 static struct type *
16951 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16952 struct type *base_type, int cnst, int voltl)
16953 {
16954 struct type *el_type, *inner_array;
16955
16956 base_type = copy_type (base_type);
16957 inner_array = base_type;
16958
16959 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16960 {
16961 TYPE_TARGET_TYPE (inner_array) =
16962 copy_type (TYPE_TARGET_TYPE (inner_array));
16963 inner_array = TYPE_TARGET_TYPE (inner_array);
16964 }
16965
16966 el_type = TYPE_TARGET_TYPE (inner_array);
16967 cnst |= TYPE_CONST (el_type);
16968 voltl |= TYPE_VOLATILE (el_type);
16969 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16970
16971 return set_die_type (die, base_type, cu);
16972 }
16973
16974 static struct type *
16975 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16976 {
16977 struct type *base_type, *cv_type;
16978
16979 base_type = die_type (die, cu);
16980
16981 /* The die_type call above may have already set the type for this DIE. */
16982 cv_type = get_die_type (die, cu);
16983 if (cv_type)
16984 return cv_type;
16985
16986 /* In case the const qualifier is applied to an array type, the element type
16987 is so qualified, not the array type (section 6.7.3 of C99). */
16988 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16989 return add_array_cv_type (die, cu, base_type, 1, 0);
16990
16991 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16992 return set_die_type (die, cv_type, cu);
16993 }
16994
16995 static struct type *
16996 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16997 {
16998 struct type *base_type, *cv_type;
16999
17000 base_type = die_type (die, cu);
17001
17002 /* The die_type call above may have already set the type for this DIE. */
17003 cv_type = get_die_type (die, cu);
17004 if (cv_type)
17005 return cv_type;
17006
17007 /* In case the volatile qualifier is applied to an array type, the
17008 element type is so qualified, not the array type (section 6.7.3
17009 of C99). */
17010 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17011 return add_array_cv_type (die, cu, base_type, 0, 1);
17012
17013 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17014 return set_die_type (die, cv_type, cu);
17015 }
17016
17017 /* Handle DW_TAG_restrict_type. */
17018
17019 static struct type *
17020 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17021 {
17022 struct type *base_type, *cv_type;
17023
17024 base_type = die_type (die, cu);
17025
17026 /* The die_type call above may have already set the type for this DIE. */
17027 cv_type = get_die_type (die, cu);
17028 if (cv_type)
17029 return cv_type;
17030
17031 cv_type = make_restrict_type (base_type);
17032 return set_die_type (die, cv_type, cu);
17033 }
17034
17035 /* Handle DW_TAG_atomic_type. */
17036
17037 static struct type *
17038 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17039 {
17040 struct type *base_type, *cv_type;
17041
17042 base_type = die_type (die, cu);
17043
17044 /* The die_type call above may have already set the type for this DIE. */
17045 cv_type = get_die_type (die, cu);
17046 if (cv_type)
17047 return cv_type;
17048
17049 cv_type = make_atomic_type (base_type);
17050 return set_die_type (die, cv_type, cu);
17051 }
17052
17053 /* Extract all information from a DW_TAG_string_type DIE and add to
17054 the user defined type vector. It isn't really a user defined type,
17055 but it behaves like one, with other DIE's using an AT_user_def_type
17056 attribute to reference it. */
17057
17058 static struct type *
17059 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17060 {
17061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17062 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17063 struct type *type, *range_type, *index_type, *char_type;
17064 struct attribute *attr;
17065 struct dynamic_prop prop;
17066 bool length_is_constant = true;
17067 LONGEST length;
17068
17069 /* There are a couple of places where bit sizes might be made use of
17070 when parsing a DW_TAG_string_type, however, no producer that we know
17071 of make use of these. Handling bit sizes that are a multiple of the
17072 byte size is easy enough, but what about other bit sizes? Lets deal
17073 with that problem when we have to. Warn about these attributes being
17074 unsupported, then parse the type and ignore them like we always
17075 have. */
17076 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17077 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17078 {
17079 static bool warning_printed = false;
17080 if (!warning_printed)
17081 {
17082 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17083 "currently supported on DW_TAG_string_type."));
17084 warning_printed = true;
17085 }
17086 }
17087
17088 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17089 if (attr != nullptr && !attr_form_is_constant (attr))
17090 {
17091 /* The string length describes the location at which the length of
17092 the string can be found. The size of the length field can be
17093 specified with one of the attributes below. */
17094 struct type *prop_type;
17095 struct attribute *len
17096 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17097 if (len == nullptr)
17098 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17099 if (len != nullptr && attr_form_is_constant (len))
17100 {
17101 /* Pass 0 as the default as we know this attribute is constant
17102 and the default value will not be returned. */
17103 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17104 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17105 }
17106 else
17107 {
17108 /* If the size is not specified then we assume it is the size of
17109 an address on this target. */
17110 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17111 }
17112
17113 /* Convert the attribute into a dynamic property. */
17114 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17115 length = 1;
17116 else
17117 length_is_constant = false;
17118 }
17119 else if (attr != nullptr)
17120 {
17121 /* This DW_AT_string_length just contains the length with no
17122 indirection. There's no need to create a dynamic property in this
17123 case. Pass 0 for the default value as we know it will not be
17124 returned in this case. */
17125 length = dwarf2_get_attr_constant_value (attr, 0);
17126 }
17127 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17128 {
17129 /* We don't currently support non-constant byte sizes for strings. */
17130 length = dwarf2_get_attr_constant_value (attr, 1);
17131 }
17132 else
17133 {
17134 /* Use 1 as a fallback length if we have nothing else. */
17135 length = 1;
17136 }
17137
17138 index_type = objfile_type (objfile)->builtin_int;
17139 if (length_is_constant)
17140 range_type = create_static_range_type (NULL, index_type, 1, length);
17141 else
17142 {
17143 struct dynamic_prop low_bound;
17144
17145 low_bound.kind = PROP_CONST;
17146 low_bound.data.const_val = 1;
17147 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17148 }
17149 char_type = language_string_char_type (cu->language_defn, gdbarch);
17150 type = create_string_type (NULL, char_type, range_type);
17151
17152 return set_die_type (die, type, cu);
17153 }
17154
17155 /* Assuming that DIE corresponds to a function, returns nonzero
17156 if the function is prototyped. */
17157
17158 static int
17159 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17160 {
17161 struct attribute *attr;
17162
17163 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17164 if (attr && (DW_UNSND (attr) != 0))
17165 return 1;
17166
17167 /* The DWARF standard implies that the DW_AT_prototyped attribute
17168 is only meaningful for C, but the concept also extends to other
17169 languages that allow unprototyped functions (Eg: Objective C).
17170 For all other languages, assume that functions are always
17171 prototyped. */
17172 if (cu->language != language_c
17173 && cu->language != language_objc
17174 && cu->language != language_opencl)
17175 return 1;
17176
17177 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17178 prototyped and unprototyped functions; default to prototyped,
17179 since that is more common in modern code (and RealView warns
17180 about unprototyped functions). */
17181 if (producer_is_realview (cu->producer))
17182 return 1;
17183
17184 return 0;
17185 }
17186
17187 /* Handle DIES due to C code like:
17188
17189 struct foo
17190 {
17191 int (*funcp)(int a, long l);
17192 int b;
17193 };
17194
17195 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17196
17197 static struct type *
17198 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17199 {
17200 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17201 struct type *type; /* Type that this function returns. */
17202 struct type *ftype; /* Function that returns above type. */
17203 struct attribute *attr;
17204
17205 type = die_type (die, cu);
17206
17207 /* The die_type call above may have already set the type for this DIE. */
17208 ftype = get_die_type (die, cu);
17209 if (ftype)
17210 return ftype;
17211
17212 ftype = lookup_function_type (type);
17213
17214 if (prototyped_function_p (die, cu))
17215 TYPE_PROTOTYPED (ftype) = 1;
17216
17217 /* Store the calling convention in the type if it's available in
17218 the subroutine die. Otherwise set the calling convention to
17219 the default value DW_CC_normal. */
17220 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17221 if (attr != nullptr
17222 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17223 TYPE_CALLING_CONVENTION (ftype)
17224 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17225 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17226 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17227 else
17228 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17229
17230 /* Record whether the function returns normally to its caller or not
17231 if the DWARF producer set that information. */
17232 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17233 if (attr && (DW_UNSND (attr) != 0))
17234 TYPE_NO_RETURN (ftype) = 1;
17235
17236 /* We need to add the subroutine type to the die immediately so
17237 we don't infinitely recurse when dealing with parameters
17238 declared as the same subroutine type. */
17239 set_die_type (die, ftype, cu);
17240
17241 if (die->child != NULL)
17242 {
17243 struct type *void_type = objfile_type (objfile)->builtin_void;
17244 struct die_info *child_die;
17245 int nparams, iparams;
17246
17247 /* Count the number of parameters.
17248 FIXME: GDB currently ignores vararg functions, but knows about
17249 vararg member functions. */
17250 nparams = 0;
17251 child_die = die->child;
17252 while (child_die && child_die->tag)
17253 {
17254 if (child_die->tag == DW_TAG_formal_parameter)
17255 nparams++;
17256 else if (child_die->tag == DW_TAG_unspecified_parameters)
17257 TYPE_VARARGS (ftype) = 1;
17258 child_die = sibling_die (child_die);
17259 }
17260
17261 /* Allocate storage for parameters and fill them in. */
17262 TYPE_NFIELDS (ftype) = nparams;
17263 TYPE_FIELDS (ftype) = (struct field *)
17264 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17265
17266 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17267 even if we error out during the parameters reading below. */
17268 for (iparams = 0; iparams < nparams; iparams++)
17269 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17270
17271 iparams = 0;
17272 child_die = die->child;
17273 while (child_die && child_die->tag)
17274 {
17275 if (child_die->tag == DW_TAG_formal_parameter)
17276 {
17277 struct type *arg_type;
17278
17279 /* DWARF version 2 has no clean way to discern C++
17280 static and non-static member functions. G++ helps
17281 GDB by marking the first parameter for non-static
17282 member functions (which is the this pointer) as
17283 artificial. We pass this information to
17284 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17285
17286 DWARF version 3 added DW_AT_object_pointer, which GCC
17287 4.5 does not yet generate. */
17288 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17289 if (attr != nullptr)
17290 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17291 else
17292 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17293 arg_type = die_type (child_die, cu);
17294
17295 /* RealView does not mark THIS as const, which the testsuite
17296 expects. GCC marks THIS as const in method definitions,
17297 but not in the class specifications (GCC PR 43053). */
17298 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17299 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17300 {
17301 int is_this = 0;
17302 struct dwarf2_cu *arg_cu = cu;
17303 const char *name = dwarf2_name (child_die, cu);
17304
17305 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17306 if (attr != nullptr)
17307 {
17308 /* If the compiler emits this, use it. */
17309 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17310 is_this = 1;
17311 }
17312 else if (name && strcmp (name, "this") == 0)
17313 /* Function definitions will have the argument names. */
17314 is_this = 1;
17315 else if (name == NULL && iparams == 0)
17316 /* Declarations may not have the names, so like
17317 elsewhere in GDB, assume an artificial first
17318 argument is "this". */
17319 is_this = 1;
17320
17321 if (is_this)
17322 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17323 arg_type, 0);
17324 }
17325
17326 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17327 iparams++;
17328 }
17329 child_die = sibling_die (child_die);
17330 }
17331 }
17332
17333 return ftype;
17334 }
17335
17336 static struct type *
17337 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17338 {
17339 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17340 const char *name = NULL;
17341 struct type *this_type, *target_type;
17342
17343 name = dwarf2_full_name (NULL, die, cu);
17344 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17345 TYPE_TARGET_STUB (this_type) = 1;
17346 set_die_type (die, this_type, cu);
17347 target_type = die_type (die, cu);
17348 if (target_type != this_type)
17349 TYPE_TARGET_TYPE (this_type) = target_type;
17350 else
17351 {
17352 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17353 spec and cause infinite loops in GDB. */
17354 complaint (_("Self-referential DW_TAG_typedef "
17355 "- DIE at %s [in module %s]"),
17356 sect_offset_str (die->sect_off), objfile_name (objfile));
17357 TYPE_TARGET_TYPE (this_type) = NULL;
17358 }
17359 return this_type;
17360 }
17361
17362 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17363 (which may be different from NAME) to the architecture back-end to allow
17364 it to guess the correct format if necessary. */
17365
17366 static struct type *
17367 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17368 const char *name_hint, enum bfd_endian byte_order)
17369 {
17370 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17371 const struct floatformat **format;
17372 struct type *type;
17373
17374 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17375 if (format)
17376 type = init_float_type (objfile, bits, name, format, byte_order);
17377 else
17378 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17379
17380 return type;
17381 }
17382
17383 /* Allocate an integer type of size BITS and name NAME. */
17384
17385 static struct type *
17386 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17387 int bits, int unsigned_p, const char *name)
17388 {
17389 struct type *type;
17390
17391 /* Versions of Intel's C Compiler generate an integer type called "void"
17392 instead of using DW_TAG_unspecified_type. This has been seen on
17393 at least versions 14, 17, and 18. */
17394 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17395 && strcmp (name, "void") == 0)
17396 type = objfile_type (objfile)->builtin_void;
17397 else
17398 type = init_integer_type (objfile, bits, unsigned_p, name);
17399
17400 return type;
17401 }
17402
17403 /* Initialise and return a floating point type of size BITS suitable for
17404 use as a component of a complex number. The NAME_HINT is passed through
17405 when initialising the floating point type and is the name of the complex
17406 type.
17407
17408 As DWARF doesn't currently provide an explicit name for the components
17409 of a complex number, but it can be helpful to have these components
17410 named, we try to select a suitable name based on the size of the
17411 component. */
17412 static struct type *
17413 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17414 struct objfile *objfile,
17415 int bits, const char *name_hint,
17416 enum bfd_endian byte_order)
17417 {
17418 gdbarch *gdbarch = get_objfile_arch (objfile);
17419 struct type *tt = nullptr;
17420
17421 /* Try to find a suitable floating point builtin type of size BITS.
17422 We're going to use the name of this type as the name for the complex
17423 target type that we are about to create. */
17424 switch (cu->language)
17425 {
17426 case language_fortran:
17427 switch (bits)
17428 {
17429 case 32:
17430 tt = builtin_f_type (gdbarch)->builtin_real;
17431 break;
17432 case 64:
17433 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17434 break;
17435 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17436 case 128:
17437 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17438 break;
17439 }
17440 break;
17441 default:
17442 switch (bits)
17443 {
17444 case 32:
17445 tt = builtin_type (gdbarch)->builtin_float;
17446 break;
17447 case 64:
17448 tt = builtin_type (gdbarch)->builtin_double;
17449 break;
17450 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17451 case 128:
17452 tt = builtin_type (gdbarch)->builtin_long_double;
17453 break;
17454 }
17455 break;
17456 }
17457
17458 /* If the type we found doesn't match the size we were looking for, then
17459 pretend we didn't find a type at all, the complex target type we
17460 create will then be nameless. */
17461 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17462 tt = nullptr;
17463
17464 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17465 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17466 }
17467
17468 /* Find a representation of a given base type and install
17469 it in the TYPE field of the die. */
17470
17471 static struct type *
17472 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17473 {
17474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17475 struct type *type;
17476 struct attribute *attr;
17477 int encoding = 0, bits = 0;
17478 const char *name;
17479 gdbarch *arch;
17480
17481 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17482 if (attr != nullptr)
17483 encoding = DW_UNSND (attr);
17484 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17485 if (attr != nullptr)
17486 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17487 name = dwarf2_name (die, cu);
17488 if (!name)
17489 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17490
17491 arch = get_objfile_arch (objfile);
17492 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17493
17494 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17495 if (attr)
17496 {
17497 int endianity = DW_UNSND (attr);
17498
17499 switch (endianity)
17500 {
17501 case DW_END_big:
17502 byte_order = BFD_ENDIAN_BIG;
17503 break;
17504 case DW_END_little:
17505 byte_order = BFD_ENDIAN_LITTLE;
17506 break;
17507 default:
17508 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17509 break;
17510 }
17511 }
17512
17513 switch (encoding)
17514 {
17515 case DW_ATE_address:
17516 /* Turn DW_ATE_address into a void * pointer. */
17517 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17518 type = init_pointer_type (objfile, bits, name, type);
17519 break;
17520 case DW_ATE_boolean:
17521 type = init_boolean_type (objfile, bits, 1, name);
17522 break;
17523 case DW_ATE_complex_float:
17524 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17525 byte_order);
17526 type = init_complex_type (objfile, name, type);
17527 break;
17528 case DW_ATE_decimal_float:
17529 type = init_decfloat_type (objfile, bits, name);
17530 break;
17531 case DW_ATE_float:
17532 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17533 break;
17534 case DW_ATE_signed:
17535 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17536 break;
17537 case DW_ATE_unsigned:
17538 if (cu->language == language_fortran
17539 && name
17540 && startswith (name, "character("))
17541 type = init_character_type (objfile, bits, 1, name);
17542 else
17543 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17544 break;
17545 case DW_ATE_signed_char:
17546 if (cu->language == language_ada || cu->language == language_m2
17547 || cu->language == language_pascal
17548 || cu->language == language_fortran)
17549 type = init_character_type (objfile, bits, 0, name);
17550 else
17551 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17552 break;
17553 case DW_ATE_unsigned_char:
17554 if (cu->language == language_ada || cu->language == language_m2
17555 || cu->language == language_pascal
17556 || cu->language == language_fortran
17557 || cu->language == language_rust)
17558 type = init_character_type (objfile, bits, 1, name);
17559 else
17560 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17561 break;
17562 case DW_ATE_UTF:
17563 {
17564 if (bits == 16)
17565 type = builtin_type (arch)->builtin_char16;
17566 else if (bits == 32)
17567 type = builtin_type (arch)->builtin_char32;
17568 else
17569 {
17570 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17571 bits);
17572 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17573 }
17574 return set_die_type (die, type, cu);
17575 }
17576 break;
17577
17578 default:
17579 complaint (_("unsupported DW_AT_encoding: '%s'"),
17580 dwarf_type_encoding_name (encoding));
17581 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17582 break;
17583 }
17584
17585 if (name && strcmp (name, "char") == 0)
17586 TYPE_NOSIGN (type) = 1;
17587
17588 maybe_set_alignment (cu, die, type);
17589
17590 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17591
17592 return set_die_type (die, type, cu);
17593 }
17594
17595 /* Parse dwarf attribute if it's a block, reference or constant and put the
17596 resulting value of the attribute into struct bound_prop.
17597 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17598
17599 static int
17600 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17601 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17602 struct type *default_type)
17603 {
17604 struct dwarf2_property_baton *baton;
17605 struct obstack *obstack
17606 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17607
17608 gdb_assert (default_type != NULL);
17609
17610 if (attr == NULL || prop == NULL)
17611 return 0;
17612
17613 if (attr_form_is_block (attr))
17614 {
17615 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17616 baton->property_type = default_type;
17617 baton->locexpr.per_cu = cu->per_cu;
17618 baton->locexpr.size = DW_BLOCK (attr)->size;
17619 baton->locexpr.data = DW_BLOCK (attr)->data;
17620 switch (attr->name)
17621 {
17622 case DW_AT_string_length:
17623 baton->locexpr.is_reference = true;
17624 break;
17625 default:
17626 baton->locexpr.is_reference = false;
17627 break;
17628 }
17629 prop->data.baton = baton;
17630 prop->kind = PROP_LOCEXPR;
17631 gdb_assert (prop->data.baton != NULL);
17632 }
17633 else if (attr_form_is_ref (attr))
17634 {
17635 struct dwarf2_cu *target_cu = cu;
17636 struct die_info *target_die;
17637 struct attribute *target_attr;
17638
17639 target_die = follow_die_ref (die, attr, &target_cu);
17640 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17641 if (target_attr == NULL)
17642 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17643 target_cu);
17644 if (target_attr == NULL)
17645 return 0;
17646
17647 switch (target_attr->name)
17648 {
17649 case DW_AT_location:
17650 if (attr_form_is_section_offset (target_attr))
17651 {
17652 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17653 baton->property_type = die_type (target_die, target_cu);
17654 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17655 prop->data.baton = baton;
17656 prop->kind = PROP_LOCLIST;
17657 gdb_assert (prop->data.baton != NULL);
17658 }
17659 else if (attr_form_is_block (target_attr))
17660 {
17661 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17662 baton->property_type = die_type (target_die, target_cu);
17663 baton->locexpr.per_cu = cu->per_cu;
17664 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17665 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17666 baton->locexpr.is_reference = true;
17667 prop->data.baton = baton;
17668 prop->kind = PROP_LOCEXPR;
17669 gdb_assert (prop->data.baton != NULL);
17670 }
17671 else
17672 {
17673 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17674 "dynamic property");
17675 return 0;
17676 }
17677 break;
17678 case DW_AT_data_member_location:
17679 {
17680 LONGEST offset;
17681
17682 if (!handle_data_member_location (target_die, target_cu,
17683 &offset))
17684 return 0;
17685
17686 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17687 baton->property_type = read_type_die (target_die->parent,
17688 target_cu);
17689 baton->offset_info.offset = offset;
17690 baton->offset_info.type = die_type (target_die, target_cu);
17691 prop->data.baton = baton;
17692 prop->kind = PROP_ADDR_OFFSET;
17693 break;
17694 }
17695 }
17696 }
17697 else if (attr_form_is_constant (attr))
17698 {
17699 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17700 prop->kind = PROP_CONST;
17701 }
17702 else
17703 {
17704 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17705 dwarf2_name (die, cu));
17706 return 0;
17707 }
17708
17709 return 1;
17710 }
17711
17712 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17713 UNSIGNED_P controls if the integer is unsigned or not. */
17714
17715 static struct type *
17716 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17717 int size_in_bytes, bool unsigned_p)
17718 {
17719 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17720 struct type *int_type;
17721
17722 /* Helper macro to examine the various builtin types. */
17723 #define TRY_TYPE(F) \
17724 int_type = (unsigned_p \
17725 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17726 : objfile_type (objfile)->builtin_ ## F); \
17727 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17728 return int_type
17729
17730 TRY_TYPE (char);
17731 TRY_TYPE (short);
17732 TRY_TYPE (int);
17733 TRY_TYPE (long);
17734 TRY_TYPE (long_long);
17735
17736 #undef TRY_TYPE
17737
17738 gdb_assert_not_reached ("unable to find suitable integer type");
17739 }
17740
17741 /* Find an integer type the same size as the address size given in the
17742 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17743 is unsigned or not. */
17744
17745 static struct type *
17746 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17747 bool unsigned_p)
17748 {
17749 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17750 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17751 }
17752
17753 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17754 present (which is valid) then compute the default type based on the
17755 compilation units address size. */
17756
17757 static struct type *
17758 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17759 {
17760 struct type *index_type = die_type (die, cu);
17761
17762 /* Dwarf-2 specifications explicitly allows to create subrange types
17763 without specifying a base type.
17764 In that case, the base type must be set to the type of
17765 the lower bound, upper bound or count, in that order, if any of these
17766 three attributes references an object that has a type.
17767 If no base type is found, the Dwarf-2 specifications say that
17768 a signed integer type of size equal to the size of an address should
17769 be used.
17770 For the following C code: `extern char gdb_int [];'
17771 GCC produces an empty range DIE.
17772 FIXME: muller/2010-05-28: Possible references to object for low bound,
17773 high bound or count are not yet handled by this code. */
17774 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17775 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17776
17777 return index_type;
17778 }
17779
17780 /* Read the given DW_AT_subrange DIE. */
17781
17782 static struct type *
17783 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17784 {
17785 struct type *base_type, *orig_base_type;
17786 struct type *range_type;
17787 struct attribute *attr;
17788 struct dynamic_prop low, high;
17789 int low_default_is_valid;
17790 int high_bound_is_count = 0;
17791 const char *name;
17792 ULONGEST negative_mask;
17793
17794 orig_base_type = read_subrange_index_type (die, cu);
17795
17796 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17797 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17798 creating the range type, but we use the result of check_typedef
17799 when examining properties of the type. */
17800 base_type = check_typedef (orig_base_type);
17801
17802 /* The die_type call above may have already set the type for this DIE. */
17803 range_type = get_die_type (die, cu);
17804 if (range_type)
17805 return range_type;
17806
17807 low.kind = PROP_CONST;
17808 high.kind = PROP_CONST;
17809 high.data.const_val = 0;
17810
17811 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17812 omitting DW_AT_lower_bound. */
17813 switch (cu->language)
17814 {
17815 case language_c:
17816 case language_cplus:
17817 low.data.const_val = 0;
17818 low_default_is_valid = 1;
17819 break;
17820 case language_fortran:
17821 low.data.const_val = 1;
17822 low_default_is_valid = 1;
17823 break;
17824 case language_d:
17825 case language_objc:
17826 case language_rust:
17827 low.data.const_val = 0;
17828 low_default_is_valid = (cu->header.version >= 4);
17829 break;
17830 case language_ada:
17831 case language_m2:
17832 case language_pascal:
17833 low.data.const_val = 1;
17834 low_default_is_valid = (cu->header.version >= 4);
17835 break;
17836 default:
17837 low.data.const_val = 0;
17838 low_default_is_valid = 0;
17839 break;
17840 }
17841
17842 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17843 if (attr != nullptr)
17844 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17845 else if (!low_default_is_valid)
17846 complaint (_("Missing DW_AT_lower_bound "
17847 "- DIE at %s [in module %s]"),
17848 sect_offset_str (die->sect_off),
17849 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17850
17851 struct attribute *attr_ub, *attr_count;
17852 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17853 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17854 {
17855 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17856 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17857 {
17858 /* If bounds are constant do the final calculation here. */
17859 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17860 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17861 else
17862 high_bound_is_count = 1;
17863 }
17864 else
17865 {
17866 if (attr_ub != NULL)
17867 complaint (_("Unresolved DW_AT_upper_bound "
17868 "- DIE at %s [in module %s]"),
17869 sect_offset_str (die->sect_off),
17870 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17871 if (attr_count != NULL)
17872 complaint (_("Unresolved DW_AT_count "
17873 "- DIE at %s [in module %s]"),
17874 sect_offset_str (die->sect_off),
17875 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17876 }
17877 }
17878
17879 LONGEST bias = 0;
17880 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17881 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17882 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17883
17884 /* Normally, the DWARF producers are expected to use a signed
17885 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17886 But this is unfortunately not always the case, as witnessed
17887 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17888 is used instead. To work around that ambiguity, we treat
17889 the bounds as signed, and thus sign-extend their values, when
17890 the base type is signed. */
17891 negative_mask =
17892 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17893 if (low.kind == PROP_CONST
17894 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17895 low.data.const_val |= negative_mask;
17896 if (high.kind == PROP_CONST
17897 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17898 high.data.const_val |= negative_mask;
17899
17900 /* Check for bit and byte strides. */
17901 struct dynamic_prop byte_stride_prop;
17902 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17903 if (attr_byte_stride != nullptr)
17904 {
17905 struct type *prop_type
17906 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17907 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17908 prop_type);
17909 }
17910
17911 struct dynamic_prop bit_stride_prop;
17912 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17913 if (attr_bit_stride != nullptr)
17914 {
17915 /* It only makes sense to have either a bit or byte stride. */
17916 if (attr_byte_stride != nullptr)
17917 {
17918 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17919 "- DIE at %s [in module %s]"),
17920 sect_offset_str (die->sect_off),
17921 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17922 attr_bit_stride = nullptr;
17923 }
17924 else
17925 {
17926 struct type *prop_type
17927 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17928 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17929 prop_type);
17930 }
17931 }
17932
17933 if (attr_byte_stride != nullptr
17934 || attr_bit_stride != nullptr)
17935 {
17936 bool byte_stride_p = (attr_byte_stride != nullptr);
17937 struct dynamic_prop *stride
17938 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17939
17940 range_type
17941 = create_range_type_with_stride (NULL, orig_base_type, &low,
17942 &high, bias, stride, byte_stride_p);
17943 }
17944 else
17945 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17946
17947 if (high_bound_is_count)
17948 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17949
17950 /* Ada expects an empty array on no boundary attributes. */
17951 if (attr == NULL && cu->language != language_ada)
17952 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17953
17954 name = dwarf2_name (die, cu);
17955 if (name)
17956 TYPE_NAME (range_type) = name;
17957
17958 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17959 if (attr != nullptr)
17960 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17961
17962 maybe_set_alignment (cu, die, range_type);
17963
17964 set_die_type (die, range_type, cu);
17965
17966 /* set_die_type should be already done. */
17967 set_descriptive_type (range_type, die, cu);
17968
17969 return range_type;
17970 }
17971
17972 static struct type *
17973 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17974 {
17975 struct type *type;
17976
17977 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17978 NULL);
17979 TYPE_NAME (type) = dwarf2_name (die, cu);
17980
17981 /* In Ada, an unspecified type is typically used when the description
17982 of the type is deferred to a different unit. When encountering
17983 such a type, we treat it as a stub, and try to resolve it later on,
17984 when needed. */
17985 if (cu->language == language_ada)
17986 TYPE_STUB (type) = 1;
17987
17988 return set_die_type (die, type, cu);
17989 }
17990
17991 /* Read a single die and all its descendents. Set the die's sibling
17992 field to NULL; set other fields in the die correctly, and set all
17993 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17994 location of the info_ptr after reading all of those dies. PARENT
17995 is the parent of the die in question. */
17996
17997 static struct die_info *
17998 read_die_and_children (const struct die_reader_specs *reader,
17999 const gdb_byte *info_ptr,
18000 const gdb_byte **new_info_ptr,
18001 struct die_info *parent)
18002 {
18003 struct die_info *die;
18004 const gdb_byte *cur_ptr;
18005 int has_children;
18006
18007 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18008 if (die == NULL)
18009 {
18010 *new_info_ptr = cur_ptr;
18011 return NULL;
18012 }
18013 store_in_ref_table (die, reader->cu);
18014
18015 if (has_children)
18016 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18017 else
18018 {
18019 die->child = NULL;
18020 *new_info_ptr = cur_ptr;
18021 }
18022
18023 die->sibling = NULL;
18024 die->parent = parent;
18025 return die;
18026 }
18027
18028 /* Read a die, all of its descendents, and all of its siblings; set
18029 all of the fields of all of the dies correctly. Arguments are as
18030 in read_die_and_children. */
18031
18032 static struct die_info *
18033 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18034 const gdb_byte *info_ptr,
18035 const gdb_byte **new_info_ptr,
18036 struct die_info *parent)
18037 {
18038 struct die_info *first_die, *last_sibling;
18039 const gdb_byte *cur_ptr;
18040
18041 cur_ptr = info_ptr;
18042 first_die = last_sibling = NULL;
18043
18044 while (1)
18045 {
18046 struct die_info *die
18047 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18048
18049 if (die == NULL)
18050 {
18051 *new_info_ptr = cur_ptr;
18052 return first_die;
18053 }
18054
18055 if (!first_die)
18056 first_die = die;
18057 else
18058 last_sibling->sibling = die;
18059
18060 last_sibling = die;
18061 }
18062 }
18063
18064 /* Read a die, all of its descendents, and all of its siblings; set
18065 all of the fields of all of the dies correctly. Arguments are as
18066 in read_die_and_children.
18067 This the main entry point for reading a DIE and all its children. */
18068
18069 static struct die_info *
18070 read_die_and_siblings (const struct die_reader_specs *reader,
18071 const gdb_byte *info_ptr,
18072 const gdb_byte **new_info_ptr,
18073 struct die_info *parent)
18074 {
18075 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18076 new_info_ptr, parent);
18077
18078 if (dwarf_die_debug)
18079 {
18080 fprintf_unfiltered (gdb_stdlog,
18081 "Read die from %s@0x%x of %s:\n",
18082 reader->die_section->get_name (),
18083 (unsigned) (info_ptr - reader->die_section->buffer),
18084 bfd_get_filename (reader->abfd));
18085 dump_die (die, dwarf_die_debug);
18086 }
18087
18088 return die;
18089 }
18090
18091 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18092 attributes.
18093 The caller is responsible for filling in the extra attributes
18094 and updating (*DIEP)->num_attrs.
18095 Set DIEP to point to a newly allocated die with its information,
18096 except for its child, sibling, and parent fields.
18097 Set HAS_CHILDREN to tell whether the die has children or not. */
18098
18099 static const gdb_byte *
18100 read_full_die_1 (const struct die_reader_specs *reader,
18101 struct die_info **diep, const gdb_byte *info_ptr,
18102 int *has_children, int num_extra_attrs)
18103 {
18104 unsigned int abbrev_number, bytes_read, i;
18105 struct abbrev_info *abbrev;
18106 struct die_info *die;
18107 struct dwarf2_cu *cu = reader->cu;
18108 bfd *abfd = reader->abfd;
18109
18110 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18111 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18112 info_ptr += bytes_read;
18113 if (!abbrev_number)
18114 {
18115 *diep = NULL;
18116 *has_children = 0;
18117 return info_ptr;
18118 }
18119
18120 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18121 if (!abbrev)
18122 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18123 abbrev_number,
18124 bfd_get_filename (abfd));
18125
18126 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18127 die->sect_off = sect_off;
18128 die->tag = abbrev->tag;
18129 die->abbrev = abbrev_number;
18130
18131 /* Make the result usable.
18132 The caller needs to update num_attrs after adding the extra
18133 attributes. */
18134 die->num_attrs = abbrev->num_attrs;
18135
18136 std::vector<int> indexes_that_need_reprocess;
18137 for (i = 0; i < abbrev->num_attrs; ++i)
18138 {
18139 bool need_reprocess;
18140 info_ptr =
18141 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18142 info_ptr, &need_reprocess);
18143 if (need_reprocess)
18144 indexes_that_need_reprocess.push_back (i);
18145 }
18146
18147 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18148 if (attr != nullptr)
18149 cu->str_offsets_base = DW_UNSND (attr);
18150
18151 auto maybe_addr_base = lookup_addr_base(die);
18152 if (maybe_addr_base.has_value ())
18153 cu->addr_base = *maybe_addr_base;
18154 for (int index : indexes_that_need_reprocess)
18155 read_attribute_reprocess (reader, &die->attrs[index]);
18156 *diep = die;
18157 *has_children = abbrev->has_children;
18158 return info_ptr;
18159 }
18160
18161 /* Read a die and all its attributes.
18162 Set DIEP to point to a newly allocated die with its information,
18163 except for its child, sibling, and parent fields.
18164 Set HAS_CHILDREN to tell whether the die has children or not. */
18165
18166 static const gdb_byte *
18167 read_full_die (const struct die_reader_specs *reader,
18168 struct die_info **diep, const gdb_byte *info_ptr,
18169 int *has_children)
18170 {
18171 const gdb_byte *result;
18172
18173 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18174
18175 if (dwarf_die_debug)
18176 {
18177 fprintf_unfiltered (gdb_stdlog,
18178 "Read die from %s@0x%x of %s:\n",
18179 reader->die_section->get_name (),
18180 (unsigned) (info_ptr - reader->die_section->buffer),
18181 bfd_get_filename (reader->abfd));
18182 dump_die (*diep, dwarf_die_debug);
18183 }
18184
18185 return result;
18186 }
18187 \f
18188
18189 /* Returns nonzero if TAG represents a type that we might generate a partial
18190 symbol for. */
18191
18192 static int
18193 is_type_tag_for_partial (int tag)
18194 {
18195 switch (tag)
18196 {
18197 #if 0
18198 /* Some types that would be reasonable to generate partial symbols for,
18199 that we don't at present. */
18200 case DW_TAG_array_type:
18201 case DW_TAG_file_type:
18202 case DW_TAG_ptr_to_member_type:
18203 case DW_TAG_set_type:
18204 case DW_TAG_string_type:
18205 case DW_TAG_subroutine_type:
18206 #endif
18207 case DW_TAG_base_type:
18208 case DW_TAG_class_type:
18209 case DW_TAG_interface_type:
18210 case DW_TAG_enumeration_type:
18211 case DW_TAG_structure_type:
18212 case DW_TAG_subrange_type:
18213 case DW_TAG_typedef:
18214 case DW_TAG_union_type:
18215 return 1;
18216 default:
18217 return 0;
18218 }
18219 }
18220
18221 /* Load all DIEs that are interesting for partial symbols into memory. */
18222
18223 static struct partial_die_info *
18224 load_partial_dies (const struct die_reader_specs *reader,
18225 const gdb_byte *info_ptr, int building_psymtab)
18226 {
18227 struct dwarf2_cu *cu = reader->cu;
18228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18229 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18230 unsigned int bytes_read;
18231 unsigned int load_all = 0;
18232 int nesting_level = 1;
18233
18234 parent_die = NULL;
18235 last_die = NULL;
18236
18237 gdb_assert (cu->per_cu != NULL);
18238 if (cu->per_cu->load_all_dies)
18239 load_all = 1;
18240
18241 cu->partial_dies
18242 = htab_create_alloc_ex (cu->header.length / 12,
18243 partial_die_hash,
18244 partial_die_eq,
18245 NULL,
18246 &cu->comp_unit_obstack,
18247 hashtab_obstack_allocate,
18248 dummy_obstack_deallocate);
18249
18250 while (1)
18251 {
18252 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18253
18254 /* A NULL abbrev means the end of a series of children. */
18255 if (abbrev == NULL)
18256 {
18257 if (--nesting_level == 0)
18258 return first_die;
18259
18260 info_ptr += bytes_read;
18261 last_die = parent_die;
18262 parent_die = parent_die->die_parent;
18263 continue;
18264 }
18265
18266 /* Check for template arguments. We never save these; if
18267 they're seen, we just mark the parent, and go on our way. */
18268 if (parent_die != NULL
18269 && cu->language == language_cplus
18270 && (abbrev->tag == DW_TAG_template_type_param
18271 || abbrev->tag == DW_TAG_template_value_param))
18272 {
18273 parent_die->has_template_arguments = 1;
18274
18275 if (!load_all)
18276 {
18277 /* We don't need a partial DIE for the template argument. */
18278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18279 continue;
18280 }
18281 }
18282
18283 /* We only recurse into c++ subprograms looking for template arguments.
18284 Skip their other children. */
18285 if (!load_all
18286 && cu->language == language_cplus
18287 && parent_die != NULL
18288 && parent_die->tag == DW_TAG_subprogram)
18289 {
18290 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18291 continue;
18292 }
18293
18294 /* Check whether this DIE is interesting enough to save. Normally
18295 we would not be interested in members here, but there may be
18296 later variables referencing them via DW_AT_specification (for
18297 static members). */
18298 if (!load_all
18299 && !is_type_tag_for_partial (abbrev->tag)
18300 && abbrev->tag != DW_TAG_constant
18301 && abbrev->tag != DW_TAG_enumerator
18302 && abbrev->tag != DW_TAG_subprogram
18303 && abbrev->tag != DW_TAG_inlined_subroutine
18304 && abbrev->tag != DW_TAG_lexical_block
18305 && abbrev->tag != DW_TAG_variable
18306 && abbrev->tag != DW_TAG_namespace
18307 && abbrev->tag != DW_TAG_module
18308 && abbrev->tag != DW_TAG_member
18309 && abbrev->tag != DW_TAG_imported_unit
18310 && abbrev->tag != DW_TAG_imported_declaration)
18311 {
18312 /* Otherwise we skip to the next sibling, if any. */
18313 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18314 continue;
18315 }
18316
18317 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18318 abbrev);
18319
18320 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18321
18322 /* This two-pass algorithm for processing partial symbols has a
18323 high cost in cache pressure. Thus, handle some simple cases
18324 here which cover the majority of C partial symbols. DIEs
18325 which neither have specification tags in them, nor could have
18326 specification tags elsewhere pointing at them, can simply be
18327 processed and discarded.
18328
18329 This segment is also optional; scan_partial_symbols and
18330 add_partial_symbol will handle these DIEs if we chain
18331 them in normally. When compilers which do not emit large
18332 quantities of duplicate debug information are more common,
18333 this code can probably be removed. */
18334
18335 /* Any complete simple types at the top level (pretty much all
18336 of them, for a language without namespaces), can be processed
18337 directly. */
18338 if (parent_die == NULL
18339 && pdi.has_specification == 0
18340 && pdi.is_declaration == 0
18341 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18342 || pdi.tag == DW_TAG_base_type
18343 || pdi.tag == DW_TAG_subrange_type))
18344 {
18345 if (building_psymtab && pdi.name != NULL)
18346 add_psymbol_to_list (pdi.name, false,
18347 VAR_DOMAIN, LOC_TYPEDEF, -1,
18348 psymbol_placement::STATIC,
18349 0, cu->language, objfile);
18350 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18351 continue;
18352 }
18353
18354 /* The exception for DW_TAG_typedef with has_children above is
18355 a workaround of GCC PR debug/47510. In the case of this complaint
18356 type_name_or_error will error on such types later.
18357
18358 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18359 it could not find the child DIEs referenced later, this is checked
18360 above. In correct DWARF DW_TAG_typedef should have no children. */
18361
18362 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18363 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18364 "- DIE at %s [in module %s]"),
18365 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18366
18367 /* If we're at the second level, and we're an enumerator, and
18368 our parent has no specification (meaning possibly lives in a
18369 namespace elsewhere), then we can add the partial symbol now
18370 instead of queueing it. */
18371 if (pdi.tag == DW_TAG_enumerator
18372 && parent_die != NULL
18373 && parent_die->die_parent == NULL
18374 && parent_die->tag == DW_TAG_enumeration_type
18375 && parent_die->has_specification == 0)
18376 {
18377 if (pdi.name == NULL)
18378 complaint (_("malformed enumerator DIE ignored"));
18379 else if (building_psymtab)
18380 add_psymbol_to_list (pdi.name, false,
18381 VAR_DOMAIN, LOC_CONST, -1,
18382 cu->language == language_cplus
18383 ? psymbol_placement::GLOBAL
18384 : psymbol_placement::STATIC,
18385 0, cu->language, objfile);
18386
18387 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18388 continue;
18389 }
18390
18391 struct partial_die_info *part_die
18392 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18393
18394 /* We'll save this DIE so link it in. */
18395 part_die->die_parent = parent_die;
18396 part_die->die_sibling = NULL;
18397 part_die->die_child = NULL;
18398
18399 if (last_die && last_die == parent_die)
18400 last_die->die_child = part_die;
18401 else if (last_die)
18402 last_die->die_sibling = part_die;
18403
18404 last_die = part_die;
18405
18406 if (first_die == NULL)
18407 first_die = part_die;
18408
18409 /* Maybe add the DIE to the hash table. Not all DIEs that we
18410 find interesting need to be in the hash table, because we
18411 also have the parent/sibling/child chains; only those that we
18412 might refer to by offset later during partial symbol reading.
18413
18414 For now this means things that might have be the target of a
18415 DW_AT_specification, DW_AT_abstract_origin, or
18416 DW_AT_extension. DW_AT_extension will refer only to
18417 namespaces; DW_AT_abstract_origin refers to functions (and
18418 many things under the function DIE, but we do not recurse
18419 into function DIEs during partial symbol reading) and
18420 possibly variables as well; DW_AT_specification refers to
18421 declarations. Declarations ought to have the DW_AT_declaration
18422 flag. It happens that GCC forgets to put it in sometimes, but
18423 only for functions, not for types.
18424
18425 Adding more things than necessary to the hash table is harmless
18426 except for the performance cost. Adding too few will result in
18427 wasted time in find_partial_die, when we reread the compilation
18428 unit with load_all_dies set. */
18429
18430 if (load_all
18431 || abbrev->tag == DW_TAG_constant
18432 || abbrev->tag == DW_TAG_subprogram
18433 || abbrev->tag == DW_TAG_variable
18434 || abbrev->tag == DW_TAG_namespace
18435 || part_die->is_declaration)
18436 {
18437 void **slot;
18438
18439 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18440 to_underlying (part_die->sect_off),
18441 INSERT);
18442 *slot = part_die;
18443 }
18444
18445 /* For some DIEs we want to follow their children (if any). For C
18446 we have no reason to follow the children of structures; for other
18447 languages we have to, so that we can get at method physnames
18448 to infer fully qualified class names, for DW_AT_specification,
18449 and for C++ template arguments. For C++, we also look one level
18450 inside functions to find template arguments (if the name of the
18451 function does not already contain the template arguments).
18452
18453 For Ada and Fortran, we need to scan the children of subprograms
18454 and lexical blocks as well because these languages allow the
18455 definition of nested entities that could be interesting for the
18456 debugger, such as nested subprograms for instance. */
18457 if (last_die->has_children
18458 && (load_all
18459 || last_die->tag == DW_TAG_namespace
18460 || last_die->tag == DW_TAG_module
18461 || last_die->tag == DW_TAG_enumeration_type
18462 || (cu->language == language_cplus
18463 && last_die->tag == DW_TAG_subprogram
18464 && (last_die->name == NULL
18465 || strchr (last_die->name, '<') == NULL))
18466 || (cu->language != language_c
18467 && (last_die->tag == DW_TAG_class_type
18468 || last_die->tag == DW_TAG_interface_type
18469 || last_die->tag == DW_TAG_structure_type
18470 || last_die->tag == DW_TAG_union_type))
18471 || ((cu->language == language_ada
18472 || cu->language == language_fortran)
18473 && (last_die->tag == DW_TAG_subprogram
18474 || last_die->tag == DW_TAG_lexical_block))))
18475 {
18476 nesting_level++;
18477 parent_die = last_die;
18478 continue;
18479 }
18480
18481 /* Otherwise we skip to the next sibling, if any. */
18482 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18483
18484 /* Back to the top, do it again. */
18485 }
18486 }
18487
18488 partial_die_info::partial_die_info (sect_offset sect_off_,
18489 struct abbrev_info *abbrev)
18490 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18491 {
18492 }
18493
18494 /* Read a minimal amount of information into the minimal die structure.
18495 INFO_PTR should point just after the initial uleb128 of a DIE. */
18496
18497 const gdb_byte *
18498 partial_die_info::read (const struct die_reader_specs *reader,
18499 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18500 {
18501 struct dwarf2_cu *cu = reader->cu;
18502 struct dwarf2_per_objfile *dwarf2_per_objfile
18503 = cu->per_cu->dwarf2_per_objfile;
18504 unsigned int i;
18505 int has_low_pc_attr = 0;
18506 int has_high_pc_attr = 0;
18507 int high_pc_relative = 0;
18508
18509 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18510 for (i = 0; i < abbrev.num_attrs; ++i)
18511 {
18512 bool need_reprocess;
18513 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18514 info_ptr, &need_reprocess);
18515 /* String and address offsets that need to do the reprocessing have
18516 already been read at this point, so there is no need to wait until
18517 the loop terminates to do the reprocessing. */
18518 if (need_reprocess)
18519 read_attribute_reprocess (reader, &attr_vec[i]);
18520 attribute &attr = attr_vec[i];
18521 /* Store the data if it is of an attribute we want to keep in a
18522 partial symbol table. */
18523 switch (attr.name)
18524 {
18525 case DW_AT_name:
18526 switch (tag)
18527 {
18528 case DW_TAG_compile_unit:
18529 case DW_TAG_partial_unit:
18530 case DW_TAG_type_unit:
18531 /* Compilation units have a DW_AT_name that is a filename, not
18532 a source language identifier. */
18533 case DW_TAG_enumeration_type:
18534 case DW_TAG_enumerator:
18535 /* These tags always have simple identifiers already; no need
18536 to canonicalize them. */
18537 name = DW_STRING (&attr);
18538 break;
18539 default:
18540 {
18541 struct objfile *objfile = dwarf2_per_objfile->objfile;
18542
18543 name
18544 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18545 &objfile->per_bfd->storage_obstack);
18546 }
18547 break;
18548 }
18549 break;
18550 case DW_AT_linkage_name:
18551 case DW_AT_MIPS_linkage_name:
18552 /* Note that both forms of linkage name might appear. We
18553 assume they will be the same, and we only store the last
18554 one we see. */
18555 linkage_name = DW_STRING (&attr);
18556 break;
18557 case DW_AT_low_pc:
18558 has_low_pc_attr = 1;
18559 lowpc = attr_value_as_address (&attr);
18560 break;
18561 case DW_AT_high_pc:
18562 has_high_pc_attr = 1;
18563 highpc = attr_value_as_address (&attr);
18564 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18565 high_pc_relative = 1;
18566 break;
18567 case DW_AT_location:
18568 /* Support the .debug_loc offsets. */
18569 if (attr_form_is_block (&attr))
18570 {
18571 d.locdesc = DW_BLOCK (&attr);
18572 }
18573 else if (attr_form_is_section_offset (&attr))
18574 {
18575 dwarf2_complex_location_expr_complaint ();
18576 }
18577 else
18578 {
18579 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18580 "partial symbol information");
18581 }
18582 break;
18583 case DW_AT_external:
18584 is_external = DW_UNSND (&attr);
18585 break;
18586 case DW_AT_declaration:
18587 is_declaration = DW_UNSND (&attr);
18588 break;
18589 case DW_AT_type:
18590 has_type = 1;
18591 break;
18592 case DW_AT_abstract_origin:
18593 case DW_AT_specification:
18594 case DW_AT_extension:
18595 has_specification = 1;
18596 spec_offset = dwarf2_get_ref_die_offset (&attr);
18597 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18598 || cu->per_cu->is_dwz);
18599 break;
18600 case DW_AT_sibling:
18601 /* Ignore absolute siblings, they might point outside of
18602 the current compile unit. */
18603 if (attr.form == DW_FORM_ref_addr)
18604 complaint (_("ignoring absolute DW_AT_sibling"));
18605 else
18606 {
18607 const gdb_byte *buffer = reader->buffer;
18608 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18609 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18610
18611 if (sibling_ptr < info_ptr)
18612 complaint (_("DW_AT_sibling points backwards"));
18613 else if (sibling_ptr > reader->buffer_end)
18614 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18615 else
18616 sibling = sibling_ptr;
18617 }
18618 break;
18619 case DW_AT_byte_size:
18620 has_byte_size = 1;
18621 break;
18622 case DW_AT_const_value:
18623 has_const_value = 1;
18624 break;
18625 case DW_AT_calling_convention:
18626 /* DWARF doesn't provide a way to identify a program's source-level
18627 entry point. DW_AT_calling_convention attributes are only meant
18628 to describe functions' calling conventions.
18629
18630 However, because it's a necessary piece of information in
18631 Fortran, and before DWARF 4 DW_CC_program was the only
18632 piece of debugging information whose definition refers to
18633 a 'main program' at all, several compilers marked Fortran
18634 main programs with DW_CC_program --- even when those
18635 functions use the standard calling conventions.
18636
18637 Although DWARF now specifies a way to provide this
18638 information, we support this practice for backward
18639 compatibility. */
18640 if (DW_UNSND (&attr) == DW_CC_program
18641 && cu->language == language_fortran)
18642 main_subprogram = 1;
18643 break;
18644 case DW_AT_inline:
18645 if (DW_UNSND (&attr) == DW_INL_inlined
18646 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18647 may_be_inlined = 1;
18648 break;
18649
18650 case DW_AT_import:
18651 if (tag == DW_TAG_imported_unit)
18652 {
18653 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18654 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18655 || cu->per_cu->is_dwz);
18656 }
18657 break;
18658
18659 case DW_AT_main_subprogram:
18660 main_subprogram = DW_UNSND (&attr);
18661 break;
18662
18663 case DW_AT_ranges:
18664 {
18665 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18666 but that requires a full DIE, so instead we just
18667 reimplement it. */
18668 int need_ranges_base = tag != DW_TAG_compile_unit;
18669 unsigned int ranges_offset = (DW_UNSND (&attr)
18670 + (need_ranges_base
18671 ? cu->ranges_base
18672 : 0));
18673
18674 /* Value of the DW_AT_ranges attribute is the offset in the
18675 .debug_ranges section. */
18676 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18677 nullptr))
18678 has_pc_info = 1;
18679 }
18680 break;
18681
18682 default:
18683 break;
18684 }
18685 }
18686
18687 /* For Ada, if both the name and the linkage name appear, we prefer
18688 the latter. This lets "catch exception" work better, regardless
18689 of the order in which the name and linkage name were emitted.
18690 Really, though, this is just a workaround for the fact that gdb
18691 doesn't store both the name and the linkage name. */
18692 if (cu->language == language_ada && linkage_name != nullptr)
18693 name = linkage_name;
18694
18695 if (high_pc_relative)
18696 highpc += lowpc;
18697
18698 if (has_low_pc_attr && has_high_pc_attr)
18699 {
18700 /* When using the GNU linker, .gnu.linkonce. sections are used to
18701 eliminate duplicate copies of functions and vtables and such.
18702 The linker will arbitrarily choose one and discard the others.
18703 The AT_*_pc values for such functions refer to local labels in
18704 these sections. If the section from that file was discarded, the
18705 labels are not in the output, so the relocs get a value of 0.
18706 If this is a discarded function, mark the pc bounds as invalid,
18707 so that GDB will ignore it. */
18708 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18709 {
18710 struct objfile *objfile = dwarf2_per_objfile->objfile;
18711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18712
18713 complaint (_("DW_AT_low_pc %s is zero "
18714 "for DIE at %s [in module %s]"),
18715 paddress (gdbarch, lowpc),
18716 sect_offset_str (sect_off),
18717 objfile_name (objfile));
18718 }
18719 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18720 else if (lowpc >= highpc)
18721 {
18722 struct objfile *objfile = dwarf2_per_objfile->objfile;
18723 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18724
18725 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18726 "for DIE at %s [in module %s]"),
18727 paddress (gdbarch, lowpc),
18728 paddress (gdbarch, highpc),
18729 sect_offset_str (sect_off),
18730 objfile_name (objfile));
18731 }
18732 else
18733 has_pc_info = 1;
18734 }
18735
18736 return info_ptr;
18737 }
18738
18739 /* Find a cached partial DIE at OFFSET in CU. */
18740
18741 struct partial_die_info *
18742 dwarf2_cu::find_partial_die (sect_offset sect_off)
18743 {
18744 struct partial_die_info *lookup_die = NULL;
18745 struct partial_die_info part_die (sect_off);
18746
18747 lookup_die = ((struct partial_die_info *)
18748 htab_find_with_hash (partial_dies, &part_die,
18749 to_underlying (sect_off)));
18750
18751 return lookup_die;
18752 }
18753
18754 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18755 except in the case of .debug_types DIEs which do not reference
18756 outside their CU (they do however referencing other types via
18757 DW_FORM_ref_sig8). */
18758
18759 static const struct cu_partial_die_info
18760 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18761 {
18762 struct dwarf2_per_objfile *dwarf2_per_objfile
18763 = cu->per_cu->dwarf2_per_objfile;
18764 struct objfile *objfile = dwarf2_per_objfile->objfile;
18765 struct dwarf2_per_cu_data *per_cu = NULL;
18766 struct partial_die_info *pd = NULL;
18767
18768 if (offset_in_dwz == cu->per_cu->is_dwz
18769 && offset_in_cu_p (&cu->header, sect_off))
18770 {
18771 pd = cu->find_partial_die (sect_off);
18772 if (pd != NULL)
18773 return { cu, pd };
18774 /* We missed recording what we needed.
18775 Load all dies and try again. */
18776 per_cu = cu->per_cu;
18777 }
18778 else
18779 {
18780 /* TUs don't reference other CUs/TUs (except via type signatures). */
18781 if (cu->per_cu->is_debug_types)
18782 {
18783 error (_("Dwarf Error: Type Unit at offset %s contains"
18784 " external reference to offset %s [in module %s].\n"),
18785 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18786 bfd_get_filename (objfile->obfd));
18787 }
18788 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18789 dwarf2_per_objfile);
18790
18791 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18792 load_partial_comp_unit (per_cu);
18793
18794 per_cu->cu->last_used = 0;
18795 pd = per_cu->cu->find_partial_die (sect_off);
18796 }
18797
18798 /* If we didn't find it, and not all dies have been loaded,
18799 load them all and try again. */
18800
18801 if (pd == NULL && per_cu->load_all_dies == 0)
18802 {
18803 per_cu->load_all_dies = 1;
18804
18805 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18806 THIS_CU->cu may already be in use. So we can't just free it and
18807 replace its DIEs with the ones we read in. Instead, we leave those
18808 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18809 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18810 set. */
18811 load_partial_comp_unit (per_cu);
18812
18813 pd = per_cu->cu->find_partial_die (sect_off);
18814 }
18815
18816 if (pd == NULL)
18817 internal_error (__FILE__, __LINE__,
18818 _("could not find partial DIE %s "
18819 "in cache [from module %s]\n"),
18820 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18821 return { per_cu->cu, pd };
18822 }
18823
18824 /* See if we can figure out if the class lives in a namespace. We do
18825 this by looking for a member function; its demangled name will
18826 contain namespace info, if there is any. */
18827
18828 static void
18829 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18830 struct dwarf2_cu *cu)
18831 {
18832 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18833 what template types look like, because the demangler
18834 frequently doesn't give the same name as the debug info. We
18835 could fix this by only using the demangled name to get the
18836 prefix (but see comment in read_structure_type). */
18837
18838 struct partial_die_info *real_pdi;
18839 struct partial_die_info *child_pdi;
18840
18841 /* If this DIE (this DIE's specification, if any) has a parent, then
18842 we should not do this. We'll prepend the parent's fully qualified
18843 name when we create the partial symbol. */
18844
18845 real_pdi = struct_pdi;
18846 while (real_pdi->has_specification)
18847 {
18848 auto res = find_partial_die (real_pdi->spec_offset,
18849 real_pdi->spec_is_dwz, cu);
18850 real_pdi = res.pdi;
18851 cu = res.cu;
18852 }
18853
18854 if (real_pdi->die_parent != NULL)
18855 return;
18856
18857 for (child_pdi = struct_pdi->die_child;
18858 child_pdi != NULL;
18859 child_pdi = child_pdi->die_sibling)
18860 {
18861 if (child_pdi->tag == DW_TAG_subprogram
18862 && child_pdi->linkage_name != NULL)
18863 {
18864 gdb::unique_xmalloc_ptr<char> actual_class_name
18865 (language_class_name_from_physname (cu->language_defn,
18866 child_pdi->linkage_name));
18867 if (actual_class_name != NULL)
18868 {
18869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18870 struct_pdi->name
18871 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18872 actual_class_name.get ());
18873 }
18874 break;
18875 }
18876 }
18877 }
18878
18879 void
18880 partial_die_info::fixup (struct dwarf2_cu *cu)
18881 {
18882 /* Once we've fixed up a die, there's no point in doing so again.
18883 This also avoids a memory leak if we were to call
18884 guess_partial_die_structure_name multiple times. */
18885 if (fixup_called)
18886 return;
18887
18888 /* If we found a reference attribute and the DIE has no name, try
18889 to find a name in the referred to DIE. */
18890
18891 if (name == NULL && has_specification)
18892 {
18893 struct partial_die_info *spec_die;
18894
18895 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18896 spec_die = res.pdi;
18897 cu = res.cu;
18898
18899 spec_die->fixup (cu);
18900
18901 if (spec_die->name)
18902 {
18903 name = spec_die->name;
18904
18905 /* Copy DW_AT_external attribute if it is set. */
18906 if (spec_die->is_external)
18907 is_external = spec_die->is_external;
18908 }
18909 }
18910
18911 /* Set default names for some unnamed DIEs. */
18912
18913 if (name == NULL && tag == DW_TAG_namespace)
18914 name = CP_ANONYMOUS_NAMESPACE_STR;
18915
18916 /* If there is no parent die to provide a namespace, and there are
18917 children, see if we can determine the namespace from their linkage
18918 name. */
18919 if (cu->language == language_cplus
18920 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18921 && die_parent == NULL
18922 && has_children
18923 && (tag == DW_TAG_class_type
18924 || tag == DW_TAG_structure_type
18925 || tag == DW_TAG_union_type))
18926 guess_partial_die_structure_name (this, cu);
18927
18928 /* GCC might emit a nameless struct or union that has a linkage
18929 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18930 if (name == NULL
18931 && (tag == DW_TAG_class_type
18932 || tag == DW_TAG_interface_type
18933 || tag == DW_TAG_structure_type
18934 || tag == DW_TAG_union_type)
18935 && linkage_name != NULL)
18936 {
18937 gdb::unique_xmalloc_ptr<char> demangled
18938 (gdb_demangle (linkage_name, DMGL_TYPES));
18939 if (demangled != nullptr)
18940 {
18941 const char *base;
18942
18943 /* Strip any leading namespaces/classes, keep only the base name.
18944 DW_AT_name for named DIEs does not contain the prefixes. */
18945 base = strrchr (demangled.get (), ':');
18946 if (base && base > demangled.get () && base[-1] == ':')
18947 base++;
18948 else
18949 base = demangled.get ();
18950
18951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18952 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18953 }
18954 }
18955
18956 fixup_called = 1;
18957 }
18958
18959 /* Process the attributes that had to be skipped in the first round. These
18960 attributes are the ones that need str_offsets_base or addr_base attributes.
18961 They could not have been processed in the first round, because at the time
18962 the values of str_offsets_base or addr_base may not have been known. */
18963 void read_attribute_reprocess (const struct die_reader_specs *reader,
18964 struct attribute *attr)
18965 {
18966 struct dwarf2_cu *cu = reader->cu;
18967 switch (attr->form)
18968 {
18969 case DW_FORM_addrx:
18970 case DW_FORM_GNU_addr_index:
18971 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18972 break;
18973 case DW_FORM_strx:
18974 case DW_FORM_strx1:
18975 case DW_FORM_strx2:
18976 case DW_FORM_strx3:
18977 case DW_FORM_strx4:
18978 case DW_FORM_GNU_str_index:
18979 {
18980 unsigned int str_index = DW_UNSND (attr);
18981 if (reader->dwo_file != NULL)
18982 {
18983 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18984 DW_STRING_IS_CANONICAL (attr) = 0;
18985 }
18986 else
18987 {
18988 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18989 DW_STRING_IS_CANONICAL (attr) = 0;
18990 }
18991 break;
18992 }
18993 default:
18994 gdb_assert_not_reached (_("Unexpected DWARF form."));
18995 }
18996 }
18997
18998 /* Read an attribute value described by an attribute form. */
18999
19000 static const gdb_byte *
19001 read_attribute_value (const struct die_reader_specs *reader,
19002 struct attribute *attr, unsigned form,
19003 LONGEST implicit_const, const gdb_byte *info_ptr,
19004 bool *need_reprocess)
19005 {
19006 struct dwarf2_cu *cu = reader->cu;
19007 struct dwarf2_per_objfile *dwarf2_per_objfile
19008 = cu->per_cu->dwarf2_per_objfile;
19009 struct objfile *objfile = dwarf2_per_objfile->objfile;
19010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19011 bfd *abfd = reader->abfd;
19012 struct comp_unit_head *cu_header = &cu->header;
19013 unsigned int bytes_read;
19014 struct dwarf_block *blk;
19015 *need_reprocess = false;
19016
19017 attr->form = (enum dwarf_form) form;
19018 switch (form)
19019 {
19020 case DW_FORM_ref_addr:
19021 if (cu->header.version == 2)
19022 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19023 else
19024 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19025 &cu->header, &bytes_read);
19026 info_ptr += bytes_read;
19027 break;
19028 case DW_FORM_GNU_ref_alt:
19029 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19030 info_ptr += bytes_read;
19031 break;
19032 case DW_FORM_addr:
19033 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19034 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19035 info_ptr += bytes_read;
19036 break;
19037 case DW_FORM_block2:
19038 blk = dwarf_alloc_block (cu);
19039 blk->size = read_2_bytes (abfd, info_ptr);
19040 info_ptr += 2;
19041 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19042 info_ptr += blk->size;
19043 DW_BLOCK (attr) = blk;
19044 break;
19045 case DW_FORM_block4:
19046 blk = dwarf_alloc_block (cu);
19047 blk->size = read_4_bytes (abfd, info_ptr);
19048 info_ptr += 4;
19049 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19050 info_ptr += blk->size;
19051 DW_BLOCK (attr) = blk;
19052 break;
19053 case DW_FORM_data2:
19054 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19055 info_ptr += 2;
19056 break;
19057 case DW_FORM_data4:
19058 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19059 info_ptr += 4;
19060 break;
19061 case DW_FORM_data8:
19062 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19063 info_ptr += 8;
19064 break;
19065 case DW_FORM_data16:
19066 blk = dwarf_alloc_block (cu);
19067 blk->size = 16;
19068 blk->data = read_n_bytes (abfd, info_ptr, 16);
19069 info_ptr += 16;
19070 DW_BLOCK (attr) = blk;
19071 break;
19072 case DW_FORM_sec_offset:
19073 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19074 info_ptr += bytes_read;
19075 break;
19076 case DW_FORM_string:
19077 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19078 DW_STRING_IS_CANONICAL (attr) = 0;
19079 info_ptr += bytes_read;
19080 break;
19081 case DW_FORM_strp:
19082 if (!cu->per_cu->is_dwz)
19083 {
19084 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19085 abfd, info_ptr, cu_header,
19086 &bytes_read);
19087 DW_STRING_IS_CANONICAL (attr) = 0;
19088 info_ptr += bytes_read;
19089 break;
19090 }
19091 /* FALLTHROUGH */
19092 case DW_FORM_line_strp:
19093 if (!cu->per_cu->is_dwz)
19094 {
19095 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19096 abfd, info_ptr,
19097 cu_header, &bytes_read);
19098 DW_STRING_IS_CANONICAL (attr) = 0;
19099 info_ptr += bytes_read;
19100 break;
19101 }
19102 /* FALLTHROUGH */
19103 case DW_FORM_GNU_strp_alt:
19104 {
19105 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19106 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19107 &bytes_read);
19108
19109 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19110 dwz, str_offset);
19111 DW_STRING_IS_CANONICAL (attr) = 0;
19112 info_ptr += bytes_read;
19113 }
19114 break;
19115 case DW_FORM_exprloc:
19116 case DW_FORM_block:
19117 blk = dwarf_alloc_block (cu);
19118 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19119 info_ptr += bytes_read;
19120 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19121 info_ptr += blk->size;
19122 DW_BLOCK (attr) = blk;
19123 break;
19124 case DW_FORM_block1:
19125 blk = dwarf_alloc_block (cu);
19126 blk->size = read_1_byte (abfd, info_ptr);
19127 info_ptr += 1;
19128 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19129 info_ptr += blk->size;
19130 DW_BLOCK (attr) = blk;
19131 break;
19132 case DW_FORM_data1:
19133 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19134 info_ptr += 1;
19135 break;
19136 case DW_FORM_flag:
19137 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19138 info_ptr += 1;
19139 break;
19140 case DW_FORM_flag_present:
19141 DW_UNSND (attr) = 1;
19142 break;
19143 case DW_FORM_sdata:
19144 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19145 info_ptr += bytes_read;
19146 break;
19147 case DW_FORM_udata:
19148 case DW_FORM_rnglistx:
19149 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19150 info_ptr += bytes_read;
19151 break;
19152 case DW_FORM_ref1:
19153 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19154 + read_1_byte (abfd, info_ptr));
19155 info_ptr += 1;
19156 break;
19157 case DW_FORM_ref2:
19158 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19159 + read_2_bytes (abfd, info_ptr));
19160 info_ptr += 2;
19161 break;
19162 case DW_FORM_ref4:
19163 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19164 + read_4_bytes (abfd, info_ptr));
19165 info_ptr += 4;
19166 break;
19167 case DW_FORM_ref8:
19168 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19169 + read_8_bytes (abfd, info_ptr));
19170 info_ptr += 8;
19171 break;
19172 case DW_FORM_ref_sig8:
19173 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19174 info_ptr += 8;
19175 break;
19176 case DW_FORM_ref_udata:
19177 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19178 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19179 info_ptr += bytes_read;
19180 break;
19181 case DW_FORM_indirect:
19182 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19183 info_ptr += bytes_read;
19184 if (form == DW_FORM_implicit_const)
19185 {
19186 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19187 info_ptr += bytes_read;
19188 }
19189 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19190 info_ptr, need_reprocess);
19191 break;
19192 case DW_FORM_implicit_const:
19193 DW_SND (attr) = implicit_const;
19194 break;
19195 case DW_FORM_addrx:
19196 case DW_FORM_GNU_addr_index:
19197 *need_reprocess = true;
19198 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19199 info_ptr += bytes_read;
19200 break;
19201 case DW_FORM_strx:
19202 case DW_FORM_strx1:
19203 case DW_FORM_strx2:
19204 case DW_FORM_strx3:
19205 case DW_FORM_strx4:
19206 case DW_FORM_GNU_str_index:
19207 {
19208 ULONGEST str_index;
19209 if (form == DW_FORM_strx1)
19210 {
19211 str_index = read_1_byte (abfd, info_ptr);
19212 info_ptr += 1;
19213 }
19214 else if (form == DW_FORM_strx2)
19215 {
19216 str_index = read_2_bytes (abfd, info_ptr);
19217 info_ptr += 2;
19218 }
19219 else if (form == DW_FORM_strx3)
19220 {
19221 str_index = read_3_bytes (abfd, info_ptr);
19222 info_ptr += 3;
19223 }
19224 else if (form == DW_FORM_strx4)
19225 {
19226 str_index = read_4_bytes (abfd, info_ptr);
19227 info_ptr += 4;
19228 }
19229 else
19230 {
19231 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19232 info_ptr += bytes_read;
19233 }
19234 *need_reprocess = true;
19235 DW_UNSND (attr) = str_index;
19236 }
19237 break;
19238 default:
19239 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19240 dwarf_form_name (form),
19241 bfd_get_filename (abfd));
19242 }
19243
19244 /* Super hack. */
19245 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19246 attr->form = DW_FORM_GNU_ref_alt;
19247
19248 /* We have seen instances where the compiler tried to emit a byte
19249 size attribute of -1 which ended up being encoded as an unsigned
19250 0xffffffff. Although 0xffffffff is technically a valid size value,
19251 an object of this size seems pretty unlikely so we can relatively
19252 safely treat these cases as if the size attribute was invalid and
19253 treat them as zero by default. */
19254 if (attr->name == DW_AT_byte_size
19255 && form == DW_FORM_data4
19256 && DW_UNSND (attr) >= 0xffffffff)
19257 {
19258 complaint
19259 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19260 hex_string (DW_UNSND (attr)));
19261 DW_UNSND (attr) = 0;
19262 }
19263
19264 return info_ptr;
19265 }
19266
19267 /* Read an attribute described by an abbreviated attribute. */
19268
19269 static const gdb_byte *
19270 read_attribute (const struct die_reader_specs *reader,
19271 struct attribute *attr, struct attr_abbrev *abbrev,
19272 const gdb_byte *info_ptr, bool *need_reprocess)
19273 {
19274 attr->name = abbrev->name;
19275 return read_attribute_value (reader, attr, abbrev->form,
19276 abbrev->implicit_const, info_ptr,
19277 need_reprocess);
19278 }
19279
19280 static CORE_ADDR
19281 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19282 unsigned int *bytes_read)
19283 {
19284 struct comp_unit_head *cu_header = &cu->header;
19285 CORE_ADDR retval = 0;
19286
19287 if (cu_header->signed_addr_p)
19288 {
19289 switch (cu_header->addr_size)
19290 {
19291 case 2:
19292 retval = bfd_get_signed_16 (abfd, buf);
19293 break;
19294 case 4:
19295 retval = bfd_get_signed_32 (abfd, buf);
19296 break;
19297 case 8:
19298 retval = bfd_get_signed_64 (abfd, buf);
19299 break;
19300 default:
19301 internal_error (__FILE__, __LINE__,
19302 _("read_address: bad switch, signed [in module %s]"),
19303 bfd_get_filename (abfd));
19304 }
19305 }
19306 else
19307 {
19308 switch (cu_header->addr_size)
19309 {
19310 case 2:
19311 retval = bfd_get_16 (abfd, buf);
19312 break;
19313 case 4:
19314 retval = bfd_get_32 (abfd, buf);
19315 break;
19316 case 8:
19317 retval = bfd_get_64 (abfd, buf);
19318 break;
19319 default:
19320 internal_error (__FILE__, __LINE__,
19321 _("read_address: bad switch, "
19322 "unsigned [in module %s]"),
19323 bfd_get_filename (abfd));
19324 }
19325 }
19326
19327 *bytes_read = cu_header->addr_size;
19328 return retval;
19329 }
19330
19331 /* Read the initial length from a section. The (draft) DWARF 3
19332 specification allows the initial length to take up either 4 bytes
19333 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19334 bytes describe the length and all offsets will be 8 bytes in length
19335 instead of 4.
19336
19337 An older, non-standard 64-bit format is also handled by this
19338 function. The older format in question stores the initial length
19339 as an 8-byte quantity without an escape value. Lengths greater
19340 than 2^32 aren't very common which means that the initial 4 bytes
19341 is almost always zero. Since a length value of zero doesn't make
19342 sense for the 32-bit format, this initial zero can be considered to
19343 be an escape value which indicates the presence of the older 64-bit
19344 format. As written, the code can't detect (old format) lengths
19345 greater than 4GB. If it becomes necessary to handle lengths
19346 somewhat larger than 4GB, we could allow other small values (such
19347 as the non-sensical values of 1, 2, and 3) to also be used as
19348 escape values indicating the presence of the old format.
19349
19350 The value returned via bytes_read should be used to increment the
19351 relevant pointer after calling read_initial_length().
19352
19353 [ Note: read_initial_length() and read_offset() are based on the
19354 document entitled "DWARF Debugging Information Format", revision
19355 3, draft 8, dated November 19, 2001. This document was obtained
19356 from:
19357
19358 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19359
19360 This document is only a draft and is subject to change. (So beware.)
19361
19362 Details regarding the older, non-standard 64-bit format were
19363 determined empirically by examining 64-bit ELF files produced by
19364 the SGI toolchain on an IRIX 6.5 machine.
19365
19366 - Kevin, July 16, 2002
19367 ] */
19368
19369 static LONGEST
19370 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19371 {
19372 LONGEST length = bfd_get_32 (abfd, buf);
19373
19374 if (length == 0xffffffff)
19375 {
19376 length = bfd_get_64 (abfd, buf + 4);
19377 *bytes_read = 12;
19378 }
19379 else if (length == 0)
19380 {
19381 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19382 length = bfd_get_64 (abfd, buf);
19383 *bytes_read = 8;
19384 }
19385 else
19386 {
19387 *bytes_read = 4;
19388 }
19389
19390 return length;
19391 }
19392
19393 /* Cover function for read_initial_length.
19394 Returns the length of the object at BUF, and stores the size of the
19395 initial length in *BYTES_READ and stores the size that offsets will be in
19396 *OFFSET_SIZE.
19397 If the initial length size is not equivalent to that specified in
19398 CU_HEADER then issue a complaint.
19399 This is useful when reading non-comp-unit headers. */
19400
19401 static LONGEST
19402 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19403 const struct comp_unit_head *cu_header,
19404 unsigned int *bytes_read,
19405 unsigned int *offset_size)
19406 {
19407 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19408
19409 gdb_assert (cu_header->initial_length_size == 4
19410 || cu_header->initial_length_size == 8
19411 || cu_header->initial_length_size == 12);
19412
19413 if (cu_header->initial_length_size != *bytes_read)
19414 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19415
19416 *offset_size = (*bytes_read == 4) ? 4 : 8;
19417 return length;
19418 }
19419
19420 /* Read an offset from the data stream. The size of the offset is
19421 given by cu_header->offset_size. */
19422
19423 static LONGEST
19424 read_offset (bfd *abfd, const gdb_byte *buf,
19425 const struct comp_unit_head *cu_header,
19426 unsigned int *bytes_read)
19427 {
19428 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19429
19430 *bytes_read = cu_header->offset_size;
19431 return offset;
19432 }
19433
19434 /* Read an offset from the data stream. */
19435
19436 static LONGEST
19437 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19438 {
19439 LONGEST retval = 0;
19440
19441 switch (offset_size)
19442 {
19443 case 4:
19444 retval = bfd_get_32 (abfd, buf);
19445 break;
19446 case 8:
19447 retval = bfd_get_64 (abfd, buf);
19448 break;
19449 default:
19450 internal_error (__FILE__, __LINE__,
19451 _("read_offset_1: bad switch [in module %s]"),
19452 bfd_get_filename (abfd));
19453 }
19454
19455 return retval;
19456 }
19457
19458 static const gdb_byte *
19459 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19460 {
19461 /* If the size of a host char is 8 bits, we can return a pointer
19462 to the buffer, otherwise we have to copy the data to a buffer
19463 allocated on the temporary obstack. */
19464 gdb_assert (HOST_CHAR_BIT == 8);
19465 return buf;
19466 }
19467
19468 static const char *
19469 read_direct_string (bfd *abfd, const gdb_byte *buf,
19470 unsigned int *bytes_read_ptr)
19471 {
19472 /* If the size of a host char is 8 bits, we can return a pointer
19473 to the string, otherwise we have to copy the string to a buffer
19474 allocated on the temporary obstack. */
19475 gdb_assert (HOST_CHAR_BIT == 8);
19476 if (*buf == '\0')
19477 {
19478 *bytes_read_ptr = 1;
19479 return NULL;
19480 }
19481 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19482 return (const char *) buf;
19483 }
19484
19485 /* Return pointer to string at section SECT offset STR_OFFSET with error
19486 reporting strings FORM_NAME and SECT_NAME. */
19487
19488 static const char *
19489 read_indirect_string_at_offset_from (struct objfile *objfile,
19490 bfd *abfd, LONGEST str_offset,
19491 struct dwarf2_section_info *sect,
19492 const char *form_name,
19493 const char *sect_name)
19494 {
19495 sect->read (objfile);
19496 if (sect->buffer == NULL)
19497 error (_("%s used without %s section [in module %s]"),
19498 form_name, sect_name, bfd_get_filename (abfd));
19499 if (str_offset >= sect->size)
19500 error (_("%s pointing outside of %s section [in module %s]"),
19501 form_name, sect_name, bfd_get_filename (abfd));
19502 gdb_assert (HOST_CHAR_BIT == 8);
19503 if (sect->buffer[str_offset] == '\0')
19504 return NULL;
19505 return (const char *) (sect->buffer + str_offset);
19506 }
19507
19508 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19509
19510 static const char *
19511 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19512 bfd *abfd, LONGEST str_offset)
19513 {
19514 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19515 abfd, str_offset,
19516 &dwarf2_per_objfile->str,
19517 "DW_FORM_strp", ".debug_str");
19518 }
19519
19520 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19521
19522 static const char *
19523 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19524 bfd *abfd, LONGEST str_offset)
19525 {
19526 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19527 abfd, str_offset,
19528 &dwarf2_per_objfile->line_str,
19529 "DW_FORM_line_strp",
19530 ".debug_line_str");
19531 }
19532
19533 /* Read a string at offset STR_OFFSET in the .debug_str section from
19534 the .dwz file DWZ. Throw an error if the offset is too large. If
19535 the string consists of a single NUL byte, return NULL; otherwise
19536 return a pointer to the string. */
19537
19538 static const char *
19539 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19540 LONGEST str_offset)
19541 {
19542 dwz->str.read (objfile);
19543
19544 if (dwz->str.buffer == NULL)
19545 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19546 "section [in module %s]"),
19547 bfd_get_filename (dwz->dwz_bfd.get ()));
19548 if (str_offset >= dwz->str.size)
19549 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19550 ".debug_str section [in module %s]"),
19551 bfd_get_filename (dwz->dwz_bfd.get ()));
19552 gdb_assert (HOST_CHAR_BIT == 8);
19553 if (dwz->str.buffer[str_offset] == '\0')
19554 return NULL;
19555 return (const char *) (dwz->str.buffer + str_offset);
19556 }
19557
19558 /* Return pointer to string at .debug_str offset as read from BUF.
19559 BUF is assumed to be in a compilation unit described by CU_HEADER.
19560 Return *BYTES_READ_PTR count of bytes read from BUF. */
19561
19562 static const char *
19563 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19564 const gdb_byte *buf,
19565 const struct comp_unit_head *cu_header,
19566 unsigned int *bytes_read_ptr)
19567 {
19568 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19569
19570 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19571 }
19572
19573 /* Return pointer to string at .debug_line_str offset as read from BUF.
19574 BUF is assumed to be in a compilation unit described by CU_HEADER.
19575 Return *BYTES_READ_PTR count of bytes read from BUF. */
19576
19577 static const char *
19578 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19579 bfd *abfd, const gdb_byte *buf,
19580 const struct comp_unit_head *cu_header,
19581 unsigned int *bytes_read_ptr)
19582 {
19583 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19584
19585 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19586 str_offset);
19587 }
19588
19589 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19590 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19591 ADDR_SIZE is the size of addresses from the CU header. */
19592
19593 static CORE_ADDR
19594 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19595 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19596 int addr_size)
19597 {
19598 struct objfile *objfile = dwarf2_per_objfile->objfile;
19599 bfd *abfd = objfile->obfd;
19600 const gdb_byte *info_ptr;
19601 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19602
19603 dwarf2_per_objfile->addr.read (objfile);
19604 if (dwarf2_per_objfile->addr.buffer == NULL)
19605 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19606 objfile_name (objfile));
19607 if (addr_base_or_zero + addr_index * addr_size
19608 >= dwarf2_per_objfile->addr.size)
19609 error (_("DW_FORM_addr_index pointing outside of "
19610 ".debug_addr section [in module %s]"),
19611 objfile_name (objfile));
19612 info_ptr = (dwarf2_per_objfile->addr.buffer
19613 + addr_base_or_zero + addr_index * addr_size);
19614 if (addr_size == 4)
19615 return bfd_get_32 (abfd, info_ptr);
19616 else
19617 return bfd_get_64 (abfd, info_ptr);
19618 }
19619
19620 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19621
19622 static CORE_ADDR
19623 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19624 {
19625 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19626 cu->addr_base, cu->header.addr_size);
19627 }
19628
19629 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19630
19631 static CORE_ADDR
19632 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19633 unsigned int *bytes_read)
19634 {
19635 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19636 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19637
19638 return read_addr_index (cu, addr_index);
19639 }
19640
19641 /* Given an index in .debug_addr, fetch the value.
19642 NOTE: This can be called during dwarf expression evaluation,
19643 long after the debug information has been read, and thus per_cu->cu
19644 may no longer exist. */
19645
19646 CORE_ADDR
19647 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19648 unsigned int addr_index)
19649 {
19650 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19651 struct dwarf2_cu *cu = per_cu->cu;
19652 gdb::optional<ULONGEST> addr_base;
19653 int addr_size;
19654
19655 /* We need addr_base and addr_size.
19656 If we don't have PER_CU->cu, we have to get it.
19657 Nasty, but the alternative is storing the needed info in PER_CU,
19658 which at this point doesn't seem justified: it's not clear how frequently
19659 it would get used and it would increase the size of every PER_CU.
19660 Entry points like dwarf2_per_cu_addr_size do a similar thing
19661 so we're not in uncharted territory here.
19662 Alas we need to be a bit more complicated as addr_base is contained
19663 in the DIE.
19664
19665 We don't need to read the entire CU(/TU).
19666 We just need the header and top level die.
19667
19668 IWBN to use the aging mechanism to let us lazily later discard the CU.
19669 For now we skip this optimization. */
19670
19671 if (cu != NULL)
19672 {
19673 addr_base = cu->addr_base;
19674 addr_size = cu->header.addr_size;
19675 }
19676 else
19677 {
19678 cutu_reader reader (per_cu, NULL, 0, 0, false);
19679 addr_base = reader.cu->addr_base;
19680 addr_size = reader.cu->header.addr_size;
19681 }
19682
19683 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19684 addr_size);
19685 }
19686
19687 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19688 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19689 DWO file. */
19690
19691 static const char *
19692 read_str_index (struct dwarf2_cu *cu,
19693 struct dwarf2_section_info *str_section,
19694 struct dwarf2_section_info *str_offsets_section,
19695 ULONGEST str_offsets_base, ULONGEST str_index)
19696 {
19697 struct dwarf2_per_objfile *dwarf2_per_objfile
19698 = cu->per_cu->dwarf2_per_objfile;
19699 struct objfile *objfile = dwarf2_per_objfile->objfile;
19700 const char *objf_name = objfile_name (objfile);
19701 bfd *abfd = objfile->obfd;
19702 const gdb_byte *info_ptr;
19703 ULONGEST str_offset;
19704 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19705
19706 str_section->read (objfile);
19707 str_offsets_section->read (objfile);
19708 if (str_section->buffer == NULL)
19709 error (_("%s used without %s section"
19710 " in CU at offset %s [in module %s]"),
19711 form_name, str_section->get_name (),
19712 sect_offset_str (cu->header.sect_off), objf_name);
19713 if (str_offsets_section->buffer == NULL)
19714 error (_("%s used without %s section"
19715 " in CU at offset %s [in module %s]"),
19716 form_name, str_section->get_name (),
19717 sect_offset_str (cu->header.sect_off), objf_name);
19718 info_ptr = (str_offsets_section->buffer
19719 + str_offsets_base
19720 + str_index * cu->header.offset_size);
19721 if (cu->header.offset_size == 4)
19722 str_offset = bfd_get_32 (abfd, info_ptr);
19723 else
19724 str_offset = bfd_get_64 (abfd, info_ptr);
19725 if (str_offset >= str_section->size)
19726 error (_("Offset from %s pointing outside of"
19727 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19728 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19729 return (const char *) (str_section->buffer + str_offset);
19730 }
19731
19732 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19733
19734 static const char *
19735 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19736 {
19737 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19738 ? reader->cu->header.addr_size : 0;
19739 return read_str_index (reader->cu,
19740 &reader->dwo_file->sections.str,
19741 &reader->dwo_file->sections.str_offsets,
19742 str_offsets_base, str_index);
19743 }
19744
19745 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19746
19747 static const char *
19748 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19749 {
19750 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19751 const char *objf_name = objfile_name (objfile);
19752 static const char form_name[] = "DW_FORM_GNU_str_index";
19753 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19754
19755 if (!cu->str_offsets_base.has_value ())
19756 error (_("%s used in Fission stub without %s"
19757 " in CU at offset 0x%lx [in module %s]"),
19758 form_name, str_offsets_attr_name,
19759 (long) cu->header.offset_size, objf_name);
19760
19761 return read_str_index (cu,
19762 &cu->per_cu->dwarf2_per_objfile->str,
19763 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19764 *cu->str_offsets_base, str_index);
19765 }
19766
19767 /* Return the length of an LEB128 number in BUF. */
19768
19769 static int
19770 leb128_size (const gdb_byte *buf)
19771 {
19772 const gdb_byte *begin = buf;
19773 gdb_byte byte;
19774
19775 while (1)
19776 {
19777 byte = *buf++;
19778 if ((byte & 128) == 0)
19779 return buf - begin;
19780 }
19781 }
19782
19783 static void
19784 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19785 {
19786 switch (lang)
19787 {
19788 case DW_LANG_C89:
19789 case DW_LANG_C99:
19790 case DW_LANG_C11:
19791 case DW_LANG_C:
19792 case DW_LANG_UPC:
19793 cu->language = language_c;
19794 break;
19795 case DW_LANG_Java:
19796 case DW_LANG_C_plus_plus:
19797 case DW_LANG_C_plus_plus_11:
19798 case DW_LANG_C_plus_plus_14:
19799 cu->language = language_cplus;
19800 break;
19801 case DW_LANG_D:
19802 cu->language = language_d;
19803 break;
19804 case DW_LANG_Fortran77:
19805 case DW_LANG_Fortran90:
19806 case DW_LANG_Fortran95:
19807 case DW_LANG_Fortran03:
19808 case DW_LANG_Fortran08:
19809 cu->language = language_fortran;
19810 break;
19811 case DW_LANG_Go:
19812 cu->language = language_go;
19813 break;
19814 case DW_LANG_Mips_Assembler:
19815 cu->language = language_asm;
19816 break;
19817 case DW_LANG_Ada83:
19818 case DW_LANG_Ada95:
19819 cu->language = language_ada;
19820 break;
19821 case DW_LANG_Modula2:
19822 cu->language = language_m2;
19823 break;
19824 case DW_LANG_Pascal83:
19825 cu->language = language_pascal;
19826 break;
19827 case DW_LANG_ObjC:
19828 cu->language = language_objc;
19829 break;
19830 case DW_LANG_Rust:
19831 case DW_LANG_Rust_old:
19832 cu->language = language_rust;
19833 break;
19834 case DW_LANG_Cobol74:
19835 case DW_LANG_Cobol85:
19836 default:
19837 cu->language = language_minimal;
19838 break;
19839 }
19840 cu->language_defn = language_def (cu->language);
19841 }
19842
19843 /* Return the named attribute or NULL if not there. */
19844
19845 static struct attribute *
19846 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19847 {
19848 for (;;)
19849 {
19850 unsigned int i;
19851 struct attribute *spec = NULL;
19852
19853 for (i = 0; i < die->num_attrs; ++i)
19854 {
19855 if (die->attrs[i].name == name)
19856 return &die->attrs[i];
19857 if (die->attrs[i].name == DW_AT_specification
19858 || die->attrs[i].name == DW_AT_abstract_origin)
19859 spec = &die->attrs[i];
19860 }
19861
19862 if (!spec)
19863 break;
19864
19865 die = follow_die_ref (die, spec, &cu);
19866 }
19867
19868 return NULL;
19869 }
19870
19871 /* Return the named attribute or NULL if not there,
19872 but do not follow DW_AT_specification, etc.
19873 This is for use in contexts where we're reading .debug_types dies.
19874 Following DW_AT_specification, DW_AT_abstract_origin will take us
19875 back up the chain, and we want to go down. */
19876
19877 static struct attribute *
19878 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19879 {
19880 unsigned int i;
19881
19882 for (i = 0; i < die->num_attrs; ++i)
19883 if (die->attrs[i].name == name)
19884 return &die->attrs[i];
19885
19886 return NULL;
19887 }
19888
19889 /* Return the string associated with a string-typed attribute, or NULL if it
19890 is either not found or is of an incorrect type. */
19891
19892 static const char *
19893 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19894 {
19895 struct attribute *attr;
19896 const char *str = NULL;
19897
19898 attr = dwarf2_attr (die, name, cu);
19899
19900 if (attr != NULL)
19901 {
19902 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19903 || attr->form == DW_FORM_string
19904 || attr->form == DW_FORM_strx
19905 || attr->form == DW_FORM_strx1
19906 || attr->form == DW_FORM_strx2
19907 || attr->form == DW_FORM_strx3
19908 || attr->form == DW_FORM_strx4
19909 || attr->form == DW_FORM_GNU_str_index
19910 || attr->form == DW_FORM_GNU_strp_alt)
19911 str = DW_STRING (attr);
19912 else
19913 complaint (_("string type expected for attribute %s for "
19914 "DIE at %s in module %s"),
19915 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19916 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19917 }
19918
19919 return str;
19920 }
19921
19922 /* Return the dwo name or NULL if not present. If present, it is in either
19923 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19924 static const char *
19925 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19926 {
19927 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19928 if (dwo_name == nullptr)
19929 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19930 return dwo_name;
19931 }
19932
19933 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19934 and holds a non-zero value. This function should only be used for
19935 DW_FORM_flag or DW_FORM_flag_present attributes. */
19936
19937 static int
19938 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19939 {
19940 struct attribute *attr = dwarf2_attr (die, name, cu);
19941
19942 return (attr && DW_UNSND (attr));
19943 }
19944
19945 static int
19946 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19947 {
19948 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19949 which value is non-zero. However, we have to be careful with
19950 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19951 (via dwarf2_flag_true_p) follows this attribute. So we may
19952 end up accidently finding a declaration attribute that belongs
19953 to a different DIE referenced by the specification attribute,
19954 even though the given DIE does not have a declaration attribute. */
19955 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19956 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19957 }
19958
19959 /* Return the die giving the specification for DIE, if there is
19960 one. *SPEC_CU is the CU containing DIE on input, and the CU
19961 containing the return value on output. If there is no
19962 specification, but there is an abstract origin, that is
19963 returned. */
19964
19965 static struct die_info *
19966 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19967 {
19968 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19969 *spec_cu);
19970
19971 if (spec_attr == NULL)
19972 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19973
19974 if (spec_attr == NULL)
19975 return NULL;
19976 else
19977 return follow_die_ref (die, spec_attr, spec_cu);
19978 }
19979
19980 /* Stub for free_line_header to match void * callback types. */
19981
19982 static void
19983 free_line_header_voidp (void *arg)
19984 {
19985 struct line_header *lh = (struct line_header *) arg;
19986
19987 delete lh;
19988 }
19989
19990 void
19991 line_header::add_include_dir (const char *include_dir)
19992 {
19993 if (dwarf_line_debug >= 2)
19994 {
19995 size_t new_size;
19996 if (version >= 5)
19997 new_size = m_include_dirs.size ();
19998 else
19999 new_size = m_include_dirs.size () + 1;
20000 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20001 new_size, include_dir);
20002 }
20003 m_include_dirs.push_back (include_dir);
20004 }
20005
20006 void
20007 line_header::add_file_name (const char *name,
20008 dir_index d_index,
20009 unsigned int mod_time,
20010 unsigned int length)
20011 {
20012 if (dwarf_line_debug >= 2)
20013 {
20014 size_t new_size;
20015 if (version >= 5)
20016 new_size = file_names_size ();
20017 else
20018 new_size = file_names_size () + 1;
20019 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20020 new_size, name);
20021 }
20022 m_file_names.emplace_back (name, d_index, mod_time, length);
20023 }
20024
20025 /* A convenience function to find the proper .debug_line section for a CU. */
20026
20027 static struct dwarf2_section_info *
20028 get_debug_line_section (struct dwarf2_cu *cu)
20029 {
20030 struct dwarf2_section_info *section;
20031 struct dwarf2_per_objfile *dwarf2_per_objfile
20032 = cu->per_cu->dwarf2_per_objfile;
20033
20034 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20035 DWO file. */
20036 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20037 section = &cu->dwo_unit->dwo_file->sections.line;
20038 else if (cu->per_cu->is_dwz)
20039 {
20040 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20041
20042 section = &dwz->line;
20043 }
20044 else
20045 section = &dwarf2_per_objfile->line;
20046
20047 return section;
20048 }
20049
20050 /* Read directory or file name entry format, starting with byte of
20051 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20052 entries count and the entries themselves in the described entry
20053 format. */
20054
20055 static void
20056 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20057 bfd *abfd, const gdb_byte **bufp,
20058 struct line_header *lh,
20059 const struct comp_unit_head *cu_header,
20060 void (*callback) (struct line_header *lh,
20061 const char *name,
20062 dir_index d_index,
20063 unsigned int mod_time,
20064 unsigned int length))
20065 {
20066 gdb_byte format_count, formati;
20067 ULONGEST data_count, datai;
20068 const gdb_byte *buf = *bufp;
20069 const gdb_byte *format_header_data;
20070 unsigned int bytes_read;
20071
20072 format_count = read_1_byte (abfd, buf);
20073 buf += 1;
20074 format_header_data = buf;
20075 for (formati = 0; formati < format_count; formati++)
20076 {
20077 read_unsigned_leb128 (abfd, buf, &bytes_read);
20078 buf += bytes_read;
20079 read_unsigned_leb128 (abfd, buf, &bytes_read);
20080 buf += bytes_read;
20081 }
20082
20083 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20084 buf += bytes_read;
20085 for (datai = 0; datai < data_count; datai++)
20086 {
20087 const gdb_byte *format = format_header_data;
20088 struct file_entry fe;
20089
20090 for (formati = 0; formati < format_count; formati++)
20091 {
20092 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20093 format += bytes_read;
20094
20095 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20096 format += bytes_read;
20097
20098 gdb::optional<const char *> string;
20099 gdb::optional<unsigned int> uint;
20100
20101 switch (form)
20102 {
20103 case DW_FORM_string:
20104 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20105 buf += bytes_read;
20106 break;
20107
20108 case DW_FORM_line_strp:
20109 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20110 abfd, buf,
20111 cu_header,
20112 &bytes_read));
20113 buf += bytes_read;
20114 break;
20115
20116 case DW_FORM_data1:
20117 uint.emplace (read_1_byte (abfd, buf));
20118 buf += 1;
20119 break;
20120
20121 case DW_FORM_data2:
20122 uint.emplace (read_2_bytes (abfd, buf));
20123 buf += 2;
20124 break;
20125
20126 case DW_FORM_data4:
20127 uint.emplace (read_4_bytes (abfd, buf));
20128 buf += 4;
20129 break;
20130
20131 case DW_FORM_data8:
20132 uint.emplace (read_8_bytes (abfd, buf));
20133 buf += 8;
20134 break;
20135
20136 case DW_FORM_data16:
20137 /* This is used for MD5, but file_entry does not record MD5s. */
20138 buf += 16;
20139 break;
20140
20141 case DW_FORM_udata:
20142 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20143 buf += bytes_read;
20144 break;
20145
20146 case DW_FORM_block:
20147 /* It is valid only for DW_LNCT_timestamp which is ignored by
20148 current GDB. */
20149 break;
20150 }
20151
20152 switch (content_type)
20153 {
20154 case DW_LNCT_path:
20155 if (string.has_value ())
20156 fe.name = *string;
20157 break;
20158 case DW_LNCT_directory_index:
20159 if (uint.has_value ())
20160 fe.d_index = (dir_index) *uint;
20161 break;
20162 case DW_LNCT_timestamp:
20163 if (uint.has_value ())
20164 fe.mod_time = *uint;
20165 break;
20166 case DW_LNCT_size:
20167 if (uint.has_value ())
20168 fe.length = *uint;
20169 break;
20170 case DW_LNCT_MD5:
20171 break;
20172 default:
20173 complaint (_("Unknown format content type %s"),
20174 pulongest (content_type));
20175 }
20176 }
20177
20178 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20179 }
20180
20181 *bufp = buf;
20182 }
20183
20184 /* Read the statement program header starting at OFFSET in
20185 .debug_line, or .debug_line.dwo. Return a pointer
20186 to a struct line_header, allocated using xmalloc.
20187 Returns NULL if there is a problem reading the header, e.g., if it
20188 has a version we don't understand.
20189
20190 NOTE: the strings in the include directory and file name tables of
20191 the returned object point into the dwarf line section buffer,
20192 and must not be freed. */
20193
20194 static line_header_up
20195 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20196 {
20197 const gdb_byte *line_ptr;
20198 unsigned int bytes_read, offset_size;
20199 int i;
20200 const char *cur_dir, *cur_file;
20201 struct dwarf2_section_info *section;
20202 bfd *abfd;
20203 struct dwarf2_per_objfile *dwarf2_per_objfile
20204 = cu->per_cu->dwarf2_per_objfile;
20205
20206 section = get_debug_line_section (cu);
20207 section->read (dwarf2_per_objfile->objfile);
20208 if (section->buffer == NULL)
20209 {
20210 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20211 complaint (_("missing .debug_line.dwo section"));
20212 else
20213 complaint (_("missing .debug_line section"));
20214 return 0;
20215 }
20216
20217 /* We can't do this until we know the section is non-empty.
20218 Only then do we know we have such a section. */
20219 abfd = section->get_bfd_owner ();
20220
20221 /* Make sure that at least there's room for the total_length field.
20222 That could be 12 bytes long, but we're just going to fudge that. */
20223 if (to_underlying (sect_off) + 4 >= section->size)
20224 {
20225 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20226 return 0;
20227 }
20228
20229 line_header_up lh (new line_header ());
20230
20231 lh->sect_off = sect_off;
20232 lh->offset_in_dwz = cu->per_cu->is_dwz;
20233
20234 line_ptr = section->buffer + to_underlying (sect_off);
20235
20236 /* Read in the header. */
20237 lh->total_length =
20238 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20239 &bytes_read, &offset_size);
20240 line_ptr += bytes_read;
20241
20242 const gdb_byte *start_here = line_ptr;
20243
20244 if (line_ptr + lh->total_length > (section->buffer + section->size))
20245 {
20246 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20247 return 0;
20248 }
20249 lh->statement_program_end = start_here + lh->total_length;
20250 lh->version = read_2_bytes (abfd, line_ptr);
20251 line_ptr += 2;
20252 if (lh->version > 5)
20253 {
20254 /* This is a version we don't understand. The format could have
20255 changed in ways we don't handle properly so just punt. */
20256 complaint (_("unsupported version in .debug_line section"));
20257 return NULL;
20258 }
20259 if (lh->version >= 5)
20260 {
20261 gdb_byte segment_selector_size;
20262
20263 /* Skip address size. */
20264 read_1_byte (abfd, line_ptr);
20265 line_ptr += 1;
20266
20267 segment_selector_size = read_1_byte (abfd, line_ptr);
20268 line_ptr += 1;
20269 if (segment_selector_size != 0)
20270 {
20271 complaint (_("unsupported segment selector size %u "
20272 "in .debug_line section"),
20273 segment_selector_size);
20274 return NULL;
20275 }
20276 }
20277 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20278 line_ptr += offset_size;
20279 lh->statement_program_start = line_ptr + lh->header_length;
20280 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20281 line_ptr += 1;
20282 if (lh->version >= 4)
20283 {
20284 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20285 line_ptr += 1;
20286 }
20287 else
20288 lh->maximum_ops_per_instruction = 1;
20289
20290 if (lh->maximum_ops_per_instruction == 0)
20291 {
20292 lh->maximum_ops_per_instruction = 1;
20293 complaint (_("invalid maximum_ops_per_instruction "
20294 "in `.debug_line' section"));
20295 }
20296
20297 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20298 line_ptr += 1;
20299 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20300 line_ptr += 1;
20301 lh->line_range = read_1_byte (abfd, line_ptr);
20302 line_ptr += 1;
20303 lh->opcode_base = read_1_byte (abfd, line_ptr);
20304 line_ptr += 1;
20305 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20306
20307 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20308 for (i = 1; i < lh->opcode_base; ++i)
20309 {
20310 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20311 line_ptr += 1;
20312 }
20313
20314 if (lh->version >= 5)
20315 {
20316 /* Read directory table. */
20317 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20318 &cu->header,
20319 [] (struct line_header *header, const char *name,
20320 dir_index d_index, unsigned int mod_time,
20321 unsigned int length)
20322 {
20323 header->add_include_dir (name);
20324 });
20325
20326 /* Read file name table. */
20327 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20328 &cu->header,
20329 [] (struct line_header *header, const char *name,
20330 dir_index d_index, unsigned int mod_time,
20331 unsigned int length)
20332 {
20333 header->add_file_name (name, d_index, mod_time, length);
20334 });
20335 }
20336 else
20337 {
20338 /* Read directory table. */
20339 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20340 {
20341 line_ptr += bytes_read;
20342 lh->add_include_dir (cur_dir);
20343 }
20344 line_ptr += bytes_read;
20345
20346 /* Read file name table. */
20347 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20348 {
20349 unsigned int mod_time, length;
20350 dir_index d_index;
20351
20352 line_ptr += bytes_read;
20353 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20354 line_ptr += bytes_read;
20355 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20356 line_ptr += bytes_read;
20357 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20358 line_ptr += bytes_read;
20359
20360 lh->add_file_name (cur_file, d_index, mod_time, length);
20361 }
20362 line_ptr += bytes_read;
20363 }
20364
20365 if (line_ptr > (section->buffer + section->size))
20366 complaint (_("line number info header doesn't "
20367 "fit in `.debug_line' section"));
20368
20369 return lh;
20370 }
20371
20372 /* Subroutine of dwarf_decode_lines to simplify it.
20373 Return the file name of the psymtab for the given file_entry.
20374 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20375 If space for the result is malloc'd, *NAME_HOLDER will be set.
20376 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20377
20378 static const char *
20379 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20380 const dwarf2_psymtab *pst,
20381 const char *comp_dir,
20382 gdb::unique_xmalloc_ptr<char> *name_holder)
20383 {
20384 const char *include_name = fe.name;
20385 const char *include_name_to_compare = include_name;
20386 const char *pst_filename;
20387 int file_is_pst;
20388
20389 const char *dir_name = fe.include_dir (lh);
20390
20391 gdb::unique_xmalloc_ptr<char> hold_compare;
20392 if (!IS_ABSOLUTE_PATH (include_name)
20393 && (dir_name != NULL || comp_dir != NULL))
20394 {
20395 /* Avoid creating a duplicate psymtab for PST.
20396 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20397 Before we do the comparison, however, we need to account
20398 for DIR_NAME and COMP_DIR.
20399 First prepend dir_name (if non-NULL). If we still don't
20400 have an absolute path prepend comp_dir (if non-NULL).
20401 However, the directory we record in the include-file's
20402 psymtab does not contain COMP_DIR (to match the
20403 corresponding symtab(s)).
20404
20405 Example:
20406
20407 bash$ cd /tmp
20408 bash$ gcc -g ./hello.c
20409 include_name = "hello.c"
20410 dir_name = "."
20411 DW_AT_comp_dir = comp_dir = "/tmp"
20412 DW_AT_name = "./hello.c"
20413
20414 */
20415
20416 if (dir_name != NULL)
20417 {
20418 name_holder->reset (concat (dir_name, SLASH_STRING,
20419 include_name, (char *) NULL));
20420 include_name = name_holder->get ();
20421 include_name_to_compare = include_name;
20422 }
20423 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20424 {
20425 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20426 include_name, (char *) NULL));
20427 include_name_to_compare = hold_compare.get ();
20428 }
20429 }
20430
20431 pst_filename = pst->filename;
20432 gdb::unique_xmalloc_ptr<char> copied_name;
20433 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20434 {
20435 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20436 pst_filename, (char *) NULL));
20437 pst_filename = copied_name.get ();
20438 }
20439
20440 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20441
20442 if (file_is_pst)
20443 return NULL;
20444 return include_name;
20445 }
20446
20447 /* State machine to track the state of the line number program. */
20448
20449 class lnp_state_machine
20450 {
20451 public:
20452 /* Initialize a machine state for the start of a line number
20453 program. */
20454 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20455 bool record_lines_p);
20456
20457 file_entry *current_file ()
20458 {
20459 /* lh->file_names is 0-based, but the file name numbers in the
20460 statement program are 1-based. */
20461 return m_line_header->file_name_at (m_file);
20462 }
20463
20464 /* Record the line in the state machine. END_SEQUENCE is true if
20465 we're processing the end of a sequence. */
20466 void record_line (bool end_sequence);
20467
20468 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20469 nop-out rest of the lines in this sequence. */
20470 void check_line_address (struct dwarf2_cu *cu,
20471 const gdb_byte *line_ptr,
20472 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20473
20474 void handle_set_discriminator (unsigned int discriminator)
20475 {
20476 m_discriminator = discriminator;
20477 m_line_has_non_zero_discriminator |= discriminator != 0;
20478 }
20479
20480 /* Handle DW_LNE_set_address. */
20481 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20482 {
20483 m_op_index = 0;
20484 address += baseaddr;
20485 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20486 }
20487
20488 /* Handle DW_LNS_advance_pc. */
20489 void handle_advance_pc (CORE_ADDR adjust);
20490
20491 /* Handle a special opcode. */
20492 void handle_special_opcode (unsigned char op_code);
20493
20494 /* Handle DW_LNS_advance_line. */
20495 void handle_advance_line (int line_delta)
20496 {
20497 advance_line (line_delta);
20498 }
20499
20500 /* Handle DW_LNS_set_file. */
20501 void handle_set_file (file_name_index file);
20502
20503 /* Handle DW_LNS_negate_stmt. */
20504 void handle_negate_stmt ()
20505 {
20506 m_is_stmt = !m_is_stmt;
20507 }
20508
20509 /* Handle DW_LNS_const_add_pc. */
20510 void handle_const_add_pc ();
20511
20512 /* Handle DW_LNS_fixed_advance_pc. */
20513 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20514 {
20515 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20516 m_op_index = 0;
20517 }
20518
20519 /* Handle DW_LNS_copy. */
20520 void handle_copy ()
20521 {
20522 record_line (false);
20523 m_discriminator = 0;
20524 }
20525
20526 /* Handle DW_LNE_end_sequence. */
20527 void handle_end_sequence ()
20528 {
20529 m_currently_recording_lines = true;
20530 }
20531
20532 private:
20533 /* Advance the line by LINE_DELTA. */
20534 void advance_line (int line_delta)
20535 {
20536 m_line += line_delta;
20537
20538 if (line_delta != 0)
20539 m_line_has_non_zero_discriminator = m_discriminator != 0;
20540 }
20541
20542 struct dwarf2_cu *m_cu;
20543
20544 gdbarch *m_gdbarch;
20545
20546 /* True if we're recording lines.
20547 Otherwise we're building partial symtabs and are just interested in
20548 finding include files mentioned by the line number program. */
20549 bool m_record_lines_p;
20550
20551 /* The line number header. */
20552 line_header *m_line_header;
20553
20554 /* These are part of the standard DWARF line number state machine,
20555 and initialized according to the DWARF spec. */
20556
20557 unsigned char m_op_index = 0;
20558 /* The line table index of the current file. */
20559 file_name_index m_file = 1;
20560 unsigned int m_line = 1;
20561
20562 /* These are initialized in the constructor. */
20563
20564 CORE_ADDR m_address;
20565 bool m_is_stmt;
20566 unsigned int m_discriminator;
20567
20568 /* Additional bits of state we need to track. */
20569
20570 /* The last file that we called dwarf2_start_subfile for.
20571 This is only used for TLLs. */
20572 unsigned int m_last_file = 0;
20573 /* The last file a line number was recorded for. */
20574 struct subfile *m_last_subfile = NULL;
20575
20576 /* When true, record the lines we decode. */
20577 bool m_currently_recording_lines = false;
20578
20579 /* The last line number that was recorded, used to coalesce
20580 consecutive entries for the same line. This can happen, for
20581 example, when discriminators are present. PR 17276. */
20582 unsigned int m_last_line = 0;
20583 bool m_line_has_non_zero_discriminator = false;
20584 };
20585
20586 void
20587 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20588 {
20589 CORE_ADDR addr_adj = (((m_op_index + adjust)
20590 / m_line_header->maximum_ops_per_instruction)
20591 * m_line_header->minimum_instruction_length);
20592 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20593 m_op_index = ((m_op_index + adjust)
20594 % m_line_header->maximum_ops_per_instruction);
20595 }
20596
20597 void
20598 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20599 {
20600 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20601 CORE_ADDR addr_adj = (((m_op_index
20602 + (adj_opcode / m_line_header->line_range))
20603 / m_line_header->maximum_ops_per_instruction)
20604 * m_line_header->minimum_instruction_length);
20605 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20606 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20607 % m_line_header->maximum_ops_per_instruction);
20608
20609 int line_delta = (m_line_header->line_base
20610 + (adj_opcode % m_line_header->line_range));
20611 advance_line (line_delta);
20612 record_line (false);
20613 m_discriminator = 0;
20614 }
20615
20616 void
20617 lnp_state_machine::handle_set_file (file_name_index file)
20618 {
20619 m_file = file;
20620
20621 const file_entry *fe = current_file ();
20622 if (fe == NULL)
20623 dwarf2_debug_line_missing_file_complaint ();
20624 else if (m_record_lines_p)
20625 {
20626 const char *dir = fe->include_dir (m_line_header);
20627
20628 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20629 m_line_has_non_zero_discriminator = m_discriminator != 0;
20630 dwarf2_start_subfile (m_cu, fe->name, dir);
20631 }
20632 }
20633
20634 void
20635 lnp_state_machine::handle_const_add_pc ()
20636 {
20637 CORE_ADDR adjust
20638 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20639
20640 CORE_ADDR addr_adj
20641 = (((m_op_index + adjust)
20642 / m_line_header->maximum_ops_per_instruction)
20643 * m_line_header->minimum_instruction_length);
20644
20645 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20646 m_op_index = ((m_op_index + adjust)
20647 % m_line_header->maximum_ops_per_instruction);
20648 }
20649
20650 /* Return non-zero if we should add LINE to the line number table.
20651 LINE is the line to add, LAST_LINE is the last line that was added,
20652 LAST_SUBFILE is the subfile for LAST_LINE.
20653 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20654 had a non-zero discriminator.
20655
20656 We have to be careful in the presence of discriminators.
20657 E.g., for this line:
20658
20659 for (i = 0; i < 100000; i++);
20660
20661 clang can emit four line number entries for that one line,
20662 each with a different discriminator.
20663 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20664
20665 However, we want gdb to coalesce all four entries into one.
20666 Otherwise the user could stepi into the middle of the line and
20667 gdb would get confused about whether the pc really was in the
20668 middle of the line.
20669
20670 Things are further complicated by the fact that two consecutive
20671 line number entries for the same line is a heuristic used by gcc
20672 to denote the end of the prologue. So we can't just discard duplicate
20673 entries, we have to be selective about it. The heuristic we use is
20674 that we only collapse consecutive entries for the same line if at least
20675 one of those entries has a non-zero discriminator. PR 17276.
20676
20677 Note: Addresses in the line number state machine can never go backwards
20678 within one sequence, thus this coalescing is ok. */
20679
20680 static int
20681 dwarf_record_line_p (struct dwarf2_cu *cu,
20682 unsigned int line, unsigned int last_line,
20683 int line_has_non_zero_discriminator,
20684 struct subfile *last_subfile)
20685 {
20686 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20687 return 1;
20688 if (line != last_line)
20689 return 1;
20690 /* Same line for the same file that we've seen already.
20691 As a last check, for pr 17276, only record the line if the line
20692 has never had a non-zero discriminator. */
20693 if (!line_has_non_zero_discriminator)
20694 return 1;
20695 return 0;
20696 }
20697
20698 /* Use the CU's builder to record line number LINE beginning at
20699 address ADDRESS in the line table of subfile SUBFILE. */
20700
20701 static void
20702 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20703 unsigned int line, CORE_ADDR address,
20704 struct dwarf2_cu *cu)
20705 {
20706 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20707
20708 if (dwarf_line_debug)
20709 {
20710 fprintf_unfiltered (gdb_stdlog,
20711 "Recording line %u, file %s, address %s\n",
20712 line, lbasename (subfile->name),
20713 paddress (gdbarch, address));
20714 }
20715
20716 if (cu != nullptr)
20717 cu->get_builder ()->record_line (subfile, line, addr);
20718 }
20719
20720 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20721 Mark the end of a set of line number records.
20722 The arguments are the same as for dwarf_record_line_1.
20723 If SUBFILE is NULL the request is ignored. */
20724
20725 static void
20726 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20727 CORE_ADDR address, struct dwarf2_cu *cu)
20728 {
20729 if (subfile == NULL)
20730 return;
20731
20732 if (dwarf_line_debug)
20733 {
20734 fprintf_unfiltered (gdb_stdlog,
20735 "Finishing current line, file %s, address %s\n",
20736 lbasename (subfile->name),
20737 paddress (gdbarch, address));
20738 }
20739
20740 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20741 }
20742
20743 void
20744 lnp_state_machine::record_line (bool end_sequence)
20745 {
20746 if (dwarf_line_debug)
20747 {
20748 fprintf_unfiltered (gdb_stdlog,
20749 "Processing actual line %u: file %u,"
20750 " address %s, is_stmt %u, discrim %u%s\n",
20751 m_line, m_file,
20752 paddress (m_gdbarch, m_address),
20753 m_is_stmt, m_discriminator,
20754 (end_sequence ? "\t(end sequence)" : ""));
20755 }
20756
20757 file_entry *fe = current_file ();
20758
20759 if (fe == NULL)
20760 dwarf2_debug_line_missing_file_complaint ();
20761 /* For now we ignore lines not starting on an instruction boundary.
20762 But not when processing end_sequence for compatibility with the
20763 previous version of the code. */
20764 else if (m_op_index == 0 || end_sequence)
20765 {
20766 fe->included_p = 1;
20767 if (m_record_lines_p
20768 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
20769 {
20770 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20771 || end_sequence)
20772 {
20773 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20774 m_currently_recording_lines ? m_cu : nullptr);
20775 }
20776
20777 if (!end_sequence)
20778 {
20779 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20780 m_line_has_non_zero_discriminator,
20781 m_last_subfile))
20782 {
20783 buildsym_compunit *builder = m_cu->get_builder ();
20784 dwarf_record_line_1 (m_gdbarch,
20785 builder->get_current_subfile (),
20786 m_line, m_address,
20787 m_currently_recording_lines ? m_cu : nullptr);
20788 }
20789 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20790 m_last_line = m_line;
20791 }
20792 }
20793 }
20794 }
20795
20796 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20797 line_header *lh, bool record_lines_p)
20798 {
20799 m_cu = cu;
20800 m_gdbarch = arch;
20801 m_record_lines_p = record_lines_p;
20802 m_line_header = lh;
20803
20804 m_currently_recording_lines = true;
20805
20806 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20807 was a line entry for it so that the backend has a chance to adjust it
20808 and also record it in case it needs it. This is currently used by MIPS
20809 code, cf. `mips_adjust_dwarf2_line'. */
20810 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20811 m_is_stmt = lh->default_is_stmt;
20812 m_discriminator = 0;
20813 }
20814
20815 void
20816 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20817 const gdb_byte *line_ptr,
20818 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20819 {
20820 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20821 the pc range of the CU. However, we restrict the test to only ADDRESS
20822 values of zero to preserve GDB's previous behaviour which is to handle
20823 the specific case of a function being GC'd by the linker. */
20824
20825 if (address == 0 && address < unrelocated_lowpc)
20826 {
20827 /* This line table is for a function which has been
20828 GCd by the linker. Ignore it. PR gdb/12528 */
20829
20830 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20831 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20832
20833 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20834 line_offset, objfile_name (objfile));
20835 m_currently_recording_lines = false;
20836 /* Note: m_currently_recording_lines is left as false until we see
20837 DW_LNE_end_sequence. */
20838 }
20839 }
20840
20841 /* Subroutine of dwarf_decode_lines to simplify it.
20842 Process the line number information in LH.
20843 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20844 program in order to set included_p for every referenced header. */
20845
20846 static void
20847 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20848 const int decode_for_pst_p, CORE_ADDR lowpc)
20849 {
20850 const gdb_byte *line_ptr, *extended_end;
20851 const gdb_byte *line_end;
20852 unsigned int bytes_read, extended_len;
20853 unsigned char op_code, extended_op;
20854 CORE_ADDR baseaddr;
20855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20856 bfd *abfd = objfile->obfd;
20857 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20858 /* True if we're recording line info (as opposed to building partial
20859 symtabs and just interested in finding include files mentioned by
20860 the line number program). */
20861 bool record_lines_p = !decode_for_pst_p;
20862
20863 baseaddr = objfile->text_section_offset ();
20864
20865 line_ptr = lh->statement_program_start;
20866 line_end = lh->statement_program_end;
20867
20868 /* Read the statement sequences until there's nothing left. */
20869 while (line_ptr < line_end)
20870 {
20871 /* The DWARF line number program state machine. Reset the state
20872 machine at the start of each sequence. */
20873 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20874 bool end_sequence = false;
20875
20876 if (record_lines_p)
20877 {
20878 /* Start a subfile for the current file of the state
20879 machine. */
20880 const file_entry *fe = state_machine.current_file ();
20881
20882 if (fe != NULL)
20883 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20884 }
20885
20886 /* Decode the table. */
20887 while (line_ptr < line_end && !end_sequence)
20888 {
20889 op_code = read_1_byte (abfd, line_ptr);
20890 line_ptr += 1;
20891
20892 if (op_code >= lh->opcode_base)
20893 {
20894 /* Special opcode. */
20895 state_machine.handle_special_opcode (op_code);
20896 }
20897 else switch (op_code)
20898 {
20899 case DW_LNS_extended_op:
20900 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20901 &bytes_read);
20902 line_ptr += bytes_read;
20903 extended_end = line_ptr + extended_len;
20904 extended_op = read_1_byte (abfd, line_ptr);
20905 line_ptr += 1;
20906 switch (extended_op)
20907 {
20908 case DW_LNE_end_sequence:
20909 state_machine.handle_end_sequence ();
20910 end_sequence = true;
20911 break;
20912 case DW_LNE_set_address:
20913 {
20914 CORE_ADDR address
20915 = read_address (abfd, line_ptr, cu, &bytes_read);
20916 line_ptr += bytes_read;
20917
20918 state_machine.check_line_address (cu, line_ptr,
20919 lowpc - baseaddr, address);
20920 state_machine.handle_set_address (baseaddr, address);
20921 }
20922 break;
20923 case DW_LNE_define_file:
20924 {
20925 const char *cur_file;
20926 unsigned int mod_time, length;
20927 dir_index dindex;
20928
20929 cur_file = read_direct_string (abfd, line_ptr,
20930 &bytes_read);
20931 line_ptr += bytes_read;
20932 dindex = (dir_index)
20933 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20934 line_ptr += bytes_read;
20935 mod_time =
20936 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20937 line_ptr += bytes_read;
20938 length =
20939 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20940 line_ptr += bytes_read;
20941 lh->add_file_name (cur_file, dindex, mod_time, length);
20942 }
20943 break;
20944 case DW_LNE_set_discriminator:
20945 {
20946 /* The discriminator is not interesting to the
20947 debugger; just ignore it. We still need to
20948 check its value though:
20949 if there are consecutive entries for the same
20950 (non-prologue) line we want to coalesce them.
20951 PR 17276. */
20952 unsigned int discr
20953 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20954 line_ptr += bytes_read;
20955
20956 state_machine.handle_set_discriminator (discr);
20957 }
20958 break;
20959 default:
20960 complaint (_("mangled .debug_line section"));
20961 return;
20962 }
20963 /* Make sure that we parsed the extended op correctly. If e.g.
20964 we expected a different address size than the producer used,
20965 we may have read the wrong number of bytes. */
20966 if (line_ptr != extended_end)
20967 {
20968 complaint (_("mangled .debug_line section"));
20969 return;
20970 }
20971 break;
20972 case DW_LNS_copy:
20973 state_machine.handle_copy ();
20974 break;
20975 case DW_LNS_advance_pc:
20976 {
20977 CORE_ADDR adjust
20978 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20979 line_ptr += bytes_read;
20980
20981 state_machine.handle_advance_pc (adjust);
20982 }
20983 break;
20984 case DW_LNS_advance_line:
20985 {
20986 int line_delta
20987 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20988 line_ptr += bytes_read;
20989
20990 state_machine.handle_advance_line (line_delta);
20991 }
20992 break;
20993 case DW_LNS_set_file:
20994 {
20995 file_name_index file
20996 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20997 &bytes_read);
20998 line_ptr += bytes_read;
20999
21000 state_machine.handle_set_file (file);
21001 }
21002 break;
21003 case DW_LNS_set_column:
21004 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21005 line_ptr += bytes_read;
21006 break;
21007 case DW_LNS_negate_stmt:
21008 state_machine.handle_negate_stmt ();
21009 break;
21010 case DW_LNS_set_basic_block:
21011 break;
21012 /* Add to the address register of the state machine the
21013 address increment value corresponding to special opcode
21014 255. I.e., this value is scaled by the minimum
21015 instruction length since special opcode 255 would have
21016 scaled the increment. */
21017 case DW_LNS_const_add_pc:
21018 state_machine.handle_const_add_pc ();
21019 break;
21020 case DW_LNS_fixed_advance_pc:
21021 {
21022 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21023 line_ptr += 2;
21024
21025 state_machine.handle_fixed_advance_pc (addr_adj);
21026 }
21027 break;
21028 default:
21029 {
21030 /* Unknown standard opcode, ignore it. */
21031 int i;
21032
21033 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21034 {
21035 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21036 line_ptr += bytes_read;
21037 }
21038 }
21039 }
21040 }
21041
21042 if (!end_sequence)
21043 dwarf2_debug_line_missing_end_sequence_complaint ();
21044
21045 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21046 in which case we still finish recording the last line). */
21047 state_machine.record_line (true);
21048 }
21049 }
21050
21051 /* Decode the Line Number Program (LNP) for the given line_header
21052 structure and CU. The actual information extracted and the type
21053 of structures created from the LNP depends on the value of PST.
21054
21055 1. If PST is NULL, then this procedure uses the data from the program
21056 to create all necessary symbol tables, and their linetables.
21057
21058 2. If PST is not NULL, this procedure reads the program to determine
21059 the list of files included by the unit represented by PST, and
21060 builds all the associated partial symbol tables.
21061
21062 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21063 It is used for relative paths in the line table.
21064 NOTE: When processing partial symtabs (pst != NULL),
21065 comp_dir == pst->dirname.
21066
21067 NOTE: It is important that psymtabs have the same file name (via strcmp)
21068 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21069 symtab we don't use it in the name of the psymtabs we create.
21070 E.g. expand_line_sal requires this when finding psymtabs to expand.
21071 A good testcase for this is mb-inline.exp.
21072
21073 LOWPC is the lowest address in CU (or 0 if not known).
21074
21075 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21076 for its PC<->lines mapping information. Otherwise only the filename
21077 table is read in. */
21078
21079 static void
21080 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21081 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21082 CORE_ADDR lowpc, int decode_mapping)
21083 {
21084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21085 const int decode_for_pst_p = (pst != NULL);
21086
21087 if (decode_mapping)
21088 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21089
21090 if (decode_for_pst_p)
21091 {
21092 /* Now that we're done scanning the Line Header Program, we can
21093 create the psymtab of each included file. */
21094 for (auto &file_entry : lh->file_names ())
21095 if (file_entry.included_p == 1)
21096 {
21097 gdb::unique_xmalloc_ptr<char> name_holder;
21098 const char *include_name =
21099 psymtab_include_file_name (lh, file_entry, pst,
21100 comp_dir, &name_holder);
21101 if (include_name != NULL)
21102 dwarf2_create_include_psymtab (include_name, pst, objfile);
21103 }
21104 }
21105 else
21106 {
21107 /* Make sure a symtab is created for every file, even files
21108 which contain only variables (i.e. no code with associated
21109 line numbers). */
21110 buildsym_compunit *builder = cu->get_builder ();
21111 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21112
21113 for (auto &fe : lh->file_names ())
21114 {
21115 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21116 if (builder->get_current_subfile ()->symtab == NULL)
21117 {
21118 builder->get_current_subfile ()->symtab
21119 = allocate_symtab (cust,
21120 builder->get_current_subfile ()->name);
21121 }
21122 fe.symtab = builder->get_current_subfile ()->symtab;
21123 }
21124 }
21125 }
21126
21127 /* Start a subfile for DWARF. FILENAME is the name of the file and
21128 DIRNAME the name of the source directory which contains FILENAME
21129 or NULL if not known.
21130 This routine tries to keep line numbers from identical absolute and
21131 relative file names in a common subfile.
21132
21133 Using the `list' example from the GDB testsuite, which resides in
21134 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21135 of /srcdir/list0.c yields the following debugging information for list0.c:
21136
21137 DW_AT_name: /srcdir/list0.c
21138 DW_AT_comp_dir: /compdir
21139 files.files[0].name: list0.h
21140 files.files[0].dir: /srcdir
21141 files.files[1].name: list0.c
21142 files.files[1].dir: /srcdir
21143
21144 The line number information for list0.c has to end up in a single
21145 subfile, so that `break /srcdir/list0.c:1' works as expected.
21146 start_subfile will ensure that this happens provided that we pass the
21147 concatenation of files.files[1].dir and files.files[1].name as the
21148 subfile's name. */
21149
21150 static void
21151 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21152 const char *dirname)
21153 {
21154 gdb::unique_xmalloc_ptr<char> copy;
21155
21156 /* In order not to lose the line information directory,
21157 we concatenate it to the filename when it makes sense.
21158 Note that the Dwarf3 standard says (speaking of filenames in line
21159 information): ``The directory index is ignored for file names
21160 that represent full path names''. Thus ignoring dirname in the
21161 `else' branch below isn't an issue. */
21162
21163 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21164 {
21165 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21166 filename = copy.get ();
21167 }
21168
21169 cu->get_builder ()->start_subfile (filename);
21170 }
21171
21172 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21173 buildsym_compunit constructor. */
21174
21175 struct compunit_symtab *
21176 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21177 CORE_ADDR low_pc)
21178 {
21179 gdb_assert (m_builder == nullptr);
21180
21181 m_builder.reset (new struct buildsym_compunit
21182 (per_cu->dwarf2_per_objfile->objfile,
21183 name, comp_dir, language, low_pc));
21184
21185 list_in_scope = get_builder ()->get_file_symbols ();
21186
21187 get_builder ()->record_debugformat ("DWARF 2");
21188 get_builder ()->record_producer (producer);
21189
21190 processing_has_namespace_info = false;
21191
21192 return get_builder ()->get_compunit_symtab ();
21193 }
21194
21195 static void
21196 var_decode_location (struct attribute *attr, struct symbol *sym,
21197 struct dwarf2_cu *cu)
21198 {
21199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21200 struct comp_unit_head *cu_header = &cu->header;
21201
21202 /* NOTE drow/2003-01-30: There used to be a comment and some special
21203 code here to turn a symbol with DW_AT_external and a
21204 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21205 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21206 with some versions of binutils) where shared libraries could have
21207 relocations against symbols in their debug information - the
21208 minimal symbol would have the right address, but the debug info
21209 would not. It's no longer necessary, because we will explicitly
21210 apply relocations when we read in the debug information now. */
21211
21212 /* A DW_AT_location attribute with no contents indicates that a
21213 variable has been optimized away. */
21214 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21215 {
21216 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21217 return;
21218 }
21219
21220 /* Handle one degenerate form of location expression specially, to
21221 preserve GDB's previous behavior when section offsets are
21222 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21223 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21224
21225 if (attr_form_is_block (attr)
21226 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21227 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21228 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21229 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21230 && (DW_BLOCK (attr)->size
21231 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21232 {
21233 unsigned int dummy;
21234
21235 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21236 SET_SYMBOL_VALUE_ADDRESS (sym,
21237 read_address (objfile->obfd,
21238 DW_BLOCK (attr)->data + 1,
21239 cu, &dummy));
21240 else
21241 SET_SYMBOL_VALUE_ADDRESS
21242 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21243 &dummy));
21244 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21245 fixup_symbol_section (sym, objfile);
21246 SET_SYMBOL_VALUE_ADDRESS
21247 (sym,
21248 SYMBOL_VALUE_ADDRESS (sym)
21249 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21250 return;
21251 }
21252
21253 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21254 expression evaluator, and use LOC_COMPUTED only when necessary
21255 (i.e. when the value of a register or memory location is
21256 referenced, or a thread-local block, etc.). Then again, it might
21257 not be worthwhile. I'm assuming that it isn't unless performance
21258 or memory numbers show me otherwise. */
21259
21260 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21261
21262 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21263 cu->has_loclist = true;
21264 }
21265
21266 /* Given a pointer to a DWARF information entry, figure out if we need
21267 to make a symbol table entry for it, and if so, create a new entry
21268 and return a pointer to it.
21269 If TYPE is NULL, determine symbol type from the die, otherwise
21270 used the passed type.
21271 If SPACE is not NULL, use it to hold the new symbol. If it is
21272 NULL, allocate a new symbol on the objfile's obstack. */
21273
21274 static struct symbol *
21275 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21276 struct symbol *space)
21277 {
21278 struct dwarf2_per_objfile *dwarf2_per_objfile
21279 = cu->per_cu->dwarf2_per_objfile;
21280 struct objfile *objfile = dwarf2_per_objfile->objfile;
21281 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21282 struct symbol *sym = NULL;
21283 const char *name;
21284 struct attribute *attr = NULL;
21285 struct attribute *attr2 = NULL;
21286 CORE_ADDR baseaddr;
21287 struct pending **list_to_add = NULL;
21288
21289 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21290
21291 baseaddr = objfile->text_section_offset ();
21292
21293 name = dwarf2_name (die, cu);
21294 if (name)
21295 {
21296 const char *linkagename;
21297 int suppress_add = 0;
21298
21299 if (space)
21300 sym = space;
21301 else
21302 sym = allocate_symbol (objfile);
21303 OBJSTAT (objfile, n_syms++);
21304
21305 /* Cache this symbol's name and the name's demangled form (if any). */
21306 sym->set_language (cu->language, &objfile->objfile_obstack);
21307 linkagename = dwarf2_physname (name, die, cu);
21308 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21309
21310 /* Fortran does not have mangling standard and the mangling does differ
21311 between gfortran, iFort etc. */
21312 if (cu->language == language_fortran
21313 && symbol_get_demangled_name (sym) == NULL)
21314 symbol_set_demangled_name (sym,
21315 dwarf2_full_name (name, die, cu),
21316 NULL);
21317
21318 /* Default assumptions.
21319 Use the passed type or decode it from the die. */
21320 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21321 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21322 if (type != NULL)
21323 SYMBOL_TYPE (sym) = type;
21324 else
21325 SYMBOL_TYPE (sym) = die_type (die, cu);
21326 attr = dwarf2_attr (die,
21327 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21328 cu);
21329 if (attr != nullptr)
21330 {
21331 SYMBOL_LINE (sym) = DW_UNSND (attr);
21332 }
21333
21334 attr = dwarf2_attr (die,
21335 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21336 cu);
21337 if (attr != nullptr)
21338 {
21339 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21340 struct file_entry *fe;
21341
21342 if (cu->line_header != NULL)
21343 fe = cu->line_header->file_name_at (file_index);
21344 else
21345 fe = NULL;
21346
21347 if (fe == NULL)
21348 complaint (_("file index out of range"));
21349 else
21350 symbol_set_symtab (sym, fe->symtab);
21351 }
21352
21353 switch (die->tag)
21354 {
21355 case DW_TAG_label:
21356 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21357 if (attr != nullptr)
21358 {
21359 CORE_ADDR addr;
21360
21361 addr = attr_value_as_address (attr);
21362 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21363 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21364 }
21365 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21366 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21367 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21368 add_symbol_to_list (sym, cu->list_in_scope);
21369 break;
21370 case DW_TAG_subprogram:
21371 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21372 finish_block. */
21373 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21374 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21375 if ((attr2 && (DW_UNSND (attr2) != 0))
21376 || cu->language == language_ada
21377 || cu->language == language_fortran)
21378 {
21379 /* Subprograms marked external are stored as a global symbol.
21380 Ada and Fortran subprograms, whether marked external or
21381 not, are always stored as a global symbol, because we want
21382 to be able to access them globally. For instance, we want
21383 to be able to break on a nested subprogram without having
21384 to specify the context. */
21385 list_to_add = cu->get_builder ()->get_global_symbols ();
21386 }
21387 else
21388 {
21389 list_to_add = cu->list_in_scope;
21390 }
21391 break;
21392 case DW_TAG_inlined_subroutine:
21393 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21394 finish_block. */
21395 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21396 SYMBOL_INLINED (sym) = 1;
21397 list_to_add = cu->list_in_scope;
21398 break;
21399 case DW_TAG_template_value_param:
21400 suppress_add = 1;
21401 /* Fall through. */
21402 case DW_TAG_constant:
21403 case DW_TAG_variable:
21404 case DW_TAG_member:
21405 /* Compilation with minimal debug info may result in
21406 variables with missing type entries. Change the
21407 misleading `void' type to something sensible. */
21408 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21409 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21410
21411 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21412 /* In the case of DW_TAG_member, we should only be called for
21413 static const members. */
21414 if (die->tag == DW_TAG_member)
21415 {
21416 /* dwarf2_add_field uses die_is_declaration,
21417 so we do the same. */
21418 gdb_assert (die_is_declaration (die, cu));
21419 gdb_assert (attr);
21420 }
21421 if (attr != nullptr)
21422 {
21423 dwarf2_const_value (attr, sym, cu);
21424 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21425 if (!suppress_add)
21426 {
21427 if (attr2 && (DW_UNSND (attr2) != 0))
21428 list_to_add = cu->get_builder ()->get_global_symbols ();
21429 else
21430 list_to_add = cu->list_in_scope;
21431 }
21432 break;
21433 }
21434 attr = dwarf2_attr (die, DW_AT_location, cu);
21435 if (attr != nullptr)
21436 {
21437 var_decode_location (attr, sym, cu);
21438 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21439
21440 /* Fortran explicitly imports any global symbols to the local
21441 scope by DW_TAG_common_block. */
21442 if (cu->language == language_fortran && die->parent
21443 && die->parent->tag == DW_TAG_common_block)
21444 attr2 = NULL;
21445
21446 if (SYMBOL_CLASS (sym) == LOC_STATIC
21447 && SYMBOL_VALUE_ADDRESS (sym) == 0
21448 && !dwarf2_per_objfile->has_section_at_zero)
21449 {
21450 /* When a static variable is eliminated by the linker,
21451 the corresponding debug information is not stripped
21452 out, but the variable address is set to null;
21453 do not add such variables into symbol table. */
21454 }
21455 else if (attr2 && (DW_UNSND (attr2) != 0))
21456 {
21457 if (SYMBOL_CLASS (sym) == LOC_STATIC
21458 && (objfile->flags & OBJF_MAINLINE) == 0
21459 && dwarf2_per_objfile->can_copy)
21460 {
21461 /* A global static variable might be subject to
21462 copy relocation. We first check for a local
21463 minsym, though, because maybe the symbol was
21464 marked hidden, in which case this would not
21465 apply. */
21466 bound_minimal_symbol found
21467 = (lookup_minimal_symbol_linkage
21468 (sym->linkage_name (), objfile));
21469 if (found.minsym != nullptr)
21470 sym->maybe_copied = 1;
21471 }
21472
21473 /* A variable with DW_AT_external is never static,
21474 but it may be block-scoped. */
21475 list_to_add
21476 = ((cu->list_in_scope
21477 == cu->get_builder ()->get_file_symbols ())
21478 ? cu->get_builder ()->get_global_symbols ()
21479 : cu->list_in_scope);
21480 }
21481 else
21482 list_to_add = cu->list_in_scope;
21483 }
21484 else
21485 {
21486 /* We do not know the address of this symbol.
21487 If it is an external symbol and we have type information
21488 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21489 The address of the variable will then be determined from
21490 the minimal symbol table whenever the variable is
21491 referenced. */
21492 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21493
21494 /* Fortran explicitly imports any global symbols to the local
21495 scope by DW_TAG_common_block. */
21496 if (cu->language == language_fortran && die->parent
21497 && die->parent->tag == DW_TAG_common_block)
21498 {
21499 /* SYMBOL_CLASS doesn't matter here because
21500 read_common_block is going to reset it. */
21501 if (!suppress_add)
21502 list_to_add = cu->list_in_scope;
21503 }
21504 else if (attr2 && (DW_UNSND (attr2) != 0)
21505 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21506 {
21507 /* A variable with DW_AT_external is never static, but it
21508 may be block-scoped. */
21509 list_to_add
21510 = ((cu->list_in_scope
21511 == cu->get_builder ()->get_file_symbols ())
21512 ? cu->get_builder ()->get_global_symbols ()
21513 : cu->list_in_scope);
21514
21515 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21516 }
21517 else if (!die_is_declaration (die, cu))
21518 {
21519 /* Use the default LOC_OPTIMIZED_OUT class. */
21520 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21521 if (!suppress_add)
21522 list_to_add = cu->list_in_scope;
21523 }
21524 }
21525 break;
21526 case DW_TAG_formal_parameter:
21527 {
21528 /* If we are inside a function, mark this as an argument. If
21529 not, we might be looking at an argument to an inlined function
21530 when we do not have enough information to show inlined frames;
21531 pretend it's a local variable in that case so that the user can
21532 still see it. */
21533 struct context_stack *curr
21534 = cu->get_builder ()->get_current_context_stack ();
21535 if (curr != nullptr && curr->name != nullptr)
21536 SYMBOL_IS_ARGUMENT (sym) = 1;
21537 attr = dwarf2_attr (die, DW_AT_location, cu);
21538 if (attr != nullptr)
21539 {
21540 var_decode_location (attr, sym, cu);
21541 }
21542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21543 if (attr != nullptr)
21544 {
21545 dwarf2_const_value (attr, sym, cu);
21546 }
21547
21548 list_to_add = cu->list_in_scope;
21549 }
21550 break;
21551 case DW_TAG_unspecified_parameters:
21552 /* From varargs functions; gdb doesn't seem to have any
21553 interest in this information, so just ignore it for now.
21554 (FIXME?) */
21555 break;
21556 case DW_TAG_template_type_param:
21557 suppress_add = 1;
21558 /* Fall through. */
21559 case DW_TAG_class_type:
21560 case DW_TAG_interface_type:
21561 case DW_TAG_structure_type:
21562 case DW_TAG_union_type:
21563 case DW_TAG_set_type:
21564 case DW_TAG_enumeration_type:
21565 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21566 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21567
21568 {
21569 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21570 really ever be static objects: otherwise, if you try
21571 to, say, break of a class's method and you're in a file
21572 which doesn't mention that class, it won't work unless
21573 the check for all static symbols in lookup_symbol_aux
21574 saves you. See the OtherFileClass tests in
21575 gdb.c++/namespace.exp. */
21576
21577 if (!suppress_add)
21578 {
21579 buildsym_compunit *builder = cu->get_builder ();
21580 list_to_add
21581 = (cu->list_in_scope == builder->get_file_symbols ()
21582 && cu->language == language_cplus
21583 ? builder->get_global_symbols ()
21584 : cu->list_in_scope);
21585
21586 /* The semantics of C++ state that "struct foo {
21587 ... }" also defines a typedef for "foo". */
21588 if (cu->language == language_cplus
21589 || cu->language == language_ada
21590 || cu->language == language_d
21591 || cu->language == language_rust)
21592 {
21593 /* The symbol's name is already allocated along
21594 with this objfile, so we don't need to
21595 duplicate it for the type. */
21596 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21597 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21598 }
21599 }
21600 }
21601 break;
21602 case DW_TAG_typedef:
21603 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21604 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21605 list_to_add = cu->list_in_scope;
21606 break;
21607 case DW_TAG_base_type:
21608 case DW_TAG_subrange_type:
21609 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21610 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21611 list_to_add = cu->list_in_scope;
21612 break;
21613 case DW_TAG_enumerator:
21614 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21615 if (attr != nullptr)
21616 {
21617 dwarf2_const_value (attr, sym, cu);
21618 }
21619 {
21620 /* NOTE: carlton/2003-11-10: See comment above in the
21621 DW_TAG_class_type, etc. block. */
21622
21623 list_to_add
21624 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21625 && cu->language == language_cplus
21626 ? cu->get_builder ()->get_global_symbols ()
21627 : cu->list_in_scope);
21628 }
21629 break;
21630 case DW_TAG_imported_declaration:
21631 case DW_TAG_namespace:
21632 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21633 list_to_add = cu->get_builder ()->get_global_symbols ();
21634 break;
21635 case DW_TAG_module:
21636 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21637 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21638 list_to_add = cu->get_builder ()->get_global_symbols ();
21639 break;
21640 case DW_TAG_common_block:
21641 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21642 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21643 add_symbol_to_list (sym, cu->list_in_scope);
21644 break;
21645 default:
21646 /* Not a tag we recognize. Hopefully we aren't processing
21647 trash data, but since we must specifically ignore things
21648 we don't recognize, there is nothing else we should do at
21649 this point. */
21650 complaint (_("unsupported tag: '%s'"),
21651 dwarf_tag_name (die->tag));
21652 break;
21653 }
21654
21655 if (suppress_add)
21656 {
21657 sym->hash_next = objfile->template_symbols;
21658 objfile->template_symbols = sym;
21659 list_to_add = NULL;
21660 }
21661
21662 if (list_to_add != NULL)
21663 add_symbol_to_list (sym, list_to_add);
21664
21665 /* For the benefit of old versions of GCC, check for anonymous
21666 namespaces based on the demangled name. */
21667 if (!cu->processing_has_namespace_info
21668 && cu->language == language_cplus)
21669 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21670 }
21671 return (sym);
21672 }
21673
21674 /* Given an attr with a DW_FORM_dataN value in host byte order,
21675 zero-extend it as appropriate for the symbol's type. The DWARF
21676 standard (v4) is not entirely clear about the meaning of using
21677 DW_FORM_dataN for a constant with a signed type, where the type is
21678 wider than the data. The conclusion of a discussion on the DWARF
21679 list was that this is unspecified. We choose to always zero-extend
21680 because that is the interpretation long in use by GCC. */
21681
21682 static gdb_byte *
21683 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21684 struct dwarf2_cu *cu, LONGEST *value, int bits)
21685 {
21686 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21687 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21688 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21689 LONGEST l = DW_UNSND (attr);
21690
21691 if (bits < sizeof (*value) * 8)
21692 {
21693 l &= ((LONGEST) 1 << bits) - 1;
21694 *value = l;
21695 }
21696 else if (bits == sizeof (*value) * 8)
21697 *value = l;
21698 else
21699 {
21700 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21701 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21702 return bytes;
21703 }
21704
21705 return NULL;
21706 }
21707
21708 /* Read a constant value from an attribute. Either set *VALUE, or if
21709 the value does not fit in *VALUE, set *BYTES - either already
21710 allocated on the objfile obstack, or newly allocated on OBSTACK,
21711 or, set *BATON, if we translated the constant to a location
21712 expression. */
21713
21714 static void
21715 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21716 const char *name, struct obstack *obstack,
21717 struct dwarf2_cu *cu,
21718 LONGEST *value, const gdb_byte **bytes,
21719 struct dwarf2_locexpr_baton **baton)
21720 {
21721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21722 struct comp_unit_head *cu_header = &cu->header;
21723 struct dwarf_block *blk;
21724 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21725 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21726
21727 *value = 0;
21728 *bytes = NULL;
21729 *baton = NULL;
21730
21731 switch (attr->form)
21732 {
21733 case DW_FORM_addr:
21734 case DW_FORM_addrx:
21735 case DW_FORM_GNU_addr_index:
21736 {
21737 gdb_byte *data;
21738
21739 if (TYPE_LENGTH (type) != cu_header->addr_size)
21740 dwarf2_const_value_length_mismatch_complaint (name,
21741 cu_header->addr_size,
21742 TYPE_LENGTH (type));
21743 /* Symbols of this form are reasonably rare, so we just
21744 piggyback on the existing location code rather than writing
21745 a new implementation of symbol_computed_ops. */
21746 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21747 (*baton)->per_cu = cu->per_cu;
21748 gdb_assert ((*baton)->per_cu);
21749
21750 (*baton)->size = 2 + cu_header->addr_size;
21751 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21752 (*baton)->data = data;
21753
21754 data[0] = DW_OP_addr;
21755 store_unsigned_integer (&data[1], cu_header->addr_size,
21756 byte_order, DW_ADDR (attr));
21757 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21758 }
21759 break;
21760 case DW_FORM_string:
21761 case DW_FORM_strp:
21762 case DW_FORM_strx:
21763 case DW_FORM_GNU_str_index:
21764 case DW_FORM_GNU_strp_alt:
21765 /* DW_STRING is already allocated on the objfile obstack, point
21766 directly to it. */
21767 *bytes = (const gdb_byte *) DW_STRING (attr);
21768 break;
21769 case DW_FORM_block1:
21770 case DW_FORM_block2:
21771 case DW_FORM_block4:
21772 case DW_FORM_block:
21773 case DW_FORM_exprloc:
21774 case DW_FORM_data16:
21775 blk = DW_BLOCK (attr);
21776 if (TYPE_LENGTH (type) != blk->size)
21777 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21778 TYPE_LENGTH (type));
21779 *bytes = blk->data;
21780 break;
21781
21782 /* The DW_AT_const_value attributes are supposed to carry the
21783 symbol's value "represented as it would be on the target
21784 architecture." By the time we get here, it's already been
21785 converted to host endianness, so we just need to sign- or
21786 zero-extend it as appropriate. */
21787 case DW_FORM_data1:
21788 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21789 break;
21790 case DW_FORM_data2:
21791 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21792 break;
21793 case DW_FORM_data4:
21794 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21795 break;
21796 case DW_FORM_data8:
21797 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21798 break;
21799
21800 case DW_FORM_sdata:
21801 case DW_FORM_implicit_const:
21802 *value = DW_SND (attr);
21803 break;
21804
21805 case DW_FORM_udata:
21806 *value = DW_UNSND (attr);
21807 break;
21808
21809 default:
21810 complaint (_("unsupported const value attribute form: '%s'"),
21811 dwarf_form_name (attr->form));
21812 *value = 0;
21813 break;
21814 }
21815 }
21816
21817
21818 /* Copy constant value from an attribute to a symbol. */
21819
21820 static void
21821 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21822 struct dwarf2_cu *cu)
21823 {
21824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21825 LONGEST value;
21826 const gdb_byte *bytes;
21827 struct dwarf2_locexpr_baton *baton;
21828
21829 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21830 sym->print_name (),
21831 &objfile->objfile_obstack, cu,
21832 &value, &bytes, &baton);
21833
21834 if (baton != NULL)
21835 {
21836 SYMBOL_LOCATION_BATON (sym) = baton;
21837 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21838 }
21839 else if (bytes != NULL)
21840 {
21841 SYMBOL_VALUE_BYTES (sym) = bytes;
21842 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21843 }
21844 else
21845 {
21846 SYMBOL_VALUE (sym) = value;
21847 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21848 }
21849 }
21850
21851 /* Return the type of the die in question using its DW_AT_type attribute. */
21852
21853 static struct type *
21854 die_type (struct die_info *die, struct dwarf2_cu *cu)
21855 {
21856 struct attribute *type_attr;
21857
21858 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21859 if (!type_attr)
21860 {
21861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21862 /* A missing DW_AT_type represents a void type. */
21863 return objfile_type (objfile)->builtin_void;
21864 }
21865
21866 return lookup_die_type (die, type_attr, cu);
21867 }
21868
21869 /* True iff CU's producer generates GNAT Ada auxiliary information
21870 that allows to find parallel types through that information instead
21871 of having to do expensive parallel lookups by type name. */
21872
21873 static int
21874 need_gnat_info (struct dwarf2_cu *cu)
21875 {
21876 /* Assume that the Ada compiler was GNAT, which always produces
21877 the auxiliary information. */
21878 return (cu->language == language_ada);
21879 }
21880
21881 /* Return the auxiliary type of the die in question using its
21882 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21883 attribute is not present. */
21884
21885 static struct type *
21886 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21887 {
21888 struct attribute *type_attr;
21889
21890 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21891 if (!type_attr)
21892 return NULL;
21893
21894 return lookup_die_type (die, type_attr, cu);
21895 }
21896
21897 /* If DIE has a descriptive_type attribute, then set the TYPE's
21898 descriptive type accordingly. */
21899
21900 static void
21901 set_descriptive_type (struct type *type, struct die_info *die,
21902 struct dwarf2_cu *cu)
21903 {
21904 struct type *descriptive_type = die_descriptive_type (die, cu);
21905
21906 if (descriptive_type)
21907 {
21908 ALLOCATE_GNAT_AUX_TYPE (type);
21909 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21910 }
21911 }
21912
21913 /* Return the containing type of the die in question using its
21914 DW_AT_containing_type attribute. */
21915
21916 static struct type *
21917 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21918 {
21919 struct attribute *type_attr;
21920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21921
21922 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21923 if (!type_attr)
21924 error (_("Dwarf Error: Problem turning containing type into gdb type "
21925 "[in module %s]"), objfile_name (objfile));
21926
21927 return lookup_die_type (die, type_attr, cu);
21928 }
21929
21930 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21931
21932 static struct type *
21933 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21934 {
21935 struct dwarf2_per_objfile *dwarf2_per_objfile
21936 = cu->per_cu->dwarf2_per_objfile;
21937 struct objfile *objfile = dwarf2_per_objfile->objfile;
21938 char *saved;
21939
21940 std::string message
21941 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21942 objfile_name (objfile),
21943 sect_offset_str (cu->header.sect_off),
21944 sect_offset_str (die->sect_off));
21945 saved = obstack_strdup (&objfile->objfile_obstack, message);
21946
21947 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21948 }
21949
21950 /* Look up the type of DIE in CU using its type attribute ATTR.
21951 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21952 DW_AT_containing_type.
21953 If there is no type substitute an error marker. */
21954
21955 static struct type *
21956 lookup_die_type (struct die_info *die, const struct attribute *attr,
21957 struct dwarf2_cu *cu)
21958 {
21959 struct dwarf2_per_objfile *dwarf2_per_objfile
21960 = cu->per_cu->dwarf2_per_objfile;
21961 struct objfile *objfile = dwarf2_per_objfile->objfile;
21962 struct type *this_type;
21963
21964 gdb_assert (attr->name == DW_AT_type
21965 || attr->name == DW_AT_GNAT_descriptive_type
21966 || attr->name == DW_AT_containing_type);
21967
21968 /* First see if we have it cached. */
21969
21970 if (attr->form == DW_FORM_GNU_ref_alt)
21971 {
21972 struct dwarf2_per_cu_data *per_cu;
21973 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21974
21975 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21976 dwarf2_per_objfile);
21977 this_type = get_die_type_at_offset (sect_off, per_cu);
21978 }
21979 else if (attr_form_is_ref (attr))
21980 {
21981 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21982
21983 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21984 }
21985 else if (attr->form == DW_FORM_ref_sig8)
21986 {
21987 ULONGEST signature = DW_SIGNATURE (attr);
21988
21989 return get_signatured_type (die, signature, cu);
21990 }
21991 else
21992 {
21993 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21994 " at %s [in module %s]"),
21995 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21996 objfile_name (objfile));
21997 return build_error_marker_type (cu, die);
21998 }
21999
22000 /* If not cached we need to read it in. */
22001
22002 if (this_type == NULL)
22003 {
22004 struct die_info *type_die = NULL;
22005 struct dwarf2_cu *type_cu = cu;
22006
22007 if (attr_form_is_ref (attr))
22008 type_die = follow_die_ref (die, attr, &type_cu);
22009 if (type_die == NULL)
22010 return build_error_marker_type (cu, die);
22011 /* If we find the type now, it's probably because the type came
22012 from an inter-CU reference and the type's CU got expanded before
22013 ours. */
22014 this_type = read_type_die (type_die, type_cu);
22015 }
22016
22017 /* If we still don't have a type use an error marker. */
22018
22019 if (this_type == NULL)
22020 return build_error_marker_type (cu, die);
22021
22022 return this_type;
22023 }
22024
22025 /* Return the type in DIE, CU.
22026 Returns NULL for invalid types.
22027
22028 This first does a lookup in die_type_hash,
22029 and only reads the die in if necessary.
22030
22031 NOTE: This can be called when reading in partial or full symbols. */
22032
22033 static struct type *
22034 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22035 {
22036 struct type *this_type;
22037
22038 this_type = get_die_type (die, cu);
22039 if (this_type)
22040 return this_type;
22041
22042 return read_type_die_1 (die, cu);
22043 }
22044
22045 /* Read the type in DIE, CU.
22046 Returns NULL for invalid types. */
22047
22048 static struct type *
22049 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22050 {
22051 struct type *this_type = NULL;
22052
22053 switch (die->tag)
22054 {
22055 case DW_TAG_class_type:
22056 case DW_TAG_interface_type:
22057 case DW_TAG_structure_type:
22058 case DW_TAG_union_type:
22059 this_type = read_structure_type (die, cu);
22060 break;
22061 case DW_TAG_enumeration_type:
22062 this_type = read_enumeration_type (die, cu);
22063 break;
22064 case DW_TAG_subprogram:
22065 case DW_TAG_subroutine_type:
22066 case DW_TAG_inlined_subroutine:
22067 this_type = read_subroutine_type (die, cu);
22068 break;
22069 case DW_TAG_array_type:
22070 this_type = read_array_type (die, cu);
22071 break;
22072 case DW_TAG_set_type:
22073 this_type = read_set_type (die, cu);
22074 break;
22075 case DW_TAG_pointer_type:
22076 this_type = read_tag_pointer_type (die, cu);
22077 break;
22078 case DW_TAG_ptr_to_member_type:
22079 this_type = read_tag_ptr_to_member_type (die, cu);
22080 break;
22081 case DW_TAG_reference_type:
22082 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22083 break;
22084 case DW_TAG_rvalue_reference_type:
22085 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22086 break;
22087 case DW_TAG_const_type:
22088 this_type = read_tag_const_type (die, cu);
22089 break;
22090 case DW_TAG_volatile_type:
22091 this_type = read_tag_volatile_type (die, cu);
22092 break;
22093 case DW_TAG_restrict_type:
22094 this_type = read_tag_restrict_type (die, cu);
22095 break;
22096 case DW_TAG_string_type:
22097 this_type = read_tag_string_type (die, cu);
22098 break;
22099 case DW_TAG_typedef:
22100 this_type = read_typedef (die, cu);
22101 break;
22102 case DW_TAG_subrange_type:
22103 this_type = read_subrange_type (die, cu);
22104 break;
22105 case DW_TAG_base_type:
22106 this_type = read_base_type (die, cu);
22107 break;
22108 case DW_TAG_unspecified_type:
22109 this_type = read_unspecified_type (die, cu);
22110 break;
22111 case DW_TAG_namespace:
22112 this_type = read_namespace_type (die, cu);
22113 break;
22114 case DW_TAG_module:
22115 this_type = read_module_type (die, cu);
22116 break;
22117 case DW_TAG_atomic_type:
22118 this_type = read_tag_atomic_type (die, cu);
22119 break;
22120 default:
22121 complaint (_("unexpected tag in read_type_die: '%s'"),
22122 dwarf_tag_name (die->tag));
22123 break;
22124 }
22125
22126 return this_type;
22127 }
22128
22129 /* See if we can figure out if the class lives in a namespace. We do
22130 this by looking for a member function; its demangled name will
22131 contain namespace info, if there is any.
22132 Return the computed name or NULL.
22133 Space for the result is allocated on the objfile's obstack.
22134 This is the full-die version of guess_partial_die_structure_name.
22135 In this case we know DIE has no useful parent. */
22136
22137 static const char *
22138 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22139 {
22140 struct die_info *spec_die;
22141 struct dwarf2_cu *spec_cu;
22142 struct die_info *child;
22143 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22144
22145 spec_cu = cu;
22146 spec_die = die_specification (die, &spec_cu);
22147 if (spec_die != NULL)
22148 {
22149 die = spec_die;
22150 cu = spec_cu;
22151 }
22152
22153 for (child = die->child;
22154 child != NULL;
22155 child = child->sibling)
22156 {
22157 if (child->tag == DW_TAG_subprogram)
22158 {
22159 const char *linkage_name = dw2_linkage_name (child, cu);
22160
22161 if (linkage_name != NULL)
22162 {
22163 gdb::unique_xmalloc_ptr<char> actual_name
22164 (language_class_name_from_physname (cu->language_defn,
22165 linkage_name));
22166 const char *name = NULL;
22167
22168 if (actual_name != NULL)
22169 {
22170 const char *die_name = dwarf2_name (die, cu);
22171
22172 if (die_name != NULL
22173 && strcmp (die_name, actual_name.get ()) != 0)
22174 {
22175 /* Strip off the class name from the full name.
22176 We want the prefix. */
22177 int die_name_len = strlen (die_name);
22178 int actual_name_len = strlen (actual_name.get ());
22179 const char *ptr = actual_name.get ();
22180
22181 /* Test for '::' as a sanity check. */
22182 if (actual_name_len > die_name_len + 2
22183 && ptr[actual_name_len - die_name_len - 1] == ':')
22184 name = obstack_strndup (
22185 &objfile->per_bfd->storage_obstack,
22186 ptr, actual_name_len - die_name_len - 2);
22187 }
22188 }
22189 return name;
22190 }
22191 }
22192 }
22193
22194 return NULL;
22195 }
22196
22197 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22198 prefix part in such case. See
22199 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22200
22201 static const char *
22202 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22203 {
22204 struct attribute *attr;
22205 const char *base;
22206
22207 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22208 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22209 return NULL;
22210
22211 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22212 return NULL;
22213
22214 attr = dw2_linkage_name_attr (die, cu);
22215 if (attr == NULL || DW_STRING (attr) == NULL)
22216 return NULL;
22217
22218 /* dwarf2_name had to be already called. */
22219 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22220
22221 /* Strip the base name, keep any leading namespaces/classes. */
22222 base = strrchr (DW_STRING (attr), ':');
22223 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22224 return "";
22225
22226 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22227 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22228 DW_STRING (attr),
22229 &base[-1] - DW_STRING (attr));
22230 }
22231
22232 /* Return the name of the namespace/class that DIE is defined within,
22233 or "" if we can't tell. The caller should not xfree the result.
22234
22235 For example, if we're within the method foo() in the following
22236 code:
22237
22238 namespace N {
22239 class C {
22240 void foo () {
22241 }
22242 };
22243 }
22244
22245 then determine_prefix on foo's die will return "N::C". */
22246
22247 static const char *
22248 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22249 {
22250 struct dwarf2_per_objfile *dwarf2_per_objfile
22251 = cu->per_cu->dwarf2_per_objfile;
22252 struct die_info *parent, *spec_die;
22253 struct dwarf2_cu *spec_cu;
22254 struct type *parent_type;
22255 const char *retval;
22256
22257 if (cu->language != language_cplus
22258 && cu->language != language_fortran && cu->language != language_d
22259 && cu->language != language_rust)
22260 return "";
22261
22262 retval = anonymous_struct_prefix (die, cu);
22263 if (retval)
22264 return retval;
22265
22266 /* We have to be careful in the presence of DW_AT_specification.
22267 For example, with GCC 3.4, given the code
22268
22269 namespace N {
22270 void foo() {
22271 // Definition of N::foo.
22272 }
22273 }
22274
22275 then we'll have a tree of DIEs like this:
22276
22277 1: DW_TAG_compile_unit
22278 2: DW_TAG_namespace // N
22279 3: DW_TAG_subprogram // declaration of N::foo
22280 4: DW_TAG_subprogram // definition of N::foo
22281 DW_AT_specification // refers to die #3
22282
22283 Thus, when processing die #4, we have to pretend that we're in
22284 the context of its DW_AT_specification, namely the contex of die
22285 #3. */
22286 spec_cu = cu;
22287 spec_die = die_specification (die, &spec_cu);
22288 if (spec_die == NULL)
22289 parent = die->parent;
22290 else
22291 {
22292 parent = spec_die->parent;
22293 cu = spec_cu;
22294 }
22295
22296 if (parent == NULL)
22297 return "";
22298 else if (parent->building_fullname)
22299 {
22300 const char *name;
22301 const char *parent_name;
22302
22303 /* It has been seen on RealView 2.2 built binaries,
22304 DW_TAG_template_type_param types actually _defined_ as
22305 children of the parent class:
22306
22307 enum E {};
22308 template class <class Enum> Class{};
22309 Class<enum E> class_e;
22310
22311 1: DW_TAG_class_type (Class)
22312 2: DW_TAG_enumeration_type (E)
22313 3: DW_TAG_enumerator (enum1:0)
22314 3: DW_TAG_enumerator (enum2:1)
22315 ...
22316 2: DW_TAG_template_type_param
22317 DW_AT_type DW_FORM_ref_udata (E)
22318
22319 Besides being broken debug info, it can put GDB into an
22320 infinite loop. Consider:
22321
22322 When we're building the full name for Class<E>, we'll start
22323 at Class, and go look over its template type parameters,
22324 finding E. We'll then try to build the full name of E, and
22325 reach here. We're now trying to build the full name of E,
22326 and look over the parent DIE for containing scope. In the
22327 broken case, if we followed the parent DIE of E, we'd again
22328 find Class, and once again go look at its template type
22329 arguments, etc., etc. Simply don't consider such parent die
22330 as source-level parent of this die (it can't be, the language
22331 doesn't allow it), and break the loop here. */
22332 name = dwarf2_name (die, cu);
22333 parent_name = dwarf2_name (parent, cu);
22334 complaint (_("template param type '%s' defined within parent '%s'"),
22335 name ? name : "<unknown>",
22336 parent_name ? parent_name : "<unknown>");
22337 return "";
22338 }
22339 else
22340 switch (parent->tag)
22341 {
22342 case DW_TAG_namespace:
22343 parent_type = read_type_die (parent, cu);
22344 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22345 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22346 Work around this problem here. */
22347 if (cu->language == language_cplus
22348 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22349 return "";
22350 /* We give a name to even anonymous namespaces. */
22351 return TYPE_NAME (parent_type);
22352 case DW_TAG_class_type:
22353 case DW_TAG_interface_type:
22354 case DW_TAG_structure_type:
22355 case DW_TAG_union_type:
22356 case DW_TAG_module:
22357 parent_type = read_type_die (parent, cu);
22358 if (TYPE_NAME (parent_type) != NULL)
22359 return TYPE_NAME (parent_type);
22360 else
22361 /* An anonymous structure is only allowed non-static data
22362 members; no typedefs, no member functions, et cetera.
22363 So it does not need a prefix. */
22364 return "";
22365 case DW_TAG_compile_unit:
22366 case DW_TAG_partial_unit:
22367 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22368 if (cu->language == language_cplus
22369 && !dwarf2_per_objfile->types.empty ()
22370 && die->child != NULL
22371 && (die->tag == DW_TAG_class_type
22372 || die->tag == DW_TAG_structure_type
22373 || die->tag == DW_TAG_union_type))
22374 {
22375 const char *name = guess_full_die_structure_name (die, cu);
22376 if (name != NULL)
22377 return name;
22378 }
22379 return "";
22380 case DW_TAG_subprogram:
22381 /* Nested subroutines in Fortran get a prefix with the name
22382 of the parent's subroutine. */
22383 if (cu->language == language_fortran)
22384 {
22385 if ((die->tag == DW_TAG_subprogram)
22386 && (dwarf2_name (parent, cu) != NULL))
22387 return dwarf2_name (parent, cu);
22388 }
22389 return determine_prefix (parent, cu);
22390 case DW_TAG_enumeration_type:
22391 parent_type = read_type_die (parent, cu);
22392 if (TYPE_DECLARED_CLASS (parent_type))
22393 {
22394 if (TYPE_NAME (parent_type) != NULL)
22395 return TYPE_NAME (parent_type);
22396 return "";
22397 }
22398 /* Fall through. */
22399 default:
22400 return determine_prefix (parent, cu);
22401 }
22402 }
22403
22404 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22405 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22406 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22407 an obconcat, otherwise allocate storage for the result. The CU argument is
22408 used to determine the language and hence, the appropriate separator. */
22409
22410 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22411
22412 static char *
22413 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22414 int physname, struct dwarf2_cu *cu)
22415 {
22416 const char *lead = "";
22417 const char *sep;
22418
22419 if (suffix == NULL || suffix[0] == '\0'
22420 || prefix == NULL || prefix[0] == '\0')
22421 sep = "";
22422 else if (cu->language == language_d)
22423 {
22424 /* For D, the 'main' function could be defined in any module, but it
22425 should never be prefixed. */
22426 if (strcmp (suffix, "D main") == 0)
22427 {
22428 prefix = "";
22429 sep = "";
22430 }
22431 else
22432 sep = ".";
22433 }
22434 else if (cu->language == language_fortran && physname)
22435 {
22436 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22437 DW_AT_MIPS_linkage_name is preferred and used instead. */
22438
22439 lead = "__";
22440 sep = "_MOD_";
22441 }
22442 else
22443 sep = "::";
22444
22445 if (prefix == NULL)
22446 prefix = "";
22447 if (suffix == NULL)
22448 suffix = "";
22449
22450 if (obs == NULL)
22451 {
22452 char *retval
22453 = ((char *)
22454 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22455
22456 strcpy (retval, lead);
22457 strcat (retval, prefix);
22458 strcat (retval, sep);
22459 strcat (retval, suffix);
22460 return retval;
22461 }
22462 else
22463 {
22464 /* We have an obstack. */
22465 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22466 }
22467 }
22468
22469 /* Return sibling of die, NULL if no sibling. */
22470
22471 static struct die_info *
22472 sibling_die (struct die_info *die)
22473 {
22474 return die->sibling;
22475 }
22476
22477 /* Get name of a die, return NULL if not found. */
22478
22479 static const char *
22480 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22481 struct obstack *obstack)
22482 {
22483 if (name && cu->language == language_cplus)
22484 {
22485 std::string canon_name = cp_canonicalize_string (name);
22486
22487 if (!canon_name.empty ())
22488 {
22489 if (canon_name != name)
22490 name = obstack_strdup (obstack, canon_name);
22491 }
22492 }
22493
22494 return name;
22495 }
22496
22497 /* Get name of a die, return NULL if not found.
22498 Anonymous namespaces are converted to their magic string. */
22499
22500 static const char *
22501 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22502 {
22503 struct attribute *attr;
22504 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22505
22506 attr = dwarf2_attr (die, DW_AT_name, cu);
22507 if ((!attr || !DW_STRING (attr))
22508 && die->tag != DW_TAG_namespace
22509 && die->tag != DW_TAG_class_type
22510 && die->tag != DW_TAG_interface_type
22511 && die->tag != DW_TAG_structure_type
22512 && die->tag != DW_TAG_union_type)
22513 return NULL;
22514
22515 switch (die->tag)
22516 {
22517 case DW_TAG_compile_unit:
22518 case DW_TAG_partial_unit:
22519 /* Compilation units have a DW_AT_name that is a filename, not
22520 a source language identifier. */
22521 case DW_TAG_enumeration_type:
22522 case DW_TAG_enumerator:
22523 /* These tags always have simple identifiers already; no need
22524 to canonicalize them. */
22525 return DW_STRING (attr);
22526
22527 case DW_TAG_namespace:
22528 if (attr != NULL && DW_STRING (attr) != NULL)
22529 return DW_STRING (attr);
22530 return CP_ANONYMOUS_NAMESPACE_STR;
22531
22532 case DW_TAG_class_type:
22533 case DW_TAG_interface_type:
22534 case DW_TAG_structure_type:
22535 case DW_TAG_union_type:
22536 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22537 structures or unions. These were of the form "._%d" in GCC 4.1,
22538 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22539 and GCC 4.4. We work around this problem by ignoring these. */
22540 if (attr && DW_STRING (attr)
22541 && (startswith (DW_STRING (attr), "._")
22542 || startswith (DW_STRING (attr), "<anonymous")))
22543 return NULL;
22544
22545 /* GCC might emit a nameless typedef that has a linkage name. See
22546 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22547 if (!attr || DW_STRING (attr) == NULL)
22548 {
22549 attr = dw2_linkage_name_attr (die, cu);
22550 if (attr == NULL || DW_STRING (attr) == NULL)
22551 return NULL;
22552
22553 /* Avoid demangling DW_STRING (attr) the second time on a second
22554 call for the same DIE. */
22555 if (!DW_STRING_IS_CANONICAL (attr))
22556 {
22557 gdb::unique_xmalloc_ptr<char> demangled
22558 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22559
22560 const char *base;
22561
22562 /* FIXME: we already did this for the partial symbol... */
22563 DW_STRING (attr)
22564 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22565 demangled.get ());
22566 DW_STRING_IS_CANONICAL (attr) = 1;
22567
22568 /* Strip any leading namespaces/classes, keep only the base name.
22569 DW_AT_name for named DIEs does not contain the prefixes. */
22570 base = strrchr (DW_STRING (attr), ':');
22571 if (base && base > DW_STRING (attr) && base[-1] == ':')
22572 return &base[1];
22573 else
22574 return DW_STRING (attr);
22575 }
22576 }
22577 break;
22578
22579 default:
22580 break;
22581 }
22582
22583 if (!DW_STRING_IS_CANONICAL (attr))
22584 {
22585 DW_STRING (attr)
22586 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22587 &objfile->per_bfd->storage_obstack);
22588 DW_STRING_IS_CANONICAL (attr) = 1;
22589 }
22590 return DW_STRING (attr);
22591 }
22592
22593 /* Return the die that this die in an extension of, or NULL if there
22594 is none. *EXT_CU is the CU containing DIE on input, and the CU
22595 containing the return value on output. */
22596
22597 static struct die_info *
22598 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22599 {
22600 struct attribute *attr;
22601
22602 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22603 if (attr == NULL)
22604 return NULL;
22605
22606 return follow_die_ref (die, attr, ext_cu);
22607 }
22608
22609 /* A convenience function that returns an "unknown" DWARF name,
22610 including the value of V. STR is the name of the entity being
22611 printed, e.g., "TAG". */
22612
22613 static const char *
22614 dwarf_unknown (const char *str, unsigned v)
22615 {
22616 char *cell = get_print_cell ();
22617 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22618 return cell;
22619 }
22620
22621 /* Convert a DIE tag into its string name. */
22622
22623 static const char *
22624 dwarf_tag_name (unsigned tag)
22625 {
22626 const char *name = get_DW_TAG_name (tag);
22627
22628 if (name == NULL)
22629 return dwarf_unknown ("TAG", tag);
22630
22631 return name;
22632 }
22633
22634 /* Convert a DWARF attribute code into its string name. */
22635
22636 static const char *
22637 dwarf_attr_name (unsigned attr)
22638 {
22639 const char *name;
22640
22641 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22642 if (attr == DW_AT_MIPS_fde)
22643 return "DW_AT_MIPS_fde";
22644 #else
22645 if (attr == DW_AT_HP_block_index)
22646 return "DW_AT_HP_block_index";
22647 #endif
22648
22649 name = get_DW_AT_name (attr);
22650
22651 if (name == NULL)
22652 return dwarf_unknown ("AT", attr);
22653
22654 return name;
22655 }
22656
22657 /* Convert a unit type to corresponding DW_UT name. */
22658
22659 static const char *
22660 dwarf_unit_type_name (int unit_type) {
22661 switch (unit_type)
22662 {
22663 case 0x01:
22664 return "DW_UT_compile (0x01)";
22665 case 0x02:
22666 return "DW_UT_type (0x02)";
22667 case 0x03:
22668 return "DW_UT_partial (0x03)";
22669 case 0x04:
22670 return "DW_UT_skeleton (0x04)";
22671 case 0x05:
22672 return "DW_UT_split_compile (0x05)";
22673 case 0x06:
22674 return "DW_UT_split_type (0x06)";
22675 case 0x80:
22676 return "DW_UT_lo_user (0x80)";
22677 case 0xff:
22678 return "DW_UT_hi_user (0xff)";
22679 default:
22680 return nullptr;
22681 }
22682 }
22683
22684 /* Convert a DWARF value form code into its string name. */
22685
22686 static const char *
22687 dwarf_form_name (unsigned form)
22688 {
22689 const char *name = get_DW_FORM_name (form);
22690
22691 if (name == NULL)
22692 return dwarf_unknown ("FORM", form);
22693
22694 return name;
22695 }
22696
22697 static const char *
22698 dwarf_bool_name (unsigned mybool)
22699 {
22700 if (mybool)
22701 return "TRUE";
22702 else
22703 return "FALSE";
22704 }
22705
22706 /* Convert a DWARF type code into its string name. */
22707
22708 static const char *
22709 dwarf_type_encoding_name (unsigned enc)
22710 {
22711 const char *name = get_DW_ATE_name (enc);
22712
22713 if (name == NULL)
22714 return dwarf_unknown ("ATE", enc);
22715
22716 return name;
22717 }
22718
22719 static void
22720 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22721 {
22722 unsigned int i;
22723
22724 print_spaces (indent, f);
22725 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22726 dwarf_tag_name (die->tag), die->abbrev,
22727 sect_offset_str (die->sect_off));
22728
22729 if (die->parent != NULL)
22730 {
22731 print_spaces (indent, f);
22732 fprintf_unfiltered (f, " parent at offset: %s\n",
22733 sect_offset_str (die->parent->sect_off));
22734 }
22735
22736 print_spaces (indent, f);
22737 fprintf_unfiltered (f, " has children: %s\n",
22738 dwarf_bool_name (die->child != NULL));
22739
22740 print_spaces (indent, f);
22741 fprintf_unfiltered (f, " attributes:\n");
22742
22743 for (i = 0; i < die->num_attrs; ++i)
22744 {
22745 print_spaces (indent, f);
22746 fprintf_unfiltered (f, " %s (%s) ",
22747 dwarf_attr_name (die->attrs[i].name),
22748 dwarf_form_name (die->attrs[i].form));
22749
22750 switch (die->attrs[i].form)
22751 {
22752 case DW_FORM_addr:
22753 case DW_FORM_addrx:
22754 case DW_FORM_GNU_addr_index:
22755 fprintf_unfiltered (f, "address: ");
22756 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22757 break;
22758 case DW_FORM_block2:
22759 case DW_FORM_block4:
22760 case DW_FORM_block:
22761 case DW_FORM_block1:
22762 fprintf_unfiltered (f, "block: size %s",
22763 pulongest (DW_BLOCK (&die->attrs[i])->size));
22764 break;
22765 case DW_FORM_exprloc:
22766 fprintf_unfiltered (f, "expression: size %s",
22767 pulongest (DW_BLOCK (&die->attrs[i])->size));
22768 break;
22769 case DW_FORM_data16:
22770 fprintf_unfiltered (f, "constant of 16 bytes");
22771 break;
22772 case DW_FORM_ref_addr:
22773 fprintf_unfiltered (f, "ref address: ");
22774 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22775 break;
22776 case DW_FORM_GNU_ref_alt:
22777 fprintf_unfiltered (f, "alt ref address: ");
22778 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22779 break;
22780 case DW_FORM_ref1:
22781 case DW_FORM_ref2:
22782 case DW_FORM_ref4:
22783 case DW_FORM_ref8:
22784 case DW_FORM_ref_udata:
22785 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22786 (long) (DW_UNSND (&die->attrs[i])));
22787 break;
22788 case DW_FORM_data1:
22789 case DW_FORM_data2:
22790 case DW_FORM_data4:
22791 case DW_FORM_data8:
22792 case DW_FORM_udata:
22793 case DW_FORM_sdata:
22794 fprintf_unfiltered (f, "constant: %s",
22795 pulongest (DW_UNSND (&die->attrs[i])));
22796 break;
22797 case DW_FORM_sec_offset:
22798 fprintf_unfiltered (f, "section offset: %s",
22799 pulongest (DW_UNSND (&die->attrs[i])));
22800 break;
22801 case DW_FORM_ref_sig8:
22802 fprintf_unfiltered (f, "signature: %s",
22803 hex_string (DW_SIGNATURE (&die->attrs[i])));
22804 break;
22805 case DW_FORM_string:
22806 case DW_FORM_strp:
22807 case DW_FORM_line_strp:
22808 case DW_FORM_strx:
22809 case DW_FORM_GNU_str_index:
22810 case DW_FORM_GNU_strp_alt:
22811 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22812 DW_STRING (&die->attrs[i])
22813 ? DW_STRING (&die->attrs[i]) : "",
22814 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22815 break;
22816 case DW_FORM_flag:
22817 if (DW_UNSND (&die->attrs[i]))
22818 fprintf_unfiltered (f, "flag: TRUE");
22819 else
22820 fprintf_unfiltered (f, "flag: FALSE");
22821 break;
22822 case DW_FORM_flag_present:
22823 fprintf_unfiltered (f, "flag: TRUE");
22824 break;
22825 case DW_FORM_indirect:
22826 /* The reader will have reduced the indirect form to
22827 the "base form" so this form should not occur. */
22828 fprintf_unfiltered (f,
22829 "unexpected attribute form: DW_FORM_indirect");
22830 break;
22831 case DW_FORM_implicit_const:
22832 fprintf_unfiltered (f, "constant: %s",
22833 plongest (DW_SND (&die->attrs[i])));
22834 break;
22835 default:
22836 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22837 die->attrs[i].form);
22838 break;
22839 }
22840 fprintf_unfiltered (f, "\n");
22841 }
22842 }
22843
22844 static void
22845 dump_die_for_error (struct die_info *die)
22846 {
22847 dump_die_shallow (gdb_stderr, 0, die);
22848 }
22849
22850 static void
22851 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22852 {
22853 int indent = level * 4;
22854
22855 gdb_assert (die != NULL);
22856
22857 if (level >= max_level)
22858 return;
22859
22860 dump_die_shallow (f, indent, die);
22861
22862 if (die->child != NULL)
22863 {
22864 print_spaces (indent, f);
22865 fprintf_unfiltered (f, " Children:");
22866 if (level + 1 < max_level)
22867 {
22868 fprintf_unfiltered (f, "\n");
22869 dump_die_1 (f, level + 1, max_level, die->child);
22870 }
22871 else
22872 {
22873 fprintf_unfiltered (f,
22874 " [not printed, max nesting level reached]\n");
22875 }
22876 }
22877
22878 if (die->sibling != NULL && level > 0)
22879 {
22880 dump_die_1 (f, level, max_level, die->sibling);
22881 }
22882 }
22883
22884 /* This is called from the pdie macro in gdbinit.in.
22885 It's not static so gcc will keep a copy callable from gdb. */
22886
22887 void
22888 dump_die (struct die_info *die, int max_level)
22889 {
22890 dump_die_1 (gdb_stdlog, 0, max_level, die);
22891 }
22892
22893 static void
22894 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22895 {
22896 void **slot;
22897
22898 slot = htab_find_slot_with_hash (cu->die_hash, die,
22899 to_underlying (die->sect_off),
22900 INSERT);
22901
22902 *slot = die;
22903 }
22904
22905 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22906 required kind. */
22907
22908 static sect_offset
22909 dwarf2_get_ref_die_offset (const struct attribute *attr)
22910 {
22911 if (attr_form_is_ref (attr))
22912 return (sect_offset) DW_UNSND (attr);
22913
22914 complaint (_("unsupported die ref attribute form: '%s'"),
22915 dwarf_form_name (attr->form));
22916 return {};
22917 }
22918
22919 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22920 * the value held by the attribute is not constant. */
22921
22922 static LONGEST
22923 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22924 {
22925 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22926 return DW_SND (attr);
22927 else if (attr->form == DW_FORM_udata
22928 || attr->form == DW_FORM_data1
22929 || attr->form == DW_FORM_data2
22930 || attr->form == DW_FORM_data4
22931 || attr->form == DW_FORM_data8)
22932 return DW_UNSND (attr);
22933 else
22934 {
22935 /* For DW_FORM_data16 see attr_form_is_constant. */
22936 complaint (_("Attribute value is not a constant (%s)"),
22937 dwarf_form_name (attr->form));
22938 return default_value;
22939 }
22940 }
22941
22942 /* Follow reference or signature attribute ATTR of SRC_DIE.
22943 On entry *REF_CU is the CU of SRC_DIE.
22944 On exit *REF_CU is the CU of the result. */
22945
22946 static struct die_info *
22947 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22948 struct dwarf2_cu **ref_cu)
22949 {
22950 struct die_info *die;
22951
22952 if (attr_form_is_ref (attr))
22953 die = follow_die_ref (src_die, attr, ref_cu);
22954 else if (attr->form == DW_FORM_ref_sig8)
22955 die = follow_die_sig (src_die, attr, ref_cu);
22956 else
22957 {
22958 dump_die_for_error (src_die);
22959 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22960 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22961 }
22962
22963 return die;
22964 }
22965
22966 /* Follow reference OFFSET.
22967 On entry *REF_CU is the CU of the source die referencing OFFSET.
22968 On exit *REF_CU is the CU of the result.
22969 Returns NULL if OFFSET is invalid. */
22970
22971 static struct die_info *
22972 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22973 struct dwarf2_cu **ref_cu)
22974 {
22975 struct die_info temp_die;
22976 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22977 struct dwarf2_per_objfile *dwarf2_per_objfile
22978 = cu->per_cu->dwarf2_per_objfile;
22979
22980 gdb_assert (cu->per_cu != NULL);
22981
22982 target_cu = cu;
22983
22984 if (cu->per_cu->is_debug_types)
22985 {
22986 /* .debug_types CUs cannot reference anything outside their CU.
22987 If they need to, they have to reference a signatured type via
22988 DW_FORM_ref_sig8. */
22989 if (!offset_in_cu_p (&cu->header, sect_off))
22990 return NULL;
22991 }
22992 else if (offset_in_dwz != cu->per_cu->is_dwz
22993 || !offset_in_cu_p (&cu->header, sect_off))
22994 {
22995 struct dwarf2_per_cu_data *per_cu;
22996
22997 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22998 dwarf2_per_objfile);
22999
23000 /* If necessary, add it to the queue and load its DIEs. */
23001 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23002 load_full_comp_unit (per_cu, false, cu->language);
23003
23004 target_cu = per_cu->cu;
23005 }
23006 else if (cu->dies == NULL)
23007 {
23008 /* We're loading full DIEs during partial symbol reading. */
23009 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23010 load_full_comp_unit (cu->per_cu, false, language_minimal);
23011 }
23012
23013 *ref_cu = target_cu;
23014 temp_die.sect_off = sect_off;
23015
23016 if (target_cu != cu)
23017 target_cu->ancestor = cu;
23018
23019 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23020 &temp_die,
23021 to_underlying (sect_off));
23022 }
23023
23024 /* Follow reference attribute ATTR of SRC_DIE.
23025 On entry *REF_CU is the CU of SRC_DIE.
23026 On exit *REF_CU is the CU of the result. */
23027
23028 static struct die_info *
23029 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23030 struct dwarf2_cu **ref_cu)
23031 {
23032 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23033 struct dwarf2_cu *cu = *ref_cu;
23034 struct die_info *die;
23035
23036 die = follow_die_offset (sect_off,
23037 (attr->form == DW_FORM_GNU_ref_alt
23038 || cu->per_cu->is_dwz),
23039 ref_cu);
23040 if (!die)
23041 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23042 "at %s [in module %s]"),
23043 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23044 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23045
23046 return die;
23047 }
23048
23049 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23050 Returned value is intended for DW_OP_call*. Returned
23051 dwarf2_locexpr_baton->data has lifetime of
23052 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23053
23054 struct dwarf2_locexpr_baton
23055 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23056 struct dwarf2_per_cu_data *per_cu,
23057 CORE_ADDR (*get_frame_pc) (void *baton),
23058 void *baton, bool resolve_abstract_p)
23059 {
23060 struct dwarf2_cu *cu;
23061 struct die_info *die;
23062 struct attribute *attr;
23063 struct dwarf2_locexpr_baton retval;
23064 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23065 struct objfile *objfile = dwarf2_per_objfile->objfile;
23066
23067 if (per_cu->cu == NULL)
23068 load_cu (per_cu, false);
23069 cu = per_cu->cu;
23070 if (cu == NULL)
23071 {
23072 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23073 Instead just throw an error, not much else we can do. */
23074 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23075 sect_offset_str (sect_off), objfile_name (objfile));
23076 }
23077
23078 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23079 if (!die)
23080 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23081 sect_offset_str (sect_off), objfile_name (objfile));
23082
23083 attr = dwarf2_attr (die, DW_AT_location, cu);
23084 if (!attr && resolve_abstract_p
23085 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23086 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23087 {
23088 CORE_ADDR pc = (*get_frame_pc) (baton);
23089 CORE_ADDR baseaddr = objfile->text_section_offset ();
23090 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23091
23092 for (const auto &cand_off
23093 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23094 {
23095 struct dwarf2_cu *cand_cu = cu;
23096 struct die_info *cand
23097 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23098 if (!cand
23099 || !cand->parent
23100 || cand->parent->tag != DW_TAG_subprogram)
23101 continue;
23102
23103 CORE_ADDR pc_low, pc_high;
23104 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23105 if (pc_low == ((CORE_ADDR) -1))
23106 continue;
23107 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23108 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23109 if (!(pc_low <= pc && pc < pc_high))
23110 continue;
23111
23112 die = cand;
23113 attr = dwarf2_attr (die, DW_AT_location, cu);
23114 break;
23115 }
23116 }
23117
23118 if (!attr)
23119 {
23120 /* DWARF: "If there is no such attribute, then there is no effect.".
23121 DATA is ignored if SIZE is 0. */
23122
23123 retval.data = NULL;
23124 retval.size = 0;
23125 }
23126 else if (attr_form_is_section_offset (attr))
23127 {
23128 struct dwarf2_loclist_baton loclist_baton;
23129 CORE_ADDR pc = (*get_frame_pc) (baton);
23130 size_t size;
23131
23132 fill_in_loclist_baton (cu, &loclist_baton, attr);
23133
23134 retval.data = dwarf2_find_location_expression (&loclist_baton,
23135 &size, pc);
23136 retval.size = size;
23137 }
23138 else
23139 {
23140 if (!attr_form_is_block (attr))
23141 error (_("Dwarf Error: DIE at %s referenced in module %s "
23142 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23143 sect_offset_str (sect_off), objfile_name (objfile));
23144
23145 retval.data = DW_BLOCK (attr)->data;
23146 retval.size = DW_BLOCK (attr)->size;
23147 }
23148 retval.per_cu = cu->per_cu;
23149
23150 age_cached_comp_units (dwarf2_per_objfile);
23151
23152 return retval;
23153 }
23154
23155 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23156 offset. */
23157
23158 struct dwarf2_locexpr_baton
23159 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23160 struct dwarf2_per_cu_data *per_cu,
23161 CORE_ADDR (*get_frame_pc) (void *baton),
23162 void *baton)
23163 {
23164 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23165
23166 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23167 }
23168
23169 /* Write a constant of a given type as target-ordered bytes into
23170 OBSTACK. */
23171
23172 static const gdb_byte *
23173 write_constant_as_bytes (struct obstack *obstack,
23174 enum bfd_endian byte_order,
23175 struct type *type,
23176 ULONGEST value,
23177 LONGEST *len)
23178 {
23179 gdb_byte *result;
23180
23181 *len = TYPE_LENGTH (type);
23182 result = (gdb_byte *) obstack_alloc (obstack, *len);
23183 store_unsigned_integer (result, *len, byte_order, value);
23184
23185 return result;
23186 }
23187
23188 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23189 pointer to the constant bytes and set LEN to the length of the
23190 data. If memory is needed, allocate it on OBSTACK. If the DIE
23191 does not have a DW_AT_const_value, return NULL. */
23192
23193 const gdb_byte *
23194 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23195 struct dwarf2_per_cu_data *per_cu,
23196 struct obstack *obstack,
23197 LONGEST *len)
23198 {
23199 struct dwarf2_cu *cu;
23200 struct die_info *die;
23201 struct attribute *attr;
23202 const gdb_byte *result = NULL;
23203 struct type *type;
23204 LONGEST value;
23205 enum bfd_endian byte_order;
23206 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23207
23208 if (per_cu->cu == NULL)
23209 load_cu (per_cu, false);
23210 cu = per_cu->cu;
23211 if (cu == NULL)
23212 {
23213 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23214 Instead just throw an error, not much else we can do. */
23215 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23216 sect_offset_str (sect_off), objfile_name (objfile));
23217 }
23218
23219 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23220 if (!die)
23221 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23222 sect_offset_str (sect_off), objfile_name (objfile));
23223
23224 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23225 if (attr == NULL)
23226 return NULL;
23227
23228 byte_order = (bfd_big_endian (objfile->obfd)
23229 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23230
23231 switch (attr->form)
23232 {
23233 case DW_FORM_addr:
23234 case DW_FORM_addrx:
23235 case DW_FORM_GNU_addr_index:
23236 {
23237 gdb_byte *tem;
23238
23239 *len = cu->header.addr_size;
23240 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23241 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23242 result = tem;
23243 }
23244 break;
23245 case DW_FORM_string:
23246 case DW_FORM_strp:
23247 case DW_FORM_strx:
23248 case DW_FORM_GNU_str_index:
23249 case DW_FORM_GNU_strp_alt:
23250 /* DW_STRING is already allocated on the objfile obstack, point
23251 directly to it. */
23252 result = (const gdb_byte *) DW_STRING (attr);
23253 *len = strlen (DW_STRING (attr));
23254 break;
23255 case DW_FORM_block1:
23256 case DW_FORM_block2:
23257 case DW_FORM_block4:
23258 case DW_FORM_block:
23259 case DW_FORM_exprloc:
23260 case DW_FORM_data16:
23261 result = DW_BLOCK (attr)->data;
23262 *len = DW_BLOCK (attr)->size;
23263 break;
23264
23265 /* The DW_AT_const_value attributes are supposed to carry the
23266 symbol's value "represented as it would be on the target
23267 architecture." By the time we get here, it's already been
23268 converted to host endianness, so we just need to sign- or
23269 zero-extend it as appropriate. */
23270 case DW_FORM_data1:
23271 type = die_type (die, cu);
23272 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23273 if (result == NULL)
23274 result = write_constant_as_bytes (obstack, byte_order,
23275 type, value, len);
23276 break;
23277 case DW_FORM_data2:
23278 type = die_type (die, cu);
23279 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23280 if (result == NULL)
23281 result = write_constant_as_bytes (obstack, byte_order,
23282 type, value, len);
23283 break;
23284 case DW_FORM_data4:
23285 type = die_type (die, cu);
23286 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23287 if (result == NULL)
23288 result = write_constant_as_bytes (obstack, byte_order,
23289 type, value, len);
23290 break;
23291 case DW_FORM_data8:
23292 type = die_type (die, cu);
23293 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23294 if (result == NULL)
23295 result = write_constant_as_bytes (obstack, byte_order,
23296 type, value, len);
23297 break;
23298
23299 case DW_FORM_sdata:
23300 case DW_FORM_implicit_const:
23301 type = die_type (die, cu);
23302 result = write_constant_as_bytes (obstack, byte_order,
23303 type, DW_SND (attr), len);
23304 break;
23305
23306 case DW_FORM_udata:
23307 type = die_type (die, cu);
23308 result = write_constant_as_bytes (obstack, byte_order,
23309 type, DW_UNSND (attr), len);
23310 break;
23311
23312 default:
23313 complaint (_("unsupported const value attribute form: '%s'"),
23314 dwarf_form_name (attr->form));
23315 break;
23316 }
23317
23318 return result;
23319 }
23320
23321 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23322 valid type for this die is found. */
23323
23324 struct type *
23325 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23326 struct dwarf2_per_cu_data *per_cu)
23327 {
23328 struct dwarf2_cu *cu;
23329 struct die_info *die;
23330
23331 if (per_cu->cu == NULL)
23332 load_cu (per_cu, false);
23333 cu = per_cu->cu;
23334 if (!cu)
23335 return NULL;
23336
23337 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23338 if (!die)
23339 return NULL;
23340
23341 return die_type (die, cu);
23342 }
23343
23344 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23345 PER_CU. */
23346
23347 struct type *
23348 dwarf2_get_die_type (cu_offset die_offset,
23349 struct dwarf2_per_cu_data *per_cu)
23350 {
23351 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23352 return get_die_type_at_offset (die_offset_sect, per_cu);
23353 }
23354
23355 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23356 On entry *REF_CU is the CU of SRC_DIE.
23357 On exit *REF_CU is the CU of the result.
23358 Returns NULL if the referenced DIE isn't found. */
23359
23360 static struct die_info *
23361 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23362 struct dwarf2_cu **ref_cu)
23363 {
23364 struct die_info temp_die;
23365 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23366 struct die_info *die;
23367
23368 /* While it might be nice to assert sig_type->type == NULL here,
23369 we can get here for DW_AT_imported_declaration where we need
23370 the DIE not the type. */
23371
23372 /* If necessary, add it to the queue and load its DIEs. */
23373
23374 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23375 read_signatured_type (sig_type);
23376
23377 sig_cu = sig_type->per_cu.cu;
23378 gdb_assert (sig_cu != NULL);
23379 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23380 temp_die.sect_off = sig_type->type_offset_in_section;
23381 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23382 to_underlying (temp_die.sect_off));
23383 if (die)
23384 {
23385 struct dwarf2_per_objfile *dwarf2_per_objfile
23386 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23387
23388 /* For .gdb_index version 7 keep track of included TUs.
23389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23390 if (dwarf2_per_objfile->index_table != NULL
23391 && dwarf2_per_objfile->index_table->version <= 7)
23392 {
23393 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23394 }
23395
23396 *ref_cu = sig_cu;
23397 if (sig_cu != cu)
23398 sig_cu->ancestor = cu;
23399
23400 return die;
23401 }
23402
23403 return NULL;
23404 }
23405
23406 /* Follow signatured type referenced by ATTR in SRC_DIE.
23407 On entry *REF_CU is the CU of SRC_DIE.
23408 On exit *REF_CU is the CU of the result.
23409 The result is the DIE of the type.
23410 If the referenced type cannot be found an error is thrown. */
23411
23412 static struct die_info *
23413 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23414 struct dwarf2_cu **ref_cu)
23415 {
23416 ULONGEST signature = DW_SIGNATURE (attr);
23417 struct signatured_type *sig_type;
23418 struct die_info *die;
23419
23420 gdb_assert (attr->form == DW_FORM_ref_sig8);
23421
23422 sig_type = lookup_signatured_type (*ref_cu, signature);
23423 /* sig_type will be NULL if the signatured type is missing from
23424 the debug info. */
23425 if (sig_type == NULL)
23426 {
23427 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23428 " from DIE at %s [in module %s]"),
23429 hex_string (signature), sect_offset_str (src_die->sect_off),
23430 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23431 }
23432
23433 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23434 if (die == NULL)
23435 {
23436 dump_die_for_error (src_die);
23437 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23438 " from DIE at %s [in module %s]"),
23439 hex_string (signature), sect_offset_str (src_die->sect_off),
23440 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23441 }
23442
23443 return die;
23444 }
23445
23446 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23447 reading in and processing the type unit if necessary. */
23448
23449 static struct type *
23450 get_signatured_type (struct die_info *die, ULONGEST signature,
23451 struct dwarf2_cu *cu)
23452 {
23453 struct dwarf2_per_objfile *dwarf2_per_objfile
23454 = cu->per_cu->dwarf2_per_objfile;
23455 struct signatured_type *sig_type;
23456 struct dwarf2_cu *type_cu;
23457 struct die_info *type_die;
23458 struct type *type;
23459
23460 sig_type = lookup_signatured_type (cu, signature);
23461 /* sig_type will be NULL if the signatured type is missing from
23462 the debug info. */
23463 if (sig_type == NULL)
23464 {
23465 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23466 " from DIE at %s [in module %s]"),
23467 hex_string (signature), sect_offset_str (die->sect_off),
23468 objfile_name (dwarf2_per_objfile->objfile));
23469 return build_error_marker_type (cu, die);
23470 }
23471
23472 /* If we already know the type we're done. */
23473 if (sig_type->type != NULL)
23474 return sig_type->type;
23475
23476 type_cu = cu;
23477 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23478 if (type_die != NULL)
23479 {
23480 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23481 is created. This is important, for example, because for c++ classes
23482 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23483 type = read_type_die (type_die, type_cu);
23484 if (type == NULL)
23485 {
23486 complaint (_("Dwarf Error: Cannot build signatured type %s"
23487 " referenced from DIE at %s [in module %s]"),
23488 hex_string (signature), sect_offset_str (die->sect_off),
23489 objfile_name (dwarf2_per_objfile->objfile));
23490 type = build_error_marker_type (cu, die);
23491 }
23492 }
23493 else
23494 {
23495 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23496 " from DIE at %s [in module %s]"),
23497 hex_string (signature), sect_offset_str (die->sect_off),
23498 objfile_name (dwarf2_per_objfile->objfile));
23499 type = build_error_marker_type (cu, die);
23500 }
23501 sig_type->type = type;
23502
23503 return type;
23504 }
23505
23506 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23507 reading in and processing the type unit if necessary. */
23508
23509 static struct type *
23510 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23511 struct dwarf2_cu *cu) /* ARI: editCase function */
23512 {
23513 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23514 if (attr_form_is_ref (attr))
23515 {
23516 struct dwarf2_cu *type_cu = cu;
23517 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23518
23519 return read_type_die (type_die, type_cu);
23520 }
23521 else if (attr->form == DW_FORM_ref_sig8)
23522 {
23523 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23524 }
23525 else
23526 {
23527 struct dwarf2_per_objfile *dwarf2_per_objfile
23528 = cu->per_cu->dwarf2_per_objfile;
23529
23530 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23531 " at %s [in module %s]"),
23532 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23533 objfile_name (dwarf2_per_objfile->objfile));
23534 return build_error_marker_type (cu, die);
23535 }
23536 }
23537
23538 /* Load the DIEs associated with type unit PER_CU into memory. */
23539
23540 static void
23541 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23542 {
23543 struct signatured_type *sig_type;
23544
23545 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23546 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23547
23548 /* We have the per_cu, but we need the signatured_type.
23549 Fortunately this is an easy translation. */
23550 gdb_assert (per_cu->is_debug_types);
23551 sig_type = (struct signatured_type *) per_cu;
23552
23553 gdb_assert (per_cu->cu == NULL);
23554
23555 read_signatured_type (sig_type);
23556
23557 gdb_assert (per_cu->cu != NULL);
23558 }
23559
23560 /* Read in a signatured type and build its CU and DIEs.
23561 If the type is a stub for the real type in a DWO file,
23562 read in the real type from the DWO file as well. */
23563
23564 static void
23565 read_signatured_type (struct signatured_type *sig_type)
23566 {
23567 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23568
23569 gdb_assert (per_cu->is_debug_types);
23570 gdb_assert (per_cu->cu == NULL);
23571
23572 cutu_reader reader (per_cu, NULL, 0, 1, false);
23573
23574 if (!reader.dummy_p)
23575 {
23576 struct dwarf2_cu *cu = reader.cu;
23577 const gdb_byte *info_ptr = reader.info_ptr;
23578
23579 gdb_assert (cu->die_hash == NULL);
23580 cu->die_hash =
23581 htab_create_alloc_ex (cu->header.length / 12,
23582 die_hash,
23583 die_eq,
23584 NULL,
23585 &cu->comp_unit_obstack,
23586 hashtab_obstack_allocate,
23587 dummy_obstack_deallocate);
23588
23589 if (reader.has_children)
23590 reader.comp_unit_die->child
23591 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23592 reader.comp_unit_die);
23593 cu->dies = reader.comp_unit_die;
23594 /* comp_unit_die is not stored in die_hash, no need. */
23595
23596 /* We try not to read any attributes in this function, because
23597 not all CUs needed for references have been loaded yet, and
23598 symbol table processing isn't initialized. But we have to
23599 set the CU language, or we won't be able to build types
23600 correctly. Similarly, if we do not read the producer, we can
23601 not apply producer-specific interpretation. */
23602 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23603 }
23604
23605 sig_type->per_cu.tu_read = 1;
23606 }
23607
23608 /* Decode simple location descriptions.
23609 Given a pointer to a dwarf block that defines a location, compute
23610 the location and return the value.
23611
23612 NOTE drow/2003-11-18: This function is called in two situations
23613 now: for the address of static or global variables (partial symbols
23614 only) and for offsets into structures which are expected to be
23615 (more or less) constant. The partial symbol case should go away,
23616 and only the constant case should remain. That will let this
23617 function complain more accurately. A few special modes are allowed
23618 without complaint for global variables (for instance, global
23619 register values and thread-local values).
23620
23621 A location description containing no operations indicates that the
23622 object is optimized out. The return value is 0 for that case.
23623 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23624 callers will only want a very basic result and this can become a
23625 complaint.
23626
23627 Note that stack[0] is unused except as a default error return. */
23628
23629 static CORE_ADDR
23630 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23631 {
23632 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23633 size_t i;
23634 size_t size = blk->size;
23635 const gdb_byte *data = blk->data;
23636 CORE_ADDR stack[64];
23637 int stacki;
23638 unsigned int bytes_read, unsnd;
23639 gdb_byte op;
23640
23641 i = 0;
23642 stacki = 0;
23643 stack[stacki] = 0;
23644 stack[++stacki] = 0;
23645
23646 while (i < size)
23647 {
23648 op = data[i++];
23649 switch (op)
23650 {
23651 case DW_OP_lit0:
23652 case DW_OP_lit1:
23653 case DW_OP_lit2:
23654 case DW_OP_lit3:
23655 case DW_OP_lit4:
23656 case DW_OP_lit5:
23657 case DW_OP_lit6:
23658 case DW_OP_lit7:
23659 case DW_OP_lit8:
23660 case DW_OP_lit9:
23661 case DW_OP_lit10:
23662 case DW_OP_lit11:
23663 case DW_OP_lit12:
23664 case DW_OP_lit13:
23665 case DW_OP_lit14:
23666 case DW_OP_lit15:
23667 case DW_OP_lit16:
23668 case DW_OP_lit17:
23669 case DW_OP_lit18:
23670 case DW_OP_lit19:
23671 case DW_OP_lit20:
23672 case DW_OP_lit21:
23673 case DW_OP_lit22:
23674 case DW_OP_lit23:
23675 case DW_OP_lit24:
23676 case DW_OP_lit25:
23677 case DW_OP_lit26:
23678 case DW_OP_lit27:
23679 case DW_OP_lit28:
23680 case DW_OP_lit29:
23681 case DW_OP_lit30:
23682 case DW_OP_lit31:
23683 stack[++stacki] = op - DW_OP_lit0;
23684 break;
23685
23686 case DW_OP_reg0:
23687 case DW_OP_reg1:
23688 case DW_OP_reg2:
23689 case DW_OP_reg3:
23690 case DW_OP_reg4:
23691 case DW_OP_reg5:
23692 case DW_OP_reg6:
23693 case DW_OP_reg7:
23694 case DW_OP_reg8:
23695 case DW_OP_reg9:
23696 case DW_OP_reg10:
23697 case DW_OP_reg11:
23698 case DW_OP_reg12:
23699 case DW_OP_reg13:
23700 case DW_OP_reg14:
23701 case DW_OP_reg15:
23702 case DW_OP_reg16:
23703 case DW_OP_reg17:
23704 case DW_OP_reg18:
23705 case DW_OP_reg19:
23706 case DW_OP_reg20:
23707 case DW_OP_reg21:
23708 case DW_OP_reg22:
23709 case DW_OP_reg23:
23710 case DW_OP_reg24:
23711 case DW_OP_reg25:
23712 case DW_OP_reg26:
23713 case DW_OP_reg27:
23714 case DW_OP_reg28:
23715 case DW_OP_reg29:
23716 case DW_OP_reg30:
23717 case DW_OP_reg31:
23718 stack[++stacki] = op - DW_OP_reg0;
23719 if (i < size)
23720 dwarf2_complex_location_expr_complaint ();
23721 break;
23722
23723 case DW_OP_regx:
23724 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23725 i += bytes_read;
23726 stack[++stacki] = unsnd;
23727 if (i < size)
23728 dwarf2_complex_location_expr_complaint ();
23729 break;
23730
23731 case DW_OP_addr:
23732 stack[++stacki] = read_address (objfile->obfd, &data[i],
23733 cu, &bytes_read);
23734 i += bytes_read;
23735 break;
23736
23737 case DW_OP_const1u:
23738 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23739 i += 1;
23740 break;
23741
23742 case DW_OP_const1s:
23743 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23744 i += 1;
23745 break;
23746
23747 case DW_OP_const2u:
23748 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23749 i += 2;
23750 break;
23751
23752 case DW_OP_const2s:
23753 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23754 i += 2;
23755 break;
23756
23757 case DW_OP_const4u:
23758 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23759 i += 4;
23760 break;
23761
23762 case DW_OP_const4s:
23763 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23764 i += 4;
23765 break;
23766
23767 case DW_OP_const8u:
23768 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23769 i += 8;
23770 break;
23771
23772 case DW_OP_constu:
23773 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23774 &bytes_read);
23775 i += bytes_read;
23776 break;
23777
23778 case DW_OP_consts:
23779 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23780 i += bytes_read;
23781 break;
23782
23783 case DW_OP_dup:
23784 stack[stacki + 1] = stack[stacki];
23785 stacki++;
23786 break;
23787
23788 case DW_OP_plus:
23789 stack[stacki - 1] += stack[stacki];
23790 stacki--;
23791 break;
23792
23793 case DW_OP_plus_uconst:
23794 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23795 &bytes_read);
23796 i += bytes_read;
23797 break;
23798
23799 case DW_OP_minus:
23800 stack[stacki - 1] -= stack[stacki];
23801 stacki--;
23802 break;
23803
23804 case DW_OP_deref:
23805 /* If we're not the last op, then we definitely can't encode
23806 this using GDB's address_class enum. This is valid for partial
23807 global symbols, although the variable's address will be bogus
23808 in the psymtab. */
23809 if (i < size)
23810 dwarf2_complex_location_expr_complaint ();
23811 break;
23812
23813 case DW_OP_GNU_push_tls_address:
23814 case DW_OP_form_tls_address:
23815 /* The top of the stack has the offset from the beginning
23816 of the thread control block at which the variable is located. */
23817 /* Nothing should follow this operator, so the top of stack would
23818 be returned. */
23819 /* This is valid for partial global symbols, but the variable's
23820 address will be bogus in the psymtab. Make it always at least
23821 non-zero to not look as a variable garbage collected by linker
23822 which have DW_OP_addr 0. */
23823 if (i < size)
23824 dwarf2_complex_location_expr_complaint ();
23825 stack[stacki]++;
23826 break;
23827
23828 case DW_OP_GNU_uninit:
23829 break;
23830
23831 case DW_OP_addrx:
23832 case DW_OP_GNU_addr_index:
23833 case DW_OP_GNU_const_index:
23834 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23835 &bytes_read);
23836 i += bytes_read;
23837 break;
23838
23839 default:
23840 {
23841 const char *name = get_DW_OP_name (op);
23842
23843 if (name)
23844 complaint (_("unsupported stack op: '%s'"),
23845 name);
23846 else
23847 complaint (_("unsupported stack op: '%02x'"),
23848 op);
23849 }
23850
23851 return (stack[stacki]);
23852 }
23853
23854 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23855 outside of the allocated space. Also enforce minimum>0. */
23856 if (stacki >= ARRAY_SIZE (stack) - 1)
23857 {
23858 complaint (_("location description stack overflow"));
23859 return 0;
23860 }
23861
23862 if (stacki <= 0)
23863 {
23864 complaint (_("location description stack underflow"));
23865 return 0;
23866 }
23867 }
23868 return (stack[stacki]);
23869 }
23870
23871 /* memory allocation interface */
23872
23873 static struct dwarf_block *
23874 dwarf_alloc_block (struct dwarf2_cu *cu)
23875 {
23876 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23877 }
23878
23879 static struct die_info *
23880 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23881 {
23882 struct die_info *die;
23883 size_t size = sizeof (struct die_info);
23884
23885 if (num_attrs > 1)
23886 size += (num_attrs - 1) * sizeof (struct attribute);
23887
23888 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23889 memset (die, 0, sizeof (struct die_info));
23890 return (die);
23891 }
23892
23893 \f
23894 /* Macro support. */
23895
23896 /* Return file name relative to the compilation directory of file number I in
23897 *LH's file name table. The result is allocated using xmalloc; the caller is
23898 responsible for freeing it. */
23899
23900 static char *
23901 file_file_name (int file, struct line_header *lh)
23902 {
23903 /* Is the file number a valid index into the line header's file name
23904 table? Remember that file numbers start with one, not zero. */
23905 if (lh->is_valid_file_index (file))
23906 {
23907 const file_entry *fe = lh->file_name_at (file);
23908
23909 if (!IS_ABSOLUTE_PATH (fe->name))
23910 {
23911 const char *dir = fe->include_dir (lh);
23912 if (dir != NULL)
23913 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
23914 }
23915 return xstrdup (fe->name);
23916 }
23917 else
23918 {
23919 /* The compiler produced a bogus file number. We can at least
23920 record the macro definitions made in the file, even if we
23921 won't be able to find the file by name. */
23922 char fake_name[80];
23923
23924 xsnprintf (fake_name, sizeof (fake_name),
23925 "<bad macro file number %d>", file);
23926
23927 complaint (_("bad file number in macro information (%d)"),
23928 file);
23929
23930 return xstrdup (fake_name);
23931 }
23932 }
23933
23934 /* Return the full name of file number I in *LH's file name table.
23935 Use COMP_DIR as the name of the current directory of the
23936 compilation. The result is allocated using xmalloc; the caller is
23937 responsible for freeing it. */
23938 static char *
23939 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23940 {
23941 /* Is the file number a valid index into the line header's file name
23942 table? Remember that file numbers start with one, not zero. */
23943 if (lh->is_valid_file_index (file))
23944 {
23945 char *relative = file_file_name (file, lh);
23946
23947 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23948 return relative;
23949 return reconcat (relative, comp_dir, SLASH_STRING,
23950 relative, (char *) NULL);
23951 }
23952 else
23953 return file_file_name (file, lh);
23954 }
23955
23956
23957 static struct macro_source_file *
23958 macro_start_file (struct dwarf2_cu *cu,
23959 int file, int line,
23960 struct macro_source_file *current_file,
23961 struct line_header *lh)
23962 {
23963 /* File name relative to the compilation directory of this source file. */
23964 char *file_name = file_file_name (file, lh);
23965
23966 if (! current_file)
23967 {
23968 /* Note: We don't create a macro table for this compilation unit
23969 at all until we actually get a filename. */
23970 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23971
23972 /* If we have no current file, then this must be the start_file
23973 directive for the compilation unit's main source file. */
23974 current_file = macro_set_main (macro_table, file_name);
23975 macro_define_special (macro_table);
23976 }
23977 else
23978 current_file = macro_include (current_file, line, file_name);
23979
23980 xfree (file_name);
23981
23982 return current_file;
23983 }
23984
23985 static const char *
23986 consume_improper_spaces (const char *p, const char *body)
23987 {
23988 if (*p == ' ')
23989 {
23990 complaint (_("macro definition contains spaces "
23991 "in formal argument list:\n`%s'"),
23992 body);
23993
23994 while (*p == ' ')
23995 p++;
23996 }
23997
23998 return p;
23999 }
24000
24001
24002 static void
24003 parse_macro_definition (struct macro_source_file *file, int line,
24004 const char *body)
24005 {
24006 const char *p;
24007
24008 /* The body string takes one of two forms. For object-like macro
24009 definitions, it should be:
24010
24011 <macro name> " " <definition>
24012
24013 For function-like macro definitions, it should be:
24014
24015 <macro name> "() " <definition>
24016 or
24017 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24018
24019 Spaces may appear only where explicitly indicated, and in the
24020 <definition>.
24021
24022 The Dwarf 2 spec says that an object-like macro's name is always
24023 followed by a space, but versions of GCC around March 2002 omit
24024 the space when the macro's definition is the empty string.
24025
24026 The Dwarf 2 spec says that there should be no spaces between the
24027 formal arguments in a function-like macro's formal argument list,
24028 but versions of GCC around March 2002 include spaces after the
24029 commas. */
24030
24031
24032 /* Find the extent of the macro name. The macro name is terminated
24033 by either a space or null character (for an object-like macro) or
24034 an opening paren (for a function-like macro). */
24035 for (p = body; *p; p++)
24036 if (*p == ' ' || *p == '(')
24037 break;
24038
24039 if (*p == ' ' || *p == '\0')
24040 {
24041 /* It's an object-like macro. */
24042 int name_len = p - body;
24043 std::string name (body, name_len);
24044 const char *replacement;
24045
24046 if (*p == ' ')
24047 replacement = body + name_len + 1;
24048 else
24049 {
24050 dwarf2_macro_malformed_definition_complaint (body);
24051 replacement = body + name_len;
24052 }
24053
24054 macro_define_object (file, line, name.c_str (), replacement);
24055 }
24056 else if (*p == '(')
24057 {
24058 /* It's a function-like macro. */
24059 std::string name (body, p - body);
24060 int argc = 0;
24061 int argv_size = 1;
24062 char **argv = XNEWVEC (char *, argv_size);
24063
24064 p++;
24065
24066 p = consume_improper_spaces (p, body);
24067
24068 /* Parse the formal argument list. */
24069 while (*p && *p != ')')
24070 {
24071 /* Find the extent of the current argument name. */
24072 const char *arg_start = p;
24073
24074 while (*p && *p != ',' && *p != ')' && *p != ' ')
24075 p++;
24076
24077 if (! *p || p == arg_start)
24078 dwarf2_macro_malformed_definition_complaint (body);
24079 else
24080 {
24081 /* Make sure argv has room for the new argument. */
24082 if (argc >= argv_size)
24083 {
24084 argv_size *= 2;
24085 argv = XRESIZEVEC (char *, argv, argv_size);
24086 }
24087
24088 argv[argc++] = savestring (arg_start, p - arg_start);
24089 }
24090
24091 p = consume_improper_spaces (p, body);
24092
24093 /* Consume the comma, if present. */
24094 if (*p == ',')
24095 {
24096 p++;
24097
24098 p = consume_improper_spaces (p, body);
24099 }
24100 }
24101
24102 if (*p == ')')
24103 {
24104 p++;
24105
24106 if (*p == ' ')
24107 /* Perfectly formed definition, no complaints. */
24108 macro_define_function (file, line, name.c_str (),
24109 argc, (const char **) argv,
24110 p + 1);
24111 else if (*p == '\0')
24112 {
24113 /* Complain, but do define it. */
24114 dwarf2_macro_malformed_definition_complaint (body);
24115 macro_define_function (file, line, name.c_str (),
24116 argc, (const char **) argv,
24117 p);
24118 }
24119 else
24120 /* Just complain. */
24121 dwarf2_macro_malformed_definition_complaint (body);
24122 }
24123 else
24124 /* Just complain. */
24125 dwarf2_macro_malformed_definition_complaint (body);
24126
24127 {
24128 int i;
24129
24130 for (i = 0; i < argc; i++)
24131 xfree (argv[i]);
24132 }
24133 xfree (argv);
24134 }
24135 else
24136 dwarf2_macro_malformed_definition_complaint (body);
24137 }
24138
24139 /* Skip some bytes from BYTES according to the form given in FORM.
24140 Returns the new pointer. */
24141
24142 static const gdb_byte *
24143 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24144 enum dwarf_form form,
24145 unsigned int offset_size,
24146 struct dwarf2_section_info *section)
24147 {
24148 unsigned int bytes_read;
24149
24150 switch (form)
24151 {
24152 case DW_FORM_data1:
24153 case DW_FORM_flag:
24154 ++bytes;
24155 break;
24156
24157 case DW_FORM_data2:
24158 bytes += 2;
24159 break;
24160
24161 case DW_FORM_data4:
24162 bytes += 4;
24163 break;
24164
24165 case DW_FORM_data8:
24166 bytes += 8;
24167 break;
24168
24169 case DW_FORM_data16:
24170 bytes += 16;
24171 break;
24172
24173 case DW_FORM_string:
24174 read_direct_string (abfd, bytes, &bytes_read);
24175 bytes += bytes_read;
24176 break;
24177
24178 case DW_FORM_sec_offset:
24179 case DW_FORM_strp:
24180 case DW_FORM_GNU_strp_alt:
24181 bytes += offset_size;
24182 break;
24183
24184 case DW_FORM_block:
24185 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24186 bytes += bytes_read;
24187 break;
24188
24189 case DW_FORM_block1:
24190 bytes += 1 + read_1_byte (abfd, bytes);
24191 break;
24192 case DW_FORM_block2:
24193 bytes += 2 + read_2_bytes (abfd, bytes);
24194 break;
24195 case DW_FORM_block4:
24196 bytes += 4 + read_4_bytes (abfd, bytes);
24197 break;
24198
24199 case DW_FORM_addrx:
24200 case DW_FORM_sdata:
24201 case DW_FORM_strx:
24202 case DW_FORM_udata:
24203 case DW_FORM_GNU_addr_index:
24204 case DW_FORM_GNU_str_index:
24205 bytes = gdb_skip_leb128 (bytes, buffer_end);
24206 if (bytes == NULL)
24207 {
24208 dwarf2_section_buffer_overflow_complaint (section);
24209 return NULL;
24210 }
24211 break;
24212
24213 case DW_FORM_implicit_const:
24214 break;
24215
24216 default:
24217 {
24218 complaint (_("invalid form 0x%x in `%s'"),
24219 form, section->get_name ());
24220 return NULL;
24221 }
24222 }
24223
24224 return bytes;
24225 }
24226
24227 /* A helper for dwarf_decode_macros that handles skipping an unknown
24228 opcode. Returns an updated pointer to the macro data buffer; or,
24229 on error, issues a complaint and returns NULL. */
24230
24231 static const gdb_byte *
24232 skip_unknown_opcode (unsigned int opcode,
24233 const gdb_byte **opcode_definitions,
24234 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24235 bfd *abfd,
24236 unsigned int offset_size,
24237 struct dwarf2_section_info *section)
24238 {
24239 unsigned int bytes_read, i;
24240 unsigned long arg;
24241 const gdb_byte *defn;
24242
24243 if (opcode_definitions[opcode] == NULL)
24244 {
24245 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24246 opcode);
24247 return NULL;
24248 }
24249
24250 defn = opcode_definitions[opcode];
24251 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24252 defn += bytes_read;
24253
24254 for (i = 0; i < arg; ++i)
24255 {
24256 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24257 (enum dwarf_form) defn[i], offset_size,
24258 section);
24259 if (mac_ptr == NULL)
24260 {
24261 /* skip_form_bytes already issued the complaint. */
24262 return NULL;
24263 }
24264 }
24265
24266 return mac_ptr;
24267 }
24268
24269 /* A helper function which parses the header of a macro section.
24270 If the macro section is the extended (for now called "GNU") type,
24271 then this updates *OFFSET_SIZE. Returns a pointer to just after
24272 the header, or issues a complaint and returns NULL on error. */
24273
24274 static const gdb_byte *
24275 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24276 bfd *abfd,
24277 const gdb_byte *mac_ptr,
24278 unsigned int *offset_size,
24279 int section_is_gnu)
24280 {
24281 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24282
24283 if (section_is_gnu)
24284 {
24285 unsigned int version, flags;
24286
24287 version = read_2_bytes (abfd, mac_ptr);
24288 if (version != 4 && version != 5)
24289 {
24290 complaint (_("unrecognized version `%d' in .debug_macro section"),
24291 version);
24292 return NULL;
24293 }
24294 mac_ptr += 2;
24295
24296 flags = read_1_byte (abfd, mac_ptr);
24297 ++mac_ptr;
24298 *offset_size = (flags & 1) ? 8 : 4;
24299
24300 if ((flags & 2) != 0)
24301 /* We don't need the line table offset. */
24302 mac_ptr += *offset_size;
24303
24304 /* Vendor opcode descriptions. */
24305 if ((flags & 4) != 0)
24306 {
24307 unsigned int i, count;
24308
24309 count = read_1_byte (abfd, mac_ptr);
24310 ++mac_ptr;
24311 for (i = 0; i < count; ++i)
24312 {
24313 unsigned int opcode, bytes_read;
24314 unsigned long arg;
24315
24316 opcode = read_1_byte (abfd, mac_ptr);
24317 ++mac_ptr;
24318 opcode_definitions[opcode] = mac_ptr;
24319 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24320 mac_ptr += bytes_read;
24321 mac_ptr += arg;
24322 }
24323 }
24324 }
24325
24326 return mac_ptr;
24327 }
24328
24329 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24330 including DW_MACRO_import. */
24331
24332 static void
24333 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24334 bfd *abfd,
24335 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24336 struct macro_source_file *current_file,
24337 struct line_header *lh,
24338 struct dwarf2_section_info *section,
24339 int section_is_gnu, int section_is_dwz,
24340 unsigned int offset_size,
24341 htab_t include_hash)
24342 {
24343 struct dwarf2_per_objfile *dwarf2_per_objfile
24344 = cu->per_cu->dwarf2_per_objfile;
24345 struct objfile *objfile = dwarf2_per_objfile->objfile;
24346 enum dwarf_macro_record_type macinfo_type;
24347 int at_commandline;
24348 const gdb_byte *opcode_definitions[256];
24349
24350 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24351 &offset_size, section_is_gnu);
24352 if (mac_ptr == NULL)
24353 {
24354 /* We already issued a complaint. */
24355 return;
24356 }
24357
24358 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24359 GDB is still reading the definitions from command line. First
24360 DW_MACINFO_start_file will need to be ignored as it was already executed
24361 to create CURRENT_FILE for the main source holding also the command line
24362 definitions. On first met DW_MACINFO_start_file this flag is reset to
24363 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24364
24365 at_commandline = 1;
24366
24367 do
24368 {
24369 /* Do we at least have room for a macinfo type byte? */
24370 if (mac_ptr >= mac_end)
24371 {
24372 dwarf2_section_buffer_overflow_complaint (section);
24373 break;
24374 }
24375
24376 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24377 mac_ptr++;
24378
24379 /* Note that we rely on the fact that the corresponding GNU and
24380 DWARF constants are the same. */
24381 DIAGNOSTIC_PUSH
24382 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24383 switch (macinfo_type)
24384 {
24385 /* A zero macinfo type indicates the end of the macro
24386 information. */
24387 case 0:
24388 break;
24389
24390 case DW_MACRO_define:
24391 case DW_MACRO_undef:
24392 case DW_MACRO_define_strp:
24393 case DW_MACRO_undef_strp:
24394 case DW_MACRO_define_sup:
24395 case DW_MACRO_undef_sup:
24396 {
24397 unsigned int bytes_read;
24398 int line;
24399 const char *body;
24400 int is_define;
24401
24402 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24403 mac_ptr += bytes_read;
24404
24405 if (macinfo_type == DW_MACRO_define
24406 || macinfo_type == DW_MACRO_undef)
24407 {
24408 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24409 mac_ptr += bytes_read;
24410 }
24411 else
24412 {
24413 LONGEST str_offset;
24414
24415 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24416 mac_ptr += offset_size;
24417
24418 if (macinfo_type == DW_MACRO_define_sup
24419 || macinfo_type == DW_MACRO_undef_sup
24420 || section_is_dwz)
24421 {
24422 struct dwz_file *dwz
24423 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24424
24425 body = read_indirect_string_from_dwz (objfile,
24426 dwz, str_offset);
24427 }
24428 else
24429 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24430 abfd, str_offset);
24431 }
24432
24433 is_define = (macinfo_type == DW_MACRO_define
24434 || macinfo_type == DW_MACRO_define_strp
24435 || macinfo_type == DW_MACRO_define_sup);
24436 if (! current_file)
24437 {
24438 /* DWARF violation as no main source is present. */
24439 complaint (_("debug info with no main source gives macro %s "
24440 "on line %d: %s"),
24441 is_define ? _("definition") : _("undefinition"),
24442 line, body);
24443 break;
24444 }
24445 if ((line == 0 && !at_commandline)
24446 || (line != 0 && at_commandline))
24447 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24448 at_commandline ? _("command-line") : _("in-file"),
24449 is_define ? _("definition") : _("undefinition"),
24450 line == 0 ? _("zero") : _("non-zero"), line, body);
24451
24452 if (body == NULL)
24453 {
24454 /* Fedora's rpm-build's "debugedit" binary
24455 corrupted .debug_macro sections.
24456
24457 For more info, see
24458 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24459 complaint (_("debug info gives %s invalid macro %s "
24460 "without body (corrupted?) at line %d "
24461 "on file %s"),
24462 at_commandline ? _("command-line") : _("in-file"),
24463 is_define ? _("definition") : _("undefinition"),
24464 line, current_file->filename);
24465 }
24466 else if (is_define)
24467 parse_macro_definition (current_file, line, body);
24468 else
24469 {
24470 gdb_assert (macinfo_type == DW_MACRO_undef
24471 || macinfo_type == DW_MACRO_undef_strp
24472 || macinfo_type == DW_MACRO_undef_sup);
24473 macro_undef (current_file, line, body);
24474 }
24475 }
24476 break;
24477
24478 case DW_MACRO_start_file:
24479 {
24480 unsigned int bytes_read;
24481 int line, file;
24482
24483 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24484 mac_ptr += bytes_read;
24485 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24486 mac_ptr += bytes_read;
24487
24488 if ((line == 0 && !at_commandline)
24489 || (line != 0 && at_commandline))
24490 complaint (_("debug info gives source %d included "
24491 "from %s at %s line %d"),
24492 file, at_commandline ? _("command-line") : _("file"),
24493 line == 0 ? _("zero") : _("non-zero"), line);
24494
24495 if (at_commandline)
24496 {
24497 /* This DW_MACRO_start_file was executed in the
24498 pass one. */
24499 at_commandline = 0;
24500 }
24501 else
24502 current_file = macro_start_file (cu, file, line, current_file,
24503 lh);
24504 }
24505 break;
24506
24507 case DW_MACRO_end_file:
24508 if (! current_file)
24509 complaint (_("macro debug info has an unmatched "
24510 "`close_file' directive"));
24511 else
24512 {
24513 current_file = current_file->included_by;
24514 if (! current_file)
24515 {
24516 enum dwarf_macro_record_type next_type;
24517
24518 /* GCC circa March 2002 doesn't produce the zero
24519 type byte marking the end of the compilation
24520 unit. Complain if it's not there, but exit no
24521 matter what. */
24522
24523 /* Do we at least have room for a macinfo type byte? */
24524 if (mac_ptr >= mac_end)
24525 {
24526 dwarf2_section_buffer_overflow_complaint (section);
24527 return;
24528 }
24529
24530 /* We don't increment mac_ptr here, so this is just
24531 a look-ahead. */
24532 next_type
24533 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24534 mac_ptr);
24535 if (next_type != 0)
24536 complaint (_("no terminating 0-type entry for "
24537 "macros in `.debug_macinfo' section"));
24538
24539 return;
24540 }
24541 }
24542 break;
24543
24544 case DW_MACRO_import:
24545 case DW_MACRO_import_sup:
24546 {
24547 LONGEST offset;
24548 void **slot;
24549 bfd *include_bfd = abfd;
24550 struct dwarf2_section_info *include_section = section;
24551 const gdb_byte *include_mac_end = mac_end;
24552 int is_dwz = section_is_dwz;
24553 const gdb_byte *new_mac_ptr;
24554
24555 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24556 mac_ptr += offset_size;
24557
24558 if (macinfo_type == DW_MACRO_import_sup)
24559 {
24560 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24561
24562 dwz->macro.read (objfile);
24563
24564 include_section = &dwz->macro;
24565 include_bfd = include_section->get_bfd_owner ();
24566 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24567 is_dwz = 1;
24568 }
24569
24570 new_mac_ptr = include_section->buffer + offset;
24571 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24572
24573 if (*slot != NULL)
24574 {
24575 /* This has actually happened; see
24576 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24577 complaint (_("recursive DW_MACRO_import in "
24578 ".debug_macro section"));
24579 }
24580 else
24581 {
24582 *slot = (void *) new_mac_ptr;
24583
24584 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24585 include_mac_end, current_file, lh,
24586 section, section_is_gnu, is_dwz,
24587 offset_size, include_hash);
24588
24589 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24590 }
24591 }
24592 break;
24593
24594 case DW_MACINFO_vendor_ext:
24595 if (!section_is_gnu)
24596 {
24597 unsigned int bytes_read;
24598
24599 /* This reads the constant, but since we don't recognize
24600 any vendor extensions, we ignore it. */
24601 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24602 mac_ptr += bytes_read;
24603 read_direct_string (abfd, mac_ptr, &bytes_read);
24604 mac_ptr += bytes_read;
24605
24606 /* We don't recognize any vendor extensions. */
24607 break;
24608 }
24609 /* FALLTHROUGH */
24610
24611 default:
24612 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24613 mac_ptr, mac_end, abfd, offset_size,
24614 section);
24615 if (mac_ptr == NULL)
24616 return;
24617 break;
24618 }
24619 DIAGNOSTIC_POP
24620 } while (macinfo_type != 0);
24621 }
24622
24623 static void
24624 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24625 int section_is_gnu)
24626 {
24627 struct dwarf2_per_objfile *dwarf2_per_objfile
24628 = cu->per_cu->dwarf2_per_objfile;
24629 struct objfile *objfile = dwarf2_per_objfile->objfile;
24630 struct line_header *lh = cu->line_header;
24631 bfd *abfd;
24632 const gdb_byte *mac_ptr, *mac_end;
24633 struct macro_source_file *current_file = 0;
24634 enum dwarf_macro_record_type macinfo_type;
24635 unsigned int offset_size = cu->header.offset_size;
24636 const gdb_byte *opcode_definitions[256];
24637 void **slot;
24638 struct dwarf2_section_info *section;
24639 const char *section_name;
24640
24641 if (cu->dwo_unit != NULL)
24642 {
24643 if (section_is_gnu)
24644 {
24645 section = &cu->dwo_unit->dwo_file->sections.macro;
24646 section_name = ".debug_macro.dwo";
24647 }
24648 else
24649 {
24650 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24651 section_name = ".debug_macinfo.dwo";
24652 }
24653 }
24654 else
24655 {
24656 if (section_is_gnu)
24657 {
24658 section = &dwarf2_per_objfile->macro;
24659 section_name = ".debug_macro";
24660 }
24661 else
24662 {
24663 section = &dwarf2_per_objfile->macinfo;
24664 section_name = ".debug_macinfo";
24665 }
24666 }
24667
24668 section->read (objfile);
24669 if (section->buffer == NULL)
24670 {
24671 complaint (_("missing %s section"), section_name);
24672 return;
24673 }
24674 abfd = section->get_bfd_owner ();
24675
24676 /* First pass: Find the name of the base filename.
24677 This filename is needed in order to process all macros whose definition
24678 (or undefinition) comes from the command line. These macros are defined
24679 before the first DW_MACINFO_start_file entry, and yet still need to be
24680 associated to the base file.
24681
24682 To determine the base file name, we scan the macro definitions until we
24683 reach the first DW_MACINFO_start_file entry. We then initialize
24684 CURRENT_FILE accordingly so that any macro definition found before the
24685 first DW_MACINFO_start_file can still be associated to the base file. */
24686
24687 mac_ptr = section->buffer + offset;
24688 mac_end = section->buffer + section->size;
24689
24690 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24691 &offset_size, section_is_gnu);
24692 if (mac_ptr == NULL)
24693 {
24694 /* We already issued a complaint. */
24695 return;
24696 }
24697
24698 do
24699 {
24700 /* Do we at least have room for a macinfo type byte? */
24701 if (mac_ptr >= mac_end)
24702 {
24703 /* Complaint is printed during the second pass as GDB will probably
24704 stop the first pass earlier upon finding
24705 DW_MACINFO_start_file. */
24706 break;
24707 }
24708
24709 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24710 mac_ptr++;
24711
24712 /* Note that we rely on the fact that the corresponding GNU and
24713 DWARF constants are the same. */
24714 DIAGNOSTIC_PUSH
24715 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24716 switch (macinfo_type)
24717 {
24718 /* A zero macinfo type indicates the end of the macro
24719 information. */
24720 case 0:
24721 break;
24722
24723 case DW_MACRO_define:
24724 case DW_MACRO_undef:
24725 /* Only skip the data by MAC_PTR. */
24726 {
24727 unsigned int bytes_read;
24728
24729 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24730 mac_ptr += bytes_read;
24731 read_direct_string (abfd, mac_ptr, &bytes_read);
24732 mac_ptr += bytes_read;
24733 }
24734 break;
24735
24736 case DW_MACRO_start_file:
24737 {
24738 unsigned int bytes_read;
24739 int line, file;
24740
24741 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24742 mac_ptr += bytes_read;
24743 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24744 mac_ptr += bytes_read;
24745
24746 current_file = macro_start_file (cu, file, line, current_file, lh);
24747 }
24748 break;
24749
24750 case DW_MACRO_end_file:
24751 /* No data to skip by MAC_PTR. */
24752 break;
24753
24754 case DW_MACRO_define_strp:
24755 case DW_MACRO_undef_strp:
24756 case DW_MACRO_define_sup:
24757 case DW_MACRO_undef_sup:
24758 {
24759 unsigned int bytes_read;
24760
24761 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24762 mac_ptr += bytes_read;
24763 mac_ptr += offset_size;
24764 }
24765 break;
24766
24767 case DW_MACRO_import:
24768 case DW_MACRO_import_sup:
24769 /* Note that, according to the spec, a transparent include
24770 chain cannot call DW_MACRO_start_file. So, we can just
24771 skip this opcode. */
24772 mac_ptr += offset_size;
24773 break;
24774
24775 case DW_MACINFO_vendor_ext:
24776 /* Only skip the data by MAC_PTR. */
24777 if (!section_is_gnu)
24778 {
24779 unsigned int bytes_read;
24780
24781 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24782 mac_ptr += bytes_read;
24783 read_direct_string (abfd, mac_ptr, &bytes_read);
24784 mac_ptr += bytes_read;
24785 }
24786 /* FALLTHROUGH */
24787
24788 default:
24789 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24790 mac_ptr, mac_end, abfd, offset_size,
24791 section);
24792 if (mac_ptr == NULL)
24793 return;
24794 break;
24795 }
24796 DIAGNOSTIC_POP
24797 } while (macinfo_type != 0 && current_file == NULL);
24798
24799 /* Second pass: Process all entries.
24800
24801 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24802 command-line macro definitions/undefinitions. This flag is unset when we
24803 reach the first DW_MACINFO_start_file entry. */
24804
24805 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24806 htab_eq_pointer,
24807 NULL, xcalloc, xfree));
24808 mac_ptr = section->buffer + offset;
24809 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24810 *slot = (void *) mac_ptr;
24811 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24812 current_file, lh, section,
24813 section_is_gnu, 0, offset_size,
24814 include_hash.get ());
24815 }
24816
24817 /* Check if the attribute's form is a DW_FORM_block*
24818 if so return true else false. */
24819
24820 static int
24821 attr_form_is_block (const struct attribute *attr)
24822 {
24823 return (attr == NULL ? 0 :
24824 attr->form == DW_FORM_block1
24825 || attr->form == DW_FORM_block2
24826 || attr->form == DW_FORM_block4
24827 || attr->form == DW_FORM_block
24828 || attr->form == DW_FORM_exprloc);
24829 }
24830
24831 /* Return non-zero if ATTR's value is a section offset --- classes
24832 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24833 You may use DW_UNSND (attr) to retrieve such offsets.
24834
24835 Section 7.5.4, "Attribute Encodings", explains that no attribute
24836 may have a value that belongs to more than one of these classes; it
24837 would be ambiguous if we did, because we use the same forms for all
24838 of them. */
24839
24840 static int
24841 attr_form_is_section_offset (const struct attribute *attr)
24842 {
24843 return (attr->form == DW_FORM_data4
24844 || attr->form == DW_FORM_data8
24845 || attr->form == DW_FORM_sec_offset);
24846 }
24847
24848 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24849 zero otherwise. When this function returns true, you can apply
24850 dwarf2_get_attr_constant_value to it.
24851
24852 However, note that for some attributes you must check
24853 attr_form_is_section_offset before using this test. DW_FORM_data4
24854 and DW_FORM_data8 are members of both the constant class, and of
24855 the classes that contain offsets into other debug sections
24856 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24857 that, if an attribute's can be either a constant or one of the
24858 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24859 taken as section offsets, not constants.
24860
24861 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24862 cannot handle that. */
24863
24864 static int
24865 attr_form_is_constant (const struct attribute *attr)
24866 {
24867 switch (attr->form)
24868 {
24869 case DW_FORM_sdata:
24870 case DW_FORM_udata:
24871 case DW_FORM_data1:
24872 case DW_FORM_data2:
24873 case DW_FORM_data4:
24874 case DW_FORM_data8:
24875 case DW_FORM_implicit_const:
24876 return 1;
24877 default:
24878 return 0;
24879 }
24880 }
24881
24882
24883 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24884 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24885
24886 static int
24887 attr_form_is_ref (const struct attribute *attr)
24888 {
24889 switch (attr->form)
24890 {
24891 case DW_FORM_ref_addr:
24892 case DW_FORM_ref1:
24893 case DW_FORM_ref2:
24894 case DW_FORM_ref4:
24895 case DW_FORM_ref8:
24896 case DW_FORM_ref_udata:
24897 case DW_FORM_GNU_ref_alt:
24898 return 1;
24899 default:
24900 return 0;
24901 }
24902 }
24903
24904 /* Return the .debug_loc section to use for CU.
24905 For DWO files use .debug_loc.dwo. */
24906
24907 static struct dwarf2_section_info *
24908 cu_debug_loc_section (struct dwarf2_cu *cu)
24909 {
24910 struct dwarf2_per_objfile *dwarf2_per_objfile
24911 = cu->per_cu->dwarf2_per_objfile;
24912
24913 if (cu->dwo_unit)
24914 {
24915 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24916
24917 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24918 }
24919 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24920 : &dwarf2_per_objfile->loc);
24921 }
24922
24923 /* A helper function that fills in a dwarf2_loclist_baton. */
24924
24925 static void
24926 fill_in_loclist_baton (struct dwarf2_cu *cu,
24927 struct dwarf2_loclist_baton *baton,
24928 const struct attribute *attr)
24929 {
24930 struct dwarf2_per_objfile *dwarf2_per_objfile
24931 = cu->per_cu->dwarf2_per_objfile;
24932 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24933
24934 section->read (dwarf2_per_objfile->objfile);
24935
24936 baton->per_cu = cu->per_cu;
24937 gdb_assert (baton->per_cu);
24938 /* We don't know how long the location list is, but make sure we
24939 don't run off the edge of the section. */
24940 baton->size = section->size - DW_UNSND (attr);
24941 baton->data = section->buffer + DW_UNSND (attr);
24942 baton->base_address = cu->base_address;
24943 baton->from_dwo = cu->dwo_unit != NULL;
24944 }
24945
24946 static void
24947 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24948 struct dwarf2_cu *cu, int is_block)
24949 {
24950 struct dwarf2_per_objfile *dwarf2_per_objfile
24951 = cu->per_cu->dwarf2_per_objfile;
24952 struct objfile *objfile = dwarf2_per_objfile->objfile;
24953 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24954
24955 if (attr_form_is_section_offset (attr)
24956 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24957 the section. If so, fall through to the complaint in the
24958 other branch. */
24959 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24960 {
24961 struct dwarf2_loclist_baton *baton;
24962
24963 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24964
24965 fill_in_loclist_baton (cu, baton, attr);
24966
24967 if (cu->base_known == 0)
24968 complaint (_("Location list used without "
24969 "specifying the CU base address."));
24970
24971 SYMBOL_ACLASS_INDEX (sym) = (is_block
24972 ? dwarf2_loclist_block_index
24973 : dwarf2_loclist_index);
24974 SYMBOL_LOCATION_BATON (sym) = baton;
24975 }
24976 else
24977 {
24978 struct dwarf2_locexpr_baton *baton;
24979
24980 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24981 baton->per_cu = cu->per_cu;
24982 gdb_assert (baton->per_cu);
24983
24984 if (attr_form_is_block (attr))
24985 {
24986 /* Note that we're just copying the block's data pointer
24987 here, not the actual data. We're still pointing into the
24988 info_buffer for SYM's objfile; right now we never release
24989 that buffer, but when we do clean up properly this may
24990 need to change. */
24991 baton->size = DW_BLOCK (attr)->size;
24992 baton->data = DW_BLOCK (attr)->data;
24993 }
24994 else
24995 {
24996 dwarf2_invalid_attrib_class_complaint ("location description",
24997 sym->natural_name ());
24998 baton->size = 0;
24999 }
25000
25001 SYMBOL_ACLASS_INDEX (sym) = (is_block
25002 ? dwarf2_locexpr_block_index
25003 : dwarf2_locexpr_index);
25004 SYMBOL_LOCATION_BATON (sym) = baton;
25005 }
25006 }
25007
25008 /* Return the OBJFILE associated with the compilation unit CU. If CU
25009 came from a separate debuginfo file, then the master objfile is
25010 returned. */
25011
25012 struct objfile *
25013 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25014 {
25015 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25016
25017 /* Return the master objfile, so that we can report and look up the
25018 correct file containing this variable. */
25019 if (objfile->separate_debug_objfile_backlink)
25020 objfile = objfile->separate_debug_objfile_backlink;
25021
25022 return objfile;
25023 }
25024
25025 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25026 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25027 CU_HEADERP first. */
25028
25029 static const struct comp_unit_head *
25030 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25031 struct dwarf2_per_cu_data *per_cu)
25032 {
25033 const gdb_byte *info_ptr;
25034
25035 if (per_cu->cu)
25036 return &per_cu->cu->header;
25037
25038 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25039
25040 memset (cu_headerp, 0, sizeof (*cu_headerp));
25041 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25042 rcuh_kind::COMPILE);
25043
25044 return cu_headerp;
25045 }
25046
25047 /* Return the address size given in the compilation unit header for CU. */
25048
25049 int
25050 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25051 {
25052 struct comp_unit_head cu_header_local;
25053 const struct comp_unit_head *cu_headerp;
25054
25055 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25056
25057 return cu_headerp->addr_size;
25058 }
25059
25060 /* Return the offset size given in the compilation unit header for CU. */
25061
25062 int
25063 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25064 {
25065 struct comp_unit_head cu_header_local;
25066 const struct comp_unit_head *cu_headerp;
25067
25068 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25069
25070 return cu_headerp->offset_size;
25071 }
25072
25073 /* See its dwarf2loc.h declaration. */
25074
25075 int
25076 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25077 {
25078 struct comp_unit_head cu_header_local;
25079 const struct comp_unit_head *cu_headerp;
25080
25081 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25082
25083 if (cu_headerp->version == 2)
25084 return cu_headerp->addr_size;
25085 else
25086 return cu_headerp->offset_size;
25087 }
25088
25089 /* Return the text offset of the CU. The returned offset comes from
25090 this CU's objfile. If this objfile came from a separate debuginfo
25091 file, then the offset may be different from the corresponding
25092 offset in the parent objfile. */
25093
25094 CORE_ADDR
25095 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25096 {
25097 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
25098 }
25099
25100 /* Return a type that is a generic pointer type, the size of which matches
25101 the address size given in the compilation unit header for PER_CU. */
25102 static struct type *
25103 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25104 {
25105 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25106 struct type *void_type = objfile_type (objfile)->builtin_void;
25107 struct type *addr_type = lookup_pointer_type (void_type);
25108 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25109
25110 if (TYPE_LENGTH (addr_type) == addr_size)
25111 return addr_type;
25112
25113 addr_type
25114 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25115 return addr_type;
25116 }
25117
25118 /* Return DWARF version number of PER_CU. */
25119
25120 short
25121 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25122 {
25123 return per_cu->dwarf_version;
25124 }
25125
25126 /* Locate the .debug_info compilation unit from CU's objfile which contains
25127 the DIE at OFFSET. Raises an error on failure. */
25128
25129 static struct dwarf2_per_cu_data *
25130 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25131 unsigned int offset_in_dwz,
25132 struct dwarf2_per_objfile *dwarf2_per_objfile)
25133 {
25134 struct dwarf2_per_cu_data *this_cu;
25135 int low, high;
25136
25137 low = 0;
25138 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25139 while (high > low)
25140 {
25141 struct dwarf2_per_cu_data *mid_cu;
25142 int mid = low + (high - low) / 2;
25143
25144 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25145 if (mid_cu->is_dwz > offset_in_dwz
25146 || (mid_cu->is_dwz == offset_in_dwz
25147 && mid_cu->sect_off + mid_cu->length >= sect_off))
25148 high = mid;
25149 else
25150 low = mid + 1;
25151 }
25152 gdb_assert (low == high);
25153 this_cu = dwarf2_per_objfile->all_comp_units[low];
25154 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25155 {
25156 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25157 error (_("Dwarf Error: could not find partial DIE containing "
25158 "offset %s [in module %s]"),
25159 sect_offset_str (sect_off),
25160 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25161
25162 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25163 <= sect_off);
25164 return dwarf2_per_objfile->all_comp_units[low-1];
25165 }
25166 else
25167 {
25168 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25169 && sect_off >= this_cu->sect_off + this_cu->length)
25170 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25171 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25172 return this_cu;
25173 }
25174 }
25175
25176 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25177
25178 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25179 : per_cu (per_cu_),
25180 mark (false),
25181 has_loclist (false),
25182 checked_producer (false),
25183 producer_is_gxx_lt_4_6 (false),
25184 producer_is_gcc_lt_4_3 (false),
25185 producer_is_icc (false),
25186 producer_is_icc_lt_14 (false),
25187 producer_is_codewarrior (false),
25188 processing_has_namespace_info (false)
25189 {
25190 per_cu->cu = this;
25191 }
25192
25193 /* Destroy a dwarf2_cu. */
25194
25195 dwarf2_cu::~dwarf2_cu ()
25196 {
25197 per_cu->cu = NULL;
25198 }
25199
25200 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25201
25202 static void
25203 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25204 enum language pretend_language)
25205 {
25206 struct attribute *attr;
25207
25208 /* Set the language we're debugging. */
25209 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25210 if (attr != nullptr)
25211 set_cu_language (DW_UNSND (attr), cu);
25212 else
25213 {
25214 cu->language = pretend_language;
25215 cu->language_defn = language_def (cu->language);
25216 }
25217
25218 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25219 }
25220
25221 /* Increase the age counter on each cached compilation unit, and free
25222 any that are too old. */
25223
25224 static void
25225 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25226 {
25227 struct dwarf2_per_cu_data *per_cu, **last_chain;
25228
25229 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25230 per_cu = dwarf2_per_objfile->read_in_chain;
25231 while (per_cu != NULL)
25232 {
25233 per_cu->cu->last_used ++;
25234 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25235 dwarf2_mark (per_cu->cu);
25236 per_cu = per_cu->cu->read_in_chain;
25237 }
25238
25239 per_cu = dwarf2_per_objfile->read_in_chain;
25240 last_chain = &dwarf2_per_objfile->read_in_chain;
25241 while (per_cu != NULL)
25242 {
25243 struct dwarf2_per_cu_data *next_cu;
25244
25245 next_cu = per_cu->cu->read_in_chain;
25246
25247 if (!per_cu->cu->mark)
25248 {
25249 delete per_cu->cu;
25250 *last_chain = next_cu;
25251 }
25252 else
25253 last_chain = &per_cu->cu->read_in_chain;
25254
25255 per_cu = next_cu;
25256 }
25257 }
25258
25259 /* Remove a single compilation unit from the cache. */
25260
25261 static void
25262 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25263 {
25264 struct dwarf2_per_cu_data *per_cu, **last_chain;
25265 struct dwarf2_per_objfile *dwarf2_per_objfile
25266 = target_per_cu->dwarf2_per_objfile;
25267
25268 per_cu = dwarf2_per_objfile->read_in_chain;
25269 last_chain = &dwarf2_per_objfile->read_in_chain;
25270 while (per_cu != NULL)
25271 {
25272 struct dwarf2_per_cu_data *next_cu;
25273
25274 next_cu = per_cu->cu->read_in_chain;
25275
25276 if (per_cu == target_per_cu)
25277 {
25278 delete per_cu->cu;
25279 per_cu->cu = NULL;
25280 *last_chain = next_cu;
25281 break;
25282 }
25283 else
25284 last_chain = &per_cu->cu->read_in_chain;
25285
25286 per_cu = next_cu;
25287 }
25288 }
25289
25290 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25291 We store these in a hash table separate from the DIEs, and preserve them
25292 when the DIEs are flushed out of cache.
25293
25294 The CU "per_cu" pointer is needed because offset alone is not enough to
25295 uniquely identify the type. A file may have multiple .debug_types sections,
25296 or the type may come from a DWO file. Furthermore, while it's more logical
25297 to use per_cu->section+offset, with Fission the section with the data is in
25298 the DWO file but we don't know that section at the point we need it.
25299 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25300 because we can enter the lookup routine, get_die_type_at_offset, from
25301 outside this file, and thus won't necessarily have PER_CU->cu.
25302 Fortunately, PER_CU is stable for the life of the objfile. */
25303
25304 struct dwarf2_per_cu_offset_and_type
25305 {
25306 const struct dwarf2_per_cu_data *per_cu;
25307 sect_offset sect_off;
25308 struct type *type;
25309 };
25310
25311 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25312
25313 static hashval_t
25314 per_cu_offset_and_type_hash (const void *item)
25315 {
25316 const struct dwarf2_per_cu_offset_and_type *ofs
25317 = (const struct dwarf2_per_cu_offset_and_type *) item;
25318
25319 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25320 }
25321
25322 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25323
25324 static int
25325 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25326 {
25327 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25328 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25329 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25330 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25331
25332 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25333 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25334 }
25335
25336 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25337 table if necessary. For convenience, return TYPE.
25338
25339 The DIEs reading must have careful ordering to:
25340 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25341 reading current DIE.
25342 * Not trying to dereference contents of still incompletely read in types
25343 while reading in other DIEs.
25344 * Enable referencing still incompletely read in types just by a pointer to
25345 the type without accessing its fields.
25346
25347 Therefore caller should follow these rules:
25348 * Try to fetch any prerequisite types we may need to build this DIE type
25349 before building the type and calling set_die_type.
25350 * After building type call set_die_type for current DIE as soon as
25351 possible before fetching more types to complete the current type.
25352 * Make the type as complete as possible before fetching more types. */
25353
25354 static struct type *
25355 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25356 {
25357 struct dwarf2_per_objfile *dwarf2_per_objfile
25358 = cu->per_cu->dwarf2_per_objfile;
25359 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25360 struct objfile *objfile = dwarf2_per_objfile->objfile;
25361 struct attribute *attr;
25362 struct dynamic_prop prop;
25363
25364 /* For Ada types, make sure that the gnat-specific data is always
25365 initialized (if not already set). There are a few types where
25366 we should not be doing so, because the type-specific area is
25367 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25368 where the type-specific area is used to store the floatformat).
25369 But this is not a problem, because the gnat-specific information
25370 is actually not needed for these types. */
25371 if (need_gnat_info (cu)
25372 && TYPE_CODE (type) != TYPE_CODE_FUNC
25373 && TYPE_CODE (type) != TYPE_CODE_FLT
25374 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25375 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25376 && TYPE_CODE (type) != TYPE_CODE_METHOD
25377 && !HAVE_GNAT_AUX_INFO (type))
25378 INIT_GNAT_SPECIFIC (type);
25379
25380 /* Read DW_AT_allocated and set in type. */
25381 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25382 if (attr_form_is_block (attr))
25383 {
25384 struct type *prop_type
25385 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25386 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25387 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25388 }
25389 else if (attr != NULL)
25390 {
25391 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25392 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25393 sect_offset_str (die->sect_off));
25394 }
25395
25396 /* Read DW_AT_associated and set in type. */
25397 attr = dwarf2_attr (die, DW_AT_associated, cu);
25398 if (attr_form_is_block (attr))
25399 {
25400 struct type *prop_type
25401 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25402 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25403 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25404 }
25405 else if (attr != NULL)
25406 {
25407 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25408 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25409 sect_offset_str (die->sect_off));
25410 }
25411
25412 /* Read DW_AT_data_location and set in type. */
25413 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25414 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25415 dwarf2_per_cu_addr_type (cu->per_cu)))
25416 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25417
25418 if (dwarf2_per_objfile->die_type_hash == NULL)
25419 {
25420 dwarf2_per_objfile->die_type_hash =
25421 htab_create_alloc_ex (127,
25422 per_cu_offset_and_type_hash,
25423 per_cu_offset_and_type_eq,
25424 NULL,
25425 &objfile->objfile_obstack,
25426 hashtab_obstack_allocate,
25427 dummy_obstack_deallocate);
25428 }
25429
25430 ofs.per_cu = cu->per_cu;
25431 ofs.sect_off = die->sect_off;
25432 ofs.type = type;
25433 slot = (struct dwarf2_per_cu_offset_and_type **)
25434 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25435 if (*slot)
25436 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25437 sect_offset_str (die->sect_off));
25438 *slot = XOBNEW (&objfile->objfile_obstack,
25439 struct dwarf2_per_cu_offset_and_type);
25440 **slot = ofs;
25441 return type;
25442 }
25443
25444 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25445 or return NULL if the die does not have a saved type. */
25446
25447 static struct type *
25448 get_die_type_at_offset (sect_offset sect_off,
25449 struct dwarf2_per_cu_data *per_cu)
25450 {
25451 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25452 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25453
25454 if (dwarf2_per_objfile->die_type_hash == NULL)
25455 return NULL;
25456
25457 ofs.per_cu = per_cu;
25458 ofs.sect_off = sect_off;
25459 slot = ((struct dwarf2_per_cu_offset_and_type *)
25460 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25461 if (slot)
25462 return slot->type;
25463 else
25464 return NULL;
25465 }
25466
25467 /* Look up the type for DIE in CU in die_type_hash,
25468 or return NULL if DIE does not have a saved type. */
25469
25470 static struct type *
25471 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25472 {
25473 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25474 }
25475
25476 /* Add a dependence relationship from CU to REF_PER_CU. */
25477
25478 static void
25479 dwarf2_add_dependence (struct dwarf2_cu *cu,
25480 struct dwarf2_per_cu_data *ref_per_cu)
25481 {
25482 void **slot;
25483
25484 if (cu->dependencies == NULL)
25485 cu->dependencies
25486 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25487 NULL, &cu->comp_unit_obstack,
25488 hashtab_obstack_allocate,
25489 dummy_obstack_deallocate);
25490
25491 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25492 if (*slot == NULL)
25493 *slot = ref_per_cu;
25494 }
25495
25496 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25497 Set the mark field in every compilation unit in the
25498 cache that we must keep because we are keeping CU. */
25499
25500 static int
25501 dwarf2_mark_helper (void **slot, void *data)
25502 {
25503 struct dwarf2_per_cu_data *per_cu;
25504
25505 per_cu = (struct dwarf2_per_cu_data *) *slot;
25506
25507 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25508 reading of the chain. As such dependencies remain valid it is not much
25509 useful to track and undo them during QUIT cleanups. */
25510 if (per_cu->cu == NULL)
25511 return 1;
25512
25513 if (per_cu->cu->mark)
25514 return 1;
25515 per_cu->cu->mark = true;
25516
25517 if (per_cu->cu->dependencies != NULL)
25518 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25519
25520 return 1;
25521 }
25522
25523 /* Set the mark field in CU and in every other compilation unit in the
25524 cache that we must keep because we are keeping CU. */
25525
25526 static void
25527 dwarf2_mark (struct dwarf2_cu *cu)
25528 {
25529 if (cu->mark)
25530 return;
25531 cu->mark = true;
25532 if (cu->dependencies != NULL)
25533 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25534 }
25535
25536 static void
25537 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25538 {
25539 while (per_cu)
25540 {
25541 per_cu->cu->mark = false;
25542 per_cu = per_cu->cu->read_in_chain;
25543 }
25544 }
25545
25546 /* Trivial hash function for partial_die_info: the hash value of a DIE
25547 is its offset in .debug_info for this objfile. */
25548
25549 static hashval_t
25550 partial_die_hash (const void *item)
25551 {
25552 const struct partial_die_info *part_die
25553 = (const struct partial_die_info *) item;
25554
25555 return to_underlying (part_die->sect_off);
25556 }
25557
25558 /* Trivial comparison function for partial_die_info structures: two DIEs
25559 are equal if they have the same offset. */
25560
25561 static int
25562 partial_die_eq (const void *item_lhs, const void *item_rhs)
25563 {
25564 const struct partial_die_info *part_die_lhs
25565 = (const struct partial_die_info *) item_lhs;
25566 const struct partial_die_info *part_die_rhs
25567 = (const struct partial_die_info *) item_rhs;
25568
25569 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25570 }
25571
25572 struct cmd_list_element *set_dwarf_cmdlist;
25573 struct cmd_list_element *show_dwarf_cmdlist;
25574
25575 static void
25576 set_dwarf_cmd (const char *args, int from_tty)
25577 {
25578 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25579 gdb_stdout);
25580 }
25581
25582 static void
25583 show_dwarf_cmd (const char *args, int from_tty)
25584 {
25585 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25586 }
25587
25588 bool dwarf_always_disassemble;
25589
25590 static void
25591 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25592 struct cmd_list_element *c, const char *value)
25593 {
25594 fprintf_filtered (file,
25595 _("Whether to always disassemble "
25596 "DWARF expressions is %s.\n"),
25597 value);
25598 }
25599
25600 static void
25601 show_check_physname (struct ui_file *file, int from_tty,
25602 struct cmd_list_element *c, const char *value)
25603 {
25604 fprintf_filtered (file,
25605 _("Whether to check \"physname\" is %s.\n"),
25606 value);
25607 }
25608
25609 void _initialize_dwarf2_read ();
25610 void
25611 _initialize_dwarf2_read ()
25612 {
25613 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25614 Set DWARF specific variables.\n\
25615 Configure DWARF variables such as the cache size."),
25616 &set_dwarf_cmdlist, "maintenance set dwarf ",
25617 0/*allow-unknown*/, &maintenance_set_cmdlist);
25618
25619 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25620 Show DWARF specific variables.\n\
25621 Show DWARF variables such as the cache size."),
25622 &show_dwarf_cmdlist, "maintenance show dwarf ",
25623 0/*allow-unknown*/, &maintenance_show_cmdlist);
25624
25625 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25626 &dwarf_max_cache_age, _("\
25627 Set the upper bound on the age of cached DWARF compilation units."), _("\
25628 Show the upper bound on the age of cached DWARF compilation units."), _("\
25629 A higher limit means that cached compilation units will be stored\n\
25630 in memory longer, and more total memory will be used. Zero disables\n\
25631 caching, which can slow down startup."),
25632 NULL,
25633 show_dwarf_max_cache_age,
25634 &set_dwarf_cmdlist,
25635 &show_dwarf_cmdlist);
25636
25637 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25638 &dwarf_always_disassemble, _("\
25639 Set whether `info address' always disassembles DWARF expressions."), _("\
25640 Show whether `info address' always disassembles DWARF expressions."), _("\
25641 When enabled, DWARF expressions are always printed in an assembly-like\n\
25642 syntax. When disabled, expressions will be printed in a more\n\
25643 conversational style, when possible."),
25644 NULL,
25645 show_dwarf_always_disassemble,
25646 &set_dwarf_cmdlist,
25647 &show_dwarf_cmdlist);
25648
25649 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25650 Set debugging of the DWARF reader."), _("\
25651 Show debugging of the DWARF reader."), _("\
25652 When enabled (non-zero), debugging messages are printed during DWARF\n\
25653 reading and symtab expansion. A value of 1 (one) provides basic\n\
25654 information. A value greater than 1 provides more verbose information."),
25655 NULL,
25656 NULL,
25657 &setdebuglist, &showdebuglist);
25658
25659 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25660 Set debugging of the DWARF DIE reader."), _("\
25661 Show debugging of the DWARF DIE reader."), _("\
25662 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25663 The value is the maximum depth to print."),
25664 NULL,
25665 NULL,
25666 &setdebuglist, &showdebuglist);
25667
25668 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25669 Set debugging of the dwarf line reader."), _("\
25670 Show debugging of the dwarf line reader."), _("\
25671 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25672 A value of 1 (one) provides basic information.\n\
25673 A value greater than 1 provides more verbose information."),
25674 NULL,
25675 NULL,
25676 &setdebuglist, &showdebuglist);
25677
25678 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25679 Set cross-checking of \"physname\" code against demangler."), _("\
25680 Show cross-checking of \"physname\" code against demangler."), _("\
25681 When enabled, GDB's internal \"physname\" code is checked against\n\
25682 the demangler."),
25683 NULL, show_check_physname,
25684 &setdebuglist, &showdebuglist);
25685
25686 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25687 no_class, &use_deprecated_index_sections, _("\
25688 Set whether to use deprecated gdb_index sections."), _("\
25689 Show whether to use deprecated gdb_index sections."), _("\
25690 When enabled, deprecated .gdb_index sections are used anyway.\n\
25691 Normally they are ignored either because of a missing feature or\n\
25692 performance issue.\n\
25693 Warning: This option must be enabled before gdb reads the file."),
25694 NULL,
25695 NULL,
25696 &setlist, &showlist);
25697
25698 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25699 &dwarf2_locexpr_funcs);
25700 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25701 &dwarf2_loclist_funcs);
25702
25703 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25704 &dwarf2_block_frame_base_locexpr_funcs);
25705 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25706 &dwarf2_block_frame_base_loclist_funcs);
25707
25708 #if GDB_SELF_TEST
25709 selftests::register_test ("dw2_expand_symtabs_matching",
25710 selftests::dw2_expand_symtabs_matching::run_test);
25711 #endif
25712 }
This page took 0.844214 seconds and 4 git commands to generate.