gdb: fix printing of flag enums with multi-bit enumerators
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "bfd.h"
41 #include "elf-bfd.h"
42 #include "symtab.h"
43 #include "gdbtypes.h"
44 #include "objfiles.h"
45 #include "dwarf2.h"
46 #include "buildsym.h"
47 #include "demangle.h"
48 #include "gdb-demangle.h"
49 #include "filenames.h" /* for DOSish file names */
50 #include "macrotab.h"
51 #include "language.h"
52 #include "complaints.h"
53 #include "dwarf2/expr.h"
54 #include "dwarf2/loc.h"
55 #include "cp-support.h"
56 #include "hashtab.h"
57 #include "command.h"
58 #include "gdbcmd.h"
59 #include "block.h"
60 #include "addrmap.h"
61 #include "typeprint.h"
62 #include "psympriv.h"
63 #include "c-lang.h"
64 #include "go-lang.h"
65 #include "valprint.h"
66 #include "gdbcore.h" /* for gnutarget */
67 #include "gdb/gdb-index.h"
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "build-id.h"
72 #include "namespace.h"
73 #include "gdbsupport/function-view.h"
74 #include "gdbsupport/gdb_optional.h"
75 #include "gdbsupport/underlying.h"
76 #include "gdbsupport/hash_enum.h"
77 #include "filename-seen-cache.h"
78 #include "producer.h"
79 #include <fcntl.h>
80 #include <algorithm>
81 #include <unordered_map>
82 #include "gdbsupport/selftest.h"
83 #include "rust-lang.h"
84 #include "gdbsupport/pathstuff.h"
85 #include "count-one-bits.h"
86
87 /* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90 static unsigned int dwarf_read_debug = 0;
91
92 /* When non-zero, dump DIEs after they are read in. */
93 static unsigned int dwarf_die_debug = 0;
94
95 /* When non-zero, dump line number entries as they are read in. */
96 unsigned int dwarf_line_debug = 0;
97
98 /* When true, cross-check physname against demangler. */
99 static bool check_physname = false;
100
101 /* When true, do not reject deprecated .gdb_index sections. */
102 static bool use_deprecated_index_sections = false;
103
104 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
105
106 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108 static int dwarf2_locexpr_index;
109 static int dwarf2_loclist_index;
110 static int dwarf2_locexpr_block_index;
111 static int dwarf2_loclist_block_index;
112
113 /* An index into a (C++) symbol name component in a symbol name as
114 recorded in the mapped_index's symbol table. For each C++ symbol
115 in the symbol table, we record one entry for the start of each
116 component in the symbol in a table of name components, and then
117 sort the table, in order to be able to binary search symbol names,
118 ignoring leading namespaces, both completion and regular look up.
119 For example, for symbol "A::B::C", we'll have an entry that points
120 to "A::B::C", another that points to "B::C", and another for "C".
121 Note that function symbols in GDB index have no parameter
122 information, just the function/method names. You can convert a
123 name_component to a "const char *" using the
124 'mapped_index::symbol_name_at(offset_type)' method. */
125
126 struct name_component
127 {
128 /* Offset in the symbol name where the component starts. Stored as
129 a (32-bit) offset instead of a pointer to save memory and improve
130 locality on 64-bit architectures. */
131 offset_type name_offset;
132
133 /* The symbol's index in the symbol and constant pool tables of a
134 mapped_index. */
135 offset_type idx;
136 };
137
138 /* Base class containing bits shared by both .gdb_index and
139 .debug_name indexes. */
140
141 struct mapped_index_base
142 {
143 mapped_index_base () = default;
144 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
145
146 /* The name_component table (a sorted vector). See name_component's
147 description above. */
148 std::vector<name_component> name_components;
149
150 /* How NAME_COMPONENTS is sorted. */
151 enum case_sensitivity name_components_casing;
152
153 /* Return the number of names in the symbol table. */
154 virtual size_t symbol_name_count () const = 0;
155
156 /* Get the name of the symbol at IDX in the symbol table. */
157 virtual const char *symbol_name_at (offset_type idx) const = 0;
158
159 /* Return whether the name at IDX in the symbol table should be
160 ignored. */
161 virtual bool symbol_name_slot_invalid (offset_type idx) const
162 {
163 return false;
164 }
165
166 /* Build the symbol name component sorted vector, if we haven't
167 yet. */
168 void build_name_components ();
169
170 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
171 possible matches for LN_NO_PARAMS in the name component
172 vector. */
173 std::pair<std::vector<name_component>::const_iterator,
174 std::vector<name_component>::const_iterator>
175 find_name_components_bounds (const lookup_name_info &ln_no_params,
176 enum language lang) const;
177
178 /* Prevent deleting/destroying via a base class pointer. */
179 protected:
180 ~mapped_index_base() = default;
181 };
182
183 /* A description of the mapped index. The file format is described in
184 a comment by the code that writes the index. */
185 struct mapped_index final : public mapped_index_base
186 {
187 /* A slot/bucket in the symbol table hash. */
188 struct symbol_table_slot
189 {
190 const offset_type name;
191 const offset_type vec;
192 };
193
194 /* Index data format version. */
195 int version = 0;
196
197 /* The address table data. */
198 gdb::array_view<const gdb_byte> address_table;
199
200 /* The symbol table, implemented as a hash table. */
201 gdb::array_view<symbol_table_slot> symbol_table;
202
203 /* A pointer to the constant pool. */
204 const char *constant_pool = nullptr;
205
206 bool symbol_name_slot_invalid (offset_type idx) const override
207 {
208 const auto &bucket = this->symbol_table[idx];
209 return bucket.name == 0 && bucket.vec == 0;
210 }
211
212 /* Convenience method to get at the name of the symbol at IDX in the
213 symbol table. */
214 const char *symbol_name_at (offset_type idx) const override
215 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
216
217 size_t symbol_name_count () const override
218 { return this->symbol_table.size (); }
219 };
220
221 /* A description of the mapped .debug_names.
222 Uninitialized map has CU_COUNT 0. */
223 struct mapped_debug_names final : public mapped_index_base
224 {
225 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
226 : dwarf2_per_objfile (dwarf2_per_objfile_)
227 {}
228
229 struct dwarf2_per_objfile *dwarf2_per_objfile;
230 bfd_endian dwarf5_byte_order;
231 bool dwarf5_is_dwarf64;
232 bool augmentation_is_gdb;
233 uint8_t offset_size;
234 uint32_t cu_count = 0;
235 uint32_t tu_count, bucket_count, name_count;
236 const gdb_byte *cu_table_reordered, *tu_table_reordered;
237 const uint32_t *bucket_table_reordered, *hash_table_reordered;
238 const gdb_byte *name_table_string_offs_reordered;
239 const gdb_byte *name_table_entry_offs_reordered;
240 const gdb_byte *entry_pool;
241
242 struct index_val
243 {
244 ULONGEST dwarf_tag;
245 struct attr
246 {
247 /* Attribute name DW_IDX_*. */
248 ULONGEST dw_idx;
249
250 /* Attribute form DW_FORM_*. */
251 ULONGEST form;
252
253 /* Value if FORM is DW_FORM_implicit_const. */
254 LONGEST implicit_const;
255 };
256 std::vector<attr> attr_vec;
257 };
258
259 std::unordered_map<ULONGEST, index_val> abbrev_map;
260
261 const char *namei_to_name (uint32_t namei) const;
262
263 /* Implementation of the mapped_index_base virtual interface, for
264 the name_components cache. */
265
266 const char *symbol_name_at (offset_type idx) const override
267 { return namei_to_name (idx); }
268
269 size_t symbol_name_count () const override
270 { return this->name_count; }
271 };
272
273 /* See dwarf2read.h. */
274
275 dwarf2_per_objfile *
276 get_dwarf2_per_objfile (struct objfile *objfile)
277 {
278 return dwarf2_objfile_data_key.get (objfile);
279 }
280
281 /* Default names of the debugging sections. */
282
283 /* Note that if the debugging section has been compressed, it might
284 have a name like .zdebug_info. */
285
286 static const struct dwarf2_debug_sections dwarf2_elf_names =
287 {
288 { ".debug_info", ".zdebug_info" },
289 { ".debug_abbrev", ".zdebug_abbrev" },
290 { ".debug_line", ".zdebug_line" },
291 { ".debug_loc", ".zdebug_loc" },
292 { ".debug_loclists", ".zdebug_loclists" },
293 { ".debug_macinfo", ".zdebug_macinfo" },
294 { ".debug_macro", ".zdebug_macro" },
295 { ".debug_str", ".zdebug_str" },
296 { ".debug_str_offsets", ".zdebug_str_offsets" },
297 { ".debug_line_str", ".zdebug_line_str" },
298 { ".debug_ranges", ".zdebug_ranges" },
299 { ".debug_rnglists", ".zdebug_rnglists" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
305 { ".debug_names", ".zdebug_names" },
306 { ".debug_aranges", ".zdebug_aranges" },
307 23
308 };
309
310 /* List of DWO/DWP sections. */
311
312 static const struct dwop_section_names
313 {
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
318 struct dwarf2_section_names loclists_dwo;
319 struct dwarf2_section_names macinfo_dwo;
320 struct dwarf2_section_names macro_dwo;
321 struct dwarf2_section_names str_dwo;
322 struct dwarf2_section_names str_offsets_dwo;
323 struct dwarf2_section_names types_dwo;
324 struct dwarf2_section_names cu_index;
325 struct dwarf2_section_names tu_index;
326 }
327 dwop_section_names =
328 {
329 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
330 { ".debug_info.dwo", ".zdebug_info.dwo" },
331 { ".debug_line.dwo", ".zdebug_line.dwo" },
332 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
333 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
334 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
335 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
336 { ".debug_str.dwo", ".zdebug_str.dwo" },
337 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
338 { ".debug_types.dwo", ".zdebug_types.dwo" },
339 { ".debug_cu_index", ".zdebug_cu_index" },
340 { ".debug_tu_index", ".zdebug_tu_index" },
341 };
342
343 /* local data types */
344
345 /* Type used for delaying computation of method physnames.
346 See comments for compute_delayed_physnames. */
347 struct delayed_method_info
348 {
349 /* The type to which the method is attached, i.e., its parent class. */
350 struct type *type;
351
352 /* The index of the method in the type's function fieldlists. */
353 int fnfield_index;
354
355 /* The index of the method in the fieldlist. */
356 int index;
357
358 /* The name of the DIE. */
359 const char *name;
360
361 /* The DIE associated with this method. */
362 struct die_info *die;
363 };
364
365 /* Internal state when decoding a particular compilation unit. */
366 struct dwarf2_cu
367 {
368 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
369 ~dwarf2_cu ();
370
371 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
372
373 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
374 Create the set of symtabs used by this TU, or if this TU is sharing
375 symtabs with another TU and the symtabs have already been created
376 then restore those symtabs in the line header.
377 We don't need the pc/line-number mapping for type units. */
378 void setup_type_unit_groups (struct die_info *die);
379
380 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
381 buildsym_compunit constructor. */
382 struct compunit_symtab *start_symtab (const char *name,
383 const char *comp_dir,
384 CORE_ADDR low_pc);
385
386 /* Reset the builder. */
387 void reset_builder () { m_builder.reset (); }
388
389 /* The header of the compilation unit. */
390 struct comp_unit_head header {};
391
392 /* Base address of this compilation unit. */
393 CORE_ADDR base_address = 0;
394
395 /* Non-zero if base_address has been set. */
396 int base_known = 0;
397
398 /* The language we are debugging. */
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
401
402 const char *producer = nullptr;
403
404 private:
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
407 std::unique_ptr<buildsym_compunit> m_builder;
408
409 public:
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope = nullptr;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies = nullptr;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 auto_obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
434
435 /* Backlink to our per_cu entry. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used = 0;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash = nullptr;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies = nullptr;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies = nullptr;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header = nullptr;
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
460 die_info *line_header_die_owner = nullptr;
461
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 std::vector<delayed_method_info> method_list;
465
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab = nullptr;
468
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
478 struct dwo_unit *dwo_unit = nullptr;
479
480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
481 Note this value comes from the Fission stub CU/TU's DIE. */
482 gdb::optional<ULONGEST> addr_base;
483
484 /* The DW_AT_rnglists_base attribute if present.
485 Note this value comes from the Fission stub CU/TU's DIE.
486 Also note that the value is zero in the non-DWO case so this value can
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
493 ULONGEST ranges_base = 0;
494
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
509 /* Mark used when releasing cached dies. */
510 bool mark : 1;
511
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
516 bool has_loclist : 1;
517
518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
525 bool producer_is_icc : 1;
526 bool producer_is_icc_lt_14 : 1;
527 bool producer_is_codewarrior : 1;
528
529 /* When true, the file that we're processing is known to have
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
533 bool processing_has_namespace_info : 1;
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
554 };
555
556 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559 struct stmt_list_hash
560 {
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
565 sect_offset line_sect_off;
566 };
567
568 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571 struct type_unit_group
572 {
573 /* dwarf2read.c's main "handle" on a TU symtab.
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
576 a "per_cu" handle on the symtab.
577 This PER_CU is recognized by having no section. */
578 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
579 struct dwarf2_per_cu_data per_cu;
580
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
584 std::vector<signatured_type *> *tus;
585
586 /* The compunit symtab.
587 Type units in a group needn't all be defined in the same source file,
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
590
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
593
594 /* The number of symtabs from the line header.
595 The value here must match line_header.num_file_names. */
596 unsigned int num_symtabs;
597
598 /* The symbol tables for this TU (obtained from the files listed in
599 DW_AT_stmt_list).
600 WARNING: The order of entries here must match the order of entries
601 in the line header. After the first TU using this type_unit_group, the
602 line header for the subsequent TUs is recreated from this. This is done
603 because we need to use the same symtabs for each TU using the same
604 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
605 there's no guarantee the line header doesn't have duplicate entries. */
606 struct symtab **symtabs;
607 };
608
609 /* These sections are what may appear in a (real or virtual) DWO file. */
610
611 struct dwo_sections
612 {
613 struct dwarf2_section_info abbrev;
614 struct dwarf2_section_info line;
615 struct dwarf2_section_info loc;
616 struct dwarf2_section_info loclists;
617 struct dwarf2_section_info macinfo;
618 struct dwarf2_section_info macro;
619 struct dwarf2_section_info str;
620 struct dwarf2_section_info str_offsets;
621 /* In the case of a virtual DWO file, these two are unused. */
622 struct dwarf2_section_info info;
623 std::vector<dwarf2_section_info> types;
624 };
625
626 /* CUs/TUs in DWP/DWO files. */
627
628 struct dwo_unit
629 {
630 /* Backlink to the containing struct dwo_file. */
631 struct dwo_file *dwo_file;
632
633 /* The "id" that distinguishes this CU/TU.
634 .debug_info calls this "dwo_id", .debug_types calls this "signature".
635 Since signatures came first, we stick with it for consistency. */
636 ULONGEST signature;
637
638 /* The section this CU/TU lives in, in the DWO file. */
639 struct dwarf2_section_info *section;
640
641 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
642 sect_offset sect_off;
643 unsigned int length;
644
645 /* For types, offset in the type's DIE of the type defined by this TU. */
646 cu_offset type_offset_in_tu;
647 };
648
649 /* include/dwarf2.h defines the DWP section codes.
650 It defines a max value but it doesn't define a min value, which we
651 use for error checking, so provide one. */
652
653 enum dwp_v2_section_ids
654 {
655 DW_SECT_MIN = 1
656 };
657
658 /* Data for one DWO file.
659
660 This includes virtual DWO files (a virtual DWO file is a DWO file as it
661 appears in a DWP file). DWP files don't really have DWO files per se -
662 comdat folding of types "loses" the DWO file they came from, and from
663 a high level view DWP files appear to contain a mass of random types.
664 However, to maintain consistency with the non-DWP case we pretend DWP
665 files contain virtual DWO files, and we assign each TU with one virtual
666 DWO file (generally based on the line and abbrev section offsets -
667 a heuristic that seems to work in practice). */
668
669 struct dwo_file
670 {
671 dwo_file () = default;
672 DISABLE_COPY_AND_ASSIGN (dwo_file);
673
674 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
675 For virtual DWO files the name is constructed from the section offsets
676 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
677 from related CU+TUs. */
678 const char *dwo_name = nullptr;
679
680 /* The DW_AT_comp_dir attribute. */
681 const char *comp_dir = nullptr;
682
683 /* The bfd, when the file is open. Otherwise this is NULL.
684 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
685 gdb_bfd_ref_ptr dbfd;
686
687 /* The sections that make up this DWO file.
688 Remember that for virtual DWO files in DWP V2, these are virtual
689 sections (for lack of a better name). */
690 struct dwo_sections sections {};
691
692 /* The CUs in the file.
693 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
694 an extension to handle LLVM's Link Time Optimization output (where
695 multiple source files may be compiled into a single object/dwo pair). */
696 htab_up cus;
697
698 /* Table of TUs in the file.
699 Each element is a struct dwo_unit. */
700 htab_up tus;
701 };
702
703 /* These sections are what may appear in a DWP file. */
704
705 struct dwp_sections
706 {
707 /* These are used by both DWP version 1 and 2. */
708 struct dwarf2_section_info str;
709 struct dwarf2_section_info cu_index;
710 struct dwarf2_section_info tu_index;
711
712 /* These are only used by DWP version 2 files.
713 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
714 sections are referenced by section number, and are not recorded here.
715 In DWP version 2 there is at most one copy of all these sections, each
716 section being (effectively) comprised of the concatenation of all of the
717 individual sections that exist in the version 1 format.
718 To keep the code simple we treat each of these concatenated pieces as a
719 section itself (a virtual section?). */
720 struct dwarf2_section_info abbrev;
721 struct dwarf2_section_info info;
722 struct dwarf2_section_info line;
723 struct dwarf2_section_info loc;
724 struct dwarf2_section_info macinfo;
725 struct dwarf2_section_info macro;
726 struct dwarf2_section_info str_offsets;
727 struct dwarf2_section_info types;
728 };
729
730 /* These sections are what may appear in a virtual DWO file in DWP version 1.
731 A virtual DWO file is a DWO file as it appears in a DWP file. */
732
733 struct virtual_v1_dwo_sections
734 {
735 struct dwarf2_section_info abbrev;
736 struct dwarf2_section_info line;
737 struct dwarf2_section_info loc;
738 struct dwarf2_section_info macinfo;
739 struct dwarf2_section_info macro;
740 struct dwarf2_section_info str_offsets;
741 /* Each DWP hash table entry records one CU or one TU.
742 That is recorded here, and copied to dwo_unit.section. */
743 struct dwarf2_section_info info_or_types;
744 };
745
746 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
747 In version 2, the sections of the DWO files are concatenated together
748 and stored in one section of that name. Thus each ELF section contains
749 several "virtual" sections. */
750
751 struct virtual_v2_dwo_sections
752 {
753 bfd_size_type abbrev_offset;
754 bfd_size_type abbrev_size;
755
756 bfd_size_type line_offset;
757 bfd_size_type line_size;
758
759 bfd_size_type loc_offset;
760 bfd_size_type loc_size;
761
762 bfd_size_type macinfo_offset;
763 bfd_size_type macinfo_size;
764
765 bfd_size_type macro_offset;
766 bfd_size_type macro_size;
767
768 bfd_size_type str_offsets_offset;
769 bfd_size_type str_offsets_size;
770
771 /* Each DWP hash table entry records one CU or one TU.
772 That is recorded here, and copied to dwo_unit.section. */
773 bfd_size_type info_or_types_offset;
774 bfd_size_type info_or_types_size;
775 };
776
777 /* Contents of DWP hash tables. */
778
779 struct dwp_hash_table
780 {
781 uint32_t version, nr_columns;
782 uint32_t nr_units, nr_slots;
783 const gdb_byte *hash_table, *unit_table;
784 union
785 {
786 struct
787 {
788 const gdb_byte *indices;
789 } v1;
790 struct
791 {
792 /* This is indexed by column number and gives the id of the section
793 in that column. */
794 #define MAX_NR_V2_DWO_SECTIONS \
795 (1 /* .debug_info or .debug_types */ \
796 + 1 /* .debug_abbrev */ \
797 + 1 /* .debug_line */ \
798 + 1 /* .debug_loc */ \
799 + 1 /* .debug_str_offsets */ \
800 + 1 /* .debug_macro or .debug_macinfo */)
801 int section_ids[MAX_NR_V2_DWO_SECTIONS];
802 const gdb_byte *offsets;
803 const gdb_byte *sizes;
804 } v2;
805 } section_pool;
806 };
807
808 /* Data for one DWP file. */
809
810 struct dwp_file
811 {
812 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
813 : name (name_),
814 dbfd (std::move (abfd))
815 {
816 }
817
818 /* Name of the file. */
819 const char *name;
820
821 /* File format version. */
822 int version = 0;
823
824 /* The bfd. */
825 gdb_bfd_ref_ptr dbfd;
826
827 /* Section info for this file. */
828 struct dwp_sections sections {};
829
830 /* Table of CUs in the file. */
831 const struct dwp_hash_table *cus = nullptr;
832
833 /* Table of TUs in the file. */
834 const struct dwp_hash_table *tus = nullptr;
835
836 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
837 htab_up loaded_cus;
838 htab_up loaded_tus;
839
840 /* Table to map ELF section numbers to their sections.
841 This is only needed for the DWP V1 file format. */
842 unsigned int num_sections = 0;
843 asection **elf_sections = nullptr;
844 };
845
846 /* Struct used to pass misc. parameters to read_die_and_children, et
847 al. which are used for both .debug_info and .debug_types dies.
848 All parameters here are unchanging for the life of the call. This
849 struct exists to abstract away the constant parameters of die reading. */
850
851 struct die_reader_specs
852 {
853 /* The bfd of die_section. */
854 bfd* abfd;
855
856 /* The CU of the DIE we are parsing. */
857 struct dwarf2_cu *cu;
858
859 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
860 struct dwo_file *dwo_file;
861
862 /* The section the die comes from.
863 This is either .debug_info or .debug_types, or the .dwo variants. */
864 struct dwarf2_section_info *die_section;
865
866 /* die_section->buffer. */
867 const gdb_byte *buffer;
868
869 /* The end of the buffer. */
870 const gdb_byte *buffer_end;
871
872 /* The abbreviation table to use when reading the DIEs. */
873 struct abbrev_table *abbrev_table;
874 };
875
876 /* A subclass of die_reader_specs that holds storage and has complex
877 constructor and destructor behavior. */
878
879 class cutu_reader : public die_reader_specs
880 {
881 public:
882
883 cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct abbrev_table *abbrev_table,
885 int use_existing_cu,
886 bool skip_partial);
887
888 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
889 struct dwarf2_cu *parent_cu = nullptr,
890 struct dwo_file *dwo_file = nullptr);
891
892 DISABLE_COPY_AND_ASSIGN (cutu_reader);
893
894 const gdb_byte *info_ptr = nullptr;
895 struct die_info *comp_unit_die = nullptr;
896 bool dummy_p = false;
897
898 /* Release the new CU, putting it on the chain. This cannot be done
899 for dummy CUs. */
900 void keep ();
901
902 private:
903 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
904 int use_existing_cu);
905
906 struct dwarf2_per_cu_data *m_this_cu;
907 std::unique_ptr<dwarf2_cu> m_new_cu;
908
909 /* The ordinary abbreviation table. */
910 abbrev_table_up m_abbrev_table_holder;
911
912 /* The DWO abbreviation table. */
913 abbrev_table_up m_dwo_abbrev_table;
914 };
915
916 /* When we construct a partial symbol table entry we only
917 need this much information. */
918 struct partial_die_info : public allocate_on_obstack
919 {
920 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
921
922 /* Disable assign but still keep copy ctor, which is needed
923 load_partial_dies. */
924 partial_die_info& operator=(const partial_die_info& rhs) = delete;
925
926 /* Adjust the partial die before generating a symbol for it. This
927 function may set the is_external flag or change the DIE's
928 name. */
929 void fixup (struct dwarf2_cu *cu);
930
931 /* Read a minimal amount of information into the minimal die
932 structure. */
933 const gdb_byte *read (const struct die_reader_specs *reader,
934 const struct abbrev_info &abbrev,
935 const gdb_byte *info_ptr);
936
937 /* Offset of this DIE. */
938 const sect_offset sect_off;
939
940 /* DWARF-2 tag for this DIE. */
941 const ENUM_BITFIELD(dwarf_tag) tag : 16;
942
943 /* Assorted flags describing the data found in this DIE. */
944 const unsigned int has_children : 1;
945
946 unsigned int is_external : 1;
947 unsigned int is_declaration : 1;
948 unsigned int has_type : 1;
949 unsigned int has_specification : 1;
950 unsigned int has_pc_info : 1;
951 unsigned int may_be_inlined : 1;
952
953 /* This DIE has been marked DW_AT_main_subprogram. */
954 unsigned int main_subprogram : 1;
955
956 /* Flag set if the SCOPE field of this structure has been
957 computed. */
958 unsigned int scope_set : 1;
959
960 /* Flag set if the DIE has a byte_size attribute. */
961 unsigned int has_byte_size : 1;
962
963 /* Flag set if the DIE has a DW_AT_const_value attribute. */
964 unsigned int has_const_value : 1;
965
966 /* Flag set if any of the DIE's children are template arguments. */
967 unsigned int has_template_arguments : 1;
968
969 /* Flag set if fixup has been called on this die. */
970 unsigned int fixup_called : 1;
971
972 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
973 unsigned int is_dwz : 1;
974
975 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
976 unsigned int spec_is_dwz : 1;
977
978 /* The name of this DIE. Normally the value of DW_AT_name, but
979 sometimes a default name for unnamed DIEs. */
980 const char *name = nullptr;
981
982 /* The linkage name, if present. */
983 const char *linkage_name = nullptr;
984
985 /* The scope to prepend to our children. This is generally
986 allocated on the comp_unit_obstack, so will disappear
987 when this compilation unit leaves the cache. */
988 const char *scope = nullptr;
989
990 /* Some data associated with the partial DIE. The tag determines
991 which field is live. */
992 union
993 {
994 /* The location description associated with this DIE, if any. */
995 struct dwarf_block *locdesc;
996 /* The offset of an import, for DW_TAG_imported_unit. */
997 sect_offset sect_off;
998 } d {};
999
1000 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1001 CORE_ADDR lowpc = 0;
1002 CORE_ADDR highpc = 0;
1003
1004 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1005 DW_AT_sibling, if any. */
1006 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1007 could return DW_AT_sibling values to its caller load_partial_dies. */
1008 const gdb_byte *sibling = nullptr;
1009
1010 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1011 DW_AT_specification (or DW_AT_abstract_origin or
1012 DW_AT_extension). */
1013 sect_offset spec_offset {};
1014
1015 /* Pointers to this DIE's parent, first child, and next sibling,
1016 if any. */
1017 struct partial_die_info *die_parent = nullptr;
1018 struct partial_die_info *die_child = nullptr;
1019 struct partial_die_info *die_sibling = nullptr;
1020
1021 friend struct partial_die_info *
1022 dwarf2_cu::find_partial_die (sect_offset sect_off);
1023
1024 private:
1025 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1026 partial_die_info (sect_offset sect_off)
1027 : partial_die_info (sect_off, DW_TAG_padding, 0)
1028 {
1029 }
1030
1031 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1032 int has_children_)
1033 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1034 {
1035 is_external = 0;
1036 is_declaration = 0;
1037 has_type = 0;
1038 has_specification = 0;
1039 has_pc_info = 0;
1040 may_be_inlined = 0;
1041 main_subprogram = 0;
1042 scope_set = 0;
1043 has_byte_size = 0;
1044 has_const_value = 0;
1045 has_template_arguments = 0;
1046 fixup_called = 0;
1047 is_dwz = 0;
1048 spec_is_dwz = 0;
1049 }
1050 };
1051
1052 /* This data structure holds a complete die structure. */
1053 struct die_info
1054 {
1055 /* DWARF-2 tag for this DIE. */
1056 ENUM_BITFIELD(dwarf_tag) tag : 16;
1057
1058 /* Number of attributes */
1059 unsigned char num_attrs;
1060
1061 /* True if we're presently building the full type name for the
1062 type derived from this DIE. */
1063 unsigned char building_fullname : 1;
1064
1065 /* True if this die is in process. PR 16581. */
1066 unsigned char in_process : 1;
1067
1068 /* True if this DIE has children. */
1069 unsigned char has_children : 1;
1070
1071 /* Abbrev number */
1072 unsigned int abbrev;
1073
1074 /* Offset in .debug_info or .debug_types section. */
1075 sect_offset sect_off;
1076
1077 /* The dies in a compilation unit form an n-ary tree. PARENT
1078 points to this die's parent; CHILD points to the first child of
1079 this node; and all the children of a given node are chained
1080 together via their SIBLING fields. */
1081 struct die_info *child; /* Its first child, if any. */
1082 struct die_info *sibling; /* Its next sibling, if any. */
1083 struct die_info *parent; /* Its parent, if any. */
1084
1085 /* An array of attributes, with NUM_ATTRS elements. There may be
1086 zero, but it's not common and zero-sized arrays are not
1087 sufficiently portable C. */
1088 struct attribute attrs[1];
1089 };
1090
1091 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1092 but this would require a corresponding change in unpack_field_as_long
1093 and friends. */
1094 static int bits_per_byte = 8;
1095
1096 /* When reading a variant or variant part, we track a bit more
1097 information about the field, and store it in an object of this
1098 type. */
1099
1100 struct variant_field
1101 {
1102 /* If we see a DW_TAG_variant, then this will be the discriminant
1103 value. */
1104 ULONGEST discriminant_value;
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch;
1108 /* While reading a DW_TAG_variant_part, this will be set if this
1109 field is the discriminant. */
1110 bool is_discriminant;
1111 };
1112
1113 struct nextfield
1114 {
1115 int accessibility = 0;
1116 int virtuality = 0;
1117 /* Extra information to describe a variant or variant part. */
1118 struct variant_field variant {};
1119 struct field field {};
1120 };
1121
1122 struct fnfieldlist
1123 {
1124 const char *name = nullptr;
1125 std::vector<struct fn_field> fnfields;
1126 };
1127
1128 /* The routines that read and process dies for a C struct or C++ class
1129 pass lists of data member fields and lists of member function fields
1130 in an instance of a field_info structure, as defined below. */
1131 struct field_info
1132 {
1133 /* List of data member and baseclasses fields. */
1134 std::vector<struct nextfield> fields;
1135 std::vector<struct nextfield> baseclasses;
1136
1137 /* Number of fields (including baseclasses). */
1138 int nfields = 0;
1139
1140 /* Set if the accessibility of one of the fields is not public. */
1141 int non_public_fields = 0;
1142
1143 /* Member function fieldlist array, contains name of possibly overloaded
1144 member function, number of overloaded member functions and a pointer
1145 to the head of the member function field chain. */
1146 std::vector<struct fnfieldlist> fnfieldlists;
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1150 std::vector<struct decl_field> typedef_field_list;
1151
1152 /* Nested types defined by this class and the number of elements in this
1153 list. */
1154 std::vector<struct decl_field> nested_types_list;
1155 };
1156
1157 /* Loaded secondary compilation units are kept in memory until they
1158 have not been referenced for the processing of this many
1159 compilation units. Set this to zero to disable caching. Cache
1160 sizes of up to at least twenty will improve startup time for
1161 typical inter-CU-reference binaries, at an obvious memory cost. */
1162 static int dwarf_max_cache_age = 5;
1163 static void
1164 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1165 struct cmd_list_element *c, const char *value)
1166 {
1167 fprintf_filtered (file, _("The upper bound on the age of cached "
1168 "DWARF compilation units is %s.\n"),
1169 value);
1170 }
1171 \f
1172 /* local function prototypes */
1173
1174 static void dwarf2_find_base_address (struct die_info *die,
1175 struct dwarf2_cu *cu);
1176
1177 static dwarf2_psymtab *create_partial_symtab
1178 (struct dwarf2_per_cu_data *per_cu, const char *name);
1179
1180 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1181 const gdb_byte *info_ptr,
1182 struct die_info *type_unit_die);
1183
1184 static void dwarf2_build_psymtabs_hard
1185 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1186
1187 static void scan_partial_symbols (struct partial_die_info *,
1188 CORE_ADDR *, CORE_ADDR *,
1189 int, struct dwarf2_cu *);
1190
1191 static void add_partial_symbol (struct partial_die_info *,
1192 struct dwarf2_cu *);
1193
1194 static void add_partial_namespace (struct partial_die_info *pdi,
1195 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1196 int set_addrmap, struct dwarf2_cu *cu);
1197
1198 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1199 CORE_ADDR *highpc, int set_addrmap,
1200 struct dwarf2_cu *cu);
1201
1202 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1203 struct dwarf2_cu *cu);
1204
1205 static void add_partial_subprogram (struct partial_die_info *pdi,
1206 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1207 int need_pc, struct dwarf2_cu *cu);
1208
1209 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1210
1211 static struct partial_die_info *load_partial_dies
1212 (const struct die_reader_specs *, const gdb_byte *, int);
1213
1214 /* A pair of partial_die_info and compilation unit. */
1215 struct cu_partial_die_info
1216 {
1217 /* The compilation unit of the partial_die_info. */
1218 struct dwarf2_cu *cu;
1219 /* A partial_die_info. */
1220 struct partial_die_info *pdi;
1221
1222 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1223 : cu (cu),
1224 pdi (pdi)
1225 { /* Nothing. */ }
1226
1227 private:
1228 cu_partial_die_info () = delete;
1229 };
1230
1231 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1232 struct dwarf2_cu *);
1233
1234 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1235 struct attribute *, struct attr_abbrev *,
1236 const gdb_byte *, bool *need_reprocess);
1237
1238 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1239 struct attribute *attr);
1240
1241 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1242
1243 static LONGEST read_checked_initial_length_and_offset
1244 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1245 unsigned int *, unsigned int *);
1246
1247 static sect_offset read_abbrev_offset
1248 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1249 struct dwarf2_section_info *, sect_offset);
1250
1251 static const char *read_indirect_string
1252 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1253 const struct comp_unit_head *, unsigned int *);
1254
1255 static const char *read_indirect_line_string
1256 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1257 const struct comp_unit_head *, unsigned int *);
1258
1259 static const char *read_indirect_string_at_offset
1260 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1261 LONGEST str_offset);
1262
1263 static const char *read_indirect_string_from_dwz
1264 (struct objfile *objfile, struct dwz_file *, LONGEST);
1265
1266 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1267 const gdb_byte *,
1268 unsigned int *);
1269
1270 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1271 ULONGEST str_index);
1272
1273 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1274 ULONGEST str_index);
1275
1276 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1277
1278 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1279 struct dwarf2_cu *);
1280
1281 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1282 unsigned int);
1283
1284 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1285 struct dwarf2_cu *cu);
1286
1287 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1288
1289 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1290 struct dwarf2_cu *cu);
1291
1292 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1293
1294 static struct die_info *die_specification (struct die_info *die,
1295 struct dwarf2_cu **);
1296
1297 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1298 struct dwarf2_cu *cu);
1299
1300 static void dwarf_decode_lines (struct line_header *, const char *,
1301 struct dwarf2_cu *, dwarf2_psymtab *,
1302 CORE_ADDR, int decode_mapping);
1303
1304 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1305 const char *);
1306
1307 static struct symbol *new_symbol (struct die_info *, struct type *,
1308 struct dwarf2_cu *, struct symbol * = NULL);
1309
1310 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1311 struct dwarf2_cu *);
1312
1313 static void dwarf2_const_value_attr (const struct attribute *attr,
1314 struct type *type,
1315 const char *name,
1316 struct obstack *obstack,
1317 struct dwarf2_cu *cu, LONGEST *value,
1318 const gdb_byte **bytes,
1319 struct dwarf2_locexpr_baton **baton);
1320
1321 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1322
1323 static int need_gnat_info (struct dwarf2_cu *);
1324
1325 static struct type *die_descriptive_type (struct die_info *,
1326 struct dwarf2_cu *);
1327
1328 static void set_descriptive_type (struct type *, struct die_info *,
1329 struct dwarf2_cu *);
1330
1331 static struct type *die_containing_type (struct die_info *,
1332 struct dwarf2_cu *);
1333
1334 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1335 struct dwarf2_cu *);
1336
1337 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1338
1339 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1340
1341 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1342
1343 static char *typename_concat (struct obstack *obs, const char *prefix,
1344 const char *suffix, int physname,
1345 struct dwarf2_cu *cu);
1346
1347 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1352
1353 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1354
1355 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1356
1357 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1358
1359 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1360 struct dwarf2_cu *, dwarf2_psymtab *);
1361
1362 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1363 values. Keep the items ordered with increasing constraints compliance. */
1364 enum pc_bounds_kind
1365 {
1366 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1367 PC_BOUNDS_NOT_PRESENT,
1368
1369 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1370 were present but they do not form a valid range of PC addresses. */
1371 PC_BOUNDS_INVALID,
1372
1373 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1374 PC_BOUNDS_RANGES,
1375
1376 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1377 PC_BOUNDS_HIGH_LOW,
1378 };
1379
1380 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *,
1383 dwarf2_psymtab *);
1384
1385 static void get_scope_pc_bounds (struct die_info *,
1386 CORE_ADDR *, CORE_ADDR *,
1387 struct dwarf2_cu *);
1388
1389 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1390 CORE_ADDR, struct dwarf2_cu *);
1391
1392 static void dwarf2_add_field (struct field_info *, struct die_info *,
1393 struct dwarf2_cu *);
1394
1395 static void dwarf2_attach_fields_to_type (struct field_info *,
1396 struct type *, struct dwarf2_cu *);
1397
1398 static void dwarf2_add_member_fn (struct field_info *,
1399 struct die_info *, struct type *,
1400 struct dwarf2_cu *);
1401
1402 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1403 struct type *,
1404 struct dwarf2_cu *);
1405
1406 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1407
1408 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1409
1410 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1411
1412 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1413
1414 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1415
1416 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1417
1418 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1419
1420 static struct type *read_module_type (struct die_info *die,
1421 struct dwarf2_cu *cu);
1422
1423 static const char *namespace_name (struct die_info *die,
1424 int *is_anonymous, struct dwarf2_cu *);
1425
1426 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1427
1428 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1429
1430 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1431 struct dwarf2_cu *);
1432
1433 static struct die_info *read_die_and_siblings_1
1434 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1435 struct die_info *);
1436
1437 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1438 const gdb_byte *info_ptr,
1439 const gdb_byte **new_info_ptr,
1440 struct die_info *parent);
1441
1442 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1443 struct die_info **, const gdb_byte *,
1444 int);
1445
1446 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1447 struct die_info **, const gdb_byte *);
1448
1449 static void process_die (struct die_info *, struct dwarf2_cu *);
1450
1451 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1452 struct obstack *);
1453
1454 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1455
1456 static const char *dwarf2_full_name (const char *name,
1457 struct die_info *die,
1458 struct dwarf2_cu *cu);
1459
1460 static const char *dwarf2_physname (const char *name, struct die_info *die,
1461 struct dwarf2_cu *cu);
1462
1463 static struct die_info *dwarf2_extension (struct die_info *die,
1464 struct dwarf2_cu **);
1465
1466 static const char *dwarf_tag_name (unsigned int);
1467
1468 static const char *dwarf_attr_name (unsigned int);
1469
1470 static const char *dwarf_form_name (unsigned int);
1471
1472 static const char *dwarf_bool_name (unsigned int);
1473
1474 static const char *dwarf_type_encoding_name (unsigned int);
1475
1476 static struct die_info *sibling_die (struct die_info *);
1477
1478 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1479
1480 static void dump_die_for_error (struct die_info *);
1481
1482 static void dump_die_1 (struct ui_file *, int level, int max_level,
1483 struct die_info *);
1484
1485 /*static*/ void dump_die (struct die_info *, int max_level);
1486
1487 static void store_in_ref_table (struct die_info *,
1488 struct dwarf2_cu *);
1489
1490 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1491
1492 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1493
1494 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498 static struct die_info *follow_die_ref (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502 static struct die_info *follow_die_sig (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu **);
1505
1506 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509 static struct type *get_DW_AT_signature_type (struct die_info *,
1510 const struct attribute *,
1511 struct dwarf2_cu *);
1512
1513 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1514
1515 static void read_signatured_type (struct signatured_type *);
1516
1517 static int attr_to_dynamic_prop (const struct attribute *attr,
1518 struct die_info *die, struct dwarf2_cu *cu,
1519 struct dynamic_prop *prop, struct type *type);
1520
1521 /* memory allocation interface */
1522
1523 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1524
1525 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1526
1527 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1528
1529 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1530 struct dwarf2_loclist_baton *baton,
1531 const struct attribute *attr);
1532
1533 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1534 struct symbol *sym,
1535 struct dwarf2_cu *cu,
1536 int is_block);
1537
1538 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1539 const gdb_byte *info_ptr,
1540 struct abbrev_info *abbrev);
1541
1542 static hashval_t partial_die_hash (const void *item);
1543
1544 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1545
1546 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1547 (sect_offset sect_off, unsigned int offset_in_dwz,
1548 struct dwarf2_per_objfile *dwarf2_per_objfile);
1549
1550 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1551 struct die_info *comp_unit_die,
1552 enum language pretend_language);
1553
1554 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1555
1556 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1557
1558 static struct type *set_die_type (struct die_info *, struct type *,
1559 struct dwarf2_cu *);
1560
1561 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1562
1563 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1564
1565 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1566 enum language);
1567
1568 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1569 enum language);
1570
1571 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1572 enum language);
1573
1574 static void dwarf2_add_dependence (struct dwarf2_cu *,
1575 struct dwarf2_per_cu_data *);
1576
1577 static void dwarf2_mark (struct dwarf2_cu *);
1578
1579 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1580
1581 static struct type *get_die_type_at_offset (sect_offset,
1582 struct dwarf2_per_cu_data *);
1583
1584 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1585
1586 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1587 enum language pretend_language);
1588
1589 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1590
1591 /* Class, the destructor of which frees all allocated queue entries. This
1592 will only have work to do if an error was thrown while processing the
1593 dwarf. If no error was thrown then the queue entries should have all
1594 been processed, and freed, as we went along. */
1595
1596 class dwarf2_queue_guard
1597 {
1598 public:
1599 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1600 : m_per_objfile (per_objfile)
1601 {
1602 }
1603
1604 /* Free any entries remaining on the queue. There should only be
1605 entries left if we hit an error while processing the dwarf. */
1606 ~dwarf2_queue_guard ()
1607 {
1608 /* Ensure that no memory is allocated by the queue. */
1609 std::queue<dwarf2_queue_item> empty;
1610 std::swap (m_per_objfile->queue, empty);
1611 }
1612
1613 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1614
1615 private:
1616 dwarf2_per_objfile *m_per_objfile;
1617 };
1618
1619 dwarf2_queue_item::~dwarf2_queue_item ()
1620 {
1621 /* Anything still marked queued is likely to be in an
1622 inconsistent state, so discard it. */
1623 if (per_cu->queued)
1624 {
1625 if (per_cu->cu != NULL)
1626 free_one_cached_comp_unit (per_cu);
1627 per_cu->queued = 0;
1628 }
1629 }
1630
1631 /* The return type of find_file_and_directory. Note, the enclosed
1632 string pointers are only valid while this object is valid. */
1633
1634 struct file_and_directory
1635 {
1636 /* The filename. This is never NULL. */
1637 const char *name;
1638
1639 /* The compilation directory. NULL if not known. If we needed to
1640 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1641 points directly to the DW_AT_comp_dir string attribute owned by
1642 the obstack that owns the DIE. */
1643 const char *comp_dir;
1644
1645 /* If we needed to build a new string for comp_dir, this is what
1646 owns the storage. */
1647 std::string comp_dir_storage;
1648 };
1649
1650 static file_and_directory find_file_and_directory (struct die_info *die,
1651 struct dwarf2_cu *cu);
1652
1653 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1654
1655 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1656
1657 static struct dwo_unit *lookup_dwo_unit_in_dwp
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1659 struct dwp_file *dwp_file, const char *comp_dir,
1660 ULONGEST signature, int is_debug_types);
1661
1662 static struct dwp_file *get_dwp_file
1663 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1664
1665 static struct dwo_unit *lookup_dwo_comp_unit
1666 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1667
1668 static struct dwo_unit *lookup_dwo_type_unit
1669 (struct signatured_type *, const char *, const char *);
1670
1671 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1672
1673 /* A unique pointer to a dwo_file. */
1674
1675 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1676
1677 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1678
1679 static void check_producer (struct dwarf2_cu *cu);
1680
1681 static void free_line_header_voidp (void *arg);
1682 \f
1683 /* Various complaints about symbol reading that don't abort the process. */
1684
1685 static void
1686 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1687 {
1688 complaint (_("statement list doesn't fit in .debug_line section"));
1689 }
1690
1691 static void
1692 dwarf2_debug_line_missing_file_complaint (void)
1693 {
1694 complaint (_(".debug_line section has line data without a file"));
1695 }
1696
1697 static void
1698 dwarf2_debug_line_missing_end_sequence_complaint (void)
1699 {
1700 complaint (_(".debug_line section has line "
1701 "program sequence without an end"));
1702 }
1703
1704 static void
1705 dwarf2_complex_location_expr_complaint (void)
1706 {
1707 complaint (_("location expression too complex"));
1708 }
1709
1710 static void
1711 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1712 int arg3)
1713 {
1714 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1715 arg1, arg2, arg3);
1716 }
1717
1718 static void
1719 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1720 {
1721 complaint (_("debug info runs off end of %s section"
1722 " [in module %s]"),
1723 section->get_name (),
1724 section->get_file_name ());
1725 }
1726
1727 static void
1728 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1729 {
1730 complaint (_("macro debug info contains a "
1731 "malformed macro definition:\n`%s'"),
1732 arg1);
1733 }
1734
1735 static void
1736 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1737 {
1738 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1739 arg1, arg2);
1740 }
1741
1742 /* Hash function for line_header_hash. */
1743
1744 static hashval_t
1745 line_header_hash (const struct line_header *ofs)
1746 {
1747 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1748 }
1749
1750 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1751
1752 static hashval_t
1753 line_header_hash_voidp (const void *item)
1754 {
1755 const struct line_header *ofs = (const struct line_header *) item;
1756
1757 return line_header_hash (ofs);
1758 }
1759
1760 /* Equality function for line_header_hash. */
1761
1762 static int
1763 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1764 {
1765 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1766 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1767
1768 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1769 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1770 }
1771
1772 \f
1773
1774 /* See declaration. */
1775
1776 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1777 const dwarf2_debug_sections *names,
1778 bool can_copy_)
1779 : objfile (objfile_),
1780 can_copy (can_copy_)
1781 {
1782 if (names == NULL)
1783 names = &dwarf2_elf_names;
1784
1785 bfd *obfd = objfile->obfd;
1786
1787 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1788 locate_sections (obfd, sec, *names);
1789 }
1790
1791 dwarf2_per_objfile::~dwarf2_per_objfile ()
1792 {
1793 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1794 free_cached_comp_units ();
1795
1796 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1797 per_cu->imported_symtabs_free ();
1798
1799 for (signatured_type *sig_type : all_type_units)
1800 sig_type->per_cu.imported_symtabs_free ();
1801
1802 /* Everything else should be on the objfile obstack. */
1803 }
1804
1805 /* See declaration. */
1806
1807 void
1808 dwarf2_per_objfile::free_cached_comp_units ()
1809 {
1810 dwarf2_per_cu_data *per_cu = read_in_chain;
1811 dwarf2_per_cu_data **last_chain = &read_in_chain;
1812 while (per_cu != NULL)
1813 {
1814 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1815
1816 delete per_cu->cu;
1817 *last_chain = next_cu;
1818 per_cu = next_cu;
1819 }
1820 }
1821
1822 /* A helper class that calls free_cached_comp_units on
1823 destruction. */
1824
1825 class free_cached_comp_units
1826 {
1827 public:
1828
1829 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1830 : m_per_objfile (per_objfile)
1831 {
1832 }
1833
1834 ~free_cached_comp_units ()
1835 {
1836 m_per_objfile->free_cached_comp_units ();
1837 }
1838
1839 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1840
1841 private:
1842
1843 dwarf2_per_objfile *m_per_objfile;
1844 };
1845
1846 /* Try to locate the sections we need for DWARF 2 debugging
1847 information and return true if we have enough to do something.
1848 NAMES points to the dwarf2 section names, or is NULL if the standard
1849 ELF names are used. CAN_COPY is true for formats where symbol
1850 interposition is possible and so symbol values must follow copy
1851 relocation rules. */
1852
1853 int
1854 dwarf2_has_info (struct objfile *objfile,
1855 const struct dwarf2_debug_sections *names,
1856 bool can_copy)
1857 {
1858 if (objfile->flags & OBJF_READNEVER)
1859 return 0;
1860
1861 struct dwarf2_per_objfile *dwarf2_per_objfile
1862 = get_dwarf2_per_objfile (objfile);
1863
1864 if (dwarf2_per_objfile == NULL)
1865 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1866 names,
1867 can_copy);
1868
1869 return (!dwarf2_per_objfile->info.is_virtual
1870 && dwarf2_per_objfile->info.s.section != NULL
1871 && !dwarf2_per_objfile->abbrev.is_virtual
1872 && dwarf2_per_objfile->abbrev.s.section != NULL);
1873 }
1874
1875 /* When loading sections, we look either for uncompressed section or for
1876 compressed section names. */
1877
1878 static int
1879 section_is_p (const char *section_name,
1880 const struct dwarf2_section_names *names)
1881 {
1882 if (names->normal != NULL
1883 && strcmp (section_name, names->normal) == 0)
1884 return 1;
1885 if (names->compressed != NULL
1886 && strcmp (section_name, names->compressed) == 0)
1887 return 1;
1888 return 0;
1889 }
1890
1891 /* See declaration. */
1892
1893 void
1894 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1895 const dwarf2_debug_sections &names)
1896 {
1897 flagword aflag = bfd_section_flags (sectp);
1898
1899 if ((aflag & SEC_HAS_CONTENTS) == 0)
1900 {
1901 }
1902 else if (elf_section_data (sectp)->this_hdr.sh_size
1903 > bfd_get_file_size (abfd))
1904 {
1905 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1906 warning (_("Discarding section %s which has a section size (%s"
1907 ") larger than the file size [in module %s]"),
1908 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1909 bfd_get_filename (abfd));
1910 }
1911 else if (section_is_p (sectp->name, &names.info))
1912 {
1913 this->info.s.section = sectp;
1914 this->info.size = bfd_section_size (sectp);
1915 }
1916 else if (section_is_p (sectp->name, &names.abbrev))
1917 {
1918 this->abbrev.s.section = sectp;
1919 this->abbrev.size = bfd_section_size (sectp);
1920 }
1921 else if (section_is_p (sectp->name, &names.line))
1922 {
1923 this->line.s.section = sectp;
1924 this->line.size = bfd_section_size (sectp);
1925 }
1926 else if (section_is_p (sectp->name, &names.loc))
1927 {
1928 this->loc.s.section = sectp;
1929 this->loc.size = bfd_section_size (sectp);
1930 }
1931 else if (section_is_p (sectp->name, &names.loclists))
1932 {
1933 this->loclists.s.section = sectp;
1934 this->loclists.size = bfd_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names.macinfo))
1937 {
1938 this->macinfo.s.section = sectp;
1939 this->macinfo.size = bfd_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &names.macro))
1942 {
1943 this->macro.s.section = sectp;
1944 this->macro.size = bfd_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &names.str))
1947 {
1948 this->str.s.section = sectp;
1949 this->str.size = bfd_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &names.str_offsets))
1952 {
1953 this->str_offsets.s.section = sectp;
1954 this->str_offsets.size = bfd_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &names.line_str))
1957 {
1958 this->line_str.s.section = sectp;
1959 this->line_str.size = bfd_section_size (sectp);
1960 }
1961 else if (section_is_p (sectp->name, &names.addr))
1962 {
1963 this->addr.s.section = sectp;
1964 this->addr.size = bfd_section_size (sectp);
1965 }
1966 else if (section_is_p (sectp->name, &names.frame))
1967 {
1968 this->frame.s.section = sectp;
1969 this->frame.size = bfd_section_size (sectp);
1970 }
1971 else if (section_is_p (sectp->name, &names.eh_frame))
1972 {
1973 this->eh_frame.s.section = sectp;
1974 this->eh_frame.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &names.ranges))
1977 {
1978 this->ranges.s.section = sectp;
1979 this->ranges.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &names.rnglists))
1982 {
1983 this->rnglists.s.section = sectp;
1984 this->rnglists.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &names.types))
1987 {
1988 struct dwarf2_section_info type_section;
1989
1990 memset (&type_section, 0, sizeof (type_section));
1991 type_section.s.section = sectp;
1992 type_section.size = bfd_section_size (sectp);
1993
1994 this->types.push_back (type_section);
1995 }
1996 else if (section_is_p (sectp->name, &names.gdb_index))
1997 {
1998 this->gdb_index.s.section = sectp;
1999 this->gdb_index.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &names.debug_names))
2002 {
2003 this->debug_names.s.section = sectp;
2004 this->debug_names.size = bfd_section_size (sectp);
2005 }
2006 else if (section_is_p (sectp->name, &names.debug_aranges))
2007 {
2008 this->debug_aranges.s.section = sectp;
2009 this->debug_aranges.size = bfd_section_size (sectp);
2010 }
2011
2012 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2013 && bfd_section_vma (sectp) == 0)
2014 this->has_section_at_zero = true;
2015 }
2016
2017 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2018 SECTION_NAME. */
2019
2020 void
2021 dwarf2_get_section_info (struct objfile *objfile,
2022 enum dwarf2_section_enum sect,
2023 asection **sectp, const gdb_byte **bufp,
2024 bfd_size_type *sizep)
2025 {
2026 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2027 struct dwarf2_section_info *info;
2028
2029 /* We may see an objfile without any DWARF, in which case we just
2030 return nothing. */
2031 if (data == NULL)
2032 {
2033 *sectp = NULL;
2034 *bufp = NULL;
2035 *sizep = 0;
2036 return;
2037 }
2038 switch (sect)
2039 {
2040 case DWARF2_DEBUG_FRAME:
2041 info = &data->frame;
2042 break;
2043 case DWARF2_EH_FRAME:
2044 info = &data->eh_frame;
2045 break;
2046 default:
2047 gdb_assert_not_reached ("unexpected section");
2048 }
2049
2050 info->read (objfile);
2051
2052 *sectp = info->get_bfd_section ();
2053 *bufp = info->buffer;
2054 *sizep = info->size;
2055 }
2056
2057 /* A helper function to find the sections for a .dwz file. */
2058
2059 static void
2060 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2061 {
2062 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2063
2064 /* Note that we only support the standard ELF names, because .dwz
2065 is ELF-only (at the time of writing). */
2066 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2067 {
2068 dwz_file->abbrev.s.section = sectp;
2069 dwz_file->abbrev.size = bfd_section_size (sectp);
2070 }
2071 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2072 {
2073 dwz_file->info.s.section = sectp;
2074 dwz_file->info.size = bfd_section_size (sectp);
2075 }
2076 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2077 {
2078 dwz_file->str.s.section = sectp;
2079 dwz_file->str.size = bfd_section_size (sectp);
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2082 {
2083 dwz_file->line.s.section = sectp;
2084 dwz_file->line.size = bfd_section_size (sectp);
2085 }
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2087 {
2088 dwz_file->macro.s.section = sectp;
2089 dwz_file->macro.size = bfd_section_size (sectp);
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2092 {
2093 dwz_file->gdb_index.s.section = sectp;
2094 dwz_file->gdb_index.size = bfd_section_size (sectp);
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2097 {
2098 dwz_file->debug_names.s.section = sectp;
2099 dwz_file->debug_names.size = bfd_section_size (sectp);
2100 }
2101 }
2102
2103 /* See dwarf2read.h. */
2104
2105 struct dwz_file *
2106 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2107 {
2108 const char *filename;
2109 bfd_size_type buildid_len_arg;
2110 size_t buildid_len;
2111 bfd_byte *buildid;
2112
2113 if (dwarf2_per_objfile->dwz_file != NULL)
2114 return dwarf2_per_objfile->dwz_file.get ();
2115
2116 bfd_set_error (bfd_error_no_error);
2117 gdb::unique_xmalloc_ptr<char> data
2118 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2119 &buildid_len_arg, &buildid));
2120 if (data == NULL)
2121 {
2122 if (bfd_get_error () == bfd_error_no_error)
2123 return NULL;
2124 error (_("could not read '.gnu_debugaltlink' section: %s"),
2125 bfd_errmsg (bfd_get_error ()));
2126 }
2127
2128 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2129
2130 buildid_len = (size_t) buildid_len_arg;
2131
2132 filename = data.get ();
2133
2134 std::string abs_storage;
2135 if (!IS_ABSOLUTE_PATH (filename))
2136 {
2137 gdb::unique_xmalloc_ptr<char> abs
2138 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2139
2140 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2141 filename = abs_storage.c_str ();
2142 }
2143
2144 /* First try the file name given in the section. If that doesn't
2145 work, try to use the build-id instead. */
2146 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2147 if (dwz_bfd != NULL)
2148 {
2149 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2150 dwz_bfd.reset (nullptr);
2151 }
2152
2153 if (dwz_bfd == NULL)
2154 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2155
2156 if (dwz_bfd == NULL)
2157 error (_("could not find '.gnu_debugaltlink' file for %s"),
2158 objfile_name (dwarf2_per_objfile->objfile));
2159
2160 std::unique_ptr<struct dwz_file> result
2161 (new struct dwz_file (std::move (dwz_bfd)));
2162
2163 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2164 result.get ());
2165
2166 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2167 result->dwz_bfd.get ());
2168 dwarf2_per_objfile->dwz_file = std::move (result);
2169 return dwarf2_per_objfile->dwz_file.get ();
2170 }
2171 \f
2172 /* DWARF quick_symbols_functions support. */
2173
2174 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2175 unique line tables, so we maintain a separate table of all .debug_line
2176 derived entries to support the sharing.
2177 All the quick functions need is the list of file names. We discard the
2178 line_header when we're done and don't need to record it here. */
2179 struct quick_file_names
2180 {
2181 /* The data used to construct the hash key. */
2182 struct stmt_list_hash hash;
2183
2184 /* The number of entries in file_names, real_names. */
2185 unsigned int num_file_names;
2186
2187 /* The file names from the line table, after being run through
2188 file_full_name. */
2189 const char **file_names;
2190
2191 /* The file names from the line table after being run through
2192 gdb_realpath. These are computed lazily. */
2193 const char **real_names;
2194 };
2195
2196 /* When using the index (and thus not using psymtabs), each CU has an
2197 object of this type. This is used to hold information needed by
2198 the various "quick" methods. */
2199 struct dwarf2_per_cu_quick_data
2200 {
2201 /* The file table. This can be NULL if there was no file table
2202 or it's currently not read in.
2203 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2204 struct quick_file_names *file_names;
2205
2206 /* The corresponding symbol table. This is NULL if symbols for this
2207 CU have not yet been read. */
2208 struct compunit_symtab *compunit_symtab;
2209
2210 /* A temporary mark bit used when iterating over all CUs in
2211 expand_symtabs_matching. */
2212 unsigned int mark : 1;
2213
2214 /* True if we've tried to read the file table and found there isn't one.
2215 There will be no point in trying to read it again next time. */
2216 unsigned int no_file_data : 1;
2217 };
2218
2219 /* Utility hash function for a stmt_list_hash. */
2220
2221 static hashval_t
2222 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2223 {
2224 hashval_t v = 0;
2225
2226 if (stmt_list_hash->dwo_unit != NULL)
2227 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2228 v += to_underlying (stmt_list_hash->line_sect_off);
2229 return v;
2230 }
2231
2232 /* Utility equality function for a stmt_list_hash. */
2233
2234 static int
2235 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2236 const struct stmt_list_hash *rhs)
2237 {
2238 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2239 return 0;
2240 if (lhs->dwo_unit != NULL
2241 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2242 return 0;
2243
2244 return lhs->line_sect_off == rhs->line_sect_off;
2245 }
2246
2247 /* Hash function for a quick_file_names. */
2248
2249 static hashval_t
2250 hash_file_name_entry (const void *e)
2251 {
2252 const struct quick_file_names *file_data
2253 = (const struct quick_file_names *) e;
2254
2255 return hash_stmt_list_entry (&file_data->hash);
2256 }
2257
2258 /* Equality function for a quick_file_names. */
2259
2260 static int
2261 eq_file_name_entry (const void *a, const void *b)
2262 {
2263 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2264 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2265
2266 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2267 }
2268
2269 /* Delete function for a quick_file_names. */
2270
2271 static void
2272 delete_file_name_entry (void *e)
2273 {
2274 struct quick_file_names *file_data = (struct quick_file_names *) e;
2275 int i;
2276
2277 for (i = 0; i < file_data->num_file_names; ++i)
2278 {
2279 xfree ((void*) file_data->file_names[i]);
2280 if (file_data->real_names)
2281 xfree ((void*) file_data->real_names[i]);
2282 }
2283
2284 /* The space for the struct itself lives on objfile_obstack,
2285 so we don't free it here. */
2286 }
2287
2288 /* Create a quick_file_names hash table. */
2289
2290 static htab_up
2291 create_quick_file_names_table (unsigned int nr_initial_entries)
2292 {
2293 return htab_up (htab_create_alloc (nr_initial_entries,
2294 hash_file_name_entry, eq_file_name_entry,
2295 delete_file_name_entry, xcalloc, xfree));
2296 }
2297
2298 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2299 have to be created afterwards. You should call age_cached_comp_units after
2300 processing PER_CU->CU. dw2_setup must have been already called. */
2301
2302 static void
2303 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2304 {
2305 if (per_cu->is_debug_types)
2306 load_full_type_unit (per_cu);
2307 else
2308 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2309
2310 if (per_cu->cu == NULL)
2311 return; /* Dummy CU. */
2312
2313 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2314 }
2315
2316 /* Read in the symbols for PER_CU. */
2317
2318 static void
2319 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2320 {
2321 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2322
2323 /* Skip type_unit_groups, reading the type units they contain
2324 is handled elsewhere. */
2325 if (IS_TYPE_UNIT_GROUP (per_cu))
2326 return;
2327
2328 /* The destructor of dwarf2_queue_guard frees any entries left on
2329 the queue. After this point we're guaranteed to leave this function
2330 with the dwarf queue empty. */
2331 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2332
2333 if (dwarf2_per_objfile->using_index
2334 ? per_cu->v.quick->compunit_symtab == NULL
2335 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2336 {
2337 queue_comp_unit (per_cu, language_minimal);
2338 load_cu (per_cu, skip_partial);
2339
2340 /* If we just loaded a CU from a DWO, and we're working with an index
2341 that may badly handle TUs, load all the TUs in that DWO as well.
2342 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2343 if (!per_cu->is_debug_types
2344 && per_cu->cu != NULL
2345 && per_cu->cu->dwo_unit != NULL
2346 && dwarf2_per_objfile->index_table != NULL
2347 && dwarf2_per_objfile->index_table->version <= 7
2348 /* DWP files aren't supported yet. */
2349 && get_dwp_file (dwarf2_per_objfile) == NULL)
2350 queue_and_load_all_dwo_tus (per_cu);
2351 }
2352
2353 process_queue (dwarf2_per_objfile);
2354
2355 /* Age the cache, releasing compilation units that have not
2356 been used recently. */
2357 age_cached_comp_units (dwarf2_per_objfile);
2358 }
2359
2360 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2361 the objfile from which this CU came. Returns the resulting symbol
2362 table. */
2363
2364 static struct compunit_symtab *
2365 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2366 {
2367 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2368
2369 gdb_assert (dwarf2_per_objfile->using_index);
2370 if (!per_cu->v.quick->compunit_symtab)
2371 {
2372 free_cached_comp_units freer (dwarf2_per_objfile);
2373 scoped_restore decrementer = increment_reading_symtab ();
2374 dw2_do_instantiate_symtab (per_cu, skip_partial);
2375 process_cu_includes (dwarf2_per_objfile);
2376 }
2377
2378 return per_cu->v.quick->compunit_symtab;
2379 }
2380
2381 /* See declaration. */
2382
2383 dwarf2_per_cu_data *
2384 dwarf2_per_objfile::get_cutu (int index)
2385 {
2386 if (index >= this->all_comp_units.size ())
2387 {
2388 index -= this->all_comp_units.size ();
2389 gdb_assert (index < this->all_type_units.size ());
2390 return &this->all_type_units[index]->per_cu;
2391 }
2392
2393 return this->all_comp_units[index];
2394 }
2395
2396 /* See declaration. */
2397
2398 dwarf2_per_cu_data *
2399 dwarf2_per_objfile::get_cu (int index)
2400 {
2401 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2402
2403 return this->all_comp_units[index];
2404 }
2405
2406 /* See declaration. */
2407
2408 signatured_type *
2409 dwarf2_per_objfile::get_tu (int index)
2410 {
2411 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2412
2413 return this->all_type_units[index];
2414 }
2415
2416 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2417 objfile_obstack, and constructed with the specified field
2418 values. */
2419
2420 static dwarf2_per_cu_data *
2421 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2422 struct dwarf2_section_info *section,
2423 int is_dwz,
2424 sect_offset sect_off, ULONGEST length)
2425 {
2426 struct objfile *objfile = dwarf2_per_objfile->objfile;
2427 dwarf2_per_cu_data *the_cu
2428 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2429 struct dwarf2_per_cu_data);
2430 the_cu->sect_off = sect_off;
2431 the_cu->length = length;
2432 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2433 the_cu->section = section;
2434 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2435 struct dwarf2_per_cu_quick_data);
2436 the_cu->is_dwz = is_dwz;
2437 return the_cu;
2438 }
2439
2440 /* A helper for create_cus_from_index that handles a given list of
2441 CUs. */
2442
2443 static void
2444 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2445 const gdb_byte *cu_list, offset_type n_elements,
2446 struct dwarf2_section_info *section,
2447 int is_dwz)
2448 {
2449 for (offset_type i = 0; i < n_elements; i += 2)
2450 {
2451 gdb_static_assert (sizeof (ULONGEST) >= 8);
2452
2453 sect_offset sect_off
2454 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2455 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2456 cu_list += 2 * 8;
2457
2458 dwarf2_per_cu_data *per_cu
2459 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2460 sect_off, length);
2461 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2462 }
2463 }
2464
2465 /* Read the CU list from the mapped index, and use it to create all
2466 the CU objects for this objfile. */
2467
2468 static void
2469 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2470 const gdb_byte *cu_list, offset_type cu_list_elements,
2471 const gdb_byte *dwz_list, offset_type dwz_elements)
2472 {
2473 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2474 dwarf2_per_objfile->all_comp_units.reserve
2475 ((cu_list_elements + dwz_elements) / 2);
2476
2477 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2478 &dwarf2_per_objfile->info, 0);
2479
2480 if (dwz_elements == 0)
2481 return;
2482
2483 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2484 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2485 &dwz->info, 1);
2486 }
2487
2488 /* Create the signatured type hash table from the index. */
2489
2490 static void
2491 create_signatured_type_table_from_index
2492 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2493 struct dwarf2_section_info *section,
2494 const gdb_byte *bytes,
2495 offset_type elements)
2496 {
2497 struct objfile *objfile = dwarf2_per_objfile->objfile;
2498
2499 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2500 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2501
2502 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2503
2504 for (offset_type i = 0; i < elements; i += 3)
2505 {
2506 struct signatured_type *sig_type;
2507 ULONGEST signature;
2508 void **slot;
2509 cu_offset type_offset_in_tu;
2510
2511 gdb_static_assert (sizeof (ULONGEST) >= 8);
2512 sect_offset sect_off
2513 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2514 type_offset_in_tu
2515 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2516 BFD_ENDIAN_LITTLE);
2517 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2518 bytes += 3 * 8;
2519
2520 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521 struct signatured_type);
2522 sig_type->signature = signature;
2523 sig_type->type_offset_in_tu = type_offset_in_tu;
2524 sig_type->per_cu.is_debug_types = 1;
2525 sig_type->per_cu.section = section;
2526 sig_type->per_cu.sect_off = sect_off;
2527 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2528 sig_type->per_cu.v.quick
2529 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2530 struct dwarf2_per_cu_quick_data);
2531
2532 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2533 *slot = sig_type;
2534
2535 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2536 }
2537
2538 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2539 }
2540
2541 /* Create the signatured type hash table from .debug_names. */
2542
2543 static void
2544 create_signatured_type_table_from_debug_names
2545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2546 const mapped_debug_names &map,
2547 struct dwarf2_section_info *section,
2548 struct dwarf2_section_info *abbrev_section)
2549 {
2550 struct objfile *objfile = dwarf2_per_objfile->objfile;
2551
2552 section->read (objfile);
2553 abbrev_section->read (objfile);
2554
2555 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2556 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2557
2558 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2559
2560 for (uint32_t i = 0; i < map.tu_count; ++i)
2561 {
2562 struct signatured_type *sig_type;
2563 void **slot;
2564
2565 sect_offset sect_off
2566 = (sect_offset) (extract_unsigned_integer
2567 (map.tu_table_reordered + i * map.offset_size,
2568 map.offset_size,
2569 map.dwarf5_byte_order));
2570
2571 comp_unit_head cu_header;
2572 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2573 abbrev_section,
2574 section->buffer + to_underlying (sect_off),
2575 rcuh_kind::TYPE);
2576
2577 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2578 struct signatured_type);
2579 sig_type->signature = cu_header.signature;
2580 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2581 sig_type->per_cu.is_debug_types = 1;
2582 sig_type->per_cu.section = section;
2583 sig_type->per_cu.sect_off = sect_off;
2584 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2585 sig_type->per_cu.v.quick
2586 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2587 struct dwarf2_per_cu_quick_data);
2588
2589 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2590 *slot = sig_type;
2591
2592 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2593 }
2594
2595 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2596 }
2597
2598 /* Read the address map data from the mapped index, and use it to
2599 populate the objfile's psymtabs_addrmap. */
2600
2601 static void
2602 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2603 struct mapped_index *index)
2604 {
2605 struct objfile *objfile = dwarf2_per_objfile->objfile;
2606 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2607 const gdb_byte *iter, *end;
2608 struct addrmap *mutable_map;
2609 CORE_ADDR baseaddr;
2610
2611 auto_obstack temp_obstack;
2612
2613 mutable_map = addrmap_create_mutable (&temp_obstack);
2614
2615 iter = index->address_table.data ();
2616 end = iter + index->address_table.size ();
2617
2618 baseaddr = objfile->text_section_offset ();
2619
2620 while (iter < end)
2621 {
2622 ULONGEST hi, lo, cu_index;
2623 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2624 iter += 8;
2625 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2626 iter += 8;
2627 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2628 iter += 4;
2629
2630 if (lo > hi)
2631 {
2632 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2633 hex_string (lo), hex_string (hi));
2634 continue;
2635 }
2636
2637 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2638 {
2639 complaint (_(".gdb_index address table has invalid CU number %u"),
2640 (unsigned) cu_index);
2641 continue;
2642 }
2643
2644 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2645 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2646 addrmap_set_empty (mutable_map, lo, hi - 1,
2647 dwarf2_per_objfile->get_cu (cu_index));
2648 }
2649
2650 objfile->partial_symtabs->psymtabs_addrmap
2651 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2652 }
2653
2654 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2655 populate the objfile's psymtabs_addrmap. */
2656
2657 static void
2658 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2659 struct dwarf2_section_info *section)
2660 {
2661 struct objfile *objfile = dwarf2_per_objfile->objfile;
2662 bfd *abfd = objfile->obfd;
2663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2664 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2665
2666 auto_obstack temp_obstack;
2667 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2668
2669 std::unordered_map<sect_offset,
2670 dwarf2_per_cu_data *,
2671 gdb::hash_enum<sect_offset>>
2672 debug_info_offset_to_per_cu;
2673 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2674 {
2675 const auto insertpair
2676 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2677 if (!insertpair.second)
2678 {
2679 warning (_("Section .debug_aranges in %s has duplicate "
2680 "debug_info_offset %s, ignoring .debug_aranges."),
2681 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2682 return;
2683 }
2684 }
2685
2686 section->read (objfile);
2687
2688 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2689
2690 const gdb_byte *addr = section->buffer;
2691
2692 while (addr < section->buffer + section->size)
2693 {
2694 const gdb_byte *const entry_addr = addr;
2695 unsigned int bytes_read;
2696
2697 const LONGEST entry_length = read_initial_length (abfd, addr,
2698 &bytes_read);
2699 addr += bytes_read;
2700
2701 const gdb_byte *const entry_end = addr + entry_length;
2702 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2703 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2704 if (addr + entry_length > section->buffer + section->size)
2705 {
2706 warning (_("Section .debug_aranges in %s entry at offset %s "
2707 "length %s exceeds section length %s, "
2708 "ignoring .debug_aranges."),
2709 objfile_name (objfile),
2710 plongest (entry_addr - section->buffer),
2711 plongest (bytes_read + entry_length),
2712 pulongest (section->size));
2713 return;
2714 }
2715
2716 /* The version number. */
2717 const uint16_t version = read_2_bytes (abfd, addr);
2718 addr += 2;
2719 if (version != 2)
2720 {
2721 warning (_("Section .debug_aranges in %s entry at offset %s "
2722 "has unsupported version %d, ignoring .debug_aranges."),
2723 objfile_name (objfile),
2724 plongest (entry_addr - section->buffer), version);
2725 return;
2726 }
2727
2728 const uint64_t debug_info_offset
2729 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2730 addr += offset_size;
2731 const auto per_cu_it
2732 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2733 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2734 {
2735 warning (_("Section .debug_aranges in %s entry at offset %s "
2736 "debug_info_offset %s does not exists, "
2737 "ignoring .debug_aranges."),
2738 objfile_name (objfile),
2739 plongest (entry_addr - section->buffer),
2740 pulongest (debug_info_offset));
2741 return;
2742 }
2743 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2744
2745 const uint8_t address_size = *addr++;
2746 if (address_size < 1 || address_size > 8)
2747 {
2748 warning (_("Section .debug_aranges in %s entry at offset %s "
2749 "address_size %u is invalid, ignoring .debug_aranges."),
2750 objfile_name (objfile),
2751 plongest (entry_addr - section->buffer), address_size);
2752 return;
2753 }
2754
2755 const uint8_t segment_selector_size = *addr++;
2756 if (segment_selector_size != 0)
2757 {
2758 warning (_("Section .debug_aranges in %s entry at offset %s "
2759 "segment_selector_size %u is not supported, "
2760 "ignoring .debug_aranges."),
2761 objfile_name (objfile),
2762 plongest (entry_addr - section->buffer),
2763 segment_selector_size);
2764 return;
2765 }
2766
2767 /* Must pad to an alignment boundary that is twice the address
2768 size. It is undocumented by the DWARF standard but GCC does
2769 use it. */
2770 for (size_t padding = ((-(addr - section->buffer))
2771 & (2 * address_size - 1));
2772 padding > 0; padding--)
2773 if (*addr++ != 0)
2774 {
2775 warning (_("Section .debug_aranges in %s entry at offset %s "
2776 "padding is not zero, ignoring .debug_aranges."),
2777 objfile_name (objfile),
2778 plongest (entry_addr - section->buffer));
2779 return;
2780 }
2781
2782 for (;;)
2783 {
2784 if (addr + 2 * address_size > entry_end)
2785 {
2786 warning (_("Section .debug_aranges in %s entry at offset %s "
2787 "address list is not properly terminated, "
2788 "ignoring .debug_aranges."),
2789 objfile_name (objfile),
2790 plongest (entry_addr - section->buffer));
2791 return;
2792 }
2793 ULONGEST start = extract_unsigned_integer (addr, address_size,
2794 dwarf5_byte_order);
2795 addr += address_size;
2796 ULONGEST length = extract_unsigned_integer (addr, address_size,
2797 dwarf5_byte_order);
2798 addr += address_size;
2799 if (start == 0 && length == 0)
2800 break;
2801 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2802 {
2803 /* Symbol was eliminated due to a COMDAT group. */
2804 continue;
2805 }
2806 ULONGEST end = start + length;
2807 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2808 - baseaddr);
2809 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2810 - baseaddr);
2811 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2812 }
2813 }
2814
2815 objfile->partial_symtabs->psymtabs_addrmap
2816 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2817 }
2818
2819 /* Find a slot in the mapped index INDEX for the object named NAME.
2820 If NAME is found, set *VEC_OUT to point to the CU vector in the
2821 constant pool and return true. If NAME cannot be found, return
2822 false. */
2823
2824 static bool
2825 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2826 offset_type **vec_out)
2827 {
2828 offset_type hash;
2829 offset_type slot, step;
2830 int (*cmp) (const char *, const char *);
2831
2832 gdb::unique_xmalloc_ptr<char> without_params;
2833 if (current_language->la_language == language_cplus
2834 || current_language->la_language == language_fortran
2835 || current_language->la_language == language_d)
2836 {
2837 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2838 not contain any. */
2839
2840 if (strchr (name, '(') != NULL)
2841 {
2842 without_params = cp_remove_params (name);
2843
2844 if (without_params != NULL)
2845 name = without_params.get ();
2846 }
2847 }
2848
2849 /* Index version 4 did not support case insensitive searches. But the
2850 indices for case insensitive languages are built in lowercase, therefore
2851 simulate our NAME being searched is also lowercased. */
2852 hash = mapped_index_string_hash ((index->version == 4
2853 && case_sensitivity == case_sensitive_off
2854 ? 5 : index->version),
2855 name);
2856
2857 slot = hash & (index->symbol_table.size () - 1);
2858 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2859 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2860
2861 for (;;)
2862 {
2863 const char *str;
2864
2865 const auto &bucket = index->symbol_table[slot];
2866 if (bucket.name == 0 && bucket.vec == 0)
2867 return false;
2868
2869 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2870 if (!cmp (name, str))
2871 {
2872 *vec_out = (offset_type *) (index->constant_pool
2873 + MAYBE_SWAP (bucket.vec));
2874 return true;
2875 }
2876
2877 slot = (slot + step) & (index->symbol_table.size () - 1);
2878 }
2879 }
2880
2881 /* A helper function that reads the .gdb_index from BUFFER and fills
2882 in MAP. FILENAME is the name of the file containing the data;
2883 it is used for error reporting. DEPRECATED_OK is true if it is
2884 ok to use deprecated sections.
2885
2886 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2887 out parameters that are filled in with information about the CU and
2888 TU lists in the section.
2889
2890 Returns true if all went well, false otherwise. */
2891
2892 static bool
2893 read_gdb_index_from_buffer (struct objfile *objfile,
2894 const char *filename,
2895 bool deprecated_ok,
2896 gdb::array_view<const gdb_byte> buffer,
2897 struct mapped_index *map,
2898 const gdb_byte **cu_list,
2899 offset_type *cu_list_elements,
2900 const gdb_byte **types_list,
2901 offset_type *types_list_elements)
2902 {
2903 const gdb_byte *addr = &buffer[0];
2904
2905 /* Version check. */
2906 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2907 /* Versions earlier than 3 emitted every copy of a psymbol. This
2908 causes the index to behave very poorly for certain requests. Version 3
2909 contained incomplete addrmap. So, it seems better to just ignore such
2910 indices. */
2911 if (version < 4)
2912 {
2913 static int warning_printed = 0;
2914 if (!warning_printed)
2915 {
2916 warning (_("Skipping obsolete .gdb_index section in %s."),
2917 filename);
2918 warning_printed = 1;
2919 }
2920 return 0;
2921 }
2922 /* Index version 4 uses a different hash function than index version
2923 5 and later.
2924
2925 Versions earlier than 6 did not emit psymbols for inlined
2926 functions. Using these files will cause GDB not to be able to
2927 set breakpoints on inlined functions by name, so we ignore these
2928 indices unless the user has done
2929 "set use-deprecated-index-sections on". */
2930 if (version < 6 && !deprecated_ok)
2931 {
2932 static int warning_printed = 0;
2933 if (!warning_printed)
2934 {
2935 warning (_("\
2936 Skipping deprecated .gdb_index section in %s.\n\
2937 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2938 to use the section anyway."),
2939 filename);
2940 warning_printed = 1;
2941 }
2942 return 0;
2943 }
2944 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2945 of the TU (for symbols coming from TUs),
2946 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2947 Plus gold-generated indices can have duplicate entries for global symbols,
2948 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2949 These are just performance bugs, and we can't distinguish gdb-generated
2950 indices from gold-generated ones, so issue no warning here. */
2951
2952 /* Indexes with higher version than the one supported by GDB may be no
2953 longer backward compatible. */
2954 if (version > 8)
2955 return 0;
2956
2957 map->version = version;
2958
2959 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2960
2961 int i = 0;
2962 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2963 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2964 / 8);
2965 ++i;
2966
2967 *types_list = addr + MAYBE_SWAP (metadata[i]);
2968 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2969 - MAYBE_SWAP (metadata[i]))
2970 / 8);
2971 ++i;
2972
2973 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->address_table
2976 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2977 ++i;
2978
2979 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2980 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2981 map->symbol_table
2982 = gdb::array_view<mapped_index::symbol_table_slot>
2983 ((mapped_index::symbol_table_slot *) symbol_table,
2984 (mapped_index::symbol_table_slot *) symbol_table_end);
2985
2986 ++i;
2987 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2988
2989 return 1;
2990 }
2991
2992 /* Callback types for dwarf2_read_gdb_index. */
2993
2994 typedef gdb::function_view
2995 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2996 get_gdb_index_contents_ftype;
2997 typedef gdb::function_view
2998 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2999 get_gdb_index_contents_dwz_ftype;
3000
3001 /* Read .gdb_index. If everything went ok, initialize the "quick"
3002 elements of all the CUs and return 1. Otherwise, return 0. */
3003
3004 static int
3005 dwarf2_read_gdb_index
3006 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3007 get_gdb_index_contents_ftype get_gdb_index_contents,
3008 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3009 {
3010 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3011 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3012 struct dwz_file *dwz;
3013 struct objfile *objfile = dwarf2_per_objfile->objfile;
3014
3015 gdb::array_view<const gdb_byte> main_index_contents
3016 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3017
3018 if (main_index_contents.empty ())
3019 return 0;
3020
3021 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3022 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3023 use_deprecated_index_sections,
3024 main_index_contents, map.get (), &cu_list,
3025 &cu_list_elements, &types_list,
3026 &types_list_elements))
3027 return 0;
3028
3029 /* Don't use the index if it's empty. */
3030 if (map->symbol_table.empty ())
3031 return 0;
3032
3033 /* If there is a .dwz file, read it so we can get its CU list as
3034 well. */
3035 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3036 if (dwz != NULL)
3037 {
3038 struct mapped_index dwz_map;
3039 const gdb_byte *dwz_types_ignore;
3040 offset_type dwz_types_elements_ignore;
3041
3042 gdb::array_view<const gdb_byte> dwz_index_content
3043 = get_gdb_index_contents_dwz (objfile, dwz);
3044
3045 if (dwz_index_content.empty ())
3046 return 0;
3047
3048 if (!read_gdb_index_from_buffer (objfile,
3049 bfd_get_filename (dwz->dwz_bfd.get ()),
3050 1, dwz_index_content, &dwz_map,
3051 &dwz_list, &dwz_list_elements,
3052 &dwz_types_ignore,
3053 &dwz_types_elements_ignore))
3054 {
3055 warning (_("could not read '.gdb_index' section from %s; skipping"),
3056 bfd_get_filename (dwz->dwz_bfd.get ()));
3057 return 0;
3058 }
3059 }
3060
3061 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3062 dwz_list, dwz_list_elements);
3063
3064 if (types_list_elements)
3065 {
3066 /* We can only handle a single .debug_types when we have an
3067 index. */
3068 if (dwarf2_per_objfile->types.size () != 1)
3069 return 0;
3070
3071 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3072
3073 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3074 types_list, types_list_elements);
3075 }
3076
3077 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3078
3079 dwarf2_per_objfile->index_table = std::move (map);
3080 dwarf2_per_objfile->using_index = 1;
3081 dwarf2_per_objfile->quick_file_names_table =
3082 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3083
3084 return 1;
3085 }
3086
3087 /* die_reader_func for dw2_get_file_names. */
3088
3089 static void
3090 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3091 const gdb_byte *info_ptr,
3092 struct die_info *comp_unit_die)
3093 {
3094 struct dwarf2_cu *cu = reader->cu;
3095 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3096 struct dwarf2_per_objfile *dwarf2_per_objfile
3097 = cu->per_cu->dwarf2_per_objfile;
3098 struct objfile *objfile = dwarf2_per_objfile->objfile;
3099 struct dwarf2_per_cu_data *lh_cu;
3100 struct attribute *attr;
3101 void **slot;
3102 struct quick_file_names *qfn;
3103
3104 gdb_assert (! this_cu->is_debug_types);
3105
3106 /* Our callers never want to match partial units -- instead they
3107 will match the enclosing full CU. */
3108 if (comp_unit_die->tag == DW_TAG_partial_unit)
3109 {
3110 this_cu->v.quick->no_file_data = 1;
3111 return;
3112 }
3113
3114 lh_cu = this_cu;
3115 slot = NULL;
3116
3117 line_header_up lh;
3118 sect_offset line_offset {};
3119
3120 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3121 if (attr != nullptr)
3122 {
3123 struct quick_file_names find_entry;
3124
3125 line_offset = (sect_offset) DW_UNSND (attr);
3126
3127 /* We may have already read in this line header (TU line header sharing).
3128 If we have we're done. */
3129 find_entry.hash.dwo_unit = cu->dwo_unit;
3130 find_entry.hash.line_sect_off = line_offset;
3131 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3132 &find_entry, INSERT);
3133 if (*slot != NULL)
3134 {
3135 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3136 return;
3137 }
3138
3139 lh = dwarf_decode_line_header (line_offset, cu);
3140 }
3141 if (lh == NULL)
3142 {
3143 lh_cu->v.quick->no_file_data = 1;
3144 return;
3145 }
3146
3147 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3148 qfn->hash.dwo_unit = cu->dwo_unit;
3149 qfn->hash.line_sect_off = line_offset;
3150 gdb_assert (slot != NULL);
3151 *slot = qfn;
3152
3153 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3154
3155 int offset = 0;
3156 if (strcmp (fnd.name, "<unknown>") != 0)
3157 ++offset;
3158
3159 qfn->num_file_names = offset + lh->file_names_size ();
3160 qfn->file_names =
3161 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3162 if (offset != 0)
3163 qfn->file_names[0] = xstrdup (fnd.name);
3164 for (int i = 0; i < lh->file_names_size (); ++i)
3165 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3166 fnd.comp_dir).release ();
3167 qfn->real_names = NULL;
3168
3169 lh_cu->v.quick->file_names = qfn;
3170 }
3171
3172 /* A helper for the "quick" functions which attempts to read the line
3173 table for THIS_CU. */
3174
3175 static struct quick_file_names *
3176 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3177 {
3178 /* This should never be called for TUs. */
3179 gdb_assert (! this_cu->is_debug_types);
3180 /* Nor type unit groups. */
3181 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3182
3183 if (this_cu->v.quick->file_names != NULL)
3184 return this_cu->v.quick->file_names;
3185 /* If we know there is no line data, no point in looking again. */
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188
3189 cutu_reader reader (this_cu);
3190 if (!reader.dummy_p)
3191 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3192
3193 if (this_cu->v.quick->no_file_data)
3194 return NULL;
3195 return this_cu->v.quick->file_names;
3196 }
3197
3198 /* A helper for the "quick" functions which computes and caches the
3199 real path for a given file name from the line table. */
3200
3201 static const char *
3202 dw2_get_real_path (struct objfile *objfile,
3203 struct quick_file_names *qfn, int index)
3204 {
3205 if (qfn->real_names == NULL)
3206 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3207 qfn->num_file_names, const char *);
3208
3209 if (qfn->real_names[index] == NULL)
3210 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3211
3212 return qfn->real_names[index];
3213 }
3214
3215 static struct symtab *
3216 dw2_find_last_source_symtab (struct objfile *objfile)
3217 {
3218 struct dwarf2_per_objfile *dwarf2_per_objfile
3219 = get_dwarf2_per_objfile (objfile);
3220 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3221 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3222
3223 if (cust == NULL)
3224 return NULL;
3225
3226 return compunit_primary_filetab (cust);
3227 }
3228
3229 /* Traversal function for dw2_forget_cached_source_info. */
3230
3231 static int
3232 dw2_free_cached_file_names (void **slot, void *info)
3233 {
3234 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3235
3236 if (file_data->real_names)
3237 {
3238 int i;
3239
3240 for (i = 0; i < file_data->num_file_names; ++i)
3241 {
3242 xfree ((void*) file_data->real_names[i]);
3243 file_data->real_names[i] = NULL;
3244 }
3245 }
3246
3247 return 1;
3248 }
3249
3250 static void
3251 dw2_forget_cached_source_info (struct objfile *objfile)
3252 {
3253 struct dwarf2_per_objfile *dwarf2_per_objfile
3254 = get_dwarf2_per_objfile (objfile);
3255
3256 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3257 dw2_free_cached_file_names, NULL);
3258 }
3259
3260 /* Helper function for dw2_map_symtabs_matching_filename that expands
3261 the symtabs and calls the iterator. */
3262
3263 static int
3264 dw2_map_expand_apply (struct objfile *objfile,
3265 struct dwarf2_per_cu_data *per_cu,
3266 const char *name, const char *real_path,
3267 gdb::function_view<bool (symtab *)> callback)
3268 {
3269 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3270
3271 /* Don't visit already-expanded CUs. */
3272 if (per_cu->v.quick->compunit_symtab)
3273 return 0;
3274
3275 /* This may expand more than one symtab, and we want to iterate over
3276 all of them. */
3277 dw2_instantiate_symtab (per_cu, false);
3278
3279 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3280 last_made, callback);
3281 }
3282
3283 /* Implementation of the map_symtabs_matching_filename method. */
3284
3285 static bool
3286 dw2_map_symtabs_matching_filename
3287 (struct objfile *objfile, const char *name, const char *real_path,
3288 gdb::function_view<bool (symtab *)> callback)
3289 {
3290 const char *name_basename = lbasename (name);
3291 struct dwarf2_per_objfile *dwarf2_per_objfile
3292 = get_dwarf2_per_objfile (objfile);
3293
3294 /* The rule is CUs specify all the files, including those used by
3295 any TU, so there's no need to scan TUs here. */
3296
3297 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3298 {
3299 /* We only need to look at symtabs not already expanded. */
3300 if (per_cu->v.quick->compunit_symtab)
3301 continue;
3302
3303 quick_file_names *file_data = dw2_get_file_names (per_cu);
3304 if (file_data == NULL)
3305 continue;
3306
3307 for (int j = 0; j < file_data->num_file_names; ++j)
3308 {
3309 const char *this_name = file_data->file_names[j];
3310 const char *this_real_name;
3311
3312 if (compare_filenames_for_search (this_name, name))
3313 {
3314 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3315 callback))
3316 return true;
3317 continue;
3318 }
3319
3320 /* Before we invoke realpath, which can get expensive when many
3321 files are involved, do a quick comparison of the basenames. */
3322 if (! basenames_may_differ
3323 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3324 continue;
3325
3326 this_real_name = dw2_get_real_path (objfile, file_data, j);
3327 if (compare_filenames_for_search (this_real_name, name))
3328 {
3329 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3330 callback))
3331 return true;
3332 continue;
3333 }
3334
3335 if (real_path != NULL)
3336 {
3337 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3338 gdb_assert (IS_ABSOLUTE_PATH (name));
3339 if (this_real_name != NULL
3340 && FILENAME_CMP (real_path, this_real_name) == 0)
3341 {
3342 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3343 callback))
3344 return true;
3345 continue;
3346 }
3347 }
3348 }
3349 }
3350
3351 return false;
3352 }
3353
3354 /* Struct used to manage iterating over all CUs looking for a symbol. */
3355
3356 struct dw2_symtab_iterator
3357 {
3358 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3359 struct dwarf2_per_objfile *dwarf2_per_objfile;
3360 /* If set, only look for symbols that match that block. Valid values are
3361 GLOBAL_BLOCK and STATIC_BLOCK. */
3362 gdb::optional<block_enum> block_index;
3363 /* The kind of symbol we're looking for. */
3364 domain_enum domain;
3365 /* The list of CUs from the index entry of the symbol,
3366 or NULL if not found. */
3367 offset_type *vec;
3368 /* The next element in VEC to look at. */
3369 int next;
3370 /* The number of elements in VEC, or zero if there is no match. */
3371 int length;
3372 /* Have we seen a global version of the symbol?
3373 If so we can ignore all further global instances.
3374 This is to work around gold/15646, inefficient gold-generated
3375 indices. */
3376 int global_seen;
3377 };
3378
3379 /* Initialize the index symtab iterator ITER. */
3380
3381 static void
3382 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3383 struct dwarf2_per_objfile *dwarf2_per_objfile,
3384 gdb::optional<block_enum> block_index,
3385 domain_enum domain,
3386 const char *name)
3387 {
3388 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3389 iter->block_index = block_index;
3390 iter->domain = domain;
3391 iter->next = 0;
3392 iter->global_seen = 0;
3393
3394 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3395
3396 /* index is NULL if OBJF_READNOW. */
3397 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3398 iter->length = MAYBE_SWAP (*iter->vec);
3399 else
3400 {
3401 iter->vec = NULL;
3402 iter->length = 0;
3403 }
3404 }
3405
3406 /* Return the next matching CU or NULL if there are no more. */
3407
3408 static struct dwarf2_per_cu_data *
3409 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3410 {
3411 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3412
3413 for ( ; iter->next < iter->length; ++iter->next)
3414 {
3415 offset_type cu_index_and_attrs =
3416 MAYBE_SWAP (iter->vec[iter->next + 1]);
3417 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3418 gdb_index_symbol_kind symbol_kind =
3419 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3420 /* Only check the symbol attributes if they're present.
3421 Indices prior to version 7 don't record them,
3422 and indices >= 7 may elide them for certain symbols
3423 (gold does this). */
3424 int attrs_valid =
3425 (dwarf2_per_objfile->index_table->version >= 7
3426 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3427
3428 /* Don't crash on bad data. */
3429 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3430 + dwarf2_per_objfile->all_type_units.size ()))
3431 {
3432 complaint (_(".gdb_index entry has bad CU index"
3433 " [in module %s]"),
3434 objfile_name (dwarf2_per_objfile->objfile));
3435 continue;
3436 }
3437
3438 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3439
3440 /* Skip if already read in. */
3441 if (per_cu->v.quick->compunit_symtab)
3442 continue;
3443
3444 /* Check static vs global. */
3445 if (attrs_valid)
3446 {
3447 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3448
3449 if (iter->block_index.has_value ())
3450 {
3451 bool want_static = *iter->block_index == STATIC_BLOCK;
3452
3453 if (is_static != want_static)
3454 continue;
3455 }
3456
3457 /* Work around gold/15646. */
3458 if (!is_static && iter->global_seen)
3459 continue;
3460 if (!is_static)
3461 iter->global_seen = 1;
3462 }
3463
3464 /* Only check the symbol's kind if it has one. */
3465 if (attrs_valid)
3466 {
3467 switch (iter->domain)
3468 {
3469 case VAR_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3471 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3472 /* Some types are also in VAR_DOMAIN. */
3473 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3474 continue;
3475 break;
3476 case STRUCT_DOMAIN:
3477 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3478 continue;
3479 break;
3480 case LABEL_DOMAIN:
3481 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3482 continue;
3483 break;
3484 case MODULE_DOMAIN:
3485 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3486 continue;
3487 break;
3488 default:
3489 break;
3490 }
3491 }
3492
3493 ++iter->next;
3494 return per_cu;
3495 }
3496
3497 return NULL;
3498 }
3499
3500 static struct compunit_symtab *
3501 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3502 const char *name, domain_enum domain)
3503 {
3504 struct compunit_symtab *stab_best = NULL;
3505 struct dwarf2_per_objfile *dwarf2_per_objfile
3506 = get_dwarf2_per_objfile (objfile);
3507
3508 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3509
3510 struct dw2_symtab_iterator iter;
3511 struct dwarf2_per_cu_data *per_cu;
3512
3513 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3514
3515 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3516 {
3517 struct symbol *sym, *with_opaque = NULL;
3518 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3519 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3520 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3521
3522 sym = block_find_symbol (block, name, domain,
3523 block_find_non_opaque_type_preferred,
3524 &with_opaque);
3525
3526 /* Some caution must be observed with overloaded functions
3527 and methods, since the index will not contain any overload
3528 information (but NAME might contain it). */
3529
3530 if (sym != NULL
3531 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3532 return stab;
3533 if (with_opaque != NULL
3534 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3535 stab_best = stab;
3536
3537 /* Keep looking through other CUs. */
3538 }
3539
3540 return stab_best;
3541 }
3542
3543 static void
3544 dw2_print_stats (struct objfile *objfile)
3545 {
3546 struct dwarf2_per_objfile *dwarf2_per_objfile
3547 = get_dwarf2_per_objfile (objfile);
3548 int total = (dwarf2_per_objfile->all_comp_units.size ()
3549 + dwarf2_per_objfile->all_type_units.size ());
3550 int count = 0;
3551
3552 for (int i = 0; i < total; ++i)
3553 {
3554 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3555
3556 if (!per_cu->v.quick->compunit_symtab)
3557 ++count;
3558 }
3559 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3560 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3561 }
3562
3563 /* This dumps minimal information about the index.
3564 It is called via "mt print objfiles".
3565 One use is to verify .gdb_index has been loaded by the
3566 gdb.dwarf2/gdb-index.exp testcase. */
3567
3568 static void
3569 dw2_dump (struct objfile *objfile)
3570 {
3571 struct dwarf2_per_objfile *dwarf2_per_objfile
3572 = get_dwarf2_per_objfile (objfile);
3573
3574 gdb_assert (dwarf2_per_objfile->using_index);
3575 printf_filtered (".gdb_index:");
3576 if (dwarf2_per_objfile->index_table != NULL)
3577 {
3578 printf_filtered (" version %d\n",
3579 dwarf2_per_objfile->index_table->version);
3580 }
3581 else
3582 printf_filtered (" faked for \"readnow\"\n");
3583 printf_filtered ("\n");
3584 }
3585
3586 static void
3587 dw2_expand_symtabs_for_function (struct objfile *objfile,
3588 const char *func_name)
3589 {
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
3592
3593 struct dw2_symtab_iterator iter;
3594 struct dwarf2_per_cu_data *per_cu;
3595
3596 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3597
3598 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3599 dw2_instantiate_symtab (per_cu, false);
3600
3601 }
3602
3603 static void
3604 dw2_expand_all_symtabs (struct objfile *objfile)
3605 {
3606 struct dwarf2_per_objfile *dwarf2_per_objfile
3607 = get_dwarf2_per_objfile (objfile);
3608 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3609 + dwarf2_per_objfile->all_type_units.size ());
3610
3611 for (int i = 0; i < total_units; ++i)
3612 {
3613 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3614
3615 /* We don't want to directly expand a partial CU, because if we
3616 read it with the wrong language, then assertion failures can
3617 be triggered later on. See PR symtab/23010. So, tell
3618 dw2_instantiate_symtab to skip partial CUs -- any important
3619 partial CU will be read via DW_TAG_imported_unit anyway. */
3620 dw2_instantiate_symtab (per_cu, true);
3621 }
3622 }
3623
3624 static void
3625 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3626 const char *fullname)
3627 {
3628 struct dwarf2_per_objfile *dwarf2_per_objfile
3629 = get_dwarf2_per_objfile (objfile);
3630
3631 /* We don't need to consider type units here.
3632 This is only called for examining code, e.g. expand_line_sal.
3633 There can be an order of magnitude (or more) more type units
3634 than comp units, and we avoid them if we can. */
3635
3636 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3637 {
3638 /* We only need to look at symtabs not already expanded. */
3639 if (per_cu->v.quick->compunit_symtab)
3640 continue;
3641
3642 quick_file_names *file_data = dw2_get_file_names (per_cu);
3643 if (file_data == NULL)
3644 continue;
3645
3646 for (int j = 0; j < file_data->num_file_names; ++j)
3647 {
3648 const char *this_fullname = file_data->file_names[j];
3649
3650 if (filename_cmp (this_fullname, fullname) == 0)
3651 {
3652 dw2_instantiate_symtab (per_cu, false);
3653 break;
3654 }
3655 }
3656 }
3657 }
3658
3659 static void
3660 dw2_map_matching_symbols
3661 (struct objfile *objfile,
3662 const lookup_name_info &name, domain_enum domain,
3663 int global,
3664 gdb::function_view<symbol_found_callback_ftype> callback,
3665 symbol_compare_ftype *ordered_compare)
3666 {
3667 /* Currently unimplemented; used for Ada. The function can be called if the
3668 current language is Ada for a non-Ada objfile using GNU index. As Ada
3669 does not look for non-Ada symbols this function should just return. */
3670 }
3671
3672 /* Starting from a search name, return the string that finds the upper
3673 bound of all strings that start with SEARCH_NAME in a sorted name
3674 list. Returns the empty string to indicate that the upper bound is
3675 the end of the list. */
3676
3677 static std::string
3678 make_sort_after_prefix_name (const char *search_name)
3679 {
3680 /* When looking to complete "func", we find the upper bound of all
3681 symbols that start with "func" by looking for where we'd insert
3682 the closest string that would follow "func" in lexicographical
3683 order. Usually, that's "func"-with-last-character-incremented,
3684 i.e. "fund". Mind non-ASCII characters, though. Usually those
3685 will be UTF-8 multi-byte sequences, but we can't be certain.
3686 Especially mind the 0xff character, which is a valid character in
3687 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3688 rule out compilers allowing it in identifiers. Note that
3689 conveniently, strcmp/strcasecmp are specified to compare
3690 characters interpreted as unsigned char. So what we do is treat
3691 the whole string as a base 256 number composed of a sequence of
3692 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3693 to 0, and carries 1 to the following more-significant position.
3694 If the very first character in SEARCH_NAME ends up incremented
3695 and carries/overflows, then the upper bound is the end of the
3696 list. The string after the empty string is also the empty
3697 string.
3698
3699 Some examples of this operation:
3700
3701 SEARCH_NAME => "+1" RESULT
3702
3703 "abc" => "abd"
3704 "ab\xff" => "ac"
3705 "\xff" "a" "\xff" => "\xff" "b"
3706 "\xff" => ""
3707 "\xff\xff" => ""
3708 "" => ""
3709
3710 Then, with these symbols for example:
3711
3712 func
3713 func1
3714 fund
3715
3716 completing "func" looks for symbols between "func" and
3717 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3718 which finds "func" and "func1", but not "fund".
3719
3720 And with:
3721
3722 funcÿ (Latin1 'ÿ' [0xff])
3723 funcÿ1
3724 fund
3725
3726 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3727 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3728
3729 And with:
3730
3731 ÿÿ (Latin1 'ÿ' [0xff])
3732 ÿÿ1
3733
3734 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3735 the end of the list.
3736 */
3737 std::string after = search_name;
3738 while (!after.empty () && (unsigned char) after.back () == 0xff)
3739 after.pop_back ();
3740 if (!after.empty ())
3741 after.back () = (unsigned char) after.back () + 1;
3742 return after;
3743 }
3744
3745 /* See declaration. */
3746
3747 std::pair<std::vector<name_component>::const_iterator,
3748 std::vector<name_component>::const_iterator>
3749 mapped_index_base::find_name_components_bounds
3750 (const lookup_name_info &lookup_name_without_params, language lang) const
3751 {
3752 auto *name_cmp
3753 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3754
3755 const char *lang_name
3756 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3757
3758 /* Comparison function object for lower_bound that matches against a
3759 given symbol name. */
3760 auto lookup_compare_lower = [&] (const name_component &elem,
3761 const char *name)
3762 {
3763 const char *elem_qualified = this->symbol_name_at (elem.idx);
3764 const char *elem_name = elem_qualified + elem.name_offset;
3765 return name_cmp (elem_name, name) < 0;
3766 };
3767
3768 /* Comparison function object for upper_bound that matches against a
3769 given symbol name. */
3770 auto lookup_compare_upper = [&] (const char *name,
3771 const name_component &elem)
3772 {
3773 const char *elem_qualified = this->symbol_name_at (elem.idx);
3774 const char *elem_name = elem_qualified + elem.name_offset;
3775 return name_cmp (name, elem_name) < 0;
3776 };
3777
3778 auto begin = this->name_components.begin ();
3779 auto end = this->name_components.end ();
3780
3781 /* Find the lower bound. */
3782 auto lower = [&] ()
3783 {
3784 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3785 return begin;
3786 else
3787 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3788 } ();
3789
3790 /* Find the upper bound. */
3791 auto upper = [&] ()
3792 {
3793 if (lookup_name_without_params.completion_mode ())
3794 {
3795 /* In completion mode, we want UPPER to point past all
3796 symbols names that have the same prefix. I.e., with
3797 these symbols, and completing "func":
3798
3799 function << lower bound
3800 function1
3801 other_function << upper bound
3802
3803 We find the upper bound by looking for the insertion
3804 point of "func"-with-last-character-incremented,
3805 i.e. "fund". */
3806 std::string after = make_sort_after_prefix_name (lang_name);
3807 if (after.empty ())
3808 return end;
3809 return std::lower_bound (lower, end, after.c_str (),
3810 lookup_compare_lower);
3811 }
3812 else
3813 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3814 } ();
3815
3816 return {lower, upper};
3817 }
3818
3819 /* See declaration. */
3820
3821 void
3822 mapped_index_base::build_name_components ()
3823 {
3824 if (!this->name_components.empty ())
3825 return;
3826
3827 this->name_components_casing = case_sensitivity;
3828 auto *name_cmp
3829 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3830
3831 /* The code below only knows how to break apart components of C++
3832 symbol names (and other languages that use '::' as
3833 namespace/module separator) and Ada symbol names. */
3834 auto count = this->symbol_name_count ();
3835 for (offset_type idx = 0; idx < count; idx++)
3836 {
3837 if (this->symbol_name_slot_invalid (idx))
3838 continue;
3839
3840 const char *name = this->symbol_name_at (idx);
3841
3842 /* Add each name component to the name component table. */
3843 unsigned int previous_len = 0;
3844
3845 if (strstr (name, "::") != nullptr)
3846 {
3847 for (unsigned int current_len = cp_find_first_component (name);
3848 name[current_len] != '\0';
3849 current_len += cp_find_first_component (name + current_len))
3850 {
3851 gdb_assert (name[current_len] == ':');
3852 this->name_components.push_back ({previous_len, idx});
3853 /* Skip the '::'. */
3854 current_len += 2;
3855 previous_len = current_len;
3856 }
3857 }
3858 else
3859 {
3860 /* Handle the Ada encoded (aka mangled) form here. */
3861 for (const char *iter = strstr (name, "__");
3862 iter != nullptr;
3863 iter = strstr (iter, "__"))
3864 {
3865 this->name_components.push_back ({previous_len, idx});
3866 iter += 2;
3867 previous_len = iter - name;
3868 }
3869 }
3870
3871 this->name_components.push_back ({previous_len, idx});
3872 }
3873
3874 /* Sort name_components elements by name. */
3875 auto name_comp_compare = [&] (const name_component &left,
3876 const name_component &right)
3877 {
3878 const char *left_qualified = this->symbol_name_at (left.idx);
3879 const char *right_qualified = this->symbol_name_at (right.idx);
3880
3881 const char *left_name = left_qualified + left.name_offset;
3882 const char *right_name = right_qualified + right.name_offset;
3883
3884 return name_cmp (left_name, right_name) < 0;
3885 };
3886
3887 std::sort (this->name_components.begin (),
3888 this->name_components.end (),
3889 name_comp_compare);
3890 }
3891
3892 /* Helper for dw2_expand_symtabs_matching that works with a
3893 mapped_index_base instead of the containing objfile. This is split
3894 to a separate function in order to be able to unit test the
3895 name_components matching using a mock mapped_index_base. For each
3896 symbol name that matches, calls MATCH_CALLBACK, passing it the
3897 symbol's index in the mapped_index_base symbol table. */
3898
3899 static void
3900 dw2_expand_symtabs_matching_symbol
3901 (mapped_index_base &index,
3902 const lookup_name_info &lookup_name_in,
3903 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3904 enum search_domain kind,
3905 gdb::function_view<bool (offset_type)> match_callback)
3906 {
3907 lookup_name_info lookup_name_without_params
3908 = lookup_name_in.make_ignore_params ();
3909
3910 /* Build the symbol name component sorted vector, if we haven't
3911 yet. */
3912 index.build_name_components ();
3913
3914 /* The same symbol may appear more than once in the range though.
3915 E.g., if we're looking for symbols that complete "w", and we have
3916 a symbol named "w1::w2", we'll find the two name components for
3917 that same symbol in the range. To be sure we only call the
3918 callback once per symbol, we first collect the symbol name
3919 indexes that matched in a temporary vector and ignore
3920 duplicates. */
3921 std::vector<offset_type> matches;
3922
3923 struct name_and_matcher
3924 {
3925 symbol_name_matcher_ftype *matcher;
3926 const std::string &name;
3927
3928 bool operator== (const name_and_matcher &other) const
3929 {
3930 return matcher == other.matcher && name == other.name;
3931 }
3932 };
3933
3934 /* A vector holding all the different symbol name matchers, for all
3935 languages. */
3936 std::vector<name_and_matcher> matchers;
3937
3938 for (int i = 0; i < nr_languages; i++)
3939 {
3940 enum language lang_e = (enum language) i;
3941
3942 const language_defn *lang = language_def (lang_e);
3943 symbol_name_matcher_ftype *name_matcher
3944 = get_symbol_name_matcher (lang, lookup_name_without_params);
3945
3946 name_and_matcher key {
3947 name_matcher,
3948 lookup_name_without_params.language_lookup_name (lang_e)
3949 };
3950
3951 /* Don't insert the same comparison routine more than once.
3952 Note that we do this linear walk. This is not a problem in
3953 practice because the number of supported languages is
3954 low. */
3955 if (std::find (matchers.begin (), matchers.end (), key)
3956 != matchers.end ())
3957 continue;
3958 matchers.push_back (std::move (key));
3959
3960 auto bounds
3961 = index.find_name_components_bounds (lookup_name_without_params,
3962 lang_e);
3963
3964 /* Now for each symbol name in range, check to see if we have a name
3965 match, and if so, call the MATCH_CALLBACK callback. */
3966
3967 for (; bounds.first != bounds.second; ++bounds.first)
3968 {
3969 const char *qualified = index.symbol_name_at (bounds.first->idx);
3970
3971 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3972 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3973 continue;
3974
3975 matches.push_back (bounds.first->idx);
3976 }
3977 }
3978
3979 std::sort (matches.begin (), matches.end ());
3980
3981 /* Finally call the callback, once per match. */
3982 ULONGEST prev = -1;
3983 for (offset_type idx : matches)
3984 {
3985 if (prev != idx)
3986 {
3987 if (!match_callback (idx))
3988 break;
3989 prev = idx;
3990 }
3991 }
3992
3993 /* Above we use a type wider than idx's for 'prev', since 0 and
3994 (offset_type)-1 are both possible values. */
3995 static_assert (sizeof (prev) > sizeof (offset_type), "");
3996 }
3997
3998 #if GDB_SELF_TEST
3999
4000 namespace selftests { namespace dw2_expand_symtabs_matching {
4001
4002 /* A mock .gdb_index/.debug_names-like name index table, enough to
4003 exercise dw2_expand_symtabs_matching_symbol, which works with the
4004 mapped_index_base interface. Builds an index from the symbol list
4005 passed as parameter to the constructor. */
4006 class mock_mapped_index : public mapped_index_base
4007 {
4008 public:
4009 mock_mapped_index (gdb::array_view<const char *> symbols)
4010 : m_symbol_table (symbols)
4011 {}
4012
4013 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4014
4015 /* Return the number of names in the symbol table. */
4016 size_t symbol_name_count () const override
4017 {
4018 return m_symbol_table.size ();
4019 }
4020
4021 /* Get the name of the symbol at IDX in the symbol table. */
4022 const char *symbol_name_at (offset_type idx) const override
4023 {
4024 return m_symbol_table[idx];
4025 }
4026
4027 private:
4028 gdb::array_view<const char *> m_symbol_table;
4029 };
4030
4031 /* Convenience function that converts a NULL pointer to a "<null>"
4032 string, to pass to print routines. */
4033
4034 static const char *
4035 string_or_null (const char *str)
4036 {
4037 return str != NULL ? str : "<null>";
4038 }
4039
4040 /* Check if a lookup_name_info built from
4041 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4042 index. EXPECTED_LIST is the list of expected matches, in expected
4043 matching order. If no match expected, then an empty list is
4044 specified. Returns true on success. On failure prints a warning
4045 indicating the file:line that failed, and returns false. */
4046
4047 static bool
4048 check_match (const char *file, int line,
4049 mock_mapped_index &mock_index,
4050 const char *name, symbol_name_match_type match_type,
4051 bool completion_mode,
4052 std::initializer_list<const char *> expected_list)
4053 {
4054 lookup_name_info lookup_name (name, match_type, completion_mode);
4055
4056 bool matched = true;
4057
4058 auto mismatch = [&] (const char *expected_str,
4059 const char *got)
4060 {
4061 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4062 "expected=\"%s\", got=\"%s\"\n"),
4063 file, line,
4064 (match_type == symbol_name_match_type::FULL
4065 ? "FULL" : "WILD"),
4066 name, string_or_null (expected_str), string_or_null (got));
4067 matched = false;
4068 };
4069
4070 auto expected_it = expected_list.begin ();
4071 auto expected_end = expected_list.end ();
4072
4073 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4074 NULL, ALL_DOMAIN,
4075 [&] (offset_type idx)
4076 {
4077 const char *matched_name = mock_index.symbol_name_at (idx);
4078 const char *expected_str
4079 = expected_it == expected_end ? NULL : *expected_it++;
4080
4081 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4082 mismatch (expected_str, matched_name);
4083 return true;
4084 });
4085
4086 const char *expected_str
4087 = expected_it == expected_end ? NULL : *expected_it++;
4088 if (expected_str != NULL)
4089 mismatch (expected_str, NULL);
4090
4091 return matched;
4092 }
4093
4094 /* The symbols added to the mock mapped_index for testing (in
4095 canonical form). */
4096 static const char *test_symbols[] = {
4097 "function",
4098 "std::bar",
4099 "std::zfunction",
4100 "std::zfunction2",
4101 "w1::w2",
4102 "ns::foo<char*>",
4103 "ns::foo<int>",
4104 "ns::foo<long>",
4105 "ns2::tmpl<int>::foo2",
4106 "(anonymous namespace)::A::B::C",
4107
4108 /* These are used to check that the increment-last-char in the
4109 matching algorithm for completion doesn't match "t1_fund" when
4110 completing "t1_func". */
4111 "t1_func",
4112 "t1_func1",
4113 "t1_fund",
4114 "t1_fund1",
4115
4116 /* A UTF-8 name with multi-byte sequences to make sure that
4117 cp-name-parser understands this as a single identifier ("função"
4118 is "function" in PT). */
4119 u8"u8função",
4120
4121 /* \377 (0xff) is Latin1 'ÿ'. */
4122 "yfunc\377",
4123
4124 /* \377 (0xff) is Latin1 'ÿ'. */
4125 "\377",
4126 "\377\377123",
4127
4128 /* A name with all sorts of complications. Starts with "z" to make
4129 it easier for the completion tests below. */
4130 #define Z_SYM_NAME \
4131 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4132 "::tuple<(anonymous namespace)::ui*, " \
4133 "std::default_delete<(anonymous namespace)::ui>, void>"
4134
4135 Z_SYM_NAME
4136 };
4137
4138 /* Returns true if the mapped_index_base::find_name_component_bounds
4139 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4140 in completion mode. */
4141
4142 static bool
4143 check_find_bounds_finds (mapped_index_base &index,
4144 const char *search_name,
4145 gdb::array_view<const char *> expected_syms)
4146 {
4147 lookup_name_info lookup_name (search_name,
4148 symbol_name_match_type::FULL, true);
4149
4150 auto bounds = index.find_name_components_bounds (lookup_name,
4151 language_cplus);
4152
4153 size_t distance = std::distance (bounds.first, bounds.second);
4154 if (distance != expected_syms.size ())
4155 return false;
4156
4157 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4158 {
4159 auto nc_elem = bounds.first + exp_elem;
4160 const char *qualified = index.symbol_name_at (nc_elem->idx);
4161 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4162 return false;
4163 }
4164
4165 return true;
4166 }
4167
4168 /* Test the lower-level mapped_index::find_name_component_bounds
4169 method. */
4170
4171 static void
4172 test_mapped_index_find_name_component_bounds ()
4173 {
4174 mock_mapped_index mock_index (test_symbols);
4175
4176 mock_index.build_name_components ();
4177
4178 /* Test the lower-level mapped_index::find_name_component_bounds
4179 method in completion mode. */
4180 {
4181 static const char *expected_syms[] = {
4182 "t1_func",
4183 "t1_func1",
4184 };
4185
4186 SELF_CHECK (check_find_bounds_finds (mock_index,
4187 "t1_func", expected_syms));
4188 }
4189
4190 /* Check that the increment-last-char in the name matching algorithm
4191 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4192 {
4193 static const char *expected_syms1[] = {
4194 "\377",
4195 "\377\377123",
4196 };
4197 SELF_CHECK (check_find_bounds_finds (mock_index,
4198 "\377", expected_syms1));
4199
4200 static const char *expected_syms2[] = {
4201 "\377\377123",
4202 };
4203 SELF_CHECK (check_find_bounds_finds (mock_index,
4204 "\377\377", expected_syms2));
4205 }
4206 }
4207
4208 /* Test dw2_expand_symtabs_matching_symbol. */
4209
4210 static void
4211 test_dw2_expand_symtabs_matching_symbol ()
4212 {
4213 mock_mapped_index mock_index (test_symbols);
4214
4215 /* We let all tests run until the end even if some fails, for debug
4216 convenience. */
4217 bool any_mismatch = false;
4218
4219 /* Create the expected symbols list (an initializer_list). Needed
4220 because lists have commas, and we need to pass them to CHECK,
4221 which is a macro. */
4222 #define EXPECT(...) { __VA_ARGS__ }
4223
4224 /* Wrapper for check_match that passes down the current
4225 __FILE__/__LINE__. */
4226 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4227 any_mismatch |= !check_match (__FILE__, __LINE__, \
4228 mock_index, \
4229 NAME, MATCH_TYPE, COMPLETION_MODE, \
4230 EXPECTED_LIST)
4231
4232 /* Identity checks. */
4233 for (const char *sym : test_symbols)
4234 {
4235 /* Should be able to match all existing symbols. */
4236 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* Should be able to match all existing symbols with
4240 parameters. */
4241 std::string with_params = std::string (sym) + "(int)";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 EXPECT (sym));
4244
4245 /* Should be able to match all existing symbols with
4246 parameters and qualifiers. */
4247 with_params = std::string (sym) + " ( int ) const";
4248 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4249 EXPECT (sym));
4250
4251 /* This should really find sym, but cp-name-parser.y doesn't
4252 know about lvalue/rvalue qualifiers yet. */
4253 with_params = std::string (sym) + " ( int ) &&";
4254 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4255 {});
4256 }
4257
4258 /* Check that the name matching algorithm for completion doesn't get
4259 confused with Latin1 'ÿ' / 0xff. */
4260 {
4261 static const char str[] = "\377";
4262 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4263 EXPECT ("\377", "\377\377123"));
4264 }
4265
4266 /* Check that the increment-last-char in the matching algorithm for
4267 completion doesn't match "t1_fund" when completing "t1_func". */
4268 {
4269 static const char str[] = "t1_func";
4270 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4271 EXPECT ("t1_func", "t1_func1"));
4272 }
4273
4274 /* Check that completion mode works at each prefix of the expected
4275 symbol name. */
4276 {
4277 static const char str[] = "function(int)";
4278 size_t len = strlen (str);
4279 std::string lookup;
4280
4281 for (size_t i = 1; i < len; i++)
4282 {
4283 lookup.assign (str, i);
4284 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4285 EXPECT ("function"));
4286 }
4287 }
4288
4289 /* While "w" is a prefix of both components, the match function
4290 should still only be called once. */
4291 {
4292 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4293 EXPECT ("w1::w2"));
4294 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4295 EXPECT ("w1::w2"));
4296 }
4297
4298 /* Same, with a "complicated" symbol. */
4299 {
4300 static const char str[] = Z_SYM_NAME;
4301 size_t len = strlen (str);
4302 std::string lookup;
4303
4304 for (size_t i = 1; i < len; i++)
4305 {
4306 lookup.assign (str, i);
4307 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4308 EXPECT (Z_SYM_NAME));
4309 }
4310 }
4311
4312 /* In FULL mode, an incomplete symbol doesn't match. */
4313 {
4314 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4315 {});
4316 }
4317
4318 /* A complete symbol with parameters matches any overload, since the
4319 index has no overload info. */
4320 {
4321 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4322 EXPECT ("std::zfunction", "std::zfunction2"));
4323 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4324 EXPECT ("std::zfunction", "std::zfunction2"));
4325 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4326 EXPECT ("std::zfunction", "std::zfunction2"));
4327 }
4328
4329 /* Check that whitespace is ignored appropriately. A symbol with a
4330 template argument list. */
4331 {
4332 static const char expected[] = "ns::foo<int>";
4333 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4334 EXPECT (expected));
4335 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4336 EXPECT (expected));
4337 }
4338
4339 /* Check that whitespace is ignored appropriately. A symbol with a
4340 template argument list that includes a pointer. */
4341 {
4342 static const char expected[] = "ns::foo<char*>";
4343 /* Try both completion and non-completion modes. */
4344 static const bool completion_mode[2] = {false, true};
4345 for (size_t i = 0; i < 2; i++)
4346 {
4347 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4348 completion_mode[i], EXPECT (expected));
4349 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4350 completion_mode[i], EXPECT (expected));
4351
4352 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4353 completion_mode[i], EXPECT (expected));
4354 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4355 completion_mode[i], EXPECT (expected));
4356 }
4357 }
4358
4359 {
4360 /* Check method qualifiers are ignored. */
4361 static const char expected[] = "ns::foo<char*>";
4362 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4363 symbol_name_match_type::FULL, true, EXPECT (expected));
4364 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4365 symbol_name_match_type::FULL, true, EXPECT (expected));
4366 CHECK_MATCH ("foo < char * > ( int ) const",
4367 symbol_name_match_type::WILD, true, EXPECT (expected));
4368 CHECK_MATCH ("foo < char * > ( int ) &&",
4369 symbol_name_match_type::WILD, true, EXPECT (expected));
4370 }
4371
4372 /* Test lookup names that don't match anything. */
4373 {
4374 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4375 {});
4376
4377 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4378 {});
4379 }
4380
4381 /* Some wild matching tests, exercising "(anonymous namespace)",
4382 which should not be confused with a parameter list. */
4383 {
4384 static const char *syms[] = {
4385 "A::B::C",
4386 "B::C",
4387 "C",
4388 "A :: B :: C ( int )",
4389 "B :: C ( int )",
4390 "C ( int )",
4391 };
4392
4393 for (const char *s : syms)
4394 {
4395 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4396 EXPECT ("(anonymous namespace)::A::B::C"));
4397 }
4398 }
4399
4400 {
4401 static const char expected[] = "ns2::tmpl<int>::foo2";
4402 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4403 EXPECT (expected));
4404 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4405 EXPECT (expected));
4406 }
4407
4408 SELF_CHECK (!any_mismatch);
4409
4410 #undef EXPECT
4411 #undef CHECK_MATCH
4412 }
4413
4414 static void
4415 run_test ()
4416 {
4417 test_mapped_index_find_name_component_bounds ();
4418 test_dw2_expand_symtabs_matching_symbol ();
4419 }
4420
4421 }} // namespace selftests::dw2_expand_symtabs_matching
4422
4423 #endif /* GDB_SELF_TEST */
4424
4425 /* If FILE_MATCHER is NULL or if PER_CU has
4426 dwarf2_per_cu_quick_data::MARK set (see
4427 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4428 EXPANSION_NOTIFY on it. */
4429
4430 static void
4431 dw2_expand_symtabs_matching_one
4432 (struct dwarf2_per_cu_data *per_cu,
4433 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4434 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4435 {
4436 if (file_matcher == NULL || per_cu->v.quick->mark)
4437 {
4438 bool symtab_was_null
4439 = (per_cu->v.quick->compunit_symtab == NULL);
4440
4441 dw2_instantiate_symtab (per_cu, false);
4442
4443 if (expansion_notify != NULL
4444 && symtab_was_null
4445 && per_cu->v.quick->compunit_symtab != NULL)
4446 expansion_notify (per_cu->v.quick->compunit_symtab);
4447 }
4448 }
4449
4450 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4451 matched, to expand corresponding CUs that were marked. IDX is the
4452 index of the symbol name that matched. */
4453
4454 static void
4455 dw2_expand_marked_cus
4456 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4457 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4458 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4459 search_domain kind)
4460 {
4461 offset_type *vec, vec_len, vec_idx;
4462 bool global_seen = false;
4463 mapped_index &index = *dwarf2_per_objfile->index_table;
4464
4465 vec = (offset_type *) (index.constant_pool
4466 + MAYBE_SWAP (index.symbol_table[idx].vec));
4467 vec_len = MAYBE_SWAP (vec[0]);
4468 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4469 {
4470 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4471 /* This value is only valid for index versions >= 7. */
4472 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4473 gdb_index_symbol_kind symbol_kind =
4474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4475 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4476 /* Only check the symbol attributes if they're present.
4477 Indices prior to version 7 don't record them,
4478 and indices >= 7 may elide them for certain symbols
4479 (gold does this). */
4480 int attrs_valid =
4481 (index.version >= 7
4482 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4483
4484 /* Work around gold/15646. */
4485 if (attrs_valid)
4486 {
4487 if (!is_static && global_seen)
4488 continue;
4489 if (!is_static)
4490 global_seen = true;
4491 }
4492
4493 /* Only check the symbol's kind if it has one. */
4494 if (attrs_valid)
4495 {
4496 switch (kind)
4497 {
4498 case VARIABLES_DOMAIN:
4499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4500 continue;
4501 break;
4502 case FUNCTIONS_DOMAIN:
4503 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4504 continue;
4505 break;
4506 case TYPES_DOMAIN:
4507 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4508 continue;
4509 break;
4510 case MODULES_DOMAIN:
4511 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4512 continue;
4513 break;
4514 default:
4515 break;
4516 }
4517 }
4518
4519 /* Don't crash on bad data. */
4520 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4521 + dwarf2_per_objfile->all_type_units.size ()))
4522 {
4523 complaint (_(".gdb_index entry has bad CU index"
4524 " [in module %s]"),
4525 objfile_name (dwarf2_per_objfile->objfile));
4526 continue;
4527 }
4528
4529 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4530 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4531 expansion_notify);
4532 }
4533 }
4534
4535 /* If FILE_MATCHER is non-NULL, set all the
4536 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4537 that match FILE_MATCHER. */
4538
4539 static void
4540 dw_expand_symtabs_matching_file_matcher
4541 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4542 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4543 {
4544 if (file_matcher == NULL)
4545 return;
4546
4547 objfile *const objfile = dwarf2_per_objfile->objfile;
4548
4549 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4550 htab_eq_pointer,
4551 NULL, xcalloc, xfree));
4552 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4553 htab_eq_pointer,
4554 NULL, xcalloc, xfree));
4555
4556 /* The rule is CUs specify all the files, including those used by
4557 any TU, so there's no need to scan TUs here. */
4558
4559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4560 {
4561 QUIT;
4562
4563 per_cu->v.quick->mark = 0;
4564
4565 /* We only need to look at symtabs not already expanded. */
4566 if (per_cu->v.quick->compunit_symtab)
4567 continue;
4568
4569 quick_file_names *file_data = dw2_get_file_names (per_cu);
4570 if (file_data == NULL)
4571 continue;
4572
4573 if (htab_find (visited_not_found.get (), file_data) != NULL)
4574 continue;
4575 else if (htab_find (visited_found.get (), file_data) != NULL)
4576 {
4577 per_cu->v.quick->mark = 1;
4578 continue;
4579 }
4580
4581 for (int j = 0; j < file_data->num_file_names; ++j)
4582 {
4583 const char *this_real_name;
4584
4585 if (file_matcher (file_data->file_names[j], false))
4586 {
4587 per_cu->v.quick->mark = 1;
4588 break;
4589 }
4590
4591 /* Before we invoke realpath, which can get expensive when many
4592 files are involved, do a quick comparison of the basenames. */
4593 if (!basenames_may_differ
4594 && !file_matcher (lbasename (file_data->file_names[j]),
4595 true))
4596 continue;
4597
4598 this_real_name = dw2_get_real_path (objfile, file_data, j);
4599 if (file_matcher (this_real_name, false))
4600 {
4601 per_cu->v.quick->mark = 1;
4602 break;
4603 }
4604 }
4605
4606 void **slot = htab_find_slot (per_cu->v.quick->mark
4607 ? visited_found.get ()
4608 : visited_not_found.get (),
4609 file_data, INSERT);
4610 *slot = file_data;
4611 }
4612 }
4613
4614 static void
4615 dw2_expand_symtabs_matching
4616 (struct objfile *objfile,
4617 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4618 const lookup_name_info &lookup_name,
4619 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4620 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4621 enum search_domain kind)
4622 {
4623 struct dwarf2_per_objfile *dwarf2_per_objfile
4624 = get_dwarf2_per_objfile (objfile);
4625
4626 /* index_table is NULL if OBJF_READNOW. */
4627 if (!dwarf2_per_objfile->index_table)
4628 return;
4629
4630 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4631
4632 mapped_index &index = *dwarf2_per_objfile->index_table;
4633
4634 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4635 symbol_matcher,
4636 kind, [&] (offset_type idx)
4637 {
4638 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4639 expansion_notify, kind);
4640 return true;
4641 });
4642 }
4643
4644 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4645 symtab. */
4646
4647 static struct compunit_symtab *
4648 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4649 CORE_ADDR pc)
4650 {
4651 int i;
4652
4653 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4654 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4655 return cust;
4656
4657 if (cust->includes == NULL)
4658 return NULL;
4659
4660 for (i = 0; cust->includes[i]; ++i)
4661 {
4662 struct compunit_symtab *s = cust->includes[i];
4663
4664 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4665 if (s != NULL)
4666 return s;
4667 }
4668
4669 return NULL;
4670 }
4671
4672 static struct compunit_symtab *
4673 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4674 struct bound_minimal_symbol msymbol,
4675 CORE_ADDR pc,
4676 struct obj_section *section,
4677 int warn_if_readin)
4678 {
4679 struct dwarf2_per_cu_data *data;
4680 struct compunit_symtab *result;
4681
4682 if (!objfile->partial_symtabs->psymtabs_addrmap)
4683 return NULL;
4684
4685 CORE_ADDR baseaddr = objfile->text_section_offset ();
4686 data = (struct dwarf2_per_cu_data *) addrmap_find
4687 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4688 if (!data)
4689 return NULL;
4690
4691 if (warn_if_readin && data->v.quick->compunit_symtab)
4692 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4693 paddress (get_objfile_arch (objfile), pc));
4694
4695 result
4696 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4697 false),
4698 pc);
4699 gdb_assert (result != NULL);
4700 return result;
4701 }
4702
4703 static void
4704 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4705 void *data, int need_fullname)
4706 {
4707 struct dwarf2_per_objfile *dwarf2_per_objfile
4708 = get_dwarf2_per_objfile (objfile);
4709
4710 if (!dwarf2_per_objfile->filenames_cache)
4711 {
4712 dwarf2_per_objfile->filenames_cache.emplace ();
4713
4714 htab_up visited (htab_create_alloc (10,
4715 htab_hash_pointer, htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used
4719 by any TU, so there's no need to scan TUs here. We can
4720 ignore file names coming from already-expanded CUs. */
4721
4722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4723 {
4724 if (per_cu->v.quick->compunit_symtab)
4725 {
4726 void **slot = htab_find_slot (visited.get (),
4727 per_cu->v.quick->file_names,
4728 INSERT);
4729
4730 *slot = per_cu->v.quick->file_names;
4731 }
4732 }
4733
4734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4735 {
4736 /* We only need to look at symtabs not already expanded. */
4737 if (per_cu->v.quick->compunit_symtab)
4738 continue;
4739
4740 quick_file_names *file_data = dw2_get_file_names (per_cu);
4741 if (file_data == NULL)
4742 continue;
4743
4744 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4745 if (*slot)
4746 {
4747 /* Already visited. */
4748 continue;
4749 }
4750 *slot = file_data;
4751
4752 for (int j = 0; j < file_data->num_file_names; ++j)
4753 {
4754 const char *filename = file_data->file_names[j];
4755 dwarf2_per_objfile->filenames_cache->seen (filename);
4756 }
4757 }
4758 }
4759
4760 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4761 {
4762 gdb::unique_xmalloc_ptr<char> this_real_name;
4763
4764 if (need_fullname)
4765 this_real_name = gdb_realpath (filename);
4766 (*fun) (filename, this_real_name.get (), data);
4767 });
4768 }
4769
4770 static int
4771 dw2_has_symbols (struct objfile *objfile)
4772 {
4773 return 1;
4774 }
4775
4776 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4777 {
4778 dw2_has_symbols,
4779 dw2_find_last_source_symtab,
4780 dw2_forget_cached_source_info,
4781 dw2_map_symtabs_matching_filename,
4782 dw2_lookup_symbol,
4783 dw2_print_stats,
4784 dw2_dump,
4785 dw2_expand_symtabs_for_function,
4786 dw2_expand_all_symtabs,
4787 dw2_expand_symtabs_with_fullname,
4788 dw2_map_matching_symbols,
4789 dw2_expand_symtabs_matching,
4790 dw2_find_pc_sect_compunit_symtab,
4791 NULL,
4792 dw2_map_symbol_filenames
4793 };
4794
4795 /* DWARF-5 debug_names reader. */
4796
4797 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4798 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4799
4800 /* A helper function that reads the .debug_names section in SECTION
4801 and fills in MAP. FILENAME is the name of the file containing the
4802 section; it is used for error reporting.
4803
4804 Returns true if all went well, false otherwise. */
4805
4806 static bool
4807 read_debug_names_from_section (struct objfile *objfile,
4808 const char *filename,
4809 struct dwarf2_section_info *section,
4810 mapped_debug_names &map)
4811 {
4812 if (section->empty ())
4813 return false;
4814
4815 /* Older elfutils strip versions could keep the section in the main
4816 executable while splitting it for the separate debug info file. */
4817 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4818 return false;
4819
4820 section->read (objfile);
4821
4822 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4823
4824 const gdb_byte *addr = section->buffer;
4825
4826 bfd *const abfd = section->get_bfd_owner ();
4827
4828 unsigned int bytes_read;
4829 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4830 addr += bytes_read;
4831
4832 map.dwarf5_is_dwarf64 = bytes_read != 4;
4833 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4834 if (bytes_read + length != section->size)
4835 {
4836 /* There may be multiple per-CU indices. */
4837 warning (_("Section .debug_names in %s length %s does not match "
4838 "section length %s, ignoring .debug_names."),
4839 filename, plongest (bytes_read + length),
4840 pulongest (section->size));
4841 return false;
4842 }
4843
4844 /* The version number. */
4845 uint16_t version = read_2_bytes (abfd, addr);
4846 addr += 2;
4847 if (version != 5)
4848 {
4849 warning (_("Section .debug_names in %s has unsupported version %d, "
4850 "ignoring .debug_names."),
4851 filename, version);
4852 return false;
4853 }
4854
4855 /* Padding. */
4856 uint16_t padding = read_2_bytes (abfd, addr);
4857 addr += 2;
4858 if (padding != 0)
4859 {
4860 warning (_("Section .debug_names in %s has unsupported padding %d, "
4861 "ignoring .debug_names."),
4862 filename, padding);
4863 return false;
4864 }
4865
4866 /* comp_unit_count - The number of CUs in the CU list. */
4867 map.cu_count = read_4_bytes (abfd, addr);
4868 addr += 4;
4869
4870 /* local_type_unit_count - The number of TUs in the local TU
4871 list. */
4872 map.tu_count = read_4_bytes (abfd, addr);
4873 addr += 4;
4874
4875 /* foreign_type_unit_count - The number of TUs in the foreign TU
4876 list. */
4877 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4878 addr += 4;
4879 if (foreign_tu_count != 0)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4882 "ignoring .debug_names."),
4883 filename, static_cast<unsigned long> (foreign_tu_count));
4884 return false;
4885 }
4886
4887 /* bucket_count - The number of hash buckets in the hash lookup
4888 table. */
4889 map.bucket_count = read_4_bytes (abfd, addr);
4890 addr += 4;
4891
4892 /* name_count - The number of unique names in the index. */
4893 map.name_count = read_4_bytes (abfd, addr);
4894 addr += 4;
4895
4896 /* abbrev_table_size - The size in bytes of the abbreviations
4897 table. */
4898 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4899 addr += 4;
4900
4901 /* augmentation_string_size - The size in bytes of the augmentation
4902 string. This value is rounded up to a multiple of 4. */
4903 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4904 addr += 4;
4905 map.augmentation_is_gdb = ((augmentation_string_size
4906 == sizeof (dwarf5_augmentation))
4907 && memcmp (addr, dwarf5_augmentation,
4908 sizeof (dwarf5_augmentation)) == 0);
4909 augmentation_string_size += (-augmentation_string_size) & 3;
4910 addr += augmentation_string_size;
4911
4912 /* List of CUs */
4913 map.cu_table_reordered = addr;
4914 addr += map.cu_count * map.offset_size;
4915
4916 /* List of Local TUs */
4917 map.tu_table_reordered = addr;
4918 addr += map.tu_count * map.offset_size;
4919
4920 /* Hash Lookup Table */
4921 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4922 addr += map.bucket_count * 4;
4923 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4924 addr += map.name_count * 4;
4925
4926 /* Name Table */
4927 map.name_table_string_offs_reordered = addr;
4928 addr += map.name_count * map.offset_size;
4929 map.name_table_entry_offs_reordered = addr;
4930 addr += map.name_count * map.offset_size;
4931
4932 const gdb_byte *abbrev_table_start = addr;
4933 for (;;)
4934 {
4935 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4936 addr += bytes_read;
4937 if (index_num == 0)
4938 break;
4939
4940 const auto insertpair
4941 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4942 if (!insertpair.second)
4943 {
4944 warning (_("Section .debug_names in %s has duplicate index %s, "
4945 "ignoring .debug_names."),
4946 filename, pulongest (index_num));
4947 return false;
4948 }
4949 mapped_debug_names::index_val &indexval = insertpair.first->second;
4950 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4951 addr += bytes_read;
4952
4953 for (;;)
4954 {
4955 mapped_debug_names::index_val::attr attr;
4956 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4959 addr += bytes_read;
4960 if (attr.form == DW_FORM_implicit_const)
4961 {
4962 attr.implicit_const = read_signed_leb128 (abfd, addr,
4963 &bytes_read);
4964 addr += bytes_read;
4965 }
4966 if (attr.dw_idx == 0 && attr.form == 0)
4967 break;
4968 indexval.attr_vec.push_back (std::move (attr));
4969 }
4970 }
4971 if (addr != abbrev_table_start + abbrev_table_size)
4972 {
4973 warning (_("Section .debug_names in %s has abbreviation_table "
4974 "of size %s vs. written as %u, ignoring .debug_names."),
4975 filename, plongest (addr - abbrev_table_start),
4976 abbrev_table_size);
4977 return false;
4978 }
4979 map.entry_pool = addr;
4980
4981 return true;
4982 }
4983
4984 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4985 list. */
4986
4987 static void
4988 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4989 const mapped_debug_names &map,
4990 dwarf2_section_info &section,
4991 bool is_dwz)
4992 {
4993 sect_offset sect_off_prev;
4994 for (uint32_t i = 0; i <= map.cu_count; ++i)
4995 {
4996 sect_offset sect_off_next;
4997 if (i < map.cu_count)
4998 {
4999 sect_off_next
5000 = (sect_offset) (extract_unsigned_integer
5001 (map.cu_table_reordered + i * map.offset_size,
5002 map.offset_size,
5003 map.dwarf5_byte_order));
5004 }
5005 else
5006 sect_off_next = (sect_offset) section.size;
5007 if (i >= 1)
5008 {
5009 const ULONGEST length = sect_off_next - sect_off_prev;
5010 dwarf2_per_cu_data *per_cu
5011 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5012 sect_off_prev, length);
5013 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5014 }
5015 sect_off_prev = sect_off_next;
5016 }
5017 }
5018
5019 /* Read the CU list from the mapped index, and use it to create all
5020 the CU objects for this dwarf2_per_objfile. */
5021
5022 static void
5023 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5024 const mapped_debug_names &map,
5025 const mapped_debug_names &dwz_map)
5026 {
5027 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5028 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5029
5030 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5031 dwarf2_per_objfile->info,
5032 false /* is_dwz */);
5033
5034 if (dwz_map.cu_count == 0)
5035 return;
5036
5037 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5038 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5039 true /* is_dwz */);
5040 }
5041
5042 /* Read .debug_names. If everything went ok, initialize the "quick"
5043 elements of all the CUs and return true. Otherwise, return false. */
5044
5045 static bool
5046 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5047 {
5048 std::unique_ptr<mapped_debug_names> map
5049 (new mapped_debug_names (dwarf2_per_objfile));
5050 mapped_debug_names dwz_map (dwarf2_per_objfile);
5051 struct objfile *objfile = dwarf2_per_objfile->objfile;
5052
5053 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5054 &dwarf2_per_objfile->debug_names,
5055 *map))
5056 return false;
5057
5058 /* Don't use the index if it's empty. */
5059 if (map->name_count == 0)
5060 return false;
5061
5062 /* If there is a .dwz file, read it so we can get its CU list as
5063 well. */
5064 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5065 if (dwz != NULL)
5066 {
5067 if (!read_debug_names_from_section (objfile,
5068 bfd_get_filename (dwz->dwz_bfd.get ()),
5069 &dwz->debug_names, dwz_map))
5070 {
5071 warning (_("could not read '.debug_names' section from %s; skipping"),
5072 bfd_get_filename (dwz->dwz_bfd.get ()));
5073 return false;
5074 }
5075 }
5076
5077 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5078
5079 if (map->tu_count != 0)
5080 {
5081 /* We can only handle a single .debug_types when we have an
5082 index. */
5083 if (dwarf2_per_objfile->types.size () != 1)
5084 return false;
5085
5086 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5087
5088 create_signatured_type_table_from_debug_names
5089 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5090 }
5091
5092 create_addrmap_from_aranges (dwarf2_per_objfile,
5093 &dwarf2_per_objfile->debug_aranges);
5094
5095 dwarf2_per_objfile->debug_names_table = std::move (map);
5096 dwarf2_per_objfile->using_index = 1;
5097 dwarf2_per_objfile->quick_file_names_table =
5098 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5099
5100 return true;
5101 }
5102
5103 /* Type used to manage iterating over all CUs looking for a symbol for
5104 .debug_names. */
5105
5106 class dw2_debug_names_iterator
5107 {
5108 public:
5109 dw2_debug_names_iterator (const mapped_debug_names &map,
5110 gdb::optional<block_enum> block_index,
5111 domain_enum domain,
5112 const char *name)
5113 : m_map (map), m_block_index (block_index), m_domain (domain),
5114 m_addr (find_vec_in_debug_names (map, name))
5115 {}
5116
5117 dw2_debug_names_iterator (const mapped_debug_names &map,
5118 search_domain search, uint32_t namei)
5119 : m_map (map),
5120 m_search (search),
5121 m_addr (find_vec_in_debug_names (map, namei))
5122 {}
5123
5124 dw2_debug_names_iterator (const mapped_debug_names &map,
5125 block_enum block_index, domain_enum domain,
5126 uint32_t namei)
5127 : m_map (map), m_block_index (block_index), m_domain (domain),
5128 m_addr (find_vec_in_debug_names (map, namei))
5129 {}
5130
5131 /* Return the next matching CU or NULL if there are no more. */
5132 dwarf2_per_cu_data *next ();
5133
5134 private:
5135 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5136 const char *name);
5137 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5138 uint32_t namei);
5139
5140 /* The internalized form of .debug_names. */
5141 const mapped_debug_names &m_map;
5142
5143 /* If set, only look for symbols that match that block. Valid values are
5144 GLOBAL_BLOCK and STATIC_BLOCK. */
5145 const gdb::optional<block_enum> m_block_index;
5146
5147 /* The kind of symbol we're looking for. */
5148 const domain_enum m_domain = UNDEF_DOMAIN;
5149 const search_domain m_search = ALL_DOMAIN;
5150
5151 /* The list of CUs from the index entry of the symbol, or NULL if
5152 not found. */
5153 const gdb_byte *m_addr;
5154 };
5155
5156 const char *
5157 mapped_debug_names::namei_to_name (uint32_t namei) const
5158 {
5159 const ULONGEST namei_string_offs
5160 = extract_unsigned_integer ((name_table_string_offs_reordered
5161 + namei * offset_size),
5162 offset_size,
5163 dwarf5_byte_order);
5164 return read_indirect_string_at_offset
5165 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5166 }
5167
5168 /* Find a slot in .debug_names for the object named NAME. If NAME is
5169 found, return pointer to its pool data. If NAME cannot be found,
5170 return NULL. */
5171
5172 const gdb_byte *
5173 dw2_debug_names_iterator::find_vec_in_debug_names
5174 (const mapped_debug_names &map, const char *name)
5175 {
5176 int (*cmp) (const char *, const char *);
5177
5178 gdb::unique_xmalloc_ptr<char> without_params;
5179 if (current_language->la_language == language_cplus
5180 || current_language->la_language == language_fortran
5181 || current_language->la_language == language_d)
5182 {
5183 /* NAME is already canonical. Drop any qualifiers as
5184 .debug_names does not contain any. */
5185
5186 if (strchr (name, '(') != NULL)
5187 {
5188 without_params = cp_remove_params (name);
5189 if (without_params != NULL)
5190 name = without_params.get ();
5191 }
5192 }
5193
5194 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5195
5196 const uint32_t full_hash = dwarf5_djb_hash (name);
5197 uint32_t namei
5198 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5199 (map.bucket_table_reordered
5200 + (full_hash % map.bucket_count)), 4,
5201 map.dwarf5_byte_order);
5202 if (namei == 0)
5203 return NULL;
5204 --namei;
5205 if (namei >= map.name_count)
5206 {
5207 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5208 "[in module %s]"),
5209 namei, map.name_count,
5210 objfile_name (map.dwarf2_per_objfile->objfile));
5211 return NULL;
5212 }
5213
5214 for (;;)
5215 {
5216 const uint32_t namei_full_hash
5217 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5218 (map.hash_table_reordered + namei), 4,
5219 map.dwarf5_byte_order);
5220 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5221 return NULL;
5222
5223 if (full_hash == namei_full_hash)
5224 {
5225 const char *const namei_string = map.namei_to_name (namei);
5226
5227 #if 0 /* An expensive sanity check. */
5228 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5229 {
5230 complaint (_("Wrong .debug_names hash for string at index %u "
5231 "[in module %s]"),
5232 namei, objfile_name (dwarf2_per_objfile->objfile));
5233 return NULL;
5234 }
5235 #endif
5236
5237 if (cmp (namei_string, name) == 0)
5238 {
5239 const ULONGEST namei_entry_offs
5240 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5241 + namei * map.offset_size),
5242 map.offset_size, map.dwarf5_byte_order);
5243 return map.entry_pool + namei_entry_offs;
5244 }
5245 }
5246
5247 ++namei;
5248 if (namei >= map.name_count)
5249 return NULL;
5250 }
5251 }
5252
5253 const gdb_byte *
5254 dw2_debug_names_iterator::find_vec_in_debug_names
5255 (const mapped_debug_names &map, uint32_t namei)
5256 {
5257 if (namei >= map.name_count)
5258 {
5259 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5260 "[in module %s]"),
5261 namei, map.name_count,
5262 objfile_name (map.dwarf2_per_objfile->objfile));
5263 return NULL;
5264 }
5265
5266 const ULONGEST namei_entry_offs
5267 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5268 + namei * map.offset_size),
5269 map.offset_size, map.dwarf5_byte_order);
5270 return map.entry_pool + namei_entry_offs;
5271 }
5272
5273 /* See dw2_debug_names_iterator. */
5274
5275 dwarf2_per_cu_data *
5276 dw2_debug_names_iterator::next ()
5277 {
5278 if (m_addr == NULL)
5279 return NULL;
5280
5281 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5282 struct objfile *objfile = dwarf2_per_objfile->objfile;
5283 bfd *const abfd = objfile->obfd;
5284
5285 again:
5286
5287 unsigned int bytes_read;
5288 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5289 m_addr += bytes_read;
5290 if (abbrev == 0)
5291 return NULL;
5292
5293 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5294 if (indexval_it == m_map.abbrev_map.cend ())
5295 {
5296 complaint (_("Wrong .debug_names undefined abbrev code %s "
5297 "[in module %s]"),
5298 pulongest (abbrev), objfile_name (objfile));
5299 return NULL;
5300 }
5301 const mapped_debug_names::index_val &indexval = indexval_it->second;
5302 enum class symbol_linkage {
5303 unknown,
5304 static_,
5305 extern_,
5306 } symbol_linkage_ = symbol_linkage::unknown;
5307 dwarf2_per_cu_data *per_cu = NULL;
5308 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5309 {
5310 ULONGEST ull;
5311 switch (attr.form)
5312 {
5313 case DW_FORM_implicit_const:
5314 ull = attr.implicit_const;
5315 break;
5316 case DW_FORM_flag_present:
5317 ull = 1;
5318 break;
5319 case DW_FORM_udata:
5320 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5321 m_addr += bytes_read;
5322 break;
5323 default:
5324 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5325 dwarf_form_name (attr.form),
5326 objfile_name (objfile));
5327 return NULL;
5328 }
5329 switch (attr.dw_idx)
5330 {
5331 case DW_IDX_compile_unit:
5332 /* Don't crash on bad data. */
5333 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5334 {
5335 complaint (_(".debug_names entry has bad CU index %s"
5336 " [in module %s]"),
5337 pulongest (ull),
5338 objfile_name (dwarf2_per_objfile->objfile));
5339 continue;
5340 }
5341 per_cu = dwarf2_per_objfile->get_cutu (ull);
5342 break;
5343 case DW_IDX_type_unit:
5344 /* Don't crash on bad data. */
5345 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5346 {
5347 complaint (_(".debug_names entry has bad TU index %s"
5348 " [in module %s]"),
5349 pulongest (ull),
5350 objfile_name (dwarf2_per_objfile->objfile));
5351 continue;
5352 }
5353 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5354 break;
5355 case DW_IDX_GNU_internal:
5356 if (!m_map.augmentation_is_gdb)
5357 break;
5358 symbol_linkage_ = symbol_linkage::static_;
5359 break;
5360 case DW_IDX_GNU_external:
5361 if (!m_map.augmentation_is_gdb)
5362 break;
5363 symbol_linkage_ = symbol_linkage::extern_;
5364 break;
5365 }
5366 }
5367
5368 /* Skip if already read in. */
5369 if (per_cu->v.quick->compunit_symtab)
5370 goto again;
5371
5372 /* Check static vs global. */
5373 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5374 {
5375 const bool want_static = *m_block_index == STATIC_BLOCK;
5376 const bool symbol_is_static =
5377 symbol_linkage_ == symbol_linkage::static_;
5378 if (want_static != symbol_is_static)
5379 goto again;
5380 }
5381
5382 /* Match dw2_symtab_iter_next, symbol_kind
5383 and debug_names::psymbol_tag. */
5384 switch (m_domain)
5385 {
5386 case VAR_DOMAIN:
5387 switch (indexval.dwarf_tag)
5388 {
5389 case DW_TAG_variable:
5390 case DW_TAG_subprogram:
5391 /* Some types are also in VAR_DOMAIN. */
5392 case DW_TAG_typedef:
5393 case DW_TAG_structure_type:
5394 break;
5395 default:
5396 goto again;
5397 }
5398 break;
5399 case STRUCT_DOMAIN:
5400 switch (indexval.dwarf_tag)
5401 {
5402 case DW_TAG_typedef:
5403 case DW_TAG_structure_type:
5404 break;
5405 default:
5406 goto again;
5407 }
5408 break;
5409 case LABEL_DOMAIN:
5410 switch (indexval.dwarf_tag)
5411 {
5412 case 0:
5413 case DW_TAG_variable:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
5419 case MODULE_DOMAIN:
5420 switch (indexval.dwarf_tag)
5421 {
5422 case DW_TAG_module:
5423 break;
5424 default:
5425 goto again;
5426 }
5427 break;
5428 default:
5429 break;
5430 }
5431
5432 /* Match dw2_expand_symtabs_matching, symbol_kind and
5433 debug_names::psymbol_tag. */
5434 switch (m_search)
5435 {
5436 case VARIABLES_DOMAIN:
5437 switch (indexval.dwarf_tag)
5438 {
5439 case DW_TAG_variable:
5440 break;
5441 default:
5442 goto again;
5443 }
5444 break;
5445 case FUNCTIONS_DOMAIN:
5446 switch (indexval.dwarf_tag)
5447 {
5448 case DW_TAG_subprogram:
5449 break;
5450 default:
5451 goto again;
5452 }
5453 break;
5454 case TYPES_DOMAIN:
5455 switch (indexval.dwarf_tag)
5456 {
5457 case DW_TAG_typedef:
5458 case DW_TAG_structure_type:
5459 break;
5460 default:
5461 goto again;
5462 }
5463 break;
5464 case MODULES_DOMAIN:
5465 switch (indexval.dwarf_tag)
5466 {
5467 case DW_TAG_module:
5468 break;
5469 default:
5470 goto again;
5471 }
5472 default:
5473 break;
5474 }
5475
5476 return per_cu;
5477 }
5478
5479 static struct compunit_symtab *
5480 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5481 const char *name, domain_enum domain)
5482 {
5483 struct dwarf2_per_objfile *dwarf2_per_objfile
5484 = get_dwarf2_per_objfile (objfile);
5485
5486 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5487 if (!mapp)
5488 {
5489 /* index is NULL if OBJF_READNOW. */
5490 return NULL;
5491 }
5492 const auto &map = *mapp;
5493
5494 dw2_debug_names_iterator iter (map, block_index, domain, name);
5495
5496 struct compunit_symtab *stab_best = NULL;
5497 struct dwarf2_per_cu_data *per_cu;
5498 while ((per_cu = iter.next ()) != NULL)
5499 {
5500 struct symbol *sym, *with_opaque = NULL;
5501 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5502 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5503 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5504
5505 sym = block_find_symbol (block, name, domain,
5506 block_find_non_opaque_type_preferred,
5507 &with_opaque);
5508
5509 /* Some caution must be observed with overloaded functions and
5510 methods, since the index will not contain any overload
5511 information (but NAME might contain it). */
5512
5513 if (sym != NULL
5514 && strcmp_iw (sym->search_name (), name) == 0)
5515 return stab;
5516 if (with_opaque != NULL
5517 && strcmp_iw (with_opaque->search_name (), name) == 0)
5518 stab_best = stab;
5519
5520 /* Keep looking through other CUs. */
5521 }
5522
5523 return stab_best;
5524 }
5525
5526 /* This dumps minimal information about .debug_names. It is called
5527 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5528 uses this to verify that .debug_names has been loaded. */
5529
5530 static void
5531 dw2_debug_names_dump (struct objfile *objfile)
5532 {
5533 struct dwarf2_per_objfile *dwarf2_per_objfile
5534 = get_dwarf2_per_objfile (objfile);
5535
5536 gdb_assert (dwarf2_per_objfile->using_index);
5537 printf_filtered (".debug_names:");
5538 if (dwarf2_per_objfile->debug_names_table)
5539 printf_filtered (" exists\n");
5540 else
5541 printf_filtered (" faked for \"readnow\"\n");
5542 printf_filtered ("\n");
5543 }
5544
5545 static void
5546 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5547 const char *func_name)
5548 {
5549 struct dwarf2_per_objfile *dwarf2_per_objfile
5550 = get_dwarf2_per_objfile (objfile);
5551
5552 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5553 if (dwarf2_per_objfile->debug_names_table)
5554 {
5555 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5556
5557 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5558
5559 struct dwarf2_per_cu_data *per_cu;
5560 while ((per_cu = iter.next ()) != NULL)
5561 dw2_instantiate_symtab (per_cu, false);
5562 }
5563 }
5564
5565 static void
5566 dw2_debug_names_map_matching_symbols
5567 (struct objfile *objfile,
5568 const lookup_name_info &name, domain_enum domain,
5569 int global,
5570 gdb::function_view<symbol_found_callback_ftype> callback,
5571 symbol_compare_ftype *ordered_compare)
5572 {
5573 struct dwarf2_per_objfile *dwarf2_per_objfile
5574 = get_dwarf2_per_objfile (objfile);
5575
5576 /* debug_names_table is NULL if OBJF_READNOW. */
5577 if (!dwarf2_per_objfile->debug_names_table)
5578 return;
5579
5580 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5581 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5582
5583 const char *match_name = name.ada ().lookup_name ().c_str ();
5584 auto matcher = [&] (const char *symname)
5585 {
5586 if (ordered_compare == nullptr)
5587 return true;
5588 return ordered_compare (symname, match_name) == 0;
5589 };
5590
5591 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5592 [&] (offset_type namei)
5593 {
5594 /* The name was matched, now expand corresponding CUs that were
5595 marked. */
5596 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5597
5598 struct dwarf2_per_cu_data *per_cu;
5599 while ((per_cu = iter.next ()) != NULL)
5600 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5601 return true;
5602 });
5603
5604 /* It's a shame we couldn't do this inside the
5605 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5606 that have already been expanded. Instead, this loop matches what
5607 the psymtab code does. */
5608 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5609 {
5610 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5611 if (cust != nullptr)
5612 {
5613 const struct block *block
5614 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5615 if (!iterate_over_symbols_terminated (block, name,
5616 domain, callback))
5617 break;
5618 }
5619 }
5620 }
5621
5622 static void
5623 dw2_debug_names_expand_symtabs_matching
5624 (struct objfile *objfile,
5625 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5626 const lookup_name_info &lookup_name,
5627 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5628 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5629 enum search_domain kind)
5630 {
5631 struct dwarf2_per_objfile *dwarf2_per_objfile
5632 = get_dwarf2_per_objfile (objfile);
5633
5634 /* debug_names_table is NULL if OBJF_READNOW. */
5635 if (!dwarf2_per_objfile->debug_names_table)
5636 return;
5637
5638 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5639
5640 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5641
5642 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5643 symbol_matcher,
5644 kind, [&] (offset_type namei)
5645 {
5646 /* The name was matched, now expand corresponding CUs that were
5647 marked. */
5648 dw2_debug_names_iterator iter (map, kind, namei);
5649
5650 struct dwarf2_per_cu_data *per_cu;
5651 while ((per_cu = iter.next ()) != NULL)
5652 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5653 expansion_notify);
5654 return true;
5655 });
5656 }
5657
5658 const struct quick_symbol_functions dwarf2_debug_names_functions =
5659 {
5660 dw2_has_symbols,
5661 dw2_find_last_source_symtab,
5662 dw2_forget_cached_source_info,
5663 dw2_map_symtabs_matching_filename,
5664 dw2_debug_names_lookup_symbol,
5665 dw2_print_stats,
5666 dw2_debug_names_dump,
5667 dw2_debug_names_expand_symtabs_for_function,
5668 dw2_expand_all_symtabs,
5669 dw2_expand_symtabs_with_fullname,
5670 dw2_debug_names_map_matching_symbols,
5671 dw2_debug_names_expand_symtabs_matching,
5672 dw2_find_pc_sect_compunit_symtab,
5673 NULL,
5674 dw2_map_symbol_filenames
5675 };
5676
5677 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5678 to either a dwarf2_per_objfile or dwz_file object. */
5679
5680 template <typename T>
5681 static gdb::array_view<const gdb_byte>
5682 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5683 {
5684 dwarf2_section_info *section = &section_owner->gdb_index;
5685
5686 if (section->empty ())
5687 return {};
5688
5689 /* Older elfutils strip versions could keep the section in the main
5690 executable while splitting it for the separate debug info file. */
5691 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5692 return {};
5693
5694 section->read (obj);
5695
5696 /* dwarf2_section_info::size is a bfd_size_type, while
5697 gdb::array_view works with size_t. On 32-bit hosts, with
5698 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5699 is 32-bit. So we need an explicit narrowing conversion here.
5700 This is fine, because it's impossible to allocate or mmap an
5701 array/buffer larger than what size_t can represent. */
5702 return gdb::make_array_view (section->buffer, section->size);
5703 }
5704
5705 /* Lookup the index cache for the contents of the index associated to
5706 DWARF2_OBJ. */
5707
5708 static gdb::array_view<const gdb_byte>
5709 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5710 {
5711 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5712 if (build_id == nullptr)
5713 return {};
5714
5715 return global_index_cache.lookup_gdb_index (build_id,
5716 &dwarf2_obj->index_cache_res);
5717 }
5718
5719 /* Same as the above, but for DWZ. */
5720
5721 static gdb::array_view<const gdb_byte>
5722 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5723 {
5724 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5725 if (build_id == nullptr)
5726 return {};
5727
5728 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5729 }
5730
5731 /* See symfile.h. */
5732
5733 bool
5734 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5735 {
5736 struct dwarf2_per_objfile *dwarf2_per_objfile
5737 = get_dwarf2_per_objfile (objfile);
5738
5739 /* If we're about to read full symbols, don't bother with the
5740 indices. In this case we also don't care if some other debug
5741 format is making psymtabs, because they are all about to be
5742 expanded anyway. */
5743 if ((objfile->flags & OBJF_READNOW))
5744 {
5745 dwarf2_per_objfile->using_index = 1;
5746 create_all_comp_units (dwarf2_per_objfile);
5747 create_all_type_units (dwarf2_per_objfile);
5748 dwarf2_per_objfile->quick_file_names_table
5749 = create_quick_file_names_table
5750 (dwarf2_per_objfile->all_comp_units.size ());
5751
5752 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5753 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5754 {
5755 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5756
5757 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5758 struct dwarf2_per_cu_quick_data);
5759 }
5760
5761 /* Return 1 so that gdb sees the "quick" functions. However,
5762 these functions will be no-ops because we will have expanded
5763 all symtabs. */
5764 *index_kind = dw_index_kind::GDB_INDEX;
5765 return true;
5766 }
5767
5768 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5769 {
5770 *index_kind = dw_index_kind::DEBUG_NAMES;
5771 return true;
5772 }
5773
5774 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5775 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5776 get_gdb_index_contents_from_section<dwz_file>))
5777 {
5778 *index_kind = dw_index_kind::GDB_INDEX;
5779 return true;
5780 }
5781
5782 /* ... otherwise, try to find the index in the index cache. */
5783 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5784 get_gdb_index_contents_from_cache,
5785 get_gdb_index_contents_from_cache_dwz))
5786 {
5787 global_index_cache.hit ();
5788 *index_kind = dw_index_kind::GDB_INDEX;
5789 return true;
5790 }
5791
5792 global_index_cache.miss ();
5793 return false;
5794 }
5795
5796 \f
5797
5798 /* Build a partial symbol table. */
5799
5800 void
5801 dwarf2_build_psymtabs (struct objfile *objfile)
5802 {
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
5805
5806 init_psymbol_list (objfile, 1024);
5807
5808 try
5809 {
5810 /* This isn't really ideal: all the data we allocate on the
5811 objfile's obstack is still uselessly kept around. However,
5812 freeing it seems unsafe. */
5813 psymtab_discarder psymtabs (objfile);
5814 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5815 psymtabs.keep ();
5816
5817 /* (maybe) store an index in the cache. */
5818 global_index_cache.store (dwarf2_per_objfile);
5819 }
5820 catch (const gdb_exception_error &except)
5821 {
5822 exception_print (gdb_stderr, except);
5823 }
5824 }
5825
5826 /* Find the base address of the compilation unit for range lists and
5827 location lists. It will normally be specified by DW_AT_low_pc.
5828 In DWARF-3 draft 4, the base address could be overridden by
5829 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5830 compilation units with discontinuous ranges. */
5831
5832 static void
5833 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5834 {
5835 struct attribute *attr;
5836
5837 cu->base_known = 0;
5838 cu->base_address = 0;
5839
5840 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5841 if (attr != nullptr)
5842 {
5843 cu->base_address = attr->value_as_address ();
5844 cu->base_known = 1;
5845 }
5846 else
5847 {
5848 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5849 if (attr != nullptr)
5850 {
5851 cu->base_address = attr->value_as_address ();
5852 cu->base_known = 1;
5853 }
5854 }
5855 }
5856
5857 /* Helper function that returns the proper abbrev section for
5858 THIS_CU. */
5859
5860 static struct dwarf2_section_info *
5861 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5862 {
5863 struct dwarf2_section_info *abbrev;
5864 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5865
5866 if (this_cu->is_dwz)
5867 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5868 else
5869 abbrev = &dwarf2_per_objfile->abbrev;
5870
5871 return abbrev;
5872 }
5873
5874 /* Fetch the abbreviation table offset from a comp or type unit header. */
5875
5876 static sect_offset
5877 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5878 struct dwarf2_section_info *section,
5879 sect_offset sect_off)
5880 {
5881 bfd *abfd = section->get_bfd_owner ();
5882 const gdb_byte *info_ptr;
5883 unsigned int initial_length_size, offset_size;
5884 uint16_t version;
5885
5886 section->read (dwarf2_per_objfile->objfile);
5887 info_ptr = section->buffer + to_underlying (sect_off);
5888 read_initial_length (abfd, info_ptr, &initial_length_size);
5889 offset_size = initial_length_size == 4 ? 4 : 8;
5890 info_ptr += initial_length_size;
5891
5892 version = read_2_bytes (abfd, info_ptr);
5893 info_ptr += 2;
5894 if (version >= 5)
5895 {
5896 /* Skip unit type and address size. */
5897 info_ptr += 2;
5898 }
5899
5900 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5901 }
5902
5903 /* Allocate a new partial symtab for file named NAME and mark this new
5904 partial symtab as being an include of PST. */
5905
5906 static void
5907 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5908 struct objfile *objfile)
5909 {
5910 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5911
5912 if (!IS_ABSOLUTE_PATH (subpst->filename))
5913 {
5914 /* It shares objfile->objfile_obstack. */
5915 subpst->dirname = pst->dirname;
5916 }
5917
5918 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5919 subpst->dependencies[0] = pst;
5920 subpst->number_of_dependencies = 1;
5921
5922 /* No private part is necessary for include psymtabs. This property
5923 can be used to differentiate between such include psymtabs and
5924 the regular ones. */
5925 subpst->per_cu_data = nullptr;
5926 }
5927
5928 /* Read the Line Number Program data and extract the list of files
5929 included by the source file represented by PST. Build an include
5930 partial symtab for each of these included files. */
5931
5932 static void
5933 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5934 struct die_info *die,
5935 dwarf2_psymtab *pst)
5936 {
5937 line_header_up lh;
5938 struct attribute *attr;
5939
5940 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5941 if (attr != nullptr)
5942 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5943 if (lh == NULL)
5944 return; /* No linetable, so no includes. */
5945
5946 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5947 that we pass in the raw text_low here; that is ok because we're
5948 only decoding the line table to make include partial symtabs, and
5949 so the addresses aren't really used. */
5950 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5951 pst->raw_text_low (), 1);
5952 }
5953
5954 static hashval_t
5955 hash_signatured_type (const void *item)
5956 {
5957 const struct signatured_type *sig_type
5958 = (const struct signatured_type *) item;
5959
5960 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5961 return sig_type->signature;
5962 }
5963
5964 static int
5965 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5966 {
5967 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5968 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5969
5970 return lhs->signature == rhs->signature;
5971 }
5972
5973 /* Allocate a hash table for signatured types. */
5974
5975 static htab_up
5976 allocate_signatured_type_table (struct objfile *objfile)
5977 {
5978 return htab_up (htab_create_alloc (41,
5979 hash_signatured_type,
5980 eq_signatured_type,
5981 NULL, xcalloc, xfree));
5982 }
5983
5984 /* A helper function to add a signatured type CU to a table. */
5985
5986 static int
5987 add_signatured_type_cu_to_table (void **slot, void *datum)
5988 {
5989 struct signatured_type *sigt = (struct signatured_type *) *slot;
5990 std::vector<signatured_type *> *all_type_units
5991 = (std::vector<signatured_type *> *) datum;
5992
5993 all_type_units->push_back (sigt);
5994
5995 return 1;
5996 }
5997
5998 /* A helper for create_debug_types_hash_table. Read types from SECTION
5999 and fill them into TYPES_HTAB. It will process only type units,
6000 therefore DW_UT_type. */
6001
6002 static void
6003 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6004 struct dwo_file *dwo_file,
6005 dwarf2_section_info *section, htab_up &types_htab,
6006 rcuh_kind section_kind)
6007 {
6008 struct objfile *objfile = dwarf2_per_objfile->objfile;
6009 struct dwarf2_section_info *abbrev_section;
6010 bfd *abfd;
6011 const gdb_byte *info_ptr, *end_ptr;
6012
6013 abbrev_section = (dwo_file != NULL
6014 ? &dwo_file->sections.abbrev
6015 : &dwarf2_per_objfile->abbrev);
6016
6017 if (dwarf_read_debug)
6018 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6019 section->get_name (),
6020 abbrev_section->get_file_name ());
6021
6022 section->read (objfile);
6023 info_ptr = section->buffer;
6024
6025 if (info_ptr == NULL)
6026 return;
6027
6028 /* We can't set abfd until now because the section may be empty or
6029 not present, in which case the bfd is unknown. */
6030 abfd = section->get_bfd_owner ();
6031
6032 /* We don't use cutu_reader here because we don't need to read
6033 any dies: the signature is in the header. */
6034
6035 end_ptr = info_ptr + section->size;
6036 while (info_ptr < end_ptr)
6037 {
6038 struct signatured_type *sig_type;
6039 struct dwo_unit *dwo_tu;
6040 void **slot;
6041 const gdb_byte *ptr = info_ptr;
6042 struct comp_unit_head header;
6043 unsigned int length;
6044
6045 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6046
6047 /* Initialize it due to a false compiler warning. */
6048 header.signature = -1;
6049 header.type_cu_offset_in_tu = (cu_offset) -1;
6050
6051 /* We need to read the type's signature in order to build the hash
6052 table, but we don't need anything else just yet. */
6053
6054 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6055 abbrev_section, ptr, section_kind);
6056
6057 length = header.get_length ();
6058
6059 /* Skip dummy type units. */
6060 if (ptr >= info_ptr + length
6061 || peek_abbrev_code (abfd, ptr) == 0
6062 || header.unit_type != DW_UT_type)
6063 {
6064 info_ptr += length;
6065 continue;
6066 }
6067
6068 if (types_htab == NULL)
6069 {
6070 if (dwo_file)
6071 types_htab = allocate_dwo_unit_table (objfile);
6072 else
6073 types_htab = allocate_signatured_type_table (objfile);
6074 }
6075
6076 if (dwo_file)
6077 {
6078 sig_type = NULL;
6079 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6080 struct dwo_unit);
6081 dwo_tu->dwo_file = dwo_file;
6082 dwo_tu->signature = header.signature;
6083 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6084 dwo_tu->section = section;
6085 dwo_tu->sect_off = sect_off;
6086 dwo_tu->length = length;
6087 }
6088 else
6089 {
6090 /* N.B.: type_offset is not usable if this type uses a DWO file.
6091 The real type_offset is in the DWO file. */
6092 dwo_tu = NULL;
6093 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6094 struct signatured_type);
6095 sig_type->signature = header.signature;
6096 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6097 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6098 sig_type->per_cu.is_debug_types = 1;
6099 sig_type->per_cu.section = section;
6100 sig_type->per_cu.sect_off = sect_off;
6101 sig_type->per_cu.length = length;
6102 }
6103
6104 slot = htab_find_slot (types_htab.get (),
6105 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6106 INSERT);
6107 gdb_assert (slot != NULL);
6108 if (*slot != NULL)
6109 {
6110 sect_offset dup_sect_off;
6111
6112 if (dwo_file)
6113 {
6114 const struct dwo_unit *dup_tu
6115 = (const struct dwo_unit *) *slot;
6116
6117 dup_sect_off = dup_tu->sect_off;
6118 }
6119 else
6120 {
6121 const struct signatured_type *dup_tu
6122 = (const struct signatured_type *) *slot;
6123
6124 dup_sect_off = dup_tu->per_cu.sect_off;
6125 }
6126
6127 complaint (_("debug type entry at offset %s is duplicate to"
6128 " the entry at offset %s, signature %s"),
6129 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6130 hex_string (header.signature));
6131 }
6132 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6133
6134 if (dwarf_read_debug > 1)
6135 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6136 sect_offset_str (sect_off),
6137 hex_string (header.signature));
6138
6139 info_ptr += length;
6140 }
6141 }
6142
6143 /* Create the hash table of all entries in the .debug_types
6144 (or .debug_types.dwo) section(s).
6145 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6146 otherwise it is NULL.
6147
6148 The result is a pointer to the hash table or NULL if there are no types.
6149
6150 Note: This function processes DWO files only, not DWP files. */
6151
6152 static void
6153 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6154 struct dwo_file *dwo_file,
6155 gdb::array_view<dwarf2_section_info> type_sections,
6156 htab_up &types_htab)
6157 {
6158 for (dwarf2_section_info &section : type_sections)
6159 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6160 types_htab, rcuh_kind::TYPE);
6161 }
6162
6163 /* Create the hash table of all entries in the .debug_types section,
6164 and initialize all_type_units.
6165 The result is zero if there is an error (e.g. missing .debug_types section),
6166 otherwise non-zero. */
6167
6168 static int
6169 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6170 {
6171 htab_up types_htab;
6172
6173 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6174 &dwarf2_per_objfile->info, types_htab,
6175 rcuh_kind::COMPILE);
6176 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6177 dwarf2_per_objfile->types, types_htab);
6178 if (types_htab == NULL)
6179 {
6180 dwarf2_per_objfile->signatured_types = NULL;
6181 return 0;
6182 }
6183
6184 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6185
6186 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6187 dwarf2_per_objfile->all_type_units.reserve
6188 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6189
6190 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6191 add_signatured_type_cu_to_table,
6192 &dwarf2_per_objfile->all_type_units);
6193
6194 return 1;
6195 }
6196
6197 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6198 If SLOT is non-NULL, it is the entry to use in the hash table.
6199 Otherwise we find one. */
6200
6201 static struct signatured_type *
6202 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6203 void **slot)
6204 {
6205 struct objfile *objfile = dwarf2_per_objfile->objfile;
6206
6207 if (dwarf2_per_objfile->all_type_units.size ()
6208 == dwarf2_per_objfile->all_type_units.capacity ())
6209 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6210
6211 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6212 struct signatured_type);
6213
6214 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6215 sig_type->signature = sig;
6216 sig_type->per_cu.is_debug_types = 1;
6217 if (dwarf2_per_objfile->using_index)
6218 {
6219 sig_type->per_cu.v.quick =
6220 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6221 struct dwarf2_per_cu_quick_data);
6222 }
6223
6224 if (slot == NULL)
6225 {
6226 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6227 sig_type, INSERT);
6228 }
6229 gdb_assert (*slot == NULL);
6230 *slot = sig_type;
6231 /* The rest of sig_type must be filled in by the caller. */
6232 return sig_type;
6233 }
6234
6235 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6236 Fill in SIG_ENTRY with DWO_ENTRY. */
6237
6238 static void
6239 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6240 struct signatured_type *sig_entry,
6241 struct dwo_unit *dwo_entry)
6242 {
6243 /* Make sure we're not clobbering something we don't expect to. */
6244 gdb_assert (! sig_entry->per_cu.queued);
6245 gdb_assert (sig_entry->per_cu.cu == NULL);
6246 if (dwarf2_per_objfile->using_index)
6247 {
6248 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6249 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6250 }
6251 else
6252 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6253 gdb_assert (sig_entry->signature == dwo_entry->signature);
6254 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6255 gdb_assert (sig_entry->type_unit_group == NULL);
6256 gdb_assert (sig_entry->dwo_unit == NULL);
6257
6258 sig_entry->per_cu.section = dwo_entry->section;
6259 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6260 sig_entry->per_cu.length = dwo_entry->length;
6261 sig_entry->per_cu.reading_dwo_directly = 1;
6262 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6263 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6264 sig_entry->dwo_unit = dwo_entry;
6265 }
6266
6267 /* Subroutine of lookup_signatured_type.
6268 If we haven't read the TU yet, create the signatured_type data structure
6269 for a TU to be read in directly from a DWO file, bypassing the stub.
6270 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6271 using .gdb_index, then when reading a CU we want to stay in the DWO file
6272 containing that CU. Otherwise we could end up reading several other DWO
6273 files (due to comdat folding) to process the transitive closure of all the
6274 mentioned TUs, and that can be slow. The current DWO file will have every
6275 type signature that it needs.
6276 We only do this for .gdb_index because in the psymtab case we already have
6277 to read all the DWOs to build the type unit groups. */
6278
6279 static struct signatured_type *
6280 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6281 {
6282 struct dwarf2_per_objfile *dwarf2_per_objfile
6283 = cu->per_cu->dwarf2_per_objfile;
6284 struct objfile *objfile = dwarf2_per_objfile->objfile;
6285 struct dwo_file *dwo_file;
6286 struct dwo_unit find_dwo_entry, *dwo_entry;
6287 struct signatured_type find_sig_entry, *sig_entry;
6288 void **slot;
6289
6290 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6291
6292 /* If TU skeletons have been removed then we may not have read in any
6293 TUs yet. */
6294 if (dwarf2_per_objfile->signatured_types == NULL)
6295 {
6296 dwarf2_per_objfile->signatured_types
6297 = allocate_signatured_type_table (objfile);
6298 }
6299
6300 /* We only ever need to read in one copy of a signatured type.
6301 Use the global signatured_types array to do our own comdat-folding
6302 of types. If this is the first time we're reading this TU, and
6303 the TU has an entry in .gdb_index, replace the recorded data from
6304 .gdb_index with this TU. */
6305
6306 find_sig_entry.signature = sig;
6307 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6308 &find_sig_entry, INSERT);
6309 sig_entry = (struct signatured_type *) *slot;
6310
6311 /* We can get here with the TU already read, *or* in the process of being
6312 read. Don't reassign the global entry to point to this DWO if that's
6313 the case. Also note that if the TU is already being read, it may not
6314 have come from a DWO, the program may be a mix of Fission-compiled
6315 code and non-Fission-compiled code. */
6316
6317 /* Have we already tried to read this TU?
6318 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6319 needn't exist in the global table yet). */
6320 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6321 return sig_entry;
6322
6323 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6324 dwo_unit of the TU itself. */
6325 dwo_file = cu->dwo_unit->dwo_file;
6326
6327 /* Ok, this is the first time we're reading this TU. */
6328 if (dwo_file->tus == NULL)
6329 return NULL;
6330 find_dwo_entry.signature = sig;
6331 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6332 &find_dwo_entry);
6333 if (dwo_entry == NULL)
6334 return NULL;
6335
6336 /* If the global table doesn't have an entry for this TU, add one. */
6337 if (sig_entry == NULL)
6338 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6339
6340 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6341 sig_entry->per_cu.tu_read = 1;
6342 return sig_entry;
6343 }
6344
6345 /* Subroutine of lookup_signatured_type.
6346 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6347 then try the DWP file. If the TU stub (skeleton) has been removed then
6348 it won't be in .gdb_index. */
6349
6350 static struct signatured_type *
6351 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6352 {
6353 struct dwarf2_per_objfile *dwarf2_per_objfile
6354 = cu->per_cu->dwarf2_per_objfile;
6355 struct objfile *objfile = dwarf2_per_objfile->objfile;
6356 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6357 struct dwo_unit *dwo_entry;
6358 struct signatured_type find_sig_entry, *sig_entry;
6359 void **slot;
6360
6361 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6362 gdb_assert (dwp_file != NULL);
6363
6364 /* If TU skeletons have been removed then we may not have read in any
6365 TUs yet. */
6366 if (dwarf2_per_objfile->signatured_types == NULL)
6367 {
6368 dwarf2_per_objfile->signatured_types
6369 = allocate_signatured_type_table (objfile);
6370 }
6371
6372 find_sig_entry.signature = sig;
6373 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6374 &find_sig_entry, INSERT);
6375 sig_entry = (struct signatured_type *) *slot;
6376
6377 /* Have we already tried to read this TU?
6378 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6379 needn't exist in the global table yet). */
6380 if (sig_entry != NULL)
6381 return sig_entry;
6382
6383 if (dwp_file->tus == NULL)
6384 return NULL;
6385 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6386 sig, 1 /* is_debug_types */);
6387 if (dwo_entry == NULL)
6388 return NULL;
6389
6390 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6391 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6392
6393 return sig_entry;
6394 }
6395
6396 /* Lookup a signature based type for DW_FORM_ref_sig8.
6397 Returns NULL if signature SIG is not present in the table.
6398 It is up to the caller to complain about this. */
6399
6400 static struct signatured_type *
6401 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6402 {
6403 struct dwarf2_per_objfile *dwarf2_per_objfile
6404 = cu->per_cu->dwarf2_per_objfile;
6405
6406 if (cu->dwo_unit
6407 && dwarf2_per_objfile->using_index)
6408 {
6409 /* We're in a DWO/DWP file, and we're using .gdb_index.
6410 These cases require special processing. */
6411 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6412 return lookup_dwo_signatured_type (cu, sig);
6413 else
6414 return lookup_dwp_signatured_type (cu, sig);
6415 }
6416 else
6417 {
6418 struct signatured_type find_entry, *entry;
6419
6420 if (dwarf2_per_objfile->signatured_types == NULL)
6421 return NULL;
6422 find_entry.signature = sig;
6423 entry = ((struct signatured_type *)
6424 htab_find (dwarf2_per_objfile->signatured_types.get (),
6425 &find_entry));
6426 return entry;
6427 }
6428 }
6429
6430 /* Return the address base of the compile unit, which, if exists, is stored
6431 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6432 static gdb::optional<ULONGEST>
6433 lookup_addr_base (struct die_info *comp_unit_die)
6434 {
6435 struct attribute *attr;
6436 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6437 if (attr == nullptr)
6438 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6439 if (attr == nullptr)
6440 return gdb::optional<ULONGEST> ();
6441 return DW_UNSND (attr);
6442 }
6443
6444 /* Return range lists base of the compile unit, which, if exists, is stored
6445 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6446 static ULONGEST
6447 lookup_ranges_base (struct die_info *comp_unit_die)
6448 {
6449 struct attribute *attr;
6450 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6451 if (attr == nullptr)
6452 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6453 if (attr == nullptr)
6454 return 0;
6455 return DW_UNSND (attr);
6456 }
6457
6458 /* Low level DIE reading support. */
6459
6460 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6461
6462 static void
6463 init_cu_die_reader (struct die_reader_specs *reader,
6464 struct dwarf2_cu *cu,
6465 struct dwarf2_section_info *section,
6466 struct dwo_file *dwo_file,
6467 struct abbrev_table *abbrev_table)
6468 {
6469 gdb_assert (section->readin && section->buffer != NULL);
6470 reader->abfd = section->get_bfd_owner ();
6471 reader->cu = cu;
6472 reader->dwo_file = dwo_file;
6473 reader->die_section = section;
6474 reader->buffer = section->buffer;
6475 reader->buffer_end = section->buffer + section->size;
6476 reader->abbrev_table = abbrev_table;
6477 }
6478
6479 /* Subroutine of cutu_reader to simplify it.
6480 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6481 There's just a lot of work to do, and cutu_reader is big enough
6482 already.
6483
6484 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6485 from it to the DIE in the DWO. If NULL we are skipping the stub.
6486 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6487 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6488 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6489 STUB_COMP_DIR may be non-NULL.
6490 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6491 are filled in with the info of the DIE from the DWO file.
6492 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6493 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6494 kept around for at least as long as *RESULT_READER.
6495
6496 The result is non-zero if a valid (non-dummy) DIE was found. */
6497
6498 static int
6499 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6500 struct dwo_unit *dwo_unit,
6501 struct die_info *stub_comp_unit_die,
6502 const char *stub_comp_dir,
6503 struct die_reader_specs *result_reader,
6504 const gdb_byte **result_info_ptr,
6505 struct die_info **result_comp_unit_die,
6506 abbrev_table_up *result_dwo_abbrev_table)
6507 {
6508 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6509 struct objfile *objfile = dwarf2_per_objfile->objfile;
6510 struct dwarf2_cu *cu = this_cu->cu;
6511 bfd *abfd;
6512 const gdb_byte *begin_info_ptr, *info_ptr;
6513 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6514 int i,num_extra_attrs;
6515 struct dwarf2_section_info *dwo_abbrev_section;
6516 struct die_info *comp_unit_die;
6517
6518 /* At most one of these may be provided. */
6519 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6520
6521 /* These attributes aren't processed until later:
6522 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6523 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6524 referenced later. However, these attributes are found in the stub
6525 which we won't have later. In order to not impose this complication
6526 on the rest of the code, we read them here and copy them to the
6527 DWO CU/TU die. */
6528
6529 stmt_list = NULL;
6530 low_pc = NULL;
6531 high_pc = NULL;
6532 ranges = NULL;
6533 comp_dir = NULL;
6534
6535 if (stub_comp_unit_die != NULL)
6536 {
6537 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6538 DWO file. */
6539 if (! this_cu->is_debug_types)
6540 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6541 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6542 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6543 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6544 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6545
6546 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6547
6548 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6549 here (if needed). We need the value before we can process
6550 DW_AT_ranges. */
6551 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6552 }
6553 else if (stub_comp_dir != NULL)
6554 {
6555 /* Reconstruct the comp_dir attribute to simplify the code below. */
6556 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6557 comp_dir->name = DW_AT_comp_dir;
6558 comp_dir->form = DW_FORM_string;
6559 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6560 DW_STRING (comp_dir) = stub_comp_dir;
6561 }
6562
6563 /* Set up for reading the DWO CU/TU. */
6564 cu->dwo_unit = dwo_unit;
6565 dwarf2_section_info *section = dwo_unit->section;
6566 section->read (objfile);
6567 abfd = section->get_bfd_owner ();
6568 begin_info_ptr = info_ptr = (section->buffer
6569 + to_underlying (dwo_unit->sect_off));
6570 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6571
6572 if (this_cu->is_debug_types)
6573 {
6574 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6575
6576 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6577 &cu->header, section,
6578 dwo_abbrev_section,
6579 info_ptr, rcuh_kind::TYPE);
6580 /* This is not an assert because it can be caused by bad debug info. */
6581 if (sig_type->signature != cu->header.signature)
6582 {
6583 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6584 " TU at offset %s [in module %s]"),
6585 hex_string (sig_type->signature),
6586 hex_string (cu->header.signature),
6587 sect_offset_str (dwo_unit->sect_off),
6588 bfd_get_filename (abfd));
6589 }
6590 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6591 /* For DWOs coming from DWP files, we don't know the CU length
6592 nor the type's offset in the TU until now. */
6593 dwo_unit->length = cu->header.get_length ();
6594 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6595
6596 /* Establish the type offset that can be used to lookup the type.
6597 For DWO files, we don't know it until now. */
6598 sig_type->type_offset_in_section
6599 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6600 }
6601 else
6602 {
6603 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6604 &cu->header, section,
6605 dwo_abbrev_section,
6606 info_ptr, rcuh_kind::COMPILE);
6607 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6608 /* For DWOs coming from DWP files, we don't know the CU length
6609 until now. */
6610 dwo_unit->length = cu->header.get_length ();
6611 }
6612
6613 *result_dwo_abbrev_table
6614 = abbrev_table::read (objfile, dwo_abbrev_section,
6615 cu->header.abbrev_sect_off);
6616 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6617 result_dwo_abbrev_table->get ());
6618
6619 /* Read in the die, but leave space to copy over the attributes
6620 from the stub. This has the benefit of simplifying the rest of
6621 the code - all the work to maintain the illusion of a single
6622 DW_TAG_{compile,type}_unit DIE is done here. */
6623 num_extra_attrs = ((stmt_list != NULL)
6624 + (low_pc != NULL)
6625 + (high_pc != NULL)
6626 + (ranges != NULL)
6627 + (comp_dir != NULL));
6628 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6629 num_extra_attrs);
6630
6631 /* Copy over the attributes from the stub to the DIE we just read in. */
6632 comp_unit_die = *result_comp_unit_die;
6633 i = comp_unit_die->num_attrs;
6634 if (stmt_list != NULL)
6635 comp_unit_die->attrs[i++] = *stmt_list;
6636 if (low_pc != NULL)
6637 comp_unit_die->attrs[i++] = *low_pc;
6638 if (high_pc != NULL)
6639 comp_unit_die->attrs[i++] = *high_pc;
6640 if (ranges != NULL)
6641 comp_unit_die->attrs[i++] = *ranges;
6642 if (comp_dir != NULL)
6643 comp_unit_die->attrs[i++] = *comp_dir;
6644 comp_unit_die->num_attrs += num_extra_attrs;
6645
6646 if (dwarf_die_debug)
6647 {
6648 fprintf_unfiltered (gdb_stdlog,
6649 "Read die from %s@0x%x of %s:\n",
6650 section->get_name (),
6651 (unsigned) (begin_info_ptr - section->buffer),
6652 bfd_get_filename (abfd));
6653 dump_die (comp_unit_die, dwarf_die_debug);
6654 }
6655
6656 /* Skip dummy compilation units. */
6657 if (info_ptr >= begin_info_ptr + dwo_unit->length
6658 || peek_abbrev_code (abfd, info_ptr) == 0)
6659 return 0;
6660
6661 *result_info_ptr = info_ptr;
6662 return 1;
6663 }
6664
6665 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6666 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6667 signature is part of the header. */
6668 static gdb::optional<ULONGEST>
6669 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6670 {
6671 if (cu->header.version >= 5)
6672 return cu->header.signature;
6673 struct attribute *attr;
6674 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6675 if (attr == nullptr)
6676 return gdb::optional<ULONGEST> ();
6677 return DW_UNSND (attr);
6678 }
6679
6680 /* Subroutine of cutu_reader to simplify it.
6681 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6682 Returns NULL if the specified DWO unit cannot be found. */
6683
6684 static struct dwo_unit *
6685 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6686 struct die_info *comp_unit_die,
6687 const char *dwo_name)
6688 {
6689 struct dwarf2_cu *cu = this_cu->cu;
6690 struct dwo_unit *dwo_unit;
6691 const char *comp_dir;
6692
6693 gdb_assert (cu != NULL);
6694
6695 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6696 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6697 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6698
6699 if (this_cu->is_debug_types)
6700 {
6701 struct signatured_type *sig_type;
6702
6703 /* Since this_cu is the first member of struct signatured_type,
6704 we can go from a pointer to one to a pointer to the other. */
6705 sig_type = (struct signatured_type *) this_cu;
6706 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6707 }
6708 else
6709 {
6710 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6711 if (!signature.has_value ())
6712 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6713 " [in module %s]"),
6714 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6715 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6716 *signature);
6717 }
6718
6719 return dwo_unit;
6720 }
6721
6722 /* Subroutine of cutu_reader to simplify it.
6723 See it for a description of the parameters.
6724 Read a TU directly from a DWO file, bypassing the stub. */
6725
6726 void
6727 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6728 int use_existing_cu)
6729 {
6730 struct signatured_type *sig_type;
6731 struct die_reader_specs reader;
6732
6733 /* Verify we can do the following downcast, and that we have the
6734 data we need. */
6735 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6736 sig_type = (struct signatured_type *) this_cu;
6737 gdb_assert (sig_type->dwo_unit != NULL);
6738
6739 if (use_existing_cu && this_cu->cu != NULL)
6740 {
6741 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6742 /* There's no need to do the rereading_dwo_cu handling that
6743 cutu_reader does since we don't read the stub. */
6744 }
6745 else
6746 {
6747 /* If !use_existing_cu, this_cu->cu must be NULL. */
6748 gdb_assert (this_cu->cu == NULL);
6749 m_new_cu.reset (new dwarf2_cu (this_cu));
6750 }
6751
6752 /* A future optimization, if needed, would be to use an existing
6753 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6754 could share abbrev tables. */
6755
6756 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6757 NULL /* stub_comp_unit_die */,
6758 sig_type->dwo_unit->dwo_file->comp_dir,
6759 &reader, &info_ptr,
6760 &comp_unit_die,
6761 &m_dwo_abbrev_table) == 0)
6762 {
6763 /* Dummy die. */
6764 dummy_p = true;
6765 }
6766 }
6767
6768 /* Initialize a CU (or TU) and read its DIEs.
6769 If the CU defers to a DWO file, read the DWO file as well.
6770
6771 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6772 Otherwise the table specified in the comp unit header is read in and used.
6773 This is an optimization for when we already have the abbrev table.
6774
6775 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6776 Otherwise, a new CU is allocated with xmalloc. */
6777
6778 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6779 struct abbrev_table *abbrev_table,
6780 int use_existing_cu,
6781 bool skip_partial)
6782 : die_reader_specs {},
6783 m_this_cu (this_cu)
6784 {
6785 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6786 struct objfile *objfile = dwarf2_per_objfile->objfile;
6787 struct dwarf2_section_info *section = this_cu->section;
6788 bfd *abfd = section->get_bfd_owner ();
6789 struct dwarf2_cu *cu;
6790 const gdb_byte *begin_info_ptr;
6791 struct signatured_type *sig_type = NULL;
6792 struct dwarf2_section_info *abbrev_section;
6793 /* Non-zero if CU currently points to a DWO file and we need to
6794 reread it. When this happens we need to reread the skeleton die
6795 before we can reread the DWO file (this only applies to CUs, not TUs). */
6796 int rereading_dwo_cu = 0;
6797
6798 if (dwarf_die_debug)
6799 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6800 this_cu->is_debug_types ? "type" : "comp",
6801 sect_offset_str (this_cu->sect_off));
6802
6803 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6804 file (instead of going through the stub), short-circuit all of this. */
6805 if (this_cu->reading_dwo_directly)
6806 {
6807 /* Narrow down the scope of possibilities to have to understand. */
6808 gdb_assert (this_cu->is_debug_types);
6809 gdb_assert (abbrev_table == NULL);
6810 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6811 return;
6812 }
6813
6814 /* This is cheap if the section is already read in. */
6815 section->read (objfile);
6816
6817 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6818
6819 abbrev_section = get_abbrev_section_for_cu (this_cu);
6820
6821 if (use_existing_cu && this_cu->cu != NULL)
6822 {
6823 cu = this_cu->cu;
6824 /* If this CU is from a DWO file we need to start over, we need to
6825 refetch the attributes from the skeleton CU.
6826 This could be optimized by retrieving those attributes from when we
6827 were here the first time: the previous comp_unit_die was stored in
6828 comp_unit_obstack. But there's no data yet that we need this
6829 optimization. */
6830 if (cu->dwo_unit != NULL)
6831 rereading_dwo_cu = 1;
6832 }
6833 else
6834 {
6835 /* If !use_existing_cu, this_cu->cu must be NULL. */
6836 gdb_assert (this_cu->cu == NULL);
6837 m_new_cu.reset (new dwarf2_cu (this_cu));
6838 cu = m_new_cu.get ();
6839 }
6840
6841 /* Get the header. */
6842 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6843 {
6844 /* We already have the header, there's no need to read it in again. */
6845 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6846 }
6847 else
6848 {
6849 if (this_cu->is_debug_types)
6850 {
6851 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6852 &cu->header, section,
6853 abbrev_section, info_ptr,
6854 rcuh_kind::TYPE);
6855
6856 /* Since per_cu is the first member of struct signatured_type,
6857 we can go from a pointer to one to a pointer to the other. */
6858 sig_type = (struct signatured_type *) this_cu;
6859 gdb_assert (sig_type->signature == cu->header.signature);
6860 gdb_assert (sig_type->type_offset_in_tu
6861 == cu->header.type_cu_offset_in_tu);
6862 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6863
6864 /* LENGTH has not been set yet for type units if we're
6865 using .gdb_index. */
6866 this_cu->length = cu->header.get_length ();
6867
6868 /* Establish the type offset that can be used to lookup the type. */
6869 sig_type->type_offset_in_section =
6870 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6871
6872 this_cu->dwarf_version = cu->header.version;
6873 }
6874 else
6875 {
6876 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6877 &cu->header, section,
6878 abbrev_section,
6879 info_ptr,
6880 rcuh_kind::COMPILE);
6881
6882 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6883 gdb_assert (this_cu->length == cu->header.get_length ());
6884 this_cu->dwarf_version = cu->header.version;
6885 }
6886 }
6887
6888 /* Skip dummy compilation units. */
6889 if (info_ptr >= begin_info_ptr + this_cu->length
6890 || peek_abbrev_code (abfd, info_ptr) == 0)
6891 {
6892 dummy_p = true;
6893 return;
6894 }
6895
6896 /* If we don't have them yet, read the abbrevs for this compilation unit.
6897 And if we need to read them now, make sure they're freed when we're
6898 done. */
6899 if (abbrev_table != NULL)
6900 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6901 else
6902 {
6903 m_abbrev_table_holder
6904 = abbrev_table::read (objfile, abbrev_section,
6905 cu->header.abbrev_sect_off);
6906 abbrev_table = m_abbrev_table_holder.get ();
6907 }
6908
6909 /* Read the top level CU/TU die. */
6910 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6911 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6912
6913 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6914 {
6915 dummy_p = true;
6916 return;
6917 }
6918
6919 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6920 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6921 table from the DWO file and pass the ownership over to us. It will be
6922 referenced from READER, so we must make sure to free it after we're done
6923 with READER.
6924
6925 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6926 DWO CU, that this test will fail (the attribute will not be present). */
6927 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6928 if (dwo_name != nullptr)
6929 {
6930 struct dwo_unit *dwo_unit;
6931 struct die_info *dwo_comp_unit_die;
6932
6933 if (comp_unit_die->has_children)
6934 {
6935 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6936 " has children (offset %s) [in module %s]"),
6937 sect_offset_str (this_cu->sect_off),
6938 bfd_get_filename (abfd));
6939 }
6940 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6941 if (dwo_unit != NULL)
6942 {
6943 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6944 comp_unit_die, NULL,
6945 this, &info_ptr,
6946 &dwo_comp_unit_die,
6947 &m_dwo_abbrev_table) == 0)
6948 {
6949 /* Dummy die. */
6950 dummy_p = true;
6951 return;
6952 }
6953 comp_unit_die = dwo_comp_unit_die;
6954 }
6955 else
6956 {
6957 /* Yikes, we couldn't find the rest of the DIE, we only have
6958 the stub. A complaint has already been logged. There's
6959 not much more we can do except pass on the stub DIE to
6960 die_reader_func. We don't want to throw an error on bad
6961 debug info. */
6962 }
6963 }
6964 }
6965
6966 void
6967 cutu_reader::keep ()
6968 {
6969 /* Done, clean up. */
6970 gdb_assert (!dummy_p);
6971 if (m_new_cu != NULL)
6972 {
6973 struct dwarf2_per_objfile *dwarf2_per_objfile
6974 = m_this_cu->dwarf2_per_objfile;
6975 /* Link this CU into read_in_chain. */
6976 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6977 dwarf2_per_objfile->read_in_chain = m_this_cu;
6978 /* The chain owns it now. */
6979 m_new_cu.release ();
6980 }
6981 }
6982
6983 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6984 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6985 assumed to have already done the lookup to find the DWO file).
6986
6987 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6988 THIS_CU->is_debug_types, but nothing else.
6989
6990 We fill in THIS_CU->length.
6991
6992 THIS_CU->cu is always freed when done.
6993 This is done in order to not leave THIS_CU->cu in a state where we have
6994 to care whether it refers to the "main" CU or the DWO CU.
6995
6996 When parent_cu is passed, it is used to provide a default value for
6997 str_offsets_base and addr_base from the parent. */
6998
6999 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7000 struct dwarf2_cu *parent_cu,
7001 struct dwo_file *dwo_file)
7002 : die_reader_specs {},
7003 m_this_cu (this_cu)
7004 {
7005 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7006 struct objfile *objfile = dwarf2_per_objfile->objfile;
7007 struct dwarf2_section_info *section = this_cu->section;
7008 bfd *abfd = section->get_bfd_owner ();
7009 struct dwarf2_section_info *abbrev_section;
7010 const gdb_byte *begin_info_ptr, *info_ptr;
7011
7012 if (dwarf_die_debug)
7013 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7014 this_cu->is_debug_types ? "type" : "comp",
7015 sect_offset_str (this_cu->sect_off));
7016
7017 gdb_assert (this_cu->cu == NULL);
7018
7019 abbrev_section = (dwo_file != NULL
7020 ? &dwo_file->sections.abbrev
7021 : get_abbrev_section_for_cu (this_cu));
7022
7023 /* This is cheap if the section is already read in. */
7024 section->read (objfile);
7025
7026 m_new_cu.reset (new dwarf2_cu (this_cu));
7027
7028 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7029 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7030 &m_new_cu->header, section,
7031 abbrev_section, info_ptr,
7032 (this_cu->is_debug_types
7033 ? rcuh_kind::TYPE
7034 : rcuh_kind::COMPILE));
7035
7036 if (parent_cu != nullptr)
7037 {
7038 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7039 m_new_cu->addr_base = parent_cu->addr_base;
7040 }
7041 this_cu->length = m_new_cu->header.get_length ();
7042
7043 /* Skip dummy compilation units. */
7044 if (info_ptr >= begin_info_ptr + this_cu->length
7045 || peek_abbrev_code (abfd, info_ptr) == 0)
7046 {
7047 dummy_p = true;
7048 return;
7049 }
7050
7051 m_abbrev_table_holder
7052 = abbrev_table::read (objfile, abbrev_section,
7053 m_new_cu->header.abbrev_sect_off);
7054
7055 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7056 m_abbrev_table_holder.get ());
7057 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7058 }
7059
7060 \f
7061 /* Type Unit Groups.
7062
7063 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7064 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7065 so that all types coming from the same compilation (.o file) are grouped
7066 together. A future step could be to put the types in the same symtab as
7067 the CU the types ultimately came from. */
7068
7069 static hashval_t
7070 hash_type_unit_group (const void *item)
7071 {
7072 const struct type_unit_group *tu_group
7073 = (const struct type_unit_group *) item;
7074
7075 return hash_stmt_list_entry (&tu_group->hash);
7076 }
7077
7078 static int
7079 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7080 {
7081 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7082 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7083
7084 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7085 }
7086
7087 /* Allocate a hash table for type unit groups. */
7088
7089 static htab_up
7090 allocate_type_unit_groups_table (struct objfile *objfile)
7091 {
7092 return htab_up (htab_create_alloc (3,
7093 hash_type_unit_group,
7094 eq_type_unit_group,
7095 NULL, xcalloc, xfree));
7096 }
7097
7098 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7099 partial symtabs. We combine several TUs per psymtab to not let the size
7100 of any one psymtab grow too big. */
7101 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7102 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7103
7104 /* Helper routine for get_type_unit_group.
7105 Create the type_unit_group object used to hold one or more TUs. */
7106
7107 static struct type_unit_group *
7108 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7109 {
7110 struct dwarf2_per_objfile *dwarf2_per_objfile
7111 = cu->per_cu->dwarf2_per_objfile;
7112 struct objfile *objfile = dwarf2_per_objfile->objfile;
7113 struct dwarf2_per_cu_data *per_cu;
7114 struct type_unit_group *tu_group;
7115
7116 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7117 struct type_unit_group);
7118 per_cu = &tu_group->per_cu;
7119 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7120
7121 if (dwarf2_per_objfile->using_index)
7122 {
7123 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7124 struct dwarf2_per_cu_quick_data);
7125 }
7126 else
7127 {
7128 unsigned int line_offset = to_underlying (line_offset_struct);
7129 dwarf2_psymtab *pst;
7130 std::string name;
7131
7132 /* Give the symtab a useful name for debug purposes. */
7133 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7134 name = string_printf ("<type_units_%d>",
7135 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7136 else
7137 name = string_printf ("<type_units_at_0x%x>", line_offset);
7138
7139 pst = create_partial_symtab (per_cu, name.c_str ());
7140 pst->anonymous = true;
7141 }
7142
7143 tu_group->hash.dwo_unit = cu->dwo_unit;
7144 tu_group->hash.line_sect_off = line_offset_struct;
7145
7146 return tu_group;
7147 }
7148
7149 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7150 STMT_LIST is a DW_AT_stmt_list attribute. */
7151
7152 static struct type_unit_group *
7153 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7154 {
7155 struct dwarf2_per_objfile *dwarf2_per_objfile
7156 = cu->per_cu->dwarf2_per_objfile;
7157 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7158 struct type_unit_group *tu_group;
7159 void **slot;
7160 unsigned int line_offset;
7161 struct type_unit_group type_unit_group_for_lookup;
7162
7163 if (dwarf2_per_objfile->type_unit_groups == NULL)
7164 {
7165 dwarf2_per_objfile->type_unit_groups =
7166 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7167 }
7168
7169 /* Do we need to create a new group, or can we use an existing one? */
7170
7171 if (stmt_list)
7172 {
7173 line_offset = DW_UNSND (stmt_list);
7174 ++tu_stats->nr_symtab_sharers;
7175 }
7176 else
7177 {
7178 /* Ugh, no stmt_list. Rare, but we have to handle it.
7179 We can do various things here like create one group per TU or
7180 spread them over multiple groups to split up the expansion work.
7181 To avoid worst case scenarios (too many groups or too large groups)
7182 we, umm, group them in bunches. */
7183 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7184 | (tu_stats->nr_stmt_less_type_units
7185 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7186 ++tu_stats->nr_stmt_less_type_units;
7187 }
7188
7189 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7190 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7191 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7192 &type_unit_group_for_lookup, INSERT);
7193 if (*slot != NULL)
7194 {
7195 tu_group = (struct type_unit_group *) *slot;
7196 gdb_assert (tu_group != NULL);
7197 }
7198 else
7199 {
7200 sect_offset line_offset_struct = (sect_offset) line_offset;
7201 tu_group = create_type_unit_group (cu, line_offset_struct);
7202 *slot = tu_group;
7203 ++tu_stats->nr_symtabs;
7204 }
7205
7206 return tu_group;
7207 }
7208 \f
7209 /* Partial symbol tables. */
7210
7211 /* Create a psymtab named NAME and assign it to PER_CU.
7212
7213 The caller must fill in the following details:
7214 dirname, textlow, texthigh. */
7215
7216 static dwarf2_psymtab *
7217 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7218 {
7219 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7220 dwarf2_psymtab *pst;
7221
7222 pst = new dwarf2_psymtab (name, objfile, 0);
7223
7224 pst->psymtabs_addrmap_supported = true;
7225
7226 /* This is the glue that links PST into GDB's symbol API. */
7227 pst->per_cu_data = per_cu;
7228 per_cu->v.psymtab = pst;
7229
7230 return pst;
7231 }
7232
7233 /* DIE reader function for process_psymtab_comp_unit. */
7234
7235 static void
7236 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7237 const gdb_byte *info_ptr,
7238 struct die_info *comp_unit_die,
7239 enum language pretend_language)
7240 {
7241 struct dwarf2_cu *cu = reader->cu;
7242 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7244 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7245 CORE_ADDR baseaddr;
7246 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7247 dwarf2_psymtab *pst;
7248 enum pc_bounds_kind cu_bounds_kind;
7249 const char *filename;
7250
7251 gdb_assert (! per_cu->is_debug_types);
7252
7253 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7254
7255 /* Allocate a new partial symbol table structure. */
7256 gdb::unique_xmalloc_ptr<char> debug_filename;
7257 static const char artificial[] = "<artificial>";
7258 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7259 if (filename == NULL)
7260 filename = "";
7261 else if (strcmp (filename, artificial) == 0)
7262 {
7263 debug_filename.reset (concat (artificial, "@",
7264 sect_offset_str (per_cu->sect_off),
7265 (char *) NULL));
7266 filename = debug_filename.get ();
7267 }
7268
7269 pst = create_partial_symtab (per_cu, filename);
7270
7271 /* This must be done before calling dwarf2_build_include_psymtabs. */
7272 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7273
7274 baseaddr = objfile->text_section_offset ();
7275
7276 dwarf2_find_base_address (comp_unit_die, cu);
7277
7278 /* Possibly set the default values of LOWPC and HIGHPC from
7279 `DW_AT_ranges'. */
7280 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7281 &best_highpc, cu, pst);
7282 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7283 {
7284 CORE_ADDR low
7285 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7286 - baseaddr);
7287 CORE_ADDR high
7288 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7289 - baseaddr - 1);
7290 /* Store the contiguous range if it is not empty; it can be
7291 empty for CUs with no code. */
7292 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7293 low, high, pst);
7294 }
7295
7296 /* Check if comp unit has_children.
7297 If so, read the rest of the partial symbols from this comp unit.
7298 If not, there's no more debug_info for this comp unit. */
7299 if (comp_unit_die->has_children)
7300 {
7301 struct partial_die_info *first_die;
7302 CORE_ADDR lowpc, highpc;
7303
7304 lowpc = ((CORE_ADDR) -1);
7305 highpc = ((CORE_ADDR) 0);
7306
7307 first_die = load_partial_dies (reader, info_ptr, 1);
7308
7309 scan_partial_symbols (first_die, &lowpc, &highpc,
7310 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7311
7312 /* If we didn't find a lowpc, set it to highpc to avoid
7313 complaints from `maint check'. */
7314 if (lowpc == ((CORE_ADDR) -1))
7315 lowpc = highpc;
7316
7317 /* If the compilation unit didn't have an explicit address range,
7318 then use the information extracted from its child dies. */
7319 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7320 {
7321 best_lowpc = lowpc;
7322 best_highpc = highpc;
7323 }
7324 }
7325 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7326 best_lowpc + baseaddr)
7327 - baseaddr);
7328 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7329 best_highpc + baseaddr)
7330 - baseaddr);
7331
7332 end_psymtab_common (objfile, pst);
7333
7334 if (!cu->per_cu->imported_symtabs_empty ())
7335 {
7336 int i;
7337 int len = cu->per_cu->imported_symtabs_size ();
7338
7339 /* Fill in 'dependencies' here; we fill in 'users' in a
7340 post-pass. */
7341 pst->number_of_dependencies = len;
7342 pst->dependencies
7343 = objfile->partial_symtabs->allocate_dependencies (len);
7344 for (i = 0; i < len; ++i)
7345 {
7346 pst->dependencies[i]
7347 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7348 }
7349
7350 cu->per_cu->imported_symtabs_free ();
7351 }
7352
7353 /* Get the list of files included in the current compilation unit,
7354 and build a psymtab for each of them. */
7355 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7356
7357 if (dwarf_read_debug)
7358 fprintf_unfiltered (gdb_stdlog,
7359 "Psymtab for %s unit @%s: %s - %s"
7360 ", %d global, %d static syms\n",
7361 per_cu->is_debug_types ? "type" : "comp",
7362 sect_offset_str (per_cu->sect_off),
7363 paddress (gdbarch, pst->text_low (objfile)),
7364 paddress (gdbarch, pst->text_high (objfile)),
7365 pst->n_global_syms, pst->n_static_syms);
7366 }
7367
7368 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7369 Process compilation unit THIS_CU for a psymtab. */
7370
7371 static void
7372 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7373 bool want_partial_unit,
7374 enum language pretend_language)
7375 {
7376 /* If this compilation unit was already read in, free the
7377 cached copy in order to read it in again. This is
7378 necessary because we skipped some symbols when we first
7379 read in the compilation unit (see load_partial_dies).
7380 This problem could be avoided, but the benefit is unclear. */
7381 if (this_cu->cu != NULL)
7382 free_one_cached_comp_unit (this_cu);
7383
7384 cutu_reader reader (this_cu, NULL, 0, false);
7385
7386 if (reader.dummy_p)
7387 {
7388 /* Nothing. */
7389 }
7390 else if (this_cu->is_debug_types)
7391 build_type_psymtabs_reader (&reader, reader.info_ptr,
7392 reader.comp_unit_die);
7393 else if (want_partial_unit
7394 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7395 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7396 reader.comp_unit_die,
7397 pretend_language);
7398
7399 /* Age out any secondary CUs. */
7400 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7401 }
7402
7403 /* Reader function for build_type_psymtabs. */
7404
7405 static void
7406 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7407 const gdb_byte *info_ptr,
7408 struct die_info *type_unit_die)
7409 {
7410 struct dwarf2_per_objfile *dwarf2_per_objfile
7411 = reader->cu->per_cu->dwarf2_per_objfile;
7412 struct objfile *objfile = dwarf2_per_objfile->objfile;
7413 struct dwarf2_cu *cu = reader->cu;
7414 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7415 struct signatured_type *sig_type;
7416 struct type_unit_group *tu_group;
7417 struct attribute *attr;
7418 struct partial_die_info *first_die;
7419 CORE_ADDR lowpc, highpc;
7420 dwarf2_psymtab *pst;
7421
7422 gdb_assert (per_cu->is_debug_types);
7423 sig_type = (struct signatured_type *) per_cu;
7424
7425 if (! type_unit_die->has_children)
7426 return;
7427
7428 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7429 tu_group = get_type_unit_group (cu, attr);
7430
7431 if (tu_group->tus == nullptr)
7432 tu_group->tus = new std::vector<signatured_type *>;
7433 tu_group->tus->push_back (sig_type);
7434
7435 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7436 pst = create_partial_symtab (per_cu, "");
7437 pst->anonymous = true;
7438
7439 first_die = load_partial_dies (reader, info_ptr, 1);
7440
7441 lowpc = (CORE_ADDR) -1;
7442 highpc = (CORE_ADDR) 0;
7443 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7444
7445 end_psymtab_common (objfile, pst);
7446 }
7447
7448 /* Struct used to sort TUs by their abbreviation table offset. */
7449
7450 struct tu_abbrev_offset
7451 {
7452 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7453 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7454 {}
7455
7456 signatured_type *sig_type;
7457 sect_offset abbrev_offset;
7458 };
7459
7460 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7461
7462 static bool
7463 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7464 const struct tu_abbrev_offset &b)
7465 {
7466 return a.abbrev_offset < b.abbrev_offset;
7467 }
7468
7469 /* Efficiently read all the type units.
7470 This does the bulk of the work for build_type_psymtabs.
7471
7472 The efficiency is because we sort TUs by the abbrev table they use and
7473 only read each abbrev table once. In one program there are 200K TUs
7474 sharing 8K abbrev tables.
7475
7476 The main purpose of this function is to support building the
7477 dwarf2_per_objfile->type_unit_groups table.
7478 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7479 can collapse the search space by grouping them by stmt_list.
7480 The savings can be significant, in the same program from above the 200K TUs
7481 share 8K stmt_list tables.
7482
7483 FUNC is expected to call get_type_unit_group, which will create the
7484 struct type_unit_group if necessary and add it to
7485 dwarf2_per_objfile->type_unit_groups. */
7486
7487 static void
7488 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7489 {
7490 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7491 abbrev_table_up abbrev_table;
7492 sect_offset abbrev_offset;
7493
7494 /* It's up to the caller to not call us multiple times. */
7495 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7496
7497 if (dwarf2_per_objfile->all_type_units.empty ())
7498 return;
7499
7500 /* TUs typically share abbrev tables, and there can be way more TUs than
7501 abbrev tables. Sort by abbrev table to reduce the number of times we
7502 read each abbrev table in.
7503 Alternatives are to punt or to maintain a cache of abbrev tables.
7504 This is simpler and efficient enough for now.
7505
7506 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7507 symtab to use). Typically TUs with the same abbrev offset have the same
7508 stmt_list value too so in practice this should work well.
7509
7510 The basic algorithm here is:
7511
7512 sort TUs by abbrev table
7513 for each TU with same abbrev table:
7514 read abbrev table if first user
7515 read TU top level DIE
7516 [IWBN if DWO skeletons had DW_AT_stmt_list]
7517 call FUNC */
7518
7519 if (dwarf_read_debug)
7520 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7521
7522 /* Sort in a separate table to maintain the order of all_type_units
7523 for .gdb_index: TU indices directly index all_type_units. */
7524 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7525 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7526
7527 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7528 sorted_by_abbrev.emplace_back
7529 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7530 sig_type->per_cu.section,
7531 sig_type->per_cu.sect_off));
7532
7533 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7534 sort_tu_by_abbrev_offset);
7535
7536 abbrev_offset = (sect_offset) ~(unsigned) 0;
7537
7538 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7539 {
7540 /* Switch to the next abbrev table if necessary. */
7541 if (abbrev_table == NULL
7542 || tu.abbrev_offset != abbrev_offset)
7543 {
7544 abbrev_offset = tu.abbrev_offset;
7545 abbrev_table =
7546 abbrev_table::read (dwarf2_per_objfile->objfile,
7547 &dwarf2_per_objfile->abbrev,
7548 abbrev_offset);
7549 ++tu_stats->nr_uniq_abbrev_tables;
7550 }
7551
7552 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7553 0, false);
7554 if (!reader.dummy_p)
7555 build_type_psymtabs_reader (&reader, reader.info_ptr,
7556 reader.comp_unit_die);
7557 }
7558 }
7559
7560 /* Print collected type unit statistics. */
7561
7562 static void
7563 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7564 {
7565 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7566
7567 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7568 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7569 dwarf2_per_objfile->all_type_units.size ());
7570 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7571 tu_stats->nr_uniq_abbrev_tables);
7572 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7573 tu_stats->nr_symtabs);
7574 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7575 tu_stats->nr_symtab_sharers);
7576 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7577 tu_stats->nr_stmt_less_type_units);
7578 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7579 tu_stats->nr_all_type_units_reallocs);
7580 }
7581
7582 /* Traversal function for build_type_psymtabs. */
7583
7584 static int
7585 build_type_psymtab_dependencies (void **slot, void *info)
7586 {
7587 struct dwarf2_per_objfile *dwarf2_per_objfile
7588 = (struct dwarf2_per_objfile *) info;
7589 struct objfile *objfile = dwarf2_per_objfile->objfile;
7590 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7591 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7592 dwarf2_psymtab *pst = per_cu->v.psymtab;
7593 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7594 int i;
7595
7596 gdb_assert (len > 0);
7597 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7598
7599 pst->number_of_dependencies = len;
7600 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7601 for (i = 0; i < len; ++i)
7602 {
7603 struct signatured_type *iter = tu_group->tus->at (i);
7604 gdb_assert (iter->per_cu.is_debug_types);
7605 pst->dependencies[i] = iter->per_cu.v.psymtab;
7606 iter->type_unit_group = tu_group;
7607 }
7608
7609 delete tu_group->tus;
7610 tu_group->tus = nullptr;
7611
7612 return 1;
7613 }
7614
7615 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7616 Build partial symbol tables for the .debug_types comp-units. */
7617
7618 static void
7619 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7620 {
7621 if (! create_all_type_units (dwarf2_per_objfile))
7622 return;
7623
7624 build_type_psymtabs_1 (dwarf2_per_objfile);
7625 }
7626
7627 /* Traversal function for process_skeletonless_type_unit.
7628 Read a TU in a DWO file and build partial symbols for it. */
7629
7630 static int
7631 process_skeletonless_type_unit (void **slot, void *info)
7632 {
7633 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7634 struct dwarf2_per_objfile *dwarf2_per_objfile
7635 = (struct dwarf2_per_objfile *) info;
7636 struct signatured_type find_entry, *entry;
7637
7638 /* If this TU doesn't exist in the global table, add it and read it in. */
7639
7640 if (dwarf2_per_objfile->signatured_types == NULL)
7641 {
7642 dwarf2_per_objfile->signatured_types
7643 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
7644 }
7645
7646 find_entry.signature = dwo_unit->signature;
7647 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7648 &find_entry, INSERT);
7649 /* If we've already seen this type there's nothing to do. What's happening
7650 is we're doing our own version of comdat-folding here. */
7651 if (*slot != NULL)
7652 return 1;
7653
7654 /* This does the job that create_all_type_units would have done for
7655 this TU. */
7656 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7657 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7658 *slot = entry;
7659
7660 /* This does the job that build_type_psymtabs_1 would have done. */
7661 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7662 if (!reader.dummy_p)
7663 build_type_psymtabs_reader (&reader, reader.info_ptr,
7664 reader.comp_unit_die);
7665
7666 return 1;
7667 }
7668
7669 /* Traversal function for process_skeletonless_type_units. */
7670
7671 static int
7672 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7673 {
7674 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7675
7676 if (dwo_file->tus != NULL)
7677 htab_traverse_noresize (dwo_file->tus.get (),
7678 process_skeletonless_type_unit, info);
7679
7680 return 1;
7681 }
7682
7683 /* Scan all TUs of DWO files, verifying we've processed them.
7684 This is needed in case a TU was emitted without its skeleton.
7685 Note: This can't be done until we know what all the DWO files are. */
7686
7687 static void
7688 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7689 {
7690 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7691 if (get_dwp_file (dwarf2_per_objfile) == NULL
7692 && dwarf2_per_objfile->dwo_files != NULL)
7693 {
7694 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7695 process_dwo_file_for_skeletonless_type_units,
7696 dwarf2_per_objfile);
7697 }
7698 }
7699
7700 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7701
7702 static void
7703 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7704 {
7705 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7706 {
7707 dwarf2_psymtab *pst = per_cu->v.psymtab;
7708
7709 if (pst == NULL)
7710 continue;
7711
7712 for (int j = 0; j < pst->number_of_dependencies; ++j)
7713 {
7714 /* Set the 'user' field only if it is not already set. */
7715 if (pst->dependencies[j]->user == NULL)
7716 pst->dependencies[j]->user = pst;
7717 }
7718 }
7719 }
7720
7721 /* Build the partial symbol table by doing a quick pass through the
7722 .debug_info and .debug_abbrev sections. */
7723
7724 static void
7725 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7726 {
7727 struct objfile *objfile = dwarf2_per_objfile->objfile;
7728
7729 if (dwarf_read_debug)
7730 {
7731 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7732 objfile_name (objfile));
7733 }
7734
7735 dwarf2_per_objfile->reading_partial_symbols = 1;
7736
7737 dwarf2_per_objfile->info.read (objfile);
7738
7739 /* Any cached compilation units will be linked by the per-objfile
7740 read_in_chain. Make sure to free them when we're done. */
7741 free_cached_comp_units freer (dwarf2_per_objfile);
7742
7743 build_type_psymtabs (dwarf2_per_objfile);
7744
7745 create_all_comp_units (dwarf2_per_objfile);
7746
7747 /* Create a temporary address map on a temporary obstack. We later
7748 copy this to the final obstack. */
7749 auto_obstack temp_obstack;
7750
7751 scoped_restore save_psymtabs_addrmap
7752 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7753 addrmap_create_mutable (&temp_obstack));
7754
7755 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7756 process_psymtab_comp_unit (per_cu, false, language_minimal);
7757
7758 /* This has to wait until we read the CUs, we need the list of DWOs. */
7759 process_skeletonless_type_units (dwarf2_per_objfile);
7760
7761 /* Now that all TUs have been processed we can fill in the dependencies. */
7762 if (dwarf2_per_objfile->type_unit_groups != NULL)
7763 {
7764 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7765 build_type_psymtab_dependencies, dwarf2_per_objfile);
7766 }
7767
7768 if (dwarf_read_debug)
7769 print_tu_stats (dwarf2_per_objfile);
7770
7771 set_partial_user (dwarf2_per_objfile);
7772
7773 objfile->partial_symtabs->psymtabs_addrmap
7774 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7775 objfile->partial_symtabs->obstack ());
7776 /* At this point we want to keep the address map. */
7777 save_psymtabs_addrmap.release ();
7778
7779 if (dwarf_read_debug)
7780 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7781 objfile_name (objfile));
7782 }
7783
7784 /* Load the partial DIEs for a secondary CU into memory.
7785 This is also used when rereading a primary CU with load_all_dies. */
7786
7787 static void
7788 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7789 {
7790 cutu_reader reader (this_cu, NULL, 1, false);
7791
7792 if (!reader.dummy_p)
7793 {
7794 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7795 language_minimal);
7796
7797 /* Check if comp unit has_children.
7798 If so, read the rest of the partial symbols from this comp unit.
7799 If not, there's no more debug_info for this comp unit. */
7800 if (reader.comp_unit_die->has_children)
7801 load_partial_dies (&reader, reader.info_ptr, 0);
7802
7803 reader.keep ();
7804 }
7805 }
7806
7807 static void
7808 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7809 struct dwarf2_section_info *section,
7810 struct dwarf2_section_info *abbrev_section,
7811 unsigned int is_dwz)
7812 {
7813 const gdb_byte *info_ptr;
7814 struct objfile *objfile = dwarf2_per_objfile->objfile;
7815
7816 if (dwarf_read_debug)
7817 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7818 section->get_name (),
7819 section->get_file_name ());
7820
7821 section->read (objfile);
7822
7823 info_ptr = section->buffer;
7824
7825 while (info_ptr < section->buffer + section->size)
7826 {
7827 struct dwarf2_per_cu_data *this_cu;
7828
7829 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7830
7831 comp_unit_head cu_header;
7832 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7833 abbrev_section, info_ptr,
7834 rcuh_kind::COMPILE);
7835
7836 /* Save the compilation unit for later lookup. */
7837 if (cu_header.unit_type != DW_UT_type)
7838 {
7839 this_cu = XOBNEW (&objfile->objfile_obstack,
7840 struct dwarf2_per_cu_data);
7841 memset (this_cu, 0, sizeof (*this_cu));
7842 }
7843 else
7844 {
7845 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7846 struct signatured_type);
7847 memset (sig_type, 0, sizeof (*sig_type));
7848 sig_type->signature = cu_header.signature;
7849 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7850 this_cu = &sig_type->per_cu;
7851 }
7852 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7853 this_cu->sect_off = sect_off;
7854 this_cu->length = cu_header.length + cu_header.initial_length_size;
7855 this_cu->is_dwz = is_dwz;
7856 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7857 this_cu->section = section;
7858
7859 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7860
7861 info_ptr = info_ptr + this_cu->length;
7862 }
7863 }
7864
7865 /* Create a list of all compilation units in OBJFILE.
7866 This is only done for -readnow and building partial symtabs. */
7867
7868 static void
7869 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7870 {
7871 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7872 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7873 &dwarf2_per_objfile->abbrev, 0);
7874
7875 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7876 if (dwz != NULL)
7877 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7878 1);
7879 }
7880
7881 /* Process all loaded DIEs for compilation unit CU, starting at
7882 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7883 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7884 DW_AT_ranges). See the comments of add_partial_subprogram on how
7885 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7886
7887 static void
7888 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7889 CORE_ADDR *highpc, int set_addrmap,
7890 struct dwarf2_cu *cu)
7891 {
7892 struct partial_die_info *pdi;
7893
7894 /* Now, march along the PDI's, descending into ones which have
7895 interesting children but skipping the children of the other ones,
7896 until we reach the end of the compilation unit. */
7897
7898 pdi = first_die;
7899
7900 while (pdi != NULL)
7901 {
7902 pdi->fixup (cu);
7903
7904 /* Anonymous namespaces or modules have no name but have interesting
7905 children, so we need to look at them. Ditto for anonymous
7906 enums. */
7907
7908 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7909 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7910 || pdi->tag == DW_TAG_imported_unit
7911 || pdi->tag == DW_TAG_inlined_subroutine)
7912 {
7913 switch (pdi->tag)
7914 {
7915 case DW_TAG_subprogram:
7916 case DW_TAG_inlined_subroutine:
7917 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7918 break;
7919 case DW_TAG_constant:
7920 case DW_TAG_variable:
7921 case DW_TAG_typedef:
7922 case DW_TAG_union_type:
7923 if (!pdi->is_declaration)
7924 {
7925 add_partial_symbol (pdi, cu);
7926 }
7927 break;
7928 case DW_TAG_class_type:
7929 case DW_TAG_interface_type:
7930 case DW_TAG_structure_type:
7931 if (!pdi->is_declaration)
7932 {
7933 add_partial_symbol (pdi, cu);
7934 }
7935 if ((cu->language == language_rust
7936 || cu->language == language_cplus) && pdi->has_children)
7937 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7938 set_addrmap, cu);
7939 break;
7940 case DW_TAG_enumeration_type:
7941 if (!pdi->is_declaration)
7942 add_partial_enumeration (pdi, cu);
7943 break;
7944 case DW_TAG_base_type:
7945 case DW_TAG_subrange_type:
7946 /* File scope base type definitions are added to the partial
7947 symbol table. */
7948 add_partial_symbol (pdi, cu);
7949 break;
7950 case DW_TAG_namespace:
7951 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7952 break;
7953 case DW_TAG_module:
7954 if (!pdi->is_declaration)
7955 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7956 break;
7957 case DW_TAG_imported_unit:
7958 {
7959 struct dwarf2_per_cu_data *per_cu;
7960
7961 /* For now we don't handle imported units in type units. */
7962 if (cu->per_cu->is_debug_types)
7963 {
7964 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7965 " supported in type units [in module %s]"),
7966 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7967 }
7968
7969 per_cu = dwarf2_find_containing_comp_unit
7970 (pdi->d.sect_off, pdi->is_dwz,
7971 cu->per_cu->dwarf2_per_objfile);
7972
7973 /* Go read the partial unit, if needed. */
7974 if (per_cu->v.psymtab == NULL)
7975 process_psymtab_comp_unit (per_cu, true, cu->language);
7976
7977 cu->per_cu->imported_symtabs_push (per_cu);
7978 }
7979 break;
7980 case DW_TAG_imported_declaration:
7981 add_partial_symbol (pdi, cu);
7982 break;
7983 default:
7984 break;
7985 }
7986 }
7987
7988 /* If the die has a sibling, skip to the sibling. */
7989
7990 pdi = pdi->die_sibling;
7991 }
7992 }
7993
7994 /* Functions used to compute the fully scoped name of a partial DIE.
7995
7996 Normally, this is simple. For C++, the parent DIE's fully scoped
7997 name is concatenated with "::" and the partial DIE's name.
7998 Enumerators are an exception; they use the scope of their parent
7999 enumeration type, i.e. the name of the enumeration type is not
8000 prepended to the enumerator.
8001
8002 There are two complexities. One is DW_AT_specification; in this
8003 case "parent" means the parent of the target of the specification,
8004 instead of the direct parent of the DIE. The other is compilers
8005 which do not emit DW_TAG_namespace; in this case we try to guess
8006 the fully qualified name of structure types from their members'
8007 linkage names. This must be done using the DIE's children rather
8008 than the children of any DW_AT_specification target. We only need
8009 to do this for structures at the top level, i.e. if the target of
8010 any DW_AT_specification (if any; otherwise the DIE itself) does not
8011 have a parent. */
8012
8013 /* Compute the scope prefix associated with PDI's parent, in
8014 compilation unit CU. The result will be allocated on CU's
8015 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8016 field. NULL is returned if no prefix is necessary. */
8017 static const char *
8018 partial_die_parent_scope (struct partial_die_info *pdi,
8019 struct dwarf2_cu *cu)
8020 {
8021 const char *grandparent_scope;
8022 struct partial_die_info *parent, *real_pdi;
8023
8024 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8025 then this means the parent of the specification DIE. */
8026
8027 real_pdi = pdi;
8028 while (real_pdi->has_specification)
8029 {
8030 auto res = find_partial_die (real_pdi->spec_offset,
8031 real_pdi->spec_is_dwz, cu);
8032 real_pdi = res.pdi;
8033 cu = res.cu;
8034 }
8035
8036 parent = real_pdi->die_parent;
8037 if (parent == NULL)
8038 return NULL;
8039
8040 if (parent->scope_set)
8041 return parent->scope;
8042
8043 parent->fixup (cu);
8044
8045 grandparent_scope = partial_die_parent_scope (parent, cu);
8046
8047 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8048 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8049 Work around this problem here. */
8050 if (cu->language == language_cplus
8051 && parent->tag == DW_TAG_namespace
8052 && strcmp (parent->name, "::") == 0
8053 && grandparent_scope == NULL)
8054 {
8055 parent->scope = NULL;
8056 parent->scope_set = 1;
8057 return NULL;
8058 }
8059
8060 /* Nested subroutines in Fortran get a prefix. */
8061 if (pdi->tag == DW_TAG_enumerator)
8062 /* Enumerators should not get the name of the enumeration as a prefix. */
8063 parent->scope = grandparent_scope;
8064 else if (parent->tag == DW_TAG_namespace
8065 || parent->tag == DW_TAG_module
8066 || parent->tag == DW_TAG_structure_type
8067 || parent->tag == DW_TAG_class_type
8068 || parent->tag == DW_TAG_interface_type
8069 || parent->tag == DW_TAG_union_type
8070 || parent->tag == DW_TAG_enumeration_type
8071 || (cu->language == language_fortran
8072 && parent->tag == DW_TAG_subprogram
8073 && pdi->tag == DW_TAG_subprogram))
8074 {
8075 if (grandparent_scope == NULL)
8076 parent->scope = parent->name;
8077 else
8078 parent->scope = typename_concat (&cu->comp_unit_obstack,
8079 grandparent_scope,
8080 parent->name, 0, cu);
8081 }
8082 else
8083 {
8084 /* FIXME drow/2004-04-01: What should we be doing with
8085 function-local names? For partial symbols, we should probably be
8086 ignoring them. */
8087 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8088 dwarf_tag_name (parent->tag),
8089 sect_offset_str (pdi->sect_off));
8090 parent->scope = grandparent_scope;
8091 }
8092
8093 parent->scope_set = 1;
8094 return parent->scope;
8095 }
8096
8097 /* Return the fully scoped name associated with PDI, from compilation unit
8098 CU. The result will be allocated with malloc. */
8099
8100 static gdb::unique_xmalloc_ptr<char>
8101 partial_die_full_name (struct partial_die_info *pdi,
8102 struct dwarf2_cu *cu)
8103 {
8104 const char *parent_scope;
8105
8106 /* If this is a template instantiation, we can not work out the
8107 template arguments from partial DIEs. So, unfortunately, we have
8108 to go through the full DIEs. At least any work we do building
8109 types here will be reused if full symbols are loaded later. */
8110 if (pdi->has_template_arguments)
8111 {
8112 pdi->fixup (cu);
8113
8114 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8115 {
8116 struct die_info *die;
8117 struct attribute attr;
8118 struct dwarf2_cu *ref_cu = cu;
8119
8120 /* DW_FORM_ref_addr is using section offset. */
8121 attr.name = (enum dwarf_attribute) 0;
8122 attr.form = DW_FORM_ref_addr;
8123 attr.u.unsnd = to_underlying (pdi->sect_off);
8124 die = follow_die_ref (NULL, &attr, &ref_cu);
8125
8126 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8127 }
8128 }
8129
8130 parent_scope = partial_die_parent_scope (pdi, cu);
8131 if (parent_scope == NULL)
8132 return NULL;
8133 else
8134 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8135 pdi->name, 0, cu));
8136 }
8137
8138 static void
8139 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8140 {
8141 struct dwarf2_per_objfile *dwarf2_per_objfile
8142 = cu->per_cu->dwarf2_per_objfile;
8143 struct objfile *objfile = dwarf2_per_objfile->objfile;
8144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8145 CORE_ADDR addr = 0;
8146 const char *actual_name = NULL;
8147 CORE_ADDR baseaddr;
8148
8149 baseaddr = objfile->text_section_offset ();
8150
8151 gdb::unique_xmalloc_ptr<char> built_actual_name
8152 = partial_die_full_name (pdi, cu);
8153 if (built_actual_name != NULL)
8154 actual_name = built_actual_name.get ();
8155
8156 if (actual_name == NULL)
8157 actual_name = pdi->name;
8158
8159 switch (pdi->tag)
8160 {
8161 case DW_TAG_inlined_subroutine:
8162 case DW_TAG_subprogram:
8163 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8164 - baseaddr);
8165 if (pdi->is_external
8166 || cu->language == language_ada
8167 || (cu->language == language_fortran
8168 && pdi->die_parent != NULL
8169 && pdi->die_parent->tag == DW_TAG_subprogram))
8170 {
8171 /* Normally, only "external" DIEs are part of the global scope.
8172 But in Ada and Fortran, we want to be able to access nested
8173 procedures globally. So all Ada and Fortran subprograms are
8174 stored in the global scope. */
8175 add_psymbol_to_list (actual_name,
8176 built_actual_name != NULL,
8177 VAR_DOMAIN, LOC_BLOCK,
8178 SECT_OFF_TEXT (objfile),
8179 psymbol_placement::GLOBAL,
8180 addr,
8181 cu->language, objfile);
8182 }
8183 else
8184 {
8185 add_psymbol_to_list (actual_name,
8186 built_actual_name != NULL,
8187 VAR_DOMAIN, LOC_BLOCK,
8188 SECT_OFF_TEXT (objfile),
8189 psymbol_placement::STATIC,
8190 addr, cu->language, objfile);
8191 }
8192
8193 if (pdi->main_subprogram && actual_name != NULL)
8194 set_objfile_main_name (objfile, actual_name, cu->language);
8195 break;
8196 case DW_TAG_constant:
8197 add_psymbol_to_list (actual_name,
8198 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8199 -1, (pdi->is_external
8200 ? psymbol_placement::GLOBAL
8201 : psymbol_placement::STATIC),
8202 0, cu->language, objfile);
8203 break;
8204 case DW_TAG_variable:
8205 if (pdi->d.locdesc)
8206 addr = decode_locdesc (pdi->d.locdesc, cu);
8207
8208 if (pdi->d.locdesc
8209 && addr == 0
8210 && !dwarf2_per_objfile->has_section_at_zero)
8211 {
8212 /* A global or static variable may also have been stripped
8213 out by the linker if unused, in which case its address
8214 will be nullified; do not add such variables into partial
8215 symbol table then. */
8216 }
8217 else if (pdi->is_external)
8218 {
8219 /* Global Variable.
8220 Don't enter into the minimal symbol tables as there is
8221 a minimal symbol table entry from the ELF symbols already.
8222 Enter into partial symbol table if it has a location
8223 descriptor or a type.
8224 If the location descriptor is missing, new_symbol will create
8225 a LOC_UNRESOLVED symbol, the address of the variable will then
8226 be determined from the minimal symbol table whenever the variable
8227 is referenced.
8228 The address for the partial symbol table entry is not
8229 used by GDB, but it comes in handy for debugging partial symbol
8230 table building. */
8231
8232 if (pdi->d.locdesc || pdi->has_type)
8233 add_psymbol_to_list (actual_name,
8234 built_actual_name != NULL,
8235 VAR_DOMAIN, LOC_STATIC,
8236 SECT_OFF_TEXT (objfile),
8237 psymbol_placement::GLOBAL,
8238 addr, cu->language, objfile);
8239 }
8240 else
8241 {
8242 int has_loc = pdi->d.locdesc != NULL;
8243
8244 /* Static Variable. Skip symbols whose value we cannot know (those
8245 without location descriptors or constant values). */
8246 if (!has_loc && !pdi->has_const_value)
8247 return;
8248
8249 add_psymbol_to_list (actual_name,
8250 built_actual_name != NULL,
8251 VAR_DOMAIN, LOC_STATIC,
8252 SECT_OFF_TEXT (objfile),
8253 psymbol_placement::STATIC,
8254 has_loc ? addr : 0,
8255 cu->language, objfile);
8256 }
8257 break;
8258 case DW_TAG_typedef:
8259 case DW_TAG_base_type:
8260 case DW_TAG_subrange_type:
8261 add_psymbol_to_list (actual_name,
8262 built_actual_name != NULL,
8263 VAR_DOMAIN, LOC_TYPEDEF, -1,
8264 psymbol_placement::STATIC,
8265 0, cu->language, objfile);
8266 break;
8267 case DW_TAG_imported_declaration:
8268 case DW_TAG_namespace:
8269 add_psymbol_to_list (actual_name,
8270 built_actual_name != NULL,
8271 VAR_DOMAIN, LOC_TYPEDEF, -1,
8272 psymbol_placement::GLOBAL,
8273 0, cu->language, objfile);
8274 break;
8275 case DW_TAG_module:
8276 /* With Fortran 77 there might be a "BLOCK DATA" module
8277 available without any name. If so, we skip the module as it
8278 doesn't bring any value. */
8279 if (actual_name != nullptr)
8280 add_psymbol_to_list (actual_name,
8281 built_actual_name != NULL,
8282 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8283 psymbol_placement::GLOBAL,
8284 0, cu->language, objfile);
8285 break;
8286 case DW_TAG_class_type:
8287 case DW_TAG_interface_type:
8288 case DW_TAG_structure_type:
8289 case DW_TAG_union_type:
8290 case DW_TAG_enumeration_type:
8291 /* Skip external references. The DWARF standard says in the section
8292 about "Structure, Union, and Class Type Entries": "An incomplete
8293 structure, union or class type is represented by a structure,
8294 union or class entry that does not have a byte size attribute
8295 and that has a DW_AT_declaration attribute." */
8296 if (!pdi->has_byte_size && pdi->is_declaration)
8297 return;
8298
8299 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8300 static vs. global. */
8301 add_psymbol_to_list (actual_name,
8302 built_actual_name != NULL,
8303 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8304 cu->language == language_cplus
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC,
8307 0, cu->language, objfile);
8308
8309 break;
8310 case DW_TAG_enumerator:
8311 add_psymbol_to_list (actual_name,
8312 built_actual_name != NULL,
8313 VAR_DOMAIN, LOC_CONST, -1,
8314 cu->language == language_cplus
8315 ? psymbol_placement::GLOBAL
8316 : psymbol_placement::STATIC,
8317 0, cu->language, objfile);
8318 break;
8319 default:
8320 break;
8321 }
8322 }
8323
8324 /* Read a partial die corresponding to a namespace; also, add a symbol
8325 corresponding to that namespace to the symbol table. NAMESPACE is
8326 the name of the enclosing namespace. */
8327
8328 static void
8329 add_partial_namespace (struct partial_die_info *pdi,
8330 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8331 int set_addrmap, struct dwarf2_cu *cu)
8332 {
8333 /* Add a symbol for the namespace. */
8334
8335 add_partial_symbol (pdi, cu);
8336
8337 /* Now scan partial symbols in that namespace. */
8338
8339 if (pdi->has_children)
8340 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8341 }
8342
8343 /* Read a partial die corresponding to a Fortran module. */
8344
8345 static void
8346 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8347 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8348 {
8349 /* Add a symbol for the namespace. */
8350
8351 add_partial_symbol (pdi, cu);
8352
8353 /* Now scan partial symbols in that module. */
8354
8355 if (pdi->has_children)
8356 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8357 }
8358
8359 /* Read a partial die corresponding to a subprogram or an inlined
8360 subprogram and create a partial symbol for that subprogram.
8361 When the CU language allows it, this routine also defines a partial
8362 symbol for each nested subprogram that this subprogram contains.
8363 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8364 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8365
8366 PDI may also be a lexical block, in which case we simply search
8367 recursively for subprograms defined inside that lexical block.
8368 Again, this is only performed when the CU language allows this
8369 type of definitions. */
8370
8371 static void
8372 add_partial_subprogram (struct partial_die_info *pdi,
8373 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8374 int set_addrmap, struct dwarf2_cu *cu)
8375 {
8376 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8377 {
8378 if (pdi->has_pc_info)
8379 {
8380 if (pdi->lowpc < *lowpc)
8381 *lowpc = pdi->lowpc;
8382 if (pdi->highpc > *highpc)
8383 *highpc = pdi->highpc;
8384 if (set_addrmap)
8385 {
8386 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8387 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8388 CORE_ADDR baseaddr;
8389 CORE_ADDR this_highpc;
8390 CORE_ADDR this_lowpc;
8391
8392 baseaddr = objfile->text_section_offset ();
8393 this_lowpc
8394 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8395 pdi->lowpc + baseaddr)
8396 - baseaddr);
8397 this_highpc
8398 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8399 pdi->highpc + baseaddr)
8400 - baseaddr);
8401 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8402 this_lowpc, this_highpc - 1,
8403 cu->per_cu->v.psymtab);
8404 }
8405 }
8406
8407 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8408 {
8409 if (!pdi->is_declaration)
8410 /* Ignore subprogram DIEs that do not have a name, they are
8411 illegal. Do not emit a complaint at this point, we will
8412 do so when we convert this psymtab into a symtab. */
8413 if (pdi->name)
8414 add_partial_symbol (pdi, cu);
8415 }
8416 }
8417
8418 if (! pdi->has_children)
8419 return;
8420
8421 if (cu->language == language_ada || cu->language == language_fortran)
8422 {
8423 pdi = pdi->die_child;
8424 while (pdi != NULL)
8425 {
8426 pdi->fixup (cu);
8427 if (pdi->tag == DW_TAG_subprogram
8428 || pdi->tag == DW_TAG_inlined_subroutine
8429 || pdi->tag == DW_TAG_lexical_block)
8430 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8431 pdi = pdi->die_sibling;
8432 }
8433 }
8434 }
8435
8436 /* Read a partial die corresponding to an enumeration type. */
8437
8438 static void
8439 add_partial_enumeration (struct partial_die_info *enum_pdi,
8440 struct dwarf2_cu *cu)
8441 {
8442 struct partial_die_info *pdi;
8443
8444 if (enum_pdi->name != NULL)
8445 add_partial_symbol (enum_pdi, cu);
8446
8447 pdi = enum_pdi->die_child;
8448 while (pdi)
8449 {
8450 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8451 complaint (_("malformed enumerator DIE ignored"));
8452 else
8453 add_partial_symbol (pdi, cu);
8454 pdi = pdi->die_sibling;
8455 }
8456 }
8457
8458 /* Return the initial uleb128 in the die at INFO_PTR. */
8459
8460 static unsigned int
8461 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8462 {
8463 unsigned int bytes_read;
8464
8465 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8466 }
8467
8468 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8469 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8470
8471 Return the corresponding abbrev, or NULL if the number is zero (indicating
8472 an empty DIE). In either case *BYTES_READ will be set to the length of
8473 the initial number. */
8474
8475 static struct abbrev_info *
8476 peek_die_abbrev (const die_reader_specs &reader,
8477 const gdb_byte *info_ptr, unsigned int *bytes_read)
8478 {
8479 dwarf2_cu *cu = reader.cu;
8480 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8481 unsigned int abbrev_number
8482 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8483
8484 if (abbrev_number == 0)
8485 return NULL;
8486
8487 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8488 if (!abbrev)
8489 {
8490 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8491 " at offset %s [in module %s]"),
8492 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8493 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8494 }
8495
8496 return abbrev;
8497 }
8498
8499 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8500 Returns a pointer to the end of a series of DIEs, terminated by an empty
8501 DIE. Any children of the skipped DIEs will also be skipped. */
8502
8503 static const gdb_byte *
8504 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8505 {
8506 while (1)
8507 {
8508 unsigned int bytes_read;
8509 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8510
8511 if (abbrev == NULL)
8512 return info_ptr + bytes_read;
8513 else
8514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8515 }
8516 }
8517
8518 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8519 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8520 abbrev corresponding to that skipped uleb128 should be passed in
8521 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8522 children. */
8523
8524 static const gdb_byte *
8525 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8526 struct abbrev_info *abbrev)
8527 {
8528 unsigned int bytes_read;
8529 struct attribute attr;
8530 bfd *abfd = reader->abfd;
8531 struct dwarf2_cu *cu = reader->cu;
8532 const gdb_byte *buffer = reader->buffer;
8533 const gdb_byte *buffer_end = reader->buffer_end;
8534 unsigned int form, i;
8535
8536 for (i = 0; i < abbrev->num_attrs; i++)
8537 {
8538 /* The only abbrev we care about is DW_AT_sibling. */
8539 if (abbrev->attrs[i].name == DW_AT_sibling)
8540 {
8541 bool ignored;
8542 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8543 &ignored);
8544 if (attr.form == DW_FORM_ref_addr)
8545 complaint (_("ignoring absolute DW_AT_sibling"));
8546 else
8547 {
8548 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8549 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8550
8551 if (sibling_ptr < info_ptr)
8552 complaint (_("DW_AT_sibling points backwards"));
8553 else if (sibling_ptr > reader->buffer_end)
8554 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8555 else
8556 return sibling_ptr;
8557 }
8558 }
8559
8560 /* If it isn't DW_AT_sibling, skip this attribute. */
8561 form = abbrev->attrs[i].form;
8562 skip_attribute:
8563 switch (form)
8564 {
8565 case DW_FORM_ref_addr:
8566 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8567 and later it is offset sized. */
8568 if (cu->header.version == 2)
8569 info_ptr += cu->header.addr_size;
8570 else
8571 info_ptr += cu->header.offset_size;
8572 break;
8573 case DW_FORM_GNU_ref_alt:
8574 info_ptr += cu->header.offset_size;
8575 break;
8576 case DW_FORM_addr:
8577 info_ptr += cu->header.addr_size;
8578 break;
8579 case DW_FORM_data1:
8580 case DW_FORM_ref1:
8581 case DW_FORM_flag:
8582 case DW_FORM_strx1:
8583 info_ptr += 1;
8584 break;
8585 case DW_FORM_flag_present:
8586 case DW_FORM_implicit_const:
8587 break;
8588 case DW_FORM_data2:
8589 case DW_FORM_ref2:
8590 case DW_FORM_strx2:
8591 info_ptr += 2;
8592 break;
8593 case DW_FORM_strx3:
8594 info_ptr += 3;
8595 break;
8596 case DW_FORM_data4:
8597 case DW_FORM_ref4:
8598 case DW_FORM_strx4:
8599 info_ptr += 4;
8600 break;
8601 case DW_FORM_data8:
8602 case DW_FORM_ref8:
8603 case DW_FORM_ref_sig8:
8604 info_ptr += 8;
8605 break;
8606 case DW_FORM_data16:
8607 info_ptr += 16;
8608 break;
8609 case DW_FORM_string:
8610 read_direct_string (abfd, info_ptr, &bytes_read);
8611 info_ptr += bytes_read;
8612 break;
8613 case DW_FORM_sec_offset:
8614 case DW_FORM_strp:
8615 case DW_FORM_GNU_strp_alt:
8616 info_ptr += cu->header.offset_size;
8617 break;
8618 case DW_FORM_exprloc:
8619 case DW_FORM_block:
8620 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8621 info_ptr += bytes_read;
8622 break;
8623 case DW_FORM_block1:
8624 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8625 break;
8626 case DW_FORM_block2:
8627 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8628 break;
8629 case DW_FORM_block4:
8630 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8631 break;
8632 case DW_FORM_addrx:
8633 case DW_FORM_strx:
8634 case DW_FORM_sdata:
8635 case DW_FORM_udata:
8636 case DW_FORM_ref_udata:
8637 case DW_FORM_GNU_addr_index:
8638 case DW_FORM_GNU_str_index:
8639 case DW_FORM_rnglistx:
8640 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8641 break;
8642 case DW_FORM_indirect:
8643 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8644 info_ptr += bytes_read;
8645 /* We need to continue parsing from here, so just go back to
8646 the top. */
8647 goto skip_attribute;
8648
8649 default:
8650 error (_("Dwarf Error: Cannot handle %s "
8651 "in DWARF reader [in module %s]"),
8652 dwarf_form_name (form),
8653 bfd_get_filename (abfd));
8654 }
8655 }
8656
8657 if (abbrev->has_children)
8658 return skip_children (reader, info_ptr);
8659 else
8660 return info_ptr;
8661 }
8662
8663 /* Locate ORIG_PDI's sibling.
8664 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8665
8666 static const gdb_byte *
8667 locate_pdi_sibling (const struct die_reader_specs *reader,
8668 struct partial_die_info *orig_pdi,
8669 const gdb_byte *info_ptr)
8670 {
8671 /* Do we know the sibling already? */
8672
8673 if (orig_pdi->sibling)
8674 return orig_pdi->sibling;
8675
8676 /* Are there any children to deal with? */
8677
8678 if (!orig_pdi->has_children)
8679 return info_ptr;
8680
8681 /* Skip the children the long way. */
8682
8683 return skip_children (reader, info_ptr);
8684 }
8685
8686 /* Expand this partial symbol table into a full symbol table. SELF is
8687 not NULL. */
8688
8689 void
8690 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8691 {
8692 struct dwarf2_per_objfile *dwarf2_per_objfile
8693 = get_dwarf2_per_objfile (objfile);
8694
8695 gdb_assert (!readin);
8696 /* If this psymtab is constructed from a debug-only objfile, the
8697 has_section_at_zero flag will not necessarily be correct. We
8698 can get the correct value for this flag by looking at the data
8699 associated with the (presumably stripped) associated objfile. */
8700 if (objfile->separate_debug_objfile_backlink)
8701 {
8702 struct dwarf2_per_objfile *dpo_backlink
8703 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8704
8705 dwarf2_per_objfile->has_section_at_zero
8706 = dpo_backlink->has_section_at_zero;
8707 }
8708
8709 dwarf2_per_objfile->reading_partial_symbols = 0;
8710
8711 expand_psymtab (objfile);
8712
8713 process_cu_includes (dwarf2_per_objfile);
8714 }
8715 \f
8716 /* Reading in full CUs. */
8717
8718 /* Add PER_CU to the queue. */
8719
8720 static void
8721 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8722 enum language pretend_language)
8723 {
8724 per_cu->queued = 1;
8725 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8726 }
8727
8728 /* If PER_CU is not yet queued, add it to the queue.
8729 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8730 dependency.
8731 The result is non-zero if PER_CU was queued, otherwise the result is zero
8732 meaning either PER_CU is already queued or it is already loaded.
8733
8734 N.B. There is an invariant here that if a CU is queued then it is loaded.
8735 The caller is required to load PER_CU if we return non-zero. */
8736
8737 static int
8738 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8739 struct dwarf2_per_cu_data *per_cu,
8740 enum language pretend_language)
8741 {
8742 /* We may arrive here during partial symbol reading, if we need full
8743 DIEs to process an unusual case (e.g. template arguments). Do
8744 not queue PER_CU, just tell our caller to load its DIEs. */
8745 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8746 {
8747 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8748 return 1;
8749 return 0;
8750 }
8751
8752 /* Mark the dependence relation so that we don't flush PER_CU
8753 too early. */
8754 if (dependent_cu != NULL)
8755 dwarf2_add_dependence (dependent_cu, per_cu);
8756
8757 /* If it's already on the queue, we have nothing to do. */
8758 if (per_cu->queued)
8759 return 0;
8760
8761 /* If the compilation unit is already loaded, just mark it as
8762 used. */
8763 if (per_cu->cu != NULL)
8764 {
8765 per_cu->cu->last_used = 0;
8766 return 0;
8767 }
8768
8769 /* Add it to the queue. */
8770 queue_comp_unit (per_cu, pretend_language);
8771
8772 return 1;
8773 }
8774
8775 /* Process the queue. */
8776
8777 static void
8778 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8779 {
8780 if (dwarf_read_debug)
8781 {
8782 fprintf_unfiltered (gdb_stdlog,
8783 "Expanding one or more symtabs of objfile %s ...\n",
8784 objfile_name (dwarf2_per_objfile->objfile));
8785 }
8786
8787 /* The queue starts out with one item, but following a DIE reference
8788 may load a new CU, adding it to the end of the queue. */
8789 while (!dwarf2_per_objfile->queue.empty ())
8790 {
8791 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8792
8793 if ((dwarf2_per_objfile->using_index
8794 ? !item.per_cu->v.quick->compunit_symtab
8795 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8796 /* Skip dummy CUs. */
8797 && item.per_cu->cu != NULL)
8798 {
8799 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8800 unsigned int debug_print_threshold;
8801 char buf[100];
8802
8803 if (per_cu->is_debug_types)
8804 {
8805 struct signatured_type *sig_type =
8806 (struct signatured_type *) per_cu;
8807
8808 sprintf (buf, "TU %s at offset %s",
8809 hex_string (sig_type->signature),
8810 sect_offset_str (per_cu->sect_off));
8811 /* There can be 100s of TUs.
8812 Only print them in verbose mode. */
8813 debug_print_threshold = 2;
8814 }
8815 else
8816 {
8817 sprintf (buf, "CU at offset %s",
8818 sect_offset_str (per_cu->sect_off));
8819 debug_print_threshold = 1;
8820 }
8821
8822 if (dwarf_read_debug >= debug_print_threshold)
8823 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8824
8825 if (per_cu->is_debug_types)
8826 process_full_type_unit (per_cu, item.pretend_language);
8827 else
8828 process_full_comp_unit (per_cu, item.pretend_language);
8829
8830 if (dwarf_read_debug >= debug_print_threshold)
8831 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8832 }
8833
8834 item.per_cu->queued = 0;
8835 dwarf2_per_objfile->queue.pop ();
8836 }
8837
8838 if (dwarf_read_debug)
8839 {
8840 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8841 objfile_name (dwarf2_per_objfile->objfile));
8842 }
8843 }
8844
8845 /* Read in full symbols for PST, and anything it depends on. */
8846
8847 void
8848 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8849 {
8850 struct dwarf2_per_cu_data *per_cu;
8851
8852 if (readin)
8853 return;
8854
8855 read_dependencies (objfile);
8856
8857 per_cu = per_cu_data;
8858
8859 if (per_cu == NULL)
8860 {
8861 /* It's an include file, no symbols to read for it.
8862 Everything is in the parent symtab. */
8863 readin = true;
8864 return;
8865 }
8866
8867 dw2_do_instantiate_symtab (per_cu, false);
8868 }
8869
8870 /* Trivial hash function for die_info: the hash value of a DIE
8871 is its offset in .debug_info for this objfile. */
8872
8873 static hashval_t
8874 die_hash (const void *item)
8875 {
8876 const struct die_info *die = (const struct die_info *) item;
8877
8878 return to_underlying (die->sect_off);
8879 }
8880
8881 /* Trivial comparison function for die_info structures: two DIEs
8882 are equal if they have the same offset. */
8883
8884 static int
8885 die_eq (const void *item_lhs, const void *item_rhs)
8886 {
8887 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8888 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8889
8890 return die_lhs->sect_off == die_rhs->sect_off;
8891 }
8892
8893 /* Load the DIEs associated with PER_CU into memory. */
8894
8895 static void
8896 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8897 bool skip_partial,
8898 enum language pretend_language)
8899 {
8900 gdb_assert (! this_cu->is_debug_types);
8901
8902 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8903 if (reader.dummy_p)
8904 return;
8905
8906 struct dwarf2_cu *cu = reader.cu;
8907 const gdb_byte *info_ptr = reader.info_ptr;
8908
8909 gdb_assert (cu->die_hash == NULL);
8910 cu->die_hash =
8911 htab_create_alloc_ex (cu->header.length / 12,
8912 die_hash,
8913 die_eq,
8914 NULL,
8915 &cu->comp_unit_obstack,
8916 hashtab_obstack_allocate,
8917 dummy_obstack_deallocate);
8918
8919 if (reader.comp_unit_die->has_children)
8920 reader.comp_unit_die->child
8921 = read_die_and_siblings (&reader, reader.info_ptr,
8922 &info_ptr, reader.comp_unit_die);
8923 cu->dies = reader.comp_unit_die;
8924 /* comp_unit_die is not stored in die_hash, no need. */
8925
8926 /* We try not to read any attributes in this function, because not
8927 all CUs needed for references have been loaded yet, and symbol
8928 table processing isn't initialized. But we have to set the CU language,
8929 or we won't be able to build types correctly.
8930 Similarly, if we do not read the producer, we can not apply
8931 producer-specific interpretation. */
8932 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8933
8934 reader.keep ();
8935 }
8936
8937 /* Add a DIE to the delayed physname list. */
8938
8939 static void
8940 add_to_method_list (struct type *type, int fnfield_index, int index,
8941 const char *name, struct die_info *die,
8942 struct dwarf2_cu *cu)
8943 {
8944 struct delayed_method_info mi;
8945 mi.type = type;
8946 mi.fnfield_index = fnfield_index;
8947 mi.index = index;
8948 mi.name = name;
8949 mi.die = die;
8950 cu->method_list.push_back (mi);
8951 }
8952
8953 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8954 "const" / "volatile". If so, decrements LEN by the length of the
8955 modifier and return true. Otherwise return false. */
8956
8957 template<size_t N>
8958 static bool
8959 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8960 {
8961 size_t mod_len = sizeof (mod) - 1;
8962 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8963 {
8964 len -= mod_len;
8965 return true;
8966 }
8967 return false;
8968 }
8969
8970 /* Compute the physnames of any methods on the CU's method list.
8971
8972 The computation of method physnames is delayed in order to avoid the
8973 (bad) condition that one of the method's formal parameters is of an as yet
8974 incomplete type. */
8975
8976 static void
8977 compute_delayed_physnames (struct dwarf2_cu *cu)
8978 {
8979 /* Only C++ delays computing physnames. */
8980 if (cu->method_list.empty ())
8981 return;
8982 gdb_assert (cu->language == language_cplus);
8983
8984 for (const delayed_method_info &mi : cu->method_list)
8985 {
8986 const char *physname;
8987 struct fn_fieldlist *fn_flp
8988 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8989 physname = dwarf2_physname (mi.name, mi.die, cu);
8990 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8991 = physname ? physname : "";
8992
8993 /* Since there's no tag to indicate whether a method is a
8994 const/volatile overload, extract that information out of the
8995 demangled name. */
8996 if (physname != NULL)
8997 {
8998 size_t len = strlen (physname);
8999
9000 while (1)
9001 {
9002 if (physname[len] == ')') /* shortcut */
9003 break;
9004 else if (check_modifier (physname, len, " const"))
9005 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9006 else if (check_modifier (physname, len, " volatile"))
9007 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9008 else
9009 break;
9010 }
9011 }
9012 }
9013
9014 /* The list is no longer needed. */
9015 cu->method_list.clear ();
9016 }
9017
9018 /* Go objects should be embedded in a DW_TAG_module DIE,
9019 and it's not clear if/how imported objects will appear.
9020 To keep Go support simple until that's worked out,
9021 go back through what we've read and create something usable.
9022 We could do this while processing each DIE, and feels kinda cleaner,
9023 but that way is more invasive.
9024 This is to, for example, allow the user to type "p var" or "b main"
9025 without having to specify the package name, and allow lookups
9026 of module.object to work in contexts that use the expression
9027 parser. */
9028
9029 static void
9030 fixup_go_packaging (struct dwarf2_cu *cu)
9031 {
9032 gdb::unique_xmalloc_ptr<char> package_name;
9033 struct pending *list;
9034 int i;
9035
9036 for (list = *cu->get_builder ()->get_global_symbols ();
9037 list != NULL;
9038 list = list->next)
9039 {
9040 for (i = 0; i < list->nsyms; ++i)
9041 {
9042 struct symbol *sym = list->symbol[i];
9043
9044 if (sym->language () == language_go
9045 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9046 {
9047 gdb::unique_xmalloc_ptr<char> this_package_name
9048 (go_symbol_package_name (sym));
9049
9050 if (this_package_name == NULL)
9051 continue;
9052 if (package_name == NULL)
9053 package_name = std::move (this_package_name);
9054 else
9055 {
9056 struct objfile *objfile
9057 = cu->per_cu->dwarf2_per_objfile->objfile;
9058 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9059 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9060 (symbol_symtab (sym) != NULL
9061 ? symtab_to_filename_for_display
9062 (symbol_symtab (sym))
9063 : objfile_name (objfile)),
9064 this_package_name.get (), package_name.get ());
9065 }
9066 }
9067 }
9068 }
9069
9070 if (package_name != NULL)
9071 {
9072 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9073 const char *saved_package_name
9074 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9075 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9076 saved_package_name);
9077 struct symbol *sym;
9078
9079 sym = allocate_symbol (objfile);
9080 sym->set_language (language_go, &objfile->objfile_obstack);
9081 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9082 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9083 e.g., "main" finds the "main" module and not C's main(). */
9084 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9085 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9086 SYMBOL_TYPE (sym) = type;
9087
9088 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9089 }
9090 }
9091
9092 /* Allocate a fully-qualified name consisting of the two parts on the
9093 obstack. */
9094
9095 static const char *
9096 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9097 {
9098 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9099 }
9100
9101 /* A helper that allocates a struct discriminant_info to attach to a
9102 union type. */
9103
9104 static struct discriminant_info *
9105 alloc_discriminant_info (struct type *type, int discriminant_index,
9106 int default_index)
9107 {
9108 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9109 gdb_assert (discriminant_index == -1
9110 || (discriminant_index >= 0
9111 && discriminant_index < TYPE_NFIELDS (type)));
9112 gdb_assert (default_index == -1
9113 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9114
9115 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9116
9117 struct discriminant_info *disc
9118 = ((struct discriminant_info *)
9119 TYPE_ZALLOC (type,
9120 offsetof (struct discriminant_info, discriminants)
9121 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9122 disc->default_index = default_index;
9123 disc->discriminant_index = discriminant_index;
9124
9125 struct dynamic_prop prop;
9126 prop.kind = PROP_UNDEFINED;
9127 prop.data.baton = disc;
9128
9129 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9130
9131 return disc;
9132 }
9133
9134 /* Some versions of rustc emitted enums in an unusual way.
9135
9136 Ordinary enums were emitted as unions. The first element of each
9137 structure in the union was named "RUST$ENUM$DISR". This element
9138 held the discriminant.
9139
9140 These versions of Rust also implemented the "non-zero"
9141 optimization. When the enum had two values, and one is empty and
9142 the other holds a pointer that cannot be zero, the pointer is used
9143 as the discriminant, with a zero value meaning the empty variant.
9144 Here, the union's first member is of the form
9145 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9146 where the fieldnos are the indices of the fields that should be
9147 traversed in order to find the field (which may be several fields deep)
9148 and the variantname is the name of the variant of the case when the
9149 field is zero.
9150
9151 This function recognizes whether TYPE is of one of these forms,
9152 and, if so, smashes it to be a variant type. */
9153
9154 static void
9155 quirk_rust_enum (struct type *type, struct objfile *objfile)
9156 {
9157 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9158
9159 /* We don't need to deal with empty enums. */
9160 if (TYPE_NFIELDS (type) == 0)
9161 return;
9162
9163 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9164 if (TYPE_NFIELDS (type) == 1
9165 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9166 {
9167 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9168
9169 /* Decode the field name to find the offset of the
9170 discriminant. */
9171 ULONGEST bit_offset = 0;
9172 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9173 while (name[0] >= '0' && name[0] <= '9')
9174 {
9175 char *tail;
9176 unsigned long index = strtoul (name, &tail, 10);
9177 name = tail;
9178 if (*name != '$'
9179 || index >= TYPE_NFIELDS (field_type)
9180 || (TYPE_FIELD_LOC_KIND (field_type, index)
9181 != FIELD_LOC_KIND_BITPOS))
9182 {
9183 complaint (_("Could not parse Rust enum encoding string \"%s\""
9184 "[in module %s]"),
9185 TYPE_FIELD_NAME (type, 0),
9186 objfile_name (objfile));
9187 return;
9188 }
9189 ++name;
9190
9191 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9192 field_type = TYPE_FIELD_TYPE (field_type, index);
9193 }
9194
9195 /* Make a union to hold the variants. */
9196 struct type *union_type = alloc_type (objfile);
9197 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9198 TYPE_NFIELDS (union_type) = 3;
9199 TYPE_FIELDS (union_type)
9200 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9201 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9202 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9203
9204 /* Put the discriminant must at index 0. */
9205 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9206 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9207 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9208 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9209
9210 /* The order of fields doesn't really matter, so put the real
9211 field at index 1 and the data-less field at index 2. */
9212 struct discriminant_info *disc
9213 = alloc_discriminant_info (union_type, 0, 1);
9214 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9215 TYPE_FIELD_NAME (union_type, 1)
9216 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9217 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9218 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9219 TYPE_FIELD_NAME (union_type, 1));
9220
9221 const char *dataless_name
9222 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9223 name);
9224 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9225 dataless_name);
9226 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9227 /* NAME points into the original discriminant name, which
9228 already has the correct lifetime. */
9229 TYPE_FIELD_NAME (union_type, 2) = name;
9230 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9231 disc->discriminants[2] = 0;
9232
9233 /* Smash this type to be a structure type. We have to do this
9234 because the type has already been recorded. */
9235 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9236 TYPE_NFIELDS (type) = 1;
9237 TYPE_FIELDS (type)
9238 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9239
9240 /* Install the variant part. */
9241 TYPE_FIELD_TYPE (type, 0) = union_type;
9242 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9243 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9244 }
9245 /* A union with a single anonymous field is probably an old-style
9246 univariant enum. */
9247 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9248 {
9249 /* Smash this type to be a structure type. We have to do this
9250 because the type has already been recorded. */
9251 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9252
9253 /* Make a union to hold the variants. */
9254 struct type *union_type = alloc_type (objfile);
9255 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9256 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9257 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9258 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9259 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9260
9261 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9262 const char *variant_name
9263 = rust_last_path_segment (TYPE_NAME (field_type));
9264 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9265 TYPE_NAME (field_type)
9266 = rust_fully_qualify (&objfile->objfile_obstack,
9267 TYPE_NAME (type), variant_name);
9268
9269 /* Install the union in the outer struct type. */
9270 TYPE_NFIELDS (type) = 1;
9271 TYPE_FIELDS (type)
9272 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9273 TYPE_FIELD_TYPE (type, 0) = union_type;
9274 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9275 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9276
9277 alloc_discriminant_info (union_type, -1, 0);
9278 }
9279 else
9280 {
9281 struct type *disr_type = nullptr;
9282 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9283 {
9284 disr_type = TYPE_FIELD_TYPE (type, i);
9285
9286 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9287 {
9288 /* All fields of a true enum will be structs. */
9289 return;
9290 }
9291 else if (TYPE_NFIELDS (disr_type) == 0)
9292 {
9293 /* Could be data-less variant, so keep going. */
9294 disr_type = nullptr;
9295 }
9296 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9297 "RUST$ENUM$DISR") != 0)
9298 {
9299 /* Not a Rust enum. */
9300 return;
9301 }
9302 else
9303 {
9304 /* Found one. */
9305 break;
9306 }
9307 }
9308
9309 /* If we got here without a discriminant, then it's probably
9310 just a union. */
9311 if (disr_type == nullptr)
9312 return;
9313
9314 /* Smash this type to be a structure type. We have to do this
9315 because the type has already been recorded. */
9316 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9317
9318 /* Make a union to hold the variants. */
9319 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9320 struct type *union_type = alloc_type (objfile);
9321 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9322 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9323 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9324 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9325 TYPE_FIELDS (union_type)
9326 = (struct field *) TYPE_ZALLOC (union_type,
9327 (TYPE_NFIELDS (union_type)
9328 * sizeof (struct field)));
9329
9330 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9331 TYPE_NFIELDS (type) * sizeof (struct field));
9332
9333 /* Install the discriminant at index 0 in the union. */
9334 TYPE_FIELD (union_type, 0) = *disr_field;
9335 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9336 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9337
9338 /* Install the union in the outer struct type. */
9339 TYPE_FIELD_TYPE (type, 0) = union_type;
9340 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9341 TYPE_NFIELDS (type) = 1;
9342
9343 /* Set the size and offset of the union type. */
9344 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9345
9346 /* We need a way to find the correct discriminant given a
9347 variant name. For convenience we build a map here. */
9348 struct type *enum_type = FIELD_TYPE (*disr_field);
9349 std::unordered_map<std::string, ULONGEST> discriminant_map;
9350 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9351 {
9352 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9353 {
9354 const char *name
9355 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9356 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9357 }
9358 }
9359
9360 int n_fields = TYPE_NFIELDS (union_type);
9361 struct discriminant_info *disc
9362 = alloc_discriminant_info (union_type, 0, -1);
9363 /* Skip the discriminant here. */
9364 for (int i = 1; i < n_fields; ++i)
9365 {
9366 /* Find the final word in the name of this variant's type.
9367 That name can be used to look up the correct
9368 discriminant. */
9369 const char *variant_name
9370 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9371 i)));
9372
9373 auto iter = discriminant_map.find (variant_name);
9374 if (iter != discriminant_map.end ())
9375 disc->discriminants[i] = iter->second;
9376
9377 /* Remove the discriminant field, if it exists. */
9378 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9379 if (TYPE_NFIELDS (sub_type) > 0)
9380 {
9381 --TYPE_NFIELDS (sub_type);
9382 ++TYPE_FIELDS (sub_type);
9383 }
9384 TYPE_FIELD_NAME (union_type, i) = variant_name;
9385 TYPE_NAME (sub_type)
9386 = rust_fully_qualify (&objfile->objfile_obstack,
9387 TYPE_NAME (type), variant_name);
9388 }
9389 }
9390 }
9391
9392 /* Rewrite some Rust unions to be structures with variants parts. */
9393
9394 static void
9395 rust_union_quirks (struct dwarf2_cu *cu)
9396 {
9397 gdb_assert (cu->language == language_rust);
9398 for (type *type_ : cu->rust_unions)
9399 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9400 /* We don't need this any more. */
9401 cu->rust_unions.clear ();
9402 }
9403
9404 /* Return the symtab for PER_CU. This works properly regardless of
9405 whether we're using the index or psymtabs. */
9406
9407 static struct compunit_symtab *
9408 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9409 {
9410 return (per_cu->dwarf2_per_objfile->using_index
9411 ? per_cu->v.quick->compunit_symtab
9412 : per_cu->v.psymtab->compunit_symtab);
9413 }
9414
9415 /* A helper function for computing the list of all symbol tables
9416 included by PER_CU. */
9417
9418 static void
9419 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9420 htab_t all_children, htab_t all_type_symtabs,
9421 struct dwarf2_per_cu_data *per_cu,
9422 struct compunit_symtab *immediate_parent)
9423 {
9424 void **slot;
9425 struct compunit_symtab *cust;
9426
9427 slot = htab_find_slot (all_children, per_cu, INSERT);
9428 if (*slot != NULL)
9429 {
9430 /* This inclusion and its children have been processed. */
9431 return;
9432 }
9433
9434 *slot = per_cu;
9435 /* Only add a CU if it has a symbol table. */
9436 cust = get_compunit_symtab (per_cu);
9437 if (cust != NULL)
9438 {
9439 /* If this is a type unit only add its symbol table if we haven't
9440 seen it yet (type unit per_cu's can share symtabs). */
9441 if (per_cu->is_debug_types)
9442 {
9443 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9444 if (*slot == NULL)
9445 {
9446 *slot = cust;
9447 result->push_back (cust);
9448 if (cust->user == NULL)
9449 cust->user = immediate_parent;
9450 }
9451 }
9452 else
9453 {
9454 result->push_back (cust);
9455 if (cust->user == NULL)
9456 cust->user = immediate_parent;
9457 }
9458 }
9459
9460 if (!per_cu->imported_symtabs_empty ())
9461 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9462 {
9463 recursively_compute_inclusions (result, all_children,
9464 all_type_symtabs, ptr, cust);
9465 }
9466 }
9467
9468 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9469 PER_CU. */
9470
9471 static void
9472 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9473 {
9474 gdb_assert (! per_cu->is_debug_types);
9475
9476 if (!per_cu->imported_symtabs_empty ())
9477 {
9478 int len;
9479 std::vector<compunit_symtab *> result_symtabs;
9480 htab_t all_children, all_type_symtabs;
9481 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9482
9483 /* If we don't have a symtab, we can just skip this case. */
9484 if (cust == NULL)
9485 return;
9486
9487 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9488 NULL, xcalloc, xfree);
9489 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9490 NULL, xcalloc, xfree);
9491
9492 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9493 {
9494 recursively_compute_inclusions (&result_symtabs, all_children,
9495 all_type_symtabs, ptr, cust);
9496 }
9497
9498 /* Now we have a transitive closure of all the included symtabs. */
9499 len = result_symtabs.size ();
9500 cust->includes
9501 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9502 struct compunit_symtab *, len + 1);
9503 memcpy (cust->includes, result_symtabs.data (),
9504 len * sizeof (compunit_symtab *));
9505 cust->includes[len] = NULL;
9506
9507 htab_delete (all_children);
9508 htab_delete (all_type_symtabs);
9509 }
9510 }
9511
9512 /* Compute the 'includes' field for the symtabs of all the CUs we just
9513 read. */
9514
9515 static void
9516 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9517 {
9518 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9519 {
9520 if (! iter->is_debug_types)
9521 compute_compunit_symtab_includes (iter);
9522 }
9523
9524 dwarf2_per_objfile->just_read_cus.clear ();
9525 }
9526
9527 /* Generate full symbol information for PER_CU, whose DIEs have
9528 already been loaded into memory. */
9529
9530 static void
9531 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9532 enum language pretend_language)
9533 {
9534 struct dwarf2_cu *cu = per_cu->cu;
9535 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9536 struct objfile *objfile = dwarf2_per_objfile->objfile;
9537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9538 CORE_ADDR lowpc, highpc;
9539 struct compunit_symtab *cust;
9540 CORE_ADDR baseaddr;
9541 struct block *static_block;
9542 CORE_ADDR addr;
9543
9544 baseaddr = objfile->text_section_offset ();
9545
9546 /* Clear the list here in case something was left over. */
9547 cu->method_list.clear ();
9548
9549 cu->language = pretend_language;
9550 cu->language_defn = language_def (cu->language);
9551
9552 /* Do line number decoding in read_file_scope () */
9553 process_die (cu->dies, cu);
9554
9555 /* For now fudge the Go package. */
9556 if (cu->language == language_go)
9557 fixup_go_packaging (cu);
9558
9559 /* Now that we have processed all the DIEs in the CU, all the types
9560 should be complete, and it should now be safe to compute all of the
9561 physnames. */
9562 compute_delayed_physnames (cu);
9563
9564 if (cu->language == language_rust)
9565 rust_union_quirks (cu);
9566
9567 /* Some compilers don't define a DW_AT_high_pc attribute for the
9568 compilation unit. If the DW_AT_high_pc is missing, synthesize
9569 it, by scanning the DIE's below the compilation unit. */
9570 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9571
9572 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9573 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9574
9575 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9576 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9577 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9578 addrmap to help ensure it has an accurate map of pc values belonging to
9579 this comp unit. */
9580 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9581
9582 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9583 SECT_OFF_TEXT (objfile),
9584 0);
9585
9586 if (cust != NULL)
9587 {
9588 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9589
9590 /* Set symtab language to language from DW_AT_language. If the
9591 compilation is from a C file generated by language preprocessors, do
9592 not set the language if it was already deduced by start_subfile. */
9593 if (!(cu->language == language_c
9594 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9595 COMPUNIT_FILETABS (cust)->language = cu->language;
9596
9597 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9598 produce DW_AT_location with location lists but it can be possibly
9599 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9600 there were bugs in prologue debug info, fixed later in GCC-4.5
9601 by "unwind info for epilogues" patch (which is not directly related).
9602
9603 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9604 needed, it would be wrong due to missing DW_AT_producer there.
9605
9606 Still one can confuse GDB by using non-standard GCC compilation
9607 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9608 */
9609 if (cu->has_loclist && gcc_4_minor >= 5)
9610 cust->locations_valid = 1;
9611
9612 if (gcc_4_minor >= 5)
9613 cust->epilogue_unwind_valid = 1;
9614
9615 cust->call_site_htab = cu->call_site_htab;
9616 }
9617
9618 if (dwarf2_per_objfile->using_index)
9619 per_cu->v.quick->compunit_symtab = cust;
9620 else
9621 {
9622 dwarf2_psymtab *pst = per_cu->v.psymtab;
9623 pst->compunit_symtab = cust;
9624 pst->readin = true;
9625 }
9626
9627 /* Push it for inclusion processing later. */
9628 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9629
9630 /* Not needed any more. */
9631 cu->reset_builder ();
9632 }
9633
9634 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9635 already been loaded into memory. */
9636
9637 static void
9638 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9639 enum language pretend_language)
9640 {
9641 struct dwarf2_cu *cu = per_cu->cu;
9642 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9643 struct objfile *objfile = dwarf2_per_objfile->objfile;
9644 struct compunit_symtab *cust;
9645 struct signatured_type *sig_type;
9646
9647 gdb_assert (per_cu->is_debug_types);
9648 sig_type = (struct signatured_type *) per_cu;
9649
9650 /* Clear the list here in case something was left over. */
9651 cu->method_list.clear ();
9652
9653 cu->language = pretend_language;
9654 cu->language_defn = language_def (cu->language);
9655
9656 /* The symbol tables are set up in read_type_unit_scope. */
9657 process_die (cu->dies, cu);
9658
9659 /* For now fudge the Go package. */
9660 if (cu->language == language_go)
9661 fixup_go_packaging (cu);
9662
9663 /* Now that we have processed all the DIEs in the CU, all the types
9664 should be complete, and it should now be safe to compute all of the
9665 physnames. */
9666 compute_delayed_physnames (cu);
9667
9668 if (cu->language == language_rust)
9669 rust_union_quirks (cu);
9670
9671 /* TUs share symbol tables.
9672 If this is the first TU to use this symtab, complete the construction
9673 of it with end_expandable_symtab. Otherwise, complete the addition of
9674 this TU's symbols to the existing symtab. */
9675 if (sig_type->type_unit_group->compunit_symtab == NULL)
9676 {
9677 buildsym_compunit *builder = cu->get_builder ();
9678 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9679 sig_type->type_unit_group->compunit_symtab = cust;
9680
9681 if (cust != NULL)
9682 {
9683 /* Set symtab language to language from DW_AT_language. If the
9684 compilation is from a C file generated by language preprocessors,
9685 do not set the language if it was already deduced by
9686 start_subfile. */
9687 if (!(cu->language == language_c
9688 && COMPUNIT_FILETABS (cust)->language != language_c))
9689 COMPUNIT_FILETABS (cust)->language = cu->language;
9690 }
9691 }
9692 else
9693 {
9694 cu->get_builder ()->augment_type_symtab ();
9695 cust = sig_type->type_unit_group->compunit_symtab;
9696 }
9697
9698 if (dwarf2_per_objfile->using_index)
9699 per_cu->v.quick->compunit_symtab = cust;
9700 else
9701 {
9702 dwarf2_psymtab *pst = per_cu->v.psymtab;
9703 pst->compunit_symtab = cust;
9704 pst->readin = true;
9705 }
9706
9707 /* Not needed any more. */
9708 cu->reset_builder ();
9709 }
9710
9711 /* Process an imported unit DIE. */
9712
9713 static void
9714 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9715 {
9716 struct attribute *attr;
9717
9718 /* For now we don't handle imported units in type units. */
9719 if (cu->per_cu->is_debug_types)
9720 {
9721 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9722 " supported in type units [in module %s]"),
9723 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9724 }
9725
9726 attr = dwarf2_attr (die, DW_AT_import, cu);
9727 if (attr != NULL)
9728 {
9729 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9730 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9731 dwarf2_per_cu_data *per_cu
9732 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9733 cu->per_cu->dwarf2_per_objfile);
9734
9735 /* If necessary, add it to the queue and load its DIEs. */
9736 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9737 load_full_comp_unit (per_cu, false, cu->language);
9738
9739 cu->per_cu->imported_symtabs_push (per_cu);
9740 }
9741 }
9742
9743 /* RAII object that represents a process_die scope: i.e.,
9744 starts/finishes processing a DIE. */
9745 class process_die_scope
9746 {
9747 public:
9748 process_die_scope (die_info *die, dwarf2_cu *cu)
9749 : m_die (die), m_cu (cu)
9750 {
9751 /* We should only be processing DIEs not already in process. */
9752 gdb_assert (!m_die->in_process);
9753 m_die->in_process = true;
9754 }
9755
9756 ~process_die_scope ()
9757 {
9758 m_die->in_process = false;
9759
9760 /* If we're done processing the DIE for the CU that owns the line
9761 header, we don't need the line header anymore. */
9762 if (m_cu->line_header_die_owner == m_die)
9763 {
9764 delete m_cu->line_header;
9765 m_cu->line_header = NULL;
9766 m_cu->line_header_die_owner = NULL;
9767 }
9768 }
9769
9770 private:
9771 die_info *m_die;
9772 dwarf2_cu *m_cu;
9773 };
9774
9775 /* Process a die and its children. */
9776
9777 static void
9778 process_die (struct die_info *die, struct dwarf2_cu *cu)
9779 {
9780 process_die_scope scope (die, cu);
9781
9782 switch (die->tag)
9783 {
9784 case DW_TAG_padding:
9785 break;
9786 case DW_TAG_compile_unit:
9787 case DW_TAG_partial_unit:
9788 read_file_scope (die, cu);
9789 break;
9790 case DW_TAG_type_unit:
9791 read_type_unit_scope (die, cu);
9792 break;
9793 case DW_TAG_subprogram:
9794 /* Nested subprograms in Fortran get a prefix. */
9795 if (cu->language == language_fortran
9796 && die->parent != NULL
9797 && die->parent->tag == DW_TAG_subprogram)
9798 cu->processing_has_namespace_info = true;
9799 /* Fall through. */
9800 case DW_TAG_inlined_subroutine:
9801 read_func_scope (die, cu);
9802 break;
9803 case DW_TAG_lexical_block:
9804 case DW_TAG_try_block:
9805 case DW_TAG_catch_block:
9806 read_lexical_block_scope (die, cu);
9807 break;
9808 case DW_TAG_call_site:
9809 case DW_TAG_GNU_call_site:
9810 read_call_site_scope (die, cu);
9811 break;
9812 case DW_TAG_class_type:
9813 case DW_TAG_interface_type:
9814 case DW_TAG_structure_type:
9815 case DW_TAG_union_type:
9816 process_structure_scope (die, cu);
9817 break;
9818 case DW_TAG_enumeration_type:
9819 process_enumeration_scope (die, cu);
9820 break;
9821
9822 /* These dies have a type, but processing them does not create
9823 a symbol or recurse to process the children. Therefore we can
9824 read them on-demand through read_type_die. */
9825 case DW_TAG_subroutine_type:
9826 case DW_TAG_set_type:
9827 case DW_TAG_array_type:
9828 case DW_TAG_pointer_type:
9829 case DW_TAG_ptr_to_member_type:
9830 case DW_TAG_reference_type:
9831 case DW_TAG_rvalue_reference_type:
9832 case DW_TAG_string_type:
9833 break;
9834
9835 case DW_TAG_base_type:
9836 case DW_TAG_subrange_type:
9837 case DW_TAG_typedef:
9838 /* Add a typedef symbol for the type definition, if it has a
9839 DW_AT_name. */
9840 new_symbol (die, read_type_die (die, cu), cu);
9841 break;
9842 case DW_TAG_common_block:
9843 read_common_block (die, cu);
9844 break;
9845 case DW_TAG_common_inclusion:
9846 break;
9847 case DW_TAG_namespace:
9848 cu->processing_has_namespace_info = true;
9849 read_namespace (die, cu);
9850 break;
9851 case DW_TAG_module:
9852 cu->processing_has_namespace_info = true;
9853 read_module (die, cu);
9854 break;
9855 case DW_TAG_imported_declaration:
9856 cu->processing_has_namespace_info = true;
9857 if (read_namespace_alias (die, cu))
9858 break;
9859 /* The declaration is not a global namespace alias. */
9860 /* Fall through. */
9861 case DW_TAG_imported_module:
9862 cu->processing_has_namespace_info = true;
9863 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9864 || cu->language != language_fortran))
9865 complaint (_("Tag '%s' has unexpected children"),
9866 dwarf_tag_name (die->tag));
9867 read_import_statement (die, cu);
9868 break;
9869
9870 case DW_TAG_imported_unit:
9871 process_imported_unit_die (die, cu);
9872 break;
9873
9874 case DW_TAG_variable:
9875 read_variable (die, cu);
9876 break;
9877
9878 default:
9879 new_symbol (die, NULL, cu);
9880 break;
9881 }
9882 }
9883 \f
9884 /* DWARF name computation. */
9885
9886 /* A helper function for dwarf2_compute_name which determines whether DIE
9887 needs to have the name of the scope prepended to the name listed in the
9888 die. */
9889
9890 static int
9891 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9892 {
9893 struct attribute *attr;
9894
9895 switch (die->tag)
9896 {
9897 case DW_TAG_namespace:
9898 case DW_TAG_typedef:
9899 case DW_TAG_class_type:
9900 case DW_TAG_interface_type:
9901 case DW_TAG_structure_type:
9902 case DW_TAG_union_type:
9903 case DW_TAG_enumeration_type:
9904 case DW_TAG_enumerator:
9905 case DW_TAG_subprogram:
9906 case DW_TAG_inlined_subroutine:
9907 case DW_TAG_member:
9908 case DW_TAG_imported_declaration:
9909 return 1;
9910
9911 case DW_TAG_variable:
9912 case DW_TAG_constant:
9913 /* We only need to prefix "globally" visible variables. These include
9914 any variable marked with DW_AT_external or any variable that
9915 lives in a namespace. [Variables in anonymous namespaces
9916 require prefixing, but they are not DW_AT_external.] */
9917
9918 if (dwarf2_attr (die, DW_AT_specification, cu))
9919 {
9920 struct dwarf2_cu *spec_cu = cu;
9921
9922 return die_needs_namespace (die_specification (die, &spec_cu),
9923 spec_cu);
9924 }
9925
9926 attr = dwarf2_attr (die, DW_AT_external, cu);
9927 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9928 && die->parent->tag != DW_TAG_module)
9929 return 0;
9930 /* A variable in a lexical block of some kind does not need a
9931 namespace, even though in C++ such variables may be external
9932 and have a mangled name. */
9933 if (die->parent->tag == DW_TAG_lexical_block
9934 || die->parent->tag == DW_TAG_try_block
9935 || die->parent->tag == DW_TAG_catch_block
9936 || die->parent->tag == DW_TAG_subprogram)
9937 return 0;
9938 return 1;
9939
9940 default:
9941 return 0;
9942 }
9943 }
9944
9945 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9946 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9947 defined for the given DIE. */
9948
9949 static struct attribute *
9950 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9951 {
9952 struct attribute *attr;
9953
9954 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9955 if (attr == NULL)
9956 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9957
9958 return attr;
9959 }
9960
9961 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9962 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9963 defined for the given DIE. */
9964
9965 static const char *
9966 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9967 {
9968 const char *linkage_name;
9969
9970 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9971 if (linkage_name == NULL)
9972 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9973
9974 return linkage_name;
9975 }
9976
9977 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9978 compute the physname for the object, which include a method's:
9979 - formal parameters (C++),
9980 - receiver type (Go),
9981
9982 The term "physname" is a bit confusing.
9983 For C++, for example, it is the demangled name.
9984 For Go, for example, it's the mangled name.
9985
9986 For Ada, return the DIE's linkage name rather than the fully qualified
9987 name. PHYSNAME is ignored..
9988
9989 The result is allocated on the objfile_obstack and canonicalized. */
9990
9991 static const char *
9992 dwarf2_compute_name (const char *name,
9993 struct die_info *die, struct dwarf2_cu *cu,
9994 int physname)
9995 {
9996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9997
9998 if (name == NULL)
9999 name = dwarf2_name (die, cu);
10000
10001 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10002 but otherwise compute it by typename_concat inside GDB.
10003 FIXME: Actually this is not really true, or at least not always true.
10004 It's all very confusing. compute_and_set_names doesn't try to demangle
10005 Fortran names because there is no mangling standard. So new_symbol
10006 will set the demangled name to the result of dwarf2_full_name, and it is
10007 the demangled name that GDB uses if it exists. */
10008 if (cu->language == language_ada
10009 || (cu->language == language_fortran && physname))
10010 {
10011 /* For Ada unit, we prefer the linkage name over the name, as
10012 the former contains the exported name, which the user expects
10013 to be able to reference. Ideally, we want the user to be able
10014 to reference this entity using either natural or linkage name,
10015 but we haven't started looking at this enhancement yet. */
10016 const char *linkage_name = dw2_linkage_name (die, cu);
10017
10018 if (linkage_name != NULL)
10019 return linkage_name;
10020 }
10021
10022 /* These are the only languages we know how to qualify names in. */
10023 if (name != NULL
10024 && (cu->language == language_cplus
10025 || cu->language == language_fortran || cu->language == language_d
10026 || cu->language == language_rust))
10027 {
10028 if (die_needs_namespace (die, cu))
10029 {
10030 const char *prefix;
10031 const char *canonical_name = NULL;
10032
10033 string_file buf;
10034
10035 prefix = determine_prefix (die, cu);
10036 if (*prefix != '\0')
10037 {
10038 gdb::unique_xmalloc_ptr<char> prefixed_name
10039 (typename_concat (NULL, prefix, name, physname, cu));
10040
10041 buf.puts (prefixed_name.get ());
10042 }
10043 else
10044 buf.puts (name);
10045
10046 /* Template parameters may be specified in the DIE's DW_AT_name, or
10047 as children with DW_TAG_template_type_param or
10048 DW_TAG_value_type_param. If the latter, add them to the name
10049 here. If the name already has template parameters, then
10050 skip this step; some versions of GCC emit both, and
10051 it is more efficient to use the pre-computed name.
10052
10053 Something to keep in mind about this process: it is very
10054 unlikely, or in some cases downright impossible, to produce
10055 something that will match the mangled name of a function.
10056 If the definition of the function has the same debug info,
10057 we should be able to match up with it anyway. But fallbacks
10058 using the minimal symbol, for instance to find a method
10059 implemented in a stripped copy of libstdc++, will not work.
10060 If we do not have debug info for the definition, we will have to
10061 match them up some other way.
10062
10063 When we do name matching there is a related problem with function
10064 templates; two instantiated function templates are allowed to
10065 differ only by their return types, which we do not add here. */
10066
10067 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10068 {
10069 struct attribute *attr;
10070 struct die_info *child;
10071 int first = 1;
10072
10073 die->building_fullname = 1;
10074
10075 for (child = die->child; child != NULL; child = child->sibling)
10076 {
10077 struct type *type;
10078 LONGEST value;
10079 const gdb_byte *bytes;
10080 struct dwarf2_locexpr_baton *baton;
10081 struct value *v;
10082
10083 if (child->tag != DW_TAG_template_type_param
10084 && child->tag != DW_TAG_template_value_param)
10085 continue;
10086
10087 if (first)
10088 {
10089 buf.puts ("<");
10090 first = 0;
10091 }
10092 else
10093 buf.puts (", ");
10094
10095 attr = dwarf2_attr (child, DW_AT_type, cu);
10096 if (attr == NULL)
10097 {
10098 complaint (_("template parameter missing DW_AT_type"));
10099 buf.puts ("UNKNOWN_TYPE");
10100 continue;
10101 }
10102 type = die_type (child, cu);
10103
10104 if (child->tag == DW_TAG_template_type_param)
10105 {
10106 c_print_type (type, "", &buf, -1, 0, cu->language,
10107 &type_print_raw_options);
10108 continue;
10109 }
10110
10111 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10112 if (attr == NULL)
10113 {
10114 complaint (_("template parameter missing "
10115 "DW_AT_const_value"));
10116 buf.puts ("UNKNOWN_VALUE");
10117 continue;
10118 }
10119
10120 dwarf2_const_value_attr (attr, type, name,
10121 &cu->comp_unit_obstack, cu,
10122 &value, &bytes, &baton);
10123
10124 if (TYPE_NOSIGN (type))
10125 /* GDB prints characters as NUMBER 'CHAR'. If that's
10126 changed, this can use value_print instead. */
10127 c_printchar (value, type, &buf);
10128 else
10129 {
10130 struct value_print_options opts;
10131
10132 if (baton != NULL)
10133 v = dwarf2_evaluate_loc_desc (type, NULL,
10134 baton->data,
10135 baton->size,
10136 baton->per_cu);
10137 else if (bytes != NULL)
10138 {
10139 v = allocate_value (type);
10140 memcpy (value_contents_writeable (v), bytes,
10141 TYPE_LENGTH (type));
10142 }
10143 else
10144 v = value_from_longest (type, value);
10145
10146 /* Specify decimal so that we do not depend on
10147 the radix. */
10148 get_formatted_print_options (&opts, 'd');
10149 opts.raw = 1;
10150 value_print (v, &buf, &opts);
10151 release_value (v);
10152 }
10153 }
10154
10155 die->building_fullname = 0;
10156
10157 if (!first)
10158 {
10159 /* Close the argument list, with a space if necessary
10160 (nested templates). */
10161 if (!buf.empty () && buf.string ().back () == '>')
10162 buf.puts (" >");
10163 else
10164 buf.puts (">");
10165 }
10166 }
10167
10168 /* For C++ methods, append formal parameter type
10169 information, if PHYSNAME. */
10170
10171 if (physname && die->tag == DW_TAG_subprogram
10172 && cu->language == language_cplus)
10173 {
10174 struct type *type = read_type_die (die, cu);
10175
10176 c_type_print_args (type, &buf, 1, cu->language,
10177 &type_print_raw_options);
10178
10179 if (cu->language == language_cplus)
10180 {
10181 /* Assume that an artificial first parameter is
10182 "this", but do not crash if it is not. RealView
10183 marks unnamed (and thus unused) parameters as
10184 artificial; there is no way to differentiate
10185 the two cases. */
10186 if (TYPE_NFIELDS (type) > 0
10187 && TYPE_FIELD_ARTIFICIAL (type, 0)
10188 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10189 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10190 0))))
10191 buf.puts (" const");
10192 }
10193 }
10194
10195 const std::string &intermediate_name = buf.string ();
10196
10197 if (cu->language == language_cplus)
10198 canonical_name
10199 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10200 &objfile->per_bfd->storage_obstack);
10201
10202 /* If we only computed INTERMEDIATE_NAME, or if
10203 INTERMEDIATE_NAME is already canonical, then we need to
10204 copy it to the appropriate obstack. */
10205 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10206 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10207 intermediate_name);
10208 else
10209 name = canonical_name;
10210 }
10211 }
10212
10213 return name;
10214 }
10215
10216 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10217 If scope qualifiers are appropriate they will be added. The result
10218 will be allocated on the storage_obstack, or NULL if the DIE does
10219 not have a name. NAME may either be from a previous call to
10220 dwarf2_name or NULL.
10221
10222 The output string will be canonicalized (if C++). */
10223
10224 static const char *
10225 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10226 {
10227 return dwarf2_compute_name (name, die, cu, 0);
10228 }
10229
10230 /* Construct a physname for the given DIE in CU. NAME may either be
10231 from a previous call to dwarf2_name or NULL. The result will be
10232 allocated on the objfile_objstack or NULL if the DIE does not have a
10233 name.
10234
10235 The output string will be canonicalized (if C++). */
10236
10237 static const char *
10238 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10239 {
10240 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10241 const char *retval, *mangled = NULL, *canon = NULL;
10242 int need_copy = 1;
10243
10244 /* In this case dwarf2_compute_name is just a shortcut not building anything
10245 on its own. */
10246 if (!die_needs_namespace (die, cu))
10247 return dwarf2_compute_name (name, die, cu, 1);
10248
10249 mangled = dw2_linkage_name (die, cu);
10250
10251 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10252 See https://github.com/rust-lang/rust/issues/32925. */
10253 if (cu->language == language_rust && mangled != NULL
10254 && strchr (mangled, '{') != NULL)
10255 mangled = NULL;
10256
10257 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10258 has computed. */
10259 gdb::unique_xmalloc_ptr<char> demangled;
10260 if (mangled != NULL)
10261 {
10262
10263 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10264 {
10265 /* Do nothing (do not demangle the symbol name). */
10266 }
10267 else if (cu->language == language_go)
10268 {
10269 /* This is a lie, but we already lie to the caller new_symbol.
10270 new_symbol assumes we return the mangled name.
10271 This just undoes that lie until things are cleaned up. */
10272 }
10273 else
10274 {
10275 /* Use DMGL_RET_DROP for C++ template functions to suppress
10276 their return type. It is easier for GDB users to search
10277 for such functions as `name(params)' than `long name(params)'.
10278 In such case the minimal symbol names do not match the full
10279 symbol names but for template functions there is never a need
10280 to look up their definition from their declaration so
10281 the only disadvantage remains the minimal symbol variant
10282 `long name(params)' does not have the proper inferior type. */
10283 demangled.reset (gdb_demangle (mangled,
10284 (DMGL_PARAMS | DMGL_ANSI
10285 | DMGL_RET_DROP)));
10286 }
10287 if (demangled)
10288 canon = demangled.get ();
10289 else
10290 {
10291 canon = mangled;
10292 need_copy = 0;
10293 }
10294 }
10295
10296 if (canon == NULL || check_physname)
10297 {
10298 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10299
10300 if (canon != NULL && strcmp (physname, canon) != 0)
10301 {
10302 /* It may not mean a bug in GDB. The compiler could also
10303 compute DW_AT_linkage_name incorrectly. But in such case
10304 GDB would need to be bug-to-bug compatible. */
10305
10306 complaint (_("Computed physname <%s> does not match demangled <%s> "
10307 "(from linkage <%s>) - DIE at %s [in module %s]"),
10308 physname, canon, mangled, sect_offset_str (die->sect_off),
10309 objfile_name (objfile));
10310
10311 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10312 is available here - over computed PHYSNAME. It is safer
10313 against both buggy GDB and buggy compilers. */
10314
10315 retval = canon;
10316 }
10317 else
10318 {
10319 retval = physname;
10320 need_copy = 0;
10321 }
10322 }
10323 else
10324 retval = canon;
10325
10326 if (need_copy)
10327 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10328
10329 return retval;
10330 }
10331
10332 /* Inspect DIE in CU for a namespace alias. If one exists, record
10333 a new symbol for it.
10334
10335 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10336
10337 static int
10338 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10339 {
10340 struct attribute *attr;
10341
10342 /* If the die does not have a name, this is not a namespace
10343 alias. */
10344 attr = dwarf2_attr (die, DW_AT_name, cu);
10345 if (attr != NULL)
10346 {
10347 int num;
10348 struct die_info *d = die;
10349 struct dwarf2_cu *imported_cu = cu;
10350
10351 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10352 keep inspecting DIEs until we hit the underlying import. */
10353 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10354 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10355 {
10356 attr = dwarf2_attr (d, DW_AT_import, cu);
10357 if (attr == NULL)
10358 break;
10359
10360 d = follow_die_ref (d, attr, &imported_cu);
10361 if (d->tag != DW_TAG_imported_declaration)
10362 break;
10363 }
10364
10365 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10366 {
10367 complaint (_("DIE at %s has too many recursively imported "
10368 "declarations"), sect_offset_str (d->sect_off));
10369 return 0;
10370 }
10371
10372 if (attr != NULL)
10373 {
10374 struct type *type;
10375 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10376
10377 type = get_die_type_at_offset (sect_off, cu->per_cu);
10378 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10379 {
10380 /* This declaration is a global namespace alias. Add
10381 a symbol for it whose type is the aliased namespace. */
10382 new_symbol (die, type, cu);
10383 return 1;
10384 }
10385 }
10386 }
10387
10388 return 0;
10389 }
10390
10391 /* Return the using directives repository (global or local?) to use in the
10392 current context for CU.
10393
10394 For Ada, imported declarations can materialize renamings, which *may* be
10395 global. However it is impossible (for now?) in DWARF to distinguish
10396 "external" imported declarations and "static" ones. As all imported
10397 declarations seem to be static in all other languages, make them all CU-wide
10398 global only in Ada. */
10399
10400 static struct using_direct **
10401 using_directives (struct dwarf2_cu *cu)
10402 {
10403 if (cu->language == language_ada
10404 && cu->get_builder ()->outermost_context_p ())
10405 return cu->get_builder ()->get_global_using_directives ();
10406 else
10407 return cu->get_builder ()->get_local_using_directives ();
10408 }
10409
10410 /* Read the import statement specified by the given die and record it. */
10411
10412 static void
10413 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10414 {
10415 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10416 struct attribute *import_attr;
10417 struct die_info *imported_die, *child_die;
10418 struct dwarf2_cu *imported_cu;
10419 const char *imported_name;
10420 const char *imported_name_prefix;
10421 const char *canonical_name;
10422 const char *import_alias;
10423 const char *imported_declaration = NULL;
10424 const char *import_prefix;
10425 std::vector<const char *> excludes;
10426
10427 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10428 if (import_attr == NULL)
10429 {
10430 complaint (_("Tag '%s' has no DW_AT_import"),
10431 dwarf_tag_name (die->tag));
10432 return;
10433 }
10434
10435 imported_cu = cu;
10436 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10437 imported_name = dwarf2_name (imported_die, imported_cu);
10438 if (imported_name == NULL)
10439 {
10440 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10441
10442 The import in the following code:
10443 namespace A
10444 {
10445 typedef int B;
10446 }
10447
10448 int main ()
10449 {
10450 using A::B;
10451 B b;
10452 return b;
10453 }
10454
10455 ...
10456 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10457 <52> DW_AT_decl_file : 1
10458 <53> DW_AT_decl_line : 6
10459 <54> DW_AT_import : <0x75>
10460 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10461 <59> DW_AT_name : B
10462 <5b> DW_AT_decl_file : 1
10463 <5c> DW_AT_decl_line : 2
10464 <5d> DW_AT_type : <0x6e>
10465 ...
10466 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10467 <76> DW_AT_byte_size : 4
10468 <77> DW_AT_encoding : 5 (signed)
10469
10470 imports the wrong die ( 0x75 instead of 0x58 ).
10471 This case will be ignored until the gcc bug is fixed. */
10472 return;
10473 }
10474
10475 /* Figure out the local name after import. */
10476 import_alias = dwarf2_name (die, cu);
10477
10478 /* Figure out where the statement is being imported to. */
10479 import_prefix = determine_prefix (die, cu);
10480
10481 /* Figure out what the scope of the imported die is and prepend it
10482 to the name of the imported die. */
10483 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10484
10485 if (imported_die->tag != DW_TAG_namespace
10486 && imported_die->tag != DW_TAG_module)
10487 {
10488 imported_declaration = imported_name;
10489 canonical_name = imported_name_prefix;
10490 }
10491 else if (strlen (imported_name_prefix) > 0)
10492 canonical_name = obconcat (&objfile->objfile_obstack,
10493 imported_name_prefix,
10494 (cu->language == language_d ? "." : "::"),
10495 imported_name, (char *) NULL);
10496 else
10497 canonical_name = imported_name;
10498
10499 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10500 for (child_die = die->child; child_die && child_die->tag;
10501 child_die = sibling_die (child_die))
10502 {
10503 /* DWARF-4: A Fortran use statement with a “rename list” may be
10504 represented by an imported module entry with an import attribute
10505 referring to the module and owned entries corresponding to those
10506 entities that are renamed as part of being imported. */
10507
10508 if (child_die->tag != DW_TAG_imported_declaration)
10509 {
10510 complaint (_("child DW_TAG_imported_declaration expected "
10511 "- DIE at %s [in module %s]"),
10512 sect_offset_str (child_die->sect_off),
10513 objfile_name (objfile));
10514 continue;
10515 }
10516
10517 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10518 if (import_attr == NULL)
10519 {
10520 complaint (_("Tag '%s' has no DW_AT_import"),
10521 dwarf_tag_name (child_die->tag));
10522 continue;
10523 }
10524
10525 imported_cu = cu;
10526 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10527 &imported_cu);
10528 imported_name = dwarf2_name (imported_die, imported_cu);
10529 if (imported_name == NULL)
10530 {
10531 complaint (_("child DW_TAG_imported_declaration has unknown "
10532 "imported name - DIE at %s [in module %s]"),
10533 sect_offset_str (child_die->sect_off),
10534 objfile_name (objfile));
10535 continue;
10536 }
10537
10538 excludes.push_back (imported_name);
10539
10540 process_die (child_die, cu);
10541 }
10542
10543 add_using_directive (using_directives (cu),
10544 import_prefix,
10545 canonical_name,
10546 import_alias,
10547 imported_declaration,
10548 excludes,
10549 0,
10550 &objfile->objfile_obstack);
10551 }
10552
10553 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10554 types, but gives them a size of zero. Starting with version 14,
10555 ICC is compatible with GCC. */
10556
10557 static bool
10558 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10559 {
10560 if (!cu->checked_producer)
10561 check_producer (cu);
10562
10563 return cu->producer_is_icc_lt_14;
10564 }
10565
10566 /* ICC generates a DW_AT_type for C void functions. This was observed on
10567 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10568 which says that void functions should not have a DW_AT_type. */
10569
10570 static bool
10571 producer_is_icc (struct dwarf2_cu *cu)
10572 {
10573 if (!cu->checked_producer)
10574 check_producer (cu);
10575
10576 return cu->producer_is_icc;
10577 }
10578
10579 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10580 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10581 this, it was first present in GCC release 4.3.0. */
10582
10583 static bool
10584 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10585 {
10586 if (!cu->checked_producer)
10587 check_producer (cu);
10588
10589 return cu->producer_is_gcc_lt_4_3;
10590 }
10591
10592 static file_and_directory
10593 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10594 {
10595 file_and_directory res;
10596
10597 /* Find the filename. Do not use dwarf2_name here, since the filename
10598 is not a source language identifier. */
10599 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10600 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10601
10602 if (res.comp_dir == NULL
10603 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10604 && IS_ABSOLUTE_PATH (res.name))
10605 {
10606 res.comp_dir_storage = ldirname (res.name);
10607 if (!res.comp_dir_storage.empty ())
10608 res.comp_dir = res.comp_dir_storage.c_str ();
10609 }
10610 if (res.comp_dir != NULL)
10611 {
10612 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10613 directory, get rid of it. */
10614 const char *cp = strchr (res.comp_dir, ':');
10615
10616 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10617 res.comp_dir = cp + 1;
10618 }
10619
10620 if (res.name == NULL)
10621 res.name = "<unknown>";
10622
10623 return res;
10624 }
10625
10626 /* Handle DW_AT_stmt_list for a compilation unit.
10627 DIE is the DW_TAG_compile_unit die for CU.
10628 COMP_DIR is the compilation directory. LOWPC is passed to
10629 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10630
10631 static void
10632 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10633 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10634 {
10635 struct dwarf2_per_objfile *dwarf2_per_objfile
10636 = cu->per_cu->dwarf2_per_objfile;
10637 struct attribute *attr;
10638 struct line_header line_header_local;
10639 hashval_t line_header_local_hash;
10640 void **slot;
10641 int decode_mapping;
10642
10643 gdb_assert (! cu->per_cu->is_debug_types);
10644
10645 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10646 if (attr == NULL)
10647 return;
10648
10649 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10650
10651 /* The line header hash table is only created if needed (it exists to
10652 prevent redundant reading of the line table for partial_units).
10653 If we're given a partial_unit, we'll need it. If we're given a
10654 compile_unit, then use the line header hash table if it's already
10655 created, but don't create one just yet. */
10656
10657 if (dwarf2_per_objfile->line_header_hash == NULL
10658 && die->tag == DW_TAG_partial_unit)
10659 {
10660 dwarf2_per_objfile->line_header_hash
10661 .reset (htab_create_alloc (127, line_header_hash_voidp,
10662 line_header_eq_voidp,
10663 free_line_header_voidp,
10664 xcalloc, xfree));
10665 }
10666
10667 line_header_local.sect_off = line_offset;
10668 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10669 line_header_local_hash = line_header_hash (&line_header_local);
10670 if (dwarf2_per_objfile->line_header_hash != NULL)
10671 {
10672 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10673 &line_header_local,
10674 line_header_local_hash, NO_INSERT);
10675
10676 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10677 is not present in *SLOT (since if there is something in *SLOT then
10678 it will be for a partial_unit). */
10679 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10680 {
10681 gdb_assert (*slot != NULL);
10682 cu->line_header = (struct line_header *) *slot;
10683 return;
10684 }
10685 }
10686
10687 /* dwarf_decode_line_header does not yet provide sufficient information.
10688 We always have to call also dwarf_decode_lines for it. */
10689 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10690 if (lh == NULL)
10691 return;
10692
10693 cu->line_header = lh.release ();
10694 cu->line_header_die_owner = die;
10695
10696 if (dwarf2_per_objfile->line_header_hash == NULL)
10697 slot = NULL;
10698 else
10699 {
10700 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10701 &line_header_local,
10702 line_header_local_hash, INSERT);
10703 gdb_assert (slot != NULL);
10704 }
10705 if (slot != NULL && *slot == NULL)
10706 {
10707 /* This newly decoded line number information unit will be owned
10708 by line_header_hash hash table. */
10709 *slot = cu->line_header;
10710 cu->line_header_die_owner = NULL;
10711 }
10712 else
10713 {
10714 /* We cannot free any current entry in (*slot) as that struct line_header
10715 may be already used by multiple CUs. Create only temporary decoded
10716 line_header for this CU - it may happen at most once for each line
10717 number information unit. And if we're not using line_header_hash
10718 then this is what we want as well. */
10719 gdb_assert (die->tag != DW_TAG_partial_unit);
10720 }
10721 decode_mapping = (die->tag != DW_TAG_partial_unit);
10722 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10723 decode_mapping);
10724
10725 }
10726
10727 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10728
10729 static void
10730 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10731 {
10732 struct dwarf2_per_objfile *dwarf2_per_objfile
10733 = cu->per_cu->dwarf2_per_objfile;
10734 struct objfile *objfile = dwarf2_per_objfile->objfile;
10735 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10736 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10737 CORE_ADDR highpc = ((CORE_ADDR) 0);
10738 struct attribute *attr;
10739 struct die_info *child_die;
10740 CORE_ADDR baseaddr;
10741
10742 prepare_one_comp_unit (cu, die, cu->language);
10743 baseaddr = objfile->text_section_offset ();
10744
10745 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10746
10747 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10748 from finish_block. */
10749 if (lowpc == ((CORE_ADDR) -1))
10750 lowpc = highpc;
10751 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10752
10753 file_and_directory fnd = find_file_and_directory (die, cu);
10754
10755 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10756 standardised yet. As a workaround for the language detection we fall
10757 back to the DW_AT_producer string. */
10758 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10759 cu->language = language_opencl;
10760
10761 /* Similar hack for Go. */
10762 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10763 set_cu_language (DW_LANG_Go, cu);
10764
10765 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10766
10767 /* Decode line number information if present. We do this before
10768 processing child DIEs, so that the line header table is available
10769 for DW_AT_decl_file. */
10770 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10771
10772 /* Process all dies in compilation unit. */
10773 if (die->child != NULL)
10774 {
10775 child_die = die->child;
10776 while (child_die && child_die->tag)
10777 {
10778 process_die (child_die, cu);
10779 child_die = sibling_die (child_die);
10780 }
10781 }
10782
10783 /* Decode macro information, if present. Dwarf 2 macro information
10784 refers to information in the line number info statement program
10785 header, so we can only read it if we've read the header
10786 successfully. */
10787 attr = dwarf2_attr (die, DW_AT_macros, cu);
10788 if (attr == NULL)
10789 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10790 if (attr && cu->line_header)
10791 {
10792 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10793 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10794
10795 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10796 }
10797 else
10798 {
10799 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10800 if (attr && cu->line_header)
10801 {
10802 unsigned int macro_offset = DW_UNSND (attr);
10803
10804 dwarf_decode_macros (cu, macro_offset, 0);
10805 }
10806 }
10807 }
10808
10809 void
10810 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10811 {
10812 struct type_unit_group *tu_group;
10813 int first_time;
10814 struct attribute *attr;
10815 unsigned int i;
10816 struct signatured_type *sig_type;
10817
10818 gdb_assert (per_cu->is_debug_types);
10819 sig_type = (struct signatured_type *) per_cu;
10820
10821 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10822
10823 /* If we're using .gdb_index (includes -readnow) then
10824 per_cu->type_unit_group may not have been set up yet. */
10825 if (sig_type->type_unit_group == NULL)
10826 sig_type->type_unit_group = get_type_unit_group (this, attr);
10827 tu_group = sig_type->type_unit_group;
10828
10829 /* If we've already processed this stmt_list there's no real need to
10830 do it again, we could fake it and just recreate the part we need
10831 (file name,index -> symtab mapping). If data shows this optimization
10832 is useful we can do it then. */
10833 first_time = tu_group->compunit_symtab == NULL;
10834
10835 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10836 debug info. */
10837 line_header_up lh;
10838 if (attr != NULL)
10839 {
10840 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10841 lh = dwarf_decode_line_header (line_offset, this);
10842 }
10843 if (lh == NULL)
10844 {
10845 if (first_time)
10846 start_symtab ("", NULL, 0);
10847 else
10848 {
10849 gdb_assert (tu_group->symtabs == NULL);
10850 gdb_assert (m_builder == nullptr);
10851 struct compunit_symtab *cust = tu_group->compunit_symtab;
10852 m_builder.reset (new struct buildsym_compunit
10853 (COMPUNIT_OBJFILE (cust), "",
10854 COMPUNIT_DIRNAME (cust),
10855 compunit_language (cust),
10856 0, cust));
10857 }
10858 return;
10859 }
10860
10861 line_header = lh.release ();
10862 line_header_die_owner = die;
10863
10864 if (first_time)
10865 {
10866 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10867
10868 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10869 still initializing it, and our caller (a few levels up)
10870 process_full_type_unit still needs to know if this is the first
10871 time. */
10872
10873 tu_group->num_symtabs = line_header->file_names_size ();
10874 tu_group->symtabs = XNEWVEC (struct symtab *,
10875 line_header->file_names_size ());
10876
10877 auto &file_names = line_header->file_names ();
10878 for (i = 0; i < file_names.size (); ++i)
10879 {
10880 file_entry &fe = file_names[i];
10881 dwarf2_start_subfile (this, fe.name,
10882 fe.include_dir (line_header));
10883 buildsym_compunit *b = get_builder ();
10884 if (b->get_current_subfile ()->symtab == NULL)
10885 {
10886 /* NOTE: start_subfile will recognize when it's been
10887 passed a file it has already seen. So we can't
10888 assume there's a simple mapping from
10889 cu->line_header->file_names to subfiles, plus
10890 cu->line_header->file_names may contain dups. */
10891 b->get_current_subfile ()->symtab
10892 = allocate_symtab (cust, b->get_current_subfile ()->name);
10893 }
10894
10895 fe.symtab = b->get_current_subfile ()->symtab;
10896 tu_group->symtabs[i] = fe.symtab;
10897 }
10898 }
10899 else
10900 {
10901 gdb_assert (m_builder == nullptr);
10902 struct compunit_symtab *cust = tu_group->compunit_symtab;
10903 m_builder.reset (new struct buildsym_compunit
10904 (COMPUNIT_OBJFILE (cust), "",
10905 COMPUNIT_DIRNAME (cust),
10906 compunit_language (cust),
10907 0, cust));
10908
10909 auto &file_names = line_header->file_names ();
10910 for (i = 0; i < file_names.size (); ++i)
10911 {
10912 file_entry &fe = file_names[i];
10913 fe.symtab = tu_group->symtabs[i];
10914 }
10915 }
10916
10917 /* The main symtab is allocated last. Type units don't have DW_AT_name
10918 so they don't have a "real" (so to speak) symtab anyway.
10919 There is later code that will assign the main symtab to all symbols
10920 that don't have one. We need to handle the case of a symbol with a
10921 missing symtab (DW_AT_decl_file) anyway. */
10922 }
10923
10924 /* Process DW_TAG_type_unit.
10925 For TUs we want to skip the first top level sibling if it's not the
10926 actual type being defined by this TU. In this case the first top
10927 level sibling is there to provide context only. */
10928
10929 static void
10930 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10931 {
10932 struct die_info *child_die;
10933
10934 prepare_one_comp_unit (cu, die, language_minimal);
10935
10936 /* Initialize (or reinitialize) the machinery for building symtabs.
10937 We do this before processing child DIEs, so that the line header table
10938 is available for DW_AT_decl_file. */
10939 cu->setup_type_unit_groups (die);
10940
10941 if (die->child != NULL)
10942 {
10943 child_die = die->child;
10944 while (child_die && child_die->tag)
10945 {
10946 process_die (child_die, cu);
10947 child_die = sibling_die (child_die);
10948 }
10949 }
10950 }
10951 \f
10952 /* DWO/DWP files.
10953
10954 http://gcc.gnu.org/wiki/DebugFission
10955 http://gcc.gnu.org/wiki/DebugFissionDWP
10956
10957 To simplify handling of both DWO files ("object" files with the DWARF info)
10958 and DWP files (a file with the DWOs packaged up into one file), we treat
10959 DWP files as having a collection of virtual DWO files. */
10960
10961 static hashval_t
10962 hash_dwo_file (const void *item)
10963 {
10964 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10965 hashval_t hash;
10966
10967 hash = htab_hash_string (dwo_file->dwo_name);
10968 if (dwo_file->comp_dir != NULL)
10969 hash += htab_hash_string (dwo_file->comp_dir);
10970 return hash;
10971 }
10972
10973 static int
10974 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10975 {
10976 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10977 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10978
10979 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10980 return 0;
10981 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10982 return lhs->comp_dir == rhs->comp_dir;
10983 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10984 }
10985
10986 /* Allocate a hash table for DWO files. */
10987
10988 static htab_up
10989 allocate_dwo_file_hash_table (struct objfile *objfile)
10990 {
10991 auto delete_dwo_file = [] (void *item)
10992 {
10993 struct dwo_file *dwo_file = (struct dwo_file *) item;
10994
10995 delete dwo_file;
10996 };
10997
10998 return htab_up (htab_create_alloc (41,
10999 hash_dwo_file,
11000 eq_dwo_file,
11001 delete_dwo_file,
11002 xcalloc, xfree));
11003 }
11004
11005 /* Lookup DWO file DWO_NAME. */
11006
11007 static void **
11008 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11009 const char *dwo_name,
11010 const char *comp_dir)
11011 {
11012 struct dwo_file find_entry;
11013 void **slot;
11014
11015 if (dwarf2_per_objfile->dwo_files == NULL)
11016 dwarf2_per_objfile->dwo_files
11017 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11018
11019 find_entry.dwo_name = dwo_name;
11020 find_entry.comp_dir = comp_dir;
11021 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11022 INSERT);
11023
11024 return slot;
11025 }
11026
11027 static hashval_t
11028 hash_dwo_unit (const void *item)
11029 {
11030 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11031
11032 /* This drops the top 32 bits of the id, but is ok for a hash. */
11033 return dwo_unit->signature;
11034 }
11035
11036 static int
11037 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11038 {
11039 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11040 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11041
11042 /* The signature is assumed to be unique within the DWO file.
11043 So while object file CU dwo_id's always have the value zero,
11044 that's OK, assuming each object file DWO file has only one CU,
11045 and that's the rule for now. */
11046 return lhs->signature == rhs->signature;
11047 }
11048
11049 /* Allocate a hash table for DWO CUs,TUs.
11050 There is one of these tables for each of CUs,TUs for each DWO file. */
11051
11052 static htab_up
11053 allocate_dwo_unit_table (struct objfile *objfile)
11054 {
11055 /* Start out with a pretty small number.
11056 Generally DWO files contain only one CU and maybe some TUs. */
11057 return htab_up (htab_create_alloc (3,
11058 hash_dwo_unit,
11059 eq_dwo_unit,
11060 NULL, xcalloc, xfree));
11061 }
11062
11063 /* die_reader_func for create_dwo_cu. */
11064
11065 static void
11066 create_dwo_cu_reader (const struct die_reader_specs *reader,
11067 const gdb_byte *info_ptr,
11068 struct die_info *comp_unit_die,
11069 struct dwo_file *dwo_file,
11070 struct dwo_unit *dwo_unit)
11071 {
11072 struct dwarf2_cu *cu = reader->cu;
11073 sect_offset sect_off = cu->per_cu->sect_off;
11074 struct dwarf2_section_info *section = cu->per_cu->section;
11075
11076 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11077 if (!signature.has_value ())
11078 {
11079 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11080 " its dwo_id [in module %s]"),
11081 sect_offset_str (sect_off), dwo_file->dwo_name);
11082 return;
11083 }
11084
11085 dwo_unit->dwo_file = dwo_file;
11086 dwo_unit->signature = *signature;
11087 dwo_unit->section = section;
11088 dwo_unit->sect_off = sect_off;
11089 dwo_unit->length = cu->per_cu->length;
11090
11091 if (dwarf_read_debug)
11092 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11093 sect_offset_str (sect_off),
11094 hex_string (dwo_unit->signature));
11095 }
11096
11097 /* Create the dwo_units for the CUs in a DWO_FILE.
11098 Note: This function processes DWO files only, not DWP files. */
11099
11100 static void
11101 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11102 dwarf2_cu *cu, struct dwo_file &dwo_file,
11103 dwarf2_section_info &section, htab_up &cus_htab)
11104 {
11105 struct objfile *objfile = dwarf2_per_objfile->objfile;
11106 const gdb_byte *info_ptr, *end_ptr;
11107
11108 section.read (objfile);
11109 info_ptr = section.buffer;
11110
11111 if (info_ptr == NULL)
11112 return;
11113
11114 if (dwarf_read_debug)
11115 {
11116 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11117 section.get_name (),
11118 section.get_file_name ());
11119 }
11120
11121 end_ptr = info_ptr + section.size;
11122 while (info_ptr < end_ptr)
11123 {
11124 struct dwarf2_per_cu_data per_cu;
11125 struct dwo_unit read_unit {};
11126 struct dwo_unit *dwo_unit;
11127 void **slot;
11128 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11129
11130 memset (&per_cu, 0, sizeof (per_cu));
11131 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11132 per_cu.is_debug_types = 0;
11133 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11134 per_cu.section = &section;
11135
11136 cutu_reader reader (&per_cu, cu, &dwo_file);
11137 if (!reader.dummy_p)
11138 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11139 &dwo_file, &read_unit);
11140 info_ptr += per_cu.length;
11141
11142 // If the unit could not be parsed, skip it.
11143 if (read_unit.dwo_file == NULL)
11144 continue;
11145
11146 if (cus_htab == NULL)
11147 cus_htab = allocate_dwo_unit_table (objfile);
11148
11149 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11150 *dwo_unit = read_unit;
11151 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11152 gdb_assert (slot != NULL);
11153 if (*slot != NULL)
11154 {
11155 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11156 sect_offset dup_sect_off = dup_cu->sect_off;
11157
11158 complaint (_("debug cu entry at offset %s is duplicate to"
11159 " the entry at offset %s, signature %s"),
11160 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11161 hex_string (dwo_unit->signature));
11162 }
11163 *slot = (void *)dwo_unit;
11164 }
11165 }
11166
11167 /* DWP file .debug_{cu,tu}_index section format:
11168 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11169
11170 DWP Version 1:
11171
11172 Both index sections have the same format, and serve to map a 64-bit
11173 signature to a set of section numbers. Each section begins with a header,
11174 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11175 indexes, and a pool of 32-bit section numbers. The index sections will be
11176 aligned at 8-byte boundaries in the file.
11177
11178 The index section header consists of:
11179
11180 V, 32 bit version number
11181 -, 32 bits unused
11182 N, 32 bit number of compilation units or type units in the index
11183 M, 32 bit number of slots in the hash table
11184
11185 Numbers are recorded using the byte order of the application binary.
11186
11187 The hash table begins at offset 16 in the section, and consists of an array
11188 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11189 order of the application binary). Unused slots in the hash table are 0.
11190 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11191
11192 The parallel table begins immediately after the hash table
11193 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11194 array of 32-bit indexes (using the byte order of the application binary),
11195 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11196 table contains a 32-bit index into the pool of section numbers. For unused
11197 hash table slots, the corresponding entry in the parallel table will be 0.
11198
11199 The pool of section numbers begins immediately following the hash table
11200 (at offset 16 + 12 * M from the beginning of the section). The pool of
11201 section numbers consists of an array of 32-bit words (using the byte order
11202 of the application binary). Each item in the array is indexed starting
11203 from 0. The hash table entry provides the index of the first section
11204 number in the set. Additional section numbers in the set follow, and the
11205 set is terminated by a 0 entry (section number 0 is not used in ELF).
11206
11207 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11208 section must be the first entry in the set, and the .debug_abbrev.dwo must
11209 be the second entry. Other members of the set may follow in any order.
11210
11211 ---
11212
11213 DWP Version 2:
11214
11215 DWP Version 2 combines all the .debug_info, etc. sections into one,
11216 and the entries in the index tables are now offsets into these sections.
11217 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11218 section.
11219
11220 Index Section Contents:
11221 Header
11222 Hash Table of Signatures dwp_hash_table.hash_table
11223 Parallel Table of Indices dwp_hash_table.unit_table
11224 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11225 Table of Section Sizes dwp_hash_table.v2.sizes
11226
11227 The index section header consists of:
11228
11229 V, 32 bit version number
11230 L, 32 bit number of columns in the table of section offsets
11231 N, 32 bit number of compilation units or type units in the index
11232 M, 32 bit number of slots in the hash table
11233
11234 Numbers are recorded using the byte order of the application binary.
11235
11236 The hash table has the same format as version 1.
11237 The parallel table of indices has the same format as version 1,
11238 except that the entries are origin-1 indices into the table of sections
11239 offsets and the table of section sizes.
11240
11241 The table of offsets begins immediately following the parallel table
11242 (at offset 16 + 12 * M from the beginning of the section). The table is
11243 a two-dimensional array of 32-bit words (using the byte order of the
11244 application binary), with L columns and N+1 rows, in row-major order.
11245 Each row in the array is indexed starting from 0. The first row provides
11246 a key to the remaining rows: each column in this row provides an identifier
11247 for a debug section, and the offsets in the same column of subsequent rows
11248 refer to that section. The section identifiers are:
11249
11250 DW_SECT_INFO 1 .debug_info.dwo
11251 DW_SECT_TYPES 2 .debug_types.dwo
11252 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11253 DW_SECT_LINE 4 .debug_line.dwo
11254 DW_SECT_LOC 5 .debug_loc.dwo
11255 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11256 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11257 DW_SECT_MACRO 8 .debug_macro.dwo
11258
11259 The offsets provided by the CU and TU index sections are the base offsets
11260 for the contributions made by each CU or TU to the corresponding section
11261 in the package file. Each CU and TU header contains an abbrev_offset
11262 field, used to find the abbreviations table for that CU or TU within the
11263 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11264 be interpreted as relative to the base offset given in the index section.
11265 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11266 should be interpreted as relative to the base offset for .debug_line.dwo,
11267 and offsets into other debug sections obtained from DWARF attributes should
11268 also be interpreted as relative to the corresponding base offset.
11269
11270 The table of sizes begins immediately following the table of offsets.
11271 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11272 with L columns and N rows, in row-major order. Each row in the array is
11273 indexed starting from 1 (row 0 is shared by the two tables).
11274
11275 ---
11276
11277 Hash table lookup is handled the same in version 1 and 2:
11278
11279 We assume that N and M will not exceed 2^32 - 1.
11280 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11281
11282 Given a 64-bit compilation unit signature or a type signature S, an entry
11283 in the hash table is located as follows:
11284
11285 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11286 the low-order k bits all set to 1.
11287
11288 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11289
11290 3) If the hash table entry at index H matches the signature, use that
11291 entry. If the hash table entry at index H is unused (all zeroes),
11292 terminate the search: the signature is not present in the table.
11293
11294 4) Let H = (H + H') modulo M. Repeat at Step 3.
11295
11296 Because M > N and H' and M are relatively prime, the search is guaranteed
11297 to stop at an unused slot or find the match. */
11298
11299 /* Create a hash table to map DWO IDs to their CU/TU entry in
11300 .debug_{info,types}.dwo in DWP_FILE.
11301 Returns NULL if there isn't one.
11302 Note: This function processes DWP files only, not DWO files. */
11303
11304 static struct dwp_hash_table *
11305 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11306 struct dwp_file *dwp_file, int is_debug_types)
11307 {
11308 struct objfile *objfile = dwarf2_per_objfile->objfile;
11309 bfd *dbfd = dwp_file->dbfd.get ();
11310 const gdb_byte *index_ptr, *index_end;
11311 struct dwarf2_section_info *index;
11312 uint32_t version, nr_columns, nr_units, nr_slots;
11313 struct dwp_hash_table *htab;
11314
11315 if (is_debug_types)
11316 index = &dwp_file->sections.tu_index;
11317 else
11318 index = &dwp_file->sections.cu_index;
11319
11320 if (index->empty ())
11321 return NULL;
11322 index->read (objfile);
11323
11324 index_ptr = index->buffer;
11325 index_end = index_ptr + index->size;
11326
11327 version = read_4_bytes (dbfd, index_ptr);
11328 index_ptr += 4;
11329 if (version == 2)
11330 nr_columns = read_4_bytes (dbfd, index_ptr);
11331 else
11332 nr_columns = 0;
11333 index_ptr += 4;
11334 nr_units = read_4_bytes (dbfd, index_ptr);
11335 index_ptr += 4;
11336 nr_slots = read_4_bytes (dbfd, index_ptr);
11337 index_ptr += 4;
11338
11339 if (version != 1 && version != 2)
11340 {
11341 error (_("Dwarf Error: unsupported DWP file version (%s)"
11342 " [in module %s]"),
11343 pulongest (version), dwp_file->name);
11344 }
11345 if (nr_slots != (nr_slots & -nr_slots))
11346 {
11347 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11348 " is not power of 2 [in module %s]"),
11349 pulongest (nr_slots), dwp_file->name);
11350 }
11351
11352 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11353 htab->version = version;
11354 htab->nr_columns = nr_columns;
11355 htab->nr_units = nr_units;
11356 htab->nr_slots = nr_slots;
11357 htab->hash_table = index_ptr;
11358 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11359
11360 /* Exit early if the table is empty. */
11361 if (nr_slots == 0 || nr_units == 0
11362 || (version == 2 && nr_columns == 0))
11363 {
11364 /* All must be zero. */
11365 if (nr_slots != 0 || nr_units != 0
11366 || (version == 2 && nr_columns != 0))
11367 {
11368 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11369 " all zero [in modules %s]"),
11370 dwp_file->name);
11371 }
11372 return htab;
11373 }
11374
11375 if (version == 1)
11376 {
11377 htab->section_pool.v1.indices =
11378 htab->unit_table + sizeof (uint32_t) * nr_slots;
11379 /* It's harder to decide whether the section is too small in v1.
11380 V1 is deprecated anyway so we punt. */
11381 }
11382 else
11383 {
11384 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11385 int *ids = htab->section_pool.v2.section_ids;
11386 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11387 /* Reverse map for error checking. */
11388 int ids_seen[DW_SECT_MAX + 1];
11389 int i;
11390
11391 if (nr_columns < 2)
11392 {
11393 error (_("Dwarf Error: bad DWP hash table, too few columns"
11394 " in section table [in module %s]"),
11395 dwp_file->name);
11396 }
11397 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11398 {
11399 error (_("Dwarf Error: bad DWP hash table, too many columns"
11400 " in section table [in module %s]"),
11401 dwp_file->name);
11402 }
11403 memset (ids, 255, sizeof_ids);
11404 memset (ids_seen, 255, sizeof (ids_seen));
11405 for (i = 0; i < nr_columns; ++i)
11406 {
11407 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11408
11409 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11410 {
11411 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11412 " in section table [in module %s]"),
11413 id, dwp_file->name);
11414 }
11415 if (ids_seen[id] != -1)
11416 {
11417 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11418 " id %d in section table [in module %s]"),
11419 id, dwp_file->name);
11420 }
11421 ids_seen[id] = i;
11422 ids[i] = id;
11423 }
11424 /* Must have exactly one info or types section. */
11425 if (((ids_seen[DW_SECT_INFO] != -1)
11426 + (ids_seen[DW_SECT_TYPES] != -1))
11427 != 1)
11428 {
11429 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11430 " DWO info/types section [in module %s]"),
11431 dwp_file->name);
11432 }
11433 /* Must have an abbrev section. */
11434 if (ids_seen[DW_SECT_ABBREV] == -1)
11435 {
11436 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11437 " section [in module %s]"),
11438 dwp_file->name);
11439 }
11440 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11441 htab->section_pool.v2.sizes =
11442 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11443 * nr_units * nr_columns);
11444 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11445 * nr_units * nr_columns))
11446 > index_end)
11447 {
11448 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11449 " [in module %s]"),
11450 dwp_file->name);
11451 }
11452 }
11453
11454 return htab;
11455 }
11456
11457 /* Update SECTIONS with the data from SECTP.
11458
11459 This function is like the other "locate" section routines that are
11460 passed to bfd_map_over_sections, but in this context the sections to
11461 read comes from the DWP V1 hash table, not the full ELF section table.
11462
11463 The result is non-zero for success, or zero if an error was found. */
11464
11465 static int
11466 locate_v1_virtual_dwo_sections (asection *sectp,
11467 struct virtual_v1_dwo_sections *sections)
11468 {
11469 const struct dwop_section_names *names = &dwop_section_names;
11470
11471 if (section_is_p (sectp->name, &names->abbrev_dwo))
11472 {
11473 /* There can be only one. */
11474 if (sections->abbrev.s.section != NULL)
11475 return 0;
11476 sections->abbrev.s.section = sectp;
11477 sections->abbrev.size = bfd_section_size (sectp);
11478 }
11479 else if (section_is_p (sectp->name, &names->info_dwo)
11480 || section_is_p (sectp->name, &names->types_dwo))
11481 {
11482 /* There can be only one. */
11483 if (sections->info_or_types.s.section != NULL)
11484 return 0;
11485 sections->info_or_types.s.section = sectp;
11486 sections->info_or_types.size = bfd_section_size (sectp);
11487 }
11488 else if (section_is_p (sectp->name, &names->line_dwo))
11489 {
11490 /* There can be only one. */
11491 if (sections->line.s.section != NULL)
11492 return 0;
11493 sections->line.s.section = sectp;
11494 sections->line.size = bfd_section_size (sectp);
11495 }
11496 else if (section_is_p (sectp->name, &names->loc_dwo))
11497 {
11498 /* There can be only one. */
11499 if (sections->loc.s.section != NULL)
11500 return 0;
11501 sections->loc.s.section = sectp;
11502 sections->loc.size = bfd_section_size (sectp);
11503 }
11504 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11505 {
11506 /* There can be only one. */
11507 if (sections->macinfo.s.section != NULL)
11508 return 0;
11509 sections->macinfo.s.section = sectp;
11510 sections->macinfo.size = bfd_section_size (sectp);
11511 }
11512 else if (section_is_p (sectp->name, &names->macro_dwo))
11513 {
11514 /* There can be only one. */
11515 if (sections->macro.s.section != NULL)
11516 return 0;
11517 sections->macro.s.section = sectp;
11518 sections->macro.size = bfd_section_size (sectp);
11519 }
11520 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11521 {
11522 /* There can be only one. */
11523 if (sections->str_offsets.s.section != NULL)
11524 return 0;
11525 sections->str_offsets.s.section = sectp;
11526 sections->str_offsets.size = bfd_section_size (sectp);
11527 }
11528 else
11529 {
11530 /* No other kind of section is valid. */
11531 return 0;
11532 }
11533
11534 return 1;
11535 }
11536
11537 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11538 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11539 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11540 This is for DWP version 1 files. */
11541
11542 static struct dwo_unit *
11543 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11544 struct dwp_file *dwp_file,
11545 uint32_t unit_index,
11546 const char *comp_dir,
11547 ULONGEST signature, int is_debug_types)
11548 {
11549 struct objfile *objfile = dwarf2_per_objfile->objfile;
11550 const struct dwp_hash_table *dwp_htab =
11551 is_debug_types ? dwp_file->tus : dwp_file->cus;
11552 bfd *dbfd = dwp_file->dbfd.get ();
11553 const char *kind = is_debug_types ? "TU" : "CU";
11554 struct dwo_file *dwo_file;
11555 struct dwo_unit *dwo_unit;
11556 struct virtual_v1_dwo_sections sections;
11557 void **dwo_file_slot;
11558 int i;
11559
11560 gdb_assert (dwp_file->version == 1);
11561
11562 if (dwarf_read_debug)
11563 {
11564 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11565 kind,
11566 pulongest (unit_index), hex_string (signature),
11567 dwp_file->name);
11568 }
11569
11570 /* Fetch the sections of this DWO unit.
11571 Put a limit on the number of sections we look for so that bad data
11572 doesn't cause us to loop forever. */
11573
11574 #define MAX_NR_V1_DWO_SECTIONS \
11575 (1 /* .debug_info or .debug_types */ \
11576 + 1 /* .debug_abbrev */ \
11577 + 1 /* .debug_line */ \
11578 + 1 /* .debug_loc */ \
11579 + 1 /* .debug_str_offsets */ \
11580 + 1 /* .debug_macro or .debug_macinfo */ \
11581 + 1 /* trailing zero */)
11582
11583 memset (&sections, 0, sizeof (sections));
11584
11585 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11586 {
11587 asection *sectp;
11588 uint32_t section_nr =
11589 read_4_bytes (dbfd,
11590 dwp_htab->section_pool.v1.indices
11591 + (unit_index + i) * sizeof (uint32_t));
11592
11593 if (section_nr == 0)
11594 break;
11595 if (section_nr >= dwp_file->num_sections)
11596 {
11597 error (_("Dwarf Error: bad DWP hash table, section number too large"
11598 " [in module %s]"),
11599 dwp_file->name);
11600 }
11601
11602 sectp = dwp_file->elf_sections[section_nr];
11603 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11604 {
11605 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11606 " [in module %s]"),
11607 dwp_file->name);
11608 }
11609 }
11610
11611 if (i < 2
11612 || sections.info_or_types.empty ()
11613 || sections.abbrev.empty ())
11614 {
11615 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11616 " [in module %s]"),
11617 dwp_file->name);
11618 }
11619 if (i == MAX_NR_V1_DWO_SECTIONS)
11620 {
11621 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11622 " [in module %s]"),
11623 dwp_file->name);
11624 }
11625
11626 /* It's easier for the rest of the code if we fake a struct dwo_file and
11627 have dwo_unit "live" in that. At least for now.
11628
11629 The DWP file can be made up of a random collection of CUs and TUs.
11630 However, for each CU + set of TUs that came from the same original DWO
11631 file, we can combine them back into a virtual DWO file to save space
11632 (fewer struct dwo_file objects to allocate). Remember that for really
11633 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11634
11635 std::string virtual_dwo_name =
11636 string_printf ("virtual-dwo/%d-%d-%d-%d",
11637 sections.abbrev.get_id (),
11638 sections.line.get_id (),
11639 sections.loc.get_id (),
11640 sections.str_offsets.get_id ());
11641 /* Can we use an existing virtual DWO file? */
11642 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11643 virtual_dwo_name.c_str (),
11644 comp_dir);
11645 /* Create one if necessary. */
11646 if (*dwo_file_slot == NULL)
11647 {
11648 if (dwarf_read_debug)
11649 {
11650 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11651 virtual_dwo_name.c_str ());
11652 }
11653 dwo_file = new struct dwo_file;
11654 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11655 virtual_dwo_name);
11656 dwo_file->comp_dir = comp_dir;
11657 dwo_file->sections.abbrev = sections.abbrev;
11658 dwo_file->sections.line = sections.line;
11659 dwo_file->sections.loc = sections.loc;
11660 dwo_file->sections.macinfo = sections.macinfo;
11661 dwo_file->sections.macro = sections.macro;
11662 dwo_file->sections.str_offsets = sections.str_offsets;
11663 /* The "str" section is global to the entire DWP file. */
11664 dwo_file->sections.str = dwp_file->sections.str;
11665 /* The info or types section is assigned below to dwo_unit,
11666 there's no need to record it in dwo_file.
11667 Also, we can't simply record type sections in dwo_file because
11668 we record a pointer into the vector in dwo_unit. As we collect more
11669 types we'll grow the vector and eventually have to reallocate space
11670 for it, invalidating all copies of pointers into the previous
11671 contents. */
11672 *dwo_file_slot = dwo_file;
11673 }
11674 else
11675 {
11676 if (dwarf_read_debug)
11677 {
11678 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11679 virtual_dwo_name.c_str ());
11680 }
11681 dwo_file = (struct dwo_file *) *dwo_file_slot;
11682 }
11683
11684 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11685 dwo_unit->dwo_file = dwo_file;
11686 dwo_unit->signature = signature;
11687 dwo_unit->section =
11688 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11689 *dwo_unit->section = sections.info_or_types;
11690 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11691
11692 return dwo_unit;
11693 }
11694
11695 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11696 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11697 piece within that section used by a TU/CU, return a virtual section
11698 of just that piece. */
11699
11700 static struct dwarf2_section_info
11701 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11702 struct dwarf2_section_info *section,
11703 bfd_size_type offset, bfd_size_type size)
11704 {
11705 struct dwarf2_section_info result;
11706 asection *sectp;
11707
11708 gdb_assert (section != NULL);
11709 gdb_assert (!section->is_virtual);
11710
11711 memset (&result, 0, sizeof (result));
11712 result.s.containing_section = section;
11713 result.is_virtual = true;
11714
11715 if (size == 0)
11716 return result;
11717
11718 sectp = section->get_bfd_section ();
11719
11720 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11721 bounds of the real section. This is a pretty-rare event, so just
11722 flag an error (easier) instead of a warning and trying to cope. */
11723 if (sectp == NULL
11724 || offset + size > bfd_section_size (sectp))
11725 {
11726 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11727 " in section %s [in module %s]"),
11728 sectp ? bfd_section_name (sectp) : "<unknown>",
11729 objfile_name (dwarf2_per_objfile->objfile));
11730 }
11731
11732 result.virtual_offset = offset;
11733 result.size = size;
11734 return result;
11735 }
11736
11737 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11738 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11739 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11740 This is for DWP version 2 files. */
11741
11742 static struct dwo_unit *
11743 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11744 struct dwp_file *dwp_file,
11745 uint32_t unit_index,
11746 const char *comp_dir,
11747 ULONGEST signature, int is_debug_types)
11748 {
11749 struct objfile *objfile = dwarf2_per_objfile->objfile;
11750 const struct dwp_hash_table *dwp_htab =
11751 is_debug_types ? dwp_file->tus : dwp_file->cus;
11752 bfd *dbfd = dwp_file->dbfd.get ();
11753 const char *kind = is_debug_types ? "TU" : "CU";
11754 struct dwo_file *dwo_file;
11755 struct dwo_unit *dwo_unit;
11756 struct virtual_v2_dwo_sections sections;
11757 void **dwo_file_slot;
11758 int i;
11759
11760 gdb_assert (dwp_file->version == 2);
11761
11762 if (dwarf_read_debug)
11763 {
11764 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11765 kind,
11766 pulongest (unit_index), hex_string (signature),
11767 dwp_file->name);
11768 }
11769
11770 /* Fetch the section offsets of this DWO unit. */
11771
11772 memset (&sections, 0, sizeof (sections));
11773
11774 for (i = 0; i < dwp_htab->nr_columns; ++i)
11775 {
11776 uint32_t offset = read_4_bytes (dbfd,
11777 dwp_htab->section_pool.v2.offsets
11778 + (((unit_index - 1) * dwp_htab->nr_columns
11779 + i)
11780 * sizeof (uint32_t)));
11781 uint32_t size = read_4_bytes (dbfd,
11782 dwp_htab->section_pool.v2.sizes
11783 + (((unit_index - 1) * dwp_htab->nr_columns
11784 + i)
11785 * sizeof (uint32_t)));
11786
11787 switch (dwp_htab->section_pool.v2.section_ids[i])
11788 {
11789 case DW_SECT_INFO:
11790 case DW_SECT_TYPES:
11791 sections.info_or_types_offset = offset;
11792 sections.info_or_types_size = size;
11793 break;
11794 case DW_SECT_ABBREV:
11795 sections.abbrev_offset = offset;
11796 sections.abbrev_size = size;
11797 break;
11798 case DW_SECT_LINE:
11799 sections.line_offset = offset;
11800 sections.line_size = size;
11801 break;
11802 case DW_SECT_LOC:
11803 sections.loc_offset = offset;
11804 sections.loc_size = size;
11805 break;
11806 case DW_SECT_STR_OFFSETS:
11807 sections.str_offsets_offset = offset;
11808 sections.str_offsets_size = size;
11809 break;
11810 case DW_SECT_MACINFO:
11811 sections.macinfo_offset = offset;
11812 sections.macinfo_size = size;
11813 break;
11814 case DW_SECT_MACRO:
11815 sections.macro_offset = offset;
11816 sections.macro_size = size;
11817 break;
11818 }
11819 }
11820
11821 /* It's easier for the rest of the code if we fake a struct dwo_file and
11822 have dwo_unit "live" in that. At least for now.
11823
11824 The DWP file can be made up of a random collection of CUs and TUs.
11825 However, for each CU + set of TUs that came from the same original DWO
11826 file, we can combine them back into a virtual DWO file to save space
11827 (fewer struct dwo_file objects to allocate). Remember that for really
11828 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11829
11830 std::string virtual_dwo_name =
11831 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11832 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11833 (long) (sections.line_size ? sections.line_offset : 0),
11834 (long) (sections.loc_size ? sections.loc_offset : 0),
11835 (long) (sections.str_offsets_size
11836 ? sections.str_offsets_offset : 0));
11837 /* Can we use an existing virtual DWO file? */
11838 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11839 virtual_dwo_name.c_str (),
11840 comp_dir);
11841 /* Create one if necessary. */
11842 if (*dwo_file_slot == NULL)
11843 {
11844 if (dwarf_read_debug)
11845 {
11846 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11847 virtual_dwo_name.c_str ());
11848 }
11849 dwo_file = new struct dwo_file;
11850 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11851 virtual_dwo_name);
11852 dwo_file->comp_dir = comp_dir;
11853 dwo_file->sections.abbrev =
11854 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11855 sections.abbrev_offset, sections.abbrev_size);
11856 dwo_file->sections.line =
11857 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11858 sections.line_offset, sections.line_size);
11859 dwo_file->sections.loc =
11860 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11861 sections.loc_offset, sections.loc_size);
11862 dwo_file->sections.macinfo =
11863 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11864 sections.macinfo_offset, sections.macinfo_size);
11865 dwo_file->sections.macro =
11866 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11867 sections.macro_offset, sections.macro_size);
11868 dwo_file->sections.str_offsets =
11869 create_dwp_v2_section (dwarf2_per_objfile,
11870 &dwp_file->sections.str_offsets,
11871 sections.str_offsets_offset,
11872 sections.str_offsets_size);
11873 /* The "str" section is global to the entire DWP file. */
11874 dwo_file->sections.str = dwp_file->sections.str;
11875 /* The info or types section is assigned below to dwo_unit,
11876 there's no need to record it in dwo_file.
11877 Also, we can't simply record type sections in dwo_file because
11878 we record a pointer into the vector in dwo_unit. As we collect more
11879 types we'll grow the vector and eventually have to reallocate space
11880 for it, invalidating all copies of pointers into the previous
11881 contents. */
11882 *dwo_file_slot = dwo_file;
11883 }
11884 else
11885 {
11886 if (dwarf_read_debug)
11887 {
11888 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11889 virtual_dwo_name.c_str ());
11890 }
11891 dwo_file = (struct dwo_file *) *dwo_file_slot;
11892 }
11893
11894 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11895 dwo_unit->dwo_file = dwo_file;
11896 dwo_unit->signature = signature;
11897 dwo_unit->section =
11898 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11899 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11900 is_debug_types
11901 ? &dwp_file->sections.types
11902 : &dwp_file->sections.info,
11903 sections.info_or_types_offset,
11904 sections.info_or_types_size);
11905 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11906
11907 return dwo_unit;
11908 }
11909
11910 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11911 Returns NULL if the signature isn't found. */
11912
11913 static struct dwo_unit *
11914 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11915 struct dwp_file *dwp_file, const char *comp_dir,
11916 ULONGEST signature, int is_debug_types)
11917 {
11918 const struct dwp_hash_table *dwp_htab =
11919 is_debug_types ? dwp_file->tus : dwp_file->cus;
11920 bfd *dbfd = dwp_file->dbfd.get ();
11921 uint32_t mask = dwp_htab->nr_slots - 1;
11922 uint32_t hash = signature & mask;
11923 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11924 unsigned int i;
11925 void **slot;
11926 struct dwo_unit find_dwo_cu;
11927
11928 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11929 find_dwo_cu.signature = signature;
11930 slot = htab_find_slot (is_debug_types
11931 ? dwp_file->loaded_tus.get ()
11932 : dwp_file->loaded_cus.get (),
11933 &find_dwo_cu, INSERT);
11934
11935 if (*slot != NULL)
11936 return (struct dwo_unit *) *slot;
11937
11938 /* Use a for loop so that we don't loop forever on bad debug info. */
11939 for (i = 0; i < dwp_htab->nr_slots; ++i)
11940 {
11941 ULONGEST signature_in_table;
11942
11943 signature_in_table =
11944 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11945 if (signature_in_table == signature)
11946 {
11947 uint32_t unit_index =
11948 read_4_bytes (dbfd,
11949 dwp_htab->unit_table + hash * sizeof (uint32_t));
11950
11951 if (dwp_file->version == 1)
11952 {
11953 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11954 dwp_file, unit_index,
11955 comp_dir, signature,
11956 is_debug_types);
11957 }
11958 else
11959 {
11960 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11961 dwp_file, unit_index,
11962 comp_dir, signature,
11963 is_debug_types);
11964 }
11965 return (struct dwo_unit *) *slot;
11966 }
11967 if (signature_in_table == 0)
11968 return NULL;
11969 hash = (hash + hash2) & mask;
11970 }
11971
11972 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11973 " [in module %s]"),
11974 dwp_file->name);
11975 }
11976
11977 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11978 Open the file specified by FILE_NAME and hand it off to BFD for
11979 preliminary analysis. Return a newly initialized bfd *, which
11980 includes a canonicalized copy of FILE_NAME.
11981 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11982 SEARCH_CWD is true if the current directory is to be searched.
11983 It will be searched before debug-file-directory.
11984 If successful, the file is added to the bfd include table of the
11985 objfile's bfd (see gdb_bfd_record_inclusion).
11986 If unable to find/open the file, return NULL.
11987 NOTE: This function is derived from symfile_bfd_open. */
11988
11989 static gdb_bfd_ref_ptr
11990 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11991 const char *file_name, int is_dwp, int search_cwd)
11992 {
11993 int desc;
11994 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11995 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11996 to debug_file_directory. */
11997 const char *search_path;
11998 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11999
12000 gdb::unique_xmalloc_ptr<char> search_path_holder;
12001 if (search_cwd)
12002 {
12003 if (*debug_file_directory != '\0')
12004 {
12005 search_path_holder.reset (concat (".", dirname_separator_string,
12006 debug_file_directory,
12007 (char *) NULL));
12008 search_path = search_path_holder.get ();
12009 }
12010 else
12011 search_path = ".";
12012 }
12013 else
12014 search_path = debug_file_directory;
12015
12016 openp_flags flags = OPF_RETURN_REALPATH;
12017 if (is_dwp)
12018 flags |= OPF_SEARCH_IN_PATH;
12019
12020 gdb::unique_xmalloc_ptr<char> absolute_name;
12021 desc = openp (search_path, flags, file_name,
12022 O_RDONLY | O_BINARY, &absolute_name);
12023 if (desc < 0)
12024 return NULL;
12025
12026 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12027 gnutarget, desc));
12028 if (sym_bfd == NULL)
12029 return NULL;
12030 bfd_set_cacheable (sym_bfd.get (), 1);
12031
12032 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12033 return NULL;
12034
12035 /* Success. Record the bfd as having been included by the objfile's bfd.
12036 This is important because things like demangled_names_hash lives in the
12037 objfile's per_bfd space and may have references to things like symbol
12038 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12039 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12040
12041 return sym_bfd;
12042 }
12043
12044 /* Try to open DWO file FILE_NAME.
12045 COMP_DIR is the DW_AT_comp_dir attribute.
12046 The result is the bfd handle of the file.
12047 If there is a problem finding or opening the file, return NULL.
12048 Upon success, the canonicalized path of the file is stored in the bfd,
12049 same as symfile_bfd_open. */
12050
12051 static gdb_bfd_ref_ptr
12052 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12053 const char *file_name, const char *comp_dir)
12054 {
12055 if (IS_ABSOLUTE_PATH (file_name))
12056 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12057 0 /*is_dwp*/, 0 /*search_cwd*/);
12058
12059 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12060
12061 if (comp_dir != NULL)
12062 {
12063 gdb::unique_xmalloc_ptr<char> path_to_try
12064 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12065
12066 /* NOTE: If comp_dir is a relative path, this will also try the
12067 search path, which seems useful. */
12068 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12069 path_to_try.get (),
12070 0 /*is_dwp*/,
12071 1 /*search_cwd*/));
12072 if (abfd != NULL)
12073 return abfd;
12074 }
12075
12076 /* That didn't work, try debug-file-directory, which, despite its name,
12077 is a list of paths. */
12078
12079 if (*debug_file_directory == '\0')
12080 return NULL;
12081
12082 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12083 0 /*is_dwp*/, 1 /*search_cwd*/);
12084 }
12085
12086 /* This function is mapped across the sections and remembers the offset and
12087 size of each of the DWO debugging sections we are interested in. */
12088
12089 static void
12090 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12091 {
12092 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12093 const struct dwop_section_names *names = &dwop_section_names;
12094
12095 if (section_is_p (sectp->name, &names->abbrev_dwo))
12096 {
12097 dwo_sections->abbrev.s.section = sectp;
12098 dwo_sections->abbrev.size = bfd_section_size (sectp);
12099 }
12100 else if (section_is_p (sectp->name, &names->info_dwo))
12101 {
12102 dwo_sections->info.s.section = sectp;
12103 dwo_sections->info.size = bfd_section_size (sectp);
12104 }
12105 else if (section_is_p (sectp->name, &names->line_dwo))
12106 {
12107 dwo_sections->line.s.section = sectp;
12108 dwo_sections->line.size = bfd_section_size (sectp);
12109 }
12110 else if (section_is_p (sectp->name, &names->loc_dwo))
12111 {
12112 dwo_sections->loc.s.section = sectp;
12113 dwo_sections->loc.size = bfd_section_size (sectp);
12114 }
12115 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12116 {
12117 dwo_sections->macinfo.s.section = sectp;
12118 dwo_sections->macinfo.size = bfd_section_size (sectp);
12119 }
12120 else if (section_is_p (sectp->name, &names->macro_dwo))
12121 {
12122 dwo_sections->macro.s.section = sectp;
12123 dwo_sections->macro.size = bfd_section_size (sectp);
12124 }
12125 else if (section_is_p (sectp->name, &names->str_dwo))
12126 {
12127 dwo_sections->str.s.section = sectp;
12128 dwo_sections->str.size = bfd_section_size (sectp);
12129 }
12130 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12131 {
12132 dwo_sections->str_offsets.s.section = sectp;
12133 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12134 }
12135 else if (section_is_p (sectp->name, &names->types_dwo))
12136 {
12137 struct dwarf2_section_info type_section;
12138
12139 memset (&type_section, 0, sizeof (type_section));
12140 type_section.s.section = sectp;
12141 type_section.size = bfd_section_size (sectp);
12142 dwo_sections->types.push_back (type_section);
12143 }
12144 }
12145
12146 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12147 by PER_CU. This is for the non-DWP case.
12148 The result is NULL if DWO_NAME can't be found. */
12149
12150 static struct dwo_file *
12151 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12152 const char *dwo_name, const char *comp_dir)
12153 {
12154 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12155
12156 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12157 if (dbfd == NULL)
12158 {
12159 if (dwarf_read_debug)
12160 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12161 return NULL;
12162 }
12163
12164 dwo_file_up dwo_file (new struct dwo_file);
12165 dwo_file->dwo_name = dwo_name;
12166 dwo_file->comp_dir = comp_dir;
12167 dwo_file->dbfd = std::move (dbfd);
12168
12169 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12170 &dwo_file->sections);
12171
12172 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12173 dwo_file->sections.info, dwo_file->cus);
12174
12175 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12176 dwo_file->sections.types, dwo_file->tus);
12177
12178 if (dwarf_read_debug)
12179 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12180
12181 return dwo_file.release ();
12182 }
12183
12184 /* This function is mapped across the sections and remembers the offset and
12185 size of each of the DWP debugging sections common to version 1 and 2 that
12186 we are interested in. */
12187
12188 static void
12189 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12190 void *dwp_file_ptr)
12191 {
12192 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12193 const struct dwop_section_names *names = &dwop_section_names;
12194 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12195
12196 /* Record the ELF section number for later lookup: this is what the
12197 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12198 gdb_assert (elf_section_nr < dwp_file->num_sections);
12199 dwp_file->elf_sections[elf_section_nr] = sectp;
12200
12201 /* Look for specific sections that we need. */
12202 if (section_is_p (sectp->name, &names->str_dwo))
12203 {
12204 dwp_file->sections.str.s.section = sectp;
12205 dwp_file->sections.str.size = bfd_section_size (sectp);
12206 }
12207 else if (section_is_p (sectp->name, &names->cu_index))
12208 {
12209 dwp_file->sections.cu_index.s.section = sectp;
12210 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12211 }
12212 else if (section_is_p (sectp->name, &names->tu_index))
12213 {
12214 dwp_file->sections.tu_index.s.section = sectp;
12215 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12216 }
12217 }
12218
12219 /* This function is mapped across the sections and remembers the offset and
12220 size of each of the DWP version 2 debugging sections that we are interested
12221 in. This is split into a separate function because we don't know if we
12222 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12223
12224 static void
12225 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12226 {
12227 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12228 const struct dwop_section_names *names = &dwop_section_names;
12229 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12230
12231 /* Record the ELF section number for later lookup: this is what the
12232 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12233 gdb_assert (elf_section_nr < dwp_file->num_sections);
12234 dwp_file->elf_sections[elf_section_nr] = sectp;
12235
12236 /* Look for specific sections that we need. */
12237 if (section_is_p (sectp->name, &names->abbrev_dwo))
12238 {
12239 dwp_file->sections.abbrev.s.section = sectp;
12240 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12241 }
12242 else if (section_is_p (sectp->name, &names->info_dwo))
12243 {
12244 dwp_file->sections.info.s.section = sectp;
12245 dwp_file->sections.info.size = bfd_section_size (sectp);
12246 }
12247 else if (section_is_p (sectp->name, &names->line_dwo))
12248 {
12249 dwp_file->sections.line.s.section = sectp;
12250 dwp_file->sections.line.size = bfd_section_size (sectp);
12251 }
12252 else if (section_is_p (sectp->name, &names->loc_dwo))
12253 {
12254 dwp_file->sections.loc.s.section = sectp;
12255 dwp_file->sections.loc.size = bfd_section_size (sectp);
12256 }
12257 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12258 {
12259 dwp_file->sections.macinfo.s.section = sectp;
12260 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12261 }
12262 else if (section_is_p (sectp->name, &names->macro_dwo))
12263 {
12264 dwp_file->sections.macro.s.section = sectp;
12265 dwp_file->sections.macro.size = bfd_section_size (sectp);
12266 }
12267 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12268 {
12269 dwp_file->sections.str_offsets.s.section = sectp;
12270 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12271 }
12272 else if (section_is_p (sectp->name, &names->types_dwo))
12273 {
12274 dwp_file->sections.types.s.section = sectp;
12275 dwp_file->sections.types.size = bfd_section_size (sectp);
12276 }
12277 }
12278
12279 /* Hash function for dwp_file loaded CUs/TUs. */
12280
12281 static hashval_t
12282 hash_dwp_loaded_cutus (const void *item)
12283 {
12284 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12285
12286 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12287 return dwo_unit->signature;
12288 }
12289
12290 /* Equality function for dwp_file loaded CUs/TUs. */
12291
12292 static int
12293 eq_dwp_loaded_cutus (const void *a, const void *b)
12294 {
12295 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12296 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12297
12298 return dua->signature == dub->signature;
12299 }
12300
12301 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12302
12303 static htab_up
12304 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12305 {
12306 return htab_up (htab_create_alloc (3,
12307 hash_dwp_loaded_cutus,
12308 eq_dwp_loaded_cutus,
12309 NULL, xcalloc, xfree));
12310 }
12311
12312 /* Try to open DWP file FILE_NAME.
12313 The result is the bfd handle of the file.
12314 If there is a problem finding or opening the file, return NULL.
12315 Upon success, the canonicalized path of the file is stored in the bfd,
12316 same as symfile_bfd_open. */
12317
12318 static gdb_bfd_ref_ptr
12319 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12320 const char *file_name)
12321 {
12322 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12323 1 /*is_dwp*/,
12324 1 /*search_cwd*/));
12325 if (abfd != NULL)
12326 return abfd;
12327
12328 /* Work around upstream bug 15652.
12329 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12330 [Whether that's a "bug" is debatable, but it is getting in our way.]
12331 We have no real idea where the dwp file is, because gdb's realpath-ing
12332 of the executable's path may have discarded the needed info.
12333 [IWBN if the dwp file name was recorded in the executable, akin to
12334 .gnu_debuglink, but that doesn't exist yet.]
12335 Strip the directory from FILE_NAME and search again. */
12336 if (*debug_file_directory != '\0')
12337 {
12338 /* Don't implicitly search the current directory here.
12339 If the user wants to search "." to handle this case,
12340 it must be added to debug-file-directory. */
12341 return try_open_dwop_file (dwarf2_per_objfile,
12342 lbasename (file_name), 1 /*is_dwp*/,
12343 0 /*search_cwd*/);
12344 }
12345
12346 return NULL;
12347 }
12348
12349 /* Initialize the use of the DWP file for the current objfile.
12350 By convention the name of the DWP file is ${objfile}.dwp.
12351 The result is NULL if it can't be found. */
12352
12353 static std::unique_ptr<struct dwp_file>
12354 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12355 {
12356 struct objfile *objfile = dwarf2_per_objfile->objfile;
12357
12358 /* Try to find first .dwp for the binary file before any symbolic links
12359 resolving. */
12360
12361 /* If the objfile is a debug file, find the name of the real binary
12362 file and get the name of dwp file from there. */
12363 std::string dwp_name;
12364 if (objfile->separate_debug_objfile_backlink != NULL)
12365 {
12366 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12367 const char *backlink_basename = lbasename (backlink->original_name);
12368
12369 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12370 }
12371 else
12372 dwp_name = objfile->original_name;
12373
12374 dwp_name += ".dwp";
12375
12376 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12377 if (dbfd == NULL
12378 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12379 {
12380 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12381 dwp_name = objfile_name (objfile);
12382 dwp_name += ".dwp";
12383 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12384 }
12385
12386 if (dbfd == NULL)
12387 {
12388 if (dwarf_read_debug)
12389 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12390 return std::unique_ptr<dwp_file> ();
12391 }
12392
12393 const char *name = bfd_get_filename (dbfd.get ());
12394 std::unique_ptr<struct dwp_file> dwp_file
12395 (new struct dwp_file (name, std::move (dbfd)));
12396
12397 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12398 dwp_file->elf_sections =
12399 OBSTACK_CALLOC (&objfile->objfile_obstack,
12400 dwp_file->num_sections, asection *);
12401
12402 bfd_map_over_sections (dwp_file->dbfd.get (),
12403 dwarf2_locate_common_dwp_sections,
12404 dwp_file.get ());
12405
12406 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12407 0);
12408
12409 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12410 1);
12411
12412 /* The DWP file version is stored in the hash table. Oh well. */
12413 if (dwp_file->cus && dwp_file->tus
12414 && dwp_file->cus->version != dwp_file->tus->version)
12415 {
12416 /* Technically speaking, we should try to limp along, but this is
12417 pretty bizarre. We use pulongest here because that's the established
12418 portability solution (e.g, we cannot use %u for uint32_t). */
12419 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12420 " TU version %s [in DWP file %s]"),
12421 pulongest (dwp_file->cus->version),
12422 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12423 }
12424
12425 if (dwp_file->cus)
12426 dwp_file->version = dwp_file->cus->version;
12427 else if (dwp_file->tus)
12428 dwp_file->version = dwp_file->tus->version;
12429 else
12430 dwp_file->version = 2;
12431
12432 if (dwp_file->version == 2)
12433 bfd_map_over_sections (dwp_file->dbfd.get (),
12434 dwarf2_locate_v2_dwp_sections,
12435 dwp_file.get ());
12436
12437 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12438 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12439
12440 if (dwarf_read_debug)
12441 {
12442 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12443 fprintf_unfiltered (gdb_stdlog,
12444 " %s CUs, %s TUs\n",
12445 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12446 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12447 }
12448
12449 return dwp_file;
12450 }
12451
12452 /* Wrapper around open_and_init_dwp_file, only open it once. */
12453
12454 static struct dwp_file *
12455 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12456 {
12457 if (! dwarf2_per_objfile->dwp_checked)
12458 {
12459 dwarf2_per_objfile->dwp_file
12460 = open_and_init_dwp_file (dwarf2_per_objfile);
12461 dwarf2_per_objfile->dwp_checked = 1;
12462 }
12463 return dwarf2_per_objfile->dwp_file.get ();
12464 }
12465
12466 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12467 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12468 or in the DWP file for the objfile, referenced by THIS_UNIT.
12469 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12470 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12471
12472 This is called, for example, when wanting to read a variable with a
12473 complex location. Therefore we don't want to do file i/o for every call.
12474 Therefore we don't want to look for a DWO file on every call.
12475 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12476 then we check if we've already seen DWO_NAME, and only THEN do we check
12477 for a DWO file.
12478
12479 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12480 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12481
12482 static struct dwo_unit *
12483 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12484 const char *dwo_name, const char *comp_dir,
12485 ULONGEST signature, int is_debug_types)
12486 {
12487 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12488 struct objfile *objfile = dwarf2_per_objfile->objfile;
12489 const char *kind = is_debug_types ? "TU" : "CU";
12490 void **dwo_file_slot;
12491 struct dwo_file *dwo_file;
12492 struct dwp_file *dwp_file;
12493
12494 /* First see if there's a DWP file.
12495 If we have a DWP file but didn't find the DWO inside it, don't
12496 look for the original DWO file. It makes gdb behave differently
12497 depending on whether one is debugging in the build tree. */
12498
12499 dwp_file = get_dwp_file (dwarf2_per_objfile);
12500 if (dwp_file != NULL)
12501 {
12502 const struct dwp_hash_table *dwp_htab =
12503 is_debug_types ? dwp_file->tus : dwp_file->cus;
12504
12505 if (dwp_htab != NULL)
12506 {
12507 struct dwo_unit *dwo_cutu =
12508 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12509 signature, is_debug_types);
12510
12511 if (dwo_cutu != NULL)
12512 {
12513 if (dwarf_read_debug)
12514 {
12515 fprintf_unfiltered (gdb_stdlog,
12516 "Virtual DWO %s %s found: @%s\n",
12517 kind, hex_string (signature),
12518 host_address_to_string (dwo_cutu));
12519 }
12520 return dwo_cutu;
12521 }
12522 }
12523 }
12524 else
12525 {
12526 /* No DWP file, look for the DWO file. */
12527
12528 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12529 dwo_name, comp_dir);
12530 if (*dwo_file_slot == NULL)
12531 {
12532 /* Read in the file and build a table of the CUs/TUs it contains. */
12533 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12534 }
12535 /* NOTE: This will be NULL if unable to open the file. */
12536 dwo_file = (struct dwo_file *) *dwo_file_slot;
12537
12538 if (dwo_file != NULL)
12539 {
12540 struct dwo_unit *dwo_cutu = NULL;
12541
12542 if (is_debug_types && dwo_file->tus)
12543 {
12544 struct dwo_unit find_dwo_cutu;
12545
12546 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12547 find_dwo_cutu.signature = signature;
12548 dwo_cutu
12549 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12550 &find_dwo_cutu);
12551 }
12552 else if (!is_debug_types && dwo_file->cus)
12553 {
12554 struct dwo_unit find_dwo_cutu;
12555
12556 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12557 find_dwo_cutu.signature = signature;
12558 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12559 &find_dwo_cutu);
12560 }
12561
12562 if (dwo_cutu != NULL)
12563 {
12564 if (dwarf_read_debug)
12565 {
12566 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12567 kind, dwo_name, hex_string (signature),
12568 host_address_to_string (dwo_cutu));
12569 }
12570 return dwo_cutu;
12571 }
12572 }
12573 }
12574
12575 /* We didn't find it. This could mean a dwo_id mismatch, or
12576 someone deleted the DWO/DWP file, or the search path isn't set up
12577 correctly to find the file. */
12578
12579 if (dwarf_read_debug)
12580 {
12581 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12582 kind, dwo_name, hex_string (signature));
12583 }
12584
12585 /* This is a warning and not a complaint because it can be caused by
12586 pilot error (e.g., user accidentally deleting the DWO). */
12587 {
12588 /* Print the name of the DWP file if we looked there, helps the user
12589 better diagnose the problem. */
12590 std::string dwp_text;
12591
12592 if (dwp_file != NULL)
12593 dwp_text = string_printf (" [in DWP file %s]",
12594 lbasename (dwp_file->name));
12595
12596 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12597 " [in module %s]"),
12598 kind, dwo_name, hex_string (signature),
12599 dwp_text.c_str (),
12600 this_unit->is_debug_types ? "TU" : "CU",
12601 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12602 }
12603 return NULL;
12604 }
12605
12606 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12607 See lookup_dwo_cutu_unit for details. */
12608
12609 static struct dwo_unit *
12610 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12611 const char *dwo_name, const char *comp_dir,
12612 ULONGEST signature)
12613 {
12614 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12615 }
12616
12617 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12618 See lookup_dwo_cutu_unit for details. */
12619
12620 static struct dwo_unit *
12621 lookup_dwo_type_unit (struct signatured_type *this_tu,
12622 const char *dwo_name, const char *comp_dir)
12623 {
12624 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12625 }
12626
12627 /* Traversal function for queue_and_load_all_dwo_tus. */
12628
12629 static int
12630 queue_and_load_dwo_tu (void **slot, void *info)
12631 {
12632 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12633 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12634 ULONGEST signature = dwo_unit->signature;
12635 struct signatured_type *sig_type =
12636 lookup_dwo_signatured_type (per_cu->cu, signature);
12637
12638 if (sig_type != NULL)
12639 {
12640 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12641
12642 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12643 a real dependency of PER_CU on SIG_TYPE. That is detected later
12644 while processing PER_CU. */
12645 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12646 load_full_type_unit (sig_cu);
12647 per_cu->imported_symtabs_push (sig_cu);
12648 }
12649
12650 return 1;
12651 }
12652
12653 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12654 The DWO may have the only definition of the type, though it may not be
12655 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12656 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12657
12658 static void
12659 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12660 {
12661 struct dwo_unit *dwo_unit;
12662 struct dwo_file *dwo_file;
12663
12664 gdb_assert (!per_cu->is_debug_types);
12665 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12666 gdb_assert (per_cu->cu != NULL);
12667
12668 dwo_unit = per_cu->cu->dwo_unit;
12669 gdb_assert (dwo_unit != NULL);
12670
12671 dwo_file = dwo_unit->dwo_file;
12672 if (dwo_file->tus != NULL)
12673 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12674 per_cu);
12675 }
12676
12677 /* Read in various DIEs. */
12678
12679 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12680 Inherit only the children of the DW_AT_abstract_origin DIE not being
12681 already referenced by DW_AT_abstract_origin from the children of the
12682 current DIE. */
12683
12684 static void
12685 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12686 {
12687 struct die_info *child_die;
12688 sect_offset *offsetp;
12689 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12690 struct die_info *origin_die;
12691 /* Iterator of the ORIGIN_DIE children. */
12692 struct die_info *origin_child_die;
12693 struct attribute *attr;
12694 struct dwarf2_cu *origin_cu;
12695 struct pending **origin_previous_list_in_scope;
12696
12697 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12698 if (!attr)
12699 return;
12700
12701 /* Note that following die references may follow to a die in a
12702 different cu. */
12703
12704 origin_cu = cu;
12705 origin_die = follow_die_ref (die, attr, &origin_cu);
12706
12707 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12708 symbols in. */
12709 origin_previous_list_in_scope = origin_cu->list_in_scope;
12710 origin_cu->list_in_scope = cu->list_in_scope;
12711
12712 if (die->tag != origin_die->tag
12713 && !(die->tag == DW_TAG_inlined_subroutine
12714 && origin_die->tag == DW_TAG_subprogram))
12715 complaint (_("DIE %s and its abstract origin %s have different tags"),
12716 sect_offset_str (die->sect_off),
12717 sect_offset_str (origin_die->sect_off));
12718
12719 std::vector<sect_offset> offsets;
12720
12721 for (child_die = die->child;
12722 child_die && child_die->tag;
12723 child_die = sibling_die (child_die))
12724 {
12725 struct die_info *child_origin_die;
12726 struct dwarf2_cu *child_origin_cu;
12727
12728 /* We are trying to process concrete instance entries:
12729 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12730 it's not relevant to our analysis here. i.e. detecting DIEs that are
12731 present in the abstract instance but not referenced in the concrete
12732 one. */
12733 if (child_die->tag == DW_TAG_call_site
12734 || child_die->tag == DW_TAG_GNU_call_site)
12735 continue;
12736
12737 /* For each CHILD_DIE, find the corresponding child of
12738 ORIGIN_DIE. If there is more than one layer of
12739 DW_AT_abstract_origin, follow them all; there shouldn't be,
12740 but GCC versions at least through 4.4 generate this (GCC PR
12741 40573). */
12742 child_origin_die = child_die;
12743 child_origin_cu = cu;
12744 while (1)
12745 {
12746 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12747 child_origin_cu);
12748 if (attr == NULL)
12749 break;
12750 child_origin_die = follow_die_ref (child_origin_die, attr,
12751 &child_origin_cu);
12752 }
12753
12754 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12755 counterpart may exist. */
12756 if (child_origin_die != child_die)
12757 {
12758 if (child_die->tag != child_origin_die->tag
12759 && !(child_die->tag == DW_TAG_inlined_subroutine
12760 && child_origin_die->tag == DW_TAG_subprogram))
12761 complaint (_("Child DIE %s and its abstract origin %s have "
12762 "different tags"),
12763 sect_offset_str (child_die->sect_off),
12764 sect_offset_str (child_origin_die->sect_off));
12765 if (child_origin_die->parent != origin_die)
12766 complaint (_("Child DIE %s and its abstract origin %s have "
12767 "different parents"),
12768 sect_offset_str (child_die->sect_off),
12769 sect_offset_str (child_origin_die->sect_off));
12770 else
12771 offsets.push_back (child_origin_die->sect_off);
12772 }
12773 }
12774 std::sort (offsets.begin (), offsets.end ());
12775 sect_offset *offsets_end = offsets.data () + offsets.size ();
12776 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12777 if (offsetp[-1] == *offsetp)
12778 complaint (_("Multiple children of DIE %s refer "
12779 "to DIE %s as their abstract origin"),
12780 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12781
12782 offsetp = offsets.data ();
12783 origin_child_die = origin_die->child;
12784 while (origin_child_die && origin_child_die->tag)
12785 {
12786 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12787 while (offsetp < offsets_end
12788 && *offsetp < origin_child_die->sect_off)
12789 offsetp++;
12790 if (offsetp >= offsets_end
12791 || *offsetp > origin_child_die->sect_off)
12792 {
12793 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12794 Check whether we're already processing ORIGIN_CHILD_DIE.
12795 This can happen with mutually referenced abstract_origins.
12796 PR 16581. */
12797 if (!origin_child_die->in_process)
12798 process_die (origin_child_die, origin_cu);
12799 }
12800 origin_child_die = sibling_die (origin_child_die);
12801 }
12802 origin_cu->list_in_scope = origin_previous_list_in_scope;
12803
12804 if (cu != origin_cu)
12805 compute_delayed_physnames (origin_cu);
12806 }
12807
12808 static void
12809 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12810 {
12811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12813 struct context_stack *newobj;
12814 CORE_ADDR lowpc;
12815 CORE_ADDR highpc;
12816 struct die_info *child_die;
12817 struct attribute *attr, *call_line, *call_file;
12818 const char *name;
12819 CORE_ADDR baseaddr;
12820 struct block *block;
12821 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12822 std::vector<struct symbol *> template_args;
12823 struct template_symbol *templ_func = NULL;
12824
12825 if (inlined_func)
12826 {
12827 /* If we do not have call site information, we can't show the
12828 caller of this inlined function. That's too confusing, so
12829 only use the scope for local variables. */
12830 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12831 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12832 if (call_line == NULL || call_file == NULL)
12833 {
12834 read_lexical_block_scope (die, cu);
12835 return;
12836 }
12837 }
12838
12839 baseaddr = objfile->text_section_offset ();
12840
12841 name = dwarf2_name (die, cu);
12842
12843 /* Ignore functions with missing or empty names. These are actually
12844 illegal according to the DWARF standard. */
12845 if (name == NULL)
12846 {
12847 complaint (_("missing name for subprogram DIE at %s"),
12848 sect_offset_str (die->sect_off));
12849 return;
12850 }
12851
12852 /* Ignore functions with missing or invalid low and high pc attributes. */
12853 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12854 <= PC_BOUNDS_INVALID)
12855 {
12856 attr = dwarf2_attr (die, DW_AT_external, cu);
12857 if (!attr || !DW_UNSND (attr))
12858 complaint (_("cannot get low and high bounds "
12859 "for subprogram DIE at %s"),
12860 sect_offset_str (die->sect_off));
12861 return;
12862 }
12863
12864 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12865 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12866
12867 /* If we have any template arguments, then we must allocate a
12868 different sort of symbol. */
12869 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12870 {
12871 if (child_die->tag == DW_TAG_template_type_param
12872 || child_die->tag == DW_TAG_template_value_param)
12873 {
12874 templ_func = allocate_template_symbol (objfile);
12875 templ_func->subclass = SYMBOL_TEMPLATE;
12876 break;
12877 }
12878 }
12879
12880 newobj = cu->get_builder ()->push_context (0, lowpc);
12881 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12882 (struct symbol *) templ_func);
12883
12884 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12885 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12886 cu->language);
12887
12888 /* If there is a location expression for DW_AT_frame_base, record
12889 it. */
12890 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12891 if (attr != nullptr)
12892 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12893
12894 /* If there is a location for the static link, record it. */
12895 newobj->static_link = NULL;
12896 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12897 if (attr != nullptr)
12898 {
12899 newobj->static_link
12900 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12901 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12902 cu->per_cu->addr_type ());
12903 }
12904
12905 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12906
12907 if (die->child != NULL)
12908 {
12909 child_die = die->child;
12910 while (child_die && child_die->tag)
12911 {
12912 if (child_die->tag == DW_TAG_template_type_param
12913 || child_die->tag == DW_TAG_template_value_param)
12914 {
12915 struct symbol *arg = new_symbol (child_die, NULL, cu);
12916
12917 if (arg != NULL)
12918 template_args.push_back (arg);
12919 }
12920 else
12921 process_die (child_die, cu);
12922 child_die = sibling_die (child_die);
12923 }
12924 }
12925
12926 inherit_abstract_dies (die, cu);
12927
12928 /* If we have a DW_AT_specification, we might need to import using
12929 directives from the context of the specification DIE. See the
12930 comment in determine_prefix. */
12931 if (cu->language == language_cplus
12932 && dwarf2_attr (die, DW_AT_specification, cu))
12933 {
12934 struct dwarf2_cu *spec_cu = cu;
12935 struct die_info *spec_die = die_specification (die, &spec_cu);
12936
12937 while (spec_die)
12938 {
12939 child_die = spec_die->child;
12940 while (child_die && child_die->tag)
12941 {
12942 if (child_die->tag == DW_TAG_imported_module)
12943 process_die (child_die, spec_cu);
12944 child_die = sibling_die (child_die);
12945 }
12946
12947 /* In some cases, GCC generates specification DIEs that
12948 themselves contain DW_AT_specification attributes. */
12949 spec_die = die_specification (spec_die, &spec_cu);
12950 }
12951 }
12952
12953 struct context_stack cstk = cu->get_builder ()->pop_context ();
12954 /* Make a block for the local symbols within. */
12955 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12956 cstk.static_link, lowpc, highpc);
12957
12958 /* For C++, set the block's scope. */
12959 if ((cu->language == language_cplus
12960 || cu->language == language_fortran
12961 || cu->language == language_d
12962 || cu->language == language_rust)
12963 && cu->processing_has_namespace_info)
12964 block_set_scope (block, determine_prefix (die, cu),
12965 &objfile->objfile_obstack);
12966
12967 /* If we have address ranges, record them. */
12968 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12969
12970 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12971
12972 /* Attach template arguments to function. */
12973 if (!template_args.empty ())
12974 {
12975 gdb_assert (templ_func != NULL);
12976
12977 templ_func->n_template_arguments = template_args.size ();
12978 templ_func->template_arguments
12979 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12980 templ_func->n_template_arguments);
12981 memcpy (templ_func->template_arguments,
12982 template_args.data (),
12983 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12984
12985 /* Make sure that the symtab is set on the new symbols. Even
12986 though they don't appear in this symtab directly, other parts
12987 of gdb assume that symbols do, and this is reasonably
12988 true. */
12989 for (symbol *sym : template_args)
12990 symbol_set_symtab (sym, symbol_symtab (templ_func));
12991 }
12992
12993 /* In C++, we can have functions nested inside functions (e.g., when
12994 a function declares a class that has methods). This means that
12995 when we finish processing a function scope, we may need to go
12996 back to building a containing block's symbol lists. */
12997 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12998 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12999
13000 /* If we've finished processing a top-level function, subsequent
13001 symbols go in the file symbol list. */
13002 if (cu->get_builder ()->outermost_context_p ())
13003 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13004 }
13005
13006 /* Process all the DIES contained within a lexical block scope. Start
13007 a new scope, process the dies, and then close the scope. */
13008
13009 static void
13010 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13011 {
13012 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13013 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13014 CORE_ADDR lowpc, highpc;
13015 struct die_info *child_die;
13016 CORE_ADDR baseaddr;
13017
13018 baseaddr = objfile->text_section_offset ();
13019
13020 /* Ignore blocks with missing or invalid low and high pc attributes. */
13021 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13022 as multiple lexical blocks? Handling children in a sane way would
13023 be nasty. Might be easier to properly extend generic blocks to
13024 describe ranges. */
13025 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13026 {
13027 case PC_BOUNDS_NOT_PRESENT:
13028 /* DW_TAG_lexical_block has no attributes, process its children as if
13029 there was no wrapping by that DW_TAG_lexical_block.
13030 GCC does no longer produces such DWARF since GCC r224161. */
13031 for (child_die = die->child;
13032 child_die != NULL && child_die->tag;
13033 child_die = sibling_die (child_die))
13034 process_die (child_die, cu);
13035 return;
13036 case PC_BOUNDS_INVALID:
13037 return;
13038 }
13039 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13040 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13041
13042 cu->get_builder ()->push_context (0, lowpc);
13043 if (die->child != NULL)
13044 {
13045 child_die = die->child;
13046 while (child_die && child_die->tag)
13047 {
13048 process_die (child_die, cu);
13049 child_die = sibling_die (child_die);
13050 }
13051 }
13052 inherit_abstract_dies (die, cu);
13053 struct context_stack cstk = cu->get_builder ()->pop_context ();
13054
13055 if (*cu->get_builder ()->get_local_symbols () != NULL
13056 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13057 {
13058 struct block *block
13059 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13060 cstk.start_addr, highpc);
13061
13062 /* Note that recording ranges after traversing children, as we
13063 do here, means that recording a parent's ranges entails
13064 walking across all its children's ranges as they appear in
13065 the address map, which is quadratic behavior.
13066
13067 It would be nicer to record the parent's ranges before
13068 traversing its children, simply overriding whatever you find
13069 there. But since we don't even decide whether to create a
13070 block until after we've traversed its children, that's hard
13071 to do. */
13072 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13073 }
13074 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13075 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13076 }
13077
13078 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13079
13080 static void
13081 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13082 {
13083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13084 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13085 CORE_ADDR pc, baseaddr;
13086 struct attribute *attr;
13087 struct call_site *call_site, call_site_local;
13088 void **slot;
13089 int nparams;
13090 struct die_info *child_die;
13091
13092 baseaddr = objfile->text_section_offset ();
13093
13094 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13095 if (attr == NULL)
13096 {
13097 /* This was a pre-DWARF-5 GNU extension alias
13098 for DW_AT_call_return_pc. */
13099 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13100 }
13101 if (!attr)
13102 {
13103 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13104 "DIE %s [in module %s]"),
13105 sect_offset_str (die->sect_off), objfile_name (objfile));
13106 return;
13107 }
13108 pc = attr->value_as_address () + baseaddr;
13109 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13110
13111 if (cu->call_site_htab == NULL)
13112 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13113 NULL, &objfile->objfile_obstack,
13114 hashtab_obstack_allocate, NULL);
13115 call_site_local.pc = pc;
13116 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13117 if (*slot != NULL)
13118 {
13119 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13120 "DIE %s [in module %s]"),
13121 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13122 objfile_name (objfile));
13123 return;
13124 }
13125
13126 /* Count parameters at the caller. */
13127
13128 nparams = 0;
13129 for (child_die = die->child; child_die && child_die->tag;
13130 child_die = sibling_die (child_die))
13131 {
13132 if (child_die->tag != DW_TAG_call_site_parameter
13133 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13134 {
13135 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13136 "DW_TAG_call_site child DIE %s [in module %s]"),
13137 child_die->tag, sect_offset_str (child_die->sect_off),
13138 objfile_name (objfile));
13139 continue;
13140 }
13141
13142 nparams++;
13143 }
13144
13145 call_site
13146 = ((struct call_site *)
13147 obstack_alloc (&objfile->objfile_obstack,
13148 sizeof (*call_site)
13149 + (sizeof (*call_site->parameter) * (nparams - 1))));
13150 *slot = call_site;
13151 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13152 call_site->pc = pc;
13153
13154 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13155 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13156 {
13157 struct die_info *func_die;
13158
13159 /* Skip also over DW_TAG_inlined_subroutine. */
13160 for (func_die = die->parent;
13161 func_die && func_die->tag != DW_TAG_subprogram
13162 && func_die->tag != DW_TAG_subroutine_type;
13163 func_die = func_die->parent);
13164
13165 /* DW_AT_call_all_calls is a superset
13166 of DW_AT_call_all_tail_calls. */
13167 if (func_die
13168 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13169 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13170 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13171 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13172 {
13173 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13174 not complete. But keep CALL_SITE for look ups via call_site_htab,
13175 both the initial caller containing the real return address PC and
13176 the final callee containing the current PC of a chain of tail
13177 calls do not need to have the tail call list complete. But any
13178 function candidate for a virtual tail call frame searched via
13179 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13180 determined unambiguously. */
13181 }
13182 else
13183 {
13184 struct type *func_type = NULL;
13185
13186 if (func_die)
13187 func_type = get_die_type (func_die, cu);
13188 if (func_type != NULL)
13189 {
13190 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13191
13192 /* Enlist this call site to the function. */
13193 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13194 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13195 }
13196 else
13197 complaint (_("Cannot find function owning DW_TAG_call_site "
13198 "DIE %s [in module %s]"),
13199 sect_offset_str (die->sect_off), objfile_name (objfile));
13200 }
13201 }
13202
13203 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13204 if (attr == NULL)
13205 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13206 if (attr == NULL)
13207 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13208 if (attr == NULL)
13209 {
13210 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13211 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13212 }
13213 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13214 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13215 /* Keep NULL DWARF_BLOCK. */;
13216 else if (attr->form_is_block ())
13217 {
13218 struct dwarf2_locexpr_baton *dlbaton;
13219
13220 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13221 dlbaton->data = DW_BLOCK (attr)->data;
13222 dlbaton->size = DW_BLOCK (attr)->size;
13223 dlbaton->per_cu = cu->per_cu;
13224
13225 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13226 }
13227 else if (attr->form_is_ref ())
13228 {
13229 struct dwarf2_cu *target_cu = cu;
13230 struct die_info *target_die;
13231
13232 target_die = follow_die_ref (die, attr, &target_cu);
13233 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13234 if (die_is_declaration (target_die, target_cu))
13235 {
13236 const char *target_physname;
13237
13238 /* Prefer the mangled name; otherwise compute the demangled one. */
13239 target_physname = dw2_linkage_name (target_die, target_cu);
13240 if (target_physname == NULL)
13241 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13242 if (target_physname == NULL)
13243 complaint (_("DW_AT_call_target target DIE has invalid "
13244 "physname, for referencing DIE %s [in module %s]"),
13245 sect_offset_str (die->sect_off), objfile_name (objfile));
13246 else
13247 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13248 }
13249 else
13250 {
13251 CORE_ADDR lowpc;
13252
13253 /* DW_AT_entry_pc should be preferred. */
13254 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13255 <= PC_BOUNDS_INVALID)
13256 complaint (_("DW_AT_call_target target DIE has invalid "
13257 "low pc, for referencing DIE %s [in module %s]"),
13258 sect_offset_str (die->sect_off), objfile_name (objfile));
13259 else
13260 {
13261 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13262 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13263 }
13264 }
13265 }
13266 else
13267 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13268 "block nor reference, for DIE %s [in module %s]"),
13269 sect_offset_str (die->sect_off), objfile_name (objfile));
13270
13271 call_site->per_cu = cu->per_cu;
13272
13273 for (child_die = die->child;
13274 child_die && child_die->tag;
13275 child_die = sibling_die (child_die))
13276 {
13277 struct call_site_parameter *parameter;
13278 struct attribute *loc, *origin;
13279
13280 if (child_die->tag != DW_TAG_call_site_parameter
13281 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13282 {
13283 /* Already printed the complaint above. */
13284 continue;
13285 }
13286
13287 gdb_assert (call_site->parameter_count < nparams);
13288 parameter = &call_site->parameter[call_site->parameter_count];
13289
13290 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13291 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13292 register is contained in DW_AT_call_value. */
13293
13294 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13295 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13296 if (origin == NULL)
13297 {
13298 /* This was a pre-DWARF-5 GNU extension alias
13299 for DW_AT_call_parameter. */
13300 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13301 }
13302 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13303 {
13304 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13305
13306 sect_offset sect_off
13307 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13308 if (!cu->header.offset_in_cu_p (sect_off))
13309 {
13310 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13311 binding can be done only inside one CU. Such referenced DIE
13312 therefore cannot be even moved to DW_TAG_partial_unit. */
13313 complaint (_("DW_AT_call_parameter offset is not in CU for "
13314 "DW_TAG_call_site child DIE %s [in module %s]"),
13315 sect_offset_str (child_die->sect_off),
13316 objfile_name (objfile));
13317 continue;
13318 }
13319 parameter->u.param_cu_off
13320 = (cu_offset) (sect_off - cu->header.sect_off);
13321 }
13322 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13323 {
13324 complaint (_("No DW_FORM_block* DW_AT_location for "
13325 "DW_TAG_call_site child DIE %s [in module %s]"),
13326 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13327 continue;
13328 }
13329 else
13330 {
13331 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13332 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13333 if (parameter->u.dwarf_reg != -1)
13334 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13335 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13336 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13337 &parameter->u.fb_offset))
13338 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13339 else
13340 {
13341 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13342 "for DW_FORM_block* DW_AT_location is supported for "
13343 "DW_TAG_call_site child DIE %s "
13344 "[in module %s]"),
13345 sect_offset_str (child_die->sect_off),
13346 objfile_name (objfile));
13347 continue;
13348 }
13349 }
13350
13351 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13352 if (attr == NULL)
13353 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13354 if (attr == NULL || !attr->form_is_block ())
13355 {
13356 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13357 "DW_TAG_call_site child DIE %s [in module %s]"),
13358 sect_offset_str (child_die->sect_off),
13359 objfile_name (objfile));
13360 continue;
13361 }
13362 parameter->value = DW_BLOCK (attr)->data;
13363 parameter->value_size = DW_BLOCK (attr)->size;
13364
13365 /* Parameters are not pre-cleared by memset above. */
13366 parameter->data_value = NULL;
13367 parameter->data_value_size = 0;
13368 call_site->parameter_count++;
13369
13370 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13371 if (attr == NULL)
13372 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13373 if (attr != nullptr)
13374 {
13375 if (!attr->form_is_block ())
13376 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13377 "DW_TAG_call_site child DIE %s [in module %s]"),
13378 sect_offset_str (child_die->sect_off),
13379 objfile_name (objfile));
13380 else
13381 {
13382 parameter->data_value = DW_BLOCK (attr)->data;
13383 parameter->data_value_size = DW_BLOCK (attr)->size;
13384 }
13385 }
13386 }
13387 }
13388
13389 /* Helper function for read_variable. If DIE represents a virtual
13390 table, then return the type of the concrete object that is
13391 associated with the virtual table. Otherwise, return NULL. */
13392
13393 static struct type *
13394 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13395 {
13396 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13397 if (attr == NULL)
13398 return NULL;
13399
13400 /* Find the type DIE. */
13401 struct die_info *type_die = NULL;
13402 struct dwarf2_cu *type_cu = cu;
13403
13404 if (attr->form_is_ref ())
13405 type_die = follow_die_ref (die, attr, &type_cu);
13406 if (type_die == NULL)
13407 return NULL;
13408
13409 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13410 return NULL;
13411 return die_containing_type (type_die, type_cu);
13412 }
13413
13414 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13415
13416 static void
13417 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13418 {
13419 struct rust_vtable_symbol *storage = NULL;
13420
13421 if (cu->language == language_rust)
13422 {
13423 struct type *containing_type = rust_containing_type (die, cu);
13424
13425 if (containing_type != NULL)
13426 {
13427 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13428
13429 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13430 initialize_objfile_symbol (storage);
13431 storage->concrete_type = containing_type;
13432 storage->subclass = SYMBOL_RUST_VTABLE;
13433 }
13434 }
13435
13436 struct symbol *res = new_symbol (die, NULL, cu, storage);
13437 struct attribute *abstract_origin
13438 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13439 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13440 if (res == NULL && loc && abstract_origin)
13441 {
13442 /* We have a variable without a name, but with a location and an abstract
13443 origin. This may be a concrete instance of an abstract variable
13444 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13445 later. */
13446 struct dwarf2_cu *origin_cu = cu;
13447 struct die_info *origin_die
13448 = follow_die_ref (die, abstract_origin, &origin_cu);
13449 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13450 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13451 }
13452 }
13453
13454 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13455 reading .debug_rnglists.
13456 Callback's type should be:
13457 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13458 Return true if the attributes are present and valid, otherwise,
13459 return false. */
13460
13461 template <typename Callback>
13462 static bool
13463 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13464 Callback &&callback)
13465 {
13466 struct dwarf2_per_objfile *dwarf2_per_objfile
13467 = cu->per_cu->dwarf2_per_objfile;
13468 struct objfile *objfile = dwarf2_per_objfile->objfile;
13469 bfd *obfd = objfile->obfd;
13470 /* Base address selection entry. */
13471 CORE_ADDR base;
13472 int found_base;
13473 const gdb_byte *buffer;
13474 CORE_ADDR baseaddr;
13475 bool overflow = false;
13476
13477 found_base = cu->base_known;
13478 base = cu->base_address;
13479
13480 dwarf2_per_objfile->rnglists.read (objfile);
13481 if (offset >= dwarf2_per_objfile->rnglists.size)
13482 {
13483 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13484 offset);
13485 return false;
13486 }
13487 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13488
13489 baseaddr = objfile->text_section_offset ();
13490
13491 while (1)
13492 {
13493 /* Initialize it due to a false compiler warning. */
13494 CORE_ADDR range_beginning = 0, range_end = 0;
13495 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13496 + dwarf2_per_objfile->rnglists.size);
13497 unsigned int bytes_read;
13498
13499 if (buffer == buf_end)
13500 {
13501 overflow = true;
13502 break;
13503 }
13504 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13505 switch (rlet)
13506 {
13507 case DW_RLE_end_of_list:
13508 break;
13509 case DW_RLE_base_address:
13510 if (buffer + cu->header.addr_size > buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
13515 base = cu->header.read_address (obfd, buffer, &bytes_read);
13516 found_base = 1;
13517 buffer += bytes_read;
13518 break;
13519 case DW_RLE_start_length:
13520 if (buffer + cu->header.addr_size > buf_end)
13521 {
13522 overflow = true;
13523 break;
13524 }
13525 range_beginning = cu->header.read_address (obfd, buffer,
13526 &bytes_read);
13527 buffer += bytes_read;
13528 range_end = (range_beginning
13529 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13530 buffer += bytes_read;
13531 if (buffer > buf_end)
13532 {
13533 overflow = true;
13534 break;
13535 }
13536 break;
13537 case DW_RLE_offset_pair:
13538 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13539 buffer += bytes_read;
13540 if (buffer > buf_end)
13541 {
13542 overflow = true;
13543 break;
13544 }
13545 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13546 buffer += bytes_read;
13547 if (buffer > buf_end)
13548 {
13549 overflow = true;
13550 break;
13551 }
13552 break;
13553 case DW_RLE_start_end:
13554 if (buffer + 2 * cu->header.addr_size > buf_end)
13555 {
13556 overflow = true;
13557 break;
13558 }
13559 range_beginning = cu->header.read_address (obfd, buffer,
13560 &bytes_read);
13561 buffer += bytes_read;
13562 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13563 buffer += bytes_read;
13564 break;
13565 default:
13566 complaint (_("Invalid .debug_rnglists data (no base address)"));
13567 return false;
13568 }
13569 if (rlet == DW_RLE_end_of_list || overflow)
13570 break;
13571 if (rlet == DW_RLE_base_address)
13572 continue;
13573
13574 if (!found_base)
13575 {
13576 /* We have no valid base address for the ranges
13577 data. */
13578 complaint (_("Invalid .debug_rnglists data (no base address)"));
13579 return false;
13580 }
13581
13582 if (range_beginning > range_end)
13583 {
13584 /* Inverted range entries are invalid. */
13585 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13586 return false;
13587 }
13588
13589 /* Empty range entries have no effect. */
13590 if (range_beginning == range_end)
13591 continue;
13592
13593 range_beginning += base;
13594 range_end += base;
13595
13596 /* A not-uncommon case of bad debug info.
13597 Don't pollute the addrmap with bad data. */
13598 if (range_beginning + baseaddr == 0
13599 && !dwarf2_per_objfile->has_section_at_zero)
13600 {
13601 complaint (_(".debug_rnglists entry has start address of zero"
13602 " [in module %s]"), objfile_name (objfile));
13603 continue;
13604 }
13605
13606 callback (range_beginning, range_end);
13607 }
13608
13609 if (overflow)
13610 {
13611 complaint (_("Offset %d is not terminated "
13612 "for DW_AT_ranges attribute"),
13613 offset);
13614 return false;
13615 }
13616
13617 return true;
13618 }
13619
13620 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13621 Callback's type should be:
13622 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13623 Return 1 if the attributes are present and valid, otherwise, return 0. */
13624
13625 template <typename Callback>
13626 static int
13627 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13628 Callback &&callback)
13629 {
13630 struct dwarf2_per_objfile *dwarf2_per_objfile
13631 = cu->per_cu->dwarf2_per_objfile;
13632 struct objfile *objfile = dwarf2_per_objfile->objfile;
13633 struct comp_unit_head *cu_header = &cu->header;
13634 bfd *obfd = objfile->obfd;
13635 unsigned int addr_size = cu_header->addr_size;
13636 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13637 /* Base address selection entry. */
13638 CORE_ADDR base;
13639 int found_base;
13640 unsigned int dummy;
13641 const gdb_byte *buffer;
13642 CORE_ADDR baseaddr;
13643
13644 if (cu_header->version >= 5)
13645 return dwarf2_rnglists_process (offset, cu, callback);
13646
13647 found_base = cu->base_known;
13648 base = cu->base_address;
13649
13650 dwarf2_per_objfile->ranges.read (objfile);
13651 if (offset >= dwarf2_per_objfile->ranges.size)
13652 {
13653 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13654 offset);
13655 return 0;
13656 }
13657 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13658
13659 baseaddr = objfile->text_section_offset ();
13660
13661 while (1)
13662 {
13663 CORE_ADDR range_beginning, range_end;
13664
13665 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13666 buffer += addr_size;
13667 range_end = cu->header.read_address (obfd, buffer, &dummy);
13668 buffer += addr_size;
13669 offset += 2 * addr_size;
13670
13671 /* An end of list marker is a pair of zero addresses. */
13672 if (range_beginning == 0 && range_end == 0)
13673 /* Found the end of list entry. */
13674 break;
13675
13676 /* Each base address selection entry is a pair of 2 values.
13677 The first is the largest possible address, the second is
13678 the base address. Check for a base address here. */
13679 if ((range_beginning & mask) == mask)
13680 {
13681 /* If we found the largest possible address, then we already
13682 have the base address in range_end. */
13683 base = range_end;
13684 found_base = 1;
13685 continue;
13686 }
13687
13688 if (!found_base)
13689 {
13690 /* We have no valid base address for the ranges
13691 data. */
13692 complaint (_("Invalid .debug_ranges data (no base address)"));
13693 return 0;
13694 }
13695
13696 if (range_beginning > range_end)
13697 {
13698 /* Inverted range entries are invalid. */
13699 complaint (_("Invalid .debug_ranges data (inverted range)"));
13700 return 0;
13701 }
13702
13703 /* Empty range entries have no effect. */
13704 if (range_beginning == range_end)
13705 continue;
13706
13707 range_beginning += base;
13708 range_end += base;
13709
13710 /* A not-uncommon case of bad debug info.
13711 Don't pollute the addrmap with bad data. */
13712 if (range_beginning + baseaddr == 0
13713 && !dwarf2_per_objfile->has_section_at_zero)
13714 {
13715 complaint (_(".debug_ranges entry has start address of zero"
13716 " [in module %s]"), objfile_name (objfile));
13717 continue;
13718 }
13719
13720 callback (range_beginning, range_end);
13721 }
13722
13723 return 1;
13724 }
13725
13726 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13727 Return 1 if the attributes are present and valid, otherwise, return 0.
13728 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13729
13730 static int
13731 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13732 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13733 dwarf2_psymtab *ranges_pst)
13734 {
13735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13737 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13738 int low_set = 0;
13739 CORE_ADDR low = 0;
13740 CORE_ADDR high = 0;
13741 int retval;
13742
13743 retval = dwarf2_ranges_process (offset, cu,
13744 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13745 {
13746 if (ranges_pst != NULL)
13747 {
13748 CORE_ADDR lowpc;
13749 CORE_ADDR highpc;
13750
13751 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13752 range_beginning + baseaddr)
13753 - baseaddr);
13754 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13755 range_end + baseaddr)
13756 - baseaddr);
13757 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13758 lowpc, highpc - 1, ranges_pst);
13759 }
13760
13761 /* FIXME: This is recording everything as a low-high
13762 segment of consecutive addresses. We should have a
13763 data structure for discontiguous block ranges
13764 instead. */
13765 if (! low_set)
13766 {
13767 low = range_beginning;
13768 high = range_end;
13769 low_set = 1;
13770 }
13771 else
13772 {
13773 if (range_beginning < low)
13774 low = range_beginning;
13775 if (range_end > high)
13776 high = range_end;
13777 }
13778 });
13779 if (!retval)
13780 return 0;
13781
13782 if (! low_set)
13783 /* If the first entry is an end-of-list marker, the range
13784 describes an empty scope, i.e. no instructions. */
13785 return 0;
13786
13787 if (low_return)
13788 *low_return = low;
13789 if (high_return)
13790 *high_return = high;
13791 return 1;
13792 }
13793
13794 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13795 definition for the return value. *LOWPC and *HIGHPC are set iff
13796 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13797
13798 static enum pc_bounds_kind
13799 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13800 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13801 dwarf2_psymtab *pst)
13802 {
13803 struct dwarf2_per_objfile *dwarf2_per_objfile
13804 = cu->per_cu->dwarf2_per_objfile;
13805 struct attribute *attr;
13806 struct attribute *attr_high;
13807 CORE_ADDR low = 0;
13808 CORE_ADDR high = 0;
13809 enum pc_bounds_kind ret;
13810
13811 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13812 if (attr_high)
13813 {
13814 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13815 if (attr != nullptr)
13816 {
13817 low = attr->value_as_address ();
13818 high = attr_high->value_as_address ();
13819 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13820 high += low;
13821 }
13822 else
13823 /* Found high w/o low attribute. */
13824 return PC_BOUNDS_INVALID;
13825
13826 /* Found consecutive range of addresses. */
13827 ret = PC_BOUNDS_HIGH_LOW;
13828 }
13829 else
13830 {
13831 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13832 if (attr != NULL)
13833 {
13834 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13835 We take advantage of the fact that DW_AT_ranges does not appear
13836 in DW_TAG_compile_unit of DWO files. */
13837 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13838 unsigned int ranges_offset = (DW_UNSND (attr)
13839 + (need_ranges_base
13840 ? cu->ranges_base
13841 : 0));
13842
13843 /* Value of the DW_AT_ranges attribute is the offset in the
13844 .debug_ranges section. */
13845 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13846 return PC_BOUNDS_INVALID;
13847 /* Found discontinuous range of addresses. */
13848 ret = PC_BOUNDS_RANGES;
13849 }
13850 else
13851 return PC_BOUNDS_NOT_PRESENT;
13852 }
13853
13854 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13855 if (high <= low)
13856 return PC_BOUNDS_INVALID;
13857
13858 /* When using the GNU linker, .gnu.linkonce. sections are used to
13859 eliminate duplicate copies of functions and vtables and such.
13860 The linker will arbitrarily choose one and discard the others.
13861 The AT_*_pc values for such functions refer to local labels in
13862 these sections. If the section from that file was discarded, the
13863 labels are not in the output, so the relocs get a value of 0.
13864 If this is a discarded function, mark the pc bounds as invalid,
13865 so that GDB will ignore it. */
13866 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13867 return PC_BOUNDS_INVALID;
13868
13869 *lowpc = low;
13870 if (highpc)
13871 *highpc = high;
13872 return ret;
13873 }
13874
13875 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13876 its low and high PC addresses. Do nothing if these addresses could not
13877 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13878 and HIGHPC to the high address if greater than HIGHPC. */
13879
13880 static void
13881 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13882 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13883 struct dwarf2_cu *cu)
13884 {
13885 CORE_ADDR low, high;
13886 struct die_info *child = die->child;
13887
13888 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13889 {
13890 *lowpc = std::min (*lowpc, low);
13891 *highpc = std::max (*highpc, high);
13892 }
13893
13894 /* If the language does not allow nested subprograms (either inside
13895 subprograms or lexical blocks), we're done. */
13896 if (cu->language != language_ada)
13897 return;
13898
13899 /* Check all the children of the given DIE. If it contains nested
13900 subprograms, then check their pc bounds. Likewise, we need to
13901 check lexical blocks as well, as they may also contain subprogram
13902 definitions. */
13903 while (child && child->tag)
13904 {
13905 if (child->tag == DW_TAG_subprogram
13906 || child->tag == DW_TAG_lexical_block)
13907 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13908 child = sibling_die (child);
13909 }
13910 }
13911
13912 /* Get the low and high pc's represented by the scope DIE, and store
13913 them in *LOWPC and *HIGHPC. If the correct values can't be
13914 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13915
13916 static void
13917 get_scope_pc_bounds (struct die_info *die,
13918 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13919 struct dwarf2_cu *cu)
13920 {
13921 CORE_ADDR best_low = (CORE_ADDR) -1;
13922 CORE_ADDR best_high = (CORE_ADDR) 0;
13923 CORE_ADDR current_low, current_high;
13924
13925 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13926 >= PC_BOUNDS_RANGES)
13927 {
13928 best_low = current_low;
13929 best_high = current_high;
13930 }
13931 else
13932 {
13933 struct die_info *child = die->child;
13934
13935 while (child && child->tag)
13936 {
13937 switch (child->tag) {
13938 case DW_TAG_subprogram:
13939 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13940 break;
13941 case DW_TAG_namespace:
13942 case DW_TAG_module:
13943 /* FIXME: carlton/2004-01-16: Should we do this for
13944 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13945 that current GCC's always emit the DIEs corresponding
13946 to definitions of methods of classes as children of a
13947 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13948 the DIEs giving the declarations, which could be
13949 anywhere). But I don't see any reason why the
13950 standards says that they have to be there. */
13951 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13952
13953 if (current_low != ((CORE_ADDR) -1))
13954 {
13955 best_low = std::min (best_low, current_low);
13956 best_high = std::max (best_high, current_high);
13957 }
13958 break;
13959 default:
13960 /* Ignore. */
13961 break;
13962 }
13963
13964 child = sibling_die (child);
13965 }
13966 }
13967
13968 *lowpc = best_low;
13969 *highpc = best_high;
13970 }
13971
13972 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13973 in DIE. */
13974
13975 static void
13976 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13977 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13978 {
13979 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13980 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13981 struct attribute *attr;
13982 struct attribute *attr_high;
13983
13984 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13985 if (attr_high)
13986 {
13987 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13988 if (attr != nullptr)
13989 {
13990 CORE_ADDR low = attr->value_as_address ();
13991 CORE_ADDR high = attr_high->value_as_address ();
13992
13993 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13994 high += low;
13995
13996 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13997 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13998 cu->get_builder ()->record_block_range (block, low, high - 1);
13999 }
14000 }
14001
14002 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14003 if (attr != nullptr)
14004 {
14005 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14006 We take advantage of the fact that DW_AT_ranges does not appear
14007 in DW_TAG_compile_unit of DWO files. */
14008 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14009
14010 /* The value of the DW_AT_ranges attribute is the offset of the
14011 address range list in the .debug_ranges section. */
14012 unsigned long offset = (DW_UNSND (attr)
14013 + (need_ranges_base ? cu->ranges_base : 0));
14014
14015 std::vector<blockrange> blockvec;
14016 dwarf2_ranges_process (offset, cu,
14017 [&] (CORE_ADDR start, CORE_ADDR end)
14018 {
14019 start += baseaddr;
14020 end += baseaddr;
14021 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14022 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14023 cu->get_builder ()->record_block_range (block, start, end - 1);
14024 blockvec.emplace_back (start, end);
14025 });
14026
14027 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14028 }
14029 }
14030
14031 /* Check whether the producer field indicates either of GCC < 4.6, or the
14032 Intel C/C++ compiler, and cache the result in CU. */
14033
14034 static void
14035 check_producer (struct dwarf2_cu *cu)
14036 {
14037 int major, minor;
14038
14039 if (cu->producer == NULL)
14040 {
14041 /* For unknown compilers expect their behavior is DWARF version
14042 compliant.
14043
14044 GCC started to support .debug_types sections by -gdwarf-4 since
14045 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14046 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14047 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14048 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14049 }
14050 else if (producer_is_gcc (cu->producer, &major, &minor))
14051 {
14052 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14053 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14054 }
14055 else if (producer_is_icc (cu->producer, &major, &minor))
14056 {
14057 cu->producer_is_icc = true;
14058 cu->producer_is_icc_lt_14 = major < 14;
14059 }
14060 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14061 cu->producer_is_codewarrior = true;
14062 else
14063 {
14064 /* For other non-GCC compilers, expect their behavior is DWARF version
14065 compliant. */
14066 }
14067
14068 cu->checked_producer = true;
14069 }
14070
14071 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14072 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14073 during 4.6.0 experimental. */
14074
14075 static bool
14076 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14077 {
14078 if (!cu->checked_producer)
14079 check_producer (cu);
14080
14081 return cu->producer_is_gxx_lt_4_6;
14082 }
14083
14084
14085 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14086 with incorrect is_stmt attributes. */
14087
14088 static bool
14089 producer_is_codewarrior (struct dwarf2_cu *cu)
14090 {
14091 if (!cu->checked_producer)
14092 check_producer (cu);
14093
14094 return cu->producer_is_codewarrior;
14095 }
14096
14097 /* Return the default accessibility type if it is not overridden by
14098 DW_AT_accessibility. */
14099
14100 static enum dwarf_access_attribute
14101 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14102 {
14103 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14104 {
14105 /* The default DWARF 2 accessibility for members is public, the default
14106 accessibility for inheritance is private. */
14107
14108 if (die->tag != DW_TAG_inheritance)
14109 return DW_ACCESS_public;
14110 else
14111 return DW_ACCESS_private;
14112 }
14113 else
14114 {
14115 /* DWARF 3+ defines the default accessibility a different way. The same
14116 rules apply now for DW_TAG_inheritance as for the members and it only
14117 depends on the container kind. */
14118
14119 if (die->parent->tag == DW_TAG_class_type)
14120 return DW_ACCESS_private;
14121 else
14122 return DW_ACCESS_public;
14123 }
14124 }
14125
14126 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14127 offset. If the attribute was not found return 0, otherwise return
14128 1. If it was found but could not properly be handled, set *OFFSET
14129 to 0. */
14130
14131 static int
14132 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14133 LONGEST *offset)
14134 {
14135 struct attribute *attr;
14136
14137 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14138 if (attr != NULL)
14139 {
14140 *offset = 0;
14141
14142 /* Note that we do not check for a section offset first here.
14143 This is because DW_AT_data_member_location is new in DWARF 4,
14144 so if we see it, we can assume that a constant form is really
14145 a constant and not a section offset. */
14146 if (attr->form_is_constant ())
14147 *offset = dwarf2_get_attr_constant_value (attr, 0);
14148 else if (attr->form_is_section_offset ())
14149 dwarf2_complex_location_expr_complaint ();
14150 else if (attr->form_is_block ())
14151 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14152 else
14153 dwarf2_complex_location_expr_complaint ();
14154
14155 return 1;
14156 }
14157
14158 return 0;
14159 }
14160
14161 /* Add an aggregate field to the field list. */
14162
14163 static void
14164 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14165 struct dwarf2_cu *cu)
14166 {
14167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14168 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14169 struct nextfield *new_field;
14170 struct attribute *attr;
14171 struct field *fp;
14172 const char *fieldname = "";
14173
14174 if (die->tag == DW_TAG_inheritance)
14175 {
14176 fip->baseclasses.emplace_back ();
14177 new_field = &fip->baseclasses.back ();
14178 }
14179 else
14180 {
14181 fip->fields.emplace_back ();
14182 new_field = &fip->fields.back ();
14183 }
14184
14185 fip->nfields++;
14186
14187 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14188 if (attr != nullptr)
14189 new_field->accessibility = DW_UNSND (attr);
14190 else
14191 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14192 if (new_field->accessibility != DW_ACCESS_public)
14193 fip->non_public_fields = 1;
14194
14195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14196 if (attr != nullptr)
14197 new_field->virtuality = DW_UNSND (attr);
14198 else
14199 new_field->virtuality = DW_VIRTUALITY_none;
14200
14201 fp = &new_field->field;
14202
14203 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14204 {
14205 LONGEST offset;
14206
14207 /* Data member other than a C++ static data member. */
14208
14209 /* Get type of field. */
14210 fp->type = die_type (die, cu);
14211
14212 SET_FIELD_BITPOS (*fp, 0);
14213
14214 /* Get bit size of field (zero if none). */
14215 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14216 if (attr != nullptr)
14217 {
14218 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14219 }
14220 else
14221 {
14222 FIELD_BITSIZE (*fp) = 0;
14223 }
14224
14225 /* Get bit offset of field. */
14226 if (handle_data_member_location (die, cu, &offset))
14227 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14228 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14229 if (attr != nullptr)
14230 {
14231 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14232 {
14233 /* For big endian bits, the DW_AT_bit_offset gives the
14234 additional bit offset from the MSB of the containing
14235 anonymous object to the MSB of the field. We don't
14236 have to do anything special since we don't need to
14237 know the size of the anonymous object. */
14238 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14239 }
14240 else
14241 {
14242 /* For little endian bits, compute the bit offset to the
14243 MSB of the anonymous object, subtract off the number of
14244 bits from the MSB of the field to the MSB of the
14245 object, and then subtract off the number of bits of
14246 the field itself. The result is the bit offset of
14247 the LSB of the field. */
14248 int anonymous_size;
14249 int bit_offset = DW_UNSND (attr);
14250
14251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14252 if (attr != nullptr)
14253 {
14254 /* The size of the anonymous object containing
14255 the bit field is explicit, so use the
14256 indicated size (in bytes). */
14257 anonymous_size = DW_UNSND (attr);
14258 }
14259 else
14260 {
14261 /* The size of the anonymous object containing
14262 the bit field must be inferred from the type
14263 attribute of the data member containing the
14264 bit field. */
14265 anonymous_size = TYPE_LENGTH (fp->type);
14266 }
14267 SET_FIELD_BITPOS (*fp,
14268 (FIELD_BITPOS (*fp)
14269 + anonymous_size * bits_per_byte
14270 - bit_offset - FIELD_BITSIZE (*fp)));
14271 }
14272 }
14273 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14274 if (attr != NULL)
14275 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14276 + dwarf2_get_attr_constant_value (attr, 0)));
14277
14278 /* Get name of field. */
14279 fieldname = dwarf2_name (die, cu);
14280 if (fieldname == NULL)
14281 fieldname = "";
14282
14283 /* The name is already allocated along with this objfile, so we don't
14284 need to duplicate it for the type. */
14285 fp->name = fieldname;
14286
14287 /* Change accessibility for artificial fields (e.g. virtual table
14288 pointer or virtual base class pointer) to private. */
14289 if (dwarf2_attr (die, DW_AT_artificial, cu))
14290 {
14291 FIELD_ARTIFICIAL (*fp) = 1;
14292 new_field->accessibility = DW_ACCESS_private;
14293 fip->non_public_fields = 1;
14294 }
14295 }
14296 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14297 {
14298 /* C++ static member. */
14299
14300 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14301 is a declaration, but all versions of G++ as of this writing
14302 (so through at least 3.2.1) incorrectly generate
14303 DW_TAG_variable tags. */
14304
14305 const char *physname;
14306
14307 /* Get name of field. */
14308 fieldname = dwarf2_name (die, cu);
14309 if (fieldname == NULL)
14310 return;
14311
14312 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14313 if (attr
14314 /* Only create a symbol if this is an external value.
14315 new_symbol checks this and puts the value in the global symbol
14316 table, which we want. If it is not external, new_symbol
14317 will try to put the value in cu->list_in_scope which is wrong. */
14318 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14319 {
14320 /* A static const member, not much different than an enum as far as
14321 we're concerned, except that we can support more types. */
14322 new_symbol (die, NULL, cu);
14323 }
14324
14325 /* Get physical name. */
14326 physname = dwarf2_physname (fieldname, die, cu);
14327
14328 /* The name is already allocated along with this objfile, so we don't
14329 need to duplicate it for the type. */
14330 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14331 FIELD_TYPE (*fp) = die_type (die, cu);
14332 FIELD_NAME (*fp) = fieldname;
14333 }
14334 else if (die->tag == DW_TAG_inheritance)
14335 {
14336 LONGEST offset;
14337
14338 /* C++ base class field. */
14339 if (handle_data_member_location (die, cu, &offset))
14340 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14341 FIELD_BITSIZE (*fp) = 0;
14342 FIELD_TYPE (*fp) = die_type (die, cu);
14343 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14344 }
14345 else if (die->tag == DW_TAG_variant_part)
14346 {
14347 /* process_structure_scope will treat this DIE as a union. */
14348 process_structure_scope (die, cu);
14349
14350 /* The variant part is relative to the start of the enclosing
14351 structure. */
14352 SET_FIELD_BITPOS (*fp, 0);
14353 fp->type = get_die_type (die, cu);
14354 fp->artificial = 1;
14355 fp->name = "<<variant>>";
14356
14357 /* Normally a DW_TAG_variant_part won't have a size, but our
14358 representation requires one, so set it to the maximum of the
14359 child sizes, being sure to account for the offset at which
14360 each child is seen. */
14361 if (TYPE_LENGTH (fp->type) == 0)
14362 {
14363 unsigned max = 0;
14364 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14365 {
14366 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14367 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14368 if (len > max)
14369 max = len;
14370 }
14371 TYPE_LENGTH (fp->type) = max;
14372 }
14373 }
14374 else
14375 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14376 }
14377
14378 /* Can the type given by DIE define another type? */
14379
14380 static bool
14381 type_can_define_types (const struct die_info *die)
14382 {
14383 switch (die->tag)
14384 {
14385 case DW_TAG_typedef:
14386 case DW_TAG_class_type:
14387 case DW_TAG_structure_type:
14388 case DW_TAG_union_type:
14389 case DW_TAG_enumeration_type:
14390 return true;
14391
14392 default:
14393 return false;
14394 }
14395 }
14396
14397 /* Add a type definition defined in the scope of the FIP's class. */
14398
14399 static void
14400 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14401 struct dwarf2_cu *cu)
14402 {
14403 struct decl_field fp;
14404 memset (&fp, 0, sizeof (fp));
14405
14406 gdb_assert (type_can_define_types (die));
14407
14408 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14409 fp.name = dwarf2_name (die, cu);
14410 fp.type = read_type_die (die, cu);
14411
14412 /* Save accessibility. */
14413 enum dwarf_access_attribute accessibility;
14414 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14415 if (attr != NULL)
14416 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14417 else
14418 accessibility = dwarf2_default_access_attribute (die, cu);
14419 switch (accessibility)
14420 {
14421 case DW_ACCESS_public:
14422 /* The assumed value if neither private nor protected. */
14423 break;
14424 case DW_ACCESS_private:
14425 fp.is_private = 1;
14426 break;
14427 case DW_ACCESS_protected:
14428 fp.is_protected = 1;
14429 break;
14430 default:
14431 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14432 }
14433
14434 if (die->tag == DW_TAG_typedef)
14435 fip->typedef_field_list.push_back (fp);
14436 else
14437 fip->nested_types_list.push_back (fp);
14438 }
14439
14440 /* Create the vector of fields, and attach it to the type. */
14441
14442 static void
14443 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14444 struct dwarf2_cu *cu)
14445 {
14446 int nfields = fip->nfields;
14447
14448 /* Record the field count, allocate space for the array of fields,
14449 and create blank accessibility bitfields if necessary. */
14450 TYPE_NFIELDS (type) = nfields;
14451 TYPE_FIELDS (type) = (struct field *)
14452 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14453
14454 if (fip->non_public_fields && cu->language != language_ada)
14455 {
14456 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14457
14458 TYPE_FIELD_PRIVATE_BITS (type) =
14459 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14460 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14461
14462 TYPE_FIELD_PROTECTED_BITS (type) =
14463 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14464 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14465
14466 TYPE_FIELD_IGNORE_BITS (type) =
14467 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14468 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14469 }
14470
14471 /* If the type has baseclasses, allocate and clear a bit vector for
14472 TYPE_FIELD_VIRTUAL_BITS. */
14473 if (!fip->baseclasses.empty () && cu->language != language_ada)
14474 {
14475 int num_bytes = B_BYTES (fip->baseclasses.size ());
14476 unsigned char *pointer;
14477
14478 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14479 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14480 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14481 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14482 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14483 }
14484
14485 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14486 {
14487 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14488
14489 for (int index = 0; index < nfields; ++index)
14490 {
14491 struct nextfield &field = fip->fields[index];
14492
14493 if (field.variant.is_discriminant)
14494 di->discriminant_index = index;
14495 else if (field.variant.default_branch)
14496 di->default_index = index;
14497 else
14498 di->discriminants[index] = field.variant.discriminant_value;
14499 }
14500 }
14501
14502 /* Copy the saved-up fields into the field vector. */
14503 for (int i = 0; i < nfields; ++i)
14504 {
14505 struct nextfield &field
14506 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14507 : fip->fields[i - fip->baseclasses.size ()]);
14508
14509 TYPE_FIELD (type, i) = field.field;
14510 switch (field.accessibility)
14511 {
14512 case DW_ACCESS_private:
14513 if (cu->language != language_ada)
14514 SET_TYPE_FIELD_PRIVATE (type, i);
14515 break;
14516
14517 case DW_ACCESS_protected:
14518 if (cu->language != language_ada)
14519 SET_TYPE_FIELD_PROTECTED (type, i);
14520 break;
14521
14522 case DW_ACCESS_public:
14523 break;
14524
14525 default:
14526 /* Unknown accessibility. Complain and treat it as public. */
14527 {
14528 complaint (_("unsupported accessibility %d"),
14529 field.accessibility);
14530 }
14531 break;
14532 }
14533 if (i < fip->baseclasses.size ())
14534 {
14535 switch (field.virtuality)
14536 {
14537 case DW_VIRTUALITY_virtual:
14538 case DW_VIRTUALITY_pure_virtual:
14539 if (cu->language == language_ada)
14540 error (_("unexpected virtuality in component of Ada type"));
14541 SET_TYPE_FIELD_VIRTUAL (type, i);
14542 break;
14543 }
14544 }
14545 }
14546 }
14547
14548 /* Return true if this member function is a constructor, false
14549 otherwise. */
14550
14551 static int
14552 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14553 {
14554 const char *fieldname;
14555 const char *type_name;
14556 int len;
14557
14558 if (die->parent == NULL)
14559 return 0;
14560
14561 if (die->parent->tag != DW_TAG_structure_type
14562 && die->parent->tag != DW_TAG_union_type
14563 && die->parent->tag != DW_TAG_class_type)
14564 return 0;
14565
14566 fieldname = dwarf2_name (die, cu);
14567 type_name = dwarf2_name (die->parent, cu);
14568 if (fieldname == NULL || type_name == NULL)
14569 return 0;
14570
14571 len = strlen (fieldname);
14572 return (strncmp (fieldname, type_name, len) == 0
14573 && (type_name[len] == '\0' || type_name[len] == '<'));
14574 }
14575
14576 /* Check if the given VALUE is a recognized enum
14577 dwarf_defaulted_attribute constant according to DWARF5 spec,
14578 Table 7.24. */
14579
14580 static bool
14581 is_valid_DW_AT_defaulted (ULONGEST value)
14582 {
14583 switch (value)
14584 {
14585 case DW_DEFAULTED_no:
14586 case DW_DEFAULTED_in_class:
14587 case DW_DEFAULTED_out_of_class:
14588 return true;
14589 }
14590
14591 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14592 return false;
14593 }
14594
14595 /* Add a member function to the proper fieldlist. */
14596
14597 static void
14598 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14599 struct type *type, struct dwarf2_cu *cu)
14600 {
14601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14602 struct attribute *attr;
14603 int i;
14604 struct fnfieldlist *flp = nullptr;
14605 struct fn_field *fnp;
14606 const char *fieldname;
14607 struct type *this_type;
14608 enum dwarf_access_attribute accessibility;
14609
14610 if (cu->language == language_ada)
14611 error (_("unexpected member function in Ada type"));
14612
14613 /* Get name of member function. */
14614 fieldname = dwarf2_name (die, cu);
14615 if (fieldname == NULL)
14616 return;
14617
14618 /* Look up member function name in fieldlist. */
14619 for (i = 0; i < fip->fnfieldlists.size (); i++)
14620 {
14621 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14622 {
14623 flp = &fip->fnfieldlists[i];
14624 break;
14625 }
14626 }
14627
14628 /* Create a new fnfieldlist if necessary. */
14629 if (flp == nullptr)
14630 {
14631 fip->fnfieldlists.emplace_back ();
14632 flp = &fip->fnfieldlists.back ();
14633 flp->name = fieldname;
14634 i = fip->fnfieldlists.size () - 1;
14635 }
14636
14637 /* Create a new member function field and add it to the vector of
14638 fnfieldlists. */
14639 flp->fnfields.emplace_back ();
14640 fnp = &flp->fnfields.back ();
14641
14642 /* Delay processing of the physname until later. */
14643 if (cu->language == language_cplus)
14644 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14645 die, cu);
14646 else
14647 {
14648 const char *physname = dwarf2_physname (fieldname, die, cu);
14649 fnp->physname = physname ? physname : "";
14650 }
14651
14652 fnp->type = alloc_type (objfile);
14653 this_type = read_type_die (die, cu);
14654 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14655 {
14656 int nparams = TYPE_NFIELDS (this_type);
14657
14658 /* TYPE is the domain of this method, and THIS_TYPE is the type
14659 of the method itself (TYPE_CODE_METHOD). */
14660 smash_to_method_type (fnp->type, type,
14661 TYPE_TARGET_TYPE (this_type),
14662 TYPE_FIELDS (this_type),
14663 TYPE_NFIELDS (this_type),
14664 TYPE_VARARGS (this_type));
14665
14666 /* Handle static member functions.
14667 Dwarf2 has no clean way to discern C++ static and non-static
14668 member functions. G++ helps GDB by marking the first
14669 parameter for non-static member functions (which is the this
14670 pointer) as artificial. We obtain this information from
14671 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14672 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14673 fnp->voffset = VOFFSET_STATIC;
14674 }
14675 else
14676 complaint (_("member function type missing for '%s'"),
14677 dwarf2_full_name (fieldname, die, cu));
14678
14679 /* Get fcontext from DW_AT_containing_type if present. */
14680 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14681 fnp->fcontext = die_containing_type (die, cu);
14682
14683 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14684 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14685
14686 /* Get accessibility. */
14687 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14688 if (attr != nullptr)
14689 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14690 else
14691 accessibility = dwarf2_default_access_attribute (die, cu);
14692 switch (accessibility)
14693 {
14694 case DW_ACCESS_private:
14695 fnp->is_private = 1;
14696 break;
14697 case DW_ACCESS_protected:
14698 fnp->is_protected = 1;
14699 break;
14700 }
14701
14702 /* Check for artificial methods. */
14703 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14704 if (attr && DW_UNSND (attr) != 0)
14705 fnp->is_artificial = 1;
14706
14707 /* Check for defaulted methods. */
14708 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14709 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14710 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14711
14712 /* Check for deleted methods. */
14713 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14714 if (attr != nullptr && DW_UNSND (attr) != 0)
14715 fnp->is_deleted = 1;
14716
14717 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14718
14719 /* Get index in virtual function table if it is a virtual member
14720 function. For older versions of GCC, this is an offset in the
14721 appropriate virtual table, as specified by DW_AT_containing_type.
14722 For everyone else, it is an expression to be evaluated relative
14723 to the object address. */
14724
14725 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14726 if (attr != nullptr)
14727 {
14728 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14729 {
14730 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14731 {
14732 /* Old-style GCC. */
14733 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14734 }
14735 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14736 || (DW_BLOCK (attr)->size > 1
14737 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14738 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14739 {
14740 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14741 if ((fnp->voffset % cu->header.addr_size) != 0)
14742 dwarf2_complex_location_expr_complaint ();
14743 else
14744 fnp->voffset /= cu->header.addr_size;
14745 fnp->voffset += 2;
14746 }
14747 else
14748 dwarf2_complex_location_expr_complaint ();
14749
14750 if (!fnp->fcontext)
14751 {
14752 /* If there is no `this' field and no DW_AT_containing_type,
14753 we cannot actually find a base class context for the
14754 vtable! */
14755 if (TYPE_NFIELDS (this_type) == 0
14756 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14757 {
14758 complaint (_("cannot determine context for virtual member "
14759 "function \"%s\" (offset %s)"),
14760 fieldname, sect_offset_str (die->sect_off));
14761 }
14762 else
14763 {
14764 fnp->fcontext
14765 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14766 }
14767 }
14768 }
14769 else if (attr->form_is_section_offset ())
14770 {
14771 dwarf2_complex_location_expr_complaint ();
14772 }
14773 else
14774 {
14775 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14776 fieldname);
14777 }
14778 }
14779 else
14780 {
14781 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14782 if (attr && DW_UNSND (attr))
14783 {
14784 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14785 complaint (_("Member function \"%s\" (offset %s) is virtual "
14786 "but the vtable offset is not specified"),
14787 fieldname, sect_offset_str (die->sect_off));
14788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14789 TYPE_CPLUS_DYNAMIC (type) = 1;
14790 }
14791 }
14792 }
14793
14794 /* Create the vector of member function fields, and attach it to the type. */
14795
14796 static void
14797 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14798 struct dwarf2_cu *cu)
14799 {
14800 if (cu->language == language_ada)
14801 error (_("unexpected member functions in Ada type"));
14802
14803 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14804 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14805 TYPE_ALLOC (type,
14806 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14807
14808 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14809 {
14810 struct fnfieldlist &nf = fip->fnfieldlists[i];
14811 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14812
14813 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14814 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14815 fn_flp->fn_fields = (struct fn_field *)
14816 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14817
14818 for (int k = 0; k < nf.fnfields.size (); ++k)
14819 fn_flp->fn_fields[k] = nf.fnfields[k];
14820 }
14821
14822 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14823 }
14824
14825 /* Returns non-zero if NAME is the name of a vtable member in CU's
14826 language, zero otherwise. */
14827 static int
14828 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14829 {
14830 static const char vptr[] = "_vptr";
14831
14832 /* Look for the C++ form of the vtable. */
14833 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14834 return 1;
14835
14836 return 0;
14837 }
14838
14839 /* GCC outputs unnamed structures that are really pointers to member
14840 functions, with the ABI-specified layout. If TYPE describes
14841 such a structure, smash it into a member function type.
14842
14843 GCC shouldn't do this; it should just output pointer to member DIEs.
14844 This is GCC PR debug/28767. */
14845
14846 static void
14847 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14848 {
14849 struct type *pfn_type, *self_type, *new_type;
14850
14851 /* Check for a structure with no name and two children. */
14852 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14853 return;
14854
14855 /* Check for __pfn and __delta members. */
14856 if (TYPE_FIELD_NAME (type, 0) == NULL
14857 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14858 || TYPE_FIELD_NAME (type, 1) == NULL
14859 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14860 return;
14861
14862 /* Find the type of the method. */
14863 pfn_type = TYPE_FIELD_TYPE (type, 0);
14864 if (pfn_type == NULL
14865 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14866 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14867 return;
14868
14869 /* Look for the "this" argument. */
14870 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14871 if (TYPE_NFIELDS (pfn_type) == 0
14872 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14873 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14874 return;
14875
14876 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14877 new_type = alloc_type (objfile);
14878 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14879 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14880 TYPE_VARARGS (pfn_type));
14881 smash_to_methodptr_type (type, new_type);
14882 }
14883
14884 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14885 appropriate error checking and issuing complaints if there is a
14886 problem. */
14887
14888 static ULONGEST
14889 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14890 {
14891 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14892
14893 if (attr == nullptr)
14894 return 0;
14895
14896 if (!attr->form_is_constant ())
14897 {
14898 complaint (_("DW_AT_alignment must have constant form"
14899 " - DIE at %s [in module %s]"),
14900 sect_offset_str (die->sect_off),
14901 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14902 return 0;
14903 }
14904
14905 ULONGEST align;
14906 if (attr->form == DW_FORM_sdata)
14907 {
14908 LONGEST val = DW_SND (attr);
14909 if (val < 0)
14910 {
14911 complaint (_("DW_AT_alignment value must not be negative"
14912 " - DIE at %s [in module %s]"),
14913 sect_offset_str (die->sect_off),
14914 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14915 return 0;
14916 }
14917 align = val;
14918 }
14919 else
14920 align = DW_UNSND (attr);
14921
14922 if (align == 0)
14923 {
14924 complaint (_("DW_AT_alignment value must not be zero"
14925 " - DIE at %s [in module %s]"),
14926 sect_offset_str (die->sect_off),
14927 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14928 return 0;
14929 }
14930 if ((align & (align - 1)) != 0)
14931 {
14932 complaint (_("DW_AT_alignment value must be a power of 2"
14933 " - DIE at %s [in module %s]"),
14934 sect_offset_str (die->sect_off),
14935 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14936 return 0;
14937 }
14938
14939 return align;
14940 }
14941
14942 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14943 the alignment for TYPE. */
14944
14945 static void
14946 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14947 struct type *type)
14948 {
14949 if (!set_type_align (type, get_alignment (cu, die)))
14950 complaint (_("DW_AT_alignment value too large"
14951 " - DIE at %s [in module %s]"),
14952 sect_offset_str (die->sect_off),
14953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14954 }
14955
14956 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14957 constant for a type, according to DWARF5 spec, Table 5.5. */
14958
14959 static bool
14960 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14961 {
14962 switch (value)
14963 {
14964 case DW_CC_normal:
14965 case DW_CC_pass_by_reference:
14966 case DW_CC_pass_by_value:
14967 return true;
14968
14969 default:
14970 complaint (_("unrecognized DW_AT_calling_convention value "
14971 "(%s) for a type"), pulongest (value));
14972 return false;
14973 }
14974 }
14975
14976 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14977 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14978 also according to GNU-specific values (see include/dwarf2.h). */
14979
14980 static bool
14981 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14982 {
14983 switch (value)
14984 {
14985 case DW_CC_normal:
14986 case DW_CC_program:
14987 case DW_CC_nocall:
14988 return true;
14989
14990 case DW_CC_GNU_renesas_sh:
14991 case DW_CC_GNU_borland_fastcall_i386:
14992 case DW_CC_GDB_IBM_OpenCL:
14993 return true;
14994
14995 default:
14996 complaint (_("unrecognized DW_AT_calling_convention value "
14997 "(%s) for a subroutine"), pulongest (value));
14998 return false;
14999 }
15000 }
15001
15002 /* Called when we find the DIE that starts a structure or union scope
15003 (definition) to create a type for the structure or union. Fill in
15004 the type's name and general properties; the members will not be
15005 processed until process_structure_scope. A symbol table entry for
15006 the type will also not be done until process_structure_scope (assuming
15007 the type has a name).
15008
15009 NOTE: we need to call these functions regardless of whether or not the
15010 DIE has a DW_AT_name attribute, since it might be an anonymous
15011 structure or union. This gets the type entered into our set of
15012 user defined types. */
15013
15014 static struct type *
15015 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15016 {
15017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15018 struct type *type;
15019 struct attribute *attr;
15020 const char *name;
15021
15022 /* If the definition of this type lives in .debug_types, read that type.
15023 Don't follow DW_AT_specification though, that will take us back up
15024 the chain and we want to go down. */
15025 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15026 if (attr != nullptr)
15027 {
15028 type = get_DW_AT_signature_type (die, attr, cu);
15029
15030 /* The type's CU may not be the same as CU.
15031 Ensure TYPE is recorded with CU in die_type_hash. */
15032 return set_die_type (die, type, cu);
15033 }
15034
15035 type = alloc_type (objfile);
15036 INIT_CPLUS_SPECIFIC (type);
15037
15038 name = dwarf2_name (die, cu);
15039 if (name != NULL)
15040 {
15041 if (cu->language == language_cplus
15042 || cu->language == language_d
15043 || cu->language == language_rust)
15044 {
15045 const char *full_name = dwarf2_full_name (name, die, cu);
15046
15047 /* dwarf2_full_name might have already finished building the DIE's
15048 type. If so, there is no need to continue. */
15049 if (get_die_type (die, cu) != NULL)
15050 return get_die_type (die, cu);
15051
15052 TYPE_NAME (type) = full_name;
15053 }
15054 else
15055 {
15056 /* The name is already allocated along with this objfile, so
15057 we don't need to duplicate it for the type. */
15058 TYPE_NAME (type) = name;
15059 }
15060 }
15061
15062 if (die->tag == DW_TAG_structure_type)
15063 {
15064 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15065 }
15066 else if (die->tag == DW_TAG_union_type)
15067 {
15068 TYPE_CODE (type) = TYPE_CODE_UNION;
15069 }
15070 else if (die->tag == DW_TAG_variant_part)
15071 {
15072 TYPE_CODE (type) = TYPE_CODE_UNION;
15073 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15074 }
15075 else
15076 {
15077 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15078 }
15079
15080 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15081 TYPE_DECLARED_CLASS (type) = 1;
15082
15083 /* Store the calling convention in the type if it's available in
15084 the die. Otherwise the calling convention remains set to
15085 the default value DW_CC_normal. */
15086 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15087 if (attr != nullptr
15088 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15089 {
15090 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15091 TYPE_CPLUS_CALLING_CONVENTION (type)
15092 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15093 }
15094
15095 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15096 if (attr != nullptr)
15097 {
15098 if (attr->form_is_constant ())
15099 TYPE_LENGTH (type) = DW_UNSND (attr);
15100 else
15101 {
15102 /* For the moment, dynamic type sizes are not supported
15103 by GDB's struct type. The actual size is determined
15104 on-demand when resolving the type of a given object,
15105 so set the type's length to zero for now. Otherwise,
15106 we record an expression as the length, and that expression
15107 could lead to a very large value, which could eventually
15108 lead to us trying to allocate that much memory when creating
15109 a value of that type. */
15110 TYPE_LENGTH (type) = 0;
15111 }
15112 }
15113 else
15114 {
15115 TYPE_LENGTH (type) = 0;
15116 }
15117
15118 maybe_set_alignment (cu, die, type);
15119
15120 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15121 {
15122 /* ICC<14 does not output the required DW_AT_declaration on
15123 incomplete types, but gives them a size of zero. */
15124 TYPE_STUB (type) = 1;
15125 }
15126 else
15127 TYPE_STUB_SUPPORTED (type) = 1;
15128
15129 if (die_is_declaration (die, cu))
15130 TYPE_STUB (type) = 1;
15131 else if (attr == NULL && die->child == NULL
15132 && producer_is_realview (cu->producer))
15133 /* RealView does not output the required DW_AT_declaration
15134 on incomplete types. */
15135 TYPE_STUB (type) = 1;
15136
15137 /* We need to add the type field to the die immediately so we don't
15138 infinitely recurse when dealing with pointers to the structure
15139 type within the structure itself. */
15140 set_die_type (die, type, cu);
15141
15142 /* set_die_type should be already done. */
15143 set_descriptive_type (type, die, cu);
15144
15145 return type;
15146 }
15147
15148 /* A helper for process_structure_scope that handles a single member
15149 DIE. */
15150
15151 static void
15152 handle_struct_member_die (struct die_info *child_die, struct type *type,
15153 struct field_info *fi,
15154 std::vector<struct symbol *> *template_args,
15155 struct dwarf2_cu *cu)
15156 {
15157 if (child_die->tag == DW_TAG_member
15158 || child_die->tag == DW_TAG_variable
15159 || child_die->tag == DW_TAG_variant_part)
15160 {
15161 /* NOTE: carlton/2002-11-05: A C++ static data member
15162 should be a DW_TAG_member that is a declaration, but
15163 all versions of G++ as of this writing (so through at
15164 least 3.2.1) incorrectly generate DW_TAG_variable
15165 tags for them instead. */
15166 dwarf2_add_field (fi, child_die, cu);
15167 }
15168 else if (child_die->tag == DW_TAG_subprogram)
15169 {
15170 /* Rust doesn't have member functions in the C++ sense.
15171 However, it does emit ordinary functions as children
15172 of a struct DIE. */
15173 if (cu->language == language_rust)
15174 read_func_scope (child_die, cu);
15175 else
15176 {
15177 /* C++ member function. */
15178 dwarf2_add_member_fn (fi, child_die, type, cu);
15179 }
15180 }
15181 else if (child_die->tag == DW_TAG_inheritance)
15182 {
15183 /* C++ base class field. */
15184 dwarf2_add_field (fi, child_die, cu);
15185 }
15186 else if (type_can_define_types (child_die))
15187 dwarf2_add_type_defn (fi, child_die, cu);
15188 else if (child_die->tag == DW_TAG_template_type_param
15189 || child_die->tag == DW_TAG_template_value_param)
15190 {
15191 struct symbol *arg = new_symbol (child_die, NULL, cu);
15192
15193 if (arg != NULL)
15194 template_args->push_back (arg);
15195 }
15196 else if (child_die->tag == DW_TAG_variant)
15197 {
15198 /* In a variant we want to get the discriminant and also add a
15199 field for our sole member child. */
15200 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15201
15202 for (die_info *variant_child = child_die->child;
15203 variant_child != NULL;
15204 variant_child = sibling_die (variant_child))
15205 {
15206 if (variant_child->tag == DW_TAG_member)
15207 {
15208 handle_struct_member_die (variant_child, type, fi,
15209 template_args, cu);
15210 /* Only handle the one. */
15211 break;
15212 }
15213 }
15214
15215 /* We don't handle this but we might as well report it if we see
15216 it. */
15217 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15218 complaint (_("DW_AT_discr_list is not supported yet"
15219 " - DIE at %s [in module %s]"),
15220 sect_offset_str (child_die->sect_off),
15221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15222
15223 /* The first field was just added, so we can stash the
15224 discriminant there. */
15225 gdb_assert (!fi->fields.empty ());
15226 if (discr == NULL)
15227 fi->fields.back ().variant.default_branch = true;
15228 else
15229 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15230 }
15231 }
15232
15233 /* Finish creating a structure or union type, including filling in
15234 its members and creating a symbol for it. */
15235
15236 static void
15237 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15238 {
15239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15240 struct die_info *child_die;
15241 struct type *type;
15242
15243 type = get_die_type (die, cu);
15244 if (type == NULL)
15245 type = read_structure_type (die, cu);
15246
15247 /* When reading a DW_TAG_variant_part, we need to notice when we
15248 read the discriminant member, so we can record it later in the
15249 discriminant_info. */
15250 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15251 sect_offset discr_offset {};
15252 bool has_template_parameters = false;
15253
15254 if (is_variant_part)
15255 {
15256 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15257 if (discr == NULL)
15258 {
15259 /* Maybe it's a univariant form, an extension we support.
15260 In this case arrange not to check the offset. */
15261 is_variant_part = false;
15262 }
15263 else if (discr->form_is_ref ())
15264 {
15265 struct dwarf2_cu *target_cu = cu;
15266 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15267
15268 discr_offset = target_die->sect_off;
15269 }
15270 else
15271 {
15272 complaint (_("DW_AT_discr does not have DIE reference form"
15273 " - DIE at %s [in module %s]"),
15274 sect_offset_str (die->sect_off),
15275 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15276 is_variant_part = false;
15277 }
15278 }
15279
15280 if (die->child != NULL && ! die_is_declaration (die, cu))
15281 {
15282 struct field_info fi;
15283 std::vector<struct symbol *> template_args;
15284
15285 child_die = die->child;
15286
15287 while (child_die && child_die->tag)
15288 {
15289 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15290
15291 if (is_variant_part && discr_offset == child_die->sect_off)
15292 fi.fields.back ().variant.is_discriminant = true;
15293
15294 child_die = sibling_die (child_die);
15295 }
15296
15297 /* Attach template arguments to type. */
15298 if (!template_args.empty ())
15299 {
15300 has_template_parameters = true;
15301 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15302 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15303 TYPE_TEMPLATE_ARGUMENTS (type)
15304 = XOBNEWVEC (&objfile->objfile_obstack,
15305 struct symbol *,
15306 TYPE_N_TEMPLATE_ARGUMENTS (type));
15307 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15308 template_args.data (),
15309 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15310 * sizeof (struct symbol *)));
15311 }
15312
15313 /* Attach fields and member functions to the type. */
15314 if (fi.nfields)
15315 dwarf2_attach_fields_to_type (&fi, type, cu);
15316 if (!fi.fnfieldlists.empty ())
15317 {
15318 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15319
15320 /* Get the type which refers to the base class (possibly this
15321 class itself) which contains the vtable pointer for the current
15322 class from the DW_AT_containing_type attribute. This use of
15323 DW_AT_containing_type is a GNU extension. */
15324
15325 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15326 {
15327 struct type *t = die_containing_type (die, cu);
15328
15329 set_type_vptr_basetype (type, t);
15330 if (type == t)
15331 {
15332 int i;
15333
15334 /* Our own class provides vtbl ptr. */
15335 for (i = TYPE_NFIELDS (t) - 1;
15336 i >= TYPE_N_BASECLASSES (t);
15337 --i)
15338 {
15339 const char *fieldname = TYPE_FIELD_NAME (t, i);
15340
15341 if (is_vtable_name (fieldname, cu))
15342 {
15343 set_type_vptr_fieldno (type, i);
15344 break;
15345 }
15346 }
15347
15348 /* Complain if virtual function table field not found. */
15349 if (i < TYPE_N_BASECLASSES (t))
15350 complaint (_("virtual function table pointer "
15351 "not found when defining class '%s'"),
15352 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15353 }
15354 else
15355 {
15356 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15357 }
15358 }
15359 else if (cu->producer
15360 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15361 {
15362 /* The IBM XLC compiler does not provide direct indication
15363 of the containing type, but the vtable pointer is
15364 always named __vfp. */
15365
15366 int i;
15367
15368 for (i = TYPE_NFIELDS (type) - 1;
15369 i >= TYPE_N_BASECLASSES (type);
15370 --i)
15371 {
15372 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15373 {
15374 set_type_vptr_fieldno (type, i);
15375 set_type_vptr_basetype (type, type);
15376 break;
15377 }
15378 }
15379 }
15380 }
15381
15382 /* Copy fi.typedef_field_list linked list elements content into the
15383 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15384 if (!fi.typedef_field_list.empty ())
15385 {
15386 int count = fi.typedef_field_list.size ();
15387
15388 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15389 TYPE_TYPEDEF_FIELD_ARRAY (type)
15390 = ((struct decl_field *)
15391 TYPE_ALLOC (type,
15392 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15393 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15394
15395 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15396 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15397 }
15398
15399 /* Copy fi.nested_types_list linked list elements content into the
15400 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15401 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15402 {
15403 int count = fi.nested_types_list.size ();
15404
15405 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15406 TYPE_NESTED_TYPES_ARRAY (type)
15407 = ((struct decl_field *)
15408 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15409 TYPE_NESTED_TYPES_COUNT (type) = count;
15410
15411 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15412 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15413 }
15414 }
15415
15416 quirk_gcc_member_function_pointer (type, objfile);
15417 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15418 cu->rust_unions.push_back (type);
15419
15420 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15421 snapshots) has been known to create a die giving a declaration
15422 for a class that has, as a child, a die giving a definition for a
15423 nested class. So we have to process our children even if the
15424 current die is a declaration. Normally, of course, a declaration
15425 won't have any children at all. */
15426
15427 child_die = die->child;
15428
15429 while (child_die != NULL && child_die->tag)
15430 {
15431 if (child_die->tag == DW_TAG_member
15432 || child_die->tag == DW_TAG_variable
15433 || child_die->tag == DW_TAG_inheritance
15434 || child_die->tag == DW_TAG_template_value_param
15435 || child_die->tag == DW_TAG_template_type_param)
15436 {
15437 /* Do nothing. */
15438 }
15439 else
15440 process_die (child_die, cu);
15441
15442 child_die = sibling_die (child_die);
15443 }
15444
15445 /* Do not consider external references. According to the DWARF standard,
15446 these DIEs are identified by the fact that they have no byte_size
15447 attribute, and a declaration attribute. */
15448 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15449 || !die_is_declaration (die, cu))
15450 {
15451 struct symbol *sym = new_symbol (die, type, cu);
15452
15453 if (has_template_parameters)
15454 {
15455 struct symtab *symtab;
15456 if (sym != nullptr)
15457 symtab = symbol_symtab (sym);
15458 else if (cu->line_header != nullptr)
15459 {
15460 /* Any related symtab will do. */
15461 symtab
15462 = cu->line_header->file_names ()[0].symtab;
15463 }
15464 else
15465 {
15466 symtab = nullptr;
15467 complaint (_("could not find suitable "
15468 "symtab for template parameter"
15469 " - DIE at %s [in module %s]"),
15470 sect_offset_str (die->sect_off),
15471 objfile_name (objfile));
15472 }
15473
15474 if (symtab != nullptr)
15475 {
15476 /* Make sure that the symtab is set on the new symbols.
15477 Even though they don't appear in this symtab directly,
15478 other parts of gdb assume that symbols do, and this is
15479 reasonably true. */
15480 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15481 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15482 }
15483 }
15484 }
15485 }
15486
15487 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15488 update TYPE using some information only available in DIE's children. */
15489
15490 static void
15491 update_enumeration_type_from_children (struct die_info *die,
15492 struct type *type,
15493 struct dwarf2_cu *cu)
15494 {
15495 struct die_info *child_die;
15496 int unsigned_enum = 1;
15497 int flag_enum = 1;
15498 ULONGEST mask = 0;
15499
15500 auto_obstack obstack;
15501
15502 for (child_die = die->child;
15503 child_die != NULL && child_die->tag;
15504 child_die = sibling_die (child_die))
15505 {
15506 struct attribute *attr;
15507 LONGEST value;
15508 const gdb_byte *bytes;
15509 struct dwarf2_locexpr_baton *baton;
15510 const char *name;
15511
15512 if (child_die->tag != DW_TAG_enumerator)
15513 continue;
15514
15515 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15516 if (attr == NULL)
15517 continue;
15518
15519 name = dwarf2_name (child_die, cu);
15520 if (name == NULL)
15521 name = "<anonymous enumerator>";
15522
15523 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15524 &value, &bytes, &baton);
15525 if (value < 0)
15526 {
15527 unsigned_enum = 0;
15528 flag_enum = 0;
15529 }
15530 else
15531 {
15532 if (count_one_bits_ll (value) >= 2)
15533 flag_enum = 0;
15534 else if ((mask & value) != 0)
15535 flag_enum = 0;
15536 else
15537 mask |= value;
15538 }
15539
15540 /* If we already know that the enum type is neither unsigned, nor
15541 a flag type, no need to look at the rest of the enumerates. */
15542 if (!unsigned_enum && !flag_enum)
15543 break;
15544 }
15545
15546 if (unsigned_enum)
15547 TYPE_UNSIGNED (type) = 1;
15548 if (flag_enum)
15549 TYPE_FLAG_ENUM (type) = 1;
15550 }
15551
15552 /* Given a DW_AT_enumeration_type die, set its type. We do not
15553 complete the type's fields yet, or create any symbols. */
15554
15555 static struct type *
15556 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15557 {
15558 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15559 struct type *type;
15560 struct attribute *attr;
15561 const char *name;
15562
15563 /* If the definition of this type lives in .debug_types, read that type.
15564 Don't follow DW_AT_specification though, that will take us back up
15565 the chain and we want to go down. */
15566 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15567 if (attr != nullptr)
15568 {
15569 type = get_DW_AT_signature_type (die, attr, cu);
15570
15571 /* The type's CU may not be the same as CU.
15572 Ensure TYPE is recorded with CU in die_type_hash. */
15573 return set_die_type (die, type, cu);
15574 }
15575
15576 type = alloc_type (objfile);
15577
15578 TYPE_CODE (type) = TYPE_CODE_ENUM;
15579 name = dwarf2_full_name (NULL, die, cu);
15580 if (name != NULL)
15581 TYPE_NAME (type) = name;
15582
15583 attr = dwarf2_attr (die, DW_AT_type, cu);
15584 if (attr != NULL)
15585 {
15586 struct type *underlying_type = die_type (die, cu);
15587
15588 TYPE_TARGET_TYPE (type) = underlying_type;
15589 }
15590
15591 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15592 if (attr != nullptr)
15593 {
15594 TYPE_LENGTH (type) = DW_UNSND (attr);
15595 }
15596 else
15597 {
15598 TYPE_LENGTH (type) = 0;
15599 }
15600
15601 maybe_set_alignment (cu, die, type);
15602
15603 /* The enumeration DIE can be incomplete. In Ada, any type can be
15604 declared as private in the package spec, and then defined only
15605 inside the package body. Such types are known as Taft Amendment
15606 Types. When another package uses such a type, an incomplete DIE
15607 may be generated by the compiler. */
15608 if (die_is_declaration (die, cu))
15609 TYPE_STUB (type) = 1;
15610
15611 /* Finish the creation of this type by using the enum's children.
15612 We must call this even when the underlying type has been provided
15613 so that we can determine if we're looking at a "flag" enum. */
15614 update_enumeration_type_from_children (die, type, cu);
15615
15616 /* If this type has an underlying type that is not a stub, then we
15617 may use its attributes. We always use the "unsigned" attribute
15618 in this situation, because ordinarily we guess whether the type
15619 is unsigned -- but the guess can be wrong and the underlying type
15620 can tell us the reality. However, we defer to a local size
15621 attribute if one exists, because this lets the compiler override
15622 the underlying type if needed. */
15623 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15624 {
15625 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15626 if (TYPE_LENGTH (type) == 0)
15627 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15628 if (TYPE_RAW_ALIGN (type) == 0
15629 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15630 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15631 }
15632
15633 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15634
15635 return set_die_type (die, type, cu);
15636 }
15637
15638 /* Given a pointer to a die which begins an enumeration, process all
15639 the dies that define the members of the enumeration, and create the
15640 symbol for the enumeration type.
15641
15642 NOTE: We reverse the order of the element list. */
15643
15644 static void
15645 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15646 {
15647 struct type *this_type;
15648
15649 this_type = get_die_type (die, cu);
15650 if (this_type == NULL)
15651 this_type = read_enumeration_type (die, cu);
15652
15653 if (die->child != NULL)
15654 {
15655 struct die_info *child_die;
15656 struct symbol *sym;
15657 std::vector<struct field> fields;
15658 const char *name;
15659
15660 child_die = die->child;
15661 while (child_die && child_die->tag)
15662 {
15663 if (child_die->tag != DW_TAG_enumerator)
15664 {
15665 process_die (child_die, cu);
15666 }
15667 else
15668 {
15669 name = dwarf2_name (child_die, cu);
15670 if (name)
15671 {
15672 sym = new_symbol (child_die, this_type, cu);
15673
15674 fields.emplace_back ();
15675 struct field &field = fields.back ();
15676
15677 FIELD_NAME (field) = sym->linkage_name ();
15678 FIELD_TYPE (field) = NULL;
15679 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15680 FIELD_BITSIZE (field) = 0;
15681 }
15682 }
15683
15684 child_die = sibling_die (child_die);
15685 }
15686
15687 if (!fields.empty ())
15688 {
15689 TYPE_NFIELDS (this_type) = fields.size ();
15690 TYPE_FIELDS (this_type) = (struct field *)
15691 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15692 memcpy (TYPE_FIELDS (this_type), fields.data (),
15693 sizeof (struct field) * fields.size ());
15694 }
15695 }
15696
15697 /* If we are reading an enum from a .debug_types unit, and the enum
15698 is a declaration, and the enum is not the signatured type in the
15699 unit, then we do not want to add a symbol for it. Adding a
15700 symbol would in some cases obscure the true definition of the
15701 enum, giving users an incomplete type when the definition is
15702 actually available. Note that we do not want to do this for all
15703 enums which are just declarations, because C++0x allows forward
15704 enum declarations. */
15705 if (cu->per_cu->is_debug_types
15706 && die_is_declaration (die, cu))
15707 {
15708 struct signatured_type *sig_type;
15709
15710 sig_type = (struct signatured_type *) cu->per_cu;
15711 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15712 if (sig_type->type_offset_in_section != die->sect_off)
15713 return;
15714 }
15715
15716 new_symbol (die, this_type, cu);
15717 }
15718
15719 /* Extract all information from a DW_TAG_array_type DIE and put it in
15720 the DIE's type field. For now, this only handles one dimensional
15721 arrays. */
15722
15723 static struct type *
15724 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15725 {
15726 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15727 struct die_info *child_die;
15728 struct type *type;
15729 struct type *element_type, *range_type, *index_type;
15730 struct attribute *attr;
15731 const char *name;
15732 struct dynamic_prop *byte_stride_prop = NULL;
15733 unsigned int bit_stride = 0;
15734
15735 element_type = die_type (die, cu);
15736
15737 /* The die_type call above may have already set the type for this DIE. */
15738 type = get_die_type (die, cu);
15739 if (type)
15740 return type;
15741
15742 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15743 if (attr != NULL)
15744 {
15745 int stride_ok;
15746 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15747
15748 byte_stride_prop
15749 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15750 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15751 prop_type);
15752 if (!stride_ok)
15753 {
15754 complaint (_("unable to read array DW_AT_byte_stride "
15755 " - DIE at %s [in module %s]"),
15756 sect_offset_str (die->sect_off),
15757 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15758 /* Ignore this attribute. We will likely not be able to print
15759 arrays of this type correctly, but there is little we can do
15760 to help if we cannot read the attribute's value. */
15761 byte_stride_prop = NULL;
15762 }
15763 }
15764
15765 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15766 if (attr != NULL)
15767 bit_stride = DW_UNSND (attr);
15768
15769 /* Irix 6.2 native cc creates array types without children for
15770 arrays with unspecified length. */
15771 if (die->child == NULL)
15772 {
15773 index_type = objfile_type (objfile)->builtin_int;
15774 range_type = create_static_range_type (NULL, index_type, 0, -1);
15775 type = create_array_type_with_stride (NULL, element_type, range_type,
15776 byte_stride_prop, bit_stride);
15777 return set_die_type (die, type, cu);
15778 }
15779
15780 std::vector<struct type *> range_types;
15781 child_die = die->child;
15782 while (child_die && child_die->tag)
15783 {
15784 if (child_die->tag == DW_TAG_subrange_type)
15785 {
15786 struct type *child_type = read_type_die (child_die, cu);
15787
15788 if (child_type != NULL)
15789 {
15790 /* The range type was succesfully read. Save it for the
15791 array type creation. */
15792 range_types.push_back (child_type);
15793 }
15794 }
15795 child_die = sibling_die (child_die);
15796 }
15797
15798 /* Dwarf2 dimensions are output from left to right, create the
15799 necessary array types in backwards order. */
15800
15801 type = element_type;
15802
15803 if (read_array_order (die, cu) == DW_ORD_col_major)
15804 {
15805 int i = 0;
15806
15807 while (i < range_types.size ())
15808 type = create_array_type_with_stride (NULL, type, range_types[i++],
15809 byte_stride_prop, bit_stride);
15810 }
15811 else
15812 {
15813 size_t ndim = range_types.size ();
15814 while (ndim-- > 0)
15815 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15816 byte_stride_prop, bit_stride);
15817 }
15818
15819 /* Understand Dwarf2 support for vector types (like they occur on
15820 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15821 array type. This is not part of the Dwarf2/3 standard yet, but a
15822 custom vendor extension. The main difference between a regular
15823 array and the vector variant is that vectors are passed by value
15824 to functions. */
15825 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15826 if (attr != nullptr)
15827 make_vector_type (type);
15828
15829 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15830 implementation may choose to implement triple vectors using this
15831 attribute. */
15832 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15833 if (attr != nullptr)
15834 {
15835 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15836 TYPE_LENGTH (type) = DW_UNSND (attr);
15837 else
15838 complaint (_("DW_AT_byte_size for array type smaller "
15839 "than the total size of elements"));
15840 }
15841
15842 name = dwarf2_name (die, cu);
15843 if (name)
15844 TYPE_NAME (type) = name;
15845
15846 maybe_set_alignment (cu, die, type);
15847
15848 /* Install the type in the die. */
15849 set_die_type (die, type, cu);
15850
15851 /* set_die_type should be already done. */
15852 set_descriptive_type (type, die, cu);
15853
15854 return type;
15855 }
15856
15857 static enum dwarf_array_dim_ordering
15858 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15859 {
15860 struct attribute *attr;
15861
15862 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15863
15864 if (attr != nullptr)
15865 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15866
15867 /* GNU F77 is a special case, as at 08/2004 array type info is the
15868 opposite order to the dwarf2 specification, but data is still
15869 laid out as per normal fortran.
15870
15871 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15872 version checking. */
15873
15874 if (cu->language == language_fortran
15875 && cu->producer && strstr (cu->producer, "GNU F77"))
15876 {
15877 return DW_ORD_row_major;
15878 }
15879
15880 switch (cu->language_defn->la_array_ordering)
15881 {
15882 case array_column_major:
15883 return DW_ORD_col_major;
15884 case array_row_major:
15885 default:
15886 return DW_ORD_row_major;
15887 };
15888 }
15889
15890 /* Extract all information from a DW_TAG_set_type DIE and put it in
15891 the DIE's type field. */
15892
15893 static struct type *
15894 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15895 {
15896 struct type *domain_type, *set_type;
15897 struct attribute *attr;
15898
15899 domain_type = die_type (die, cu);
15900
15901 /* The die_type call above may have already set the type for this DIE. */
15902 set_type = get_die_type (die, cu);
15903 if (set_type)
15904 return set_type;
15905
15906 set_type = create_set_type (NULL, domain_type);
15907
15908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15909 if (attr != nullptr)
15910 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15911
15912 maybe_set_alignment (cu, die, set_type);
15913
15914 return set_die_type (die, set_type, cu);
15915 }
15916
15917 /* A helper for read_common_block that creates a locexpr baton.
15918 SYM is the symbol which we are marking as computed.
15919 COMMON_DIE is the DIE for the common block.
15920 COMMON_LOC is the location expression attribute for the common
15921 block itself.
15922 MEMBER_LOC is the location expression attribute for the particular
15923 member of the common block that we are processing.
15924 CU is the CU from which the above come. */
15925
15926 static void
15927 mark_common_block_symbol_computed (struct symbol *sym,
15928 struct die_info *common_die,
15929 struct attribute *common_loc,
15930 struct attribute *member_loc,
15931 struct dwarf2_cu *cu)
15932 {
15933 struct dwarf2_per_objfile *dwarf2_per_objfile
15934 = cu->per_cu->dwarf2_per_objfile;
15935 struct objfile *objfile = dwarf2_per_objfile->objfile;
15936 struct dwarf2_locexpr_baton *baton;
15937 gdb_byte *ptr;
15938 unsigned int cu_off;
15939 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15940 LONGEST offset = 0;
15941
15942 gdb_assert (common_loc && member_loc);
15943 gdb_assert (common_loc->form_is_block ());
15944 gdb_assert (member_loc->form_is_block ()
15945 || member_loc->form_is_constant ());
15946
15947 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15948 baton->per_cu = cu->per_cu;
15949 gdb_assert (baton->per_cu);
15950
15951 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15952
15953 if (member_loc->form_is_constant ())
15954 {
15955 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15956 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15957 }
15958 else
15959 baton->size += DW_BLOCK (member_loc)->size;
15960
15961 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15962 baton->data = ptr;
15963
15964 *ptr++ = DW_OP_call4;
15965 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15966 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15967 ptr += 4;
15968
15969 if (member_loc->form_is_constant ())
15970 {
15971 *ptr++ = DW_OP_addr;
15972 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15973 ptr += cu->header.addr_size;
15974 }
15975 else
15976 {
15977 /* We have to copy the data here, because DW_OP_call4 will only
15978 use a DW_AT_location attribute. */
15979 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15980 ptr += DW_BLOCK (member_loc)->size;
15981 }
15982
15983 *ptr++ = DW_OP_plus;
15984 gdb_assert (ptr - baton->data == baton->size);
15985
15986 SYMBOL_LOCATION_BATON (sym) = baton;
15987 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15988 }
15989
15990 /* Create appropriate locally-scoped variables for all the
15991 DW_TAG_common_block entries. Also create a struct common_block
15992 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15993 is used to separate the common blocks name namespace from regular
15994 variable names. */
15995
15996 static void
15997 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15998 {
15999 struct attribute *attr;
16000
16001 attr = dwarf2_attr (die, DW_AT_location, cu);
16002 if (attr != nullptr)
16003 {
16004 /* Support the .debug_loc offsets. */
16005 if (attr->form_is_block ())
16006 {
16007 /* Ok. */
16008 }
16009 else if (attr->form_is_section_offset ())
16010 {
16011 dwarf2_complex_location_expr_complaint ();
16012 attr = NULL;
16013 }
16014 else
16015 {
16016 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16017 "common block member");
16018 attr = NULL;
16019 }
16020 }
16021
16022 if (die->child != NULL)
16023 {
16024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16025 struct die_info *child_die;
16026 size_t n_entries = 0, size;
16027 struct common_block *common_block;
16028 struct symbol *sym;
16029
16030 for (child_die = die->child;
16031 child_die && child_die->tag;
16032 child_die = sibling_die (child_die))
16033 ++n_entries;
16034
16035 size = (sizeof (struct common_block)
16036 + (n_entries - 1) * sizeof (struct symbol *));
16037 common_block
16038 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16039 size);
16040 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16041 common_block->n_entries = 0;
16042
16043 for (child_die = die->child;
16044 child_die && child_die->tag;
16045 child_die = sibling_die (child_die))
16046 {
16047 /* Create the symbol in the DW_TAG_common_block block in the current
16048 symbol scope. */
16049 sym = new_symbol (child_die, NULL, cu);
16050 if (sym != NULL)
16051 {
16052 struct attribute *member_loc;
16053
16054 common_block->contents[common_block->n_entries++] = sym;
16055
16056 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16057 cu);
16058 if (member_loc)
16059 {
16060 /* GDB has handled this for a long time, but it is
16061 not specified by DWARF. It seems to have been
16062 emitted by gfortran at least as recently as:
16063 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16064 complaint (_("Variable in common block has "
16065 "DW_AT_data_member_location "
16066 "- DIE at %s [in module %s]"),
16067 sect_offset_str (child_die->sect_off),
16068 objfile_name (objfile));
16069
16070 if (member_loc->form_is_section_offset ())
16071 dwarf2_complex_location_expr_complaint ();
16072 else if (member_loc->form_is_constant ()
16073 || member_loc->form_is_block ())
16074 {
16075 if (attr != nullptr)
16076 mark_common_block_symbol_computed (sym, die, attr,
16077 member_loc, cu);
16078 }
16079 else
16080 dwarf2_complex_location_expr_complaint ();
16081 }
16082 }
16083 }
16084
16085 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16086 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16087 }
16088 }
16089
16090 /* Create a type for a C++ namespace. */
16091
16092 static struct type *
16093 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16094 {
16095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16096 const char *previous_prefix, *name;
16097 int is_anonymous;
16098 struct type *type;
16099
16100 /* For extensions, reuse the type of the original namespace. */
16101 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16102 {
16103 struct die_info *ext_die;
16104 struct dwarf2_cu *ext_cu = cu;
16105
16106 ext_die = dwarf2_extension (die, &ext_cu);
16107 type = read_type_die (ext_die, ext_cu);
16108
16109 /* EXT_CU may not be the same as CU.
16110 Ensure TYPE is recorded with CU in die_type_hash. */
16111 return set_die_type (die, type, cu);
16112 }
16113
16114 name = namespace_name (die, &is_anonymous, cu);
16115
16116 /* Now build the name of the current namespace. */
16117
16118 previous_prefix = determine_prefix (die, cu);
16119 if (previous_prefix[0] != '\0')
16120 name = typename_concat (&objfile->objfile_obstack,
16121 previous_prefix, name, 0, cu);
16122
16123 /* Create the type. */
16124 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16125
16126 return set_die_type (die, type, cu);
16127 }
16128
16129 /* Read a namespace scope. */
16130
16131 static void
16132 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16133 {
16134 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16135 int is_anonymous;
16136
16137 /* Add a symbol associated to this if we haven't seen the namespace
16138 before. Also, add a using directive if it's an anonymous
16139 namespace. */
16140
16141 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16142 {
16143 struct type *type;
16144
16145 type = read_type_die (die, cu);
16146 new_symbol (die, type, cu);
16147
16148 namespace_name (die, &is_anonymous, cu);
16149 if (is_anonymous)
16150 {
16151 const char *previous_prefix = determine_prefix (die, cu);
16152
16153 std::vector<const char *> excludes;
16154 add_using_directive (using_directives (cu),
16155 previous_prefix, TYPE_NAME (type), NULL,
16156 NULL, excludes, 0, &objfile->objfile_obstack);
16157 }
16158 }
16159
16160 if (die->child != NULL)
16161 {
16162 struct die_info *child_die = die->child;
16163
16164 while (child_die && child_die->tag)
16165 {
16166 process_die (child_die, cu);
16167 child_die = sibling_die (child_die);
16168 }
16169 }
16170 }
16171
16172 /* Read a Fortran module as type. This DIE can be only a declaration used for
16173 imported module. Still we need that type as local Fortran "use ... only"
16174 declaration imports depend on the created type in determine_prefix. */
16175
16176 static struct type *
16177 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16178 {
16179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16180 const char *module_name;
16181 struct type *type;
16182
16183 module_name = dwarf2_name (die, cu);
16184 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16185
16186 return set_die_type (die, type, cu);
16187 }
16188
16189 /* Read a Fortran module. */
16190
16191 static void
16192 read_module (struct die_info *die, struct dwarf2_cu *cu)
16193 {
16194 struct die_info *child_die = die->child;
16195 struct type *type;
16196
16197 type = read_type_die (die, cu);
16198 new_symbol (die, type, cu);
16199
16200 while (child_die && child_die->tag)
16201 {
16202 process_die (child_die, cu);
16203 child_die = sibling_die (child_die);
16204 }
16205 }
16206
16207 /* Return the name of the namespace represented by DIE. Set
16208 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16209 namespace. */
16210
16211 static const char *
16212 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16213 {
16214 struct die_info *current_die;
16215 const char *name = NULL;
16216
16217 /* Loop through the extensions until we find a name. */
16218
16219 for (current_die = die;
16220 current_die != NULL;
16221 current_die = dwarf2_extension (die, &cu))
16222 {
16223 /* We don't use dwarf2_name here so that we can detect the absence
16224 of a name -> anonymous namespace. */
16225 name = dwarf2_string_attr (die, DW_AT_name, cu);
16226
16227 if (name != NULL)
16228 break;
16229 }
16230
16231 /* Is it an anonymous namespace? */
16232
16233 *is_anonymous = (name == NULL);
16234 if (*is_anonymous)
16235 name = CP_ANONYMOUS_NAMESPACE_STR;
16236
16237 return name;
16238 }
16239
16240 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16241 the user defined type vector. */
16242
16243 static struct type *
16244 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16245 {
16246 struct gdbarch *gdbarch
16247 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16248 struct comp_unit_head *cu_header = &cu->header;
16249 struct type *type;
16250 struct attribute *attr_byte_size;
16251 struct attribute *attr_address_class;
16252 int byte_size, addr_class;
16253 struct type *target_type;
16254
16255 target_type = die_type (die, cu);
16256
16257 /* The die_type call above may have already set the type for this DIE. */
16258 type = get_die_type (die, cu);
16259 if (type)
16260 return type;
16261
16262 type = lookup_pointer_type (target_type);
16263
16264 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16265 if (attr_byte_size)
16266 byte_size = DW_UNSND (attr_byte_size);
16267 else
16268 byte_size = cu_header->addr_size;
16269
16270 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16271 if (attr_address_class)
16272 addr_class = DW_UNSND (attr_address_class);
16273 else
16274 addr_class = DW_ADDR_none;
16275
16276 ULONGEST alignment = get_alignment (cu, die);
16277
16278 /* If the pointer size, alignment, or address class is different
16279 than the default, create a type variant marked as such and set
16280 the length accordingly. */
16281 if (TYPE_LENGTH (type) != byte_size
16282 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16283 && alignment != TYPE_RAW_ALIGN (type))
16284 || addr_class != DW_ADDR_none)
16285 {
16286 if (gdbarch_address_class_type_flags_p (gdbarch))
16287 {
16288 int type_flags;
16289
16290 type_flags = gdbarch_address_class_type_flags
16291 (gdbarch, byte_size, addr_class);
16292 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16293 == 0);
16294 type = make_type_with_address_space (type, type_flags);
16295 }
16296 else if (TYPE_LENGTH (type) != byte_size)
16297 {
16298 complaint (_("invalid pointer size %d"), byte_size);
16299 }
16300 else if (TYPE_RAW_ALIGN (type) != alignment)
16301 {
16302 complaint (_("Invalid DW_AT_alignment"
16303 " - DIE at %s [in module %s]"),
16304 sect_offset_str (die->sect_off),
16305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16306 }
16307 else
16308 {
16309 /* Should we also complain about unhandled address classes? */
16310 }
16311 }
16312
16313 TYPE_LENGTH (type) = byte_size;
16314 set_type_align (type, alignment);
16315 return set_die_type (die, type, cu);
16316 }
16317
16318 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16319 the user defined type vector. */
16320
16321 static struct type *
16322 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16323 {
16324 struct type *type;
16325 struct type *to_type;
16326 struct type *domain;
16327
16328 to_type = die_type (die, cu);
16329 domain = die_containing_type (die, cu);
16330
16331 /* The calls above may have already set the type for this DIE. */
16332 type = get_die_type (die, cu);
16333 if (type)
16334 return type;
16335
16336 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16337 type = lookup_methodptr_type (to_type);
16338 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16339 {
16340 struct type *new_type
16341 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16342
16343 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16344 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16345 TYPE_VARARGS (to_type));
16346 type = lookup_methodptr_type (new_type);
16347 }
16348 else
16349 type = lookup_memberptr_type (to_type, domain);
16350
16351 return set_die_type (die, type, cu);
16352 }
16353
16354 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16355 the user defined type vector. */
16356
16357 static struct type *
16358 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16359 enum type_code refcode)
16360 {
16361 struct comp_unit_head *cu_header = &cu->header;
16362 struct type *type, *target_type;
16363 struct attribute *attr;
16364
16365 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16366
16367 target_type = die_type (die, cu);
16368
16369 /* The die_type call above may have already set the type for this DIE. */
16370 type = get_die_type (die, cu);
16371 if (type)
16372 return type;
16373
16374 type = lookup_reference_type (target_type, refcode);
16375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16376 if (attr != nullptr)
16377 {
16378 TYPE_LENGTH (type) = DW_UNSND (attr);
16379 }
16380 else
16381 {
16382 TYPE_LENGTH (type) = cu_header->addr_size;
16383 }
16384 maybe_set_alignment (cu, die, type);
16385 return set_die_type (die, type, cu);
16386 }
16387
16388 /* Add the given cv-qualifiers to the element type of the array. GCC
16389 outputs DWARF type qualifiers that apply to an array, not the
16390 element type. But GDB relies on the array element type to carry
16391 the cv-qualifiers. This mimics section 6.7.3 of the C99
16392 specification. */
16393
16394 static struct type *
16395 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16396 struct type *base_type, int cnst, int voltl)
16397 {
16398 struct type *el_type, *inner_array;
16399
16400 base_type = copy_type (base_type);
16401 inner_array = base_type;
16402
16403 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16404 {
16405 TYPE_TARGET_TYPE (inner_array) =
16406 copy_type (TYPE_TARGET_TYPE (inner_array));
16407 inner_array = TYPE_TARGET_TYPE (inner_array);
16408 }
16409
16410 el_type = TYPE_TARGET_TYPE (inner_array);
16411 cnst |= TYPE_CONST (el_type);
16412 voltl |= TYPE_VOLATILE (el_type);
16413 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16414
16415 return set_die_type (die, base_type, cu);
16416 }
16417
16418 static struct type *
16419 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16420 {
16421 struct type *base_type, *cv_type;
16422
16423 base_type = die_type (die, cu);
16424
16425 /* The die_type call above may have already set the type for this DIE. */
16426 cv_type = get_die_type (die, cu);
16427 if (cv_type)
16428 return cv_type;
16429
16430 /* In case the const qualifier is applied to an array type, the element type
16431 is so qualified, not the array type (section 6.7.3 of C99). */
16432 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16433 return add_array_cv_type (die, cu, base_type, 1, 0);
16434
16435 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16436 return set_die_type (die, cv_type, cu);
16437 }
16438
16439 static struct type *
16440 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16441 {
16442 struct type *base_type, *cv_type;
16443
16444 base_type = die_type (die, cu);
16445
16446 /* The die_type call above may have already set the type for this DIE. */
16447 cv_type = get_die_type (die, cu);
16448 if (cv_type)
16449 return cv_type;
16450
16451 /* In case the volatile qualifier is applied to an array type, the
16452 element type is so qualified, not the array type (section 6.7.3
16453 of C99). */
16454 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16455 return add_array_cv_type (die, cu, base_type, 0, 1);
16456
16457 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16458 return set_die_type (die, cv_type, cu);
16459 }
16460
16461 /* Handle DW_TAG_restrict_type. */
16462
16463 static struct type *
16464 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16465 {
16466 struct type *base_type, *cv_type;
16467
16468 base_type = die_type (die, cu);
16469
16470 /* The die_type call above may have already set the type for this DIE. */
16471 cv_type = get_die_type (die, cu);
16472 if (cv_type)
16473 return cv_type;
16474
16475 cv_type = make_restrict_type (base_type);
16476 return set_die_type (die, cv_type, cu);
16477 }
16478
16479 /* Handle DW_TAG_atomic_type. */
16480
16481 static struct type *
16482 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16483 {
16484 struct type *base_type, *cv_type;
16485
16486 base_type = die_type (die, cu);
16487
16488 /* The die_type call above may have already set the type for this DIE. */
16489 cv_type = get_die_type (die, cu);
16490 if (cv_type)
16491 return cv_type;
16492
16493 cv_type = make_atomic_type (base_type);
16494 return set_die_type (die, cv_type, cu);
16495 }
16496
16497 /* Extract all information from a DW_TAG_string_type DIE and add to
16498 the user defined type vector. It isn't really a user defined type,
16499 but it behaves like one, with other DIE's using an AT_user_def_type
16500 attribute to reference it. */
16501
16502 static struct type *
16503 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16504 {
16505 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16507 struct type *type, *range_type, *index_type, *char_type;
16508 struct attribute *attr;
16509 struct dynamic_prop prop;
16510 bool length_is_constant = true;
16511 LONGEST length;
16512
16513 /* There are a couple of places where bit sizes might be made use of
16514 when parsing a DW_TAG_string_type, however, no producer that we know
16515 of make use of these. Handling bit sizes that are a multiple of the
16516 byte size is easy enough, but what about other bit sizes? Lets deal
16517 with that problem when we have to. Warn about these attributes being
16518 unsupported, then parse the type and ignore them like we always
16519 have. */
16520 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16521 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16522 {
16523 static bool warning_printed = false;
16524 if (!warning_printed)
16525 {
16526 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16527 "currently supported on DW_TAG_string_type."));
16528 warning_printed = true;
16529 }
16530 }
16531
16532 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16533 if (attr != nullptr && !attr->form_is_constant ())
16534 {
16535 /* The string length describes the location at which the length of
16536 the string can be found. The size of the length field can be
16537 specified with one of the attributes below. */
16538 struct type *prop_type;
16539 struct attribute *len
16540 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16541 if (len == nullptr)
16542 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16543 if (len != nullptr && len->form_is_constant ())
16544 {
16545 /* Pass 0 as the default as we know this attribute is constant
16546 and the default value will not be returned. */
16547 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16548 prop_type = cu->per_cu->int_type (sz, true);
16549 }
16550 else
16551 {
16552 /* If the size is not specified then we assume it is the size of
16553 an address on this target. */
16554 prop_type = cu->per_cu->addr_sized_int_type (true);
16555 }
16556
16557 /* Convert the attribute into a dynamic property. */
16558 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16559 length = 1;
16560 else
16561 length_is_constant = false;
16562 }
16563 else if (attr != nullptr)
16564 {
16565 /* This DW_AT_string_length just contains the length with no
16566 indirection. There's no need to create a dynamic property in this
16567 case. Pass 0 for the default value as we know it will not be
16568 returned in this case. */
16569 length = dwarf2_get_attr_constant_value (attr, 0);
16570 }
16571 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16572 {
16573 /* We don't currently support non-constant byte sizes for strings. */
16574 length = dwarf2_get_attr_constant_value (attr, 1);
16575 }
16576 else
16577 {
16578 /* Use 1 as a fallback length if we have nothing else. */
16579 length = 1;
16580 }
16581
16582 index_type = objfile_type (objfile)->builtin_int;
16583 if (length_is_constant)
16584 range_type = create_static_range_type (NULL, index_type, 1, length);
16585 else
16586 {
16587 struct dynamic_prop low_bound;
16588
16589 low_bound.kind = PROP_CONST;
16590 low_bound.data.const_val = 1;
16591 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16592 }
16593 char_type = language_string_char_type (cu->language_defn, gdbarch);
16594 type = create_string_type (NULL, char_type, range_type);
16595
16596 return set_die_type (die, type, cu);
16597 }
16598
16599 /* Assuming that DIE corresponds to a function, returns nonzero
16600 if the function is prototyped. */
16601
16602 static int
16603 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16604 {
16605 struct attribute *attr;
16606
16607 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16608 if (attr && (DW_UNSND (attr) != 0))
16609 return 1;
16610
16611 /* The DWARF standard implies that the DW_AT_prototyped attribute
16612 is only meaningful for C, but the concept also extends to other
16613 languages that allow unprototyped functions (Eg: Objective C).
16614 For all other languages, assume that functions are always
16615 prototyped. */
16616 if (cu->language != language_c
16617 && cu->language != language_objc
16618 && cu->language != language_opencl)
16619 return 1;
16620
16621 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16622 prototyped and unprototyped functions; default to prototyped,
16623 since that is more common in modern code (and RealView warns
16624 about unprototyped functions). */
16625 if (producer_is_realview (cu->producer))
16626 return 1;
16627
16628 return 0;
16629 }
16630
16631 /* Handle DIES due to C code like:
16632
16633 struct foo
16634 {
16635 int (*funcp)(int a, long l);
16636 int b;
16637 };
16638
16639 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16640
16641 static struct type *
16642 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16643 {
16644 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16645 struct type *type; /* Type that this function returns. */
16646 struct type *ftype; /* Function that returns above type. */
16647 struct attribute *attr;
16648
16649 type = die_type (die, cu);
16650
16651 /* The die_type call above may have already set the type for this DIE. */
16652 ftype = get_die_type (die, cu);
16653 if (ftype)
16654 return ftype;
16655
16656 ftype = lookup_function_type (type);
16657
16658 if (prototyped_function_p (die, cu))
16659 TYPE_PROTOTYPED (ftype) = 1;
16660
16661 /* Store the calling convention in the type if it's available in
16662 the subroutine die. Otherwise set the calling convention to
16663 the default value DW_CC_normal. */
16664 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16665 if (attr != nullptr
16666 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16667 TYPE_CALLING_CONVENTION (ftype)
16668 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16669 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16670 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16671 else
16672 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16673
16674 /* Record whether the function returns normally to its caller or not
16675 if the DWARF producer set that information. */
16676 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16677 if (attr && (DW_UNSND (attr) != 0))
16678 TYPE_NO_RETURN (ftype) = 1;
16679
16680 /* We need to add the subroutine type to the die immediately so
16681 we don't infinitely recurse when dealing with parameters
16682 declared as the same subroutine type. */
16683 set_die_type (die, ftype, cu);
16684
16685 if (die->child != NULL)
16686 {
16687 struct type *void_type = objfile_type (objfile)->builtin_void;
16688 struct die_info *child_die;
16689 int nparams, iparams;
16690
16691 /* Count the number of parameters.
16692 FIXME: GDB currently ignores vararg functions, but knows about
16693 vararg member functions. */
16694 nparams = 0;
16695 child_die = die->child;
16696 while (child_die && child_die->tag)
16697 {
16698 if (child_die->tag == DW_TAG_formal_parameter)
16699 nparams++;
16700 else if (child_die->tag == DW_TAG_unspecified_parameters)
16701 TYPE_VARARGS (ftype) = 1;
16702 child_die = sibling_die (child_die);
16703 }
16704
16705 /* Allocate storage for parameters and fill them in. */
16706 TYPE_NFIELDS (ftype) = nparams;
16707 TYPE_FIELDS (ftype) = (struct field *)
16708 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16709
16710 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16711 even if we error out during the parameters reading below. */
16712 for (iparams = 0; iparams < nparams; iparams++)
16713 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16714
16715 iparams = 0;
16716 child_die = die->child;
16717 while (child_die && child_die->tag)
16718 {
16719 if (child_die->tag == DW_TAG_formal_parameter)
16720 {
16721 struct type *arg_type;
16722
16723 /* DWARF version 2 has no clean way to discern C++
16724 static and non-static member functions. G++ helps
16725 GDB by marking the first parameter for non-static
16726 member functions (which is the this pointer) as
16727 artificial. We pass this information to
16728 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16729
16730 DWARF version 3 added DW_AT_object_pointer, which GCC
16731 4.5 does not yet generate. */
16732 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16733 if (attr != nullptr)
16734 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16735 else
16736 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16737 arg_type = die_type (child_die, cu);
16738
16739 /* RealView does not mark THIS as const, which the testsuite
16740 expects. GCC marks THIS as const in method definitions,
16741 but not in the class specifications (GCC PR 43053). */
16742 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16743 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16744 {
16745 int is_this = 0;
16746 struct dwarf2_cu *arg_cu = cu;
16747 const char *name = dwarf2_name (child_die, cu);
16748
16749 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16750 if (attr != nullptr)
16751 {
16752 /* If the compiler emits this, use it. */
16753 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16754 is_this = 1;
16755 }
16756 else if (name && strcmp (name, "this") == 0)
16757 /* Function definitions will have the argument names. */
16758 is_this = 1;
16759 else if (name == NULL && iparams == 0)
16760 /* Declarations may not have the names, so like
16761 elsewhere in GDB, assume an artificial first
16762 argument is "this". */
16763 is_this = 1;
16764
16765 if (is_this)
16766 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16767 arg_type, 0);
16768 }
16769
16770 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16771 iparams++;
16772 }
16773 child_die = sibling_die (child_die);
16774 }
16775 }
16776
16777 return ftype;
16778 }
16779
16780 static struct type *
16781 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16782 {
16783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16784 const char *name = NULL;
16785 struct type *this_type, *target_type;
16786
16787 name = dwarf2_full_name (NULL, die, cu);
16788 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16789 TYPE_TARGET_STUB (this_type) = 1;
16790 set_die_type (die, this_type, cu);
16791 target_type = die_type (die, cu);
16792 if (target_type != this_type)
16793 TYPE_TARGET_TYPE (this_type) = target_type;
16794 else
16795 {
16796 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16797 spec and cause infinite loops in GDB. */
16798 complaint (_("Self-referential DW_TAG_typedef "
16799 "- DIE at %s [in module %s]"),
16800 sect_offset_str (die->sect_off), objfile_name (objfile));
16801 TYPE_TARGET_TYPE (this_type) = NULL;
16802 }
16803 return this_type;
16804 }
16805
16806 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16807 (which may be different from NAME) to the architecture back-end to allow
16808 it to guess the correct format if necessary. */
16809
16810 static struct type *
16811 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16812 const char *name_hint, enum bfd_endian byte_order)
16813 {
16814 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16815 const struct floatformat **format;
16816 struct type *type;
16817
16818 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16819 if (format)
16820 type = init_float_type (objfile, bits, name, format, byte_order);
16821 else
16822 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16823
16824 return type;
16825 }
16826
16827 /* Allocate an integer type of size BITS and name NAME. */
16828
16829 static struct type *
16830 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16831 int bits, int unsigned_p, const char *name)
16832 {
16833 struct type *type;
16834
16835 /* Versions of Intel's C Compiler generate an integer type called "void"
16836 instead of using DW_TAG_unspecified_type. This has been seen on
16837 at least versions 14, 17, and 18. */
16838 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16839 && strcmp (name, "void") == 0)
16840 type = objfile_type (objfile)->builtin_void;
16841 else
16842 type = init_integer_type (objfile, bits, unsigned_p, name);
16843
16844 return type;
16845 }
16846
16847 /* Initialise and return a floating point type of size BITS suitable for
16848 use as a component of a complex number. The NAME_HINT is passed through
16849 when initialising the floating point type and is the name of the complex
16850 type.
16851
16852 As DWARF doesn't currently provide an explicit name for the components
16853 of a complex number, but it can be helpful to have these components
16854 named, we try to select a suitable name based on the size of the
16855 component. */
16856 static struct type *
16857 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16858 struct objfile *objfile,
16859 int bits, const char *name_hint,
16860 enum bfd_endian byte_order)
16861 {
16862 gdbarch *gdbarch = get_objfile_arch (objfile);
16863 struct type *tt = nullptr;
16864
16865 /* Try to find a suitable floating point builtin type of size BITS.
16866 We're going to use the name of this type as the name for the complex
16867 target type that we are about to create. */
16868 switch (cu->language)
16869 {
16870 case language_fortran:
16871 switch (bits)
16872 {
16873 case 32:
16874 tt = builtin_f_type (gdbarch)->builtin_real;
16875 break;
16876 case 64:
16877 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16878 break;
16879 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16880 case 128:
16881 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16882 break;
16883 }
16884 break;
16885 default:
16886 switch (bits)
16887 {
16888 case 32:
16889 tt = builtin_type (gdbarch)->builtin_float;
16890 break;
16891 case 64:
16892 tt = builtin_type (gdbarch)->builtin_double;
16893 break;
16894 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16895 case 128:
16896 tt = builtin_type (gdbarch)->builtin_long_double;
16897 break;
16898 }
16899 break;
16900 }
16901
16902 /* If the type we found doesn't match the size we were looking for, then
16903 pretend we didn't find a type at all, the complex target type we
16904 create will then be nameless. */
16905 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16906 tt = nullptr;
16907
16908 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16909 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16910 }
16911
16912 /* Find a representation of a given base type and install
16913 it in the TYPE field of the die. */
16914
16915 static struct type *
16916 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16917 {
16918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16919 struct type *type;
16920 struct attribute *attr;
16921 int encoding = 0, bits = 0;
16922 const char *name;
16923 gdbarch *arch;
16924
16925 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16926 if (attr != nullptr)
16927 encoding = DW_UNSND (attr);
16928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16929 if (attr != nullptr)
16930 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16931 name = dwarf2_name (die, cu);
16932 if (!name)
16933 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16934
16935 arch = get_objfile_arch (objfile);
16936 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16937
16938 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16939 if (attr)
16940 {
16941 int endianity = DW_UNSND (attr);
16942
16943 switch (endianity)
16944 {
16945 case DW_END_big:
16946 byte_order = BFD_ENDIAN_BIG;
16947 break;
16948 case DW_END_little:
16949 byte_order = BFD_ENDIAN_LITTLE;
16950 break;
16951 default:
16952 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16953 break;
16954 }
16955 }
16956
16957 switch (encoding)
16958 {
16959 case DW_ATE_address:
16960 /* Turn DW_ATE_address into a void * pointer. */
16961 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16962 type = init_pointer_type (objfile, bits, name, type);
16963 break;
16964 case DW_ATE_boolean:
16965 type = init_boolean_type (objfile, bits, 1, name);
16966 break;
16967 case DW_ATE_complex_float:
16968 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16969 byte_order);
16970 type = init_complex_type (objfile, name, type);
16971 break;
16972 case DW_ATE_decimal_float:
16973 type = init_decfloat_type (objfile, bits, name);
16974 break;
16975 case DW_ATE_float:
16976 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16977 break;
16978 case DW_ATE_signed:
16979 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16980 break;
16981 case DW_ATE_unsigned:
16982 if (cu->language == language_fortran
16983 && name
16984 && startswith (name, "character("))
16985 type = init_character_type (objfile, bits, 1, name);
16986 else
16987 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16988 break;
16989 case DW_ATE_signed_char:
16990 if (cu->language == language_ada || cu->language == language_m2
16991 || cu->language == language_pascal
16992 || cu->language == language_fortran)
16993 type = init_character_type (objfile, bits, 0, name);
16994 else
16995 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16996 break;
16997 case DW_ATE_unsigned_char:
16998 if (cu->language == language_ada || cu->language == language_m2
16999 || cu->language == language_pascal
17000 || cu->language == language_fortran
17001 || cu->language == language_rust)
17002 type = init_character_type (objfile, bits, 1, name);
17003 else
17004 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17005 break;
17006 case DW_ATE_UTF:
17007 {
17008 if (bits == 16)
17009 type = builtin_type (arch)->builtin_char16;
17010 else if (bits == 32)
17011 type = builtin_type (arch)->builtin_char32;
17012 else
17013 {
17014 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17015 bits);
17016 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17017 }
17018 return set_die_type (die, type, cu);
17019 }
17020 break;
17021
17022 default:
17023 complaint (_("unsupported DW_AT_encoding: '%s'"),
17024 dwarf_type_encoding_name (encoding));
17025 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17026 break;
17027 }
17028
17029 if (name && strcmp (name, "char") == 0)
17030 TYPE_NOSIGN (type) = 1;
17031
17032 maybe_set_alignment (cu, die, type);
17033
17034 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17035
17036 return set_die_type (die, type, cu);
17037 }
17038
17039 /* Parse dwarf attribute if it's a block, reference or constant and put the
17040 resulting value of the attribute into struct bound_prop.
17041 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17042
17043 static int
17044 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17045 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17046 struct type *default_type)
17047 {
17048 struct dwarf2_property_baton *baton;
17049 struct obstack *obstack
17050 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17051
17052 gdb_assert (default_type != NULL);
17053
17054 if (attr == NULL || prop == NULL)
17055 return 0;
17056
17057 if (attr->form_is_block ())
17058 {
17059 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17060 baton->property_type = default_type;
17061 baton->locexpr.per_cu = cu->per_cu;
17062 baton->locexpr.size = DW_BLOCK (attr)->size;
17063 baton->locexpr.data = DW_BLOCK (attr)->data;
17064 switch (attr->name)
17065 {
17066 case DW_AT_string_length:
17067 baton->locexpr.is_reference = true;
17068 break;
17069 default:
17070 baton->locexpr.is_reference = false;
17071 break;
17072 }
17073 prop->data.baton = baton;
17074 prop->kind = PROP_LOCEXPR;
17075 gdb_assert (prop->data.baton != NULL);
17076 }
17077 else if (attr->form_is_ref ())
17078 {
17079 struct dwarf2_cu *target_cu = cu;
17080 struct die_info *target_die;
17081 struct attribute *target_attr;
17082
17083 target_die = follow_die_ref (die, attr, &target_cu);
17084 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17085 if (target_attr == NULL)
17086 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17087 target_cu);
17088 if (target_attr == NULL)
17089 return 0;
17090
17091 switch (target_attr->name)
17092 {
17093 case DW_AT_location:
17094 if (target_attr->form_is_section_offset ())
17095 {
17096 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17097 baton->property_type = die_type (target_die, target_cu);
17098 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17099 prop->data.baton = baton;
17100 prop->kind = PROP_LOCLIST;
17101 gdb_assert (prop->data.baton != NULL);
17102 }
17103 else if (target_attr->form_is_block ())
17104 {
17105 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17106 baton->property_type = die_type (target_die, target_cu);
17107 baton->locexpr.per_cu = cu->per_cu;
17108 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17109 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17110 baton->locexpr.is_reference = true;
17111 prop->data.baton = baton;
17112 prop->kind = PROP_LOCEXPR;
17113 gdb_assert (prop->data.baton != NULL);
17114 }
17115 else
17116 {
17117 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17118 "dynamic property");
17119 return 0;
17120 }
17121 break;
17122 case DW_AT_data_member_location:
17123 {
17124 LONGEST offset;
17125
17126 if (!handle_data_member_location (target_die, target_cu,
17127 &offset))
17128 return 0;
17129
17130 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17131 baton->property_type = read_type_die (target_die->parent,
17132 target_cu);
17133 baton->offset_info.offset = offset;
17134 baton->offset_info.type = die_type (target_die, target_cu);
17135 prop->data.baton = baton;
17136 prop->kind = PROP_ADDR_OFFSET;
17137 break;
17138 }
17139 }
17140 }
17141 else if (attr->form_is_constant ())
17142 {
17143 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17144 prop->kind = PROP_CONST;
17145 }
17146 else
17147 {
17148 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17149 dwarf2_name (die, cu));
17150 return 0;
17151 }
17152
17153 return 1;
17154 }
17155
17156 /* See read.h. */
17157
17158 struct type *
17159 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17160 {
17161 struct objfile *objfile = dwarf2_per_objfile->objfile;
17162 struct type *int_type;
17163
17164 /* Helper macro to examine the various builtin types. */
17165 #define TRY_TYPE(F) \
17166 int_type = (unsigned_p \
17167 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17168 : objfile_type (objfile)->builtin_ ## F); \
17169 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17170 return int_type
17171
17172 TRY_TYPE (char);
17173 TRY_TYPE (short);
17174 TRY_TYPE (int);
17175 TRY_TYPE (long);
17176 TRY_TYPE (long_long);
17177
17178 #undef TRY_TYPE
17179
17180 gdb_assert_not_reached ("unable to find suitable integer type");
17181 }
17182
17183 /* See read.h. */
17184
17185 struct type *
17186 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17187 {
17188 int addr_size = this->addr_size ();
17189 return int_type (addr_size, unsigned_p);
17190 }
17191
17192 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17193 present (which is valid) then compute the default type based on the
17194 compilation units address size. */
17195
17196 static struct type *
17197 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17198 {
17199 struct type *index_type = die_type (die, cu);
17200
17201 /* Dwarf-2 specifications explicitly allows to create subrange types
17202 without specifying a base type.
17203 In that case, the base type must be set to the type of
17204 the lower bound, upper bound or count, in that order, if any of these
17205 three attributes references an object that has a type.
17206 If no base type is found, the Dwarf-2 specifications say that
17207 a signed integer type of size equal to the size of an address should
17208 be used.
17209 For the following C code: `extern char gdb_int [];'
17210 GCC produces an empty range DIE.
17211 FIXME: muller/2010-05-28: Possible references to object for low bound,
17212 high bound or count are not yet handled by this code. */
17213 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17214 index_type = cu->per_cu->addr_sized_int_type (false);
17215
17216 return index_type;
17217 }
17218
17219 /* Read the given DW_AT_subrange DIE. */
17220
17221 static struct type *
17222 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17223 {
17224 struct type *base_type, *orig_base_type;
17225 struct type *range_type;
17226 struct attribute *attr;
17227 struct dynamic_prop low, high;
17228 int low_default_is_valid;
17229 int high_bound_is_count = 0;
17230 const char *name;
17231 ULONGEST negative_mask;
17232
17233 orig_base_type = read_subrange_index_type (die, cu);
17234
17235 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17236 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17237 creating the range type, but we use the result of check_typedef
17238 when examining properties of the type. */
17239 base_type = check_typedef (orig_base_type);
17240
17241 /* The die_type call above may have already set the type for this DIE. */
17242 range_type = get_die_type (die, cu);
17243 if (range_type)
17244 return range_type;
17245
17246 low.kind = PROP_CONST;
17247 high.kind = PROP_CONST;
17248 high.data.const_val = 0;
17249
17250 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17251 omitting DW_AT_lower_bound. */
17252 switch (cu->language)
17253 {
17254 case language_c:
17255 case language_cplus:
17256 low.data.const_val = 0;
17257 low_default_is_valid = 1;
17258 break;
17259 case language_fortran:
17260 low.data.const_val = 1;
17261 low_default_is_valid = 1;
17262 break;
17263 case language_d:
17264 case language_objc:
17265 case language_rust:
17266 low.data.const_val = 0;
17267 low_default_is_valid = (cu->header.version >= 4);
17268 break;
17269 case language_ada:
17270 case language_m2:
17271 case language_pascal:
17272 low.data.const_val = 1;
17273 low_default_is_valid = (cu->header.version >= 4);
17274 break;
17275 default:
17276 low.data.const_val = 0;
17277 low_default_is_valid = 0;
17278 break;
17279 }
17280
17281 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17282 if (attr != nullptr)
17283 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17284 else if (!low_default_is_valid)
17285 complaint (_("Missing DW_AT_lower_bound "
17286 "- DIE at %s [in module %s]"),
17287 sect_offset_str (die->sect_off),
17288 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17289
17290 struct attribute *attr_ub, *attr_count;
17291 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17292 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17293 {
17294 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17295 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17296 {
17297 /* If bounds are constant do the final calculation here. */
17298 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17299 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17300 else
17301 high_bound_is_count = 1;
17302 }
17303 else
17304 {
17305 if (attr_ub != NULL)
17306 complaint (_("Unresolved DW_AT_upper_bound "
17307 "- DIE at %s [in module %s]"),
17308 sect_offset_str (die->sect_off),
17309 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17310 if (attr_count != NULL)
17311 complaint (_("Unresolved DW_AT_count "
17312 "- DIE at %s [in module %s]"),
17313 sect_offset_str (die->sect_off),
17314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17315 }
17316 }
17317
17318 LONGEST bias = 0;
17319 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17320 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17321 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17322
17323 /* Normally, the DWARF producers are expected to use a signed
17324 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17325 But this is unfortunately not always the case, as witnessed
17326 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17327 is used instead. To work around that ambiguity, we treat
17328 the bounds as signed, and thus sign-extend their values, when
17329 the base type is signed. */
17330 negative_mask =
17331 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17332 if (low.kind == PROP_CONST
17333 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17334 low.data.const_val |= negative_mask;
17335 if (high.kind == PROP_CONST
17336 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17337 high.data.const_val |= negative_mask;
17338
17339 /* Check for bit and byte strides. */
17340 struct dynamic_prop byte_stride_prop;
17341 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17342 if (attr_byte_stride != nullptr)
17343 {
17344 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17345 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17346 prop_type);
17347 }
17348
17349 struct dynamic_prop bit_stride_prop;
17350 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17351 if (attr_bit_stride != nullptr)
17352 {
17353 /* It only makes sense to have either a bit or byte stride. */
17354 if (attr_byte_stride != nullptr)
17355 {
17356 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17357 "- DIE at %s [in module %s]"),
17358 sect_offset_str (die->sect_off),
17359 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17360 attr_bit_stride = nullptr;
17361 }
17362 else
17363 {
17364 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17365 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17366 prop_type);
17367 }
17368 }
17369
17370 if (attr_byte_stride != nullptr
17371 || attr_bit_stride != nullptr)
17372 {
17373 bool byte_stride_p = (attr_byte_stride != nullptr);
17374 struct dynamic_prop *stride
17375 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17376
17377 range_type
17378 = create_range_type_with_stride (NULL, orig_base_type, &low,
17379 &high, bias, stride, byte_stride_p);
17380 }
17381 else
17382 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17383
17384 if (high_bound_is_count)
17385 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17386
17387 /* Ada expects an empty array on no boundary attributes. */
17388 if (attr == NULL && cu->language != language_ada)
17389 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17390
17391 name = dwarf2_name (die, cu);
17392 if (name)
17393 TYPE_NAME (range_type) = name;
17394
17395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17396 if (attr != nullptr)
17397 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17398
17399 maybe_set_alignment (cu, die, range_type);
17400
17401 set_die_type (die, range_type, cu);
17402
17403 /* set_die_type should be already done. */
17404 set_descriptive_type (range_type, die, cu);
17405
17406 return range_type;
17407 }
17408
17409 static struct type *
17410 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17411 {
17412 struct type *type;
17413
17414 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17415 NULL);
17416 TYPE_NAME (type) = dwarf2_name (die, cu);
17417
17418 /* In Ada, an unspecified type is typically used when the description
17419 of the type is deferred to a different unit. When encountering
17420 such a type, we treat it as a stub, and try to resolve it later on,
17421 when needed. */
17422 if (cu->language == language_ada)
17423 TYPE_STUB (type) = 1;
17424
17425 return set_die_type (die, type, cu);
17426 }
17427
17428 /* Read a single die and all its descendents. Set the die's sibling
17429 field to NULL; set other fields in the die correctly, and set all
17430 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17431 location of the info_ptr after reading all of those dies. PARENT
17432 is the parent of the die in question. */
17433
17434 static struct die_info *
17435 read_die_and_children (const struct die_reader_specs *reader,
17436 const gdb_byte *info_ptr,
17437 const gdb_byte **new_info_ptr,
17438 struct die_info *parent)
17439 {
17440 struct die_info *die;
17441 const gdb_byte *cur_ptr;
17442
17443 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17444 if (die == NULL)
17445 {
17446 *new_info_ptr = cur_ptr;
17447 return NULL;
17448 }
17449 store_in_ref_table (die, reader->cu);
17450
17451 if (die->has_children)
17452 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17453 else
17454 {
17455 die->child = NULL;
17456 *new_info_ptr = cur_ptr;
17457 }
17458
17459 die->sibling = NULL;
17460 die->parent = parent;
17461 return die;
17462 }
17463
17464 /* Read a die, all of its descendents, and all of its siblings; set
17465 all of the fields of all of the dies correctly. Arguments are as
17466 in read_die_and_children. */
17467
17468 static struct die_info *
17469 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17470 const gdb_byte *info_ptr,
17471 const gdb_byte **new_info_ptr,
17472 struct die_info *parent)
17473 {
17474 struct die_info *first_die, *last_sibling;
17475 const gdb_byte *cur_ptr;
17476
17477 cur_ptr = info_ptr;
17478 first_die = last_sibling = NULL;
17479
17480 while (1)
17481 {
17482 struct die_info *die
17483 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17484
17485 if (die == NULL)
17486 {
17487 *new_info_ptr = cur_ptr;
17488 return first_die;
17489 }
17490
17491 if (!first_die)
17492 first_die = die;
17493 else
17494 last_sibling->sibling = die;
17495
17496 last_sibling = die;
17497 }
17498 }
17499
17500 /* Read a die, all of its descendents, and all of its siblings; set
17501 all of the fields of all of the dies correctly. Arguments are as
17502 in read_die_and_children.
17503 This the main entry point for reading a DIE and all its children. */
17504
17505 static struct die_info *
17506 read_die_and_siblings (const struct die_reader_specs *reader,
17507 const gdb_byte *info_ptr,
17508 const gdb_byte **new_info_ptr,
17509 struct die_info *parent)
17510 {
17511 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17512 new_info_ptr, parent);
17513
17514 if (dwarf_die_debug)
17515 {
17516 fprintf_unfiltered (gdb_stdlog,
17517 "Read die from %s@0x%x of %s:\n",
17518 reader->die_section->get_name (),
17519 (unsigned) (info_ptr - reader->die_section->buffer),
17520 bfd_get_filename (reader->abfd));
17521 dump_die (die, dwarf_die_debug);
17522 }
17523
17524 return die;
17525 }
17526
17527 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17528 attributes.
17529 The caller is responsible for filling in the extra attributes
17530 and updating (*DIEP)->num_attrs.
17531 Set DIEP to point to a newly allocated die with its information,
17532 except for its child, sibling, and parent fields. */
17533
17534 static const gdb_byte *
17535 read_full_die_1 (const struct die_reader_specs *reader,
17536 struct die_info **diep, const gdb_byte *info_ptr,
17537 int num_extra_attrs)
17538 {
17539 unsigned int abbrev_number, bytes_read, i;
17540 struct abbrev_info *abbrev;
17541 struct die_info *die;
17542 struct dwarf2_cu *cu = reader->cu;
17543 bfd *abfd = reader->abfd;
17544
17545 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17546 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17547 info_ptr += bytes_read;
17548 if (!abbrev_number)
17549 {
17550 *diep = NULL;
17551 return info_ptr;
17552 }
17553
17554 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17555 if (!abbrev)
17556 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17557 abbrev_number,
17558 bfd_get_filename (abfd));
17559
17560 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17561 die->sect_off = sect_off;
17562 die->tag = abbrev->tag;
17563 die->abbrev = abbrev_number;
17564 die->has_children = abbrev->has_children;
17565
17566 /* Make the result usable.
17567 The caller needs to update num_attrs after adding the extra
17568 attributes. */
17569 die->num_attrs = abbrev->num_attrs;
17570
17571 std::vector<int> indexes_that_need_reprocess;
17572 for (i = 0; i < abbrev->num_attrs; ++i)
17573 {
17574 bool need_reprocess;
17575 info_ptr =
17576 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17577 info_ptr, &need_reprocess);
17578 if (need_reprocess)
17579 indexes_that_need_reprocess.push_back (i);
17580 }
17581
17582 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17583 if (attr != nullptr)
17584 cu->str_offsets_base = DW_UNSND (attr);
17585
17586 auto maybe_addr_base = lookup_addr_base(die);
17587 if (maybe_addr_base.has_value ())
17588 cu->addr_base = *maybe_addr_base;
17589 for (int index : indexes_that_need_reprocess)
17590 read_attribute_reprocess (reader, &die->attrs[index]);
17591 *diep = die;
17592 return info_ptr;
17593 }
17594
17595 /* Read a die and all its attributes.
17596 Set DIEP to point to a newly allocated die with its information,
17597 except for its child, sibling, and parent fields. */
17598
17599 static const gdb_byte *
17600 read_full_die (const struct die_reader_specs *reader,
17601 struct die_info **diep, const gdb_byte *info_ptr)
17602 {
17603 const gdb_byte *result;
17604
17605 result = read_full_die_1 (reader, diep, info_ptr, 0);
17606
17607 if (dwarf_die_debug)
17608 {
17609 fprintf_unfiltered (gdb_stdlog,
17610 "Read die from %s@0x%x of %s:\n",
17611 reader->die_section->get_name (),
17612 (unsigned) (info_ptr - reader->die_section->buffer),
17613 bfd_get_filename (reader->abfd));
17614 dump_die (*diep, dwarf_die_debug);
17615 }
17616
17617 return result;
17618 }
17619 \f
17620
17621 /* Returns nonzero if TAG represents a type that we might generate a partial
17622 symbol for. */
17623
17624 static int
17625 is_type_tag_for_partial (int tag)
17626 {
17627 switch (tag)
17628 {
17629 #if 0
17630 /* Some types that would be reasonable to generate partial symbols for,
17631 that we don't at present. */
17632 case DW_TAG_array_type:
17633 case DW_TAG_file_type:
17634 case DW_TAG_ptr_to_member_type:
17635 case DW_TAG_set_type:
17636 case DW_TAG_string_type:
17637 case DW_TAG_subroutine_type:
17638 #endif
17639 case DW_TAG_base_type:
17640 case DW_TAG_class_type:
17641 case DW_TAG_interface_type:
17642 case DW_TAG_enumeration_type:
17643 case DW_TAG_structure_type:
17644 case DW_TAG_subrange_type:
17645 case DW_TAG_typedef:
17646 case DW_TAG_union_type:
17647 return 1;
17648 default:
17649 return 0;
17650 }
17651 }
17652
17653 /* Load all DIEs that are interesting for partial symbols into memory. */
17654
17655 static struct partial_die_info *
17656 load_partial_dies (const struct die_reader_specs *reader,
17657 const gdb_byte *info_ptr, int building_psymtab)
17658 {
17659 struct dwarf2_cu *cu = reader->cu;
17660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17661 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17662 unsigned int bytes_read;
17663 unsigned int load_all = 0;
17664 int nesting_level = 1;
17665
17666 parent_die = NULL;
17667 last_die = NULL;
17668
17669 gdb_assert (cu->per_cu != NULL);
17670 if (cu->per_cu->load_all_dies)
17671 load_all = 1;
17672
17673 cu->partial_dies
17674 = htab_create_alloc_ex (cu->header.length / 12,
17675 partial_die_hash,
17676 partial_die_eq,
17677 NULL,
17678 &cu->comp_unit_obstack,
17679 hashtab_obstack_allocate,
17680 dummy_obstack_deallocate);
17681
17682 while (1)
17683 {
17684 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17685
17686 /* A NULL abbrev means the end of a series of children. */
17687 if (abbrev == NULL)
17688 {
17689 if (--nesting_level == 0)
17690 return first_die;
17691
17692 info_ptr += bytes_read;
17693 last_die = parent_die;
17694 parent_die = parent_die->die_parent;
17695 continue;
17696 }
17697
17698 /* Check for template arguments. We never save these; if
17699 they're seen, we just mark the parent, and go on our way. */
17700 if (parent_die != NULL
17701 && cu->language == language_cplus
17702 && (abbrev->tag == DW_TAG_template_type_param
17703 || abbrev->tag == DW_TAG_template_value_param))
17704 {
17705 parent_die->has_template_arguments = 1;
17706
17707 if (!load_all)
17708 {
17709 /* We don't need a partial DIE for the template argument. */
17710 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17711 continue;
17712 }
17713 }
17714
17715 /* We only recurse into c++ subprograms looking for template arguments.
17716 Skip their other children. */
17717 if (!load_all
17718 && cu->language == language_cplus
17719 && parent_die != NULL
17720 && parent_die->tag == DW_TAG_subprogram)
17721 {
17722 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17723 continue;
17724 }
17725
17726 /* Check whether this DIE is interesting enough to save. Normally
17727 we would not be interested in members here, but there may be
17728 later variables referencing them via DW_AT_specification (for
17729 static members). */
17730 if (!load_all
17731 && !is_type_tag_for_partial (abbrev->tag)
17732 && abbrev->tag != DW_TAG_constant
17733 && abbrev->tag != DW_TAG_enumerator
17734 && abbrev->tag != DW_TAG_subprogram
17735 && abbrev->tag != DW_TAG_inlined_subroutine
17736 && abbrev->tag != DW_TAG_lexical_block
17737 && abbrev->tag != DW_TAG_variable
17738 && abbrev->tag != DW_TAG_namespace
17739 && abbrev->tag != DW_TAG_module
17740 && abbrev->tag != DW_TAG_member
17741 && abbrev->tag != DW_TAG_imported_unit
17742 && abbrev->tag != DW_TAG_imported_declaration)
17743 {
17744 /* Otherwise we skip to the next sibling, if any. */
17745 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17746 continue;
17747 }
17748
17749 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17750 abbrev);
17751
17752 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17753
17754 /* This two-pass algorithm for processing partial symbols has a
17755 high cost in cache pressure. Thus, handle some simple cases
17756 here which cover the majority of C partial symbols. DIEs
17757 which neither have specification tags in them, nor could have
17758 specification tags elsewhere pointing at them, can simply be
17759 processed and discarded.
17760
17761 This segment is also optional; scan_partial_symbols and
17762 add_partial_symbol will handle these DIEs if we chain
17763 them in normally. When compilers which do not emit large
17764 quantities of duplicate debug information are more common,
17765 this code can probably be removed. */
17766
17767 /* Any complete simple types at the top level (pretty much all
17768 of them, for a language without namespaces), can be processed
17769 directly. */
17770 if (parent_die == NULL
17771 && pdi.has_specification == 0
17772 && pdi.is_declaration == 0
17773 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17774 || pdi.tag == DW_TAG_base_type
17775 || pdi.tag == DW_TAG_subrange_type))
17776 {
17777 if (building_psymtab && pdi.name != NULL)
17778 add_psymbol_to_list (pdi.name, false,
17779 VAR_DOMAIN, LOC_TYPEDEF, -1,
17780 psymbol_placement::STATIC,
17781 0, cu->language, objfile);
17782 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17783 continue;
17784 }
17785
17786 /* The exception for DW_TAG_typedef with has_children above is
17787 a workaround of GCC PR debug/47510. In the case of this complaint
17788 type_name_or_error will error on such types later.
17789
17790 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17791 it could not find the child DIEs referenced later, this is checked
17792 above. In correct DWARF DW_TAG_typedef should have no children. */
17793
17794 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17795 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17796 "- DIE at %s [in module %s]"),
17797 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17798
17799 /* If we're at the second level, and we're an enumerator, and
17800 our parent has no specification (meaning possibly lives in a
17801 namespace elsewhere), then we can add the partial symbol now
17802 instead of queueing it. */
17803 if (pdi.tag == DW_TAG_enumerator
17804 && parent_die != NULL
17805 && parent_die->die_parent == NULL
17806 && parent_die->tag == DW_TAG_enumeration_type
17807 && parent_die->has_specification == 0)
17808 {
17809 if (pdi.name == NULL)
17810 complaint (_("malformed enumerator DIE ignored"));
17811 else if (building_psymtab)
17812 add_psymbol_to_list (pdi.name, false,
17813 VAR_DOMAIN, LOC_CONST, -1,
17814 cu->language == language_cplus
17815 ? psymbol_placement::GLOBAL
17816 : psymbol_placement::STATIC,
17817 0, cu->language, objfile);
17818
17819 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17820 continue;
17821 }
17822
17823 struct partial_die_info *part_die
17824 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17825
17826 /* We'll save this DIE so link it in. */
17827 part_die->die_parent = parent_die;
17828 part_die->die_sibling = NULL;
17829 part_die->die_child = NULL;
17830
17831 if (last_die && last_die == parent_die)
17832 last_die->die_child = part_die;
17833 else if (last_die)
17834 last_die->die_sibling = part_die;
17835
17836 last_die = part_die;
17837
17838 if (first_die == NULL)
17839 first_die = part_die;
17840
17841 /* Maybe add the DIE to the hash table. Not all DIEs that we
17842 find interesting need to be in the hash table, because we
17843 also have the parent/sibling/child chains; only those that we
17844 might refer to by offset later during partial symbol reading.
17845
17846 For now this means things that might have be the target of a
17847 DW_AT_specification, DW_AT_abstract_origin, or
17848 DW_AT_extension. DW_AT_extension will refer only to
17849 namespaces; DW_AT_abstract_origin refers to functions (and
17850 many things under the function DIE, but we do not recurse
17851 into function DIEs during partial symbol reading) and
17852 possibly variables as well; DW_AT_specification refers to
17853 declarations. Declarations ought to have the DW_AT_declaration
17854 flag. It happens that GCC forgets to put it in sometimes, but
17855 only for functions, not for types.
17856
17857 Adding more things than necessary to the hash table is harmless
17858 except for the performance cost. Adding too few will result in
17859 wasted time in find_partial_die, when we reread the compilation
17860 unit with load_all_dies set. */
17861
17862 if (load_all
17863 || abbrev->tag == DW_TAG_constant
17864 || abbrev->tag == DW_TAG_subprogram
17865 || abbrev->tag == DW_TAG_variable
17866 || abbrev->tag == DW_TAG_namespace
17867 || part_die->is_declaration)
17868 {
17869 void **slot;
17870
17871 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17872 to_underlying (part_die->sect_off),
17873 INSERT);
17874 *slot = part_die;
17875 }
17876
17877 /* For some DIEs we want to follow their children (if any). For C
17878 we have no reason to follow the children of structures; for other
17879 languages we have to, so that we can get at method physnames
17880 to infer fully qualified class names, for DW_AT_specification,
17881 and for C++ template arguments. For C++, we also look one level
17882 inside functions to find template arguments (if the name of the
17883 function does not already contain the template arguments).
17884
17885 For Ada and Fortran, we need to scan the children of subprograms
17886 and lexical blocks as well because these languages allow the
17887 definition of nested entities that could be interesting for the
17888 debugger, such as nested subprograms for instance. */
17889 if (last_die->has_children
17890 && (load_all
17891 || last_die->tag == DW_TAG_namespace
17892 || last_die->tag == DW_TAG_module
17893 || last_die->tag == DW_TAG_enumeration_type
17894 || (cu->language == language_cplus
17895 && last_die->tag == DW_TAG_subprogram
17896 && (last_die->name == NULL
17897 || strchr (last_die->name, '<') == NULL))
17898 || (cu->language != language_c
17899 && (last_die->tag == DW_TAG_class_type
17900 || last_die->tag == DW_TAG_interface_type
17901 || last_die->tag == DW_TAG_structure_type
17902 || last_die->tag == DW_TAG_union_type))
17903 || ((cu->language == language_ada
17904 || cu->language == language_fortran)
17905 && (last_die->tag == DW_TAG_subprogram
17906 || last_die->tag == DW_TAG_lexical_block))))
17907 {
17908 nesting_level++;
17909 parent_die = last_die;
17910 continue;
17911 }
17912
17913 /* Otherwise we skip to the next sibling, if any. */
17914 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17915
17916 /* Back to the top, do it again. */
17917 }
17918 }
17919
17920 partial_die_info::partial_die_info (sect_offset sect_off_,
17921 struct abbrev_info *abbrev)
17922 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17923 {
17924 }
17925
17926 /* Read a minimal amount of information into the minimal die structure.
17927 INFO_PTR should point just after the initial uleb128 of a DIE. */
17928
17929 const gdb_byte *
17930 partial_die_info::read (const struct die_reader_specs *reader,
17931 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17932 {
17933 struct dwarf2_cu *cu = reader->cu;
17934 struct dwarf2_per_objfile *dwarf2_per_objfile
17935 = cu->per_cu->dwarf2_per_objfile;
17936 unsigned int i;
17937 int has_low_pc_attr = 0;
17938 int has_high_pc_attr = 0;
17939 int high_pc_relative = 0;
17940
17941 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17942 for (i = 0; i < abbrev.num_attrs; ++i)
17943 {
17944 bool need_reprocess;
17945 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17946 info_ptr, &need_reprocess);
17947 /* String and address offsets that need to do the reprocessing have
17948 already been read at this point, so there is no need to wait until
17949 the loop terminates to do the reprocessing. */
17950 if (need_reprocess)
17951 read_attribute_reprocess (reader, &attr_vec[i]);
17952 attribute &attr = attr_vec[i];
17953 /* Store the data if it is of an attribute we want to keep in a
17954 partial symbol table. */
17955 switch (attr.name)
17956 {
17957 case DW_AT_name:
17958 switch (tag)
17959 {
17960 case DW_TAG_compile_unit:
17961 case DW_TAG_partial_unit:
17962 case DW_TAG_type_unit:
17963 /* Compilation units have a DW_AT_name that is a filename, not
17964 a source language identifier. */
17965 case DW_TAG_enumeration_type:
17966 case DW_TAG_enumerator:
17967 /* These tags always have simple identifiers already; no need
17968 to canonicalize them. */
17969 name = DW_STRING (&attr);
17970 break;
17971 default:
17972 {
17973 struct objfile *objfile = dwarf2_per_objfile->objfile;
17974
17975 name
17976 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17977 &objfile->per_bfd->storage_obstack);
17978 }
17979 break;
17980 }
17981 break;
17982 case DW_AT_linkage_name:
17983 case DW_AT_MIPS_linkage_name:
17984 /* Note that both forms of linkage name might appear. We
17985 assume they will be the same, and we only store the last
17986 one we see. */
17987 linkage_name = DW_STRING (&attr);
17988 break;
17989 case DW_AT_low_pc:
17990 has_low_pc_attr = 1;
17991 lowpc = attr.value_as_address ();
17992 break;
17993 case DW_AT_high_pc:
17994 has_high_pc_attr = 1;
17995 highpc = attr.value_as_address ();
17996 if (cu->header.version >= 4 && attr.form_is_constant ())
17997 high_pc_relative = 1;
17998 break;
17999 case DW_AT_location:
18000 /* Support the .debug_loc offsets. */
18001 if (attr.form_is_block ())
18002 {
18003 d.locdesc = DW_BLOCK (&attr);
18004 }
18005 else if (attr.form_is_section_offset ())
18006 {
18007 dwarf2_complex_location_expr_complaint ();
18008 }
18009 else
18010 {
18011 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18012 "partial symbol information");
18013 }
18014 break;
18015 case DW_AT_external:
18016 is_external = DW_UNSND (&attr);
18017 break;
18018 case DW_AT_declaration:
18019 is_declaration = DW_UNSND (&attr);
18020 break;
18021 case DW_AT_type:
18022 has_type = 1;
18023 break;
18024 case DW_AT_abstract_origin:
18025 case DW_AT_specification:
18026 case DW_AT_extension:
18027 has_specification = 1;
18028 spec_offset = dwarf2_get_ref_die_offset (&attr);
18029 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18030 || cu->per_cu->is_dwz);
18031 break;
18032 case DW_AT_sibling:
18033 /* Ignore absolute siblings, they might point outside of
18034 the current compile unit. */
18035 if (attr.form == DW_FORM_ref_addr)
18036 complaint (_("ignoring absolute DW_AT_sibling"));
18037 else
18038 {
18039 const gdb_byte *buffer = reader->buffer;
18040 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18041 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18042
18043 if (sibling_ptr < info_ptr)
18044 complaint (_("DW_AT_sibling points backwards"));
18045 else if (sibling_ptr > reader->buffer_end)
18046 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18047 else
18048 sibling = sibling_ptr;
18049 }
18050 break;
18051 case DW_AT_byte_size:
18052 has_byte_size = 1;
18053 break;
18054 case DW_AT_const_value:
18055 has_const_value = 1;
18056 break;
18057 case DW_AT_calling_convention:
18058 /* DWARF doesn't provide a way to identify a program's source-level
18059 entry point. DW_AT_calling_convention attributes are only meant
18060 to describe functions' calling conventions.
18061
18062 However, because it's a necessary piece of information in
18063 Fortran, and before DWARF 4 DW_CC_program was the only
18064 piece of debugging information whose definition refers to
18065 a 'main program' at all, several compilers marked Fortran
18066 main programs with DW_CC_program --- even when those
18067 functions use the standard calling conventions.
18068
18069 Although DWARF now specifies a way to provide this
18070 information, we support this practice for backward
18071 compatibility. */
18072 if (DW_UNSND (&attr) == DW_CC_program
18073 && cu->language == language_fortran)
18074 main_subprogram = 1;
18075 break;
18076 case DW_AT_inline:
18077 if (DW_UNSND (&attr) == DW_INL_inlined
18078 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18079 may_be_inlined = 1;
18080 break;
18081
18082 case DW_AT_import:
18083 if (tag == DW_TAG_imported_unit)
18084 {
18085 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18086 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18087 || cu->per_cu->is_dwz);
18088 }
18089 break;
18090
18091 case DW_AT_main_subprogram:
18092 main_subprogram = DW_UNSND (&attr);
18093 break;
18094
18095 case DW_AT_ranges:
18096 {
18097 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18098 but that requires a full DIE, so instead we just
18099 reimplement it. */
18100 int need_ranges_base = tag != DW_TAG_compile_unit;
18101 unsigned int ranges_offset = (DW_UNSND (&attr)
18102 + (need_ranges_base
18103 ? cu->ranges_base
18104 : 0));
18105
18106 /* Value of the DW_AT_ranges attribute is the offset in the
18107 .debug_ranges section. */
18108 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18109 nullptr))
18110 has_pc_info = 1;
18111 }
18112 break;
18113
18114 default:
18115 break;
18116 }
18117 }
18118
18119 /* For Ada, if both the name and the linkage name appear, we prefer
18120 the latter. This lets "catch exception" work better, regardless
18121 of the order in which the name and linkage name were emitted.
18122 Really, though, this is just a workaround for the fact that gdb
18123 doesn't store both the name and the linkage name. */
18124 if (cu->language == language_ada && linkage_name != nullptr)
18125 name = linkage_name;
18126
18127 if (high_pc_relative)
18128 highpc += lowpc;
18129
18130 if (has_low_pc_attr && has_high_pc_attr)
18131 {
18132 /* When using the GNU linker, .gnu.linkonce. sections are used to
18133 eliminate duplicate copies of functions and vtables and such.
18134 The linker will arbitrarily choose one and discard the others.
18135 The AT_*_pc values for such functions refer to local labels in
18136 these sections. If the section from that file was discarded, the
18137 labels are not in the output, so the relocs get a value of 0.
18138 If this is a discarded function, mark the pc bounds as invalid,
18139 so that GDB will ignore it. */
18140 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18141 {
18142 struct objfile *objfile = dwarf2_per_objfile->objfile;
18143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18144
18145 complaint (_("DW_AT_low_pc %s is zero "
18146 "for DIE at %s [in module %s]"),
18147 paddress (gdbarch, lowpc),
18148 sect_offset_str (sect_off),
18149 objfile_name (objfile));
18150 }
18151 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18152 else if (lowpc >= highpc)
18153 {
18154 struct objfile *objfile = dwarf2_per_objfile->objfile;
18155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18156
18157 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18158 "for DIE at %s [in module %s]"),
18159 paddress (gdbarch, lowpc),
18160 paddress (gdbarch, highpc),
18161 sect_offset_str (sect_off),
18162 objfile_name (objfile));
18163 }
18164 else
18165 has_pc_info = 1;
18166 }
18167
18168 return info_ptr;
18169 }
18170
18171 /* Find a cached partial DIE at OFFSET in CU. */
18172
18173 struct partial_die_info *
18174 dwarf2_cu::find_partial_die (sect_offset sect_off)
18175 {
18176 struct partial_die_info *lookup_die = NULL;
18177 struct partial_die_info part_die (sect_off);
18178
18179 lookup_die = ((struct partial_die_info *)
18180 htab_find_with_hash (partial_dies, &part_die,
18181 to_underlying (sect_off)));
18182
18183 return lookup_die;
18184 }
18185
18186 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18187 except in the case of .debug_types DIEs which do not reference
18188 outside their CU (they do however referencing other types via
18189 DW_FORM_ref_sig8). */
18190
18191 static const struct cu_partial_die_info
18192 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18193 {
18194 struct dwarf2_per_objfile *dwarf2_per_objfile
18195 = cu->per_cu->dwarf2_per_objfile;
18196 struct objfile *objfile = dwarf2_per_objfile->objfile;
18197 struct dwarf2_per_cu_data *per_cu = NULL;
18198 struct partial_die_info *pd = NULL;
18199
18200 if (offset_in_dwz == cu->per_cu->is_dwz
18201 && cu->header.offset_in_cu_p (sect_off))
18202 {
18203 pd = cu->find_partial_die (sect_off);
18204 if (pd != NULL)
18205 return { cu, pd };
18206 /* We missed recording what we needed.
18207 Load all dies and try again. */
18208 per_cu = cu->per_cu;
18209 }
18210 else
18211 {
18212 /* TUs don't reference other CUs/TUs (except via type signatures). */
18213 if (cu->per_cu->is_debug_types)
18214 {
18215 error (_("Dwarf Error: Type Unit at offset %s contains"
18216 " external reference to offset %s [in module %s].\n"),
18217 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18218 bfd_get_filename (objfile->obfd));
18219 }
18220 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18221 dwarf2_per_objfile);
18222
18223 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18224 load_partial_comp_unit (per_cu);
18225
18226 per_cu->cu->last_used = 0;
18227 pd = per_cu->cu->find_partial_die (sect_off);
18228 }
18229
18230 /* If we didn't find it, and not all dies have been loaded,
18231 load them all and try again. */
18232
18233 if (pd == NULL && per_cu->load_all_dies == 0)
18234 {
18235 per_cu->load_all_dies = 1;
18236
18237 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18238 THIS_CU->cu may already be in use. So we can't just free it and
18239 replace its DIEs with the ones we read in. Instead, we leave those
18240 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18241 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18242 set. */
18243 load_partial_comp_unit (per_cu);
18244
18245 pd = per_cu->cu->find_partial_die (sect_off);
18246 }
18247
18248 if (pd == NULL)
18249 internal_error (__FILE__, __LINE__,
18250 _("could not find partial DIE %s "
18251 "in cache [from module %s]\n"),
18252 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18253 return { per_cu->cu, pd };
18254 }
18255
18256 /* See if we can figure out if the class lives in a namespace. We do
18257 this by looking for a member function; its demangled name will
18258 contain namespace info, if there is any. */
18259
18260 static void
18261 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18262 struct dwarf2_cu *cu)
18263 {
18264 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18265 what template types look like, because the demangler
18266 frequently doesn't give the same name as the debug info. We
18267 could fix this by only using the demangled name to get the
18268 prefix (but see comment in read_structure_type). */
18269
18270 struct partial_die_info *real_pdi;
18271 struct partial_die_info *child_pdi;
18272
18273 /* If this DIE (this DIE's specification, if any) has a parent, then
18274 we should not do this. We'll prepend the parent's fully qualified
18275 name when we create the partial symbol. */
18276
18277 real_pdi = struct_pdi;
18278 while (real_pdi->has_specification)
18279 {
18280 auto res = find_partial_die (real_pdi->spec_offset,
18281 real_pdi->spec_is_dwz, cu);
18282 real_pdi = res.pdi;
18283 cu = res.cu;
18284 }
18285
18286 if (real_pdi->die_parent != NULL)
18287 return;
18288
18289 for (child_pdi = struct_pdi->die_child;
18290 child_pdi != NULL;
18291 child_pdi = child_pdi->die_sibling)
18292 {
18293 if (child_pdi->tag == DW_TAG_subprogram
18294 && child_pdi->linkage_name != NULL)
18295 {
18296 gdb::unique_xmalloc_ptr<char> actual_class_name
18297 (language_class_name_from_physname (cu->language_defn,
18298 child_pdi->linkage_name));
18299 if (actual_class_name != NULL)
18300 {
18301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18302 struct_pdi->name
18303 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18304 actual_class_name.get ());
18305 }
18306 break;
18307 }
18308 }
18309 }
18310
18311 void
18312 partial_die_info::fixup (struct dwarf2_cu *cu)
18313 {
18314 /* Once we've fixed up a die, there's no point in doing so again.
18315 This also avoids a memory leak if we were to call
18316 guess_partial_die_structure_name multiple times. */
18317 if (fixup_called)
18318 return;
18319
18320 /* If we found a reference attribute and the DIE has no name, try
18321 to find a name in the referred to DIE. */
18322
18323 if (name == NULL && has_specification)
18324 {
18325 struct partial_die_info *spec_die;
18326
18327 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18328 spec_die = res.pdi;
18329 cu = res.cu;
18330
18331 spec_die->fixup (cu);
18332
18333 if (spec_die->name)
18334 {
18335 name = spec_die->name;
18336
18337 /* Copy DW_AT_external attribute if it is set. */
18338 if (spec_die->is_external)
18339 is_external = spec_die->is_external;
18340 }
18341 }
18342
18343 /* Set default names for some unnamed DIEs. */
18344
18345 if (name == NULL && tag == DW_TAG_namespace)
18346 name = CP_ANONYMOUS_NAMESPACE_STR;
18347
18348 /* If there is no parent die to provide a namespace, and there are
18349 children, see if we can determine the namespace from their linkage
18350 name. */
18351 if (cu->language == language_cplus
18352 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18353 && die_parent == NULL
18354 && has_children
18355 && (tag == DW_TAG_class_type
18356 || tag == DW_TAG_structure_type
18357 || tag == DW_TAG_union_type))
18358 guess_partial_die_structure_name (this, cu);
18359
18360 /* GCC might emit a nameless struct or union that has a linkage
18361 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18362 if (name == NULL
18363 && (tag == DW_TAG_class_type
18364 || tag == DW_TAG_interface_type
18365 || tag == DW_TAG_structure_type
18366 || tag == DW_TAG_union_type)
18367 && linkage_name != NULL)
18368 {
18369 gdb::unique_xmalloc_ptr<char> demangled
18370 (gdb_demangle (linkage_name, DMGL_TYPES));
18371 if (demangled != nullptr)
18372 {
18373 const char *base;
18374
18375 /* Strip any leading namespaces/classes, keep only the base name.
18376 DW_AT_name for named DIEs does not contain the prefixes. */
18377 base = strrchr (demangled.get (), ':');
18378 if (base && base > demangled.get () && base[-1] == ':')
18379 base++;
18380 else
18381 base = demangled.get ();
18382
18383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18384 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18385 }
18386 }
18387
18388 fixup_called = 1;
18389 }
18390
18391 /* Process the attributes that had to be skipped in the first round. These
18392 attributes are the ones that need str_offsets_base or addr_base attributes.
18393 They could not have been processed in the first round, because at the time
18394 the values of str_offsets_base or addr_base may not have been known. */
18395 void read_attribute_reprocess (const struct die_reader_specs *reader,
18396 struct attribute *attr)
18397 {
18398 struct dwarf2_cu *cu = reader->cu;
18399 switch (attr->form)
18400 {
18401 case DW_FORM_addrx:
18402 case DW_FORM_GNU_addr_index:
18403 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18404 break;
18405 case DW_FORM_strx:
18406 case DW_FORM_strx1:
18407 case DW_FORM_strx2:
18408 case DW_FORM_strx3:
18409 case DW_FORM_strx4:
18410 case DW_FORM_GNU_str_index:
18411 {
18412 unsigned int str_index = DW_UNSND (attr);
18413 if (reader->dwo_file != NULL)
18414 {
18415 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18416 DW_STRING_IS_CANONICAL (attr) = 0;
18417 }
18418 else
18419 {
18420 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18421 DW_STRING_IS_CANONICAL (attr) = 0;
18422 }
18423 break;
18424 }
18425 default:
18426 gdb_assert_not_reached (_("Unexpected DWARF form."));
18427 }
18428 }
18429
18430 /* Read an attribute value described by an attribute form. */
18431
18432 static const gdb_byte *
18433 read_attribute_value (const struct die_reader_specs *reader,
18434 struct attribute *attr, unsigned form,
18435 LONGEST implicit_const, const gdb_byte *info_ptr,
18436 bool *need_reprocess)
18437 {
18438 struct dwarf2_cu *cu = reader->cu;
18439 struct dwarf2_per_objfile *dwarf2_per_objfile
18440 = cu->per_cu->dwarf2_per_objfile;
18441 struct objfile *objfile = dwarf2_per_objfile->objfile;
18442 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18443 bfd *abfd = reader->abfd;
18444 struct comp_unit_head *cu_header = &cu->header;
18445 unsigned int bytes_read;
18446 struct dwarf_block *blk;
18447 *need_reprocess = false;
18448
18449 attr->form = (enum dwarf_form) form;
18450 switch (form)
18451 {
18452 case DW_FORM_ref_addr:
18453 if (cu->header.version == 2)
18454 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18455 &bytes_read);
18456 else
18457 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18458 &bytes_read);
18459 info_ptr += bytes_read;
18460 break;
18461 case DW_FORM_GNU_ref_alt:
18462 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18463 info_ptr += bytes_read;
18464 break;
18465 case DW_FORM_addr:
18466 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18467 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18468 info_ptr += bytes_read;
18469 break;
18470 case DW_FORM_block2:
18471 blk = dwarf_alloc_block (cu);
18472 blk->size = read_2_bytes (abfd, info_ptr);
18473 info_ptr += 2;
18474 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18475 info_ptr += blk->size;
18476 DW_BLOCK (attr) = blk;
18477 break;
18478 case DW_FORM_block4:
18479 blk = dwarf_alloc_block (cu);
18480 blk->size = read_4_bytes (abfd, info_ptr);
18481 info_ptr += 4;
18482 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18483 info_ptr += blk->size;
18484 DW_BLOCK (attr) = blk;
18485 break;
18486 case DW_FORM_data2:
18487 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18488 info_ptr += 2;
18489 break;
18490 case DW_FORM_data4:
18491 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18492 info_ptr += 4;
18493 break;
18494 case DW_FORM_data8:
18495 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18496 info_ptr += 8;
18497 break;
18498 case DW_FORM_data16:
18499 blk = dwarf_alloc_block (cu);
18500 blk->size = 16;
18501 blk->data = read_n_bytes (abfd, info_ptr, 16);
18502 info_ptr += 16;
18503 DW_BLOCK (attr) = blk;
18504 break;
18505 case DW_FORM_sec_offset:
18506 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18507 info_ptr += bytes_read;
18508 break;
18509 case DW_FORM_string:
18510 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18511 DW_STRING_IS_CANONICAL (attr) = 0;
18512 info_ptr += bytes_read;
18513 break;
18514 case DW_FORM_strp:
18515 if (!cu->per_cu->is_dwz)
18516 {
18517 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18518 abfd, info_ptr, cu_header,
18519 &bytes_read);
18520 DW_STRING_IS_CANONICAL (attr) = 0;
18521 info_ptr += bytes_read;
18522 break;
18523 }
18524 /* FALLTHROUGH */
18525 case DW_FORM_line_strp:
18526 if (!cu->per_cu->is_dwz)
18527 {
18528 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18529 abfd, info_ptr,
18530 cu_header, &bytes_read);
18531 DW_STRING_IS_CANONICAL (attr) = 0;
18532 info_ptr += bytes_read;
18533 break;
18534 }
18535 /* FALLTHROUGH */
18536 case DW_FORM_GNU_strp_alt:
18537 {
18538 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18539 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18540 &bytes_read);
18541
18542 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18543 dwz, str_offset);
18544 DW_STRING_IS_CANONICAL (attr) = 0;
18545 info_ptr += bytes_read;
18546 }
18547 break;
18548 case DW_FORM_exprloc:
18549 case DW_FORM_block:
18550 blk = dwarf_alloc_block (cu);
18551 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18552 info_ptr += bytes_read;
18553 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18554 info_ptr += blk->size;
18555 DW_BLOCK (attr) = blk;
18556 break;
18557 case DW_FORM_block1:
18558 blk = dwarf_alloc_block (cu);
18559 blk->size = read_1_byte (abfd, info_ptr);
18560 info_ptr += 1;
18561 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18562 info_ptr += blk->size;
18563 DW_BLOCK (attr) = blk;
18564 break;
18565 case DW_FORM_data1:
18566 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18567 info_ptr += 1;
18568 break;
18569 case DW_FORM_flag:
18570 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18571 info_ptr += 1;
18572 break;
18573 case DW_FORM_flag_present:
18574 DW_UNSND (attr) = 1;
18575 break;
18576 case DW_FORM_sdata:
18577 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18578 info_ptr += bytes_read;
18579 break;
18580 case DW_FORM_udata:
18581 case DW_FORM_rnglistx:
18582 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18583 info_ptr += bytes_read;
18584 break;
18585 case DW_FORM_ref1:
18586 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18587 + read_1_byte (abfd, info_ptr));
18588 info_ptr += 1;
18589 break;
18590 case DW_FORM_ref2:
18591 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18592 + read_2_bytes (abfd, info_ptr));
18593 info_ptr += 2;
18594 break;
18595 case DW_FORM_ref4:
18596 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18597 + read_4_bytes (abfd, info_ptr));
18598 info_ptr += 4;
18599 break;
18600 case DW_FORM_ref8:
18601 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18602 + read_8_bytes (abfd, info_ptr));
18603 info_ptr += 8;
18604 break;
18605 case DW_FORM_ref_sig8:
18606 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18607 info_ptr += 8;
18608 break;
18609 case DW_FORM_ref_udata:
18610 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18611 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18612 info_ptr += bytes_read;
18613 break;
18614 case DW_FORM_indirect:
18615 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18616 info_ptr += bytes_read;
18617 if (form == DW_FORM_implicit_const)
18618 {
18619 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18620 info_ptr += bytes_read;
18621 }
18622 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18623 info_ptr, need_reprocess);
18624 break;
18625 case DW_FORM_implicit_const:
18626 DW_SND (attr) = implicit_const;
18627 break;
18628 case DW_FORM_addrx:
18629 case DW_FORM_GNU_addr_index:
18630 *need_reprocess = true;
18631 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18632 info_ptr += bytes_read;
18633 break;
18634 case DW_FORM_strx:
18635 case DW_FORM_strx1:
18636 case DW_FORM_strx2:
18637 case DW_FORM_strx3:
18638 case DW_FORM_strx4:
18639 case DW_FORM_GNU_str_index:
18640 {
18641 ULONGEST str_index;
18642 if (form == DW_FORM_strx1)
18643 {
18644 str_index = read_1_byte (abfd, info_ptr);
18645 info_ptr += 1;
18646 }
18647 else if (form == DW_FORM_strx2)
18648 {
18649 str_index = read_2_bytes (abfd, info_ptr);
18650 info_ptr += 2;
18651 }
18652 else if (form == DW_FORM_strx3)
18653 {
18654 str_index = read_3_bytes (abfd, info_ptr);
18655 info_ptr += 3;
18656 }
18657 else if (form == DW_FORM_strx4)
18658 {
18659 str_index = read_4_bytes (abfd, info_ptr);
18660 info_ptr += 4;
18661 }
18662 else
18663 {
18664 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18665 info_ptr += bytes_read;
18666 }
18667 *need_reprocess = true;
18668 DW_UNSND (attr) = str_index;
18669 }
18670 break;
18671 default:
18672 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18673 dwarf_form_name (form),
18674 bfd_get_filename (abfd));
18675 }
18676
18677 /* Super hack. */
18678 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18679 attr->form = DW_FORM_GNU_ref_alt;
18680
18681 /* We have seen instances where the compiler tried to emit a byte
18682 size attribute of -1 which ended up being encoded as an unsigned
18683 0xffffffff. Although 0xffffffff is technically a valid size value,
18684 an object of this size seems pretty unlikely so we can relatively
18685 safely treat these cases as if the size attribute was invalid and
18686 treat them as zero by default. */
18687 if (attr->name == DW_AT_byte_size
18688 && form == DW_FORM_data4
18689 && DW_UNSND (attr) >= 0xffffffff)
18690 {
18691 complaint
18692 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18693 hex_string (DW_UNSND (attr)));
18694 DW_UNSND (attr) = 0;
18695 }
18696
18697 return info_ptr;
18698 }
18699
18700 /* Read an attribute described by an abbreviated attribute. */
18701
18702 static const gdb_byte *
18703 read_attribute (const struct die_reader_specs *reader,
18704 struct attribute *attr, struct attr_abbrev *abbrev,
18705 const gdb_byte *info_ptr, bool *need_reprocess)
18706 {
18707 attr->name = abbrev->name;
18708 return read_attribute_value (reader, attr, abbrev->form,
18709 abbrev->implicit_const, info_ptr,
18710 need_reprocess);
18711 }
18712
18713 /* Cover function for read_initial_length.
18714 Returns the length of the object at BUF, and stores the size of the
18715 initial length in *BYTES_READ and stores the size that offsets will be in
18716 *OFFSET_SIZE.
18717 If the initial length size is not equivalent to that specified in
18718 CU_HEADER then issue a complaint.
18719 This is useful when reading non-comp-unit headers. */
18720
18721 static LONGEST
18722 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18723 const struct comp_unit_head *cu_header,
18724 unsigned int *bytes_read,
18725 unsigned int *offset_size)
18726 {
18727 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18728
18729 gdb_assert (cu_header->initial_length_size == 4
18730 || cu_header->initial_length_size == 8
18731 || cu_header->initial_length_size == 12);
18732
18733 if (cu_header->initial_length_size != *bytes_read)
18734 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18735
18736 *offset_size = (*bytes_read == 4) ? 4 : 8;
18737 return length;
18738 }
18739
18740 /* Return pointer to string at section SECT offset STR_OFFSET with error
18741 reporting strings FORM_NAME and SECT_NAME. */
18742
18743 static const char *
18744 read_indirect_string_at_offset_from (struct objfile *objfile,
18745 bfd *abfd, LONGEST str_offset,
18746 struct dwarf2_section_info *sect,
18747 const char *form_name,
18748 const char *sect_name)
18749 {
18750 sect->read (objfile);
18751 if (sect->buffer == NULL)
18752 error (_("%s used without %s section [in module %s]"),
18753 form_name, sect_name, bfd_get_filename (abfd));
18754 if (str_offset >= sect->size)
18755 error (_("%s pointing outside of %s section [in module %s]"),
18756 form_name, sect_name, bfd_get_filename (abfd));
18757 gdb_assert (HOST_CHAR_BIT == 8);
18758 if (sect->buffer[str_offset] == '\0')
18759 return NULL;
18760 return (const char *) (sect->buffer + str_offset);
18761 }
18762
18763 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18764
18765 static const char *
18766 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18767 bfd *abfd, LONGEST str_offset)
18768 {
18769 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18770 abfd, str_offset,
18771 &dwarf2_per_objfile->str,
18772 "DW_FORM_strp", ".debug_str");
18773 }
18774
18775 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18776
18777 static const char *
18778 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18779 bfd *abfd, LONGEST str_offset)
18780 {
18781 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18782 abfd, str_offset,
18783 &dwarf2_per_objfile->line_str,
18784 "DW_FORM_line_strp",
18785 ".debug_line_str");
18786 }
18787
18788 /* Read a string at offset STR_OFFSET in the .debug_str section from
18789 the .dwz file DWZ. Throw an error if the offset is too large. If
18790 the string consists of a single NUL byte, return NULL; otherwise
18791 return a pointer to the string. */
18792
18793 static const char *
18794 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18795 LONGEST str_offset)
18796 {
18797 dwz->str.read (objfile);
18798
18799 if (dwz->str.buffer == NULL)
18800 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18801 "section [in module %s]"),
18802 bfd_get_filename (dwz->dwz_bfd.get ()));
18803 if (str_offset >= dwz->str.size)
18804 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18805 ".debug_str section [in module %s]"),
18806 bfd_get_filename (dwz->dwz_bfd.get ()));
18807 gdb_assert (HOST_CHAR_BIT == 8);
18808 if (dwz->str.buffer[str_offset] == '\0')
18809 return NULL;
18810 return (const char *) (dwz->str.buffer + str_offset);
18811 }
18812
18813 /* Return pointer to string at .debug_str offset as read from BUF.
18814 BUF is assumed to be in a compilation unit described by CU_HEADER.
18815 Return *BYTES_READ_PTR count of bytes read from BUF. */
18816
18817 static const char *
18818 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18819 const gdb_byte *buf,
18820 const struct comp_unit_head *cu_header,
18821 unsigned int *bytes_read_ptr)
18822 {
18823 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18824
18825 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18826 }
18827
18828 /* Return pointer to string at .debug_line_str offset as read from BUF.
18829 BUF is assumed to be in a compilation unit described by CU_HEADER.
18830 Return *BYTES_READ_PTR count of bytes read from BUF. */
18831
18832 static const char *
18833 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18834 bfd *abfd, const gdb_byte *buf,
18835 const struct comp_unit_head *cu_header,
18836 unsigned int *bytes_read_ptr)
18837 {
18838 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18839
18840 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18841 str_offset);
18842 }
18843
18844 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18845 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18846 ADDR_SIZE is the size of addresses from the CU header. */
18847
18848 static CORE_ADDR
18849 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18850 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18851 int addr_size)
18852 {
18853 struct objfile *objfile = dwarf2_per_objfile->objfile;
18854 bfd *abfd = objfile->obfd;
18855 const gdb_byte *info_ptr;
18856 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18857
18858 dwarf2_per_objfile->addr.read (objfile);
18859 if (dwarf2_per_objfile->addr.buffer == NULL)
18860 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18861 objfile_name (objfile));
18862 if (addr_base_or_zero + addr_index * addr_size
18863 >= dwarf2_per_objfile->addr.size)
18864 error (_("DW_FORM_addr_index pointing outside of "
18865 ".debug_addr section [in module %s]"),
18866 objfile_name (objfile));
18867 info_ptr = (dwarf2_per_objfile->addr.buffer
18868 + addr_base_or_zero + addr_index * addr_size);
18869 if (addr_size == 4)
18870 return bfd_get_32 (abfd, info_ptr);
18871 else
18872 return bfd_get_64 (abfd, info_ptr);
18873 }
18874
18875 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18876
18877 static CORE_ADDR
18878 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18879 {
18880 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18881 cu->addr_base, cu->header.addr_size);
18882 }
18883
18884 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18885
18886 static CORE_ADDR
18887 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18888 unsigned int *bytes_read)
18889 {
18890 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18891 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18892
18893 return read_addr_index (cu, addr_index);
18894 }
18895
18896 /* Given an index in .debug_addr, fetch the value.
18897 NOTE: This can be called during dwarf expression evaluation,
18898 long after the debug information has been read, and thus per_cu->cu
18899 may no longer exist. */
18900
18901 CORE_ADDR
18902 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18903 unsigned int addr_index)
18904 {
18905 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18906 struct dwarf2_cu *cu = per_cu->cu;
18907 gdb::optional<ULONGEST> addr_base;
18908 int addr_size;
18909
18910 /* We need addr_base and addr_size.
18911 If we don't have PER_CU->cu, we have to get it.
18912 Nasty, but the alternative is storing the needed info in PER_CU,
18913 which at this point doesn't seem justified: it's not clear how frequently
18914 it would get used and it would increase the size of every PER_CU.
18915 Entry points like dwarf2_per_cu_addr_size do a similar thing
18916 so we're not in uncharted territory here.
18917 Alas we need to be a bit more complicated as addr_base is contained
18918 in the DIE.
18919
18920 We don't need to read the entire CU(/TU).
18921 We just need the header and top level die.
18922
18923 IWBN to use the aging mechanism to let us lazily later discard the CU.
18924 For now we skip this optimization. */
18925
18926 if (cu != NULL)
18927 {
18928 addr_base = cu->addr_base;
18929 addr_size = cu->header.addr_size;
18930 }
18931 else
18932 {
18933 cutu_reader reader (per_cu, NULL, 0, false);
18934 addr_base = reader.cu->addr_base;
18935 addr_size = reader.cu->header.addr_size;
18936 }
18937
18938 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18939 addr_size);
18940 }
18941
18942 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18943 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18944 DWO file. */
18945
18946 static const char *
18947 read_str_index (struct dwarf2_cu *cu,
18948 struct dwarf2_section_info *str_section,
18949 struct dwarf2_section_info *str_offsets_section,
18950 ULONGEST str_offsets_base, ULONGEST str_index)
18951 {
18952 struct dwarf2_per_objfile *dwarf2_per_objfile
18953 = cu->per_cu->dwarf2_per_objfile;
18954 struct objfile *objfile = dwarf2_per_objfile->objfile;
18955 const char *objf_name = objfile_name (objfile);
18956 bfd *abfd = objfile->obfd;
18957 const gdb_byte *info_ptr;
18958 ULONGEST str_offset;
18959 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18960
18961 str_section->read (objfile);
18962 str_offsets_section->read (objfile);
18963 if (str_section->buffer == NULL)
18964 error (_("%s used without %s section"
18965 " in CU at offset %s [in module %s]"),
18966 form_name, str_section->get_name (),
18967 sect_offset_str (cu->header.sect_off), objf_name);
18968 if (str_offsets_section->buffer == NULL)
18969 error (_("%s used without %s section"
18970 " in CU at offset %s [in module %s]"),
18971 form_name, str_section->get_name (),
18972 sect_offset_str (cu->header.sect_off), objf_name);
18973 info_ptr = (str_offsets_section->buffer
18974 + str_offsets_base
18975 + str_index * cu->header.offset_size);
18976 if (cu->header.offset_size == 4)
18977 str_offset = bfd_get_32 (abfd, info_ptr);
18978 else
18979 str_offset = bfd_get_64 (abfd, info_ptr);
18980 if (str_offset >= str_section->size)
18981 error (_("Offset from %s pointing outside of"
18982 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18983 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18984 return (const char *) (str_section->buffer + str_offset);
18985 }
18986
18987 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18988
18989 static const char *
18990 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18991 {
18992 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18993 ? reader->cu->header.addr_size : 0;
18994 return read_str_index (reader->cu,
18995 &reader->dwo_file->sections.str,
18996 &reader->dwo_file->sections.str_offsets,
18997 str_offsets_base, str_index);
18998 }
18999
19000 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19001
19002 static const char *
19003 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19004 {
19005 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19006 const char *objf_name = objfile_name (objfile);
19007 static const char form_name[] = "DW_FORM_GNU_str_index";
19008 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19009
19010 if (!cu->str_offsets_base.has_value ())
19011 error (_("%s used in Fission stub without %s"
19012 " in CU at offset 0x%lx [in module %s]"),
19013 form_name, str_offsets_attr_name,
19014 (long) cu->header.offset_size, objf_name);
19015
19016 return read_str_index (cu,
19017 &cu->per_cu->dwarf2_per_objfile->str,
19018 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19019 *cu->str_offsets_base, str_index);
19020 }
19021
19022 /* Return the length of an LEB128 number in BUF. */
19023
19024 static int
19025 leb128_size (const gdb_byte *buf)
19026 {
19027 const gdb_byte *begin = buf;
19028 gdb_byte byte;
19029
19030 while (1)
19031 {
19032 byte = *buf++;
19033 if ((byte & 128) == 0)
19034 return buf - begin;
19035 }
19036 }
19037
19038 static void
19039 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19040 {
19041 switch (lang)
19042 {
19043 case DW_LANG_C89:
19044 case DW_LANG_C99:
19045 case DW_LANG_C11:
19046 case DW_LANG_C:
19047 case DW_LANG_UPC:
19048 cu->language = language_c;
19049 break;
19050 case DW_LANG_Java:
19051 case DW_LANG_C_plus_plus:
19052 case DW_LANG_C_plus_plus_11:
19053 case DW_LANG_C_plus_plus_14:
19054 cu->language = language_cplus;
19055 break;
19056 case DW_LANG_D:
19057 cu->language = language_d;
19058 break;
19059 case DW_LANG_Fortran77:
19060 case DW_LANG_Fortran90:
19061 case DW_LANG_Fortran95:
19062 case DW_LANG_Fortran03:
19063 case DW_LANG_Fortran08:
19064 cu->language = language_fortran;
19065 break;
19066 case DW_LANG_Go:
19067 cu->language = language_go;
19068 break;
19069 case DW_LANG_Mips_Assembler:
19070 cu->language = language_asm;
19071 break;
19072 case DW_LANG_Ada83:
19073 case DW_LANG_Ada95:
19074 cu->language = language_ada;
19075 break;
19076 case DW_LANG_Modula2:
19077 cu->language = language_m2;
19078 break;
19079 case DW_LANG_Pascal83:
19080 cu->language = language_pascal;
19081 break;
19082 case DW_LANG_ObjC:
19083 cu->language = language_objc;
19084 break;
19085 case DW_LANG_Rust:
19086 case DW_LANG_Rust_old:
19087 cu->language = language_rust;
19088 break;
19089 case DW_LANG_Cobol74:
19090 case DW_LANG_Cobol85:
19091 default:
19092 cu->language = language_minimal;
19093 break;
19094 }
19095 cu->language_defn = language_def (cu->language);
19096 }
19097
19098 /* Return the named attribute or NULL if not there. */
19099
19100 static struct attribute *
19101 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19102 {
19103 for (;;)
19104 {
19105 unsigned int i;
19106 struct attribute *spec = NULL;
19107
19108 for (i = 0; i < die->num_attrs; ++i)
19109 {
19110 if (die->attrs[i].name == name)
19111 return &die->attrs[i];
19112 if (die->attrs[i].name == DW_AT_specification
19113 || die->attrs[i].name == DW_AT_abstract_origin)
19114 spec = &die->attrs[i];
19115 }
19116
19117 if (!spec)
19118 break;
19119
19120 die = follow_die_ref (die, spec, &cu);
19121 }
19122
19123 return NULL;
19124 }
19125
19126 /* Return the named attribute or NULL if not there,
19127 but do not follow DW_AT_specification, etc.
19128 This is for use in contexts where we're reading .debug_types dies.
19129 Following DW_AT_specification, DW_AT_abstract_origin will take us
19130 back up the chain, and we want to go down. */
19131
19132 static struct attribute *
19133 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19134 {
19135 unsigned int i;
19136
19137 for (i = 0; i < die->num_attrs; ++i)
19138 if (die->attrs[i].name == name)
19139 return &die->attrs[i];
19140
19141 return NULL;
19142 }
19143
19144 /* Return the string associated with a string-typed attribute, or NULL if it
19145 is either not found or is of an incorrect type. */
19146
19147 static const char *
19148 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19149 {
19150 struct attribute *attr;
19151 const char *str = NULL;
19152
19153 attr = dwarf2_attr (die, name, cu);
19154
19155 if (attr != NULL)
19156 {
19157 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19158 || attr->form == DW_FORM_string
19159 || attr->form == DW_FORM_strx
19160 || attr->form == DW_FORM_strx1
19161 || attr->form == DW_FORM_strx2
19162 || attr->form == DW_FORM_strx3
19163 || attr->form == DW_FORM_strx4
19164 || attr->form == DW_FORM_GNU_str_index
19165 || attr->form == DW_FORM_GNU_strp_alt)
19166 str = DW_STRING (attr);
19167 else
19168 complaint (_("string type expected for attribute %s for "
19169 "DIE at %s in module %s"),
19170 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19171 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19172 }
19173
19174 return str;
19175 }
19176
19177 /* Return the dwo name or NULL if not present. If present, it is in either
19178 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19179 static const char *
19180 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19181 {
19182 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19183 if (dwo_name == nullptr)
19184 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19185 return dwo_name;
19186 }
19187
19188 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19189 and holds a non-zero value. This function should only be used for
19190 DW_FORM_flag or DW_FORM_flag_present attributes. */
19191
19192 static int
19193 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19194 {
19195 struct attribute *attr = dwarf2_attr (die, name, cu);
19196
19197 return (attr && DW_UNSND (attr));
19198 }
19199
19200 static int
19201 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19202 {
19203 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19204 which value is non-zero. However, we have to be careful with
19205 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19206 (via dwarf2_flag_true_p) follows this attribute. So we may
19207 end up accidently finding a declaration attribute that belongs
19208 to a different DIE referenced by the specification attribute,
19209 even though the given DIE does not have a declaration attribute. */
19210 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19211 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19212 }
19213
19214 /* Return the die giving the specification for DIE, if there is
19215 one. *SPEC_CU is the CU containing DIE on input, and the CU
19216 containing the return value on output. If there is no
19217 specification, but there is an abstract origin, that is
19218 returned. */
19219
19220 static struct die_info *
19221 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19222 {
19223 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19224 *spec_cu);
19225
19226 if (spec_attr == NULL)
19227 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19228
19229 if (spec_attr == NULL)
19230 return NULL;
19231 else
19232 return follow_die_ref (die, spec_attr, spec_cu);
19233 }
19234
19235 /* Stub for free_line_header to match void * callback types. */
19236
19237 static void
19238 free_line_header_voidp (void *arg)
19239 {
19240 struct line_header *lh = (struct line_header *) arg;
19241
19242 delete lh;
19243 }
19244
19245 /* A convenience function to find the proper .debug_line section for a CU. */
19246
19247 static struct dwarf2_section_info *
19248 get_debug_line_section (struct dwarf2_cu *cu)
19249 {
19250 struct dwarf2_section_info *section;
19251 struct dwarf2_per_objfile *dwarf2_per_objfile
19252 = cu->per_cu->dwarf2_per_objfile;
19253
19254 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19255 DWO file. */
19256 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19257 section = &cu->dwo_unit->dwo_file->sections.line;
19258 else if (cu->per_cu->is_dwz)
19259 {
19260 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19261
19262 section = &dwz->line;
19263 }
19264 else
19265 section = &dwarf2_per_objfile->line;
19266
19267 return section;
19268 }
19269
19270 /* Read directory or file name entry format, starting with byte of
19271 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19272 entries count and the entries themselves in the described entry
19273 format. */
19274
19275 static void
19276 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19277 bfd *abfd, const gdb_byte **bufp,
19278 struct line_header *lh,
19279 const struct comp_unit_head *cu_header,
19280 void (*callback) (struct line_header *lh,
19281 const char *name,
19282 dir_index d_index,
19283 unsigned int mod_time,
19284 unsigned int length))
19285 {
19286 gdb_byte format_count, formati;
19287 ULONGEST data_count, datai;
19288 const gdb_byte *buf = *bufp;
19289 const gdb_byte *format_header_data;
19290 unsigned int bytes_read;
19291
19292 format_count = read_1_byte (abfd, buf);
19293 buf += 1;
19294 format_header_data = buf;
19295 for (formati = 0; formati < format_count; formati++)
19296 {
19297 read_unsigned_leb128 (abfd, buf, &bytes_read);
19298 buf += bytes_read;
19299 read_unsigned_leb128 (abfd, buf, &bytes_read);
19300 buf += bytes_read;
19301 }
19302
19303 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19304 buf += bytes_read;
19305 for (datai = 0; datai < data_count; datai++)
19306 {
19307 const gdb_byte *format = format_header_data;
19308 struct file_entry fe;
19309
19310 for (formati = 0; formati < format_count; formati++)
19311 {
19312 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19313 format += bytes_read;
19314
19315 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19316 format += bytes_read;
19317
19318 gdb::optional<const char *> string;
19319 gdb::optional<unsigned int> uint;
19320
19321 switch (form)
19322 {
19323 case DW_FORM_string:
19324 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19325 buf += bytes_read;
19326 break;
19327
19328 case DW_FORM_line_strp:
19329 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19330 abfd, buf,
19331 cu_header,
19332 &bytes_read));
19333 buf += bytes_read;
19334 break;
19335
19336 case DW_FORM_data1:
19337 uint.emplace (read_1_byte (abfd, buf));
19338 buf += 1;
19339 break;
19340
19341 case DW_FORM_data2:
19342 uint.emplace (read_2_bytes (abfd, buf));
19343 buf += 2;
19344 break;
19345
19346 case DW_FORM_data4:
19347 uint.emplace (read_4_bytes (abfd, buf));
19348 buf += 4;
19349 break;
19350
19351 case DW_FORM_data8:
19352 uint.emplace (read_8_bytes (abfd, buf));
19353 buf += 8;
19354 break;
19355
19356 case DW_FORM_data16:
19357 /* This is used for MD5, but file_entry does not record MD5s. */
19358 buf += 16;
19359 break;
19360
19361 case DW_FORM_udata:
19362 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19363 buf += bytes_read;
19364 break;
19365
19366 case DW_FORM_block:
19367 /* It is valid only for DW_LNCT_timestamp which is ignored by
19368 current GDB. */
19369 break;
19370 }
19371
19372 switch (content_type)
19373 {
19374 case DW_LNCT_path:
19375 if (string.has_value ())
19376 fe.name = *string;
19377 break;
19378 case DW_LNCT_directory_index:
19379 if (uint.has_value ())
19380 fe.d_index = (dir_index) *uint;
19381 break;
19382 case DW_LNCT_timestamp:
19383 if (uint.has_value ())
19384 fe.mod_time = *uint;
19385 break;
19386 case DW_LNCT_size:
19387 if (uint.has_value ())
19388 fe.length = *uint;
19389 break;
19390 case DW_LNCT_MD5:
19391 break;
19392 default:
19393 complaint (_("Unknown format content type %s"),
19394 pulongest (content_type));
19395 }
19396 }
19397
19398 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19399 }
19400
19401 *bufp = buf;
19402 }
19403
19404 /* Read the statement program header starting at OFFSET in
19405 .debug_line, or .debug_line.dwo. Return a pointer
19406 to a struct line_header, allocated using xmalloc.
19407 Returns NULL if there is a problem reading the header, e.g., if it
19408 has a version we don't understand.
19409
19410 NOTE: the strings in the include directory and file name tables of
19411 the returned object point into the dwarf line section buffer,
19412 and must not be freed. */
19413
19414 static line_header_up
19415 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19416 {
19417 const gdb_byte *line_ptr;
19418 unsigned int bytes_read, offset_size;
19419 int i;
19420 const char *cur_dir, *cur_file;
19421 struct dwarf2_section_info *section;
19422 bfd *abfd;
19423 struct dwarf2_per_objfile *dwarf2_per_objfile
19424 = cu->per_cu->dwarf2_per_objfile;
19425
19426 section = get_debug_line_section (cu);
19427 section->read (dwarf2_per_objfile->objfile);
19428 if (section->buffer == NULL)
19429 {
19430 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19431 complaint (_("missing .debug_line.dwo section"));
19432 else
19433 complaint (_("missing .debug_line section"));
19434 return 0;
19435 }
19436
19437 /* We can't do this until we know the section is non-empty.
19438 Only then do we know we have such a section. */
19439 abfd = section->get_bfd_owner ();
19440
19441 /* Make sure that at least there's room for the total_length field.
19442 That could be 12 bytes long, but we're just going to fudge that. */
19443 if (to_underlying (sect_off) + 4 >= section->size)
19444 {
19445 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19446 return 0;
19447 }
19448
19449 line_header_up lh (new line_header ());
19450
19451 lh->sect_off = sect_off;
19452 lh->offset_in_dwz = cu->per_cu->is_dwz;
19453
19454 line_ptr = section->buffer + to_underlying (sect_off);
19455
19456 /* Read in the header. */
19457 lh->total_length =
19458 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19459 &bytes_read, &offset_size);
19460 line_ptr += bytes_read;
19461
19462 const gdb_byte *start_here = line_ptr;
19463
19464 if (line_ptr + lh->total_length > (section->buffer + section->size))
19465 {
19466 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19467 return 0;
19468 }
19469 lh->statement_program_end = start_here + lh->total_length;
19470 lh->version = read_2_bytes (abfd, line_ptr);
19471 line_ptr += 2;
19472 if (lh->version > 5)
19473 {
19474 /* This is a version we don't understand. The format could have
19475 changed in ways we don't handle properly so just punt. */
19476 complaint (_("unsupported version in .debug_line section"));
19477 return NULL;
19478 }
19479 if (lh->version >= 5)
19480 {
19481 gdb_byte segment_selector_size;
19482
19483 /* Skip address size. */
19484 read_1_byte (abfd, line_ptr);
19485 line_ptr += 1;
19486
19487 segment_selector_size = read_1_byte (abfd, line_ptr);
19488 line_ptr += 1;
19489 if (segment_selector_size != 0)
19490 {
19491 complaint (_("unsupported segment selector size %u "
19492 "in .debug_line section"),
19493 segment_selector_size);
19494 return NULL;
19495 }
19496 }
19497 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19498 line_ptr += offset_size;
19499 lh->statement_program_start = line_ptr + lh->header_length;
19500 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19501 line_ptr += 1;
19502 if (lh->version >= 4)
19503 {
19504 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 }
19507 else
19508 lh->maximum_ops_per_instruction = 1;
19509
19510 if (lh->maximum_ops_per_instruction == 0)
19511 {
19512 lh->maximum_ops_per_instruction = 1;
19513 complaint (_("invalid maximum_ops_per_instruction "
19514 "in `.debug_line' section"));
19515 }
19516
19517 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
19519 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19520 line_ptr += 1;
19521 lh->line_range = read_1_byte (abfd, line_ptr);
19522 line_ptr += 1;
19523 lh->opcode_base = read_1_byte (abfd, line_ptr);
19524 line_ptr += 1;
19525 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19526
19527 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19528 for (i = 1; i < lh->opcode_base; ++i)
19529 {
19530 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19531 line_ptr += 1;
19532 }
19533
19534 if (lh->version >= 5)
19535 {
19536 /* Read directory table. */
19537 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19538 &cu->header,
19539 [] (struct line_header *header, const char *name,
19540 dir_index d_index, unsigned int mod_time,
19541 unsigned int length)
19542 {
19543 header->add_include_dir (name);
19544 });
19545
19546 /* Read file name table. */
19547 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19548 &cu->header,
19549 [] (struct line_header *header, const char *name,
19550 dir_index d_index, unsigned int mod_time,
19551 unsigned int length)
19552 {
19553 header->add_file_name (name, d_index, mod_time, length);
19554 });
19555 }
19556 else
19557 {
19558 /* Read directory table. */
19559 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19560 {
19561 line_ptr += bytes_read;
19562 lh->add_include_dir (cur_dir);
19563 }
19564 line_ptr += bytes_read;
19565
19566 /* Read file name table. */
19567 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19568 {
19569 unsigned int mod_time, length;
19570 dir_index d_index;
19571
19572 line_ptr += bytes_read;
19573 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19574 line_ptr += bytes_read;
19575 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19576 line_ptr += bytes_read;
19577 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19578 line_ptr += bytes_read;
19579
19580 lh->add_file_name (cur_file, d_index, mod_time, length);
19581 }
19582 line_ptr += bytes_read;
19583 }
19584
19585 if (line_ptr > (section->buffer + section->size))
19586 complaint (_("line number info header doesn't "
19587 "fit in `.debug_line' section"));
19588
19589 return lh;
19590 }
19591
19592 /* Subroutine of dwarf_decode_lines to simplify it.
19593 Return the file name of the psymtab for the given file_entry.
19594 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19595 If space for the result is malloc'd, *NAME_HOLDER will be set.
19596 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19597
19598 static const char *
19599 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19600 const dwarf2_psymtab *pst,
19601 const char *comp_dir,
19602 gdb::unique_xmalloc_ptr<char> *name_holder)
19603 {
19604 const char *include_name = fe.name;
19605 const char *include_name_to_compare = include_name;
19606 const char *pst_filename;
19607 int file_is_pst;
19608
19609 const char *dir_name = fe.include_dir (lh);
19610
19611 gdb::unique_xmalloc_ptr<char> hold_compare;
19612 if (!IS_ABSOLUTE_PATH (include_name)
19613 && (dir_name != NULL || comp_dir != NULL))
19614 {
19615 /* Avoid creating a duplicate psymtab for PST.
19616 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19617 Before we do the comparison, however, we need to account
19618 for DIR_NAME and COMP_DIR.
19619 First prepend dir_name (if non-NULL). If we still don't
19620 have an absolute path prepend comp_dir (if non-NULL).
19621 However, the directory we record in the include-file's
19622 psymtab does not contain COMP_DIR (to match the
19623 corresponding symtab(s)).
19624
19625 Example:
19626
19627 bash$ cd /tmp
19628 bash$ gcc -g ./hello.c
19629 include_name = "hello.c"
19630 dir_name = "."
19631 DW_AT_comp_dir = comp_dir = "/tmp"
19632 DW_AT_name = "./hello.c"
19633
19634 */
19635
19636 if (dir_name != NULL)
19637 {
19638 name_holder->reset (concat (dir_name, SLASH_STRING,
19639 include_name, (char *) NULL));
19640 include_name = name_holder->get ();
19641 include_name_to_compare = include_name;
19642 }
19643 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19644 {
19645 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19646 include_name, (char *) NULL));
19647 include_name_to_compare = hold_compare.get ();
19648 }
19649 }
19650
19651 pst_filename = pst->filename;
19652 gdb::unique_xmalloc_ptr<char> copied_name;
19653 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19654 {
19655 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19656 pst_filename, (char *) NULL));
19657 pst_filename = copied_name.get ();
19658 }
19659
19660 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19661
19662 if (file_is_pst)
19663 return NULL;
19664 return include_name;
19665 }
19666
19667 /* State machine to track the state of the line number program. */
19668
19669 class lnp_state_machine
19670 {
19671 public:
19672 /* Initialize a machine state for the start of a line number
19673 program. */
19674 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19675 bool record_lines_p);
19676
19677 file_entry *current_file ()
19678 {
19679 /* lh->file_names is 0-based, but the file name numbers in the
19680 statement program are 1-based. */
19681 return m_line_header->file_name_at (m_file);
19682 }
19683
19684 /* Record the line in the state machine. END_SEQUENCE is true if
19685 we're processing the end of a sequence. */
19686 void record_line (bool end_sequence);
19687
19688 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19689 nop-out rest of the lines in this sequence. */
19690 void check_line_address (struct dwarf2_cu *cu,
19691 const gdb_byte *line_ptr,
19692 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19693
19694 void handle_set_discriminator (unsigned int discriminator)
19695 {
19696 m_discriminator = discriminator;
19697 m_line_has_non_zero_discriminator |= discriminator != 0;
19698 }
19699
19700 /* Handle DW_LNE_set_address. */
19701 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19702 {
19703 m_op_index = 0;
19704 address += baseaddr;
19705 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19706 }
19707
19708 /* Handle DW_LNS_advance_pc. */
19709 void handle_advance_pc (CORE_ADDR adjust);
19710
19711 /* Handle a special opcode. */
19712 void handle_special_opcode (unsigned char op_code);
19713
19714 /* Handle DW_LNS_advance_line. */
19715 void handle_advance_line (int line_delta)
19716 {
19717 advance_line (line_delta);
19718 }
19719
19720 /* Handle DW_LNS_set_file. */
19721 void handle_set_file (file_name_index file);
19722
19723 /* Handle DW_LNS_negate_stmt. */
19724 void handle_negate_stmt ()
19725 {
19726 m_is_stmt = !m_is_stmt;
19727 }
19728
19729 /* Handle DW_LNS_const_add_pc. */
19730 void handle_const_add_pc ();
19731
19732 /* Handle DW_LNS_fixed_advance_pc. */
19733 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19734 {
19735 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19736 m_op_index = 0;
19737 }
19738
19739 /* Handle DW_LNS_copy. */
19740 void handle_copy ()
19741 {
19742 record_line (false);
19743 m_discriminator = 0;
19744 }
19745
19746 /* Handle DW_LNE_end_sequence. */
19747 void handle_end_sequence ()
19748 {
19749 m_currently_recording_lines = true;
19750 }
19751
19752 private:
19753 /* Advance the line by LINE_DELTA. */
19754 void advance_line (int line_delta)
19755 {
19756 m_line += line_delta;
19757
19758 if (line_delta != 0)
19759 m_line_has_non_zero_discriminator = m_discriminator != 0;
19760 }
19761
19762 struct dwarf2_cu *m_cu;
19763
19764 gdbarch *m_gdbarch;
19765
19766 /* True if we're recording lines.
19767 Otherwise we're building partial symtabs and are just interested in
19768 finding include files mentioned by the line number program. */
19769 bool m_record_lines_p;
19770
19771 /* The line number header. */
19772 line_header *m_line_header;
19773
19774 /* These are part of the standard DWARF line number state machine,
19775 and initialized according to the DWARF spec. */
19776
19777 unsigned char m_op_index = 0;
19778 /* The line table index of the current file. */
19779 file_name_index m_file = 1;
19780 unsigned int m_line = 1;
19781
19782 /* These are initialized in the constructor. */
19783
19784 CORE_ADDR m_address;
19785 bool m_is_stmt;
19786 unsigned int m_discriminator;
19787
19788 /* Additional bits of state we need to track. */
19789
19790 /* The last file that we called dwarf2_start_subfile for.
19791 This is only used for TLLs. */
19792 unsigned int m_last_file = 0;
19793 /* The last file a line number was recorded for. */
19794 struct subfile *m_last_subfile = NULL;
19795
19796 /* When true, record the lines we decode. */
19797 bool m_currently_recording_lines = false;
19798
19799 /* The last line number that was recorded, used to coalesce
19800 consecutive entries for the same line. This can happen, for
19801 example, when discriminators are present. PR 17276. */
19802 unsigned int m_last_line = 0;
19803 bool m_line_has_non_zero_discriminator = false;
19804 };
19805
19806 void
19807 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19808 {
19809 CORE_ADDR addr_adj = (((m_op_index + adjust)
19810 / m_line_header->maximum_ops_per_instruction)
19811 * m_line_header->minimum_instruction_length);
19812 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19813 m_op_index = ((m_op_index + adjust)
19814 % m_line_header->maximum_ops_per_instruction);
19815 }
19816
19817 void
19818 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19819 {
19820 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19821 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19822 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19823 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19824 / m_line_header->maximum_ops_per_instruction)
19825 * m_line_header->minimum_instruction_length);
19826 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19827 m_op_index = ((m_op_index + adj_opcode_d)
19828 % m_line_header->maximum_ops_per_instruction);
19829
19830 int line_delta = m_line_header->line_base + adj_opcode_r;
19831 advance_line (line_delta);
19832 record_line (false);
19833 m_discriminator = 0;
19834 }
19835
19836 void
19837 lnp_state_machine::handle_set_file (file_name_index file)
19838 {
19839 m_file = file;
19840
19841 const file_entry *fe = current_file ();
19842 if (fe == NULL)
19843 dwarf2_debug_line_missing_file_complaint ();
19844 else if (m_record_lines_p)
19845 {
19846 const char *dir = fe->include_dir (m_line_header);
19847
19848 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19849 m_line_has_non_zero_discriminator = m_discriminator != 0;
19850 dwarf2_start_subfile (m_cu, fe->name, dir);
19851 }
19852 }
19853
19854 void
19855 lnp_state_machine::handle_const_add_pc ()
19856 {
19857 CORE_ADDR adjust
19858 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19859
19860 CORE_ADDR addr_adj
19861 = (((m_op_index + adjust)
19862 / m_line_header->maximum_ops_per_instruction)
19863 * m_line_header->minimum_instruction_length);
19864
19865 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19866 m_op_index = ((m_op_index + adjust)
19867 % m_line_header->maximum_ops_per_instruction);
19868 }
19869
19870 /* Return non-zero if we should add LINE to the line number table.
19871 LINE is the line to add, LAST_LINE is the last line that was added,
19872 LAST_SUBFILE is the subfile for LAST_LINE.
19873 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19874 had a non-zero discriminator.
19875
19876 We have to be careful in the presence of discriminators.
19877 E.g., for this line:
19878
19879 for (i = 0; i < 100000; i++);
19880
19881 clang can emit four line number entries for that one line,
19882 each with a different discriminator.
19883 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19884
19885 However, we want gdb to coalesce all four entries into one.
19886 Otherwise the user could stepi into the middle of the line and
19887 gdb would get confused about whether the pc really was in the
19888 middle of the line.
19889
19890 Things are further complicated by the fact that two consecutive
19891 line number entries for the same line is a heuristic used by gcc
19892 to denote the end of the prologue. So we can't just discard duplicate
19893 entries, we have to be selective about it. The heuristic we use is
19894 that we only collapse consecutive entries for the same line if at least
19895 one of those entries has a non-zero discriminator. PR 17276.
19896
19897 Note: Addresses in the line number state machine can never go backwards
19898 within one sequence, thus this coalescing is ok. */
19899
19900 static int
19901 dwarf_record_line_p (struct dwarf2_cu *cu,
19902 unsigned int line, unsigned int last_line,
19903 int line_has_non_zero_discriminator,
19904 struct subfile *last_subfile)
19905 {
19906 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19907 return 1;
19908 if (line != last_line)
19909 return 1;
19910 /* Same line for the same file that we've seen already.
19911 As a last check, for pr 17276, only record the line if the line
19912 has never had a non-zero discriminator. */
19913 if (!line_has_non_zero_discriminator)
19914 return 1;
19915 return 0;
19916 }
19917
19918 /* Use the CU's builder to record line number LINE beginning at
19919 address ADDRESS in the line table of subfile SUBFILE. */
19920
19921 static void
19922 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19923 unsigned int line, CORE_ADDR address,
19924 struct dwarf2_cu *cu)
19925 {
19926 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19927
19928 if (dwarf_line_debug)
19929 {
19930 fprintf_unfiltered (gdb_stdlog,
19931 "Recording line %u, file %s, address %s\n",
19932 line, lbasename (subfile->name),
19933 paddress (gdbarch, address));
19934 }
19935
19936 if (cu != nullptr)
19937 cu->get_builder ()->record_line (subfile, line, addr);
19938 }
19939
19940 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19941 Mark the end of a set of line number records.
19942 The arguments are the same as for dwarf_record_line_1.
19943 If SUBFILE is NULL the request is ignored. */
19944
19945 static void
19946 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19947 CORE_ADDR address, struct dwarf2_cu *cu)
19948 {
19949 if (subfile == NULL)
19950 return;
19951
19952 if (dwarf_line_debug)
19953 {
19954 fprintf_unfiltered (gdb_stdlog,
19955 "Finishing current line, file %s, address %s\n",
19956 lbasename (subfile->name),
19957 paddress (gdbarch, address));
19958 }
19959
19960 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19961 }
19962
19963 void
19964 lnp_state_machine::record_line (bool end_sequence)
19965 {
19966 if (dwarf_line_debug)
19967 {
19968 fprintf_unfiltered (gdb_stdlog,
19969 "Processing actual line %u: file %u,"
19970 " address %s, is_stmt %u, discrim %u%s\n",
19971 m_line, m_file,
19972 paddress (m_gdbarch, m_address),
19973 m_is_stmt, m_discriminator,
19974 (end_sequence ? "\t(end sequence)" : ""));
19975 }
19976
19977 file_entry *fe = current_file ();
19978
19979 if (fe == NULL)
19980 dwarf2_debug_line_missing_file_complaint ();
19981 /* For now we ignore lines not starting on an instruction boundary.
19982 But not when processing end_sequence for compatibility with the
19983 previous version of the code. */
19984 else if (m_op_index == 0 || end_sequence)
19985 {
19986 fe->included_p = 1;
19987 if (m_record_lines_p
19988 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19989 {
19990 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19991 || end_sequence)
19992 {
19993 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19994 m_currently_recording_lines ? m_cu : nullptr);
19995 }
19996
19997 if (!end_sequence)
19998 {
19999 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20000 m_line_has_non_zero_discriminator,
20001 m_last_subfile))
20002 {
20003 buildsym_compunit *builder = m_cu->get_builder ();
20004 dwarf_record_line_1 (m_gdbarch,
20005 builder->get_current_subfile (),
20006 m_line, m_address,
20007 m_currently_recording_lines ? m_cu : nullptr);
20008 }
20009 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20010 m_last_line = m_line;
20011 }
20012 }
20013 }
20014 }
20015
20016 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20017 line_header *lh, bool record_lines_p)
20018 {
20019 m_cu = cu;
20020 m_gdbarch = arch;
20021 m_record_lines_p = record_lines_p;
20022 m_line_header = lh;
20023
20024 m_currently_recording_lines = true;
20025
20026 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20027 was a line entry for it so that the backend has a chance to adjust it
20028 and also record it in case it needs it. This is currently used by MIPS
20029 code, cf. `mips_adjust_dwarf2_line'. */
20030 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20031 m_is_stmt = lh->default_is_stmt;
20032 m_discriminator = 0;
20033 }
20034
20035 void
20036 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20037 const gdb_byte *line_ptr,
20038 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20039 {
20040 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20041 the pc range of the CU. However, we restrict the test to only ADDRESS
20042 values of zero to preserve GDB's previous behaviour which is to handle
20043 the specific case of a function being GC'd by the linker. */
20044
20045 if (address == 0 && address < unrelocated_lowpc)
20046 {
20047 /* This line table is for a function which has been
20048 GCd by the linker. Ignore it. PR gdb/12528 */
20049
20050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20051 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20052
20053 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20054 line_offset, objfile_name (objfile));
20055 m_currently_recording_lines = false;
20056 /* Note: m_currently_recording_lines is left as false until we see
20057 DW_LNE_end_sequence. */
20058 }
20059 }
20060
20061 /* Subroutine of dwarf_decode_lines to simplify it.
20062 Process the line number information in LH.
20063 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20064 program in order to set included_p for every referenced header. */
20065
20066 static void
20067 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20068 const int decode_for_pst_p, CORE_ADDR lowpc)
20069 {
20070 const gdb_byte *line_ptr, *extended_end;
20071 const gdb_byte *line_end;
20072 unsigned int bytes_read, extended_len;
20073 unsigned char op_code, extended_op;
20074 CORE_ADDR baseaddr;
20075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20076 bfd *abfd = objfile->obfd;
20077 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20078 /* True if we're recording line info (as opposed to building partial
20079 symtabs and just interested in finding include files mentioned by
20080 the line number program). */
20081 bool record_lines_p = !decode_for_pst_p;
20082
20083 baseaddr = objfile->text_section_offset ();
20084
20085 line_ptr = lh->statement_program_start;
20086 line_end = lh->statement_program_end;
20087
20088 /* Read the statement sequences until there's nothing left. */
20089 while (line_ptr < line_end)
20090 {
20091 /* The DWARF line number program state machine. Reset the state
20092 machine at the start of each sequence. */
20093 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20094 bool end_sequence = false;
20095
20096 if (record_lines_p)
20097 {
20098 /* Start a subfile for the current file of the state
20099 machine. */
20100 const file_entry *fe = state_machine.current_file ();
20101
20102 if (fe != NULL)
20103 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20104 }
20105
20106 /* Decode the table. */
20107 while (line_ptr < line_end && !end_sequence)
20108 {
20109 op_code = read_1_byte (abfd, line_ptr);
20110 line_ptr += 1;
20111
20112 if (op_code >= lh->opcode_base)
20113 {
20114 /* Special opcode. */
20115 state_machine.handle_special_opcode (op_code);
20116 }
20117 else switch (op_code)
20118 {
20119 case DW_LNS_extended_op:
20120 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20121 &bytes_read);
20122 line_ptr += bytes_read;
20123 extended_end = line_ptr + extended_len;
20124 extended_op = read_1_byte (abfd, line_ptr);
20125 line_ptr += 1;
20126 switch (extended_op)
20127 {
20128 case DW_LNE_end_sequence:
20129 state_machine.handle_end_sequence ();
20130 end_sequence = true;
20131 break;
20132 case DW_LNE_set_address:
20133 {
20134 CORE_ADDR address
20135 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20136 line_ptr += bytes_read;
20137
20138 state_machine.check_line_address (cu, line_ptr,
20139 lowpc - baseaddr, address);
20140 state_machine.handle_set_address (baseaddr, address);
20141 }
20142 break;
20143 case DW_LNE_define_file:
20144 {
20145 const char *cur_file;
20146 unsigned int mod_time, length;
20147 dir_index dindex;
20148
20149 cur_file = read_direct_string (abfd, line_ptr,
20150 &bytes_read);
20151 line_ptr += bytes_read;
20152 dindex = (dir_index)
20153 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20154 line_ptr += bytes_read;
20155 mod_time =
20156 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20157 line_ptr += bytes_read;
20158 length =
20159 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20160 line_ptr += bytes_read;
20161 lh->add_file_name (cur_file, dindex, mod_time, length);
20162 }
20163 break;
20164 case DW_LNE_set_discriminator:
20165 {
20166 /* The discriminator is not interesting to the
20167 debugger; just ignore it. We still need to
20168 check its value though:
20169 if there are consecutive entries for the same
20170 (non-prologue) line we want to coalesce them.
20171 PR 17276. */
20172 unsigned int discr
20173 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
20175
20176 state_machine.handle_set_discriminator (discr);
20177 }
20178 break;
20179 default:
20180 complaint (_("mangled .debug_line section"));
20181 return;
20182 }
20183 /* Make sure that we parsed the extended op correctly. If e.g.
20184 we expected a different address size than the producer used,
20185 we may have read the wrong number of bytes. */
20186 if (line_ptr != extended_end)
20187 {
20188 complaint (_("mangled .debug_line section"));
20189 return;
20190 }
20191 break;
20192 case DW_LNS_copy:
20193 state_machine.handle_copy ();
20194 break;
20195 case DW_LNS_advance_pc:
20196 {
20197 CORE_ADDR adjust
20198 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20199 line_ptr += bytes_read;
20200
20201 state_machine.handle_advance_pc (adjust);
20202 }
20203 break;
20204 case DW_LNS_advance_line:
20205 {
20206 int line_delta
20207 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20208 line_ptr += bytes_read;
20209
20210 state_machine.handle_advance_line (line_delta);
20211 }
20212 break;
20213 case DW_LNS_set_file:
20214 {
20215 file_name_index file
20216 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20217 &bytes_read);
20218 line_ptr += bytes_read;
20219
20220 state_machine.handle_set_file (file);
20221 }
20222 break;
20223 case DW_LNS_set_column:
20224 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20225 line_ptr += bytes_read;
20226 break;
20227 case DW_LNS_negate_stmt:
20228 state_machine.handle_negate_stmt ();
20229 break;
20230 case DW_LNS_set_basic_block:
20231 break;
20232 /* Add to the address register of the state machine the
20233 address increment value corresponding to special opcode
20234 255. I.e., this value is scaled by the minimum
20235 instruction length since special opcode 255 would have
20236 scaled the increment. */
20237 case DW_LNS_const_add_pc:
20238 state_machine.handle_const_add_pc ();
20239 break;
20240 case DW_LNS_fixed_advance_pc:
20241 {
20242 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20243 line_ptr += 2;
20244
20245 state_machine.handle_fixed_advance_pc (addr_adj);
20246 }
20247 break;
20248 default:
20249 {
20250 /* Unknown standard opcode, ignore it. */
20251 int i;
20252
20253 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20254 {
20255 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20256 line_ptr += bytes_read;
20257 }
20258 }
20259 }
20260 }
20261
20262 if (!end_sequence)
20263 dwarf2_debug_line_missing_end_sequence_complaint ();
20264
20265 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20266 in which case we still finish recording the last line). */
20267 state_machine.record_line (true);
20268 }
20269 }
20270
20271 /* Decode the Line Number Program (LNP) for the given line_header
20272 structure and CU. The actual information extracted and the type
20273 of structures created from the LNP depends on the value of PST.
20274
20275 1. If PST is NULL, then this procedure uses the data from the program
20276 to create all necessary symbol tables, and their linetables.
20277
20278 2. If PST is not NULL, this procedure reads the program to determine
20279 the list of files included by the unit represented by PST, and
20280 builds all the associated partial symbol tables.
20281
20282 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20283 It is used for relative paths in the line table.
20284 NOTE: When processing partial symtabs (pst != NULL),
20285 comp_dir == pst->dirname.
20286
20287 NOTE: It is important that psymtabs have the same file name (via strcmp)
20288 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20289 symtab we don't use it in the name of the psymtabs we create.
20290 E.g. expand_line_sal requires this when finding psymtabs to expand.
20291 A good testcase for this is mb-inline.exp.
20292
20293 LOWPC is the lowest address in CU (or 0 if not known).
20294
20295 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20296 for its PC<->lines mapping information. Otherwise only the filename
20297 table is read in. */
20298
20299 static void
20300 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20301 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20302 CORE_ADDR lowpc, int decode_mapping)
20303 {
20304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20305 const int decode_for_pst_p = (pst != NULL);
20306
20307 if (decode_mapping)
20308 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20309
20310 if (decode_for_pst_p)
20311 {
20312 /* Now that we're done scanning the Line Header Program, we can
20313 create the psymtab of each included file. */
20314 for (auto &file_entry : lh->file_names ())
20315 if (file_entry.included_p == 1)
20316 {
20317 gdb::unique_xmalloc_ptr<char> name_holder;
20318 const char *include_name =
20319 psymtab_include_file_name (lh, file_entry, pst,
20320 comp_dir, &name_holder);
20321 if (include_name != NULL)
20322 dwarf2_create_include_psymtab (include_name, pst, objfile);
20323 }
20324 }
20325 else
20326 {
20327 /* Make sure a symtab is created for every file, even files
20328 which contain only variables (i.e. no code with associated
20329 line numbers). */
20330 buildsym_compunit *builder = cu->get_builder ();
20331 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20332
20333 for (auto &fe : lh->file_names ())
20334 {
20335 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20336 if (builder->get_current_subfile ()->symtab == NULL)
20337 {
20338 builder->get_current_subfile ()->symtab
20339 = allocate_symtab (cust,
20340 builder->get_current_subfile ()->name);
20341 }
20342 fe.symtab = builder->get_current_subfile ()->symtab;
20343 }
20344 }
20345 }
20346
20347 /* Start a subfile for DWARF. FILENAME is the name of the file and
20348 DIRNAME the name of the source directory which contains FILENAME
20349 or NULL if not known.
20350 This routine tries to keep line numbers from identical absolute and
20351 relative file names in a common subfile.
20352
20353 Using the `list' example from the GDB testsuite, which resides in
20354 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20355 of /srcdir/list0.c yields the following debugging information for list0.c:
20356
20357 DW_AT_name: /srcdir/list0.c
20358 DW_AT_comp_dir: /compdir
20359 files.files[0].name: list0.h
20360 files.files[0].dir: /srcdir
20361 files.files[1].name: list0.c
20362 files.files[1].dir: /srcdir
20363
20364 The line number information for list0.c has to end up in a single
20365 subfile, so that `break /srcdir/list0.c:1' works as expected.
20366 start_subfile will ensure that this happens provided that we pass the
20367 concatenation of files.files[1].dir and files.files[1].name as the
20368 subfile's name. */
20369
20370 static void
20371 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20372 const char *dirname)
20373 {
20374 gdb::unique_xmalloc_ptr<char> copy;
20375
20376 /* In order not to lose the line information directory,
20377 we concatenate it to the filename when it makes sense.
20378 Note that the Dwarf3 standard says (speaking of filenames in line
20379 information): ``The directory index is ignored for file names
20380 that represent full path names''. Thus ignoring dirname in the
20381 `else' branch below isn't an issue. */
20382
20383 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20384 {
20385 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20386 filename = copy.get ();
20387 }
20388
20389 cu->get_builder ()->start_subfile (filename);
20390 }
20391
20392 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20393 buildsym_compunit constructor. */
20394
20395 struct compunit_symtab *
20396 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20397 CORE_ADDR low_pc)
20398 {
20399 gdb_assert (m_builder == nullptr);
20400
20401 m_builder.reset (new struct buildsym_compunit
20402 (per_cu->dwarf2_per_objfile->objfile,
20403 name, comp_dir, language, low_pc));
20404
20405 list_in_scope = get_builder ()->get_file_symbols ();
20406
20407 get_builder ()->record_debugformat ("DWARF 2");
20408 get_builder ()->record_producer (producer);
20409
20410 processing_has_namespace_info = false;
20411
20412 return get_builder ()->get_compunit_symtab ();
20413 }
20414
20415 static void
20416 var_decode_location (struct attribute *attr, struct symbol *sym,
20417 struct dwarf2_cu *cu)
20418 {
20419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20420 struct comp_unit_head *cu_header = &cu->header;
20421
20422 /* NOTE drow/2003-01-30: There used to be a comment and some special
20423 code here to turn a symbol with DW_AT_external and a
20424 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20425 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20426 with some versions of binutils) where shared libraries could have
20427 relocations against symbols in their debug information - the
20428 minimal symbol would have the right address, but the debug info
20429 would not. It's no longer necessary, because we will explicitly
20430 apply relocations when we read in the debug information now. */
20431
20432 /* A DW_AT_location attribute with no contents indicates that a
20433 variable has been optimized away. */
20434 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20435 {
20436 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20437 return;
20438 }
20439
20440 /* Handle one degenerate form of location expression specially, to
20441 preserve GDB's previous behavior when section offsets are
20442 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20443 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20444
20445 if (attr->form_is_block ()
20446 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20447 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20448 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20449 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20450 && (DW_BLOCK (attr)->size
20451 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20452 {
20453 unsigned int dummy;
20454
20455 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20456 SET_SYMBOL_VALUE_ADDRESS
20457 (sym, cu->header.read_address (objfile->obfd,
20458 DW_BLOCK (attr)->data + 1,
20459 &dummy));
20460 else
20461 SET_SYMBOL_VALUE_ADDRESS
20462 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20463 &dummy));
20464 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20465 fixup_symbol_section (sym, objfile);
20466 SET_SYMBOL_VALUE_ADDRESS
20467 (sym,
20468 SYMBOL_VALUE_ADDRESS (sym)
20469 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20470 return;
20471 }
20472
20473 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20474 expression evaluator, and use LOC_COMPUTED only when necessary
20475 (i.e. when the value of a register or memory location is
20476 referenced, or a thread-local block, etc.). Then again, it might
20477 not be worthwhile. I'm assuming that it isn't unless performance
20478 or memory numbers show me otherwise. */
20479
20480 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20481
20482 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20483 cu->has_loclist = true;
20484 }
20485
20486 /* Given a pointer to a DWARF information entry, figure out if we need
20487 to make a symbol table entry for it, and if so, create a new entry
20488 and return a pointer to it.
20489 If TYPE is NULL, determine symbol type from the die, otherwise
20490 used the passed type.
20491 If SPACE is not NULL, use it to hold the new symbol. If it is
20492 NULL, allocate a new symbol on the objfile's obstack. */
20493
20494 static struct symbol *
20495 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20496 struct symbol *space)
20497 {
20498 struct dwarf2_per_objfile *dwarf2_per_objfile
20499 = cu->per_cu->dwarf2_per_objfile;
20500 struct objfile *objfile = dwarf2_per_objfile->objfile;
20501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20502 struct symbol *sym = NULL;
20503 const char *name;
20504 struct attribute *attr = NULL;
20505 struct attribute *attr2 = NULL;
20506 CORE_ADDR baseaddr;
20507 struct pending **list_to_add = NULL;
20508
20509 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20510
20511 baseaddr = objfile->text_section_offset ();
20512
20513 name = dwarf2_name (die, cu);
20514 if (name)
20515 {
20516 const char *linkagename;
20517 int suppress_add = 0;
20518
20519 if (space)
20520 sym = space;
20521 else
20522 sym = allocate_symbol (objfile);
20523 OBJSTAT (objfile, n_syms++);
20524
20525 /* Cache this symbol's name and the name's demangled form (if any). */
20526 sym->set_language (cu->language, &objfile->objfile_obstack);
20527 linkagename = dwarf2_physname (name, die, cu);
20528 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20529
20530 /* Fortran does not have mangling standard and the mangling does differ
20531 between gfortran, iFort etc. */
20532 if (cu->language == language_fortran
20533 && symbol_get_demangled_name (sym) == NULL)
20534 symbol_set_demangled_name (sym,
20535 dwarf2_full_name (name, die, cu),
20536 NULL);
20537
20538 /* Default assumptions.
20539 Use the passed type or decode it from the die. */
20540 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20541 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20542 if (type != NULL)
20543 SYMBOL_TYPE (sym) = type;
20544 else
20545 SYMBOL_TYPE (sym) = die_type (die, cu);
20546 attr = dwarf2_attr (die,
20547 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20548 cu);
20549 if (attr != nullptr)
20550 {
20551 SYMBOL_LINE (sym) = DW_UNSND (attr);
20552 }
20553
20554 attr = dwarf2_attr (die,
20555 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20556 cu);
20557 if (attr != nullptr)
20558 {
20559 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20560 struct file_entry *fe;
20561
20562 if (cu->line_header != NULL)
20563 fe = cu->line_header->file_name_at (file_index);
20564 else
20565 fe = NULL;
20566
20567 if (fe == NULL)
20568 complaint (_("file index out of range"));
20569 else
20570 symbol_set_symtab (sym, fe->symtab);
20571 }
20572
20573 switch (die->tag)
20574 {
20575 case DW_TAG_label:
20576 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20577 if (attr != nullptr)
20578 {
20579 CORE_ADDR addr;
20580
20581 addr = attr->value_as_address ();
20582 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20583 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20584 }
20585 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20586 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20587 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20588 add_symbol_to_list (sym, cu->list_in_scope);
20589 break;
20590 case DW_TAG_subprogram:
20591 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20592 finish_block. */
20593 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20594 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20595 if ((attr2 && (DW_UNSND (attr2) != 0))
20596 || cu->language == language_ada
20597 || cu->language == language_fortran)
20598 {
20599 /* Subprograms marked external are stored as a global symbol.
20600 Ada and Fortran subprograms, whether marked external or
20601 not, are always stored as a global symbol, because we want
20602 to be able to access them globally. For instance, we want
20603 to be able to break on a nested subprogram without having
20604 to specify the context. */
20605 list_to_add = cu->get_builder ()->get_global_symbols ();
20606 }
20607 else
20608 {
20609 list_to_add = cu->list_in_scope;
20610 }
20611 break;
20612 case DW_TAG_inlined_subroutine:
20613 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20614 finish_block. */
20615 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20616 SYMBOL_INLINED (sym) = 1;
20617 list_to_add = cu->list_in_scope;
20618 break;
20619 case DW_TAG_template_value_param:
20620 suppress_add = 1;
20621 /* Fall through. */
20622 case DW_TAG_constant:
20623 case DW_TAG_variable:
20624 case DW_TAG_member:
20625 /* Compilation with minimal debug info may result in
20626 variables with missing type entries. Change the
20627 misleading `void' type to something sensible. */
20628 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20629 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20630
20631 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20632 /* In the case of DW_TAG_member, we should only be called for
20633 static const members. */
20634 if (die->tag == DW_TAG_member)
20635 {
20636 /* dwarf2_add_field uses die_is_declaration,
20637 so we do the same. */
20638 gdb_assert (die_is_declaration (die, cu));
20639 gdb_assert (attr);
20640 }
20641 if (attr != nullptr)
20642 {
20643 dwarf2_const_value (attr, sym, cu);
20644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20645 if (!suppress_add)
20646 {
20647 if (attr2 && (DW_UNSND (attr2) != 0))
20648 list_to_add = cu->get_builder ()->get_global_symbols ();
20649 else
20650 list_to_add = cu->list_in_scope;
20651 }
20652 break;
20653 }
20654 attr = dwarf2_attr (die, DW_AT_location, cu);
20655 if (attr != nullptr)
20656 {
20657 var_decode_location (attr, sym, cu);
20658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20659
20660 /* Fortran explicitly imports any global symbols to the local
20661 scope by DW_TAG_common_block. */
20662 if (cu->language == language_fortran && die->parent
20663 && die->parent->tag == DW_TAG_common_block)
20664 attr2 = NULL;
20665
20666 if (SYMBOL_CLASS (sym) == LOC_STATIC
20667 && SYMBOL_VALUE_ADDRESS (sym) == 0
20668 && !dwarf2_per_objfile->has_section_at_zero)
20669 {
20670 /* When a static variable is eliminated by the linker,
20671 the corresponding debug information is not stripped
20672 out, but the variable address is set to null;
20673 do not add such variables into symbol table. */
20674 }
20675 else if (attr2 && (DW_UNSND (attr2) != 0))
20676 {
20677 if (SYMBOL_CLASS (sym) == LOC_STATIC
20678 && (objfile->flags & OBJF_MAINLINE) == 0
20679 && dwarf2_per_objfile->can_copy)
20680 {
20681 /* A global static variable might be subject to
20682 copy relocation. We first check for a local
20683 minsym, though, because maybe the symbol was
20684 marked hidden, in which case this would not
20685 apply. */
20686 bound_minimal_symbol found
20687 = (lookup_minimal_symbol_linkage
20688 (sym->linkage_name (), objfile));
20689 if (found.minsym != nullptr)
20690 sym->maybe_copied = 1;
20691 }
20692
20693 /* A variable with DW_AT_external is never static,
20694 but it may be block-scoped. */
20695 list_to_add
20696 = ((cu->list_in_scope
20697 == cu->get_builder ()->get_file_symbols ())
20698 ? cu->get_builder ()->get_global_symbols ()
20699 : cu->list_in_scope);
20700 }
20701 else
20702 list_to_add = cu->list_in_scope;
20703 }
20704 else
20705 {
20706 /* We do not know the address of this symbol.
20707 If it is an external symbol and we have type information
20708 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20709 The address of the variable will then be determined from
20710 the minimal symbol table whenever the variable is
20711 referenced. */
20712 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20713
20714 /* Fortran explicitly imports any global symbols to the local
20715 scope by DW_TAG_common_block. */
20716 if (cu->language == language_fortran && die->parent
20717 && die->parent->tag == DW_TAG_common_block)
20718 {
20719 /* SYMBOL_CLASS doesn't matter here because
20720 read_common_block is going to reset it. */
20721 if (!suppress_add)
20722 list_to_add = cu->list_in_scope;
20723 }
20724 else if (attr2 && (DW_UNSND (attr2) != 0)
20725 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20726 {
20727 /* A variable with DW_AT_external is never static, but it
20728 may be block-scoped. */
20729 list_to_add
20730 = ((cu->list_in_scope
20731 == cu->get_builder ()->get_file_symbols ())
20732 ? cu->get_builder ()->get_global_symbols ()
20733 : cu->list_in_scope);
20734
20735 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20736 }
20737 else if (!die_is_declaration (die, cu))
20738 {
20739 /* Use the default LOC_OPTIMIZED_OUT class. */
20740 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20741 if (!suppress_add)
20742 list_to_add = cu->list_in_scope;
20743 }
20744 }
20745 break;
20746 case DW_TAG_formal_parameter:
20747 {
20748 /* If we are inside a function, mark this as an argument. If
20749 not, we might be looking at an argument to an inlined function
20750 when we do not have enough information to show inlined frames;
20751 pretend it's a local variable in that case so that the user can
20752 still see it. */
20753 struct context_stack *curr
20754 = cu->get_builder ()->get_current_context_stack ();
20755 if (curr != nullptr && curr->name != nullptr)
20756 SYMBOL_IS_ARGUMENT (sym) = 1;
20757 attr = dwarf2_attr (die, DW_AT_location, cu);
20758 if (attr != nullptr)
20759 {
20760 var_decode_location (attr, sym, cu);
20761 }
20762 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20763 if (attr != nullptr)
20764 {
20765 dwarf2_const_value (attr, sym, cu);
20766 }
20767
20768 list_to_add = cu->list_in_scope;
20769 }
20770 break;
20771 case DW_TAG_unspecified_parameters:
20772 /* From varargs functions; gdb doesn't seem to have any
20773 interest in this information, so just ignore it for now.
20774 (FIXME?) */
20775 break;
20776 case DW_TAG_template_type_param:
20777 suppress_add = 1;
20778 /* Fall through. */
20779 case DW_TAG_class_type:
20780 case DW_TAG_interface_type:
20781 case DW_TAG_structure_type:
20782 case DW_TAG_union_type:
20783 case DW_TAG_set_type:
20784 case DW_TAG_enumeration_type:
20785 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20786 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20787
20788 {
20789 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20790 really ever be static objects: otherwise, if you try
20791 to, say, break of a class's method and you're in a file
20792 which doesn't mention that class, it won't work unless
20793 the check for all static symbols in lookup_symbol_aux
20794 saves you. See the OtherFileClass tests in
20795 gdb.c++/namespace.exp. */
20796
20797 if (!suppress_add)
20798 {
20799 buildsym_compunit *builder = cu->get_builder ();
20800 list_to_add
20801 = (cu->list_in_scope == builder->get_file_symbols ()
20802 && cu->language == language_cplus
20803 ? builder->get_global_symbols ()
20804 : cu->list_in_scope);
20805
20806 /* The semantics of C++ state that "struct foo {
20807 ... }" also defines a typedef for "foo". */
20808 if (cu->language == language_cplus
20809 || cu->language == language_ada
20810 || cu->language == language_d
20811 || cu->language == language_rust)
20812 {
20813 /* The symbol's name is already allocated along
20814 with this objfile, so we don't need to
20815 duplicate it for the type. */
20816 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20817 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20818 }
20819 }
20820 }
20821 break;
20822 case DW_TAG_typedef:
20823 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20824 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20825 list_to_add = cu->list_in_scope;
20826 break;
20827 case DW_TAG_base_type:
20828 case DW_TAG_subrange_type:
20829 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20831 list_to_add = cu->list_in_scope;
20832 break;
20833 case DW_TAG_enumerator:
20834 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20835 if (attr != nullptr)
20836 {
20837 dwarf2_const_value (attr, sym, cu);
20838 }
20839 {
20840 /* NOTE: carlton/2003-11-10: See comment above in the
20841 DW_TAG_class_type, etc. block. */
20842
20843 list_to_add
20844 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20845 && cu->language == language_cplus
20846 ? cu->get_builder ()->get_global_symbols ()
20847 : cu->list_in_scope);
20848 }
20849 break;
20850 case DW_TAG_imported_declaration:
20851 case DW_TAG_namespace:
20852 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20853 list_to_add = cu->get_builder ()->get_global_symbols ();
20854 break;
20855 case DW_TAG_module:
20856 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20857 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20858 list_to_add = cu->get_builder ()->get_global_symbols ();
20859 break;
20860 case DW_TAG_common_block:
20861 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20862 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20863 add_symbol_to_list (sym, cu->list_in_scope);
20864 break;
20865 default:
20866 /* Not a tag we recognize. Hopefully we aren't processing
20867 trash data, but since we must specifically ignore things
20868 we don't recognize, there is nothing else we should do at
20869 this point. */
20870 complaint (_("unsupported tag: '%s'"),
20871 dwarf_tag_name (die->tag));
20872 break;
20873 }
20874
20875 if (suppress_add)
20876 {
20877 sym->hash_next = objfile->template_symbols;
20878 objfile->template_symbols = sym;
20879 list_to_add = NULL;
20880 }
20881
20882 if (list_to_add != NULL)
20883 add_symbol_to_list (sym, list_to_add);
20884
20885 /* For the benefit of old versions of GCC, check for anonymous
20886 namespaces based on the demangled name. */
20887 if (!cu->processing_has_namespace_info
20888 && cu->language == language_cplus)
20889 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20890 }
20891 return (sym);
20892 }
20893
20894 /* Given an attr with a DW_FORM_dataN value in host byte order,
20895 zero-extend it as appropriate for the symbol's type. The DWARF
20896 standard (v4) is not entirely clear about the meaning of using
20897 DW_FORM_dataN for a constant with a signed type, where the type is
20898 wider than the data. The conclusion of a discussion on the DWARF
20899 list was that this is unspecified. We choose to always zero-extend
20900 because that is the interpretation long in use by GCC. */
20901
20902 static gdb_byte *
20903 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20904 struct dwarf2_cu *cu, LONGEST *value, int bits)
20905 {
20906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20907 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20908 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20909 LONGEST l = DW_UNSND (attr);
20910
20911 if (bits < sizeof (*value) * 8)
20912 {
20913 l &= ((LONGEST) 1 << bits) - 1;
20914 *value = l;
20915 }
20916 else if (bits == sizeof (*value) * 8)
20917 *value = l;
20918 else
20919 {
20920 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20921 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20922 return bytes;
20923 }
20924
20925 return NULL;
20926 }
20927
20928 /* Read a constant value from an attribute. Either set *VALUE, or if
20929 the value does not fit in *VALUE, set *BYTES - either already
20930 allocated on the objfile obstack, or newly allocated on OBSTACK,
20931 or, set *BATON, if we translated the constant to a location
20932 expression. */
20933
20934 static void
20935 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20936 const char *name, struct obstack *obstack,
20937 struct dwarf2_cu *cu,
20938 LONGEST *value, const gdb_byte **bytes,
20939 struct dwarf2_locexpr_baton **baton)
20940 {
20941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20942 struct comp_unit_head *cu_header = &cu->header;
20943 struct dwarf_block *blk;
20944 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20945 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20946
20947 *value = 0;
20948 *bytes = NULL;
20949 *baton = NULL;
20950
20951 switch (attr->form)
20952 {
20953 case DW_FORM_addr:
20954 case DW_FORM_addrx:
20955 case DW_FORM_GNU_addr_index:
20956 {
20957 gdb_byte *data;
20958
20959 if (TYPE_LENGTH (type) != cu_header->addr_size)
20960 dwarf2_const_value_length_mismatch_complaint (name,
20961 cu_header->addr_size,
20962 TYPE_LENGTH (type));
20963 /* Symbols of this form are reasonably rare, so we just
20964 piggyback on the existing location code rather than writing
20965 a new implementation of symbol_computed_ops. */
20966 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20967 (*baton)->per_cu = cu->per_cu;
20968 gdb_assert ((*baton)->per_cu);
20969
20970 (*baton)->size = 2 + cu_header->addr_size;
20971 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20972 (*baton)->data = data;
20973
20974 data[0] = DW_OP_addr;
20975 store_unsigned_integer (&data[1], cu_header->addr_size,
20976 byte_order, DW_ADDR (attr));
20977 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20978 }
20979 break;
20980 case DW_FORM_string:
20981 case DW_FORM_strp:
20982 case DW_FORM_strx:
20983 case DW_FORM_GNU_str_index:
20984 case DW_FORM_GNU_strp_alt:
20985 /* DW_STRING is already allocated on the objfile obstack, point
20986 directly to it. */
20987 *bytes = (const gdb_byte *) DW_STRING (attr);
20988 break;
20989 case DW_FORM_block1:
20990 case DW_FORM_block2:
20991 case DW_FORM_block4:
20992 case DW_FORM_block:
20993 case DW_FORM_exprloc:
20994 case DW_FORM_data16:
20995 blk = DW_BLOCK (attr);
20996 if (TYPE_LENGTH (type) != blk->size)
20997 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20998 TYPE_LENGTH (type));
20999 *bytes = blk->data;
21000 break;
21001
21002 /* The DW_AT_const_value attributes are supposed to carry the
21003 symbol's value "represented as it would be on the target
21004 architecture." By the time we get here, it's already been
21005 converted to host endianness, so we just need to sign- or
21006 zero-extend it as appropriate. */
21007 case DW_FORM_data1:
21008 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21009 break;
21010 case DW_FORM_data2:
21011 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21012 break;
21013 case DW_FORM_data4:
21014 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21015 break;
21016 case DW_FORM_data8:
21017 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21018 break;
21019
21020 case DW_FORM_sdata:
21021 case DW_FORM_implicit_const:
21022 *value = DW_SND (attr);
21023 break;
21024
21025 case DW_FORM_udata:
21026 *value = DW_UNSND (attr);
21027 break;
21028
21029 default:
21030 complaint (_("unsupported const value attribute form: '%s'"),
21031 dwarf_form_name (attr->form));
21032 *value = 0;
21033 break;
21034 }
21035 }
21036
21037
21038 /* Copy constant value from an attribute to a symbol. */
21039
21040 static void
21041 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21042 struct dwarf2_cu *cu)
21043 {
21044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21045 LONGEST value;
21046 const gdb_byte *bytes;
21047 struct dwarf2_locexpr_baton *baton;
21048
21049 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21050 sym->print_name (),
21051 &objfile->objfile_obstack, cu,
21052 &value, &bytes, &baton);
21053
21054 if (baton != NULL)
21055 {
21056 SYMBOL_LOCATION_BATON (sym) = baton;
21057 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21058 }
21059 else if (bytes != NULL)
21060 {
21061 SYMBOL_VALUE_BYTES (sym) = bytes;
21062 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21063 }
21064 else
21065 {
21066 SYMBOL_VALUE (sym) = value;
21067 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21068 }
21069 }
21070
21071 /* Return the type of the die in question using its DW_AT_type attribute. */
21072
21073 static struct type *
21074 die_type (struct die_info *die, struct dwarf2_cu *cu)
21075 {
21076 struct attribute *type_attr;
21077
21078 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21079 if (!type_attr)
21080 {
21081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21082 /* A missing DW_AT_type represents a void type. */
21083 return objfile_type (objfile)->builtin_void;
21084 }
21085
21086 return lookup_die_type (die, type_attr, cu);
21087 }
21088
21089 /* True iff CU's producer generates GNAT Ada auxiliary information
21090 that allows to find parallel types through that information instead
21091 of having to do expensive parallel lookups by type name. */
21092
21093 static int
21094 need_gnat_info (struct dwarf2_cu *cu)
21095 {
21096 /* Assume that the Ada compiler was GNAT, which always produces
21097 the auxiliary information. */
21098 return (cu->language == language_ada);
21099 }
21100
21101 /* Return the auxiliary type of the die in question using its
21102 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21103 attribute is not present. */
21104
21105 static struct type *
21106 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21107 {
21108 struct attribute *type_attr;
21109
21110 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21111 if (!type_attr)
21112 return NULL;
21113
21114 return lookup_die_type (die, type_attr, cu);
21115 }
21116
21117 /* If DIE has a descriptive_type attribute, then set the TYPE's
21118 descriptive type accordingly. */
21119
21120 static void
21121 set_descriptive_type (struct type *type, struct die_info *die,
21122 struct dwarf2_cu *cu)
21123 {
21124 struct type *descriptive_type = die_descriptive_type (die, cu);
21125
21126 if (descriptive_type)
21127 {
21128 ALLOCATE_GNAT_AUX_TYPE (type);
21129 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21130 }
21131 }
21132
21133 /* Return the containing type of the die in question using its
21134 DW_AT_containing_type attribute. */
21135
21136 static struct type *
21137 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21138 {
21139 struct attribute *type_attr;
21140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21141
21142 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21143 if (!type_attr)
21144 error (_("Dwarf Error: Problem turning containing type into gdb type "
21145 "[in module %s]"), objfile_name (objfile));
21146
21147 return lookup_die_type (die, type_attr, cu);
21148 }
21149
21150 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21151
21152 static struct type *
21153 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21154 {
21155 struct dwarf2_per_objfile *dwarf2_per_objfile
21156 = cu->per_cu->dwarf2_per_objfile;
21157 struct objfile *objfile = dwarf2_per_objfile->objfile;
21158 char *saved;
21159
21160 std::string message
21161 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21162 objfile_name (objfile),
21163 sect_offset_str (cu->header.sect_off),
21164 sect_offset_str (die->sect_off));
21165 saved = obstack_strdup (&objfile->objfile_obstack, message);
21166
21167 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21168 }
21169
21170 /* Look up the type of DIE in CU using its type attribute ATTR.
21171 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21172 DW_AT_containing_type.
21173 If there is no type substitute an error marker. */
21174
21175 static struct type *
21176 lookup_die_type (struct die_info *die, const struct attribute *attr,
21177 struct dwarf2_cu *cu)
21178 {
21179 struct dwarf2_per_objfile *dwarf2_per_objfile
21180 = cu->per_cu->dwarf2_per_objfile;
21181 struct objfile *objfile = dwarf2_per_objfile->objfile;
21182 struct type *this_type;
21183
21184 gdb_assert (attr->name == DW_AT_type
21185 || attr->name == DW_AT_GNAT_descriptive_type
21186 || attr->name == DW_AT_containing_type);
21187
21188 /* First see if we have it cached. */
21189
21190 if (attr->form == DW_FORM_GNU_ref_alt)
21191 {
21192 struct dwarf2_per_cu_data *per_cu;
21193 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21194
21195 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21196 dwarf2_per_objfile);
21197 this_type = get_die_type_at_offset (sect_off, per_cu);
21198 }
21199 else if (attr->form_is_ref ())
21200 {
21201 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21202
21203 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21204 }
21205 else if (attr->form == DW_FORM_ref_sig8)
21206 {
21207 ULONGEST signature = DW_SIGNATURE (attr);
21208
21209 return get_signatured_type (die, signature, cu);
21210 }
21211 else
21212 {
21213 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21214 " at %s [in module %s]"),
21215 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21216 objfile_name (objfile));
21217 return build_error_marker_type (cu, die);
21218 }
21219
21220 /* If not cached we need to read it in. */
21221
21222 if (this_type == NULL)
21223 {
21224 struct die_info *type_die = NULL;
21225 struct dwarf2_cu *type_cu = cu;
21226
21227 if (attr->form_is_ref ())
21228 type_die = follow_die_ref (die, attr, &type_cu);
21229 if (type_die == NULL)
21230 return build_error_marker_type (cu, die);
21231 /* If we find the type now, it's probably because the type came
21232 from an inter-CU reference and the type's CU got expanded before
21233 ours. */
21234 this_type = read_type_die (type_die, type_cu);
21235 }
21236
21237 /* If we still don't have a type use an error marker. */
21238
21239 if (this_type == NULL)
21240 return build_error_marker_type (cu, die);
21241
21242 return this_type;
21243 }
21244
21245 /* Return the type in DIE, CU.
21246 Returns NULL for invalid types.
21247
21248 This first does a lookup in die_type_hash,
21249 and only reads the die in if necessary.
21250
21251 NOTE: This can be called when reading in partial or full symbols. */
21252
21253 static struct type *
21254 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21255 {
21256 struct type *this_type;
21257
21258 this_type = get_die_type (die, cu);
21259 if (this_type)
21260 return this_type;
21261
21262 return read_type_die_1 (die, cu);
21263 }
21264
21265 /* Read the type in DIE, CU.
21266 Returns NULL for invalid types. */
21267
21268 static struct type *
21269 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21270 {
21271 struct type *this_type = NULL;
21272
21273 switch (die->tag)
21274 {
21275 case DW_TAG_class_type:
21276 case DW_TAG_interface_type:
21277 case DW_TAG_structure_type:
21278 case DW_TAG_union_type:
21279 this_type = read_structure_type (die, cu);
21280 break;
21281 case DW_TAG_enumeration_type:
21282 this_type = read_enumeration_type (die, cu);
21283 break;
21284 case DW_TAG_subprogram:
21285 case DW_TAG_subroutine_type:
21286 case DW_TAG_inlined_subroutine:
21287 this_type = read_subroutine_type (die, cu);
21288 break;
21289 case DW_TAG_array_type:
21290 this_type = read_array_type (die, cu);
21291 break;
21292 case DW_TAG_set_type:
21293 this_type = read_set_type (die, cu);
21294 break;
21295 case DW_TAG_pointer_type:
21296 this_type = read_tag_pointer_type (die, cu);
21297 break;
21298 case DW_TAG_ptr_to_member_type:
21299 this_type = read_tag_ptr_to_member_type (die, cu);
21300 break;
21301 case DW_TAG_reference_type:
21302 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21303 break;
21304 case DW_TAG_rvalue_reference_type:
21305 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21306 break;
21307 case DW_TAG_const_type:
21308 this_type = read_tag_const_type (die, cu);
21309 break;
21310 case DW_TAG_volatile_type:
21311 this_type = read_tag_volatile_type (die, cu);
21312 break;
21313 case DW_TAG_restrict_type:
21314 this_type = read_tag_restrict_type (die, cu);
21315 break;
21316 case DW_TAG_string_type:
21317 this_type = read_tag_string_type (die, cu);
21318 break;
21319 case DW_TAG_typedef:
21320 this_type = read_typedef (die, cu);
21321 break;
21322 case DW_TAG_subrange_type:
21323 this_type = read_subrange_type (die, cu);
21324 break;
21325 case DW_TAG_base_type:
21326 this_type = read_base_type (die, cu);
21327 break;
21328 case DW_TAG_unspecified_type:
21329 this_type = read_unspecified_type (die, cu);
21330 break;
21331 case DW_TAG_namespace:
21332 this_type = read_namespace_type (die, cu);
21333 break;
21334 case DW_TAG_module:
21335 this_type = read_module_type (die, cu);
21336 break;
21337 case DW_TAG_atomic_type:
21338 this_type = read_tag_atomic_type (die, cu);
21339 break;
21340 default:
21341 complaint (_("unexpected tag in read_type_die: '%s'"),
21342 dwarf_tag_name (die->tag));
21343 break;
21344 }
21345
21346 return this_type;
21347 }
21348
21349 /* See if we can figure out if the class lives in a namespace. We do
21350 this by looking for a member function; its demangled name will
21351 contain namespace info, if there is any.
21352 Return the computed name or NULL.
21353 Space for the result is allocated on the objfile's obstack.
21354 This is the full-die version of guess_partial_die_structure_name.
21355 In this case we know DIE has no useful parent. */
21356
21357 static const char *
21358 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21359 {
21360 struct die_info *spec_die;
21361 struct dwarf2_cu *spec_cu;
21362 struct die_info *child;
21363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21364
21365 spec_cu = cu;
21366 spec_die = die_specification (die, &spec_cu);
21367 if (spec_die != NULL)
21368 {
21369 die = spec_die;
21370 cu = spec_cu;
21371 }
21372
21373 for (child = die->child;
21374 child != NULL;
21375 child = child->sibling)
21376 {
21377 if (child->tag == DW_TAG_subprogram)
21378 {
21379 const char *linkage_name = dw2_linkage_name (child, cu);
21380
21381 if (linkage_name != NULL)
21382 {
21383 gdb::unique_xmalloc_ptr<char> actual_name
21384 (language_class_name_from_physname (cu->language_defn,
21385 linkage_name));
21386 const char *name = NULL;
21387
21388 if (actual_name != NULL)
21389 {
21390 const char *die_name = dwarf2_name (die, cu);
21391
21392 if (die_name != NULL
21393 && strcmp (die_name, actual_name.get ()) != 0)
21394 {
21395 /* Strip off the class name from the full name.
21396 We want the prefix. */
21397 int die_name_len = strlen (die_name);
21398 int actual_name_len = strlen (actual_name.get ());
21399 const char *ptr = actual_name.get ();
21400
21401 /* Test for '::' as a sanity check. */
21402 if (actual_name_len > die_name_len + 2
21403 && ptr[actual_name_len - die_name_len - 1] == ':')
21404 name = obstack_strndup (
21405 &objfile->per_bfd->storage_obstack,
21406 ptr, actual_name_len - die_name_len - 2);
21407 }
21408 }
21409 return name;
21410 }
21411 }
21412 }
21413
21414 return NULL;
21415 }
21416
21417 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21418 prefix part in such case. See
21419 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21420
21421 static const char *
21422 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21423 {
21424 struct attribute *attr;
21425 const char *base;
21426
21427 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21428 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21429 return NULL;
21430
21431 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21432 return NULL;
21433
21434 attr = dw2_linkage_name_attr (die, cu);
21435 if (attr == NULL || DW_STRING (attr) == NULL)
21436 return NULL;
21437
21438 /* dwarf2_name had to be already called. */
21439 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21440
21441 /* Strip the base name, keep any leading namespaces/classes. */
21442 base = strrchr (DW_STRING (attr), ':');
21443 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21444 return "";
21445
21446 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21447 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21448 DW_STRING (attr),
21449 &base[-1] - DW_STRING (attr));
21450 }
21451
21452 /* Return the name of the namespace/class that DIE is defined within,
21453 or "" if we can't tell. The caller should not xfree the result.
21454
21455 For example, if we're within the method foo() in the following
21456 code:
21457
21458 namespace N {
21459 class C {
21460 void foo () {
21461 }
21462 };
21463 }
21464
21465 then determine_prefix on foo's die will return "N::C". */
21466
21467 static const char *
21468 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21469 {
21470 struct dwarf2_per_objfile *dwarf2_per_objfile
21471 = cu->per_cu->dwarf2_per_objfile;
21472 struct die_info *parent, *spec_die;
21473 struct dwarf2_cu *spec_cu;
21474 struct type *parent_type;
21475 const char *retval;
21476
21477 if (cu->language != language_cplus
21478 && cu->language != language_fortran && cu->language != language_d
21479 && cu->language != language_rust)
21480 return "";
21481
21482 retval = anonymous_struct_prefix (die, cu);
21483 if (retval)
21484 return retval;
21485
21486 /* We have to be careful in the presence of DW_AT_specification.
21487 For example, with GCC 3.4, given the code
21488
21489 namespace N {
21490 void foo() {
21491 // Definition of N::foo.
21492 }
21493 }
21494
21495 then we'll have a tree of DIEs like this:
21496
21497 1: DW_TAG_compile_unit
21498 2: DW_TAG_namespace // N
21499 3: DW_TAG_subprogram // declaration of N::foo
21500 4: DW_TAG_subprogram // definition of N::foo
21501 DW_AT_specification // refers to die #3
21502
21503 Thus, when processing die #4, we have to pretend that we're in
21504 the context of its DW_AT_specification, namely the contex of die
21505 #3. */
21506 spec_cu = cu;
21507 spec_die = die_specification (die, &spec_cu);
21508 if (spec_die == NULL)
21509 parent = die->parent;
21510 else
21511 {
21512 parent = spec_die->parent;
21513 cu = spec_cu;
21514 }
21515
21516 if (parent == NULL)
21517 return "";
21518 else if (parent->building_fullname)
21519 {
21520 const char *name;
21521 const char *parent_name;
21522
21523 /* It has been seen on RealView 2.2 built binaries,
21524 DW_TAG_template_type_param types actually _defined_ as
21525 children of the parent class:
21526
21527 enum E {};
21528 template class <class Enum> Class{};
21529 Class<enum E> class_e;
21530
21531 1: DW_TAG_class_type (Class)
21532 2: DW_TAG_enumeration_type (E)
21533 3: DW_TAG_enumerator (enum1:0)
21534 3: DW_TAG_enumerator (enum2:1)
21535 ...
21536 2: DW_TAG_template_type_param
21537 DW_AT_type DW_FORM_ref_udata (E)
21538
21539 Besides being broken debug info, it can put GDB into an
21540 infinite loop. Consider:
21541
21542 When we're building the full name for Class<E>, we'll start
21543 at Class, and go look over its template type parameters,
21544 finding E. We'll then try to build the full name of E, and
21545 reach here. We're now trying to build the full name of E,
21546 and look over the parent DIE for containing scope. In the
21547 broken case, if we followed the parent DIE of E, we'd again
21548 find Class, and once again go look at its template type
21549 arguments, etc., etc. Simply don't consider such parent die
21550 as source-level parent of this die (it can't be, the language
21551 doesn't allow it), and break the loop here. */
21552 name = dwarf2_name (die, cu);
21553 parent_name = dwarf2_name (parent, cu);
21554 complaint (_("template param type '%s' defined within parent '%s'"),
21555 name ? name : "<unknown>",
21556 parent_name ? parent_name : "<unknown>");
21557 return "";
21558 }
21559 else
21560 switch (parent->tag)
21561 {
21562 case DW_TAG_namespace:
21563 parent_type = read_type_die (parent, cu);
21564 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21565 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21566 Work around this problem here. */
21567 if (cu->language == language_cplus
21568 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21569 return "";
21570 /* We give a name to even anonymous namespaces. */
21571 return TYPE_NAME (parent_type);
21572 case DW_TAG_class_type:
21573 case DW_TAG_interface_type:
21574 case DW_TAG_structure_type:
21575 case DW_TAG_union_type:
21576 case DW_TAG_module:
21577 parent_type = read_type_die (parent, cu);
21578 if (TYPE_NAME (parent_type) != NULL)
21579 return TYPE_NAME (parent_type);
21580 else
21581 /* An anonymous structure is only allowed non-static data
21582 members; no typedefs, no member functions, et cetera.
21583 So it does not need a prefix. */
21584 return "";
21585 case DW_TAG_compile_unit:
21586 case DW_TAG_partial_unit:
21587 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21588 if (cu->language == language_cplus
21589 && !dwarf2_per_objfile->types.empty ()
21590 && die->child != NULL
21591 && (die->tag == DW_TAG_class_type
21592 || die->tag == DW_TAG_structure_type
21593 || die->tag == DW_TAG_union_type))
21594 {
21595 const char *name = guess_full_die_structure_name (die, cu);
21596 if (name != NULL)
21597 return name;
21598 }
21599 return "";
21600 case DW_TAG_subprogram:
21601 /* Nested subroutines in Fortran get a prefix with the name
21602 of the parent's subroutine. */
21603 if (cu->language == language_fortran)
21604 {
21605 if ((die->tag == DW_TAG_subprogram)
21606 && (dwarf2_name (parent, cu) != NULL))
21607 return dwarf2_name (parent, cu);
21608 }
21609 return determine_prefix (parent, cu);
21610 case DW_TAG_enumeration_type:
21611 parent_type = read_type_die (parent, cu);
21612 if (TYPE_DECLARED_CLASS (parent_type))
21613 {
21614 if (TYPE_NAME (parent_type) != NULL)
21615 return TYPE_NAME (parent_type);
21616 return "";
21617 }
21618 /* Fall through. */
21619 default:
21620 return determine_prefix (parent, cu);
21621 }
21622 }
21623
21624 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21625 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21626 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21627 an obconcat, otherwise allocate storage for the result. The CU argument is
21628 used to determine the language and hence, the appropriate separator. */
21629
21630 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21631
21632 static char *
21633 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21634 int physname, struct dwarf2_cu *cu)
21635 {
21636 const char *lead = "";
21637 const char *sep;
21638
21639 if (suffix == NULL || suffix[0] == '\0'
21640 || prefix == NULL || prefix[0] == '\0')
21641 sep = "";
21642 else if (cu->language == language_d)
21643 {
21644 /* For D, the 'main' function could be defined in any module, but it
21645 should never be prefixed. */
21646 if (strcmp (suffix, "D main") == 0)
21647 {
21648 prefix = "";
21649 sep = "";
21650 }
21651 else
21652 sep = ".";
21653 }
21654 else if (cu->language == language_fortran && physname)
21655 {
21656 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21657 DW_AT_MIPS_linkage_name is preferred and used instead. */
21658
21659 lead = "__";
21660 sep = "_MOD_";
21661 }
21662 else
21663 sep = "::";
21664
21665 if (prefix == NULL)
21666 prefix = "";
21667 if (suffix == NULL)
21668 suffix = "";
21669
21670 if (obs == NULL)
21671 {
21672 char *retval
21673 = ((char *)
21674 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21675
21676 strcpy (retval, lead);
21677 strcat (retval, prefix);
21678 strcat (retval, sep);
21679 strcat (retval, suffix);
21680 return retval;
21681 }
21682 else
21683 {
21684 /* We have an obstack. */
21685 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21686 }
21687 }
21688
21689 /* Return sibling of die, NULL if no sibling. */
21690
21691 static struct die_info *
21692 sibling_die (struct die_info *die)
21693 {
21694 return die->sibling;
21695 }
21696
21697 /* Get name of a die, return NULL if not found. */
21698
21699 static const char *
21700 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21701 struct obstack *obstack)
21702 {
21703 if (name && cu->language == language_cplus)
21704 {
21705 std::string canon_name = cp_canonicalize_string (name);
21706
21707 if (!canon_name.empty ())
21708 {
21709 if (canon_name != name)
21710 name = obstack_strdup (obstack, canon_name);
21711 }
21712 }
21713
21714 return name;
21715 }
21716
21717 /* Get name of a die, return NULL if not found.
21718 Anonymous namespaces are converted to their magic string. */
21719
21720 static const char *
21721 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21722 {
21723 struct attribute *attr;
21724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21725
21726 attr = dwarf2_attr (die, DW_AT_name, cu);
21727 if ((!attr || !DW_STRING (attr))
21728 && die->tag != DW_TAG_namespace
21729 && die->tag != DW_TAG_class_type
21730 && die->tag != DW_TAG_interface_type
21731 && die->tag != DW_TAG_structure_type
21732 && die->tag != DW_TAG_union_type)
21733 return NULL;
21734
21735 switch (die->tag)
21736 {
21737 case DW_TAG_compile_unit:
21738 case DW_TAG_partial_unit:
21739 /* Compilation units have a DW_AT_name that is a filename, not
21740 a source language identifier. */
21741 case DW_TAG_enumeration_type:
21742 case DW_TAG_enumerator:
21743 /* These tags always have simple identifiers already; no need
21744 to canonicalize them. */
21745 return DW_STRING (attr);
21746
21747 case DW_TAG_namespace:
21748 if (attr != NULL && DW_STRING (attr) != NULL)
21749 return DW_STRING (attr);
21750 return CP_ANONYMOUS_NAMESPACE_STR;
21751
21752 case DW_TAG_class_type:
21753 case DW_TAG_interface_type:
21754 case DW_TAG_structure_type:
21755 case DW_TAG_union_type:
21756 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21757 structures or unions. These were of the form "._%d" in GCC 4.1,
21758 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21759 and GCC 4.4. We work around this problem by ignoring these. */
21760 if (attr && DW_STRING (attr)
21761 && (startswith (DW_STRING (attr), "._")
21762 || startswith (DW_STRING (attr), "<anonymous")))
21763 return NULL;
21764
21765 /* GCC might emit a nameless typedef that has a linkage name. See
21766 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21767 if (!attr || DW_STRING (attr) == NULL)
21768 {
21769 attr = dw2_linkage_name_attr (die, cu);
21770 if (attr == NULL || DW_STRING (attr) == NULL)
21771 return NULL;
21772
21773 /* Avoid demangling DW_STRING (attr) the second time on a second
21774 call for the same DIE. */
21775 if (!DW_STRING_IS_CANONICAL (attr))
21776 {
21777 gdb::unique_xmalloc_ptr<char> demangled
21778 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21779
21780 const char *base;
21781
21782 /* FIXME: we already did this for the partial symbol... */
21783 DW_STRING (attr)
21784 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21785 demangled.get ());
21786 DW_STRING_IS_CANONICAL (attr) = 1;
21787
21788 /* Strip any leading namespaces/classes, keep only the base name.
21789 DW_AT_name for named DIEs does not contain the prefixes. */
21790 base = strrchr (DW_STRING (attr), ':');
21791 if (base && base > DW_STRING (attr) && base[-1] == ':')
21792 return &base[1];
21793 else
21794 return DW_STRING (attr);
21795 }
21796 }
21797 break;
21798
21799 default:
21800 break;
21801 }
21802
21803 if (!DW_STRING_IS_CANONICAL (attr))
21804 {
21805 DW_STRING (attr)
21806 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21807 &objfile->per_bfd->storage_obstack);
21808 DW_STRING_IS_CANONICAL (attr) = 1;
21809 }
21810 return DW_STRING (attr);
21811 }
21812
21813 /* Return the die that this die in an extension of, or NULL if there
21814 is none. *EXT_CU is the CU containing DIE on input, and the CU
21815 containing the return value on output. */
21816
21817 static struct die_info *
21818 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21819 {
21820 struct attribute *attr;
21821
21822 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21823 if (attr == NULL)
21824 return NULL;
21825
21826 return follow_die_ref (die, attr, ext_cu);
21827 }
21828
21829 /* A convenience function that returns an "unknown" DWARF name,
21830 including the value of V. STR is the name of the entity being
21831 printed, e.g., "TAG". */
21832
21833 static const char *
21834 dwarf_unknown (const char *str, unsigned v)
21835 {
21836 char *cell = get_print_cell ();
21837 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21838 return cell;
21839 }
21840
21841 /* Convert a DIE tag into its string name. */
21842
21843 static const char *
21844 dwarf_tag_name (unsigned tag)
21845 {
21846 const char *name = get_DW_TAG_name (tag);
21847
21848 if (name == NULL)
21849 return dwarf_unknown ("TAG", tag);
21850
21851 return name;
21852 }
21853
21854 /* Convert a DWARF attribute code into its string name. */
21855
21856 static const char *
21857 dwarf_attr_name (unsigned attr)
21858 {
21859 const char *name;
21860
21861 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21862 if (attr == DW_AT_MIPS_fde)
21863 return "DW_AT_MIPS_fde";
21864 #else
21865 if (attr == DW_AT_HP_block_index)
21866 return "DW_AT_HP_block_index";
21867 #endif
21868
21869 name = get_DW_AT_name (attr);
21870
21871 if (name == NULL)
21872 return dwarf_unknown ("AT", attr);
21873
21874 return name;
21875 }
21876
21877 /* Convert a DWARF value form code into its string name. */
21878
21879 static const char *
21880 dwarf_form_name (unsigned form)
21881 {
21882 const char *name = get_DW_FORM_name (form);
21883
21884 if (name == NULL)
21885 return dwarf_unknown ("FORM", form);
21886
21887 return name;
21888 }
21889
21890 static const char *
21891 dwarf_bool_name (unsigned mybool)
21892 {
21893 if (mybool)
21894 return "TRUE";
21895 else
21896 return "FALSE";
21897 }
21898
21899 /* Convert a DWARF type code into its string name. */
21900
21901 static const char *
21902 dwarf_type_encoding_name (unsigned enc)
21903 {
21904 const char *name = get_DW_ATE_name (enc);
21905
21906 if (name == NULL)
21907 return dwarf_unknown ("ATE", enc);
21908
21909 return name;
21910 }
21911
21912 static void
21913 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21914 {
21915 unsigned int i;
21916
21917 print_spaces (indent, f);
21918 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21919 dwarf_tag_name (die->tag), die->abbrev,
21920 sect_offset_str (die->sect_off));
21921
21922 if (die->parent != NULL)
21923 {
21924 print_spaces (indent, f);
21925 fprintf_unfiltered (f, " parent at offset: %s\n",
21926 sect_offset_str (die->parent->sect_off));
21927 }
21928
21929 print_spaces (indent, f);
21930 fprintf_unfiltered (f, " has children: %s\n",
21931 dwarf_bool_name (die->child != NULL));
21932
21933 print_spaces (indent, f);
21934 fprintf_unfiltered (f, " attributes:\n");
21935
21936 for (i = 0; i < die->num_attrs; ++i)
21937 {
21938 print_spaces (indent, f);
21939 fprintf_unfiltered (f, " %s (%s) ",
21940 dwarf_attr_name (die->attrs[i].name),
21941 dwarf_form_name (die->attrs[i].form));
21942
21943 switch (die->attrs[i].form)
21944 {
21945 case DW_FORM_addr:
21946 case DW_FORM_addrx:
21947 case DW_FORM_GNU_addr_index:
21948 fprintf_unfiltered (f, "address: ");
21949 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21950 break;
21951 case DW_FORM_block2:
21952 case DW_FORM_block4:
21953 case DW_FORM_block:
21954 case DW_FORM_block1:
21955 fprintf_unfiltered (f, "block: size %s",
21956 pulongest (DW_BLOCK (&die->attrs[i])->size));
21957 break;
21958 case DW_FORM_exprloc:
21959 fprintf_unfiltered (f, "expression: size %s",
21960 pulongest (DW_BLOCK (&die->attrs[i])->size));
21961 break;
21962 case DW_FORM_data16:
21963 fprintf_unfiltered (f, "constant of 16 bytes");
21964 break;
21965 case DW_FORM_ref_addr:
21966 fprintf_unfiltered (f, "ref address: ");
21967 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21968 break;
21969 case DW_FORM_GNU_ref_alt:
21970 fprintf_unfiltered (f, "alt ref address: ");
21971 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21972 break;
21973 case DW_FORM_ref1:
21974 case DW_FORM_ref2:
21975 case DW_FORM_ref4:
21976 case DW_FORM_ref8:
21977 case DW_FORM_ref_udata:
21978 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21979 (long) (DW_UNSND (&die->attrs[i])));
21980 break;
21981 case DW_FORM_data1:
21982 case DW_FORM_data2:
21983 case DW_FORM_data4:
21984 case DW_FORM_data8:
21985 case DW_FORM_udata:
21986 case DW_FORM_sdata:
21987 fprintf_unfiltered (f, "constant: %s",
21988 pulongest (DW_UNSND (&die->attrs[i])));
21989 break;
21990 case DW_FORM_sec_offset:
21991 fprintf_unfiltered (f, "section offset: %s",
21992 pulongest (DW_UNSND (&die->attrs[i])));
21993 break;
21994 case DW_FORM_ref_sig8:
21995 fprintf_unfiltered (f, "signature: %s",
21996 hex_string (DW_SIGNATURE (&die->attrs[i])));
21997 break;
21998 case DW_FORM_string:
21999 case DW_FORM_strp:
22000 case DW_FORM_line_strp:
22001 case DW_FORM_strx:
22002 case DW_FORM_GNU_str_index:
22003 case DW_FORM_GNU_strp_alt:
22004 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22005 DW_STRING (&die->attrs[i])
22006 ? DW_STRING (&die->attrs[i]) : "",
22007 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22008 break;
22009 case DW_FORM_flag:
22010 if (DW_UNSND (&die->attrs[i]))
22011 fprintf_unfiltered (f, "flag: TRUE");
22012 else
22013 fprintf_unfiltered (f, "flag: FALSE");
22014 break;
22015 case DW_FORM_flag_present:
22016 fprintf_unfiltered (f, "flag: TRUE");
22017 break;
22018 case DW_FORM_indirect:
22019 /* The reader will have reduced the indirect form to
22020 the "base form" so this form should not occur. */
22021 fprintf_unfiltered (f,
22022 "unexpected attribute form: DW_FORM_indirect");
22023 break;
22024 case DW_FORM_implicit_const:
22025 fprintf_unfiltered (f, "constant: %s",
22026 plongest (DW_SND (&die->attrs[i])));
22027 break;
22028 default:
22029 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22030 die->attrs[i].form);
22031 break;
22032 }
22033 fprintf_unfiltered (f, "\n");
22034 }
22035 }
22036
22037 static void
22038 dump_die_for_error (struct die_info *die)
22039 {
22040 dump_die_shallow (gdb_stderr, 0, die);
22041 }
22042
22043 static void
22044 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22045 {
22046 int indent = level * 4;
22047
22048 gdb_assert (die != NULL);
22049
22050 if (level >= max_level)
22051 return;
22052
22053 dump_die_shallow (f, indent, die);
22054
22055 if (die->child != NULL)
22056 {
22057 print_spaces (indent, f);
22058 fprintf_unfiltered (f, " Children:");
22059 if (level + 1 < max_level)
22060 {
22061 fprintf_unfiltered (f, "\n");
22062 dump_die_1 (f, level + 1, max_level, die->child);
22063 }
22064 else
22065 {
22066 fprintf_unfiltered (f,
22067 " [not printed, max nesting level reached]\n");
22068 }
22069 }
22070
22071 if (die->sibling != NULL && level > 0)
22072 {
22073 dump_die_1 (f, level, max_level, die->sibling);
22074 }
22075 }
22076
22077 /* This is called from the pdie macro in gdbinit.in.
22078 It's not static so gcc will keep a copy callable from gdb. */
22079
22080 void
22081 dump_die (struct die_info *die, int max_level)
22082 {
22083 dump_die_1 (gdb_stdlog, 0, max_level, die);
22084 }
22085
22086 static void
22087 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22088 {
22089 void **slot;
22090
22091 slot = htab_find_slot_with_hash (cu->die_hash, die,
22092 to_underlying (die->sect_off),
22093 INSERT);
22094
22095 *slot = die;
22096 }
22097
22098 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22099 required kind. */
22100
22101 static sect_offset
22102 dwarf2_get_ref_die_offset (const struct attribute *attr)
22103 {
22104 if (attr->form_is_ref ())
22105 return (sect_offset) DW_UNSND (attr);
22106
22107 complaint (_("unsupported die ref attribute form: '%s'"),
22108 dwarf_form_name (attr->form));
22109 return {};
22110 }
22111
22112 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22113 * the value held by the attribute is not constant. */
22114
22115 static LONGEST
22116 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22117 {
22118 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22119 return DW_SND (attr);
22120 else if (attr->form == DW_FORM_udata
22121 || attr->form == DW_FORM_data1
22122 || attr->form == DW_FORM_data2
22123 || attr->form == DW_FORM_data4
22124 || attr->form == DW_FORM_data8)
22125 return DW_UNSND (attr);
22126 else
22127 {
22128 /* For DW_FORM_data16 see attribute::form_is_constant. */
22129 complaint (_("Attribute value is not a constant (%s)"),
22130 dwarf_form_name (attr->form));
22131 return default_value;
22132 }
22133 }
22134
22135 /* Follow reference or signature attribute ATTR of SRC_DIE.
22136 On entry *REF_CU is the CU of SRC_DIE.
22137 On exit *REF_CU is the CU of the result. */
22138
22139 static struct die_info *
22140 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22141 struct dwarf2_cu **ref_cu)
22142 {
22143 struct die_info *die;
22144
22145 if (attr->form_is_ref ())
22146 die = follow_die_ref (src_die, attr, ref_cu);
22147 else if (attr->form == DW_FORM_ref_sig8)
22148 die = follow_die_sig (src_die, attr, ref_cu);
22149 else
22150 {
22151 dump_die_for_error (src_die);
22152 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22153 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22154 }
22155
22156 return die;
22157 }
22158
22159 /* Follow reference OFFSET.
22160 On entry *REF_CU is the CU of the source die referencing OFFSET.
22161 On exit *REF_CU is the CU of the result.
22162 Returns NULL if OFFSET is invalid. */
22163
22164 static struct die_info *
22165 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22166 struct dwarf2_cu **ref_cu)
22167 {
22168 struct die_info temp_die;
22169 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22170 struct dwarf2_per_objfile *dwarf2_per_objfile
22171 = cu->per_cu->dwarf2_per_objfile;
22172
22173 gdb_assert (cu->per_cu != NULL);
22174
22175 target_cu = cu;
22176
22177 if (cu->per_cu->is_debug_types)
22178 {
22179 /* .debug_types CUs cannot reference anything outside their CU.
22180 If they need to, they have to reference a signatured type via
22181 DW_FORM_ref_sig8. */
22182 if (!cu->header.offset_in_cu_p (sect_off))
22183 return NULL;
22184 }
22185 else if (offset_in_dwz != cu->per_cu->is_dwz
22186 || !cu->header.offset_in_cu_p (sect_off))
22187 {
22188 struct dwarf2_per_cu_data *per_cu;
22189
22190 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22191 dwarf2_per_objfile);
22192
22193 /* If necessary, add it to the queue and load its DIEs. */
22194 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22195 load_full_comp_unit (per_cu, false, cu->language);
22196
22197 target_cu = per_cu->cu;
22198 }
22199 else if (cu->dies == NULL)
22200 {
22201 /* We're loading full DIEs during partial symbol reading. */
22202 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22203 load_full_comp_unit (cu->per_cu, false, language_minimal);
22204 }
22205
22206 *ref_cu = target_cu;
22207 temp_die.sect_off = sect_off;
22208
22209 if (target_cu != cu)
22210 target_cu->ancestor = cu;
22211
22212 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22213 &temp_die,
22214 to_underlying (sect_off));
22215 }
22216
22217 /* Follow reference attribute ATTR of SRC_DIE.
22218 On entry *REF_CU is the CU of SRC_DIE.
22219 On exit *REF_CU is the CU of the result. */
22220
22221 static struct die_info *
22222 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22223 struct dwarf2_cu **ref_cu)
22224 {
22225 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22226 struct dwarf2_cu *cu = *ref_cu;
22227 struct die_info *die;
22228
22229 die = follow_die_offset (sect_off,
22230 (attr->form == DW_FORM_GNU_ref_alt
22231 || cu->per_cu->is_dwz),
22232 ref_cu);
22233 if (!die)
22234 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22235 "at %s [in module %s]"),
22236 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22237 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22238
22239 return die;
22240 }
22241
22242 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22243 Returned value is intended for DW_OP_call*. Returned
22244 dwarf2_locexpr_baton->data has lifetime of
22245 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22246
22247 struct dwarf2_locexpr_baton
22248 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22249 struct dwarf2_per_cu_data *per_cu,
22250 CORE_ADDR (*get_frame_pc) (void *baton),
22251 void *baton, bool resolve_abstract_p)
22252 {
22253 struct dwarf2_cu *cu;
22254 struct die_info *die;
22255 struct attribute *attr;
22256 struct dwarf2_locexpr_baton retval;
22257 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22258 struct objfile *objfile = dwarf2_per_objfile->objfile;
22259
22260 if (per_cu->cu == NULL)
22261 load_cu (per_cu, false);
22262 cu = per_cu->cu;
22263 if (cu == NULL)
22264 {
22265 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22266 Instead just throw an error, not much else we can do. */
22267 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22268 sect_offset_str (sect_off), objfile_name (objfile));
22269 }
22270
22271 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22272 if (!die)
22273 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22274 sect_offset_str (sect_off), objfile_name (objfile));
22275
22276 attr = dwarf2_attr (die, DW_AT_location, cu);
22277 if (!attr && resolve_abstract_p
22278 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22279 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22280 {
22281 CORE_ADDR pc = (*get_frame_pc) (baton);
22282 CORE_ADDR baseaddr = objfile->text_section_offset ();
22283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22284
22285 for (const auto &cand_off
22286 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22287 {
22288 struct dwarf2_cu *cand_cu = cu;
22289 struct die_info *cand
22290 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22291 if (!cand
22292 || !cand->parent
22293 || cand->parent->tag != DW_TAG_subprogram)
22294 continue;
22295
22296 CORE_ADDR pc_low, pc_high;
22297 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22298 if (pc_low == ((CORE_ADDR) -1))
22299 continue;
22300 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22301 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22302 if (!(pc_low <= pc && pc < pc_high))
22303 continue;
22304
22305 die = cand;
22306 attr = dwarf2_attr (die, DW_AT_location, cu);
22307 break;
22308 }
22309 }
22310
22311 if (!attr)
22312 {
22313 /* DWARF: "If there is no such attribute, then there is no effect.".
22314 DATA is ignored if SIZE is 0. */
22315
22316 retval.data = NULL;
22317 retval.size = 0;
22318 }
22319 else if (attr->form_is_section_offset ())
22320 {
22321 struct dwarf2_loclist_baton loclist_baton;
22322 CORE_ADDR pc = (*get_frame_pc) (baton);
22323 size_t size;
22324
22325 fill_in_loclist_baton (cu, &loclist_baton, attr);
22326
22327 retval.data = dwarf2_find_location_expression (&loclist_baton,
22328 &size, pc);
22329 retval.size = size;
22330 }
22331 else
22332 {
22333 if (!attr->form_is_block ())
22334 error (_("Dwarf Error: DIE at %s referenced in module %s "
22335 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22336 sect_offset_str (sect_off), objfile_name (objfile));
22337
22338 retval.data = DW_BLOCK (attr)->data;
22339 retval.size = DW_BLOCK (attr)->size;
22340 }
22341 retval.per_cu = cu->per_cu;
22342
22343 age_cached_comp_units (dwarf2_per_objfile);
22344
22345 return retval;
22346 }
22347
22348 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22349 offset. */
22350
22351 struct dwarf2_locexpr_baton
22352 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22353 struct dwarf2_per_cu_data *per_cu,
22354 CORE_ADDR (*get_frame_pc) (void *baton),
22355 void *baton)
22356 {
22357 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22358
22359 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22360 }
22361
22362 /* Write a constant of a given type as target-ordered bytes into
22363 OBSTACK. */
22364
22365 static const gdb_byte *
22366 write_constant_as_bytes (struct obstack *obstack,
22367 enum bfd_endian byte_order,
22368 struct type *type,
22369 ULONGEST value,
22370 LONGEST *len)
22371 {
22372 gdb_byte *result;
22373
22374 *len = TYPE_LENGTH (type);
22375 result = (gdb_byte *) obstack_alloc (obstack, *len);
22376 store_unsigned_integer (result, *len, byte_order, value);
22377
22378 return result;
22379 }
22380
22381 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22382 pointer to the constant bytes and set LEN to the length of the
22383 data. If memory is needed, allocate it on OBSTACK. If the DIE
22384 does not have a DW_AT_const_value, return NULL. */
22385
22386 const gdb_byte *
22387 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22388 struct dwarf2_per_cu_data *per_cu,
22389 struct obstack *obstack,
22390 LONGEST *len)
22391 {
22392 struct dwarf2_cu *cu;
22393 struct die_info *die;
22394 struct attribute *attr;
22395 const gdb_byte *result = NULL;
22396 struct type *type;
22397 LONGEST value;
22398 enum bfd_endian byte_order;
22399 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22400
22401 if (per_cu->cu == NULL)
22402 load_cu (per_cu, false);
22403 cu = per_cu->cu;
22404 if (cu == NULL)
22405 {
22406 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22407 Instead just throw an error, not much else we can do. */
22408 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22409 sect_offset_str (sect_off), objfile_name (objfile));
22410 }
22411
22412 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22413 if (!die)
22414 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22415 sect_offset_str (sect_off), objfile_name (objfile));
22416
22417 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22418 if (attr == NULL)
22419 return NULL;
22420
22421 byte_order = (bfd_big_endian (objfile->obfd)
22422 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22423
22424 switch (attr->form)
22425 {
22426 case DW_FORM_addr:
22427 case DW_FORM_addrx:
22428 case DW_FORM_GNU_addr_index:
22429 {
22430 gdb_byte *tem;
22431
22432 *len = cu->header.addr_size;
22433 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22434 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22435 result = tem;
22436 }
22437 break;
22438 case DW_FORM_string:
22439 case DW_FORM_strp:
22440 case DW_FORM_strx:
22441 case DW_FORM_GNU_str_index:
22442 case DW_FORM_GNU_strp_alt:
22443 /* DW_STRING is already allocated on the objfile obstack, point
22444 directly to it. */
22445 result = (const gdb_byte *) DW_STRING (attr);
22446 *len = strlen (DW_STRING (attr));
22447 break;
22448 case DW_FORM_block1:
22449 case DW_FORM_block2:
22450 case DW_FORM_block4:
22451 case DW_FORM_block:
22452 case DW_FORM_exprloc:
22453 case DW_FORM_data16:
22454 result = DW_BLOCK (attr)->data;
22455 *len = DW_BLOCK (attr)->size;
22456 break;
22457
22458 /* The DW_AT_const_value attributes are supposed to carry the
22459 symbol's value "represented as it would be on the target
22460 architecture." By the time we get here, it's already been
22461 converted to host endianness, so we just need to sign- or
22462 zero-extend it as appropriate. */
22463 case DW_FORM_data1:
22464 type = die_type (die, cu);
22465 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22466 if (result == NULL)
22467 result = write_constant_as_bytes (obstack, byte_order,
22468 type, value, len);
22469 break;
22470 case DW_FORM_data2:
22471 type = die_type (die, cu);
22472 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22473 if (result == NULL)
22474 result = write_constant_as_bytes (obstack, byte_order,
22475 type, value, len);
22476 break;
22477 case DW_FORM_data4:
22478 type = die_type (die, cu);
22479 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22480 if (result == NULL)
22481 result = write_constant_as_bytes (obstack, byte_order,
22482 type, value, len);
22483 break;
22484 case DW_FORM_data8:
22485 type = die_type (die, cu);
22486 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22487 if (result == NULL)
22488 result = write_constant_as_bytes (obstack, byte_order,
22489 type, value, len);
22490 break;
22491
22492 case DW_FORM_sdata:
22493 case DW_FORM_implicit_const:
22494 type = die_type (die, cu);
22495 result = write_constant_as_bytes (obstack, byte_order,
22496 type, DW_SND (attr), len);
22497 break;
22498
22499 case DW_FORM_udata:
22500 type = die_type (die, cu);
22501 result = write_constant_as_bytes (obstack, byte_order,
22502 type, DW_UNSND (attr), len);
22503 break;
22504
22505 default:
22506 complaint (_("unsupported const value attribute form: '%s'"),
22507 dwarf_form_name (attr->form));
22508 break;
22509 }
22510
22511 return result;
22512 }
22513
22514 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22515 valid type for this die is found. */
22516
22517 struct type *
22518 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22519 struct dwarf2_per_cu_data *per_cu)
22520 {
22521 struct dwarf2_cu *cu;
22522 struct die_info *die;
22523
22524 if (per_cu->cu == NULL)
22525 load_cu (per_cu, false);
22526 cu = per_cu->cu;
22527 if (!cu)
22528 return NULL;
22529
22530 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22531 if (!die)
22532 return NULL;
22533
22534 return die_type (die, cu);
22535 }
22536
22537 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22538 PER_CU. */
22539
22540 struct type *
22541 dwarf2_get_die_type (cu_offset die_offset,
22542 struct dwarf2_per_cu_data *per_cu)
22543 {
22544 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22545 return get_die_type_at_offset (die_offset_sect, per_cu);
22546 }
22547
22548 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22549 On entry *REF_CU is the CU of SRC_DIE.
22550 On exit *REF_CU is the CU of the result.
22551 Returns NULL if the referenced DIE isn't found. */
22552
22553 static struct die_info *
22554 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22555 struct dwarf2_cu **ref_cu)
22556 {
22557 struct die_info temp_die;
22558 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22559 struct die_info *die;
22560
22561 /* While it might be nice to assert sig_type->type == NULL here,
22562 we can get here for DW_AT_imported_declaration where we need
22563 the DIE not the type. */
22564
22565 /* If necessary, add it to the queue and load its DIEs. */
22566
22567 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22568 read_signatured_type (sig_type);
22569
22570 sig_cu = sig_type->per_cu.cu;
22571 gdb_assert (sig_cu != NULL);
22572 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22573 temp_die.sect_off = sig_type->type_offset_in_section;
22574 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22575 to_underlying (temp_die.sect_off));
22576 if (die)
22577 {
22578 struct dwarf2_per_objfile *dwarf2_per_objfile
22579 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22580
22581 /* For .gdb_index version 7 keep track of included TUs.
22582 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22583 if (dwarf2_per_objfile->index_table != NULL
22584 && dwarf2_per_objfile->index_table->version <= 7)
22585 {
22586 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22587 }
22588
22589 *ref_cu = sig_cu;
22590 if (sig_cu != cu)
22591 sig_cu->ancestor = cu;
22592
22593 return die;
22594 }
22595
22596 return NULL;
22597 }
22598
22599 /* Follow signatured type referenced by ATTR in SRC_DIE.
22600 On entry *REF_CU is the CU of SRC_DIE.
22601 On exit *REF_CU is the CU of the result.
22602 The result is the DIE of the type.
22603 If the referenced type cannot be found an error is thrown. */
22604
22605 static struct die_info *
22606 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22607 struct dwarf2_cu **ref_cu)
22608 {
22609 ULONGEST signature = DW_SIGNATURE (attr);
22610 struct signatured_type *sig_type;
22611 struct die_info *die;
22612
22613 gdb_assert (attr->form == DW_FORM_ref_sig8);
22614
22615 sig_type = lookup_signatured_type (*ref_cu, signature);
22616 /* sig_type will be NULL if the signatured type is missing from
22617 the debug info. */
22618 if (sig_type == NULL)
22619 {
22620 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22621 " from DIE at %s [in module %s]"),
22622 hex_string (signature), sect_offset_str (src_die->sect_off),
22623 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22624 }
22625
22626 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22627 if (die == NULL)
22628 {
22629 dump_die_for_error (src_die);
22630 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature), sect_offset_str (src_die->sect_off),
22633 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22634 }
22635
22636 return die;
22637 }
22638
22639 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22640 reading in and processing the type unit if necessary. */
22641
22642 static struct type *
22643 get_signatured_type (struct die_info *die, ULONGEST signature,
22644 struct dwarf2_cu *cu)
22645 {
22646 struct dwarf2_per_objfile *dwarf2_per_objfile
22647 = cu->per_cu->dwarf2_per_objfile;
22648 struct signatured_type *sig_type;
22649 struct dwarf2_cu *type_cu;
22650 struct die_info *type_die;
22651 struct type *type;
22652
22653 sig_type = lookup_signatured_type (cu, signature);
22654 /* sig_type will be NULL if the signatured type is missing from
22655 the debug info. */
22656 if (sig_type == NULL)
22657 {
22658 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22659 " from DIE at %s [in module %s]"),
22660 hex_string (signature), sect_offset_str (die->sect_off),
22661 objfile_name (dwarf2_per_objfile->objfile));
22662 return build_error_marker_type (cu, die);
22663 }
22664
22665 /* If we already know the type we're done. */
22666 if (sig_type->type != NULL)
22667 return sig_type->type;
22668
22669 type_cu = cu;
22670 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22671 if (type_die != NULL)
22672 {
22673 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22674 is created. This is important, for example, because for c++ classes
22675 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22676 type = read_type_die (type_die, type_cu);
22677 if (type == NULL)
22678 {
22679 complaint (_("Dwarf Error: Cannot build signatured type %s"
22680 " referenced from DIE at %s [in module %s]"),
22681 hex_string (signature), sect_offset_str (die->sect_off),
22682 objfile_name (dwarf2_per_objfile->objfile));
22683 type = build_error_marker_type (cu, die);
22684 }
22685 }
22686 else
22687 {
22688 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
22691 objfile_name (dwarf2_per_objfile->objfile));
22692 type = build_error_marker_type (cu, die);
22693 }
22694 sig_type->type = type;
22695
22696 return type;
22697 }
22698
22699 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22700 reading in and processing the type unit if necessary. */
22701
22702 static struct type *
22703 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22704 struct dwarf2_cu *cu) /* ARI: editCase function */
22705 {
22706 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22707 if (attr->form_is_ref ())
22708 {
22709 struct dwarf2_cu *type_cu = cu;
22710 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22711
22712 return read_type_die (type_die, type_cu);
22713 }
22714 else if (attr->form == DW_FORM_ref_sig8)
22715 {
22716 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22717 }
22718 else
22719 {
22720 struct dwarf2_per_objfile *dwarf2_per_objfile
22721 = cu->per_cu->dwarf2_per_objfile;
22722
22723 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22724 " at %s [in module %s]"),
22725 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22726 objfile_name (dwarf2_per_objfile->objfile));
22727 return build_error_marker_type (cu, die);
22728 }
22729 }
22730
22731 /* Load the DIEs associated with type unit PER_CU into memory. */
22732
22733 static void
22734 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22735 {
22736 struct signatured_type *sig_type;
22737
22738 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22739 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22740
22741 /* We have the per_cu, but we need the signatured_type.
22742 Fortunately this is an easy translation. */
22743 gdb_assert (per_cu->is_debug_types);
22744 sig_type = (struct signatured_type *) per_cu;
22745
22746 gdb_assert (per_cu->cu == NULL);
22747
22748 read_signatured_type (sig_type);
22749
22750 gdb_assert (per_cu->cu != NULL);
22751 }
22752
22753 /* Read in a signatured type and build its CU and DIEs.
22754 If the type is a stub for the real type in a DWO file,
22755 read in the real type from the DWO file as well. */
22756
22757 static void
22758 read_signatured_type (struct signatured_type *sig_type)
22759 {
22760 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22761
22762 gdb_assert (per_cu->is_debug_types);
22763 gdb_assert (per_cu->cu == NULL);
22764
22765 cutu_reader reader (per_cu, NULL, 0, false);
22766
22767 if (!reader.dummy_p)
22768 {
22769 struct dwarf2_cu *cu = reader.cu;
22770 const gdb_byte *info_ptr = reader.info_ptr;
22771
22772 gdb_assert (cu->die_hash == NULL);
22773 cu->die_hash =
22774 htab_create_alloc_ex (cu->header.length / 12,
22775 die_hash,
22776 die_eq,
22777 NULL,
22778 &cu->comp_unit_obstack,
22779 hashtab_obstack_allocate,
22780 dummy_obstack_deallocate);
22781
22782 if (reader.comp_unit_die->has_children)
22783 reader.comp_unit_die->child
22784 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22785 reader.comp_unit_die);
22786 cu->dies = reader.comp_unit_die;
22787 /* comp_unit_die is not stored in die_hash, no need. */
22788
22789 /* We try not to read any attributes in this function, because
22790 not all CUs needed for references have been loaded yet, and
22791 symbol table processing isn't initialized. But we have to
22792 set the CU language, or we won't be able to build types
22793 correctly. Similarly, if we do not read the producer, we can
22794 not apply producer-specific interpretation. */
22795 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22796
22797 reader.keep ();
22798 }
22799
22800 sig_type->per_cu.tu_read = 1;
22801 }
22802
22803 /* Decode simple location descriptions.
22804 Given a pointer to a dwarf block that defines a location, compute
22805 the location and return the value.
22806
22807 NOTE drow/2003-11-18: This function is called in two situations
22808 now: for the address of static or global variables (partial symbols
22809 only) and for offsets into structures which are expected to be
22810 (more or less) constant. The partial symbol case should go away,
22811 and only the constant case should remain. That will let this
22812 function complain more accurately. A few special modes are allowed
22813 without complaint for global variables (for instance, global
22814 register values and thread-local values).
22815
22816 A location description containing no operations indicates that the
22817 object is optimized out. The return value is 0 for that case.
22818 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22819 callers will only want a very basic result and this can become a
22820 complaint.
22821
22822 Note that stack[0] is unused except as a default error return. */
22823
22824 static CORE_ADDR
22825 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22826 {
22827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22828 size_t i;
22829 size_t size = blk->size;
22830 const gdb_byte *data = blk->data;
22831 CORE_ADDR stack[64];
22832 int stacki;
22833 unsigned int bytes_read, unsnd;
22834 gdb_byte op;
22835
22836 i = 0;
22837 stacki = 0;
22838 stack[stacki] = 0;
22839 stack[++stacki] = 0;
22840
22841 while (i < size)
22842 {
22843 op = data[i++];
22844 switch (op)
22845 {
22846 case DW_OP_lit0:
22847 case DW_OP_lit1:
22848 case DW_OP_lit2:
22849 case DW_OP_lit3:
22850 case DW_OP_lit4:
22851 case DW_OP_lit5:
22852 case DW_OP_lit6:
22853 case DW_OP_lit7:
22854 case DW_OP_lit8:
22855 case DW_OP_lit9:
22856 case DW_OP_lit10:
22857 case DW_OP_lit11:
22858 case DW_OP_lit12:
22859 case DW_OP_lit13:
22860 case DW_OP_lit14:
22861 case DW_OP_lit15:
22862 case DW_OP_lit16:
22863 case DW_OP_lit17:
22864 case DW_OP_lit18:
22865 case DW_OP_lit19:
22866 case DW_OP_lit20:
22867 case DW_OP_lit21:
22868 case DW_OP_lit22:
22869 case DW_OP_lit23:
22870 case DW_OP_lit24:
22871 case DW_OP_lit25:
22872 case DW_OP_lit26:
22873 case DW_OP_lit27:
22874 case DW_OP_lit28:
22875 case DW_OP_lit29:
22876 case DW_OP_lit30:
22877 case DW_OP_lit31:
22878 stack[++stacki] = op - DW_OP_lit0;
22879 break;
22880
22881 case DW_OP_reg0:
22882 case DW_OP_reg1:
22883 case DW_OP_reg2:
22884 case DW_OP_reg3:
22885 case DW_OP_reg4:
22886 case DW_OP_reg5:
22887 case DW_OP_reg6:
22888 case DW_OP_reg7:
22889 case DW_OP_reg8:
22890 case DW_OP_reg9:
22891 case DW_OP_reg10:
22892 case DW_OP_reg11:
22893 case DW_OP_reg12:
22894 case DW_OP_reg13:
22895 case DW_OP_reg14:
22896 case DW_OP_reg15:
22897 case DW_OP_reg16:
22898 case DW_OP_reg17:
22899 case DW_OP_reg18:
22900 case DW_OP_reg19:
22901 case DW_OP_reg20:
22902 case DW_OP_reg21:
22903 case DW_OP_reg22:
22904 case DW_OP_reg23:
22905 case DW_OP_reg24:
22906 case DW_OP_reg25:
22907 case DW_OP_reg26:
22908 case DW_OP_reg27:
22909 case DW_OP_reg28:
22910 case DW_OP_reg29:
22911 case DW_OP_reg30:
22912 case DW_OP_reg31:
22913 stack[++stacki] = op - DW_OP_reg0;
22914 if (i < size)
22915 dwarf2_complex_location_expr_complaint ();
22916 break;
22917
22918 case DW_OP_regx:
22919 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22920 i += bytes_read;
22921 stack[++stacki] = unsnd;
22922 if (i < size)
22923 dwarf2_complex_location_expr_complaint ();
22924 break;
22925
22926 case DW_OP_addr:
22927 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22928 &bytes_read);
22929 i += bytes_read;
22930 break;
22931
22932 case DW_OP_const1u:
22933 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22934 i += 1;
22935 break;
22936
22937 case DW_OP_const1s:
22938 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22939 i += 1;
22940 break;
22941
22942 case DW_OP_const2u:
22943 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22944 i += 2;
22945 break;
22946
22947 case DW_OP_const2s:
22948 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22949 i += 2;
22950 break;
22951
22952 case DW_OP_const4u:
22953 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22954 i += 4;
22955 break;
22956
22957 case DW_OP_const4s:
22958 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22959 i += 4;
22960 break;
22961
22962 case DW_OP_const8u:
22963 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22964 i += 8;
22965 break;
22966
22967 case DW_OP_constu:
22968 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22969 &bytes_read);
22970 i += bytes_read;
22971 break;
22972
22973 case DW_OP_consts:
22974 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22975 i += bytes_read;
22976 break;
22977
22978 case DW_OP_dup:
22979 stack[stacki + 1] = stack[stacki];
22980 stacki++;
22981 break;
22982
22983 case DW_OP_plus:
22984 stack[stacki - 1] += stack[stacki];
22985 stacki--;
22986 break;
22987
22988 case DW_OP_plus_uconst:
22989 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22990 &bytes_read);
22991 i += bytes_read;
22992 break;
22993
22994 case DW_OP_minus:
22995 stack[stacki - 1] -= stack[stacki];
22996 stacki--;
22997 break;
22998
22999 case DW_OP_deref:
23000 /* If we're not the last op, then we definitely can't encode
23001 this using GDB's address_class enum. This is valid for partial
23002 global symbols, although the variable's address will be bogus
23003 in the psymtab. */
23004 if (i < size)
23005 dwarf2_complex_location_expr_complaint ();
23006 break;
23007
23008 case DW_OP_GNU_push_tls_address:
23009 case DW_OP_form_tls_address:
23010 /* The top of the stack has the offset from the beginning
23011 of the thread control block at which the variable is located. */
23012 /* Nothing should follow this operator, so the top of stack would
23013 be returned. */
23014 /* This is valid for partial global symbols, but the variable's
23015 address will be bogus in the psymtab. Make it always at least
23016 non-zero to not look as a variable garbage collected by linker
23017 which have DW_OP_addr 0. */
23018 if (i < size)
23019 dwarf2_complex_location_expr_complaint ();
23020 stack[stacki]++;
23021 break;
23022
23023 case DW_OP_GNU_uninit:
23024 break;
23025
23026 case DW_OP_addrx:
23027 case DW_OP_GNU_addr_index:
23028 case DW_OP_GNU_const_index:
23029 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23030 &bytes_read);
23031 i += bytes_read;
23032 break;
23033
23034 default:
23035 {
23036 const char *name = get_DW_OP_name (op);
23037
23038 if (name)
23039 complaint (_("unsupported stack op: '%s'"),
23040 name);
23041 else
23042 complaint (_("unsupported stack op: '%02x'"),
23043 op);
23044 }
23045
23046 return (stack[stacki]);
23047 }
23048
23049 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23050 outside of the allocated space. Also enforce minimum>0. */
23051 if (stacki >= ARRAY_SIZE (stack) - 1)
23052 {
23053 complaint (_("location description stack overflow"));
23054 return 0;
23055 }
23056
23057 if (stacki <= 0)
23058 {
23059 complaint (_("location description stack underflow"));
23060 return 0;
23061 }
23062 }
23063 return (stack[stacki]);
23064 }
23065
23066 /* memory allocation interface */
23067
23068 static struct dwarf_block *
23069 dwarf_alloc_block (struct dwarf2_cu *cu)
23070 {
23071 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23072 }
23073
23074 static struct die_info *
23075 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23076 {
23077 struct die_info *die;
23078 size_t size = sizeof (struct die_info);
23079
23080 if (num_attrs > 1)
23081 size += (num_attrs - 1) * sizeof (struct attribute);
23082
23083 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23084 memset (die, 0, sizeof (struct die_info));
23085 return (die);
23086 }
23087
23088 \f
23089 /* Macro support. */
23090
23091 static struct macro_source_file *
23092 macro_start_file (struct dwarf2_cu *cu,
23093 int file, int line,
23094 struct macro_source_file *current_file,
23095 struct line_header *lh)
23096 {
23097 /* File name relative to the compilation directory of this source file. */
23098 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23099
23100 if (! current_file)
23101 {
23102 /* Note: We don't create a macro table for this compilation unit
23103 at all until we actually get a filename. */
23104 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23105
23106 /* If we have no current file, then this must be the start_file
23107 directive for the compilation unit's main source file. */
23108 current_file = macro_set_main (macro_table, file_name.get ());
23109 macro_define_special (macro_table);
23110 }
23111 else
23112 current_file = macro_include (current_file, line, file_name.get ());
23113
23114 return current_file;
23115 }
23116
23117 static const char *
23118 consume_improper_spaces (const char *p, const char *body)
23119 {
23120 if (*p == ' ')
23121 {
23122 complaint (_("macro definition contains spaces "
23123 "in formal argument list:\n`%s'"),
23124 body);
23125
23126 while (*p == ' ')
23127 p++;
23128 }
23129
23130 return p;
23131 }
23132
23133
23134 static void
23135 parse_macro_definition (struct macro_source_file *file, int line,
23136 const char *body)
23137 {
23138 const char *p;
23139
23140 /* The body string takes one of two forms. For object-like macro
23141 definitions, it should be:
23142
23143 <macro name> " " <definition>
23144
23145 For function-like macro definitions, it should be:
23146
23147 <macro name> "() " <definition>
23148 or
23149 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23150
23151 Spaces may appear only where explicitly indicated, and in the
23152 <definition>.
23153
23154 The Dwarf 2 spec says that an object-like macro's name is always
23155 followed by a space, but versions of GCC around March 2002 omit
23156 the space when the macro's definition is the empty string.
23157
23158 The Dwarf 2 spec says that there should be no spaces between the
23159 formal arguments in a function-like macro's formal argument list,
23160 but versions of GCC around March 2002 include spaces after the
23161 commas. */
23162
23163
23164 /* Find the extent of the macro name. The macro name is terminated
23165 by either a space or null character (for an object-like macro) or
23166 an opening paren (for a function-like macro). */
23167 for (p = body; *p; p++)
23168 if (*p == ' ' || *p == '(')
23169 break;
23170
23171 if (*p == ' ' || *p == '\0')
23172 {
23173 /* It's an object-like macro. */
23174 int name_len = p - body;
23175 std::string name (body, name_len);
23176 const char *replacement;
23177
23178 if (*p == ' ')
23179 replacement = body + name_len + 1;
23180 else
23181 {
23182 dwarf2_macro_malformed_definition_complaint (body);
23183 replacement = body + name_len;
23184 }
23185
23186 macro_define_object (file, line, name.c_str (), replacement);
23187 }
23188 else if (*p == '(')
23189 {
23190 /* It's a function-like macro. */
23191 std::string name (body, p - body);
23192 int argc = 0;
23193 int argv_size = 1;
23194 char **argv = XNEWVEC (char *, argv_size);
23195
23196 p++;
23197
23198 p = consume_improper_spaces (p, body);
23199
23200 /* Parse the formal argument list. */
23201 while (*p && *p != ')')
23202 {
23203 /* Find the extent of the current argument name. */
23204 const char *arg_start = p;
23205
23206 while (*p && *p != ',' && *p != ')' && *p != ' ')
23207 p++;
23208
23209 if (! *p || p == arg_start)
23210 dwarf2_macro_malformed_definition_complaint (body);
23211 else
23212 {
23213 /* Make sure argv has room for the new argument. */
23214 if (argc >= argv_size)
23215 {
23216 argv_size *= 2;
23217 argv = XRESIZEVEC (char *, argv, argv_size);
23218 }
23219
23220 argv[argc++] = savestring (arg_start, p - arg_start);
23221 }
23222
23223 p = consume_improper_spaces (p, body);
23224
23225 /* Consume the comma, if present. */
23226 if (*p == ',')
23227 {
23228 p++;
23229
23230 p = consume_improper_spaces (p, body);
23231 }
23232 }
23233
23234 if (*p == ')')
23235 {
23236 p++;
23237
23238 if (*p == ' ')
23239 /* Perfectly formed definition, no complaints. */
23240 macro_define_function (file, line, name.c_str (),
23241 argc, (const char **) argv,
23242 p + 1);
23243 else if (*p == '\0')
23244 {
23245 /* Complain, but do define it. */
23246 dwarf2_macro_malformed_definition_complaint (body);
23247 macro_define_function (file, line, name.c_str (),
23248 argc, (const char **) argv,
23249 p);
23250 }
23251 else
23252 /* Just complain. */
23253 dwarf2_macro_malformed_definition_complaint (body);
23254 }
23255 else
23256 /* Just complain. */
23257 dwarf2_macro_malformed_definition_complaint (body);
23258
23259 {
23260 int i;
23261
23262 for (i = 0; i < argc; i++)
23263 xfree (argv[i]);
23264 }
23265 xfree (argv);
23266 }
23267 else
23268 dwarf2_macro_malformed_definition_complaint (body);
23269 }
23270
23271 /* Skip some bytes from BYTES according to the form given in FORM.
23272 Returns the new pointer. */
23273
23274 static const gdb_byte *
23275 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23276 enum dwarf_form form,
23277 unsigned int offset_size,
23278 struct dwarf2_section_info *section)
23279 {
23280 unsigned int bytes_read;
23281
23282 switch (form)
23283 {
23284 case DW_FORM_data1:
23285 case DW_FORM_flag:
23286 ++bytes;
23287 break;
23288
23289 case DW_FORM_data2:
23290 bytes += 2;
23291 break;
23292
23293 case DW_FORM_data4:
23294 bytes += 4;
23295 break;
23296
23297 case DW_FORM_data8:
23298 bytes += 8;
23299 break;
23300
23301 case DW_FORM_data16:
23302 bytes += 16;
23303 break;
23304
23305 case DW_FORM_string:
23306 read_direct_string (abfd, bytes, &bytes_read);
23307 bytes += bytes_read;
23308 break;
23309
23310 case DW_FORM_sec_offset:
23311 case DW_FORM_strp:
23312 case DW_FORM_GNU_strp_alt:
23313 bytes += offset_size;
23314 break;
23315
23316 case DW_FORM_block:
23317 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23318 bytes += bytes_read;
23319 break;
23320
23321 case DW_FORM_block1:
23322 bytes += 1 + read_1_byte (abfd, bytes);
23323 break;
23324 case DW_FORM_block2:
23325 bytes += 2 + read_2_bytes (abfd, bytes);
23326 break;
23327 case DW_FORM_block4:
23328 bytes += 4 + read_4_bytes (abfd, bytes);
23329 break;
23330
23331 case DW_FORM_addrx:
23332 case DW_FORM_sdata:
23333 case DW_FORM_strx:
23334 case DW_FORM_udata:
23335 case DW_FORM_GNU_addr_index:
23336 case DW_FORM_GNU_str_index:
23337 bytes = gdb_skip_leb128 (bytes, buffer_end);
23338 if (bytes == NULL)
23339 {
23340 dwarf2_section_buffer_overflow_complaint (section);
23341 return NULL;
23342 }
23343 break;
23344
23345 case DW_FORM_implicit_const:
23346 break;
23347
23348 default:
23349 {
23350 complaint (_("invalid form 0x%x in `%s'"),
23351 form, section->get_name ());
23352 return NULL;
23353 }
23354 }
23355
23356 return bytes;
23357 }
23358
23359 /* A helper for dwarf_decode_macros that handles skipping an unknown
23360 opcode. Returns an updated pointer to the macro data buffer; or,
23361 on error, issues a complaint and returns NULL. */
23362
23363 static const gdb_byte *
23364 skip_unknown_opcode (unsigned int opcode,
23365 const gdb_byte **opcode_definitions,
23366 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23367 bfd *abfd,
23368 unsigned int offset_size,
23369 struct dwarf2_section_info *section)
23370 {
23371 unsigned int bytes_read, i;
23372 unsigned long arg;
23373 const gdb_byte *defn;
23374
23375 if (opcode_definitions[opcode] == NULL)
23376 {
23377 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23378 opcode);
23379 return NULL;
23380 }
23381
23382 defn = opcode_definitions[opcode];
23383 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23384 defn += bytes_read;
23385
23386 for (i = 0; i < arg; ++i)
23387 {
23388 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23389 (enum dwarf_form) defn[i], offset_size,
23390 section);
23391 if (mac_ptr == NULL)
23392 {
23393 /* skip_form_bytes already issued the complaint. */
23394 return NULL;
23395 }
23396 }
23397
23398 return mac_ptr;
23399 }
23400
23401 /* A helper function which parses the header of a macro section.
23402 If the macro section is the extended (for now called "GNU") type,
23403 then this updates *OFFSET_SIZE. Returns a pointer to just after
23404 the header, or issues a complaint and returns NULL on error. */
23405
23406 static const gdb_byte *
23407 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23408 bfd *abfd,
23409 const gdb_byte *mac_ptr,
23410 unsigned int *offset_size,
23411 int section_is_gnu)
23412 {
23413 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23414
23415 if (section_is_gnu)
23416 {
23417 unsigned int version, flags;
23418
23419 version = read_2_bytes (abfd, mac_ptr);
23420 if (version != 4 && version != 5)
23421 {
23422 complaint (_("unrecognized version `%d' in .debug_macro section"),
23423 version);
23424 return NULL;
23425 }
23426 mac_ptr += 2;
23427
23428 flags = read_1_byte (abfd, mac_ptr);
23429 ++mac_ptr;
23430 *offset_size = (flags & 1) ? 8 : 4;
23431
23432 if ((flags & 2) != 0)
23433 /* We don't need the line table offset. */
23434 mac_ptr += *offset_size;
23435
23436 /* Vendor opcode descriptions. */
23437 if ((flags & 4) != 0)
23438 {
23439 unsigned int i, count;
23440
23441 count = read_1_byte (abfd, mac_ptr);
23442 ++mac_ptr;
23443 for (i = 0; i < count; ++i)
23444 {
23445 unsigned int opcode, bytes_read;
23446 unsigned long arg;
23447
23448 opcode = read_1_byte (abfd, mac_ptr);
23449 ++mac_ptr;
23450 opcode_definitions[opcode] = mac_ptr;
23451 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23452 mac_ptr += bytes_read;
23453 mac_ptr += arg;
23454 }
23455 }
23456 }
23457
23458 return mac_ptr;
23459 }
23460
23461 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23462 including DW_MACRO_import. */
23463
23464 static void
23465 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23466 bfd *abfd,
23467 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23468 struct macro_source_file *current_file,
23469 struct line_header *lh,
23470 struct dwarf2_section_info *section,
23471 int section_is_gnu, int section_is_dwz,
23472 unsigned int offset_size,
23473 htab_t include_hash)
23474 {
23475 struct dwarf2_per_objfile *dwarf2_per_objfile
23476 = cu->per_cu->dwarf2_per_objfile;
23477 struct objfile *objfile = dwarf2_per_objfile->objfile;
23478 enum dwarf_macro_record_type macinfo_type;
23479 int at_commandline;
23480 const gdb_byte *opcode_definitions[256];
23481
23482 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23483 &offset_size, section_is_gnu);
23484 if (mac_ptr == NULL)
23485 {
23486 /* We already issued a complaint. */
23487 return;
23488 }
23489
23490 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23491 GDB is still reading the definitions from command line. First
23492 DW_MACINFO_start_file will need to be ignored as it was already executed
23493 to create CURRENT_FILE for the main source holding also the command line
23494 definitions. On first met DW_MACINFO_start_file this flag is reset to
23495 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23496
23497 at_commandline = 1;
23498
23499 do
23500 {
23501 /* Do we at least have room for a macinfo type byte? */
23502 if (mac_ptr >= mac_end)
23503 {
23504 dwarf2_section_buffer_overflow_complaint (section);
23505 break;
23506 }
23507
23508 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23509 mac_ptr++;
23510
23511 /* Note that we rely on the fact that the corresponding GNU and
23512 DWARF constants are the same. */
23513 DIAGNOSTIC_PUSH
23514 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23515 switch (macinfo_type)
23516 {
23517 /* A zero macinfo type indicates the end of the macro
23518 information. */
23519 case 0:
23520 break;
23521
23522 case DW_MACRO_define:
23523 case DW_MACRO_undef:
23524 case DW_MACRO_define_strp:
23525 case DW_MACRO_undef_strp:
23526 case DW_MACRO_define_sup:
23527 case DW_MACRO_undef_sup:
23528 {
23529 unsigned int bytes_read;
23530 int line;
23531 const char *body;
23532 int is_define;
23533
23534 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23535 mac_ptr += bytes_read;
23536
23537 if (macinfo_type == DW_MACRO_define
23538 || macinfo_type == DW_MACRO_undef)
23539 {
23540 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23541 mac_ptr += bytes_read;
23542 }
23543 else
23544 {
23545 LONGEST str_offset;
23546
23547 str_offset = read_offset (abfd, mac_ptr, offset_size);
23548 mac_ptr += offset_size;
23549
23550 if (macinfo_type == DW_MACRO_define_sup
23551 || macinfo_type == DW_MACRO_undef_sup
23552 || section_is_dwz)
23553 {
23554 struct dwz_file *dwz
23555 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23556
23557 body = read_indirect_string_from_dwz (objfile,
23558 dwz, str_offset);
23559 }
23560 else
23561 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23562 abfd, str_offset);
23563 }
23564
23565 is_define = (macinfo_type == DW_MACRO_define
23566 || macinfo_type == DW_MACRO_define_strp
23567 || macinfo_type == DW_MACRO_define_sup);
23568 if (! current_file)
23569 {
23570 /* DWARF violation as no main source is present. */
23571 complaint (_("debug info with no main source gives macro %s "
23572 "on line %d: %s"),
23573 is_define ? _("definition") : _("undefinition"),
23574 line, body);
23575 break;
23576 }
23577 if ((line == 0 && !at_commandline)
23578 || (line != 0 && at_commandline))
23579 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23580 at_commandline ? _("command-line") : _("in-file"),
23581 is_define ? _("definition") : _("undefinition"),
23582 line == 0 ? _("zero") : _("non-zero"), line, body);
23583
23584 if (body == NULL)
23585 {
23586 /* Fedora's rpm-build's "debugedit" binary
23587 corrupted .debug_macro sections.
23588
23589 For more info, see
23590 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23591 complaint (_("debug info gives %s invalid macro %s "
23592 "without body (corrupted?) at line %d "
23593 "on file %s"),
23594 at_commandline ? _("command-line") : _("in-file"),
23595 is_define ? _("definition") : _("undefinition"),
23596 line, current_file->filename);
23597 }
23598 else if (is_define)
23599 parse_macro_definition (current_file, line, body);
23600 else
23601 {
23602 gdb_assert (macinfo_type == DW_MACRO_undef
23603 || macinfo_type == DW_MACRO_undef_strp
23604 || macinfo_type == DW_MACRO_undef_sup);
23605 macro_undef (current_file, line, body);
23606 }
23607 }
23608 break;
23609
23610 case DW_MACRO_start_file:
23611 {
23612 unsigned int bytes_read;
23613 int line, file;
23614
23615 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23616 mac_ptr += bytes_read;
23617 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23618 mac_ptr += bytes_read;
23619
23620 if ((line == 0 && !at_commandline)
23621 || (line != 0 && at_commandline))
23622 complaint (_("debug info gives source %d included "
23623 "from %s at %s line %d"),
23624 file, at_commandline ? _("command-line") : _("file"),
23625 line == 0 ? _("zero") : _("non-zero"), line);
23626
23627 if (at_commandline)
23628 {
23629 /* This DW_MACRO_start_file was executed in the
23630 pass one. */
23631 at_commandline = 0;
23632 }
23633 else
23634 current_file = macro_start_file (cu, file, line, current_file,
23635 lh);
23636 }
23637 break;
23638
23639 case DW_MACRO_end_file:
23640 if (! current_file)
23641 complaint (_("macro debug info has an unmatched "
23642 "`close_file' directive"));
23643 else
23644 {
23645 current_file = current_file->included_by;
23646 if (! current_file)
23647 {
23648 enum dwarf_macro_record_type next_type;
23649
23650 /* GCC circa March 2002 doesn't produce the zero
23651 type byte marking the end of the compilation
23652 unit. Complain if it's not there, but exit no
23653 matter what. */
23654
23655 /* Do we at least have room for a macinfo type byte? */
23656 if (mac_ptr >= mac_end)
23657 {
23658 dwarf2_section_buffer_overflow_complaint (section);
23659 return;
23660 }
23661
23662 /* We don't increment mac_ptr here, so this is just
23663 a look-ahead. */
23664 next_type
23665 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23666 mac_ptr);
23667 if (next_type != 0)
23668 complaint (_("no terminating 0-type entry for "
23669 "macros in `.debug_macinfo' section"));
23670
23671 return;
23672 }
23673 }
23674 break;
23675
23676 case DW_MACRO_import:
23677 case DW_MACRO_import_sup:
23678 {
23679 LONGEST offset;
23680 void **slot;
23681 bfd *include_bfd = abfd;
23682 struct dwarf2_section_info *include_section = section;
23683 const gdb_byte *include_mac_end = mac_end;
23684 int is_dwz = section_is_dwz;
23685 const gdb_byte *new_mac_ptr;
23686
23687 offset = read_offset (abfd, mac_ptr, offset_size);
23688 mac_ptr += offset_size;
23689
23690 if (macinfo_type == DW_MACRO_import_sup)
23691 {
23692 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23693
23694 dwz->macro.read (objfile);
23695
23696 include_section = &dwz->macro;
23697 include_bfd = include_section->get_bfd_owner ();
23698 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23699 is_dwz = 1;
23700 }
23701
23702 new_mac_ptr = include_section->buffer + offset;
23703 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23704
23705 if (*slot != NULL)
23706 {
23707 /* This has actually happened; see
23708 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23709 complaint (_("recursive DW_MACRO_import in "
23710 ".debug_macro section"));
23711 }
23712 else
23713 {
23714 *slot = (void *) new_mac_ptr;
23715
23716 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23717 include_mac_end, current_file, lh,
23718 section, section_is_gnu, is_dwz,
23719 offset_size, include_hash);
23720
23721 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23722 }
23723 }
23724 break;
23725
23726 case DW_MACINFO_vendor_ext:
23727 if (!section_is_gnu)
23728 {
23729 unsigned int bytes_read;
23730
23731 /* This reads the constant, but since we don't recognize
23732 any vendor extensions, we ignore it. */
23733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23734 mac_ptr += bytes_read;
23735 read_direct_string (abfd, mac_ptr, &bytes_read);
23736 mac_ptr += bytes_read;
23737
23738 /* We don't recognize any vendor extensions. */
23739 break;
23740 }
23741 /* FALLTHROUGH */
23742
23743 default:
23744 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23745 mac_ptr, mac_end, abfd, offset_size,
23746 section);
23747 if (mac_ptr == NULL)
23748 return;
23749 break;
23750 }
23751 DIAGNOSTIC_POP
23752 } while (macinfo_type != 0);
23753 }
23754
23755 static void
23756 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23757 int section_is_gnu)
23758 {
23759 struct dwarf2_per_objfile *dwarf2_per_objfile
23760 = cu->per_cu->dwarf2_per_objfile;
23761 struct objfile *objfile = dwarf2_per_objfile->objfile;
23762 struct line_header *lh = cu->line_header;
23763 bfd *abfd;
23764 const gdb_byte *mac_ptr, *mac_end;
23765 struct macro_source_file *current_file = 0;
23766 enum dwarf_macro_record_type macinfo_type;
23767 unsigned int offset_size = cu->header.offset_size;
23768 const gdb_byte *opcode_definitions[256];
23769 void **slot;
23770 struct dwarf2_section_info *section;
23771 const char *section_name;
23772
23773 if (cu->dwo_unit != NULL)
23774 {
23775 if (section_is_gnu)
23776 {
23777 section = &cu->dwo_unit->dwo_file->sections.macro;
23778 section_name = ".debug_macro.dwo";
23779 }
23780 else
23781 {
23782 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23783 section_name = ".debug_macinfo.dwo";
23784 }
23785 }
23786 else
23787 {
23788 if (section_is_gnu)
23789 {
23790 section = &dwarf2_per_objfile->macro;
23791 section_name = ".debug_macro";
23792 }
23793 else
23794 {
23795 section = &dwarf2_per_objfile->macinfo;
23796 section_name = ".debug_macinfo";
23797 }
23798 }
23799
23800 section->read (objfile);
23801 if (section->buffer == NULL)
23802 {
23803 complaint (_("missing %s section"), section_name);
23804 return;
23805 }
23806 abfd = section->get_bfd_owner ();
23807
23808 /* First pass: Find the name of the base filename.
23809 This filename is needed in order to process all macros whose definition
23810 (or undefinition) comes from the command line. These macros are defined
23811 before the first DW_MACINFO_start_file entry, and yet still need to be
23812 associated to the base file.
23813
23814 To determine the base file name, we scan the macro definitions until we
23815 reach the first DW_MACINFO_start_file entry. We then initialize
23816 CURRENT_FILE accordingly so that any macro definition found before the
23817 first DW_MACINFO_start_file can still be associated to the base file. */
23818
23819 mac_ptr = section->buffer + offset;
23820 mac_end = section->buffer + section->size;
23821
23822 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23823 &offset_size, section_is_gnu);
23824 if (mac_ptr == NULL)
23825 {
23826 /* We already issued a complaint. */
23827 return;
23828 }
23829
23830 do
23831 {
23832 /* Do we at least have room for a macinfo type byte? */
23833 if (mac_ptr >= mac_end)
23834 {
23835 /* Complaint is printed during the second pass as GDB will probably
23836 stop the first pass earlier upon finding
23837 DW_MACINFO_start_file. */
23838 break;
23839 }
23840
23841 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23842 mac_ptr++;
23843
23844 /* Note that we rely on the fact that the corresponding GNU and
23845 DWARF constants are the same. */
23846 DIAGNOSTIC_PUSH
23847 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23848 switch (macinfo_type)
23849 {
23850 /* A zero macinfo type indicates the end of the macro
23851 information. */
23852 case 0:
23853 break;
23854
23855 case DW_MACRO_define:
23856 case DW_MACRO_undef:
23857 /* Only skip the data by MAC_PTR. */
23858 {
23859 unsigned int bytes_read;
23860
23861 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23862 mac_ptr += bytes_read;
23863 read_direct_string (abfd, mac_ptr, &bytes_read);
23864 mac_ptr += bytes_read;
23865 }
23866 break;
23867
23868 case DW_MACRO_start_file:
23869 {
23870 unsigned int bytes_read;
23871 int line, file;
23872
23873 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23874 mac_ptr += bytes_read;
23875 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23876 mac_ptr += bytes_read;
23877
23878 current_file = macro_start_file (cu, file, line, current_file, lh);
23879 }
23880 break;
23881
23882 case DW_MACRO_end_file:
23883 /* No data to skip by MAC_PTR. */
23884 break;
23885
23886 case DW_MACRO_define_strp:
23887 case DW_MACRO_undef_strp:
23888 case DW_MACRO_define_sup:
23889 case DW_MACRO_undef_sup:
23890 {
23891 unsigned int bytes_read;
23892
23893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23894 mac_ptr += bytes_read;
23895 mac_ptr += offset_size;
23896 }
23897 break;
23898
23899 case DW_MACRO_import:
23900 case DW_MACRO_import_sup:
23901 /* Note that, according to the spec, a transparent include
23902 chain cannot call DW_MACRO_start_file. So, we can just
23903 skip this opcode. */
23904 mac_ptr += offset_size;
23905 break;
23906
23907 case DW_MACINFO_vendor_ext:
23908 /* Only skip the data by MAC_PTR. */
23909 if (!section_is_gnu)
23910 {
23911 unsigned int bytes_read;
23912
23913 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23914 mac_ptr += bytes_read;
23915 read_direct_string (abfd, mac_ptr, &bytes_read);
23916 mac_ptr += bytes_read;
23917 }
23918 /* FALLTHROUGH */
23919
23920 default:
23921 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23922 mac_ptr, mac_end, abfd, offset_size,
23923 section);
23924 if (mac_ptr == NULL)
23925 return;
23926 break;
23927 }
23928 DIAGNOSTIC_POP
23929 } while (macinfo_type != 0 && current_file == NULL);
23930
23931 /* Second pass: Process all entries.
23932
23933 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23934 command-line macro definitions/undefinitions. This flag is unset when we
23935 reach the first DW_MACINFO_start_file entry. */
23936
23937 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23938 htab_eq_pointer,
23939 NULL, xcalloc, xfree));
23940 mac_ptr = section->buffer + offset;
23941 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23942 *slot = (void *) mac_ptr;
23943 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23944 current_file, lh, section,
23945 section_is_gnu, 0, offset_size,
23946 include_hash.get ());
23947 }
23948
23949 /* Return the .debug_loc section to use for CU.
23950 For DWO files use .debug_loc.dwo. */
23951
23952 static struct dwarf2_section_info *
23953 cu_debug_loc_section (struct dwarf2_cu *cu)
23954 {
23955 struct dwarf2_per_objfile *dwarf2_per_objfile
23956 = cu->per_cu->dwarf2_per_objfile;
23957
23958 if (cu->dwo_unit)
23959 {
23960 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23961
23962 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23963 }
23964 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23965 : &dwarf2_per_objfile->loc);
23966 }
23967
23968 /* A helper function that fills in a dwarf2_loclist_baton. */
23969
23970 static void
23971 fill_in_loclist_baton (struct dwarf2_cu *cu,
23972 struct dwarf2_loclist_baton *baton,
23973 const struct attribute *attr)
23974 {
23975 struct dwarf2_per_objfile *dwarf2_per_objfile
23976 = cu->per_cu->dwarf2_per_objfile;
23977 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23978
23979 section->read (dwarf2_per_objfile->objfile);
23980
23981 baton->per_cu = cu->per_cu;
23982 gdb_assert (baton->per_cu);
23983 /* We don't know how long the location list is, but make sure we
23984 don't run off the edge of the section. */
23985 baton->size = section->size - DW_UNSND (attr);
23986 baton->data = section->buffer + DW_UNSND (attr);
23987 baton->base_address = cu->base_address;
23988 baton->from_dwo = cu->dwo_unit != NULL;
23989 }
23990
23991 static void
23992 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23993 struct dwarf2_cu *cu, int is_block)
23994 {
23995 struct dwarf2_per_objfile *dwarf2_per_objfile
23996 = cu->per_cu->dwarf2_per_objfile;
23997 struct objfile *objfile = dwarf2_per_objfile->objfile;
23998 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23999
24000 if (attr->form_is_section_offset ()
24001 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24002 the section. If so, fall through to the complaint in the
24003 other branch. */
24004 && DW_UNSND (attr) < section->get_size (objfile))
24005 {
24006 struct dwarf2_loclist_baton *baton;
24007
24008 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24009
24010 fill_in_loclist_baton (cu, baton, attr);
24011
24012 if (cu->base_known == 0)
24013 complaint (_("Location list used without "
24014 "specifying the CU base address."));
24015
24016 SYMBOL_ACLASS_INDEX (sym) = (is_block
24017 ? dwarf2_loclist_block_index
24018 : dwarf2_loclist_index);
24019 SYMBOL_LOCATION_BATON (sym) = baton;
24020 }
24021 else
24022 {
24023 struct dwarf2_locexpr_baton *baton;
24024
24025 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24026 baton->per_cu = cu->per_cu;
24027 gdb_assert (baton->per_cu);
24028
24029 if (attr->form_is_block ())
24030 {
24031 /* Note that we're just copying the block's data pointer
24032 here, not the actual data. We're still pointing into the
24033 info_buffer for SYM's objfile; right now we never release
24034 that buffer, but when we do clean up properly this may
24035 need to change. */
24036 baton->size = DW_BLOCK (attr)->size;
24037 baton->data = DW_BLOCK (attr)->data;
24038 }
24039 else
24040 {
24041 dwarf2_invalid_attrib_class_complaint ("location description",
24042 sym->natural_name ());
24043 baton->size = 0;
24044 }
24045
24046 SYMBOL_ACLASS_INDEX (sym) = (is_block
24047 ? dwarf2_locexpr_block_index
24048 : dwarf2_locexpr_index);
24049 SYMBOL_LOCATION_BATON (sym) = baton;
24050 }
24051 }
24052
24053 /* See read.h. */
24054
24055 struct objfile *
24056 dwarf2_per_cu_data::objfile () const
24057 {
24058 struct objfile *objfile = dwarf2_per_objfile->objfile;
24059
24060 /* Return the master objfile, so that we can report and look up the
24061 correct file containing this variable. */
24062 if (objfile->separate_debug_objfile_backlink)
24063 objfile = objfile->separate_debug_objfile_backlink;
24064
24065 return objfile;
24066 }
24067
24068 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24069 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24070 CU_HEADERP first. */
24071
24072 static const struct comp_unit_head *
24073 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24074 const struct dwarf2_per_cu_data *per_cu)
24075 {
24076 const gdb_byte *info_ptr;
24077
24078 if (per_cu->cu)
24079 return &per_cu->cu->header;
24080
24081 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24082
24083 memset (cu_headerp, 0, sizeof (*cu_headerp));
24084 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24085 rcuh_kind::COMPILE);
24086
24087 return cu_headerp;
24088 }
24089
24090 /* See read.h. */
24091
24092 int
24093 dwarf2_per_cu_data::addr_size () const
24094 {
24095 struct comp_unit_head cu_header_local;
24096 const struct comp_unit_head *cu_headerp;
24097
24098 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24099
24100 return cu_headerp->addr_size;
24101 }
24102
24103 /* See read.h. */
24104
24105 int
24106 dwarf2_per_cu_data::offset_size () const
24107 {
24108 struct comp_unit_head cu_header_local;
24109 const struct comp_unit_head *cu_headerp;
24110
24111 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24112
24113 return cu_headerp->offset_size;
24114 }
24115
24116 /* See read.h. */
24117
24118 int
24119 dwarf2_per_cu_data::ref_addr_size () const
24120 {
24121 struct comp_unit_head cu_header_local;
24122 const struct comp_unit_head *cu_headerp;
24123
24124 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24125
24126 if (cu_headerp->version == 2)
24127 return cu_headerp->addr_size;
24128 else
24129 return cu_headerp->offset_size;
24130 }
24131
24132 /* See read.h. */
24133
24134 CORE_ADDR
24135 dwarf2_per_cu_data::text_offset () const
24136 {
24137 struct objfile *objfile = dwarf2_per_objfile->objfile;
24138
24139 return objfile->text_section_offset ();
24140 }
24141
24142 /* See read.h. */
24143
24144 struct type *
24145 dwarf2_per_cu_data::addr_type () const
24146 {
24147 struct objfile *objfile = dwarf2_per_objfile->objfile;
24148 struct type *void_type = objfile_type (objfile)->builtin_void;
24149 struct type *addr_type = lookup_pointer_type (void_type);
24150 int addr_size = this->addr_size ();
24151
24152 if (TYPE_LENGTH (addr_type) == addr_size)
24153 return addr_type;
24154
24155 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24156 return addr_type;
24157 }
24158
24159 /* Locate the .debug_info compilation unit from CU's objfile which contains
24160 the DIE at OFFSET. Raises an error on failure. */
24161
24162 static struct dwarf2_per_cu_data *
24163 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24164 unsigned int offset_in_dwz,
24165 struct dwarf2_per_objfile *dwarf2_per_objfile)
24166 {
24167 struct dwarf2_per_cu_data *this_cu;
24168 int low, high;
24169
24170 low = 0;
24171 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24172 while (high > low)
24173 {
24174 struct dwarf2_per_cu_data *mid_cu;
24175 int mid = low + (high - low) / 2;
24176
24177 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24178 if (mid_cu->is_dwz > offset_in_dwz
24179 || (mid_cu->is_dwz == offset_in_dwz
24180 && mid_cu->sect_off + mid_cu->length >= sect_off))
24181 high = mid;
24182 else
24183 low = mid + 1;
24184 }
24185 gdb_assert (low == high);
24186 this_cu = dwarf2_per_objfile->all_comp_units[low];
24187 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24188 {
24189 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24190 error (_("Dwarf Error: could not find partial DIE containing "
24191 "offset %s [in module %s]"),
24192 sect_offset_str (sect_off),
24193 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24194
24195 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24196 <= sect_off);
24197 return dwarf2_per_objfile->all_comp_units[low-1];
24198 }
24199 else
24200 {
24201 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24202 && sect_off >= this_cu->sect_off + this_cu->length)
24203 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24204 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24205 return this_cu;
24206 }
24207 }
24208
24209 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24210
24211 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24212 : per_cu (per_cu_),
24213 mark (false),
24214 has_loclist (false),
24215 checked_producer (false),
24216 producer_is_gxx_lt_4_6 (false),
24217 producer_is_gcc_lt_4_3 (false),
24218 producer_is_icc (false),
24219 producer_is_icc_lt_14 (false),
24220 producer_is_codewarrior (false),
24221 processing_has_namespace_info (false)
24222 {
24223 per_cu->cu = this;
24224 }
24225
24226 /* Destroy a dwarf2_cu. */
24227
24228 dwarf2_cu::~dwarf2_cu ()
24229 {
24230 per_cu->cu = NULL;
24231 }
24232
24233 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24234
24235 static void
24236 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24237 enum language pretend_language)
24238 {
24239 struct attribute *attr;
24240
24241 /* Set the language we're debugging. */
24242 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24243 if (attr != nullptr)
24244 set_cu_language (DW_UNSND (attr), cu);
24245 else
24246 {
24247 cu->language = pretend_language;
24248 cu->language_defn = language_def (cu->language);
24249 }
24250
24251 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24252 }
24253
24254 /* Increase the age counter on each cached compilation unit, and free
24255 any that are too old. */
24256
24257 static void
24258 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24259 {
24260 struct dwarf2_per_cu_data *per_cu, **last_chain;
24261
24262 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24263 per_cu = dwarf2_per_objfile->read_in_chain;
24264 while (per_cu != NULL)
24265 {
24266 per_cu->cu->last_used ++;
24267 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24268 dwarf2_mark (per_cu->cu);
24269 per_cu = per_cu->cu->read_in_chain;
24270 }
24271
24272 per_cu = dwarf2_per_objfile->read_in_chain;
24273 last_chain = &dwarf2_per_objfile->read_in_chain;
24274 while (per_cu != NULL)
24275 {
24276 struct dwarf2_per_cu_data *next_cu;
24277
24278 next_cu = per_cu->cu->read_in_chain;
24279
24280 if (!per_cu->cu->mark)
24281 {
24282 delete per_cu->cu;
24283 *last_chain = next_cu;
24284 }
24285 else
24286 last_chain = &per_cu->cu->read_in_chain;
24287
24288 per_cu = next_cu;
24289 }
24290 }
24291
24292 /* Remove a single compilation unit from the cache. */
24293
24294 static void
24295 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24296 {
24297 struct dwarf2_per_cu_data *per_cu, **last_chain;
24298 struct dwarf2_per_objfile *dwarf2_per_objfile
24299 = target_per_cu->dwarf2_per_objfile;
24300
24301 per_cu = dwarf2_per_objfile->read_in_chain;
24302 last_chain = &dwarf2_per_objfile->read_in_chain;
24303 while (per_cu != NULL)
24304 {
24305 struct dwarf2_per_cu_data *next_cu;
24306
24307 next_cu = per_cu->cu->read_in_chain;
24308
24309 if (per_cu == target_per_cu)
24310 {
24311 delete per_cu->cu;
24312 per_cu->cu = NULL;
24313 *last_chain = next_cu;
24314 break;
24315 }
24316 else
24317 last_chain = &per_cu->cu->read_in_chain;
24318
24319 per_cu = next_cu;
24320 }
24321 }
24322
24323 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24324 We store these in a hash table separate from the DIEs, and preserve them
24325 when the DIEs are flushed out of cache.
24326
24327 The CU "per_cu" pointer is needed because offset alone is not enough to
24328 uniquely identify the type. A file may have multiple .debug_types sections,
24329 or the type may come from a DWO file. Furthermore, while it's more logical
24330 to use per_cu->section+offset, with Fission the section with the data is in
24331 the DWO file but we don't know that section at the point we need it.
24332 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24333 because we can enter the lookup routine, get_die_type_at_offset, from
24334 outside this file, and thus won't necessarily have PER_CU->cu.
24335 Fortunately, PER_CU is stable for the life of the objfile. */
24336
24337 struct dwarf2_per_cu_offset_and_type
24338 {
24339 const struct dwarf2_per_cu_data *per_cu;
24340 sect_offset sect_off;
24341 struct type *type;
24342 };
24343
24344 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24345
24346 static hashval_t
24347 per_cu_offset_and_type_hash (const void *item)
24348 {
24349 const struct dwarf2_per_cu_offset_and_type *ofs
24350 = (const struct dwarf2_per_cu_offset_and_type *) item;
24351
24352 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24353 }
24354
24355 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24356
24357 static int
24358 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24359 {
24360 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24361 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24362 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24363 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24364
24365 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24366 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24367 }
24368
24369 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24370 table if necessary. For convenience, return TYPE.
24371
24372 The DIEs reading must have careful ordering to:
24373 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24374 reading current DIE.
24375 * Not trying to dereference contents of still incompletely read in types
24376 while reading in other DIEs.
24377 * Enable referencing still incompletely read in types just by a pointer to
24378 the type without accessing its fields.
24379
24380 Therefore caller should follow these rules:
24381 * Try to fetch any prerequisite types we may need to build this DIE type
24382 before building the type and calling set_die_type.
24383 * After building type call set_die_type for current DIE as soon as
24384 possible before fetching more types to complete the current type.
24385 * Make the type as complete as possible before fetching more types. */
24386
24387 static struct type *
24388 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24389 {
24390 struct dwarf2_per_objfile *dwarf2_per_objfile
24391 = cu->per_cu->dwarf2_per_objfile;
24392 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24393 struct objfile *objfile = dwarf2_per_objfile->objfile;
24394 struct attribute *attr;
24395 struct dynamic_prop prop;
24396
24397 /* For Ada types, make sure that the gnat-specific data is always
24398 initialized (if not already set). There are a few types where
24399 we should not be doing so, because the type-specific area is
24400 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24401 where the type-specific area is used to store the floatformat).
24402 But this is not a problem, because the gnat-specific information
24403 is actually not needed for these types. */
24404 if (need_gnat_info (cu)
24405 && TYPE_CODE (type) != TYPE_CODE_FUNC
24406 && TYPE_CODE (type) != TYPE_CODE_FLT
24407 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24408 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24409 && TYPE_CODE (type) != TYPE_CODE_METHOD
24410 && !HAVE_GNAT_AUX_INFO (type))
24411 INIT_GNAT_SPECIFIC (type);
24412
24413 /* Read DW_AT_allocated and set in type. */
24414 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24415 if (attr != NULL && attr->form_is_block ())
24416 {
24417 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24418 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24419 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24420 }
24421 else if (attr != NULL)
24422 {
24423 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24424 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24425 sect_offset_str (die->sect_off));
24426 }
24427
24428 /* Read DW_AT_associated and set in type. */
24429 attr = dwarf2_attr (die, DW_AT_associated, cu);
24430 if (attr != NULL && attr->form_is_block ())
24431 {
24432 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24433 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24434 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24435 }
24436 else if (attr != NULL)
24437 {
24438 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24439 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24440 sect_offset_str (die->sect_off));
24441 }
24442
24443 /* Read DW_AT_data_location and set in type. */
24444 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24445 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24446 cu->per_cu->addr_type ()))
24447 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24448
24449 if (dwarf2_per_objfile->die_type_hash == NULL)
24450 dwarf2_per_objfile->die_type_hash
24451 = htab_up (htab_create_alloc (127,
24452 per_cu_offset_and_type_hash,
24453 per_cu_offset_and_type_eq,
24454 NULL, xcalloc, xfree));
24455
24456 ofs.per_cu = cu->per_cu;
24457 ofs.sect_off = die->sect_off;
24458 ofs.type = type;
24459 slot = (struct dwarf2_per_cu_offset_and_type **)
24460 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24461 if (*slot)
24462 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24463 sect_offset_str (die->sect_off));
24464 *slot = XOBNEW (&objfile->objfile_obstack,
24465 struct dwarf2_per_cu_offset_and_type);
24466 **slot = ofs;
24467 return type;
24468 }
24469
24470 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24471 or return NULL if the die does not have a saved type. */
24472
24473 static struct type *
24474 get_die_type_at_offset (sect_offset sect_off,
24475 struct dwarf2_per_cu_data *per_cu)
24476 {
24477 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24478 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24479
24480 if (dwarf2_per_objfile->die_type_hash == NULL)
24481 return NULL;
24482
24483 ofs.per_cu = per_cu;
24484 ofs.sect_off = sect_off;
24485 slot = ((struct dwarf2_per_cu_offset_and_type *)
24486 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24487 if (slot)
24488 return slot->type;
24489 else
24490 return NULL;
24491 }
24492
24493 /* Look up the type for DIE in CU in die_type_hash,
24494 or return NULL if DIE does not have a saved type. */
24495
24496 static struct type *
24497 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24498 {
24499 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24500 }
24501
24502 /* Add a dependence relationship from CU to REF_PER_CU. */
24503
24504 static void
24505 dwarf2_add_dependence (struct dwarf2_cu *cu,
24506 struct dwarf2_per_cu_data *ref_per_cu)
24507 {
24508 void **slot;
24509
24510 if (cu->dependencies == NULL)
24511 cu->dependencies
24512 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24513 NULL, &cu->comp_unit_obstack,
24514 hashtab_obstack_allocate,
24515 dummy_obstack_deallocate);
24516
24517 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24518 if (*slot == NULL)
24519 *slot = ref_per_cu;
24520 }
24521
24522 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24523 Set the mark field in every compilation unit in the
24524 cache that we must keep because we are keeping CU. */
24525
24526 static int
24527 dwarf2_mark_helper (void **slot, void *data)
24528 {
24529 struct dwarf2_per_cu_data *per_cu;
24530
24531 per_cu = (struct dwarf2_per_cu_data *) *slot;
24532
24533 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24534 reading of the chain. As such dependencies remain valid it is not much
24535 useful to track and undo them during QUIT cleanups. */
24536 if (per_cu->cu == NULL)
24537 return 1;
24538
24539 if (per_cu->cu->mark)
24540 return 1;
24541 per_cu->cu->mark = true;
24542
24543 if (per_cu->cu->dependencies != NULL)
24544 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24545
24546 return 1;
24547 }
24548
24549 /* Set the mark field in CU and in every other compilation unit in the
24550 cache that we must keep because we are keeping CU. */
24551
24552 static void
24553 dwarf2_mark (struct dwarf2_cu *cu)
24554 {
24555 if (cu->mark)
24556 return;
24557 cu->mark = true;
24558 if (cu->dependencies != NULL)
24559 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24560 }
24561
24562 static void
24563 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24564 {
24565 while (per_cu)
24566 {
24567 per_cu->cu->mark = false;
24568 per_cu = per_cu->cu->read_in_chain;
24569 }
24570 }
24571
24572 /* Trivial hash function for partial_die_info: the hash value of a DIE
24573 is its offset in .debug_info for this objfile. */
24574
24575 static hashval_t
24576 partial_die_hash (const void *item)
24577 {
24578 const struct partial_die_info *part_die
24579 = (const struct partial_die_info *) item;
24580
24581 return to_underlying (part_die->sect_off);
24582 }
24583
24584 /* Trivial comparison function for partial_die_info structures: two DIEs
24585 are equal if they have the same offset. */
24586
24587 static int
24588 partial_die_eq (const void *item_lhs, const void *item_rhs)
24589 {
24590 const struct partial_die_info *part_die_lhs
24591 = (const struct partial_die_info *) item_lhs;
24592 const struct partial_die_info *part_die_rhs
24593 = (const struct partial_die_info *) item_rhs;
24594
24595 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24596 }
24597
24598 struct cmd_list_element *set_dwarf_cmdlist;
24599 struct cmd_list_element *show_dwarf_cmdlist;
24600
24601 static void
24602 set_dwarf_cmd (const char *args, int from_tty)
24603 {
24604 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24605 gdb_stdout);
24606 }
24607
24608 static void
24609 show_dwarf_cmd (const char *args, int from_tty)
24610 {
24611 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24612 }
24613
24614 static void
24615 show_check_physname (struct ui_file *file, int from_tty,
24616 struct cmd_list_element *c, const char *value)
24617 {
24618 fprintf_filtered (file,
24619 _("Whether to check \"physname\" is %s.\n"),
24620 value);
24621 }
24622
24623 void _initialize_dwarf2_read ();
24624 void
24625 _initialize_dwarf2_read ()
24626 {
24627 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24628 Set DWARF specific variables.\n\
24629 Configure DWARF variables such as the cache size."),
24630 &set_dwarf_cmdlist, "maintenance set dwarf ",
24631 0/*allow-unknown*/, &maintenance_set_cmdlist);
24632
24633 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24634 Show DWARF specific variables.\n\
24635 Show DWARF variables such as the cache size."),
24636 &show_dwarf_cmdlist, "maintenance show dwarf ",
24637 0/*allow-unknown*/, &maintenance_show_cmdlist);
24638
24639 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24640 &dwarf_max_cache_age, _("\
24641 Set the upper bound on the age of cached DWARF compilation units."), _("\
24642 Show the upper bound on the age of cached DWARF compilation units."), _("\
24643 A higher limit means that cached compilation units will be stored\n\
24644 in memory longer, and more total memory will be used. Zero disables\n\
24645 caching, which can slow down startup."),
24646 NULL,
24647 show_dwarf_max_cache_age,
24648 &set_dwarf_cmdlist,
24649 &show_dwarf_cmdlist);
24650
24651 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24652 Set debugging of the DWARF reader."), _("\
24653 Show debugging of the DWARF reader."), _("\
24654 When enabled (non-zero), debugging messages are printed during DWARF\n\
24655 reading and symtab expansion. A value of 1 (one) provides basic\n\
24656 information. A value greater than 1 provides more verbose information."),
24657 NULL,
24658 NULL,
24659 &setdebuglist, &showdebuglist);
24660
24661 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24662 Set debugging of the DWARF DIE reader."), _("\
24663 Show debugging of the DWARF DIE reader."), _("\
24664 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24665 The value is the maximum depth to print."),
24666 NULL,
24667 NULL,
24668 &setdebuglist, &showdebuglist);
24669
24670 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24671 Set debugging of the dwarf line reader."), _("\
24672 Show debugging of the dwarf line reader."), _("\
24673 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24674 A value of 1 (one) provides basic information.\n\
24675 A value greater than 1 provides more verbose information."),
24676 NULL,
24677 NULL,
24678 &setdebuglist, &showdebuglist);
24679
24680 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24681 Set cross-checking of \"physname\" code against demangler."), _("\
24682 Show cross-checking of \"physname\" code against demangler."), _("\
24683 When enabled, GDB's internal \"physname\" code is checked against\n\
24684 the demangler."),
24685 NULL, show_check_physname,
24686 &setdebuglist, &showdebuglist);
24687
24688 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24689 no_class, &use_deprecated_index_sections, _("\
24690 Set whether to use deprecated gdb_index sections."), _("\
24691 Show whether to use deprecated gdb_index sections."), _("\
24692 When enabled, deprecated .gdb_index sections are used anyway.\n\
24693 Normally they are ignored either because of a missing feature or\n\
24694 performance issue.\n\
24695 Warning: This option must be enabled before gdb reads the file."),
24696 NULL,
24697 NULL,
24698 &setlist, &showlist);
24699
24700 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24701 &dwarf2_locexpr_funcs);
24702 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24703 &dwarf2_loclist_funcs);
24704
24705 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24706 &dwarf2_block_frame_base_locexpr_funcs);
24707 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24708 &dwarf2_block_frame_base_loclist_funcs);
24709
24710 #if GDB_SELF_TEST
24711 selftests::register_test ("dw2_expand_symtabs_matching",
24712 selftests::dw2_expand_symtabs_matching::run_test);
24713 #endif
24714 }
This page took 0.577241 seconds and 4 git commands to generate.