]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
gdb: small cleanups in dwarf2_psymtab constructors
[thirdparty/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 "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* An index into a (C++) symbol name component in a symbol name as
118 recorded in the mapped_index's symbol table. For each C++ symbol
119 in the symbol table, we record one entry for the start of each
120 component in the symbol in a table of name components, and then
121 sort the table, in order to be able to binary search symbol names,
122 ignoring leading namespaces, both completion and regular look up.
123 For example, for symbol "A::B::C", we'll have an entry that points
124 to "A::B::C", another that points to "B::C", and another for "C".
125 Note that function symbols in GDB index have no parameter
126 information, just the function/method names. You can convert a
127 name_component to a "const char *" using the
128 'mapped_index::symbol_name_at(offset_type)' method. */
129
130 struct name_component
131 {
132 /* Offset in the symbol name where the component starts. Stored as
133 a (32-bit) offset instead of a pointer to save memory and improve
134 locality on 64-bit architectures. */
135 offset_type name_offset;
136
137 /* The symbol's index in the symbol and constant pool tables of a
138 mapped_index. */
139 offset_type idx;
140 };
141
142 /* Base class containing bits shared by both .gdb_index and
143 .debug_name indexes. */
144
145 struct mapped_index_base
146 {
147 mapped_index_base () = default;
148 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
149
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params,
180 enum language lang) const;
181
182 /* Prevent deleting/destroying via a base class pointer. */
183 protected:
184 ~mapped_index_base() = default;
185 };
186
187 /* A description of the mapped index. The file format is described in
188 a comment by the code that writes the index. */
189 struct mapped_index final : public mapped_index_base
190 {
191 /* A slot/bucket in the symbol table hash. */
192 struct symbol_table_slot
193 {
194 const offset_type name;
195 const offset_type vec;
196 };
197
198 /* Index data format version. */
199 int version = 0;
200
201 /* The address table data. */
202 gdb::array_view<const gdb_byte> address_table;
203
204 /* The symbol table, implemented as a hash table. */
205 gdb::array_view<symbol_table_slot> symbol_table;
206
207 /* A pointer to the constant pool. */
208 const char *constant_pool = nullptr;
209
210 bool symbol_name_slot_invalid (offset_type idx) const override
211 {
212 const auto &bucket = this->symbol_table[idx];
213 return bucket.name == 0 && bucket.vec == 0;
214 }
215
216 /* Convenience method to get at the name of the symbol at IDX in the
217 symbol table. */
218 const char *symbol_name_at (offset_type idx) const override
219 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
220
221 size_t symbol_name_count () const override
222 { return this->symbol_table.size (); }
223 };
224
225 /* A description of the mapped .debug_names.
226 Uninitialized map has CU_COUNT 0. */
227 struct mapped_debug_names final : public mapped_index_base
228 {
229 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
230 : dwarf2_per_objfile (dwarf2_per_objfile_)
231 {}
232
233 struct dwarf2_per_objfile *dwarf2_per_objfile;
234 bfd_endian dwarf5_byte_order;
235 bool dwarf5_is_dwarf64;
236 bool augmentation_is_gdb;
237 uint8_t offset_size;
238 uint32_t cu_count = 0;
239 uint32_t tu_count, bucket_count, name_count;
240 const gdb_byte *cu_table_reordered, *tu_table_reordered;
241 const uint32_t *bucket_table_reordered, *hash_table_reordered;
242 const gdb_byte *name_table_string_offs_reordered;
243 const gdb_byte *name_table_entry_offs_reordered;
244 const gdb_byte *entry_pool;
245
246 struct index_val
247 {
248 ULONGEST dwarf_tag;
249 struct attr
250 {
251 /* Attribute name DW_IDX_*. */
252 ULONGEST dw_idx;
253
254 /* Attribute form DW_FORM_*. */
255 ULONGEST form;
256
257 /* Value if FORM is DW_FORM_implicit_const. */
258 LONGEST implicit_const;
259 };
260 std::vector<attr> attr_vec;
261 };
262
263 std::unordered_map<ULONGEST, index_val> abbrev_map;
264
265 const char *namei_to_name (uint32_t namei) const;
266
267 /* Implementation of the mapped_index_base virtual interface, for
268 the name_components cache. */
269
270 const char *symbol_name_at (offset_type idx) const override
271 { return namei_to_name (idx); }
272
273 size_t symbol_name_count () const override
274 { return this->name_count; }
275 };
276
277 /* See dwarf2read.h. */
278
279 dwarf2_per_objfile *
280 get_dwarf2_per_objfile (struct objfile *objfile)
281 {
282 return dwarf2_objfile_data_key.get (objfile);
283 }
284
285 /* Default names of the debugging sections. */
286
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_loclists", ".zdebug_loclists" },
297 { ".debug_macinfo", ".zdebug_macinfo" },
298 { ".debug_macro", ".zdebug_macro" },
299 { ".debug_str", ".zdebug_str" },
300 { ".debug_str_offsets", ".zdebug_str_offsets" },
301 { ".debug_line_str", ".zdebug_line_str" },
302 { ".debug_ranges", ".zdebug_ranges" },
303 { ".debug_rnglists", ".zdebug_rnglists" },
304 { ".debug_types", ".zdebug_types" },
305 { ".debug_addr", ".zdebug_addr" },
306 { ".debug_frame", ".zdebug_frame" },
307 { ".eh_frame", NULL },
308 { ".gdb_index", ".zgdb_index" },
309 { ".debug_names", ".zdebug_names" },
310 { ".debug_aranges", ".zdebug_aranges" },
311 23
312 };
313
314 /* List of DWO/DWP sections. */
315
316 static const struct dwop_section_names
317 {
318 struct dwarf2_section_names abbrev_dwo;
319 struct dwarf2_section_names info_dwo;
320 struct dwarf2_section_names line_dwo;
321 struct dwarf2_section_names loc_dwo;
322 struct dwarf2_section_names loclists_dwo;
323 struct dwarf2_section_names macinfo_dwo;
324 struct dwarf2_section_names macro_dwo;
325 struct dwarf2_section_names str_dwo;
326 struct dwarf2_section_names str_offsets_dwo;
327 struct dwarf2_section_names types_dwo;
328 struct dwarf2_section_names cu_index;
329 struct dwarf2_section_names tu_index;
330 }
331 dwop_section_names =
332 {
333 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
334 { ".debug_info.dwo", ".zdebug_info.dwo" },
335 { ".debug_line.dwo", ".zdebug_line.dwo" },
336 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
337 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
338 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
339 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
340 { ".debug_str.dwo", ".zdebug_str.dwo" },
341 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
342 { ".debug_types.dwo", ".zdebug_types.dwo" },
343 { ".debug_cu_index", ".zdebug_cu_index" },
344 { ".debug_tu_index", ".zdebug_tu_index" },
345 };
346
347 /* local data types */
348
349 /* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351 struct delayed_method_info
352 {
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367 };
368
369 /* Internal state when decoding a particular compilation unit. */
370 struct dwarf2_cu
371 {
372 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
373 ~dwarf2_cu ();
374
375 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
376
377 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
378 Create the set of symtabs used by this TU, or if this TU is sharing
379 symtabs with another TU and the symtabs have already been created
380 then restore those symtabs in the line header.
381 We don't need the pc/line-number mapping for type units. */
382 void setup_type_unit_groups (struct die_info *die);
383
384 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
385 buildsym_compunit constructor. */
386 struct compunit_symtab *start_symtab (const char *name,
387 const char *comp_dir,
388 CORE_ADDR low_pc);
389
390 /* Reset the builder. */
391 void reset_builder () { m_builder.reset (); }
392
393 /* The header of the compilation unit. */
394 struct comp_unit_head header {};
395
396 /* Base address of this compilation unit. */
397 gdb::optional<CORE_ADDR> base_address;
398
399 /* The language we are debugging. */
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
402
403 const char *producer = nullptr;
404
405 private:
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
408 std::unique_ptr<buildsym_compunit> m_builder;
409
410 public:
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
420 struct pending **list_in_scope = nullptr;
421
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
424 htab_t partial_dies = nullptr;
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
428 auto_obstack comp_unit_obstack;
429
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
435
436 /* Backlink to our per_cu entry. */
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
440 int last_used = 0;
441
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
444 htab_t die_hash = nullptr;
445
446 /* Full DIEs if read in. */
447 struct die_info *dies = nullptr;
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
452 htab_t dependencies = nullptr;
453
454 /* Header data from the line table, during full symbol processing. */
455 struct line_header *line_header = nullptr;
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
461 die_info *line_header_die_owner = nullptr;
462
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 std::vector<delayed_method_info> method_list;
466
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab = nullptr;
469
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
479 struct dwo_unit *dwo_unit = nullptr;
480
481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
482 Note this value comes from the Fission stub CU/TU's DIE. */
483 gdb::optional<ULONGEST> addr_base;
484
485 /* The DW_AT_rnglists_base attribute if present.
486 Note this value comes from the Fission stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base = 0;
495
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
510 /* Mark used when releasing cached dies. */
511 bool mark : 1;
512
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
517 bool has_loclist : 1;
518
519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
526 bool producer_is_icc : 1;
527 bool producer_is_icc_lt_14 : 1;
528 bool producer_is_codewarrior : 1;
529
530 /* When true, the file that we're processing is known to have
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
534 bool processing_has_namespace_info : 1;
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
555 };
556
557 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560 struct stmt_list_hash
561 {
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
566 sect_offset line_sect_off;
567 };
568
569 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572 struct type_unit_group
573 {
574 /* dwarf2read.c's main "handle" on a TU symtab.
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
577 a "per_cu" handle on the symtab. */
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 symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602 };
603
604 /* These sections are what may appear in a (real or virtual) DWO file. */
605
606 struct dwo_sections
607 {
608 struct dwarf2_section_info abbrev;
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
611 struct dwarf2_section_info loclists;
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
618 std::vector<dwarf2_section_info> types;
619 };
620
621 /* CUs/TUs in DWP/DWO files. */
622
623 struct dwo_unit
624 {
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
634 struct dwarf2_section_info *section;
635
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642 };
643
644 /* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648 enum dwp_v2_section_ids
649 {
650 DW_SECT_MIN = 1
651 };
652
653 /* Data for one DWO file.
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
663
664 struct dwo_file
665 {
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
673 const char *dwo_name = nullptr;
674
675 /* The DW_AT_comp_dir attribute. */
676 const char *comp_dir = nullptr;
677
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
680 gdb_bfd_ref_ptr dbfd;
681
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
685 struct dwo_sections sections {};
686
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
691 htab_up cus;
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
695 htab_up tus;
696 };
697
698 /* These sections are what may appear in a DWP file. */
699
700 struct dwp_sections
701 {
702 /* These are used by both DWP version 1 and 2. */
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
723 };
724
725 /* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
727
728 struct virtual_v1_dwo_sections
729 {
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
737 That is recorded here, and copied to dwo_unit.section. */
738 struct dwarf2_section_info info_or_types;
739 };
740
741 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746 struct virtual_v2_dwo_sections
747 {
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770 };
771
772 /* Contents of DWP hash tables. */
773
774 struct dwp_hash_table
775 {
776 uint32_t version, nr_columns;
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789 #define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
801 };
802
803 /* Data for one DWP file. */
804
805 struct dwp_file
806 {
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
813 /* Name of the file. */
814 const char *name;
815
816 /* File format version. */
817 int version = 0;
818
819 /* The bfd. */
820 gdb_bfd_ref_ptr dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections {};
824
825 /* Table of CUs in the file. */
826 const struct dwp_hash_table *cus = nullptr;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus = nullptr;
830
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_up loaded_cus;
833 htab_up loaded_tus;
834
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
839 };
840
841 /* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
844 struct exists to abstract away the constant parameters of die reading. */
845
846 struct die_reader_specs
847 {
848 /* The bfd of die_section. */
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
855 struct dwo_file *dwo_file;
856
857 /* The section the die comes from.
858 This is either .debug_info or .debug_types, or the .dwo variants. */
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
862 const gdb_byte *buffer;
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
866
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
869 };
870
871 /* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874 class cutu_reader : public die_reader_specs
875 {
876 public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
880 int use_existing_cu,
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
891 bool dummy_p = false;
892
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
897 private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
899 int use_existing_cu);
900
901 struct dwarf2_per_cu_data *m_this_cu;
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909 };
910
911 /* When we construct a partial symbol table entry we only
912 need this much information. */
913 struct partial_die_info : public allocate_on_obstack
914 {
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
932 /* Offset of this DIE. */
933 const sect_offset sect_off;
934
935 /* DWARF-2 tag for this DIE. */
936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
937
938 /* Assorted flags describing the data found in this DIE. */
939 const unsigned int has_children : 1;
940
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
946 unsigned int may_be_inlined : 1;
947
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
964 /* Flag set if fixup has been called on this die. */
965 unsigned int fixup_called : 1;
966
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
973 /* The name of this DIE. Normally the value of DW_AT_name, but
974 sometimes a default name for unnamed DIEs. */
975 const char *name = nullptr;
976
977 /* The linkage name, if present. */
978 const char *linkage_name = nullptr;
979
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
983 const char *scope = nullptr;
984
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
992 sect_offset sect_off;
993 } d {};
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
998
999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1000 DW_AT_sibling, if any. */
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
1003 const gdb_byte *sibling = nullptr;
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
1008 sect_offset spec_offset {};
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
1045 };
1046
1047 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1048 but this would require a corresponding change in unpack_field_as_long
1049 and friends. */
1050 static int bits_per_byte = 8;
1051
1052 /* When reading a variant or variant part, we track a bit more
1053 information about the field, and store it in an object of this
1054 type. */
1055
1056 struct variant_field
1057 {
1058 /* If we see a DW_TAG_variant, then this will be the discriminant
1059 value. */
1060 ULONGEST discriminant_value;
1061 /* If we see a DW_TAG_variant, then this will be set if this is the
1062 default branch. */
1063 bool default_branch;
1064 /* While reading a DW_TAG_variant_part, this will be set if this
1065 field is the discriminant. */
1066 bool is_discriminant;
1067 };
1068
1069 struct nextfield
1070 {
1071 int accessibility = 0;
1072 int virtuality = 0;
1073 /* Extra information to describe a variant or variant part. */
1074 struct variant_field variant {};
1075 struct field field {};
1076 };
1077
1078 struct fnfieldlist
1079 {
1080 const char *name = nullptr;
1081 std::vector<struct fn_field> fnfields;
1082 };
1083
1084 /* The routines that read and process dies for a C struct or C++ class
1085 pass lists of data member fields and lists of member function fields
1086 in an instance of a field_info structure, as defined below. */
1087 struct field_info
1088 {
1089 /* List of data member and baseclasses fields. */
1090 std::vector<struct nextfield> fields;
1091 std::vector<struct nextfield> baseclasses;
1092
1093 /* Set if the accessibility of one of the fields is not public. */
1094 int non_public_fields = 0;
1095
1096 /* Member function fieldlist array, contains name of possibly overloaded
1097 member function, number of overloaded member functions and a pointer
1098 to the head of the member function field chain. */
1099 std::vector<struct fnfieldlist> fnfieldlists;
1100
1101 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1102 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1103 std::vector<struct decl_field> typedef_field_list;
1104
1105 /* Nested types defined by this class and the number of elements in this
1106 list. */
1107 std::vector<struct decl_field> nested_types_list;
1108
1109 /* Return the total number of fields (including baseclasses). */
1110 int nfields () const
1111 {
1112 return fields.size () + baseclasses.size ();
1113 }
1114 };
1115
1116 /* Loaded secondary compilation units are kept in memory until they
1117 have not been referenced for the processing of this many
1118 compilation units. Set this to zero to disable caching. Cache
1119 sizes of up to at least twenty will improve startup time for
1120 typical inter-CU-reference binaries, at an obvious memory cost. */
1121 static int dwarf_max_cache_age = 5;
1122 static void
1123 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1124 struct cmd_list_element *c, const char *value)
1125 {
1126 fprintf_filtered (file, _("The upper bound on the age of cached "
1127 "DWARF compilation units is %s.\n"),
1128 value);
1129 }
1130 \f
1131 /* local function prototypes */
1132
1133 static void dwarf2_find_base_address (struct die_info *die,
1134 struct dwarf2_cu *cu);
1135
1136 static dwarf2_psymtab *create_partial_symtab
1137 (struct dwarf2_per_cu_data *per_cu, const char *name);
1138
1139 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1140 const gdb_byte *info_ptr,
1141 struct die_info *type_unit_die);
1142
1143 static void dwarf2_build_psymtabs_hard
1144 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1145
1146 static void scan_partial_symbols (struct partial_die_info *,
1147 CORE_ADDR *, CORE_ADDR *,
1148 int, struct dwarf2_cu *);
1149
1150 static void add_partial_symbol (struct partial_die_info *,
1151 struct dwarf2_cu *);
1152
1153 static void add_partial_namespace (struct partial_die_info *pdi,
1154 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1155 int set_addrmap, struct dwarf2_cu *cu);
1156
1157 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1158 CORE_ADDR *highpc, int set_addrmap,
1159 struct dwarf2_cu *cu);
1160
1161 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1162 struct dwarf2_cu *cu);
1163
1164 static void add_partial_subprogram (struct partial_die_info *pdi,
1165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1166 int need_pc, struct dwarf2_cu *cu);
1167
1168 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1169
1170 static struct partial_die_info *load_partial_dies
1171 (const struct die_reader_specs *, const gdb_byte *, int);
1172
1173 /* A pair of partial_die_info and compilation unit. */
1174 struct cu_partial_die_info
1175 {
1176 /* The compilation unit of the partial_die_info. */
1177 struct dwarf2_cu *cu;
1178 /* A partial_die_info. */
1179 struct partial_die_info *pdi;
1180
1181 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1182 : cu (cu),
1183 pdi (pdi)
1184 { /* Nothing. */ }
1185
1186 private:
1187 cu_partial_die_info () = delete;
1188 };
1189
1190 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1191 struct dwarf2_cu *);
1192
1193 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1194 struct attribute *, struct attr_abbrev *,
1195 const gdb_byte *, bool *need_reprocess);
1196
1197 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1198 struct attribute *attr);
1199
1200 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1201
1202 static sect_offset read_abbrev_offset
1203 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1204 struct dwarf2_section_info *, sect_offset);
1205
1206 static const char *read_indirect_string
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1208 const struct comp_unit_head *, unsigned int *);
1209
1210 static const char *read_indirect_string_at_offset
1211 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1212
1213 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1214 const gdb_byte *,
1215 unsigned int *);
1216
1217 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1218 ULONGEST str_index);
1219
1220 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1221 ULONGEST str_index);
1222
1223 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1224
1225 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1226 struct dwarf2_cu *);
1227
1228 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1229 struct dwarf2_cu *cu);
1230
1231 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1232
1233 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1234 struct dwarf2_cu *cu);
1235
1236 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1237
1238 static struct die_info *die_specification (struct die_info *die,
1239 struct dwarf2_cu **);
1240
1241 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1242 struct dwarf2_cu *cu);
1243
1244 static void dwarf_decode_lines (struct line_header *, const char *,
1245 struct dwarf2_cu *, dwarf2_psymtab *,
1246 CORE_ADDR, int decode_mapping);
1247
1248 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1249 const char *);
1250
1251 static struct symbol *new_symbol (struct die_info *, struct type *,
1252 struct dwarf2_cu *, struct symbol * = NULL);
1253
1254 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1255 struct dwarf2_cu *);
1256
1257 static void dwarf2_const_value_attr (const struct attribute *attr,
1258 struct type *type,
1259 const char *name,
1260 struct obstack *obstack,
1261 struct dwarf2_cu *cu, LONGEST *value,
1262 const gdb_byte **bytes,
1263 struct dwarf2_locexpr_baton **baton);
1264
1265 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1266
1267 static int need_gnat_info (struct dwarf2_cu *);
1268
1269 static struct type *die_descriptive_type (struct die_info *,
1270 struct dwarf2_cu *);
1271
1272 static void set_descriptive_type (struct type *, struct die_info *,
1273 struct dwarf2_cu *);
1274
1275 static struct type *die_containing_type (struct die_info *,
1276 struct dwarf2_cu *);
1277
1278 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1279 struct dwarf2_cu *);
1280
1281 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1282
1283 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1284
1285 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1286
1287 static char *typename_concat (struct obstack *obs, const char *prefix,
1288 const char *suffix, int physname,
1289 struct dwarf2_cu *cu);
1290
1291 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1292
1293 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1294
1295 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1296
1297 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1298
1299 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1302
1303 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *, dwarf2_psymtab *);
1305
1306 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1307 values. Keep the items ordered with increasing constraints compliance. */
1308 enum pc_bounds_kind
1309 {
1310 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1311 PC_BOUNDS_NOT_PRESENT,
1312
1313 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1314 were present but they do not form a valid range of PC addresses. */
1315 PC_BOUNDS_INVALID,
1316
1317 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1318 PC_BOUNDS_RANGES,
1319
1320 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1321 PC_BOUNDS_HIGH_LOW,
1322 };
1323
1324 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1325 CORE_ADDR *, CORE_ADDR *,
1326 struct dwarf2_cu *,
1327 dwarf2_psymtab *);
1328
1329 static void get_scope_pc_bounds (struct die_info *,
1330 CORE_ADDR *, CORE_ADDR *,
1331 struct dwarf2_cu *);
1332
1333 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1334 CORE_ADDR, struct dwarf2_cu *);
1335
1336 static void dwarf2_add_field (struct field_info *, struct die_info *,
1337 struct dwarf2_cu *);
1338
1339 static void dwarf2_attach_fields_to_type (struct field_info *,
1340 struct type *, struct dwarf2_cu *);
1341
1342 static void dwarf2_add_member_fn (struct field_info *,
1343 struct die_info *, struct type *,
1344 struct dwarf2_cu *);
1345
1346 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1347 struct type *,
1348 struct dwarf2_cu *);
1349
1350 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1351
1352 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1355
1356 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1357
1358 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1359
1360 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1361
1362 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static struct type *read_module_type (struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
1367 static const char *namespace_name (struct die_info *die,
1368 int *is_anonymous, struct dwarf2_cu *);
1369
1370 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1371
1372 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1373
1374 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1375 struct dwarf2_cu *);
1376
1377 static struct die_info *read_die_and_siblings_1
1378 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1379 struct die_info *);
1380
1381 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1382 const gdb_byte *info_ptr,
1383 const gdb_byte **new_info_ptr,
1384 struct die_info *parent);
1385
1386 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1387 struct die_info **, const gdb_byte *,
1388 int);
1389
1390 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1391 struct die_info **, const gdb_byte *);
1392
1393 static void process_die (struct die_info *, struct dwarf2_cu *);
1394
1395 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1396 struct objfile *);
1397
1398 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1399
1400 static const char *dwarf2_full_name (const char *name,
1401 struct die_info *die,
1402 struct dwarf2_cu *cu);
1403
1404 static const char *dwarf2_physname (const char *name, struct die_info *die,
1405 struct dwarf2_cu *cu);
1406
1407 static struct die_info *dwarf2_extension (struct die_info *die,
1408 struct dwarf2_cu **);
1409
1410 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1411
1412 static void dump_die_for_error (struct die_info *);
1413
1414 static void dump_die_1 (struct ui_file *, int level, int max_level,
1415 struct die_info *);
1416
1417 /*static*/ void dump_die (struct die_info *, int max_level);
1418
1419 static void store_in_ref_table (struct die_info *,
1420 struct dwarf2_cu *);
1421
1422 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1423 const struct attribute *,
1424 struct dwarf2_cu **);
1425
1426 static struct die_info *follow_die_ref (struct die_info *,
1427 const struct attribute *,
1428 struct dwarf2_cu **);
1429
1430 static struct die_info *follow_die_sig (struct die_info *,
1431 const struct attribute *,
1432 struct dwarf2_cu **);
1433
1434 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1435 struct dwarf2_cu *);
1436
1437 static struct type *get_DW_AT_signature_type (struct die_info *,
1438 const struct attribute *,
1439 struct dwarf2_cu *);
1440
1441 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1442
1443 static void read_signatured_type (struct signatured_type *);
1444
1445 static int attr_to_dynamic_prop (const struct attribute *attr,
1446 struct die_info *die, struct dwarf2_cu *cu,
1447 struct dynamic_prop *prop, struct type *type);
1448
1449 /* memory allocation interface */
1450
1451 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1452
1453 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1454
1455 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1456
1457 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1458 struct dwarf2_loclist_baton *baton,
1459 const struct attribute *attr);
1460
1461 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1462 struct symbol *sym,
1463 struct dwarf2_cu *cu,
1464 int is_block);
1465
1466 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1467 const gdb_byte *info_ptr,
1468 struct abbrev_info *abbrev);
1469
1470 static hashval_t partial_die_hash (const void *item);
1471
1472 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1473
1474 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1475 (sect_offset sect_off, unsigned int offset_in_dwz,
1476 struct dwarf2_per_objfile *dwarf2_per_objfile);
1477
1478 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1479 struct die_info *comp_unit_die,
1480 enum language pretend_language);
1481
1482 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1483
1484 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1485
1486 static struct type *set_die_type (struct die_info *, struct type *,
1487 struct dwarf2_cu *);
1488
1489 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1490
1491 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1492
1493 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1494 enum language);
1495
1496 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1497 enum language);
1498
1499 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1500 enum language);
1501
1502 static void dwarf2_add_dependence (struct dwarf2_cu *,
1503 struct dwarf2_per_cu_data *);
1504
1505 static void dwarf2_mark (struct dwarf2_cu *);
1506
1507 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1508
1509 static struct type *get_die_type_at_offset (sect_offset,
1510 struct dwarf2_per_cu_data *);
1511
1512 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1513
1514 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1515 enum language pretend_language);
1516
1517 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1518
1519 /* Class, the destructor of which frees all allocated queue entries. This
1520 will only have work to do if an error was thrown while processing the
1521 dwarf. If no error was thrown then the queue entries should have all
1522 been processed, and freed, as we went along. */
1523
1524 class dwarf2_queue_guard
1525 {
1526 public:
1527 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1528 : m_per_objfile (per_objfile)
1529 {
1530 }
1531
1532 /* Free any entries remaining on the queue. There should only be
1533 entries left if we hit an error while processing the dwarf. */
1534 ~dwarf2_queue_guard ()
1535 {
1536 /* Ensure that no memory is allocated by the queue. */
1537 std::queue<dwarf2_queue_item> empty;
1538 std::swap (m_per_objfile->queue, empty);
1539 }
1540
1541 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1542
1543 private:
1544 dwarf2_per_objfile *m_per_objfile;
1545 };
1546
1547 dwarf2_queue_item::~dwarf2_queue_item ()
1548 {
1549 /* Anything still marked queued is likely to be in an
1550 inconsistent state, so discard it. */
1551 if (per_cu->queued)
1552 {
1553 if (per_cu->cu != NULL)
1554 free_one_cached_comp_unit (per_cu);
1555 per_cu->queued = 0;
1556 }
1557 }
1558
1559 /* The return type of find_file_and_directory. Note, the enclosed
1560 string pointers are only valid while this object is valid. */
1561
1562 struct file_and_directory
1563 {
1564 /* The filename. This is never NULL. */
1565 const char *name;
1566
1567 /* The compilation directory. NULL if not known. If we needed to
1568 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1569 points directly to the DW_AT_comp_dir string attribute owned by
1570 the obstack that owns the DIE. */
1571 const char *comp_dir;
1572
1573 /* If we needed to build a new string for comp_dir, this is what
1574 owns the storage. */
1575 std::string comp_dir_storage;
1576 };
1577
1578 static file_and_directory find_file_and_directory (struct die_info *die,
1579 struct dwarf2_cu *cu);
1580
1581 static htab_up allocate_signatured_type_table ();
1582
1583 static htab_up allocate_dwo_unit_table ();
1584
1585 static struct dwo_unit *lookup_dwo_unit_in_dwp
1586 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1587 struct dwp_file *dwp_file, const char *comp_dir,
1588 ULONGEST signature, int is_debug_types);
1589
1590 static struct dwp_file *get_dwp_file
1591 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1592
1593 static struct dwo_unit *lookup_dwo_comp_unit
1594 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1595
1596 static struct dwo_unit *lookup_dwo_type_unit
1597 (struct signatured_type *, const char *, const char *);
1598
1599 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1600
1601 /* A unique pointer to a dwo_file. */
1602
1603 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1604
1605 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1606
1607 static void check_producer (struct dwarf2_cu *cu);
1608
1609 static void free_line_header_voidp (void *arg);
1610 \f
1611 /* Various complaints about symbol reading that don't abort the process. */
1612
1613 static void
1614 dwarf2_debug_line_missing_file_complaint (void)
1615 {
1616 complaint (_(".debug_line section has line data without a file"));
1617 }
1618
1619 static void
1620 dwarf2_debug_line_missing_end_sequence_complaint (void)
1621 {
1622 complaint (_(".debug_line section has line "
1623 "program sequence without an end"));
1624 }
1625
1626 static void
1627 dwarf2_complex_location_expr_complaint (void)
1628 {
1629 complaint (_("location expression too complex"));
1630 }
1631
1632 static void
1633 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1634 int arg3)
1635 {
1636 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1637 arg1, arg2, arg3);
1638 }
1639
1640 static void
1641 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1642 {
1643 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1644 arg1, arg2);
1645 }
1646
1647 /* Hash function for line_header_hash. */
1648
1649 static hashval_t
1650 line_header_hash (const struct line_header *ofs)
1651 {
1652 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1653 }
1654
1655 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1656
1657 static hashval_t
1658 line_header_hash_voidp (const void *item)
1659 {
1660 const struct line_header *ofs = (const struct line_header *) item;
1661
1662 return line_header_hash (ofs);
1663 }
1664
1665 /* Equality function for line_header_hash. */
1666
1667 static int
1668 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1669 {
1670 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1671 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1672
1673 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1674 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1675 }
1676
1677 \f
1678
1679 /* See declaration. */
1680
1681 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1682 const dwarf2_debug_sections *names,
1683 bool can_copy_)
1684 : objfile (objfile_),
1685 can_copy (can_copy_)
1686 {
1687 if (names == NULL)
1688 names = &dwarf2_elf_names;
1689
1690 bfd *obfd = objfile->obfd;
1691
1692 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1693 locate_sections (obfd, sec, *names);
1694 }
1695
1696 dwarf2_per_objfile::~dwarf2_per_objfile ()
1697 {
1698 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1699 free_cached_comp_units ();
1700
1701 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1702 per_cu->imported_symtabs_free ();
1703
1704 for (signatured_type *sig_type : all_type_units)
1705 sig_type->per_cu.imported_symtabs_free ();
1706
1707 /* Everything else should be on the objfile obstack. */
1708 }
1709
1710 /* See declaration. */
1711
1712 void
1713 dwarf2_per_objfile::free_cached_comp_units ()
1714 {
1715 dwarf2_per_cu_data *per_cu = read_in_chain;
1716 dwarf2_per_cu_data **last_chain = &read_in_chain;
1717 while (per_cu != NULL)
1718 {
1719 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1720
1721 delete per_cu->cu;
1722 *last_chain = next_cu;
1723 per_cu = next_cu;
1724 }
1725 }
1726
1727 /* A helper class that calls free_cached_comp_units on
1728 destruction. */
1729
1730 class free_cached_comp_units
1731 {
1732 public:
1733
1734 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1735 : m_per_objfile (per_objfile)
1736 {
1737 }
1738
1739 ~free_cached_comp_units ()
1740 {
1741 m_per_objfile->free_cached_comp_units ();
1742 }
1743
1744 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1745
1746 private:
1747
1748 dwarf2_per_objfile *m_per_objfile;
1749 };
1750
1751 /* Try to locate the sections we need for DWARF 2 debugging
1752 information and return true if we have enough to do something.
1753 NAMES points to the dwarf2 section names, or is NULL if the standard
1754 ELF names are used. CAN_COPY is true for formats where symbol
1755 interposition is possible and so symbol values must follow copy
1756 relocation rules. */
1757
1758 int
1759 dwarf2_has_info (struct objfile *objfile,
1760 const struct dwarf2_debug_sections *names,
1761 bool can_copy)
1762 {
1763 if (objfile->flags & OBJF_READNEVER)
1764 return 0;
1765
1766 struct dwarf2_per_objfile *dwarf2_per_objfile
1767 = get_dwarf2_per_objfile (objfile);
1768
1769 if (dwarf2_per_objfile == NULL)
1770 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1771 names,
1772 can_copy);
1773
1774 return (!dwarf2_per_objfile->info.is_virtual
1775 && dwarf2_per_objfile->info.s.section != NULL
1776 && !dwarf2_per_objfile->abbrev.is_virtual
1777 && dwarf2_per_objfile->abbrev.s.section != NULL);
1778 }
1779
1780 /* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
1782
1783 static int
1784 section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
1786 {
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
1794 }
1795
1796 /* See declaration. */
1797
1798 void
1799 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1800 const dwarf2_debug_sections &names)
1801 {
1802 flagword aflag = bfd_section_flags (sectp);
1803
1804 if ((aflag & SEC_HAS_CONTENTS) == 0)
1805 {
1806 }
1807 else if (elf_section_data (sectp)->this_hdr.sh_size
1808 > bfd_get_file_size (abfd))
1809 {
1810 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1811 warning (_("Discarding section %s which has a section size (%s"
1812 ") larger than the file size [in module %s]"),
1813 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1814 bfd_get_filename (abfd));
1815 }
1816 else if (section_is_p (sectp->name, &names.info))
1817 {
1818 this->info.s.section = sectp;
1819 this->info.size = bfd_section_size (sectp);
1820 }
1821 else if (section_is_p (sectp->name, &names.abbrev))
1822 {
1823 this->abbrev.s.section = sectp;
1824 this->abbrev.size = bfd_section_size (sectp);
1825 }
1826 else if (section_is_p (sectp->name, &names.line))
1827 {
1828 this->line.s.section = sectp;
1829 this->line.size = bfd_section_size (sectp);
1830 }
1831 else if (section_is_p (sectp->name, &names.loc))
1832 {
1833 this->loc.s.section = sectp;
1834 this->loc.size = bfd_section_size (sectp);
1835 }
1836 else if (section_is_p (sectp->name, &names.loclists))
1837 {
1838 this->loclists.s.section = sectp;
1839 this->loclists.size = bfd_section_size (sectp);
1840 }
1841 else if (section_is_p (sectp->name, &names.macinfo))
1842 {
1843 this->macinfo.s.section = sectp;
1844 this->macinfo.size = bfd_section_size (sectp);
1845 }
1846 else if (section_is_p (sectp->name, &names.macro))
1847 {
1848 this->macro.s.section = sectp;
1849 this->macro.size = bfd_section_size (sectp);
1850 }
1851 else if (section_is_p (sectp->name, &names.str))
1852 {
1853 this->str.s.section = sectp;
1854 this->str.size = bfd_section_size (sectp);
1855 }
1856 else if (section_is_p (sectp->name, &names.str_offsets))
1857 {
1858 this->str_offsets.s.section = sectp;
1859 this->str_offsets.size = bfd_section_size (sectp);
1860 }
1861 else if (section_is_p (sectp->name, &names.line_str))
1862 {
1863 this->line_str.s.section = sectp;
1864 this->line_str.size = bfd_section_size (sectp);
1865 }
1866 else if (section_is_p (sectp->name, &names.addr))
1867 {
1868 this->addr.s.section = sectp;
1869 this->addr.size = bfd_section_size (sectp);
1870 }
1871 else if (section_is_p (sectp->name, &names.frame))
1872 {
1873 this->frame.s.section = sectp;
1874 this->frame.size = bfd_section_size (sectp);
1875 }
1876 else if (section_is_p (sectp->name, &names.eh_frame))
1877 {
1878 this->eh_frame.s.section = sectp;
1879 this->eh_frame.size = bfd_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names.ranges))
1882 {
1883 this->ranges.s.section = sectp;
1884 this->ranges.size = bfd_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names.rnglists))
1887 {
1888 this->rnglists.s.section = sectp;
1889 this->rnglists.size = bfd_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names.types))
1892 {
1893 struct dwarf2_section_info type_section;
1894
1895 memset (&type_section, 0, sizeof (type_section));
1896 type_section.s.section = sectp;
1897 type_section.size = bfd_section_size (sectp);
1898
1899 this->types.push_back (type_section);
1900 }
1901 else if (section_is_p (sectp->name, &names.gdb_index))
1902 {
1903 this->gdb_index.s.section = sectp;
1904 this->gdb_index.size = bfd_section_size (sectp);
1905 }
1906 else if (section_is_p (sectp->name, &names.debug_names))
1907 {
1908 this->debug_names.s.section = sectp;
1909 this->debug_names.size = bfd_section_size (sectp);
1910 }
1911 else if (section_is_p (sectp->name, &names.debug_aranges))
1912 {
1913 this->debug_aranges.s.section = sectp;
1914 this->debug_aranges.size = bfd_section_size (sectp);
1915 }
1916
1917 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1918 && bfd_section_vma (sectp) == 0)
1919 this->has_section_at_zero = true;
1920 }
1921
1922 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1923 SECTION_NAME. */
1924
1925 void
1926 dwarf2_get_section_info (struct objfile *objfile,
1927 enum dwarf2_section_enum sect,
1928 asection **sectp, const gdb_byte **bufp,
1929 bfd_size_type *sizep)
1930 {
1931 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1932 struct dwarf2_section_info *info;
1933
1934 /* We may see an objfile without any DWARF, in which case we just
1935 return nothing. */
1936 if (data == NULL)
1937 {
1938 *sectp = NULL;
1939 *bufp = NULL;
1940 *sizep = 0;
1941 return;
1942 }
1943 switch (sect)
1944 {
1945 case DWARF2_DEBUG_FRAME:
1946 info = &data->frame;
1947 break;
1948 case DWARF2_EH_FRAME:
1949 info = &data->eh_frame;
1950 break;
1951 default:
1952 gdb_assert_not_reached ("unexpected section");
1953 }
1954
1955 info->read (objfile);
1956
1957 *sectp = info->get_bfd_section ();
1958 *bufp = info->buffer;
1959 *sizep = info->size;
1960 }
1961
1962 /* A helper function to find the sections for a .dwz file. */
1963
1964 static void
1965 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1966 {
1967 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1968
1969 /* Note that we only support the standard ELF names, because .dwz
1970 is ELF-only (at the time of writing). */
1971 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1972 {
1973 dwz_file->abbrev.s.section = sectp;
1974 dwz_file->abbrev.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1977 {
1978 dwz_file->info.s.section = sectp;
1979 dwz_file->info.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1982 {
1983 dwz_file->str.s.section = sectp;
1984 dwz_file->str.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1987 {
1988 dwz_file->line.s.section = sectp;
1989 dwz_file->line.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1992 {
1993 dwz_file->macro.s.section = sectp;
1994 dwz_file->macro.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1997 {
1998 dwz_file->gdb_index.s.section = sectp;
1999 dwz_file->gdb_index.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2002 {
2003 dwz_file->debug_names.s.section = sectp;
2004 dwz_file->debug_names.size = bfd_section_size (sectp);
2005 }
2006 }
2007
2008 /* See dwarf2read.h. */
2009
2010 struct dwz_file *
2011 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2012 {
2013 const char *filename;
2014 bfd_size_type buildid_len_arg;
2015 size_t buildid_len;
2016 bfd_byte *buildid;
2017
2018 if (dwarf2_per_objfile->dwz_file != NULL)
2019 return dwarf2_per_objfile->dwz_file.get ();
2020
2021 bfd_set_error (bfd_error_no_error);
2022 gdb::unique_xmalloc_ptr<char> data
2023 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2024 &buildid_len_arg, &buildid));
2025 if (data == NULL)
2026 {
2027 if (bfd_get_error () == bfd_error_no_error)
2028 return NULL;
2029 error (_("could not read '.gnu_debugaltlink' section: %s"),
2030 bfd_errmsg (bfd_get_error ()));
2031 }
2032
2033 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2034
2035 buildid_len = (size_t) buildid_len_arg;
2036
2037 filename = data.get ();
2038
2039 std::string abs_storage;
2040 if (!IS_ABSOLUTE_PATH (filename))
2041 {
2042 gdb::unique_xmalloc_ptr<char> abs
2043 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2044
2045 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2046 filename = abs_storage.c_str ();
2047 }
2048
2049 /* First try the file name given in the section. If that doesn't
2050 work, try to use the build-id instead. */
2051 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2052 if (dwz_bfd != NULL)
2053 {
2054 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2055 dwz_bfd.reset (nullptr);
2056 }
2057
2058 if (dwz_bfd == NULL)
2059 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2060
2061 if (dwz_bfd == nullptr)
2062 {
2063 gdb::unique_xmalloc_ptr<char> alt_filename;
2064 const char *origname = dwarf2_per_objfile->objfile->original_name;
2065
2066 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2067 buildid_len,
2068 origname,
2069 &alt_filename));
2070
2071 if (fd.get () >= 0)
2072 {
2073 /* File successfully retrieved from server. */
2074 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2075
2076 if (dwz_bfd == nullptr)
2077 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2078 alt_filename.get ());
2079 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2080 dwz_bfd.reset (nullptr);
2081 }
2082 }
2083
2084 if (dwz_bfd == NULL)
2085 error (_("could not find '.gnu_debugaltlink' file for %s"),
2086 objfile_name (dwarf2_per_objfile->objfile));
2087
2088 std::unique_ptr<struct dwz_file> result
2089 (new struct dwz_file (std::move (dwz_bfd)));
2090
2091 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2092 result.get ());
2093
2094 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2095 result->dwz_bfd.get ());
2096 dwarf2_per_objfile->dwz_file = std::move (result);
2097 return dwarf2_per_objfile->dwz_file.get ();
2098 }
2099 \f
2100 /* DWARF quick_symbols_functions support. */
2101
2102 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2103 unique line tables, so we maintain a separate table of all .debug_line
2104 derived entries to support the sharing.
2105 All the quick functions need is the list of file names. We discard the
2106 line_header when we're done and don't need to record it here. */
2107 struct quick_file_names
2108 {
2109 /* The data used to construct the hash key. */
2110 struct stmt_list_hash hash;
2111
2112 /* The number of entries in file_names, real_names. */
2113 unsigned int num_file_names;
2114
2115 /* The file names from the line table, after being run through
2116 file_full_name. */
2117 const char **file_names;
2118
2119 /* The file names from the line table after being run through
2120 gdb_realpath. These are computed lazily. */
2121 const char **real_names;
2122 };
2123
2124 /* When using the index (and thus not using psymtabs), each CU has an
2125 object of this type. This is used to hold information needed by
2126 the various "quick" methods. */
2127 struct dwarf2_per_cu_quick_data
2128 {
2129 /* The file table. This can be NULL if there was no file table
2130 or it's currently not read in.
2131 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2132 struct quick_file_names *file_names;
2133
2134 /* The corresponding symbol table. This is NULL if symbols for this
2135 CU have not yet been read. */
2136 struct compunit_symtab *compunit_symtab;
2137
2138 /* A temporary mark bit used when iterating over all CUs in
2139 expand_symtabs_matching. */
2140 unsigned int mark : 1;
2141
2142 /* True if we've tried to read the file table and found there isn't one.
2143 There will be no point in trying to read it again next time. */
2144 unsigned int no_file_data : 1;
2145 };
2146
2147 /* Utility hash function for a stmt_list_hash. */
2148
2149 static hashval_t
2150 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2151 {
2152 hashval_t v = 0;
2153
2154 if (stmt_list_hash->dwo_unit != NULL)
2155 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2156 v += to_underlying (stmt_list_hash->line_sect_off);
2157 return v;
2158 }
2159
2160 /* Utility equality function for a stmt_list_hash. */
2161
2162 static int
2163 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2164 const struct stmt_list_hash *rhs)
2165 {
2166 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2167 return 0;
2168 if (lhs->dwo_unit != NULL
2169 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2170 return 0;
2171
2172 return lhs->line_sect_off == rhs->line_sect_off;
2173 }
2174
2175 /* Hash function for a quick_file_names. */
2176
2177 static hashval_t
2178 hash_file_name_entry (const void *e)
2179 {
2180 const struct quick_file_names *file_data
2181 = (const struct quick_file_names *) e;
2182
2183 return hash_stmt_list_entry (&file_data->hash);
2184 }
2185
2186 /* Equality function for a quick_file_names. */
2187
2188 static int
2189 eq_file_name_entry (const void *a, const void *b)
2190 {
2191 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2192 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2193
2194 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2195 }
2196
2197 /* Delete function for a quick_file_names. */
2198
2199 static void
2200 delete_file_name_entry (void *e)
2201 {
2202 struct quick_file_names *file_data = (struct quick_file_names *) e;
2203 int i;
2204
2205 for (i = 0; i < file_data->num_file_names; ++i)
2206 {
2207 xfree ((void*) file_data->file_names[i]);
2208 if (file_data->real_names)
2209 xfree ((void*) file_data->real_names[i]);
2210 }
2211
2212 /* The space for the struct itself lives on objfile_obstack,
2213 so we don't free it here. */
2214 }
2215
2216 /* Create a quick_file_names hash table. */
2217
2218 static htab_up
2219 create_quick_file_names_table (unsigned int nr_initial_entries)
2220 {
2221 return htab_up (htab_create_alloc (nr_initial_entries,
2222 hash_file_name_entry, eq_file_name_entry,
2223 delete_file_name_entry, xcalloc, xfree));
2224 }
2225
2226 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2227 have to be created afterwards. You should call age_cached_comp_units after
2228 processing PER_CU->CU. dw2_setup must have been already called. */
2229
2230 static void
2231 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2232 {
2233 if (per_cu->is_debug_types)
2234 load_full_type_unit (per_cu);
2235 else
2236 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2237
2238 if (per_cu->cu == NULL)
2239 return; /* Dummy CU. */
2240
2241 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2242 }
2243
2244 /* Read in the symbols for PER_CU. */
2245
2246 static void
2247 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2248 {
2249 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2250
2251 /* Skip type_unit_groups, reading the type units they contain
2252 is handled elsewhere. */
2253 if (per_cu->type_unit_group_p ())
2254 return;
2255
2256 /* The destructor of dwarf2_queue_guard frees any entries left on
2257 the queue. After this point we're guaranteed to leave this function
2258 with the dwarf queue empty. */
2259 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2260
2261 if (dwarf2_per_objfile->using_index
2262 ? per_cu->v.quick->compunit_symtab == NULL
2263 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2264 {
2265 queue_comp_unit (per_cu, language_minimal);
2266 load_cu (per_cu, skip_partial);
2267
2268 /* If we just loaded a CU from a DWO, and we're working with an index
2269 that may badly handle TUs, load all the TUs in that DWO as well.
2270 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2271 if (!per_cu->is_debug_types
2272 && per_cu->cu != NULL
2273 && per_cu->cu->dwo_unit != NULL
2274 && dwarf2_per_objfile->index_table != NULL
2275 && dwarf2_per_objfile->index_table->version <= 7
2276 /* DWP files aren't supported yet. */
2277 && get_dwp_file (dwarf2_per_objfile) == NULL)
2278 queue_and_load_all_dwo_tus (per_cu);
2279 }
2280
2281 process_queue (dwarf2_per_objfile);
2282
2283 /* Age the cache, releasing compilation units that have not
2284 been used recently. */
2285 age_cached_comp_units (dwarf2_per_objfile);
2286 }
2287
2288 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2289 the objfile from which this CU came. Returns the resulting symbol
2290 table. */
2291
2292 static struct compunit_symtab *
2293 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2294 {
2295 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2296
2297 gdb_assert (dwarf2_per_objfile->using_index);
2298 if (!per_cu->v.quick->compunit_symtab)
2299 {
2300 free_cached_comp_units freer (dwarf2_per_objfile);
2301 scoped_restore decrementer = increment_reading_symtab ();
2302 dw2_do_instantiate_symtab (per_cu, skip_partial);
2303 process_cu_includes (dwarf2_per_objfile);
2304 }
2305
2306 return per_cu->v.quick->compunit_symtab;
2307 }
2308
2309 /* See declaration. */
2310
2311 dwarf2_per_cu_data *
2312 dwarf2_per_objfile::get_cutu (int index)
2313 {
2314 if (index >= this->all_comp_units.size ())
2315 {
2316 index -= this->all_comp_units.size ();
2317 gdb_assert (index < this->all_type_units.size ());
2318 return &this->all_type_units[index]->per_cu;
2319 }
2320
2321 return this->all_comp_units[index];
2322 }
2323
2324 /* See declaration. */
2325
2326 dwarf2_per_cu_data *
2327 dwarf2_per_objfile::get_cu (int index)
2328 {
2329 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2330
2331 return this->all_comp_units[index];
2332 }
2333
2334 /* See declaration. */
2335
2336 signatured_type *
2337 dwarf2_per_objfile::get_tu (int index)
2338 {
2339 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2340
2341 return this->all_type_units[index];
2342 }
2343
2344 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2345 objfile_obstack, and constructed with the specified field
2346 values. */
2347
2348 static dwarf2_per_cu_data *
2349 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2350 struct dwarf2_section_info *section,
2351 int is_dwz,
2352 sect_offset sect_off, ULONGEST length)
2353 {
2354 struct objfile *objfile = dwarf2_per_objfile->objfile;
2355 dwarf2_per_cu_data *the_cu
2356 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2357 struct dwarf2_per_cu_data);
2358 the_cu->sect_off = sect_off;
2359 the_cu->length = length;
2360 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2361 the_cu->section = section;
2362 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2363 struct dwarf2_per_cu_quick_data);
2364 the_cu->is_dwz = is_dwz;
2365 return the_cu;
2366 }
2367
2368 /* A helper for create_cus_from_index that handles a given list of
2369 CUs. */
2370
2371 static void
2372 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2373 const gdb_byte *cu_list, offset_type n_elements,
2374 struct dwarf2_section_info *section,
2375 int is_dwz)
2376 {
2377 for (offset_type i = 0; i < n_elements; i += 2)
2378 {
2379 gdb_static_assert (sizeof (ULONGEST) >= 8);
2380
2381 sect_offset sect_off
2382 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2383 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2384 cu_list += 2 * 8;
2385
2386 dwarf2_per_cu_data *per_cu
2387 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2388 sect_off, length);
2389 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2390 }
2391 }
2392
2393 /* Read the CU list from the mapped index, and use it to create all
2394 the CU objects for this objfile. */
2395
2396 static void
2397 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2398 const gdb_byte *cu_list, offset_type cu_list_elements,
2399 const gdb_byte *dwz_list, offset_type dwz_elements)
2400 {
2401 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2402 dwarf2_per_objfile->all_comp_units.reserve
2403 ((cu_list_elements + dwz_elements) / 2);
2404
2405 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2406 &dwarf2_per_objfile->info, 0);
2407
2408 if (dwz_elements == 0)
2409 return;
2410
2411 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2412 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2413 &dwz->info, 1);
2414 }
2415
2416 /* Create the signatured type hash table from the index. */
2417
2418 static void
2419 create_signatured_type_table_from_index
2420 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2421 struct dwarf2_section_info *section,
2422 const gdb_byte *bytes,
2423 offset_type elements)
2424 {
2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
2426
2427 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2428 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2429
2430 htab_up sig_types_hash = allocate_signatured_type_table ();
2431
2432 for (offset_type i = 0; i < elements; i += 3)
2433 {
2434 struct signatured_type *sig_type;
2435 ULONGEST signature;
2436 void **slot;
2437 cu_offset type_offset_in_tu;
2438
2439 gdb_static_assert (sizeof (ULONGEST) >= 8);
2440 sect_offset sect_off
2441 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2442 type_offset_in_tu
2443 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2444 BFD_ENDIAN_LITTLE);
2445 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2446 bytes += 3 * 8;
2447
2448 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2449 struct signatured_type);
2450 sig_type->signature = signature;
2451 sig_type->type_offset_in_tu = type_offset_in_tu;
2452 sig_type->per_cu.is_debug_types = 1;
2453 sig_type->per_cu.section = section;
2454 sig_type->per_cu.sect_off = sect_off;
2455 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2456 sig_type->per_cu.v.quick
2457 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2458 struct dwarf2_per_cu_quick_data);
2459
2460 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2461 *slot = sig_type;
2462
2463 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2464 }
2465
2466 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2467 }
2468
2469 /* Create the signatured type hash table from .debug_names. */
2470
2471 static void
2472 create_signatured_type_table_from_debug_names
2473 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2474 const mapped_debug_names &map,
2475 struct dwarf2_section_info *section,
2476 struct dwarf2_section_info *abbrev_section)
2477 {
2478 struct objfile *objfile = dwarf2_per_objfile->objfile;
2479
2480 section->read (objfile);
2481 abbrev_section->read (objfile);
2482
2483 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2484 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2485
2486 htab_up sig_types_hash = allocate_signatured_type_table ();
2487
2488 for (uint32_t i = 0; i < map.tu_count; ++i)
2489 {
2490 struct signatured_type *sig_type;
2491 void **slot;
2492
2493 sect_offset sect_off
2494 = (sect_offset) (extract_unsigned_integer
2495 (map.tu_table_reordered + i * map.offset_size,
2496 map.offset_size,
2497 map.dwarf5_byte_order));
2498
2499 comp_unit_head cu_header;
2500 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2501 abbrev_section,
2502 section->buffer + to_underlying (sect_off),
2503 rcuh_kind::TYPE);
2504
2505 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2506 struct signatured_type);
2507 sig_type->signature = cu_header.signature;
2508 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2509 sig_type->per_cu.is_debug_types = 1;
2510 sig_type->per_cu.section = section;
2511 sig_type->per_cu.sect_off = sect_off;
2512 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2513 sig_type->per_cu.v.quick
2514 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2515 struct dwarf2_per_cu_quick_data);
2516
2517 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2518 *slot = sig_type;
2519
2520 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2521 }
2522
2523 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2524 }
2525
2526 /* Read the address map data from the mapped index, and use it to
2527 populate the objfile's psymtabs_addrmap. */
2528
2529 static void
2530 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2531 struct mapped_index *index)
2532 {
2533 struct objfile *objfile = dwarf2_per_objfile->objfile;
2534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 dwarf2_per_objfile->get_cu (cu_index));
2576 }
2577
2578 objfile->partial_symtabs->psymtabs_addrmap
2579 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the objfile's psymtabs_addrmap. */
2584
2585 static void
2586 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2587 struct dwarf2_section_info *section)
2588 {
2589 struct objfile *objfile = dwarf2_per_objfile->objfile;
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2593
2594 auto_obstack temp_obstack;
2595 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2596
2597 std::unordered_map<sect_offset,
2598 dwarf2_per_cu_data *,
2599 gdb::hash_enum<sect_offset>>
2600 debug_info_offset_to_per_cu;
2601 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2602 {
2603 const auto insertpair
2604 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2605 if (!insertpair.second)
2606 {
2607 warning (_("Section .debug_aranges in %s has duplicate "
2608 "debug_info_offset %s, ignoring .debug_aranges."),
2609 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2610 return;
2611 }
2612 }
2613
2614 section->read (objfile);
2615
2616 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2617
2618 const gdb_byte *addr = section->buffer;
2619
2620 while (addr < section->buffer + section->size)
2621 {
2622 const gdb_byte *const entry_addr = addr;
2623 unsigned int bytes_read;
2624
2625 const LONGEST entry_length = read_initial_length (abfd, addr,
2626 &bytes_read);
2627 addr += bytes_read;
2628
2629 const gdb_byte *const entry_end = addr + entry_length;
2630 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2631 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2632 if (addr + entry_length > section->buffer + section->size)
2633 {
2634 warning (_("Section .debug_aranges in %s entry at offset %s "
2635 "length %s exceeds section length %s, "
2636 "ignoring .debug_aranges."),
2637 objfile_name (objfile),
2638 plongest (entry_addr - section->buffer),
2639 plongest (bytes_read + entry_length),
2640 pulongest (section->size));
2641 return;
2642 }
2643
2644 /* The version number. */
2645 const uint16_t version = read_2_bytes (abfd, addr);
2646 addr += 2;
2647 if (version != 2)
2648 {
2649 warning (_("Section .debug_aranges in %s entry at offset %s "
2650 "has unsupported version %d, ignoring .debug_aranges."),
2651 objfile_name (objfile),
2652 plongest (entry_addr - section->buffer), version);
2653 return;
2654 }
2655
2656 const uint64_t debug_info_offset
2657 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2658 addr += offset_size;
2659 const auto per_cu_it
2660 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2661 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2662 {
2663 warning (_("Section .debug_aranges in %s entry at offset %s "
2664 "debug_info_offset %s does not exists, "
2665 "ignoring .debug_aranges."),
2666 objfile_name (objfile),
2667 plongest (entry_addr - section->buffer),
2668 pulongest (debug_info_offset));
2669 return;
2670 }
2671 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2672
2673 const uint8_t address_size = *addr++;
2674 if (address_size < 1 || address_size > 8)
2675 {
2676 warning (_("Section .debug_aranges in %s entry at offset %s "
2677 "address_size %u is invalid, ignoring .debug_aranges."),
2678 objfile_name (objfile),
2679 plongest (entry_addr - section->buffer), address_size);
2680 return;
2681 }
2682
2683 const uint8_t segment_selector_size = *addr++;
2684 if (segment_selector_size != 0)
2685 {
2686 warning (_("Section .debug_aranges in %s entry at offset %s "
2687 "segment_selector_size %u is not supported, "
2688 "ignoring .debug_aranges."),
2689 objfile_name (objfile),
2690 plongest (entry_addr - section->buffer),
2691 segment_selector_size);
2692 return;
2693 }
2694
2695 /* Must pad to an alignment boundary that is twice the address
2696 size. It is undocumented by the DWARF standard but GCC does
2697 use it. */
2698 for (size_t padding = ((-(addr - section->buffer))
2699 & (2 * address_size - 1));
2700 padding > 0; padding--)
2701 if (*addr++ != 0)
2702 {
2703 warning (_("Section .debug_aranges in %s entry at offset %s "
2704 "padding is not zero, ignoring .debug_aranges."),
2705 objfile_name (objfile),
2706 plongest (entry_addr - section->buffer));
2707 return;
2708 }
2709
2710 for (;;)
2711 {
2712 if (addr + 2 * address_size > entry_end)
2713 {
2714 warning (_("Section .debug_aranges in %s entry at offset %s "
2715 "address list is not properly terminated, "
2716 "ignoring .debug_aranges."),
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer));
2719 return;
2720 }
2721 ULONGEST start = extract_unsigned_integer (addr, address_size,
2722 dwarf5_byte_order);
2723 addr += address_size;
2724 ULONGEST length = extract_unsigned_integer (addr, address_size,
2725 dwarf5_byte_order);
2726 addr += address_size;
2727 if (start == 0 && length == 0)
2728 break;
2729 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2730 {
2731 /* Symbol was eliminated due to a COMDAT group. */
2732 continue;
2733 }
2734 ULONGEST end = start + length;
2735 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2736 - baseaddr);
2737 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2738 - baseaddr);
2739 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2740 }
2741 }
2742
2743 objfile->partial_symtabs->psymtabs_addrmap
2744 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2745 }
2746
2747 /* Find a slot in the mapped index INDEX for the object named NAME.
2748 If NAME is found, set *VEC_OUT to point to the CU vector in the
2749 constant pool and return true. If NAME cannot be found, return
2750 false. */
2751
2752 static bool
2753 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2754 offset_type **vec_out)
2755 {
2756 offset_type hash;
2757 offset_type slot, step;
2758 int (*cmp) (const char *, const char *);
2759
2760 gdb::unique_xmalloc_ptr<char> without_params;
2761 if (current_language->la_language == language_cplus
2762 || current_language->la_language == language_fortran
2763 || current_language->la_language == language_d)
2764 {
2765 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2766 not contain any. */
2767
2768 if (strchr (name, '(') != NULL)
2769 {
2770 without_params = cp_remove_params (name);
2771
2772 if (without_params != NULL)
2773 name = without_params.get ();
2774 }
2775 }
2776
2777 /* Index version 4 did not support case insensitive searches. But the
2778 indices for case insensitive languages are built in lowercase, therefore
2779 simulate our NAME being searched is also lowercased. */
2780 hash = mapped_index_string_hash ((index->version == 4
2781 && case_sensitivity == case_sensitive_off
2782 ? 5 : index->version),
2783 name);
2784
2785 slot = hash & (index->symbol_table.size () - 1);
2786 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2787 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2788
2789 for (;;)
2790 {
2791 const char *str;
2792
2793 const auto &bucket = index->symbol_table[slot];
2794 if (bucket.name == 0 && bucket.vec == 0)
2795 return false;
2796
2797 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2798 if (!cmp (name, str))
2799 {
2800 *vec_out = (offset_type *) (index->constant_pool
2801 + MAYBE_SWAP (bucket.vec));
2802 return true;
2803 }
2804
2805 slot = (slot + step) & (index->symbol_table.size () - 1);
2806 }
2807 }
2808
2809 /* A helper function that reads the .gdb_index from BUFFER and fills
2810 in MAP. FILENAME is the name of the file containing the data;
2811 it is used for error reporting. DEPRECATED_OK is true if it is
2812 ok to use deprecated sections.
2813
2814 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2815 out parameters that are filled in with information about the CU and
2816 TU lists in the section.
2817
2818 Returns true if all went well, false otherwise. */
2819
2820 static bool
2821 read_gdb_index_from_buffer (struct objfile *objfile,
2822 const char *filename,
2823 bool deprecated_ok,
2824 gdb::array_view<const gdb_byte> buffer,
2825 struct mapped_index *map,
2826 const gdb_byte **cu_list,
2827 offset_type *cu_list_elements,
2828 const gdb_byte **types_list,
2829 offset_type *types_list_elements)
2830 {
2831 const gdb_byte *addr = &buffer[0];
2832
2833 /* Version check. */
2834 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2835 /* Versions earlier than 3 emitted every copy of a psymbol. This
2836 causes the index to behave very poorly for certain requests. Version 3
2837 contained incomplete addrmap. So, it seems better to just ignore such
2838 indices. */
2839 if (version < 4)
2840 {
2841 static int warning_printed = 0;
2842 if (!warning_printed)
2843 {
2844 warning (_("Skipping obsolete .gdb_index section in %s."),
2845 filename);
2846 warning_printed = 1;
2847 }
2848 return 0;
2849 }
2850 /* Index version 4 uses a different hash function than index version
2851 5 and later.
2852
2853 Versions earlier than 6 did not emit psymbols for inlined
2854 functions. Using these files will cause GDB not to be able to
2855 set breakpoints on inlined functions by name, so we ignore these
2856 indices unless the user has done
2857 "set use-deprecated-index-sections on". */
2858 if (version < 6 && !deprecated_ok)
2859 {
2860 static int warning_printed = 0;
2861 if (!warning_printed)
2862 {
2863 warning (_("\
2864 Skipping deprecated .gdb_index section in %s.\n\
2865 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2866 to use the section anyway."),
2867 filename);
2868 warning_printed = 1;
2869 }
2870 return 0;
2871 }
2872 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2873 of the TU (for symbols coming from TUs),
2874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2875 Plus gold-generated indices can have duplicate entries for global symbols,
2876 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2877 These are just performance bugs, and we can't distinguish gdb-generated
2878 indices from gold-generated ones, so issue no warning here. */
2879
2880 /* Indexes with higher version than the one supported by GDB may be no
2881 longer backward compatible. */
2882 if (version > 8)
2883 return 0;
2884
2885 map->version = version;
2886
2887 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2888
2889 int i = 0;
2890 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2891 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2892 / 8);
2893 ++i;
2894
2895 *types_list = addr + MAYBE_SWAP (metadata[i]);
2896 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2897 - MAYBE_SWAP (metadata[i]))
2898 / 8);
2899 ++i;
2900
2901 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2902 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2903 map->address_table
2904 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2905 ++i;
2906
2907 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2908 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2909 map->symbol_table
2910 = gdb::array_view<mapped_index::symbol_table_slot>
2911 ((mapped_index::symbol_table_slot *) symbol_table,
2912 (mapped_index::symbol_table_slot *) symbol_table_end);
2913
2914 ++i;
2915 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2916
2917 return 1;
2918 }
2919
2920 /* Callback types for dwarf2_read_gdb_index. */
2921
2922 typedef gdb::function_view
2923 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2924 get_gdb_index_contents_ftype;
2925 typedef gdb::function_view
2926 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2927 get_gdb_index_contents_dwz_ftype;
2928
2929 /* Read .gdb_index. If everything went ok, initialize the "quick"
2930 elements of all the CUs and return 1. Otherwise, return 0. */
2931
2932 static int
2933 dwarf2_read_gdb_index
2934 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2935 get_gdb_index_contents_ftype get_gdb_index_contents,
2936 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2937 {
2938 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2939 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2940 struct dwz_file *dwz;
2941 struct objfile *objfile = dwarf2_per_objfile->objfile;
2942
2943 gdb::array_view<const gdb_byte> main_index_contents
2944 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2945
2946 if (main_index_contents.empty ())
2947 return 0;
2948
2949 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2950 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2951 use_deprecated_index_sections,
2952 main_index_contents, map.get (), &cu_list,
2953 &cu_list_elements, &types_list,
2954 &types_list_elements))
2955 return 0;
2956
2957 /* Don't use the index if it's empty. */
2958 if (map->symbol_table.empty ())
2959 return 0;
2960
2961 /* If there is a .dwz file, read it so we can get its CU list as
2962 well. */
2963 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2964 if (dwz != NULL)
2965 {
2966 struct mapped_index dwz_map;
2967 const gdb_byte *dwz_types_ignore;
2968 offset_type dwz_types_elements_ignore;
2969
2970 gdb::array_view<const gdb_byte> dwz_index_content
2971 = get_gdb_index_contents_dwz (objfile, dwz);
2972
2973 if (dwz_index_content.empty ())
2974 return 0;
2975
2976 if (!read_gdb_index_from_buffer (objfile,
2977 bfd_get_filename (dwz->dwz_bfd.get ()),
2978 1, dwz_index_content, &dwz_map,
2979 &dwz_list, &dwz_list_elements,
2980 &dwz_types_ignore,
2981 &dwz_types_elements_ignore))
2982 {
2983 warning (_("could not read '.gdb_index' section from %s; skipping"),
2984 bfd_get_filename (dwz->dwz_bfd.get ()));
2985 return 0;
2986 }
2987 }
2988
2989 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
2990 dwz_list, dwz_list_elements);
2991
2992 if (types_list_elements)
2993 {
2994 /* We can only handle a single .debug_types when we have an
2995 index. */
2996 if (dwarf2_per_objfile->types.size () != 1)
2997 return 0;
2998
2999 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3000
3001 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3002 types_list, types_list_elements);
3003 }
3004
3005 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3006
3007 dwarf2_per_objfile->index_table = std::move (map);
3008 dwarf2_per_objfile->using_index = 1;
3009 dwarf2_per_objfile->quick_file_names_table =
3010 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3011
3012 return 1;
3013 }
3014
3015 /* die_reader_func for dw2_get_file_names. */
3016
3017 static void
3018 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3019 const gdb_byte *info_ptr,
3020 struct die_info *comp_unit_die)
3021 {
3022 struct dwarf2_cu *cu = reader->cu;
3023 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3024 struct dwarf2_per_objfile *dwarf2_per_objfile
3025 = cu->per_cu->dwarf2_per_objfile;
3026 struct objfile *objfile = dwarf2_per_objfile->objfile;
3027 struct dwarf2_per_cu_data *lh_cu;
3028 struct attribute *attr;
3029 void **slot;
3030 struct quick_file_names *qfn;
3031
3032 gdb_assert (! this_cu->is_debug_types);
3033
3034 /* Our callers never want to match partial units -- instead they
3035 will match the enclosing full CU. */
3036 if (comp_unit_die->tag == DW_TAG_partial_unit)
3037 {
3038 this_cu->v.quick->no_file_data = 1;
3039 return;
3040 }
3041
3042 lh_cu = this_cu;
3043 slot = NULL;
3044
3045 line_header_up lh;
3046 sect_offset line_offset {};
3047
3048 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3049 if (attr != nullptr)
3050 {
3051 struct quick_file_names find_entry;
3052
3053 line_offset = (sect_offset) DW_UNSND (attr);
3054
3055 /* We may have already read in this line header (TU line header sharing).
3056 If we have we're done. */
3057 find_entry.hash.dwo_unit = cu->dwo_unit;
3058 find_entry.hash.line_sect_off = line_offset;
3059 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3060 &find_entry, INSERT);
3061 if (*slot != NULL)
3062 {
3063 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3064 return;
3065 }
3066
3067 lh = dwarf_decode_line_header (line_offset, cu);
3068 }
3069 if (lh == NULL)
3070 {
3071 lh_cu->v.quick->no_file_data = 1;
3072 return;
3073 }
3074
3075 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3076 qfn->hash.dwo_unit = cu->dwo_unit;
3077 qfn->hash.line_sect_off = line_offset;
3078 gdb_assert (slot != NULL);
3079 *slot = qfn;
3080
3081 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3082
3083 int offset = 0;
3084 if (strcmp (fnd.name, "<unknown>") != 0)
3085 ++offset;
3086
3087 qfn->num_file_names = offset + lh->file_names_size ();
3088 qfn->file_names =
3089 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3090 if (offset != 0)
3091 qfn->file_names[0] = xstrdup (fnd.name);
3092 for (int i = 0; i < lh->file_names_size (); ++i)
3093 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3094 fnd.comp_dir).release ();
3095 qfn->real_names = NULL;
3096
3097 lh_cu->v.quick->file_names = qfn;
3098 }
3099
3100 /* A helper for the "quick" functions which attempts to read the line
3101 table for THIS_CU. */
3102
3103 static struct quick_file_names *
3104 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3105 {
3106 /* This should never be called for TUs. */
3107 gdb_assert (! this_cu->is_debug_types);
3108 /* Nor type unit groups. */
3109 gdb_assert (! this_cu->type_unit_group_p ());
3110
3111 if (this_cu->v.quick->file_names != NULL)
3112 return this_cu->v.quick->file_names;
3113 /* If we know there is no line data, no point in looking again. */
3114 if (this_cu->v.quick->no_file_data)
3115 return NULL;
3116
3117 cutu_reader reader (this_cu);
3118 if (!reader.dummy_p)
3119 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3120
3121 if (this_cu->v.quick->no_file_data)
3122 return NULL;
3123 return this_cu->v.quick->file_names;
3124 }
3125
3126 /* A helper for the "quick" functions which computes and caches the
3127 real path for a given file name from the line table. */
3128
3129 static const char *
3130 dw2_get_real_path (struct objfile *objfile,
3131 struct quick_file_names *qfn, int index)
3132 {
3133 if (qfn->real_names == NULL)
3134 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3135 qfn->num_file_names, const char *);
3136
3137 if (qfn->real_names[index] == NULL)
3138 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3139
3140 return qfn->real_names[index];
3141 }
3142
3143 static struct symtab *
3144 dw2_find_last_source_symtab (struct objfile *objfile)
3145 {
3146 struct dwarf2_per_objfile *dwarf2_per_objfile
3147 = get_dwarf2_per_objfile (objfile);
3148 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3149 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3150
3151 if (cust == NULL)
3152 return NULL;
3153
3154 return compunit_primary_filetab (cust);
3155 }
3156
3157 /* Traversal function for dw2_forget_cached_source_info. */
3158
3159 static int
3160 dw2_free_cached_file_names (void **slot, void *info)
3161 {
3162 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3163
3164 if (file_data->real_names)
3165 {
3166 int i;
3167
3168 for (i = 0; i < file_data->num_file_names; ++i)
3169 {
3170 xfree ((void*) file_data->real_names[i]);
3171 file_data->real_names[i] = NULL;
3172 }
3173 }
3174
3175 return 1;
3176 }
3177
3178 static void
3179 dw2_forget_cached_source_info (struct objfile *objfile)
3180 {
3181 struct dwarf2_per_objfile *dwarf2_per_objfile
3182 = get_dwarf2_per_objfile (objfile);
3183
3184 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3185 dw2_free_cached_file_names, NULL);
3186 }
3187
3188 /* Helper function for dw2_map_symtabs_matching_filename that expands
3189 the symtabs and calls the iterator. */
3190
3191 static int
3192 dw2_map_expand_apply (struct objfile *objfile,
3193 struct dwarf2_per_cu_data *per_cu,
3194 const char *name, const char *real_path,
3195 gdb::function_view<bool (symtab *)> callback)
3196 {
3197 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3198
3199 /* Don't visit already-expanded CUs. */
3200 if (per_cu->v.quick->compunit_symtab)
3201 return 0;
3202
3203 /* This may expand more than one symtab, and we want to iterate over
3204 all of them. */
3205 dw2_instantiate_symtab (per_cu, false);
3206
3207 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3208 last_made, callback);
3209 }
3210
3211 /* Implementation of the map_symtabs_matching_filename method. */
3212
3213 static bool
3214 dw2_map_symtabs_matching_filename
3215 (struct objfile *objfile, const char *name, const char *real_path,
3216 gdb::function_view<bool (symtab *)> callback)
3217 {
3218 const char *name_basename = lbasename (name);
3219 struct dwarf2_per_objfile *dwarf2_per_objfile
3220 = get_dwarf2_per_objfile (objfile);
3221
3222 /* The rule is CUs specify all the files, including those used by
3223 any TU, so there's no need to scan TUs here. */
3224
3225 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3226 {
3227 /* We only need to look at symtabs not already expanded. */
3228 if (per_cu->v.quick->compunit_symtab)
3229 continue;
3230
3231 quick_file_names *file_data = dw2_get_file_names (per_cu);
3232 if (file_data == NULL)
3233 continue;
3234
3235 for (int j = 0; j < file_data->num_file_names; ++j)
3236 {
3237 const char *this_name = file_data->file_names[j];
3238 const char *this_real_name;
3239
3240 if (compare_filenames_for_search (this_name, name))
3241 {
3242 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3243 callback))
3244 return true;
3245 continue;
3246 }
3247
3248 /* Before we invoke realpath, which can get expensive when many
3249 files are involved, do a quick comparison of the basenames. */
3250 if (! basenames_may_differ
3251 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3252 continue;
3253
3254 this_real_name = dw2_get_real_path (objfile, file_data, j);
3255 if (compare_filenames_for_search (this_real_name, name))
3256 {
3257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3258 callback))
3259 return true;
3260 continue;
3261 }
3262
3263 if (real_path != NULL)
3264 {
3265 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3266 gdb_assert (IS_ABSOLUTE_PATH (name));
3267 if (this_real_name != NULL
3268 && FILENAME_CMP (real_path, this_real_name) == 0)
3269 {
3270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3271 callback))
3272 return true;
3273 continue;
3274 }
3275 }
3276 }
3277 }
3278
3279 return false;
3280 }
3281
3282 /* Struct used to manage iterating over all CUs looking for a symbol. */
3283
3284 struct dw2_symtab_iterator
3285 {
3286 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3287 struct dwarf2_per_objfile *dwarf2_per_objfile;
3288 /* If set, only look for symbols that match that block. Valid values are
3289 GLOBAL_BLOCK and STATIC_BLOCK. */
3290 gdb::optional<block_enum> block_index;
3291 /* The kind of symbol we're looking for. */
3292 domain_enum domain;
3293 /* The list of CUs from the index entry of the symbol,
3294 or NULL if not found. */
3295 offset_type *vec;
3296 /* The next element in VEC to look at. */
3297 int next;
3298 /* The number of elements in VEC, or zero if there is no match. */
3299 int length;
3300 /* Have we seen a global version of the symbol?
3301 If so we can ignore all further global instances.
3302 This is to work around gold/15646, inefficient gold-generated
3303 indices. */
3304 int global_seen;
3305 };
3306
3307 /* Initialize the index symtab iterator ITER. */
3308
3309 static void
3310 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3311 struct dwarf2_per_objfile *dwarf2_per_objfile,
3312 gdb::optional<block_enum> block_index,
3313 domain_enum domain,
3314 const char *name)
3315 {
3316 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3317 iter->block_index = block_index;
3318 iter->domain = domain;
3319 iter->next = 0;
3320 iter->global_seen = 0;
3321
3322 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3323
3324 /* index is NULL if OBJF_READNOW. */
3325 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3326 iter->length = MAYBE_SWAP (*iter->vec);
3327 else
3328 {
3329 iter->vec = NULL;
3330 iter->length = 0;
3331 }
3332 }
3333
3334 /* Return the next matching CU or NULL if there are no more. */
3335
3336 static struct dwarf2_per_cu_data *
3337 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3338 {
3339 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3340
3341 for ( ; iter->next < iter->length; ++iter->next)
3342 {
3343 offset_type cu_index_and_attrs =
3344 MAYBE_SWAP (iter->vec[iter->next + 1]);
3345 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3346 gdb_index_symbol_kind symbol_kind =
3347 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3348 /* Only check the symbol attributes if they're present.
3349 Indices prior to version 7 don't record them,
3350 and indices >= 7 may elide them for certain symbols
3351 (gold does this). */
3352 int attrs_valid =
3353 (dwarf2_per_objfile->index_table->version >= 7
3354 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3355
3356 /* Don't crash on bad data. */
3357 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3358 + dwarf2_per_objfile->all_type_units.size ()))
3359 {
3360 complaint (_(".gdb_index entry has bad CU index"
3361 " [in module %s]"),
3362 objfile_name (dwarf2_per_objfile->objfile));
3363 continue;
3364 }
3365
3366 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3367
3368 /* Skip if already read in. */
3369 if (per_cu->v.quick->compunit_symtab)
3370 continue;
3371
3372 /* Check static vs global. */
3373 if (attrs_valid)
3374 {
3375 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3376
3377 if (iter->block_index.has_value ())
3378 {
3379 bool want_static = *iter->block_index == STATIC_BLOCK;
3380
3381 if (is_static != want_static)
3382 continue;
3383 }
3384
3385 /* Work around gold/15646. */
3386 if (!is_static && iter->global_seen)
3387 continue;
3388 if (!is_static)
3389 iter->global_seen = 1;
3390 }
3391
3392 /* Only check the symbol's kind if it has one. */
3393 if (attrs_valid)
3394 {
3395 switch (iter->domain)
3396 {
3397 case VAR_DOMAIN:
3398 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3399 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3400 /* Some types are also in VAR_DOMAIN. */
3401 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3402 continue;
3403 break;
3404 case STRUCT_DOMAIN:
3405 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3406 continue;
3407 break;
3408 case LABEL_DOMAIN:
3409 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3410 continue;
3411 break;
3412 case MODULE_DOMAIN:
3413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3414 continue;
3415 break;
3416 default:
3417 break;
3418 }
3419 }
3420
3421 ++iter->next;
3422 return per_cu;
3423 }
3424
3425 return NULL;
3426 }
3427
3428 static struct compunit_symtab *
3429 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3430 const char *name, domain_enum domain)
3431 {
3432 struct compunit_symtab *stab_best = NULL;
3433 struct dwarf2_per_objfile *dwarf2_per_objfile
3434 = get_dwarf2_per_objfile (objfile);
3435
3436 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3437
3438 struct dw2_symtab_iterator iter;
3439 struct dwarf2_per_cu_data *per_cu;
3440
3441 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3442
3443 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3444 {
3445 struct symbol *sym, *with_opaque = NULL;
3446 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3447 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3448 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3449
3450 sym = block_find_symbol (block, name, domain,
3451 block_find_non_opaque_type_preferred,
3452 &with_opaque);
3453
3454 /* Some caution must be observed with overloaded functions
3455 and methods, since the index will not contain any overload
3456 information (but NAME might contain it). */
3457
3458 if (sym != NULL
3459 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3460 return stab;
3461 if (with_opaque != NULL
3462 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3463 stab_best = stab;
3464
3465 /* Keep looking through other CUs. */
3466 }
3467
3468 return stab_best;
3469 }
3470
3471 static void
3472 dw2_print_stats (struct objfile *objfile)
3473 {
3474 struct dwarf2_per_objfile *dwarf2_per_objfile
3475 = get_dwarf2_per_objfile (objfile);
3476 int total = (dwarf2_per_objfile->all_comp_units.size ()
3477 + dwarf2_per_objfile->all_type_units.size ());
3478 int count = 0;
3479
3480 for (int i = 0; i < total; ++i)
3481 {
3482 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3483
3484 if (!per_cu->v.quick->compunit_symtab)
3485 ++count;
3486 }
3487 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3488 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3489 }
3490
3491 /* This dumps minimal information about the index.
3492 It is called via "mt print objfiles".
3493 One use is to verify .gdb_index has been loaded by the
3494 gdb.dwarf2/gdb-index.exp testcase. */
3495
3496 static void
3497 dw2_dump (struct objfile *objfile)
3498 {
3499 struct dwarf2_per_objfile *dwarf2_per_objfile
3500 = get_dwarf2_per_objfile (objfile);
3501
3502 gdb_assert (dwarf2_per_objfile->using_index);
3503 printf_filtered (".gdb_index:");
3504 if (dwarf2_per_objfile->index_table != NULL)
3505 {
3506 printf_filtered (" version %d\n",
3507 dwarf2_per_objfile->index_table->version);
3508 }
3509 else
3510 printf_filtered (" faked for \"readnow\"\n");
3511 printf_filtered ("\n");
3512 }
3513
3514 static void
3515 dw2_expand_symtabs_for_function (struct objfile *objfile,
3516 const char *func_name)
3517 {
3518 struct dwarf2_per_objfile *dwarf2_per_objfile
3519 = get_dwarf2_per_objfile (objfile);
3520
3521 struct dw2_symtab_iterator iter;
3522 struct dwarf2_per_cu_data *per_cu;
3523
3524 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3525
3526 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3527 dw2_instantiate_symtab (per_cu, false);
3528
3529 }
3530
3531 static void
3532 dw2_expand_all_symtabs (struct objfile *objfile)
3533 {
3534 struct dwarf2_per_objfile *dwarf2_per_objfile
3535 = get_dwarf2_per_objfile (objfile);
3536 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3537 + dwarf2_per_objfile->all_type_units.size ());
3538
3539 for (int i = 0; i < total_units; ++i)
3540 {
3541 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3542
3543 /* We don't want to directly expand a partial CU, because if we
3544 read it with the wrong language, then assertion failures can
3545 be triggered later on. See PR symtab/23010. So, tell
3546 dw2_instantiate_symtab to skip partial CUs -- any important
3547 partial CU will be read via DW_TAG_imported_unit anyway. */
3548 dw2_instantiate_symtab (per_cu, true);
3549 }
3550 }
3551
3552 static void
3553 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3554 const char *fullname)
3555 {
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
3558
3559 /* We don't need to consider type units here.
3560 This is only called for examining code, e.g. expand_line_sal.
3561 There can be an order of magnitude (or more) more type units
3562 than comp units, and we avoid them if we can. */
3563
3564 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3565 {
3566 /* We only need to look at symtabs not already expanded. */
3567 if (per_cu->v.quick->compunit_symtab)
3568 continue;
3569
3570 quick_file_names *file_data = dw2_get_file_names (per_cu);
3571 if (file_data == NULL)
3572 continue;
3573
3574 for (int j = 0; j < file_data->num_file_names; ++j)
3575 {
3576 const char *this_fullname = file_data->file_names[j];
3577
3578 if (filename_cmp (this_fullname, fullname) == 0)
3579 {
3580 dw2_instantiate_symtab (per_cu, false);
3581 break;
3582 }
3583 }
3584 }
3585 }
3586
3587 static void
3588 dw2_map_matching_symbols
3589 (struct objfile *objfile,
3590 const lookup_name_info &name, domain_enum domain,
3591 int global,
3592 gdb::function_view<symbol_found_callback_ftype> callback,
3593 symbol_compare_ftype *ordered_compare)
3594 {
3595 /* Used for Ada. */
3596 struct dwarf2_per_objfile *dwarf2_per_objfile
3597 = get_dwarf2_per_objfile (objfile);
3598
3599 if (dwarf2_per_objfile->index_table != nullptr)
3600 {
3601 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3602 here though if the current language is Ada for a non-Ada objfile
3603 using GNU index. As Ada does not look for non-Ada symbols this
3604 function should just return. */
3605 return;
3606 }
3607
3608 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3609 inline psym_map_matching_symbols here, assuming all partial symtabs have
3610 been read in. */
3611 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3612
3613 for (compunit_symtab *cust : objfile->compunits ())
3614 {
3615 const struct block *block;
3616
3617 if (cust == NULL)
3618 continue;
3619 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3620 if (!iterate_over_symbols_terminated (block, name,
3621 domain, callback))
3622 return;
3623 }
3624 }
3625
3626 /* Starting from a search name, return the string that finds the upper
3627 bound of all strings that start with SEARCH_NAME in a sorted name
3628 list. Returns the empty string to indicate that the upper bound is
3629 the end of the list. */
3630
3631 static std::string
3632 make_sort_after_prefix_name (const char *search_name)
3633 {
3634 /* When looking to complete "func", we find the upper bound of all
3635 symbols that start with "func" by looking for where we'd insert
3636 the closest string that would follow "func" in lexicographical
3637 order. Usually, that's "func"-with-last-character-incremented,
3638 i.e. "fund". Mind non-ASCII characters, though. Usually those
3639 will be UTF-8 multi-byte sequences, but we can't be certain.
3640 Especially mind the 0xff character, which is a valid character in
3641 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3642 rule out compilers allowing it in identifiers. Note that
3643 conveniently, strcmp/strcasecmp are specified to compare
3644 characters interpreted as unsigned char. So what we do is treat
3645 the whole string as a base 256 number composed of a sequence of
3646 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3647 to 0, and carries 1 to the following more-significant position.
3648 If the very first character in SEARCH_NAME ends up incremented
3649 and carries/overflows, then the upper bound is the end of the
3650 list. The string after the empty string is also the empty
3651 string.
3652
3653 Some examples of this operation:
3654
3655 SEARCH_NAME => "+1" RESULT
3656
3657 "abc" => "abd"
3658 "ab\xff" => "ac"
3659 "\xff" "a" "\xff" => "\xff" "b"
3660 "\xff" => ""
3661 "\xff\xff" => ""
3662 "" => ""
3663
3664 Then, with these symbols for example:
3665
3666 func
3667 func1
3668 fund
3669
3670 completing "func" looks for symbols between "func" and
3671 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3672 which finds "func" and "func1", but not "fund".
3673
3674 And with:
3675
3676 funcÿ (Latin1 'ÿ' [0xff])
3677 funcÿ1
3678 fund
3679
3680 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3681 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3682
3683 And with:
3684
3685 ÿÿ (Latin1 'ÿ' [0xff])
3686 ÿÿ1
3687
3688 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3689 the end of the list.
3690 */
3691 std::string after = search_name;
3692 while (!after.empty () && (unsigned char) after.back () == 0xff)
3693 after.pop_back ();
3694 if (!after.empty ())
3695 after.back () = (unsigned char) after.back () + 1;
3696 return after;
3697 }
3698
3699 /* See declaration. */
3700
3701 std::pair<std::vector<name_component>::const_iterator,
3702 std::vector<name_component>::const_iterator>
3703 mapped_index_base::find_name_components_bounds
3704 (const lookup_name_info &lookup_name_without_params, language lang) const
3705 {
3706 auto *name_cmp
3707 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3708
3709 const char *lang_name
3710 = lookup_name_without_params.language_lookup_name (lang);
3711
3712 /* Comparison function object for lower_bound that matches against a
3713 given symbol name. */
3714 auto lookup_compare_lower = [&] (const name_component &elem,
3715 const char *name)
3716 {
3717 const char *elem_qualified = this->symbol_name_at (elem.idx);
3718 const char *elem_name = elem_qualified + elem.name_offset;
3719 return name_cmp (elem_name, name) < 0;
3720 };
3721
3722 /* Comparison function object for upper_bound that matches against a
3723 given symbol name. */
3724 auto lookup_compare_upper = [&] (const char *name,
3725 const name_component &elem)
3726 {
3727 const char *elem_qualified = this->symbol_name_at (elem.idx);
3728 const char *elem_name = elem_qualified + elem.name_offset;
3729 return name_cmp (name, elem_name) < 0;
3730 };
3731
3732 auto begin = this->name_components.begin ();
3733 auto end = this->name_components.end ();
3734
3735 /* Find the lower bound. */
3736 auto lower = [&] ()
3737 {
3738 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3739 return begin;
3740 else
3741 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3742 } ();
3743
3744 /* Find the upper bound. */
3745 auto upper = [&] ()
3746 {
3747 if (lookup_name_without_params.completion_mode ())
3748 {
3749 /* In completion mode, we want UPPER to point past all
3750 symbols names that have the same prefix. I.e., with
3751 these symbols, and completing "func":
3752
3753 function << lower bound
3754 function1
3755 other_function << upper bound
3756
3757 We find the upper bound by looking for the insertion
3758 point of "func"-with-last-character-incremented,
3759 i.e. "fund". */
3760 std::string after = make_sort_after_prefix_name (lang_name);
3761 if (after.empty ())
3762 return end;
3763 return std::lower_bound (lower, end, after.c_str (),
3764 lookup_compare_lower);
3765 }
3766 else
3767 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3768 } ();
3769
3770 return {lower, upper};
3771 }
3772
3773 /* See declaration. */
3774
3775 void
3776 mapped_index_base::build_name_components ()
3777 {
3778 if (!this->name_components.empty ())
3779 return;
3780
3781 this->name_components_casing = case_sensitivity;
3782 auto *name_cmp
3783 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3784
3785 /* The code below only knows how to break apart components of C++
3786 symbol names (and other languages that use '::' as
3787 namespace/module separator) and Ada symbol names. */
3788 auto count = this->symbol_name_count ();
3789 for (offset_type idx = 0; idx < count; idx++)
3790 {
3791 if (this->symbol_name_slot_invalid (idx))
3792 continue;
3793
3794 const char *name = this->symbol_name_at (idx);
3795
3796 /* Add each name component to the name component table. */
3797 unsigned int previous_len = 0;
3798
3799 if (strstr (name, "::") != nullptr)
3800 {
3801 for (unsigned int current_len = cp_find_first_component (name);
3802 name[current_len] != '\0';
3803 current_len += cp_find_first_component (name + current_len))
3804 {
3805 gdb_assert (name[current_len] == ':');
3806 this->name_components.push_back ({previous_len, idx});
3807 /* Skip the '::'. */
3808 current_len += 2;
3809 previous_len = current_len;
3810 }
3811 }
3812 else
3813 {
3814 /* Handle the Ada encoded (aka mangled) form here. */
3815 for (const char *iter = strstr (name, "__");
3816 iter != nullptr;
3817 iter = strstr (iter, "__"))
3818 {
3819 this->name_components.push_back ({previous_len, idx});
3820 iter += 2;
3821 previous_len = iter - name;
3822 }
3823 }
3824
3825 this->name_components.push_back ({previous_len, idx});
3826 }
3827
3828 /* Sort name_components elements by name. */
3829 auto name_comp_compare = [&] (const name_component &left,
3830 const name_component &right)
3831 {
3832 const char *left_qualified = this->symbol_name_at (left.idx);
3833 const char *right_qualified = this->symbol_name_at (right.idx);
3834
3835 const char *left_name = left_qualified + left.name_offset;
3836 const char *right_name = right_qualified + right.name_offset;
3837
3838 return name_cmp (left_name, right_name) < 0;
3839 };
3840
3841 std::sort (this->name_components.begin (),
3842 this->name_components.end (),
3843 name_comp_compare);
3844 }
3845
3846 /* Helper for dw2_expand_symtabs_matching that works with a
3847 mapped_index_base instead of the containing objfile. This is split
3848 to a separate function in order to be able to unit test the
3849 name_components matching using a mock mapped_index_base. For each
3850 symbol name that matches, calls MATCH_CALLBACK, passing it the
3851 symbol's index in the mapped_index_base symbol table. */
3852
3853 static void
3854 dw2_expand_symtabs_matching_symbol
3855 (mapped_index_base &index,
3856 const lookup_name_info &lookup_name_in,
3857 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3858 enum search_domain kind,
3859 gdb::function_view<bool (offset_type)> match_callback)
3860 {
3861 lookup_name_info lookup_name_without_params
3862 = lookup_name_in.make_ignore_params ();
3863
3864 /* Build the symbol name component sorted vector, if we haven't
3865 yet. */
3866 index.build_name_components ();
3867
3868 /* The same symbol may appear more than once in the range though.
3869 E.g., if we're looking for symbols that complete "w", and we have
3870 a symbol named "w1::w2", we'll find the two name components for
3871 that same symbol in the range. To be sure we only call the
3872 callback once per symbol, we first collect the symbol name
3873 indexes that matched in a temporary vector and ignore
3874 duplicates. */
3875 std::vector<offset_type> matches;
3876
3877 struct name_and_matcher
3878 {
3879 symbol_name_matcher_ftype *matcher;
3880 const std::string &name;
3881
3882 bool operator== (const name_and_matcher &other) const
3883 {
3884 return matcher == other.matcher && name == other.name;
3885 }
3886 };
3887
3888 /* A vector holding all the different symbol name matchers, for all
3889 languages. */
3890 std::vector<name_and_matcher> matchers;
3891
3892 for (int i = 0; i < nr_languages; i++)
3893 {
3894 enum language lang_e = (enum language) i;
3895
3896 const language_defn *lang = language_def (lang_e);
3897 symbol_name_matcher_ftype *name_matcher
3898 = get_symbol_name_matcher (lang, lookup_name_without_params);
3899
3900 name_and_matcher key {
3901 name_matcher,
3902 lookup_name_without_params.language_lookup_name (lang_e)
3903 };
3904
3905 /* Don't insert the same comparison routine more than once.
3906 Note that we do this linear walk. This is not a problem in
3907 practice because the number of supported languages is
3908 low. */
3909 if (std::find (matchers.begin (), matchers.end (), key)
3910 != matchers.end ())
3911 continue;
3912 matchers.push_back (std::move (key));
3913
3914 auto bounds
3915 = index.find_name_components_bounds (lookup_name_without_params,
3916 lang_e);
3917
3918 /* Now for each symbol name in range, check to see if we have a name
3919 match, and if so, call the MATCH_CALLBACK callback. */
3920
3921 for (; bounds.first != bounds.second; ++bounds.first)
3922 {
3923 const char *qualified = index.symbol_name_at (bounds.first->idx);
3924
3925 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3926 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3927 continue;
3928
3929 matches.push_back (bounds.first->idx);
3930 }
3931 }
3932
3933 std::sort (matches.begin (), matches.end ());
3934
3935 /* Finally call the callback, once per match. */
3936 ULONGEST prev = -1;
3937 for (offset_type idx : matches)
3938 {
3939 if (prev != idx)
3940 {
3941 if (!match_callback (idx))
3942 break;
3943 prev = idx;
3944 }
3945 }
3946
3947 /* Above we use a type wider than idx's for 'prev', since 0 and
3948 (offset_type)-1 are both possible values. */
3949 static_assert (sizeof (prev) > sizeof (offset_type), "");
3950 }
3951
3952 #if GDB_SELF_TEST
3953
3954 namespace selftests { namespace dw2_expand_symtabs_matching {
3955
3956 /* A mock .gdb_index/.debug_names-like name index table, enough to
3957 exercise dw2_expand_symtabs_matching_symbol, which works with the
3958 mapped_index_base interface. Builds an index from the symbol list
3959 passed as parameter to the constructor. */
3960 class mock_mapped_index : public mapped_index_base
3961 {
3962 public:
3963 mock_mapped_index (gdb::array_view<const char *> symbols)
3964 : m_symbol_table (symbols)
3965 {}
3966
3967 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3968
3969 /* Return the number of names in the symbol table. */
3970 size_t symbol_name_count () const override
3971 {
3972 return m_symbol_table.size ();
3973 }
3974
3975 /* Get the name of the symbol at IDX in the symbol table. */
3976 const char *symbol_name_at (offset_type idx) const override
3977 {
3978 return m_symbol_table[idx];
3979 }
3980
3981 private:
3982 gdb::array_view<const char *> m_symbol_table;
3983 };
3984
3985 /* Convenience function that converts a NULL pointer to a "<null>"
3986 string, to pass to print routines. */
3987
3988 static const char *
3989 string_or_null (const char *str)
3990 {
3991 return str != NULL ? str : "<null>";
3992 }
3993
3994 /* Check if a lookup_name_info built from
3995 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3996 index. EXPECTED_LIST is the list of expected matches, in expected
3997 matching order. If no match expected, then an empty list is
3998 specified. Returns true on success. On failure prints a warning
3999 indicating the file:line that failed, and returns false. */
4000
4001 static bool
4002 check_match (const char *file, int line,
4003 mock_mapped_index &mock_index,
4004 const char *name, symbol_name_match_type match_type,
4005 bool completion_mode,
4006 std::initializer_list<const char *> expected_list)
4007 {
4008 lookup_name_info lookup_name (name, match_type, completion_mode);
4009
4010 bool matched = true;
4011
4012 auto mismatch = [&] (const char *expected_str,
4013 const char *got)
4014 {
4015 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4016 "expected=\"%s\", got=\"%s\"\n"),
4017 file, line,
4018 (match_type == symbol_name_match_type::FULL
4019 ? "FULL" : "WILD"),
4020 name, string_or_null (expected_str), string_or_null (got));
4021 matched = false;
4022 };
4023
4024 auto expected_it = expected_list.begin ();
4025 auto expected_end = expected_list.end ();
4026
4027 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4028 NULL, ALL_DOMAIN,
4029 [&] (offset_type idx)
4030 {
4031 const char *matched_name = mock_index.symbol_name_at (idx);
4032 const char *expected_str
4033 = expected_it == expected_end ? NULL : *expected_it++;
4034
4035 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4036 mismatch (expected_str, matched_name);
4037 return true;
4038 });
4039
4040 const char *expected_str
4041 = expected_it == expected_end ? NULL : *expected_it++;
4042 if (expected_str != NULL)
4043 mismatch (expected_str, NULL);
4044
4045 return matched;
4046 }
4047
4048 /* The symbols added to the mock mapped_index for testing (in
4049 canonical form). */
4050 static const char *test_symbols[] = {
4051 "function",
4052 "std::bar",
4053 "std::zfunction",
4054 "std::zfunction2",
4055 "w1::w2",
4056 "ns::foo<char*>",
4057 "ns::foo<int>",
4058 "ns::foo<long>",
4059 "ns2::tmpl<int>::foo2",
4060 "(anonymous namespace)::A::B::C",
4061
4062 /* These are used to check that the increment-last-char in the
4063 matching algorithm for completion doesn't match "t1_fund" when
4064 completing "t1_func". */
4065 "t1_func",
4066 "t1_func1",
4067 "t1_fund",
4068 "t1_fund1",
4069
4070 /* A UTF-8 name with multi-byte sequences to make sure that
4071 cp-name-parser understands this as a single identifier ("função"
4072 is "function" in PT). */
4073 u8"u8função",
4074
4075 /* \377 (0xff) is Latin1 'ÿ'. */
4076 "yfunc\377",
4077
4078 /* \377 (0xff) is Latin1 'ÿ'. */
4079 "\377",
4080 "\377\377123",
4081
4082 /* A name with all sorts of complications. Starts with "z" to make
4083 it easier for the completion tests below. */
4084 #define Z_SYM_NAME \
4085 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4086 "::tuple<(anonymous namespace)::ui*, " \
4087 "std::default_delete<(anonymous namespace)::ui>, void>"
4088
4089 Z_SYM_NAME
4090 };
4091
4092 /* Returns true if the mapped_index_base::find_name_component_bounds
4093 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4094 in completion mode. */
4095
4096 static bool
4097 check_find_bounds_finds (mapped_index_base &index,
4098 const char *search_name,
4099 gdb::array_view<const char *> expected_syms)
4100 {
4101 lookup_name_info lookup_name (search_name,
4102 symbol_name_match_type::FULL, true);
4103
4104 auto bounds = index.find_name_components_bounds (lookup_name,
4105 language_cplus);
4106
4107 size_t distance = std::distance (bounds.first, bounds.second);
4108 if (distance != expected_syms.size ())
4109 return false;
4110
4111 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4112 {
4113 auto nc_elem = bounds.first + exp_elem;
4114 const char *qualified = index.symbol_name_at (nc_elem->idx);
4115 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4116 return false;
4117 }
4118
4119 return true;
4120 }
4121
4122 /* Test the lower-level mapped_index::find_name_component_bounds
4123 method. */
4124
4125 static void
4126 test_mapped_index_find_name_component_bounds ()
4127 {
4128 mock_mapped_index mock_index (test_symbols);
4129
4130 mock_index.build_name_components ();
4131
4132 /* Test the lower-level mapped_index::find_name_component_bounds
4133 method in completion mode. */
4134 {
4135 static const char *expected_syms[] = {
4136 "t1_func",
4137 "t1_func1",
4138 };
4139
4140 SELF_CHECK (check_find_bounds_finds (mock_index,
4141 "t1_func", expected_syms));
4142 }
4143
4144 /* Check that the increment-last-char in the name matching algorithm
4145 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4146 {
4147 static const char *expected_syms1[] = {
4148 "\377",
4149 "\377\377123",
4150 };
4151 SELF_CHECK (check_find_bounds_finds (mock_index,
4152 "\377", expected_syms1));
4153
4154 static const char *expected_syms2[] = {
4155 "\377\377123",
4156 };
4157 SELF_CHECK (check_find_bounds_finds (mock_index,
4158 "\377\377", expected_syms2));
4159 }
4160 }
4161
4162 /* Test dw2_expand_symtabs_matching_symbol. */
4163
4164 static void
4165 test_dw2_expand_symtabs_matching_symbol ()
4166 {
4167 mock_mapped_index mock_index (test_symbols);
4168
4169 /* We let all tests run until the end even if some fails, for debug
4170 convenience. */
4171 bool any_mismatch = false;
4172
4173 /* Create the expected symbols list (an initializer_list). Needed
4174 because lists have commas, and we need to pass them to CHECK,
4175 which is a macro. */
4176 #define EXPECT(...) { __VA_ARGS__ }
4177
4178 /* Wrapper for check_match that passes down the current
4179 __FILE__/__LINE__. */
4180 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4181 any_mismatch |= !check_match (__FILE__, __LINE__, \
4182 mock_index, \
4183 NAME, MATCH_TYPE, COMPLETION_MODE, \
4184 EXPECTED_LIST)
4185
4186 /* Identity checks. */
4187 for (const char *sym : test_symbols)
4188 {
4189 /* Should be able to match all existing symbols. */
4190 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4191 EXPECT (sym));
4192
4193 /* Should be able to match all existing symbols with
4194 parameters. */
4195 std::string with_params = std::string (sym) + "(int)";
4196 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4197 EXPECT (sym));
4198
4199 /* Should be able to match all existing symbols with
4200 parameters and qualifiers. */
4201 with_params = std::string (sym) + " ( int ) const";
4202 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4203 EXPECT (sym));
4204
4205 /* This should really find sym, but cp-name-parser.y doesn't
4206 know about lvalue/rvalue qualifiers yet. */
4207 with_params = std::string (sym) + " ( int ) &&";
4208 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4209 {});
4210 }
4211
4212 /* Check that the name matching algorithm for completion doesn't get
4213 confused with Latin1 'ÿ' / 0xff. */
4214 {
4215 static const char str[] = "\377";
4216 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4217 EXPECT ("\377", "\377\377123"));
4218 }
4219
4220 /* Check that the increment-last-char in the matching algorithm for
4221 completion doesn't match "t1_fund" when completing "t1_func". */
4222 {
4223 static const char str[] = "t1_func";
4224 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4225 EXPECT ("t1_func", "t1_func1"));
4226 }
4227
4228 /* Check that completion mode works at each prefix of the expected
4229 symbol name. */
4230 {
4231 static const char str[] = "function(int)";
4232 size_t len = strlen (str);
4233 std::string lookup;
4234
4235 for (size_t i = 1; i < len; i++)
4236 {
4237 lookup.assign (str, i);
4238 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4239 EXPECT ("function"));
4240 }
4241 }
4242
4243 /* While "w" is a prefix of both components, the match function
4244 should still only be called once. */
4245 {
4246 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4247 EXPECT ("w1::w2"));
4248 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4249 EXPECT ("w1::w2"));
4250 }
4251
4252 /* Same, with a "complicated" symbol. */
4253 {
4254 static const char str[] = Z_SYM_NAME;
4255 size_t len = strlen (str);
4256 std::string lookup;
4257
4258 for (size_t i = 1; i < len; i++)
4259 {
4260 lookup.assign (str, i);
4261 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4262 EXPECT (Z_SYM_NAME));
4263 }
4264 }
4265
4266 /* In FULL mode, an incomplete symbol doesn't match. */
4267 {
4268 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4269 {});
4270 }
4271
4272 /* A complete symbol with parameters matches any overload, since the
4273 index has no overload info. */
4274 {
4275 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4276 EXPECT ("std::zfunction", "std::zfunction2"));
4277 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4278 EXPECT ("std::zfunction", "std::zfunction2"));
4279 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4280 EXPECT ("std::zfunction", "std::zfunction2"));
4281 }
4282
4283 /* Check that whitespace is ignored appropriately. A symbol with a
4284 template argument list. */
4285 {
4286 static const char expected[] = "ns::foo<int>";
4287 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4288 EXPECT (expected));
4289 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4290 EXPECT (expected));
4291 }
4292
4293 /* Check that whitespace is ignored appropriately. A symbol with a
4294 template argument list that includes a pointer. */
4295 {
4296 static const char expected[] = "ns::foo<char*>";
4297 /* Try both completion and non-completion modes. */
4298 static const bool completion_mode[2] = {false, true};
4299 for (size_t i = 0; i < 2; i++)
4300 {
4301 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4302 completion_mode[i], EXPECT (expected));
4303 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4304 completion_mode[i], EXPECT (expected));
4305
4306 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4307 completion_mode[i], EXPECT (expected));
4308 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4309 completion_mode[i], EXPECT (expected));
4310 }
4311 }
4312
4313 {
4314 /* Check method qualifiers are ignored. */
4315 static const char expected[] = "ns::foo<char*>";
4316 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4317 symbol_name_match_type::FULL, true, EXPECT (expected));
4318 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4319 symbol_name_match_type::FULL, true, EXPECT (expected));
4320 CHECK_MATCH ("foo < char * > ( int ) const",
4321 symbol_name_match_type::WILD, true, EXPECT (expected));
4322 CHECK_MATCH ("foo < char * > ( int ) &&",
4323 symbol_name_match_type::WILD, true, EXPECT (expected));
4324 }
4325
4326 /* Test lookup names that don't match anything. */
4327 {
4328 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4329 {});
4330
4331 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4332 {});
4333 }
4334
4335 /* Some wild matching tests, exercising "(anonymous namespace)",
4336 which should not be confused with a parameter list. */
4337 {
4338 static const char *syms[] = {
4339 "A::B::C",
4340 "B::C",
4341 "C",
4342 "A :: B :: C ( int )",
4343 "B :: C ( int )",
4344 "C ( int )",
4345 };
4346
4347 for (const char *s : syms)
4348 {
4349 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4350 EXPECT ("(anonymous namespace)::A::B::C"));
4351 }
4352 }
4353
4354 {
4355 static const char expected[] = "ns2::tmpl<int>::foo2";
4356 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4357 EXPECT (expected));
4358 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4359 EXPECT (expected));
4360 }
4361
4362 SELF_CHECK (!any_mismatch);
4363
4364 #undef EXPECT
4365 #undef CHECK_MATCH
4366 }
4367
4368 static void
4369 run_test ()
4370 {
4371 test_mapped_index_find_name_component_bounds ();
4372 test_dw2_expand_symtabs_matching_symbol ();
4373 }
4374
4375 }} // namespace selftests::dw2_expand_symtabs_matching
4376
4377 #endif /* GDB_SELF_TEST */
4378
4379 /* If FILE_MATCHER is NULL or if PER_CU has
4380 dwarf2_per_cu_quick_data::MARK set (see
4381 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4382 EXPANSION_NOTIFY on it. */
4383
4384 static void
4385 dw2_expand_symtabs_matching_one
4386 (struct dwarf2_per_cu_data *per_cu,
4387 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4388 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4389 {
4390 if (file_matcher == NULL || per_cu->v.quick->mark)
4391 {
4392 bool symtab_was_null
4393 = (per_cu->v.quick->compunit_symtab == NULL);
4394
4395 dw2_instantiate_symtab (per_cu, false);
4396
4397 if (expansion_notify != NULL
4398 && symtab_was_null
4399 && per_cu->v.quick->compunit_symtab != NULL)
4400 expansion_notify (per_cu->v.quick->compunit_symtab);
4401 }
4402 }
4403
4404 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4405 matched, to expand corresponding CUs that were marked. IDX is the
4406 index of the symbol name that matched. */
4407
4408 static void
4409 dw2_expand_marked_cus
4410 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4411 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4412 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4413 search_domain kind)
4414 {
4415 offset_type *vec, vec_len, vec_idx;
4416 bool global_seen = false;
4417 mapped_index &index = *dwarf2_per_objfile->index_table;
4418
4419 vec = (offset_type *) (index.constant_pool
4420 + MAYBE_SWAP (index.symbol_table[idx].vec));
4421 vec_len = MAYBE_SWAP (vec[0]);
4422 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4423 {
4424 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4425 /* This value is only valid for index versions >= 7. */
4426 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4427 gdb_index_symbol_kind symbol_kind =
4428 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4429 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4430 /* Only check the symbol attributes if they're present.
4431 Indices prior to version 7 don't record them,
4432 and indices >= 7 may elide them for certain symbols
4433 (gold does this). */
4434 int attrs_valid =
4435 (index.version >= 7
4436 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4437
4438 /* Work around gold/15646. */
4439 if (attrs_valid)
4440 {
4441 if (!is_static && global_seen)
4442 continue;
4443 if (!is_static)
4444 global_seen = true;
4445 }
4446
4447 /* Only check the symbol's kind if it has one. */
4448 if (attrs_valid)
4449 {
4450 switch (kind)
4451 {
4452 case VARIABLES_DOMAIN:
4453 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4454 continue;
4455 break;
4456 case FUNCTIONS_DOMAIN:
4457 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4458 continue;
4459 break;
4460 case TYPES_DOMAIN:
4461 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4462 continue;
4463 break;
4464 case MODULES_DOMAIN:
4465 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4466 continue;
4467 break;
4468 default:
4469 break;
4470 }
4471 }
4472
4473 /* Don't crash on bad data. */
4474 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4475 + dwarf2_per_objfile->all_type_units.size ()))
4476 {
4477 complaint (_(".gdb_index entry has bad CU index"
4478 " [in module %s]"),
4479 objfile_name (dwarf2_per_objfile->objfile));
4480 continue;
4481 }
4482
4483 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4484 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4485 expansion_notify);
4486 }
4487 }
4488
4489 /* If FILE_MATCHER is non-NULL, set all the
4490 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4491 that match FILE_MATCHER. */
4492
4493 static void
4494 dw_expand_symtabs_matching_file_matcher
4495 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4496 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4497 {
4498 if (file_matcher == NULL)
4499 return;
4500
4501 objfile *const objfile = dwarf2_per_objfile->objfile;
4502
4503 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4504 htab_eq_pointer,
4505 NULL, xcalloc, xfree));
4506 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4507 htab_eq_pointer,
4508 NULL, xcalloc, xfree));
4509
4510 /* The rule is CUs specify all the files, including those used by
4511 any TU, so there's no need to scan TUs here. */
4512
4513 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4514 {
4515 QUIT;
4516
4517 per_cu->v.quick->mark = 0;
4518
4519 /* We only need to look at symtabs not already expanded. */
4520 if (per_cu->v.quick->compunit_symtab)
4521 continue;
4522
4523 quick_file_names *file_data = dw2_get_file_names (per_cu);
4524 if (file_data == NULL)
4525 continue;
4526
4527 if (htab_find (visited_not_found.get (), file_data) != NULL)
4528 continue;
4529 else if (htab_find (visited_found.get (), file_data) != NULL)
4530 {
4531 per_cu->v.quick->mark = 1;
4532 continue;
4533 }
4534
4535 for (int j = 0; j < file_data->num_file_names; ++j)
4536 {
4537 const char *this_real_name;
4538
4539 if (file_matcher (file_data->file_names[j], false))
4540 {
4541 per_cu->v.quick->mark = 1;
4542 break;
4543 }
4544
4545 /* Before we invoke realpath, which can get expensive when many
4546 files are involved, do a quick comparison of the basenames. */
4547 if (!basenames_may_differ
4548 && !file_matcher (lbasename (file_data->file_names[j]),
4549 true))
4550 continue;
4551
4552 this_real_name = dw2_get_real_path (objfile, file_data, j);
4553 if (file_matcher (this_real_name, false))
4554 {
4555 per_cu->v.quick->mark = 1;
4556 break;
4557 }
4558 }
4559
4560 void **slot = htab_find_slot (per_cu->v.quick->mark
4561 ? visited_found.get ()
4562 : visited_not_found.get (),
4563 file_data, INSERT);
4564 *slot = file_data;
4565 }
4566 }
4567
4568 static void
4569 dw2_expand_symtabs_matching
4570 (struct objfile *objfile,
4571 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4572 const lookup_name_info &lookup_name,
4573 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4574 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4575 enum search_domain kind)
4576 {
4577 struct dwarf2_per_objfile *dwarf2_per_objfile
4578 = get_dwarf2_per_objfile (objfile);
4579
4580 /* index_table is NULL if OBJF_READNOW. */
4581 if (!dwarf2_per_objfile->index_table)
4582 return;
4583
4584 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4585
4586 mapped_index &index = *dwarf2_per_objfile->index_table;
4587
4588 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4589 symbol_matcher,
4590 kind, [&] (offset_type idx)
4591 {
4592 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4593 expansion_notify, kind);
4594 return true;
4595 });
4596 }
4597
4598 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4599 symtab. */
4600
4601 static struct compunit_symtab *
4602 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4603 CORE_ADDR pc)
4604 {
4605 int i;
4606
4607 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4608 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4609 return cust;
4610
4611 if (cust->includes == NULL)
4612 return NULL;
4613
4614 for (i = 0; cust->includes[i]; ++i)
4615 {
4616 struct compunit_symtab *s = cust->includes[i];
4617
4618 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4619 if (s != NULL)
4620 return s;
4621 }
4622
4623 return NULL;
4624 }
4625
4626 static struct compunit_symtab *
4627 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4628 struct bound_minimal_symbol msymbol,
4629 CORE_ADDR pc,
4630 struct obj_section *section,
4631 int warn_if_readin)
4632 {
4633 struct dwarf2_per_cu_data *data;
4634 struct compunit_symtab *result;
4635
4636 if (!objfile->partial_symtabs->psymtabs_addrmap)
4637 return NULL;
4638
4639 CORE_ADDR baseaddr = objfile->text_section_offset ();
4640 data = (struct dwarf2_per_cu_data *) addrmap_find
4641 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4642 if (!data)
4643 return NULL;
4644
4645 if (warn_if_readin && data->v.quick->compunit_symtab)
4646 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4647 paddress (get_objfile_arch (objfile), pc));
4648
4649 result
4650 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4651 false),
4652 pc);
4653 gdb_assert (result != NULL);
4654 return result;
4655 }
4656
4657 static void
4658 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4659 void *data, int need_fullname)
4660 {
4661 struct dwarf2_per_objfile *dwarf2_per_objfile
4662 = get_dwarf2_per_objfile (objfile);
4663
4664 if (!dwarf2_per_objfile->filenames_cache)
4665 {
4666 dwarf2_per_objfile->filenames_cache.emplace ();
4667
4668 htab_up visited (htab_create_alloc (10,
4669 htab_hash_pointer, htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671
4672 /* The rule is CUs specify all the files, including those used
4673 by any TU, so there's no need to scan TUs here. We can
4674 ignore file names coming from already-expanded CUs. */
4675
4676 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4677 {
4678 if (per_cu->v.quick->compunit_symtab)
4679 {
4680 void **slot = htab_find_slot (visited.get (),
4681 per_cu->v.quick->file_names,
4682 INSERT);
4683
4684 *slot = per_cu->v.quick->file_names;
4685 }
4686 }
4687
4688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4689 {
4690 /* We only need to look at symtabs not already expanded. */
4691 if (per_cu->v.quick->compunit_symtab)
4692 continue;
4693
4694 quick_file_names *file_data = dw2_get_file_names (per_cu);
4695 if (file_data == NULL)
4696 continue;
4697
4698 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4699 if (*slot)
4700 {
4701 /* Already visited. */
4702 continue;
4703 }
4704 *slot = file_data;
4705
4706 for (int j = 0; j < file_data->num_file_names; ++j)
4707 {
4708 const char *filename = file_data->file_names[j];
4709 dwarf2_per_objfile->filenames_cache->seen (filename);
4710 }
4711 }
4712 }
4713
4714 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4715 {
4716 gdb::unique_xmalloc_ptr<char> this_real_name;
4717
4718 if (need_fullname)
4719 this_real_name = gdb_realpath (filename);
4720 (*fun) (filename, this_real_name.get (), data);
4721 });
4722 }
4723
4724 static int
4725 dw2_has_symbols (struct objfile *objfile)
4726 {
4727 return 1;
4728 }
4729
4730 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4731 {
4732 dw2_has_symbols,
4733 dw2_find_last_source_symtab,
4734 dw2_forget_cached_source_info,
4735 dw2_map_symtabs_matching_filename,
4736 dw2_lookup_symbol,
4737 NULL,
4738 dw2_print_stats,
4739 dw2_dump,
4740 dw2_expand_symtabs_for_function,
4741 dw2_expand_all_symtabs,
4742 dw2_expand_symtabs_with_fullname,
4743 dw2_map_matching_symbols,
4744 dw2_expand_symtabs_matching,
4745 dw2_find_pc_sect_compunit_symtab,
4746 NULL,
4747 dw2_map_symbol_filenames
4748 };
4749
4750 /* DWARF-5 debug_names reader. */
4751
4752 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4753 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4754
4755 /* A helper function that reads the .debug_names section in SECTION
4756 and fills in MAP. FILENAME is the name of the file containing the
4757 section; it is used for error reporting.
4758
4759 Returns true if all went well, false otherwise. */
4760
4761 static bool
4762 read_debug_names_from_section (struct objfile *objfile,
4763 const char *filename,
4764 struct dwarf2_section_info *section,
4765 mapped_debug_names &map)
4766 {
4767 if (section->empty ())
4768 return false;
4769
4770 /* Older elfutils strip versions could keep the section in the main
4771 executable while splitting it for the separate debug info file. */
4772 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4773 return false;
4774
4775 section->read (objfile);
4776
4777 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4778
4779 const gdb_byte *addr = section->buffer;
4780
4781 bfd *const abfd = section->get_bfd_owner ();
4782
4783 unsigned int bytes_read;
4784 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4785 addr += bytes_read;
4786
4787 map.dwarf5_is_dwarf64 = bytes_read != 4;
4788 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4789 if (bytes_read + length != section->size)
4790 {
4791 /* There may be multiple per-CU indices. */
4792 warning (_("Section .debug_names in %s length %s does not match "
4793 "section length %s, ignoring .debug_names."),
4794 filename, plongest (bytes_read + length),
4795 pulongest (section->size));
4796 return false;
4797 }
4798
4799 /* The version number. */
4800 uint16_t version = read_2_bytes (abfd, addr);
4801 addr += 2;
4802 if (version != 5)
4803 {
4804 warning (_("Section .debug_names in %s has unsupported version %d, "
4805 "ignoring .debug_names."),
4806 filename, version);
4807 return false;
4808 }
4809
4810 /* Padding. */
4811 uint16_t padding = read_2_bytes (abfd, addr);
4812 addr += 2;
4813 if (padding != 0)
4814 {
4815 warning (_("Section .debug_names in %s has unsupported padding %d, "
4816 "ignoring .debug_names."),
4817 filename, padding);
4818 return false;
4819 }
4820
4821 /* comp_unit_count - The number of CUs in the CU list. */
4822 map.cu_count = read_4_bytes (abfd, addr);
4823 addr += 4;
4824
4825 /* local_type_unit_count - The number of TUs in the local TU
4826 list. */
4827 map.tu_count = read_4_bytes (abfd, addr);
4828 addr += 4;
4829
4830 /* foreign_type_unit_count - The number of TUs in the foreign TU
4831 list. */
4832 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4833 addr += 4;
4834 if (foreign_tu_count != 0)
4835 {
4836 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4837 "ignoring .debug_names."),
4838 filename, static_cast<unsigned long> (foreign_tu_count));
4839 return false;
4840 }
4841
4842 /* bucket_count - The number of hash buckets in the hash lookup
4843 table. */
4844 map.bucket_count = read_4_bytes (abfd, addr);
4845 addr += 4;
4846
4847 /* name_count - The number of unique names in the index. */
4848 map.name_count = read_4_bytes (abfd, addr);
4849 addr += 4;
4850
4851 /* abbrev_table_size - The size in bytes of the abbreviations
4852 table. */
4853 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4854 addr += 4;
4855
4856 /* augmentation_string_size - The size in bytes of the augmentation
4857 string. This value is rounded up to a multiple of 4. */
4858 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4859 addr += 4;
4860 map.augmentation_is_gdb = ((augmentation_string_size
4861 == sizeof (dwarf5_augmentation))
4862 && memcmp (addr, dwarf5_augmentation,
4863 sizeof (dwarf5_augmentation)) == 0);
4864 augmentation_string_size += (-augmentation_string_size) & 3;
4865 addr += augmentation_string_size;
4866
4867 /* List of CUs */
4868 map.cu_table_reordered = addr;
4869 addr += map.cu_count * map.offset_size;
4870
4871 /* List of Local TUs */
4872 map.tu_table_reordered = addr;
4873 addr += map.tu_count * map.offset_size;
4874
4875 /* Hash Lookup Table */
4876 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4877 addr += map.bucket_count * 4;
4878 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4879 addr += map.name_count * 4;
4880
4881 /* Name Table */
4882 map.name_table_string_offs_reordered = addr;
4883 addr += map.name_count * map.offset_size;
4884 map.name_table_entry_offs_reordered = addr;
4885 addr += map.name_count * map.offset_size;
4886
4887 const gdb_byte *abbrev_table_start = addr;
4888 for (;;)
4889 {
4890 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4891 addr += bytes_read;
4892 if (index_num == 0)
4893 break;
4894
4895 const auto insertpair
4896 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4897 if (!insertpair.second)
4898 {
4899 warning (_("Section .debug_names in %s has duplicate index %s, "
4900 "ignoring .debug_names."),
4901 filename, pulongest (index_num));
4902 return false;
4903 }
4904 mapped_debug_names::index_val &indexval = insertpair.first->second;
4905 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4906 addr += bytes_read;
4907
4908 for (;;)
4909 {
4910 mapped_debug_names::index_val::attr attr;
4911 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4912 addr += bytes_read;
4913 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4914 addr += bytes_read;
4915 if (attr.form == DW_FORM_implicit_const)
4916 {
4917 attr.implicit_const = read_signed_leb128 (abfd, addr,
4918 &bytes_read);
4919 addr += bytes_read;
4920 }
4921 if (attr.dw_idx == 0 && attr.form == 0)
4922 break;
4923 indexval.attr_vec.push_back (std::move (attr));
4924 }
4925 }
4926 if (addr != abbrev_table_start + abbrev_table_size)
4927 {
4928 warning (_("Section .debug_names in %s has abbreviation_table "
4929 "of size %s vs. written as %u, ignoring .debug_names."),
4930 filename, plongest (addr - abbrev_table_start),
4931 abbrev_table_size);
4932 return false;
4933 }
4934 map.entry_pool = addr;
4935
4936 return true;
4937 }
4938
4939 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4940 list. */
4941
4942 static void
4943 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4944 const mapped_debug_names &map,
4945 dwarf2_section_info &section,
4946 bool is_dwz)
4947 {
4948 sect_offset sect_off_prev;
4949 for (uint32_t i = 0; i <= map.cu_count; ++i)
4950 {
4951 sect_offset sect_off_next;
4952 if (i < map.cu_count)
4953 {
4954 sect_off_next
4955 = (sect_offset) (extract_unsigned_integer
4956 (map.cu_table_reordered + i * map.offset_size,
4957 map.offset_size,
4958 map.dwarf5_byte_order));
4959 }
4960 else
4961 sect_off_next = (sect_offset) section.size;
4962 if (i >= 1)
4963 {
4964 const ULONGEST length = sect_off_next - sect_off_prev;
4965 dwarf2_per_cu_data *per_cu
4966 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4967 sect_off_prev, length);
4968 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4969 }
4970 sect_off_prev = sect_off_next;
4971 }
4972 }
4973
4974 /* Read the CU list from the mapped index, and use it to create all
4975 the CU objects for this dwarf2_per_objfile. */
4976
4977 static void
4978 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4979 const mapped_debug_names &map,
4980 const mapped_debug_names &dwz_map)
4981 {
4982 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4983 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4984
4985 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4986 dwarf2_per_objfile->info,
4987 false /* is_dwz */);
4988
4989 if (dwz_map.cu_count == 0)
4990 return;
4991
4992 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4993 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4994 true /* is_dwz */);
4995 }
4996
4997 /* Read .debug_names. If everything went ok, initialize the "quick"
4998 elements of all the CUs and return true. Otherwise, return false. */
4999
5000 static bool
5001 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5002 {
5003 std::unique_ptr<mapped_debug_names> map
5004 (new mapped_debug_names (dwarf2_per_objfile));
5005 mapped_debug_names dwz_map (dwarf2_per_objfile);
5006 struct objfile *objfile = dwarf2_per_objfile->objfile;
5007
5008 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5009 &dwarf2_per_objfile->debug_names,
5010 *map))
5011 return false;
5012
5013 /* Don't use the index if it's empty. */
5014 if (map->name_count == 0)
5015 return false;
5016
5017 /* If there is a .dwz file, read it so we can get its CU list as
5018 well. */
5019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5020 if (dwz != NULL)
5021 {
5022 if (!read_debug_names_from_section (objfile,
5023 bfd_get_filename (dwz->dwz_bfd.get ()),
5024 &dwz->debug_names, dwz_map))
5025 {
5026 warning (_("could not read '.debug_names' section from %s; skipping"),
5027 bfd_get_filename (dwz->dwz_bfd.get ()));
5028 return false;
5029 }
5030 }
5031
5032 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5033
5034 if (map->tu_count != 0)
5035 {
5036 /* We can only handle a single .debug_types when we have an
5037 index. */
5038 if (dwarf2_per_objfile->types.size () != 1)
5039 return false;
5040
5041 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5042
5043 create_signatured_type_table_from_debug_names
5044 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5045 }
5046
5047 create_addrmap_from_aranges (dwarf2_per_objfile,
5048 &dwarf2_per_objfile->debug_aranges);
5049
5050 dwarf2_per_objfile->debug_names_table = std::move (map);
5051 dwarf2_per_objfile->using_index = 1;
5052 dwarf2_per_objfile->quick_file_names_table =
5053 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5054
5055 return true;
5056 }
5057
5058 /* Type used to manage iterating over all CUs looking for a symbol for
5059 .debug_names. */
5060
5061 class dw2_debug_names_iterator
5062 {
5063 public:
5064 dw2_debug_names_iterator (const mapped_debug_names &map,
5065 gdb::optional<block_enum> block_index,
5066 domain_enum domain,
5067 const char *name)
5068 : m_map (map), m_block_index (block_index), m_domain (domain),
5069 m_addr (find_vec_in_debug_names (map, name))
5070 {}
5071
5072 dw2_debug_names_iterator (const mapped_debug_names &map,
5073 search_domain search, uint32_t namei)
5074 : m_map (map),
5075 m_search (search),
5076 m_addr (find_vec_in_debug_names (map, namei))
5077 {}
5078
5079 dw2_debug_names_iterator (const mapped_debug_names &map,
5080 block_enum block_index, domain_enum domain,
5081 uint32_t namei)
5082 : m_map (map), m_block_index (block_index), m_domain (domain),
5083 m_addr (find_vec_in_debug_names (map, namei))
5084 {}
5085
5086 /* Return the next matching CU or NULL if there are no more. */
5087 dwarf2_per_cu_data *next ();
5088
5089 private:
5090 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5091 const char *name);
5092 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5093 uint32_t namei);
5094
5095 /* The internalized form of .debug_names. */
5096 const mapped_debug_names &m_map;
5097
5098 /* If set, only look for symbols that match that block. Valid values are
5099 GLOBAL_BLOCK and STATIC_BLOCK. */
5100 const gdb::optional<block_enum> m_block_index;
5101
5102 /* The kind of symbol we're looking for. */
5103 const domain_enum m_domain = UNDEF_DOMAIN;
5104 const search_domain m_search = ALL_DOMAIN;
5105
5106 /* The list of CUs from the index entry of the symbol, or NULL if
5107 not found. */
5108 const gdb_byte *m_addr;
5109 };
5110
5111 const char *
5112 mapped_debug_names::namei_to_name (uint32_t namei) const
5113 {
5114 const ULONGEST namei_string_offs
5115 = extract_unsigned_integer ((name_table_string_offs_reordered
5116 + namei * offset_size),
5117 offset_size,
5118 dwarf5_byte_order);
5119 return read_indirect_string_at_offset (dwarf2_per_objfile,
5120 namei_string_offs);
5121 }
5122
5123 /* Find a slot in .debug_names for the object named NAME. If NAME is
5124 found, return pointer to its pool data. If NAME cannot be found,
5125 return NULL. */
5126
5127 const gdb_byte *
5128 dw2_debug_names_iterator::find_vec_in_debug_names
5129 (const mapped_debug_names &map, const char *name)
5130 {
5131 int (*cmp) (const char *, const char *);
5132
5133 gdb::unique_xmalloc_ptr<char> without_params;
5134 if (current_language->la_language == language_cplus
5135 || current_language->la_language == language_fortran
5136 || current_language->la_language == language_d)
5137 {
5138 /* NAME is already canonical. Drop any qualifiers as
5139 .debug_names does not contain any. */
5140
5141 if (strchr (name, '(') != NULL)
5142 {
5143 without_params = cp_remove_params (name);
5144 if (without_params != NULL)
5145 name = without_params.get ();
5146 }
5147 }
5148
5149 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5150
5151 const uint32_t full_hash = dwarf5_djb_hash (name);
5152 uint32_t namei
5153 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5154 (map.bucket_table_reordered
5155 + (full_hash % map.bucket_count)), 4,
5156 map.dwarf5_byte_order);
5157 if (namei == 0)
5158 return NULL;
5159 --namei;
5160 if (namei >= map.name_count)
5161 {
5162 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5163 "[in module %s]"),
5164 namei, map.name_count,
5165 objfile_name (map.dwarf2_per_objfile->objfile));
5166 return NULL;
5167 }
5168
5169 for (;;)
5170 {
5171 const uint32_t namei_full_hash
5172 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5173 (map.hash_table_reordered + namei), 4,
5174 map.dwarf5_byte_order);
5175 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5176 return NULL;
5177
5178 if (full_hash == namei_full_hash)
5179 {
5180 const char *const namei_string = map.namei_to_name (namei);
5181
5182 #if 0 /* An expensive sanity check. */
5183 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5184 {
5185 complaint (_("Wrong .debug_names hash for string at index %u "
5186 "[in module %s]"),
5187 namei, objfile_name (dwarf2_per_objfile->objfile));
5188 return NULL;
5189 }
5190 #endif
5191
5192 if (cmp (namei_string, name) == 0)
5193 {
5194 const ULONGEST namei_entry_offs
5195 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5196 + namei * map.offset_size),
5197 map.offset_size, map.dwarf5_byte_order);
5198 return map.entry_pool + namei_entry_offs;
5199 }
5200 }
5201
5202 ++namei;
5203 if (namei >= map.name_count)
5204 return NULL;
5205 }
5206 }
5207
5208 const gdb_byte *
5209 dw2_debug_names_iterator::find_vec_in_debug_names
5210 (const mapped_debug_names &map, uint32_t namei)
5211 {
5212 if (namei >= map.name_count)
5213 {
5214 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5215 "[in module %s]"),
5216 namei, map.name_count,
5217 objfile_name (map.dwarf2_per_objfile->objfile));
5218 return NULL;
5219 }
5220
5221 const ULONGEST namei_entry_offs
5222 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5223 + namei * map.offset_size),
5224 map.offset_size, map.dwarf5_byte_order);
5225 return map.entry_pool + namei_entry_offs;
5226 }
5227
5228 /* See dw2_debug_names_iterator. */
5229
5230 dwarf2_per_cu_data *
5231 dw2_debug_names_iterator::next ()
5232 {
5233 if (m_addr == NULL)
5234 return NULL;
5235
5236 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5237 struct objfile *objfile = dwarf2_per_objfile->objfile;
5238 bfd *const abfd = objfile->obfd;
5239
5240 again:
5241
5242 unsigned int bytes_read;
5243 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5244 m_addr += bytes_read;
5245 if (abbrev == 0)
5246 return NULL;
5247
5248 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5249 if (indexval_it == m_map.abbrev_map.cend ())
5250 {
5251 complaint (_("Wrong .debug_names undefined abbrev code %s "
5252 "[in module %s]"),
5253 pulongest (abbrev), objfile_name (objfile));
5254 return NULL;
5255 }
5256 const mapped_debug_names::index_val &indexval = indexval_it->second;
5257 enum class symbol_linkage {
5258 unknown,
5259 static_,
5260 extern_,
5261 } symbol_linkage_ = symbol_linkage::unknown;
5262 dwarf2_per_cu_data *per_cu = NULL;
5263 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5264 {
5265 ULONGEST ull;
5266 switch (attr.form)
5267 {
5268 case DW_FORM_implicit_const:
5269 ull = attr.implicit_const;
5270 break;
5271 case DW_FORM_flag_present:
5272 ull = 1;
5273 break;
5274 case DW_FORM_udata:
5275 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5276 m_addr += bytes_read;
5277 break;
5278 default:
5279 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5280 dwarf_form_name (attr.form),
5281 objfile_name (objfile));
5282 return NULL;
5283 }
5284 switch (attr.dw_idx)
5285 {
5286 case DW_IDX_compile_unit:
5287 /* Don't crash on bad data. */
5288 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5289 {
5290 complaint (_(".debug_names entry has bad CU index %s"
5291 " [in module %s]"),
5292 pulongest (ull),
5293 objfile_name (dwarf2_per_objfile->objfile));
5294 continue;
5295 }
5296 per_cu = dwarf2_per_objfile->get_cutu (ull);
5297 break;
5298 case DW_IDX_type_unit:
5299 /* Don't crash on bad data. */
5300 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5301 {
5302 complaint (_(".debug_names entry has bad TU index %s"
5303 " [in module %s]"),
5304 pulongest (ull),
5305 objfile_name (dwarf2_per_objfile->objfile));
5306 continue;
5307 }
5308 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5309 break;
5310 case DW_IDX_GNU_internal:
5311 if (!m_map.augmentation_is_gdb)
5312 break;
5313 symbol_linkage_ = symbol_linkage::static_;
5314 break;
5315 case DW_IDX_GNU_external:
5316 if (!m_map.augmentation_is_gdb)
5317 break;
5318 symbol_linkage_ = symbol_linkage::extern_;
5319 break;
5320 }
5321 }
5322
5323 /* Skip if already read in. */
5324 if (per_cu->v.quick->compunit_symtab)
5325 goto again;
5326
5327 /* Check static vs global. */
5328 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5329 {
5330 const bool want_static = *m_block_index == STATIC_BLOCK;
5331 const bool symbol_is_static =
5332 symbol_linkage_ == symbol_linkage::static_;
5333 if (want_static != symbol_is_static)
5334 goto again;
5335 }
5336
5337 /* Match dw2_symtab_iter_next, symbol_kind
5338 and debug_names::psymbol_tag. */
5339 switch (m_domain)
5340 {
5341 case VAR_DOMAIN:
5342 switch (indexval.dwarf_tag)
5343 {
5344 case DW_TAG_variable:
5345 case DW_TAG_subprogram:
5346 /* Some types are also in VAR_DOMAIN. */
5347 case DW_TAG_typedef:
5348 case DW_TAG_structure_type:
5349 break;
5350 default:
5351 goto again;
5352 }
5353 break;
5354 case STRUCT_DOMAIN:
5355 switch (indexval.dwarf_tag)
5356 {
5357 case DW_TAG_typedef:
5358 case DW_TAG_structure_type:
5359 break;
5360 default:
5361 goto again;
5362 }
5363 break;
5364 case LABEL_DOMAIN:
5365 switch (indexval.dwarf_tag)
5366 {
5367 case 0:
5368 case DW_TAG_variable:
5369 break;
5370 default:
5371 goto again;
5372 }
5373 break;
5374 case MODULE_DOMAIN:
5375 switch (indexval.dwarf_tag)
5376 {
5377 case DW_TAG_module:
5378 break;
5379 default:
5380 goto again;
5381 }
5382 break;
5383 default:
5384 break;
5385 }
5386
5387 /* Match dw2_expand_symtabs_matching, symbol_kind and
5388 debug_names::psymbol_tag. */
5389 switch (m_search)
5390 {
5391 case VARIABLES_DOMAIN:
5392 switch (indexval.dwarf_tag)
5393 {
5394 case DW_TAG_variable:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case FUNCTIONS_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_subprogram:
5404 break;
5405 default:
5406 goto again;
5407 }
5408 break;
5409 case TYPES_DOMAIN:
5410 switch (indexval.dwarf_tag)
5411 {
5412 case DW_TAG_typedef:
5413 case DW_TAG_structure_type:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
5419 case MODULES_DOMAIN:
5420 switch (indexval.dwarf_tag)
5421 {
5422 case DW_TAG_module:
5423 break;
5424 default:
5425 goto again;
5426 }
5427 default:
5428 break;
5429 }
5430
5431 return per_cu;
5432 }
5433
5434 static struct compunit_symtab *
5435 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5436 const char *name, domain_enum domain)
5437 {
5438 struct dwarf2_per_objfile *dwarf2_per_objfile
5439 = get_dwarf2_per_objfile (objfile);
5440
5441 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5442 if (!mapp)
5443 {
5444 /* index is NULL if OBJF_READNOW. */
5445 return NULL;
5446 }
5447 const auto &map = *mapp;
5448
5449 dw2_debug_names_iterator iter (map, block_index, domain, name);
5450
5451 struct compunit_symtab *stab_best = NULL;
5452 struct dwarf2_per_cu_data *per_cu;
5453 while ((per_cu = iter.next ()) != NULL)
5454 {
5455 struct symbol *sym, *with_opaque = NULL;
5456 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5457 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5458 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5459
5460 sym = block_find_symbol (block, name, domain,
5461 block_find_non_opaque_type_preferred,
5462 &with_opaque);
5463
5464 /* Some caution must be observed with overloaded functions and
5465 methods, since the index will not contain any overload
5466 information (but NAME might contain it). */
5467
5468 if (sym != NULL
5469 && strcmp_iw (sym->search_name (), name) == 0)
5470 return stab;
5471 if (with_opaque != NULL
5472 && strcmp_iw (with_opaque->search_name (), name) == 0)
5473 stab_best = stab;
5474
5475 /* Keep looking through other CUs. */
5476 }
5477
5478 return stab_best;
5479 }
5480
5481 /* This dumps minimal information about .debug_names. It is called
5482 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5483 uses this to verify that .debug_names has been loaded. */
5484
5485 static void
5486 dw2_debug_names_dump (struct objfile *objfile)
5487 {
5488 struct dwarf2_per_objfile *dwarf2_per_objfile
5489 = get_dwarf2_per_objfile (objfile);
5490
5491 gdb_assert (dwarf2_per_objfile->using_index);
5492 printf_filtered (".debug_names:");
5493 if (dwarf2_per_objfile->debug_names_table)
5494 printf_filtered (" exists\n");
5495 else
5496 printf_filtered (" faked for \"readnow\"\n");
5497 printf_filtered ("\n");
5498 }
5499
5500 static void
5501 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5502 const char *func_name)
5503 {
5504 struct dwarf2_per_objfile *dwarf2_per_objfile
5505 = get_dwarf2_per_objfile (objfile);
5506
5507 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5508 if (dwarf2_per_objfile->debug_names_table)
5509 {
5510 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5511
5512 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5513
5514 struct dwarf2_per_cu_data *per_cu;
5515 while ((per_cu = iter.next ()) != NULL)
5516 dw2_instantiate_symtab (per_cu, false);
5517 }
5518 }
5519
5520 static void
5521 dw2_debug_names_map_matching_symbols
5522 (struct objfile *objfile,
5523 const lookup_name_info &name, domain_enum domain,
5524 int global,
5525 gdb::function_view<symbol_found_callback_ftype> callback,
5526 symbol_compare_ftype *ordered_compare)
5527 {
5528 struct dwarf2_per_objfile *dwarf2_per_objfile
5529 = get_dwarf2_per_objfile (objfile);
5530
5531 /* debug_names_table is NULL if OBJF_READNOW. */
5532 if (!dwarf2_per_objfile->debug_names_table)
5533 return;
5534
5535 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5536 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5537
5538 const char *match_name = name.ada ().lookup_name ().c_str ();
5539 auto matcher = [&] (const char *symname)
5540 {
5541 if (ordered_compare == nullptr)
5542 return true;
5543 return ordered_compare (symname, match_name) == 0;
5544 };
5545
5546 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5547 [&] (offset_type namei)
5548 {
5549 /* The name was matched, now expand corresponding CUs that were
5550 marked. */
5551 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5552
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
5555 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5556 return true;
5557 });
5558
5559 /* It's a shame we couldn't do this inside the
5560 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5561 that have already been expanded. Instead, this loop matches what
5562 the psymtab code does. */
5563 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5564 {
5565 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5566 if (cust != nullptr)
5567 {
5568 const struct block *block
5569 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5570 if (!iterate_over_symbols_terminated (block, name,
5571 domain, callback))
5572 break;
5573 }
5574 }
5575 }
5576
5577 static void
5578 dw2_debug_names_expand_symtabs_matching
5579 (struct objfile *objfile,
5580 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5581 const lookup_name_info &lookup_name,
5582 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5583 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5584 enum search_domain kind)
5585 {
5586 struct dwarf2_per_objfile *dwarf2_per_objfile
5587 = get_dwarf2_per_objfile (objfile);
5588
5589 /* debug_names_table is NULL if OBJF_READNOW. */
5590 if (!dwarf2_per_objfile->debug_names_table)
5591 return;
5592
5593 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5594
5595 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5596
5597 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5598 symbol_matcher,
5599 kind, [&] (offset_type namei)
5600 {
5601 /* The name was matched, now expand corresponding CUs that were
5602 marked. */
5603 dw2_debug_names_iterator iter (map, kind, namei);
5604
5605 struct dwarf2_per_cu_data *per_cu;
5606 while ((per_cu = iter.next ()) != NULL)
5607 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5608 expansion_notify);
5609 return true;
5610 });
5611 }
5612
5613 const struct quick_symbol_functions dwarf2_debug_names_functions =
5614 {
5615 dw2_has_symbols,
5616 dw2_find_last_source_symtab,
5617 dw2_forget_cached_source_info,
5618 dw2_map_symtabs_matching_filename,
5619 dw2_debug_names_lookup_symbol,
5620 NULL,
5621 dw2_print_stats,
5622 dw2_debug_names_dump,
5623 dw2_debug_names_expand_symtabs_for_function,
5624 dw2_expand_all_symtabs,
5625 dw2_expand_symtabs_with_fullname,
5626 dw2_debug_names_map_matching_symbols,
5627 dw2_debug_names_expand_symtabs_matching,
5628 dw2_find_pc_sect_compunit_symtab,
5629 NULL,
5630 dw2_map_symbol_filenames
5631 };
5632
5633 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5634 to either a dwarf2_per_objfile or dwz_file object. */
5635
5636 template <typename T>
5637 static gdb::array_view<const gdb_byte>
5638 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5639 {
5640 dwarf2_section_info *section = &section_owner->gdb_index;
5641
5642 if (section->empty ())
5643 return {};
5644
5645 /* Older elfutils strip versions could keep the section in the main
5646 executable while splitting it for the separate debug info file. */
5647 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5648 return {};
5649
5650 section->read (obj);
5651
5652 /* dwarf2_section_info::size is a bfd_size_type, while
5653 gdb::array_view works with size_t. On 32-bit hosts, with
5654 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5655 is 32-bit. So we need an explicit narrowing conversion here.
5656 This is fine, because it's impossible to allocate or mmap an
5657 array/buffer larger than what size_t can represent. */
5658 return gdb::make_array_view (section->buffer, section->size);
5659 }
5660
5661 /* Lookup the index cache for the contents of the index associated to
5662 DWARF2_OBJ. */
5663
5664 static gdb::array_view<const gdb_byte>
5665 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5666 {
5667 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5668 if (build_id == nullptr)
5669 return {};
5670
5671 return global_index_cache.lookup_gdb_index (build_id,
5672 &dwarf2_obj->index_cache_res);
5673 }
5674
5675 /* Same as the above, but for DWZ. */
5676
5677 static gdb::array_view<const gdb_byte>
5678 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5679 {
5680 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5681 if (build_id == nullptr)
5682 return {};
5683
5684 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5685 }
5686
5687 /* See symfile.h. */
5688
5689 bool
5690 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5691 {
5692 struct dwarf2_per_objfile *dwarf2_per_objfile
5693 = get_dwarf2_per_objfile (objfile);
5694
5695 /* If we're about to read full symbols, don't bother with the
5696 indices. In this case we also don't care if some other debug
5697 format is making psymtabs, because they are all about to be
5698 expanded anyway. */
5699 if ((objfile->flags & OBJF_READNOW))
5700 {
5701 dwarf2_per_objfile->using_index = 1;
5702 create_all_comp_units (dwarf2_per_objfile);
5703 create_all_type_units (dwarf2_per_objfile);
5704 dwarf2_per_objfile->quick_file_names_table
5705 = create_quick_file_names_table
5706 (dwarf2_per_objfile->all_comp_units.size ());
5707
5708 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5709 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5710 {
5711 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5712
5713 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5714 struct dwarf2_per_cu_quick_data);
5715 }
5716
5717 /* Return 1 so that gdb sees the "quick" functions. However,
5718 these functions will be no-ops because we will have expanded
5719 all symtabs. */
5720 *index_kind = dw_index_kind::GDB_INDEX;
5721 return true;
5722 }
5723
5724 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5725 {
5726 *index_kind = dw_index_kind::DEBUG_NAMES;
5727 return true;
5728 }
5729
5730 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5731 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5732 get_gdb_index_contents_from_section<dwz_file>))
5733 {
5734 *index_kind = dw_index_kind::GDB_INDEX;
5735 return true;
5736 }
5737
5738 /* ... otherwise, try to find the index in the index cache. */
5739 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5740 get_gdb_index_contents_from_cache,
5741 get_gdb_index_contents_from_cache_dwz))
5742 {
5743 global_index_cache.hit ();
5744 *index_kind = dw_index_kind::GDB_INDEX;
5745 return true;
5746 }
5747
5748 global_index_cache.miss ();
5749 return false;
5750 }
5751
5752 \f
5753
5754 /* Build a partial symbol table. */
5755
5756 void
5757 dwarf2_build_psymtabs (struct objfile *objfile)
5758 {
5759 struct dwarf2_per_objfile *dwarf2_per_objfile
5760 = get_dwarf2_per_objfile (objfile);
5761
5762 init_psymbol_list (objfile, 1024);
5763
5764 try
5765 {
5766 /* This isn't really ideal: all the data we allocate on the
5767 objfile's obstack is still uselessly kept around. However,
5768 freeing it seems unsafe. */
5769 psymtab_discarder psymtabs (objfile);
5770 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5771 psymtabs.keep ();
5772
5773 /* (maybe) store an index in the cache. */
5774 global_index_cache.store (dwarf2_per_objfile);
5775 }
5776 catch (const gdb_exception_error &except)
5777 {
5778 exception_print (gdb_stderr, except);
5779 }
5780 }
5781
5782 /* Find the base address of the compilation unit for range lists and
5783 location lists. It will normally be specified by DW_AT_low_pc.
5784 In DWARF-3 draft 4, the base address could be overridden by
5785 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5786 compilation units with discontinuous ranges. */
5787
5788 static void
5789 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5790 {
5791 struct attribute *attr;
5792
5793 cu->base_address.reset ();
5794
5795 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5796 if (attr != nullptr)
5797 cu->base_address = attr->value_as_address ();
5798 else
5799 {
5800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5801 if (attr != nullptr)
5802 cu->base_address = attr->value_as_address ();
5803 }
5804 }
5805
5806 /* Helper function that returns the proper abbrev section for
5807 THIS_CU. */
5808
5809 static struct dwarf2_section_info *
5810 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5811 {
5812 struct dwarf2_section_info *abbrev;
5813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5814
5815 if (this_cu->is_dwz)
5816 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5817 else
5818 abbrev = &dwarf2_per_objfile->abbrev;
5819
5820 return abbrev;
5821 }
5822
5823 /* Fetch the abbreviation table offset from a comp or type unit header. */
5824
5825 static sect_offset
5826 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5827 struct dwarf2_section_info *section,
5828 sect_offset sect_off)
5829 {
5830 bfd *abfd = section->get_bfd_owner ();
5831 const gdb_byte *info_ptr;
5832 unsigned int initial_length_size, offset_size;
5833 uint16_t version;
5834
5835 section->read (dwarf2_per_objfile->objfile);
5836 info_ptr = section->buffer + to_underlying (sect_off);
5837 read_initial_length (abfd, info_ptr, &initial_length_size);
5838 offset_size = initial_length_size == 4 ? 4 : 8;
5839 info_ptr += initial_length_size;
5840
5841 version = read_2_bytes (abfd, info_ptr);
5842 info_ptr += 2;
5843 if (version >= 5)
5844 {
5845 /* Skip unit type and address size. */
5846 info_ptr += 2;
5847 }
5848
5849 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5850 }
5851
5852 /* A partial symtab that is used only for include files. */
5853 struct dwarf2_include_psymtab : public partial_symtab
5854 {
5855 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5856 : partial_symtab (filename, objfile)
5857 {
5858 }
5859
5860 void read_symtab (struct objfile *objfile) override
5861 {
5862 expand_psymtab (objfile);
5863 }
5864
5865 void expand_psymtab (struct objfile *objfile) override
5866 {
5867 if (m_readin)
5868 return;
5869 /* It's an include file, no symbols to read for it.
5870 Everything is in the parent symtab. */
5871 expand_dependencies (objfile);
5872 m_readin = true;
5873 }
5874
5875 bool readin_p () const override
5876 {
5877 return m_readin;
5878 }
5879
5880 struct compunit_symtab *get_compunit_symtab () const override
5881 {
5882 return nullptr;
5883 }
5884
5885 private:
5886
5887 bool m_readin = false;
5888 };
5889
5890 /* Allocate a new partial symtab for file named NAME and mark this new
5891 partial symtab as being an include of PST. */
5892
5893 static void
5894 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5895 struct objfile *objfile)
5896 {
5897 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5898
5899 if (!IS_ABSOLUTE_PATH (subpst->filename))
5900 {
5901 /* It shares objfile->objfile_obstack. */
5902 subpst->dirname = pst->dirname;
5903 }
5904
5905 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5906 subpst->dependencies[0] = pst;
5907 subpst->number_of_dependencies = 1;
5908 }
5909
5910 /* Read the Line Number Program data and extract the list of files
5911 included by the source file represented by PST. Build an include
5912 partial symtab for each of these included files. */
5913
5914 static void
5915 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5916 struct die_info *die,
5917 dwarf2_psymtab *pst)
5918 {
5919 line_header_up lh;
5920 struct attribute *attr;
5921
5922 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5923 if (attr != nullptr)
5924 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5925 if (lh == NULL)
5926 return; /* No linetable, so no includes. */
5927
5928 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5929 that we pass in the raw text_low here; that is ok because we're
5930 only decoding the line table to make include partial symtabs, and
5931 so the addresses aren't really used. */
5932 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5933 pst->raw_text_low (), 1);
5934 }
5935
5936 static hashval_t
5937 hash_signatured_type (const void *item)
5938 {
5939 const struct signatured_type *sig_type
5940 = (const struct signatured_type *) item;
5941
5942 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5943 return sig_type->signature;
5944 }
5945
5946 static int
5947 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5948 {
5949 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5950 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5951
5952 return lhs->signature == rhs->signature;
5953 }
5954
5955 /* Allocate a hash table for signatured types. */
5956
5957 static htab_up
5958 allocate_signatured_type_table ()
5959 {
5960 return htab_up (htab_create_alloc (41,
5961 hash_signatured_type,
5962 eq_signatured_type,
5963 NULL, xcalloc, xfree));
5964 }
5965
5966 /* A helper function to add a signatured type CU to a table. */
5967
5968 static int
5969 add_signatured_type_cu_to_table (void **slot, void *datum)
5970 {
5971 struct signatured_type *sigt = (struct signatured_type *) *slot;
5972 std::vector<signatured_type *> *all_type_units
5973 = (std::vector<signatured_type *> *) datum;
5974
5975 all_type_units->push_back (sigt);
5976
5977 return 1;
5978 }
5979
5980 /* A helper for create_debug_types_hash_table. Read types from SECTION
5981 and fill them into TYPES_HTAB. It will process only type units,
5982 therefore DW_UT_type. */
5983
5984 static void
5985 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5986 struct dwo_file *dwo_file,
5987 dwarf2_section_info *section, htab_up &types_htab,
5988 rcuh_kind section_kind)
5989 {
5990 struct objfile *objfile = dwarf2_per_objfile->objfile;
5991 struct dwarf2_section_info *abbrev_section;
5992 bfd *abfd;
5993 const gdb_byte *info_ptr, *end_ptr;
5994
5995 abbrev_section = (dwo_file != NULL
5996 ? &dwo_file->sections.abbrev
5997 : &dwarf2_per_objfile->abbrev);
5998
5999 if (dwarf_read_debug)
6000 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6001 section->get_name (),
6002 abbrev_section->get_file_name ());
6003
6004 section->read (objfile);
6005 info_ptr = section->buffer;
6006
6007 if (info_ptr == NULL)
6008 return;
6009
6010 /* We can't set abfd until now because the section may be empty or
6011 not present, in which case the bfd is unknown. */
6012 abfd = section->get_bfd_owner ();
6013
6014 /* We don't use cutu_reader here because we don't need to read
6015 any dies: the signature is in the header. */
6016
6017 end_ptr = info_ptr + section->size;
6018 while (info_ptr < end_ptr)
6019 {
6020 struct signatured_type *sig_type;
6021 struct dwo_unit *dwo_tu;
6022 void **slot;
6023 const gdb_byte *ptr = info_ptr;
6024 struct comp_unit_head header;
6025 unsigned int length;
6026
6027 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6028
6029 /* Initialize it due to a false compiler warning. */
6030 header.signature = -1;
6031 header.type_cu_offset_in_tu = (cu_offset) -1;
6032
6033 /* We need to read the type's signature in order to build the hash
6034 table, but we don't need anything else just yet. */
6035
6036 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6037 abbrev_section, ptr, section_kind);
6038
6039 length = header.get_length ();
6040
6041 /* Skip dummy type units. */
6042 if (ptr >= info_ptr + length
6043 || peek_abbrev_code (abfd, ptr) == 0
6044 || header.unit_type != DW_UT_type)
6045 {
6046 info_ptr += length;
6047 continue;
6048 }
6049
6050 if (types_htab == NULL)
6051 {
6052 if (dwo_file)
6053 types_htab = allocate_dwo_unit_table ();
6054 else
6055 types_htab = allocate_signatured_type_table ();
6056 }
6057
6058 if (dwo_file)
6059 {
6060 sig_type = NULL;
6061 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6062 struct dwo_unit);
6063 dwo_tu->dwo_file = dwo_file;
6064 dwo_tu->signature = header.signature;
6065 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6066 dwo_tu->section = section;
6067 dwo_tu->sect_off = sect_off;
6068 dwo_tu->length = length;
6069 }
6070 else
6071 {
6072 /* N.B.: type_offset is not usable if this type uses a DWO file.
6073 The real type_offset is in the DWO file. */
6074 dwo_tu = NULL;
6075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6076 struct signatured_type);
6077 sig_type->signature = header.signature;
6078 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6079 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6080 sig_type->per_cu.is_debug_types = 1;
6081 sig_type->per_cu.section = section;
6082 sig_type->per_cu.sect_off = sect_off;
6083 sig_type->per_cu.length = length;
6084 }
6085
6086 slot = htab_find_slot (types_htab.get (),
6087 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6088 INSERT);
6089 gdb_assert (slot != NULL);
6090 if (*slot != NULL)
6091 {
6092 sect_offset dup_sect_off;
6093
6094 if (dwo_file)
6095 {
6096 const struct dwo_unit *dup_tu
6097 = (const struct dwo_unit *) *slot;
6098
6099 dup_sect_off = dup_tu->sect_off;
6100 }
6101 else
6102 {
6103 const struct signatured_type *dup_tu
6104 = (const struct signatured_type *) *slot;
6105
6106 dup_sect_off = dup_tu->per_cu.sect_off;
6107 }
6108
6109 complaint (_("debug type entry at offset %s is duplicate to"
6110 " the entry at offset %s, signature %s"),
6111 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6112 hex_string (header.signature));
6113 }
6114 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6115
6116 if (dwarf_read_debug > 1)
6117 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6118 sect_offset_str (sect_off),
6119 hex_string (header.signature));
6120
6121 info_ptr += length;
6122 }
6123 }
6124
6125 /* Create the hash table of all entries in the .debug_types
6126 (or .debug_types.dwo) section(s).
6127 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6128 otherwise it is NULL.
6129
6130 The result is a pointer to the hash table or NULL if there are no types.
6131
6132 Note: This function processes DWO files only, not DWP files. */
6133
6134 static void
6135 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6136 struct dwo_file *dwo_file,
6137 gdb::array_view<dwarf2_section_info> type_sections,
6138 htab_up &types_htab)
6139 {
6140 for (dwarf2_section_info &section : type_sections)
6141 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6142 types_htab, rcuh_kind::TYPE);
6143 }
6144
6145 /* Create the hash table of all entries in the .debug_types section,
6146 and initialize all_type_units.
6147 The result is zero if there is an error (e.g. missing .debug_types section),
6148 otherwise non-zero. */
6149
6150 static int
6151 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6152 {
6153 htab_up types_htab;
6154
6155 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6156 &dwarf2_per_objfile->info, types_htab,
6157 rcuh_kind::COMPILE);
6158 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6159 dwarf2_per_objfile->types, types_htab);
6160 if (types_htab == NULL)
6161 {
6162 dwarf2_per_objfile->signatured_types = NULL;
6163 return 0;
6164 }
6165
6166 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6167
6168 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6169 dwarf2_per_objfile->all_type_units.reserve
6170 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6171
6172 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6173 add_signatured_type_cu_to_table,
6174 &dwarf2_per_objfile->all_type_units);
6175
6176 return 1;
6177 }
6178
6179 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6180 If SLOT is non-NULL, it is the entry to use in the hash table.
6181 Otherwise we find one. */
6182
6183 static struct signatured_type *
6184 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6185 void **slot)
6186 {
6187 struct objfile *objfile = dwarf2_per_objfile->objfile;
6188
6189 if (dwarf2_per_objfile->all_type_units.size ()
6190 == dwarf2_per_objfile->all_type_units.capacity ())
6191 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6192
6193 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6194 struct signatured_type);
6195
6196 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6197 sig_type->signature = sig;
6198 sig_type->per_cu.is_debug_types = 1;
6199 if (dwarf2_per_objfile->using_index)
6200 {
6201 sig_type->per_cu.v.quick =
6202 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6203 struct dwarf2_per_cu_quick_data);
6204 }
6205
6206 if (slot == NULL)
6207 {
6208 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6209 sig_type, INSERT);
6210 }
6211 gdb_assert (*slot == NULL);
6212 *slot = sig_type;
6213 /* The rest of sig_type must be filled in by the caller. */
6214 return sig_type;
6215 }
6216
6217 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6218 Fill in SIG_ENTRY with DWO_ENTRY. */
6219
6220 static void
6221 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6222 struct signatured_type *sig_entry,
6223 struct dwo_unit *dwo_entry)
6224 {
6225 /* Make sure we're not clobbering something we don't expect to. */
6226 gdb_assert (! sig_entry->per_cu.queued);
6227 gdb_assert (sig_entry->per_cu.cu == NULL);
6228 if (dwarf2_per_objfile->using_index)
6229 {
6230 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6231 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6232 }
6233 else
6234 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6235 gdb_assert (sig_entry->signature == dwo_entry->signature);
6236 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6237 gdb_assert (sig_entry->type_unit_group == NULL);
6238 gdb_assert (sig_entry->dwo_unit == NULL);
6239
6240 sig_entry->per_cu.section = dwo_entry->section;
6241 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6242 sig_entry->per_cu.length = dwo_entry->length;
6243 sig_entry->per_cu.reading_dwo_directly = 1;
6244 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6245 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6246 sig_entry->dwo_unit = dwo_entry;
6247 }
6248
6249 /* Subroutine of lookup_signatured_type.
6250 If we haven't read the TU yet, create the signatured_type data structure
6251 for a TU to be read in directly from a DWO file, bypassing the stub.
6252 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6253 using .gdb_index, then when reading a CU we want to stay in the DWO file
6254 containing that CU. Otherwise we could end up reading several other DWO
6255 files (due to comdat folding) to process the transitive closure of all the
6256 mentioned TUs, and that can be slow. The current DWO file will have every
6257 type signature that it needs.
6258 We only do this for .gdb_index because in the psymtab case we already have
6259 to read all the DWOs to build the type unit groups. */
6260
6261 static struct signatured_type *
6262 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6263 {
6264 struct dwarf2_per_objfile *dwarf2_per_objfile
6265 = cu->per_cu->dwarf2_per_objfile;
6266 struct dwo_file *dwo_file;
6267 struct dwo_unit find_dwo_entry, *dwo_entry;
6268 struct signatured_type find_sig_entry, *sig_entry;
6269 void **slot;
6270
6271 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6272
6273 /* If TU skeletons have been removed then we may not have read in any
6274 TUs yet. */
6275 if (dwarf2_per_objfile->signatured_types == NULL)
6276 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6277
6278 /* We only ever need to read in one copy of a signatured type.
6279 Use the global signatured_types array to do our own comdat-folding
6280 of types. If this is the first time we're reading this TU, and
6281 the TU has an entry in .gdb_index, replace the recorded data from
6282 .gdb_index with this TU. */
6283
6284 find_sig_entry.signature = sig;
6285 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6286 &find_sig_entry, INSERT);
6287 sig_entry = (struct signatured_type *) *slot;
6288
6289 /* We can get here with the TU already read, *or* in the process of being
6290 read. Don't reassign the global entry to point to this DWO if that's
6291 the case. Also note that if the TU is already being read, it may not
6292 have come from a DWO, the program may be a mix of Fission-compiled
6293 code and non-Fission-compiled code. */
6294
6295 /* Have we already tried to read this TU?
6296 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6297 needn't exist in the global table yet). */
6298 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6299 return sig_entry;
6300
6301 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6302 dwo_unit of the TU itself. */
6303 dwo_file = cu->dwo_unit->dwo_file;
6304
6305 /* Ok, this is the first time we're reading this TU. */
6306 if (dwo_file->tus == NULL)
6307 return NULL;
6308 find_dwo_entry.signature = sig;
6309 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6310 &find_dwo_entry);
6311 if (dwo_entry == NULL)
6312 return NULL;
6313
6314 /* If the global table doesn't have an entry for this TU, add one. */
6315 if (sig_entry == NULL)
6316 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6317
6318 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6319 sig_entry->per_cu.tu_read = 1;
6320 return sig_entry;
6321 }
6322
6323 /* Subroutine of lookup_signatured_type.
6324 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6325 then try the DWP file. If the TU stub (skeleton) has been removed then
6326 it won't be in .gdb_index. */
6327
6328 static struct signatured_type *
6329 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6330 {
6331 struct dwarf2_per_objfile *dwarf2_per_objfile
6332 = cu->per_cu->dwarf2_per_objfile;
6333 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6334 struct dwo_unit *dwo_entry;
6335 struct signatured_type find_sig_entry, *sig_entry;
6336 void **slot;
6337
6338 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6339 gdb_assert (dwp_file != NULL);
6340
6341 /* If TU skeletons have been removed then we may not have read in any
6342 TUs yet. */
6343 if (dwarf2_per_objfile->signatured_types == NULL)
6344 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6345
6346 find_sig_entry.signature = sig;
6347 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6348 &find_sig_entry, INSERT);
6349 sig_entry = (struct signatured_type *) *slot;
6350
6351 /* Have we already tried to read this TU?
6352 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6353 needn't exist in the global table yet). */
6354 if (sig_entry != NULL)
6355 return sig_entry;
6356
6357 if (dwp_file->tus == NULL)
6358 return NULL;
6359 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6360 sig, 1 /* is_debug_types */);
6361 if (dwo_entry == NULL)
6362 return NULL;
6363
6364 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6365 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6366
6367 return sig_entry;
6368 }
6369
6370 /* Lookup a signature based type for DW_FORM_ref_sig8.
6371 Returns NULL if signature SIG is not present in the table.
6372 It is up to the caller to complain about this. */
6373
6374 static struct signatured_type *
6375 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6376 {
6377 struct dwarf2_per_objfile *dwarf2_per_objfile
6378 = cu->per_cu->dwarf2_per_objfile;
6379
6380 if (cu->dwo_unit
6381 && dwarf2_per_objfile->using_index)
6382 {
6383 /* We're in a DWO/DWP file, and we're using .gdb_index.
6384 These cases require special processing. */
6385 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6386 return lookup_dwo_signatured_type (cu, sig);
6387 else
6388 return lookup_dwp_signatured_type (cu, sig);
6389 }
6390 else
6391 {
6392 struct signatured_type find_entry, *entry;
6393
6394 if (dwarf2_per_objfile->signatured_types == NULL)
6395 return NULL;
6396 find_entry.signature = sig;
6397 entry = ((struct signatured_type *)
6398 htab_find (dwarf2_per_objfile->signatured_types.get (),
6399 &find_entry));
6400 return entry;
6401 }
6402 }
6403
6404 /* Low level DIE reading support. */
6405
6406 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6407
6408 static void
6409 init_cu_die_reader (struct die_reader_specs *reader,
6410 struct dwarf2_cu *cu,
6411 struct dwarf2_section_info *section,
6412 struct dwo_file *dwo_file,
6413 struct abbrev_table *abbrev_table)
6414 {
6415 gdb_assert (section->readin && section->buffer != NULL);
6416 reader->abfd = section->get_bfd_owner ();
6417 reader->cu = cu;
6418 reader->dwo_file = dwo_file;
6419 reader->die_section = section;
6420 reader->buffer = section->buffer;
6421 reader->buffer_end = section->buffer + section->size;
6422 reader->abbrev_table = abbrev_table;
6423 }
6424
6425 /* Subroutine of cutu_reader to simplify it.
6426 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6427 There's just a lot of work to do, and cutu_reader is big enough
6428 already.
6429
6430 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6431 from it to the DIE in the DWO. If NULL we are skipping the stub.
6432 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6433 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6434 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6435 STUB_COMP_DIR may be non-NULL.
6436 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6437 are filled in with the info of the DIE from the DWO file.
6438 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6439 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6440 kept around for at least as long as *RESULT_READER.
6441
6442 The result is non-zero if a valid (non-dummy) DIE was found. */
6443
6444 static int
6445 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6446 struct dwo_unit *dwo_unit,
6447 struct die_info *stub_comp_unit_die,
6448 const char *stub_comp_dir,
6449 struct die_reader_specs *result_reader,
6450 const gdb_byte **result_info_ptr,
6451 struct die_info **result_comp_unit_die,
6452 abbrev_table_up *result_dwo_abbrev_table)
6453 {
6454 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6455 struct objfile *objfile = dwarf2_per_objfile->objfile;
6456 struct dwarf2_cu *cu = this_cu->cu;
6457 bfd *abfd;
6458 const gdb_byte *begin_info_ptr, *info_ptr;
6459 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6460 int i,num_extra_attrs;
6461 struct dwarf2_section_info *dwo_abbrev_section;
6462 struct die_info *comp_unit_die;
6463
6464 /* At most one of these may be provided. */
6465 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6466
6467 /* These attributes aren't processed until later:
6468 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6469 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6470 referenced later. However, these attributes are found in the stub
6471 which we won't have later. In order to not impose this complication
6472 on the rest of the code, we read them here and copy them to the
6473 DWO CU/TU die. */
6474
6475 stmt_list = NULL;
6476 low_pc = NULL;
6477 high_pc = NULL;
6478 ranges = NULL;
6479 comp_dir = NULL;
6480
6481 if (stub_comp_unit_die != NULL)
6482 {
6483 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6484 DWO file. */
6485 if (! this_cu->is_debug_types)
6486 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6487 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6488 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6489 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6490 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6491
6492 cu->addr_base = stub_comp_unit_die->addr_base ();
6493
6494 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6495 here (if needed). We need the value before we can process
6496 DW_AT_ranges. */
6497 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6498 }
6499 else if (stub_comp_dir != NULL)
6500 {
6501 /* Reconstruct the comp_dir attribute to simplify the code below. */
6502 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6503 comp_dir->name = DW_AT_comp_dir;
6504 comp_dir->form = DW_FORM_string;
6505 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6506 DW_STRING (comp_dir) = stub_comp_dir;
6507 }
6508
6509 /* Set up for reading the DWO CU/TU. */
6510 cu->dwo_unit = dwo_unit;
6511 dwarf2_section_info *section = dwo_unit->section;
6512 section->read (objfile);
6513 abfd = section->get_bfd_owner ();
6514 begin_info_ptr = info_ptr = (section->buffer
6515 + to_underlying (dwo_unit->sect_off));
6516 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6517
6518 if (this_cu->is_debug_types)
6519 {
6520 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6521
6522 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6523 &cu->header, section,
6524 dwo_abbrev_section,
6525 info_ptr, rcuh_kind::TYPE);
6526 /* This is not an assert because it can be caused by bad debug info. */
6527 if (sig_type->signature != cu->header.signature)
6528 {
6529 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6530 " TU at offset %s [in module %s]"),
6531 hex_string (sig_type->signature),
6532 hex_string (cu->header.signature),
6533 sect_offset_str (dwo_unit->sect_off),
6534 bfd_get_filename (abfd));
6535 }
6536 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6537 /* For DWOs coming from DWP files, we don't know the CU length
6538 nor the type's offset in the TU until now. */
6539 dwo_unit->length = cu->header.get_length ();
6540 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6541
6542 /* Establish the type offset that can be used to lookup the type.
6543 For DWO files, we don't know it until now. */
6544 sig_type->type_offset_in_section
6545 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6546 }
6547 else
6548 {
6549 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6550 &cu->header, section,
6551 dwo_abbrev_section,
6552 info_ptr, rcuh_kind::COMPILE);
6553 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6554 /* For DWOs coming from DWP files, we don't know the CU length
6555 until now. */
6556 dwo_unit->length = cu->header.get_length ();
6557 }
6558
6559 *result_dwo_abbrev_table
6560 = abbrev_table::read (objfile, dwo_abbrev_section,
6561 cu->header.abbrev_sect_off);
6562 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6563 result_dwo_abbrev_table->get ());
6564
6565 /* Read in the die, but leave space to copy over the attributes
6566 from the stub. This has the benefit of simplifying the rest of
6567 the code - all the work to maintain the illusion of a single
6568 DW_TAG_{compile,type}_unit DIE is done here. */
6569 num_extra_attrs = ((stmt_list != NULL)
6570 + (low_pc != NULL)
6571 + (high_pc != NULL)
6572 + (ranges != NULL)
6573 + (comp_dir != NULL));
6574 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6575 num_extra_attrs);
6576
6577 /* Copy over the attributes from the stub to the DIE we just read in. */
6578 comp_unit_die = *result_comp_unit_die;
6579 i = comp_unit_die->num_attrs;
6580 if (stmt_list != NULL)
6581 comp_unit_die->attrs[i++] = *stmt_list;
6582 if (low_pc != NULL)
6583 comp_unit_die->attrs[i++] = *low_pc;
6584 if (high_pc != NULL)
6585 comp_unit_die->attrs[i++] = *high_pc;
6586 if (ranges != NULL)
6587 comp_unit_die->attrs[i++] = *ranges;
6588 if (comp_dir != NULL)
6589 comp_unit_die->attrs[i++] = *comp_dir;
6590 comp_unit_die->num_attrs += num_extra_attrs;
6591
6592 if (dwarf_die_debug)
6593 {
6594 fprintf_unfiltered (gdb_stdlog,
6595 "Read die from %s@0x%x of %s:\n",
6596 section->get_name (),
6597 (unsigned) (begin_info_ptr - section->buffer),
6598 bfd_get_filename (abfd));
6599 dump_die (comp_unit_die, dwarf_die_debug);
6600 }
6601
6602 /* Skip dummy compilation units. */
6603 if (info_ptr >= begin_info_ptr + dwo_unit->length
6604 || peek_abbrev_code (abfd, info_ptr) == 0)
6605 return 0;
6606
6607 *result_info_ptr = info_ptr;
6608 return 1;
6609 }
6610
6611 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6612 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6613 signature is part of the header. */
6614 static gdb::optional<ULONGEST>
6615 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6616 {
6617 if (cu->header.version >= 5)
6618 return cu->header.signature;
6619 struct attribute *attr;
6620 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6621 if (attr == nullptr)
6622 return gdb::optional<ULONGEST> ();
6623 return DW_UNSND (attr);
6624 }
6625
6626 /* Subroutine of cutu_reader to simplify it.
6627 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6628 Returns NULL if the specified DWO unit cannot be found. */
6629
6630 static struct dwo_unit *
6631 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6632 struct die_info *comp_unit_die,
6633 const char *dwo_name)
6634 {
6635 struct dwarf2_cu *cu = this_cu->cu;
6636 struct dwo_unit *dwo_unit;
6637 const char *comp_dir;
6638
6639 gdb_assert (cu != NULL);
6640
6641 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6642 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6643 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6644
6645 if (this_cu->is_debug_types)
6646 {
6647 struct signatured_type *sig_type;
6648
6649 /* Since this_cu is the first member of struct signatured_type,
6650 we can go from a pointer to one to a pointer to the other. */
6651 sig_type = (struct signatured_type *) this_cu;
6652 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6653 }
6654 else
6655 {
6656 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6657 if (!signature.has_value ())
6658 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6659 " [in module %s]"),
6660 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6661 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6662 *signature);
6663 }
6664
6665 return dwo_unit;
6666 }
6667
6668 /* Subroutine of cutu_reader to simplify it.
6669 See it for a description of the parameters.
6670 Read a TU directly from a DWO file, bypassing the stub. */
6671
6672 void
6673 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6674 int use_existing_cu)
6675 {
6676 struct signatured_type *sig_type;
6677
6678 /* Verify we can do the following downcast, and that we have the
6679 data we need. */
6680 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6681 sig_type = (struct signatured_type *) this_cu;
6682 gdb_assert (sig_type->dwo_unit != NULL);
6683
6684 if (use_existing_cu && this_cu->cu != NULL)
6685 {
6686 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6687 /* There's no need to do the rereading_dwo_cu handling that
6688 cutu_reader does since we don't read the stub. */
6689 }
6690 else
6691 {
6692 /* If !use_existing_cu, this_cu->cu must be NULL. */
6693 gdb_assert (this_cu->cu == NULL);
6694 m_new_cu.reset (new dwarf2_cu (this_cu));
6695 }
6696
6697 /* A future optimization, if needed, would be to use an existing
6698 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6699 could share abbrev tables. */
6700
6701 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6702 NULL /* stub_comp_unit_die */,
6703 sig_type->dwo_unit->dwo_file->comp_dir,
6704 this, &info_ptr,
6705 &comp_unit_die,
6706 &m_dwo_abbrev_table) == 0)
6707 {
6708 /* Dummy die. */
6709 dummy_p = true;
6710 }
6711 }
6712
6713 /* Initialize a CU (or TU) and read its DIEs.
6714 If the CU defers to a DWO file, read the DWO file as well.
6715
6716 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6717 Otherwise the table specified in the comp unit header is read in and used.
6718 This is an optimization for when we already have the abbrev table.
6719
6720 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6721 Otherwise, a new CU is allocated with xmalloc. */
6722
6723 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6724 struct abbrev_table *abbrev_table,
6725 int use_existing_cu,
6726 bool skip_partial)
6727 : die_reader_specs {},
6728 m_this_cu (this_cu)
6729 {
6730 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6731 struct objfile *objfile = dwarf2_per_objfile->objfile;
6732 struct dwarf2_section_info *section = this_cu->section;
6733 bfd *abfd = section->get_bfd_owner ();
6734 struct dwarf2_cu *cu;
6735 const gdb_byte *begin_info_ptr;
6736 struct signatured_type *sig_type = NULL;
6737 struct dwarf2_section_info *abbrev_section;
6738 /* Non-zero if CU currently points to a DWO file and we need to
6739 reread it. When this happens we need to reread the skeleton die
6740 before we can reread the DWO file (this only applies to CUs, not TUs). */
6741 int rereading_dwo_cu = 0;
6742
6743 if (dwarf_die_debug)
6744 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6745 this_cu->is_debug_types ? "type" : "comp",
6746 sect_offset_str (this_cu->sect_off));
6747
6748 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6749 file (instead of going through the stub), short-circuit all of this. */
6750 if (this_cu->reading_dwo_directly)
6751 {
6752 /* Narrow down the scope of possibilities to have to understand. */
6753 gdb_assert (this_cu->is_debug_types);
6754 gdb_assert (abbrev_table == NULL);
6755 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6756 return;
6757 }
6758
6759 /* This is cheap if the section is already read in. */
6760 section->read (objfile);
6761
6762 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6763
6764 abbrev_section = get_abbrev_section_for_cu (this_cu);
6765
6766 if (use_existing_cu && this_cu->cu != NULL)
6767 {
6768 cu = this_cu->cu;
6769 /* If this CU is from a DWO file we need to start over, we need to
6770 refetch the attributes from the skeleton CU.
6771 This could be optimized by retrieving those attributes from when we
6772 were here the first time: the previous comp_unit_die was stored in
6773 comp_unit_obstack. But there's no data yet that we need this
6774 optimization. */
6775 if (cu->dwo_unit != NULL)
6776 rereading_dwo_cu = 1;
6777 }
6778 else
6779 {
6780 /* If !use_existing_cu, this_cu->cu must be NULL. */
6781 gdb_assert (this_cu->cu == NULL);
6782 m_new_cu.reset (new dwarf2_cu (this_cu));
6783 cu = m_new_cu.get ();
6784 }
6785
6786 /* Get the header. */
6787 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6788 {
6789 /* We already have the header, there's no need to read it in again. */
6790 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6791 }
6792 else
6793 {
6794 if (this_cu->is_debug_types)
6795 {
6796 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6797 &cu->header, section,
6798 abbrev_section, info_ptr,
6799 rcuh_kind::TYPE);
6800
6801 /* Since per_cu is the first member of struct signatured_type,
6802 we can go from a pointer to one to a pointer to the other. */
6803 sig_type = (struct signatured_type *) this_cu;
6804 gdb_assert (sig_type->signature == cu->header.signature);
6805 gdb_assert (sig_type->type_offset_in_tu
6806 == cu->header.type_cu_offset_in_tu);
6807 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6808
6809 /* LENGTH has not been set yet for type units if we're
6810 using .gdb_index. */
6811 this_cu->length = cu->header.get_length ();
6812
6813 /* Establish the type offset that can be used to lookup the type. */
6814 sig_type->type_offset_in_section =
6815 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6816
6817 this_cu->dwarf_version = cu->header.version;
6818 }
6819 else
6820 {
6821 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6822 &cu->header, section,
6823 abbrev_section,
6824 info_ptr,
6825 rcuh_kind::COMPILE);
6826
6827 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6828 gdb_assert (this_cu->length == cu->header.get_length ());
6829 this_cu->dwarf_version = cu->header.version;
6830 }
6831 }
6832
6833 /* Skip dummy compilation units. */
6834 if (info_ptr >= begin_info_ptr + this_cu->length
6835 || peek_abbrev_code (abfd, info_ptr) == 0)
6836 {
6837 dummy_p = true;
6838 return;
6839 }
6840
6841 /* If we don't have them yet, read the abbrevs for this compilation unit.
6842 And if we need to read them now, make sure they're freed when we're
6843 done. */
6844 if (abbrev_table != NULL)
6845 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6846 else
6847 {
6848 m_abbrev_table_holder
6849 = abbrev_table::read (objfile, abbrev_section,
6850 cu->header.abbrev_sect_off);
6851 abbrev_table = m_abbrev_table_holder.get ();
6852 }
6853
6854 /* Read the top level CU/TU die. */
6855 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6856 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6857
6858 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6859 {
6860 dummy_p = true;
6861 return;
6862 }
6863
6864 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6865 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6866 table from the DWO file and pass the ownership over to us. It will be
6867 referenced from READER, so we must make sure to free it after we're done
6868 with READER.
6869
6870 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6871 DWO CU, that this test will fail (the attribute will not be present). */
6872 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6873 if (dwo_name != nullptr)
6874 {
6875 struct dwo_unit *dwo_unit;
6876 struct die_info *dwo_comp_unit_die;
6877
6878 if (comp_unit_die->has_children)
6879 {
6880 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6881 " has children (offset %s) [in module %s]"),
6882 sect_offset_str (this_cu->sect_off),
6883 bfd_get_filename (abfd));
6884 }
6885 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6886 if (dwo_unit != NULL)
6887 {
6888 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6889 comp_unit_die, NULL,
6890 this, &info_ptr,
6891 &dwo_comp_unit_die,
6892 &m_dwo_abbrev_table) == 0)
6893 {
6894 /* Dummy die. */
6895 dummy_p = true;
6896 return;
6897 }
6898 comp_unit_die = dwo_comp_unit_die;
6899 }
6900 else
6901 {
6902 /* Yikes, we couldn't find the rest of the DIE, we only have
6903 the stub. A complaint has already been logged. There's
6904 not much more we can do except pass on the stub DIE to
6905 die_reader_func. We don't want to throw an error on bad
6906 debug info. */
6907 }
6908 }
6909 }
6910
6911 void
6912 cutu_reader::keep ()
6913 {
6914 /* Done, clean up. */
6915 gdb_assert (!dummy_p);
6916 if (m_new_cu != NULL)
6917 {
6918 struct dwarf2_per_objfile *dwarf2_per_objfile
6919 = m_this_cu->dwarf2_per_objfile;
6920 /* Link this CU into read_in_chain. */
6921 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6922 dwarf2_per_objfile->read_in_chain = m_this_cu;
6923 /* The chain owns it now. */
6924 m_new_cu.release ();
6925 }
6926 }
6927
6928 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6929 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6930 assumed to have already done the lookup to find the DWO file).
6931
6932 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6933 THIS_CU->is_debug_types, but nothing else.
6934
6935 We fill in THIS_CU->length.
6936
6937 THIS_CU->cu is always freed when done.
6938 This is done in order to not leave THIS_CU->cu in a state where we have
6939 to care whether it refers to the "main" CU or the DWO CU.
6940
6941 When parent_cu is passed, it is used to provide a default value for
6942 str_offsets_base and addr_base from the parent. */
6943
6944 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6945 struct dwarf2_cu *parent_cu,
6946 struct dwo_file *dwo_file)
6947 : die_reader_specs {},
6948 m_this_cu (this_cu)
6949 {
6950 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6951 struct objfile *objfile = dwarf2_per_objfile->objfile;
6952 struct dwarf2_section_info *section = this_cu->section;
6953 bfd *abfd = section->get_bfd_owner ();
6954 struct dwarf2_section_info *abbrev_section;
6955 const gdb_byte *begin_info_ptr, *info_ptr;
6956
6957 if (dwarf_die_debug)
6958 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6959 this_cu->is_debug_types ? "type" : "comp",
6960 sect_offset_str (this_cu->sect_off));
6961
6962 gdb_assert (this_cu->cu == NULL);
6963
6964 abbrev_section = (dwo_file != NULL
6965 ? &dwo_file->sections.abbrev
6966 : get_abbrev_section_for_cu (this_cu));
6967
6968 /* This is cheap if the section is already read in. */
6969 section->read (objfile);
6970
6971 m_new_cu.reset (new dwarf2_cu (this_cu));
6972
6973 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6974 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6975 &m_new_cu->header, section,
6976 abbrev_section, info_ptr,
6977 (this_cu->is_debug_types
6978 ? rcuh_kind::TYPE
6979 : rcuh_kind::COMPILE));
6980
6981 if (parent_cu != nullptr)
6982 {
6983 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6984 m_new_cu->addr_base = parent_cu->addr_base;
6985 }
6986 this_cu->length = m_new_cu->header.get_length ();
6987
6988 /* Skip dummy compilation units. */
6989 if (info_ptr >= begin_info_ptr + this_cu->length
6990 || peek_abbrev_code (abfd, info_ptr) == 0)
6991 {
6992 dummy_p = true;
6993 return;
6994 }
6995
6996 m_abbrev_table_holder
6997 = abbrev_table::read (objfile, abbrev_section,
6998 m_new_cu->header.abbrev_sect_off);
6999
7000 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7001 m_abbrev_table_holder.get ());
7002 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7003 }
7004
7005 \f
7006 /* Type Unit Groups.
7007
7008 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7009 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7010 so that all types coming from the same compilation (.o file) are grouped
7011 together. A future step could be to put the types in the same symtab as
7012 the CU the types ultimately came from. */
7013
7014 static hashval_t
7015 hash_type_unit_group (const void *item)
7016 {
7017 const struct type_unit_group *tu_group
7018 = (const struct type_unit_group *) item;
7019
7020 return hash_stmt_list_entry (&tu_group->hash);
7021 }
7022
7023 static int
7024 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7025 {
7026 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7027 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7028
7029 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7030 }
7031
7032 /* Allocate a hash table for type unit groups. */
7033
7034 static htab_up
7035 allocate_type_unit_groups_table ()
7036 {
7037 return htab_up (htab_create_alloc (3,
7038 hash_type_unit_group,
7039 eq_type_unit_group,
7040 NULL, xcalloc, xfree));
7041 }
7042
7043 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7044 partial symtabs. We combine several TUs per psymtab to not let the size
7045 of any one psymtab grow too big. */
7046 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7047 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7048
7049 /* Helper routine for get_type_unit_group.
7050 Create the type_unit_group object used to hold one or more TUs. */
7051
7052 static struct type_unit_group *
7053 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7054 {
7055 struct dwarf2_per_objfile *dwarf2_per_objfile
7056 = cu->per_cu->dwarf2_per_objfile;
7057 struct objfile *objfile = dwarf2_per_objfile->objfile;
7058 struct dwarf2_per_cu_data *per_cu;
7059 struct type_unit_group *tu_group;
7060
7061 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7062 struct type_unit_group);
7063 per_cu = &tu_group->per_cu;
7064 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7065
7066 if (dwarf2_per_objfile->using_index)
7067 {
7068 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7069 struct dwarf2_per_cu_quick_data);
7070 }
7071 else
7072 {
7073 unsigned int line_offset = to_underlying (line_offset_struct);
7074 dwarf2_psymtab *pst;
7075 std::string name;
7076
7077 /* Give the symtab a useful name for debug purposes. */
7078 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7079 name = string_printf ("<type_units_%d>",
7080 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7081 else
7082 name = string_printf ("<type_units_at_0x%x>", line_offset);
7083
7084 pst = create_partial_symtab (per_cu, name.c_str ());
7085 pst->anonymous = true;
7086 }
7087
7088 tu_group->hash.dwo_unit = cu->dwo_unit;
7089 tu_group->hash.line_sect_off = line_offset_struct;
7090
7091 return tu_group;
7092 }
7093
7094 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7095 STMT_LIST is a DW_AT_stmt_list attribute. */
7096
7097 static struct type_unit_group *
7098 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7099 {
7100 struct dwarf2_per_objfile *dwarf2_per_objfile
7101 = cu->per_cu->dwarf2_per_objfile;
7102 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7103 struct type_unit_group *tu_group;
7104 void **slot;
7105 unsigned int line_offset;
7106 struct type_unit_group type_unit_group_for_lookup;
7107
7108 if (dwarf2_per_objfile->type_unit_groups == NULL)
7109 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7110
7111 /* Do we need to create a new group, or can we use an existing one? */
7112
7113 if (stmt_list)
7114 {
7115 line_offset = DW_UNSND (stmt_list);
7116 ++tu_stats->nr_symtab_sharers;
7117 }
7118 else
7119 {
7120 /* Ugh, no stmt_list. Rare, but we have to handle it.
7121 We can do various things here like create one group per TU or
7122 spread them over multiple groups to split up the expansion work.
7123 To avoid worst case scenarios (too many groups or too large groups)
7124 we, umm, group them in bunches. */
7125 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7126 | (tu_stats->nr_stmt_less_type_units
7127 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7128 ++tu_stats->nr_stmt_less_type_units;
7129 }
7130
7131 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7132 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7133 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7134 &type_unit_group_for_lookup, INSERT);
7135 if (*slot != NULL)
7136 {
7137 tu_group = (struct type_unit_group *) *slot;
7138 gdb_assert (tu_group != NULL);
7139 }
7140 else
7141 {
7142 sect_offset line_offset_struct = (sect_offset) line_offset;
7143 tu_group = create_type_unit_group (cu, line_offset_struct);
7144 *slot = tu_group;
7145 ++tu_stats->nr_symtabs;
7146 }
7147
7148 return tu_group;
7149 }
7150 \f
7151 /* Partial symbol tables. */
7152
7153 /* Create a psymtab named NAME and assign it to PER_CU.
7154
7155 The caller must fill in the following details:
7156 dirname, textlow, texthigh. */
7157
7158 static dwarf2_psymtab *
7159 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7160 {
7161 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7162 dwarf2_psymtab *pst;
7163
7164 pst = new dwarf2_psymtab (name, objfile, per_cu);
7165
7166 pst->psymtabs_addrmap_supported = true;
7167
7168 /* This is the glue that links PST into GDB's symbol API. */
7169 per_cu->v.psymtab = pst;
7170
7171 return pst;
7172 }
7173
7174 /* DIE reader function for process_psymtab_comp_unit. */
7175
7176 static void
7177 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7178 const gdb_byte *info_ptr,
7179 struct die_info *comp_unit_die,
7180 enum language pretend_language)
7181 {
7182 struct dwarf2_cu *cu = reader->cu;
7183 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7185 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7186 CORE_ADDR baseaddr;
7187 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7188 dwarf2_psymtab *pst;
7189 enum pc_bounds_kind cu_bounds_kind;
7190 const char *filename;
7191
7192 gdb_assert (! per_cu->is_debug_types);
7193
7194 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7195
7196 /* Allocate a new partial symbol table structure. */
7197 gdb::unique_xmalloc_ptr<char> debug_filename;
7198 static const char artificial[] = "<artificial>";
7199 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7200 if (filename == NULL)
7201 filename = "";
7202 else if (strcmp (filename, artificial) == 0)
7203 {
7204 debug_filename.reset (concat (artificial, "@",
7205 sect_offset_str (per_cu->sect_off),
7206 (char *) NULL));
7207 filename = debug_filename.get ();
7208 }
7209
7210 pst = create_partial_symtab (per_cu, filename);
7211
7212 /* This must be done before calling dwarf2_build_include_psymtabs. */
7213 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7214
7215 baseaddr = objfile->text_section_offset ();
7216
7217 dwarf2_find_base_address (comp_unit_die, cu);
7218
7219 /* Possibly set the default values of LOWPC and HIGHPC from
7220 `DW_AT_ranges'. */
7221 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7222 &best_highpc, cu, pst);
7223 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7224 {
7225 CORE_ADDR low
7226 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7227 - baseaddr);
7228 CORE_ADDR high
7229 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7230 - baseaddr - 1);
7231 /* Store the contiguous range if it is not empty; it can be
7232 empty for CUs with no code. */
7233 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7234 low, high, pst);
7235 }
7236
7237 /* Check if comp unit has_children.
7238 If so, read the rest of the partial symbols from this comp unit.
7239 If not, there's no more debug_info for this comp unit. */
7240 if (comp_unit_die->has_children)
7241 {
7242 struct partial_die_info *first_die;
7243 CORE_ADDR lowpc, highpc;
7244
7245 lowpc = ((CORE_ADDR) -1);
7246 highpc = ((CORE_ADDR) 0);
7247
7248 first_die = load_partial_dies (reader, info_ptr, 1);
7249
7250 scan_partial_symbols (first_die, &lowpc, &highpc,
7251 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7252
7253 /* If we didn't find a lowpc, set it to highpc to avoid
7254 complaints from `maint check'. */
7255 if (lowpc == ((CORE_ADDR) -1))
7256 lowpc = highpc;
7257
7258 /* If the compilation unit didn't have an explicit address range,
7259 then use the information extracted from its child dies. */
7260 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7261 {
7262 best_lowpc = lowpc;
7263 best_highpc = highpc;
7264 }
7265 }
7266 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7267 best_lowpc + baseaddr)
7268 - baseaddr);
7269 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7270 best_highpc + baseaddr)
7271 - baseaddr);
7272
7273 end_psymtab_common (objfile, pst);
7274
7275 if (!cu->per_cu->imported_symtabs_empty ())
7276 {
7277 int i;
7278 int len = cu->per_cu->imported_symtabs_size ();
7279
7280 /* Fill in 'dependencies' here; we fill in 'users' in a
7281 post-pass. */
7282 pst->number_of_dependencies = len;
7283 pst->dependencies
7284 = objfile->partial_symtabs->allocate_dependencies (len);
7285 for (i = 0; i < len; ++i)
7286 {
7287 pst->dependencies[i]
7288 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7289 }
7290
7291 cu->per_cu->imported_symtabs_free ();
7292 }
7293
7294 /* Get the list of files included in the current compilation unit,
7295 and build a psymtab for each of them. */
7296 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7297
7298 if (dwarf_read_debug)
7299 fprintf_unfiltered (gdb_stdlog,
7300 "Psymtab for %s unit @%s: %s - %s"
7301 ", %d global, %d static syms\n",
7302 per_cu->is_debug_types ? "type" : "comp",
7303 sect_offset_str (per_cu->sect_off),
7304 paddress (gdbarch, pst->text_low (objfile)),
7305 paddress (gdbarch, pst->text_high (objfile)),
7306 pst->n_global_syms, pst->n_static_syms);
7307 }
7308
7309 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7310 Process compilation unit THIS_CU for a psymtab. */
7311
7312 static void
7313 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7314 bool want_partial_unit,
7315 enum language pretend_language)
7316 {
7317 /* If this compilation unit was already read in, free the
7318 cached copy in order to read it in again. This is
7319 necessary because we skipped some symbols when we first
7320 read in the compilation unit (see load_partial_dies).
7321 This problem could be avoided, but the benefit is unclear. */
7322 if (this_cu->cu != NULL)
7323 free_one_cached_comp_unit (this_cu);
7324
7325 cutu_reader reader (this_cu, NULL, 0, false);
7326
7327 switch (reader.comp_unit_die->tag)
7328 {
7329 case DW_TAG_compile_unit:
7330 this_cu->unit_type = DW_UT_compile;
7331 break;
7332 case DW_TAG_partial_unit:
7333 this_cu->unit_type = DW_UT_partial;
7334 break;
7335 default:
7336 abort ();
7337 }
7338
7339 if (reader.dummy_p)
7340 {
7341 /* Nothing. */
7342 }
7343 else if (this_cu->is_debug_types)
7344 build_type_psymtabs_reader (&reader, reader.info_ptr,
7345 reader.comp_unit_die);
7346 else if (want_partial_unit
7347 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7348 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7349 reader.comp_unit_die,
7350 pretend_language);
7351
7352 this_cu->lang = this_cu->cu->language;
7353
7354 /* Age out any secondary CUs. */
7355 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7356 }
7357
7358 /* Reader function for build_type_psymtabs. */
7359
7360 static void
7361 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7362 const gdb_byte *info_ptr,
7363 struct die_info *type_unit_die)
7364 {
7365 struct dwarf2_per_objfile *dwarf2_per_objfile
7366 = reader->cu->per_cu->dwarf2_per_objfile;
7367 struct objfile *objfile = dwarf2_per_objfile->objfile;
7368 struct dwarf2_cu *cu = reader->cu;
7369 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7370 struct signatured_type *sig_type;
7371 struct type_unit_group *tu_group;
7372 struct attribute *attr;
7373 struct partial_die_info *first_die;
7374 CORE_ADDR lowpc, highpc;
7375 dwarf2_psymtab *pst;
7376
7377 gdb_assert (per_cu->is_debug_types);
7378 sig_type = (struct signatured_type *) per_cu;
7379
7380 if (! type_unit_die->has_children)
7381 return;
7382
7383 attr = type_unit_die->attr (DW_AT_stmt_list);
7384 tu_group = get_type_unit_group (cu, attr);
7385
7386 if (tu_group->tus == nullptr)
7387 tu_group->tus = new std::vector<signatured_type *>;
7388 tu_group->tus->push_back (sig_type);
7389
7390 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7391 pst = create_partial_symtab (per_cu, "");
7392 pst->anonymous = true;
7393
7394 first_die = load_partial_dies (reader, info_ptr, 1);
7395
7396 lowpc = (CORE_ADDR) -1;
7397 highpc = (CORE_ADDR) 0;
7398 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7399
7400 end_psymtab_common (objfile, pst);
7401 }
7402
7403 /* Struct used to sort TUs by their abbreviation table offset. */
7404
7405 struct tu_abbrev_offset
7406 {
7407 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7408 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7409 {}
7410
7411 signatured_type *sig_type;
7412 sect_offset abbrev_offset;
7413 };
7414
7415 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7416
7417 static bool
7418 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7419 const struct tu_abbrev_offset &b)
7420 {
7421 return a.abbrev_offset < b.abbrev_offset;
7422 }
7423
7424 /* Efficiently read all the type units.
7425 This does the bulk of the work for build_type_psymtabs.
7426
7427 The efficiency is because we sort TUs by the abbrev table they use and
7428 only read each abbrev table once. In one program there are 200K TUs
7429 sharing 8K abbrev tables.
7430
7431 The main purpose of this function is to support building the
7432 dwarf2_per_objfile->type_unit_groups table.
7433 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7434 can collapse the search space by grouping them by stmt_list.
7435 The savings can be significant, in the same program from above the 200K TUs
7436 share 8K stmt_list tables.
7437
7438 FUNC is expected to call get_type_unit_group, which will create the
7439 struct type_unit_group if necessary and add it to
7440 dwarf2_per_objfile->type_unit_groups. */
7441
7442 static void
7443 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7444 {
7445 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7446 abbrev_table_up abbrev_table;
7447 sect_offset abbrev_offset;
7448
7449 /* It's up to the caller to not call us multiple times. */
7450 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7451
7452 if (dwarf2_per_objfile->all_type_units.empty ())
7453 return;
7454
7455 /* TUs typically share abbrev tables, and there can be way more TUs than
7456 abbrev tables. Sort by abbrev table to reduce the number of times we
7457 read each abbrev table in.
7458 Alternatives are to punt or to maintain a cache of abbrev tables.
7459 This is simpler and efficient enough for now.
7460
7461 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7462 symtab to use). Typically TUs with the same abbrev offset have the same
7463 stmt_list value too so in practice this should work well.
7464
7465 The basic algorithm here is:
7466
7467 sort TUs by abbrev table
7468 for each TU with same abbrev table:
7469 read abbrev table if first user
7470 read TU top level DIE
7471 [IWBN if DWO skeletons had DW_AT_stmt_list]
7472 call FUNC */
7473
7474 if (dwarf_read_debug)
7475 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7476
7477 /* Sort in a separate table to maintain the order of all_type_units
7478 for .gdb_index: TU indices directly index all_type_units. */
7479 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7480 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7481
7482 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7483 sorted_by_abbrev.emplace_back
7484 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7485 sig_type->per_cu.section,
7486 sig_type->per_cu.sect_off));
7487
7488 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7489 sort_tu_by_abbrev_offset);
7490
7491 abbrev_offset = (sect_offset) ~(unsigned) 0;
7492
7493 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7494 {
7495 /* Switch to the next abbrev table if necessary. */
7496 if (abbrev_table == NULL
7497 || tu.abbrev_offset != abbrev_offset)
7498 {
7499 abbrev_offset = tu.abbrev_offset;
7500 abbrev_table =
7501 abbrev_table::read (dwarf2_per_objfile->objfile,
7502 &dwarf2_per_objfile->abbrev,
7503 abbrev_offset);
7504 ++tu_stats->nr_uniq_abbrev_tables;
7505 }
7506
7507 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7508 0, false);
7509 if (!reader.dummy_p)
7510 build_type_psymtabs_reader (&reader, reader.info_ptr,
7511 reader.comp_unit_die);
7512 }
7513 }
7514
7515 /* Print collected type unit statistics. */
7516
7517 static void
7518 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7519 {
7520 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7521
7522 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7523 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7524 dwarf2_per_objfile->all_type_units.size ());
7525 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7526 tu_stats->nr_uniq_abbrev_tables);
7527 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7528 tu_stats->nr_symtabs);
7529 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7530 tu_stats->nr_symtab_sharers);
7531 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7532 tu_stats->nr_stmt_less_type_units);
7533 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7534 tu_stats->nr_all_type_units_reallocs);
7535 }
7536
7537 /* Traversal function for build_type_psymtabs. */
7538
7539 static int
7540 build_type_psymtab_dependencies (void **slot, void *info)
7541 {
7542 struct dwarf2_per_objfile *dwarf2_per_objfile
7543 = (struct dwarf2_per_objfile *) info;
7544 struct objfile *objfile = dwarf2_per_objfile->objfile;
7545 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7546 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7547 dwarf2_psymtab *pst = per_cu->v.psymtab;
7548 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7549 int i;
7550
7551 gdb_assert (len > 0);
7552 gdb_assert (per_cu->type_unit_group_p ());
7553
7554 pst->number_of_dependencies = len;
7555 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7556 for (i = 0; i < len; ++i)
7557 {
7558 struct signatured_type *iter = tu_group->tus->at (i);
7559 gdb_assert (iter->per_cu.is_debug_types);
7560 pst->dependencies[i] = iter->per_cu.v.psymtab;
7561 iter->type_unit_group = tu_group;
7562 }
7563
7564 delete tu_group->tus;
7565 tu_group->tus = nullptr;
7566
7567 return 1;
7568 }
7569
7570 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7571 Build partial symbol tables for the .debug_types comp-units. */
7572
7573 static void
7574 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7575 {
7576 if (! create_all_type_units (dwarf2_per_objfile))
7577 return;
7578
7579 build_type_psymtabs_1 (dwarf2_per_objfile);
7580 }
7581
7582 /* Traversal function for process_skeletonless_type_unit.
7583 Read a TU in a DWO file and build partial symbols for it. */
7584
7585 static int
7586 process_skeletonless_type_unit (void **slot, void *info)
7587 {
7588 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7589 struct dwarf2_per_objfile *dwarf2_per_objfile
7590 = (struct dwarf2_per_objfile *) info;
7591 struct signatured_type find_entry, *entry;
7592
7593 /* If this TU doesn't exist in the global table, add it and read it in. */
7594
7595 if (dwarf2_per_objfile->signatured_types == NULL)
7596 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7597
7598 find_entry.signature = dwo_unit->signature;
7599 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7600 &find_entry, INSERT);
7601 /* If we've already seen this type there's nothing to do. What's happening
7602 is we're doing our own version of comdat-folding here. */
7603 if (*slot != NULL)
7604 return 1;
7605
7606 /* This does the job that create_all_type_units would have done for
7607 this TU. */
7608 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7609 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7610 *slot = entry;
7611
7612 /* This does the job that build_type_psymtabs_1 would have done. */
7613 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7614 if (!reader.dummy_p)
7615 build_type_psymtabs_reader (&reader, reader.info_ptr,
7616 reader.comp_unit_die);
7617
7618 return 1;
7619 }
7620
7621 /* Traversal function for process_skeletonless_type_units. */
7622
7623 static int
7624 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7625 {
7626 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7627
7628 if (dwo_file->tus != NULL)
7629 htab_traverse_noresize (dwo_file->tus.get (),
7630 process_skeletonless_type_unit, info);
7631
7632 return 1;
7633 }
7634
7635 /* Scan all TUs of DWO files, verifying we've processed them.
7636 This is needed in case a TU was emitted without its skeleton.
7637 Note: This can't be done until we know what all the DWO files are. */
7638
7639 static void
7640 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7641 {
7642 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7643 if (get_dwp_file (dwarf2_per_objfile) == NULL
7644 && dwarf2_per_objfile->dwo_files != NULL)
7645 {
7646 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7647 process_dwo_file_for_skeletonless_type_units,
7648 dwarf2_per_objfile);
7649 }
7650 }
7651
7652 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7653
7654 static void
7655 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7656 {
7657 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7658 {
7659 dwarf2_psymtab *pst = per_cu->v.psymtab;
7660
7661 if (pst == NULL)
7662 continue;
7663
7664 for (int j = 0; j < pst->number_of_dependencies; ++j)
7665 {
7666 /* Set the 'user' field only if it is not already set. */
7667 if (pst->dependencies[j]->user == NULL)
7668 pst->dependencies[j]->user = pst;
7669 }
7670 }
7671 }
7672
7673 /* Build the partial symbol table by doing a quick pass through the
7674 .debug_info and .debug_abbrev sections. */
7675
7676 static void
7677 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7678 {
7679 struct objfile *objfile = dwarf2_per_objfile->objfile;
7680
7681 if (dwarf_read_debug)
7682 {
7683 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7684 objfile_name (objfile));
7685 }
7686
7687 scoped_restore restore_reading_psyms
7688 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7689 true);
7690
7691 dwarf2_per_objfile->info.read (objfile);
7692
7693 /* Any cached compilation units will be linked by the per-objfile
7694 read_in_chain. Make sure to free them when we're done. */
7695 free_cached_comp_units freer (dwarf2_per_objfile);
7696
7697 build_type_psymtabs (dwarf2_per_objfile);
7698
7699 create_all_comp_units (dwarf2_per_objfile);
7700
7701 /* Create a temporary address map on a temporary obstack. We later
7702 copy this to the final obstack. */
7703 auto_obstack temp_obstack;
7704
7705 scoped_restore save_psymtabs_addrmap
7706 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7707 addrmap_create_mutable (&temp_obstack));
7708
7709 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7710 process_psymtab_comp_unit (per_cu, false, language_minimal);
7711
7712 /* This has to wait until we read the CUs, we need the list of DWOs. */
7713 process_skeletonless_type_units (dwarf2_per_objfile);
7714
7715 /* Now that all TUs have been processed we can fill in the dependencies. */
7716 if (dwarf2_per_objfile->type_unit_groups != NULL)
7717 {
7718 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7719 build_type_psymtab_dependencies, dwarf2_per_objfile);
7720 }
7721
7722 if (dwarf_read_debug)
7723 print_tu_stats (dwarf2_per_objfile);
7724
7725 set_partial_user (dwarf2_per_objfile);
7726
7727 objfile->partial_symtabs->psymtabs_addrmap
7728 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7729 objfile->partial_symtabs->obstack ());
7730 /* At this point we want to keep the address map. */
7731 save_psymtabs_addrmap.release ();
7732
7733 if (dwarf_read_debug)
7734 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7735 objfile_name (objfile));
7736 }
7737
7738 /* Load the partial DIEs for a secondary CU into memory.
7739 This is also used when rereading a primary CU with load_all_dies. */
7740
7741 static void
7742 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7743 {
7744 cutu_reader reader (this_cu, NULL, 1, false);
7745
7746 if (!reader.dummy_p)
7747 {
7748 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7749 language_minimal);
7750
7751 /* Check if comp unit has_children.
7752 If so, read the rest of the partial symbols from this comp unit.
7753 If not, there's no more debug_info for this comp unit. */
7754 if (reader.comp_unit_die->has_children)
7755 load_partial_dies (&reader, reader.info_ptr, 0);
7756
7757 reader.keep ();
7758 }
7759 }
7760
7761 static void
7762 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7763 struct dwarf2_section_info *section,
7764 struct dwarf2_section_info *abbrev_section,
7765 unsigned int is_dwz)
7766 {
7767 const gdb_byte *info_ptr;
7768 struct objfile *objfile = dwarf2_per_objfile->objfile;
7769
7770 if (dwarf_read_debug)
7771 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7772 section->get_name (),
7773 section->get_file_name ());
7774
7775 section->read (objfile);
7776
7777 info_ptr = section->buffer;
7778
7779 while (info_ptr < section->buffer + section->size)
7780 {
7781 struct dwarf2_per_cu_data *this_cu;
7782
7783 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7784
7785 comp_unit_head cu_header;
7786 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7787 abbrev_section, info_ptr,
7788 rcuh_kind::COMPILE);
7789
7790 /* Save the compilation unit for later lookup. */
7791 if (cu_header.unit_type != DW_UT_type)
7792 {
7793 this_cu = XOBNEW (&objfile->objfile_obstack,
7794 struct dwarf2_per_cu_data);
7795 memset (this_cu, 0, sizeof (*this_cu));
7796 }
7797 else
7798 {
7799 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7800 struct signatured_type);
7801 memset (sig_type, 0, sizeof (*sig_type));
7802 sig_type->signature = cu_header.signature;
7803 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7804 this_cu = &sig_type->per_cu;
7805 }
7806 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7807 this_cu->sect_off = sect_off;
7808 this_cu->length = cu_header.length + cu_header.initial_length_size;
7809 this_cu->is_dwz = is_dwz;
7810 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7811 this_cu->section = section;
7812
7813 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7814
7815 info_ptr = info_ptr + this_cu->length;
7816 }
7817 }
7818
7819 /* Create a list of all compilation units in OBJFILE.
7820 This is only done for -readnow and building partial symtabs. */
7821
7822 static void
7823 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7824 {
7825 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7826 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7827 &dwarf2_per_objfile->abbrev, 0);
7828
7829 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7830 if (dwz != NULL)
7831 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7832 1);
7833 }
7834
7835 /* Process all loaded DIEs for compilation unit CU, starting at
7836 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7837 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7838 DW_AT_ranges). See the comments of add_partial_subprogram on how
7839 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7840
7841 static void
7842 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7843 CORE_ADDR *highpc, int set_addrmap,
7844 struct dwarf2_cu *cu)
7845 {
7846 struct partial_die_info *pdi;
7847
7848 /* Now, march along the PDI's, descending into ones which have
7849 interesting children but skipping the children of the other ones,
7850 until we reach the end of the compilation unit. */
7851
7852 pdi = first_die;
7853
7854 while (pdi != NULL)
7855 {
7856 pdi->fixup (cu);
7857
7858 /* Anonymous namespaces or modules have no name but have interesting
7859 children, so we need to look at them. Ditto for anonymous
7860 enums. */
7861
7862 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7863 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7864 || pdi->tag == DW_TAG_imported_unit
7865 || pdi->tag == DW_TAG_inlined_subroutine)
7866 {
7867 switch (pdi->tag)
7868 {
7869 case DW_TAG_subprogram:
7870 case DW_TAG_inlined_subroutine:
7871 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7872 break;
7873 case DW_TAG_constant:
7874 case DW_TAG_variable:
7875 case DW_TAG_typedef:
7876 case DW_TAG_union_type:
7877 if (!pdi->is_declaration)
7878 {
7879 add_partial_symbol (pdi, cu);
7880 }
7881 break;
7882 case DW_TAG_class_type:
7883 case DW_TAG_interface_type:
7884 case DW_TAG_structure_type:
7885 if (!pdi->is_declaration)
7886 {
7887 add_partial_symbol (pdi, cu);
7888 }
7889 if ((cu->language == language_rust
7890 || cu->language == language_cplus) && pdi->has_children)
7891 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7892 set_addrmap, cu);
7893 break;
7894 case DW_TAG_enumeration_type:
7895 if (!pdi->is_declaration)
7896 add_partial_enumeration (pdi, cu);
7897 break;
7898 case DW_TAG_base_type:
7899 case DW_TAG_subrange_type:
7900 /* File scope base type definitions are added to the partial
7901 symbol table. */
7902 add_partial_symbol (pdi, cu);
7903 break;
7904 case DW_TAG_namespace:
7905 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7906 break;
7907 case DW_TAG_module:
7908 if (!pdi->is_declaration)
7909 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7910 break;
7911 case DW_TAG_imported_unit:
7912 {
7913 struct dwarf2_per_cu_data *per_cu;
7914
7915 /* For now we don't handle imported units in type units. */
7916 if (cu->per_cu->is_debug_types)
7917 {
7918 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7919 " supported in type units [in module %s]"),
7920 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7921 }
7922
7923 per_cu = dwarf2_find_containing_comp_unit
7924 (pdi->d.sect_off, pdi->is_dwz,
7925 cu->per_cu->dwarf2_per_objfile);
7926
7927 /* Go read the partial unit, if needed. */
7928 if (per_cu->v.psymtab == NULL)
7929 process_psymtab_comp_unit (per_cu, true, cu->language);
7930
7931 cu->per_cu->imported_symtabs_push (per_cu);
7932 }
7933 break;
7934 case DW_TAG_imported_declaration:
7935 add_partial_symbol (pdi, cu);
7936 break;
7937 default:
7938 break;
7939 }
7940 }
7941
7942 /* If the die has a sibling, skip to the sibling. */
7943
7944 pdi = pdi->die_sibling;
7945 }
7946 }
7947
7948 /* Functions used to compute the fully scoped name of a partial DIE.
7949
7950 Normally, this is simple. For C++, the parent DIE's fully scoped
7951 name is concatenated with "::" and the partial DIE's name.
7952 Enumerators are an exception; they use the scope of their parent
7953 enumeration type, i.e. the name of the enumeration type is not
7954 prepended to the enumerator.
7955
7956 There are two complexities. One is DW_AT_specification; in this
7957 case "parent" means the parent of the target of the specification,
7958 instead of the direct parent of the DIE. The other is compilers
7959 which do not emit DW_TAG_namespace; in this case we try to guess
7960 the fully qualified name of structure types from their members'
7961 linkage names. This must be done using the DIE's children rather
7962 than the children of any DW_AT_specification target. We only need
7963 to do this for structures at the top level, i.e. if the target of
7964 any DW_AT_specification (if any; otherwise the DIE itself) does not
7965 have a parent. */
7966
7967 /* Compute the scope prefix associated with PDI's parent, in
7968 compilation unit CU. The result will be allocated on CU's
7969 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7970 field. NULL is returned if no prefix is necessary. */
7971 static const char *
7972 partial_die_parent_scope (struct partial_die_info *pdi,
7973 struct dwarf2_cu *cu)
7974 {
7975 const char *grandparent_scope;
7976 struct partial_die_info *parent, *real_pdi;
7977
7978 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7979 then this means the parent of the specification DIE. */
7980
7981 real_pdi = pdi;
7982 while (real_pdi->has_specification)
7983 {
7984 auto res = find_partial_die (real_pdi->spec_offset,
7985 real_pdi->spec_is_dwz, cu);
7986 real_pdi = res.pdi;
7987 cu = res.cu;
7988 }
7989
7990 parent = real_pdi->die_parent;
7991 if (parent == NULL)
7992 return NULL;
7993
7994 if (parent->scope_set)
7995 return parent->scope;
7996
7997 parent->fixup (cu);
7998
7999 grandparent_scope = partial_die_parent_scope (parent, cu);
8000
8001 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8002 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8003 Work around this problem here. */
8004 if (cu->language == language_cplus
8005 && parent->tag == DW_TAG_namespace
8006 && strcmp (parent->name, "::") == 0
8007 && grandparent_scope == NULL)
8008 {
8009 parent->scope = NULL;
8010 parent->scope_set = 1;
8011 return NULL;
8012 }
8013
8014 /* Nested subroutines in Fortran get a prefix. */
8015 if (pdi->tag == DW_TAG_enumerator)
8016 /* Enumerators should not get the name of the enumeration as a prefix. */
8017 parent->scope = grandparent_scope;
8018 else if (parent->tag == DW_TAG_namespace
8019 || parent->tag == DW_TAG_module
8020 || parent->tag == DW_TAG_structure_type
8021 || parent->tag == DW_TAG_class_type
8022 || parent->tag == DW_TAG_interface_type
8023 || parent->tag == DW_TAG_union_type
8024 || parent->tag == DW_TAG_enumeration_type
8025 || (cu->language == language_fortran
8026 && parent->tag == DW_TAG_subprogram
8027 && pdi->tag == DW_TAG_subprogram))
8028 {
8029 if (grandparent_scope == NULL)
8030 parent->scope = parent->name;
8031 else
8032 parent->scope = typename_concat (&cu->comp_unit_obstack,
8033 grandparent_scope,
8034 parent->name, 0, cu);
8035 }
8036 else
8037 {
8038 /* FIXME drow/2004-04-01: What should we be doing with
8039 function-local names? For partial symbols, we should probably be
8040 ignoring them. */
8041 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8042 dwarf_tag_name (parent->tag),
8043 sect_offset_str (pdi->sect_off));
8044 parent->scope = grandparent_scope;
8045 }
8046
8047 parent->scope_set = 1;
8048 return parent->scope;
8049 }
8050
8051 /* Return the fully scoped name associated with PDI, from compilation unit
8052 CU. The result will be allocated with malloc. */
8053
8054 static gdb::unique_xmalloc_ptr<char>
8055 partial_die_full_name (struct partial_die_info *pdi,
8056 struct dwarf2_cu *cu)
8057 {
8058 const char *parent_scope;
8059
8060 /* If this is a template instantiation, we can not work out the
8061 template arguments from partial DIEs. So, unfortunately, we have
8062 to go through the full DIEs. At least any work we do building
8063 types here will be reused if full symbols are loaded later. */
8064 if (pdi->has_template_arguments)
8065 {
8066 pdi->fixup (cu);
8067
8068 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8069 {
8070 struct die_info *die;
8071 struct attribute attr;
8072 struct dwarf2_cu *ref_cu = cu;
8073
8074 /* DW_FORM_ref_addr is using section offset. */
8075 attr.name = (enum dwarf_attribute) 0;
8076 attr.form = DW_FORM_ref_addr;
8077 attr.u.unsnd = to_underlying (pdi->sect_off);
8078 die = follow_die_ref (NULL, &attr, &ref_cu);
8079
8080 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8081 }
8082 }
8083
8084 parent_scope = partial_die_parent_scope (pdi, cu);
8085 if (parent_scope == NULL)
8086 return NULL;
8087 else
8088 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8089 pdi->name, 0, cu));
8090 }
8091
8092 static void
8093 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8094 {
8095 struct dwarf2_per_objfile *dwarf2_per_objfile
8096 = cu->per_cu->dwarf2_per_objfile;
8097 struct objfile *objfile = dwarf2_per_objfile->objfile;
8098 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8099 CORE_ADDR addr = 0;
8100 const char *actual_name = NULL;
8101 CORE_ADDR baseaddr;
8102
8103 baseaddr = objfile->text_section_offset ();
8104
8105 gdb::unique_xmalloc_ptr<char> built_actual_name
8106 = partial_die_full_name (pdi, cu);
8107 if (built_actual_name != NULL)
8108 actual_name = built_actual_name.get ();
8109
8110 if (actual_name == NULL)
8111 actual_name = pdi->name;
8112
8113 switch (pdi->tag)
8114 {
8115 case DW_TAG_inlined_subroutine:
8116 case DW_TAG_subprogram:
8117 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8118 - baseaddr);
8119 if (pdi->is_external
8120 || cu->language == language_ada
8121 || (cu->language == language_fortran
8122 && pdi->die_parent != NULL
8123 && pdi->die_parent->tag == DW_TAG_subprogram))
8124 {
8125 /* Normally, only "external" DIEs are part of the global scope.
8126 But in Ada and Fortran, we want to be able to access nested
8127 procedures globally. So all Ada and Fortran subprograms are
8128 stored in the global scope. */
8129 add_psymbol_to_list (actual_name,
8130 built_actual_name != NULL,
8131 VAR_DOMAIN, LOC_BLOCK,
8132 SECT_OFF_TEXT (objfile),
8133 psymbol_placement::GLOBAL,
8134 addr,
8135 cu->language, objfile);
8136 }
8137 else
8138 {
8139 add_psymbol_to_list (actual_name,
8140 built_actual_name != NULL,
8141 VAR_DOMAIN, LOC_BLOCK,
8142 SECT_OFF_TEXT (objfile),
8143 psymbol_placement::STATIC,
8144 addr, cu->language, objfile);
8145 }
8146
8147 if (pdi->main_subprogram && actual_name != NULL)
8148 set_objfile_main_name (objfile, actual_name, cu->language);
8149 break;
8150 case DW_TAG_constant:
8151 add_psymbol_to_list (actual_name,
8152 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8153 -1, (pdi->is_external
8154 ? psymbol_placement::GLOBAL
8155 : psymbol_placement::STATIC),
8156 0, cu->language, objfile);
8157 break;
8158 case DW_TAG_variable:
8159 if (pdi->d.locdesc)
8160 addr = decode_locdesc (pdi->d.locdesc, cu);
8161
8162 if (pdi->d.locdesc
8163 && addr == 0
8164 && !dwarf2_per_objfile->has_section_at_zero)
8165 {
8166 /* A global or static variable may also have been stripped
8167 out by the linker if unused, in which case its address
8168 will be nullified; do not add such variables into partial
8169 symbol table then. */
8170 }
8171 else if (pdi->is_external)
8172 {
8173 /* Global Variable.
8174 Don't enter into the minimal symbol tables as there is
8175 a minimal symbol table entry from the ELF symbols already.
8176 Enter into partial symbol table if it has a location
8177 descriptor or a type.
8178 If the location descriptor is missing, new_symbol will create
8179 a LOC_UNRESOLVED symbol, the address of the variable will then
8180 be determined from the minimal symbol table whenever the variable
8181 is referenced.
8182 The address for the partial symbol table entry is not
8183 used by GDB, but it comes in handy for debugging partial symbol
8184 table building. */
8185
8186 if (pdi->d.locdesc || pdi->has_type)
8187 add_psymbol_to_list (actual_name,
8188 built_actual_name != NULL,
8189 VAR_DOMAIN, LOC_STATIC,
8190 SECT_OFF_TEXT (objfile),
8191 psymbol_placement::GLOBAL,
8192 addr, cu->language, objfile);
8193 }
8194 else
8195 {
8196 int has_loc = pdi->d.locdesc != NULL;
8197
8198 /* Static Variable. Skip symbols whose value we cannot know (those
8199 without location descriptors or constant values). */
8200 if (!has_loc && !pdi->has_const_value)
8201 return;
8202
8203 add_psymbol_to_list (actual_name,
8204 built_actual_name != NULL,
8205 VAR_DOMAIN, LOC_STATIC,
8206 SECT_OFF_TEXT (objfile),
8207 psymbol_placement::STATIC,
8208 has_loc ? addr : 0,
8209 cu->language, objfile);
8210 }
8211 break;
8212 case DW_TAG_typedef:
8213 case DW_TAG_base_type:
8214 case DW_TAG_subrange_type:
8215 add_psymbol_to_list (actual_name,
8216 built_actual_name != NULL,
8217 VAR_DOMAIN, LOC_TYPEDEF, -1,
8218 psymbol_placement::STATIC,
8219 0, cu->language, objfile);
8220 break;
8221 case DW_TAG_imported_declaration:
8222 case DW_TAG_namespace:
8223 add_psymbol_to_list (actual_name,
8224 built_actual_name != NULL,
8225 VAR_DOMAIN, LOC_TYPEDEF, -1,
8226 psymbol_placement::GLOBAL,
8227 0, cu->language, objfile);
8228 break;
8229 case DW_TAG_module:
8230 /* With Fortran 77 there might be a "BLOCK DATA" module
8231 available without any name. If so, we skip the module as it
8232 doesn't bring any value. */
8233 if (actual_name != nullptr)
8234 add_psymbol_to_list (actual_name,
8235 built_actual_name != NULL,
8236 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8237 psymbol_placement::GLOBAL,
8238 0, cu->language, objfile);
8239 break;
8240 case DW_TAG_class_type:
8241 case DW_TAG_interface_type:
8242 case DW_TAG_structure_type:
8243 case DW_TAG_union_type:
8244 case DW_TAG_enumeration_type:
8245 /* Skip external references. The DWARF standard says in the section
8246 about "Structure, Union, and Class Type Entries": "An incomplete
8247 structure, union or class type is represented by a structure,
8248 union or class entry that does not have a byte size attribute
8249 and that has a DW_AT_declaration attribute." */
8250 if (!pdi->has_byte_size && pdi->is_declaration)
8251 return;
8252
8253 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8254 static vs. global. */
8255 add_psymbol_to_list (actual_name,
8256 built_actual_name != NULL,
8257 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8258 cu->language == language_cplus
8259 ? psymbol_placement::GLOBAL
8260 : psymbol_placement::STATIC,
8261 0, cu->language, objfile);
8262
8263 break;
8264 case DW_TAG_enumerator:
8265 add_psymbol_to_list (actual_name,
8266 built_actual_name != NULL,
8267 VAR_DOMAIN, LOC_CONST, -1,
8268 cu->language == language_cplus
8269 ? psymbol_placement::GLOBAL
8270 : psymbol_placement::STATIC,
8271 0, cu->language, objfile);
8272 break;
8273 default:
8274 break;
8275 }
8276 }
8277
8278 /* Read a partial die corresponding to a namespace; also, add a symbol
8279 corresponding to that namespace to the symbol table. NAMESPACE is
8280 the name of the enclosing namespace. */
8281
8282 static void
8283 add_partial_namespace (struct partial_die_info *pdi,
8284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8285 int set_addrmap, struct dwarf2_cu *cu)
8286 {
8287 /* Add a symbol for the namespace. */
8288
8289 add_partial_symbol (pdi, cu);
8290
8291 /* Now scan partial symbols in that namespace. */
8292
8293 if (pdi->has_children)
8294 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8295 }
8296
8297 /* Read a partial die corresponding to a Fortran module. */
8298
8299 static void
8300 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8301 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8302 {
8303 /* Add a symbol for the namespace. */
8304
8305 add_partial_symbol (pdi, cu);
8306
8307 /* Now scan partial symbols in that module. */
8308
8309 if (pdi->has_children)
8310 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8311 }
8312
8313 /* Read a partial die corresponding to a subprogram or an inlined
8314 subprogram and create a partial symbol for that subprogram.
8315 When the CU language allows it, this routine also defines a partial
8316 symbol for each nested subprogram that this subprogram contains.
8317 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8318 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8319
8320 PDI may also be a lexical block, in which case we simply search
8321 recursively for subprograms defined inside that lexical block.
8322 Again, this is only performed when the CU language allows this
8323 type of definitions. */
8324
8325 static void
8326 add_partial_subprogram (struct partial_die_info *pdi,
8327 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8328 int set_addrmap, struct dwarf2_cu *cu)
8329 {
8330 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8331 {
8332 if (pdi->has_pc_info)
8333 {
8334 if (pdi->lowpc < *lowpc)
8335 *lowpc = pdi->lowpc;
8336 if (pdi->highpc > *highpc)
8337 *highpc = pdi->highpc;
8338 if (set_addrmap)
8339 {
8340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8341 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8342 CORE_ADDR baseaddr;
8343 CORE_ADDR this_highpc;
8344 CORE_ADDR this_lowpc;
8345
8346 baseaddr = objfile->text_section_offset ();
8347 this_lowpc
8348 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8349 pdi->lowpc + baseaddr)
8350 - baseaddr);
8351 this_highpc
8352 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8353 pdi->highpc + baseaddr)
8354 - baseaddr);
8355 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8356 this_lowpc, this_highpc - 1,
8357 cu->per_cu->v.psymtab);
8358 }
8359 }
8360
8361 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8362 {
8363 if (!pdi->is_declaration)
8364 /* Ignore subprogram DIEs that do not have a name, they are
8365 illegal. Do not emit a complaint at this point, we will
8366 do so when we convert this psymtab into a symtab. */
8367 if (pdi->name)
8368 add_partial_symbol (pdi, cu);
8369 }
8370 }
8371
8372 if (! pdi->has_children)
8373 return;
8374
8375 if (cu->language == language_ada || cu->language == language_fortran)
8376 {
8377 pdi = pdi->die_child;
8378 while (pdi != NULL)
8379 {
8380 pdi->fixup (cu);
8381 if (pdi->tag == DW_TAG_subprogram
8382 || pdi->tag == DW_TAG_inlined_subroutine
8383 || pdi->tag == DW_TAG_lexical_block)
8384 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8385 pdi = pdi->die_sibling;
8386 }
8387 }
8388 }
8389
8390 /* Read a partial die corresponding to an enumeration type. */
8391
8392 static void
8393 add_partial_enumeration (struct partial_die_info *enum_pdi,
8394 struct dwarf2_cu *cu)
8395 {
8396 struct partial_die_info *pdi;
8397
8398 if (enum_pdi->name != NULL)
8399 add_partial_symbol (enum_pdi, cu);
8400
8401 pdi = enum_pdi->die_child;
8402 while (pdi)
8403 {
8404 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8405 complaint (_("malformed enumerator DIE ignored"));
8406 else
8407 add_partial_symbol (pdi, cu);
8408 pdi = pdi->die_sibling;
8409 }
8410 }
8411
8412 /* Return the initial uleb128 in the die at INFO_PTR. */
8413
8414 static unsigned int
8415 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8416 {
8417 unsigned int bytes_read;
8418
8419 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8420 }
8421
8422 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8423 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8424
8425 Return the corresponding abbrev, or NULL if the number is zero (indicating
8426 an empty DIE). In either case *BYTES_READ will be set to the length of
8427 the initial number. */
8428
8429 static struct abbrev_info *
8430 peek_die_abbrev (const die_reader_specs &reader,
8431 const gdb_byte *info_ptr, unsigned int *bytes_read)
8432 {
8433 dwarf2_cu *cu = reader.cu;
8434 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8435 unsigned int abbrev_number
8436 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8437
8438 if (abbrev_number == 0)
8439 return NULL;
8440
8441 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8442 if (!abbrev)
8443 {
8444 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8445 " at offset %s [in module %s]"),
8446 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8447 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8448 }
8449
8450 return abbrev;
8451 }
8452
8453 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8454 Returns a pointer to the end of a series of DIEs, terminated by an empty
8455 DIE. Any children of the skipped DIEs will also be skipped. */
8456
8457 static const gdb_byte *
8458 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8459 {
8460 while (1)
8461 {
8462 unsigned int bytes_read;
8463 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8464
8465 if (abbrev == NULL)
8466 return info_ptr + bytes_read;
8467 else
8468 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8469 }
8470 }
8471
8472 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8473 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8474 abbrev corresponding to that skipped uleb128 should be passed in
8475 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8476 children. */
8477
8478 static const gdb_byte *
8479 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8480 struct abbrev_info *abbrev)
8481 {
8482 unsigned int bytes_read;
8483 struct attribute attr;
8484 bfd *abfd = reader->abfd;
8485 struct dwarf2_cu *cu = reader->cu;
8486 const gdb_byte *buffer = reader->buffer;
8487 const gdb_byte *buffer_end = reader->buffer_end;
8488 unsigned int form, i;
8489
8490 for (i = 0; i < abbrev->num_attrs; i++)
8491 {
8492 /* The only abbrev we care about is DW_AT_sibling. */
8493 if (abbrev->attrs[i].name == DW_AT_sibling)
8494 {
8495 bool ignored;
8496 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8497 &ignored);
8498 if (attr.form == DW_FORM_ref_addr)
8499 complaint (_("ignoring absolute DW_AT_sibling"));
8500 else
8501 {
8502 sect_offset off = attr.get_ref_die_offset ();
8503 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8504
8505 if (sibling_ptr < info_ptr)
8506 complaint (_("DW_AT_sibling points backwards"));
8507 else if (sibling_ptr > reader->buffer_end)
8508 reader->die_section->overflow_complaint ();
8509 else
8510 return sibling_ptr;
8511 }
8512 }
8513
8514 /* If it isn't DW_AT_sibling, skip this attribute. */
8515 form = abbrev->attrs[i].form;
8516 skip_attribute:
8517 switch (form)
8518 {
8519 case DW_FORM_ref_addr:
8520 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8521 and later it is offset sized. */
8522 if (cu->header.version == 2)
8523 info_ptr += cu->header.addr_size;
8524 else
8525 info_ptr += cu->header.offset_size;
8526 break;
8527 case DW_FORM_GNU_ref_alt:
8528 info_ptr += cu->header.offset_size;
8529 break;
8530 case DW_FORM_addr:
8531 info_ptr += cu->header.addr_size;
8532 break;
8533 case DW_FORM_data1:
8534 case DW_FORM_ref1:
8535 case DW_FORM_flag:
8536 case DW_FORM_strx1:
8537 info_ptr += 1;
8538 break;
8539 case DW_FORM_flag_present:
8540 case DW_FORM_implicit_const:
8541 break;
8542 case DW_FORM_data2:
8543 case DW_FORM_ref2:
8544 case DW_FORM_strx2:
8545 info_ptr += 2;
8546 break;
8547 case DW_FORM_strx3:
8548 info_ptr += 3;
8549 break;
8550 case DW_FORM_data4:
8551 case DW_FORM_ref4:
8552 case DW_FORM_strx4:
8553 info_ptr += 4;
8554 break;
8555 case DW_FORM_data8:
8556 case DW_FORM_ref8:
8557 case DW_FORM_ref_sig8:
8558 info_ptr += 8;
8559 break;
8560 case DW_FORM_data16:
8561 info_ptr += 16;
8562 break;
8563 case DW_FORM_string:
8564 read_direct_string (abfd, info_ptr, &bytes_read);
8565 info_ptr += bytes_read;
8566 break;
8567 case DW_FORM_sec_offset:
8568 case DW_FORM_strp:
8569 case DW_FORM_GNU_strp_alt:
8570 info_ptr += cu->header.offset_size;
8571 break;
8572 case DW_FORM_exprloc:
8573 case DW_FORM_block:
8574 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8575 info_ptr += bytes_read;
8576 break;
8577 case DW_FORM_block1:
8578 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8579 break;
8580 case DW_FORM_block2:
8581 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8582 break;
8583 case DW_FORM_block4:
8584 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8585 break;
8586 case DW_FORM_addrx:
8587 case DW_FORM_strx:
8588 case DW_FORM_sdata:
8589 case DW_FORM_udata:
8590 case DW_FORM_ref_udata:
8591 case DW_FORM_GNU_addr_index:
8592 case DW_FORM_GNU_str_index:
8593 case DW_FORM_rnglistx:
8594 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8595 break;
8596 case DW_FORM_indirect:
8597 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8598 info_ptr += bytes_read;
8599 /* We need to continue parsing from here, so just go back to
8600 the top. */
8601 goto skip_attribute;
8602
8603 default:
8604 error (_("Dwarf Error: Cannot handle %s "
8605 "in DWARF reader [in module %s]"),
8606 dwarf_form_name (form),
8607 bfd_get_filename (abfd));
8608 }
8609 }
8610
8611 if (abbrev->has_children)
8612 return skip_children (reader, info_ptr);
8613 else
8614 return info_ptr;
8615 }
8616
8617 /* Locate ORIG_PDI's sibling.
8618 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8619
8620 static const gdb_byte *
8621 locate_pdi_sibling (const struct die_reader_specs *reader,
8622 struct partial_die_info *orig_pdi,
8623 const gdb_byte *info_ptr)
8624 {
8625 /* Do we know the sibling already? */
8626
8627 if (orig_pdi->sibling)
8628 return orig_pdi->sibling;
8629
8630 /* Are there any children to deal with? */
8631
8632 if (!orig_pdi->has_children)
8633 return info_ptr;
8634
8635 /* Skip the children the long way. */
8636
8637 return skip_children (reader, info_ptr);
8638 }
8639
8640 /* Expand this partial symbol table into a full symbol table. SELF is
8641 not NULL. */
8642
8643 void
8644 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8645 {
8646 struct dwarf2_per_objfile *dwarf2_per_objfile
8647 = get_dwarf2_per_objfile (objfile);
8648
8649 gdb_assert (!readin);
8650 /* If this psymtab is constructed from a debug-only objfile, the
8651 has_section_at_zero flag will not necessarily be correct. We
8652 can get the correct value for this flag by looking at the data
8653 associated with the (presumably stripped) associated objfile. */
8654 if (objfile->separate_debug_objfile_backlink)
8655 {
8656 struct dwarf2_per_objfile *dpo_backlink
8657 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8658
8659 dwarf2_per_objfile->has_section_at_zero
8660 = dpo_backlink->has_section_at_zero;
8661 }
8662
8663 expand_psymtab (objfile);
8664
8665 process_cu_includes (dwarf2_per_objfile);
8666 }
8667 \f
8668 /* Reading in full CUs. */
8669
8670 /* Add PER_CU to the queue. */
8671
8672 static void
8673 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8674 enum language pretend_language)
8675 {
8676 per_cu->queued = 1;
8677 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8678 }
8679
8680 /* If PER_CU is not yet queued, add it to the queue.
8681 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8682 dependency.
8683 The result is non-zero if PER_CU was queued, otherwise the result is zero
8684 meaning either PER_CU is already queued or it is already loaded.
8685
8686 N.B. There is an invariant here that if a CU is queued then it is loaded.
8687 The caller is required to load PER_CU if we return non-zero. */
8688
8689 static int
8690 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8691 struct dwarf2_per_cu_data *per_cu,
8692 enum language pretend_language)
8693 {
8694 /* We may arrive here during partial symbol reading, if we need full
8695 DIEs to process an unusual case (e.g. template arguments). Do
8696 not queue PER_CU, just tell our caller to load its DIEs. */
8697 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8698 {
8699 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8700 return 1;
8701 return 0;
8702 }
8703
8704 /* Mark the dependence relation so that we don't flush PER_CU
8705 too early. */
8706 if (dependent_cu != NULL)
8707 dwarf2_add_dependence (dependent_cu, per_cu);
8708
8709 /* If it's already on the queue, we have nothing to do. */
8710 if (per_cu->queued)
8711 return 0;
8712
8713 /* If the compilation unit is already loaded, just mark it as
8714 used. */
8715 if (per_cu->cu != NULL)
8716 {
8717 per_cu->cu->last_used = 0;
8718 return 0;
8719 }
8720
8721 /* Add it to the queue. */
8722 queue_comp_unit (per_cu, pretend_language);
8723
8724 return 1;
8725 }
8726
8727 /* Process the queue. */
8728
8729 static void
8730 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8731 {
8732 if (dwarf_read_debug)
8733 {
8734 fprintf_unfiltered (gdb_stdlog,
8735 "Expanding one or more symtabs of objfile %s ...\n",
8736 objfile_name (dwarf2_per_objfile->objfile));
8737 }
8738
8739 /* The queue starts out with one item, but following a DIE reference
8740 may load a new CU, adding it to the end of the queue. */
8741 while (!dwarf2_per_objfile->queue.empty ())
8742 {
8743 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8744
8745 if ((dwarf2_per_objfile->using_index
8746 ? !item.per_cu->v.quick->compunit_symtab
8747 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8748 /* Skip dummy CUs. */
8749 && item.per_cu->cu != NULL)
8750 {
8751 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8752 unsigned int debug_print_threshold;
8753 char buf[100];
8754
8755 if (per_cu->is_debug_types)
8756 {
8757 struct signatured_type *sig_type =
8758 (struct signatured_type *) per_cu;
8759
8760 sprintf (buf, "TU %s at offset %s",
8761 hex_string (sig_type->signature),
8762 sect_offset_str (per_cu->sect_off));
8763 /* There can be 100s of TUs.
8764 Only print them in verbose mode. */
8765 debug_print_threshold = 2;
8766 }
8767 else
8768 {
8769 sprintf (buf, "CU at offset %s",
8770 sect_offset_str (per_cu->sect_off));
8771 debug_print_threshold = 1;
8772 }
8773
8774 if (dwarf_read_debug >= debug_print_threshold)
8775 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8776
8777 if (per_cu->is_debug_types)
8778 process_full_type_unit (per_cu, item.pretend_language);
8779 else
8780 process_full_comp_unit (per_cu, item.pretend_language);
8781
8782 if (dwarf_read_debug >= debug_print_threshold)
8783 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8784 }
8785
8786 item.per_cu->queued = 0;
8787 dwarf2_per_objfile->queue.pop ();
8788 }
8789
8790 if (dwarf_read_debug)
8791 {
8792 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8793 objfile_name (dwarf2_per_objfile->objfile));
8794 }
8795 }
8796
8797 /* Read in full symbols for PST, and anything it depends on. */
8798
8799 void
8800 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8801 {
8802 if (readin)
8803 return;
8804
8805 expand_dependencies (objfile);
8806
8807 dw2_do_instantiate_symtab (per_cu_data, false);
8808 gdb_assert (get_compunit_symtab () != nullptr);
8809 }
8810
8811 /* Trivial hash function for die_info: the hash value of a DIE
8812 is its offset in .debug_info for this objfile. */
8813
8814 static hashval_t
8815 die_hash (const void *item)
8816 {
8817 const struct die_info *die = (const struct die_info *) item;
8818
8819 return to_underlying (die->sect_off);
8820 }
8821
8822 /* Trivial comparison function for die_info structures: two DIEs
8823 are equal if they have the same offset. */
8824
8825 static int
8826 die_eq (const void *item_lhs, const void *item_rhs)
8827 {
8828 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8829 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8830
8831 return die_lhs->sect_off == die_rhs->sect_off;
8832 }
8833
8834 /* Load the DIEs associated with PER_CU into memory. */
8835
8836 static void
8837 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8838 bool skip_partial,
8839 enum language pretend_language)
8840 {
8841 gdb_assert (! this_cu->is_debug_types);
8842
8843 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8844 if (reader.dummy_p)
8845 return;
8846
8847 struct dwarf2_cu *cu = reader.cu;
8848 const gdb_byte *info_ptr = reader.info_ptr;
8849
8850 gdb_assert (cu->die_hash == NULL);
8851 cu->die_hash =
8852 htab_create_alloc_ex (cu->header.length / 12,
8853 die_hash,
8854 die_eq,
8855 NULL,
8856 &cu->comp_unit_obstack,
8857 hashtab_obstack_allocate,
8858 dummy_obstack_deallocate);
8859
8860 if (reader.comp_unit_die->has_children)
8861 reader.comp_unit_die->child
8862 = read_die_and_siblings (&reader, reader.info_ptr,
8863 &info_ptr, reader.comp_unit_die);
8864 cu->dies = reader.comp_unit_die;
8865 /* comp_unit_die is not stored in die_hash, no need. */
8866
8867 /* We try not to read any attributes in this function, because not
8868 all CUs needed for references have been loaded yet, and symbol
8869 table processing isn't initialized. But we have to set the CU language,
8870 or we won't be able to build types correctly.
8871 Similarly, if we do not read the producer, we can not apply
8872 producer-specific interpretation. */
8873 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8874
8875 reader.keep ();
8876 }
8877
8878 /* Add a DIE to the delayed physname list. */
8879
8880 static void
8881 add_to_method_list (struct type *type, int fnfield_index, int index,
8882 const char *name, struct die_info *die,
8883 struct dwarf2_cu *cu)
8884 {
8885 struct delayed_method_info mi;
8886 mi.type = type;
8887 mi.fnfield_index = fnfield_index;
8888 mi.index = index;
8889 mi.name = name;
8890 mi.die = die;
8891 cu->method_list.push_back (mi);
8892 }
8893
8894 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8895 "const" / "volatile". If so, decrements LEN by the length of the
8896 modifier and return true. Otherwise return false. */
8897
8898 template<size_t N>
8899 static bool
8900 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8901 {
8902 size_t mod_len = sizeof (mod) - 1;
8903 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8904 {
8905 len -= mod_len;
8906 return true;
8907 }
8908 return false;
8909 }
8910
8911 /* Compute the physnames of any methods on the CU's method list.
8912
8913 The computation of method physnames is delayed in order to avoid the
8914 (bad) condition that one of the method's formal parameters is of an as yet
8915 incomplete type. */
8916
8917 static void
8918 compute_delayed_physnames (struct dwarf2_cu *cu)
8919 {
8920 /* Only C++ delays computing physnames. */
8921 if (cu->method_list.empty ())
8922 return;
8923 gdb_assert (cu->language == language_cplus);
8924
8925 for (const delayed_method_info &mi : cu->method_list)
8926 {
8927 const char *physname;
8928 struct fn_fieldlist *fn_flp
8929 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8930 physname = dwarf2_physname (mi.name, mi.die, cu);
8931 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8932 = physname ? physname : "";
8933
8934 /* Since there's no tag to indicate whether a method is a
8935 const/volatile overload, extract that information out of the
8936 demangled name. */
8937 if (physname != NULL)
8938 {
8939 size_t len = strlen (physname);
8940
8941 while (1)
8942 {
8943 if (physname[len] == ')') /* shortcut */
8944 break;
8945 else if (check_modifier (physname, len, " const"))
8946 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8947 else if (check_modifier (physname, len, " volatile"))
8948 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8949 else
8950 break;
8951 }
8952 }
8953 }
8954
8955 /* The list is no longer needed. */
8956 cu->method_list.clear ();
8957 }
8958
8959 /* Go objects should be embedded in a DW_TAG_module DIE,
8960 and it's not clear if/how imported objects will appear.
8961 To keep Go support simple until that's worked out,
8962 go back through what we've read and create something usable.
8963 We could do this while processing each DIE, and feels kinda cleaner,
8964 but that way is more invasive.
8965 This is to, for example, allow the user to type "p var" or "b main"
8966 without having to specify the package name, and allow lookups
8967 of module.object to work in contexts that use the expression
8968 parser. */
8969
8970 static void
8971 fixup_go_packaging (struct dwarf2_cu *cu)
8972 {
8973 gdb::unique_xmalloc_ptr<char> package_name;
8974 struct pending *list;
8975 int i;
8976
8977 for (list = *cu->get_builder ()->get_global_symbols ();
8978 list != NULL;
8979 list = list->next)
8980 {
8981 for (i = 0; i < list->nsyms; ++i)
8982 {
8983 struct symbol *sym = list->symbol[i];
8984
8985 if (sym->language () == language_go
8986 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8987 {
8988 gdb::unique_xmalloc_ptr<char> this_package_name
8989 (go_symbol_package_name (sym));
8990
8991 if (this_package_name == NULL)
8992 continue;
8993 if (package_name == NULL)
8994 package_name = std::move (this_package_name);
8995 else
8996 {
8997 struct objfile *objfile
8998 = cu->per_cu->dwarf2_per_objfile->objfile;
8999 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9000 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9001 (symbol_symtab (sym) != NULL
9002 ? symtab_to_filename_for_display
9003 (symbol_symtab (sym))
9004 : objfile_name (objfile)),
9005 this_package_name.get (), package_name.get ());
9006 }
9007 }
9008 }
9009 }
9010
9011 if (package_name != NULL)
9012 {
9013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9014 const char *saved_package_name = objfile->intern (package_name.get ());
9015 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9016 saved_package_name);
9017 struct symbol *sym;
9018
9019 sym = allocate_symbol (objfile);
9020 sym->set_language (language_go, &objfile->objfile_obstack);
9021 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9022 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9023 e.g., "main" finds the "main" module and not C's main(). */
9024 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9025 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9026 SYMBOL_TYPE (sym) = type;
9027
9028 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9029 }
9030 }
9031
9032 /* Allocate a fully-qualified name consisting of the two parts on the
9033 obstack. */
9034
9035 static const char *
9036 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9037 {
9038 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9039 }
9040
9041 /* A helper that allocates a struct discriminant_info to attach to a
9042 union type. */
9043
9044 static struct discriminant_info *
9045 alloc_discriminant_info (struct type *type, int discriminant_index,
9046 int default_index)
9047 {
9048 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9049 gdb_assert (discriminant_index == -1
9050 || (discriminant_index >= 0
9051 && discriminant_index < TYPE_NFIELDS (type)));
9052 gdb_assert (default_index == -1
9053 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9054
9055 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9056
9057 struct discriminant_info *disc
9058 = ((struct discriminant_info *)
9059 TYPE_ZALLOC (type,
9060 offsetof (struct discriminant_info, discriminants)
9061 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9062 disc->default_index = default_index;
9063 disc->discriminant_index = discriminant_index;
9064
9065 struct dynamic_prop prop;
9066 prop.kind = PROP_UNDEFINED;
9067 prop.data.baton = disc;
9068
9069 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9070
9071 return disc;
9072 }
9073
9074 /* Some versions of rustc emitted enums in an unusual way.
9075
9076 Ordinary enums were emitted as unions. The first element of each
9077 structure in the union was named "RUST$ENUM$DISR". This element
9078 held the discriminant.
9079
9080 These versions of Rust also implemented the "non-zero"
9081 optimization. When the enum had two values, and one is empty and
9082 the other holds a pointer that cannot be zero, the pointer is used
9083 as the discriminant, with a zero value meaning the empty variant.
9084 Here, the union's first member is of the form
9085 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9086 where the fieldnos are the indices of the fields that should be
9087 traversed in order to find the field (which may be several fields deep)
9088 and the variantname is the name of the variant of the case when the
9089 field is zero.
9090
9091 This function recognizes whether TYPE is of one of these forms,
9092 and, if so, smashes it to be a variant type. */
9093
9094 static void
9095 quirk_rust_enum (struct type *type, struct objfile *objfile)
9096 {
9097 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9098
9099 /* We don't need to deal with empty enums. */
9100 if (TYPE_NFIELDS (type) == 0)
9101 return;
9102
9103 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9104 if (TYPE_NFIELDS (type) == 1
9105 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9106 {
9107 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9108
9109 /* Decode the field name to find the offset of the
9110 discriminant. */
9111 ULONGEST bit_offset = 0;
9112 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9113 while (name[0] >= '0' && name[0] <= '9')
9114 {
9115 char *tail;
9116 unsigned long index = strtoul (name, &tail, 10);
9117 name = tail;
9118 if (*name != '$'
9119 || index >= TYPE_NFIELDS (field_type)
9120 || (TYPE_FIELD_LOC_KIND (field_type, index)
9121 != FIELD_LOC_KIND_BITPOS))
9122 {
9123 complaint (_("Could not parse Rust enum encoding string \"%s\""
9124 "[in module %s]"),
9125 TYPE_FIELD_NAME (type, 0),
9126 objfile_name (objfile));
9127 return;
9128 }
9129 ++name;
9130
9131 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9132 field_type = TYPE_FIELD_TYPE (field_type, index);
9133 }
9134
9135 /* Make a union to hold the variants. */
9136 struct type *union_type = alloc_type (objfile);
9137 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9138 TYPE_NFIELDS (union_type) = 3;
9139 TYPE_FIELDS (union_type)
9140 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9141 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9142 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9143
9144 /* Put the discriminant must at index 0. */
9145 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9146 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9147 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9148 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9149
9150 /* The order of fields doesn't really matter, so put the real
9151 field at index 1 and the data-less field at index 2. */
9152 struct discriminant_info *disc
9153 = alloc_discriminant_info (union_type, 0, 1);
9154 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9155 TYPE_FIELD_NAME (union_type, 1)
9156 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9157 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9158 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9159 TYPE_FIELD_NAME (union_type, 1));
9160
9161 const char *dataless_name
9162 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9163 name);
9164 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9165 dataless_name);
9166 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9167 /* NAME points into the original discriminant name, which
9168 already has the correct lifetime. */
9169 TYPE_FIELD_NAME (union_type, 2) = name;
9170 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9171 disc->discriminants[2] = 0;
9172
9173 /* Smash this type to be a structure type. We have to do this
9174 because the type has already been recorded. */
9175 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9176 TYPE_NFIELDS (type) = 1;
9177 TYPE_FIELDS (type)
9178 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9179
9180 /* Install the variant part. */
9181 TYPE_FIELD_TYPE (type, 0) = union_type;
9182 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9183 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9184 }
9185 /* A union with a single anonymous field is probably an old-style
9186 univariant enum. */
9187 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9188 {
9189 /* Smash this type to be a structure type. We have to do this
9190 because the type has already been recorded. */
9191 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9192
9193 /* Make a union to hold the variants. */
9194 struct type *union_type = alloc_type (objfile);
9195 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9196 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9197 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9198 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9199 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9200
9201 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9202 const char *variant_name
9203 = rust_last_path_segment (TYPE_NAME (field_type));
9204 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9205 TYPE_NAME (field_type)
9206 = rust_fully_qualify (&objfile->objfile_obstack,
9207 TYPE_NAME (type), variant_name);
9208
9209 /* Install the union in the outer struct type. */
9210 TYPE_NFIELDS (type) = 1;
9211 TYPE_FIELDS (type)
9212 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9213 TYPE_FIELD_TYPE (type, 0) = union_type;
9214 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9215 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9216
9217 alloc_discriminant_info (union_type, -1, 0);
9218 }
9219 else
9220 {
9221 struct type *disr_type = nullptr;
9222 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9223 {
9224 disr_type = TYPE_FIELD_TYPE (type, i);
9225
9226 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9227 {
9228 /* All fields of a true enum will be structs. */
9229 return;
9230 }
9231 else if (TYPE_NFIELDS (disr_type) == 0)
9232 {
9233 /* Could be data-less variant, so keep going. */
9234 disr_type = nullptr;
9235 }
9236 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9237 "RUST$ENUM$DISR") != 0)
9238 {
9239 /* Not a Rust enum. */
9240 return;
9241 }
9242 else
9243 {
9244 /* Found one. */
9245 break;
9246 }
9247 }
9248
9249 /* If we got here without a discriminant, then it's probably
9250 just a union. */
9251 if (disr_type == nullptr)
9252 return;
9253
9254 /* Smash this type to be a structure type. We have to do this
9255 because the type has already been recorded. */
9256 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9257
9258 /* Make a union to hold the variants. */
9259 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9260 struct type *union_type = alloc_type (objfile);
9261 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9262 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9263 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9264 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9265 TYPE_FIELDS (union_type)
9266 = (struct field *) TYPE_ZALLOC (union_type,
9267 (TYPE_NFIELDS (union_type)
9268 * sizeof (struct field)));
9269
9270 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9271 TYPE_NFIELDS (type) * sizeof (struct field));
9272
9273 /* Install the discriminant at index 0 in the union. */
9274 TYPE_FIELD (union_type, 0) = *disr_field;
9275 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9276 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9277
9278 /* Install the union in the outer struct type. */
9279 TYPE_FIELD_TYPE (type, 0) = union_type;
9280 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9281 TYPE_NFIELDS (type) = 1;
9282
9283 /* Set the size and offset of the union type. */
9284 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9285
9286 /* We need a way to find the correct discriminant given a
9287 variant name. For convenience we build a map here. */
9288 struct type *enum_type = FIELD_TYPE (*disr_field);
9289 std::unordered_map<std::string, ULONGEST> discriminant_map;
9290 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9291 {
9292 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9293 {
9294 const char *name
9295 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9296 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9297 }
9298 }
9299
9300 int n_fields = TYPE_NFIELDS (union_type);
9301 struct discriminant_info *disc
9302 = alloc_discriminant_info (union_type, 0, -1);
9303 /* Skip the discriminant here. */
9304 for (int i = 1; i < n_fields; ++i)
9305 {
9306 /* Find the final word in the name of this variant's type.
9307 That name can be used to look up the correct
9308 discriminant. */
9309 const char *variant_name
9310 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9311 i)));
9312
9313 auto iter = discriminant_map.find (variant_name);
9314 if (iter != discriminant_map.end ())
9315 disc->discriminants[i] = iter->second;
9316
9317 /* Remove the discriminant field, if it exists. */
9318 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9319 if (TYPE_NFIELDS (sub_type) > 0)
9320 {
9321 --TYPE_NFIELDS (sub_type);
9322 ++TYPE_FIELDS (sub_type);
9323 }
9324 TYPE_FIELD_NAME (union_type, i) = variant_name;
9325 TYPE_NAME (sub_type)
9326 = rust_fully_qualify (&objfile->objfile_obstack,
9327 TYPE_NAME (type), variant_name);
9328 }
9329 }
9330 }
9331
9332 /* Rewrite some Rust unions to be structures with variants parts. */
9333
9334 static void
9335 rust_union_quirks (struct dwarf2_cu *cu)
9336 {
9337 gdb_assert (cu->language == language_rust);
9338 for (type *type_ : cu->rust_unions)
9339 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9340 /* We don't need this any more. */
9341 cu->rust_unions.clear ();
9342 }
9343
9344 /* Return the symtab for PER_CU. This works properly regardless of
9345 whether we're using the index or psymtabs. */
9346
9347 static struct compunit_symtab *
9348 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9349 {
9350 return (per_cu->dwarf2_per_objfile->using_index
9351 ? per_cu->v.quick->compunit_symtab
9352 : per_cu->v.psymtab->compunit_symtab);
9353 }
9354
9355 /* A helper function for computing the list of all symbol tables
9356 included by PER_CU. */
9357
9358 static void
9359 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9360 htab_t all_children, htab_t all_type_symtabs,
9361 struct dwarf2_per_cu_data *per_cu,
9362 struct compunit_symtab *immediate_parent)
9363 {
9364 void **slot;
9365 struct compunit_symtab *cust;
9366
9367 slot = htab_find_slot (all_children, per_cu, INSERT);
9368 if (*slot != NULL)
9369 {
9370 /* This inclusion and its children have been processed. */
9371 return;
9372 }
9373
9374 *slot = per_cu;
9375 /* Only add a CU if it has a symbol table. */
9376 cust = get_compunit_symtab (per_cu);
9377 if (cust != NULL)
9378 {
9379 /* If this is a type unit only add its symbol table if we haven't
9380 seen it yet (type unit per_cu's can share symtabs). */
9381 if (per_cu->is_debug_types)
9382 {
9383 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9384 if (*slot == NULL)
9385 {
9386 *slot = cust;
9387 result->push_back (cust);
9388 if (cust->user == NULL)
9389 cust->user = immediate_parent;
9390 }
9391 }
9392 else
9393 {
9394 result->push_back (cust);
9395 if (cust->user == NULL)
9396 cust->user = immediate_parent;
9397 }
9398 }
9399
9400 if (!per_cu->imported_symtabs_empty ())
9401 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9402 {
9403 recursively_compute_inclusions (result, all_children,
9404 all_type_symtabs, ptr, cust);
9405 }
9406 }
9407
9408 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9409 PER_CU. */
9410
9411 static void
9412 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9413 {
9414 gdb_assert (! per_cu->is_debug_types);
9415
9416 if (!per_cu->imported_symtabs_empty ())
9417 {
9418 int len;
9419 std::vector<compunit_symtab *> result_symtabs;
9420 htab_t all_children, all_type_symtabs;
9421 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9422
9423 /* If we don't have a symtab, we can just skip this case. */
9424 if (cust == NULL)
9425 return;
9426
9427 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9428 NULL, xcalloc, xfree);
9429 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9430 NULL, xcalloc, xfree);
9431
9432 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9433 {
9434 recursively_compute_inclusions (&result_symtabs, all_children,
9435 all_type_symtabs, ptr, cust);
9436 }
9437
9438 /* Now we have a transitive closure of all the included symtabs. */
9439 len = result_symtabs.size ();
9440 cust->includes
9441 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9442 struct compunit_symtab *, len + 1);
9443 memcpy (cust->includes, result_symtabs.data (),
9444 len * sizeof (compunit_symtab *));
9445 cust->includes[len] = NULL;
9446
9447 htab_delete (all_children);
9448 htab_delete (all_type_symtabs);
9449 }
9450 }
9451
9452 /* Compute the 'includes' field for the symtabs of all the CUs we just
9453 read. */
9454
9455 static void
9456 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9457 {
9458 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9459 {
9460 if (! iter->is_debug_types)
9461 compute_compunit_symtab_includes (iter);
9462 }
9463
9464 dwarf2_per_objfile->just_read_cus.clear ();
9465 }
9466
9467 /* Generate full symbol information for PER_CU, whose DIEs have
9468 already been loaded into memory. */
9469
9470 static void
9471 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9472 enum language pretend_language)
9473 {
9474 struct dwarf2_cu *cu = per_cu->cu;
9475 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9476 struct objfile *objfile = dwarf2_per_objfile->objfile;
9477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9478 CORE_ADDR lowpc, highpc;
9479 struct compunit_symtab *cust;
9480 CORE_ADDR baseaddr;
9481 struct block *static_block;
9482 CORE_ADDR addr;
9483
9484 baseaddr = objfile->text_section_offset ();
9485
9486 /* Clear the list here in case something was left over. */
9487 cu->method_list.clear ();
9488
9489 cu->language = pretend_language;
9490 cu->language_defn = language_def (cu->language);
9491
9492 /* Do line number decoding in read_file_scope () */
9493 process_die (cu->dies, cu);
9494
9495 /* For now fudge the Go package. */
9496 if (cu->language == language_go)
9497 fixup_go_packaging (cu);
9498
9499 /* Now that we have processed all the DIEs in the CU, all the types
9500 should be complete, and it should now be safe to compute all of the
9501 physnames. */
9502 compute_delayed_physnames (cu);
9503
9504 if (cu->language == language_rust)
9505 rust_union_quirks (cu);
9506
9507 /* Some compilers don't define a DW_AT_high_pc attribute for the
9508 compilation unit. If the DW_AT_high_pc is missing, synthesize
9509 it, by scanning the DIE's below the compilation unit. */
9510 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9511
9512 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9513 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9514
9515 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9516 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9517 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9518 addrmap to help ensure it has an accurate map of pc values belonging to
9519 this comp unit. */
9520 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9521
9522 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9523 SECT_OFF_TEXT (objfile),
9524 0);
9525
9526 if (cust != NULL)
9527 {
9528 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9529
9530 /* Set symtab language to language from DW_AT_language. If the
9531 compilation is from a C file generated by language preprocessors, do
9532 not set the language if it was already deduced by start_subfile. */
9533 if (!(cu->language == language_c
9534 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9535 COMPUNIT_FILETABS (cust)->language = cu->language;
9536
9537 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9538 produce DW_AT_location with location lists but it can be possibly
9539 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9540 there were bugs in prologue debug info, fixed later in GCC-4.5
9541 by "unwind info for epilogues" patch (which is not directly related).
9542
9543 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9544 needed, it would be wrong due to missing DW_AT_producer there.
9545
9546 Still one can confuse GDB by using non-standard GCC compilation
9547 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9548 */
9549 if (cu->has_loclist && gcc_4_minor >= 5)
9550 cust->locations_valid = 1;
9551
9552 if (gcc_4_minor >= 5)
9553 cust->epilogue_unwind_valid = 1;
9554
9555 cust->call_site_htab = cu->call_site_htab;
9556 }
9557
9558 if (dwarf2_per_objfile->using_index)
9559 per_cu->v.quick->compunit_symtab = cust;
9560 else
9561 {
9562 dwarf2_psymtab *pst = per_cu->v.psymtab;
9563 pst->compunit_symtab = cust;
9564 pst->readin = true;
9565 }
9566
9567 /* Push it for inclusion processing later. */
9568 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9569
9570 /* Not needed any more. */
9571 cu->reset_builder ();
9572 }
9573
9574 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9575 already been loaded into memory. */
9576
9577 static void
9578 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9579 enum language pretend_language)
9580 {
9581 struct dwarf2_cu *cu = per_cu->cu;
9582 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9583 struct objfile *objfile = dwarf2_per_objfile->objfile;
9584 struct compunit_symtab *cust;
9585 struct signatured_type *sig_type;
9586
9587 gdb_assert (per_cu->is_debug_types);
9588 sig_type = (struct signatured_type *) per_cu;
9589
9590 /* Clear the list here in case something was left over. */
9591 cu->method_list.clear ();
9592
9593 cu->language = pretend_language;
9594 cu->language_defn = language_def (cu->language);
9595
9596 /* The symbol tables are set up in read_type_unit_scope. */
9597 process_die (cu->dies, cu);
9598
9599 /* For now fudge the Go package. */
9600 if (cu->language == language_go)
9601 fixup_go_packaging (cu);
9602
9603 /* Now that we have processed all the DIEs in the CU, all the types
9604 should be complete, and it should now be safe to compute all of the
9605 physnames. */
9606 compute_delayed_physnames (cu);
9607
9608 if (cu->language == language_rust)
9609 rust_union_quirks (cu);
9610
9611 /* TUs share symbol tables.
9612 If this is the first TU to use this symtab, complete the construction
9613 of it with end_expandable_symtab. Otherwise, complete the addition of
9614 this TU's symbols to the existing symtab. */
9615 if (sig_type->type_unit_group->compunit_symtab == NULL)
9616 {
9617 buildsym_compunit *builder = cu->get_builder ();
9618 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9619 sig_type->type_unit_group->compunit_symtab = cust;
9620
9621 if (cust != NULL)
9622 {
9623 /* Set symtab language to language from DW_AT_language. If the
9624 compilation is from a C file generated by language preprocessors,
9625 do not set the language if it was already deduced by
9626 start_subfile. */
9627 if (!(cu->language == language_c
9628 && COMPUNIT_FILETABS (cust)->language != language_c))
9629 COMPUNIT_FILETABS (cust)->language = cu->language;
9630 }
9631 }
9632 else
9633 {
9634 cu->get_builder ()->augment_type_symtab ();
9635 cust = sig_type->type_unit_group->compunit_symtab;
9636 }
9637
9638 if (dwarf2_per_objfile->using_index)
9639 per_cu->v.quick->compunit_symtab = cust;
9640 else
9641 {
9642 dwarf2_psymtab *pst = per_cu->v.psymtab;
9643 pst->compunit_symtab = cust;
9644 pst->readin = true;
9645 }
9646
9647 /* Not needed any more. */
9648 cu->reset_builder ();
9649 }
9650
9651 /* Process an imported unit DIE. */
9652
9653 static void
9654 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9655 {
9656 struct attribute *attr;
9657
9658 /* For now we don't handle imported units in type units. */
9659 if (cu->per_cu->is_debug_types)
9660 {
9661 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9662 " supported in type units [in module %s]"),
9663 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9664 }
9665
9666 attr = dwarf2_attr (die, DW_AT_import, cu);
9667 if (attr != NULL)
9668 {
9669 sect_offset sect_off = attr->get_ref_die_offset ();
9670 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9671 dwarf2_per_cu_data *per_cu
9672 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9673 cu->per_cu->dwarf2_per_objfile);
9674
9675 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9676 into another compilation unit, at root level. Regard this as a hint,
9677 and ignore it. */
9678 if (die->parent && die->parent->parent == NULL
9679 && per_cu->unit_type == DW_UT_compile
9680 && per_cu->lang == language_cplus)
9681 return;
9682
9683 /* If necessary, add it to the queue and load its DIEs. */
9684 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9685 load_full_comp_unit (per_cu, false, cu->language);
9686
9687 cu->per_cu->imported_symtabs_push (per_cu);
9688 }
9689 }
9690
9691 /* RAII object that represents a process_die scope: i.e.,
9692 starts/finishes processing a DIE. */
9693 class process_die_scope
9694 {
9695 public:
9696 process_die_scope (die_info *die, dwarf2_cu *cu)
9697 : m_die (die), m_cu (cu)
9698 {
9699 /* We should only be processing DIEs not already in process. */
9700 gdb_assert (!m_die->in_process);
9701 m_die->in_process = true;
9702 }
9703
9704 ~process_die_scope ()
9705 {
9706 m_die->in_process = false;
9707
9708 /* If we're done processing the DIE for the CU that owns the line
9709 header, we don't need the line header anymore. */
9710 if (m_cu->line_header_die_owner == m_die)
9711 {
9712 delete m_cu->line_header;
9713 m_cu->line_header = NULL;
9714 m_cu->line_header_die_owner = NULL;
9715 }
9716 }
9717
9718 private:
9719 die_info *m_die;
9720 dwarf2_cu *m_cu;
9721 };
9722
9723 /* Process a die and its children. */
9724
9725 static void
9726 process_die (struct die_info *die, struct dwarf2_cu *cu)
9727 {
9728 process_die_scope scope (die, cu);
9729
9730 switch (die->tag)
9731 {
9732 case DW_TAG_padding:
9733 break;
9734 case DW_TAG_compile_unit:
9735 case DW_TAG_partial_unit:
9736 read_file_scope (die, cu);
9737 break;
9738 case DW_TAG_type_unit:
9739 read_type_unit_scope (die, cu);
9740 break;
9741 case DW_TAG_subprogram:
9742 /* Nested subprograms in Fortran get a prefix. */
9743 if (cu->language == language_fortran
9744 && die->parent != NULL
9745 && die->parent->tag == DW_TAG_subprogram)
9746 cu->processing_has_namespace_info = true;
9747 /* Fall through. */
9748 case DW_TAG_inlined_subroutine:
9749 read_func_scope (die, cu);
9750 break;
9751 case DW_TAG_lexical_block:
9752 case DW_TAG_try_block:
9753 case DW_TAG_catch_block:
9754 read_lexical_block_scope (die, cu);
9755 break;
9756 case DW_TAG_call_site:
9757 case DW_TAG_GNU_call_site:
9758 read_call_site_scope (die, cu);
9759 break;
9760 case DW_TAG_class_type:
9761 case DW_TAG_interface_type:
9762 case DW_TAG_structure_type:
9763 case DW_TAG_union_type:
9764 process_structure_scope (die, cu);
9765 break;
9766 case DW_TAG_enumeration_type:
9767 process_enumeration_scope (die, cu);
9768 break;
9769
9770 /* These dies have a type, but processing them does not create
9771 a symbol or recurse to process the children. Therefore we can
9772 read them on-demand through read_type_die. */
9773 case DW_TAG_subroutine_type:
9774 case DW_TAG_set_type:
9775 case DW_TAG_array_type:
9776 case DW_TAG_pointer_type:
9777 case DW_TAG_ptr_to_member_type:
9778 case DW_TAG_reference_type:
9779 case DW_TAG_rvalue_reference_type:
9780 case DW_TAG_string_type:
9781 break;
9782
9783 case DW_TAG_base_type:
9784 case DW_TAG_subrange_type:
9785 case DW_TAG_typedef:
9786 /* Add a typedef symbol for the type definition, if it has a
9787 DW_AT_name. */
9788 new_symbol (die, read_type_die (die, cu), cu);
9789 break;
9790 case DW_TAG_common_block:
9791 read_common_block (die, cu);
9792 break;
9793 case DW_TAG_common_inclusion:
9794 break;
9795 case DW_TAG_namespace:
9796 cu->processing_has_namespace_info = true;
9797 read_namespace (die, cu);
9798 break;
9799 case DW_TAG_module:
9800 cu->processing_has_namespace_info = true;
9801 read_module (die, cu);
9802 break;
9803 case DW_TAG_imported_declaration:
9804 cu->processing_has_namespace_info = true;
9805 if (read_namespace_alias (die, cu))
9806 break;
9807 /* The declaration is not a global namespace alias. */
9808 /* Fall through. */
9809 case DW_TAG_imported_module:
9810 cu->processing_has_namespace_info = true;
9811 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9812 || cu->language != language_fortran))
9813 complaint (_("Tag '%s' has unexpected children"),
9814 dwarf_tag_name (die->tag));
9815 read_import_statement (die, cu);
9816 break;
9817
9818 case DW_TAG_imported_unit:
9819 process_imported_unit_die (die, cu);
9820 break;
9821
9822 case DW_TAG_variable:
9823 read_variable (die, cu);
9824 break;
9825
9826 default:
9827 new_symbol (die, NULL, cu);
9828 break;
9829 }
9830 }
9831 \f
9832 /* DWARF name computation. */
9833
9834 /* A helper function for dwarf2_compute_name which determines whether DIE
9835 needs to have the name of the scope prepended to the name listed in the
9836 die. */
9837
9838 static int
9839 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9840 {
9841 struct attribute *attr;
9842
9843 switch (die->tag)
9844 {
9845 case DW_TAG_namespace:
9846 case DW_TAG_typedef:
9847 case DW_TAG_class_type:
9848 case DW_TAG_interface_type:
9849 case DW_TAG_structure_type:
9850 case DW_TAG_union_type:
9851 case DW_TAG_enumeration_type:
9852 case DW_TAG_enumerator:
9853 case DW_TAG_subprogram:
9854 case DW_TAG_inlined_subroutine:
9855 case DW_TAG_member:
9856 case DW_TAG_imported_declaration:
9857 return 1;
9858
9859 case DW_TAG_variable:
9860 case DW_TAG_constant:
9861 /* We only need to prefix "globally" visible variables. These include
9862 any variable marked with DW_AT_external or any variable that
9863 lives in a namespace. [Variables in anonymous namespaces
9864 require prefixing, but they are not DW_AT_external.] */
9865
9866 if (dwarf2_attr (die, DW_AT_specification, cu))
9867 {
9868 struct dwarf2_cu *spec_cu = cu;
9869
9870 return die_needs_namespace (die_specification (die, &spec_cu),
9871 spec_cu);
9872 }
9873
9874 attr = dwarf2_attr (die, DW_AT_external, cu);
9875 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9876 && die->parent->tag != DW_TAG_module)
9877 return 0;
9878 /* A variable in a lexical block of some kind does not need a
9879 namespace, even though in C++ such variables may be external
9880 and have a mangled name. */
9881 if (die->parent->tag == DW_TAG_lexical_block
9882 || die->parent->tag == DW_TAG_try_block
9883 || die->parent->tag == DW_TAG_catch_block
9884 || die->parent->tag == DW_TAG_subprogram)
9885 return 0;
9886 return 1;
9887
9888 default:
9889 return 0;
9890 }
9891 }
9892
9893 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9894 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9895 defined for the given DIE. */
9896
9897 static struct attribute *
9898 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9899 {
9900 struct attribute *attr;
9901
9902 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9903 if (attr == NULL)
9904 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9905
9906 return attr;
9907 }
9908
9909 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9910 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9911 defined for the given DIE. */
9912
9913 static const char *
9914 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9915 {
9916 const char *linkage_name;
9917
9918 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9919 if (linkage_name == NULL)
9920 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9921
9922 return linkage_name;
9923 }
9924
9925 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9926 compute the physname for the object, which include a method's:
9927 - formal parameters (C++),
9928 - receiver type (Go),
9929
9930 The term "physname" is a bit confusing.
9931 For C++, for example, it is the demangled name.
9932 For Go, for example, it's the mangled name.
9933
9934 For Ada, return the DIE's linkage name rather than the fully qualified
9935 name. PHYSNAME is ignored..
9936
9937 The result is allocated on the objfile_obstack and canonicalized. */
9938
9939 static const char *
9940 dwarf2_compute_name (const char *name,
9941 struct die_info *die, struct dwarf2_cu *cu,
9942 int physname)
9943 {
9944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9945
9946 if (name == NULL)
9947 name = dwarf2_name (die, cu);
9948
9949 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9950 but otherwise compute it by typename_concat inside GDB.
9951 FIXME: Actually this is not really true, or at least not always true.
9952 It's all very confusing. compute_and_set_names doesn't try to demangle
9953 Fortran names because there is no mangling standard. So new_symbol
9954 will set the demangled name to the result of dwarf2_full_name, and it is
9955 the demangled name that GDB uses if it exists. */
9956 if (cu->language == language_ada
9957 || (cu->language == language_fortran && physname))
9958 {
9959 /* For Ada unit, we prefer the linkage name over the name, as
9960 the former contains the exported name, which the user expects
9961 to be able to reference. Ideally, we want the user to be able
9962 to reference this entity using either natural or linkage name,
9963 but we haven't started looking at this enhancement yet. */
9964 const char *linkage_name = dw2_linkage_name (die, cu);
9965
9966 if (linkage_name != NULL)
9967 return linkage_name;
9968 }
9969
9970 /* These are the only languages we know how to qualify names in. */
9971 if (name != NULL
9972 && (cu->language == language_cplus
9973 || cu->language == language_fortran || cu->language == language_d
9974 || cu->language == language_rust))
9975 {
9976 if (die_needs_namespace (die, cu))
9977 {
9978 const char *prefix;
9979 const char *canonical_name = NULL;
9980
9981 string_file buf;
9982
9983 prefix = determine_prefix (die, cu);
9984 if (*prefix != '\0')
9985 {
9986 gdb::unique_xmalloc_ptr<char> prefixed_name
9987 (typename_concat (NULL, prefix, name, physname, cu));
9988
9989 buf.puts (prefixed_name.get ());
9990 }
9991 else
9992 buf.puts (name);
9993
9994 /* Template parameters may be specified in the DIE's DW_AT_name, or
9995 as children with DW_TAG_template_type_param or
9996 DW_TAG_value_type_param. If the latter, add them to the name
9997 here. If the name already has template parameters, then
9998 skip this step; some versions of GCC emit both, and
9999 it is more efficient to use the pre-computed name.
10000
10001 Something to keep in mind about this process: it is very
10002 unlikely, or in some cases downright impossible, to produce
10003 something that will match the mangled name of a function.
10004 If the definition of the function has the same debug info,
10005 we should be able to match up with it anyway. But fallbacks
10006 using the minimal symbol, for instance to find a method
10007 implemented in a stripped copy of libstdc++, will not work.
10008 If we do not have debug info for the definition, we will have to
10009 match them up some other way.
10010
10011 When we do name matching there is a related problem with function
10012 templates; two instantiated function templates are allowed to
10013 differ only by their return types, which we do not add here. */
10014
10015 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10016 {
10017 struct attribute *attr;
10018 struct die_info *child;
10019 int first = 1;
10020
10021 die->building_fullname = 1;
10022
10023 for (child = die->child; child != NULL; child = child->sibling)
10024 {
10025 struct type *type;
10026 LONGEST value;
10027 const gdb_byte *bytes;
10028 struct dwarf2_locexpr_baton *baton;
10029 struct value *v;
10030
10031 if (child->tag != DW_TAG_template_type_param
10032 && child->tag != DW_TAG_template_value_param)
10033 continue;
10034
10035 if (first)
10036 {
10037 buf.puts ("<");
10038 first = 0;
10039 }
10040 else
10041 buf.puts (", ");
10042
10043 attr = dwarf2_attr (child, DW_AT_type, cu);
10044 if (attr == NULL)
10045 {
10046 complaint (_("template parameter missing DW_AT_type"));
10047 buf.puts ("UNKNOWN_TYPE");
10048 continue;
10049 }
10050 type = die_type (child, cu);
10051
10052 if (child->tag == DW_TAG_template_type_param)
10053 {
10054 c_print_type (type, "", &buf, -1, 0, cu->language,
10055 &type_print_raw_options);
10056 continue;
10057 }
10058
10059 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10060 if (attr == NULL)
10061 {
10062 complaint (_("template parameter missing "
10063 "DW_AT_const_value"));
10064 buf.puts ("UNKNOWN_VALUE");
10065 continue;
10066 }
10067
10068 dwarf2_const_value_attr (attr, type, name,
10069 &cu->comp_unit_obstack, cu,
10070 &value, &bytes, &baton);
10071
10072 if (TYPE_NOSIGN (type))
10073 /* GDB prints characters as NUMBER 'CHAR'. If that's
10074 changed, this can use value_print instead. */
10075 c_printchar (value, type, &buf);
10076 else
10077 {
10078 struct value_print_options opts;
10079
10080 if (baton != NULL)
10081 v = dwarf2_evaluate_loc_desc (type, NULL,
10082 baton->data,
10083 baton->size,
10084 baton->per_cu);
10085 else if (bytes != NULL)
10086 {
10087 v = allocate_value (type);
10088 memcpy (value_contents_writeable (v), bytes,
10089 TYPE_LENGTH (type));
10090 }
10091 else
10092 v = value_from_longest (type, value);
10093
10094 /* Specify decimal so that we do not depend on
10095 the radix. */
10096 get_formatted_print_options (&opts, 'd');
10097 opts.raw = 1;
10098 value_print (v, &buf, &opts);
10099 release_value (v);
10100 }
10101 }
10102
10103 die->building_fullname = 0;
10104
10105 if (!first)
10106 {
10107 /* Close the argument list, with a space if necessary
10108 (nested templates). */
10109 if (!buf.empty () && buf.string ().back () == '>')
10110 buf.puts (" >");
10111 else
10112 buf.puts (">");
10113 }
10114 }
10115
10116 /* For C++ methods, append formal parameter type
10117 information, if PHYSNAME. */
10118
10119 if (physname && die->tag == DW_TAG_subprogram
10120 && cu->language == language_cplus)
10121 {
10122 struct type *type = read_type_die (die, cu);
10123
10124 c_type_print_args (type, &buf, 1, cu->language,
10125 &type_print_raw_options);
10126
10127 if (cu->language == language_cplus)
10128 {
10129 /* Assume that an artificial first parameter is
10130 "this", but do not crash if it is not. RealView
10131 marks unnamed (and thus unused) parameters as
10132 artificial; there is no way to differentiate
10133 the two cases. */
10134 if (TYPE_NFIELDS (type) > 0
10135 && TYPE_FIELD_ARTIFICIAL (type, 0)
10136 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10137 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10138 0))))
10139 buf.puts (" const");
10140 }
10141 }
10142
10143 const std::string &intermediate_name = buf.string ();
10144
10145 if (cu->language == language_cplus)
10146 canonical_name
10147 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10148 objfile);
10149
10150 /* If we only computed INTERMEDIATE_NAME, or if
10151 INTERMEDIATE_NAME is already canonical, then we need to
10152 intern it. */
10153 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10154 name = objfile->intern (intermediate_name);
10155 else
10156 name = canonical_name;
10157 }
10158 }
10159
10160 return name;
10161 }
10162
10163 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10164 If scope qualifiers are appropriate they will be added. The result
10165 will be allocated on the storage_obstack, or NULL if the DIE does
10166 not have a name. NAME may either be from a previous call to
10167 dwarf2_name or NULL.
10168
10169 The output string will be canonicalized (if C++). */
10170
10171 static const char *
10172 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10173 {
10174 return dwarf2_compute_name (name, die, cu, 0);
10175 }
10176
10177 /* Construct a physname for the given DIE in CU. NAME may either be
10178 from a previous call to dwarf2_name or NULL. The result will be
10179 allocated on the objfile_objstack or NULL if the DIE does not have a
10180 name.
10181
10182 The output string will be canonicalized (if C++). */
10183
10184 static const char *
10185 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10186 {
10187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10188 const char *retval, *mangled = NULL, *canon = NULL;
10189 int need_copy = 1;
10190
10191 /* In this case dwarf2_compute_name is just a shortcut not building anything
10192 on its own. */
10193 if (!die_needs_namespace (die, cu))
10194 return dwarf2_compute_name (name, die, cu, 1);
10195
10196 mangled = dw2_linkage_name (die, cu);
10197
10198 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10199 See https://github.com/rust-lang/rust/issues/32925. */
10200 if (cu->language == language_rust && mangled != NULL
10201 && strchr (mangled, '{') != NULL)
10202 mangled = NULL;
10203
10204 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10205 has computed. */
10206 gdb::unique_xmalloc_ptr<char> demangled;
10207 if (mangled != NULL)
10208 {
10209
10210 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10211 {
10212 /* Do nothing (do not demangle the symbol name). */
10213 }
10214 else if (cu->language == language_go)
10215 {
10216 /* This is a lie, but we already lie to the caller new_symbol.
10217 new_symbol assumes we return the mangled name.
10218 This just undoes that lie until things are cleaned up. */
10219 }
10220 else
10221 {
10222 /* Use DMGL_RET_DROP for C++ template functions to suppress
10223 their return type. It is easier for GDB users to search
10224 for such functions as `name(params)' than `long name(params)'.
10225 In such case the minimal symbol names do not match the full
10226 symbol names but for template functions there is never a need
10227 to look up their definition from their declaration so
10228 the only disadvantage remains the minimal symbol variant
10229 `long name(params)' does not have the proper inferior type. */
10230 demangled.reset (gdb_demangle (mangled,
10231 (DMGL_PARAMS | DMGL_ANSI
10232 | DMGL_RET_DROP)));
10233 }
10234 if (demangled)
10235 canon = demangled.get ();
10236 else
10237 {
10238 canon = mangled;
10239 need_copy = 0;
10240 }
10241 }
10242
10243 if (canon == NULL || check_physname)
10244 {
10245 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10246
10247 if (canon != NULL && strcmp (physname, canon) != 0)
10248 {
10249 /* It may not mean a bug in GDB. The compiler could also
10250 compute DW_AT_linkage_name incorrectly. But in such case
10251 GDB would need to be bug-to-bug compatible. */
10252
10253 complaint (_("Computed physname <%s> does not match demangled <%s> "
10254 "(from linkage <%s>) - DIE at %s [in module %s]"),
10255 physname, canon, mangled, sect_offset_str (die->sect_off),
10256 objfile_name (objfile));
10257
10258 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10259 is available here - over computed PHYSNAME. It is safer
10260 against both buggy GDB and buggy compilers. */
10261
10262 retval = canon;
10263 }
10264 else
10265 {
10266 retval = physname;
10267 need_copy = 0;
10268 }
10269 }
10270 else
10271 retval = canon;
10272
10273 if (need_copy)
10274 retval = objfile->intern (retval);
10275
10276 return retval;
10277 }
10278
10279 /* Inspect DIE in CU for a namespace alias. If one exists, record
10280 a new symbol for it.
10281
10282 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10283
10284 static int
10285 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10286 {
10287 struct attribute *attr;
10288
10289 /* If the die does not have a name, this is not a namespace
10290 alias. */
10291 attr = dwarf2_attr (die, DW_AT_name, cu);
10292 if (attr != NULL)
10293 {
10294 int num;
10295 struct die_info *d = die;
10296 struct dwarf2_cu *imported_cu = cu;
10297
10298 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10299 keep inspecting DIEs until we hit the underlying import. */
10300 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10301 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10302 {
10303 attr = dwarf2_attr (d, DW_AT_import, cu);
10304 if (attr == NULL)
10305 break;
10306
10307 d = follow_die_ref (d, attr, &imported_cu);
10308 if (d->tag != DW_TAG_imported_declaration)
10309 break;
10310 }
10311
10312 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10313 {
10314 complaint (_("DIE at %s has too many recursively imported "
10315 "declarations"), sect_offset_str (d->sect_off));
10316 return 0;
10317 }
10318
10319 if (attr != NULL)
10320 {
10321 struct type *type;
10322 sect_offset sect_off = attr->get_ref_die_offset ();
10323
10324 type = get_die_type_at_offset (sect_off, cu->per_cu);
10325 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10326 {
10327 /* This declaration is a global namespace alias. Add
10328 a symbol for it whose type is the aliased namespace. */
10329 new_symbol (die, type, cu);
10330 return 1;
10331 }
10332 }
10333 }
10334
10335 return 0;
10336 }
10337
10338 /* Return the using directives repository (global or local?) to use in the
10339 current context for CU.
10340
10341 For Ada, imported declarations can materialize renamings, which *may* be
10342 global. However it is impossible (for now?) in DWARF to distinguish
10343 "external" imported declarations and "static" ones. As all imported
10344 declarations seem to be static in all other languages, make them all CU-wide
10345 global only in Ada. */
10346
10347 static struct using_direct **
10348 using_directives (struct dwarf2_cu *cu)
10349 {
10350 if (cu->language == language_ada
10351 && cu->get_builder ()->outermost_context_p ())
10352 return cu->get_builder ()->get_global_using_directives ();
10353 else
10354 return cu->get_builder ()->get_local_using_directives ();
10355 }
10356
10357 /* Read the import statement specified by the given die and record it. */
10358
10359 static void
10360 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10361 {
10362 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10363 struct attribute *import_attr;
10364 struct die_info *imported_die, *child_die;
10365 struct dwarf2_cu *imported_cu;
10366 const char *imported_name;
10367 const char *imported_name_prefix;
10368 const char *canonical_name;
10369 const char *import_alias;
10370 const char *imported_declaration = NULL;
10371 const char *import_prefix;
10372 std::vector<const char *> excludes;
10373
10374 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10375 if (import_attr == NULL)
10376 {
10377 complaint (_("Tag '%s' has no DW_AT_import"),
10378 dwarf_tag_name (die->tag));
10379 return;
10380 }
10381
10382 imported_cu = cu;
10383 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10384 imported_name = dwarf2_name (imported_die, imported_cu);
10385 if (imported_name == NULL)
10386 {
10387 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10388
10389 The import in the following code:
10390 namespace A
10391 {
10392 typedef int B;
10393 }
10394
10395 int main ()
10396 {
10397 using A::B;
10398 B b;
10399 return b;
10400 }
10401
10402 ...
10403 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10404 <52> DW_AT_decl_file : 1
10405 <53> DW_AT_decl_line : 6
10406 <54> DW_AT_import : <0x75>
10407 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10408 <59> DW_AT_name : B
10409 <5b> DW_AT_decl_file : 1
10410 <5c> DW_AT_decl_line : 2
10411 <5d> DW_AT_type : <0x6e>
10412 ...
10413 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10414 <76> DW_AT_byte_size : 4
10415 <77> DW_AT_encoding : 5 (signed)
10416
10417 imports the wrong die ( 0x75 instead of 0x58 ).
10418 This case will be ignored until the gcc bug is fixed. */
10419 return;
10420 }
10421
10422 /* Figure out the local name after import. */
10423 import_alias = dwarf2_name (die, cu);
10424
10425 /* Figure out where the statement is being imported to. */
10426 import_prefix = determine_prefix (die, cu);
10427
10428 /* Figure out what the scope of the imported die is and prepend it
10429 to the name of the imported die. */
10430 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10431
10432 if (imported_die->tag != DW_TAG_namespace
10433 && imported_die->tag != DW_TAG_module)
10434 {
10435 imported_declaration = imported_name;
10436 canonical_name = imported_name_prefix;
10437 }
10438 else if (strlen (imported_name_prefix) > 0)
10439 canonical_name = obconcat (&objfile->objfile_obstack,
10440 imported_name_prefix,
10441 (cu->language == language_d ? "." : "::"),
10442 imported_name, (char *) NULL);
10443 else
10444 canonical_name = imported_name;
10445
10446 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10447 for (child_die = die->child; child_die && child_die->tag;
10448 child_die = child_die->sibling)
10449 {
10450 /* DWARF-4: A Fortran use statement with a “rename list” may be
10451 represented by an imported module entry with an import attribute
10452 referring to the module and owned entries corresponding to those
10453 entities that are renamed as part of being imported. */
10454
10455 if (child_die->tag != DW_TAG_imported_declaration)
10456 {
10457 complaint (_("child DW_TAG_imported_declaration expected "
10458 "- DIE at %s [in module %s]"),
10459 sect_offset_str (child_die->sect_off),
10460 objfile_name (objfile));
10461 continue;
10462 }
10463
10464 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10465 if (import_attr == NULL)
10466 {
10467 complaint (_("Tag '%s' has no DW_AT_import"),
10468 dwarf_tag_name (child_die->tag));
10469 continue;
10470 }
10471
10472 imported_cu = cu;
10473 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10474 &imported_cu);
10475 imported_name = dwarf2_name (imported_die, imported_cu);
10476 if (imported_name == NULL)
10477 {
10478 complaint (_("child DW_TAG_imported_declaration has unknown "
10479 "imported name - DIE at %s [in module %s]"),
10480 sect_offset_str (child_die->sect_off),
10481 objfile_name (objfile));
10482 continue;
10483 }
10484
10485 excludes.push_back (imported_name);
10486
10487 process_die (child_die, cu);
10488 }
10489
10490 add_using_directive (using_directives (cu),
10491 import_prefix,
10492 canonical_name,
10493 import_alias,
10494 imported_declaration,
10495 excludes,
10496 0,
10497 &objfile->objfile_obstack);
10498 }
10499
10500 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10501 types, but gives them a size of zero. Starting with version 14,
10502 ICC is compatible with GCC. */
10503
10504 static bool
10505 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10506 {
10507 if (!cu->checked_producer)
10508 check_producer (cu);
10509
10510 return cu->producer_is_icc_lt_14;
10511 }
10512
10513 /* ICC generates a DW_AT_type for C void functions. This was observed on
10514 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10515 which says that void functions should not have a DW_AT_type. */
10516
10517 static bool
10518 producer_is_icc (struct dwarf2_cu *cu)
10519 {
10520 if (!cu->checked_producer)
10521 check_producer (cu);
10522
10523 return cu->producer_is_icc;
10524 }
10525
10526 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10527 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10528 this, it was first present in GCC release 4.3.0. */
10529
10530 static bool
10531 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10532 {
10533 if (!cu->checked_producer)
10534 check_producer (cu);
10535
10536 return cu->producer_is_gcc_lt_4_3;
10537 }
10538
10539 static file_and_directory
10540 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10541 {
10542 file_and_directory res;
10543
10544 /* Find the filename. Do not use dwarf2_name here, since the filename
10545 is not a source language identifier. */
10546 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10547 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10548
10549 if (res.comp_dir == NULL
10550 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10551 && IS_ABSOLUTE_PATH (res.name))
10552 {
10553 res.comp_dir_storage = ldirname (res.name);
10554 if (!res.comp_dir_storage.empty ())
10555 res.comp_dir = res.comp_dir_storage.c_str ();
10556 }
10557 if (res.comp_dir != NULL)
10558 {
10559 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10560 directory, get rid of it. */
10561 const char *cp = strchr (res.comp_dir, ':');
10562
10563 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10564 res.comp_dir = cp + 1;
10565 }
10566
10567 if (res.name == NULL)
10568 res.name = "<unknown>";
10569
10570 return res;
10571 }
10572
10573 /* Handle DW_AT_stmt_list for a compilation unit.
10574 DIE is the DW_TAG_compile_unit die for CU.
10575 COMP_DIR is the compilation directory. LOWPC is passed to
10576 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10577
10578 static void
10579 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10580 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10581 {
10582 struct dwarf2_per_objfile *dwarf2_per_objfile
10583 = cu->per_cu->dwarf2_per_objfile;
10584 struct attribute *attr;
10585 struct line_header line_header_local;
10586 hashval_t line_header_local_hash;
10587 void **slot;
10588 int decode_mapping;
10589
10590 gdb_assert (! cu->per_cu->is_debug_types);
10591
10592 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10593 if (attr == NULL)
10594 return;
10595
10596 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10597
10598 /* The line header hash table is only created if needed (it exists to
10599 prevent redundant reading of the line table for partial_units).
10600 If we're given a partial_unit, we'll need it. If we're given a
10601 compile_unit, then use the line header hash table if it's already
10602 created, but don't create one just yet. */
10603
10604 if (dwarf2_per_objfile->line_header_hash == NULL
10605 && die->tag == DW_TAG_partial_unit)
10606 {
10607 dwarf2_per_objfile->line_header_hash
10608 .reset (htab_create_alloc (127, line_header_hash_voidp,
10609 line_header_eq_voidp,
10610 free_line_header_voidp,
10611 xcalloc, xfree));
10612 }
10613
10614 line_header_local.sect_off = line_offset;
10615 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10616 line_header_local_hash = line_header_hash (&line_header_local);
10617 if (dwarf2_per_objfile->line_header_hash != NULL)
10618 {
10619 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10620 &line_header_local,
10621 line_header_local_hash, NO_INSERT);
10622
10623 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10624 is not present in *SLOT (since if there is something in *SLOT then
10625 it will be for a partial_unit). */
10626 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10627 {
10628 gdb_assert (*slot != NULL);
10629 cu->line_header = (struct line_header *) *slot;
10630 return;
10631 }
10632 }
10633
10634 /* dwarf_decode_line_header does not yet provide sufficient information.
10635 We always have to call also dwarf_decode_lines for it. */
10636 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10637 if (lh == NULL)
10638 return;
10639
10640 cu->line_header = lh.release ();
10641 cu->line_header_die_owner = die;
10642
10643 if (dwarf2_per_objfile->line_header_hash == NULL)
10644 slot = NULL;
10645 else
10646 {
10647 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10648 &line_header_local,
10649 line_header_local_hash, INSERT);
10650 gdb_assert (slot != NULL);
10651 }
10652 if (slot != NULL && *slot == NULL)
10653 {
10654 /* This newly decoded line number information unit will be owned
10655 by line_header_hash hash table. */
10656 *slot = cu->line_header;
10657 cu->line_header_die_owner = NULL;
10658 }
10659 else
10660 {
10661 /* We cannot free any current entry in (*slot) as that struct line_header
10662 may be already used by multiple CUs. Create only temporary decoded
10663 line_header for this CU - it may happen at most once for each line
10664 number information unit. And if we're not using line_header_hash
10665 then this is what we want as well. */
10666 gdb_assert (die->tag != DW_TAG_partial_unit);
10667 }
10668 decode_mapping = (die->tag != DW_TAG_partial_unit);
10669 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10670 decode_mapping);
10671
10672 }
10673
10674 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10675
10676 static void
10677 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10678 {
10679 struct dwarf2_per_objfile *dwarf2_per_objfile
10680 = cu->per_cu->dwarf2_per_objfile;
10681 struct objfile *objfile = dwarf2_per_objfile->objfile;
10682 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10683 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10684 CORE_ADDR highpc = ((CORE_ADDR) 0);
10685 struct attribute *attr;
10686 struct die_info *child_die;
10687 CORE_ADDR baseaddr;
10688
10689 prepare_one_comp_unit (cu, die, cu->language);
10690 baseaddr = objfile->text_section_offset ();
10691
10692 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10693
10694 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10695 from finish_block. */
10696 if (lowpc == ((CORE_ADDR) -1))
10697 lowpc = highpc;
10698 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10699
10700 file_and_directory fnd = find_file_and_directory (die, cu);
10701
10702 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10703 standardised yet. As a workaround for the language detection we fall
10704 back to the DW_AT_producer string. */
10705 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10706 cu->language = language_opencl;
10707
10708 /* Similar hack for Go. */
10709 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10710 set_cu_language (DW_LANG_Go, cu);
10711
10712 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10713
10714 /* Decode line number information if present. We do this before
10715 processing child DIEs, so that the line header table is available
10716 for DW_AT_decl_file. */
10717 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10718
10719 /* Process all dies in compilation unit. */
10720 if (die->child != NULL)
10721 {
10722 child_die = die->child;
10723 while (child_die && child_die->tag)
10724 {
10725 process_die (child_die, cu);
10726 child_die = child_die->sibling;
10727 }
10728 }
10729
10730 /* Decode macro information, if present. Dwarf 2 macro information
10731 refers to information in the line number info statement program
10732 header, so we can only read it if we've read the header
10733 successfully. */
10734 attr = dwarf2_attr (die, DW_AT_macros, cu);
10735 if (attr == NULL)
10736 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10737 if (attr && cu->line_header)
10738 {
10739 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10740 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10741
10742 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10743 }
10744 else
10745 {
10746 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10747 if (attr && cu->line_header)
10748 {
10749 unsigned int macro_offset = DW_UNSND (attr);
10750
10751 dwarf_decode_macros (cu, macro_offset, 0);
10752 }
10753 }
10754 }
10755
10756 void
10757 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10758 {
10759 struct type_unit_group *tu_group;
10760 int first_time;
10761 struct attribute *attr;
10762 unsigned int i;
10763 struct signatured_type *sig_type;
10764
10765 gdb_assert (per_cu->is_debug_types);
10766 sig_type = (struct signatured_type *) per_cu;
10767
10768 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10769
10770 /* If we're using .gdb_index (includes -readnow) then
10771 per_cu->type_unit_group may not have been set up yet. */
10772 if (sig_type->type_unit_group == NULL)
10773 sig_type->type_unit_group = get_type_unit_group (this, attr);
10774 tu_group = sig_type->type_unit_group;
10775
10776 /* If we've already processed this stmt_list there's no real need to
10777 do it again, we could fake it and just recreate the part we need
10778 (file name,index -> symtab mapping). If data shows this optimization
10779 is useful we can do it then. */
10780 first_time = tu_group->compunit_symtab == NULL;
10781
10782 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10783 debug info. */
10784 line_header_up lh;
10785 if (attr != NULL)
10786 {
10787 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10788 lh = dwarf_decode_line_header (line_offset, this);
10789 }
10790 if (lh == NULL)
10791 {
10792 if (first_time)
10793 start_symtab ("", NULL, 0);
10794 else
10795 {
10796 gdb_assert (tu_group->symtabs == NULL);
10797 gdb_assert (m_builder == nullptr);
10798 struct compunit_symtab *cust = tu_group->compunit_symtab;
10799 m_builder.reset (new struct buildsym_compunit
10800 (COMPUNIT_OBJFILE (cust), "",
10801 COMPUNIT_DIRNAME (cust),
10802 compunit_language (cust),
10803 0, cust));
10804 }
10805 return;
10806 }
10807
10808 line_header = lh.release ();
10809 line_header_die_owner = die;
10810
10811 if (first_time)
10812 {
10813 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10814
10815 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10816 still initializing it, and our caller (a few levels up)
10817 process_full_type_unit still needs to know if this is the first
10818 time. */
10819
10820 tu_group->symtabs
10821 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10822 struct symtab *, line_header->file_names_size ());
10823
10824 auto &file_names = line_header->file_names ();
10825 for (i = 0; i < file_names.size (); ++i)
10826 {
10827 file_entry &fe = file_names[i];
10828 dwarf2_start_subfile (this, fe.name,
10829 fe.include_dir (line_header));
10830 buildsym_compunit *b = get_builder ();
10831 if (b->get_current_subfile ()->symtab == NULL)
10832 {
10833 /* NOTE: start_subfile will recognize when it's been
10834 passed a file it has already seen. So we can't
10835 assume there's a simple mapping from
10836 cu->line_header->file_names to subfiles, plus
10837 cu->line_header->file_names may contain dups. */
10838 b->get_current_subfile ()->symtab
10839 = allocate_symtab (cust, b->get_current_subfile ()->name);
10840 }
10841
10842 fe.symtab = b->get_current_subfile ()->symtab;
10843 tu_group->symtabs[i] = fe.symtab;
10844 }
10845 }
10846 else
10847 {
10848 gdb_assert (m_builder == nullptr);
10849 struct compunit_symtab *cust = tu_group->compunit_symtab;
10850 m_builder.reset (new struct buildsym_compunit
10851 (COMPUNIT_OBJFILE (cust), "",
10852 COMPUNIT_DIRNAME (cust),
10853 compunit_language (cust),
10854 0, cust));
10855
10856 auto &file_names = line_header->file_names ();
10857 for (i = 0; i < file_names.size (); ++i)
10858 {
10859 file_entry &fe = file_names[i];
10860 fe.symtab = tu_group->symtabs[i];
10861 }
10862 }
10863
10864 /* The main symtab is allocated last. Type units don't have DW_AT_name
10865 so they don't have a "real" (so to speak) symtab anyway.
10866 There is later code that will assign the main symtab to all symbols
10867 that don't have one. We need to handle the case of a symbol with a
10868 missing symtab (DW_AT_decl_file) anyway. */
10869 }
10870
10871 /* Process DW_TAG_type_unit.
10872 For TUs we want to skip the first top level sibling if it's not the
10873 actual type being defined by this TU. In this case the first top
10874 level sibling is there to provide context only. */
10875
10876 static void
10877 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10878 {
10879 struct die_info *child_die;
10880
10881 prepare_one_comp_unit (cu, die, language_minimal);
10882
10883 /* Initialize (or reinitialize) the machinery for building symtabs.
10884 We do this before processing child DIEs, so that the line header table
10885 is available for DW_AT_decl_file. */
10886 cu->setup_type_unit_groups (die);
10887
10888 if (die->child != NULL)
10889 {
10890 child_die = die->child;
10891 while (child_die && child_die->tag)
10892 {
10893 process_die (child_die, cu);
10894 child_die = child_die->sibling;
10895 }
10896 }
10897 }
10898 \f
10899 /* DWO/DWP files.
10900
10901 http://gcc.gnu.org/wiki/DebugFission
10902 http://gcc.gnu.org/wiki/DebugFissionDWP
10903
10904 To simplify handling of both DWO files ("object" files with the DWARF info)
10905 and DWP files (a file with the DWOs packaged up into one file), we treat
10906 DWP files as having a collection of virtual DWO files. */
10907
10908 static hashval_t
10909 hash_dwo_file (const void *item)
10910 {
10911 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10912 hashval_t hash;
10913
10914 hash = htab_hash_string (dwo_file->dwo_name);
10915 if (dwo_file->comp_dir != NULL)
10916 hash += htab_hash_string (dwo_file->comp_dir);
10917 return hash;
10918 }
10919
10920 static int
10921 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10922 {
10923 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10924 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10925
10926 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10927 return 0;
10928 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10929 return lhs->comp_dir == rhs->comp_dir;
10930 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10931 }
10932
10933 /* Allocate a hash table for DWO files. */
10934
10935 static htab_up
10936 allocate_dwo_file_hash_table ()
10937 {
10938 auto delete_dwo_file = [] (void *item)
10939 {
10940 struct dwo_file *dwo_file = (struct dwo_file *) item;
10941
10942 delete dwo_file;
10943 };
10944
10945 return htab_up (htab_create_alloc (41,
10946 hash_dwo_file,
10947 eq_dwo_file,
10948 delete_dwo_file,
10949 xcalloc, xfree));
10950 }
10951
10952 /* Lookup DWO file DWO_NAME. */
10953
10954 static void **
10955 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10956 const char *dwo_name,
10957 const char *comp_dir)
10958 {
10959 struct dwo_file find_entry;
10960 void **slot;
10961
10962 if (dwarf2_per_objfile->dwo_files == NULL)
10963 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10964
10965 find_entry.dwo_name = dwo_name;
10966 find_entry.comp_dir = comp_dir;
10967 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10968 INSERT);
10969
10970 return slot;
10971 }
10972
10973 static hashval_t
10974 hash_dwo_unit (const void *item)
10975 {
10976 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10977
10978 /* This drops the top 32 bits of the id, but is ok for a hash. */
10979 return dwo_unit->signature;
10980 }
10981
10982 static int
10983 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10984 {
10985 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10986 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10987
10988 /* The signature is assumed to be unique within the DWO file.
10989 So while object file CU dwo_id's always have the value zero,
10990 that's OK, assuming each object file DWO file has only one CU,
10991 and that's the rule for now. */
10992 return lhs->signature == rhs->signature;
10993 }
10994
10995 /* Allocate a hash table for DWO CUs,TUs.
10996 There is one of these tables for each of CUs,TUs for each DWO file. */
10997
10998 static htab_up
10999 allocate_dwo_unit_table ()
11000 {
11001 /* Start out with a pretty small number.
11002 Generally DWO files contain only one CU and maybe some TUs. */
11003 return htab_up (htab_create_alloc (3,
11004 hash_dwo_unit,
11005 eq_dwo_unit,
11006 NULL, xcalloc, xfree));
11007 }
11008
11009 /* die_reader_func for create_dwo_cu. */
11010
11011 static void
11012 create_dwo_cu_reader (const struct die_reader_specs *reader,
11013 const gdb_byte *info_ptr,
11014 struct die_info *comp_unit_die,
11015 struct dwo_file *dwo_file,
11016 struct dwo_unit *dwo_unit)
11017 {
11018 struct dwarf2_cu *cu = reader->cu;
11019 sect_offset sect_off = cu->per_cu->sect_off;
11020 struct dwarf2_section_info *section = cu->per_cu->section;
11021
11022 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11023 if (!signature.has_value ())
11024 {
11025 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11026 " its dwo_id [in module %s]"),
11027 sect_offset_str (sect_off), dwo_file->dwo_name);
11028 return;
11029 }
11030
11031 dwo_unit->dwo_file = dwo_file;
11032 dwo_unit->signature = *signature;
11033 dwo_unit->section = section;
11034 dwo_unit->sect_off = sect_off;
11035 dwo_unit->length = cu->per_cu->length;
11036
11037 if (dwarf_read_debug)
11038 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11039 sect_offset_str (sect_off),
11040 hex_string (dwo_unit->signature));
11041 }
11042
11043 /* Create the dwo_units for the CUs in a DWO_FILE.
11044 Note: This function processes DWO files only, not DWP files. */
11045
11046 static void
11047 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11048 dwarf2_cu *cu, struct dwo_file &dwo_file,
11049 dwarf2_section_info &section, htab_up &cus_htab)
11050 {
11051 struct objfile *objfile = dwarf2_per_objfile->objfile;
11052 const gdb_byte *info_ptr, *end_ptr;
11053
11054 section.read (objfile);
11055 info_ptr = section.buffer;
11056
11057 if (info_ptr == NULL)
11058 return;
11059
11060 if (dwarf_read_debug)
11061 {
11062 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11063 section.get_name (),
11064 section.get_file_name ());
11065 }
11066
11067 end_ptr = info_ptr + section.size;
11068 while (info_ptr < end_ptr)
11069 {
11070 struct dwarf2_per_cu_data per_cu;
11071 struct dwo_unit read_unit {};
11072 struct dwo_unit *dwo_unit;
11073 void **slot;
11074 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11075
11076 memset (&per_cu, 0, sizeof (per_cu));
11077 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11078 per_cu.is_debug_types = 0;
11079 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11080 per_cu.section = &section;
11081
11082 cutu_reader reader (&per_cu, cu, &dwo_file);
11083 if (!reader.dummy_p)
11084 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11085 &dwo_file, &read_unit);
11086 info_ptr += per_cu.length;
11087
11088 // If the unit could not be parsed, skip it.
11089 if (read_unit.dwo_file == NULL)
11090 continue;
11091
11092 if (cus_htab == NULL)
11093 cus_htab = allocate_dwo_unit_table ();
11094
11095 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11096 *dwo_unit = read_unit;
11097 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11098 gdb_assert (slot != NULL);
11099 if (*slot != NULL)
11100 {
11101 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11102 sect_offset dup_sect_off = dup_cu->sect_off;
11103
11104 complaint (_("debug cu entry at offset %s is duplicate to"
11105 " the entry at offset %s, signature %s"),
11106 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11107 hex_string (dwo_unit->signature));
11108 }
11109 *slot = (void *)dwo_unit;
11110 }
11111 }
11112
11113 /* DWP file .debug_{cu,tu}_index section format:
11114 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11115
11116 DWP Version 1:
11117
11118 Both index sections have the same format, and serve to map a 64-bit
11119 signature to a set of section numbers. Each section begins with a header,
11120 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11121 indexes, and a pool of 32-bit section numbers. The index sections will be
11122 aligned at 8-byte boundaries in the file.
11123
11124 The index section header consists of:
11125
11126 V, 32 bit version number
11127 -, 32 bits unused
11128 N, 32 bit number of compilation units or type units in the index
11129 M, 32 bit number of slots in the hash table
11130
11131 Numbers are recorded using the byte order of the application binary.
11132
11133 The hash table begins at offset 16 in the section, and consists of an array
11134 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11135 order of the application binary). Unused slots in the hash table are 0.
11136 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11137
11138 The parallel table begins immediately after the hash table
11139 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11140 array of 32-bit indexes (using the byte order of the application binary),
11141 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11142 table contains a 32-bit index into the pool of section numbers. For unused
11143 hash table slots, the corresponding entry in the parallel table will be 0.
11144
11145 The pool of section numbers begins immediately following the hash table
11146 (at offset 16 + 12 * M from the beginning of the section). The pool of
11147 section numbers consists of an array of 32-bit words (using the byte order
11148 of the application binary). Each item in the array is indexed starting
11149 from 0. The hash table entry provides the index of the first section
11150 number in the set. Additional section numbers in the set follow, and the
11151 set is terminated by a 0 entry (section number 0 is not used in ELF).
11152
11153 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11154 section must be the first entry in the set, and the .debug_abbrev.dwo must
11155 be the second entry. Other members of the set may follow in any order.
11156
11157 ---
11158
11159 DWP Version 2:
11160
11161 DWP Version 2 combines all the .debug_info, etc. sections into one,
11162 and the entries in the index tables are now offsets into these sections.
11163 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11164 section.
11165
11166 Index Section Contents:
11167 Header
11168 Hash Table of Signatures dwp_hash_table.hash_table
11169 Parallel Table of Indices dwp_hash_table.unit_table
11170 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11171 Table of Section Sizes dwp_hash_table.v2.sizes
11172
11173 The index section header consists of:
11174
11175 V, 32 bit version number
11176 L, 32 bit number of columns in the table of section offsets
11177 N, 32 bit number of compilation units or type units in the index
11178 M, 32 bit number of slots in the hash table
11179
11180 Numbers are recorded using the byte order of the application binary.
11181
11182 The hash table has the same format as version 1.
11183 The parallel table of indices has the same format as version 1,
11184 except that the entries are origin-1 indices into the table of sections
11185 offsets and the table of section sizes.
11186
11187 The table of offsets begins immediately following the parallel table
11188 (at offset 16 + 12 * M from the beginning of the section). The table is
11189 a two-dimensional array of 32-bit words (using the byte order of the
11190 application binary), with L columns and N+1 rows, in row-major order.
11191 Each row in the array is indexed starting from 0. The first row provides
11192 a key to the remaining rows: each column in this row provides an identifier
11193 for a debug section, and the offsets in the same column of subsequent rows
11194 refer to that section. The section identifiers are:
11195
11196 DW_SECT_INFO 1 .debug_info.dwo
11197 DW_SECT_TYPES 2 .debug_types.dwo
11198 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11199 DW_SECT_LINE 4 .debug_line.dwo
11200 DW_SECT_LOC 5 .debug_loc.dwo
11201 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11202 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11203 DW_SECT_MACRO 8 .debug_macro.dwo
11204
11205 The offsets provided by the CU and TU index sections are the base offsets
11206 for the contributions made by each CU or TU to the corresponding section
11207 in the package file. Each CU and TU header contains an abbrev_offset
11208 field, used to find the abbreviations table for that CU or TU within the
11209 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11210 be interpreted as relative to the base offset given in the index section.
11211 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11212 should be interpreted as relative to the base offset for .debug_line.dwo,
11213 and offsets into other debug sections obtained from DWARF attributes should
11214 also be interpreted as relative to the corresponding base offset.
11215
11216 The table of sizes begins immediately following the table of offsets.
11217 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11218 with L columns and N rows, in row-major order. Each row in the array is
11219 indexed starting from 1 (row 0 is shared by the two tables).
11220
11221 ---
11222
11223 Hash table lookup is handled the same in version 1 and 2:
11224
11225 We assume that N and M will not exceed 2^32 - 1.
11226 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11227
11228 Given a 64-bit compilation unit signature or a type signature S, an entry
11229 in the hash table is located as follows:
11230
11231 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11232 the low-order k bits all set to 1.
11233
11234 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11235
11236 3) If the hash table entry at index H matches the signature, use that
11237 entry. If the hash table entry at index H is unused (all zeroes),
11238 terminate the search: the signature is not present in the table.
11239
11240 4) Let H = (H + H') modulo M. Repeat at Step 3.
11241
11242 Because M > N and H' and M are relatively prime, the search is guaranteed
11243 to stop at an unused slot or find the match. */
11244
11245 /* Create a hash table to map DWO IDs to their CU/TU entry in
11246 .debug_{info,types}.dwo in DWP_FILE.
11247 Returns NULL if there isn't one.
11248 Note: This function processes DWP files only, not DWO files. */
11249
11250 static struct dwp_hash_table *
11251 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11252 struct dwp_file *dwp_file, int is_debug_types)
11253 {
11254 struct objfile *objfile = dwarf2_per_objfile->objfile;
11255 bfd *dbfd = dwp_file->dbfd.get ();
11256 const gdb_byte *index_ptr, *index_end;
11257 struct dwarf2_section_info *index;
11258 uint32_t version, nr_columns, nr_units, nr_slots;
11259 struct dwp_hash_table *htab;
11260
11261 if (is_debug_types)
11262 index = &dwp_file->sections.tu_index;
11263 else
11264 index = &dwp_file->sections.cu_index;
11265
11266 if (index->empty ())
11267 return NULL;
11268 index->read (objfile);
11269
11270 index_ptr = index->buffer;
11271 index_end = index_ptr + index->size;
11272
11273 version = read_4_bytes (dbfd, index_ptr);
11274 index_ptr += 4;
11275 if (version == 2)
11276 nr_columns = read_4_bytes (dbfd, index_ptr);
11277 else
11278 nr_columns = 0;
11279 index_ptr += 4;
11280 nr_units = read_4_bytes (dbfd, index_ptr);
11281 index_ptr += 4;
11282 nr_slots = read_4_bytes (dbfd, index_ptr);
11283 index_ptr += 4;
11284
11285 if (version != 1 && version != 2)
11286 {
11287 error (_("Dwarf Error: unsupported DWP file version (%s)"
11288 " [in module %s]"),
11289 pulongest (version), dwp_file->name);
11290 }
11291 if (nr_slots != (nr_slots & -nr_slots))
11292 {
11293 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11294 " is not power of 2 [in module %s]"),
11295 pulongest (nr_slots), dwp_file->name);
11296 }
11297
11298 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11299 htab->version = version;
11300 htab->nr_columns = nr_columns;
11301 htab->nr_units = nr_units;
11302 htab->nr_slots = nr_slots;
11303 htab->hash_table = index_ptr;
11304 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11305
11306 /* Exit early if the table is empty. */
11307 if (nr_slots == 0 || nr_units == 0
11308 || (version == 2 && nr_columns == 0))
11309 {
11310 /* All must be zero. */
11311 if (nr_slots != 0 || nr_units != 0
11312 || (version == 2 && nr_columns != 0))
11313 {
11314 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11315 " all zero [in modules %s]"),
11316 dwp_file->name);
11317 }
11318 return htab;
11319 }
11320
11321 if (version == 1)
11322 {
11323 htab->section_pool.v1.indices =
11324 htab->unit_table + sizeof (uint32_t) * nr_slots;
11325 /* It's harder to decide whether the section is too small in v1.
11326 V1 is deprecated anyway so we punt. */
11327 }
11328 else
11329 {
11330 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11331 int *ids = htab->section_pool.v2.section_ids;
11332 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11333 /* Reverse map for error checking. */
11334 int ids_seen[DW_SECT_MAX + 1];
11335 int i;
11336
11337 if (nr_columns < 2)
11338 {
11339 error (_("Dwarf Error: bad DWP hash table, too few columns"
11340 " in section table [in module %s]"),
11341 dwp_file->name);
11342 }
11343 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11344 {
11345 error (_("Dwarf Error: bad DWP hash table, too many columns"
11346 " in section table [in module %s]"),
11347 dwp_file->name);
11348 }
11349 memset (ids, 255, sizeof_ids);
11350 memset (ids_seen, 255, sizeof (ids_seen));
11351 for (i = 0; i < nr_columns; ++i)
11352 {
11353 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11354
11355 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11356 {
11357 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11358 " in section table [in module %s]"),
11359 id, dwp_file->name);
11360 }
11361 if (ids_seen[id] != -1)
11362 {
11363 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11364 " id %d in section table [in module %s]"),
11365 id, dwp_file->name);
11366 }
11367 ids_seen[id] = i;
11368 ids[i] = id;
11369 }
11370 /* Must have exactly one info or types section. */
11371 if (((ids_seen[DW_SECT_INFO] != -1)
11372 + (ids_seen[DW_SECT_TYPES] != -1))
11373 != 1)
11374 {
11375 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11376 " DWO info/types section [in module %s]"),
11377 dwp_file->name);
11378 }
11379 /* Must have an abbrev section. */
11380 if (ids_seen[DW_SECT_ABBREV] == -1)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11383 " section [in module %s]"),
11384 dwp_file->name);
11385 }
11386 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11387 htab->section_pool.v2.sizes =
11388 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11389 * nr_units * nr_columns);
11390 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11391 * nr_units * nr_columns))
11392 > index_end)
11393 {
11394 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11395 " [in module %s]"),
11396 dwp_file->name);
11397 }
11398 }
11399
11400 return htab;
11401 }
11402
11403 /* Update SECTIONS with the data from SECTP.
11404
11405 This function is like the other "locate" section routines that are
11406 passed to bfd_map_over_sections, but in this context the sections to
11407 read comes from the DWP V1 hash table, not the full ELF section table.
11408
11409 The result is non-zero for success, or zero if an error was found. */
11410
11411 static int
11412 locate_v1_virtual_dwo_sections (asection *sectp,
11413 struct virtual_v1_dwo_sections *sections)
11414 {
11415 const struct dwop_section_names *names = &dwop_section_names;
11416
11417 if (section_is_p (sectp->name, &names->abbrev_dwo))
11418 {
11419 /* There can be only one. */
11420 if (sections->abbrev.s.section != NULL)
11421 return 0;
11422 sections->abbrev.s.section = sectp;
11423 sections->abbrev.size = bfd_section_size (sectp);
11424 }
11425 else if (section_is_p (sectp->name, &names->info_dwo)
11426 || section_is_p (sectp->name, &names->types_dwo))
11427 {
11428 /* There can be only one. */
11429 if (sections->info_or_types.s.section != NULL)
11430 return 0;
11431 sections->info_or_types.s.section = sectp;
11432 sections->info_or_types.size = bfd_section_size (sectp);
11433 }
11434 else if (section_is_p (sectp->name, &names->line_dwo))
11435 {
11436 /* There can be only one. */
11437 if (sections->line.s.section != NULL)
11438 return 0;
11439 sections->line.s.section = sectp;
11440 sections->line.size = bfd_section_size (sectp);
11441 }
11442 else if (section_is_p (sectp->name, &names->loc_dwo))
11443 {
11444 /* There can be only one. */
11445 if (sections->loc.s.section != NULL)
11446 return 0;
11447 sections->loc.s.section = sectp;
11448 sections->loc.size = bfd_section_size (sectp);
11449 }
11450 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11451 {
11452 /* There can be only one. */
11453 if (sections->macinfo.s.section != NULL)
11454 return 0;
11455 sections->macinfo.s.section = sectp;
11456 sections->macinfo.size = bfd_section_size (sectp);
11457 }
11458 else if (section_is_p (sectp->name, &names->macro_dwo))
11459 {
11460 /* There can be only one. */
11461 if (sections->macro.s.section != NULL)
11462 return 0;
11463 sections->macro.s.section = sectp;
11464 sections->macro.size = bfd_section_size (sectp);
11465 }
11466 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11467 {
11468 /* There can be only one. */
11469 if (sections->str_offsets.s.section != NULL)
11470 return 0;
11471 sections->str_offsets.s.section = sectp;
11472 sections->str_offsets.size = bfd_section_size (sectp);
11473 }
11474 else
11475 {
11476 /* No other kind of section is valid. */
11477 return 0;
11478 }
11479
11480 return 1;
11481 }
11482
11483 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11484 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11485 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11486 This is for DWP version 1 files. */
11487
11488 static struct dwo_unit *
11489 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11490 struct dwp_file *dwp_file,
11491 uint32_t unit_index,
11492 const char *comp_dir,
11493 ULONGEST signature, int is_debug_types)
11494 {
11495 struct objfile *objfile = dwarf2_per_objfile->objfile;
11496 const struct dwp_hash_table *dwp_htab =
11497 is_debug_types ? dwp_file->tus : dwp_file->cus;
11498 bfd *dbfd = dwp_file->dbfd.get ();
11499 const char *kind = is_debug_types ? "TU" : "CU";
11500 struct dwo_file *dwo_file;
11501 struct dwo_unit *dwo_unit;
11502 struct virtual_v1_dwo_sections sections;
11503 void **dwo_file_slot;
11504 int i;
11505
11506 gdb_assert (dwp_file->version == 1);
11507
11508 if (dwarf_read_debug)
11509 {
11510 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11511 kind,
11512 pulongest (unit_index), hex_string (signature),
11513 dwp_file->name);
11514 }
11515
11516 /* Fetch the sections of this DWO unit.
11517 Put a limit on the number of sections we look for so that bad data
11518 doesn't cause us to loop forever. */
11519
11520 #define MAX_NR_V1_DWO_SECTIONS \
11521 (1 /* .debug_info or .debug_types */ \
11522 + 1 /* .debug_abbrev */ \
11523 + 1 /* .debug_line */ \
11524 + 1 /* .debug_loc */ \
11525 + 1 /* .debug_str_offsets */ \
11526 + 1 /* .debug_macro or .debug_macinfo */ \
11527 + 1 /* trailing zero */)
11528
11529 memset (&sections, 0, sizeof (sections));
11530
11531 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11532 {
11533 asection *sectp;
11534 uint32_t section_nr =
11535 read_4_bytes (dbfd,
11536 dwp_htab->section_pool.v1.indices
11537 + (unit_index + i) * sizeof (uint32_t));
11538
11539 if (section_nr == 0)
11540 break;
11541 if (section_nr >= dwp_file->num_sections)
11542 {
11543 error (_("Dwarf Error: bad DWP hash table, section number too large"
11544 " [in module %s]"),
11545 dwp_file->name);
11546 }
11547
11548 sectp = dwp_file->elf_sections[section_nr];
11549 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11550 {
11551 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11552 " [in module %s]"),
11553 dwp_file->name);
11554 }
11555 }
11556
11557 if (i < 2
11558 || sections.info_or_types.empty ()
11559 || sections.abbrev.empty ())
11560 {
11561 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11562 " [in module %s]"),
11563 dwp_file->name);
11564 }
11565 if (i == MAX_NR_V1_DWO_SECTIONS)
11566 {
11567 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11568 " [in module %s]"),
11569 dwp_file->name);
11570 }
11571
11572 /* It's easier for the rest of the code if we fake a struct dwo_file and
11573 have dwo_unit "live" in that. At least for now.
11574
11575 The DWP file can be made up of a random collection of CUs and TUs.
11576 However, for each CU + set of TUs that came from the same original DWO
11577 file, we can combine them back into a virtual DWO file to save space
11578 (fewer struct dwo_file objects to allocate). Remember that for really
11579 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11580
11581 std::string virtual_dwo_name =
11582 string_printf ("virtual-dwo/%d-%d-%d-%d",
11583 sections.abbrev.get_id (),
11584 sections.line.get_id (),
11585 sections.loc.get_id (),
11586 sections.str_offsets.get_id ());
11587 /* Can we use an existing virtual DWO file? */
11588 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11589 virtual_dwo_name.c_str (),
11590 comp_dir);
11591 /* Create one if necessary. */
11592 if (*dwo_file_slot == NULL)
11593 {
11594 if (dwarf_read_debug)
11595 {
11596 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11597 virtual_dwo_name.c_str ());
11598 }
11599 dwo_file = new struct dwo_file;
11600 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11601 dwo_file->comp_dir = comp_dir;
11602 dwo_file->sections.abbrev = sections.abbrev;
11603 dwo_file->sections.line = sections.line;
11604 dwo_file->sections.loc = sections.loc;
11605 dwo_file->sections.macinfo = sections.macinfo;
11606 dwo_file->sections.macro = sections.macro;
11607 dwo_file->sections.str_offsets = sections.str_offsets;
11608 /* The "str" section is global to the entire DWP file. */
11609 dwo_file->sections.str = dwp_file->sections.str;
11610 /* The info or types section is assigned below to dwo_unit,
11611 there's no need to record it in dwo_file.
11612 Also, we can't simply record type sections in dwo_file because
11613 we record a pointer into the vector in dwo_unit. As we collect more
11614 types we'll grow the vector and eventually have to reallocate space
11615 for it, invalidating all copies of pointers into the previous
11616 contents. */
11617 *dwo_file_slot = dwo_file;
11618 }
11619 else
11620 {
11621 if (dwarf_read_debug)
11622 {
11623 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11624 virtual_dwo_name.c_str ());
11625 }
11626 dwo_file = (struct dwo_file *) *dwo_file_slot;
11627 }
11628
11629 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11630 dwo_unit->dwo_file = dwo_file;
11631 dwo_unit->signature = signature;
11632 dwo_unit->section =
11633 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11634 *dwo_unit->section = sections.info_or_types;
11635 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11636
11637 return dwo_unit;
11638 }
11639
11640 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11641 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11642 piece within that section used by a TU/CU, return a virtual section
11643 of just that piece. */
11644
11645 static struct dwarf2_section_info
11646 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11647 struct dwarf2_section_info *section,
11648 bfd_size_type offset, bfd_size_type size)
11649 {
11650 struct dwarf2_section_info result;
11651 asection *sectp;
11652
11653 gdb_assert (section != NULL);
11654 gdb_assert (!section->is_virtual);
11655
11656 memset (&result, 0, sizeof (result));
11657 result.s.containing_section = section;
11658 result.is_virtual = true;
11659
11660 if (size == 0)
11661 return result;
11662
11663 sectp = section->get_bfd_section ();
11664
11665 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11666 bounds of the real section. This is a pretty-rare event, so just
11667 flag an error (easier) instead of a warning and trying to cope. */
11668 if (sectp == NULL
11669 || offset + size > bfd_section_size (sectp))
11670 {
11671 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11672 " in section %s [in module %s]"),
11673 sectp ? bfd_section_name (sectp) : "<unknown>",
11674 objfile_name (dwarf2_per_objfile->objfile));
11675 }
11676
11677 result.virtual_offset = offset;
11678 result.size = size;
11679 return result;
11680 }
11681
11682 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11683 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11684 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11685 This is for DWP version 2 files. */
11686
11687 static struct dwo_unit *
11688 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11689 struct dwp_file *dwp_file,
11690 uint32_t unit_index,
11691 const char *comp_dir,
11692 ULONGEST signature, int is_debug_types)
11693 {
11694 struct objfile *objfile = dwarf2_per_objfile->objfile;
11695 const struct dwp_hash_table *dwp_htab =
11696 is_debug_types ? dwp_file->tus : dwp_file->cus;
11697 bfd *dbfd = dwp_file->dbfd.get ();
11698 const char *kind = is_debug_types ? "TU" : "CU";
11699 struct dwo_file *dwo_file;
11700 struct dwo_unit *dwo_unit;
11701 struct virtual_v2_dwo_sections sections;
11702 void **dwo_file_slot;
11703 int i;
11704
11705 gdb_assert (dwp_file->version == 2);
11706
11707 if (dwarf_read_debug)
11708 {
11709 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11710 kind,
11711 pulongest (unit_index), hex_string (signature),
11712 dwp_file->name);
11713 }
11714
11715 /* Fetch the section offsets of this DWO unit. */
11716
11717 memset (&sections, 0, sizeof (sections));
11718
11719 for (i = 0; i < dwp_htab->nr_columns; ++i)
11720 {
11721 uint32_t offset = read_4_bytes (dbfd,
11722 dwp_htab->section_pool.v2.offsets
11723 + (((unit_index - 1) * dwp_htab->nr_columns
11724 + i)
11725 * sizeof (uint32_t)));
11726 uint32_t size = read_4_bytes (dbfd,
11727 dwp_htab->section_pool.v2.sizes
11728 + (((unit_index - 1) * dwp_htab->nr_columns
11729 + i)
11730 * sizeof (uint32_t)));
11731
11732 switch (dwp_htab->section_pool.v2.section_ids[i])
11733 {
11734 case DW_SECT_INFO:
11735 case DW_SECT_TYPES:
11736 sections.info_or_types_offset = offset;
11737 sections.info_or_types_size = size;
11738 break;
11739 case DW_SECT_ABBREV:
11740 sections.abbrev_offset = offset;
11741 sections.abbrev_size = size;
11742 break;
11743 case DW_SECT_LINE:
11744 sections.line_offset = offset;
11745 sections.line_size = size;
11746 break;
11747 case DW_SECT_LOC:
11748 sections.loc_offset = offset;
11749 sections.loc_size = size;
11750 break;
11751 case DW_SECT_STR_OFFSETS:
11752 sections.str_offsets_offset = offset;
11753 sections.str_offsets_size = size;
11754 break;
11755 case DW_SECT_MACINFO:
11756 sections.macinfo_offset = offset;
11757 sections.macinfo_size = size;
11758 break;
11759 case DW_SECT_MACRO:
11760 sections.macro_offset = offset;
11761 sections.macro_size = size;
11762 break;
11763 }
11764 }
11765
11766 /* It's easier for the rest of the code if we fake a struct dwo_file and
11767 have dwo_unit "live" in that. At least for now.
11768
11769 The DWP file can be made up of a random collection of CUs and TUs.
11770 However, for each CU + set of TUs that came from the same original DWO
11771 file, we can combine them back into a virtual DWO file to save space
11772 (fewer struct dwo_file objects to allocate). Remember that for really
11773 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11774
11775 std::string virtual_dwo_name =
11776 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11777 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11778 (long) (sections.line_size ? sections.line_offset : 0),
11779 (long) (sections.loc_size ? sections.loc_offset : 0),
11780 (long) (sections.str_offsets_size
11781 ? sections.str_offsets_offset : 0));
11782 /* Can we use an existing virtual DWO file? */
11783 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11784 virtual_dwo_name.c_str (),
11785 comp_dir);
11786 /* Create one if necessary. */
11787 if (*dwo_file_slot == NULL)
11788 {
11789 if (dwarf_read_debug)
11790 {
11791 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11792 virtual_dwo_name.c_str ());
11793 }
11794 dwo_file = new struct dwo_file;
11795 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11796 dwo_file->comp_dir = comp_dir;
11797 dwo_file->sections.abbrev =
11798 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11799 sections.abbrev_offset, sections.abbrev_size);
11800 dwo_file->sections.line =
11801 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11802 sections.line_offset, sections.line_size);
11803 dwo_file->sections.loc =
11804 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11805 sections.loc_offset, sections.loc_size);
11806 dwo_file->sections.macinfo =
11807 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11808 sections.macinfo_offset, sections.macinfo_size);
11809 dwo_file->sections.macro =
11810 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11811 sections.macro_offset, sections.macro_size);
11812 dwo_file->sections.str_offsets =
11813 create_dwp_v2_section (dwarf2_per_objfile,
11814 &dwp_file->sections.str_offsets,
11815 sections.str_offsets_offset,
11816 sections.str_offsets_size);
11817 /* The "str" section is global to the entire DWP file. */
11818 dwo_file->sections.str = dwp_file->sections.str;
11819 /* The info or types section is assigned below to dwo_unit,
11820 there's no need to record it in dwo_file.
11821 Also, we can't simply record type sections in dwo_file because
11822 we record a pointer into the vector in dwo_unit. As we collect more
11823 types we'll grow the vector and eventually have to reallocate space
11824 for it, invalidating all copies of pointers into the previous
11825 contents. */
11826 *dwo_file_slot = dwo_file;
11827 }
11828 else
11829 {
11830 if (dwarf_read_debug)
11831 {
11832 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11833 virtual_dwo_name.c_str ());
11834 }
11835 dwo_file = (struct dwo_file *) *dwo_file_slot;
11836 }
11837
11838 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11839 dwo_unit->dwo_file = dwo_file;
11840 dwo_unit->signature = signature;
11841 dwo_unit->section =
11842 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11843 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11844 is_debug_types
11845 ? &dwp_file->sections.types
11846 : &dwp_file->sections.info,
11847 sections.info_or_types_offset,
11848 sections.info_or_types_size);
11849 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11850
11851 return dwo_unit;
11852 }
11853
11854 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11855 Returns NULL if the signature isn't found. */
11856
11857 static struct dwo_unit *
11858 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11859 struct dwp_file *dwp_file, const char *comp_dir,
11860 ULONGEST signature, int is_debug_types)
11861 {
11862 const struct dwp_hash_table *dwp_htab =
11863 is_debug_types ? dwp_file->tus : dwp_file->cus;
11864 bfd *dbfd = dwp_file->dbfd.get ();
11865 uint32_t mask = dwp_htab->nr_slots - 1;
11866 uint32_t hash = signature & mask;
11867 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11868 unsigned int i;
11869 void **slot;
11870 struct dwo_unit find_dwo_cu;
11871
11872 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11873 find_dwo_cu.signature = signature;
11874 slot = htab_find_slot (is_debug_types
11875 ? dwp_file->loaded_tus.get ()
11876 : dwp_file->loaded_cus.get (),
11877 &find_dwo_cu, INSERT);
11878
11879 if (*slot != NULL)
11880 return (struct dwo_unit *) *slot;
11881
11882 /* Use a for loop so that we don't loop forever on bad debug info. */
11883 for (i = 0; i < dwp_htab->nr_slots; ++i)
11884 {
11885 ULONGEST signature_in_table;
11886
11887 signature_in_table =
11888 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11889 if (signature_in_table == signature)
11890 {
11891 uint32_t unit_index =
11892 read_4_bytes (dbfd,
11893 dwp_htab->unit_table + hash * sizeof (uint32_t));
11894
11895 if (dwp_file->version == 1)
11896 {
11897 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11898 dwp_file, unit_index,
11899 comp_dir, signature,
11900 is_debug_types);
11901 }
11902 else
11903 {
11904 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11905 dwp_file, unit_index,
11906 comp_dir, signature,
11907 is_debug_types);
11908 }
11909 return (struct dwo_unit *) *slot;
11910 }
11911 if (signature_in_table == 0)
11912 return NULL;
11913 hash = (hash + hash2) & mask;
11914 }
11915
11916 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11917 " [in module %s]"),
11918 dwp_file->name);
11919 }
11920
11921 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11922 Open the file specified by FILE_NAME and hand it off to BFD for
11923 preliminary analysis. Return a newly initialized bfd *, which
11924 includes a canonicalized copy of FILE_NAME.
11925 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11926 SEARCH_CWD is true if the current directory is to be searched.
11927 It will be searched before debug-file-directory.
11928 If successful, the file is added to the bfd include table of the
11929 objfile's bfd (see gdb_bfd_record_inclusion).
11930 If unable to find/open the file, return NULL.
11931 NOTE: This function is derived from symfile_bfd_open. */
11932
11933 static gdb_bfd_ref_ptr
11934 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11935 const char *file_name, int is_dwp, int search_cwd)
11936 {
11937 int desc;
11938 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11939 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11940 to debug_file_directory. */
11941 const char *search_path;
11942 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11943
11944 gdb::unique_xmalloc_ptr<char> search_path_holder;
11945 if (search_cwd)
11946 {
11947 if (*debug_file_directory != '\0')
11948 {
11949 search_path_holder.reset (concat (".", dirname_separator_string,
11950 debug_file_directory,
11951 (char *) NULL));
11952 search_path = search_path_holder.get ();
11953 }
11954 else
11955 search_path = ".";
11956 }
11957 else
11958 search_path = debug_file_directory;
11959
11960 openp_flags flags = OPF_RETURN_REALPATH;
11961 if (is_dwp)
11962 flags |= OPF_SEARCH_IN_PATH;
11963
11964 gdb::unique_xmalloc_ptr<char> absolute_name;
11965 desc = openp (search_path, flags, file_name,
11966 O_RDONLY | O_BINARY, &absolute_name);
11967 if (desc < 0)
11968 return NULL;
11969
11970 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11971 gnutarget, desc));
11972 if (sym_bfd == NULL)
11973 return NULL;
11974 bfd_set_cacheable (sym_bfd.get (), 1);
11975
11976 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11977 return NULL;
11978
11979 /* Success. Record the bfd as having been included by the objfile's bfd.
11980 This is important because things like demangled_names_hash lives in the
11981 objfile's per_bfd space and may have references to things like symbol
11982 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11983 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11984
11985 return sym_bfd;
11986 }
11987
11988 /* Try to open DWO file FILE_NAME.
11989 COMP_DIR is the DW_AT_comp_dir attribute.
11990 The result is the bfd handle of the file.
11991 If there is a problem finding or opening the file, return NULL.
11992 Upon success, the canonicalized path of the file is stored in the bfd,
11993 same as symfile_bfd_open. */
11994
11995 static gdb_bfd_ref_ptr
11996 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11997 const char *file_name, const char *comp_dir)
11998 {
11999 if (IS_ABSOLUTE_PATH (file_name))
12000 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12001 0 /*is_dwp*/, 0 /*search_cwd*/);
12002
12003 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12004
12005 if (comp_dir != NULL)
12006 {
12007 gdb::unique_xmalloc_ptr<char> path_to_try
12008 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12009
12010 /* NOTE: If comp_dir is a relative path, this will also try the
12011 search path, which seems useful. */
12012 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12013 path_to_try.get (),
12014 0 /*is_dwp*/,
12015 1 /*search_cwd*/));
12016 if (abfd != NULL)
12017 return abfd;
12018 }
12019
12020 /* That didn't work, try debug-file-directory, which, despite its name,
12021 is a list of paths. */
12022
12023 if (*debug_file_directory == '\0')
12024 return NULL;
12025
12026 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12027 0 /*is_dwp*/, 1 /*search_cwd*/);
12028 }
12029
12030 /* This function is mapped across the sections and remembers the offset and
12031 size of each of the DWO debugging sections we are interested in. */
12032
12033 static void
12034 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12035 {
12036 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12037 const struct dwop_section_names *names = &dwop_section_names;
12038
12039 if (section_is_p (sectp->name, &names->abbrev_dwo))
12040 {
12041 dwo_sections->abbrev.s.section = sectp;
12042 dwo_sections->abbrev.size = bfd_section_size (sectp);
12043 }
12044 else if (section_is_p (sectp->name, &names->info_dwo))
12045 {
12046 dwo_sections->info.s.section = sectp;
12047 dwo_sections->info.size = bfd_section_size (sectp);
12048 }
12049 else if (section_is_p (sectp->name, &names->line_dwo))
12050 {
12051 dwo_sections->line.s.section = sectp;
12052 dwo_sections->line.size = bfd_section_size (sectp);
12053 }
12054 else if (section_is_p (sectp->name, &names->loc_dwo))
12055 {
12056 dwo_sections->loc.s.section = sectp;
12057 dwo_sections->loc.size = bfd_section_size (sectp);
12058 }
12059 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12060 {
12061 dwo_sections->macinfo.s.section = sectp;
12062 dwo_sections->macinfo.size = bfd_section_size (sectp);
12063 }
12064 else if (section_is_p (sectp->name, &names->macro_dwo))
12065 {
12066 dwo_sections->macro.s.section = sectp;
12067 dwo_sections->macro.size = bfd_section_size (sectp);
12068 }
12069 else if (section_is_p (sectp->name, &names->str_dwo))
12070 {
12071 dwo_sections->str.s.section = sectp;
12072 dwo_sections->str.size = bfd_section_size (sectp);
12073 }
12074 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12075 {
12076 dwo_sections->str_offsets.s.section = sectp;
12077 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12078 }
12079 else if (section_is_p (sectp->name, &names->types_dwo))
12080 {
12081 struct dwarf2_section_info type_section;
12082
12083 memset (&type_section, 0, sizeof (type_section));
12084 type_section.s.section = sectp;
12085 type_section.size = bfd_section_size (sectp);
12086 dwo_sections->types.push_back (type_section);
12087 }
12088 }
12089
12090 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12091 by PER_CU. This is for the non-DWP case.
12092 The result is NULL if DWO_NAME can't be found. */
12093
12094 static struct dwo_file *
12095 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12096 const char *dwo_name, const char *comp_dir)
12097 {
12098 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12099
12100 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12101 if (dbfd == NULL)
12102 {
12103 if (dwarf_read_debug)
12104 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12105 return NULL;
12106 }
12107
12108 dwo_file_up dwo_file (new struct dwo_file);
12109 dwo_file->dwo_name = dwo_name;
12110 dwo_file->comp_dir = comp_dir;
12111 dwo_file->dbfd = std::move (dbfd);
12112
12113 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12114 &dwo_file->sections);
12115
12116 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12117 dwo_file->sections.info, dwo_file->cus);
12118
12119 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12120 dwo_file->sections.types, dwo_file->tus);
12121
12122 if (dwarf_read_debug)
12123 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12124
12125 return dwo_file.release ();
12126 }
12127
12128 /* This function is mapped across the sections and remembers the offset and
12129 size of each of the DWP debugging sections common to version 1 and 2 that
12130 we are interested in. */
12131
12132 static void
12133 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12134 void *dwp_file_ptr)
12135 {
12136 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12137 const struct dwop_section_names *names = &dwop_section_names;
12138 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12139
12140 /* Record the ELF section number for later lookup: this is what the
12141 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12142 gdb_assert (elf_section_nr < dwp_file->num_sections);
12143 dwp_file->elf_sections[elf_section_nr] = sectp;
12144
12145 /* Look for specific sections that we need. */
12146 if (section_is_p (sectp->name, &names->str_dwo))
12147 {
12148 dwp_file->sections.str.s.section = sectp;
12149 dwp_file->sections.str.size = bfd_section_size (sectp);
12150 }
12151 else if (section_is_p (sectp->name, &names->cu_index))
12152 {
12153 dwp_file->sections.cu_index.s.section = sectp;
12154 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12155 }
12156 else if (section_is_p (sectp->name, &names->tu_index))
12157 {
12158 dwp_file->sections.tu_index.s.section = sectp;
12159 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12160 }
12161 }
12162
12163 /* This function is mapped across the sections and remembers the offset and
12164 size of each of the DWP version 2 debugging sections that we are interested
12165 in. This is split into a separate function because we don't know if we
12166 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12167
12168 static void
12169 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12170 {
12171 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12172 const struct dwop_section_names *names = &dwop_section_names;
12173 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12174
12175 /* Record the ELF section number for later lookup: this is what the
12176 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12177 gdb_assert (elf_section_nr < dwp_file->num_sections);
12178 dwp_file->elf_sections[elf_section_nr] = sectp;
12179
12180 /* Look for specific sections that we need. */
12181 if (section_is_p (sectp->name, &names->abbrev_dwo))
12182 {
12183 dwp_file->sections.abbrev.s.section = sectp;
12184 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12185 }
12186 else if (section_is_p (sectp->name, &names->info_dwo))
12187 {
12188 dwp_file->sections.info.s.section = sectp;
12189 dwp_file->sections.info.size = bfd_section_size (sectp);
12190 }
12191 else if (section_is_p (sectp->name, &names->line_dwo))
12192 {
12193 dwp_file->sections.line.s.section = sectp;
12194 dwp_file->sections.line.size = bfd_section_size (sectp);
12195 }
12196 else if (section_is_p (sectp->name, &names->loc_dwo))
12197 {
12198 dwp_file->sections.loc.s.section = sectp;
12199 dwp_file->sections.loc.size = bfd_section_size (sectp);
12200 }
12201 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12202 {
12203 dwp_file->sections.macinfo.s.section = sectp;
12204 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12205 }
12206 else if (section_is_p (sectp->name, &names->macro_dwo))
12207 {
12208 dwp_file->sections.macro.s.section = sectp;
12209 dwp_file->sections.macro.size = bfd_section_size (sectp);
12210 }
12211 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12212 {
12213 dwp_file->sections.str_offsets.s.section = sectp;
12214 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12215 }
12216 else if (section_is_p (sectp->name, &names->types_dwo))
12217 {
12218 dwp_file->sections.types.s.section = sectp;
12219 dwp_file->sections.types.size = bfd_section_size (sectp);
12220 }
12221 }
12222
12223 /* Hash function for dwp_file loaded CUs/TUs. */
12224
12225 static hashval_t
12226 hash_dwp_loaded_cutus (const void *item)
12227 {
12228 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12229
12230 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12231 return dwo_unit->signature;
12232 }
12233
12234 /* Equality function for dwp_file loaded CUs/TUs. */
12235
12236 static int
12237 eq_dwp_loaded_cutus (const void *a, const void *b)
12238 {
12239 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12240 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12241
12242 return dua->signature == dub->signature;
12243 }
12244
12245 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12246
12247 static htab_up
12248 allocate_dwp_loaded_cutus_table ()
12249 {
12250 return htab_up (htab_create_alloc (3,
12251 hash_dwp_loaded_cutus,
12252 eq_dwp_loaded_cutus,
12253 NULL, xcalloc, xfree));
12254 }
12255
12256 /* Try to open DWP file FILE_NAME.
12257 The result is the bfd handle of the file.
12258 If there is a problem finding or opening the file, return NULL.
12259 Upon success, the canonicalized path of the file is stored in the bfd,
12260 same as symfile_bfd_open. */
12261
12262 static gdb_bfd_ref_ptr
12263 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12264 const char *file_name)
12265 {
12266 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12267 1 /*is_dwp*/,
12268 1 /*search_cwd*/));
12269 if (abfd != NULL)
12270 return abfd;
12271
12272 /* Work around upstream bug 15652.
12273 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12274 [Whether that's a "bug" is debatable, but it is getting in our way.]
12275 We have no real idea where the dwp file is, because gdb's realpath-ing
12276 of the executable's path may have discarded the needed info.
12277 [IWBN if the dwp file name was recorded in the executable, akin to
12278 .gnu_debuglink, but that doesn't exist yet.]
12279 Strip the directory from FILE_NAME and search again. */
12280 if (*debug_file_directory != '\0')
12281 {
12282 /* Don't implicitly search the current directory here.
12283 If the user wants to search "." to handle this case,
12284 it must be added to debug-file-directory. */
12285 return try_open_dwop_file (dwarf2_per_objfile,
12286 lbasename (file_name), 1 /*is_dwp*/,
12287 0 /*search_cwd*/);
12288 }
12289
12290 return NULL;
12291 }
12292
12293 /* Initialize the use of the DWP file for the current objfile.
12294 By convention the name of the DWP file is ${objfile}.dwp.
12295 The result is NULL if it can't be found. */
12296
12297 static std::unique_ptr<struct dwp_file>
12298 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12299 {
12300 struct objfile *objfile = dwarf2_per_objfile->objfile;
12301
12302 /* Try to find first .dwp for the binary file before any symbolic links
12303 resolving. */
12304
12305 /* If the objfile is a debug file, find the name of the real binary
12306 file and get the name of dwp file from there. */
12307 std::string dwp_name;
12308 if (objfile->separate_debug_objfile_backlink != NULL)
12309 {
12310 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12311 const char *backlink_basename = lbasename (backlink->original_name);
12312
12313 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12314 }
12315 else
12316 dwp_name = objfile->original_name;
12317
12318 dwp_name += ".dwp";
12319
12320 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12321 if (dbfd == NULL
12322 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12323 {
12324 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12325 dwp_name = objfile_name (objfile);
12326 dwp_name += ".dwp";
12327 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12328 }
12329
12330 if (dbfd == NULL)
12331 {
12332 if (dwarf_read_debug)
12333 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12334 return std::unique_ptr<dwp_file> ();
12335 }
12336
12337 const char *name = bfd_get_filename (dbfd.get ());
12338 std::unique_ptr<struct dwp_file> dwp_file
12339 (new struct dwp_file (name, std::move (dbfd)));
12340
12341 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12342 dwp_file->elf_sections =
12343 OBSTACK_CALLOC (&objfile->objfile_obstack,
12344 dwp_file->num_sections, asection *);
12345
12346 bfd_map_over_sections (dwp_file->dbfd.get (),
12347 dwarf2_locate_common_dwp_sections,
12348 dwp_file.get ());
12349
12350 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12351 0);
12352
12353 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12354 1);
12355
12356 /* The DWP file version is stored in the hash table. Oh well. */
12357 if (dwp_file->cus && dwp_file->tus
12358 && dwp_file->cus->version != dwp_file->tus->version)
12359 {
12360 /* Technically speaking, we should try to limp along, but this is
12361 pretty bizarre. We use pulongest here because that's the established
12362 portability solution (e.g, we cannot use %u for uint32_t). */
12363 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12364 " TU version %s [in DWP file %s]"),
12365 pulongest (dwp_file->cus->version),
12366 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12367 }
12368
12369 if (dwp_file->cus)
12370 dwp_file->version = dwp_file->cus->version;
12371 else if (dwp_file->tus)
12372 dwp_file->version = dwp_file->tus->version;
12373 else
12374 dwp_file->version = 2;
12375
12376 if (dwp_file->version == 2)
12377 bfd_map_over_sections (dwp_file->dbfd.get (),
12378 dwarf2_locate_v2_dwp_sections,
12379 dwp_file.get ());
12380
12381 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12382 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12383
12384 if (dwarf_read_debug)
12385 {
12386 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12387 fprintf_unfiltered (gdb_stdlog,
12388 " %s CUs, %s TUs\n",
12389 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12390 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12391 }
12392
12393 return dwp_file;
12394 }
12395
12396 /* Wrapper around open_and_init_dwp_file, only open it once. */
12397
12398 static struct dwp_file *
12399 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12400 {
12401 if (! dwarf2_per_objfile->dwp_checked)
12402 {
12403 dwarf2_per_objfile->dwp_file
12404 = open_and_init_dwp_file (dwarf2_per_objfile);
12405 dwarf2_per_objfile->dwp_checked = 1;
12406 }
12407 return dwarf2_per_objfile->dwp_file.get ();
12408 }
12409
12410 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12411 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12412 or in the DWP file for the objfile, referenced by THIS_UNIT.
12413 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12414 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12415
12416 This is called, for example, when wanting to read a variable with a
12417 complex location. Therefore we don't want to do file i/o for every call.
12418 Therefore we don't want to look for a DWO file on every call.
12419 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12420 then we check if we've already seen DWO_NAME, and only THEN do we check
12421 for a DWO file.
12422
12423 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12424 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12425
12426 static struct dwo_unit *
12427 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12428 const char *dwo_name, const char *comp_dir,
12429 ULONGEST signature, int is_debug_types)
12430 {
12431 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12432 struct objfile *objfile = dwarf2_per_objfile->objfile;
12433 const char *kind = is_debug_types ? "TU" : "CU";
12434 void **dwo_file_slot;
12435 struct dwo_file *dwo_file;
12436 struct dwp_file *dwp_file;
12437
12438 /* First see if there's a DWP file.
12439 If we have a DWP file but didn't find the DWO inside it, don't
12440 look for the original DWO file. It makes gdb behave differently
12441 depending on whether one is debugging in the build tree. */
12442
12443 dwp_file = get_dwp_file (dwarf2_per_objfile);
12444 if (dwp_file != NULL)
12445 {
12446 const struct dwp_hash_table *dwp_htab =
12447 is_debug_types ? dwp_file->tus : dwp_file->cus;
12448
12449 if (dwp_htab != NULL)
12450 {
12451 struct dwo_unit *dwo_cutu =
12452 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12453 signature, is_debug_types);
12454
12455 if (dwo_cutu != NULL)
12456 {
12457 if (dwarf_read_debug)
12458 {
12459 fprintf_unfiltered (gdb_stdlog,
12460 "Virtual DWO %s %s found: @%s\n",
12461 kind, hex_string (signature),
12462 host_address_to_string (dwo_cutu));
12463 }
12464 return dwo_cutu;
12465 }
12466 }
12467 }
12468 else
12469 {
12470 /* No DWP file, look for the DWO file. */
12471
12472 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12473 dwo_name, comp_dir);
12474 if (*dwo_file_slot == NULL)
12475 {
12476 /* Read in the file and build a table of the CUs/TUs it contains. */
12477 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12478 }
12479 /* NOTE: This will be NULL if unable to open the file. */
12480 dwo_file = (struct dwo_file *) *dwo_file_slot;
12481
12482 if (dwo_file != NULL)
12483 {
12484 struct dwo_unit *dwo_cutu = NULL;
12485
12486 if (is_debug_types && dwo_file->tus)
12487 {
12488 struct dwo_unit find_dwo_cutu;
12489
12490 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12491 find_dwo_cutu.signature = signature;
12492 dwo_cutu
12493 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12494 &find_dwo_cutu);
12495 }
12496 else if (!is_debug_types && dwo_file->cus)
12497 {
12498 struct dwo_unit find_dwo_cutu;
12499
12500 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12501 find_dwo_cutu.signature = signature;
12502 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12503 &find_dwo_cutu);
12504 }
12505
12506 if (dwo_cutu != NULL)
12507 {
12508 if (dwarf_read_debug)
12509 {
12510 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12511 kind, dwo_name, hex_string (signature),
12512 host_address_to_string (dwo_cutu));
12513 }
12514 return dwo_cutu;
12515 }
12516 }
12517 }
12518
12519 /* We didn't find it. This could mean a dwo_id mismatch, or
12520 someone deleted the DWO/DWP file, or the search path isn't set up
12521 correctly to find the file. */
12522
12523 if (dwarf_read_debug)
12524 {
12525 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12526 kind, dwo_name, hex_string (signature));
12527 }
12528
12529 /* This is a warning and not a complaint because it can be caused by
12530 pilot error (e.g., user accidentally deleting the DWO). */
12531 {
12532 /* Print the name of the DWP file if we looked there, helps the user
12533 better diagnose the problem. */
12534 std::string dwp_text;
12535
12536 if (dwp_file != NULL)
12537 dwp_text = string_printf (" [in DWP file %s]",
12538 lbasename (dwp_file->name));
12539
12540 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12541 " [in module %s]"),
12542 kind, dwo_name, hex_string (signature),
12543 dwp_text.c_str (),
12544 this_unit->is_debug_types ? "TU" : "CU",
12545 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12546 }
12547 return NULL;
12548 }
12549
12550 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12551 See lookup_dwo_cutu_unit for details. */
12552
12553 static struct dwo_unit *
12554 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12555 const char *dwo_name, const char *comp_dir,
12556 ULONGEST signature)
12557 {
12558 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12559 }
12560
12561 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12562 See lookup_dwo_cutu_unit for details. */
12563
12564 static struct dwo_unit *
12565 lookup_dwo_type_unit (struct signatured_type *this_tu,
12566 const char *dwo_name, const char *comp_dir)
12567 {
12568 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12569 }
12570
12571 /* Traversal function for queue_and_load_all_dwo_tus. */
12572
12573 static int
12574 queue_and_load_dwo_tu (void **slot, void *info)
12575 {
12576 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12577 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12578 ULONGEST signature = dwo_unit->signature;
12579 struct signatured_type *sig_type =
12580 lookup_dwo_signatured_type (per_cu->cu, signature);
12581
12582 if (sig_type != NULL)
12583 {
12584 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12585
12586 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12587 a real dependency of PER_CU on SIG_TYPE. That is detected later
12588 while processing PER_CU. */
12589 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12590 load_full_type_unit (sig_cu);
12591 per_cu->imported_symtabs_push (sig_cu);
12592 }
12593
12594 return 1;
12595 }
12596
12597 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12598 The DWO may have the only definition of the type, though it may not be
12599 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12600 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12601
12602 static void
12603 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12604 {
12605 struct dwo_unit *dwo_unit;
12606 struct dwo_file *dwo_file;
12607
12608 gdb_assert (!per_cu->is_debug_types);
12609 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12610 gdb_assert (per_cu->cu != NULL);
12611
12612 dwo_unit = per_cu->cu->dwo_unit;
12613 gdb_assert (dwo_unit != NULL);
12614
12615 dwo_file = dwo_unit->dwo_file;
12616 if (dwo_file->tus != NULL)
12617 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12618 per_cu);
12619 }
12620
12621 /* Read in various DIEs. */
12622
12623 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12624 Inherit only the children of the DW_AT_abstract_origin DIE not being
12625 already referenced by DW_AT_abstract_origin from the children of the
12626 current DIE. */
12627
12628 static void
12629 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12630 {
12631 struct die_info *child_die;
12632 sect_offset *offsetp;
12633 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12634 struct die_info *origin_die;
12635 /* Iterator of the ORIGIN_DIE children. */
12636 struct die_info *origin_child_die;
12637 struct attribute *attr;
12638 struct dwarf2_cu *origin_cu;
12639 struct pending **origin_previous_list_in_scope;
12640
12641 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12642 if (!attr)
12643 return;
12644
12645 /* Note that following die references may follow to a die in a
12646 different cu. */
12647
12648 origin_cu = cu;
12649 origin_die = follow_die_ref (die, attr, &origin_cu);
12650
12651 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12652 symbols in. */
12653 origin_previous_list_in_scope = origin_cu->list_in_scope;
12654 origin_cu->list_in_scope = cu->list_in_scope;
12655
12656 if (die->tag != origin_die->tag
12657 && !(die->tag == DW_TAG_inlined_subroutine
12658 && origin_die->tag == DW_TAG_subprogram))
12659 complaint (_("DIE %s and its abstract origin %s have different tags"),
12660 sect_offset_str (die->sect_off),
12661 sect_offset_str (origin_die->sect_off));
12662
12663 std::vector<sect_offset> offsets;
12664
12665 for (child_die = die->child;
12666 child_die && child_die->tag;
12667 child_die = child_die->sibling)
12668 {
12669 struct die_info *child_origin_die;
12670 struct dwarf2_cu *child_origin_cu;
12671
12672 /* We are trying to process concrete instance entries:
12673 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12674 it's not relevant to our analysis here. i.e. detecting DIEs that are
12675 present in the abstract instance but not referenced in the concrete
12676 one. */
12677 if (child_die->tag == DW_TAG_call_site
12678 || child_die->tag == DW_TAG_GNU_call_site)
12679 continue;
12680
12681 /* For each CHILD_DIE, find the corresponding child of
12682 ORIGIN_DIE. If there is more than one layer of
12683 DW_AT_abstract_origin, follow them all; there shouldn't be,
12684 but GCC versions at least through 4.4 generate this (GCC PR
12685 40573). */
12686 child_origin_die = child_die;
12687 child_origin_cu = cu;
12688 while (1)
12689 {
12690 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12691 child_origin_cu);
12692 if (attr == NULL)
12693 break;
12694 child_origin_die = follow_die_ref (child_origin_die, attr,
12695 &child_origin_cu);
12696 }
12697
12698 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12699 counterpart may exist. */
12700 if (child_origin_die != child_die)
12701 {
12702 if (child_die->tag != child_origin_die->tag
12703 && !(child_die->tag == DW_TAG_inlined_subroutine
12704 && child_origin_die->tag == DW_TAG_subprogram))
12705 complaint (_("Child DIE %s and its abstract origin %s have "
12706 "different tags"),
12707 sect_offset_str (child_die->sect_off),
12708 sect_offset_str (child_origin_die->sect_off));
12709 if (child_origin_die->parent != origin_die)
12710 complaint (_("Child DIE %s and its abstract origin %s have "
12711 "different parents"),
12712 sect_offset_str (child_die->sect_off),
12713 sect_offset_str (child_origin_die->sect_off));
12714 else
12715 offsets.push_back (child_origin_die->sect_off);
12716 }
12717 }
12718 std::sort (offsets.begin (), offsets.end ());
12719 sect_offset *offsets_end = offsets.data () + offsets.size ();
12720 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12721 if (offsetp[-1] == *offsetp)
12722 complaint (_("Multiple children of DIE %s refer "
12723 "to DIE %s as their abstract origin"),
12724 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12725
12726 offsetp = offsets.data ();
12727 origin_child_die = origin_die->child;
12728 while (origin_child_die && origin_child_die->tag)
12729 {
12730 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12731 while (offsetp < offsets_end
12732 && *offsetp < origin_child_die->sect_off)
12733 offsetp++;
12734 if (offsetp >= offsets_end
12735 || *offsetp > origin_child_die->sect_off)
12736 {
12737 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12738 Check whether we're already processing ORIGIN_CHILD_DIE.
12739 This can happen with mutually referenced abstract_origins.
12740 PR 16581. */
12741 if (!origin_child_die->in_process)
12742 process_die (origin_child_die, origin_cu);
12743 }
12744 origin_child_die = origin_child_die->sibling;
12745 }
12746 origin_cu->list_in_scope = origin_previous_list_in_scope;
12747
12748 if (cu != origin_cu)
12749 compute_delayed_physnames (origin_cu);
12750 }
12751
12752 static void
12753 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12754 {
12755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12756 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12757 struct context_stack *newobj;
12758 CORE_ADDR lowpc;
12759 CORE_ADDR highpc;
12760 struct die_info *child_die;
12761 struct attribute *attr, *call_line, *call_file;
12762 const char *name;
12763 CORE_ADDR baseaddr;
12764 struct block *block;
12765 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12766 std::vector<struct symbol *> template_args;
12767 struct template_symbol *templ_func = NULL;
12768
12769 if (inlined_func)
12770 {
12771 /* If we do not have call site information, we can't show the
12772 caller of this inlined function. That's too confusing, so
12773 only use the scope for local variables. */
12774 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12775 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12776 if (call_line == NULL || call_file == NULL)
12777 {
12778 read_lexical_block_scope (die, cu);
12779 return;
12780 }
12781 }
12782
12783 baseaddr = objfile->text_section_offset ();
12784
12785 name = dwarf2_name (die, cu);
12786
12787 /* Ignore functions with missing or empty names. These are actually
12788 illegal according to the DWARF standard. */
12789 if (name == NULL)
12790 {
12791 complaint (_("missing name for subprogram DIE at %s"),
12792 sect_offset_str (die->sect_off));
12793 return;
12794 }
12795
12796 /* Ignore functions with missing or invalid low and high pc attributes. */
12797 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12798 <= PC_BOUNDS_INVALID)
12799 {
12800 attr = dwarf2_attr (die, DW_AT_external, cu);
12801 if (!attr || !DW_UNSND (attr))
12802 complaint (_("cannot get low and high bounds "
12803 "for subprogram DIE at %s"),
12804 sect_offset_str (die->sect_off));
12805 return;
12806 }
12807
12808 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12809 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12810
12811 /* If we have any template arguments, then we must allocate a
12812 different sort of symbol. */
12813 for (child_die = die->child; child_die; child_die = child_die->sibling)
12814 {
12815 if (child_die->tag == DW_TAG_template_type_param
12816 || child_die->tag == DW_TAG_template_value_param)
12817 {
12818 templ_func = allocate_template_symbol (objfile);
12819 templ_func->subclass = SYMBOL_TEMPLATE;
12820 break;
12821 }
12822 }
12823
12824 newobj = cu->get_builder ()->push_context (0, lowpc);
12825 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12826 (struct symbol *) templ_func);
12827
12828 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12829 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12830 cu->language);
12831
12832 /* If there is a location expression for DW_AT_frame_base, record
12833 it. */
12834 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12835 if (attr != nullptr)
12836 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12837
12838 /* If there is a location for the static link, record it. */
12839 newobj->static_link = NULL;
12840 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12841 if (attr != nullptr)
12842 {
12843 newobj->static_link
12844 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12845 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12846 cu->per_cu->addr_type ());
12847 }
12848
12849 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12850
12851 if (die->child != NULL)
12852 {
12853 child_die = die->child;
12854 while (child_die && child_die->tag)
12855 {
12856 if (child_die->tag == DW_TAG_template_type_param
12857 || child_die->tag == DW_TAG_template_value_param)
12858 {
12859 struct symbol *arg = new_symbol (child_die, NULL, cu);
12860
12861 if (arg != NULL)
12862 template_args.push_back (arg);
12863 }
12864 else
12865 process_die (child_die, cu);
12866 child_die = child_die->sibling;
12867 }
12868 }
12869
12870 inherit_abstract_dies (die, cu);
12871
12872 /* If we have a DW_AT_specification, we might need to import using
12873 directives from the context of the specification DIE. See the
12874 comment in determine_prefix. */
12875 if (cu->language == language_cplus
12876 && dwarf2_attr (die, DW_AT_specification, cu))
12877 {
12878 struct dwarf2_cu *spec_cu = cu;
12879 struct die_info *spec_die = die_specification (die, &spec_cu);
12880
12881 while (spec_die)
12882 {
12883 child_die = spec_die->child;
12884 while (child_die && child_die->tag)
12885 {
12886 if (child_die->tag == DW_TAG_imported_module)
12887 process_die (child_die, spec_cu);
12888 child_die = child_die->sibling;
12889 }
12890
12891 /* In some cases, GCC generates specification DIEs that
12892 themselves contain DW_AT_specification attributes. */
12893 spec_die = die_specification (spec_die, &spec_cu);
12894 }
12895 }
12896
12897 struct context_stack cstk = cu->get_builder ()->pop_context ();
12898 /* Make a block for the local symbols within. */
12899 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12900 cstk.static_link, lowpc, highpc);
12901
12902 /* For C++, set the block's scope. */
12903 if ((cu->language == language_cplus
12904 || cu->language == language_fortran
12905 || cu->language == language_d
12906 || cu->language == language_rust)
12907 && cu->processing_has_namespace_info)
12908 block_set_scope (block, determine_prefix (die, cu),
12909 &objfile->objfile_obstack);
12910
12911 /* If we have address ranges, record them. */
12912 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12913
12914 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12915
12916 /* Attach template arguments to function. */
12917 if (!template_args.empty ())
12918 {
12919 gdb_assert (templ_func != NULL);
12920
12921 templ_func->n_template_arguments = template_args.size ();
12922 templ_func->template_arguments
12923 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12924 templ_func->n_template_arguments);
12925 memcpy (templ_func->template_arguments,
12926 template_args.data (),
12927 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12928
12929 /* Make sure that the symtab is set on the new symbols. Even
12930 though they don't appear in this symtab directly, other parts
12931 of gdb assume that symbols do, and this is reasonably
12932 true. */
12933 for (symbol *sym : template_args)
12934 symbol_set_symtab (sym, symbol_symtab (templ_func));
12935 }
12936
12937 /* In C++, we can have functions nested inside functions (e.g., when
12938 a function declares a class that has methods). This means that
12939 when we finish processing a function scope, we may need to go
12940 back to building a containing block's symbol lists. */
12941 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12942 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12943
12944 /* If we've finished processing a top-level function, subsequent
12945 symbols go in the file symbol list. */
12946 if (cu->get_builder ()->outermost_context_p ())
12947 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12948 }
12949
12950 /* Process all the DIES contained within a lexical block scope. Start
12951 a new scope, process the dies, and then close the scope. */
12952
12953 static void
12954 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12955 {
12956 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12958 CORE_ADDR lowpc, highpc;
12959 struct die_info *child_die;
12960 CORE_ADDR baseaddr;
12961
12962 baseaddr = objfile->text_section_offset ();
12963
12964 /* Ignore blocks with missing or invalid low and high pc attributes. */
12965 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12966 as multiple lexical blocks? Handling children in a sane way would
12967 be nasty. Might be easier to properly extend generic blocks to
12968 describe ranges. */
12969 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12970 {
12971 case PC_BOUNDS_NOT_PRESENT:
12972 /* DW_TAG_lexical_block has no attributes, process its children as if
12973 there was no wrapping by that DW_TAG_lexical_block.
12974 GCC does no longer produces such DWARF since GCC r224161. */
12975 for (child_die = die->child;
12976 child_die != NULL && child_die->tag;
12977 child_die = child_die->sibling)
12978 process_die (child_die, cu);
12979 return;
12980 case PC_BOUNDS_INVALID:
12981 return;
12982 }
12983 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12984 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12985
12986 cu->get_builder ()->push_context (0, lowpc);
12987 if (die->child != NULL)
12988 {
12989 child_die = die->child;
12990 while (child_die && child_die->tag)
12991 {
12992 process_die (child_die, cu);
12993 child_die = child_die->sibling;
12994 }
12995 }
12996 inherit_abstract_dies (die, cu);
12997 struct context_stack cstk = cu->get_builder ()->pop_context ();
12998
12999 if (*cu->get_builder ()->get_local_symbols () != NULL
13000 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13001 {
13002 struct block *block
13003 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13004 cstk.start_addr, highpc);
13005
13006 /* Note that recording ranges after traversing children, as we
13007 do here, means that recording a parent's ranges entails
13008 walking across all its children's ranges as they appear in
13009 the address map, which is quadratic behavior.
13010
13011 It would be nicer to record the parent's ranges before
13012 traversing its children, simply overriding whatever you find
13013 there. But since we don't even decide whether to create a
13014 block until after we've traversed its children, that's hard
13015 to do. */
13016 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13017 }
13018 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13019 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13020 }
13021
13022 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13023
13024 static void
13025 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13026 {
13027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13029 CORE_ADDR pc, baseaddr;
13030 struct attribute *attr;
13031 struct call_site *call_site, call_site_local;
13032 void **slot;
13033 int nparams;
13034 struct die_info *child_die;
13035
13036 baseaddr = objfile->text_section_offset ();
13037
13038 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13039 if (attr == NULL)
13040 {
13041 /* This was a pre-DWARF-5 GNU extension alias
13042 for DW_AT_call_return_pc. */
13043 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13044 }
13045 if (!attr)
13046 {
13047 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13048 "DIE %s [in module %s]"),
13049 sect_offset_str (die->sect_off), objfile_name (objfile));
13050 return;
13051 }
13052 pc = attr->value_as_address () + baseaddr;
13053 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13054
13055 if (cu->call_site_htab == NULL)
13056 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13057 NULL, &objfile->objfile_obstack,
13058 hashtab_obstack_allocate, NULL);
13059 call_site_local.pc = pc;
13060 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13061 if (*slot != NULL)
13062 {
13063 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13064 "DIE %s [in module %s]"),
13065 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13066 objfile_name (objfile));
13067 return;
13068 }
13069
13070 /* Count parameters at the caller. */
13071
13072 nparams = 0;
13073 for (child_die = die->child; child_die && child_die->tag;
13074 child_die = child_die->sibling)
13075 {
13076 if (child_die->tag != DW_TAG_call_site_parameter
13077 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13078 {
13079 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13080 "DW_TAG_call_site child DIE %s [in module %s]"),
13081 child_die->tag, sect_offset_str (child_die->sect_off),
13082 objfile_name (objfile));
13083 continue;
13084 }
13085
13086 nparams++;
13087 }
13088
13089 call_site
13090 = ((struct call_site *)
13091 obstack_alloc (&objfile->objfile_obstack,
13092 sizeof (*call_site)
13093 + (sizeof (*call_site->parameter) * (nparams - 1))));
13094 *slot = call_site;
13095 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13096 call_site->pc = pc;
13097
13098 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13099 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13100 {
13101 struct die_info *func_die;
13102
13103 /* Skip also over DW_TAG_inlined_subroutine. */
13104 for (func_die = die->parent;
13105 func_die && func_die->tag != DW_TAG_subprogram
13106 && func_die->tag != DW_TAG_subroutine_type;
13107 func_die = func_die->parent);
13108
13109 /* DW_AT_call_all_calls is a superset
13110 of DW_AT_call_all_tail_calls. */
13111 if (func_die
13112 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13113 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13114 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13115 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13116 {
13117 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13118 not complete. But keep CALL_SITE for look ups via call_site_htab,
13119 both the initial caller containing the real return address PC and
13120 the final callee containing the current PC of a chain of tail
13121 calls do not need to have the tail call list complete. But any
13122 function candidate for a virtual tail call frame searched via
13123 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13124 determined unambiguously. */
13125 }
13126 else
13127 {
13128 struct type *func_type = NULL;
13129
13130 if (func_die)
13131 func_type = get_die_type (func_die, cu);
13132 if (func_type != NULL)
13133 {
13134 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13135
13136 /* Enlist this call site to the function. */
13137 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13138 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13139 }
13140 else
13141 complaint (_("Cannot find function owning DW_TAG_call_site "
13142 "DIE %s [in module %s]"),
13143 sect_offset_str (die->sect_off), objfile_name (objfile));
13144 }
13145 }
13146
13147 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13148 if (attr == NULL)
13149 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13150 if (attr == NULL)
13151 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13152 if (attr == NULL)
13153 {
13154 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13155 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13156 }
13157 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13158 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13159 /* Keep NULL DWARF_BLOCK. */;
13160 else if (attr->form_is_block ())
13161 {
13162 struct dwarf2_locexpr_baton *dlbaton;
13163
13164 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13165 dlbaton->data = DW_BLOCK (attr)->data;
13166 dlbaton->size = DW_BLOCK (attr)->size;
13167 dlbaton->per_cu = cu->per_cu;
13168
13169 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13170 }
13171 else if (attr->form_is_ref ())
13172 {
13173 struct dwarf2_cu *target_cu = cu;
13174 struct die_info *target_die;
13175
13176 target_die = follow_die_ref (die, attr, &target_cu);
13177 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13178 if (die_is_declaration (target_die, target_cu))
13179 {
13180 const char *target_physname;
13181
13182 /* Prefer the mangled name; otherwise compute the demangled one. */
13183 target_physname = dw2_linkage_name (target_die, target_cu);
13184 if (target_physname == NULL)
13185 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13186 if (target_physname == NULL)
13187 complaint (_("DW_AT_call_target target DIE has invalid "
13188 "physname, for referencing DIE %s [in module %s]"),
13189 sect_offset_str (die->sect_off), objfile_name (objfile));
13190 else
13191 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13192 }
13193 else
13194 {
13195 CORE_ADDR lowpc;
13196
13197 /* DW_AT_entry_pc should be preferred. */
13198 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13199 <= PC_BOUNDS_INVALID)
13200 complaint (_("DW_AT_call_target target DIE has invalid "
13201 "low pc, for referencing DIE %s [in module %s]"),
13202 sect_offset_str (die->sect_off), objfile_name (objfile));
13203 else
13204 {
13205 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13206 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13207 }
13208 }
13209 }
13210 else
13211 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13212 "block nor reference, for DIE %s [in module %s]"),
13213 sect_offset_str (die->sect_off), objfile_name (objfile));
13214
13215 call_site->per_cu = cu->per_cu;
13216
13217 for (child_die = die->child;
13218 child_die && child_die->tag;
13219 child_die = child_die->sibling)
13220 {
13221 struct call_site_parameter *parameter;
13222 struct attribute *loc, *origin;
13223
13224 if (child_die->tag != DW_TAG_call_site_parameter
13225 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13226 {
13227 /* Already printed the complaint above. */
13228 continue;
13229 }
13230
13231 gdb_assert (call_site->parameter_count < nparams);
13232 parameter = &call_site->parameter[call_site->parameter_count];
13233
13234 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13235 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13236 register is contained in DW_AT_call_value. */
13237
13238 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13239 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13240 if (origin == NULL)
13241 {
13242 /* This was a pre-DWARF-5 GNU extension alias
13243 for DW_AT_call_parameter. */
13244 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13245 }
13246 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13247 {
13248 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13249
13250 sect_offset sect_off = origin->get_ref_die_offset ();
13251 if (!cu->header.offset_in_cu_p (sect_off))
13252 {
13253 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13254 binding can be done only inside one CU. Such referenced DIE
13255 therefore cannot be even moved to DW_TAG_partial_unit. */
13256 complaint (_("DW_AT_call_parameter offset is not in CU for "
13257 "DW_TAG_call_site child DIE %s [in module %s]"),
13258 sect_offset_str (child_die->sect_off),
13259 objfile_name (objfile));
13260 continue;
13261 }
13262 parameter->u.param_cu_off
13263 = (cu_offset) (sect_off - cu->header.sect_off);
13264 }
13265 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13266 {
13267 complaint (_("No DW_FORM_block* DW_AT_location for "
13268 "DW_TAG_call_site child DIE %s [in module %s]"),
13269 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13270 continue;
13271 }
13272 else
13273 {
13274 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13275 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13276 if (parameter->u.dwarf_reg != -1)
13277 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13278 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13279 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13280 &parameter->u.fb_offset))
13281 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13282 else
13283 {
13284 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13285 "for DW_FORM_block* DW_AT_location is supported for "
13286 "DW_TAG_call_site child DIE %s "
13287 "[in module %s]"),
13288 sect_offset_str (child_die->sect_off),
13289 objfile_name (objfile));
13290 continue;
13291 }
13292 }
13293
13294 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13295 if (attr == NULL)
13296 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13297 if (attr == NULL || !attr->form_is_block ())
13298 {
13299 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13300 "DW_TAG_call_site child DIE %s [in module %s]"),
13301 sect_offset_str (child_die->sect_off),
13302 objfile_name (objfile));
13303 continue;
13304 }
13305 parameter->value = DW_BLOCK (attr)->data;
13306 parameter->value_size = DW_BLOCK (attr)->size;
13307
13308 /* Parameters are not pre-cleared by memset above. */
13309 parameter->data_value = NULL;
13310 parameter->data_value_size = 0;
13311 call_site->parameter_count++;
13312
13313 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13314 if (attr == NULL)
13315 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13316 if (attr != nullptr)
13317 {
13318 if (!attr->form_is_block ())
13319 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13320 "DW_TAG_call_site child DIE %s [in module %s]"),
13321 sect_offset_str (child_die->sect_off),
13322 objfile_name (objfile));
13323 else
13324 {
13325 parameter->data_value = DW_BLOCK (attr)->data;
13326 parameter->data_value_size = DW_BLOCK (attr)->size;
13327 }
13328 }
13329 }
13330 }
13331
13332 /* Helper function for read_variable. If DIE represents a virtual
13333 table, then return the type of the concrete object that is
13334 associated with the virtual table. Otherwise, return NULL. */
13335
13336 static struct type *
13337 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13338 {
13339 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13340 if (attr == NULL)
13341 return NULL;
13342
13343 /* Find the type DIE. */
13344 struct die_info *type_die = NULL;
13345 struct dwarf2_cu *type_cu = cu;
13346
13347 if (attr->form_is_ref ())
13348 type_die = follow_die_ref (die, attr, &type_cu);
13349 if (type_die == NULL)
13350 return NULL;
13351
13352 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13353 return NULL;
13354 return die_containing_type (type_die, type_cu);
13355 }
13356
13357 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13358
13359 static void
13360 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13361 {
13362 struct rust_vtable_symbol *storage = NULL;
13363
13364 if (cu->language == language_rust)
13365 {
13366 struct type *containing_type = rust_containing_type (die, cu);
13367
13368 if (containing_type != NULL)
13369 {
13370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13371
13372 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13373 initialize_objfile_symbol (storage);
13374 storage->concrete_type = containing_type;
13375 storage->subclass = SYMBOL_RUST_VTABLE;
13376 }
13377 }
13378
13379 struct symbol *res = new_symbol (die, NULL, cu, storage);
13380 struct attribute *abstract_origin
13381 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13382 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13383 if (res == NULL && loc && abstract_origin)
13384 {
13385 /* We have a variable without a name, but with a location and an abstract
13386 origin. This may be a concrete instance of an abstract variable
13387 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13388 later. */
13389 struct dwarf2_cu *origin_cu = cu;
13390 struct die_info *origin_die
13391 = follow_die_ref (die, abstract_origin, &origin_cu);
13392 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13393 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13394 }
13395 }
13396
13397 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13398 reading .debug_rnglists.
13399 Callback's type should be:
13400 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13401 Return true if the attributes are present and valid, otherwise,
13402 return false. */
13403
13404 template <typename Callback>
13405 static bool
13406 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13407 Callback &&callback)
13408 {
13409 struct dwarf2_per_objfile *dwarf2_per_objfile
13410 = cu->per_cu->dwarf2_per_objfile;
13411 struct objfile *objfile = dwarf2_per_objfile->objfile;
13412 bfd *obfd = objfile->obfd;
13413 /* Base address selection entry. */
13414 gdb::optional<CORE_ADDR> base;
13415 const gdb_byte *buffer;
13416 CORE_ADDR baseaddr;
13417 bool overflow = false;
13418
13419 base = cu->base_address;
13420
13421 dwarf2_per_objfile->rnglists.read (objfile);
13422 if (offset >= dwarf2_per_objfile->rnglists.size)
13423 {
13424 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13425 offset);
13426 return false;
13427 }
13428 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13429
13430 baseaddr = objfile->text_section_offset ();
13431
13432 while (1)
13433 {
13434 /* Initialize it due to a false compiler warning. */
13435 CORE_ADDR range_beginning = 0, range_end = 0;
13436 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13437 + dwarf2_per_objfile->rnglists.size);
13438 unsigned int bytes_read;
13439
13440 if (buffer == buf_end)
13441 {
13442 overflow = true;
13443 break;
13444 }
13445 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13446 switch (rlet)
13447 {
13448 case DW_RLE_end_of_list:
13449 break;
13450 case DW_RLE_base_address:
13451 if (buffer + cu->header.addr_size > buf_end)
13452 {
13453 overflow = true;
13454 break;
13455 }
13456 base = cu->header.read_address (obfd, buffer, &bytes_read);
13457 buffer += bytes_read;
13458 break;
13459 case DW_RLE_start_length:
13460 if (buffer + cu->header.addr_size > buf_end)
13461 {
13462 overflow = true;
13463 break;
13464 }
13465 range_beginning = cu->header.read_address (obfd, buffer,
13466 &bytes_read);
13467 buffer += bytes_read;
13468 range_end = (range_beginning
13469 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13470 buffer += bytes_read;
13471 if (buffer > buf_end)
13472 {
13473 overflow = true;
13474 break;
13475 }
13476 break;
13477 case DW_RLE_offset_pair:
13478 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13479 buffer += bytes_read;
13480 if (buffer > buf_end)
13481 {
13482 overflow = true;
13483 break;
13484 }
13485 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13486 buffer += bytes_read;
13487 if (buffer > buf_end)
13488 {
13489 overflow = true;
13490 break;
13491 }
13492 break;
13493 case DW_RLE_start_end:
13494 if (buffer + 2 * cu->header.addr_size > buf_end)
13495 {
13496 overflow = true;
13497 break;
13498 }
13499 range_beginning = cu->header.read_address (obfd, buffer,
13500 &bytes_read);
13501 buffer += bytes_read;
13502 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13503 buffer += bytes_read;
13504 break;
13505 default:
13506 complaint (_("Invalid .debug_rnglists data (no base address)"));
13507 return false;
13508 }
13509 if (rlet == DW_RLE_end_of_list || overflow)
13510 break;
13511 if (rlet == DW_RLE_base_address)
13512 continue;
13513
13514 if (!base.has_value ())
13515 {
13516 /* We have no valid base address for the ranges
13517 data. */
13518 complaint (_("Invalid .debug_rnglists data (no base address)"));
13519 return false;
13520 }
13521
13522 if (range_beginning > range_end)
13523 {
13524 /* Inverted range entries are invalid. */
13525 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13526 return false;
13527 }
13528
13529 /* Empty range entries have no effect. */
13530 if (range_beginning == range_end)
13531 continue;
13532
13533 range_beginning += *base;
13534 range_end += *base;
13535
13536 /* A not-uncommon case of bad debug info.
13537 Don't pollute the addrmap with bad data. */
13538 if (range_beginning + baseaddr == 0
13539 && !dwarf2_per_objfile->has_section_at_zero)
13540 {
13541 complaint (_(".debug_rnglists entry has start address of zero"
13542 " [in module %s]"), objfile_name (objfile));
13543 continue;
13544 }
13545
13546 callback (range_beginning, range_end);
13547 }
13548
13549 if (overflow)
13550 {
13551 complaint (_("Offset %d is not terminated "
13552 "for DW_AT_ranges attribute"),
13553 offset);
13554 return false;
13555 }
13556
13557 return true;
13558 }
13559
13560 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13561 Callback's type should be:
13562 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13563 Return 1 if the attributes are present and valid, otherwise, return 0. */
13564
13565 template <typename Callback>
13566 static int
13567 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13568 Callback &&callback)
13569 {
13570 struct dwarf2_per_objfile *dwarf2_per_objfile
13571 = cu->per_cu->dwarf2_per_objfile;
13572 struct objfile *objfile = dwarf2_per_objfile->objfile;
13573 struct comp_unit_head *cu_header = &cu->header;
13574 bfd *obfd = objfile->obfd;
13575 unsigned int addr_size = cu_header->addr_size;
13576 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13577 /* Base address selection entry. */
13578 gdb::optional<CORE_ADDR> base;
13579 unsigned int dummy;
13580 const gdb_byte *buffer;
13581 CORE_ADDR baseaddr;
13582
13583 if (cu_header->version >= 5)
13584 return dwarf2_rnglists_process (offset, cu, callback);
13585
13586 base = cu->base_address;
13587
13588 dwarf2_per_objfile->ranges.read (objfile);
13589 if (offset >= dwarf2_per_objfile->ranges.size)
13590 {
13591 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13592 offset);
13593 return 0;
13594 }
13595 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13596
13597 baseaddr = objfile->text_section_offset ();
13598
13599 while (1)
13600 {
13601 CORE_ADDR range_beginning, range_end;
13602
13603 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13604 buffer += addr_size;
13605 range_end = cu->header.read_address (obfd, buffer, &dummy);
13606 buffer += addr_size;
13607 offset += 2 * addr_size;
13608
13609 /* An end of list marker is a pair of zero addresses. */
13610 if (range_beginning == 0 && range_end == 0)
13611 /* Found the end of list entry. */
13612 break;
13613
13614 /* Each base address selection entry is a pair of 2 values.
13615 The first is the largest possible address, the second is
13616 the base address. Check for a base address here. */
13617 if ((range_beginning & mask) == mask)
13618 {
13619 /* If we found the largest possible address, then we already
13620 have the base address in range_end. */
13621 base = range_end;
13622 continue;
13623 }
13624
13625 if (!base.has_value ())
13626 {
13627 /* We have no valid base address for the ranges
13628 data. */
13629 complaint (_("Invalid .debug_ranges data (no base address)"));
13630 return 0;
13631 }
13632
13633 if (range_beginning > range_end)
13634 {
13635 /* Inverted range entries are invalid. */
13636 complaint (_("Invalid .debug_ranges data (inverted range)"));
13637 return 0;
13638 }
13639
13640 /* Empty range entries have no effect. */
13641 if (range_beginning == range_end)
13642 continue;
13643
13644 range_beginning += *base;
13645 range_end += *base;
13646
13647 /* A not-uncommon case of bad debug info.
13648 Don't pollute the addrmap with bad data. */
13649 if (range_beginning + baseaddr == 0
13650 && !dwarf2_per_objfile->has_section_at_zero)
13651 {
13652 complaint (_(".debug_ranges entry has start address of zero"
13653 " [in module %s]"), objfile_name (objfile));
13654 continue;
13655 }
13656
13657 callback (range_beginning, range_end);
13658 }
13659
13660 return 1;
13661 }
13662
13663 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13664 Return 1 if the attributes are present and valid, otherwise, return 0.
13665 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13666
13667 static int
13668 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13669 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13670 dwarf2_psymtab *ranges_pst)
13671 {
13672 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13673 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13674 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13675 int low_set = 0;
13676 CORE_ADDR low = 0;
13677 CORE_ADDR high = 0;
13678 int retval;
13679
13680 retval = dwarf2_ranges_process (offset, cu,
13681 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13682 {
13683 if (ranges_pst != NULL)
13684 {
13685 CORE_ADDR lowpc;
13686 CORE_ADDR highpc;
13687
13688 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13689 range_beginning + baseaddr)
13690 - baseaddr);
13691 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13692 range_end + baseaddr)
13693 - baseaddr);
13694 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13695 lowpc, highpc - 1, ranges_pst);
13696 }
13697
13698 /* FIXME: This is recording everything as a low-high
13699 segment of consecutive addresses. We should have a
13700 data structure for discontiguous block ranges
13701 instead. */
13702 if (! low_set)
13703 {
13704 low = range_beginning;
13705 high = range_end;
13706 low_set = 1;
13707 }
13708 else
13709 {
13710 if (range_beginning < low)
13711 low = range_beginning;
13712 if (range_end > high)
13713 high = range_end;
13714 }
13715 });
13716 if (!retval)
13717 return 0;
13718
13719 if (! low_set)
13720 /* If the first entry is an end-of-list marker, the range
13721 describes an empty scope, i.e. no instructions. */
13722 return 0;
13723
13724 if (low_return)
13725 *low_return = low;
13726 if (high_return)
13727 *high_return = high;
13728 return 1;
13729 }
13730
13731 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13732 definition for the return value. *LOWPC and *HIGHPC are set iff
13733 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13734
13735 static enum pc_bounds_kind
13736 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13737 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13738 dwarf2_psymtab *pst)
13739 {
13740 struct dwarf2_per_objfile *dwarf2_per_objfile
13741 = cu->per_cu->dwarf2_per_objfile;
13742 struct attribute *attr;
13743 struct attribute *attr_high;
13744 CORE_ADDR low = 0;
13745 CORE_ADDR high = 0;
13746 enum pc_bounds_kind ret;
13747
13748 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13749 if (attr_high)
13750 {
13751 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13752 if (attr != nullptr)
13753 {
13754 low = attr->value_as_address ();
13755 high = attr_high->value_as_address ();
13756 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13757 high += low;
13758 }
13759 else
13760 /* Found high w/o low attribute. */
13761 return PC_BOUNDS_INVALID;
13762
13763 /* Found consecutive range of addresses. */
13764 ret = PC_BOUNDS_HIGH_LOW;
13765 }
13766 else
13767 {
13768 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13769 if (attr != NULL)
13770 {
13771 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13772 We take advantage of the fact that DW_AT_ranges does not appear
13773 in DW_TAG_compile_unit of DWO files. */
13774 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13775 unsigned int ranges_offset = (DW_UNSND (attr)
13776 + (need_ranges_base
13777 ? cu->ranges_base
13778 : 0));
13779
13780 /* Value of the DW_AT_ranges attribute is the offset in the
13781 .debug_ranges section. */
13782 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13783 return PC_BOUNDS_INVALID;
13784 /* Found discontinuous range of addresses. */
13785 ret = PC_BOUNDS_RANGES;
13786 }
13787 else
13788 return PC_BOUNDS_NOT_PRESENT;
13789 }
13790
13791 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13792 if (high <= low)
13793 return PC_BOUNDS_INVALID;
13794
13795 /* When using the GNU linker, .gnu.linkonce. sections are used to
13796 eliminate duplicate copies of functions and vtables and such.
13797 The linker will arbitrarily choose one and discard the others.
13798 The AT_*_pc values for such functions refer to local labels in
13799 these sections. If the section from that file was discarded, the
13800 labels are not in the output, so the relocs get a value of 0.
13801 If this is a discarded function, mark the pc bounds as invalid,
13802 so that GDB will ignore it. */
13803 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13804 return PC_BOUNDS_INVALID;
13805
13806 *lowpc = low;
13807 if (highpc)
13808 *highpc = high;
13809 return ret;
13810 }
13811
13812 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13813 its low and high PC addresses. Do nothing if these addresses could not
13814 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13815 and HIGHPC to the high address if greater than HIGHPC. */
13816
13817 static void
13818 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13819 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13820 struct dwarf2_cu *cu)
13821 {
13822 CORE_ADDR low, high;
13823 struct die_info *child = die->child;
13824
13825 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13826 {
13827 *lowpc = std::min (*lowpc, low);
13828 *highpc = std::max (*highpc, high);
13829 }
13830
13831 /* If the language does not allow nested subprograms (either inside
13832 subprograms or lexical blocks), we're done. */
13833 if (cu->language != language_ada)
13834 return;
13835
13836 /* Check all the children of the given DIE. If it contains nested
13837 subprograms, then check their pc bounds. Likewise, we need to
13838 check lexical blocks as well, as they may also contain subprogram
13839 definitions. */
13840 while (child && child->tag)
13841 {
13842 if (child->tag == DW_TAG_subprogram
13843 || child->tag == DW_TAG_lexical_block)
13844 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13845 child = child->sibling;
13846 }
13847 }
13848
13849 /* Get the low and high pc's represented by the scope DIE, and store
13850 them in *LOWPC and *HIGHPC. If the correct values can't be
13851 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13852
13853 static void
13854 get_scope_pc_bounds (struct die_info *die,
13855 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13856 struct dwarf2_cu *cu)
13857 {
13858 CORE_ADDR best_low = (CORE_ADDR) -1;
13859 CORE_ADDR best_high = (CORE_ADDR) 0;
13860 CORE_ADDR current_low, current_high;
13861
13862 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13863 >= PC_BOUNDS_RANGES)
13864 {
13865 best_low = current_low;
13866 best_high = current_high;
13867 }
13868 else
13869 {
13870 struct die_info *child = die->child;
13871
13872 while (child && child->tag)
13873 {
13874 switch (child->tag) {
13875 case DW_TAG_subprogram:
13876 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13877 break;
13878 case DW_TAG_namespace:
13879 case DW_TAG_module:
13880 /* FIXME: carlton/2004-01-16: Should we do this for
13881 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13882 that current GCC's always emit the DIEs corresponding
13883 to definitions of methods of classes as children of a
13884 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13885 the DIEs giving the declarations, which could be
13886 anywhere). But I don't see any reason why the
13887 standards says that they have to be there. */
13888 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13889
13890 if (current_low != ((CORE_ADDR) -1))
13891 {
13892 best_low = std::min (best_low, current_low);
13893 best_high = std::max (best_high, current_high);
13894 }
13895 break;
13896 default:
13897 /* Ignore. */
13898 break;
13899 }
13900
13901 child = child->sibling;
13902 }
13903 }
13904
13905 *lowpc = best_low;
13906 *highpc = best_high;
13907 }
13908
13909 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13910 in DIE. */
13911
13912 static void
13913 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13914 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13915 {
13916 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13917 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13918 struct attribute *attr;
13919 struct attribute *attr_high;
13920
13921 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13922 if (attr_high)
13923 {
13924 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13925 if (attr != nullptr)
13926 {
13927 CORE_ADDR low = attr->value_as_address ();
13928 CORE_ADDR high = attr_high->value_as_address ();
13929
13930 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13931 high += low;
13932
13933 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13934 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13935 cu->get_builder ()->record_block_range (block, low, high - 1);
13936 }
13937 }
13938
13939 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13940 if (attr != nullptr)
13941 {
13942 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13943 We take advantage of the fact that DW_AT_ranges does not appear
13944 in DW_TAG_compile_unit of DWO files. */
13945 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13946
13947 /* The value of the DW_AT_ranges attribute is the offset of the
13948 address range list in the .debug_ranges section. */
13949 unsigned long offset = (DW_UNSND (attr)
13950 + (need_ranges_base ? cu->ranges_base : 0));
13951
13952 std::vector<blockrange> blockvec;
13953 dwarf2_ranges_process (offset, cu,
13954 [&] (CORE_ADDR start, CORE_ADDR end)
13955 {
13956 start += baseaddr;
13957 end += baseaddr;
13958 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13959 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13960 cu->get_builder ()->record_block_range (block, start, end - 1);
13961 blockvec.emplace_back (start, end);
13962 });
13963
13964 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13965 }
13966 }
13967
13968 /* Check whether the producer field indicates either of GCC < 4.6, or the
13969 Intel C/C++ compiler, and cache the result in CU. */
13970
13971 static void
13972 check_producer (struct dwarf2_cu *cu)
13973 {
13974 int major, minor;
13975
13976 if (cu->producer == NULL)
13977 {
13978 /* For unknown compilers expect their behavior is DWARF version
13979 compliant.
13980
13981 GCC started to support .debug_types sections by -gdwarf-4 since
13982 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13983 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13984 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13985 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13986 }
13987 else if (producer_is_gcc (cu->producer, &major, &minor))
13988 {
13989 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13990 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13991 }
13992 else if (producer_is_icc (cu->producer, &major, &minor))
13993 {
13994 cu->producer_is_icc = true;
13995 cu->producer_is_icc_lt_14 = major < 14;
13996 }
13997 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13998 cu->producer_is_codewarrior = true;
13999 else
14000 {
14001 /* For other non-GCC compilers, expect their behavior is DWARF version
14002 compliant. */
14003 }
14004
14005 cu->checked_producer = true;
14006 }
14007
14008 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14009 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14010 during 4.6.0 experimental. */
14011
14012 static bool
14013 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14014 {
14015 if (!cu->checked_producer)
14016 check_producer (cu);
14017
14018 return cu->producer_is_gxx_lt_4_6;
14019 }
14020
14021
14022 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14023 with incorrect is_stmt attributes. */
14024
14025 static bool
14026 producer_is_codewarrior (struct dwarf2_cu *cu)
14027 {
14028 if (!cu->checked_producer)
14029 check_producer (cu);
14030
14031 return cu->producer_is_codewarrior;
14032 }
14033
14034 /* Return the default accessibility type if it is not overridden by
14035 DW_AT_accessibility. */
14036
14037 static enum dwarf_access_attribute
14038 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14039 {
14040 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14041 {
14042 /* The default DWARF 2 accessibility for members is public, the default
14043 accessibility for inheritance is private. */
14044
14045 if (die->tag != DW_TAG_inheritance)
14046 return DW_ACCESS_public;
14047 else
14048 return DW_ACCESS_private;
14049 }
14050 else
14051 {
14052 /* DWARF 3+ defines the default accessibility a different way. The same
14053 rules apply now for DW_TAG_inheritance as for the members and it only
14054 depends on the container kind. */
14055
14056 if (die->parent->tag == DW_TAG_class_type)
14057 return DW_ACCESS_private;
14058 else
14059 return DW_ACCESS_public;
14060 }
14061 }
14062
14063 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14064 offset. If the attribute was not found return 0, otherwise return
14065 1. If it was found but could not properly be handled, set *OFFSET
14066 to 0. */
14067
14068 static int
14069 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14070 LONGEST *offset)
14071 {
14072 struct attribute *attr;
14073
14074 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14075 if (attr != NULL)
14076 {
14077 *offset = 0;
14078
14079 /* Note that we do not check for a section offset first here.
14080 This is because DW_AT_data_member_location is new in DWARF 4,
14081 so if we see it, we can assume that a constant form is really
14082 a constant and not a section offset. */
14083 if (attr->form_is_constant ())
14084 *offset = attr->constant_value (0);
14085 else if (attr->form_is_section_offset ())
14086 dwarf2_complex_location_expr_complaint ();
14087 else if (attr->form_is_block ())
14088 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14089 else
14090 dwarf2_complex_location_expr_complaint ();
14091
14092 return 1;
14093 }
14094
14095 return 0;
14096 }
14097
14098 /* Add an aggregate field to the field list. */
14099
14100 static void
14101 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14102 struct dwarf2_cu *cu)
14103 {
14104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14106 struct nextfield *new_field;
14107 struct attribute *attr;
14108 struct field *fp;
14109 const char *fieldname = "";
14110
14111 if (die->tag == DW_TAG_inheritance)
14112 {
14113 fip->baseclasses.emplace_back ();
14114 new_field = &fip->baseclasses.back ();
14115 }
14116 else
14117 {
14118 fip->fields.emplace_back ();
14119 new_field = &fip->fields.back ();
14120 }
14121
14122 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14123 if (attr != nullptr)
14124 new_field->accessibility = DW_UNSND (attr);
14125 else
14126 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14127 if (new_field->accessibility != DW_ACCESS_public)
14128 fip->non_public_fields = 1;
14129
14130 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14131 if (attr != nullptr)
14132 new_field->virtuality = DW_UNSND (attr);
14133 else
14134 new_field->virtuality = DW_VIRTUALITY_none;
14135
14136 fp = &new_field->field;
14137
14138 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14139 {
14140 LONGEST offset;
14141
14142 /* Data member other than a C++ static data member. */
14143
14144 /* Get type of field. */
14145 fp->type = die_type (die, cu);
14146
14147 SET_FIELD_BITPOS (*fp, 0);
14148
14149 /* Get bit size of field (zero if none). */
14150 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14151 if (attr != nullptr)
14152 {
14153 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14154 }
14155 else
14156 {
14157 FIELD_BITSIZE (*fp) = 0;
14158 }
14159
14160 /* Get bit offset of field. */
14161 if (handle_data_member_location (die, cu, &offset))
14162 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14163 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14164 if (attr != nullptr)
14165 {
14166 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14167 {
14168 /* For big endian bits, the DW_AT_bit_offset gives the
14169 additional bit offset from the MSB of the containing
14170 anonymous object to the MSB of the field. We don't
14171 have to do anything special since we don't need to
14172 know the size of the anonymous object. */
14173 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14174 }
14175 else
14176 {
14177 /* For little endian bits, compute the bit offset to the
14178 MSB of the anonymous object, subtract off the number of
14179 bits from the MSB of the field to the MSB of the
14180 object, and then subtract off the number of bits of
14181 the field itself. The result is the bit offset of
14182 the LSB of the field. */
14183 int anonymous_size;
14184 int bit_offset = DW_UNSND (attr);
14185
14186 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14187 if (attr != nullptr)
14188 {
14189 /* The size of the anonymous object containing
14190 the bit field is explicit, so use the
14191 indicated size (in bytes). */
14192 anonymous_size = DW_UNSND (attr);
14193 }
14194 else
14195 {
14196 /* The size of the anonymous object containing
14197 the bit field must be inferred from the type
14198 attribute of the data member containing the
14199 bit field. */
14200 anonymous_size = TYPE_LENGTH (fp->type);
14201 }
14202 SET_FIELD_BITPOS (*fp,
14203 (FIELD_BITPOS (*fp)
14204 + anonymous_size * bits_per_byte
14205 - bit_offset - FIELD_BITSIZE (*fp)));
14206 }
14207 }
14208 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14209 if (attr != NULL)
14210 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14211 + attr->constant_value (0)));
14212
14213 /* Get name of field. */
14214 fieldname = dwarf2_name (die, cu);
14215 if (fieldname == NULL)
14216 fieldname = "";
14217
14218 /* The name is already allocated along with this objfile, so we don't
14219 need to duplicate it for the type. */
14220 fp->name = fieldname;
14221
14222 /* Change accessibility for artificial fields (e.g. virtual table
14223 pointer or virtual base class pointer) to private. */
14224 if (dwarf2_attr (die, DW_AT_artificial, cu))
14225 {
14226 FIELD_ARTIFICIAL (*fp) = 1;
14227 new_field->accessibility = DW_ACCESS_private;
14228 fip->non_public_fields = 1;
14229 }
14230 }
14231 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14232 {
14233 /* C++ static member. */
14234
14235 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14236 is a declaration, but all versions of G++ as of this writing
14237 (so through at least 3.2.1) incorrectly generate
14238 DW_TAG_variable tags. */
14239
14240 const char *physname;
14241
14242 /* Get name of field. */
14243 fieldname = dwarf2_name (die, cu);
14244 if (fieldname == NULL)
14245 return;
14246
14247 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14248 if (attr
14249 /* Only create a symbol if this is an external value.
14250 new_symbol checks this and puts the value in the global symbol
14251 table, which we want. If it is not external, new_symbol
14252 will try to put the value in cu->list_in_scope which is wrong. */
14253 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14254 {
14255 /* A static const member, not much different than an enum as far as
14256 we're concerned, except that we can support more types. */
14257 new_symbol (die, NULL, cu);
14258 }
14259
14260 /* Get physical name. */
14261 physname = dwarf2_physname (fieldname, die, cu);
14262
14263 /* The name is already allocated along with this objfile, so we don't
14264 need to duplicate it for the type. */
14265 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14266 FIELD_TYPE (*fp) = die_type (die, cu);
14267 FIELD_NAME (*fp) = fieldname;
14268 }
14269 else if (die->tag == DW_TAG_inheritance)
14270 {
14271 LONGEST offset;
14272
14273 /* C++ base class field. */
14274 if (handle_data_member_location (die, cu, &offset))
14275 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14276 FIELD_BITSIZE (*fp) = 0;
14277 FIELD_TYPE (*fp) = die_type (die, cu);
14278 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14279 }
14280 else if (die->tag == DW_TAG_variant_part)
14281 {
14282 /* process_structure_scope will treat this DIE as a union. */
14283 process_structure_scope (die, cu);
14284
14285 /* The variant part is relative to the start of the enclosing
14286 structure. */
14287 SET_FIELD_BITPOS (*fp, 0);
14288 fp->type = get_die_type (die, cu);
14289 fp->artificial = 1;
14290 fp->name = "<<variant>>";
14291
14292 /* Normally a DW_TAG_variant_part won't have a size, but our
14293 representation requires one, so set it to the maximum of the
14294 child sizes, being sure to account for the offset at which
14295 each child is seen. */
14296 if (TYPE_LENGTH (fp->type) == 0)
14297 {
14298 unsigned max = 0;
14299 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14300 {
14301 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14302 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14303 if (len > max)
14304 max = len;
14305 }
14306 TYPE_LENGTH (fp->type) = max;
14307 }
14308 }
14309 else
14310 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14311 }
14312
14313 /* Can the type given by DIE define another type? */
14314
14315 static bool
14316 type_can_define_types (const struct die_info *die)
14317 {
14318 switch (die->tag)
14319 {
14320 case DW_TAG_typedef:
14321 case DW_TAG_class_type:
14322 case DW_TAG_structure_type:
14323 case DW_TAG_union_type:
14324 case DW_TAG_enumeration_type:
14325 return true;
14326
14327 default:
14328 return false;
14329 }
14330 }
14331
14332 /* Add a type definition defined in the scope of the FIP's class. */
14333
14334 static void
14335 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14336 struct dwarf2_cu *cu)
14337 {
14338 struct decl_field fp;
14339 memset (&fp, 0, sizeof (fp));
14340
14341 gdb_assert (type_can_define_types (die));
14342
14343 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14344 fp.name = dwarf2_name (die, cu);
14345 fp.type = read_type_die (die, cu);
14346
14347 /* Save accessibility. */
14348 enum dwarf_access_attribute accessibility;
14349 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14350 if (attr != NULL)
14351 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14352 else
14353 accessibility = dwarf2_default_access_attribute (die, cu);
14354 switch (accessibility)
14355 {
14356 case DW_ACCESS_public:
14357 /* The assumed value if neither private nor protected. */
14358 break;
14359 case DW_ACCESS_private:
14360 fp.is_private = 1;
14361 break;
14362 case DW_ACCESS_protected:
14363 fp.is_protected = 1;
14364 break;
14365 default:
14366 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14367 }
14368
14369 if (die->tag == DW_TAG_typedef)
14370 fip->typedef_field_list.push_back (fp);
14371 else
14372 fip->nested_types_list.push_back (fp);
14373 }
14374
14375 /* Create the vector of fields, and attach it to the type. */
14376
14377 static void
14378 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14379 struct dwarf2_cu *cu)
14380 {
14381 int nfields = fip->nfields ();
14382
14383 /* Record the field count, allocate space for the array of fields,
14384 and create blank accessibility bitfields if necessary. */
14385 TYPE_NFIELDS (type) = nfields;
14386 TYPE_FIELDS (type) = (struct field *)
14387 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14388
14389 if (fip->non_public_fields && cu->language != language_ada)
14390 {
14391 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14392
14393 TYPE_FIELD_PRIVATE_BITS (type) =
14394 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14395 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14396
14397 TYPE_FIELD_PROTECTED_BITS (type) =
14398 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14399 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14400
14401 TYPE_FIELD_IGNORE_BITS (type) =
14402 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14403 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14404 }
14405
14406 /* If the type has baseclasses, allocate and clear a bit vector for
14407 TYPE_FIELD_VIRTUAL_BITS. */
14408 if (!fip->baseclasses.empty () && cu->language != language_ada)
14409 {
14410 int num_bytes = B_BYTES (fip->baseclasses.size ());
14411 unsigned char *pointer;
14412
14413 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14414 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14415 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14416 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14417 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14418 }
14419
14420 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14421 {
14422 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14423
14424 for (int index = 0; index < nfields; ++index)
14425 {
14426 struct nextfield &field = fip->fields[index];
14427
14428 if (field.variant.is_discriminant)
14429 di->discriminant_index = index;
14430 else if (field.variant.default_branch)
14431 di->default_index = index;
14432 else
14433 di->discriminants[index] = field.variant.discriminant_value;
14434 }
14435 }
14436
14437 /* Copy the saved-up fields into the field vector. */
14438 for (int i = 0; i < nfields; ++i)
14439 {
14440 struct nextfield &field
14441 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14442 : fip->fields[i - fip->baseclasses.size ()]);
14443
14444 TYPE_FIELD (type, i) = field.field;
14445 switch (field.accessibility)
14446 {
14447 case DW_ACCESS_private:
14448 if (cu->language != language_ada)
14449 SET_TYPE_FIELD_PRIVATE (type, i);
14450 break;
14451
14452 case DW_ACCESS_protected:
14453 if (cu->language != language_ada)
14454 SET_TYPE_FIELD_PROTECTED (type, i);
14455 break;
14456
14457 case DW_ACCESS_public:
14458 break;
14459
14460 default:
14461 /* Unknown accessibility. Complain and treat it as public. */
14462 {
14463 complaint (_("unsupported accessibility %d"),
14464 field.accessibility);
14465 }
14466 break;
14467 }
14468 if (i < fip->baseclasses.size ())
14469 {
14470 switch (field.virtuality)
14471 {
14472 case DW_VIRTUALITY_virtual:
14473 case DW_VIRTUALITY_pure_virtual:
14474 if (cu->language == language_ada)
14475 error (_("unexpected virtuality in component of Ada type"));
14476 SET_TYPE_FIELD_VIRTUAL (type, i);
14477 break;
14478 }
14479 }
14480 }
14481 }
14482
14483 /* Return true if this member function is a constructor, false
14484 otherwise. */
14485
14486 static int
14487 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14488 {
14489 const char *fieldname;
14490 const char *type_name;
14491 int len;
14492
14493 if (die->parent == NULL)
14494 return 0;
14495
14496 if (die->parent->tag != DW_TAG_structure_type
14497 && die->parent->tag != DW_TAG_union_type
14498 && die->parent->tag != DW_TAG_class_type)
14499 return 0;
14500
14501 fieldname = dwarf2_name (die, cu);
14502 type_name = dwarf2_name (die->parent, cu);
14503 if (fieldname == NULL || type_name == NULL)
14504 return 0;
14505
14506 len = strlen (fieldname);
14507 return (strncmp (fieldname, type_name, len) == 0
14508 && (type_name[len] == '\0' || type_name[len] == '<'));
14509 }
14510
14511 /* Check if the given VALUE is a recognized enum
14512 dwarf_defaulted_attribute constant according to DWARF5 spec,
14513 Table 7.24. */
14514
14515 static bool
14516 is_valid_DW_AT_defaulted (ULONGEST value)
14517 {
14518 switch (value)
14519 {
14520 case DW_DEFAULTED_no:
14521 case DW_DEFAULTED_in_class:
14522 case DW_DEFAULTED_out_of_class:
14523 return true;
14524 }
14525
14526 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14527 return false;
14528 }
14529
14530 /* Add a member function to the proper fieldlist. */
14531
14532 static void
14533 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14534 struct type *type, struct dwarf2_cu *cu)
14535 {
14536 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14537 struct attribute *attr;
14538 int i;
14539 struct fnfieldlist *flp = nullptr;
14540 struct fn_field *fnp;
14541 const char *fieldname;
14542 struct type *this_type;
14543 enum dwarf_access_attribute accessibility;
14544
14545 if (cu->language == language_ada)
14546 error (_("unexpected member function in Ada type"));
14547
14548 /* Get name of member function. */
14549 fieldname = dwarf2_name (die, cu);
14550 if (fieldname == NULL)
14551 return;
14552
14553 /* Look up member function name in fieldlist. */
14554 for (i = 0; i < fip->fnfieldlists.size (); i++)
14555 {
14556 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14557 {
14558 flp = &fip->fnfieldlists[i];
14559 break;
14560 }
14561 }
14562
14563 /* Create a new fnfieldlist if necessary. */
14564 if (flp == nullptr)
14565 {
14566 fip->fnfieldlists.emplace_back ();
14567 flp = &fip->fnfieldlists.back ();
14568 flp->name = fieldname;
14569 i = fip->fnfieldlists.size () - 1;
14570 }
14571
14572 /* Create a new member function field and add it to the vector of
14573 fnfieldlists. */
14574 flp->fnfields.emplace_back ();
14575 fnp = &flp->fnfields.back ();
14576
14577 /* Delay processing of the physname until later. */
14578 if (cu->language == language_cplus)
14579 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14580 die, cu);
14581 else
14582 {
14583 const char *physname = dwarf2_physname (fieldname, die, cu);
14584 fnp->physname = physname ? physname : "";
14585 }
14586
14587 fnp->type = alloc_type (objfile);
14588 this_type = read_type_die (die, cu);
14589 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14590 {
14591 int nparams = TYPE_NFIELDS (this_type);
14592
14593 /* TYPE is the domain of this method, and THIS_TYPE is the type
14594 of the method itself (TYPE_CODE_METHOD). */
14595 smash_to_method_type (fnp->type, type,
14596 TYPE_TARGET_TYPE (this_type),
14597 TYPE_FIELDS (this_type),
14598 TYPE_NFIELDS (this_type),
14599 TYPE_VARARGS (this_type));
14600
14601 /* Handle static member functions.
14602 Dwarf2 has no clean way to discern C++ static and non-static
14603 member functions. G++ helps GDB by marking the first
14604 parameter for non-static member functions (which is the this
14605 pointer) as artificial. We obtain this information from
14606 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14607 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14608 fnp->voffset = VOFFSET_STATIC;
14609 }
14610 else
14611 complaint (_("member function type missing for '%s'"),
14612 dwarf2_full_name (fieldname, die, cu));
14613
14614 /* Get fcontext from DW_AT_containing_type if present. */
14615 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14616 fnp->fcontext = die_containing_type (die, cu);
14617
14618 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14619 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14620
14621 /* Get accessibility. */
14622 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14623 if (attr != nullptr)
14624 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14625 else
14626 accessibility = dwarf2_default_access_attribute (die, cu);
14627 switch (accessibility)
14628 {
14629 case DW_ACCESS_private:
14630 fnp->is_private = 1;
14631 break;
14632 case DW_ACCESS_protected:
14633 fnp->is_protected = 1;
14634 break;
14635 }
14636
14637 /* Check for artificial methods. */
14638 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14639 if (attr && DW_UNSND (attr) != 0)
14640 fnp->is_artificial = 1;
14641
14642 /* Check for defaulted methods. */
14643 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14644 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14645 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14646
14647 /* Check for deleted methods. */
14648 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14649 if (attr != nullptr && DW_UNSND (attr) != 0)
14650 fnp->is_deleted = 1;
14651
14652 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14653
14654 /* Get index in virtual function table if it is a virtual member
14655 function. For older versions of GCC, this is an offset in the
14656 appropriate virtual table, as specified by DW_AT_containing_type.
14657 For everyone else, it is an expression to be evaluated relative
14658 to the object address. */
14659
14660 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14661 if (attr != nullptr)
14662 {
14663 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14664 {
14665 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14666 {
14667 /* Old-style GCC. */
14668 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14669 }
14670 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14671 || (DW_BLOCK (attr)->size > 1
14672 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14673 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14674 {
14675 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14676 if ((fnp->voffset % cu->header.addr_size) != 0)
14677 dwarf2_complex_location_expr_complaint ();
14678 else
14679 fnp->voffset /= cu->header.addr_size;
14680 fnp->voffset += 2;
14681 }
14682 else
14683 dwarf2_complex_location_expr_complaint ();
14684
14685 if (!fnp->fcontext)
14686 {
14687 /* If there is no `this' field and no DW_AT_containing_type,
14688 we cannot actually find a base class context for the
14689 vtable! */
14690 if (TYPE_NFIELDS (this_type) == 0
14691 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14692 {
14693 complaint (_("cannot determine context for virtual member "
14694 "function \"%s\" (offset %s)"),
14695 fieldname, sect_offset_str (die->sect_off));
14696 }
14697 else
14698 {
14699 fnp->fcontext
14700 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14701 }
14702 }
14703 }
14704 else if (attr->form_is_section_offset ())
14705 {
14706 dwarf2_complex_location_expr_complaint ();
14707 }
14708 else
14709 {
14710 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14711 fieldname);
14712 }
14713 }
14714 else
14715 {
14716 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14717 if (attr && DW_UNSND (attr))
14718 {
14719 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14720 complaint (_("Member function \"%s\" (offset %s) is virtual "
14721 "but the vtable offset is not specified"),
14722 fieldname, sect_offset_str (die->sect_off));
14723 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14724 TYPE_CPLUS_DYNAMIC (type) = 1;
14725 }
14726 }
14727 }
14728
14729 /* Create the vector of member function fields, and attach it to the type. */
14730
14731 static void
14732 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14733 struct dwarf2_cu *cu)
14734 {
14735 if (cu->language == language_ada)
14736 error (_("unexpected member functions in Ada type"));
14737
14738 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14739 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14740 TYPE_ALLOC (type,
14741 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14742
14743 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14744 {
14745 struct fnfieldlist &nf = fip->fnfieldlists[i];
14746 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14747
14748 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14749 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14750 fn_flp->fn_fields = (struct fn_field *)
14751 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14752
14753 for (int k = 0; k < nf.fnfields.size (); ++k)
14754 fn_flp->fn_fields[k] = nf.fnfields[k];
14755 }
14756
14757 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14758 }
14759
14760 /* Returns non-zero if NAME is the name of a vtable member in CU's
14761 language, zero otherwise. */
14762 static int
14763 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14764 {
14765 static const char vptr[] = "_vptr";
14766
14767 /* Look for the C++ form of the vtable. */
14768 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14769 return 1;
14770
14771 return 0;
14772 }
14773
14774 /* GCC outputs unnamed structures that are really pointers to member
14775 functions, with the ABI-specified layout. If TYPE describes
14776 such a structure, smash it into a member function type.
14777
14778 GCC shouldn't do this; it should just output pointer to member DIEs.
14779 This is GCC PR debug/28767. */
14780
14781 static void
14782 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14783 {
14784 struct type *pfn_type, *self_type, *new_type;
14785
14786 /* Check for a structure with no name and two children. */
14787 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14788 return;
14789
14790 /* Check for __pfn and __delta members. */
14791 if (TYPE_FIELD_NAME (type, 0) == NULL
14792 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14793 || TYPE_FIELD_NAME (type, 1) == NULL
14794 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14795 return;
14796
14797 /* Find the type of the method. */
14798 pfn_type = TYPE_FIELD_TYPE (type, 0);
14799 if (pfn_type == NULL
14800 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14801 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14802 return;
14803
14804 /* Look for the "this" argument. */
14805 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14806 if (TYPE_NFIELDS (pfn_type) == 0
14807 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14808 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14809 return;
14810
14811 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14812 new_type = alloc_type (objfile);
14813 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14814 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14815 TYPE_VARARGS (pfn_type));
14816 smash_to_methodptr_type (type, new_type);
14817 }
14818
14819 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14820 appropriate error checking and issuing complaints if there is a
14821 problem. */
14822
14823 static ULONGEST
14824 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14825 {
14826 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14827
14828 if (attr == nullptr)
14829 return 0;
14830
14831 if (!attr->form_is_constant ())
14832 {
14833 complaint (_("DW_AT_alignment must have constant form"
14834 " - DIE at %s [in module %s]"),
14835 sect_offset_str (die->sect_off),
14836 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14837 return 0;
14838 }
14839
14840 ULONGEST align;
14841 if (attr->form == DW_FORM_sdata)
14842 {
14843 LONGEST val = DW_SND (attr);
14844 if (val < 0)
14845 {
14846 complaint (_("DW_AT_alignment value must not be negative"
14847 " - DIE at %s [in module %s]"),
14848 sect_offset_str (die->sect_off),
14849 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14850 return 0;
14851 }
14852 align = val;
14853 }
14854 else
14855 align = DW_UNSND (attr);
14856
14857 if (align == 0)
14858 {
14859 complaint (_("DW_AT_alignment value must not be zero"
14860 " - DIE at %s [in module %s]"),
14861 sect_offset_str (die->sect_off),
14862 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14863 return 0;
14864 }
14865 if ((align & (align - 1)) != 0)
14866 {
14867 complaint (_("DW_AT_alignment value must be a power of 2"
14868 " - DIE at %s [in module %s]"),
14869 sect_offset_str (die->sect_off),
14870 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14871 return 0;
14872 }
14873
14874 return align;
14875 }
14876
14877 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14878 the alignment for TYPE. */
14879
14880 static void
14881 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14882 struct type *type)
14883 {
14884 if (!set_type_align (type, get_alignment (cu, die)))
14885 complaint (_("DW_AT_alignment value too large"
14886 " - DIE at %s [in module %s]"),
14887 sect_offset_str (die->sect_off),
14888 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14889 }
14890
14891 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14892 constant for a type, according to DWARF5 spec, Table 5.5. */
14893
14894 static bool
14895 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14896 {
14897 switch (value)
14898 {
14899 case DW_CC_normal:
14900 case DW_CC_pass_by_reference:
14901 case DW_CC_pass_by_value:
14902 return true;
14903
14904 default:
14905 complaint (_("unrecognized DW_AT_calling_convention value "
14906 "(%s) for a type"), pulongest (value));
14907 return false;
14908 }
14909 }
14910
14911 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14912 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14913 also according to GNU-specific values (see include/dwarf2.h). */
14914
14915 static bool
14916 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14917 {
14918 switch (value)
14919 {
14920 case DW_CC_normal:
14921 case DW_CC_program:
14922 case DW_CC_nocall:
14923 return true;
14924
14925 case DW_CC_GNU_renesas_sh:
14926 case DW_CC_GNU_borland_fastcall_i386:
14927 case DW_CC_GDB_IBM_OpenCL:
14928 return true;
14929
14930 default:
14931 complaint (_("unrecognized DW_AT_calling_convention value "
14932 "(%s) for a subroutine"), pulongest (value));
14933 return false;
14934 }
14935 }
14936
14937 /* Called when we find the DIE that starts a structure or union scope
14938 (definition) to create a type for the structure or union. Fill in
14939 the type's name and general properties; the members will not be
14940 processed until process_structure_scope. A symbol table entry for
14941 the type will also not be done until process_structure_scope (assuming
14942 the type has a name).
14943
14944 NOTE: we need to call these functions regardless of whether or not the
14945 DIE has a DW_AT_name attribute, since it might be an anonymous
14946 structure or union. This gets the type entered into our set of
14947 user defined types. */
14948
14949 static struct type *
14950 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14951 {
14952 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14953 struct type *type;
14954 struct attribute *attr;
14955 const char *name;
14956
14957 /* If the definition of this type lives in .debug_types, read that type.
14958 Don't follow DW_AT_specification though, that will take us back up
14959 the chain and we want to go down. */
14960 attr = die->attr (DW_AT_signature);
14961 if (attr != nullptr)
14962 {
14963 type = get_DW_AT_signature_type (die, attr, cu);
14964
14965 /* The type's CU may not be the same as CU.
14966 Ensure TYPE is recorded with CU in die_type_hash. */
14967 return set_die_type (die, type, cu);
14968 }
14969
14970 type = alloc_type (objfile);
14971 INIT_CPLUS_SPECIFIC (type);
14972
14973 name = dwarf2_name (die, cu);
14974 if (name != NULL)
14975 {
14976 if (cu->language == language_cplus
14977 || cu->language == language_d
14978 || cu->language == language_rust)
14979 {
14980 const char *full_name = dwarf2_full_name (name, die, cu);
14981
14982 /* dwarf2_full_name might have already finished building the DIE's
14983 type. If so, there is no need to continue. */
14984 if (get_die_type (die, cu) != NULL)
14985 return get_die_type (die, cu);
14986
14987 TYPE_NAME (type) = full_name;
14988 }
14989 else
14990 {
14991 /* The name is already allocated along with this objfile, so
14992 we don't need to duplicate it for the type. */
14993 TYPE_NAME (type) = name;
14994 }
14995 }
14996
14997 if (die->tag == DW_TAG_structure_type)
14998 {
14999 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15000 }
15001 else if (die->tag == DW_TAG_union_type)
15002 {
15003 TYPE_CODE (type) = TYPE_CODE_UNION;
15004 }
15005 else if (die->tag == DW_TAG_variant_part)
15006 {
15007 TYPE_CODE (type) = TYPE_CODE_UNION;
15008 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15009 }
15010 else
15011 {
15012 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15013 }
15014
15015 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15016 TYPE_DECLARED_CLASS (type) = 1;
15017
15018 /* Store the calling convention in the type if it's available in
15019 the die. Otherwise the calling convention remains set to
15020 the default value DW_CC_normal. */
15021 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15022 if (attr != nullptr
15023 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15024 {
15025 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15026 TYPE_CPLUS_CALLING_CONVENTION (type)
15027 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15028 }
15029
15030 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15031 if (attr != nullptr)
15032 {
15033 if (attr->form_is_constant ())
15034 TYPE_LENGTH (type) = DW_UNSND (attr);
15035 else
15036 {
15037 /* For the moment, dynamic type sizes are not supported
15038 by GDB's struct type. The actual size is determined
15039 on-demand when resolving the type of a given object,
15040 so set the type's length to zero for now. Otherwise,
15041 we record an expression as the length, and that expression
15042 could lead to a very large value, which could eventually
15043 lead to us trying to allocate that much memory when creating
15044 a value of that type. */
15045 TYPE_LENGTH (type) = 0;
15046 }
15047 }
15048 else
15049 {
15050 TYPE_LENGTH (type) = 0;
15051 }
15052
15053 maybe_set_alignment (cu, die, type);
15054
15055 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15056 {
15057 /* ICC<14 does not output the required DW_AT_declaration on
15058 incomplete types, but gives them a size of zero. */
15059 TYPE_STUB (type) = 1;
15060 }
15061 else
15062 TYPE_STUB_SUPPORTED (type) = 1;
15063
15064 if (die_is_declaration (die, cu))
15065 TYPE_STUB (type) = 1;
15066 else if (attr == NULL && die->child == NULL
15067 && producer_is_realview (cu->producer))
15068 /* RealView does not output the required DW_AT_declaration
15069 on incomplete types. */
15070 TYPE_STUB (type) = 1;
15071
15072 /* We need to add the type field to the die immediately so we don't
15073 infinitely recurse when dealing with pointers to the structure
15074 type within the structure itself. */
15075 set_die_type (die, type, cu);
15076
15077 /* set_die_type should be already done. */
15078 set_descriptive_type (type, die, cu);
15079
15080 return type;
15081 }
15082
15083 /* A helper for process_structure_scope that handles a single member
15084 DIE. */
15085
15086 static void
15087 handle_struct_member_die (struct die_info *child_die, struct type *type,
15088 struct field_info *fi,
15089 std::vector<struct symbol *> *template_args,
15090 struct dwarf2_cu *cu)
15091 {
15092 if (child_die->tag == DW_TAG_member
15093 || child_die->tag == DW_TAG_variable
15094 || child_die->tag == DW_TAG_variant_part)
15095 {
15096 /* NOTE: carlton/2002-11-05: A C++ static data member
15097 should be a DW_TAG_member that is a declaration, but
15098 all versions of G++ as of this writing (so through at
15099 least 3.2.1) incorrectly generate DW_TAG_variable
15100 tags for them instead. */
15101 dwarf2_add_field (fi, child_die, cu);
15102 }
15103 else if (child_die->tag == DW_TAG_subprogram)
15104 {
15105 /* Rust doesn't have member functions in the C++ sense.
15106 However, it does emit ordinary functions as children
15107 of a struct DIE. */
15108 if (cu->language == language_rust)
15109 read_func_scope (child_die, cu);
15110 else
15111 {
15112 /* C++ member function. */
15113 dwarf2_add_member_fn (fi, child_die, type, cu);
15114 }
15115 }
15116 else if (child_die->tag == DW_TAG_inheritance)
15117 {
15118 /* C++ base class field. */
15119 dwarf2_add_field (fi, child_die, cu);
15120 }
15121 else if (type_can_define_types (child_die))
15122 dwarf2_add_type_defn (fi, child_die, cu);
15123 else if (child_die->tag == DW_TAG_template_type_param
15124 || child_die->tag == DW_TAG_template_value_param)
15125 {
15126 struct symbol *arg = new_symbol (child_die, NULL, cu);
15127
15128 if (arg != NULL)
15129 template_args->push_back (arg);
15130 }
15131 else if (child_die->tag == DW_TAG_variant)
15132 {
15133 /* In a variant we want to get the discriminant and also add a
15134 field for our sole member child. */
15135 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15136
15137 for (die_info *variant_child = child_die->child;
15138 variant_child != NULL;
15139 variant_child = variant_child->sibling)
15140 {
15141 if (variant_child->tag == DW_TAG_member)
15142 {
15143 handle_struct_member_die (variant_child, type, fi,
15144 template_args, cu);
15145 /* Only handle the one. */
15146 break;
15147 }
15148 }
15149
15150 /* We don't handle this but we might as well report it if we see
15151 it. */
15152 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15153 complaint (_("DW_AT_discr_list is not supported yet"
15154 " - DIE at %s [in module %s]"),
15155 sect_offset_str (child_die->sect_off),
15156 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15157
15158 /* The first field was just added, so we can stash the
15159 discriminant there. */
15160 gdb_assert (!fi->fields.empty ());
15161 if (discr == NULL)
15162 fi->fields.back ().variant.default_branch = true;
15163 else
15164 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15165 }
15166 }
15167
15168 /* Finish creating a structure or union type, including filling in
15169 its members and creating a symbol for it. */
15170
15171 static void
15172 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15173 {
15174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15175 struct die_info *child_die;
15176 struct type *type;
15177
15178 type = get_die_type (die, cu);
15179 if (type == NULL)
15180 type = read_structure_type (die, cu);
15181
15182 /* When reading a DW_TAG_variant_part, we need to notice when we
15183 read the discriminant member, so we can record it later in the
15184 discriminant_info. */
15185 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15186 sect_offset discr_offset {};
15187 bool has_template_parameters = false;
15188
15189 if (is_variant_part)
15190 {
15191 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15192 if (discr == NULL)
15193 {
15194 /* Maybe it's a univariant form, an extension we support.
15195 In this case arrange not to check the offset. */
15196 is_variant_part = false;
15197 }
15198 else if (discr->form_is_ref ())
15199 {
15200 struct dwarf2_cu *target_cu = cu;
15201 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15202
15203 discr_offset = target_die->sect_off;
15204 }
15205 else
15206 {
15207 complaint (_("DW_AT_discr does not have DIE reference form"
15208 " - DIE at %s [in module %s]"),
15209 sect_offset_str (die->sect_off),
15210 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15211 is_variant_part = false;
15212 }
15213 }
15214
15215 if (die->child != NULL && ! die_is_declaration (die, cu))
15216 {
15217 struct field_info fi;
15218 std::vector<struct symbol *> template_args;
15219
15220 child_die = die->child;
15221
15222 while (child_die && child_die->tag)
15223 {
15224 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15225
15226 if (is_variant_part && discr_offset == child_die->sect_off)
15227 fi.fields.back ().variant.is_discriminant = true;
15228
15229 child_die = child_die->sibling;
15230 }
15231
15232 /* Attach template arguments to type. */
15233 if (!template_args.empty ())
15234 {
15235 has_template_parameters = true;
15236 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15237 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15238 TYPE_TEMPLATE_ARGUMENTS (type)
15239 = XOBNEWVEC (&objfile->objfile_obstack,
15240 struct symbol *,
15241 TYPE_N_TEMPLATE_ARGUMENTS (type));
15242 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15243 template_args.data (),
15244 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15245 * sizeof (struct symbol *)));
15246 }
15247
15248 /* Attach fields and member functions to the type. */
15249 if (fi.nfields () > 0)
15250 dwarf2_attach_fields_to_type (&fi, type, cu);
15251 if (!fi.fnfieldlists.empty ())
15252 {
15253 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15254
15255 /* Get the type which refers to the base class (possibly this
15256 class itself) which contains the vtable pointer for the current
15257 class from the DW_AT_containing_type attribute. This use of
15258 DW_AT_containing_type is a GNU extension. */
15259
15260 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15261 {
15262 struct type *t = die_containing_type (die, cu);
15263
15264 set_type_vptr_basetype (type, t);
15265 if (type == t)
15266 {
15267 int i;
15268
15269 /* Our own class provides vtbl ptr. */
15270 for (i = TYPE_NFIELDS (t) - 1;
15271 i >= TYPE_N_BASECLASSES (t);
15272 --i)
15273 {
15274 const char *fieldname = TYPE_FIELD_NAME (t, i);
15275
15276 if (is_vtable_name (fieldname, cu))
15277 {
15278 set_type_vptr_fieldno (type, i);
15279 break;
15280 }
15281 }
15282
15283 /* Complain if virtual function table field not found. */
15284 if (i < TYPE_N_BASECLASSES (t))
15285 complaint (_("virtual function table pointer "
15286 "not found when defining class '%s'"),
15287 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15288 }
15289 else
15290 {
15291 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15292 }
15293 }
15294 else if (cu->producer
15295 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15296 {
15297 /* The IBM XLC compiler does not provide direct indication
15298 of the containing type, but the vtable pointer is
15299 always named __vfp. */
15300
15301 int i;
15302
15303 for (i = TYPE_NFIELDS (type) - 1;
15304 i >= TYPE_N_BASECLASSES (type);
15305 --i)
15306 {
15307 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15308 {
15309 set_type_vptr_fieldno (type, i);
15310 set_type_vptr_basetype (type, type);
15311 break;
15312 }
15313 }
15314 }
15315 }
15316
15317 /* Copy fi.typedef_field_list linked list elements content into the
15318 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15319 if (!fi.typedef_field_list.empty ())
15320 {
15321 int count = fi.typedef_field_list.size ();
15322
15323 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15324 TYPE_TYPEDEF_FIELD_ARRAY (type)
15325 = ((struct decl_field *)
15326 TYPE_ALLOC (type,
15327 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15328 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15329
15330 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15331 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15332 }
15333
15334 /* Copy fi.nested_types_list linked list elements content into the
15335 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15336 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15337 {
15338 int count = fi.nested_types_list.size ();
15339
15340 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15341 TYPE_NESTED_TYPES_ARRAY (type)
15342 = ((struct decl_field *)
15343 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15344 TYPE_NESTED_TYPES_COUNT (type) = count;
15345
15346 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15347 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15348 }
15349 }
15350
15351 quirk_gcc_member_function_pointer (type, objfile);
15352 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15353 cu->rust_unions.push_back (type);
15354
15355 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15356 snapshots) has been known to create a die giving a declaration
15357 for a class that has, as a child, a die giving a definition for a
15358 nested class. So we have to process our children even if the
15359 current die is a declaration. Normally, of course, a declaration
15360 won't have any children at all. */
15361
15362 child_die = die->child;
15363
15364 while (child_die != NULL && child_die->tag)
15365 {
15366 if (child_die->tag == DW_TAG_member
15367 || child_die->tag == DW_TAG_variable
15368 || child_die->tag == DW_TAG_inheritance
15369 || child_die->tag == DW_TAG_template_value_param
15370 || child_die->tag == DW_TAG_template_type_param)
15371 {
15372 /* Do nothing. */
15373 }
15374 else
15375 process_die (child_die, cu);
15376
15377 child_die = child_die->sibling;
15378 }
15379
15380 /* Do not consider external references. According to the DWARF standard,
15381 these DIEs are identified by the fact that they have no byte_size
15382 attribute, and a declaration attribute. */
15383 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15384 || !die_is_declaration (die, cu))
15385 {
15386 struct symbol *sym = new_symbol (die, type, cu);
15387
15388 if (has_template_parameters)
15389 {
15390 struct symtab *symtab;
15391 if (sym != nullptr)
15392 symtab = symbol_symtab (sym);
15393 else if (cu->line_header != nullptr)
15394 {
15395 /* Any related symtab will do. */
15396 symtab
15397 = cu->line_header->file_names ()[0].symtab;
15398 }
15399 else
15400 {
15401 symtab = nullptr;
15402 complaint (_("could not find suitable "
15403 "symtab for template parameter"
15404 " - DIE at %s [in module %s]"),
15405 sect_offset_str (die->sect_off),
15406 objfile_name (objfile));
15407 }
15408
15409 if (symtab != nullptr)
15410 {
15411 /* Make sure that the symtab is set on the new symbols.
15412 Even though they don't appear in this symtab directly,
15413 other parts of gdb assume that symbols do, and this is
15414 reasonably true. */
15415 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15416 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15417 }
15418 }
15419 }
15420 }
15421
15422 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15423 update TYPE using some information only available in DIE's children. */
15424
15425 static void
15426 update_enumeration_type_from_children (struct die_info *die,
15427 struct type *type,
15428 struct dwarf2_cu *cu)
15429 {
15430 struct die_info *child_die;
15431 int unsigned_enum = 1;
15432 int flag_enum = 1;
15433
15434 auto_obstack obstack;
15435
15436 for (child_die = die->child;
15437 child_die != NULL && child_die->tag;
15438 child_die = child_die->sibling)
15439 {
15440 struct attribute *attr;
15441 LONGEST value;
15442 const gdb_byte *bytes;
15443 struct dwarf2_locexpr_baton *baton;
15444 const char *name;
15445
15446 if (child_die->tag != DW_TAG_enumerator)
15447 continue;
15448
15449 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15450 if (attr == NULL)
15451 continue;
15452
15453 name = dwarf2_name (child_die, cu);
15454 if (name == NULL)
15455 name = "<anonymous enumerator>";
15456
15457 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15458 &value, &bytes, &baton);
15459 if (value < 0)
15460 {
15461 unsigned_enum = 0;
15462 flag_enum = 0;
15463 }
15464 else
15465 {
15466 if (count_one_bits_ll (value) >= 2)
15467 flag_enum = 0;
15468 }
15469
15470 /* If we already know that the enum type is neither unsigned, nor
15471 a flag type, no need to look at the rest of the enumerates. */
15472 if (!unsigned_enum && !flag_enum)
15473 break;
15474 }
15475
15476 if (unsigned_enum)
15477 TYPE_UNSIGNED (type) = 1;
15478 if (flag_enum)
15479 TYPE_FLAG_ENUM (type) = 1;
15480 }
15481
15482 /* Given a DW_AT_enumeration_type die, set its type. We do not
15483 complete the type's fields yet, or create any symbols. */
15484
15485 static struct type *
15486 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15487 {
15488 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15489 struct type *type;
15490 struct attribute *attr;
15491 const char *name;
15492
15493 /* If the definition of this type lives in .debug_types, read that type.
15494 Don't follow DW_AT_specification though, that will take us back up
15495 the chain and we want to go down. */
15496 attr = die->attr (DW_AT_signature);
15497 if (attr != nullptr)
15498 {
15499 type = get_DW_AT_signature_type (die, attr, cu);
15500
15501 /* The type's CU may not be the same as CU.
15502 Ensure TYPE is recorded with CU in die_type_hash. */
15503 return set_die_type (die, type, cu);
15504 }
15505
15506 type = alloc_type (objfile);
15507
15508 TYPE_CODE (type) = TYPE_CODE_ENUM;
15509 name = dwarf2_full_name (NULL, die, cu);
15510 if (name != NULL)
15511 TYPE_NAME (type) = name;
15512
15513 attr = dwarf2_attr (die, DW_AT_type, cu);
15514 if (attr != NULL)
15515 {
15516 struct type *underlying_type = die_type (die, cu);
15517
15518 TYPE_TARGET_TYPE (type) = underlying_type;
15519 }
15520
15521 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15522 if (attr != nullptr)
15523 {
15524 TYPE_LENGTH (type) = DW_UNSND (attr);
15525 }
15526 else
15527 {
15528 TYPE_LENGTH (type) = 0;
15529 }
15530
15531 maybe_set_alignment (cu, die, type);
15532
15533 /* The enumeration DIE can be incomplete. In Ada, any type can be
15534 declared as private in the package spec, and then defined only
15535 inside the package body. Such types are known as Taft Amendment
15536 Types. When another package uses such a type, an incomplete DIE
15537 may be generated by the compiler. */
15538 if (die_is_declaration (die, cu))
15539 TYPE_STUB (type) = 1;
15540
15541 /* Finish the creation of this type by using the enum's children.
15542 We must call this even when the underlying type has been provided
15543 so that we can determine if we're looking at a "flag" enum. */
15544 update_enumeration_type_from_children (die, type, cu);
15545
15546 /* If this type has an underlying type that is not a stub, then we
15547 may use its attributes. We always use the "unsigned" attribute
15548 in this situation, because ordinarily we guess whether the type
15549 is unsigned -- but the guess can be wrong and the underlying type
15550 can tell us the reality. However, we defer to a local size
15551 attribute if one exists, because this lets the compiler override
15552 the underlying type if needed. */
15553 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15554 {
15555 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15556 underlying_type = check_typedef (underlying_type);
15557 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15558 if (TYPE_LENGTH (type) == 0)
15559 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15560 if (TYPE_RAW_ALIGN (type) == 0
15561 && TYPE_RAW_ALIGN (underlying_type) != 0)
15562 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15563 }
15564
15565 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15566
15567 return set_die_type (die, type, cu);
15568 }
15569
15570 /* Given a pointer to a die which begins an enumeration, process all
15571 the dies that define the members of the enumeration, and create the
15572 symbol for the enumeration type.
15573
15574 NOTE: We reverse the order of the element list. */
15575
15576 static void
15577 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15578 {
15579 struct type *this_type;
15580
15581 this_type = get_die_type (die, cu);
15582 if (this_type == NULL)
15583 this_type = read_enumeration_type (die, cu);
15584
15585 if (die->child != NULL)
15586 {
15587 struct die_info *child_die;
15588 struct symbol *sym;
15589 std::vector<struct field> fields;
15590 const char *name;
15591
15592 child_die = die->child;
15593 while (child_die && child_die->tag)
15594 {
15595 if (child_die->tag != DW_TAG_enumerator)
15596 {
15597 process_die (child_die, cu);
15598 }
15599 else
15600 {
15601 name = dwarf2_name (child_die, cu);
15602 if (name)
15603 {
15604 sym = new_symbol (child_die, this_type, cu);
15605
15606 fields.emplace_back ();
15607 struct field &field = fields.back ();
15608
15609 FIELD_NAME (field) = sym->linkage_name ();
15610 FIELD_TYPE (field) = NULL;
15611 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15612 FIELD_BITSIZE (field) = 0;
15613 }
15614 }
15615
15616 child_die = child_die->sibling;
15617 }
15618
15619 if (!fields.empty ())
15620 {
15621 TYPE_NFIELDS (this_type) = fields.size ();
15622 TYPE_FIELDS (this_type) = (struct field *)
15623 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15624 memcpy (TYPE_FIELDS (this_type), fields.data (),
15625 sizeof (struct field) * fields.size ());
15626 }
15627 }
15628
15629 /* If we are reading an enum from a .debug_types unit, and the enum
15630 is a declaration, and the enum is not the signatured type in the
15631 unit, then we do not want to add a symbol for it. Adding a
15632 symbol would in some cases obscure the true definition of the
15633 enum, giving users an incomplete type when the definition is
15634 actually available. Note that we do not want to do this for all
15635 enums which are just declarations, because C++0x allows forward
15636 enum declarations. */
15637 if (cu->per_cu->is_debug_types
15638 && die_is_declaration (die, cu))
15639 {
15640 struct signatured_type *sig_type;
15641
15642 sig_type = (struct signatured_type *) cu->per_cu;
15643 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15644 if (sig_type->type_offset_in_section != die->sect_off)
15645 return;
15646 }
15647
15648 new_symbol (die, this_type, cu);
15649 }
15650
15651 /* Extract all information from a DW_TAG_array_type DIE and put it in
15652 the DIE's type field. For now, this only handles one dimensional
15653 arrays. */
15654
15655 static struct type *
15656 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15657 {
15658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15659 struct die_info *child_die;
15660 struct type *type;
15661 struct type *element_type, *range_type, *index_type;
15662 struct attribute *attr;
15663 const char *name;
15664 struct dynamic_prop *byte_stride_prop = NULL;
15665 unsigned int bit_stride = 0;
15666
15667 element_type = die_type (die, cu);
15668
15669 /* The die_type call above may have already set the type for this DIE. */
15670 type = get_die_type (die, cu);
15671 if (type)
15672 return type;
15673
15674 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15675 if (attr != NULL)
15676 {
15677 int stride_ok;
15678 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15679
15680 byte_stride_prop
15681 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15682 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15683 prop_type);
15684 if (!stride_ok)
15685 {
15686 complaint (_("unable to read array DW_AT_byte_stride "
15687 " - DIE at %s [in module %s]"),
15688 sect_offset_str (die->sect_off),
15689 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15690 /* Ignore this attribute. We will likely not be able to print
15691 arrays of this type correctly, but there is little we can do
15692 to help if we cannot read the attribute's value. */
15693 byte_stride_prop = NULL;
15694 }
15695 }
15696
15697 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15698 if (attr != NULL)
15699 bit_stride = DW_UNSND (attr);
15700
15701 /* Irix 6.2 native cc creates array types without children for
15702 arrays with unspecified length. */
15703 if (die->child == NULL)
15704 {
15705 index_type = objfile_type (objfile)->builtin_int;
15706 range_type = create_static_range_type (NULL, index_type, 0, -1);
15707 type = create_array_type_with_stride (NULL, element_type, range_type,
15708 byte_stride_prop, bit_stride);
15709 return set_die_type (die, type, cu);
15710 }
15711
15712 std::vector<struct type *> range_types;
15713 child_die = die->child;
15714 while (child_die && child_die->tag)
15715 {
15716 if (child_die->tag == DW_TAG_subrange_type)
15717 {
15718 struct type *child_type = read_type_die (child_die, cu);
15719
15720 if (child_type != NULL)
15721 {
15722 /* The range type was succesfully read. Save it for the
15723 array type creation. */
15724 range_types.push_back (child_type);
15725 }
15726 }
15727 child_die = child_die->sibling;
15728 }
15729
15730 /* Dwarf2 dimensions are output from left to right, create the
15731 necessary array types in backwards order. */
15732
15733 type = element_type;
15734
15735 if (read_array_order (die, cu) == DW_ORD_col_major)
15736 {
15737 int i = 0;
15738
15739 while (i < range_types.size ())
15740 type = create_array_type_with_stride (NULL, type, range_types[i++],
15741 byte_stride_prop, bit_stride);
15742 }
15743 else
15744 {
15745 size_t ndim = range_types.size ();
15746 while (ndim-- > 0)
15747 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15748 byte_stride_prop, bit_stride);
15749 }
15750
15751 /* Understand Dwarf2 support for vector types (like they occur on
15752 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15753 array type. This is not part of the Dwarf2/3 standard yet, but a
15754 custom vendor extension. The main difference between a regular
15755 array and the vector variant is that vectors are passed by value
15756 to functions. */
15757 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15758 if (attr != nullptr)
15759 make_vector_type (type);
15760
15761 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15762 implementation may choose to implement triple vectors using this
15763 attribute. */
15764 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15765 if (attr != nullptr)
15766 {
15767 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15768 TYPE_LENGTH (type) = DW_UNSND (attr);
15769 else
15770 complaint (_("DW_AT_byte_size for array type smaller "
15771 "than the total size of elements"));
15772 }
15773
15774 name = dwarf2_name (die, cu);
15775 if (name)
15776 TYPE_NAME (type) = name;
15777
15778 maybe_set_alignment (cu, die, type);
15779
15780 /* Install the type in the die. */
15781 set_die_type (die, type, cu);
15782
15783 /* set_die_type should be already done. */
15784 set_descriptive_type (type, die, cu);
15785
15786 return type;
15787 }
15788
15789 static enum dwarf_array_dim_ordering
15790 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15791 {
15792 struct attribute *attr;
15793
15794 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15795
15796 if (attr != nullptr)
15797 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15798
15799 /* GNU F77 is a special case, as at 08/2004 array type info is the
15800 opposite order to the dwarf2 specification, but data is still
15801 laid out as per normal fortran.
15802
15803 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15804 version checking. */
15805
15806 if (cu->language == language_fortran
15807 && cu->producer && strstr (cu->producer, "GNU F77"))
15808 {
15809 return DW_ORD_row_major;
15810 }
15811
15812 switch (cu->language_defn->la_array_ordering)
15813 {
15814 case array_column_major:
15815 return DW_ORD_col_major;
15816 case array_row_major:
15817 default:
15818 return DW_ORD_row_major;
15819 };
15820 }
15821
15822 /* Extract all information from a DW_TAG_set_type DIE and put it in
15823 the DIE's type field. */
15824
15825 static struct type *
15826 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15827 {
15828 struct type *domain_type, *set_type;
15829 struct attribute *attr;
15830
15831 domain_type = die_type (die, cu);
15832
15833 /* The die_type call above may have already set the type for this DIE. */
15834 set_type = get_die_type (die, cu);
15835 if (set_type)
15836 return set_type;
15837
15838 set_type = create_set_type (NULL, domain_type);
15839
15840 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15841 if (attr != nullptr)
15842 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15843
15844 maybe_set_alignment (cu, die, set_type);
15845
15846 return set_die_type (die, set_type, cu);
15847 }
15848
15849 /* A helper for read_common_block that creates a locexpr baton.
15850 SYM is the symbol which we are marking as computed.
15851 COMMON_DIE is the DIE for the common block.
15852 COMMON_LOC is the location expression attribute for the common
15853 block itself.
15854 MEMBER_LOC is the location expression attribute for the particular
15855 member of the common block that we are processing.
15856 CU is the CU from which the above come. */
15857
15858 static void
15859 mark_common_block_symbol_computed (struct symbol *sym,
15860 struct die_info *common_die,
15861 struct attribute *common_loc,
15862 struct attribute *member_loc,
15863 struct dwarf2_cu *cu)
15864 {
15865 struct dwarf2_per_objfile *dwarf2_per_objfile
15866 = cu->per_cu->dwarf2_per_objfile;
15867 struct objfile *objfile = dwarf2_per_objfile->objfile;
15868 struct dwarf2_locexpr_baton *baton;
15869 gdb_byte *ptr;
15870 unsigned int cu_off;
15871 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15872 LONGEST offset = 0;
15873
15874 gdb_assert (common_loc && member_loc);
15875 gdb_assert (common_loc->form_is_block ());
15876 gdb_assert (member_loc->form_is_block ()
15877 || member_loc->form_is_constant ());
15878
15879 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15880 baton->per_cu = cu->per_cu;
15881 gdb_assert (baton->per_cu);
15882
15883 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15884
15885 if (member_loc->form_is_constant ())
15886 {
15887 offset = member_loc->constant_value (0);
15888 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15889 }
15890 else
15891 baton->size += DW_BLOCK (member_loc)->size;
15892
15893 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15894 baton->data = ptr;
15895
15896 *ptr++ = DW_OP_call4;
15897 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15898 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15899 ptr += 4;
15900
15901 if (member_loc->form_is_constant ())
15902 {
15903 *ptr++ = DW_OP_addr;
15904 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15905 ptr += cu->header.addr_size;
15906 }
15907 else
15908 {
15909 /* We have to copy the data here, because DW_OP_call4 will only
15910 use a DW_AT_location attribute. */
15911 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15912 ptr += DW_BLOCK (member_loc)->size;
15913 }
15914
15915 *ptr++ = DW_OP_plus;
15916 gdb_assert (ptr - baton->data == baton->size);
15917
15918 SYMBOL_LOCATION_BATON (sym) = baton;
15919 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15920 }
15921
15922 /* Create appropriate locally-scoped variables for all the
15923 DW_TAG_common_block entries. Also create a struct common_block
15924 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15925 is used to separate the common blocks name namespace from regular
15926 variable names. */
15927
15928 static void
15929 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15930 {
15931 struct attribute *attr;
15932
15933 attr = dwarf2_attr (die, DW_AT_location, cu);
15934 if (attr != nullptr)
15935 {
15936 /* Support the .debug_loc offsets. */
15937 if (attr->form_is_block ())
15938 {
15939 /* Ok. */
15940 }
15941 else if (attr->form_is_section_offset ())
15942 {
15943 dwarf2_complex_location_expr_complaint ();
15944 attr = NULL;
15945 }
15946 else
15947 {
15948 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15949 "common block member");
15950 attr = NULL;
15951 }
15952 }
15953
15954 if (die->child != NULL)
15955 {
15956 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15957 struct die_info *child_die;
15958 size_t n_entries = 0, size;
15959 struct common_block *common_block;
15960 struct symbol *sym;
15961
15962 for (child_die = die->child;
15963 child_die && child_die->tag;
15964 child_die = child_die->sibling)
15965 ++n_entries;
15966
15967 size = (sizeof (struct common_block)
15968 + (n_entries - 1) * sizeof (struct symbol *));
15969 common_block
15970 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15971 size);
15972 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15973 common_block->n_entries = 0;
15974
15975 for (child_die = die->child;
15976 child_die && child_die->tag;
15977 child_die = child_die->sibling)
15978 {
15979 /* Create the symbol in the DW_TAG_common_block block in the current
15980 symbol scope. */
15981 sym = new_symbol (child_die, NULL, cu);
15982 if (sym != NULL)
15983 {
15984 struct attribute *member_loc;
15985
15986 common_block->contents[common_block->n_entries++] = sym;
15987
15988 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15989 cu);
15990 if (member_loc)
15991 {
15992 /* GDB has handled this for a long time, but it is
15993 not specified by DWARF. It seems to have been
15994 emitted by gfortran at least as recently as:
15995 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15996 complaint (_("Variable in common block has "
15997 "DW_AT_data_member_location "
15998 "- DIE at %s [in module %s]"),
15999 sect_offset_str (child_die->sect_off),
16000 objfile_name (objfile));
16001
16002 if (member_loc->form_is_section_offset ())
16003 dwarf2_complex_location_expr_complaint ();
16004 else if (member_loc->form_is_constant ()
16005 || member_loc->form_is_block ())
16006 {
16007 if (attr != nullptr)
16008 mark_common_block_symbol_computed (sym, die, attr,
16009 member_loc, cu);
16010 }
16011 else
16012 dwarf2_complex_location_expr_complaint ();
16013 }
16014 }
16015 }
16016
16017 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16018 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16019 }
16020 }
16021
16022 /* Create a type for a C++ namespace. */
16023
16024 static struct type *
16025 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16026 {
16027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16028 const char *previous_prefix, *name;
16029 int is_anonymous;
16030 struct type *type;
16031
16032 /* For extensions, reuse the type of the original namespace. */
16033 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16034 {
16035 struct die_info *ext_die;
16036 struct dwarf2_cu *ext_cu = cu;
16037
16038 ext_die = dwarf2_extension (die, &ext_cu);
16039 type = read_type_die (ext_die, ext_cu);
16040
16041 /* EXT_CU may not be the same as CU.
16042 Ensure TYPE is recorded with CU in die_type_hash. */
16043 return set_die_type (die, type, cu);
16044 }
16045
16046 name = namespace_name (die, &is_anonymous, cu);
16047
16048 /* Now build the name of the current namespace. */
16049
16050 previous_prefix = determine_prefix (die, cu);
16051 if (previous_prefix[0] != '\0')
16052 name = typename_concat (&objfile->objfile_obstack,
16053 previous_prefix, name, 0, cu);
16054
16055 /* Create the type. */
16056 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16057
16058 return set_die_type (die, type, cu);
16059 }
16060
16061 /* Read a namespace scope. */
16062
16063 static void
16064 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16065 {
16066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16067 int is_anonymous;
16068
16069 /* Add a symbol associated to this if we haven't seen the namespace
16070 before. Also, add a using directive if it's an anonymous
16071 namespace. */
16072
16073 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16074 {
16075 struct type *type;
16076
16077 type = read_type_die (die, cu);
16078 new_symbol (die, type, cu);
16079
16080 namespace_name (die, &is_anonymous, cu);
16081 if (is_anonymous)
16082 {
16083 const char *previous_prefix = determine_prefix (die, cu);
16084
16085 std::vector<const char *> excludes;
16086 add_using_directive (using_directives (cu),
16087 previous_prefix, TYPE_NAME (type), NULL,
16088 NULL, excludes, 0, &objfile->objfile_obstack);
16089 }
16090 }
16091
16092 if (die->child != NULL)
16093 {
16094 struct die_info *child_die = die->child;
16095
16096 while (child_die && child_die->tag)
16097 {
16098 process_die (child_die, cu);
16099 child_die = child_die->sibling;
16100 }
16101 }
16102 }
16103
16104 /* Read a Fortran module as type. This DIE can be only a declaration used for
16105 imported module. Still we need that type as local Fortran "use ... only"
16106 declaration imports depend on the created type in determine_prefix. */
16107
16108 static struct type *
16109 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16110 {
16111 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16112 const char *module_name;
16113 struct type *type;
16114
16115 module_name = dwarf2_name (die, cu);
16116 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16117
16118 return set_die_type (die, type, cu);
16119 }
16120
16121 /* Read a Fortran module. */
16122
16123 static void
16124 read_module (struct die_info *die, struct dwarf2_cu *cu)
16125 {
16126 struct die_info *child_die = die->child;
16127 struct type *type;
16128
16129 type = read_type_die (die, cu);
16130 new_symbol (die, type, cu);
16131
16132 while (child_die && child_die->tag)
16133 {
16134 process_die (child_die, cu);
16135 child_die = child_die->sibling;
16136 }
16137 }
16138
16139 /* Return the name of the namespace represented by DIE. Set
16140 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16141 namespace. */
16142
16143 static const char *
16144 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16145 {
16146 struct die_info *current_die;
16147 const char *name = NULL;
16148
16149 /* Loop through the extensions until we find a name. */
16150
16151 for (current_die = die;
16152 current_die != NULL;
16153 current_die = dwarf2_extension (die, &cu))
16154 {
16155 /* We don't use dwarf2_name here so that we can detect the absence
16156 of a name -> anonymous namespace. */
16157 name = dwarf2_string_attr (die, DW_AT_name, cu);
16158
16159 if (name != NULL)
16160 break;
16161 }
16162
16163 /* Is it an anonymous namespace? */
16164
16165 *is_anonymous = (name == NULL);
16166 if (*is_anonymous)
16167 name = CP_ANONYMOUS_NAMESPACE_STR;
16168
16169 return name;
16170 }
16171
16172 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16173 the user defined type vector. */
16174
16175 static struct type *
16176 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16177 {
16178 struct gdbarch *gdbarch
16179 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16180 struct comp_unit_head *cu_header = &cu->header;
16181 struct type *type;
16182 struct attribute *attr_byte_size;
16183 struct attribute *attr_address_class;
16184 int byte_size, addr_class;
16185 struct type *target_type;
16186
16187 target_type = die_type (die, cu);
16188
16189 /* The die_type call above may have already set the type for this DIE. */
16190 type = get_die_type (die, cu);
16191 if (type)
16192 return type;
16193
16194 type = lookup_pointer_type (target_type);
16195
16196 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16197 if (attr_byte_size)
16198 byte_size = DW_UNSND (attr_byte_size);
16199 else
16200 byte_size = cu_header->addr_size;
16201
16202 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16203 if (attr_address_class)
16204 addr_class = DW_UNSND (attr_address_class);
16205 else
16206 addr_class = DW_ADDR_none;
16207
16208 ULONGEST alignment = get_alignment (cu, die);
16209
16210 /* If the pointer size, alignment, or address class is different
16211 than the default, create a type variant marked as such and set
16212 the length accordingly. */
16213 if (TYPE_LENGTH (type) != byte_size
16214 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16215 && alignment != TYPE_RAW_ALIGN (type))
16216 || addr_class != DW_ADDR_none)
16217 {
16218 if (gdbarch_address_class_type_flags_p (gdbarch))
16219 {
16220 int type_flags;
16221
16222 type_flags = gdbarch_address_class_type_flags
16223 (gdbarch, byte_size, addr_class);
16224 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16225 == 0);
16226 type = make_type_with_address_space (type, type_flags);
16227 }
16228 else if (TYPE_LENGTH (type) != byte_size)
16229 {
16230 complaint (_("invalid pointer size %d"), byte_size);
16231 }
16232 else if (TYPE_RAW_ALIGN (type) != alignment)
16233 {
16234 complaint (_("Invalid DW_AT_alignment"
16235 " - DIE at %s [in module %s]"),
16236 sect_offset_str (die->sect_off),
16237 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16238 }
16239 else
16240 {
16241 /* Should we also complain about unhandled address classes? */
16242 }
16243 }
16244
16245 TYPE_LENGTH (type) = byte_size;
16246 set_type_align (type, alignment);
16247 return set_die_type (die, type, cu);
16248 }
16249
16250 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16251 the user defined type vector. */
16252
16253 static struct type *
16254 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16255 {
16256 struct type *type;
16257 struct type *to_type;
16258 struct type *domain;
16259
16260 to_type = die_type (die, cu);
16261 domain = die_containing_type (die, cu);
16262
16263 /* The calls above may have already set the type for this DIE. */
16264 type = get_die_type (die, cu);
16265 if (type)
16266 return type;
16267
16268 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16269 type = lookup_methodptr_type (to_type);
16270 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16271 {
16272 struct type *new_type
16273 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16274
16275 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16276 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16277 TYPE_VARARGS (to_type));
16278 type = lookup_methodptr_type (new_type);
16279 }
16280 else
16281 type = lookup_memberptr_type (to_type, domain);
16282
16283 return set_die_type (die, type, cu);
16284 }
16285
16286 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16287 the user defined type vector. */
16288
16289 static struct type *
16290 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16291 enum type_code refcode)
16292 {
16293 struct comp_unit_head *cu_header = &cu->header;
16294 struct type *type, *target_type;
16295 struct attribute *attr;
16296
16297 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16298
16299 target_type = die_type (die, cu);
16300
16301 /* The die_type call above may have already set the type for this DIE. */
16302 type = get_die_type (die, cu);
16303 if (type)
16304 return type;
16305
16306 type = lookup_reference_type (target_type, refcode);
16307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16308 if (attr != nullptr)
16309 {
16310 TYPE_LENGTH (type) = DW_UNSND (attr);
16311 }
16312 else
16313 {
16314 TYPE_LENGTH (type) = cu_header->addr_size;
16315 }
16316 maybe_set_alignment (cu, die, type);
16317 return set_die_type (die, type, cu);
16318 }
16319
16320 /* Add the given cv-qualifiers to the element type of the array. GCC
16321 outputs DWARF type qualifiers that apply to an array, not the
16322 element type. But GDB relies on the array element type to carry
16323 the cv-qualifiers. This mimics section 6.7.3 of the C99
16324 specification. */
16325
16326 static struct type *
16327 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16328 struct type *base_type, int cnst, int voltl)
16329 {
16330 struct type *el_type, *inner_array;
16331
16332 base_type = copy_type (base_type);
16333 inner_array = base_type;
16334
16335 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16336 {
16337 TYPE_TARGET_TYPE (inner_array) =
16338 copy_type (TYPE_TARGET_TYPE (inner_array));
16339 inner_array = TYPE_TARGET_TYPE (inner_array);
16340 }
16341
16342 el_type = TYPE_TARGET_TYPE (inner_array);
16343 cnst |= TYPE_CONST (el_type);
16344 voltl |= TYPE_VOLATILE (el_type);
16345 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16346
16347 return set_die_type (die, base_type, cu);
16348 }
16349
16350 static struct type *
16351 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16352 {
16353 struct type *base_type, *cv_type;
16354
16355 base_type = die_type (die, cu);
16356
16357 /* The die_type call above may have already set the type for this DIE. */
16358 cv_type = get_die_type (die, cu);
16359 if (cv_type)
16360 return cv_type;
16361
16362 /* In case the const qualifier is applied to an array type, the element type
16363 is so qualified, not the array type (section 6.7.3 of C99). */
16364 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16365 return add_array_cv_type (die, cu, base_type, 1, 0);
16366
16367 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16368 return set_die_type (die, cv_type, cu);
16369 }
16370
16371 static struct type *
16372 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16373 {
16374 struct type *base_type, *cv_type;
16375
16376 base_type = die_type (die, cu);
16377
16378 /* The die_type call above may have already set the type for this DIE. */
16379 cv_type = get_die_type (die, cu);
16380 if (cv_type)
16381 return cv_type;
16382
16383 /* In case the volatile qualifier is applied to an array type, the
16384 element type is so qualified, not the array type (section 6.7.3
16385 of C99). */
16386 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16387 return add_array_cv_type (die, cu, base_type, 0, 1);
16388
16389 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16390 return set_die_type (die, cv_type, cu);
16391 }
16392
16393 /* Handle DW_TAG_restrict_type. */
16394
16395 static struct type *
16396 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16397 {
16398 struct type *base_type, *cv_type;
16399
16400 base_type = die_type (die, cu);
16401
16402 /* The die_type call above may have already set the type for this DIE. */
16403 cv_type = get_die_type (die, cu);
16404 if (cv_type)
16405 return cv_type;
16406
16407 cv_type = make_restrict_type (base_type);
16408 return set_die_type (die, cv_type, cu);
16409 }
16410
16411 /* Handle DW_TAG_atomic_type. */
16412
16413 static struct type *
16414 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct type *base_type, *cv_type;
16417
16418 base_type = die_type (die, cu);
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
16425 cv_type = make_atomic_type (base_type);
16426 return set_die_type (die, cv_type, cu);
16427 }
16428
16429 /* Extract all information from a DW_TAG_string_type DIE and add to
16430 the user defined type vector. It isn't really a user defined type,
16431 but it behaves like one, with other DIE's using an AT_user_def_type
16432 attribute to reference it. */
16433
16434 static struct type *
16435 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16436 {
16437 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16439 struct type *type, *range_type, *index_type, *char_type;
16440 struct attribute *attr;
16441 struct dynamic_prop prop;
16442 bool length_is_constant = true;
16443 LONGEST length;
16444
16445 /* There are a couple of places where bit sizes might be made use of
16446 when parsing a DW_TAG_string_type, however, no producer that we know
16447 of make use of these. Handling bit sizes that are a multiple of the
16448 byte size is easy enough, but what about other bit sizes? Lets deal
16449 with that problem when we have to. Warn about these attributes being
16450 unsupported, then parse the type and ignore them like we always
16451 have. */
16452 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16453 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16454 {
16455 static bool warning_printed = false;
16456 if (!warning_printed)
16457 {
16458 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16459 "currently supported on DW_TAG_string_type."));
16460 warning_printed = true;
16461 }
16462 }
16463
16464 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16465 if (attr != nullptr && !attr->form_is_constant ())
16466 {
16467 /* The string length describes the location at which the length of
16468 the string can be found. The size of the length field can be
16469 specified with one of the attributes below. */
16470 struct type *prop_type;
16471 struct attribute *len
16472 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16473 if (len == nullptr)
16474 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16475 if (len != nullptr && len->form_is_constant ())
16476 {
16477 /* Pass 0 as the default as we know this attribute is constant
16478 and the default value will not be returned. */
16479 LONGEST sz = len->constant_value (0);
16480 prop_type = cu->per_cu->int_type (sz, true);
16481 }
16482 else
16483 {
16484 /* If the size is not specified then we assume it is the size of
16485 an address on this target. */
16486 prop_type = cu->per_cu->addr_sized_int_type (true);
16487 }
16488
16489 /* Convert the attribute into a dynamic property. */
16490 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16491 length = 1;
16492 else
16493 length_is_constant = false;
16494 }
16495 else if (attr != nullptr)
16496 {
16497 /* This DW_AT_string_length just contains the length with no
16498 indirection. There's no need to create a dynamic property in this
16499 case. Pass 0 for the default value as we know it will not be
16500 returned in this case. */
16501 length = attr->constant_value (0);
16502 }
16503 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16504 {
16505 /* We don't currently support non-constant byte sizes for strings. */
16506 length = attr->constant_value (1);
16507 }
16508 else
16509 {
16510 /* Use 1 as a fallback length if we have nothing else. */
16511 length = 1;
16512 }
16513
16514 index_type = objfile_type (objfile)->builtin_int;
16515 if (length_is_constant)
16516 range_type = create_static_range_type (NULL, index_type, 1, length);
16517 else
16518 {
16519 struct dynamic_prop low_bound;
16520
16521 low_bound.kind = PROP_CONST;
16522 low_bound.data.const_val = 1;
16523 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16524 }
16525 char_type = language_string_char_type (cu->language_defn, gdbarch);
16526 type = create_string_type (NULL, char_type, range_type);
16527
16528 return set_die_type (die, type, cu);
16529 }
16530
16531 /* Assuming that DIE corresponds to a function, returns nonzero
16532 if the function is prototyped. */
16533
16534 static int
16535 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16536 {
16537 struct attribute *attr;
16538
16539 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16540 if (attr && (DW_UNSND (attr) != 0))
16541 return 1;
16542
16543 /* The DWARF standard implies that the DW_AT_prototyped attribute
16544 is only meaningful for C, but the concept also extends to other
16545 languages that allow unprototyped functions (Eg: Objective C).
16546 For all other languages, assume that functions are always
16547 prototyped. */
16548 if (cu->language != language_c
16549 && cu->language != language_objc
16550 && cu->language != language_opencl)
16551 return 1;
16552
16553 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16554 prototyped and unprototyped functions; default to prototyped,
16555 since that is more common in modern code (and RealView warns
16556 about unprototyped functions). */
16557 if (producer_is_realview (cu->producer))
16558 return 1;
16559
16560 return 0;
16561 }
16562
16563 /* Handle DIES due to C code like:
16564
16565 struct foo
16566 {
16567 int (*funcp)(int a, long l);
16568 int b;
16569 };
16570
16571 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16572
16573 static struct type *
16574 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16575 {
16576 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16577 struct type *type; /* Type that this function returns. */
16578 struct type *ftype; /* Function that returns above type. */
16579 struct attribute *attr;
16580
16581 type = die_type (die, cu);
16582
16583 /* The die_type call above may have already set the type for this DIE. */
16584 ftype = get_die_type (die, cu);
16585 if (ftype)
16586 return ftype;
16587
16588 ftype = lookup_function_type (type);
16589
16590 if (prototyped_function_p (die, cu))
16591 TYPE_PROTOTYPED (ftype) = 1;
16592
16593 /* Store the calling convention in the type if it's available in
16594 the subroutine die. Otherwise set the calling convention to
16595 the default value DW_CC_normal. */
16596 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16597 if (attr != nullptr
16598 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16599 TYPE_CALLING_CONVENTION (ftype)
16600 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16601 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16602 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16603 else
16604 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16605
16606 /* Record whether the function returns normally to its caller or not
16607 if the DWARF producer set that information. */
16608 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16609 if (attr && (DW_UNSND (attr) != 0))
16610 TYPE_NO_RETURN (ftype) = 1;
16611
16612 /* We need to add the subroutine type to the die immediately so
16613 we don't infinitely recurse when dealing with parameters
16614 declared as the same subroutine type. */
16615 set_die_type (die, ftype, cu);
16616
16617 if (die->child != NULL)
16618 {
16619 struct type *void_type = objfile_type (objfile)->builtin_void;
16620 struct die_info *child_die;
16621 int nparams, iparams;
16622
16623 /* Count the number of parameters.
16624 FIXME: GDB currently ignores vararg functions, but knows about
16625 vararg member functions. */
16626 nparams = 0;
16627 child_die = die->child;
16628 while (child_die && child_die->tag)
16629 {
16630 if (child_die->tag == DW_TAG_formal_parameter)
16631 nparams++;
16632 else if (child_die->tag == DW_TAG_unspecified_parameters)
16633 TYPE_VARARGS (ftype) = 1;
16634 child_die = child_die->sibling;
16635 }
16636
16637 /* Allocate storage for parameters and fill them in. */
16638 TYPE_NFIELDS (ftype) = nparams;
16639 TYPE_FIELDS (ftype) = (struct field *)
16640 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16641
16642 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16643 even if we error out during the parameters reading below. */
16644 for (iparams = 0; iparams < nparams; iparams++)
16645 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16646
16647 iparams = 0;
16648 child_die = die->child;
16649 while (child_die && child_die->tag)
16650 {
16651 if (child_die->tag == DW_TAG_formal_parameter)
16652 {
16653 struct type *arg_type;
16654
16655 /* DWARF version 2 has no clean way to discern C++
16656 static and non-static member functions. G++ helps
16657 GDB by marking the first parameter for non-static
16658 member functions (which is the this pointer) as
16659 artificial. We pass this information to
16660 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16661
16662 DWARF version 3 added DW_AT_object_pointer, which GCC
16663 4.5 does not yet generate. */
16664 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16665 if (attr != nullptr)
16666 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16667 else
16668 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16669 arg_type = die_type (child_die, cu);
16670
16671 /* RealView does not mark THIS as const, which the testsuite
16672 expects. GCC marks THIS as const in method definitions,
16673 but not in the class specifications (GCC PR 43053). */
16674 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16675 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16676 {
16677 int is_this = 0;
16678 struct dwarf2_cu *arg_cu = cu;
16679 const char *name = dwarf2_name (child_die, cu);
16680
16681 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16682 if (attr != nullptr)
16683 {
16684 /* If the compiler emits this, use it. */
16685 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16686 is_this = 1;
16687 }
16688 else if (name && strcmp (name, "this") == 0)
16689 /* Function definitions will have the argument names. */
16690 is_this = 1;
16691 else if (name == NULL && iparams == 0)
16692 /* Declarations may not have the names, so like
16693 elsewhere in GDB, assume an artificial first
16694 argument is "this". */
16695 is_this = 1;
16696
16697 if (is_this)
16698 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16699 arg_type, 0);
16700 }
16701
16702 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16703 iparams++;
16704 }
16705 child_die = child_die->sibling;
16706 }
16707 }
16708
16709 return ftype;
16710 }
16711
16712 static struct type *
16713 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16714 {
16715 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16716 const char *name = NULL;
16717 struct type *this_type, *target_type;
16718
16719 name = dwarf2_full_name (NULL, die, cu);
16720 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16721 TYPE_TARGET_STUB (this_type) = 1;
16722 set_die_type (die, this_type, cu);
16723 target_type = die_type (die, cu);
16724 if (target_type != this_type)
16725 TYPE_TARGET_TYPE (this_type) = target_type;
16726 else
16727 {
16728 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16729 spec and cause infinite loops in GDB. */
16730 complaint (_("Self-referential DW_TAG_typedef "
16731 "- DIE at %s [in module %s]"),
16732 sect_offset_str (die->sect_off), objfile_name (objfile));
16733 TYPE_TARGET_TYPE (this_type) = NULL;
16734 }
16735 if (name == NULL)
16736 {
16737 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16738 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16739 Handle these by just returning the target type, rather than
16740 constructing an anonymous typedef type and trying to handle this
16741 elsewhere. */
16742 set_die_type (die, target_type, cu);
16743 return target_type;
16744 }
16745 return this_type;
16746 }
16747
16748 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16749 (which may be different from NAME) to the architecture back-end to allow
16750 it to guess the correct format if necessary. */
16751
16752 static struct type *
16753 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16754 const char *name_hint, enum bfd_endian byte_order)
16755 {
16756 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16757 const struct floatformat **format;
16758 struct type *type;
16759
16760 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16761 if (format)
16762 type = init_float_type (objfile, bits, name, format, byte_order);
16763 else
16764 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16765
16766 return type;
16767 }
16768
16769 /* Allocate an integer type of size BITS and name NAME. */
16770
16771 static struct type *
16772 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16773 int bits, int unsigned_p, const char *name)
16774 {
16775 struct type *type;
16776
16777 /* Versions of Intel's C Compiler generate an integer type called "void"
16778 instead of using DW_TAG_unspecified_type. This has been seen on
16779 at least versions 14, 17, and 18. */
16780 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16781 && strcmp (name, "void") == 0)
16782 type = objfile_type (objfile)->builtin_void;
16783 else
16784 type = init_integer_type (objfile, bits, unsigned_p, name);
16785
16786 return type;
16787 }
16788
16789 /* Initialise and return a floating point type of size BITS suitable for
16790 use as a component of a complex number. The NAME_HINT is passed through
16791 when initialising the floating point type and is the name of the complex
16792 type.
16793
16794 As DWARF doesn't currently provide an explicit name for the components
16795 of a complex number, but it can be helpful to have these components
16796 named, we try to select a suitable name based on the size of the
16797 component. */
16798 static struct type *
16799 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16800 struct objfile *objfile,
16801 int bits, const char *name_hint,
16802 enum bfd_endian byte_order)
16803 {
16804 gdbarch *gdbarch = get_objfile_arch (objfile);
16805 struct type *tt = nullptr;
16806
16807 /* Try to find a suitable floating point builtin type of size BITS.
16808 We're going to use the name of this type as the name for the complex
16809 target type that we are about to create. */
16810 switch (cu->language)
16811 {
16812 case language_fortran:
16813 switch (bits)
16814 {
16815 case 32:
16816 tt = builtin_f_type (gdbarch)->builtin_real;
16817 break;
16818 case 64:
16819 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16820 break;
16821 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16822 case 128:
16823 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16824 break;
16825 }
16826 break;
16827 default:
16828 switch (bits)
16829 {
16830 case 32:
16831 tt = builtin_type (gdbarch)->builtin_float;
16832 break;
16833 case 64:
16834 tt = builtin_type (gdbarch)->builtin_double;
16835 break;
16836 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16837 case 128:
16838 tt = builtin_type (gdbarch)->builtin_long_double;
16839 break;
16840 }
16841 break;
16842 }
16843
16844 /* If the type we found doesn't match the size we were looking for, then
16845 pretend we didn't find a type at all, the complex target type we
16846 create will then be nameless. */
16847 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16848 tt = nullptr;
16849
16850 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16851 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16852 }
16853
16854 /* Find a representation of a given base type and install
16855 it in the TYPE field of the die. */
16856
16857 static struct type *
16858 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16859 {
16860 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16861 struct type *type;
16862 struct attribute *attr;
16863 int encoding = 0, bits = 0;
16864 const char *name;
16865 gdbarch *arch;
16866
16867 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16868 if (attr != nullptr)
16869 encoding = DW_UNSND (attr);
16870 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16871 if (attr != nullptr)
16872 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16873 name = dwarf2_name (die, cu);
16874 if (!name)
16875 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16876
16877 arch = get_objfile_arch (objfile);
16878 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16879
16880 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16881 if (attr)
16882 {
16883 int endianity = DW_UNSND (attr);
16884
16885 switch (endianity)
16886 {
16887 case DW_END_big:
16888 byte_order = BFD_ENDIAN_BIG;
16889 break;
16890 case DW_END_little:
16891 byte_order = BFD_ENDIAN_LITTLE;
16892 break;
16893 default:
16894 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16895 break;
16896 }
16897 }
16898
16899 switch (encoding)
16900 {
16901 case DW_ATE_address:
16902 /* Turn DW_ATE_address into a void * pointer. */
16903 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16904 type = init_pointer_type (objfile, bits, name, type);
16905 break;
16906 case DW_ATE_boolean:
16907 type = init_boolean_type (objfile, bits, 1, name);
16908 break;
16909 case DW_ATE_complex_float:
16910 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16911 byte_order);
16912 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16913 {
16914 if (name == nullptr)
16915 {
16916 struct obstack *obstack
16917 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16918 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16919 nullptr);
16920 }
16921 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16922 }
16923 else
16924 type = init_complex_type (name, type);
16925 break;
16926 case DW_ATE_decimal_float:
16927 type = init_decfloat_type (objfile, bits, name);
16928 break;
16929 case DW_ATE_float:
16930 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16931 break;
16932 case DW_ATE_signed:
16933 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16934 break;
16935 case DW_ATE_unsigned:
16936 if (cu->language == language_fortran
16937 && name
16938 && startswith (name, "character("))
16939 type = init_character_type (objfile, bits, 1, name);
16940 else
16941 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16942 break;
16943 case DW_ATE_signed_char:
16944 if (cu->language == language_ada || cu->language == language_m2
16945 || cu->language == language_pascal
16946 || cu->language == language_fortran)
16947 type = init_character_type (objfile, bits, 0, name);
16948 else
16949 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16950 break;
16951 case DW_ATE_unsigned_char:
16952 if (cu->language == language_ada || cu->language == language_m2
16953 || cu->language == language_pascal
16954 || cu->language == language_fortran
16955 || cu->language == language_rust)
16956 type = init_character_type (objfile, bits, 1, name);
16957 else
16958 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16959 break;
16960 case DW_ATE_UTF:
16961 {
16962 if (bits == 16)
16963 type = builtin_type (arch)->builtin_char16;
16964 else if (bits == 32)
16965 type = builtin_type (arch)->builtin_char32;
16966 else
16967 {
16968 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16969 bits);
16970 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16971 }
16972 return set_die_type (die, type, cu);
16973 }
16974 break;
16975
16976 default:
16977 complaint (_("unsupported DW_AT_encoding: '%s'"),
16978 dwarf_type_encoding_name (encoding));
16979 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16980 break;
16981 }
16982
16983 if (name && strcmp (name, "char") == 0)
16984 TYPE_NOSIGN (type) = 1;
16985
16986 maybe_set_alignment (cu, die, type);
16987
16988 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16989
16990 return set_die_type (die, type, cu);
16991 }
16992
16993 /* Parse dwarf attribute if it's a block, reference or constant and put the
16994 resulting value of the attribute into struct bound_prop.
16995 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16996
16997 static int
16998 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16999 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17000 struct type *default_type)
17001 {
17002 struct dwarf2_property_baton *baton;
17003 struct obstack *obstack
17004 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17005
17006 gdb_assert (default_type != NULL);
17007
17008 if (attr == NULL || prop == NULL)
17009 return 0;
17010
17011 if (attr->form_is_block ())
17012 {
17013 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17014 baton->property_type = default_type;
17015 baton->locexpr.per_cu = cu->per_cu;
17016 baton->locexpr.size = DW_BLOCK (attr)->size;
17017 baton->locexpr.data = DW_BLOCK (attr)->data;
17018 switch (attr->name)
17019 {
17020 case DW_AT_string_length:
17021 baton->locexpr.is_reference = true;
17022 break;
17023 default:
17024 baton->locexpr.is_reference = false;
17025 break;
17026 }
17027 prop->data.baton = baton;
17028 prop->kind = PROP_LOCEXPR;
17029 gdb_assert (prop->data.baton != NULL);
17030 }
17031 else if (attr->form_is_ref ())
17032 {
17033 struct dwarf2_cu *target_cu = cu;
17034 struct die_info *target_die;
17035 struct attribute *target_attr;
17036
17037 target_die = follow_die_ref (die, attr, &target_cu);
17038 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17039 if (target_attr == NULL)
17040 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17041 target_cu);
17042 if (target_attr == NULL)
17043 return 0;
17044
17045 switch (target_attr->name)
17046 {
17047 case DW_AT_location:
17048 if (target_attr->form_is_section_offset ())
17049 {
17050 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17051 baton->property_type = die_type (target_die, target_cu);
17052 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17053 prop->data.baton = baton;
17054 prop->kind = PROP_LOCLIST;
17055 gdb_assert (prop->data.baton != NULL);
17056 }
17057 else if (target_attr->form_is_block ())
17058 {
17059 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17060 baton->property_type = die_type (target_die, target_cu);
17061 baton->locexpr.per_cu = cu->per_cu;
17062 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17063 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17064 baton->locexpr.is_reference = true;
17065 prop->data.baton = baton;
17066 prop->kind = PROP_LOCEXPR;
17067 gdb_assert (prop->data.baton != NULL);
17068 }
17069 else
17070 {
17071 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17072 "dynamic property");
17073 return 0;
17074 }
17075 break;
17076 case DW_AT_data_member_location:
17077 {
17078 LONGEST offset;
17079
17080 if (!handle_data_member_location (target_die, target_cu,
17081 &offset))
17082 return 0;
17083
17084 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17085 baton->property_type = read_type_die (target_die->parent,
17086 target_cu);
17087 baton->offset_info.offset = offset;
17088 baton->offset_info.type = die_type (target_die, target_cu);
17089 prop->data.baton = baton;
17090 prop->kind = PROP_ADDR_OFFSET;
17091 break;
17092 }
17093 }
17094 }
17095 else if (attr->form_is_constant ())
17096 {
17097 prop->data.const_val = attr->constant_value (0);
17098 prop->kind = PROP_CONST;
17099 }
17100 else
17101 {
17102 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17103 dwarf2_name (die, cu));
17104 return 0;
17105 }
17106
17107 return 1;
17108 }
17109
17110 /* See read.h. */
17111
17112 struct type *
17113 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17114 {
17115 struct objfile *objfile = dwarf2_per_objfile->objfile;
17116 struct type *int_type;
17117
17118 /* Helper macro to examine the various builtin types. */
17119 #define TRY_TYPE(F) \
17120 int_type = (unsigned_p \
17121 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17122 : objfile_type (objfile)->builtin_ ## F); \
17123 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17124 return int_type
17125
17126 TRY_TYPE (char);
17127 TRY_TYPE (short);
17128 TRY_TYPE (int);
17129 TRY_TYPE (long);
17130 TRY_TYPE (long_long);
17131
17132 #undef TRY_TYPE
17133
17134 gdb_assert_not_reached ("unable to find suitable integer type");
17135 }
17136
17137 /* See read.h. */
17138
17139 struct type *
17140 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17141 {
17142 int addr_size = this->addr_size ();
17143 return int_type (addr_size, unsigned_p);
17144 }
17145
17146 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17147 present (which is valid) then compute the default type based on the
17148 compilation units address size. */
17149
17150 static struct type *
17151 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17152 {
17153 struct type *index_type = die_type (die, cu);
17154
17155 /* Dwarf-2 specifications explicitly allows to create subrange types
17156 without specifying a base type.
17157 In that case, the base type must be set to the type of
17158 the lower bound, upper bound or count, in that order, if any of these
17159 three attributes references an object that has a type.
17160 If no base type is found, the Dwarf-2 specifications say that
17161 a signed integer type of size equal to the size of an address should
17162 be used.
17163 For the following C code: `extern char gdb_int [];'
17164 GCC produces an empty range DIE.
17165 FIXME: muller/2010-05-28: Possible references to object for low bound,
17166 high bound or count are not yet handled by this code. */
17167 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17168 index_type = cu->per_cu->addr_sized_int_type (false);
17169
17170 return index_type;
17171 }
17172
17173 /* Read the given DW_AT_subrange DIE. */
17174
17175 static struct type *
17176 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17177 {
17178 struct type *base_type, *orig_base_type;
17179 struct type *range_type;
17180 struct attribute *attr;
17181 struct dynamic_prop low, high;
17182 int low_default_is_valid;
17183 int high_bound_is_count = 0;
17184 const char *name;
17185 ULONGEST negative_mask;
17186
17187 orig_base_type = read_subrange_index_type (die, cu);
17188
17189 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17190 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17191 creating the range type, but we use the result of check_typedef
17192 when examining properties of the type. */
17193 base_type = check_typedef (orig_base_type);
17194
17195 /* The die_type call above may have already set the type for this DIE. */
17196 range_type = get_die_type (die, cu);
17197 if (range_type)
17198 return range_type;
17199
17200 low.kind = PROP_CONST;
17201 high.kind = PROP_CONST;
17202 high.data.const_val = 0;
17203
17204 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17205 omitting DW_AT_lower_bound. */
17206 switch (cu->language)
17207 {
17208 case language_c:
17209 case language_cplus:
17210 low.data.const_val = 0;
17211 low_default_is_valid = 1;
17212 break;
17213 case language_fortran:
17214 low.data.const_val = 1;
17215 low_default_is_valid = 1;
17216 break;
17217 case language_d:
17218 case language_objc:
17219 case language_rust:
17220 low.data.const_val = 0;
17221 low_default_is_valid = (cu->header.version >= 4);
17222 break;
17223 case language_ada:
17224 case language_m2:
17225 case language_pascal:
17226 low.data.const_val = 1;
17227 low_default_is_valid = (cu->header.version >= 4);
17228 break;
17229 default:
17230 low.data.const_val = 0;
17231 low_default_is_valid = 0;
17232 break;
17233 }
17234
17235 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17236 if (attr != nullptr)
17237 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17238 else if (!low_default_is_valid)
17239 complaint (_("Missing DW_AT_lower_bound "
17240 "- DIE at %s [in module %s]"),
17241 sect_offset_str (die->sect_off),
17242 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17243
17244 struct attribute *attr_ub, *attr_count;
17245 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17246 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17247 {
17248 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17249 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17250 {
17251 /* If bounds are constant do the final calculation here. */
17252 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17253 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17254 else
17255 high_bound_is_count = 1;
17256 }
17257 else
17258 {
17259 if (attr_ub != NULL)
17260 complaint (_("Unresolved DW_AT_upper_bound "
17261 "- DIE at %s [in module %s]"),
17262 sect_offset_str (die->sect_off),
17263 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17264 if (attr_count != NULL)
17265 complaint (_("Unresolved DW_AT_count "
17266 "- DIE at %s [in module %s]"),
17267 sect_offset_str (die->sect_off),
17268 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17269 }
17270 }
17271
17272 LONGEST bias = 0;
17273 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17274 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17275 bias = bias_attr->constant_value (0);
17276
17277 /* Normally, the DWARF producers are expected to use a signed
17278 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17279 But this is unfortunately not always the case, as witnessed
17280 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17281 is used instead. To work around that ambiguity, we treat
17282 the bounds as signed, and thus sign-extend their values, when
17283 the base type is signed. */
17284 negative_mask =
17285 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17286 if (low.kind == PROP_CONST
17287 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17288 low.data.const_val |= negative_mask;
17289 if (high.kind == PROP_CONST
17290 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17291 high.data.const_val |= negative_mask;
17292
17293 /* Check for bit and byte strides. */
17294 struct dynamic_prop byte_stride_prop;
17295 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17296 if (attr_byte_stride != nullptr)
17297 {
17298 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17299 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17300 prop_type);
17301 }
17302
17303 struct dynamic_prop bit_stride_prop;
17304 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17305 if (attr_bit_stride != nullptr)
17306 {
17307 /* It only makes sense to have either a bit or byte stride. */
17308 if (attr_byte_stride != nullptr)
17309 {
17310 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17311 "- DIE at %s [in module %s]"),
17312 sect_offset_str (die->sect_off),
17313 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17314 attr_bit_stride = nullptr;
17315 }
17316 else
17317 {
17318 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17319 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17320 prop_type);
17321 }
17322 }
17323
17324 if (attr_byte_stride != nullptr
17325 || attr_bit_stride != nullptr)
17326 {
17327 bool byte_stride_p = (attr_byte_stride != nullptr);
17328 struct dynamic_prop *stride
17329 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17330
17331 range_type
17332 = create_range_type_with_stride (NULL, orig_base_type, &low,
17333 &high, bias, stride, byte_stride_p);
17334 }
17335 else
17336 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17337
17338 if (high_bound_is_count)
17339 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17340
17341 /* Ada expects an empty array on no boundary attributes. */
17342 if (attr == NULL && cu->language != language_ada)
17343 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17344
17345 name = dwarf2_name (die, cu);
17346 if (name)
17347 TYPE_NAME (range_type) = name;
17348
17349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17350 if (attr != nullptr)
17351 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17352
17353 maybe_set_alignment (cu, die, range_type);
17354
17355 set_die_type (die, range_type, cu);
17356
17357 /* set_die_type should be already done. */
17358 set_descriptive_type (range_type, die, cu);
17359
17360 return range_type;
17361 }
17362
17363 static struct type *
17364 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17365 {
17366 struct type *type;
17367
17368 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17369 NULL);
17370 TYPE_NAME (type) = dwarf2_name (die, cu);
17371
17372 /* In Ada, an unspecified type is typically used when the description
17373 of the type is deferred to a different unit. When encountering
17374 such a type, we treat it as a stub, and try to resolve it later on,
17375 when needed. */
17376 if (cu->language == language_ada)
17377 TYPE_STUB (type) = 1;
17378
17379 return set_die_type (die, type, cu);
17380 }
17381
17382 /* Read a single die and all its descendents. Set the die's sibling
17383 field to NULL; set other fields in the die correctly, and set all
17384 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17385 location of the info_ptr after reading all of those dies. PARENT
17386 is the parent of the die in question. */
17387
17388 static struct die_info *
17389 read_die_and_children (const struct die_reader_specs *reader,
17390 const gdb_byte *info_ptr,
17391 const gdb_byte **new_info_ptr,
17392 struct die_info *parent)
17393 {
17394 struct die_info *die;
17395 const gdb_byte *cur_ptr;
17396
17397 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17398 if (die == NULL)
17399 {
17400 *new_info_ptr = cur_ptr;
17401 return NULL;
17402 }
17403 store_in_ref_table (die, reader->cu);
17404
17405 if (die->has_children)
17406 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17407 else
17408 {
17409 die->child = NULL;
17410 *new_info_ptr = cur_ptr;
17411 }
17412
17413 die->sibling = NULL;
17414 die->parent = parent;
17415 return die;
17416 }
17417
17418 /* Read a die, all of its descendents, and all of its siblings; set
17419 all of the fields of all of the dies correctly. Arguments are as
17420 in read_die_and_children. */
17421
17422 static struct die_info *
17423 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17424 const gdb_byte *info_ptr,
17425 const gdb_byte **new_info_ptr,
17426 struct die_info *parent)
17427 {
17428 struct die_info *first_die, *last_sibling;
17429 const gdb_byte *cur_ptr;
17430
17431 cur_ptr = info_ptr;
17432 first_die = last_sibling = NULL;
17433
17434 while (1)
17435 {
17436 struct die_info *die
17437 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17438
17439 if (die == NULL)
17440 {
17441 *new_info_ptr = cur_ptr;
17442 return first_die;
17443 }
17444
17445 if (!first_die)
17446 first_die = die;
17447 else
17448 last_sibling->sibling = die;
17449
17450 last_sibling = die;
17451 }
17452 }
17453
17454 /* Read a die, all of its descendents, and all of its siblings; set
17455 all of the fields of all of the dies correctly. Arguments are as
17456 in read_die_and_children.
17457 This the main entry point for reading a DIE and all its children. */
17458
17459 static struct die_info *
17460 read_die_and_siblings (const struct die_reader_specs *reader,
17461 const gdb_byte *info_ptr,
17462 const gdb_byte **new_info_ptr,
17463 struct die_info *parent)
17464 {
17465 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17466 new_info_ptr, parent);
17467
17468 if (dwarf_die_debug)
17469 {
17470 fprintf_unfiltered (gdb_stdlog,
17471 "Read die from %s@0x%x of %s:\n",
17472 reader->die_section->get_name (),
17473 (unsigned) (info_ptr - reader->die_section->buffer),
17474 bfd_get_filename (reader->abfd));
17475 dump_die (die, dwarf_die_debug);
17476 }
17477
17478 return die;
17479 }
17480
17481 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17482 attributes.
17483 The caller is responsible for filling in the extra attributes
17484 and updating (*DIEP)->num_attrs.
17485 Set DIEP to point to a newly allocated die with its information,
17486 except for its child, sibling, and parent fields. */
17487
17488 static const gdb_byte *
17489 read_full_die_1 (const struct die_reader_specs *reader,
17490 struct die_info **diep, const gdb_byte *info_ptr,
17491 int num_extra_attrs)
17492 {
17493 unsigned int abbrev_number, bytes_read, i;
17494 struct abbrev_info *abbrev;
17495 struct die_info *die;
17496 struct dwarf2_cu *cu = reader->cu;
17497 bfd *abfd = reader->abfd;
17498
17499 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17500 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17501 info_ptr += bytes_read;
17502 if (!abbrev_number)
17503 {
17504 *diep = NULL;
17505 return info_ptr;
17506 }
17507
17508 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17509 if (!abbrev)
17510 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17511 abbrev_number,
17512 bfd_get_filename (abfd));
17513
17514 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17515 die->sect_off = sect_off;
17516 die->tag = abbrev->tag;
17517 die->abbrev = abbrev_number;
17518 die->has_children = abbrev->has_children;
17519
17520 /* Make the result usable.
17521 The caller needs to update num_attrs after adding the extra
17522 attributes. */
17523 die->num_attrs = abbrev->num_attrs;
17524
17525 std::vector<int> indexes_that_need_reprocess;
17526 for (i = 0; i < abbrev->num_attrs; ++i)
17527 {
17528 bool need_reprocess;
17529 info_ptr =
17530 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17531 info_ptr, &need_reprocess);
17532 if (need_reprocess)
17533 indexes_that_need_reprocess.push_back (i);
17534 }
17535
17536 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17537 if (attr != nullptr)
17538 cu->str_offsets_base = DW_UNSND (attr);
17539
17540 auto maybe_addr_base = die->addr_base ();
17541 if (maybe_addr_base.has_value ())
17542 cu->addr_base = *maybe_addr_base;
17543 for (int index : indexes_that_need_reprocess)
17544 read_attribute_reprocess (reader, &die->attrs[index]);
17545 *diep = die;
17546 return info_ptr;
17547 }
17548
17549 /* Read a die and all its attributes.
17550 Set DIEP to point to a newly allocated die with its information,
17551 except for its child, sibling, and parent fields. */
17552
17553 static const gdb_byte *
17554 read_full_die (const struct die_reader_specs *reader,
17555 struct die_info **diep, const gdb_byte *info_ptr)
17556 {
17557 const gdb_byte *result;
17558
17559 result = read_full_die_1 (reader, diep, info_ptr, 0);
17560
17561 if (dwarf_die_debug)
17562 {
17563 fprintf_unfiltered (gdb_stdlog,
17564 "Read die from %s@0x%x of %s:\n",
17565 reader->die_section->get_name (),
17566 (unsigned) (info_ptr - reader->die_section->buffer),
17567 bfd_get_filename (reader->abfd));
17568 dump_die (*diep, dwarf_die_debug);
17569 }
17570
17571 return result;
17572 }
17573 \f
17574
17575 /* Returns nonzero if TAG represents a type that we might generate a partial
17576 symbol for. */
17577
17578 static int
17579 is_type_tag_for_partial (int tag)
17580 {
17581 switch (tag)
17582 {
17583 #if 0
17584 /* Some types that would be reasonable to generate partial symbols for,
17585 that we don't at present. */
17586 case DW_TAG_array_type:
17587 case DW_TAG_file_type:
17588 case DW_TAG_ptr_to_member_type:
17589 case DW_TAG_set_type:
17590 case DW_TAG_string_type:
17591 case DW_TAG_subroutine_type:
17592 #endif
17593 case DW_TAG_base_type:
17594 case DW_TAG_class_type:
17595 case DW_TAG_interface_type:
17596 case DW_TAG_enumeration_type:
17597 case DW_TAG_structure_type:
17598 case DW_TAG_subrange_type:
17599 case DW_TAG_typedef:
17600 case DW_TAG_union_type:
17601 return 1;
17602 default:
17603 return 0;
17604 }
17605 }
17606
17607 /* Load all DIEs that are interesting for partial symbols into memory. */
17608
17609 static struct partial_die_info *
17610 load_partial_dies (const struct die_reader_specs *reader,
17611 const gdb_byte *info_ptr, int building_psymtab)
17612 {
17613 struct dwarf2_cu *cu = reader->cu;
17614 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17615 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17616 unsigned int bytes_read;
17617 unsigned int load_all = 0;
17618 int nesting_level = 1;
17619
17620 parent_die = NULL;
17621 last_die = NULL;
17622
17623 gdb_assert (cu->per_cu != NULL);
17624 if (cu->per_cu->load_all_dies)
17625 load_all = 1;
17626
17627 cu->partial_dies
17628 = htab_create_alloc_ex (cu->header.length / 12,
17629 partial_die_hash,
17630 partial_die_eq,
17631 NULL,
17632 &cu->comp_unit_obstack,
17633 hashtab_obstack_allocate,
17634 dummy_obstack_deallocate);
17635
17636 while (1)
17637 {
17638 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17639
17640 /* A NULL abbrev means the end of a series of children. */
17641 if (abbrev == NULL)
17642 {
17643 if (--nesting_level == 0)
17644 return first_die;
17645
17646 info_ptr += bytes_read;
17647 last_die = parent_die;
17648 parent_die = parent_die->die_parent;
17649 continue;
17650 }
17651
17652 /* Check for template arguments. We never save these; if
17653 they're seen, we just mark the parent, and go on our way. */
17654 if (parent_die != NULL
17655 && cu->language == language_cplus
17656 && (abbrev->tag == DW_TAG_template_type_param
17657 || abbrev->tag == DW_TAG_template_value_param))
17658 {
17659 parent_die->has_template_arguments = 1;
17660
17661 if (!load_all)
17662 {
17663 /* We don't need a partial DIE for the template argument. */
17664 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17665 continue;
17666 }
17667 }
17668
17669 /* We only recurse into c++ subprograms looking for template arguments.
17670 Skip their other children. */
17671 if (!load_all
17672 && cu->language == language_cplus
17673 && parent_die != NULL
17674 && parent_die->tag == DW_TAG_subprogram)
17675 {
17676 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17677 continue;
17678 }
17679
17680 /* Check whether this DIE is interesting enough to save. Normally
17681 we would not be interested in members here, but there may be
17682 later variables referencing them via DW_AT_specification (for
17683 static members). */
17684 if (!load_all
17685 && !is_type_tag_for_partial (abbrev->tag)
17686 && abbrev->tag != DW_TAG_constant
17687 && abbrev->tag != DW_TAG_enumerator
17688 && abbrev->tag != DW_TAG_subprogram
17689 && abbrev->tag != DW_TAG_inlined_subroutine
17690 && abbrev->tag != DW_TAG_lexical_block
17691 && abbrev->tag != DW_TAG_variable
17692 && abbrev->tag != DW_TAG_namespace
17693 && abbrev->tag != DW_TAG_module
17694 && abbrev->tag != DW_TAG_member
17695 && abbrev->tag != DW_TAG_imported_unit
17696 && abbrev->tag != DW_TAG_imported_declaration)
17697 {
17698 /* Otherwise we skip to the next sibling, if any. */
17699 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17700 continue;
17701 }
17702
17703 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17704 abbrev);
17705
17706 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17707
17708 /* This two-pass algorithm for processing partial symbols has a
17709 high cost in cache pressure. Thus, handle some simple cases
17710 here which cover the majority of C partial symbols. DIEs
17711 which neither have specification tags in them, nor could have
17712 specification tags elsewhere pointing at them, can simply be
17713 processed and discarded.
17714
17715 This segment is also optional; scan_partial_symbols and
17716 add_partial_symbol will handle these DIEs if we chain
17717 them in normally. When compilers which do not emit large
17718 quantities of duplicate debug information are more common,
17719 this code can probably be removed. */
17720
17721 /* Any complete simple types at the top level (pretty much all
17722 of them, for a language without namespaces), can be processed
17723 directly. */
17724 if (parent_die == NULL
17725 && pdi.has_specification == 0
17726 && pdi.is_declaration == 0
17727 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17728 || pdi.tag == DW_TAG_base_type
17729 || pdi.tag == DW_TAG_subrange_type))
17730 {
17731 if (building_psymtab && pdi.name != NULL)
17732 add_psymbol_to_list (pdi.name, false,
17733 VAR_DOMAIN, LOC_TYPEDEF, -1,
17734 psymbol_placement::STATIC,
17735 0, cu->language, objfile);
17736 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17737 continue;
17738 }
17739
17740 /* The exception for DW_TAG_typedef with has_children above is
17741 a workaround of GCC PR debug/47510. In the case of this complaint
17742 type_name_or_error will error on such types later.
17743
17744 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17745 it could not find the child DIEs referenced later, this is checked
17746 above. In correct DWARF DW_TAG_typedef should have no children. */
17747
17748 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17749 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17750 "- DIE at %s [in module %s]"),
17751 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17752
17753 /* If we're at the second level, and we're an enumerator, and
17754 our parent has no specification (meaning possibly lives in a
17755 namespace elsewhere), then we can add the partial symbol now
17756 instead of queueing it. */
17757 if (pdi.tag == DW_TAG_enumerator
17758 && parent_die != NULL
17759 && parent_die->die_parent == NULL
17760 && parent_die->tag == DW_TAG_enumeration_type
17761 && parent_die->has_specification == 0)
17762 {
17763 if (pdi.name == NULL)
17764 complaint (_("malformed enumerator DIE ignored"));
17765 else if (building_psymtab)
17766 add_psymbol_to_list (pdi.name, false,
17767 VAR_DOMAIN, LOC_CONST, -1,
17768 cu->language == language_cplus
17769 ? psymbol_placement::GLOBAL
17770 : psymbol_placement::STATIC,
17771 0, cu->language, objfile);
17772
17773 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17774 continue;
17775 }
17776
17777 struct partial_die_info *part_die
17778 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17779
17780 /* We'll save this DIE so link it in. */
17781 part_die->die_parent = parent_die;
17782 part_die->die_sibling = NULL;
17783 part_die->die_child = NULL;
17784
17785 if (last_die && last_die == parent_die)
17786 last_die->die_child = part_die;
17787 else if (last_die)
17788 last_die->die_sibling = part_die;
17789
17790 last_die = part_die;
17791
17792 if (first_die == NULL)
17793 first_die = part_die;
17794
17795 /* Maybe add the DIE to the hash table. Not all DIEs that we
17796 find interesting need to be in the hash table, because we
17797 also have the parent/sibling/child chains; only those that we
17798 might refer to by offset later during partial symbol reading.
17799
17800 For now this means things that might have be the target of a
17801 DW_AT_specification, DW_AT_abstract_origin, or
17802 DW_AT_extension. DW_AT_extension will refer only to
17803 namespaces; DW_AT_abstract_origin refers to functions (and
17804 many things under the function DIE, but we do not recurse
17805 into function DIEs during partial symbol reading) and
17806 possibly variables as well; DW_AT_specification refers to
17807 declarations. Declarations ought to have the DW_AT_declaration
17808 flag. It happens that GCC forgets to put it in sometimes, but
17809 only for functions, not for types.
17810
17811 Adding more things than necessary to the hash table is harmless
17812 except for the performance cost. Adding too few will result in
17813 wasted time in find_partial_die, when we reread the compilation
17814 unit with load_all_dies set. */
17815
17816 if (load_all
17817 || abbrev->tag == DW_TAG_constant
17818 || abbrev->tag == DW_TAG_subprogram
17819 || abbrev->tag == DW_TAG_variable
17820 || abbrev->tag == DW_TAG_namespace
17821 || part_die->is_declaration)
17822 {
17823 void **slot;
17824
17825 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17826 to_underlying (part_die->sect_off),
17827 INSERT);
17828 *slot = part_die;
17829 }
17830
17831 /* For some DIEs we want to follow their children (if any). For C
17832 we have no reason to follow the children of structures; for other
17833 languages we have to, so that we can get at method physnames
17834 to infer fully qualified class names, for DW_AT_specification,
17835 and for C++ template arguments. For C++, we also look one level
17836 inside functions to find template arguments (if the name of the
17837 function does not already contain the template arguments).
17838
17839 For Ada and Fortran, we need to scan the children of subprograms
17840 and lexical blocks as well because these languages allow the
17841 definition of nested entities that could be interesting for the
17842 debugger, such as nested subprograms for instance. */
17843 if (last_die->has_children
17844 && (load_all
17845 || last_die->tag == DW_TAG_namespace
17846 || last_die->tag == DW_TAG_module
17847 || last_die->tag == DW_TAG_enumeration_type
17848 || (cu->language == language_cplus
17849 && last_die->tag == DW_TAG_subprogram
17850 && (last_die->name == NULL
17851 || strchr (last_die->name, '<') == NULL))
17852 || (cu->language != language_c
17853 && (last_die->tag == DW_TAG_class_type
17854 || last_die->tag == DW_TAG_interface_type
17855 || last_die->tag == DW_TAG_structure_type
17856 || last_die->tag == DW_TAG_union_type))
17857 || ((cu->language == language_ada
17858 || cu->language == language_fortran)
17859 && (last_die->tag == DW_TAG_subprogram
17860 || last_die->tag == DW_TAG_lexical_block))))
17861 {
17862 nesting_level++;
17863 parent_die = last_die;
17864 continue;
17865 }
17866
17867 /* Otherwise we skip to the next sibling, if any. */
17868 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17869
17870 /* Back to the top, do it again. */
17871 }
17872 }
17873
17874 partial_die_info::partial_die_info (sect_offset sect_off_,
17875 struct abbrev_info *abbrev)
17876 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17877 {
17878 }
17879
17880 /* Read a minimal amount of information into the minimal die structure.
17881 INFO_PTR should point just after the initial uleb128 of a DIE. */
17882
17883 const gdb_byte *
17884 partial_die_info::read (const struct die_reader_specs *reader,
17885 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17886 {
17887 struct dwarf2_cu *cu = reader->cu;
17888 struct dwarf2_per_objfile *dwarf2_per_objfile
17889 = cu->per_cu->dwarf2_per_objfile;
17890 unsigned int i;
17891 int has_low_pc_attr = 0;
17892 int has_high_pc_attr = 0;
17893 int high_pc_relative = 0;
17894
17895 for (i = 0; i < abbrev.num_attrs; ++i)
17896 {
17897 attribute attr;
17898 bool need_reprocess;
17899 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
17900 info_ptr, &need_reprocess);
17901 /* String and address offsets that need to do the reprocessing have
17902 already been read at this point, so there is no need to wait until
17903 the loop terminates to do the reprocessing. */
17904 if (need_reprocess)
17905 read_attribute_reprocess (reader, &attr);
17906 /* Store the data if it is of an attribute we want to keep in a
17907 partial symbol table. */
17908 switch (attr.name)
17909 {
17910 case DW_AT_name:
17911 switch (tag)
17912 {
17913 case DW_TAG_compile_unit:
17914 case DW_TAG_partial_unit:
17915 case DW_TAG_type_unit:
17916 /* Compilation units have a DW_AT_name that is a filename, not
17917 a source language identifier. */
17918 case DW_TAG_enumeration_type:
17919 case DW_TAG_enumerator:
17920 /* These tags always have simple identifiers already; no need
17921 to canonicalize them. */
17922 name = DW_STRING (&attr);
17923 break;
17924 default:
17925 {
17926 struct objfile *objfile = dwarf2_per_objfile->objfile;
17927
17928 name
17929 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17930 }
17931 break;
17932 }
17933 break;
17934 case DW_AT_linkage_name:
17935 case DW_AT_MIPS_linkage_name:
17936 /* Note that both forms of linkage name might appear. We
17937 assume they will be the same, and we only store the last
17938 one we see. */
17939 linkage_name = DW_STRING (&attr);
17940 break;
17941 case DW_AT_low_pc:
17942 has_low_pc_attr = 1;
17943 lowpc = attr.value_as_address ();
17944 break;
17945 case DW_AT_high_pc:
17946 has_high_pc_attr = 1;
17947 highpc = attr.value_as_address ();
17948 if (cu->header.version >= 4 && attr.form_is_constant ())
17949 high_pc_relative = 1;
17950 break;
17951 case DW_AT_location:
17952 /* Support the .debug_loc offsets. */
17953 if (attr.form_is_block ())
17954 {
17955 d.locdesc = DW_BLOCK (&attr);
17956 }
17957 else if (attr.form_is_section_offset ())
17958 {
17959 dwarf2_complex_location_expr_complaint ();
17960 }
17961 else
17962 {
17963 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17964 "partial symbol information");
17965 }
17966 break;
17967 case DW_AT_external:
17968 is_external = DW_UNSND (&attr);
17969 break;
17970 case DW_AT_declaration:
17971 is_declaration = DW_UNSND (&attr);
17972 break;
17973 case DW_AT_type:
17974 has_type = 1;
17975 break;
17976 case DW_AT_abstract_origin:
17977 case DW_AT_specification:
17978 case DW_AT_extension:
17979 has_specification = 1;
17980 spec_offset = attr.get_ref_die_offset ();
17981 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17982 || cu->per_cu->is_dwz);
17983 break;
17984 case DW_AT_sibling:
17985 /* Ignore absolute siblings, they might point outside of
17986 the current compile unit. */
17987 if (attr.form == DW_FORM_ref_addr)
17988 complaint (_("ignoring absolute DW_AT_sibling"));
17989 else
17990 {
17991 const gdb_byte *buffer = reader->buffer;
17992 sect_offset off = attr.get_ref_die_offset ();
17993 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17994
17995 if (sibling_ptr < info_ptr)
17996 complaint (_("DW_AT_sibling points backwards"));
17997 else if (sibling_ptr > reader->buffer_end)
17998 reader->die_section->overflow_complaint ();
17999 else
18000 sibling = sibling_ptr;
18001 }
18002 break;
18003 case DW_AT_byte_size:
18004 has_byte_size = 1;
18005 break;
18006 case DW_AT_const_value:
18007 has_const_value = 1;
18008 break;
18009 case DW_AT_calling_convention:
18010 /* DWARF doesn't provide a way to identify a program's source-level
18011 entry point. DW_AT_calling_convention attributes are only meant
18012 to describe functions' calling conventions.
18013
18014 However, because it's a necessary piece of information in
18015 Fortran, and before DWARF 4 DW_CC_program was the only
18016 piece of debugging information whose definition refers to
18017 a 'main program' at all, several compilers marked Fortran
18018 main programs with DW_CC_program --- even when those
18019 functions use the standard calling conventions.
18020
18021 Although DWARF now specifies a way to provide this
18022 information, we support this practice for backward
18023 compatibility. */
18024 if (DW_UNSND (&attr) == DW_CC_program
18025 && cu->language == language_fortran)
18026 main_subprogram = 1;
18027 break;
18028 case DW_AT_inline:
18029 if (DW_UNSND (&attr) == DW_INL_inlined
18030 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18031 may_be_inlined = 1;
18032 break;
18033
18034 case DW_AT_import:
18035 if (tag == DW_TAG_imported_unit)
18036 {
18037 d.sect_off = attr.get_ref_die_offset ();
18038 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18039 || cu->per_cu->is_dwz);
18040 }
18041 break;
18042
18043 case DW_AT_main_subprogram:
18044 main_subprogram = DW_UNSND (&attr);
18045 break;
18046
18047 case DW_AT_ranges:
18048 {
18049 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18050 but that requires a full DIE, so instead we just
18051 reimplement it. */
18052 int need_ranges_base = tag != DW_TAG_compile_unit;
18053 unsigned int ranges_offset = (DW_UNSND (&attr)
18054 + (need_ranges_base
18055 ? cu->ranges_base
18056 : 0));
18057
18058 /* Value of the DW_AT_ranges attribute is the offset in the
18059 .debug_ranges section. */
18060 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18061 nullptr))
18062 has_pc_info = 1;
18063 }
18064 break;
18065
18066 default:
18067 break;
18068 }
18069 }
18070
18071 /* For Ada, if both the name and the linkage name appear, we prefer
18072 the latter. This lets "catch exception" work better, regardless
18073 of the order in which the name and linkage name were emitted.
18074 Really, though, this is just a workaround for the fact that gdb
18075 doesn't store both the name and the linkage name. */
18076 if (cu->language == language_ada && linkage_name != nullptr)
18077 name = linkage_name;
18078
18079 if (high_pc_relative)
18080 highpc += lowpc;
18081
18082 if (has_low_pc_attr && has_high_pc_attr)
18083 {
18084 /* When using the GNU linker, .gnu.linkonce. sections are used to
18085 eliminate duplicate copies of functions and vtables and such.
18086 The linker will arbitrarily choose one and discard the others.
18087 The AT_*_pc values for such functions refer to local labels in
18088 these sections. If the section from that file was discarded, the
18089 labels are not in the output, so the relocs get a value of 0.
18090 If this is a discarded function, mark the pc bounds as invalid,
18091 so that GDB will ignore it. */
18092 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18093 {
18094 struct objfile *objfile = dwarf2_per_objfile->objfile;
18095 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18096
18097 complaint (_("DW_AT_low_pc %s is zero "
18098 "for DIE at %s [in module %s]"),
18099 paddress (gdbarch, lowpc),
18100 sect_offset_str (sect_off),
18101 objfile_name (objfile));
18102 }
18103 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18104 else if (lowpc >= highpc)
18105 {
18106 struct objfile *objfile = dwarf2_per_objfile->objfile;
18107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18108
18109 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18110 "for DIE at %s [in module %s]"),
18111 paddress (gdbarch, lowpc),
18112 paddress (gdbarch, highpc),
18113 sect_offset_str (sect_off),
18114 objfile_name (objfile));
18115 }
18116 else
18117 has_pc_info = 1;
18118 }
18119
18120 return info_ptr;
18121 }
18122
18123 /* Find a cached partial DIE at OFFSET in CU. */
18124
18125 struct partial_die_info *
18126 dwarf2_cu::find_partial_die (sect_offset sect_off)
18127 {
18128 struct partial_die_info *lookup_die = NULL;
18129 struct partial_die_info part_die (sect_off);
18130
18131 lookup_die = ((struct partial_die_info *)
18132 htab_find_with_hash (partial_dies, &part_die,
18133 to_underlying (sect_off)));
18134
18135 return lookup_die;
18136 }
18137
18138 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18139 except in the case of .debug_types DIEs which do not reference
18140 outside their CU (they do however referencing other types via
18141 DW_FORM_ref_sig8). */
18142
18143 static const struct cu_partial_die_info
18144 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18145 {
18146 struct dwarf2_per_objfile *dwarf2_per_objfile
18147 = cu->per_cu->dwarf2_per_objfile;
18148 struct objfile *objfile = dwarf2_per_objfile->objfile;
18149 struct dwarf2_per_cu_data *per_cu = NULL;
18150 struct partial_die_info *pd = NULL;
18151
18152 if (offset_in_dwz == cu->per_cu->is_dwz
18153 && cu->header.offset_in_cu_p (sect_off))
18154 {
18155 pd = cu->find_partial_die (sect_off);
18156 if (pd != NULL)
18157 return { cu, pd };
18158 /* We missed recording what we needed.
18159 Load all dies and try again. */
18160 per_cu = cu->per_cu;
18161 }
18162 else
18163 {
18164 /* TUs don't reference other CUs/TUs (except via type signatures). */
18165 if (cu->per_cu->is_debug_types)
18166 {
18167 error (_("Dwarf Error: Type Unit at offset %s contains"
18168 " external reference to offset %s [in module %s].\n"),
18169 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18170 bfd_get_filename (objfile->obfd));
18171 }
18172 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18173 dwarf2_per_objfile);
18174
18175 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18176 load_partial_comp_unit (per_cu);
18177
18178 per_cu->cu->last_used = 0;
18179 pd = per_cu->cu->find_partial_die (sect_off);
18180 }
18181
18182 /* If we didn't find it, and not all dies have been loaded,
18183 load them all and try again. */
18184
18185 if (pd == NULL && per_cu->load_all_dies == 0)
18186 {
18187 per_cu->load_all_dies = 1;
18188
18189 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18190 THIS_CU->cu may already be in use. So we can't just free it and
18191 replace its DIEs with the ones we read in. Instead, we leave those
18192 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18193 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18194 set. */
18195 load_partial_comp_unit (per_cu);
18196
18197 pd = per_cu->cu->find_partial_die (sect_off);
18198 }
18199
18200 if (pd == NULL)
18201 internal_error (__FILE__, __LINE__,
18202 _("could not find partial DIE %s "
18203 "in cache [from module %s]\n"),
18204 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18205 return { per_cu->cu, pd };
18206 }
18207
18208 /* See if we can figure out if the class lives in a namespace. We do
18209 this by looking for a member function; its demangled name will
18210 contain namespace info, if there is any. */
18211
18212 static void
18213 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18214 struct dwarf2_cu *cu)
18215 {
18216 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18217 what template types look like, because the demangler
18218 frequently doesn't give the same name as the debug info. We
18219 could fix this by only using the demangled name to get the
18220 prefix (but see comment in read_structure_type). */
18221
18222 struct partial_die_info *real_pdi;
18223 struct partial_die_info *child_pdi;
18224
18225 /* If this DIE (this DIE's specification, if any) has a parent, then
18226 we should not do this. We'll prepend the parent's fully qualified
18227 name when we create the partial symbol. */
18228
18229 real_pdi = struct_pdi;
18230 while (real_pdi->has_specification)
18231 {
18232 auto res = find_partial_die (real_pdi->spec_offset,
18233 real_pdi->spec_is_dwz, cu);
18234 real_pdi = res.pdi;
18235 cu = res.cu;
18236 }
18237
18238 if (real_pdi->die_parent != NULL)
18239 return;
18240
18241 for (child_pdi = struct_pdi->die_child;
18242 child_pdi != NULL;
18243 child_pdi = child_pdi->die_sibling)
18244 {
18245 if (child_pdi->tag == DW_TAG_subprogram
18246 && child_pdi->linkage_name != NULL)
18247 {
18248 gdb::unique_xmalloc_ptr<char> actual_class_name
18249 (language_class_name_from_physname (cu->language_defn,
18250 child_pdi->linkage_name));
18251 if (actual_class_name != NULL)
18252 {
18253 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18254 struct_pdi->name = objfile->intern (actual_class_name.get ());
18255 }
18256 break;
18257 }
18258 }
18259 }
18260
18261 /* Return true if a DIE with TAG may have the DW_AT_const_value
18262 attribute. */
18263
18264 static bool
18265 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18266 {
18267 switch (tag)
18268 {
18269 case DW_TAG_constant:
18270 case DW_TAG_enumerator:
18271 case DW_TAG_formal_parameter:
18272 case DW_TAG_template_value_param:
18273 case DW_TAG_variable:
18274 return true;
18275 }
18276
18277 return false;
18278 }
18279
18280 void
18281 partial_die_info::fixup (struct dwarf2_cu *cu)
18282 {
18283 /* Once we've fixed up a die, there's no point in doing so again.
18284 This also avoids a memory leak if we were to call
18285 guess_partial_die_structure_name multiple times. */
18286 if (fixup_called)
18287 return;
18288
18289 /* If we found a reference attribute and the DIE has no name, try
18290 to find a name in the referred to DIE. */
18291
18292 if (name == NULL && has_specification)
18293 {
18294 struct partial_die_info *spec_die;
18295
18296 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18297 spec_die = res.pdi;
18298 cu = res.cu;
18299
18300 spec_die->fixup (cu);
18301
18302 if (spec_die->name)
18303 {
18304 name = spec_die->name;
18305
18306 /* Copy DW_AT_external attribute if it is set. */
18307 if (spec_die->is_external)
18308 is_external = spec_die->is_external;
18309 }
18310 }
18311
18312 if (!has_const_value && has_specification
18313 && can_have_DW_AT_const_value_p (tag))
18314 {
18315 struct partial_die_info *spec_die;
18316
18317 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18318 spec_die = res.pdi;
18319 cu = res.cu;
18320
18321 spec_die->fixup (cu);
18322
18323 if (spec_die->has_const_value)
18324 {
18325 /* Copy DW_AT_const_value attribute if it is set. */
18326 has_const_value = spec_die->has_const_value;
18327 }
18328 }
18329
18330 /* Set default names for some unnamed DIEs. */
18331
18332 if (name == NULL && tag == DW_TAG_namespace)
18333 name = CP_ANONYMOUS_NAMESPACE_STR;
18334
18335 /* If there is no parent die to provide a namespace, and there are
18336 children, see if we can determine the namespace from their linkage
18337 name. */
18338 if (cu->language == language_cplus
18339 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18340 && die_parent == NULL
18341 && has_children
18342 && (tag == DW_TAG_class_type
18343 || tag == DW_TAG_structure_type
18344 || tag == DW_TAG_union_type))
18345 guess_partial_die_structure_name (this, cu);
18346
18347 /* GCC might emit a nameless struct or union that has a linkage
18348 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18349 if (name == NULL
18350 && (tag == DW_TAG_class_type
18351 || tag == DW_TAG_interface_type
18352 || tag == DW_TAG_structure_type
18353 || tag == DW_TAG_union_type)
18354 && linkage_name != NULL)
18355 {
18356 gdb::unique_xmalloc_ptr<char> demangled
18357 (gdb_demangle (linkage_name, DMGL_TYPES));
18358 if (demangled != nullptr)
18359 {
18360 const char *base;
18361
18362 /* Strip any leading namespaces/classes, keep only the base name.
18363 DW_AT_name for named DIEs does not contain the prefixes. */
18364 base = strrchr (demangled.get (), ':');
18365 if (base && base > demangled.get () && base[-1] == ':')
18366 base++;
18367 else
18368 base = demangled.get ();
18369
18370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18371 name = objfile->intern (base);
18372 }
18373 }
18374
18375 fixup_called = 1;
18376 }
18377
18378 /* Process the attributes that had to be skipped in the first round. These
18379 attributes are the ones that need str_offsets_base or addr_base attributes.
18380 They could not have been processed in the first round, because at the time
18381 the values of str_offsets_base or addr_base may not have been known. */
18382 static void
18383 read_attribute_reprocess (const struct die_reader_specs *reader,
18384 struct attribute *attr)
18385 {
18386 struct dwarf2_cu *cu = reader->cu;
18387 switch (attr->form)
18388 {
18389 case DW_FORM_addrx:
18390 case DW_FORM_GNU_addr_index:
18391 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18392 break;
18393 case DW_FORM_strx:
18394 case DW_FORM_strx1:
18395 case DW_FORM_strx2:
18396 case DW_FORM_strx3:
18397 case DW_FORM_strx4:
18398 case DW_FORM_GNU_str_index:
18399 {
18400 unsigned int str_index = DW_UNSND (attr);
18401 if (reader->dwo_file != NULL)
18402 {
18403 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18404 DW_STRING_IS_CANONICAL (attr) = 0;
18405 }
18406 else
18407 {
18408 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18409 DW_STRING_IS_CANONICAL (attr) = 0;
18410 }
18411 break;
18412 }
18413 default:
18414 gdb_assert_not_reached (_("Unexpected DWARF form."));
18415 }
18416 }
18417
18418 /* Read an attribute value described by an attribute form. */
18419
18420 static const gdb_byte *
18421 read_attribute_value (const struct die_reader_specs *reader,
18422 struct attribute *attr, unsigned form,
18423 LONGEST implicit_const, const gdb_byte *info_ptr,
18424 bool *need_reprocess)
18425 {
18426 struct dwarf2_cu *cu = reader->cu;
18427 struct dwarf2_per_objfile *dwarf2_per_objfile
18428 = cu->per_cu->dwarf2_per_objfile;
18429 struct objfile *objfile = dwarf2_per_objfile->objfile;
18430 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18431 bfd *abfd = reader->abfd;
18432 struct comp_unit_head *cu_header = &cu->header;
18433 unsigned int bytes_read;
18434 struct dwarf_block *blk;
18435 *need_reprocess = false;
18436
18437 attr->form = (enum dwarf_form) form;
18438 switch (form)
18439 {
18440 case DW_FORM_ref_addr:
18441 if (cu->header.version == 2)
18442 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18443 &bytes_read);
18444 else
18445 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18446 &bytes_read);
18447 info_ptr += bytes_read;
18448 break;
18449 case DW_FORM_GNU_ref_alt:
18450 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18451 info_ptr += bytes_read;
18452 break;
18453 case DW_FORM_addr:
18454 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18455 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18456 info_ptr += bytes_read;
18457 break;
18458 case DW_FORM_block2:
18459 blk = dwarf_alloc_block (cu);
18460 blk->size = read_2_bytes (abfd, info_ptr);
18461 info_ptr += 2;
18462 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18463 info_ptr += blk->size;
18464 DW_BLOCK (attr) = blk;
18465 break;
18466 case DW_FORM_block4:
18467 blk = dwarf_alloc_block (cu);
18468 blk->size = read_4_bytes (abfd, info_ptr);
18469 info_ptr += 4;
18470 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18471 info_ptr += blk->size;
18472 DW_BLOCK (attr) = blk;
18473 break;
18474 case DW_FORM_data2:
18475 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18476 info_ptr += 2;
18477 break;
18478 case DW_FORM_data4:
18479 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18480 info_ptr += 4;
18481 break;
18482 case DW_FORM_data8:
18483 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18484 info_ptr += 8;
18485 break;
18486 case DW_FORM_data16:
18487 blk = dwarf_alloc_block (cu);
18488 blk->size = 16;
18489 blk->data = read_n_bytes (abfd, info_ptr, 16);
18490 info_ptr += 16;
18491 DW_BLOCK (attr) = blk;
18492 break;
18493 case DW_FORM_sec_offset:
18494 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18495 info_ptr += bytes_read;
18496 break;
18497 case DW_FORM_string:
18498 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18499 DW_STRING_IS_CANONICAL (attr) = 0;
18500 info_ptr += bytes_read;
18501 break;
18502 case DW_FORM_strp:
18503 if (!cu->per_cu->is_dwz)
18504 {
18505 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18506 abfd, info_ptr, cu_header,
18507 &bytes_read);
18508 DW_STRING_IS_CANONICAL (attr) = 0;
18509 info_ptr += bytes_read;
18510 break;
18511 }
18512 /* FALLTHROUGH */
18513 case DW_FORM_line_strp:
18514 if (!cu->per_cu->is_dwz)
18515 {
18516 DW_STRING (attr)
18517 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18518 &bytes_read);
18519 DW_STRING_IS_CANONICAL (attr) = 0;
18520 info_ptr += bytes_read;
18521 break;
18522 }
18523 /* FALLTHROUGH */
18524 case DW_FORM_GNU_strp_alt:
18525 {
18526 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18527 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18528 &bytes_read);
18529
18530 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18531 DW_STRING_IS_CANONICAL (attr) = 0;
18532 info_ptr += bytes_read;
18533 }
18534 break;
18535 case DW_FORM_exprloc:
18536 case DW_FORM_block:
18537 blk = dwarf_alloc_block (cu);
18538 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18539 info_ptr += bytes_read;
18540 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18541 info_ptr += blk->size;
18542 DW_BLOCK (attr) = blk;
18543 break;
18544 case DW_FORM_block1:
18545 blk = dwarf_alloc_block (cu);
18546 blk->size = read_1_byte (abfd, info_ptr);
18547 info_ptr += 1;
18548 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18549 info_ptr += blk->size;
18550 DW_BLOCK (attr) = blk;
18551 break;
18552 case DW_FORM_data1:
18553 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18554 info_ptr += 1;
18555 break;
18556 case DW_FORM_flag:
18557 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18558 info_ptr += 1;
18559 break;
18560 case DW_FORM_flag_present:
18561 DW_UNSND (attr) = 1;
18562 break;
18563 case DW_FORM_sdata:
18564 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18565 info_ptr += bytes_read;
18566 break;
18567 case DW_FORM_udata:
18568 case DW_FORM_rnglistx:
18569 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18570 info_ptr += bytes_read;
18571 break;
18572 case DW_FORM_ref1:
18573 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18574 + read_1_byte (abfd, info_ptr));
18575 info_ptr += 1;
18576 break;
18577 case DW_FORM_ref2:
18578 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18579 + read_2_bytes (abfd, info_ptr));
18580 info_ptr += 2;
18581 break;
18582 case DW_FORM_ref4:
18583 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18584 + read_4_bytes (abfd, info_ptr));
18585 info_ptr += 4;
18586 break;
18587 case DW_FORM_ref8:
18588 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18589 + read_8_bytes (abfd, info_ptr));
18590 info_ptr += 8;
18591 break;
18592 case DW_FORM_ref_sig8:
18593 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18594 info_ptr += 8;
18595 break;
18596 case DW_FORM_ref_udata:
18597 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18598 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18599 info_ptr += bytes_read;
18600 break;
18601 case DW_FORM_indirect:
18602 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18603 info_ptr += bytes_read;
18604 if (form == DW_FORM_implicit_const)
18605 {
18606 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18607 info_ptr += bytes_read;
18608 }
18609 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18610 info_ptr, need_reprocess);
18611 break;
18612 case DW_FORM_implicit_const:
18613 DW_SND (attr) = implicit_const;
18614 break;
18615 case DW_FORM_addrx:
18616 case DW_FORM_GNU_addr_index:
18617 *need_reprocess = true;
18618 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18619 info_ptr += bytes_read;
18620 break;
18621 case DW_FORM_strx:
18622 case DW_FORM_strx1:
18623 case DW_FORM_strx2:
18624 case DW_FORM_strx3:
18625 case DW_FORM_strx4:
18626 case DW_FORM_GNU_str_index:
18627 {
18628 ULONGEST str_index;
18629 if (form == DW_FORM_strx1)
18630 {
18631 str_index = read_1_byte (abfd, info_ptr);
18632 info_ptr += 1;
18633 }
18634 else if (form == DW_FORM_strx2)
18635 {
18636 str_index = read_2_bytes (abfd, info_ptr);
18637 info_ptr += 2;
18638 }
18639 else if (form == DW_FORM_strx3)
18640 {
18641 str_index = read_3_bytes (abfd, info_ptr);
18642 info_ptr += 3;
18643 }
18644 else if (form == DW_FORM_strx4)
18645 {
18646 str_index = read_4_bytes (abfd, info_ptr);
18647 info_ptr += 4;
18648 }
18649 else
18650 {
18651 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18652 info_ptr += bytes_read;
18653 }
18654 *need_reprocess = true;
18655 DW_UNSND (attr) = str_index;
18656 }
18657 break;
18658 default:
18659 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18660 dwarf_form_name (form),
18661 bfd_get_filename (abfd));
18662 }
18663
18664 /* Super hack. */
18665 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18666 attr->form = DW_FORM_GNU_ref_alt;
18667
18668 /* We have seen instances where the compiler tried to emit a byte
18669 size attribute of -1 which ended up being encoded as an unsigned
18670 0xffffffff. Although 0xffffffff is technically a valid size value,
18671 an object of this size seems pretty unlikely so we can relatively
18672 safely treat these cases as if the size attribute was invalid and
18673 treat them as zero by default. */
18674 if (attr->name == DW_AT_byte_size
18675 && form == DW_FORM_data4
18676 && DW_UNSND (attr) >= 0xffffffff)
18677 {
18678 complaint
18679 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18680 hex_string (DW_UNSND (attr)));
18681 DW_UNSND (attr) = 0;
18682 }
18683
18684 return info_ptr;
18685 }
18686
18687 /* Read an attribute described by an abbreviated attribute. */
18688
18689 static const gdb_byte *
18690 read_attribute (const struct die_reader_specs *reader,
18691 struct attribute *attr, struct attr_abbrev *abbrev,
18692 const gdb_byte *info_ptr, bool *need_reprocess)
18693 {
18694 attr->name = abbrev->name;
18695 return read_attribute_value (reader, attr, abbrev->form,
18696 abbrev->implicit_const, info_ptr,
18697 need_reprocess);
18698 }
18699
18700 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18701
18702 static const char *
18703 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18704 LONGEST str_offset)
18705 {
18706 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18707 str_offset, "DW_FORM_strp");
18708 }
18709
18710 /* Return pointer to string at .debug_str offset as read from BUF.
18711 BUF is assumed to be in a compilation unit described by CU_HEADER.
18712 Return *BYTES_READ_PTR count of bytes read from BUF. */
18713
18714 static const char *
18715 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18716 const gdb_byte *buf,
18717 const struct comp_unit_head *cu_header,
18718 unsigned int *bytes_read_ptr)
18719 {
18720 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18721
18722 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18723 }
18724
18725 /* See read.h. */
18726
18727 const char *
18728 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18729 const struct comp_unit_head *cu_header,
18730 unsigned int *bytes_read_ptr)
18731 {
18732 bfd *abfd = objfile->obfd;
18733 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18734
18735 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18736 }
18737
18738 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18739 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18740 ADDR_SIZE is the size of addresses from the CU header. */
18741
18742 static CORE_ADDR
18743 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18744 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18745 int addr_size)
18746 {
18747 struct objfile *objfile = dwarf2_per_objfile->objfile;
18748 bfd *abfd = objfile->obfd;
18749 const gdb_byte *info_ptr;
18750 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18751
18752 dwarf2_per_objfile->addr.read (objfile);
18753 if (dwarf2_per_objfile->addr.buffer == NULL)
18754 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18755 objfile_name (objfile));
18756 if (addr_base_or_zero + addr_index * addr_size
18757 >= dwarf2_per_objfile->addr.size)
18758 error (_("DW_FORM_addr_index pointing outside of "
18759 ".debug_addr section [in module %s]"),
18760 objfile_name (objfile));
18761 info_ptr = (dwarf2_per_objfile->addr.buffer
18762 + addr_base_or_zero + addr_index * addr_size);
18763 if (addr_size == 4)
18764 return bfd_get_32 (abfd, info_ptr);
18765 else
18766 return bfd_get_64 (abfd, info_ptr);
18767 }
18768
18769 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18770
18771 static CORE_ADDR
18772 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18773 {
18774 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18775 cu->addr_base, cu->header.addr_size);
18776 }
18777
18778 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18779
18780 static CORE_ADDR
18781 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18782 unsigned int *bytes_read)
18783 {
18784 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18785 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18786
18787 return read_addr_index (cu, addr_index);
18788 }
18789
18790 /* See read.h. */
18791
18792 CORE_ADDR
18793 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18794 {
18795 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18796 struct dwarf2_cu *cu = per_cu->cu;
18797 gdb::optional<ULONGEST> addr_base;
18798 int addr_size;
18799
18800 /* We need addr_base and addr_size.
18801 If we don't have PER_CU->cu, we have to get it.
18802 Nasty, but the alternative is storing the needed info in PER_CU,
18803 which at this point doesn't seem justified: it's not clear how frequently
18804 it would get used and it would increase the size of every PER_CU.
18805 Entry points like dwarf2_per_cu_addr_size do a similar thing
18806 so we're not in uncharted territory here.
18807 Alas we need to be a bit more complicated as addr_base is contained
18808 in the DIE.
18809
18810 We don't need to read the entire CU(/TU).
18811 We just need the header and top level die.
18812
18813 IWBN to use the aging mechanism to let us lazily later discard the CU.
18814 For now we skip this optimization. */
18815
18816 if (cu != NULL)
18817 {
18818 addr_base = cu->addr_base;
18819 addr_size = cu->header.addr_size;
18820 }
18821 else
18822 {
18823 cutu_reader reader (per_cu, NULL, 0, false);
18824 addr_base = reader.cu->addr_base;
18825 addr_size = reader.cu->header.addr_size;
18826 }
18827
18828 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18829 addr_size);
18830 }
18831
18832 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18833 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18834 DWO file. */
18835
18836 static const char *
18837 read_str_index (struct dwarf2_cu *cu,
18838 struct dwarf2_section_info *str_section,
18839 struct dwarf2_section_info *str_offsets_section,
18840 ULONGEST str_offsets_base, ULONGEST str_index)
18841 {
18842 struct dwarf2_per_objfile *dwarf2_per_objfile
18843 = cu->per_cu->dwarf2_per_objfile;
18844 struct objfile *objfile = dwarf2_per_objfile->objfile;
18845 const char *objf_name = objfile_name (objfile);
18846 bfd *abfd = objfile->obfd;
18847 const gdb_byte *info_ptr;
18848 ULONGEST str_offset;
18849 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18850
18851 str_section->read (objfile);
18852 str_offsets_section->read (objfile);
18853 if (str_section->buffer == NULL)
18854 error (_("%s used without %s section"
18855 " in CU at offset %s [in module %s]"),
18856 form_name, str_section->get_name (),
18857 sect_offset_str (cu->header.sect_off), objf_name);
18858 if (str_offsets_section->buffer == NULL)
18859 error (_("%s used without %s section"
18860 " in CU at offset %s [in module %s]"),
18861 form_name, str_section->get_name (),
18862 sect_offset_str (cu->header.sect_off), objf_name);
18863 info_ptr = (str_offsets_section->buffer
18864 + str_offsets_base
18865 + str_index * cu->header.offset_size);
18866 if (cu->header.offset_size == 4)
18867 str_offset = bfd_get_32 (abfd, info_ptr);
18868 else
18869 str_offset = bfd_get_64 (abfd, info_ptr);
18870 if (str_offset >= str_section->size)
18871 error (_("Offset from %s pointing outside of"
18872 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18873 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18874 return (const char *) (str_section->buffer + str_offset);
18875 }
18876
18877 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18878
18879 static const char *
18880 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18881 {
18882 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18883 ? reader->cu->header.addr_size : 0;
18884 return read_str_index (reader->cu,
18885 &reader->dwo_file->sections.str,
18886 &reader->dwo_file->sections.str_offsets,
18887 str_offsets_base, str_index);
18888 }
18889
18890 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18891
18892 static const char *
18893 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18894 {
18895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18896 const char *objf_name = objfile_name (objfile);
18897 static const char form_name[] = "DW_FORM_GNU_str_index";
18898 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18899
18900 if (!cu->str_offsets_base.has_value ())
18901 error (_("%s used in Fission stub without %s"
18902 " in CU at offset 0x%lx [in module %s]"),
18903 form_name, str_offsets_attr_name,
18904 (long) cu->header.offset_size, objf_name);
18905
18906 return read_str_index (cu,
18907 &cu->per_cu->dwarf2_per_objfile->str,
18908 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18909 *cu->str_offsets_base, str_index);
18910 }
18911
18912 /* Return the length of an LEB128 number in BUF. */
18913
18914 static int
18915 leb128_size (const gdb_byte *buf)
18916 {
18917 const gdb_byte *begin = buf;
18918 gdb_byte byte;
18919
18920 while (1)
18921 {
18922 byte = *buf++;
18923 if ((byte & 128) == 0)
18924 return buf - begin;
18925 }
18926 }
18927
18928 static void
18929 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18930 {
18931 switch (lang)
18932 {
18933 case DW_LANG_C89:
18934 case DW_LANG_C99:
18935 case DW_LANG_C11:
18936 case DW_LANG_C:
18937 case DW_LANG_UPC:
18938 cu->language = language_c;
18939 break;
18940 case DW_LANG_Java:
18941 case DW_LANG_C_plus_plus:
18942 case DW_LANG_C_plus_plus_11:
18943 case DW_LANG_C_plus_plus_14:
18944 cu->language = language_cplus;
18945 break;
18946 case DW_LANG_D:
18947 cu->language = language_d;
18948 break;
18949 case DW_LANG_Fortran77:
18950 case DW_LANG_Fortran90:
18951 case DW_LANG_Fortran95:
18952 case DW_LANG_Fortran03:
18953 case DW_LANG_Fortran08:
18954 cu->language = language_fortran;
18955 break;
18956 case DW_LANG_Go:
18957 cu->language = language_go;
18958 break;
18959 case DW_LANG_Mips_Assembler:
18960 cu->language = language_asm;
18961 break;
18962 case DW_LANG_Ada83:
18963 case DW_LANG_Ada95:
18964 cu->language = language_ada;
18965 break;
18966 case DW_LANG_Modula2:
18967 cu->language = language_m2;
18968 break;
18969 case DW_LANG_Pascal83:
18970 cu->language = language_pascal;
18971 break;
18972 case DW_LANG_ObjC:
18973 cu->language = language_objc;
18974 break;
18975 case DW_LANG_Rust:
18976 case DW_LANG_Rust_old:
18977 cu->language = language_rust;
18978 break;
18979 case DW_LANG_Cobol74:
18980 case DW_LANG_Cobol85:
18981 default:
18982 cu->language = language_minimal;
18983 break;
18984 }
18985 cu->language_defn = language_def (cu->language);
18986 }
18987
18988 /* Return the named attribute or NULL if not there. */
18989
18990 static struct attribute *
18991 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18992 {
18993 for (;;)
18994 {
18995 unsigned int i;
18996 struct attribute *spec = NULL;
18997
18998 for (i = 0; i < die->num_attrs; ++i)
18999 {
19000 if (die->attrs[i].name == name)
19001 return &die->attrs[i];
19002 if (die->attrs[i].name == DW_AT_specification
19003 || die->attrs[i].name == DW_AT_abstract_origin)
19004 spec = &die->attrs[i];
19005 }
19006
19007 if (!spec)
19008 break;
19009
19010 die = follow_die_ref (die, spec, &cu);
19011 }
19012
19013 return NULL;
19014 }
19015
19016 /* Return the string associated with a string-typed attribute, or NULL if it
19017 is either not found or is of an incorrect type. */
19018
19019 static const char *
19020 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19021 {
19022 struct attribute *attr;
19023 const char *str = NULL;
19024
19025 attr = dwarf2_attr (die, name, cu);
19026
19027 if (attr != NULL)
19028 {
19029 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19030 || attr->form == DW_FORM_string
19031 || attr->form == DW_FORM_strx
19032 || attr->form == DW_FORM_strx1
19033 || attr->form == DW_FORM_strx2
19034 || attr->form == DW_FORM_strx3
19035 || attr->form == DW_FORM_strx4
19036 || attr->form == DW_FORM_GNU_str_index
19037 || attr->form == DW_FORM_GNU_strp_alt)
19038 str = DW_STRING (attr);
19039 else
19040 complaint (_("string type expected for attribute %s for "
19041 "DIE at %s in module %s"),
19042 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19043 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19044 }
19045
19046 return str;
19047 }
19048
19049 /* Return the dwo name or NULL if not present. If present, it is in either
19050 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19051 static const char *
19052 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19053 {
19054 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19055 if (dwo_name == nullptr)
19056 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19057 return dwo_name;
19058 }
19059
19060 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19061 and holds a non-zero value. This function should only be used for
19062 DW_FORM_flag or DW_FORM_flag_present attributes. */
19063
19064 static int
19065 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19066 {
19067 struct attribute *attr = dwarf2_attr (die, name, cu);
19068
19069 return (attr && DW_UNSND (attr));
19070 }
19071
19072 static int
19073 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19074 {
19075 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19076 which value is non-zero. However, we have to be careful with
19077 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19078 (via dwarf2_flag_true_p) follows this attribute. So we may
19079 end up accidently finding a declaration attribute that belongs
19080 to a different DIE referenced by the specification attribute,
19081 even though the given DIE does not have a declaration attribute. */
19082 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19083 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19084 }
19085
19086 /* Return the die giving the specification for DIE, if there is
19087 one. *SPEC_CU is the CU containing DIE on input, and the CU
19088 containing the return value on output. If there is no
19089 specification, but there is an abstract origin, that is
19090 returned. */
19091
19092 static struct die_info *
19093 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19094 {
19095 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19096 *spec_cu);
19097
19098 if (spec_attr == NULL)
19099 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19100
19101 if (spec_attr == NULL)
19102 return NULL;
19103 else
19104 return follow_die_ref (die, spec_attr, spec_cu);
19105 }
19106
19107 /* Stub for free_line_header to match void * callback types. */
19108
19109 static void
19110 free_line_header_voidp (void *arg)
19111 {
19112 struct line_header *lh = (struct line_header *) arg;
19113
19114 delete lh;
19115 }
19116
19117 /* A convenience function to find the proper .debug_line section for a CU. */
19118
19119 static struct dwarf2_section_info *
19120 get_debug_line_section (struct dwarf2_cu *cu)
19121 {
19122 struct dwarf2_section_info *section;
19123 struct dwarf2_per_objfile *dwarf2_per_objfile
19124 = cu->per_cu->dwarf2_per_objfile;
19125
19126 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19127 DWO file. */
19128 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19129 section = &cu->dwo_unit->dwo_file->sections.line;
19130 else if (cu->per_cu->is_dwz)
19131 {
19132 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19133
19134 section = &dwz->line;
19135 }
19136 else
19137 section = &dwarf2_per_objfile->line;
19138
19139 return section;
19140 }
19141
19142 /* Read the statement program header starting at OFFSET in
19143 .debug_line, or .debug_line.dwo. Return a pointer
19144 to a struct line_header, allocated using xmalloc.
19145 Returns NULL if there is a problem reading the header, e.g., if it
19146 has a version we don't understand.
19147
19148 NOTE: the strings in the include directory and file name tables of
19149 the returned object point into the dwarf line section buffer,
19150 and must not be freed. */
19151
19152 static line_header_up
19153 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19154 {
19155 struct dwarf2_section_info *section;
19156 struct dwarf2_per_objfile *dwarf2_per_objfile
19157 = cu->per_cu->dwarf2_per_objfile;
19158
19159 section = get_debug_line_section (cu);
19160 section->read (dwarf2_per_objfile->objfile);
19161 if (section->buffer == NULL)
19162 {
19163 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19164 complaint (_("missing .debug_line.dwo section"));
19165 else
19166 complaint (_("missing .debug_line section"));
19167 return 0;
19168 }
19169
19170 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19171 dwarf2_per_objfile, section,
19172 &cu->header);
19173 }
19174
19175 /* Subroutine of dwarf_decode_lines to simplify it.
19176 Return the file name of the psymtab for the given file_entry.
19177 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19178 If space for the result is malloc'd, *NAME_HOLDER will be set.
19179 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19180
19181 static const char *
19182 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19183 const dwarf2_psymtab *pst,
19184 const char *comp_dir,
19185 gdb::unique_xmalloc_ptr<char> *name_holder)
19186 {
19187 const char *include_name = fe.name;
19188 const char *include_name_to_compare = include_name;
19189 const char *pst_filename;
19190 int file_is_pst;
19191
19192 const char *dir_name = fe.include_dir (lh);
19193
19194 gdb::unique_xmalloc_ptr<char> hold_compare;
19195 if (!IS_ABSOLUTE_PATH (include_name)
19196 && (dir_name != NULL || comp_dir != NULL))
19197 {
19198 /* Avoid creating a duplicate psymtab for PST.
19199 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19200 Before we do the comparison, however, we need to account
19201 for DIR_NAME and COMP_DIR.
19202 First prepend dir_name (if non-NULL). If we still don't
19203 have an absolute path prepend comp_dir (if non-NULL).
19204 However, the directory we record in the include-file's
19205 psymtab does not contain COMP_DIR (to match the
19206 corresponding symtab(s)).
19207
19208 Example:
19209
19210 bash$ cd /tmp
19211 bash$ gcc -g ./hello.c
19212 include_name = "hello.c"
19213 dir_name = "."
19214 DW_AT_comp_dir = comp_dir = "/tmp"
19215 DW_AT_name = "./hello.c"
19216
19217 */
19218
19219 if (dir_name != NULL)
19220 {
19221 name_holder->reset (concat (dir_name, SLASH_STRING,
19222 include_name, (char *) NULL));
19223 include_name = name_holder->get ();
19224 include_name_to_compare = include_name;
19225 }
19226 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19227 {
19228 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19229 include_name, (char *) NULL));
19230 include_name_to_compare = hold_compare.get ();
19231 }
19232 }
19233
19234 pst_filename = pst->filename;
19235 gdb::unique_xmalloc_ptr<char> copied_name;
19236 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19237 {
19238 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19239 pst_filename, (char *) NULL));
19240 pst_filename = copied_name.get ();
19241 }
19242
19243 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19244
19245 if (file_is_pst)
19246 return NULL;
19247 return include_name;
19248 }
19249
19250 /* State machine to track the state of the line number program. */
19251
19252 class lnp_state_machine
19253 {
19254 public:
19255 /* Initialize a machine state for the start of a line number
19256 program. */
19257 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19258 bool record_lines_p);
19259
19260 file_entry *current_file ()
19261 {
19262 /* lh->file_names is 0-based, but the file name numbers in the
19263 statement program are 1-based. */
19264 return m_line_header->file_name_at (m_file);
19265 }
19266
19267 /* Record the line in the state machine. END_SEQUENCE is true if
19268 we're processing the end of a sequence. */
19269 void record_line (bool end_sequence);
19270
19271 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19272 nop-out rest of the lines in this sequence. */
19273 void check_line_address (struct dwarf2_cu *cu,
19274 const gdb_byte *line_ptr,
19275 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19276
19277 void handle_set_discriminator (unsigned int discriminator)
19278 {
19279 m_discriminator = discriminator;
19280 m_line_has_non_zero_discriminator |= discriminator != 0;
19281 }
19282
19283 /* Handle DW_LNE_set_address. */
19284 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19285 {
19286 m_op_index = 0;
19287 address += baseaddr;
19288 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19289 }
19290
19291 /* Handle DW_LNS_advance_pc. */
19292 void handle_advance_pc (CORE_ADDR adjust);
19293
19294 /* Handle a special opcode. */
19295 void handle_special_opcode (unsigned char op_code);
19296
19297 /* Handle DW_LNS_advance_line. */
19298 void handle_advance_line (int line_delta)
19299 {
19300 advance_line (line_delta);
19301 }
19302
19303 /* Handle DW_LNS_set_file. */
19304 void handle_set_file (file_name_index file);
19305
19306 /* Handle DW_LNS_negate_stmt. */
19307 void handle_negate_stmt ()
19308 {
19309 m_is_stmt = !m_is_stmt;
19310 }
19311
19312 /* Handle DW_LNS_const_add_pc. */
19313 void handle_const_add_pc ();
19314
19315 /* Handle DW_LNS_fixed_advance_pc. */
19316 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19317 {
19318 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19319 m_op_index = 0;
19320 }
19321
19322 /* Handle DW_LNS_copy. */
19323 void handle_copy ()
19324 {
19325 record_line (false);
19326 m_discriminator = 0;
19327 }
19328
19329 /* Handle DW_LNE_end_sequence. */
19330 void handle_end_sequence ()
19331 {
19332 m_currently_recording_lines = true;
19333 }
19334
19335 private:
19336 /* Advance the line by LINE_DELTA. */
19337 void advance_line (int line_delta)
19338 {
19339 m_line += line_delta;
19340
19341 if (line_delta != 0)
19342 m_line_has_non_zero_discriminator = m_discriminator != 0;
19343 }
19344
19345 struct dwarf2_cu *m_cu;
19346
19347 gdbarch *m_gdbarch;
19348
19349 /* True if we're recording lines.
19350 Otherwise we're building partial symtabs and are just interested in
19351 finding include files mentioned by the line number program. */
19352 bool m_record_lines_p;
19353
19354 /* The line number header. */
19355 line_header *m_line_header;
19356
19357 /* These are part of the standard DWARF line number state machine,
19358 and initialized according to the DWARF spec. */
19359
19360 unsigned char m_op_index = 0;
19361 /* The line table index of the current file. */
19362 file_name_index m_file = 1;
19363 unsigned int m_line = 1;
19364
19365 /* These are initialized in the constructor. */
19366
19367 CORE_ADDR m_address;
19368 bool m_is_stmt;
19369 unsigned int m_discriminator;
19370
19371 /* Additional bits of state we need to track. */
19372
19373 /* The last file that we called dwarf2_start_subfile for.
19374 This is only used for TLLs. */
19375 unsigned int m_last_file = 0;
19376 /* The last file a line number was recorded for. */
19377 struct subfile *m_last_subfile = NULL;
19378
19379 /* When true, record the lines we decode. */
19380 bool m_currently_recording_lines = false;
19381
19382 /* The last line number that was recorded, used to coalesce
19383 consecutive entries for the same line. This can happen, for
19384 example, when discriminators are present. PR 17276. */
19385 unsigned int m_last_line = 0;
19386 bool m_line_has_non_zero_discriminator = false;
19387 };
19388
19389 void
19390 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19391 {
19392 CORE_ADDR addr_adj = (((m_op_index + adjust)
19393 / m_line_header->maximum_ops_per_instruction)
19394 * m_line_header->minimum_instruction_length);
19395 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19396 m_op_index = ((m_op_index + adjust)
19397 % m_line_header->maximum_ops_per_instruction);
19398 }
19399
19400 void
19401 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19402 {
19403 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19404 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19405 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19406 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19407 / m_line_header->maximum_ops_per_instruction)
19408 * m_line_header->minimum_instruction_length);
19409 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19410 m_op_index = ((m_op_index + adj_opcode_d)
19411 % m_line_header->maximum_ops_per_instruction);
19412
19413 int line_delta = m_line_header->line_base + adj_opcode_r;
19414 advance_line (line_delta);
19415 record_line (false);
19416 m_discriminator = 0;
19417 }
19418
19419 void
19420 lnp_state_machine::handle_set_file (file_name_index file)
19421 {
19422 m_file = file;
19423
19424 const file_entry *fe = current_file ();
19425 if (fe == NULL)
19426 dwarf2_debug_line_missing_file_complaint ();
19427 else if (m_record_lines_p)
19428 {
19429 const char *dir = fe->include_dir (m_line_header);
19430
19431 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19432 m_line_has_non_zero_discriminator = m_discriminator != 0;
19433 dwarf2_start_subfile (m_cu, fe->name, dir);
19434 }
19435 }
19436
19437 void
19438 lnp_state_machine::handle_const_add_pc ()
19439 {
19440 CORE_ADDR adjust
19441 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19442
19443 CORE_ADDR addr_adj
19444 = (((m_op_index + adjust)
19445 / m_line_header->maximum_ops_per_instruction)
19446 * m_line_header->minimum_instruction_length);
19447
19448 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19449 m_op_index = ((m_op_index + adjust)
19450 % m_line_header->maximum_ops_per_instruction);
19451 }
19452
19453 /* Return non-zero if we should add LINE to the line number table.
19454 LINE is the line to add, LAST_LINE is the last line that was added,
19455 LAST_SUBFILE is the subfile for LAST_LINE.
19456 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19457 had a non-zero discriminator.
19458
19459 We have to be careful in the presence of discriminators.
19460 E.g., for this line:
19461
19462 for (i = 0; i < 100000; i++);
19463
19464 clang can emit four line number entries for that one line,
19465 each with a different discriminator.
19466 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19467
19468 However, we want gdb to coalesce all four entries into one.
19469 Otherwise the user could stepi into the middle of the line and
19470 gdb would get confused about whether the pc really was in the
19471 middle of the line.
19472
19473 Things are further complicated by the fact that two consecutive
19474 line number entries for the same line is a heuristic used by gcc
19475 to denote the end of the prologue. So we can't just discard duplicate
19476 entries, we have to be selective about it. The heuristic we use is
19477 that we only collapse consecutive entries for the same line if at least
19478 one of those entries has a non-zero discriminator. PR 17276.
19479
19480 Note: Addresses in the line number state machine can never go backwards
19481 within one sequence, thus this coalescing is ok. */
19482
19483 static int
19484 dwarf_record_line_p (struct dwarf2_cu *cu,
19485 unsigned int line, unsigned int last_line,
19486 int line_has_non_zero_discriminator,
19487 struct subfile *last_subfile)
19488 {
19489 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19490 return 1;
19491 if (line != last_line)
19492 return 1;
19493 /* Same line for the same file that we've seen already.
19494 As a last check, for pr 17276, only record the line if the line
19495 has never had a non-zero discriminator. */
19496 if (!line_has_non_zero_discriminator)
19497 return 1;
19498 return 0;
19499 }
19500
19501 /* Use the CU's builder to record line number LINE beginning at
19502 address ADDRESS in the line table of subfile SUBFILE. */
19503
19504 static void
19505 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19506 unsigned int line, CORE_ADDR address, bool is_stmt,
19507 struct dwarf2_cu *cu)
19508 {
19509 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19510
19511 if (dwarf_line_debug)
19512 {
19513 fprintf_unfiltered (gdb_stdlog,
19514 "Recording line %u, file %s, address %s\n",
19515 line, lbasename (subfile->name),
19516 paddress (gdbarch, address));
19517 }
19518
19519 if (cu != nullptr)
19520 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19521 }
19522
19523 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19524 Mark the end of a set of line number records.
19525 The arguments are the same as for dwarf_record_line_1.
19526 If SUBFILE is NULL the request is ignored. */
19527
19528 static void
19529 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19530 CORE_ADDR address, struct dwarf2_cu *cu)
19531 {
19532 if (subfile == NULL)
19533 return;
19534
19535 if (dwarf_line_debug)
19536 {
19537 fprintf_unfiltered (gdb_stdlog,
19538 "Finishing current line, file %s, address %s\n",
19539 lbasename (subfile->name),
19540 paddress (gdbarch, address));
19541 }
19542
19543 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19544 }
19545
19546 void
19547 lnp_state_machine::record_line (bool end_sequence)
19548 {
19549 if (dwarf_line_debug)
19550 {
19551 fprintf_unfiltered (gdb_stdlog,
19552 "Processing actual line %u: file %u,"
19553 " address %s, is_stmt %u, discrim %u%s\n",
19554 m_line, m_file,
19555 paddress (m_gdbarch, m_address),
19556 m_is_stmt, m_discriminator,
19557 (end_sequence ? "\t(end sequence)" : ""));
19558 }
19559
19560 file_entry *fe = current_file ();
19561
19562 if (fe == NULL)
19563 dwarf2_debug_line_missing_file_complaint ();
19564 /* For now we ignore lines not starting on an instruction boundary.
19565 But not when processing end_sequence for compatibility with the
19566 previous version of the code. */
19567 else if (m_op_index == 0 || end_sequence)
19568 {
19569 fe->included_p = 1;
19570 if (m_record_lines_p)
19571 {
19572 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19573 || end_sequence)
19574 {
19575 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19576 m_currently_recording_lines ? m_cu : nullptr);
19577 }
19578
19579 if (!end_sequence)
19580 {
19581 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19582
19583 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19584 m_line_has_non_zero_discriminator,
19585 m_last_subfile))
19586 {
19587 buildsym_compunit *builder = m_cu->get_builder ();
19588 dwarf_record_line_1 (m_gdbarch,
19589 builder->get_current_subfile (),
19590 m_line, m_address, is_stmt,
19591 m_currently_recording_lines ? m_cu : nullptr);
19592 }
19593 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19594 m_last_line = m_line;
19595 }
19596 }
19597 }
19598 }
19599
19600 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19601 line_header *lh, bool record_lines_p)
19602 {
19603 m_cu = cu;
19604 m_gdbarch = arch;
19605 m_record_lines_p = record_lines_p;
19606 m_line_header = lh;
19607
19608 m_currently_recording_lines = true;
19609
19610 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19611 was a line entry for it so that the backend has a chance to adjust it
19612 and also record it in case it needs it. This is currently used by MIPS
19613 code, cf. `mips_adjust_dwarf2_line'. */
19614 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19615 m_is_stmt = lh->default_is_stmt;
19616 m_discriminator = 0;
19617 }
19618
19619 void
19620 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19621 const gdb_byte *line_ptr,
19622 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19623 {
19624 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19625 the pc range of the CU. However, we restrict the test to only ADDRESS
19626 values of zero to preserve GDB's previous behaviour which is to handle
19627 the specific case of a function being GC'd by the linker. */
19628
19629 if (address == 0 && address < unrelocated_lowpc)
19630 {
19631 /* This line table is for a function which has been
19632 GCd by the linker. Ignore it. PR gdb/12528 */
19633
19634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19635 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19636
19637 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19638 line_offset, objfile_name (objfile));
19639 m_currently_recording_lines = false;
19640 /* Note: m_currently_recording_lines is left as false until we see
19641 DW_LNE_end_sequence. */
19642 }
19643 }
19644
19645 /* Subroutine of dwarf_decode_lines to simplify it.
19646 Process the line number information in LH.
19647 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19648 program in order to set included_p for every referenced header. */
19649
19650 static void
19651 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19652 const int decode_for_pst_p, CORE_ADDR lowpc)
19653 {
19654 const gdb_byte *line_ptr, *extended_end;
19655 const gdb_byte *line_end;
19656 unsigned int bytes_read, extended_len;
19657 unsigned char op_code, extended_op;
19658 CORE_ADDR baseaddr;
19659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19660 bfd *abfd = objfile->obfd;
19661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19662 /* True if we're recording line info (as opposed to building partial
19663 symtabs and just interested in finding include files mentioned by
19664 the line number program). */
19665 bool record_lines_p = !decode_for_pst_p;
19666
19667 baseaddr = objfile->text_section_offset ();
19668
19669 line_ptr = lh->statement_program_start;
19670 line_end = lh->statement_program_end;
19671
19672 /* Read the statement sequences until there's nothing left. */
19673 while (line_ptr < line_end)
19674 {
19675 /* The DWARF line number program state machine. Reset the state
19676 machine at the start of each sequence. */
19677 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19678 bool end_sequence = false;
19679
19680 if (record_lines_p)
19681 {
19682 /* Start a subfile for the current file of the state
19683 machine. */
19684 const file_entry *fe = state_machine.current_file ();
19685
19686 if (fe != NULL)
19687 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19688 }
19689
19690 /* Decode the table. */
19691 while (line_ptr < line_end && !end_sequence)
19692 {
19693 op_code = read_1_byte (abfd, line_ptr);
19694 line_ptr += 1;
19695
19696 if (op_code >= lh->opcode_base)
19697 {
19698 /* Special opcode. */
19699 state_machine.handle_special_opcode (op_code);
19700 }
19701 else switch (op_code)
19702 {
19703 case DW_LNS_extended_op:
19704 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19705 &bytes_read);
19706 line_ptr += bytes_read;
19707 extended_end = line_ptr + extended_len;
19708 extended_op = read_1_byte (abfd, line_ptr);
19709 line_ptr += 1;
19710 switch (extended_op)
19711 {
19712 case DW_LNE_end_sequence:
19713 state_machine.handle_end_sequence ();
19714 end_sequence = true;
19715 break;
19716 case DW_LNE_set_address:
19717 {
19718 CORE_ADDR address
19719 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19720 line_ptr += bytes_read;
19721
19722 state_machine.check_line_address (cu, line_ptr,
19723 lowpc - baseaddr, address);
19724 state_machine.handle_set_address (baseaddr, address);
19725 }
19726 break;
19727 case DW_LNE_define_file:
19728 {
19729 const char *cur_file;
19730 unsigned int mod_time, length;
19731 dir_index dindex;
19732
19733 cur_file = read_direct_string (abfd, line_ptr,
19734 &bytes_read);
19735 line_ptr += bytes_read;
19736 dindex = (dir_index)
19737 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19738 line_ptr += bytes_read;
19739 mod_time =
19740 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19741 line_ptr += bytes_read;
19742 length =
19743 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19744 line_ptr += bytes_read;
19745 lh->add_file_name (cur_file, dindex, mod_time, length);
19746 }
19747 break;
19748 case DW_LNE_set_discriminator:
19749 {
19750 /* The discriminator is not interesting to the
19751 debugger; just ignore it. We still need to
19752 check its value though:
19753 if there are consecutive entries for the same
19754 (non-prologue) line we want to coalesce them.
19755 PR 17276. */
19756 unsigned int discr
19757 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19758 line_ptr += bytes_read;
19759
19760 state_machine.handle_set_discriminator (discr);
19761 }
19762 break;
19763 default:
19764 complaint (_("mangled .debug_line section"));
19765 return;
19766 }
19767 /* Make sure that we parsed the extended op correctly. If e.g.
19768 we expected a different address size than the producer used,
19769 we may have read the wrong number of bytes. */
19770 if (line_ptr != extended_end)
19771 {
19772 complaint (_("mangled .debug_line section"));
19773 return;
19774 }
19775 break;
19776 case DW_LNS_copy:
19777 state_machine.handle_copy ();
19778 break;
19779 case DW_LNS_advance_pc:
19780 {
19781 CORE_ADDR adjust
19782 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19783 line_ptr += bytes_read;
19784
19785 state_machine.handle_advance_pc (adjust);
19786 }
19787 break;
19788 case DW_LNS_advance_line:
19789 {
19790 int line_delta
19791 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19792 line_ptr += bytes_read;
19793
19794 state_machine.handle_advance_line (line_delta);
19795 }
19796 break;
19797 case DW_LNS_set_file:
19798 {
19799 file_name_index file
19800 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19801 &bytes_read);
19802 line_ptr += bytes_read;
19803
19804 state_machine.handle_set_file (file);
19805 }
19806 break;
19807 case DW_LNS_set_column:
19808 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19809 line_ptr += bytes_read;
19810 break;
19811 case DW_LNS_negate_stmt:
19812 state_machine.handle_negate_stmt ();
19813 break;
19814 case DW_LNS_set_basic_block:
19815 break;
19816 /* Add to the address register of the state machine the
19817 address increment value corresponding to special opcode
19818 255. I.e., this value is scaled by the minimum
19819 instruction length since special opcode 255 would have
19820 scaled the increment. */
19821 case DW_LNS_const_add_pc:
19822 state_machine.handle_const_add_pc ();
19823 break;
19824 case DW_LNS_fixed_advance_pc:
19825 {
19826 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19827 line_ptr += 2;
19828
19829 state_machine.handle_fixed_advance_pc (addr_adj);
19830 }
19831 break;
19832 default:
19833 {
19834 /* Unknown standard opcode, ignore it. */
19835 int i;
19836
19837 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19838 {
19839 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19840 line_ptr += bytes_read;
19841 }
19842 }
19843 }
19844 }
19845
19846 if (!end_sequence)
19847 dwarf2_debug_line_missing_end_sequence_complaint ();
19848
19849 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19850 in which case we still finish recording the last line). */
19851 state_machine.record_line (true);
19852 }
19853 }
19854
19855 /* Decode the Line Number Program (LNP) for the given line_header
19856 structure and CU. The actual information extracted and the type
19857 of structures created from the LNP depends on the value of PST.
19858
19859 1. If PST is NULL, then this procedure uses the data from the program
19860 to create all necessary symbol tables, and their linetables.
19861
19862 2. If PST is not NULL, this procedure reads the program to determine
19863 the list of files included by the unit represented by PST, and
19864 builds all the associated partial symbol tables.
19865
19866 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19867 It is used for relative paths in the line table.
19868 NOTE: When processing partial symtabs (pst != NULL),
19869 comp_dir == pst->dirname.
19870
19871 NOTE: It is important that psymtabs have the same file name (via strcmp)
19872 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19873 symtab we don't use it in the name of the psymtabs we create.
19874 E.g. expand_line_sal requires this when finding psymtabs to expand.
19875 A good testcase for this is mb-inline.exp.
19876
19877 LOWPC is the lowest address in CU (or 0 if not known).
19878
19879 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19880 for its PC<->lines mapping information. Otherwise only the filename
19881 table is read in. */
19882
19883 static void
19884 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19885 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19886 CORE_ADDR lowpc, int decode_mapping)
19887 {
19888 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19889 const int decode_for_pst_p = (pst != NULL);
19890
19891 if (decode_mapping)
19892 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19893
19894 if (decode_for_pst_p)
19895 {
19896 /* Now that we're done scanning the Line Header Program, we can
19897 create the psymtab of each included file. */
19898 for (auto &file_entry : lh->file_names ())
19899 if (file_entry.included_p == 1)
19900 {
19901 gdb::unique_xmalloc_ptr<char> name_holder;
19902 const char *include_name =
19903 psymtab_include_file_name (lh, file_entry, pst,
19904 comp_dir, &name_holder);
19905 if (include_name != NULL)
19906 dwarf2_create_include_psymtab (include_name, pst, objfile);
19907 }
19908 }
19909 else
19910 {
19911 /* Make sure a symtab is created for every file, even files
19912 which contain only variables (i.e. no code with associated
19913 line numbers). */
19914 buildsym_compunit *builder = cu->get_builder ();
19915 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19916
19917 for (auto &fe : lh->file_names ())
19918 {
19919 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19920 if (builder->get_current_subfile ()->symtab == NULL)
19921 {
19922 builder->get_current_subfile ()->symtab
19923 = allocate_symtab (cust,
19924 builder->get_current_subfile ()->name);
19925 }
19926 fe.symtab = builder->get_current_subfile ()->symtab;
19927 }
19928 }
19929 }
19930
19931 /* Start a subfile for DWARF. FILENAME is the name of the file and
19932 DIRNAME the name of the source directory which contains FILENAME
19933 or NULL if not known.
19934 This routine tries to keep line numbers from identical absolute and
19935 relative file names in a common subfile.
19936
19937 Using the `list' example from the GDB testsuite, which resides in
19938 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19939 of /srcdir/list0.c yields the following debugging information for list0.c:
19940
19941 DW_AT_name: /srcdir/list0.c
19942 DW_AT_comp_dir: /compdir
19943 files.files[0].name: list0.h
19944 files.files[0].dir: /srcdir
19945 files.files[1].name: list0.c
19946 files.files[1].dir: /srcdir
19947
19948 The line number information for list0.c has to end up in a single
19949 subfile, so that `break /srcdir/list0.c:1' works as expected.
19950 start_subfile will ensure that this happens provided that we pass the
19951 concatenation of files.files[1].dir and files.files[1].name as the
19952 subfile's name. */
19953
19954 static void
19955 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19956 const char *dirname)
19957 {
19958 gdb::unique_xmalloc_ptr<char> copy;
19959
19960 /* In order not to lose the line information directory,
19961 we concatenate it to the filename when it makes sense.
19962 Note that the Dwarf3 standard says (speaking of filenames in line
19963 information): ``The directory index is ignored for file names
19964 that represent full path names''. Thus ignoring dirname in the
19965 `else' branch below isn't an issue. */
19966
19967 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19968 {
19969 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19970 filename = copy.get ();
19971 }
19972
19973 cu->get_builder ()->start_subfile (filename);
19974 }
19975
19976 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19977 buildsym_compunit constructor. */
19978
19979 struct compunit_symtab *
19980 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19981 CORE_ADDR low_pc)
19982 {
19983 gdb_assert (m_builder == nullptr);
19984
19985 m_builder.reset (new struct buildsym_compunit
19986 (per_cu->dwarf2_per_objfile->objfile,
19987 name, comp_dir, language, low_pc));
19988
19989 list_in_scope = get_builder ()->get_file_symbols ();
19990
19991 get_builder ()->record_debugformat ("DWARF 2");
19992 get_builder ()->record_producer (producer);
19993
19994 processing_has_namespace_info = false;
19995
19996 return get_builder ()->get_compunit_symtab ();
19997 }
19998
19999 static void
20000 var_decode_location (struct attribute *attr, struct symbol *sym,
20001 struct dwarf2_cu *cu)
20002 {
20003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20004 struct comp_unit_head *cu_header = &cu->header;
20005
20006 /* NOTE drow/2003-01-30: There used to be a comment and some special
20007 code here to turn a symbol with DW_AT_external and a
20008 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20009 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20010 with some versions of binutils) where shared libraries could have
20011 relocations against symbols in their debug information - the
20012 minimal symbol would have the right address, but the debug info
20013 would not. It's no longer necessary, because we will explicitly
20014 apply relocations when we read in the debug information now. */
20015
20016 /* A DW_AT_location attribute with no contents indicates that a
20017 variable has been optimized away. */
20018 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20019 {
20020 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20021 return;
20022 }
20023
20024 /* Handle one degenerate form of location expression specially, to
20025 preserve GDB's previous behavior when section offsets are
20026 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20027 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20028
20029 if (attr->form_is_block ()
20030 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20031 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20032 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20033 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20034 && (DW_BLOCK (attr)->size
20035 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20036 {
20037 unsigned int dummy;
20038
20039 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20040 SET_SYMBOL_VALUE_ADDRESS
20041 (sym, cu->header.read_address (objfile->obfd,
20042 DW_BLOCK (attr)->data + 1,
20043 &dummy));
20044 else
20045 SET_SYMBOL_VALUE_ADDRESS
20046 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20047 &dummy));
20048 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20049 fixup_symbol_section (sym, objfile);
20050 SET_SYMBOL_VALUE_ADDRESS
20051 (sym,
20052 SYMBOL_VALUE_ADDRESS (sym)
20053 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20054 return;
20055 }
20056
20057 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20058 expression evaluator, and use LOC_COMPUTED only when necessary
20059 (i.e. when the value of a register or memory location is
20060 referenced, or a thread-local block, etc.). Then again, it might
20061 not be worthwhile. I'm assuming that it isn't unless performance
20062 or memory numbers show me otherwise. */
20063
20064 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20065
20066 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20067 cu->has_loclist = true;
20068 }
20069
20070 /* Given a pointer to a DWARF information entry, figure out if we need
20071 to make a symbol table entry for it, and if so, create a new entry
20072 and return a pointer to it.
20073 If TYPE is NULL, determine symbol type from the die, otherwise
20074 used the passed type.
20075 If SPACE is not NULL, use it to hold the new symbol. If it is
20076 NULL, allocate a new symbol on the objfile's obstack. */
20077
20078 static struct symbol *
20079 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20080 struct symbol *space)
20081 {
20082 struct dwarf2_per_objfile *dwarf2_per_objfile
20083 = cu->per_cu->dwarf2_per_objfile;
20084 struct objfile *objfile = dwarf2_per_objfile->objfile;
20085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20086 struct symbol *sym = NULL;
20087 const char *name;
20088 struct attribute *attr = NULL;
20089 struct attribute *attr2 = NULL;
20090 CORE_ADDR baseaddr;
20091 struct pending **list_to_add = NULL;
20092
20093 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20094
20095 baseaddr = objfile->text_section_offset ();
20096
20097 name = dwarf2_name (die, cu);
20098 if (name)
20099 {
20100 const char *linkagename;
20101 int suppress_add = 0;
20102
20103 if (space)
20104 sym = space;
20105 else
20106 sym = allocate_symbol (objfile);
20107 OBJSTAT (objfile, n_syms++);
20108
20109 /* Cache this symbol's name and the name's demangled form (if any). */
20110 sym->set_language (cu->language, &objfile->objfile_obstack);
20111 linkagename = dwarf2_physname (name, die, cu);
20112 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20113
20114 /* Fortran does not have mangling standard and the mangling does differ
20115 between gfortran, iFort etc. */
20116 if (cu->language == language_fortran
20117 && symbol_get_demangled_name (sym) == NULL)
20118 symbol_set_demangled_name (sym,
20119 dwarf2_full_name (name, die, cu),
20120 NULL);
20121
20122 /* Default assumptions.
20123 Use the passed type or decode it from the die. */
20124 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20125 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20126 if (type != NULL)
20127 SYMBOL_TYPE (sym) = type;
20128 else
20129 SYMBOL_TYPE (sym) = die_type (die, cu);
20130 attr = dwarf2_attr (die,
20131 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20132 cu);
20133 if (attr != nullptr)
20134 {
20135 SYMBOL_LINE (sym) = DW_UNSND (attr);
20136 }
20137
20138 attr = dwarf2_attr (die,
20139 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20140 cu);
20141 if (attr != nullptr)
20142 {
20143 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20144 struct file_entry *fe;
20145
20146 if (cu->line_header != NULL)
20147 fe = cu->line_header->file_name_at (file_index);
20148 else
20149 fe = NULL;
20150
20151 if (fe == NULL)
20152 complaint (_("file index out of range"));
20153 else
20154 symbol_set_symtab (sym, fe->symtab);
20155 }
20156
20157 switch (die->tag)
20158 {
20159 case DW_TAG_label:
20160 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20161 if (attr != nullptr)
20162 {
20163 CORE_ADDR addr;
20164
20165 addr = attr->value_as_address ();
20166 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20167 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20168 }
20169 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20170 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20171 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20172 add_symbol_to_list (sym, cu->list_in_scope);
20173 break;
20174 case DW_TAG_subprogram:
20175 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20176 finish_block. */
20177 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20178 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20179 if ((attr2 && (DW_UNSND (attr2) != 0))
20180 || cu->language == language_ada
20181 || cu->language == language_fortran)
20182 {
20183 /* Subprograms marked external are stored as a global symbol.
20184 Ada and Fortran subprograms, whether marked external or
20185 not, are always stored as a global symbol, because we want
20186 to be able to access them globally. For instance, we want
20187 to be able to break on a nested subprogram without having
20188 to specify the context. */
20189 list_to_add = cu->get_builder ()->get_global_symbols ();
20190 }
20191 else
20192 {
20193 list_to_add = cu->list_in_scope;
20194 }
20195 break;
20196 case DW_TAG_inlined_subroutine:
20197 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20198 finish_block. */
20199 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20200 SYMBOL_INLINED (sym) = 1;
20201 list_to_add = cu->list_in_scope;
20202 break;
20203 case DW_TAG_template_value_param:
20204 suppress_add = 1;
20205 /* Fall through. */
20206 case DW_TAG_constant:
20207 case DW_TAG_variable:
20208 case DW_TAG_member:
20209 /* Compilation with minimal debug info may result in
20210 variables with missing type entries. Change the
20211 misleading `void' type to something sensible. */
20212 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20213 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20214
20215 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20216 /* In the case of DW_TAG_member, we should only be called for
20217 static const members. */
20218 if (die->tag == DW_TAG_member)
20219 {
20220 /* dwarf2_add_field uses die_is_declaration,
20221 so we do the same. */
20222 gdb_assert (die_is_declaration (die, cu));
20223 gdb_assert (attr);
20224 }
20225 if (attr != nullptr)
20226 {
20227 dwarf2_const_value (attr, sym, cu);
20228 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20229 if (!suppress_add)
20230 {
20231 if (attr2 && (DW_UNSND (attr2) != 0))
20232 list_to_add = cu->get_builder ()->get_global_symbols ();
20233 else
20234 list_to_add = cu->list_in_scope;
20235 }
20236 break;
20237 }
20238 attr = dwarf2_attr (die, DW_AT_location, cu);
20239 if (attr != nullptr)
20240 {
20241 var_decode_location (attr, sym, cu);
20242 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20243
20244 /* Fortran explicitly imports any global symbols to the local
20245 scope by DW_TAG_common_block. */
20246 if (cu->language == language_fortran && die->parent
20247 && die->parent->tag == DW_TAG_common_block)
20248 attr2 = NULL;
20249
20250 if (SYMBOL_CLASS (sym) == LOC_STATIC
20251 && SYMBOL_VALUE_ADDRESS (sym) == 0
20252 && !dwarf2_per_objfile->has_section_at_zero)
20253 {
20254 /* When a static variable is eliminated by the linker,
20255 the corresponding debug information is not stripped
20256 out, but the variable address is set to null;
20257 do not add such variables into symbol table. */
20258 }
20259 else if (attr2 && (DW_UNSND (attr2) != 0))
20260 {
20261 if (SYMBOL_CLASS (sym) == LOC_STATIC
20262 && (objfile->flags & OBJF_MAINLINE) == 0
20263 && dwarf2_per_objfile->can_copy)
20264 {
20265 /* A global static variable might be subject to
20266 copy relocation. We first check for a local
20267 minsym, though, because maybe the symbol was
20268 marked hidden, in which case this would not
20269 apply. */
20270 bound_minimal_symbol found
20271 = (lookup_minimal_symbol_linkage
20272 (sym->linkage_name (), objfile));
20273 if (found.minsym != nullptr)
20274 sym->maybe_copied = 1;
20275 }
20276
20277 /* A variable with DW_AT_external is never static,
20278 but it may be block-scoped. */
20279 list_to_add
20280 = ((cu->list_in_scope
20281 == cu->get_builder ()->get_file_symbols ())
20282 ? cu->get_builder ()->get_global_symbols ()
20283 : cu->list_in_scope);
20284 }
20285 else
20286 list_to_add = cu->list_in_scope;
20287 }
20288 else
20289 {
20290 /* We do not know the address of this symbol.
20291 If it is an external symbol and we have type information
20292 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20293 The address of the variable will then be determined from
20294 the minimal symbol table whenever the variable is
20295 referenced. */
20296 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20297
20298 /* Fortran explicitly imports any global symbols to the local
20299 scope by DW_TAG_common_block. */
20300 if (cu->language == language_fortran && die->parent
20301 && die->parent->tag == DW_TAG_common_block)
20302 {
20303 /* SYMBOL_CLASS doesn't matter here because
20304 read_common_block is going to reset it. */
20305 if (!suppress_add)
20306 list_to_add = cu->list_in_scope;
20307 }
20308 else if (attr2 && (DW_UNSND (attr2) != 0)
20309 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20310 {
20311 /* A variable with DW_AT_external is never static, but it
20312 may be block-scoped. */
20313 list_to_add
20314 = ((cu->list_in_scope
20315 == cu->get_builder ()->get_file_symbols ())
20316 ? cu->get_builder ()->get_global_symbols ()
20317 : cu->list_in_scope);
20318
20319 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20320 }
20321 else if (!die_is_declaration (die, cu))
20322 {
20323 /* Use the default LOC_OPTIMIZED_OUT class. */
20324 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20325 if (!suppress_add)
20326 list_to_add = cu->list_in_scope;
20327 }
20328 }
20329 break;
20330 case DW_TAG_formal_parameter:
20331 {
20332 /* If we are inside a function, mark this as an argument. If
20333 not, we might be looking at an argument to an inlined function
20334 when we do not have enough information to show inlined frames;
20335 pretend it's a local variable in that case so that the user can
20336 still see it. */
20337 struct context_stack *curr
20338 = cu->get_builder ()->get_current_context_stack ();
20339 if (curr != nullptr && curr->name != nullptr)
20340 SYMBOL_IS_ARGUMENT (sym) = 1;
20341 attr = dwarf2_attr (die, DW_AT_location, cu);
20342 if (attr != nullptr)
20343 {
20344 var_decode_location (attr, sym, cu);
20345 }
20346 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20347 if (attr != nullptr)
20348 {
20349 dwarf2_const_value (attr, sym, cu);
20350 }
20351
20352 list_to_add = cu->list_in_scope;
20353 }
20354 break;
20355 case DW_TAG_unspecified_parameters:
20356 /* From varargs functions; gdb doesn't seem to have any
20357 interest in this information, so just ignore it for now.
20358 (FIXME?) */
20359 break;
20360 case DW_TAG_template_type_param:
20361 suppress_add = 1;
20362 /* Fall through. */
20363 case DW_TAG_class_type:
20364 case DW_TAG_interface_type:
20365 case DW_TAG_structure_type:
20366 case DW_TAG_union_type:
20367 case DW_TAG_set_type:
20368 case DW_TAG_enumeration_type:
20369 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20370 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20371
20372 {
20373 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20374 really ever be static objects: otherwise, if you try
20375 to, say, break of a class's method and you're in a file
20376 which doesn't mention that class, it won't work unless
20377 the check for all static symbols in lookup_symbol_aux
20378 saves you. See the OtherFileClass tests in
20379 gdb.c++/namespace.exp. */
20380
20381 if (!suppress_add)
20382 {
20383 buildsym_compunit *builder = cu->get_builder ();
20384 list_to_add
20385 = (cu->list_in_scope == builder->get_file_symbols ()
20386 && cu->language == language_cplus
20387 ? builder->get_global_symbols ()
20388 : cu->list_in_scope);
20389
20390 /* The semantics of C++ state that "struct foo {
20391 ... }" also defines a typedef for "foo". */
20392 if (cu->language == language_cplus
20393 || cu->language == language_ada
20394 || cu->language == language_d
20395 || cu->language == language_rust)
20396 {
20397 /* The symbol's name is already allocated along
20398 with this objfile, so we don't need to
20399 duplicate it for the type. */
20400 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20401 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20402 }
20403 }
20404 }
20405 break;
20406 case DW_TAG_typedef:
20407 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20408 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20409 list_to_add = cu->list_in_scope;
20410 break;
20411 case DW_TAG_base_type:
20412 case DW_TAG_subrange_type:
20413 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20414 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20415 list_to_add = cu->list_in_scope;
20416 break;
20417 case DW_TAG_enumerator:
20418 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20419 if (attr != nullptr)
20420 {
20421 dwarf2_const_value (attr, sym, cu);
20422 }
20423 {
20424 /* NOTE: carlton/2003-11-10: See comment above in the
20425 DW_TAG_class_type, etc. block. */
20426
20427 list_to_add
20428 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20429 && cu->language == language_cplus
20430 ? cu->get_builder ()->get_global_symbols ()
20431 : cu->list_in_scope);
20432 }
20433 break;
20434 case DW_TAG_imported_declaration:
20435 case DW_TAG_namespace:
20436 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20437 list_to_add = cu->get_builder ()->get_global_symbols ();
20438 break;
20439 case DW_TAG_module:
20440 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20441 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20442 list_to_add = cu->get_builder ()->get_global_symbols ();
20443 break;
20444 case DW_TAG_common_block:
20445 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20446 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20447 add_symbol_to_list (sym, cu->list_in_scope);
20448 break;
20449 default:
20450 /* Not a tag we recognize. Hopefully we aren't processing
20451 trash data, but since we must specifically ignore things
20452 we don't recognize, there is nothing else we should do at
20453 this point. */
20454 complaint (_("unsupported tag: '%s'"),
20455 dwarf_tag_name (die->tag));
20456 break;
20457 }
20458
20459 if (suppress_add)
20460 {
20461 sym->hash_next = objfile->template_symbols;
20462 objfile->template_symbols = sym;
20463 list_to_add = NULL;
20464 }
20465
20466 if (list_to_add != NULL)
20467 add_symbol_to_list (sym, list_to_add);
20468
20469 /* For the benefit of old versions of GCC, check for anonymous
20470 namespaces based on the demangled name. */
20471 if (!cu->processing_has_namespace_info
20472 && cu->language == language_cplus)
20473 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20474 }
20475 return (sym);
20476 }
20477
20478 /* Given an attr with a DW_FORM_dataN value in host byte order,
20479 zero-extend it as appropriate for the symbol's type. The DWARF
20480 standard (v4) is not entirely clear about the meaning of using
20481 DW_FORM_dataN for a constant with a signed type, where the type is
20482 wider than the data. The conclusion of a discussion on the DWARF
20483 list was that this is unspecified. We choose to always zero-extend
20484 because that is the interpretation long in use by GCC. */
20485
20486 static gdb_byte *
20487 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20488 struct dwarf2_cu *cu, LONGEST *value, int bits)
20489 {
20490 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20491 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20492 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20493 LONGEST l = DW_UNSND (attr);
20494
20495 if (bits < sizeof (*value) * 8)
20496 {
20497 l &= ((LONGEST) 1 << bits) - 1;
20498 *value = l;
20499 }
20500 else if (bits == sizeof (*value) * 8)
20501 *value = l;
20502 else
20503 {
20504 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20505 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20506 return bytes;
20507 }
20508
20509 return NULL;
20510 }
20511
20512 /* Read a constant value from an attribute. Either set *VALUE, or if
20513 the value does not fit in *VALUE, set *BYTES - either already
20514 allocated on the objfile obstack, or newly allocated on OBSTACK,
20515 or, set *BATON, if we translated the constant to a location
20516 expression. */
20517
20518 static void
20519 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20520 const char *name, struct obstack *obstack,
20521 struct dwarf2_cu *cu,
20522 LONGEST *value, const gdb_byte **bytes,
20523 struct dwarf2_locexpr_baton **baton)
20524 {
20525 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20526 struct comp_unit_head *cu_header = &cu->header;
20527 struct dwarf_block *blk;
20528 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20529 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20530
20531 *value = 0;
20532 *bytes = NULL;
20533 *baton = NULL;
20534
20535 switch (attr->form)
20536 {
20537 case DW_FORM_addr:
20538 case DW_FORM_addrx:
20539 case DW_FORM_GNU_addr_index:
20540 {
20541 gdb_byte *data;
20542
20543 if (TYPE_LENGTH (type) != cu_header->addr_size)
20544 dwarf2_const_value_length_mismatch_complaint (name,
20545 cu_header->addr_size,
20546 TYPE_LENGTH (type));
20547 /* Symbols of this form are reasonably rare, so we just
20548 piggyback on the existing location code rather than writing
20549 a new implementation of symbol_computed_ops. */
20550 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20551 (*baton)->per_cu = cu->per_cu;
20552 gdb_assert ((*baton)->per_cu);
20553
20554 (*baton)->size = 2 + cu_header->addr_size;
20555 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20556 (*baton)->data = data;
20557
20558 data[0] = DW_OP_addr;
20559 store_unsigned_integer (&data[1], cu_header->addr_size,
20560 byte_order, DW_ADDR (attr));
20561 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20562 }
20563 break;
20564 case DW_FORM_string:
20565 case DW_FORM_strp:
20566 case DW_FORM_strx:
20567 case DW_FORM_GNU_str_index:
20568 case DW_FORM_GNU_strp_alt:
20569 /* DW_STRING is already allocated on the objfile obstack, point
20570 directly to it. */
20571 *bytes = (const gdb_byte *) DW_STRING (attr);
20572 break;
20573 case DW_FORM_block1:
20574 case DW_FORM_block2:
20575 case DW_FORM_block4:
20576 case DW_FORM_block:
20577 case DW_FORM_exprloc:
20578 case DW_FORM_data16:
20579 blk = DW_BLOCK (attr);
20580 if (TYPE_LENGTH (type) != blk->size)
20581 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20582 TYPE_LENGTH (type));
20583 *bytes = blk->data;
20584 break;
20585
20586 /* The DW_AT_const_value attributes are supposed to carry the
20587 symbol's value "represented as it would be on the target
20588 architecture." By the time we get here, it's already been
20589 converted to host endianness, so we just need to sign- or
20590 zero-extend it as appropriate. */
20591 case DW_FORM_data1:
20592 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20593 break;
20594 case DW_FORM_data2:
20595 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20596 break;
20597 case DW_FORM_data4:
20598 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20599 break;
20600 case DW_FORM_data8:
20601 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20602 break;
20603
20604 case DW_FORM_sdata:
20605 case DW_FORM_implicit_const:
20606 *value = DW_SND (attr);
20607 break;
20608
20609 case DW_FORM_udata:
20610 *value = DW_UNSND (attr);
20611 break;
20612
20613 default:
20614 complaint (_("unsupported const value attribute form: '%s'"),
20615 dwarf_form_name (attr->form));
20616 *value = 0;
20617 break;
20618 }
20619 }
20620
20621
20622 /* Copy constant value from an attribute to a symbol. */
20623
20624 static void
20625 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20626 struct dwarf2_cu *cu)
20627 {
20628 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20629 LONGEST value;
20630 const gdb_byte *bytes;
20631 struct dwarf2_locexpr_baton *baton;
20632
20633 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20634 sym->print_name (),
20635 &objfile->objfile_obstack, cu,
20636 &value, &bytes, &baton);
20637
20638 if (baton != NULL)
20639 {
20640 SYMBOL_LOCATION_BATON (sym) = baton;
20641 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20642 }
20643 else if (bytes != NULL)
20644 {
20645 SYMBOL_VALUE_BYTES (sym) = bytes;
20646 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20647 }
20648 else
20649 {
20650 SYMBOL_VALUE (sym) = value;
20651 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20652 }
20653 }
20654
20655 /* Return the type of the die in question using its DW_AT_type attribute. */
20656
20657 static struct type *
20658 die_type (struct die_info *die, struct dwarf2_cu *cu)
20659 {
20660 struct attribute *type_attr;
20661
20662 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20663 if (!type_attr)
20664 {
20665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20666 /* A missing DW_AT_type represents a void type. */
20667 return objfile_type (objfile)->builtin_void;
20668 }
20669
20670 return lookup_die_type (die, type_attr, cu);
20671 }
20672
20673 /* True iff CU's producer generates GNAT Ada auxiliary information
20674 that allows to find parallel types through that information instead
20675 of having to do expensive parallel lookups by type name. */
20676
20677 static int
20678 need_gnat_info (struct dwarf2_cu *cu)
20679 {
20680 /* Assume that the Ada compiler was GNAT, which always produces
20681 the auxiliary information. */
20682 return (cu->language == language_ada);
20683 }
20684
20685 /* Return the auxiliary type of the die in question using its
20686 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20687 attribute is not present. */
20688
20689 static struct type *
20690 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20691 {
20692 struct attribute *type_attr;
20693
20694 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20695 if (!type_attr)
20696 return NULL;
20697
20698 return lookup_die_type (die, type_attr, cu);
20699 }
20700
20701 /* If DIE has a descriptive_type attribute, then set the TYPE's
20702 descriptive type accordingly. */
20703
20704 static void
20705 set_descriptive_type (struct type *type, struct die_info *die,
20706 struct dwarf2_cu *cu)
20707 {
20708 struct type *descriptive_type = die_descriptive_type (die, cu);
20709
20710 if (descriptive_type)
20711 {
20712 ALLOCATE_GNAT_AUX_TYPE (type);
20713 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20714 }
20715 }
20716
20717 /* Return the containing type of the die in question using its
20718 DW_AT_containing_type attribute. */
20719
20720 static struct type *
20721 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20722 {
20723 struct attribute *type_attr;
20724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20725
20726 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20727 if (!type_attr)
20728 error (_("Dwarf Error: Problem turning containing type into gdb type "
20729 "[in module %s]"), objfile_name (objfile));
20730
20731 return lookup_die_type (die, type_attr, cu);
20732 }
20733
20734 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20735
20736 static struct type *
20737 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20738 {
20739 struct dwarf2_per_objfile *dwarf2_per_objfile
20740 = cu->per_cu->dwarf2_per_objfile;
20741 struct objfile *objfile = dwarf2_per_objfile->objfile;
20742 char *saved;
20743
20744 std::string message
20745 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20746 objfile_name (objfile),
20747 sect_offset_str (cu->header.sect_off),
20748 sect_offset_str (die->sect_off));
20749 saved = obstack_strdup (&objfile->objfile_obstack, message);
20750
20751 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20752 }
20753
20754 /* Look up the type of DIE in CU using its type attribute ATTR.
20755 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20756 DW_AT_containing_type.
20757 If there is no type substitute an error marker. */
20758
20759 static struct type *
20760 lookup_die_type (struct die_info *die, const struct attribute *attr,
20761 struct dwarf2_cu *cu)
20762 {
20763 struct dwarf2_per_objfile *dwarf2_per_objfile
20764 = cu->per_cu->dwarf2_per_objfile;
20765 struct objfile *objfile = dwarf2_per_objfile->objfile;
20766 struct type *this_type;
20767
20768 gdb_assert (attr->name == DW_AT_type
20769 || attr->name == DW_AT_GNAT_descriptive_type
20770 || attr->name == DW_AT_containing_type);
20771
20772 /* First see if we have it cached. */
20773
20774 if (attr->form == DW_FORM_GNU_ref_alt)
20775 {
20776 struct dwarf2_per_cu_data *per_cu;
20777 sect_offset sect_off = attr->get_ref_die_offset ();
20778
20779 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20780 dwarf2_per_objfile);
20781 this_type = get_die_type_at_offset (sect_off, per_cu);
20782 }
20783 else if (attr->form_is_ref ())
20784 {
20785 sect_offset sect_off = attr->get_ref_die_offset ();
20786
20787 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20788 }
20789 else if (attr->form == DW_FORM_ref_sig8)
20790 {
20791 ULONGEST signature = DW_SIGNATURE (attr);
20792
20793 return get_signatured_type (die, signature, cu);
20794 }
20795 else
20796 {
20797 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20798 " at %s [in module %s]"),
20799 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20800 objfile_name (objfile));
20801 return build_error_marker_type (cu, die);
20802 }
20803
20804 /* If not cached we need to read it in. */
20805
20806 if (this_type == NULL)
20807 {
20808 struct die_info *type_die = NULL;
20809 struct dwarf2_cu *type_cu = cu;
20810
20811 if (attr->form_is_ref ())
20812 type_die = follow_die_ref (die, attr, &type_cu);
20813 if (type_die == NULL)
20814 return build_error_marker_type (cu, die);
20815 /* If we find the type now, it's probably because the type came
20816 from an inter-CU reference and the type's CU got expanded before
20817 ours. */
20818 this_type = read_type_die (type_die, type_cu);
20819 }
20820
20821 /* If we still don't have a type use an error marker. */
20822
20823 if (this_type == NULL)
20824 return build_error_marker_type (cu, die);
20825
20826 return this_type;
20827 }
20828
20829 /* Return the type in DIE, CU.
20830 Returns NULL for invalid types.
20831
20832 This first does a lookup in die_type_hash,
20833 and only reads the die in if necessary.
20834
20835 NOTE: This can be called when reading in partial or full symbols. */
20836
20837 static struct type *
20838 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20839 {
20840 struct type *this_type;
20841
20842 this_type = get_die_type (die, cu);
20843 if (this_type)
20844 return this_type;
20845
20846 return read_type_die_1 (die, cu);
20847 }
20848
20849 /* Read the type in DIE, CU.
20850 Returns NULL for invalid types. */
20851
20852 static struct type *
20853 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20854 {
20855 struct type *this_type = NULL;
20856
20857 switch (die->tag)
20858 {
20859 case DW_TAG_class_type:
20860 case DW_TAG_interface_type:
20861 case DW_TAG_structure_type:
20862 case DW_TAG_union_type:
20863 this_type = read_structure_type (die, cu);
20864 break;
20865 case DW_TAG_enumeration_type:
20866 this_type = read_enumeration_type (die, cu);
20867 break;
20868 case DW_TAG_subprogram:
20869 case DW_TAG_subroutine_type:
20870 case DW_TAG_inlined_subroutine:
20871 this_type = read_subroutine_type (die, cu);
20872 break;
20873 case DW_TAG_array_type:
20874 this_type = read_array_type (die, cu);
20875 break;
20876 case DW_TAG_set_type:
20877 this_type = read_set_type (die, cu);
20878 break;
20879 case DW_TAG_pointer_type:
20880 this_type = read_tag_pointer_type (die, cu);
20881 break;
20882 case DW_TAG_ptr_to_member_type:
20883 this_type = read_tag_ptr_to_member_type (die, cu);
20884 break;
20885 case DW_TAG_reference_type:
20886 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20887 break;
20888 case DW_TAG_rvalue_reference_type:
20889 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20890 break;
20891 case DW_TAG_const_type:
20892 this_type = read_tag_const_type (die, cu);
20893 break;
20894 case DW_TAG_volatile_type:
20895 this_type = read_tag_volatile_type (die, cu);
20896 break;
20897 case DW_TAG_restrict_type:
20898 this_type = read_tag_restrict_type (die, cu);
20899 break;
20900 case DW_TAG_string_type:
20901 this_type = read_tag_string_type (die, cu);
20902 break;
20903 case DW_TAG_typedef:
20904 this_type = read_typedef (die, cu);
20905 break;
20906 case DW_TAG_subrange_type:
20907 this_type = read_subrange_type (die, cu);
20908 break;
20909 case DW_TAG_base_type:
20910 this_type = read_base_type (die, cu);
20911 break;
20912 case DW_TAG_unspecified_type:
20913 this_type = read_unspecified_type (die, cu);
20914 break;
20915 case DW_TAG_namespace:
20916 this_type = read_namespace_type (die, cu);
20917 break;
20918 case DW_TAG_module:
20919 this_type = read_module_type (die, cu);
20920 break;
20921 case DW_TAG_atomic_type:
20922 this_type = read_tag_atomic_type (die, cu);
20923 break;
20924 default:
20925 complaint (_("unexpected tag in read_type_die: '%s'"),
20926 dwarf_tag_name (die->tag));
20927 break;
20928 }
20929
20930 return this_type;
20931 }
20932
20933 /* See if we can figure out if the class lives in a namespace. We do
20934 this by looking for a member function; its demangled name will
20935 contain namespace info, if there is any.
20936 Return the computed name or NULL.
20937 Space for the result is allocated on the objfile's obstack.
20938 This is the full-die version of guess_partial_die_structure_name.
20939 In this case we know DIE has no useful parent. */
20940
20941 static const char *
20942 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20943 {
20944 struct die_info *spec_die;
20945 struct dwarf2_cu *spec_cu;
20946 struct die_info *child;
20947 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20948
20949 spec_cu = cu;
20950 spec_die = die_specification (die, &spec_cu);
20951 if (spec_die != NULL)
20952 {
20953 die = spec_die;
20954 cu = spec_cu;
20955 }
20956
20957 for (child = die->child;
20958 child != NULL;
20959 child = child->sibling)
20960 {
20961 if (child->tag == DW_TAG_subprogram)
20962 {
20963 const char *linkage_name = dw2_linkage_name (child, cu);
20964
20965 if (linkage_name != NULL)
20966 {
20967 gdb::unique_xmalloc_ptr<char> actual_name
20968 (language_class_name_from_physname (cu->language_defn,
20969 linkage_name));
20970 const char *name = NULL;
20971
20972 if (actual_name != NULL)
20973 {
20974 const char *die_name = dwarf2_name (die, cu);
20975
20976 if (die_name != NULL
20977 && strcmp (die_name, actual_name.get ()) != 0)
20978 {
20979 /* Strip off the class name from the full name.
20980 We want the prefix. */
20981 int die_name_len = strlen (die_name);
20982 int actual_name_len = strlen (actual_name.get ());
20983 const char *ptr = actual_name.get ();
20984
20985 /* Test for '::' as a sanity check. */
20986 if (actual_name_len > die_name_len + 2
20987 && ptr[actual_name_len - die_name_len - 1] == ':')
20988 name = obstack_strndup (
20989 &objfile->per_bfd->storage_obstack,
20990 ptr, actual_name_len - die_name_len - 2);
20991 }
20992 }
20993 return name;
20994 }
20995 }
20996 }
20997
20998 return NULL;
20999 }
21000
21001 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21002 prefix part in such case. See
21003 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21004
21005 static const char *
21006 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21007 {
21008 struct attribute *attr;
21009 const char *base;
21010
21011 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21012 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21013 return NULL;
21014
21015 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21016 return NULL;
21017
21018 attr = dw2_linkage_name_attr (die, cu);
21019 if (attr == NULL || DW_STRING (attr) == NULL)
21020 return NULL;
21021
21022 /* dwarf2_name had to be already called. */
21023 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21024
21025 /* Strip the base name, keep any leading namespaces/classes. */
21026 base = strrchr (DW_STRING (attr), ':');
21027 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21028 return "";
21029
21030 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21031 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21032 DW_STRING (attr),
21033 &base[-1] - DW_STRING (attr));
21034 }
21035
21036 /* Return the name of the namespace/class that DIE is defined within,
21037 or "" if we can't tell. The caller should not xfree the result.
21038
21039 For example, if we're within the method foo() in the following
21040 code:
21041
21042 namespace N {
21043 class C {
21044 void foo () {
21045 }
21046 };
21047 }
21048
21049 then determine_prefix on foo's die will return "N::C". */
21050
21051 static const char *
21052 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21053 {
21054 struct dwarf2_per_objfile *dwarf2_per_objfile
21055 = cu->per_cu->dwarf2_per_objfile;
21056 struct die_info *parent, *spec_die;
21057 struct dwarf2_cu *spec_cu;
21058 struct type *parent_type;
21059 const char *retval;
21060
21061 if (cu->language != language_cplus
21062 && cu->language != language_fortran && cu->language != language_d
21063 && cu->language != language_rust)
21064 return "";
21065
21066 retval = anonymous_struct_prefix (die, cu);
21067 if (retval)
21068 return retval;
21069
21070 /* We have to be careful in the presence of DW_AT_specification.
21071 For example, with GCC 3.4, given the code
21072
21073 namespace N {
21074 void foo() {
21075 // Definition of N::foo.
21076 }
21077 }
21078
21079 then we'll have a tree of DIEs like this:
21080
21081 1: DW_TAG_compile_unit
21082 2: DW_TAG_namespace // N
21083 3: DW_TAG_subprogram // declaration of N::foo
21084 4: DW_TAG_subprogram // definition of N::foo
21085 DW_AT_specification // refers to die #3
21086
21087 Thus, when processing die #4, we have to pretend that we're in
21088 the context of its DW_AT_specification, namely the contex of die
21089 #3. */
21090 spec_cu = cu;
21091 spec_die = die_specification (die, &spec_cu);
21092 if (spec_die == NULL)
21093 parent = die->parent;
21094 else
21095 {
21096 parent = spec_die->parent;
21097 cu = spec_cu;
21098 }
21099
21100 if (parent == NULL)
21101 return "";
21102 else if (parent->building_fullname)
21103 {
21104 const char *name;
21105 const char *parent_name;
21106
21107 /* It has been seen on RealView 2.2 built binaries,
21108 DW_TAG_template_type_param types actually _defined_ as
21109 children of the parent class:
21110
21111 enum E {};
21112 template class <class Enum> Class{};
21113 Class<enum E> class_e;
21114
21115 1: DW_TAG_class_type (Class)
21116 2: DW_TAG_enumeration_type (E)
21117 3: DW_TAG_enumerator (enum1:0)
21118 3: DW_TAG_enumerator (enum2:1)
21119 ...
21120 2: DW_TAG_template_type_param
21121 DW_AT_type DW_FORM_ref_udata (E)
21122
21123 Besides being broken debug info, it can put GDB into an
21124 infinite loop. Consider:
21125
21126 When we're building the full name for Class<E>, we'll start
21127 at Class, and go look over its template type parameters,
21128 finding E. We'll then try to build the full name of E, and
21129 reach here. We're now trying to build the full name of E,
21130 and look over the parent DIE for containing scope. In the
21131 broken case, if we followed the parent DIE of E, we'd again
21132 find Class, and once again go look at its template type
21133 arguments, etc., etc. Simply don't consider such parent die
21134 as source-level parent of this die (it can't be, the language
21135 doesn't allow it), and break the loop here. */
21136 name = dwarf2_name (die, cu);
21137 parent_name = dwarf2_name (parent, cu);
21138 complaint (_("template param type '%s' defined within parent '%s'"),
21139 name ? name : "<unknown>",
21140 parent_name ? parent_name : "<unknown>");
21141 return "";
21142 }
21143 else
21144 switch (parent->tag)
21145 {
21146 case DW_TAG_namespace:
21147 parent_type = read_type_die (parent, cu);
21148 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21149 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21150 Work around this problem here. */
21151 if (cu->language == language_cplus
21152 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21153 return "";
21154 /* We give a name to even anonymous namespaces. */
21155 return TYPE_NAME (parent_type);
21156 case DW_TAG_class_type:
21157 case DW_TAG_interface_type:
21158 case DW_TAG_structure_type:
21159 case DW_TAG_union_type:
21160 case DW_TAG_module:
21161 parent_type = read_type_die (parent, cu);
21162 if (TYPE_NAME (parent_type) != NULL)
21163 return TYPE_NAME (parent_type);
21164 else
21165 /* An anonymous structure is only allowed non-static data
21166 members; no typedefs, no member functions, et cetera.
21167 So it does not need a prefix. */
21168 return "";
21169 case DW_TAG_compile_unit:
21170 case DW_TAG_partial_unit:
21171 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21172 if (cu->language == language_cplus
21173 && !dwarf2_per_objfile->types.empty ()
21174 && die->child != NULL
21175 && (die->tag == DW_TAG_class_type
21176 || die->tag == DW_TAG_structure_type
21177 || die->tag == DW_TAG_union_type))
21178 {
21179 const char *name = guess_full_die_structure_name (die, cu);
21180 if (name != NULL)
21181 return name;
21182 }
21183 return "";
21184 case DW_TAG_subprogram:
21185 /* Nested subroutines in Fortran get a prefix with the name
21186 of the parent's subroutine. */
21187 if (cu->language == language_fortran)
21188 {
21189 if ((die->tag == DW_TAG_subprogram)
21190 && (dwarf2_name (parent, cu) != NULL))
21191 return dwarf2_name (parent, cu);
21192 }
21193 return determine_prefix (parent, cu);
21194 case DW_TAG_enumeration_type:
21195 parent_type = read_type_die (parent, cu);
21196 if (TYPE_DECLARED_CLASS (parent_type))
21197 {
21198 if (TYPE_NAME (parent_type) != NULL)
21199 return TYPE_NAME (parent_type);
21200 return "";
21201 }
21202 /* Fall through. */
21203 default:
21204 return determine_prefix (parent, cu);
21205 }
21206 }
21207
21208 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21209 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21210 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21211 an obconcat, otherwise allocate storage for the result. The CU argument is
21212 used to determine the language and hence, the appropriate separator. */
21213
21214 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21215
21216 static char *
21217 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21218 int physname, struct dwarf2_cu *cu)
21219 {
21220 const char *lead = "";
21221 const char *sep;
21222
21223 if (suffix == NULL || suffix[0] == '\0'
21224 || prefix == NULL || prefix[0] == '\0')
21225 sep = "";
21226 else if (cu->language == language_d)
21227 {
21228 /* For D, the 'main' function could be defined in any module, but it
21229 should never be prefixed. */
21230 if (strcmp (suffix, "D main") == 0)
21231 {
21232 prefix = "";
21233 sep = "";
21234 }
21235 else
21236 sep = ".";
21237 }
21238 else if (cu->language == language_fortran && physname)
21239 {
21240 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21241 DW_AT_MIPS_linkage_name is preferred and used instead. */
21242
21243 lead = "__";
21244 sep = "_MOD_";
21245 }
21246 else
21247 sep = "::";
21248
21249 if (prefix == NULL)
21250 prefix = "";
21251 if (suffix == NULL)
21252 suffix = "";
21253
21254 if (obs == NULL)
21255 {
21256 char *retval
21257 = ((char *)
21258 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21259
21260 strcpy (retval, lead);
21261 strcat (retval, prefix);
21262 strcat (retval, sep);
21263 strcat (retval, suffix);
21264 return retval;
21265 }
21266 else
21267 {
21268 /* We have an obstack. */
21269 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21270 }
21271 }
21272
21273 /* Get name of a die, return NULL if not found. */
21274
21275 static const char *
21276 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21277 struct objfile *objfile)
21278 {
21279 if (name && cu->language == language_cplus)
21280 {
21281 std::string canon_name = cp_canonicalize_string (name);
21282
21283 if (!canon_name.empty ())
21284 {
21285 if (canon_name != name)
21286 name = objfile->intern (canon_name);
21287 }
21288 }
21289
21290 return name;
21291 }
21292
21293 /* Get name of a die, return NULL if not found.
21294 Anonymous namespaces are converted to their magic string. */
21295
21296 static const char *
21297 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21298 {
21299 struct attribute *attr;
21300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21301
21302 attr = dwarf2_attr (die, DW_AT_name, cu);
21303 if ((!attr || !DW_STRING (attr))
21304 && die->tag != DW_TAG_namespace
21305 && die->tag != DW_TAG_class_type
21306 && die->tag != DW_TAG_interface_type
21307 && die->tag != DW_TAG_structure_type
21308 && die->tag != DW_TAG_union_type)
21309 return NULL;
21310
21311 switch (die->tag)
21312 {
21313 case DW_TAG_compile_unit:
21314 case DW_TAG_partial_unit:
21315 /* Compilation units have a DW_AT_name that is a filename, not
21316 a source language identifier. */
21317 case DW_TAG_enumeration_type:
21318 case DW_TAG_enumerator:
21319 /* These tags always have simple identifiers already; no need
21320 to canonicalize them. */
21321 return DW_STRING (attr);
21322
21323 case DW_TAG_namespace:
21324 if (attr != NULL && DW_STRING (attr) != NULL)
21325 return DW_STRING (attr);
21326 return CP_ANONYMOUS_NAMESPACE_STR;
21327
21328 case DW_TAG_class_type:
21329 case DW_TAG_interface_type:
21330 case DW_TAG_structure_type:
21331 case DW_TAG_union_type:
21332 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21333 structures or unions. These were of the form "._%d" in GCC 4.1,
21334 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21335 and GCC 4.4. We work around this problem by ignoring these. */
21336 if (attr && DW_STRING (attr)
21337 && (startswith (DW_STRING (attr), "._")
21338 || startswith (DW_STRING (attr), "<anonymous")))
21339 return NULL;
21340
21341 /* GCC might emit a nameless typedef that has a linkage name. See
21342 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21343 if (!attr || DW_STRING (attr) == NULL)
21344 {
21345 attr = dw2_linkage_name_attr (die, cu);
21346 if (attr == NULL || DW_STRING (attr) == NULL)
21347 return NULL;
21348
21349 /* Avoid demangling DW_STRING (attr) the second time on a second
21350 call for the same DIE. */
21351 if (!DW_STRING_IS_CANONICAL (attr))
21352 {
21353 gdb::unique_xmalloc_ptr<char> demangled
21354 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21355 if (demangled == nullptr)
21356 return nullptr;
21357
21358 DW_STRING (attr) = objfile->intern (demangled.get ());
21359 DW_STRING_IS_CANONICAL (attr) = 1;
21360 }
21361
21362 /* Strip any leading namespaces/classes, keep only the base name.
21363 DW_AT_name for named DIEs does not contain the prefixes. */
21364 const char *base = strrchr (DW_STRING (attr), ':');
21365 if (base && base > DW_STRING (attr) && base[-1] == ':')
21366 return &base[1];
21367 else
21368 return DW_STRING (attr);
21369 }
21370 break;
21371
21372 default:
21373 break;
21374 }
21375
21376 if (!DW_STRING_IS_CANONICAL (attr))
21377 {
21378 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21379 objfile);
21380 DW_STRING_IS_CANONICAL (attr) = 1;
21381 }
21382 return DW_STRING (attr);
21383 }
21384
21385 /* Return the die that this die in an extension of, or NULL if there
21386 is none. *EXT_CU is the CU containing DIE on input, and the CU
21387 containing the return value on output. */
21388
21389 static struct die_info *
21390 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21391 {
21392 struct attribute *attr;
21393
21394 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21395 if (attr == NULL)
21396 return NULL;
21397
21398 return follow_die_ref (die, attr, ext_cu);
21399 }
21400
21401 static void
21402 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21403 {
21404 unsigned int i;
21405
21406 print_spaces (indent, f);
21407 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21408 dwarf_tag_name (die->tag), die->abbrev,
21409 sect_offset_str (die->sect_off));
21410
21411 if (die->parent != NULL)
21412 {
21413 print_spaces (indent, f);
21414 fprintf_unfiltered (f, " parent at offset: %s\n",
21415 sect_offset_str (die->parent->sect_off));
21416 }
21417
21418 print_spaces (indent, f);
21419 fprintf_unfiltered (f, " has children: %s\n",
21420 dwarf_bool_name (die->child != NULL));
21421
21422 print_spaces (indent, f);
21423 fprintf_unfiltered (f, " attributes:\n");
21424
21425 for (i = 0; i < die->num_attrs; ++i)
21426 {
21427 print_spaces (indent, f);
21428 fprintf_unfiltered (f, " %s (%s) ",
21429 dwarf_attr_name (die->attrs[i].name),
21430 dwarf_form_name (die->attrs[i].form));
21431
21432 switch (die->attrs[i].form)
21433 {
21434 case DW_FORM_addr:
21435 case DW_FORM_addrx:
21436 case DW_FORM_GNU_addr_index:
21437 fprintf_unfiltered (f, "address: ");
21438 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21439 break;
21440 case DW_FORM_block2:
21441 case DW_FORM_block4:
21442 case DW_FORM_block:
21443 case DW_FORM_block1:
21444 fprintf_unfiltered (f, "block: size %s",
21445 pulongest (DW_BLOCK (&die->attrs[i])->size));
21446 break;
21447 case DW_FORM_exprloc:
21448 fprintf_unfiltered (f, "expression: size %s",
21449 pulongest (DW_BLOCK (&die->attrs[i])->size));
21450 break;
21451 case DW_FORM_data16:
21452 fprintf_unfiltered (f, "constant of 16 bytes");
21453 break;
21454 case DW_FORM_ref_addr:
21455 fprintf_unfiltered (f, "ref address: ");
21456 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21457 break;
21458 case DW_FORM_GNU_ref_alt:
21459 fprintf_unfiltered (f, "alt ref address: ");
21460 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21461 break;
21462 case DW_FORM_ref1:
21463 case DW_FORM_ref2:
21464 case DW_FORM_ref4:
21465 case DW_FORM_ref8:
21466 case DW_FORM_ref_udata:
21467 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21468 (long) (DW_UNSND (&die->attrs[i])));
21469 break;
21470 case DW_FORM_data1:
21471 case DW_FORM_data2:
21472 case DW_FORM_data4:
21473 case DW_FORM_data8:
21474 case DW_FORM_udata:
21475 case DW_FORM_sdata:
21476 fprintf_unfiltered (f, "constant: %s",
21477 pulongest (DW_UNSND (&die->attrs[i])));
21478 break;
21479 case DW_FORM_sec_offset:
21480 fprintf_unfiltered (f, "section offset: %s",
21481 pulongest (DW_UNSND (&die->attrs[i])));
21482 break;
21483 case DW_FORM_ref_sig8:
21484 fprintf_unfiltered (f, "signature: %s",
21485 hex_string (DW_SIGNATURE (&die->attrs[i])));
21486 break;
21487 case DW_FORM_string:
21488 case DW_FORM_strp:
21489 case DW_FORM_line_strp:
21490 case DW_FORM_strx:
21491 case DW_FORM_GNU_str_index:
21492 case DW_FORM_GNU_strp_alt:
21493 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21494 DW_STRING (&die->attrs[i])
21495 ? DW_STRING (&die->attrs[i]) : "",
21496 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21497 break;
21498 case DW_FORM_flag:
21499 if (DW_UNSND (&die->attrs[i]))
21500 fprintf_unfiltered (f, "flag: TRUE");
21501 else
21502 fprintf_unfiltered (f, "flag: FALSE");
21503 break;
21504 case DW_FORM_flag_present:
21505 fprintf_unfiltered (f, "flag: TRUE");
21506 break;
21507 case DW_FORM_indirect:
21508 /* The reader will have reduced the indirect form to
21509 the "base form" so this form should not occur. */
21510 fprintf_unfiltered (f,
21511 "unexpected attribute form: DW_FORM_indirect");
21512 break;
21513 case DW_FORM_implicit_const:
21514 fprintf_unfiltered (f, "constant: %s",
21515 plongest (DW_SND (&die->attrs[i])));
21516 break;
21517 default:
21518 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21519 die->attrs[i].form);
21520 break;
21521 }
21522 fprintf_unfiltered (f, "\n");
21523 }
21524 }
21525
21526 static void
21527 dump_die_for_error (struct die_info *die)
21528 {
21529 dump_die_shallow (gdb_stderr, 0, die);
21530 }
21531
21532 static void
21533 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21534 {
21535 int indent = level * 4;
21536
21537 gdb_assert (die != NULL);
21538
21539 if (level >= max_level)
21540 return;
21541
21542 dump_die_shallow (f, indent, die);
21543
21544 if (die->child != NULL)
21545 {
21546 print_spaces (indent, f);
21547 fprintf_unfiltered (f, " Children:");
21548 if (level + 1 < max_level)
21549 {
21550 fprintf_unfiltered (f, "\n");
21551 dump_die_1 (f, level + 1, max_level, die->child);
21552 }
21553 else
21554 {
21555 fprintf_unfiltered (f,
21556 " [not printed, max nesting level reached]\n");
21557 }
21558 }
21559
21560 if (die->sibling != NULL && level > 0)
21561 {
21562 dump_die_1 (f, level, max_level, die->sibling);
21563 }
21564 }
21565
21566 /* This is called from the pdie macro in gdbinit.in.
21567 It's not static so gcc will keep a copy callable from gdb. */
21568
21569 void
21570 dump_die (struct die_info *die, int max_level)
21571 {
21572 dump_die_1 (gdb_stdlog, 0, max_level, die);
21573 }
21574
21575 static void
21576 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21577 {
21578 void **slot;
21579
21580 slot = htab_find_slot_with_hash (cu->die_hash, die,
21581 to_underlying (die->sect_off),
21582 INSERT);
21583
21584 *slot = die;
21585 }
21586
21587 /* Follow reference or signature attribute ATTR of SRC_DIE.
21588 On entry *REF_CU is the CU of SRC_DIE.
21589 On exit *REF_CU is the CU of the result. */
21590
21591 static struct die_info *
21592 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21593 struct dwarf2_cu **ref_cu)
21594 {
21595 struct die_info *die;
21596
21597 if (attr->form_is_ref ())
21598 die = follow_die_ref (src_die, attr, ref_cu);
21599 else if (attr->form == DW_FORM_ref_sig8)
21600 die = follow_die_sig (src_die, attr, ref_cu);
21601 else
21602 {
21603 dump_die_for_error (src_die);
21604 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21605 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21606 }
21607
21608 return die;
21609 }
21610
21611 /* Follow reference OFFSET.
21612 On entry *REF_CU is the CU of the source die referencing OFFSET.
21613 On exit *REF_CU is the CU of the result.
21614 Returns NULL if OFFSET is invalid. */
21615
21616 static struct die_info *
21617 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21618 struct dwarf2_cu **ref_cu)
21619 {
21620 struct die_info temp_die;
21621 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21622 struct dwarf2_per_objfile *dwarf2_per_objfile
21623 = cu->per_cu->dwarf2_per_objfile;
21624
21625 gdb_assert (cu->per_cu != NULL);
21626
21627 target_cu = cu;
21628
21629 if (cu->per_cu->is_debug_types)
21630 {
21631 /* .debug_types CUs cannot reference anything outside their CU.
21632 If they need to, they have to reference a signatured type via
21633 DW_FORM_ref_sig8. */
21634 if (!cu->header.offset_in_cu_p (sect_off))
21635 return NULL;
21636 }
21637 else if (offset_in_dwz != cu->per_cu->is_dwz
21638 || !cu->header.offset_in_cu_p (sect_off))
21639 {
21640 struct dwarf2_per_cu_data *per_cu;
21641
21642 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21643 dwarf2_per_objfile);
21644
21645 /* If necessary, add it to the queue and load its DIEs. */
21646 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21647 load_full_comp_unit (per_cu, false, cu->language);
21648
21649 target_cu = per_cu->cu;
21650 }
21651 else if (cu->dies == NULL)
21652 {
21653 /* We're loading full DIEs during partial symbol reading. */
21654 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21655 load_full_comp_unit (cu->per_cu, false, language_minimal);
21656 }
21657
21658 *ref_cu = target_cu;
21659 temp_die.sect_off = sect_off;
21660
21661 if (target_cu != cu)
21662 target_cu->ancestor = cu;
21663
21664 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21665 &temp_die,
21666 to_underlying (sect_off));
21667 }
21668
21669 /* Follow reference attribute ATTR of SRC_DIE.
21670 On entry *REF_CU is the CU of SRC_DIE.
21671 On exit *REF_CU is the CU of the result. */
21672
21673 static struct die_info *
21674 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21675 struct dwarf2_cu **ref_cu)
21676 {
21677 sect_offset sect_off = attr->get_ref_die_offset ();
21678 struct dwarf2_cu *cu = *ref_cu;
21679 struct die_info *die;
21680
21681 die = follow_die_offset (sect_off,
21682 (attr->form == DW_FORM_GNU_ref_alt
21683 || cu->per_cu->is_dwz),
21684 ref_cu);
21685 if (!die)
21686 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21687 "at %s [in module %s]"),
21688 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21689 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21690
21691 return die;
21692 }
21693
21694 /* See read.h. */
21695
21696 struct dwarf2_locexpr_baton
21697 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21698 dwarf2_per_cu_data *per_cu,
21699 CORE_ADDR (*get_frame_pc) (void *baton),
21700 void *baton, bool resolve_abstract_p)
21701 {
21702 struct dwarf2_cu *cu;
21703 struct die_info *die;
21704 struct attribute *attr;
21705 struct dwarf2_locexpr_baton retval;
21706 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21707 struct objfile *objfile = dwarf2_per_objfile->objfile;
21708
21709 if (per_cu->cu == NULL)
21710 load_cu (per_cu, false);
21711 cu = per_cu->cu;
21712 if (cu == NULL)
21713 {
21714 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21715 Instead just throw an error, not much else we can do. */
21716 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21717 sect_offset_str (sect_off), objfile_name (objfile));
21718 }
21719
21720 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21721 if (!die)
21722 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21723 sect_offset_str (sect_off), objfile_name (objfile));
21724
21725 attr = dwarf2_attr (die, DW_AT_location, cu);
21726 if (!attr && resolve_abstract_p
21727 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21728 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21729 {
21730 CORE_ADDR pc = (*get_frame_pc) (baton);
21731 CORE_ADDR baseaddr = objfile->text_section_offset ();
21732 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21733
21734 for (const auto &cand_off
21735 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21736 {
21737 struct dwarf2_cu *cand_cu = cu;
21738 struct die_info *cand
21739 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21740 if (!cand
21741 || !cand->parent
21742 || cand->parent->tag != DW_TAG_subprogram)
21743 continue;
21744
21745 CORE_ADDR pc_low, pc_high;
21746 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21747 if (pc_low == ((CORE_ADDR) -1))
21748 continue;
21749 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21750 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21751 if (!(pc_low <= pc && pc < pc_high))
21752 continue;
21753
21754 die = cand;
21755 attr = dwarf2_attr (die, DW_AT_location, cu);
21756 break;
21757 }
21758 }
21759
21760 if (!attr)
21761 {
21762 /* DWARF: "If there is no such attribute, then there is no effect.".
21763 DATA is ignored if SIZE is 0. */
21764
21765 retval.data = NULL;
21766 retval.size = 0;
21767 }
21768 else if (attr->form_is_section_offset ())
21769 {
21770 struct dwarf2_loclist_baton loclist_baton;
21771 CORE_ADDR pc = (*get_frame_pc) (baton);
21772 size_t size;
21773
21774 fill_in_loclist_baton (cu, &loclist_baton, attr);
21775
21776 retval.data = dwarf2_find_location_expression (&loclist_baton,
21777 &size, pc);
21778 retval.size = size;
21779 }
21780 else
21781 {
21782 if (!attr->form_is_block ())
21783 error (_("Dwarf Error: DIE at %s referenced in module %s "
21784 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21785 sect_offset_str (sect_off), objfile_name (objfile));
21786
21787 retval.data = DW_BLOCK (attr)->data;
21788 retval.size = DW_BLOCK (attr)->size;
21789 }
21790 retval.per_cu = cu->per_cu;
21791
21792 age_cached_comp_units (dwarf2_per_objfile);
21793
21794 return retval;
21795 }
21796
21797 /* See read.h. */
21798
21799 struct dwarf2_locexpr_baton
21800 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21801 dwarf2_per_cu_data *per_cu,
21802 CORE_ADDR (*get_frame_pc) (void *baton),
21803 void *baton)
21804 {
21805 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21806
21807 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21808 }
21809
21810 /* Write a constant of a given type as target-ordered bytes into
21811 OBSTACK. */
21812
21813 static const gdb_byte *
21814 write_constant_as_bytes (struct obstack *obstack,
21815 enum bfd_endian byte_order,
21816 struct type *type,
21817 ULONGEST value,
21818 LONGEST *len)
21819 {
21820 gdb_byte *result;
21821
21822 *len = TYPE_LENGTH (type);
21823 result = (gdb_byte *) obstack_alloc (obstack, *len);
21824 store_unsigned_integer (result, *len, byte_order, value);
21825
21826 return result;
21827 }
21828
21829 /* See read.h. */
21830
21831 const gdb_byte *
21832 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21833 dwarf2_per_cu_data *per_cu,
21834 obstack *obstack,
21835 LONGEST *len)
21836 {
21837 struct dwarf2_cu *cu;
21838 struct die_info *die;
21839 struct attribute *attr;
21840 const gdb_byte *result = NULL;
21841 struct type *type;
21842 LONGEST value;
21843 enum bfd_endian byte_order;
21844 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21845
21846 if (per_cu->cu == NULL)
21847 load_cu (per_cu, false);
21848 cu = per_cu->cu;
21849 if (cu == NULL)
21850 {
21851 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21852 Instead just throw an error, not much else we can do. */
21853 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21854 sect_offset_str (sect_off), objfile_name (objfile));
21855 }
21856
21857 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21858 if (!die)
21859 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21860 sect_offset_str (sect_off), objfile_name (objfile));
21861
21862 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21863 if (attr == NULL)
21864 return NULL;
21865
21866 byte_order = (bfd_big_endian (objfile->obfd)
21867 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21868
21869 switch (attr->form)
21870 {
21871 case DW_FORM_addr:
21872 case DW_FORM_addrx:
21873 case DW_FORM_GNU_addr_index:
21874 {
21875 gdb_byte *tem;
21876
21877 *len = cu->header.addr_size;
21878 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21879 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21880 result = tem;
21881 }
21882 break;
21883 case DW_FORM_string:
21884 case DW_FORM_strp:
21885 case DW_FORM_strx:
21886 case DW_FORM_GNU_str_index:
21887 case DW_FORM_GNU_strp_alt:
21888 /* DW_STRING is already allocated on the objfile obstack, point
21889 directly to it. */
21890 result = (const gdb_byte *) DW_STRING (attr);
21891 *len = strlen (DW_STRING (attr));
21892 break;
21893 case DW_FORM_block1:
21894 case DW_FORM_block2:
21895 case DW_FORM_block4:
21896 case DW_FORM_block:
21897 case DW_FORM_exprloc:
21898 case DW_FORM_data16:
21899 result = DW_BLOCK (attr)->data;
21900 *len = DW_BLOCK (attr)->size;
21901 break;
21902
21903 /* The DW_AT_const_value attributes are supposed to carry the
21904 symbol's value "represented as it would be on the target
21905 architecture." By the time we get here, it's already been
21906 converted to host endianness, so we just need to sign- or
21907 zero-extend it as appropriate. */
21908 case DW_FORM_data1:
21909 type = die_type (die, cu);
21910 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21911 if (result == NULL)
21912 result = write_constant_as_bytes (obstack, byte_order,
21913 type, value, len);
21914 break;
21915 case DW_FORM_data2:
21916 type = die_type (die, cu);
21917 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21918 if (result == NULL)
21919 result = write_constant_as_bytes (obstack, byte_order,
21920 type, value, len);
21921 break;
21922 case DW_FORM_data4:
21923 type = die_type (die, cu);
21924 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21925 if (result == NULL)
21926 result = write_constant_as_bytes (obstack, byte_order,
21927 type, value, len);
21928 break;
21929 case DW_FORM_data8:
21930 type = die_type (die, cu);
21931 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21932 if (result == NULL)
21933 result = write_constant_as_bytes (obstack, byte_order,
21934 type, value, len);
21935 break;
21936
21937 case DW_FORM_sdata:
21938 case DW_FORM_implicit_const:
21939 type = die_type (die, cu);
21940 result = write_constant_as_bytes (obstack, byte_order,
21941 type, DW_SND (attr), len);
21942 break;
21943
21944 case DW_FORM_udata:
21945 type = die_type (die, cu);
21946 result = write_constant_as_bytes (obstack, byte_order,
21947 type, DW_UNSND (attr), len);
21948 break;
21949
21950 default:
21951 complaint (_("unsupported const value attribute form: '%s'"),
21952 dwarf_form_name (attr->form));
21953 break;
21954 }
21955
21956 return result;
21957 }
21958
21959 /* See read.h. */
21960
21961 struct type *
21962 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21963 dwarf2_per_cu_data *per_cu)
21964 {
21965 struct dwarf2_cu *cu;
21966 struct die_info *die;
21967
21968 if (per_cu->cu == NULL)
21969 load_cu (per_cu, false);
21970 cu = per_cu->cu;
21971 if (!cu)
21972 return NULL;
21973
21974 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21975 if (!die)
21976 return NULL;
21977
21978 return die_type (die, cu);
21979 }
21980
21981 /* See read.h. */
21982
21983 struct type *
21984 dwarf2_get_die_type (cu_offset die_offset,
21985 struct dwarf2_per_cu_data *per_cu)
21986 {
21987 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21988 return get_die_type_at_offset (die_offset_sect, per_cu);
21989 }
21990
21991 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21992 On entry *REF_CU is the CU of SRC_DIE.
21993 On exit *REF_CU is the CU of the result.
21994 Returns NULL if the referenced DIE isn't found. */
21995
21996 static struct die_info *
21997 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21998 struct dwarf2_cu **ref_cu)
21999 {
22000 struct die_info temp_die;
22001 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22002 struct die_info *die;
22003
22004 /* While it might be nice to assert sig_type->type == NULL here,
22005 we can get here for DW_AT_imported_declaration where we need
22006 the DIE not the type. */
22007
22008 /* If necessary, add it to the queue and load its DIEs. */
22009
22010 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22011 read_signatured_type (sig_type);
22012
22013 sig_cu = sig_type->per_cu.cu;
22014 gdb_assert (sig_cu != NULL);
22015 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22016 temp_die.sect_off = sig_type->type_offset_in_section;
22017 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22018 to_underlying (temp_die.sect_off));
22019 if (die)
22020 {
22021 struct dwarf2_per_objfile *dwarf2_per_objfile
22022 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22023
22024 /* For .gdb_index version 7 keep track of included TUs.
22025 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22026 if (dwarf2_per_objfile->index_table != NULL
22027 && dwarf2_per_objfile->index_table->version <= 7)
22028 {
22029 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22030 }
22031
22032 *ref_cu = sig_cu;
22033 if (sig_cu != cu)
22034 sig_cu->ancestor = cu;
22035
22036 return die;
22037 }
22038
22039 return NULL;
22040 }
22041
22042 /* Follow signatured type referenced by ATTR in SRC_DIE.
22043 On entry *REF_CU is the CU of SRC_DIE.
22044 On exit *REF_CU is the CU of the result.
22045 The result is the DIE of the type.
22046 If the referenced type cannot be found an error is thrown. */
22047
22048 static struct die_info *
22049 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22050 struct dwarf2_cu **ref_cu)
22051 {
22052 ULONGEST signature = DW_SIGNATURE (attr);
22053 struct signatured_type *sig_type;
22054 struct die_info *die;
22055
22056 gdb_assert (attr->form == DW_FORM_ref_sig8);
22057
22058 sig_type = lookup_signatured_type (*ref_cu, signature);
22059 /* sig_type will be NULL if the signatured type is missing from
22060 the debug info. */
22061 if (sig_type == NULL)
22062 {
22063 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22064 " from DIE at %s [in module %s]"),
22065 hex_string (signature), sect_offset_str (src_die->sect_off),
22066 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22067 }
22068
22069 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22070 if (die == NULL)
22071 {
22072 dump_die_for_error (src_die);
22073 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22074 " from DIE at %s [in module %s]"),
22075 hex_string (signature), sect_offset_str (src_die->sect_off),
22076 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22077 }
22078
22079 return die;
22080 }
22081
22082 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22083 reading in and processing the type unit if necessary. */
22084
22085 static struct type *
22086 get_signatured_type (struct die_info *die, ULONGEST signature,
22087 struct dwarf2_cu *cu)
22088 {
22089 struct dwarf2_per_objfile *dwarf2_per_objfile
22090 = cu->per_cu->dwarf2_per_objfile;
22091 struct signatured_type *sig_type;
22092 struct dwarf2_cu *type_cu;
22093 struct die_info *type_die;
22094 struct type *type;
22095
22096 sig_type = lookup_signatured_type (cu, signature);
22097 /* sig_type will be NULL if the signatured type is missing from
22098 the debug info. */
22099 if (sig_type == NULL)
22100 {
22101 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22102 " from DIE at %s [in module %s]"),
22103 hex_string (signature), sect_offset_str (die->sect_off),
22104 objfile_name (dwarf2_per_objfile->objfile));
22105 return build_error_marker_type (cu, die);
22106 }
22107
22108 /* If we already know the type we're done. */
22109 if (sig_type->type != NULL)
22110 return sig_type->type;
22111
22112 type_cu = cu;
22113 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22114 if (type_die != NULL)
22115 {
22116 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22117 is created. This is important, for example, because for c++ classes
22118 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22119 type = read_type_die (type_die, type_cu);
22120 if (type == NULL)
22121 {
22122 complaint (_("Dwarf Error: Cannot build signatured type %s"
22123 " referenced from DIE at %s [in module %s]"),
22124 hex_string (signature), sect_offset_str (die->sect_off),
22125 objfile_name (dwarf2_per_objfile->objfile));
22126 type = build_error_marker_type (cu, die);
22127 }
22128 }
22129 else
22130 {
22131 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22132 " from DIE at %s [in module %s]"),
22133 hex_string (signature), sect_offset_str (die->sect_off),
22134 objfile_name (dwarf2_per_objfile->objfile));
22135 type = build_error_marker_type (cu, die);
22136 }
22137 sig_type->type = type;
22138
22139 return type;
22140 }
22141
22142 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22143 reading in and processing the type unit if necessary. */
22144
22145 static struct type *
22146 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22147 struct dwarf2_cu *cu) /* ARI: editCase function */
22148 {
22149 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22150 if (attr->form_is_ref ())
22151 {
22152 struct dwarf2_cu *type_cu = cu;
22153 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22154
22155 return read_type_die (type_die, type_cu);
22156 }
22157 else if (attr->form == DW_FORM_ref_sig8)
22158 {
22159 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22160 }
22161 else
22162 {
22163 struct dwarf2_per_objfile *dwarf2_per_objfile
22164 = cu->per_cu->dwarf2_per_objfile;
22165
22166 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22167 " at %s [in module %s]"),
22168 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22169 objfile_name (dwarf2_per_objfile->objfile));
22170 return build_error_marker_type (cu, die);
22171 }
22172 }
22173
22174 /* Load the DIEs associated with type unit PER_CU into memory. */
22175
22176 static void
22177 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22178 {
22179 struct signatured_type *sig_type;
22180
22181 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22182 gdb_assert (! per_cu->type_unit_group_p ());
22183
22184 /* We have the per_cu, but we need the signatured_type.
22185 Fortunately this is an easy translation. */
22186 gdb_assert (per_cu->is_debug_types);
22187 sig_type = (struct signatured_type *) per_cu;
22188
22189 gdb_assert (per_cu->cu == NULL);
22190
22191 read_signatured_type (sig_type);
22192
22193 gdb_assert (per_cu->cu != NULL);
22194 }
22195
22196 /* Read in a signatured type and build its CU and DIEs.
22197 If the type is a stub for the real type in a DWO file,
22198 read in the real type from the DWO file as well. */
22199
22200 static void
22201 read_signatured_type (struct signatured_type *sig_type)
22202 {
22203 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22204
22205 gdb_assert (per_cu->is_debug_types);
22206 gdb_assert (per_cu->cu == NULL);
22207
22208 cutu_reader reader (per_cu, NULL, 0, false);
22209
22210 if (!reader.dummy_p)
22211 {
22212 struct dwarf2_cu *cu = reader.cu;
22213 const gdb_byte *info_ptr = reader.info_ptr;
22214
22215 gdb_assert (cu->die_hash == NULL);
22216 cu->die_hash =
22217 htab_create_alloc_ex (cu->header.length / 12,
22218 die_hash,
22219 die_eq,
22220 NULL,
22221 &cu->comp_unit_obstack,
22222 hashtab_obstack_allocate,
22223 dummy_obstack_deallocate);
22224
22225 if (reader.comp_unit_die->has_children)
22226 reader.comp_unit_die->child
22227 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22228 reader.comp_unit_die);
22229 cu->dies = reader.comp_unit_die;
22230 /* comp_unit_die is not stored in die_hash, no need. */
22231
22232 /* We try not to read any attributes in this function, because
22233 not all CUs needed for references have been loaded yet, and
22234 symbol table processing isn't initialized. But we have to
22235 set the CU language, or we won't be able to build types
22236 correctly. Similarly, if we do not read the producer, we can
22237 not apply producer-specific interpretation. */
22238 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22239
22240 reader.keep ();
22241 }
22242
22243 sig_type->per_cu.tu_read = 1;
22244 }
22245
22246 /* Decode simple location descriptions.
22247 Given a pointer to a dwarf block that defines a location, compute
22248 the location and return the value.
22249
22250 NOTE drow/2003-11-18: This function is called in two situations
22251 now: for the address of static or global variables (partial symbols
22252 only) and for offsets into structures which are expected to be
22253 (more or less) constant. The partial symbol case should go away,
22254 and only the constant case should remain. That will let this
22255 function complain more accurately. A few special modes are allowed
22256 without complaint for global variables (for instance, global
22257 register values and thread-local values).
22258
22259 A location description containing no operations indicates that the
22260 object is optimized out. The return value is 0 for that case.
22261 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22262 callers will only want a very basic result and this can become a
22263 complaint.
22264
22265 Note that stack[0] is unused except as a default error return. */
22266
22267 static CORE_ADDR
22268 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22269 {
22270 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22271 size_t i;
22272 size_t size = blk->size;
22273 const gdb_byte *data = blk->data;
22274 CORE_ADDR stack[64];
22275 int stacki;
22276 unsigned int bytes_read, unsnd;
22277 gdb_byte op;
22278
22279 i = 0;
22280 stacki = 0;
22281 stack[stacki] = 0;
22282 stack[++stacki] = 0;
22283
22284 while (i < size)
22285 {
22286 op = data[i++];
22287 switch (op)
22288 {
22289 case DW_OP_lit0:
22290 case DW_OP_lit1:
22291 case DW_OP_lit2:
22292 case DW_OP_lit3:
22293 case DW_OP_lit4:
22294 case DW_OP_lit5:
22295 case DW_OP_lit6:
22296 case DW_OP_lit7:
22297 case DW_OP_lit8:
22298 case DW_OP_lit9:
22299 case DW_OP_lit10:
22300 case DW_OP_lit11:
22301 case DW_OP_lit12:
22302 case DW_OP_lit13:
22303 case DW_OP_lit14:
22304 case DW_OP_lit15:
22305 case DW_OP_lit16:
22306 case DW_OP_lit17:
22307 case DW_OP_lit18:
22308 case DW_OP_lit19:
22309 case DW_OP_lit20:
22310 case DW_OP_lit21:
22311 case DW_OP_lit22:
22312 case DW_OP_lit23:
22313 case DW_OP_lit24:
22314 case DW_OP_lit25:
22315 case DW_OP_lit26:
22316 case DW_OP_lit27:
22317 case DW_OP_lit28:
22318 case DW_OP_lit29:
22319 case DW_OP_lit30:
22320 case DW_OP_lit31:
22321 stack[++stacki] = op - DW_OP_lit0;
22322 break;
22323
22324 case DW_OP_reg0:
22325 case DW_OP_reg1:
22326 case DW_OP_reg2:
22327 case DW_OP_reg3:
22328 case DW_OP_reg4:
22329 case DW_OP_reg5:
22330 case DW_OP_reg6:
22331 case DW_OP_reg7:
22332 case DW_OP_reg8:
22333 case DW_OP_reg9:
22334 case DW_OP_reg10:
22335 case DW_OP_reg11:
22336 case DW_OP_reg12:
22337 case DW_OP_reg13:
22338 case DW_OP_reg14:
22339 case DW_OP_reg15:
22340 case DW_OP_reg16:
22341 case DW_OP_reg17:
22342 case DW_OP_reg18:
22343 case DW_OP_reg19:
22344 case DW_OP_reg20:
22345 case DW_OP_reg21:
22346 case DW_OP_reg22:
22347 case DW_OP_reg23:
22348 case DW_OP_reg24:
22349 case DW_OP_reg25:
22350 case DW_OP_reg26:
22351 case DW_OP_reg27:
22352 case DW_OP_reg28:
22353 case DW_OP_reg29:
22354 case DW_OP_reg30:
22355 case DW_OP_reg31:
22356 stack[++stacki] = op - DW_OP_reg0;
22357 if (i < size)
22358 dwarf2_complex_location_expr_complaint ();
22359 break;
22360
22361 case DW_OP_regx:
22362 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22363 i += bytes_read;
22364 stack[++stacki] = unsnd;
22365 if (i < size)
22366 dwarf2_complex_location_expr_complaint ();
22367 break;
22368
22369 case DW_OP_addr:
22370 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22371 &bytes_read);
22372 i += bytes_read;
22373 break;
22374
22375 case DW_OP_const1u:
22376 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22377 i += 1;
22378 break;
22379
22380 case DW_OP_const1s:
22381 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22382 i += 1;
22383 break;
22384
22385 case DW_OP_const2u:
22386 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22387 i += 2;
22388 break;
22389
22390 case DW_OP_const2s:
22391 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22392 i += 2;
22393 break;
22394
22395 case DW_OP_const4u:
22396 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22397 i += 4;
22398 break;
22399
22400 case DW_OP_const4s:
22401 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22402 i += 4;
22403 break;
22404
22405 case DW_OP_const8u:
22406 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22407 i += 8;
22408 break;
22409
22410 case DW_OP_constu:
22411 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22412 &bytes_read);
22413 i += bytes_read;
22414 break;
22415
22416 case DW_OP_consts:
22417 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22418 i += bytes_read;
22419 break;
22420
22421 case DW_OP_dup:
22422 stack[stacki + 1] = stack[stacki];
22423 stacki++;
22424 break;
22425
22426 case DW_OP_plus:
22427 stack[stacki - 1] += stack[stacki];
22428 stacki--;
22429 break;
22430
22431 case DW_OP_plus_uconst:
22432 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22433 &bytes_read);
22434 i += bytes_read;
22435 break;
22436
22437 case DW_OP_minus:
22438 stack[stacki - 1] -= stack[stacki];
22439 stacki--;
22440 break;
22441
22442 case DW_OP_deref:
22443 /* If we're not the last op, then we definitely can't encode
22444 this using GDB's address_class enum. This is valid for partial
22445 global symbols, although the variable's address will be bogus
22446 in the psymtab. */
22447 if (i < size)
22448 dwarf2_complex_location_expr_complaint ();
22449 break;
22450
22451 case DW_OP_GNU_push_tls_address:
22452 case DW_OP_form_tls_address:
22453 /* The top of the stack has the offset from the beginning
22454 of the thread control block at which the variable is located. */
22455 /* Nothing should follow this operator, so the top of stack would
22456 be returned. */
22457 /* This is valid for partial global symbols, but the variable's
22458 address will be bogus in the psymtab. Make it always at least
22459 non-zero to not look as a variable garbage collected by linker
22460 which have DW_OP_addr 0. */
22461 if (i < size)
22462 dwarf2_complex_location_expr_complaint ();
22463 stack[stacki]++;
22464 break;
22465
22466 case DW_OP_GNU_uninit:
22467 break;
22468
22469 case DW_OP_addrx:
22470 case DW_OP_GNU_addr_index:
22471 case DW_OP_GNU_const_index:
22472 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22473 &bytes_read);
22474 i += bytes_read;
22475 break;
22476
22477 default:
22478 {
22479 const char *name = get_DW_OP_name (op);
22480
22481 if (name)
22482 complaint (_("unsupported stack op: '%s'"),
22483 name);
22484 else
22485 complaint (_("unsupported stack op: '%02x'"),
22486 op);
22487 }
22488
22489 return (stack[stacki]);
22490 }
22491
22492 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22493 outside of the allocated space. Also enforce minimum>0. */
22494 if (stacki >= ARRAY_SIZE (stack) - 1)
22495 {
22496 complaint (_("location description stack overflow"));
22497 return 0;
22498 }
22499
22500 if (stacki <= 0)
22501 {
22502 complaint (_("location description stack underflow"));
22503 return 0;
22504 }
22505 }
22506 return (stack[stacki]);
22507 }
22508
22509 /* memory allocation interface */
22510
22511 static struct dwarf_block *
22512 dwarf_alloc_block (struct dwarf2_cu *cu)
22513 {
22514 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22515 }
22516
22517 static struct die_info *
22518 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22519 {
22520 struct die_info *die;
22521 size_t size = sizeof (struct die_info);
22522
22523 if (num_attrs > 1)
22524 size += (num_attrs - 1) * sizeof (struct attribute);
22525
22526 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22527 memset (die, 0, sizeof (struct die_info));
22528 return (die);
22529 }
22530
22531 \f
22532
22533 /* Macro support. */
22534
22535 /* An overload of dwarf_decode_macros that finds the correct section
22536 and ensures it is read in before calling the other overload. */
22537
22538 static void
22539 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22540 int section_is_gnu)
22541 {
22542 struct dwarf2_per_objfile *dwarf2_per_objfile
22543 = cu->per_cu->dwarf2_per_objfile;
22544 struct objfile *objfile = dwarf2_per_objfile->objfile;
22545 const struct line_header *lh = cu->line_header;
22546 unsigned int offset_size = cu->header.offset_size;
22547 struct dwarf2_section_info *section;
22548 const char *section_name;
22549
22550 if (cu->dwo_unit != nullptr)
22551 {
22552 if (section_is_gnu)
22553 {
22554 section = &cu->dwo_unit->dwo_file->sections.macro;
22555 section_name = ".debug_macro.dwo";
22556 }
22557 else
22558 {
22559 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22560 section_name = ".debug_macinfo.dwo";
22561 }
22562 }
22563 else
22564 {
22565 if (section_is_gnu)
22566 {
22567 section = &dwarf2_per_objfile->macro;
22568 section_name = ".debug_macro";
22569 }
22570 else
22571 {
22572 section = &dwarf2_per_objfile->macinfo;
22573 section_name = ".debug_macinfo";
22574 }
22575 }
22576
22577 section->read (objfile);
22578 if (section->buffer == nullptr)
22579 {
22580 complaint (_("missing %s section"), section_name);
22581 return;
22582 }
22583
22584 buildsym_compunit *builder = cu->get_builder ();
22585
22586 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22587 offset_size, offset, section_is_gnu);
22588 }
22589
22590 /* Return the .debug_loc section to use for CU.
22591 For DWO files use .debug_loc.dwo. */
22592
22593 static struct dwarf2_section_info *
22594 cu_debug_loc_section (struct dwarf2_cu *cu)
22595 {
22596 struct dwarf2_per_objfile *dwarf2_per_objfile
22597 = cu->per_cu->dwarf2_per_objfile;
22598
22599 if (cu->dwo_unit)
22600 {
22601 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22602
22603 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22604 }
22605 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22606 : &dwarf2_per_objfile->loc);
22607 }
22608
22609 /* A helper function that fills in a dwarf2_loclist_baton. */
22610
22611 static void
22612 fill_in_loclist_baton (struct dwarf2_cu *cu,
22613 struct dwarf2_loclist_baton *baton,
22614 const struct attribute *attr)
22615 {
22616 struct dwarf2_per_objfile *dwarf2_per_objfile
22617 = cu->per_cu->dwarf2_per_objfile;
22618 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22619
22620 section->read (dwarf2_per_objfile->objfile);
22621
22622 baton->per_cu = cu->per_cu;
22623 gdb_assert (baton->per_cu);
22624 /* We don't know how long the location list is, but make sure we
22625 don't run off the edge of the section. */
22626 baton->size = section->size - DW_UNSND (attr);
22627 baton->data = section->buffer + DW_UNSND (attr);
22628 if (cu->base_address.has_value ())
22629 baton->base_address = *cu->base_address;
22630 else
22631 baton->base_address = 0;
22632 baton->from_dwo = cu->dwo_unit != NULL;
22633 }
22634
22635 static void
22636 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22637 struct dwarf2_cu *cu, int is_block)
22638 {
22639 struct dwarf2_per_objfile *dwarf2_per_objfile
22640 = cu->per_cu->dwarf2_per_objfile;
22641 struct objfile *objfile = dwarf2_per_objfile->objfile;
22642 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22643
22644 if (attr->form_is_section_offset ()
22645 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22646 the section. If so, fall through to the complaint in the
22647 other branch. */
22648 && DW_UNSND (attr) < section->get_size (objfile))
22649 {
22650 struct dwarf2_loclist_baton *baton;
22651
22652 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22653
22654 fill_in_loclist_baton (cu, baton, attr);
22655
22656 if (!cu->base_address.has_value ())
22657 complaint (_("Location list used without "
22658 "specifying the CU base address."));
22659
22660 SYMBOL_ACLASS_INDEX (sym) = (is_block
22661 ? dwarf2_loclist_block_index
22662 : dwarf2_loclist_index);
22663 SYMBOL_LOCATION_BATON (sym) = baton;
22664 }
22665 else
22666 {
22667 struct dwarf2_locexpr_baton *baton;
22668
22669 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22670 baton->per_cu = cu->per_cu;
22671 gdb_assert (baton->per_cu);
22672
22673 if (attr->form_is_block ())
22674 {
22675 /* Note that we're just copying the block's data pointer
22676 here, not the actual data. We're still pointing into the
22677 info_buffer for SYM's objfile; right now we never release
22678 that buffer, but when we do clean up properly this may
22679 need to change. */
22680 baton->size = DW_BLOCK (attr)->size;
22681 baton->data = DW_BLOCK (attr)->data;
22682 }
22683 else
22684 {
22685 dwarf2_invalid_attrib_class_complaint ("location description",
22686 sym->natural_name ());
22687 baton->size = 0;
22688 }
22689
22690 SYMBOL_ACLASS_INDEX (sym) = (is_block
22691 ? dwarf2_locexpr_block_index
22692 : dwarf2_locexpr_index);
22693 SYMBOL_LOCATION_BATON (sym) = baton;
22694 }
22695 }
22696
22697 /* See read.h. */
22698
22699 struct objfile *
22700 dwarf2_per_cu_data::objfile () const
22701 {
22702 struct objfile *objfile = dwarf2_per_objfile->objfile;
22703
22704 /* Return the master objfile, so that we can report and look up the
22705 correct file containing this variable. */
22706 if (objfile->separate_debug_objfile_backlink)
22707 objfile = objfile->separate_debug_objfile_backlink;
22708
22709 return objfile;
22710 }
22711
22712 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22713 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22714 CU_HEADERP first. */
22715
22716 static const struct comp_unit_head *
22717 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22718 const struct dwarf2_per_cu_data *per_cu)
22719 {
22720 const gdb_byte *info_ptr;
22721
22722 if (per_cu->cu)
22723 return &per_cu->cu->header;
22724
22725 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22726
22727 memset (cu_headerp, 0, sizeof (*cu_headerp));
22728 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22729 rcuh_kind::COMPILE);
22730
22731 return cu_headerp;
22732 }
22733
22734 /* See read.h. */
22735
22736 int
22737 dwarf2_per_cu_data::addr_size () const
22738 {
22739 struct comp_unit_head cu_header_local;
22740 const struct comp_unit_head *cu_headerp;
22741
22742 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22743
22744 return cu_headerp->addr_size;
22745 }
22746
22747 /* See read.h. */
22748
22749 int
22750 dwarf2_per_cu_data::offset_size () const
22751 {
22752 struct comp_unit_head cu_header_local;
22753 const struct comp_unit_head *cu_headerp;
22754
22755 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22756
22757 return cu_headerp->offset_size;
22758 }
22759
22760 /* See read.h. */
22761
22762 int
22763 dwarf2_per_cu_data::ref_addr_size () const
22764 {
22765 struct comp_unit_head cu_header_local;
22766 const struct comp_unit_head *cu_headerp;
22767
22768 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22769
22770 if (cu_headerp->version == 2)
22771 return cu_headerp->addr_size;
22772 else
22773 return cu_headerp->offset_size;
22774 }
22775
22776 /* See read.h. */
22777
22778 CORE_ADDR
22779 dwarf2_per_cu_data::text_offset () const
22780 {
22781 struct objfile *objfile = dwarf2_per_objfile->objfile;
22782
22783 return objfile->text_section_offset ();
22784 }
22785
22786 /* See read.h. */
22787
22788 struct type *
22789 dwarf2_per_cu_data::addr_type () const
22790 {
22791 struct objfile *objfile = dwarf2_per_objfile->objfile;
22792 struct type *void_type = objfile_type (objfile)->builtin_void;
22793 struct type *addr_type = lookup_pointer_type (void_type);
22794 int addr_size = this->addr_size ();
22795
22796 if (TYPE_LENGTH (addr_type) == addr_size)
22797 return addr_type;
22798
22799 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22800 return addr_type;
22801 }
22802
22803 /* A helper function for dwarf2_find_containing_comp_unit that returns
22804 the index of the result, and that searches a vector. It will
22805 return a result even if the offset in question does not actually
22806 occur in any CU. This is separate so that it can be unit
22807 tested. */
22808
22809 static int
22810 dwarf2_find_containing_comp_unit
22811 (sect_offset sect_off,
22812 unsigned int offset_in_dwz,
22813 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22814 {
22815 int low, high;
22816
22817 low = 0;
22818 high = all_comp_units.size () - 1;
22819 while (high > low)
22820 {
22821 struct dwarf2_per_cu_data *mid_cu;
22822 int mid = low + (high - low) / 2;
22823
22824 mid_cu = all_comp_units[mid];
22825 if (mid_cu->is_dwz > offset_in_dwz
22826 || (mid_cu->is_dwz == offset_in_dwz
22827 && mid_cu->sect_off + mid_cu->length > sect_off))
22828 high = mid;
22829 else
22830 low = mid + 1;
22831 }
22832 gdb_assert (low == high);
22833 return low;
22834 }
22835
22836 /* Locate the .debug_info compilation unit from CU's objfile which contains
22837 the DIE at OFFSET. Raises an error on failure. */
22838
22839 static struct dwarf2_per_cu_data *
22840 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22841 unsigned int offset_in_dwz,
22842 struct dwarf2_per_objfile *dwarf2_per_objfile)
22843 {
22844 int low
22845 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22846 dwarf2_per_objfile->all_comp_units);
22847 struct dwarf2_per_cu_data *this_cu
22848 = dwarf2_per_objfile->all_comp_units[low];
22849
22850 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22851 {
22852 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22853 error (_("Dwarf Error: could not find partial DIE containing "
22854 "offset %s [in module %s]"),
22855 sect_offset_str (sect_off),
22856 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22857
22858 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22859 <= sect_off);
22860 return dwarf2_per_objfile->all_comp_units[low-1];
22861 }
22862 else
22863 {
22864 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22865 && sect_off >= this_cu->sect_off + this_cu->length)
22866 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22867 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22868 return this_cu;
22869 }
22870 }
22871
22872 #if GDB_SELF_TEST
22873
22874 namespace selftests {
22875 namespace find_containing_comp_unit {
22876
22877 static void
22878 run_test ()
22879 {
22880 struct dwarf2_per_cu_data one {};
22881 struct dwarf2_per_cu_data two {};
22882 struct dwarf2_per_cu_data three {};
22883 struct dwarf2_per_cu_data four {};
22884
22885 one.length = 5;
22886 two.sect_off = sect_offset (one.length);
22887 two.length = 7;
22888
22889 three.length = 5;
22890 three.is_dwz = 1;
22891 four.sect_off = sect_offset (three.length);
22892 four.length = 7;
22893 four.is_dwz = 1;
22894
22895 std::vector<dwarf2_per_cu_data *> units;
22896 units.push_back (&one);
22897 units.push_back (&two);
22898 units.push_back (&three);
22899 units.push_back (&four);
22900
22901 int result;
22902
22903 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22904 SELF_CHECK (units[result] == &one);
22905 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22906 SELF_CHECK (units[result] == &one);
22907 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22908 SELF_CHECK (units[result] == &two);
22909
22910 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22911 SELF_CHECK (units[result] == &three);
22912 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22913 SELF_CHECK (units[result] == &three);
22914 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22915 SELF_CHECK (units[result] == &four);
22916 }
22917
22918 }
22919 }
22920
22921 #endif /* GDB_SELF_TEST */
22922
22923 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22924
22925 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22926 : per_cu (per_cu_),
22927 mark (false),
22928 has_loclist (false),
22929 checked_producer (false),
22930 producer_is_gxx_lt_4_6 (false),
22931 producer_is_gcc_lt_4_3 (false),
22932 producer_is_icc (false),
22933 producer_is_icc_lt_14 (false),
22934 producer_is_codewarrior (false),
22935 processing_has_namespace_info (false)
22936 {
22937 per_cu->cu = this;
22938 }
22939
22940 /* Destroy a dwarf2_cu. */
22941
22942 dwarf2_cu::~dwarf2_cu ()
22943 {
22944 per_cu->cu = NULL;
22945 }
22946
22947 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22948
22949 static void
22950 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22951 enum language pretend_language)
22952 {
22953 struct attribute *attr;
22954
22955 /* Set the language we're debugging. */
22956 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22957 if (attr != nullptr)
22958 set_cu_language (DW_UNSND (attr), cu);
22959 else
22960 {
22961 cu->language = pretend_language;
22962 cu->language_defn = language_def (cu->language);
22963 }
22964
22965 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22966 }
22967
22968 /* Increase the age counter on each cached compilation unit, and free
22969 any that are too old. */
22970
22971 static void
22972 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
22973 {
22974 struct dwarf2_per_cu_data *per_cu, **last_chain;
22975
22976 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22977 per_cu = dwarf2_per_objfile->read_in_chain;
22978 while (per_cu != NULL)
22979 {
22980 per_cu->cu->last_used ++;
22981 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22982 dwarf2_mark (per_cu->cu);
22983 per_cu = per_cu->cu->read_in_chain;
22984 }
22985
22986 per_cu = dwarf2_per_objfile->read_in_chain;
22987 last_chain = &dwarf2_per_objfile->read_in_chain;
22988 while (per_cu != NULL)
22989 {
22990 struct dwarf2_per_cu_data *next_cu;
22991
22992 next_cu = per_cu->cu->read_in_chain;
22993
22994 if (!per_cu->cu->mark)
22995 {
22996 delete per_cu->cu;
22997 *last_chain = next_cu;
22998 }
22999 else
23000 last_chain = &per_cu->cu->read_in_chain;
23001
23002 per_cu = next_cu;
23003 }
23004 }
23005
23006 /* Remove a single compilation unit from the cache. */
23007
23008 static void
23009 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23010 {
23011 struct dwarf2_per_cu_data *per_cu, **last_chain;
23012 struct dwarf2_per_objfile *dwarf2_per_objfile
23013 = target_per_cu->dwarf2_per_objfile;
23014
23015 per_cu = dwarf2_per_objfile->read_in_chain;
23016 last_chain = &dwarf2_per_objfile->read_in_chain;
23017 while (per_cu != NULL)
23018 {
23019 struct dwarf2_per_cu_data *next_cu;
23020
23021 next_cu = per_cu->cu->read_in_chain;
23022
23023 if (per_cu == target_per_cu)
23024 {
23025 delete per_cu->cu;
23026 per_cu->cu = NULL;
23027 *last_chain = next_cu;
23028 break;
23029 }
23030 else
23031 last_chain = &per_cu->cu->read_in_chain;
23032
23033 per_cu = next_cu;
23034 }
23035 }
23036
23037 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23038 We store these in a hash table separate from the DIEs, and preserve them
23039 when the DIEs are flushed out of cache.
23040
23041 The CU "per_cu" pointer is needed because offset alone is not enough to
23042 uniquely identify the type. A file may have multiple .debug_types sections,
23043 or the type may come from a DWO file. Furthermore, while it's more logical
23044 to use per_cu->section+offset, with Fission the section with the data is in
23045 the DWO file but we don't know that section at the point we need it.
23046 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23047 because we can enter the lookup routine, get_die_type_at_offset, from
23048 outside this file, and thus won't necessarily have PER_CU->cu.
23049 Fortunately, PER_CU is stable for the life of the objfile. */
23050
23051 struct dwarf2_per_cu_offset_and_type
23052 {
23053 const struct dwarf2_per_cu_data *per_cu;
23054 sect_offset sect_off;
23055 struct type *type;
23056 };
23057
23058 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23059
23060 static hashval_t
23061 per_cu_offset_and_type_hash (const void *item)
23062 {
23063 const struct dwarf2_per_cu_offset_and_type *ofs
23064 = (const struct dwarf2_per_cu_offset_and_type *) item;
23065
23066 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23067 }
23068
23069 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23070
23071 static int
23072 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23073 {
23074 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23075 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23076 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23077 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23078
23079 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23080 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23081 }
23082
23083 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23084 table if necessary. For convenience, return TYPE.
23085
23086 The DIEs reading must have careful ordering to:
23087 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23088 reading current DIE.
23089 * Not trying to dereference contents of still incompletely read in types
23090 while reading in other DIEs.
23091 * Enable referencing still incompletely read in types just by a pointer to
23092 the type without accessing its fields.
23093
23094 Therefore caller should follow these rules:
23095 * Try to fetch any prerequisite types we may need to build this DIE type
23096 before building the type and calling set_die_type.
23097 * After building type call set_die_type for current DIE as soon as
23098 possible before fetching more types to complete the current type.
23099 * Make the type as complete as possible before fetching more types. */
23100
23101 static struct type *
23102 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23103 {
23104 struct dwarf2_per_objfile *dwarf2_per_objfile
23105 = cu->per_cu->dwarf2_per_objfile;
23106 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23107 struct objfile *objfile = dwarf2_per_objfile->objfile;
23108 struct attribute *attr;
23109 struct dynamic_prop prop;
23110
23111 /* For Ada types, make sure that the gnat-specific data is always
23112 initialized (if not already set). There are a few types where
23113 we should not be doing so, because the type-specific area is
23114 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23115 where the type-specific area is used to store the floatformat).
23116 But this is not a problem, because the gnat-specific information
23117 is actually not needed for these types. */
23118 if (need_gnat_info (cu)
23119 && TYPE_CODE (type) != TYPE_CODE_FUNC
23120 && TYPE_CODE (type) != TYPE_CODE_FLT
23121 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23122 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23123 && TYPE_CODE (type) != TYPE_CODE_METHOD
23124 && !HAVE_GNAT_AUX_INFO (type))
23125 INIT_GNAT_SPECIFIC (type);
23126
23127 /* Read DW_AT_allocated and set in type. */
23128 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23129 if (attr != NULL && attr->form_is_block ())
23130 {
23131 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23132 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23133 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23134 }
23135 else if (attr != NULL)
23136 {
23137 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23138 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23139 sect_offset_str (die->sect_off));
23140 }
23141
23142 /* Read DW_AT_associated and set in type. */
23143 attr = dwarf2_attr (die, DW_AT_associated, cu);
23144 if (attr != NULL && attr->form_is_block ())
23145 {
23146 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23147 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23148 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23149 }
23150 else if (attr != NULL)
23151 {
23152 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23153 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23154 sect_offset_str (die->sect_off));
23155 }
23156
23157 /* Read DW_AT_data_location and set in type. */
23158 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23159 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23160 cu->per_cu->addr_type ()))
23161 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23162
23163 if (dwarf2_per_objfile->die_type_hash == NULL)
23164 dwarf2_per_objfile->die_type_hash
23165 = htab_up (htab_create_alloc (127,
23166 per_cu_offset_and_type_hash,
23167 per_cu_offset_and_type_eq,
23168 NULL, xcalloc, xfree));
23169
23170 ofs.per_cu = cu->per_cu;
23171 ofs.sect_off = die->sect_off;
23172 ofs.type = type;
23173 slot = (struct dwarf2_per_cu_offset_and_type **)
23174 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23175 if (*slot)
23176 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23177 sect_offset_str (die->sect_off));
23178 *slot = XOBNEW (&objfile->objfile_obstack,
23179 struct dwarf2_per_cu_offset_and_type);
23180 **slot = ofs;
23181 return type;
23182 }
23183
23184 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23185 or return NULL if the die does not have a saved type. */
23186
23187 static struct type *
23188 get_die_type_at_offset (sect_offset sect_off,
23189 struct dwarf2_per_cu_data *per_cu)
23190 {
23191 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23192 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23193
23194 if (dwarf2_per_objfile->die_type_hash == NULL)
23195 return NULL;
23196
23197 ofs.per_cu = per_cu;
23198 ofs.sect_off = sect_off;
23199 slot = ((struct dwarf2_per_cu_offset_and_type *)
23200 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23201 if (slot)
23202 return slot->type;
23203 else
23204 return NULL;
23205 }
23206
23207 /* Look up the type for DIE in CU in die_type_hash,
23208 or return NULL if DIE does not have a saved type. */
23209
23210 static struct type *
23211 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23212 {
23213 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23214 }
23215
23216 /* Add a dependence relationship from CU to REF_PER_CU. */
23217
23218 static void
23219 dwarf2_add_dependence (struct dwarf2_cu *cu,
23220 struct dwarf2_per_cu_data *ref_per_cu)
23221 {
23222 void **slot;
23223
23224 if (cu->dependencies == NULL)
23225 cu->dependencies
23226 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23227 NULL, &cu->comp_unit_obstack,
23228 hashtab_obstack_allocate,
23229 dummy_obstack_deallocate);
23230
23231 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23232 if (*slot == NULL)
23233 *slot = ref_per_cu;
23234 }
23235
23236 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23237 Set the mark field in every compilation unit in the
23238 cache that we must keep because we are keeping CU. */
23239
23240 static int
23241 dwarf2_mark_helper (void **slot, void *data)
23242 {
23243 struct dwarf2_per_cu_data *per_cu;
23244
23245 per_cu = (struct dwarf2_per_cu_data *) *slot;
23246
23247 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23248 reading of the chain. As such dependencies remain valid it is not much
23249 useful to track and undo them during QUIT cleanups. */
23250 if (per_cu->cu == NULL)
23251 return 1;
23252
23253 if (per_cu->cu->mark)
23254 return 1;
23255 per_cu->cu->mark = true;
23256
23257 if (per_cu->cu->dependencies != NULL)
23258 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23259
23260 return 1;
23261 }
23262
23263 /* Set the mark field in CU and in every other compilation unit in the
23264 cache that we must keep because we are keeping CU. */
23265
23266 static void
23267 dwarf2_mark (struct dwarf2_cu *cu)
23268 {
23269 if (cu->mark)
23270 return;
23271 cu->mark = true;
23272 if (cu->dependencies != NULL)
23273 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23274 }
23275
23276 static void
23277 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23278 {
23279 while (per_cu)
23280 {
23281 per_cu->cu->mark = false;
23282 per_cu = per_cu->cu->read_in_chain;
23283 }
23284 }
23285
23286 /* Trivial hash function for partial_die_info: the hash value of a DIE
23287 is its offset in .debug_info for this objfile. */
23288
23289 static hashval_t
23290 partial_die_hash (const void *item)
23291 {
23292 const struct partial_die_info *part_die
23293 = (const struct partial_die_info *) item;
23294
23295 return to_underlying (part_die->sect_off);
23296 }
23297
23298 /* Trivial comparison function for partial_die_info structures: two DIEs
23299 are equal if they have the same offset. */
23300
23301 static int
23302 partial_die_eq (const void *item_lhs, const void *item_rhs)
23303 {
23304 const struct partial_die_info *part_die_lhs
23305 = (const struct partial_die_info *) item_lhs;
23306 const struct partial_die_info *part_die_rhs
23307 = (const struct partial_die_info *) item_rhs;
23308
23309 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23310 }
23311
23312 struct cmd_list_element *set_dwarf_cmdlist;
23313 struct cmd_list_element *show_dwarf_cmdlist;
23314
23315 static void
23316 set_dwarf_cmd (const char *args, int from_tty)
23317 {
23318 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23319 gdb_stdout);
23320 }
23321
23322 static void
23323 show_dwarf_cmd (const char *args, int from_tty)
23324 {
23325 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23326 }
23327
23328 static void
23329 show_check_physname (struct ui_file *file, int from_tty,
23330 struct cmd_list_element *c, const char *value)
23331 {
23332 fprintf_filtered (file,
23333 _("Whether to check \"physname\" is %s.\n"),
23334 value);
23335 }
23336
23337 void _initialize_dwarf2_read ();
23338 void
23339 _initialize_dwarf2_read ()
23340 {
23341 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23342 Set DWARF specific variables.\n\
23343 Configure DWARF variables such as the cache size."),
23344 &set_dwarf_cmdlist, "maintenance set dwarf ",
23345 0/*allow-unknown*/, &maintenance_set_cmdlist);
23346
23347 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23348 Show DWARF specific variables.\n\
23349 Show DWARF variables such as the cache size."),
23350 &show_dwarf_cmdlist, "maintenance show dwarf ",
23351 0/*allow-unknown*/, &maintenance_show_cmdlist);
23352
23353 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23354 &dwarf_max_cache_age, _("\
23355 Set the upper bound on the age of cached DWARF compilation units."), _("\
23356 Show the upper bound on the age of cached DWARF compilation units."), _("\
23357 A higher limit means that cached compilation units will be stored\n\
23358 in memory longer, and more total memory will be used. Zero disables\n\
23359 caching, which can slow down startup."),
23360 NULL,
23361 show_dwarf_max_cache_age,
23362 &set_dwarf_cmdlist,
23363 &show_dwarf_cmdlist);
23364
23365 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23366 Set debugging of the DWARF reader."), _("\
23367 Show debugging of the DWARF reader."), _("\
23368 When enabled (non-zero), debugging messages are printed during DWARF\n\
23369 reading and symtab expansion. A value of 1 (one) provides basic\n\
23370 information. A value greater than 1 provides more verbose information."),
23371 NULL,
23372 NULL,
23373 &setdebuglist, &showdebuglist);
23374
23375 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23376 Set debugging of the DWARF DIE reader."), _("\
23377 Show debugging of the DWARF DIE reader."), _("\
23378 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23379 The value is the maximum depth to print."),
23380 NULL,
23381 NULL,
23382 &setdebuglist, &showdebuglist);
23383
23384 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23385 Set debugging of the dwarf line reader."), _("\
23386 Show debugging of the dwarf line reader."), _("\
23387 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23388 A value of 1 (one) provides basic information.\n\
23389 A value greater than 1 provides more verbose information."),
23390 NULL,
23391 NULL,
23392 &setdebuglist, &showdebuglist);
23393
23394 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23395 Set cross-checking of \"physname\" code against demangler."), _("\
23396 Show cross-checking of \"physname\" code against demangler."), _("\
23397 When enabled, GDB's internal \"physname\" code is checked against\n\
23398 the demangler."),
23399 NULL, show_check_physname,
23400 &setdebuglist, &showdebuglist);
23401
23402 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23403 no_class, &use_deprecated_index_sections, _("\
23404 Set whether to use deprecated gdb_index sections."), _("\
23405 Show whether to use deprecated gdb_index sections."), _("\
23406 When enabled, deprecated .gdb_index sections are used anyway.\n\
23407 Normally they are ignored either because of a missing feature or\n\
23408 performance issue.\n\
23409 Warning: This option must be enabled before gdb reads the file."),
23410 NULL,
23411 NULL,
23412 &setlist, &showlist);
23413
23414 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23415 &dwarf2_locexpr_funcs);
23416 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23417 &dwarf2_loclist_funcs);
23418
23419 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23420 &dwarf2_block_frame_base_locexpr_funcs);
23421 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23422 &dwarf2_block_frame_base_loclist_funcs);
23423
23424 #if GDB_SELF_TEST
23425 selftests::register_test ("dw2_expand_symtabs_matching",
23426 selftests::dw2_expand_symtabs_matching::run_test);
23427 selftests::register_test ("dwarf2_find_containing_comp_unit",
23428 selftests::find_containing_comp_unit::run_test);
23429 #endif
23430 }