]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
gdb: rename partial_symtab::read_dependencies to expand_dependencies
[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 /* Currently unimplemented; used for Ada. The function can be called if the
3596 current language is Ada for a non-Ada objfile using GNU index. As Ada
3597 does not look for non-Ada symbols this function should just return. */
3598 }
3599
3600 /* Starting from a search name, return the string that finds the upper
3601 bound of all strings that start with SEARCH_NAME in a sorted name
3602 list. Returns the empty string to indicate that the upper bound is
3603 the end of the list. */
3604
3605 static std::string
3606 make_sort_after_prefix_name (const char *search_name)
3607 {
3608 /* When looking to complete "func", we find the upper bound of all
3609 symbols that start with "func" by looking for where we'd insert
3610 the closest string that would follow "func" in lexicographical
3611 order. Usually, that's "func"-with-last-character-incremented,
3612 i.e. "fund". Mind non-ASCII characters, though. Usually those
3613 will be UTF-8 multi-byte sequences, but we can't be certain.
3614 Especially mind the 0xff character, which is a valid character in
3615 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3616 rule out compilers allowing it in identifiers. Note that
3617 conveniently, strcmp/strcasecmp are specified to compare
3618 characters interpreted as unsigned char. So what we do is treat
3619 the whole string as a base 256 number composed of a sequence of
3620 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3621 to 0, and carries 1 to the following more-significant position.
3622 If the very first character in SEARCH_NAME ends up incremented
3623 and carries/overflows, then the upper bound is the end of the
3624 list. The string after the empty string is also the empty
3625 string.
3626
3627 Some examples of this operation:
3628
3629 SEARCH_NAME => "+1" RESULT
3630
3631 "abc" => "abd"
3632 "ab\xff" => "ac"
3633 "\xff" "a" "\xff" => "\xff" "b"
3634 "\xff" => ""
3635 "\xff\xff" => ""
3636 "" => ""
3637
3638 Then, with these symbols for example:
3639
3640 func
3641 func1
3642 fund
3643
3644 completing "func" looks for symbols between "func" and
3645 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3646 which finds "func" and "func1", but not "fund".
3647
3648 And with:
3649
3650 funcÿ (Latin1 'ÿ' [0xff])
3651 funcÿ1
3652 fund
3653
3654 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3655 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3656
3657 And with:
3658
3659 ÿÿ (Latin1 'ÿ' [0xff])
3660 ÿÿ1
3661
3662 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3663 the end of the list.
3664 */
3665 std::string after = search_name;
3666 while (!after.empty () && (unsigned char) after.back () == 0xff)
3667 after.pop_back ();
3668 if (!after.empty ())
3669 after.back () = (unsigned char) after.back () + 1;
3670 return after;
3671 }
3672
3673 /* See declaration. */
3674
3675 std::pair<std::vector<name_component>::const_iterator,
3676 std::vector<name_component>::const_iterator>
3677 mapped_index_base::find_name_components_bounds
3678 (const lookup_name_info &lookup_name_without_params, language lang) const
3679 {
3680 auto *name_cmp
3681 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3682
3683 const char *lang_name
3684 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3685
3686 /* Comparison function object for lower_bound that matches against a
3687 given symbol name. */
3688 auto lookup_compare_lower = [&] (const name_component &elem,
3689 const char *name)
3690 {
3691 const char *elem_qualified = this->symbol_name_at (elem.idx);
3692 const char *elem_name = elem_qualified + elem.name_offset;
3693 return name_cmp (elem_name, name) < 0;
3694 };
3695
3696 /* Comparison function object for upper_bound that matches against a
3697 given symbol name. */
3698 auto lookup_compare_upper = [&] (const char *name,
3699 const name_component &elem)
3700 {
3701 const char *elem_qualified = this->symbol_name_at (elem.idx);
3702 const char *elem_name = elem_qualified + elem.name_offset;
3703 return name_cmp (name, elem_name) < 0;
3704 };
3705
3706 auto begin = this->name_components.begin ();
3707 auto end = this->name_components.end ();
3708
3709 /* Find the lower bound. */
3710 auto lower = [&] ()
3711 {
3712 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3713 return begin;
3714 else
3715 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3716 } ();
3717
3718 /* Find the upper bound. */
3719 auto upper = [&] ()
3720 {
3721 if (lookup_name_without_params.completion_mode ())
3722 {
3723 /* In completion mode, we want UPPER to point past all
3724 symbols names that have the same prefix. I.e., with
3725 these symbols, and completing "func":
3726
3727 function << lower bound
3728 function1
3729 other_function << upper bound
3730
3731 We find the upper bound by looking for the insertion
3732 point of "func"-with-last-character-incremented,
3733 i.e. "fund". */
3734 std::string after = make_sort_after_prefix_name (lang_name);
3735 if (after.empty ())
3736 return end;
3737 return std::lower_bound (lower, end, after.c_str (),
3738 lookup_compare_lower);
3739 }
3740 else
3741 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3742 } ();
3743
3744 return {lower, upper};
3745 }
3746
3747 /* See declaration. */
3748
3749 void
3750 mapped_index_base::build_name_components ()
3751 {
3752 if (!this->name_components.empty ())
3753 return;
3754
3755 this->name_components_casing = case_sensitivity;
3756 auto *name_cmp
3757 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3758
3759 /* The code below only knows how to break apart components of C++
3760 symbol names (and other languages that use '::' as
3761 namespace/module separator) and Ada symbol names. */
3762 auto count = this->symbol_name_count ();
3763 for (offset_type idx = 0; idx < count; idx++)
3764 {
3765 if (this->symbol_name_slot_invalid (idx))
3766 continue;
3767
3768 const char *name = this->symbol_name_at (idx);
3769
3770 /* Add each name component to the name component table. */
3771 unsigned int previous_len = 0;
3772
3773 if (strstr (name, "::") != nullptr)
3774 {
3775 for (unsigned int current_len = cp_find_first_component (name);
3776 name[current_len] != '\0';
3777 current_len += cp_find_first_component (name + current_len))
3778 {
3779 gdb_assert (name[current_len] == ':');
3780 this->name_components.push_back ({previous_len, idx});
3781 /* Skip the '::'. */
3782 current_len += 2;
3783 previous_len = current_len;
3784 }
3785 }
3786 else
3787 {
3788 /* Handle the Ada encoded (aka mangled) form here. */
3789 for (const char *iter = strstr (name, "__");
3790 iter != nullptr;
3791 iter = strstr (iter, "__"))
3792 {
3793 this->name_components.push_back ({previous_len, idx});
3794 iter += 2;
3795 previous_len = iter - name;
3796 }
3797 }
3798
3799 this->name_components.push_back ({previous_len, idx});
3800 }
3801
3802 /* Sort name_components elements by name. */
3803 auto name_comp_compare = [&] (const name_component &left,
3804 const name_component &right)
3805 {
3806 const char *left_qualified = this->symbol_name_at (left.idx);
3807 const char *right_qualified = this->symbol_name_at (right.idx);
3808
3809 const char *left_name = left_qualified + left.name_offset;
3810 const char *right_name = right_qualified + right.name_offset;
3811
3812 return name_cmp (left_name, right_name) < 0;
3813 };
3814
3815 std::sort (this->name_components.begin (),
3816 this->name_components.end (),
3817 name_comp_compare);
3818 }
3819
3820 /* Helper for dw2_expand_symtabs_matching that works with a
3821 mapped_index_base instead of the containing objfile. This is split
3822 to a separate function in order to be able to unit test the
3823 name_components matching using a mock mapped_index_base. For each
3824 symbol name that matches, calls MATCH_CALLBACK, passing it the
3825 symbol's index in the mapped_index_base symbol table. */
3826
3827 static void
3828 dw2_expand_symtabs_matching_symbol
3829 (mapped_index_base &index,
3830 const lookup_name_info &lookup_name_in,
3831 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3832 enum search_domain kind,
3833 gdb::function_view<bool (offset_type)> match_callback)
3834 {
3835 lookup_name_info lookup_name_without_params
3836 = lookup_name_in.make_ignore_params ();
3837
3838 /* Build the symbol name component sorted vector, if we haven't
3839 yet. */
3840 index.build_name_components ();
3841
3842 /* The same symbol may appear more than once in the range though.
3843 E.g., if we're looking for symbols that complete "w", and we have
3844 a symbol named "w1::w2", we'll find the two name components for
3845 that same symbol in the range. To be sure we only call the
3846 callback once per symbol, we first collect the symbol name
3847 indexes that matched in a temporary vector and ignore
3848 duplicates. */
3849 std::vector<offset_type> matches;
3850
3851 struct name_and_matcher
3852 {
3853 symbol_name_matcher_ftype *matcher;
3854 const std::string &name;
3855
3856 bool operator== (const name_and_matcher &other) const
3857 {
3858 return matcher == other.matcher && name == other.name;
3859 }
3860 };
3861
3862 /* A vector holding all the different symbol name matchers, for all
3863 languages. */
3864 std::vector<name_and_matcher> matchers;
3865
3866 for (int i = 0; i < nr_languages; i++)
3867 {
3868 enum language lang_e = (enum language) i;
3869
3870 const language_defn *lang = language_def (lang_e);
3871 symbol_name_matcher_ftype *name_matcher
3872 = get_symbol_name_matcher (lang, lookup_name_without_params);
3873
3874 name_and_matcher key {
3875 name_matcher,
3876 lookup_name_without_params.language_lookup_name (lang_e)
3877 };
3878
3879 /* Don't insert the same comparison routine more than once.
3880 Note that we do this linear walk. This is not a problem in
3881 practice because the number of supported languages is
3882 low. */
3883 if (std::find (matchers.begin (), matchers.end (), key)
3884 != matchers.end ())
3885 continue;
3886 matchers.push_back (std::move (key));
3887
3888 auto bounds
3889 = index.find_name_components_bounds (lookup_name_without_params,
3890 lang_e);
3891
3892 /* Now for each symbol name in range, check to see if we have a name
3893 match, and if so, call the MATCH_CALLBACK callback. */
3894
3895 for (; bounds.first != bounds.second; ++bounds.first)
3896 {
3897 const char *qualified = index.symbol_name_at (bounds.first->idx);
3898
3899 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3900 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3901 continue;
3902
3903 matches.push_back (bounds.first->idx);
3904 }
3905 }
3906
3907 std::sort (matches.begin (), matches.end ());
3908
3909 /* Finally call the callback, once per match. */
3910 ULONGEST prev = -1;
3911 for (offset_type idx : matches)
3912 {
3913 if (prev != idx)
3914 {
3915 if (!match_callback (idx))
3916 break;
3917 prev = idx;
3918 }
3919 }
3920
3921 /* Above we use a type wider than idx's for 'prev', since 0 and
3922 (offset_type)-1 are both possible values. */
3923 static_assert (sizeof (prev) > sizeof (offset_type), "");
3924 }
3925
3926 #if GDB_SELF_TEST
3927
3928 namespace selftests { namespace dw2_expand_symtabs_matching {
3929
3930 /* A mock .gdb_index/.debug_names-like name index table, enough to
3931 exercise dw2_expand_symtabs_matching_symbol, which works with the
3932 mapped_index_base interface. Builds an index from the symbol list
3933 passed as parameter to the constructor. */
3934 class mock_mapped_index : public mapped_index_base
3935 {
3936 public:
3937 mock_mapped_index (gdb::array_view<const char *> symbols)
3938 : m_symbol_table (symbols)
3939 {}
3940
3941 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3942
3943 /* Return the number of names in the symbol table. */
3944 size_t symbol_name_count () const override
3945 {
3946 return m_symbol_table.size ();
3947 }
3948
3949 /* Get the name of the symbol at IDX in the symbol table. */
3950 const char *symbol_name_at (offset_type idx) const override
3951 {
3952 return m_symbol_table[idx];
3953 }
3954
3955 private:
3956 gdb::array_view<const char *> m_symbol_table;
3957 };
3958
3959 /* Convenience function that converts a NULL pointer to a "<null>"
3960 string, to pass to print routines. */
3961
3962 static const char *
3963 string_or_null (const char *str)
3964 {
3965 return str != NULL ? str : "<null>";
3966 }
3967
3968 /* Check if a lookup_name_info built from
3969 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3970 index. EXPECTED_LIST is the list of expected matches, in expected
3971 matching order. If no match expected, then an empty list is
3972 specified. Returns true on success. On failure prints a warning
3973 indicating the file:line that failed, and returns false. */
3974
3975 static bool
3976 check_match (const char *file, int line,
3977 mock_mapped_index &mock_index,
3978 const char *name, symbol_name_match_type match_type,
3979 bool completion_mode,
3980 std::initializer_list<const char *> expected_list)
3981 {
3982 lookup_name_info lookup_name (name, match_type, completion_mode);
3983
3984 bool matched = true;
3985
3986 auto mismatch = [&] (const char *expected_str,
3987 const char *got)
3988 {
3989 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3990 "expected=\"%s\", got=\"%s\"\n"),
3991 file, line,
3992 (match_type == symbol_name_match_type::FULL
3993 ? "FULL" : "WILD"),
3994 name, string_or_null (expected_str), string_or_null (got));
3995 matched = false;
3996 };
3997
3998 auto expected_it = expected_list.begin ();
3999 auto expected_end = expected_list.end ();
4000
4001 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4002 NULL, ALL_DOMAIN,
4003 [&] (offset_type idx)
4004 {
4005 const char *matched_name = mock_index.symbol_name_at (idx);
4006 const char *expected_str
4007 = expected_it == expected_end ? NULL : *expected_it++;
4008
4009 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4010 mismatch (expected_str, matched_name);
4011 return true;
4012 });
4013
4014 const char *expected_str
4015 = expected_it == expected_end ? NULL : *expected_it++;
4016 if (expected_str != NULL)
4017 mismatch (expected_str, NULL);
4018
4019 return matched;
4020 }
4021
4022 /* The symbols added to the mock mapped_index for testing (in
4023 canonical form). */
4024 static const char *test_symbols[] = {
4025 "function",
4026 "std::bar",
4027 "std::zfunction",
4028 "std::zfunction2",
4029 "w1::w2",
4030 "ns::foo<char*>",
4031 "ns::foo<int>",
4032 "ns::foo<long>",
4033 "ns2::tmpl<int>::foo2",
4034 "(anonymous namespace)::A::B::C",
4035
4036 /* These are used to check that the increment-last-char in the
4037 matching algorithm for completion doesn't match "t1_fund" when
4038 completing "t1_func". */
4039 "t1_func",
4040 "t1_func1",
4041 "t1_fund",
4042 "t1_fund1",
4043
4044 /* A UTF-8 name with multi-byte sequences to make sure that
4045 cp-name-parser understands this as a single identifier ("função"
4046 is "function" in PT). */
4047 u8"u8função",
4048
4049 /* \377 (0xff) is Latin1 'ÿ'. */
4050 "yfunc\377",
4051
4052 /* \377 (0xff) is Latin1 'ÿ'. */
4053 "\377",
4054 "\377\377123",
4055
4056 /* A name with all sorts of complications. Starts with "z" to make
4057 it easier for the completion tests below. */
4058 #define Z_SYM_NAME \
4059 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4060 "::tuple<(anonymous namespace)::ui*, " \
4061 "std::default_delete<(anonymous namespace)::ui>, void>"
4062
4063 Z_SYM_NAME
4064 };
4065
4066 /* Returns true if the mapped_index_base::find_name_component_bounds
4067 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4068 in completion mode. */
4069
4070 static bool
4071 check_find_bounds_finds (mapped_index_base &index,
4072 const char *search_name,
4073 gdb::array_view<const char *> expected_syms)
4074 {
4075 lookup_name_info lookup_name (search_name,
4076 symbol_name_match_type::FULL, true);
4077
4078 auto bounds = index.find_name_components_bounds (lookup_name,
4079 language_cplus);
4080
4081 size_t distance = std::distance (bounds.first, bounds.second);
4082 if (distance != expected_syms.size ())
4083 return false;
4084
4085 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4086 {
4087 auto nc_elem = bounds.first + exp_elem;
4088 const char *qualified = index.symbol_name_at (nc_elem->idx);
4089 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4090 return false;
4091 }
4092
4093 return true;
4094 }
4095
4096 /* Test the lower-level mapped_index::find_name_component_bounds
4097 method. */
4098
4099 static void
4100 test_mapped_index_find_name_component_bounds ()
4101 {
4102 mock_mapped_index mock_index (test_symbols);
4103
4104 mock_index.build_name_components ();
4105
4106 /* Test the lower-level mapped_index::find_name_component_bounds
4107 method in completion mode. */
4108 {
4109 static const char *expected_syms[] = {
4110 "t1_func",
4111 "t1_func1",
4112 };
4113
4114 SELF_CHECK (check_find_bounds_finds (mock_index,
4115 "t1_func", expected_syms));
4116 }
4117
4118 /* Check that the increment-last-char in the name matching algorithm
4119 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4120 {
4121 static const char *expected_syms1[] = {
4122 "\377",
4123 "\377\377123",
4124 };
4125 SELF_CHECK (check_find_bounds_finds (mock_index,
4126 "\377", expected_syms1));
4127
4128 static const char *expected_syms2[] = {
4129 "\377\377123",
4130 };
4131 SELF_CHECK (check_find_bounds_finds (mock_index,
4132 "\377\377", expected_syms2));
4133 }
4134 }
4135
4136 /* Test dw2_expand_symtabs_matching_symbol. */
4137
4138 static void
4139 test_dw2_expand_symtabs_matching_symbol ()
4140 {
4141 mock_mapped_index mock_index (test_symbols);
4142
4143 /* We let all tests run until the end even if some fails, for debug
4144 convenience. */
4145 bool any_mismatch = false;
4146
4147 /* Create the expected symbols list (an initializer_list). Needed
4148 because lists have commas, and we need to pass them to CHECK,
4149 which is a macro. */
4150 #define EXPECT(...) { __VA_ARGS__ }
4151
4152 /* Wrapper for check_match that passes down the current
4153 __FILE__/__LINE__. */
4154 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4155 any_mismatch |= !check_match (__FILE__, __LINE__, \
4156 mock_index, \
4157 NAME, MATCH_TYPE, COMPLETION_MODE, \
4158 EXPECTED_LIST)
4159
4160 /* Identity checks. */
4161 for (const char *sym : test_symbols)
4162 {
4163 /* Should be able to match all existing symbols. */
4164 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4165 EXPECT (sym));
4166
4167 /* Should be able to match all existing symbols with
4168 parameters. */
4169 std::string with_params = std::string (sym) + "(int)";
4170 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4171 EXPECT (sym));
4172
4173 /* Should be able to match all existing symbols with
4174 parameters and qualifiers. */
4175 with_params = std::string (sym) + " ( int ) const";
4176 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4177 EXPECT (sym));
4178
4179 /* This should really find sym, but cp-name-parser.y doesn't
4180 know about lvalue/rvalue qualifiers yet. */
4181 with_params = std::string (sym) + " ( int ) &&";
4182 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4183 {});
4184 }
4185
4186 /* Check that the name matching algorithm for completion doesn't get
4187 confused with Latin1 'ÿ' / 0xff. */
4188 {
4189 static const char str[] = "\377";
4190 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4191 EXPECT ("\377", "\377\377123"));
4192 }
4193
4194 /* Check that the increment-last-char in the matching algorithm for
4195 completion doesn't match "t1_fund" when completing "t1_func". */
4196 {
4197 static const char str[] = "t1_func";
4198 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4199 EXPECT ("t1_func", "t1_func1"));
4200 }
4201
4202 /* Check that completion mode works at each prefix of the expected
4203 symbol name. */
4204 {
4205 static const char str[] = "function(int)";
4206 size_t len = strlen (str);
4207 std::string lookup;
4208
4209 for (size_t i = 1; i < len; i++)
4210 {
4211 lookup.assign (str, i);
4212 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4213 EXPECT ("function"));
4214 }
4215 }
4216
4217 /* While "w" is a prefix of both components, the match function
4218 should still only be called once. */
4219 {
4220 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4221 EXPECT ("w1::w2"));
4222 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4223 EXPECT ("w1::w2"));
4224 }
4225
4226 /* Same, with a "complicated" symbol. */
4227 {
4228 static const char str[] = Z_SYM_NAME;
4229 size_t len = strlen (str);
4230 std::string lookup;
4231
4232 for (size_t i = 1; i < len; i++)
4233 {
4234 lookup.assign (str, i);
4235 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4236 EXPECT (Z_SYM_NAME));
4237 }
4238 }
4239
4240 /* In FULL mode, an incomplete symbol doesn't match. */
4241 {
4242 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4243 {});
4244 }
4245
4246 /* A complete symbol with parameters matches any overload, since the
4247 index has no overload info. */
4248 {
4249 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4250 EXPECT ("std::zfunction", "std::zfunction2"));
4251 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4252 EXPECT ("std::zfunction", "std::zfunction2"));
4253 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4254 EXPECT ("std::zfunction", "std::zfunction2"));
4255 }
4256
4257 /* Check that whitespace is ignored appropriately. A symbol with a
4258 template argument list. */
4259 {
4260 static const char expected[] = "ns::foo<int>";
4261 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4262 EXPECT (expected));
4263 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4264 EXPECT (expected));
4265 }
4266
4267 /* Check that whitespace is ignored appropriately. A symbol with a
4268 template argument list that includes a pointer. */
4269 {
4270 static const char expected[] = "ns::foo<char*>";
4271 /* Try both completion and non-completion modes. */
4272 static const bool completion_mode[2] = {false, true};
4273 for (size_t i = 0; i < 2; i++)
4274 {
4275 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4276 completion_mode[i], EXPECT (expected));
4277 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4278 completion_mode[i], EXPECT (expected));
4279
4280 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4281 completion_mode[i], EXPECT (expected));
4282 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4283 completion_mode[i], EXPECT (expected));
4284 }
4285 }
4286
4287 {
4288 /* Check method qualifiers are ignored. */
4289 static const char expected[] = "ns::foo<char*>";
4290 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4291 symbol_name_match_type::FULL, true, EXPECT (expected));
4292 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4293 symbol_name_match_type::FULL, true, EXPECT (expected));
4294 CHECK_MATCH ("foo < char * > ( int ) const",
4295 symbol_name_match_type::WILD, true, EXPECT (expected));
4296 CHECK_MATCH ("foo < char * > ( int ) &&",
4297 symbol_name_match_type::WILD, true, EXPECT (expected));
4298 }
4299
4300 /* Test lookup names that don't match anything. */
4301 {
4302 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4303 {});
4304
4305 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4306 {});
4307 }
4308
4309 /* Some wild matching tests, exercising "(anonymous namespace)",
4310 which should not be confused with a parameter list. */
4311 {
4312 static const char *syms[] = {
4313 "A::B::C",
4314 "B::C",
4315 "C",
4316 "A :: B :: C ( int )",
4317 "B :: C ( int )",
4318 "C ( int )",
4319 };
4320
4321 for (const char *s : syms)
4322 {
4323 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4324 EXPECT ("(anonymous namespace)::A::B::C"));
4325 }
4326 }
4327
4328 {
4329 static const char expected[] = "ns2::tmpl<int>::foo2";
4330 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4331 EXPECT (expected));
4332 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4333 EXPECT (expected));
4334 }
4335
4336 SELF_CHECK (!any_mismatch);
4337
4338 #undef EXPECT
4339 #undef CHECK_MATCH
4340 }
4341
4342 static void
4343 run_test ()
4344 {
4345 test_mapped_index_find_name_component_bounds ();
4346 test_dw2_expand_symtabs_matching_symbol ();
4347 }
4348
4349 }} // namespace selftests::dw2_expand_symtabs_matching
4350
4351 #endif /* GDB_SELF_TEST */
4352
4353 /* If FILE_MATCHER is NULL or if PER_CU has
4354 dwarf2_per_cu_quick_data::MARK set (see
4355 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4356 EXPANSION_NOTIFY on it. */
4357
4358 static void
4359 dw2_expand_symtabs_matching_one
4360 (struct dwarf2_per_cu_data *per_cu,
4361 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4362 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4363 {
4364 if (file_matcher == NULL || per_cu->v.quick->mark)
4365 {
4366 bool symtab_was_null
4367 = (per_cu->v.quick->compunit_symtab == NULL);
4368
4369 dw2_instantiate_symtab (per_cu, false);
4370
4371 if (expansion_notify != NULL
4372 && symtab_was_null
4373 && per_cu->v.quick->compunit_symtab != NULL)
4374 expansion_notify (per_cu->v.quick->compunit_symtab);
4375 }
4376 }
4377
4378 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4379 matched, to expand corresponding CUs that were marked. IDX is the
4380 index of the symbol name that matched. */
4381
4382 static void
4383 dw2_expand_marked_cus
4384 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4385 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4386 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4387 search_domain kind)
4388 {
4389 offset_type *vec, vec_len, vec_idx;
4390 bool global_seen = false;
4391 mapped_index &index = *dwarf2_per_objfile->index_table;
4392
4393 vec = (offset_type *) (index.constant_pool
4394 + MAYBE_SWAP (index.symbol_table[idx].vec));
4395 vec_len = MAYBE_SWAP (vec[0]);
4396 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4397 {
4398 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4399 /* This value is only valid for index versions >= 7. */
4400 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4401 gdb_index_symbol_kind symbol_kind =
4402 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4403 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4404 /* Only check the symbol attributes if they're present.
4405 Indices prior to version 7 don't record them,
4406 and indices >= 7 may elide them for certain symbols
4407 (gold does this). */
4408 int attrs_valid =
4409 (index.version >= 7
4410 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4411
4412 /* Work around gold/15646. */
4413 if (attrs_valid)
4414 {
4415 if (!is_static && global_seen)
4416 continue;
4417 if (!is_static)
4418 global_seen = true;
4419 }
4420
4421 /* Only check the symbol's kind if it has one. */
4422 if (attrs_valid)
4423 {
4424 switch (kind)
4425 {
4426 case VARIABLES_DOMAIN:
4427 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4428 continue;
4429 break;
4430 case FUNCTIONS_DOMAIN:
4431 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4432 continue;
4433 break;
4434 case TYPES_DOMAIN:
4435 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4436 continue;
4437 break;
4438 case MODULES_DOMAIN:
4439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4440 continue;
4441 break;
4442 default:
4443 break;
4444 }
4445 }
4446
4447 /* Don't crash on bad data. */
4448 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4449 + dwarf2_per_objfile->all_type_units.size ()))
4450 {
4451 complaint (_(".gdb_index entry has bad CU index"
4452 " [in module %s]"),
4453 objfile_name (dwarf2_per_objfile->objfile));
4454 continue;
4455 }
4456
4457 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4458 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4459 expansion_notify);
4460 }
4461 }
4462
4463 /* If FILE_MATCHER is non-NULL, set all the
4464 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4465 that match FILE_MATCHER. */
4466
4467 static void
4468 dw_expand_symtabs_matching_file_matcher
4469 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4470 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4471 {
4472 if (file_matcher == NULL)
4473 return;
4474
4475 objfile *const objfile = dwarf2_per_objfile->objfile;
4476
4477 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4478 htab_eq_pointer,
4479 NULL, xcalloc, xfree));
4480 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4481 htab_eq_pointer,
4482 NULL, xcalloc, xfree));
4483
4484 /* The rule is CUs specify all the files, including those used by
4485 any TU, so there's no need to scan TUs here. */
4486
4487 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4488 {
4489 QUIT;
4490
4491 per_cu->v.quick->mark = 0;
4492
4493 /* We only need to look at symtabs not already expanded. */
4494 if (per_cu->v.quick->compunit_symtab)
4495 continue;
4496
4497 quick_file_names *file_data = dw2_get_file_names (per_cu);
4498 if (file_data == NULL)
4499 continue;
4500
4501 if (htab_find (visited_not_found.get (), file_data) != NULL)
4502 continue;
4503 else if (htab_find (visited_found.get (), file_data) != NULL)
4504 {
4505 per_cu->v.quick->mark = 1;
4506 continue;
4507 }
4508
4509 for (int j = 0; j < file_data->num_file_names; ++j)
4510 {
4511 const char *this_real_name;
4512
4513 if (file_matcher (file_data->file_names[j], false))
4514 {
4515 per_cu->v.quick->mark = 1;
4516 break;
4517 }
4518
4519 /* Before we invoke realpath, which can get expensive when many
4520 files are involved, do a quick comparison of the basenames. */
4521 if (!basenames_may_differ
4522 && !file_matcher (lbasename (file_data->file_names[j]),
4523 true))
4524 continue;
4525
4526 this_real_name = dw2_get_real_path (objfile, file_data, j);
4527 if (file_matcher (this_real_name, false))
4528 {
4529 per_cu->v.quick->mark = 1;
4530 break;
4531 }
4532 }
4533
4534 void **slot = htab_find_slot (per_cu->v.quick->mark
4535 ? visited_found.get ()
4536 : visited_not_found.get (),
4537 file_data, INSERT);
4538 *slot = file_data;
4539 }
4540 }
4541
4542 static void
4543 dw2_expand_symtabs_matching
4544 (struct objfile *objfile,
4545 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4546 const lookup_name_info &lookup_name,
4547 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4548 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4549 enum search_domain kind)
4550 {
4551 struct dwarf2_per_objfile *dwarf2_per_objfile
4552 = get_dwarf2_per_objfile (objfile);
4553
4554 /* index_table is NULL if OBJF_READNOW. */
4555 if (!dwarf2_per_objfile->index_table)
4556 return;
4557
4558 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4559
4560 mapped_index &index = *dwarf2_per_objfile->index_table;
4561
4562 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4563 symbol_matcher,
4564 kind, [&] (offset_type idx)
4565 {
4566 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4567 expansion_notify, kind);
4568 return true;
4569 });
4570 }
4571
4572 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4573 symtab. */
4574
4575 static struct compunit_symtab *
4576 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4577 CORE_ADDR pc)
4578 {
4579 int i;
4580
4581 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4582 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4583 return cust;
4584
4585 if (cust->includes == NULL)
4586 return NULL;
4587
4588 for (i = 0; cust->includes[i]; ++i)
4589 {
4590 struct compunit_symtab *s = cust->includes[i];
4591
4592 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4593 if (s != NULL)
4594 return s;
4595 }
4596
4597 return NULL;
4598 }
4599
4600 static struct compunit_symtab *
4601 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4602 struct bound_minimal_symbol msymbol,
4603 CORE_ADDR pc,
4604 struct obj_section *section,
4605 int warn_if_readin)
4606 {
4607 struct dwarf2_per_cu_data *data;
4608 struct compunit_symtab *result;
4609
4610 if (!objfile->partial_symtabs->psymtabs_addrmap)
4611 return NULL;
4612
4613 CORE_ADDR baseaddr = objfile->text_section_offset ();
4614 data = (struct dwarf2_per_cu_data *) addrmap_find
4615 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4616 if (!data)
4617 return NULL;
4618
4619 if (warn_if_readin && data->v.quick->compunit_symtab)
4620 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4621 paddress (get_objfile_arch (objfile), pc));
4622
4623 result
4624 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4625 false),
4626 pc);
4627 gdb_assert (result != NULL);
4628 return result;
4629 }
4630
4631 static void
4632 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4633 void *data, int need_fullname)
4634 {
4635 struct dwarf2_per_objfile *dwarf2_per_objfile
4636 = get_dwarf2_per_objfile (objfile);
4637
4638 if (!dwarf2_per_objfile->filenames_cache)
4639 {
4640 dwarf2_per_objfile->filenames_cache.emplace ();
4641
4642 htab_up visited (htab_create_alloc (10,
4643 htab_hash_pointer, htab_eq_pointer,
4644 NULL, xcalloc, xfree));
4645
4646 /* The rule is CUs specify all the files, including those used
4647 by any TU, so there's no need to scan TUs here. We can
4648 ignore file names coming from already-expanded CUs. */
4649
4650 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4651 {
4652 if (per_cu->v.quick->compunit_symtab)
4653 {
4654 void **slot = htab_find_slot (visited.get (),
4655 per_cu->v.quick->file_names,
4656 INSERT);
4657
4658 *slot = per_cu->v.quick->file_names;
4659 }
4660 }
4661
4662 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4663 {
4664 /* We only need to look at symtabs not already expanded. */
4665 if (per_cu->v.quick->compunit_symtab)
4666 continue;
4667
4668 quick_file_names *file_data = dw2_get_file_names (per_cu);
4669 if (file_data == NULL)
4670 continue;
4671
4672 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4673 if (*slot)
4674 {
4675 /* Already visited. */
4676 continue;
4677 }
4678 *slot = file_data;
4679
4680 for (int j = 0; j < file_data->num_file_names; ++j)
4681 {
4682 const char *filename = file_data->file_names[j];
4683 dwarf2_per_objfile->filenames_cache->seen (filename);
4684 }
4685 }
4686 }
4687
4688 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4689 {
4690 gdb::unique_xmalloc_ptr<char> this_real_name;
4691
4692 if (need_fullname)
4693 this_real_name = gdb_realpath (filename);
4694 (*fun) (filename, this_real_name.get (), data);
4695 });
4696 }
4697
4698 static int
4699 dw2_has_symbols (struct objfile *objfile)
4700 {
4701 return 1;
4702 }
4703
4704 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4705 {
4706 dw2_has_symbols,
4707 dw2_find_last_source_symtab,
4708 dw2_forget_cached_source_info,
4709 dw2_map_symtabs_matching_filename,
4710 dw2_lookup_symbol,
4711 dw2_print_stats,
4712 dw2_dump,
4713 dw2_expand_symtabs_for_function,
4714 dw2_expand_all_symtabs,
4715 dw2_expand_symtabs_with_fullname,
4716 dw2_map_matching_symbols,
4717 dw2_expand_symtabs_matching,
4718 dw2_find_pc_sect_compunit_symtab,
4719 NULL,
4720 dw2_map_symbol_filenames
4721 };
4722
4723 /* DWARF-5 debug_names reader. */
4724
4725 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4726 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4727
4728 /* A helper function that reads the .debug_names section in SECTION
4729 and fills in MAP. FILENAME is the name of the file containing the
4730 section; it is used for error reporting.
4731
4732 Returns true if all went well, false otherwise. */
4733
4734 static bool
4735 read_debug_names_from_section (struct objfile *objfile,
4736 const char *filename,
4737 struct dwarf2_section_info *section,
4738 mapped_debug_names &map)
4739 {
4740 if (section->empty ())
4741 return false;
4742
4743 /* Older elfutils strip versions could keep the section in the main
4744 executable while splitting it for the separate debug info file. */
4745 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4746 return false;
4747
4748 section->read (objfile);
4749
4750 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4751
4752 const gdb_byte *addr = section->buffer;
4753
4754 bfd *const abfd = section->get_bfd_owner ();
4755
4756 unsigned int bytes_read;
4757 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4758 addr += bytes_read;
4759
4760 map.dwarf5_is_dwarf64 = bytes_read != 4;
4761 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4762 if (bytes_read + length != section->size)
4763 {
4764 /* There may be multiple per-CU indices. */
4765 warning (_("Section .debug_names in %s length %s does not match "
4766 "section length %s, ignoring .debug_names."),
4767 filename, plongest (bytes_read + length),
4768 pulongest (section->size));
4769 return false;
4770 }
4771
4772 /* The version number. */
4773 uint16_t version = read_2_bytes (abfd, addr);
4774 addr += 2;
4775 if (version != 5)
4776 {
4777 warning (_("Section .debug_names in %s has unsupported version %d, "
4778 "ignoring .debug_names."),
4779 filename, version);
4780 return false;
4781 }
4782
4783 /* Padding. */
4784 uint16_t padding = read_2_bytes (abfd, addr);
4785 addr += 2;
4786 if (padding != 0)
4787 {
4788 warning (_("Section .debug_names in %s has unsupported padding %d, "
4789 "ignoring .debug_names."),
4790 filename, padding);
4791 return false;
4792 }
4793
4794 /* comp_unit_count - The number of CUs in the CU list. */
4795 map.cu_count = read_4_bytes (abfd, addr);
4796 addr += 4;
4797
4798 /* local_type_unit_count - The number of TUs in the local TU
4799 list. */
4800 map.tu_count = read_4_bytes (abfd, addr);
4801 addr += 4;
4802
4803 /* foreign_type_unit_count - The number of TUs in the foreign TU
4804 list. */
4805 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4806 addr += 4;
4807 if (foreign_tu_count != 0)
4808 {
4809 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4810 "ignoring .debug_names."),
4811 filename, static_cast<unsigned long> (foreign_tu_count));
4812 return false;
4813 }
4814
4815 /* bucket_count - The number of hash buckets in the hash lookup
4816 table. */
4817 map.bucket_count = read_4_bytes (abfd, addr);
4818 addr += 4;
4819
4820 /* name_count - The number of unique names in the index. */
4821 map.name_count = read_4_bytes (abfd, addr);
4822 addr += 4;
4823
4824 /* abbrev_table_size - The size in bytes of the abbreviations
4825 table. */
4826 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4827 addr += 4;
4828
4829 /* augmentation_string_size - The size in bytes of the augmentation
4830 string. This value is rounded up to a multiple of 4. */
4831 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4832 addr += 4;
4833 map.augmentation_is_gdb = ((augmentation_string_size
4834 == sizeof (dwarf5_augmentation))
4835 && memcmp (addr, dwarf5_augmentation,
4836 sizeof (dwarf5_augmentation)) == 0);
4837 augmentation_string_size += (-augmentation_string_size) & 3;
4838 addr += augmentation_string_size;
4839
4840 /* List of CUs */
4841 map.cu_table_reordered = addr;
4842 addr += map.cu_count * map.offset_size;
4843
4844 /* List of Local TUs */
4845 map.tu_table_reordered = addr;
4846 addr += map.tu_count * map.offset_size;
4847
4848 /* Hash Lookup Table */
4849 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4850 addr += map.bucket_count * 4;
4851 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4852 addr += map.name_count * 4;
4853
4854 /* Name Table */
4855 map.name_table_string_offs_reordered = addr;
4856 addr += map.name_count * map.offset_size;
4857 map.name_table_entry_offs_reordered = addr;
4858 addr += map.name_count * map.offset_size;
4859
4860 const gdb_byte *abbrev_table_start = addr;
4861 for (;;)
4862 {
4863 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4864 addr += bytes_read;
4865 if (index_num == 0)
4866 break;
4867
4868 const auto insertpair
4869 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4870 if (!insertpair.second)
4871 {
4872 warning (_("Section .debug_names in %s has duplicate index %s, "
4873 "ignoring .debug_names."),
4874 filename, pulongest (index_num));
4875 return false;
4876 }
4877 mapped_debug_names::index_val &indexval = insertpair.first->second;
4878 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4879 addr += bytes_read;
4880
4881 for (;;)
4882 {
4883 mapped_debug_names::index_val::attr attr;
4884 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4885 addr += bytes_read;
4886 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4887 addr += bytes_read;
4888 if (attr.form == DW_FORM_implicit_const)
4889 {
4890 attr.implicit_const = read_signed_leb128 (abfd, addr,
4891 &bytes_read);
4892 addr += bytes_read;
4893 }
4894 if (attr.dw_idx == 0 && attr.form == 0)
4895 break;
4896 indexval.attr_vec.push_back (std::move (attr));
4897 }
4898 }
4899 if (addr != abbrev_table_start + abbrev_table_size)
4900 {
4901 warning (_("Section .debug_names in %s has abbreviation_table "
4902 "of size %s vs. written as %u, ignoring .debug_names."),
4903 filename, plongest (addr - abbrev_table_start),
4904 abbrev_table_size);
4905 return false;
4906 }
4907 map.entry_pool = addr;
4908
4909 return true;
4910 }
4911
4912 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4913 list. */
4914
4915 static void
4916 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4917 const mapped_debug_names &map,
4918 dwarf2_section_info &section,
4919 bool is_dwz)
4920 {
4921 sect_offset sect_off_prev;
4922 for (uint32_t i = 0; i <= map.cu_count; ++i)
4923 {
4924 sect_offset sect_off_next;
4925 if (i < map.cu_count)
4926 {
4927 sect_off_next
4928 = (sect_offset) (extract_unsigned_integer
4929 (map.cu_table_reordered + i * map.offset_size,
4930 map.offset_size,
4931 map.dwarf5_byte_order));
4932 }
4933 else
4934 sect_off_next = (sect_offset) section.size;
4935 if (i >= 1)
4936 {
4937 const ULONGEST length = sect_off_next - sect_off_prev;
4938 dwarf2_per_cu_data *per_cu
4939 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4940 sect_off_prev, length);
4941 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4942 }
4943 sect_off_prev = sect_off_next;
4944 }
4945 }
4946
4947 /* Read the CU list from the mapped index, and use it to create all
4948 the CU objects for this dwarf2_per_objfile. */
4949
4950 static void
4951 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4952 const mapped_debug_names &map,
4953 const mapped_debug_names &dwz_map)
4954 {
4955 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4956 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4957
4958 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4959 dwarf2_per_objfile->info,
4960 false /* is_dwz */);
4961
4962 if (dwz_map.cu_count == 0)
4963 return;
4964
4965 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4966 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4967 true /* is_dwz */);
4968 }
4969
4970 /* Read .debug_names. If everything went ok, initialize the "quick"
4971 elements of all the CUs and return true. Otherwise, return false. */
4972
4973 static bool
4974 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4975 {
4976 std::unique_ptr<mapped_debug_names> map
4977 (new mapped_debug_names (dwarf2_per_objfile));
4978 mapped_debug_names dwz_map (dwarf2_per_objfile);
4979 struct objfile *objfile = dwarf2_per_objfile->objfile;
4980
4981 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4982 &dwarf2_per_objfile->debug_names,
4983 *map))
4984 return false;
4985
4986 /* Don't use the index if it's empty. */
4987 if (map->name_count == 0)
4988 return false;
4989
4990 /* If there is a .dwz file, read it so we can get its CU list as
4991 well. */
4992 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4993 if (dwz != NULL)
4994 {
4995 if (!read_debug_names_from_section (objfile,
4996 bfd_get_filename (dwz->dwz_bfd.get ()),
4997 &dwz->debug_names, dwz_map))
4998 {
4999 warning (_("could not read '.debug_names' section from %s; skipping"),
5000 bfd_get_filename (dwz->dwz_bfd.get ()));
5001 return false;
5002 }
5003 }
5004
5005 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5006
5007 if (map->tu_count != 0)
5008 {
5009 /* We can only handle a single .debug_types when we have an
5010 index. */
5011 if (dwarf2_per_objfile->types.size () != 1)
5012 return false;
5013
5014 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5015
5016 create_signatured_type_table_from_debug_names
5017 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5018 }
5019
5020 create_addrmap_from_aranges (dwarf2_per_objfile,
5021 &dwarf2_per_objfile->debug_aranges);
5022
5023 dwarf2_per_objfile->debug_names_table = std::move (map);
5024 dwarf2_per_objfile->using_index = 1;
5025 dwarf2_per_objfile->quick_file_names_table =
5026 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5027
5028 return true;
5029 }
5030
5031 /* Type used to manage iterating over all CUs looking for a symbol for
5032 .debug_names. */
5033
5034 class dw2_debug_names_iterator
5035 {
5036 public:
5037 dw2_debug_names_iterator (const mapped_debug_names &map,
5038 gdb::optional<block_enum> block_index,
5039 domain_enum domain,
5040 const char *name)
5041 : m_map (map), m_block_index (block_index), m_domain (domain),
5042 m_addr (find_vec_in_debug_names (map, name))
5043 {}
5044
5045 dw2_debug_names_iterator (const mapped_debug_names &map,
5046 search_domain search, uint32_t namei)
5047 : m_map (map),
5048 m_search (search),
5049 m_addr (find_vec_in_debug_names (map, namei))
5050 {}
5051
5052 dw2_debug_names_iterator (const mapped_debug_names &map,
5053 block_enum block_index, domain_enum domain,
5054 uint32_t namei)
5055 : m_map (map), m_block_index (block_index), m_domain (domain),
5056 m_addr (find_vec_in_debug_names (map, namei))
5057 {}
5058
5059 /* Return the next matching CU or NULL if there are no more. */
5060 dwarf2_per_cu_data *next ();
5061
5062 private:
5063 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5064 const char *name);
5065 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5066 uint32_t namei);
5067
5068 /* The internalized form of .debug_names. */
5069 const mapped_debug_names &m_map;
5070
5071 /* If set, only look for symbols that match that block. Valid values are
5072 GLOBAL_BLOCK and STATIC_BLOCK. */
5073 const gdb::optional<block_enum> m_block_index;
5074
5075 /* The kind of symbol we're looking for. */
5076 const domain_enum m_domain = UNDEF_DOMAIN;
5077 const search_domain m_search = ALL_DOMAIN;
5078
5079 /* The list of CUs from the index entry of the symbol, or NULL if
5080 not found. */
5081 const gdb_byte *m_addr;
5082 };
5083
5084 const char *
5085 mapped_debug_names::namei_to_name (uint32_t namei) const
5086 {
5087 const ULONGEST namei_string_offs
5088 = extract_unsigned_integer ((name_table_string_offs_reordered
5089 + namei * offset_size),
5090 offset_size,
5091 dwarf5_byte_order);
5092 return read_indirect_string_at_offset (dwarf2_per_objfile,
5093 namei_string_offs);
5094 }
5095
5096 /* Find a slot in .debug_names for the object named NAME. If NAME is
5097 found, return pointer to its pool data. If NAME cannot be found,
5098 return NULL. */
5099
5100 const gdb_byte *
5101 dw2_debug_names_iterator::find_vec_in_debug_names
5102 (const mapped_debug_names &map, const char *name)
5103 {
5104 int (*cmp) (const char *, const char *);
5105
5106 gdb::unique_xmalloc_ptr<char> without_params;
5107 if (current_language->la_language == language_cplus
5108 || current_language->la_language == language_fortran
5109 || current_language->la_language == language_d)
5110 {
5111 /* NAME is already canonical. Drop any qualifiers as
5112 .debug_names does not contain any. */
5113
5114 if (strchr (name, '(') != NULL)
5115 {
5116 without_params = cp_remove_params (name);
5117 if (without_params != NULL)
5118 name = without_params.get ();
5119 }
5120 }
5121
5122 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5123
5124 const uint32_t full_hash = dwarf5_djb_hash (name);
5125 uint32_t namei
5126 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5127 (map.bucket_table_reordered
5128 + (full_hash % map.bucket_count)), 4,
5129 map.dwarf5_byte_order);
5130 if (namei == 0)
5131 return NULL;
5132 --namei;
5133 if (namei >= map.name_count)
5134 {
5135 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5136 "[in module %s]"),
5137 namei, map.name_count,
5138 objfile_name (map.dwarf2_per_objfile->objfile));
5139 return NULL;
5140 }
5141
5142 for (;;)
5143 {
5144 const uint32_t namei_full_hash
5145 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5146 (map.hash_table_reordered + namei), 4,
5147 map.dwarf5_byte_order);
5148 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5149 return NULL;
5150
5151 if (full_hash == namei_full_hash)
5152 {
5153 const char *const namei_string = map.namei_to_name (namei);
5154
5155 #if 0 /* An expensive sanity check. */
5156 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5157 {
5158 complaint (_("Wrong .debug_names hash for string at index %u "
5159 "[in module %s]"),
5160 namei, objfile_name (dwarf2_per_objfile->objfile));
5161 return NULL;
5162 }
5163 #endif
5164
5165 if (cmp (namei_string, name) == 0)
5166 {
5167 const ULONGEST namei_entry_offs
5168 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5169 + namei * map.offset_size),
5170 map.offset_size, map.dwarf5_byte_order);
5171 return map.entry_pool + namei_entry_offs;
5172 }
5173 }
5174
5175 ++namei;
5176 if (namei >= map.name_count)
5177 return NULL;
5178 }
5179 }
5180
5181 const gdb_byte *
5182 dw2_debug_names_iterator::find_vec_in_debug_names
5183 (const mapped_debug_names &map, uint32_t namei)
5184 {
5185 if (namei >= map.name_count)
5186 {
5187 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5188 "[in module %s]"),
5189 namei, map.name_count,
5190 objfile_name (map.dwarf2_per_objfile->objfile));
5191 return NULL;
5192 }
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 /* See dw2_debug_names_iterator. */
5202
5203 dwarf2_per_cu_data *
5204 dw2_debug_names_iterator::next ()
5205 {
5206 if (m_addr == NULL)
5207 return NULL;
5208
5209 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5210 struct objfile *objfile = dwarf2_per_objfile->objfile;
5211 bfd *const abfd = objfile->obfd;
5212
5213 again:
5214
5215 unsigned int bytes_read;
5216 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5217 m_addr += bytes_read;
5218 if (abbrev == 0)
5219 return NULL;
5220
5221 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5222 if (indexval_it == m_map.abbrev_map.cend ())
5223 {
5224 complaint (_("Wrong .debug_names undefined abbrev code %s "
5225 "[in module %s]"),
5226 pulongest (abbrev), objfile_name (objfile));
5227 return NULL;
5228 }
5229 const mapped_debug_names::index_val &indexval = indexval_it->second;
5230 enum class symbol_linkage {
5231 unknown,
5232 static_,
5233 extern_,
5234 } symbol_linkage_ = symbol_linkage::unknown;
5235 dwarf2_per_cu_data *per_cu = NULL;
5236 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5237 {
5238 ULONGEST ull;
5239 switch (attr.form)
5240 {
5241 case DW_FORM_implicit_const:
5242 ull = attr.implicit_const;
5243 break;
5244 case DW_FORM_flag_present:
5245 ull = 1;
5246 break;
5247 case DW_FORM_udata:
5248 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5249 m_addr += bytes_read;
5250 break;
5251 default:
5252 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5253 dwarf_form_name (attr.form),
5254 objfile_name (objfile));
5255 return NULL;
5256 }
5257 switch (attr.dw_idx)
5258 {
5259 case DW_IDX_compile_unit:
5260 /* Don't crash on bad data. */
5261 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5262 {
5263 complaint (_(".debug_names entry has bad CU index %s"
5264 " [in module %s]"),
5265 pulongest (ull),
5266 objfile_name (dwarf2_per_objfile->objfile));
5267 continue;
5268 }
5269 per_cu = dwarf2_per_objfile->get_cutu (ull);
5270 break;
5271 case DW_IDX_type_unit:
5272 /* Don't crash on bad data. */
5273 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5274 {
5275 complaint (_(".debug_names entry has bad TU index %s"
5276 " [in module %s]"),
5277 pulongest (ull),
5278 objfile_name (dwarf2_per_objfile->objfile));
5279 continue;
5280 }
5281 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5282 break;
5283 case DW_IDX_GNU_internal:
5284 if (!m_map.augmentation_is_gdb)
5285 break;
5286 symbol_linkage_ = symbol_linkage::static_;
5287 break;
5288 case DW_IDX_GNU_external:
5289 if (!m_map.augmentation_is_gdb)
5290 break;
5291 symbol_linkage_ = symbol_linkage::extern_;
5292 break;
5293 }
5294 }
5295
5296 /* Skip if already read in. */
5297 if (per_cu->v.quick->compunit_symtab)
5298 goto again;
5299
5300 /* Check static vs global. */
5301 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5302 {
5303 const bool want_static = *m_block_index == STATIC_BLOCK;
5304 const bool symbol_is_static =
5305 symbol_linkage_ == symbol_linkage::static_;
5306 if (want_static != symbol_is_static)
5307 goto again;
5308 }
5309
5310 /* Match dw2_symtab_iter_next, symbol_kind
5311 and debug_names::psymbol_tag. */
5312 switch (m_domain)
5313 {
5314 case VAR_DOMAIN:
5315 switch (indexval.dwarf_tag)
5316 {
5317 case DW_TAG_variable:
5318 case DW_TAG_subprogram:
5319 /* Some types are also in VAR_DOMAIN. */
5320 case DW_TAG_typedef:
5321 case DW_TAG_structure_type:
5322 break;
5323 default:
5324 goto again;
5325 }
5326 break;
5327 case STRUCT_DOMAIN:
5328 switch (indexval.dwarf_tag)
5329 {
5330 case DW_TAG_typedef:
5331 case DW_TAG_structure_type:
5332 break;
5333 default:
5334 goto again;
5335 }
5336 break;
5337 case LABEL_DOMAIN:
5338 switch (indexval.dwarf_tag)
5339 {
5340 case 0:
5341 case DW_TAG_variable:
5342 break;
5343 default:
5344 goto again;
5345 }
5346 break;
5347 case MODULE_DOMAIN:
5348 switch (indexval.dwarf_tag)
5349 {
5350 case DW_TAG_module:
5351 break;
5352 default:
5353 goto again;
5354 }
5355 break;
5356 default:
5357 break;
5358 }
5359
5360 /* Match dw2_expand_symtabs_matching, symbol_kind and
5361 debug_names::psymbol_tag. */
5362 switch (m_search)
5363 {
5364 case VARIABLES_DOMAIN:
5365 switch (indexval.dwarf_tag)
5366 {
5367 case DW_TAG_variable:
5368 break;
5369 default:
5370 goto again;
5371 }
5372 break;
5373 case FUNCTIONS_DOMAIN:
5374 switch (indexval.dwarf_tag)
5375 {
5376 case DW_TAG_subprogram:
5377 break;
5378 default:
5379 goto again;
5380 }
5381 break;
5382 case TYPES_DOMAIN:
5383 switch (indexval.dwarf_tag)
5384 {
5385 case DW_TAG_typedef:
5386 case DW_TAG_structure_type:
5387 break;
5388 default:
5389 goto again;
5390 }
5391 break;
5392 case MODULES_DOMAIN:
5393 switch (indexval.dwarf_tag)
5394 {
5395 case DW_TAG_module:
5396 break;
5397 default:
5398 goto again;
5399 }
5400 default:
5401 break;
5402 }
5403
5404 return per_cu;
5405 }
5406
5407 static struct compunit_symtab *
5408 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5409 const char *name, domain_enum domain)
5410 {
5411 struct dwarf2_per_objfile *dwarf2_per_objfile
5412 = get_dwarf2_per_objfile (objfile);
5413
5414 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5415 if (!mapp)
5416 {
5417 /* index is NULL if OBJF_READNOW. */
5418 return NULL;
5419 }
5420 const auto &map = *mapp;
5421
5422 dw2_debug_names_iterator iter (map, block_index, domain, name);
5423
5424 struct compunit_symtab *stab_best = NULL;
5425 struct dwarf2_per_cu_data *per_cu;
5426 while ((per_cu = iter.next ()) != NULL)
5427 {
5428 struct symbol *sym, *with_opaque = NULL;
5429 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5430 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5431 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5432
5433 sym = block_find_symbol (block, name, domain,
5434 block_find_non_opaque_type_preferred,
5435 &with_opaque);
5436
5437 /* Some caution must be observed with overloaded functions and
5438 methods, since the index will not contain any overload
5439 information (but NAME might contain it). */
5440
5441 if (sym != NULL
5442 && strcmp_iw (sym->search_name (), name) == 0)
5443 return stab;
5444 if (with_opaque != NULL
5445 && strcmp_iw (with_opaque->search_name (), name) == 0)
5446 stab_best = stab;
5447
5448 /* Keep looking through other CUs. */
5449 }
5450
5451 return stab_best;
5452 }
5453
5454 /* This dumps minimal information about .debug_names. It is called
5455 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5456 uses this to verify that .debug_names has been loaded. */
5457
5458 static void
5459 dw2_debug_names_dump (struct objfile *objfile)
5460 {
5461 struct dwarf2_per_objfile *dwarf2_per_objfile
5462 = get_dwarf2_per_objfile (objfile);
5463
5464 gdb_assert (dwarf2_per_objfile->using_index);
5465 printf_filtered (".debug_names:");
5466 if (dwarf2_per_objfile->debug_names_table)
5467 printf_filtered (" exists\n");
5468 else
5469 printf_filtered (" faked for \"readnow\"\n");
5470 printf_filtered ("\n");
5471 }
5472
5473 static void
5474 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5475 const char *func_name)
5476 {
5477 struct dwarf2_per_objfile *dwarf2_per_objfile
5478 = get_dwarf2_per_objfile (objfile);
5479
5480 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5481 if (dwarf2_per_objfile->debug_names_table)
5482 {
5483 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5484
5485 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5486
5487 struct dwarf2_per_cu_data *per_cu;
5488 while ((per_cu = iter.next ()) != NULL)
5489 dw2_instantiate_symtab (per_cu, false);
5490 }
5491 }
5492
5493 static void
5494 dw2_debug_names_map_matching_symbols
5495 (struct objfile *objfile,
5496 const lookup_name_info &name, domain_enum domain,
5497 int global,
5498 gdb::function_view<symbol_found_callback_ftype> callback,
5499 symbol_compare_ftype *ordered_compare)
5500 {
5501 struct dwarf2_per_objfile *dwarf2_per_objfile
5502 = get_dwarf2_per_objfile (objfile);
5503
5504 /* debug_names_table is NULL if OBJF_READNOW. */
5505 if (!dwarf2_per_objfile->debug_names_table)
5506 return;
5507
5508 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5509 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5510
5511 const char *match_name = name.ada ().lookup_name ().c_str ();
5512 auto matcher = [&] (const char *symname)
5513 {
5514 if (ordered_compare == nullptr)
5515 return true;
5516 return ordered_compare (symname, match_name) == 0;
5517 };
5518
5519 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5520 [&] (offset_type namei)
5521 {
5522 /* The name was matched, now expand corresponding CUs that were
5523 marked. */
5524 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5525
5526 struct dwarf2_per_cu_data *per_cu;
5527 while ((per_cu = iter.next ()) != NULL)
5528 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5529 return true;
5530 });
5531
5532 /* It's a shame we couldn't do this inside the
5533 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5534 that have already been expanded. Instead, this loop matches what
5535 the psymtab code does. */
5536 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5537 {
5538 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5539 if (cust != nullptr)
5540 {
5541 const struct block *block
5542 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5543 if (!iterate_over_symbols_terminated (block, name,
5544 domain, callback))
5545 break;
5546 }
5547 }
5548 }
5549
5550 static void
5551 dw2_debug_names_expand_symtabs_matching
5552 (struct objfile *objfile,
5553 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5554 const lookup_name_info &lookup_name,
5555 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5556 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5557 enum search_domain kind)
5558 {
5559 struct dwarf2_per_objfile *dwarf2_per_objfile
5560 = get_dwarf2_per_objfile (objfile);
5561
5562 /* debug_names_table is NULL if OBJF_READNOW. */
5563 if (!dwarf2_per_objfile->debug_names_table)
5564 return;
5565
5566 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5567
5568 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5569
5570 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5571 symbol_matcher,
5572 kind, [&] (offset_type namei)
5573 {
5574 /* The name was matched, now expand corresponding CUs that were
5575 marked. */
5576 dw2_debug_names_iterator iter (map, kind, namei);
5577
5578 struct dwarf2_per_cu_data *per_cu;
5579 while ((per_cu = iter.next ()) != NULL)
5580 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5581 expansion_notify);
5582 return true;
5583 });
5584 }
5585
5586 const struct quick_symbol_functions dwarf2_debug_names_functions =
5587 {
5588 dw2_has_symbols,
5589 dw2_find_last_source_symtab,
5590 dw2_forget_cached_source_info,
5591 dw2_map_symtabs_matching_filename,
5592 dw2_debug_names_lookup_symbol,
5593 dw2_print_stats,
5594 dw2_debug_names_dump,
5595 dw2_debug_names_expand_symtabs_for_function,
5596 dw2_expand_all_symtabs,
5597 dw2_expand_symtabs_with_fullname,
5598 dw2_debug_names_map_matching_symbols,
5599 dw2_debug_names_expand_symtabs_matching,
5600 dw2_find_pc_sect_compunit_symtab,
5601 NULL,
5602 dw2_map_symbol_filenames
5603 };
5604
5605 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5606 to either a dwarf2_per_objfile or dwz_file object. */
5607
5608 template <typename T>
5609 static gdb::array_view<const gdb_byte>
5610 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5611 {
5612 dwarf2_section_info *section = &section_owner->gdb_index;
5613
5614 if (section->empty ())
5615 return {};
5616
5617 /* Older elfutils strip versions could keep the section in the main
5618 executable while splitting it for the separate debug info file. */
5619 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5620 return {};
5621
5622 section->read (obj);
5623
5624 /* dwarf2_section_info::size is a bfd_size_type, while
5625 gdb::array_view works with size_t. On 32-bit hosts, with
5626 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5627 is 32-bit. So we need an explicit narrowing conversion here.
5628 This is fine, because it's impossible to allocate or mmap an
5629 array/buffer larger than what size_t can represent. */
5630 return gdb::make_array_view (section->buffer, section->size);
5631 }
5632
5633 /* Lookup the index cache for the contents of the index associated to
5634 DWARF2_OBJ. */
5635
5636 static gdb::array_view<const gdb_byte>
5637 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5638 {
5639 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5640 if (build_id == nullptr)
5641 return {};
5642
5643 return global_index_cache.lookup_gdb_index (build_id,
5644 &dwarf2_obj->index_cache_res);
5645 }
5646
5647 /* Same as the above, but for DWZ. */
5648
5649 static gdb::array_view<const gdb_byte>
5650 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5651 {
5652 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5653 if (build_id == nullptr)
5654 return {};
5655
5656 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5657 }
5658
5659 /* See symfile.h. */
5660
5661 bool
5662 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5663 {
5664 struct dwarf2_per_objfile *dwarf2_per_objfile
5665 = get_dwarf2_per_objfile (objfile);
5666
5667 /* If we're about to read full symbols, don't bother with the
5668 indices. In this case we also don't care if some other debug
5669 format is making psymtabs, because they are all about to be
5670 expanded anyway. */
5671 if ((objfile->flags & OBJF_READNOW))
5672 {
5673 dwarf2_per_objfile->using_index = 1;
5674 create_all_comp_units (dwarf2_per_objfile);
5675 create_all_type_units (dwarf2_per_objfile);
5676 dwarf2_per_objfile->quick_file_names_table
5677 = create_quick_file_names_table
5678 (dwarf2_per_objfile->all_comp_units.size ());
5679
5680 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5681 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5682 {
5683 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5684
5685 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5686 struct dwarf2_per_cu_quick_data);
5687 }
5688
5689 /* Return 1 so that gdb sees the "quick" functions. However,
5690 these functions will be no-ops because we will have expanded
5691 all symtabs. */
5692 *index_kind = dw_index_kind::GDB_INDEX;
5693 return true;
5694 }
5695
5696 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5697 {
5698 *index_kind = dw_index_kind::DEBUG_NAMES;
5699 return true;
5700 }
5701
5702 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5703 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5704 get_gdb_index_contents_from_section<dwz_file>))
5705 {
5706 *index_kind = dw_index_kind::GDB_INDEX;
5707 return true;
5708 }
5709
5710 /* ... otherwise, try to find the index in the index cache. */
5711 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5712 get_gdb_index_contents_from_cache,
5713 get_gdb_index_contents_from_cache_dwz))
5714 {
5715 global_index_cache.hit ();
5716 *index_kind = dw_index_kind::GDB_INDEX;
5717 return true;
5718 }
5719
5720 global_index_cache.miss ();
5721 return false;
5722 }
5723
5724 \f
5725
5726 /* Build a partial symbol table. */
5727
5728 void
5729 dwarf2_build_psymtabs (struct objfile *objfile)
5730 {
5731 struct dwarf2_per_objfile *dwarf2_per_objfile
5732 = get_dwarf2_per_objfile (objfile);
5733
5734 init_psymbol_list (objfile, 1024);
5735
5736 try
5737 {
5738 /* This isn't really ideal: all the data we allocate on the
5739 objfile's obstack is still uselessly kept around. However,
5740 freeing it seems unsafe. */
5741 psymtab_discarder psymtabs (objfile);
5742 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5743 psymtabs.keep ();
5744
5745 /* (maybe) store an index in the cache. */
5746 global_index_cache.store (dwarf2_per_objfile);
5747 }
5748 catch (const gdb_exception_error &except)
5749 {
5750 exception_print (gdb_stderr, except);
5751 }
5752 }
5753
5754 /* Find the base address of the compilation unit for range lists and
5755 location lists. It will normally be specified by DW_AT_low_pc.
5756 In DWARF-3 draft 4, the base address could be overridden by
5757 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5758 compilation units with discontinuous ranges. */
5759
5760 static void
5761 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5762 {
5763 struct attribute *attr;
5764
5765 cu->base_address.reset ();
5766
5767 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5768 if (attr != nullptr)
5769 cu->base_address = attr->value_as_address ();
5770 else
5771 {
5772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5773 if (attr != nullptr)
5774 cu->base_address = attr->value_as_address ();
5775 }
5776 }
5777
5778 /* Helper function that returns the proper abbrev section for
5779 THIS_CU. */
5780
5781 static struct dwarf2_section_info *
5782 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5783 {
5784 struct dwarf2_section_info *abbrev;
5785 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5786
5787 if (this_cu->is_dwz)
5788 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5789 else
5790 abbrev = &dwarf2_per_objfile->abbrev;
5791
5792 return abbrev;
5793 }
5794
5795 /* Fetch the abbreviation table offset from a comp or type unit header. */
5796
5797 static sect_offset
5798 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5799 struct dwarf2_section_info *section,
5800 sect_offset sect_off)
5801 {
5802 bfd *abfd = section->get_bfd_owner ();
5803 const gdb_byte *info_ptr;
5804 unsigned int initial_length_size, offset_size;
5805 uint16_t version;
5806
5807 section->read (dwarf2_per_objfile->objfile);
5808 info_ptr = section->buffer + to_underlying (sect_off);
5809 read_initial_length (abfd, info_ptr, &initial_length_size);
5810 offset_size = initial_length_size == 4 ? 4 : 8;
5811 info_ptr += initial_length_size;
5812
5813 version = read_2_bytes (abfd, info_ptr);
5814 info_ptr += 2;
5815 if (version >= 5)
5816 {
5817 /* Skip unit type and address size. */
5818 info_ptr += 2;
5819 }
5820
5821 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5822 }
5823
5824 /* A partial symtab that is used only for include files. */
5825 struct dwarf2_include_psymtab : public partial_symtab
5826 {
5827 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5828 : partial_symtab (filename, objfile)
5829 {
5830 }
5831
5832 void read_symtab (struct objfile *objfile) override
5833 {
5834 expand_psymtab (objfile);
5835 }
5836
5837 void expand_psymtab (struct objfile *objfile) override
5838 {
5839 if (m_readin)
5840 return;
5841 /* It's an include file, no symbols to read for it.
5842 Everything is in the parent symtab. */
5843 expand_dependencies (objfile);
5844 m_readin = true;
5845 }
5846
5847 bool readin_p () const override
5848 {
5849 return m_readin;
5850 }
5851
5852 struct compunit_symtab *get_compunit_symtab () const override
5853 {
5854 return nullptr;
5855 }
5856
5857 private:
5858
5859 bool m_readin = false;
5860 };
5861
5862 /* Allocate a new partial symtab for file named NAME and mark this new
5863 partial symtab as being an include of PST. */
5864
5865 static void
5866 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5867 struct objfile *objfile)
5868 {
5869 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5870
5871 if (!IS_ABSOLUTE_PATH (subpst->filename))
5872 {
5873 /* It shares objfile->objfile_obstack. */
5874 subpst->dirname = pst->dirname;
5875 }
5876
5877 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5878 subpst->dependencies[0] = pst;
5879 subpst->number_of_dependencies = 1;
5880 }
5881
5882 /* Read the Line Number Program data and extract the list of files
5883 included by the source file represented by PST. Build an include
5884 partial symtab for each of these included files. */
5885
5886 static void
5887 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5888 struct die_info *die,
5889 dwarf2_psymtab *pst)
5890 {
5891 line_header_up lh;
5892 struct attribute *attr;
5893
5894 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5895 if (attr != nullptr)
5896 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5897 if (lh == NULL)
5898 return; /* No linetable, so no includes. */
5899
5900 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5901 that we pass in the raw text_low here; that is ok because we're
5902 only decoding the line table to make include partial symtabs, and
5903 so the addresses aren't really used. */
5904 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5905 pst->raw_text_low (), 1);
5906 }
5907
5908 static hashval_t
5909 hash_signatured_type (const void *item)
5910 {
5911 const struct signatured_type *sig_type
5912 = (const struct signatured_type *) item;
5913
5914 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5915 return sig_type->signature;
5916 }
5917
5918 static int
5919 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5920 {
5921 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5922 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5923
5924 return lhs->signature == rhs->signature;
5925 }
5926
5927 /* Allocate a hash table for signatured types. */
5928
5929 static htab_up
5930 allocate_signatured_type_table ()
5931 {
5932 return htab_up (htab_create_alloc (41,
5933 hash_signatured_type,
5934 eq_signatured_type,
5935 NULL, xcalloc, xfree));
5936 }
5937
5938 /* A helper function to add a signatured type CU to a table. */
5939
5940 static int
5941 add_signatured_type_cu_to_table (void **slot, void *datum)
5942 {
5943 struct signatured_type *sigt = (struct signatured_type *) *slot;
5944 std::vector<signatured_type *> *all_type_units
5945 = (std::vector<signatured_type *> *) datum;
5946
5947 all_type_units->push_back (sigt);
5948
5949 return 1;
5950 }
5951
5952 /* A helper for create_debug_types_hash_table. Read types from SECTION
5953 and fill them into TYPES_HTAB. It will process only type units,
5954 therefore DW_UT_type. */
5955
5956 static void
5957 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5958 struct dwo_file *dwo_file,
5959 dwarf2_section_info *section, htab_up &types_htab,
5960 rcuh_kind section_kind)
5961 {
5962 struct objfile *objfile = dwarf2_per_objfile->objfile;
5963 struct dwarf2_section_info *abbrev_section;
5964 bfd *abfd;
5965 const gdb_byte *info_ptr, *end_ptr;
5966
5967 abbrev_section = (dwo_file != NULL
5968 ? &dwo_file->sections.abbrev
5969 : &dwarf2_per_objfile->abbrev);
5970
5971 if (dwarf_read_debug)
5972 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5973 section->get_name (),
5974 abbrev_section->get_file_name ());
5975
5976 section->read (objfile);
5977 info_ptr = section->buffer;
5978
5979 if (info_ptr == NULL)
5980 return;
5981
5982 /* We can't set abfd until now because the section may be empty or
5983 not present, in which case the bfd is unknown. */
5984 abfd = section->get_bfd_owner ();
5985
5986 /* We don't use cutu_reader here because we don't need to read
5987 any dies: the signature is in the header. */
5988
5989 end_ptr = info_ptr + section->size;
5990 while (info_ptr < end_ptr)
5991 {
5992 struct signatured_type *sig_type;
5993 struct dwo_unit *dwo_tu;
5994 void **slot;
5995 const gdb_byte *ptr = info_ptr;
5996 struct comp_unit_head header;
5997 unsigned int length;
5998
5999 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6000
6001 /* Initialize it due to a false compiler warning. */
6002 header.signature = -1;
6003 header.type_cu_offset_in_tu = (cu_offset) -1;
6004
6005 /* We need to read the type's signature in order to build the hash
6006 table, but we don't need anything else just yet. */
6007
6008 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6009 abbrev_section, ptr, section_kind);
6010
6011 length = header.get_length ();
6012
6013 /* Skip dummy type units. */
6014 if (ptr >= info_ptr + length
6015 || peek_abbrev_code (abfd, ptr) == 0
6016 || header.unit_type != DW_UT_type)
6017 {
6018 info_ptr += length;
6019 continue;
6020 }
6021
6022 if (types_htab == NULL)
6023 {
6024 if (dwo_file)
6025 types_htab = allocate_dwo_unit_table ();
6026 else
6027 types_htab = allocate_signatured_type_table ();
6028 }
6029
6030 if (dwo_file)
6031 {
6032 sig_type = NULL;
6033 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6034 struct dwo_unit);
6035 dwo_tu->dwo_file = dwo_file;
6036 dwo_tu->signature = header.signature;
6037 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6038 dwo_tu->section = section;
6039 dwo_tu->sect_off = sect_off;
6040 dwo_tu->length = length;
6041 }
6042 else
6043 {
6044 /* N.B.: type_offset is not usable if this type uses a DWO file.
6045 The real type_offset is in the DWO file. */
6046 dwo_tu = NULL;
6047 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6048 struct signatured_type);
6049 sig_type->signature = header.signature;
6050 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6051 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6052 sig_type->per_cu.is_debug_types = 1;
6053 sig_type->per_cu.section = section;
6054 sig_type->per_cu.sect_off = sect_off;
6055 sig_type->per_cu.length = length;
6056 }
6057
6058 slot = htab_find_slot (types_htab.get (),
6059 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6060 INSERT);
6061 gdb_assert (slot != NULL);
6062 if (*slot != NULL)
6063 {
6064 sect_offset dup_sect_off;
6065
6066 if (dwo_file)
6067 {
6068 const struct dwo_unit *dup_tu
6069 = (const struct dwo_unit *) *slot;
6070
6071 dup_sect_off = dup_tu->sect_off;
6072 }
6073 else
6074 {
6075 const struct signatured_type *dup_tu
6076 = (const struct signatured_type *) *slot;
6077
6078 dup_sect_off = dup_tu->per_cu.sect_off;
6079 }
6080
6081 complaint (_("debug type entry at offset %s is duplicate to"
6082 " the entry at offset %s, signature %s"),
6083 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6084 hex_string (header.signature));
6085 }
6086 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6087
6088 if (dwarf_read_debug > 1)
6089 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6090 sect_offset_str (sect_off),
6091 hex_string (header.signature));
6092
6093 info_ptr += length;
6094 }
6095 }
6096
6097 /* Create the hash table of all entries in the .debug_types
6098 (or .debug_types.dwo) section(s).
6099 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6100 otherwise it is NULL.
6101
6102 The result is a pointer to the hash table or NULL if there are no types.
6103
6104 Note: This function processes DWO files only, not DWP files. */
6105
6106 static void
6107 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6108 struct dwo_file *dwo_file,
6109 gdb::array_view<dwarf2_section_info> type_sections,
6110 htab_up &types_htab)
6111 {
6112 for (dwarf2_section_info &section : type_sections)
6113 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6114 types_htab, rcuh_kind::TYPE);
6115 }
6116
6117 /* Create the hash table of all entries in the .debug_types section,
6118 and initialize all_type_units.
6119 The result is zero if there is an error (e.g. missing .debug_types section),
6120 otherwise non-zero. */
6121
6122 static int
6123 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6124 {
6125 htab_up types_htab;
6126
6127 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6128 &dwarf2_per_objfile->info, types_htab,
6129 rcuh_kind::COMPILE);
6130 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6131 dwarf2_per_objfile->types, types_htab);
6132 if (types_htab == NULL)
6133 {
6134 dwarf2_per_objfile->signatured_types = NULL;
6135 return 0;
6136 }
6137
6138 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6139
6140 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6141 dwarf2_per_objfile->all_type_units.reserve
6142 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6143
6144 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6145 add_signatured_type_cu_to_table,
6146 &dwarf2_per_objfile->all_type_units);
6147
6148 return 1;
6149 }
6150
6151 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6152 If SLOT is non-NULL, it is the entry to use in the hash table.
6153 Otherwise we find one. */
6154
6155 static struct signatured_type *
6156 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6157 void **slot)
6158 {
6159 struct objfile *objfile = dwarf2_per_objfile->objfile;
6160
6161 if (dwarf2_per_objfile->all_type_units.size ()
6162 == dwarf2_per_objfile->all_type_units.capacity ())
6163 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6164
6165 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6166 struct signatured_type);
6167
6168 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6169 sig_type->signature = sig;
6170 sig_type->per_cu.is_debug_types = 1;
6171 if (dwarf2_per_objfile->using_index)
6172 {
6173 sig_type->per_cu.v.quick =
6174 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6175 struct dwarf2_per_cu_quick_data);
6176 }
6177
6178 if (slot == NULL)
6179 {
6180 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6181 sig_type, INSERT);
6182 }
6183 gdb_assert (*slot == NULL);
6184 *slot = sig_type;
6185 /* The rest of sig_type must be filled in by the caller. */
6186 return sig_type;
6187 }
6188
6189 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6190 Fill in SIG_ENTRY with DWO_ENTRY. */
6191
6192 static void
6193 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6194 struct signatured_type *sig_entry,
6195 struct dwo_unit *dwo_entry)
6196 {
6197 /* Make sure we're not clobbering something we don't expect to. */
6198 gdb_assert (! sig_entry->per_cu.queued);
6199 gdb_assert (sig_entry->per_cu.cu == NULL);
6200 if (dwarf2_per_objfile->using_index)
6201 {
6202 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6203 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6204 }
6205 else
6206 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6207 gdb_assert (sig_entry->signature == dwo_entry->signature);
6208 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6209 gdb_assert (sig_entry->type_unit_group == NULL);
6210 gdb_assert (sig_entry->dwo_unit == NULL);
6211
6212 sig_entry->per_cu.section = dwo_entry->section;
6213 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6214 sig_entry->per_cu.length = dwo_entry->length;
6215 sig_entry->per_cu.reading_dwo_directly = 1;
6216 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6217 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6218 sig_entry->dwo_unit = dwo_entry;
6219 }
6220
6221 /* Subroutine of lookup_signatured_type.
6222 If we haven't read the TU yet, create the signatured_type data structure
6223 for a TU to be read in directly from a DWO file, bypassing the stub.
6224 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6225 using .gdb_index, then when reading a CU we want to stay in the DWO file
6226 containing that CU. Otherwise we could end up reading several other DWO
6227 files (due to comdat folding) to process the transitive closure of all the
6228 mentioned TUs, and that can be slow. The current DWO file will have every
6229 type signature that it needs.
6230 We only do this for .gdb_index because in the psymtab case we already have
6231 to read all the DWOs to build the type unit groups. */
6232
6233 static struct signatured_type *
6234 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6235 {
6236 struct dwarf2_per_objfile *dwarf2_per_objfile
6237 = cu->per_cu->dwarf2_per_objfile;
6238 struct dwo_file *dwo_file;
6239 struct dwo_unit find_dwo_entry, *dwo_entry;
6240 struct signatured_type find_sig_entry, *sig_entry;
6241 void **slot;
6242
6243 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6244
6245 /* If TU skeletons have been removed then we may not have read in any
6246 TUs yet. */
6247 if (dwarf2_per_objfile->signatured_types == NULL)
6248 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6249
6250 /* We only ever need to read in one copy of a signatured type.
6251 Use the global signatured_types array to do our own comdat-folding
6252 of types. If this is the first time we're reading this TU, and
6253 the TU has an entry in .gdb_index, replace the recorded data from
6254 .gdb_index with this TU. */
6255
6256 find_sig_entry.signature = sig;
6257 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6258 &find_sig_entry, INSERT);
6259 sig_entry = (struct signatured_type *) *slot;
6260
6261 /* We can get here with the TU already read, *or* in the process of being
6262 read. Don't reassign the global entry to point to this DWO if that's
6263 the case. Also note that if the TU is already being read, it may not
6264 have come from a DWO, the program may be a mix of Fission-compiled
6265 code and non-Fission-compiled code. */
6266
6267 /* Have we already tried to read this TU?
6268 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6269 needn't exist in the global table yet). */
6270 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6271 return sig_entry;
6272
6273 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6274 dwo_unit of the TU itself. */
6275 dwo_file = cu->dwo_unit->dwo_file;
6276
6277 /* Ok, this is the first time we're reading this TU. */
6278 if (dwo_file->tus == NULL)
6279 return NULL;
6280 find_dwo_entry.signature = sig;
6281 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6282 &find_dwo_entry);
6283 if (dwo_entry == NULL)
6284 return NULL;
6285
6286 /* If the global table doesn't have an entry for this TU, add one. */
6287 if (sig_entry == NULL)
6288 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6289
6290 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6291 sig_entry->per_cu.tu_read = 1;
6292 return sig_entry;
6293 }
6294
6295 /* Subroutine of lookup_signatured_type.
6296 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6297 then try the DWP file. If the TU stub (skeleton) has been removed then
6298 it won't be in .gdb_index. */
6299
6300 static struct signatured_type *
6301 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6302 {
6303 struct dwarf2_per_objfile *dwarf2_per_objfile
6304 = cu->per_cu->dwarf2_per_objfile;
6305 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6306 struct dwo_unit *dwo_entry;
6307 struct signatured_type find_sig_entry, *sig_entry;
6308 void **slot;
6309
6310 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6311 gdb_assert (dwp_file != NULL);
6312
6313 /* If TU skeletons have been removed then we may not have read in any
6314 TUs yet. */
6315 if (dwarf2_per_objfile->signatured_types == NULL)
6316 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6317
6318 find_sig_entry.signature = sig;
6319 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6320 &find_sig_entry, INSERT);
6321 sig_entry = (struct signatured_type *) *slot;
6322
6323 /* Have we already tried to read this TU?
6324 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6325 needn't exist in the global table yet). */
6326 if (sig_entry != NULL)
6327 return sig_entry;
6328
6329 if (dwp_file->tus == NULL)
6330 return NULL;
6331 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6332 sig, 1 /* is_debug_types */);
6333 if (dwo_entry == NULL)
6334 return NULL;
6335
6336 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6337 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6338
6339 return sig_entry;
6340 }
6341
6342 /* Lookup a signature based type for DW_FORM_ref_sig8.
6343 Returns NULL if signature SIG is not present in the table.
6344 It is up to the caller to complain about this. */
6345
6346 static struct signatured_type *
6347 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6348 {
6349 struct dwarf2_per_objfile *dwarf2_per_objfile
6350 = cu->per_cu->dwarf2_per_objfile;
6351
6352 if (cu->dwo_unit
6353 && dwarf2_per_objfile->using_index)
6354 {
6355 /* We're in a DWO/DWP file, and we're using .gdb_index.
6356 These cases require special processing. */
6357 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6358 return lookup_dwo_signatured_type (cu, sig);
6359 else
6360 return lookup_dwp_signatured_type (cu, sig);
6361 }
6362 else
6363 {
6364 struct signatured_type find_entry, *entry;
6365
6366 if (dwarf2_per_objfile->signatured_types == NULL)
6367 return NULL;
6368 find_entry.signature = sig;
6369 entry = ((struct signatured_type *)
6370 htab_find (dwarf2_per_objfile->signatured_types.get (),
6371 &find_entry));
6372 return entry;
6373 }
6374 }
6375
6376 /* Low level DIE reading support. */
6377
6378 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6379
6380 static void
6381 init_cu_die_reader (struct die_reader_specs *reader,
6382 struct dwarf2_cu *cu,
6383 struct dwarf2_section_info *section,
6384 struct dwo_file *dwo_file,
6385 struct abbrev_table *abbrev_table)
6386 {
6387 gdb_assert (section->readin && section->buffer != NULL);
6388 reader->abfd = section->get_bfd_owner ();
6389 reader->cu = cu;
6390 reader->dwo_file = dwo_file;
6391 reader->die_section = section;
6392 reader->buffer = section->buffer;
6393 reader->buffer_end = section->buffer + section->size;
6394 reader->abbrev_table = abbrev_table;
6395 }
6396
6397 /* Subroutine of cutu_reader to simplify it.
6398 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6399 There's just a lot of work to do, and cutu_reader is big enough
6400 already.
6401
6402 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6403 from it to the DIE in the DWO. If NULL we are skipping the stub.
6404 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6405 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6406 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6407 STUB_COMP_DIR may be non-NULL.
6408 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6409 are filled in with the info of the DIE from the DWO file.
6410 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6411 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6412 kept around for at least as long as *RESULT_READER.
6413
6414 The result is non-zero if a valid (non-dummy) DIE was found. */
6415
6416 static int
6417 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6418 struct dwo_unit *dwo_unit,
6419 struct die_info *stub_comp_unit_die,
6420 const char *stub_comp_dir,
6421 struct die_reader_specs *result_reader,
6422 const gdb_byte **result_info_ptr,
6423 struct die_info **result_comp_unit_die,
6424 abbrev_table_up *result_dwo_abbrev_table)
6425 {
6426 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6427 struct objfile *objfile = dwarf2_per_objfile->objfile;
6428 struct dwarf2_cu *cu = this_cu->cu;
6429 bfd *abfd;
6430 const gdb_byte *begin_info_ptr, *info_ptr;
6431 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6432 int i,num_extra_attrs;
6433 struct dwarf2_section_info *dwo_abbrev_section;
6434 struct die_info *comp_unit_die;
6435
6436 /* At most one of these may be provided. */
6437 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6438
6439 /* These attributes aren't processed until later:
6440 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6441 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6442 referenced later. However, these attributes are found in the stub
6443 which we won't have later. In order to not impose this complication
6444 on the rest of the code, we read them here and copy them to the
6445 DWO CU/TU die. */
6446
6447 stmt_list = NULL;
6448 low_pc = NULL;
6449 high_pc = NULL;
6450 ranges = NULL;
6451 comp_dir = NULL;
6452
6453 if (stub_comp_unit_die != NULL)
6454 {
6455 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6456 DWO file. */
6457 if (! this_cu->is_debug_types)
6458 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6459 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6460 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6461 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6462 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6463
6464 cu->addr_base = stub_comp_unit_die->addr_base ();
6465
6466 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6467 here (if needed). We need the value before we can process
6468 DW_AT_ranges. */
6469 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6470 }
6471 else if (stub_comp_dir != NULL)
6472 {
6473 /* Reconstruct the comp_dir attribute to simplify the code below. */
6474 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6475 comp_dir->name = DW_AT_comp_dir;
6476 comp_dir->form = DW_FORM_string;
6477 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6478 DW_STRING (comp_dir) = stub_comp_dir;
6479 }
6480
6481 /* Set up for reading the DWO CU/TU. */
6482 cu->dwo_unit = dwo_unit;
6483 dwarf2_section_info *section = dwo_unit->section;
6484 section->read (objfile);
6485 abfd = section->get_bfd_owner ();
6486 begin_info_ptr = info_ptr = (section->buffer
6487 + to_underlying (dwo_unit->sect_off));
6488 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6489
6490 if (this_cu->is_debug_types)
6491 {
6492 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6493
6494 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6495 &cu->header, section,
6496 dwo_abbrev_section,
6497 info_ptr, rcuh_kind::TYPE);
6498 /* This is not an assert because it can be caused by bad debug info. */
6499 if (sig_type->signature != cu->header.signature)
6500 {
6501 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6502 " TU at offset %s [in module %s]"),
6503 hex_string (sig_type->signature),
6504 hex_string (cu->header.signature),
6505 sect_offset_str (dwo_unit->sect_off),
6506 bfd_get_filename (abfd));
6507 }
6508 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6509 /* For DWOs coming from DWP files, we don't know the CU length
6510 nor the type's offset in the TU until now. */
6511 dwo_unit->length = cu->header.get_length ();
6512 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6513
6514 /* Establish the type offset that can be used to lookup the type.
6515 For DWO files, we don't know it until now. */
6516 sig_type->type_offset_in_section
6517 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6518 }
6519 else
6520 {
6521 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6522 &cu->header, section,
6523 dwo_abbrev_section,
6524 info_ptr, rcuh_kind::COMPILE);
6525 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6526 /* For DWOs coming from DWP files, we don't know the CU length
6527 until now. */
6528 dwo_unit->length = cu->header.get_length ();
6529 }
6530
6531 *result_dwo_abbrev_table
6532 = abbrev_table::read (objfile, dwo_abbrev_section,
6533 cu->header.abbrev_sect_off);
6534 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6535 result_dwo_abbrev_table->get ());
6536
6537 /* Read in the die, but leave space to copy over the attributes
6538 from the stub. This has the benefit of simplifying the rest of
6539 the code - all the work to maintain the illusion of a single
6540 DW_TAG_{compile,type}_unit DIE is done here. */
6541 num_extra_attrs = ((stmt_list != NULL)
6542 + (low_pc != NULL)
6543 + (high_pc != NULL)
6544 + (ranges != NULL)
6545 + (comp_dir != NULL));
6546 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6547 num_extra_attrs);
6548
6549 /* Copy over the attributes from the stub to the DIE we just read in. */
6550 comp_unit_die = *result_comp_unit_die;
6551 i = comp_unit_die->num_attrs;
6552 if (stmt_list != NULL)
6553 comp_unit_die->attrs[i++] = *stmt_list;
6554 if (low_pc != NULL)
6555 comp_unit_die->attrs[i++] = *low_pc;
6556 if (high_pc != NULL)
6557 comp_unit_die->attrs[i++] = *high_pc;
6558 if (ranges != NULL)
6559 comp_unit_die->attrs[i++] = *ranges;
6560 if (comp_dir != NULL)
6561 comp_unit_die->attrs[i++] = *comp_dir;
6562 comp_unit_die->num_attrs += num_extra_attrs;
6563
6564 if (dwarf_die_debug)
6565 {
6566 fprintf_unfiltered (gdb_stdlog,
6567 "Read die from %s@0x%x of %s:\n",
6568 section->get_name (),
6569 (unsigned) (begin_info_ptr - section->buffer),
6570 bfd_get_filename (abfd));
6571 dump_die (comp_unit_die, dwarf_die_debug);
6572 }
6573
6574 /* Skip dummy compilation units. */
6575 if (info_ptr >= begin_info_ptr + dwo_unit->length
6576 || peek_abbrev_code (abfd, info_ptr) == 0)
6577 return 0;
6578
6579 *result_info_ptr = info_ptr;
6580 return 1;
6581 }
6582
6583 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6584 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6585 signature is part of the header. */
6586 static gdb::optional<ULONGEST>
6587 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6588 {
6589 if (cu->header.version >= 5)
6590 return cu->header.signature;
6591 struct attribute *attr;
6592 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6593 if (attr == nullptr)
6594 return gdb::optional<ULONGEST> ();
6595 return DW_UNSND (attr);
6596 }
6597
6598 /* Subroutine of cutu_reader to simplify it.
6599 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6600 Returns NULL if the specified DWO unit cannot be found. */
6601
6602 static struct dwo_unit *
6603 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6604 struct die_info *comp_unit_die,
6605 const char *dwo_name)
6606 {
6607 struct dwarf2_cu *cu = this_cu->cu;
6608 struct dwo_unit *dwo_unit;
6609 const char *comp_dir;
6610
6611 gdb_assert (cu != NULL);
6612
6613 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6614 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6615 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6616
6617 if (this_cu->is_debug_types)
6618 {
6619 struct signatured_type *sig_type;
6620
6621 /* Since this_cu is the first member of struct signatured_type,
6622 we can go from a pointer to one to a pointer to the other. */
6623 sig_type = (struct signatured_type *) this_cu;
6624 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6625 }
6626 else
6627 {
6628 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6629 if (!signature.has_value ())
6630 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6631 " [in module %s]"),
6632 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6633 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6634 *signature);
6635 }
6636
6637 return dwo_unit;
6638 }
6639
6640 /* Subroutine of cutu_reader to simplify it.
6641 See it for a description of the parameters.
6642 Read a TU directly from a DWO file, bypassing the stub. */
6643
6644 void
6645 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6646 int use_existing_cu)
6647 {
6648 struct signatured_type *sig_type;
6649
6650 /* Verify we can do the following downcast, and that we have the
6651 data we need. */
6652 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6653 sig_type = (struct signatured_type *) this_cu;
6654 gdb_assert (sig_type->dwo_unit != NULL);
6655
6656 if (use_existing_cu && this_cu->cu != NULL)
6657 {
6658 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6659 /* There's no need to do the rereading_dwo_cu handling that
6660 cutu_reader does since we don't read the stub. */
6661 }
6662 else
6663 {
6664 /* If !use_existing_cu, this_cu->cu must be NULL. */
6665 gdb_assert (this_cu->cu == NULL);
6666 m_new_cu.reset (new dwarf2_cu (this_cu));
6667 }
6668
6669 /* A future optimization, if needed, would be to use an existing
6670 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6671 could share abbrev tables. */
6672
6673 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6674 NULL /* stub_comp_unit_die */,
6675 sig_type->dwo_unit->dwo_file->comp_dir,
6676 this, &info_ptr,
6677 &comp_unit_die,
6678 &m_dwo_abbrev_table) == 0)
6679 {
6680 /* Dummy die. */
6681 dummy_p = true;
6682 }
6683 }
6684
6685 /* Initialize a CU (or TU) and read its DIEs.
6686 If the CU defers to a DWO file, read the DWO file as well.
6687
6688 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6689 Otherwise the table specified in the comp unit header is read in and used.
6690 This is an optimization for when we already have the abbrev table.
6691
6692 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6693 Otherwise, a new CU is allocated with xmalloc. */
6694
6695 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6696 struct abbrev_table *abbrev_table,
6697 int use_existing_cu,
6698 bool skip_partial)
6699 : die_reader_specs {},
6700 m_this_cu (this_cu)
6701 {
6702 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6703 struct objfile *objfile = dwarf2_per_objfile->objfile;
6704 struct dwarf2_section_info *section = this_cu->section;
6705 bfd *abfd = section->get_bfd_owner ();
6706 struct dwarf2_cu *cu;
6707 const gdb_byte *begin_info_ptr;
6708 struct signatured_type *sig_type = NULL;
6709 struct dwarf2_section_info *abbrev_section;
6710 /* Non-zero if CU currently points to a DWO file and we need to
6711 reread it. When this happens we need to reread the skeleton die
6712 before we can reread the DWO file (this only applies to CUs, not TUs). */
6713 int rereading_dwo_cu = 0;
6714
6715 if (dwarf_die_debug)
6716 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6717 this_cu->is_debug_types ? "type" : "comp",
6718 sect_offset_str (this_cu->sect_off));
6719
6720 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6721 file (instead of going through the stub), short-circuit all of this. */
6722 if (this_cu->reading_dwo_directly)
6723 {
6724 /* Narrow down the scope of possibilities to have to understand. */
6725 gdb_assert (this_cu->is_debug_types);
6726 gdb_assert (abbrev_table == NULL);
6727 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6728 return;
6729 }
6730
6731 /* This is cheap if the section is already read in. */
6732 section->read (objfile);
6733
6734 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6735
6736 abbrev_section = get_abbrev_section_for_cu (this_cu);
6737
6738 if (use_existing_cu && this_cu->cu != NULL)
6739 {
6740 cu = this_cu->cu;
6741 /* If this CU is from a DWO file we need to start over, we need to
6742 refetch the attributes from the skeleton CU.
6743 This could be optimized by retrieving those attributes from when we
6744 were here the first time: the previous comp_unit_die was stored in
6745 comp_unit_obstack. But there's no data yet that we need this
6746 optimization. */
6747 if (cu->dwo_unit != NULL)
6748 rereading_dwo_cu = 1;
6749 }
6750 else
6751 {
6752 /* If !use_existing_cu, this_cu->cu must be NULL. */
6753 gdb_assert (this_cu->cu == NULL);
6754 m_new_cu.reset (new dwarf2_cu (this_cu));
6755 cu = m_new_cu.get ();
6756 }
6757
6758 /* Get the header. */
6759 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6760 {
6761 /* We already have the header, there's no need to read it in again. */
6762 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6763 }
6764 else
6765 {
6766 if (this_cu->is_debug_types)
6767 {
6768 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6769 &cu->header, section,
6770 abbrev_section, info_ptr,
6771 rcuh_kind::TYPE);
6772
6773 /* Since per_cu is the first member of struct signatured_type,
6774 we can go from a pointer to one to a pointer to the other. */
6775 sig_type = (struct signatured_type *) this_cu;
6776 gdb_assert (sig_type->signature == cu->header.signature);
6777 gdb_assert (sig_type->type_offset_in_tu
6778 == cu->header.type_cu_offset_in_tu);
6779 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6780
6781 /* LENGTH has not been set yet for type units if we're
6782 using .gdb_index. */
6783 this_cu->length = cu->header.get_length ();
6784
6785 /* Establish the type offset that can be used to lookup the type. */
6786 sig_type->type_offset_in_section =
6787 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6788
6789 this_cu->dwarf_version = cu->header.version;
6790 }
6791 else
6792 {
6793 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6794 &cu->header, section,
6795 abbrev_section,
6796 info_ptr,
6797 rcuh_kind::COMPILE);
6798
6799 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6800 gdb_assert (this_cu->length == cu->header.get_length ());
6801 this_cu->dwarf_version = cu->header.version;
6802 }
6803 }
6804
6805 /* Skip dummy compilation units. */
6806 if (info_ptr >= begin_info_ptr + this_cu->length
6807 || peek_abbrev_code (abfd, info_ptr) == 0)
6808 {
6809 dummy_p = true;
6810 return;
6811 }
6812
6813 /* If we don't have them yet, read the abbrevs for this compilation unit.
6814 And if we need to read them now, make sure they're freed when we're
6815 done. */
6816 if (abbrev_table != NULL)
6817 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6818 else
6819 {
6820 m_abbrev_table_holder
6821 = abbrev_table::read (objfile, abbrev_section,
6822 cu->header.abbrev_sect_off);
6823 abbrev_table = m_abbrev_table_holder.get ();
6824 }
6825
6826 /* Read the top level CU/TU die. */
6827 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6828 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6829
6830 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6831 {
6832 dummy_p = true;
6833 return;
6834 }
6835
6836 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6837 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6838 table from the DWO file and pass the ownership over to us. It will be
6839 referenced from READER, so we must make sure to free it after we're done
6840 with READER.
6841
6842 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6843 DWO CU, that this test will fail (the attribute will not be present). */
6844 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6845 if (dwo_name != nullptr)
6846 {
6847 struct dwo_unit *dwo_unit;
6848 struct die_info *dwo_comp_unit_die;
6849
6850 if (comp_unit_die->has_children)
6851 {
6852 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6853 " has children (offset %s) [in module %s]"),
6854 sect_offset_str (this_cu->sect_off),
6855 bfd_get_filename (abfd));
6856 }
6857 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6858 if (dwo_unit != NULL)
6859 {
6860 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6861 comp_unit_die, NULL,
6862 this, &info_ptr,
6863 &dwo_comp_unit_die,
6864 &m_dwo_abbrev_table) == 0)
6865 {
6866 /* Dummy die. */
6867 dummy_p = true;
6868 return;
6869 }
6870 comp_unit_die = dwo_comp_unit_die;
6871 }
6872 else
6873 {
6874 /* Yikes, we couldn't find the rest of the DIE, we only have
6875 the stub. A complaint has already been logged. There's
6876 not much more we can do except pass on the stub DIE to
6877 die_reader_func. We don't want to throw an error on bad
6878 debug info. */
6879 }
6880 }
6881 }
6882
6883 void
6884 cutu_reader::keep ()
6885 {
6886 /* Done, clean up. */
6887 gdb_assert (!dummy_p);
6888 if (m_new_cu != NULL)
6889 {
6890 struct dwarf2_per_objfile *dwarf2_per_objfile
6891 = m_this_cu->dwarf2_per_objfile;
6892 /* Link this CU into read_in_chain. */
6893 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6894 dwarf2_per_objfile->read_in_chain = m_this_cu;
6895 /* The chain owns it now. */
6896 m_new_cu.release ();
6897 }
6898 }
6899
6900 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6901 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6902 assumed to have already done the lookup to find the DWO file).
6903
6904 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6905 THIS_CU->is_debug_types, but nothing else.
6906
6907 We fill in THIS_CU->length.
6908
6909 THIS_CU->cu is always freed when done.
6910 This is done in order to not leave THIS_CU->cu in a state where we have
6911 to care whether it refers to the "main" CU or the DWO CU.
6912
6913 When parent_cu is passed, it is used to provide a default value for
6914 str_offsets_base and addr_base from the parent. */
6915
6916 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6917 struct dwarf2_cu *parent_cu,
6918 struct dwo_file *dwo_file)
6919 : die_reader_specs {},
6920 m_this_cu (this_cu)
6921 {
6922 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6923 struct objfile *objfile = dwarf2_per_objfile->objfile;
6924 struct dwarf2_section_info *section = this_cu->section;
6925 bfd *abfd = section->get_bfd_owner ();
6926 struct dwarf2_section_info *abbrev_section;
6927 const gdb_byte *begin_info_ptr, *info_ptr;
6928
6929 if (dwarf_die_debug)
6930 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6931 this_cu->is_debug_types ? "type" : "comp",
6932 sect_offset_str (this_cu->sect_off));
6933
6934 gdb_assert (this_cu->cu == NULL);
6935
6936 abbrev_section = (dwo_file != NULL
6937 ? &dwo_file->sections.abbrev
6938 : get_abbrev_section_for_cu (this_cu));
6939
6940 /* This is cheap if the section is already read in. */
6941 section->read (objfile);
6942
6943 m_new_cu.reset (new dwarf2_cu (this_cu));
6944
6945 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6946 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6947 &m_new_cu->header, section,
6948 abbrev_section, info_ptr,
6949 (this_cu->is_debug_types
6950 ? rcuh_kind::TYPE
6951 : rcuh_kind::COMPILE));
6952
6953 if (parent_cu != nullptr)
6954 {
6955 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6956 m_new_cu->addr_base = parent_cu->addr_base;
6957 }
6958 this_cu->length = m_new_cu->header.get_length ();
6959
6960 /* Skip dummy compilation units. */
6961 if (info_ptr >= begin_info_ptr + this_cu->length
6962 || peek_abbrev_code (abfd, info_ptr) == 0)
6963 {
6964 dummy_p = true;
6965 return;
6966 }
6967
6968 m_abbrev_table_holder
6969 = abbrev_table::read (objfile, abbrev_section,
6970 m_new_cu->header.abbrev_sect_off);
6971
6972 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6973 m_abbrev_table_holder.get ());
6974 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6975 }
6976
6977 \f
6978 /* Type Unit Groups.
6979
6980 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6981 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6982 so that all types coming from the same compilation (.o file) are grouped
6983 together. A future step could be to put the types in the same symtab as
6984 the CU the types ultimately came from. */
6985
6986 static hashval_t
6987 hash_type_unit_group (const void *item)
6988 {
6989 const struct type_unit_group *tu_group
6990 = (const struct type_unit_group *) item;
6991
6992 return hash_stmt_list_entry (&tu_group->hash);
6993 }
6994
6995 static int
6996 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6997 {
6998 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6999 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7000
7001 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7002 }
7003
7004 /* Allocate a hash table for type unit groups. */
7005
7006 static htab_up
7007 allocate_type_unit_groups_table ()
7008 {
7009 return htab_up (htab_create_alloc (3,
7010 hash_type_unit_group,
7011 eq_type_unit_group,
7012 NULL, xcalloc, xfree));
7013 }
7014
7015 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7016 partial symtabs. We combine several TUs per psymtab to not let the size
7017 of any one psymtab grow too big. */
7018 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7019 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7020
7021 /* Helper routine for get_type_unit_group.
7022 Create the type_unit_group object used to hold one or more TUs. */
7023
7024 static struct type_unit_group *
7025 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7026 {
7027 struct dwarf2_per_objfile *dwarf2_per_objfile
7028 = cu->per_cu->dwarf2_per_objfile;
7029 struct objfile *objfile = dwarf2_per_objfile->objfile;
7030 struct dwarf2_per_cu_data *per_cu;
7031 struct type_unit_group *tu_group;
7032
7033 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7034 struct type_unit_group);
7035 per_cu = &tu_group->per_cu;
7036 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7037
7038 if (dwarf2_per_objfile->using_index)
7039 {
7040 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7041 struct dwarf2_per_cu_quick_data);
7042 }
7043 else
7044 {
7045 unsigned int line_offset = to_underlying (line_offset_struct);
7046 dwarf2_psymtab *pst;
7047 std::string name;
7048
7049 /* Give the symtab a useful name for debug purposes. */
7050 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7051 name = string_printf ("<type_units_%d>",
7052 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7053 else
7054 name = string_printf ("<type_units_at_0x%x>", line_offset);
7055
7056 pst = create_partial_symtab (per_cu, name.c_str ());
7057 pst->anonymous = true;
7058 }
7059
7060 tu_group->hash.dwo_unit = cu->dwo_unit;
7061 tu_group->hash.line_sect_off = line_offset_struct;
7062
7063 return tu_group;
7064 }
7065
7066 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7067 STMT_LIST is a DW_AT_stmt_list attribute. */
7068
7069 static struct type_unit_group *
7070 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7071 {
7072 struct dwarf2_per_objfile *dwarf2_per_objfile
7073 = cu->per_cu->dwarf2_per_objfile;
7074 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7075 struct type_unit_group *tu_group;
7076 void **slot;
7077 unsigned int line_offset;
7078 struct type_unit_group type_unit_group_for_lookup;
7079
7080 if (dwarf2_per_objfile->type_unit_groups == NULL)
7081 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7082
7083 /* Do we need to create a new group, or can we use an existing one? */
7084
7085 if (stmt_list)
7086 {
7087 line_offset = DW_UNSND (stmt_list);
7088 ++tu_stats->nr_symtab_sharers;
7089 }
7090 else
7091 {
7092 /* Ugh, no stmt_list. Rare, but we have to handle it.
7093 We can do various things here like create one group per TU or
7094 spread them over multiple groups to split up the expansion work.
7095 To avoid worst case scenarios (too many groups or too large groups)
7096 we, umm, group them in bunches. */
7097 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7098 | (tu_stats->nr_stmt_less_type_units
7099 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7100 ++tu_stats->nr_stmt_less_type_units;
7101 }
7102
7103 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7104 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7105 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7106 &type_unit_group_for_lookup, INSERT);
7107 if (*slot != NULL)
7108 {
7109 tu_group = (struct type_unit_group *) *slot;
7110 gdb_assert (tu_group != NULL);
7111 }
7112 else
7113 {
7114 sect_offset line_offset_struct = (sect_offset) line_offset;
7115 tu_group = create_type_unit_group (cu, line_offset_struct);
7116 *slot = tu_group;
7117 ++tu_stats->nr_symtabs;
7118 }
7119
7120 return tu_group;
7121 }
7122 \f
7123 /* Partial symbol tables. */
7124
7125 /* Create a psymtab named NAME and assign it to PER_CU.
7126
7127 The caller must fill in the following details:
7128 dirname, textlow, texthigh. */
7129
7130 static dwarf2_psymtab *
7131 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7132 {
7133 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7134 dwarf2_psymtab *pst;
7135
7136 pst = new dwarf2_psymtab (name, objfile, 0);
7137
7138 pst->psymtabs_addrmap_supported = true;
7139
7140 /* This is the glue that links PST into GDB's symbol API. */
7141 pst->per_cu_data = per_cu;
7142 per_cu->v.psymtab = pst;
7143
7144 return pst;
7145 }
7146
7147 /* DIE reader function for process_psymtab_comp_unit. */
7148
7149 static void
7150 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7151 const gdb_byte *info_ptr,
7152 struct die_info *comp_unit_die,
7153 enum language pretend_language)
7154 {
7155 struct dwarf2_cu *cu = reader->cu;
7156 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7158 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7159 CORE_ADDR baseaddr;
7160 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7161 dwarf2_psymtab *pst;
7162 enum pc_bounds_kind cu_bounds_kind;
7163 const char *filename;
7164
7165 gdb_assert (! per_cu->is_debug_types);
7166
7167 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7168
7169 /* Allocate a new partial symbol table structure. */
7170 gdb::unique_xmalloc_ptr<char> debug_filename;
7171 static const char artificial[] = "<artificial>";
7172 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7173 if (filename == NULL)
7174 filename = "";
7175 else if (strcmp (filename, artificial) == 0)
7176 {
7177 debug_filename.reset (concat (artificial, "@",
7178 sect_offset_str (per_cu->sect_off),
7179 (char *) NULL));
7180 filename = debug_filename.get ();
7181 }
7182
7183 pst = create_partial_symtab (per_cu, filename);
7184
7185 /* This must be done before calling dwarf2_build_include_psymtabs. */
7186 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7187
7188 baseaddr = objfile->text_section_offset ();
7189
7190 dwarf2_find_base_address (comp_unit_die, cu);
7191
7192 /* Possibly set the default values of LOWPC and HIGHPC from
7193 `DW_AT_ranges'. */
7194 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7195 &best_highpc, cu, pst);
7196 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7197 {
7198 CORE_ADDR low
7199 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7200 - baseaddr);
7201 CORE_ADDR high
7202 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7203 - baseaddr - 1);
7204 /* Store the contiguous range if it is not empty; it can be
7205 empty for CUs with no code. */
7206 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7207 low, high, pst);
7208 }
7209
7210 /* Check if comp unit has_children.
7211 If so, read the rest of the partial symbols from this comp unit.
7212 If not, there's no more debug_info for this comp unit. */
7213 if (comp_unit_die->has_children)
7214 {
7215 struct partial_die_info *first_die;
7216 CORE_ADDR lowpc, highpc;
7217
7218 lowpc = ((CORE_ADDR) -1);
7219 highpc = ((CORE_ADDR) 0);
7220
7221 first_die = load_partial_dies (reader, info_ptr, 1);
7222
7223 scan_partial_symbols (first_die, &lowpc, &highpc,
7224 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7225
7226 /* If we didn't find a lowpc, set it to highpc to avoid
7227 complaints from `maint check'. */
7228 if (lowpc == ((CORE_ADDR) -1))
7229 lowpc = highpc;
7230
7231 /* If the compilation unit didn't have an explicit address range,
7232 then use the information extracted from its child dies. */
7233 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7234 {
7235 best_lowpc = lowpc;
7236 best_highpc = highpc;
7237 }
7238 }
7239 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7240 best_lowpc + baseaddr)
7241 - baseaddr);
7242 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7243 best_highpc + baseaddr)
7244 - baseaddr);
7245
7246 end_psymtab_common (objfile, pst);
7247
7248 if (!cu->per_cu->imported_symtabs_empty ())
7249 {
7250 int i;
7251 int len = cu->per_cu->imported_symtabs_size ();
7252
7253 /* Fill in 'dependencies' here; we fill in 'users' in a
7254 post-pass. */
7255 pst->number_of_dependencies = len;
7256 pst->dependencies
7257 = objfile->partial_symtabs->allocate_dependencies (len);
7258 for (i = 0; i < len; ++i)
7259 {
7260 pst->dependencies[i]
7261 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7262 }
7263
7264 cu->per_cu->imported_symtabs_free ();
7265 }
7266
7267 /* Get the list of files included in the current compilation unit,
7268 and build a psymtab for each of them. */
7269 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7270
7271 if (dwarf_read_debug)
7272 fprintf_unfiltered (gdb_stdlog,
7273 "Psymtab for %s unit @%s: %s - %s"
7274 ", %d global, %d static syms\n",
7275 per_cu->is_debug_types ? "type" : "comp",
7276 sect_offset_str (per_cu->sect_off),
7277 paddress (gdbarch, pst->text_low (objfile)),
7278 paddress (gdbarch, pst->text_high (objfile)),
7279 pst->n_global_syms, pst->n_static_syms);
7280 }
7281
7282 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7283 Process compilation unit THIS_CU for a psymtab. */
7284
7285 static void
7286 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7287 bool want_partial_unit,
7288 enum language pretend_language)
7289 {
7290 /* If this compilation unit was already read in, free the
7291 cached copy in order to read it in again. This is
7292 necessary because we skipped some symbols when we first
7293 read in the compilation unit (see load_partial_dies).
7294 This problem could be avoided, but the benefit is unclear. */
7295 if (this_cu->cu != NULL)
7296 free_one_cached_comp_unit (this_cu);
7297
7298 cutu_reader reader (this_cu, NULL, 0, false);
7299
7300 switch (reader.comp_unit_die->tag)
7301 {
7302 case DW_TAG_compile_unit:
7303 this_cu->unit_type = DW_UT_compile;
7304 break;
7305 case DW_TAG_partial_unit:
7306 this_cu->unit_type = DW_UT_partial;
7307 break;
7308 default:
7309 abort ();
7310 }
7311
7312 if (reader.dummy_p)
7313 {
7314 /* Nothing. */
7315 }
7316 else if (this_cu->is_debug_types)
7317 build_type_psymtabs_reader (&reader, reader.info_ptr,
7318 reader.comp_unit_die);
7319 else if (want_partial_unit
7320 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7321 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7322 reader.comp_unit_die,
7323 pretend_language);
7324
7325 this_cu->lang = this_cu->cu->language;
7326
7327 /* Age out any secondary CUs. */
7328 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7329 }
7330
7331 /* Reader function for build_type_psymtabs. */
7332
7333 static void
7334 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7335 const gdb_byte *info_ptr,
7336 struct die_info *type_unit_die)
7337 {
7338 struct dwarf2_per_objfile *dwarf2_per_objfile
7339 = reader->cu->per_cu->dwarf2_per_objfile;
7340 struct objfile *objfile = dwarf2_per_objfile->objfile;
7341 struct dwarf2_cu *cu = reader->cu;
7342 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7343 struct signatured_type *sig_type;
7344 struct type_unit_group *tu_group;
7345 struct attribute *attr;
7346 struct partial_die_info *first_die;
7347 CORE_ADDR lowpc, highpc;
7348 dwarf2_psymtab *pst;
7349
7350 gdb_assert (per_cu->is_debug_types);
7351 sig_type = (struct signatured_type *) per_cu;
7352
7353 if (! type_unit_die->has_children)
7354 return;
7355
7356 attr = type_unit_die->attr (DW_AT_stmt_list);
7357 tu_group = get_type_unit_group (cu, attr);
7358
7359 if (tu_group->tus == nullptr)
7360 tu_group->tus = new std::vector<signatured_type *>;
7361 tu_group->tus->push_back (sig_type);
7362
7363 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7364 pst = create_partial_symtab (per_cu, "");
7365 pst->anonymous = true;
7366
7367 first_die = load_partial_dies (reader, info_ptr, 1);
7368
7369 lowpc = (CORE_ADDR) -1;
7370 highpc = (CORE_ADDR) 0;
7371 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7372
7373 end_psymtab_common (objfile, pst);
7374 }
7375
7376 /* Struct used to sort TUs by their abbreviation table offset. */
7377
7378 struct tu_abbrev_offset
7379 {
7380 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7381 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7382 {}
7383
7384 signatured_type *sig_type;
7385 sect_offset abbrev_offset;
7386 };
7387
7388 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7389
7390 static bool
7391 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7392 const struct tu_abbrev_offset &b)
7393 {
7394 return a.abbrev_offset < b.abbrev_offset;
7395 }
7396
7397 /* Efficiently read all the type units.
7398 This does the bulk of the work for build_type_psymtabs.
7399
7400 The efficiency is because we sort TUs by the abbrev table they use and
7401 only read each abbrev table once. In one program there are 200K TUs
7402 sharing 8K abbrev tables.
7403
7404 The main purpose of this function is to support building the
7405 dwarf2_per_objfile->type_unit_groups table.
7406 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7407 can collapse the search space by grouping them by stmt_list.
7408 The savings can be significant, in the same program from above the 200K TUs
7409 share 8K stmt_list tables.
7410
7411 FUNC is expected to call get_type_unit_group, which will create the
7412 struct type_unit_group if necessary and add it to
7413 dwarf2_per_objfile->type_unit_groups. */
7414
7415 static void
7416 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7417 {
7418 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7419 abbrev_table_up abbrev_table;
7420 sect_offset abbrev_offset;
7421
7422 /* It's up to the caller to not call us multiple times. */
7423 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7424
7425 if (dwarf2_per_objfile->all_type_units.empty ())
7426 return;
7427
7428 /* TUs typically share abbrev tables, and there can be way more TUs than
7429 abbrev tables. Sort by abbrev table to reduce the number of times we
7430 read each abbrev table in.
7431 Alternatives are to punt or to maintain a cache of abbrev tables.
7432 This is simpler and efficient enough for now.
7433
7434 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7435 symtab to use). Typically TUs with the same abbrev offset have the same
7436 stmt_list value too so in practice this should work well.
7437
7438 The basic algorithm here is:
7439
7440 sort TUs by abbrev table
7441 for each TU with same abbrev table:
7442 read abbrev table if first user
7443 read TU top level DIE
7444 [IWBN if DWO skeletons had DW_AT_stmt_list]
7445 call FUNC */
7446
7447 if (dwarf_read_debug)
7448 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7449
7450 /* Sort in a separate table to maintain the order of all_type_units
7451 for .gdb_index: TU indices directly index all_type_units. */
7452 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7453 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7454
7455 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7456 sorted_by_abbrev.emplace_back
7457 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7458 sig_type->per_cu.section,
7459 sig_type->per_cu.sect_off));
7460
7461 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7462 sort_tu_by_abbrev_offset);
7463
7464 abbrev_offset = (sect_offset) ~(unsigned) 0;
7465
7466 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7467 {
7468 /* Switch to the next abbrev table if necessary. */
7469 if (abbrev_table == NULL
7470 || tu.abbrev_offset != abbrev_offset)
7471 {
7472 abbrev_offset = tu.abbrev_offset;
7473 abbrev_table =
7474 abbrev_table::read (dwarf2_per_objfile->objfile,
7475 &dwarf2_per_objfile->abbrev,
7476 abbrev_offset);
7477 ++tu_stats->nr_uniq_abbrev_tables;
7478 }
7479
7480 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7481 0, false);
7482 if (!reader.dummy_p)
7483 build_type_psymtabs_reader (&reader, reader.info_ptr,
7484 reader.comp_unit_die);
7485 }
7486 }
7487
7488 /* Print collected type unit statistics. */
7489
7490 static void
7491 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7492 {
7493 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7494
7495 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7496 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7497 dwarf2_per_objfile->all_type_units.size ());
7498 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7499 tu_stats->nr_uniq_abbrev_tables);
7500 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7501 tu_stats->nr_symtabs);
7502 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7503 tu_stats->nr_symtab_sharers);
7504 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7505 tu_stats->nr_stmt_less_type_units);
7506 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7507 tu_stats->nr_all_type_units_reallocs);
7508 }
7509
7510 /* Traversal function for build_type_psymtabs. */
7511
7512 static int
7513 build_type_psymtab_dependencies (void **slot, void *info)
7514 {
7515 struct dwarf2_per_objfile *dwarf2_per_objfile
7516 = (struct dwarf2_per_objfile *) info;
7517 struct objfile *objfile = dwarf2_per_objfile->objfile;
7518 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7519 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7520 dwarf2_psymtab *pst = per_cu->v.psymtab;
7521 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7522 int i;
7523
7524 gdb_assert (len > 0);
7525 gdb_assert (per_cu->type_unit_group_p ());
7526
7527 pst->number_of_dependencies = len;
7528 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7529 for (i = 0; i < len; ++i)
7530 {
7531 struct signatured_type *iter = tu_group->tus->at (i);
7532 gdb_assert (iter->per_cu.is_debug_types);
7533 pst->dependencies[i] = iter->per_cu.v.psymtab;
7534 iter->type_unit_group = tu_group;
7535 }
7536
7537 delete tu_group->tus;
7538 tu_group->tus = nullptr;
7539
7540 return 1;
7541 }
7542
7543 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7544 Build partial symbol tables for the .debug_types comp-units. */
7545
7546 static void
7547 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7548 {
7549 if (! create_all_type_units (dwarf2_per_objfile))
7550 return;
7551
7552 build_type_psymtabs_1 (dwarf2_per_objfile);
7553 }
7554
7555 /* Traversal function for process_skeletonless_type_unit.
7556 Read a TU in a DWO file and build partial symbols for it. */
7557
7558 static int
7559 process_skeletonless_type_unit (void **slot, void *info)
7560 {
7561 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7562 struct dwarf2_per_objfile *dwarf2_per_objfile
7563 = (struct dwarf2_per_objfile *) info;
7564 struct signatured_type find_entry, *entry;
7565
7566 /* If this TU doesn't exist in the global table, add it and read it in. */
7567
7568 if (dwarf2_per_objfile->signatured_types == NULL)
7569 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7570
7571 find_entry.signature = dwo_unit->signature;
7572 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7573 &find_entry, INSERT);
7574 /* If we've already seen this type there's nothing to do. What's happening
7575 is we're doing our own version of comdat-folding here. */
7576 if (*slot != NULL)
7577 return 1;
7578
7579 /* This does the job that create_all_type_units would have done for
7580 this TU. */
7581 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7582 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7583 *slot = entry;
7584
7585 /* This does the job that build_type_psymtabs_1 would have done. */
7586 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7587 if (!reader.dummy_p)
7588 build_type_psymtabs_reader (&reader, reader.info_ptr,
7589 reader.comp_unit_die);
7590
7591 return 1;
7592 }
7593
7594 /* Traversal function for process_skeletonless_type_units. */
7595
7596 static int
7597 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7598 {
7599 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7600
7601 if (dwo_file->tus != NULL)
7602 htab_traverse_noresize (dwo_file->tus.get (),
7603 process_skeletonless_type_unit, info);
7604
7605 return 1;
7606 }
7607
7608 /* Scan all TUs of DWO files, verifying we've processed them.
7609 This is needed in case a TU was emitted without its skeleton.
7610 Note: This can't be done until we know what all the DWO files are. */
7611
7612 static void
7613 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7614 {
7615 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7616 if (get_dwp_file (dwarf2_per_objfile) == NULL
7617 && dwarf2_per_objfile->dwo_files != NULL)
7618 {
7619 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7620 process_dwo_file_for_skeletonless_type_units,
7621 dwarf2_per_objfile);
7622 }
7623 }
7624
7625 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7626
7627 static void
7628 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7629 {
7630 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7631 {
7632 dwarf2_psymtab *pst = per_cu->v.psymtab;
7633
7634 if (pst == NULL)
7635 continue;
7636
7637 for (int j = 0; j < pst->number_of_dependencies; ++j)
7638 {
7639 /* Set the 'user' field only if it is not already set. */
7640 if (pst->dependencies[j]->user == NULL)
7641 pst->dependencies[j]->user = pst;
7642 }
7643 }
7644 }
7645
7646 /* Build the partial symbol table by doing a quick pass through the
7647 .debug_info and .debug_abbrev sections. */
7648
7649 static void
7650 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7651 {
7652 struct objfile *objfile = dwarf2_per_objfile->objfile;
7653
7654 if (dwarf_read_debug)
7655 {
7656 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7657 objfile_name (objfile));
7658 }
7659
7660 scoped_restore restore_reading_psyms
7661 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7662 true);
7663
7664 dwarf2_per_objfile->info.read (objfile);
7665
7666 /* Any cached compilation units will be linked by the per-objfile
7667 read_in_chain. Make sure to free them when we're done. */
7668 free_cached_comp_units freer (dwarf2_per_objfile);
7669
7670 build_type_psymtabs (dwarf2_per_objfile);
7671
7672 create_all_comp_units (dwarf2_per_objfile);
7673
7674 /* Create a temporary address map on a temporary obstack. We later
7675 copy this to the final obstack. */
7676 auto_obstack temp_obstack;
7677
7678 scoped_restore save_psymtabs_addrmap
7679 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7680 addrmap_create_mutable (&temp_obstack));
7681
7682 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7683 process_psymtab_comp_unit (per_cu, false, language_minimal);
7684
7685 /* This has to wait until we read the CUs, we need the list of DWOs. */
7686 process_skeletonless_type_units (dwarf2_per_objfile);
7687
7688 /* Now that all TUs have been processed we can fill in the dependencies. */
7689 if (dwarf2_per_objfile->type_unit_groups != NULL)
7690 {
7691 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7692 build_type_psymtab_dependencies, dwarf2_per_objfile);
7693 }
7694
7695 if (dwarf_read_debug)
7696 print_tu_stats (dwarf2_per_objfile);
7697
7698 set_partial_user (dwarf2_per_objfile);
7699
7700 objfile->partial_symtabs->psymtabs_addrmap
7701 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7702 objfile->partial_symtabs->obstack ());
7703 /* At this point we want to keep the address map. */
7704 save_psymtabs_addrmap.release ();
7705
7706 if (dwarf_read_debug)
7707 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7708 objfile_name (objfile));
7709 }
7710
7711 /* Load the partial DIEs for a secondary CU into memory.
7712 This is also used when rereading a primary CU with load_all_dies. */
7713
7714 static void
7715 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7716 {
7717 cutu_reader reader (this_cu, NULL, 1, false);
7718
7719 if (!reader.dummy_p)
7720 {
7721 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7722 language_minimal);
7723
7724 /* Check if comp unit has_children.
7725 If so, read the rest of the partial symbols from this comp unit.
7726 If not, there's no more debug_info for this comp unit. */
7727 if (reader.comp_unit_die->has_children)
7728 load_partial_dies (&reader, reader.info_ptr, 0);
7729
7730 reader.keep ();
7731 }
7732 }
7733
7734 static void
7735 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7736 struct dwarf2_section_info *section,
7737 struct dwarf2_section_info *abbrev_section,
7738 unsigned int is_dwz)
7739 {
7740 const gdb_byte *info_ptr;
7741 struct objfile *objfile = dwarf2_per_objfile->objfile;
7742
7743 if (dwarf_read_debug)
7744 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7745 section->get_name (),
7746 section->get_file_name ());
7747
7748 section->read (objfile);
7749
7750 info_ptr = section->buffer;
7751
7752 while (info_ptr < section->buffer + section->size)
7753 {
7754 struct dwarf2_per_cu_data *this_cu;
7755
7756 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7757
7758 comp_unit_head cu_header;
7759 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7760 abbrev_section, info_ptr,
7761 rcuh_kind::COMPILE);
7762
7763 /* Save the compilation unit for later lookup. */
7764 if (cu_header.unit_type != DW_UT_type)
7765 {
7766 this_cu = XOBNEW (&objfile->objfile_obstack,
7767 struct dwarf2_per_cu_data);
7768 memset (this_cu, 0, sizeof (*this_cu));
7769 }
7770 else
7771 {
7772 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7773 struct signatured_type);
7774 memset (sig_type, 0, sizeof (*sig_type));
7775 sig_type->signature = cu_header.signature;
7776 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7777 this_cu = &sig_type->per_cu;
7778 }
7779 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7780 this_cu->sect_off = sect_off;
7781 this_cu->length = cu_header.length + cu_header.initial_length_size;
7782 this_cu->is_dwz = is_dwz;
7783 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7784 this_cu->section = section;
7785
7786 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7787
7788 info_ptr = info_ptr + this_cu->length;
7789 }
7790 }
7791
7792 /* Create a list of all compilation units in OBJFILE.
7793 This is only done for -readnow and building partial symtabs. */
7794
7795 static void
7796 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7797 {
7798 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7799 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7800 &dwarf2_per_objfile->abbrev, 0);
7801
7802 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7803 if (dwz != NULL)
7804 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7805 1);
7806 }
7807
7808 /* Process all loaded DIEs for compilation unit CU, starting at
7809 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7810 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7811 DW_AT_ranges). See the comments of add_partial_subprogram on how
7812 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7813
7814 static void
7815 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7816 CORE_ADDR *highpc, int set_addrmap,
7817 struct dwarf2_cu *cu)
7818 {
7819 struct partial_die_info *pdi;
7820
7821 /* Now, march along the PDI's, descending into ones which have
7822 interesting children but skipping the children of the other ones,
7823 until we reach the end of the compilation unit. */
7824
7825 pdi = first_die;
7826
7827 while (pdi != NULL)
7828 {
7829 pdi->fixup (cu);
7830
7831 /* Anonymous namespaces or modules have no name but have interesting
7832 children, so we need to look at them. Ditto for anonymous
7833 enums. */
7834
7835 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7836 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7837 || pdi->tag == DW_TAG_imported_unit
7838 || pdi->tag == DW_TAG_inlined_subroutine)
7839 {
7840 switch (pdi->tag)
7841 {
7842 case DW_TAG_subprogram:
7843 case DW_TAG_inlined_subroutine:
7844 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7845 break;
7846 case DW_TAG_constant:
7847 case DW_TAG_variable:
7848 case DW_TAG_typedef:
7849 case DW_TAG_union_type:
7850 if (!pdi->is_declaration)
7851 {
7852 add_partial_symbol (pdi, cu);
7853 }
7854 break;
7855 case DW_TAG_class_type:
7856 case DW_TAG_interface_type:
7857 case DW_TAG_structure_type:
7858 if (!pdi->is_declaration)
7859 {
7860 add_partial_symbol (pdi, cu);
7861 }
7862 if ((cu->language == language_rust
7863 || cu->language == language_cplus) && pdi->has_children)
7864 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7865 set_addrmap, cu);
7866 break;
7867 case DW_TAG_enumeration_type:
7868 if (!pdi->is_declaration)
7869 add_partial_enumeration (pdi, cu);
7870 break;
7871 case DW_TAG_base_type:
7872 case DW_TAG_subrange_type:
7873 /* File scope base type definitions are added to the partial
7874 symbol table. */
7875 add_partial_symbol (pdi, cu);
7876 break;
7877 case DW_TAG_namespace:
7878 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7879 break;
7880 case DW_TAG_module:
7881 if (!pdi->is_declaration)
7882 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7883 break;
7884 case DW_TAG_imported_unit:
7885 {
7886 struct dwarf2_per_cu_data *per_cu;
7887
7888 /* For now we don't handle imported units in type units. */
7889 if (cu->per_cu->is_debug_types)
7890 {
7891 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7892 " supported in type units [in module %s]"),
7893 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7894 }
7895
7896 per_cu = dwarf2_find_containing_comp_unit
7897 (pdi->d.sect_off, pdi->is_dwz,
7898 cu->per_cu->dwarf2_per_objfile);
7899
7900 /* Go read the partial unit, if needed. */
7901 if (per_cu->v.psymtab == NULL)
7902 process_psymtab_comp_unit (per_cu, true, cu->language);
7903
7904 cu->per_cu->imported_symtabs_push (per_cu);
7905 }
7906 break;
7907 case DW_TAG_imported_declaration:
7908 add_partial_symbol (pdi, cu);
7909 break;
7910 default:
7911 break;
7912 }
7913 }
7914
7915 /* If the die has a sibling, skip to the sibling. */
7916
7917 pdi = pdi->die_sibling;
7918 }
7919 }
7920
7921 /* Functions used to compute the fully scoped name of a partial DIE.
7922
7923 Normally, this is simple. For C++, the parent DIE's fully scoped
7924 name is concatenated with "::" and the partial DIE's name.
7925 Enumerators are an exception; they use the scope of their parent
7926 enumeration type, i.e. the name of the enumeration type is not
7927 prepended to the enumerator.
7928
7929 There are two complexities. One is DW_AT_specification; in this
7930 case "parent" means the parent of the target of the specification,
7931 instead of the direct parent of the DIE. The other is compilers
7932 which do not emit DW_TAG_namespace; in this case we try to guess
7933 the fully qualified name of structure types from their members'
7934 linkage names. This must be done using the DIE's children rather
7935 than the children of any DW_AT_specification target. We only need
7936 to do this for structures at the top level, i.e. if the target of
7937 any DW_AT_specification (if any; otherwise the DIE itself) does not
7938 have a parent. */
7939
7940 /* Compute the scope prefix associated with PDI's parent, in
7941 compilation unit CU. The result will be allocated on CU's
7942 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7943 field. NULL is returned if no prefix is necessary. */
7944 static const char *
7945 partial_die_parent_scope (struct partial_die_info *pdi,
7946 struct dwarf2_cu *cu)
7947 {
7948 const char *grandparent_scope;
7949 struct partial_die_info *parent, *real_pdi;
7950
7951 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7952 then this means the parent of the specification DIE. */
7953
7954 real_pdi = pdi;
7955 while (real_pdi->has_specification)
7956 {
7957 auto res = find_partial_die (real_pdi->spec_offset,
7958 real_pdi->spec_is_dwz, cu);
7959 real_pdi = res.pdi;
7960 cu = res.cu;
7961 }
7962
7963 parent = real_pdi->die_parent;
7964 if (parent == NULL)
7965 return NULL;
7966
7967 if (parent->scope_set)
7968 return parent->scope;
7969
7970 parent->fixup (cu);
7971
7972 grandparent_scope = partial_die_parent_scope (parent, cu);
7973
7974 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7975 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7976 Work around this problem here. */
7977 if (cu->language == language_cplus
7978 && parent->tag == DW_TAG_namespace
7979 && strcmp (parent->name, "::") == 0
7980 && grandparent_scope == NULL)
7981 {
7982 parent->scope = NULL;
7983 parent->scope_set = 1;
7984 return NULL;
7985 }
7986
7987 /* Nested subroutines in Fortran get a prefix. */
7988 if (pdi->tag == DW_TAG_enumerator)
7989 /* Enumerators should not get the name of the enumeration as a prefix. */
7990 parent->scope = grandparent_scope;
7991 else if (parent->tag == DW_TAG_namespace
7992 || parent->tag == DW_TAG_module
7993 || parent->tag == DW_TAG_structure_type
7994 || parent->tag == DW_TAG_class_type
7995 || parent->tag == DW_TAG_interface_type
7996 || parent->tag == DW_TAG_union_type
7997 || parent->tag == DW_TAG_enumeration_type
7998 || (cu->language == language_fortran
7999 && parent->tag == DW_TAG_subprogram
8000 && pdi->tag == DW_TAG_subprogram))
8001 {
8002 if (grandparent_scope == NULL)
8003 parent->scope = parent->name;
8004 else
8005 parent->scope = typename_concat (&cu->comp_unit_obstack,
8006 grandparent_scope,
8007 parent->name, 0, cu);
8008 }
8009 else
8010 {
8011 /* FIXME drow/2004-04-01: What should we be doing with
8012 function-local names? For partial symbols, we should probably be
8013 ignoring them. */
8014 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8015 dwarf_tag_name (parent->tag),
8016 sect_offset_str (pdi->sect_off));
8017 parent->scope = grandparent_scope;
8018 }
8019
8020 parent->scope_set = 1;
8021 return parent->scope;
8022 }
8023
8024 /* Return the fully scoped name associated with PDI, from compilation unit
8025 CU. The result will be allocated with malloc. */
8026
8027 static gdb::unique_xmalloc_ptr<char>
8028 partial_die_full_name (struct partial_die_info *pdi,
8029 struct dwarf2_cu *cu)
8030 {
8031 const char *parent_scope;
8032
8033 /* If this is a template instantiation, we can not work out the
8034 template arguments from partial DIEs. So, unfortunately, we have
8035 to go through the full DIEs. At least any work we do building
8036 types here will be reused if full symbols are loaded later. */
8037 if (pdi->has_template_arguments)
8038 {
8039 pdi->fixup (cu);
8040
8041 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8042 {
8043 struct die_info *die;
8044 struct attribute attr;
8045 struct dwarf2_cu *ref_cu = cu;
8046
8047 /* DW_FORM_ref_addr is using section offset. */
8048 attr.name = (enum dwarf_attribute) 0;
8049 attr.form = DW_FORM_ref_addr;
8050 attr.u.unsnd = to_underlying (pdi->sect_off);
8051 die = follow_die_ref (NULL, &attr, &ref_cu);
8052
8053 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8054 }
8055 }
8056
8057 parent_scope = partial_die_parent_scope (pdi, cu);
8058 if (parent_scope == NULL)
8059 return NULL;
8060 else
8061 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8062 pdi->name, 0, cu));
8063 }
8064
8065 static void
8066 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8067 {
8068 struct dwarf2_per_objfile *dwarf2_per_objfile
8069 = cu->per_cu->dwarf2_per_objfile;
8070 struct objfile *objfile = dwarf2_per_objfile->objfile;
8071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8072 CORE_ADDR addr = 0;
8073 const char *actual_name = NULL;
8074 CORE_ADDR baseaddr;
8075
8076 baseaddr = objfile->text_section_offset ();
8077
8078 gdb::unique_xmalloc_ptr<char> built_actual_name
8079 = partial_die_full_name (pdi, cu);
8080 if (built_actual_name != NULL)
8081 actual_name = built_actual_name.get ();
8082
8083 if (actual_name == NULL)
8084 actual_name = pdi->name;
8085
8086 switch (pdi->tag)
8087 {
8088 case DW_TAG_inlined_subroutine:
8089 case DW_TAG_subprogram:
8090 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8091 - baseaddr);
8092 if (pdi->is_external
8093 || cu->language == language_ada
8094 || (cu->language == language_fortran
8095 && pdi->die_parent != NULL
8096 && pdi->die_parent->tag == DW_TAG_subprogram))
8097 {
8098 /* Normally, only "external" DIEs are part of the global scope.
8099 But in Ada and Fortran, we want to be able to access nested
8100 procedures globally. So all Ada and Fortran subprograms are
8101 stored in the global scope. */
8102 add_psymbol_to_list (actual_name,
8103 built_actual_name != NULL,
8104 VAR_DOMAIN, LOC_BLOCK,
8105 SECT_OFF_TEXT (objfile),
8106 psymbol_placement::GLOBAL,
8107 addr,
8108 cu->language, objfile);
8109 }
8110 else
8111 {
8112 add_psymbol_to_list (actual_name,
8113 built_actual_name != NULL,
8114 VAR_DOMAIN, LOC_BLOCK,
8115 SECT_OFF_TEXT (objfile),
8116 psymbol_placement::STATIC,
8117 addr, cu->language, objfile);
8118 }
8119
8120 if (pdi->main_subprogram && actual_name != NULL)
8121 set_objfile_main_name (objfile, actual_name, cu->language);
8122 break;
8123 case DW_TAG_constant:
8124 add_psymbol_to_list (actual_name,
8125 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8126 -1, (pdi->is_external
8127 ? psymbol_placement::GLOBAL
8128 : psymbol_placement::STATIC),
8129 0, cu->language, objfile);
8130 break;
8131 case DW_TAG_variable:
8132 if (pdi->d.locdesc)
8133 addr = decode_locdesc (pdi->d.locdesc, cu);
8134
8135 if (pdi->d.locdesc
8136 && addr == 0
8137 && !dwarf2_per_objfile->has_section_at_zero)
8138 {
8139 /* A global or static variable may also have been stripped
8140 out by the linker if unused, in which case its address
8141 will be nullified; do not add such variables into partial
8142 symbol table then. */
8143 }
8144 else if (pdi->is_external)
8145 {
8146 /* Global Variable.
8147 Don't enter into the minimal symbol tables as there is
8148 a minimal symbol table entry from the ELF symbols already.
8149 Enter into partial symbol table if it has a location
8150 descriptor or a type.
8151 If the location descriptor is missing, new_symbol will create
8152 a LOC_UNRESOLVED symbol, the address of the variable will then
8153 be determined from the minimal symbol table whenever the variable
8154 is referenced.
8155 The address for the partial symbol table entry is not
8156 used by GDB, but it comes in handy for debugging partial symbol
8157 table building. */
8158
8159 if (pdi->d.locdesc || pdi->has_type)
8160 add_psymbol_to_list (actual_name,
8161 built_actual_name != NULL,
8162 VAR_DOMAIN, LOC_STATIC,
8163 SECT_OFF_TEXT (objfile),
8164 psymbol_placement::GLOBAL,
8165 addr, cu->language, objfile);
8166 }
8167 else
8168 {
8169 int has_loc = pdi->d.locdesc != NULL;
8170
8171 /* Static Variable. Skip symbols whose value we cannot know (those
8172 without location descriptors or constant values). */
8173 if (!has_loc && !pdi->has_const_value)
8174 return;
8175
8176 add_psymbol_to_list (actual_name,
8177 built_actual_name != NULL,
8178 VAR_DOMAIN, LOC_STATIC,
8179 SECT_OFF_TEXT (objfile),
8180 psymbol_placement::STATIC,
8181 has_loc ? addr : 0,
8182 cu->language, objfile);
8183 }
8184 break;
8185 case DW_TAG_typedef:
8186 case DW_TAG_base_type:
8187 case DW_TAG_subrange_type:
8188 add_psymbol_to_list (actual_name,
8189 built_actual_name != NULL,
8190 VAR_DOMAIN, LOC_TYPEDEF, -1,
8191 psymbol_placement::STATIC,
8192 0, cu->language, objfile);
8193 break;
8194 case DW_TAG_imported_declaration:
8195 case DW_TAG_namespace:
8196 add_psymbol_to_list (actual_name,
8197 built_actual_name != NULL,
8198 VAR_DOMAIN, LOC_TYPEDEF, -1,
8199 psymbol_placement::GLOBAL,
8200 0, cu->language, objfile);
8201 break;
8202 case DW_TAG_module:
8203 /* With Fortran 77 there might be a "BLOCK DATA" module
8204 available without any name. If so, we skip the module as it
8205 doesn't bring any value. */
8206 if (actual_name != nullptr)
8207 add_psymbol_to_list (actual_name,
8208 built_actual_name != NULL,
8209 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8210 psymbol_placement::GLOBAL,
8211 0, cu->language, objfile);
8212 break;
8213 case DW_TAG_class_type:
8214 case DW_TAG_interface_type:
8215 case DW_TAG_structure_type:
8216 case DW_TAG_union_type:
8217 case DW_TAG_enumeration_type:
8218 /* Skip external references. The DWARF standard says in the section
8219 about "Structure, Union, and Class Type Entries": "An incomplete
8220 structure, union or class type is represented by a structure,
8221 union or class entry that does not have a byte size attribute
8222 and that has a DW_AT_declaration attribute." */
8223 if (!pdi->has_byte_size && pdi->is_declaration)
8224 return;
8225
8226 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8227 static vs. global. */
8228 add_psymbol_to_list (actual_name,
8229 built_actual_name != NULL,
8230 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8231 cu->language == language_cplus
8232 ? psymbol_placement::GLOBAL
8233 : psymbol_placement::STATIC,
8234 0, cu->language, objfile);
8235
8236 break;
8237 case DW_TAG_enumerator:
8238 add_psymbol_to_list (actual_name,
8239 built_actual_name != NULL,
8240 VAR_DOMAIN, LOC_CONST, -1,
8241 cu->language == language_cplus
8242 ? psymbol_placement::GLOBAL
8243 : psymbol_placement::STATIC,
8244 0, cu->language, objfile);
8245 break;
8246 default:
8247 break;
8248 }
8249 }
8250
8251 /* Read a partial die corresponding to a namespace; also, add a symbol
8252 corresponding to that namespace to the symbol table. NAMESPACE is
8253 the name of the enclosing namespace. */
8254
8255 static void
8256 add_partial_namespace (struct partial_die_info *pdi,
8257 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8258 int set_addrmap, struct dwarf2_cu *cu)
8259 {
8260 /* Add a symbol for the namespace. */
8261
8262 add_partial_symbol (pdi, cu);
8263
8264 /* Now scan partial symbols in that namespace. */
8265
8266 if (pdi->has_children)
8267 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8268 }
8269
8270 /* Read a partial die corresponding to a Fortran module. */
8271
8272 static void
8273 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8274 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8275 {
8276 /* Add a symbol for the namespace. */
8277
8278 add_partial_symbol (pdi, cu);
8279
8280 /* Now scan partial symbols in that module. */
8281
8282 if (pdi->has_children)
8283 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8284 }
8285
8286 /* Read a partial die corresponding to a subprogram or an inlined
8287 subprogram and create a partial symbol for that subprogram.
8288 When the CU language allows it, this routine also defines a partial
8289 symbol for each nested subprogram that this subprogram contains.
8290 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8291 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8292
8293 PDI may also be a lexical block, in which case we simply search
8294 recursively for subprograms defined inside that lexical block.
8295 Again, this is only performed when the CU language allows this
8296 type of definitions. */
8297
8298 static void
8299 add_partial_subprogram (struct partial_die_info *pdi,
8300 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8301 int set_addrmap, struct dwarf2_cu *cu)
8302 {
8303 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8304 {
8305 if (pdi->has_pc_info)
8306 {
8307 if (pdi->lowpc < *lowpc)
8308 *lowpc = pdi->lowpc;
8309 if (pdi->highpc > *highpc)
8310 *highpc = pdi->highpc;
8311 if (set_addrmap)
8312 {
8313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8314 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8315 CORE_ADDR baseaddr;
8316 CORE_ADDR this_highpc;
8317 CORE_ADDR this_lowpc;
8318
8319 baseaddr = objfile->text_section_offset ();
8320 this_lowpc
8321 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8322 pdi->lowpc + baseaddr)
8323 - baseaddr);
8324 this_highpc
8325 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8326 pdi->highpc + baseaddr)
8327 - baseaddr);
8328 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8329 this_lowpc, this_highpc - 1,
8330 cu->per_cu->v.psymtab);
8331 }
8332 }
8333
8334 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8335 {
8336 if (!pdi->is_declaration)
8337 /* Ignore subprogram DIEs that do not have a name, they are
8338 illegal. Do not emit a complaint at this point, we will
8339 do so when we convert this psymtab into a symtab. */
8340 if (pdi->name)
8341 add_partial_symbol (pdi, cu);
8342 }
8343 }
8344
8345 if (! pdi->has_children)
8346 return;
8347
8348 if (cu->language == language_ada || cu->language == language_fortran)
8349 {
8350 pdi = pdi->die_child;
8351 while (pdi != NULL)
8352 {
8353 pdi->fixup (cu);
8354 if (pdi->tag == DW_TAG_subprogram
8355 || pdi->tag == DW_TAG_inlined_subroutine
8356 || pdi->tag == DW_TAG_lexical_block)
8357 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8358 pdi = pdi->die_sibling;
8359 }
8360 }
8361 }
8362
8363 /* Read a partial die corresponding to an enumeration type. */
8364
8365 static void
8366 add_partial_enumeration (struct partial_die_info *enum_pdi,
8367 struct dwarf2_cu *cu)
8368 {
8369 struct partial_die_info *pdi;
8370
8371 if (enum_pdi->name != NULL)
8372 add_partial_symbol (enum_pdi, cu);
8373
8374 pdi = enum_pdi->die_child;
8375 while (pdi)
8376 {
8377 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8378 complaint (_("malformed enumerator DIE ignored"));
8379 else
8380 add_partial_symbol (pdi, cu);
8381 pdi = pdi->die_sibling;
8382 }
8383 }
8384
8385 /* Return the initial uleb128 in the die at INFO_PTR. */
8386
8387 static unsigned int
8388 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8389 {
8390 unsigned int bytes_read;
8391
8392 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8393 }
8394
8395 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8396 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8397
8398 Return the corresponding abbrev, or NULL if the number is zero (indicating
8399 an empty DIE). In either case *BYTES_READ will be set to the length of
8400 the initial number. */
8401
8402 static struct abbrev_info *
8403 peek_die_abbrev (const die_reader_specs &reader,
8404 const gdb_byte *info_ptr, unsigned int *bytes_read)
8405 {
8406 dwarf2_cu *cu = reader.cu;
8407 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8408 unsigned int abbrev_number
8409 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8410
8411 if (abbrev_number == 0)
8412 return NULL;
8413
8414 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8415 if (!abbrev)
8416 {
8417 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8418 " at offset %s [in module %s]"),
8419 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8420 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8421 }
8422
8423 return abbrev;
8424 }
8425
8426 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8427 Returns a pointer to the end of a series of DIEs, terminated by an empty
8428 DIE. Any children of the skipped DIEs will also be skipped. */
8429
8430 static const gdb_byte *
8431 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8432 {
8433 while (1)
8434 {
8435 unsigned int bytes_read;
8436 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8437
8438 if (abbrev == NULL)
8439 return info_ptr + bytes_read;
8440 else
8441 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8442 }
8443 }
8444
8445 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8446 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8447 abbrev corresponding to that skipped uleb128 should be passed in
8448 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8449 children. */
8450
8451 static const gdb_byte *
8452 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8453 struct abbrev_info *abbrev)
8454 {
8455 unsigned int bytes_read;
8456 struct attribute attr;
8457 bfd *abfd = reader->abfd;
8458 struct dwarf2_cu *cu = reader->cu;
8459 const gdb_byte *buffer = reader->buffer;
8460 const gdb_byte *buffer_end = reader->buffer_end;
8461 unsigned int form, i;
8462
8463 for (i = 0; i < abbrev->num_attrs; i++)
8464 {
8465 /* The only abbrev we care about is DW_AT_sibling. */
8466 if (abbrev->attrs[i].name == DW_AT_sibling)
8467 {
8468 bool ignored;
8469 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8470 &ignored);
8471 if (attr.form == DW_FORM_ref_addr)
8472 complaint (_("ignoring absolute DW_AT_sibling"));
8473 else
8474 {
8475 sect_offset off = attr.get_ref_die_offset ();
8476 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8477
8478 if (sibling_ptr < info_ptr)
8479 complaint (_("DW_AT_sibling points backwards"));
8480 else if (sibling_ptr > reader->buffer_end)
8481 reader->die_section->overflow_complaint ();
8482 else
8483 return sibling_ptr;
8484 }
8485 }
8486
8487 /* If it isn't DW_AT_sibling, skip this attribute. */
8488 form = abbrev->attrs[i].form;
8489 skip_attribute:
8490 switch (form)
8491 {
8492 case DW_FORM_ref_addr:
8493 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8494 and later it is offset sized. */
8495 if (cu->header.version == 2)
8496 info_ptr += cu->header.addr_size;
8497 else
8498 info_ptr += cu->header.offset_size;
8499 break;
8500 case DW_FORM_GNU_ref_alt:
8501 info_ptr += cu->header.offset_size;
8502 break;
8503 case DW_FORM_addr:
8504 info_ptr += cu->header.addr_size;
8505 break;
8506 case DW_FORM_data1:
8507 case DW_FORM_ref1:
8508 case DW_FORM_flag:
8509 case DW_FORM_strx1:
8510 info_ptr += 1;
8511 break;
8512 case DW_FORM_flag_present:
8513 case DW_FORM_implicit_const:
8514 break;
8515 case DW_FORM_data2:
8516 case DW_FORM_ref2:
8517 case DW_FORM_strx2:
8518 info_ptr += 2;
8519 break;
8520 case DW_FORM_strx3:
8521 info_ptr += 3;
8522 break;
8523 case DW_FORM_data4:
8524 case DW_FORM_ref4:
8525 case DW_FORM_strx4:
8526 info_ptr += 4;
8527 break;
8528 case DW_FORM_data8:
8529 case DW_FORM_ref8:
8530 case DW_FORM_ref_sig8:
8531 info_ptr += 8;
8532 break;
8533 case DW_FORM_data16:
8534 info_ptr += 16;
8535 break;
8536 case DW_FORM_string:
8537 read_direct_string (abfd, info_ptr, &bytes_read);
8538 info_ptr += bytes_read;
8539 break;
8540 case DW_FORM_sec_offset:
8541 case DW_FORM_strp:
8542 case DW_FORM_GNU_strp_alt:
8543 info_ptr += cu->header.offset_size;
8544 break;
8545 case DW_FORM_exprloc:
8546 case DW_FORM_block:
8547 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8548 info_ptr += bytes_read;
8549 break;
8550 case DW_FORM_block1:
8551 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8552 break;
8553 case DW_FORM_block2:
8554 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8555 break;
8556 case DW_FORM_block4:
8557 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8558 break;
8559 case DW_FORM_addrx:
8560 case DW_FORM_strx:
8561 case DW_FORM_sdata:
8562 case DW_FORM_udata:
8563 case DW_FORM_ref_udata:
8564 case DW_FORM_GNU_addr_index:
8565 case DW_FORM_GNU_str_index:
8566 case DW_FORM_rnglistx:
8567 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8568 break;
8569 case DW_FORM_indirect:
8570 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8571 info_ptr += bytes_read;
8572 /* We need to continue parsing from here, so just go back to
8573 the top. */
8574 goto skip_attribute;
8575
8576 default:
8577 error (_("Dwarf Error: Cannot handle %s "
8578 "in DWARF reader [in module %s]"),
8579 dwarf_form_name (form),
8580 bfd_get_filename (abfd));
8581 }
8582 }
8583
8584 if (abbrev->has_children)
8585 return skip_children (reader, info_ptr);
8586 else
8587 return info_ptr;
8588 }
8589
8590 /* Locate ORIG_PDI's sibling.
8591 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8592
8593 static const gdb_byte *
8594 locate_pdi_sibling (const struct die_reader_specs *reader,
8595 struct partial_die_info *orig_pdi,
8596 const gdb_byte *info_ptr)
8597 {
8598 /* Do we know the sibling already? */
8599
8600 if (orig_pdi->sibling)
8601 return orig_pdi->sibling;
8602
8603 /* Are there any children to deal with? */
8604
8605 if (!orig_pdi->has_children)
8606 return info_ptr;
8607
8608 /* Skip the children the long way. */
8609
8610 return skip_children (reader, info_ptr);
8611 }
8612
8613 /* Expand this partial symbol table into a full symbol table. SELF is
8614 not NULL. */
8615
8616 void
8617 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8618 {
8619 struct dwarf2_per_objfile *dwarf2_per_objfile
8620 = get_dwarf2_per_objfile (objfile);
8621
8622 gdb_assert (!readin);
8623 /* If this psymtab is constructed from a debug-only objfile, the
8624 has_section_at_zero flag will not necessarily be correct. We
8625 can get the correct value for this flag by looking at the data
8626 associated with the (presumably stripped) associated objfile. */
8627 if (objfile->separate_debug_objfile_backlink)
8628 {
8629 struct dwarf2_per_objfile *dpo_backlink
8630 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8631
8632 dwarf2_per_objfile->has_section_at_zero
8633 = dpo_backlink->has_section_at_zero;
8634 }
8635
8636 expand_psymtab (objfile);
8637
8638 process_cu_includes (dwarf2_per_objfile);
8639 }
8640 \f
8641 /* Reading in full CUs. */
8642
8643 /* Add PER_CU to the queue. */
8644
8645 static void
8646 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8647 enum language pretend_language)
8648 {
8649 per_cu->queued = 1;
8650 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8651 }
8652
8653 /* If PER_CU is not yet queued, add it to the queue.
8654 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8655 dependency.
8656 The result is non-zero if PER_CU was queued, otherwise the result is zero
8657 meaning either PER_CU is already queued or it is already loaded.
8658
8659 N.B. There is an invariant here that if a CU is queued then it is loaded.
8660 The caller is required to load PER_CU if we return non-zero. */
8661
8662 static int
8663 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8664 struct dwarf2_per_cu_data *per_cu,
8665 enum language pretend_language)
8666 {
8667 /* We may arrive here during partial symbol reading, if we need full
8668 DIEs to process an unusual case (e.g. template arguments). Do
8669 not queue PER_CU, just tell our caller to load its DIEs. */
8670 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8671 {
8672 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8673 return 1;
8674 return 0;
8675 }
8676
8677 /* Mark the dependence relation so that we don't flush PER_CU
8678 too early. */
8679 if (dependent_cu != NULL)
8680 dwarf2_add_dependence (dependent_cu, per_cu);
8681
8682 /* If it's already on the queue, we have nothing to do. */
8683 if (per_cu->queued)
8684 return 0;
8685
8686 /* If the compilation unit is already loaded, just mark it as
8687 used. */
8688 if (per_cu->cu != NULL)
8689 {
8690 per_cu->cu->last_used = 0;
8691 return 0;
8692 }
8693
8694 /* Add it to the queue. */
8695 queue_comp_unit (per_cu, pretend_language);
8696
8697 return 1;
8698 }
8699
8700 /* Process the queue. */
8701
8702 static void
8703 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8704 {
8705 if (dwarf_read_debug)
8706 {
8707 fprintf_unfiltered (gdb_stdlog,
8708 "Expanding one or more symtabs of objfile %s ...\n",
8709 objfile_name (dwarf2_per_objfile->objfile));
8710 }
8711
8712 /* The queue starts out with one item, but following a DIE reference
8713 may load a new CU, adding it to the end of the queue. */
8714 while (!dwarf2_per_objfile->queue.empty ())
8715 {
8716 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8717
8718 if ((dwarf2_per_objfile->using_index
8719 ? !item.per_cu->v.quick->compunit_symtab
8720 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8721 /* Skip dummy CUs. */
8722 && item.per_cu->cu != NULL)
8723 {
8724 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8725 unsigned int debug_print_threshold;
8726 char buf[100];
8727
8728 if (per_cu->is_debug_types)
8729 {
8730 struct signatured_type *sig_type =
8731 (struct signatured_type *) per_cu;
8732
8733 sprintf (buf, "TU %s at offset %s",
8734 hex_string (sig_type->signature),
8735 sect_offset_str (per_cu->sect_off));
8736 /* There can be 100s of TUs.
8737 Only print them in verbose mode. */
8738 debug_print_threshold = 2;
8739 }
8740 else
8741 {
8742 sprintf (buf, "CU at offset %s",
8743 sect_offset_str (per_cu->sect_off));
8744 debug_print_threshold = 1;
8745 }
8746
8747 if (dwarf_read_debug >= debug_print_threshold)
8748 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8749
8750 if (per_cu->is_debug_types)
8751 process_full_type_unit (per_cu, item.pretend_language);
8752 else
8753 process_full_comp_unit (per_cu, item.pretend_language);
8754
8755 if (dwarf_read_debug >= debug_print_threshold)
8756 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8757 }
8758
8759 item.per_cu->queued = 0;
8760 dwarf2_per_objfile->queue.pop ();
8761 }
8762
8763 if (dwarf_read_debug)
8764 {
8765 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8766 objfile_name (dwarf2_per_objfile->objfile));
8767 }
8768 }
8769
8770 /* Read in full symbols for PST, and anything it depends on. */
8771
8772 void
8773 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8774 {
8775 if (readin)
8776 return;
8777
8778 expand_dependencies (objfile);
8779
8780 dw2_do_instantiate_symtab (per_cu_data, false);
8781 gdb_assert (get_compunit_symtab () != nullptr);
8782 }
8783
8784 /* Trivial hash function for die_info: the hash value of a DIE
8785 is its offset in .debug_info for this objfile. */
8786
8787 static hashval_t
8788 die_hash (const void *item)
8789 {
8790 const struct die_info *die = (const struct die_info *) item;
8791
8792 return to_underlying (die->sect_off);
8793 }
8794
8795 /* Trivial comparison function for die_info structures: two DIEs
8796 are equal if they have the same offset. */
8797
8798 static int
8799 die_eq (const void *item_lhs, const void *item_rhs)
8800 {
8801 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8802 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8803
8804 return die_lhs->sect_off == die_rhs->sect_off;
8805 }
8806
8807 /* Load the DIEs associated with PER_CU into memory. */
8808
8809 static void
8810 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8811 bool skip_partial,
8812 enum language pretend_language)
8813 {
8814 gdb_assert (! this_cu->is_debug_types);
8815
8816 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8817 if (reader.dummy_p)
8818 return;
8819
8820 struct dwarf2_cu *cu = reader.cu;
8821 const gdb_byte *info_ptr = reader.info_ptr;
8822
8823 gdb_assert (cu->die_hash == NULL);
8824 cu->die_hash =
8825 htab_create_alloc_ex (cu->header.length / 12,
8826 die_hash,
8827 die_eq,
8828 NULL,
8829 &cu->comp_unit_obstack,
8830 hashtab_obstack_allocate,
8831 dummy_obstack_deallocate);
8832
8833 if (reader.comp_unit_die->has_children)
8834 reader.comp_unit_die->child
8835 = read_die_and_siblings (&reader, reader.info_ptr,
8836 &info_ptr, reader.comp_unit_die);
8837 cu->dies = reader.comp_unit_die;
8838 /* comp_unit_die is not stored in die_hash, no need. */
8839
8840 /* We try not to read any attributes in this function, because not
8841 all CUs needed for references have been loaded yet, and symbol
8842 table processing isn't initialized. But we have to set the CU language,
8843 or we won't be able to build types correctly.
8844 Similarly, if we do not read the producer, we can not apply
8845 producer-specific interpretation. */
8846 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8847
8848 reader.keep ();
8849 }
8850
8851 /* Add a DIE to the delayed physname list. */
8852
8853 static void
8854 add_to_method_list (struct type *type, int fnfield_index, int index,
8855 const char *name, struct die_info *die,
8856 struct dwarf2_cu *cu)
8857 {
8858 struct delayed_method_info mi;
8859 mi.type = type;
8860 mi.fnfield_index = fnfield_index;
8861 mi.index = index;
8862 mi.name = name;
8863 mi.die = die;
8864 cu->method_list.push_back (mi);
8865 }
8866
8867 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8868 "const" / "volatile". If so, decrements LEN by the length of the
8869 modifier and return true. Otherwise return false. */
8870
8871 template<size_t N>
8872 static bool
8873 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8874 {
8875 size_t mod_len = sizeof (mod) - 1;
8876 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8877 {
8878 len -= mod_len;
8879 return true;
8880 }
8881 return false;
8882 }
8883
8884 /* Compute the physnames of any methods on the CU's method list.
8885
8886 The computation of method physnames is delayed in order to avoid the
8887 (bad) condition that one of the method's formal parameters is of an as yet
8888 incomplete type. */
8889
8890 static void
8891 compute_delayed_physnames (struct dwarf2_cu *cu)
8892 {
8893 /* Only C++ delays computing physnames. */
8894 if (cu->method_list.empty ())
8895 return;
8896 gdb_assert (cu->language == language_cplus);
8897
8898 for (const delayed_method_info &mi : cu->method_list)
8899 {
8900 const char *physname;
8901 struct fn_fieldlist *fn_flp
8902 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8903 physname = dwarf2_physname (mi.name, mi.die, cu);
8904 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8905 = physname ? physname : "";
8906
8907 /* Since there's no tag to indicate whether a method is a
8908 const/volatile overload, extract that information out of the
8909 demangled name. */
8910 if (physname != NULL)
8911 {
8912 size_t len = strlen (physname);
8913
8914 while (1)
8915 {
8916 if (physname[len] == ')') /* shortcut */
8917 break;
8918 else if (check_modifier (physname, len, " const"))
8919 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8920 else if (check_modifier (physname, len, " volatile"))
8921 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8922 else
8923 break;
8924 }
8925 }
8926 }
8927
8928 /* The list is no longer needed. */
8929 cu->method_list.clear ();
8930 }
8931
8932 /* Go objects should be embedded in a DW_TAG_module DIE,
8933 and it's not clear if/how imported objects will appear.
8934 To keep Go support simple until that's worked out,
8935 go back through what we've read and create something usable.
8936 We could do this while processing each DIE, and feels kinda cleaner,
8937 but that way is more invasive.
8938 This is to, for example, allow the user to type "p var" or "b main"
8939 without having to specify the package name, and allow lookups
8940 of module.object to work in contexts that use the expression
8941 parser. */
8942
8943 static void
8944 fixup_go_packaging (struct dwarf2_cu *cu)
8945 {
8946 gdb::unique_xmalloc_ptr<char> package_name;
8947 struct pending *list;
8948 int i;
8949
8950 for (list = *cu->get_builder ()->get_global_symbols ();
8951 list != NULL;
8952 list = list->next)
8953 {
8954 for (i = 0; i < list->nsyms; ++i)
8955 {
8956 struct symbol *sym = list->symbol[i];
8957
8958 if (sym->language () == language_go
8959 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8960 {
8961 gdb::unique_xmalloc_ptr<char> this_package_name
8962 (go_symbol_package_name (sym));
8963
8964 if (this_package_name == NULL)
8965 continue;
8966 if (package_name == NULL)
8967 package_name = std::move (this_package_name);
8968 else
8969 {
8970 struct objfile *objfile
8971 = cu->per_cu->dwarf2_per_objfile->objfile;
8972 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8973 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8974 (symbol_symtab (sym) != NULL
8975 ? symtab_to_filename_for_display
8976 (symbol_symtab (sym))
8977 : objfile_name (objfile)),
8978 this_package_name.get (), package_name.get ());
8979 }
8980 }
8981 }
8982 }
8983
8984 if (package_name != NULL)
8985 {
8986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8987 const char *saved_package_name = objfile->intern (package_name.get ());
8988 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8989 saved_package_name);
8990 struct symbol *sym;
8991
8992 sym = allocate_symbol (objfile);
8993 sym->set_language (language_go, &objfile->objfile_obstack);
8994 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8995 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8996 e.g., "main" finds the "main" module and not C's main(). */
8997 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8999 SYMBOL_TYPE (sym) = type;
9000
9001 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9002 }
9003 }
9004
9005 /* Allocate a fully-qualified name consisting of the two parts on the
9006 obstack. */
9007
9008 static const char *
9009 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9010 {
9011 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9012 }
9013
9014 /* A helper that allocates a struct discriminant_info to attach to a
9015 union type. */
9016
9017 static struct discriminant_info *
9018 alloc_discriminant_info (struct type *type, int discriminant_index,
9019 int default_index)
9020 {
9021 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9022 gdb_assert (discriminant_index == -1
9023 || (discriminant_index >= 0
9024 && discriminant_index < TYPE_NFIELDS (type)));
9025 gdb_assert (default_index == -1
9026 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9027
9028 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9029
9030 struct discriminant_info *disc
9031 = ((struct discriminant_info *)
9032 TYPE_ZALLOC (type,
9033 offsetof (struct discriminant_info, discriminants)
9034 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9035 disc->default_index = default_index;
9036 disc->discriminant_index = discriminant_index;
9037
9038 struct dynamic_prop prop;
9039 prop.kind = PROP_UNDEFINED;
9040 prop.data.baton = disc;
9041
9042 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9043
9044 return disc;
9045 }
9046
9047 /* Some versions of rustc emitted enums in an unusual way.
9048
9049 Ordinary enums were emitted as unions. The first element of each
9050 structure in the union was named "RUST$ENUM$DISR". This element
9051 held the discriminant.
9052
9053 These versions of Rust also implemented the "non-zero"
9054 optimization. When the enum had two values, and one is empty and
9055 the other holds a pointer that cannot be zero, the pointer is used
9056 as the discriminant, with a zero value meaning the empty variant.
9057 Here, the union's first member is of the form
9058 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9059 where the fieldnos are the indices of the fields that should be
9060 traversed in order to find the field (which may be several fields deep)
9061 and the variantname is the name of the variant of the case when the
9062 field is zero.
9063
9064 This function recognizes whether TYPE is of one of these forms,
9065 and, if so, smashes it to be a variant type. */
9066
9067 static void
9068 quirk_rust_enum (struct type *type, struct objfile *objfile)
9069 {
9070 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9071
9072 /* We don't need to deal with empty enums. */
9073 if (TYPE_NFIELDS (type) == 0)
9074 return;
9075
9076 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9077 if (TYPE_NFIELDS (type) == 1
9078 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9079 {
9080 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9081
9082 /* Decode the field name to find the offset of the
9083 discriminant. */
9084 ULONGEST bit_offset = 0;
9085 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9086 while (name[0] >= '0' && name[0] <= '9')
9087 {
9088 char *tail;
9089 unsigned long index = strtoul (name, &tail, 10);
9090 name = tail;
9091 if (*name != '$'
9092 || index >= TYPE_NFIELDS (field_type)
9093 || (TYPE_FIELD_LOC_KIND (field_type, index)
9094 != FIELD_LOC_KIND_BITPOS))
9095 {
9096 complaint (_("Could not parse Rust enum encoding string \"%s\""
9097 "[in module %s]"),
9098 TYPE_FIELD_NAME (type, 0),
9099 objfile_name (objfile));
9100 return;
9101 }
9102 ++name;
9103
9104 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9105 field_type = TYPE_FIELD_TYPE (field_type, index);
9106 }
9107
9108 /* Make a union to hold the variants. */
9109 struct type *union_type = alloc_type (objfile);
9110 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9111 TYPE_NFIELDS (union_type) = 3;
9112 TYPE_FIELDS (union_type)
9113 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9114 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9115 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9116
9117 /* Put the discriminant must at index 0. */
9118 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9119 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9120 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9121 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9122
9123 /* The order of fields doesn't really matter, so put the real
9124 field at index 1 and the data-less field at index 2. */
9125 struct discriminant_info *disc
9126 = alloc_discriminant_info (union_type, 0, 1);
9127 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9128 TYPE_FIELD_NAME (union_type, 1)
9129 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9130 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9131 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9132 TYPE_FIELD_NAME (union_type, 1));
9133
9134 const char *dataless_name
9135 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9136 name);
9137 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9138 dataless_name);
9139 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9140 /* NAME points into the original discriminant name, which
9141 already has the correct lifetime. */
9142 TYPE_FIELD_NAME (union_type, 2) = name;
9143 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9144 disc->discriminants[2] = 0;
9145
9146 /* Smash this type to be a structure type. We have to do this
9147 because the type has already been recorded. */
9148 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9149 TYPE_NFIELDS (type) = 1;
9150 TYPE_FIELDS (type)
9151 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9152
9153 /* Install the variant part. */
9154 TYPE_FIELD_TYPE (type, 0) = union_type;
9155 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9156 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9157 }
9158 /* A union with a single anonymous field is probably an old-style
9159 univariant enum. */
9160 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9161 {
9162 /* Smash this type to be a structure type. We have to do this
9163 because the type has already been recorded. */
9164 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9165
9166 /* Make a union to hold the variants. */
9167 struct type *union_type = alloc_type (objfile);
9168 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9169 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9170 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9171 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9172 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9173
9174 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9175 const char *variant_name
9176 = rust_last_path_segment (TYPE_NAME (field_type));
9177 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9178 TYPE_NAME (field_type)
9179 = rust_fully_qualify (&objfile->objfile_obstack,
9180 TYPE_NAME (type), variant_name);
9181
9182 /* Install the union in the outer struct type. */
9183 TYPE_NFIELDS (type) = 1;
9184 TYPE_FIELDS (type)
9185 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9186 TYPE_FIELD_TYPE (type, 0) = union_type;
9187 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9188 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9189
9190 alloc_discriminant_info (union_type, -1, 0);
9191 }
9192 else
9193 {
9194 struct type *disr_type = nullptr;
9195 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9196 {
9197 disr_type = TYPE_FIELD_TYPE (type, i);
9198
9199 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9200 {
9201 /* All fields of a true enum will be structs. */
9202 return;
9203 }
9204 else if (TYPE_NFIELDS (disr_type) == 0)
9205 {
9206 /* Could be data-less variant, so keep going. */
9207 disr_type = nullptr;
9208 }
9209 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9210 "RUST$ENUM$DISR") != 0)
9211 {
9212 /* Not a Rust enum. */
9213 return;
9214 }
9215 else
9216 {
9217 /* Found one. */
9218 break;
9219 }
9220 }
9221
9222 /* If we got here without a discriminant, then it's probably
9223 just a union. */
9224 if (disr_type == nullptr)
9225 return;
9226
9227 /* Smash this type to be a structure type. We have to do this
9228 because the type has already been recorded. */
9229 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9230
9231 /* Make a union to hold the variants. */
9232 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9233 struct type *union_type = alloc_type (objfile);
9234 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9235 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9236 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9237 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9238 TYPE_FIELDS (union_type)
9239 = (struct field *) TYPE_ZALLOC (union_type,
9240 (TYPE_NFIELDS (union_type)
9241 * sizeof (struct field)));
9242
9243 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9244 TYPE_NFIELDS (type) * sizeof (struct field));
9245
9246 /* Install the discriminant at index 0 in the union. */
9247 TYPE_FIELD (union_type, 0) = *disr_field;
9248 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9249 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9250
9251 /* Install the union in the outer struct type. */
9252 TYPE_FIELD_TYPE (type, 0) = union_type;
9253 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9254 TYPE_NFIELDS (type) = 1;
9255
9256 /* Set the size and offset of the union type. */
9257 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9258
9259 /* We need a way to find the correct discriminant given a
9260 variant name. For convenience we build a map here. */
9261 struct type *enum_type = FIELD_TYPE (*disr_field);
9262 std::unordered_map<std::string, ULONGEST> discriminant_map;
9263 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9264 {
9265 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9266 {
9267 const char *name
9268 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9269 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9270 }
9271 }
9272
9273 int n_fields = TYPE_NFIELDS (union_type);
9274 struct discriminant_info *disc
9275 = alloc_discriminant_info (union_type, 0, -1);
9276 /* Skip the discriminant here. */
9277 for (int i = 1; i < n_fields; ++i)
9278 {
9279 /* Find the final word in the name of this variant's type.
9280 That name can be used to look up the correct
9281 discriminant. */
9282 const char *variant_name
9283 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9284 i)));
9285
9286 auto iter = discriminant_map.find (variant_name);
9287 if (iter != discriminant_map.end ())
9288 disc->discriminants[i] = iter->second;
9289
9290 /* Remove the discriminant field, if it exists. */
9291 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9292 if (TYPE_NFIELDS (sub_type) > 0)
9293 {
9294 --TYPE_NFIELDS (sub_type);
9295 ++TYPE_FIELDS (sub_type);
9296 }
9297 TYPE_FIELD_NAME (union_type, i) = variant_name;
9298 TYPE_NAME (sub_type)
9299 = rust_fully_qualify (&objfile->objfile_obstack,
9300 TYPE_NAME (type), variant_name);
9301 }
9302 }
9303 }
9304
9305 /* Rewrite some Rust unions to be structures with variants parts. */
9306
9307 static void
9308 rust_union_quirks (struct dwarf2_cu *cu)
9309 {
9310 gdb_assert (cu->language == language_rust);
9311 for (type *type_ : cu->rust_unions)
9312 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9313 /* We don't need this any more. */
9314 cu->rust_unions.clear ();
9315 }
9316
9317 /* Return the symtab for PER_CU. This works properly regardless of
9318 whether we're using the index or psymtabs. */
9319
9320 static struct compunit_symtab *
9321 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9322 {
9323 return (per_cu->dwarf2_per_objfile->using_index
9324 ? per_cu->v.quick->compunit_symtab
9325 : per_cu->v.psymtab->compunit_symtab);
9326 }
9327
9328 /* A helper function for computing the list of all symbol tables
9329 included by PER_CU. */
9330
9331 static void
9332 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9333 htab_t all_children, htab_t all_type_symtabs,
9334 struct dwarf2_per_cu_data *per_cu,
9335 struct compunit_symtab *immediate_parent)
9336 {
9337 void **slot;
9338 struct compunit_symtab *cust;
9339
9340 slot = htab_find_slot (all_children, per_cu, INSERT);
9341 if (*slot != NULL)
9342 {
9343 /* This inclusion and its children have been processed. */
9344 return;
9345 }
9346
9347 *slot = per_cu;
9348 /* Only add a CU if it has a symbol table. */
9349 cust = get_compunit_symtab (per_cu);
9350 if (cust != NULL)
9351 {
9352 /* If this is a type unit only add its symbol table if we haven't
9353 seen it yet (type unit per_cu's can share symtabs). */
9354 if (per_cu->is_debug_types)
9355 {
9356 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9357 if (*slot == NULL)
9358 {
9359 *slot = cust;
9360 result->push_back (cust);
9361 if (cust->user == NULL)
9362 cust->user = immediate_parent;
9363 }
9364 }
9365 else
9366 {
9367 result->push_back (cust);
9368 if (cust->user == NULL)
9369 cust->user = immediate_parent;
9370 }
9371 }
9372
9373 if (!per_cu->imported_symtabs_empty ())
9374 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9375 {
9376 recursively_compute_inclusions (result, all_children,
9377 all_type_symtabs, ptr, cust);
9378 }
9379 }
9380
9381 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9382 PER_CU. */
9383
9384 static void
9385 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9386 {
9387 gdb_assert (! per_cu->is_debug_types);
9388
9389 if (!per_cu->imported_symtabs_empty ())
9390 {
9391 int len;
9392 std::vector<compunit_symtab *> result_symtabs;
9393 htab_t all_children, all_type_symtabs;
9394 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9395
9396 /* If we don't have a symtab, we can just skip this case. */
9397 if (cust == NULL)
9398 return;
9399
9400 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9401 NULL, xcalloc, xfree);
9402 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9403 NULL, xcalloc, xfree);
9404
9405 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9406 {
9407 recursively_compute_inclusions (&result_symtabs, all_children,
9408 all_type_symtabs, ptr, cust);
9409 }
9410
9411 /* Now we have a transitive closure of all the included symtabs. */
9412 len = result_symtabs.size ();
9413 cust->includes
9414 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9415 struct compunit_symtab *, len + 1);
9416 memcpy (cust->includes, result_symtabs.data (),
9417 len * sizeof (compunit_symtab *));
9418 cust->includes[len] = NULL;
9419
9420 htab_delete (all_children);
9421 htab_delete (all_type_symtabs);
9422 }
9423 }
9424
9425 /* Compute the 'includes' field for the symtabs of all the CUs we just
9426 read. */
9427
9428 static void
9429 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9430 {
9431 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9432 {
9433 if (! iter->is_debug_types)
9434 compute_compunit_symtab_includes (iter);
9435 }
9436
9437 dwarf2_per_objfile->just_read_cus.clear ();
9438 }
9439
9440 /* Generate full symbol information for PER_CU, whose DIEs have
9441 already been loaded into memory. */
9442
9443 static void
9444 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9445 enum language pretend_language)
9446 {
9447 struct dwarf2_cu *cu = per_cu->cu;
9448 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9449 struct objfile *objfile = dwarf2_per_objfile->objfile;
9450 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9451 CORE_ADDR lowpc, highpc;
9452 struct compunit_symtab *cust;
9453 CORE_ADDR baseaddr;
9454 struct block *static_block;
9455 CORE_ADDR addr;
9456
9457 baseaddr = objfile->text_section_offset ();
9458
9459 /* Clear the list here in case something was left over. */
9460 cu->method_list.clear ();
9461
9462 cu->language = pretend_language;
9463 cu->language_defn = language_def (cu->language);
9464
9465 /* Do line number decoding in read_file_scope () */
9466 process_die (cu->dies, cu);
9467
9468 /* For now fudge the Go package. */
9469 if (cu->language == language_go)
9470 fixup_go_packaging (cu);
9471
9472 /* Now that we have processed all the DIEs in the CU, all the types
9473 should be complete, and it should now be safe to compute all of the
9474 physnames. */
9475 compute_delayed_physnames (cu);
9476
9477 if (cu->language == language_rust)
9478 rust_union_quirks (cu);
9479
9480 /* Some compilers don't define a DW_AT_high_pc attribute for the
9481 compilation unit. If the DW_AT_high_pc is missing, synthesize
9482 it, by scanning the DIE's below the compilation unit. */
9483 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9484
9485 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9486 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9487
9488 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9489 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9490 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9491 addrmap to help ensure it has an accurate map of pc values belonging to
9492 this comp unit. */
9493 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9494
9495 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9496 SECT_OFF_TEXT (objfile),
9497 0);
9498
9499 if (cust != NULL)
9500 {
9501 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9502
9503 /* Set symtab language to language from DW_AT_language. If the
9504 compilation is from a C file generated by language preprocessors, do
9505 not set the language if it was already deduced by start_subfile. */
9506 if (!(cu->language == language_c
9507 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9508 COMPUNIT_FILETABS (cust)->language = cu->language;
9509
9510 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9511 produce DW_AT_location with location lists but it can be possibly
9512 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9513 there were bugs in prologue debug info, fixed later in GCC-4.5
9514 by "unwind info for epilogues" patch (which is not directly related).
9515
9516 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9517 needed, it would be wrong due to missing DW_AT_producer there.
9518
9519 Still one can confuse GDB by using non-standard GCC compilation
9520 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9521 */
9522 if (cu->has_loclist && gcc_4_minor >= 5)
9523 cust->locations_valid = 1;
9524
9525 if (gcc_4_minor >= 5)
9526 cust->epilogue_unwind_valid = 1;
9527
9528 cust->call_site_htab = cu->call_site_htab;
9529 }
9530
9531 if (dwarf2_per_objfile->using_index)
9532 per_cu->v.quick->compunit_symtab = cust;
9533 else
9534 {
9535 dwarf2_psymtab *pst = per_cu->v.psymtab;
9536 pst->compunit_symtab = cust;
9537 pst->readin = true;
9538 }
9539
9540 /* Push it for inclusion processing later. */
9541 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9542
9543 /* Not needed any more. */
9544 cu->reset_builder ();
9545 }
9546
9547 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9548 already been loaded into memory. */
9549
9550 static void
9551 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9552 enum language pretend_language)
9553 {
9554 struct dwarf2_cu *cu = per_cu->cu;
9555 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9556 struct objfile *objfile = dwarf2_per_objfile->objfile;
9557 struct compunit_symtab *cust;
9558 struct signatured_type *sig_type;
9559
9560 gdb_assert (per_cu->is_debug_types);
9561 sig_type = (struct signatured_type *) per_cu;
9562
9563 /* Clear the list here in case something was left over. */
9564 cu->method_list.clear ();
9565
9566 cu->language = pretend_language;
9567 cu->language_defn = language_def (cu->language);
9568
9569 /* The symbol tables are set up in read_type_unit_scope. */
9570 process_die (cu->dies, cu);
9571
9572 /* For now fudge the Go package. */
9573 if (cu->language == language_go)
9574 fixup_go_packaging (cu);
9575
9576 /* Now that we have processed all the DIEs in the CU, all the types
9577 should be complete, and it should now be safe to compute all of the
9578 physnames. */
9579 compute_delayed_physnames (cu);
9580
9581 if (cu->language == language_rust)
9582 rust_union_quirks (cu);
9583
9584 /* TUs share symbol tables.
9585 If this is the first TU to use this symtab, complete the construction
9586 of it with end_expandable_symtab. Otherwise, complete the addition of
9587 this TU's symbols to the existing symtab. */
9588 if (sig_type->type_unit_group->compunit_symtab == NULL)
9589 {
9590 buildsym_compunit *builder = cu->get_builder ();
9591 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9592 sig_type->type_unit_group->compunit_symtab = cust;
9593
9594 if (cust != NULL)
9595 {
9596 /* Set symtab language to language from DW_AT_language. If the
9597 compilation is from a C file generated by language preprocessors,
9598 do not set the language if it was already deduced by
9599 start_subfile. */
9600 if (!(cu->language == language_c
9601 && COMPUNIT_FILETABS (cust)->language != language_c))
9602 COMPUNIT_FILETABS (cust)->language = cu->language;
9603 }
9604 }
9605 else
9606 {
9607 cu->get_builder ()->augment_type_symtab ();
9608 cust = sig_type->type_unit_group->compunit_symtab;
9609 }
9610
9611 if (dwarf2_per_objfile->using_index)
9612 per_cu->v.quick->compunit_symtab = cust;
9613 else
9614 {
9615 dwarf2_psymtab *pst = per_cu->v.psymtab;
9616 pst->compunit_symtab = cust;
9617 pst->readin = true;
9618 }
9619
9620 /* Not needed any more. */
9621 cu->reset_builder ();
9622 }
9623
9624 /* Process an imported unit DIE. */
9625
9626 static void
9627 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9628 {
9629 struct attribute *attr;
9630
9631 /* For now we don't handle imported units in type units. */
9632 if (cu->per_cu->is_debug_types)
9633 {
9634 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9635 " supported in type units [in module %s]"),
9636 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9637 }
9638
9639 attr = dwarf2_attr (die, DW_AT_import, cu);
9640 if (attr != NULL)
9641 {
9642 sect_offset sect_off = attr->get_ref_die_offset ();
9643 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9644 dwarf2_per_cu_data *per_cu
9645 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9646 cu->per_cu->dwarf2_per_objfile);
9647
9648 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9649 into another compilation unit, at root level. Regard this as a hint,
9650 and ignore it. */
9651 if (die->parent && die->parent->parent == NULL
9652 && per_cu->unit_type == DW_UT_compile
9653 && per_cu->lang == language_cplus)
9654 return;
9655
9656 /* If necessary, add it to the queue and load its DIEs. */
9657 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9658 load_full_comp_unit (per_cu, false, cu->language);
9659
9660 cu->per_cu->imported_symtabs_push (per_cu);
9661 }
9662 }
9663
9664 /* RAII object that represents a process_die scope: i.e.,
9665 starts/finishes processing a DIE. */
9666 class process_die_scope
9667 {
9668 public:
9669 process_die_scope (die_info *die, dwarf2_cu *cu)
9670 : m_die (die), m_cu (cu)
9671 {
9672 /* We should only be processing DIEs not already in process. */
9673 gdb_assert (!m_die->in_process);
9674 m_die->in_process = true;
9675 }
9676
9677 ~process_die_scope ()
9678 {
9679 m_die->in_process = false;
9680
9681 /* If we're done processing the DIE for the CU that owns the line
9682 header, we don't need the line header anymore. */
9683 if (m_cu->line_header_die_owner == m_die)
9684 {
9685 delete m_cu->line_header;
9686 m_cu->line_header = NULL;
9687 m_cu->line_header_die_owner = NULL;
9688 }
9689 }
9690
9691 private:
9692 die_info *m_die;
9693 dwarf2_cu *m_cu;
9694 };
9695
9696 /* Process a die and its children. */
9697
9698 static void
9699 process_die (struct die_info *die, struct dwarf2_cu *cu)
9700 {
9701 process_die_scope scope (die, cu);
9702
9703 switch (die->tag)
9704 {
9705 case DW_TAG_padding:
9706 break;
9707 case DW_TAG_compile_unit:
9708 case DW_TAG_partial_unit:
9709 read_file_scope (die, cu);
9710 break;
9711 case DW_TAG_type_unit:
9712 read_type_unit_scope (die, cu);
9713 break;
9714 case DW_TAG_subprogram:
9715 /* Nested subprograms in Fortran get a prefix. */
9716 if (cu->language == language_fortran
9717 && die->parent != NULL
9718 && die->parent->tag == DW_TAG_subprogram)
9719 cu->processing_has_namespace_info = true;
9720 /* Fall through. */
9721 case DW_TAG_inlined_subroutine:
9722 read_func_scope (die, cu);
9723 break;
9724 case DW_TAG_lexical_block:
9725 case DW_TAG_try_block:
9726 case DW_TAG_catch_block:
9727 read_lexical_block_scope (die, cu);
9728 break;
9729 case DW_TAG_call_site:
9730 case DW_TAG_GNU_call_site:
9731 read_call_site_scope (die, cu);
9732 break;
9733 case DW_TAG_class_type:
9734 case DW_TAG_interface_type:
9735 case DW_TAG_structure_type:
9736 case DW_TAG_union_type:
9737 process_structure_scope (die, cu);
9738 break;
9739 case DW_TAG_enumeration_type:
9740 process_enumeration_scope (die, cu);
9741 break;
9742
9743 /* These dies have a type, but processing them does not create
9744 a symbol or recurse to process the children. Therefore we can
9745 read them on-demand through read_type_die. */
9746 case DW_TAG_subroutine_type:
9747 case DW_TAG_set_type:
9748 case DW_TAG_array_type:
9749 case DW_TAG_pointer_type:
9750 case DW_TAG_ptr_to_member_type:
9751 case DW_TAG_reference_type:
9752 case DW_TAG_rvalue_reference_type:
9753 case DW_TAG_string_type:
9754 break;
9755
9756 case DW_TAG_base_type:
9757 case DW_TAG_subrange_type:
9758 case DW_TAG_typedef:
9759 /* Add a typedef symbol for the type definition, if it has a
9760 DW_AT_name. */
9761 new_symbol (die, read_type_die (die, cu), cu);
9762 break;
9763 case DW_TAG_common_block:
9764 read_common_block (die, cu);
9765 break;
9766 case DW_TAG_common_inclusion:
9767 break;
9768 case DW_TAG_namespace:
9769 cu->processing_has_namespace_info = true;
9770 read_namespace (die, cu);
9771 break;
9772 case DW_TAG_module:
9773 cu->processing_has_namespace_info = true;
9774 read_module (die, cu);
9775 break;
9776 case DW_TAG_imported_declaration:
9777 cu->processing_has_namespace_info = true;
9778 if (read_namespace_alias (die, cu))
9779 break;
9780 /* The declaration is not a global namespace alias. */
9781 /* Fall through. */
9782 case DW_TAG_imported_module:
9783 cu->processing_has_namespace_info = true;
9784 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9785 || cu->language != language_fortran))
9786 complaint (_("Tag '%s' has unexpected children"),
9787 dwarf_tag_name (die->tag));
9788 read_import_statement (die, cu);
9789 break;
9790
9791 case DW_TAG_imported_unit:
9792 process_imported_unit_die (die, cu);
9793 break;
9794
9795 case DW_TAG_variable:
9796 read_variable (die, cu);
9797 break;
9798
9799 default:
9800 new_symbol (die, NULL, cu);
9801 break;
9802 }
9803 }
9804 \f
9805 /* DWARF name computation. */
9806
9807 /* A helper function for dwarf2_compute_name which determines whether DIE
9808 needs to have the name of the scope prepended to the name listed in the
9809 die. */
9810
9811 static int
9812 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9813 {
9814 struct attribute *attr;
9815
9816 switch (die->tag)
9817 {
9818 case DW_TAG_namespace:
9819 case DW_TAG_typedef:
9820 case DW_TAG_class_type:
9821 case DW_TAG_interface_type:
9822 case DW_TAG_structure_type:
9823 case DW_TAG_union_type:
9824 case DW_TAG_enumeration_type:
9825 case DW_TAG_enumerator:
9826 case DW_TAG_subprogram:
9827 case DW_TAG_inlined_subroutine:
9828 case DW_TAG_member:
9829 case DW_TAG_imported_declaration:
9830 return 1;
9831
9832 case DW_TAG_variable:
9833 case DW_TAG_constant:
9834 /* We only need to prefix "globally" visible variables. These include
9835 any variable marked with DW_AT_external or any variable that
9836 lives in a namespace. [Variables in anonymous namespaces
9837 require prefixing, but they are not DW_AT_external.] */
9838
9839 if (dwarf2_attr (die, DW_AT_specification, cu))
9840 {
9841 struct dwarf2_cu *spec_cu = cu;
9842
9843 return die_needs_namespace (die_specification (die, &spec_cu),
9844 spec_cu);
9845 }
9846
9847 attr = dwarf2_attr (die, DW_AT_external, cu);
9848 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9849 && die->parent->tag != DW_TAG_module)
9850 return 0;
9851 /* A variable in a lexical block of some kind does not need a
9852 namespace, even though in C++ such variables may be external
9853 and have a mangled name. */
9854 if (die->parent->tag == DW_TAG_lexical_block
9855 || die->parent->tag == DW_TAG_try_block
9856 || die->parent->tag == DW_TAG_catch_block
9857 || die->parent->tag == DW_TAG_subprogram)
9858 return 0;
9859 return 1;
9860
9861 default:
9862 return 0;
9863 }
9864 }
9865
9866 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9867 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9868 defined for the given DIE. */
9869
9870 static struct attribute *
9871 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9872 {
9873 struct attribute *attr;
9874
9875 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9876 if (attr == NULL)
9877 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9878
9879 return attr;
9880 }
9881
9882 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9883 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9884 defined for the given DIE. */
9885
9886 static const char *
9887 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9888 {
9889 const char *linkage_name;
9890
9891 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9892 if (linkage_name == NULL)
9893 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9894
9895 return linkage_name;
9896 }
9897
9898 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9899 compute the physname for the object, which include a method's:
9900 - formal parameters (C++),
9901 - receiver type (Go),
9902
9903 The term "physname" is a bit confusing.
9904 For C++, for example, it is the demangled name.
9905 For Go, for example, it's the mangled name.
9906
9907 For Ada, return the DIE's linkage name rather than the fully qualified
9908 name. PHYSNAME is ignored..
9909
9910 The result is allocated on the objfile_obstack and canonicalized. */
9911
9912 static const char *
9913 dwarf2_compute_name (const char *name,
9914 struct die_info *die, struct dwarf2_cu *cu,
9915 int physname)
9916 {
9917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9918
9919 if (name == NULL)
9920 name = dwarf2_name (die, cu);
9921
9922 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9923 but otherwise compute it by typename_concat inside GDB.
9924 FIXME: Actually this is not really true, or at least not always true.
9925 It's all very confusing. compute_and_set_names doesn't try to demangle
9926 Fortran names because there is no mangling standard. So new_symbol
9927 will set the demangled name to the result of dwarf2_full_name, and it is
9928 the demangled name that GDB uses if it exists. */
9929 if (cu->language == language_ada
9930 || (cu->language == language_fortran && physname))
9931 {
9932 /* For Ada unit, we prefer the linkage name over the name, as
9933 the former contains the exported name, which the user expects
9934 to be able to reference. Ideally, we want the user to be able
9935 to reference this entity using either natural or linkage name,
9936 but we haven't started looking at this enhancement yet. */
9937 const char *linkage_name = dw2_linkage_name (die, cu);
9938
9939 if (linkage_name != NULL)
9940 return linkage_name;
9941 }
9942
9943 /* These are the only languages we know how to qualify names in. */
9944 if (name != NULL
9945 && (cu->language == language_cplus
9946 || cu->language == language_fortran || cu->language == language_d
9947 || cu->language == language_rust))
9948 {
9949 if (die_needs_namespace (die, cu))
9950 {
9951 const char *prefix;
9952 const char *canonical_name = NULL;
9953
9954 string_file buf;
9955
9956 prefix = determine_prefix (die, cu);
9957 if (*prefix != '\0')
9958 {
9959 gdb::unique_xmalloc_ptr<char> prefixed_name
9960 (typename_concat (NULL, prefix, name, physname, cu));
9961
9962 buf.puts (prefixed_name.get ());
9963 }
9964 else
9965 buf.puts (name);
9966
9967 /* Template parameters may be specified in the DIE's DW_AT_name, or
9968 as children with DW_TAG_template_type_param or
9969 DW_TAG_value_type_param. If the latter, add them to the name
9970 here. If the name already has template parameters, then
9971 skip this step; some versions of GCC emit both, and
9972 it is more efficient to use the pre-computed name.
9973
9974 Something to keep in mind about this process: it is very
9975 unlikely, or in some cases downright impossible, to produce
9976 something that will match the mangled name of a function.
9977 If the definition of the function has the same debug info,
9978 we should be able to match up with it anyway. But fallbacks
9979 using the minimal symbol, for instance to find a method
9980 implemented in a stripped copy of libstdc++, will not work.
9981 If we do not have debug info for the definition, we will have to
9982 match them up some other way.
9983
9984 When we do name matching there is a related problem with function
9985 templates; two instantiated function templates are allowed to
9986 differ only by their return types, which we do not add here. */
9987
9988 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9989 {
9990 struct attribute *attr;
9991 struct die_info *child;
9992 int first = 1;
9993
9994 die->building_fullname = 1;
9995
9996 for (child = die->child; child != NULL; child = child->sibling)
9997 {
9998 struct type *type;
9999 LONGEST value;
10000 const gdb_byte *bytes;
10001 struct dwarf2_locexpr_baton *baton;
10002 struct value *v;
10003
10004 if (child->tag != DW_TAG_template_type_param
10005 && child->tag != DW_TAG_template_value_param)
10006 continue;
10007
10008 if (first)
10009 {
10010 buf.puts ("<");
10011 first = 0;
10012 }
10013 else
10014 buf.puts (", ");
10015
10016 attr = dwarf2_attr (child, DW_AT_type, cu);
10017 if (attr == NULL)
10018 {
10019 complaint (_("template parameter missing DW_AT_type"));
10020 buf.puts ("UNKNOWN_TYPE");
10021 continue;
10022 }
10023 type = die_type (child, cu);
10024
10025 if (child->tag == DW_TAG_template_type_param)
10026 {
10027 c_print_type (type, "", &buf, -1, 0, cu->language,
10028 &type_print_raw_options);
10029 continue;
10030 }
10031
10032 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10033 if (attr == NULL)
10034 {
10035 complaint (_("template parameter missing "
10036 "DW_AT_const_value"));
10037 buf.puts ("UNKNOWN_VALUE");
10038 continue;
10039 }
10040
10041 dwarf2_const_value_attr (attr, type, name,
10042 &cu->comp_unit_obstack, cu,
10043 &value, &bytes, &baton);
10044
10045 if (TYPE_NOSIGN (type))
10046 /* GDB prints characters as NUMBER 'CHAR'. If that's
10047 changed, this can use value_print instead. */
10048 c_printchar (value, type, &buf);
10049 else
10050 {
10051 struct value_print_options opts;
10052
10053 if (baton != NULL)
10054 v = dwarf2_evaluate_loc_desc (type, NULL,
10055 baton->data,
10056 baton->size,
10057 baton->per_cu);
10058 else if (bytes != NULL)
10059 {
10060 v = allocate_value (type);
10061 memcpy (value_contents_writeable (v), bytes,
10062 TYPE_LENGTH (type));
10063 }
10064 else
10065 v = value_from_longest (type, value);
10066
10067 /* Specify decimal so that we do not depend on
10068 the radix. */
10069 get_formatted_print_options (&opts, 'd');
10070 opts.raw = 1;
10071 value_print (v, &buf, &opts);
10072 release_value (v);
10073 }
10074 }
10075
10076 die->building_fullname = 0;
10077
10078 if (!first)
10079 {
10080 /* Close the argument list, with a space if necessary
10081 (nested templates). */
10082 if (!buf.empty () && buf.string ().back () == '>')
10083 buf.puts (" >");
10084 else
10085 buf.puts (">");
10086 }
10087 }
10088
10089 /* For C++ methods, append formal parameter type
10090 information, if PHYSNAME. */
10091
10092 if (physname && die->tag == DW_TAG_subprogram
10093 && cu->language == language_cplus)
10094 {
10095 struct type *type = read_type_die (die, cu);
10096
10097 c_type_print_args (type, &buf, 1, cu->language,
10098 &type_print_raw_options);
10099
10100 if (cu->language == language_cplus)
10101 {
10102 /* Assume that an artificial first parameter is
10103 "this", but do not crash if it is not. RealView
10104 marks unnamed (and thus unused) parameters as
10105 artificial; there is no way to differentiate
10106 the two cases. */
10107 if (TYPE_NFIELDS (type) > 0
10108 && TYPE_FIELD_ARTIFICIAL (type, 0)
10109 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10110 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10111 0))))
10112 buf.puts (" const");
10113 }
10114 }
10115
10116 const std::string &intermediate_name = buf.string ();
10117
10118 if (cu->language == language_cplus)
10119 canonical_name
10120 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10121 objfile);
10122
10123 /* If we only computed INTERMEDIATE_NAME, or if
10124 INTERMEDIATE_NAME is already canonical, then we need to
10125 intern it. */
10126 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10127 name = objfile->intern (intermediate_name);
10128 else
10129 name = canonical_name;
10130 }
10131 }
10132
10133 return name;
10134 }
10135
10136 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10137 If scope qualifiers are appropriate they will be added. The result
10138 will be allocated on the storage_obstack, or NULL if the DIE does
10139 not have a name. NAME may either be from a previous call to
10140 dwarf2_name or NULL.
10141
10142 The output string will be canonicalized (if C++). */
10143
10144 static const char *
10145 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10146 {
10147 return dwarf2_compute_name (name, die, cu, 0);
10148 }
10149
10150 /* Construct a physname for the given DIE in CU. NAME may either be
10151 from a previous call to dwarf2_name or NULL. The result will be
10152 allocated on the objfile_objstack or NULL if the DIE does not have a
10153 name.
10154
10155 The output string will be canonicalized (if C++). */
10156
10157 static const char *
10158 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10159 {
10160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10161 const char *retval, *mangled = NULL, *canon = NULL;
10162 int need_copy = 1;
10163
10164 /* In this case dwarf2_compute_name is just a shortcut not building anything
10165 on its own. */
10166 if (!die_needs_namespace (die, cu))
10167 return dwarf2_compute_name (name, die, cu, 1);
10168
10169 mangled = dw2_linkage_name (die, cu);
10170
10171 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10172 See https://github.com/rust-lang/rust/issues/32925. */
10173 if (cu->language == language_rust && mangled != NULL
10174 && strchr (mangled, '{') != NULL)
10175 mangled = NULL;
10176
10177 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10178 has computed. */
10179 gdb::unique_xmalloc_ptr<char> demangled;
10180 if (mangled != NULL)
10181 {
10182
10183 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10184 {
10185 /* Do nothing (do not demangle the symbol name). */
10186 }
10187 else if (cu->language == language_go)
10188 {
10189 /* This is a lie, but we already lie to the caller new_symbol.
10190 new_symbol assumes we return the mangled name.
10191 This just undoes that lie until things are cleaned up. */
10192 }
10193 else
10194 {
10195 /* Use DMGL_RET_DROP for C++ template functions to suppress
10196 their return type. It is easier for GDB users to search
10197 for such functions as `name(params)' than `long name(params)'.
10198 In such case the minimal symbol names do not match the full
10199 symbol names but for template functions there is never a need
10200 to look up their definition from their declaration so
10201 the only disadvantage remains the minimal symbol variant
10202 `long name(params)' does not have the proper inferior type. */
10203 demangled.reset (gdb_demangle (mangled,
10204 (DMGL_PARAMS | DMGL_ANSI
10205 | DMGL_RET_DROP)));
10206 }
10207 if (demangled)
10208 canon = demangled.get ();
10209 else
10210 {
10211 canon = mangled;
10212 need_copy = 0;
10213 }
10214 }
10215
10216 if (canon == NULL || check_physname)
10217 {
10218 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10219
10220 if (canon != NULL && strcmp (physname, canon) != 0)
10221 {
10222 /* It may not mean a bug in GDB. The compiler could also
10223 compute DW_AT_linkage_name incorrectly. But in such case
10224 GDB would need to be bug-to-bug compatible. */
10225
10226 complaint (_("Computed physname <%s> does not match demangled <%s> "
10227 "(from linkage <%s>) - DIE at %s [in module %s]"),
10228 physname, canon, mangled, sect_offset_str (die->sect_off),
10229 objfile_name (objfile));
10230
10231 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10232 is available here - over computed PHYSNAME. It is safer
10233 against both buggy GDB and buggy compilers. */
10234
10235 retval = canon;
10236 }
10237 else
10238 {
10239 retval = physname;
10240 need_copy = 0;
10241 }
10242 }
10243 else
10244 retval = canon;
10245
10246 if (need_copy)
10247 retval = objfile->intern (retval);
10248
10249 return retval;
10250 }
10251
10252 /* Inspect DIE in CU for a namespace alias. If one exists, record
10253 a new symbol for it.
10254
10255 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10256
10257 static int
10258 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10259 {
10260 struct attribute *attr;
10261
10262 /* If the die does not have a name, this is not a namespace
10263 alias. */
10264 attr = dwarf2_attr (die, DW_AT_name, cu);
10265 if (attr != NULL)
10266 {
10267 int num;
10268 struct die_info *d = die;
10269 struct dwarf2_cu *imported_cu = cu;
10270
10271 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10272 keep inspecting DIEs until we hit the underlying import. */
10273 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10274 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10275 {
10276 attr = dwarf2_attr (d, DW_AT_import, cu);
10277 if (attr == NULL)
10278 break;
10279
10280 d = follow_die_ref (d, attr, &imported_cu);
10281 if (d->tag != DW_TAG_imported_declaration)
10282 break;
10283 }
10284
10285 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10286 {
10287 complaint (_("DIE at %s has too many recursively imported "
10288 "declarations"), sect_offset_str (d->sect_off));
10289 return 0;
10290 }
10291
10292 if (attr != NULL)
10293 {
10294 struct type *type;
10295 sect_offset sect_off = attr->get_ref_die_offset ();
10296
10297 type = get_die_type_at_offset (sect_off, cu->per_cu);
10298 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10299 {
10300 /* This declaration is a global namespace alias. Add
10301 a symbol for it whose type is the aliased namespace. */
10302 new_symbol (die, type, cu);
10303 return 1;
10304 }
10305 }
10306 }
10307
10308 return 0;
10309 }
10310
10311 /* Return the using directives repository (global or local?) to use in the
10312 current context for CU.
10313
10314 For Ada, imported declarations can materialize renamings, which *may* be
10315 global. However it is impossible (for now?) in DWARF to distinguish
10316 "external" imported declarations and "static" ones. As all imported
10317 declarations seem to be static in all other languages, make them all CU-wide
10318 global only in Ada. */
10319
10320 static struct using_direct **
10321 using_directives (struct dwarf2_cu *cu)
10322 {
10323 if (cu->language == language_ada
10324 && cu->get_builder ()->outermost_context_p ())
10325 return cu->get_builder ()->get_global_using_directives ();
10326 else
10327 return cu->get_builder ()->get_local_using_directives ();
10328 }
10329
10330 /* Read the import statement specified by the given die and record it. */
10331
10332 static void
10333 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10334 {
10335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10336 struct attribute *import_attr;
10337 struct die_info *imported_die, *child_die;
10338 struct dwarf2_cu *imported_cu;
10339 const char *imported_name;
10340 const char *imported_name_prefix;
10341 const char *canonical_name;
10342 const char *import_alias;
10343 const char *imported_declaration = NULL;
10344 const char *import_prefix;
10345 std::vector<const char *> excludes;
10346
10347 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10348 if (import_attr == NULL)
10349 {
10350 complaint (_("Tag '%s' has no DW_AT_import"),
10351 dwarf_tag_name (die->tag));
10352 return;
10353 }
10354
10355 imported_cu = cu;
10356 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10357 imported_name = dwarf2_name (imported_die, imported_cu);
10358 if (imported_name == NULL)
10359 {
10360 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10361
10362 The import in the following code:
10363 namespace A
10364 {
10365 typedef int B;
10366 }
10367
10368 int main ()
10369 {
10370 using A::B;
10371 B b;
10372 return b;
10373 }
10374
10375 ...
10376 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10377 <52> DW_AT_decl_file : 1
10378 <53> DW_AT_decl_line : 6
10379 <54> DW_AT_import : <0x75>
10380 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10381 <59> DW_AT_name : B
10382 <5b> DW_AT_decl_file : 1
10383 <5c> DW_AT_decl_line : 2
10384 <5d> DW_AT_type : <0x6e>
10385 ...
10386 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10387 <76> DW_AT_byte_size : 4
10388 <77> DW_AT_encoding : 5 (signed)
10389
10390 imports the wrong die ( 0x75 instead of 0x58 ).
10391 This case will be ignored until the gcc bug is fixed. */
10392 return;
10393 }
10394
10395 /* Figure out the local name after import. */
10396 import_alias = dwarf2_name (die, cu);
10397
10398 /* Figure out where the statement is being imported to. */
10399 import_prefix = determine_prefix (die, cu);
10400
10401 /* Figure out what the scope of the imported die is and prepend it
10402 to the name of the imported die. */
10403 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10404
10405 if (imported_die->tag != DW_TAG_namespace
10406 && imported_die->tag != DW_TAG_module)
10407 {
10408 imported_declaration = imported_name;
10409 canonical_name = imported_name_prefix;
10410 }
10411 else if (strlen (imported_name_prefix) > 0)
10412 canonical_name = obconcat (&objfile->objfile_obstack,
10413 imported_name_prefix,
10414 (cu->language == language_d ? "." : "::"),
10415 imported_name, (char *) NULL);
10416 else
10417 canonical_name = imported_name;
10418
10419 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10420 for (child_die = die->child; child_die && child_die->tag;
10421 child_die = child_die->sibling)
10422 {
10423 /* DWARF-4: A Fortran use statement with a “rename list” may be
10424 represented by an imported module entry with an import attribute
10425 referring to the module and owned entries corresponding to those
10426 entities that are renamed as part of being imported. */
10427
10428 if (child_die->tag != DW_TAG_imported_declaration)
10429 {
10430 complaint (_("child DW_TAG_imported_declaration expected "
10431 "- DIE at %s [in module %s]"),
10432 sect_offset_str (child_die->sect_off),
10433 objfile_name (objfile));
10434 continue;
10435 }
10436
10437 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10438 if (import_attr == NULL)
10439 {
10440 complaint (_("Tag '%s' has no DW_AT_import"),
10441 dwarf_tag_name (child_die->tag));
10442 continue;
10443 }
10444
10445 imported_cu = cu;
10446 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10447 &imported_cu);
10448 imported_name = dwarf2_name (imported_die, imported_cu);
10449 if (imported_name == NULL)
10450 {
10451 complaint (_("child DW_TAG_imported_declaration has unknown "
10452 "imported name - DIE at %s [in module %s]"),
10453 sect_offset_str (child_die->sect_off),
10454 objfile_name (objfile));
10455 continue;
10456 }
10457
10458 excludes.push_back (imported_name);
10459
10460 process_die (child_die, cu);
10461 }
10462
10463 add_using_directive (using_directives (cu),
10464 import_prefix,
10465 canonical_name,
10466 import_alias,
10467 imported_declaration,
10468 excludes,
10469 0,
10470 &objfile->objfile_obstack);
10471 }
10472
10473 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10474 types, but gives them a size of zero. Starting with version 14,
10475 ICC is compatible with GCC. */
10476
10477 static bool
10478 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10479 {
10480 if (!cu->checked_producer)
10481 check_producer (cu);
10482
10483 return cu->producer_is_icc_lt_14;
10484 }
10485
10486 /* ICC generates a DW_AT_type for C void functions. This was observed on
10487 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10488 which says that void functions should not have a DW_AT_type. */
10489
10490 static bool
10491 producer_is_icc (struct dwarf2_cu *cu)
10492 {
10493 if (!cu->checked_producer)
10494 check_producer (cu);
10495
10496 return cu->producer_is_icc;
10497 }
10498
10499 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10500 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10501 this, it was first present in GCC release 4.3.0. */
10502
10503 static bool
10504 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10505 {
10506 if (!cu->checked_producer)
10507 check_producer (cu);
10508
10509 return cu->producer_is_gcc_lt_4_3;
10510 }
10511
10512 static file_and_directory
10513 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10514 {
10515 file_and_directory res;
10516
10517 /* Find the filename. Do not use dwarf2_name here, since the filename
10518 is not a source language identifier. */
10519 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10520 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10521
10522 if (res.comp_dir == NULL
10523 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10524 && IS_ABSOLUTE_PATH (res.name))
10525 {
10526 res.comp_dir_storage = ldirname (res.name);
10527 if (!res.comp_dir_storage.empty ())
10528 res.comp_dir = res.comp_dir_storage.c_str ();
10529 }
10530 if (res.comp_dir != NULL)
10531 {
10532 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10533 directory, get rid of it. */
10534 const char *cp = strchr (res.comp_dir, ':');
10535
10536 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10537 res.comp_dir = cp + 1;
10538 }
10539
10540 if (res.name == NULL)
10541 res.name = "<unknown>";
10542
10543 return res;
10544 }
10545
10546 /* Handle DW_AT_stmt_list for a compilation unit.
10547 DIE is the DW_TAG_compile_unit die for CU.
10548 COMP_DIR is the compilation directory. LOWPC is passed to
10549 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10550
10551 static void
10552 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10553 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10554 {
10555 struct dwarf2_per_objfile *dwarf2_per_objfile
10556 = cu->per_cu->dwarf2_per_objfile;
10557 struct attribute *attr;
10558 struct line_header line_header_local;
10559 hashval_t line_header_local_hash;
10560 void **slot;
10561 int decode_mapping;
10562
10563 gdb_assert (! cu->per_cu->is_debug_types);
10564
10565 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10566 if (attr == NULL)
10567 return;
10568
10569 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10570
10571 /* The line header hash table is only created if needed (it exists to
10572 prevent redundant reading of the line table for partial_units).
10573 If we're given a partial_unit, we'll need it. If we're given a
10574 compile_unit, then use the line header hash table if it's already
10575 created, but don't create one just yet. */
10576
10577 if (dwarf2_per_objfile->line_header_hash == NULL
10578 && die->tag == DW_TAG_partial_unit)
10579 {
10580 dwarf2_per_objfile->line_header_hash
10581 .reset (htab_create_alloc (127, line_header_hash_voidp,
10582 line_header_eq_voidp,
10583 free_line_header_voidp,
10584 xcalloc, xfree));
10585 }
10586
10587 line_header_local.sect_off = line_offset;
10588 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10589 line_header_local_hash = line_header_hash (&line_header_local);
10590 if (dwarf2_per_objfile->line_header_hash != NULL)
10591 {
10592 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10593 &line_header_local,
10594 line_header_local_hash, NO_INSERT);
10595
10596 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10597 is not present in *SLOT (since if there is something in *SLOT then
10598 it will be for a partial_unit). */
10599 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10600 {
10601 gdb_assert (*slot != NULL);
10602 cu->line_header = (struct line_header *) *slot;
10603 return;
10604 }
10605 }
10606
10607 /* dwarf_decode_line_header does not yet provide sufficient information.
10608 We always have to call also dwarf_decode_lines for it. */
10609 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10610 if (lh == NULL)
10611 return;
10612
10613 cu->line_header = lh.release ();
10614 cu->line_header_die_owner = die;
10615
10616 if (dwarf2_per_objfile->line_header_hash == NULL)
10617 slot = NULL;
10618 else
10619 {
10620 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10621 &line_header_local,
10622 line_header_local_hash, INSERT);
10623 gdb_assert (slot != NULL);
10624 }
10625 if (slot != NULL && *slot == NULL)
10626 {
10627 /* This newly decoded line number information unit will be owned
10628 by line_header_hash hash table. */
10629 *slot = cu->line_header;
10630 cu->line_header_die_owner = NULL;
10631 }
10632 else
10633 {
10634 /* We cannot free any current entry in (*slot) as that struct line_header
10635 may be already used by multiple CUs. Create only temporary decoded
10636 line_header for this CU - it may happen at most once for each line
10637 number information unit. And if we're not using line_header_hash
10638 then this is what we want as well. */
10639 gdb_assert (die->tag != DW_TAG_partial_unit);
10640 }
10641 decode_mapping = (die->tag != DW_TAG_partial_unit);
10642 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10643 decode_mapping);
10644
10645 }
10646
10647 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10648
10649 static void
10650 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10651 {
10652 struct dwarf2_per_objfile *dwarf2_per_objfile
10653 = cu->per_cu->dwarf2_per_objfile;
10654 struct objfile *objfile = dwarf2_per_objfile->objfile;
10655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10656 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10657 CORE_ADDR highpc = ((CORE_ADDR) 0);
10658 struct attribute *attr;
10659 struct die_info *child_die;
10660 CORE_ADDR baseaddr;
10661
10662 prepare_one_comp_unit (cu, die, cu->language);
10663 baseaddr = objfile->text_section_offset ();
10664
10665 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10666
10667 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10668 from finish_block. */
10669 if (lowpc == ((CORE_ADDR) -1))
10670 lowpc = highpc;
10671 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10672
10673 file_and_directory fnd = find_file_and_directory (die, cu);
10674
10675 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10676 standardised yet. As a workaround for the language detection we fall
10677 back to the DW_AT_producer string. */
10678 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10679 cu->language = language_opencl;
10680
10681 /* Similar hack for Go. */
10682 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10683 set_cu_language (DW_LANG_Go, cu);
10684
10685 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10686
10687 /* Decode line number information if present. We do this before
10688 processing child DIEs, so that the line header table is available
10689 for DW_AT_decl_file. */
10690 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10691
10692 /* Process all dies in compilation unit. */
10693 if (die->child != NULL)
10694 {
10695 child_die = die->child;
10696 while (child_die && child_die->tag)
10697 {
10698 process_die (child_die, cu);
10699 child_die = child_die->sibling;
10700 }
10701 }
10702
10703 /* Decode macro information, if present. Dwarf 2 macro information
10704 refers to information in the line number info statement program
10705 header, so we can only read it if we've read the header
10706 successfully. */
10707 attr = dwarf2_attr (die, DW_AT_macros, cu);
10708 if (attr == NULL)
10709 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10710 if (attr && cu->line_header)
10711 {
10712 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10713 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10714
10715 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10716 }
10717 else
10718 {
10719 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10720 if (attr && cu->line_header)
10721 {
10722 unsigned int macro_offset = DW_UNSND (attr);
10723
10724 dwarf_decode_macros (cu, macro_offset, 0);
10725 }
10726 }
10727 }
10728
10729 void
10730 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10731 {
10732 struct type_unit_group *tu_group;
10733 int first_time;
10734 struct attribute *attr;
10735 unsigned int i;
10736 struct signatured_type *sig_type;
10737
10738 gdb_assert (per_cu->is_debug_types);
10739 sig_type = (struct signatured_type *) per_cu;
10740
10741 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10742
10743 /* If we're using .gdb_index (includes -readnow) then
10744 per_cu->type_unit_group may not have been set up yet. */
10745 if (sig_type->type_unit_group == NULL)
10746 sig_type->type_unit_group = get_type_unit_group (this, attr);
10747 tu_group = sig_type->type_unit_group;
10748
10749 /* If we've already processed this stmt_list there's no real need to
10750 do it again, we could fake it and just recreate the part we need
10751 (file name,index -> symtab mapping). If data shows this optimization
10752 is useful we can do it then. */
10753 first_time = tu_group->compunit_symtab == NULL;
10754
10755 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10756 debug info. */
10757 line_header_up lh;
10758 if (attr != NULL)
10759 {
10760 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10761 lh = dwarf_decode_line_header (line_offset, this);
10762 }
10763 if (lh == NULL)
10764 {
10765 if (first_time)
10766 start_symtab ("", NULL, 0);
10767 else
10768 {
10769 gdb_assert (tu_group->symtabs == NULL);
10770 gdb_assert (m_builder == nullptr);
10771 struct compunit_symtab *cust = tu_group->compunit_symtab;
10772 m_builder.reset (new struct buildsym_compunit
10773 (COMPUNIT_OBJFILE (cust), "",
10774 COMPUNIT_DIRNAME (cust),
10775 compunit_language (cust),
10776 0, cust));
10777 }
10778 return;
10779 }
10780
10781 line_header = lh.release ();
10782 line_header_die_owner = die;
10783
10784 if (first_time)
10785 {
10786 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10787
10788 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10789 still initializing it, and our caller (a few levels up)
10790 process_full_type_unit still needs to know if this is the first
10791 time. */
10792
10793 tu_group->symtabs
10794 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10795 struct symtab *, line_header->file_names_size ());
10796
10797 auto &file_names = line_header->file_names ();
10798 for (i = 0; i < file_names.size (); ++i)
10799 {
10800 file_entry &fe = file_names[i];
10801 dwarf2_start_subfile (this, fe.name,
10802 fe.include_dir (line_header));
10803 buildsym_compunit *b = get_builder ();
10804 if (b->get_current_subfile ()->symtab == NULL)
10805 {
10806 /* NOTE: start_subfile will recognize when it's been
10807 passed a file it has already seen. So we can't
10808 assume there's a simple mapping from
10809 cu->line_header->file_names to subfiles, plus
10810 cu->line_header->file_names may contain dups. */
10811 b->get_current_subfile ()->symtab
10812 = allocate_symtab (cust, b->get_current_subfile ()->name);
10813 }
10814
10815 fe.symtab = b->get_current_subfile ()->symtab;
10816 tu_group->symtabs[i] = fe.symtab;
10817 }
10818 }
10819 else
10820 {
10821 gdb_assert (m_builder == nullptr);
10822 struct compunit_symtab *cust = tu_group->compunit_symtab;
10823 m_builder.reset (new struct buildsym_compunit
10824 (COMPUNIT_OBJFILE (cust), "",
10825 COMPUNIT_DIRNAME (cust),
10826 compunit_language (cust),
10827 0, cust));
10828
10829 auto &file_names = line_header->file_names ();
10830 for (i = 0; i < file_names.size (); ++i)
10831 {
10832 file_entry &fe = file_names[i];
10833 fe.symtab = tu_group->symtabs[i];
10834 }
10835 }
10836
10837 /* The main symtab is allocated last. Type units don't have DW_AT_name
10838 so they don't have a "real" (so to speak) symtab anyway.
10839 There is later code that will assign the main symtab to all symbols
10840 that don't have one. We need to handle the case of a symbol with a
10841 missing symtab (DW_AT_decl_file) anyway. */
10842 }
10843
10844 /* Process DW_TAG_type_unit.
10845 For TUs we want to skip the first top level sibling if it's not the
10846 actual type being defined by this TU. In this case the first top
10847 level sibling is there to provide context only. */
10848
10849 static void
10850 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10851 {
10852 struct die_info *child_die;
10853
10854 prepare_one_comp_unit (cu, die, language_minimal);
10855
10856 /* Initialize (or reinitialize) the machinery for building symtabs.
10857 We do this before processing child DIEs, so that the line header table
10858 is available for DW_AT_decl_file. */
10859 cu->setup_type_unit_groups (die);
10860
10861 if (die->child != NULL)
10862 {
10863 child_die = die->child;
10864 while (child_die && child_die->tag)
10865 {
10866 process_die (child_die, cu);
10867 child_die = child_die->sibling;
10868 }
10869 }
10870 }
10871 \f
10872 /* DWO/DWP files.
10873
10874 http://gcc.gnu.org/wiki/DebugFission
10875 http://gcc.gnu.org/wiki/DebugFissionDWP
10876
10877 To simplify handling of both DWO files ("object" files with the DWARF info)
10878 and DWP files (a file with the DWOs packaged up into one file), we treat
10879 DWP files as having a collection of virtual DWO files. */
10880
10881 static hashval_t
10882 hash_dwo_file (const void *item)
10883 {
10884 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10885 hashval_t hash;
10886
10887 hash = htab_hash_string (dwo_file->dwo_name);
10888 if (dwo_file->comp_dir != NULL)
10889 hash += htab_hash_string (dwo_file->comp_dir);
10890 return hash;
10891 }
10892
10893 static int
10894 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10895 {
10896 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10897 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10898
10899 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10900 return 0;
10901 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10902 return lhs->comp_dir == rhs->comp_dir;
10903 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10904 }
10905
10906 /* Allocate a hash table for DWO files. */
10907
10908 static htab_up
10909 allocate_dwo_file_hash_table ()
10910 {
10911 auto delete_dwo_file = [] (void *item)
10912 {
10913 struct dwo_file *dwo_file = (struct dwo_file *) item;
10914
10915 delete dwo_file;
10916 };
10917
10918 return htab_up (htab_create_alloc (41,
10919 hash_dwo_file,
10920 eq_dwo_file,
10921 delete_dwo_file,
10922 xcalloc, xfree));
10923 }
10924
10925 /* Lookup DWO file DWO_NAME. */
10926
10927 static void **
10928 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10929 const char *dwo_name,
10930 const char *comp_dir)
10931 {
10932 struct dwo_file find_entry;
10933 void **slot;
10934
10935 if (dwarf2_per_objfile->dwo_files == NULL)
10936 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10937
10938 find_entry.dwo_name = dwo_name;
10939 find_entry.comp_dir = comp_dir;
10940 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10941 INSERT);
10942
10943 return slot;
10944 }
10945
10946 static hashval_t
10947 hash_dwo_unit (const void *item)
10948 {
10949 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10950
10951 /* This drops the top 32 bits of the id, but is ok for a hash. */
10952 return dwo_unit->signature;
10953 }
10954
10955 static int
10956 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10957 {
10958 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10959 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10960
10961 /* The signature is assumed to be unique within the DWO file.
10962 So while object file CU dwo_id's always have the value zero,
10963 that's OK, assuming each object file DWO file has only one CU,
10964 and that's the rule for now. */
10965 return lhs->signature == rhs->signature;
10966 }
10967
10968 /* Allocate a hash table for DWO CUs,TUs.
10969 There is one of these tables for each of CUs,TUs for each DWO file. */
10970
10971 static htab_up
10972 allocate_dwo_unit_table ()
10973 {
10974 /* Start out with a pretty small number.
10975 Generally DWO files contain only one CU and maybe some TUs. */
10976 return htab_up (htab_create_alloc (3,
10977 hash_dwo_unit,
10978 eq_dwo_unit,
10979 NULL, xcalloc, xfree));
10980 }
10981
10982 /* die_reader_func for create_dwo_cu. */
10983
10984 static void
10985 create_dwo_cu_reader (const struct die_reader_specs *reader,
10986 const gdb_byte *info_ptr,
10987 struct die_info *comp_unit_die,
10988 struct dwo_file *dwo_file,
10989 struct dwo_unit *dwo_unit)
10990 {
10991 struct dwarf2_cu *cu = reader->cu;
10992 sect_offset sect_off = cu->per_cu->sect_off;
10993 struct dwarf2_section_info *section = cu->per_cu->section;
10994
10995 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10996 if (!signature.has_value ())
10997 {
10998 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10999 " its dwo_id [in module %s]"),
11000 sect_offset_str (sect_off), dwo_file->dwo_name);
11001 return;
11002 }
11003
11004 dwo_unit->dwo_file = dwo_file;
11005 dwo_unit->signature = *signature;
11006 dwo_unit->section = section;
11007 dwo_unit->sect_off = sect_off;
11008 dwo_unit->length = cu->per_cu->length;
11009
11010 if (dwarf_read_debug)
11011 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11012 sect_offset_str (sect_off),
11013 hex_string (dwo_unit->signature));
11014 }
11015
11016 /* Create the dwo_units for the CUs in a DWO_FILE.
11017 Note: This function processes DWO files only, not DWP files. */
11018
11019 static void
11020 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11021 dwarf2_cu *cu, struct dwo_file &dwo_file,
11022 dwarf2_section_info &section, htab_up &cus_htab)
11023 {
11024 struct objfile *objfile = dwarf2_per_objfile->objfile;
11025 const gdb_byte *info_ptr, *end_ptr;
11026
11027 section.read (objfile);
11028 info_ptr = section.buffer;
11029
11030 if (info_ptr == NULL)
11031 return;
11032
11033 if (dwarf_read_debug)
11034 {
11035 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11036 section.get_name (),
11037 section.get_file_name ());
11038 }
11039
11040 end_ptr = info_ptr + section.size;
11041 while (info_ptr < end_ptr)
11042 {
11043 struct dwarf2_per_cu_data per_cu;
11044 struct dwo_unit read_unit {};
11045 struct dwo_unit *dwo_unit;
11046 void **slot;
11047 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11048
11049 memset (&per_cu, 0, sizeof (per_cu));
11050 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11051 per_cu.is_debug_types = 0;
11052 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11053 per_cu.section = &section;
11054
11055 cutu_reader reader (&per_cu, cu, &dwo_file);
11056 if (!reader.dummy_p)
11057 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11058 &dwo_file, &read_unit);
11059 info_ptr += per_cu.length;
11060
11061 // If the unit could not be parsed, skip it.
11062 if (read_unit.dwo_file == NULL)
11063 continue;
11064
11065 if (cus_htab == NULL)
11066 cus_htab = allocate_dwo_unit_table ();
11067
11068 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11069 *dwo_unit = read_unit;
11070 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11071 gdb_assert (slot != NULL);
11072 if (*slot != NULL)
11073 {
11074 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11075 sect_offset dup_sect_off = dup_cu->sect_off;
11076
11077 complaint (_("debug cu entry at offset %s is duplicate to"
11078 " the entry at offset %s, signature %s"),
11079 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11080 hex_string (dwo_unit->signature));
11081 }
11082 *slot = (void *)dwo_unit;
11083 }
11084 }
11085
11086 /* DWP file .debug_{cu,tu}_index section format:
11087 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11088
11089 DWP Version 1:
11090
11091 Both index sections have the same format, and serve to map a 64-bit
11092 signature to a set of section numbers. Each section begins with a header,
11093 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11094 indexes, and a pool of 32-bit section numbers. The index sections will be
11095 aligned at 8-byte boundaries in the file.
11096
11097 The index section header consists of:
11098
11099 V, 32 bit version number
11100 -, 32 bits unused
11101 N, 32 bit number of compilation units or type units in the index
11102 M, 32 bit number of slots in the hash table
11103
11104 Numbers are recorded using the byte order of the application binary.
11105
11106 The hash table begins at offset 16 in the section, and consists of an array
11107 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11108 order of the application binary). Unused slots in the hash table are 0.
11109 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11110
11111 The parallel table begins immediately after the hash table
11112 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11113 array of 32-bit indexes (using the byte order of the application binary),
11114 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11115 table contains a 32-bit index into the pool of section numbers. For unused
11116 hash table slots, the corresponding entry in the parallel table will be 0.
11117
11118 The pool of section numbers begins immediately following the hash table
11119 (at offset 16 + 12 * M from the beginning of the section). The pool of
11120 section numbers consists of an array of 32-bit words (using the byte order
11121 of the application binary). Each item in the array is indexed starting
11122 from 0. The hash table entry provides the index of the first section
11123 number in the set. Additional section numbers in the set follow, and the
11124 set is terminated by a 0 entry (section number 0 is not used in ELF).
11125
11126 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11127 section must be the first entry in the set, and the .debug_abbrev.dwo must
11128 be the second entry. Other members of the set may follow in any order.
11129
11130 ---
11131
11132 DWP Version 2:
11133
11134 DWP Version 2 combines all the .debug_info, etc. sections into one,
11135 and the entries in the index tables are now offsets into these sections.
11136 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11137 section.
11138
11139 Index Section Contents:
11140 Header
11141 Hash Table of Signatures dwp_hash_table.hash_table
11142 Parallel Table of Indices dwp_hash_table.unit_table
11143 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11144 Table of Section Sizes dwp_hash_table.v2.sizes
11145
11146 The index section header consists of:
11147
11148 V, 32 bit version number
11149 L, 32 bit number of columns in the table of section offsets
11150 N, 32 bit number of compilation units or type units in the index
11151 M, 32 bit number of slots in the hash table
11152
11153 Numbers are recorded using the byte order of the application binary.
11154
11155 The hash table has the same format as version 1.
11156 The parallel table of indices has the same format as version 1,
11157 except that the entries are origin-1 indices into the table of sections
11158 offsets and the table of section sizes.
11159
11160 The table of offsets begins immediately following the parallel table
11161 (at offset 16 + 12 * M from the beginning of the section). The table is
11162 a two-dimensional array of 32-bit words (using the byte order of the
11163 application binary), with L columns and N+1 rows, in row-major order.
11164 Each row in the array is indexed starting from 0. The first row provides
11165 a key to the remaining rows: each column in this row provides an identifier
11166 for a debug section, and the offsets in the same column of subsequent rows
11167 refer to that section. The section identifiers are:
11168
11169 DW_SECT_INFO 1 .debug_info.dwo
11170 DW_SECT_TYPES 2 .debug_types.dwo
11171 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11172 DW_SECT_LINE 4 .debug_line.dwo
11173 DW_SECT_LOC 5 .debug_loc.dwo
11174 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11175 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11176 DW_SECT_MACRO 8 .debug_macro.dwo
11177
11178 The offsets provided by the CU and TU index sections are the base offsets
11179 for the contributions made by each CU or TU to the corresponding section
11180 in the package file. Each CU and TU header contains an abbrev_offset
11181 field, used to find the abbreviations table for that CU or TU within the
11182 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11183 be interpreted as relative to the base offset given in the index section.
11184 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11185 should be interpreted as relative to the base offset for .debug_line.dwo,
11186 and offsets into other debug sections obtained from DWARF attributes should
11187 also be interpreted as relative to the corresponding base offset.
11188
11189 The table of sizes begins immediately following the table of offsets.
11190 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11191 with L columns and N rows, in row-major order. Each row in the array is
11192 indexed starting from 1 (row 0 is shared by the two tables).
11193
11194 ---
11195
11196 Hash table lookup is handled the same in version 1 and 2:
11197
11198 We assume that N and M will not exceed 2^32 - 1.
11199 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11200
11201 Given a 64-bit compilation unit signature or a type signature S, an entry
11202 in the hash table is located as follows:
11203
11204 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11205 the low-order k bits all set to 1.
11206
11207 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11208
11209 3) If the hash table entry at index H matches the signature, use that
11210 entry. If the hash table entry at index H is unused (all zeroes),
11211 terminate the search: the signature is not present in the table.
11212
11213 4) Let H = (H + H') modulo M. Repeat at Step 3.
11214
11215 Because M > N and H' and M are relatively prime, the search is guaranteed
11216 to stop at an unused slot or find the match. */
11217
11218 /* Create a hash table to map DWO IDs to their CU/TU entry in
11219 .debug_{info,types}.dwo in DWP_FILE.
11220 Returns NULL if there isn't one.
11221 Note: This function processes DWP files only, not DWO files. */
11222
11223 static struct dwp_hash_table *
11224 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11225 struct dwp_file *dwp_file, int is_debug_types)
11226 {
11227 struct objfile *objfile = dwarf2_per_objfile->objfile;
11228 bfd *dbfd = dwp_file->dbfd.get ();
11229 const gdb_byte *index_ptr, *index_end;
11230 struct dwarf2_section_info *index;
11231 uint32_t version, nr_columns, nr_units, nr_slots;
11232 struct dwp_hash_table *htab;
11233
11234 if (is_debug_types)
11235 index = &dwp_file->sections.tu_index;
11236 else
11237 index = &dwp_file->sections.cu_index;
11238
11239 if (index->empty ())
11240 return NULL;
11241 index->read (objfile);
11242
11243 index_ptr = index->buffer;
11244 index_end = index_ptr + index->size;
11245
11246 version = read_4_bytes (dbfd, index_ptr);
11247 index_ptr += 4;
11248 if (version == 2)
11249 nr_columns = read_4_bytes (dbfd, index_ptr);
11250 else
11251 nr_columns = 0;
11252 index_ptr += 4;
11253 nr_units = read_4_bytes (dbfd, index_ptr);
11254 index_ptr += 4;
11255 nr_slots = read_4_bytes (dbfd, index_ptr);
11256 index_ptr += 4;
11257
11258 if (version != 1 && version != 2)
11259 {
11260 error (_("Dwarf Error: unsupported DWP file version (%s)"
11261 " [in module %s]"),
11262 pulongest (version), dwp_file->name);
11263 }
11264 if (nr_slots != (nr_slots & -nr_slots))
11265 {
11266 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11267 " is not power of 2 [in module %s]"),
11268 pulongest (nr_slots), dwp_file->name);
11269 }
11270
11271 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11272 htab->version = version;
11273 htab->nr_columns = nr_columns;
11274 htab->nr_units = nr_units;
11275 htab->nr_slots = nr_slots;
11276 htab->hash_table = index_ptr;
11277 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11278
11279 /* Exit early if the table is empty. */
11280 if (nr_slots == 0 || nr_units == 0
11281 || (version == 2 && nr_columns == 0))
11282 {
11283 /* All must be zero. */
11284 if (nr_slots != 0 || nr_units != 0
11285 || (version == 2 && nr_columns != 0))
11286 {
11287 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11288 " all zero [in modules %s]"),
11289 dwp_file->name);
11290 }
11291 return htab;
11292 }
11293
11294 if (version == 1)
11295 {
11296 htab->section_pool.v1.indices =
11297 htab->unit_table + sizeof (uint32_t) * nr_slots;
11298 /* It's harder to decide whether the section is too small in v1.
11299 V1 is deprecated anyway so we punt. */
11300 }
11301 else
11302 {
11303 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11304 int *ids = htab->section_pool.v2.section_ids;
11305 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11306 /* Reverse map for error checking. */
11307 int ids_seen[DW_SECT_MAX + 1];
11308 int i;
11309
11310 if (nr_columns < 2)
11311 {
11312 error (_("Dwarf Error: bad DWP hash table, too few columns"
11313 " in section table [in module %s]"),
11314 dwp_file->name);
11315 }
11316 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11317 {
11318 error (_("Dwarf Error: bad DWP hash table, too many columns"
11319 " in section table [in module %s]"),
11320 dwp_file->name);
11321 }
11322 memset (ids, 255, sizeof_ids);
11323 memset (ids_seen, 255, sizeof (ids_seen));
11324 for (i = 0; i < nr_columns; ++i)
11325 {
11326 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11327
11328 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11329 {
11330 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11331 " in section table [in module %s]"),
11332 id, dwp_file->name);
11333 }
11334 if (ids_seen[id] != -1)
11335 {
11336 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11337 " id %d in section table [in module %s]"),
11338 id, dwp_file->name);
11339 }
11340 ids_seen[id] = i;
11341 ids[i] = id;
11342 }
11343 /* Must have exactly one info or types section. */
11344 if (((ids_seen[DW_SECT_INFO] != -1)
11345 + (ids_seen[DW_SECT_TYPES] != -1))
11346 != 1)
11347 {
11348 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11349 " DWO info/types section [in module %s]"),
11350 dwp_file->name);
11351 }
11352 /* Must have an abbrev section. */
11353 if (ids_seen[DW_SECT_ABBREV] == -1)
11354 {
11355 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11356 " section [in module %s]"),
11357 dwp_file->name);
11358 }
11359 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11360 htab->section_pool.v2.sizes =
11361 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11362 * nr_units * nr_columns);
11363 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11364 * nr_units * nr_columns))
11365 > index_end)
11366 {
11367 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11368 " [in module %s]"),
11369 dwp_file->name);
11370 }
11371 }
11372
11373 return htab;
11374 }
11375
11376 /* Update SECTIONS with the data from SECTP.
11377
11378 This function is like the other "locate" section routines that are
11379 passed to bfd_map_over_sections, but in this context the sections to
11380 read comes from the DWP V1 hash table, not the full ELF section table.
11381
11382 The result is non-zero for success, or zero if an error was found. */
11383
11384 static int
11385 locate_v1_virtual_dwo_sections (asection *sectp,
11386 struct virtual_v1_dwo_sections *sections)
11387 {
11388 const struct dwop_section_names *names = &dwop_section_names;
11389
11390 if (section_is_p (sectp->name, &names->abbrev_dwo))
11391 {
11392 /* There can be only one. */
11393 if (sections->abbrev.s.section != NULL)
11394 return 0;
11395 sections->abbrev.s.section = sectp;
11396 sections->abbrev.size = bfd_section_size (sectp);
11397 }
11398 else if (section_is_p (sectp->name, &names->info_dwo)
11399 || section_is_p (sectp->name, &names->types_dwo))
11400 {
11401 /* There can be only one. */
11402 if (sections->info_or_types.s.section != NULL)
11403 return 0;
11404 sections->info_or_types.s.section = sectp;
11405 sections->info_or_types.size = bfd_section_size (sectp);
11406 }
11407 else if (section_is_p (sectp->name, &names->line_dwo))
11408 {
11409 /* There can be only one. */
11410 if (sections->line.s.section != NULL)
11411 return 0;
11412 sections->line.s.section = sectp;
11413 sections->line.size = bfd_section_size (sectp);
11414 }
11415 else if (section_is_p (sectp->name, &names->loc_dwo))
11416 {
11417 /* There can be only one. */
11418 if (sections->loc.s.section != NULL)
11419 return 0;
11420 sections->loc.s.section = sectp;
11421 sections->loc.size = bfd_section_size (sectp);
11422 }
11423 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11424 {
11425 /* There can be only one. */
11426 if (sections->macinfo.s.section != NULL)
11427 return 0;
11428 sections->macinfo.s.section = sectp;
11429 sections->macinfo.size = bfd_section_size (sectp);
11430 }
11431 else if (section_is_p (sectp->name, &names->macro_dwo))
11432 {
11433 /* There can be only one. */
11434 if (sections->macro.s.section != NULL)
11435 return 0;
11436 sections->macro.s.section = sectp;
11437 sections->macro.size = bfd_section_size (sectp);
11438 }
11439 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11440 {
11441 /* There can be only one. */
11442 if (sections->str_offsets.s.section != NULL)
11443 return 0;
11444 sections->str_offsets.s.section = sectp;
11445 sections->str_offsets.size = bfd_section_size (sectp);
11446 }
11447 else
11448 {
11449 /* No other kind of section is valid. */
11450 return 0;
11451 }
11452
11453 return 1;
11454 }
11455
11456 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11457 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11458 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11459 This is for DWP version 1 files. */
11460
11461 static struct dwo_unit *
11462 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11463 struct dwp_file *dwp_file,
11464 uint32_t unit_index,
11465 const char *comp_dir,
11466 ULONGEST signature, int is_debug_types)
11467 {
11468 struct objfile *objfile = dwarf2_per_objfile->objfile;
11469 const struct dwp_hash_table *dwp_htab =
11470 is_debug_types ? dwp_file->tus : dwp_file->cus;
11471 bfd *dbfd = dwp_file->dbfd.get ();
11472 const char *kind = is_debug_types ? "TU" : "CU";
11473 struct dwo_file *dwo_file;
11474 struct dwo_unit *dwo_unit;
11475 struct virtual_v1_dwo_sections sections;
11476 void **dwo_file_slot;
11477 int i;
11478
11479 gdb_assert (dwp_file->version == 1);
11480
11481 if (dwarf_read_debug)
11482 {
11483 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11484 kind,
11485 pulongest (unit_index), hex_string (signature),
11486 dwp_file->name);
11487 }
11488
11489 /* Fetch the sections of this DWO unit.
11490 Put a limit on the number of sections we look for so that bad data
11491 doesn't cause us to loop forever. */
11492
11493 #define MAX_NR_V1_DWO_SECTIONS \
11494 (1 /* .debug_info or .debug_types */ \
11495 + 1 /* .debug_abbrev */ \
11496 + 1 /* .debug_line */ \
11497 + 1 /* .debug_loc */ \
11498 + 1 /* .debug_str_offsets */ \
11499 + 1 /* .debug_macro or .debug_macinfo */ \
11500 + 1 /* trailing zero */)
11501
11502 memset (&sections, 0, sizeof (sections));
11503
11504 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11505 {
11506 asection *sectp;
11507 uint32_t section_nr =
11508 read_4_bytes (dbfd,
11509 dwp_htab->section_pool.v1.indices
11510 + (unit_index + i) * sizeof (uint32_t));
11511
11512 if (section_nr == 0)
11513 break;
11514 if (section_nr >= dwp_file->num_sections)
11515 {
11516 error (_("Dwarf Error: bad DWP hash table, section number too large"
11517 " [in module %s]"),
11518 dwp_file->name);
11519 }
11520
11521 sectp = dwp_file->elf_sections[section_nr];
11522 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11523 {
11524 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11525 " [in module %s]"),
11526 dwp_file->name);
11527 }
11528 }
11529
11530 if (i < 2
11531 || sections.info_or_types.empty ()
11532 || sections.abbrev.empty ())
11533 {
11534 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11535 " [in module %s]"),
11536 dwp_file->name);
11537 }
11538 if (i == MAX_NR_V1_DWO_SECTIONS)
11539 {
11540 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11541 " [in module %s]"),
11542 dwp_file->name);
11543 }
11544
11545 /* It's easier for the rest of the code if we fake a struct dwo_file and
11546 have dwo_unit "live" in that. At least for now.
11547
11548 The DWP file can be made up of a random collection of CUs and TUs.
11549 However, for each CU + set of TUs that came from the same original DWO
11550 file, we can combine them back into a virtual DWO file to save space
11551 (fewer struct dwo_file objects to allocate). Remember that for really
11552 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11553
11554 std::string virtual_dwo_name =
11555 string_printf ("virtual-dwo/%d-%d-%d-%d",
11556 sections.abbrev.get_id (),
11557 sections.line.get_id (),
11558 sections.loc.get_id (),
11559 sections.str_offsets.get_id ());
11560 /* Can we use an existing virtual DWO file? */
11561 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11562 virtual_dwo_name.c_str (),
11563 comp_dir);
11564 /* Create one if necessary. */
11565 if (*dwo_file_slot == NULL)
11566 {
11567 if (dwarf_read_debug)
11568 {
11569 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11570 virtual_dwo_name.c_str ());
11571 }
11572 dwo_file = new struct dwo_file;
11573 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11574 dwo_file->comp_dir = comp_dir;
11575 dwo_file->sections.abbrev = sections.abbrev;
11576 dwo_file->sections.line = sections.line;
11577 dwo_file->sections.loc = sections.loc;
11578 dwo_file->sections.macinfo = sections.macinfo;
11579 dwo_file->sections.macro = sections.macro;
11580 dwo_file->sections.str_offsets = sections.str_offsets;
11581 /* The "str" section is global to the entire DWP file. */
11582 dwo_file->sections.str = dwp_file->sections.str;
11583 /* The info or types section is assigned below to dwo_unit,
11584 there's no need to record it in dwo_file.
11585 Also, we can't simply record type sections in dwo_file because
11586 we record a pointer into the vector in dwo_unit. As we collect more
11587 types we'll grow the vector and eventually have to reallocate space
11588 for it, invalidating all copies of pointers into the previous
11589 contents. */
11590 *dwo_file_slot = dwo_file;
11591 }
11592 else
11593 {
11594 if (dwarf_read_debug)
11595 {
11596 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11597 virtual_dwo_name.c_str ());
11598 }
11599 dwo_file = (struct dwo_file *) *dwo_file_slot;
11600 }
11601
11602 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11603 dwo_unit->dwo_file = dwo_file;
11604 dwo_unit->signature = signature;
11605 dwo_unit->section =
11606 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11607 *dwo_unit->section = sections.info_or_types;
11608 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11609
11610 return dwo_unit;
11611 }
11612
11613 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11614 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11615 piece within that section used by a TU/CU, return a virtual section
11616 of just that piece. */
11617
11618 static struct dwarf2_section_info
11619 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11620 struct dwarf2_section_info *section,
11621 bfd_size_type offset, bfd_size_type size)
11622 {
11623 struct dwarf2_section_info result;
11624 asection *sectp;
11625
11626 gdb_assert (section != NULL);
11627 gdb_assert (!section->is_virtual);
11628
11629 memset (&result, 0, sizeof (result));
11630 result.s.containing_section = section;
11631 result.is_virtual = true;
11632
11633 if (size == 0)
11634 return result;
11635
11636 sectp = section->get_bfd_section ();
11637
11638 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11639 bounds of the real section. This is a pretty-rare event, so just
11640 flag an error (easier) instead of a warning and trying to cope. */
11641 if (sectp == NULL
11642 || offset + size > bfd_section_size (sectp))
11643 {
11644 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11645 " in section %s [in module %s]"),
11646 sectp ? bfd_section_name (sectp) : "<unknown>",
11647 objfile_name (dwarf2_per_objfile->objfile));
11648 }
11649
11650 result.virtual_offset = offset;
11651 result.size = size;
11652 return result;
11653 }
11654
11655 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11656 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11657 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11658 This is for DWP version 2 files. */
11659
11660 static struct dwo_unit *
11661 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11662 struct dwp_file *dwp_file,
11663 uint32_t unit_index,
11664 const char *comp_dir,
11665 ULONGEST signature, int is_debug_types)
11666 {
11667 struct objfile *objfile = dwarf2_per_objfile->objfile;
11668 const struct dwp_hash_table *dwp_htab =
11669 is_debug_types ? dwp_file->tus : dwp_file->cus;
11670 bfd *dbfd = dwp_file->dbfd.get ();
11671 const char *kind = is_debug_types ? "TU" : "CU";
11672 struct dwo_file *dwo_file;
11673 struct dwo_unit *dwo_unit;
11674 struct virtual_v2_dwo_sections sections;
11675 void **dwo_file_slot;
11676 int i;
11677
11678 gdb_assert (dwp_file->version == 2);
11679
11680 if (dwarf_read_debug)
11681 {
11682 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11683 kind,
11684 pulongest (unit_index), hex_string (signature),
11685 dwp_file->name);
11686 }
11687
11688 /* Fetch the section offsets of this DWO unit. */
11689
11690 memset (&sections, 0, sizeof (sections));
11691
11692 for (i = 0; i < dwp_htab->nr_columns; ++i)
11693 {
11694 uint32_t offset = read_4_bytes (dbfd,
11695 dwp_htab->section_pool.v2.offsets
11696 + (((unit_index - 1) * dwp_htab->nr_columns
11697 + i)
11698 * sizeof (uint32_t)));
11699 uint32_t size = read_4_bytes (dbfd,
11700 dwp_htab->section_pool.v2.sizes
11701 + (((unit_index - 1) * dwp_htab->nr_columns
11702 + i)
11703 * sizeof (uint32_t)));
11704
11705 switch (dwp_htab->section_pool.v2.section_ids[i])
11706 {
11707 case DW_SECT_INFO:
11708 case DW_SECT_TYPES:
11709 sections.info_or_types_offset = offset;
11710 sections.info_or_types_size = size;
11711 break;
11712 case DW_SECT_ABBREV:
11713 sections.abbrev_offset = offset;
11714 sections.abbrev_size = size;
11715 break;
11716 case DW_SECT_LINE:
11717 sections.line_offset = offset;
11718 sections.line_size = size;
11719 break;
11720 case DW_SECT_LOC:
11721 sections.loc_offset = offset;
11722 sections.loc_size = size;
11723 break;
11724 case DW_SECT_STR_OFFSETS:
11725 sections.str_offsets_offset = offset;
11726 sections.str_offsets_size = size;
11727 break;
11728 case DW_SECT_MACINFO:
11729 sections.macinfo_offset = offset;
11730 sections.macinfo_size = size;
11731 break;
11732 case DW_SECT_MACRO:
11733 sections.macro_offset = offset;
11734 sections.macro_size = size;
11735 break;
11736 }
11737 }
11738
11739 /* It's easier for the rest of the code if we fake a struct dwo_file and
11740 have dwo_unit "live" in that. At least for now.
11741
11742 The DWP file can be made up of a random collection of CUs and TUs.
11743 However, for each CU + set of TUs that came from the same original DWO
11744 file, we can combine them back into a virtual DWO file to save space
11745 (fewer struct dwo_file objects to allocate). Remember that for really
11746 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11747
11748 std::string virtual_dwo_name =
11749 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11750 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11751 (long) (sections.line_size ? sections.line_offset : 0),
11752 (long) (sections.loc_size ? sections.loc_offset : 0),
11753 (long) (sections.str_offsets_size
11754 ? sections.str_offsets_offset : 0));
11755 /* Can we use an existing virtual DWO file? */
11756 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11757 virtual_dwo_name.c_str (),
11758 comp_dir);
11759 /* Create one if necessary. */
11760 if (*dwo_file_slot == NULL)
11761 {
11762 if (dwarf_read_debug)
11763 {
11764 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11765 virtual_dwo_name.c_str ());
11766 }
11767 dwo_file = new struct dwo_file;
11768 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11769 dwo_file->comp_dir = comp_dir;
11770 dwo_file->sections.abbrev =
11771 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11772 sections.abbrev_offset, sections.abbrev_size);
11773 dwo_file->sections.line =
11774 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11775 sections.line_offset, sections.line_size);
11776 dwo_file->sections.loc =
11777 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11778 sections.loc_offset, sections.loc_size);
11779 dwo_file->sections.macinfo =
11780 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11781 sections.macinfo_offset, sections.macinfo_size);
11782 dwo_file->sections.macro =
11783 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11784 sections.macro_offset, sections.macro_size);
11785 dwo_file->sections.str_offsets =
11786 create_dwp_v2_section (dwarf2_per_objfile,
11787 &dwp_file->sections.str_offsets,
11788 sections.str_offsets_offset,
11789 sections.str_offsets_size);
11790 /* The "str" section is global to the entire DWP file. */
11791 dwo_file->sections.str = dwp_file->sections.str;
11792 /* The info or types section is assigned below to dwo_unit,
11793 there's no need to record it in dwo_file.
11794 Also, we can't simply record type sections in dwo_file because
11795 we record a pointer into the vector in dwo_unit. As we collect more
11796 types we'll grow the vector and eventually have to reallocate space
11797 for it, invalidating all copies of pointers into the previous
11798 contents. */
11799 *dwo_file_slot = dwo_file;
11800 }
11801 else
11802 {
11803 if (dwarf_read_debug)
11804 {
11805 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11806 virtual_dwo_name.c_str ());
11807 }
11808 dwo_file = (struct dwo_file *) *dwo_file_slot;
11809 }
11810
11811 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11812 dwo_unit->dwo_file = dwo_file;
11813 dwo_unit->signature = signature;
11814 dwo_unit->section =
11815 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11816 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11817 is_debug_types
11818 ? &dwp_file->sections.types
11819 : &dwp_file->sections.info,
11820 sections.info_or_types_offset,
11821 sections.info_or_types_size);
11822 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11823
11824 return dwo_unit;
11825 }
11826
11827 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11828 Returns NULL if the signature isn't found. */
11829
11830 static struct dwo_unit *
11831 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11832 struct dwp_file *dwp_file, const char *comp_dir,
11833 ULONGEST signature, int is_debug_types)
11834 {
11835 const struct dwp_hash_table *dwp_htab =
11836 is_debug_types ? dwp_file->tus : dwp_file->cus;
11837 bfd *dbfd = dwp_file->dbfd.get ();
11838 uint32_t mask = dwp_htab->nr_slots - 1;
11839 uint32_t hash = signature & mask;
11840 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11841 unsigned int i;
11842 void **slot;
11843 struct dwo_unit find_dwo_cu;
11844
11845 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11846 find_dwo_cu.signature = signature;
11847 slot = htab_find_slot (is_debug_types
11848 ? dwp_file->loaded_tus.get ()
11849 : dwp_file->loaded_cus.get (),
11850 &find_dwo_cu, INSERT);
11851
11852 if (*slot != NULL)
11853 return (struct dwo_unit *) *slot;
11854
11855 /* Use a for loop so that we don't loop forever on bad debug info. */
11856 for (i = 0; i < dwp_htab->nr_slots; ++i)
11857 {
11858 ULONGEST signature_in_table;
11859
11860 signature_in_table =
11861 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11862 if (signature_in_table == signature)
11863 {
11864 uint32_t unit_index =
11865 read_4_bytes (dbfd,
11866 dwp_htab->unit_table + hash * sizeof (uint32_t));
11867
11868 if (dwp_file->version == 1)
11869 {
11870 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11871 dwp_file, unit_index,
11872 comp_dir, signature,
11873 is_debug_types);
11874 }
11875 else
11876 {
11877 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11878 dwp_file, unit_index,
11879 comp_dir, signature,
11880 is_debug_types);
11881 }
11882 return (struct dwo_unit *) *slot;
11883 }
11884 if (signature_in_table == 0)
11885 return NULL;
11886 hash = (hash + hash2) & mask;
11887 }
11888
11889 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11890 " [in module %s]"),
11891 dwp_file->name);
11892 }
11893
11894 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11895 Open the file specified by FILE_NAME and hand it off to BFD for
11896 preliminary analysis. Return a newly initialized bfd *, which
11897 includes a canonicalized copy of FILE_NAME.
11898 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11899 SEARCH_CWD is true if the current directory is to be searched.
11900 It will be searched before debug-file-directory.
11901 If successful, the file is added to the bfd include table of the
11902 objfile's bfd (see gdb_bfd_record_inclusion).
11903 If unable to find/open the file, return NULL.
11904 NOTE: This function is derived from symfile_bfd_open. */
11905
11906 static gdb_bfd_ref_ptr
11907 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11908 const char *file_name, int is_dwp, int search_cwd)
11909 {
11910 int desc;
11911 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11912 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11913 to debug_file_directory. */
11914 const char *search_path;
11915 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11916
11917 gdb::unique_xmalloc_ptr<char> search_path_holder;
11918 if (search_cwd)
11919 {
11920 if (*debug_file_directory != '\0')
11921 {
11922 search_path_holder.reset (concat (".", dirname_separator_string,
11923 debug_file_directory,
11924 (char *) NULL));
11925 search_path = search_path_holder.get ();
11926 }
11927 else
11928 search_path = ".";
11929 }
11930 else
11931 search_path = debug_file_directory;
11932
11933 openp_flags flags = OPF_RETURN_REALPATH;
11934 if (is_dwp)
11935 flags |= OPF_SEARCH_IN_PATH;
11936
11937 gdb::unique_xmalloc_ptr<char> absolute_name;
11938 desc = openp (search_path, flags, file_name,
11939 O_RDONLY | O_BINARY, &absolute_name);
11940 if (desc < 0)
11941 return NULL;
11942
11943 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11944 gnutarget, desc));
11945 if (sym_bfd == NULL)
11946 return NULL;
11947 bfd_set_cacheable (sym_bfd.get (), 1);
11948
11949 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11950 return NULL;
11951
11952 /* Success. Record the bfd as having been included by the objfile's bfd.
11953 This is important because things like demangled_names_hash lives in the
11954 objfile's per_bfd space and may have references to things like symbol
11955 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11956 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11957
11958 return sym_bfd;
11959 }
11960
11961 /* Try to open DWO file FILE_NAME.
11962 COMP_DIR is the DW_AT_comp_dir attribute.
11963 The result is the bfd handle of the file.
11964 If there is a problem finding or opening the file, return NULL.
11965 Upon success, the canonicalized path of the file is stored in the bfd,
11966 same as symfile_bfd_open. */
11967
11968 static gdb_bfd_ref_ptr
11969 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11970 const char *file_name, const char *comp_dir)
11971 {
11972 if (IS_ABSOLUTE_PATH (file_name))
11973 return try_open_dwop_file (dwarf2_per_objfile, file_name,
11974 0 /*is_dwp*/, 0 /*search_cwd*/);
11975
11976 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11977
11978 if (comp_dir != NULL)
11979 {
11980 gdb::unique_xmalloc_ptr<char> path_to_try
11981 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
11982
11983 /* NOTE: If comp_dir is a relative path, this will also try the
11984 search path, which seems useful. */
11985 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
11986 path_to_try.get (),
11987 0 /*is_dwp*/,
11988 1 /*search_cwd*/));
11989 if (abfd != NULL)
11990 return abfd;
11991 }
11992
11993 /* That didn't work, try debug-file-directory, which, despite its name,
11994 is a list of paths. */
11995
11996 if (*debug_file_directory == '\0')
11997 return NULL;
11998
11999 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12000 0 /*is_dwp*/, 1 /*search_cwd*/);
12001 }
12002
12003 /* This function is mapped across the sections and remembers the offset and
12004 size of each of the DWO debugging sections we are interested in. */
12005
12006 static void
12007 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12008 {
12009 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12010 const struct dwop_section_names *names = &dwop_section_names;
12011
12012 if (section_is_p (sectp->name, &names->abbrev_dwo))
12013 {
12014 dwo_sections->abbrev.s.section = sectp;
12015 dwo_sections->abbrev.size = bfd_section_size (sectp);
12016 }
12017 else if (section_is_p (sectp->name, &names->info_dwo))
12018 {
12019 dwo_sections->info.s.section = sectp;
12020 dwo_sections->info.size = bfd_section_size (sectp);
12021 }
12022 else if (section_is_p (sectp->name, &names->line_dwo))
12023 {
12024 dwo_sections->line.s.section = sectp;
12025 dwo_sections->line.size = bfd_section_size (sectp);
12026 }
12027 else if (section_is_p (sectp->name, &names->loc_dwo))
12028 {
12029 dwo_sections->loc.s.section = sectp;
12030 dwo_sections->loc.size = bfd_section_size (sectp);
12031 }
12032 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12033 {
12034 dwo_sections->macinfo.s.section = sectp;
12035 dwo_sections->macinfo.size = bfd_section_size (sectp);
12036 }
12037 else if (section_is_p (sectp->name, &names->macro_dwo))
12038 {
12039 dwo_sections->macro.s.section = sectp;
12040 dwo_sections->macro.size = bfd_section_size (sectp);
12041 }
12042 else if (section_is_p (sectp->name, &names->str_dwo))
12043 {
12044 dwo_sections->str.s.section = sectp;
12045 dwo_sections->str.size = bfd_section_size (sectp);
12046 }
12047 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12048 {
12049 dwo_sections->str_offsets.s.section = sectp;
12050 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12051 }
12052 else if (section_is_p (sectp->name, &names->types_dwo))
12053 {
12054 struct dwarf2_section_info type_section;
12055
12056 memset (&type_section, 0, sizeof (type_section));
12057 type_section.s.section = sectp;
12058 type_section.size = bfd_section_size (sectp);
12059 dwo_sections->types.push_back (type_section);
12060 }
12061 }
12062
12063 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12064 by PER_CU. This is for the non-DWP case.
12065 The result is NULL if DWO_NAME can't be found. */
12066
12067 static struct dwo_file *
12068 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12069 const char *dwo_name, const char *comp_dir)
12070 {
12071 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12072
12073 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12074 if (dbfd == NULL)
12075 {
12076 if (dwarf_read_debug)
12077 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12078 return NULL;
12079 }
12080
12081 dwo_file_up dwo_file (new struct dwo_file);
12082 dwo_file->dwo_name = dwo_name;
12083 dwo_file->comp_dir = comp_dir;
12084 dwo_file->dbfd = std::move (dbfd);
12085
12086 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12087 &dwo_file->sections);
12088
12089 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12090 dwo_file->sections.info, dwo_file->cus);
12091
12092 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12093 dwo_file->sections.types, dwo_file->tus);
12094
12095 if (dwarf_read_debug)
12096 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12097
12098 return dwo_file.release ();
12099 }
12100
12101 /* This function is mapped across the sections and remembers the offset and
12102 size of each of the DWP debugging sections common to version 1 and 2 that
12103 we are interested in. */
12104
12105 static void
12106 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12107 void *dwp_file_ptr)
12108 {
12109 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12110 const struct dwop_section_names *names = &dwop_section_names;
12111 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12112
12113 /* Record the ELF section number for later lookup: this is what the
12114 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12115 gdb_assert (elf_section_nr < dwp_file->num_sections);
12116 dwp_file->elf_sections[elf_section_nr] = sectp;
12117
12118 /* Look for specific sections that we need. */
12119 if (section_is_p (sectp->name, &names->str_dwo))
12120 {
12121 dwp_file->sections.str.s.section = sectp;
12122 dwp_file->sections.str.size = bfd_section_size (sectp);
12123 }
12124 else if (section_is_p (sectp->name, &names->cu_index))
12125 {
12126 dwp_file->sections.cu_index.s.section = sectp;
12127 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12128 }
12129 else if (section_is_p (sectp->name, &names->tu_index))
12130 {
12131 dwp_file->sections.tu_index.s.section = sectp;
12132 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12133 }
12134 }
12135
12136 /* This function is mapped across the sections and remembers the offset and
12137 size of each of the DWP version 2 debugging sections that we are interested
12138 in. This is split into a separate function because we don't know if we
12139 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12140
12141 static void
12142 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12143 {
12144 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12145 const struct dwop_section_names *names = &dwop_section_names;
12146 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12147
12148 /* Record the ELF section number for later lookup: this is what the
12149 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12150 gdb_assert (elf_section_nr < dwp_file->num_sections);
12151 dwp_file->elf_sections[elf_section_nr] = sectp;
12152
12153 /* Look for specific sections that we need. */
12154 if (section_is_p (sectp->name, &names->abbrev_dwo))
12155 {
12156 dwp_file->sections.abbrev.s.section = sectp;
12157 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12158 }
12159 else if (section_is_p (sectp->name, &names->info_dwo))
12160 {
12161 dwp_file->sections.info.s.section = sectp;
12162 dwp_file->sections.info.size = bfd_section_size (sectp);
12163 }
12164 else if (section_is_p (sectp->name, &names->line_dwo))
12165 {
12166 dwp_file->sections.line.s.section = sectp;
12167 dwp_file->sections.line.size = bfd_section_size (sectp);
12168 }
12169 else if (section_is_p (sectp->name, &names->loc_dwo))
12170 {
12171 dwp_file->sections.loc.s.section = sectp;
12172 dwp_file->sections.loc.size = bfd_section_size (sectp);
12173 }
12174 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12175 {
12176 dwp_file->sections.macinfo.s.section = sectp;
12177 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12178 }
12179 else if (section_is_p (sectp->name, &names->macro_dwo))
12180 {
12181 dwp_file->sections.macro.s.section = sectp;
12182 dwp_file->sections.macro.size = bfd_section_size (sectp);
12183 }
12184 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12185 {
12186 dwp_file->sections.str_offsets.s.section = sectp;
12187 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12188 }
12189 else if (section_is_p (sectp->name, &names->types_dwo))
12190 {
12191 dwp_file->sections.types.s.section = sectp;
12192 dwp_file->sections.types.size = bfd_section_size (sectp);
12193 }
12194 }
12195
12196 /* Hash function for dwp_file loaded CUs/TUs. */
12197
12198 static hashval_t
12199 hash_dwp_loaded_cutus (const void *item)
12200 {
12201 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12202
12203 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12204 return dwo_unit->signature;
12205 }
12206
12207 /* Equality function for dwp_file loaded CUs/TUs. */
12208
12209 static int
12210 eq_dwp_loaded_cutus (const void *a, const void *b)
12211 {
12212 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12213 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12214
12215 return dua->signature == dub->signature;
12216 }
12217
12218 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12219
12220 static htab_up
12221 allocate_dwp_loaded_cutus_table ()
12222 {
12223 return htab_up (htab_create_alloc (3,
12224 hash_dwp_loaded_cutus,
12225 eq_dwp_loaded_cutus,
12226 NULL, xcalloc, xfree));
12227 }
12228
12229 /* Try to open DWP file FILE_NAME.
12230 The result is the bfd handle of the file.
12231 If there is a problem finding or opening the file, return NULL.
12232 Upon success, the canonicalized path of the file is stored in the bfd,
12233 same as symfile_bfd_open. */
12234
12235 static gdb_bfd_ref_ptr
12236 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12237 const char *file_name)
12238 {
12239 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12240 1 /*is_dwp*/,
12241 1 /*search_cwd*/));
12242 if (abfd != NULL)
12243 return abfd;
12244
12245 /* Work around upstream bug 15652.
12246 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12247 [Whether that's a "bug" is debatable, but it is getting in our way.]
12248 We have no real idea where the dwp file is, because gdb's realpath-ing
12249 of the executable's path may have discarded the needed info.
12250 [IWBN if the dwp file name was recorded in the executable, akin to
12251 .gnu_debuglink, but that doesn't exist yet.]
12252 Strip the directory from FILE_NAME and search again. */
12253 if (*debug_file_directory != '\0')
12254 {
12255 /* Don't implicitly search the current directory here.
12256 If the user wants to search "." to handle this case,
12257 it must be added to debug-file-directory. */
12258 return try_open_dwop_file (dwarf2_per_objfile,
12259 lbasename (file_name), 1 /*is_dwp*/,
12260 0 /*search_cwd*/);
12261 }
12262
12263 return NULL;
12264 }
12265
12266 /* Initialize the use of the DWP file for the current objfile.
12267 By convention the name of the DWP file is ${objfile}.dwp.
12268 The result is NULL if it can't be found. */
12269
12270 static std::unique_ptr<struct dwp_file>
12271 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12272 {
12273 struct objfile *objfile = dwarf2_per_objfile->objfile;
12274
12275 /* Try to find first .dwp for the binary file before any symbolic links
12276 resolving. */
12277
12278 /* If the objfile is a debug file, find the name of the real binary
12279 file and get the name of dwp file from there. */
12280 std::string dwp_name;
12281 if (objfile->separate_debug_objfile_backlink != NULL)
12282 {
12283 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12284 const char *backlink_basename = lbasename (backlink->original_name);
12285
12286 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12287 }
12288 else
12289 dwp_name = objfile->original_name;
12290
12291 dwp_name += ".dwp";
12292
12293 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12294 if (dbfd == NULL
12295 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12296 {
12297 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12298 dwp_name = objfile_name (objfile);
12299 dwp_name += ".dwp";
12300 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12301 }
12302
12303 if (dbfd == NULL)
12304 {
12305 if (dwarf_read_debug)
12306 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12307 return std::unique_ptr<dwp_file> ();
12308 }
12309
12310 const char *name = bfd_get_filename (dbfd.get ());
12311 std::unique_ptr<struct dwp_file> dwp_file
12312 (new struct dwp_file (name, std::move (dbfd)));
12313
12314 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12315 dwp_file->elf_sections =
12316 OBSTACK_CALLOC (&objfile->objfile_obstack,
12317 dwp_file->num_sections, asection *);
12318
12319 bfd_map_over_sections (dwp_file->dbfd.get (),
12320 dwarf2_locate_common_dwp_sections,
12321 dwp_file.get ());
12322
12323 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12324 0);
12325
12326 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12327 1);
12328
12329 /* The DWP file version is stored in the hash table. Oh well. */
12330 if (dwp_file->cus && dwp_file->tus
12331 && dwp_file->cus->version != dwp_file->tus->version)
12332 {
12333 /* Technically speaking, we should try to limp along, but this is
12334 pretty bizarre. We use pulongest here because that's the established
12335 portability solution (e.g, we cannot use %u for uint32_t). */
12336 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12337 " TU version %s [in DWP file %s]"),
12338 pulongest (dwp_file->cus->version),
12339 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12340 }
12341
12342 if (dwp_file->cus)
12343 dwp_file->version = dwp_file->cus->version;
12344 else if (dwp_file->tus)
12345 dwp_file->version = dwp_file->tus->version;
12346 else
12347 dwp_file->version = 2;
12348
12349 if (dwp_file->version == 2)
12350 bfd_map_over_sections (dwp_file->dbfd.get (),
12351 dwarf2_locate_v2_dwp_sections,
12352 dwp_file.get ());
12353
12354 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12355 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12356
12357 if (dwarf_read_debug)
12358 {
12359 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12360 fprintf_unfiltered (gdb_stdlog,
12361 " %s CUs, %s TUs\n",
12362 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12363 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12364 }
12365
12366 return dwp_file;
12367 }
12368
12369 /* Wrapper around open_and_init_dwp_file, only open it once. */
12370
12371 static struct dwp_file *
12372 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12373 {
12374 if (! dwarf2_per_objfile->dwp_checked)
12375 {
12376 dwarf2_per_objfile->dwp_file
12377 = open_and_init_dwp_file (dwarf2_per_objfile);
12378 dwarf2_per_objfile->dwp_checked = 1;
12379 }
12380 return dwarf2_per_objfile->dwp_file.get ();
12381 }
12382
12383 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12384 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12385 or in the DWP file for the objfile, referenced by THIS_UNIT.
12386 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12387 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12388
12389 This is called, for example, when wanting to read a variable with a
12390 complex location. Therefore we don't want to do file i/o for every call.
12391 Therefore we don't want to look for a DWO file on every call.
12392 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12393 then we check if we've already seen DWO_NAME, and only THEN do we check
12394 for a DWO file.
12395
12396 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12397 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12398
12399 static struct dwo_unit *
12400 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12401 const char *dwo_name, const char *comp_dir,
12402 ULONGEST signature, int is_debug_types)
12403 {
12404 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12405 struct objfile *objfile = dwarf2_per_objfile->objfile;
12406 const char *kind = is_debug_types ? "TU" : "CU";
12407 void **dwo_file_slot;
12408 struct dwo_file *dwo_file;
12409 struct dwp_file *dwp_file;
12410
12411 /* First see if there's a DWP file.
12412 If we have a DWP file but didn't find the DWO inside it, don't
12413 look for the original DWO file. It makes gdb behave differently
12414 depending on whether one is debugging in the build tree. */
12415
12416 dwp_file = get_dwp_file (dwarf2_per_objfile);
12417 if (dwp_file != NULL)
12418 {
12419 const struct dwp_hash_table *dwp_htab =
12420 is_debug_types ? dwp_file->tus : dwp_file->cus;
12421
12422 if (dwp_htab != NULL)
12423 {
12424 struct dwo_unit *dwo_cutu =
12425 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12426 signature, is_debug_types);
12427
12428 if (dwo_cutu != NULL)
12429 {
12430 if (dwarf_read_debug)
12431 {
12432 fprintf_unfiltered (gdb_stdlog,
12433 "Virtual DWO %s %s found: @%s\n",
12434 kind, hex_string (signature),
12435 host_address_to_string (dwo_cutu));
12436 }
12437 return dwo_cutu;
12438 }
12439 }
12440 }
12441 else
12442 {
12443 /* No DWP file, look for the DWO file. */
12444
12445 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12446 dwo_name, comp_dir);
12447 if (*dwo_file_slot == NULL)
12448 {
12449 /* Read in the file and build a table of the CUs/TUs it contains. */
12450 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12451 }
12452 /* NOTE: This will be NULL if unable to open the file. */
12453 dwo_file = (struct dwo_file *) *dwo_file_slot;
12454
12455 if (dwo_file != NULL)
12456 {
12457 struct dwo_unit *dwo_cutu = NULL;
12458
12459 if (is_debug_types && dwo_file->tus)
12460 {
12461 struct dwo_unit find_dwo_cutu;
12462
12463 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12464 find_dwo_cutu.signature = signature;
12465 dwo_cutu
12466 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12467 &find_dwo_cutu);
12468 }
12469 else if (!is_debug_types && dwo_file->cus)
12470 {
12471 struct dwo_unit find_dwo_cutu;
12472
12473 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12474 find_dwo_cutu.signature = signature;
12475 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12476 &find_dwo_cutu);
12477 }
12478
12479 if (dwo_cutu != NULL)
12480 {
12481 if (dwarf_read_debug)
12482 {
12483 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12484 kind, dwo_name, hex_string (signature),
12485 host_address_to_string (dwo_cutu));
12486 }
12487 return dwo_cutu;
12488 }
12489 }
12490 }
12491
12492 /* We didn't find it. This could mean a dwo_id mismatch, or
12493 someone deleted the DWO/DWP file, or the search path isn't set up
12494 correctly to find the file. */
12495
12496 if (dwarf_read_debug)
12497 {
12498 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12499 kind, dwo_name, hex_string (signature));
12500 }
12501
12502 /* This is a warning and not a complaint because it can be caused by
12503 pilot error (e.g., user accidentally deleting the DWO). */
12504 {
12505 /* Print the name of the DWP file if we looked there, helps the user
12506 better diagnose the problem. */
12507 std::string dwp_text;
12508
12509 if (dwp_file != NULL)
12510 dwp_text = string_printf (" [in DWP file %s]",
12511 lbasename (dwp_file->name));
12512
12513 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12514 " [in module %s]"),
12515 kind, dwo_name, hex_string (signature),
12516 dwp_text.c_str (),
12517 this_unit->is_debug_types ? "TU" : "CU",
12518 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12519 }
12520 return NULL;
12521 }
12522
12523 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12524 See lookup_dwo_cutu_unit for details. */
12525
12526 static struct dwo_unit *
12527 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12528 const char *dwo_name, const char *comp_dir,
12529 ULONGEST signature)
12530 {
12531 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12532 }
12533
12534 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12535 See lookup_dwo_cutu_unit for details. */
12536
12537 static struct dwo_unit *
12538 lookup_dwo_type_unit (struct signatured_type *this_tu,
12539 const char *dwo_name, const char *comp_dir)
12540 {
12541 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12542 }
12543
12544 /* Traversal function for queue_and_load_all_dwo_tus. */
12545
12546 static int
12547 queue_and_load_dwo_tu (void **slot, void *info)
12548 {
12549 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12550 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12551 ULONGEST signature = dwo_unit->signature;
12552 struct signatured_type *sig_type =
12553 lookup_dwo_signatured_type (per_cu->cu, signature);
12554
12555 if (sig_type != NULL)
12556 {
12557 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12558
12559 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12560 a real dependency of PER_CU on SIG_TYPE. That is detected later
12561 while processing PER_CU. */
12562 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12563 load_full_type_unit (sig_cu);
12564 per_cu->imported_symtabs_push (sig_cu);
12565 }
12566
12567 return 1;
12568 }
12569
12570 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12571 The DWO may have the only definition of the type, though it may not be
12572 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12573 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12574
12575 static void
12576 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12577 {
12578 struct dwo_unit *dwo_unit;
12579 struct dwo_file *dwo_file;
12580
12581 gdb_assert (!per_cu->is_debug_types);
12582 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12583 gdb_assert (per_cu->cu != NULL);
12584
12585 dwo_unit = per_cu->cu->dwo_unit;
12586 gdb_assert (dwo_unit != NULL);
12587
12588 dwo_file = dwo_unit->dwo_file;
12589 if (dwo_file->tus != NULL)
12590 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12591 per_cu);
12592 }
12593
12594 /* Read in various DIEs. */
12595
12596 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12597 Inherit only the children of the DW_AT_abstract_origin DIE not being
12598 already referenced by DW_AT_abstract_origin from the children of the
12599 current DIE. */
12600
12601 static void
12602 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12603 {
12604 struct die_info *child_die;
12605 sect_offset *offsetp;
12606 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12607 struct die_info *origin_die;
12608 /* Iterator of the ORIGIN_DIE children. */
12609 struct die_info *origin_child_die;
12610 struct attribute *attr;
12611 struct dwarf2_cu *origin_cu;
12612 struct pending **origin_previous_list_in_scope;
12613
12614 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12615 if (!attr)
12616 return;
12617
12618 /* Note that following die references may follow to a die in a
12619 different cu. */
12620
12621 origin_cu = cu;
12622 origin_die = follow_die_ref (die, attr, &origin_cu);
12623
12624 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12625 symbols in. */
12626 origin_previous_list_in_scope = origin_cu->list_in_scope;
12627 origin_cu->list_in_scope = cu->list_in_scope;
12628
12629 if (die->tag != origin_die->tag
12630 && !(die->tag == DW_TAG_inlined_subroutine
12631 && origin_die->tag == DW_TAG_subprogram))
12632 complaint (_("DIE %s and its abstract origin %s have different tags"),
12633 sect_offset_str (die->sect_off),
12634 sect_offset_str (origin_die->sect_off));
12635
12636 std::vector<sect_offset> offsets;
12637
12638 for (child_die = die->child;
12639 child_die && child_die->tag;
12640 child_die = child_die->sibling)
12641 {
12642 struct die_info *child_origin_die;
12643 struct dwarf2_cu *child_origin_cu;
12644
12645 /* We are trying to process concrete instance entries:
12646 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12647 it's not relevant to our analysis here. i.e. detecting DIEs that are
12648 present in the abstract instance but not referenced in the concrete
12649 one. */
12650 if (child_die->tag == DW_TAG_call_site
12651 || child_die->tag == DW_TAG_GNU_call_site)
12652 continue;
12653
12654 /* For each CHILD_DIE, find the corresponding child of
12655 ORIGIN_DIE. If there is more than one layer of
12656 DW_AT_abstract_origin, follow them all; there shouldn't be,
12657 but GCC versions at least through 4.4 generate this (GCC PR
12658 40573). */
12659 child_origin_die = child_die;
12660 child_origin_cu = cu;
12661 while (1)
12662 {
12663 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12664 child_origin_cu);
12665 if (attr == NULL)
12666 break;
12667 child_origin_die = follow_die_ref (child_origin_die, attr,
12668 &child_origin_cu);
12669 }
12670
12671 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12672 counterpart may exist. */
12673 if (child_origin_die != child_die)
12674 {
12675 if (child_die->tag != child_origin_die->tag
12676 && !(child_die->tag == DW_TAG_inlined_subroutine
12677 && child_origin_die->tag == DW_TAG_subprogram))
12678 complaint (_("Child DIE %s and its abstract origin %s have "
12679 "different tags"),
12680 sect_offset_str (child_die->sect_off),
12681 sect_offset_str (child_origin_die->sect_off));
12682 if (child_origin_die->parent != origin_die)
12683 complaint (_("Child DIE %s and its abstract origin %s have "
12684 "different parents"),
12685 sect_offset_str (child_die->sect_off),
12686 sect_offset_str (child_origin_die->sect_off));
12687 else
12688 offsets.push_back (child_origin_die->sect_off);
12689 }
12690 }
12691 std::sort (offsets.begin (), offsets.end ());
12692 sect_offset *offsets_end = offsets.data () + offsets.size ();
12693 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12694 if (offsetp[-1] == *offsetp)
12695 complaint (_("Multiple children of DIE %s refer "
12696 "to DIE %s as their abstract origin"),
12697 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12698
12699 offsetp = offsets.data ();
12700 origin_child_die = origin_die->child;
12701 while (origin_child_die && origin_child_die->tag)
12702 {
12703 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12704 while (offsetp < offsets_end
12705 && *offsetp < origin_child_die->sect_off)
12706 offsetp++;
12707 if (offsetp >= offsets_end
12708 || *offsetp > origin_child_die->sect_off)
12709 {
12710 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12711 Check whether we're already processing ORIGIN_CHILD_DIE.
12712 This can happen with mutually referenced abstract_origins.
12713 PR 16581. */
12714 if (!origin_child_die->in_process)
12715 process_die (origin_child_die, origin_cu);
12716 }
12717 origin_child_die = origin_child_die->sibling;
12718 }
12719 origin_cu->list_in_scope = origin_previous_list_in_scope;
12720
12721 if (cu != origin_cu)
12722 compute_delayed_physnames (origin_cu);
12723 }
12724
12725 static void
12726 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12727 {
12728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12729 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12730 struct context_stack *newobj;
12731 CORE_ADDR lowpc;
12732 CORE_ADDR highpc;
12733 struct die_info *child_die;
12734 struct attribute *attr, *call_line, *call_file;
12735 const char *name;
12736 CORE_ADDR baseaddr;
12737 struct block *block;
12738 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12739 std::vector<struct symbol *> template_args;
12740 struct template_symbol *templ_func = NULL;
12741
12742 if (inlined_func)
12743 {
12744 /* If we do not have call site information, we can't show the
12745 caller of this inlined function. That's too confusing, so
12746 only use the scope for local variables. */
12747 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12748 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12749 if (call_line == NULL || call_file == NULL)
12750 {
12751 read_lexical_block_scope (die, cu);
12752 return;
12753 }
12754 }
12755
12756 baseaddr = objfile->text_section_offset ();
12757
12758 name = dwarf2_name (die, cu);
12759
12760 /* Ignore functions with missing or empty names. These are actually
12761 illegal according to the DWARF standard. */
12762 if (name == NULL)
12763 {
12764 complaint (_("missing name for subprogram DIE at %s"),
12765 sect_offset_str (die->sect_off));
12766 return;
12767 }
12768
12769 /* Ignore functions with missing or invalid low and high pc attributes. */
12770 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12771 <= PC_BOUNDS_INVALID)
12772 {
12773 attr = dwarf2_attr (die, DW_AT_external, cu);
12774 if (!attr || !DW_UNSND (attr))
12775 complaint (_("cannot get low and high bounds "
12776 "for subprogram DIE at %s"),
12777 sect_offset_str (die->sect_off));
12778 return;
12779 }
12780
12781 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12782 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12783
12784 /* If we have any template arguments, then we must allocate a
12785 different sort of symbol. */
12786 for (child_die = die->child; child_die; child_die = child_die->sibling)
12787 {
12788 if (child_die->tag == DW_TAG_template_type_param
12789 || child_die->tag == DW_TAG_template_value_param)
12790 {
12791 templ_func = allocate_template_symbol (objfile);
12792 templ_func->subclass = SYMBOL_TEMPLATE;
12793 break;
12794 }
12795 }
12796
12797 newobj = cu->get_builder ()->push_context (0, lowpc);
12798 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12799 (struct symbol *) templ_func);
12800
12801 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12802 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12803 cu->language);
12804
12805 /* If there is a location expression for DW_AT_frame_base, record
12806 it. */
12807 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12808 if (attr != nullptr)
12809 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12810
12811 /* If there is a location for the static link, record it. */
12812 newobj->static_link = NULL;
12813 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12814 if (attr != nullptr)
12815 {
12816 newobj->static_link
12817 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12818 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12819 cu->per_cu->addr_type ());
12820 }
12821
12822 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12823
12824 if (die->child != NULL)
12825 {
12826 child_die = die->child;
12827 while (child_die && child_die->tag)
12828 {
12829 if (child_die->tag == DW_TAG_template_type_param
12830 || child_die->tag == DW_TAG_template_value_param)
12831 {
12832 struct symbol *arg = new_symbol (child_die, NULL, cu);
12833
12834 if (arg != NULL)
12835 template_args.push_back (arg);
12836 }
12837 else
12838 process_die (child_die, cu);
12839 child_die = child_die->sibling;
12840 }
12841 }
12842
12843 inherit_abstract_dies (die, cu);
12844
12845 /* If we have a DW_AT_specification, we might need to import using
12846 directives from the context of the specification DIE. See the
12847 comment in determine_prefix. */
12848 if (cu->language == language_cplus
12849 && dwarf2_attr (die, DW_AT_specification, cu))
12850 {
12851 struct dwarf2_cu *spec_cu = cu;
12852 struct die_info *spec_die = die_specification (die, &spec_cu);
12853
12854 while (spec_die)
12855 {
12856 child_die = spec_die->child;
12857 while (child_die && child_die->tag)
12858 {
12859 if (child_die->tag == DW_TAG_imported_module)
12860 process_die (child_die, spec_cu);
12861 child_die = child_die->sibling;
12862 }
12863
12864 /* In some cases, GCC generates specification DIEs that
12865 themselves contain DW_AT_specification attributes. */
12866 spec_die = die_specification (spec_die, &spec_cu);
12867 }
12868 }
12869
12870 struct context_stack cstk = cu->get_builder ()->pop_context ();
12871 /* Make a block for the local symbols within. */
12872 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12873 cstk.static_link, lowpc, highpc);
12874
12875 /* For C++, set the block's scope. */
12876 if ((cu->language == language_cplus
12877 || cu->language == language_fortran
12878 || cu->language == language_d
12879 || cu->language == language_rust)
12880 && cu->processing_has_namespace_info)
12881 block_set_scope (block, determine_prefix (die, cu),
12882 &objfile->objfile_obstack);
12883
12884 /* If we have address ranges, record them. */
12885 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12886
12887 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12888
12889 /* Attach template arguments to function. */
12890 if (!template_args.empty ())
12891 {
12892 gdb_assert (templ_func != NULL);
12893
12894 templ_func->n_template_arguments = template_args.size ();
12895 templ_func->template_arguments
12896 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12897 templ_func->n_template_arguments);
12898 memcpy (templ_func->template_arguments,
12899 template_args.data (),
12900 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12901
12902 /* Make sure that the symtab is set on the new symbols. Even
12903 though they don't appear in this symtab directly, other parts
12904 of gdb assume that symbols do, and this is reasonably
12905 true. */
12906 for (symbol *sym : template_args)
12907 symbol_set_symtab (sym, symbol_symtab (templ_func));
12908 }
12909
12910 /* In C++, we can have functions nested inside functions (e.g., when
12911 a function declares a class that has methods). This means that
12912 when we finish processing a function scope, we may need to go
12913 back to building a containing block's symbol lists. */
12914 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12915 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12916
12917 /* If we've finished processing a top-level function, subsequent
12918 symbols go in the file symbol list. */
12919 if (cu->get_builder ()->outermost_context_p ())
12920 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12921 }
12922
12923 /* Process all the DIES contained within a lexical block scope. Start
12924 a new scope, process the dies, and then close the scope. */
12925
12926 static void
12927 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12928 {
12929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12931 CORE_ADDR lowpc, highpc;
12932 struct die_info *child_die;
12933 CORE_ADDR baseaddr;
12934
12935 baseaddr = objfile->text_section_offset ();
12936
12937 /* Ignore blocks with missing or invalid low and high pc attributes. */
12938 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12939 as multiple lexical blocks? Handling children in a sane way would
12940 be nasty. Might be easier to properly extend generic blocks to
12941 describe ranges. */
12942 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12943 {
12944 case PC_BOUNDS_NOT_PRESENT:
12945 /* DW_TAG_lexical_block has no attributes, process its children as if
12946 there was no wrapping by that DW_TAG_lexical_block.
12947 GCC does no longer produces such DWARF since GCC r224161. */
12948 for (child_die = die->child;
12949 child_die != NULL && child_die->tag;
12950 child_die = child_die->sibling)
12951 process_die (child_die, cu);
12952 return;
12953 case PC_BOUNDS_INVALID:
12954 return;
12955 }
12956 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12957 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12958
12959 cu->get_builder ()->push_context (0, lowpc);
12960 if (die->child != NULL)
12961 {
12962 child_die = die->child;
12963 while (child_die && child_die->tag)
12964 {
12965 process_die (child_die, cu);
12966 child_die = child_die->sibling;
12967 }
12968 }
12969 inherit_abstract_dies (die, cu);
12970 struct context_stack cstk = cu->get_builder ()->pop_context ();
12971
12972 if (*cu->get_builder ()->get_local_symbols () != NULL
12973 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12974 {
12975 struct block *block
12976 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12977 cstk.start_addr, highpc);
12978
12979 /* Note that recording ranges after traversing children, as we
12980 do here, means that recording a parent's ranges entails
12981 walking across all its children's ranges as they appear in
12982 the address map, which is quadratic behavior.
12983
12984 It would be nicer to record the parent's ranges before
12985 traversing its children, simply overriding whatever you find
12986 there. But since we don't even decide whether to create a
12987 block until after we've traversed its children, that's hard
12988 to do. */
12989 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12990 }
12991 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12992 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12993 }
12994
12995 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12996
12997 static void
12998 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12999 {
13000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13001 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13002 CORE_ADDR pc, baseaddr;
13003 struct attribute *attr;
13004 struct call_site *call_site, call_site_local;
13005 void **slot;
13006 int nparams;
13007 struct die_info *child_die;
13008
13009 baseaddr = objfile->text_section_offset ();
13010
13011 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13012 if (attr == NULL)
13013 {
13014 /* This was a pre-DWARF-5 GNU extension alias
13015 for DW_AT_call_return_pc. */
13016 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13017 }
13018 if (!attr)
13019 {
13020 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13021 "DIE %s [in module %s]"),
13022 sect_offset_str (die->sect_off), objfile_name (objfile));
13023 return;
13024 }
13025 pc = attr->value_as_address () + baseaddr;
13026 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13027
13028 if (cu->call_site_htab == NULL)
13029 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13030 NULL, &objfile->objfile_obstack,
13031 hashtab_obstack_allocate, NULL);
13032 call_site_local.pc = pc;
13033 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13034 if (*slot != NULL)
13035 {
13036 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13037 "DIE %s [in module %s]"),
13038 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13039 objfile_name (objfile));
13040 return;
13041 }
13042
13043 /* Count parameters at the caller. */
13044
13045 nparams = 0;
13046 for (child_die = die->child; child_die && child_die->tag;
13047 child_die = child_die->sibling)
13048 {
13049 if (child_die->tag != DW_TAG_call_site_parameter
13050 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13051 {
13052 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13053 "DW_TAG_call_site child DIE %s [in module %s]"),
13054 child_die->tag, sect_offset_str (child_die->sect_off),
13055 objfile_name (objfile));
13056 continue;
13057 }
13058
13059 nparams++;
13060 }
13061
13062 call_site
13063 = ((struct call_site *)
13064 obstack_alloc (&objfile->objfile_obstack,
13065 sizeof (*call_site)
13066 + (sizeof (*call_site->parameter) * (nparams - 1))));
13067 *slot = call_site;
13068 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13069 call_site->pc = pc;
13070
13071 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13072 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13073 {
13074 struct die_info *func_die;
13075
13076 /* Skip also over DW_TAG_inlined_subroutine. */
13077 for (func_die = die->parent;
13078 func_die && func_die->tag != DW_TAG_subprogram
13079 && func_die->tag != DW_TAG_subroutine_type;
13080 func_die = func_die->parent);
13081
13082 /* DW_AT_call_all_calls is a superset
13083 of DW_AT_call_all_tail_calls. */
13084 if (func_die
13085 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13086 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13087 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13088 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13089 {
13090 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13091 not complete. But keep CALL_SITE for look ups via call_site_htab,
13092 both the initial caller containing the real return address PC and
13093 the final callee containing the current PC of a chain of tail
13094 calls do not need to have the tail call list complete. But any
13095 function candidate for a virtual tail call frame searched via
13096 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13097 determined unambiguously. */
13098 }
13099 else
13100 {
13101 struct type *func_type = NULL;
13102
13103 if (func_die)
13104 func_type = get_die_type (func_die, cu);
13105 if (func_type != NULL)
13106 {
13107 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13108
13109 /* Enlist this call site to the function. */
13110 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13111 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13112 }
13113 else
13114 complaint (_("Cannot find function owning DW_TAG_call_site "
13115 "DIE %s [in module %s]"),
13116 sect_offset_str (die->sect_off), objfile_name (objfile));
13117 }
13118 }
13119
13120 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13121 if (attr == NULL)
13122 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13123 if (attr == NULL)
13124 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13125 if (attr == NULL)
13126 {
13127 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13128 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13129 }
13130 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13131 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13132 /* Keep NULL DWARF_BLOCK. */;
13133 else if (attr->form_is_block ())
13134 {
13135 struct dwarf2_locexpr_baton *dlbaton;
13136
13137 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13138 dlbaton->data = DW_BLOCK (attr)->data;
13139 dlbaton->size = DW_BLOCK (attr)->size;
13140 dlbaton->per_cu = cu->per_cu;
13141
13142 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13143 }
13144 else if (attr->form_is_ref ())
13145 {
13146 struct dwarf2_cu *target_cu = cu;
13147 struct die_info *target_die;
13148
13149 target_die = follow_die_ref (die, attr, &target_cu);
13150 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13151 if (die_is_declaration (target_die, target_cu))
13152 {
13153 const char *target_physname;
13154
13155 /* Prefer the mangled name; otherwise compute the demangled one. */
13156 target_physname = dw2_linkage_name (target_die, target_cu);
13157 if (target_physname == NULL)
13158 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13159 if (target_physname == NULL)
13160 complaint (_("DW_AT_call_target target DIE has invalid "
13161 "physname, for referencing DIE %s [in module %s]"),
13162 sect_offset_str (die->sect_off), objfile_name (objfile));
13163 else
13164 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13165 }
13166 else
13167 {
13168 CORE_ADDR lowpc;
13169
13170 /* DW_AT_entry_pc should be preferred. */
13171 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13172 <= PC_BOUNDS_INVALID)
13173 complaint (_("DW_AT_call_target target DIE has invalid "
13174 "low pc, for referencing DIE %s [in module %s]"),
13175 sect_offset_str (die->sect_off), objfile_name (objfile));
13176 else
13177 {
13178 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13179 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13180 }
13181 }
13182 }
13183 else
13184 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13185 "block nor reference, for DIE %s [in module %s]"),
13186 sect_offset_str (die->sect_off), objfile_name (objfile));
13187
13188 call_site->per_cu = cu->per_cu;
13189
13190 for (child_die = die->child;
13191 child_die && child_die->tag;
13192 child_die = child_die->sibling)
13193 {
13194 struct call_site_parameter *parameter;
13195 struct attribute *loc, *origin;
13196
13197 if (child_die->tag != DW_TAG_call_site_parameter
13198 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13199 {
13200 /* Already printed the complaint above. */
13201 continue;
13202 }
13203
13204 gdb_assert (call_site->parameter_count < nparams);
13205 parameter = &call_site->parameter[call_site->parameter_count];
13206
13207 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13208 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13209 register is contained in DW_AT_call_value. */
13210
13211 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13212 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13213 if (origin == NULL)
13214 {
13215 /* This was a pre-DWARF-5 GNU extension alias
13216 for DW_AT_call_parameter. */
13217 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13218 }
13219 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13220 {
13221 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13222
13223 sect_offset sect_off = origin->get_ref_die_offset ();
13224 if (!cu->header.offset_in_cu_p (sect_off))
13225 {
13226 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13227 binding can be done only inside one CU. Such referenced DIE
13228 therefore cannot be even moved to DW_TAG_partial_unit. */
13229 complaint (_("DW_AT_call_parameter offset is not in CU for "
13230 "DW_TAG_call_site child DIE %s [in module %s]"),
13231 sect_offset_str (child_die->sect_off),
13232 objfile_name (objfile));
13233 continue;
13234 }
13235 parameter->u.param_cu_off
13236 = (cu_offset) (sect_off - cu->header.sect_off);
13237 }
13238 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13239 {
13240 complaint (_("No DW_FORM_block* DW_AT_location for "
13241 "DW_TAG_call_site child DIE %s [in module %s]"),
13242 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13243 continue;
13244 }
13245 else
13246 {
13247 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13248 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13249 if (parameter->u.dwarf_reg != -1)
13250 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13251 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13252 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13253 &parameter->u.fb_offset))
13254 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13255 else
13256 {
13257 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13258 "for DW_FORM_block* DW_AT_location is supported for "
13259 "DW_TAG_call_site child DIE %s "
13260 "[in module %s]"),
13261 sect_offset_str (child_die->sect_off),
13262 objfile_name (objfile));
13263 continue;
13264 }
13265 }
13266
13267 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13268 if (attr == NULL)
13269 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13270 if (attr == NULL || !attr->form_is_block ())
13271 {
13272 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13273 "DW_TAG_call_site child DIE %s [in module %s]"),
13274 sect_offset_str (child_die->sect_off),
13275 objfile_name (objfile));
13276 continue;
13277 }
13278 parameter->value = DW_BLOCK (attr)->data;
13279 parameter->value_size = DW_BLOCK (attr)->size;
13280
13281 /* Parameters are not pre-cleared by memset above. */
13282 parameter->data_value = NULL;
13283 parameter->data_value_size = 0;
13284 call_site->parameter_count++;
13285
13286 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13287 if (attr == NULL)
13288 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13289 if (attr != nullptr)
13290 {
13291 if (!attr->form_is_block ())
13292 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13293 "DW_TAG_call_site child DIE %s [in module %s]"),
13294 sect_offset_str (child_die->sect_off),
13295 objfile_name (objfile));
13296 else
13297 {
13298 parameter->data_value = DW_BLOCK (attr)->data;
13299 parameter->data_value_size = DW_BLOCK (attr)->size;
13300 }
13301 }
13302 }
13303 }
13304
13305 /* Helper function for read_variable. If DIE represents a virtual
13306 table, then return the type of the concrete object that is
13307 associated with the virtual table. Otherwise, return NULL. */
13308
13309 static struct type *
13310 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13311 {
13312 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13313 if (attr == NULL)
13314 return NULL;
13315
13316 /* Find the type DIE. */
13317 struct die_info *type_die = NULL;
13318 struct dwarf2_cu *type_cu = cu;
13319
13320 if (attr->form_is_ref ())
13321 type_die = follow_die_ref (die, attr, &type_cu);
13322 if (type_die == NULL)
13323 return NULL;
13324
13325 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13326 return NULL;
13327 return die_containing_type (type_die, type_cu);
13328 }
13329
13330 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13331
13332 static void
13333 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13334 {
13335 struct rust_vtable_symbol *storage = NULL;
13336
13337 if (cu->language == language_rust)
13338 {
13339 struct type *containing_type = rust_containing_type (die, cu);
13340
13341 if (containing_type != NULL)
13342 {
13343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13344
13345 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13346 initialize_objfile_symbol (storage);
13347 storage->concrete_type = containing_type;
13348 storage->subclass = SYMBOL_RUST_VTABLE;
13349 }
13350 }
13351
13352 struct symbol *res = new_symbol (die, NULL, cu, storage);
13353 struct attribute *abstract_origin
13354 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13355 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13356 if (res == NULL && loc && abstract_origin)
13357 {
13358 /* We have a variable without a name, but with a location and an abstract
13359 origin. This may be a concrete instance of an abstract variable
13360 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13361 later. */
13362 struct dwarf2_cu *origin_cu = cu;
13363 struct die_info *origin_die
13364 = follow_die_ref (die, abstract_origin, &origin_cu);
13365 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13366 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13367 }
13368 }
13369
13370 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13371 reading .debug_rnglists.
13372 Callback's type should be:
13373 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13374 Return true if the attributes are present and valid, otherwise,
13375 return false. */
13376
13377 template <typename Callback>
13378 static bool
13379 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13380 Callback &&callback)
13381 {
13382 struct dwarf2_per_objfile *dwarf2_per_objfile
13383 = cu->per_cu->dwarf2_per_objfile;
13384 struct objfile *objfile = dwarf2_per_objfile->objfile;
13385 bfd *obfd = objfile->obfd;
13386 /* Base address selection entry. */
13387 gdb::optional<CORE_ADDR> base;
13388 const gdb_byte *buffer;
13389 CORE_ADDR baseaddr;
13390 bool overflow = false;
13391
13392 base = cu->base_address;
13393
13394 dwarf2_per_objfile->rnglists.read (objfile);
13395 if (offset >= dwarf2_per_objfile->rnglists.size)
13396 {
13397 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13398 offset);
13399 return false;
13400 }
13401 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13402
13403 baseaddr = objfile->text_section_offset ();
13404
13405 while (1)
13406 {
13407 /* Initialize it due to a false compiler warning. */
13408 CORE_ADDR range_beginning = 0, range_end = 0;
13409 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13410 + dwarf2_per_objfile->rnglists.size);
13411 unsigned int bytes_read;
13412
13413 if (buffer == buf_end)
13414 {
13415 overflow = true;
13416 break;
13417 }
13418 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13419 switch (rlet)
13420 {
13421 case DW_RLE_end_of_list:
13422 break;
13423 case DW_RLE_base_address:
13424 if (buffer + cu->header.addr_size > buf_end)
13425 {
13426 overflow = true;
13427 break;
13428 }
13429 base = cu->header.read_address (obfd, buffer, &bytes_read);
13430 buffer += bytes_read;
13431 break;
13432 case DW_RLE_start_length:
13433 if (buffer + cu->header.addr_size > buf_end)
13434 {
13435 overflow = true;
13436 break;
13437 }
13438 range_beginning = cu->header.read_address (obfd, buffer,
13439 &bytes_read);
13440 buffer += bytes_read;
13441 range_end = (range_beginning
13442 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13443 buffer += bytes_read;
13444 if (buffer > buf_end)
13445 {
13446 overflow = true;
13447 break;
13448 }
13449 break;
13450 case DW_RLE_offset_pair:
13451 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13452 buffer += bytes_read;
13453 if (buffer > buf_end)
13454 {
13455 overflow = true;
13456 break;
13457 }
13458 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13459 buffer += bytes_read;
13460 if (buffer > buf_end)
13461 {
13462 overflow = true;
13463 break;
13464 }
13465 break;
13466 case DW_RLE_start_end:
13467 if (buffer + 2 * cu->header.addr_size > buf_end)
13468 {
13469 overflow = true;
13470 break;
13471 }
13472 range_beginning = cu->header.read_address (obfd, buffer,
13473 &bytes_read);
13474 buffer += bytes_read;
13475 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13476 buffer += bytes_read;
13477 break;
13478 default:
13479 complaint (_("Invalid .debug_rnglists data (no base address)"));
13480 return false;
13481 }
13482 if (rlet == DW_RLE_end_of_list || overflow)
13483 break;
13484 if (rlet == DW_RLE_base_address)
13485 continue;
13486
13487 if (!base.has_value ())
13488 {
13489 /* We have no valid base address for the ranges
13490 data. */
13491 complaint (_("Invalid .debug_rnglists data (no base address)"));
13492 return false;
13493 }
13494
13495 if (range_beginning > range_end)
13496 {
13497 /* Inverted range entries are invalid. */
13498 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13499 return false;
13500 }
13501
13502 /* Empty range entries have no effect. */
13503 if (range_beginning == range_end)
13504 continue;
13505
13506 range_beginning += *base;
13507 range_end += *base;
13508
13509 /* A not-uncommon case of bad debug info.
13510 Don't pollute the addrmap with bad data. */
13511 if (range_beginning + baseaddr == 0
13512 && !dwarf2_per_objfile->has_section_at_zero)
13513 {
13514 complaint (_(".debug_rnglists entry has start address of zero"
13515 " [in module %s]"), objfile_name (objfile));
13516 continue;
13517 }
13518
13519 callback (range_beginning, range_end);
13520 }
13521
13522 if (overflow)
13523 {
13524 complaint (_("Offset %d is not terminated "
13525 "for DW_AT_ranges attribute"),
13526 offset);
13527 return false;
13528 }
13529
13530 return true;
13531 }
13532
13533 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13534 Callback's type should be:
13535 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13536 Return 1 if the attributes are present and valid, otherwise, return 0. */
13537
13538 template <typename Callback>
13539 static int
13540 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13541 Callback &&callback)
13542 {
13543 struct dwarf2_per_objfile *dwarf2_per_objfile
13544 = cu->per_cu->dwarf2_per_objfile;
13545 struct objfile *objfile = dwarf2_per_objfile->objfile;
13546 struct comp_unit_head *cu_header = &cu->header;
13547 bfd *obfd = objfile->obfd;
13548 unsigned int addr_size = cu_header->addr_size;
13549 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13550 /* Base address selection entry. */
13551 gdb::optional<CORE_ADDR> base;
13552 unsigned int dummy;
13553 const gdb_byte *buffer;
13554 CORE_ADDR baseaddr;
13555
13556 if (cu_header->version >= 5)
13557 return dwarf2_rnglists_process (offset, cu, callback);
13558
13559 base = cu->base_address;
13560
13561 dwarf2_per_objfile->ranges.read (objfile);
13562 if (offset >= dwarf2_per_objfile->ranges.size)
13563 {
13564 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13565 offset);
13566 return 0;
13567 }
13568 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13569
13570 baseaddr = objfile->text_section_offset ();
13571
13572 while (1)
13573 {
13574 CORE_ADDR range_beginning, range_end;
13575
13576 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13577 buffer += addr_size;
13578 range_end = cu->header.read_address (obfd, buffer, &dummy);
13579 buffer += addr_size;
13580 offset += 2 * addr_size;
13581
13582 /* An end of list marker is a pair of zero addresses. */
13583 if (range_beginning == 0 && range_end == 0)
13584 /* Found the end of list entry. */
13585 break;
13586
13587 /* Each base address selection entry is a pair of 2 values.
13588 The first is the largest possible address, the second is
13589 the base address. Check for a base address here. */
13590 if ((range_beginning & mask) == mask)
13591 {
13592 /* If we found the largest possible address, then we already
13593 have the base address in range_end. */
13594 base = range_end;
13595 continue;
13596 }
13597
13598 if (!base.has_value ())
13599 {
13600 /* We have no valid base address for the ranges
13601 data. */
13602 complaint (_("Invalid .debug_ranges data (no base address)"));
13603 return 0;
13604 }
13605
13606 if (range_beginning > range_end)
13607 {
13608 /* Inverted range entries are invalid. */
13609 complaint (_("Invalid .debug_ranges data (inverted range)"));
13610 return 0;
13611 }
13612
13613 /* Empty range entries have no effect. */
13614 if (range_beginning == range_end)
13615 continue;
13616
13617 range_beginning += *base;
13618 range_end += *base;
13619
13620 /* A not-uncommon case of bad debug info.
13621 Don't pollute the addrmap with bad data. */
13622 if (range_beginning + baseaddr == 0
13623 && !dwarf2_per_objfile->has_section_at_zero)
13624 {
13625 complaint (_(".debug_ranges entry has start address of zero"
13626 " [in module %s]"), objfile_name (objfile));
13627 continue;
13628 }
13629
13630 callback (range_beginning, range_end);
13631 }
13632
13633 return 1;
13634 }
13635
13636 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13637 Return 1 if the attributes are present and valid, otherwise, return 0.
13638 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13639
13640 static int
13641 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13642 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13643 dwarf2_psymtab *ranges_pst)
13644 {
13645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13646 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13647 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13648 int low_set = 0;
13649 CORE_ADDR low = 0;
13650 CORE_ADDR high = 0;
13651 int retval;
13652
13653 retval = dwarf2_ranges_process (offset, cu,
13654 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13655 {
13656 if (ranges_pst != NULL)
13657 {
13658 CORE_ADDR lowpc;
13659 CORE_ADDR highpc;
13660
13661 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13662 range_beginning + baseaddr)
13663 - baseaddr);
13664 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13665 range_end + baseaddr)
13666 - baseaddr);
13667 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13668 lowpc, highpc - 1, ranges_pst);
13669 }
13670
13671 /* FIXME: This is recording everything as a low-high
13672 segment of consecutive addresses. We should have a
13673 data structure for discontiguous block ranges
13674 instead. */
13675 if (! low_set)
13676 {
13677 low = range_beginning;
13678 high = range_end;
13679 low_set = 1;
13680 }
13681 else
13682 {
13683 if (range_beginning < low)
13684 low = range_beginning;
13685 if (range_end > high)
13686 high = range_end;
13687 }
13688 });
13689 if (!retval)
13690 return 0;
13691
13692 if (! low_set)
13693 /* If the first entry is an end-of-list marker, the range
13694 describes an empty scope, i.e. no instructions. */
13695 return 0;
13696
13697 if (low_return)
13698 *low_return = low;
13699 if (high_return)
13700 *high_return = high;
13701 return 1;
13702 }
13703
13704 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13705 definition for the return value. *LOWPC and *HIGHPC are set iff
13706 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13707
13708 static enum pc_bounds_kind
13709 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13710 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13711 dwarf2_psymtab *pst)
13712 {
13713 struct dwarf2_per_objfile *dwarf2_per_objfile
13714 = cu->per_cu->dwarf2_per_objfile;
13715 struct attribute *attr;
13716 struct attribute *attr_high;
13717 CORE_ADDR low = 0;
13718 CORE_ADDR high = 0;
13719 enum pc_bounds_kind ret;
13720
13721 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13722 if (attr_high)
13723 {
13724 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13725 if (attr != nullptr)
13726 {
13727 low = attr->value_as_address ();
13728 high = attr_high->value_as_address ();
13729 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13730 high += low;
13731 }
13732 else
13733 /* Found high w/o low attribute. */
13734 return PC_BOUNDS_INVALID;
13735
13736 /* Found consecutive range of addresses. */
13737 ret = PC_BOUNDS_HIGH_LOW;
13738 }
13739 else
13740 {
13741 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13742 if (attr != NULL)
13743 {
13744 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13745 We take advantage of the fact that DW_AT_ranges does not appear
13746 in DW_TAG_compile_unit of DWO files. */
13747 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13748 unsigned int ranges_offset = (DW_UNSND (attr)
13749 + (need_ranges_base
13750 ? cu->ranges_base
13751 : 0));
13752
13753 /* Value of the DW_AT_ranges attribute is the offset in the
13754 .debug_ranges section. */
13755 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13756 return PC_BOUNDS_INVALID;
13757 /* Found discontinuous range of addresses. */
13758 ret = PC_BOUNDS_RANGES;
13759 }
13760 else
13761 return PC_BOUNDS_NOT_PRESENT;
13762 }
13763
13764 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13765 if (high <= low)
13766 return PC_BOUNDS_INVALID;
13767
13768 /* When using the GNU linker, .gnu.linkonce. sections are used to
13769 eliminate duplicate copies of functions and vtables and such.
13770 The linker will arbitrarily choose one and discard the others.
13771 The AT_*_pc values for such functions refer to local labels in
13772 these sections. If the section from that file was discarded, the
13773 labels are not in the output, so the relocs get a value of 0.
13774 If this is a discarded function, mark the pc bounds as invalid,
13775 so that GDB will ignore it. */
13776 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13777 return PC_BOUNDS_INVALID;
13778
13779 *lowpc = low;
13780 if (highpc)
13781 *highpc = high;
13782 return ret;
13783 }
13784
13785 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13786 its low and high PC addresses. Do nothing if these addresses could not
13787 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13788 and HIGHPC to the high address if greater than HIGHPC. */
13789
13790 static void
13791 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13792 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13793 struct dwarf2_cu *cu)
13794 {
13795 CORE_ADDR low, high;
13796 struct die_info *child = die->child;
13797
13798 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13799 {
13800 *lowpc = std::min (*lowpc, low);
13801 *highpc = std::max (*highpc, high);
13802 }
13803
13804 /* If the language does not allow nested subprograms (either inside
13805 subprograms or lexical blocks), we're done. */
13806 if (cu->language != language_ada)
13807 return;
13808
13809 /* Check all the children of the given DIE. If it contains nested
13810 subprograms, then check their pc bounds. Likewise, we need to
13811 check lexical blocks as well, as they may also contain subprogram
13812 definitions. */
13813 while (child && child->tag)
13814 {
13815 if (child->tag == DW_TAG_subprogram
13816 || child->tag == DW_TAG_lexical_block)
13817 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13818 child = child->sibling;
13819 }
13820 }
13821
13822 /* Get the low and high pc's represented by the scope DIE, and store
13823 them in *LOWPC and *HIGHPC. If the correct values can't be
13824 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13825
13826 static void
13827 get_scope_pc_bounds (struct die_info *die,
13828 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13829 struct dwarf2_cu *cu)
13830 {
13831 CORE_ADDR best_low = (CORE_ADDR) -1;
13832 CORE_ADDR best_high = (CORE_ADDR) 0;
13833 CORE_ADDR current_low, current_high;
13834
13835 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13836 >= PC_BOUNDS_RANGES)
13837 {
13838 best_low = current_low;
13839 best_high = current_high;
13840 }
13841 else
13842 {
13843 struct die_info *child = die->child;
13844
13845 while (child && child->tag)
13846 {
13847 switch (child->tag) {
13848 case DW_TAG_subprogram:
13849 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13850 break;
13851 case DW_TAG_namespace:
13852 case DW_TAG_module:
13853 /* FIXME: carlton/2004-01-16: Should we do this for
13854 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13855 that current GCC's always emit the DIEs corresponding
13856 to definitions of methods of classes as children of a
13857 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13858 the DIEs giving the declarations, which could be
13859 anywhere). But I don't see any reason why the
13860 standards says that they have to be there. */
13861 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13862
13863 if (current_low != ((CORE_ADDR) -1))
13864 {
13865 best_low = std::min (best_low, current_low);
13866 best_high = std::max (best_high, current_high);
13867 }
13868 break;
13869 default:
13870 /* Ignore. */
13871 break;
13872 }
13873
13874 child = child->sibling;
13875 }
13876 }
13877
13878 *lowpc = best_low;
13879 *highpc = best_high;
13880 }
13881
13882 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13883 in DIE. */
13884
13885 static void
13886 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13887 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13888 {
13889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13891 struct attribute *attr;
13892 struct attribute *attr_high;
13893
13894 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13895 if (attr_high)
13896 {
13897 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13898 if (attr != nullptr)
13899 {
13900 CORE_ADDR low = attr->value_as_address ();
13901 CORE_ADDR high = attr_high->value_as_address ();
13902
13903 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13904 high += low;
13905
13906 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13907 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13908 cu->get_builder ()->record_block_range (block, low, high - 1);
13909 }
13910 }
13911
13912 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13913 if (attr != nullptr)
13914 {
13915 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13916 We take advantage of the fact that DW_AT_ranges does not appear
13917 in DW_TAG_compile_unit of DWO files. */
13918 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13919
13920 /* The value of the DW_AT_ranges attribute is the offset of the
13921 address range list in the .debug_ranges section. */
13922 unsigned long offset = (DW_UNSND (attr)
13923 + (need_ranges_base ? cu->ranges_base : 0));
13924
13925 std::vector<blockrange> blockvec;
13926 dwarf2_ranges_process (offset, cu,
13927 [&] (CORE_ADDR start, CORE_ADDR end)
13928 {
13929 start += baseaddr;
13930 end += baseaddr;
13931 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13932 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13933 cu->get_builder ()->record_block_range (block, start, end - 1);
13934 blockvec.emplace_back (start, end);
13935 });
13936
13937 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13938 }
13939 }
13940
13941 /* Check whether the producer field indicates either of GCC < 4.6, or the
13942 Intel C/C++ compiler, and cache the result in CU. */
13943
13944 static void
13945 check_producer (struct dwarf2_cu *cu)
13946 {
13947 int major, minor;
13948
13949 if (cu->producer == NULL)
13950 {
13951 /* For unknown compilers expect their behavior is DWARF version
13952 compliant.
13953
13954 GCC started to support .debug_types sections by -gdwarf-4 since
13955 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13956 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13957 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13958 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13959 }
13960 else if (producer_is_gcc (cu->producer, &major, &minor))
13961 {
13962 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13963 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13964 }
13965 else if (producer_is_icc (cu->producer, &major, &minor))
13966 {
13967 cu->producer_is_icc = true;
13968 cu->producer_is_icc_lt_14 = major < 14;
13969 }
13970 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13971 cu->producer_is_codewarrior = true;
13972 else
13973 {
13974 /* For other non-GCC compilers, expect their behavior is DWARF version
13975 compliant. */
13976 }
13977
13978 cu->checked_producer = true;
13979 }
13980
13981 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13982 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13983 during 4.6.0 experimental. */
13984
13985 static bool
13986 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13987 {
13988 if (!cu->checked_producer)
13989 check_producer (cu);
13990
13991 return cu->producer_is_gxx_lt_4_6;
13992 }
13993
13994
13995 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13996 with incorrect is_stmt attributes. */
13997
13998 static bool
13999 producer_is_codewarrior (struct dwarf2_cu *cu)
14000 {
14001 if (!cu->checked_producer)
14002 check_producer (cu);
14003
14004 return cu->producer_is_codewarrior;
14005 }
14006
14007 /* Return the default accessibility type if it is not overridden by
14008 DW_AT_accessibility. */
14009
14010 static enum dwarf_access_attribute
14011 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14012 {
14013 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14014 {
14015 /* The default DWARF 2 accessibility for members is public, the default
14016 accessibility for inheritance is private. */
14017
14018 if (die->tag != DW_TAG_inheritance)
14019 return DW_ACCESS_public;
14020 else
14021 return DW_ACCESS_private;
14022 }
14023 else
14024 {
14025 /* DWARF 3+ defines the default accessibility a different way. The same
14026 rules apply now for DW_TAG_inheritance as for the members and it only
14027 depends on the container kind. */
14028
14029 if (die->parent->tag == DW_TAG_class_type)
14030 return DW_ACCESS_private;
14031 else
14032 return DW_ACCESS_public;
14033 }
14034 }
14035
14036 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14037 offset. If the attribute was not found return 0, otherwise return
14038 1. If it was found but could not properly be handled, set *OFFSET
14039 to 0. */
14040
14041 static int
14042 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14043 LONGEST *offset)
14044 {
14045 struct attribute *attr;
14046
14047 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14048 if (attr != NULL)
14049 {
14050 *offset = 0;
14051
14052 /* Note that we do not check for a section offset first here.
14053 This is because DW_AT_data_member_location is new in DWARF 4,
14054 so if we see it, we can assume that a constant form is really
14055 a constant and not a section offset. */
14056 if (attr->form_is_constant ())
14057 *offset = attr->constant_value (0);
14058 else if (attr->form_is_section_offset ())
14059 dwarf2_complex_location_expr_complaint ();
14060 else if (attr->form_is_block ())
14061 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14062 else
14063 dwarf2_complex_location_expr_complaint ();
14064
14065 return 1;
14066 }
14067
14068 return 0;
14069 }
14070
14071 /* Add an aggregate field to the field list. */
14072
14073 static void
14074 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14075 struct dwarf2_cu *cu)
14076 {
14077 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14078 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14079 struct nextfield *new_field;
14080 struct attribute *attr;
14081 struct field *fp;
14082 const char *fieldname = "";
14083
14084 if (die->tag == DW_TAG_inheritance)
14085 {
14086 fip->baseclasses.emplace_back ();
14087 new_field = &fip->baseclasses.back ();
14088 }
14089 else
14090 {
14091 fip->fields.emplace_back ();
14092 new_field = &fip->fields.back ();
14093 }
14094
14095 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14096 if (attr != nullptr)
14097 new_field->accessibility = DW_UNSND (attr);
14098 else
14099 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14100 if (new_field->accessibility != DW_ACCESS_public)
14101 fip->non_public_fields = 1;
14102
14103 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14104 if (attr != nullptr)
14105 new_field->virtuality = DW_UNSND (attr);
14106 else
14107 new_field->virtuality = DW_VIRTUALITY_none;
14108
14109 fp = &new_field->field;
14110
14111 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14112 {
14113 LONGEST offset;
14114
14115 /* Data member other than a C++ static data member. */
14116
14117 /* Get type of field. */
14118 fp->type = die_type (die, cu);
14119
14120 SET_FIELD_BITPOS (*fp, 0);
14121
14122 /* Get bit size of field (zero if none). */
14123 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14124 if (attr != nullptr)
14125 {
14126 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14127 }
14128 else
14129 {
14130 FIELD_BITSIZE (*fp) = 0;
14131 }
14132
14133 /* Get bit offset of field. */
14134 if (handle_data_member_location (die, cu, &offset))
14135 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14136 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14137 if (attr != nullptr)
14138 {
14139 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14140 {
14141 /* For big endian bits, the DW_AT_bit_offset gives the
14142 additional bit offset from the MSB of the containing
14143 anonymous object to the MSB of the field. We don't
14144 have to do anything special since we don't need to
14145 know the size of the anonymous object. */
14146 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14147 }
14148 else
14149 {
14150 /* For little endian bits, compute the bit offset to the
14151 MSB of the anonymous object, subtract off the number of
14152 bits from the MSB of the field to the MSB of the
14153 object, and then subtract off the number of bits of
14154 the field itself. The result is the bit offset of
14155 the LSB of the field. */
14156 int anonymous_size;
14157 int bit_offset = DW_UNSND (attr);
14158
14159 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14160 if (attr != nullptr)
14161 {
14162 /* The size of the anonymous object containing
14163 the bit field is explicit, so use the
14164 indicated size (in bytes). */
14165 anonymous_size = DW_UNSND (attr);
14166 }
14167 else
14168 {
14169 /* The size of the anonymous object containing
14170 the bit field must be inferred from the type
14171 attribute of the data member containing the
14172 bit field. */
14173 anonymous_size = TYPE_LENGTH (fp->type);
14174 }
14175 SET_FIELD_BITPOS (*fp,
14176 (FIELD_BITPOS (*fp)
14177 + anonymous_size * bits_per_byte
14178 - bit_offset - FIELD_BITSIZE (*fp)));
14179 }
14180 }
14181 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14182 if (attr != NULL)
14183 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14184 + attr->constant_value (0)));
14185
14186 /* Get name of field. */
14187 fieldname = dwarf2_name (die, cu);
14188 if (fieldname == NULL)
14189 fieldname = "";
14190
14191 /* The name is already allocated along with this objfile, so we don't
14192 need to duplicate it for the type. */
14193 fp->name = fieldname;
14194
14195 /* Change accessibility for artificial fields (e.g. virtual table
14196 pointer or virtual base class pointer) to private. */
14197 if (dwarf2_attr (die, DW_AT_artificial, cu))
14198 {
14199 FIELD_ARTIFICIAL (*fp) = 1;
14200 new_field->accessibility = DW_ACCESS_private;
14201 fip->non_public_fields = 1;
14202 }
14203 }
14204 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14205 {
14206 /* C++ static member. */
14207
14208 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14209 is a declaration, but all versions of G++ as of this writing
14210 (so through at least 3.2.1) incorrectly generate
14211 DW_TAG_variable tags. */
14212
14213 const char *physname;
14214
14215 /* Get name of field. */
14216 fieldname = dwarf2_name (die, cu);
14217 if (fieldname == NULL)
14218 return;
14219
14220 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14221 if (attr
14222 /* Only create a symbol if this is an external value.
14223 new_symbol checks this and puts the value in the global symbol
14224 table, which we want. If it is not external, new_symbol
14225 will try to put the value in cu->list_in_scope which is wrong. */
14226 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14227 {
14228 /* A static const member, not much different than an enum as far as
14229 we're concerned, except that we can support more types. */
14230 new_symbol (die, NULL, cu);
14231 }
14232
14233 /* Get physical name. */
14234 physname = dwarf2_physname (fieldname, die, cu);
14235
14236 /* The name is already allocated along with this objfile, so we don't
14237 need to duplicate it for the type. */
14238 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14239 FIELD_TYPE (*fp) = die_type (die, cu);
14240 FIELD_NAME (*fp) = fieldname;
14241 }
14242 else if (die->tag == DW_TAG_inheritance)
14243 {
14244 LONGEST offset;
14245
14246 /* C++ base class field. */
14247 if (handle_data_member_location (die, cu, &offset))
14248 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14249 FIELD_BITSIZE (*fp) = 0;
14250 FIELD_TYPE (*fp) = die_type (die, cu);
14251 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14252 }
14253 else if (die->tag == DW_TAG_variant_part)
14254 {
14255 /* process_structure_scope will treat this DIE as a union. */
14256 process_structure_scope (die, cu);
14257
14258 /* The variant part is relative to the start of the enclosing
14259 structure. */
14260 SET_FIELD_BITPOS (*fp, 0);
14261 fp->type = get_die_type (die, cu);
14262 fp->artificial = 1;
14263 fp->name = "<<variant>>";
14264
14265 /* Normally a DW_TAG_variant_part won't have a size, but our
14266 representation requires one, so set it to the maximum of the
14267 child sizes, being sure to account for the offset at which
14268 each child is seen. */
14269 if (TYPE_LENGTH (fp->type) == 0)
14270 {
14271 unsigned max = 0;
14272 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14273 {
14274 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14275 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14276 if (len > max)
14277 max = len;
14278 }
14279 TYPE_LENGTH (fp->type) = max;
14280 }
14281 }
14282 else
14283 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14284 }
14285
14286 /* Can the type given by DIE define another type? */
14287
14288 static bool
14289 type_can_define_types (const struct die_info *die)
14290 {
14291 switch (die->tag)
14292 {
14293 case DW_TAG_typedef:
14294 case DW_TAG_class_type:
14295 case DW_TAG_structure_type:
14296 case DW_TAG_union_type:
14297 case DW_TAG_enumeration_type:
14298 return true;
14299
14300 default:
14301 return false;
14302 }
14303 }
14304
14305 /* Add a type definition defined in the scope of the FIP's class. */
14306
14307 static void
14308 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14309 struct dwarf2_cu *cu)
14310 {
14311 struct decl_field fp;
14312 memset (&fp, 0, sizeof (fp));
14313
14314 gdb_assert (type_can_define_types (die));
14315
14316 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14317 fp.name = dwarf2_name (die, cu);
14318 fp.type = read_type_die (die, cu);
14319
14320 /* Save accessibility. */
14321 enum dwarf_access_attribute accessibility;
14322 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14323 if (attr != NULL)
14324 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14325 else
14326 accessibility = dwarf2_default_access_attribute (die, cu);
14327 switch (accessibility)
14328 {
14329 case DW_ACCESS_public:
14330 /* The assumed value if neither private nor protected. */
14331 break;
14332 case DW_ACCESS_private:
14333 fp.is_private = 1;
14334 break;
14335 case DW_ACCESS_protected:
14336 fp.is_protected = 1;
14337 break;
14338 default:
14339 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14340 }
14341
14342 if (die->tag == DW_TAG_typedef)
14343 fip->typedef_field_list.push_back (fp);
14344 else
14345 fip->nested_types_list.push_back (fp);
14346 }
14347
14348 /* Create the vector of fields, and attach it to the type. */
14349
14350 static void
14351 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14352 struct dwarf2_cu *cu)
14353 {
14354 int nfields = fip->nfields ();
14355
14356 /* Record the field count, allocate space for the array of fields,
14357 and create blank accessibility bitfields if necessary. */
14358 TYPE_NFIELDS (type) = nfields;
14359 TYPE_FIELDS (type) = (struct field *)
14360 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14361
14362 if (fip->non_public_fields && cu->language != language_ada)
14363 {
14364 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14365
14366 TYPE_FIELD_PRIVATE_BITS (type) =
14367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14368 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14369
14370 TYPE_FIELD_PROTECTED_BITS (type) =
14371 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14372 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14373
14374 TYPE_FIELD_IGNORE_BITS (type) =
14375 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14376 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14377 }
14378
14379 /* If the type has baseclasses, allocate and clear a bit vector for
14380 TYPE_FIELD_VIRTUAL_BITS. */
14381 if (!fip->baseclasses.empty () && cu->language != language_ada)
14382 {
14383 int num_bytes = B_BYTES (fip->baseclasses.size ());
14384 unsigned char *pointer;
14385
14386 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14387 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14388 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14389 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14390 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14391 }
14392
14393 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14394 {
14395 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14396
14397 for (int index = 0; index < nfields; ++index)
14398 {
14399 struct nextfield &field = fip->fields[index];
14400
14401 if (field.variant.is_discriminant)
14402 di->discriminant_index = index;
14403 else if (field.variant.default_branch)
14404 di->default_index = index;
14405 else
14406 di->discriminants[index] = field.variant.discriminant_value;
14407 }
14408 }
14409
14410 /* Copy the saved-up fields into the field vector. */
14411 for (int i = 0; i < nfields; ++i)
14412 {
14413 struct nextfield &field
14414 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14415 : fip->fields[i - fip->baseclasses.size ()]);
14416
14417 TYPE_FIELD (type, i) = field.field;
14418 switch (field.accessibility)
14419 {
14420 case DW_ACCESS_private:
14421 if (cu->language != language_ada)
14422 SET_TYPE_FIELD_PRIVATE (type, i);
14423 break;
14424
14425 case DW_ACCESS_protected:
14426 if (cu->language != language_ada)
14427 SET_TYPE_FIELD_PROTECTED (type, i);
14428 break;
14429
14430 case DW_ACCESS_public:
14431 break;
14432
14433 default:
14434 /* Unknown accessibility. Complain and treat it as public. */
14435 {
14436 complaint (_("unsupported accessibility %d"),
14437 field.accessibility);
14438 }
14439 break;
14440 }
14441 if (i < fip->baseclasses.size ())
14442 {
14443 switch (field.virtuality)
14444 {
14445 case DW_VIRTUALITY_virtual:
14446 case DW_VIRTUALITY_pure_virtual:
14447 if (cu->language == language_ada)
14448 error (_("unexpected virtuality in component of Ada type"));
14449 SET_TYPE_FIELD_VIRTUAL (type, i);
14450 break;
14451 }
14452 }
14453 }
14454 }
14455
14456 /* Return true if this member function is a constructor, false
14457 otherwise. */
14458
14459 static int
14460 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14461 {
14462 const char *fieldname;
14463 const char *type_name;
14464 int len;
14465
14466 if (die->parent == NULL)
14467 return 0;
14468
14469 if (die->parent->tag != DW_TAG_structure_type
14470 && die->parent->tag != DW_TAG_union_type
14471 && die->parent->tag != DW_TAG_class_type)
14472 return 0;
14473
14474 fieldname = dwarf2_name (die, cu);
14475 type_name = dwarf2_name (die->parent, cu);
14476 if (fieldname == NULL || type_name == NULL)
14477 return 0;
14478
14479 len = strlen (fieldname);
14480 return (strncmp (fieldname, type_name, len) == 0
14481 && (type_name[len] == '\0' || type_name[len] == '<'));
14482 }
14483
14484 /* Check if the given VALUE is a recognized enum
14485 dwarf_defaulted_attribute constant according to DWARF5 spec,
14486 Table 7.24. */
14487
14488 static bool
14489 is_valid_DW_AT_defaulted (ULONGEST value)
14490 {
14491 switch (value)
14492 {
14493 case DW_DEFAULTED_no:
14494 case DW_DEFAULTED_in_class:
14495 case DW_DEFAULTED_out_of_class:
14496 return true;
14497 }
14498
14499 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14500 return false;
14501 }
14502
14503 /* Add a member function to the proper fieldlist. */
14504
14505 static void
14506 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14507 struct type *type, struct dwarf2_cu *cu)
14508 {
14509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14510 struct attribute *attr;
14511 int i;
14512 struct fnfieldlist *flp = nullptr;
14513 struct fn_field *fnp;
14514 const char *fieldname;
14515 struct type *this_type;
14516 enum dwarf_access_attribute accessibility;
14517
14518 if (cu->language == language_ada)
14519 error (_("unexpected member function in Ada type"));
14520
14521 /* Get name of member function. */
14522 fieldname = dwarf2_name (die, cu);
14523 if (fieldname == NULL)
14524 return;
14525
14526 /* Look up member function name in fieldlist. */
14527 for (i = 0; i < fip->fnfieldlists.size (); i++)
14528 {
14529 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14530 {
14531 flp = &fip->fnfieldlists[i];
14532 break;
14533 }
14534 }
14535
14536 /* Create a new fnfieldlist if necessary. */
14537 if (flp == nullptr)
14538 {
14539 fip->fnfieldlists.emplace_back ();
14540 flp = &fip->fnfieldlists.back ();
14541 flp->name = fieldname;
14542 i = fip->fnfieldlists.size () - 1;
14543 }
14544
14545 /* Create a new member function field and add it to the vector of
14546 fnfieldlists. */
14547 flp->fnfields.emplace_back ();
14548 fnp = &flp->fnfields.back ();
14549
14550 /* Delay processing of the physname until later. */
14551 if (cu->language == language_cplus)
14552 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14553 die, cu);
14554 else
14555 {
14556 const char *physname = dwarf2_physname (fieldname, die, cu);
14557 fnp->physname = physname ? physname : "";
14558 }
14559
14560 fnp->type = alloc_type (objfile);
14561 this_type = read_type_die (die, cu);
14562 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14563 {
14564 int nparams = TYPE_NFIELDS (this_type);
14565
14566 /* TYPE is the domain of this method, and THIS_TYPE is the type
14567 of the method itself (TYPE_CODE_METHOD). */
14568 smash_to_method_type (fnp->type, type,
14569 TYPE_TARGET_TYPE (this_type),
14570 TYPE_FIELDS (this_type),
14571 TYPE_NFIELDS (this_type),
14572 TYPE_VARARGS (this_type));
14573
14574 /* Handle static member functions.
14575 Dwarf2 has no clean way to discern C++ static and non-static
14576 member functions. G++ helps GDB by marking the first
14577 parameter for non-static member functions (which is the this
14578 pointer) as artificial. We obtain this information from
14579 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14580 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14581 fnp->voffset = VOFFSET_STATIC;
14582 }
14583 else
14584 complaint (_("member function type missing for '%s'"),
14585 dwarf2_full_name (fieldname, die, cu));
14586
14587 /* Get fcontext from DW_AT_containing_type if present. */
14588 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14589 fnp->fcontext = die_containing_type (die, cu);
14590
14591 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14592 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14593
14594 /* Get accessibility. */
14595 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14596 if (attr != nullptr)
14597 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14598 else
14599 accessibility = dwarf2_default_access_attribute (die, cu);
14600 switch (accessibility)
14601 {
14602 case DW_ACCESS_private:
14603 fnp->is_private = 1;
14604 break;
14605 case DW_ACCESS_protected:
14606 fnp->is_protected = 1;
14607 break;
14608 }
14609
14610 /* Check for artificial methods. */
14611 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14612 if (attr && DW_UNSND (attr) != 0)
14613 fnp->is_artificial = 1;
14614
14615 /* Check for defaulted methods. */
14616 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14617 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14618 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14619
14620 /* Check for deleted methods. */
14621 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14622 if (attr != nullptr && DW_UNSND (attr) != 0)
14623 fnp->is_deleted = 1;
14624
14625 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14626
14627 /* Get index in virtual function table if it is a virtual member
14628 function. For older versions of GCC, this is an offset in the
14629 appropriate virtual table, as specified by DW_AT_containing_type.
14630 For everyone else, it is an expression to be evaluated relative
14631 to the object address. */
14632
14633 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14634 if (attr != nullptr)
14635 {
14636 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14637 {
14638 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14639 {
14640 /* Old-style GCC. */
14641 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14642 }
14643 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14644 || (DW_BLOCK (attr)->size > 1
14645 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14646 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14647 {
14648 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14649 if ((fnp->voffset % cu->header.addr_size) != 0)
14650 dwarf2_complex_location_expr_complaint ();
14651 else
14652 fnp->voffset /= cu->header.addr_size;
14653 fnp->voffset += 2;
14654 }
14655 else
14656 dwarf2_complex_location_expr_complaint ();
14657
14658 if (!fnp->fcontext)
14659 {
14660 /* If there is no `this' field and no DW_AT_containing_type,
14661 we cannot actually find a base class context for the
14662 vtable! */
14663 if (TYPE_NFIELDS (this_type) == 0
14664 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14665 {
14666 complaint (_("cannot determine context for virtual member "
14667 "function \"%s\" (offset %s)"),
14668 fieldname, sect_offset_str (die->sect_off));
14669 }
14670 else
14671 {
14672 fnp->fcontext
14673 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14674 }
14675 }
14676 }
14677 else if (attr->form_is_section_offset ())
14678 {
14679 dwarf2_complex_location_expr_complaint ();
14680 }
14681 else
14682 {
14683 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14684 fieldname);
14685 }
14686 }
14687 else
14688 {
14689 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14690 if (attr && DW_UNSND (attr))
14691 {
14692 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14693 complaint (_("Member function \"%s\" (offset %s) is virtual "
14694 "but the vtable offset is not specified"),
14695 fieldname, sect_offset_str (die->sect_off));
14696 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14697 TYPE_CPLUS_DYNAMIC (type) = 1;
14698 }
14699 }
14700 }
14701
14702 /* Create the vector of member function fields, and attach it to the type. */
14703
14704 static void
14705 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14706 struct dwarf2_cu *cu)
14707 {
14708 if (cu->language == language_ada)
14709 error (_("unexpected member functions in Ada type"));
14710
14711 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14712 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14713 TYPE_ALLOC (type,
14714 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14715
14716 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14717 {
14718 struct fnfieldlist &nf = fip->fnfieldlists[i];
14719 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14720
14721 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14722 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14723 fn_flp->fn_fields = (struct fn_field *)
14724 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14725
14726 for (int k = 0; k < nf.fnfields.size (); ++k)
14727 fn_flp->fn_fields[k] = nf.fnfields[k];
14728 }
14729
14730 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14731 }
14732
14733 /* Returns non-zero if NAME is the name of a vtable member in CU's
14734 language, zero otherwise. */
14735 static int
14736 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14737 {
14738 static const char vptr[] = "_vptr";
14739
14740 /* Look for the C++ form of the vtable. */
14741 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14742 return 1;
14743
14744 return 0;
14745 }
14746
14747 /* GCC outputs unnamed structures that are really pointers to member
14748 functions, with the ABI-specified layout. If TYPE describes
14749 such a structure, smash it into a member function type.
14750
14751 GCC shouldn't do this; it should just output pointer to member DIEs.
14752 This is GCC PR debug/28767. */
14753
14754 static void
14755 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14756 {
14757 struct type *pfn_type, *self_type, *new_type;
14758
14759 /* Check for a structure with no name and two children. */
14760 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14761 return;
14762
14763 /* Check for __pfn and __delta members. */
14764 if (TYPE_FIELD_NAME (type, 0) == NULL
14765 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14766 || TYPE_FIELD_NAME (type, 1) == NULL
14767 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14768 return;
14769
14770 /* Find the type of the method. */
14771 pfn_type = TYPE_FIELD_TYPE (type, 0);
14772 if (pfn_type == NULL
14773 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14774 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14775 return;
14776
14777 /* Look for the "this" argument. */
14778 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14779 if (TYPE_NFIELDS (pfn_type) == 0
14780 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14781 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14782 return;
14783
14784 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14785 new_type = alloc_type (objfile);
14786 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14787 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14788 TYPE_VARARGS (pfn_type));
14789 smash_to_methodptr_type (type, new_type);
14790 }
14791
14792 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14793 appropriate error checking and issuing complaints if there is a
14794 problem. */
14795
14796 static ULONGEST
14797 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14798 {
14799 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14800
14801 if (attr == nullptr)
14802 return 0;
14803
14804 if (!attr->form_is_constant ())
14805 {
14806 complaint (_("DW_AT_alignment must have constant form"
14807 " - DIE at %s [in module %s]"),
14808 sect_offset_str (die->sect_off),
14809 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14810 return 0;
14811 }
14812
14813 ULONGEST align;
14814 if (attr->form == DW_FORM_sdata)
14815 {
14816 LONGEST val = DW_SND (attr);
14817 if (val < 0)
14818 {
14819 complaint (_("DW_AT_alignment value must not be negative"
14820 " - DIE at %s [in module %s]"),
14821 sect_offset_str (die->sect_off),
14822 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14823 return 0;
14824 }
14825 align = val;
14826 }
14827 else
14828 align = DW_UNSND (attr);
14829
14830 if (align == 0)
14831 {
14832 complaint (_("DW_AT_alignment value must not be zero"
14833 " - DIE at %s [in module %s]"),
14834 sect_offset_str (die->sect_off),
14835 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14836 return 0;
14837 }
14838 if ((align & (align - 1)) != 0)
14839 {
14840 complaint (_("DW_AT_alignment value must be a power of 2"
14841 " - DIE at %s [in module %s]"),
14842 sect_offset_str (die->sect_off),
14843 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14844 return 0;
14845 }
14846
14847 return align;
14848 }
14849
14850 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14851 the alignment for TYPE. */
14852
14853 static void
14854 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14855 struct type *type)
14856 {
14857 if (!set_type_align (type, get_alignment (cu, die)))
14858 complaint (_("DW_AT_alignment value too large"
14859 " - DIE at %s [in module %s]"),
14860 sect_offset_str (die->sect_off),
14861 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14862 }
14863
14864 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14865 constant for a type, according to DWARF5 spec, Table 5.5. */
14866
14867 static bool
14868 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14869 {
14870 switch (value)
14871 {
14872 case DW_CC_normal:
14873 case DW_CC_pass_by_reference:
14874 case DW_CC_pass_by_value:
14875 return true;
14876
14877 default:
14878 complaint (_("unrecognized DW_AT_calling_convention value "
14879 "(%s) for a type"), pulongest (value));
14880 return false;
14881 }
14882 }
14883
14884 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14885 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14886 also according to GNU-specific values (see include/dwarf2.h). */
14887
14888 static bool
14889 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14890 {
14891 switch (value)
14892 {
14893 case DW_CC_normal:
14894 case DW_CC_program:
14895 case DW_CC_nocall:
14896 return true;
14897
14898 case DW_CC_GNU_renesas_sh:
14899 case DW_CC_GNU_borland_fastcall_i386:
14900 case DW_CC_GDB_IBM_OpenCL:
14901 return true;
14902
14903 default:
14904 complaint (_("unrecognized DW_AT_calling_convention value "
14905 "(%s) for a subroutine"), pulongest (value));
14906 return false;
14907 }
14908 }
14909
14910 /* Called when we find the DIE that starts a structure or union scope
14911 (definition) to create a type for the structure or union. Fill in
14912 the type's name and general properties; the members will not be
14913 processed until process_structure_scope. A symbol table entry for
14914 the type will also not be done until process_structure_scope (assuming
14915 the type has a name).
14916
14917 NOTE: we need to call these functions regardless of whether or not the
14918 DIE has a DW_AT_name attribute, since it might be an anonymous
14919 structure or union. This gets the type entered into our set of
14920 user defined types. */
14921
14922 static struct type *
14923 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14924 {
14925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14926 struct type *type;
14927 struct attribute *attr;
14928 const char *name;
14929
14930 /* If the definition of this type lives in .debug_types, read that type.
14931 Don't follow DW_AT_specification though, that will take us back up
14932 the chain and we want to go down. */
14933 attr = die->attr (DW_AT_signature);
14934 if (attr != nullptr)
14935 {
14936 type = get_DW_AT_signature_type (die, attr, cu);
14937
14938 /* The type's CU may not be the same as CU.
14939 Ensure TYPE is recorded with CU in die_type_hash. */
14940 return set_die_type (die, type, cu);
14941 }
14942
14943 type = alloc_type (objfile);
14944 INIT_CPLUS_SPECIFIC (type);
14945
14946 name = dwarf2_name (die, cu);
14947 if (name != NULL)
14948 {
14949 if (cu->language == language_cplus
14950 || cu->language == language_d
14951 || cu->language == language_rust)
14952 {
14953 const char *full_name = dwarf2_full_name (name, die, cu);
14954
14955 /* dwarf2_full_name might have already finished building the DIE's
14956 type. If so, there is no need to continue. */
14957 if (get_die_type (die, cu) != NULL)
14958 return get_die_type (die, cu);
14959
14960 TYPE_NAME (type) = full_name;
14961 }
14962 else
14963 {
14964 /* The name is already allocated along with this objfile, so
14965 we don't need to duplicate it for the type. */
14966 TYPE_NAME (type) = name;
14967 }
14968 }
14969
14970 if (die->tag == DW_TAG_structure_type)
14971 {
14972 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14973 }
14974 else if (die->tag == DW_TAG_union_type)
14975 {
14976 TYPE_CODE (type) = TYPE_CODE_UNION;
14977 }
14978 else if (die->tag == DW_TAG_variant_part)
14979 {
14980 TYPE_CODE (type) = TYPE_CODE_UNION;
14981 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
14982 }
14983 else
14984 {
14985 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14986 }
14987
14988 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14989 TYPE_DECLARED_CLASS (type) = 1;
14990
14991 /* Store the calling convention in the type if it's available in
14992 the die. Otherwise the calling convention remains set to
14993 the default value DW_CC_normal. */
14994 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14995 if (attr != nullptr
14996 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
14997 {
14998 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14999 TYPE_CPLUS_CALLING_CONVENTION (type)
15000 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15001 }
15002
15003 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15004 if (attr != nullptr)
15005 {
15006 if (attr->form_is_constant ())
15007 TYPE_LENGTH (type) = DW_UNSND (attr);
15008 else
15009 {
15010 /* For the moment, dynamic type sizes are not supported
15011 by GDB's struct type. The actual size is determined
15012 on-demand when resolving the type of a given object,
15013 so set the type's length to zero for now. Otherwise,
15014 we record an expression as the length, and that expression
15015 could lead to a very large value, which could eventually
15016 lead to us trying to allocate that much memory when creating
15017 a value of that type. */
15018 TYPE_LENGTH (type) = 0;
15019 }
15020 }
15021 else
15022 {
15023 TYPE_LENGTH (type) = 0;
15024 }
15025
15026 maybe_set_alignment (cu, die, type);
15027
15028 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15029 {
15030 /* ICC<14 does not output the required DW_AT_declaration on
15031 incomplete types, but gives them a size of zero. */
15032 TYPE_STUB (type) = 1;
15033 }
15034 else
15035 TYPE_STUB_SUPPORTED (type) = 1;
15036
15037 if (die_is_declaration (die, cu))
15038 TYPE_STUB (type) = 1;
15039 else if (attr == NULL && die->child == NULL
15040 && producer_is_realview (cu->producer))
15041 /* RealView does not output the required DW_AT_declaration
15042 on incomplete types. */
15043 TYPE_STUB (type) = 1;
15044
15045 /* We need to add the type field to the die immediately so we don't
15046 infinitely recurse when dealing with pointers to the structure
15047 type within the structure itself. */
15048 set_die_type (die, type, cu);
15049
15050 /* set_die_type should be already done. */
15051 set_descriptive_type (type, die, cu);
15052
15053 return type;
15054 }
15055
15056 /* A helper for process_structure_scope that handles a single member
15057 DIE. */
15058
15059 static void
15060 handle_struct_member_die (struct die_info *child_die, struct type *type,
15061 struct field_info *fi,
15062 std::vector<struct symbol *> *template_args,
15063 struct dwarf2_cu *cu)
15064 {
15065 if (child_die->tag == DW_TAG_member
15066 || child_die->tag == DW_TAG_variable
15067 || child_die->tag == DW_TAG_variant_part)
15068 {
15069 /* NOTE: carlton/2002-11-05: A C++ static data member
15070 should be a DW_TAG_member that is a declaration, but
15071 all versions of G++ as of this writing (so through at
15072 least 3.2.1) incorrectly generate DW_TAG_variable
15073 tags for them instead. */
15074 dwarf2_add_field (fi, child_die, cu);
15075 }
15076 else if (child_die->tag == DW_TAG_subprogram)
15077 {
15078 /* Rust doesn't have member functions in the C++ sense.
15079 However, it does emit ordinary functions as children
15080 of a struct DIE. */
15081 if (cu->language == language_rust)
15082 read_func_scope (child_die, cu);
15083 else
15084 {
15085 /* C++ member function. */
15086 dwarf2_add_member_fn (fi, child_die, type, cu);
15087 }
15088 }
15089 else if (child_die->tag == DW_TAG_inheritance)
15090 {
15091 /* C++ base class field. */
15092 dwarf2_add_field (fi, child_die, cu);
15093 }
15094 else if (type_can_define_types (child_die))
15095 dwarf2_add_type_defn (fi, child_die, cu);
15096 else if (child_die->tag == DW_TAG_template_type_param
15097 || child_die->tag == DW_TAG_template_value_param)
15098 {
15099 struct symbol *arg = new_symbol (child_die, NULL, cu);
15100
15101 if (arg != NULL)
15102 template_args->push_back (arg);
15103 }
15104 else if (child_die->tag == DW_TAG_variant)
15105 {
15106 /* In a variant we want to get the discriminant and also add a
15107 field for our sole member child. */
15108 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15109
15110 for (die_info *variant_child = child_die->child;
15111 variant_child != NULL;
15112 variant_child = variant_child->sibling)
15113 {
15114 if (variant_child->tag == DW_TAG_member)
15115 {
15116 handle_struct_member_die (variant_child, type, fi,
15117 template_args, cu);
15118 /* Only handle the one. */
15119 break;
15120 }
15121 }
15122
15123 /* We don't handle this but we might as well report it if we see
15124 it. */
15125 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15126 complaint (_("DW_AT_discr_list is not supported yet"
15127 " - DIE at %s [in module %s]"),
15128 sect_offset_str (child_die->sect_off),
15129 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15130
15131 /* The first field was just added, so we can stash the
15132 discriminant there. */
15133 gdb_assert (!fi->fields.empty ());
15134 if (discr == NULL)
15135 fi->fields.back ().variant.default_branch = true;
15136 else
15137 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15138 }
15139 }
15140
15141 /* Finish creating a structure or union type, including filling in
15142 its members and creating a symbol for it. */
15143
15144 static void
15145 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15146 {
15147 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15148 struct die_info *child_die;
15149 struct type *type;
15150
15151 type = get_die_type (die, cu);
15152 if (type == NULL)
15153 type = read_structure_type (die, cu);
15154
15155 /* When reading a DW_TAG_variant_part, we need to notice when we
15156 read the discriminant member, so we can record it later in the
15157 discriminant_info. */
15158 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15159 sect_offset discr_offset {};
15160 bool has_template_parameters = false;
15161
15162 if (is_variant_part)
15163 {
15164 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15165 if (discr == NULL)
15166 {
15167 /* Maybe it's a univariant form, an extension we support.
15168 In this case arrange not to check the offset. */
15169 is_variant_part = false;
15170 }
15171 else if (discr->form_is_ref ())
15172 {
15173 struct dwarf2_cu *target_cu = cu;
15174 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15175
15176 discr_offset = target_die->sect_off;
15177 }
15178 else
15179 {
15180 complaint (_("DW_AT_discr does not have DIE reference form"
15181 " - DIE at %s [in module %s]"),
15182 sect_offset_str (die->sect_off),
15183 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15184 is_variant_part = false;
15185 }
15186 }
15187
15188 if (die->child != NULL && ! die_is_declaration (die, cu))
15189 {
15190 struct field_info fi;
15191 std::vector<struct symbol *> template_args;
15192
15193 child_die = die->child;
15194
15195 while (child_die && child_die->tag)
15196 {
15197 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15198
15199 if (is_variant_part && discr_offset == child_die->sect_off)
15200 fi.fields.back ().variant.is_discriminant = true;
15201
15202 child_die = child_die->sibling;
15203 }
15204
15205 /* Attach template arguments to type. */
15206 if (!template_args.empty ())
15207 {
15208 has_template_parameters = true;
15209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15210 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15211 TYPE_TEMPLATE_ARGUMENTS (type)
15212 = XOBNEWVEC (&objfile->objfile_obstack,
15213 struct symbol *,
15214 TYPE_N_TEMPLATE_ARGUMENTS (type));
15215 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15216 template_args.data (),
15217 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15218 * sizeof (struct symbol *)));
15219 }
15220
15221 /* Attach fields and member functions to the type. */
15222 if (fi.nfields () > 0)
15223 dwarf2_attach_fields_to_type (&fi, type, cu);
15224 if (!fi.fnfieldlists.empty ())
15225 {
15226 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15227
15228 /* Get the type which refers to the base class (possibly this
15229 class itself) which contains the vtable pointer for the current
15230 class from the DW_AT_containing_type attribute. This use of
15231 DW_AT_containing_type is a GNU extension. */
15232
15233 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15234 {
15235 struct type *t = die_containing_type (die, cu);
15236
15237 set_type_vptr_basetype (type, t);
15238 if (type == t)
15239 {
15240 int i;
15241
15242 /* Our own class provides vtbl ptr. */
15243 for (i = TYPE_NFIELDS (t) - 1;
15244 i >= TYPE_N_BASECLASSES (t);
15245 --i)
15246 {
15247 const char *fieldname = TYPE_FIELD_NAME (t, i);
15248
15249 if (is_vtable_name (fieldname, cu))
15250 {
15251 set_type_vptr_fieldno (type, i);
15252 break;
15253 }
15254 }
15255
15256 /* Complain if virtual function table field not found. */
15257 if (i < TYPE_N_BASECLASSES (t))
15258 complaint (_("virtual function table pointer "
15259 "not found when defining class '%s'"),
15260 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15261 }
15262 else
15263 {
15264 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15265 }
15266 }
15267 else if (cu->producer
15268 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15269 {
15270 /* The IBM XLC compiler does not provide direct indication
15271 of the containing type, but the vtable pointer is
15272 always named __vfp. */
15273
15274 int i;
15275
15276 for (i = TYPE_NFIELDS (type) - 1;
15277 i >= TYPE_N_BASECLASSES (type);
15278 --i)
15279 {
15280 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15281 {
15282 set_type_vptr_fieldno (type, i);
15283 set_type_vptr_basetype (type, type);
15284 break;
15285 }
15286 }
15287 }
15288 }
15289
15290 /* Copy fi.typedef_field_list linked list elements content into the
15291 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15292 if (!fi.typedef_field_list.empty ())
15293 {
15294 int count = fi.typedef_field_list.size ();
15295
15296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15297 TYPE_TYPEDEF_FIELD_ARRAY (type)
15298 = ((struct decl_field *)
15299 TYPE_ALLOC (type,
15300 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15301 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15302
15303 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15304 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15305 }
15306
15307 /* Copy fi.nested_types_list linked list elements content into the
15308 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15309 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15310 {
15311 int count = fi.nested_types_list.size ();
15312
15313 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15314 TYPE_NESTED_TYPES_ARRAY (type)
15315 = ((struct decl_field *)
15316 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15317 TYPE_NESTED_TYPES_COUNT (type) = count;
15318
15319 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15320 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15321 }
15322 }
15323
15324 quirk_gcc_member_function_pointer (type, objfile);
15325 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15326 cu->rust_unions.push_back (type);
15327
15328 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15329 snapshots) has been known to create a die giving a declaration
15330 for a class that has, as a child, a die giving a definition for a
15331 nested class. So we have to process our children even if the
15332 current die is a declaration. Normally, of course, a declaration
15333 won't have any children at all. */
15334
15335 child_die = die->child;
15336
15337 while (child_die != NULL && child_die->tag)
15338 {
15339 if (child_die->tag == DW_TAG_member
15340 || child_die->tag == DW_TAG_variable
15341 || child_die->tag == DW_TAG_inheritance
15342 || child_die->tag == DW_TAG_template_value_param
15343 || child_die->tag == DW_TAG_template_type_param)
15344 {
15345 /* Do nothing. */
15346 }
15347 else
15348 process_die (child_die, cu);
15349
15350 child_die = child_die->sibling;
15351 }
15352
15353 /* Do not consider external references. According to the DWARF standard,
15354 these DIEs are identified by the fact that they have no byte_size
15355 attribute, and a declaration attribute. */
15356 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15357 || !die_is_declaration (die, cu))
15358 {
15359 struct symbol *sym = new_symbol (die, type, cu);
15360
15361 if (has_template_parameters)
15362 {
15363 struct symtab *symtab;
15364 if (sym != nullptr)
15365 symtab = symbol_symtab (sym);
15366 else if (cu->line_header != nullptr)
15367 {
15368 /* Any related symtab will do. */
15369 symtab
15370 = cu->line_header->file_names ()[0].symtab;
15371 }
15372 else
15373 {
15374 symtab = nullptr;
15375 complaint (_("could not find suitable "
15376 "symtab for template parameter"
15377 " - DIE at %s [in module %s]"),
15378 sect_offset_str (die->sect_off),
15379 objfile_name (objfile));
15380 }
15381
15382 if (symtab != nullptr)
15383 {
15384 /* Make sure that the symtab is set on the new symbols.
15385 Even though they don't appear in this symtab directly,
15386 other parts of gdb assume that symbols do, and this is
15387 reasonably true. */
15388 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15389 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15390 }
15391 }
15392 }
15393 }
15394
15395 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15396 update TYPE using some information only available in DIE's children. */
15397
15398 static void
15399 update_enumeration_type_from_children (struct die_info *die,
15400 struct type *type,
15401 struct dwarf2_cu *cu)
15402 {
15403 struct die_info *child_die;
15404 int unsigned_enum = 1;
15405 int flag_enum = 1;
15406
15407 auto_obstack obstack;
15408
15409 for (child_die = die->child;
15410 child_die != NULL && child_die->tag;
15411 child_die = child_die->sibling)
15412 {
15413 struct attribute *attr;
15414 LONGEST value;
15415 const gdb_byte *bytes;
15416 struct dwarf2_locexpr_baton *baton;
15417 const char *name;
15418
15419 if (child_die->tag != DW_TAG_enumerator)
15420 continue;
15421
15422 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15423 if (attr == NULL)
15424 continue;
15425
15426 name = dwarf2_name (child_die, cu);
15427 if (name == NULL)
15428 name = "<anonymous enumerator>";
15429
15430 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15431 &value, &bytes, &baton);
15432 if (value < 0)
15433 {
15434 unsigned_enum = 0;
15435 flag_enum = 0;
15436 }
15437 else
15438 {
15439 if (count_one_bits_ll (value) >= 2)
15440 flag_enum = 0;
15441 }
15442
15443 /* If we already know that the enum type is neither unsigned, nor
15444 a flag type, no need to look at the rest of the enumerates. */
15445 if (!unsigned_enum && !flag_enum)
15446 break;
15447 }
15448
15449 if (unsigned_enum)
15450 TYPE_UNSIGNED (type) = 1;
15451 if (flag_enum)
15452 TYPE_FLAG_ENUM (type) = 1;
15453 }
15454
15455 /* Given a DW_AT_enumeration_type die, set its type. We do not
15456 complete the type's fields yet, or create any symbols. */
15457
15458 static struct type *
15459 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15460 {
15461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15462 struct type *type;
15463 struct attribute *attr;
15464 const char *name;
15465
15466 /* If the definition of this type lives in .debug_types, read that type.
15467 Don't follow DW_AT_specification though, that will take us back up
15468 the chain and we want to go down. */
15469 attr = die->attr (DW_AT_signature);
15470 if (attr != nullptr)
15471 {
15472 type = get_DW_AT_signature_type (die, attr, cu);
15473
15474 /* The type's CU may not be the same as CU.
15475 Ensure TYPE is recorded with CU in die_type_hash. */
15476 return set_die_type (die, type, cu);
15477 }
15478
15479 type = alloc_type (objfile);
15480
15481 TYPE_CODE (type) = TYPE_CODE_ENUM;
15482 name = dwarf2_full_name (NULL, die, cu);
15483 if (name != NULL)
15484 TYPE_NAME (type) = name;
15485
15486 attr = dwarf2_attr (die, DW_AT_type, cu);
15487 if (attr != NULL)
15488 {
15489 struct type *underlying_type = die_type (die, cu);
15490
15491 TYPE_TARGET_TYPE (type) = underlying_type;
15492 }
15493
15494 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15495 if (attr != nullptr)
15496 {
15497 TYPE_LENGTH (type) = DW_UNSND (attr);
15498 }
15499 else
15500 {
15501 TYPE_LENGTH (type) = 0;
15502 }
15503
15504 maybe_set_alignment (cu, die, type);
15505
15506 /* The enumeration DIE can be incomplete. In Ada, any type can be
15507 declared as private in the package spec, and then defined only
15508 inside the package body. Such types are known as Taft Amendment
15509 Types. When another package uses such a type, an incomplete DIE
15510 may be generated by the compiler. */
15511 if (die_is_declaration (die, cu))
15512 TYPE_STUB (type) = 1;
15513
15514 /* Finish the creation of this type by using the enum's children.
15515 We must call this even when the underlying type has been provided
15516 so that we can determine if we're looking at a "flag" enum. */
15517 update_enumeration_type_from_children (die, type, cu);
15518
15519 /* If this type has an underlying type that is not a stub, then we
15520 may use its attributes. We always use the "unsigned" attribute
15521 in this situation, because ordinarily we guess whether the type
15522 is unsigned -- but the guess can be wrong and the underlying type
15523 can tell us the reality. However, we defer to a local size
15524 attribute if one exists, because this lets the compiler override
15525 the underlying type if needed. */
15526 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15527 {
15528 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15529 if (TYPE_LENGTH (type) == 0)
15530 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15531 if (TYPE_RAW_ALIGN (type) == 0
15532 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15533 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15534 }
15535
15536 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15537
15538 return set_die_type (die, type, cu);
15539 }
15540
15541 /* Given a pointer to a die which begins an enumeration, process all
15542 the dies that define the members of the enumeration, and create the
15543 symbol for the enumeration type.
15544
15545 NOTE: We reverse the order of the element list. */
15546
15547 static void
15548 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15549 {
15550 struct type *this_type;
15551
15552 this_type = get_die_type (die, cu);
15553 if (this_type == NULL)
15554 this_type = read_enumeration_type (die, cu);
15555
15556 if (die->child != NULL)
15557 {
15558 struct die_info *child_die;
15559 struct symbol *sym;
15560 std::vector<struct field> fields;
15561 const char *name;
15562
15563 child_die = die->child;
15564 while (child_die && child_die->tag)
15565 {
15566 if (child_die->tag != DW_TAG_enumerator)
15567 {
15568 process_die (child_die, cu);
15569 }
15570 else
15571 {
15572 name = dwarf2_name (child_die, cu);
15573 if (name)
15574 {
15575 sym = new_symbol (child_die, this_type, cu);
15576
15577 fields.emplace_back ();
15578 struct field &field = fields.back ();
15579
15580 FIELD_NAME (field) = sym->linkage_name ();
15581 FIELD_TYPE (field) = NULL;
15582 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15583 FIELD_BITSIZE (field) = 0;
15584 }
15585 }
15586
15587 child_die = child_die->sibling;
15588 }
15589
15590 if (!fields.empty ())
15591 {
15592 TYPE_NFIELDS (this_type) = fields.size ();
15593 TYPE_FIELDS (this_type) = (struct field *)
15594 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15595 memcpy (TYPE_FIELDS (this_type), fields.data (),
15596 sizeof (struct field) * fields.size ());
15597 }
15598 }
15599
15600 /* If we are reading an enum from a .debug_types unit, and the enum
15601 is a declaration, and the enum is not the signatured type in the
15602 unit, then we do not want to add a symbol for it. Adding a
15603 symbol would in some cases obscure the true definition of the
15604 enum, giving users an incomplete type when the definition is
15605 actually available. Note that we do not want to do this for all
15606 enums which are just declarations, because C++0x allows forward
15607 enum declarations. */
15608 if (cu->per_cu->is_debug_types
15609 && die_is_declaration (die, cu))
15610 {
15611 struct signatured_type *sig_type;
15612
15613 sig_type = (struct signatured_type *) cu->per_cu;
15614 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15615 if (sig_type->type_offset_in_section != die->sect_off)
15616 return;
15617 }
15618
15619 new_symbol (die, this_type, cu);
15620 }
15621
15622 /* Extract all information from a DW_TAG_array_type DIE and put it in
15623 the DIE's type field. For now, this only handles one dimensional
15624 arrays. */
15625
15626 static struct type *
15627 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15628 {
15629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15630 struct die_info *child_die;
15631 struct type *type;
15632 struct type *element_type, *range_type, *index_type;
15633 struct attribute *attr;
15634 const char *name;
15635 struct dynamic_prop *byte_stride_prop = NULL;
15636 unsigned int bit_stride = 0;
15637
15638 element_type = die_type (die, cu);
15639
15640 /* The die_type call above may have already set the type for this DIE. */
15641 type = get_die_type (die, cu);
15642 if (type)
15643 return type;
15644
15645 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15646 if (attr != NULL)
15647 {
15648 int stride_ok;
15649 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15650
15651 byte_stride_prop
15652 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15653 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15654 prop_type);
15655 if (!stride_ok)
15656 {
15657 complaint (_("unable to read array DW_AT_byte_stride "
15658 " - DIE at %s [in module %s]"),
15659 sect_offset_str (die->sect_off),
15660 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15661 /* Ignore this attribute. We will likely not be able to print
15662 arrays of this type correctly, but there is little we can do
15663 to help if we cannot read the attribute's value. */
15664 byte_stride_prop = NULL;
15665 }
15666 }
15667
15668 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15669 if (attr != NULL)
15670 bit_stride = DW_UNSND (attr);
15671
15672 /* Irix 6.2 native cc creates array types without children for
15673 arrays with unspecified length. */
15674 if (die->child == NULL)
15675 {
15676 index_type = objfile_type (objfile)->builtin_int;
15677 range_type = create_static_range_type (NULL, index_type, 0, -1);
15678 type = create_array_type_with_stride (NULL, element_type, range_type,
15679 byte_stride_prop, bit_stride);
15680 return set_die_type (die, type, cu);
15681 }
15682
15683 std::vector<struct type *> range_types;
15684 child_die = die->child;
15685 while (child_die && child_die->tag)
15686 {
15687 if (child_die->tag == DW_TAG_subrange_type)
15688 {
15689 struct type *child_type = read_type_die (child_die, cu);
15690
15691 if (child_type != NULL)
15692 {
15693 /* The range type was succesfully read. Save it for the
15694 array type creation. */
15695 range_types.push_back (child_type);
15696 }
15697 }
15698 child_die = child_die->sibling;
15699 }
15700
15701 /* Dwarf2 dimensions are output from left to right, create the
15702 necessary array types in backwards order. */
15703
15704 type = element_type;
15705
15706 if (read_array_order (die, cu) == DW_ORD_col_major)
15707 {
15708 int i = 0;
15709
15710 while (i < range_types.size ())
15711 type = create_array_type_with_stride (NULL, type, range_types[i++],
15712 byte_stride_prop, bit_stride);
15713 }
15714 else
15715 {
15716 size_t ndim = range_types.size ();
15717 while (ndim-- > 0)
15718 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15719 byte_stride_prop, bit_stride);
15720 }
15721
15722 /* Understand Dwarf2 support for vector types (like they occur on
15723 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15724 array type. This is not part of the Dwarf2/3 standard yet, but a
15725 custom vendor extension. The main difference between a regular
15726 array and the vector variant is that vectors are passed by value
15727 to functions. */
15728 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15729 if (attr != nullptr)
15730 make_vector_type (type);
15731
15732 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15733 implementation may choose to implement triple vectors using this
15734 attribute. */
15735 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15736 if (attr != nullptr)
15737 {
15738 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15739 TYPE_LENGTH (type) = DW_UNSND (attr);
15740 else
15741 complaint (_("DW_AT_byte_size for array type smaller "
15742 "than the total size of elements"));
15743 }
15744
15745 name = dwarf2_name (die, cu);
15746 if (name)
15747 TYPE_NAME (type) = name;
15748
15749 maybe_set_alignment (cu, die, type);
15750
15751 /* Install the type in the die. */
15752 set_die_type (die, type, cu);
15753
15754 /* set_die_type should be already done. */
15755 set_descriptive_type (type, die, cu);
15756
15757 return type;
15758 }
15759
15760 static enum dwarf_array_dim_ordering
15761 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15762 {
15763 struct attribute *attr;
15764
15765 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15766
15767 if (attr != nullptr)
15768 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15769
15770 /* GNU F77 is a special case, as at 08/2004 array type info is the
15771 opposite order to the dwarf2 specification, but data is still
15772 laid out as per normal fortran.
15773
15774 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15775 version checking. */
15776
15777 if (cu->language == language_fortran
15778 && cu->producer && strstr (cu->producer, "GNU F77"))
15779 {
15780 return DW_ORD_row_major;
15781 }
15782
15783 switch (cu->language_defn->la_array_ordering)
15784 {
15785 case array_column_major:
15786 return DW_ORD_col_major;
15787 case array_row_major:
15788 default:
15789 return DW_ORD_row_major;
15790 };
15791 }
15792
15793 /* Extract all information from a DW_TAG_set_type DIE and put it in
15794 the DIE's type field. */
15795
15796 static struct type *
15797 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15798 {
15799 struct type *domain_type, *set_type;
15800 struct attribute *attr;
15801
15802 domain_type = die_type (die, cu);
15803
15804 /* The die_type call above may have already set the type for this DIE. */
15805 set_type = get_die_type (die, cu);
15806 if (set_type)
15807 return set_type;
15808
15809 set_type = create_set_type (NULL, domain_type);
15810
15811 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15812 if (attr != nullptr)
15813 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15814
15815 maybe_set_alignment (cu, die, set_type);
15816
15817 return set_die_type (die, set_type, cu);
15818 }
15819
15820 /* A helper for read_common_block that creates a locexpr baton.
15821 SYM is the symbol which we are marking as computed.
15822 COMMON_DIE is the DIE for the common block.
15823 COMMON_LOC is the location expression attribute for the common
15824 block itself.
15825 MEMBER_LOC is the location expression attribute for the particular
15826 member of the common block that we are processing.
15827 CU is the CU from which the above come. */
15828
15829 static void
15830 mark_common_block_symbol_computed (struct symbol *sym,
15831 struct die_info *common_die,
15832 struct attribute *common_loc,
15833 struct attribute *member_loc,
15834 struct dwarf2_cu *cu)
15835 {
15836 struct dwarf2_per_objfile *dwarf2_per_objfile
15837 = cu->per_cu->dwarf2_per_objfile;
15838 struct objfile *objfile = dwarf2_per_objfile->objfile;
15839 struct dwarf2_locexpr_baton *baton;
15840 gdb_byte *ptr;
15841 unsigned int cu_off;
15842 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15843 LONGEST offset = 0;
15844
15845 gdb_assert (common_loc && member_loc);
15846 gdb_assert (common_loc->form_is_block ());
15847 gdb_assert (member_loc->form_is_block ()
15848 || member_loc->form_is_constant ());
15849
15850 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15851 baton->per_cu = cu->per_cu;
15852 gdb_assert (baton->per_cu);
15853
15854 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15855
15856 if (member_loc->form_is_constant ())
15857 {
15858 offset = member_loc->constant_value (0);
15859 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15860 }
15861 else
15862 baton->size += DW_BLOCK (member_loc)->size;
15863
15864 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15865 baton->data = ptr;
15866
15867 *ptr++ = DW_OP_call4;
15868 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15869 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15870 ptr += 4;
15871
15872 if (member_loc->form_is_constant ())
15873 {
15874 *ptr++ = DW_OP_addr;
15875 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15876 ptr += cu->header.addr_size;
15877 }
15878 else
15879 {
15880 /* We have to copy the data here, because DW_OP_call4 will only
15881 use a DW_AT_location attribute. */
15882 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15883 ptr += DW_BLOCK (member_loc)->size;
15884 }
15885
15886 *ptr++ = DW_OP_plus;
15887 gdb_assert (ptr - baton->data == baton->size);
15888
15889 SYMBOL_LOCATION_BATON (sym) = baton;
15890 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15891 }
15892
15893 /* Create appropriate locally-scoped variables for all the
15894 DW_TAG_common_block entries. Also create a struct common_block
15895 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15896 is used to separate the common blocks name namespace from regular
15897 variable names. */
15898
15899 static void
15900 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15901 {
15902 struct attribute *attr;
15903
15904 attr = dwarf2_attr (die, DW_AT_location, cu);
15905 if (attr != nullptr)
15906 {
15907 /* Support the .debug_loc offsets. */
15908 if (attr->form_is_block ())
15909 {
15910 /* Ok. */
15911 }
15912 else if (attr->form_is_section_offset ())
15913 {
15914 dwarf2_complex_location_expr_complaint ();
15915 attr = NULL;
15916 }
15917 else
15918 {
15919 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15920 "common block member");
15921 attr = NULL;
15922 }
15923 }
15924
15925 if (die->child != NULL)
15926 {
15927 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15928 struct die_info *child_die;
15929 size_t n_entries = 0, size;
15930 struct common_block *common_block;
15931 struct symbol *sym;
15932
15933 for (child_die = die->child;
15934 child_die && child_die->tag;
15935 child_die = child_die->sibling)
15936 ++n_entries;
15937
15938 size = (sizeof (struct common_block)
15939 + (n_entries - 1) * sizeof (struct symbol *));
15940 common_block
15941 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15942 size);
15943 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15944 common_block->n_entries = 0;
15945
15946 for (child_die = die->child;
15947 child_die && child_die->tag;
15948 child_die = child_die->sibling)
15949 {
15950 /* Create the symbol in the DW_TAG_common_block block in the current
15951 symbol scope. */
15952 sym = new_symbol (child_die, NULL, cu);
15953 if (sym != NULL)
15954 {
15955 struct attribute *member_loc;
15956
15957 common_block->contents[common_block->n_entries++] = sym;
15958
15959 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15960 cu);
15961 if (member_loc)
15962 {
15963 /* GDB has handled this for a long time, but it is
15964 not specified by DWARF. It seems to have been
15965 emitted by gfortran at least as recently as:
15966 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15967 complaint (_("Variable in common block has "
15968 "DW_AT_data_member_location "
15969 "- DIE at %s [in module %s]"),
15970 sect_offset_str (child_die->sect_off),
15971 objfile_name (objfile));
15972
15973 if (member_loc->form_is_section_offset ())
15974 dwarf2_complex_location_expr_complaint ();
15975 else if (member_loc->form_is_constant ()
15976 || member_loc->form_is_block ())
15977 {
15978 if (attr != nullptr)
15979 mark_common_block_symbol_computed (sym, die, attr,
15980 member_loc, cu);
15981 }
15982 else
15983 dwarf2_complex_location_expr_complaint ();
15984 }
15985 }
15986 }
15987
15988 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15989 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15990 }
15991 }
15992
15993 /* Create a type for a C++ namespace. */
15994
15995 static struct type *
15996 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15997 {
15998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15999 const char *previous_prefix, *name;
16000 int is_anonymous;
16001 struct type *type;
16002
16003 /* For extensions, reuse the type of the original namespace. */
16004 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16005 {
16006 struct die_info *ext_die;
16007 struct dwarf2_cu *ext_cu = cu;
16008
16009 ext_die = dwarf2_extension (die, &ext_cu);
16010 type = read_type_die (ext_die, ext_cu);
16011
16012 /* EXT_CU may not be the same as CU.
16013 Ensure TYPE is recorded with CU in die_type_hash. */
16014 return set_die_type (die, type, cu);
16015 }
16016
16017 name = namespace_name (die, &is_anonymous, cu);
16018
16019 /* Now build the name of the current namespace. */
16020
16021 previous_prefix = determine_prefix (die, cu);
16022 if (previous_prefix[0] != '\0')
16023 name = typename_concat (&objfile->objfile_obstack,
16024 previous_prefix, name, 0, cu);
16025
16026 /* Create the type. */
16027 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16028
16029 return set_die_type (die, type, cu);
16030 }
16031
16032 /* Read a namespace scope. */
16033
16034 static void
16035 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16036 {
16037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16038 int is_anonymous;
16039
16040 /* Add a symbol associated to this if we haven't seen the namespace
16041 before. Also, add a using directive if it's an anonymous
16042 namespace. */
16043
16044 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16045 {
16046 struct type *type;
16047
16048 type = read_type_die (die, cu);
16049 new_symbol (die, type, cu);
16050
16051 namespace_name (die, &is_anonymous, cu);
16052 if (is_anonymous)
16053 {
16054 const char *previous_prefix = determine_prefix (die, cu);
16055
16056 std::vector<const char *> excludes;
16057 add_using_directive (using_directives (cu),
16058 previous_prefix, TYPE_NAME (type), NULL,
16059 NULL, excludes, 0, &objfile->objfile_obstack);
16060 }
16061 }
16062
16063 if (die->child != NULL)
16064 {
16065 struct die_info *child_die = die->child;
16066
16067 while (child_die && child_die->tag)
16068 {
16069 process_die (child_die, cu);
16070 child_die = child_die->sibling;
16071 }
16072 }
16073 }
16074
16075 /* Read a Fortran module as type. This DIE can be only a declaration used for
16076 imported module. Still we need that type as local Fortran "use ... only"
16077 declaration imports depend on the created type in determine_prefix. */
16078
16079 static struct type *
16080 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16081 {
16082 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16083 const char *module_name;
16084 struct type *type;
16085
16086 module_name = dwarf2_name (die, cu);
16087 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16088
16089 return set_die_type (die, type, cu);
16090 }
16091
16092 /* Read a Fortran module. */
16093
16094 static void
16095 read_module (struct die_info *die, struct dwarf2_cu *cu)
16096 {
16097 struct die_info *child_die = die->child;
16098 struct type *type;
16099
16100 type = read_type_die (die, cu);
16101 new_symbol (die, type, cu);
16102
16103 while (child_die && child_die->tag)
16104 {
16105 process_die (child_die, cu);
16106 child_die = child_die->sibling;
16107 }
16108 }
16109
16110 /* Return the name of the namespace represented by DIE. Set
16111 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16112 namespace. */
16113
16114 static const char *
16115 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16116 {
16117 struct die_info *current_die;
16118 const char *name = NULL;
16119
16120 /* Loop through the extensions until we find a name. */
16121
16122 for (current_die = die;
16123 current_die != NULL;
16124 current_die = dwarf2_extension (die, &cu))
16125 {
16126 /* We don't use dwarf2_name here so that we can detect the absence
16127 of a name -> anonymous namespace. */
16128 name = dwarf2_string_attr (die, DW_AT_name, cu);
16129
16130 if (name != NULL)
16131 break;
16132 }
16133
16134 /* Is it an anonymous namespace? */
16135
16136 *is_anonymous = (name == NULL);
16137 if (*is_anonymous)
16138 name = CP_ANONYMOUS_NAMESPACE_STR;
16139
16140 return name;
16141 }
16142
16143 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16144 the user defined type vector. */
16145
16146 static struct type *
16147 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16148 {
16149 struct gdbarch *gdbarch
16150 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16151 struct comp_unit_head *cu_header = &cu->header;
16152 struct type *type;
16153 struct attribute *attr_byte_size;
16154 struct attribute *attr_address_class;
16155 int byte_size, addr_class;
16156 struct type *target_type;
16157
16158 target_type = die_type (die, cu);
16159
16160 /* The die_type call above may have already set the type for this DIE. */
16161 type = get_die_type (die, cu);
16162 if (type)
16163 return type;
16164
16165 type = lookup_pointer_type (target_type);
16166
16167 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16168 if (attr_byte_size)
16169 byte_size = DW_UNSND (attr_byte_size);
16170 else
16171 byte_size = cu_header->addr_size;
16172
16173 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16174 if (attr_address_class)
16175 addr_class = DW_UNSND (attr_address_class);
16176 else
16177 addr_class = DW_ADDR_none;
16178
16179 ULONGEST alignment = get_alignment (cu, die);
16180
16181 /* If the pointer size, alignment, or address class is different
16182 than the default, create a type variant marked as such and set
16183 the length accordingly. */
16184 if (TYPE_LENGTH (type) != byte_size
16185 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16186 && alignment != TYPE_RAW_ALIGN (type))
16187 || addr_class != DW_ADDR_none)
16188 {
16189 if (gdbarch_address_class_type_flags_p (gdbarch))
16190 {
16191 int type_flags;
16192
16193 type_flags = gdbarch_address_class_type_flags
16194 (gdbarch, byte_size, addr_class);
16195 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16196 == 0);
16197 type = make_type_with_address_space (type, type_flags);
16198 }
16199 else if (TYPE_LENGTH (type) != byte_size)
16200 {
16201 complaint (_("invalid pointer size %d"), byte_size);
16202 }
16203 else if (TYPE_RAW_ALIGN (type) != alignment)
16204 {
16205 complaint (_("Invalid DW_AT_alignment"
16206 " - DIE at %s [in module %s]"),
16207 sect_offset_str (die->sect_off),
16208 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16209 }
16210 else
16211 {
16212 /* Should we also complain about unhandled address classes? */
16213 }
16214 }
16215
16216 TYPE_LENGTH (type) = byte_size;
16217 set_type_align (type, alignment);
16218 return set_die_type (die, type, cu);
16219 }
16220
16221 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16222 the user defined type vector. */
16223
16224 static struct type *
16225 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16226 {
16227 struct type *type;
16228 struct type *to_type;
16229 struct type *domain;
16230
16231 to_type = die_type (die, cu);
16232 domain = die_containing_type (die, cu);
16233
16234 /* The calls above may have already set the type for this DIE. */
16235 type = get_die_type (die, cu);
16236 if (type)
16237 return type;
16238
16239 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16240 type = lookup_methodptr_type (to_type);
16241 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16242 {
16243 struct type *new_type
16244 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16245
16246 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16247 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16248 TYPE_VARARGS (to_type));
16249 type = lookup_methodptr_type (new_type);
16250 }
16251 else
16252 type = lookup_memberptr_type (to_type, domain);
16253
16254 return set_die_type (die, type, cu);
16255 }
16256
16257 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16258 the user defined type vector. */
16259
16260 static struct type *
16261 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16262 enum type_code refcode)
16263 {
16264 struct comp_unit_head *cu_header = &cu->header;
16265 struct type *type, *target_type;
16266 struct attribute *attr;
16267
16268 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16269
16270 target_type = die_type (die, cu);
16271
16272 /* The die_type call above may have already set the type for this DIE. */
16273 type = get_die_type (die, cu);
16274 if (type)
16275 return type;
16276
16277 type = lookup_reference_type (target_type, refcode);
16278 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16279 if (attr != nullptr)
16280 {
16281 TYPE_LENGTH (type) = DW_UNSND (attr);
16282 }
16283 else
16284 {
16285 TYPE_LENGTH (type) = cu_header->addr_size;
16286 }
16287 maybe_set_alignment (cu, die, type);
16288 return set_die_type (die, type, cu);
16289 }
16290
16291 /* Add the given cv-qualifiers to the element type of the array. GCC
16292 outputs DWARF type qualifiers that apply to an array, not the
16293 element type. But GDB relies on the array element type to carry
16294 the cv-qualifiers. This mimics section 6.7.3 of the C99
16295 specification. */
16296
16297 static struct type *
16298 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16299 struct type *base_type, int cnst, int voltl)
16300 {
16301 struct type *el_type, *inner_array;
16302
16303 base_type = copy_type (base_type);
16304 inner_array = base_type;
16305
16306 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16307 {
16308 TYPE_TARGET_TYPE (inner_array) =
16309 copy_type (TYPE_TARGET_TYPE (inner_array));
16310 inner_array = TYPE_TARGET_TYPE (inner_array);
16311 }
16312
16313 el_type = TYPE_TARGET_TYPE (inner_array);
16314 cnst |= TYPE_CONST (el_type);
16315 voltl |= TYPE_VOLATILE (el_type);
16316 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16317
16318 return set_die_type (die, base_type, cu);
16319 }
16320
16321 static struct type *
16322 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16323 {
16324 struct type *base_type, *cv_type;
16325
16326 base_type = die_type (die, cu);
16327
16328 /* The die_type call above may have already set the type for this DIE. */
16329 cv_type = get_die_type (die, cu);
16330 if (cv_type)
16331 return cv_type;
16332
16333 /* In case the const qualifier is applied to an array type, the element type
16334 is so qualified, not the array type (section 6.7.3 of C99). */
16335 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16336 return add_array_cv_type (die, cu, base_type, 1, 0);
16337
16338 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16339 return set_die_type (die, cv_type, cu);
16340 }
16341
16342 static struct type *
16343 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16344 {
16345 struct type *base_type, *cv_type;
16346
16347 base_type = die_type (die, cu);
16348
16349 /* The die_type call above may have already set the type for this DIE. */
16350 cv_type = get_die_type (die, cu);
16351 if (cv_type)
16352 return cv_type;
16353
16354 /* In case the volatile qualifier is applied to an array type, the
16355 element type is so qualified, not the array type (section 6.7.3
16356 of C99). */
16357 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16358 return add_array_cv_type (die, cu, base_type, 0, 1);
16359
16360 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16361 return set_die_type (die, cv_type, cu);
16362 }
16363
16364 /* Handle DW_TAG_restrict_type. */
16365
16366 static struct type *
16367 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16368 {
16369 struct type *base_type, *cv_type;
16370
16371 base_type = die_type (die, cu);
16372
16373 /* The die_type call above may have already set the type for this DIE. */
16374 cv_type = get_die_type (die, cu);
16375 if (cv_type)
16376 return cv_type;
16377
16378 cv_type = make_restrict_type (base_type);
16379 return set_die_type (die, cv_type, cu);
16380 }
16381
16382 /* Handle DW_TAG_atomic_type. */
16383
16384 static struct type *
16385 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16386 {
16387 struct type *base_type, *cv_type;
16388
16389 base_type = die_type (die, cu);
16390
16391 /* The die_type call above may have already set the type for this DIE. */
16392 cv_type = get_die_type (die, cu);
16393 if (cv_type)
16394 return cv_type;
16395
16396 cv_type = make_atomic_type (base_type);
16397 return set_die_type (die, cv_type, cu);
16398 }
16399
16400 /* Extract all information from a DW_TAG_string_type DIE and add to
16401 the user defined type vector. It isn't really a user defined type,
16402 but it behaves like one, with other DIE's using an AT_user_def_type
16403 attribute to reference it. */
16404
16405 static struct type *
16406 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16407 {
16408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16409 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16410 struct type *type, *range_type, *index_type, *char_type;
16411 struct attribute *attr;
16412 struct dynamic_prop prop;
16413 bool length_is_constant = true;
16414 LONGEST length;
16415
16416 /* There are a couple of places where bit sizes might be made use of
16417 when parsing a DW_TAG_string_type, however, no producer that we know
16418 of make use of these. Handling bit sizes that are a multiple of the
16419 byte size is easy enough, but what about other bit sizes? Lets deal
16420 with that problem when we have to. Warn about these attributes being
16421 unsupported, then parse the type and ignore them like we always
16422 have. */
16423 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16424 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16425 {
16426 static bool warning_printed = false;
16427 if (!warning_printed)
16428 {
16429 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16430 "currently supported on DW_TAG_string_type."));
16431 warning_printed = true;
16432 }
16433 }
16434
16435 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16436 if (attr != nullptr && !attr->form_is_constant ())
16437 {
16438 /* The string length describes the location at which the length of
16439 the string can be found. The size of the length field can be
16440 specified with one of the attributes below. */
16441 struct type *prop_type;
16442 struct attribute *len
16443 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16444 if (len == nullptr)
16445 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16446 if (len != nullptr && len->form_is_constant ())
16447 {
16448 /* Pass 0 as the default as we know this attribute is constant
16449 and the default value will not be returned. */
16450 LONGEST sz = len->constant_value (0);
16451 prop_type = cu->per_cu->int_type (sz, true);
16452 }
16453 else
16454 {
16455 /* If the size is not specified then we assume it is the size of
16456 an address on this target. */
16457 prop_type = cu->per_cu->addr_sized_int_type (true);
16458 }
16459
16460 /* Convert the attribute into a dynamic property. */
16461 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16462 length = 1;
16463 else
16464 length_is_constant = false;
16465 }
16466 else if (attr != nullptr)
16467 {
16468 /* This DW_AT_string_length just contains the length with no
16469 indirection. There's no need to create a dynamic property in this
16470 case. Pass 0 for the default value as we know it will not be
16471 returned in this case. */
16472 length = attr->constant_value (0);
16473 }
16474 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16475 {
16476 /* We don't currently support non-constant byte sizes for strings. */
16477 length = attr->constant_value (1);
16478 }
16479 else
16480 {
16481 /* Use 1 as a fallback length if we have nothing else. */
16482 length = 1;
16483 }
16484
16485 index_type = objfile_type (objfile)->builtin_int;
16486 if (length_is_constant)
16487 range_type = create_static_range_type (NULL, index_type, 1, length);
16488 else
16489 {
16490 struct dynamic_prop low_bound;
16491
16492 low_bound.kind = PROP_CONST;
16493 low_bound.data.const_val = 1;
16494 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16495 }
16496 char_type = language_string_char_type (cu->language_defn, gdbarch);
16497 type = create_string_type (NULL, char_type, range_type);
16498
16499 return set_die_type (die, type, cu);
16500 }
16501
16502 /* Assuming that DIE corresponds to a function, returns nonzero
16503 if the function is prototyped. */
16504
16505 static int
16506 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16507 {
16508 struct attribute *attr;
16509
16510 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16511 if (attr && (DW_UNSND (attr) != 0))
16512 return 1;
16513
16514 /* The DWARF standard implies that the DW_AT_prototyped attribute
16515 is only meaningful for C, but the concept also extends to other
16516 languages that allow unprototyped functions (Eg: Objective C).
16517 For all other languages, assume that functions are always
16518 prototyped. */
16519 if (cu->language != language_c
16520 && cu->language != language_objc
16521 && cu->language != language_opencl)
16522 return 1;
16523
16524 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16525 prototyped and unprototyped functions; default to prototyped,
16526 since that is more common in modern code (and RealView warns
16527 about unprototyped functions). */
16528 if (producer_is_realview (cu->producer))
16529 return 1;
16530
16531 return 0;
16532 }
16533
16534 /* Handle DIES due to C code like:
16535
16536 struct foo
16537 {
16538 int (*funcp)(int a, long l);
16539 int b;
16540 };
16541
16542 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16543
16544 static struct type *
16545 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16546 {
16547 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16548 struct type *type; /* Type that this function returns. */
16549 struct type *ftype; /* Function that returns above type. */
16550 struct attribute *attr;
16551
16552 type = die_type (die, cu);
16553
16554 /* The die_type call above may have already set the type for this DIE. */
16555 ftype = get_die_type (die, cu);
16556 if (ftype)
16557 return ftype;
16558
16559 ftype = lookup_function_type (type);
16560
16561 if (prototyped_function_p (die, cu))
16562 TYPE_PROTOTYPED (ftype) = 1;
16563
16564 /* Store the calling convention in the type if it's available in
16565 the subroutine die. Otherwise set the calling convention to
16566 the default value DW_CC_normal. */
16567 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16568 if (attr != nullptr
16569 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16570 TYPE_CALLING_CONVENTION (ftype)
16571 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16572 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16573 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16574 else
16575 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16576
16577 /* Record whether the function returns normally to its caller or not
16578 if the DWARF producer set that information. */
16579 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16580 if (attr && (DW_UNSND (attr) != 0))
16581 TYPE_NO_RETURN (ftype) = 1;
16582
16583 /* We need to add the subroutine type to the die immediately so
16584 we don't infinitely recurse when dealing with parameters
16585 declared as the same subroutine type. */
16586 set_die_type (die, ftype, cu);
16587
16588 if (die->child != NULL)
16589 {
16590 struct type *void_type = objfile_type (objfile)->builtin_void;
16591 struct die_info *child_die;
16592 int nparams, iparams;
16593
16594 /* Count the number of parameters.
16595 FIXME: GDB currently ignores vararg functions, but knows about
16596 vararg member functions. */
16597 nparams = 0;
16598 child_die = die->child;
16599 while (child_die && child_die->tag)
16600 {
16601 if (child_die->tag == DW_TAG_formal_parameter)
16602 nparams++;
16603 else if (child_die->tag == DW_TAG_unspecified_parameters)
16604 TYPE_VARARGS (ftype) = 1;
16605 child_die = child_die->sibling;
16606 }
16607
16608 /* Allocate storage for parameters and fill them in. */
16609 TYPE_NFIELDS (ftype) = nparams;
16610 TYPE_FIELDS (ftype) = (struct field *)
16611 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16612
16613 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16614 even if we error out during the parameters reading below. */
16615 for (iparams = 0; iparams < nparams; iparams++)
16616 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16617
16618 iparams = 0;
16619 child_die = die->child;
16620 while (child_die && child_die->tag)
16621 {
16622 if (child_die->tag == DW_TAG_formal_parameter)
16623 {
16624 struct type *arg_type;
16625
16626 /* DWARF version 2 has no clean way to discern C++
16627 static and non-static member functions. G++ helps
16628 GDB by marking the first parameter for non-static
16629 member functions (which is the this pointer) as
16630 artificial. We pass this information to
16631 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16632
16633 DWARF version 3 added DW_AT_object_pointer, which GCC
16634 4.5 does not yet generate. */
16635 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16636 if (attr != nullptr)
16637 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16638 else
16639 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16640 arg_type = die_type (child_die, cu);
16641
16642 /* RealView does not mark THIS as const, which the testsuite
16643 expects. GCC marks THIS as const in method definitions,
16644 but not in the class specifications (GCC PR 43053). */
16645 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16646 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16647 {
16648 int is_this = 0;
16649 struct dwarf2_cu *arg_cu = cu;
16650 const char *name = dwarf2_name (child_die, cu);
16651
16652 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16653 if (attr != nullptr)
16654 {
16655 /* If the compiler emits this, use it. */
16656 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16657 is_this = 1;
16658 }
16659 else if (name && strcmp (name, "this") == 0)
16660 /* Function definitions will have the argument names. */
16661 is_this = 1;
16662 else if (name == NULL && iparams == 0)
16663 /* Declarations may not have the names, so like
16664 elsewhere in GDB, assume an artificial first
16665 argument is "this". */
16666 is_this = 1;
16667
16668 if (is_this)
16669 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16670 arg_type, 0);
16671 }
16672
16673 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16674 iparams++;
16675 }
16676 child_die = child_die->sibling;
16677 }
16678 }
16679
16680 return ftype;
16681 }
16682
16683 static struct type *
16684 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16685 {
16686 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16687 const char *name = NULL;
16688 struct type *this_type, *target_type;
16689
16690 name = dwarf2_full_name (NULL, die, cu);
16691 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16692 TYPE_TARGET_STUB (this_type) = 1;
16693 set_die_type (die, this_type, cu);
16694 target_type = die_type (die, cu);
16695 if (target_type != this_type)
16696 TYPE_TARGET_TYPE (this_type) = target_type;
16697 else
16698 {
16699 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16700 spec and cause infinite loops in GDB. */
16701 complaint (_("Self-referential DW_TAG_typedef "
16702 "- DIE at %s [in module %s]"),
16703 sect_offset_str (die->sect_off), objfile_name (objfile));
16704 TYPE_TARGET_TYPE (this_type) = NULL;
16705 }
16706 if (name == NULL)
16707 {
16708 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16709 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16710 Handle these by just returning the target type, rather than
16711 constructing an anonymous typedef type and trying to handle this
16712 elsewhere. */
16713 set_die_type (die, target_type, cu);
16714 return target_type;
16715 }
16716 return this_type;
16717 }
16718
16719 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16720 (which may be different from NAME) to the architecture back-end to allow
16721 it to guess the correct format if necessary. */
16722
16723 static struct type *
16724 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16725 const char *name_hint, enum bfd_endian byte_order)
16726 {
16727 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16728 const struct floatformat **format;
16729 struct type *type;
16730
16731 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16732 if (format)
16733 type = init_float_type (objfile, bits, name, format, byte_order);
16734 else
16735 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16736
16737 return type;
16738 }
16739
16740 /* Allocate an integer type of size BITS and name NAME. */
16741
16742 static struct type *
16743 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16744 int bits, int unsigned_p, const char *name)
16745 {
16746 struct type *type;
16747
16748 /* Versions of Intel's C Compiler generate an integer type called "void"
16749 instead of using DW_TAG_unspecified_type. This has been seen on
16750 at least versions 14, 17, and 18. */
16751 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16752 && strcmp (name, "void") == 0)
16753 type = objfile_type (objfile)->builtin_void;
16754 else
16755 type = init_integer_type (objfile, bits, unsigned_p, name);
16756
16757 return type;
16758 }
16759
16760 /* Initialise and return a floating point type of size BITS suitable for
16761 use as a component of a complex number. The NAME_HINT is passed through
16762 when initialising the floating point type and is the name of the complex
16763 type.
16764
16765 As DWARF doesn't currently provide an explicit name for the components
16766 of a complex number, but it can be helpful to have these components
16767 named, we try to select a suitable name based on the size of the
16768 component. */
16769 static struct type *
16770 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16771 struct objfile *objfile,
16772 int bits, const char *name_hint,
16773 enum bfd_endian byte_order)
16774 {
16775 gdbarch *gdbarch = get_objfile_arch (objfile);
16776 struct type *tt = nullptr;
16777
16778 /* Try to find a suitable floating point builtin type of size BITS.
16779 We're going to use the name of this type as the name for the complex
16780 target type that we are about to create. */
16781 switch (cu->language)
16782 {
16783 case language_fortran:
16784 switch (bits)
16785 {
16786 case 32:
16787 tt = builtin_f_type (gdbarch)->builtin_real;
16788 break;
16789 case 64:
16790 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16791 break;
16792 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16793 case 128:
16794 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16795 break;
16796 }
16797 break;
16798 default:
16799 switch (bits)
16800 {
16801 case 32:
16802 tt = builtin_type (gdbarch)->builtin_float;
16803 break;
16804 case 64:
16805 tt = builtin_type (gdbarch)->builtin_double;
16806 break;
16807 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16808 case 128:
16809 tt = builtin_type (gdbarch)->builtin_long_double;
16810 break;
16811 }
16812 break;
16813 }
16814
16815 /* If the type we found doesn't match the size we were looking for, then
16816 pretend we didn't find a type at all, the complex target type we
16817 create will then be nameless. */
16818 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16819 tt = nullptr;
16820
16821 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16822 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16823 }
16824
16825 /* Find a representation of a given base type and install
16826 it in the TYPE field of the die. */
16827
16828 static struct type *
16829 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16830 {
16831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16832 struct type *type;
16833 struct attribute *attr;
16834 int encoding = 0, bits = 0;
16835 const char *name;
16836 gdbarch *arch;
16837
16838 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16839 if (attr != nullptr)
16840 encoding = DW_UNSND (attr);
16841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16842 if (attr != nullptr)
16843 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16844 name = dwarf2_name (die, cu);
16845 if (!name)
16846 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16847
16848 arch = get_objfile_arch (objfile);
16849 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16850
16851 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16852 if (attr)
16853 {
16854 int endianity = DW_UNSND (attr);
16855
16856 switch (endianity)
16857 {
16858 case DW_END_big:
16859 byte_order = BFD_ENDIAN_BIG;
16860 break;
16861 case DW_END_little:
16862 byte_order = BFD_ENDIAN_LITTLE;
16863 break;
16864 default:
16865 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16866 break;
16867 }
16868 }
16869
16870 switch (encoding)
16871 {
16872 case DW_ATE_address:
16873 /* Turn DW_ATE_address into a void * pointer. */
16874 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16875 type = init_pointer_type (objfile, bits, name, type);
16876 break;
16877 case DW_ATE_boolean:
16878 type = init_boolean_type (objfile, bits, 1, name);
16879 break;
16880 case DW_ATE_complex_float:
16881 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16882 byte_order);
16883 type = init_complex_type (objfile, name, type);
16884 break;
16885 case DW_ATE_decimal_float:
16886 type = init_decfloat_type (objfile, bits, name);
16887 break;
16888 case DW_ATE_float:
16889 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16890 break;
16891 case DW_ATE_signed:
16892 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16893 break;
16894 case DW_ATE_unsigned:
16895 if (cu->language == language_fortran
16896 && name
16897 && startswith (name, "character("))
16898 type = init_character_type (objfile, bits, 1, name);
16899 else
16900 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16901 break;
16902 case DW_ATE_signed_char:
16903 if (cu->language == language_ada || cu->language == language_m2
16904 || cu->language == language_pascal
16905 || cu->language == language_fortran)
16906 type = init_character_type (objfile, bits, 0, name);
16907 else
16908 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16909 break;
16910 case DW_ATE_unsigned_char:
16911 if (cu->language == language_ada || cu->language == language_m2
16912 || cu->language == language_pascal
16913 || cu->language == language_fortran
16914 || cu->language == language_rust)
16915 type = init_character_type (objfile, bits, 1, name);
16916 else
16917 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16918 break;
16919 case DW_ATE_UTF:
16920 {
16921 if (bits == 16)
16922 type = builtin_type (arch)->builtin_char16;
16923 else if (bits == 32)
16924 type = builtin_type (arch)->builtin_char32;
16925 else
16926 {
16927 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16928 bits);
16929 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16930 }
16931 return set_die_type (die, type, cu);
16932 }
16933 break;
16934
16935 default:
16936 complaint (_("unsupported DW_AT_encoding: '%s'"),
16937 dwarf_type_encoding_name (encoding));
16938 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16939 break;
16940 }
16941
16942 if (name && strcmp (name, "char") == 0)
16943 TYPE_NOSIGN (type) = 1;
16944
16945 maybe_set_alignment (cu, die, type);
16946
16947 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16948
16949 return set_die_type (die, type, cu);
16950 }
16951
16952 /* Parse dwarf attribute if it's a block, reference or constant and put the
16953 resulting value of the attribute into struct bound_prop.
16954 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16955
16956 static int
16957 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16958 struct dwarf2_cu *cu, struct dynamic_prop *prop,
16959 struct type *default_type)
16960 {
16961 struct dwarf2_property_baton *baton;
16962 struct obstack *obstack
16963 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16964
16965 gdb_assert (default_type != NULL);
16966
16967 if (attr == NULL || prop == NULL)
16968 return 0;
16969
16970 if (attr->form_is_block ())
16971 {
16972 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16973 baton->property_type = default_type;
16974 baton->locexpr.per_cu = cu->per_cu;
16975 baton->locexpr.size = DW_BLOCK (attr)->size;
16976 baton->locexpr.data = DW_BLOCK (attr)->data;
16977 switch (attr->name)
16978 {
16979 case DW_AT_string_length:
16980 baton->locexpr.is_reference = true;
16981 break;
16982 default:
16983 baton->locexpr.is_reference = false;
16984 break;
16985 }
16986 prop->data.baton = baton;
16987 prop->kind = PROP_LOCEXPR;
16988 gdb_assert (prop->data.baton != NULL);
16989 }
16990 else if (attr->form_is_ref ())
16991 {
16992 struct dwarf2_cu *target_cu = cu;
16993 struct die_info *target_die;
16994 struct attribute *target_attr;
16995
16996 target_die = follow_die_ref (die, attr, &target_cu);
16997 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
16998 if (target_attr == NULL)
16999 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17000 target_cu);
17001 if (target_attr == NULL)
17002 return 0;
17003
17004 switch (target_attr->name)
17005 {
17006 case DW_AT_location:
17007 if (target_attr->form_is_section_offset ())
17008 {
17009 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17010 baton->property_type = die_type (target_die, target_cu);
17011 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17012 prop->data.baton = baton;
17013 prop->kind = PROP_LOCLIST;
17014 gdb_assert (prop->data.baton != NULL);
17015 }
17016 else if (target_attr->form_is_block ())
17017 {
17018 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17019 baton->property_type = die_type (target_die, target_cu);
17020 baton->locexpr.per_cu = cu->per_cu;
17021 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17022 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17023 baton->locexpr.is_reference = true;
17024 prop->data.baton = baton;
17025 prop->kind = PROP_LOCEXPR;
17026 gdb_assert (prop->data.baton != NULL);
17027 }
17028 else
17029 {
17030 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17031 "dynamic property");
17032 return 0;
17033 }
17034 break;
17035 case DW_AT_data_member_location:
17036 {
17037 LONGEST offset;
17038
17039 if (!handle_data_member_location (target_die, target_cu,
17040 &offset))
17041 return 0;
17042
17043 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17044 baton->property_type = read_type_die (target_die->parent,
17045 target_cu);
17046 baton->offset_info.offset = offset;
17047 baton->offset_info.type = die_type (target_die, target_cu);
17048 prop->data.baton = baton;
17049 prop->kind = PROP_ADDR_OFFSET;
17050 break;
17051 }
17052 }
17053 }
17054 else if (attr->form_is_constant ())
17055 {
17056 prop->data.const_val = attr->constant_value (0);
17057 prop->kind = PROP_CONST;
17058 }
17059 else
17060 {
17061 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17062 dwarf2_name (die, cu));
17063 return 0;
17064 }
17065
17066 return 1;
17067 }
17068
17069 /* See read.h. */
17070
17071 struct type *
17072 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17073 {
17074 struct objfile *objfile = dwarf2_per_objfile->objfile;
17075 struct type *int_type;
17076
17077 /* Helper macro to examine the various builtin types. */
17078 #define TRY_TYPE(F) \
17079 int_type = (unsigned_p \
17080 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17081 : objfile_type (objfile)->builtin_ ## F); \
17082 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17083 return int_type
17084
17085 TRY_TYPE (char);
17086 TRY_TYPE (short);
17087 TRY_TYPE (int);
17088 TRY_TYPE (long);
17089 TRY_TYPE (long_long);
17090
17091 #undef TRY_TYPE
17092
17093 gdb_assert_not_reached ("unable to find suitable integer type");
17094 }
17095
17096 /* See read.h. */
17097
17098 struct type *
17099 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17100 {
17101 int addr_size = this->addr_size ();
17102 return int_type (addr_size, unsigned_p);
17103 }
17104
17105 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17106 present (which is valid) then compute the default type based on the
17107 compilation units address size. */
17108
17109 static struct type *
17110 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17111 {
17112 struct type *index_type = die_type (die, cu);
17113
17114 /* Dwarf-2 specifications explicitly allows to create subrange types
17115 without specifying a base type.
17116 In that case, the base type must be set to the type of
17117 the lower bound, upper bound or count, in that order, if any of these
17118 three attributes references an object that has a type.
17119 If no base type is found, the Dwarf-2 specifications say that
17120 a signed integer type of size equal to the size of an address should
17121 be used.
17122 For the following C code: `extern char gdb_int [];'
17123 GCC produces an empty range DIE.
17124 FIXME: muller/2010-05-28: Possible references to object for low bound,
17125 high bound or count are not yet handled by this code. */
17126 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17127 index_type = cu->per_cu->addr_sized_int_type (false);
17128
17129 return index_type;
17130 }
17131
17132 /* Read the given DW_AT_subrange DIE. */
17133
17134 static struct type *
17135 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17136 {
17137 struct type *base_type, *orig_base_type;
17138 struct type *range_type;
17139 struct attribute *attr;
17140 struct dynamic_prop low, high;
17141 int low_default_is_valid;
17142 int high_bound_is_count = 0;
17143 const char *name;
17144 ULONGEST negative_mask;
17145
17146 orig_base_type = read_subrange_index_type (die, cu);
17147
17148 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17149 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17150 creating the range type, but we use the result of check_typedef
17151 when examining properties of the type. */
17152 base_type = check_typedef (orig_base_type);
17153
17154 /* The die_type call above may have already set the type for this DIE. */
17155 range_type = get_die_type (die, cu);
17156 if (range_type)
17157 return range_type;
17158
17159 low.kind = PROP_CONST;
17160 high.kind = PROP_CONST;
17161 high.data.const_val = 0;
17162
17163 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17164 omitting DW_AT_lower_bound. */
17165 switch (cu->language)
17166 {
17167 case language_c:
17168 case language_cplus:
17169 low.data.const_val = 0;
17170 low_default_is_valid = 1;
17171 break;
17172 case language_fortran:
17173 low.data.const_val = 1;
17174 low_default_is_valid = 1;
17175 break;
17176 case language_d:
17177 case language_objc:
17178 case language_rust:
17179 low.data.const_val = 0;
17180 low_default_is_valid = (cu->header.version >= 4);
17181 break;
17182 case language_ada:
17183 case language_m2:
17184 case language_pascal:
17185 low.data.const_val = 1;
17186 low_default_is_valid = (cu->header.version >= 4);
17187 break;
17188 default:
17189 low.data.const_val = 0;
17190 low_default_is_valid = 0;
17191 break;
17192 }
17193
17194 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17195 if (attr != nullptr)
17196 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17197 else if (!low_default_is_valid)
17198 complaint (_("Missing DW_AT_lower_bound "
17199 "- DIE at %s [in module %s]"),
17200 sect_offset_str (die->sect_off),
17201 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17202
17203 struct attribute *attr_ub, *attr_count;
17204 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17205 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17206 {
17207 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17208 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17209 {
17210 /* If bounds are constant do the final calculation here. */
17211 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17212 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17213 else
17214 high_bound_is_count = 1;
17215 }
17216 else
17217 {
17218 if (attr_ub != NULL)
17219 complaint (_("Unresolved DW_AT_upper_bound "
17220 "- DIE at %s [in module %s]"),
17221 sect_offset_str (die->sect_off),
17222 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17223 if (attr_count != NULL)
17224 complaint (_("Unresolved DW_AT_count "
17225 "- DIE at %s [in module %s]"),
17226 sect_offset_str (die->sect_off),
17227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17228 }
17229 }
17230
17231 LONGEST bias = 0;
17232 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17233 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17234 bias = bias_attr->constant_value (0);
17235
17236 /* Normally, the DWARF producers are expected to use a signed
17237 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17238 But this is unfortunately not always the case, as witnessed
17239 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17240 is used instead. To work around that ambiguity, we treat
17241 the bounds as signed, and thus sign-extend their values, when
17242 the base type is signed. */
17243 negative_mask =
17244 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17245 if (low.kind == PROP_CONST
17246 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17247 low.data.const_val |= negative_mask;
17248 if (high.kind == PROP_CONST
17249 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17250 high.data.const_val |= negative_mask;
17251
17252 /* Check for bit and byte strides. */
17253 struct dynamic_prop byte_stride_prop;
17254 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17255 if (attr_byte_stride != nullptr)
17256 {
17257 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17258 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17259 prop_type);
17260 }
17261
17262 struct dynamic_prop bit_stride_prop;
17263 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17264 if (attr_bit_stride != nullptr)
17265 {
17266 /* It only makes sense to have either a bit or byte stride. */
17267 if (attr_byte_stride != nullptr)
17268 {
17269 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17270 "- DIE at %s [in module %s]"),
17271 sect_offset_str (die->sect_off),
17272 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17273 attr_bit_stride = nullptr;
17274 }
17275 else
17276 {
17277 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17278 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17279 prop_type);
17280 }
17281 }
17282
17283 if (attr_byte_stride != nullptr
17284 || attr_bit_stride != nullptr)
17285 {
17286 bool byte_stride_p = (attr_byte_stride != nullptr);
17287 struct dynamic_prop *stride
17288 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17289
17290 range_type
17291 = create_range_type_with_stride (NULL, orig_base_type, &low,
17292 &high, bias, stride, byte_stride_p);
17293 }
17294 else
17295 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17296
17297 if (high_bound_is_count)
17298 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17299
17300 /* Ada expects an empty array on no boundary attributes. */
17301 if (attr == NULL && cu->language != language_ada)
17302 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17303
17304 name = dwarf2_name (die, cu);
17305 if (name)
17306 TYPE_NAME (range_type) = name;
17307
17308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17309 if (attr != nullptr)
17310 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17311
17312 maybe_set_alignment (cu, die, range_type);
17313
17314 set_die_type (die, range_type, cu);
17315
17316 /* set_die_type should be already done. */
17317 set_descriptive_type (range_type, die, cu);
17318
17319 return range_type;
17320 }
17321
17322 static struct type *
17323 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17324 {
17325 struct type *type;
17326
17327 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17328 NULL);
17329 TYPE_NAME (type) = dwarf2_name (die, cu);
17330
17331 /* In Ada, an unspecified type is typically used when the description
17332 of the type is deferred to a different unit. When encountering
17333 such a type, we treat it as a stub, and try to resolve it later on,
17334 when needed. */
17335 if (cu->language == language_ada)
17336 TYPE_STUB (type) = 1;
17337
17338 return set_die_type (die, type, cu);
17339 }
17340
17341 /* Read a single die and all its descendents. Set the die's sibling
17342 field to NULL; set other fields in the die correctly, and set all
17343 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17344 location of the info_ptr after reading all of those dies. PARENT
17345 is the parent of the die in question. */
17346
17347 static struct die_info *
17348 read_die_and_children (const struct die_reader_specs *reader,
17349 const gdb_byte *info_ptr,
17350 const gdb_byte **new_info_ptr,
17351 struct die_info *parent)
17352 {
17353 struct die_info *die;
17354 const gdb_byte *cur_ptr;
17355
17356 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17357 if (die == NULL)
17358 {
17359 *new_info_ptr = cur_ptr;
17360 return NULL;
17361 }
17362 store_in_ref_table (die, reader->cu);
17363
17364 if (die->has_children)
17365 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17366 else
17367 {
17368 die->child = NULL;
17369 *new_info_ptr = cur_ptr;
17370 }
17371
17372 die->sibling = NULL;
17373 die->parent = parent;
17374 return die;
17375 }
17376
17377 /* Read a die, all of its descendents, and all of its siblings; set
17378 all of the fields of all of the dies correctly. Arguments are as
17379 in read_die_and_children. */
17380
17381 static struct die_info *
17382 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17383 const gdb_byte *info_ptr,
17384 const gdb_byte **new_info_ptr,
17385 struct die_info *parent)
17386 {
17387 struct die_info *first_die, *last_sibling;
17388 const gdb_byte *cur_ptr;
17389
17390 cur_ptr = info_ptr;
17391 first_die = last_sibling = NULL;
17392
17393 while (1)
17394 {
17395 struct die_info *die
17396 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17397
17398 if (die == NULL)
17399 {
17400 *new_info_ptr = cur_ptr;
17401 return first_die;
17402 }
17403
17404 if (!first_die)
17405 first_die = die;
17406 else
17407 last_sibling->sibling = die;
17408
17409 last_sibling = die;
17410 }
17411 }
17412
17413 /* Read a die, all of its descendents, and all of its siblings; set
17414 all of the fields of all of the dies correctly. Arguments are as
17415 in read_die_and_children.
17416 This the main entry point for reading a DIE and all its children. */
17417
17418 static struct die_info *
17419 read_die_and_siblings (const struct die_reader_specs *reader,
17420 const gdb_byte *info_ptr,
17421 const gdb_byte **new_info_ptr,
17422 struct die_info *parent)
17423 {
17424 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17425 new_info_ptr, parent);
17426
17427 if (dwarf_die_debug)
17428 {
17429 fprintf_unfiltered (gdb_stdlog,
17430 "Read die from %s@0x%x of %s:\n",
17431 reader->die_section->get_name (),
17432 (unsigned) (info_ptr - reader->die_section->buffer),
17433 bfd_get_filename (reader->abfd));
17434 dump_die (die, dwarf_die_debug);
17435 }
17436
17437 return die;
17438 }
17439
17440 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17441 attributes.
17442 The caller is responsible for filling in the extra attributes
17443 and updating (*DIEP)->num_attrs.
17444 Set DIEP to point to a newly allocated die with its information,
17445 except for its child, sibling, and parent fields. */
17446
17447 static const gdb_byte *
17448 read_full_die_1 (const struct die_reader_specs *reader,
17449 struct die_info **diep, const gdb_byte *info_ptr,
17450 int num_extra_attrs)
17451 {
17452 unsigned int abbrev_number, bytes_read, i;
17453 struct abbrev_info *abbrev;
17454 struct die_info *die;
17455 struct dwarf2_cu *cu = reader->cu;
17456 bfd *abfd = reader->abfd;
17457
17458 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17459 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17460 info_ptr += bytes_read;
17461 if (!abbrev_number)
17462 {
17463 *diep = NULL;
17464 return info_ptr;
17465 }
17466
17467 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17468 if (!abbrev)
17469 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17470 abbrev_number,
17471 bfd_get_filename (abfd));
17472
17473 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17474 die->sect_off = sect_off;
17475 die->tag = abbrev->tag;
17476 die->abbrev = abbrev_number;
17477 die->has_children = abbrev->has_children;
17478
17479 /* Make the result usable.
17480 The caller needs to update num_attrs after adding the extra
17481 attributes. */
17482 die->num_attrs = abbrev->num_attrs;
17483
17484 std::vector<int> indexes_that_need_reprocess;
17485 for (i = 0; i < abbrev->num_attrs; ++i)
17486 {
17487 bool need_reprocess;
17488 info_ptr =
17489 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17490 info_ptr, &need_reprocess);
17491 if (need_reprocess)
17492 indexes_that_need_reprocess.push_back (i);
17493 }
17494
17495 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17496 if (attr != nullptr)
17497 cu->str_offsets_base = DW_UNSND (attr);
17498
17499 auto maybe_addr_base = die->addr_base ();
17500 if (maybe_addr_base.has_value ())
17501 cu->addr_base = *maybe_addr_base;
17502 for (int index : indexes_that_need_reprocess)
17503 read_attribute_reprocess (reader, &die->attrs[index]);
17504 *diep = die;
17505 return info_ptr;
17506 }
17507
17508 /* Read a die and all its attributes.
17509 Set DIEP to point to a newly allocated die with its information,
17510 except for its child, sibling, and parent fields. */
17511
17512 static const gdb_byte *
17513 read_full_die (const struct die_reader_specs *reader,
17514 struct die_info **diep, const gdb_byte *info_ptr)
17515 {
17516 const gdb_byte *result;
17517
17518 result = read_full_die_1 (reader, diep, info_ptr, 0);
17519
17520 if (dwarf_die_debug)
17521 {
17522 fprintf_unfiltered (gdb_stdlog,
17523 "Read die from %s@0x%x of %s:\n",
17524 reader->die_section->get_name (),
17525 (unsigned) (info_ptr - reader->die_section->buffer),
17526 bfd_get_filename (reader->abfd));
17527 dump_die (*diep, dwarf_die_debug);
17528 }
17529
17530 return result;
17531 }
17532 \f
17533
17534 /* Returns nonzero if TAG represents a type that we might generate a partial
17535 symbol for. */
17536
17537 static int
17538 is_type_tag_for_partial (int tag)
17539 {
17540 switch (tag)
17541 {
17542 #if 0
17543 /* Some types that would be reasonable to generate partial symbols for,
17544 that we don't at present. */
17545 case DW_TAG_array_type:
17546 case DW_TAG_file_type:
17547 case DW_TAG_ptr_to_member_type:
17548 case DW_TAG_set_type:
17549 case DW_TAG_string_type:
17550 case DW_TAG_subroutine_type:
17551 #endif
17552 case DW_TAG_base_type:
17553 case DW_TAG_class_type:
17554 case DW_TAG_interface_type:
17555 case DW_TAG_enumeration_type:
17556 case DW_TAG_structure_type:
17557 case DW_TAG_subrange_type:
17558 case DW_TAG_typedef:
17559 case DW_TAG_union_type:
17560 return 1;
17561 default:
17562 return 0;
17563 }
17564 }
17565
17566 /* Load all DIEs that are interesting for partial symbols into memory. */
17567
17568 static struct partial_die_info *
17569 load_partial_dies (const struct die_reader_specs *reader,
17570 const gdb_byte *info_ptr, int building_psymtab)
17571 {
17572 struct dwarf2_cu *cu = reader->cu;
17573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17574 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17575 unsigned int bytes_read;
17576 unsigned int load_all = 0;
17577 int nesting_level = 1;
17578
17579 parent_die = NULL;
17580 last_die = NULL;
17581
17582 gdb_assert (cu->per_cu != NULL);
17583 if (cu->per_cu->load_all_dies)
17584 load_all = 1;
17585
17586 cu->partial_dies
17587 = htab_create_alloc_ex (cu->header.length / 12,
17588 partial_die_hash,
17589 partial_die_eq,
17590 NULL,
17591 &cu->comp_unit_obstack,
17592 hashtab_obstack_allocate,
17593 dummy_obstack_deallocate);
17594
17595 while (1)
17596 {
17597 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17598
17599 /* A NULL abbrev means the end of a series of children. */
17600 if (abbrev == NULL)
17601 {
17602 if (--nesting_level == 0)
17603 return first_die;
17604
17605 info_ptr += bytes_read;
17606 last_die = parent_die;
17607 parent_die = parent_die->die_parent;
17608 continue;
17609 }
17610
17611 /* Check for template arguments. We never save these; if
17612 they're seen, we just mark the parent, and go on our way. */
17613 if (parent_die != NULL
17614 && cu->language == language_cplus
17615 && (abbrev->tag == DW_TAG_template_type_param
17616 || abbrev->tag == DW_TAG_template_value_param))
17617 {
17618 parent_die->has_template_arguments = 1;
17619
17620 if (!load_all)
17621 {
17622 /* We don't need a partial DIE for the template argument. */
17623 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17624 continue;
17625 }
17626 }
17627
17628 /* We only recurse into c++ subprograms looking for template arguments.
17629 Skip their other children. */
17630 if (!load_all
17631 && cu->language == language_cplus
17632 && parent_die != NULL
17633 && parent_die->tag == DW_TAG_subprogram)
17634 {
17635 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17636 continue;
17637 }
17638
17639 /* Check whether this DIE is interesting enough to save. Normally
17640 we would not be interested in members here, but there may be
17641 later variables referencing them via DW_AT_specification (for
17642 static members). */
17643 if (!load_all
17644 && !is_type_tag_for_partial (abbrev->tag)
17645 && abbrev->tag != DW_TAG_constant
17646 && abbrev->tag != DW_TAG_enumerator
17647 && abbrev->tag != DW_TAG_subprogram
17648 && abbrev->tag != DW_TAG_inlined_subroutine
17649 && abbrev->tag != DW_TAG_lexical_block
17650 && abbrev->tag != DW_TAG_variable
17651 && abbrev->tag != DW_TAG_namespace
17652 && abbrev->tag != DW_TAG_module
17653 && abbrev->tag != DW_TAG_member
17654 && abbrev->tag != DW_TAG_imported_unit
17655 && abbrev->tag != DW_TAG_imported_declaration)
17656 {
17657 /* Otherwise we skip to the next sibling, if any. */
17658 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17659 continue;
17660 }
17661
17662 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17663 abbrev);
17664
17665 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17666
17667 /* This two-pass algorithm for processing partial symbols has a
17668 high cost in cache pressure. Thus, handle some simple cases
17669 here which cover the majority of C partial symbols. DIEs
17670 which neither have specification tags in them, nor could have
17671 specification tags elsewhere pointing at them, can simply be
17672 processed and discarded.
17673
17674 This segment is also optional; scan_partial_symbols and
17675 add_partial_symbol will handle these DIEs if we chain
17676 them in normally. When compilers which do not emit large
17677 quantities of duplicate debug information are more common,
17678 this code can probably be removed. */
17679
17680 /* Any complete simple types at the top level (pretty much all
17681 of them, for a language without namespaces), can be processed
17682 directly. */
17683 if (parent_die == NULL
17684 && pdi.has_specification == 0
17685 && pdi.is_declaration == 0
17686 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17687 || pdi.tag == DW_TAG_base_type
17688 || pdi.tag == DW_TAG_subrange_type))
17689 {
17690 if (building_psymtab && pdi.name != NULL)
17691 add_psymbol_to_list (pdi.name, false,
17692 VAR_DOMAIN, LOC_TYPEDEF, -1,
17693 psymbol_placement::STATIC,
17694 0, cu->language, objfile);
17695 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17696 continue;
17697 }
17698
17699 /* The exception for DW_TAG_typedef with has_children above is
17700 a workaround of GCC PR debug/47510. In the case of this complaint
17701 type_name_or_error will error on such types later.
17702
17703 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17704 it could not find the child DIEs referenced later, this is checked
17705 above. In correct DWARF DW_TAG_typedef should have no children. */
17706
17707 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17708 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17709 "- DIE at %s [in module %s]"),
17710 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17711
17712 /* If we're at the second level, and we're an enumerator, and
17713 our parent has no specification (meaning possibly lives in a
17714 namespace elsewhere), then we can add the partial symbol now
17715 instead of queueing it. */
17716 if (pdi.tag == DW_TAG_enumerator
17717 && parent_die != NULL
17718 && parent_die->die_parent == NULL
17719 && parent_die->tag == DW_TAG_enumeration_type
17720 && parent_die->has_specification == 0)
17721 {
17722 if (pdi.name == NULL)
17723 complaint (_("malformed enumerator DIE ignored"));
17724 else if (building_psymtab)
17725 add_psymbol_to_list (pdi.name, false,
17726 VAR_DOMAIN, LOC_CONST, -1,
17727 cu->language == language_cplus
17728 ? psymbol_placement::GLOBAL
17729 : psymbol_placement::STATIC,
17730 0, cu->language, objfile);
17731
17732 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17733 continue;
17734 }
17735
17736 struct partial_die_info *part_die
17737 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17738
17739 /* We'll save this DIE so link it in. */
17740 part_die->die_parent = parent_die;
17741 part_die->die_sibling = NULL;
17742 part_die->die_child = NULL;
17743
17744 if (last_die && last_die == parent_die)
17745 last_die->die_child = part_die;
17746 else if (last_die)
17747 last_die->die_sibling = part_die;
17748
17749 last_die = part_die;
17750
17751 if (first_die == NULL)
17752 first_die = part_die;
17753
17754 /* Maybe add the DIE to the hash table. Not all DIEs that we
17755 find interesting need to be in the hash table, because we
17756 also have the parent/sibling/child chains; only those that we
17757 might refer to by offset later during partial symbol reading.
17758
17759 For now this means things that might have be the target of a
17760 DW_AT_specification, DW_AT_abstract_origin, or
17761 DW_AT_extension. DW_AT_extension will refer only to
17762 namespaces; DW_AT_abstract_origin refers to functions (and
17763 many things under the function DIE, but we do not recurse
17764 into function DIEs during partial symbol reading) and
17765 possibly variables as well; DW_AT_specification refers to
17766 declarations. Declarations ought to have the DW_AT_declaration
17767 flag. It happens that GCC forgets to put it in sometimes, but
17768 only for functions, not for types.
17769
17770 Adding more things than necessary to the hash table is harmless
17771 except for the performance cost. Adding too few will result in
17772 wasted time in find_partial_die, when we reread the compilation
17773 unit with load_all_dies set. */
17774
17775 if (load_all
17776 || abbrev->tag == DW_TAG_constant
17777 || abbrev->tag == DW_TAG_subprogram
17778 || abbrev->tag == DW_TAG_variable
17779 || abbrev->tag == DW_TAG_namespace
17780 || part_die->is_declaration)
17781 {
17782 void **slot;
17783
17784 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17785 to_underlying (part_die->sect_off),
17786 INSERT);
17787 *slot = part_die;
17788 }
17789
17790 /* For some DIEs we want to follow their children (if any). For C
17791 we have no reason to follow the children of structures; for other
17792 languages we have to, so that we can get at method physnames
17793 to infer fully qualified class names, for DW_AT_specification,
17794 and for C++ template arguments. For C++, we also look one level
17795 inside functions to find template arguments (if the name of the
17796 function does not already contain the template arguments).
17797
17798 For Ada and Fortran, we need to scan the children of subprograms
17799 and lexical blocks as well because these languages allow the
17800 definition of nested entities that could be interesting for the
17801 debugger, such as nested subprograms for instance. */
17802 if (last_die->has_children
17803 && (load_all
17804 || last_die->tag == DW_TAG_namespace
17805 || last_die->tag == DW_TAG_module
17806 || last_die->tag == DW_TAG_enumeration_type
17807 || (cu->language == language_cplus
17808 && last_die->tag == DW_TAG_subprogram
17809 && (last_die->name == NULL
17810 || strchr (last_die->name, '<') == NULL))
17811 || (cu->language != language_c
17812 && (last_die->tag == DW_TAG_class_type
17813 || last_die->tag == DW_TAG_interface_type
17814 || last_die->tag == DW_TAG_structure_type
17815 || last_die->tag == DW_TAG_union_type))
17816 || ((cu->language == language_ada
17817 || cu->language == language_fortran)
17818 && (last_die->tag == DW_TAG_subprogram
17819 || last_die->tag == DW_TAG_lexical_block))))
17820 {
17821 nesting_level++;
17822 parent_die = last_die;
17823 continue;
17824 }
17825
17826 /* Otherwise we skip to the next sibling, if any. */
17827 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17828
17829 /* Back to the top, do it again. */
17830 }
17831 }
17832
17833 partial_die_info::partial_die_info (sect_offset sect_off_,
17834 struct abbrev_info *abbrev)
17835 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17836 {
17837 }
17838
17839 /* Read a minimal amount of information into the minimal die structure.
17840 INFO_PTR should point just after the initial uleb128 of a DIE. */
17841
17842 const gdb_byte *
17843 partial_die_info::read (const struct die_reader_specs *reader,
17844 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17845 {
17846 struct dwarf2_cu *cu = reader->cu;
17847 struct dwarf2_per_objfile *dwarf2_per_objfile
17848 = cu->per_cu->dwarf2_per_objfile;
17849 unsigned int i;
17850 int has_low_pc_attr = 0;
17851 int has_high_pc_attr = 0;
17852 int high_pc_relative = 0;
17853
17854 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17855 for (i = 0; i < abbrev.num_attrs; ++i)
17856 {
17857 bool need_reprocess;
17858 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17859 info_ptr, &need_reprocess);
17860 /* String and address offsets that need to do the reprocessing have
17861 already been read at this point, so there is no need to wait until
17862 the loop terminates to do the reprocessing. */
17863 if (need_reprocess)
17864 read_attribute_reprocess (reader, &attr_vec[i]);
17865 attribute &attr = attr_vec[i];
17866 /* Store the data if it is of an attribute we want to keep in a
17867 partial symbol table. */
17868 switch (attr.name)
17869 {
17870 case DW_AT_name:
17871 switch (tag)
17872 {
17873 case DW_TAG_compile_unit:
17874 case DW_TAG_partial_unit:
17875 case DW_TAG_type_unit:
17876 /* Compilation units have a DW_AT_name that is a filename, not
17877 a source language identifier. */
17878 case DW_TAG_enumeration_type:
17879 case DW_TAG_enumerator:
17880 /* These tags always have simple identifiers already; no need
17881 to canonicalize them. */
17882 name = DW_STRING (&attr);
17883 break;
17884 default:
17885 {
17886 struct objfile *objfile = dwarf2_per_objfile->objfile;
17887
17888 name
17889 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17890 }
17891 break;
17892 }
17893 break;
17894 case DW_AT_linkage_name:
17895 case DW_AT_MIPS_linkage_name:
17896 /* Note that both forms of linkage name might appear. We
17897 assume they will be the same, and we only store the last
17898 one we see. */
17899 linkage_name = DW_STRING (&attr);
17900 break;
17901 case DW_AT_low_pc:
17902 has_low_pc_attr = 1;
17903 lowpc = attr.value_as_address ();
17904 break;
17905 case DW_AT_high_pc:
17906 has_high_pc_attr = 1;
17907 highpc = attr.value_as_address ();
17908 if (cu->header.version >= 4 && attr.form_is_constant ())
17909 high_pc_relative = 1;
17910 break;
17911 case DW_AT_location:
17912 /* Support the .debug_loc offsets. */
17913 if (attr.form_is_block ())
17914 {
17915 d.locdesc = DW_BLOCK (&attr);
17916 }
17917 else if (attr.form_is_section_offset ())
17918 {
17919 dwarf2_complex_location_expr_complaint ();
17920 }
17921 else
17922 {
17923 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17924 "partial symbol information");
17925 }
17926 break;
17927 case DW_AT_external:
17928 is_external = DW_UNSND (&attr);
17929 break;
17930 case DW_AT_declaration:
17931 is_declaration = DW_UNSND (&attr);
17932 break;
17933 case DW_AT_type:
17934 has_type = 1;
17935 break;
17936 case DW_AT_abstract_origin:
17937 case DW_AT_specification:
17938 case DW_AT_extension:
17939 has_specification = 1;
17940 spec_offset = attr.get_ref_die_offset ();
17941 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17942 || cu->per_cu->is_dwz);
17943 break;
17944 case DW_AT_sibling:
17945 /* Ignore absolute siblings, they might point outside of
17946 the current compile unit. */
17947 if (attr.form == DW_FORM_ref_addr)
17948 complaint (_("ignoring absolute DW_AT_sibling"));
17949 else
17950 {
17951 const gdb_byte *buffer = reader->buffer;
17952 sect_offset off = attr.get_ref_die_offset ();
17953 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17954
17955 if (sibling_ptr < info_ptr)
17956 complaint (_("DW_AT_sibling points backwards"));
17957 else if (sibling_ptr > reader->buffer_end)
17958 reader->die_section->overflow_complaint ();
17959 else
17960 sibling = sibling_ptr;
17961 }
17962 break;
17963 case DW_AT_byte_size:
17964 has_byte_size = 1;
17965 break;
17966 case DW_AT_const_value:
17967 has_const_value = 1;
17968 break;
17969 case DW_AT_calling_convention:
17970 /* DWARF doesn't provide a way to identify a program's source-level
17971 entry point. DW_AT_calling_convention attributes are only meant
17972 to describe functions' calling conventions.
17973
17974 However, because it's a necessary piece of information in
17975 Fortran, and before DWARF 4 DW_CC_program was the only
17976 piece of debugging information whose definition refers to
17977 a 'main program' at all, several compilers marked Fortran
17978 main programs with DW_CC_program --- even when those
17979 functions use the standard calling conventions.
17980
17981 Although DWARF now specifies a way to provide this
17982 information, we support this practice for backward
17983 compatibility. */
17984 if (DW_UNSND (&attr) == DW_CC_program
17985 && cu->language == language_fortran)
17986 main_subprogram = 1;
17987 break;
17988 case DW_AT_inline:
17989 if (DW_UNSND (&attr) == DW_INL_inlined
17990 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17991 may_be_inlined = 1;
17992 break;
17993
17994 case DW_AT_import:
17995 if (tag == DW_TAG_imported_unit)
17996 {
17997 d.sect_off = attr.get_ref_die_offset ();
17998 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17999 || cu->per_cu->is_dwz);
18000 }
18001 break;
18002
18003 case DW_AT_main_subprogram:
18004 main_subprogram = DW_UNSND (&attr);
18005 break;
18006
18007 case DW_AT_ranges:
18008 {
18009 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18010 but that requires a full DIE, so instead we just
18011 reimplement it. */
18012 int need_ranges_base = tag != DW_TAG_compile_unit;
18013 unsigned int ranges_offset = (DW_UNSND (&attr)
18014 + (need_ranges_base
18015 ? cu->ranges_base
18016 : 0));
18017
18018 /* Value of the DW_AT_ranges attribute is the offset in the
18019 .debug_ranges section. */
18020 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18021 nullptr))
18022 has_pc_info = 1;
18023 }
18024 break;
18025
18026 default:
18027 break;
18028 }
18029 }
18030
18031 /* For Ada, if both the name and the linkage name appear, we prefer
18032 the latter. This lets "catch exception" work better, regardless
18033 of the order in which the name and linkage name were emitted.
18034 Really, though, this is just a workaround for the fact that gdb
18035 doesn't store both the name and the linkage name. */
18036 if (cu->language == language_ada && linkage_name != nullptr)
18037 name = linkage_name;
18038
18039 if (high_pc_relative)
18040 highpc += lowpc;
18041
18042 if (has_low_pc_attr && has_high_pc_attr)
18043 {
18044 /* When using the GNU linker, .gnu.linkonce. sections are used to
18045 eliminate duplicate copies of functions and vtables and such.
18046 The linker will arbitrarily choose one and discard the others.
18047 The AT_*_pc values for such functions refer to local labels in
18048 these sections. If the section from that file was discarded, the
18049 labels are not in the output, so the relocs get a value of 0.
18050 If this is a discarded function, mark the pc bounds as invalid,
18051 so that GDB will ignore it. */
18052 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18053 {
18054 struct objfile *objfile = dwarf2_per_objfile->objfile;
18055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18056
18057 complaint (_("DW_AT_low_pc %s is zero "
18058 "for DIE at %s [in module %s]"),
18059 paddress (gdbarch, lowpc),
18060 sect_offset_str (sect_off),
18061 objfile_name (objfile));
18062 }
18063 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18064 else if (lowpc >= highpc)
18065 {
18066 struct objfile *objfile = dwarf2_per_objfile->objfile;
18067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18068
18069 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18070 "for DIE at %s [in module %s]"),
18071 paddress (gdbarch, lowpc),
18072 paddress (gdbarch, highpc),
18073 sect_offset_str (sect_off),
18074 objfile_name (objfile));
18075 }
18076 else
18077 has_pc_info = 1;
18078 }
18079
18080 return info_ptr;
18081 }
18082
18083 /* Find a cached partial DIE at OFFSET in CU. */
18084
18085 struct partial_die_info *
18086 dwarf2_cu::find_partial_die (sect_offset sect_off)
18087 {
18088 struct partial_die_info *lookup_die = NULL;
18089 struct partial_die_info part_die (sect_off);
18090
18091 lookup_die = ((struct partial_die_info *)
18092 htab_find_with_hash (partial_dies, &part_die,
18093 to_underlying (sect_off)));
18094
18095 return lookup_die;
18096 }
18097
18098 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18099 except in the case of .debug_types DIEs which do not reference
18100 outside their CU (they do however referencing other types via
18101 DW_FORM_ref_sig8). */
18102
18103 static const struct cu_partial_die_info
18104 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18105 {
18106 struct dwarf2_per_objfile *dwarf2_per_objfile
18107 = cu->per_cu->dwarf2_per_objfile;
18108 struct objfile *objfile = dwarf2_per_objfile->objfile;
18109 struct dwarf2_per_cu_data *per_cu = NULL;
18110 struct partial_die_info *pd = NULL;
18111
18112 if (offset_in_dwz == cu->per_cu->is_dwz
18113 && cu->header.offset_in_cu_p (sect_off))
18114 {
18115 pd = cu->find_partial_die (sect_off);
18116 if (pd != NULL)
18117 return { cu, pd };
18118 /* We missed recording what we needed.
18119 Load all dies and try again. */
18120 per_cu = cu->per_cu;
18121 }
18122 else
18123 {
18124 /* TUs don't reference other CUs/TUs (except via type signatures). */
18125 if (cu->per_cu->is_debug_types)
18126 {
18127 error (_("Dwarf Error: Type Unit at offset %s contains"
18128 " external reference to offset %s [in module %s].\n"),
18129 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18130 bfd_get_filename (objfile->obfd));
18131 }
18132 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18133 dwarf2_per_objfile);
18134
18135 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18136 load_partial_comp_unit (per_cu);
18137
18138 per_cu->cu->last_used = 0;
18139 pd = per_cu->cu->find_partial_die (sect_off);
18140 }
18141
18142 /* If we didn't find it, and not all dies have been loaded,
18143 load them all and try again. */
18144
18145 if (pd == NULL && per_cu->load_all_dies == 0)
18146 {
18147 per_cu->load_all_dies = 1;
18148
18149 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18150 THIS_CU->cu may already be in use. So we can't just free it and
18151 replace its DIEs with the ones we read in. Instead, we leave those
18152 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18153 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18154 set. */
18155 load_partial_comp_unit (per_cu);
18156
18157 pd = per_cu->cu->find_partial_die (sect_off);
18158 }
18159
18160 if (pd == NULL)
18161 internal_error (__FILE__, __LINE__,
18162 _("could not find partial DIE %s "
18163 "in cache [from module %s]\n"),
18164 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18165 return { per_cu->cu, pd };
18166 }
18167
18168 /* See if we can figure out if the class lives in a namespace. We do
18169 this by looking for a member function; its demangled name will
18170 contain namespace info, if there is any. */
18171
18172 static void
18173 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18174 struct dwarf2_cu *cu)
18175 {
18176 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18177 what template types look like, because the demangler
18178 frequently doesn't give the same name as the debug info. We
18179 could fix this by only using the demangled name to get the
18180 prefix (but see comment in read_structure_type). */
18181
18182 struct partial_die_info *real_pdi;
18183 struct partial_die_info *child_pdi;
18184
18185 /* If this DIE (this DIE's specification, if any) has a parent, then
18186 we should not do this. We'll prepend the parent's fully qualified
18187 name when we create the partial symbol. */
18188
18189 real_pdi = struct_pdi;
18190 while (real_pdi->has_specification)
18191 {
18192 auto res = find_partial_die (real_pdi->spec_offset,
18193 real_pdi->spec_is_dwz, cu);
18194 real_pdi = res.pdi;
18195 cu = res.cu;
18196 }
18197
18198 if (real_pdi->die_parent != NULL)
18199 return;
18200
18201 for (child_pdi = struct_pdi->die_child;
18202 child_pdi != NULL;
18203 child_pdi = child_pdi->die_sibling)
18204 {
18205 if (child_pdi->tag == DW_TAG_subprogram
18206 && child_pdi->linkage_name != NULL)
18207 {
18208 gdb::unique_xmalloc_ptr<char> actual_class_name
18209 (language_class_name_from_physname (cu->language_defn,
18210 child_pdi->linkage_name));
18211 if (actual_class_name != NULL)
18212 {
18213 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18214 struct_pdi->name = objfile->intern (actual_class_name.get ());
18215 }
18216 break;
18217 }
18218 }
18219 }
18220
18221 void
18222 partial_die_info::fixup (struct dwarf2_cu *cu)
18223 {
18224 /* Once we've fixed up a die, there's no point in doing so again.
18225 This also avoids a memory leak if we were to call
18226 guess_partial_die_structure_name multiple times. */
18227 if (fixup_called)
18228 return;
18229
18230 /* If we found a reference attribute and the DIE has no name, try
18231 to find a name in the referred to DIE. */
18232
18233 if (name == NULL && has_specification)
18234 {
18235 struct partial_die_info *spec_die;
18236
18237 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18238 spec_die = res.pdi;
18239 cu = res.cu;
18240
18241 spec_die->fixup (cu);
18242
18243 if (spec_die->name)
18244 {
18245 name = spec_die->name;
18246
18247 /* Copy DW_AT_external attribute if it is set. */
18248 if (spec_die->is_external)
18249 is_external = spec_die->is_external;
18250 }
18251 }
18252
18253 /* Set default names for some unnamed DIEs. */
18254
18255 if (name == NULL && tag == DW_TAG_namespace)
18256 name = CP_ANONYMOUS_NAMESPACE_STR;
18257
18258 /* If there is no parent die to provide a namespace, and there are
18259 children, see if we can determine the namespace from their linkage
18260 name. */
18261 if (cu->language == language_cplus
18262 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18263 && die_parent == NULL
18264 && has_children
18265 && (tag == DW_TAG_class_type
18266 || tag == DW_TAG_structure_type
18267 || tag == DW_TAG_union_type))
18268 guess_partial_die_structure_name (this, cu);
18269
18270 /* GCC might emit a nameless struct or union that has a linkage
18271 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18272 if (name == NULL
18273 && (tag == DW_TAG_class_type
18274 || tag == DW_TAG_interface_type
18275 || tag == DW_TAG_structure_type
18276 || tag == DW_TAG_union_type)
18277 && linkage_name != NULL)
18278 {
18279 gdb::unique_xmalloc_ptr<char> demangled
18280 (gdb_demangle (linkage_name, DMGL_TYPES));
18281 if (demangled != nullptr)
18282 {
18283 const char *base;
18284
18285 /* Strip any leading namespaces/classes, keep only the base name.
18286 DW_AT_name for named DIEs does not contain the prefixes. */
18287 base = strrchr (demangled.get (), ':');
18288 if (base && base > demangled.get () && base[-1] == ':')
18289 base++;
18290 else
18291 base = demangled.get ();
18292
18293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18294 name = objfile->intern (base);
18295 }
18296 }
18297
18298 fixup_called = 1;
18299 }
18300
18301 /* Process the attributes that had to be skipped in the first round. These
18302 attributes are the ones that need str_offsets_base or addr_base attributes.
18303 They could not have been processed in the first round, because at the time
18304 the values of str_offsets_base or addr_base may not have been known. */
18305 static void
18306 read_attribute_reprocess (const struct die_reader_specs *reader,
18307 struct attribute *attr)
18308 {
18309 struct dwarf2_cu *cu = reader->cu;
18310 switch (attr->form)
18311 {
18312 case DW_FORM_addrx:
18313 case DW_FORM_GNU_addr_index:
18314 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18315 break;
18316 case DW_FORM_strx:
18317 case DW_FORM_strx1:
18318 case DW_FORM_strx2:
18319 case DW_FORM_strx3:
18320 case DW_FORM_strx4:
18321 case DW_FORM_GNU_str_index:
18322 {
18323 unsigned int str_index = DW_UNSND (attr);
18324 if (reader->dwo_file != NULL)
18325 {
18326 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18327 DW_STRING_IS_CANONICAL (attr) = 0;
18328 }
18329 else
18330 {
18331 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18332 DW_STRING_IS_CANONICAL (attr) = 0;
18333 }
18334 break;
18335 }
18336 default:
18337 gdb_assert_not_reached (_("Unexpected DWARF form."));
18338 }
18339 }
18340
18341 /* Read an attribute value described by an attribute form. */
18342
18343 static const gdb_byte *
18344 read_attribute_value (const struct die_reader_specs *reader,
18345 struct attribute *attr, unsigned form,
18346 LONGEST implicit_const, const gdb_byte *info_ptr,
18347 bool *need_reprocess)
18348 {
18349 struct dwarf2_cu *cu = reader->cu;
18350 struct dwarf2_per_objfile *dwarf2_per_objfile
18351 = cu->per_cu->dwarf2_per_objfile;
18352 struct objfile *objfile = dwarf2_per_objfile->objfile;
18353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18354 bfd *abfd = reader->abfd;
18355 struct comp_unit_head *cu_header = &cu->header;
18356 unsigned int bytes_read;
18357 struct dwarf_block *blk;
18358 *need_reprocess = false;
18359
18360 attr->form = (enum dwarf_form) form;
18361 switch (form)
18362 {
18363 case DW_FORM_ref_addr:
18364 if (cu->header.version == 2)
18365 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18366 &bytes_read);
18367 else
18368 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18369 &bytes_read);
18370 info_ptr += bytes_read;
18371 break;
18372 case DW_FORM_GNU_ref_alt:
18373 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18374 info_ptr += bytes_read;
18375 break;
18376 case DW_FORM_addr:
18377 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18378 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18379 info_ptr += bytes_read;
18380 break;
18381 case DW_FORM_block2:
18382 blk = dwarf_alloc_block (cu);
18383 blk->size = read_2_bytes (abfd, info_ptr);
18384 info_ptr += 2;
18385 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18386 info_ptr += blk->size;
18387 DW_BLOCK (attr) = blk;
18388 break;
18389 case DW_FORM_block4:
18390 blk = dwarf_alloc_block (cu);
18391 blk->size = read_4_bytes (abfd, info_ptr);
18392 info_ptr += 4;
18393 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18394 info_ptr += blk->size;
18395 DW_BLOCK (attr) = blk;
18396 break;
18397 case DW_FORM_data2:
18398 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18399 info_ptr += 2;
18400 break;
18401 case DW_FORM_data4:
18402 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18403 info_ptr += 4;
18404 break;
18405 case DW_FORM_data8:
18406 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18407 info_ptr += 8;
18408 break;
18409 case DW_FORM_data16:
18410 blk = dwarf_alloc_block (cu);
18411 blk->size = 16;
18412 blk->data = read_n_bytes (abfd, info_ptr, 16);
18413 info_ptr += 16;
18414 DW_BLOCK (attr) = blk;
18415 break;
18416 case DW_FORM_sec_offset:
18417 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18418 info_ptr += bytes_read;
18419 break;
18420 case DW_FORM_string:
18421 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18422 DW_STRING_IS_CANONICAL (attr) = 0;
18423 info_ptr += bytes_read;
18424 break;
18425 case DW_FORM_strp:
18426 if (!cu->per_cu->is_dwz)
18427 {
18428 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18429 abfd, info_ptr, cu_header,
18430 &bytes_read);
18431 DW_STRING_IS_CANONICAL (attr) = 0;
18432 info_ptr += bytes_read;
18433 break;
18434 }
18435 /* FALLTHROUGH */
18436 case DW_FORM_line_strp:
18437 if (!cu->per_cu->is_dwz)
18438 {
18439 DW_STRING (attr)
18440 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18441 &bytes_read);
18442 DW_STRING_IS_CANONICAL (attr) = 0;
18443 info_ptr += bytes_read;
18444 break;
18445 }
18446 /* FALLTHROUGH */
18447 case DW_FORM_GNU_strp_alt:
18448 {
18449 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18450 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18451 &bytes_read);
18452
18453 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18454 DW_STRING_IS_CANONICAL (attr) = 0;
18455 info_ptr += bytes_read;
18456 }
18457 break;
18458 case DW_FORM_exprloc:
18459 case DW_FORM_block:
18460 blk = dwarf_alloc_block (cu);
18461 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18462 info_ptr += bytes_read;
18463 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18464 info_ptr += blk->size;
18465 DW_BLOCK (attr) = blk;
18466 break;
18467 case DW_FORM_block1:
18468 blk = dwarf_alloc_block (cu);
18469 blk->size = read_1_byte (abfd, info_ptr);
18470 info_ptr += 1;
18471 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18472 info_ptr += blk->size;
18473 DW_BLOCK (attr) = blk;
18474 break;
18475 case DW_FORM_data1:
18476 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18477 info_ptr += 1;
18478 break;
18479 case DW_FORM_flag:
18480 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18481 info_ptr += 1;
18482 break;
18483 case DW_FORM_flag_present:
18484 DW_UNSND (attr) = 1;
18485 break;
18486 case DW_FORM_sdata:
18487 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18488 info_ptr += bytes_read;
18489 break;
18490 case DW_FORM_udata:
18491 case DW_FORM_rnglistx:
18492 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18493 info_ptr += bytes_read;
18494 break;
18495 case DW_FORM_ref1:
18496 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18497 + read_1_byte (abfd, info_ptr));
18498 info_ptr += 1;
18499 break;
18500 case DW_FORM_ref2:
18501 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18502 + read_2_bytes (abfd, info_ptr));
18503 info_ptr += 2;
18504 break;
18505 case DW_FORM_ref4:
18506 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18507 + read_4_bytes (abfd, info_ptr));
18508 info_ptr += 4;
18509 break;
18510 case DW_FORM_ref8:
18511 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18512 + read_8_bytes (abfd, info_ptr));
18513 info_ptr += 8;
18514 break;
18515 case DW_FORM_ref_sig8:
18516 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18517 info_ptr += 8;
18518 break;
18519 case DW_FORM_ref_udata:
18520 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18521 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18522 info_ptr += bytes_read;
18523 break;
18524 case DW_FORM_indirect:
18525 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18526 info_ptr += bytes_read;
18527 if (form == DW_FORM_implicit_const)
18528 {
18529 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18530 info_ptr += bytes_read;
18531 }
18532 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18533 info_ptr, need_reprocess);
18534 break;
18535 case DW_FORM_implicit_const:
18536 DW_SND (attr) = implicit_const;
18537 break;
18538 case DW_FORM_addrx:
18539 case DW_FORM_GNU_addr_index:
18540 *need_reprocess = true;
18541 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18542 info_ptr += bytes_read;
18543 break;
18544 case DW_FORM_strx:
18545 case DW_FORM_strx1:
18546 case DW_FORM_strx2:
18547 case DW_FORM_strx3:
18548 case DW_FORM_strx4:
18549 case DW_FORM_GNU_str_index:
18550 {
18551 ULONGEST str_index;
18552 if (form == DW_FORM_strx1)
18553 {
18554 str_index = read_1_byte (abfd, info_ptr);
18555 info_ptr += 1;
18556 }
18557 else if (form == DW_FORM_strx2)
18558 {
18559 str_index = read_2_bytes (abfd, info_ptr);
18560 info_ptr += 2;
18561 }
18562 else if (form == DW_FORM_strx3)
18563 {
18564 str_index = read_3_bytes (abfd, info_ptr);
18565 info_ptr += 3;
18566 }
18567 else if (form == DW_FORM_strx4)
18568 {
18569 str_index = read_4_bytes (abfd, info_ptr);
18570 info_ptr += 4;
18571 }
18572 else
18573 {
18574 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18575 info_ptr += bytes_read;
18576 }
18577 *need_reprocess = true;
18578 DW_UNSND (attr) = str_index;
18579 }
18580 break;
18581 default:
18582 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18583 dwarf_form_name (form),
18584 bfd_get_filename (abfd));
18585 }
18586
18587 /* Super hack. */
18588 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18589 attr->form = DW_FORM_GNU_ref_alt;
18590
18591 /* We have seen instances where the compiler tried to emit a byte
18592 size attribute of -1 which ended up being encoded as an unsigned
18593 0xffffffff. Although 0xffffffff is technically a valid size value,
18594 an object of this size seems pretty unlikely so we can relatively
18595 safely treat these cases as if the size attribute was invalid and
18596 treat them as zero by default. */
18597 if (attr->name == DW_AT_byte_size
18598 && form == DW_FORM_data4
18599 && DW_UNSND (attr) >= 0xffffffff)
18600 {
18601 complaint
18602 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18603 hex_string (DW_UNSND (attr)));
18604 DW_UNSND (attr) = 0;
18605 }
18606
18607 return info_ptr;
18608 }
18609
18610 /* Read an attribute described by an abbreviated attribute. */
18611
18612 static const gdb_byte *
18613 read_attribute (const struct die_reader_specs *reader,
18614 struct attribute *attr, struct attr_abbrev *abbrev,
18615 const gdb_byte *info_ptr, bool *need_reprocess)
18616 {
18617 attr->name = abbrev->name;
18618 return read_attribute_value (reader, attr, abbrev->form,
18619 abbrev->implicit_const, info_ptr,
18620 need_reprocess);
18621 }
18622
18623 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18624
18625 static const char *
18626 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18627 LONGEST str_offset)
18628 {
18629 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18630 str_offset, "DW_FORM_strp");
18631 }
18632
18633 /* Return pointer to string at .debug_str offset as read from BUF.
18634 BUF is assumed to be in a compilation unit described by CU_HEADER.
18635 Return *BYTES_READ_PTR count of bytes read from BUF. */
18636
18637 static const char *
18638 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18639 const gdb_byte *buf,
18640 const struct comp_unit_head *cu_header,
18641 unsigned int *bytes_read_ptr)
18642 {
18643 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18644
18645 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18646 }
18647
18648 /* See read.h. */
18649
18650 const char *
18651 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18652 const struct comp_unit_head *cu_header,
18653 unsigned int *bytes_read_ptr)
18654 {
18655 bfd *abfd = objfile->obfd;
18656 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18657
18658 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18659 }
18660
18661 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18662 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18663 ADDR_SIZE is the size of addresses from the CU header. */
18664
18665 static CORE_ADDR
18666 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18667 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18668 int addr_size)
18669 {
18670 struct objfile *objfile = dwarf2_per_objfile->objfile;
18671 bfd *abfd = objfile->obfd;
18672 const gdb_byte *info_ptr;
18673 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18674
18675 dwarf2_per_objfile->addr.read (objfile);
18676 if (dwarf2_per_objfile->addr.buffer == NULL)
18677 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18678 objfile_name (objfile));
18679 if (addr_base_or_zero + addr_index * addr_size
18680 >= dwarf2_per_objfile->addr.size)
18681 error (_("DW_FORM_addr_index pointing outside of "
18682 ".debug_addr section [in module %s]"),
18683 objfile_name (objfile));
18684 info_ptr = (dwarf2_per_objfile->addr.buffer
18685 + addr_base_or_zero + addr_index * addr_size);
18686 if (addr_size == 4)
18687 return bfd_get_32 (abfd, info_ptr);
18688 else
18689 return bfd_get_64 (abfd, info_ptr);
18690 }
18691
18692 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18693
18694 static CORE_ADDR
18695 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18696 {
18697 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18698 cu->addr_base, cu->header.addr_size);
18699 }
18700
18701 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18702
18703 static CORE_ADDR
18704 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18705 unsigned int *bytes_read)
18706 {
18707 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18708 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18709
18710 return read_addr_index (cu, addr_index);
18711 }
18712
18713 /* See read.h. */
18714
18715 CORE_ADDR
18716 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18717 {
18718 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18719 struct dwarf2_cu *cu = per_cu->cu;
18720 gdb::optional<ULONGEST> addr_base;
18721 int addr_size;
18722
18723 /* We need addr_base and addr_size.
18724 If we don't have PER_CU->cu, we have to get it.
18725 Nasty, but the alternative is storing the needed info in PER_CU,
18726 which at this point doesn't seem justified: it's not clear how frequently
18727 it would get used and it would increase the size of every PER_CU.
18728 Entry points like dwarf2_per_cu_addr_size do a similar thing
18729 so we're not in uncharted territory here.
18730 Alas we need to be a bit more complicated as addr_base is contained
18731 in the DIE.
18732
18733 We don't need to read the entire CU(/TU).
18734 We just need the header and top level die.
18735
18736 IWBN to use the aging mechanism to let us lazily later discard the CU.
18737 For now we skip this optimization. */
18738
18739 if (cu != NULL)
18740 {
18741 addr_base = cu->addr_base;
18742 addr_size = cu->header.addr_size;
18743 }
18744 else
18745 {
18746 cutu_reader reader (per_cu, NULL, 0, false);
18747 addr_base = reader.cu->addr_base;
18748 addr_size = reader.cu->header.addr_size;
18749 }
18750
18751 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18752 addr_size);
18753 }
18754
18755 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18756 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18757 DWO file. */
18758
18759 static const char *
18760 read_str_index (struct dwarf2_cu *cu,
18761 struct dwarf2_section_info *str_section,
18762 struct dwarf2_section_info *str_offsets_section,
18763 ULONGEST str_offsets_base, ULONGEST str_index)
18764 {
18765 struct dwarf2_per_objfile *dwarf2_per_objfile
18766 = cu->per_cu->dwarf2_per_objfile;
18767 struct objfile *objfile = dwarf2_per_objfile->objfile;
18768 const char *objf_name = objfile_name (objfile);
18769 bfd *abfd = objfile->obfd;
18770 const gdb_byte *info_ptr;
18771 ULONGEST str_offset;
18772 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18773
18774 str_section->read (objfile);
18775 str_offsets_section->read (objfile);
18776 if (str_section->buffer == NULL)
18777 error (_("%s used without %s section"
18778 " in CU at offset %s [in module %s]"),
18779 form_name, str_section->get_name (),
18780 sect_offset_str (cu->header.sect_off), objf_name);
18781 if (str_offsets_section->buffer == NULL)
18782 error (_("%s used without %s section"
18783 " in CU at offset %s [in module %s]"),
18784 form_name, str_section->get_name (),
18785 sect_offset_str (cu->header.sect_off), objf_name);
18786 info_ptr = (str_offsets_section->buffer
18787 + str_offsets_base
18788 + str_index * cu->header.offset_size);
18789 if (cu->header.offset_size == 4)
18790 str_offset = bfd_get_32 (abfd, info_ptr);
18791 else
18792 str_offset = bfd_get_64 (abfd, info_ptr);
18793 if (str_offset >= str_section->size)
18794 error (_("Offset from %s pointing outside of"
18795 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18796 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18797 return (const char *) (str_section->buffer + str_offset);
18798 }
18799
18800 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18801
18802 static const char *
18803 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18804 {
18805 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18806 ? reader->cu->header.addr_size : 0;
18807 return read_str_index (reader->cu,
18808 &reader->dwo_file->sections.str,
18809 &reader->dwo_file->sections.str_offsets,
18810 str_offsets_base, str_index);
18811 }
18812
18813 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18814
18815 static const char *
18816 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18817 {
18818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18819 const char *objf_name = objfile_name (objfile);
18820 static const char form_name[] = "DW_FORM_GNU_str_index";
18821 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18822
18823 if (!cu->str_offsets_base.has_value ())
18824 error (_("%s used in Fission stub without %s"
18825 " in CU at offset 0x%lx [in module %s]"),
18826 form_name, str_offsets_attr_name,
18827 (long) cu->header.offset_size, objf_name);
18828
18829 return read_str_index (cu,
18830 &cu->per_cu->dwarf2_per_objfile->str,
18831 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18832 *cu->str_offsets_base, str_index);
18833 }
18834
18835 /* Return the length of an LEB128 number in BUF. */
18836
18837 static int
18838 leb128_size (const gdb_byte *buf)
18839 {
18840 const gdb_byte *begin = buf;
18841 gdb_byte byte;
18842
18843 while (1)
18844 {
18845 byte = *buf++;
18846 if ((byte & 128) == 0)
18847 return buf - begin;
18848 }
18849 }
18850
18851 static void
18852 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18853 {
18854 switch (lang)
18855 {
18856 case DW_LANG_C89:
18857 case DW_LANG_C99:
18858 case DW_LANG_C11:
18859 case DW_LANG_C:
18860 case DW_LANG_UPC:
18861 cu->language = language_c;
18862 break;
18863 case DW_LANG_Java:
18864 case DW_LANG_C_plus_plus:
18865 case DW_LANG_C_plus_plus_11:
18866 case DW_LANG_C_plus_plus_14:
18867 cu->language = language_cplus;
18868 break;
18869 case DW_LANG_D:
18870 cu->language = language_d;
18871 break;
18872 case DW_LANG_Fortran77:
18873 case DW_LANG_Fortran90:
18874 case DW_LANG_Fortran95:
18875 case DW_LANG_Fortran03:
18876 case DW_LANG_Fortran08:
18877 cu->language = language_fortran;
18878 break;
18879 case DW_LANG_Go:
18880 cu->language = language_go;
18881 break;
18882 case DW_LANG_Mips_Assembler:
18883 cu->language = language_asm;
18884 break;
18885 case DW_LANG_Ada83:
18886 case DW_LANG_Ada95:
18887 cu->language = language_ada;
18888 break;
18889 case DW_LANG_Modula2:
18890 cu->language = language_m2;
18891 break;
18892 case DW_LANG_Pascal83:
18893 cu->language = language_pascal;
18894 break;
18895 case DW_LANG_ObjC:
18896 cu->language = language_objc;
18897 break;
18898 case DW_LANG_Rust:
18899 case DW_LANG_Rust_old:
18900 cu->language = language_rust;
18901 break;
18902 case DW_LANG_Cobol74:
18903 case DW_LANG_Cobol85:
18904 default:
18905 cu->language = language_minimal;
18906 break;
18907 }
18908 cu->language_defn = language_def (cu->language);
18909 }
18910
18911 /* Return the named attribute or NULL if not there. */
18912
18913 static struct attribute *
18914 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18915 {
18916 for (;;)
18917 {
18918 unsigned int i;
18919 struct attribute *spec = NULL;
18920
18921 for (i = 0; i < die->num_attrs; ++i)
18922 {
18923 if (die->attrs[i].name == name)
18924 return &die->attrs[i];
18925 if (die->attrs[i].name == DW_AT_specification
18926 || die->attrs[i].name == DW_AT_abstract_origin)
18927 spec = &die->attrs[i];
18928 }
18929
18930 if (!spec)
18931 break;
18932
18933 die = follow_die_ref (die, spec, &cu);
18934 }
18935
18936 return NULL;
18937 }
18938
18939 /* Return the string associated with a string-typed attribute, or NULL if it
18940 is either not found or is of an incorrect type. */
18941
18942 static const char *
18943 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18944 {
18945 struct attribute *attr;
18946 const char *str = NULL;
18947
18948 attr = dwarf2_attr (die, name, cu);
18949
18950 if (attr != NULL)
18951 {
18952 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18953 || attr->form == DW_FORM_string
18954 || attr->form == DW_FORM_strx
18955 || attr->form == DW_FORM_strx1
18956 || attr->form == DW_FORM_strx2
18957 || attr->form == DW_FORM_strx3
18958 || attr->form == DW_FORM_strx4
18959 || attr->form == DW_FORM_GNU_str_index
18960 || attr->form == DW_FORM_GNU_strp_alt)
18961 str = DW_STRING (attr);
18962 else
18963 complaint (_("string type expected for attribute %s for "
18964 "DIE at %s in module %s"),
18965 dwarf_attr_name (name), sect_offset_str (die->sect_off),
18966 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18967 }
18968
18969 return str;
18970 }
18971
18972 /* Return the dwo name or NULL if not present. If present, it is in either
18973 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
18974 static const char *
18975 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
18976 {
18977 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
18978 if (dwo_name == nullptr)
18979 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
18980 return dwo_name;
18981 }
18982
18983 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18984 and holds a non-zero value. This function should only be used for
18985 DW_FORM_flag or DW_FORM_flag_present attributes. */
18986
18987 static int
18988 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18989 {
18990 struct attribute *attr = dwarf2_attr (die, name, cu);
18991
18992 return (attr && DW_UNSND (attr));
18993 }
18994
18995 static int
18996 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18997 {
18998 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18999 which value is non-zero. However, we have to be careful with
19000 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19001 (via dwarf2_flag_true_p) follows this attribute. So we may
19002 end up accidently finding a declaration attribute that belongs
19003 to a different DIE referenced by the specification attribute,
19004 even though the given DIE does not have a declaration attribute. */
19005 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19006 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19007 }
19008
19009 /* Return the die giving the specification for DIE, if there is
19010 one. *SPEC_CU is the CU containing DIE on input, and the CU
19011 containing the return value on output. If there is no
19012 specification, but there is an abstract origin, that is
19013 returned. */
19014
19015 static struct die_info *
19016 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19017 {
19018 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19019 *spec_cu);
19020
19021 if (spec_attr == NULL)
19022 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19023
19024 if (spec_attr == NULL)
19025 return NULL;
19026 else
19027 return follow_die_ref (die, spec_attr, spec_cu);
19028 }
19029
19030 /* Stub for free_line_header to match void * callback types. */
19031
19032 static void
19033 free_line_header_voidp (void *arg)
19034 {
19035 struct line_header *lh = (struct line_header *) arg;
19036
19037 delete lh;
19038 }
19039
19040 /* A convenience function to find the proper .debug_line section for a CU. */
19041
19042 static struct dwarf2_section_info *
19043 get_debug_line_section (struct dwarf2_cu *cu)
19044 {
19045 struct dwarf2_section_info *section;
19046 struct dwarf2_per_objfile *dwarf2_per_objfile
19047 = cu->per_cu->dwarf2_per_objfile;
19048
19049 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19050 DWO file. */
19051 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19052 section = &cu->dwo_unit->dwo_file->sections.line;
19053 else if (cu->per_cu->is_dwz)
19054 {
19055 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19056
19057 section = &dwz->line;
19058 }
19059 else
19060 section = &dwarf2_per_objfile->line;
19061
19062 return section;
19063 }
19064
19065 /* Read the statement program header starting at OFFSET in
19066 .debug_line, or .debug_line.dwo. Return a pointer
19067 to a struct line_header, allocated using xmalloc.
19068 Returns NULL if there is a problem reading the header, e.g., if it
19069 has a version we don't understand.
19070
19071 NOTE: the strings in the include directory and file name tables of
19072 the returned object point into the dwarf line section buffer,
19073 and must not be freed. */
19074
19075 static line_header_up
19076 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19077 {
19078 struct dwarf2_section_info *section;
19079 struct dwarf2_per_objfile *dwarf2_per_objfile
19080 = cu->per_cu->dwarf2_per_objfile;
19081
19082 section = get_debug_line_section (cu);
19083 section->read (dwarf2_per_objfile->objfile);
19084 if (section->buffer == NULL)
19085 {
19086 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19087 complaint (_("missing .debug_line.dwo section"));
19088 else
19089 complaint (_("missing .debug_line section"));
19090 return 0;
19091 }
19092
19093 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19094 dwarf2_per_objfile, section,
19095 &cu->header);
19096 }
19097
19098 /* Subroutine of dwarf_decode_lines to simplify it.
19099 Return the file name of the psymtab for the given file_entry.
19100 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19101 If space for the result is malloc'd, *NAME_HOLDER will be set.
19102 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19103
19104 static const char *
19105 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19106 const dwarf2_psymtab *pst,
19107 const char *comp_dir,
19108 gdb::unique_xmalloc_ptr<char> *name_holder)
19109 {
19110 const char *include_name = fe.name;
19111 const char *include_name_to_compare = include_name;
19112 const char *pst_filename;
19113 int file_is_pst;
19114
19115 const char *dir_name = fe.include_dir (lh);
19116
19117 gdb::unique_xmalloc_ptr<char> hold_compare;
19118 if (!IS_ABSOLUTE_PATH (include_name)
19119 && (dir_name != NULL || comp_dir != NULL))
19120 {
19121 /* Avoid creating a duplicate psymtab for PST.
19122 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19123 Before we do the comparison, however, we need to account
19124 for DIR_NAME and COMP_DIR.
19125 First prepend dir_name (if non-NULL). If we still don't
19126 have an absolute path prepend comp_dir (if non-NULL).
19127 However, the directory we record in the include-file's
19128 psymtab does not contain COMP_DIR (to match the
19129 corresponding symtab(s)).
19130
19131 Example:
19132
19133 bash$ cd /tmp
19134 bash$ gcc -g ./hello.c
19135 include_name = "hello.c"
19136 dir_name = "."
19137 DW_AT_comp_dir = comp_dir = "/tmp"
19138 DW_AT_name = "./hello.c"
19139
19140 */
19141
19142 if (dir_name != NULL)
19143 {
19144 name_holder->reset (concat (dir_name, SLASH_STRING,
19145 include_name, (char *) NULL));
19146 include_name = name_holder->get ();
19147 include_name_to_compare = include_name;
19148 }
19149 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19150 {
19151 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19152 include_name, (char *) NULL));
19153 include_name_to_compare = hold_compare.get ();
19154 }
19155 }
19156
19157 pst_filename = pst->filename;
19158 gdb::unique_xmalloc_ptr<char> copied_name;
19159 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19160 {
19161 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19162 pst_filename, (char *) NULL));
19163 pst_filename = copied_name.get ();
19164 }
19165
19166 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19167
19168 if (file_is_pst)
19169 return NULL;
19170 return include_name;
19171 }
19172
19173 /* State machine to track the state of the line number program. */
19174
19175 class lnp_state_machine
19176 {
19177 public:
19178 /* Initialize a machine state for the start of a line number
19179 program. */
19180 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19181 bool record_lines_p);
19182
19183 file_entry *current_file ()
19184 {
19185 /* lh->file_names is 0-based, but the file name numbers in the
19186 statement program are 1-based. */
19187 return m_line_header->file_name_at (m_file);
19188 }
19189
19190 /* Record the line in the state machine. END_SEQUENCE is true if
19191 we're processing the end of a sequence. */
19192 void record_line (bool end_sequence);
19193
19194 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19195 nop-out rest of the lines in this sequence. */
19196 void check_line_address (struct dwarf2_cu *cu,
19197 const gdb_byte *line_ptr,
19198 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19199
19200 void handle_set_discriminator (unsigned int discriminator)
19201 {
19202 m_discriminator = discriminator;
19203 m_line_has_non_zero_discriminator |= discriminator != 0;
19204 }
19205
19206 /* Handle DW_LNE_set_address. */
19207 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19208 {
19209 m_op_index = 0;
19210 address += baseaddr;
19211 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19212 }
19213
19214 /* Handle DW_LNS_advance_pc. */
19215 void handle_advance_pc (CORE_ADDR adjust);
19216
19217 /* Handle a special opcode. */
19218 void handle_special_opcode (unsigned char op_code);
19219
19220 /* Handle DW_LNS_advance_line. */
19221 void handle_advance_line (int line_delta)
19222 {
19223 advance_line (line_delta);
19224 }
19225
19226 /* Handle DW_LNS_set_file. */
19227 void handle_set_file (file_name_index file);
19228
19229 /* Handle DW_LNS_negate_stmt. */
19230 void handle_negate_stmt ()
19231 {
19232 m_is_stmt = !m_is_stmt;
19233 }
19234
19235 /* Handle DW_LNS_const_add_pc. */
19236 void handle_const_add_pc ();
19237
19238 /* Handle DW_LNS_fixed_advance_pc. */
19239 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19240 {
19241 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19242 m_op_index = 0;
19243 }
19244
19245 /* Handle DW_LNS_copy. */
19246 void handle_copy ()
19247 {
19248 record_line (false);
19249 m_discriminator = 0;
19250 }
19251
19252 /* Handle DW_LNE_end_sequence. */
19253 void handle_end_sequence ()
19254 {
19255 m_currently_recording_lines = true;
19256 }
19257
19258 private:
19259 /* Advance the line by LINE_DELTA. */
19260 void advance_line (int line_delta)
19261 {
19262 m_line += line_delta;
19263
19264 if (line_delta != 0)
19265 m_line_has_non_zero_discriminator = m_discriminator != 0;
19266 }
19267
19268 struct dwarf2_cu *m_cu;
19269
19270 gdbarch *m_gdbarch;
19271
19272 /* True if we're recording lines.
19273 Otherwise we're building partial symtabs and are just interested in
19274 finding include files mentioned by the line number program. */
19275 bool m_record_lines_p;
19276
19277 /* The line number header. */
19278 line_header *m_line_header;
19279
19280 /* These are part of the standard DWARF line number state machine,
19281 and initialized according to the DWARF spec. */
19282
19283 unsigned char m_op_index = 0;
19284 /* The line table index of the current file. */
19285 file_name_index m_file = 1;
19286 unsigned int m_line = 1;
19287
19288 /* These are initialized in the constructor. */
19289
19290 CORE_ADDR m_address;
19291 bool m_is_stmt;
19292 unsigned int m_discriminator;
19293
19294 /* Additional bits of state we need to track. */
19295
19296 /* The last file that we called dwarf2_start_subfile for.
19297 This is only used for TLLs. */
19298 unsigned int m_last_file = 0;
19299 /* The last file a line number was recorded for. */
19300 struct subfile *m_last_subfile = NULL;
19301
19302 /* When true, record the lines we decode. */
19303 bool m_currently_recording_lines = false;
19304
19305 /* The last line number that was recorded, used to coalesce
19306 consecutive entries for the same line. This can happen, for
19307 example, when discriminators are present. PR 17276. */
19308 unsigned int m_last_line = 0;
19309 bool m_line_has_non_zero_discriminator = false;
19310 };
19311
19312 void
19313 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19314 {
19315 CORE_ADDR addr_adj = (((m_op_index + adjust)
19316 / m_line_header->maximum_ops_per_instruction)
19317 * m_line_header->minimum_instruction_length);
19318 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19319 m_op_index = ((m_op_index + adjust)
19320 % m_line_header->maximum_ops_per_instruction);
19321 }
19322
19323 void
19324 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19325 {
19326 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19327 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19328 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19329 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19330 / m_line_header->maximum_ops_per_instruction)
19331 * m_line_header->minimum_instruction_length);
19332 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19333 m_op_index = ((m_op_index + adj_opcode_d)
19334 % m_line_header->maximum_ops_per_instruction);
19335
19336 int line_delta = m_line_header->line_base + adj_opcode_r;
19337 advance_line (line_delta);
19338 record_line (false);
19339 m_discriminator = 0;
19340 }
19341
19342 void
19343 lnp_state_machine::handle_set_file (file_name_index file)
19344 {
19345 m_file = file;
19346
19347 const file_entry *fe = current_file ();
19348 if (fe == NULL)
19349 dwarf2_debug_line_missing_file_complaint ();
19350 else if (m_record_lines_p)
19351 {
19352 const char *dir = fe->include_dir (m_line_header);
19353
19354 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19355 m_line_has_non_zero_discriminator = m_discriminator != 0;
19356 dwarf2_start_subfile (m_cu, fe->name, dir);
19357 }
19358 }
19359
19360 void
19361 lnp_state_machine::handle_const_add_pc ()
19362 {
19363 CORE_ADDR adjust
19364 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19365
19366 CORE_ADDR addr_adj
19367 = (((m_op_index + adjust)
19368 / m_line_header->maximum_ops_per_instruction)
19369 * m_line_header->minimum_instruction_length);
19370
19371 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19372 m_op_index = ((m_op_index + adjust)
19373 % m_line_header->maximum_ops_per_instruction);
19374 }
19375
19376 /* Return non-zero if we should add LINE to the line number table.
19377 LINE is the line to add, LAST_LINE is the last line that was added,
19378 LAST_SUBFILE is the subfile for LAST_LINE.
19379 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19380 had a non-zero discriminator.
19381
19382 We have to be careful in the presence of discriminators.
19383 E.g., for this line:
19384
19385 for (i = 0; i < 100000; i++);
19386
19387 clang can emit four line number entries for that one line,
19388 each with a different discriminator.
19389 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19390
19391 However, we want gdb to coalesce all four entries into one.
19392 Otherwise the user could stepi into the middle of the line and
19393 gdb would get confused about whether the pc really was in the
19394 middle of the line.
19395
19396 Things are further complicated by the fact that two consecutive
19397 line number entries for the same line is a heuristic used by gcc
19398 to denote the end of the prologue. So we can't just discard duplicate
19399 entries, we have to be selective about it. The heuristic we use is
19400 that we only collapse consecutive entries for the same line if at least
19401 one of those entries has a non-zero discriminator. PR 17276.
19402
19403 Note: Addresses in the line number state machine can never go backwards
19404 within one sequence, thus this coalescing is ok. */
19405
19406 static int
19407 dwarf_record_line_p (struct dwarf2_cu *cu,
19408 unsigned int line, unsigned int last_line,
19409 int line_has_non_zero_discriminator,
19410 struct subfile *last_subfile)
19411 {
19412 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19413 return 1;
19414 if (line != last_line)
19415 return 1;
19416 /* Same line for the same file that we've seen already.
19417 As a last check, for pr 17276, only record the line if the line
19418 has never had a non-zero discriminator. */
19419 if (!line_has_non_zero_discriminator)
19420 return 1;
19421 return 0;
19422 }
19423
19424 /* Use the CU's builder to record line number LINE beginning at
19425 address ADDRESS in the line table of subfile SUBFILE. */
19426
19427 static void
19428 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19429 unsigned int line, CORE_ADDR address, bool is_stmt,
19430 struct dwarf2_cu *cu)
19431 {
19432 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19433
19434 if (dwarf_line_debug)
19435 {
19436 fprintf_unfiltered (gdb_stdlog,
19437 "Recording line %u, file %s, address %s\n",
19438 line, lbasename (subfile->name),
19439 paddress (gdbarch, address));
19440 }
19441
19442 if (cu != nullptr)
19443 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19444 }
19445
19446 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19447 Mark the end of a set of line number records.
19448 The arguments are the same as for dwarf_record_line_1.
19449 If SUBFILE is NULL the request is ignored. */
19450
19451 static void
19452 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19453 CORE_ADDR address, struct dwarf2_cu *cu)
19454 {
19455 if (subfile == NULL)
19456 return;
19457
19458 if (dwarf_line_debug)
19459 {
19460 fprintf_unfiltered (gdb_stdlog,
19461 "Finishing current line, file %s, address %s\n",
19462 lbasename (subfile->name),
19463 paddress (gdbarch, address));
19464 }
19465
19466 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19467 }
19468
19469 void
19470 lnp_state_machine::record_line (bool end_sequence)
19471 {
19472 if (dwarf_line_debug)
19473 {
19474 fprintf_unfiltered (gdb_stdlog,
19475 "Processing actual line %u: file %u,"
19476 " address %s, is_stmt %u, discrim %u%s\n",
19477 m_line, m_file,
19478 paddress (m_gdbarch, m_address),
19479 m_is_stmt, m_discriminator,
19480 (end_sequence ? "\t(end sequence)" : ""));
19481 }
19482
19483 file_entry *fe = current_file ();
19484
19485 if (fe == NULL)
19486 dwarf2_debug_line_missing_file_complaint ();
19487 /* For now we ignore lines not starting on an instruction boundary.
19488 But not when processing end_sequence for compatibility with the
19489 previous version of the code. */
19490 else if (m_op_index == 0 || end_sequence)
19491 {
19492 fe->included_p = 1;
19493 if (m_record_lines_p)
19494 {
19495 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19496 || end_sequence)
19497 {
19498 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19499 m_currently_recording_lines ? m_cu : nullptr);
19500 }
19501
19502 if (!end_sequence)
19503 {
19504 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19505
19506 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19507 m_line_has_non_zero_discriminator,
19508 m_last_subfile))
19509 {
19510 buildsym_compunit *builder = m_cu->get_builder ();
19511 dwarf_record_line_1 (m_gdbarch,
19512 builder->get_current_subfile (),
19513 m_line, m_address, is_stmt,
19514 m_currently_recording_lines ? m_cu : nullptr);
19515 }
19516 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19517 m_last_line = m_line;
19518 }
19519 }
19520 }
19521 }
19522
19523 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19524 line_header *lh, bool record_lines_p)
19525 {
19526 m_cu = cu;
19527 m_gdbarch = arch;
19528 m_record_lines_p = record_lines_p;
19529 m_line_header = lh;
19530
19531 m_currently_recording_lines = true;
19532
19533 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19534 was a line entry for it so that the backend has a chance to adjust it
19535 and also record it in case it needs it. This is currently used by MIPS
19536 code, cf. `mips_adjust_dwarf2_line'. */
19537 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19538 m_is_stmt = lh->default_is_stmt;
19539 m_discriminator = 0;
19540 }
19541
19542 void
19543 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19544 const gdb_byte *line_ptr,
19545 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19546 {
19547 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19548 the pc range of the CU. However, we restrict the test to only ADDRESS
19549 values of zero to preserve GDB's previous behaviour which is to handle
19550 the specific case of a function being GC'd by the linker. */
19551
19552 if (address == 0 && address < unrelocated_lowpc)
19553 {
19554 /* This line table is for a function which has been
19555 GCd by the linker. Ignore it. PR gdb/12528 */
19556
19557 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19558 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19559
19560 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19561 line_offset, objfile_name (objfile));
19562 m_currently_recording_lines = false;
19563 /* Note: m_currently_recording_lines is left as false until we see
19564 DW_LNE_end_sequence. */
19565 }
19566 }
19567
19568 /* Subroutine of dwarf_decode_lines to simplify it.
19569 Process the line number information in LH.
19570 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19571 program in order to set included_p for every referenced header. */
19572
19573 static void
19574 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19575 const int decode_for_pst_p, CORE_ADDR lowpc)
19576 {
19577 const gdb_byte *line_ptr, *extended_end;
19578 const gdb_byte *line_end;
19579 unsigned int bytes_read, extended_len;
19580 unsigned char op_code, extended_op;
19581 CORE_ADDR baseaddr;
19582 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19583 bfd *abfd = objfile->obfd;
19584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19585 /* True if we're recording line info (as opposed to building partial
19586 symtabs and just interested in finding include files mentioned by
19587 the line number program). */
19588 bool record_lines_p = !decode_for_pst_p;
19589
19590 baseaddr = objfile->text_section_offset ();
19591
19592 line_ptr = lh->statement_program_start;
19593 line_end = lh->statement_program_end;
19594
19595 /* Read the statement sequences until there's nothing left. */
19596 while (line_ptr < line_end)
19597 {
19598 /* The DWARF line number program state machine. Reset the state
19599 machine at the start of each sequence. */
19600 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19601 bool end_sequence = false;
19602
19603 if (record_lines_p)
19604 {
19605 /* Start a subfile for the current file of the state
19606 machine. */
19607 const file_entry *fe = state_machine.current_file ();
19608
19609 if (fe != NULL)
19610 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19611 }
19612
19613 /* Decode the table. */
19614 while (line_ptr < line_end && !end_sequence)
19615 {
19616 op_code = read_1_byte (abfd, line_ptr);
19617 line_ptr += 1;
19618
19619 if (op_code >= lh->opcode_base)
19620 {
19621 /* Special opcode. */
19622 state_machine.handle_special_opcode (op_code);
19623 }
19624 else switch (op_code)
19625 {
19626 case DW_LNS_extended_op:
19627 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19628 &bytes_read);
19629 line_ptr += bytes_read;
19630 extended_end = line_ptr + extended_len;
19631 extended_op = read_1_byte (abfd, line_ptr);
19632 line_ptr += 1;
19633 switch (extended_op)
19634 {
19635 case DW_LNE_end_sequence:
19636 state_machine.handle_end_sequence ();
19637 end_sequence = true;
19638 break;
19639 case DW_LNE_set_address:
19640 {
19641 CORE_ADDR address
19642 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19643 line_ptr += bytes_read;
19644
19645 state_machine.check_line_address (cu, line_ptr,
19646 lowpc - baseaddr, address);
19647 state_machine.handle_set_address (baseaddr, address);
19648 }
19649 break;
19650 case DW_LNE_define_file:
19651 {
19652 const char *cur_file;
19653 unsigned int mod_time, length;
19654 dir_index dindex;
19655
19656 cur_file = read_direct_string (abfd, line_ptr,
19657 &bytes_read);
19658 line_ptr += bytes_read;
19659 dindex = (dir_index)
19660 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19661 line_ptr += bytes_read;
19662 mod_time =
19663 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19664 line_ptr += bytes_read;
19665 length =
19666 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19667 line_ptr += bytes_read;
19668 lh->add_file_name (cur_file, dindex, mod_time, length);
19669 }
19670 break;
19671 case DW_LNE_set_discriminator:
19672 {
19673 /* The discriminator is not interesting to the
19674 debugger; just ignore it. We still need to
19675 check its value though:
19676 if there are consecutive entries for the same
19677 (non-prologue) line we want to coalesce them.
19678 PR 17276. */
19679 unsigned int discr
19680 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19681 line_ptr += bytes_read;
19682
19683 state_machine.handle_set_discriminator (discr);
19684 }
19685 break;
19686 default:
19687 complaint (_("mangled .debug_line section"));
19688 return;
19689 }
19690 /* Make sure that we parsed the extended op correctly. If e.g.
19691 we expected a different address size than the producer used,
19692 we may have read the wrong number of bytes. */
19693 if (line_ptr != extended_end)
19694 {
19695 complaint (_("mangled .debug_line section"));
19696 return;
19697 }
19698 break;
19699 case DW_LNS_copy:
19700 state_machine.handle_copy ();
19701 break;
19702 case DW_LNS_advance_pc:
19703 {
19704 CORE_ADDR adjust
19705 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19706 line_ptr += bytes_read;
19707
19708 state_machine.handle_advance_pc (adjust);
19709 }
19710 break;
19711 case DW_LNS_advance_line:
19712 {
19713 int line_delta
19714 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19715 line_ptr += bytes_read;
19716
19717 state_machine.handle_advance_line (line_delta);
19718 }
19719 break;
19720 case DW_LNS_set_file:
19721 {
19722 file_name_index file
19723 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19724 &bytes_read);
19725 line_ptr += bytes_read;
19726
19727 state_machine.handle_set_file (file);
19728 }
19729 break;
19730 case DW_LNS_set_column:
19731 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19732 line_ptr += bytes_read;
19733 break;
19734 case DW_LNS_negate_stmt:
19735 state_machine.handle_negate_stmt ();
19736 break;
19737 case DW_LNS_set_basic_block:
19738 break;
19739 /* Add to the address register of the state machine the
19740 address increment value corresponding to special opcode
19741 255. I.e., this value is scaled by the minimum
19742 instruction length since special opcode 255 would have
19743 scaled the increment. */
19744 case DW_LNS_const_add_pc:
19745 state_machine.handle_const_add_pc ();
19746 break;
19747 case DW_LNS_fixed_advance_pc:
19748 {
19749 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19750 line_ptr += 2;
19751
19752 state_machine.handle_fixed_advance_pc (addr_adj);
19753 }
19754 break;
19755 default:
19756 {
19757 /* Unknown standard opcode, ignore it. */
19758 int i;
19759
19760 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19761 {
19762 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19763 line_ptr += bytes_read;
19764 }
19765 }
19766 }
19767 }
19768
19769 if (!end_sequence)
19770 dwarf2_debug_line_missing_end_sequence_complaint ();
19771
19772 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19773 in which case we still finish recording the last line). */
19774 state_machine.record_line (true);
19775 }
19776 }
19777
19778 /* Decode the Line Number Program (LNP) for the given line_header
19779 structure and CU. The actual information extracted and the type
19780 of structures created from the LNP depends on the value of PST.
19781
19782 1. If PST is NULL, then this procedure uses the data from the program
19783 to create all necessary symbol tables, and their linetables.
19784
19785 2. If PST is not NULL, this procedure reads the program to determine
19786 the list of files included by the unit represented by PST, and
19787 builds all the associated partial symbol tables.
19788
19789 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19790 It is used for relative paths in the line table.
19791 NOTE: When processing partial symtabs (pst != NULL),
19792 comp_dir == pst->dirname.
19793
19794 NOTE: It is important that psymtabs have the same file name (via strcmp)
19795 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19796 symtab we don't use it in the name of the psymtabs we create.
19797 E.g. expand_line_sal requires this when finding psymtabs to expand.
19798 A good testcase for this is mb-inline.exp.
19799
19800 LOWPC is the lowest address in CU (or 0 if not known).
19801
19802 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19803 for its PC<->lines mapping information. Otherwise only the filename
19804 table is read in. */
19805
19806 static void
19807 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19808 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19809 CORE_ADDR lowpc, int decode_mapping)
19810 {
19811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19812 const int decode_for_pst_p = (pst != NULL);
19813
19814 if (decode_mapping)
19815 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19816
19817 if (decode_for_pst_p)
19818 {
19819 /* Now that we're done scanning the Line Header Program, we can
19820 create the psymtab of each included file. */
19821 for (auto &file_entry : lh->file_names ())
19822 if (file_entry.included_p == 1)
19823 {
19824 gdb::unique_xmalloc_ptr<char> name_holder;
19825 const char *include_name =
19826 psymtab_include_file_name (lh, file_entry, pst,
19827 comp_dir, &name_holder);
19828 if (include_name != NULL)
19829 dwarf2_create_include_psymtab (include_name, pst, objfile);
19830 }
19831 }
19832 else
19833 {
19834 /* Make sure a symtab is created for every file, even files
19835 which contain only variables (i.e. no code with associated
19836 line numbers). */
19837 buildsym_compunit *builder = cu->get_builder ();
19838 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19839
19840 for (auto &fe : lh->file_names ())
19841 {
19842 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19843 if (builder->get_current_subfile ()->symtab == NULL)
19844 {
19845 builder->get_current_subfile ()->symtab
19846 = allocate_symtab (cust,
19847 builder->get_current_subfile ()->name);
19848 }
19849 fe.symtab = builder->get_current_subfile ()->symtab;
19850 }
19851 }
19852 }
19853
19854 /* Start a subfile for DWARF. FILENAME is the name of the file and
19855 DIRNAME the name of the source directory which contains FILENAME
19856 or NULL if not known.
19857 This routine tries to keep line numbers from identical absolute and
19858 relative file names in a common subfile.
19859
19860 Using the `list' example from the GDB testsuite, which resides in
19861 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19862 of /srcdir/list0.c yields the following debugging information for list0.c:
19863
19864 DW_AT_name: /srcdir/list0.c
19865 DW_AT_comp_dir: /compdir
19866 files.files[0].name: list0.h
19867 files.files[0].dir: /srcdir
19868 files.files[1].name: list0.c
19869 files.files[1].dir: /srcdir
19870
19871 The line number information for list0.c has to end up in a single
19872 subfile, so that `break /srcdir/list0.c:1' works as expected.
19873 start_subfile will ensure that this happens provided that we pass the
19874 concatenation of files.files[1].dir and files.files[1].name as the
19875 subfile's name. */
19876
19877 static void
19878 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19879 const char *dirname)
19880 {
19881 gdb::unique_xmalloc_ptr<char> copy;
19882
19883 /* In order not to lose the line information directory,
19884 we concatenate it to the filename when it makes sense.
19885 Note that the Dwarf3 standard says (speaking of filenames in line
19886 information): ``The directory index is ignored for file names
19887 that represent full path names''. Thus ignoring dirname in the
19888 `else' branch below isn't an issue. */
19889
19890 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19891 {
19892 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19893 filename = copy.get ();
19894 }
19895
19896 cu->get_builder ()->start_subfile (filename);
19897 }
19898
19899 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19900 buildsym_compunit constructor. */
19901
19902 struct compunit_symtab *
19903 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19904 CORE_ADDR low_pc)
19905 {
19906 gdb_assert (m_builder == nullptr);
19907
19908 m_builder.reset (new struct buildsym_compunit
19909 (per_cu->dwarf2_per_objfile->objfile,
19910 name, comp_dir, language, low_pc));
19911
19912 list_in_scope = get_builder ()->get_file_symbols ();
19913
19914 get_builder ()->record_debugformat ("DWARF 2");
19915 get_builder ()->record_producer (producer);
19916
19917 processing_has_namespace_info = false;
19918
19919 return get_builder ()->get_compunit_symtab ();
19920 }
19921
19922 static void
19923 var_decode_location (struct attribute *attr, struct symbol *sym,
19924 struct dwarf2_cu *cu)
19925 {
19926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19927 struct comp_unit_head *cu_header = &cu->header;
19928
19929 /* NOTE drow/2003-01-30: There used to be a comment and some special
19930 code here to turn a symbol with DW_AT_external and a
19931 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19932 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19933 with some versions of binutils) where shared libraries could have
19934 relocations against symbols in their debug information - the
19935 minimal symbol would have the right address, but the debug info
19936 would not. It's no longer necessary, because we will explicitly
19937 apply relocations when we read in the debug information now. */
19938
19939 /* A DW_AT_location attribute with no contents indicates that a
19940 variable has been optimized away. */
19941 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
19942 {
19943 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19944 return;
19945 }
19946
19947 /* Handle one degenerate form of location expression specially, to
19948 preserve GDB's previous behavior when section offsets are
19949 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19950 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
19951
19952 if (attr->form_is_block ()
19953 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19954 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19955 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19956 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
19957 && (DW_BLOCK (attr)->size
19958 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19959 {
19960 unsigned int dummy;
19961
19962 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19963 SET_SYMBOL_VALUE_ADDRESS
19964 (sym, cu->header.read_address (objfile->obfd,
19965 DW_BLOCK (attr)->data + 1,
19966 &dummy));
19967 else
19968 SET_SYMBOL_VALUE_ADDRESS
19969 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
19970 &dummy));
19971 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19972 fixup_symbol_section (sym, objfile);
19973 SET_SYMBOL_VALUE_ADDRESS
19974 (sym,
19975 SYMBOL_VALUE_ADDRESS (sym)
19976 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
19977 return;
19978 }
19979
19980 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19981 expression evaluator, and use LOC_COMPUTED only when necessary
19982 (i.e. when the value of a register or memory location is
19983 referenced, or a thread-local block, etc.). Then again, it might
19984 not be worthwhile. I'm assuming that it isn't unless performance
19985 or memory numbers show me otherwise. */
19986
19987 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19988
19989 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19990 cu->has_loclist = true;
19991 }
19992
19993 /* Given a pointer to a DWARF information entry, figure out if we need
19994 to make a symbol table entry for it, and if so, create a new entry
19995 and return a pointer to it.
19996 If TYPE is NULL, determine symbol type from the die, otherwise
19997 used the passed type.
19998 If SPACE is not NULL, use it to hold the new symbol. If it is
19999 NULL, allocate a new symbol on the objfile's obstack. */
20000
20001 static struct symbol *
20002 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20003 struct symbol *space)
20004 {
20005 struct dwarf2_per_objfile *dwarf2_per_objfile
20006 = cu->per_cu->dwarf2_per_objfile;
20007 struct objfile *objfile = dwarf2_per_objfile->objfile;
20008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20009 struct symbol *sym = NULL;
20010 const char *name;
20011 struct attribute *attr = NULL;
20012 struct attribute *attr2 = NULL;
20013 CORE_ADDR baseaddr;
20014 struct pending **list_to_add = NULL;
20015
20016 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20017
20018 baseaddr = objfile->text_section_offset ();
20019
20020 name = dwarf2_name (die, cu);
20021 if (name)
20022 {
20023 const char *linkagename;
20024 int suppress_add = 0;
20025
20026 if (space)
20027 sym = space;
20028 else
20029 sym = allocate_symbol (objfile);
20030 OBJSTAT (objfile, n_syms++);
20031
20032 /* Cache this symbol's name and the name's demangled form (if any). */
20033 sym->set_language (cu->language, &objfile->objfile_obstack);
20034 linkagename = dwarf2_physname (name, die, cu);
20035 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20036
20037 /* Fortran does not have mangling standard and the mangling does differ
20038 between gfortran, iFort etc. */
20039 if (cu->language == language_fortran
20040 && symbol_get_demangled_name (sym) == NULL)
20041 symbol_set_demangled_name (sym,
20042 dwarf2_full_name (name, die, cu),
20043 NULL);
20044
20045 /* Default assumptions.
20046 Use the passed type or decode it from the die. */
20047 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20048 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20049 if (type != NULL)
20050 SYMBOL_TYPE (sym) = type;
20051 else
20052 SYMBOL_TYPE (sym) = die_type (die, cu);
20053 attr = dwarf2_attr (die,
20054 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20055 cu);
20056 if (attr != nullptr)
20057 {
20058 SYMBOL_LINE (sym) = DW_UNSND (attr);
20059 }
20060
20061 attr = dwarf2_attr (die,
20062 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20063 cu);
20064 if (attr != nullptr)
20065 {
20066 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20067 struct file_entry *fe;
20068
20069 if (cu->line_header != NULL)
20070 fe = cu->line_header->file_name_at (file_index);
20071 else
20072 fe = NULL;
20073
20074 if (fe == NULL)
20075 complaint (_("file index out of range"));
20076 else
20077 symbol_set_symtab (sym, fe->symtab);
20078 }
20079
20080 switch (die->tag)
20081 {
20082 case DW_TAG_label:
20083 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20084 if (attr != nullptr)
20085 {
20086 CORE_ADDR addr;
20087
20088 addr = attr->value_as_address ();
20089 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20090 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20091 }
20092 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20093 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20094 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20095 add_symbol_to_list (sym, cu->list_in_scope);
20096 break;
20097 case DW_TAG_subprogram:
20098 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20099 finish_block. */
20100 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20101 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20102 if ((attr2 && (DW_UNSND (attr2) != 0))
20103 || cu->language == language_ada
20104 || cu->language == language_fortran)
20105 {
20106 /* Subprograms marked external are stored as a global symbol.
20107 Ada and Fortran subprograms, whether marked external or
20108 not, are always stored as a global symbol, because we want
20109 to be able to access them globally. For instance, we want
20110 to be able to break on a nested subprogram without having
20111 to specify the context. */
20112 list_to_add = cu->get_builder ()->get_global_symbols ();
20113 }
20114 else
20115 {
20116 list_to_add = cu->list_in_scope;
20117 }
20118 break;
20119 case DW_TAG_inlined_subroutine:
20120 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20121 finish_block. */
20122 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20123 SYMBOL_INLINED (sym) = 1;
20124 list_to_add = cu->list_in_scope;
20125 break;
20126 case DW_TAG_template_value_param:
20127 suppress_add = 1;
20128 /* Fall through. */
20129 case DW_TAG_constant:
20130 case DW_TAG_variable:
20131 case DW_TAG_member:
20132 /* Compilation with minimal debug info may result in
20133 variables with missing type entries. Change the
20134 misleading `void' type to something sensible. */
20135 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20136 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20137
20138 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20139 /* In the case of DW_TAG_member, we should only be called for
20140 static const members. */
20141 if (die->tag == DW_TAG_member)
20142 {
20143 /* dwarf2_add_field uses die_is_declaration,
20144 so we do the same. */
20145 gdb_assert (die_is_declaration (die, cu));
20146 gdb_assert (attr);
20147 }
20148 if (attr != nullptr)
20149 {
20150 dwarf2_const_value (attr, sym, cu);
20151 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20152 if (!suppress_add)
20153 {
20154 if (attr2 && (DW_UNSND (attr2) != 0))
20155 list_to_add = cu->get_builder ()->get_global_symbols ();
20156 else
20157 list_to_add = cu->list_in_scope;
20158 }
20159 break;
20160 }
20161 attr = dwarf2_attr (die, DW_AT_location, cu);
20162 if (attr != nullptr)
20163 {
20164 var_decode_location (attr, sym, cu);
20165 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20166
20167 /* Fortran explicitly imports any global symbols to the local
20168 scope by DW_TAG_common_block. */
20169 if (cu->language == language_fortran && die->parent
20170 && die->parent->tag == DW_TAG_common_block)
20171 attr2 = NULL;
20172
20173 if (SYMBOL_CLASS (sym) == LOC_STATIC
20174 && SYMBOL_VALUE_ADDRESS (sym) == 0
20175 && !dwarf2_per_objfile->has_section_at_zero)
20176 {
20177 /* When a static variable is eliminated by the linker,
20178 the corresponding debug information is not stripped
20179 out, but the variable address is set to null;
20180 do not add such variables into symbol table. */
20181 }
20182 else if (attr2 && (DW_UNSND (attr2) != 0))
20183 {
20184 if (SYMBOL_CLASS (sym) == LOC_STATIC
20185 && (objfile->flags & OBJF_MAINLINE) == 0
20186 && dwarf2_per_objfile->can_copy)
20187 {
20188 /* A global static variable might be subject to
20189 copy relocation. We first check for a local
20190 minsym, though, because maybe the symbol was
20191 marked hidden, in which case this would not
20192 apply. */
20193 bound_minimal_symbol found
20194 = (lookup_minimal_symbol_linkage
20195 (sym->linkage_name (), objfile));
20196 if (found.minsym != nullptr)
20197 sym->maybe_copied = 1;
20198 }
20199
20200 /* A variable with DW_AT_external is never static,
20201 but it may be block-scoped. */
20202 list_to_add
20203 = ((cu->list_in_scope
20204 == cu->get_builder ()->get_file_symbols ())
20205 ? cu->get_builder ()->get_global_symbols ()
20206 : cu->list_in_scope);
20207 }
20208 else
20209 list_to_add = cu->list_in_scope;
20210 }
20211 else
20212 {
20213 /* We do not know the address of this symbol.
20214 If it is an external symbol and we have type information
20215 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20216 The address of the variable will then be determined from
20217 the minimal symbol table whenever the variable is
20218 referenced. */
20219 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20220
20221 /* Fortran explicitly imports any global symbols to the local
20222 scope by DW_TAG_common_block. */
20223 if (cu->language == language_fortran && die->parent
20224 && die->parent->tag == DW_TAG_common_block)
20225 {
20226 /* SYMBOL_CLASS doesn't matter here because
20227 read_common_block is going to reset it. */
20228 if (!suppress_add)
20229 list_to_add = cu->list_in_scope;
20230 }
20231 else if (attr2 && (DW_UNSND (attr2) != 0)
20232 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20233 {
20234 /* A variable with DW_AT_external is never static, but it
20235 may be block-scoped. */
20236 list_to_add
20237 = ((cu->list_in_scope
20238 == cu->get_builder ()->get_file_symbols ())
20239 ? cu->get_builder ()->get_global_symbols ()
20240 : cu->list_in_scope);
20241
20242 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20243 }
20244 else if (!die_is_declaration (die, cu))
20245 {
20246 /* Use the default LOC_OPTIMIZED_OUT class. */
20247 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20248 if (!suppress_add)
20249 list_to_add = cu->list_in_scope;
20250 }
20251 }
20252 break;
20253 case DW_TAG_formal_parameter:
20254 {
20255 /* If we are inside a function, mark this as an argument. If
20256 not, we might be looking at an argument to an inlined function
20257 when we do not have enough information to show inlined frames;
20258 pretend it's a local variable in that case so that the user can
20259 still see it. */
20260 struct context_stack *curr
20261 = cu->get_builder ()->get_current_context_stack ();
20262 if (curr != nullptr && curr->name != nullptr)
20263 SYMBOL_IS_ARGUMENT (sym) = 1;
20264 attr = dwarf2_attr (die, DW_AT_location, cu);
20265 if (attr != nullptr)
20266 {
20267 var_decode_location (attr, sym, cu);
20268 }
20269 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20270 if (attr != nullptr)
20271 {
20272 dwarf2_const_value (attr, sym, cu);
20273 }
20274
20275 list_to_add = cu->list_in_scope;
20276 }
20277 break;
20278 case DW_TAG_unspecified_parameters:
20279 /* From varargs functions; gdb doesn't seem to have any
20280 interest in this information, so just ignore it for now.
20281 (FIXME?) */
20282 break;
20283 case DW_TAG_template_type_param:
20284 suppress_add = 1;
20285 /* Fall through. */
20286 case DW_TAG_class_type:
20287 case DW_TAG_interface_type:
20288 case DW_TAG_structure_type:
20289 case DW_TAG_union_type:
20290 case DW_TAG_set_type:
20291 case DW_TAG_enumeration_type:
20292 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20293 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20294
20295 {
20296 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20297 really ever be static objects: otherwise, if you try
20298 to, say, break of a class's method and you're in a file
20299 which doesn't mention that class, it won't work unless
20300 the check for all static symbols in lookup_symbol_aux
20301 saves you. See the OtherFileClass tests in
20302 gdb.c++/namespace.exp. */
20303
20304 if (!suppress_add)
20305 {
20306 buildsym_compunit *builder = cu->get_builder ();
20307 list_to_add
20308 = (cu->list_in_scope == builder->get_file_symbols ()
20309 && cu->language == language_cplus
20310 ? builder->get_global_symbols ()
20311 : cu->list_in_scope);
20312
20313 /* The semantics of C++ state that "struct foo {
20314 ... }" also defines a typedef for "foo". */
20315 if (cu->language == language_cplus
20316 || cu->language == language_ada
20317 || cu->language == language_d
20318 || cu->language == language_rust)
20319 {
20320 /* The symbol's name is already allocated along
20321 with this objfile, so we don't need to
20322 duplicate it for the type. */
20323 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20324 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20325 }
20326 }
20327 }
20328 break;
20329 case DW_TAG_typedef:
20330 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20331 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20332 list_to_add = cu->list_in_scope;
20333 break;
20334 case DW_TAG_base_type:
20335 case DW_TAG_subrange_type:
20336 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20337 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20338 list_to_add = cu->list_in_scope;
20339 break;
20340 case DW_TAG_enumerator:
20341 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20342 if (attr != nullptr)
20343 {
20344 dwarf2_const_value (attr, sym, cu);
20345 }
20346 {
20347 /* NOTE: carlton/2003-11-10: See comment above in the
20348 DW_TAG_class_type, etc. block. */
20349
20350 list_to_add
20351 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20352 && cu->language == language_cplus
20353 ? cu->get_builder ()->get_global_symbols ()
20354 : cu->list_in_scope);
20355 }
20356 break;
20357 case DW_TAG_imported_declaration:
20358 case DW_TAG_namespace:
20359 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20360 list_to_add = cu->get_builder ()->get_global_symbols ();
20361 break;
20362 case DW_TAG_module:
20363 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20364 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20365 list_to_add = cu->get_builder ()->get_global_symbols ();
20366 break;
20367 case DW_TAG_common_block:
20368 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20369 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20370 add_symbol_to_list (sym, cu->list_in_scope);
20371 break;
20372 default:
20373 /* Not a tag we recognize. Hopefully we aren't processing
20374 trash data, but since we must specifically ignore things
20375 we don't recognize, there is nothing else we should do at
20376 this point. */
20377 complaint (_("unsupported tag: '%s'"),
20378 dwarf_tag_name (die->tag));
20379 break;
20380 }
20381
20382 if (suppress_add)
20383 {
20384 sym->hash_next = objfile->template_symbols;
20385 objfile->template_symbols = sym;
20386 list_to_add = NULL;
20387 }
20388
20389 if (list_to_add != NULL)
20390 add_symbol_to_list (sym, list_to_add);
20391
20392 /* For the benefit of old versions of GCC, check for anonymous
20393 namespaces based on the demangled name. */
20394 if (!cu->processing_has_namespace_info
20395 && cu->language == language_cplus)
20396 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20397 }
20398 return (sym);
20399 }
20400
20401 /* Given an attr with a DW_FORM_dataN value in host byte order,
20402 zero-extend it as appropriate for the symbol's type. The DWARF
20403 standard (v4) is not entirely clear about the meaning of using
20404 DW_FORM_dataN for a constant with a signed type, where the type is
20405 wider than the data. The conclusion of a discussion on the DWARF
20406 list was that this is unspecified. We choose to always zero-extend
20407 because that is the interpretation long in use by GCC. */
20408
20409 static gdb_byte *
20410 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20411 struct dwarf2_cu *cu, LONGEST *value, int bits)
20412 {
20413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20414 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20415 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20416 LONGEST l = DW_UNSND (attr);
20417
20418 if (bits < sizeof (*value) * 8)
20419 {
20420 l &= ((LONGEST) 1 << bits) - 1;
20421 *value = l;
20422 }
20423 else if (bits == sizeof (*value) * 8)
20424 *value = l;
20425 else
20426 {
20427 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20428 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20429 return bytes;
20430 }
20431
20432 return NULL;
20433 }
20434
20435 /* Read a constant value from an attribute. Either set *VALUE, or if
20436 the value does not fit in *VALUE, set *BYTES - either already
20437 allocated on the objfile obstack, or newly allocated on OBSTACK,
20438 or, set *BATON, if we translated the constant to a location
20439 expression. */
20440
20441 static void
20442 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20443 const char *name, struct obstack *obstack,
20444 struct dwarf2_cu *cu,
20445 LONGEST *value, const gdb_byte **bytes,
20446 struct dwarf2_locexpr_baton **baton)
20447 {
20448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20449 struct comp_unit_head *cu_header = &cu->header;
20450 struct dwarf_block *blk;
20451 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20452 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20453
20454 *value = 0;
20455 *bytes = NULL;
20456 *baton = NULL;
20457
20458 switch (attr->form)
20459 {
20460 case DW_FORM_addr:
20461 case DW_FORM_addrx:
20462 case DW_FORM_GNU_addr_index:
20463 {
20464 gdb_byte *data;
20465
20466 if (TYPE_LENGTH (type) != cu_header->addr_size)
20467 dwarf2_const_value_length_mismatch_complaint (name,
20468 cu_header->addr_size,
20469 TYPE_LENGTH (type));
20470 /* Symbols of this form are reasonably rare, so we just
20471 piggyback on the existing location code rather than writing
20472 a new implementation of symbol_computed_ops. */
20473 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20474 (*baton)->per_cu = cu->per_cu;
20475 gdb_assert ((*baton)->per_cu);
20476
20477 (*baton)->size = 2 + cu_header->addr_size;
20478 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20479 (*baton)->data = data;
20480
20481 data[0] = DW_OP_addr;
20482 store_unsigned_integer (&data[1], cu_header->addr_size,
20483 byte_order, DW_ADDR (attr));
20484 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20485 }
20486 break;
20487 case DW_FORM_string:
20488 case DW_FORM_strp:
20489 case DW_FORM_strx:
20490 case DW_FORM_GNU_str_index:
20491 case DW_FORM_GNU_strp_alt:
20492 /* DW_STRING is already allocated on the objfile obstack, point
20493 directly to it. */
20494 *bytes = (const gdb_byte *) DW_STRING (attr);
20495 break;
20496 case DW_FORM_block1:
20497 case DW_FORM_block2:
20498 case DW_FORM_block4:
20499 case DW_FORM_block:
20500 case DW_FORM_exprloc:
20501 case DW_FORM_data16:
20502 blk = DW_BLOCK (attr);
20503 if (TYPE_LENGTH (type) != blk->size)
20504 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20505 TYPE_LENGTH (type));
20506 *bytes = blk->data;
20507 break;
20508
20509 /* The DW_AT_const_value attributes are supposed to carry the
20510 symbol's value "represented as it would be on the target
20511 architecture." By the time we get here, it's already been
20512 converted to host endianness, so we just need to sign- or
20513 zero-extend it as appropriate. */
20514 case DW_FORM_data1:
20515 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20516 break;
20517 case DW_FORM_data2:
20518 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20519 break;
20520 case DW_FORM_data4:
20521 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20522 break;
20523 case DW_FORM_data8:
20524 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20525 break;
20526
20527 case DW_FORM_sdata:
20528 case DW_FORM_implicit_const:
20529 *value = DW_SND (attr);
20530 break;
20531
20532 case DW_FORM_udata:
20533 *value = DW_UNSND (attr);
20534 break;
20535
20536 default:
20537 complaint (_("unsupported const value attribute form: '%s'"),
20538 dwarf_form_name (attr->form));
20539 *value = 0;
20540 break;
20541 }
20542 }
20543
20544
20545 /* Copy constant value from an attribute to a symbol. */
20546
20547 static void
20548 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20549 struct dwarf2_cu *cu)
20550 {
20551 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20552 LONGEST value;
20553 const gdb_byte *bytes;
20554 struct dwarf2_locexpr_baton *baton;
20555
20556 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20557 sym->print_name (),
20558 &objfile->objfile_obstack, cu,
20559 &value, &bytes, &baton);
20560
20561 if (baton != NULL)
20562 {
20563 SYMBOL_LOCATION_BATON (sym) = baton;
20564 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20565 }
20566 else if (bytes != NULL)
20567 {
20568 SYMBOL_VALUE_BYTES (sym) = bytes;
20569 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20570 }
20571 else
20572 {
20573 SYMBOL_VALUE (sym) = value;
20574 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20575 }
20576 }
20577
20578 /* Return the type of the die in question using its DW_AT_type attribute. */
20579
20580 static struct type *
20581 die_type (struct die_info *die, struct dwarf2_cu *cu)
20582 {
20583 struct attribute *type_attr;
20584
20585 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20586 if (!type_attr)
20587 {
20588 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20589 /* A missing DW_AT_type represents a void type. */
20590 return objfile_type (objfile)->builtin_void;
20591 }
20592
20593 return lookup_die_type (die, type_attr, cu);
20594 }
20595
20596 /* True iff CU's producer generates GNAT Ada auxiliary information
20597 that allows to find parallel types through that information instead
20598 of having to do expensive parallel lookups by type name. */
20599
20600 static int
20601 need_gnat_info (struct dwarf2_cu *cu)
20602 {
20603 /* Assume that the Ada compiler was GNAT, which always produces
20604 the auxiliary information. */
20605 return (cu->language == language_ada);
20606 }
20607
20608 /* Return the auxiliary type of the die in question using its
20609 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20610 attribute is not present. */
20611
20612 static struct type *
20613 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20614 {
20615 struct attribute *type_attr;
20616
20617 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20618 if (!type_attr)
20619 return NULL;
20620
20621 return lookup_die_type (die, type_attr, cu);
20622 }
20623
20624 /* If DIE has a descriptive_type attribute, then set the TYPE's
20625 descriptive type accordingly. */
20626
20627 static void
20628 set_descriptive_type (struct type *type, struct die_info *die,
20629 struct dwarf2_cu *cu)
20630 {
20631 struct type *descriptive_type = die_descriptive_type (die, cu);
20632
20633 if (descriptive_type)
20634 {
20635 ALLOCATE_GNAT_AUX_TYPE (type);
20636 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20637 }
20638 }
20639
20640 /* Return the containing type of the die in question using its
20641 DW_AT_containing_type attribute. */
20642
20643 static struct type *
20644 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20645 {
20646 struct attribute *type_attr;
20647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20648
20649 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20650 if (!type_attr)
20651 error (_("Dwarf Error: Problem turning containing type into gdb type "
20652 "[in module %s]"), objfile_name (objfile));
20653
20654 return lookup_die_type (die, type_attr, cu);
20655 }
20656
20657 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20658
20659 static struct type *
20660 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20661 {
20662 struct dwarf2_per_objfile *dwarf2_per_objfile
20663 = cu->per_cu->dwarf2_per_objfile;
20664 struct objfile *objfile = dwarf2_per_objfile->objfile;
20665 char *saved;
20666
20667 std::string message
20668 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20669 objfile_name (objfile),
20670 sect_offset_str (cu->header.sect_off),
20671 sect_offset_str (die->sect_off));
20672 saved = obstack_strdup (&objfile->objfile_obstack, message);
20673
20674 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20675 }
20676
20677 /* Look up the type of DIE in CU using its type attribute ATTR.
20678 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20679 DW_AT_containing_type.
20680 If there is no type substitute an error marker. */
20681
20682 static struct type *
20683 lookup_die_type (struct die_info *die, const struct attribute *attr,
20684 struct dwarf2_cu *cu)
20685 {
20686 struct dwarf2_per_objfile *dwarf2_per_objfile
20687 = cu->per_cu->dwarf2_per_objfile;
20688 struct objfile *objfile = dwarf2_per_objfile->objfile;
20689 struct type *this_type;
20690
20691 gdb_assert (attr->name == DW_AT_type
20692 || attr->name == DW_AT_GNAT_descriptive_type
20693 || attr->name == DW_AT_containing_type);
20694
20695 /* First see if we have it cached. */
20696
20697 if (attr->form == DW_FORM_GNU_ref_alt)
20698 {
20699 struct dwarf2_per_cu_data *per_cu;
20700 sect_offset sect_off = attr->get_ref_die_offset ();
20701
20702 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20703 dwarf2_per_objfile);
20704 this_type = get_die_type_at_offset (sect_off, per_cu);
20705 }
20706 else if (attr->form_is_ref ())
20707 {
20708 sect_offset sect_off = attr->get_ref_die_offset ();
20709
20710 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20711 }
20712 else if (attr->form == DW_FORM_ref_sig8)
20713 {
20714 ULONGEST signature = DW_SIGNATURE (attr);
20715
20716 return get_signatured_type (die, signature, cu);
20717 }
20718 else
20719 {
20720 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20721 " at %s [in module %s]"),
20722 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20723 objfile_name (objfile));
20724 return build_error_marker_type (cu, die);
20725 }
20726
20727 /* If not cached we need to read it in. */
20728
20729 if (this_type == NULL)
20730 {
20731 struct die_info *type_die = NULL;
20732 struct dwarf2_cu *type_cu = cu;
20733
20734 if (attr->form_is_ref ())
20735 type_die = follow_die_ref (die, attr, &type_cu);
20736 if (type_die == NULL)
20737 return build_error_marker_type (cu, die);
20738 /* If we find the type now, it's probably because the type came
20739 from an inter-CU reference and the type's CU got expanded before
20740 ours. */
20741 this_type = read_type_die (type_die, type_cu);
20742 }
20743
20744 /* If we still don't have a type use an error marker. */
20745
20746 if (this_type == NULL)
20747 return build_error_marker_type (cu, die);
20748
20749 return this_type;
20750 }
20751
20752 /* Return the type in DIE, CU.
20753 Returns NULL for invalid types.
20754
20755 This first does a lookup in die_type_hash,
20756 and only reads the die in if necessary.
20757
20758 NOTE: This can be called when reading in partial or full symbols. */
20759
20760 static struct type *
20761 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20762 {
20763 struct type *this_type;
20764
20765 this_type = get_die_type (die, cu);
20766 if (this_type)
20767 return this_type;
20768
20769 return read_type_die_1 (die, cu);
20770 }
20771
20772 /* Read the type in DIE, CU.
20773 Returns NULL for invalid types. */
20774
20775 static struct type *
20776 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20777 {
20778 struct type *this_type = NULL;
20779
20780 switch (die->tag)
20781 {
20782 case DW_TAG_class_type:
20783 case DW_TAG_interface_type:
20784 case DW_TAG_structure_type:
20785 case DW_TAG_union_type:
20786 this_type = read_structure_type (die, cu);
20787 break;
20788 case DW_TAG_enumeration_type:
20789 this_type = read_enumeration_type (die, cu);
20790 break;
20791 case DW_TAG_subprogram:
20792 case DW_TAG_subroutine_type:
20793 case DW_TAG_inlined_subroutine:
20794 this_type = read_subroutine_type (die, cu);
20795 break;
20796 case DW_TAG_array_type:
20797 this_type = read_array_type (die, cu);
20798 break;
20799 case DW_TAG_set_type:
20800 this_type = read_set_type (die, cu);
20801 break;
20802 case DW_TAG_pointer_type:
20803 this_type = read_tag_pointer_type (die, cu);
20804 break;
20805 case DW_TAG_ptr_to_member_type:
20806 this_type = read_tag_ptr_to_member_type (die, cu);
20807 break;
20808 case DW_TAG_reference_type:
20809 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20810 break;
20811 case DW_TAG_rvalue_reference_type:
20812 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20813 break;
20814 case DW_TAG_const_type:
20815 this_type = read_tag_const_type (die, cu);
20816 break;
20817 case DW_TAG_volatile_type:
20818 this_type = read_tag_volatile_type (die, cu);
20819 break;
20820 case DW_TAG_restrict_type:
20821 this_type = read_tag_restrict_type (die, cu);
20822 break;
20823 case DW_TAG_string_type:
20824 this_type = read_tag_string_type (die, cu);
20825 break;
20826 case DW_TAG_typedef:
20827 this_type = read_typedef (die, cu);
20828 break;
20829 case DW_TAG_subrange_type:
20830 this_type = read_subrange_type (die, cu);
20831 break;
20832 case DW_TAG_base_type:
20833 this_type = read_base_type (die, cu);
20834 break;
20835 case DW_TAG_unspecified_type:
20836 this_type = read_unspecified_type (die, cu);
20837 break;
20838 case DW_TAG_namespace:
20839 this_type = read_namespace_type (die, cu);
20840 break;
20841 case DW_TAG_module:
20842 this_type = read_module_type (die, cu);
20843 break;
20844 case DW_TAG_atomic_type:
20845 this_type = read_tag_atomic_type (die, cu);
20846 break;
20847 default:
20848 complaint (_("unexpected tag in read_type_die: '%s'"),
20849 dwarf_tag_name (die->tag));
20850 break;
20851 }
20852
20853 return this_type;
20854 }
20855
20856 /* See if we can figure out if the class lives in a namespace. We do
20857 this by looking for a member function; its demangled name will
20858 contain namespace info, if there is any.
20859 Return the computed name or NULL.
20860 Space for the result is allocated on the objfile's obstack.
20861 This is the full-die version of guess_partial_die_structure_name.
20862 In this case we know DIE has no useful parent. */
20863
20864 static const char *
20865 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20866 {
20867 struct die_info *spec_die;
20868 struct dwarf2_cu *spec_cu;
20869 struct die_info *child;
20870 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20871
20872 spec_cu = cu;
20873 spec_die = die_specification (die, &spec_cu);
20874 if (spec_die != NULL)
20875 {
20876 die = spec_die;
20877 cu = spec_cu;
20878 }
20879
20880 for (child = die->child;
20881 child != NULL;
20882 child = child->sibling)
20883 {
20884 if (child->tag == DW_TAG_subprogram)
20885 {
20886 const char *linkage_name = dw2_linkage_name (child, cu);
20887
20888 if (linkage_name != NULL)
20889 {
20890 gdb::unique_xmalloc_ptr<char> actual_name
20891 (language_class_name_from_physname (cu->language_defn,
20892 linkage_name));
20893 const char *name = NULL;
20894
20895 if (actual_name != NULL)
20896 {
20897 const char *die_name = dwarf2_name (die, cu);
20898
20899 if (die_name != NULL
20900 && strcmp (die_name, actual_name.get ()) != 0)
20901 {
20902 /* Strip off the class name from the full name.
20903 We want the prefix. */
20904 int die_name_len = strlen (die_name);
20905 int actual_name_len = strlen (actual_name.get ());
20906 const char *ptr = actual_name.get ();
20907
20908 /* Test for '::' as a sanity check. */
20909 if (actual_name_len > die_name_len + 2
20910 && ptr[actual_name_len - die_name_len - 1] == ':')
20911 name = obstack_strndup (
20912 &objfile->per_bfd->storage_obstack,
20913 ptr, actual_name_len - die_name_len - 2);
20914 }
20915 }
20916 return name;
20917 }
20918 }
20919 }
20920
20921 return NULL;
20922 }
20923
20924 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20925 prefix part in such case. See
20926 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20927
20928 static const char *
20929 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20930 {
20931 struct attribute *attr;
20932 const char *base;
20933
20934 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20935 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20936 return NULL;
20937
20938 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20939 return NULL;
20940
20941 attr = dw2_linkage_name_attr (die, cu);
20942 if (attr == NULL || DW_STRING (attr) == NULL)
20943 return NULL;
20944
20945 /* dwarf2_name had to be already called. */
20946 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20947
20948 /* Strip the base name, keep any leading namespaces/classes. */
20949 base = strrchr (DW_STRING (attr), ':');
20950 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20951 return "";
20952
20953 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20954 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20955 DW_STRING (attr),
20956 &base[-1] - DW_STRING (attr));
20957 }
20958
20959 /* Return the name of the namespace/class that DIE is defined within,
20960 or "" if we can't tell. The caller should not xfree the result.
20961
20962 For example, if we're within the method foo() in the following
20963 code:
20964
20965 namespace N {
20966 class C {
20967 void foo () {
20968 }
20969 };
20970 }
20971
20972 then determine_prefix on foo's die will return "N::C". */
20973
20974 static const char *
20975 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20976 {
20977 struct dwarf2_per_objfile *dwarf2_per_objfile
20978 = cu->per_cu->dwarf2_per_objfile;
20979 struct die_info *parent, *spec_die;
20980 struct dwarf2_cu *spec_cu;
20981 struct type *parent_type;
20982 const char *retval;
20983
20984 if (cu->language != language_cplus
20985 && cu->language != language_fortran && cu->language != language_d
20986 && cu->language != language_rust)
20987 return "";
20988
20989 retval = anonymous_struct_prefix (die, cu);
20990 if (retval)
20991 return retval;
20992
20993 /* We have to be careful in the presence of DW_AT_specification.
20994 For example, with GCC 3.4, given the code
20995
20996 namespace N {
20997 void foo() {
20998 // Definition of N::foo.
20999 }
21000 }
21001
21002 then we'll have a tree of DIEs like this:
21003
21004 1: DW_TAG_compile_unit
21005 2: DW_TAG_namespace // N
21006 3: DW_TAG_subprogram // declaration of N::foo
21007 4: DW_TAG_subprogram // definition of N::foo
21008 DW_AT_specification // refers to die #3
21009
21010 Thus, when processing die #4, we have to pretend that we're in
21011 the context of its DW_AT_specification, namely the contex of die
21012 #3. */
21013 spec_cu = cu;
21014 spec_die = die_specification (die, &spec_cu);
21015 if (spec_die == NULL)
21016 parent = die->parent;
21017 else
21018 {
21019 parent = spec_die->parent;
21020 cu = spec_cu;
21021 }
21022
21023 if (parent == NULL)
21024 return "";
21025 else if (parent->building_fullname)
21026 {
21027 const char *name;
21028 const char *parent_name;
21029
21030 /* It has been seen on RealView 2.2 built binaries,
21031 DW_TAG_template_type_param types actually _defined_ as
21032 children of the parent class:
21033
21034 enum E {};
21035 template class <class Enum> Class{};
21036 Class<enum E> class_e;
21037
21038 1: DW_TAG_class_type (Class)
21039 2: DW_TAG_enumeration_type (E)
21040 3: DW_TAG_enumerator (enum1:0)
21041 3: DW_TAG_enumerator (enum2:1)
21042 ...
21043 2: DW_TAG_template_type_param
21044 DW_AT_type DW_FORM_ref_udata (E)
21045
21046 Besides being broken debug info, it can put GDB into an
21047 infinite loop. Consider:
21048
21049 When we're building the full name for Class<E>, we'll start
21050 at Class, and go look over its template type parameters,
21051 finding E. We'll then try to build the full name of E, and
21052 reach here. We're now trying to build the full name of E,
21053 and look over the parent DIE for containing scope. In the
21054 broken case, if we followed the parent DIE of E, we'd again
21055 find Class, and once again go look at its template type
21056 arguments, etc., etc. Simply don't consider such parent die
21057 as source-level parent of this die (it can't be, the language
21058 doesn't allow it), and break the loop here. */
21059 name = dwarf2_name (die, cu);
21060 parent_name = dwarf2_name (parent, cu);
21061 complaint (_("template param type '%s' defined within parent '%s'"),
21062 name ? name : "<unknown>",
21063 parent_name ? parent_name : "<unknown>");
21064 return "";
21065 }
21066 else
21067 switch (parent->tag)
21068 {
21069 case DW_TAG_namespace:
21070 parent_type = read_type_die (parent, cu);
21071 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21072 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21073 Work around this problem here. */
21074 if (cu->language == language_cplus
21075 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21076 return "";
21077 /* We give a name to even anonymous namespaces. */
21078 return TYPE_NAME (parent_type);
21079 case DW_TAG_class_type:
21080 case DW_TAG_interface_type:
21081 case DW_TAG_structure_type:
21082 case DW_TAG_union_type:
21083 case DW_TAG_module:
21084 parent_type = read_type_die (parent, cu);
21085 if (TYPE_NAME (parent_type) != NULL)
21086 return TYPE_NAME (parent_type);
21087 else
21088 /* An anonymous structure is only allowed non-static data
21089 members; no typedefs, no member functions, et cetera.
21090 So it does not need a prefix. */
21091 return "";
21092 case DW_TAG_compile_unit:
21093 case DW_TAG_partial_unit:
21094 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21095 if (cu->language == language_cplus
21096 && !dwarf2_per_objfile->types.empty ()
21097 && die->child != NULL
21098 && (die->tag == DW_TAG_class_type
21099 || die->tag == DW_TAG_structure_type
21100 || die->tag == DW_TAG_union_type))
21101 {
21102 const char *name = guess_full_die_structure_name (die, cu);
21103 if (name != NULL)
21104 return name;
21105 }
21106 return "";
21107 case DW_TAG_subprogram:
21108 /* Nested subroutines in Fortran get a prefix with the name
21109 of the parent's subroutine. */
21110 if (cu->language == language_fortran)
21111 {
21112 if ((die->tag == DW_TAG_subprogram)
21113 && (dwarf2_name (parent, cu) != NULL))
21114 return dwarf2_name (parent, cu);
21115 }
21116 return determine_prefix (parent, cu);
21117 case DW_TAG_enumeration_type:
21118 parent_type = read_type_die (parent, cu);
21119 if (TYPE_DECLARED_CLASS (parent_type))
21120 {
21121 if (TYPE_NAME (parent_type) != NULL)
21122 return TYPE_NAME (parent_type);
21123 return "";
21124 }
21125 /* Fall through. */
21126 default:
21127 return determine_prefix (parent, cu);
21128 }
21129 }
21130
21131 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21132 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21133 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21134 an obconcat, otherwise allocate storage for the result. The CU argument is
21135 used to determine the language and hence, the appropriate separator. */
21136
21137 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21138
21139 static char *
21140 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21141 int physname, struct dwarf2_cu *cu)
21142 {
21143 const char *lead = "";
21144 const char *sep;
21145
21146 if (suffix == NULL || suffix[0] == '\0'
21147 || prefix == NULL || prefix[0] == '\0')
21148 sep = "";
21149 else if (cu->language == language_d)
21150 {
21151 /* For D, the 'main' function could be defined in any module, but it
21152 should never be prefixed. */
21153 if (strcmp (suffix, "D main") == 0)
21154 {
21155 prefix = "";
21156 sep = "";
21157 }
21158 else
21159 sep = ".";
21160 }
21161 else if (cu->language == language_fortran && physname)
21162 {
21163 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21164 DW_AT_MIPS_linkage_name is preferred and used instead. */
21165
21166 lead = "__";
21167 sep = "_MOD_";
21168 }
21169 else
21170 sep = "::";
21171
21172 if (prefix == NULL)
21173 prefix = "";
21174 if (suffix == NULL)
21175 suffix = "";
21176
21177 if (obs == NULL)
21178 {
21179 char *retval
21180 = ((char *)
21181 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21182
21183 strcpy (retval, lead);
21184 strcat (retval, prefix);
21185 strcat (retval, sep);
21186 strcat (retval, suffix);
21187 return retval;
21188 }
21189 else
21190 {
21191 /* We have an obstack. */
21192 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21193 }
21194 }
21195
21196 /* Get name of a die, return NULL if not found. */
21197
21198 static const char *
21199 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21200 struct objfile *objfile)
21201 {
21202 if (name && cu->language == language_cplus)
21203 {
21204 std::string canon_name = cp_canonicalize_string (name);
21205
21206 if (!canon_name.empty ())
21207 {
21208 if (canon_name != name)
21209 name = objfile->intern (canon_name);
21210 }
21211 }
21212
21213 return name;
21214 }
21215
21216 /* Get name of a die, return NULL if not found.
21217 Anonymous namespaces are converted to their magic string. */
21218
21219 static const char *
21220 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21221 {
21222 struct attribute *attr;
21223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21224
21225 attr = dwarf2_attr (die, DW_AT_name, cu);
21226 if ((!attr || !DW_STRING (attr))
21227 && die->tag != DW_TAG_namespace
21228 && die->tag != DW_TAG_class_type
21229 && die->tag != DW_TAG_interface_type
21230 && die->tag != DW_TAG_structure_type
21231 && die->tag != DW_TAG_union_type)
21232 return NULL;
21233
21234 switch (die->tag)
21235 {
21236 case DW_TAG_compile_unit:
21237 case DW_TAG_partial_unit:
21238 /* Compilation units have a DW_AT_name that is a filename, not
21239 a source language identifier. */
21240 case DW_TAG_enumeration_type:
21241 case DW_TAG_enumerator:
21242 /* These tags always have simple identifiers already; no need
21243 to canonicalize them. */
21244 return DW_STRING (attr);
21245
21246 case DW_TAG_namespace:
21247 if (attr != NULL && DW_STRING (attr) != NULL)
21248 return DW_STRING (attr);
21249 return CP_ANONYMOUS_NAMESPACE_STR;
21250
21251 case DW_TAG_class_type:
21252 case DW_TAG_interface_type:
21253 case DW_TAG_structure_type:
21254 case DW_TAG_union_type:
21255 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21256 structures or unions. These were of the form "._%d" in GCC 4.1,
21257 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21258 and GCC 4.4. We work around this problem by ignoring these. */
21259 if (attr && DW_STRING (attr)
21260 && (startswith (DW_STRING (attr), "._")
21261 || startswith (DW_STRING (attr), "<anonymous")))
21262 return NULL;
21263
21264 /* GCC might emit a nameless typedef that has a linkage name. See
21265 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21266 if (!attr || DW_STRING (attr) == NULL)
21267 {
21268 attr = dw2_linkage_name_attr (die, cu);
21269 if (attr == NULL || DW_STRING (attr) == NULL)
21270 return NULL;
21271
21272 /* Avoid demangling DW_STRING (attr) the second time on a second
21273 call for the same DIE. */
21274 if (!DW_STRING_IS_CANONICAL (attr))
21275 {
21276 gdb::unique_xmalloc_ptr<char> demangled
21277 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21278 if (demangled == nullptr)
21279 return nullptr;
21280
21281 DW_STRING (attr) = objfile->intern (demangled.get ());
21282 DW_STRING_IS_CANONICAL (attr) = 1;
21283 }
21284
21285 /* Strip any leading namespaces/classes, keep only the base name.
21286 DW_AT_name for named DIEs does not contain the prefixes. */
21287 const char *base = strrchr (DW_STRING (attr), ':');
21288 if (base && base > DW_STRING (attr) && base[-1] == ':')
21289 return &base[1];
21290 else
21291 return DW_STRING (attr);
21292 }
21293 break;
21294
21295 default:
21296 break;
21297 }
21298
21299 if (!DW_STRING_IS_CANONICAL (attr))
21300 {
21301 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21302 objfile);
21303 DW_STRING_IS_CANONICAL (attr) = 1;
21304 }
21305 return DW_STRING (attr);
21306 }
21307
21308 /* Return the die that this die in an extension of, or NULL if there
21309 is none. *EXT_CU is the CU containing DIE on input, and the CU
21310 containing the return value on output. */
21311
21312 static struct die_info *
21313 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21314 {
21315 struct attribute *attr;
21316
21317 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21318 if (attr == NULL)
21319 return NULL;
21320
21321 return follow_die_ref (die, attr, ext_cu);
21322 }
21323
21324 static void
21325 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21326 {
21327 unsigned int i;
21328
21329 print_spaces (indent, f);
21330 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21331 dwarf_tag_name (die->tag), die->abbrev,
21332 sect_offset_str (die->sect_off));
21333
21334 if (die->parent != NULL)
21335 {
21336 print_spaces (indent, f);
21337 fprintf_unfiltered (f, " parent at offset: %s\n",
21338 sect_offset_str (die->parent->sect_off));
21339 }
21340
21341 print_spaces (indent, f);
21342 fprintf_unfiltered (f, " has children: %s\n",
21343 dwarf_bool_name (die->child != NULL));
21344
21345 print_spaces (indent, f);
21346 fprintf_unfiltered (f, " attributes:\n");
21347
21348 for (i = 0; i < die->num_attrs; ++i)
21349 {
21350 print_spaces (indent, f);
21351 fprintf_unfiltered (f, " %s (%s) ",
21352 dwarf_attr_name (die->attrs[i].name),
21353 dwarf_form_name (die->attrs[i].form));
21354
21355 switch (die->attrs[i].form)
21356 {
21357 case DW_FORM_addr:
21358 case DW_FORM_addrx:
21359 case DW_FORM_GNU_addr_index:
21360 fprintf_unfiltered (f, "address: ");
21361 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21362 break;
21363 case DW_FORM_block2:
21364 case DW_FORM_block4:
21365 case DW_FORM_block:
21366 case DW_FORM_block1:
21367 fprintf_unfiltered (f, "block: size %s",
21368 pulongest (DW_BLOCK (&die->attrs[i])->size));
21369 break;
21370 case DW_FORM_exprloc:
21371 fprintf_unfiltered (f, "expression: size %s",
21372 pulongest (DW_BLOCK (&die->attrs[i])->size));
21373 break;
21374 case DW_FORM_data16:
21375 fprintf_unfiltered (f, "constant of 16 bytes");
21376 break;
21377 case DW_FORM_ref_addr:
21378 fprintf_unfiltered (f, "ref address: ");
21379 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21380 break;
21381 case DW_FORM_GNU_ref_alt:
21382 fprintf_unfiltered (f, "alt ref address: ");
21383 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21384 break;
21385 case DW_FORM_ref1:
21386 case DW_FORM_ref2:
21387 case DW_FORM_ref4:
21388 case DW_FORM_ref8:
21389 case DW_FORM_ref_udata:
21390 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21391 (long) (DW_UNSND (&die->attrs[i])));
21392 break;
21393 case DW_FORM_data1:
21394 case DW_FORM_data2:
21395 case DW_FORM_data4:
21396 case DW_FORM_data8:
21397 case DW_FORM_udata:
21398 case DW_FORM_sdata:
21399 fprintf_unfiltered (f, "constant: %s",
21400 pulongest (DW_UNSND (&die->attrs[i])));
21401 break;
21402 case DW_FORM_sec_offset:
21403 fprintf_unfiltered (f, "section offset: %s",
21404 pulongest (DW_UNSND (&die->attrs[i])));
21405 break;
21406 case DW_FORM_ref_sig8:
21407 fprintf_unfiltered (f, "signature: %s",
21408 hex_string (DW_SIGNATURE (&die->attrs[i])));
21409 break;
21410 case DW_FORM_string:
21411 case DW_FORM_strp:
21412 case DW_FORM_line_strp:
21413 case DW_FORM_strx:
21414 case DW_FORM_GNU_str_index:
21415 case DW_FORM_GNU_strp_alt:
21416 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21417 DW_STRING (&die->attrs[i])
21418 ? DW_STRING (&die->attrs[i]) : "",
21419 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21420 break;
21421 case DW_FORM_flag:
21422 if (DW_UNSND (&die->attrs[i]))
21423 fprintf_unfiltered (f, "flag: TRUE");
21424 else
21425 fprintf_unfiltered (f, "flag: FALSE");
21426 break;
21427 case DW_FORM_flag_present:
21428 fprintf_unfiltered (f, "flag: TRUE");
21429 break;
21430 case DW_FORM_indirect:
21431 /* The reader will have reduced the indirect form to
21432 the "base form" so this form should not occur. */
21433 fprintf_unfiltered (f,
21434 "unexpected attribute form: DW_FORM_indirect");
21435 break;
21436 case DW_FORM_implicit_const:
21437 fprintf_unfiltered (f, "constant: %s",
21438 plongest (DW_SND (&die->attrs[i])));
21439 break;
21440 default:
21441 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21442 die->attrs[i].form);
21443 break;
21444 }
21445 fprintf_unfiltered (f, "\n");
21446 }
21447 }
21448
21449 static void
21450 dump_die_for_error (struct die_info *die)
21451 {
21452 dump_die_shallow (gdb_stderr, 0, die);
21453 }
21454
21455 static void
21456 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21457 {
21458 int indent = level * 4;
21459
21460 gdb_assert (die != NULL);
21461
21462 if (level >= max_level)
21463 return;
21464
21465 dump_die_shallow (f, indent, die);
21466
21467 if (die->child != NULL)
21468 {
21469 print_spaces (indent, f);
21470 fprintf_unfiltered (f, " Children:");
21471 if (level + 1 < max_level)
21472 {
21473 fprintf_unfiltered (f, "\n");
21474 dump_die_1 (f, level + 1, max_level, die->child);
21475 }
21476 else
21477 {
21478 fprintf_unfiltered (f,
21479 " [not printed, max nesting level reached]\n");
21480 }
21481 }
21482
21483 if (die->sibling != NULL && level > 0)
21484 {
21485 dump_die_1 (f, level, max_level, die->sibling);
21486 }
21487 }
21488
21489 /* This is called from the pdie macro in gdbinit.in.
21490 It's not static so gcc will keep a copy callable from gdb. */
21491
21492 void
21493 dump_die (struct die_info *die, int max_level)
21494 {
21495 dump_die_1 (gdb_stdlog, 0, max_level, die);
21496 }
21497
21498 static void
21499 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21500 {
21501 void **slot;
21502
21503 slot = htab_find_slot_with_hash (cu->die_hash, die,
21504 to_underlying (die->sect_off),
21505 INSERT);
21506
21507 *slot = die;
21508 }
21509
21510 /* Follow reference or signature attribute ATTR of SRC_DIE.
21511 On entry *REF_CU is the CU of SRC_DIE.
21512 On exit *REF_CU is the CU of the result. */
21513
21514 static struct die_info *
21515 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21516 struct dwarf2_cu **ref_cu)
21517 {
21518 struct die_info *die;
21519
21520 if (attr->form_is_ref ())
21521 die = follow_die_ref (src_die, attr, ref_cu);
21522 else if (attr->form == DW_FORM_ref_sig8)
21523 die = follow_die_sig (src_die, attr, ref_cu);
21524 else
21525 {
21526 dump_die_for_error (src_die);
21527 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21528 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21529 }
21530
21531 return die;
21532 }
21533
21534 /* Follow reference OFFSET.
21535 On entry *REF_CU is the CU of the source die referencing OFFSET.
21536 On exit *REF_CU is the CU of the result.
21537 Returns NULL if OFFSET is invalid. */
21538
21539 static struct die_info *
21540 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21541 struct dwarf2_cu **ref_cu)
21542 {
21543 struct die_info temp_die;
21544 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21545 struct dwarf2_per_objfile *dwarf2_per_objfile
21546 = cu->per_cu->dwarf2_per_objfile;
21547
21548 gdb_assert (cu->per_cu != NULL);
21549
21550 target_cu = cu;
21551
21552 if (cu->per_cu->is_debug_types)
21553 {
21554 /* .debug_types CUs cannot reference anything outside their CU.
21555 If they need to, they have to reference a signatured type via
21556 DW_FORM_ref_sig8. */
21557 if (!cu->header.offset_in_cu_p (sect_off))
21558 return NULL;
21559 }
21560 else if (offset_in_dwz != cu->per_cu->is_dwz
21561 || !cu->header.offset_in_cu_p (sect_off))
21562 {
21563 struct dwarf2_per_cu_data *per_cu;
21564
21565 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21566 dwarf2_per_objfile);
21567
21568 /* If necessary, add it to the queue and load its DIEs. */
21569 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21570 load_full_comp_unit (per_cu, false, cu->language);
21571
21572 target_cu = per_cu->cu;
21573 }
21574 else if (cu->dies == NULL)
21575 {
21576 /* We're loading full DIEs during partial symbol reading. */
21577 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21578 load_full_comp_unit (cu->per_cu, false, language_minimal);
21579 }
21580
21581 *ref_cu = target_cu;
21582 temp_die.sect_off = sect_off;
21583
21584 if (target_cu != cu)
21585 target_cu->ancestor = cu;
21586
21587 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21588 &temp_die,
21589 to_underlying (sect_off));
21590 }
21591
21592 /* Follow reference attribute ATTR of SRC_DIE.
21593 On entry *REF_CU is the CU of SRC_DIE.
21594 On exit *REF_CU is the CU of the result. */
21595
21596 static struct die_info *
21597 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21598 struct dwarf2_cu **ref_cu)
21599 {
21600 sect_offset sect_off = attr->get_ref_die_offset ();
21601 struct dwarf2_cu *cu = *ref_cu;
21602 struct die_info *die;
21603
21604 die = follow_die_offset (sect_off,
21605 (attr->form == DW_FORM_GNU_ref_alt
21606 || cu->per_cu->is_dwz),
21607 ref_cu);
21608 if (!die)
21609 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21610 "at %s [in module %s]"),
21611 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21612 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21613
21614 return die;
21615 }
21616
21617 /* See read.h. */
21618
21619 struct dwarf2_locexpr_baton
21620 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21621 dwarf2_per_cu_data *per_cu,
21622 CORE_ADDR (*get_frame_pc) (void *baton),
21623 void *baton, bool resolve_abstract_p)
21624 {
21625 struct dwarf2_cu *cu;
21626 struct die_info *die;
21627 struct attribute *attr;
21628 struct dwarf2_locexpr_baton retval;
21629 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21630 struct objfile *objfile = dwarf2_per_objfile->objfile;
21631
21632 if (per_cu->cu == NULL)
21633 load_cu (per_cu, false);
21634 cu = per_cu->cu;
21635 if (cu == NULL)
21636 {
21637 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21638 Instead just throw an error, not much else we can do. */
21639 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21640 sect_offset_str (sect_off), objfile_name (objfile));
21641 }
21642
21643 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21644 if (!die)
21645 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21646 sect_offset_str (sect_off), objfile_name (objfile));
21647
21648 attr = dwarf2_attr (die, DW_AT_location, cu);
21649 if (!attr && resolve_abstract_p
21650 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21651 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21652 {
21653 CORE_ADDR pc = (*get_frame_pc) (baton);
21654 CORE_ADDR baseaddr = objfile->text_section_offset ();
21655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21656
21657 for (const auto &cand_off
21658 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21659 {
21660 struct dwarf2_cu *cand_cu = cu;
21661 struct die_info *cand
21662 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21663 if (!cand
21664 || !cand->parent
21665 || cand->parent->tag != DW_TAG_subprogram)
21666 continue;
21667
21668 CORE_ADDR pc_low, pc_high;
21669 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21670 if (pc_low == ((CORE_ADDR) -1))
21671 continue;
21672 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21673 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21674 if (!(pc_low <= pc && pc < pc_high))
21675 continue;
21676
21677 die = cand;
21678 attr = dwarf2_attr (die, DW_AT_location, cu);
21679 break;
21680 }
21681 }
21682
21683 if (!attr)
21684 {
21685 /* DWARF: "If there is no such attribute, then there is no effect.".
21686 DATA is ignored if SIZE is 0. */
21687
21688 retval.data = NULL;
21689 retval.size = 0;
21690 }
21691 else if (attr->form_is_section_offset ())
21692 {
21693 struct dwarf2_loclist_baton loclist_baton;
21694 CORE_ADDR pc = (*get_frame_pc) (baton);
21695 size_t size;
21696
21697 fill_in_loclist_baton (cu, &loclist_baton, attr);
21698
21699 retval.data = dwarf2_find_location_expression (&loclist_baton,
21700 &size, pc);
21701 retval.size = size;
21702 }
21703 else
21704 {
21705 if (!attr->form_is_block ())
21706 error (_("Dwarf Error: DIE at %s referenced in module %s "
21707 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21708 sect_offset_str (sect_off), objfile_name (objfile));
21709
21710 retval.data = DW_BLOCK (attr)->data;
21711 retval.size = DW_BLOCK (attr)->size;
21712 }
21713 retval.per_cu = cu->per_cu;
21714
21715 age_cached_comp_units (dwarf2_per_objfile);
21716
21717 return retval;
21718 }
21719
21720 /* See read.h. */
21721
21722 struct dwarf2_locexpr_baton
21723 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21724 dwarf2_per_cu_data *per_cu,
21725 CORE_ADDR (*get_frame_pc) (void *baton),
21726 void *baton)
21727 {
21728 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21729
21730 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21731 }
21732
21733 /* Write a constant of a given type as target-ordered bytes into
21734 OBSTACK. */
21735
21736 static const gdb_byte *
21737 write_constant_as_bytes (struct obstack *obstack,
21738 enum bfd_endian byte_order,
21739 struct type *type,
21740 ULONGEST value,
21741 LONGEST *len)
21742 {
21743 gdb_byte *result;
21744
21745 *len = TYPE_LENGTH (type);
21746 result = (gdb_byte *) obstack_alloc (obstack, *len);
21747 store_unsigned_integer (result, *len, byte_order, value);
21748
21749 return result;
21750 }
21751
21752 /* See read.h. */
21753
21754 const gdb_byte *
21755 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21756 dwarf2_per_cu_data *per_cu,
21757 obstack *obstack,
21758 LONGEST *len)
21759 {
21760 struct dwarf2_cu *cu;
21761 struct die_info *die;
21762 struct attribute *attr;
21763 const gdb_byte *result = NULL;
21764 struct type *type;
21765 LONGEST value;
21766 enum bfd_endian byte_order;
21767 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21768
21769 if (per_cu->cu == NULL)
21770 load_cu (per_cu, false);
21771 cu = per_cu->cu;
21772 if (cu == NULL)
21773 {
21774 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21775 Instead just throw an error, not much else we can do. */
21776 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21777 sect_offset_str (sect_off), objfile_name (objfile));
21778 }
21779
21780 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21781 if (!die)
21782 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21783 sect_offset_str (sect_off), objfile_name (objfile));
21784
21785 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21786 if (attr == NULL)
21787 return NULL;
21788
21789 byte_order = (bfd_big_endian (objfile->obfd)
21790 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21791
21792 switch (attr->form)
21793 {
21794 case DW_FORM_addr:
21795 case DW_FORM_addrx:
21796 case DW_FORM_GNU_addr_index:
21797 {
21798 gdb_byte *tem;
21799
21800 *len = cu->header.addr_size;
21801 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21802 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21803 result = tem;
21804 }
21805 break;
21806 case DW_FORM_string:
21807 case DW_FORM_strp:
21808 case DW_FORM_strx:
21809 case DW_FORM_GNU_str_index:
21810 case DW_FORM_GNU_strp_alt:
21811 /* DW_STRING is already allocated on the objfile obstack, point
21812 directly to it. */
21813 result = (const gdb_byte *) DW_STRING (attr);
21814 *len = strlen (DW_STRING (attr));
21815 break;
21816 case DW_FORM_block1:
21817 case DW_FORM_block2:
21818 case DW_FORM_block4:
21819 case DW_FORM_block:
21820 case DW_FORM_exprloc:
21821 case DW_FORM_data16:
21822 result = DW_BLOCK (attr)->data;
21823 *len = DW_BLOCK (attr)->size;
21824 break;
21825
21826 /* The DW_AT_const_value attributes are supposed to carry the
21827 symbol's value "represented as it would be on the target
21828 architecture." By the time we get here, it's already been
21829 converted to host endianness, so we just need to sign- or
21830 zero-extend it as appropriate. */
21831 case DW_FORM_data1:
21832 type = die_type (die, cu);
21833 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21834 if (result == NULL)
21835 result = write_constant_as_bytes (obstack, byte_order,
21836 type, value, len);
21837 break;
21838 case DW_FORM_data2:
21839 type = die_type (die, cu);
21840 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21841 if (result == NULL)
21842 result = write_constant_as_bytes (obstack, byte_order,
21843 type, value, len);
21844 break;
21845 case DW_FORM_data4:
21846 type = die_type (die, cu);
21847 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21848 if (result == NULL)
21849 result = write_constant_as_bytes (obstack, byte_order,
21850 type, value, len);
21851 break;
21852 case DW_FORM_data8:
21853 type = die_type (die, cu);
21854 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21855 if (result == NULL)
21856 result = write_constant_as_bytes (obstack, byte_order,
21857 type, value, len);
21858 break;
21859
21860 case DW_FORM_sdata:
21861 case DW_FORM_implicit_const:
21862 type = die_type (die, cu);
21863 result = write_constant_as_bytes (obstack, byte_order,
21864 type, DW_SND (attr), len);
21865 break;
21866
21867 case DW_FORM_udata:
21868 type = die_type (die, cu);
21869 result = write_constant_as_bytes (obstack, byte_order,
21870 type, DW_UNSND (attr), len);
21871 break;
21872
21873 default:
21874 complaint (_("unsupported const value attribute form: '%s'"),
21875 dwarf_form_name (attr->form));
21876 break;
21877 }
21878
21879 return result;
21880 }
21881
21882 /* See read.h. */
21883
21884 struct type *
21885 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21886 dwarf2_per_cu_data *per_cu)
21887 {
21888 struct dwarf2_cu *cu;
21889 struct die_info *die;
21890
21891 if (per_cu->cu == NULL)
21892 load_cu (per_cu, false);
21893 cu = per_cu->cu;
21894 if (!cu)
21895 return NULL;
21896
21897 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21898 if (!die)
21899 return NULL;
21900
21901 return die_type (die, cu);
21902 }
21903
21904 /* See read.h. */
21905
21906 struct type *
21907 dwarf2_get_die_type (cu_offset die_offset,
21908 struct dwarf2_per_cu_data *per_cu)
21909 {
21910 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21911 return get_die_type_at_offset (die_offset_sect, per_cu);
21912 }
21913
21914 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21915 On entry *REF_CU is the CU of SRC_DIE.
21916 On exit *REF_CU is the CU of the result.
21917 Returns NULL if the referenced DIE isn't found. */
21918
21919 static struct die_info *
21920 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21921 struct dwarf2_cu **ref_cu)
21922 {
21923 struct die_info temp_die;
21924 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
21925 struct die_info *die;
21926
21927 /* While it might be nice to assert sig_type->type == NULL here,
21928 we can get here for DW_AT_imported_declaration where we need
21929 the DIE not the type. */
21930
21931 /* If necessary, add it to the queue and load its DIEs. */
21932
21933 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21934 read_signatured_type (sig_type);
21935
21936 sig_cu = sig_type->per_cu.cu;
21937 gdb_assert (sig_cu != NULL);
21938 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21939 temp_die.sect_off = sig_type->type_offset_in_section;
21940 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21941 to_underlying (temp_die.sect_off));
21942 if (die)
21943 {
21944 struct dwarf2_per_objfile *dwarf2_per_objfile
21945 = (*ref_cu)->per_cu->dwarf2_per_objfile;
21946
21947 /* For .gdb_index version 7 keep track of included TUs.
21948 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21949 if (dwarf2_per_objfile->index_table != NULL
21950 && dwarf2_per_objfile->index_table->version <= 7)
21951 {
21952 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
21953 }
21954
21955 *ref_cu = sig_cu;
21956 if (sig_cu != cu)
21957 sig_cu->ancestor = cu;
21958
21959 return die;
21960 }
21961
21962 return NULL;
21963 }
21964
21965 /* Follow signatured type referenced by ATTR in SRC_DIE.
21966 On entry *REF_CU is the CU of SRC_DIE.
21967 On exit *REF_CU is the CU of the result.
21968 The result is the DIE of the type.
21969 If the referenced type cannot be found an error is thrown. */
21970
21971 static struct die_info *
21972 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21973 struct dwarf2_cu **ref_cu)
21974 {
21975 ULONGEST signature = DW_SIGNATURE (attr);
21976 struct signatured_type *sig_type;
21977 struct die_info *die;
21978
21979 gdb_assert (attr->form == DW_FORM_ref_sig8);
21980
21981 sig_type = lookup_signatured_type (*ref_cu, signature);
21982 /* sig_type will be NULL if the signatured type is missing from
21983 the debug info. */
21984 if (sig_type == NULL)
21985 {
21986 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21987 " from DIE at %s [in module %s]"),
21988 hex_string (signature), sect_offset_str (src_die->sect_off),
21989 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21990 }
21991
21992 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21993 if (die == NULL)
21994 {
21995 dump_die_for_error (src_die);
21996 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21997 " from DIE at %s [in module %s]"),
21998 hex_string (signature), sect_offset_str (src_die->sect_off),
21999 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22000 }
22001
22002 return die;
22003 }
22004
22005 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22006 reading in and processing the type unit if necessary. */
22007
22008 static struct type *
22009 get_signatured_type (struct die_info *die, ULONGEST signature,
22010 struct dwarf2_cu *cu)
22011 {
22012 struct dwarf2_per_objfile *dwarf2_per_objfile
22013 = cu->per_cu->dwarf2_per_objfile;
22014 struct signatured_type *sig_type;
22015 struct dwarf2_cu *type_cu;
22016 struct die_info *type_die;
22017 struct type *type;
22018
22019 sig_type = lookup_signatured_type (cu, signature);
22020 /* sig_type will be NULL if the signatured type is missing from
22021 the debug info. */
22022 if (sig_type == NULL)
22023 {
22024 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22025 " from DIE at %s [in module %s]"),
22026 hex_string (signature), sect_offset_str (die->sect_off),
22027 objfile_name (dwarf2_per_objfile->objfile));
22028 return build_error_marker_type (cu, die);
22029 }
22030
22031 /* If we already know the type we're done. */
22032 if (sig_type->type != NULL)
22033 return sig_type->type;
22034
22035 type_cu = cu;
22036 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22037 if (type_die != NULL)
22038 {
22039 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22040 is created. This is important, for example, because for c++ classes
22041 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22042 type = read_type_die (type_die, type_cu);
22043 if (type == NULL)
22044 {
22045 complaint (_("Dwarf Error: Cannot build signatured type %s"
22046 " referenced from DIE at %s [in module %s]"),
22047 hex_string (signature), sect_offset_str (die->sect_off),
22048 objfile_name (dwarf2_per_objfile->objfile));
22049 type = build_error_marker_type (cu, die);
22050 }
22051 }
22052 else
22053 {
22054 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22055 " from DIE at %s [in module %s]"),
22056 hex_string (signature), sect_offset_str (die->sect_off),
22057 objfile_name (dwarf2_per_objfile->objfile));
22058 type = build_error_marker_type (cu, die);
22059 }
22060 sig_type->type = type;
22061
22062 return type;
22063 }
22064
22065 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22066 reading in and processing the type unit if necessary. */
22067
22068 static struct type *
22069 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22070 struct dwarf2_cu *cu) /* ARI: editCase function */
22071 {
22072 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22073 if (attr->form_is_ref ())
22074 {
22075 struct dwarf2_cu *type_cu = cu;
22076 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22077
22078 return read_type_die (type_die, type_cu);
22079 }
22080 else if (attr->form == DW_FORM_ref_sig8)
22081 {
22082 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22083 }
22084 else
22085 {
22086 struct dwarf2_per_objfile *dwarf2_per_objfile
22087 = cu->per_cu->dwarf2_per_objfile;
22088
22089 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22090 " at %s [in module %s]"),
22091 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22092 objfile_name (dwarf2_per_objfile->objfile));
22093 return build_error_marker_type (cu, die);
22094 }
22095 }
22096
22097 /* Load the DIEs associated with type unit PER_CU into memory. */
22098
22099 static void
22100 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22101 {
22102 struct signatured_type *sig_type;
22103
22104 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22105 gdb_assert (! per_cu->type_unit_group_p ());
22106
22107 /* We have the per_cu, but we need the signatured_type.
22108 Fortunately this is an easy translation. */
22109 gdb_assert (per_cu->is_debug_types);
22110 sig_type = (struct signatured_type *) per_cu;
22111
22112 gdb_assert (per_cu->cu == NULL);
22113
22114 read_signatured_type (sig_type);
22115
22116 gdb_assert (per_cu->cu != NULL);
22117 }
22118
22119 /* Read in a signatured type and build its CU and DIEs.
22120 If the type is a stub for the real type in a DWO file,
22121 read in the real type from the DWO file as well. */
22122
22123 static void
22124 read_signatured_type (struct signatured_type *sig_type)
22125 {
22126 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22127
22128 gdb_assert (per_cu->is_debug_types);
22129 gdb_assert (per_cu->cu == NULL);
22130
22131 cutu_reader reader (per_cu, NULL, 0, false);
22132
22133 if (!reader.dummy_p)
22134 {
22135 struct dwarf2_cu *cu = reader.cu;
22136 const gdb_byte *info_ptr = reader.info_ptr;
22137
22138 gdb_assert (cu->die_hash == NULL);
22139 cu->die_hash =
22140 htab_create_alloc_ex (cu->header.length / 12,
22141 die_hash,
22142 die_eq,
22143 NULL,
22144 &cu->comp_unit_obstack,
22145 hashtab_obstack_allocate,
22146 dummy_obstack_deallocate);
22147
22148 if (reader.comp_unit_die->has_children)
22149 reader.comp_unit_die->child
22150 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22151 reader.comp_unit_die);
22152 cu->dies = reader.comp_unit_die;
22153 /* comp_unit_die is not stored in die_hash, no need. */
22154
22155 /* We try not to read any attributes in this function, because
22156 not all CUs needed for references have been loaded yet, and
22157 symbol table processing isn't initialized. But we have to
22158 set the CU language, or we won't be able to build types
22159 correctly. Similarly, if we do not read the producer, we can
22160 not apply producer-specific interpretation. */
22161 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22162
22163 reader.keep ();
22164 }
22165
22166 sig_type->per_cu.tu_read = 1;
22167 }
22168
22169 /* Decode simple location descriptions.
22170 Given a pointer to a dwarf block that defines a location, compute
22171 the location and return the value.
22172
22173 NOTE drow/2003-11-18: This function is called in two situations
22174 now: for the address of static or global variables (partial symbols
22175 only) and for offsets into structures which are expected to be
22176 (more or less) constant. The partial symbol case should go away,
22177 and only the constant case should remain. That will let this
22178 function complain more accurately. A few special modes are allowed
22179 without complaint for global variables (for instance, global
22180 register values and thread-local values).
22181
22182 A location description containing no operations indicates that the
22183 object is optimized out. The return value is 0 for that case.
22184 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22185 callers will only want a very basic result and this can become a
22186 complaint.
22187
22188 Note that stack[0] is unused except as a default error return. */
22189
22190 static CORE_ADDR
22191 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22192 {
22193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22194 size_t i;
22195 size_t size = blk->size;
22196 const gdb_byte *data = blk->data;
22197 CORE_ADDR stack[64];
22198 int stacki;
22199 unsigned int bytes_read, unsnd;
22200 gdb_byte op;
22201
22202 i = 0;
22203 stacki = 0;
22204 stack[stacki] = 0;
22205 stack[++stacki] = 0;
22206
22207 while (i < size)
22208 {
22209 op = data[i++];
22210 switch (op)
22211 {
22212 case DW_OP_lit0:
22213 case DW_OP_lit1:
22214 case DW_OP_lit2:
22215 case DW_OP_lit3:
22216 case DW_OP_lit4:
22217 case DW_OP_lit5:
22218 case DW_OP_lit6:
22219 case DW_OP_lit7:
22220 case DW_OP_lit8:
22221 case DW_OP_lit9:
22222 case DW_OP_lit10:
22223 case DW_OP_lit11:
22224 case DW_OP_lit12:
22225 case DW_OP_lit13:
22226 case DW_OP_lit14:
22227 case DW_OP_lit15:
22228 case DW_OP_lit16:
22229 case DW_OP_lit17:
22230 case DW_OP_lit18:
22231 case DW_OP_lit19:
22232 case DW_OP_lit20:
22233 case DW_OP_lit21:
22234 case DW_OP_lit22:
22235 case DW_OP_lit23:
22236 case DW_OP_lit24:
22237 case DW_OP_lit25:
22238 case DW_OP_lit26:
22239 case DW_OP_lit27:
22240 case DW_OP_lit28:
22241 case DW_OP_lit29:
22242 case DW_OP_lit30:
22243 case DW_OP_lit31:
22244 stack[++stacki] = op - DW_OP_lit0;
22245 break;
22246
22247 case DW_OP_reg0:
22248 case DW_OP_reg1:
22249 case DW_OP_reg2:
22250 case DW_OP_reg3:
22251 case DW_OP_reg4:
22252 case DW_OP_reg5:
22253 case DW_OP_reg6:
22254 case DW_OP_reg7:
22255 case DW_OP_reg8:
22256 case DW_OP_reg9:
22257 case DW_OP_reg10:
22258 case DW_OP_reg11:
22259 case DW_OP_reg12:
22260 case DW_OP_reg13:
22261 case DW_OP_reg14:
22262 case DW_OP_reg15:
22263 case DW_OP_reg16:
22264 case DW_OP_reg17:
22265 case DW_OP_reg18:
22266 case DW_OP_reg19:
22267 case DW_OP_reg20:
22268 case DW_OP_reg21:
22269 case DW_OP_reg22:
22270 case DW_OP_reg23:
22271 case DW_OP_reg24:
22272 case DW_OP_reg25:
22273 case DW_OP_reg26:
22274 case DW_OP_reg27:
22275 case DW_OP_reg28:
22276 case DW_OP_reg29:
22277 case DW_OP_reg30:
22278 case DW_OP_reg31:
22279 stack[++stacki] = op - DW_OP_reg0;
22280 if (i < size)
22281 dwarf2_complex_location_expr_complaint ();
22282 break;
22283
22284 case DW_OP_regx:
22285 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22286 i += bytes_read;
22287 stack[++stacki] = unsnd;
22288 if (i < size)
22289 dwarf2_complex_location_expr_complaint ();
22290 break;
22291
22292 case DW_OP_addr:
22293 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22294 &bytes_read);
22295 i += bytes_read;
22296 break;
22297
22298 case DW_OP_const1u:
22299 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22300 i += 1;
22301 break;
22302
22303 case DW_OP_const1s:
22304 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22305 i += 1;
22306 break;
22307
22308 case DW_OP_const2u:
22309 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22310 i += 2;
22311 break;
22312
22313 case DW_OP_const2s:
22314 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22315 i += 2;
22316 break;
22317
22318 case DW_OP_const4u:
22319 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22320 i += 4;
22321 break;
22322
22323 case DW_OP_const4s:
22324 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22325 i += 4;
22326 break;
22327
22328 case DW_OP_const8u:
22329 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22330 i += 8;
22331 break;
22332
22333 case DW_OP_constu:
22334 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22335 &bytes_read);
22336 i += bytes_read;
22337 break;
22338
22339 case DW_OP_consts:
22340 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22341 i += bytes_read;
22342 break;
22343
22344 case DW_OP_dup:
22345 stack[stacki + 1] = stack[stacki];
22346 stacki++;
22347 break;
22348
22349 case DW_OP_plus:
22350 stack[stacki - 1] += stack[stacki];
22351 stacki--;
22352 break;
22353
22354 case DW_OP_plus_uconst:
22355 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22356 &bytes_read);
22357 i += bytes_read;
22358 break;
22359
22360 case DW_OP_minus:
22361 stack[stacki - 1] -= stack[stacki];
22362 stacki--;
22363 break;
22364
22365 case DW_OP_deref:
22366 /* If we're not the last op, then we definitely can't encode
22367 this using GDB's address_class enum. This is valid for partial
22368 global symbols, although the variable's address will be bogus
22369 in the psymtab. */
22370 if (i < size)
22371 dwarf2_complex_location_expr_complaint ();
22372 break;
22373
22374 case DW_OP_GNU_push_tls_address:
22375 case DW_OP_form_tls_address:
22376 /* The top of the stack has the offset from the beginning
22377 of the thread control block at which the variable is located. */
22378 /* Nothing should follow this operator, so the top of stack would
22379 be returned. */
22380 /* This is valid for partial global symbols, but the variable's
22381 address will be bogus in the psymtab. Make it always at least
22382 non-zero to not look as a variable garbage collected by linker
22383 which have DW_OP_addr 0. */
22384 if (i < size)
22385 dwarf2_complex_location_expr_complaint ();
22386 stack[stacki]++;
22387 break;
22388
22389 case DW_OP_GNU_uninit:
22390 break;
22391
22392 case DW_OP_addrx:
22393 case DW_OP_GNU_addr_index:
22394 case DW_OP_GNU_const_index:
22395 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22396 &bytes_read);
22397 i += bytes_read;
22398 break;
22399
22400 default:
22401 {
22402 const char *name = get_DW_OP_name (op);
22403
22404 if (name)
22405 complaint (_("unsupported stack op: '%s'"),
22406 name);
22407 else
22408 complaint (_("unsupported stack op: '%02x'"),
22409 op);
22410 }
22411
22412 return (stack[stacki]);
22413 }
22414
22415 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22416 outside of the allocated space. Also enforce minimum>0. */
22417 if (stacki >= ARRAY_SIZE (stack) - 1)
22418 {
22419 complaint (_("location description stack overflow"));
22420 return 0;
22421 }
22422
22423 if (stacki <= 0)
22424 {
22425 complaint (_("location description stack underflow"));
22426 return 0;
22427 }
22428 }
22429 return (stack[stacki]);
22430 }
22431
22432 /* memory allocation interface */
22433
22434 static struct dwarf_block *
22435 dwarf_alloc_block (struct dwarf2_cu *cu)
22436 {
22437 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22438 }
22439
22440 static struct die_info *
22441 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22442 {
22443 struct die_info *die;
22444 size_t size = sizeof (struct die_info);
22445
22446 if (num_attrs > 1)
22447 size += (num_attrs - 1) * sizeof (struct attribute);
22448
22449 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22450 memset (die, 0, sizeof (struct die_info));
22451 return (die);
22452 }
22453
22454 \f
22455
22456 /* Macro support. */
22457
22458 /* An overload of dwarf_decode_macros that finds the correct section
22459 and ensures it is read in before calling the other overload. */
22460
22461 static void
22462 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22463 int section_is_gnu)
22464 {
22465 struct dwarf2_per_objfile *dwarf2_per_objfile
22466 = cu->per_cu->dwarf2_per_objfile;
22467 struct objfile *objfile = dwarf2_per_objfile->objfile;
22468 const struct line_header *lh = cu->line_header;
22469 unsigned int offset_size = cu->header.offset_size;
22470 struct dwarf2_section_info *section;
22471 const char *section_name;
22472
22473 if (cu->dwo_unit != nullptr)
22474 {
22475 if (section_is_gnu)
22476 {
22477 section = &cu->dwo_unit->dwo_file->sections.macro;
22478 section_name = ".debug_macro.dwo";
22479 }
22480 else
22481 {
22482 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22483 section_name = ".debug_macinfo.dwo";
22484 }
22485 }
22486 else
22487 {
22488 if (section_is_gnu)
22489 {
22490 section = &dwarf2_per_objfile->macro;
22491 section_name = ".debug_macro";
22492 }
22493 else
22494 {
22495 section = &dwarf2_per_objfile->macinfo;
22496 section_name = ".debug_macinfo";
22497 }
22498 }
22499
22500 section->read (objfile);
22501 if (section->buffer == nullptr)
22502 {
22503 complaint (_("missing %s section"), section_name);
22504 return;
22505 }
22506
22507 buildsym_compunit *builder = cu->get_builder ();
22508
22509 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22510 offset_size, offset, section_is_gnu);
22511 }
22512
22513 /* Return the .debug_loc section to use for CU.
22514 For DWO files use .debug_loc.dwo. */
22515
22516 static struct dwarf2_section_info *
22517 cu_debug_loc_section (struct dwarf2_cu *cu)
22518 {
22519 struct dwarf2_per_objfile *dwarf2_per_objfile
22520 = cu->per_cu->dwarf2_per_objfile;
22521
22522 if (cu->dwo_unit)
22523 {
22524 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22525
22526 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22527 }
22528 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22529 : &dwarf2_per_objfile->loc);
22530 }
22531
22532 /* A helper function that fills in a dwarf2_loclist_baton. */
22533
22534 static void
22535 fill_in_loclist_baton (struct dwarf2_cu *cu,
22536 struct dwarf2_loclist_baton *baton,
22537 const struct attribute *attr)
22538 {
22539 struct dwarf2_per_objfile *dwarf2_per_objfile
22540 = cu->per_cu->dwarf2_per_objfile;
22541 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22542
22543 section->read (dwarf2_per_objfile->objfile);
22544
22545 baton->per_cu = cu->per_cu;
22546 gdb_assert (baton->per_cu);
22547 /* We don't know how long the location list is, but make sure we
22548 don't run off the edge of the section. */
22549 baton->size = section->size - DW_UNSND (attr);
22550 baton->data = section->buffer + DW_UNSND (attr);
22551 if (cu->base_address.has_value ())
22552 baton->base_address = *cu->base_address;
22553 else
22554 baton->base_address = 0;
22555 baton->from_dwo = cu->dwo_unit != NULL;
22556 }
22557
22558 static void
22559 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22560 struct dwarf2_cu *cu, int is_block)
22561 {
22562 struct dwarf2_per_objfile *dwarf2_per_objfile
22563 = cu->per_cu->dwarf2_per_objfile;
22564 struct objfile *objfile = dwarf2_per_objfile->objfile;
22565 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22566
22567 if (attr->form_is_section_offset ()
22568 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22569 the section. If so, fall through to the complaint in the
22570 other branch. */
22571 && DW_UNSND (attr) < section->get_size (objfile))
22572 {
22573 struct dwarf2_loclist_baton *baton;
22574
22575 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22576
22577 fill_in_loclist_baton (cu, baton, attr);
22578
22579 if (!cu->base_address.has_value ())
22580 complaint (_("Location list used without "
22581 "specifying the CU base address."));
22582
22583 SYMBOL_ACLASS_INDEX (sym) = (is_block
22584 ? dwarf2_loclist_block_index
22585 : dwarf2_loclist_index);
22586 SYMBOL_LOCATION_BATON (sym) = baton;
22587 }
22588 else
22589 {
22590 struct dwarf2_locexpr_baton *baton;
22591
22592 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22593 baton->per_cu = cu->per_cu;
22594 gdb_assert (baton->per_cu);
22595
22596 if (attr->form_is_block ())
22597 {
22598 /* Note that we're just copying the block's data pointer
22599 here, not the actual data. We're still pointing into the
22600 info_buffer for SYM's objfile; right now we never release
22601 that buffer, but when we do clean up properly this may
22602 need to change. */
22603 baton->size = DW_BLOCK (attr)->size;
22604 baton->data = DW_BLOCK (attr)->data;
22605 }
22606 else
22607 {
22608 dwarf2_invalid_attrib_class_complaint ("location description",
22609 sym->natural_name ());
22610 baton->size = 0;
22611 }
22612
22613 SYMBOL_ACLASS_INDEX (sym) = (is_block
22614 ? dwarf2_locexpr_block_index
22615 : dwarf2_locexpr_index);
22616 SYMBOL_LOCATION_BATON (sym) = baton;
22617 }
22618 }
22619
22620 /* See read.h. */
22621
22622 struct objfile *
22623 dwarf2_per_cu_data::objfile () const
22624 {
22625 struct objfile *objfile = dwarf2_per_objfile->objfile;
22626
22627 /* Return the master objfile, so that we can report and look up the
22628 correct file containing this variable. */
22629 if (objfile->separate_debug_objfile_backlink)
22630 objfile = objfile->separate_debug_objfile_backlink;
22631
22632 return objfile;
22633 }
22634
22635 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22636 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22637 CU_HEADERP first. */
22638
22639 static const struct comp_unit_head *
22640 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22641 const struct dwarf2_per_cu_data *per_cu)
22642 {
22643 const gdb_byte *info_ptr;
22644
22645 if (per_cu->cu)
22646 return &per_cu->cu->header;
22647
22648 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22649
22650 memset (cu_headerp, 0, sizeof (*cu_headerp));
22651 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22652 rcuh_kind::COMPILE);
22653
22654 return cu_headerp;
22655 }
22656
22657 /* See read.h. */
22658
22659 int
22660 dwarf2_per_cu_data::addr_size () const
22661 {
22662 struct comp_unit_head cu_header_local;
22663 const struct comp_unit_head *cu_headerp;
22664
22665 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22666
22667 return cu_headerp->addr_size;
22668 }
22669
22670 /* See read.h. */
22671
22672 int
22673 dwarf2_per_cu_data::offset_size () const
22674 {
22675 struct comp_unit_head cu_header_local;
22676 const struct comp_unit_head *cu_headerp;
22677
22678 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22679
22680 return cu_headerp->offset_size;
22681 }
22682
22683 /* See read.h. */
22684
22685 int
22686 dwarf2_per_cu_data::ref_addr_size () const
22687 {
22688 struct comp_unit_head cu_header_local;
22689 const struct comp_unit_head *cu_headerp;
22690
22691 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22692
22693 if (cu_headerp->version == 2)
22694 return cu_headerp->addr_size;
22695 else
22696 return cu_headerp->offset_size;
22697 }
22698
22699 /* See read.h. */
22700
22701 CORE_ADDR
22702 dwarf2_per_cu_data::text_offset () const
22703 {
22704 struct objfile *objfile = dwarf2_per_objfile->objfile;
22705
22706 return objfile->text_section_offset ();
22707 }
22708
22709 /* See read.h. */
22710
22711 struct type *
22712 dwarf2_per_cu_data::addr_type () const
22713 {
22714 struct objfile *objfile = dwarf2_per_objfile->objfile;
22715 struct type *void_type = objfile_type (objfile)->builtin_void;
22716 struct type *addr_type = lookup_pointer_type (void_type);
22717 int addr_size = this->addr_size ();
22718
22719 if (TYPE_LENGTH (addr_type) == addr_size)
22720 return addr_type;
22721
22722 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22723 return addr_type;
22724 }
22725
22726 /* A helper function for dwarf2_find_containing_comp_unit that returns
22727 the index of the result, and that searches a vector. It will
22728 return a result even if the offset in question does not actually
22729 occur in any CU. This is separate so that it can be unit
22730 tested. */
22731
22732 static int
22733 dwarf2_find_containing_comp_unit
22734 (sect_offset sect_off,
22735 unsigned int offset_in_dwz,
22736 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22737 {
22738 int low, high;
22739
22740 low = 0;
22741 high = all_comp_units.size () - 1;
22742 while (high > low)
22743 {
22744 struct dwarf2_per_cu_data *mid_cu;
22745 int mid = low + (high - low) / 2;
22746
22747 mid_cu = all_comp_units[mid];
22748 if (mid_cu->is_dwz > offset_in_dwz
22749 || (mid_cu->is_dwz == offset_in_dwz
22750 && mid_cu->sect_off + mid_cu->length > sect_off))
22751 high = mid;
22752 else
22753 low = mid + 1;
22754 }
22755 gdb_assert (low == high);
22756 return low;
22757 }
22758
22759 /* Locate the .debug_info compilation unit from CU's objfile which contains
22760 the DIE at OFFSET. Raises an error on failure. */
22761
22762 static struct dwarf2_per_cu_data *
22763 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22764 unsigned int offset_in_dwz,
22765 struct dwarf2_per_objfile *dwarf2_per_objfile)
22766 {
22767 int low
22768 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22769 dwarf2_per_objfile->all_comp_units);
22770 struct dwarf2_per_cu_data *this_cu
22771 = dwarf2_per_objfile->all_comp_units[low];
22772
22773 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22774 {
22775 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22776 error (_("Dwarf Error: could not find partial DIE containing "
22777 "offset %s [in module %s]"),
22778 sect_offset_str (sect_off),
22779 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22780
22781 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22782 <= sect_off);
22783 return dwarf2_per_objfile->all_comp_units[low-1];
22784 }
22785 else
22786 {
22787 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22788 && sect_off >= this_cu->sect_off + this_cu->length)
22789 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22790 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22791 return this_cu;
22792 }
22793 }
22794
22795 #if GDB_SELF_TEST
22796
22797 namespace selftests {
22798 namespace find_containing_comp_unit {
22799
22800 static void
22801 run_test ()
22802 {
22803 struct dwarf2_per_cu_data one {};
22804 struct dwarf2_per_cu_data two {};
22805 struct dwarf2_per_cu_data three {};
22806 struct dwarf2_per_cu_data four {};
22807
22808 one.length = 5;
22809 two.sect_off = sect_offset (one.length);
22810 two.length = 7;
22811
22812 three.length = 5;
22813 three.is_dwz = 1;
22814 four.sect_off = sect_offset (three.length);
22815 four.length = 7;
22816 four.is_dwz = 1;
22817
22818 std::vector<dwarf2_per_cu_data *> units;
22819 units.push_back (&one);
22820 units.push_back (&two);
22821 units.push_back (&three);
22822 units.push_back (&four);
22823
22824 int result;
22825
22826 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22827 SELF_CHECK (units[result] == &one);
22828 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22829 SELF_CHECK (units[result] == &one);
22830 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22831 SELF_CHECK (units[result] == &two);
22832
22833 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22834 SELF_CHECK (units[result] == &three);
22835 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22836 SELF_CHECK (units[result] == &three);
22837 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22838 SELF_CHECK (units[result] == &four);
22839 }
22840
22841 }
22842 }
22843
22844 #endif /* GDB_SELF_TEST */
22845
22846 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22847
22848 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22849 : per_cu (per_cu_),
22850 mark (false),
22851 has_loclist (false),
22852 checked_producer (false),
22853 producer_is_gxx_lt_4_6 (false),
22854 producer_is_gcc_lt_4_3 (false),
22855 producer_is_icc (false),
22856 producer_is_icc_lt_14 (false),
22857 producer_is_codewarrior (false),
22858 processing_has_namespace_info (false)
22859 {
22860 per_cu->cu = this;
22861 }
22862
22863 /* Destroy a dwarf2_cu. */
22864
22865 dwarf2_cu::~dwarf2_cu ()
22866 {
22867 per_cu->cu = NULL;
22868 }
22869
22870 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22871
22872 static void
22873 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22874 enum language pretend_language)
22875 {
22876 struct attribute *attr;
22877
22878 /* Set the language we're debugging. */
22879 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22880 if (attr != nullptr)
22881 set_cu_language (DW_UNSND (attr), cu);
22882 else
22883 {
22884 cu->language = pretend_language;
22885 cu->language_defn = language_def (cu->language);
22886 }
22887
22888 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22889 }
22890
22891 /* Increase the age counter on each cached compilation unit, and free
22892 any that are too old. */
22893
22894 static void
22895 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
22896 {
22897 struct dwarf2_per_cu_data *per_cu, **last_chain;
22898
22899 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22900 per_cu = dwarf2_per_objfile->read_in_chain;
22901 while (per_cu != NULL)
22902 {
22903 per_cu->cu->last_used ++;
22904 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22905 dwarf2_mark (per_cu->cu);
22906 per_cu = per_cu->cu->read_in_chain;
22907 }
22908
22909 per_cu = dwarf2_per_objfile->read_in_chain;
22910 last_chain = &dwarf2_per_objfile->read_in_chain;
22911 while (per_cu != NULL)
22912 {
22913 struct dwarf2_per_cu_data *next_cu;
22914
22915 next_cu = per_cu->cu->read_in_chain;
22916
22917 if (!per_cu->cu->mark)
22918 {
22919 delete per_cu->cu;
22920 *last_chain = next_cu;
22921 }
22922 else
22923 last_chain = &per_cu->cu->read_in_chain;
22924
22925 per_cu = next_cu;
22926 }
22927 }
22928
22929 /* Remove a single compilation unit from the cache. */
22930
22931 static void
22932 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22933 {
22934 struct dwarf2_per_cu_data *per_cu, **last_chain;
22935 struct dwarf2_per_objfile *dwarf2_per_objfile
22936 = target_per_cu->dwarf2_per_objfile;
22937
22938 per_cu = dwarf2_per_objfile->read_in_chain;
22939 last_chain = &dwarf2_per_objfile->read_in_chain;
22940 while (per_cu != NULL)
22941 {
22942 struct dwarf2_per_cu_data *next_cu;
22943
22944 next_cu = per_cu->cu->read_in_chain;
22945
22946 if (per_cu == target_per_cu)
22947 {
22948 delete per_cu->cu;
22949 per_cu->cu = NULL;
22950 *last_chain = next_cu;
22951 break;
22952 }
22953 else
22954 last_chain = &per_cu->cu->read_in_chain;
22955
22956 per_cu = next_cu;
22957 }
22958 }
22959
22960 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22961 We store these in a hash table separate from the DIEs, and preserve them
22962 when the DIEs are flushed out of cache.
22963
22964 The CU "per_cu" pointer is needed because offset alone is not enough to
22965 uniquely identify the type. A file may have multiple .debug_types sections,
22966 or the type may come from a DWO file. Furthermore, while it's more logical
22967 to use per_cu->section+offset, with Fission the section with the data is in
22968 the DWO file but we don't know that section at the point we need it.
22969 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22970 because we can enter the lookup routine, get_die_type_at_offset, from
22971 outside this file, and thus won't necessarily have PER_CU->cu.
22972 Fortunately, PER_CU is stable for the life of the objfile. */
22973
22974 struct dwarf2_per_cu_offset_and_type
22975 {
22976 const struct dwarf2_per_cu_data *per_cu;
22977 sect_offset sect_off;
22978 struct type *type;
22979 };
22980
22981 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22982
22983 static hashval_t
22984 per_cu_offset_and_type_hash (const void *item)
22985 {
22986 const struct dwarf2_per_cu_offset_and_type *ofs
22987 = (const struct dwarf2_per_cu_offset_and_type *) item;
22988
22989 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
22990 }
22991
22992 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22993
22994 static int
22995 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22996 {
22997 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22998 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22999 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23000 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23001
23002 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23003 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23004 }
23005
23006 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23007 table if necessary. For convenience, return TYPE.
23008
23009 The DIEs reading must have careful ordering to:
23010 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23011 reading current DIE.
23012 * Not trying to dereference contents of still incompletely read in types
23013 while reading in other DIEs.
23014 * Enable referencing still incompletely read in types just by a pointer to
23015 the type without accessing its fields.
23016
23017 Therefore caller should follow these rules:
23018 * Try to fetch any prerequisite types we may need to build this DIE type
23019 before building the type and calling set_die_type.
23020 * After building type call set_die_type for current DIE as soon as
23021 possible before fetching more types to complete the current type.
23022 * Make the type as complete as possible before fetching more types. */
23023
23024 static struct type *
23025 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23026 {
23027 struct dwarf2_per_objfile *dwarf2_per_objfile
23028 = cu->per_cu->dwarf2_per_objfile;
23029 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23030 struct objfile *objfile = dwarf2_per_objfile->objfile;
23031 struct attribute *attr;
23032 struct dynamic_prop prop;
23033
23034 /* For Ada types, make sure that the gnat-specific data is always
23035 initialized (if not already set). There are a few types where
23036 we should not be doing so, because the type-specific area is
23037 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23038 where the type-specific area is used to store the floatformat).
23039 But this is not a problem, because the gnat-specific information
23040 is actually not needed for these types. */
23041 if (need_gnat_info (cu)
23042 && TYPE_CODE (type) != TYPE_CODE_FUNC
23043 && TYPE_CODE (type) != TYPE_CODE_FLT
23044 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23045 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23046 && TYPE_CODE (type) != TYPE_CODE_METHOD
23047 && !HAVE_GNAT_AUX_INFO (type))
23048 INIT_GNAT_SPECIFIC (type);
23049
23050 /* Read DW_AT_allocated and set in type. */
23051 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23052 if (attr != NULL && attr->form_is_block ())
23053 {
23054 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23055 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23056 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23057 }
23058 else if (attr != NULL)
23059 {
23060 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23061 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23062 sect_offset_str (die->sect_off));
23063 }
23064
23065 /* Read DW_AT_associated and set in type. */
23066 attr = dwarf2_attr (die, DW_AT_associated, cu);
23067 if (attr != NULL && attr->form_is_block ())
23068 {
23069 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23070 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23071 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23072 }
23073 else if (attr != NULL)
23074 {
23075 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23076 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23077 sect_offset_str (die->sect_off));
23078 }
23079
23080 /* Read DW_AT_data_location and set in type. */
23081 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23082 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23083 cu->per_cu->addr_type ()))
23084 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23085
23086 if (dwarf2_per_objfile->die_type_hash == NULL)
23087 dwarf2_per_objfile->die_type_hash
23088 = htab_up (htab_create_alloc (127,
23089 per_cu_offset_and_type_hash,
23090 per_cu_offset_and_type_eq,
23091 NULL, xcalloc, xfree));
23092
23093 ofs.per_cu = cu->per_cu;
23094 ofs.sect_off = die->sect_off;
23095 ofs.type = type;
23096 slot = (struct dwarf2_per_cu_offset_and_type **)
23097 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23098 if (*slot)
23099 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23100 sect_offset_str (die->sect_off));
23101 *slot = XOBNEW (&objfile->objfile_obstack,
23102 struct dwarf2_per_cu_offset_and_type);
23103 **slot = ofs;
23104 return type;
23105 }
23106
23107 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23108 or return NULL if the die does not have a saved type. */
23109
23110 static struct type *
23111 get_die_type_at_offset (sect_offset sect_off,
23112 struct dwarf2_per_cu_data *per_cu)
23113 {
23114 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23115 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23116
23117 if (dwarf2_per_objfile->die_type_hash == NULL)
23118 return NULL;
23119
23120 ofs.per_cu = per_cu;
23121 ofs.sect_off = sect_off;
23122 slot = ((struct dwarf2_per_cu_offset_and_type *)
23123 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23124 if (slot)
23125 return slot->type;
23126 else
23127 return NULL;
23128 }
23129
23130 /* Look up the type for DIE in CU in die_type_hash,
23131 or return NULL if DIE does not have a saved type. */
23132
23133 static struct type *
23134 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23135 {
23136 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23137 }
23138
23139 /* Add a dependence relationship from CU to REF_PER_CU. */
23140
23141 static void
23142 dwarf2_add_dependence (struct dwarf2_cu *cu,
23143 struct dwarf2_per_cu_data *ref_per_cu)
23144 {
23145 void **slot;
23146
23147 if (cu->dependencies == NULL)
23148 cu->dependencies
23149 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23150 NULL, &cu->comp_unit_obstack,
23151 hashtab_obstack_allocate,
23152 dummy_obstack_deallocate);
23153
23154 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23155 if (*slot == NULL)
23156 *slot = ref_per_cu;
23157 }
23158
23159 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23160 Set the mark field in every compilation unit in the
23161 cache that we must keep because we are keeping CU. */
23162
23163 static int
23164 dwarf2_mark_helper (void **slot, void *data)
23165 {
23166 struct dwarf2_per_cu_data *per_cu;
23167
23168 per_cu = (struct dwarf2_per_cu_data *) *slot;
23169
23170 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23171 reading of the chain. As such dependencies remain valid it is not much
23172 useful to track and undo them during QUIT cleanups. */
23173 if (per_cu->cu == NULL)
23174 return 1;
23175
23176 if (per_cu->cu->mark)
23177 return 1;
23178 per_cu->cu->mark = true;
23179
23180 if (per_cu->cu->dependencies != NULL)
23181 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23182
23183 return 1;
23184 }
23185
23186 /* Set the mark field in CU and in every other compilation unit in the
23187 cache that we must keep because we are keeping CU. */
23188
23189 static void
23190 dwarf2_mark (struct dwarf2_cu *cu)
23191 {
23192 if (cu->mark)
23193 return;
23194 cu->mark = true;
23195 if (cu->dependencies != NULL)
23196 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23197 }
23198
23199 static void
23200 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23201 {
23202 while (per_cu)
23203 {
23204 per_cu->cu->mark = false;
23205 per_cu = per_cu->cu->read_in_chain;
23206 }
23207 }
23208
23209 /* Trivial hash function for partial_die_info: the hash value of a DIE
23210 is its offset in .debug_info for this objfile. */
23211
23212 static hashval_t
23213 partial_die_hash (const void *item)
23214 {
23215 const struct partial_die_info *part_die
23216 = (const struct partial_die_info *) item;
23217
23218 return to_underlying (part_die->sect_off);
23219 }
23220
23221 /* Trivial comparison function for partial_die_info structures: two DIEs
23222 are equal if they have the same offset. */
23223
23224 static int
23225 partial_die_eq (const void *item_lhs, const void *item_rhs)
23226 {
23227 const struct partial_die_info *part_die_lhs
23228 = (const struct partial_die_info *) item_lhs;
23229 const struct partial_die_info *part_die_rhs
23230 = (const struct partial_die_info *) item_rhs;
23231
23232 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23233 }
23234
23235 struct cmd_list_element *set_dwarf_cmdlist;
23236 struct cmd_list_element *show_dwarf_cmdlist;
23237
23238 static void
23239 set_dwarf_cmd (const char *args, int from_tty)
23240 {
23241 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23242 gdb_stdout);
23243 }
23244
23245 static void
23246 show_dwarf_cmd (const char *args, int from_tty)
23247 {
23248 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23249 }
23250
23251 static void
23252 show_check_physname (struct ui_file *file, int from_tty,
23253 struct cmd_list_element *c, const char *value)
23254 {
23255 fprintf_filtered (file,
23256 _("Whether to check \"physname\" is %s.\n"),
23257 value);
23258 }
23259
23260 void _initialize_dwarf2_read ();
23261 void
23262 _initialize_dwarf2_read ()
23263 {
23264 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23265 Set DWARF specific variables.\n\
23266 Configure DWARF variables such as the cache size."),
23267 &set_dwarf_cmdlist, "maintenance set dwarf ",
23268 0/*allow-unknown*/, &maintenance_set_cmdlist);
23269
23270 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23271 Show DWARF specific variables.\n\
23272 Show DWARF variables such as the cache size."),
23273 &show_dwarf_cmdlist, "maintenance show dwarf ",
23274 0/*allow-unknown*/, &maintenance_show_cmdlist);
23275
23276 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23277 &dwarf_max_cache_age, _("\
23278 Set the upper bound on the age of cached DWARF compilation units."), _("\
23279 Show the upper bound on the age of cached DWARF compilation units."), _("\
23280 A higher limit means that cached compilation units will be stored\n\
23281 in memory longer, and more total memory will be used. Zero disables\n\
23282 caching, which can slow down startup."),
23283 NULL,
23284 show_dwarf_max_cache_age,
23285 &set_dwarf_cmdlist,
23286 &show_dwarf_cmdlist);
23287
23288 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23289 Set debugging of the DWARF reader."), _("\
23290 Show debugging of the DWARF reader."), _("\
23291 When enabled (non-zero), debugging messages are printed during DWARF\n\
23292 reading and symtab expansion. A value of 1 (one) provides basic\n\
23293 information. A value greater than 1 provides more verbose information."),
23294 NULL,
23295 NULL,
23296 &setdebuglist, &showdebuglist);
23297
23298 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23299 Set debugging of the DWARF DIE reader."), _("\
23300 Show debugging of the DWARF DIE reader."), _("\
23301 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23302 The value is the maximum depth to print."),
23303 NULL,
23304 NULL,
23305 &setdebuglist, &showdebuglist);
23306
23307 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23308 Set debugging of the dwarf line reader."), _("\
23309 Show debugging of the dwarf line reader."), _("\
23310 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23311 A value of 1 (one) provides basic information.\n\
23312 A value greater than 1 provides more verbose information."),
23313 NULL,
23314 NULL,
23315 &setdebuglist, &showdebuglist);
23316
23317 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23318 Set cross-checking of \"physname\" code against demangler."), _("\
23319 Show cross-checking of \"physname\" code against demangler."), _("\
23320 When enabled, GDB's internal \"physname\" code is checked against\n\
23321 the demangler."),
23322 NULL, show_check_physname,
23323 &setdebuglist, &showdebuglist);
23324
23325 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23326 no_class, &use_deprecated_index_sections, _("\
23327 Set whether to use deprecated gdb_index sections."), _("\
23328 Show whether to use deprecated gdb_index sections."), _("\
23329 When enabled, deprecated .gdb_index sections are used anyway.\n\
23330 Normally they are ignored either because of a missing feature or\n\
23331 performance issue.\n\
23332 Warning: This option must be enabled before gdb reads the file."),
23333 NULL,
23334 NULL,
23335 &setlist, &showlist);
23336
23337 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23338 &dwarf2_locexpr_funcs);
23339 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23340 &dwarf2_loclist_funcs);
23341
23342 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23343 &dwarf2_block_frame_base_locexpr_funcs);
23344 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23345 &dwarf2_block_frame_base_loclist_funcs);
23346
23347 #if GDB_SELF_TEST
23348 selftests::register_test ("dw2_expand_symtabs_matching",
23349 selftests::dw2_expand_symtabs_matching::run_test);
23350 selftests::register_test ("dwarf2_find_containing_comp_unit",
23351 selftests::find_containing_comp_unit::run_test);
23352 #endif
23353 }