[gdb] Speedup lnp_state_machine::handle_special_opcode
[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
86 /* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89 static unsigned int dwarf_read_debug = 0;
90
91 /* When non-zero, dump DIEs after they are read in. */
92 static unsigned int dwarf_die_debug = 0;
93
94 /* When non-zero, dump line number entries as they are read in. */
95 unsigned int dwarf_line_debug = 0;
96
97 /* When true, cross-check physname against demangler. */
98 static bool check_physname = false;
99
100 /* When true, do not reject deprecated .gdb_index sections. */
101 static bool use_deprecated_index_sections = false;
102
103 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
104
105 /* The "aclass" indices for various kinds of computed DWARF symbols. */
106
107 static int dwarf2_locexpr_index;
108 static int dwarf2_loclist_index;
109 static int dwarf2_locexpr_block_index;
110 static int dwarf2_loclist_block_index;
111
112 /* An index into a (C++) symbol name component in a symbol name as
113 recorded in the mapped_index's symbol table. For each C++ symbol
114 in the symbol table, we record one entry for the start of each
115 component in the symbol in a table of name components, and then
116 sort the table, in order to be able to binary search symbol names,
117 ignoring leading namespaces, both completion and regular look up.
118 For example, for symbol "A::B::C", we'll have an entry that points
119 to "A::B::C", another that points to "B::C", and another for "C".
120 Note that function symbols in GDB index have no parameter
121 information, just the function/method names. You can convert a
122 name_component to a "const char *" using the
123 'mapped_index::symbol_name_at(offset_type)' method. */
124
125 struct name_component
126 {
127 /* Offset in the symbol name where the component starts. Stored as
128 a (32-bit) offset instead of a pointer to save memory and improve
129 locality on 64-bit architectures. */
130 offset_type name_offset;
131
132 /* The symbol's index in the symbol and constant pool tables of a
133 mapped_index. */
134 offset_type idx;
135 };
136
137 /* Base class containing bits shared by both .gdb_index and
138 .debug_name indexes. */
139
140 struct mapped_index_base
141 {
142 mapped_index_base () = default;
143 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
144
145 /* The name_component table (a sorted vector). See name_component's
146 description above. */
147 std::vector<name_component> name_components;
148
149 /* How NAME_COMPONENTS is sorted. */
150 enum case_sensitivity name_components_casing;
151
152 /* Return the number of names in the symbol table. */
153 virtual size_t symbol_name_count () const = 0;
154
155 /* Get the name of the symbol at IDX in the symbol table. */
156 virtual const char *symbol_name_at (offset_type idx) const = 0;
157
158 /* Return whether the name at IDX in the symbol table should be
159 ignored. */
160 virtual bool symbol_name_slot_invalid (offset_type idx) const
161 {
162 return false;
163 }
164
165 /* Build the symbol name component sorted vector, if we haven't
166 yet. */
167 void build_name_components ();
168
169 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
170 possible matches for LN_NO_PARAMS in the name component
171 vector. */
172 std::pair<std::vector<name_component>::const_iterator,
173 std::vector<name_component>::const_iterator>
174 find_name_components_bounds (const lookup_name_info &ln_no_params,
175 enum language lang) const;
176
177 /* Prevent deleting/destroying via a base class pointer. */
178 protected:
179 ~mapped_index_base() = default;
180 };
181
182 /* A description of the mapped index. The file format is described in
183 a comment by the code that writes the index. */
184 struct mapped_index final : public mapped_index_base
185 {
186 /* A slot/bucket in the symbol table hash. */
187 struct symbol_table_slot
188 {
189 const offset_type name;
190 const offset_type vec;
191 };
192
193 /* Index data format version. */
194 int version = 0;
195
196 /* The address table data. */
197 gdb::array_view<const gdb_byte> address_table;
198
199 /* The symbol table, implemented as a hash table. */
200 gdb::array_view<symbol_table_slot> symbol_table;
201
202 /* A pointer to the constant pool. */
203 const char *constant_pool = nullptr;
204
205 bool symbol_name_slot_invalid (offset_type idx) const override
206 {
207 const auto &bucket = this->symbol_table[idx];
208 return bucket.name == 0 && bucket.vec == 0;
209 }
210
211 /* Convenience method to get at the name of the symbol at IDX in the
212 symbol table. */
213 const char *symbol_name_at (offset_type idx) const override
214 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
215
216 size_t symbol_name_count () const override
217 { return this->symbol_table.size (); }
218 };
219
220 /* A description of the mapped .debug_names.
221 Uninitialized map has CU_COUNT 0. */
222 struct mapped_debug_names final : public mapped_index_base
223 {
224 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
225 : dwarf2_per_objfile (dwarf2_per_objfile_)
226 {}
227
228 struct dwarf2_per_objfile *dwarf2_per_objfile;
229 bfd_endian dwarf5_byte_order;
230 bool dwarf5_is_dwarf64;
231 bool augmentation_is_gdb;
232 uint8_t offset_size;
233 uint32_t cu_count = 0;
234 uint32_t tu_count, bucket_count, name_count;
235 const gdb_byte *cu_table_reordered, *tu_table_reordered;
236 const uint32_t *bucket_table_reordered, *hash_table_reordered;
237 const gdb_byte *name_table_string_offs_reordered;
238 const gdb_byte *name_table_entry_offs_reordered;
239 const gdb_byte *entry_pool;
240
241 struct index_val
242 {
243 ULONGEST dwarf_tag;
244 struct attr
245 {
246 /* Attribute name DW_IDX_*. */
247 ULONGEST dw_idx;
248
249 /* Attribute form DW_FORM_*. */
250 ULONGEST form;
251
252 /* Value if FORM is DW_FORM_implicit_const. */
253 LONGEST implicit_const;
254 };
255 std::vector<attr> attr_vec;
256 };
257
258 std::unordered_map<ULONGEST, index_val> abbrev_map;
259
260 const char *namei_to_name (uint32_t namei) const;
261
262 /* Implementation of the mapped_index_base virtual interface, for
263 the name_components cache. */
264
265 const char *symbol_name_at (offset_type idx) const override
266 { return namei_to_name (idx); }
267
268 size_t symbol_name_count () const override
269 { return this->name_count; }
270 };
271
272 /* See dwarf2read.h. */
273
274 dwarf2_per_objfile *
275 get_dwarf2_per_objfile (struct objfile *objfile)
276 {
277 return dwarf2_objfile_data_key.get (objfile);
278 }
279
280 /* Default names of the debugging sections. */
281
282 /* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
285 static const struct dwarf2_debug_sections dwarf2_elf_names =
286 {
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
291 { ".debug_loclists", ".zdebug_loclists" },
292 { ".debug_macinfo", ".zdebug_macinfo" },
293 { ".debug_macro", ".zdebug_macro" },
294 { ".debug_str", ".zdebug_str" },
295 { ".debug_str_offsets", ".zdebug_str_offsets" },
296 { ".debug_line_str", ".zdebug_line_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_rnglists", ".zdebug_rnglists" },
299 { ".debug_types", ".zdebug_types" },
300 { ".debug_addr", ".zdebug_addr" },
301 { ".debug_frame", ".zdebug_frame" },
302 { ".eh_frame", NULL },
303 { ".gdb_index", ".zgdb_index" },
304 { ".debug_names", ".zdebug_names" },
305 { ".debug_aranges", ".zdebug_aranges" },
306 23
307 };
308
309 /* List of DWO/DWP sections. */
310
311 static const struct dwop_section_names
312 {
313 struct dwarf2_section_names abbrev_dwo;
314 struct dwarf2_section_names info_dwo;
315 struct dwarf2_section_names line_dwo;
316 struct dwarf2_section_names loc_dwo;
317 struct dwarf2_section_names loclists_dwo;
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
325 }
326 dwop_section_names =
327 {
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
332 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
333 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
334 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
335 { ".debug_str.dwo", ".zdebug_str.dwo" },
336 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
337 { ".debug_types.dwo", ".zdebug_types.dwo" },
338 { ".debug_cu_index", ".zdebug_cu_index" },
339 { ".debug_tu_index", ".zdebug_tu_index" },
340 };
341
342 /* local data types */
343
344 /* Type used for delaying computation of method physnames.
345 See comments for compute_delayed_physnames. */
346 struct delayed_method_info
347 {
348 /* The type to which the method is attached, i.e., its parent class. */
349 struct type *type;
350
351 /* The index of the method in the type's function fieldlists. */
352 int fnfield_index;
353
354 /* The index of the method in the fieldlist. */
355 int index;
356
357 /* The name of the DIE. */
358 const char *name;
359
360 /* The DIE associated with this method. */
361 struct die_info *die;
362 };
363
364 /* Internal state when decoding a particular compilation unit. */
365 struct dwarf2_cu
366 {
367 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
368 ~dwarf2_cu ();
369
370 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
371
372 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
373 Create the set of symtabs used by this TU, or if this TU is sharing
374 symtabs with another TU and the symtabs have already been created
375 then restore those symtabs in the line header.
376 We don't need the pc/line-number mapping for type units. */
377 void setup_type_unit_groups (struct die_info *die);
378
379 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
380 buildsym_compunit constructor. */
381 struct compunit_symtab *start_symtab (const char *name,
382 const char *comp_dir,
383 CORE_ADDR low_pc);
384
385 /* Reset the builder. */
386 void reset_builder () { m_builder.reset (); }
387
388 /* The header of the compilation unit. */
389 struct comp_unit_head header {};
390
391 /* Base address of this compilation unit. */
392 CORE_ADDR base_address = 0;
393
394 /* Non-zero if base_address has been set. */
395 int base_known = 0;
396
397 /* The language we are debugging. */
398 enum language language = language_unknown;
399 const struct language_defn *language_defn = nullptr;
400
401 const char *producer = nullptr;
402
403 private:
404 /* The symtab builder for this CU. This is only non-NULL when full
405 symbols are being read. */
406 std::unique_ptr<buildsym_compunit> m_builder;
407
408 public:
409 /* The generic symbol table building routines have separate lists for
410 file scope symbols and all all other scopes (local scopes). So
411 we need to select the right one to pass to add_symbol_to_list().
412 We do it by keeping a pointer to the correct list in list_in_scope.
413
414 FIXME: The original dwarf code just treated the file scope as the
415 first local scope, and all other local scopes as nested local
416 scopes, and worked fine. Check to see if we really need to
417 distinguish these in buildsym.c. */
418 struct pending **list_in_scope = nullptr;
419
420 /* Hash table holding all the loaded partial DIEs
421 with partial_die->offset.SECT_OFF as hash. */
422 htab_t partial_dies = nullptr;
423
424 /* Storage for things with the same lifetime as this read-in compilation
425 unit, including partial DIEs. */
426 auto_obstack comp_unit_obstack;
427
428 /* When multiple dwarf2_cu structures are living in memory, this field
429 chains them all together, so that they can be released efficiently.
430 We will probably also want a generation counter so that most-recently-used
431 compilation units are cached... */
432 struct dwarf2_per_cu_data *read_in_chain = nullptr;
433
434 /* Backlink to our per_cu entry. */
435 struct dwarf2_per_cu_data *per_cu;
436
437 /* How many compilation units ago was this CU last referenced? */
438 int last_used = 0;
439
440 /* A hash table of DIE cu_offset for following references with
441 die_info->offset.sect_off as hash. */
442 htab_t die_hash = nullptr;
443
444 /* Full DIEs if read in. */
445 struct die_info *dies = nullptr;
446
447 /* A set of pointers to dwarf2_per_cu_data objects for compilation
448 units referenced by this one. Only set during full symbol processing;
449 partial symbol tables do not have dependencies. */
450 htab_t dependencies = nullptr;
451
452 /* Header data from the line table, during full symbol processing. */
453 struct line_header *line_header = nullptr;
454 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
455 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
456 this is the DW_TAG_compile_unit die for this CU. We'll hold on
457 to the line header as long as this DIE is being processed. See
458 process_die_scope. */
459 die_info *line_header_die_owner = nullptr;
460
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 std::vector<delayed_method_info> method_list;
464
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab = nullptr;
467
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
477 struct dwo_unit *dwo_unit = nullptr;
478
479 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
480 Note this value comes from the Fission stub CU/TU's DIE. */
481 gdb::optional<ULONGEST> addr_base;
482
483 /* The DW_AT_rnglists_base attribute if present.
484 Note this value comes from the Fission stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
492 ULONGEST ranges_base = 0;
493
494 /* When reading debug info generated by older versions of rustc, we
495 have to rewrite some union types to be struct types with a
496 variant part. This rewriting must be done after the CU is fully
497 read in, because otherwise at the point of rewriting some struct
498 type might not have been fully processed. So, we keep a list of
499 all such types here and process them after expansion. */
500 std::vector<struct type *> rust_unions;
501
502 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
503 files, the value is implicitly zero. For DWARF 5 version DWO files, the
504 value is often implicit and is the size of the header of
505 .debug_str_offsets section (8 or 4, depending on the address size). */
506 gdb::optional<ULONGEST> str_offsets_base;
507
508 /* Mark used when releasing cached dies. */
509 bool mark : 1;
510
511 /* This CU references .debug_loc. See the symtab->locations_valid field.
512 This test is imperfect as there may exist optimized debug code not using
513 any location list and still facing inlining issues if handled as
514 unoptimized code. For a future better test see GCC PR other/32998. */
515 bool has_loclist : 1;
516
517 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
518 if all the producer_is_* fields are valid. This information is cached
519 because profiling CU expansion showed excessive time spent in
520 producer_is_gxx_lt_4_6. */
521 bool checked_producer : 1;
522 bool producer_is_gxx_lt_4_6 : 1;
523 bool producer_is_gcc_lt_4_3 : 1;
524 bool producer_is_icc : 1;
525 bool producer_is_icc_lt_14 : 1;
526 bool producer_is_codewarrior : 1;
527
528 /* When true, the file that we're processing is known to have
529 debugging info for C++ namespaces. GCC 3.3.x did not produce
530 this information, but later versions do. */
531
532 bool processing_has_namespace_info : 1;
533
534 struct partial_die_info *find_partial_die (sect_offset sect_off);
535
536 /* If this CU was inherited by another CU (via specification,
537 abstract_origin, etc), this is the ancestor CU. */
538 dwarf2_cu *ancestor;
539
540 /* Get the buildsym_compunit for this CU. */
541 buildsym_compunit *get_builder ()
542 {
543 /* If this CU has a builder associated with it, use that. */
544 if (m_builder != nullptr)
545 return m_builder.get ();
546
547 /* Otherwise, search ancestors for a valid builder. */
548 if (ancestor != nullptr)
549 return ancestor->get_builder ();
550
551 return nullptr;
552 }
553 };
554
555 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
556 This includes type_unit_group and quick_file_names. */
557
558 struct stmt_list_hash
559 {
560 /* The DWO unit this table is from or NULL if there is none. */
561 struct dwo_unit *dwo_unit;
562
563 /* Offset in .debug_line or .debug_line.dwo. */
564 sect_offset line_sect_off;
565 };
566
567 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
568 an object of this type. */
569
570 struct type_unit_group
571 {
572 /* dwarf2read.c's main "handle" on a TU symtab.
573 To simplify things we create an artificial CU that "includes" all the
574 type units using this stmt_list so that the rest of the code still has
575 a "per_cu" handle on the symtab.
576 This PER_CU is recognized by having no section. */
577 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
578 struct dwarf2_per_cu_data per_cu;
579
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
583 std::vector<signatured_type *> *tus;
584
585 /* The compunit symtab.
586 Type units in a group needn't all be defined in the same source file,
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
589
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
592
593 /* The number of symtabs from the line header.
594 The value here must match line_header.num_file_names. */
595 unsigned int num_symtabs;
596
597 /* The symbol tables for this TU (obtained from the files listed in
598 DW_AT_stmt_list).
599 WARNING: The order of entries here must match the order of entries
600 in the line header. After the first TU using this type_unit_group, the
601 line header for the subsequent TUs is recreated from this. This is done
602 because we need to use the same symtabs for each TU using the same
603 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
604 there's no guarantee the line header doesn't have duplicate entries. */
605 struct symtab **symtabs;
606 };
607
608 /* These sections are what may appear in a (real or virtual) DWO file. */
609
610 struct dwo_sections
611 {
612 struct dwarf2_section_info abbrev;
613 struct dwarf2_section_info line;
614 struct dwarf2_section_info loc;
615 struct dwarf2_section_info loclists;
616 struct dwarf2_section_info macinfo;
617 struct dwarf2_section_info macro;
618 struct dwarf2_section_info str;
619 struct dwarf2_section_info str_offsets;
620 /* In the case of a virtual DWO file, these two are unused. */
621 struct dwarf2_section_info info;
622 std::vector<dwarf2_section_info> types;
623 };
624
625 /* CUs/TUs in DWP/DWO files. */
626
627 struct dwo_unit
628 {
629 /* Backlink to the containing struct dwo_file. */
630 struct dwo_file *dwo_file;
631
632 /* The "id" that distinguishes this CU/TU.
633 .debug_info calls this "dwo_id", .debug_types calls this "signature".
634 Since signatures came first, we stick with it for consistency. */
635 ULONGEST signature;
636
637 /* The section this CU/TU lives in, in the DWO file. */
638 struct dwarf2_section_info *section;
639
640 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
641 sect_offset sect_off;
642 unsigned int length;
643
644 /* For types, offset in the type's DIE of the type defined by this TU. */
645 cu_offset type_offset_in_tu;
646 };
647
648 /* include/dwarf2.h defines the DWP section codes.
649 It defines a max value but it doesn't define a min value, which we
650 use for error checking, so provide one. */
651
652 enum dwp_v2_section_ids
653 {
654 DW_SECT_MIN = 1
655 };
656
657 /* Data for one DWO file.
658
659 This includes virtual DWO files (a virtual DWO file is a DWO file as it
660 appears in a DWP file). DWP files don't really have DWO files per se -
661 comdat folding of types "loses" the DWO file they came from, and from
662 a high level view DWP files appear to contain a mass of random types.
663 However, to maintain consistency with the non-DWP case we pretend DWP
664 files contain virtual DWO files, and we assign each TU with one virtual
665 DWO file (generally based on the line and abbrev section offsets -
666 a heuristic that seems to work in practice). */
667
668 struct dwo_file
669 {
670 dwo_file () = default;
671 DISABLE_COPY_AND_ASSIGN (dwo_file);
672
673 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
674 For virtual DWO files the name is constructed from the section offsets
675 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
676 from related CU+TUs. */
677 const char *dwo_name = nullptr;
678
679 /* The DW_AT_comp_dir attribute. */
680 const char *comp_dir = nullptr;
681
682 /* The bfd, when the file is open. Otherwise this is NULL.
683 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
684 gdb_bfd_ref_ptr dbfd;
685
686 /* The sections that make up this DWO file.
687 Remember that for virtual DWO files in DWP V2, these are virtual
688 sections (for lack of a better name). */
689 struct dwo_sections sections {};
690
691 /* The CUs in the file.
692 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
693 an extension to handle LLVM's Link Time Optimization output (where
694 multiple source files may be compiled into a single object/dwo pair). */
695 htab_up cus;
696
697 /* Table of TUs in the file.
698 Each element is a struct dwo_unit. */
699 htab_up tus;
700 };
701
702 /* These sections are what may appear in a DWP file. */
703
704 struct dwp_sections
705 {
706 /* These are used by both DWP version 1 and 2. */
707 struct dwarf2_section_info str;
708 struct dwarf2_section_info cu_index;
709 struct dwarf2_section_info tu_index;
710
711 /* These are only used by DWP version 2 files.
712 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
713 sections are referenced by section number, and are not recorded here.
714 In DWP version 2 there is at most one copy of all these sections, each
715 section being (effectively) comprised of the concatenation of all of the
716 individual sections that exist in the version 1 format.
717 To keep the code simple we treat each of these concatenated pieces as a
718 section itself (a virtual section?). */
719 struct dwarf2_section_info abbrev;
720 struct dwarf2_section_info info;
721 struct dwarf2_section_info line;
722 struct dwarf2_section_info loc;
723 struct dwarf2_section_info macinfo;
724 struct dwarf2_section_info macro;
725 struct dwarf2_section_info str_offsets;
726 struct dwarf2_section_info types;
727 };
728
729 /* These sections are what may appear in a virtual DWO file in DWP version 1.
730 A virtual DWO file is a DWO file as it appears in a DWP file. */
731
732 struct virtual_v1_dwo_sections
733 {
734 struct dwarf2_section_info abbrev;
735 struct dwarf2_section_info line;
736 struct dwarf2_section_info loc;
737 struct dwarf2_section_info macinfo;
738 struct dwarf2_section_info macro;
739 struct dwarf2_section_info str_offsets;
740 /* Each DWP hash table entry records one CU or one TU.
741 That is recorded here, and copied to dwo_unit.section. */
742 struct dwarf2_section_info info_or_types;
743 };
744
745 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
746 In version 2, the sections of the DWO files are concatenated together
747 and stored in one section of that name. Thus each ELF section contains
748 several "virtual" sections. */
749
750 struct virtual_v2_dwo_sections
751 {
752 bfd_size_type abbrev_offset;
753 bfd_size_type abbrev_size;
754
755 bfd_size_type line_offset;
756 bfd_size_type line_size;
757
758 bfd_size_type loc_offset;
759 bfd_size_type loc_size;
760
761 bfd_size_type macinfo_offset;
762 bfd_size_type macinfo_size;
763
764 bfd_size_type macro_offset;
765 bfd_size_type macro_size;
766
767 bfd_size_type str_offsets_offset;
768 bfd_size_type str_offsets_size;
769
770 /* Each DWP hash table entry records one CU or one TU.
771 That is recorded here, and copied to dwo_unit.section. */
772 bfd_size_type info_or_types_offset;
773 bfd_size_type info_or_types_size;
774 };
775
776 /* Contents of DWP hash tables. */
777
778 struct dwp_hash_table
779 {
780 uint32_t version, nr_columns;
781 uint32_t nr_units, nr_slots;
782 const gdb_byte *hash_table, *unit_table;
783 union
784 {
785 struct
786 {
787 const gdb_byte *indices;
788 } v1;
789 struct
790 {
791 /* This is indexed by column number and gives the id of the section
792 in that column. */
793 #define MAX_NR_V2_DWO_SECTIONS \
794 (1 /* .debug_info or .debug_types */ \
795 + 1 /* .debug_abbrev */ \
796 + 1 /* .debug_line */ \
797 + 1 /* .debug_loc */ \
798 + 1 /* .debug_str_offsets */ \
799 + 1 /* .debug_macro or .debug_macinfo */)
800 int section_ids[MAX_NR_V2_DWO_SECTIONS];
801 const gdb_byte *offsets;
802 const gdb_byte *sizes;
803 } v2;
804 } section_pool;
805 };
806
807 /* Data for one DWP file. */
808
809 struct dwp_file
810 {
811 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
812 : name (name_),
813 dbfd (std::move (abfd))
814 {
815 }
816
817 /* Name of the file. */
818 const char *name;
819
820 /* File format version. */
821 int version = 0;
822
823 /* The bfd. */
824 gdb_bfd_ref_ptr dbfd;
825
826 /* Section info for this file. */
827 struct dwp_sections sections {};
828
829 /* Table of CUs in the file. */
830 const struct dwp_hash_table *cus = nullptr;
831
832 /* Table of TUs in the file. */
833 const struct dwp_hash_table *tus = nullptr;
834
835 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
836 htab_up loaded_cus;
837 htab_up loaded_tus;
838
839 /* Table to map ELF section numbers to their sections.
840 This is only needed for the DWP V1 file format. */
841 unsigned int num_sections = 0;
842 asection **elf_sections = nullptr;
843 };
844
845 /* Struct used to pass misc. parameters to read_die_and_children, et
846 al. which are used for both .debug_info and .debug_types dies.
847 All parameters here are unchanging for the life of the call. This
848 struct exists to abstract away the constant parameters of die reading. */
849
850 struct die_reader_specs
851 {
852 /* The bfd of die_section. */
853 bfd* abfd;
854
855 /* The CU of the DIE we are parsing. */
856 struct dwarf2_cu *cu;
857
858 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
859 struct dwo_file *dwo_file;
860
861 /* The section the die comes from.
862 This is either .debug_info or .debug_types, or the .dwo variants. */
863 struct dwarf2_section_info *die_section;
864
865 /* die_section->buffer. */
866 const gdb_byte *buffer;
867
868 /* The end of the buffer. */
869 const gdb_byte *buffer_end;
870
871 /* The abbreviation table to use when reading the DIEs. */
872 struct abbrev_table *abbrev_table;
873 };
874
875 /* A subclass of die_reader_specs that holds storage and has complex
876 constructor and destructor behavior. */
877
878 class cutu_reader : public die_reader_specs
879 {
880 public:
881
882 cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct abbrev_table *abbrev_table,
884 int use_existing_cu,
885 bool skip_partial);
886
887 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
888 struct dwarf2_cu *parent_cu = nullptr,
889 struct dwo_file *dwo_file = nullptr);
890
891 DISABLE_COPY_AND_ASSIGN (cutu_reader);
892
893 const gdb_byte *info_ptr = nullptr;
894 struct die_info *comp_unit_die = nullptr;
895 bool dummy_p = false;
896
897 /* Release the new CU, putting it on the chain. This cannot be done
898 for dummy CUs. */
899 void keep ();
900
901 private:
902 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
903 int use_existing_cu);
904
905 struct dwarf2_per_cu_data *m_this_cu;
906 std::unique_ptr<dwarf2_cu> m_new_cu;
907
908 /* The ordinary abbreviation table. */
909 abbrev_table_up m_abbrev_table_holder;
910
911 /* The DWO abbreviation table. */
912 abbrev_table_up m_dwo_abbrev_table;
913 };
914
915 /* When we construct a partial symbol table entry we only
916 need this much information. */
917 struct partial_die_info : public allocate_on_obstack
918 {
919 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
920
921 /* Disable assign but still keep copy ctor, which is needed
922 load_partial_dies. */
923 partial_die_info& operator=(const partial_die_info& rhs) = delete;
924
925 /* Adjust the partial die before generating a symbol for it. This
926 function may set the is_external flag or change the DIE's
927 name. */
928 void fixup (struct dwarf2_cu *cu);
929
930 /* Read a minimal amount of information into the minimal die
931 structure. */
932 const gdb_byte *read (const struct die_reader_specs *reader,
933 const struct abbrev_info &abbrev,
934 const gdb_byte *info_ptr);
935
936 /* Offset of this DIE. */
937 const sect_offset sect_off;
938
939 /* DWARF-2 tag for this DIE. */
940 const ENUM_BITFIELD(dwarf_tag) tag : 16;
941
942 /* Assorted flags describing the data found in this DIE. */
943 const unsigned int has_children : 1;
944
945 unsigned int is_external : 1;
946 unsigned int is_declaration : 1;
947 unsigned int has_type : 1;
948 unsigned int has_specification : 1;
949 unsigned int has_pc_info : 1;
950 unsigned int may_be_inlined : 1;
951
952 /* This DIE has been marked DW_AT_main_subprogram. */
953 unsigned int main_subprogram : 1;
954
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
962 /* Flag set if the DIE has a DW_AT_const_value attribute. */
963 unsigned int has_const_value : 1;
964
965 /* Flag set if any of the DIE's children are template arguments. */
966 unsigned int has_template_arguments : 1;
967
968 /* Flag set if fixup has been called on this die. */
969 unsigned int fixup_called : 1;
970
971 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
972 unsigned int is_dwz : 1;
973
974 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
975 unsigned int spec_is_dwz : 1;
976
977 /* The name of this DIE. Normally the value of DW_AT_name, but
978 sometimes a default name for unnamed DIEs. */
979 const char *name = nullptr;
980
981 /* The linkage name, if present. */
982 const char *linkage_name = nullptr;
983
984 /* The scope to prepend to our children. This is generally
985 allocated on the comp_unit_obstack, so will disappear
986 when this compilation unit leaves the cache. */
987 const char *scope = nullptr;
988
989 /* Some data associated with the partial DIE. The tag determines
990 which field is live. */
991 union
992 {
993 /* The location description associated with this DIE, if any. */
994 struct dwarf_block *locdesc;
995 /* The offset of an import, for DW_TAG_imported_unit. */
996 sect_offset sect_off;
997 } d {};
998
999 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1000 CORE_ADDR lowpc = 0;
1001 CORE_ADDR highpc = 0;
1002
1003 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1004 DW_AT_sibling, if any. */
1005 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1006 could return DW_AT_sibling values to its caller load_partial_dies. */
1007 const gdb_byte *sibling = nullptr;
1008
1009 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1010 DW_AT_specification (or DW_AT_abstract_origin or
1011 DW_AT_extension). */
1012 sect_offset spec_offset {};
1013
1014 /* Pointers to this DIE's parent, first child, and next sibling,
1015 if any. */
1016 struct partial_die_info *die_parent = nullptr;
1017 struct partial_die_info *die_child = nullptr;
1018 struct partial_die_info *die_sibling = nullptr;
1019
1020 friend struct partial_die_info *
1021 dwarf2_cu::find_partial_die (sect_offset sect_off);
1022
1023 private:
1024 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1025 partial_die_info (sect_offset sect_off)
1026 : partial_die_info (sect_off, DW_TAG_padding, 0)
1027 {
1028 }
1029
1030 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1031 int has_children_)
1032 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1033 {
1034 is_external = 0;
1035 is_declaration = 0;
1036 has_type = 0;
1037 has_specification = 0;
1038 has_pc_info = 0;
1039 may_be_inlined = 0;
1040 main_subprogram = 0;
1041 scope_set = 0;
1042 has_byte_size = 0;
1043 has_const_value = 0;
1044 has_template_arguments = 0;
1045 fixup_called = 0;
1046 is_dwz = 0;
1047 spec_is_dwz = 0;
1048 }
1049 };
1050
1051 /* This data structure holds a complete die structure. */
1052 struct die_info
1053 {
1054 /* DWARF-2 tag for this DIE. */
1055 ENUM_BITFIELD(dwarf_tag) tag : 16;
1056
1057 /* Number of attributes */
1058 unsigned char num_attrs;
1059
1060 /* True if we're presently building the full type name for the
1061 type derived from this DIE. */
1062 unsigned char building_fullname : 1;
1063
1064 /* True if this die is in process. PR 16581. */
1065 unsigned char in_process : 1;
1066
1067 /* True if this DIE has children. */
1068 unsigned char has_children : 1;
1069
1070 /* Abbrev number */
1071 unsigned int abbrev;
1072
1073 /* Offset in .debug_info or .debug_types section. */
1074 sect_offset sect_off;
1075
1076 /* The dies in a compilation unit form an n-ary tree. PARENT
1077 points to this die's parent; CHILD points to the first child of
1078 this node; and all the children of a given node are chained
1079 together via their SIBLING fields. */
1080 struct die_info *child; /* Its first child, if any. */
1081 struct die_info *sibling; /* Its next sibling, if any. */
1082 struct die_info *parent; /* Its parent, if any. */
1083
1084 /* An array of attributes, with NUM_ATTRS elements. There may be
1085 zero, but it's not common and zero-sized arrays are not
1086 sufficiently portable C. */
1087 struct attribute attrs[1];
1088 };
1089
1090 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1091 but this would require a corresponding change in unpack_field_as_long
1092 and friends. */
1093 static int bits_per_byte = 8;
1094
1095 /* When reading a variant or variant part, we track a bit more
1096 information about the field, and store it in an object of this
1097 type. */
1098
1099 struct variant_field
1100 {
1101 /* If we see a DW_TAG_variant, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value;
1104 /* If we see a DW_TAG_variant, then this will be set if this is the
1105 default branch. */
1106 bool default_branch;
1107 /* While reading a DW_TAG_variant_part, this will be set if this
1108 field is the discriminant. */
1109 bool is_discriminant;
1110 };
1111
1112 struct nextfield
1113 {
1114 int accessibility = 0;
1115 int virtuality = 0;
1116 /* Extra information to describe a variant or variant part. */
1117 struct variant_field variant {};
1118 struct field field {};
1119 };
1120
1121 struct fnfieldlist
1122 {
1123 const char *name = nullptr;
1124 std::vector<struct fn_field> fnfields;
1125 };
1126
1127 /* The routines that read and process dies for a C struct or C++ class
1128 pass lists of data member fields and lists of member function fields
1129 in an instance of a field_info structure, as defined below. */
1130 struct field_info
1131 {
1132 /* List of data member and baseclasses fields. */
1133 std::vector<struct nextfield> fields;
1134 std::vector<struct nextfield> baseclasses;
1135
1136 /* Number of fields (including baseclasses). */
1137 int nfields = 0;
1138
1139 /* Set if the accessibility of one of the fields is not public. */
1140 int non_public_fields = 0;
1141
1142 /* Member function fieldlist array, contains name of possibly overloaded
1143 member function, number of overloaded member functions and a pointer
1144 to the head of the member function field chain. */
1145 std::vector<struct fnfieldlist> fnfieldlists;
1146
1147 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1148 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1149 std::vector<struct decl_field> typedef_field_list;
1150
1151 /* Nested types defined by this class and the number of elements in this
1152 list. */
1153 std::vector<struct decl_field> nested_types_list;
1154 };
1155
1156 /* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
1161 static int dwarf_max_cache_age = 5;
1162 static void
1163 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
1165 {
1166 fprintf_filtered (file, _("The upper bound on the age of cached "
1167 "DWARF compilation units is %s.\n"),
1168 value);
1169 }
1170 \f
1171 /* local function prototypes */
1172
1173 static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
1176 static dwarf2_psymtab *create_partial_symtab
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
1179 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
1181 struct die_info *type_unit_die);
1182
1183 static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1185
1186 static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
1188 int, struct dwarf2_cu *);
1189
1190 static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
1192
1193 static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1195 int set_addrmap, struct dwarf2_cu *cu);
1196
1197 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1198 CORE_ADDR *highpc, int set_addrmap,
1199 struct dwarf2_cu *cu);
1200
1201 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
1203
1204 static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1206 int need_pc, struct dwarf2_cu *cu);
1207
1208 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1209
1210 static struct partial_die_info *load_partial_dies
1211 (const struct die_reader_specs *, const gdb_byte *, int);
1212
1213 /* A pair of partial_die_info and compilation unit. */
1214 struct cu_partial_die_info
1215 {
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
1224 { /* Nothing. */ }
1225
1226 private:
1227 cu_partial_die_info () = delete;
1228 };
1229
1230 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
1232
1233 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
1235 const gdb_byte *, bool *need_reprocess);
1236
1237 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1241
1242 static LONGEST read_checked_initial_length_and_offset
1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1244 unsigned int *, unsigned int *);
1245
1246 static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
1249
1250 static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
1253
1254 static const char *read_indirect_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
1257
1258 static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1260 LONGEST str_offset);
1261
1262 static const char *read_indirect_string_from_dwz
1263 (struct objfile *objfile, struct dwz_file *, LONGEST);
1264
1265 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1266 const gdb_byte *,
1267 unsigned int *);
1268
1269 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1270 ULONGEST str_index);
1271
1272 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1273 ULONGEST str_index);
1274
1275 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1276
1277 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1278 struct dwarf2_cu *);
1279
1280 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1281 unsigned int);
1282
1283 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1284 struct dwarf2_cu *cu);
1285
1286 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1287
1288 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1289 struct dwarf2_cu *cu);
1290
1291 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1292
1293 static struct die_info *die_specification (struct die_info *die,
1294 struct dwarf2_cu **);
1295
1296 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1297 struct dwarf2_cu *cu);
1298
1299 static void dwarf_decode_lines (struct line_header *, const char *,
1300 struct dwarf2_cu *, dwarf2_psymtab *,
1301 CORE_ADDR, int decode_mapping);
1302
1303 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1304 const char *);
1305
1306 static struct symbol *new_symbol (struct die_info *, struct type *,
1307 struct dwarf2_cu *, struct symbol * = NULL);
1308
1309 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1310 struct dwarf2_cu *);
1311
1312 static void dwarf2_const_value_attr (const struct attribute *attr,
1313 struct type *type,
1314 const char *name,
1315 struct obstack *obstack,
1316 struct dwarf2_cu *cu, LONGEST *value,
1317 const gdb_byte **bytes,
1318 struct dwarf2_locexpr_baton **baton);
1319
1320 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1321
1322 static int need_gnat_info (struct dwarf2_cu *);
1323
1324 static struct type *die_descriptive_type (struct die_info *,
1325 struct dwarf2_cu *);
1326
1327 static void set_descriptive_type (struct type *, struct die_info *,
1328 struct dwarf2_cu *);
1329
1330 static struct type *die_containing_type (struct die_info *,
1331 struct dwarf2_cu *);
1332
1333 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1334 struct dwarf2_cu *);
1335
1336 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1337
1338 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1339
1340 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1341
1342 static char *typename_concat (struct obstack *obs, const char *prefix,
1343 const char *suffix, int physname,
1344 struct dwarf2_cu *cu);
1345
1346 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1347
1348 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1349
1350 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1351
1352 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1355
1356 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1357
1358 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1359 struct dwarf2_cu *, dwarf2_psymtab *);
1360
1361 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1362 values. Keep the items ordered with increasing constraints compliance. */
1363 enum pc_bounds_kind
1364 {
1365 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1366 PC_BOUNDS_NOT_PRESENT,
1367
1368 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1369 were present but they do not form a valid range of PC addresses. */
1370 PC_BOUNDS_INVALID,
1371
1372 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1373 PC_BOUNDS_RANGES,
1374
1375 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1376 PC_BOUNDS_HIGH_LOW,
1377 };
1378
1379 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *,
1382 dwarf2_psymtab *);
1383
1384 static void get_scope_pc_bounds (struct die_info *,
1385 CORE_ADDR *, CORE_ADDR *,
1386 struct dwarf2_cu *);
1387
1388 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1389 CORE_ADDR, struct dwarf2_cu *);
1390
1391 static void dwarf2_add_field (struct field_info *, struct die_info *,
1392 struct dwarf2_cu *);
1393
1394 static void dwarf2_attach_fields_to_type (struct field_info *,
1395 struct type *, struct dwarf2_cu *);
1396
1397 static void dwarf2_add_member_fn (struct field_info *,
1398 struct die_info *, struct type *,
1399 struct dwarf2_cu *);
1400
1401 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1402 struct type *,
1403 struct dwarf2_cu *);
1404
1405 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1406
1407 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1408
1409 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1410
1411 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1412
1413 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1414
1415 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1416
1417 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1418
1419 static struct type *read_module_type (struct die_info *die,
1420 struct dwarf2_cu *cu);
1421
1422 static const char *namespace_name (struct die_info *die,
1423 int *is_anonymous, struct dwarf2_cu *);
1424
1425 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1426
1427 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1428
1429 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1430 struct dwarf2_cu *);
1431
1432 static struct die_info *read_die_and_siblings_1
1433 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1434 struct die_info *);
1435
1436 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1437 const gdb_byte *info_ptr,
1438 const gdb_byte **new_info_ptr,
1439 struct die_info *parent);
1440
1441 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1442 struct die_info **, const gdb_byte *,
1443 int);
1444
1445 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1446 struct die_info **, const gdb_byte *);
1447
1448 static void process_die (struct die_info *, struct dwarf2_cu *);
1449
1450 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1451 struct obstack *);
1452
1453 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1454
1455 static const char *dwarf2_full_name (const char *name,
1456 struct die_info *die,
1457 struct dwarf2_cu *cu);
1458
1459 static const char *dwarf2_physname (const char *name, struct die_info *die,
1460 struct dwarf2_cu *cu);
1461
1462 static struct die_info *dwarf2_extension (struct die_info *die,
1463 struct dwarf2_cu **);
1464
1465 static const char *dwarf_tag_name (unsigned int);
1466
1467 static const char *dwarf_attr_name (unsigned int);
1468
1469 static const char *dwarf_form_name (unsigned int);
1470
1471 static const char *dwarf_bool_name (unsigned int);
1472
1473 static const char *dwarf_type_encoding_name (unsigned int);
1474
1475 static struct die_info *sibling_die (struct die_info *);
1476
1477 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479 static void dump_die_for_error (struct die_info *);
1480
1481 static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
1483
1484 /*static*/ void dump_die (struct die_info *, int max_level);
1485
1486 static void store_in_ref_table (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1490
1491 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1492
1493 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_ref (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct die_info *follow_die_sig (struct die_info *,
1502 const struct attribute *,
1503 struct dwarf2_cu **);
1504
1505 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1506 struct dwarf2_cu *);
1507
1508 static struct type *get_DW_AT_signature_type (struct die_info *,
1509 const struct attribute *,
1510 struct dwarf2_cu *);
1511
1512 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1513
1514 static void read_signatured_type (struct signatured_type *);
1515
1516 static int attr_to_dynamic_prop (const struct attribute *attr,
1517 struct die_info *die, struct dwarf2_cu *cu,
1518 struct dynamic_prop *prop, struct type *type);
1519
1520 /* memory allocation interface */
1521
1522 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1523
1524 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1525
1526 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1527
1528 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1529 struct dwarf2_loclist_baton *baton,
1530 const struct attribute *attr);
1531
1532 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1533 struct symbol *sym,
1534 struct dwarf2_cu *cu,
1535 int is_block);
1536
1537 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1538 const gdb_byte *info_ptr,
1539 struct abbrev_info *abbrev);
1540
1541 static hashval_t partial_die_hash (const void *item);
1542
1543 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1544
1545 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1546 (sect_offset sect_off, unsigned int offset_in_dwz,
1547 struct dwarf2_per_objfile *dwarf2_per_objfile);
1548
1549 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1550 struct die_info *comp_unit_die,
1551 enum language pretend_language);
1552
1553 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1556
1557 static struct type *set_die_type (struct die_info *, struct type *,
1558 struct dwarf2_cu *);
1559
1560 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1561
1562 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1563
1564 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1565 enum language);
1566
1567 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1568 enum language);
1569
1570 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1571 enum language);
1572
1573 static void dwarf2_add_dependence (struct dwarf2_cu *,
1574 struct dwarf2_per_cu_data *);
1575
1576 static void dwarf2_mark (struct dwarf2_cu *);
1577
1578 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1579
1580 static struct type *get_die_type_at_offset (sect_offset,
1581 struct dwarf2_per_cu_data *);
1582
1583 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1584
1585 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1586 enum language pretend_language);
1587
1588 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1589
1590 /* Class, the destructor of which frees all allocated queue entries. This
1591 will only have work to do if an error was thrown while processing the
1592 dwarf. If no error was thrown then the queue entries should have all
1593 been processed, and freed, as we went along. */
1594
1595 class dwarf2_queue_guard
1596 {
1597 public:
1598 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1599 : m_per_objfile (per_objfile)
1600 {
1601 }
1602
1603 /* Free any entries remaining on the queue. There should only be
1604 entries left if we hit an error while processing the dwarf. */
1605 ~dwarf2_queue_guard ()
1606 {
1607 /* Ensure that no memory is allocated by the queue. */
1608 std::queue<dwarf2_queue_item> empty;
1609 std::swap (m_per_objfile->queue, empty);
1610 }
1611
1612 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1613
1614 private:
1615 dwarf2_per_objfile *m_per_objfile;
1616 };
1617
1618 dwarf2_queue_item::~dwarf2_queue_item ()
1619 {
1620 /* Anything still marked queued is likely to be in an
1621 inconsistent state, so discard it. */
1622 if (per_cu->queued)
1623 {
1624 if (per_cu->cu != NULL)
1625 free_one_cached_comp_unit (per_cu);
1626 per_cu->queued = 0;
1627 }
1628 }
1629
1630 /* The return type of find_file_and_directory. Note, the enclosed
1631 string pointers are only valid while this object is valid. */
1632
1633 struct file_and_directory
1634 {
1635 /* The filename. This is never NULL. */
1636 const char *name;
1637
1638 /* The compilation directory. NULL if not known. If we needed to
1639 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1640 points directly to the DW_AT_comp_dir string attribute owned by
1641 the obstack that owns the DIE. */
1642 const char *comp_dir;
1643
1644 /* If we needed to build a new string for comp_dir, this is what
1645 owns the storage. */
1646 std::string comp_dir_storage;
1647 };
1648
1649 static file_and_directory find_file_and_directory (struct die_info *die,
1650 struct dwarf2_cu *cu);
1651
1652 static htab_up allocate_signatured_type_table (struct objfile *objfile);
1653
1654 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
1655
1656 static struct dwo_unit *lookup_dwo_unit_in_dwp
1657 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1658 struct dwp_file *dwp_file, const char *comp_dir,
1659 ULONGEST signature, int is_debug_types);
1660
1661 static struct dwp_file *get_dwp_file
1662 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1663
1664 static struct dwo_unit *lookup_dwo_comp_unit
1665 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1666
1667 static struct dwo_unit *lookup_dwo_type_unit
1668 (struct signatured_type *, const char *, const char *);
1669
1670 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1671
1672 /* A unique pointer to a dwo_file. */
1673
1674 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1675
1676 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1677
1678 static void check_producer (struct dwarf2_cu *cu);
1679
1680 static void free_line_header_voidp (void *arg);
1681 \f
1682 /* Various complaints about symbol reading that don't abort the process. */
1683
1684 static void
1685 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1686 {
1687 complaint (_("statement list doesn't fit in .debug_line section"));
1688 }
1689
1690 static void
1691 dwarf2_debug_line_missing_file_complaint (void)
1692 {
1693 complaint (_(".debug_line section has line data without a file"));
1694 }
1695
1696 static void
1697 dwarf2_debug_line_missing_end_sequence_complaint (void)
1698 {
1699 complaint (_(".debug_line section has line "
1700 "program sequence without an end"));
1701 }
1702
1703 static void
1704 dwarf2_complex_location_expr_complaint (void)
1705 {
1706 complaint (_("location expression too complex"));
1707 }
1708
1709 static void
1710 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1711 int arg3)
1712 {
1713 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1714 arg1, arg2, arg3);
1715 }
1716
1717 static void
1718 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1719 {
1720 complaint (_("debug info runs off end of %s section"
1721 " [in module %s]"),
1722 section->get_name (),
1723 section->get_file_name ());
1724 }
1725
1726 static void
1727 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1728 {
1729 complaint (_("macro debug info contains a "
1730 "malformed macro definition:\n`%s'"),
1731 arg1);
1732 }
1733
1734 static void
1735 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1736 {
1737 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1738 arg1, arg2);
1739 }
1740
1741 /* Hash function for line_header_hash. */
1742
1743 static hashval_t
1744 line_header_hash (const struct line_header *ofs)
1745 {
1746 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1747 }
1748
1749 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1750
1751 static hashval_t
1752 line_header_hash_voidp (const void *item)
1753 {
1754 const struct line_header *ofs = (const struct line_header *) item;
1755
1756 return line_header_hash (ofs);
1757 }
1758
1759 /* Equality function for line_header_hash. */
1760
1761 static int
1762 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1763 {
1764 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1765 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1766
1767 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1768 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1769 }
1770
1771 \f
1772
1773 /* See declaration. */
1774
1775 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1776 const dwarf2_debug_sections *names,
1777 bool can_copy_)
1778 : objfile (objfile_),
1779 can_copy (can_copy_)
1780 {
1781 if (names == NULL)
1782 names = &dwarf2_elf_names;
1783
1784 bfd *obfd = objfile->obfd;
1785
1786 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1787 locate_sections (obfd, sec, *names);
1788 }
1789
1790 dwarf2_per_objfile::~dwarf2_per_objfile ()
1791 {
1792 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1793 free_cached_comp_units ();
1794
1795 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1796 per_cu->imported_symtabs_free ();
1797
1798 for (signatured_type *sig_type : all_type_units)
1799 sig_type->per_cu.imported_symtabs_free ();
1800
1801 /* Everything else should be on the objfile obstack. */
1802 }
1803
1804 /* See declaration. */
1805
1806 void
1807 dwarf2_per_objfile::free_cached_comp_units ()
1808 {
1809 dwarf2_per_cu_data *per_cu = read_in_chain;
1810 dwarf2_per_cu_data **last_chain = &read_in_chain;
1811 while (per_cu != NULL)
1812 {
1813 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1814
1815 delete per_cu->cu;
1816 *last_chain = next_cu;
1817 per_cu = next_cu;
1818 }
1819 }
1820
1821 /* A helper class that calls free_cached_comp_units on
1822 destruction. */
1823
1824 class free_cached_comp_units
1825 {
1826 public:
1827
1828 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1829 : m_per_objfile (per_objfile)
1830 {
1831 }
1832
1833 ~free_cached_comp_units ()
1834 {
1835 m_per_objfile->free_cached_comp_units ();
1836 }
1837
1838 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1839
1840 private:
1841
1842 dwarf2_per_objfile *m_per_objfile;
1843 };
1844
1845 /* Try to locate the sections we need for DWARF 2 debugging
1846 information and return true if we have enough to do something.
1847 NAMES points to the dwarf2 section names, or is NULL if the standard
1848 ELF names are used. CAN_COPY is true for formats where symbol
1849 interposition is possible and so symbol values must follow copy
1850 relocation rules. */
1851
1852 int
1853 dwarf2_has_info (struct objfile *objfile,
1854 const struct dwarf2_debug_sections *names,
1855 bool can_copy)
1856 {
1857 if (objfile->flags & OBJF_READNEVER)
1858 return 0;
1859
1860 struct dwarf2_per_objfile *dwarf2_per_objfile
1861 = get_dwarf2_per_objfile (objfile);
1862
1863 if (dwarf2_per_objfile == NULL)
1864 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1865 names,
1866 can_copy);
1867
1868 return (!dwarf2_per_objfile->info.is_virtual
1869 && dwarf2_per_objfile->info.s.section != NULL
1870 && !dwarf2_per_objfile->abbrev.is_virtual
1871 && dwarf2_per_objfile->abbrev.s.section != NULL);
1872 }
1873
1874 /* When loading sections, we look either for uncompressed section or for
1875 compressed section names. */
1876
1877 static int
1878 section_is_p (const char *section_name,
1879 const struct dwarf2_section_names *names)
1880 {
1881 if (names->normal != NULL
1882 && strcmp (section_name, names->normal) == 0)
1883 return 1;
1884 if (names->compressed != NULL
1885 && strcmp (section_name, names->compressed) == 0)
1886 return 1;
1887 return 0;
1888 }
1889
1890 /* See declaration. */
1891
1892 void
1893 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1894 const dwarf2_debug_sections &names)
1895 {
1896 flagword aflag = bfd_section_flags (sectp);
1897
1898 if ((aflag & SEC_HAS_CONTENTS) == 0)
1899 {
1900 }
1901 else if (elf_section_data (sectp)->this_hdr.sh_size
1902 > bfd_get_file_size (abfd))
1903 {
1904 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1905 warning (_("Discarding section %s which has a section size (%s"
1906 ") larger than the file size [in module %s]"),
1907 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1908 bfd_get_filename (abfd));
1909 }
1910 else if (section_is_p (sectp->name, &names.info))
1911 {
1912 this->info.s.section = sectp;
1913 this->info.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.abbrev))
1916 {
1917 this->abbrev.s.section = sectp;
1918 this->abbrev.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.line))
1921 {
1922 this->line.s.section = sectp;
1923 this->line.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.loc))
1926 {
1927 this->loc.s.section = sectp;
1928 this->loc.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.loclists))
1931 {
1932 this->loclists.s.section = sectp;
1933 this->loclists.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.macinfo))
1936 {
1937 this->macinfo.s.section = sectp;
1938 this->macinfo.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.macro))
1941 {
1942 this->macro.s.section = sectp;
1943 this->macro.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.str))
1946 {
1947 this->str.s.section = sectp;
1948 this->str.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.str_offsets))
1951 {
1952 this->str_offsets.s.section = sectp;
1953 this->str_offsets.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.line_str))
1956 {
1957 this->line_str.s.section = sectp;
1958 this->line_str.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.addr))
1961 {
1962 this->addr.s.section = sectp;
1963 this->addr.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.frame))
1966 {
1967 this->frame.s.section = sectp;
1968 this->frame.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.eh_frame))
1971 {
1972 this->eh_frame.s.section = sectp;
1973 this->eh_frame.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.ranges))
1976 {
1977 this->ranges.s.section = sectp;
1978 this->ranges.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.rnglists))
1981 {
1982 this->rnglists.s.section = sectp;
1983 this->rnglists.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.types))
1986 {
1987 struct dwarf2_section_info type_section;
1988
1989 memset (&type_section, 0, sizeof (type_section));
1990 type_section.s.section = sectp;
1991 type_section.size = bfd_section_size (sectp);
1992
1993 this->types.push_back (type_section);
1994 }
1995 else if (section_is_p (sectp->name, &names.gdb_index))
1996 {
1997 this->gdb_index.s.section = sectp;
1998 this->gdb_index.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_names))
2001 {
2002 this->debug_names.s.section = sectp;
2003 this->debug_names.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.debug_aranges))
2006 {
2007 this->debug_aranges.s.section = sectp;
2008 this->debug_aranges.size = bfd_section_size (sectp);
2009 }
2010
2011 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2012 && bfd_section_vma (sectp) == 0)
2013 this->has_section_at_zero = true;
2014 }
2015
2016 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2017 SECTION_NAME. */
2018
2019 void
2020 dwarf2_get_section_info (struct objfile *objfile,
2021 enum dwarf2_section_enum sect,
2022 asection **sectp, const gdb_byte **bufp,
2023 bfd_size_type *sizep)
2024 {
2025 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2026 struct dwarf2_section_info *info;
2027
2028 /* We may see an objfile without any DWARF, in which case we just
2029 return nothing. */
2030 if (data == NULL)
2031 {
2032 *sectp = NULL;
2033 *bufp = NULL;
2034 *sizep = 0;
2035 return;
2036 }
2037 switch (sect)
2038 {
2039 case DWARF2_DEBUG_FRAME:
2040 info = &data->frame;
2041 break;
2042 case DWARF2_EH_FRAME:
2043 info = &data->eh_frame;
2044 break;
2045 default:
2046 gdb_assert_not_reached ("unexpected section");
2047 }
2048
2049 info->read (objfile);
2050
2051 *sectp = info->get_bfd_section ();
2052 *bufp = info->buffer;
2053 *sizep = info->size;
2054 }
2055
2056 /* A helper function to find the sections for a .dwz file. */
2057
2058 static void
2059 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2060 {
2061 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2062
2063 /* Note that we only support the standard ELF names, because .dwz
2064 is ELF-only (at the time of writing). */
2065 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2066 {
2067 dwz_file->abbrev.s.section = sectp;
2068 dwz_file->abbrev.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2071 {
2072 dwz_file->info.s.section = sectp;
2073 dwz_file->info.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2076 {
2077 dwz_file->str.s.section = sectp;
2078 dwz_file->str.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2081 {
2082 dwz_file->line.s.section = sectp;
2083 dwz_file->line.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2086 {
2087 dwz_file->macro.s.section = sectp;
2088 dwz_file->macro.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2091 {
2092 dwz_file->gdb_index.s.section = sectp;
2093 dwz_file->gdb_index.size = bfd_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2096 {
2097 dwz_file->debug_names.s.section = sectp;
2098 dwz_file->debug_names.size = bfd_section_size (sectp);
2099 }
2100 }
2101
2102 /* See dwarf2read.h. */
2103
2104 struct dwz_file *
2105 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2106 {
2107 const char *filename;
2108 bfd_size_type buildid_len_arg;
2109 size_t buildid_len;
2110 bfd_byte *buildid;
2111
2112 if (dwarf2_per_objfile->dwz_file != NULL)
2113 return dwarf2_per_objfile->dwz_file.get ();
2114
2115 bfd_set_error (bfd_error_no_error);
2116 gdb::unique_xmalloc_ptr<char> data
2117 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2118 &buildid_len_arg, &buildid));
2119 if (data == NULL)
2120 {
2121 if (bfd_get_error () == bfd_error_no_error)
2122 return NULL;
2123 error (_("could not read '.gnu_debugaltlink' section: %s"),
2124 bfd_errmsg (bfd_get_error ()));
2125 }
2126
2127 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2128
2129 buildid_len = (size_t) buildid_len_arg;
2130
2131 filename = data.get ();
2132
2133 std::string abs_storage;
2134 if (!IS_ABSOLUTE_PATH (filename))
2135 {
2136 gdb::unique_xmalloc_ptr<char> abs
2137 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2138
2139 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2140 filename = abs_storage.c_str ();
2141 }
2142
2143 /* First try the file name given in the section. If that doesn't
2144 work, try to use the build-id instead. */
2145 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2146 if (dwz_bfd != NULL)
2147 {
2148 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2149 dwz_bfd.reset (nullptr);
2150 }
2151
2152 if (dwz_bfd == NULL)
2153 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2154
2155 if (dwz_bfd == NULL)
2156 error (_("could not find '.gnu_debugaltlink' file for %s"),
2157 objfile_name (dwarf2_per_objfile->objfile));
2158
2159 std::unique_ptr<struct dwz_file> result
2160 (new struct dwz_file (std::move (dwz_bfd)));
2161
2162 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2163 result.get ());
2164
2165 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2166 result->dwz_bfd.get ());
2167 dwarf2_per_objfile->dwz_file = std::move (result);
2168 return dwarf2_per_objfile->dwz_file.get ();
2169 }
2170 \f
2171 /* DWARF quick_symbols_functions support. */
2172
2173 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2174 unique line tables, so we maintain a separate table of all .debug_line
2175 derived entries to support the sharing.
2176 All the quick functions need is the list of file names. We discard the
2177 line_header when we're done and don't need to record it here. */
2178 struct quick_file_names
2179 {
2180 /* The data used to construct the hash key. */
2181 struct stmt_list_hash hash;
2182
2183 /* The number of entries in file_names, real_names. */
2184 unsigned int num_file_names;
2185
2186 /* The file names from the line table, after being run through
2187 file_full_name. */
2188 const char **file_names;
2189
2190 /* The file names from the line table after being run through
2191 gdb_realpath. These are computed lazily. */
2192 const char **real_names;
2193 };
2194
2195 /* When using the index (and thus not using psymtabs), each CU has an
2196 object of this type. This is used to hold information needed by
2197 the various "quick" methods. */
2198 struct dwarf2_per_cu_quick_data
2199 {
2200 /* The file table. This can be NULL if there was no file table
2201 or it's currently not read in.
2202 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2203 struct quick_file_names *file_names;
2204
2205 /* The corresponding symbol table. This is NULL if symbols for this
2206 CU have not yet been read. */
2207 struct compunit_symtab *compunit_symtab;
2208
2209 /* A temporary mark bit used when iterating over all CUs in
2210 expand_symtabs_matching. */
2211 unsigned int mark : 1;
2212
2213 /* True if we've tried to read the file table and found there isn't one.
2214 There will be no point in trying to read it again next time. */
2215 unsigned int no_file_data : 1;
2216 };
2217
2218 /* Utility hash function for a stmt_list_hash. */
2219
2220 static hashval_t
2221 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2222 {
2223 hashval_t v = 0;
2224
2225 if (stmt_list_hash->dwo_unit != NULL)
2226 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2227 v += to_underlying (stmt_list_hash->line_sect_off);
2228 return v;
2229 }
2230
2231 /* Utility equality function for a stmt_list_hash. */
2232
2233 static int
2234 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2235 const struct stmt_list_hash *rhs)
2236 {
2237 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2238 return 0;
2239 if (lhs->dwo_unit != NULL
2240 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2241 return 0;
2242
2243 return lhs->line_sect_off == rhs->line_sect_off;
2244 }
2245
2246 /* Hash function for a quick_file_names. */
2247
2248 static hashval_t
2249 hash_file_name_entry (const void *e)
2250 {
2251 const struct quick_file_names *file_data
2252 = (const struct quick_file_names *) e;
2253
2254 return hash_stmt_list_entry (&file_data->hash);
2255 }
2256
2257 /* Equality function for a quick_file_names. */
2258
2259 static int
2260 eq_file_name_entry (const void *a, const void *b)
2261 {
2262 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2263 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2264
2265 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2266 }
2267
2268 /* Delete function for a quick_file_names. */
2269
2270 static void
2271 delete_file_name_entry (void *e)
2272 {
2273 struct quick_file_names *file_data = (struct quick_file_names *) e;
2274 int i;
2275
2276 for (i = 0; i < file_data->num_file_names; ++i)
2277 {
2278 xfree ((void*) file_data->file_names[i]);
2279 if (file_data->real_names)
2280 xfree ((void*) file_data->real_names[i]);
2281 }
2282
2283 /* The space for the struct itself lives on objfile_obstack,
2284 so we don't free it here. */
2285 }
2286
2287 /* Create a quick_file_names hash table. */
2288
2289 static htab_up
2290 create_quick_file_names_table (unsigned int nr_initial_entries)
2291 {
2292 return htab_up (htab_create_alloc (nr_initial_entries,
2293 hash_file_name_entry, eq_file_name_entry,
2294 delete_file_name_entry, xcalloc, xfree));
2295 }
2296
2297 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2298 have to be created afterwards. You should call age_cached_comp_units after
2299 processing PER_CU->CU. dw2_setup must have been already called. */
2300
2301 static void
2302 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2303 {
2304 if (per_cu->is_debug_types)
2305 load_full_type_unit (per_cu);
2306 else
2307 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2308
2309 if (per_cu->cu == NULL)
2310 return; /* Dummy CU. */
2311
2312 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2313 }
2314
2315 /* Read in the symbols for PER_CU. */
2316
2317 static void
2318 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2319 {
2320 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2321
2322 /* Skip type_unit_groups, reading the type units they contain
2323 is handled elsewhere. */
2324 if (IS_TYPE_UNIT_GROUP (per_cu))
2325 return;
2326
2327 /* The destructor of dwarf2_queue_guard frees any entries left on
2328 the queue. After this point we're guaranteed to leave this function
2329 with the dwarf queue empty. */
2330 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2331
2332 if (dwarf2_per_objfile->using_index
2333 ? per_cu->v.quick->compunit_symtab == NULL
2334 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2335 {
2336 queue_comp_unit (per_cu, language_minimal);
2337 load_cu (per_cu, skip_partial);
2338
2339 /* If we just loaded a CU from a DWO, and we're working with an index
2340 that may badly handle TUs, load all the TUs in that DWO as well.
2341 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2342 if (!per_cu->is_debug_types
2343 && per_cu->cu != NULL
2344 && per_cu->cu->dwo_unit != NULL
2345 && dwarf2_per_objfile->index_table != NULL
2346 && dwarf2_per_objfile->index_table->version <= 7
2347 /* DWP files aren't supported yet. */
2348 && get_dwp_file (dwarf2_per_objfile) == NULL)
2349 queue_and_load_all_dwo_tus (per_cu);
2350 }
2351
2352 process_queue (dwarf2_per_objfile);
2353
2354 /* Age the cache, releasing compilation units that have not
2355 been used recently. */
2356 age_cached_comp_units (dwarf2_per_objfile);
2357 }
2358
2359 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2360 the objfile from which this CU came. Returns the resulting symbol
2361 table. */
2362
2363 static struct compunit_symtab *
2364 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2365 {
2366 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2367
2368 gdb_assert (dwarf2_per_objfile->using_index);
2369 if (!per_cu->v.quick->compunit_symtab)
2370 {
2371 free_cached_comp_units freer (dwarf2_per_objfile);
2372 scoped_restore decrementer = increment_reading_symtab ();
2373 dw2_do_instantiate_symtab (per_cu, skip_partial);
2374 process_cu_includes (dwarf2_per_objfile);
2375 }
2376
2377 return per_cu->v.quick->compunit_symtab;
2378 }
2379
2380 /* See declaration. */
2381
2382 dwarf2_per_cu_data *
2383 dwarf2_per_objfile::get_cutu (int index)
2384 {
2385 if (index >= this->all_comp_units.size ())
2386 {
2387 index -= this->all_comp_units.size ();
2388 gdb_assert (index < this->all_type_units.size ());
2389 return &this->all_type_units[index]->per_cu;
2390 }
2391
2392 return this->all_comp_units[index];
2393 }
2394
2395 /* See declaration. */
2396
2397 dwarf2_per_cu_data *
2398 dwarf2_per_objfile::get_cu (int index)
2399 {
2400 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2401
2402 return this->all_comp_units[index];
2403 }
2404
2405 /* See declaration. */
2406
2407 signatured_type *
2408 dwarf2_per_objfile::get_tu (int index)
2409 {
2410 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2411
2412 return this->all_type_units[index];
2413 }
2414
2415 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2416 objfile_obstack, and constructed with the specified field
2417 values. */
2418
2419 static dwarf2_per_cu_data *
2420 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2421 struct dwarf2_section_info *section,
2422 int is_dwz,
2423 sect_offset sect_off, ULONGEST length)
2424 {
2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
2426 dwarf2_per_cu_data *the_cu
2427 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2428 struct dwarf2_per_cu_data);
2429 the_cu->sect_off = sect_off;
2430 the_cu->length = length;
2431 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2432 the_cu->section = section;
2433 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2434 struct dwarf2_per_cu_quick_data);
2435 the_cu->is_dwz = is_dwz;
2436 return the_cu;
2437 }
2438
2439 /* A helper for create_cus_from_index that handles a given list of
2440 CUs. */
2441
2442 static void
2443 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2444 const gdb_byte *cu_list, offset_type n_elements,
2445 struct dwarf2_section_info *section,
2446 int is_dwz)
2447 {
2448 for (offset_type i = 0; i < n_elements; i += 2)
2449 {
2450 gdb_static_assert (sizeof (ULONGEST) >= 8);
2451
2452 sect_offset sect_off
2453 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2454 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2455 cu_list += 2 * 8;
2456
2457 dwarf2_per_cu_data *per_cu
2458 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2459 sect_off, length);
2460 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2461 }
2462 }
2463
2464 /* Read the CU list from the mapped index, and use it to create all
2465 the CU objects for this objfile. */
2466
2467 static void
2468 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2469 const gdb_byte *cu_list, offset_type cu_list_elements,
2470 const gdb_byte *dwz_list, offset_type dwz_elements)
2471 {
2472 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2473 dwarf2_per_objfile->all_comp_units.reserve
2474 ((cu_list_elements + dwz_elements) / 2);
2475
2476 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2477 &dwarf2_per_objfile->info, 0);
2478
2479 if (dwz_elements == 0)
2480 return;
2481
2482 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2483 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2484 &dwz->info, 1);
2485 }
2486
2487 /* Create the signatured type hash table from the index. */
2488
2489 static void
2490 create_signatured_type_table_from_index
2491 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2492 struct dwarf2_section_info *section,
2493 const gdb_byte *bytes,
2494 offset_type elements)
2495 {
2496 struct objfile *objfile = dwarf2_per_objfile->objfile;
2497
2498 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2499 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2500
2501 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2502
2503 for (offset_type i = 0; i < elements; i += 3)
2504 {
2505 struct signatured_type *sig_type;
2506 ULONGEST signature;
2507 void **slot;
2508 cu_offset type_offset_in_tu;
2509
2510 gdb_static_assert (sizeof (ULONGEST) >= 8);
2511 sect_offset sect_off
2512 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2513 type_offset_in_tu
2514 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2515 BFD_ENDIAN_LITTLE);
2516 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2517 bytes += 3 * 8;
2518
2519 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2520 struct signatured_type);
2521 sig_type->signature = signature;
2522 sig_type->type_offset_in_tu = type_offset_in_tu;
2523 sig_type->per_cu.is_debug_types = 1;
2524 sig_type->per_cu.section = section;
2525 sig_type->per_cu.sect_off = sect_off;
2526 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2527 sig_type->per_cu.v.quick
2528 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2529 struct dwarf2_per_cu_quick_data);
2530
2531 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2532 *slot = sig_type;
2533
2534 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2535 }
2536
2537 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2538 }
2539
2540 /* Create the signatured type hash table from .debug_names. */
2541
2542 static void
2543 create_signatured_type_table_from_debug_names
2544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2545 const mapped_debug_names &map,
2546 struct dwarf2_section_info *section,
2547 struct dwarf2_section_info *abbrev_section)
2548 {
2549 struct objfile *objfile = dwarf2_per_objfile->objfile;
2550
2551 section->read (objfile);
2552 abbrev_section->read (objfile);
2553
2554 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2555 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2556
2557 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2558
2559 for (uint32_t i = 0; i < map.tu_count; ++i)
2560 {
2561 struct signatured_type *sig_type;
2562 void **slot;
2563
2564 sect_offset sect_off
2565 = (sect_offset) (extract_unsigned_integer
2566 (map.tu_table_reordered + i * map.offset_size,
2567 map.offset_size,
2568 map.dwarf5_byte_order));
2569
2570 comp_unit_head cu_header;
2571 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2572 abbrev_section,
2573 section->buffer + to_underlying (sect_off),
2574 rcuh_kind::TYPE);
2575
2576 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2577 struct signatured_type);
2578 sig_type->signature = cu_header.signature;
2579 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2580 sig_type->per_cu.is_debug_types = 1;
2581 sig_type->per_cu.section = section;
2582 sig_type->per_cu.sect_off = sect_off;
2583 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2584 sig_type->per_cu.v.quick
2585 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2586 struct dwarf2_per_cu_quick_data);
2587
2588 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2589 *slot = sig_type;
2590
2591 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2592 }
2593
2594 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2595 }
2596
2597 /* Read the address map data from the mapped index, and use it to
2598 populate the objfile's psymtabs_addrmap. */
2599
2600 static void
2601 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2602 struct mapped_index *index)
2603 {
2604 struct objfile *objfile = dwarf2_per_objfile->objfile;
2605 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2606 const gdb_byte *iter, *end;
2607 struct addrmap *mutable_map;
2608 CORE_ADDR baseaddr;
2609
2610 auto_obstack temp_obstack;
2611
2612 mutable_map = addrmap_create_mutable (&temp_obstack);
2613
2614 iter = index->address_table.data ();
2615 end = iter + index->address_table.size ();
2616
2617 baseaddr = objfile->text_section_offset ();
2618
2619 while (iter < end)
2620 {
2621 ULONGEST hi, lo, cu_index;
2622 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2623 iter += 8;
2624 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2625 iter += 8;
2626 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2627 iter += 4;
2628
2629 if (lo > hi)
2630 {
2631 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2632 hex_string (lo), hex_string (hi));
2633 continue;
2634 }
2635
2636 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2637 {
2638 complaint (_(".gdb_index address table has invalid CU number %u"),
2639 (unsigned) cu_index);
2640 continue;
2641 }
2642
2643 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2644 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2645 addrmap_set_empty (mutable_map, lo, hi - 1,
2646 dwarf2_per_objfile->get_cu (cu_index));
2647 }
2648
2649 objfile->partial_symtabs->psymtabs_addrmap
2650 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2651 }
2652
2653 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2654 populate the objfile's psymtabs_addrmap. */
2655
2656 static void
2657 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2658 struct dwarf2_section_info *section)
2659 {
2660 struct objfile *objfile = dwarf2_per_objfile->objfile;
2661 bfd *abfd = objfile->obfd;
2662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2663 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2664
2665 auto_obstack temp_obstack;
2666 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2667
2668 std::unordered_map<sect_offset,
2669 dwarf2_per_cu_data *,
2670 gdb::hash_enum<sect_offset>>
2671 debug_info_offset_to_per_cu;
2672 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2673 {
2674 const auto insertpair
2675 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2676 if (!insertpair.second)
2677 {
2678 warning (_("Section .debug_aranges in %s has duplicate "
2679 "debug_info_offset %s, ignoring .debug_aranges."),
2680 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2681 return;
2682 }
2683 }
2684
2685 section->read (objfile);
2686
2687 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2688
2689 const gdb_byte *addr = section->buffer;
2690
2691 while (addr < section->buffer + section->size)
2692 {
2693 const gdb_byte *const entry_addr = addr;
2694 unsigned int bytes_read;
2695
2696 const LONGEST entry_length = read_initial_length (abfd, addr,
2697 &bytes_read);
2698 addr += bytes_read;
2699
2700 const gdb_byte *const entry_end = addr + entry_length;
2701 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2702 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2703 if (addr + entry_length > section->buffer + section->size)
2704 {
2705 warning (_("Section .debug_aranges in %s entry at offset %s "
2706 "length %s exceeds section length %s, "
2707 "ignoring .debug_aranges."),
2708 objfile_name (objfile),
2709 plongest (entry_addr - section->buffer),
2710 plongest (bytes_read + entry_length),
2711 pulongest (section->size));
2712 return;
2713 }
2714
2715 /* The version number. */
2716 const uint16_t version = read_2_bytes (abfd, addr);
2717 addr += 2;
2718 if (version != 2)
2719 {
2720 warning (_("Section .debug_aranges in %s entry at offset %s "
2721 "has unsupported version %d, ignoring .debug_aranges."),
2722 objfile_name (objfile),
2723 plongest (entry_addr - section->buffer), version);
2724 return;
2725 }
2726
2727 const uint64_t debug_info_offset
2728 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2729 addr += offset_size;
2730 const auto per_cu_it
2731 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2732 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2733 {
2734 warning (_("Section .debug_aranges in %s entry at offset %s "
2735 "debug_info_offset %s does not exists, "
2736 "ignoring .debug_aranges."),
2737 objfile_name (objfile),
2738 plongest (entry_addr - section->buffer),
2739 pulongest (debug_info_offset));
2740 return;
2741 }
2742 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2743
2744 const uint8_t address_size = *addr++;
2745 if (address_size < 1 || address_size > 8)
2746 {
2747 warning (_("Section .debug_aranges in %s entry at offset %s "
2748 "address_size %u is invalid, ignoring .debug_aranges."),
2749 objfile_name (objfile),
2750 plongest (entry_addr - section->buffer), address_size);
2751 return;
2752 }
2753
2754 const uint8_t segment_selector_size = *addr++;
2755 if (segment_selector_size != 0)
2756 {
2757 warning (_("Section .debug_aranges in %s entry at offset %s "
2758 "segment_selector_size %u is not supported, "
2759 "ignoring .debug_aranges."),
2760 objfile_name (objfile),
2761 plongest (entry_addr - section->buffer),
2762 segment_selector_size);
2763 return;
2764 }
2765
2766 /* Must pad to an alignment boundary that is twice the address
2767 size. It is undocumented by the DWARF standard but GCC does
2768 use it. */
2769 for (size_t padding = ((-(addr - section->buffer))
2770 & (2 * address_size - 1));
2771 padding > 0; padding--)
2772 if (*addr++ != 0)
2773 {
2774 warning (_("Section .debug_aranges in %s entry at offset %s "
2775 "padding is not zero, ignoring .debug_aranges."),
2776 objfile_name (objfile),
2777 plongest (entry_addr - section->buffer));
2778 return;
2779 }
2780
2781 for (;;)
2782 {
2783 if (addr + 2 * address_size > entry_end)
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "address list is not properly terminated, "
2787 "ignoring .debug_aranges."),
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer));
2790 return;
2791 }
2792 ULONGEST start = extract_unsigned_integer (addr, address_size,
2793 dwarf5_byte_order);
2794 addr += address_size;
2795 ULONGEST length = extract_unsigned_integer (addr, address_size,
2796 dwarf5_byte_order);
2797 addr += address_size;
2798 if (start == 0 && length == 0)
2799 break;
2800 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2801 {
2802 /* Symbol was eliminated due to a COMDAT group. */
2803 continue;
2804 }
2805 ULONGEST end = start + length;
2806 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2807 - baseaddr);
2808 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2809 - baseaddr);
2810 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2811 }
2812 }
2813
2814 objfile->partial_symtabs->psymtabs_addrmap
2815 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2816 }
2817
2818 /* Find a slot in the mapped index INDEX for the object named NAME.
2819 If NAME is found, set *VEC_OUT to point to the CU vector in the
2820 constant pool and return true. If NAME cannot be found, return
2821 false. */
2822
2823 static bool
2824 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2825 offset_type **vec_out)
2826 {
2827 offset_type hash;
2828 offset_type slot, step;
2829 int (*cmp) (const char *, const char *);
2830
2831 gdb::unique_xmalloc_ptr<char> without_params;
2832 if (current_language->la_language == language_cplus
2833 || current_language->la_language == language_fortran
2834 || current_language->la_language == language_d)
2835 {
2836 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2837 not contain any. */
2838
2839 if (strchr (name, '(') != NULL)
2840 {
2841 without_params = cp_remove_params (name);
2842
2843 if (without_params != NULL)
2844 name = without_params.get ();
2845 }
2846 }
2847
2848 /* Index version 4 did not support case insensitive searches. But the
2849 indices for case insensitive languages are built in lowercase, therefore
2850 simulate our NAME being searched is also lowercased. */
2851 hash = mapped_index_string_hash ((index->version == 4
2852 && case_sensitivity == case_sensitive_off
2853 ? 5 : index->version),
2854 name);
2855
2856 slot = hash & (index->symbol_table.size () - 1);
2857 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2858 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2859
2860 for (;;)
2861 {
2862 const char *str;
2863
2864 const auto &bucket = index->symbol_table[slot];
2865 if (bucket.name == 0 && bucket.vec == 0)
2866 return false;
2867
2868 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2869 if (!cmp (name, str))
2870 {
2871 *vec_out = (offset_type *) (index->constant_pool
2872 + MAYBE_SWAP (bucket.vec));
2873 return true;
2874 }
2875
2876 slot = (slot + step) & (index->symbol_table.size () - 1);
2877 }
2878 }
2879
2880 /* A helper function that reads the .gdb_index from BUFFER and fills
2881 in MAP. FILENAME is the name of the file containing the data;
2882 it is used for error reporting. DEPRECATED_OK is true if it is
2883 ok to use deprecated sections.
2884
2885 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2886 out parameters that are filled in with information about the CU and
2887 TU lists in the section.
2888
2889 Returns true if all went well, false otherwise. */
2890
2891 static bool
2892 read_gdb_index_from_buffer (struct objfile *objfile,
2893 const char *filename,
2894 bool deprecated_ok,
2895 gdb::array_view<const gdb_byte> buffer,
2896 struct mapped_index *map,
2897 const gdb_byte **cu_list,
2898 offset_type *cu_list_elements,
2899 const gdb_byte **types_list,
2900 offset_type *types_list_elements)
2901 {
2902 const gdb_byte *addr = &buffer[0];
2903
2904 /* Version check. */
2905 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2906 /* Versions earlier than 3 emitted every copy of a psymbol. This
2907 causes the index to behave very poorly for certain requests. Version 3
2908 contained incomplete addrmap. So, it seems better to just ignore such
2909 indices. */
2910 if (version < 4)
2911 {
2912 static int warning_printed = 0;
2913 if (!warning_printed)
2914 {
2915 warning (_("Skipping obsolete .gdb_index section in %s."),
2916 filename);
2917 warning_printed = 1;
2918 }
2919 return 0;
2920 }
2921 /* Index version 4 uses a different hash function than index version
2922 5 and later.
2923
2924 Versions earlier than 6 did not emit psymbols for inlined
2925 functions. Using these files will cause GDB not to be able to
2926 set breakpoints on inlined functions by name, so we ignore these
2927 indices unless the user has done
2928 "set use-deprecated-index-sections on". */
2929 if (version < 6 && !deprecated_ok)
2930 {
2931 static int warning_printed = 0;
2932 if (!warning_printed)
2933 {
2934 warning (_("\
2935 Skipping deprecated .gdb_index section in %s.\n\
2936 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2937 to use the section anyway."),
2938 filename);
2939 warning_printed = 1;
2940 }
2941 return 0;
2942 }
2943 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2944 of the TU (for symbols coming from TUs),
2945 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2946 Plus gold-generated indices can have duplicate entries for global symbols,
2947 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2948 These are just performance bugs, and we can't distinguish gdb-generated
2949 indices from gold-generated ones, so issue no warning here. */
2950
2951 /* Indexes with higher version than the one supported by GDB may be no
2952 longer backward compatible. */
2953 if (version > 8)
2954 return 0;
2955
2956 map->version = version;
2957
2958 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2959
2960 int i = 0;
2961 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2962 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2963 / 8);
2964 ++i;
2965
2966 *types_list = addr + MAYBE_SWAP (metadata[i]);
2967 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2968 - MAYBE_SWAP (metadata[i]))
2969 / 8);
2970 ++i;
2971
2972 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2973 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2974 map->address_table
2975 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2976 ++i;
2977
2978 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2979 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2980 map->symbol_table
2981 = gdb::array_view<mapped_index::symbol_table_slot>
2982 ((mapped_index::symbol_table_slot *) symbol_table,
2983 (mapped_index::symbol_table_slot *) symbol_table_end);
2984
2985 ++i;
2986 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2987
2988 return 1;
2989 }
2990
2991 /* Callback types for dwarf2_read_gdb_index. */
2992
2993 typedef gdb::function_view
2994 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2995 get_gdb_index_contents_ftype;
2996 typedef gdb::function_view
2997 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2998 get_gdb_index_contents_dwz_ftype;
2999
3000 /* Read .gdb_index. If everything went ok, initialize the "quick"
3001 elements of all the CUs and return 1. Otherwise, return 0. */
3002
3003 static int
3004 dwarf2_read_gdb_index
3005 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 get_gdb_index_contents_ftype get_gdb_index_contents,
3007 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3008 {
3009 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3010 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3011 struct dwz_file *dwz;
3012 struct objfile *objfile = dwarf2_per_objfile->objfile;
3013
3014 gdb::array_view<const gdb_byte> main_index_contents
3015 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3016
3017 if (main_index_contents.empty ())
3018 return 0;
3019
3020 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3021 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3022 use_deprecated_index_sections,
3023 main_index_contents, map.get (), &cu_list,
3024 &cu_list_elements, &types_list,
3025 &types_list_elements))
3026 return 0;
3027
3028 /* Don't use the index if it's empty. */
3029 if (map->symbol_table.empty ())
3030 return 0;
3031
3032 /* If there is a .dwz file, read it so we can get its CU list as
3033 well. */
3034 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3035 if (dwz != NULL)
3036 {
3037 struct mapped_index dwz_map;
3038 const gdb_byte *dwz_types_ignore;
3039 offset_type dwz_types_elements_ignore;
3040
3041 gdb::array_view<const gdb_byte> dwz_index_content
3042 = get_gdb_index_contents_dwz (objfile, dwz);
3043
3044 if (dwz_index_content.empty ())
3045 return 0;
3046
3047 if (!read_gdb_index_from_buffer (objfile,
3048 bfd_get_filename (dwz->dwz_bfd.get ()),
3049 1, dwz_index_content, &dwz_map,
3050 &dwz_list, &dwz_list_elements,
3051 &dwz_types_ignore,
3052 &dwz_types_elements_ignore))
3053 {
3054 warning (_("could not read '.gdb_index' section from %s; skipping"),
3055 bfd_get_filename (dwz->dwz_bfd.get ()));
3056 return 0;
3057 }
3058 }
3059
3060 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3061 dwz_list, dwz_list_elements);
3062
3063 if (types_list_elements)
3064 {
3065 /* We can only handle a single .debug_types when we have an
3066 index. */
3067 if (dwarf2_per_objfile->types.size () != 1)
3068 return 0;
3069
3070 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3071
3072 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3073 types_list, types_list_elements);
3074 }
3075
3076 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3077
3078 dwarf2_per_objfile->index_table = std::move (map);
3079 dwarf2_per_objfile->using_index = 1;
3080 dwarf2_per_objfile->quick_file_names_table =
3081 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3082
3083 return 1;
3084 }
3085
3086 /* die_reader_func for dw2_get_file_names. */
3087
3088 static void
3089 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3090 const gdb_byte *info_ptr,
3091 struct die_info *comp_unit_die)
3092 {
3093 struct dwarf2_cu *cu = reader->cu;
3094 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3095 struct dwarf2_per_objfile *dwarf2_per_objfile
3096 = cu->per_cu->dwarf2_per_objfile;
3097 struct objfile *objfile = dwarf2_per_objfile->objfile;
3098 struct dwarf2_per_cu_data *lh_cu;
3099 struct attribute *attr;
3100 void **slot;
3101 struct quick_file_names *qfn;
3102
3103 gdb_assert (! this_cu->is_debug_types);
3104
3105 /* Our callers never want to match partial units -- instead they
3106 will match the enclosing full CU. */
3107 if (comp_unit_die->tag == DW_TAG_partial_unit)
3108 {
3109 this_cu->v.quick->no_file_data = 1;
3110 return;
3111 }
3112
3113 lh_cu = this_cu;
3114 slot = NULL;
3115
3116 line_header_up lh;
3117 sect_offset line_offset {};
3118
3119 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3120 if (attr != nullptr)
3121 {
3122 struct quick_file_names find_entry;
3123
3124 line_offset = (sect_offset) DW_UNSND (attr);
3125
3126 /* We may have already read in this line header (TU line header sharing).
3127 If we have we're done. */
3128 find_entry.hash.dwo_unit = cu->dwo_unit;
3129 find_entry.hash.line_sect_off = line_offset;
3130 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3131 &find_entry, INSERT);
3132 if (*slot != NULL)
3133 {
3134 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3135 return;
3136 }
3137
3138 lh = dwarf_decode_line_header (line_offset, cu);
3139 }
3140 if (lh == NULL)
3141 {
3142 lh_cu->v.quick->no_file_data = 1;
3143 return;
3144 }
3145
3146 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3147 qfn->hash.dwo_unit = cu->dwo_unit;
3148 qfn->hash.line_sect_off = line_offset;
3149 gdb_assert (slot != NULL);
3150 *slot = qfn;
3151
3152 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3153
3154 int offset = 0;
3155 if (strcmp (fnd.name, "<unknown>") != 0)
3156 ++offset;
3157
3158 qfn->num_file_names = offset + lh->file_names_size ();
3159 qfn->file_names =
3160 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3161 if (offset != 0)
3162 qfn->file_names[0] = xstrdup (fnd.name);
3163 for (int i = 0; i < lh->file_names_size (); ++i)
3164 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3165 fnd.comp_dir).release ();
3166 qfn->real_names = NULL;
3167
3168 lh_cu->v.quick->file_names = qfn;
3169 }
3170
3171 /* A helper for the "quick" functions which attempts to read the line
3172 table for THIS_CU. */
3173
3174 static struct quick_file_names *
3175 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3176 {
3177 /* This should never be called for TUs. */
3178 gdb_assert (! this_cu->is_debug_types);
3179 /* Nor type unit groups. */
3180 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3181
3182 if (this_cu->v.quick->file_names != NULL)
3183 return this_cu->v.quick->file_names;
3184 /* If we know there is no line data, no point in looking again. */
3185 if (this_cu->v.quick->no_file_data)
3186 return NULL;
3187
3188 cutu_reader reader (this_cu);
3189 if (!reader.dummy_p)
3190 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3191
3192 if (this_cu->v.quick->no_file_data)
3193 return NULL;
3194 return this_cu->v.quick->file_names;
3195 }
3196
3197 /* A helper for the "quick" functions which computes and caches the
3198 real path for a given file name from the line table. */
3199
3200 static const char *
3201 dw2_get_real_path (struct objfile *objfile,
3202 struct quick_file_names *qfn, int index)
3203 {
3204 if (qfn->real_names == NULL)
3205 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3206 qfn->num_file_names, const char *);
3207
3208 if (qfn->real_names[index] == NULL)
3209 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3210
3211 return qfn->real_names[index];
3212 }
3213
3214 static struct symtab *
3215 dw2_find_last_source_symtab (struct objfile *objfile)
3216 {
3217 struct dwarf2_per_objfile *dwarf2_per_objfile
3218 = get_dwarf2_per_objfile (objfile);
3219 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3220 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3221
3222 if (cust == NULL)
3223 return NULL;
3224
3225 return compunit_primary_filetab (cust);
3226 }
3227
3228 /* Traversal function for dw2_forget_cached_source_info. */
3229
3230 static int
3231 dw2_free_cached_file_names (void **slot, void *info)
3232 {
3233 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3234
3235 if (file_data->real_names)
3236 {
3237 int i;
3238
3239 for (i = 0; i < file_data->num_file_names; ++i)
3240 {
3241 xfree ((void*) file_data->real_names[i]);
3242 file_data->real_names[i] = NULL;
3243 }
3244 }
3245
3246 return 1;
3247 }
3248
3249 static void
3250 dw2_forget_cached_source_info (struct objfile *objfile)
3251 {
3252 struct dwarf2_per_objfile *dwarf2_per_objfile
3253 = get_dwarf2_per_objfile (objfile);
3254
3255 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3256 dw2_free_cached_file_names, NULL);
3257 }
3258
3259 /* Helper function for dw2_map_symtabs_matching_filename that expands
3260 the symtabs and calls the iterator. */
3261
3262 static int
3263 dw2_map_expand_apply (struct objfile *objfile,
3264 struct dwarf2_per_cu_data *per_cu,
3265 const char *name, const char *real_path,
3266 gdb::function_view<bool (symtab *)> callback)
3267 {
3268 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3269
3270 /* Don't visit already-expanded CUs. */
3271 if (per_cu->v.quick->compunit_symtab)
3272 return 0;
3273
3274 /* This may expand more than one symtab, and we want to iterate over
3275 all of them. */
3276 dw2_instantiate_symtab (per_cu, false);
3277
3278 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3279 last_made, callback);
3280 }
3281
3282 /* Implementation of the map_symtabs_matching_filename method. */
3283
3284 static bool
3285 dw2_map_symtabs_matching_filename
3286 (struct objfile *objfile, const char *name, const char *real_path,
3287 gdb::function_view<bool (symtab *)> callback)
3288 {
3289 const char *name_basename = lbasename (name);
3290 struct dwarf2_per_objfile *dwarf2_per_objfile
3291 = get_dwarf2_per_objfile (objfile);
3292
3293 /* The rule is CUs specify all the files, including those used by
3294 any TU, so there's no need to scan TUs here. */
3295
3296 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3297 {
3298 /* We only need to look at symtabs not already expanded. */
3299 if (per_cu->v.quick->compunit_symtab)
3300 continue;
3301
3302 quick_file_names *file_data = dw2_get_file_names (per_cu);
3303 if (file_data == NULL)
3304 continue;
3305
3306 for (int j = 0; j < file_data->num_file_names; ++j)
3307 {
3308 const char *this_name = file_data->file_names[j];
3309 const char *this_real_name;
3310
3311 if (compare_filenames_for_search (this_name, name))
3312 {
3313 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3314 callback))
3315 return true;
3316 continue;
3317 }
3318
3319 /* Before we invoke realpath, which can get expensive when many
3320 files are involved, do a quick comparison of the basenames. */
3321 if (! basenames_may_differ
3322 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3323 continue;
3324
3325 this_real_name = dw2_get_real_path (objfile, file_data, j);
3326 if (compare_filenames_for_search (this_real_name, name))
3327 {
3328 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3329 callback))
3330 return true;
3331 continue;
3332 }
3333
3334 if (real_path != NULL)
3335 {
3336 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3337 gdb_assert (IS_ABSOLUTE_PATH (name));
3338 if (this_real_name != NULL
3339 && FILENAME_CMP (real_path, this_real_name) == 0)
3340 {
3341 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3342 callback))
3343 return true;
3344 continue;
3345 }
3346 }
3347 }
3348 }
3349
3350 return false;
3351 }
3352
3353 /* Struct used to manage iterating over all CUs looking for a symbol. */
3354
3355 struct dw2_symtab_iterator
3356 {
3357 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3358 struct dwarf2_per_objfile *dwarf2_per_objfile;
3359 /* If set, only look for symbols that match that block. Valid values are
3360 GLOBAL_BLOCK and STATIC_BLOCK. */
3361 gdb::optional<block_enum> block_index;
3362 /* The kind of symbol we're looking for. */
3363 domain_enum domain;
3364 /* The list of CUs from the index entry of the symbol,
3365 or NULL if not found. */
3366 offset_type *vec;
3367 /* The next element in VEC to look at. */
3368 int next;
3369 /* The number of elements in VEC, or zero if there is no match. */
3370 int length;
3371 /* Have we seen a global version of the symbol?
3372 If so we can ignore all further global instances.
3373 This is to work around gold/15646, inefficient gold-generated
3374 indices. */
3375 int global_seen;
3376 };
3377
3378 /* Initialize the index symtab iterator ITER. */
3379
3380 static void
3381 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3382 struct dwarf2_per_objfile *dwarf2_per_objfile,
3383 gdb::optional<block_enum> block_index,
3384 domain_enum domain,
3385 const char *name)
3386 {
3387 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3388 iter->block_index = block_index;
3389 iter->domain = domain;
3390 iter->next = 0;
3391 iter->global_seen = 0;
3392
3393 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3394
3395 /* index is NULL if OBJF_READNOW. */
3396 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3397 iter->length = MAYBE_SWAP (*iter->vec);
3398 else
3399 {
3400 iter->vec = NULL;
3401 iter->length = 0;
3402 }
3403 }
3404
3405 /* Return the next matching CU or NULL if there are no more. */
3406
3407 static struct dwarf2_per_cu_data *
3408 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3409 {
3410 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3411
3412 for ( ; iter->next < iter->length; ++iter->next)
3413 {
3414 offset_type cu_index_and_attrs =
3415 MAYBE_SWAP (iter->vec[iter->next + 1]);
3416 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3417 gdb_index_symbol_kind symbol_kind =
3418 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3419 /* Only check the symbol attributes if they're present.
3420 Indices prior to version 7 don't record them,
3421 and indices >= 7 may elide them for certain symbols
3422 (gold does this). */
3423 int attrs_valid =
3424 (dwarf2_per_objfile->index_table->version >= 7
3425 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3426
3427 /* Don't crash on bad data. */
3428 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3429 + dwarf2_per_objfile->all_type_units.size ()))
3430 {
3431 complaint (_(".gdb_index entry has bad CU index"
3432 " [in module %s]"),
3433 objfile_name (dwarf2_per_objfile->objfile));
3434 continue;
3435 }
3436
3437 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3438
3439 /* Skip if already read in. */
3440 if (per_cu->v.quick->compunit_symtab)
3441 continue;
3442
3443 /* Check static vs global. */
3444 if (attrs_valid)
3445 {
3446 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3447
3448 if (iter->block_index.has_value ())
3449 {
3450 bool want_static = *iter->block_index == STATIC_BLOCK;
3451
3452 if (is_static != want_static)
3453 continue;
3454 }
3455
3456 /* Work around gold/15646. */
3457 if (!is_static && iter->global_seen)
3458 continue;
3459 if (!is_static)
3460 iter->global_seen = 1;
3461 }
3462
3463 /* Only check the symbol's kind if it has one. */
3464 if (attrs_valid)
3465 {
3466 switch (iter->domain)
3467 {
3468 case VAR_DOMAIN:
3469 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3471 /* Some types are also in VAR_DOMAIN. */
3472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3473 continue;
3474 break;
3475 case STRUCT_DOMAIN:
3476 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3477 continue;
3478 break;
3479 case LABEL_DOMAIN:
3480 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3481 continue;
3482 break;
3483 case MODULE_DOMAIN:
3484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3485 continue;
3486 break;
3487 default:
3488 break;
3489 }
3490 }
3491
3492 ++iter->next;
3493 return per_cu;
3494 }
3495
3496 return NULL;
3497 }
3498
3499 static struct compunit_symtab *
3500 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3501 const char *name, domain_enum domain)
3502 {
3503 struct compunit_symtab *stab_best = NULL;
3504 struct dwarf2_per_objfile *dwarf2_per_objfile
3505 = get_dwarf2_per_objfile (objfile);
3506
3507 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3508
3509 struct dw2_symtab_iterator iter;
3510 struct dwarf2_per_cu_data *per_cu;
3511
3512 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3513
3514 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3515 {
3516 struct symbol *sym, *with_opaque = NULL;
3517 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3518 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3519 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3520
3521 sym = block_find_symbol (block, name, domain,
3522 block_find_non_opaque_type_preferred,
3523 &with_opaque);
3524
3525 /* Some caution must be observed with overloaded functions
3526 and methods, since the index will not contain any overload
3527 information (but NAME might contain it). */
3528
3529 if (sym != NULL
3530 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3531 return stab;
3532 if (with_opaque != NULL
3533 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3534 stab_best = stab;
3535
3536 /* Keep looking through other CUs. */
3537 }
3538
3539 return stab_best;
3540 }
3541
3542 static void
3543 dw2_print_stats (struct objfile *objfile)
3544 {
3545 struct dwarf2_per_objfile *dwarf2_per_objfile
3546 = get_dwarf2_per_objfile (objfile);
3547 int total = (dwarf2_per_objfile->all_comp_units.size ()
3548 + dwarf2_per_objfile->all_type_units.size ());
3549 int count = 0;
3550
3551 for (int i = 0; i < total; ++i)
3552 {
3553 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3554
3555 if (!per_cu->v.quick->compunit_symtab)
3556 ++count;
3557 }
3558 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3559 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3560 }
3561
3562 /* This dumps minimal information about the index.
3563 It is called via "mt print objfiles".
3564 One use is to verify .gdb_index has been loaded by the
3565 gdb.dwarf2/gdb-index.exp testcase. */
3566
3567 static void
3568 dw2_dump (struct objfile *objfile)
3569 {
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
3572
3573 gdb_assert (dwarf2_per_objfile->using_index);
3574 printf_filtered (".gdb_index:");
3575 if (dwarf2_per_objfile->index_table != NULL)
3576 {
3577 printf_filtered (" version %d\n",
3578 dwarf2_per_objfile->index_table->version);
3579 }
3580 else
3581 printf_filtered (" faked for \"readnow\"\n");
3582 printf_filtered ("\n");
3583 }
3584
3585 static void
3586 dw2_expand_symtabs_for_function (struct objfile *objfile,
3587 const char *func_name)
3588 {
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
3591
3592 struct dw2_symtab_iterator iter;
3593 struct dwarf2_per_cu_data *per_cu;
3594
3595 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3596
3597 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3598 dw2_instantiate_symtab (per_cu, false);
3599
3600 }
3601
3602 static void
3603 dw2_expand_all_symtabs (struct objfile *objfile)
3604 {
3605 struct dwarf2_per_objfile *dwarf2_per_objfile
3606 = get_dwarf2_per_objfile (objfile);
3607 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3608 + dwarf2_per_objfile->all_type_units.size ());
3609
3610 for (int i = 0; i < total_units; ++i)
3611 {
3612 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3613
3614 /* We don't want to directly expand a partial CU, because if we
3615 read it with the wrong language, then assertion failures can
3616 be triggered later on. See PR symtab/23010. So, tell
3617 dw2_instantiate_symtab to skip partial CUs -- any important
3618 partial CU will be read via DW_TAG_imported_unit anyway. */
3619 dw2_instantiate_symtab (per_cu, true);
3620 }
3621 }
3622
3623 static void
3624 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3625 const char *fullname)
3626 {
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
3629
3630 /* We don't need to consider type units here.
3631 This is only called for examining code, e.g. expand_line_sal.
3632 There can be an order of magnitude (or more) more type units
3633 than comp units, and we avoid them if we can. */
3634
3635 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3636 {
3637 /* We only need to look at symtabs not already expanded. */
3638 if (per_cu->v.quick->compunit_symtab)
3639 continue;
3640
3641 quick_file_names *file_data = dw2_get_file_names (per_cu);
3642 if (file_data == NULL)
3643 continue;
3644
3645 for (int j = 0; j < file_data->num_file_names; ++j)
3646 {
3647 const char *this_fullname = file_data->file_names[j];
3648
3649 if (filename_cmp (this_fullname, fullname) == 0)
3650 {
3651 dw2_instantiate_symtab (per_cu, false);
3652 break;
3653 }
3654 }
3655 }
3656 }
3657
3658 static void
3659 dw2_map_matching_symbols
3660 (struct objfile *objfile,
3661 const lookup_name_info &name, domain_enum domain,
3662 int global,
3663 gdb::function_view<symbol_found_callback_ftype> callback,
3664 symbol_compare_ftype *ordered_compare)
3665 {
3666 /* Currently unimplemented; used for Ada. The function can be called if the
3667 current language is Ada for a non-Ada objfile using GNU index. As Ada
3668 does not look for non-Ada symbols this function should just return. */
3669 }
3670
3671 /* Starting from a search name, return the string that finds the upper
3672 bound of all strings that start with SEARCH_NAME in a sorted name
3673 list. Returns the empty string to indicate that the upper bound is
3674 the end of the list. */
3675
3676 static std::string
3677 make_sort_after_prefix_name (const char *search_name)
3678 {
3679 /* When looking to complete "func", we find the upper bound of all
3680 symbols that start with "func" by looking for where we'd insert
3681 the closest string that would follow "func" in lexicographical
3682 order. Usually, that's "func"-with-last-character-incremented,
3683 i.e. "fund". Mind non-ASCII characters, though. Usually those
3684 will be UTF-8 multi-byte sequences, but we can't be certain.
3685 Especially mind the 0xff character, which is a valid character in
3686 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3687 rule out compilers allowing it in identifiers. Note that
3688 conveniently, strcmp/strcasecmp are specified to compare
3689 characters interpreted as unsigned char. So what we do is treat
3690 the whole string as a base 256 number composed of a sequence of
3691 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3692 to 0, and carries 1 to the following more-significant position.
3693 If the very first character in SEARCH_NAME ends up incremented
3694 and carries/overflows, then the upper bound is the end of the
3695 list. The string after the empty string is also the empty
3696 string.
3697
3698 Some examples of this operation:
3699
3700 SEARCH_NAME => "+1" RESULT
3701
3702 "abc" => "abd"
3703 "ab\xff" => "ac"
3704 "\xff" "a" "\xff" => "\xff" "b"
3705 "\xff" => ""
3706 "\xff\xff" => ""
3707 "" => ""
3708
3709 Then, with these symbols for example:
3710
3711 func
3712 func1
3713 fund
3714
3715 completing "func" looks for symbols between "func" and
3716 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3717 which finds "func" and "func1", but not "fund".
3718
3719 And with:
3720
3721 funcÿ (Latin1 'ÿ' [0xff])
3722 funcÿ1
3723 fund
3724
3725 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3726 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3727
3728 And with:
3729
3730 ÿÿ (Latin1 'ÿ' [0xff])
3731 ÿÿ1
3732
3733 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3734 the end of the list.
3735 */
3736 std::string after = search_name;
3737 while (!after.empty () && (unsigned char) after.back () == 0xff)
3738 after.pop_back ();
3739 if (!after.empty ())
3740 after.back () = (unsigned char) after.back () + 1;
3741 return after;
3742 }
3743
3744 /* See declaration. */
3745
3746 std::pair<std::vector<name_component>::const_iterator,
3747 std::vector<name_component>::const_iterator>
3748 mapped_index_base::find_name_components_bounds
3749 (const lookup_name_info &lookup_name_without_params, language lang) const
3750 {
3751 auto *name_cmp
3752 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3753
3754 const char *lang_name
3755 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3756
3757 /* Comparison function object for lower_bound that matches against a
3758 given symbol name. */
3759 auto lookup_compare_lower = [&] (const name_component &elem,
3760 const char *name)
3761 {
3762 const char *elem_qualified = this->symbol_name_at (elem.idx);
3763 const char *elem_name = elem_qualified + elem.name_offset;
3764 return name_cmp (elem_name, name) < 0;
3765 };
3766
3767 /* Comparison function object for upper_bound that matches against a
3768 given symbol name. */
3769 auto lookup_compare_upper = [&] (const char *name,
3770 const name_component &elem)
3771 {
3772 const char *elem_qualified = this->symbol_name_at (elem.idx);
3773 const char *elem_name = elem_qualified + elem.name_offset;
3774 return name_cmp (name, elem_name) < 0;
3775 };
3776
3777 auto begin = this->name_components.begin ();
3778 auto end = this->name_components.end ();
3779
3780 /* Find the lower bound. */
3781 auto lower = [&] ()
3782 {
3783 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3784 return begin;
3785 else
3786 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3787 } ();
3788
3789 /* Find the upper bound. */
3790 auto upper = [&] ()
3791 {
3792 if (lookup_name_without_params.completion_mode ())
3793 {
3794 /* In completion mode, we want UPPER to point past all
3795 symbols names that have the same prefix. I.e., with
3796 these symbols, and completing "func":
3797
3798 function << lower bound
3799 function1
3800 other_function << upper bound
3801
3802 We find the upper bound by looking for the insertion
3803 point of "func"-with-last-character-incremented,
3804 i.e. "fund". */
3805 std::string after = make_sort_after_prefix_name (lang_name);
3806 if (after.empty ())
3807 return end;
3808 return std::lower_bound (lower, end, after.c_str (),
3809 lookup_compare_lower);
3810 }
3811 else
3812 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3813 } ();
3814
3815 return {lower, upper};
3816 }
3817
3818 /* See declaration. */
3819
3820 void
3821 mapped_index_base::build_name_components ()
3822 {
3823 if (!this->name_components.empty ())
3824 return;
3825
3826 this->name_components_casing = case_sensitivity;
3827 auto *name_cmp
3828 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3829
3830 /* The code below only knows how to break apart components of C++
3831 symbol names (and other languages that use '::' as
3832 namespace/module separator) and Ada symbol names. */
3833 auto count = this->symbol_name_count ();
3834 for (offset_type idx = 0; idx < count; idx++)
3835 {
3836 if (this->symbol_name_slot_invalid (idx))
3837 continue;
3838
3839 const char *name = this->symbol_name_at (idx);
3840
3841 /* Add each name component to the name component table. */
3842 unsigned int previous_len = 0;
3843
3844 if (strstr (name, "::") != nullptr)
3845 {
3846 for (unsigned int current_len = cp_find_first_component (name);
3847 name[current_len] != '\0';
3848 current_len += cp_find_first_component (name + current_len))
3849 {
3850 gdb_assert (name[current_len] == ':');
3851 this->name_components.push_back ({previous_len, idx});
3852 /* Skip the '::'. */
3853 current_len += 2;
3854 previous_len = current_len;
3855 }
3856 }
3857 else
3858 {
3859 /* Handle the Ada encoded (aka mangled) form here. */
3860 for (const char *iter = strstr (name, "__");
3861 iter != nullptr;
3862 iter = strstr (iter, "__"))
3863 {
3864 this->name_components.push_back ({previous_len, idx});
3865 iter += 2;
3866 previous_len = iter - name;
3867 }
3868 }
3869
3870 this->name_components.push_back ({previous_len, idx});
3871 }
3872
3873 /* Sort name_components elements by name. */
3874 auto name_comp_compare = [&] (const name_component &left,
3875 const name_component &right)
3876 {
3877 const char *left_qualified = this->symbol_name_at (left.idx);
3878 const char *right_qualified = this->symbol_name_at (right.idx);
3879
3880 const char *left_name = left_qualified + left.name_offset;
3881 const char *right_name = right_qualified + right.name_offset;
3882
3883 return name_cmp (left_name, right_name) < 0;
3884 };
3885
3886 std::sort (this->name_components.begin (),
3887 this->name_components.end (),
3888 name_comp_compare);
3889 }
3890
3891 /* Helper for dw2_expand_symtabs_matching that works with a
3892 mapped_index_base instead of the containing objfile. This is split
3893 to a separate function in order to be able to unit test the
3894 name_components matching using a mock mapped_index_base. For each
3895 symbol name that matches, calls MATCH_CALLBACK, passing it the
3896 symbol's index in the mapped_index_base symbol table. */
3897
3898 static void
3899 dw2_expand_symtabs_matching_symbol
3900 (mapped_index_base &index,
3901 const lookup_name_info &lookup_name_in,
3902 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3903 enum search_domain kind,
3904 gdb::function_view<bool (offset_type)> match_callback)
3905 {
3906 lookup_name_info lookup_name_without_params
3907 = lookup_name_in.make_ignore_params ();
3908
3909 /* Build the symbol name component sorted vector, if we haven't
3910 yet. */
3911 index.build_name_components ();
3912
3913 /* The same symbol may appear more than once in the range though.
3914 E.g., if we're looking for symbols that complete "w", and we have
3915 a symbol named "w1::w2", we'll find the two name components for
3916 that same symbol in the range. To be sure we only call the
3917 callback once per symbol, we first collect the symbol name
3918 indexes that matched in a temporary vector and ignore
3919 duplicates. */
3920 std::vector<offset_type> matches;
3921
3922 struct name_and_matcher
3923 {
3924 symbol_name_matcher_ftype *matcher;
3925 const std::string &name;
3926
3927 bool operator== (const name_and_matcher &other) const
3928 {
3929 return matcher == other.matcher && name == other.name;
3930 }
3931 };
3932
3933 /* A vector holding all the different symbol name matchers, for all
3934 languages. */
3935 std::vector<name_and_matcher> matchers;
3936
3937 for (int i = 0; i < nr_languages; i++)
3938 {
3939 enum language lang_e = (enum language) i;
3940
3941 const language_defn *lang = language_def (lang_e);
3942 symbol_name_matcher_ftype *name_matcher
3943 = get_symbol_name_matcher (lang, lookup_name_without_params);
3944
3945 name_and_matcher key {
3946 name_matcher,
3947 lookup_name_without_params.language_lookup_name (lang_e)
3948 };
3949
3950 /* Don't insert the same comparison routine more than once.
3951 Note that we do this linear walk. This is not a problem in
3952 practice because the number of supported languages is
3953 low. */
3954 if (std::find (matchers.begin (), matchers.end (), key)
3955 != matchers.end ())
3956 continue;
3957 matchers.push_back (std::move (key));
3958
3959 auto bounds
3960 = index.find_name_components_bounds (lookup_name_without_params,
3961 lang_e);
3962
3963 /* Now for each symbol name in range, check to see if we have a name
3964 match, and if so, call the MATCH_CALLBACK callback. */
3965
3966 for (; bounds.first != bounds.second; ++bounds.first)
3967 {
3968 const char *qualified = index.symbol_name_at (bounds.first->idx);
3969
3970 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3971 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3972 continue;
3973
3974 matches.push_back (bounds.first->idx);
3975 }
3976 }
3977
3978 std::sort (matches.begin (), matches.end ());
3979
3980 /* Finally call the callback, once per match. */
3981 ULONGEST prev = -1;
3982 for (offset_type idx : matches)
3983 {
3984 if (prev != idx)
3985 {
3986 if (!match_callback (idx))
3987 break;
3988 prev = idx;
3989 }
3990 }
3991
3992 /* Above we use a type wider than idx's for 'prev', since 0 and
3993 (offset_type)-1 are both possible values. */
3994 static_assert (sizeof (prev) > sizeof (offset_type), "");
3995 }
3996
3997 #if GDB_SELF_TEST
3998
3999 namespace selftests { namespace dw2_expand_symtabs_matching {
4000
4001 /* A mock .gdb_index/.debug_names-like name index table, enough to
4002 exercise dw2_expand_symtabs_matching_symbol, which works with the
4003 mapped_index_base interface. Builds an index from the symbol list
4004 passed as parameter to the constructor. */
4005 class mock_mapped_index : public mapped_index_base
4006 {
4007 public:
4008 mock_mapped_index (gdb::array_view<const char *> symbols)
4009 : m_symbol_table (symbols)
4010 {}
4011
4012 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4013
4014 /* Return the number of names in the symbol table. */
4015 size_t symbol_name_count () const override
4016 {
4017 return m_symbol_table.size ();
4018 }
4019
4020 /* Get the name of the symbol at IDX in the symbol table. */
4021 const char *symbol_name_at (offset_type idx) const override
4022 {
4023 return m_symbol_table[idx];
4024 }
4025
4026 private:
4027 gdb::array_view<const char *> m_symbol_table;
4028 };
4029
4030 /* Convenience function that converts a NULL pointer to a "<null>"
4031 string, to pass to print routines. */
4032
4033 static const char *
4034 string_or_null (const char *str)
4035 {
4036 return str != NULL ? str : "<null>";
4037 }
4038
4039 /* Check if a lookup_name_info built from
4040 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4041 index. EXPECTED_LIST is the list of expected matches, in expected
4042 matching order. If no match expected, then an empty list is
4043 specified. Returns true on success. On failure prints a warning
4044 indicating the file:line that failed, and returns false. */
4045
4046 static bool
4047 check_match (const char *file, int line,
4048 mock_mapped_index &mock_index,
4049 const char *name, symbol_name_match_type match_type,
4050 bool completion_mode,
4051 std::initializer_list<const char *> expected_list)
4052 {
4053 lookup_name_info lookup_name (name, match_type, completion_mode);
4054
4055 bool matched = true;
4056
4057 auto mismatch = [&] (const char *expected_str,
4058 const char *got)
4059 {
4060 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4061 "expected=\"%s\", got=\"%s\"\n"),
4062 file, line,
4063 (match_type == symbol_name_match_type::FULL
4064 ? "FULL" : "WILD"),
4065 name, string_or_null (expected_str), string_or_null (got));
4066 matched = false;
4067 };
4068
4069 auto expected_it = expected_list.begin ();
4070 auto expected_end = expected_list.end ();
4071
4072 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4073 NULL, ALL_DOMAIN,
4074 [&] (offset_type idx)
4075 {
4076 const char *matched_name = mock_index.symbol_name_at (idx);
4077 const char *expected_str
4078 = expected_it == expected_end ? NULL : *expected_it++;
4079
4080 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4081 mismatch (expected_str, matched_name);
4082 return true;
4083 });
4084
4085 const char *expected_str
4086 = expected_it == expected_end ? NULL : *expected_it++;
4087 if (expected_str != NULL)
4088 mismatch (expected_str, NULL);
4089
4090 return matched;
4091 }
4092
4093 /* The symbols added to the mock mapped_index for testing (in
4094 canonical form). */
4095 static const char *test_symbols[] = {
4096 "function",
4097 "std::bar",
4098 "std::zfunction",
4099 "std::zfunction2",
4100 "w1::w2",
4101 "ns::foo<char*>",
4102 "ns::foo<int>",
4103 "ns::foo<long>",
4104 "ns2::tmpl<int>::foo2",
4105 "(anonymous namespace)::A::B::C",
4106
4107 /* These are used to check that the increment-last-char in the
4108 matching algorithm for completion doesn't match "t1_fund" when
4109 completing "t1_func". */
4110 "t1_func",
4111 "t1_func1",
4112 "t1_fund",
4113 "t1_fund1",
4114
4115 /* A UTF-8 name with multi-byte sequences to make sure that
4116 cp-name-parser understands this as a single identifier ("função"
4117 is "function" in PT). */
4118 u8"u8função",
4119
4120 /* \377 (0xff) is Latin1 'ÿ'. */
4121 "yfunc\377",
4122
4123 /* \377 (0xff) is Latin1 'ÿ'. */
4124 "\377",
4125 "\377\377123",
4126
4127 /* A name with all sorts of complications. Starts with "z" to make
4128 it easier for the completion tests below. */
4129 #define Z_SYM_NAME \
4130 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4131 "::tuple<(anonymous namespace)::ui*, " \
4132 "std::default_delete<(anonymous namespace)::ui>, void>"
4133
4134 Z_SYM_NAME
4135 };
4136
4137 /* Returns true if the mapped_index_base::find_name_component_bounds
4138 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4139 in completion mode. */
4140
4141 static bool
4142 check_find_bounds_finds (mapped_index_base &index,
4143 const char *search_name,
4144 gdb::array_view<const char *> expected_syms)
4145 {
4146 lookup_name_info lookup_name (search_name,
4147 symbol_name_match_type::FULL, true);
4148
4149 auto bounds = index.find_name_components_bounds (lookup_name,
4150 language_cplus);
4151
4152 size_t distance = std::distance (bounds.first, bounds.second);
4153 if (distance != expected_syms.size ())
4154 return false;
4155
4156 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4157 {
4158 auto nc_elem = bounds.first + exp_elem;
4159 const char *qualified = index.symbol_name_at (nc_elem->idx);
4160 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4161 return false;
4162 }
4163
4164 return true;
4165 }
4166
4167 /* Test the lower-level mapped_index::find_name_component_bounds
4168 method. */
4169
4170 static void
4171 test_mapped_index_find_name_component_bounds ()
4172 {
4173 mock_mapped_index mock_index (test_symbols);
4174
4175 mock_index.build_name_components ();
4176
4177 /* Test the lower-level mapped_index::find_name_component_bounds
4178 method in completion mode. */
4179 {
4180 static const char *expected_syms[] = {
4181 "t1_func",
4182 "t1_func1",
4183 };
4184
4185 SELF_CHECK (check_find_bounds_finds (mock_index,
4186 "t1_func", expected_syms));
4187 }
4188
4189 /* Check that the increment-last-char in the name matching algorithm
4190 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4191 {
4192 static const char *expected_syms1[] = {
4193 "\377",
4194 "\377\377123",
4195 };
4196 SELF_CHECK (check_find_bounds_finds (mock_index,
4197 "\377", expected_syms1));
4198
4199 static const char *expected_syms2[] = {
4200 "\377\377123",
4201 };
4202 SELF_CHECK (check_find_bounds_finds (mock_index,
4203 "\377\377", expected_syms2));
4204 }
4205 }
4206
4207 /* Test dw2_expand_symtabs_matching_symbol. */
4208
4209 static void
4210 test_dw2_expand_symtabs_matching_symbol ()
4211 {
4212 mock_mapped_index mock_index (test_symbols);
4213
4214 /* We let all tests run until the end even if some fails, for debug
4215 convenience. */
4216 bool any_mismatch = false;
4217
4218 /* Create the expected symbols list (an initializer_list). Needed
4219 because lists have commas, and we need to pass them to CHECK,
4220 which is a macro. */
4221 #define EXPECT(...) { __VA_ARGS__ }
4222
4223 /* Wrapper for check_match that passes down the current
4224 __FILE__/__LINE__. */
4225 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4226 any_mismatch |= !check_match (__FILE__, __LINE__, \
4227 mock_index, \
4228 NAME, MATCH_TYPE, COMPLETION_MODE, \
4229 EXPECTED_LIST)
4230
4231 /* Identity checks. */
4232 for (const char *sym : test_symbols)
4233 {
4234 /* Should be able to match all existing symbols. */
4235 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4236 EXPECT (sym));
4237
4238 /* Should be able to match all existing symbols with
4239 parameters. */
4240 std::string with_params = std::string (sym) + "(int)";
4241 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4242 EXPECT (sym));
4243
4244 /* Should be able to match all existing symbols with
4245 parameters and qualifiers. */
4246 with_params = std::string (sym) + " ( int ) const";
4247 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4248 EXPECT (sym));
4249
4250 /* This should really find sym, but cp-name-parser.y doesn't
4251 know about lvalue/rvalue qualifiers yet. */
4252 with_params = std::string (sym) + " ( int ) &&";
4253 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4254 {});
4255 }
4256
4257 /* Check that the name matching algorithm for completion doesn't get
4258 confused with Latin1 'ÿ' / 0xff. */
4259 {
4260 static const char str[] = "\377";
4261 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4262 EXPECT ("\377", "\377\377123"));
4263 }
4264
4265 /* Check that the increment-last-char in the matching algorithm for
4266 completion doesn't match "t1_fund" when completing "t1_func". */
4267 {
4268 static const char str[] = "t1_func";
4269 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4270 EXPECT ("t1_func", "t1_func1"));
4271 }
4272
4273 /* Check that completion mode works at each prefix of the expected
4274 symbol name. */
4275 {
4276 static const char str[] = "function(int)";
4277 size_t len = strlen (str);
4278 std::string lookup;
4279
4280 for (size_t i = 1; i < len; i++)
4281 {
4282 lookup.assign (str, i);
4283 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4284 EXPECT ("function"));
4285 }
4286 }
4287
4288 /* While "w" is a prefix of both components, the match function
4289 should still only be called once. */
4290 {
4291 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4292 EXPECT ("w1::w2"));
4293 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4294 EXPECT ("w1::w2"));
4295 }
4296
4297 /* Same, with a "complicated" symbol. */
4298 {
4299 static const char str[] = Z_SYM_NAME;
4300 size_t len = strlen (str);
4301 std::string lookup;
4302
4303 for (size_t i = 1; i < len; i++)
4304 {
4305 lookup.assign (str, i);
4306 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4307 EXPECT (Z_SYM_NAME));
4308 }
4309 }
4310
4311 /* In FULL mode, an incomplete symbol doesn't match. */
4312 {
4313 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4314 {});
4315 }
4316
4317 /* A complete symbol with parameters matches any overload, since the
4318 index has no overload info. */
4319 {
4320 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4321 EXPECT ("std::zfunction", "std::zfunction2"));
4322 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4323 EXPECT ("std::zfunction", "std::zfunction2"));
4324 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4325 EXPECT ("std::zfunction", "std::zfunction2"));
4326 }
4327
4328 /* Check that whitespace is ignored appropriately. A symbol with a
4329 template argument list. */
4330 {
4331 static const char expected[] = "ns::foo<int>";
4332 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4333 EXPECT (expected));
4334 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4335 EXPECT (expected));
4336 }
4337
4338 /* Check that whitespace is ignored appropriately. A symbol with a
4339 template argument list that includes a pointer. */
4340 {
4341 static const char expected[] = "ns::foo<char*>";
4342 /* Try both completion and non-completion modes. */
4343 static const bool completion_mode[2] = {false, true};
4344 for (size_t i = 0; i < 2; i++)
4345 {
4346 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4347 completion_mode[i], EXPECT (expected));
4348 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4349 completion_mode[i], EXPECT (expected));
4350
4351 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4352 completion_mode[i], EXPECT (expected));
4353 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4354 completion_mode[i], EXPECT (expected));
4355 }
4356 }
4357
4358 {
4359 /* Check method qualifiers are ignored. */
4360 static const char expected[] = "ns::foo<char*>";
4361 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4362 symbol_name_match_type::FULL, true, EXPECT (expected));
4363 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4364 symbol_name_match_type::FULL, true, EXPECT (expected));
4365 CHECK_MATCH ("foo < char * > ( int ) const",
4366 symbol_name_match_type::WILD, true, EXPECT (expected));
4367 CHECK_MATCH ("foo < char * > ( int ) &&",
4368 symbol_name_match_type::WILD, true, EXPECT (expected));
4369 }
4370
4371 /* Test lookup names that don't match anything. */
4372 {
4373 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4374 {});
4375
4376 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4377 {});
4378 }
4379
4380 /* Some wild matching tests, exercising "(anonymous namespace)",
4381 which should not be confused with a parameter list. */
4382 {
4383 static const char *syms[] = {
4384 "A::B::C",
4385 "B::C",
4386 "C",
4387 "A :: B :: C ( int )",
4388 "B :: C ( int )",
4389 "C ( int )",
4390 };
4391
4392 for (const char *s : syms)
4393 {
4394 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4395 EXPECT ("(anonymous namespace)::A::B::C"));
4396 }
4397 }
4398
4399 {
4400 static const char expected[] = "ns2::tmpl<int>::foo2";
4401 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4402 EXPECT (expected));
4403 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4404 EXPECT (expected));
4405 }
4406
4407 SELF_CHECK (!any_mismatch);
4408
4409 #undef EXPECT
4410 #undef CHECK_MATCH
4411 }
4412
4413 static void
4414 run_test ()
4415 {
4416 test_mapped_index_find_name_component_bounds ();
4417 test_dw2_expand_symtabs_matching_symbol ();
4418 }
4419
4420 }} // namespace selftests::dw2_expand_symtabs_matching
4421
4422 #endif /* GDB_SELF_TEST */
4423
4424 /* If FILE_MATCHER is NULL or if PER_CU has
4425 dwarf2_per_cu_quick_data::MARK set (see
4426 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4427 EXPANSION_NOTIFY on it. */
4428
4429 static void
4430 dw2_expand_symtabs_matching_one
4431 (struct dwarf2_per_cu_data *per_cu,
4432 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4433 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4434 {
4435 if (file_matcher == NULL || per_cu->v.quick->mark)
4436 {
4437 bool symtab_was_null
4438 = (per_cu->v.quick->compunit_symtab == NULL);
4439
4440 dw2_instantiate_symtab (per_cu, false);
4441
4442 if (expansion_notify != NULL
4443 && symtab_was_null
4444 && per_cu->v.quick->compunit_symtab != NULL)
4445 expansion_notify (per_cu->v.quick->compunit_symtab);
4446 }
4447 }
4448
4449 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4450 matched, to expand corresponding CUs that were marked. IDX is the
4451 index of the symbol name that matched. */
4452
4453 static void
4454 dw2_expand_marked_cus
4455 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4456 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4457 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4458 search_domain kind)
4459 {
4460 offset_type *vec, vec_len, vec_idx;
4461 bool global_seen = false;
4462 mapped_index &index = *dwarf2_per_objfile->index_table;
4463
4464 vec = (offset_type *) (index.constant_pool
4465 + MAYBE_SWAP (index.symbol_table[idx].vec));
4466 vec_len = MAYBE_SWAP (vec[0]);
4467 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4468 {
4469 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4470 /* This value is only valid for index versions >= 7. */
4471 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4472 gdb_index_symbol_kind symbol_kind =
4473 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4474 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4475 /* Only check the symbol attributes if they're present.
4476 Indices prior to version 7 don't record them,
4477 and indices >= 7 may elide them for certain symbols
4478 (gold does this). */
4479 int attrs_valid =
4480 (index.version >= 7
4481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4482
4483 /* Work around gold/15646. */
4484 if (attrs_valid)
4485 {
4486 if (!is_static && global_seen)
4487 continue;
4488 if (!is_static)
4489 global_seen = true;
4490 }
4491
4492 /* Only check the symbol's kind if it has one. */
4493 if (attrs_valid)
4494 {
4495 switch (kind)
4496 {
4497 case VARIABLES_DOMAIN:
4498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4499 continue;
4500 break;
4501 case FUNCTIONS_DOMAIN:
4502 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4503 continue;
4504 break;
4505 case TYPES_DOMAIN:
4506 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4507 continue;
4508 break;
4509 case MODULES_DOMAIN:
4510 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4511 continue;
4512 break;
4513 default:
4514 break;
4515 }
4516 }
4517
4518 /* Don't crash on bad data. */
4519 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4520 + dwarf2_per_objfile->all_type_units.size ()))
4521 {
4522 complaint (_(".gdb_index entry has bad CU index"
4523 " [in module %s]"),
4524 objfile_name (dwarf2_per_objfile->objfile));
4525 continue;
4526 }
4527
4528 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4529 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4530 expansion_notify);
4531 }
4532 }
4533
4534 /* If FILE_MATCHER is non-NULL, set all the
4535 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4536 that match FILE_MATCHER. */
4537
4538 static void
4539 dw_expand_symtabs_matching_file_matcher
4540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4541 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4542 {
4543 if (file_matcher == NULL)
4544 return;
4545
4546 objfile *const objfile = dwarf2_per_objfile->objfile;
4547
4548 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4549 htab_eq_pointer,
4550 NULL, xcalloc, xfree));
4551 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4552 htab_eq_pointer,
4553 NULL, xcalloc, xfree));
4554
4555 /* The rule is CUs specify all the files, including those used by
4556 any TU, so there's no need to scan TUs here. */
4557
4558 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4559 {
4560 QUIT;
4561
4562 per_cu->v.quick->mark = 0;
4563
4564 /* We only need to look at symtabs not already expanded. */
4565 if (per_cu->v.quick->compunit_symtab)
4566 continue;
4567
4568 quick_file_names *file_data = dw2_get_file_names (per_cu);
4569 if (file_data == NULL)
4570 continue;
4571
4572 if (htab_find (visited_not_found.get (), file_data) != NULL)
4573 continue;
4574 else if (htab_find (visited_found.get (), file_data) != NULL)
4575 {
4576 per_cu->v.quick->mark = 1;
4577 continue;
4578 }
4579
4580 for (int j = 0; j < file_data->num_file_names; ++j)
4581 {
4582 const char *this_real_name;
4583
4584 if (file_matcher (file_data->file_names[j], false))
4585 {
4586 per_cu->v.quick->mark = 1;
4587 break;
4588 }
4589
4590 /* Before we invoke realpath, which can get expensive when many
4591 files are involved, do a quick comparison of the basenames. */
4592 if (!basenames_may_differ
4593 && !file_matcher (lbasename (file_data->file_names[j]),
4594 true))
4595 continue;
4596
4597 this_real_name = dw2_get_real_path (objfile, file_data, j);
4598 if (file_matcher (this_real_name, false))
4599 {
4600 per_cu->v.quick->mark = 1;
4601 break;
4602 }
4603 }
4604
4605 void **slot = htab_find_slot (per_cu->v.quick->mark
4606 ? visited_found.get ()
4607 : visited_not_found.get (),
4608 file_data, INSERT);
4609 *slot = file_data;
4610 }
4611 }
4612
4613 static void
4614 dw2_expand_symtabs_matching
4615 (struct objfile *objfile,
4616 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4617 const lookup_name_info &lookup_name,
4618 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4619 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4620 enum search_domain kind)
4621 {
4622 struct dwarf2_per_objfile *dwarf2_per_objfile
4623 = get_dwarf2_per_objfile (objfile);
4624
4625 /* index_table is NULL if OBJF_READNOW. */
4626 if (!dwarf2_per_objfile->index_table)
4627 return;
4628
4629 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4630
4631 mapped_index &index = *dwarf2_per_objfile->index_table;
4632
4633 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4634 symbol_matcher,
4635 kind, [&] (offset_type idx)
4636 {
4637 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4638 expansion_notify, kind);
4639 return true;
4640 });
4641 }
4642
4643 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4644 symtab. */
4645
4646 static struct compunit_symtab *
4647 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4648 CORE_ADDR pc)
4649 {
4650 int i;
4651
4652 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4653 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4654 return cust;
4655
4656 if (cust->includes == NULL)
4657 return NULL;
4658
4659 for (i = 0; cust->includes[i]; ++i)
4660 {
4661 struct compunit_symtab *s = cust->includes[i];
4662
4663 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4664 if (s != NULL)
4665 return s;
4666 }
4667
4668 return NULL;
4669 }
4670
4671 static struct compunit_symtab *
4672 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4673 struct bound_minimal_symbol msymbol,
4674 CORE_ADDR pc,
4675 struct obj_section *section,
4676 int warn_if_readin)
4677 {
4678 struct dwarf2_per_cu_data *data;
4679 struct compunit_symtab *result;
4680
4681 if (!objfile->partial_symtabs->psymtabs_addrmap)
4682 return NULL;
4683
4684 CORE_ADDR baseaddr = objfile->text_section_offset ();
4685 data = (struct dwarf2_per_cu_data *) addrmap_find
4686 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4687 if (!data)
4688 return NULL;
4689
4690 if (warn_if_readin && data->v.quick->compunit_symtab)
4691 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4692 paddress (get_objfile_arch (objfile), pc));
4693
4694 result
4695 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4696 false),
4697 pc);
4698 gdb_assert (result != NULL);
4699 return result;
4700 }
4701
4702 static void
4703 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4704 void *data, int need_fullname)
4705 {
4706 struct dwarf2_per_objfile *dwarf2_per_objfile
4707 = get_dwarf2_per_objfile (objfile);
4708
4709 if (!dwarf2_per_objfile->filenames_cache)
4710 {
4711 dwarf2_per_objfile->filenames_cache.emplace ();
4712
4713 htab_up visited (htab_create_alloc (10,
4714 htab_hash_pointer, htab_eq_pointer,
4715 NULL, xcalloc, xfree));
4716
4717 /* The rule is CUs specify all the files, including those used
4718 by any TU, so there's no need to scan TUs here. We can
4719 ignore file names coming from already-expanded CUs. */
4720
4721 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4722 {
4723 if (per_cu->v.quick->compunit_symtab)
4724 {
4725 void **slot = htab_find_slot (visited.get (),
4726 per_cu->v.quick->file_names,
4727 INSERT);
4728
4729 *slot = per_cu->v.quick->file_names;
4730 }
4731 }
4732
4733 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4734 {
4735 /* We only need to look at symtabs not already expanded. */
4736 if (per_cu->v.quick->compunit_symtab)
4737 continue;
4738
4739 quick_file_names *file_data = dw2_get_file_names (per_cu);
4740 if (file_data == NULL)
4741 continue;
4742
4743 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4744 if (*slot)
4745 {
4746 /* Already visited. */
4747 continue;
4748 }
4749 *slot = file_data;
4750
4751 for (int j = 0; j < file_data->num_file_names; ++j)
4752 {
4753 const char *filename = file_data->file_names[j];
4754 dwarf2_per_objfile->filenames_cache->seen (filename);
4755 }
4756 }
4757 }
4758
4759 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4760 {
4761 gdb::unique_xmalloc_ptr<char> this_real_name;
4762
4763 if (need_fullname)
4764 this_real_name = gdb_realpath (filename);
4765 (*fun) (filename, this_real_name.get (), data);
4766 });
4767 }
4768
4769 static int
4770 dw2_has_symbols (struct objfile *objfile)
4771 {
4772 return 1;
4773 }
4774
4775 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4776 {
4777 dw2_has_symbols,
4778 dw2_find_last_source_symtab,
4779 dw2_forget_cached_source_info,
4780 dw2_map_symtabs_matching_filename,
4781 dw2_lookup_symbol,
4782 dw2_print_stats,
4783 dw2_dump,
4784 dw2_expand_symtabs_for_function,
4785 dw2_expand_all_symtabs,
4786 dw2_expand_symtabs_with_fullname,
4787 dw2_map_matching_symbols,
4788 dw2_expand_symtabs_matching,
4789 dw2_find_pc_sect_compunit_symtab,
4790 NULL,
4791 dw2_map_symbol_filenames
4792 };
4793
4794 /* DWARF-5 debug_names reader. */
4795
4796 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4797 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4798
4799 /* A helper function that reads the .debug_names section in SECTION
4800 and fills in MAP. FILENAME is the name of the file containing the
4801 section; it is used for error reporting.
4802
4803 Returns true if all went well, false otherwise. */
4804
4805 static bool
4806 read_debug_names_from_section (struct objfile *objfile,
4807 const char *filename,
4808 struct dwarf2_section_info *section,
4809 mapped_debug_names &map)
4810 {
4811 if (section->empty ())
4812 return false;
4813
4814 /* Older elfutils strip versions could keep the section in the main
4815 executable while splitting it for the separate debug info file. */
4816 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4817 return false;
4818
4819 section->read (objfile);
4820
4821 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4822
4823 const gdb_byte *addr = section->buffer;
4824
4825 bfd *const abfd = section->get_bfd_owner ();
4826
4827 unsigned int bytes_read;
4828 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4829 addr += bytes_read;
4830
4831 map.dwarf5_is_dwarf64 = bytes_read != 4;
4832 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4833 if (bytes_read + length != section->size)
4834 {
4835 /* There may be multiple per-CU indices. */
4836 warning (_("Section .debug_names in %s length %s does not match "
4837 "section length %s, ignoring .debug_names."),
4838 filename, plongest (bytes_read + length),
4839 pulongest (section->size));
4840 return false;
4841 }
4842
4843 /* The version number. */
4844 uint16_t version = read_2_bytes (abfd, addr);
4845 addr += 2;
4846 if (version != 5)
4847 {
4848 warning (_("Section .debug_names in %s has unsupported version %d, "
4849 "ignoring .debug_names."),
4850 filename, version);
4851 return false;
4852 }
4853
4854 /* Padding. */
4855 uint16_t padding = read_2_bytes (abfd, addr);
4856 addr += 2;
4857 if (padding != 0)
4858 {
4859 warning (_("Section .debug_names in %s has unsupported padding %d, "
4860 "ignoring .debug_names."),
4861 filename, padding);
4862 return false;
4863 }
4864
4865 /* comp_unit_count - The number of CUs in the CU list. */
4866 map.cu_count = read_4_bytes (abfd, addr);
4867 addr += 4;
4868
4869 /* local_type_unit_count - The number of TUs in the local TU
4870 list. */
4871 map.tu_count = read_4_bytes (abfd, addr);
4872 addr += 4;
4873
4874 /* foreign_type_unit_count - The number of TUs in the foreign TU
4875 list. */
4876 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4877 addr += 4;
4878 if (foreign_tu_count != 0)
4879 {
4880 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4881 "ignoring .debug_names."),
4882 filename, static_cast<unsigned long> (foreign_tu_count));
4883 return false;
4884 }
4885
4886 /* bucket_count - The number of hash buckets in the hash lookup
4887 table. */
4888 map.bucket_count = read_4_bytes (abfd, addr);
4889 addr += 4;
4890
4891 /* name_count - The number of unique names in the index. */
4892 map.name_count = read_4_bytes (abfd, addr);
4893 addr += 4;
4894
4895 /* abbrev_table_size - The size in bytes of the abbreviations
4896 table. */
4897 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4898 addr += 4;
4899
4900 /* augmentation_string_size - The size in bytes of the augmentation
4901 string. This value is rounded up to a multiple of 4. */
4902 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4903 addr += 4;
4904 map.augmentation_is_gdb = ((augmentation_string_size
4905 == sizeof (dwarf5_augmentation))
4906 && memcmp (addr, dwarf5_augmentation,
4907 sizeof (dwarf5_augmentation)) == 0);
4908 augmentation_string_size += (-augmentation_string_size) & 3;
4909 addr += augmentation_string_size;
4910
4911 /* List of CUs */
4912 map.cu_table_reordered = addr;
4913 addr += map.cu_count * map.offset_size;
4914
4915 /* List of Local TUs */
4916 map.tu_table_reordered = addr;
4917 addr += map.tu_count * map.offset_size;
4918
4919 /* Hash Lookup Table */
4920 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4921 addr += map.bucket_count * 4;
4922 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.name_count * 4;
4924
4925 /* Name Table */
4926 map.name_table_string_offs_reordered = addr;
4927 addr += map.name_count * map.offset_size;
4928 map.name_table_entry_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930
4931 const gdb_byte *abbrev_table_start = addr;
4932 for (;;)
4933 {
4934 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4935 addr += bytes_read;
4936 if (index_num == 0)
4937 break;
4938
4939 const auto insertpair
4940 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4941 if (!insertpair.second)
4942 {
4943 warning (_("Section .debug_names in %s has duplicate index %s, "
4944 "ignoring .debug_names."),
4945 filename, pulongest (index_num));
4946 return false;
4947 }
4948 mapped_debug_names::index_val &indexval = insertpair.first->second;
4949 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4950 addr += bytes_read;
4951
4952 for (;;)
4953 {
4954 mapped_debug_names::index_val::attr attr;
4955 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4956 addr += bytes_read;
4957 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 if (attr.form == DW_FORM_implicit_const)
4960 {
4961 attr.implicit_const = read_signed_leb128 (abfd, addr,
4962 &bytes_read);
4963 addr += bytes_read;
4964 }
4965 if (attr.dw_idx == 0 && attr.form == 0)
4966 break;
4967 indexval.attr_vec.push_back (std::move (attr));
4968 }
4969 }
4970 if (addr != abbrev_table_start + abbrev_table_size)
4971 {
4972 warning (_("Section .debug_names in %s has abbreviation_table "
4973 "of size %s vs. written as %u, ignoring .debug_names."),
4974 filename, plongest (addr - abbrev_table_start),
4975 abbrev_table_size);
4976 return false;
4977 }
4978 map.entry_pool = addr;
4979
4980 return true;
4981 }
4982
4983 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4984 list. */
4985
4986 static void
4987 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4988 const mapped_debug_names &map,
4989 dwarf2_section_info &section,
4990 bool is_dwz)
4991 {
4992 sect_offset sect_off_prev;
4993 for (uint32_t i = 0; i <= map.cu_count; ++i)
4994 {
4995 sect_offset sect_off_next;
4996 if (i < map.cu_count)
4997 {
4998 sect_off_next
4999 = (sect_offset) (extract_unsigned_integer
5000 (map.cu_table_reordered + i * map.offset_size,
5001 map.offset_size,
5002 map.dwarf5_byte_order));
5003 }
5004 else
5005 sect_off_next = (sect_offset) section.size;
5006 if (i >= 1)
5007 {
5008 const ULONGEST length = sect_off_next - sect_off_prev;
5009 dwarf2_per_cu_data *per_cu
5010 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5011 sect_off_prev, length);
5012 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5013 }
5014 sect_off_prev = sect_off_next;
5015 }
5016 }
5017
5018 /* Read the CU list from the mapped index, and use it to create all
5019 the CU objects for this dwarf2_per_objfile. */
5020
5021 static void
5022 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5023 const mapped_debug_names &map,
5024 const mapped_debug_names &dwz_map)
5025 {
5026 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5027 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5028
5029 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5030 dwarf2_per_objfile->info,
5031 false /* is_dwz */);
5032
5033 if (dwz_map.cu_count == 0)
5034 return;
5035
5036 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5037 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5038 true /* is_dwz */);
5039 }
5040
5041 /* Read .debug_names. If everything went ok, initialize the "quick"
5042 elements of all the CUs and return true. Otherwise, return false. */
5043
5044 static bool
5045 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5046 {
5047 std::unique_ptr<mapped_debug_names> map
5048 (new mapped_debug_names (dwarf2_per_objfile));
5049 mapped_debug_names dwz_map (dwarf2_per_objfile);
5050 struct objfile *objfile = dwarf2_per_objfile->objfile;
5051
5052 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5053 &dwarf2_per_objfile->debug_names,
5054 *map))
5055 return false;
5056
5057 /* Don't use the index if it's empty. */
5058 if (map->name_count == 0)
5059 return false;
5060
5061 /* If there is a .dwz file, read it so we can get its CU list as
5062 well. */
5063 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5064 if (dwz != NULL)
5065 {
5066 if (!read_debug_names_from_section (objfile,
5067 bfd_get_filename (dwz->dwz_bfd.get ()),
5068 &dwz->debug_names, dwz_map))
5069 {
5070 warning (_("could not read '.debug_names' section from %s; skipping"),
5071 bfd_get_filename (dwz->dwz_bfd.get ()));
5072 return false;
5073 }
5074 }
5075
5076 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5077
5078 if (map->tu_count != 0)
5079 {
5080 /* We can only handle a single .debug_types when we have an
5081 index. */
5082 if (dwarf2_per_objfile->types.size () != 1)
5083 return false;
5084
5085 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5086
5087 create_signatured_type_table_from_debug_names
5088 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5089 }
5090
5091 create_addrmap_from_aranges (dwarf2_per_objfile,
5092 &dwarf2_per_objfile->debug_aranges);
5093
5094 dwarf2_per_objfile->debug_names_table = std::move (map);
5095 dwarf2_per_objfile->using_index = 1;
5096 dwarf2_per_objfile->quick_file_names_table =
5097 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5098
5099 return true;
5100 }
5101
5102 /* Type used to manage iterating over all CUs looking for a symbol for
5103 .debug_names. */
5104
5105 class dw2_debug_names_iterator
5106 {
5107 public:
5108 dw2_debug_names_iterator (const mapped_debug_names &map,
5109 gdb::optional<block_enum> block_index,
5110 domain_enum domain,
5111 const char *name)
5112 : m_map (map), m_block_index (block_index), m_domain (domain),
5113 m_addr (find_vec_in_debug_names (map, name))
5114 {}
5115
5116 dw2_debug_names_iterator (const mapped_debug_names &map,
5117 search_domain search, uint32_t namei)
5118 : m_map (map),
5119 m_search (search),
5120 m_addr (find_vec_in_debug_names (map, namei))
5121 {}
5122
5123 dw2_debug_names_iterator (const mapped_debug_names &map,
5124 block_enum block_index, domain_enum domain,
5125 uint32_t namei)
5126 : m_map (map), m_block_index (block_index), m_domain (domain),
5127 m_addr (find_vec_in_debug_names (map, namei))
5128 {}
5129
5130 /* Return the next matching CU or NULL if there are no more. */
5131 dwarf2_per_cu_data *next ();
5132
5133 private:
5134 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5135 const char *name);
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 uint32_t namei);
5138
5139 /* The internalized form of .debug_names. */
5140 const mapped_debug_names &m_map;
5141
5142 /* If set, only look for symbols that match that block. Valid values are
5143 GLOBAL_BLOCK and STATIC_BLOCK. */
5144 const gdb::optional<block_enum> m_block_index;
5145
5146 /* The kind of symbol we're looking for. */
5147 const domain_enum m_domain = UNDEF_DOMAIN;
5148 const search_domain m_search = ALL_DOMAIN;
5149
5150 /* The list of CUs from the index entry of the symbol, or NULL if
5151 not found. */
5152 const gdb_byte *m_addr;
5153 };
5154
5155 const char *
5156 mapped_debug_names::namei_to_name (uint32_t namei) const
5157 {
5158 const ULONGEST namei_string_offs
5159 = extract_unsigned_integer ((name_table_string_offs_reordered
5160 + namei * offset_size),
5161 offset_size,
5162 dwarf5_byte_order);
5163 return read_indirect_string_at_offset
5164 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5165 }
5166
5167 /* Find a slot in .debug_names for the object named NAME. If NAME is
5168 found, return pointer to its pool data. If NAME cannot be found,
5169 return NULL. */
5170
5171 const gdb_byte *
5172 dw2_debug_names_iterator::find_vec_in_debug_names
5173 (const mapped_debug_names &map, const char *name)
5174 {
5175 int (*cmp) (const char *, const char *);
5176
5177 gdb::unique_xmalloc_ptr<char> without_params;
5178 if (current_language->la_language == language_cplus
5179 || current_language->la_language == language_fortran
5180 || current_language->la_language == language_d)
5181 {
5182 /* NAME is already canonical. Drop any qualifiers as
5183 .debug_names does not contain any. */
5184
5185 if (strchr (name, '(') != NULL)
5186 {
5187 without_params = cp_remove_params (name);
5188 if (without_params != NULL)
5189 name = without_params.get ();
5190 }
5191 }
5192
5193 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5194
5195 const uint32_t full_hash = dwarf5_djb_hash (name);
5196 uint32_t namei
5197 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5198 (map.bucket_table_reordered
5199 + (full_hash % map.bucket_count)), 4,
5200 map.dwarf5_byte_order);
5201 if (namei == 0)
5202 return NULL;
5203 --namei;
5204 if (namei >= map.name_count)
5205 {
5206 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5207 "[in module %s]"),
5208 namei, map.name_count,
5209 objfile_name (map.dwarf2_per_objfile->objfile));
5210 return NULL;
5211 }
5212
5213 for (;;)
5214 {
5215 const uint32_t namei_full_hash
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.hash_table_reordered + namei), 4,
5218 map.dwarf5_byte_order);
5219 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5220 return NULL;
5221
5222 if (full_hash == namei_full_hash)
5223 {
5224 const char *const namei_string = map.namei_to_name (namei);
5225
5226 #if 0 /* An expensive sanity check. */
5227 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5228 {
5229 complaint (_("Wrong .debug_names hash for string at index %u "
5230 "[in module %s]"),
5231 namei, objfile_name (dwarf2_per_objfile->objfile));
5232 return NULL;
5233 }
5234 #endif
5235
5236 if (cmp (namei_string, name) == 0)
5237 {
5238 const ULONGEST namei_entry_offs
5239 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5240 + namei * map.offset_size),
5241 map.offset_size, map.dwarf5_byte_order);
5242 return map.entry_pool + namei_entry_offs;
5243 }
5244 }
5245
5246 ++namei;
5247 if (namei >= map.name_count)
5248 return NULL;
5249 }
5250 }
5251
5252 const gdb_byte *
5253 dw2_debug_names_iterator::find_vec_in_debug_names
5254 (const mapped_debug_names &map, uint32_t namei)
5255 {
5256 if (namei >= map.name_count)
5257 {
5258 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5259 "[in module %s]"),
5260 namei, map.name_count,
5261 objfile_name (map.dwarf2_per_objfile->objfile));
5262 return NULL;
5263 }
5264
5265 const ULONGEST namei_entry_offs
5266 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5267 + namei * map.offset_size),
5268 map.offset_size, map.dwarf5_byte_order);
5269 return map.entry_pool + namei_entry_offs;
5270 }
5271
5272 /* See dw2_debug_names_iterator. */
5273
5274 dwarf2_per_cu_data *
5275 dw2_debug_names_iterator::next ()
5276 {
5277 if (m_addr == NULL)
5278 return NULL;
5279
5280 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5281 struct objfile *objfile = dwarf2_per_objfile->objfile;
5282 bfd *const abfd = objfile->obfd;
5283
5284 again:
5285
5286 unsigned int bytes_read;
5287 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5288 m_addr += bytes_read;
5289 if (abbrev == 0)
5290 return NULL;
5291
5292 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5293 if (indexval_it == m_map.abbrev_map.cend ())
5294 {
5295 complaint (_("Wrong .debug_names undefined abbrev code %s "
5296 "[in module %s]"),
5297 pulongest (abbrev), objfile_name (objfile));
5298 return NULL;
5299 }
5300 const mapped_debug_names::index_val &indexval = indexval_it->second;
5301 enum class symbol_linkage {
5302 unknown,
5303 static_,
5304 extern_,
5305 } symbol_linkage_ = symbol_linkage::unknown;
5306 dwarf2_per_cu_data *per_cu = NULL;
5307 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5308 {
5309 ULONGEST ull;
5310 switch (attr.form)
5311 {
5312 case DW_FORM_implicit_const:
5313 ull = attr.implicit_const;
5314 break;
5315 case DW_FORM_flag_present:
5316 ull = 1;
5317 break;
5318 case DW_FORM_udata:
5319 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5320 m_addr += bytes_read;
5321 break;
5322 default:
5323 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5324 dwarf_form_name (attr.form),
5325 objfile_name (objfile));
5326 return NULL;
5327 }
5328 switch (attr.dw_idx)
5329 {
5330 case DW_IDX_compile_unit:
5331 /* Don't crash on bad data. */
5332 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5333 {
5334 complaint (_(".debug_names entry has bad CU index %s"
5335 " [in module %s]"),
5336 pulongest (ull),
5337 objfile_name (dwarf2_per_objfile->objfile));
5338 continue;
5339 }
5340 per_cu = dwarf2_per_objfile->get_cutu (ull);
5341 break;
5342 case DW_IDX_type_unit:
5343 /* Don't crash on bad data. */
5344 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5345 {
5346 complaint (_(".debug_names entry has bad TU index %s"
5347 " [in module %s]"),
5348 pulongest (ull),
5349 objfile_name (dwarf2_per_objfile->objfile));
5350 continue;
5351 }
5352 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5353 break;
5354 case DW_IDX_GNU_internal:
5355 if (!m_map.augmentation_is_gdb)
5356 break;
5357 symbol_linkage_ = symbol_linkage::static_;
5358 break;
5359 case DW_IDX_GNU_external:
5360 if (!m_map.augmentation_is_gdb)
5361 break;
5362 symbol_linkage_ = symbol_linkage::extern_;
5363 break;
5364 }
5365 }
5366
5367 /* Skip if already read in. */
5368 if (per_cu->v.quick->compunit_symtab)
5369 goto again;
5370
5371 /* Check static vs global. */
5372 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5373 {
5374 const bool want_static = *m_block_index == STATIC_BLOCK;
5375 const bool symbol_is_static =
5376 symbol_linkage_ == symbol_linkage::static_;
5377 if (want_static != symbol_is_static)
5378 goto again;
5379 }
5380
5381 /* Match dw2_symtab_iter_next, symbol_kind
5382 and debug_names::psymbol_tag. */
5383 switch (m_domain)
5384 {
5385 case VAR_DOMAIN:
5386 switch (indexval.dwarf_tag)
5387 {
5388 case DW_TAG_variable:
5389 case DW_TAG_subprogram:
5390 /* Some types are also in VAR_DOMAIN. */
5391 case DW_TAG_typedef:
5392 case DW_TAG_structure_type:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
5398 case STRUCT_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_typedef:
5402 case DW_TAG_structure_type:
5403 break;
5404 default:
5405 goto again;
5406 }
5407 break;
5408 case LABEL_DOMAIN:
5409 switch (indexval.dwarf_tag)
5410 {
5411 case 0:
5412 case DW_TAG_variable:
5413 break;
5414 default:
5415 goto again;
5416 }
5417 break;
5418 case MODULE_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_module:
5422 break;
5423 default:
5424 goto again;
5425 }
5426 break;
5427 default:
5428 break;
5429 }
5430
5431 /* Match dw2_expand_symtabs_matching, symbol_kind and
5432 debug_names::psymbol_tag. */
5433 switch (m_search)
5434 {
5435 case VARIABLES_DOMAIN:
5436 switch (indexval.dwarf_tag)
5437 {
5438 case DW_TAG_variable:
5439 break;
5440 default:
5441 goto again;
5442 }
5443 break;
5444 case FUNCTIONS_DOMAIN:
5445 switch (indexval.dwarf_tag)
5446 {
5447 case DW_TAG_subprogram:
5448 break;
5449 default:
5450 goto again;
5451 }
5452 break;
5453 case TYPES_DOMAIN:
5454 switch (indexval.dwarf_tag)
5455 {
5456 case DW_TAG_typedef:
5457 case DW_TAG_structure_type:
5458 break;
5459 default:
5460 goto again;
5461 }
5462 break;
5463 case MODULES_DOMAIN:
5464 switch (indexval.dwarf_tag)
5465 {
5466 case DW_TAG_module:
5467 break;
5468 default:
5469 goto again;
5470 }
5471 default:
5472 break;
5473 }
5474
5475 return per_cu;
5476 }
5477
5478 static struct compunit_symtab *
5479 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5480 const char *name, domain_enum domain)
5481 {
5482 struct dwarf2_per_objfile *dwarf2_per_objfile
5483 = get_dwarf2_per_objfile (objfile);
5484
5485 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5486 if (!mapp)
5487 {
5488 /* index is NULL if OBJF_READNOW. */
5489 return NULL;
5490 }
5491 const auto &map = *mapp;
5492
5493 dw2_debug_names_iterator iter (map, block_index, domain, name);
5494
5495 struct compunit_symtab *stab_best = NULL;
5496 struct dwarf2_per_cu_data *per_cu;
5497 while ((per_cu = iter.next ()) != NULL)
5498 {
5499 struct symbol *sym, *with_opaque = NULL;
5500 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5501 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5502 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5503
5504 sym = block_find_symbol (block, name, domain,
5505 block_find_non_opaque_type_preferred,
5506 &with_opaque);
5507
5508 /* Some caution must be observed with overloaded functions and
5509 methods, since the index will not contain any overload
5510 information (but NAME might contain it). */
5511
5512 if (sym != NULL
5513 && strcmp_iw (sym->search_name (), name) == 0)
5514 return stab;
5515 if (with_opaque != NULL
5516 && strcmp_iw (with_opaque->search_name (), name) == 0)
5517 stab_best = stab;
5518
5519 /* Keep looking through other CUs. */
5520 }
5521
5522 return stab_best;
5523 }
5524
5525 /* This dumps minimal information about .debug_names. It is called
5526 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5527 uses this to verify that .debug_names has been loaded. */
5528
5529 static void
5530 dw2_debug_names_dump (struct objfile *objfile)
5531 {
5532 struct dwarf2_per_objfile *dwarf2_per_objfile
5533 = get_dwarf2_per_objfile (objfile);
5534
5535 gdb_assert (dwarf2_per_objfile->using_index);
5536 printf_filtered (".debug_names:");
5537 if (dwarf2_per_objfile->debug_names_table)
5538 printf_filtered (" exists\n");
5539 else
5540 printf_filtered (" faked for \"readnow\"\n");
5541 printf_filtered ("\n");
5542 }
5543
5544 static void
5545 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5546 const char *func_name)
5547 {
5548 struct dwarf2_per_objfile *dwarf2_per_objfile
5549 = get_dwarf2_per_objfile (objfile);
5550
5551 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5552 if (dwarf2_per_objfile->debug_names_table)
5553 {
5554 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5555
5556 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5557
5558 struct dwarf2_per_cu_data *per_cu;
5559 while ((per_cu = iter.next ()) != NULL)
5560 dw2_instantiate_symtab (per_cu, false);
5561 }
5562 }
5563
5564 static void
5565 dw2_debug_names_map_matching_symbols
5566 (struct objfile *objfile,
5567 const lookup_name_info &name, domain_enum domain,
5568 int global,
5569 gdb::function_view<symbol_found_callback_ftype> callback,
5570 symbol_compare_ftype *ordered_compare)
5571 {
5572 struct dwarf2_per_objfile *dwarf2_per_objfile
5573 = get_dwarf2_per_objfile (objfile);
5574
5575 /* debug_names_table is NULL if OBJF_READNOW. */
5576 if (!dwarf2_per_objfile->debug_names_table)
5577 return;
5578
5579 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5580 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5581
5582 const char *match_name = name.ada ().lookup_name ().c_str ();
5583 auto matcher = [&] (const char *symname)
5584 {
5585 if (ordered_compare == nullptr)
5586 return true;
5587 return ordered_compare (symname, match_name) == 0;
5588 };
5589
5590 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5591 [&] (offset_type namei)
5592 {
5593 /* The name was matched, now expand corresponding CUs that were
5594 marked. */
5595 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5596
5597 struct dwarf2_per_cu_data *per_cu;
5598 while ((per_cu = iter.next ()) != NULL)
5599 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5600 return true;
5601 });
5602
5603 /* It's a shame we couldn't do this inside the
5604 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5605 that have already been expanded. Instead, this loop matches what
5606 the psymtab code does. */
5607 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5608 {
5609 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5610 if (cust != nullptr)
5611 {
5612 const struct block *block
5613 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5614 if (!iterate_over_symbols_terminated (block, name,
5615 domain, callback))
5616 break;
5617 }
5618 }
5619 }
5620
5621 static void
5622 dw2_debug_names_expand_symtabs_matching
5623 (struct objfile *objfile,
5624 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5625 const lookup_name_info &lookup_name,
5626 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5627 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5628 enum search_domain kind)
5629 {
5630 struct dwarf2_per_objfile *dwarf2_per_objfile
5631 = get_dwarf2_per_objfile (objfile);
5632
5633 /* debug_names_table is NULL if OBJF_READNOW. */
5634 if (!dwarf2_per_objfile->debug_names_table)
5635 return;
5636
5637 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5638
5639 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5640
5641 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5642 symbol_matcher,
5643 kind, [&] (offset_type namei)
5644 {
5645 /* The name was matched, now expand corresponding CUs that were
5646 marked. */
5647 dw2_debug_names_iterator iter (map, kind, namei);
5648
5649 struct dwarf2_per_cu_data *per_cu;
5650 while ((per_cu = iter.next ()) != NULL)
5651 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5652 expansion_notify);
5653 return true;
5654 });
5655 }
5656
5657 const struct quick_symbol_functions dwarf2_debug_names_functions =
5658 {
5659 dw2_has_symbols,
5660 dw2_find_last_source_symtab,
5661 dw2_forget_cached_source_info,
5662 dw2_map_symtabs_matching_filename,
5663 dw2_debug_names_lookup_symbol,
5664 dw2_print_stats,
5665 dw2_debug_names_dump,
5666 dw2_debug_names_expand_symtabs_for_function,
5667 dw2_expand_all_symtabs,
5668 dw2_expand_symtabs_with_fullname,
5669 dw2_debug_names_map_matching_symbols,
5670 dw2_debug_names_expand_symtabs_matching,
5671 dw2_find_pc_sect_compunit_symtab,
5672 NULL,
5673 dw2_map_symbol_filenames
5674 };
5675
5676 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5677 to either a dwarf2_per_objfile or dwz_file object. */
5678
5679 template <typename T>
5680 static gdb::array_view<const gdb_byte>
5681 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5682 {
5683 dwarf2_section_info *section = &section_owner->gdb_index;
5684
5685 if (section->empty ())
5686 return {};
5687
5688 /* Older elfutils strip versions could keep the section in the main
5689 executable while splitting it for the separate debug info file. */
5690 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5691 return {};
5692
5693 section->read (obj);
5694
5695 /* dwarf2_section_info::size is a bfd_size_type, while
5696 gdb::array_view works with size_t. On 32-bit hosts, with
5697 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5698 is 32-bit. So we need an explicit narrowing conversion here.
5699 This is fine, because it's impossible to allocate or mmap an
5700 array/buffer larger than what size_t can represent. */
5701 return gdb::make_array_view (section->buffer, section->size);
5702 }
5703
5704 /* Lookup the index cache for the contents of the index associated to
5705 DWARF2_OBJ. */
5706
5707 static gdb::array_view<const gdb_byte>
5708 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5709 {
5710 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5711 if (build_id == nullptr)
5712 return {};
5713
5714 return global_index_cache.lookup_gdb_index (build_id,
5715 &dwarf2_obj->index_cache_res);
5716 }
5717
5718 /* Same as the above, but for DWZ. */
5719
5720 static gdb::array_view<const gdb_byte>
5721 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5722 {
5723 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5724 if (build_id == nullptr)
5725 return {};
5726
5727 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5728 }
5729
5730 /* See symfile.h. */
5731
5732 bool
5733 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5734 {
5735 struct dwarf2_per_objfile *dwarf2_per_objfile
5736 = get_dwarf2_per_objfile (objfile);
5737
5738 /* If we're about to read full symbols, don't bother with the
5739 indices. In this case we also don't care if some other debug
5740 format is making psymtabs, because they are all about to be
5741 expanded anyway. */
5742 if ((objfile->flags & OBJF_READNOW))
5743 {
5744 dwarf2_per_objfile->using_index = 1;
5745 create_all_comp_units (dwarf2_per_objfile);
5746 create_all_type_units (dwarf2_per_objfile);
5747 dwarf2_per_objfile->quick_file_names_table
5748 = create_quick_file_names_table
5749 (dwarf2_per_objfile->all_comp_units.size ());
5750
5751 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5752 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5753 {
5754 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5755
5756 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5757 struct dwarf2_per_cu_quick_data);
5758 }
5759
5760 /* Return 1 so that gdb sees the "quick" functions. However,
5761 these functions will be no-ops because we will have expanded
5762 all symtabs. */
5763 *index_kind = dw_index_kind::GDB_INDEX;
5764 return true;
5765 }
5766
5767 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5768 {
5769 *index_kind = dw_index_kind::DEBUG_NAMES;
5770 return true;
5771 }
5772
5773 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5774 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5775 get_gdb_index_contents_from_section<dwz_file>))
5776 {
5777 *index_kind = dw_index_kind::GDB_INDEX;
5778 return true;
5779 }
5780
5781 /* ... otherwise, try to find the index in the index cache. */
5782 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5783 get_gdb_index_contents_from_cache,
5784 get_gdb_index_contents_from_cache_dwz))
5785 {
5786 global_index_cache.hit ();
5787 *index_kind = dw_index_kind::GDB_INDEX;
5788 return true;
5789 }
5790
5791 global_index_cache.miss ();
5792 return false;
5793 }
5794
5795 \f
5796
5797 /* Build a partial symbol table. */
5798
5799 void
5800 dwarf2_build_psymtabs (struct objfile *objfile)
5801 {
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
5804
5805 init_psymbol_list (objfile, 1024);
5806
5807 try
5808 {
5809 /* This isn't really ideal: all the data we allocate on the
5810 objfile's obstack is still uselessly kept around. However,
5811 freeing it seems unsafe. */
5812 psymtab_discarder psymtabs (objfile);
5813 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5814 psymtabs.keep ();
5815
5816 /* (maybe) store an index in the cache. */
5817 global_index_cache.store (dwarf2_per_objfile);
5818 }
5819 catch (const gdb_exception_error &except)
5820 {
5821 exception_print (gdb_stderr, except);
5822 }
5823 }
5824
5825 /* Find the base address of the compilation unit for range lists and
5826 location lists. It will normally be specified by DW_AT_low_pc.
5827 In DWARF-3 draft 4, the base address could be overridden by
5828 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5829 compilation units with discontinuous ranges. */
5830
5831 static void
5832 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5833 {
5834 struct attribute *attr;
5835
5836 cu->base_known = 0;
5837 cu->base_address = 0;
5838
5839 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5840 if (attr != nullptr)
5841 {
5842 cu->base_address = attr->value_as_address ();
5843 cu->base_known = 1;
5844 }
5845 else
5846 {
5847 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5848 if (attr != nullptr)
5849 {
5850 cu->base_address = attr->value_as_address ();
5851 cu->base_known = 1;
5852 }
5853 }
5854 }
5855
5856 /* Helper function that returns the proper abbrev section for
5857 THIS_CU. */
5858
5859 static struct dwarf2_section_info *
5860 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5861 {
5862 struct dwarf2_section_info *abbrev;
5863 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5864
5865 if (this_cu->is_dwz)
5866 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5867 else
5868 abbrev = &dwarf2_per_objfile->abbrev;
5869
5870 return abbrev;
5871 }
5872
5873 /* Fetch the abbreviation table offset from a comp or type unit header. */
5874
5875 static sect_offset
5876 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5877 struct dwarf2_section_info *section,
5878 sect_offset sect_off)
5879 {
5880 bfd *abfd = section->get_bfd_owner ();
5881 const gdb_byte *info_ptr;
5882 unsigned int initial_length_size, offset_size;
5883 uint16_t version;
5884
5885 section->read (dwarf2_per_objfile->objfile);
5886 info_ptr = section->buffer + to_underlying (sect_off);
5887 read_initial_length (abfd, info_ptr, &initial_length_size);
5888 offset_size = initial_length_size == 4 ? 4 : 8;
5889 info_ptr += initial_length_size;
5890
5891 version = read_2_bytes (abfd, info_ptr);
5892 info_ptr += 2;
5893 if (version >= 5)
5894 {
5895 /* Skip unit type and address size. */
5896 info_ptr += 2;
5897 }
5898
5899 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5900 }
5901
5902 /* Allocate a new partial symtab for file named NAME and mark this new
5903 partial symtab as being an include of PST. */
5904
5905 static void
5906 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5907 struct objfile *objfile)
5908 {
5909 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5910
5911 if (!IS_ABSOLUTE_PATH (subpst->filename))
5912 {
5913 /* It shares objfile->objfile_obstack. */
5914 subpst->dirname = pst->dirname;
5915 }
5916
5917 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5918 subpst->dependencies[0] = pst;
5919 subpst->number_of_dependencies = 1;
5920
5921 /* No private part is necessary for include psymtabs. This property
5922 can be used to differentiate between such include psymtabs and
5923 the regular ones. */
5924 subpst->per_cu_data = nullptr;
5925 }
5926
5927 /* Read the Line Number Program data and extract the list of files
5928 included by the source file represented by PST. Build an include
5929 partial symtab for each of these included files. */
5930
5931 static void
5932 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5933 struct die_info *die,
5934 dwarf2_psymtab *pst)
5935 {
5936 line_header_up lh;
5937 struct attribute *attr;
5938
5939 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5940 if (attr != nullptr)
5941 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5942 if (lh == NULL)
5943 return; /* No linetable, so no includes. */
5944
5945 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5946 that we pass in the raw text_low here; that is ok because we're
5947 only decoding the line table to make include partial symtabs, and
5948 so the addresses aren't really used. */
5949 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5950 pst->raw_text_low (), 1);
5951 }
5952
5953 static hashval_t
5954 hash_signatured_type (const void *item)
5955 {
5956 const struct signatured_type *sig_type
5957 = (const struct signatured_type *) item;
5958
5959 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5960 return sig_type->signature;
5961 }
5962
5963 static int
5964 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5965 {
5966 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5967 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5968
5969 return lhs->signature == rhs->signature;
5970 }
5971
5972 /* Allocate a hash table for signatured types. */
5973
5974 static htab_up
5975 allocate_signatured_type_table (struct objfile *objfile)
5976 {
5977 return htab_up (htab_create_alloc (41,
5978 hash_signatured_type,
5979 eq_signatured_type,
5980 NULL, xcalloc, xfree));
5981 }
5982
5983 /* A helper function to add a signatured type CU to a table. */
5984
5985 static int
5986 add_signatured_type_cu_to_table (void **slot, void *datum)
5987 {
5988 struct signatured_type *sigt = (struct signatured_type *) *slot;
5989 std::vector<signatured_type *> *all_type_units
5990 = (std::vector<signatured_type *> *) datum;
5991
5992 all_type_units->push_back (sigt);
5993
5994 return 1;
5995 }
5996
5997 /* A helper for create_debug_types_hash_table. Read types from SECTION
5998 and fill them into TYPES_HTAB. It will process only type units,
5999 therefore DW_UT_type. */
6000
6001 static void
6002 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6003 struct dwo_file *dwo_file,
6004 dwarf2_section_info *section, htab_up &types_htab,
6005 rcuh_kind section_kind)
6006 {
6007 struct objfile *objfile = dwarf2_per_objfile->objfile;
6008 struct dwarf2_section_info *abbrev_section;
6009 bfd *abfd;
6010 const gdb_byte *info_ptr, *end_ptr;
6011
6012 abbrev_section = (dwo_file != NULL
6013 ? &dwo_file->sections.abbrev
6014 : &dwarf2_per_objfile->abbrev);
6015
6016 if (dwarf_read_debug)
6017 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6018 section->get_name (),
6019 abbrev_section->get_file_name ());
6020
6021 section->read (objfile);
6022 info_ptr = section->buffer;
6023
6024 if (info_ptr == NULL)
6025 return;
6026
6027 /* We can't set abfd until now because the section may be empty or
6028 not present, in which case the bfd is unknown. */
6029 abfd = section->get_bfd_owner ();
6030
6031 /* We don't use cutu_reader here because we don't need to read
6032 any dies: the signature is in the header. */
6033
6034 end_ptr = info_ptr + section->size;
6035 while (info_ptr < end_ptr)
6036 {
6037 struct signatured_type *sig_type;
6038 struct dwo_unit *dwo_tu;
6039 void **slot;
6040 const gdb_byte *ptr = info_ptr;
6041 struct comp_unit_head header;
6042 unsigned int length;
6043
6044 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6045
6046 /* Initialize it due to a false compiler warning. */
6047 header.signature = -1;
6048 header.type_cu_offset_in_tu = (cu_offset) -1;
6049
6050 /* We need to read the type's signature in order to build the hash
6051 table, but we don't need anything else just yet. */
6052
6053 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6054 abbrev_section, ptr, section_kind);
6055
6056 length = header.get_length ();
6057
6058 /* Skip dummy type units. */
6059 if (ptr >= info_ptr + length
6060 || peek_abbrev_code (abfd, ptr) == 0
6061 || header.unit_type != DW_UT_type)
6062 {
6063 info_ptr += length;
6064 continue;
6065 }
6066
6067 if (types_htab == NULL)
6068 {
6069 if (dwo_file)
6070 types_htab = allocate_dwo_unit_table (objfile);
6071 else
6072 types_htab = allocate_signatured_type_table (objfile);
6073 }
6074
6075 if (dwo_file)
6076 {
6077 sig_type = NULL;
6078 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6079 struct dwo_unit);
6080 dwo_tu->dwo_file = dwo_file;
6081 dwo_tu->signature = header.signature;
6082 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6083 dwo_tu->section = section;
6084 dwo_tu->sect_off = sect_off;
6085 dwo_tu->length = length;
6086 }
6087 else
6088 {
6089 /* N.B.: type_offset is not usable if this type uses a DWO file.
6090 The real type_offset is in the DWO file. */
6091 dwo_tu = NULL;
6092 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6093 struct signatured_type);
6094 sig_type->signature = header.signature;
6095 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6096 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6097 sig_type->per_cu.is_debug_types = 1;
6098 sig_type->per_cu.section = section;
6099 sig_type->per_cu.sect_off = sect_off;
6100 sig_type->per_cu.length = length;
6101 }
6102
6103 slot = htab_find_slot (types_htab.get (),
6104 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6105 INSERT);
6106 gdb_assert (slot != NULL);
6107 if (*slot != NULL)
6108 {
6109 sect_offset dup_sect_off;
6110
6111 if (dwo_file)
6112 {
6113 const struct dwo_unit *dup_tu
6114 = (const struct dwo_unit *) *slot;
6115
6116 dup_sect_off = dup_tu->sect_off;
6117 }
6118 else
6119 {
6120 const struct signatured_type *dup_tu
6121 = (const struct signatured_type *) *slot;
6122
6123 dup_sect_off = dup_tu->per_cu.sect_off;
6124 }
6125
6126 complaint (_("debug type entry at offset %s is duplicate to"
6127 " the entry at offset %s, signature %s"),
6128 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6129 hex_string (header.signature));
6130 }
6131 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6132
6133 if (dwarf_read_debug > 1)
6134 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6135 sect_offset_str (sect_off),
6136 hex_string (header.signature));
6137
6138 info_ptr += length;
6139 }
6140 }
6141
6142 /* Create the hash table of all entries in the .debug_types
6143 (or .debug_types.dwo) section(s).
6144 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6145 otherwise it is NULL.
6146
6147 The result is a pointer to the hash table or NULL if there are no types.
6148
6149 Note: This function processes DWO files only, not DWP files. */
6150
6151 static void
6152 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6153 struct dwo_file *dwo_file,
6154 gdb::array_view<dwarf2_section_info> type_sections,
6155 htab_up &types_htab)
6156 {
6157 for (dwarf2_section_info &section : type_sections)
6158 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6159 types_htab, rcuh_kind::TYPE);
6160 }
6161
6162 /* Create the hash table of all entries in the .debug_types section,
6163 and initialize all_type_units.
6164 The result is zero if there is an error (e.g. missing .debug_types section),
6165 otherwise non-zero. */
6166
6167 static int
6168 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6169 {
6170 htab_up types_htab;
6171
6172 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6173 &dwarf2_per_objfile->info, types_htab,
6174 rcuh_kind::COMPILE);
6175 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6176 dwarf2_per_objfile->types, types_htab);
6177 if (types_htab == NULL)
6178 {
6179 dwarf2_per_objfile->signatured_types = NULL;
6180 return 0;
6181 }
6182
6183 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6184
6185 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6186 dwarf2_per_objfile->all_type_units.reserve
6187 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6188
6189 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6190 add_signatured_type_cu_to_table,
6191 &dwarf2_per_objfile->all_type_units);
6192
6193 return 1;
6194 }
6195
6196 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6197 If SLOT is non-NULL, it is the entry to use in the hash table.
6198 Otherwise we find one. */
6199
6200 static struct signatured_type *
6201 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6202 void **slot)
6203 {
6204 struct objfile *objfile = dwarf2_per_objfile->objfile;
6205
6206 if (dwarf2_per_objfile->all_type_units.size ()
6207 == dwarf2_per_objfile->all_type_units.capacity ())
6208 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6209
6210 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6211 struct signatured_type);
6212
6213 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6214 sig_type->signature = sig;
6215 sig_type->per_cu.is_debug_types = 1;
6216 if (dwarf2_per_objfile->using_index)
6217 {
6218 sig_type->per_cu.v.quick =
6219 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6220 struct dwarf2_per_cu_quick_data);
6221 }
6222
6223 if (slot == NULL)
6224 {
6225 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6226 sig_type, INSERT);
6227 }
6228 gdb_assert (*slot == NULL);
6229 *slot = sig_type;
6230 /* The rest of sig_type must be filled in by the caller. */
6231 return sig_type;
6232 }
6233
6234 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6235 Fill in SIG_ENTRY with DWO_ENTRY. */
6236
6237 static void
6238 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6239 struct signatured_type *sig_entry,
6240 struct dwo_unit *dwo_entry)
6241 {
6242 /* Make sure we're not clobbering something we don't expect to. */
6243 gdb_assert (! sig_entry->per_cu.queued);
6244 gdb_assert (sig_entry->per_cu.cu == NULL);
6245 if (dwarf2_per_objfile->using_index)
6246 {
6247 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6248 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6249 }
6250 else
6251 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6252 gdb_assert (sig_entry->signature == dwo_entry->signature);
6253 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6254 gdb_assert (sig_entry->type_unit_group == NULL);
6255 gdb_assert (sig_entry->dwo_unit == NULL);
6256
6257 sig_entry->per_cu.section = dwo_entry->section;
6258 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6259 sig_entry->per_cu.length = dwo_entry->length;
6260 sig_entry->per_cu.reading_dwo_directly = 1;
6261 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6262 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6263 sig_entry->dwo_unit = dwo_entry;
6264 }
6265
6266 /* Subroutine of lookup_signatured_type.
6267 If we haven't read the TU yet, create the signatured_type data structure
6268 for a TU to be read in directly from a DWO file, bypassing the stub.
6269 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6270 using .gdb_index, then when reading a CU we want to stay in the DWO file
6271 containing that CU. Otherwise we could end up reading several other DWO
6272 files (due to comdat folding) to process the transitive closure of all the
6273 mentioned TUs, and that can be slow. The current DWO file will have every
6274 type signature that it needs.
6275 We only do this for .gdb_index because in the psymtab case we already have
6276 to read all the DWOs to build the type unit groups. */
6277
6278 static struct signatured_type *
6279 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6280 {
6281 struct dwarf2_per_objfile *dwarf2_per_objfile
6282 = cu->per_cu->dwarf2_per_objfile;
6283 struct objfile *objfile = dwarf2_per_objfile->objfile;
6284 struct dwo_file *dwo_file;
6285 struct dwo_unit find_dwo_entry, *dwo_entry;
6286 struct signatured_type find_sig_entry, *sig_entry;
6287 void **slot;
6288
6289 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6290
6291 /* If TU skeletons have been removed then we may not have read in any
6292 TUs yet. */
6293 if (dwarf2_per_objfile->signatured_types == NULL)
6294 {
6295 dwarf2_per_objfile->signatured_types
6296 = allocate_signatured_type_table (objfile);
6297 }
6298
6299 /* We only ever need to read in one copy of a signatured type.
6300 Use the global signatured_types array to do our own comdat-folding
6301 of types. If this is the first time we're reading this TU, and
6302 the TU has an entry in .gdb_index, replace the recorded data from
6303 .gdb_index with this TU. */
6304
6305 find_sig_entry.signature = sig;
6306 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6307 &find_sig_entry, INSERT);
6308 sig_entry = (struct signatured_type *) *slot;
6309
6310 /* We can get here with the TU already read, *or* in the process of being
6311 read. Don't reassign the global entry to point to this DWO if that's
6312 the case. Also note that if the TU is already being read, it may not
6313 have come from a DWO, the program may be a mix of Fission-compiled
6314 code and non-Fission-compiled code. */
6315
6316 /* Have we already tried to read this TU?
6317 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6318 needn't exist in the global table yet). */
6319 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6320 return sig_entry;
6321
6322 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6323 dwo_unit of the TU itself. */
6324 dwo_file = cu->dwo_unit->dwo_file;
6325
6326 /* Ok, this is the first time we're reading this TU. */
6327 if (dwo_file->tus == NULL)
6328 return NULL;
6329 find_dwo_entry.signature = sig;
6330 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6331 &find_dwo_entry);
6332 if (dwo_entry == NULL)
6333 return NULL;
6334
6335 /* If the global table doesn't have an entry for this TU, add one. */
6336 if (sig_entry == NULL)
6337 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6338
6339 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6340 sig_entry->per_cu.tu_read = 1;
6341 return sig_entry;
6342 }
6343
6344 /* Subroutine of lookup_signatured_type.
6345 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6346 then try the DWP file. If the TU stub (skeleton) has been removed then
6347 it won't be in .gdb_index. */
6348
6349 static struct signatured_type *
6350 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6351 {
6352 struct dwarf2_per_objfile *dwarf2_per_objfile
6353 = cu->per_cu->dwarf2_per_objfile;
6354 struct objfile *objfile = dwarf2_per_objfile->objfile;
6355 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6356 struct dwo_unit *dwo_entry;
6357 struct signatured_type find_sig_entry, *sig_entry;
6358 void **slot;
6359
6360 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6361 gdb_assert (dwp_file != NULL);
6362
6363 /* If TU skeletons have been removed then we may not have read in any
6364 TUs yet. */
6365 if (dwarf2_per_objfile->signatured_types == NULL)
6366 {
6367 dwarf2_per_objfile->signatured_types
6368 = allocate_signatured_type_table (objfile);
6369 }
6370
6371 find_sig_entry.signature = sig;
6372 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6373 &find_sig_entry, INSERT);
6374 sig_entry = (struct signatured_type *) *slot;
6375
6376 /* Have we already tried to read this TU?
6377 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6378 needn't exist in the global table yet). */
6379 if (sig_entry != NULL)
6380 return sig_entry;
6381
6382 if (dwp_file->tus == NULL)
6383 return NULL;
6384 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6385 sig, 1 /* is_debug_types */);
6386 if (dwo_entry == NULL)
6387 return NULL;
6388
6389 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6391
6392 return sig_entry;
6393 }
6394
6395 /* Lookup a signature based type for DW_FORM_ref_sig8.
6396 Returns NULL if signature SIG is not present in the table.
6397 It is up to the caller to complain about this. */
6398
6399 static struct signatured_type *
6400 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6401 {
6402 struct dwarf2_per_objfile *dwarf2_per_objfile
6403 = cu->per_cu->dwarf2_per_objfile;
6404
6405 if (cu->dwo_unit
6406 && dwarf2_per_objfile->using_index)
6407 {
6408 /* We're in a DWO/DWP file, and we're using .gdb_index.
6409 These cases require special processing. */
6410 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6411 return lookup_dwo_signatured_type (cu, sig);
6412 else
6413 return lookup_dwp_signatured_type (cu, sig);
6414 }
6415 else
6416 {
6417 struct signatured_type find_entry, *entry;
6418
6419 if (dwarf2_per_objfile->signatured_types == NULL)
6420 return NULL;
6421 find_entry.signature = sig;
6422 entry = ((struct signatured_type *)
6423 htab_find (dwarf2_per_objfile->signatured_types.get (),
6424 &find_entry));
6425 return entry;
6426 }
6427 }
6428
6429 /* Return the address base of the compile unit, which, if exists, is stored
6430 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6431 static gdb::optional<ULONGEST>
6432 lookup_addr_base (struct die_info *comp_unit_die)
6433 {
6434 struct attribute *attr;
6435 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6436 if (attr == nullptr)
6437 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6438 if (attr == nullptr)
6439 return gdb::optional<ULONGEST> ();
6440 return DW_UNSND (attr);
6441 }
6442
6443 /* Return range lists base of the compile unit, which, if exists, is stored
6444 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6445 static ULONGEST
6446 lookup_ranges_base (struct die_info *comp_unit_die)
6447 {
6448 struct attribute *attr;
6449 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6450 if (attr == nullptr)
6451 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6452 if (attr == nullptr)
6453 return 0;
6454 return DW_UNSND (attr);
6455 }
6456
6457 /* Low level DIE reading support. */
6458
6459 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6460
6461 static void
6462 init_cu_die_reader (struct die_reader_specs *reader,
6463 struct dwarf2_cu *cu,
6464 struct dwarf2_section_info *section,
6465 struct dwo_file *dwo_file,
6466 struct abbrev_table *abbrev_table)
6467 {
6468 gdb_assert (section->readin && section->buffer != NULL);
6469 reader->abfd = section->get_bfd_owner ();
6470 reader->cu = cu;
6471 reader->dwo_file = dwo_file;
6472 reader->die_section = section;
6473 reader->buffer = section->buffer;
6474 reader->buffer_end = section->buffer + section->size;
6475 reader->abbrev_table = abbrev_table;
6476 }
6477
6478 /* Subroutine of cutu_reader to simplify it.
6479 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6480 There's just a lot of work to do, and cutu_reader is big enough
6481 already.
6482
6483 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6484 from it to the DIE in the DWO. If NULL we are skipping the stub.
6485 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6486 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6487 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6488 STUB_COMP_DIR may be non-NULL.
6489 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6490 are filled in with the info of the DIE from the DWO file.
6491 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6492 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6493 kept around for at least as long as *RESULT_READER.
6494
6495 The result is non-zero if a valid (non-dummy) DIE was found. */
6496
6497 static int
6498 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6499 struct dwo_unit *dwo_unit,
6500 struct die_info *stub_comp_unit_die,
6501 const char *stub_comp_dir,
6502 struct die_reader_specs *result_reader,
6503 const gdb_byte **result_info_ptr,
6504 struct die_info **result_comp_unit_die,
6505 abbrev_table_up *result_dwo_abbrev_table)
6506 {
6507 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6508 struct objfile *objfile = dwarf2_per_objfile->objfile;
6509 struct dwarf2_cu *cu = this_cu->cu;
6510 bfd *abfd;
6511 const gdb_byte *begin_info_ptr, *info_ptr;
6512 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6513 int i,num_extra_attrs;
6514 struct dwarf2_section_info *dwo_abbrev_section;
6515 struct die_info *comp_unit_die;
6516
6517 /* At most one of these may be provided. */
6518 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6519
6520 /* These attributes aren't processed until later:
6521 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6522 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6523 referenced later. However, these attributes are found in the stub
6524 which we won't have later. In order to not impose this complication
6525 on the rest of the code, we read them here and copy them to the
6526 DWO CU/TU die. */
6527
6528 stmt_list = NULL;
6529 low_pc = NULL;
6530 high_pc = NULL;
6531 ranges = NULL;
6532 comp_dir = NULL;
6533
6534 if (stub_comp_unit_die != NULL)
6535 {
6536 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6537 DWO file. */
6538 if (! this_cu->is_debug_types)
6539 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6540 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6541 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6542 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6543 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6544
6545 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6546
6547 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6548 here (if needed). We need the value before we can process
6549 DW_AT_ranges. */
6550 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6551 }
6552 else if (stub_comp_dir != NULL)
6553 {
6554 /* Reconstruct the comp_dir attribute to simplify the code below. */
6555 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6556 comp_dir->name = DW_AT_comp_dir;
6557 comp_dir->form = DW_FORM_string;
6558 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6559 DW_STRING (comp_dir) = stub_comp_dir;
6560 }
6561
6562 /* Set up for reading the DWO CU/TU. */
6563 cu->dwo_unit = dwo_unit;
6564 dwarf2_section_info *section = dwo_unit->section;
6565 section->read (objfile);
6566 abfd = section->get_bfd_owner ();
6567 begin_info_ptr = info_ptr = (section->buffer
6568 + to_underlying (dwo_unit->sect_off));
6569 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6570
6571 if (this_cu->is_debug_types)
6572 {
6573 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6574
6575 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6576 &cu->header, section,
6577 dwo_abbrev_section,
6578 info_ptr, rcuh_kind::TYPE);
6579 /* This is not an assert because it can be caused by bad debug info. */
6580 if (sig_type->signature != cu->header.signature)
6581 {
6582 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6583 " TU at offset %s [in module %s]"),
6584 hex_string (sig_type->signature),
6585 hex_string (cu->header.signature),
6586 sect_offset_str (dwo_unit->sect_off),
6587 bfd_get_filename (abfd));
6588 }
6589 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6590 /* For DWOs coming from DWP files, we don't know the CU length
6591 nor the type's offset in the TU until now. */
6592 dwo_unit->length = cu->header.get_length ();
6593 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6594
6595 /* Establish the type offset that can be used to lookup the type.
6596 For DWO files, we don't know it until now. */
6597 sig_type->type_offset_in_section
6598 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6599 }
6600 else
6601 {
6602 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6603 &cu->header, section,
6604 dwo_abbrev_section,
6605 info_ptr, rcuh_kind::COMPILE);
6606 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6607 /* For DWOs coming from DWP files, we don't know the CU length
6608 until now. */
6609 dwo_unit->length = cu->header.get_length ();
6610 }
6611
6612 *result_dwo_abbrev_table
6613 = abbrev_table::read (objfile, dwo_abbrev_section,
6614 cu->header.abbrev_sect_off);
6615 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6616 result_dwo_abbrev_table->get ());
6617
6618 /* Read in the die, but leave space to copy over the attributes
6619 from the stub. This has the benefit of simplifying the rest of
6620 the code - all the work to maintain the illusion of a single
6621 DW_TAG_{compile,type}_unit DIE is done here. */
6622 num_extra_attrs = ((stmt_list != NULL)
6623 + (low_pc != NULL)
6624 + (high_pc != NULL)
6625 + (ranges != NULL)
6626 + (comp_dir != NULL));
6627 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6628 num_extra_attrs);
6629
6630 /* Copy over the attributes from the stub to the DIE we just read in. */
6631 comp_unit_die = *result_comp_unit_die;
6632 i = comp_unit_die->num_attrs;
6633 if (stmt_list != NULL)
6634 comp_unit_die->attrs[i++] = *stmt_list;
6635 if (low_pc != NULL)
6636 comp_unit_die->attrs[i++] = *low_pc;
6637 if (high_pc != NULL)
6638 comp_unit_die->attrs[i++] = *high_pc;
6639 if (ranges != NULL)
6640 comp_unit_die->attrs[i++] = *ranges;
6641 if (comp_dir != NULL)
6642 comp_unit_die->attrs[i++] = *comp_dir;
6643 comp_unit_die->num_attrs += num_extra_attrs;
6644
6645 if (dwarf_die_debug)
6646 {
6647 fprintf_unfiltered (gdb_stdlog,
6648 "Read die from %s@0x%x of %s:\n",
6649 section->get_name (),
6650 (unsigned) (begin_info_ptr - section->buffer),
6651 bfd_get_filename (abfd));
6652 dump_die (comp_unit_die, dwarf_die_debug);
6653 }
6654
6655 /* Skip dummy compilation units. */
6656 if (info_ptr >= begin_info_ptr + dwo_unit->length
6657 || peek_abbrev_code (abfd, info_ptr) == 0)
6658 return 0;
6659
6660 *result_info_ptr = info_ptr;
6661 return 1;
6662 }
6663
6664 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6665 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6666 signature is part of the header. */
6667 static gdb::optional<ULONGEST>
6668 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6669 {
6670 if (cu->header.version >= 5)
6671 return cu->header.signature;
6672 struct attribute *attr;
6673 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6674 if (attr == nullptr)
6675 return gdb::optional<ULONGEST> ();
6676 return DW_UNSND (attr);
6677 }
6678
6679 /* Subroutine of cutu_reader to simplify it.
6680 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6681 Returns NULL if the specified DWO unit cannot be found. */
6682
6683 static struct dwo_unit *
6684 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6685 struct die_info *comp_unit_die,
6686 const char *dwo_name)
6687 {
6688 struct dwarf2_cu *cu = this_cu->cu;
6689 struct dwo_unit *dwo_unit;
6690 const char *comp_dir;
6691
6692 gdb_assert (cu != NULL);
6693
6694 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6695 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6696 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6697
6698 if (this_cu->is_debug_types)
6699 {
6700 struct signatured_type *sig_type;
6701
6702 /* Since this_cu is the first member of struct signatured_type,
6703 we can go from a pointer to one to a pointer to the other. */
6704 sig_type = (struct signatured_type *) this_cu;
6705 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6706 }
6707 else
6708 {
6709 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6710 if (!signature.has_value ())
6711 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6712 " [in module %s]"),
6713 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6714 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6715 *signature);
6716 }
6717
6718 return dwo_unit;
6719 }
6720
6721 /* Subroutine of cutu_reader to simplify it.
6722 See it for a description of the parameters.
6723 Read a TU directly from a DWO file, bypassing the stub. */
6724
6725 void
6726 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6727 int use_existing_cu)
6728 {
6729 struct signatured_type *sig_type;
6730 struct die_reader_specs reader;
6731
6732 /* Verify we can do the following downcast, and that we have the
6733 data we need. */
6734 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6735 sig_type = (struct signatured_type *) this_cu;
6736 gdb_assert (sig_type->dwo_unit != NULL);
6737
6738 if (use_existing_cu && this_cu->cu != NULL)
6739 {
6740 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6741 /* There's no need to do the rereading_dwo_cu handling that
6742 cutu_reader does since we don't read the stub. */
6743 }
6744 else
6745 {
6746 /* If !use_existing_cu, this_cu->cu must be NULL. */
6747 gdb_assert (this_cu->cu == NULL);
6748 m_new_cu.reset (new dwarf2_cu (this_cu));
6749 }
6750
6751 /* A future optimization, if needed, would be to use an existing
6752 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6753 could share abbrev tables. */
6754
6755 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6756 NULL /* stub_comp_unit_die */,
6757 sig_type->dwo_unit->dwo_file->comp_dir,
6758 &reader, &info_ptr,
6759 &comp_unit_die,
6760 &m_dwo_abbrev_table) == 0)
6761 {
6762 /* Dummy die. */
6763 dummy_p = true;
6764 }
6765 }
6766
6767 /* Initialize a CU (or TU) and read its DIEs.
6768 If the CU defers to a DWO file, read the DWO file as well.
6769
6770 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6771 Otherwise the table specified in the comp unit header is read in and used.
6772 This is an optimization for when we already have the abbrev table.
6773
6774 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6775 Otherwise, a new CU is allocated with xmalloc. */
6776
6777 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6778 struct abbrev_table *abbrev_table,
6779 int use_existing_cu,
6780 bool skip_partial)
6781 : die_reader_specs {},
6782 m_this_cu (this_cu)
6783 {
6784 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6785 struct objfile *objfile = dwarf2_per_objfile->objfile;
6786 struct dwarf2_section_info *section = this_cu->section;
6787 bfd *abfd = section->get_bfd_owner ();
6788 struct dwarf2_cu *cu;
6789 const gdb_byte *begin_info_ptr;
6790 struct signatured_type *sig_type = NULL;
6791 struct dwarf2_section_info *abbrev_section;
6792 /* Non-zero if CU currently points to a DWO file and we need to
6793 reread it. When this happens we need to reread the skeleton die
6794 before we can reread the DWO file (this only applies to CUs, not TUs). */
6795 int rereading_dwo_cu = 0;
6796
6797 if (dwarf_die_debug)
6798 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6799 this_cu->is_debug_types ? "type" : "comp",
6800 sect_offset_str (this_cu->sect_off));
6801
6802 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6803 file (instead of going through the stub), short-circuit all of this. */
6804 if (this_cu->reading_dwo_directly)
6805 {
6806 /* Narrow down the scope of possibilities to have to understand. */
6807 gdb_assert (this_cu->is_debug_types);
6808 gdb_assert (abbrev_table == NULL);
6809 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6810 return;
6811 }
6812
6813 /* This is cheap if the section is already read in. */
6814 section->read (objfile);
6815
6816 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6817
6818 abbrev_section = get_abbrev_section_for_cu (this_cu);
6819
6820 if (use_existing_cu && this_cu->cu != NULL)
6821 {
6822 cu = this_cu->cu;
6823 /* If this CU is from a DWO file we need to start over, we need to
6824 refetch the attributes from the skeleton CU.
6825 This could be optimized by retrieving those attributes from when we
6826 were here the first time: the previous comp_unit_die was stored in
6827 comp_unit_obstack. But there's no data yet that we need this
6828 optimization. */
6829 if (cu->dwo_unit != NULL)
6830 rereading_dwo_cu = 1;
6831 }
6832 else
6833 {
6834 /* If !use_existing_cu, this_cu->cu must be NULL. */
6835 gdb_assert (this_cu->cu == NULL);
6836 m_new_cu.reset (new dwarf2_cu (this_cu));
6837 cu = m_new_cu.get ();
6838 }
6839
6840 /* Get the header. */
6841 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6842 {
6843 /* We already have the header, there's no need to read it in again. */
6844 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6845 }
6846 else
6847 {
6848 if (this_cu->is_debug_types)
6849 {
6850 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6851 &cu->header, section,
6852 abbrev_section, info_ptr,
6853 rcuh_kind::TYPE);
6854
6855 /* Since per_cu is the first member of struct signatured_type,
6856 we can go from a pointer to one to a pointer to the other. */
6857 sig_type = (struct signatured_type *) this_cu;
6858 gdb_assert (sig_type->signature == cu->header.signature);
6859 gdb_assert (sig_type->type_offset_in_tu
6860 == cu->header.type_cu_offset_in_tu);
6861 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6862
6863 /* LENGTH has not been set yet for type units if we're
6864 using .gdb_index. */
6865 this_cu->length = cu->header.get_length ();
6866
6867 /* Establish the type offset that can be used to lookup the type. */
6868 sig_type->type_offset_in_section =
6869 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6870
6871 this_cu->dwarf_version = cu->header.version;
6872 }
6873 else
6874 {
6875 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6876 &cu->header, section,
6877 abbrev_section,
6878 info_ptr,
6879 rcuh_kind::COMPILE);
6880
6881 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6882 gdb_assert (this_cu->length == cu->header.get_length ());
6883 this_cu->dwarf_version = cu->header.version;
6884 }
6885 }
6886
6887 /* Skip dummy compilation units. */
6888 if (info_ptr >= begin_info_ptr + this_cu->length
6889 || peek_abbrev_code (abfd, info_ptr) == 0)
6890 {
6891 dummy_p = true;
6892 return;
6893 }
6894
6895 /* If we don't have them yet, read the abbrevs for this compilation unit.
6896 And if we need to read them now, make sure they're freed when we're
6897 done. */
6898 if (abbrev_table != NULL)
6899 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6900 else
6901 {
6902 m_abbrev_table_holder
6903 = abbrev_table::read (objfile, abbrev_section,
6904 cu->header.abbrev_sect_off);
6905 abbrev_table = m_abbrev_table_holder.get ();
6906 }
6907
6908 /* Read the top level CU/TU die. */
6909 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6910 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6911
6912 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6913 {
6914 dummy_p = true;
6915 return;
6916 }
6917
6918 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6919 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6920 table from the DWO file and pass the ownership over to us. It will be
6921 referenced from READER, so we must make sure to free it after we're done
6922 with READER.
6923
6924 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6925 DWO CU, that this test will fail (the attribute will not be present). */
6926 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6927 if (dwo_name != nullptr)
6928 {
6929 struct dwo_unit *dwo_unit;
6930 struct die_info *dwo_comp_unit_die;
6931
6932 if (comp_unit_die->has_children)
6933 {
6934 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6935 " has children (offset %s) [in module %s]"),
6936 sect_offset_str (this_cu->sect_off),
6937 bfd_get_filename (abfd));
6938 }
6939 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6940 if (dwo_unit != NULL)
6941 {
6942 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6943 comp_unit_die, NULL,
6944 this, &info_ptr,
6945 &dwo_comp_unit_die,
6946 &m_dwo_abbrev_table) == 0)
6947 {
6948 /* Dummy die. */
6949 dummy_p = true;
6950 return;
6951 }
6952 comp_unit_die = dwo_comp_unit_die;
6953 }
6954 else
6955 {
6956 /* Yikes, we couldn't find the rest of the DIE, we only have
6957 the stub. A complaint has already been logged. There's
6958 not much more we can do except pass on the stub DIE to
6959 die_reader_func. We don't want to throw an error on bad
6960 debug info. */
6961 }
6962 }
6963 }
6964
6965 void
6966 cutu_reader::keep ()
6967 {
6968 /* Done, clean up. */
6969 gdb_assert (!dummy_p);
6970 if (m_new_cu != NULL)
6971 {
6972 struct dwarf2_per_objfile *dwarf2_per_objfile
6973 = m_this_cu->dwarf2_per_objfile;
6974 /* Link this CU into read_in_chain. */
6975 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6976 dwarf2_per_objfile->read_in_chain = m_this_cu;
6977 /* The chain owns it now. */
6978 m_new_cu.release ();
6979 }
6980 }
6981
6982 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6983 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6984 assumed to have already done the lookup to find the DWO file).
6985
6986 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6987 THIS_CU->is_debug_types, but nothing else.
6988
6989 We fill in THIS_CU->length.
6990
6991 THIS_CU->cu is always freed when done.
6992 This is done in order to not leave THIS_CU->cu in a state where we have
6993 to care whether it refers to the "main" CU or the DWO CU.
6994
6995 When parent_cu is passed, it is used to provide a default value for
6996 str_offsets_base and addr_base from the parent. */
6997
6998 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6999 struct dwarf2_cu *parent_cu,
7000 struct dwo_file *dwo_file)
7001 : die_reader_specs {},
7002 m_this_cu (this_cu)
7003 {
7004 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7005 struct objfile *objfile = dwarf2_per_objfile->objfile;
7006 struct dwarf2_section_info *section = this_cu->section;
7007 bfd *abfd = section->get_bfd_owner ();
7008 struct dwarf2_section_info *abbrev_section;
7009 const gdb_byte *begin_info_ptr, *info_ptr;
7010
7011 if (dwarf_die_debug)
7012 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7013 this_cu->is_debug_types ? "type" : "comp",
7014 sect_offset_str (this_cu->sect_off));
7015
7016 gdb_assert (this_cu->cu == NULL);
7017
7018 abbrev_section = (dwo_file != NULL
7019 ? &dwo_file->sections.abbrev
7020 : get_abbrev_section_for_cu (this_cu));
7021
7022 /* This is cheap if the section is already read in. */
7023 section->read (objfile);
7024
7025 m_new_cu.reset (new dwarf2_cu (this_cu));
7026
7027 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7028 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7029 &m_new_cu->header, section,
7030 abbrev_section, info_ptr,
7031 (this_cu->is_debug_types
7032 ? rcuh_kind::TYPE
7033 : rcuh_kind::COMPILE));
7034
7035 if (parent_cu != nullptr)
7036 {
7037 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7038 m_new_cu->addr_base = parent_cu->addr_base;
7039 }
7040 this_cu->length = m_new_cu->header.get_length ();
7041
7042 /* Skip dummy compilation units. */
7043 if (info_ptr >= begin_info_ptr + this_cu->length
7044 || peek_abbrev_code (abfd, info_ptr) == 0)
7045 {
7046 dummy_p = true;
7047 return;
7048 }
7049
7050 m_abbrev_table_holder
7051 = abbrev_table::read (objfile, abbrev_section,
7052 m_new_cu->header.abbrev_sect_off);
7053
7054 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7055 m_abbrev_table_holder.get ());
7056 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7057 }
7058
7059 \f
7060 /* Type Unit Groups.
7061
7062 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7063 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7064 so that all types coming from the same compilation (.o file) are grouped
7065 together. A future step could be to put the types in the same symtab as
7066 the CU the types ultimately came from. */
7067
7068 static hashval_t
7069 hash_type_unit_group (const void *item)
7070 {
7071 const struct type_unit_group *tu_group
7072 = (const struct type_unit_group *) item;
7073
7074 return hash_stmt_list_entry (&tu_group->hash);
7075 }
7076
7077 static int
7078 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7079 {
7080 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7081 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7082
7083 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7084 }
7085
7086 /* Allocate a hash table for type unit groups. */
7087
7088 static htab_up
7089 allocate_type_unit_groups_table (struct objfile *objfile)
7090 {
7091 return htab_up (htab_create_alloc (3,
7092 hash_type_unit_group,
7093 eq_type_unit_group,
7094 NULL, xcalloc, xfree));
7095 }
7096
7097 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7098 partial symtabs. We combine several TUs per psymtab to not let the size
7099 of any one psymtab grow too big. */
7100 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7101 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7102
7103 /* Helper routine for get_type_unit_group.
7104 Create the type_unit_group object used to hold one or more TUs. */
7105
7106 static struct type_unit_group *
7107 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7108 {
7109 struct dwarf2_per_objfile *dwarf2_per_objfile
7110 = cu->per_cu->dwarf2_per_objfile;
7111 struct objfile *objfile = dwarf2_per_objfile->objfile;
7112 struct dwarf2_per_cu_data *per_cu;
7113 struct type_unit_group *tu_group;
7114
7115 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7116 struct type_unit_group);
7117 per_cu = &tu_group->per_cu;
7118 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7119
7120 if (dwarf2_per_objfile->using_index)
7121 {
7122 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7123 struct dwarf2_per_cu_quick_data);
7124 }
7125 else
7126 {
7127 unsigned int line_offset = to_underlying (line_offset_struct);
7128 dwarf2_psymtab *pst;
7129 std::string name;
7130
7131 /* Give the symtab a useful name for debug purposes. */
7132 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7133 name = string_printf ("<type_units_%d>",
7134 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7135 else
7136 name = string_printf ("<type_units_at_0x%x>", line_offset);
7137
7138 pst = create_partial_symtab (per_cu, name.c_str ());
7139 pst->anonymous = true;
7140 }
7141
7142 tu_group->hash.dwo_unit = cu->dwo_unit;
7143 tu_group->hash.line_sect_off = line_offset_struct;
7144
7145 return tu_group;
7146 }
7147
7148 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7149 STMT_LIST is a DW_AT_stmt_list attribute. */
7150
7151 static struct type_unit_group *
7152 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7153 {
7154 struct dwarf2_per_objfile *dwarf2_per_objfile
7155 = cu->per_cu->dwarf2_per_objfile;
7156 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7157 struct type_unit_group *tu_group;
7158 void **slot;
7159 unsigned int line_offset;
7160 struct type_unit_group type_unit_group_for_lookup;
7161
7162 if (dwarf2_per_objfile->type_unit_groups == NULL)
7163 {
7164 dwarf2_per_objfile->type_unit_groups =
7165 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7166 }
7167
7168 /* Do we need to create a new group, or can we use an existing one? */
7169
7170 if (stmt_list)
7171 {
7172 line_offset = DW_UNSND (stmt_list);
7173 ++tu_stats->nr_symtab_sharers;
7174 }
7175 else
7176 {
7177 /* Ugh, no stmt_list. Rare, but we have to handle it.
7178 We can do various things here like create one group per TU or
7179 spread them over multiple groups to split up the expansion work.
7180 To avoid worst case scenarios (too many groups or too large groups)
7181 we, umm, group them in bunches. */
7182 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7183 | (tu_stats->nr_stmt_less_type_units
7184 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7185 ++tu_stats->nr_stmt_less_type_units;
7186 }
7187
7188 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7189 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7190 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7191 &type_unit_group_for_lookup, INSERT);
7192 if (*slot != NULL)
7193 {
7194 tu_group = (struct type_unit_group *) *slot;
7195 gdb_assert (tu_group != NULL);
7196 }
7197 else
7198 {
7199 sect_offset line_offset_struct = (sect_offset) line_offset;
7200 tu_group = create_type_unit_group (cu, line_offset_struct);
7201 *slot = tu_group;
7202 ++tu_stats->nr_symtabs;
7203 }
7204
7205 return tu_group;
7206 }
7207 \f
7208 /* Partial symbol tables. */
7209
7210 /* Create a psymtab named NAME and assign it to PER_CU.
7211
7212 The caller must fill in the following details:
7213 dirname, textlow, texthigh. */
7214
7215 static dwarf2_psymtab *
7216 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7217 {
7218 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7219 dwarf2_psymtab *pst;
7220
7221 pst = new dwarf2_psymtab (name, objfile, 0);
7222
7223 pst->psymtabs_addrmap_supported = true;
7224
7225 /* This is the glue that links PST into GDB's symbol API. */
7226 pst->per_cu_data = per_cu;
7227 per_cu->v.psymtab = pst;
7228
7229 return pst;
7230 }
7231
7232 /* DIE reader function for process_psymtab_comp_unit. */
7233
7234 static void
7235 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7236 const gdb_byte *info_ptr,
7237 struct die_info *comp_unit_die,
7238 enum language pretend_language)
7239 {
7240 struct dwarf2_cu *cu = reader->cu;
7241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7243 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7244 CORE_ADDR baseaddr;
7245 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7246 dwarf2_psymtab *pst;
7247 enum pc_bounds_kind cu_bounds_kind;
7248 const char *filename;
7249
7250 gdb_assert (! per_cu->is_debug_types);
7251
7252 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7253
7254 /* Allocate a new partial symbol table structure. */
7255 gdb::unique_xmalloc_ptr<char> debug_filename;
7256 static const char artificial[] = "<artificial>";
7257 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7258 if (filename == NULL)
7259 filename = "";
7260 else if (strcmp (filename, artificial) == 0)
7261 {
7262 debug_filename.reset (concat (artificial, "@",
7263 sect_offset_str (per_cu->sect_off),
7264 (char *) NULL));
7265 filename = debug_filename.get ();
7266 }
7267
7268 pst = create_partial_symtab (per_cu, filename);
7269
7270 /* This must be done before calling dwarf2_build_include_psymtabs. */
7271 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7272
7273 baseaddr = objfile->text_section_offset ();
7274
7275 dwarf2_find_base_address (comp_unit_die, cu);
7276
7277 /* Possibly set the default values of LOWPC and HIGHPC from
7278 `DW_AT_ranges'. */
7279 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7280 &best_highpc, cu, pst);
7281 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7282 {
7283 CORE_ADDR low
7284 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7285 - baseaddr);
7286 CORE_ADDR high
7287 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7288 - baseaddr - 1);
7289 /* Store the contiguous range if it is not empty; it can be
7290 empty for CUs with no code. */
7291 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7292 low, high, pst);
7293 }
7294
7295 /* Check if comp unit has_children.
7296 If so, read the rest of the partial symbols from this comp unit.
7297 If not, there's no more debug_info for this comp unit. */
7298 if (comp_unit_die->has_children)
7299 {
7300 struct partial_die_info *first_die;
7301 CORE_ADDR lowpc, highpc;
7302
7303 lowpc = ((CORE_ADDR) -1);
7304 highpc = ((CORE_ADDR) 0);
7305
7306 first_die = load_partial_dies (reader, info_ptr, 1);
7307
7308 scan_partial_symbols (first_die, &lowpc, &highpc,
7309 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7310
7311 /* If we didn't find a lowpc, set it to highpc to avoid
7312 complaints from `maint check'. */
7313 if (lowpc == ((CORE_ADDR) -1))
7314 lowpc = highpc;
7315
7316 /* If the compilation unit didn't have an explicit address range,
7317 then use the information extracted from its child dies. */
7318 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7319 {
7320 best_lowpc = lowpc;
7321 best_highpc = highpc;
7322 }
7323 }
7324 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7325 best_lowpc + baseaddr)
7326 - baseaddr);
7327 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7328 best_highpc + baseaddr)
7329 - baseaddr);
7330
7331 end_psymtab_common (objfile, pst);
7332
7333 if (!cu->per_cu->imported_symtabs_empty ())
7334 {
7335 int i;
7336 int len = cu->per_cu->imported_symtabs_size ();
7337
7338 /* Fill in 'dependencies' here; we fill in 'users' in a
7339 post-pass. */
7340 pst->number_of_dependencies = len;
7341 pst->dependencies
7342 = objfile->partial_symtabs->allocate_dependencies (len);
7343 for (i = 0; i < len; ++i)
7344 {
7345 pst->dependencies[i]
7346 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7347 }
7348
7349 cu->per_cu->imported_symtabs_free ();
7350 }
7351
7352 /* Get the list of files included in the current compilation unit,
7353 and build a psymtab for each of them. */
7354 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7355
7356 if (dwarf_read_debug)
7357 fprintf_unfiltered (gdb_stdlog,
7358 "Psymtab for %s unit @%s: %s - %s"
7359 ", %d global, %d static syms\n",
7360 per_cu->is_debug_types ? "type" : "comp",
7361 sect_offset_str (per_cu->sect_off),
7362 paddress (gdbarch, pst->text_low (objfile)),
7363 paddress (gdbarch, pst->text_high (objfile)),
7364 pst->n_global_syms, pst->n_static_syms);
7365 }
7366
7367 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7368 Process compilation unit THIS_CU for a psymtab. */
7369
7370 static void
7371 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7372 bool want_partial_unit,
7373 enum language pretend_language)
7374 {
7375 /* If this compilation unit was already read in, free the
7376 cached copy in order to read it in again. This is
7377 necessary because we skipped some symbols when we first
7378 read in the compilation unit (see load_partial_dies).
7379 This problem could be avoided, but the benefit is unclear. */
7380 if (this_cu->cu != NULL)
7381 free_one_cached_comp_unit (this_cu);
7382
7383 cutu_reader reader (this_cu, NULL, 0, false);
7384
7385 if (reader.dummy_p)
7386 {
7387 /* Nothing. */
7388 }
7389 else if (this_cu->is_debug_types)
7390 build_type_psymtabs_reader (&reader, reader.info_ptr,
7391 reader.comp_unit_die);
7392 else if (want_partial_unit
7393 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7394 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7395 reader.comp_unit_die,
7396 pretend_language);
7397
7398 /* Age out any secondary CUs. */
7399 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7400 }
7401
7402 /* Reader function for build_type_psymtabs. */
7403
7404 static void
7405 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7406 const gdb_byte *info_ptr,
7407 struct die_info *type_unit_die)
7408 {
7409 struct dwarf2_per_objfile *dwarf2_per_objfile
7410 = reader->cu->per_cu->dwarf2_per_objfile;
7411 struct objfile *objfile = dwarf2_per_objfile->objfile;
7412 struct dwarf2_cu *cu = reader->cu;
7413 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7414 struct signatured_type *sig_type;
7415 struct type_unit_group *tu_group;
7416 struct attribute *attr;
7417 struct partial_die_info *first_die;
7418 CORE_ADDR lowpc, highpc;
7419 dwarf2_psymtab *pst;
7420
7421 gdb_assert (per_cu->is_debug_types);
7422 sig_type = (struct signatured_type *) per_cu;
7423
7424 if (! type_unit_die->has_children)
7425 return;
7426
7427 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7428 tu_group = get_type_unit_group (cu, attr);
7429
7430 if (tu_group->tus == nullptr)
7431 tu_group->tus = new std::vector<signatured_type *>;
7432 tu_group->tus->push_back (sig_type);
7433
7434 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7435 pst = create_partial_symtab (per_cu, "");
7436 pst->anonymous = true;
7437
7438 first_die = load_partial_dies (reader, info_ptr, 1);
7439
7440 lowpc = (CORE_ADDR) -1;
7441 highpc = (CORE_ADDR) 0;
7442 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7443
7444 end_psymtab_common (objfile, pst);
7445 }
7446
7447 /* Struct used to sort TUs by their abbreviation table offset. */
7448
7449 struct tu_abbrev_offset
7450 {
7451 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7452 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7453 {}
7454
7455 signatured_type *sig_type;
7456 sect_offset abbrev_offset;
7457 };
7458
7459 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7460
7461 static bool
7462 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7463 const struct tu_abbrev_offset &b)
7464 {
7465 return a.abbrev_offset < b.abbrev_offset;
7466 }
7467
7468 /* Efficiently read all the type units.
7469 This does the bulk of the work for build_type_psymtabs.
7470
7471 The efficiency is because we sort TUs by the abbrev table they use and
7472 only read each abbrev table once. In one program there are 200K TUs
7473 sharing 8K abbrev tables.
7474
7475 The main purpose of this function is to support building the
7476 dwarf2_per_objfile->type_unit_groups table.
7477 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7478 can collapse the search space by grouping them by stmt_list.
7479 The savings can be significant, in the same program from above the 200K TUs
7480 share 8K stmt_list tables.
7481
7482 FUNC is expected to call get_type_unit_group, which will create the
7483 struct type_unit_group if necessary and add it to
7484 dwarf2_per_objfile->type_unit_groups. */
7485
7486 static void
7487 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7488 {
7489 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7490 abbrev_table_up abbrev_table;
7491 sect_offset abbrev_offset;
7492
7493 /* It's up to the caller to not call us multiple times. */
7494 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7495
7496 if (dwarf2_per_objfile->all_type_units.empty ())
7497 return;
7498
7499 /* TUs typically share abbrev tables, and there can be way more TUs than
7500 abbrev tables. Sort by abbrev table to reduce the number of times we
7501 read each abbrev table in.
7502 Alternatives are to punt or to maintain a cache of abbrev tables.
7503 This is simpler and efficient enough for now.
7504
7505 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7506 symtab to use). Typically TUs with the same abbrev offset have the same
7507 stmt_list value too so in practice this should work well.
7508
7509 The basic algorithm here is:
7510
7511 sort TUs by abbrev table
7512 for each TU with same abbrev table:
7513 read abbrev table if first user
7514 read TU top level DIE
7515 [IWBN if DWO skeletons had DW_AT_stmt_list]
7516 call FUNC */
7517
7518 if (dwarf_read_debug)
7519 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7520
7521 /* Sort in a separate table to maintain the order of all_type_units
7522 for .gdb_index: TU indices directly index all_type_units. */
7523 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7524 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7525
7526 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7527 sorted_by_abbrev.emplace_back
7528 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7529 sig_type->per_cu.section,
7530 sig_type->per_cu.sect_off));
7531
7532 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7533 sort_tu_by_abbrev_offset);
7534
7535 abbrev_offset = (sect_offset) ~(unsigned) 0;
7536
7537 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7538 {
7539 /* Switch to the next abbrev table if necessary. */
7540 if (abbrev_table == NULL
7541 || tu.abbrev_offset != abbrev_offset)
7542 {
7543 abbrev_offset = tu.abbrev_offset;
7544 abbrev_table =
7545 abbrev_table::read (dwarf2_per_objfile->objfile,
7546 &dwarf2_per_objfile->abbrev,
7547 abbrev_offset);
7548 ++tu_stats->nr_uniq_abbrev_tables;
7549 }
7550
7551 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7552 0, false);
7553 if (!reader.dummy_p)
7554 build_type_psymtabs_reader (&reader, reader.info_ptr,
7555 reader.comp_unit_die);
7556 }
7557 }
7558
7559 /* Print collected type unit statistics. */
7560
7561 static void
7562 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7563 {
7564 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7565
7566 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7567 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7568 dwarf2_per_objfile->all_type_units.size ());
7569 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7570 tu_stats->nr_uniq_abbrev_tables);
7571 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7572 tu_stats->nr_symtabs);
7573 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7574 tu_stats->nr_symtab_sharers);
7575 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7576 tu_stats->nr_stmt_less_type_units);
7577 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7578 tu_stats->nr_all_type_units_reallocs);
7579 }
7580
7581 /* Traversal function for build_type_psymtabs. */
7582
7583 static int
7584 build_type_psymtab_dependencies (void **slot, void *info)
7585 {
7586 struct dwarf2_per_objfile *dwarf2_per_objfile
7587 = (struct dwarf2_per_objfile *) info;
7588 struct objfile *objfile = dwarf2_per_objfile->objfile;
7589 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7590 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7591 dwarf2_psymtab *pst = per_cu->v.psymtab;
7592 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7593 int i;
7594
7595 gdb_assert (len > 0);
7596 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7597
7598 pst->number_of_dependencies = len;
7599 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7600 for (i = 0; i < len; ++i)
7601 {
7602 struct signatured_type *iter = tu_group->tus->at (i);
7603 gdb_assert (iter->per_cu.is_debug_types);
7604 pst->dependencies[i] = iter->per_cu.v.psymtab;
7605 iter->type_unit_group = tu_group;
7606 }
7607
7608 delete tu_group->tus;
7609 tu_group->tus = nullptr;
7610
7611 return 1;
7612 }
7613
7614 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7615 Build partial symbol tables for the .debug_types comp-units. */
7616
7617 static void
7618 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7619 {
7620 if (! create_all_type_units (dwarf2_per_objfile))
7621 return;
7622
7623 build_type_psymtabs_1 (dwarf2_per_objfile);
7624 }
7625
7626 /* Traversal function for process_skeletonless_type_unit.
7627 Read a TU in a DWO file and build partial symbols for it. */
7628
7629 static int
7630 process_skeletonless_type_unit (void **slot, void *info)
7631 {
7632 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7633 struct dwarf2_per_objfile *dwarf2_per_objfile
7634 = (struct dwarf2_per_objfile *) info;
7635 struct signatured_type find_entry, *entry;
7636
7637 /* If this TU doesn't exist in the global table, add it and read it in. */
7638
7639 if (dwarf2_per_objfile->signatured_types == NULL)
7640 {
7641 dwarf2_per_objfile->signatured_types
7642 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
7643 }
7644
7645 find_entry.signature = dwo_unit->signature;
7646 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7647 &find_entry, INSERT);
7648 /* If we've already seen this type there's nothing to do. What's happening
7649 is we're doing our own version of comdat-folding here. */
7650 if (*slot != NULL)
7651 return 1;
7652
7653 /* This does the job that create_all_type_units would have done for
7654 this TU. */
7655 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7656 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7657 *slot = entry;
7658
7659 /* This does the job that build_type_psymtabs_1 would have done. */
7660 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7661 if (!reader.dummy_p)
7662 build_type_psymtabs_reader (&reader, reader.info_ptr,
7663 reader.comp_unit_die);
7664
7665 return 1;
7666 }
7667
7668 /* Traversal function for process_skeletonless_type_units. */
7669
7670 static int
7671 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7672 {
7673 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7674
7675 if (dwo_file->tus != NULL)
7676 htab_traverse_noresize (dwo_file->tus.get (),
7677 process_skeletonless_type_unit, info);
7678
7679 return 1;
7680 }
7681
7682 /* Scan all TUs of DWO files, verifying we've processed them.
7683 This is needed in case a TU was emitted without its skeleton.
7684 Note: This can't be done until we know what all the DWO files are. */
7685
7686 static void
7687 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7688 {
7689 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7690 if (get_dwp_file (dwarf2_per_objfile) == NULL
7691 && dwarf2_per_objfile->dwo_files != NULL)
7692 {
7693 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7694 process_dwo_file_for_skeletonless_type_units,
7695 dwarf2_per_objfile);
7696 }
7697 }
7698
7699 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7700
7701 static void
7702 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7703 {
7704 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7705 {
7706 dwarf2_psymtab *pst = per_cu->v.psymtab;
7707
7708 if (pst == NULL)
7709 continue;
7710
7711 for (int j = 0; j < pst->number_of_dependencies; ++j)
7712 {
7713 /* Set the 'user' field only if it is not already set. */
7714 if (pst->dependencies[j]->user == NULL)
7715 pst->dependencies[j]->user = pst;
7716 }
7717 }
7718 }
7719
7720 /* Build the partial symbol table by doing a quick pass through the
7721 .debug_info and .debug_abbrev sections. */
7722
7723 static void
7724 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7725 {
7726 struct objfile *objfile = dwarf2_per_objfile->objfile;
7727
7728 if (dwarf_read_debug)
7729 {
7730 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7731 objfile_name (objfile));
7732 }
7733
7734 dwarf2_per_objfile->reading_partial_symbols = 1;
7735
7736 dwarf2_per_objfile->info.read (objfile);
7737
7738 /* Any cached compilation units will be linked by the per-objfile
7739 read_in_chain. Make sure to free them when we're done. */
7740 free_cached_comp_units freer (dwarf2_per_objfile);
7741
7742 build_type_psymtabs (dwarf2_per_objfile);
7743
7744 create_all_comp_units (dwarf2_per_objfile);
7745
7746 /* Create a temporary address map on a temporary obstack. We later
7747 copy this to the final obstack. */
7748 auto_obstack temp_obstack;
7749
7750 scoped_restore save_psymtabs_addrmap
7751 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7752 addrmap_create_mutable (&temp_obstack));
7753
7754 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7755 process_psymtab_comp_unit (per_cu, false, language_minimal);
7756
7757 /* This has to wait until we read the CUs, we need the list of DWOs. */
7758 process_skeletonless_type_units (dwarf2_per_objfile);
7759
7760 /* Now that all TUs have been processed we can fill in the dependencies. */
7761 if (dwarf2_per_objfile->type_unit_groups != NULL)
7762 {
7763 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7764 build_type_psymtab_dependencies, dwarf2_per_objfile);
7765 }
7766
7767 if (dwarf_read_debug)
7768 print_tu_stats (dwarf2_per_objfile);
7769
7770 set_partial_user (dwarf2_per_objfile);
7771
7772 objfile->partial_symtabs->psymtabs_addrmap
7773 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7774 objfile->partial_symtabs->obstack ());
7775 /* At this point we want to keep the address map. */
7776 save_psymtabs_addrmap.release ();
7777
7778 if (dwarf_read_debug)
7779 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7780 objfile_name (objfile));
7781 }
7782
7783 /* Load the partial DIEs for a secondary CU into memory.
7784 This is also used when rereading a primary CU with load_all_dies. */
7785
7786 static void
7787 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7788 {
7789 cutu_reader reader (this_cu, NULL, 1, false);
7790
7791 if (!reader.dummy_p)
7792 {
7793 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7794 language_minimal);
7795
7796 /* Check if comp unit has_children.
7797 If so, read the rest of the partial symbols from this comp unit.
7798 If not, there's no more debug_info for this comp unit. */
7799 if (reader.comp_unit_die->has_children)
7800 load_partial_dies (&reader, reader.info_ptr, 0);
7801
7802 reader.keep ();
7803 }
7804 }
7805
7806 static void
7807 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7808 struct dwarf2_section_info *section,
7809 struct dwarf2_section_info *abbrev_section,
7810 unsigned int is_dwz)
7811 {
7812 const gdb_byte *info_ptr;
7813 struct objfile *objfile = dwarf2_per_objfile->objfile;
7814
7815 if (dwarf_read_debug)
7816 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7817 section->get_name (),
7818 section->get_file_name ());
7819
7820 section->read (objfile);
7821
7822 info_ptr = section->buffer;
7823
7824 while (info_ptr < section->buffer + section->size)
7825 {
7826 struct dwarf2_per_cu_data *this_cu;
7827
7828 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7829
7830 comp_unit_head cu_header;
7831 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7832 abbrev_section, info_ptr,
7833 rcuh_kind::COMPILE);
7834
7835 /* Save the compilation unit for later lookup. */
7836 if (cu_header.unit_type != DW_UT_type)
7837 {
7838 this_cu = XOBNEW (&objfile->objfile_obstack,
7839 struct dwarf2_per_cu_data);
7840 memset (this_cu, 0, sizeof (*this_cu));
7841 }
7842 else
7843 {
7844 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7845 struct signatured_type);
7846 memset (sig_type, 0, sizeof (*sig_type));
7847 sig_type->signature = cu_header.signature;
7848 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7849 this_cu = &sig_type->per_cu;
7850 }
7851 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7852 this_cu->sect_off = sect_off;
7853 this_cu->length = cu_header.length + cu_header.initial_length_size;
7854 this_cu->is_dwz = is_dwz;
7855 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7856 this_cu->section = section;
7857
7858 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7859
7860 info_ptr = info_ptr + this_cu->length;
7861 }
7862 }
7863
7864 /* Create a list of all compilation units in OBJFILE.
7865 This is only done for -readnow and building partial symtabs. */
7866
7867 static void
7868 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7869 {
7870 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7871 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7872 &dwarf2_per_objfile->abbrev, 0);
7873
7874 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7875 if (dwz != NULL)
7876 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7877 1);
7878 }
7879
7880 /* Process all loaded DIEs for compilation unit CU, starting at
7881 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7882 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7883 DW_AT_ranges). See the comments of add_partial_subprogram on how
7884 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7885
7886 static void
7887 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7888 CORE_ADDR *highpc, int set_addrmap,
7889 struct dwarf2_cu *cu)
7890 {
7891 struct partial_die_info *pdi;
7892
7893 /* Now, march along the PDI's, descending into ones which have
7894 interesting children but skipping the children of the other ones,
7895 until we reach the end of the compilation unit. */
7896
7897 pdi = first_die;
7898
7899 while (pdi != NULL)
7900 {
7901 pdi->fixup (cu);
7902
7903 /* Anonymous namespaces or modules have no name but have interesting
7904 children, so we need to look at them. Ditto for anonymous
7905 enums. */
7906
7907 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7908 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7909 || pdi->tag == DW_TAG_imported_unit
7910 || pdi->tag == DW_TAG_inlined_subroutine)
7911 {
7912 switch (pdi->tag)
7913 {
7914 case DW_TAG_subprogram:
7915 case DW_TAG_inlined_subroutine:
7916 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7917 break;
7918 case DW_TAG_constant:
7919 case DW_TAG_variable:
7920 case DW_TAG_typedef:
7921 case DW_TAG_union_type:
7922 if (!pdi->is_declaration)
7923 {
7924 add_partial_symbol (pdi, cu);
7925 }
7926 break;
7927 case DW_TAG_class_type:
7928 case DW_TAG_interface_type:
7929 case DW_TAG_structure_type:
7930 if (!pdi->is_declaration)
7931 {
7932 add_partial_symbol (pdi, cu);
7933 }
7934 if ((cu->language == language_rust
7935 || cu->language == language_cplus) && pdi->has_children)
7936 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7937 set_addrmap, cu);
7938 break;
7939 case DW_TAG_enumeration_type:
7940 if (!pdi->is_declaration)
7941 add_partial_enumeration (pdi, cu);
7942 break;
7943 case DW_TAG_base_type:
7944 case DW_TAG_subrange_type:
7945 /* File scope base type definitions are added to the partial
7946 symbol table. */
7947 add_partial_symbol (pdi, cu);
7948 break;
7949 case DW_TAG_namespace:
7950 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7951 break;
7952 case DW_TAG_module:
7953 if (!pdi->is_declaration)
7954 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7955 break;
7956 case DW_TAG_imported_unit:
7957 {
7958 struct dwarf2_per_cu_data *per_cu;
7959
7960 /* For now we don't handle imported units in type units. */
7961 if (cu->per_cu->is_debug_types)
7962 {
7963 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7964 " supported in type units [in module %s]"),
7965 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7966 }
7967
7968 per_cu = dwarf2_find_containing_comp_unit
7969 (pdi->d.sect_off, pdi->is_dwz,
7970 cu->per_cu->dwarf2_per_objfile);
7971
7972 /* Go read the partial unit, if needed. */
7973 if (per_cu->v.psymtab == NULL)
7974 process_psymtab_comp_unit (per_cu, true, cu->language);
7975
7976 cu->per_cu->imported_symtabs_push (per_cu);
7977 }
7978 break;
7979 case DW_TAG_imported_declaration:
7980 add_partial_symbol (pdi, cu);
7981 break;
7982 default:
7983 break;
7984 }
7985 }
7986
7987 /* If the die has a sibling, skip to the sibling. */
7988
7989 pdi = pdi->die_sibling;
7990 }
7991 }
7992
7993 /* Functions used to compute the fully scoped name of a partial DIE.
7994
7995 Normally, this is simple. For C++, the parent DIE's fully scoped
7996 name is concatenated with "::" and the partial DIE's name.
7997 Enumerators are an exception; they use the scope of their parent
7998 enumeration type, i.e. the name of the enumeration type is not
7999 prepended to the enumerator.
8000
8001 There are two complexities. One is DW_AT_specification; in this
8002 case "parent" means the parent of the target of the specification,
8003 instead of the direct parent of the DIE. The other is compilers
8004 which do not emit DW_TAG_namespace; in this case we try to guess
8005 the fully qualified name of structure types from their members'
8006 linkage names. This must be done using the DIE's children rather
8007 than the children of any DW_AT_specification target. We only need
8008 to do this for structures at the top level, i.e. if the target of
8009 any DW_AT_specification (if any; otherwise the DIE itself) does not
8010 have a parent. */
8011
8012 /* Compute the scope prefix associated with PDI's parent, in
8013 compilation unit CU. The result will be allocated on CU's
8014 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8015 field. NULL is returned if no prefix is necessary. */
8016 static const char *
8017 partial_die_parent_scope (struct partial_die_info *pdi,
8018 struct dwarf2_cu *cu)
8019 {
8020 const char *grandparent_scope;
8021 struct partial_die_info *parent, *real_pdi;
8022
8023 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8024 then this means the parent of the specification DIE. */
8025
8026 real_pdi = pdi;
8027 while (real_pdi->has_specification)
8028 {
8029 auto res = find_partial_die (real_pdi->spec_offset,
8030 real_pdi->spec_is_dwz, cu);
8031 real_pdi = res.pdi;
8032 cu = res.cu;
8033 }
8034
8035 parent = real_pdi->die_parent;
8036 if (parent == NULL)
8037 return NULL;
8038
8039 if (parent->scope_set)
8040 return parent->scope;
8041
8042 parent->fixup (cu);
8043
8044 grandparent_scope = partial_die_parent_scope (parent, cu);
8045
8046 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8047 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8048 Work around this problem here. */
8049 if (cu->language == language_cplus
8050 && parent->tag == DW_TAG_namespace
8051 && strcmp (parent->name, "::") == 0
8052 && grandparent_scope == NULL)
8053 {
8054 parent->scope = NULL;
8055 parent->scope_set = 1;
8056 return NULL;
8057 }
8058
8059 /* Nested subroutines in Fortran get a prefix. */
8060 if (pdi->tag == DW_TAG_enumerator)
8061 /* Enumerators should not get the name of the enumeration as a prefix. */
8062 parent->scope = grandparent_scope;
8063 else if (parent->tag == DW_TAG_namespace
8064 || parent->tag == DW_TAG_module
8065 || parent->tag == DW_TAG_structure_type
8066 || parent->tag == DW_TAG_class_type
8067 || parent->tag == DW_TAG_interface_type
8068 || parent->tag == DW_TAG_union_type
8069 || parent->tag == DW_TAG_enumeration_type
8070 || (cu->language == language_fortran
8071 && parent->tag == DW_TAG_subprogram
8072 && pdi->tag == DW_TAG_subprogram))
8073 {
8074 if (grandparent_scope == NULL)
8075 parent->scope = parent->name;
8076 else
8077 parent->scope = typename_concat (&cu->comp_unit_obstack,
8078 grandparent_scope,
8079 parent->name, 0, cu);
8080 }
8081 else
8082 {
8083 /* FIXME drow/2004-04-01: What should we be doing with
8084 function-local names? For partial symbols, we should probably be
8085 ignoring them. */
8086 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8087 dwarf_tag_name (parent->tag),
8088 sect_offset_str (pdi->sect_off));
8089 parent->scope = grandparent_scope;
8090 }
8091
8092 parent->scope_set = 1;
8093 return parent->scope;
8094 }
8095
8096 /* Return the fully scoped name associated with PDI, from compilation unit
8097 CU. The result will be allocated with malloc. */
8098
8099 static gdb::unique_xmalloc_ptr<char>
8100 partial_die_full_name (struct partial_die_info *pdi,
8101 struct dwarf2_cu *cu)
8102 {
8103 const char *parent_scope;
8104
8105 /* If this is a template instantiation, we can not work out the
8106 template arguments from partial DIEs. So, unfortunately, we have
8107 to go through the full DIEs. At least any work we do building
8108 types here will be reused if full symbols are loaded later. */
8109 if (pdi->has_template_arguments)
8110 {
8111 pdi->fixup (cu);
8112
8113 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8114 {
8115 struct die_info *die;
8116 struct attribute attr;
8117 struct dwarf2_cu *ref_cu = cu;
8118
8119 /* DW_FORM_ref_addr is using section offset. */
8120 attr.name = (enum dwarf_attribute) 0;
8121 attr.form = DW_FORM_ref_addr;
8122 attr.u.unsnd = to_underlying (pdi->sect_off);
8123 die = follow_die_ref (NULL, &attr, &ref_cu);
8124
8125 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8126 }
8127 }
8128
8129 parent_scope = partial_die_parent_scope (pdi, cu);
8130 if (parent_scope == NULL)
8131 return NULL;
8132 else
8133 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8134 pdi->name, 0, cu));
8135 }
8136
8137 static void
8138 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8139 {
8140 struct dwarf2_per_objfile *dwarf2_per_objfile
8141 = cu->per_cu->dwarf2_per_objfile;
8142 struct objfile *objfile = dwarf2_per_objfile->objfile;
8143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8144 CORE_ADDR addr = 0;
8145 const char *actual_name = NULL;
8146 CORE_ADDR baseaddr;
8147
8148 baseaddr = objfile->text_section_offset ();
8149
8150 gdb::unique_xmalloc_ptr<char> built_actual_name
8151 = partial_die_full_name (pdi, cu);
8152 if (built_actual_name != NULL)
8153 actual_name = built_actual_name.get ();
8154
8155 if (actual_name == NULL)
8156 actual_name = pdi->name;
8157
8158 switch (pdi->tag)
8159 {
8160 case DW_TAG_inlined_subroutine:
8161 case DW_TAG_subprogram:
8162 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8163 - baseaddr);
8164 if (pdi->is_external
8165 || cu->language == language_ada
8166 || (cu->language == language_fortran
8167 && pdi->die_parent != NULL
8168 && pdi->die_parent->tag == DW_TAG_subprogram))
8169 {
8170 /* Normally, only "external" DIEs are part of the global scope.
8171 But in Ada and Fortran, we want to be able to access nested
8172 procedures globally. So all Ada and Fortran subprograms are
8173 stored in the global scope. */
8174 add_psymbol_to_list (actual_name,
8175 built_actual_name != NULL,
8176 VAR_DOMAIN, LOC_BLOCK,
8177 SECT_OFF_TEXT (objfile),
8178 psymbol_placement::GLOBAL,
8179 addr,
8180 cu->language, objfile);
8181 }
8182 else
8183 {
8184 add_psymbol_to_list (actual_name,
8185 built_actual_name != NULL,
8186 VAR_DOMAIN, LOC_BLOCK,
8187 SECT_OFF_TEXT (objfile),
8188 psymbol_placement::STATIC,
8189 addr, cu->language, objfile);
8190 }
8191
8192 if (pdi->main_subprogram && actual_name != NULL)
8193 set_objfile_main_name (objfile, actual_name, cu->language);
8194 break;
8195 case DW_TAG_constant:
8196 add_psymbol_to_list (actual_name,
8197 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8198 -1, (pdi->is_external
8199 ? psymbol_placement::GLOBAL
8200 : psymbol_placement::STATIC),
8201 0, cu->language, objfile);
8202 break;
8203 case DW_TAG_variable:
8204 if (pdi->d.locdesc)
8205 addr = decode_locdesc (pdi->d.locdesc, cu);
8206
8207 if (pdi->d.locdesc
8208 && addr == 0
8209 && !dwarf2_per_objfile->has_section_at_zero)
8210 {
8211 /* A global or static variable may also have been stripped
8212 out by the linker if unused, in which case its address
8213 will be nullified; do not add such variables into partial
8214 symbol table then. */
8215 }
8216 else if (pdi->is_external)
8217 {
8218 /* Global Variable.
8219 Don't enter into the minimal symbol tables as there is
8220 a minimal symbol table entry from the ELF symbols already.
8221 Enter into partial symbol table if it has a location
8222 descriptor or a type.
8223 If the location descriptor is missing, new_symbol will create
8224 a LOC_UNRESOLVED symbol, the address of the variable will then
8225 be determined from the minimal symbol table whenever the variable
8226 is referenced.
8227 The address for the partial symbol table entry is not
8228 used by GDB, but it comes in handy for debugging partial symbol
8229 table building. */
8230
8231 if (pdi->d.locdesc || pdi->has_type)
8232 add_psymbol_to_list (actual_name,
8233 built_actual_name != NULL,
8234 VAR_DOMAIN, LOC_STATIC,
8235 SECT_OFF_TEXT (objfile),
8236 psymbol_placement::GLOBAL,
8237 addr, cu->language, objfile);
8238 }
8239 else
8240 {
8241 int has_loc = pdi->d.locdesc != NULL;
8242
8243 /* Static Variable. Skip symbols whose value we cannot know (those
8244 without location descriptors or constant values). */
8245 if (!has_loc && !pdi->has_const_value)
8246 return;
8247
8248 add_psymbol_to_list (actual_name,
8249 built_actual_name != NULL,
8250 VAR_DOMAIN, LOC_STATIC,
8251 SECT_OFF_TEXT (objfile),
8252 psymbol_placement::STATIC,
8253 has_loc ? addr : 0,
8254 cu->language, objfile);
8255 }
8256 break;
8257 case DW_TAG_typedef:
8258 case DW_TAG_base_type:
8259 case DW_TAG_subrange_type:
8260 add_psymbol_to_list (actual_name,
8261 built_actual_name != NULL,
8262 VAR_DOMAIN, LOC_TYPEDEF, -1,
8263 psymbol_placement::STATIC,
8264 0, cu->language, objfile);
8265 break;
8266 case DW_TAG_imported_declaration:
8267 case DW_TAG_namespace:
8268 add_psymbol_to_list (actual_name,
8269 built_actual_name != NULL,
8270 VAR_DOMAIN, LOC_TYPEDEF, -1,
8271 psymbol_placement::GLOBAL,
8272 0, cu->language, objfile);
8273 break;
8274 case DW_TAG_module:
8275 /* With Fortran 77 there might be a "BLOCK DATA" module
8276 available without any name. If so, we skip the module as it
8277 doesn't bring any value. */
8278 if (actual_name != nullptr)
8279 add_psymbol_to_list (actual_name,
8280 built_actual_name != NULL,
8281 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8282 psymbol_placement::GLOBAL,
8283 0, cu->language, objfile);
8284 break;
8285 case DW_TAG_class_type:
8286 case DW_TAG_interface_type:
8287 case DW_TAG_structure_type:
8288 case DW_TAG_union_type:
8289 case DW_TAG_enumeration_type:
8290 /* Skip external references. The DWARF standard says in the section
8291 about "Structure, Union, and Class Type Entries": "An incomplete
8292 structure, union or class type is represented by a structure,
8293 union or class entry that does not have a byte size attribute
8294 and that has a DW_AT_declaration attribute." */
8295 if (!pdi->has_byte_size && pdi->is_declaration)
8296 return;
8297
8298 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8299 static vs. global. */
8300 add_psymbol_to_list (actual_name,
8301 built_actual_name != NULL,
8302 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8303 cu->language == language_cplus
8304 ? psymbol_placement::GLOBAL
8305 : psymbol_placement::STATIC,
8306 0, cu->language, objfile);
8307
8308 break;
8309 case DW_TAG_enumerator:
8310 add_psymbol_to_list (actual_name,
8311 built_actual_name != NULL,
8312 VAR_DOMAIN, LOC_CONST, -1,
8313 cu->language == language_cplus
8314 ? psymbol_placement::GLOBAL
8315 : psymbol_placement::STATIC,
8316 0, cu->language, objfile);
8317 break;
8318 default:
8319 break;
8320 }
8321 }
8322
8323 /* Read a partial die corresponding to a namespace; also, add a symbol
8324 corresponding to that namespace to the symbol table. NAMESPACE is
8325 the name of the enclosing namespace. */
8326
8327 static void
8328 add_partial_namespace (struct partial_die_info *pdi,
8329 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8330 int set_addrmap, struct dwarf2_cu *cu)
8331 {
8332 /* Add a symbol for the namespace. */
8333
8334 add_partial_symbol (pdi, cu);
8335
8336 /* Now scan partial symbols in that namespace. */
8337
8338 if (pdi->has_children)
8339 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8340 }
8341
8342 /* Read a partial die corresponding to a Fortran module. */
8343
8344 static void
8345 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8346 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8347 {
8348 /* Add a symbol for the namespace. */
8349
8350 add_partial_symbol (pdi, cu);
8351
8352 /* Now scan partial symbols in that module. */
8353
8354 if (pdi->has_children)
8355 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8356 }
8357
8358 /* Read a partial die corresponding to a subprogram or an inlined
8359 subprogram and create a partial symbol for that subprogram.
8360 When the CU language allows it, this routine also defines a partial
8361 symbol for each nested subprogram that this subprogram contains.
8362 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8363 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8364
8365 PDI may also be a lexical block, in which case we simply search
8366 recursively for subprograms defined inside that lexical block.
8367 Again, this is only performed when the CU language allows this
8368 type of definitions. */
8369
8370 static void
8371 add_partial_subprogram (struct partial_die_info *pdi,
8372 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8373 int set_addrmap, struct dwarf2_cu *cu)
8374 {
8375 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8376 {
8377 if (pdi->has_pc_info)
8378 {
8379 if (pdi->lowpc < *lowpc)
8380 *lowpc = pdi->lowpc;
8381 if (pdi->highpc > *highpc)
8382 *highpc = pdi->highpc;
8383 if (set_addrmap)
8384 {
8385 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8386 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8387 CORE_ADDR baseaddr;
8388 CORE_ADDR this_highpc;
8389 CORE_ADDR this_lowpc;
8390
8391 baseaddr = objfile->text_section_offset ();
8392 this_lowpc
8393 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8394 pdi->lowpc + baseaddr)
8395 - baseaddr);
8396 this_highpc
8397 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8398 pdi->highpc + baseaddr)
8399 - baseaddr);
8400 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8401 this_lowpc, this_highpc - 1,
8402 cu->per_cu->v.psymtab);
8403 }
8404 }
8405
8406 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8407 {
8408 if (!pdi->is_declaration)
8409 /* Ignore subprogram DIEs that do not have a name, they are
8410 illegal. Do not emit a complaint at this point, we will
8411 do so when we convert this psymtab into a symtab. */
8412 if (pdi->name)
8413 add_partial_symbol (pdi, cu);
8414 }
8415 }
8416
8417 if (! pdi->has_children)
8418 return;
8419
8420 if (cu->language == language_ada || cu->language == language_fortran)
8421 {
8422 pdi = pdi->die_child;
8423 while (pdi != NULL)
8424 {
8425 pdi->fixup (cu);
8426 if (pdi->tag == DW_TAG_subprogram
8427 || pdi->tag == DW_TAG_inlined_subroutine
8428 || pdi->tag == DW_TAG_lexical_block)
8429 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8430 pdi = pdi->die_sibling;
8431 }
8432 }
8433 }
8434
8435 /* Read a partial die corresponding to an enumeration type. */
8436
8437 static void
8438 add_partial_enumeration (struct partial_die_info *enum_pdi,
8439 struct dwarf2_cu *cu)
8440 {
8441 struct partial_die_info *pdi;
8442
8443 if (enum_pdi->name != NULL)
8444 add_partial_symbol (enum_pdi, cu);
8445
8446 pdi = enum_pdi->die_child;
8447 while (pdi)
8448 {
8449 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8450 complaint (_("malformed enumerator DIE ignored"));
8451 else
8452 add_partial_symbol (pdi, cu);
8453 pdi = pdi->die_sibling;
8454 }
8455 }
8456
8457 /* Return the initial uleb128 in the die at INFO_PTR. */
8458
8459 static unsigned int
8460 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8461 {
8462 unsigned int bytes_read;
8463
8464 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8465 }
8466
8467 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8468 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8469
8470 Return the corresponding abbrev, or NULL if the number is zero (indicating
8471 an empty DIE). In either case *BYTES_READ will be set to the length of
8472 the initial number. */
8473
8474 static struct abbrev_info *
8475 peek_die_abbrev (const die_reader_specs &reader,
8476 const gdb_byte *info_ptr, unsigned int *bytes_read)
8477 {
8478 dwarf2_cu *cu = reader.cu;
8479 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8480 unsigned int abbrev_number
8481 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8482
8483 if (abbrev_number == 0)
8484 return NULL;
8485
8486 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8487 if (!abbrev)
8488 {
8489 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8490 " at offset %s [in module %s]"),
8491 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8492 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8493 }
8494
8495 return abbrev;
8496 }
8497
8498 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8499 Returns a pointer to the end of a series of DIEs, terminated by an empty
8500 DIE. Any children of the skipped DIEs will also be skipped. */
8501
8502 static const gdb_byte *
8503 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8504 {
8505 while (1)
8506 {
8507 unsigned int bytes_read;
8508 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8509
8510 if (abbrev == NULL)
8511 return info_ptr + bytes_read;
8512 else
8513 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8514 }
8515 }
8516
8517 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8518 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8519 abbrev corresponding to that skipped uleb128 should be passed in
8520 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8521 children. */
8522
8523 static const gdb_byte *
8524 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8525 struct abbrev_info *abbrev)
8526 {
8527 unsigned int bytes_read;
8528 struct attribute attr;
8529 bfd *abfd = reader->abfd;
8530 struct dwarf2_cu *cu = reader->cu;
8531 const gdb_byte *buffer = reader->buffer;
8532 const gdb_byte *buffer_end = reader->buffer_end;
8533 unsigned int form, i;
8534
8535 for (i = 0; i < abbrev->num_attrs; i++)
8536 {
8537 /* The only abbrev we care about is DW_AT_sibling. */
8538 if (abbrev->attrs[i].name == DW_AT_sibling)
8539 {
8540 bool ignored;
8541 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8542 &ignored);
8543 if (attr.form == DW_FORM_ref_addr)
8544 complaint (_("ignoring absolute DW_AT_sibling"));
8545 else
8546 {
8547 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8548 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8549
8550 if (sibling_ptr < info_ptr)
8551 complaint (_("DW_AT_sibling points backwards"));
8552 else if (sibling_ptr > reader->buffer_end)
8553 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8554 else
8555 return sibling_ptr;
8556 }
8557 }
8558
8559 /* If it isn't DW_AT_sibling, skip this attribute. */
8560 form = abbrev->attrs[i].form;
8561 skip_attribute:
8562 switch (form)
8563 {
8564 case DW_FORM_ref_addr:
8565 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8566 and later it is offset sized. */
8567 if (cu->header.version == 2)
8568 info_ptr += cu->header.addr_size;
8569 else
8570 info_ptr += cu->header.offset_size;
8571 break;
8572 case DW_FORM_GNU_ref_alt:
8573 info_ptr += cu->header.offset_size;
8574 break;
8575 case DW_FORM_addr:
8576 info_ptr += cu->header.addr_size;
8577 break;
8578 case DW_FORM_data1:
8579 case DW_FORM_ref1:
8580 case DW_FORM_flag:
8581 case DW_FORM_strx1:
8582 info_ptr += 1;
8583 break;
8584 case DW_FORM_flag_present:
8585 case DW_FORM_implicit_const:
8586 break;
8587 case DW_FORM_data2:
8588 case DW_FORM_ref2:
8589 case DW_FORM_strx2:
8590 info_ptr += 2;
8591 break;
8592 case DW_FORM_strx3:
8593 info_ptr += 3;
8594 break;
8595 case DW_FORM_data4:
8596 case DW_FORM_ref4:
8597 case DW_FORM_strx4:
8598 info_ptr += 4;
8599 break;
8600 case DW_FORM_data8:
8601 case DW_FORM_ref8:
8602 case DW_FORM_ref_sig8:
8603 info_ptr += 8;
8604 break;
8605 case DW_FORM_data16:
8606 info_ptr += 16;
8607 break;
8608 case DW_FORM_string:
8609 read_direct_string (abfd, info_ptr, &bytes_read);
8610 info_ptr += bytes_read;
8611 break;
8612 case DW_FORM_sec_offset:
8613 case DW_FORM_strp:
8614 case DW_FORM_GNU_strp_alt:
8615 info_ptr += cu->header.offset_size;
8616 break;
8617 case DW_FORM_exprloc:
8618 case DW_FORM_block:
8619 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8620 info_ptr += bytes_read;
8621 break;
8622 case DW_FORM_block1:
8623 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8624 break;
8625 case DW_FORM_block2:
8626 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8627 break;
8628 case DW_FORM_block4:
8629 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8630 break;
8631 case DW_FORM_addrx:
8632 case DW_FORM_strx:
8633 case DW_FORM_sdata:
8634 case DW_FORM_udata:
8635 case DW_FORM_ref_udata:
8636 case DW_FORM_GNU_addr_index:
8637 case DW_FORM_GNU_str_index:
8638 case DW_FORM_rnglistx:
8639 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8640 break;
8641 case DW_FORM_indirect:
8642 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8643 info_ptr += bytes_read;
8644 /* We need to continue parsing from here, so just go back to
8645 the top. */
8646 goto skip_attribute;
8647
8648 default:
8649 error (_("Dwarf Error: Cannot handle %s "
8650 "in DWARF reader [in module %s]"),
8651 dwarf_form_name (form),
8652 bfd_get_filename (abfd));
8653 }
8654 }
8655
8656 if (abbrev->has_children)
8657 return skip_children (reader, info_ptr);
8658 else
8659 return info_ptr;
8660 }
8661
8662 /* Locate ORIG_PDI's sibling.
8663 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8664
8665 static const gdb_byte *
8666 locate_pdi_sibling (const struct die_reader_specs *reader,
8667 struct partial_die_info *orig_pdi,
8668 const gdb_byte *info_ptr)
8669 {
8670 /* Do we know the sibling already? */
8671
8672 if (orig_pdi->sibling)
8673 return orig_pdi->sibling;
8674
8675 /* Are there any children to deal with? */
8676
8677 if (!orig_pdi->has_children)
8678 return info_ptr;
8679
8680 /* Skip the children the long way. */
8681
8682 return skip_children (reader, info_ptr);
8683 }
8684
8685 /* Expand this partial symbol table into a full symbol table. SELF is
8686 not NULL. */
8687
8688 void
8689 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8690 {
8691 struct dwarf2_per_objfile *dwarf2_per_objfile
8692 = get_dwarf2_per_objfile (objfile);
8693
8694 gdb_assert (!readin);
8695 /* If this psymtab is constructed from a debug-only objfile, the
8696 has_section_at_zero flag will not necessarily be correct. We
8697 can get the correct value for this flag by looking at the data
8698 associated with the (presumably stripped) associated objfile. */
8699 if (objfile->separate_debug_objfile_backlink)
8700 {
8701 struct dwarf2_per_objfile *dpo_backlink
8702 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8703
8704 dwarf2_per_objfile->has_section_at_zero
8705 = dpo_backlink->has_section_at_zero;
8706 }
8707
8708 dwarf2_per_objfile->reading_partial_symbols = 0;
8709
8710 expand_psymtab (objfile);
8711
8712 process_cu_includes (dwarf2_per_objfile);
8713 }
8714 \f
8715 /* Reading in full CUs. */
8716
8717 /* Add PER_CU to the queue. */
8718
8719 static void
8720 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8721 enum language pretend_language)
8722 {
8723 per_cu->queued = 1;
8724 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8725 }
8726
8727 /* If PER_CU is not yet queued, add it to the queue.
8728 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8729 dependency.
8730 The result is non-zero if PER_CU was queued, otherwise the result is zero
8731 meaning either PER_CU is already queued or it is already loaded.
8732
8733 N.B. There is an invariant here that if a CU is queued then it is loaded.
8734 The caller is required to load PER_CU if we return non-zero. */
8735
8736 static int
8737 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8738 struct dwarf2_per_cu_data *per_cu,
8739 enum language pretend_language)
8740 {
8741 /* We may arrive here during partial symbol reading, if we need full
8742 DIEs to process an unusual case (e.g. template arguments). Do
8743 not queue PER_CU, just tell our caller to load its DIEs. */
8744 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8745 {
8746 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8747 return 1;
8748 return 0;
8749 }
8750
8751 /* Mark the dependence relation so that we don't flush PER_CU
8752 too early. */
8753 if (dependent_cu != NULL)
8754 dwarf2_add_dependence (dependent_cu, per_cu);
8755
8756 /* If it's already on the queue, we have nothing to do. */
8757 if (per_cu->queued)
8758 return 0;
8759
8760 /* If the compilation unit is already loaded, just mark it as
8761 used. */
8762 if (per_cu->cu != NULL)
8763 {
8764 per_cu->cu->last_used = 0;
8765 return 0;
8766 }
8767
8768 /* Add it to the queue. */
8769 queue_comp_unit (per_cu, pretend_language);
8770
8771 return 1;
8772 }
8773
8774 /* Process the queue. */
8775
8776 static void
8777 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8778 {
8779 if (dwarf_read_debug)
8780 {
8781 fprintf_unfiltered (gdb_stdlog,
8782 "Expanding one or more symtabs of objfile %s ...\n",
8783 objfile_name (dwarf2_per_objfile->objfile));
8784 }
8785
8786 /* The queue starts out with one item, but following a DIE reference
8787 may load a new CU, adding it to the end of the queue. */
8788 while (!dwarf2_per_objfile->queue.empty ())
8789 {
8790 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8791
8792 if ((dwarf2_per_objfile->using_index
8793 ? !item.per_cu->v.quick->compunit_symtab
8794 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8795 /* Skip dummy CUs. */
8796 && item.per_cu->cu != NULL)
8797 {
8798 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8799 unsigned int debug_print_threshold;
8800 char buf[100];
8801
8802 if (per_cu->is_debug_types)
8803 {
8804 struct signatured_type *sig_type =
8805 (struct signatured_type *) per_cu;
8806
8807 sprintf (buf, "TU %s at offset %s",
8808 hex_string (sig_type->signature),
8809 sect_offset_str (per_cu->sect_off));
8810 /* There can be 100s of TUs.
8811 Only print them in verbose mode. */
8812 debug_print_threshold = 2;
8813 }
8814 else
8815 {
8816 sprintf (buf, "CU at offset %s",
8817 sect_offset_str (per_cu->sect_off));
8818 debug_print_threshold = 1;
8819 }
8820
8821 if (dwarf_read_debug >= debug_print_threshold)
8822 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8823
8824 if (per_cu->is_debug_types)
8825 process_full_type_unit (per_cu, item.pretend_language);
8826 else
8827 process_full_comp_unit (per_cu, item.pretend_language);
8828
8829 if (dwarf_read_debug >= debug_print_threshold)
8830 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8831 }
8832
8833 item.per_cu->queued = 0;
8834 dwarf2_per_objfile->queue.pop ();
8835 }
8836
8837 if (dwarf_read_debug)
8838 {
8839 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8840 objfile_name (dwarf2_per_objfile->objfile));
8841 }
8842 }
8843
8844 /* Read in full symbols for PST, and anything it depends on. */
8845
8846 void
8847 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8848 {
8849 struct dwarf2_per_cu_data *per_cu;
8850
8851 if (readin)
8852 return;
8853
8854 read_dependencies (objfile);
8855
8856 per_cu = per_cu_data;
8857
8858 if (per_cu == NULL)
8859 {
8860 /* It's an include file, no symbols to read for it.
8861 Everything is in the parent symtab. */
8862 readin = true;
8863 return;
8864 }
8865
8866 dw2_do_instantiate_symtab (per_cu, false);
8867 }
8868
8869 /* Trivial hash function for die_info: the hash value of a DIE
8870 is its offset in .debug_info for this objfile. */
8871
8872 static hashval_t
8873 die_hash (const void *item)
8874 {
8875 const struct die_info *die = (const struct die_info *) item;
8876
8877 return to_underlying (die->sect_off);
8878 }
8879
8880 /* Trivial comparison function for die_info structures: two DIEs
8881 are equal if they have the same offset. */
8882
8883 static int
8884 die_eq (const void *item_lhs, const void *item_rhs)
8885 {
8886 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8887 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8888
8889 return die_lhs->sect_off == die_rhs->sect_off;
8890 }
8891
8892 /* Load the DIEs associated with PER_CU into memory. */
8893
8894 static void
8895 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8896 bool skip_partial,
8897 enum language pretend_language)
8898 {
8899 gdb_assert (! this_cu->is_debug_types);
8900
8901 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8902 if (reader.dummy_p)
8903 return;
8904
8905 struct dwarf2_cu *cu = reader.cu;
8906 const gdb_byte *info_ptr = reader.info_ptr;
8907
8908 gdb_assert (cu->die_hash == NULL);
8909 cu->die_hash =
8910 htab_create_alloc_ex (cu->header.length / 12,
8911 die_hash,
8912 die_eq,
8913 NULL,
8914 &cu->comp_unit_obstack,
8915 hashtab_obstack_allocate,
8916 dummy_obstack_deallocate);
8917
8918 if (reader.comp_unit_die->has_children)
8919 reader.comp_unit_die->child
8920 = read_die_and_siblings (&reader, reader.info_ptr,
8921 &info_ptr, reader.comp_unit_die);
8922 cu->dies = reader.comp_unit_die;
8923 /* comp_unit_die is not stored in die_hash, no need. */
8924
8925 /* We try not to read any attributes in this function, because not
8926 all CUs needed for references have been loaded yet, and symbol
8927 table processing isn't initialized. But we have to set the CU language,
8928 or we won't be able to build types correctly.
8929 Similarly, if we do not read the producer, we can not apply
8930 producer-specific interpretation. */
8931 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8932
8933 reader.keep ();
8934 }
8935
8936 /* Add a DIE to the delayed physname list. */
8937
8938 static void
8939 add_to_method_list (struct type *type, int fnfield_index, int index,
8940 const char *name, struct die_info *die,
8941 struct dwarf2_cu *cu)
8942 {
8943 struct delayed_method_info mi;
8944 mi.type = type;
8945 mi.fnfield_index = fnfield_index;
8946 mi.index = index;
8947 mi.name = name;
8948 mi.die = die;
8949 cu->method_list.push_back (mi);
8950 }
8951
8952 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8953 "const" / "volatile". If so, decrements LEN by the length of the
8954 modifier and return true. Otherwise return false. */
8955
8956 template<size_t N>
8957 static bool
8958 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8959 {
8960 size_t mod_len = sizeof (mod) - 1;
8961 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8962 {
8963 len -= mod_len;
8964 return true;
8965 }
8966 return false;
8967 }
8968
8969 /* Compute the physnames of any methods on the CU's method list.
8970
8971 The computation of method physnames is delayed in order to avoid the
8972 (bad) condition that one of the method's formal parameters is of an as yet
8973 incomplete type. */
8974
8975 static void
8976 compute_delayed_physnames (struct dwarf2_cu *cu)
8977 {
8978 /* Only C++ delays computing physnames. */
8979 if (cu->method_list.empty ())
8980 return;
8981 gdb_assert (cu->language == language_cplus);
8982
8983 for (const delayed_method_info &mi : cu->method_list)
8984 {
8985 const char *physname;
8986 struct fn_fieldlist *fn_flp
8987 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8988 physname = dwarf2_physname (mi.name, mi.die, cu);
8989 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8990 = physname ? physname : "";
8991
8992 /* Since there's no tag to indicate whether a method is a
8993 const/volatile overload, extract that information out of the
8994 demangled name. */
8995 if (physname != NULL)
8996 {
8997 size_t len = strlen (physname);
8998
8999 while (1)
9000 {
9001 if (physname[len] == ')') /* shortcut */
9002 break;
9003 else if (check_modifier (physname, len, " const"))
9004 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9005 else if (check_modifier (physname, len, " volatile"))
9006 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9007 else
9008 break;
9009 }
9010 }
9011 }
9012
9013 /* The list is no longer needed. */
9014 cu->method_list.clear ();
9015 }
9016
9017 /* Go objects should be embedded in a DW_TAG_module DIE,
9018 and it's not clear if/how imported objects will appear.
9019 To keep Go support simple until that's worked out,
9020 go back through what we've read and create something usable.
9021 We could do this while processing each DIE, and feels kinda cleaner,
9022 but that way is more invasive.
9023 This is to, for example, allow the user to type "p var" or "b main"
9024 without having to specify the package name, and allow lookups
9025 of module.object to work in contexts that use the expression
9026 parser. */
9027
9028 static void
9029 fixup_go_packaging (struct dwarf2_cu *cu)
9030 {
9031 gdb::unique_xmalloc_ptr<char> package_name;
9032 struct pending *list;
9033 int i;
9034
9035 for (list = *cu->get_builder ()->get_global_symbols ();
9036 list != NULL;
9037 list = list->next)
9038 {
9039 for (i = 0; i < list->nsyms; ++i)
9040 {
9041 struct symbol *sym = list->symbol[i];
9042
9043 if (sym->language () == language_go
9044 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9045 {
9046 gdb::unique_xmalloc_ptr<char> this_package_name
9047 (go_symbol_package_name (sym));
9048
9049 if (this_package_name == NULL)
9050 continue;
9051 if (package_name == NULL)
9052 package_name = std::move (this_package_name);
9053 else
9054 {
9055 struct objfile *objfile
9056 = cu->per_cu->dwarf2_per_objfile->objfile;
9057 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9058 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9059 (symbol_symtab (sym) != NULL
9060 ? symtab_to_filename_for_display
9061 (symbol_symtab (sym))
9062 : objfile_name (objfile)),
9063 this_package_name.get (), package_name.get ());
9064 }
9065 }
9066 }
9067 }
9068
9069 if (package_name != NULL)
9070 {
9071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9072 const char *saved_package_name
9073 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9074 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9075 saved_package_name);
9076 struct symbol *sym;
9077
9078 sym = allocate_symbol (objfile);
9079 sym->set_language (language_go, &objfile->objfile_obstack);
9080 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9081 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9082 e.g., "main" finds the "main" module and not C's main(). */
9083 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9084 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9085 SYMBOL_TYPE (sym) = type;
9086
9087 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9088 }
9089 }
9090
9091 /* Allocate a fully-qualified name consisting of the two parts on the
9092 obstack. */
9093
9094 static const char *
9095 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9096 {
9097 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9098 }
9099
9100 /* A helper that allocates a struct discriminant_info to attach to a
9101 union type. */
9102
9103 static struct discriminant_info *
9104 alloc_discriminant_info (struct type *type, int discriminant_index,
9105 int default_index)
9106 {
9107 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9108 gdb_assert (discriminant_index == -1
9109 || (discriminant_index >= 0
9110 && discriminant_index < TYPE_NFIELDS (type)));
9111 gdb_assert (default_index == -1
9112 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9113
9114 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9115
9116 struct discriminant_info *disc
9117 = ((struct discriminant_info *)
9118 TYPE_ZALLOC (type,
9119 offsetof (struct discriminant_info, discriminants)
9120 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9121 disc->default_index = default_index;
9122 disc->discriminant_index = discriminant_index;
9123
9124 struct dynamic_prop prop;
9125 prop.kind = PROP_UNDEFINED;
9126 prop.data.baton = disc;
9127
9128 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9129
9130 return disc;
9131 }
9132
9133 /* Some versions of rustc emitted enums in an unusual way.
9134
9135 Ordinary enums were emitted as unions. The first element of each
9136 structure in the union was named "RUST$ENUM$DISR". This element
9137 held the discriminant.
9138
9139 These versions of Rust also implemented the "non-zero"
9140 optimization. When the enum had two values, and one is empty and
9141 the other holds a pointer that cannot be zero, the pointer is used
9142 as the discriminant, with a zero value meaning the empty variant.
9143 Here, the union's first member is of the form
9144 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9145 where the fieldnos are the indices of the fields that should be
9146 traversed in order to find the field (which may be several fields deep)
9147 and the variantname is the name of the variant of the case when the
9148 field is zero.
9149
9150 This function recognizes whether TYPE is of one of these forms,
9151 and, if so, smashes it to be a variant type. */
9152
9153 static void
9154 quirk_rust_enum (struct type *type, struct objfile *objfile)
9155 {
9156 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9157
9158 /* We don't need to deal with empty enums. */
9159 if (TYPE_NFIELDS (type) == 0)
9160 return;
9161
9162 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9163 if (TYPE_NFIELDS (type) == 1
9164 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9165 {
9166 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9167
9168 /* Decode the field name to find the offset of the
9169 discriminant. */
9170 ULONGEST bit_offset = 0;
9171 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9172 while (name[0] >= '0' && name[0] <= '9')
9173 {
9174 char *tail;
9175 unsigned long index = strtoul (name, &tail, 10);
9176 name = tail;
9177 if (*name != '$'
9178 || index >= TYPE_NFIELDS (field_type)
9179 || (TYPE_FIELD_LOC_KIND (field_type, index)
9180 != FIELD_LOC_KIND_BITPOS))
9181 {
9182 complaint (_("Could not parse Rust enum encoding string \"%s\""
9183 "[in module %s]"),
9184 TYPE_FIELD_NAME (type, 0),
9185 objfile_name (objfile));
9186 return;
9187 }
9188 ++name;
9189
9190 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9191 field_type = TYPE_FIELD_TYPE (field_type, index);
9192 }
9193
9194 /* Make a union to hold the variants. */
9195 struct type *union_type = alloc_type (objfile);
9196 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9197 TYPE_NFIELDS (union_type) = 3;
9198 TYPE_FIELDS (union_type)
9199 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9200 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9201 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9202
9203 /* Put the discriminant must at index 0. */
9204 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9205 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9206 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9207 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9208
9209 /* The order of fields doesn't really matter, so put the real
9210 field at index 1 and the data-less field at index 2. */
9211 struct discriminant_info *disc
9212 = alloc_discriminant_info (union_type, 0, 1);
9213 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9214 TYPE_FIELD_NAME (union_type, 1)
9215 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9216 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9217 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9218 TYPE_FIELD_NAME (union_type, 1));
9219
9220 const char *dataless_name
9221 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9222 name);
9223 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9224 dataless_name);
9225 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9226 /* NAME points into the original discriminant name, which
9227 already has the correct lifetime. */
9228 TYPE_FIELD_NAME (union_type, 2) = name;
9229 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9230 disc->discriminants[2] = 0;
9231
9232 /* Smash this type to be a structure type. We have to do this
9233 because the type has already been recorded. */
9234 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9235 TYPE_NFIELDS (type) = 1;
9236 TYPE_FIELDS (type)
9237 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9238
9239 /* Install the variant part. */
9240 TYPE_FIELD_TYPE (type, 0) = union_type;
9241 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9242 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9243 }
9244 /* A union with a single anonymous field is probably an old-style
9245 univariant enum. */
9246 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9247 {
9248 /* Smash this type to be a structure type. We have to do this
9249 because the type has already been recorded. */
9250 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9251
9252 /* Make a union to hold the variants. */
9253 struct type *union_type = alloc_type (objfile);
9254 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9255 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9256 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9257 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9258 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9259
9260 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9261 const char *variant_name
9262 = rust_last_path_segment (TYPE_NAME (field_type));
9263 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9264 TYPE_NAME (field_type)
9265 = rust_fully_qualify (&objfile->objfile_obstack,
9266 TYPE_NAME (type), variant_name);
9267
9268 /* Install the union in the outer struct type. */
9269 TYPE_NFIELDS (type) = 1;
9270 TYPE_FIELDS (type)
9271 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9272 TYPE_FIELD_TYPE (type, 0) = union_type;
9273 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9274 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9275
9276 alloc_discriminant_info (union_type, -1, 0);
9277 }
9278 else
9279 {
9280 struct type *disr_type = nullptr;
9281 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9282 {
9283 disr_type = TYPE_FIELD_TYPE (type, i);
9284
9285 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9286 {
9287 /* All fields of a true enum will be structs. */
9288 return;
9289 }
9290 else if (TYPE_NFIELDS (disr_type) == 0)
9291 {
9292 /* Could be data-less variant, so keep going. */
9293 disr_type = nullptr;
9294 }
9295 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9296 "RUST$ENUM$DISR") != 0)
9297 {
9298 /* Not a Rust enum. */
9299 return;
9300 }
9301 else
9302 {
9303 /* Found one. */
9304 break;
9305 }
9306 }
9307
9308 /* If we got here without a discriminant, then it's probably
9309 just a union. */
9310 if (disr_type == nullptr)
9311 return;
9312
9313 /* Smash this type to be a structure type. We have to do this
9314 because the type has already been recorded. */
9315 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9316
9317 /* Make a union to hold the variants. */
9318 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9319 struct type *union_type = alloc_type (objfile);
9320 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9321 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9322 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9323 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9324 TYPE_FIELDS (union_type)
9325 = (struct field *) TYPE_ZALLOC (union_type,
9326 (TYPE_NFIELDS (union_type)
9327 * sizeof (struct field)));
9328
9329 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9330 TYPE_NFIELDS (type) * sizeof (struct field));
9331
9332 /* Install the discriminant at index 0 in the union. */
9333 TYPE_FIELD (union_type, 0) = *disr_field;
9334 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9335 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9336
9337 /* Install the union in the outer struct type. */
9338 TYPE_FIELD_TYPE (type, 0) = union_type;
9339 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9340 TYPE_NFIELDS (type) = 1;
9341
9342 /* Set the size and offset of the union type. */
9343 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9344
9345 /* We need a way to find the correct discriminant given a
9346 variant name. For convenience we build a map here. */
9347 struct type *enum_type = FIELD_TYPE (*disr_field);
9348 std::unordered_map<std::string, ULONGEST> discriminant_map;
9349 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9350 {
9351 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9352 {
9353 const char *name
9354 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9355 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9356 }
9357 }
9358
9359 int n_fields = TYPE_NFIELDS (union_type);
9360 struct discriminant_info *disc
9361 = alloc_discriminant_info (union_type, 0, -1);
9362 /* Skip the discriminant here. */
9363 for (int i = 1; i < n_fields; ++i)
9364 {
9365 /* Find the final word in the name of this variant's type.
9366 That name can be used to look up the correct
9367 discriminant. */
9368 const char *variant_name
9369 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9370 i)));
9371
9372 auto iter = discriminant_map.find (variant_name);
9373 if (iter != discriminant_map.end ())
9374 disc->discriminants[i] = iter->second;
9375
9376 /* Remove the discriminant field, if it exists. */
9377 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9378 if (TYPE_NFIELDS (sub_type) > 0)
9379 {
9380 --TYPE_NFIELDS (sub_type);
9381 ++TYPE_FIELDS (sub_type);
9382 }
9383 TYPE_FIELD_NAME (union_type, i) = variant_name;
9384 TYPE_NAME (sub_type)
9385 = rust_fully_qualify (&objfile->objfile_obstack,
9386 TYPE_NAME (type), variant_name);
9387 }
9388 }
9389 }
9390
9391 /* Rewrite some Rust unions to be structures with variants parts. */
9392
9393 static void
9394 rust_union_quirks (struct dwarf2_cu *cu)
9395 {
9396 gdb_assert (cu->language == language_rust);
9397 for (type *type_ : cu->rust_unions)
9398 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9399 /* We don't need this any more. */
9400 cu->rust_unions.clear ();
9401 }
9402
9403 /* Return the symtab for PER_CU. This works properly regardless of
9404 whether we're using the index or psymtabs. */
9405
9406 static struct compunit_symtab *
9407 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9408 {
9409 return (per_cu->dwarf2_per_objfile->using_index
9410 ? per_cu->v.quick->compunit_symtab
9411 : per_cu->v.psymtab->compunit_symtab);
9412 }
9413
9414 /* A helper function for computing the list of all symbol tables
9415 included by PER_CU. */
9416
9417 static void
9418 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9419 htab_t all_children, htab_t all_type_symtabs,
9420 struct dwarf2_per_cu_data *per_cu,
9421 struct compunit_symtab *immediate_parent)
9422 {
9423 void **slot;
9424 struct compunit_symtab *cust;
9425
9426 slot = htab_find_slot (all_children, per_cu, INSERT);
9427 if (*slot != NULL)
9428 {
9429 /* This inclusion and its children have been processed. */
9430 return;
9431 }
9432
9433 *slot = per_cu;
9434 /* Only add a CU if it has a symbol table. */
9435 cust = get_compunit_symtab (per_cu);
9436 if (cust != NULL)
9437 {
9438 /* If this is a type unit only add its symbol table if we haven't
9439 seen it yet (type unit per_cu's can share symtabs). */
9440 if (per_cu->is_debug_types)
9441 {
9442 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9443 if (*slot == NULL)
9444 {
9445 *slot = cust;
9446 result->push_back (cust);
9447 if (cust->user == NULL)
9448 cust->user = immediate_parent;
9449 }
9450 }
9451 else
9452 {
9453 result->push_back (cust);
9454 if (cust->user == NULL)
9455 cust->user = immediate_parent;
9456 }
9457 }
9458
9459 if (!per_cu->imported_symtabs_empty ())
9460 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9461 {
9462 recursively_compute_inclusions (result, all_children,
9463 all_type_symtabs, ptr, cust);
9464 }
9465 }
9466
9467 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9468 PER_CU. */
9469
9470 static void
9471 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9472 {
9473 gdb_assert (! per_cu->is_debug_types);
9474
9475 if (!per_cu->imported_symtabs_empty ())
9476 {
9477 int len;
9478 std::vector<compunit_symtab *> result_symtabs;
9479 htab_t all_children, all_type_symtabs;
9480 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9481
9482 /* If we don't have a symtab, we can just skip this case. */
9483 if (cust == NULL)
9484 return;
9485
9486 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9487 NULL, xcalloc, xfree);
9488 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9489 NULL, xcalloc, xfree);
9490
9491 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9492 {
9493 recursively_compute_inclusions (&result_symtabs, all_children,
9494 all_type_symtabs, ptr, cust);
9495 }
9496
9497 /* Now we have a transitive closure of all the included symtabs. */
9498 len = result_symtabs.size ();
9499 cust->includes
9500 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9501 struct compunit_symtab *, len + 1);
9502 memcpy (cust->includes, result_symtabs.data (),
9503 len * sizeof (compunit_symtab *));
9504 cust->includes[len] = NULL;
9505
9506 htab_delete (all_children);
9507 htab_delete (all_type_symtabs);
9508 }
9509 }
9510
9511 /* Compute the 'includes' field for the symtabs of all the CUs we just
9512 read. */
9513
9514 static void
9515 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9516 {
9517 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9518 {
9519 if (! iter->is_debug_types)
9520 compute_compunit_symtab_includes (iter);
9521 }
9522
9523 dwarf2_per_objfile->just_read_cus.clear ();
9524 }
9525
9526 /* Generate full symbol information for PER_CU, whose DIEs have
9527 already been loaded into memory. */
9528
9529 static void
9530 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9531 enum language pretend_language)
9532 {
9533 struct dwarf2_cu *cu = per_cu->cu;
9534 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9535 struct objfile *objfile = dwarf2_per_objfile->objfile;
9536 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9537 CORE_ADDR lowpc, highpc;
9538 struct compunit_symtab *cust;
9539 CORE_ADDR baseaddr;
9540 struct block *static_block;
9541 CORE_ADDR addr;
9542
9543 baseaddr = objfile->text_section_offset ();
9544
9545 /* Clear the list here in case something was left over. */
9546 cu->method_list.clear ();
9547
9548 cu->language = pretend_language;
9549 cu->language_defn = language_def (cu->language);
9550
9551 /* Do line number decoding in read_file_scope () */
9552 process_die (cu->dies, cu);
9553
9554 /* For now fudge the Go package. */
9555 if (cu->language == language_go)
9556 fixup_go_packaging (cu);
9557
9558 /* Now that we have processed all the DIEs in the CU, all the types
9559 should be complete, and it should now be safe to compute all of the
9560 physnames. */
9561 compute_delayed_physnames (cu);
9562
9563 if (cu->language == language_rust)
9564 rust_union_quirks (cu);
9565
9566 /* Some compilers don't define a DW_AT_high_pc attribute for the
9567 compilation unit. If the DW_AT_high_pc is missing, synthesize
9568 it, by scanning the DIE's below the compilation unit. */
9569 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9570
9571 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9572 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9573
9574 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9575 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9576 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9577 addrmap to help ensure it has an accurate map of pc values belonging to
9578 this comp unit. */
9579 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9580
9581 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9582 SECT_OFF_TEXT (objfile),
9583 0);
9584
9585 if (cust != NULL)
9586 {
9587 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9588
9589 /* Set symtab language to language from DW_AT_language. If the
9590 compilation is from a C file generated by language preprocessors, do
9591 not set the language if it was already deduced by start_subfile. */
9592 if (!(cu->language == language_c
9593 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9594 COMPUNIT_FILETABS (cust)->language = cu->language;
9595
9596 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9597 produce DW_AT_location with location lists but it can be possibly
9598 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9599 there were bugs in prologue debug info, fixed later in GCC-4.5
9600 by "unwind info for epilogues" patch (which is not directly related).
9601
9602 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9603 needed, it would be wrong due to missing DW_AT_producer there.
9604
9605 Still one can confuse GDB by using non-standard GCC compilation
9606 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9607 */
9608 if (cu->has_loclist && gcc_4_minor >= 5)
9609 cust->locations_valid = 1;
9610
9611 if (gcc_4_minor >= 5)
9612 cust->epilogue_unwind_valid = 1;
9613
9614 cust->call_site_htab = cu->call_site_htab;
9615 }
9616
9617 if (dwarf2_per_objfile->using_index)
9618 per_cu->v.quick->compunit_symtab = cust;
9619 else
9620 {
9621 dwarf2_psymtab *pst = per_cu->v.psymtab;
9622 pst->compunit_symtab = cust;
9623 pst->readin = true;
9624 }
9625
9626 /* Push it for inclusion processing later. */
9627 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9628
9629 /* Not needed any more. */
9630 cu->reset_builder ();
9631 }
9632
9633 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9634 already been loaded into memory. */
9635
9636 static void
9637 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9638 enum language pretend_language)
9639 {
9640 struct dwarf2_cu *cu = per_cu->cu;
9641 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9642 struct objfile *objfile = dwarf2_per_objfile->objfile;
9643 struct compunit_symtab *cust;
9644 struct signatured_type *sig_type;
9645
9646 gdb_assert (per_cu->is_debug_types);
9647 sig_type = (struct signatured_type *) per_cu;
9648
9649 /* Clear the list here in case something was left over. */
9650 cu->method_list.clear ();
9651
9652 cu->language = pretend_language;
9653 cu->language_defn = language_def (cu->language);
9654
9655 /* The symbol tables are set up in read_type_unit_scope. */
9656 process_die (cu->dies, cu);
9657
9658 /* For now fudge the Go package. */
9659 if (cu->language == language_go)
9660 fixup_go_packaging (cu);
9661
9662 /* Now that we have processed all the DIEs in the CU, all the types
9663 should be complete, and it should now be safe to compute all of the
9664 physnames. */
9665 compute_delayed_physnames (cu);
9666
9667 if (cu->language == language_rust)
9668 rust_union_quirks (cu);
9669
9670 /* TUs share symbol tables.
9671 If this is the first TU to use this symtab, complete the construction
9672 of it with end_expandable_symtab. Otherwise, complete the addition of
9673 this TU's symbols to the existing symtab. */
9674 if (sig_type->type_unit_group->compunit_symtab == NULL)
9675 {
9676 buildsym_compunit *builder = cu->get_builder ();
9677 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9678 sig_type->type_unit_group->compunit_symtab = cust;
9679
9680 if (cust != NULL)
9681 {
9682 /* Set symtab language to language from DW_AT_language. If the
9683 compilation is from a C file generated by language preprocessors,
9684 do not set the language if it was already deduced by
9685 start_subfile. */
9686 if (!(cu->language == language_c
9687 && COMPUNIT_FILETABS (cust)->language != language_c))
9688 COMPUNIT_FILETABS (cust)->language = cu->language;
9689 }
9690 }
9691 else
9692 {
9693 cu->get_builder ()->augment_type_symtab ();
9694 cust = sig_type->type_unit_group->compunit_symtab;
9695 }
9696
9697 if (dwarf2_per_objfile->using_index)
9698 per_cu->v.quick->compunit_symtab = cust;
9699 else
9700 {
9701 dwarf2_psymtab *pst = per_cu->v.psymtab;
9702 pst->compunit_symtab = cust;
9703 pst->readin = true;
9704 }
9705
9706 /* Not needed any more. */
9707 cu->reset_builder ();
9708 }
9709
9710 /* Process an imported unit DIE. */
9711
9712 static void
9713 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9714 {
9715 struct attribute *attr;
9716
9717 /* For now we don't handle imported units in type units. */
9718 if (cu->per_cu->is_debug_types)
9719 {
9720 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9721 " supported in type units [in module %s]"),
9722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9723 }
9724
9725 attr = dwarf2_attr (die, DW_AT_import, cu);
9726 if (attr != NULL)
9727 {
9728 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9729 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9730 dwarf2_per_cu_data *per_cu
9731 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9732 cu->per_cu->dwarf2_per_objfile);
9733
9734 /* If necessary, add it to the queue and load its DIEs. */
9735 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9736 load_full_comp_unit (per_cu, false, cu->language);
9737
9738 cu->per_cu->imported_symtabs_push (per_cu);
9739 }
9740 }
9741
9742 /* RAII object that represents a process_die scope: i.e.,
9743 starts/finishes processing a DIE. */
9744 class process_die_scope
9745 {
9746 public:
9747 process_die_scope (die_info *die, dwarf2_cu *cu)
9748 : m_die (die), m_cu (cu)
9749 {
9750 /* We should only be processing DIEs not already in process. */
9751 gdb_assert (!m_die->in_process);
9752 m_die->in_process = true;
9753 }
9754
9755 ~process_die_scope ()
9756 {
9757 m_die->in_process = false;
9758
9759 /* If we're done processing the DIE for the CU that owns the line
9760 header, we don't need the line header anymore. */
9761 if (m_cu->line_header_die_owner == m_die)
9762 {
9763 delete m_cu->line_header;
9764 m_cu->line_header = NULL;
9765 m_cu->line_header_die_owner = NULL;
9766 }
9767 }
9768
9769 private:
9770 die_info *m_die;
9771 dwarf2_cu *m_cu;
9772 };
9773
9774 /* Process a die and its children. */
9775
9776 static void
9777 process_die (struct die_info *die, struct dwarf2_cu *cu)
9778 {
9779 process_die_scope scope (die, cu);
9780
9781 switch (die->tag)
9782 {
9783 case DW_TAG_padding:
9784 break;
9785 case DW_TAG_compile_unit:
9786 case DW_TAG_partial_unit:
9787 read_file_scope (die, cu);
9788 break;
9789 case DW_TAG_type_unit:
9790 read_type_unit_scope (die, cu);
9791 break;
9792 case DW_TAG_subprogram:
9793 /* Nested subprograms in Fortran get a prefix. */
9794 if (cu->language == language_fortran
9795 && die->parent != NULL
9796 && die->parent->tag == DW_TAG_subprogram)
9797 cu->processing_has_namespace_info = true;
9798 /* Fall through. */
9799 case DW_TAG_inlined_subroutine:
9800 read_func_scope (die, cu);
9801 break;
9802 case DW_TAG_lexical_block:
9803 case DW_TAG_try_block:
9804 case DW_TAG_catch_block:
9805 read_lexical_block_scope (die, cu);
9806 break;
9807 case DW_TAG_call_site:
9808 case DW_TAG_GNU_call_site:
9809 read_call_site_scope (die, cu);
9810 break;
9811 case DW_TAG_class_type:
9812 case DW_TAG_interface_type:
9813 case DW_TAG_structure_type:
9814 case DW_TAG_union_type:
9815 process_structure_scope (die, cu);
9816 break;
9817 case DW_TAG_enumeration_type:
9818 process_enumeration_scope (die, cu);
9819 break;
9820
9821 /* These dies have a type, but processing them does not create
9822 a symbol or recurse to process the children. Therefore we can
9823 read them on-demand through read_type_die. */
9824 case DW_TAG_subroutine_type:
9825 case DW_TAG_set_type:
9826 case DW_TAG_array_type:
9827 case DW_TAG_pointer_type:
9828 case DW_TAG_ptr_to_member_type:
9829 case DW_TAG_reference_type:
9830 case DW_TAG_rvalue_reference_type:
9831 case DW_TAG_string_type:
9832 break;
9833
9834 case DW_TAG_base_type:
9835 case DW_TAG_subrange_type:
9836 case DW_TAG_typedef:
9837 /* Add a typedef symbol for the type definition, if it has a
9838 DW_AT_name. */
9839 new_symbol (die, read_type_die (die, cu), cu);
9840 break;
9841 case DW_TAG_common_block:
9842 read_common_block (die, cu);
9843 break;
9844 case DW_TAG_common_inclusion:
9845 break;
9846 case DW_TAG_namespace:
9847 cu->processing_has_namespace_info = true;
9848 read_namespace (die, cu);
9849 break;
9850 case DW_TAG_module:
9851 cu->processing_has_namespace_info = true;
9852 read_module (die, cu);
9853 break;
9854 case DW_TAG_imported_declaration:
9855 cu->processing_has_namespace_info = true;
9856 if (read_namespace_alias (die, cu))
9857 break;
9858 /* The declaration is not a global namespace alias. */
9859 /* Fall through. */
9860 case DW_TAG_imported_module:
9861 cu->processing_has_namespace_info = true;
9862 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9863 || cu->language != language_fortran))
9864 complaint (_("Tag '%s' has unexpected children"),
9865 dwarf_tag_name (die->tag));
9866 read_import_statement (die, cu);
9867 break;
9868
9869 case DW_TAG_imported_unit:
9870 process_imported_unit_die (die, cu);
9871 break;
9872
9873 case DW_TAG_variable:
9874 read_variable (die, cu);
9875 break;
9876
9877 default:
9878 new_symbol (die, NULL, cu);
9879 break;
9880 }
9881 }
9882 \f
9883 /* DWARF name computation. */
9884
9885 /* A helper function for dwarf2_compute_name which determines whether DIE
9886 needs to have the name of the scope prepended to the name listed in the
9887 die. */
9888
9889 static int
9890 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9891 {
9892 struct attribute *attr;
9893
9894 switch (die->tag)
9895 {
9896 case DW_TAG_namespace:
9897 case DW_TAG_typedef:
9898 case DW_TAG_class_type:
9899 case DW_TAG_interface_type:
9900 case DW_TAG_structure_type:
9901 case DW_TAG_union_type:
9902 case DW_TAG_enumeration_type:
9903 case DW_TAG_enumerator:
9904 case DW_TAG_subprogram:
9905 case DW_TAG_inlined_subroutine:
9906 case DW_TAG_member:
9907 case DW_TAG_imported_declaration:
9908 return 1;
9909
9910 case DW_TAG_variable:
9911 case DW_TAG_constant:
9912 /* We only need to prefix "globally" visible variables. These include
9913 any variable marked with DW_AT_external or any variable that
9914 lives in a namespace. [Variables in anonymous namespaces
9915 require prefixing, but they are not DW_AT_external.] */
9916
9917 if (dwarf2_attr (die, DW_AT_specification, cu))
9918 {
9919 struct dwarf2_cu *spec_cu = cu;
9920
9921 return die_needs_namespace (die_specification (die, &spec_cu),
9922 spec_cu);
9923 }
9924
9925 attr = dwarf2_attr (die, DW_AT_external, cu);
9926 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9927 && die->parent->tag != DW_TAG_module)
9928 return 0;
9929 /* A variable in a lexical block of some kind does not need a
9930 namespace, even though in C++ such variables may be external
9931 and have a mangled name. */
9932 if (die->parent->tag == DW_TAG_lexical_block
9933 || die->parent->tag == DW_TAG_try_block
9934 || die->parent->tag == DW_TAG_catch_block
9935 || die->parent->tag == DW_TAG_subprogram)
9936 return 0;
9937 return 1;
9938
9939 default:
9940 return 0;
9941 }
9942 }
9943
9944 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9945 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9946 defined for the given DIE. */
9947
9948 static struct attribute *
9949 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9950 {
9951 struct attribute *attr;
9952
9953 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9954 if (attr == NULL)
9955 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9956
9957 return attr;
9958 }
9959
9960 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9961 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9962 defined for the given DIE. */
9963
9964 static const char *
9965 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9966 {
9967 const char *linkage_name;
9968
9969 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9970 if (linkage_name == NULL)
9971 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9972
9973 return linkage_name;
9974 }
9975
9976 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9977 compute the physname for the object, which include a method's:
9978 - formal parameters (C++),
9979 - receiver type (Go),
9980
9981 The term "physname" is a bit confusing.
9982 For C++, for example, it is the demangled name.
9983 For Go, for example, it's the mangled name.
9984
9985 For Ada, return the DIE's linkage name rather than the fully qualified
9986 name. PHYSNAME is ignored..
9987
9988 The result is allocated on the objfile_obstack and canonicalized. */
9989
9990 static const char *
9991 dwarf2_compute_name (const char *name,
9992 struct die_info *die, struct dwarf2_cu *cu,
9993 int physname)
9994 {
9995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9996
9997 if (name == NULL)
9998 name = dwarf2_name (die, cu);
9999
10000 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10001 but otherwise compute it by typename_concat inside GDB.
10002 FIXME: Actually this is not really true, or at least not always true.
10003 It's all very confusing. compute_and_set_names doesn't try to demangle
10004 Fortran names because there is no mangling standard. So new_symbol
10005 will set the demangled name to the result of dwarf2_full_name, and it is
10006 the demangled name that GDB uses if it exists. */
10007 if (cu->language == language_ada
10008 || (cu->language == language_fortran && physname))
10009 {
10010 /* For Ada unit, we prefer the linkage name over the name, as
10011 the former contains the exported name, which the user expects
10012 to be able to reference. Ideally, we want the user to be able
10013 to reference this entity using either natural or linkage name,
10014 but we haven't started looking at this enhancement yet. */
10015 const char *linkage_name = dw2_linkage_name (die, cu);
10016
10017 if (linkage_name != NULL)
10018 return linkage_name;
10019 }
10020
10021 /* These are the only languages we know how to qualify names in. */
10022 if (name != NULL
10023 && (cu->language == language_cplus
10024 || cu->language == language_fortran || cu->language == language_d
10025 || cu->language == language_rust))
10026 {
10027 if (die_needs_namespace (die, cu))
10028 {
10029 const char *prefix;
10030 const char *canonical_name = NULL;
10031
10032 string_file buf;
10033
10034 prefix = determine_prefix (die, cu);
10035 if (*prefix != '\0')
10036 {
10037 gdb::unique_xmalloc_ptr<char> prefixed_name
10038 (typename_concat (NULL, prefix, name, physname, cu));
10039
10040 buf.puts (prefixed_name.get ());
10041 }
10042 else
10043 buf.puts (name);
10044
10045 /* Template parameters may be specified in the DIE's DW_AT_name, or
10046 as children with DW_TAG_template_type_param or
10047 DW_TAG_value_type_param. If the latter, add them to the name
10048 here. If the name already has template parameters, then
10049 skip this step; some versions of GCC emit both, and
10050 it is more efficient to use the pre-computed name.
10051
10052 Something to keep in mind about this process: it is very
10053 unlikely, or in some cases downright impossible, to produce
10054 something that will match the mangled name of a function.
10055 If the definition of the function has the same debug info,
10056 we should be able to match up with it anyway. But fallbacks
10057 using the minimal symbol, for instance to find a method
10058 implemented in a stripped copy of libstdc++, will not work.
10059 If we do not have debug info for the definition, we will have to
10060 match them up some other way.
10061
10062 When we do name matching there is a related problem with function
10063 templates; two instantiated function templates are allowed to
10064 differ only by their return types, which we do not add here. */
10065
10066 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10067 {
10068 struct attribute *attr;
10069 struct die_info *child;
10070 int first = 1;
10071
10072 die->building_fullname = 1;
10073
10074 for (child = die->child; child != NULL; child = child->sibling)
10075 {
10076 struct type *type;
10077 LONGEST value;
10078 const gdb_byte *bytes;
10079 struct dwarf2_locexpr_baton *baton;
10080 struct value *v;
10081
10082 if (child->tag != DW_TAG_template_type_param
10083 && child->tag != DW_TAG_template_value_param)
10084 continue;
10085
10086 if (first)
10087 {
10088 buf.puts ("<");
10089 first = 0;
10090 }
10091 else
10092 buf.puts (", ");
10093
10094 attr = dwarf2_attr (child, DW_AT_type, cu);
10095 if (attr == NULL)
10096 {
10097 complaint (_("template parameter missing DW_AT_type"));
10098 buf.puts ("UNKNOWN_TYPE");
10099 continue;
10100 }
10101 type = die_type (child, cu);
10102
10103 if (child->tag == DW_TAG_template_type_param)
10104 {
10105 c_print_type (type, "", &buf, -1, 0, cu->language,
10106 &type_print_raw_options);
10107 continue;
10108 }
10109
10110 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10111 if (attr == NULL)
10112 {
10113 complaint (_("template parameter missing "
10114 "DW_AT_const_value"));
10115 buf.puts ("UNKNOWN_VALUE");
10116 continue;
10117 }
10118
10119 dwarf2_const_value_attr (attr, type, name,
10120 &cu->comp_unit_obstack, cu,
10121 &value, &bytes, &baton);
10122
10123 if (TYPE_NOSIGN (type))
10124 /* GDB prints characters as NUMBER 'CHAR'. If that's
10125 changed, this can use value_print instead. */
10126 c_printchar (value, type, &buf);
10127 else
10128 {
10129 struct value_print_options opts;
10130
10131 if (baton != NULL)
10132 v = dwarf2_evaluate_loc_desc (type, NULL,
10133 baton->data,
10134 baton->size,
10135 baton->per_cu);
10136 else if (bytes != NULL)
10137 {
10138 v = allocate_value (type);
10139 memcpy (value_contents_writeable (v), bytes,
10140 TYPE_LENGTH (type));
10141 }
10142 else
10143 v = value_from_longest (type, value);
10144
10145 /* Specify decimal so that we do not depend on
10146 the radix. */
10147 get_formatted_print_options (&opts, 'd');
10148 opts.raw = 1;
10149 value_print (v, &buf, &opts);
10150 release_value (v);
10151 }
10152 }
10153
10154 die->building_fullname = 0;
10155
10156 if (!first)
10157 {
10158 /* Close the argument list, with a space if necessary
10159 (nested templates). */
10160 if (!buf.empty () && buf.string ().back () == '>')
10161 buf.puts (" >");
10162 else
10163 buf.puts (">");
10164 }
10165 }
10166
10167 /* For C++ methods, append formal parameter type
10168 information, if PHYSNAME. */
10169
10170 if (physname && die->tag == DW_TAG_subprogram
10171 && cu->language == language_cplus)
10172 {
10173 struct type *type = read_type_die (die, cu);
10174
10175 c_type_print_args (type, &buf, 1, cu->language,
10176 &type_print_raw_options);
10177
10178 if (cu->language == language_cplus)
10179 {
10180 /* Assume that an artificial first parameter is
10181 "this", but do not crash if it is not. RealView
10182 marks unnamed (and thus unused) parameters as
10183 artificial; there is no way to differentiate
10184 the two cases. */
10185 if (TYPE_NFIELDS (type) > 0
10186 && TYPE_FIELD_ARTIFICIAL (type, 0)
10187 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10188 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10189 0))))
10190 buf.puts (" const");
10191 }
10192 }
10193
10194 const std::string &intermediate_name = buf.string ();
10195
10196 if (cu->language == language_cplus)
10197 canonical_name
10198 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10199 &objfile->per_bfd->storage_obstack);
10200
10201 /* If we only computed INTERMEDIATE_NAME, or if
10202 INTERMEDIATE_NAME is already canonical, then we need to
10203 copy it to the appropriate obstack. */
10204 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10205 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10206 intermediate_name);
10207 else
10208 name = canonical_name;
10209 }
10210 }
10211
10212 return name;
10213 }
10214
10215 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10216 If scope qualifiers are appropriate they will be added. The result
10217 will be allocated on the storage_obstack, or NULL if the DIE does
10218 not have a name. NAME may either be from a previous call to
10219 dwarf2_name or NULL.
10220
10221 The output string will be canonicalized (if C++). */
10222
10223 static const char *
10224 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10225 {
10226 return dwarf2_compute_name (name, die, cu, 0);
10227 }
10228
10229 /* Construct a physname for the given DIE in CU. NAME may either be
10230 from a previous call to dwarf2_name or NULL. The result will be
10231 allocated on the objfile_objstack or NULL if the DIE does not have a
10232 name.
10233
10234 The output string will be canonicalized (if C++). */
10235
10236 static const char *
10237 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10238 {
10239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10240 const char *retval, *mangled = NULL, *canon = NULL;
10241 int need_copy = 1;
10242
10243 /* In this case dwarf2_compute_name is just a shortcut not building anything
10244 on its own. */
10245 if (!die_needs_namespace (die, cu))
10246 return dwarf2_compute_name (name, die, cu, 1);
10247
10248 mangled = dw2_linkage_name (die, cu);
10249
10250 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10251 See https://github.com/rust-lang/rust/issues/32925. */
10252 if (cu->language == language_rust && mangled != NULL
10253 && strchr (mangled, '{') != NULL)
10254 mangled = NULL;
10255
10256 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10257 has computed. */
10258 gdb::unique_xmalloc_ptr<char> demangled;
10259 if (mangled != NULL)
10260 {
10261
10262 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10263 {
10264 /* Do nothing (do not demangle the symbol name). */
10265 }
10266 else if (cu->language == language_go)
10267 {
10268 /* This is a lie, but we already lie to the caller new_symbol.
10269 new_symbol assumes we return the mangled name.
10270 This just undoes that lie until things are cleaned up. */
10271 }
10272 else
10273 {
10274 /* Use DMGL_RET_DROP for C++ template functions to suppress
10275 their return type. It is easier for GDB users to search
10276 for such functions as `name(params)' than `long name(params)'.
10277 In such case the minimal symbol names do not match the full
10278 symbol names but for template functions there is never a need
10279 to look up their definition from their declaration so
10280 the only disadvantage remains the minimal symbol variant
10281 `long name(params)' does not have the proper inferior type. */
10282 demangled.reset (gdb_demangle (mangled,
10283 (DMGL_PARAMS | DMGL_ANSI
10284 | DMGL_RET_DROP)));
10285 }
10286 if (demangled)
10287 canon = demangled.get ();
10288 else
10289 {
10290 canon = mangled;
10291 need_copy = 0;
10292 }
10293 }
10294
10295 if (canon == NULL || check_physname)
10296 {
10297 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10298
10299 if (canon != NULL && strcmp (physname, canon) != 0)
10300 {
10301 /* It may not mean a bug in GDB. The compiler could also
10302 compute DW_AT_linkage_name incorrectly. But in such case
10303 GDB would need to be bug-to-bug compatible. */
10304
10305 complaint (_("Computed physname <%s> does not match demangled <%s> "
10306 "(from linkage <%s>) - DIE at %s [in module %s]"),
10307 physname, canon, mangled, sect_offset_str (die->sect_off),
10308 objfile_name (objfile));
10309
10310 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10311 is available here - over computed PHYSNAME. It is safer
10312 against both buggy GDB and buggy compilers. */
10313
10314 retval = canon;
10315 }
10316 else
10317 {
10318 retval = physname;
10319 need_copy = 0;
10320 }
10321 }
10322 else
10323 retval = canon;
10324
10325 if (need_copy)
10326 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10327
10328 return retval;
10329 }
10330
10331 /* Inspect DIE in CU for a namespace alias. If one exists, record
10332 a new symbol for it.
10333
10334 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10335
10336 static int
10337 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10338 {
10339 struct attribute *attr;
10340
10341 /* If the die does not have a name, this is not a namespace
10342 alias. */
10343 attr = dwarf2_attr (die, DW_AT_name, cu);
10344 if (attr != NULL)
10345 {
10346 int num;
10347 struct die_info *d = die;
10348 struct dwarf2_cu *imported_cu = cu;
10349
10350 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10351 keep inspecting DIEs until we hit the underlying import. */
10352 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10353 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10354 {
10355 attr = dwarf2_attr (d, DW_AT_import, cu);
10356 if (attr == NULL)
10357 break;
10358
10359 d = follow_die_ref (d, attr, &imported_cu);
10360 if (d->tag != DW_TAG_imported_declaration)
10361 break;
10362 }
10363
10364 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10365 {
10366 complaint (_("DIE at %s has too many recursively imported "
10367 "declarations"), sect_offset_str (d->sect_off));
10368 return 0;
10369 }
10370
10371 if (attr != NULL)
10372 {
10373 struct type *type;
10374 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10375
10376 type = get_die_type_at_offset (sect_off, cu->per_cu);
10377 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10378 {
10379 /* This declaration is a global namespace alias. Add
10380 a symbol for it whose type is the aliased namespace. */
10381 new_symbol (die, type, cu);
10382 return 1;
10383 }
10384 }
10385 }
10386
10387 return 0;
10388 }
10389
10390 /* Return the using directives repository (global or local?) to use in the
10391 current context for CU.
10392
10393 For Ada, imported declarations can materialize renamings, which *may* be
10394 global. However it is impossible (for now?) in DWARF to distinguish
10395 "external" imported declarations and "static" ones. As all imported
10396 declarations seem to be static in all other languages, make them all CU-wide
10397 global only in Ada. */
10398
10399 static struct using_direct **
10400 using_directives (struct dwarf2_cu *cu)
10401 {
10402 if (cu->language == language_ada
10403 && cu->get_builder ()->outermost_context_p ())
10404 return cu->get_builder ()->get_global_using_directives ();
10405 else
10406 return cu->get_builder ()->get_local_using_directives ();
10407 }
10408
10409 /* Read the import statement specified by the given die and record it. */
10410
10411 static void
10412 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10413 {
10414 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10415 struct attribute *import_attr;
10416 struct die_info *imported_die, *child_die;
10417 struct dwarf2_cu *imported_cu;
10418 const char *imported_name;
10419 const char *imported_name_prefix;
10420 const char *canonical_name;
10421 const char *import_alias;
10422 const char *imported_declaration = NULL;
10423 const char *import_prefix;
10424 std::vector<const char *> excludes;
10425
10426 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10427 if (import_attr == NULL)
10428 {
10429 complaint (_("Tag '%s' has no DW_AT_import"),
10430 dwarf_tag_name (die->tag));
10431 return;
10432 }
10433
10434 imported_cu = cu;
10435 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10436 imported_name = dwarf2_name (imported_die, imported_cu);
10437 if (imported_name == NULL)
10438 {
10439 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10440
10441 The import in the following code:
10442 namespace A
10443 {
10444 typedef int B;
10445 }
10446
10447 int main ()
10448 {
10449 using A::B;
10450 B b;
10451 return b;
10452 }
10453
10454 ...
10455 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10456 <52> DW_AT_decl_file : 1
10457 <53> DW_AT_decl_line : 6
10458 <54> DW_AT_import : <0x75>
10459 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10460 <59> DW_AT_name : B
10461 <5b> DW_AT_decl_file : 1
10462 <5c> DW_AT_decl_line : 2
10463 <5d> DW_AT_type : <0x6e>
10464 ...
10465 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10466 <76> DW_AT_byte_size : 4
10467 <77> DW_AT_encoding : 5 (signed)
10468
10469 imports the wrong die ( 0x75 instead of 0x58 ).
10470 This case will be ignored until the gcc bug is fixed. */
10471 return;
10472 }
10473
10474 /* Figure out the local name after import. */
10475 import_alias = dwarf2_name (die, cu);
10476
10477 /* Figure out where the statement is being imported to. */
10478 import_prefix = determine_prefix (die, cu);
10479
10480 /* Figure out what the scope of the imported die is and prepend it
10481 to the name of the imported die. */
10482 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10483
10484 if (imported_die->tag != DW_TAG_namespace
10485 && imported_die->tag != DW_TAG_module)
10486 {
10487 imported_declaration = imported_name;
10488 canonical_name = imported_name_prefix;
10489 }
10490 else if (strlen (imported_name_prefix) > 0)
10491 canonical_name = obconcat (&objfile->objfile_obstack,
10492 imported_name_prefix,
10493 (cu->language == language_d ? "." : "::"),
10494 imported_name, (char *) NULL);
10495 else
10496 canonical_name = imported_name;
10497
10498 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10499 for (child_die = die->child; child_die && child_die->tag;
10500 child_die = sibling_die (child_die))
10501 {
10502 /* DWARF-4: A Fortran use statement with a “rename list” may be
10503 represented by an imported module entry with an import attribute
10504 referring to the module and owned entries corresponding to those
10505 entities that are renamed as part of being imported. */
10506
10507 if (child_die->tag != DW_TAG_imported_declaration)
10508 {
10509 complaint (_("child DW_TAG_imported_declaration expected "
10510 "- DIE at %s [in module %s]"),
10511 sect_offset_str (child_die->sect_off),
10512 objfile_name (objfile));
10513 continue;
10514 }
10515
10516 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10517 if (import_attr == NULL)
10518 {
10519 complaint (_("Tag '%s' has no DW_AT_import"),
10520 dwarf_tag_name (child_die->tag));
10521 continue;
10522 }
10523
10524 imported_cu = cu;
10525 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10526 &imported_cu);
10527 imported_name = dwarf2_name (imported_die, imported_cu);
10528 if (imported_name == NULL)
10529 {
10530 complaint (_("child DW_TAG_imported_declaration has unknown "
10531 "imported name - DIE at %s [in module %s]"),
10532 sect_offset_str (child_die->sect_off),
10533 objfile_name (objfile));
10534 continue;
10535 }
10536
10537 excludes.push_back (imported_name);
10538
10539 process_die (child_die, cu);
10540 }
10541
10542 add_using_directive (using_directives (cu),
10543 import_prefix,
10544 canonical_name,
10545 import_alias,
10546 imported_declaration,
10547 excludes,
10548 0,
10549 &objfile->objfile_obstack);
10550 }
10551
10552 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10553 types, but gives them a size of zero. Starting with version 14,
10554 ICC is compatible with GCC. */
10555
10556 static bool
10557 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10558 {
10559 if (!cu->checked_producer)
10560 check_producer (cu);
10561
10562 return cu->producer_is_icc_lt_14;
10563 }
10564
10565 /* ICC generates a DW_AT_type for C void functions. This was observed on
10566 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10567 which says that void functions should not have a DW_AT_type. */
10568
10569 static bool
10570 producer_is_icc (struct dwarf2_cu *cu)
10571 {
10572 if (!cu->checked_producer)
10573 check_producer (cu);
10574
10575 return cu->producer_is_icc;
10576 }
10577
10578 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10579 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10580 this, it was first present in GCC release 4.3.0. */
10581
10582 static bool
10583 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10584 {
10585 if (!cu->checked_producer)
10586 check_producer (cu);
10587
10588 return cu->producer_is_gcc_lt_4_3;
10589 }
10590
10591 static file_and_directory
10592 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10593 {
10594 file_and_directory res;
10595
10596 /* Find the filename. Do not use dwarf2_name here, since the filename
10597 is not a source language identifier. */
10598 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10599 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10600
10601 if (res.comp_dir == NULL
10602 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10603 && IS_ABSOLUTE_PATH (res.name))
10604 {
10605 res.comp_dir_storage = ldirname (res.name);
10606 if (!res.comp_dir_storage.empty ())
10607 res.comp_dir = res.comp_dir_storage.c_str ();
10608 }
10609 if (res.comp_dir != NULL)
10610 {
10611 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10612 directory, get rid of it. */
10613 const char *cp = strchr (res.comp_dir, ':');
10614
10615 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10616 res.comp_dir = cp + 1;
10617 }
10618
10619 if (res.name == NULL)
10620 res.name = "<unknown>";
10621
10622 return res;
10623 }
10624
10625 /* Handle DW_AT_stmt_list for a compilation unit.
10626 DIE is the DW_TAG_compile_unit die for CU.
10627 COMP_DIR is the compilation directory. LOWPC is passed to
10628 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10629
10630 static void
10631 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10632 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10633 {
10634 struct dwarf2_per_objfile *dwarf2_per_objfile
10635 = cu->per_cu->dwarf2_per_objfile;
10636 struct attribute *attr;
10637 struct line_header line_header_local;
10638 hashval_t line_header_local_hash;
10639 void **slot;
10640 int decode_mapping;
10641
10642 gdb_assert (! cu->per_cu->is_debug_types);
10643
10644 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10645 if (attr == NULL)
10646 return;
10647
10648 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10649
10650 /* The line header hash table is only created if needed (it exists to
10651 prevent redundant reading of the line table for partial_units).
10652 If we're given a partial_unit, we'll need it. If we're given a
10653 compile_unit, then use the line header hash table if it's already
10654 created, but don't create one just yet. */
10655
10656 if (dwarf2_per_objfile->line_header_hash == NULL
10657 && die->tag == DW_TAG_partial_unit)
10658 {
10659 dwarf2_per_objfile->line_header_hash
10660 .reset (htab_create_alloc (127, line_header_hash_voidp,
10661 line_header_eq_voidp,
10662 free_line_header_voidp,
10663 xcalloc, xfree));
10664 }
10665
10666 line_header_local.sect_off = line_offset;
10667 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10668 line_header_local_hash = line_header_hash (&line_header_local);
10669 if (dwarf2_per_objfile->line_header_hash != NULL)
10670 {
10671 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10672 &line_header_local,
10673 line_header_local_hash, NO_INSERT);
10674
10675 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10676 is not present in *SLOT (since if there is something in *SLOT then
10677 it will be for a partial_unit). */
10678 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10679 {
10680 gdb_assert (*slot != NULL);
10681 cu->line_header = (struct line_header *) *slot;
10682 return;
10683 }
10684 }
10685
10686 /* dwarf_decode_line_header does not yet provide sufficient information.
10687 We always have to call also dwarf_decode_lines for it. */
10688 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10689 if (lh == NULL)
10690 return;
10691
10692 cu->line_header = lh.release ();
10693 cu->line_header_die_owner = die;
10694
10695 if (dwarf2_per_objfile->line_header_hash == NULL)
10696 slot = NULL;
10697 else
10698 {
10699 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10700 &line_header_local,
10701 line_header_local_hash, INSERT);
10702 gdb_assert (slot != NULL);
10703 }
10704 if (slot != NULL && *slot == NULL)
10705 {
10706 /* This newly decoded line number information unit will be owned
10707 by line_header_hash hash table. */
10708 *slot = cu->line_header;
10709 cu->line_header_die_owner = NULL;
10710 }
10711 else
10712 {
10713 /* We cannot free any current entry in (*slot) as that struct line_header
10714 may be already used by multiple CUs. Create only temporary decoded
10715 line_header for this CU - it may happen at most once for each line
10716 number information unit. And if we're not using line_header_hash
10717 then this is what we want as well. */
10718 gdb_assert (die->tag != DW_TAG_partial_unit);
10719 }
10720 decode_mapping = (die->tag != DW_TAG_partial_unit);
10721 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10722 decode_mapping);
10723
10724 }
10725
10726 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10727
10728 static void
10729 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10730 {
10731 struct dwarf2_per_objfile *dwarf2_per_objfile
10732 = cu->per_cu->dwarf2_per_objfile;
10733 struct objfile *objfile = dwarf2_per_objfile->objfile;
10734 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10735 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10736 CORE_ADDR highpc = ((CORE_ADDR) 0);
10737 struct attribute *attr;
10738 struct die_info *child_die;
10739 CORE_ADDR baseaddr;
10740
10741 prepare_one_comp_unit (cu, die, cu->language);
10742 baseaddr = objfile->text_section_offset ();
10743
10744 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10745
10746 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10747 from finish_block. */
10748 if (lowpc == ((CORE_ADDR) -1))
10749 lowpc = highpc;
10750 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10751
10752 file_and_directory fnd = find_file_and_directory (die, cu);
10753
10754 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10755 standardised yet. As a workaround for the language detection we fall
10756 back to the DW_AT_producer string. */
10757 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10758 cu->language = language_opencl;
10759
10760 /* Similar hack for Go. */
10761 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10762 set_cu_language (DW_LANG_Go, cu);
10763
10764 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10765
10766 /* Decode line number information if present. We do this before
10767 processing child DIEs, so that the line header table is available
10768 for DW_AT_decl_file. */
10769 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10770
10771 /* Process all dies in compilation unit. */
10772 if (die->child != NULL)
10773 {
10774 child_die = die->child;
10775 while (child_die && child_die->tag)
10776 {
10777 process_die (child_die, cu);
10778 child_die = sibling_die (child_die);
10779 }
10780 }
10781
10782 /* Decode macro information, if present. Dwarf 2 macro information
10783 refers to information in the line number info statement program
10784 header, so we can only read it if we've read the header
10785 successfully. */
10786 attr = dwarf2_attr (die, DW_AT_macros, cu);
10787 if (attr == NULL)
10788 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10789 if (attr && cu->line_header)
10790 {
10791 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10792 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10793
10794 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10795 }
10796 else
10797 {
10798 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10799 if (attr && cu->line_header)
10800 {
10801 unsigned int macro_offset = DW_UNSND (attr);
10802
10803 dwarf_decode_macros (cu, macro_offset, 0);
10804 }
10805 }
10806 }
10807
10808 void
10809 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10810 {
10811 struct type_unit_group *tu_group;
10812 int first_time;
10813 struct attribute *attr;
10814 unsigned int i;
10815 struct signatured_type *sig_type;
10816
10817 gdb_assert (per_cu->is_debug_types);
10818 sig_type = (struct signatured_type *) per_cu;
10819
10820 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10821
10822 /* If we're using .gdb_index (includes -readnow) then
10823 per_cu->type_unit_group may not have been set up yet. */
10824 if (sig_type->type_unit_group == NULL)
10825 sig_type->type_unit_group = get_type_unit_group (this, attr);
10826 tu_group = sig_type->type_unit_group;
10827
10828 /* If we've already processed this stmt_list there's no real need to
10829 do it again, we could fake it and just recreate the part we need
10830 (file name,index -> symtab mapping). If data shows this optimization
10831 is useful we can do it then. */
10832 first_time = tu_group->compunit_symtab == NULL;
10833
10834 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10835 debug info. */
10836 line_header_up lh;
10837 if (attr != NULL)
10838 {
10839 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10840 lh = dwarf_decode_line_header (line_offset, this);
10841 }
10842 if (lh == NULL)
10843 {
10844 if (first_time)
10845 start_symtab ("", NULL, 0);
10846 else
10847 {
10848 gdb_assert (tu_group->symtabs == NULL);
10849 gdb_assert (m_builder == nullptr);
10850 struct compunit_symtab *cust = tu_group->compunit_symtab;
10851 m_builder.reset (new struct buildsym_compunit
10852 (COMPUNIT_OBJFILE (cust), "",
10853 COMPUNIT_DIRNAME (cust),
10854 compunit_language (cust),
10855 0, cust));
10856 }
10857 return;
10858 }
10859
10860 line_header = lh.release ();
10861 line_header_die_owner = die;
10862
10863 if (first_time)
10864 {
10865 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10866
10867 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10868 still initializing it, and our caller (a few levels up)
10869 process_full_type_unit still needs to know if this is the first
10870 time. */
10871
10872 tu_group->num_symtabs = line_header->file_names_size ();
10873 tu_group->symtabs = XNEWVEC (struct symtab *,
10874 line_header->file_names_size ());
10875
10876 auto &file_names = line_header->file_names ();
10877 for (i = 0; i < file_names.size (); ++i)
10878 {
10879 file_entry &fe = file_names[i];
10880 dwarf2_start_subfile (this, fe.name,
10881 fe.include_dir (line_header));
10882 buildsym_compunit *b = get_builder ();
10883 if (b->get_current_subfile ()->symtab == NULL)
10884 {
10885 /* NOTE: start_subfile will recognize when it's been
10886 passed a file it has already seen. So we can't
10887 assume there's a simple mapping from
10888 cu->line_header->file_names to subfiles, plus
10889 cu->line_header->file_names may contain dups. */
10890 b->get_current_subfile ()->symtab
10891 = allocate_symtab (cust, b->get_current_subfile ()->name);
10892 }
10893
10894 fe.symtab = b->get_current_subfile ()->symtab;
10895 tu_group->symtabs[i] = fe.symtab;
10896 }
10897 }
10898 else
10899 {
10900 gdb_assert (m_builder == nullptr);
10901 struct compunit_symtab *cust = tu_group->compunit_symtab;
10902 m_builder.reset (new struct buildsym_compunit
10903 (COMPUNIT_OBJFILE (cust), "",
10904 COMPUNIT_DIRNAME (cust),
10905 compunit_language (cust),
10906 0, cust));
10907
10908 auto &file_names = line_header->file_names ();
10909 for (i = 0; i < file_names.size (); ++i)
10910 {
10911 file_entry &fe = file_names[i];
10912 fe.symtab = tu_group->symtabs[i];
10913 }
10914 }
10915
10916 /* The main symtab is allocated last. Type units don't have DW_AT_name
10917 so they don't have a "real" (so to speak) symtab anyway.
10918 There is later code that will assign the main symtab to all symbols
10919 that don't have one. We need to handle the case of a symbol with a
10920 missing symtab (DW_AT_decl_file) anyway. */
10921 }
10922
10923 /* Process DW_TAG_type_unit.
10924 For TUs we want to skip the first top level sibling if it's not the
10925 actual type being defined by this TU. In this case the first top
10926 level sibling is there to provide context only. */
10927
10928 static void
10929 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10930 {
10931 struct die_info *child_die;
10932
10933 prepare_one_comp_unit (cu, die, language_minimal);
10934
10935 /* Initialize (or reinitialize) the machinery for building symtabs.
10936 We do this before processing child DIEs, so that the line header table
10937 is available for DW_AT_decl_file. */
10938 cu->setup_type_unit_groups (die);
10939
10940 if (die->child != NULL)
10941 {
10942 child_die = die->child;
10943 while (child_die && child_die->tag)
10944 {
10945 process_die (child_die, cu);
10946 child_die = sibling_die (child_die);
10947 }
10948 }
10949 }
10950 \f
10951 /* DWO/DWP files.
10952
10953 http://gcc.gnu.org/wiki/DebugFission
10954 http://gcc.gnu.org/wiki/DebugFissionDWP
10955
10956 To simplify handling of both DWO files ("object" files with the DWARF info)
10957 and DWP files (a file with the DWOs packaged up into one file), we treat
10958 DWP files as having a collection of virtual DWO files. */
10959
10960 static hashval_t
10961 hash_dwo_file (const void *item)
10962 {
10963 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10964 hashval_t hash;
10965
10966 hash = htab_hash_string (dwo_file->dwo_name);
10967 if (dwo_file->comp_dir != NULL)
10968 hash += htab_hash_string (dwo_file->comp_dir);
10969 return hash;
10970 }
10971
10972 static int
10973 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10974 {
10975 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10976 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10977
10978 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10979 return 0;
10980 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10981 return lhs->comp_dir == rhs->comp_dir;
10982 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10983 }
10984
10985 /* Allocate a hash table for DWO files. */
10986
10987 static htab_up
10988 allocate_dwo_file_hash_table (struct objfile *objfile)
10989 {
10990 auto delete_dwo_file = [] (void *item)
10991 {
10992 struct dwo_file *dwo_file = (struct dwo_file *) item;
10993
10994 delete dwo_file;
10995 };
10996
10997 return htab_up (htab_create_alloc (41,
10998 hash_dwo_file,
10999 eq_dwo_file,
11000 delete_dwo_file,
11001 xcalloc, xfree));
11002 }
11003
11004 /* Lookup DWO file DWO_NAME. */
11005
11006 static void **
11007 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11008 const char *dwo_name,
11009 const char *comp_dir)
11010 {
11011 struct dwo_file find_entry;
11012 void **slot;
11013
11014 if (dwarf2_per_objfile->dwo_files == NULL)
11015 dwarf2_per_objfile->dwo_files
11016 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11017
11018 find_entry.dwo_name = dwo_name;
11019 find_entry.comp_dir = comp_dir;
11020 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11021 INSERT);
11022
11023 return slot;
11024 }
11025
11026 static hashval_t
11027 hash_dwo_unit (const void *item)
11028 {
11029 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11030
11031 /* This drops the top 32 bits of the id, but is ok for a hash. */
11032 return dwo_unit->signature;
11033 }
11034
11035 static int
11036 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11037 {
11038 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11039 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11040
11041 /* The signature is assumed to be unique within the DWO file.
11042 So while object file CU dwo_id's always have the value zero,
11043 that's OK, assuming each object file DWO file has only one CU,
11044 and that's the rule for now. */
11045 return lhs->signature == rhs->signature;
11046 }
11047
11048 /* Allocate a hash table for DWO CUs,TUs.
11049 There is one of these tables for each of CUs,TUs for each DWO file. */
11050
11051 static htab_up
11052 allocate_dwo_unit_table (struct objfile *objfile)
11053 {
11054 /* Start out with a pretty small number.
11055 Generally DWO files contain only one CU and maybe some TUs. */
11056 return htab_up (htab_create_alloc (3,
11057 hash_dwo_unit,
11058 eq_dwo_unit,
11059 NULL, xcalloc, xfree));
11060 }
11061
11062 /* die_reader_func for create_dwo_cu. */
11063
11064 static void
11065 create_dwo_cu_reader (const struct die_reader_specs *reader,
11066 const gdb_byte *info_ptr,
11067 struct die_info *comp_unit_die,
11068 struct dwo_file *dwo_file,
11069 struct dwo_unit *dwo_unit)
11070 {
11071 struct dwarf2_cu *cu = reader->cu;
11072 sect_offset sect_off = cu->per_cu->sect_off;
11073 struct dwarf2_section_info *section = cu->per_cu->section;
11074
11075 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11076 if (!signature.has_value ())
11077 {
11078 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11079 " its dwo_id [in module %s]"),
11080 sect_offset_str (sect_off), dwo_file->dwo_name);
11081 return;
11082 }
11083
11084 dwo_unit->dwo_file = dwo_file;
11085 dwo_unit->signature = *signature;
11086 dwo_unit->section = section;
11087 dwo_unit->sect_off = sect_off;
11088 dwo_unit->length = cu->per_cu->length;
11089
11090 if (dwarf_read_debug)
11091 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11092 sect_offset_str (sect_off),
11093 hex_string (dwo_unit->signature));
11094 }
11095
11096 /* Create the dwo_units for the CUs in a DWO_FILE.
11097 Note: This function processes DWO files only, not DWP files. */
11098
11099 static void
11100 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11101 dwarf2_cu *cu, struct dwo_file &dwo_file,
11102 dwarf2_section_info &section, htab_up &cus_htab)
11103 {
11104 struct objfile *objfile = dwarf2_per_objfile->objfile;
11105 const gdb_byte *info_ptr, *end_ptr;
11106
11107 section.read (objfile);
11108 info_ptr = section.buffer;
11109
11110 if (info_ptr == NULL)
11111 return;
11112
11113 if (dwarf_read_debug)
11114 {
11115 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11116 section.get_name (),
11117 section.get_file_name ());
11118 }
11119
11120 end_ptr = info_ptr + section.size;
11121 while (info_ptr < end_ptr)
11122 {
11123 struct dwarf2_per_cu_data per_cu;
11124 struct dwo_unit read_unit {};
11125 struct dwo_unit *dwo_unit;
11126 void **slot;
11127 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11128
11129 memset (&per_cu, 0, sizeof (per_cu));
11130 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11131 per_cu.is_debug_types = 0;
11132 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11133 per_cu.section = &section;
11134
11135 cutu_reader reader (&per_cu, cu, &dwo_file);
11136 if (!reader.dummy_p)
11137 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11138 &dwo_file, &read_unit);
11139 info_ptr += per_cu.length;
11140
11141 // If the unit could not be parsed, skip it.
11142 if (read_unit.dwo_file == NULL)
11143 continue;
11144
11145 if (cus_htab == NULL)
11146 cus_htab = allocate_dwo_unit_table (objfile);
11147
11148 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11149 *dwo_unit = read_unit;
11150 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11151 gdb_assert (slot != NULL);
11152 if (*slot != NULL)
11153 {
11154 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11155 sect_offset dup_sect_off = dup_cu->sect_off;
11156
11157 complaint (_("debug cu entry at offset %s is duplicate to"
11158 " the entry at offset %s, signature %s"),
11159 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11160 hex_string (dwo_unit->signature));
11161 }
11162 *slot = (void *)dwo_unit;
11163 }
11164 }
11165
11166 /* DWP file .debug_{cu,tu}_index section format:
11167 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11168
11169 DWP Version 1:
11170
11171 Both index sections have the same format, and serve to map a 64-bit
11172 signature to a set of section numbers. Each section begins with a header,
11173 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11174 indexes, and a pool of 32-bit section numbers. The index sections will be
11175 aligned at 8-byte boundaries in the file.
11176
11177 The index section header consists of:
11178
11179 V, 32 bit version number
11180 -, 32 bits unused
11181 N, 32 bit number of compilation units or type units in the index
11182 M, 32 bit number of slots in the hash table
11183
11184 Numbers are recorded using the byte order of the application binary.
11185
11186 The hash table begins at offset 16 in the section, and consists of an array
11187 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11188 order of the application binary). Unused slots in the hash table are 0.
11189 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11190
11191 The parallel table begins immediately after the hash table
11192 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11193 array of 32-bit indexes (using the byte order of the application binary),
11194 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11195 table contains a 32-bit index into the pool of section numbers. For unused
11196 hash table slots, the corresponding entry in the parallel table will be 0.
11197
11198 The pool of section numbers begins immediately following the hash table
11199 (at offset 16 + 12 * M from the beginning of the section). The pool of
11200 section numbers consists of an array of 32-bit words (using the byte order
11201 of the application binary). Each item in the array is indexed starting
11202 from 0. The hash table entry provides the index of the first section
11203 number in the set. Additional section numbers in the set follow, and the
11204 set is terminated by a 0 entry (section number 0 is not used in ELF).
11205
11206 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11207 section must be the first entry in the set, and the .debug_abbrev.dwo must
11208 be the second entry. Other members of the set may follow in any order.
11209
11210 ---
11211
11212 DWP Version 2:
11213
11214 DWP Version 2 combines all the .debug_info, etc. sections into one,
11215 and the entries in the index tables are now offsets into these sections.
11216 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11217 section.
11218
11219 Index Section Contents:
11220 Header
11221 Hash Table of Signatures dwp_hash_table.hash_table
11222 Parallel Table of Indices dwp_hash_table.unit_table
11223 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11224 Table of Section Sizes dwp_hash_table.v2.sizes
11225
11226 The index section header consists of:
11227
11228 V, 32 bit version number
11229 L, 32 bit number of columns in the table of section offsets
11230 N, 32 bit number of compilation units or type units in the index
11231 M, 32 bit number of slots in the hash table
11232
11233 Numbers are recorded using the byte order of the application binary.
11234
11235 The hash table has the same format as version 1.
11236 The parallel table of indices has the same format as version 1,
11237 except that the entries are origin-1 indices into the table of sections
11238 offsets and the table of section sizes.
11239
11240 The table of offsets begins immediately following the parallel table
11241 (at offset 16 + 12 * M from the beginning of the section). The table is
11242 a two-dimensional array of 32-bit words (using the byte order of the
11243 application binary), with L columns and N+1 rows, in row-major order.
11244 Each row in the array is indexed starting from 0. The first row provides
11245 a key to the remaining rows: each column in this row provides an identifier
11246 for a debug section, and the offsets in the same column of subsequent rows
11247 refer to that section. The section identifiers are:
11248
11249 DW_SECT_INFO 1 .debug_info.dwo
11250 DW_SECT_TYPES 2 .debug_types.dwo
11251 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11252 DW_SECT_LINE 4 .debug_line.dwo
11253 DW_SECT_LOC 5 .debug_loc.dwo
11254 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11255 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11256 DW_SECT_MACRO 8 .debug_macro.dwo
11257
11258 The offsets provided by the CU and TU index sections are the base offsets
11259 for the contributions made by each CU or TU to the corresponding section
11260 in the package file. Each CU and TU header contains an abbrev_offset
11261 field, used to find the abbreviations table for that CU or TU within the
11262 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11263 be interpreted as relative to the base offset given in the index section.
11264 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11265 should be interpreted as relative to the base offset for .debug_line.dwo,
11266 and offsets into other debug sections obtained from DWARF attributes should
11267 also be interpreted as relative to the corresponding base offset.
11268
11269 The table of sizes begins immediately following the table of offsets.
11270 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11271 with L columns and N rows, in row-major order. Each row in the array is
11272 indexed starting from 1 (row 0 is shared by the two tables).
11273
11274 ---
11275
11276 Hash table lookup is handled the same in version 1 and 2:
11277
11278 We assume that N and M will not exceed 2^32 - 1.
11279 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11280
11281 Given a 64-bit compilation unit signature or a type signature S, an entry
11282 in the hash table is located as follows:
11283
11284 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11285 the low-order k bits all set to 1.
11286
11287 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11288
11289 3) If the hash table entry at index H matches the signature, use that
11290 entry. If the hash table entry at index H is unused (all zeroes),
11291 terminate the search: the signature is not present in the table.
11292
11293 4) Let H = (H + H') modulo M. Repeat at Step 3.
11294
11295 Because M > N and H' and M are relatively prime, the search is guaranteed
11296 to stop at an unused slot or find the match. */
11297
11298 /* Create a hash table to map DWO IDs to their CU/TU entry in
11299 .debug_{info,types}.dwo in DWP_FILE.
11300 Returns NULL if there isn't one.
11301 Note: This function processes DWP files only, not DWO files. */
11302
11303 static struct dwp_hash_table *
11304 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11305 struct dwp_file *dwp_file, int is_debug_types)
11306 {
11307 struct objfile *objfile = dwarf2_per_objfile->objfile;
11308 bfd *dbfd = dwp_file->dbfd.get ();
11309 const gdb_byte *index_ptr, *index_end;
11310 struct dwarf2_section_info *index;
11311 uint32_t version, nr_columns, nr_units, nr_slots;
11312 struct dwp_hash_table *htab;
11313
11314 if (is_debug_types)
11315 index = &dwp_file->sections.tu_index;
11316 else
11317 index = &dwp_file->sections.cu_index;
11318
11319 if (index->empty ())
11320 return NULL;
11321 index->read (objfile);
11322
11323 index_ptr = index->buffer;
11324 index_end = index_ptr + index->size;
11325
11326 version = read_4_bytes (dbfd, index_ptr);
11327 index_ptr += 4;
11328 if (version == 2)
11329 nr_columns = read_4_bytes (dbfd, index_ptr);
11330 else
11331 nr_columns = 0;
11332 index_ptr += 4;
11333 nr_units = read_4_bytes (dbfd, index_ptr);
11334 index_ptr += 4;
11335 nr_slots = read_4_bytes (dbfd, index_ptr);
11336 index_ptr += 4;
11337
11338 if (version != 1 && version != 2)
11339 {
11340 error (_("Dwarf Error: unsupported DWP file version (%s)"
11341 " [in module %s]"),
11342 pulongest (version), dwp_file->name);
11343 }
11344 if (nr_slots != (nr_slots & -nr_slots))
11345 {
11346 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11347 " is not power of 2 [in module %s]"),
11348 pulongest (nr_slots), dwp_file->name);
11349 }
11350
11351 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11352 htab->version = version;
11353 htab->nr_columns = nr_columns;
11354 htab->nr_units = nr_units;
11355 htab->nr_slots = nr_slots;
11356 htab->hash_table = index_ptr;
11357 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11358
11359 /* Exit early if the table is empty. */
11360 if (nr_slots == 0 || nr_units == 0
11361 || (version == 2 && nr_columns == 0))
11362 {
11363 /* All must be zero. */
11364 if (nr_slots != 0 || nr_units != 0
11365 || (version == 2 && nr_columns != 0))
11366 {
11367 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11368 " all zero [in modules %s]"),
11369 dwp_file->name);
11370 }
11371 return htab;
11372 }
11373
11374 if (version == 1)
11375 {
11376 htab->section_pool.v1.indices =
11377 htab->unit_table + sizeof (uint32_t) * nr_slots;
11378 /* It's harder to decide whether the section is too small in v1.
11379 V1 is deprecated anyway so we punt. */
11380 }
11381 else
11382 {
11383 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11384 int *ids = htab->section_pool.v2.section_ids;
11385 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11386 /* Reverse map for error checking. */
11387 int ids_seen[DW_SECT_MAX + 1];
11388 int i;
11389
11390 if (nr_columns < 2)
11391 {
11392 error (_("Dwarf Error: bad DWP hash table, too few columns"
11393 " in section table [in module %s]"),
11394 dwp_file->name);
11395 }
11396 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11397 {
11398 error (_("Dwarf Error: bad DWP hash table, too many columns"
11399 " in section table [in module %s]"),
11400 dwp_file->name);
11401 }
11402 memset (ids, 255, sizeof_ids);
11403 memset (ids_seen, 255, sizeof (ids_seen));
11404 for (i = 0; i < nr_columns; ++i)
11405 {
11406 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11407
11408 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11409 {
11410 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11411 " in section table [in module %s]"),
11412 id, dwp_file->name);
11413 }
11414 if (ids_seen[id] != -1)
11415 {
11416 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11417 " id %d in section table [in module %s]"),
11418 id, dwp_file->name);
11419 }
11420 ids_seen[id] = i;
11421 ids[i] = id;
11422 }
11423 /* Must have exactly one info or types section. */
11424 if (((ids_seen[DW_SECT_INFO] != -1)
11425 + (ids_seen[DW_SECT_TYPES] != -1))
11426 != 1)
11427 {
11428 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11429 " DWO info/types section [in module %s]"),
11430 dwp_file->name);
11431 }
11432 /* Must have an abbrev section. */
11433 if (ids_seen[DW_SECT_ABBREV] == -1)
11434 {
11435 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11436 " section [in module %s]"),
11437 dwp_file->name);
11438 }
11439 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11440 htab->section_pool.v2.sizes =
11441 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11442 * nr_units * nr_columns);
11443 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11444 * nr_units * nr_columns))
11445 > index_end)
11446 {
11447 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11448 " [in module %s]"),
11449 dwp_file->name);
11450 }
11451 }
11452
11453 return htab;
11454 }
11455
11456 /* Update SECTIONS with the data from SECTP.
11457
11458 This function is like the other "locate" section routines that are
11459 passed to bfd_map_over_sections, but in this context the sections to
11460 read comes from the DWP V1 hash table, not the full ELF section table.
11461
11462 The result is non-zero for success, or zero if an error was found. */
11463
11464 static int
11465 locate_v1_virtual_dwo_sections (asection *sectp,
11466 struct virtual_v1_dwo_sections *sections)
11467 {
11468 const struct dwop_section_names *names = &dwop_section_names;
11469
11470 if (section_is_p (sectp->name, &names->abbrev_dwo))
11471 {
11472 /* There can be only one. */
11473 if (sections->abbrev.s.section != NULL)
11474 return 0;
11475 sections->abbrev.s.section = sectp;
11476 sections->abbrev.size = bfd_section_size (sectp);
11477 }
11478 else if (section_is_p (sectp->name, &names->info_dwo)
11479 || section_is_p (sectp->name, &names->types_dwo))
11480 {
11481 /* There can be only one. */
11482 if (sections->info_or_types.s.section != NULL)
11483 return 0;
11484 sections->info_or_types.s.section = sectp;
11485 sections->info_or_types.size = bfd_section_size (sectp);
11486 }
11487 else if (section_is_p (sectp->name, &names->line_dwo))
11488 {
11489 /* There can be only one. */
11490 if (sections->line.s.section != NULL)
11491 return 0;
11492 sections->line.s.section = sectp;
11493 sections->line.size = bfd_section_size (sectp);
11494 }
11495 else if (section_is_p (sectp->name, &names->loc_dwo))
11496 {
11497 /* There can be only one. */
11498 if (sections->loc.s.section != NULL)
11499 return 0;
11500 sections->loc.s.section = sectp;
11501 sections->loc.size = bfd_section_size (sectp);
11502 }
11503 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11504 {
11505 /* There can be only one. */
11506 if (sections->macinfo.s.section != NULL)
11507 return 0;
11508 sections->macinfo.s.section = sectp;
11509 sections->macinfo.size = bfd_section_size (sectp);
11510 }
11511 else if (section_is_p (sectp->name, &names->macro_dwo))
11512 {
11513 /* There can be only one. */
11514 if (sections->macro.s.section != NULL)
11515 return 0;
11516 sections->macro.s.section = sectp;
11517 sections->macro.size = bfd_section_size (sectp);
11518 }
11519 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11520 {
11521 /* There can be only one. */
11522 if (sections->str_offsets.s.section != NULL)
11523 return 0;
11524 sections->str_offsets.s.section = sectp;
11525 sections->str_offsets.size = bfd_section_size (sectp);
11526 }
11527 else
11528 {
11529 /* No other kind of section is valid. */
11530 return 0;
11531 }
11532
11533 return 1;
11534 }
11535
11536 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11537 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11538 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11539 This is for DWP version 1 files. */
11540
11541 static struct dwo_unit *
11542 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11543 struct dwp_file *dwp_file,
11544 uint32_t unit_index,
11545 const char *comp_dir,
11546 ULONGEST signature, int is_debug_types)
11547 {
11548 struct objfile *objfile = dwarf2_per_objfile->objfile;
11549 const struct dwp_hash_table *dwp_htab =
11550 is_debug_types ? dwp_file->tus : dwp_file->cus;
11551 bfd *dbfd = dwp_file->dbfd.get ();
11552 const char *kind = is_debug_types ? "TU" : "CU";
11553 struct dwo_file *dwo_file;
11554 struct dwo_unit *dwo_unit;
11555 struct virtual_v1_dwo_sections sections;
11556 void **dwo_file_slot;
11557 int i;
11558
11559 gdb_assert (dwp_file->version == 1);
11560
11561 if (dwarf_read_debug)
11562 {
11563 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11564 kind,
11565 pulongest (unit_index), hex_string (signature),
11566 dwp_file->name);
11567 }
11568
11569 /* Fetch the sections of this DWO unit.
11570 Put a limit on the number of sections we look for so that bad data
11571 doesn't cause us to loop forever. */
11572
11573 #define MAX_NR_V1_DWO_SECTIONS \
11574 (1 /* .debug_info or .debug_types */ \
11575 + 1 /* .debug_abbrev */ \
11576 + 1 /* .debug_line */ \
11577 + 1 /* .debug_loc */ \
11578 + 1 /* .debug_str_offsets */ \
11579 + 1 /* .debug_macro or .debug_macinfo */ \
11580 + 1 /* trailing zero */)
11581
11582 memset (&sections, 0, sizeof (sections));
11583
11584 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11585 {
11586 asection *sectp;
11587 uint32_t section_nr =
11588 read_4_bytes (dbfd,
11589 dwp_htab->section_pool.v1.indices
11590 + (unit_index + i) * sizeof (uint32_t));
11591
11592 if (section_nr == 0)
11593 break;
11594 if (section_nr >= dwp_file->num_sections)
11595 {
11596 error (_("Dwarf Error: bad DWP hash table, section number too large"
11597 " [in module %s]"),
11598 dwp_file->name);
11599 }
11600
11601 sectp = dwp_file->elf_sections[section_nr];
11602 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11603 {
11604 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11605 " [in module %s]"),
11606 dwp_file->name);
11607 }
11608 }
11609
11610 if (i < 2
11611 || sections.info_or_types.empty ()
11612 || sections.abbrev.empty ())
11613 {
11614 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11615 " [in module %s]"),
11616 dwp_file->name);
11617 }
11618 if (i == MAX_NR_V1_DWO_SECTIONS)
11619 {
11620 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11621 " [in module %s]"),
11622 dwp_file->name);
11623 }
11624
11625 /* It's easier for the rest of the code if we fake a struct dwo_file and
11626 have dwo_unit "live" in that. At least for now.
11627
11628 The DWP file can be made up of a random collection of CUs and TUs.
11629 However, for each CU + set of TUs that came from the same original DWO
11630 file, we can combine them back into a virtual DWO file to save space
11631 (fewer struct dwo_file objects to allocate). Remember that for really
11632 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11633
11634 std::string virtual_dwo_name =
11635 string_printf ("virtual-dwo/%d-%d-%d-%d",
11636 sections.abbrev.get_id (),
11637 sections.line.get_id (),
11638 sections.loc.get_id (),
11639 sections.str_offsets.get_id ());
11640 /* Can we use an existing virtual DWO file? */
11641 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11642 virtual_dwo_name.c_str (),
11643 comp_dir);
11644 /* Create one if necessary. */
11645 if (*dwo_file_slot == NULL)
11646 {
11647 if (dwarf_read_debug)
11648 {
11649 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11650 virtual_dwo_name.c_str ());
11651 }
11652 dwo_file = new struct dwo_file;
11653 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11654 virtual_dwo_name);
11655 dwo_file->comp_dir = comp_dir;
11656 dwo_file->sections.abbrev = sections.abbrev;
11657 dwo_file->sections.line = sections.line;
11658 dwo_file->sections.loc = sections.loc;
11659 dwo_file->sections.macinfo = sections.macinfo;
11660 dwo_file->sections.macro = sections.macro;
11661 dwo_file->sections.str_offsets = sections.str_offsets;
11662 /* The "str" section is global to the entire DWP file. */
11663 dwo_file->sections.str = dwp_file->sections.str;
11664 /* The info or types section is assigned below to dwo_unit,
11665 there's no need to record it in dwo_file.
11666 Also, we can't simply record type sections in dwo_file because
11667 we record a pointer into the vector in dwo_unit. As we collect more
11668 types we'll grow the vector and eventually have to reallocate space
11669 for it, invalidating all copies of pointers into the previous
11670 contents. */
11671 *dwo_file_slot = dwo_file;
11672 }
11673 else
11674 {
11675 if (dwarf_read_debug)
11676 {
11677 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11678 virtual_dwo_name.c_str ());
11679 }
11680 dwo_file = (struct dwo_file *) *dwo_file_slot;
11681 }
11682
11683 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11684 dwo_unit->dwo_file = dwo_file;
11685 dwo_unit->signature = signature;
11686 dwo_unit->section =
11687 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11688 *dwo_unit->section = sections.info_or_types;
11689 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11690
11691 return dwo_unit;
11692 }
11693
11694 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11695 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11696 piece within that section used by a TU/CU, return a virtual section
11697 of just that piece. */
11698
11699 static struct dwarf2_section_info
11700 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11701 struct dwarf2_section_info *section,
11702 bfd_size_type offset, bfd_size_type size)
11703 {
11704 struct dwarf2_section_info result;
11705 asection *sectp;
11706
11707 gdb_assert (section != NULL);
11708 gdb_assert (!section->is_virtual);
11709
11710 memset (&result, 0, sizeof (result));
11711 result.s.containing_section = section;
11712 result.is_virtual = true;
11713
11714 if (size == 0)
11715 return result;
11716
11717 sectp = section->get_bfd_section ();
11718
11719 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11720 bounds of the real section. This is a pretty-rare event, so just
11721 flag an error (easier) instead of a warning and trying to cope. */
11722 if (sectp == NULL
11723 || offset + size > bfd_section_size (sectp))
11724 {
11725 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11726 " in section %s [in module %s]"),
11727 sectp ? bfd_section_name (sectp) : "<unknown>",
11728 objfile_name (dwarf2_per_objfile->objfile));
11729 }
11730
11731 result.virtual_offset = offset;
11732 result.size = size;
11733 return result;
11734 }
11735
11736 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11737 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11738 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11739 This is for DWP version 2 files. */
11740
11741 static struct dwo_unit *
11742 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11743 struct dwp_file *dwp_file,
11744 uint32_t unit_index,
11745 const char *comp_dir,
11746 ULONGEST signature, int is_debug_types)
11747 {
11748 struct objfile *objfile = dwarf2_per_objfile->objfile;
11749 const struct dwp_hash_table *dwp_htab =
11750 is_debug_types ? dwp_file->tus : dwp_file->cus;
11751 bfd *dbfd = dwp_file->dbfd.get ();
11752 const char *kind = is_debug_types ? "TU" : "CU";
11753 struct dwo_file *dwo_file;
11754 struct dwo_unit *dwo_unit;
11755 struct virtual_v2_dwo_sections sections;
11756 void **dwo_file_slot;
11757 int i;
11758
11759 gdb_assert (dwp_file->version == 2);
11760
11761 if (dwarf_read_debug)
11762 {
11763 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11764 kind,
11765 pulongest (unit_index), hex_string (signature),
11766 dwp_file->name);
11767 }
11768
11769 /* Fetch the section offsets of this DWO unit. */
11770
11771 memset (&sections, 0, sizeof (sections));
11772
11773 for (i = 0; i < dwp_htab->nr_columns; ++i)
11774 {
11775 uint32_t offset = read_4_bytes (dbfd,
11776 dwp_htab->section_pool.v2.offsets
11777 + (((unit_index - 1) * dwp_htab->nr_columns
11778 + i)
11779 * sizeof (uint32_t)));
11780 uint32_t size = read_4_bytes (dbfd,
11781 dwp_htab->section_pool.v2.sizes
11782 + (((unit_index - 1) * dwp_htab->nr_columns
11783 + i)
11784 * sizeof (uint32_t)));
11785
11786 switch (dwp_htab->section_pool.v2.section_ids[i])
11787 {
11788 case DW_SECT_INFO:
11789 case DW_SECT_TYPES:
11790 sections.info_or_types_offset = offset;
11791 sections.info_or_types_size = size;
11792 break;
11793 case DW_SECT_ABBREV:
11794 sections.abbrev_offset = offset;
11795 sections.abbrev_size = size;
11796 break;
11797 case DW_SECT_LINE:
11798 sections.line_offset = offset;
11799 sections.line_size = size;
11800 break;
11801 case DW_SECT_LOC:
11802 sections.loc_offset = offset;
11803 sections.loc_size = size;
11804 break;
11805 case DW_SECT_STR_OFFSETS:
11806 sections.str_offsets_offset = offset;
11807 sections.str_offsets_size = size;
11808 break;
11809 case DW_SECT_MACINFO:
11810 sections.macinfo_offset = offset;
11811 sections.macinfo_size = size;
11812 break;
11813 case DW_SECT_MACRO:
11814 sections.macro_offset = offset;
11815 sections.macro_size = size;
11816 break;
11817 }
11818 }
11819
11820 /* It's easier for the rest of the code if we fake a struct dwo_file and
11821 have dwo_unit "live" in that. At least for now.
11822
11823 The DWP file can be made up of a random collection of CUs and TUs.
11824 However, for each CU + set of TUs that came from the same original DWO
11825 file, we can combine them back into a virtual DWO file to save space
11826 (fewer struct dwo_file objects to allocate). Remember that for really
11827 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11828
11829 std::string virtual_dwo_name =
11830 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11831 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11832 (long) (sections.line_size ? sections.line_offset : 0),
11833 (long) (sections.loc_size ? sections.loc_offset : 0),
11834 (long) (sections.str_offsets_size
11835 ? sections.str_offsets_offset : 0));
11836 /* Can we use an existing virtual DWO file? */
11837 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11838 virtual_dwo_name.c_str (),
11839 comp_dir);
11840 /* Create one if necessary. */
11841 if (*dwo_file_slot == NULL)
11842 {
11843 if (dwarf_read_debug)
11844 {
11845 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11846 virtual_dwo_name.c_str ());
11847 }
11848 dwo_file = new struct dwo_file;
11849 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11850 virtual_dwo_name);
11851 dwo_file->comp_dir = comp_dir;
11852 dwo_file->sections.abbrev =
11853 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11854 sections.abbrev_offset, sections.abbrev_size);
11855 dwo_file->sections.line =
11856 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11857 sections.line_offset, sections.line_size);
11858 dwo_file->sections.loc =
11859 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11860 sections.loc_offset, sections.loc_size);
11861 dwo_file->sections.macinfo =
11862 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11863 sections.macinfo_offset, sections.macinfo_size);
11864 dwo_file->sections.macro =
11865 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11866 sections.macro_offset, sections.macro_size);
11867 dwo_file->sections.str_offsets =
11868 create_dwp_v2_section (dwarf2_per_objfile,
11869 &dwp_file->sections.str_offsets,
11870 sections.str_offsets_offset,
11871 sections.str_offsets_size);
11872 /* The "str" section is global to the entire DWP file. */
11873 dwo_file->sections.str = dwp_file->sections.str;
11874 /* The info or types section is assigned below to dwo_unit,
11875 there's no need to record it in dwo_file.
11876 Also, we can't simply record type sections in dwo_file because
11877 we record a pointer into the vector in dwo_unit. As we collect more
11878 types we'll grow the vector and eventually have to reallocate space
11879 for it, invalidating all copies of pointers into the previous
11880 contents. */
11881 *dwo_file_slot = dwo_file;
11882 }
11883 else
11884 {
11885 if (dwarf_read_debug)
11886 {
11887 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11888 virtual_dwo_name.c_str ());
11889 }
11890 dwo_file = (struct dwo_file *) *dwo_file_slot;
11891 }
11892
11893 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11894 dwo_unit->dwo_file = dwo_file;
11895 dwo_unit->signature = signature;
11896 dwo_unit->section =
11897 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11898 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11899 is_debug_types
11900 ? &dwp_file->sections.types
11901 : &dwp_file->sections.info,
11902 sections.info_or_types_offset,
11903 sections.info_or_types_size);
11904 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11905
11906 return dwo_unit;
11907 }
11908
11909 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11910 Returns NULL if the signature isn't found. */
11911
11912 static struct dwo_unit *
11913 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11914 struct dwp_file *dwp_file, const char *comp_dir,
11915 ULONGEST signature, int is_debug_types)
11916 {
11917 const struct dwp_hash_table *dwp_htab =
11918 is_debug_types ? dwp_file->tus : dwp_file->cus;
11919 bfd *dbfd = dwp_file->dbfd.get ();
11920 uint32_t mask = dwp_htab->nr_slots - 1;
11921 uint32_t hash = signature & mask;
11922 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11923 unsigned int i;
11924 void **slot;
11925 struct dwo_unit find_dwo_cu;
11926
11927 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11928 find_dwo_cu.signature = signature;
11929 slot = htab_find_slot (is_debug_types
11930 ? dwp_file->loaded_tus.get ()
11931 : dwp_file->loaded_cus.get (),
11932 &find_dwo_cu, INSERT);
11933
11934 if (*slot != NULL)
11935 return (struct dwo_unit *) *slot;
11936
11937 /* Use a for loop so that we don't loop forever on bad debug info. */
11938 for (i = 0; i < dwp_htab->nr_slots; ++i)
11939 {
11940 ULONGEST signature_in_table;
11941
11942 signature_in_table =
11943 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11944 if (signature_in_table == signature)
11945 {
11946 uint32_t unit_index =
11947 read_4_bytes (dbfd,
11948 dwp_htab->unit_table + hash * sizeof (uint32_t));
11949
11950 if (dwp_file->version == 1)
11951 {
11952 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11953 dwp_file, unit_index,
11954 comp_dir, signature,
11955 is_debug_types);
11956 }
11957 else
11958 {
11959 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11960 dwp_file, unit_index,
11961 comp_dir, signature,
11962 is_debug_types);
11963 }
11964 return (struct dwo_unit *) *slot;
11965 }
11966 if (signature_in_table == 0)
11967 return NULL;
11968 hash = (hash + hash2) & mask;
11969 }
11970
11971 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11972 " [in module %s]"),
11973 dwp_file->name);
11974 }
11975
11976 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11977 Open the file specified by FILE_NAME and hand it off to BFD for
11978 preliminary analysis. Return a newly initialized bfd *, which
11979 includes a canonicalized copy of FILE_NAME.
11980 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11981 SEARCH_CWD is true if the current directory is to be searched.
11982 It will be searched before debug-file-directory.
11983 If successful, the file is added to the bfd include table of the
11984 objfile's bfd (see gdb_bfd_record_inclusion).
11985 If unable to find/open the file, return NULL.
11986 NOTE: This function is derived from symfile_bfd_open. */
11987
11988 static gdb_bfd_ref_ptr
11989 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11990 const char *file_name, int is_dwp, int search_cwd)
11991 {
11992 int desc;
11993 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11994 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11995 to debug_file_directory. */
11996 const char *search_path;
11997 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11998
11999 gdb::unique_xmalloc_ptr<char> search_path_holder;
12000 if (search_cwd)
12001 {
12002 if (*debug_file_directory != '\0')
12003 {
12004 search_path_holder.reset (concat (".", dirname_separator_string,
12005 debug_file_directory,
12006 (char *) NULL));
12007 search_path = search_path_holder.get ();
12008 }
12009 else
12010 search_path = ".";
12011 }
12012 else
12013 search_path = debug_file_directory;
12014
12015 openp_flags flags = OPF_RETURN_REALPATH;
12016 if (is_dwp)
12017 flags |= OPF_SEARCH_IN_PATH;
12018
12019 gdb::unique_xmalloc_ptr<char> absolute_name;
12020 desc = openp (search_path, flags, file_name,
12021 O_RDONLY | O_BINARY, &absolute_name);
12022 if (desc < 0)
12023 return NULL;
12024
12025 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12026 gnutarget, desc));
12027 if (sym_bfd == NULL)
12028 return NULL;
12029 bfd_set_cacheable (sym_bfd.get (), 1);
12030
12031 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12032 return NULL;
12033
12034 /* Success. Record the bfd as having been included by the objfile's bfd.
12035 This is important because things like demangled_names_hash lives in the
12036 objfile's per_bfd space and may have references to things like symbol
12037 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12038 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12039
12040 return sym_bfd;
12041 }
12042
12043 /* Try to open DWO file FILE_NAME.
12044 COMP_DIR is the DW_AT_comp_dir attribute.
12045 The result is the bfd handle of the file.
12046 If there is a problem finding or opening the file, return NULL.
12047 Upon success, the canonicalized path of the file is stored in the bfd,
12048 same as symfile_bfd_open. */
12049
12050 static gdb_bfd_ref_ptr
12051 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12052 const char *file_name, const char *comp_dir)
12053 {
12054 if (IS_ABSOLUTE_PATH (file_name))
12055 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12056 0 /*is_dwp*/, 0 /*search_cwd*/);
12057
12058 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12059
12060 if (comp_dir != NULL)
12061 {
12062 gdb::unique_xmalloc_ptr<char> path_to_try
12063 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12064
12065 /* NOTE: If comp_dir is a relative path, this will also try the
12066 search path, which seems useful. */
12067 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12068 path_to_try.get (),
12069 0 /*is_dwp*/,
12070 1 /*search_cwd*/));
12071 if (abfd != NULL)
12072 return abfd;
12073 }
12074
12075 /* That didn't work, try debug-file-directory, which, despite its name,
12076 is a list of paths. */
12077
12078 if (*debug_file_directory == '\0')
12079 return NULL;
12080
12081 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12082 0 /*is_dwp*/, 1 /*search_cwd*/);
12083 }
12084
12085 /* This function is mapped across the sections and remembers the offset and
12086 size of each of the DWO debugging sections we are interested in. */
12087
12088 static void
12089 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12090 {
12091 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12092 const struct dwop_section_names *names = &dwop_section_names;
12093
12094 if (section_is_p (sectp->name, &names->abbrev_dwo))
12095 {
12096 dwo_sections->abbrev.s.section = sectp;
12097 dwo_sections->abbrev.size = bfd_section_size (sectp);
12098 }
12099 else if (section_is_p (sectp->name, &names->info_dwo))
12100 {
12101 dwo_sections->info.s.section = sectp;
12102 dwo_sections->info.size = bfd_section_size (sectp);
12103 }
12104 else if (section_is_p (sectp->name, &names->line_dwo))
12105 {
12106 dwo_sections->line.s.section = sectp;
12107 dwo_sections->line.size = bfd_section_size (sectp);
12108 }
12109 else if (section_is_p (sectp->name, &names->loc_dwo))
12110 {
12111 dwo_sections->loc.s.section = sectp;
12112 dwo_sections->loc.size = bfd_section_size (sectp);
12113 }
12114 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12115 {
12116 dwo_sections->macinfo.s.section = sectp;
12117 dwo_sections->macinfo.size = bfd_section_size (sectp);
12118 }
12119 else if (section_is_p (sectp->name, &names->macro_dwo))
12120 {
12121 dwo_sections->macro.s.section = sectp;
12122 dwo_sections->macro.size = bfd_section_size (sectp);
12123 }
12124 else if (section_is_p (sectp->name, &names->str_dwo))
12125 {
12126 dwo_sections->str.s.section = sectp;
12127 dwo_sections->str.size = bfd_section_size (sectp);
12128 }
12129 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12130 {
12131 dwo_sections->str_offsets.s.section = sectp;
12132 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12133 }
12134 else if (section_is_p (sectp->name, &names->types_dwo))
12135 {
12136 struct dwarf2_section_info type_section;
12137
12138 memset (&type_section, 0, sizeof (type_section));
12139 type_section.s.section = sectp;
12140 type_section.size = bfd_section_size (sectp);
12141 dwo_sections->types.push_back (type_section);
12142 }
12143 }
12144
12145 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12146 by PER_CU. This is for the non-DWP case.
12147 The result is NULL if DWO_NAME can't be found. */
12148
12149 static struct dwo_file *
12150 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12151 const char *dwo_name, const char *comp_dir)
12152 {
12153 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12154
12155 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12156 if (dbfd == NULL)
12157 {
12158 if (dwarf_read_debug)
12159 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12160 return NULL;
12161 }
12162
12163 dwo_file_up dwo_file (new struct dwo_file);
12164 dwo_file->dwo_name = dwo_name;
12165 dwo_file->comp_dir = comp_dir;
12166 dwo_file->dbfd = std::move (dbfd);
12167
12168 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12169 &dwo_file->sections);
12170
12171 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12172 dwo_file->sections.info, dwo_file->cus);
12173
12174 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12175 dwo_file->sections.types, dwo_file->tus);
12176
12177 if (dwarf_read_debug)
12178 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12179
12180 return dwo_file.release ();
12181 }
12182
12183 /* This function is mapped across the sections and remembers the offset and
12184 size of each of the DWP debugging sections common to version 1 and 2 that
12185 we are interested in. */
12186
12187 static void
12188 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12189 void *dwp_file_ptr)
12190 {
12191 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12192 const struct dwop_section_names *names = &dwop_section_names;
12193 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12194
12195 /* Record the ELF section number for later lookup: this is what the
12196 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12197 gdb_assert (elf_section_nr < dwp_file->num_sections);
12198 dwp_file->elf_sections[elf_section_nr] = sectp;
12199
12200 /* Look for specific sections that we need. */
12201 if (section_is_p (sectp->name, &names->str_dwo))
12202 {
12203 dwp_file->sections.str.s.section = sectp;
12204 dwp_file->sections.str.size = bfd_section_size (sectp);
12205 }
12206 else if (section_is_p (sectp->name, &names->cu_index))
12207 {
12208 dwp_file->sections.cu_index.s.section = sectp;
12209 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12210 }
12211 else if (section_is_p (sectp->name, &names->tu_index))
12212 {
12213 dwp_file->sections.tu_index.s.section = sectp;
12214 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12215 }
12216 }
12217
12218 /* This function is mapped across the sections and remembers the offset and
12219 size of each of the DWP version 2 debugging sections that we are interested
12220 in. This is split into a separate function because we don't know if we
12221 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12222
12223 static void
12224 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12225 {
12226 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12227 const struct dwop_section_names *names = &dwop_section_names;
12228 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12229
12230 /* Record the ELF section number for later lookup: this is what the
12231 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12232 gdb_assert (elf_section_nr < dwp_file->num_sections);
12233 dwp_file->elf_sections[elf_section_nr] = sectp;
12234
12235 /* Look for specific sections that we need. */
12236 if (section_is_p (sectp->name, &names->abbrev_dwo))
12237 {
12238 dwp_file->sections.abbrev.s.section = sectp;
12239 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12240 }
12241 else if (section_is_p (sectp->name, &names->info_dwo))
12242 {
12243 dwp_file->sections.info.s.section = sectp;
12244 dwp_file->sections.info.size = bfd_section_size (sectp);
12245 }
12246 else if (section_is_p (sectp->name, &names->line_dwo))
12247 {
12248 dwp_file->sections.line.s.section = sectp;
12249 dwp_file->sections.line.size = bfd_section_size (sectp);
12250 }
12251 else if (section_is_p (sectp->name, &names->loc_dwo))
12252 {
12253 dwp_file->sections.loc.s.section = sectp;
12254 dwp_file->sections.loc.size = bfd_section_size (sectp);
12255 }
12256 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12257 {
12258 dwp_file->sections.macinfo.s.section = sectp;
12259 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12260 }
12261 else if (section_is_p (sectp->name, &names->macro_dwo))
12262 {
12263 dwp_file->sections.macro.s.section = sectp;
12264 dwp_file->sections.macro.size = bfd_section_size (sectp);
12265 }
12266 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12267 {
12268 dwp_file->sections.str_offsets.s.section = sectp;
12269 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12270 }
12271 else if (section_is_p (sectp->name, &names->types_dwo))
12272 {
12273 dwp_file->sections.types.s.section = sectp;
12274 dwp_file->sections.types.size = bfd_section_size (sectp);
12275 }
12276 }
12277
12278 /* Hash function for dwp_file loaded CUs/TUs. */
12279
12280 static hashval_t
12281 hash_dwp_loaded_cutus (const void *item)
12282 {
12283 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12284
12285 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12286 return dwo_unit->signature;
12287 }
12288
12289 /* Equality function for dwp_file loaded CUs/TUs. */
12290
12291 static int
12292 eq_dwp_loaded_cutus (const void *a, const void *b)
12293 {
12294 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12295 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12296
12297 return dua->signature == dub->signature;
12298 }
12299
12300 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12301
12302 static htab_up
12303 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12304 {
12305 return htab_up (htab_create_alloc (3,
12306 hash_dwp_loaded_cutus,
12307 eq_dwp_loaded_cutus,
12308 NULL, xcalloc, xfree));
12309 }
12310
12311 /* Try to open DWP file FILE_NAME.
12312 The result is the bfd handle of the file.
12313 If there is a problem finding or opening the file, return NULL.
12314 Upon success, the canonicalized path of the file is stored in the bfd,
12315 same as symfile_bfd_open. */
12316
12317 static gdb_bfd_ref_ptr
12318 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12319 const char *file_name)
12320 {
12321 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12322 1 /*is_dwp*/,
12323 1 /*search_cwd*/));
12324 if (abfd != NULL)
12325 return abfd;
12326
12327 /* Work around upstream bug 15652.
12328 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12329 [Whether that's a "bug" is debatable, but it is getting in our way.]
12330 We have no real idea where the dwp file is, because gdb's realpath-ing
12331 of the executable's path may have discarded the needed info.
12332 [IWBN if the dwp file name was recorded in the executable, akin to
12333 .gnu_debuglink, but that doesn't exist yet.]
12334 Strip the directory from FILE_NAME and search again. */
12335 if (*debug_file_directory != '\0')
12336 {
12337 /* Don't implicitly search the current directory here.
12338 If the user wants to search "." to handle this case,
12339 it must be added to debug-file-directory. */
12340 return try_open_dwop_file (dwarf2_per_objfile,
12341 lbasename (file_name), 1 /*is_dwp*/,
12342 0 /*search_cwd*/);
12343 }
12344
12345 return NULL;
12346 }
12347
12348 /* Initialize the use of the DWP file for the current objfile.
12349 By convention the name of the DWP file is ${objfile}.dwp.
12350 The result is NULL if it can't be found. */
12351
12352 static std::unique_ptr<struct dwp_file>
12353 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12354 {
12355 struct objfile *objfile = dwarf2_per_objfile->objfile;
12356
12357 /* Try to find first .dwp for the binary file before any symbolic links
12358 resolving. */
12359
12360 /* If the objfile is a debug file, find the name of the real binary
12361 file and get the name of dwp file from there. */
12362 std::string dwp_name;
12363 if (objfile->separate_debug_objfile_backlink != NULL)
12364 {
12365 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12366 const char *backlink_basename = lbasename (backlink->original_name);
12367
12368 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12369 }
12370 else
12371 dwp_name = objfile->original_name;
12372
12373 dwp_name += ".dwp";
12374
12375 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12376 if (dbfd == NULL
12377 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12378 {
12379 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12380 dwp_name = objfile_name (objfile);
12381 dwp_name += ".dwp";
12382 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12383 }
12384
12385 if (dbfd == NULL)
12386 {
12387 if (dwarf_read_debug)
12388 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12389 return std::unique_ptr<dwp_file> ();
12390 }
12391
12392 const char *name = bfd_get_filename (dbfd.get ());
12393 std::unique_ptr<struct dwp_file> dwp_file
12394 (new struct dwp_file (name, std::move (dbfd)));
12395
12396 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12397 dwp_file->elf_sections =
12398 OBSTACK_CALLOC (&objfile->objfile_obstack,
12399 dwp_file->num_sections, asection *);
12400
12401 bfd_map_over_sections (dwp_file->dbfd.get (),
12402 dwarf2_locate_common_dwp_sections,
12403 dwp_file.get ());
12404
12405 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12406 0);
12407
12408 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12409 1);
12410
12411 /* The DWP file version is stored in the hash table. Oh well. */
12412 if (dwp_file->cus && dwp_file->tus
12413 && dwp_file->cus->version != dwp_file->tus->version)
12414 {
12415 /* Technically speaking, we should try to limp along, but this is
12416 pretty bizarre. We use pulongest here because that's the established
12417 portability solution (e.g, we cannot use %u for uint32_t). */
12418 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12419 " TU version %s [in DWP file %s]"),
12420 pulongest (dwp_file->cus->version),
12421 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12422 }
12423
12424 if (dwp_file->cus)
12425 dwp_file->version = dwp_file->cus->version;
12426 else if (dwp_file->tus)
12427 dwp_file->version = dwp_file->tus->version;
12428 else
12429 dwp_file->version = 2;
12430
12431 if (dwp_file->version == 2)
12432 bfd_map_over_sections (dwp_file->dbfd.get (),
12433 dwarf2_locate_v2_dwp_sections,
12434 dwp_file.get ());
12435
12436 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12437 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12438
12439 if (dwarf_read_debug)
12440 {
12441 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12442 fprintf_unfiltered (gdb_stdlog,
12443 " %s CUs, %s TUs\n",
12444 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12445 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12446 }
12447
12448 return dwp_file;
12449 }
12450
12451 /* Wrapper around open_and_init_dwp_file, only open it once. */
12452
12453 static struct dwp_file *
12454 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12455 {
12456 if (! dwarf2_per_objfile->dwp_checked)
12457 {
12458 dwarf2_per_objfile->dwp_file
12459 = open_and_init_dwp_file (dwarf2_per_objfile);
12460 dwarf2_per_objfile->dwp_checked = 1;
12461 }
12462 return dwarf2_per_objfile->dwp_file.get ();
12463 }
12464
12465 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12466 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12467 or in the DWP file for the objfile, referenced by THIS_UNIT.
12468 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12469 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12470
12471 This is called, for example, when wanting to read a variable with a
12472 complex location. Therefore we don't want to do file i/o for every call.
12473 Therefore we don't want to look for a DWO file on every call.
12474 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12475 then we check if we've already seen DWO_NAME, and only THEN do we check
12476 for a DWO file.
12477
12478 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12479 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12480
12481 static struct dwo_unit *
12482 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12483 const char *dwo_name, const char *comp_dir,
12484 ULONGEST signature, int is_debug_types)
12485 {
12486 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12487 struct objfile *objfile = dwarf2_per_objfile->objfile;
12488 const char *kind = is_debug_types ? "TU" : "CU";
12489 void **dwo_file_slot;
12490 struct dwo_file *dwo_file;
12491 struct dwp_file *dwp_file;
12492
12493 /* First see if there's a DWP file.
12494 If we have a DWP file but didn't find the DWO inside it, don't
12495 look for the original DWO file. It makes gdb behave differently
12496 depending on whether one is debugging in the build tree. */
12497
12498 dwp_file = get_dwp_file (dwarf2_per_objfile);
12499 if (dwp_file != NULL)
12500 {
12501 const struct dwp_hash_table *dwp_htab =
12502 is_debug_types ? dwp_file->tus : dwp_file->cus;
12503
12504 if (dwp_htab != NULL)
12505 {
12506 struct dwo_unit *dwo_cutu =
12507 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12508 signature, is_debug_types);
12509
12510 if (dwo_cutu != NULL)
12511 {
12512 if (dwarf_read_debug)
12513 {
12514 fprintf_unfiltered (gdb_stdlog,
12515 "Virtual DWO %s %s found: @%s\n",
12516 kind, hex_string (signature),
12517 host_address_to_string (dwo_cutu));
12518 }
12519 return dwo_cutu;
12520 }
12521 }
12522 }
12523 else
12524 {
12525 /* No DWP file, look for the DWO file. */
12526
12527 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12528 dwo_name, comp_dir);
12529 if (*dwo_file_slot == NULL)
12530 {
12531 /* Read in the file and build a table of the CUs/TUs it contains. */
12532 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12533 }
12534 /* NOTE: This will be NULL if unable to open the file. */
12535 dwo_file = (struct dwo_file *) *dwo_file_slot;
12536
12537 if (dwo_file != NULL)
12538 {
12539 struct dwo_unit *dwo_cutu = NULL;
12540
12541 if (is_debug_types && dwo_file->tus)
12542 {
12543 struct dwo_unit find_dwo_cutu;
12544
12545 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12546 find_dwo_cutu.signature = signature;
12547 dwo_cutu
12548 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12549 &find_dwo_cutu);
12550 }
12551 else if (!is_debug_types && dwo_file->cus)
12552 {
12553 struct dwo_unit find_dwo_cutu;
12554
12555 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12556 find_dwo_cutu.signature = signature;
12557 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12558 &find_dwo_cutu);
12559 }
12560
12561 if (dwo_cutu != NULL)
12562 {
12563 if (dwarf_read_debug)
12564 {
12565 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12566 kind, dwo_name, hex_string (signature),
12567 host_address_to_string (dwo_cutu));
12568 }
12569 return dwo_cutu;
12570 }
12571 }
12572 }
12573
12574 /* We didn't find it. This could mean a dwo_id mismatch, or
12575 someone deleted the DWO/DWP file, or the search path isn't set up
12576 correctly to find the file. */
12577
12578 if (dwarf_read_debug)
12579 {
12580 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12581 kind, dwo_name, hex_string (signature));
12582 }
12583
12584 /* This is a warning and not a complaint because it can be caused by
12585 pilot error (e.g., user accidentally deleting the DWO). */
12586 {
12587 /* Print the name of the DWP file if we looked there, helps the user
12588 better diagnose the problem. */
12589 std::string dwp_text;
12590
12591 if (dwp_file != NULL)
12592 dwp_text = string_printf (" [in DWP file %s]",
12593 lbasename (dwp_file->name));
12594
12595 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12596 " [in module %s]"),
12597 kind, dwo_name, hex_string (signature),
12598 dwp_text.c_str (),
12599 this_unit->is_debug_types ? "TU" : "CU",
12600 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12601 }
12602 return NULL;
12603 }
12604
12605 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12606 See lookup_dwo_cutu_unit for details. */
12607
12608 static struct dwo_unit *
12609 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12610 const char *dwo_name, const char *comp_dir,
12611 ULONGEST signature)
12612 {
12613 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12614 }
12615
12616 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12617 See lookup_dwo_cutu_unit for details. */
12618
12619 static struct dwo_unit *
12620 lookup_dwo_type_unit (struct signatured_type *this_tu,
12621 const char *dwo_name, const char *comp_dir)
12622 {
12623 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12624 }
12625
12626 /* Traversal function for queue_and_load_all_dwo_tus. */
12627
12628 static int
12629 queue_and_load_dwo_tu (void **slot, void *info)
12630 {
12631 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12632 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12633 ULONGEST signature = dwo_unit->signature;
12634 struct signatured_type *sig_type =
12635 lookup_dwo_signatured_type (per_cu->cu, signature);
12636
12637 if (sig_type != NULL)
12638 {
12639 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12640
12641 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12642 a real dependency of PER_CU on SIG_TYPE. That is detected later
12643 while processing PER_CU. */
12644 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12645 load_full_type_unit (sig_cu);
12646 per_cu->imported_symtabs_push (sig_cu);
12647 }
12648
12649 return 1;
12650 }
12651
12652 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12653 The DWO may have the only definition of the type, though it may not be
12654 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12655 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12656
12657 static void
12658 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12659 {
12660 struct dwo_unit *dwo_unit;
12661 struct dwo_file *dwo_file;
12662
12663 gdb_assert (!per_cu->is_debug_types);
12664 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12665 gdb_assert (per_cu->cu != NULL);
12666
12667 dwo_unit = per_cu->cu->dwo_unit;
12668 gdb_assert (dwo_unit != NULL);
12669
12670 dwo_file = dwo_unit->dwo_file;
12671 if (dwo_file->tus != NULL)
12672 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12673 per_cu);
12674 }
12675
12676 /* Read in various DIEs. */
12677
12678 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12679 Inherit only the children of the DW_AT_abstract_origin DIE not being
12680 already referenced by DW_AT_abstract_origin from the children of the
12681 current DIE. */
12682
12683 static void
12684 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12685 {
12686 struct die_info *child_die;
12687 sect_offset *offsetp;
12688 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12689 struct die_info *origin_die;
12690 /* Iterator of the ORIGIN_DIE children. */
12691 struct die_info *origin_child_die;
12692 struct attribute *attr;
12693 struct dwarf2_cu *origin_cu;
12694 struct pending **origin_previous_list_in_scope;
12695
12696 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12697 if (!attr)
12698 return;
12699
12700 /* Note that following die references may follow to a die in a
12701 different cu. */
12702
12703 origin_cu = cu;
12704 origin_die = follow_die_ref (die, attr, &origin_cu);
12705
12706 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12707 symbols in. */
12708 origin_previous_list_in_scope = origin_cu->list_in_scope;
12709 origin_cu->list_in_scope = cu->list_in_scope;
12710
12711 if (die->tag != origin_die->tag
12712 && !(die->tag == DW_TAG_inlined_subroutine
12713 && origin_die->tag == DW_TAG_subprogram))
12714 complaint (_("DIE %s and its abstract origin %s have different tags"),
12715 sect_offset_str (die->sect_off),
12716 sect_offset_str (origin_die->sect_off));
12717
12718 std::vector<sect_offset> offsets;
12719
12720 for (child_die = die->child;
12721 child_die && child_die->tag;
12722 child_die = sibling_die (child_die))
12723 {
12724 struct die_info *child_origin_die;
12725 struct dwarf2_cu *child_origin_cu;
12726
12727 /* We are trying to process concrete instance entries:
12728 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12729 it's not relevant to our analysis here. i.e. detecting DIEs that are
12730 present in the abstract instance but not referenced in the concrete
12731 one. */
12732 if (child_die->tag == DW_TAG_call_site
12733 || child_die->tag == DW_TAG_GNU_call_site)
12734 continue;
12735
12736 /* For each CHILD_DIE, find the corresponding child of
12737 ORIGIN_DIE. If there is more than one layer of
12738 DW_AT_abstract_origin, follow them all; there shouldn't be,
12739 but GCC versions at least through 4.4 generate this (GCC PR
12740 40573). */
12741 child_origin_die = child_die;
12742 child_origin_cu = cu;
12743 while (1)
12744 {
12745 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12746 child_origin_cu);
12747 if (attr == NULL)
12748 break;
12749 child_origin_die = follow_die_ref (child_origin_die, attr,
12750 &child_origin_cu);
12751 }
12752
12753 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12754 counterpart may exist. */
12755 if (child_origin_die != child_die)
12756 {
12757 if (child_die->tag != child_origin_die->tag
12758 && !(child_die->tag == DW_TAG_inlined_subroutine
12759 && child_origin_die->tag == DW_TAG_subprogram))
12760 complaint (_("Child DIE %s and its abstract origin %s have "
12761 "different tags"),
12762 sect_offset_str (child_die->sect_off),
12763 sect_offset_str (child_origin_die->sect_off));
12764 if (child_origin_die->parent != origin_die)
12765 complaint (_("Child DIE %s and its abstract origin %s have "
12766 "different parents"),
12767 sect_offset_str (child_die->sect_off),
12768 sect_offset_str (child_origin_die->sect_off));
12769 else
12770 offsets.push_back (child_origin_die->sect_off);
12771 }
12772 }
12773 std::sort (offsets.begin (), offsets.end ());
12774 sect_offset *offsets_end = offsets.data () + offsets.size ();
12775 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12776 if (offsetp[-1] == *offsetp)
12777 complaint (_("Multiple children of DIE %s refer "
12778 "to DIE %s as their abstract origin"),
12779 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12780
12781 offsetp = offsets.data ();
12782 origin_child_die = origin_die->child;
12783 while (origin_child_die && origin_child_die->tag)
12784 {
12785 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12786 while (offsetp < offsets_end
12787 && *offsetp < origin_child_die->sect_off)
12788 offsetp++;
12789 if (offsetp >= offsets_end
12790 || *offsetp > origin_child_die->sect_off)
12791 {
12792 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12793 Check whether we're already processing ORIGIN_CHILD_DIE.
12794 This can happen with mutually referenced abstract_origins.
12795 PR 16581. */
12796 if (!origin_child_die->in_process)
12797 process_die (origin_child_die, origin_cu);
12798 }
12799 origin_child_die = sibling_die (origin_child_die);
12800 }
12801 origin_cu->list_in_scope = origin_previous_list_in_scope;
12802
12803 if (cu != origin_cu)
12804 compute_delayed_physnames (origin_cu);
12805 }
12806
12807 static void
12808 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12809 {
12810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12811 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12812 struct context_stack *newobj;
12813 CORE_ADDR lowpc;
12814 CORE_ADDR highpc;
12815 struct die_info *child_die;
12816 struct attribute *attr, *call_line, *call_file;
12817 const char *name;
12818 CORE_ADDR baseaddr;
12819 struct block *block;
12820 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12821 std::vector<struct symbol *> template_args;
12822 struct template_symbol *templ_func = NULL;
12823
12824 if (inlined_func)
12825 {
12826 /* If we do not have call site information, we can't show the
12827 caller of this inlined function. That's too confusing, so
12828 only use the scope for local variables. */
12829 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12830 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12831 if (call_line == NULL || call_file == NULL)
12832 {
12833 read_lexical_block_scope (die, cu);
12834 return;
12835 }
12836 }
12837
12838 baseaddr = objfile->text_section_offset ();
12839
12840 name = dwarf2_name (die, cu);
12841
12842 /* Ignore functions with missing or empty names. These are actually
12843 illegal according to the DWARF standard. */
12844 if (name == NULL)
12845 {
12846 complaint (_("missing name for subprogram DIE at %s"),
12847 sect_offset_str (die->sect_off));
12848 return;
12849 }
12850
12851 /* Ignore functions with missing or invalid low and high pc attributes. */
12852 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12853 <= PC_BOUNDS_INVALID)
12854 {
12855 attr = dwarf2_attr (die, DW_AT_external, cu);
12856 if (!attr || !DW_UNSND (attr))
12857 complaint (_("cannot get low and high bounds "
12858 "for subprogram DIE at %s"),
12859 sect_offset_str (die->sect_off));
12860 return;
12861 }
12862
12863 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12864 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12865
12866 /* If we have any template arguments, then we must allocate a
12867 different sort of symbol. */
12868 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12869 {
12870 if (child_die->tag == DW_TAG_template_type_param
12871 || child_die->tag == DW_TAG_template_value_param)
12872 {
12873 templ_func = allocate_template_symbol (objfile);
12874 templ_func->subclass = SYMBOL_TEMPLATE;
12875 break;
12876 }
12877 }
12878
12879 newobj = cu->get_builder ()->push_context (0, lowpc);
12880 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12881 (struct symbol *) templ_func);
12882
12883 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12884 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12885 cu->language);
12886
12887 /* If there is a location expression for DW_AT_frame_base, record
12888 it. */
12889 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12890 if (attr != nullptr)
12891 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12892
12893 /* If there is a location for the static link, record it. */
12894 newobj->static_link = NULL;
12895 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12896 if (attr != nullptr)
12897 {
12898 newobj->static_link
12899 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12900 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12901 cu->per_cu->addr_type ());
12902 }
12903
12904 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12905
12906 if (die->child != NULL)
12907 {
12908 child_die = die->child;
12909 while (child_die && child_die->tag)
12910 {
12911 if (child_die->tag == DW_TAG_template_type_param
12912 || child_die->tag == DW_TAG_template_value_param)
12913 {
12914 struct symbol *arg = new_symbol (child_die, NULL, cu);
12915
12916 if (arg != NULL)
12917 template_args.push_back (arg);
12918 }
12919 else
12920 process_die (child_die, cu);
12921 child_die = sibling_die (child_die);
12922 }
12923 }
12924
12925 inherit_abstract_dies (die, cu);
12926
12927 /* If we have a DW_AT_specification, we might need to import using
12928 directives from the context of the specification DIE. See the
12929 comment in determine_prefix. */
12930 if (cu->language == language_cplus
12931 && dwarf2_attr (die, DW_AT_specification, cu))
12932 {
12933 struct dwarf2_cu *spec_cu = cu;
12934 struct die_info *spec_die = die_specification (die, &spec_cu);
12935
12936 while (spec_die)
12937 {
12938 child_die = spec_die->child;
12939 while (child_die && child_die->tag)
12940 {
12941 if (child_die->tag == DW_TAG_imported_module)
12942 process_die (child_die, spec_cu);
12943 child_die = sibling_die (child_die);
12944 }
12945
12946 /* In some cases, GCC generates specification DIEs that
12947 themselves contain DW_AT_specification attributes. */
12948 spec_die = die_specification (spec_die, &spec_cu);
12949 }
12950 }
12951
12952 struct context_stack cstk = cu->get_builder ()->pop_context ();
12953 /* Make a block for the local symbols within. */
12954 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12955 cstk.static_link, lowpc, highpc);
12956
12957 /* For C++, set the block's scope. */
12958 if ((cu->language == language_cplus
12959 || cu->language == language_fortran
12960 || cu->language == language_d
12961 || cu->language == language_rust)
12962 && cu->processing_has_namespace_info)
12963 block_set_scope (block, determine_prefix (die, cu),
12964 &objfile->objfile_obstack);
12965
12966 /* If we have address ranges, record them. */
12967 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12968
12969 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12970
12971 /* Attach template arguments to function. */
12972 if (!template_args.empty ())
12973 {
12974 gdb_assert (templ_func != NULL);
12975
12976 templ_func->n_template_arguments = template_args.size ();
12977 templ_func->template_arguments
12978 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12979 templ_func->n_template_arguments);
12980 memcpy (templ_func->template_arguments,
12981 template_args.data (),
12982 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12983
12984 /* Make sure that the symtab is set on the new symbols. Even
12985 though they don't appear in this symtab directly, other parts
12986 of gdb assume that symbols do, and this is reasonably
12987 true. */
12988 for (symbol *sym : template_args)
12989 symbol_set_symtab (sym, symbol_symtab (templ_func));
12990 }
12991
12992 /* In C++, we can have functions nested inside functions (e.g., when
12993 a function declares a class that has methods). This means that
12994 when we finish processing a function scope, we may need to go
12995 back to building a containing block's symbol lists. */
12996 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12997 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12998
12999 /* If we've finished processing a top-level function, subsequent
13000 symbols go in the file symbol list. */
13001 if (cu->get_builder ()->outermost_context_p ())
13002 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13003 }
13004
13005 /* Process all the DIES contained within a lexical block scope. Start
13006 a new scope, process the dies, and then close the scope. */
13007
13008 static void
13009 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13010 {
13011 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13012 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13013 CORE_ADDR lowpc, highpc;
13014 struct die_info *child_die;
13015 CORE_ADDR baseaddr;
13016
13017 baseaddr = objfile->text_section_offset ();
13018
13019 /* Ignore blocks with missing or invalid low and high pc attributes. */
13020 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13021 as multiple lexical blocks? Handling children in a sane way would
13022 be nasty. Might be easier to properly extend generic blocks to
13023 describe ranges. */
13024 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13025 {
13026 case PC_BOUNDS_NOT_PRESENT:
13027 /* DW_TAG_lexical_block has no attributes, process its children as if
13028 there was no wrapping by that DW_TAG_lexical_block.
13029 GCC does no longer produces such DWARF since GCC r224161. */
13030 for (child_die = die->child;
13031 child_die != NULL && child_die->tag;
13032 child_die = sibling_die (child_die))
13033 process_die (child_die, cu);
13034 return;
13035 case PC_BOUNDS_INVALID:
13036 return;
13037 }
13038 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13039 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13040
13041 cu->get_builder ()->push_context (0, lowpc);
13042 if (die->child != NULL)
13043 {
13044 child_die = die->child;
13045 while (child_die && child_die->tag)
13046 {
13047 process_die (child_die, cu);
13048 child_die = sibling_die (child_die);
13049 }
13050 }
13051 inherit_abstract_dies (die, cu);
13052 struct context_stack cstk = cu->get_builder ()->pop_context ();
13053
13054 if (*cu->get_builder ()->get_local_symbols () != NULL
13055 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13056 {
13057 struct block *block
13058 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13059 cstk.start_addr, highpc);
13060
13061 /* Note that recording ranges after traversing children, as we
13062 do here, means that recording a parent's ranges entails
13063 walking across all its children's ranges as they appear in
13064 the address map, which is quadratic behavior.
13065
13066 It would be nicer to record the parent's ranges before
13067 traversing its children, simply overriding whatever you find
13068 there. But since we don't even decide whether to create a
13069 block until after we've traversed its children, that's hard
13070 to do. */
13071 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13072 }
13073 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13074 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13075 }
13076
13077 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13078
13079 static void
13080 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13081 {
13082 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13083 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13084 CORE_ADDR pc, baseaddr;
13085 struct attribute *attr;
13086 struct call_site *call_site, call_site_local;
13087 void **slot;
13088 int nparams;
13089 struct die_info *child_die;
13090
13091 baseaddr = objfile->text_section_offset ();
13092
13093 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13094 if (attr == NULL)
13095 {
13096 /* This was a pre-DWARF-5 GNU extension alias
13097 for DW_AT_call_return_pc. */
13098 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13099 }
13100 if (!attr)
13101 {
13102 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13103 "DIE %s [in module %s]"),
13104 sect_offset_str (die->sect_off), objfile_name (objfile));
13105 return;
13106 }
13107 pc = attr->value_as_address () + baseaddr;
13108 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13109
13110 if (cu->call_site_htab == NULL)
13111 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13112 NULL, &objfile->objfile_obstack,
13113 hashtab_obstack_allocate, NULL);
13114 call_site_local.pc = pc;
13115 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13116 if (*slot != NULL)
13117 {
13118 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13119 "DIE %s [in module %s]"),
13120 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13121 objfile_name (objfile));
13122 return;
13123 }
13124
13125 /* Count parameters at the caller. */
13126
13127 nparams = 0;
13128 for (child_die = die->child; child_die && child_die->tag;
13129 child_die = sibling_die (child_die))
13130 {
13131 if (child_die->tag != DW_TAG_call_site_parameter
13132 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13133 {
13134 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13135 "DW_TAG_call_site child DIE %s [in module %s]"),
13136 child_die->tag, sect_offset_str (child_die->sect_off),
13137 objfile_name (objfile));
13138 continue;
13139 }
13140
13141 nparams++;
13142 }
13143
13144 call_site
13145 = ((struct call_site *)
13146 obstack_alloc (&objfile->objfile_obstack,
13147 sizeof (*call_site)
13148 + (sizeof (*call_site->parameter) * (nparams - 1))));
13149 *slot = call_site;
13150 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13151 call_site->pc = pc;
13152
13153 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13154 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13155 {
13156 struct die_info *func_die;
13157
13158 /* Skip also over DW_TAG_inlined_subroutine. */
13159 for (func_die = die->parent;
13160 func_die && func_die->tag != DW_TAG_subprogram
13161 && func_die->tag != DW_TAG_subroutine_type;
13162 func_die = func_die->parent);
13163
13164 /* DW_AT_call_all_calls is a superset
13165 of DW_AT_call_all_tail_calls. */
13166 if (func_die
13167 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13168 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13169 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13170 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13171 {
13172 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13173 not complete. But keep CALL_SITE for look ups via call_site_htab,
13174 both the initial caller containing the real return address PC and
13175 the final callee containing the current PC of a chain of tail
13176 calls do not need to have the tail call list complete. But any
13177 function candidate for a virtual tail call frame searched via
13178 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13179 determined unambiguously. */
13180 }
13181 else
13182 {
13183 struct type *func_type = NULL;
13184
13185 if (func_die)
13186 func_type = get_die_type (func_die, cu);
13187 if (func_type != NULL)
13188 {
13189 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13190
13191 /* Enlist this call site to the function. */
13192 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13193 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13194 }
13195 else
13196 complaint (_("Cannot find function owning DW_TAG_call_site "
13197 "DIE %s [in module %s]"),
13198 sect_offset_str (die->sect_off), objfile_name (objfile));
13199 }
13200 }
13201
13202 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13203 if (attr == NULL)
13204 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13205 if (attr == NULL)
13206 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13207 if (attr == NULL)
13208 {
13209 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13210 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13211 }
13212 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13213 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13214 /* Keep NULL DWARF_BLOCK. */;
13215 else if (attr->form_is_block ())
13216 {
13217 struct dwarf2_locexpr_baton *dlbaton;
13218
13219 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13220 dlbaton->data = DW_BLOCK (attr)->data;
13221 dlbaton->size = DW_BLOCK (attr)->size;
13222 dlbaton->per_cu = cu->per_cu;
13223
13224 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13225 }
13226 else if (attr->form_is_ref ())
13227 {
13228 struct dwarf2_cu *target_cu = cu;
13229 struct die_info *target_die;
13230
13231 target_die = follow_die_ref (die, attr, &target_cu);
13232 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13233 if (die_is_declaration (target_die, target_cu))
13234 {
13235 const char *target_physname;
13236
13237 /* Prefer the mangled name; otherwise compute the demangled one. */
13238 target_physname = dw2_linkage_name (target_die, target_cu);
13239 if (target_physname == NULL)
13240 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13241 if (target_physname == NULL)
13242 complaint (_("DW_AT_call_target target DIE has invalid "
13243 "physname, for referencing DIE %s [in module %s]"),
13244 sect_offset_str (die->sect_off), objfile_name (objfile));
13245 else
13246 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13247 }
13248 else
13249 {
13250 CORE_ADDR lowpc;
13251
13252 /* DW_AT_entry_pc should be preferred. */
13253 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13254 <= PC_BOUNDS_INVALID)
13255 complaint (_("DW_AT_call_target target DIE has invalid "
13256 "low pc, for referencing DIE %s [in module %s]"),
13257 sect_offset_str (die->sect_off), objfile_name (objfile));
13258 else
13259 {
13260 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13261 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13262 }
13263 }
13264 }
13265 else
13266 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13267 "block nor reference, for DIE %s [in module %s]"),
13268 sect_offset_str (die->sect_off), objfile_name (objfile));
13269
13270 call_site->per_cu = cu->per_cu;
13271
13272 for (child_die = die->child;
13273 child_die && child_die->tag;
13274 child_die = sibling_die (child_die))
13275 {
13276 struct call_site_parameter *parameter;
13277 struct attribute *loc, *origin;
13278
13279 if (child_die->tag != DW_TAG_call_site_parameter
13280 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13281 {
13282 /* Already printed the complaint above. */
13283 continue;
13284 }
13285
13286 gdb_assert (call_site->parameter_count < nparams);
13287 parameter = &call_site->parameter[call_site->parameter_count];
13288
13289 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13290 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13291 register is contained in DW_AT_call_value. */
13292
13293 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13294 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13295 if (origin == NULL)
13296 {
13297 /* This was a pre-DWARF-5 GNU extension alias
13298 for DW_AT_call_parameter. */
13299 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13300 }
13301 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13302 {
13303 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13304
13305 sect_offset sect_off
13306 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13307 if (!cu->header.offset_in_cu_p (sect_off))
13308 {
13309 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13310 binding can be done only inside one CU. Such referenced DIE
13311 therefore cannot be even moved to DW_TAG_partial_unit. */
13312 complaint (_("DW_AT_call_parameter offset is not in CU for "
13313 "DW_TAG_call_site child DIE %s [in module %s]"),
13314 sect_offset_str (child_die->sect_off),
13315 objfile_name (objfile));
13316 continue;
13317 }
13318 parameter->u.param_cu_off
13319 = (cu_offset) (sect_off - cu->header.sect_off);
13320 }
13321 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13322 {
13323 complaint (_("No DW_FORM_block* DW_AT_location for "
13324 "DW_TAG_call_site child DIE %s [in module %s]"),
13325 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13326 continue;
13327 }
13328 else
13329 {
13330 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13331 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13332 if (parameter->u.dwarf_reg != -1)
13333 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13334 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13335 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13336 &parameter->u.fb_offset))
13337 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13338 else
13339 {
13340 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13341 "for DW_FORM_block* DW_AT_location is supported for "
13342 "DW_TAG_call_site child DIE %s "
13343 "[in module %s]"),
13344 sect_offset_str (child_die->sect_off),
13345 objfile_name (objfile));
13346 continue;
13347 }
13348 }
13349
13350 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13351 if (attr == NULL)
13352 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13353 if (attr == NULL || !attr->form_is_block ())
13354 {
13355 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13356 "DW_TAG_call_site child DIE %s [in module %s]"),
13357 sect_offset_str (child_die->sect_off),
13358 objfile_name (objfile));
13359 continue;
13360 }
13361 parameter->value = DW_BLOCK (attr)->data;
13362 parameter->value_size = DW_BLOCK (attr)->size;
13363
13364 /* Parameters are not pre-cleared by memset above. */
13365 parameter->data_value = NULL;
13366 parameter->data_value_size = 0;
13367 call_site->parameter_count++;
13368
13369 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13370 if (attr == NULL)
13371 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13372 if (attr != nullptr)
13373 {
13374 if (!attr->form_is_block ())
13375 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13376 "DW_TAG_call_site child DIE %s [in module %s]"),
13377 sect_offset_str (child_die->sect_off),
13378 objfile_name (objfile));
13379 else
13380 {
13381 parameter->data_value = DW_BLOCK (attr)->data;
13382 parameter->data_value_size = DW_BLOCK (attr)->size;
13383 }
13384 }
13385 }
13386 }
13387
13388 /* Helper function for read_variable. If DIE represents a virtual
13389 table, then return the type of the concrete object that is
13390 associated with the virtual table. Otherwise, return NULL. */
13391
13392 static struct type *
13393 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13394 {
13395 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13396 if (attr == NULL)
13397 return NULL;
13398
13399 /* Find the type DIE. */
13400 struct die_info *type_die = NULL;
13401 struct dwarf2_cu *type_cu = cu;
13402
13403 if (attr->form_is_ref ())
13404 type_die = follow_die_ref (die, attr, &type_cu);
13405 if (type_die == NULL)
13406 return NULL;
13407
13408 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13409 return NULL;
13410 return die_containing_type (type_die, type_cu);
13411 }
13412
13413 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13414
13415 static void
13416 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13417 {
13418 struct rust_vtable_symbol *storage = NULL;
13419
13420 if (cu->language == language_rust)
13421 {
13422 struct type *containing_type = rust_containing_type (die, cu);
13423
13424 if (containing_type != NULL)
13425 {
13426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13427
13428 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13429 initialize_objfile_symbol (storage);
13430 storage->concrete_type = containing_type;
13431 storage->subclass = SYMBOL_RUST_VTABLE;
13432 }
13433 }
13434
13435 struct symbol *res = new_symbol (die, NULL, cu, storage);
13436 struct attribute *abstract_origin
13437 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13438 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13439 if (res == NULL && loc && abstract_origin)
13440 {
13441 /* We have a variable without a name, but with a location and an abstract
13442 origin. This may be a concrete instance of an abstract variable
13443 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13444 later. */
13445 struct dwarf2_cu *origin_cu = cu;
13446 struct die_info *origin_die
13447 = follow_die_ref (die, abstract_origin, &origin_cu);
13448 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13449 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13450 }
13451 }
13452
13453 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13454 reading .debug_rnglists.
13455 Callback's type should be:
13456 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13457 Return true if the attributes are present and valid, otherwise,
13458 return false. */
13459
13460 template <typename Callback>
13461 static bool
13462 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13463 Callback &&callback)
13464 {
13465 struct dwarf2_per_objfile *dwarf2_per_objfile
13466 = cu->per_cu->dwarf2_per_objfile;
13467 struct objfile *objfile = dwarf2_per_objfile->objfile;
13468 bfd *obfd = objfile->obfd;
13469 /* Base address selection entry. */
13470 CORE_ADDR base;
13471 int found_base;
13472 const gdb_byte *buffer;
13473 CORE_ADDR baseaddr;
13474 bool overflow = false;
13475
13476 found_base = cu->base_known;
13477 base = cu->base_address;
13478
13479 dwarf2_per_objfile->rnglists.read (objfile);
13480 if (offset >= dwarf2_per_objfile->rnglists.size)
13481 {
13482 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13483 offset);
13484 return false;
13485 }
13486 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13487
13488 baseaddr = objfile->text_section_offset ();
13489
13490 while (1)
13491 {
13492 /* Initialize it due to a false compiler warning. */
13493 CORE_ADDR range_beginning = 0, range_end = 0;
13494 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13495 + dwarf2_per_objfile->rnglists.size);
13496 unsigned int bytes_read;
13497
13498 if (buffer == buf_end)
13499 {
13500 overflow = true;
13501 break;
13502 }
13503 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13504 switch (rlet)
13505 {
13506 case DW_RLE_end_of_list:
13507 break;
13508 case DW_RLE_base_address:
13509 if (buffer + cu->header.addr_size > buf_end)
13510 {
13511 overflow = true;
13512 break;
13513 }
13514 base = cu->header.read_address (obfd, buffer, &bytes_read);
13515 found_base = 1;
13516 buffer += bytes_read;
13517 break;
13518 case DW_RLE_start_length:
13519 if (buffer + cu->header.addr_size > buf_end)
13520 {
13521 overflow = true;
13522 break;
13523 }
13524 range_beginning = cu->header.read_address (obfd, buffer,
13525 &bytes_read);
13526 buffer += bytes_read;
13527 range_end = (range_beginning
13528 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13529 buffer += bytes_read;
13530 if (buffer > buf_end)
13531 {
13532 overflow = true;
13533 break;
13534 }
13535 break;
13536 case DW_RLE_offset_pair:
13537 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13538 buffer += bytes_read;
13539 if (buffer > buf_end)
13540 {
13541 overflow = true;
13542 break;
13543 }
13544 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13545 buffer += bytes_read;
13546 if (buffer > buf_end)
13547 {
13548 overflow = true;
13549 break;
13550 }
13551 break;
13552 case DW_RLE_start_end:
13553 if (buffer + 2 * cu->header.addr_size > buf_end)
13554 {
13555 overflow = true;
13556 break;
13557 }
13558 range_beginning = cu->header.read_address (obfd, buffer,
13559 &bytes_read);
13560 buffer += bytes_read;
13561 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13562 buffer += bytes_read;
13563 break;
13564 default:
13565 complaint (_("Invalid .debug_rnglists data (no base address)"));
13566 return false;
13567 }
13568 if (rlet == DW_RLE_end_of_list || overflow)
13569 break;
13570 if (rlet == DW_RLE_base_address)
13571 continue;
13572
13573 if (!found_base)
13574 {
13575 /* We have no valid base address for the ranges
13576 data. */
13577 complaint (_("Invalid .debug_rnglists data (no base address)"));
13578 return false;
13579 }
13580
13581 if (range_beginning > range_end)
13582 {
13583 /* Inverted range entries are invalid. */
13584 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13585 return false;
13586 }
13587
13588 /* Empty range entries have no effect. */
13589 if (range_beginning == range_end)
13590 continue;
13591
13592 range_beginning += base;
13593 range_end += base;
13594
13595 /* A not-uncommon case of bad debug info.
13596 Don't pollute the addrmap with bad data. */
13597 if (range_beginning + baseaddr == 0
13598 && !dwarf2_per_objfile->has_section_at_zero)
13599 {
13600 complaint (_(".debug_rnglists entry has start address of zero"
13601 " [in module %s]"), objfile_name (objfile));
13602 continue;
13603 }
13604
13605 callback (range_beginning, range_end);
13606 }
13607
13608 if (overflow)
13609 {
13610 complaint (_("Offset %d is not terminated "
13611 "for DW_AT_ranges attribute"),
13612 offset);
13613 return false;
13614 }
13615
13616 return true;
13617 }
13618
13619 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13620 Callback's type should be:
13621 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13622 Return 1 if the attributes are present and valid, otherwise, return 0. */
13623
13624 template <typename Callback>
13625 static int
13626 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13627 Callback &&callback)
13628 {
13629 struct dwarf2_per_objfile *dwarf2_per_objfile
13630 = cu->per_cu->dwarf2_per_objfile;
13631 struct objfile *objfile = dwarf2_per_objfile->objfile;
13632 struct comp_unit_head *cu_header = &cu->header;
13633 bfd *obfd = objfile->obfd;
13634 unsigned int addr_size = cu_header->addr_size;
13635 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13636 /* Base address selection entry. */
13637 CORE_ADDR base;
13638 int found_base;
13639 unsigned int dummy;
13640 const gdb_byte *buffer;
13641 CORE_ADDR baseaddr;
13642
13643 if (cu_header->version >= 5)
13644 return dwarf2_rnglists_process (offset, cu, callback);
13645
13646 found_base = cu->base_known;
13647 base = cu->base_address;
13648
13649 dwarf2_per_objfile->ranges.read (objfile);
13650 if (offset >= dwarf2_per_objfile->ranges.size)
13651 {
13652 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13653 offset);
13654 return 0;
13655 }
13656 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13657
13658 baseaddr = objfile->text_section_offset ();
13659
13660 while (1)
13661 {
13662 CORE_ADDR range_beginning, range_end;
13663
13664 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13665 buffer += addr_size;
13666 range_end = cu->header.read_address (obfd, buffer, &dummy);
13667 buffer += addr_size;
13668 offset += 2 * addr_size;
13669
13670 /* An end of list marker is a pair of zero addresses. */
13671 if (range_beginning == 0 && range_end == 0)
13672 /* Found the end of list entry. */
13673 break;
13674
13675 /* Each base address selection entry is a pair of 2 values.
13676 The first is the largest possible address, the second is
13677 the base address. Check for a base address here. */
13678 if ((range_beginning & mask) == mask)
13679 {
13680 /* If we found the largest possible address, then we already
13681 have the base address in range_end. */
13682 base = range_end;
13683 found_base = 1;
13684 continue;
13685 }
13686
13687 if (!found_base)
13688 {
13689 /* We have no valid base address for the ranges
13690 data. */
13691 complaint (_("Invalid .debug_ranges data (no base address)"));
13692 return 0;
13693 }
13694
13695 if (range_beginning > range_end)
13696 {
13697 /* Inverted range entries are invalid. */
13698 complaint (_("Invalid .debug_ranges data (inverted range)"));
13699 return 0;
13700 }
13701
13702 /* Empty range entries have no effect. */
13703 if (range_beginning == range_end)
13704 continue;
13705
13706 range_beginning += base;
13707 range_end += base;
13708
13709 /* A not-uncommon case of bad debug info.
13710 Don't pollute the addrmap with bad data. */
13711 if (range_beginning + baseaddr == 0
13712 && !dwarf2_per_objfile->has_section_at_zero)
13713 {
13714 complaint (_(".debug_ranges entry has start address of zero"
13715 " [in module %s]"), objfile_name (objfile));
13716 continue;
13717 }
13718
13719 callback (range_beginning, range_end);
13720 }
13721
13722 return 1;
13723 }
13724
13725 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13726 Return 1 if the attributes are present and valid, otherwise, return 0.
13727 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13728
13729 static int
13730 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13731 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13732 dwarf2_psymtab *ranges_pst)
13733 {
13734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13735 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13736 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13737 int low_set = 0;
13738 CORE_ADDR low = 0;
13739 CORE_ADDR high = 0;
13740 int retval;
13741
13742 retval = dwarf2_ranges_process (offset, cu,
13743 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13744 {
13745 if (ranges_pst != NULL)
13746 {
13747 CORE_ADDR lowpc;
13748 CORE_ADDR highpc;
13749
13750 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13751 range_beginning + baseaddr)
13752 - baseaddr);
13753 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13754 range_end + baseaddr)
13755 - baseaddr);
13756 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13757 lowpc, highpc - 1, ranges_pst);
13758 }
13759
13760 /* FIXME: This is recording everything as a low-high
13761 segment of consecutive addresses. We should have a
13762 data structure for discontiguous block ranges
13763 instead. */
13764 if (! low_set)
13765 {
13766 low = range_beginning;
13767 high = range_end;
13768 low_set = 1;
13769 }
13770 else
13771 {
13772 if (range_beginning < low)
13773 low = range_beginning;
13774 if (range_end > high)
13775 high = range_end;
13776 }
13777 });
13778 if (!retval)
13779 return 0;
13780
13781 if (! low_set)
13782 /* If the first entry is an end-of-list marker, the range
13783 describes an empty scope, i.e. no instructions. */
13784 return 0;
13785
13786 if (low_return)
13787 *low_return = low;
13788 if (high_return)
13789 *high_return = high;
13790 return 1;
13791 }
13792
13793 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13794 definition for the return value. *LOWPC and *HIGHPC are set iff
13795 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13796
13797 static enum pc_bounds_kind
13798 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13799 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13800 dwarf2_psymtab *pst)
13801 {
13802 struct dwarf2_per_objfile *dwarf2_per_objfile
13803 = cu->per_cu->dwarf2_per_objfile;
13804 struct attribute *attr;
13805 struct attribute *attr_high;
13806 CORE_ADDR low = 0;
13807 CORE_ADDR high = 0;
13808 enum pc_bounds_kind ret;
13809
13810 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13811 if (attr_high)
13812 {
13813 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13814 if (attr != nullptr)
13815 {
13816 low = attr->value_as_address ();
13817 high = attr_high->value_as_address ();
13818 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13819 high += low;
13820 }
13821 else
13822 /* Found high w/o low attribute. */
13823 return PC_BOUNDS_INVALID;
13824
13825 /* Found consecutive range of addresses. */
13826 ret = PC_BOUNDS_HIGH_LOW;
13827 }
13828 else
13829 {
13830 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13831 if (attr != NULL)
13832 {
13833 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13834 We take advantage of the fact that DW_AT_ranges does not appear
13835 in DW_TAG_compile_unit of DWO files. */
13836 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13837 unsigned int ranges_offset = (DW_UNSND (attr)
13838 + (need_ranges_base
13839 ? cu->ranges_base
13840 : 0));
13841
13842 /* Value of the DW_AT_ranges attribute is the offset in the
13843 .debug_ranges section. */
13844 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13845 return PC_BOUNDS_INVALID;
13846 /* Found discontinuous range of addresses. */
13847 ret = PC_BOUNDS_RANGES;
13848 }
13849 else
13850 return PC_BOUNDS_NOT_PRESENT;
13851 }
13852
13853 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13854 if (high <= low)
13855 return PC_BOUNDS_INVALID;
13856
13857 /* When using the GNU linker, .gnu.linkonce. sections are used to
13858 eliminate duplicate copies of functions and vtables and such.
13859 The linker will arbitrarily choose one and discard the others.
13860 The AT_*_pc values for such functions refer to local labels in
13861 these sections. If the section from that file was discarded, the
13862 labels are not in the output, so the relocs get a value of 0.
13863 If this is a discarded function, mark the pc bounds as invalid,
13864 so that GDB will ignore it. */
13865 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13866 return PC_BOUNDS_INVALID;
13867
13868 *lowpc = low;
13869 if (highpc)
13870 *highpc = high;
13871 return ret;
13872 }
13873
13874 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13875 its low and high PC addresses. Do nothing if these addresses could not
13876 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13877 and HIGHPC to the high address if greater than HIGHPC. */
13878
13879 static void
13880 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13881 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13882 struct dwarf2_cu *cu)
13883 {
13884 CORE_ADDR low, high;
13885 struct die_info *child = die->child;
13886
13887 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13888 {
13889 *lowpc = std::min (*lowpc, low);
13890 *highpc = std::max (*highpc, high);
13891 }
13892
13893 /* If the language does not allow nested subprograms (either inside
13894 subprograms or lexical blocks), we're done. */
13895 if (cu->language != language_ada)
13896 return;
13897
13898 /* Check all the children of the given DIE. If it contains nested
13899 subprograms, then check their pc bounds. Likewise, we need to
13900 check lexical blocks as well, as they may also contain subprogram
13901 definitions. */
13902 while (child && child->tag)
13903 {
13904 if (child->tag == DW_TAG_subprogram
13905 || child->tag == DW_TAG_lexical_block)
13906 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13907 child = sibling_die (child);
13908 }
13909 }
13910
13911 /* Get the low and high pc's represented by the scope DIE, and store
13912 them in *LOWPC and *HIGHPC. If the correct values can't be
13913 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13914
13915 static void
13916 get_scope_pc_bounds (struct die_info *die,
13917 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13918 struct dwarf2_cu *cu)
13919 {
13920 CORE_ADDR best_low = (CORE_ADDR) -1;
13921 CORE_ADDR best_high = (CORE_ADDR) 0;
13922 CORE_ADDR current_low, current_high;
13923
13924 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13925 >= PC_BOUNDS_RANGES)
13926 {
13927 best_low = current_low;
13928 best_high = current_high;
13929 }
13930 else
13931 {
13932 struct die_info *child = die->child;
13933
13934 while (child && child->tag)
13935 {
13936 switch (child->tag) {
13937 case DW_TAG_subprogram:
13938 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13939 break;
13940 case DW_TAG_namespace:
13941 case DW_TAG_module:
13942 /* FIXME: carlton/2004-01-16: Should we do this for
13943 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13944 that current GCC's always emit the DIEs corresponding
13945 to definitions of methods of classes as children of a
13946 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13947 the DIEs giving the declarations, which could be
13948 anywhere). But I don't see any reason why the
13949 standards says that they have to be there. */
13950 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13951
13952 if (current_low != ((CORE_ADDR) -1))
13953 {
13954 best_low = std::min (best_low, current_low);
13955 best_high = std::max (best_high, current_high);
13956 }
13957 break;
13958 default:
13959 /* Ignore. */
13960 break;
13961 }
13962
13963 child = sibling_die (child);
13964 }
13965 }
13966
13967 *lowpc = best_low;
13968 *highpc = best_high;
13969 }
13970
13971 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13972 in DIE. */
13973
13974 static void
13975 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13976 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13977 {
13978 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13979 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13980 struct attribute *attr;
13981 struct attribute *attr_high;
13982
13983 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13984 if (attr_high)
13985 {
13986 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13987 if (attr != nullptr)
13988 {
13989 CORE_ADDR low = attr->value_as_address ();
13990 CORE_ADDR high = attr_high->value_as_address ();
13991
13992 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13993 high += low;
13994
13995 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13996 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13997 cu->get_builder ()->record_block_range (block, low, high - 1);
13998 }
13999 }
14000
14001 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14002 if (attr != nullptr)
14003 {
14004 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14005 We take advantage of the fact that DW_AT_ranges does not appear
14006 in DW_TAG_compile_unit of DWO files. */
14007 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14008
14009 /* The value of the DW_AT_ranges attribute is the offset of the
14010 address range list in the .debug_ranges section. */
14011 unsigned long offset = (DW_UNSND (attr)
14012 + (need_ranges_base ? cu->ranges_base : 0));
14013
14014 std::vector<blockrange> blockvec;
14015 dwarf2_ranges_process (offset, cu,
14016 [&] (CORE_ADDR start, CORE_ADDR end)
14017 {
14018 start += baseaddr;
14019 end += baseaddr;
14020 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14021 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14022 cu->get_builder ()->record_block_range (block, start, end - 1);
14023 blockvec.emplace_back (start, end);
14024 });
14025
14026 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14027 }
14028 }
14029
14030 /* Check whether the producer field indicates either of GCC < 4.6, or the
14031 Intel C/C++ compiler, and cache the result in CU. */
14032
14033 static void
14034 check_producer (struct dwarf2_cu *cu)
14035 {
14036 int major, minor;
14037
14038 if (cu->producer == NULL)
14039 {
14040 /* For unknown compilers expect their behavior is DWARF version
14041 compliant.
14042
14043 GCC started to support .debug_types sections by -gdwarf-4 since
14044 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14045 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14046 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14047 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14048 }
14049 else if (producer_is_gcc (cu->producer, &major, &minor))
14050 {
14051 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14052 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14053 }
14054 else if (producer_is_icc (cu->producer, &major, &minor))
14055 {
14056 cu->producer_is_icc = true;
14057 cu->producer_is_icc_lt_14 = major < 14;
14058 }
14059 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14060 cu->producer_is_codewarrior = true;
14061 else
14062 {
14063 /* For other non-GCC compilers, expect their behavior is DWARF version
14064 compliant. */
14065 }
14066
14067 cu->checked_producer = true;
14068 }
14069
14070 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14071 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14072 during 4.6.0 experimental. */
14073
14074 static bool
14075 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14076 {
14077 if (!cu->checked_producer)
14078 check_producer (cu);
14079
14080 return cu->producer_is_gxx_lt_4_6;
14081 }
14082
14083
14084 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14085 with incorrect is_stmt attributes. */
14086
14087 static bool
14088 producer_is_codewarrior (struct dwarf2_cu *cu)
14089 {
14090 if (!cu->checked_producer)
14091 check_producer (cu);
14092
14093 return cu->producer_is_codewarrior;
14094 }
14095
14096 /* Return the default accessibility type if it is not overridden by
14097 DW_AT_accessibility. */
14098
14099 static enum dwarf_access_attribute
14100 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14101 {
14102 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14103 {
14104 /* The default DWARF 2 accessibility for members is public, the default
14105 accessibility for inheritance is private. */
14106
14107 if (die->tag != DW_TAG_inheritance)
14108 return DW_ACCESS_public;
14109 else
14110 return DW_ACCESS_private;
14111 }
14112 else
14113 {
14114 /* DWARF 3+ defines the default accessibility a different way. The same
14115 rules apply now for DW_TAG_inheritance as for the members and it only
14116 depends on the container kind. */
14117
14118 if (die->parent->tag == DW_TAG_class_type)
14119 return DW_ACCESS_private;
14120 else
14121 return DW_ACCESS_public;
14122 }
14123 }
14124
14125 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14126 offset. If the attribute was not found return 0, otherwise return
14127 1. If it was found but could not properly be handled, set *OFFSET
14128 to 0. */
14129
14130 static int
14131 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14132 LONGEST *offset)
14133 {
14134 struct attribute *attr;
14135
14136 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14137 if (attr != NULL)
14138 {
14139 *offset = 0;
14140
14141 /* Note that we do not check for a section offset first here.
14142 This is because DW_AT_data_member_location is new in DWARF 4,
14143 so if we see it, we can assume that a constant form is really
14144 a constant and not a section offset. */
14145 if (attr->form_is_constant ())
14146 *offset = dwarf2_get_attr_constant_value (attr, 0);
14147 else if (attr->form_is_section_offset ())
14148 dwarf2_complex_location_expr_complaint ();
14149 else if (attr->form_is_block ())
14150 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14151 else
14152 dwarf2_complex_location_expr_complaint ();
14153
14154 return 1;
14155 }
14156
14157 return 0;
14158 }
14159
14160 /* Add an aggregate field to the field list. */
14161
14162 static void
14163 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14164 struct dwarf2_cu *cu)
14165 {
14166 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14168 struct nextfield *new_field;
14169 struct attribute *attr;
14170 struct field *fp;
14171 const char *fieldname = "";
14172
14173 if (die->tag == DW_TAG_inheritance)
14174 {
14175 fip->baseclasses.emplace_back ();
14176 new_field = &fip->baseclasses.back ();
14177 }
14178 else
14179 {
14180 fip->fields.emplace_back ();
14181 new_field = &fip->fields.back ();
14182 }
14183
14184 fip->nfields++;
14185
14186 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14187 if (attr != nullptr)
14188 new_field->accessibility = DW_UNSND (attr);
14189 else
14190 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14191 if (new_field->accessibility != DW_ACCESS_public)
14192 fip->non_public_fields = 1;
14193
14194 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14195 if (attr != nullptr)
14196 new_field->virtuality = DW_UNSND (attr);
14197 else
14198 new_field->virtuality = DW_VIRTUALITY_none;
14199
14200 fp = &new_field->field;
14201
14202 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14203 {
14204 LONGEST offset;
14205
14206 /* Data member other than a C++ static data member. */
14207
14208 /* Get type of field. */
14209 fp->type = die_type (die, cu);
14210
14211 SET_FIELD_BITPOS (*fp, 0);
14212
14213 /* Get bit size of field (zero if none). */
14214 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14215 if (attr != nullptr)
14216 {
14217 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14218 }
14219 else
14220 {
14221 FIELD_BITSIZE (*fp) = 0;
14222 }
14223
14224 /* Get bit offset of field. */
14225 if (handle_data_member_location (die, cu, &offset))
14226 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14227 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14228 if (attr != nullptr)
14229 {
14230 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14231 {
14232 /* For big endian bits, the DW_AT_bit_offset gives the
14233 additional bit offset from the MSB of the containing
14234 anonymous object to the MSB of the field. We don't
14235 have to do anything special since we don't need to
14236 know the size of the anonymous object. */
14237 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14238 }
14239 else
14240 {
14241 /* For little endian bits, compute the bit offset to the
14242 MSB of the anonymous object, subtract off the number of
14243 bits from the MSB of the field to the MSB of the
14244 object, and then subtract off the number of bits of
14245 the field itself. The result is the bit offset of
14246 the LSB of the field. */
14247 int anonymous_size;
14248 int bit_offset = DW_UNSND (attr);
14249
14250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14251 if (attr != nullptr)
14252 {
14253 /* The size of the anonymous object containing
14254 the bit field is explicit, so use the
14255 indicated size (in bytes). */
14256 anonymous_size = DW_UNSND (attr);
14257 }
14258 else
14259 {
14260 /* The size of the anonymous object containing
14261 the bit field must be inferred from the type
14262 attribute of the data member containing the
14263 bit field. */
14264 anonymous_size = TYPE_LENGTH (fp->type);
14265 }
14266 SET_FIELD_BITPOS (*fp,
14267 (FIELD_BITPOS (*fp)
14268 + anonymous_size * bits_per_byte
14269 - bit_offset - FIELD_BITSIZE (*fp)));
14270 }
14271 }
14272 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14273 if (attr != NULL)
14274 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14275 + dwarf2_get_attr_constant_value (attr, 0)));
14276
14277 /* Get name of field. */
14278 fieldname = dwarf2_name (die, cu);
14279 if (fieldname == NULL)
14280 fieldname = "";
14281
14282 /* The name is already allocated along with this objfile, so we don't
14283 need to duplicate it for the type. */
14284 fp->name = fieldname;
14285
14286 /* Change accessibility for artificial fields (e.g. virtual table
14287 pointer or virtual base class pointer) to private. */
14288 if (dwarf2_attr (die, DW_AT_artificial, cu))
14289 {
14290 FIELD_ARTIFICIAL (*fp) = 1;
14291 new_field->accessibility = DW_ACCESS_private;
14292 fip->non_public_fields = 1;
14293 }
14294 }
14295 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14296 {
14297 /* C++ static member. */
14298
14299 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14300 is a declaration, but all versions of G++ as of this writing
14301 (so through at least 3.2.1) incorrectly generate
14302 DW_TAG_variable tags. */
14303
14304 const char *physname;
14305
14306 /* Get name of field. */
14307 fieldname = dwarf2_name (die, cu);
14308 if (fieldname == NULL)
14309 return;
14310
14311 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14312 if (attr
14313 /* Only create a symbol if this is an external value.
14314 new_symbol checks this and puts the value in the global symbol
14315 table, which we want. If it is not external, new_symbol
14316 will try to put the value in cu->list_in_scope which is wrong. */
14317 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14318 {
14319 /* A static const member, not much different than an enum as far as
14320 we're concerned, except that we can support more types. */
14321 new_symbol (die, NULL, cu);
14322 }
14323
14324 /* Get physical name. */
14325 physname = dwarf2_physname (fieldname, die, cu);
14326
14327 /* The name is already allocated along with this objfile, so we don't
14328 need to duplicate it for the type. */
14329 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14330 FIELD_TYPE (*fp) = die_type (die, cu);
14331 FIELD_NAME (*fp) = fieldname;
14332 }
14333 else if (die->tag == DW_TAG_inheritance)
14334 {
14335 LONGEST offset;
14336
14337 /* C++ base class field. */
14338 if (handle_data_member_location (die, cu, &offset))
14339 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14340 FIELD_BITSIZE (*fp) = 0;
14341 FIELD_TYPE (*fp) = die_type (die, cu);
14342 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14343 }
14344 else if (die->tag == DW_TAG_variant_part)
14345 {
14346 /* process_structure_scope will treat this DIE as a union. */
14347 process_structure_scope (die, cu);
14348
14349 /* The variant part is relative to the start of the enclosing
14350 structure. */
14351 SET_FIELD_BITPOS (*fp, 0);
14352 fp->type = get_die_type (die, cu);
14353 fp->artificial = 1;
14354 fp->name = "<<variant>>";
14355
14356 /* Normally a DW_TAG_variant_part won't have a size, but our
14357 representation requires one, so set it to the maximum of the
14358 child sizes, being sure to account for the offset at which
14359 each child is seen. */
14360 if (TYPE_LENGTH (fp->type) == 0)
14361 {
14362 unsigned max = 0;
14363 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14364 {
14365 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14366 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14367 if (len > max)
14368 max = len;
14369 }
14370 TYPE_LENGTH (fp->type) = max;
14371 }
14372 }
14373 else
14374 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14375 }
14376
14377 /* Can the type given by DIE define another type? */
14378
14379 static bool
14380 type_can_define_types (const struct die_info *die)
14381 {
14382 switch (die->tag)
14383 {
14384 case DW_TAG_typedef:
14385 case DW_TAG_class_type:
14386 case DW_TAG_structure_type:
14387 case DW_TAG_union_type:
14388 case DW_TAG_enumeration_type:
14389 return true;
14390
14391 default:
14392 return false;
14393 }
14394 }
14395
14396 /* Add a type definition defined in the scope of the FIP's class. */
14397
14398 static void
14399 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14400 struct dwarf2_cu *cu)
14401 {
14402 struct decl_field fp;
14403 memset (&fp, 0, sizeof (fp));
14404
14405 gdb_assert (type_can_define_types (die));
14406
14407 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14408 fp.name = dwarf2_name (die, cu);
14409 fp.type = read_type_die (die, cu);
14410
14411 /* Save accessibility. */
14412 enum dwarf_access_attribute accessibility;
14413 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14414 if (attr != NULL)
14415 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14416 else
14417 accessibility = dwarf2_default_access_attribute (die, cu);
14418 switch (accessibility)
14419 {
14420 case DW_ACCESS_public:
14421 /* The assumed value if neither private nor protected. */
14422 break;
14423 case DW_ACCESS_private:
14424 fp.is_private = 1;
14425 break;
14426 case DW_ACCESS_protected:
14427 fp.is_protected = 1;
14428 break;
14429 default:
14430 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14431 }
14432
14433 if (die->tag == DW_TAG_typedef)
14434 fip->typedef_field_list.push_back (fp);
14435 else
14436 fip->nested_types_list.push_back (fp);
14437 }
14438
14439 /* Create the vector of fields, and attach it to the type. */
14440
14441 static void
14442 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14443 struct dwarf2_cu *cu)
14444 {
14445 int nfields = fip->nfields;
14446
14447 /* Record the field count, allocate space for the array of fields,
14448 and create blank accessibility bitfields if necessary. */
14449 TYPE_NFIELDS (type) = nfields;
14450 TYPE_FIELDS (type) = (struct field *)
14451 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14452
14453 if (fip->non_public_fields && cu->language != language_ada)
14454 {
14455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14456
14457 TYPE_FIELD_PRIVATE_BITS (type) =
14458 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14459 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14460
14461 TYPE_FIELD_PROTECTED_BITS (type) =
14462 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14463 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14464
14465 TYPE_FIELD_IGNORE_BITS (type) =
14466 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14467 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14468 }
14469
14470 /* If the type has baseclasses, allocate and clear a bit vector for
14471 TYPE_FIELD_VIRTUAL_BITS. */
14472 if (!fip->baseclasses.empty () && cu->language != language_ada)
14473 {
14474 int num_bytes = B_BYTES (fip->baseclasses.size ());
14475 unsigned char *pointer;
14476
14477 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14478 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14479 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14480 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14481 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14482 }
14483
14484 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14485 {
14486 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14487
14488 for (int index = 0; index < nfields; ++index)
14489 {
14490 struct nextfield &field = fip->fields[index];
14491
14492 if (field.variant.is_discriminant)
14493 di->discriminant_index = index;
14494 else if (field.variant.default_branch)
14495 di->default_index = index;
14496 else
14497 di->discriminants[index] = field.variant.discriminant_value;
14498 }
14499 }
14500
14501 /* Copy the saved-up fields into the field vector. */
14502 for (int i = 0; i < nfields; ++i)
14503 {
14504 struct nextfield &field
14505 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14506 : fip->fields[i - fip->baseclasses.size ()]);
14507
14508 TYPE_FIELD (type, i) = field.field;
14509 switch (field.accessibility)
14510 {
14511 case DW_ACCESS_private:
14512 if (cu->language != language_ada)
14513 SET_TYPE_FIELD_PRIVATE (type, i);
14514 break;
14515
14516 case DW_ACCESS_protected:
14517 if (cu->language != language_ada)
14518 SET_TYPE_FIELD_PROTECTED (type, i);
14519 break;
14520
14521 case DW_ACCESS_public:
14522 break;
14523
14524 default:
14525 /* Unknown accessibility. Complain and treat it as public. */
14526 {
14527 complaint (_("unsupported accessibility %d"),
14528 field.accessibility);
14529 }
14530 break;
14531 }
14532 if (i < fip->baseclasses.size ())
14533 {
14534 switch (field.virtuality)
14535 {
14536 case DW_VIRTUALITY_virtual:
14537 case DW_VIRTUALITY_pure_virtual:
14538 if (cu->language == language_ada)
14539 error (_("unexpected virtuality in component of Ada type"));
14540 SET_TYPE_FIELD_VIRTUAL (type, i);
14541 break;
14542 }
14543 }
14544 }
14545 }
14546
14547 /* Return true if this member function is a constructor, false
14548 otherwise. */
14549
14550 static int
14551 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14552 {
14553 const char *fieldname;
14554 const char *type_name;
14555 int len;
14556
14557 if (die->parent == NULL)
14558 return 0;
14559
14560 if (die->parent->tag != DW_TAG_structure_type
14561 && die->parent->tag != DW_TAG_union_type
14562 && die->parent->tag != DW_TAG_class_type)
14563 return 0;
14564
14565 fieldname = dwarf2_name (die, cu);
14566 type_name = dwarf2_name (die->parent, cu);
14567 if (fieldname == NULL || type_name == NULL)
14568 return 0;
14569
14570 len = strlen (fieldname);
14571 return (strncmp (fieldname, type_name, len) == 0
14572 && (type_name[len] == '\0' || type_name[len] == '<'));
14573 }
14574
14575 /* Check if the given VALUE is a recognized enum
14576 dwarf_defaulted_attribute constant according to DWARF5 spec,
14577 Table 7.24. */
14578
14579 static bool
14580 is_valid_DW_AT_defaulted (ULONGEST value)
14581 {
14582 switch (value)
14583 {
14584 case DW_DEFAULTED_no:
14585 case DW_DEFAULTED_in_class:
14586 case DW_DEFAULTED_out_of_class:
14587 return true;
14588 }
14589
14590 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14591 return false;
14592 }
14593
14594 /* Add a member function to the proper fieldlist. */
14595
14596 static void
14597 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14598 struct type *type, struct dwarf2_cu *cu)
14599 {
14600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14601 struct attribute *attr;
14602 int i;
14603 struct fnfieldlist *flp = nullptr;
14604 struct fn_field *fnp;
14605 const char *fieldname;
14606 struct type *this_type;
14607 enum dwarf_access_attribute accessibility;
14608
14609 if (cu->language == language_ada)
14610 error (_("unexpected member function in Ada type"));
14611
14612 /* Get name of member function. */
14613 fieldname = dwarf2_name (die, cu);
14614 if (fieldname == NULL)
14615 return;
14616
14617 /* Look up member function name in fieldlist. */
14618 for (i = 0; i < fip->fnfieldlists.size (); i++)
14619 {
14620 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14621 {
14622 flp = &fip->fnfieldlists[i];
14623 break;
14624 }
14625 }
14626
14627 /* Create a new fnfieldlist if necessary. */
14628 if (flp == nullptr)
14629 {
14630 fip->fnfieldlists.emplace_back ();
14631 flp = &fip->fnfieldlists.back ();
14632 flp->name = fieldname;
14633 i = fip->fnfieldlists.size () - 1;
14634 }
14635
14636 /* Create a new member function field and add it to the vector of
14637 fnfieldlists. */
14638 flp->fnfields.emplace_back ();
14639 fnp = &flp->fnfields.back ();
14640
14641 /* Delay processing of the physname until later. */
14642 if (cu->language == language_cplus)
14643 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14644 die, cu);
14645 else
14646 {
14647 const char *physname = dwarf2_physname (fieldname, die, cu);
14648 fnp->physname = physname ? physname : "";
14649 }
14650
14651 fnp->type = alloc_type (objfile);
14652 this_type = read_type_die (die, cu);
14653 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14654 {
14655 int nparams = TYPE_NFIELDS (this_type);
14656
14657 /* TYPE is the domain of this method, and THIS_TYPE is the type
14658 of the method itself (TYPE_CODE_METHOD). */
14659 smash_to_method_type (fnp->type, type,
14660 TYPE_TARGET_TYPE (this_type),
14661 TYPE_FIELDS (this_type),
14662 TYPE_NFIELDS (this_type),
14663 TYPE_VARARGS (this_type));
14664
14665 /* Handle static member functions.
14666 Dwarf2 has no clean way to discern C++ static and non-static
14667 member functions. G++ helps GDB by marking the first
14668 parameter for non-static member functions (which is the this
14669 pointer) as artificial. We obtain this information from
14670 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14671 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14672 fnp->voffset = VOFFSET_STATIC;
14673 }
14674 else
14675 complaint (_("member function type missing for '%s'"),
14676 dwarf2_full_name (fieldname, die, cu));
14677
14678 /* Get fcontext from DW_AT_containing_type if present. */
14679 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14680 fnp->fcontext = die_containing_type (die, cu);
14681
14682 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14683 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14684
14685 /* Get accessibility. */
14686 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14687 if (attr != nullptr)
14688 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14689 else
14690 accessibility = dwarf2_default_access_attribute (die, cu);
14691 switch (accessibility)
14692 {
14693 case DW_ACCESS_private:
14694 fnp->is_private = 1;
14695 break;
14696 case DW_ACCESS_protected:
14697 fnp->is_protected = 1;
14698 break;
14699 }
14700
14701 /* Check for artificial methods. */
14702 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14703 if (attr && DW_UNSND (attr) != 0)
14704 fnp->is_artificial = 1;
14705
14706 /* Check for defaulted methods. */
14707 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14708 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14709 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14710
14711 /* Check for deleted methods. */
14712 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14713 if (attr != nullptr && DW_UNSND (attr) != 0)
14714 fnp->is_deleted = 1;
14715
14716 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14717
14718 /* Get index in virtual function table if it is a virtual member
14719 function. For older versions of GCC, this is an offset in the
14720 appropriate virtual table, as specified by DW_AT_containing_type.
14721 For everyone else, it is an expression to be evaluated relative
14722 to the object address. */
14723
14724 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14725 if (attr != nullptr)
14726 {
14727 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14728 {
14729 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14730 {
14731 /* Old-style GCC. */
14732 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14733 }
14734 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14735 || (DW_BLOCK (attr)->size > 1
14736 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14737 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14738 {
14739 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14740 if ((fnp->voffset % cu->header.addr_size) != 0)
14741 dwarf2_complex_location_expr_complaint ();
14742 else
14743 fnp->voffset /= cu->header.addr_size;
14744 fnp->voffset += 2;
14745 }
14746 else
14747 dwarf2_complex_location_expr_complaint ();
14748
14749 if (!fnp->fcontext)
14750 {
14751 /* If there is no `this' field and no DW_AT_containing_type,
14752 we cannot actually find a base class context for the
14753 vtable! */
14754 if (TYPE_NFIELDS (this_type) == 0
14755 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14756 {
14757 complaint (_("cannot determine context for virtual member "
14758 "function \"%s\" (offset %s)"),
14759 fieldname, sect_offset_str (die->sect_off));
14760 }
14761 else
14762 {
14763 fnp->fcontext
14764 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14765 }
14766 }
14767 }
14768 else if (attr->form_is_section_offset ())
14769 {
14770 dwarf2_complex_location_expr_complaint ();
14771 }
14772 else
14773 {
14774 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14775 fieldname);
14776 }
14777 }
14778 else
14779 {
14780 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14781 if (attr && DW_UNSND (attr))
14782 {
14783 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14784 complaint (_("Member function \"%s\" (offset %s) is virtual "
14785 "but the vtable offset is not specified"),
14786 fieldname, sect_offset_str (die->sect_off));
14787 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14788 TYPE_CPLUS_DYNAMIC (type) = 1;
14789 }
14790 }
14791 }
14792
14793 /* Create the vector of member function fields, and attach it to the type. */
14794
14795 static void
14796 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14797 struct dwarf2_cu *cu)
14798 {
14799 if (cu->language == language_ada)
14800 error (_("unexpected member functions in Ada type"));
14801
14802 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14803 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14804 TYPE_ALLOC (type,
14805 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14806
14807 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14808 {
14809 struct fnfieldlist &nf = fip->fnfieldlists[i];
14810 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14811
14812 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14813 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14814 fn_flp->fn_fields = (struct fn_field *)
14815 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14816
14817 for (int k = 0; k < nf.fnfields.size (); ++k)
14818 fn_flp->fn_fields[k] = nf.fnfields[k];
14819 }
14820
14821 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14822 }
14823
14824 /* Returns non-zero if NAME is the name of a vtable member in CU's
14825 language, zero otherwise. */
14826 static int
14827 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14828 {
14829 static const char vptr[] = "_vptr";
14830
14831 /* Look for the C++ form of the vtable. */
14832 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14833 return 1;
14834
14835 return 0;
14836 }
14837
14838 /* GCC outputs unnamed structures that are really pointers to member
14839 functions, with the ABI-specified layout. If TYPE describes
14840 such a structure, smash it into a member function type.
14841
14842 GCC shouldn't do this; it should just output pointer to member DIEs.
14843 This is GCC PR debug/28767. */
14844
14845 static void
14846 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14847 {
14848 struct type *pfn_type, *self_type, *new_type;
14849
14850 /* Check for a structure with no name and two children. */
14851 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14852 return;
14853
14854 /* Check for __pfn and __delta members. */
14855 if (TYPE_FIELD_NAME (type, 0) == NULL
14856 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14857 || TYPE_FIELD_NAME (type, 1) == NULL
14858 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14859 return;
14860
14861 /* Find the type of the method. */
14862 pfn_type = TYPE_FIELD_TYPE (type, 0);
14863 if (pfn_type == NULL
14864 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14865 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14866 return;
14867
14868 /* Look for the "this" argument. */
14869 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14870 if (TYPE_NFIELDS (pfn_type) == 0
14871 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14872 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14873 return;
14874
14875 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14876 new_type = alloc_type (objfile);
14877 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14878 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14879 TYPE_VARARGS (pfn_type));
14880 smash_to_methodptr_type (type, new_type);
14881 }
14882
14883 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14884 appropriate error checking and issuing complaints if there is a
14885 problem. */
14886
14887 static ULONGEST
14888 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14889 {
14890 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14891
14892 if (attr == nullptr)
14893 return 0;
14894
14895 if (!attr->form_is_constant ())
14896 {
14897 complaint (_("DW_AT_alignment must have constant form"
14898 " - DIE at %s [in module %s]"),
14899 sect_offset_str (die->sect_off),
14900 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14901 return 0;
14902 }
14903
14904 ULONGEST align;
14905 if (attr->form == DW_FORM_sdata)
14906 {
14907 LONGEST val = DW_SND (attr);
14908 if (val < 0)
14909 {
14910 complaint (_("DW_AT_alignment value must not be negative"
14911 " - DIE at %s [in module %s]"),
14912 sect_offset_str (die->sect_off),
14913 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14914 return 0;
14915 }
14916 align = val;
14917 }
14918 else
14919 align = DW_UNSND (attr);
14920
14921 if (align == 0)
14922 {
14923 complaint (_("DW_AT_alignment value must not be zero"
14924 " - DIE at %s [in module %s]"),
14925 sect_offset_str (die->sect_off),
14926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14927 return 0;
14928 }
14929 if ((align & (align - 1)) != 0)
14930 {
14931 complaint (_("DW_AT_alignment value must be a power of 2"
14932 " - DIE at %s [in module %s]"),
14933 sect_offset_str (die->sect_off),
14934 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14935 return 0;
14936 }
14937
14938 return align;
14939 }
14940
14941 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14942 the alignment for TYPE. */
14943
14944 static void
14945 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14946 struct type *type)
14947 {
14948 if (!set_type_align (type, get_alignment (cu, die)))
14949 complaint (_("DW_AT_alignment value too large"
14950 " - DIE at %s [in module %s]"),
14951 sect_offset_str (die->sect_off),
14952 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14953 }
14954
14955 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14956 constant for a type, according to DWARF5 spec, Table 5.5. */
14957
14958 static bool
14959 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14960 {
14961 switch (value)
14962 {
14963 case DW_CC_normal:
14964 case DW_CC_pass_by_reference:
14965 case DW_CC_pass_by_value:
14966 return true;
14967
14968 default:
14969 complaint (_("unrecognized DW_AT_calling_convention value "
14970 "(%s) for a type"), pulongest (value));
14971 return false;
14972 }
14973 }
14974
14975 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14976 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14977 also according to GNU-specific values (see include/dwarf2.h). */
14978
14979 static bool
14980 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14981 {
14982 switch (value)
14983 {
14984 case DW_CC_normal:
14985 case DW_CC_program:
14986 case DW_CC_nocall:
14987 return true;
14988
14989 case DW_CC_GNU_renesas_sh:
14990 case DW_CC_GNU_borland_fastcall_i386:
14991 case DW_CC_GDB_IBM_OpenCL:
14992 return true;
14993
14994 default:
14995 complaint (_("unrecognized DW_AT_calling_convention value "
14996 "(%s) for a subroutine"), pulongest (value));
14997 return false;
14998 }
14999 }
15000
15001 /* Called when we find the DIE that starts a structure or union scope
15002 (definition) to create a type for the structure or union. Fill in
15003 the type's name and general properties; the members will not be
15004 processed until process_structure_scope. A symbol table entry for
15005 the type will also not be done until process_structure_scope (assuming
15006 the type has a name).
15007
15008 NOTE: we need to call these functions regardless of whether or not the
15009 DIE has a DW_AT_name attribute, since it might be an anonymous
15010 structure or union. This gets the type entered into our set of
15011 user defined types. */
15012
15013 static struct type *
15014 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15015 {
15016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15017 struct type *type;
15018 struct attribute *attr;
15019 const char *name;
15020
15021 /* If the definition of this type lives in .debug_types, read that type.
15022 Don't follow DW_AT_specification though, that will take us back up
15023 the chain and we want to go down. */
15024 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15025 if (attr != nullptr)
15026 {
15027 type = get_DW_AT_signature_type (die, attr, cu);
15028
15029 /* The type's CU may not be the same as CU.
15030 Ensure TYPE is recorded with CU in die_type_hash. */
15031 return set_die_type (die, type, cu);
15032 }
15033
15034 type = alloc_type (objfile);
15035 INIT_CPLUS_SPECIFIC (type);
15036
15037 name = dwarf2_name (die, cu);
15038 if (name != NULL)
15039 {
15040 if (cu->language == language_cplus
15041 || cu->language == language_d
15042 || cu->language == language_rust)
15043 {
15044 const char *full_name = dwarf2_full_name (name, die, cu);
15045
15046 /* dwarf2_full_name might have already finished building the DIE's
15047 type. If so, there is no need to continue. */
15048 if (get_die_type (die, cu) != NULL)
15049 return get_die_type (die, cu);
15050
15051 TYPE_NAME (type) = full_name;
15052 }
15053 else
15054 {
15055 /* The name is already allocated along with this objfile, so
15056 we don't need to duplicate it for the type. */
15057 TYPE_NAME (type) = name;
15058 }
15059 }
15060
15061 if (die->tag == DW_TAG_structure_type)
15062 {
15063 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15064 }
15065 else if (die->tag == DW_TAG_union_type)
15066 {
15067 TYPE_CODE (type) = TYPE_CODE_UNION;
15068 }
15069 else if (die->tag == DW_TAG_variant_part)
15070 {
15071 TYPE_CODE (type) = TYPE_CODE_UNION;
15072 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15073 }
15074 else
15075 {
15076 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15077 }
15078
15079 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15080 TYPE_DECLARED_CLASS (type) = 1;
15081
15082 /* Store the calling convention in the type if it's available in
15083 the die. Otherwise the calling convention remains set to
15084 the default value DW_CC_normal. */
15085 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15086 if (attr != nullptr
15087 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15088 {
15089 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15090 TYPE_CPLUS_CALLING_CONVENTION (type)
15091 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15092 }
15093
15094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15095 if (attr != nullptr)
15096 {
15097 if (attr->form_is_constant ())
15098 TYPE_LENGTH (type) = DW_UNSND (attr);
15099 else
15100 {
15101 /* For the moment, dynamic type sizes are not supported
15102 by GDB's struct type. The actual size is determined
15103 on-demand when resolving the type of a given object,
15104 so set the type's length to zero for now. Otherwise,
15105 we record an expression as the length, and that expression
15106 could lead to a very large value, which could eventually
15107 lead to us trying to allocate that much memory when creating
15108 a value of that type. */
15109 TYPE_LENGTH (type) = 0;
15110 }
15111 }
15112 else
15113 {
15114 TYPE_LENGTH (type) = 0;
15115 }
15116
15117 maybe_set_alignment (cu, die, type);
15118
15119 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15120 {
15121 /* ICC<14 does not output the required DW_AT_declaration on
15122 incomplete types, but gives them a size of zero. */
15123 TYPE_STUB (type) = 1;
15124 }
15125 else
15126 TYPE_STUB_SUPPORTED (type) = 1;
15127
15128 if (die_is_declaration (die, cu))
15129 TYPE_STUB (type) = 1;
15130 else if (attr == NULL && die->child == NULL
15131 && producer_is_realview (cu->producer))
15132 /* RealView does not output the required DW_AT_declaration
15133 on incomplete types. */
15134 TYPE_STUB (type) = 1;
15135
15136 /* We need to add the type field to the die immediately so we don't
15137 infinitely recurse when dealing with pointers to the structure
15138 type within the structure itself. */
15139 set_die_type (die, type, cu);
15140
15141 /* set_die_type should be already done. */
15142 set_descriptive_type (type, die, cu);
15143
15144 return type;
15145 }
15146
15147 /* A helper for process_structure_scope that handles a single member
15148 DIE. */
15149
15150 static void
15151 handle_struct_member_die (struct die_info *child_die, struct type *type,
15152 struct field_info *fi,
15153 std::vector<struct symbol *> *template_args,
15154 struct dwarf2_cu *cu)
15155 {
15156 if (child_die->tag == DW_TAG_member
15157 || child_die->tag == DW_TAG_variable
15158 || child_die->tag == DW_TAG_variant_part)
15159 {
15160 /* NOTE: carlton/2002-11-05: A C++ static data member
15161 should be a DW_TAG_member that is a declaration, but
15162 all versions of G++ as of this writing (so through at
15163 least 3.2.1) incorrectly generate DW_TAG_variable
15164 tags for them instead. */
15165 dwarf2_add_field (fi, child_die, cu);
15166 }
15167 else if (child_die->tag == DW_TAG_subprogram)
15168 {
15169 /* Rust doesn't have member functions in the C++ sense.
15170 However, it does emit ordinary functions as children
15171 of a struct DIE. */
15172 if (cu->language == language_rust)
15173 read_func_scope (child_die, cu);
15174 else
15175 {
15176 /* C++ member function. */
15177 dwarf2_add_member_fn (fi, child_die, type, cu);
15178 }
15179 }
15180 else if (child_die->tag == DW_TAG_inheritance)
15181 {
15182 /* C++ base class field. */
15183 dwarf2_add_field (fi, child_die, cu);
15184 }
15185 else if (type_can_define_types (child_die))
15186 dwarf2_add_type_defn (fi, child_die, cu);
15187 else if (child_die->tag == DW_TAG_template_type_param
15188 || child_die->tag == DW_TAG_template_value_param)
15189 {
15190 struct symbol *arg = new_symbol (child_die, NULL, cu);
15191
15192 if (arg != NULL)
15193 template_args->push_back (arg);
15194 }
15195 else if (child_die->tag == DW_TAG_variant)
15196 {
15197 /* In a variant we want to get the discriminant and also add a
15198 field for our sole member child. */
15199 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15200
15201 for (die_info *variant_child = child_die->child;
15202 variant_child != NULL;
15203 variant_child = sibling_die (variant_child))
15204 {
15205 if (variant_child->tag == DW_TAG_member)
15206 {
15207 handle_struct_member_die (variant_child, type, fi,
15208 template_args, cu);
15209 /* Only handle the one. */
15210 break;
15211 }
15212 }
15213
15214 /* We don't handle this but we might as well report it if we see
15215 it. */
15216 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15217 complaint (_("DW_AT_discr_list is not supported yet"
15218 " - DIE at %s [in module %s]"),
15219 sect_offset_str (child_die->sect_off),
15220 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15221
15222 /* The first field was just added, so we can stash the
15223 discriminant there. */
15224 gdb_assert (!fi->fields.empty ());
15225 if (discr == NULL)
15226 fi->fields.back ().variant.default_branch = true;
15227 else
15228 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15229 }
15230 }
15231
15232 /* Finish creating a structure or union type, including filling in
15233 its members and creating a symbol for it. */
15234
15235 static void
15236 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15237 {
15238 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15239 struct die_info *child_die;
15240 struct type *type;
15241
15242 type = get_die_type (die, cu);
15243 if (type == NULL)
15244 type = read_structure_type (die, cu);
15245
15246 /* When reading a DW_TAG_variant_part, we need to notice when we
15247 read the discriminant member, so we can record it later in the
15248 discriminant_info. */
15249 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15250 sect_offset discr_offset {};
15251 bool has_template_parameters = false;
15252
15253 if (is_variant_part)
15254 {
15255 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15256 if (discr == NULL)
15257 {
15258 /* Maybe it's a univariant form, an extension we support.
15259 In this case arrange not to check the offset. */
15260 is_variant_part = false;
15261 }
15262 else if (discr->form_is_ref ())
15263 {
15264 struct dwarf2_cu *target_cu = cu;
15265 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15266
15267 discr_offset = target_die->sect_off;
15268 }
15269 else
15270 {
15271 complaint (_("DW_AT_discr does not have DIE reference form"
15272 " - DIE at %s [in module %s]"),
15273 sect_offset_str (die->sect_off),
15274 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15275 is_variant_part = false;
15276 }
15277 }
15278
15279 if (die->child != NULL && ! die_is_declaration (die, cu))
15280 {
15281 struct field_info fi;
15282 std::vector<struct symbol *> template_args;
15283
15284 child_die = die->child;
15285
15286 while (child_die && child_die->tag)
15287 {
15288 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15289
15290 if (is_variant_part && discr_offset == child_die->sect_off)
15291 fi.fields.back ().variant.is_discriminant = true;
15292
15293 child_die = sibling_die (child_die);
15294 }
15295
15296 /* Attach template arguments to type. */
15297 if (!template_args.empty ())
15298 {
15299 has_template_parameters = true;
15300 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15301 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15302 TYPE_TEMPLATE_ARGUMENTS (type)
15303 = XOBNEWVEC (&objfile->objfile_obstack,
15304 struct symbol *,
15305 TYPE_N_TEMPLATE_ARGUMENTS (type));
15306 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15307 template_args.data (),
15308 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15309 * sizeof (struct symbol *)));
15310 }
15311
15312 /* Attach fields and member functions to the type. */
15313 if (fi.nfields)
15314 dwarf2_attach_fields_to_type (&fi, type, cu);
15315 if (!fi.fnfieldlists.empty ())
15316 {
15317 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15318
15319 /* Get the type which refers to the base class (possibly this
15320 class itself) which contains the vtable pointer for the current
15321 class from the DW_AT_containing_type attribute. This use of
15322 DW_AT_containing_type is a GNU extension. */
15323
15324 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15325 {
15326 struct type *t = die_containing_type (die, cu);
15327
15328 set_type_vptr_basetype (type, t);
15329 if (type == t)
15330 {
15331 int i;
15332
15333 /* Our own class provides vtbl ptr. */
15334 for (i = TYPE_NFIELDS (t) - 1;
15335 i >= TYPE_N_BASECLASSES (t);
15336 --i)
15337 {
15338 const char *fieldname = TYPE_FIELD_NAME (t, i);
15339
15340 if (is_vtable_name (fieldname, cu))
15341 {
15342 set_type_vptr_fieldno (type, i);
15343 break;
15344 }
15345 }
15346
15347 /* Complain if virtual function table field not found. */
15348 if (i < TYPE_N_BASECLASSES (t))
15349 complaint (_("virtual function table pointer "
15350 "not found when defining class '%s'"),
15351 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15352 }
15353 else
15354 {
15355 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15356 }
15357 }
15358 else if (cu->producer
15359 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15360 {
15361 /* The IBM XLC compiler does not provide direct indication
15362 of the containing type, but the vtable pointer is
15363 always named __vfp. */
15364
15365 int i;
15366
15367 for (i = TYPE_NFIELDS (type) - 1;
15368 i >= TYPE_N_BASECLASSES (type);
15369 --i)
15370 {
15371 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15372 {
15373 set_type_vptr_fieldno (type, i);
15374 set_type_vptr_basetype (type, type);
15375 break;
15376 }
15377 }
15378 }
15379 }
15380
15381 /* Copy fi.typedef_field_list linked list elements content into the
15382 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15383 if (!fi.typedef_field_list.empty ())
15384 {
15385 int count = fi.typedef_field_list.size ();
15386
15387 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15388 TYPE_TYPEDEF_FIELD_ARRAY (type)
15389 = ((struct decl_field *)
15390 TYPE_ALLOC (type,
15391 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15392 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15393
15394 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15395 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15396 }
15397
15398 /* Copy fi.nested_types_list linked list elements content into the
15399 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15400 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15401 {
15402 int count = fi.nested_types_list.size ();
15403
15404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15405 TYPE_NESTED_TYPES_ARRAY (type)
15406 = ((struct decl_field *)
15407 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15408 TYPE_NESTED_TYPES_COUNT (type) = count;
15409
15410 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15411 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15412 }
15413 }
15414
15415 quirk_gcc_member_function_pointer (type, objfile);
15416 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15417 cu->rust_unions.push_back (type);
15418
15419 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15420 snapshots) has been known to create a die giving a declaration
15421 for a class that has, as a child, a die giving a definition for a
15422 nested class. So we have to process our children even if the
15423 current die is a declaration. Normally, of course, a declaration
15424 won't have any children at all. */
15425
15426 child_die = die->child;
15427
15428 while (child_die != NULL && child_die->tag)
15429 {
15430 if (child_die->tag == DW_TAG_member
15431 || child_die->tag == DW_TAG_variable
15432 || child_die->tag == DW_TAG_inheritance
15433 || child_die->tag == DW_TAG_template_value_param
15434 || child_die->tag == DW_TAG_template_type_param)
15435 {
15436 /* Do nothing. */
15437 }
15438 else
15439 process_die (child_die, cu);
15440
15441 child_die = sibling_die (child_die);
15442 }
15443
15444 /* Do not consider external references. According to the DWARF standard,
15445 these DIEs are identified by the fact that they have no byte_size
15446 attribute, and a declaration attribute. */
15447 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15448 || !die_is_declaration (die, cu))
15449 {
15450 struct symbol *sym = new_symbol (die, type, cu);
15451
15452 if (has_template_parameters)
15453 {
15454 struct symtab *symtab;
15455 if (sym != nullptr)
15456 symtab = symbol_symtab (sym);
15457 else if (cu->line_header != nullptr)
15458 {
15459 /* Any related symtab will do. */
15460 symtab
15461 = cu->line_header->file_names ()[0].symtab;
15462 }
15463 else
15464 {
15465 symtab = nullptr;
15466 complaint (_("could not find suitable "
15467 "symtab for template parameter"
15468 " - DIE at %s [in module %s]"),
15469 sect_offset_str (die->sect_off),
15470 objfile_name (objfile));
15471 }
15472
15473 if (symtab != nullptr)
15474 {
15475 /* Make sure that the symtab is set on the new symbols.
15476 Even though they don't appear in this symtab directly,
15477 other parts of gdb assume that symbols do, and this is
15478 reasonably true. */
15479 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15480 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15481 }
15482 }
15483 }
15484 }
15485
15486 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15487 update TYPE using some information only available in DIE's children. */
15488
15489 static void
15490 update_enumeration_type_from_children (struct die_info *die,
15491 struct type *type,
15492 struct dwarf2_cu *cu)
15493 {
15494 struct die_info *child_die;
15495 int unsigned_enum = 1;
15496 int flag_enum = 1;
15497 ULONGEST mask = 0;
15498
15499 auto_obstack obstack;
15500
15501 for (child_die = die->child;
15502 child_die != NULL && child_die->tag;
15503 child_die = sibling_die (child_die))
15504 {
15505 struct attribute *attr;
15506 LONGEST value;
15507 const gdb_byte *bytes;
15508 struct dwarf2_locexpr_baton *baton;
15509 const char *name;
15510
15511 if (child_die->tag != DW_TAG_enumerator)
15512 continue;
15513
15514 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15515 if (attr == NULL)
15516 continue;
15517
15518 name = dwarf2_name (child_die, cu);
15519 if (name == NULL)
15520 name = "<anonymous enumerator>";
15521
15522 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15523 &value, &bytes, &baton);
15524 if (value < 0)
15525 {
15526 unsigned_enum = 0;
15527 flag_enum = 0;
15528 }
15529 else if ((mask & value) != 0)
15530 flag_enum = 0;
15531 else
15532 mask |= value;
15533
15534 /* If we already know that the enum type is neither unsigned, nor
15535 a flag type, no need to look at the rest of the enumerates. */
15536 if (!unsigned_enum && !flag_enum)
15537 break;
15538 }
15539
15540 if (unsigned_enum)
15541 TYPE_UNSIGNED (type) = 1;
15542 if (flag_enum)
15543 TYPE_FLAG_ENUM (type) = 1;
15544 }
15545
15546 /* Given a DW_AT_enumeration_type die, set its type. We do not
15547 complete the type's fields yet, or create any symbols. */
15548
15549 static struct type *
15550 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15551 {
15552 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15553 struct type *type;
15554 struct attribute *attr;
15555 const char *name;
15556
15557 /* If the definition of this type lives in .debug_types, read that type.
15558 Don't follow DW_AT_specification though, that will take us back up
15559 the chain and we want to go down. */
15560 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15561 if (attr != nullptr)
15562 {
15563 type = get_DW_AT_signature_type (die, attr, cu);
15564
15565 /* The type's CU may not be the same as CU.
15566 Ensure TYPE is recorded with CU in die_type_hash. */
15567 return set_die_type (die, type, cu);
15568 }
15569
15570 type = alloc_type (objfile);
15571
15572 TYPE_CODE (type) = TYPE_CODE_ENUM;
15573 name = dwarf2_full_name (NULL, die, cu);
15574 if (name != NULL)
15575 TYPE_NAME (type) = name;
15576
15577 attr = dwarf2_attr (die, DW_AT_type, cu);
15578 if (attr != NULL)
15579 {
15580 struct type *underlying_type = die_type (die, cu);
15581
15582 TYPE_TARGET_TYPE (type) = underlying_type;
15583 }
15584
15585 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15586 if (attr != nullptr)
15587 {
15588 TYPE_LENGTH (type) = DW_UNSND (attr);
15589 }
15590 else
15591 {
15592 TYPE_LENGTH (type) = 0;
15593 }
15594
15595 maybe_set_alignment (cu, die, type);
15596
15597 /* The enumeration DIE can be incomplete. In Ada, any type can be
15598 declared as private in the package spec, and then defined only
15599 inside the package body. Such types are known as Taft Amendment
15600 Types. When another package uses such a type, an incomplete DIE
15601 may be generated by the compiler. */
15602 if (die_is_declaration (die, cu))
15603 TYPE_STUB (type) = 1;
15604
15605 /* Finish the creation of this type by using the enum's children.
15606 We must call this even when the underlying type has been provided
15607 so that we can determine if we're looking at a "flag" enum. */
15608 update_enumeration_type_from_children (die, type, cu);
15609
15610 /* If this type has an underlying type that is not a stub, then we
15611 may use its attributes. We always use the "unsigned" attribute
15612 in this situation, because ordinarily we guess whether the type
15613 is unsigned -- but the guess can be wrong and the underlying type
15614 can tell us the reality. However, we defer to a local size
15615 attribute if one exists, because this lets the compiler override
15616 the underlying type if needed. */
15617 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15618 {
15619 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15620 if (TYPE_LENGTH (type) == 0)
15621 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15622 if (TYPE_RAW_ALIGN (type) == 0
15623 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15624 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15625 }
15626
15627 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15628
15629 return set_die_type (die, type, cu);
15630 }
15631
15632 /* Given a pointer to a die which begins an enumeration, process all
15633 the dies that define the members of the enumeration, and create the
15634 symbol for the enumeration type.
15635
15636 NOTE: We reverse the order of the element list. */
15637
15638 static void
15639 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15640 {
15641 struct type *this_type;
15642
15643 this_type = get_die_type (die, cu);
15644 if (this_type == NULL)
15645 this_type = read_enumeration_type (die, cu);
15646
15647 if (die->child != NULL)
15648 {
15649 struct die_info *child_die;
15650 struct symbol *sym;
15651 std::vector<struct field> fields;
15652 const char *name;
15653
15654 child_die = die->child;
15655 while (child_die && child_die->tag)
15656 {
15657 if (child_die->tag != DW_TAG_enumerator)
15658 {
15659 process_die (child_die, cu);
15660 }
15661 else
15662 {
15663 name = dwarf2_name (child_die, cu);
15664 if (name)
15665 {
15666 sym = new_symbol (child_die, this_type, cu);
15667
15668 fields.emplace_back ();
15669 struct field &field = fields.back ();
15670
15671 FIELD_NAME (field) = sym->linkage_name ();
15672 FIELD_TYPE (field) = NULL;
15673 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15674 FIELD_BITSIZE (field) = 0;
15675 }
15676 }
15677
15678 child_die = sibling_die (child_die);
15679 }
15680
15681 if (!fields.empty ())
15682 {
15683 TYPE_NFIELDS (this_type) = fields.size ();
15684 TYPE_FIELDS (this_type) = (struct field *)
15685 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15686 memcpy (TYPE_FIELDS (this_type), fields.data (),
15687 sizeof (struct field) * fields.size ());
15688 }
15689 }
15690
15691 /* If we are reading an enum from a .debug_types unit, and the enum
15692 is a declaration, and the enum is not the signatured type in the
15693 unit, then we do not want to add a symbol for it. Adding a
15694 symbol would in some cases obscure the true definition of the
15695 enum, giving users an incomplete type when the definition is
15696 actually available. Note that we do not want to do this for all
15697 enums which are just declarations, because C++0x allows forward
15698 enum declarations. */
15699 if (cu->per_cu->is_debug_types
15700 && die_is_declaration (die, cu))
15701 {
15702 struct signatured_type *sig_type;
15703
15704 sig_type = (struct signatured_type *) cu->per_cu;
15705 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15706 if (sig_type->type_offset_in_section != die->sect_off)
15707 return;
15708 }
15709
15710 new_symbol (die, this_type, cu);
15711 }
15712
15713 /* Extract all information from a DW_TAG_array_type DIE and put it in
15714 the DIE's type field. For now, this only handles one dimensional
15715 arrays. */
15716
15717 static struct type *
15718 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15719 {
15720 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15721 struct die_info *child_die;
15722 struct type *type;
15723 struct type *element_type, *range_type, *index_type;
15724 struct attribute *attr;
15725 const char *name;
15726 struct dynamic_prop *byte_stride_prop = NULL;
15727 unsigned int bit_stride = 0;
15728
15729 element_type = die_type (die, cu);
15730
15731 /* The die_type call above may have already set the type for this DIE. */
15732 type = get_die_type (die, cu);
15733 if (type)
15734 return type;
15735
15736 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15737 if (attr != NULL)
15738 {
15739 int stride_ok;
15740 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15741
15742 byte_stride_prop
15743 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15744 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15745 prop_type);
15746 if (!stride_ok)
15747 {
15748 complaint (_("unable to read array DW_AT_byte_stride "
15749 " - DIE at %s [in module %s]"),
15750 sect_offset_str (die->sect_off),
15751 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15752 /* Ignore this attribute. We will likely not be able to print
15753 arrays of this type correctly, but there is little we can do
15754 to help if we cannot read the attribute's value. */
15755 byte_stride_prop = NULL;
15756 }
15757 }
15758
15759 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15760 if (attr != NULL)
15761 bit_stride = DW_UNSND (attr);
15762
15763 /* Irix 6.2 native cc creates array types without children for
15764 arrays with unspecified length. */
15765 if (die->child == NULL)
15766 {
15767 index_type = objfile_type (objfile)->builtin_int;
15768 range_type = create_static_range_type (NULL, index_type, 0, -1);
15769 type = create_array_type_with_stride (NULL, element_type, range_type,
15770 byte_stride_prop, bit_stride);
15771 return set_die_type (die, type, cu);
15772 }
15773
15774 std::vector<struct type *> range_types;
15775 child_die = die->child;
15776 while (child_die && child_die->tag)
15777 {
15778 if (child_die->tag == DW_TAG_subrange_type)
15779 {
15780 struct type *child_type = read_type_die (child_die, cu);
15781
15782 if (child_type != NULL)
15783 {
15784 /* The range type was succesfully read. Save it for the
15785 array type creation. */
15786 range_types.push_back (child_type);
15787 }
15788 }
15789 child_die = sibling_die (child_die);
15790 }
15791
15792 /* Dwarf2 dimensions are output from left to right, create the
15793 necessary array types in backwards order. */
15794
15795 type = element_type;
15796
15797 if (read_array_order (die, cu) == DW_ORD_col_major)
15798 {
15799 int i = 0;
15800
15801 while (i < range_types.size ())
15802 type = create_array_type_with_stride (NULL, type, range_types[i++],
15803 byte_stride_prop, bit_stride);
15804 }
15805 else
15806 {
15807 size_t ndim = range_types.size ();
15808 while (ndim-- > 0)
15809 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15810 byte_stride_prop, bit_stride);
15811 }
15812
15813 /* Understand Dwarf2 support for vector types (like they occur on
15814 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15815 array type. This is not part of the Dwarf2/3 standard yet, but a
15816 custom vendor extension. The main difference between a regular
15817 array and the vector variant is that vectors are passed by value
15818 to functions. */
15819 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15820 if (attr != nullptr)
15821 make_vector_type (type);
15822
15823 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15824 implementation may choose to implement triple vectors using this
15825 attribute. */
15826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15827 if (attr != nullptr)
15828 {
15829 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15830 TYPE_LENGTH (type) = DW_UNSND (attr);
15831 else
15832 complaint (_("DW_AT_byte_size for array type smaller "
15833 "than the total size of elements"));
15834 }
15835
15836 name = dwarf2_name (die, cu);
15837 if (name)
15838 TYPE_NAME (type) = name;
15839
15840 maybe_set_alignment (cu, die, type);
15841
15842 /* Install the type in the die. */
15843 set_die_type (die, type, cu);
15844
15845 /* set_die_type should be already done. */
15846 set_descriptive_type (type, die, cu);
15847
15848 return type;
15849 }
15850
15851 static enum dwarf_array_dim_ordering
15852 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15853 {
15854 struct attribute *attr;
15855
15856 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15857
15858 if (attr != nullptr)
15859 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15860
15861 /* GNU F77 is a special case, as at 08/2004 array type info is the
15862 opposite order to the dwarf2 specification, but data is still
15863 laid out as per normal fortran.
15864
15865 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15866 version checking. */
15867
15868 if (cu->language == language_fortran
15869 && cu->producer && strstr (cu->producer, "GNU F77"))
15870 {
15871 return DW_ORD_row_major;
15872 }
15873
15874 switch (cu->language_defn->la_array_ordering)
15875 {
15876 case array_column_major:
15877 return DW_ORD_col_major;
15878 case array_row_major:
15879 default:
15880 return DW_ORD_row_major;
15881 };
15882 }
15883
15884 /* Extract all information from a DW_TAG_set_type DIE and put it in
15885 the DIE's type field. */
15886
15887 static struct type *
15888 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15889 {
15890 struct type *domain_type, *set_type;
15891 struct attribute *attr;
15892
15893 domain_type = die_type (die, cu);
15894
15895 /* The die_type call above may have already set the type for this DIE. */
15896 set_type = get_die_type (die, cu);
15897 if (set_type)
15898 return set_type;
15899
15900 set_type = create_set_type (NULL, domain_type);
15901
15902 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15903 if (attr != nullptr)
15904 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15905
15906 maybe_set_alignment (cu, die, set_type);
15907
15908 return set_die_type (die, set_type, cu);
15909 }
15910
15911 /* A helper for read_common_block that creates a locexpr baton.
15912 SYM is the symbol which we are marking as computed.
15913 COMMON_DIE is the DIE for the common block.
15914 COMMON_LOC is the location expression attribute for the common
15915 block itself.
15916 MEMBER_LOC is the location expression attribute for the particular
15917 member of the common block that we are processing.
15918 CU is the CU from which the above come. */
15919
15920 static void
15921 mark_common_block_symbol_computed (struct symbol *sym,
15922 struct die_info *common_die,
15923 struct attribute *common_loc,
15924 struct attribute *member_loc,
15925 struct dwarf2_cu *cu)
15926 {
15927 struct dwarf2_per_objfile *dwarf2_per_objfile
15928 = cu->per_cu->dwarf2_per_objfile;
15929 struct objfile *objfile = dwarf2_per_objfile->objfile;
15930 struct dwarf2_locexpr_baton *baton;
15931 gdb_byte *ptr;
15932 unsigned int cu_off;
15933 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15934 LONGEST offset = 0;
15935
15936 gdb_assert (common_loc && member_loc);
15937 gdb_assert (common_loc->form_is_block ());
15938 gdb_assert (member_loc->form_is_block ()
15939 || member_loc->form_is_constant ());
15940
15941 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15942 baton->per_cu = cu->per_cu;
15943 gdb_assert (baton->per_cu);
15944
15945 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15946
15947 if (member_loc->form_is_constant ())
15948 {
15949 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15950 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15951 }
15952 else
15953 baton->size += DW_BLOCK (member_loc)->size;
15954
15955 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15956 baton->data = ptr;
15957
15958 *ptr++ = DW_OP_call4;
15959 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15960 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15961 ptr += 4;
15962
15963 if (member_loc->form_is_constant ())
15964 {
15965 *ptr++ = DW_OP_addr;
15966 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15967 ptr += cu->header.addr_size;
15968 }
15969 else
15970 {
15971 /* We have to copy the data here, because DW_OP_call4 will only
15972 use a DW_AT_location attribute. */
15973 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15974 ptr += DW_BLOCK (member_loc)->size;
15975 }
15976
15977 *ptr++ = DW_OP_plus;
15978 gdb_assert (ptr - baton->data == baton->size);
15979
15980 SYMBOL_LOCATION_BATON (sym) = baton;
15981 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15982 }
15983
15984 /* Create appropriate locally-scoped variables for all the
15985 DW_TAG_common_block entries. Also create a struct common_block
15986 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15987 is used to separate the common blocks name namespace from regular
15988 variable names. */
15989
15990 static void
15991 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15992 {
15993 struct attribute *attr;
15994
15995 attr = dwarf2_attr (die, DW_AT_location, cu);
15996 if (attr != nullptr)
15997 {
15998 /* Support the .debug_loc offsets. */
15999 if (attr->form_is_block ())
16000 {
16001 /* Ok. */
16002 }
16003 else if (attr->form_is_section_offset ())
16004 {
16005 dwarf2_complex_location_expr_complaint ();
16006 attr = NULL;
16007 }
16008 else
16009 {
16010 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16011 "common block member");
16012 attr = NULL;
16013 }
16014 }
16015
16016 if (die->child != NULL)
16017 {
16018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16019 struct die_info *child_die;
16020 size_t n_entries = 0, size;
16021 struct common_block *common_block;
16022 struct symbol *sym;
16023
16024 for (child_die = die->child;
16025 child_die && child_die->tag;
16026 child_die = sibling_die (child_die))
16027 ++n_entries;
16028
16029 size = (sizeof (struct common_block)
16030 + (n_entries - 1) * sizeof (struct symbol *));
16031 common_block
16032 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16033 size);
16034 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16035 common_block->n_entries = 0;
16036
16037 for (child_die = die->child;
16038 child_die && child_die->tag;
16039 child_die = sibling_die (child_die))
16040 {
16041 /* Create the symbol in the DW_TAG_common_block block in the current
16042 symbol scope. */
16043 sym = new_symbol (child_die, NULL, cu);
16044 if (sym != NULL)
16045 {
16046 struct attribute *member_loc;
16047
16048 common_block->contents[common_block->n_entries++] = sym;
16049
16050 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16051 cu);
16052 if (member_loc)
16053 {
16054 /* GDB has handled this for a long time, but it is
16055 not specified by DWARF. It seems to have been
16056 emitted by gfortran at least as recently as:
16057 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16058 complaint (_("Variable in common block has "
16059 "DW_AT_data_member_location "
16060 "- DIE at %s [in module %s]"),
16061 sect_offset_str (child_die->sect_off),
16062 objfile_name (objfile));
16063
16064 if (member_loc->form_is_section_offset ())
16065 dwarf2_complex_location_expr_complaint ();
16066 else if (member_loc->form_is_constant ()
16067 || member_loc->form_is_block ())
16068 {
16069 if (attr != nullptr)
16070 mark_common_block_symbol_computed (sym, die, attr,
16071 member_loc, cu);
16072 }
16073 else
16074 dwarf2_complex_location_expr_complaint ();
16075 }
16076 }
16077 }
16078
16079 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16080 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16081 }
16082 }
16083
16084 /* Create a type for a C++ namespace. */
16085
16086 static struct type *
16087 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16088 {
16089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16090 const char *previous_prefix, *name;
16091 int is_anonymous;
16092 struct type *type;
16093
16094 /* For extensions, reuse the type of the original namespace. */
16095 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16096 {
16097 struct die_info *ext_die;
16098 struct dwarf2_cu *ext_cu = cu;
16099
16100 ext_die = dwarf2_extension (die, &ext_cu);
16101 type = read_type_die (ext_die, ext_cu);
16102
16103 /* EXT_CU may not be the same as CU.
16104 Ensure TYPE is recorded with CU in die_type_hash. */
16105 return set_die_type (die, type, cu);
16106 }
16107
16108 name = namespace_name (die, &is_anonymous, cu);
16109
16110 /* Now build the name of the current namespace. */
16111
16112 previous_prefix = determine_prefix (die, cu);
16113 if (previous_prefix[0] != '\0')
16114 name = typename_concat (&objfile->objfile_obstack,
16115 previous_prefix, name, 0, cu);
16116
16117 /* Create the type. */
16118 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16119
16120 return set_die_type (die, type, cu);
16121 }
16122
16123 /* Read a namespace scope. */
16124
16125 static void
16126 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16127 {
16128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16129 int is_anonymous;
16130
16131 /* Add a symbol associated to this if we haven't seen the namespace
16132 before. Also, add a using directive if it's an anonymous
16133 namespace. */
16134
16135 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16136 {
16137 struct type *type;
16138
16139 type = read_type_die (die, cu);
16140 new_symbol (die, type, cu);
16141
16142 namespace_name (die, &is_anonymous, cu);
16143 if (is_anonymous)
16144 {
16145 const char *previous_prefix = determine_prefix (die, cu);
16146
16147 std::vector<const char *> excludes;
16148 add_using_directive (using_directives (cu),
16149 previous_prefix, TYPE_NAME (type), NULL,
16150 NULL, excludes, 0, &objfile->objfile_obstack);
16151 }
16152 }
16153
16154 if (die->child != NULL)
16155 {
16156 struct die_info *child_die = die->child;
16157
16158 while (child_die && child_die->tag)
16159 {
16160 process_die (child_die, cu);
16161 child_die = sibling_die (child_die);
16162 }
16163 }
16164 }
16165
16166 /* Read a Fortran module as type. This DIE can be only a declaration used for
16167 imported module. Still we need that type as local Fortran "use ... only"
16168 declaration imports depend on the created type in determine_prefix. */
16169
16170 static struct type *
16171 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16172 {
16173 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16174 const char *module_name;
16175 struct type *type;
16176
16177 module_name = dwarf2_name (die, cu);
16178 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16179
16180 return set_die_type (die, type, cu);
16181 }
16182
16183 /* Read a Fortran module. */
16184
16185 static void
16186 read_module (struct die_info *die, struct dwarf2_cu *cu)
16187 {
16188 struct die_info *child_die = die->child;
16189 struct type *type;
16190
16191 type = read_type_die (die, cu);
16192 new_symbol (die, type, cu);
16193
16194 while (child_die && child_die->tag)
16195 {
16196 process_die (child_die, cu);
16197 child_die = sibling_die (child_die);
16198 }
16199 }
16200
16201 /* Return the name of the namespace represented by DIE. Set
16202 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16203 namespace. */
16204
16205 static const char *
16206 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16207 {
16208 struct die_info *current_die;
16209 const char *name = NULL;
16210
16211 /* Loop through the extensions until we find a name. */
16212
16213 for (current_die = die;
16214 current_die != NULL;
16215 current_die = dwarf2_extension (die, &cu))
16216 {
16217 /* We don't use dwarf2_name here so that we can detect the absence
16218 of a name -> anonymous namespace. */
16219 name = dwarf2_string_attr (die, DW_AT_name, cu);
16220
16221 if (name != NULL)
16222 break;
16223 }
16224
16225 /* Is it an anonymous namespace? */
16226
16227 *is_anonymous = (name == NULL);
16228 if (*is_anonymous)
16229 name = CP_ANONYMOUS_NAMESPACE_STR;
16230
16231 return name;
16232 }
16233
16234 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16235 the user defined type vector. */
16236
16237 static struct type *
16238 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16239 {
16240 struct gdbarch *gdbarch
16241 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16242 struct comp_unit_head *cu_header = &cu->header;
16243 struct type *type;
16244 struct attribute *attr_byte_size;
16245 struct attribute *attr_address_class;
16246 int byte_size, addr_class;
16247 struct type *target_type;
16248
16249 target_type = die_type (die, cu);
16250
16251 /* The die_type call above may have already set the type for this DIE. */
16252 type = get_die_type (die, cu);
16253 if (type)
16254 return type;
16255
16256 type = lookup_pointer_type (target_type);
16257
16258 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16259 if (attr_byte_size)
16260 byte_size = DW_UNSND (attr_byte_size);
16261 else
16262 byte_size = cu_header->addr_size;
16263
16264 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16265 if (attr_address_class)
16266 addr_class = DW_UNSND (attr_address_class);
16267 else
16268 addr_class = DW_ADDR_none;
16269
16270 ULONGEST alignment = get_alignment (cu, die);
16271
16272 /* If the pointer size, alignment, or address class is different
16273 than the default, create a type variant marked as such and set
16274 the length accordingly. */
16275 if (TYPE_LENGTH (type) != byte_size
16276 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16277 && alignment != TYPE_RAW_ALIGN (type))
16278 || addr_class != DW_ADDR_none)
16279 {
16280 if (gdbarch_address_class_type_flags_p (gdbarch))
16281 {
16282 int type_flags;
16283
16284 type_flags = gdbarch_address_class_type_flags
16285 (gdbarch, byte_size, addr_class);
16286 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16287 == 0);
16288 type = make_type_with_address_space (type, type_flags);
16289 }
16290 else if (TYPE_LENGTH (type) != byte_size)
16291 {
16292 complaint (_("invalid pointer size %d"), byte_size);
16293 }
16294 else if (TYPE_RAW_ALIGN (type) != alignment)
16295 {
16296 complaint (_("Invalid DW_AT_alignment"
16297 " - DIE at %s [in module %s]"),
16298 sect_offset_str (die->sect_off),
16299 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16300 }
16301 else
16302 {
16303 /* Should we also complain about unhandled address classes? */
16304 }
16305 }
16306
16307 TYPE_LENGTH (type) = byte_size;
16308 set_type_align (type, alignment);
16309 return set_die_type (die, type, cu);
16310 }
16311
16312 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16313 the user defined type vector. */
16314
16315 static struct type *
16316 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16317 {
16318 struct type *type;
16319 struct type *to_type;
16320 struct type *domain;
16321
16322 to_type = die_type (die, cu);
16323 domain = die_containing_type (die, cu);
16324
16325 /* The calls above may have already set the type for this DIE. */
16326 type = get_die_type (die, cu);
16327 if (type)
16328 return type;
16329
16330 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16331 type = lookup_methodptr_type (to_type);
16332 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16333 {
16334 struct type *new_type
16335 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16336
16337 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16338 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16339 TYPE_VARARGS (to_type));
16340 type = lookup_methodptr_type (new_type);
16341 }
16342 else
16343 type = lookup_memberptr_type (to_type, domain);
16344
16345 return set_die_type (die, type, cu);
16346 }
16347
16348 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16349 the user defined type vector. */
16350
16351 static struct type *
16352 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16353 enum type_code refcode)
16354 {
16355 struct comp_unit_head *cu_header = &cu->header;
16356 struct type *type, *target_type;
16357 struct attribute *attr;
16358
16359 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16360
16361 target_type = die_type (die, cu);
16362
16363 /* The die_type call above may have already set the type for this DIE. */
16364 type = get_die_type (die, cu);
16365 if (type)
16366 return type;
16367
16368 type = lookup_reference_type (target_type, refcode);
16369 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16370 if (attr != nullptr)
16371 {
16372 TYPE_LENGTH (type) = DW_UNSND (attr);
16373 }
16374 else
16375 {
16376 TYPE_LENGTH (type) = cu_header->addr_size;
16377 }
16378 maybe_set_alignment (cu, die, type);
16379 return set_die_type (die, type, cu);
16380 }
16381
16382 /* Add the given cv-qualifiers to the element type of the array. GCC
16383 outputs DWARF type qualifiers that apply to an array, not the
16384 element type. But GDB relies on the array element type to carry
16385 the cv-qualifiers. This mimics section 6.7.3 of the C99
16386 specification. */
16387
16388 static struct type *
16389 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16390 struct type *base_type, int cnst, int voltl)
16391 {
16392 struct type *el_type, *inner_array;
16393
16394 base_type = copy_type (base_type);
16395 inner_array = base_type;
16396
16397 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16398 {
16399 TYPE_TARGET_TYPE (inner_array) =
16400 copy_type (TYPE_TARGET_TYPE (inner_array));
16401 inner_array = TYPE_TARGET_TYPE (inner_array);
16402 }
16403
16404 el_type = TYPE_TARGET_TYPE (inner_array);
16405 cnst |= TYPE_CONST (el_type);
16406 voltl |= TYPE_VOLATILE (el_type);
16407 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16408
16409 return set_die_type (die, base_type, cu);
16410 }
16411
16412 static struct type *
16413 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16414 {
16415 struct type *base_type, *cv_type;
16416
16417 base_type = die_type (die, cu);
16418
16419 /* The die_type call above may have already set the type for this DIE. */
16420 cv_type = get_die_type (die, cu);
16421 if (cv_type)
16422 return cv_type;
16423
16424 /* In case the const qualifier is applied to an array type, the element type
16425 is so qualified, not the array type (section 6.7.3 of C99). */
16426 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16427 return add_array_cv_type (die, cu, base_type, 1, 0);
16428
16429 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16430 return set_die_type (die, cv_type, cu);
16431 }
16432
16433 static struct type *
16434 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16435 {
16436 struct type *base_type, *cv_type;
16437
16438 base_type = die_type (die, cu);
16439
16440 /* The die_type call above may have already set the type for this DIE. */
16441 cv_type = get_die_type (die, cu);
16442 if (cv_type)
16443 return cv_type;
16444
16445 /* In case the volatile qualifier is applied to an array type, the
16446 element type is so qualified, not the array type (section 6.7.3
16447 of C99). */
16448 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16449 return add_array_cv_type (die, cu, base_type, 0, 1);
16450
16451 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16452 return set_die_type (die, cv_type, cu);
16453 }
16454
16455 /* Handle DW_TAG_restrict_type. */
16456
16457 static struct type *
16458 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16459 {
16460 struct type *base_type, *cv_type;
16461
16462 base_type = die_type (die, cu);
16463
16464 /* The die_type call above may have already set the type for this DIE. */
16465 cv_type = get_die_type (die, cu);
16466 if (cv_type)
16467 return cv_type;
16468
16469 cv_type = make_restrict_type (base_type);
16470 return set_die_type (die, cv_type, cu);
16471 }
16472
16473 /* Handle DW_TAG_atomic_type. */
16474
16475 static struct type *
16476 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16477 {
16478 struct type *base_type, *cv_type;
16479
16480 base_type = die_type (die, cu);
16481
16482 /* The die_type call above may have already set the type for this DIE. */
16483 cv_type = get_die_type (die, cu);
16484 if (cv_type)
16485 return cv_type;
16486
16487 cv_type = make_atomic_type (base_type);
16488 return set_die_type (die, cv_type, cu);
16489 }
16490
16491 /* Extract all information from a DW_TAG_string_type DIE and add to
16492 the user defined type vector. It isn't really a user defined type,
16493 but it behaves like one, with other DIE's using an AT_user_def_type
16494 attribute to reference it. */
16495
16496 static struct type *
16497 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16498 {
16499 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16500 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16501 struct type *type, *range_type, *index_type, *char_type;
16502 struct attribute *attr;
16503 struct dynamic_prop prop;
16504 bool length_is_constant = true;
16505 LONGEST length;
16506
16507 /* There are a couple of places where bit sizes might be made use of
16508 when parsing a DW_TAG_string_type, however, no producer that we know
16509 of make use of these. Handling bit sizes that are a multiple of the
16510 byte size is easy enough, but what about other bit sizes? Lets deal
16511 with that problem when we have to. Warn about these attributes being
16512 unsupported, then parse the type and ignore them like we always
16513 have. */
16514 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16515 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16516 {
16517 static bool warning_printed = false;
16518 if (!warning_printed)
16519 {
16520 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16521 "currently supported on DW_TAG_string_type."));
16522 warning_printed = true;
16523 }
16524 }
16525
16526 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16527 if (attr != nullptr && !attr->form_is_constant ())
16528 {
16529 /* The string length describes the location at which the length of
16530 the string can be found. The size of the length field can be
16531 specified with one of the attributes below. */
16532 struct type *prop_type;
16533 struct attribute *len
16534 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16535 if (len == nullptr)
16536 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16537 if (len != nullptr && len->form_is_constant ())
16538 {
16539 /* Pass 0 as the default as we know this attribute is constant
16540 and the default value will not be returned. */
16541 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16542 prop_type = cu->per_cu->int_type (sz, true);
16543 }
16544 else
16545 {
16546 /* If the size is not specified then we assume it is the size of
16547 an address on this target. */
16548 prop_type = cu->per_cu->addr_sized_int_type (true);
16549 }
16550
16551 /* Convert the attribute into a dynamic property. */
16552 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16553 length = 1;
16554 else
16555 length_is_constant = false;
16556 }
16557 else if (attr != nullptr)
16558 {
16559 /* This DW_AT_string_length just contains the length with no
16560 indirection. There's no need to create a dynamic property in this
16561 case. Pass 0 for the default value as we know it will not be
16562 returned in this case. */
16563 length = dwarf2_get_attr_constant_value (attr, 0);
16564 }
16565 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16566 {
16567 /* We don't currently support non-constant byte sizes for strings. */
16568 length = dwarf2_get_attr_constant_value (attr, 1);
16569 }
16570 else
16571 {
16572 /* Use 1 as a fallback length if we have nothing else. */
16573 length = 1;
16574 }
16575
16576 index_type = objfile_type (objfile)->builtin_int;
16577 if (length_is_constant)
16578 range_type = create_static_range_type (NULL, index_type, 1, length);
16579 else
16580 {
16581 struct dynamic_prop low_bound;
16582
16583 low_bound.kind = PROP_CONST;
16584 low_bound.data.const_val = 1;
16585 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16586 }
16587 char_type = language_string_char_type (cu->language_defn, gdbarch);
16588 type = create_string_type (NULL, char_type, range_type);
16589
16590 return set_die_type (die, type, cu);
16591 }
16592
16593 /* Assuming that DIE corresponds to a function, returns nonzero
16594 if the function is prototyped. */
16595
16596 static int
16597 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16598 {
16599 struct attribute *attr;
16600
16601 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16602 if (attr && (DW_UNSND (attr) != 0))
16603 return 1;
16604
16605 /* The DWARF standard implies that the DW_AT_prototyped attribute
16606 is only meaningful for C, but the concept also extends to other
16607 languages that allow unprototyped functions (Eg: Objective C).
16608 For all other languages, assume that functions are always
16609 prototyped. */
16610 if (cu->language != language_c
16611 && cu->language != language_objc
16612 && cu->language != language_opencl)
16613 return 1;
16614
16615 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16616 prototyped and unprototyped functions; default to prototyped,
16617 since that is more common in modern code (and RealView warns
16618 about unprototyped functions). */
16619 if (producer_is_realview (cu->producer))
16620 return 1;
16621
16622 return 0;
16623 }
16624
16625 /* Handle DIES due to C code like:
16626
16627 struct foo
16628 {
16629 int (*funcp)(int a, long l);
16630 int b;
16631 };
16632
16633 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16634
16635 static struct type *
16636 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16637 {
16638 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16639 struct type *type; /* Type that this function returns. */
16640 struct type *ftype; /* Function that returns above type. */
16641 struct attribute *attr;
16642
16643 type = die_type (die, cu);
16644
16645 /* The die_type call above may have already set the type for this DIE. */
16646 ftype = get_die_type (die, cu);
16647 if (ftype)
16648 return ftype;
16649
16650 ftype = lookup_function_type (type);
16651
16652 if (prototyped_function_p (die, cu))
16653 TYPE_PROTOTYPED (ftype) = 1;
16654
16655 /* Store the calling convention in the type if it's available in
16656 the subroutine die. Otherwise set the calling convention to
16657 the default value DW_CC_normal. */
16658 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16659 if (attr != nullptr
16660 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16661 TYPE_CALLING_CONVENTION (ftype)
16662 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16663 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16664 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16665 else
16666 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16667
16668 /* Record whether the function returns normally to its caller or not
16669 if the DWARF producer set that information. */
16670 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16671 if (attr && (DW_UNSND (attr) != 0))
16672 TYPE_NO_RETURN (ftype) = 1;
16673
16674 /* We need to add the subroutine type to the die immediately so
16675 we don't infinitely recurse when dealing with parameters
16676 declared as the same subroutine type. */
16677 set_die_type (die, ftype, cu);
16678
16679 if (die->child != NULL)
16680 {
16681 struct type *void_type = objfile_type (objfile)->builtin_void;
16682 struct die_info *child_die;
16683 int nparams, iparams;
16684
16685 /* Count the number of parameters.
16686 FIXME: GDB currently ignores vararg functions, but knows about
16687 vararg member functions. */
16688 nparams = 0;
16689 child_die = die->child;
16690 while (child_die && child_die->tag)
16691 {
16692 if (child_die->tag == DW_TAG_formal_parameter)
16693 nparams++;
16694 else if (child_die->tag == DW_TAG_unspecified_parameters)
16695 TYPE_VARARGS (ftype) = 1;
16696 child_die = sibling_die (child_die);
16697 }
16698
16699 /* Allocate storage for parameters and fill them in. */
16700 TYPE_NFIELDS (ftype) = nparams;
16701 TYPE_FIELDS (ftype) = (struct field *)
16702 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16703
16704 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16705 even if we error out during the parameters reading below. */
16706 for (iparams = 0; iparams < nparams; iparams++)
16707 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16708
16709 iparams = 0;
16710 child_die = die->child;
16711 while (child_die && child_die->tag)
16712 {
16713 if (child_die->tag == DW_TAG_formal_parameter)
16714 {
16715 struct type *arg_type;
16716
16717 /* DWARF version 2 has no clean way to discern C++
16718 static and non-static member functions. G++ helps
16719 GDB by marking the first parameter for non-static
16720 member functions (which is the this pointer) as
16721 artificial. We pass this information to
16722 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16723
16724 DWARF version 3 added DW_AT_object_pointer, which GCC
16725 4.5 does not yet generate. */
16726 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16727 if (attr != nullptr)
16728 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16729 else
16730 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16731 arg_type = die_type (child_die, cu);
16732
16733 /* RealView does not mark THIS as const, which the testsuite
16734 expects. GCC marks THIS as const in method definitions,
16735 but not in the class specifications (GCC PR 43053). */
16736 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16737 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16738 {
16739 int is_this = 0;
16740 struct dwarf2_cu *arg_cu = cu;
16741 const char *name = dwarf2_name (child_die, cu);
16742
16743 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16744 if (attr != nullptr)
16745 {
16746 /* If the compiler emits this, use it. */
16747 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16748 is_this = 1;
16749 }
16750 else if (name && strcmp (name, "this") == 0)
16751 /* Function definitions will have the argument names. */
16752 is_this = 1;
16753 else if (name == NULL && iparams == 0)
16754 /* Declarations may not have the names, so like
16755 elsewhere in GDB, assume an artificial first
16756 argument is "this". */
16757 is_this = 1;
16758
16759 if (is_this)
16760 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16761 arg_type, 0);
16762 }
16763
16764 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16765 iparams++;
16766 }
16767 child_die = sibling_die (child_die);
16768 }
16769 }
16770
16771 return ftype;
16772 }
16773
16774 static struct type *
16775 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16776 {
16777 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16778 const char *name = NULL;
16779 struct type *this_type, *target_type;
16780
16781 name = dwarf2_full_name (NULL, die, cu);
16782 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16783 TYPE_TARGET_STUB (this_type) = 1;
16784 set_die_type (die, this_type, cu);
16785 target_type = die_type (die, cu);
16786 if (target_type != this_type)
16787 TYPE_TARGET_TYPE (this_type) = target_type;
16788 else
16789 {
16790 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16791 spec and cause infinite loops in GDB. */
16792 complaint (_("Self-referential DW_TAG_typedef "
16793 "- DIE at %s [in module %s]"),
16794 sect_offset_str (die->sect_off), objfile_name (objfile));
16795 TYPE_TARGET_TYPE (this_type) = NULL;
16796 }
16797 return this_type;
16798 }
16799
16800 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16801 (which may be different from NAME) to the architecture back-end to allow
16802 it to guess the correct format if necessary. */
16803
16804 static struct type *
16805 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16806 const char *name_hint, enum bfd_endian byte_order)
16807 {
16808 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16809 const struct floatformat **format;
16810 struct type *type;
16811
16812 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16813 if (format)
16814 type = init_float_type (objfile, bits, name, format, byte_order);
16815 else
16816 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16817
16818 return type;
16819 }
16820
16821 /* Allocate an integer type of size BITS and name NAME. */
16822
16823 static struct type *
16824 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16825 int bits, int unsigned_p, const char *name)
16826 {
16827 struct type *type;
16828
16829 /* Versions of Intel's C Compiler generate an integer type called "void"
16830 instead of using DW_TAG_unspecified_type. This has been seen on
16831 at least versions 14, 17, and 18. */
16832 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16833 && strcmp (name, "void") == 0)
16834 type = objfile_type (objfile)->builtin_void;
16835 else
16836 type = init_integer_type (objfile, bits, unsigned_p, name);
16837
16838 return type;
16839 }
16840
16841 /* Initialise and return a floating point type of size BITS suitable for
16842 use as a component of a complex number. The NAME_HINT is passed through
16843 when initialising the floating point type and is the name of the complex
16844 type.
16845
16846 As DWARF doesn't currently provide an explicit name for the components
16847 of a complex number, but it can be helpful to have these components
16848 named, we try to select a suitable name based on the size of the
16849 component. */
16850 static struct type *
16851 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16852 struct objfile *objfile,
16853 int bits, const char *name_hint,
16854 enum bfd_endian byte_order)
16855 {
16856 gdbarch *gdbarch = get_objfile_arch (objfile);
16857 struct type *tt = nullptr;
16858
16859 /* Try to find a suitable floating point builtin type of size BITS.
16860 We're going to use the name of this type as the name for the complex
16861 target type that we are about to create. */
16862 switch (cu->language)
16863 {
16864 case language_fortran:
16865 switch (bits)
16866 {
16867 case 32:
16868 tt = builtin_f_type (gdbarch)->builtin_real;
16869 break;
16870 case 64:
16871 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16872 break;
16873 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16874 case 128:
16875 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16876 break;
16877 }
16878 break;
16879 default:
16880 switch (bits)
16881 {
16882 case 32:
16883 tt = builtin_type (gdbarch)->builtin_float;
16884 break;
16885 case 64:
16886 tt = builtin_type (gdbarch)->builtin_double;
16887 break;
16888 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16889 case 128:
16890 tt = builtin_type (gdbarch)->builtin_long_double;
16891 break;
16892 }
16893 break;
16894 }
16895
16896 /* If the type we found doesn't match the size we were looking for, then
16897 pretend we didn't find a type at all, the complex target type we
16898 create will then be nameless. */
16899 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16900 tt = nullptr;
16901
16902 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16903 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16904 }
16905
16906 /* Find a representation of a given base type and install
16907 it in the TYPE field of the die. */
16908
16909 static struct type *
16910 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16911 {
16912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16913 struct type *type;
16914 struct attribute *attr;
16915 int encoding = 0, bits = 0;
16916 const char *name;
16917 gdbarch *arch;
16918
16919 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16920 if (attr != nullptr)
16921 encoding = DW_UNSND (attr);
16922 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16923 if (attr != nullptr)
16924 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16925 name = dwarf2_name (die, cu);
16926 if (!name)
16927 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16928
16929 arch = get_objfile_arch (objfile);
16930 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16931
16932 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16933 if (attr)
16934 {
16935 int endianity = DW_UNSND (attr);
16936
16937 switch (endianity)
16938 {
16939 case DW_END_big:
16940 byte_order = BFD_ENDIAN_BIG;
16941 break;
16942 case DW_END_little:
16943 byte_order = BFD_ENDIAN_LITTLE;
16944 break;
16945 default:
16946 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16947 break;
16948 }
16949 }
16950
16951 switch (encoding)
16952 {
16953 case DW_ATE_address:
16954 /* Turn DW_ATE_address into a void * pointer. */
16955 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16956 type = init_pointer_type (objfile, bits, name, type);
16957 break;
16958 case DW_ATE_boolean:
16959 type = init_boolean_type (objfile, bits, 1, name);
16960 break;
16961 case DW_ATE_complex_float:
16962 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16963 byte_order);
16964 type = init_complex_type (objfile, name, type);
16965 break;
16966 case DW_ATE_decimal_float:
16967 type = init_decfloat_type (objfile, bits, name);
16968 break;
16969 case DW_ATE_float:
16970 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16971 break;
16972 case DW_ATE_signed:
16973 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16974 break;
16975 case DW_ATE_unsigned:
16976 if (cu->language == language_fortran
16977 && name
16978 && startswith (name, "character("))
16979 type = init_character_type (objfile, bits, 1, name);
16980 else
16981 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16982 break;
16983 case DW_ATE_signed_char:
16984 if (cu->language == language_ada || cu->language == language_m2
16985 || cu->language == language_pascal
16986 || cu->language == language_fortran)
16987 type = init_character_type (objfile, bits, 0, name);
16988 else
16989 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16990 break;
16991 case DW_ATE_unsigned_char:
16992 if (cu->language == language_ada || cu->language == language_m2
16993 || cu->language == language_pascal
16994 || cu->language == language_fortran
16995 || cu->language == language_rust)
16996 type = init_character_type (objfile, bits, 1, name);
16997 else
16998 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16999 break;
17000 case DW_ATE_UTF:
17001 {
17002 if (bits == 16)
17003 type = builtin_type (arch)->builtin_char16;
17004 else if (bits == 32)
17005 type = builtin_type (arch)->builtin_char32;
17006 else
17007 {
17008 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17009 bits);
17010 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17011 }
17012 return set_die_type (die, type, cu);
17013 }
17014 break;
17015
17016 default:
17017 complaint (_("unsupported DW_AT_encoding: '%s'"),
17018 dwarf_type_encoding_name (encoding));
17019 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17020 break;
17021 }
17022
17023 if (name && strcmp (name, "char") == 0)
17024 TYPE_NOSIGN (type) = 1;
17025
17026 maybe_set_alignment (cu, die, type);
17027
17028 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17029
17030 return set_die_type (die, type, cu);
17031 }
17032
17033 /* Parse dwarf attribute if it's a block, reference or constant and put the
17034 resulting value of the attribute into struct bound_prop.
17035 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17036
17037 static int
17038 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17039 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17040 struct type *default_type)
17041 {
17042 struct dwarf2_property_baton *baton;
17043 struct obstack *obstack
17044 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17045
17046 gdb_assert (default_type != NULL);
17047
17048 if (attr == NULL || prop == NULL)
17049 return 0;
17050
17051 if (attr->form_is_block ())
17052 {
17053 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17054 baton->property_type = default_type;
17055 baton->locexpr.per_cu = cu->per_cu;
17056 baton->locexpr.size = DW_BLOCK (attr)->size;
17057 baton->locexpr.data = DW_BLOCK (attr)->data;
17058 switch (attr->name)
17059 {
17060 case DW_AT_string_length:
17061 baton->locexpr.is_reference = true;
17062 break;
17063 default:
17064 baton->locexpr.is_reference = false;
17065 break;
17066 }
17067 prop->data.baton = baton;
17068 prop->kind = PROP_LOCEXPR;
17069 gdb_assert (prop->data.baton != NULL);
17070 }
17071 else if (attr->form_is_ref ())
17072 {
17073 struct dwarf2_cu *target_cu = cu;
17074 struct die_info *target_die;
17075 struct attribute *target_attr;
17076
17077 target_die = follow_die_ref (die, attr, &target_cu);
17078 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17079 if (target_attr == NULL)
17080 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17081 target_cu);
17082 if (target_attr == NULL)
17083 return 0;
17084
17085 switch (target_attr->name)
17086 {
17087 case DW_AT_location:
17088 if (target_attr->form_is_section_offset ())
17089 {
17090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17091 baton->property_type = die_type (target_die, target_cu);
17092 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17093 prop->data.baton = baton;
17094 prop->kind = PROP_LOCLIST;
17095 gdb_assert (prop->data.baton != NULL);
17096 }
17097 else if (target_attr->form_is_block ())
17098 {
17099 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17100 baton->property_type = die_type (target_die, target_cu);
17101 baton->locexpr.per_cu = cu->per_cu;
17102 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17103 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17104 baton->locexpr.is_reference = true;
17105 prop->data.baton = baton;
17106 prop->kind = PROP_LOCEXPR;
17107 gdb_assert (prop->data.baton != NULL);
17108 }
17109 else
17110 {
17111 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17112 "dynamic property");
17113 return 0;
17114 }
17115 break;
17116 case DW_AT_data_member_location:
17117 {
17118 LONGEST offset;
17119
17120 if (!handle_data_member_location (target_die, target_cu,
17121 &offset))
17122 return 0;
17123
17124 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17125 baton->property_type = read_type_die (target_die->parent,
17126 target_cu);
17127 baton->offset_info.offset = offset;
17128 baton->offset_info.type = die_type (target_die, target_cu);
17129 prop->data.baton = baton;
17130 prop->kind = PROP_ADDR_OFFSET;
17131 break;
17132 }
17133 }
17134 }
17135 else if (attr->form_is_constant ())
17136 {
17137 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17138 prop->kind = PROP_CONST;
17139 }
17140 else
17141 {
17142 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17143 dwarf2_name (die, cu));
17144 return 0;
17145 }
17146
17147 return 1;
17148 }
17149
17150 /* See read.h. */
17151
17152 struct type *
17153 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17154 {
17155 struct objfile *objfile = dwarf2_per_objfile->objfile;
17156 struct type *int_type;
17157
17158 /* Helper macro to examine the various builtin types. */
17159 #define TRY_TYPE(F) \
17160 int_type = (unsigned_p \
17161 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17162 : objfile_type (objfile)->builtin_ ## F); \
17163 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17164 return int_type
17165
17166 TRY_TYPE (char);
17167 TRY_TYPE (short);
17168 TRY_TYPE (int);
17169 TRY_TYPE (long);
17170 TRY_TYPE (long_long);
17171
17172 #undef TRY_TYPE
17173
17174 gdb_assert_not_reached ("unable to find suitable integer type");
17175 }
17176
17177 /* See read.h. */
17178
17179 struct type *
17180 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17181 {
17182 int addr_size = this->addr_size ();
17183 return int_type (addr_size, unsigned_p);
17184 }
17185
17186 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17187 present (which is valid) then compute the default type based on the
17188 compilation units address size. */
17189
17190 static struct type *
17191 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17192 {
17193 struct type *index_type = die_type (die, cu);
17194
17195 /* Dwarf-2 specifications explicitly allows to create subrange types
17196 without specifying a base type.
17197 In that case, the base type must be set to the type of
17198 the lower bound, upper bound or count, in that order, if any of these
17199 three attributes references an object that has a type.
17200 If no base type is found, the Dwarf-2 specifications say that
17201 a signed integer type of size equal to the size of an address should
17202 be used.
17203 For the following C code: `extern char gdb_int [];'
17204 GCC produces an empty range DIE.
17205 FIXME: muller/2010-05-28: Possible references to object for low bound,
17206 high bound or count are not yet handled by this code. */
17207 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17208 index_type = cu->per_cu->addr_sized_int_type (false);
17209
17210 return index_type;
17211 }
17212
17213 /* Read the given DW_AT_subrange DIE. */
17214
17215 static struct type *
17216 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17217 {
17218 struct type *base_type, *orig_base_type;
17219 struct type *range_type;
17220 struct attribute *attr;
17221 struct dynamic_prop low, high;
17222 int low_default_is_valid;
17223 int high_bound_is_count = 0;
17224 const char *name;
17225 ULONGEST negative_mask;
17226
17227 orig_base_type = read_subrange_index_type (die, cu);
17228
17229 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17230 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17231 creating the range type, but we use the result of check_typedef
17232 when examining properties of the type. */
17233 base_type = check_typedef (orig_base_type);
17234
17235 /* The die_type call above may have already set the type for this DIE. */
17236 range_type = get_die_type (die, cu);
17237 if (range_type)
17238 return range_type;
17239
17240 low.kind = PROP_CONST;
17241 high.kind = PROP_CONST;
17242 high.data.const_val = 0;
17243
17244 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17245 omitting DW_AT_lower_bound. */
17246 switch (cu->language)
17247 {
17248 case language_c:
17249 case language_cplus:
17250 low.data.const_val = 0;
17251 low_default_is_valid = 1;
17252 break;
17253 case language_fortran:
17254 low.data.const_val = 1;
17255 low_default_is_valid = 1;
17256 break;
17257 case language_d:
17258 case language_objc:
17259 case language_rust:
17260 low.data.const_val = 0;
17261 low_default_is_valid = (cu->header.version >= 4);
17262 break;
17263 case language_ada:
17264 case language_m2:
17265 case language_pascal:
17266 low.data.const_val = 1;
17267 low_default_is_valid = (cu->header.version >= 4);
17268 break;
17269 default:
17270 low.data.const_val = 0;
17271 low_default_is_valid = 0;
17272 break;
17273 }
17274
17275 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17276 if (attr != nullptr)
17277 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17278 else if (!low_default_is_valid)
17279 complaint (_("Missing DW_AT_lower_bound "
17280 "- DIE at %s [in module %s]"),
17281 sect_offset_str (die->sect_off),
17282 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17283
17284 struct attribute *attr_ub, *attr_count;
17285 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17286 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17287 {
17288 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17289 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17290 {
17291 /* If bounds are constant do the final calculation here. */
17292 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17293 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17294 else
17295 high_bound_is_count = 1;
17296 }
17297 else
17298 {
17299 if (attr_ub != NULL)
17300 complaint (_("Unresolved DW_AT_upper_bound "
17301 "- DIE at %s [in module %s]"),
17302 sect_offset_str (die->sect_off),
17303 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17304 if (attr_count != NULL)
17305 complaint (_("Unresolved DW_AT_count "
17306 "- DIE at %s [in module %s]"),
17307 sect_offset_str (die->sect_off),
17308 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17309 }
17310 }
17311
17312 LONGEST bias = 0;
17313 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17314 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17315 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17316
17317 /* Normally, the DWARF producers are expected to use a signed
17318 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17319 But this is unfortunately not always the case, as witnessed
17320 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17321 is used instead. To work around that ambiguity, we treat
17322 the bounds as signed, and thus sign-extend their values, when
17323 the base type is signed. */
17324 negative_mask =
17325 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17326 if (low.kind == PROP_CONST
17327 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17328 low.data.const_val |= negative_mask;
17329 if (high.kind == PROP_CONST
17330 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17331 high.data.const_val |= negative_mask;
17332
17333 /* Check for bit and byte strides. */
17334 struct dynamic_prop byte_stride_prop;
17335 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17336 if (attr_byte_stride != nullptr)
17337 {
17338 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17339 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17340 prop_type);
17341 }
17342
17343 struct dynamic_prop bit_stride_prop;
17344 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17345 if (attr_bit_stride != nullptr)
17346 {
17347 /* It only makes sense to have either a bit or byte stride. */
17348 if (attr_byte_stride != nullptr)
17349 {
17350 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17351 "- DIE at %s [in module %s]"),
17352 sect_offset_str (die->sect_off),
17353 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17354 attr_bit_stride = nullptr;
17355 }
17356 else
17357 {
17358 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17359 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17360 prop_type);
17361 }
17362 }
17363
17364 if (attr_byte_stride != nullptr
17365 || attr_bit_stride != nullptr)
17366 {
17367 bool byte_stride_p = (attr_byte_stride != nullptr);
17368 struct dynamic_prop *stride
17369 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17370
17371 range_type
17372 = create_range_type_with_stride (NULL, orig_base_type, &low,
17373 &high, bias, stride, byte_stride_p);
17374 }
17375 else
17376 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17377
17378 if (high_bound_is_count)
17379 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17380
17381 /* Ada expects an empty array on no boundary attributes. */
17382 if (attr == NULL && cu->language != language_ada)
17383 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17384
17385 name = dwarf2_name (die, cu);
17386 if (name)
17387 TYPE_NAME (range_type) = name;
17388
17389 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17390 if (attr != nullptr)
17391 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17392
17393 maybe_set_alignment (cu, die, range_type);
17394
17395 set_die_type (die, range_type, cu);
17396
17397 /* set_die_type should be already done. */
17398 set_descriptive_type (range_type, die, cu);
17399
17400 return range_type;
17401 }
17402
17403 static struct type *
17404 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17405 {
17406 struct type *type;
17407
17408 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17409 NULL);
17410 TYPE_NAME (type) = dwarf2_name (die, cu);
17411
17412 /* In Ada, an unspecified type is typically used when the description
17413 of the type is deferred to a different unit. When encountering
17414 such a type, we treat it as a stub, and try to resolve it later on,
17415 when needed. */
17416 if (cu->language == language_ada)
17417 TYPE_STUB (type) = 1;
17418
17419 return set_die_type (die, type, cu);
17420 }
17421
17422 /* Read a single die and all its descendents. Set the die's sibling
17423 field to NULL; set other fields in the die correctly, and set all
17424 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17425 location of the info_ptr after reading all of those dies. PARENT
17426 is the parent of the die in question. */
17427
17428 static struct die_info *
17429 read_die_and_children (const struct die_reader_specs *reader,
17430 const gdb_byte *info_ptr,
17431 const gdb_byte **new_info_ptr,
17432 struct die_info *parent)
17433 {
17434 struct die_info *die;
17435 const gdb_byte *cur_ptr;
17436
17437 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17438 if (die == NULL)
17439 {
17440 *new_info_ptr = cur_ptr;
17441 return NULL;
17442 }
17443 store_in_ref_table (die, reader->cu);
17444
17445 if (die->has_children)
17446 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17447 else
17448 {
17449 die->child = NULL;
17450 *new_info_ptr = cur_ptr;
17451 }
17452
17453 die->sibling = NULL;
17454 die->parent = parent;
17455 return die;
17456 }
17457
17458 /* Read a die, all of its descendents, and all of its siblings; set
17459 all of the fields of all of the dies correctly. Arguments are as
17460 in read_die_and_children. */
17461
17462 static struct die_info *
17463 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17464 const gdb_byte *info_ptr,
17465 const gdb_byte **new_info_ptr,
17466 struct die_info *parent)
17467 {
17468 struct die_info *first_die, *last_sibling;
17469 const gdb_byte *cur_ptr;
17470
17471 cur_ptr = info_ptr;
17472 first_die = last_sibling = NULL;
17473
17474 while (1)
17475 {
17476 struct die_info *die
17477 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17478
17479 if (die == NULL)
17480 {
17481 *new_info_ptr = cur_ptr;
17482 return first_die;
17483 }
17484
17485 if (!first_die)
17486 first_die = die;
17487 else
17488 last_sibling->sibling = die;
17489
17490 last_sibling = die;
17491 }
17492 }
17493
17494 /* Read a die, all of its descendents, and all of its siblings; set
17495 all of the fields of all of the dies correctly. Arguments are as
17496 in read_die_and_children.
17497 This the main entry point for reading a DIE and all its children. */
17498
17499 static struct die_info *
17500 read_die_and_siblings (const struct die_reader_specs *reader,
17501 const gdb_byte *info_ptr,
17502 const gdb_byte **new_info_ptr,
17503 struct die_info *parent)
17504 {
17505 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17506 new_info_ptr, parent);
17507
17508 if (dwarf_die_debug)
17509 {
17510 fprintf_unfiltered (gdb_stdlog,
17511 "Read die from %s@0x%x of %s:\n",
17512 reader->die_section->get_name (),
17513 (unsigned) (info_ptr - reader->die_section->buffer),
17514 bfd_get_filename (reader->abfd));
17515 dump_die (die, dwarf_die_debug);
17516 }
17517
17518 return die;
17519 }
17520
17521 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17522 attributes.
17523 The caller is responsible for filling in the extra attributes
17524 and updating (*DIEP)->num_attrs.
17525 Set DIEP to point to a newly allocated die with its information,
17526 except for its child, sibling, and parent fields. */
17527
17528 static const gdb_byte *
17529 read_full_die_1 (const struct die_reader_specs *reader,
17530 struct die_info **diep, const gdb_byte *info_ptr,
17531 int num_extra_attrs)
17532 {
17533 unsigned int abbrev_number, bytes_read, i;
17534 struct abbrev_info *abbrev;
17535 struct die_info *die;
17536 struct dwarf2_cu *cu = reader->cu;
17537 bfd *abfd = reader->abfd;
17538
17539 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17540 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17541 info_ptr += bytes_read;
17542 if (!abbrev_number)
17543 {
17544 *diep = NULL;
17545 return info_ptr;
17546 }
17547
17548 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17549 if (!abbrev)
17550 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17551 abbrev_number,
17552 bfd_get_filename (abfd));
17553
17554 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17555 die->sect_off = sect_off;
17556 die->tag = abbrev->tag;
17557 die->abbrev = abbrev_number;
17558 die->has_children = abbrev->has_children;
17559
17560 /* Make the result usable.
17561 The caller needs to update num_attrs after adding the extra
17562 attributes. */
17563 die->num_attrs = abbrev->num_attrs;
17564
17565 std::vector<int> indexes_that_need_reprocess;
17566 for (i = 0; i < abbrev->num_attrs; ++i)
17567 {
17568 bool need_reprocess;
17569 info_ptr =
17570 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17571 info_ptr, &need_reprocess);
17572 if (need_reprocess)
17573 indexes_that_need_reprocess.push_back (i);
17574 }
17575
17576 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17577 if (attr != nullptr)
17578 cu->str_offsets_base = DW_UNSND (attr);
17579
17580 auto maybe_addr_base = lookup_addr_base(die);
17581 if (maybe_addr_base.has_value ())
17582 cu->addr_base = *maybe_addr_base;
17583 for (int index : indexes_that_need_reprocess)
17584 read_attribute_reprocess (reader, &die->attrs[index]);
17585 *diep = die;
17586 return info_ptr;
17587 }
17588
17589 /* Read a die and all its attributes.
17590 Set DIEP to point to a newly allocated die with its information,
17591 except for its child, sibling, and parent fields. */
17592
17593 static const gdb_byte *
17594 read_full_die (const struct die_reader_specs *reader,
17595 struct die_info **diep, const gdb_byte *info_ptr)
17596 {
17597 const gdb_byte *result;
17598
17599 result = read_full_die_1 (reader, diep, info_ptr, 0);
17600
17601 if (dwarf_die_debug)
17602 {
17603 fprintf_unfiltered (gdb_stdlog,
17604 "Read die from %s@0x%x of %s:\n",
17605 reader->die_section->get_name (),
17606 (unsigned) (info_ptr - reader->die_section->buffer),
17607 bfd_get_filename (reader->abfd));
17608 dump_die (*diep, dwarf_die_debug);
17609 }
17610
17611 return result;
17612 }
17613 \f
17614
17615 /* Returns nonzero if TAG represents a type that we might generate a partial
17616 symbol for. */
17617
17618 static int
17619 is_type_tag_for_partial (int tag)
17620 {
17621 switch (tag)
17622 {
17623 #if 0
17624 /* Some types that would be reasonable to generate partial symbols for,
17625 that we don't at present. */
17626 case DW_TAG_array_type:
17627 case DW_TAG_file_type:
17628 case DW_TAG_ptr_to_member_type:
17629 case DW_TAG_set_type:
17630 case DW_TAG_string_type:
17631 case DW_TAG_subroutine_type:
17632 #endif
17633 case DW_TAG_base_type:
17634 case DW_TAG_class_type:
17635 case DW_TAG_interface_type:
17636 case DW_TAG_enumeration_type:
17637 case DW_TAG_structure_type:
17638 case DW_TAG_subrange_type:
17639 case DW_TAG_typedef:
17640 case DW_TAG_union_type:
17641 return 1;
17642 default:
17643 return 0;
17644 }
17645 }
17646
17647 /* Load all DIEs that are interesting for partial symbols into memory. */
17648
17649 static struct partial_die_info *
17650 load_partial_dies (const struct die_reader_specs *reader,
17651 const gdb_byte *info_ptr, int building_psymtab)
17652 {
17653 struct dwarf2_cu *cu = reader->cu;
17654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17655 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17656 unsigned int bytes_read;
17657 unsigned int load_all = 0;
17658 int nesting_level = 1;
17659
17660 parent_die = NULL;
17661 last_die = NULL;
17662
17663 gdb_assert (cu->per_cu != NULL);
17664 if (cu->per_cu->load_all_dies)
17665 load_all = 1;
17666
17667 cu->partial_dies
17668 = htab_create_alloc_ex (cu->header.length / 12,
17669 partial_die_hash,
17670 partial_die_eq,
17671 NULL,
17672 &cu->comp_unit_obstack,
17673 hashtab_obstack_allocate,
17674 dummy_obstack_deallocate);
17675
17676 while (1)
17677 {
17678 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17679
17680 /* A NULL abbrev means the end of a series of children. */
17681 if (abbrev == NULL)
17682 {
17683 if (--nesting_level == 0)
17684 return first_die;
17685
17686 info_ptr += bytes_read;
17687 last_die = parent_die;
17688 parent_die = parent_die->die_parent;
17689 continue;
17690 }
17691
17692 /* Check for template arguments. We never save these; if
17693 they're seen, we just mark the parent, and go on our way. */
17694 if (parent_die != NULL
17695 && cu->language == language_cplus
17696 && (abbrev->tag == DW_TAG_template_type_param
17697 || abbrev->tag == DW_TAG_template_value_param))
17698 {
17699 parent_die->has_template_arguments = 1;
17700
17701 if (!load_all)
17702 {
17703 /* We don't need a partial DIE for the template argument. */
17704 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17705 continue;
17706 }
17707 }
17708
17709 /* We only recurse into c++ subprograms looking for template arguments.
17710 Skip their other children. */
17711 if (!load_all
17712 && cu->language == language_cplus
17713 && parent_die != NULL
17714 && parent_die->tag == DW_TAG_subprogram)
17715 {
17716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17717 continue;
17718 }
17719
17720 /* Check whether this DIE is interesting enough to save. Normally
17721 we would not be interested in members here, but there may be
17722 later variables referencing them via DW_AT_specification (for
17723 static members). */
17724 if (!load_all
17725 && !is_type_tag_for_partial (abbrev->tag)
17726 && abbrev->tag != DW_TAG_constant
17727 && abbrev->tag != DW_TAG_enumerator
17728 && abbrev->tag != DW_TAG_subprogram
17729 && abbrev->tag != DW_TAG_inlined_subroutine
17730 && abbrev->tag != DW_TAG_lexical_block
17731 && abbrev->tag != DW_TAG_variable
17732 && abbrev->tag != DW_TAG_namespace
17733 && abbrev->tag != DW_TAG_module
17734 && abbrev->tag != DW_TAG_member
17735 && abbrev->tag != DW_TAG_imported_unit
17736 && abbrev->tag != DW_TAG_imported_declaration)
17737 {
17738 /* Otherwise we skip to the next sibling, if any. */
17739 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17740 continue;
17741 }
17742
17743 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17744 abbrev);
17745
17746 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17747
17748 /* This two-pass algorithm for processing partial symbols has a
17749 high cost in cache pressure. Thus, handle some simple cases
17750 here which cover the majority of C partial symbols. DIEs
17751 which neither have specification tags in them, nor could have
17752 specification tags elsewhere pointing at them, can simply be
17753 processed and discarded.
17754
17755 This segment is also optional; scan_partial_symbols and
17756 add_partial_symbol will handle these DIEs if we chain
17757 them in normally. When compilers which do not emit large
17758 quantities of duplicate debug information are more common,
17759 this code can probably be removed. */
17760
17761 /* Any complete simple types at the top level (pretty much all
17762 of them, for a language without namespaces), can be processed
17763 directly. */
17764 if (parent_die == NULL
17765 && pdi.has_specification == 0
17766 && pdi.is_declaration == 0
17767 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17768 || pdi.tag == DW_TAG_base_type
17769 || pdi.tag == DW_TAG_subrange_type))
17770 {
17771 if (building_psymtab && pdi.name != NULL)
17772 add_psymbol_to_list (pdi.name, false,
17773 VAR_DOMAIN, LOC_TYPEDEF, -1,
17774 psymbol_placement::STATIC,
17775 0, cu->language, objfile);
17776 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17777 continue;
17778 }
17779
17780 /* The exception for DW_TAG_typedef with has_children above is
17781 a workaround of GCC PR debug/47510. In the case of this complaint
17782 type_name_or_error will error on such types later.
17783
17784 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17785 it could not find the child DIEs referenced later, this is checked
17786 above. In correct DWARF DW_TAG_typedef should have no children. */
17787
17788 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17789 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17790 "- DIE at %s [in module %s]"),
17791 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17792
17793 /* If we're at the second level, and we're an enumerator, and
17794 our parent has no specification (meaning possibly lives in a
17795 namespace elsewhere), then we can add the partial symbol now
17796 instead of queueing it. */
17797 if (pdi.tag == DW_TAG_enumerator
17798 && parent_die != NULL
17799 && parent_die->die_parent == NULL
17800 && parent_die->tag == DW_TAG_enumeration_type
17801 && parent_die->has_specification == 0)
17802 {
17803 if (pdi.name == NULL)
17804 complaint (_("malformed enumerator DIE ignored"));
17805 else if (building_psymtab)
17806 add_psymbol_to_list (pdi.name, false,
17807 VAR_DOMAIN, LOC_CONST, -1,
17808 cu->language == language_cplus
17809 ? psymbol_placement::GLOBAL
17810 : psymbol_placement::STATIC,
17811 0, cu->language, objfile);
17812
17813 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17814 continue;
17815 }
17816
17817 struct partial_die_info *part_die
17818 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17819
17820 /* We'll save this DIE so link it in. */
17821 part_die->die_parent = parent_die;
17822 part_die->die_sibling = NULL;
17823 part_die->die_child = NULL;
17824
17825 if (last_die && last_die == parent_die)
17826 last_die->die_child = part_die;
17827 else if (last_die)
17828 last_die->die_sibling = part_die;
17829
17830 last_die = part_die;
17831
17832 if (first_die == NULL)
17833 first_die = part_die;
17834
17835 /* Maybe add the DIE to the hash table. Not all DIEs that we
17836 find interesting need to be in the hash table, because we
17837 also have the parent/sibling/child chains; only those that we
17838 might refer to by offset later during partial symbol reading.
17839
17840 For now this means things that might have be the target of a
17841 DW_AT_specification, DW_AT_abstract_origin, or
17842 DW_AT_extension. DW_AT_extension will refer only to
17843 namespaces; DW_AT_abstract_origin refers to functions (and
17844 many things under the function DIE, but we do not recurse
17845 into function DIEs during partial symbol reading) and
17846 possibly variables as well; DW_AT_specification refers to
17847 declarations. Declarations ought to have the DW_AT_declaration
17848 flag. It happens that GCC forgets to put it in sometimes, but
17849 only for functions, not for types.
17850
17851 Adding more things than necessary to the hash table is harmless
17852 except for the performance cost. Adding too few will result in
17853 wasted time in find_partial_die, when we reread the compilation
17854 unit with load_all_dies set. */
17855
17856 if (load_all
17857 || abbrev->tag == DW_TAG_constant
17858 || abbrev->tag == DW_TAG_subprogram
17859 || abbrev->tag == DW_TAG_variable
17860 || abbrev->tag == DW_TAG_namespace
17861 || part_die->is_declaration)
17862 {
17863 void **slot;
17864
17865 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17866 to_underlying (part_die->sect_off),
17867 INSERT);
17868 *slot = part_die;
17869 }
17870
17871 /* For some DIEs we want to follow their children (if any). For C
17872 we have no reason to follow the children of structures; for other
17873 languages we have to, so that we can get at method physnames
17874 to infer fully qualified class names, for DW_AT_specification,
17875 and for C++ template arguments. For C++, we also look one level
17876 inside functions to find template arguments (if the name of the
17877 function does not already contain the template arguments).
17878
17879 For Ada and Fortran, we need to scan the children of subprograms
17880 and lexical blocks as well because these languages allow the
17881 definition of nested entities that could be interesting for the
17882 debugger, such as nested subprograms for instance. */
17883 if (last_die->has_children
17884 && (load_all
17885 || last_die->tag == DW_TAG_namespace
17886 || last_die->tag == DW_TAG_module
17887 || last_die->tag == DW_TAG_enumeration_type
17888 || (cu->language == language_cplus
17889 && last_die->tag == DW_TAG_subprogram
17890 && (last_die->name == NULL
17891 || strchr (last_die->name, '<') == NULL))
17892 || (cu->language != language_c
17893 && (last_die->tag == DW_TAG_class_type
17894 || last_die->tag == DW_TAG_interface_type
17895 || last_die->tag == DW_TAG_structure_type
17896 || last_die->tag == DW_TAG_union_type))
17897 || ((cu->language == language_ada
17898 || cu->language == language_fortran)
17899 && (last_die->tag == DW_TAG_subprogram
17900 || last_die->tag == DW_TAG_lexical_block))))
17901 {
17902 nesting_level++;
17903 parent_die = last_die;
17904 continue;
17905 }
17906
17907 /* Otherwise we skip to the next sibling, if any. */
17908 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17909
17910 /* Back to the top, do it again. */
17911 }
17912 }
17913
17914 partial_die_info::partial_die_info (sect_offset sect_off_,
17915 struct abbrev_info *abbrev)
17916 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17917 {
17918 }
17919
17920 /* Read a minimal amount of information into the minimal die structure.
17921 INFO_PTR should point just after the initial uleb128 of a DIE. */
17922
17923 const gdb_byte *
17924 partial_die_info::read (const struct die_reader_specs *reader,
17925 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17926 {
17927 struct dwarf2_cu *cu = reader->cu;
17928 struct dwarf2_per_objfile *dwarf2_per_objfile
17929 = cu->per_cu->dwarf2_per_objfile;
17930 unsigned int i;
17931 int has_low_pc_attr = 0;
17932 int has_high_pc_attr = 0;
17933 int high_pc_relative = 0;
17934
17935 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17936 for (i = 0; i < abbrev.num_attrs; ++i)
17937 {
17938 bool need_reprocess;
17939 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17940 info_ptr, &need_reprocess);
17941 /* String and address offsets that need to do the reprocessing have
17942 already been read at this point, so there is no need to wait until
17943 the loop terminates to do the reprocessing. */
17944 if (need_reprocess)
17945 read_attribute_reprocess (reader, &attr_vec[i]);
17946 attribute &attr = attr_vec[i];
17947 /* Store the data if it is of an attribute we want to keep in a
17948 partial symbol table. */
17949 switch (attr.name)
17950 {
17951 case DW_AT_name:
17952 switch (tag)
17953 {
17954 case DW_TAG_compile_unit:
17955 case DW_TAG_partial_unit:
17956 case DW_TAG_type_unit:
17957 /* Compilation units have a DW_AT_name that is a filename, not
17958 a source language identifier. */
17959 case DW_TAG_enumeration_type:
17960 case DW_TAG_enumerator:
17961 /* These tags always have simple identifiers already; no need
17962 to canonicalize them. */
17963 name = DW_STRING (&attr);
17964 break;
17965 default:
17966 {
17967 struct objfile *objfile = dwarf2_per_objfile->objfile;
17968
17969 name
17970 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17971 &objfile->per_bfd->storage_obstack);
17972 }
17973 break;
17974 }
17975 break;
17976 case DW_AT_linkage_name:
17977 case DW_AT_MIPS_linkage_name:
17978 /* Note that both forms of linkage name might appear. We
17979 assume they will be the same, and we only store the last
17980 one we see. */
17981 linkage_name = DW_STRING (&attr);
17982 break;
17983 case DW_AT_low_pc:
17984 has_low_pc_attr = 1;
17985 lowpc = attr.value_as_address ();
17986 break;
17987 case DW_AT_high_pc:
17988 has_high_pc_attr = 1;
17989 highpc = attr.value_as_address ();
17990 if (cu->header.version >= 4 && attr.form_is_constant ())
17991 high_pc_relative = 1;
17992 break;
17993 case DW_AT_location:
17994 /* Support the .debug_loc offsets. */
17995 if (attr.form_is_block ())
17996 {
17997 d.locdesc = DW_BLOCK (&attr);
17998 }
17999 else if (attr.form_is_section_offset ())
18000 {
18001 dwarf2_complex_location_expr_complaint ();
18002 }
18003 else
18004 {
18005 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18006 "partial symbol information");
18007 }
18008 break;
18009 case DW_AT_external:
18010 is_external = DW_UNSND (&attr);
18011 break;
18012 case DW_AT_declaration:
18013 is_declaration = DW_UNSND (&attr);
18014 break;
18015 case DW_AT_type:
18016 has_type = 1;
18017 break;
18018 case DW_AT_abstract_origin:
18019 case DW_AT_specification:
18020 case DW_AT_extension:
18021 has_specification = 1;
18022 spec_offset = dwarf2_get_ref_die_offset (&attr);
18023 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18024 || cu->per_cu->is_dwz);
18025 break;
18026 case DW_AT_sibling:
18027 /* Ignore absolute siblings, they might point outside of
18028 the current compile unit. */
18029 if (attr.form == DW_FORM_ref_addr)
18030 complaint (_("ignoring absolute DW_AT_sibling"));
18031 else
18032 {
18033 const gdb_byte *buffer = reader->buffer;
18034 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18035 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18036
18037 if (sibling_ptr < info_ptr)
18038 complaint (_("DW_AT_sibling points backwards"));
18039 else if (sibling_ptr > reader->buffer_end)
18040 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18041 else
18042 sibling = sibling_ptr;
18043 }
18044 break;
18045 case DW_AT_byte_size:
18046 has_byte_size = 1;
18047 break;
18048 case DW_AT_const_value:
18049 has_const_value = 1;
18050 break;
18051 case DW_AT_calling_convention:
18052 /* DWARF doesn't provide a way to identify a program's source-level
18053 entry point. DW_AT_calling_convention attributes are only meant
18054 to describe functions' calling conventions.
18055
18056 However, because it's a necessary piece of information in
18057 Fortran, and before DWARF 4 DW_CC_program was the only
18058 piece of debugging information whose definition refers to
18059 a 'main program' at all, several compilers marked Fortran
18060 main programs with DW_CC_program --- even when those
18061 functions use the standard calling conventions.
18062
18063 Although DWARF now specifies a way to provide this
18064 information, we support this practice for backward
18065 compatibility. */
18066 if (DW_UNSND (&attr) == DW_CC_program
18067 && cu->language == language_fortran)
18068 main_subprogram = 1;
18069 break;
18070 case DW_AT_inline:
18071 if (DW_UNSND (&attr) == DW_INL_inlined
18072 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18073 may_be_inlined = 1;
18074 break;
18075
18076 case DW_AT_import:
18077 if (tag == DW_TAG_imported_unit)
18078 {
18079 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18080 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18081 || cu->per_cu->is_dwz);
18082 }
18083 break;
18084
18085 case DW_AT_main_subprogram:
18086 main_subprogram = DW_UNSND (&attr);
18087 break;
18088
18089 case DW_AT_ranges:
18090 {
18091 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18092 but that requires a full DIE, so instead we just
18093 reimplement it. */
18094 int need_ranges_base = tag != DW_TAG_compile_unit;
18095 unsigned int ranges_offset = (DW_UNSND (&attr)
18096 + (need_ranges_base
18097 ? cu->ranges_base
18098 : 0));
18099
18100 /* Value of the DW_AT_ranges attribute is the offset in the
18101 .debug_ranges section. */
18102 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18103 nullptr))
18104 has_pc_info = 1;
18105 }
18106 break;
18107
18108 default:
18109 break;
18110 }
18111 }
18112
18113 /* For Ada, if both the name and the linkage name appear, we prefer
18114 the latter. This lets "catch exception" work better, regardless
18115 of the order in which the name and linkage name were emitted.
18116 Really, though, this is just a workaround for the fact that gdb
18117 doesn't store both the name and the linkage name. */
18118 if (cu->language == language_ada && linkage_name != nullptr)
18119 name = linkage_name;
18120
18121 if (high_pc_relative)
18122 highpc += lowpc;
18123
18124 if (has_low_pc_attr && has_high_pc_attr)
18125 {
18126 /* When using the GNU linker, .gnu.linkonce. sections are used to
18127 eliminate duplicate copies of functions and vtables and such.
18128 The linker will arbitrarily choose one and discard the others.
18129 The AT_*_pc values for such functions refer to local labels in
18130 these sections. If the section from that file was discarded, the
18131 labels are not in the output, so the relocs get a value of 0.
18132 If this is a discarded function, mark the pc bounds as invalid,
18133 so that GDB will ignore it. */
18134 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18135 {
18136 struct objfile *objfile = dwarf2_per_objfile->objfile;
18137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18138
18139 complaint (_("DW_AT_low_pc %s is zero "
18140 "for DIE at %s [in module %s]"),
18141 paddress (gdbarch, lowpc),
18142 sect_offset_str (sect_off),
18143 objfile_name (objfile));
18144 }
18145 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18146 else if (lowpc >= highpc)
18147 {
18148 struct objfile *objfile = dwarf2_per_objfile->objfile;
18149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18150
18151 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18152 "for DIE at %s [in module %s]"),
18153 paddress (gdbarch, lowpc),
18154 paddress (gdbarch, highpc),
18155 sect_offset_str (sect_off),
18156 objfile_name (objfile));
18157 }
18158 else
18159 has_pc_info = 1;
18160 }
18161
18162 return info_ptr;
18163 }
18164
18165 /* Find a cached partial DIE at OFFSET in CU. */
18166
18167 struct partial_die_info *
18168 dwarf2_cu::find_partial_die (sect_offset sect_off)
18169 {
18170 struct partial_die_info *lookup_die = NULL;
18171 struct partial_die_info part_die (sect_off);
18172
18173 lookup_die = ((struct partial_die_info *)
18174 htab_find_with_hash (partial_dies, &part_die,
18175 to_underlying (sect_off)));
18176
18177 return lookup_die;
18178 }
18179
18180 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18181 except in the case of .debug_types DIEs which do not reference
18182 outside their CU (they do however referencing other types via
18183 DW_FORM_ref_sig8). */
18184
18185 static const struct cu_partial_die_info
18186 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18187 {
18188 struct dwarf2_per_objfile *dwarf2_per_objfile
18189 = cu->per_cu->dwarf2_per_objfile;
18190 struct objfile *objfile = dwarf2_per_objfile->objfile;
18191 struct dwarf2_per_cu_data *per_cu = NULL;
18192 struct partial_die_info *pd = NULL;
18193
18194 if (offset_in_dwz == cu->per_cu->is_dwz
18195 && cu->header.offset_in_cu_p (sect_off))
18196 {
18197 pd = cu->find_partial_die (sect_off);
18198 if (pd != NULL)
18199 return { cu, pd };
18200 /* We missed recording what we needed.
18201 Load all dies and try again. */
18202 per_cu = cu->per_cu;
18203 }
18204 else
18205 {
18206 /* TUs don't reference other CUs/TUs (except via type signatures). */
18207 if (cu->per_cu->is_debug_types)
18208 {
18209 error (_("Dwarf Error: Type Unit at offset %s contains"
18210 " external reference to offset %s [in module %s].\n"),
18211 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18212 bfd_get_filename (objfile->obfd));
18213 }
18214 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18215 dwarf2_per_objfile);
18216
18217 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18218 load_partial_comp_unit (per_cu);
18219
18220 per_cu->cu->last_used = 0;
18221 pd = per_cu->cu->find_partial_die (sect_off);
18222 }
18223
18224 /* If we didn't find it, and not all dies have been loaded,
18225 load them all and try again. */
18226
18227 if (pd == NULL && per_cu->load_all_dies == 0)
18228 {
18229 per_cu->load_all_dies = 1;
18230
18231 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18232 THIS_CU->cu may already be in use. So we can't just free it and
18233 replace its DIEs with the ones we read in. Instead, we leave those
18234 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18235 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18236 set. */
18237 load_partial_comp_unit (per_cu);
18238
18239 pd = per_cu->cu->find_partial_die (sect_off);
18240 }
18241
18242 if (pd == NULL)
18243 internal_error (__FILE__, __LINE__,
18244 _("could not find partial DIE %s "
18245 "in cache [from module %s]\n"),
18246 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18247 return { per_cu->cu, pd };
18248 }
18249
18250 /* See if we can figure out if the class lives in a namespace. We do
18251 this by looking for a member function; its demangled name will
18252 contain namespace info, if there is any. */
18253
18254 static void
18255 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18256 struct dwarf2_cu *cu)
18257 {
18258 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18259 what template types look like, because the demangler
18260 frequently doesn't give the same name as the debug info. We
18261 could fix this by only using the demangled name to get the
18262 prefix (but see comment in read_structure_type). */
18263
18264 struct partial_die_info *real_pdi;
18265 struct partial_die_info *child_pdi;
18266
18267 /* If this DIE (this DIE's specification, if any) has a parent, then
18268 we should not do this. We'll prepend the parent's fully qualified
18269 name when we create the partial symbol. */
18270
18271 real_pdi = struct_pdi;
18272 while (real_pdi->has_specification)
18273 {
18274 auto res = find_partial_die (real_pdi->spec_offset,
18275 real_pdi->spec_is_dwz, cu);
18276 real_pdi = res.pdi;
18277 cu = res.cu;
18278 }
18279
18280 if (real_pdi->die_parent != NULL)
18281 return;
18282
18283 for (child_pdi = struct_pdi->die_child;
18284 child_pdi != NULL;
18285 child_pdi = child_pdi->die_sibling)
18286 {
18287 if (child_pdi->tag == DW_TAG_subprogram
18288 && child_pdi->linkage_name != NULL)
18289 {
18290 gdb::unique_xmalloc_ptr<char> actual_class_name
18291 (language_class_name_from_physname (cu->language_defn,
18292 child_pdi->linkage_name));
18293 if (actual_class_name != NULL)
18294 {
18295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18296 struct_pdi->name
18297 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18298 actual_class_name.get ());
18299 }
18300 break;
18301 }
18302 }
18303 }
18304
18305 void
18306 partial_die_info::fixup (struct dwarf2_cu *cu)
18307 {
18308 /* Once we've fixed up a die, there's no point in doing so again.
18309 This also avoids a memory leak if we were to call
18310 guess_partial_die_structure_name multiple times. */
18311 if (fixup_called)
18312 return;
18313
18314 /* If we found a reference attribute and the DIE has no name, try
18315 to find a name in the referred to DIE. */
18316
18317 if (name == NULL && has_specification)
18318 {
18319 struct partial_die_info *spec_die;
18320
18321 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18322 spec_die = res.pdi;
18323 cu = res.cu;
18324
18325 spec_die->fixup (cu);
18326
18327 if (spec_die->name)
18328 {
18329 name = spec_die->name;
18330
18331 /* Copy DW_AT_external attribute if it is set. */
18332 if (spec_die->is_external)
18333 is_external = spec_die->is_external;
18334 }
18335 }
18336
18337 /* Set default names for some unnamed DIEs. */
18338
18339 if (name == NULL && tag == DW_TAG_namespace)
18340 name = CP_ANONYMOUS_NAMESPACE_STR;
18341
18342 /* If there is no parent die to provide a namespace, and there are
18343 children, see if we can determine the namespace from their linkage
18344 name. */
18345 if (cu->language == language_cplus
18346 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18347 && die_parent == NULL
18348 && has_children
18349 && (tag == DW_TAG_class_type
18350 || tag == DW_TAG_structure_type
18351 || tag == DW_TAG_union_type))
18352 guess_partial_die_structure_name (this, cu);
18353
18354 /* GCC might emit a nameless struct or union that has a linkage
18355 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18356 if (name == NULL
18357 && (tag == DW_TAG_class_type
18358 || tag == DW_TAG_interface_type
18359 || tag == DW_TAG_structure_type
18360 || tag == DW_TAG_union_type)
18361 && linkage_name != NULL)
18362 {
18363 gdb::unique_xmalloc_ptr<char> demangled
18364 (gdb_demangle (linkage_name, DMGL_TYPES));
18365 if (demangled != nullptr)
18366 {
18367 const char *base;
18368
18369 /* Strip any leading namespaces/classes, keep only the base name.
18370 DW_AT_name for named DIEs does not contain the prefixes. */
18371 base = strrchr (demangled.get (), ':');
18372 if (base && base > demangled.get () && base[-1] == ':')
18373 base++;
18374 else
18375 base = demangled.get ();
18376
18377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18378 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18379 }
18380 }
18381
18382 fixup_called = 1;
18383 }
18384
18385 /* Process the attributes that had to be skipped in the first round. These
18386 attributes are the ones that need str_offsets_base or addr_base attributes.
18387 They could not have been processed in the first round, because at the time
18388 the values of str_offsets_base or addr_base may not have been known. */
18389 void read_attribute_reprocess (const struct die_reader_specs *reader,
18390 struct attribute *attr)
18391 {
18392 struct dwarf2_cu *cu = reader->cu;
18393 switch (attr->form)
18394 {
18395 case DW_FORM_addrx:
18396 case DW_FORM_GNU_addr_index:
18397 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18398 break;
18399 case DW_FORM_strx:
18400 case DW_FORM_strx1:
18401 case DW_FORM_strx2:
18402 case DW_FORM_strx3:
18403 case DW_FORM_strx4:
18404 case DW_FORM_GNU_str_index:
18405 {
18406 unsigned int str_index = DW_UNSND (attr);
18407 if (reader->dwo_file != NULL)
18408 {
18409 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18410 DW_STRING_IS_CANONICAL (attr) = 0;
18411 }
18412 else
18413 {
18414 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18415 DW_STRING_IS_CANONICAL (attr) = 0;
18416 }
18417 break;
18418 }
18419 default:
18420 gdb_assert_not_reached (_("Unexpected DWARF form."));
18421 }
18422 }
18423
18424 /* Read an attribute value described by an attribute form. */
18425
18426 static const gdb_byte *
18427 read_attribute_value (const struct die_reader_specs *reader,
18428 struct attribute *attr, unsigned form,
18429 LONGEST implicit_const, const gdb_byte *info_ptr,
18430 bool *need_reprocess)
18431 {
18432 struct dwarf2_cu *cu = reader->cu;
18433 struct dwarf2_per_objfile *dwarf2_per_objfile
18434 = cu->per_cu->dwarf2_per_objfile;
18435 struct objfile *objfile = dwarf2_per_objfile->objfile;
18436 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18437 bfd *abfd = reader->abfd;
18438 struct comp_unit_head *cu_header = &cu->header;
18439 unsigned int bytes_read;
18440 struct dwarf_block *blk;
18441 *need_reprocess = false;
18442
18443 attr->form = (enum dwarf_form) form;
18444 switch (form)
18445 {
18446 case DW_FORM_ref_addr:
18447 if (cu->header.version == 2)
18448 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18449 &bytes_read);
18450 else
18451 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18452 &bytes_read);
18453 info_ptr += bytes_read;
18454 break;
18455 case DW_FORM_GNU_ref_alt:
18456 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18457 info_ptr += bytes_read;
18458 break;
18459 case DW_FORM_addr:
18460 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18461 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18462 info_ptr += bytes_read;
18463 break;
18464 case DW_FORM_block2:
18465 blk = dwarf_alloc_block (cu);
18466 blk->size = read_2_bytes (abfd, info_ptr);
18467 info_ptr += 2;
18468 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18469 info_ptr += blk->size;
18470 DW_BLOCK (attr) = blk;
18471 break;
18472 case DW_FORM_block4:
18473 blk = dwarf_alloc_block (cu);
18474 blk->size = read_4_bytes (abfd, info_ptr);
18475 info_ptr += 4;
18476 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18477 info_ptr += blk->size;
18478 DW_BLOCK (attr) = blk;
18479 break;
18480 case DW_FORM_data2:
18481 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18482 info_ptr += 2;
18483 break;
18484 case DW_FORM_data4:
18485 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18486 info_ptr += 4;
18487 break;
18488 case DW_FORM_data8:
18489 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18490 info_ptr += 8;
18491 break;
18492 case DW_FORM_data16:
18493 blk = dwarf_alloc_block (cu);
18494 blk->size = 16;
18495 blk->data = read_n_bytes (abfd, info_ptr, 16);
18496 info_ptr += 16;
18497 DW_BLOCK (attr) = blk;
18498 break;
18499 case DW_FORM_sec_offset:
18500 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18501 info_ptr += bytes_read;
18502 break;
18503 case DW_FORM_string:
18504 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18505 DW_STRING_IS_CANONICAL (attr) = 0;
18506 info_ptr += bytes_read;
18507 break;
18508 case DW_FORM_strp:
18509 if (!cu->per_cu->is_dwz)
18510 {
18511 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18512 abfd, info_ptr, cu_header,
18513 &bytes_read);
18514 DW_STRING_IS_CANONICAL (attr) = 0;
18515 info_ptr += bytes_read;
18516 break;
18517 }
18518 /* FALLTHROUGH */
18519 case DW_FORM_line_strp:
18520 if (!cu->per_cu->is_dwz)
18521 {
18522 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18523 abfd, info_ptr,
18524 cu_header, &bytes_read);
18525 DW_STRING_IS_CANONICAL (attr) = 0;
18526 info_ptr += bytes_read;
18527 break;
18528 }
18529 /* FALLTHROUGH */
18530 case DW_FORM_GNU_strp_alt:
18531 {
18532 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18533 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18534 &bytes_read);
18535
18536 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18537 dwz, str_offset);
18538 DW_STRING_IS_CANONICAL (attr) = 0;
18539 info_ptr += bytes_read;
18540 }
18541 break;
18542 case DW_FORM_exprloc:
18543 case DW_FORM_block:
18544 blk = dwarf_alloc_block (cu);
18545 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18546 info_ptr += bytes_read;
18547 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18548 info_ptr += blk->size;
18549 DW_BLOCK (attr) = blk;
18550 break;
18551 case DW_FORM_block1:
18552 blk = dwarf_alloc_block (cu);
18553 blk->size = read_1_byte (abfd, info_ptr);
18554 info_ptr += 1;
18555 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18556 info_ptr += blk->size;
18557 DW_BLOCK (attr) = blk;
18558 break;
18559 case DW_FORM_data1:
18560 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18561 info_ptr += 1;
18562 break;
18563 case DW_FORM_flag:
18564 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18565 info_ptr += 1;
18566 break;
18567 case DW_FORM_flag_present:
18568 DW_UNSND (attr) = 1;
18569 break;
18570 case DW_FORM_sdata:
18571 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 break;
18574 case DW_FORM_udata:
18575 case DW_FORM_rnglistx:
18576 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18577 info_ptr += bytes_read;
18578 break;
18579 case DW_FORM_ref1:
18580 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18581 + read_1_byte (abfd, info_ptr));
18582 info_ptr += 1;
18583 break;
18584 case DW_FORM_ref2:
18585 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18586 + read_2_bytes (abfd, info_ptr));
18587 info_ptr += 2;
18588 break;
18589 case DW_FORM_ref4:
18590 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18591 + read_4_bytes (abfd, info_ptr));
18592 info_ptr += 4;
18593 break;
18594 case DW_FORM_ref8:
18595 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18596 + read_8_bytes (abfd, info_ptr));
18597 info_ptr += 8;
18598 break;
18599 case DW_FORM_ref_sig8:
18600 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18601 info_ptr += 8;
18602 break;
18603 case DW_FORM_ref_udata:
18604 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18605 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18606 info_ptr += bytes_read;
18607 break;
18608 case DW_FORM_indirect:
18609 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18610 info_ptr += bytes_read;
18611 if (form == DW_FORM_implicit_const)
18612 {
18613 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18614 info_ptr += bytes_read;
18615 }
18616 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18617 info_ptr, need_reprocess);
18618 break;
18619 case DW_FORM_implicit_const:
18620 DW_SND (attr) = implicit_const;
18621 break;
18622 case DW_FORM_addrx:
18623 case DW_FORM_GNU_addr_index:
18624 *need_reprocess = true;
18625 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18626 info_ptr += bytes_read;
18627 break;
18628 case DW_FORM_strx:
18629 case DW_FORM_strx1:
18630 case DW_FORM_strx2:
18631 case DW_FORM_strx3:
18632 case DW_FORM_strx4:
18633 case DW_FORM_GNU_str_index:
18634 {
18635 ULONGEST str_index;
18636 if (form == DW_FORM_strx1)
18637 {
18638 str_index = read_1_byte (abfd, info_ptr);
18639 info_ptr += 1;
18640 }
18641 else if (form == DW_FORM_strx2)
18642 {
18643 str_index = read_2_bytes (abfd, info_ptr);
18644 info_ptr += 2;
18645 }
18646 else if (form == DW_FORM_strx3)
18647 {
18648 str_index = read_3_bytes (abfd, info_ptr);
18649 info_ptr += 3;
18650 }
18651 else if (form == DW_FORM_strx4)
18652 {
18653 str_index = read_4_bytes (abfd, info_ptr);
18654 info_ptr += 4;
18655 }
18656 else
18657 {
18658 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18659 info_ptr += bytes_read;
18660 }
18661 *need_reprocess = true;
18662 DW_UNSND (attr) = str_index;
18663 }
18664 break;
18665 default:
18666 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18667 dwarf_form_name (form),
18668 bfd_get_filename (abfd));
18669 }
18670
18671 /* Super hack. */
18672 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18673 attr->form = DW_FORM_GNU_ref_alt;
18674
18675 /* We have seen instances where the compiler tried to emit a byte
18676 size attribute of -1 which ended up being encoded as an unsigned
18677 0xffffffff. Although 0xffffffff is technically a valid size value,
18678 an object of this size seems pretty unlikely so we can relatively
18679 safely treat these cases as if the size attribute was invalid and
18680 treat them as zero by default. */
18681 if (attr->name == DW_AT_byte_size
18682 && form == DW_FORM_data4
18683 && DW_UNSND (attr) >= 0xffffffff)
18684 {
18685 complaint
18686 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18687 hex_string (DW_UNSND (attr)));
18688 DW_UNSND (attr) = 0;
18689 }
18690
18691 return info_ptr;
18692 }
18693
18694 /* Read an attribute described by an abbreviated attribute. */
18695
18696 static const gdb_byte *
18697 read_attribute (const struct die_reader_specs *reader,
18698 struct attribute *attr, struct attr_abbrev *abbrev,
18699 const gdb_byte *info_ptr, bool *need_reprocess)
18700 {
18701 attr->name = abbrev->name;
18702 return read_attribute_value (reader, attr, abbrev->form,
18703 abbrev->implicit_const, info_ptr,
18704 need_reprocess);
18705 }
18706
18707 /* Cover function for read_initial_length.
18708 Returns the length of the object at BUF, and stores the size of the
18709 initial length in *BYTES_READ and stores the size that offsets will be in
18710 *OFFSET_SIZE.
18711 If the initial length size is not equivalent to that specified in
18712 CU_HEADER then issue a complaint.
18713 This is useful when reading non-comp-unit headers. */
18714
18715 static LONGEST
18716 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18717 const struct comp_unit_head *cu_header,
18718 unsigned int *bytes_read,
18719 unsigned int *offset_size)
18720 {
18721 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18722
18723 gdb_assert (cu_header->initial_length_size == 4
18724 || cu_header->initial_length_size == 8
18725 || cu_header->initial_length_size == 12);
18726
18727 if (cu_header->initial_length_size != *bytes_read)
18728 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18729
18730 *offset_size = (*bytes_read == 4) ? 4 : 8;
18731 return length;
18732 }
18733
18734 /* Return pointer to string at section SECT offset STR_OFFSET with error
18735 reporting strings FORM_NAME and SECT_NAME. */
18736
18737 static const char *
18738 read_indirect_string_at_offset_from (struct objfile *objfile,
18739 bfd *abfd, LONGEST str_offset,
18740 struct dwarf2_section_info *sect,
18741 const char *form_name,
18742 const char *sect_name)
18743 {
18744 sect->read (objfile);
18745 if (sect->buffer == NULL)
18746 error (_("%s used without %s section [in module %s]"),
18747 form_name, sect_name, bfd_get_filename (abfd));
18748 if (str_offset >= sect->size)
18749 error (_("%s pointing outside of %s section [in module %s]"),
18750 form_name, sect_name, bfd_get_filename (abfd));
18751 gdb_assert (HOST_CHAR_BIT == 8);
18752 if (sect->buffer[str_offset] == '\0')
18753 return NULL;
18754 return (const char *) (sect->buffer + str_offset);
18755 }
18756
18757 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18758
18759 static const char *
18760 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18761 bfd *abfd, LONGEST str_offset)
18762 {
18763 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18764 abfd, str_offset,
18765 &dwarf2_per_objfile->str,
18766 "DW_FORM_strp", ".debug_str");
18767 }
18768
18769 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18770
18771 static const char *
18772 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18773 bfd *abfd, LONGEST str_offset)
18774 {
18775 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18776 abfd, str_offset,
18777 &dwarf2_per_objfile->line_str,
18778 "DW_FORM_line_strp",
18779 ".debug_line_str");
18780 }
18781
18782 /* Read a string at offset STR_OFFSET in the .debug_str section from
18783 the .dwz file DWZ. Throw an error if the offset is too large. If
18784 the string consists of a single NUL byte, return NULL; otherwise
18785 return a pointer to the string. */
18786
18787 static const char *
18788 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18789 LONGEST str_offset)
18790 {
18791 dwz->str.read (objfile);
18792
18793 if (dwz->str.buffer == NULL)
18794 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18795 "section [in module %s]"),
18796 bfd_get_filename (dwz->dwz_bfd.get ()));
18797 if (str_offset >= dwz->str.size)
18798 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18799 ".debug_str section [in module %s]"),
18800 bfd_get_filename (dwz->dwz_bfd.get ()));
18801 gdb_assert (HOST_CHAR_BIT == 8);
18802 if (dwz->str.buffer[str_offset] == '\0')
18803 return NULL;
18804 return (const char *) (dwz->str.buffer + str_offset);
18805 }
18806
18807 /* Return pointer to string at .debug_str offset as read from BUF.
18808 BUF is assumed to be in a compilation unit described by CU_HEADER.
18809 Return *BYTES_READ_PTR count of bytes read from BUF. */
18810
18811 static const char *
18812 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18813 const gdb_byte *buf,
18814 const struct comp_unit_head *cu_header,
18815 unsigned int *bytes_read_ptr)
18816 {
18817 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18818
18819 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18820 }
18821
18822 /* Return pointer to string at .debug_line_str offset as read from BUF.
18823 BUF is assumed to be in a compilation unit described by CU_HEADER.
18824 Return *BYTES_READ_PTR count of bytes read from BUF. */
18825
18826 static const char *
18827 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18828 bfd *abfd, const gdb_byte *buf,
18829 const struct comp_unit_head *cu_header,
18830 unsigned int *bytes_read_ptr)
18831 {
18832 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18833
18834 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18835 str_offset);
18836 }
18837
18838 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18839 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18840 ADDR_SIZE is the size of addresses from the CU header. */
18841
18842 static CORE_ADDR
18843 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18844 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18845 int addr_size)
18846 {
18847 struct objfile *objfile = dwarf2_per_objfile->objfile;
18848 bfd *abfd = objfile->obfd;
18849 const gdb_byte *info_ptr;
18850 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18851
18852 dwarf2_per_objfile->addr.read (objfile);
18853 if (dwarf2_per_objfile->addr.buffer == NULL)
18854 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18855 objfile_name (objfile));
18856 if (addr_base_or_zero + addr_index * addr_size
18857 >= dwarf2_per_objfile->addr.size)
18858 error (_("DW_FORM_addr_index pointing outside of "
18859 ".debug_addr section [in module %s]"),
18860 objfile_name (objfile));
18861 info_ptr = (dwarf2_per_objfile->addr.buffer
18862 + addr_base_or_zero + addr_index * addr_size);
18863 if (addr_size == 4)
18864 return bfd_get_32 (abfd, info_ptr);
18865 else
18866 return bfd_get_64 (abfd, info_ptr);
18867 }
18868
18869 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18870
18871 static CORE_ADDR
18872 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18873 {
18874 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18875 cu->addr_base, cu->header.addr_size);
18876 }
18877
18878 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18879
18880 static CORE_ADDR
18881 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18882 unsigned int *bytes_read)
18883 {
18884 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18885 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18886
18887 return read_addr_index (cu, addr_index);
18888 }
18889
18890 /* Given an index in .debug_addr, fetch the value.
18891 NOTE: This can be called during dwarf expression evaluation,
18892 long after the debug information has been read, and thus per_cu->cu
18893 may no longer exist. */
18894
18895 CORE_ADDR
18896 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18897 unsigned int addr_index)
18898 {
18899 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18900 struct dwarf2_cu *cu = per_cu->cu;
18901 gdb::optional<ULONGEST> addr_base;
18902 int addr_size;
18903
18904 /* We need addr_base and addr_size.
18905 If we don't have PER_CU->cu, we have to get it.
18906 Nasty, but the alternative is storing the needed info in PER_CU,
18907 which at this point doesn't seem justified: it's not clear how frequently
18908 it would get used and it would increase the size of every PER_CU.
18909 Entry points like dwarf2_per_cu_addr_size do a similar thing
18910 so we're not in uncharted territory here.
18911 Alas we need to be a bit more complicated as addr_base is contained
18912 in the DIE.
18913
18914 We don't need to read the entire CU(/TU).
18915 We just need the header and top level die.
18916
18917 IWBN to use the aging mechanism to let us lazily later discard the CU.
18918 For now we skip this optimization. */
18919
18920 if (cu != NULL)
18921 {
18922 addr_base = cu->addr_base;
18923 addr_size = cu->header.addr_size;
18924 }
18925 else
18926 {
18927 cutu_reader reader (per_cu, NULL, 0, false);
18928 addr_base = reader.cu->addr_base;
18929 addr_size = reader.cu->header.addr_size;
18930 }
18931
18932 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18933 addr_size);
18934 }
18935
18936 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18937 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18938 DWO file. */
18939
18940 static const char *
18941 read_str_index (struct dwarf2_cu *cu,
18942 struct dwarf2_section_info *str_section,
18943 struct dwarf2_section_info *str_offsets_section,
18944 ULONGEST str_offsets_base, ULONGEST str_index)
18945 {
18946 struct dwarf2_per_objfile *dwarf2_per_objfile
18947 = cu->per_cu->dwarf2_per_objfile;
18948 struct objfile *objfile = dwarf2_per_objfile->objfile;
18949 const char *objf_name = objfile_name (objfile);
18950 bfd *abfd = objfile->obfd;
18951 const gdb_byte *info_ptr;
18952 ULONGEST str_offset;
18953 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18954
18955 str_section->read (objfile);
18956 str_offsets_section->read (objfile);
18957 if (str_section->buffer == NULL)
18958 error (_("%s used without %s section"
18959 " in CU at offset %s [in module %s]"),
18960 form_name, str_section->get_name (),
18961 sect_offset_str (cu->header.sect_off), objf_name);
18962 if (str_offsets_section->buffer == NULL)
18963 error (_("%s used without %s section"
18964 " in CU at offset %s [in module %s]"),
18965 form_name, str_section->get_name (),
18966 sect_offset_str (cu->header.sect_off), objf_name);
18967 info_ptr = (str_offsets_section->buffer
18968 + str_offsets_base
18969 + str_index * cu->header.offset_size);
18970 if (cu->header.offset_size == 4)
18971 str_offset = bfd_get_32 (abfd, info_ptr);
18972 else
18973 str_offset = bfd_get_64 (abfd, info_ptr);
18974 if (str_offset >= str_section->size)
18975 error (_("Offset from %s pointing outside of"
18976 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18977 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18978 return (const char *) (str_section->buffer + str_offset);
18979 }
18980
18981 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18982
18983 static const char *
18984 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18985 {
18986 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18987 ? reader->cu->header.addr_size : 0;
18988 return read_str_index (reader->cu,
18989 &reader->dwo_file->sections.str,
18990 &reader->dwo_file->sections.str_offsets,
18991 str_offsets_base, str_index);
18992 }
18993
18994 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18995
18996 static const char *
18997 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18998 {
18999 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19000 const char *objf_name = objfile_name (objfile);
19001 static const char form_name[] = "DW_FORM_GNU_str_index";
19002 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19003
19004 if (!cu->str_offsets_base.has_value ())
19005 error (_("%s used in Fission stub without %s"
19006 " in CU at offset 0x%lx [in module %s]"),
19007 form_name, str_offsets_attr_name,
19008 (long) cu->header.offset_size, objf_name);
19009
19010 return read_str_index (cu,
19011 &cu->per_cu->dwarf2_per_objfile->str,
19012 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19013 *cu->str_offsets_base, str_index);
19014 }
19015
19016 /* Return the length of an LEB128 number in BUF. */
19017
19018 static int
19019 leb128_size (const gdb_byte *buf)
19020 {
19021 const gdb_byte *begin = buf;
19022 gdb_byte byte;
19023
19024 while (1)
19025 {
19026 byte = *buf++;
19027 if ((byte & 128) == 0)
19028 return buf - begin;
19029 }
19030 }
19031
19032 static void
19033 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19034 {
19035 switch (lang)
19036 {
19037 case DW_LANG_C89:
19038 case DW_LANG_C99:
19039 case DW_LANG_C11:
19040 case DW_LANG_C:
19041 case DW_LANG_UPC:
19042 cu->language = language_c;
19043 break;
19044 case DW_LANG_Java:
19045 case DW_LANG_C_plus_plus:
19046 case DW_LANG_C_plus_plus_11:
19047 case DW_LANG_C_plus_plus_14:
19048 cu->language = language_cplus;
19049 break;
19050 case DW_LANG_D:
19051 cu->language = language_d;
19052 break;
19053 case DW_LANG_Fortran77:
19054 case DW_LANG_Fortran90:
19055 case DW_LANG_Fortran95:
19056 case DW_LANG_Fortran03:
19057 case DW_LANG_Fortran08:
19058 cu->language = language_fortran;
19059 break;
19060 case DW_LANG_Go:
19061 cu->language = language_go;
19062 break;
19063 case DW_LANG_Mips_Assembler:
19064 cu->language = language_asm;
19065 break;
19066 case DW_LANG_Ada83:
19067 case DW_LANG_Ada95:
19068 cu->language = language_ada;
19069 break;
19070 case DW_LANG_Modula2:
19071 cu->language = language_m2;
19072 break;
19073 case DW_LANG_Pascal83:
19074 cu->language = language_pascal;
19075 break;
19076 case DW_LANG_ObjC:
19077 cu->language = language_objc;
19078 break;
19079 case DW_LANG_Rust:
19080 case DW_LANG_Rust_old:
19081 cu->language = language_rust;
19082 break;
19083 case DW_LANG_Cobol74:
19084 case DW_LANG_Cobol85:
19085 default:
19086 cu->language = language_minimal;
19087 break;
19088 }
19089 cu->language_defn = language_def (cu->language);
19090 }
19091
19092 /* Return the named attribute or NULL if not there. */
19093
19094 static struct attribute *
19095 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19096 {
19097 for (;;)
19098 {
19099 unsigned int i;
19100 struct attribute *spec = NULL;
19101
19102 for (i = 0; i < die->num_attrs; ++i)
19103 {
19104 if (die->attrs[i].name == name)
19105 return &die->attrs[i];
19106 if (die->attrs[i].name == DW_AT_specification
19107 || die->attrs[i].name == DW_AT_abstract_origin)
19108 spec = &die->attrs[i];
19109 }
19110
19111 if (!spec)
19112 break;
19113
19114 die = follow_die_ref (die, spec, &cu);
19115 }
19116
19117 return NULL;
19118 }
19119
19120 /* Return the named attribute or NULL if not there,
19121 but do not follow DW_AT_specification, etc.
19122 This is for use in contexts where we're reading .debug_types dies.
19123 Following DW_AT_specification, DW_AT_abstract_origin will take us
19124 back up the chain, and we want to go down. */
19125
19126 static struct attribute *
19127 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19128 {
19129 unsigned int i;
19130
19131 for (i = 0; i < die->num_attrs; ++i)
19132 if (die->attrs[i].name == name)
19133 return &die->attrs[i];
19134
19135 return NULL;
19136 }
19137
19138 /* Return the string associated with a string-typed attribute, or NULL if it
19139 is either not found or is of an incorrect type. */
19140
19141 static const char *
19142 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19143 {
19144 struct attribute *attr;
19145 const char *str = NULL;
19146
19147 attr = dwarf2_attr (die, name, cu);
19148
19149 if (attr != NULL)
19150 {
19151 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19152 || attr->form == DW_FORM_string
19153 || attr->form == DW_FORM_strx
19154 || attr->form == DW_FORM_strx1
19155 || attr->form == DW_FORM_strx2
19156 || attr->form == DW_FORM_strx3
19157 || attr->form == DW_FORM_strx4
19158 || attr->form == DW_FORM_GNU_str_index
19159 || attr->form == DW_FORM_GNU_strp_alt)
19160 str = DW_STRING (attr);
19161 else
19162 complaint (_("string type expected for attribute %s for "
19163 "DIE at %s in module %s"),
19164 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19165 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19166 }
19167
19168 return str;
19169 }
19170
19171 /* Return the dwo name or NULL if not present. If present, it is in either
19172 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19173 static const char *
19174 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19175 {
19176 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19177 if (dwo_name == nullptr)
19178 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19179 return dwo_name;
19180 }
19181
19182 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19183 and holds a non-zero value. This function should only be used for
19184 DW_FORM_flag or DW_FORM_flag_present attributes. */
19185
19186 static int
19187 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19188 {
19189 struct attribute *attr = dwarf2_attr (die, name, cu);
19190
19191 return (attr && DW_UNSND (attr));
19192 }
19193
19194 static int
19195 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19196 {
19197 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19198 which value is non-zero. However, we have to be careful with
19199 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19200 (via dwarf2_flag_true_p) follows this attribute. So we may
19201 end up accidently finding a declaration attribute that belongs
19202 to a different DIE referenced by the specification attribute,
19203 even though the given DIE does not have a declaration attribute. */
19204 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19205 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19206 }
19207
19208 /* Return the die giving the specification for DIE, if there is
19209 one. *SPEC_CU is the CU containing DIE on input, and the CU
19210 containing the return value on output. If there is no
19211 specification, but there is an abstract origin, that is
19212 returned. */
19213
19214 static struct die_info *
19215 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19216 {
19217 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19218 *spec_cu);
19219
19220 if (spec_attr == NULL)
19221 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19222
19223 if (spec_attr == NULL)
19224 return NULL;
19225 else
19226 return follow_die_ref (die, spec_attr, spec_cu);
19227 }
19228
19229 /* Stub for free_line_header to match void * callback types. */
19230
19231 static void
19232 free_line_header_voidp (void *arg)
19233 {
19234 struct line_header *lh = (struct line_header *) arg;
19235
19236 delete lh;
19237 }
19238
19239 /* A convenience function to find the proper .debug_line section for a CU. */
19240
19241 static struct dwarf2_section_info *
19242 get_debug_line_section (struct dwarf2_cu *cu)
19243 {
19244 struct dwarf2_section_info *section;
19245 struct dwarf2_per_objfile *dwarf2_per_objfile
19246 = cu->per_cu->dwarf2_per_objfile;
19247
19248 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19249 DWO file. */
19250 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19251 section = &cu->dwo_unit->dwo_file->sections.line;
19252 else if (cu->per_cu->is_dwz)
19253 {
19254 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19255
19256 section = &dwz->line;
19257 }
19258 else
19259 section = &dwarf2_per_objfile->line;
19260
19261 return section;
19262 }
19263
19264 /* Read directory or file name entry format, starting with byte of
19265 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19266 entries count and the entries themselves in the described entry
19267 format. */
19268
19269 static void
19270 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19271 bfd *abfd, const gdb_byte **bufp,
19272 struct line_header *lh,
19273 const struct comp_unit_head *cu_header,
19274 void (*callback) (struct line_header *lh,
19275 const char *name,
19276 dir_index d_index,
19277 unsigned int mod_time,
19278 unsigned int length))
19279 {
19280 gdb_byte format_count, formati;
19281 ULONGEST data_count, datai;
19282 const gdb_byte *buf = *bufp;
19283 const gdb_byte *format_header_data;
19284 unsigned int bytes_read;
19285
19286 format_count = read_1_byte (abfd, buf);
19287 buf += 1;
19288 format_header_data = buf;
19289 for (formati = 0; formati < format_count; formati++)
19290 {
19291 read_unsigned_leb128 (abfd, buf, &bytes_read);
19292 buf += bytes_read;
19293 read_unsigned_leb128 (abfd, buf, &bytes_read);
19294 buf += bytes_read;
19295 }
19296
19297 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19298 buf += bytes_read;
19299 for (datai = 0; datai < data_count; datai++)
19300 {
19301 const gdb_byte *format = format_header_data;
19302 struct file_entry fe;
19303
19304 for (formati = 0; formati < format_count; formati++)
19305 {
19306 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19307 format += bytes_read;
19308
19309 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19310 format += bytes_read;
19311
19312 gdb::optional<const char *> string;
19313 gdb::optional<unsigned int> uint;
19314
19315 switch (form)
19316 {
19317 case DW_FORM_string:
19318 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19319 buf += bytes_read;
19320 break;
19321
19322 case DW_FORM_line_strp:
19323 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19324 abfd, buf,
19325 cu_header,
19326 &bytes_read));
19327 buf += bytes_read;
19328 break;
19329
19330 case DW_FORM_data1:
19331 uint.emplace (read_1_byte (abfd, buf));
19332 buf += 1;
19333 break;
19334
19335 case DW_FORM_data2:
19336 uint.emplace (read_2_bytes (abfd, buf));
19337 buf += 2;
19338 break;
19339
19340 case DW_FORM_data4:
19341 uint.emplace (read_4_bytes (abfd, buf));
19342 buf += 4;
19343 break;
19344
19345 case DW_FORM_data8:
19346 uint.emplace (read_8_bytes (abfd, buf));
19347 buf += 8;
19348 break;
19349
19350 case DW_FORM_data16:
19351 /* This is used for MD5, but file_entry does not record MD5s. */
19352 buf += 16;
19353 break;
19354
19355 case DW_FORM_udata:
19356 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19357 buf += bytes_read;
19358 break;
19359
19360 case DW_FORM_block:
19361 /* It is valid only for DW_LNCT_timestamp which is ignored by
19362 current GDB. */
19363 break;
19364 }
19365
19366 switch (content_type)
19367 {
19368 case DW_LNCT_path:
19369 if (string.has_value ())
19370 fe.name = *string;
19371 break;
19372 case DW_LNCT_directory_index:
19373 if (uint.has_value ())
19374 fe.d_index = (dir_index) *uint;
19375 break;
19376 case DW_LNCT_timestamp:
19377 if (uint.has_value ())
19378 fe.mod_time = *uint;
19379 break;
19380 case DW_LNCT_size:
19381 if (uint.has_value ())
19382 fe.length = *uint;
19383 break;
19384 case DW_LNCT_MD5:
19385 break;
19386 default:
19387 complaint (_("Unknown format content type %s"),
19388 pulongest (content_type));
19389 }
19390 }
19391
19392 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19393 }
19394
19395 *bufp = buf;
19396 }
19397
19398 /* Read the statement program header starting at OFFSET in
19399 .debug_line, or .debug_line.dwo. Return a pointer
19400 to a struct line_header, allocated using xmalloc.
19401 Returns NULL if there is a problem reading the header, e.g., if it
19402 has a version we don't understand.
19403
19404 NOTE: the strings in the include directory and file name tables of
19405 the returned object point into the dwarf line section buffer,
19406 and must not be freed. */
19407
19408 static line_header_up
19409 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19410 {
19411 const gdb_byte *line_ptr;
19412 unsigned int bytes_read, offset_size;
19413 int i;
19414 const char *cur_dir, *cur_file;
19415 struct dwarf2_section_info *section;
19416 bfd *abfd;
19417 struct dwarf2_per_objfile *dwarf2_per_objfile
19418 = cu->per_cu->dwarf2_per_objfile;
19419
19420 section = get_debug_line_section (cu);
19421 section->read (dwarf2_per_objfile->objfile);
19422 if (section->buffer == NULL)
19423 {
19424 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19425 complaint (_("missing .debug_line.dwo section"));
19426 else
19427 complaint (_("missing .debug_line section"));
19428 return 0;
19429 }
19430
19431 /* We can't do this until we know the section is non-empty.
19432 Only then do we know we have such a section. */
19433 abfd = section->get_bfd_owner ();
19434
19435 /* Make sure that at least there's room for the total_length field.
19436 That could be 12 bytes long, but we're just going to fudge that. */
19437 if (to_underlying (sect_off) + 4 >= section->size)
19438 {
19439 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19440 return 0;
19441 }
19442
19443 line_header_up lh (new line_header ());
19444
19445 lh->sect_off = sect_off;
19446 lh->offset_in_dwz = cu->per_cu->is_dwz;
19447
19448 line_ptr = section->buffer + to_underlying (sect_off);
19449
19450 /* Read in the header. */
19451 lh->total_length =
19452 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19453 &bytes_read, &offset_size);
19454 line_ptr += bytes_read;
19455
19456 const gdb_byte *start_here = line_ptr;
19457
19458 if (line_ptr + lh->total_length > (section->buffer + section->size))
19459 {
19460 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19461 return 0;
19462 }
19463 lh->statement_program_end = start_here + lh->total_length;
19464 lh->version = read_2_bytes (abfd, line_ptr);
19465 line_ptr += 2;
19466 if (lh->version > 5)
19467 {
19468 /* This is a version we don't understand. The format could have
19469 changed in ways we don't handle properly so just punt. */
19470 complaint (_("unsupported version in .debug_line section"));
19471 return NULL;
19472 }
19473 if (lh->version >= 5)
19474 {
19475 gdb_byte segment_selector_size;
19476
19477 /* Skip address size. */
19478 read_1_byte (abfd, line_ptr);
19479 line_ptr += 1;
19480
19481 segment_selector_size = read_1_byte (abfd, line_ptr);
19482 line_ptr += 1;
19483 if (segment_selector_size != 0)
19484 {
19485 complaint (_("unsupported segment selector size %u "
19486 "in .debug_line section"),
19487 segment_selector_size);
19488 return NULL;
19489 }
19490 }
19491 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19492 line_ptr += offset_size;
19493 lh->statement_program_start = line_ptr + lh->header_length;
19494 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19495 line_ptr += 1;
19496 if (lh->version >= 4)
19497 {
19498 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19499 line_ptr += 1;
19500 }
19501 else
19502 lh->maximum_ops_per_instruction = 1;
19503
19504 if (lh->maximum_ops_per_instruction == 0)
19505 {
19506 lh->maximum_ops_per_instruction = 1;
19507 complaint (_("invalid maximum_ops_per_instruction "
19508 "in `.debug_line' section"));
19509 }
19510
19511 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19512 line_ptr += 1;
19513 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19514 line_ptr += 1;
19515 lh->line_range = read_1_byte (abfd, line_ptr);
19516 line_ptr += 1;
19517 lh->opcode_base = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
19519 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19520
19521 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19522 for (i = 1; i < lh->opcode_base; ++i)
19523 {
19524 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19525 line_ptr += 1;
19526 }
19527
19528 if (lh->version >= 5)
19529 {
19530 /* Read directory table. */
19531 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19532 &cu->header,
19533 [] (struct line_header *header, const char *name,
19534 dir_index d_index, unsigned int mod_time,
19535 unsigned int length)
19536 {
19537 header->add_include_dir (name);
19538 });
19539
19540 /* Read file name table. */
19541 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19542 &cu->header,
19543 [] (struct line_header *header, const char *name,
19544 dir_index d_index, unsigned int mod_time,
19545 unsigned int length)
19546 {
19547 header->add_file_name (name, d_index, mod_time, length);
19548 });
19549 }
19550 else
19551 {
19552 /* Read directory table. */
19553 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19554 {
19555 line_ptr += bytes_read;
19556 lh->add_include_dir (cur_dir);
19557 }
19558 line_ptr += bytes_read;
19559
19560 /* Read file name table. */
19561 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19562 {
19563 unsigned int mod_time, length;
19564 dir_index d_index;
19565
19566 line_ptr += bytes_read;
19567 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19568 line_ptr += bytes_read;
19569 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19570 line_ptr += bytes_read;
19571 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19572 line_ptr += bytes_read;
19573
19574 lh->add_file_name (cur_file, d_index, mod_time, length);
19575 }
19576 line_ptr += bytes_read;
19577 }
19578
19579 if (line_ptr > (section->buffer + section->size))
19580 complaint (_("line number info header doesn't "
19581 "fit in `.debug_line' section"));
19582
19583 return lh;
19584 }
19585
19586 /* Subroutine of dwarf_decode_lines to simplify it.
19587 Return the file name of the psymtab for the given file_entry.
19588 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19589 If space for the result is malloc'd, *NAME_HOLDER will be set.
19590 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19591
19592 static const char *
19593 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19594 const dwarf2_psymtab *pst,
19595 const char *comp_dir,
19596 gdb::unique_xmalloc_ptr<char> *name_holder)
19597 {
19598 const char *include_name = fe.name;
19599 const char *include_name_to_compare = include_name;
19600 const char *pst_filename;
19601 int file_is_pst;
19602
19603 const char *dir_name = fe.include_dir (lh);
19604
19605 gdb::unique_xmalloc_ptr<char> hold_compare;
19606 if (!IS_ABSOLUTE_PATH (include_name)
19607 && (dir_name != NULL || comp_dir != NULL))
19608 {
19609 /* Avoid creating a duplicate psymtab for PST.
19610 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19611 Before we do the comparison, however, we need to account
19612 for DIR_NAME and COMP_DIR.
19613 First prepend dir_name (if non-NULL). If we still don't
19614 have an absolute path prepend comp_dir (if non-NULL).
19615 However, the directory we record in the include-file's
19616 psymtab does not contain COMP_DIR (to match the
19617 corresponding symtab(s)).
19618
19619 Example:
19620
19621 bash$ cd /tmp
19622 bash$ gcc -g ./hello.c
19623 include_name = "hello.c"
19624 dir_name = "."
19625 DW_AT_comp_dir = comp_dir = "/tmp"
19626 DW_AT_name = "./hello.c"
19627
19628 */
19629
19630 if (dir_name != NULL)
19631 {
19632 name_holder->reset (concat (dir_name, SLASH_STRING,
19633 include_name, (char *) NULL));
19634 include_name = name_holder->get ();
19635 include_name_to_compare = include_name;
19636 }
19637 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19638 {
19639 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19640 include_name, (char *) NULL));
19641 include_name_to_compare = hold_compare.get ();
19642 }
19643 }
19644
19645 pst_filename = pst->filename;
19646 gdb::unique_xmalloc_ptr<char> copied_name;
19647 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19648 {
19649 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19650 pst_filename, (char *) NULL));
19651 pst_filename = copied_name.get ();
19652 }
19653
19654 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19655
19656 if (file_is_pst)
19657 return NULL;
19658 return include_name;
19659 }
19660
19661 /* State machine to track the state of the line number program. */
19662
19663 class lnp_state_machine
19664 {
19665 public:
19666 /* Initialize a machine state for the start of a line number
19667 program. */
19668 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19669 bool record_lines_p);
19670
19671 file_entry *current_file ()
19672 {
19673 /* lh->file_names is 0-based, but the file name numbers in the
19674 statement program are 1-based. */
19675 return m_line_header->file_name_at (m_file);
19676 }
19677
19678 /* Record the line in the state machine. END_SEQUENCE is true if
19679 we're processing the end of a sequence. */
19680 void record_line (bool end_sequence);
19681
19682 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19683 nop-out rest of the lines in this sequence. */
19684 void check_line_address (struct dwarf2_cu *cu,
19685 const gdb_byte *line_ptr,
19686 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19687
19688 void handle_set_discriminator (unsigned int discriminator)
19689 {
19690 m_discriminator = discriminator;
19691 m_line_has_non_zero_discriminator |= discriminator != 0;
19692 }
19693
19694 /* Handle DW_LNE_set_address. */
19695 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19696 {
19697 m_op_index = 0;
19698 address += baseaddr;
19699 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19700 }
19701
19702 /* Handle DW_LNS_advance_pc. */
19703 void handle_advance_pc (CORE_ADDR adjust);
19704
19705 /* Handle a special opcode. */
19706 void handle_special_opcode (unsigned char op_code);
19707
19708 /* Handle DW_LNS_advance_line. */
19709 void handle_advance_line (int line_delta)
19710 {
19711 advance_line (line_delta);
19712 }
19713
19714 /* Handle DW_LNS_set_file. */
19715 void handle_set_file (file_name_index file);
19716
19717 /* Handle DW_LNS_negate_stmt. */
19718 void handle_negate_stmt ()
19719 {
19720 m_is_stmt = !m_is_stmt;
19721 }
19722
19723 /* Handle DW_LNS_const_add_pc. */
19724 void handle_const_add_pc ();
19725
19726 /* Handle DW_LNS_fixed_advance_pc. */
19727 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19728 {
19729 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19730 m_op_index = 0;
19731 }
19732
19733 /* Handle DW_LNS_copy. */
19734 void handle_copy ()
19735 {
19736 record_line (false);
19737 m_discriminator = 0;
19738 }
19739
19740 /* Handle DW_LNE_end_sequence. */
19741 void handle_end_sequence ()
19742 {
19743 m_currently_recording_lines = true;
19744 }
19745
19746 private:
19747 /* Advance the line by LINE_DELTA. */
19748 void advance_line (int line_delta)
19749 {
19750 m_line += line_delta;
19751
19752 if (line_delta != 0)
19753 m_line_has_non_zero_discriminator = m_discriminator != 0;
19754 }
19755
19756 struct dwarf2_cu *m_cu;
19757
19758 gdbarch *m_gdbarch;
19759
19760 /* True if we're recording lines.
19761 Otherwise we're building partial symtabs and are just interested in
19762 finding include files mentioned by the line number program. */
19763 bool m_record_lines_p;
19764
19765 /* The line number header. */
19766 line_header *m_line_header;
19767
19768 /* These are part of the standard DWARF line number state machine,
19769 and initialized according to the DWARF spec. */
19770
19771 unsigned char m_op_index = 0;
19772 /* The line table index of the current file. */
19773 file_name_index m_file = 1;
19774 unsigned int m_line = 1;
19775
19776 /* These are initialized in the constructor. */
19777
19778 CORE_ADDR m_address;
19779 bool m_is_stmt;
19780 unsigned int m_discriminator;
19781
19782 /* Additional bits of state we need to track. */
19783
19784 /* The last file that we called dwarf2_start_subfile for.
19785 This is only used for TLLs. */
19786 unsigned int m_last_file = 0;
19787 /* The last file a line number was recorded for. */
19788 struct subfile *m_last_subfile = NULL;
19789
19790 /* When true, record the lines we decode. */
19791 bool m_currently_recording_lines = false;
19792
19793 /* The last line number that was recorded, used to coalesce
19794 consecutive entries for the same line. This can happen, for
19795 example, when discriminators are present. PR 17276. */
19796 unsigned int m_last_line = 0;
19797 bool m_line_has_non_zero_discriminator = false;
19798 };
19799
19800 void
19801 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19802 {
19803 CORE_ADDR addr_adj = (((m_op_index + adjust)
19804 / m_line_header->maximum_ops_per_instruction)
19805 * m_line_header->minimum_instruction_length);
19806 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19807 m_op_index = ((m_op_index + adjust)
19808 % m_line_header->maximum_ops_per_instruction);
19809 }
19810
19811 void
19812 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19813 {
19814 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19815 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19816 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19817 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19818 / m_line_header->maximum_ops_per_instruction)
19819 * m_line_header->minimum_instruction_length);
19820 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19821 m_op_index = ((m_op_index + adj_opcode_d)
19822 % m_line_header->maximum_ops_per_instruction);
19823
19824 int line_delta = m_line_header->line_base + adj_opcode_r;
19825 advance_line (line_delta);
19826 record_line (false);
19827 m_discriminator = 0;
19828 }
19829
19830 void
19831 lnp_state_machine::handle_set_file (file_name_index file)
19832 {
19833 m_file = file;
19834
19835 const file_entry *fe = current_file ();
19836 if (fe == NULL)
19837 dwarf2_debug_line_missing_file_complaint ();
19838 else if (m_record_lines_p)
19839 {
19840 const char *dir = fe->include_dir (m_line_header);
19841
19842 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19843 m_line_has_non_zero_discriminator = m_discriminator != 0;
19844 dwarf2_start_subfile (m_cu, fe->name, dir);
19845 }
19846 }
19847
19848 void
19849 lnp_state_machine::handle_const_add_pc ()
19850 {
19851 CORE_ADDR adjust
19852 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19853
19854 CORE_ADDR addr_adj
19855 = (((m_op_index + adjust)
19856 / m_line_header->maximum_ops_per_instruction)
19857 * m_line_header->minimum_instruction_length);
19858
19859 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19860 m_op_index = ((m_op_index + adjust)
19861 % m_line_header->maximum_ops_per_instruction);
19862 }
19863
19864 /* Return non-zero if we should add LINE to the line number table.
19865 LINE is the line to add, LAST_LINE is the last line that was added,
19866 LAST_SUBFILE is the subfile for LAST_LINE.
19867 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19868 had a non-zero discriminator.
19869
19870 We have to be careful in the presence of discriminators.
19871 E.g., for this line:
19872
19873 for (i = 0; i < 100000; i++);
19874
19875 clang can emit four line number entries for that one line,
19876 each with a different discriminator.
19877 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19878
19879 However, we want gdb to coalesce all four entries into one.
19880 Otherwise the user could stepi into the middle of the line and
19881 gdb would get confused about whether the pc really was in the
19882 middle of the line.
19883
19884 Things are further complicated by the fact that two consecutive
19885 line number entries for the same line is a heuristic used by gcc
19886 to denote the end of the prologue. So we can't just discard duplicate
19887 entries, we have to be selective about it. The heuristic we use is
19888 that we only collapse consecutive entries for the same line if at least
19889 one of those entries has a non-zero discriminator. PR 17276.
19890
19891 Note: Addresses in the line number state machine can never go backwards
19892 within one sequence, thus this coalescing is ok. */
19893
19894 static int
19895 dwarf_record_line_p (struct dwarf2_cu *cu,
19896 unsigned int line, unsigned int last_line,
19897 int line_has_non_zero_discriminator,
19898 struct subfile *last_subfile)
19899 {
19900 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19901 return 1;
19902 if (line != last_line)
19903 return 1;
19904 /* Same line for the same file that we've seen already.
19905 As a last check, for pr 17276, only record the line if the line
19906 has never had a non-zero discriminator. */
19907 if (!line_has_non_zero_discriminator)
19908 return 1;
19909 return 0;
19910 }
19911
19912 /* Use the CU's builder to record line number LINE beginning at
19913 address ADDRESS in the line table of subfile SUBFILE. */
19914
19915 static void
19916 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19917 unsigned int line, CORE_ADDR address,
19918 struct dwarf2_cu *cu)
19919 {
19920 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19921
19922 if (dwarf_line_debug)
19923 {
19924 fprintf_unfiltered (gdb_stdlog,
19925 "Recording line %u, file %s, address %s\n",
19926 line, lbasename (subfile->name),
19927 paddress (gdbarch, address));
19928 }
19929
19930 if (cu != nullptr)
19931 cu->get_builder ()->record_line (subfile, line, addr);
19932 }
19933
19934 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19935 Mark the end of a set of line number records.
19936 The arguments are the same as for dwarf_record_line_1.
19937 If SUBFILE is NULL the request is ignored. */
19938
19939 static void
19940 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19941 CORE_ADDR address, struct dwarf2_cu *cu)
19942 {
19943 if (subfile == NULL)
19944 return;
19945
19946 if (dwarf_line_debug)
19947 {
19948 fprintf_unfiltered (gdb_stdlog,
19949 "Finishing current line, file %s, address %s\n",
19950 lbasename (subfile->name),
19951 paddress (gdbarch, address));
19952 }
19953
19954 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19955 }
19956
19957 void
19958 lnp_state_machine::record_line (bool end_sequence)
19959 {
19960 if (dwarf_line_debug)
19961 {
19962 fprintf_unfiltered (gdb_stdlog,
19963 "Processing actual line %u: file %u,"
19964 " address %s, is_stmt %u, discrim %u%s\n",
19965 m_line, m_file,
19966 paddress (m_gdbarch, m_address),
19967 m_is_stmt, m_discriminator,
19968 (end_sequence ? "\t(end sequence)" : ""));
19969 }
19970
19971 file_entry *fe = current_file ();
19972
19973 if (fe == NULL)
19974 dwarf2_debug_line_missing_file_complaint ();
19975 /* For now we ignore lines not starting on an instruction boundary.
19976 But not when processing end_sequence for compatibility with the
19977 previous version of the code. */
19978 else if (m_op_index == 0 || end_sequence)
19979 {
19980 fe->included_p = 1;
19981 if (m_record_lines_p
19982 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19983 {
19984 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19985 || end_sequence)
19986 {
19987 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19988 m_currently_recording_lines ? m_cu : nullptr);
19989 }
19990
19991 if (!end_sequence)
19992 {
19993 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19994 m_line_has_non_zero_discriminator,
19995 m_last_subfile))
19996 {
19997 buildsym_compunit *builder = m_cu->get_builder ();
19998 dwarf_record_line_1 (m_gdbarch,
19999 builder->get_current_subfile (),
20000 m_line, m_address,
20001 m_currently_recording_lines ? m_cu : nullptr);
20002 }
20003 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20004 m_last_line = m_line;
20005 }
20006 }
20007 }
20008 }
20009
20010 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20011 line_header *lh, bool record_lines_p)
20012 {
20013 m_cu = cu;
20014 m_gdbarch = arch;
20015 m_record_lines_p = record_lines_p;
20016 m_line_header = lh;
20017
20018 m_currently_recording_lines = true;
20019
20020 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20021 was a line entry for it so that the backend has a chance to adjust it
20022 and also record it in case it needs it. This is currently used by MIPS
20023 code, cf. `mips_adjust_dwarf2_line'. */
20024 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20025 m_is_stmt = lh->default_is_stmt;
20026 m_discriminator = 0;
20027 }
20028
20029 void
20030 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20031 const gdb_byte *line_ptr,
20032 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20033 {
20034 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20035 the pc range of the CU. However, we restrict the test to only ADDRESS
20036 values of zero to preserve GDB's previous behaviour which is to handle
20037 the specific case of a function being GC'd by the linker. */
20038
20039 if (address == 0 && address < unrelocated_lowpc)
20040 {
20041 /* This line table is for a function which has been
20042 GCd by the linker. Ignore it. PR gdb/12528 */
20043
20044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20045 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20046
20047 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20048 line_offset, objfile_name (objfile));
20049 m_currently_recording_lines = false;
20050 /* Note: m_currently_recording_lines is left as false until we see
20051 DW_LNE_end_sequence. */
20052 }
20053 }
20054
20055 /* Subroutine of dwarf_decode_lines to simplify it.
20056 Process the line number information in LH.
20057 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20058 program in order to set included_p for every referenced header. */
20059
20060 static void
20061 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20062 const int decode_for_pst_p, CORE_ADDR lowpc)
20063 {
20064 const gdb_byte *line_ptr, *extended_end;
20065 const gdb_byte *line_end;
20066 unsigned int bytes_read, extended_len;
20067 unsigned char op_code, extended_op;
20068 CORE_ADDR baseaddr;
20069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20070 bfd *abfd = objfile->obfd;
20071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20072 /* True if we're recording line info (as opposed to building partial
20073 symtabs and just interested in finding include files mentioned by
20074 the line number program). */
20075 bool record_lines_p = !decode_for_pst_p;
20076
20077 baseaddr = objfile->text_section_offset ();
20078
20079 line_ptr = lh->statement_program_start;
20080 line_end = lh->statement_program_end;
20081
20082 /* Read the statement sequences until there's nothing left. */
20083 while (line_ptr < line_end)
20084 {
20085 /* The DWARF line number program state machine. Reset the state
20086 machine at the start of each sequence. */
20087 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20088 bool end_sequence = false;
20089
20090 if (record_lines_p)
20091 {
20092 /* Start a subfile for the current file of the state
20093 machine. */
20094 const file_entry *fe = state_machine.current_file ();
20095
20096 if (fe != NULL)
20097 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20098 }
20099
20100 /* Decode the table. */
20101 while (line_ptr < line_end && !end_sequence)
20102 {
20103 op_code = read_1_byte (abfd, line_ptr);
20104 line_ptr += 1;
20105
20106 if (op_code >= lh->opcode_base)
20107 {
20108 /* Special opcode. */
20109 state_machine.handle_special_opcode (op_code);
20110 }
20111 else switch (op_code)
20112 {
20113 case DW_LNS_extended_op:
20114 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20115 &bytes_read);
20116 line_ptr += bytes_read;
20117 extended_end = line_ptr + extended_len;
20118 extended_op = read_1_byte (abfd, line_ptr);
20119 line_ptr += 1;
20120 switch (extended_op)
20121 {
20122 case DW_LNE_end_sequence:
20123 state_machine.handle_end_sequence ();
20124 end_sequence = true;
20125 break;
20126 case DW_LNE_set_address:
20127 {
20128 CORE_ADDR address
20129 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20130 line_ptr += bytes_read;
20131
20132 state_machine.check_line_address (cu, line_ptr,
20133 lowpc - baseaddr, address);
20134 state_machine.handle_set_address (baseaddr, address);
20135 }
20136 break;
20137 case DW_LNE_define_file:
20138 {
20139 const char *cur_file;
20140 unsigned int mod_time, length;
20141 dir_index dindex;
20142
20143 cur_file = read_direct_string (abfd, line_ptr,
20144 &bytes_read);
20145 line_ptr += bytes_read;
20146 dindex = (dir_index)
20147 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20148 line_ptr += bytes_read;
20149 mod_time =
20150 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20151 line_ptr += bytes_read;
20152 length =
20153 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20154 line_ptr += bytes_read;
20155 lh->add_file_name (cur_file, dindex, mod_time, length);
20156 }
20157 break;
20158 case DW_LNE_set_discriminator:
20159 {
20160 /* The discriminator is not interesting to the
20161 debugger; just ignore it. We still need to
20162 check its value though:
20163 if there are consecutive entries for the same
20164 (non-prologue) line we want to coalesce them.
20165 PR 17276. */
20166 unsigned int discr
20167 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20168 line_ptr += bytes_read;
20169
20170 state_machine.handle_set_discriminator (discr);
20171 }
20172 break;
20173 default:
20174 complaint (_("mangled .debug_line section"));
20175 return;
20176 }
20177 /* Make sure that we parsed the extended op correctly. If e.g.
20178 we expected a different address size than the producer used,
20179 we may have read the wrong number of bytes. */
20180 if (line_ptr != extended_end)
20181 {
20182 complaint (_("mangled .debug_line section"));
20183 return;
20184 }
20185 break;
20186 case DW_LNS_copy:
20187 state_machine.handle_copy ();
20188 break;
20189 case DW_LNS_advance_pc:
20190 {
20191 CORE_ADDR adjust
20192 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20193 line_ptr += bytes_read;
20194
20195 state_machine.handle_advance_pc (adjust);
20196 }
20197 break;
20198 case DW_LNS_advance_line:
20199 {
20200 int line_delta
20201 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20202 line_ptr += bytes_read;
20203
20204 state_machine.handle_advance_line (line_delta);
20205 }
20206 break;
20207 case DW_LNS_set_file:
20208 {
20209 file_name_index file
20210 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20211 &bytes_read);
20212 line_ptr += bytes_read;
20213
20214 state_machine.handle_set_file (file);
20215 }
20216 break;
20217 case DW_LNS_set_column:
20218 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20219 line_ptr += bytes_read;
20220 break;
20221 case DW_LNS_negate_stmt:
20222 state_machine.handle_negate_stmt ();
20223 break;
20224 case DW_LNS_set_basic_block:
20225 break;
20226 /* Add to the address register of the state machine the
20227 address increment value corresponding to special opcode
20228 255. I.e., this value is scaled by the minimum
20229 instruction length since special opcode 255 would have
20230 scaled the increment. */
20231 case DW_LNS_const_add_pc:
20232 state_machine.handle_const_add_pc ();
20233 break;
20234 case DW_LNS_fixed_advance_pc:
20235 {
20236 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20237 line_ptr += 2;
20238
20239 state_machine.handle_fixed_advance_pc (addr_adj);
20240 }
20241 break;
20242 default:
20243 {
20244 /* Unknown standard opcode, ignore it. */
20245 int i;
20246
20247 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20248 {
20249 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20250 line_ptr += bytes_read;
20251 }
20252 }
20253 }
20254 }
20255
20256 if (!end_sequence)
20257 dwarf2_debug_line_missing_end_sequence_complaint ();
20258
20259 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20260 in which case we still finish recording the last line). */
20261 state_machine.record_line (true);
20262 }
20263 }
20264
20265 /* Decode the Line Number Program (LNP) for the given line_header
20266 structure and CU. The actual information extracted and the type
20267 of structures created from the LNP depends on the value of PST.
20268
20269 1. If PST is NULL, then this procedure uses the data from the program
20270 to create all necessary symbol tables, and their linetables.
20271
20272 2. If PST is not NULL, this procedure reads the program to determine
20273 the list of files included by the unit represented by PST, and
20274 builds all the associated partial symbol tables.
20275
20276 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20277 It is used for relative paths in the line table.
20278 NOTE: When processing partial symtabs (pst != NULL),
20279 comp_dir == pst->dirname.
20280
20281 NOTE: It is important that psymtabs have the same file name (via strcmp)
20282 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20283 symtab we don't use it in the name of the psymtabs we create.
20284 E.g. expand_line_sal requires this when finding psymtabs to expand.
20285 A good testcase for this is mb-inline.exp.
20286
20287 LOWPC is the lowest address in CU (or 0 if not known).
20288
20289 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20290 for its PC<->lines mapping information. Otherwise only the filename
20291 table is read in. */
20292
20293 static void
20294 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20295 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20296 CORE_ADDR lowpc, int decode_mapping)
20297 {
20298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20299 const int decode_for_pst_p = (pst != NULL);
20300
20301 if (decode_mapping)
20302 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20303
20304 if (decode_for_pst_p)
20305 {
20306 /* Now that we're done scanning the Line Header Program, we can
20307 create the psymtab of each included file. */
20308 for (auto &file_entry : lh->file_names ())
20309 if (file_entry.included_p == 1)
20310 {
20311 gdb::unique_xmalloc_ptr<char> name_holder;
20312 const char *include_name =
20313 psymtab_include_file_name (lh, file_entry, pst,
20314 comp_dir, &name_holder);
20315 if (include_name != NULL)
20316 dwarf2_create_include_psymtab (include_name, pst, objfile);
20317 }
20318 }
20319 else
20320 {
20321 /* Make sure a symtab is created for every file, even files
20322 which contain only variables (i.e. no code with associated
20323 line numbers). */
20324 buildsym_compunit *builder = cu->get_builder ();
20325 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20326
20327 for (auto &fe : lh->file_names ())
20328 {
20329 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20330 if (builder->get_current_subfile ()->symtab == NULL)
20331 {
20332 builder->get_current_subfile ()->symtab
20333 = allocate_symtab (cust,
20334 builder->get_current_subfile ()->name);
20335 }
20336 fe.symtab = builder->get_current_subfile ()->symtab;
20337 }
20338 }
20339 }
20340
20341 /* Start a subfile for DWARF. FILENAME is the name of the file and
20342 DIRNAME the name of the source directory which contains FILENAME
20343 or NULL if not known.
20344 This routine tries to keep line numbers from identical absolute and
20345 relative file names in a common subfile.
20346
20347 Using the `list' example from the GDB testsuite, which resides in
20348 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20349 of /srcdir/list0.c yields the following debugging information for list0.c:
20350
20351 DW_AT_name: /srcdir/list0.c
20352 DW_AT_comp_dir: /compdir
20353 files.files[0].name: list0.h
20354 files.files[0].dir: /srcdir
20355 files.files[1].name: list0.c
20356 files.files[1].dir: /srcdir
20357
20358 The line number information for list0.c has to end up in a single
20359 subfile, so that `break /srcdir/list0.c:1' works as expected.
20360 start_subfile will ensure that this happens provided that we pass the
20361 concatenation of files.files[1].dir and files.files[1].name as the
20362 subfile's name. */
20363
20364 static void
20365 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20366 const char *dirname)
20367 {
20368 gdb::unique_xmalloc_ptr<char> copy;
20369
20370 /* In order not to lose the line information directory,
20371 we concatenate it to the filename when it makes sense.
20372 Note that the Dwarf3 standard says (speaking of filenames in line
20373 information): ``The directory index is ignored for file names
20374 that represent full path names''. Thus ignoring dirname in the
20375 `else' branch below isn't an issue. */
20376
20377 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20378 {
20379 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20380 filename = copy.get ();
20381 }
20382
20383 cu->get_builder ()->start_subfile (filename);
20384 }
20385
20386 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20387 buildsym_compunit constructor. */
20388
20389 struct compunit_symtab *
20390 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20391 CORE_ADDR low_pc)
20392 {
20393 gdb_assert (m_builder == nullptr);
20394
20395 m_builder.reset (new struct buildsym_compunit
20396 (per_cu->dwarf2_per_objfile->objfile,
20397 name, comp_dir, language, low_pc));
20398
20399 list_in_scope = get_builder ()->get_file_symbols ();
20400
20401 get_builder ()->record_debugformat ("DWARF 2");
20402 get_builder ()->record_producer (producer);
20403
20404 processing_has_namespace_info = false;
20405
20406 return get_builder ()->get_compunit_symtab ();
20407 }
20408
20409 static void
20410 var_decode_location (struct attribute *attr, struct symbol *sym,
20411 struct dwarf2_cu *cu)
20412 {
20413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20414 struct comp_unit_head *cu_header = &cu->header;
20415
20416 /* NOTE drow/2003-01-30: There used to be a comment and some special
20417 code here to turn a symbol with DW_AT_external and a
20418 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20419 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20420 with some versions of binutils) where shared libraries could have
20421 relocations against symbols in their debug information - the
20422 minimal symbol would have the right address, but the debug info
20423 would not. It's no longer necessary, because we will explicitly
20424 apply relocations when we read in the debug information now. */
20425
20426 /* A DW_AT_location attribute with no contents indicates that a
20427 variable has been optimized away. */
20428 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20429 {
20430 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20431 return;
20432 }
20433
20434 /* Handle one degenerate form of location expression specially, to
20435 preserve GDB's previous behavior when section offsets are
20436 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20437 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20438
20439 if (attr->form_is_block ()
20440 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20441 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20442 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20443 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20444 && (DW_BLOCK (attr)->size
20445 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20446 {
20447 unsigned int dummy;
20448
20449 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20450 SET_SYMBOL_VALUE_ADDRESS
20451 (sym, cu->header.read_address (objfile->obfd,
20452 DW_BLOCK (attr)->data + 1,
20453 &dummy));
20454 else
20455 SET_SYMBOL_VALUE_ADDRESS
20456 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20457 &dummy));
20458 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20459 fixup_symbol_section (sym, objfile);
20460 SET_SYMBOL_VALUE_ADDRESS
20461 (sym,
20462 SYMBOL_VALUE_ADDRESS (sym)
20463 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20464 return;
20465 }
20466
20467 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20468 expression evaluator, and use LOC_COMPUTED only when necessary
20469 (i.e. when the value of a register or memory location is
20470 referenced, or a thread-local block, etc.). Then again, it might
20471 not be worthwhile. I'm assuming that it isn't unless performance
20472 or memory numbers show me otherwise. */
20473
20474 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20475
20476 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20477 cu->has_loclist = true;
20478 }
20479
20480 /* Given a pointer to a DWARF information entry, figure out if we need
20481 to make a symbol table entry for it, and if so, create a new entry
20482 and return a pointer to it.
20483 If TYPE is NULL, determine symbol type from the die, otherwise
20484 used the passed type.
20485 If SPACE is not NULL, use it to hold the new symbol. If it is
20486 NULL, allocate a new symbol on the objfile's obstack. */
20487
20488 static struct symbol *
20489 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20490 struct symbol *space)
20491 {
20492 struct dwarf2_per_objfile *dwarf2_per_objfile
20493 = cu->per_cu->dwarf2_per_objfile;
20494 struct objfile *objfile = dwarf2_per_objfile->objfile;
20495 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20496 struct symbol *sym = NULL;
20497 const char *name;
20498 struct attribute *attr = NULL;
20499 struct attribute *attr2 = NULL;
20500 CORE_ADDR baseaddr;
20501 struct pending **list_to_add = NULL;
20502
20503 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20504
20505 baseaddr = objfile->text_section_offset ();
20506
20507 name = dwarf2_name (die, cu);
20508 if (name)
20509 {
20510 const char *linkagename;
20511 int suppress_add = 0;
20512
20513 if (space)
20514 sym = space;
20515 else
20516 sym = allocate_symbol (objfile);
20517 OBJSTAT (objfile, n_syms++);
20518
20519 /* Cache this symbol's name and the name's demangled form (if any). */
20520 sym->set_language (cu->language, &objfile->objfile_obstack);
20521 linkagename = dwarf2_physname (name, die, cu);
20522 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20523
20524 /* Fortran does not have mangling standard and the mangling does differ
20525 between gfortran, iFort etc. */
20526 if (cu->language == language_fortran
20527 && symbol_get_demangled_name (sym) == NULL)
20528 symbol_set_demangled_name (sym,
20529 dwarf2_full_name (name, die, cu),
20530 NULL);
20531
20532 /* Default assumptions.
20533 Use the passed type or decode it from the die. */
20534 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20535 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20536 if (type != NULL)
20537 SYMBOL_TYPE (sym) = type;
20538 else
20539 SYMBOL_TYPE (sym) = die_type (die, cu);
20540 attr = dwarf2_attr (die,
20541 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20542 cu);
20543 if (attr != nullptr)
20544 {
20545 SYMBOL_LINE (sym) = DW_UNSND (attr);
20546 }
20547
20548 attr = dwarf2_attr (die,
20549 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20550 cu);
20551 if (attr != nullptr)
20552 {
20553 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20554 struct file_entry *fe;
20555
20556 if (cu->line_header != NULL)
20557 fe = cu->line_header->file_name_at (file_index);
20558 else
20559 fe = NULL;
20560
20561 if (fe == NULL)
20562 complaint (_("file index out of range"));
20563 else
20564 symbol_set_symtab (sym, fe->symtab);
20565 }
20566
20567 switch (die->tag)
20568 {
20569 case DW_TAG_label:
20570 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20571 if (attr != nullptr)
20572 {
20573 CORE_ADDR addr;
20574
20575 addr = attr->value_as_address ();
20576 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20577 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20578 }
20579 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20580 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20581 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20582 add_symbol_to_list (sym, cu->list_in_scope);
20583 break;
20584 case DW_TAG_subprogram:
20585 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20586 finish_block. */
20587 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20588 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20589 if ((attr2 && (DW_UNSND (attr2) != 0))
20590 || cu->language == language_ada
20591 || cu->language == language_fortran)
20592 {
20593 /* Subprograms marked external are stored as a global symbol.
20594 Ada and Fortran subprograms, whether marked external or
20595 not, are always stored as a global symbol, because we want
20596 to be able to access them globally. For instance, we want
20597 to be able to break on a nested subprogram without having
20598 to specify the context. */
20599 list_to_add = cu->get_builder ()->get_global_symbols ();
20600 }
20601 else
20602 {
20603 list_to_add = cu->list_in_scope;
20604 }
20605 break;
20606 case DW_TAG_inlined_subroutine:
20607 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20608 finish_block. */
20609 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20610 SYMBOL_INLINED (sym) = 1;
20611 list_to_add = cu->list_in_scope;
20612 break;
20613 case DW_TAG_template_value_param:
20614 suppress_add = 1;
20615 /* Fall through. */
20616 case DW_TAG_constant:
20617 case DW_TAG_variable:
20618 case DW_TAG_member:
20619 /* Compilation with minimal debug info may result in
20620 variables with missing type entries. Change the
20621 misleading `void' type to something sensible. */
20622 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20623 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20624
20625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20626 /* In the case of DW_TAG_member, we should only be called for
20627 static const members. */
20628 if (die->tag == DW_TAG_member)
20629 {
20630 /* dwarf2_add_field uses die_is_declaration,
20631 so we do the same. */
20632 gdb_assert (die_is_declaration (die, cu));
20633 gdb_assert (attr);
20634 }
20635 if (attr != nullptr)
20636 {
20637 dwarf2_const_value (attr, sym, cu);
20638 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20639 if (!suppress_add)
20640 {
20641 if (attr2 && (DW_UNSND (attr2) != 0))
20642 list_to_add = cu->get_builder ()->get_global_symbols ();
20643 else
20644 list_to_add = cu->list_in_scope;
20645 }
20646 break;
20647 }
20648 attr = dwarf2_attr (die, DW_AT_location, cu);
20649 if (attr != nullptr)
20650 {
20651 var_decode_location (attr, sym, cu);
20652 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20653
20654 /* Fortran explicitly imports any global symbols to the local
20655 scope by DW_TAG_common_block. */
20656 if (cu->language == language_fortran && die->parent
20657 && die->parent->tag == DW_TAG_common_block)
20658 attr2 = NULL;
20659
20660 if (SYMBOL_CLASS (sym) == LOC_STATIC
20661 && SYMBOL_VALUE_ADDRESS (sym) == 0
20662 && !dwarf2_per_objfile->has_section_at_zero)
20663 {
20664 /* When a static variable is eliminated by the linker,
20665 the corresponding debug information is not stripped
20666 out, but the variable address is set to null;
20667 do not add such variables into symbol table. */
20668 }
20669 else if (attr2 && (DW_UNSND (attr2) != 0))
20670 {
20671 if (SYMBOL_CLASS (sym) == LOC_STATIC
20672 && (objfile->flags & OBJF_MAINLINE) == 0
20673 && dwarf2_per_objfile->can_copy)
20674 {
20675 /* A global static variable might be subject to
20676 copy relocation. We first check for a local
20677 minsym, though, because maybe the symbol was
20678 marked hidden, in which case this would not
20679 apply. */
20680 bound_minimal_symbol found
20681 = (lookup_minimal_symbol_linkage
20682 (sym->linkage_name (), objfile));
20683 if (found.minsym != nullptr)
20684 sym->maybe_copied = 1;
20685 }
20686
20687 /* A variable with DW_AT_external is never static,
20688 but it may be block-scoped. */
20689 list_to_add
20690 = ((cu->list_in_scope
20691 == cu->get_builder ()->get_file_symbols ())
20692 ? cu->get_builder ()->get_global_symbols ()
20693 : cu->list_in_scope);
20694 }
20695 else
20696 list_to_add = cu->list_in_scope;
20697 }
20698 else
20699 {
20700 /* We do not know the address of this symbol.
20701 If it is an external symbol and we have type information
20702 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20703 The address of the variable will then be determined from
20704 the minimal symbol table whenever the variable is
20705 referenced. */
20706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20707
20708 /* Fortran explicitly imports any global symbols to the local
20709 scope by DW_TAG_common_block. */
20710 if (cu->language == language_fortran && die->parent
20711 && die->parent->tag == DW_TAG_common_block)
20712 {
20713 /* SYMBOL_CLASS doesn't matter here because
20714 read_common_block is going to reset it. */
20715 if (!suppress_add)
20716 list_to_add = cu->list_in_scope;
20717 }
20718 else if (attr2 && (DW_UNSND (attr2) != 0)
20719 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20720 {
20721 /* A variable with DW_AT_external is never static, but it
20722 may be block-scoped. */
20723 list_to_add
20724 = ((cu->list_in_scope
20725 == cu->get_builder ()->get_file_symbols ())
20726 ? cu->get_builder ()->get_global_symbols ()
20727 : cu->list_in_scope);
20728
20729 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20730 }
20731 else if (!die_is_declaration (die, cu))
20732 {
20733 /* Use the default LOC_OPTIMIZED_OUT class. */
20734 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20735 if (!suppress_add)
20736 list_to_add = cu->list_in_scope;
20737 }
20738 }
20739 break;
20740 case DW_TAG_formal_parameter:
20741 {
20742 /* If we are inside a function, mark this as an argument. If
20743 not, we might be looking at an argument to an inlined function
20744 when we do not have enough information to show inlined frames;
20745 pretend it's a local variable in that case so that the user can
20746 still see it. */
20747 struct context_stack *curr
20748 = cu->get_builder ()->get_current_context_stack ();
20749 if (curr != nullptr && curr->name != nullptr)
20750 SYMBOL_IS_ARGUMENT (sym) = 1;
20751 attr = dwarf2_attr (die, DW_AT_location, cu);
20752 if (attr != nullptr)
20753 {
20754 var_decode_location (attr, sym, cu);
20755 }
20756 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20757 if (attr != nullptr)
20758 {
20759 dwarf2_const_value (attr, sym, cu);
20760 }
20761
20762 list_to_add = cu->list_in_scope;
20763 }
20764 break;
20765 case DW_TAG_unspecified_parameters:
20766 /* From varargs functions; gdb doesn't seem to have any
20767 interest in this information, so just ignore it for now.
20768 (FIXME?) */
20769 break;
20770 case DW_TAG_template_type_param:
20771 suppress_add = 1;
20772 /* Fall through. */
20773 case DW_TAG_class_type:
20774 case DW_TAG_interface_type:
20775 case DW_TAG_structure_type:
20776 case DW_TAG_union_type:
20777 case DW_TAG_set_type:
20778 case DW_TAG_enumeration_type:
20779 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20780 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20781
20782 {
20783 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20784 really ever be static objects: otherwise, if you try
20785 to, say, break of a class's method and you're in a file
20786 which doesn't mention that class, it won't work unless
20787 the check for all static symbols in lookup_symbol_aux
20788 saves you. See the OtherFileClass tests in
20789 gdb.c++/namespace.exp. */
20790
20791 if (!suppress_add)
20792 {
20793 buildsym_compunit *builder = cu->get_builder ();
20794 list_to_add
20795 = (cu->list_in_scope == builder->get_file_symbols ()
20796 && cu->language == language_cplus
20797 ? builder->get_global_symbols ()
20798 : cu->list_in_scope);
20799
20800 /* The semantics of C++ state that "struct foo {
20801 ... }" also defines a typedef for "foo". */
20802 if (cu->language == language_cplus
20803 || cu->language == language_ada
20804 || cu->language == language_d
20805 || cu->language == language_rust)
20806 {
20807 /* The symbol's name is already allocated along
20808 with this objfile, so we don't need to
20809 duplicate it for the type. */
20810 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20811 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20812 }
20813 }
20814 }
20815 break;
20816 case DW_TAG_typedef:
20817 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20818 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20819 list_to_add = cu->list_in_scope;
20820 break;
20821 case DW_TAG_base_type:
20822 case DW_TAG_subrange_type:
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_enumerator:
20828 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20829 if (attr != nullptr)
20830 {
20831 dwarf2_const_value (attr, sym, cu);
20832 }
20833 {
20834 /* NOTE: carlton/2003-11-10: See comment above in the
20835 DW_TAG_class_type, etc. block. */
20836
20837 list_to_add
20838 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20839 && cu->language == language_cplus
20840 ? cu->get_builder ()->get_global_symbols ()
20841 : cu->list_in_scope);
20842 }
20843 break;
20844 case DW_TAG_imported_declaration:
20845 case DW_TAG_namespace:
20846 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20847 list_to_add = cu->get_builder ()->get_global_symbols ();
20848 break;
20849 case DW_TAG_module:
20850 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20851 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20852 list_to_add = cu->get_builder ()->get_global_symbols ();
20853 break;
20854 case DW_TAG_common_block:
20855 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20856 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20857 add_symbol_to_list (sym, cu->list_in_scope);
20858 break;
20859 default:
20860 /* Not a tag we recognize. Hopefully we aren't processing
20861 trash data, but since we must specifically ignore things
20862 we don't recognize, there is nothing else we should do at
20863 this point. */
20864 complaint (_("unsupported tag: '%s'"),
20865 dwarf_tag_name (die->tag));
20866 break;
20867 }
20868
20869 if (suppress_add)
20870 {
20871 sym->hash_next = objfile->template_symbols;
20872 objfile->template_symbols = sym;
20873 list_to_add = NULL;
20874 }
20875
20876 if (list_to_add != NULL)
20877 add_symbol_to_list (sym, list_to_add);
20878
20879 /* For the benefit of old versions of GCC, check for anonymous
20880 namespaces based on the demangled name. */
20881 if (!cu->processing_has_namespace_info
20882 && cu->language == language_cplus)
20883 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20884 }
20885 return (sym);
20886 }
20887
20888 /* Given an attr with a DW_FORM_dataN value in host byte order,
20889 zero-extend it as appropriate for the symbol's type. The DWARF
20890 standard (v4) is not entirely clear about the meaning of using
20891 DW_FORM_dataN for a constant with a signed type, where the type is
20892 wider than the data. The conclusion of a discussion on the DWARF
20893 list was that this is unspecified. We choose to always zero-extend
20894 because that is the interpretation long in use by GCC. */
20895
20896 static gdb_byte *
20897 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20898 struct dwarf2_cu *cu, LONGEST *value, int bits)
20899 {
20900 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20901 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20902 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20903 LONGEST l = DW_UNSND (attr);
20904
20905 if (bits < sizeof (*value) * 8)
20906 {
20907 l &= ((LONGEST) 1 << bits) - 1;
20908 *value = l;
20909 }
20910 else if (bits == sizeof (*value) * 8)
20911 *value = l;
20912 else
20913 {
20914 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20915 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20916 return bytes;
20917 }
20918
20919 return NULL;
20920 }
20921
20922 /* Read a constant value from an attribute. Either set *VALUE, or if
20923 the value does not fit in *VALUE, set *BYTES - either already
20924 allocated on the objfile obstack, or newly allocated on OBSTACK,
20925 or, set *BATON, if we translated the constant to a location
20926 expression. */
20927
20928 static void
20929 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20930 const char *name, struct obstack *obstack,
20931 struct dwarf2_cu *cu,
20932 LONGEST *value, const gdb_byte **bytes,
20933 struct dwarf2_locexpr_baton **baton)
20934 {
20935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20936 struct comp_unit_head *cu_header = &cu->header;
20937 struct dwarf_block *blk;
20938 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20939 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20940
20941 *value = 0;
20942 *bytes = NULL;
20943 *baton = NULL;
20944
20945 switch (attr->form)
20946 {
20947 case DW_FORM_addr:
20948 case DW_FORM_addrx:
20949 case DW_FORM_GNU_addr_index:
20950 {
20951 gdb_byte *data;
20952
20953 if (TYPE_LENGTH (type) != cu_header->addr_size)
20954 dwarf2_const_value_length_mismatch_complaint (name,
20955 cu_header->addr_size,
20956 TYPE_LENGTH (type));
20957 /* Symbols of this form are reasonably rare, so we just
20958 piggyback on the existing location code rather than writing
20959 a new implementation of symbol_computed_ops. */
20960 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20961 (*baton)->per_cu = cu->per_cu;
20962 gdb_assert ((*baton)->per_cu);
20963
20964 (*baton)->size = 2 + cu_header->addr_size;
20965 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20966 (*baton)->data = data;
20967
20968 data[0] = DW_OP_addr;
20969 store_unsigned_integer (&data[1], cu_header->addr_size,
20970 byte_order, DW_ADDR (attr));
20971 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20972 }
20973 break;
20974 case DW_FORM_string:
20975 case DW_FORM_strp:
20976 case DW_FORM_strx:
20977 case DW_FORM_GNU_str_index:
20978 case DW_FORM_GNU_strp_alt:
20979 /* DW_STRING is already allocated on the objfile obstack, point
20980 directly to it. */
20981 *bytes = (const gdb_byte *) DW_STRING (attr);
20982 break;
20983 case DW_FORM_block1:
20984 case DW_FORM_block2:
20985 case DW_FORM_block4:
20986 case DW_FORM_block:
20987 case DW_FORM_exprloc:
20988 case DW_FORM_data16:
20989 blk = DW_BLOCK (attr);
20990 if (TYPE_LENGTH (type) != blk->size)
20991 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20992 TYPE_LENGTH (type));
20993 *bytes = blk->data;
20994 break;
20995
20996 /* The DW_AT_const_value attributes are supposed to carry the
20997 symbol's value "represented as it would be on the target
20998 architecture." By the time we get here, it's already been
20999 converted to host endianness, so we just need to sign- or
21000 zero-extend it as appropriate. */
21001 case DW_FORM_data1:
21002 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21003 break;
21004 case DW_FORM_data2:
21005 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21006 break;
21007 case DW_FORM_data4:
21008 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21009 break;
21010 case DW_FORM_data8:
21011 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21012 break;
21013
21014 case DW_FORM_sdata:
21015 case DW_FORM_implicit_const:
21016 *value = DW_SND (attr);
21017 break;
21018
21019 case DW_FORM_udata:
21020 *value = DW_UNSND (attr);
21021 break;
21022
21023 default:
21024 complaint (_("unsupported const value attribute form: '%s'"),
21025 dwarf_form_name (attr->form));
21026 *value = 0;
21027 break;
21028 }
21029 }
21030
21031
21032 /* Copy constant value from an attribute to a symbol. */
21033
21034 static void
21035 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21036 struct dwarf2_cu *cu)
21037 {
21038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21039 LONGEST value;
21040 const gdb_byte *bytes;
21041 struct dwarf2_locexpr_baton *baton;
21042
21043 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21044 sym->print_name (),
21045 &objfile->objfile_obstack, cu,
21046 &value, &bytes, &baton);
21047
21048 if (baton != NULL)
21049 {
21050 SYMBOL_LOCATION_BATON (sym) = baton;
21051 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21052 }
21053 else if (bytes != NULL)
21054 {
21055 SYMBOL_VALUE_BYTES (sym) = bytes;
21056 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21057 }
21058 else
21059 {
21060 SYMBOL_VALUE (sym) = value;
21061 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21062 }
21063 }
21064
21065 /* Return the type of the die in question using its DW_AT_type attribute. */
21066
21067 static struct type *
21068 die_type (struct die_info *die, struct dwarf2_cu *cu)
21069 {
21070 struct attribute *type_attr;
21071
21072 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21073 if (!type_attr)
21074 {
21075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21076 /* A missing DW_AT_type represents a void type. */
21077 return objfile_type (objfile)->builtin_void;
21078 }
21079
21080 return lookup_die_type (die, type_attr, cu);
21081 }
21082
21083 /* True iff CU's producer generates GNAT Ada auxiliary information
21084 that allows to find parallel types through that information instead
21085 of having to do expensive parallel lookups by type name. */
21086
21087 static int
21088 need_gnat_info (struct dwarf2_cu *cu)
21089 {
21090 /* Assume that the Ada compiler was GNAT, which always produces
21091 the auxiliary information. */
21092 return (cu->language == language_ada);
21093 }
21094
21095 /* Return the auxiliary type of the die in question using its
21096 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21097 attribute is not present. */
21098
21099 static struct type *
21100 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21101 {
21102 struct attribute *type_attr;
21103
21104 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21105 if (!type_attr)
21106 return NULL;
21107
21108 return lookup_die_type (die, type_attr, cu);
21109 }
21110
21111 /* If DIE has a descriptive_type attribute, then set the TYPE's
21112 descriptive type accordingly. */
21113
21114 static void
21115 set_descriptive_type (struct type *type, struct die_info *die,
21116 struct dwarf2_cu *cu)
21117 {
21118 struct type *descriptive_type = die_descriptive_type (die, cu);
21119
21120 if (descriptive_type)
21121 {
21122 ALLOCATE_GNAT_AUX_TYPE (type);
21123 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21124 }
21125 }
21126
21127 /* Return the containing type of the die in question using its
21128 DW_AT_containing_type attribute. */
21129
21130 static struct type *
21131 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21132 {
21133 struct attribute *type_attr;
21134 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21135
21136 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21137 if (!type_attr)
21138 error (_("Dwarf Error: Problem turning containing type into gdb type "
21139 "[in module %s]"), objfile_name (objfile));
21140
21141 return lookup_die_type (die, type_attr, cu);
21142 }
21143
21144 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21145
21146 static struct type *
21147 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21148 {
21149 struct dwarf2_per_objfile *dwarf2_per_objfile
21150 = cu->per_cu->dwarf2_per_objfile;
21151 struct objfile *objfile = dwarf2_per_objfile->objfile;
21152 char *saved;
21153
21154 std::string message
21155 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21156 objfile_name (objfile),
21157 sect_offset_str (cu->header.sect_off),
21158 sect_offset_str (die->sect_off));
21159 saved = obstack_strdup (&objfile->objfile_obstack, message);
21160
21161 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21162 }
21163
21164 /* Look up the type of DIE in CU using its type attribute ATTR.
21165 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21166 DW_AT_containing_type.
21167 If there is no type substitute an error marker. */
21168
21169 static struct type *
21170 lookup_die_type (struct die_info *die, const struct attribute *attr,
21171 struct dwarf2_cu *cu)
21172 {
21173 struct dwarf2_per_objfile *dwarf2_per_objfile
21174 = cu->per_cu->dwarf2_per_objfile;
21175 struct objfile *objfile = dwarf2_per_objfile->objfile;
21176 struct type *this_type;
21177
21178 gdb_assert (attr->name == DW_AT_type
21179 || attr->name == DW_AT_GNAT_descriptive_type
21180 || attr->name == DW_AT_containing_type);
21181
21182 /* First see if we have it cached. */
21183
21184 if (attr->form == DW_FORM_GNU_ref_alt)
21185 {
21186 struct dwarf2_per_cu_data *per_cu;
21187 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21188
21189 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21190 dwarf2_per_objfile);
21191 this_type = get_die_type_at_offset (sect_off, per_cu);
21192 }
21193 else if (attr->form_is_ref ())
21194 {
21195 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21196
21197 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21198 }
21199 else if (attr->form == DW_FORM_ref_sig8)
21200 {
21201 ULONGEST signature = DW_SIGNATURE (attr);
21202
21203 return get_signatured_type (die, signature, cu);
21204 }
21205 else
21206 {
21207 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21208 " at %s [in module %s]"),
21209 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21210 objfile_name (objfile));
21211 return build_error_marker_type (cu, die);
21212 }
21213
21214 /* If not cached we need to read it in. */
21215
21216 if (this_type == NULL)
21217 {
21218 struct die_info *type_die = NULL;
21219 struct dwarf2_cu *type_cu = cu;
21220
21221 if (attr->form_is_ref ())
21222 type_die = follow_die_ref (die, attr, &type_cu);
21223 if (type_die == NULL)
21224 return build_error_marker_type (cu, die);
21225 /* If we find the type now, it's probably because the type came
21226 from an inter-CU reference and the type's CU got expanded before
21227 ours. */
21228 this_type = read_type_die (type_die, type_cu);
21229 }
21230
21231 /* If we still don't have a type use an error marker. */
21232
21233 if (this_type == NULL)
21234 return build_error_marker_type (cu, die);
21235
21236 return this_type;
21237 }
21238
21239 /* Return the type in DIE, CU.
21240 Returns NULL for invalid types.
21241
21242 This first does a lookup in die_type_hash,
21243 and only reads the die in if necessary.
21244
21245 NOTE: This can be called when reading in partial or full symbols. */
21246
21247 static struct type *
21248 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21249 {
21250 struct type *this_type;
21251
21252 this_type = get_die_type (die, cu);
21253 if (this_type)
21254 return this_type;
21255
21256 return read_type_die_1 (die, cu);
21257 }
21258
21259 /* Read the type in DIE, CU.
21260 Returns NULL for invalid types. */
21261
21262 static struct type *
21263 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21264 {
21265 struct type *this_type = NULL;
21266
21267 switch (die->tag)
21268 {
21269 case DW_TAG_class_type:
21270 case DW_TAG_interface_type:
21271 case DW_TAG_structure_type:
21272 case DW_TAG_union_type:
21273 this_type = read_structure_type (die, cu);
21274 break;
21275 case DW_TAG_enumeration_type:
21276 this_type = read_enumeration_type (die, cu);
21277 break;
21278 case DW_TAG_subprogram:
21279 case DW_TAG_subroutine_type:
21280 case DW_TAG_inlined_subroutine:
21281 this_type = read_subroutine_type (die, cu);
21282 break;
21283 case DW_TAG_array_type:
21284 this_type = read_array_type (die, cu);
21285 break;
21286 case DW_TAG_set_type:
21287 this_type = read_set_type (die, cu);
21288 break;
21289 case DW_TAG_pointer_type:
21290 this_type = read_tag_pointer_type (die, cu);
21291 break;
21292 case DW_TAG_ptr_to_member_type:
21293 this_type = read_tag_ptr_to_member_type (die, cu);
21294 break;
21295 case DW_TAG_reference_type:
21296 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21297 break;
21298 case DW_TAG_rvalue_reference_type:
21299 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21300 break;
21301 case DW_TAG_const_type:
21302 this_type = read_tag_const_type (die, cu);
21303 break;
21304 case DW_TAG_volatile_type:
21305 this_type = read_tag_volatile_type (die, cu);
21306 break;
21307 case DW_TAG_restrict_type:
21308 this_type = read_tag_restrict_type (die, cu);
21309 break;
21310 case DW_TAG_string_type:
21311 this_type = read_tag_string_type (die, cu);
21312 break;
21313 case DW_TAG_typedef:
21314 this_type = read_typedef (die, cu);
21315 break;
21316 case DW_TAG_subrange_type:
21317 this_type = read_subrange_type (die, cu);
21318 break;
21319 case DW_TAG_base_type:
21320 this_type = read_base_type (die, cu);
21321 break;
21322 case DW_TAG_unspecified_type:
21323 this_type = read_unspecified_type (die, cu);
21324 break;
21325 case DW_TAG_namespace:
21326 this_type = read_namespace_type (die, cu);
21327 break;
21328 case DW_TAG_module:
21329 this_type = read_module_type (die, cu);
21330 break;
21331 case DW_TAG_atomic_type:
21332 this_type = read_tag_atomic_type (die, cu);
21333 break;
21334 default:
21335 complaint (_("unexpected tag in read_type_die: '%s'"),
21336 dwarf_tag_name (die->tag));
21337 break;
21338 }
21339
21340 return this_type;
21341 }
21342
21343 /* See if we can figure out if the class lives in a namespace. We do
21344 this by looking for a member function; its demangled name will
21345 contain namespace info, if there is any.
21346 Return the computed name or NULL.
21347 Space for the result is allocated on the objfile's obstack.
21348 This is the full-die version of guess_partial_die_structure_name.
21349 In this case we know DIE has no useful parent. */
21350
21351 static const char *
21352 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21353 {
21354 struct die_info *spec_die;
21355 struct dwarf2_cu *spec_cu;
21356 struct die_info *child;
21357 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21358
21359 spec_cu = cu;
21360 spec_die = die_specification (die, &spec_cu);
21361 if (spec_die != NULL)
21362 {
21363 die = spec_die;
21364 cu = spec_cu;
21365 }
21366
21367 for (child = die->child;
21368 child != NULL;
21369 child = child->sibling)
21370 {
21371 if (child->tag == DW_TAG_subprogram)
21372 {
21373 const char *linkage_name = dw2_linkage_name (child, cu);
21374
21375 if (linkage_name != NULL)
21376 {
21377 gdb::unique_xmalloc_ptr<char> actual_name
21378 (language_class_name_from_physname (cu->language_defn,
21379 linkage_name));
21380 const char *name = NULL;
21381
21382 if (actual_name != NULL)
21383 {
21384 const char *die_name = dwarf2_name (die, cu);
21385
21386 if (die_name != NULL
21387 && strcmp (die_name, actual_name.get ()) != 0)
21388 {
21389 /* Strip off the class name from the full name.
21390 We want the prefix. */
21391 int die_name_len = strlen (die_name);
21392 int actual_name_len = strlen (actual_name.get ());
21393 const char *ptr = actual_name.get ();
21394
21395 /* Test for '::' as a sanity check. */
21396 if (actual_name_len > die_name_len + 2
21397 && ptr[actual_name_len - die_name_len - 1] == ':')
21398 name = obstack_strndup (
21399 &objfile->per_bfd->storage_obstack,
21400 ptr, actual_name_len - die_name_len - 2);
21401 }
21402 }
21403 return name;
21404 }
21405 }
21406 }
21407
21408 return NULL;
21409 }
21410
21411 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21412 prefix part in such case. See
21413 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21414
21415 static const char *
21416 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21417 {
21418 struct attribute *attr;
21419 const char *base;
21420
21421 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21422 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21423 return NULL;
21424
21425 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21426 return NULL;
21427
21428 attr = dw2_linkage_name_attr (die, cu);
21429 if (attr == NULL || DW_STRING (attr) == NULL)
21430 return NULL;
21431
21432 /* dwarf2_name had to be already called. */
21433 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21434
21435 /* Strip the base name, keep any leading namespaces/classes. */
21436 base = strrchr (DW_STRING (attr), ':');
21437 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21438 return "";
21439
21440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21441 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21442 DW_STRING (attr),
21443 &base[-1] - DW_STRING (attr));
21444 }
21445
21446 /* Return the name of the namespace/class that DIE is defined within,
21447 or "" if we can't tell. The caller should not xfree the result.
21448
21449 For example, if we're within the method foo() in the following
21450 code:
21451
21452 namespace N {
21453 class C {
21454 void foo () {
21455 }
21456 };
21457 }
21458
21459 then determine_prefix on foo's die will return "N::C". */
21460
21461 static const char *
21462 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21463 {
21464 struct dwarf2_per_objfile *dwarf2_per_objfile
21465 = cu->per_cu->dwarf2_per_objfile;
21466 struct die_info *parent, *spec_die;
21467 struct dwarf2_cu *spec_cu;
21468 struct type *parent_type;
21469 const char *retval;
21470
21471 if (cu->language != language_cplus
21472 && cu->language != language_fortran && cu->language != language_d
21473 && cu->language != language_rust)
21474 return "";
21475
21476 retval = anonymous_struct_prefix (die, cu);
21477 if (retval)
21478 return retval;
21479
21480 /* We have to be careful in the presence of DW_AT_specification.
21481 For example, with GCC 3.4, given the code
21482
21483 namespace N {
21484 void foo() {
21485 // Definition of N::foo.
21486 }
21487 }
21488
21489 then we'll have a tree of DIEs like this:
21490
21491 1: DW_TAG_compile_unit
21492 2: DW_TAG_namespace // N
21493 3: DW_TAG_subprogram // declaration of N::foo
21494 4: DW_TAG_subprogram // definition of N::foo
21495 DW_AT_specification // refers to die #3
21496
21497 Thus, when processing die #4, we have to pretend that we're in
21498 the context of its DW_AT_specification, namely the contex of die
21499 #3. */
21500 spec_cu = cu;
21501 spec_die = die_specification (die, &spec_cu);
21502 if (spec_die == NULL)
21503 parent = die->parent;
21504 else
21505 {
21506 parent = spec_die->parent;
21507 cu = spec_cu;
21508 }
21509
21510 if (parent == NULL)
21511 return "";
21512 else if (parent->building_fullname)
21513 {
21514 const char *name;
21515 const char *parent_name;
21516
21517 /* It has been seen on RealView 2.2 built binaries,
21518 DW_TAG_template_type_param types actually _defined_ as
21519 children of the parent class:
21520
21521 enum E {};
21522 template class <class Enum> Class{};
21523 Class<enum E> class_e;
21524
21525 1: DW_TAG_class_type (Class)
21526 2: DW_TAG_enumeration_type (E)
21527 3: DW_TAG_enumerator (enum1:0)
21528 3: DW_TAG_enumerator (enum2:1)
21529 ...
21530 2: DW_TAG_template_type_param
21531 DW_AT_type DW_FORM_ref_udata (E)
21532
21533 Besides being broken debug info, it can put GDB into an
21534 infinite loop. Consider:
21535
21536 When we're building the full name for Class<E>, we'll start
21537 at Class, and go look over its template type parameters,
21538 finding E. We'll then try to build the full name of E, and
21539 reach here. We're now trying to build the full name of E,
21540 and look over the parent DIE for containing scope. In the
21541 broken case, if we followed the parent DIE of E, we'd again
21542 find Class, and once again go look at its template type
21543 arguments, etc., etc. Simply don't consider such parent die
21544 as source-level parent of this die (it can't be, the language
21545 doesn't allow it), and break the loop here. */
21546 name = dwarf2_name (die, cu);
21547 parent_name = dwarf2_name (parent, cu);
21548 complaint (_("template param type '%s' defined within parent '%s'"),
21549 name ? name : "<unknown>",
21550 parent_name ? parent_name : "<unknown>");
21551 return "";
21552 }
21553 else
21554 switch (parent->tag)
21555 {
21556 case DW_TAG_namespace:
21557 parent_type = read_type_die (parent, cu);
21558 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21559 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21560 Work around this problem here. */
21561 if (cu->language == language_cplus
21562 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21563 return "";
21564 /* We give a name to even anonymous namespaces. */
21565 return TYPE_NAME (parent_type);
21566 case DW_TAG_class_type:
21567 case DW_TAG_interface_type:
21568 case DW_TAG_structure_type:
21569 case DW_TAG_union_type:
21570 case DW_TAG_module:
21571 parent_type = read_type_die (parent, cu);
21572 if (TYPE_NAME (parent_type) != NULL)
21573 return TYPE_NAME (parent_type);
21574 else
21575 /* An anonymous structure is only allowed non-static data
21576 members; no typedefs, no member functions, et cetera.
21577 So it does not need a prefix. */
21578 return "";
21579 case DW_TAG_compile_unit:
21580 case DW_TAG_partial_unit:
21581 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21582 if (cu->language == language_cplus
21583 && !dwarf2_per_objfile->types.empty ()
21584 && die->child != NULL
21585 && (die->tag == DW_TAG_class_type
21586 || die->tag == DW_TAG_structure_type
21587 || die->tag == DW_TAG_union_type))
21588 {
21589 const char *name = guess_full_die_structure_name (die, cu);
21590 if (name != NULL)
21591 return name;
21592 }
21593 return "";
21594 case DW_TAG_subprogram:
21595 /* Nested subroutines in Fortran get a prefix with the name
21596 of the parent's subroutine. */
21597 if (cu->language == language_fortran)
21598 {
21599 if ((die->tag == DW_TAG_subprogram)
21600 && (dwarf2_name (parent, cu) != NULL))
21601 return dwarf2_name (parent, cu);
21602 }
21603 return determine_prefix (parent, cu);
21604 case DW_TAG_enumeration_type:
21605 parent_type = read_type_die (parent, cu);
21606 if (TYPE_DECLARED_CLASS (parent_type))
21607 {
21608 if (TYPE_NAME (parent_type) != NULL)
21609 return TYPE_NAME (parent_type);
21610 return "";
21611 }
21612 /* Fall through. */
21613 default:
21614 return determine_prefix (parent, cu);
21615 }
21616 }
21617
21618 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21619 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21620 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21621 an obconcat, otherwise allocate storage for the result. The CU argument is
21622 used to determine the language and hence, the appropriate separator. */
21623
21624 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21625
21626 static char *
21627 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21628 int physname, struct dwarf2_cu *cu)
21629 {
21630 const char *lead = "";
21631 const char *sep;
21632
21633 if (suffix == NULL || suffix[0] == '\0'
21634 || prefix == NULL || prefix[0] == '\0')
21635 sep = "";
21636 else if (cu->language == language_d)
21637 {
21638 /* For D, the 'main' function could be defined in any module, but it
21639 should never be prefixed. */
21640 if (strcmp (suffix, "D main") == 0)
21641 {
21642 prefix = "";
21643 sep = "";
21644 }
21645 else
21646 sep = ".";
21647 }
21648 else if (cu->language == language_fortran && physname)
21649 {
21650 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21651 DW_AT_MIPS_linkage_name is preferred and used instead. */
21652
21653 lead = "__";
21654 sep = "_MOD_";
21655 }
21656 else
21657 sep = "::";
21658
21659 if (prefix == NULL)
21660 prefix = "";
21661 if (suffix == NULL)
21662 suffix = "";
21663
21664 if (obs == NULL)
21665 {
21666 char *retval
21667 = ((char *)
21668 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21669
21670 strcpy (retval, lead);
21671 strcat (retval, prefix);
21672 strcat (retval, sep);
21673 strcat (retval, suffix);
21674 return retval;
21675 }
21676 else
21677 {
21678 /* We have an obstack. */
21679 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21680 }
21681 }
21682
21683 /* Return sibling of die, NULL if no sibling. */
21684
21685 static struct die_info *
21686 sibling_die (struct die_info *die)
21687 {
21688 return die->sibling;
21689 }
21690
21691 /* Get name of a die, return NULL if not found. */
21692
21693 static const char *
21694 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21695 struct obstack *obstack)
21696 {
21697 if (name && cu->language == language_cplus)
21698 {
21699 std::string canon_name = cp_canonicalize_string (name);
21700
21701 if (!canon_name.empty ())
21702 {
21703 if (canon_name != name)
21704 name = obstack_strdup (obstack, canon_name);
21705 }
21706 }
21707
21708 return name;
21709 }
21710
21711 /* Get name of a die, return NULL if not found.
21712 Anonymous namespaces are converted to their magic string. */
21713
21714 static const char *
21715 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21716 {
21717 struct attribute *attr;
21718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21719
21720 attr = dwarf2_attr (die, DW_AT_name, cu);
21721 if ((!attr || !DW_STRING (attr))
21722 && die->tag != DW_TAG_namespace
21723 && die->tag != DW_TAG_class_type
21724 && die->tag != DW_TAG_interface_type
21725 && die->tag != DW_TAG_structure_type
21726 && die->tag != DW_TAG_union_type)
21727 return NULL;
21728
21729 switch (die->tag)
21730 {
21731 case DW_TAG_compile_unit:
21732 case DW_TAG_partial_unit:
21733 /* Compilation units have a DW_AT_name that is a filename, not
21734 a source language identifier. */
21735 case DW_TAG_enumeration_type:
21736 case DW_TAG_enumerator:
21737 /* These tags always have simple identifiers already; no need
21738 to canonicalize them. */
21739 return DW_STRING (attr);
21740
21741 case DW_TAG_namespace:
21742 if (attr != NULL && DW_STRING (attr) != NULL)
21743 return DW_STRING (attr);
21744 return CP_ANONYMOUS_NAMESPACE_STR;
21745
21746 case DW_TAG_class_type:
21747 case DW_TAG_interface_type:
21748 case DW_TAG_structure_type:
21749 case DW_TAG_union_type:
21750 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21751 structures or unions. These were of the form "._%d" in GCC 4.1,
21752 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21753 and GCC 4.4. We work around this problem by ignoring these. */
21754 if (attr && DW_STRING (attr)
21755 && (startswith (DW_STRING (attr), "._")
21756 || startswith (DW_STRING (attr), "<anonymous")))
21757 return NULL;
21758
21759 /* GCC might emit a nameless typedef that has a linkage name. See
21760 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21761 if (!attr || DW_STRING (attr) == NULL)
21762 {
21763 attr = dw2_linkage_name_attr (die, cu);
21764 if (attr == NULL || DW_STRING (attr) == NULL)
21765 return NULL;
21766
21767 /* Avoid demangling DW_STRING (attr) the second time on a second
21768 call for the same DIE. */
21769 if (!DW_STRING_IS_CANONICAL (attr))
21770 {
21771 gdb::unique_xmalloc_ptr<char> demangled
21772 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21773
21774 const char *base;
21775
21776 /* FIXME: we already did this for the partial symbol... */
21777 DW_STRING (attr)
21778 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21779 demangled.get ());
21780 DW_STRING_IS_CANONICAL (attr) = 1;
21781
21782 /* Strip any leading namespaces/classes, keep only the base name.
21783 DW_AT_name for named DIEs does not contain the prefixes. */
21784 base = strrchr (DW_STRING (attr), ':');
21785 if (base && base > DW_STRING (attr) && base[-1] == ':')
21786 return &base[1];
21787 else
21788 return DW_STRING (attr);
21789 }
21790 }
21791 break;
21792
21793 default:
21794 break;
21795 }
21796
21797 if (!DW_STRING_IS_CANONICAL (attr))
21798 {
21799 DW_STRING (attr)
21800 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21801 &objfile->per_bfd->storage_obstack);
21802 DW_STRING_IS_CANONICAL (attr) = 1;
21803 }
21804 return DW_STRING (attr);
21805 }
21806
21807 /* Return the die that this die in an extension of, or NULL if there
21808 is none. *EXT_CU is the CU containing DIE on input, and the CU
21809 containing the return value on output. */
21810
21811 static struct die_info *
21812 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21813 {
21814 struct attribute *attr;
21815
21816 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21817 if (attr == NULL)
21818 return NULL;
21819
21820 return follow_die_ref (die, attr, ext_cu);
21821 }
21822
21823 /* A convenience function that returns an "unknown" DWARF name,
21824 including the value of V. STR is the name of the entity being
21825 printed, e.g., "TAG". */
21826
21827 static const char *
21828 dwarf_unknown (const char *str, unsigned v)
21829 {
21830 char *cell = get_print_cell ();
21831 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21832 return cell;
21833 }
21834
21835 /* Convert a DIE tag into its string name. */
21836
21837 static const char *
21838 dwarf_tag_name (unsigned tag)
21839 {
21840 const char *name = get_DW_TAG_name (tag);
21841
21842 if (name == NULL)
21843 return dwarf_unknown ("TAG", tag);
21844
21845 return name;
21846 }
21847
21848 /* Convert a DWARF attribute code into its string name. */
21849
21850 static const char *
21851 dwarf_attr_name (unsigned attr)
21852 {
21853 const char *name;
21854
21855 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21856 if (attr == DW_AT_MIPS_fde)
21857 return "DW_AT_MIPS_fde";
21858 #else
21859 if (attr == DW_AT_HP_block_index)
21860 return "DW_AT_HP_block_index";
21861 #endif
21862
21863 name = get_DW_AT_name (attr);
21864
21865 if (name == NULL)
21866 return dwarf_unknown ("AT", attr);
21867
21868 return name;
21869 }
21870
21871 /* Convert a DWARF value form code into its string name. */
21872
21873 static const char *
21874 dwarf_form_name (unsigned form)
21875 {
21876 const char *name = get_DW_FORM_name (form);
21877
21878 if (name == NULL)
21879 return dwarf_unknown ("FORM", form);
21880
21881 return name;
21882 }
21883
21884 static const char *
21885 dwarf_bool_name (unsigned mybool)
21886 {
21887 if (mybool)
21888 return "TRUE";
21889 else
21890 return "FALSE";
21891 }
21892
21893 /* Convert a DWARF type code into its string name. */
21894
21895 static const char *
21896 dwarf_type_encoding_name (unsigned enc)
21897 {
21898 const char *name = get_DW_ATE_name (enc);
21899
21900 if (name == NULL)
21901 return dwarf_unknown ("ATE", enc);
21902
21903 return name;
21904 }
21905
21906 static void
21907 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21908 {
21909 unsigned int i;
21910
21911 print_spaces (indent, f);
21912 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21913 dwarf_tag_name (die->tag), die->abbrev,
21914 sect_offset_str (die->sect_off));
21915
21916 if (die->parent != NULL)
21917 {
21918 print_spaces (indent, f);
21919 fprintf_unfiltered (f, " parent at offset: %s\n",
21920 sect_offset_str (die->parent->sect_off));
21921 }
21922
21923 print_spaces (indent, f);
21924 fprintf_unfiltered (f, " has children: %s\n",
21925 dwarf_bool_name (die->child != NULL));
21926
21927 print_spaces (indent, f);
21928 fprintf_unfiltered (f, " attributes:\n");
21929
21930 for (i = 0; i < die->num_attrs; ++i)
21931 {
21932 print_spaces (indent, f);
21933 fprintf_unfiltered (f, " %s (%s) ",
21934 dwarf_attr_name (die->attrs[i].name),
21935 dwarf_form_name (die->attrs[i].form));
21936
21937 switch (die->attrs[i].form)
21938 {
21939 case DW_FORM_addr:
21940 case DW_FORM_addrx:
21941 case DW_FORM_GNU_addr_index:
21942 fprintf_unfiltered (f, "address: ");
21943 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21944 break;
21945 case DW_FORM_block2:
21946 case DW_FORM_block4:
21947 case DW_FORM_block:
21948 case DW_FORM_block1:
21949 fprintf_unfiltered (f, "block: size %s",
21950 pulongest (DW_BLOCK (&die->attrs[i])->size));
21951 break;
21952 case DW_FORM_exprloc:
21953 fprintf_unfiltered (f, "expression: size %s",
21954 pulongest (DW_BLOCK (&die->attrs[i])->size));
21955 break;
21956 case DW_FORM_data16:
21957 fprintf_unfiltered (f, "constant of 16 bytes");
21958 break;
21959 case DW_FORM_ref_addr:
21960 fprintf_unfiltered (f, "ref address: ");
21961 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21962 break;
21963 case DW_FORM_GNU_ref_alt:
21964 fprintf_unfiltered (f, "alt ref address: ");
21965 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21966 break;
21967 case DW_FORM_ref1:
21968 case DW_FORM_ref2:
21969 case DW_FORM_ref4:
21970 case DW_FORM_ref8:
21971 case DW_FORM_ref_udata:
21972 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21973 (long) (DW_UNSND (&die->attrs[i])));
21974 break;
21975 case DW_FORM_data1:
21976 case DW_FORM_data2:
21977 case DW_FORM_data4:
21978 case DW_FORM_data8:
21979 case DW_FORM_udata:
21980 case DW_FORM_sdata:
21981 fprintf_unfiltered (f, "constant: %s",
21982 pulongest (DW_UNSND (&die->attrs[i])));
21983 break;
21984 case DW_FORM_sec_offset:
21985 fprintf_unfiltered (f, "section offset: %s",
21986 pulongest (DW_UNSND (&die->attrs[i])));
21987 break;
21988 case DW_FORM_ref_sig8:
21989 fprintf_unfiltered (f, "signature: %s",
21990 hex_string (DW_SIGNATURE (&die->attrs[i])));
21991 break;
21992 case DW_FORM_string:
21993 case DW_FORM_strp:
21994 case DW_FORM_line_strp:
21995 case DW_FORM_strx:
21996 case DW_FORM_GNU_str_index:
21997 case DW_FORM_GNU_strp_alt:
21998 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21999 DW_STRING (&die->attrs[i])
22000 ? DW_STRING (&die->attrs[i]) : "",
22001 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22002 break;
22003 case DW_FORM_flag:
22004 if (DW_UNSND (&die->attrs[i]))
22005 fprintf_unfiltered (f, "flag: TRUE");
22006 else
22007 fprintf_unfiltered (f, "flag: FALSE");
22008 break;
22009 case DW_FORM_flag_present:
22010 fprintf_unfiltered (f, "flag: TRUE");
22011 break;
22012 case DW_FORM_indirect:
22013 /* The reader will have reduced the indirect form to
22014 the "base form" so this form should not occur. */
22015 fprintf_unfiltered (f,
22016 "unexpected attribute form: DW_FORM_indirect");
22017 break;
22018 case DW_FORM_implicit_const:
22019 fprintf_unfiltered (f, "constant: %s",
22020 plongest (DW_SND (&die->attrs[i])));
22021 break;
22022 default:
22023 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22024 die->attrs[i].form);
22025 break;
22026 }
22027 fprintf_unfiltered (f, "\n");
22028 }
22029 }
22030
22031 static void
22032 dump_die_for_error (struct die_info *die)
22033 {
22034 dump_die_shallow (gdb_stderr, 0, die);
22035 }
22036
22037 static void
22038 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22039 {
22040 int indent = level * 4;
22041
22042 gdb_assert (die != NULL);
22043
22044 if (level >= max_level)
22045 return;
22046
22047 dump_die_shallow (f, indent, die);
22048
22049 if (die->child != NULL)
22050 {
22051 print_spaces (indent, f);
22052 fprintf_unfiltered (f, " Children:");
22053 if (level + 1 < max_level)
22054 {
22055 fprintf_unfiltered (f, "\n");
22056 dump_die_1 (f, level + 1, max_level, die->child);
22057 }
22058 else
22059 {
22060 fprintf_unfiltered (f,
22061 " [not printed, max nesting level reached]\n");
22062 }
22063 }
22064
22065 if (die->sibling != NULL && level > 0)
22066 {
22067 dump_die_1 (f, level, max_level, die->sibling);
22068 }
22069 }
22070
22071 /* This is called from the pdie macro in gdbinit.in.
22072 It's not static so gcc will keep a copy callable from gdb. */
22073
22074 void
22075 dump_die (struct die_info *die, int max_level)
22076 {
22077 dump_die_1 (gdb_stdlog, 0, max_level, die);
22078 }
22079
22080 static void
22081 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22082 {
22083 void **slot;
22084
22085 slot = htab_find_slot_with_hash (cu->die_hash, die,
22086 to_underlying (die->sect_off),
22087 INSERT);
22088
22089 *slot = die;
22090 }
22091
22092 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22093 required kind. */
22094
22095 static sect_offset
22096 dwarf2_get_ref_die_offset (const struct attribute *attr)
22097 {
22098 if (attr->form_is_ref ())
22099 return (sect_offset) DW_UNSND (attr);
22100
22101 complaint (_("unsupported die ref attribute form: '%s'"),
22102 dwarf_form_name (attr->form));
22103 return {};
22104 }
22105
22106 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22107 * the value held by the attribute is not constant. */
22108
22109 static LONGEST
22110 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22111 {
22112 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22113 return DW_SND (attr);
22114 else if (attr->form == DW_FORM_udata
22115 || attr->form == DW_FORM_data1
22116 || attr->form == DW_FORM_data2
22117 || attr->form == DW_FORM_data4
22118 || attr->form == DW_FORM_data8)
22119 return DW_UNSND (attr);
22120 else
22121 {
22122 /* For DW_FORM_data16 see attribute::form_is_constant. */
22123 complaint (_("Attribute value is not a constant (%s)"),
22124 dwarf_form_name (attr->form));
22125 return default_value;
22126 }
22127 }
22128
22129 /* Follow reference or signature attribute ATTR of SRC_DIE.
22130 On entry *REF_CU is the CU of SRC_DIE.
22131 On exit *REF_CU is the CU of the result. */
22132
22133 static struct die_info *
22134 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22135 struct dwarf2_cu **ref_cu)
22136 {
22137 struct die_info *die;
22138
22139 if (attr->form_is_ref ())
22140 die = follow_die_ref (src_die, attr, ref_cu);
22141 else if (attr->form == DW_FORM_ref_sig8)
22142 die = follow_die_sig (src_die, attr, ref_cu);
22143 else
22144 {
22145 dump_die_for_error (src_die);
22146 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22147 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22148 }
22149
22150 return die;
22151 }
22152
22153 /* Follow reference OFFSET.
22154 On entry *REF_CU is the CU of the source die referencing OFFSET.
22155 On exit *REF_CU is the CU of the result.
22156 Returns NULL if OFFSET is invalid. */
22157
22158 static struct die_info *
22159 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22160 struct dwarf2_cu **ref_cu)
22161 {
22162 struct die_info temp_die;
22163 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22164 struct dwarf2_per_objfile *dwarf2_per_objfile
22165 = cu->per_cu->dwarf2_per_objfile;
22166
22167 gdb_assert (cu->per_cu != NULL);
22168
22169 target_cu = cu;
22170
22171 if (cu->per_cu->is_debug_types)
22172 {
22173 /* .debug_types CUs cannot reference anything outside their CU.
22174 If they need to, they have to reference a signatured type via
22175 DW_FORM_ref_sig8. */
22176 if (!cu->header.offset_in_cu_p (sect_off))
22177 return NULL;
22178 }
22179 else if (offset_in_dwz != cu->per_cu->is_dwz
22180 || !cu->header.offset_in_cu_p (sect_off))
22181 {
22182 struct dwarf2_per_cu_data *per_cu;
22183
22184 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22185 dwarf2_per_objfile);
22186
22187 /* If necessary, add it to the queue and load its DIEs. */
22188 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22189 load_full_comp_unit (per_cu, false, cu->language);
22190
22191 target_cu = per_cu->cu;
22192 }
22193 else if (cu->dies == NULL)
22194 {
22195 /* We're loading full DIEs during partial symbol reading. */
22196 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22197 load_full_comp_unit (cu->per_cu, false, language_minimal);
22198 }
22199
22200 *ref_cu = target_cu;
22201 temp_die.sect_off = sect_off;
22202
22203 if (target_cu != cu)
22204 target_cu->ancestor = cu;
22205
22206 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22207 &temp_die,
22208 to_underlying (sect_off));
22209 }
22210
22211 /* Follow reference attribute ATTR of SRC_DIE.
22212 On entry *REF_CU is the CU of SRC_DIE.
22213 On exit *REF_CU is the CU of the result. */
22214
22215 static struct die_info *
22216 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22217 struct dwarf2_cu **ref_cu)
22218 {
22219 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22220 struct dwarf2_cu *cu = *ref_cu;
22221 struct die_info *die;
22222
22223 die = follow_die_offset (sect_off,
22224 (attr->form == DW_FORM_GNU_ref_alt
22225 || cu->per_cu->is_dwz),
22226 ref_cu);
22227 if (!die)
22228 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22229 "at %s [in module %s]"),
22230 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22231 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22232
22233 return die;
22234 }
22235
22236 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22237 Returned value is intended for DW_OP_call*. Returned
22238 dwarf2_locexpr_baton->data has lifetime of
22239 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22240
22241 struct dwarf2_locexpr_baton
22242 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22243 struct dwarf2_per_cu_data *per_cu,
22244 CORE_ADDR (*get_frame_pc) (void *baton),
22245 void *baton, bool resolve_abstract_p)
22246 {
22247 struct dwarf2_cu *cu;
22248 struct die_info *die;
22249 struct attribute *attr;
22250 struct dwarf2_locexpr_baton retval;
22251 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22252 struct objfile *objfile = dwarf2_per_objfile->objfile;
22253
22254 if (per_cu->cu == NULL)
22255 load_cu (per_cu, false);
22256 cu = per_cu->cu;
22257 if (cu == NULL)
22258 {
22259 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22260 Instead just throw an error, not much else we can do. */
22261 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22262 sect_offset_str (sect_off), objfile_name (objfile));
22263 }
22264
22265 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22266 if (!die)
22267 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22268 sect_offset_str (sect_off), objfile_name (objfile));
22269
22270 attr = dwarf2_attr (die, DW_AT_location, cu);
22271 if (!attr && resolve_abstract_p
22272 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22273 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22274 {
22275 CORE_ADDR pc = (*get_frame_pc) (baton);
22276 CORE_ADDR baseaddr = objfile->text_section_offset ();
22277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22278
22279 for (const auto &cand_off
22280 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22281 {
22282 struct dwarf2_cu *cand_cu = cu;
22283 struct die_info *cand
22284 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22285 if (!cand
22286 || !cand->parent
22287 || cand->parent->tag != DW_TAG_subprogram)
22288 continue;
22289
22290 CORE_ADDR pc_low, pc_high;
22291 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22292 if (pc_low == ((CORE_ADDR) -1))
22293 continue;
22294 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22295 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22296 if (!(pc_low <= pc && pc < pc_high))
22297 continue;
22298
22299 die = cand;
22300 attr = dwarf2_attr (die, DW_AT_location, cu);
22301 break;
22302 }
22303 }
22304
22305 if (!attr)
22306 {
22307 /* DWARF: "If there is no such attribute, then there is no effect.".
22308 DATA is ignored if SIZE is 0. */
22309
22310 retval.data = NULL;
22311 retval.size = 0;
22312 }
22313 else if (attr->form_is_section_offset ())
22314 {
22315 struct dwarf2_loclist_baton loclist_baton;
22316 CORE_ADDR pc = (*get_frame_pc) (baton);
22317 size_t size;
22318
22319 fill_in_loclist_baton (cu, &loclist_baton, attr);
22320
22321 retval.data = dwarf2_find_location_expression (&loclist_baton,
22322 &size, pc);
22323 retval.size = size;
22324 }
22325 else
22326 {
22327 if (!attr->form_is_block ())
22328 error (_("Dwarf Error: DIE at %s referenced in module %s "
22329 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22330 sect_offset_str (sect_off), objfile_name (objfile));
22331
22332 retval.data = DW_BLOCK (attr)->data;
22333 retval.size = DW_BLOCK (attr)->size;
22334 }
22335 retval.per_cu = cu->per_cu;
22336
22337 age_cached_comp_units (dwarf2_per_objfile);
22338
22339 return retval;
22340 }
22341
22342 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22343 offset. */
22344
22345 struct dwarf2_locexpr_baton
22346 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22347 struct dwarf2_per_cu_data *per_cu,
22348 CORE_ADDR (*get_frame_pc) (void *baton),
22349 void *baton)
22350 {
22351 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22352
22353 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22354 }
22355
22356 /* Write a constant of a given type as target-ordered bytes into
22357 OBSTACK. */
22358
22359 static const gdb_byte *
22360 write_constant_as_bytes (struct obstack *obstack,
22361 enum bfd_endian byte_order,
22362 struct type *type,
22363 ULONGEST value,
22364 LONGEST *len)
22365 {
22366 gdb_byte *result;
22367
22368 *len = TYPE_LENGTH (type);
22369 result = (gdb_byte *) obstack_alloc (obstack, *len);
22370 store_unsigned_integer (result, *len, byte_order, value);
22371
22372 return result;
22373 }
22374
22375 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22376 pointer to the constant bytes and set LEN to the length of the
22377 data. If memory is needed, allocate it on OBSTACK. If the DIE
22378 does not have a DW_AT_const_value, return NULL. */
22379
22380 const gdb_byte *
22381 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22382 struct dwarf2_per_cu_data *per_cu,
22383 struct obstack *obstack,
22384 LONGEST *len)
22385 {
22386 struct dwarf2_cu *cu;
22387 struct die_info *die;
22388 struct attribute *attr;
22389 const gdb_byte *result = NULL;
22390 struct type *type;
22391 LONGEST value;
22392 enum bfd_endian byte_order;
22393 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22394
22395 if (per_cu->cu == NULL)
22396 load_cu (per_cu, false);
22397 cu = per_cu->cu;
22398 if (cu == NULL)
22399 {
22400 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22401 Instead just throw an error, not much else we can do. */
22402 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22403 sect_offset_str (sect_off), objfile_name (objfile));
22404 }
22405
22406 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22407 if (!die)
22408 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22409 sect_offset_str (sect_off), objfile_name (objfile));
22410
22411 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22412 if (attr == NULL)
22413 return NULL;
22414
22415 byte_order = (bfd_big_endian (objfile->obfd)
22416 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22417
22418 switch (attr->form)
22419 {
22420 case DW_FORM_addr:
22421 case DW_FORM_addrx:
22422 case DW_FORM_GNU_addr_index:
22423 {
22424 gdb_byte *tem;
22425
22426 *len = cu->header.addr_size;
22427 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22428 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22429 result = tem;
22430 }
22431 break;
22432 case DW_FORM_string:
22433 case DW_FORM_strp:
22434 case DW_FORM_strx:
22435 case DW_FORM_GNU_str_index:
22436 case DW_FORM_GNU_strp_alt:
22437 /* DW_STRING is already allocated on the objfile obstack, point
22438 directly to it. */
22439 result = (const gdb_byte *) DW_STRING (attr);
22440 *len = strlen (DW_STRING (attr));
22441 break;
22442 case DW_FORM_block1:
22443 case DW_FORM_block2:
22444 case DW_FORM_block4:
22445 case DW_FORM_block:
22446 case DW_FORM_exprloc:
22447 case DW_FORM_data16:
22448 result = DW_BLOCK (attr)->data;
22449 *len = DW_BLOCK (attr)->size;
22450 break;
22451
22452 /* The DW_AT_const_value attributes are supposed to carry the
22453 symbol's value "represented as it would be on the target
22454 architecture." By the time we get here, it's already been
22455 converted to host endianness, so we just need to sign- or
22456 zero-extend it as appropriate. */
22457 case DW_FORM_data1:
22458 type = die_type (die, cu);
22459 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22460 if (result == NULL)
22461 result = write_constant_as_bytes (obstack, byte_order,
22462 type, value, len);
22463 break;
22464 case DW_FORM_data2:
22465 type = die_type (die, cu);
22466 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22467 if (result == NULL)
22468 result = write_constant_as_bytes (obstack, byte_order,
22469 type, value, len);
22470 break;
22471 case DW_FORM_data4:
22472 type = die_type (die, cu);
22473 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22474 if (result == NULL)
22475 result = write_constant_as_bytes (obstack, byte_order,
22476 type, value, len);
22477 break;
22478 case DW_FORM_data8:
22479 type = die_type (die, cu);
22480 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22481 if (result == NULL)
22482 result = write_constant_as_bytes (obstack, byte_order,
22483 type, value, len);
22484 break;
22485
22486 case DW_FORM_sdata:
22487 case DW_FORM_implicit_const:
22488 type = die_type (die, cu);
22489 result = write_constant_as_bytes (obstack, byte_order,
22490 type, DW_SND (attr), len);
22491 break;
22492
22493 case DW_FORM_udata:
22494 type = die_type (die, cu);
22495 result = write_constant_as_bytes (obstack, byte_order,
22496 type, DW_UNSND (attr), len);
22497 break;
22498
22499 default:
22500 complaint (_("unsupported const value attribute form: '%s'"),
22501 dwarf_form_name (attr->form));
22502 break;
22503 }
22504
22505 return result;
22506 }
22507
22508 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22509 valid type for this die is found. */
22510
22511 struct type *
22512 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22513 struct dwarf2_per_cu_data *per_cu)
22514 {
22515 struct dwarf2_cu *cu;
22516 struct die_info *die;
22517
22518 if (per_cu->cu == NULL)
22519 load_cu (per_cu, false);
22520 cu = per_cu->cu;
22521 if (!cu)
22522 return NULL;
22523
22524 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22525 if (!die)
22526 return NULL;
22527
22528 return die_type (die, cu);
22529 }
22530
22531 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22532 PER_CU. */
22533
22534 struct type *
22535 dwarf2_get_die_type (cu_offset die_offset,
22536 struct dwarf2_per_cu_data *per_cu)
22537 {
22538 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22539 return get_die_type_at_offset (die_offset_sect, per_cu);
22540 }
22541
22542 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22543 On entry *REF_CU is the CU of SRC_DIE.
22544 On exit *REF_CU is the CU of the result.
22545 Returns NULL if the referenced DIE isn't found. */
22546
22547 static struct die_info *
22548 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22549 struct dwarf2_cu **ref_cu)
22550 {
22551 struct die_info temp_die;
22552 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22553 struct die_info *die;
22554
22555 /* While it might be nice to assert sig_type->type == NULL here,
22556 we can get here for DW_AT_imported_declaration where we need
22557 the DIE not the type. */
22558
22559 /* If necessary, add it to the queue and load its DIEs. */
22560
22561 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22562 read_signatured_type (sig_type);
22563
22564 sig_cu = sig_type->per_cu.cu;
22565 gdb_assert (sig_cu != NULL);
22566 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22567 temp_die.sect_off = sig_type->type_offset_in_section;
22568 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22569 to_underlying (temp_die.sect_off));
22570 if (die)
22571 {
22572 struct dwarf2_per_objfile *dwarf2_per_objfile
22573 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22574
22575 /* For .gdb_index version 7 keep track of included TUs.
22576 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22577 if (dwarf2_per_objfile->index_table != NULL
22578 && dwarf2_per_objfile->index_table->version <= 7)
22579 {
22580 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22581 }
22582
22583 *ref_cu = sig_cu;
22584 if (sig_cu != cu)
22585 sig_cu->ancestor = cu;
22586
22587 return die;
22588 }
22589
22590 return NULL;
22591 }
22592
22593 /* Follow signatured type referenced by ATTR in SRC_DIE.
22594 On entry *REF_CU is the CU of SRC_DIE.
22595 On exit *REF_CU is the CU of the result.
22596 The result is the DIE of the type.
22597 If the referenced type cannot be found an error is thrown. */
22598
22599 static struct die_info *
22600 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22601 struct dwarf2_cu **ref_cu)
22602 {
22603 ULONGEST signature = DW_SIGNATURE (attr);
22604 struct signatured_type *sig_type;
22605 struct die_info *die;
22606
22607 gdb_assert (attr->form == DW_FORM_ref_sig8);
22608
22609 sig_type = lookup_signatured_type (*ref_cu, signature);
22610 /* sig_type will be NULL if the signatured type is missing from
22611 the debug info. */
22612 if (sig_type == NULL)
22613 {
22614 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22615 " from DIE at %s [in module %s]"),
22616 hex_string (signature), sect_offset_str (src_die->sect_off),
22617 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22618 }
22619
22620 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22621 if (die == NULL)
22622 {
22623 dump_die_for_error (src_die);
22624 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22625 " from DIE at %s [in module %s]"),
22626 hex_string (signature), sect_offset_str (src_die->sect_off),
22627 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22628 }
22629
22630 return die;
22631 }
22632
22633 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22634 reading in and processing the type unit if necessary. */
22635
22636 static struct type *
22637 get_signatured_type (struct die_info *die, ULONGEST signature,
22638 struct dwarf2_cu *cu)
22639 {
22640 struct dwarf2_per_objfile *dwarf2_per_objfile
22641 = cu->per_cu->dwarf2_per_objfile;
22642 struct signatured_type *sig_type;
22643 struct dwarf2_cu *type_cu;
22644 struct die_info *type_die;
22645 struct type *type;
22646
22647 sig_type = lookup_signatured_type (cu, signature);
22648 /* sig_type will be NULL if the signatured type is missing from
22649 the debug info. */
22650 if (sig_type == NULL)
22651 {
22652 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22653 " from DIE at %s [in module %s]"),
22654 hex_string (signature), sect_offset_str (die->sect_off),
22655 objfile_name (dwarf2_per_objfile->objfile));
22656 return build_error_marker_type (cu, die);
22657 }
22658
22659 /* If we already know the type we're done. */
22660 if (sig_type->type != NULL)
22661 return sig_type->type;
22662
22663 type_cu = cu;
22664 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22665 if (type_die != NULL)
22666 {
22667 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22668 is created. This is important, for example, because for c++ classes
22669 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22670 type = read_type_die (type_die, type_cu);
22671 if (type == NULL)
22672 {
22673 complaint (_("Dwarf Error: Cannot build signatured type %s"
22674 " referenced from DIE at %s [in module %s]"),
22675 hex_string (signature), sect_offset_str (die->sect_off),
22676 objfile_name (dwarf2_per_objfile->objfile));
22677 type = build_error_marker_type (cu, die);
22678 }
22679 }
22680 else
22681 {
22682 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22683 " from DIE at %s [in module %s]"),
22684 hex_string (signature), sect_offset_str (die->sect_off),
22685 objfile_name (dwarf2_per_objfile->objfile));
22686 type = build_error_marker_type (cu, die);
22687 }
22688 sig_type->type = type;
22689
22690 return type;
22691 }
22692
22693 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22694 reading in and processing the type unit if necessary. */
22695
22696 static struct type *
22697 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22698 struct dwarf2_cu *cu) /* ARI: editCase function */
22699 {
22700 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22701 if (attr->form_is_ref ())
22702 {
22703 struct dwarf2_cu *type_cu = cu;
22704 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22705
22706 return read_type_die (type_die, type_cu);
22707 }
22708 else if (attr->form == DW_FORM_ref_sig8)
22709 {
22710 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22711 }
22712 else
22713 {
22714 struct dwarf2_per_objfile *dwarf2_per_objfile
22715 = cu->per_cu->dwarf2_per_objfile;
22716
22717 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22718 " at %s [in module %s]"),
22719 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22720 objfile_name (dwarf2_per_objfile->objfile));
22721 return build_error_marker_type (cu, die);
22722 }
22723 }
22724
22725 /* Load the DIEs associated with type unit PER_CU into memory. */
22726
22727 static void
22728 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22729 {
22730 struct signatured_type *sig_type;
22731
22732 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22733 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22734
22735 /* We have the per_cu, but we need the signatured_type.
22736 Fortunately this is an easy translation. */
22737 gdb_assert (per_cu->is_debug_types);
22738 sig_type = (struct signatured_type *) per_cu;
22739
22740 gdb_assert (per_cu->cu == NULL);
22741
22742 read_signatured_type (sig_type);
22743
22744 gdb_assert (per_cu->cu != NULL);
22745 }
22746
22747 /* Read in a signatured type and build its CU and DIEs.
22748 If the type is a stub for the real type in a DWO file,
22749 read in the real type from the DWO file as well. */
22750
22751 static void
22752 read_signatured_type (struct signatured_type *sig_type)
22753 {
22754 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22755
22756 gdb_assert (per_cu->is_debug_types);
22757 gdb_assert (per_cu->cu == NULL);
22758
22759 cutu_reader reader (per_cu, NULL, 0, false);
22760
22761 if (!reader.dummy_p)
22762 {
22763 struct dwarf2_cu *cu = reader.cu;
22764 const gdb_byte *info_ptr = reader.info_ptr;
22765
22766 gdb_assert (cu->die_hash == NULL);
22767 cu->die_hash =
22768 htab_create_alloc_ex (cu->header.length / 12,
22769 die_hash,
22770 die_eq,
22771 NULL,
22772 &cu->comp_unit_obstack,
22773 hashtab_obstack_allocate,
22774 dummy_obstack_deallocate);
22775
22776 if (reader.comp_unit_die->has_children)
22777 reader.comp_unit_die->child
22778 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22779 reader.comp_unit_die);
22780 cu->dies = reader.comp_unit_die;
22781 /* comp_unit_die is not stored in die_hash, no need. */
22782
22783 /* We try not to read any attributes in this function, because
22784 not all CUs needed for references have been loaded yet, and
22785 symbol table processing isn't initialized. But we have to
22786 set the CU language, or we won't be able to build types
22787 correctly. Similarly, if we do not read the producer, we can
22788 not apply producer-specific interpretation. */
22789 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22790
22791 reader.keep ();
22792 }
22793
22794 sig_type->per_cu.tu_read = 1;
22795 }
22796
22797 /* Decode simple location descriptions.
22798 Given a pointer to a dwarf block that defines a location, compute
22799 the location and return the value.
22800
22801 NOTE drow/2003-11-18: This function is called in two situations
22802 now: for the address of static or global variables (partial symbols
22803 only) and for offsets into structures which are expected to be
22804 (more or less) constant. The partial symbol case should go away,
22805 and only the constant case should remain. That will let this
22806 function complain more accurately. A few special modes are allowed
22807 without complaint for global variables (for instance, global
22808 register values and thread-local values).
22809
22810 A location description containing no operations indicates that the
22811 object is optimized out. The return value is 0 for that case.
22812 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22813 callers will only want a very basic result and this can become a
22814 complaint.
22815
22816 Note that stack[0] is unused except as a default error return. */
22817
22818 static CORE_ADDR
22819 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22820 {
22821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22822 size_t i;
22823 size_t size = blk->size;
22824 const gdb_byte *data = blk->data;
22825 CORE_ADDR stack[64];
22826 int stacki;
22827 unsigned int bytes_read, unsnd;
22828 gdb_byte op;
22829
22830 i = 0;
22831 stacki = 0;
22832 stack[stacki] = 0;
22833 stack[++stacki] = 0;
22834
22835 while (i < size)
22836 {
22837 op = data[i++];
22838 switch (op)
22839 {
22840 case DW_OP_lit0:
22841 case DW_OP_lit1:
22842 case DW_OP_lit2:
22843 case DW_OP_lit3:
22844 case DW_OP_lit4:
22845 case DW_OP_lit5:
22846 case DW_OP_lit6:
22847 case DW_OP_lit7:
22848 case DW_OP_lit8:
22849 case DW_OP_lit9:
22850 case DW_OP_lit10:
22851 case DW_OP_lit11:
22852 case DW_OP_lit12:
22853 case DW_OP_lit13:
22854 case DW_OP_lit14:
22855 case DW_OP_lit15:
22856 case DW_OP_lit16:
22857 case DW_OP_lit17:
22858 case DW_OP_lit18:
22859 case DW_OP_lit19:
22860 case DW_OP_lit20:
22861 case DW_OP_lit21:
22862 case DW_OP_lit22:
22863 case DW_OP_lit23:
22864 case DW_OP_lit24:
22865 case DW_OP_lit25:
22866 case DW_OP_lit26:
22867 case DW_OP_lit27:
22868 case DW_OP_lit28:
22869 case DW_OP_lit29:
22870 case DW_OP_lit30:
22871 case DW_OP_lit31:
22872 stack[++stacki] = op - DW_OP_lit0;
22873 break;
22874
22875 case DW_OP_reg0:
22876 case DW_OP_reg1:
22877 case DW_OP_reg2:
22878 case DW_OP_reg3:
22879 case DW_OP_reg4:
22880 case DW_OP_reg5:
22881 case DW_OP_reg6:
22882 case DW_OP_reg7:
22883 case DW_OP_reg8:
22884 case DW_OP_reg9:
22885 case DW_OP_reg10:
22886 case DW_OP_reg11:
22887 case DW_OP_reg12:
22888 case DW_OP_reg13:
22889 case DW_OP_reg14:
22890 case DW_OP_reg15:
22891 case DW_OP_reg16:
22892 case DW_OP_reg17:
22893 case DW_OP_reg18:
22894 case DW_OP_reg19:
22895 case DW_OP_reg20:
22896 case DW_OP_reg21:
22897 case DW_OP_reg22:
22898 case DW_OP_reg23:
22899 case DW_OP_reg24:
22900 case DW_OP_reg25:
22901 case DW_OP_reg26:
22902 case DW_OP_reg27:
22903 case DW_OP_reg28:
22904 case DW_OP_reg29:
22905 case DW_OP_reg30:
22906 case DW_OP_reg31:
22907 stack[++stacki] = op - DW_OP_reg0;
22908 if (i < size)
22909 dwarf2_complex_location_expr_complaint ();
22910 break;
22911
22912 case DW_OP_regx:
22913 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22914 i += bytes_read;
22915 stack[++stacki] = unsnd;
22916 if (i < size)
22917 dwarf2_complex_location_expr_complaint ();
22918 break;
22919
22920 case DW_OP_addr:
22921 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22922 &bytes_read);
22923 i += bytes_read;
22924 break;
22925
22926 case DW_OP_const1u:
22927 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22928 i += 1;
22929 break;
22930
22931 case DW_OP_const1s:
22932 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22933 i += 1;
22934 break;
22935
22936 case DW_OP_const2u:
22937 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22938 i += 2;
22939 break;
22940
22941 case DW_OP_const2s:
22942 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22943 i += 2;
22944 break;
22945
22946 case DW_OP_const4u:
22947 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22948 i += 4;
22949 break;
22950
22951 case DW_OP_const4s:
22952 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22953 i += 4;
22954 break;
22955
22956 case DW_OP_const8u:
22957 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22958 i += 8;
22959 break;
22960
22961 case DW_OP_constu:
22962 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22963 &bytes_read);
22964 i += bytes_read;
22965 break;
22966
22967 case DW_OP_consts:
22968 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22969 i += bytes_read;
22970 break;
22971
22972 case DW_OP_dup:
22973 stack[stacki + 1] = stack[stacki];
22974 stacki++;
22975 break;
22976
22977 case DW_OP_plus:
22978 stack[stacki - 1] += stack[stacki];
22979 stacki--;
22980 break;
22981
22982 case DW_OP_plus_uconst:
22983 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22984 &bytes_read);
22985 i += bytes_read;
22986 break;
22987
22988 case DW_OP_minus:
22989 stack[stacki - 1] -= stack[stacki];
22990 stacki--;
22991 break;
22992
22993 case DW_OP_deref:
22994 /* If we're not the last op, then we definitely can't encode
22995 this using GDB's address_class enum. This is valid for partial
22996 global symbols, although the variable's address will be bogus
22997 in the psymtab. */
22998 if (i < size)
22999 dwarf2_complex_location_expr_complaint ();
23000 break;
23001
23002 case DW_OP_GNU_push_tls_address:
23003 case DW_OP_form_tls_address:
23004 /* The top of the stack has the offset from the beginning
23005 of the thread control block at which the variable is located. */
23006 /* Nothing should follow this operator, so the top of stack would
23007 be returned. */
23008 /* This is valid for partial global symbols, but the variable's
23009 address will be bogus in the psymtab. Make it always at least
23010 non-zero to not look as a variable garbage collected by linker
23011 which have DW_OP_addr 0. */
23012 if (i < size)
23013 dwarf2_complex_location_expr_complaint ();
23014 stack[stacki]++;
23015 break;
23016
23017 case DW_OP_GNU_uninit:
23018 break;
23019
23020 case DW_OP_addrx:
23021 case DW_OP_GNU_addr_index:
23022 case DW_OP_GNU_const_index:
23023 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23024 &bytes_read);
23025 i += bytes_read;
23026 break;
23027
23028 default:
23029 {
23030 const char *name = get_DW_OP_name (op);
23031
23032 if (name)
23033 complaint (_("unsupported stack op: '%s'"),
23034 name);
23035 else
23036 complaint (_("unsupported stack op: '%02x'"),
23037 op);
23038 }
23039
23040 return (stack[stacki]);
23041 }
23042
23043 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23044 outside of the allocated space. Also enforce minimum>0. */
23045 if (stacki >= ARRAY_SIZE (stack) - 1)
23046 {
23047 complaint (_("location description stack overflow"));
23048 return 0;
23049 }
23050
23051 if (stacki <= 0)
23052 {
23053 complaint (_("location description stack underflow"));
23054 return 0;
23055 }
23056 }
23057 return (stack[stacki]);
23058 }
23059
23060 /* memory allocation interface */
23061
23062 static struct dwarf_block *
23063 dwarf_alloc_block (struct dwarf2_cu *cu)
23064 {
23065 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23066 }
23067
23068 static struct die_info *
23069 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23070 {
23071 struct die_info *die;
23072 size_t size = sizeof (struct die_info);
23073
23074 if (num_attrs > 1)
23075 size += (num_attrs - 1) * sizeof (struct attribute);
23076
23077 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23078 memset (die, 0, sizeof (struct die_info));
23079 return (die);
23080 }
23081
23082 \f
23083 /* Macro support. */
23084
23085 static struct macro_source_file *
23086 macro_start_file (struct dwarf2_cu *cu,
23087 int file, int line,
23088 struct macro_source_file *current_file,
23089 struct line_header *lh)
23090 {
23091 /* File name relative to the compilation directory of this source file. */
23092 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23093
23094 if (! current_file)
23095 {
23096 /* Note: We don't create a macro table for this compilation unit
23097 at all until we actually get a filename. */
23098 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23099
23100 /* If we have no current file, then this must be the start_file
23101 directive for the compilation unit's main source file. */
23102 current_file = macro_set_main (macro_table, file_name.get ());
23103 macro_define_special (macro_table);
23104 }
23105 else
23106 current_file = macro_include (current_file, line, file_name.get ());
23107
23108 return current_file;
23109 }
23110
23111 static const char *
23112 consume_improper_spaces (const char *p, const char *body)
23113 {
23114 if (*p == ' ')
23115 {
23116 complaint (_("macro definition contains spaces "
23117 "in formal argument list:\n`%s'"),
23118 body);
23119
23120 while (*p == ' ')
23121 p++;
23122 }
23123
23124 return p;
23125 }
23126
23127
23128 static void
23129 parse_macro_definition (struct macro_source_file *file, int line,
23130 const char *body)
23131 {
23132 const char *p;
23133
23134 /* The body string takes one of two forms. For object-like macro
23135 definitions, it should be:
23136
23137 <macro name> " " <definition>
23138
23139 For function-like macro definitions, it should be:
23140
23141 <macro name> "() " <definition>
23142 or
23143 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23144
23145 Spaces may appear only where explicitly indicated, and in the
23146 <definition>.
23147
23148 The Dwarf 2 spec says that an object-like macro's name is always
23149 followed by a space, but versions of GCC around March 2002 omit
23150 the space when the macro's definition is the empty string.
23151
23152 The Dwarf 2 spec says that there should be no spaces between the
23153 formal arguments in a function-like macro's formal argument list,
23154 but versions of GCC around March 2002 include spaces after the
23155 commas. */
23156
23157
23158 /* Find the extent of the macro name. The macro name is terminated
23159 by either a space or null character (for an object-like macro) or
23160 an opening paren (for a function-like macro). */
23161 for (p = body; *p; p++)
23162 if (*p == ' ' || *p == '(')
23163 break;
23164
23165 if (*p == ' ' || *p == '\0')
23166 {
23167 /* It's an object-like macro. */
23168 int name_len = p - body;
23169 std::string name (body, name_len);
23170 const char *replacement;
23171
23172 if (*p == ' ')
23173 replacement = body + name_len + 1;
23174 else
23175 {
23176 dwarf2_macro_malformed_definition_complaint (body);
23177 replacement = body + name_len;
23178 }
23179
23180 macro_define_object (file, line, name.c_str (), replacement);
23181 }
23182 else if (*p == '(')
23183 {
23184 /* It's a function-like macro. */
23185 std::string name (body, p - body);
23186 int argc = 0;
23187 int argv_size = 1;
23188 char **argv = XNEWVEC (char *, argv_size);
23189
23190 p++;
23191
23192 p = consume_improper_spaces (p, body);
23193
23194 /* Parse the formal argument list. */
23195 while (*p && *p != ')')
23196 {
23197 /* Find the extent of the current argument name. */
23198 const char *arg_start = p;
23199
23200 while (*p && *p != ',' && *p != ')' && *p != ' ')
23201 p++;
23202
23203 if (! *p || p == arg_start)
23204 dwarf2_macro_malformed_definition_complaint (body);
23205 else
23206 {
23207 /* Make sure argv has room for the new argument. */
23208 if (argc >= argv_size)
23209 {
23210 argv_size *= 2;
23211 argv = XRESIZEVEC (char *, argv, argv_size);
23212 }
23213
23214 argv[argc++] = savestring (arg_start, p - arg_start);
23215 }
23216
23217 p = consume_improper_spaces (p, body);
23218
23219 /* Consume the comma, if present. */
23220 if (*p == ',')
23221 {
23222 p++;
23223
23224 p = consume_improper_spaces (p, body);
23225 }
23226 }
23227
23228 if (*p == ')')
23229 {
23230 p++;
23231
23232 if (*p == ' ')
23233 /* Perfectly formed definition, no complaints. */
23234 macro_define_function (file, line, name.c_str (),
23235 argc, (const char **) argv,
23236 p + 1);
23237 else if (*p == '\0')
23238 {
23239 /* Complain, but do define it. */
23240 dwarf2_macro_malformed_definition_complaint (body);
23241 macro_define_function (file, line, name.c_str (),
23242 argc, (const char **) argv,
23243 p);
23244 }
23245 else
23246 /* Just complain. */
23247 dwarf2_macro_malformed_definition_complaint (body);
23248 }
23249 else
23250 /* Just complain. */
23251 dwarf2_macro_malformed_definition_complaint (body);
23252
23253 {
23254 int i;
23255
23256 for (i = 0; i < argc; i++)
23257 xfree (argv[i]);
23258 }
23259 xfree (argv);
23260 }
23261 else
23262 dwarf2_macro_malformed_definition_complaint (body);
23263 }
23264
23265 /* Skip some bytes from BYTES according to the form given in FORM.
23266 Returns the new pointer. */
23267
23268 static const gdb_byte *
23269 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23270 enum dwarf_form form,
23271 unsigned int offset_size,
23272 struct dwarf2_section_info *section)
23273 {
23274 unsigned int bytes_read;
23275
23276 switch (form)
23277 {
23278 case DW_FORM_data1:
23279 case DW_FORM_flag:
23280 ++bytes;
23281 break;
23282
23283 case DW_FORM_data2:
23284 bytes += 2;
23285 break;
23286
23287 case DW_FORM_data4:
23288 bytes += 4;
23289 break;
23290
23291 case DW_FORM_data8:
23292 bytes += 8;
23293 break;
23294
23295 case DW_FORM_data16:
23296 bytes += 16;
23297 break;
23298
23299 case DW_FORM_string:
23300 read_direct_string (abfd, bytes, &bytes_read);
23301 bytes += bytes_read;
23302 break;
23303
23304 case DW_FORM_sec_offset:
23305 case DW_FORM_strp:
23306 case DW_FORM_GNU_strp_alt:
23307 bytes += offset_size;
23308 break;
23309
23310 case DW_FORM_block:
23311 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23312 bytes += bytes_read;
23313 break;
23314
23315 case DW_FORM_block1:
23316 bytes += 1 + read_1_byte (abfd, bytes);
23317 break;
23318 case DW_FORM_block2:
23319 bytes += 2 + read_2_bytes (abfd, bytes);
23320 break;
23321 case DW_FORM_block4:
23322 bytes += 4 + read_4_bytes (abfd, bytes);
23323 break;
23324
23325 case DW_FORM_addrx:
23326 case DW_FORM_sdata:
23327 case DW_FORM_strx:
23328 case DW_FORM_udata:
23329 case DW_FORM_GNU_addr_index:
23330 case DW_FORM_GNU_str_index:
23331 bytes = gdb_skip_leb128 (bytes, buffer_end);
23332 if (bytes == NULL)
23333 {
23334 dwarf2_section_buffer_overflow_complaint (section);
23335 return NULL;
23336 }
23337 break;
23338
23339 case DW_FORM_implicit_const:
23340 break;
23341
23342 default:
23343 {
23344 complaint (_("invalid form 0x%x in `%s'"),
23345 form, section->get_name ());
23346 return NULL;
23347 }
23348 }
23349
23350 return bytes;
23351 }
23352
23353 /* A helper for dwarf_decode_macros that handles skipping an unknown
23354 opcode. Returns an updated pointer to the macro data buffer; or,
23355 on error, issues a complaint and returns NULL. */
23356
23357 static const gdb_byte *
23358 skip_unknown_opcode (unsigned int opcode,
23359 const gdb_byte **opcode_definitions,
23360 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23361 bfd *abfd,
23362 unsigned int offset_size,
23363 struct dwarf2_section_info *section)
23364 {
23365 unsigned int bytes_read, i;
23366 unsigned long arg;
23367 const gdb_byte *defn;
23368
23369 if (opcode_definitions[opcode] == NULL)
23370 {
23371 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23372 opcode);
23373 return NULL;
23374 }
23375
23376 defn = opcode_definitions[opcode];
23377 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23378 defn += bytes_read;
23379
23380 for (i = 0; i < arg; ++i)
23381 {
23382 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23383 (enum dwarf_form) defn[i], offset_size,
23384 section);
23385 if (mac_ptr == NULL)
23386 {
23387 /* skip_form_bytes already issued the complaint. */
23388 return NULL;
23389 }
23390 }
23391
23392 return mac_ptr;
23393 }
23394
23395 /* A helper function which parses the header of a macro section.
23396 If the macro section is the extended (for now called "GNU") type,
23397 then this updates *OFFSET_SIZE. Returns a pointer to just after
23398 the header, or issues a complaint and returns NULL on error. */
23399
23400 static const gdb_byte *
23401 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23402 bfd *abfd,
23403 const gdb_byte *mac_ptr,
23404 unsigned int *offset_size,
23405 int section_is_gnu)
23406 {
23407 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23408
23409 if (section_is_gnu)
23410 {
23411 unsigned int version, flags;
23412
23413 version = read_2_bytes (abfd, mac_ptr);
23414 if (version != 4 && version != 5)
23415 {
23416 complaint (_("unrecognized version `%d' in .debug_macro section"),
23417 version);
23418 return NULL;
23419 }
23420 mac_ptr += 2;
23421
23422 flags = read_1_byte (abfd, mac_ptr);
23423 ++mac_ptr;
23424 *offset_size = (flags & 1) ? 8 : 4;
23425
23426 if ((flags & 2) != 0)
23427 /* We don't need the line table offset. */
23428 mac_ptr += *offset_size;
23429
23430 /* Vendor opcode descriptions. */
23431 if ((flags & 4) != 0)
23432 {
23433 unsigned int i, count;
23434
23435 count = read_1_byte (abfd, mac_ptr);
23436 ++mac_ptr;
23437 for (i = 0; i < count; ++i)
23438 {
23439 unsigned int opcode, bytes_read;
23440 unsigned long arg;
23441
23442 opcode = read_1_byte (abfd, mac_ptr);
23443 ++mac_ptr;
23444 opcode_definitions[opcode] = mac_ptr;
23445 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23446 mac_ptr += bytes_read;
23447 mac_ptr += arg;
23448 }
23449 }
23450 }
23451
23452 return mac_ptr;
23453 }
23454
23455 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23456 including DW_MACRO_import. */
23457
23458 static void
23459 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23460 bfd *abfd,
23461 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23462 struct macro_source_file *current_file,
23463 struct line_header *lh,
23464 struct dwarf2_section_info *section,
23465 int section_is_gnu, int section_is_dwz,
23466 unsigned int offset_size,
23467 htab_t include_hash)
23468 {
23469 struct dwarf2_per_objfile *dwarf2_per_objfile
23470 = cu->per_cu->dwarf2_per_objfile;
23471 struct objfile *objfile = dwarf2_per_objfile->objfile;
23472 enum dwarf_macro_record_type macinfo_type;
23473 int at_commandline;
23474 const gdb_byte *opcode_definitions[256];
23475
23476 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23477 &offset_size, section_is_gnu);
23478 if (mac_ptr == NULL)
23479 {
23480 /* We already issued a complaint. */
23481 return;
23482 }
23483
23484 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23485 GDB is still reading the definitions from command line. First
23486 DW_MACINFO_start_file will need to be ignored as it was already executed
23487 to create CURRENT_FILE for the main source holding also the command line
23488 definitions. On first met DW_MACINFO_start_file this flag is reset to
23489 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23490
23491 at_commandline = 1;
23492
23493 do
23494 {
23495 /* Do we at least have room for a macinfo type byte? */
23496 if (mac_ptr >= mac_end)
23497 {
23498 dwarf2_section_buffer_overflow_complaint (section);
23499 break;
23500 }
23501
23502 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23503 mac_ptr++;
23504
23505 /* Note that we rely on the fact that the corresponding GNU and
23506 DWARF constants are the same. */
23507 DIAGNOSTIC_PUSH
23508 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23509 switch (macinfo_type)
23510 {
23511 /* A zero macinfo type indicates the end of the macro
23512 information. */
23513 case 0:
23514 break;
23515
23516 case DW_MACRO_define:
23517 case DW_MACRO_undef:
23518 case DW_MACRO_define_strp:
23519 case DW_MACRO_undef_strp:
23520 case DW_MACRO_define_sup:
23521 case DW_MACRO_undef_sup:
23522 {
23523 unsigned int bytes_read;
23524 int line;
23525 const char *body;
23526 int is_define;
23527
23528 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23529 mac_ptr += bytes_read;
23530
23531 if (macinfo_type == DW_MACRO_define
23532 || macinfo_type == DW_MACRO_undef)
23533 {
23534 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23535 mac_ptr += bytes_read;
23536 }
23537 else
23538 {
23539 LONGEST str_offset;
23540
23541 str_offset = read_offset (abfd, mac_ptr, offset_size);
23542 mac_ptr += offset_size;
23543
23544 if (macinfo_type == DW_MACRO_define_sup
23545 || macinfo_type == DW_MACRO_undef_sup
23546 || section_is_dwz)
23547 {
23548 struct dwz_file *dwz
23549 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23550
23551 body = read_indirect_string_from_dwz (objfile,
23552 dwz, str_offset);
23553 }
23554 else
23555 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23556 abfd, str_offset);
23557 }
23558
23559 is_define = (macinfo_type == DW_MACRO_define
23560 || macinfo_type == DW_MACRO_define_strp
23561 || macinfo_type == DW_MACRO_define_sup);
23562 if (! current_file)
23563 {
23564 /* DWARF violation as no main source is present. */
23565 complaint (_("debug info with no main source gives macro %s "
23566 "on line %d: %s"),
23567 is_define ? _("definition") : _("undefinition"),
23568 line, body);
23569 break;
23570 }
23571 if ((line == 0 && !at_commandline)
23572 || (line != 0 && at_commandline))
23573 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23574 at_commandline ? _("command-line") : _("in-file"),
23575 is_define ? _("definition") : _("undefinition"),
23576 line == 0 ? _("zero") : _("non-zero"), line, body);
23577
23578 if (body == NULL)
23579 {
23580 /* Fedora's rpm-build's "debugedit" binary
23581 corrupted .debug_macro sections.
23582
23583 For more info, see
23584 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23585 complaint (_("debug info gives %s invalid macro %s "
23586 "without body (corrupted?) at line %d "
23587 "on file %s"),
23588 at_commandline ? _("command-line") : _("in-file"),
23589 is_define ? _("definition") : _("undefinition"),
23590 line, current_file->filename);
23591 }
23592 else if (is_define)
23593 parse_macro_definition (current_file, line, body);
23594 else
23595 {
23596 gdb_assert (macinfo_type == DW_MACRO_undef
23597 || macinfo_type == DW_MACRO_undef_strp
23598 || macinfo_type == DW_MACRO_undef_sup);
23599 macro_undef (current_file, line, body);
23600 }
23601 }
23602 break;
23603
23604 case DW_MACRO_start_file:
23605 {
23606 unsigned int bytes_read;
23607 int line, file;
23608
23609 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23610 mac_ptr += bytes_read;
23611 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23612 mac_ptr += bytes_read;
23613
23614 if ((line == 0 && !at_commandline)
23615 || (line != 0 && at_commandline))
23616 complaint (_("debug info gives source %d included "
23617 "from %s at %s line %d"),
23618 file, at_commandline ? _("command-line") : _("file"),
23619 line == 0 ? _("zero") : _("non-zero"), line);
23620
23621 if (at_commandline)
23622 {
23623 /* This DW_MACRO_start_file was executed in the
23624 pass one. */
23625 at_commandline = 0;
23626 }
23627 else
23628 current_file = macro_start_file (cu, file, line, current_file,
23629 lh);
23630 }
23631 break;
23632
23633 case DW_MACRO_end_file:
23634 if (! current_file)
23635 complaint (_("macro debug info has an unmatched "
23636 "`close_file' directive"));
23637 else
23638 {
23639 current_file = current_file->included_by;
23640 if (! current_file)
23641 {
23642 enum dwarf_macro_record_type next_type;
23643
23644 /* GCC circa March 2002 doesn't produce the zero
23645 type byte marking the end of the compilation
23646 unit. Complain if it's not there, but exit no
23647 matter what. */
23648
23649 /* Do we at least have room for a macinfo type byte? */
23650 if (mac_ptr >= mac_end)
23651 {
23652 dwarf2_section_buffer_overflow_complaint (section);
23653 return;
23654 }
23655
23656 /* We don't increment mac_ptr here, so this is just
23657 a look-ahead. */
23658 next_type
23659 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23660 mac_ptr);
23661 if (next_type != 0)
23662 complaint (_("no terminating 0-type entry for "
23663 "macros in `.debug_macinfo' section"));
23664
23665 return;
23666 }
23667 }
23668 break;
23669
23670 case DW_MACRO_import:
23671 case DW_MACRO_import_sup:
23672 {
23673 LONGEST offset;
23674 void **slot;
23675 bfd *include_bfd = abfd;
23676 struct dwarf2_section_info *include_section = section;
23677 const gdb_byte *include_mac_end = mac_end;
23678 int is_dwz = section_is_dwz;
23679 const gdb_byte *new_mac_ptr;
23680
23681 offset = read_offset (abfd, mac_ptr, offset_size);
23682 mac_ptr += offset_size;
23683
23684 if (macinfo_type == DW_MACRO_import_sup)
23685 {
23686 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23687
23688 dwz->macro.read (objfile);
23689
23690 include_section = &dwz->macro;
23691 include_bfd = include_section->get_bfd_owner ();
23692 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23693 is_dwz = 1;
23694 }
23695
23696 new_mac_ptr = include_section->buffer + offset;
23697 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23698
23699 if (*slot != NULL)
23700 {
23701 /* This has actually happened; see
23702 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23703 complaint (_("recursive DW_MACRO_import in "
23704 ".debug_macro section"));
23705 }
23706 else
23707 {
23708 *slot = (void *) new_mac_ptr;
23709
23710 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23711 include_mac_end, current_file, lh,
23712 section, section_is_gnu, is_dwz,
23713 offset_size, include_hash);
23714
23715 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23716 }
23717 }
23718 break;
23719
23720 case DW_MACINFO_vendor_ext:
23721 if (!section_is_gnu)
23722 {
23723 unsigned int bytes_read;
23724
23725 /* This reads the constant, but since we don't recognize
23726 any vendor extensions, we ignore it. */
23727 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23728 mac_ptr += bytes_read;
23729 read_direct_string (abfd, mac_ptr, &bytes_read);
23730 mac_ptr += bytes_read;
23731
23732 /* We don't recognize any vendor extensions. */
23733 break;
23734 }
23735 /* FALLTHROUGH */
23736
23737 default:
23738 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23739 mac_ptr, mac_end, abfd, offset_size,
23740 section);
23741 if (mac_ptr == NULL)
23742 return;
23743 break;
23744 }
23745 DIAGNOSTIC_POP
23746 } while (macinfo_type != 0);
23747 }
23748
23749 static void
23750 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23751 int section_is_gnu)
23752 {
23753 struct dwarf2_per_objfile *dwarf2_per_objfile
23754 = cu->per_cu->dwarf2_per_objfile;
23755 struct objfile *objfile = dwarf2_per_objfile->objfile;
23756 struct line_header *lh = cu->line_header;
23757 bfd *abfd;
23758 const gdb_byte *mac_ptr, *mac_end;
23759 struct macro_source_file *current_file = 0;
23760 enum dwarf_macro_record_type macinfo_type;
23761 unsigned int offset_size = cu->header.offset_size;
23762 const gdb_byte *opcode_definitions[256];
23763 void **slot;
23764 struct dwarf2_section_info *section;
23765 const char *section_name;
23766
23767 if (cu->dwo_unit != NULL)
23768 {
23769 if (section_is_gnu)
23770 {
23771 section = &cu->dwo_unit->dwo_file->sections.macro;
23772 section_name = ".debug_macro.dwo";
23773 }
23774 else
23775 {
23776 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23777 section_name = ".debug_macinfo.dwo";
23778 }
23779 }
23780 else
23781 {
23782 if (section_is_gnu)
23783 {
23784 section = &dwarf2_per_objfile->macro;
23785 section_name = ".debug_macro";
23786 }
23787 else
23788 {
23789 section = &dwarf2_per_objfile->macinfo;
23790 section_name = ".debug_macinfo";
23791 }
23792 }
23793
23794 section->read (objfile);
23795 if (section->buffer == NULL)
23796 {
23797 complaint (_("missing %s section"), section_name);
23798 return;
23799 }
23800 abfd = section->get_bfd_owner ();
23801
23802 /* First pass: Find the name of the base filename.
23803 This filename is needed in order to process all macros whose definition
23804 (or undefinition) comes from the command line. These macros are defined
23805 before the first DW_MACINFO_start_file entry, and yet still need to be
23806 associated to the base file.
23807
23808 To determine the base file name, we scan the macro definitions until we
23809 reach the first DW_MACINFO_start_file entry. We then initialize
23810 CURRENT_FILE accordingly so that any macro definition found before the
23811 first DW_MACINFO_start_file can still be associated to the base file. */
23812
23813 mac_ptr = section->buffer + offset;
23814 mac_end = section->buffer + section->size;
23815
23816 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23817 &offset_size, section_is_gnu);
23818 if (mac_ptr == NULL)
23819 {
23820 /* We already issued a complaint. */
23821 return;
23822 }
23823
23824 do
23825 {
23826 /* Do we at least have room for a macinfo type byte? */
23827 if (mac_ptr >= mac_end)
23828 {
23829 /* Complaint is printed during the second pass as GDB will probably
23830 stop the first pass earlier upon finding
23831 DW_MACINFO_start_file. */
23832 break;
23833 }
23834
23835 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23836 mac_ptr++;
23837
23838 /* Note that we rely on the fact that the corresponding GNU and
23839 DWARF constants are the same. */
23840 DIAGNOSTIC_PUSH
23841 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23842 switch (macinfo_type)
23843 {
23844 /* A zero macinfo type indicates the end of the macro
23845 information. */
23846 case 0:
23847 break;
23848
23849 case DW_MACRO_define:
23850 case DW_MACRO_undef:
23851 /* Only skip the data by MAC_PTR. */
23852 {
23853 unsigned int bytes_read;
23854
23855 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23856 mac_ptr += bytes_read;
23857 read_direct_string (abfd, mac_ptr, &bytes_read);
23858 mac_ptr += bytes_read;
23859 }
23860 break;
23861
23862 case DW_MACRO_start_file:
23863 {
23864 unsigned int bytes_read;
23865 int line, file;
23866
23867 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23868 mac_ptr += bytes_read;
23869 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23870 mac_ptr += bytes_read;
23871
23872 current_file = macro_start_file (cu, file, line, current_file, lh);
23873 }
23874 break;
23875
23876 case DW_MACRO_end_file:
23877 /* No data to skip by MAC_PTR. */
23878 break;
23879
23880 case DW_MACRO_define_strp:
23881 case DW_MACRO_undef_strp:
23882 case DW_MACRO_define_sup:
23883 case DW_MACRO_undef_sup:
23884 {
23885 unsigned int bytes_read;
23886
23887 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23888 mac_ptr += bytes_read;
23889 mac_ptr += offset_size;
23890 }
23891 break;
23892
23893 case DW_MACRO_import:
23894 case DW_MACRO_import_sup:
23895 /* Note that, according to the spec, a transparent include
23896 chain cannot call DW_MACRO_start_file. So, we can just
23897 skip this opcode. */
23898 mac_ptr += offset_size;
23899 break;
23900
23901 case DW_MACINFO_vendor_ext:
23902 /* Only skip the data by MAC_PTR. */
23903 if (!section_is_gnu)
23904 {
23905 unsigned int bytes_read;
23906
23907 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23908 mac_ptr += bytes_read;
23909 read_direct_string (abfd, mac_ptr, &bytes_read);
23910 mac_ptr += bytes_read;
23911 }
23912 /* FALLTHROUGH */
23913
23914 default:
23915 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23916 mac_ptr, mac_end, abfd, offset_size,
23917 section);
23918 if (mac_ptr == NULL)
23919 return;
23920 break;
23921 }
23922 DIAGNOSTIC_POP
23923 } while (macinfo_type != 0 && current_file == NULL);
23924
23925 /* Second pass: Process all entries.
23926
23927 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23928 command-line macro definitions/undefinitions. This flag is unset when we
23929 reach the first DW_MACINFO_start_file entry. */
23930
23931 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23932 htab_eq_pointer,
23933 NULL, xcalloc, xfree));
23934 mac_ptr = section->buffer + offset;
23935 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23936 *slot = (void *) mac_ptr;
23937 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23938 current_file, lh, section,
23939 section_is_gnu, 0, offset_size,
23940 include_hash.get ());
23941 }
23942
23943 /* Return the .debug_loc section to use for CU.
23944 For DWO files use .debug_loc.dwo. */
23945
23946 static struct dwarf2_section_info *
23947 cu_debug_loc_section (struct dwarf2_cu *cu)
23948 {
23949 struct dwarf2_per_objfile *dwarf2_per_objfile
23950 = cu->per_cu->dwarf2_per_objfile;
23951
23952 if (cu->dwo_unit)
23953 {
23954 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23955
23956 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23957 }
23958 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23959 : &dwarf2_per_objfile->loc);
23960 }
23961
23962 /* A helper function that fills in a dwarf2_loclist_baton. */
23963
23964 static void
23965 fill_in_loclist_baton (struct dwarf2_cu *cu,
23966 struct dwarf2_loclist_baton *baton,
23967 const struct attribute *attr)
23968 {
23969 struct dwarf2_per_objfile *dwarf2_per_objfile
23970 = cu->per_cu->dwarf2_per_objfile;
23971 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23972
23973 section->read (dwarf2_per_objfile->objfile);
23974
23975 baton->per_cu = cu->per_cu;
23976 gdb_assert (baton->per_cu);
23977 /* We don't know how long the location list is, but make sure we
23978 don't run off the edge of the section. */
23979 baton->size = section->size - DW_UNSND (attr);
23980 baton->data = section->buffer + DW_UNSND (attr);
23981 baton->base_address = cu->base_address;
23982 baton->from_dwo = cu->dwo_unit != NULL;
23983 }
23984
23985 static void
23986 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23987 struct dwarf2_cu *cu, int is_block)
23988 {
23989 struct dwarf2_per_objfile *dwarf2_per_objfile
23990 = cu->per_cu->dwarf2_per_objfile;
23991 struct objfile *objfile = dwarf2_per_objfile->objfile;
23992 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23993
23994 if (attr->form_is_section_offset ()
23995 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23996 the section. If so, fall through to the complaint in the
23997 other branch. */
23998 && DW_UNSND (attr) < section->get_size (objfile))
23999 {
24000 struct dwarf2_loclist_baton *baton;
24001
24002 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24003
24004 fill_in_loclist_baton (cu, baton, attr);
24005
24006 if (cu->base_known == 0)
24007 complaint (_("Location list used without "
24008 "specifying the CU base address."));
24009
24010 SYMBOL_ACLASS_INDEX (sym) = (is_block
24011 ? dwarf2_loclist_block_index
24012 : dwarf2_loclist_index);
24013 SYMBOL_LOCATION_BATON (sym) = baton;
24014 }
24015 else
24016 {
24017 struct dwarf2_locexpr_baton *baton;
24018
24019 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24020 baton->per_cu = cu->per_cu;
24021 gdb_assert (baton->per_cu);
24022
24023 if (attr->form_is_block ())
24024 {
24025 /* Note that we're just copying the block's data pointer
24026 here, not the actual data. We're still pointing into the
24027 info_buffer for SYM's objfile; right now we never release
24028 that buffer, but when we do clean up properly this may
24029 need to change. */
24030 baton->size = DW_BLOCK (attr)->size;
24031 baton->data = DW_BLOCK (attr)->data;
24032 }
24033 else
24034 {
24035 dwarf2_invalid_attrib_class_complaint ("location description",
24036 sym->natural_name ());
24037 baton->size = 0;
24038 }
24039
24040 SYMBOL_ACLASS_INDEX (sym) = (is_block
24041 ? dwarf2_locexpr_block_index
24042 : dwarf2_locexpr_index);
24043 SYMBOL_LOCATION_BATON (sym) = baton;
24044 }
24045 }
24046
24047 /* See read.h. */
24048
24049 struct objfile *
24050 dwarf2_per_cu_data::objfile () const
24051 {
24052 struct objfile *objfile = dwarf2_per_objfile->objfile;
24053
24054 /* Return the master objfile, so that we can report and look up the
24055 correct file containing this variable. */
24056 if (objfile->separate_debug_objfile_backlink)
24057 objfile = objfile->separate_debug_objfile_backlink;
24058
24059 return objfile;
24060 }
24061
24062 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24063 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24064 CU_HEADERP first. */
24065
24066 static const struct comp_unit_head *
24067 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24068 const struct dwarf2_per_cu_data *per_cu)
24069 {
24070 const gdb_byte *info_ptr;
24071
24072 if (per_cu->cu)
24073 return &per_cu->cu->header;
24074
24075 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24076
24077 memset (cu_headerp, 0, sizeof (*cu_headerp));
24078 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24079 rcuh_kind::COMPILE);
24080
24081 return cu_headerp;
24082 }
24083
24084 /* See read.h. */
24085
24086 int
24087 dwarf2_per_cu_data::addr_size () const
24088 {
24089 struct comp_unit_head cu_header_local;
24090 const struct comp_unit_head *cu_headerp;
24091
24092 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24093
24094 return cu_headerp->addr_size;
24095 }
24096
24097 /* See read.h. */
24098
24099 int
24100 dwarf2_per_cu_data::offset_size () const
24101 {
24102 struct comp_unit_head cu_header_local;
24103 const struct comp_unit_head *cu_headerp;
24104
24105 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24106
24107 return cu_headerp->offset_size;
24108 }
24109
24110 /* See read.h. */
24111
24112 int
24113 dwarf2_per_cu_data::ref_addr_size () const
24114 {
24115 struct comp_unit_head cu_header_local;
24116 const struct comp_unit_head *cu_headerp;
24117
24118 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24119
24120 if (cu_headerp->version == 2)
24121 return cu_headerp->addr_size;
24122 else
24123 return cu_headerp->offset_size;
24124 }
24125
24126 /* See read.h. */
24127
24128 CORE_ADDR
24129 dwarf2_per_cu_data::text_offset () const
24130 {
24131 struct objfile *objfile = dwarf2_per_objfile->objfile;
24132
24133 return objfile->text_section_offset ();
24134 }
24135
24136 /* See read.h. */
24137
24138 struct type *
24139 dwarf2_per_cu_data::addr_type () const
24140 {
24141 struct objfile *objfile = dwarf2_per_objfile->objfile;
24142 struct type *void_type = objfile_type (objfile)->builtin_void;
24143 struct type *addr_type = lookup_pointer_type (void_type);
24144 int addr_size = this->addr_size ();
24145
24146 if (TYPE_LENGTH (addr_type) == addr_size)
24147 return addr_type;
24148
24149 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24150 return addr_type;
24151 }
24152
24153 /* Locate the .debug_info compilation unit from CU's objfile which contains
24154 the DIE at OFFSET. Raises an error on failure. */
24155
24156 static struct dwarf2_per_cu_data *
24157 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24158 unsigned int offset_in_dwz,
24159 struct dwarf2_per_objfile *dwarf2_per_objfile)
24160 {
24161 struct dwarf2_per_cu_data *this_cu;
24162 int low, high;
24163
24164 low = 0;
24165 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24166 while (high > low)
24167 {
24168 struct dwarf2_per_cu_data *mid_cu;
24169 int mid = low + (high - low) / 2;
24170
24171 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24172 if (mid_cu->is_dwz > offset_in_dwz
24173 || (mid_cu->is_dwz == offset_in_dwz
24174 && mid_cu->sect_off + mid_cu->length >= sect_off))
24175 high = mid;
24176 else
24177 low = mid + 1;
24178 }
24179 gdb_assert (low == high);
24180 this_cu = dwarf2_per_objfile->all_comp_units[low];
24181 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24182 {
24183 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24184 error (_("Dwarf Error: could not find partial DIE containing "
24185 "offset %s [in module %s]"),
24186 sect_offset_str (sect_off),
24187 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24188
24189 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24190 <= sect_off);
24191 return dwarf2_per_objfile->all_comp_units[low-1];
24192 }
24193 else
24194 {
24195 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24196 && sect_off >= this_cu->sect_off + this_cu->length)
24197 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24198 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24199 return this_cu;
24200 }
24201 }
24202
24203 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24204
24205 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24206 : per_cu (per_cu_),
24207 mark (false),
24208 has_loclist (false),
24209 checked_producer (false),
24210 producer_is_gxx_lt_4_6 (false),
24211 producer_is_gcc_lt_4_3 (false),
24212 producer_is_icc (false),
24213 producer_is_icc_lt_14 (false),
24214 producer_is_codewarrior (false),
24215 processing_has_namespace_info (false)
24216 {
24217 per_cu->cu = this;
24218 }
24219
24220 /* Destroy a dwarf2_cu. */
24221
24222 dwarf2_cu::~dwarf2_cu ()
24223 {
24224 per_cu->cu = NULL;
24225 }
24226
24227 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24228
24229 static void
24230 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24231 enum language pretend_language)
24232 {
24233 struct attribute *attr;
24234
24235 /* Set the language we're debugging. */
24236 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24237 if (attr != nullptr)
24238 set_cu_language (DW_UNSND (attr), cu);
24239 else
24240 {
24241 cu->language = pretend_language;
24242 cu->language_defn = language_def (cu->language);
24243 }
24244
24245 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24246 }
24247
24248 /* Increase the age counter on each cached compilation unit, and free
24249 any that are too old. */
24250
24251 static void
24252 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24253 {
24254 struct dwarf2_per_cu_data *per_cu, **last_chain;
24255
24256 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24257 per_cu = dwarf2_per_objfile->read_in_chain;
24258 while (per_cu != NULL)
24259 {
24260 per_cu->cu->last_used ++;
24261 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24262 dwarf2_mark (per_cu->cu);
24263 per_cu = per_cu->cu->read_in_chain;
24264 }
24265
24266 per_cu = dwarf2_per_objfile->read_in_chain;
24267 last_chain = &dwarf2_per_objfile->read_in_chain;
24268 while (per_cu != NULL)
24269 {
24270 struct dwarf2_per_cu_data *next_cu;
24271
24272 next_cu = per_cu->cu->read_in_chain;
24273
24274 if (!per_cu->cu->mark)
24275 {
24276 delete per_cu->cu;
24277 *last_chain = next_cu;
24278 }
24279 else
24280 last_chain = &per_cu->cu->read_in_chain;
24281
24282 per_cu = next_cu;
24283 }
24284 }
24285
24286 /* Remove a single compilation unit from the cache. */
24287
24288 static void
24289 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24290 {
24291 struct dwarf2_per_cu_data *per_cu, **last_chain;
24292 struct dwarf2_per_objfile *dwarf2_per_objfile
24293 = target_per_cu->dwarf2_per_objfile;
24294
24295 per_cu = dwarf2_per_objfile->read_in_chain;
24296 last_chain = &dwarf2_per_objfile->read_in_chain;
24297 while (per_cu != NULL)
24298 {
24299 struct dwarf2_per_cu_data *next_cu;
24300
24301 next_cu = per_cu->cu->read_in_chain;
24302
24303 if (per_cu == target_per_cu)
24304 {
24305 delete per_cu->cu;
24306 per_cu->cu = NULL;
24307 *last_chain = next_cu;
24308 break;
24309 }
24310 else
24311 last_chain = &per_cu->cu->read_in_chain;
24312
24313 per_cu = next_cu;
24314 }
24315 }
24316
24317 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24318 We store these in a hash table separate from the DIEs, and preserve them
24319 when the DIEs are flushed out of cache.
24320
24321 The CU "per_cu" pointer is needed because offset alone is not enough to
24322 uniquely identify the type. A file may have multiple .debug_types sections,
24323 or the type may come from a DWO file. Furthermore, while it's more logical
24324 to use per_cu->section+offset, with Fission the section with the data is in
24325 the DWO file but we don't know that section at the point we need it.
24326 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24327 because we can enter the lookup routine, get_die_type_at_offset, from
24328 outside this file, and thus won't necessarily have PER_CU->cu.
24329 Fortunately, PER_CU is stable for the life of the objfile. */
24330
24331 struct dwarf2_per_cu_offset_and_type
24332 {
24333 const struct dwarf2_per_cu_data *per_cu;
24334 sect_offset sect_off;
24335 struct type *type;
24336 };
24337
24338 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24339
24340 static hashval_t
24341 per_cu_offset_and_type_hash (const void *item)
24342 {
24343 const struct dwarf2_per_cu_offset_and_type *ofs
24344 = (const struct dwarf2_per_cu_offset_and_type *) item;
24345
24346 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24347 }
24348
24349 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24350
24351 static int
24352 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24353 {
24354 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24355 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24356 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24357 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24358
24359 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24360 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24361 }
24362
24363 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24364 table if necessary. For convenience, return TYPE.
24365
24366 The DIEs reading must have careful ordering to:
24367 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24368 reading current DIE.
24369 * Not trying to dereference contents of still incompletely read in types
24370 while reading in other DIEs.
24371 * Enable referencing still incompletely read in types just by a pointer to
24372 the type without accessing its fields.
24373
24374 Therefore caller should follow these rules:
24375 * Try to fetch any prerequisite types we may need to build this DIE type
24376 before building the type and calling set_die_type.
24377 * After building type call set_die_type for current DIE as soon as
24378 possible before fetching more types to complete the current type.
24379 * Make the type as complete as possible before fetching more types. */
24380
24381 static struct type *
24382 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24383 {
24384 struct dwarf2_per_objfile *dwarf2_per_objfile
24385 = cu->per_cu->dwarf2_per_objfile;
24386 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24387 struct objfile *objfile = dwarf2_per_objfile->objfile;
24388 struct attribute *attr;
24389 struct dynamic_prop prop;
24390
24391 /* For Ada types, make sure that the gnat-specific data is always
24392 initialized (if not already set). There are a few types where
24393 we should not be doing so, because the type-specific area is
24394 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24395 where the type-specific area is used to store the floatformat).
24396 But this is not a problem, because the gnat-specific information
24397 is actually not needed for these types. */
24398 if (need_gnat_info (cu)
24399 && TYPE_CODE (type) != TYPE_CODE_FUNC
24400 && TYPE_CODE (type) != TYPE_CODE_FLT
24401 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24402 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24403 && TYPE_CODE (type) != TYPE_CODE_METHOD
24404 && !HAVE_GNAT_AUX_INFO (type))
24405 INIT_GNAT_SPECIFIC (type);
24406
24407 /* Read DW_AT_allocated and set in type. */
24408 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24409 if (attr != NULL && attr->form_is_block ())
24410 {
24411 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24412 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24413 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24414 }
24415 else if (attr != NULL)
24416 {
24417 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24418 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24419 sect_offset_str (die->sect_off));
24420 }
24421
24422 /* Read DW_AT_associated and set in type. */
24423 attr = dwarf2_attr (die, DW_AT_associated, cu);
24424 if (attr != NULL && attr->form_is_block ())
24425 {
24426 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24427 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24428 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24429 }
24430 else if (attr != NULL)
24431 {
24432 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24433 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24434 sect_offset_str (die->sect_off));
24435 }
24436
24437 /* Read DW_AT_data_location and set in type. */
24438 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24439 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24440 cu->per_cu->addr_type ()))
24441 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24442
24443 if (dwarf2_per_objfile->die_type_hash == NULL)
24444 dwarf2_per_objfile->die_type_hash
24445 = htab_up (htab_create_alloc (127,
24446 per_cu_offset_and_type_hash,
24447 per_cu_offset_and_type_eq,
24448 NULL, xcalloc, xfree));
24449
24450 ofs.per_cu = cu->per_cu;
24451 ofs.sect_off = die->sect_off;
24452 ofs.type = type;
24453 slot = (struct dwarf2_per_cu_offset_and_type **)
24454 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24455 if (*slot)
24456 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24457 sect_offset_str (die->sect_off));
24458 *slot = XOBNEW (&objfile->objfile_obstack,
24459 struct dwarf2_per_cu_offset_and_type);
24460 **slot = ofs;
24461 return type;
24462 }
24463
24464 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24465 or return NULL if the die does not have a saved type. */
24466
24467 static struct type *
24468 get_die_type_at_offset (sect_offset sect_off,
24469 struct dwarf2_per_cu_data *per_cu)
24470 {
24471 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24472 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24473
24474 if (dwarf2_per_objfile->die_type_hash == NULL)
24475 return NULL;
24476
24477 ofs.per_cu = per_cu;
24478 ofs.sect_off = sect_off;
24479 slot = ((struct dwarf2_per_cu_offset_and_type *)
24480 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24481 if (slot)
24482 return slot->type;
24483 else
24484 return NULL;
24485 }
24486
24487 /* Look up the type for DIE in CU in die_type_hash,
24488 or return NULL if DIE does not have a saved type. */
24489
24490 static struct type *
24491 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24492 {
24493 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24494 }
24495
24496 /* Add a dependence relationship from CU to REF_PER_CU. */
24497
24498 static void
24499 dwarf2_add_dependence (struct dwarf2_cu *cu,
24500 struct dwarf2_per_cu_data *ref_per_cu)
24501 {
24502 void **slot;
24503
24504 if (cu->dependencies == NULL)
24505 cu->dependencies
24506 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24507 NULL, &cu->comp_unit_obstack,
24508 hashtab_obstack_allocate,
24509 dummy_obstack_deallocate);
24510
24511 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24512 if (*slot == NULL)
24513 *slot = ref_per_cu;
24514 }
24515
24516 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24517 Set the mark field in every compilation unit in the
24518 cache that we must keep because we are keeping CU. */
24519
24520 static int
24521 dwarf2_mark_helper (void **slot, void *data)
24522 {
24523 struct dwarf2_per_cu_data *per_cu;
24524
24525 per_cu = (struct dwarf2_per_cu_data *) *slot;
24526
24527 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24528 reading of the chain. As such dependencies remain valid it is not much
24529 useful to track and undo them during QUIT cleanups. */
24530 if (per_cu->cu == NULL)
24531 return 1;
24532
24533 if (per_cu->cu->mark)
24534 return 1;
24535 per_cu->cu->mark = true;
24536
24537 if (per_cu->cu->dependencies != NULL)
24538 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24539
24540 return 1;
24541 }
24542
24543 /* Set the mark field in CU and in every other compilation unit in the
24544 cache that we must keep because we are keeping CU. */
24545
24546 static void
24547 dwarf2_mark (struct dwarf2_cu *cu)
24548 {
24549 if (cu->mark)
24550 return;
24551 cu->mark = true;
24552 if (cu->dependencies != NULL)
24553 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24554 }
24555
24556 static void
24557 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24558 {
24559 while (per_cu)
24560 {
24561 per_cu->cu->mark = false;
24562 per_cu = per_cu->cu->read_in_chain;
24563 }
24564 }
24565
24566 /* Trivial hash function for partial_die_info: the hash value of a DIE
24567 is its offset in .debug_info for this objfile. */
24568
24569 static hashval_t
24570 partial_die_hash (const void *item)
24571 {
24572 const struct partial_die_info *part_die
24573 = (const struct partial_die_info *) item;
24574
24575 return to_underlying (part_die->sect_off);
24576 }
24577
24578 /* Trivial comparison function for partial_die_info structures: two DIEs
24579 are equal if they have the same offset. */
24580
24581 static int
24582 partial_die_eq (const void *item_lhs, const void *item_rhs)
24583 {
24584 const struct partial_die_info *part_die_lhs
24585 = (const struct partial_die_info *) item_lhs;
24586 const struct partial_die_info *part_die_rhs
24587 = (const struct partial_die_info *) item_rhs;
24588
24589 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24590 }
24591
24592 struct cmd_list_element *set_dwarf_cmdlist;
24593 struct cmd_list_element *show_dwarf_cmdlist;
24594
24595 static void
24596 set_dwarf_cmd (const char *args, int from_tty)
24597 {
24598 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24599 gdb_stdout);
24600 }
24601
24602 static void
24603 show_dwarf_cmd (const char *args, int from_tty)
24604 {
24605 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24606 }
24607
24608 static void
24609 show_check_physname (struct ui_file *file, int from_tty,
24610 struct cmd_list_element *c, const char *value)
24611 {
24612 fprintf_filtered (file,
24613 _("Whether to check \"physname\" is %s.\n"),
24614 value);
24615 }
24616
24617 void _initialize_dwarf2_read ();
24618 void
24619 _initialize_dwarf2_read ()
24620 {
24621 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24622 Set DWARF specific variables.\n\
24623 Configure DWARF variables such as the cache size."),
24624 &set_dwarf_cmdlist, "maintenance set dwarf ",
24625 0/*allow-unknown*/, &maintenance_set_cmdlist);
24626
24627 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24628 Show DWARF specific variables.\n\
24629 Show DWARF variables such as the cache size."),
24630 &show_dwarf_cmdlist, "maintenance show dwarf ",
24631 0/*allow-unknown*/, &maintenance_show_cmdlist);
24632
24633 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24634 &dwarf_max_cache_age, _("\
24635 Set the upper bound on the age of cached DWARF compilation units."), _("\
24636 Show the upper bound on the age of cached DWARF compilation units."), _("\
24637 A higher limit means that cached compilation units will be stored\n\
24638 in memory longer, and more total memory will be used. Zero disables\n\
24639 caching, which can slow down startup."),
24640 NULL,
24641 show_dwarf_max_cache_age,
24642 &set_dwarf_cmdlist,
24643 &show_dwarf_cmdlist);
24644
24645 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24646 Set debugging of the DWARF reader."), _("\
24647 Show debugging of the DWARF reader."), _("\
24648 When enabled (non-zero), debugging messages are printed during DWARF\n\
24649 reading and symtab expansion. A value of 1 (one) provides basic\n\
24650 information. A value greater than 1 provides more verbose information."),
24651 NULL,
24652 NULL,
24653 &setdebuglist, &showdebuglist);
24654
24655 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24656 Set debugging of the DWARF DIE reader."), _("\
24657 Show debugging of the DWARF DIE reader."), _("\
24658 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24659 The value is the maximum depth to print."),
24660 NULL,
24661 NULL,
24662 &setdebuglist, &showdebuglist);
24663
24664 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24665 Set debugging of the dwarf line reader."), _("\
24666 Show debugging of the dwarf line reader."), _("\
24667 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24668 A value of 1 (one) provides basic information.\n\
24669 A value greater than 1 provides more verbose information."),
24670 NULL,
24671 NULL,
24672 &setdebuglist, &showdebuglist);
24673
24674 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24675 Set cross-checking of \"physname\" code against demangler."), _("\
24676 Show cross-checking of \"physname\" code against demangler."), _("\
24677 When enabled, GDB's internal \"physname\" code is checked against\n\
24678 the demangler."),
24679 NULL, show_check_physname,
24680 &setdebuglist, &showdebuglist);
24681
24682 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24683 no_class, &use_deprecated_index_sections, _("\
24684 Set whether to use deprecated gdb_index sections."), _("\
24685 Show whether to use deprecated gdb_index sections."), _("\
24686 When enabled, deprecated .gdb_index sections are used anyway.\n\
24687 Normally they are ignored either because of a missing feature or\n\
24688 performance issue.\n\
24689 Warning: This option must be enabled before gdb reads the file."),
24690 NULL,
24691 NULL,
24692 &setlist, &showlist);
24693
24694 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24695 &dwarf2_locexpr_funcs);
24696 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24697 &dwarf2_loclist_funcs);
24698
24699 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24700 &dwarf2_block_frame_base_locexpr_funcs);
24701 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24702 &dwarf2_block_frame_base_loclist_funcs);
24703
24704 #if GDB_SELF_TEST
24705 selftests::register_test ("dw2_expand_symtabs_matching",
24706 selftests::dw2_expand_symtabs_matching::run_test);
24707 #endif
24708 }
This page took 0.693007 seconds and 5 git commands to generate.